diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2022-06-17 23:10:54 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-17 23:10:54 +0000 |
| commit | f414aa4d37e0c6a7adf55b772fa93714be6e9c9c (patch) | |
| tree | 8cfb7d33114b9537b50477d5d6980995eec6cc5c /azalea-world/src | |
| parent | 56f98c1b243d1ba8906ac73a2f2d8eec5646183e (diff) | |
| parent | 0a945e73ec43b3b0389e004e138c83f41cddc532 (diff) | |
| download | azalea-drasl-f414aa4d37e0c6a7adf55b772fa93714be6e9c9c.tar.xz | |
Merge pull request #9 from mat-1/azalea-block
azalea-block
Diffstat (limited to 'azalea-world/src')
| -rw-r--r-- | azalea-world/src/bit_storage.rs | 18 | ||||
| -rw-r--r-- | azalea-world/src/lib.rs | 26 |
2 files changed, 33 insertions, 11 deletions
diff --git a/azalea-world/src/bit_storage.rs b/azalea-world/src/bit_storage.rs index aba52aef..0dc81f9a 100644 --- a/azalea-world/src/bit_storage.rs +++ b/azalea-world/src/bit_storage.rs @@ -77,8 +77,8 @@ pub struct BitStorage { mask: u64, size: usize, values_per_long: u8, - divide_mul: i32, - divide_add: i32, + divide_mul: u64, + divide_add: u64, divide_shift: i32, } @@ -138,16 +138,16 @@ impl BitStorage { mask, size, values_per_long: values_per_long as u8, - divide_mul, - divide_add, + divide_mul: divide_mul as u32 as u64, + divide_add: divide_add as u32 as u64, divide_shift, }) } pub fn cell_index(&self, index: u64) -> usize { // as unsigned wrap - let first = self.divide_mul as u32 as u64; - let second = self.divide_add as u64; + let first = self.divide_mul; + let second = self.divide_add; (((index * first) + second) >> 32 >> self.divide_shift) .try_into() @@ -188,6 +188,12 @@ impl BitStorage { let bit_index = (index - cell_index * self.values_per_long as usize) * self.bits; *cell = *cell & !(self.mask << bit_index) | (value & self.mask) << bit_index; } + + /// The number of entries. + #[inline] + pub fn size(&self) -> usize { + self.size + } } #[cfg(test)] diff --git a/azalea-world/src/lib.rs b/azalea-world/src/lib.rs index 766c61f0..e651e455 100644 --- a/azalea-world/src/lib.rs +++ b/azalea-world/src/lib.rs @@ -4,6 +4,7 @@ mod bit_storage; mod palette; use crate::palette::PalettedContainerType; +use azalea_block::BlockState; use azalea_core::{BlockPos, ChunkBlockPos, ChunkPos, ChunkSectionBlockPos}; use azalea_protocol::mc_buf::{McBufReadable, McBufWritable}; pub use bit_storage::BitStorage; @@ -57,7 +58,7 @@ impl World { self.storage.view_center = *pos; } - pub fn get_block_state(&self, pos: &BlockPos) -> Option<u32> { + pub fn get_block_state(&self, pos: &BlockPos) -> Option<BlockState> { self.storage.get_block_state(pos, self.min_y) } } @@ -122,7 +123,7 @@ impl ChunkStorage { && (chunk_pos.z - self.view_center.z).unsigned_abs() <= self.chunk_radius } - pub fn get_block_state(&self, pos: &BlockPos, min_y: i32) -> Option<u32> { + pub fn get_block_state(&self, pos: &BlockPos, min_y: i32) -> Option<BlockState> { let chunk_pos = ChunkPos::from(pos); println!("chunk_pos {:?} block_pos {:?}", chunk_pos, pos); let chunk = &self[&chunk_pos]; @@ -175,7 +176,7 @@ impl Chunk { (y.div_floor(16) - min_section_index) as u32 } - pub fn get(&self, pos: &ChunkBlockPos, min_y: i32) -> u32 { + pub fn get(&self, pos: &ChunkBlockPos, min_y: i32) -> BlockState { let section_index = self.section_index(pos.y, min_y); // TODO: make sure the section exists let section = &self.sections[section_index as usize]; @@ -204,12 +205,25 @@ pub struct Section { impl McBufReadable for Section { fn read_into(buf: &mut impl Read) -> Result<Self, String> { let block_count = u16::read_into(buf)?; + // this is commented out because the vanilla server is wrong // assert!( // block_count <= 16 * 16 * 16, // "A section has more blocks than what should be possible. This is a bug!" // ); + let states = PalettedContainer::read_with_type(buf, &PalettedContainerType::BlockStates)?; + + for i in 0..states.storage.size() { + if !BlockState::is_valid_state(states.storage.get(i) as u32) { + return Err(format!( + "Invalid block state {} (index {}) found in section.", + states.storage.get(i), + i + )); + } + } + let biomes = PalettedContainer::read_with_type(buf, &PalettedContainerType::Biomes)?; Ok(Section { block_count, @@ -229,9 +243,11 @@ impl McBufWritable for Section { } impl Section { - // TODO: return a BlockState instead of a u32 - fn get(&self, pos: ChunkSectionBlockPos) -> u32 { + fn get(&self, pos: ChunkSectionBlockPos) -> BlockState { + // TODO: use the unsafe method and do the check earlier self.states .get(pos.x as usize, pos.y as usize, pos.z as usize) + .try_into() + .expect("Invalid block state.") } } |
