From 4fd316f3fceba0b5cdf97e349a88b937772573ea Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 4 Mar 2023 20:33:10 +0100 Subject: [PATCH] world/layer: return None from top_layer() for empty chunks Allow ignoring these chunks for the light map as well. --- src/main.rs | 12 +++++++++--- src/world/chunk.rs | 9 +++++++++ src/world/layer.rs | 8 ++++++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 899e772..2ae6be3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -98,7 +98,10 @@ impl<'a> RegionProcessor<'a> { } /// Processes a single chunk - fn process_chunk(&self, data: world::de::Chunk) -> Result> { + fn process_chunk( + &self, + data: world::de::Chunk, + ) -> Result>> { let chunk = world::chunk::Chunk::new(&data, &self.block_types)?; world::layer::top_layer(&chunk) } @@ -127,9 +130,12 @@ impl<'a> RegionProcessor<'a> { minedmap::io::region::from_file(path)?.foreach_chunk( |chunk_coords, data: world::de::Chunk| { - let processed_chunk = self + let Some(processed_chunk) = self .process_chunk(data) - .with_context(|| format!("Failed to process chunk {:?}", chunk_coords))?; + .with_context(|| format!("Failed to process chunk {:?}", chunk_coords))? + else { + return Ok(()); + }; processed_region[chunk_coords] = Some(processed_chunk); Ok(()) }, diff --git a/src/world/chunk.rs b/src/world/chunk.rs index 984d8fe..10efc6a 100644 --- a/src/world/chunk.rs +++ b/src/world/chunk.rs @@ -179,6 +179,15 @@ impl<'a> Chunk<'a> { ) } + pub fn is_empty(&self) -> bool { + match self { + Chunk::V1_18 { section_map } => section_map.is_empty(), + Chunk::V1_13 { section_map, .. } => section_map.is_empty(), + Chunk::V0 { section_map, .. } => section_map.is_empty(), + Chunk::Empty => true, + } + } + /// Returns an interator over the chunk's sections and their Y coordinates pub fn sections(&self) -> SectionIter { use SectionIterInner::*; diff --git a/src/world/layer.rs b/src/world/layer.rs index 2fd879b..8f9897a 100644 --- a/src/world/layer.rs +++ b/src/world/layer.rs @@ -91,9 +91,13 @@ 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) -> Result> { +pub fn top_layer(chunk: &Chunk) -> Result>> { use BLOCKS_PER_CHUNK as N; + if chunk.is_empty() { + return Ok(None); + } + let mut done = 0; let mut ret = Box::::default(); @@ -129,5 +133,5 @@ pub fn top_layer(chunk: &Chunk) -> Result> { } } - Ok(ret) + Ok(Some(ret)) }