From 921a218d561095d4d077c34edb8f276984f8605a Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 15 Feb 2023 00:33:58 +0100 Subject: [PATCH] types: split BlockCoords into SectionBlockCoords and LayerBlockCoords --- src/types.rs | 28 ++++++++++++++++++++-------- src/world/section.rs | 8 ++++---- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/types.rs b/src/types.rs index 765ed39..39b106f 100644 --- a/src/types.rs +++ b/src/types.rs @@ -39,15 +39,27 @@ coord_impl!(BlockY, BLOCKS_PER_CHUNK); pub struct BlockZ(pub u8); 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)] -pub struct BlockCoords { +pub struct LayerBlockCoords { pub x: BlockX, - pub y: BlockY, 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 /// /// 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. pub fn offset(&self) -> usize { 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 z = self.z.0 as usize; + let z = self.xz.z.0 as usize; ((y * N) + z) * N + x } } -impl Debug for BlockCoords { +impl Debug for SectionBlockCoords { 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) } } diff --git a/src/world/section.rs b/src/world/section.rs index e59daba..cee8b2a 100644 --- a/src/world/section.rs +++ b/src/world/section.rs @@ -24,7 +24,7 @@ fn palette_bits(len: usize, min: u8, max: u8) -> Option { /// Trait for common functions of [SectionV1_13] and [SectionV0] 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 @@ -101,7 +101,7 @@ impl<'a> SectionV1_13<'a> { } /// 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 { return 0; }; @@ -133,7 +133,7 @@ impl<'a> 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 entry = self .palette @@ -167,7 +167,7 @@ impl<'a> 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 block = self.blocks[offset] as u8;