world/layer: introduce LayerData struct to fix clippy type complexity warning

This commit is contained in:
Matthias Schiffer 2023-05-05 23:04:06 +02:00
parent b66bdf5ce1
commit 924ee01f91
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C
2 changed files with 23 additions and 26 deletions

View file

@ -2,7 +2,12 @@ use std::{fs, path::Path};
use anyhow::{Context, Result};
use minedmap::{io::storage, resource, types::*, world};
use minedmap::{
io::storage,
resource,
types::*,
world::{self, layer::LayerData},
};
use super::common::*;
@ -34,18 +39,7 @@ impl<'a> RegionProcessor<'a> {
}
/// Processes a single chunk
fn process_chunk(
&self,
data: world::de::Chunk,
) -> Result<
Option<(
(
Box<world::layer::BlockInfoArray>,
Box<world::layer::BiomeArray>,
),
Box<world::layer::BlockLightArray>,
)>,
> {
fn process_chunk(&self, data: world::de::Chunk) -> Result<Option<LayerData>> {
let chunk = world::chunk::Chunk::new(&data, &self.block_types, &self.biome_types)?;
world::layer::top_layer(&chunk)
}
@ -110,15 +104,15 @@ impl<'a> RegionProcessor<'a> {
minedmap::io::region::from_file(path)?.foreach_chunk(
|chunk_coords, data: world::de::Chunk| {
let Some((processed_chunk, block_light)) = self
let Some(layer_data) = self
.process_chunk(data)
.with_context(|| format!("Failed to process chunk {:?}", chunk_coords))?
else {
return Ok(());
};
processed_region[chunk_coords] = Some(processed_chunk);
processed_region[chunk_coords] = Some((layer_data.blocks, layer_data.biomes));
let chunk_lightmap = Self::render_chunk_lightmap(block_light);
let chunk_lightmap = Self::render_chunk_lightmap(layer_data.block_light);
overlay_chunk(&mut lightmap, &chunk_lightmap, chunk_coords);
Ok(())

View file

@ -86,6 +86,13 @@ pub type BlockInfoArray = LayerBlockArray<Option<BlockInfo>>;
pub type BiomeArray = LayerBlockArray<Option<Biome>>;
pub type BlockLightArray = LayerBlockArray<u8>;
#[derive(Debug, Default)]
pub struct LayerData {
pub blocks: Box<BlockInfoArray>,
pub biomes: Box<BiomeArray>,
pub block_light: Box<BlockLightArray>,
}
/// Fills in a [BlockInfoArray] with the information of the chunk's top
/// block layer
///
@ -93,9 +100,7 @@ pub type BlockLightArray = LayerBlockArray<u8>;
/// 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<Option<((Box<BlockInfoArray>, Box<BiomeArray>), Box<BlockLightArray>)>> {
pub fn top_layer(chunk: &Chunk) -> Result<Option<LayerData>> {
use BLOCKS_PER_CHUNK as N;
if chunk.is_empty() {
@ -103,9 +108,7 @@ pub fn top_layer(
}
let mut done = 0;
let mut blocks = Box::<BlockInfoArray>::default();
let mut block_biomes = Box::<BiomeArray>::default();
let mut light = Box::<BlockLightArray>::default();
let mut ret = LayerData::default();
for SectionIterItem {
y: section_y,
@ -116,7 +119,7 @@ pub fn top_layer(
{
for y in BlockY::iter().rev() {
for xz in BlockInfoArray::keys() {
let entry = &mut blocks[xz];
let entry = &mut ret.blocks[xz];
if entry.done() {
continue;
}
@ -137,13 +140,13 @@ pub fn top_layer(
done += 1;
};
let biome_entry = &mut block_biomes[xz];
let biome_entry = &mut ret.biomes[xz];
if !entry.is_none() && biome_entry.is_none() {
*biome_entry = biomes.biome_at(section_y, coords)?.copied();
}
if entry.is_none() {
light[xz] = block_light.block_light_at(coords);
ret.block_light[xz] = block_light.block_light_at(coords);
}
if done == N * N {
@ -153,5 +156,5 @@ pub fn top_layer(
}
}
Ok(Some(((blocks, block_biomes), light)))
Ok(Some(ret))
}