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:
Matthias Schiffer 2023-03-02 00:19:01 +01:00
parent f2ab424590
commit 5c82b80924
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C
3 changed files with 19 additions and 16 deletions

View file

@ -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(

View file

@ -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)?;

View file

@ -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))
}
}