Added consts CHUNK_AREA, SUBCHUNK_VOLUME and CHUNK_VOLUME,

This commit is contained in:
DaniD3v committed 2024-08-28 12:52:07 +02:00
1 parent 4565133ef5
commit 1d1dd2ed6c
1 file changed
+9 -5
+9 -5
View File
@@ -11,8 +11,12 @@ use crate::{
WORLD_HEIGHT, WORLD_HEIGHT,
}; };
const CHUNK_AREA: usize = 16 * 16;
const SUBCHUNK_VOLUME: usize = CHUNK_AREA * 16;
const CHUNK_VOLUME: usize = CHUNK_AREA * WORLD_HEIGHT;
pub struct ChunkData { pub struct ChunkData {
pub blocks: Box<[BlockId; 16 * 16 * WORLD_HEIGHT]>, pub blocks: Box<[BlockId; CHUNK_VOLUME]>,
pub position: ChunkCoordinates, pub position: ChunkCoordinates,
pub heightmaps: ChunkHeightmaps, pub heightmaps: ChunkHeightmaps,
} }
@@ -63,7 +67,7 @@ impl ChunkData {
}; };
// this needs to be boxed, otherwise it will cause a stack-overflow // this needs to be boxed, otherwise it will cause a stack-overflow
let mut blocks = Box::new([BlockId::default(); 16 * 16 * WORLD_HEIGHT]); let mut blocks = Box::new([BlockId::default(); CHUNK_VOLUME]);
let mut block_index = 0; // which block we're currently at let mut block_index = 0; // which block we're currently at
for section in chunk_data.sections.into_iter() { for section in chunk_data.sections.into_iter() {
@@ -82,7 +86,7 @@ impl ChunkData {
None => { None => {
// We skipped placing an empty subchunk. // We skipped placing an empty subchunk.
// We need to increase the y coordinate of the next subchunk being placed. // We need to increase the y coordinate of the next subchunk being placed.
block_index += 16 * 16 * 16; block_index += SUBCHUNK_VOLUME;
continue; continue;
} }
Some(d) => d, Some(d) => d,
@@ -108,7 +112,7 @@ impl ChunkData {
// if `SUBCHUNK_VOLUME `is not divisible by `blocks_in_pallete` the block_data // if `SUBCHUNK_VOLUME `is not divisible by `blocks_in_pallete` the block_data
// can sometimes spill into other subchunks. We avoid that by aborting early // can sometimes spill into other subchunks. We avoid that by aborting early
if (block_index % (16 * 16 * 16)) == 0 { if (block_index % SUBCHUNK_VOLUME) == 0 {
break 'block_loop; break 'block_loop;
} }
} }
@@ -129,7 +133,7 @@ impl ChunkData {
) -> Result<BlockId, WorldError> { ) -> Result<BlockId, WorldError> {
Ok(std::mem::replace( Ok(std::mem::replace(
&mut self.blocks &mut self.blocks
[(at.y.get_absolute() * 16 * 16 + *at.z as u16 * 16 + *at.x as u16) as usize], [at.y.get_absolute() as usize * CHUNK_AREA + (*at.z * 16 + *at.x) as usize],
block_id, block_id,
)) ))
} }