world: distinguish pre-v1.18 biome data structures

This commit is contained in:
Matthias Schiffer 2023-04-02 15:10:40 +02:00
parent e62e19796c
commit f2b7808e84
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C
2 changed files with 33 additions and 4 deletions

View file

@ -24,14 +24,14 @@ pub enum Chunk<'a> {
/// block IDs
V1_13 {
section_map: BTreeMap<SectionY, (SectionV1_13<'a>, BlockLight<'a>)>,
biomes: &'a de::BiomesV0,
biomes: BiomesV0<'a>,
},
/// Original pre-1.13 chunk
///
/// The original chunk format with fixed 8-bit numeric block IDs
V0 {
section_map: BTreeMap<SectionY, (SectionV0<'a>, BlockLight<'a>)>,
biomes: &'a de::BiomesV0,
biomes: BiomesV0<'a>,
},
/// Unpopulated chunk without any block data
Empty,
@ -158,8 +158,7 @@ impl<'a> Chunk<'a> {
}
}
// TODO Check biomes length
let biomes = level.biomes.as_ref().context("Invalid biome data");
let biomes = BiomesV0::new(level.biomes.as_ref());
Ok(
match (section_map_v1_13.is_empty(), section_map_v0.is_empty()) {

View file

@ -202,6 +202,36 @@ impl<'a> BiomesV18<'a> {
}
}
/// Pre-v1.18 section biome data
///
/// There are a 3 formats for biome data that were used in
/// different pre-v1.18 Minecraft versions
#[derive(Debug)]
pub enum BiomesV0<'a> {
IntArrayV15(&'a fastnbt::IntArray),
IntArrayV0(&'a fastnbt::IntArray),
ByteArray(&'a fastnbt::ByteArray),
}
impl<'a> BiomesV0<'a> {
/// Constructs a new [BiomesV0] from deserialized data structures
pub fn new(biomes: Option<&'a de::BiomesV0>) -> Result<Self> {
const N: usize = BLOCKS_PER_CHUNK;
const MAXY: usize = 256;
const BN: usize = N >> 2;
const BMAXY: usize = MAXY >> 2;
Ok(match biomes {
Some(de::BiomesV0::IntArray(data)) if data.len() == BN * BN * BMAXY => {
BiomesV0::IntArrayV15(data)
}
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"),
})
}
}
#[derive(Debug, Clone, Copy)]
pub struct BlockLight<'a>(Option<&'a [i8]>);