world/layer: move Option into BlockInfo struct

Preparation for top_layer() cleanup.
This commit is contained in:
Matthias Schiffer 2023-05-05 23:29:57 +02:00
parent 263fc6d6ce
commit 2d5eec13c2
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C
2 changed files with 18 additions and 39 deletions

View file

@ -35,11 +35,11 @@ impl<'a> TileRenderer<'a> {
}; };
image::Rgba(match (&blocks[coords], &biomes[coords]) { image::Rgba(match (&blocks[coords], &biomes[coords]) {
( (
Some(world::layer::BlockInfo { world::layer::BlockInfo {
block_type, block_type: Some(block_type),
depth: Some(depth), depth: Some(depth),
.. ..
}), },
Some(biome), Some(biome),
) => block_color(*block_type, biome, depth.0 as f32), ) => block_color(*block_type, biome, depth.0 as f32),
_ => [0, 0, 0, 0], _ => [0, 0, 0, 0],

View file

@ -25,36 +25,23 @@ impl BlockHeight {
} }
} }
#[derive(Debug, Clone, Copy, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
pub struct BlockInfo { pub struct BlockInfo {
pub block_type: BlockType, pub block_type: Option<BlockType>,
pub depth: Option<BlockHeight>, pub depth: Option<BlockHeight>,
} }
/// Helper methods for [BlockInfo] pub type BlockInfoArray = LayerBlockArray<BlockInfo>;
trait OptionBlockInfoExt { pub type BiomeArray = LayerBlockArray<Option<Biome>>;
/// Checks if a [BlockInfo] has been filled in completely pub type BlockLightArray = LayerBlockArray<u8>;
///
/// Helper used by [top_layer]
fn done(&self) -> bool;
/// Fills in a [BlockInfo] based on a [BlockType] impl BlockInfo {
/// fn is_empty(&self) -> bool {
/// Only fills in data if the block is part of the visible top layer self.block_type.is_none()
/// 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 OptionBlockInfoExt for Option<BlockInfo> {
fn done(&self) -> bool { fn done(&self) -> bool {
let Some(info) = self else { self.depth.is_some()
return false;
};
info.depth.is_some()
} }
fn fill(&mut self, y: BlockHeight, block_type: BlockType) -> bool { fn fill(&mut self, y: BlockHeight, block_type: BlockType) -> bool {
@ -62,28 +49,20 @@ impl OptionBlockInfoExt for Option<BlockInfo> {
return false; return false;
} }
if self.is_none() { if self.block_type.is_none() {
*self = Some(BlockInfo { self.block_type = Some(block_type);
block_type,
depth: None,
});
} }
if block_type.is(BlockFlag::Water) { if block_type.is(BlockFlag::Water) {
return false; return false;
} }
let info = self.as_mut().unwrap(); self.depth = Some(y);
info.depth = Some(y);
true true
} }
} }
pub type BlockInfoArray = LayerBlockArray<Option<BlockInfo>>;
pub type BiomeArray = LayerBlockArray<Option<Biome>>;
pub type BlockLightArray = LayerBlockArray<u8>;
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct LayerData { pub struct LayerData {
pub blocks: Box<BlockInfoArray>, pub blocks: Box<BlockInfoArray>,
@ -139,11 +118,11 @@ pub fn top_layer(chunk: &Chunk) -> Result<Option<LayerData>> {
}; };
let biome_entry = &mut ret.biomes[xz]; 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(); *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); ret.block_light[xz] = block_light.block_light_at(coords);
} }