types: split BlockCoords into SectionBlockCoords and LayerBlockCoords

This commit is contained in:
Matthias Schiffer 2023-02-15 00:33:58 +01:00
parent 339e0b6a05
commit 921a218d56
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C
2 changed files with 24 additions and 12 deletions

View file

@ -39,15 +39,27 @@ coord_impl!(BlockY, BLOCKS_PER_CHUNK);
pub struct BlockZ(pub u8); pub struct BlockZ(pub u8);
coord_impl!(BlockZ, BLOCKS_PER_CHUNK); coord_impl!(BlockZ, BLOCKS_PER_CHUNK);
/// X, Y and Z coordinates of a block in a chunk section /// X and Z coordinates of a block in a chunk
#[derive(Clone, Copy, PartialEq, Eq)] #[derive(Clone, Copy, PartialEq, Eq)]
pub struct BlockCoords { pub struct LayerBlockCoords {
pub x: BlockX, pub x: BlockX,
pub y: BlockY,
pub z: BlockZ, pub z: BlockZ,
} }
impl BlockCoords { impl Debug for LayerBlockCoords {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "({}, {})", self.x.0, self.z.0)
}
}
/// X, Y and Z coordinates of a block in a chunk section
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct SectionBlockCoords {
pub xz: LayerBlockCoords,
pub y: BlockY,
}
impl SectionBlockCoords {
/// Computes a block's offset in various data structures /// Computes a block's offset in various data structures
/// ///
/// Many chunk data structures store block and biome data in the same /// Many chunk data structures store block and biome data in the same
@ -55,16 +67,16 @@ impl BlockCoords {
/// for the block at a given coordinate is stored. /// for the block at a given coordinate is stored.
pub fn offset(&self) -> usize { pub fn offset(&self) -> usize {
use BLOCKS_PER_CHUNK as N; use BLOCKS_PER_CHUNK as N;
let x = self.x.0 as usize; let x = self.xz.x.0 as usize;
let y = self.y.0 as usize; let y = self.y.0 as usize;
let z = self.z.0 as usize; let z = self.xz.z.0 as usize;
((y * N) + z) * N + x ((y * N) + z) * N + x
} }
} }
impl Debug for BlockCoords { impl Debug for SectionBlockCoords {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "({}, {}, {})", self.x.0, self.y.0, self.z.0) write!(f, "({}, {}, {})", self.xz.x.0, self.y.0, self.xz.z.0)
} }
} }

View file

@ -24,7 +24,7 @@ fn palette_bits(len: usize, min: u8, max: u8) -> Option<u8> {
/// Trait for common functions of [SectionV1_13] and [SectionV0] /// Trait for common functions of [SectionV1_13] and [SectionV0]
pub trait Section { pub trait Section {
fn block_id_at(&self, coords: BlockCoords) -> Result<&str>; fn block_id_at(&self, coords: SectionBlockCoords) -> Result<&str>;
} }
/// Minecraft v1.18+ section biome data /// Minecraft v1.18+ section biome data
@ -101,7 +101,7 @@ impl<'a> SectionV1_13<'a> {
} }
/// Looks up the block type palette index at the given coordinates /// Looks up the block type palette index at the given coordinates
fn palette_index_at(&self, coords: BlockCoords) -> usize { fn palette_index_at(&self, coords: SectionBlockCoords) -> usize {
let Some(block_states) = self.block_states else { let Some(block_states) = self.block_states else {
return 0; return 0;
}; };
@ -133,7 +133,7 @@ impl<'a> SectionV1_13<'a> {
} }
impl<'a> Section for SectionV1_13<'a> { impl<'a> Section for SectionV1_13<'a> {
fn block_id_at(&self, coords: BlockCoords) -> Result<&str> { fn block_id_at(&self, coords: SectionBlockCoords) -> Result<&str> {
let index = self.palette_index_at(coords); let index = self.palette_index_at(coords);
let entry = self let entry = self
.palette .palette
@ -167,7 +167,7 @@ impl<'a> SectionV0<'a> {
} }
impl<'a> Section for SectionV0<'a> { impl<'a> Section for SectionV0<'a> {
fn block_id_at(&self, coords: BlockCoords) -> Result<&str> { fn block_id_at(&self, coords: SectionBlockCoords) -> Result<&str> {
let offset = coords.offset(); let offset = coords.offset();
let block = self.blocks[offset] as u8; let block = self.blocks[offset] as u8;