From 5c82b80924d4c2b093ad8b71faffeb7df851c4f0 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 2 Mar 2023 00:19:01 +0100 Subject: [PATCH] world/section: replace block_id_at() method with block_at() Directly return a BlockType, preparing for early lookup of palettes. --- src/main.rs | 2 +- src/world/layer.rs | 8 +++----- src/world/section.rs | 25 +++++++++++++++---------- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index c351230..80f2b25 100644 --- a/src/main.rs +++ b/src/main.rs @@ -76,7 +76,7 @@ impl<'a> RegionProcessor<'a> { /// Processes a single chunk fn process_chunk(&self, data: world::de::Chunk) -> Result> { let chunk = world::chunk::Chunk::new(&data, &self.block_types)?; - world::layer::top_layer(&chunk, &self.block_types) + world::layer::top_layer(&chunk) } fn save_region( diff --git a/src/world/layer.rs b/src/world/layer.rs index 0039136..a5d8383 100644 --- a/src/world/layer.rs +++ b/src/world/layer.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use super::chunk::Chunk; use crate::{ - resource::{BlockFlag, BlockType, BlockTypes}, + resource::{BlockFlag, BlockType}, types::*, }; @@ -92,7 +92,7 @@ pub type BlockInfoArray = LayerBlockArray>; /// determined as the block that should be visible on the rendered /// map. For water blocks, the height of the first non-water block /// is additionally filled in as the water depth. -pub fn top_layer(chunk: &Chunk, block_types: &BlockTypes) -> Result> { +pub fn top_layer(chunk: &Chunk) -> Result> { use BLOCKS_PER_CHUNK as N; let mut done = 0; @@ -109,9 +109,7 @@ pub fn top_layer(chunk: &Chunk, block_types: &BlockTypes) -> Result Option { /// Trait for common functions of [SectionV1_13] and [SectionV0] pub trait Section { - fn block_id_at(&self, coords: SectionBlockCoords) -> Result<&str>; + fn block_at(&self, coords: SectionBlockCoords) -> Result>; } /// Minecraft v1.18+ section biome data @@ -68,7 +68,7 @@ impl<'a> BiomesV18<'a> { pub struct SectionV1_13<'a> { block_states: Option<&'a fastnbt::LongArray>, palette: &'a Vec, - _block_types: &'a BlockTypes, + block_types: &'a BlockTypes, bits: u8, aligned_blocks: bool, } @@ -100,7 +100,7 @@ impl<'a> SectionV1_13<'a> { Ok(Self { block_states, palette, - _block_types: block_types, + block_types, bits, aligned_blocks, }) @@ -137,13 +137,18 @@ impl<'a> SectionV1_13<'a> { } impl<'a> Section for SectionV1_13<'a> { - fn block_id_at(&self, coords: SectionBlockCoords) -> Result<&str> { + fn block_at(&self, coords: SectionBlockCoords) -> Result> { let index = self.palette_index_at(coords); let entry = self .palette .get(index) .context("Palette index out of bounds")?; - Ok(&entry.name) + + let block_type = self.block_types.get(&entry.name); + if block_type.is_none() { + eprintln!("Unknown block type: {}", entry.name); + } + Ok(block_type) } } @@ -152,7 +157,7 @@ impl<'a> Section for SectionV1_13<'a> { pub struct SectionV0<'a> { blocks: &'a fastnbt::ByteArray, data: &'a fastnbt::ByteArray, - _block_types: &'a BlockTypes, + block_types: &'a BlockTypes, } impl<'a> SectionV0<'a> { @@ -174,13 +179,13 @@ impl<'a> SectionV0<'a> { Ok(SectionV0 { blocks, data, - _block_types: block_types, + block_types, }) } } impl<'a> Section for SectionV0<'a> { - fn block_id_at(&self, coords: SectionBlockCoords) -> Result<&str> { + fn block_at(&self, coords: SectionBlockCoords) -> Result> { let offset = coords.offset(); let block = self.blocks[offset] as u8; @@ -192,6 +197,6 @@ impl<'a> Section for SectionV0<'a> { data_byte & 0xf }; - Ok(resource::LEGACY_BLOCK_TYPES[block as usize][data as usize]) + Ok(self.block_types.get_legacy(block, data)) } }