mirror of
https://github.com/neocturne/MinedMap.git
synced 2025-03-04 17:23:33 +01:00
util: split to_flat_coord() and from_flat_coord() out of coord_offset()
This commit is contained in:
parent
4a824680a9
commit
d5ac38ed9b
1 changed files with 18 additions and 5 deletions
23
src/util.rs
23
src/util.rs
|
@ -27,6 +27,23 @@ impl ShiftMask for i32 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn to_flat_coord<const AXIS: u8>(
|
||||||
|
region: i8,
|
||||||
|
chunk: ChunkCoord<AXIS>,
|
||||||
|
block: BlockCoord<AXIS>,
|
||||||
|
) -> i32 {
|
||||||
|
(region as i32) << (BLOCK_BITS + CHUNK_BITS) | ((chunk.0 as i32) << BLOCK_BITS | block.0 as i32)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn from_flat_coord<const AXIS: u8>(coord: i32) -> (i8, ChunkCoord<AXIS>, BlockCoord<AXIS>) {
|
||||||
|
let (region_chunk, block) = coord.shift_mask(BLOCK_BITS);
|
||||||
|
let (region, chunk) = region_chunk.shift_mask(CHUNK_BITS);
|
||||||
|
debug_assert!(i8::try_from(region).is_ok());
|
||||||
|
(region as i8, ChunkCoord::new(chunk), BlockCoord::new(block))
|
||||||
|
}
|
||||||
|
|
||||||
/// Offsets a chunk and block coordinate pair by a number of blocks
|
/// Offsets a chunk and block coordinate pair by a number of blocks
|
||||||
///
|
///
|
||||||
/// As the new coordinate may end up in a different region, a region offset
|
/// As the new coordinate may end up in a different region, a region offset
|
||||||
|
@ -37,11 +54,7 @@ pub fn coord_offset<const AXIS: u8>(
|
||||||
block: BlockCoord<AXIS>,
|
block: BlockCoord<AXIS>,
|
||||||
offset: i32,
|
offset: i32,
|
||||||
) -> (i8, ChunkCoord<AXIS>, BlockCoord<AXIS>) {
|
) -> (i8, ChunkCoord<AXIS>, BlockCoord<AXIS>) {
|
||||||
let coord = ((chunk.0 as i32) << BLOCK_BITS | block.0 as i32) + offset;
|
from_flat_coord(to_flat_coord(0, chunk, block) + offset)
|
||||||
let (region_chunk, block) = coord.shift_mask(BLOCK_BITS);
|
|
||||||
let (region, chunk) = region_chunk.shift_mask(CHUNK_BITS);
|
|
||||||
debug_assert!(i8::try_from(region).is_ok());
|
|
||||||
(region as i8, ChunkCoord::new(chunk), BlockCoord::new(block))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
Loading…
Add table
Reference in a new issue