2023-03-04 16:53:53 +01:00
|
|
|
use std::fmt::Debug;
|
|
|
|
|
2023-02-12 01:01:53 +01:00
|
|
|
use anyhow::{bail, Context, Result};
|
2023-02-12 23:07:55 +01:00
|
|
|
use num_integer::div_rem;
|
2023-02-12 01:01:53 +01:00
|
|
|
|
|
|
|
use super::de;
|
2023-03-01 23:14:57 +01:00
|
|
|
use crate::{
|
2023-03-02 00:19:01 +01:00
|
|
|
resource::{BlockType, BlockTypes},
|
2023-03-01 23:14:57 +01:00
|
|
|
types::*,
|
|
|
|
};
|
2023-02-12 01:01:53 +01:00
|
|
|
|
2023-02-12 20:59:23 +01:00
|
|
|
/// Determine the number of bits required for indexing into a palette of a given length
|
|
|
|
///
|
|
|
|
/// This is basically a base-2 logarithm, with clamping to a minimum value and
|
|
|
|
/// check against a maximum value. If the result would be greater than the passed
|
|
|
|
/// `max` value, [None] is returned.
|
2023-02-12 01:01:53 +01:00
|
|
|
fn palette_bits(len: usize, min: u8, max: u8) -> Option<u8> {
|
|
|
|
let mut bits = min;
|
|
|
|
while (1 << bits) < len {
|
|
|
|
bits += 1;
|
|
|
|
|
|
|
|
if bits > max {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(bits)
|
|
|
|
}
|
|
|
|
|
2023-02-12 22:54:43 +01:00
|
|
|
/// Trait for common functions of [SectionV1_13] and [SectionV0]
|
2023-03-04 16:53:53 +01:00
|
|
|
pub trait Section: Debug {
|
2023-03-02 00:19:01 +01:00
|
|
|
fn block_at(&self, coords: SectionBlockCoords) -> Result<Option<BlockType>>;
|
2023-02-12 20:04:31 +01:00
|
|
|
}
|
2023-02-12 16:19:41 +01:00
|
|
|
|
2023-02-12 20:59:23 +01:00
|
|
|
/// Minecraft v1.13+ section block data
|
2023-02-12 01:01:53 +01:00
|
|
|
#[derive(Debug)]
|
2023-02-12 22:54:43 +01:00
|
|
|
pub struct SectionV1_13<'a> {
|
2023-03-04 00:27:26 +01:00
|
|
|
block_states: Option<&'a [i64]>,
|
2023-03-02 00:37:25 +01:00
|
|
|
palette: Vec<Option<BlockType>>,
|
2023-02-12 01:01:53 +01:00
|
|
|
bits: u8,
|
|
|
|
aligned_blocks: bool,
|
|
|
|
}
|
|
|
|
|
2023-02-12 22:54:43 +01:00
|
|
|
impl<'a> SectionV1_13<'a> {
|
|
|
|
/// Constructs a new [SectionV1_13] from deserialized data structures
|
2023-02-12 01:01:53 +01:00
|
|
|
pub fn new(
|
|
|
|
data_version: u32,
|
2023-03-04 00:27:26 +01:00
|
|
|
block_states: Option<&'a [i64]>,
|
|
|
|
palette: &'a [de::BlockStatePaletteEntry],
|
2023-03-01 23:14:57 +01:00
|
|
|
block_types: &'a BlockTypes,
|
2023-02-12 01:01:53 +01:00
|
|
|
) -> Result<Self> {
|
|
|
|
let aligned_blocks = data_version >= 2529;
|
|
|
|
|
|
|
|
let bits = palette_bits(palette.len(), 4, 12).context("Unsupported block palette size")?;
|
|
|
|
|
|
|
|
if let Some(block_states) = block_states {
|
|
|
|
let expected_length = if aligned_blocks {
|
|
|
|
let blocks_per_word = 64 / bits as usize;
|
|
|
|
(4096 + blocks_per_word - 1) / blocks_per_word
|
|
|
|
} else {
|
|
|
|
64 * bits as usize
|
|
|
|
};
|
|
|
|
if block_states.len() != expected_length {
|
|
|
|
bail!("Invalid section block data");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-02 00:37:25 +01:00
|
|
|
let palette_types = palette
|
|
|
|
.iter()
|
|
|
|
.map(|entry| {
|
|
|
|
let block_type = block_types.get(&entry.name);
|
|
|
|
if block_type.is_none() {
|
|
|
|
eprintln!("Unknown block type: {}", entry.name);
|
|
|
|
}
|
|
|
|
block_type
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2023-02-12 01:01:53 +01:00
|
|
|
Ok(Self {
|
|
|
|
block_states,
|
2023-03-02 00:37:25 +01:00
|
|
|
palette: palette_types,
|
2023-02-12 01:01:53 +01:00
|
|
|
bits,
|
|
|
|
aligned_blocks,
|
|
|
|
})
|
|
|
|
}
|
2023-02-12 20:04:31 +01:00
|
|
|
|
2023-02-12 20:59:23 +01:00
|
|
|
/// Looks up the block type palette index at the given coordinates
|
2023-02-15 00:33:58 +01:00
|
|
|
fn palette_index_at(&self, coords: SectionBlockCoords) -> usize {
|
2023-02-12 20:04:31 +01:00
|
|
|
let Some(block_states) = self.block_states else {
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
let bits = self.bits as usize;
|
|
|
|
let mask = (1 << bits) - 1;
|
|
|
|
|
|
|
|
let offset = coords.offset();
|
|
|
|
|
|
|
|
let shifted = if self.aligned_blocks {
|
|
|
|
let blocks_per_word = 64 / bits;
|
2023-02-12 23:07:55 +01:00
|
|
|
let (word, shift) = div_rem(offset, blocks_per_word);
|
2023-02-12 20:04:31 +01:00
|
|
|
block_states[word] as u64 >> (shift * bits)
|
|
|
|
} else {
|
|
|
|
let bit_offset = offset * bits;
|
2023-02-12 23:07:55 +01:00
|
|
|
let (word, bit_shift) = div_rem(bit_offset, 64);
|
2023-02-12 20:04:31 +01:00
|
|
|
|
2023-02-18 10:55:28 +01:00
|
|
|
let mut tmp = (block_states[word] as u64) >> bit_shift;
|
|
|
|
if bit_shift + bits > 64 {
|
|
|
|
tmp |= (block_states[word + 1] as u64) << (64 - bit_shift);
|
2023-02-12 20:04:31 +01:00
|
|
|
}
|
2023-02-18 10:55:28 +01:00
|
|
|
tmp
|
2023-02-12 20:04:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
(shifted & mask) as usize
|
|
|
|
}
|
2023-02-12 01:01:53 +01:00
|
|
|
}
|
|
|
|
|
2023-02-12 22:54:43 +01:00
|
|
|
impl<'a> Section for SectionV1_13<'a> {
|
2023-03-02 00:19:01 +01:00
|
|
|
fn block_at(&self, coords: SectionBlockCoords) -> Result<Option<BlockType>> {
|
2023-02-12 23:36:58 +01:00
|
|
|
let index = self.palette_index_at(coords);
|
2023-03-02 00:37:25 +01:00
|
|
|
Ok(*self
|
2023-02-12 20:04:31 +01:00
|
|
|
.palette
|
|
|
|
.get(index)
|
2023-03-02 00:37:25 +01:00
|
|
|
.context("Palette index out of bounds")?)
|
2023-02-12 20:04:31 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-12 16:19:41 +01:00
|
|
|
|
2023-02-12 20:59:23 +01:00
|
|
|
/// Pre-1.13 section block data
|
2023-02-12 01:01:53 +01:00
|
|
|
#[derive(Debug)]
|
2023-02-12 22:54:43 +01:00
|
|
|
pub struct SectionV0<'a> {
|
2023-03-04 00:27:26 +01:00
|
|
|
blocks: &'a [i8],
|
|
|
|
data: &'a [i8],
|
2023-03-02 00:19:01 +01:00
|
|
|
block_types: &'a BlockTypes,
|
2023-02-12 01:01:53 +01:00
|
|
|
}
|
|
|
|
|
2023-02-12 22:54:43 +01:00
|
|
|
impl<'a> SectionV0<'a> {
|
|
|
|
/// Constructs a new [SectionV0] from deserialized data structures
|
2023-03-04 00:27:26 +01:00
|
|
|
pub fn new(blocks: &'a [i8], data: &'a [i8], block_types: &'a BlockTypes) -> Result<Self> {
|
2023-02-12 20:17:20 +01:00
|
|
|
use BLOCKS_PER_CHUNK as N;
|
|
|
|
|
2023-02-12 20:04:31 +01:00
|
|
|
if blocks.len() != N * N * N {
|
|
|
|
bail!("Invalid section block data");
|
|
|
|
}
|
|
|
|
if data.len() != N * N * N / 2 {
|
|
|
|
bail!("Invalid section extra data");
|
|
|
|
}
|
|
|
|
|
2023-03-01 23:14:57 +01:00
|
|
|
Ok(SectionV0 {
|
|
|
|
blocks,
|
|
|
|
data,
|
2023-03-02 00:19:01 +01:00
|
|
|
block_types,
|
2023-03-01 23:14:57 +01:00
|
|
|
})
|
2023-02-12 01:01:53 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-12 16:19:41 +01:00
|
|
|
|
2023-02-12 22:54:43 +01:00
|
|
|
impl<'a> Section for SectionV0<'a> {
|
2023-03-02 00:19:01 +01:00
|
|
|
fn block_at(&self, coords: SectionBlockCoords) -> Result<Option<BlockType>> {
|
2023-02-12 20:04:31 +01:00
|
|
|
let offset = coords.offset();
|
|
|
|
let block = self.blocks[offset] as u8;
|
|
|
|
|
2023-02-12 23:07:55 +01:00
|
|
|
let (data_offset, data_nibble) = div_rem(offset, 2);
|
2023-02-12 20:04:31 +01:00
|
|
|
let data_byte = self.data[data_offset] as u8;
|
|
|
|
let data = if data_nibble == 1 {
|
|
|
|
data_byte >> 4
|
|
|
|
} else {
|
|
|
|
data_byte & 0xf
|
|
|
|
};
|
|
|
|
|
2023-03-02 00:19:01 +01:00
|
|
|
Ok(self.block_types.get_legacy(block, data))
|
2023-02-12 20:04:31 +01:00
|
|
|
}
|
|
|
|
}
|
2023-03-04 16:58:52 +01:00
|
|
|
|
|
|
|
/// Minecraft v1.18+ section biome data
|
|
|
|
///
|
|
|
|
/// The biome data is part of the section structure in Minecraft v1.18+, with
|
|
|
|
/// the biomes laid out as an array of indices into a palette, similar to the
|
|
|
|
/// v1.13+ block data.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct BiomesV18<'a> {
|
|
|
|
_biomes: Option<&'a [i64]>,
|
|
|
|
_palette: &'a [String],
|
|
|
|
_bits: u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> BiomesV18<'a> {
|
|
|
|
/// Constructs a new [BiomesV18] from deserialized data structures
|
|
|
|
pub fn new(biomes: Option<&'a [i64]>, palette: &'a [String]) -> Result<Self> {
|
|
|
|
let bits = palette_bits(palette.len(), 1, 6).context("Unsupported block palette size")?;
|
|
|
|
|
|
|
|
if let Some(biomes) = biomes {
|
|
|
|
let biomes_per_word = 64 / bits as usize;
|
|
|
|
let expected_length = (64 + biomes_per_word - 1) / biomes_per_word;
|
|
|
|
if biomes.len() != expected_length {
|
|
|
|
bail!("Invalid section biome data");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(BiomesV18 {
|
|
|
|
_biomes: biomes,
|
|
|
|
_palette: palette,
|
|
|
|
_bits: bits,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-03-04 17:03:22 +01:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub struct BlockLight<'a>(Option<&'a [i8]>);
|
|
|
|
|
|
|
|
impl<'a> BlockLight<'a> {
|
|
|
|
pub fn new(block_light: Option<&'a [i8]>) -> Result<Self> {
|
|
|
|
use BLOCKS_PER_CHUNK as N;
|
|
|
|
if let Some(block_light) = block_light {
|
|
|
|
if block_light.len() != N * N * N / 2 {
|
|
|
|
bail!("Invalid section block light data");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(BlockLight(block_light))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn block_light_at(&self, coords: SectionBlockCoords) -> u8 {
|
|
|
|
let Some(block_light) = self.0 else {
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
let (offset, nibble) = div_rem(coords.offset(), 2);
|
|
|
|
let byte = block_light[offset] as u8;
|
|
|
|
|
|
|
|
if nibble == 1 {
|
|
|
|
byte >> 4
|
|
|
|
} else {
|
|
|
|
byte & 0xf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|