world/layer: return None from top_layer() for empty chunks

Allow ignoring these chunks for the light map as well.
This commit is contained in:
Matthias Schiffer 2023-03-04 20:33:10 +01:00
parent 202364bfca
commit 4fd316f3fc
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C
3 changed files with 24 additions and 5 deletions

View file

@ -98,7 +98,10 @@ impl<'a> RegionProcessor<'a> {
} }
/// Processes a single chunk /// Processes a single chunk
fn process_chunk(&self, data: world::de::Chunk) -> Result<Box<world::layer::BlockInfoArray>> { fn process_chunk(
&self,
data: world::de::Chunk,
) -> Result<Option<Box<world::layer::BlockInfoArray>>> {
let chunk = world::chunk::Chunk::new(&data, &self.block_types)?; let chunk = world::chunk::Chunk::new(&data, &self.block_types)?;
world::layer::top_layer(&chunk) world::layer::top_layer(&chunk)
} }
@ -127,9 +130,12 @@ impl<'a> RegionProcessor<'a> {
minedmap::io::region::from_file(path)?.foreach_chunk( minedmap::io::region::from_file(path)?.foreach_chunk(
|chunk_coords, data: world::de::Chunk| { |chunk_coords, data: world::de::Chunk| {
let processed_chunk = self let Some(processed_chunk) = self
.process_chunk(data) .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); processed_region[chunk_coords] = Some(processed_chunk);
Ok(()) Ok(())
}, },

View file

@ -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 /// Returns an interator over the chunk's sections and their Y coordinates
pub fn sections(&self) -> SectionIter { pub fn sections(&self) -> SectionIter {
use SectionIterInner::*; use SectionIterInner::*;

View file

@ -91,9 +91,13 @@ pub type BlockInfoArray = LayerBlockArray<Option<BlockInfo>>;
/// determined as the block that should be visible on the rendered /// determined as the block that should be visible on the rendered
/// map. For water blocks, the height of the first non-water block /// map. For water blocks, the height of the first non-water block
/// is additionally filled in as the water depth. /// is additionally filled in as the water depth.
pub fn top_layer(chunk: &Chunk) -> Result<Box<BlockInfoArray>> { pub fn top_layer(chunk: &Chunk) -> Result<Option<Box<BlockInfoArray>>> {
use BLOCKS_PER_CHUNK as N; use BLOCKS_PER_CHUNK as N;
if chunk.is_empty() {
return Ok(None);
}
let mut done = 0; let mut done = 0;
let mut ret = Box::<BlockInfoArray>::default(); let mut ret = Box::<BlockInfoArray>::default();
@ -129,5 +133,5 @@ pub fn top_layer(chunk: &Chunk) -> Result<Box<BlockInfoArray>> {
} }
} }
Ok(ret) Ok(Some(ret))
} }