minedmap: move some internal functions out of impl blocks

This commit is contained in:
Matthias Schiffer 2023-05-01 17:51:38 +02:00
parent 1a5e8894fe
commit 2dd9283d95
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C
2 changed files with 30 additions and 28 deletions

View file

@ -6,6 +6,17 @@ use minedmap::{io::storage, resource, types::*, world};
use super::common::*;
/// Parses a filename in the format r.X.Z.mca into the contained X and Z values
fn parse_region_filename(path: &Path) -> Option<RegionCoords> {
let file_name = path.file_name()?.to_str()?;
let parts: Vec<_> = file_name.split('.').collect();
let &["r", x, z, "mca"] = parts.as_slice() else {
return None;
};
Some((x.parse().ok()?, z.parse().ok()?))
}
/// Type with methods for processing the regions of a Minecraft save directory
pub struct RegionProcessor<'a> {
block_types: resource::BlockTypes,
@ -22,17 +33,6 @@ impl<'a> RegionProcessor<'a> {
}
}
/// Parses a filename in the format r.X.Z.mca into the contained X and Z values
fn parse_region_filename(path: &Path) -> Option<RegionCoords> {
let file_name = path.file_name()?.to_str()?;
let parts: Vec<_> = file_name.split('.').collect();
let &["r", x, z, "mca"] = parts.as_slice() else {
return None;
};
Some((x.parse().ok()?, z.parse().ok()?))
}
/// Processes a single chunk
fn process_chunk(
&self,
@ -50,7 +50,9 @@ impl<'a> RegionProcessor<'a> {
world::layer::top_layer(&chunk)
}
fn chunk_lightmap(block_light: Box<world::layer::BlockLightArray>) -> image::GrayAlphaImage {
fn render_chunk_lightmap(
block_light: Box<world::layer::BlockLightArray>,
) -> image::GrayAlphaImage {
const N: u32 = BLOCKS_PER_CHUNK as u32;
image::GrayAlphaImage::from_fn(N, N, |x, z| {
@ -116,7 +118,7 @@ impl<'a> RegionProcessor<'a> {
};
processed_region[chunk_coords] = Some(processed_chunk);
let chunk_lightmap = Self::chunk_lightmap(block_light);
let chunk_lightmap = Self::render_chunk_lightmap(block_light);
overlay_chunk(&mut lightmap, &chunk_lightmap, chunk_coords);
Ok(())
@ -163,7 +165,7 @@ impl<'a> RegionProcessor<'a> {
.unwrap_or_default()
}) {
let path = entry.path();
let Some(coords) = Self::parse_region_filename(&path) else {
let Some(coords) = parse_region_filename(&path) else {
continue;
};

View file

@ -6,6 +6,19 @@ use minedmap::{io::storage, resource::Biome, types::*, world};
use super::common::*;
fn block_color(block: &world::layer::BlockInfo, _biome: &Biome) -> [u8; 4] {
let h = block
.depth
.map(|depth| 0.5 + 0.005 * depth.0 as f32)
.unwrap_or_default();
let c = block
.block_type
.color
.0
.map(|v| (f32::from(v) * h).clamp(0.0, 255.0) as u8);
[c[0], c[1], c[2], 255]
}
pub struct TileRenderer<'a> {
config: &'a Config,
}
@ -20,19 +33,6 @@ impl<'a> TileRenderer<'a> {
storage::read(&processed_path).context("Failed to load processed region data")
}
fn block_color(block: &world::layer::BlockInfo, _biome: &Biome) -> [u8; 4] {
let h = block
.depth
.map(|depth| 0.5 + 0.005 * depth.0 as f32)
.unwrap_or_default();
let c = block
.block_type
.color
.0
.map(|v| (f32::from(v) * h).clamp(0.0, 255.0) as u8);
[c[0], c[1], c[2], 255]
}
fn render_chunk(
image: &mut image::RgbaImage,
coords: ChunkCoords,
@ -47,7 +47,7 @@ impl<'a> TileRenderer<'a> {
z: BlockZ(z as u8),
};
image::Rgba(match (&blocks[coords], &biomes[coords]) {
(Some(block), Some(biome)) => Self::block_color(block, biome),
(Some(block), Some(biome)) => block_color(block, biome),
_ => [0, 0, 0, 0],
})
});