types: make CHUNKS_PER_REGION and BLOCKS_PER_CHUNK usize

Having these as usize is more convenient.
This commit is contained in:
Matthias Schiffer 2023-02-12 20:17:20 +01:00
parent b918ff6106
commit 65c39ea153
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C
2 changed files with 11 additions and 8 deletions

View file

@ -5,7 +5,7 @@ use std::{
use itertools::iproduct; use itertools::iproduct;
pub const BLOCKS_PER_CHUNK: u8 = 16; pub const BLOCKS_PER_CHUNK: usize = 16;
/// A block X coordinate relative to a chunk /// A block X coordinate relative to a chunk
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
@ -28,7 +28,7 @@ pub struct BlockCoords {
impl BlockCoords { impl BlockCoords {
pub fn offset(&self) -> usize { pub fn offset(&self) -> usize {
const N: usize = BLOCKS_PER_CHUNK as usize; use BLOCKS_PER_CHUNK as N;
let x = self.x.0 as usize; let x = self.x.0 as usize;
let y = self.y.0 as usize; let y = self.y.0 as usize;
let z = self.z.0 as usize; let z = self.z.0 as usize;
@ -46,7 +46,7 @@ impl Debug for BlockCoords {
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct SectionY(pub i32); pub struct SectionY(pub i32);
pub const CHUNKS_PER_REGION: u8 = 32; pub const CHUNKS_PER_REGION: usize = 32;
/// A chunk X coordinate relative to a region /// A chunk X coordinate relative to a region
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
@ -70,13 +70,15 @@ impl Debug for ChunkCoords {
} }
#[derive(Debug, Clone, Copy, Default)] #[derive(Debug, Clone, Copy, Default)]
pub struct ChunkArray<T>(pub [[T; CHUNKS_PER_REGION as usize]; CHUNKS_PER_REGION as usize]); pub struct ChunkArray<T>(pub [[T; CHUNKS_PER_REGION]; CHUNKS_PER_REGION]);
impl<T> ChunkArray<T> { impl<T> ChunkArray<T> {
pub fn keys() -> impl Iterator<Item = ChunkCoords> { pub fn keys() -> impl Iterator<Item = ChunkCoords> {
iproduct!(0..CHUNKS_PER_REGION, 0..CHUNKS_PER_REGION).map(|(z, x)| ChunkCoords { iproduct!(0..(CHUNKS_PER_REGION as u8), 0..(CHUNKS_PER_REGION as u8)).map(|(z, x)| {
x: ChunkX(x), ChunkCoords {
z: ChunkZ(z), x: ChunkX(x),
z: ChunkZ(z),
}
}) })
} }

View file

@ -135,7 +135,8 @@ pub struct OldSection<'a> {
impl<'a> OldSection<'a> { impl<'a> OldSection<'a> {
pub fn new(blocks: &'a fastnbt::ByteArray, data: &'a fastnbt::ByteArray) -> Result<Self> { pub fn new(blocks: &'a fastnbt::ByteArray, data: &'a fastnbt::ByteArray) -> Result<Self> {
const N: usize = BLOCKS_PER_CHUNK as usize; use BLOCKS_PER_CHUNK as N;
if blocks.len() != N * N * N { if blocks.len() != N * N * N {
bail!("Invalid section block data"); bail!("Invalid section block data");
} }