mirror of
https://github.com/neocturne/MinedMap.git
synced 2025-03-04 17:23:33 +01:00
world/section: replace block_id_at() method with block_at()
Directly return a BlockType, preparing for early lookup of palettes.
This commit is contained in:
parent
f2ab424590
commit
5c82b80924
3 changed files with 19 additions and 16 deletions
|
@ -76,7 +76,7 @@ impl<'a> RegionProcessor<'a> {
|
|||
/// Processes a single chunk
|
||||
fn process_chunk(&self, data: world::de::Chunk) -> Result<Box<world::layer::BlockInfoArray>> {
|
||||
let chunk = world::chunk::Chunk::new(&data, &self.block_types)?;
|
||||
world::layer::top_layer(&chunk, &self.block_types)
|
||||
world::layer::top_layer(&chunk)
|
||||
}
|
||||
|
||||
fn save_region(
|
||||
|
|
|
@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
|
|||
|
||||
use super::chunk::Chunk;
|
||||
use crate::{
|
||||
resource::{BlockFlag, BlockType, BlockTypes},
|
||||
resource::{BlockFlag, BlockType},
|
||||
types::*,
|
||||
};
|
||||
|
||||
|
@ -92,7 +92,7 @@ pub type BlockInfoArray = LayerBlockArray<Option<BlockInfo>>;
|
|||
/// 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, block_types: &BlockTypes) -> Result<Box<BlockInfoArray>> {
|
||||
pub fn top_layer(chunk: &Chunk) -> Result<Box<BlockInfoArray>> {
|
||||
use BLOCKS_PER_CHUNK as N;
|
||||
|
||||
let mut done = 0;
|
||||
|
@ -109,9 +109,7 @@ pub fn top_layer(chunk: &Chunk, block_types: &BlockTypes) -> Result<Box<BlockInf
|
|||
}
|
||||
|
||||
let coords = SectionBlockCoords { xz, y };
|
||||
let block_id = section.block_id_at(coords)?;
|
||||
let Some(block_type) = block_types.get(block_id) else {
|
||||
eprintln!("Unknown block type: {}", block_id);
|
||||
let Some(block_type) = section.block_at(coords)? else {
|
||||
continue;
|
||||
};
|
||||
let height = BlockHeight::new(section_y, y)?;
|
||||
|
|
|
@ -3,7 +3,7 @@ use num_integer::div_rem;
|
|||
|
||||
use super::de;
|
||||
use crate::{
|
||||
resource::{self, BlockTypes},
|
||||
resource::{BlockType, BlockTypes},
|
||||
types::*,
|
||||
};
|
||||
|
||||
|
@ -27,7 +27,7 @@ fn palette_bits(len: usize, min: u8, max: u8) -> Option<u8> {
|
|||
|
||||
/// Trait for common functions of [SectionV1_13] and [SectionV0]
|
||||
pub trait Section {
|
||||
fn block_id_at(&self, coords: SectionBlockCoords) -> Result<&str>;
|
||||
fn block_at(&self, coords: SectionBlockCoords) -> Result<Option<BlockType>>;
|
||||
}
|
||||
|
||||
/// Minecraft v1.18+ section biome data
|
||||
|
@ -68,7 +68,7 @@ impl<'a> BiomesV18<'a> {
|
|||
pub struct SectionV1_13<'a> {
|
||||
block_states: Option<&'a fastnbt::LongArray>,
|
||||
palette: &'a Vec<de::BlockStatePaletteEntry>,
|
||||
_block_types: &'a BlockTypes,
|
||||
block_types: &'a BlockTypes,
|
||||
bits: u8,
|
||||
aligned_blocks: bool,
|
||||
}
|
||||
|
@ -100,7 +100,7 @@ impl<'a> SectionV1_13<'a> {
|
|||
Ok(Self {
|
||||
block_states,
|
||||
palette,
|
||||
_block_types: block_types,
|
||||
block_types,
|
||||
bits,
|
||||
aligned_blocks,
|
||||
})
|
||||
|
@ -137,13 +137,18 @@ impl<'a> SectionV1_13<'a> {
|
|||
}
|
||||
|
||||
impl<'a> Section for SectionV1_13<'a> {
|
||||
fn block_id_at(&self, coords: SectionBlockCoords) -> Result<&str> {
|
||||
fn block_at(&self, coords: SectionBlockCoords) -> Result<Option<BlockType>> {
|
||||
let index = self.palette_index_at(coords);
|
||||
let entry = self
|
||||
.palette
|
||||
.get(index)
|
||||
.context("Palette index out of bounds")?;
|
||||
Ok(&entry.name)
|
||||
|
||||
let block_type = self.block_types.get(&entry.name);
|
||||
if block_type.is_none() {
|
||||
eprintln!("Unknown block type: {}", entry.name);
|
||||
}
|
||||
Ok(block_type)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -152,7 +157,7 @@ impl<'a> Section for SectionV1_13<'a> {
|
|||
pub struct SectionV0<'a> {
|
||||
blocks: &'a fastnbt::ByteArray,
|
||||
data: &'a fastnbt::ByteArray,
|
||||
_block_types: &'a BlockTypes,
|
||||
block_types: &'a BlockTypes,
|
||||
}
|
||||
|
||||
impl<'a> SectionV0<'a> {
|
||||
|
@ -174,13 +179,13 @@ impl<'a> SectionV0<'a> {
|
|||
Ok(SectionV0 {
|
||||
blocks,
|
||||
data,
|
||||
_block_types: block_types,
|
||||
block_types,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Section for SectionV0<'a> {
|
||||
fn block_id_at(&self, coords: SectionBlockCoords) -> Result<&str> {
|
||||
fn block_at(&self, coords: SectionBlockCoords) -> Result<Option<BlockType>> {
|
||||
let offset = coords.offset();
|
||||
let block = self.blocks[offset] as u8;
|
||||
|
||||
|
@ -192,6 +197,6 @@ impl<'a> Section for SectionV0<'a> {
|
|||
data_byte & 0xf
|
||||
};
|
||||
|
||||
Ok(resource::LEGACY_BLOCK_TYPES[block as usize][data as usize])
|
||||
Ok(self.block_types.get_legacy(block, data))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue