world: pass biome data into chunk/section structures

This commit is contained in:
Matthias Schiffer 2023-04-07 09:29:01 +02:00
parent 524b1b1913
commit e117c76937
3 changed files with 51 additions and 19 deletions

View file

@ -96,6 +96,7 @@ where
/// Type with methods for processing the regions of a Minecraft save directory
struct RegionProcessor<'a> {
block_types: resource::BlockTypes,
biome_types: resource::BiomeTypes,
config: &'a Config,
}
@ -103,6 +104,7 @@ impl<'a> RegionProcessor<'a> {
fn new(config: &'a Config) -> Self {
RegionProcessor {
block_types: resource::BlockTypes::default(),
biome_types: resource::BiomeTypes::default(),
config,
}
}
@ -128,7 +130,7 @@ impl<'a> RegionProcessor<'a> {
Box<world::layer::BlockLightArray>,
)>,
> {
let chunk = world::chunk::Chunk::new(&data, &self.block_types)?;
let chunk = world::chunk::Chunk::new(&data, &self.block_types, &self.biome_types)?;
world::layer::top_layer(&chunk)
}

View file

@ -6,7 +6,10 @@ use std::{
use anyhow::{bail, Context, Result};
use super::{de, section::*};
use crate::{resource::BlockTypes, types::*};
use crate::{
resource::{BiomeTypes, BlockTypes},
types::*,
};
/// Chunk data structure wrapping a [de::Chunk] for convenient access to
/// block and biome data
@ -64,14 +67,20 @@ pub struct SectionIter<'a> {
impl<'a> Chunk<'a> {
/// Creates a new [Chunk] from a deserialized [de::Chunk]
pub fn new(data: &'a de::Chunk, block_types: &'a BlockTypes) -> Result<Self> {
pub fn new(
data: &'a de::Chunk,
block_types: &'a BlockTypes,
biome_types: &'a BiomeTypes,
) -> Result<Self> {
let data_version = data.data_version.unwrap_or_default();
match &data.chunk {
de::ChunkVariants::V1_18 { sections } => {
Self::new_v1_18(data_version, sections, block_types)
Self::new_v1_18(data_version, sections, block_types, biome_types)
}
de::ChunkVariants::V0 { level } => {
Self::new_v0(data_version, level, block_types, biome_types)
}
de::ChunkVariants::V0 { level } => Self::new_v0(data_version, level, block_types),
}
}
@ -80,6 +89,7 @@ impl<'a> Chunk<'a> {
data_version: u32,
sections: &'a Vec<de::SectionV1_18>,
block_types: &'a BlockTypes,
biome_types: &'a BiomeTypes,
) -> Result<Self> {
let mut section_map = BTreeMap::new();
@ -94,10 +104,12 @@ impl<'a> Chunk<'a> {
block_types,
)
.with_context(|| format!("Failed to load section at Y={}", section.y))?,
BiomesV18::new(section.biomes.data.as_deref(), &section.biomes.palette)
.with_context(|| {
format!("Failed to load section biomes at Y={}", section.y)
})?,
BiomesV18::new(
section.biomes.data.as_deref(),
&section.biomes.palette,
biome_types,
)
.with_context(|| format!("Failed to load section biomes at Y={}", section.y))?,
BlockLight::new(section.block_light.as_deref()).with_context(|| {
format!("Failed to load section block light at Y={}", section.y)
})?,
@ -113,6 +125,7 @@ impl<'a> Chunk<'a> {
data_version: u32,
level: &'a de::LevelV0,
block_types: &'a BlockTypes,
biome_types: &'a BiomeTypes,
) -> Result<Self> {
let mut section_map_v1_13 = BTreeMap::new();
let mut section_map_v0 = BTreeMap::new();
@ -158,7 +171,7 @@ impl<'a> Chunk<'a> {
}
}
let biomes = BiomesV0::new(level.biomes.as_ref());
let biomes = BiomesV0::new(level.biomes.as_ref(), biome_types);
Ok(
match (section_map_v1_13.is_empty(), section_map_v0.is_empty()) {

View file

@ -5,7 +5,7 @@ use num_integer::div_rem;
use super::de;
use crate::{
resource::{BlockType, BlockTypes},
resource::{BiomeTypes, BlockType, BlockTypes},
types::*,
};
@ -183,7 +183,11 @@ pub struct BiomesV18<'a> {
impl<'a> BiomesV18<'a> {
/// Constructs a new [BiomesV18] from deserialized data structures
pub fn new(biomes: Option<&'a [i64]>, palette: &'a [String]) -> Result<Self> {
pub fn new(
biomes: Option<&'a [i64]>,
palette: &'a [String],
_biome_types: &'a BiomeTypes,
) -> Result<Self> {
let bits = palette_bits(palette.len(), 1, 6).context("Unsupported block palette size")?;
if let Some(biomes) = biomes {
@ -208,14 +212,23 @@ impl<'a> BiomesV18<'a> {
/// different pre-v1.18 Minecraft versions
#[derive(Debug)]
pub enum BiomesV0<'a> {
IntArrayV15(&'a fastnbt::IntArray),
IntArrayV0(&'a fastnbt::IntArray),
ByteArray(&'a fastnbt::ByteArray),
IntArrayV15 {
data: &'a fastnbt::IntArray,
biome_types: &'a BiomeTypes,
},
IntArrayV0 {
data: &'a fastnbt::IntArray,
biome_types: &'a BiomeTypes,
},
ByteArray {
data: &'a fastnbt::ByteArray,
biome_types: &'a BiomeTypes,
},
}
impl<'a> BiomesV0<'a> {
/// Constructs a new [BiomesV0] from deserialized data structures
pub fn new(biomes: Option<&'a de::BiomesV0>) -> Result<Self> {
pub fn new(biomes: Option<&'a de::BiomesV0>, biome_types: &'a BiomeTypes) -> Result<Self> {
const N: usize = BLOCKS_PER_CHUNK;
const MAXY: usize = 256;
const BN: usize = N >> 2;
@ -223,10 +236,14 @@ impl<'a> BiomesV0<'a> {
Ok(match biomes {
Some(de::BiomesV0::IntArray(data)) if data.len() == BN * BN * BMAXY => {
BiomesV0::IntArrayV15(data)
BiomesV0::IntArrayV15 { data, biome_types }
}
Some(de::BiomesV0::IntArray(data)) if data.len() == N * N => {
BiomesV0::IntArrayV0 { data, biome_types }
}
Some(de::BiomesV0::ByteArray(data)) if data.len() == N * N => {
BiomesV0::ByteArray { data, biome_types }
}
Some(de::BiomesV0::IntArray(data)) if data.len() == N * N => BiomesV0::IntArrayV0(data),
Some(de::BiomesV0::ByteArray(data)) if data.len() == N * N => BiomesV0::ByteArray(data),
_ => bail!("Invalid biome data"),
})
}