From 2d5eec13c2e0b0cb9fea2ba9d8097fba38573e4c Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 5 May 2023 23:29:57 +0200 Subject: [PATCH] world/layer: move Option into BlockInfo struct Preparation for top_layer() cleanup. --- src/bin/minedmap/tile_renderer.rs | 6 ++-- src/world/layer.rs | 51 +++++++++---------------------- 2 files changed, 18 insertions(+), 39 deletions(-) diff --git a/src/bin/minedmap/tile_renderer.rs b/src/bin/minedmap/tile_renderer.rs index 64c9d55..ac1193a 100644 --- a/src/bin/minedmap/tile_renderer.rs +++ b/src/bin/minedmap/tile_renderer.rs @@ -35,11 +35,11 @@ impl<'a> TileRenderer<'a> { }; image::Rgba(match (&blocks[coords], &biomes[coords]) { ( - Some(world::layer::BlockInfo { - block_type, + world::layer::BlockInfo { + block_type: Some(block_type), depth: Some(depth), .. - }), + }, Some(biome), ) => block_color(*block_type, biome, depth.0 as f32), _ => [0, 0, 0, 0], diff --git a/src/world/layer.rs b/src/world/layer.rs index 4365e1f..7c8b9bb 100644 --- a/src/world/layer.rs +++ b/src/world/layer.rs @@ -25,36 +25,23 @@ impl BlockHeight { } } -#[derive(Debug, Clone, Copy, Serialize, Deserialize)] +#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)] pub struct BlockInfo { - pub block_type: BlockType, + pub block_type: Option, pub depth: Option, } -/// Helper methods for [BlockInfo] -trait OptionBlockInfoExt { - /// Checks if a [BlockInfo] has been filled in completely - /// - /// Helper used by [top_layer] - fn done(&self) -> bool; +pub type BlockInfoArray = LayerBlockArray; +pub type BiomeArray = LayerBlockArray>; +pub type BlockLightArray = LayerBlockArray; - /// Fills in a [BlockInfo] based on a [BlockType] - /// - /// Only fills in data if the block is part of the visible top layer - /// of the rendered map. - /// - /// Must be called on an incomplete [BlockInfo] entry. Returns `true` - /// if the entry has been filled in completely. - fn fill(&mut self, y: BlockHeight, block_type: BlockType) -> bool; -} +impl BlockInfo { + fn is_empty(&self) -> bool { + self.block_type.is_none() + } -impl OptionBlockInfoExt for Option { fn done(&self) -> bool { - let Some(info) = self else { - return false; - }; - - info.depth.is_some() + self.depth.is_some() } fn fill(&mut self, y: BlockHeight, block_type: BlockType) -> bool { @@ -62,28 +49,20 @@ impl OptionBlockInfoExt for Option { return false; } - if self.is_none() { - *self = Some(BlockInfo { - block_type, - depth: None, - }); + if self.block_type.is_none() { + self.block_type = Some(block_type); } if block_type.is(BlockFlag::Water) { return false; } - let info = self.as_mut().unwrap(); - info.depth = Some(y); + self.depth = Some(y); true } } -pub type BlockInfoArray = LayerBlockArray>; -pub type BiomeArray = LayerBlockArray>; -pub type BlockLightArray = LayerBlockArray; - #[derive(Debug, Default)] pub struct LayerData { pub blocks: Box, @@ -139,11 +118,11 @@ pub fn top_layer(chunk: &Chunk) -> Result> { }; let biome_entry = &mut ret.biomes[xz]; - if !entry.is_none() && biome_entry.is_none() { + if !entry.is_empty() && biome_entry.is_none() { *biome_entry = biomes.biome_at(section_y, coords)?.copied(); } - if entry.is_none() { + if entry.is_empty() { ret.block_light[xz] = block_light.block_light_at(coords); }