diff options
| author | mat <git@matdoes.dev> | 2023-10-04 19:49:30 -0500 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2023-10-04 19:49:30 -0500 |
| commit | c2fb99159556e1d55552aac0a782dbc69be338f3 (patch) | |
| tree | 946aa44d8637335c05d3312eddfe6874b6c29f27 /azalea-core/src/position.rs | |
| parent | a060ffff93f84df2c419c6180d4ee916ace36ad9 (diff) | |
| download | azalea-drasl-c2fb99159556e1d55552aac0a782dbc69be338f3.tar.xz | |
BLAZINGLY FAST 🚀🚀🚀 pathfinding
Diffstat (limited to 'azalea-core/src/position.rs')
| -rwxr-xr-x | azalea-core/src/position.rs | 93 |
1 files changed, 81 insertions, 12 deletions
diff --git a/azalea-core/src/position.rs b/azalea-core/src/position.rs index 6d0b28da..b731384b 100755 --- a/azalea-core/src/position.rs +++ b/azalea-core/src/position.rs @@ -261,6 +261,7 @@ impl ChunkSectionPos { } /// The coordinates of a block inside a chunk. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +#[repr(align(8))] pub struct ChunkBlockPos { pub x: u8, pub y: i32, @@ -273,9 +274,33 @@ impl ChunkBlockPos { } } +impl Hash for ChunkBlockPos { + // optimized hash that only calls hash once + #[inline] + fn hash<H: std::hash::Hasher>(&self, state: &mut H) { + u64::from(*self).hash(state); + } +} +impl From<ChunkBlockPos> for u64 { + #[inline] + fn from(pos: ChunkBlockPos) -> Self { + // convert to u64 + let mut val: u64 = 0; + // first 32 bits are y + val |= pos.y as u64; + // next 8 bits are z + val |= (pos.z as u64) << 32; + // last 8 bits are x + val |= (pos.x as u64) << 40; + val + } +} +impl nohash_hasher::IsEnabled for ChunkBlockPos {} + /// The coordinates of a block inside a chunk section. Each coordinate must be /// in the range [0, 15]. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +#[repr(align(4))] pub struct ChunkSectionBlockPos { pub x: u8, pub y: u8, @@ -294,6 +319,28 @@ impl Add<ChunkSectionBlockPos> for ChunkSectionPos { ) } } +impl Hash for ChunkSectionBlockPos { + // optimized hash that only calls hash once + #[inline] + fn hash<H: std::hash::Hasher>(&self, state: &mut H) { + u16::from(*self).hash(state); + } +} + +impl From<ChunkSectionBlockPos> for u16 { + #[inline] + fn from(pos: ChunkSectionBlockPos) -> Self { + let mut val: u16 = 0; + // first 4 bits are z + val |= pos.z as u16; + // next 4 bits are y + val |= (pos.y as u16) << 4; + // last 4 bits are x + val |= (pos.x as u16) << 8; + val + } +} +impl nohash_hasher::IsEnabled for ChunkSectionBlockPos {} /// A block pos with an attached world #[derive(Debug, Clone)] @@ -312,22 +359,33 @@ impl From<&BlockPos> for ChunkPos { } } } +impl From<BlockPos> for ChunkPos { + #[inline] + fn from(pos: BlockPos) -> Self { + ChunkPos { + x: pos.x.div_floor(16), + z: pos.z.div_floor(16), + } + } +} impl From<BlockPos> for ChunkSectionPos { + #[inline] fn from(pos: BlockPos) -> Self { ChunkSectionPos { - x: pos.x.div_floor(16), - y: pos.y.div_floor(16), - z: pos.z.div_floor(16), + x: pos.x >> 4, + y: pos.y >> 4, + z: pos.z >> 4, } } } impl From<&BlockPos> for ChunkSectionPos { + #[inline] fn from(pos: &BlockPos) -> Self { ChunkSectionPos { - x: pos.x.div_floor(16), - y: pos.y.div_floor(16), - z: pos.z.div_floor(16), + x: pos.x >> 4, + y: pos.y >> 4, + z: pos.z >> 4, } } } @@ -348,17 +406,28 @@ impl From<&BlockPos> for ChunkBlockPos { } } } - -impl From<&BlockPos> for ChunkSectionBlockPos { - fn from(pos: &BlockPos) -> Self { - ChunkSectionBlockPos { +impl From<BlockPos> for ChunkBlockPos { + #[inline] + fn from(pos: BlockPos) -> Self { + ChunkBlockPos { x: pos.x.rem_euclid(16) as u8, - y: pos.y.rem_euclid(16) as u8, + y: pos.y, z: pos.z.rem_euclid(16) as u8, } } } +impl From<BlockPos> for ChunkSectionBlockPos { + #[inline] + fn from(pos: BlockPos) -> Self { + ChunkSectionBlockPos { + x: pos.x as u8 & 0xF, + y: pos.y as u8 & 0xF, + z: pos.z as u8 & 0xF, + } + } +} + impl From<&ChunkBlockPos> for ChunkSectionBlockPos { #[inline] fn from(pos: &ChunkBlockPos) -> Self { @@ -529,7 +598,7 @@ mod tests { fn test_into_chunk_section_block_pos() { let block_pos = BlockPos::new(0, -60, 0); assert_eq!( - ChunkSectionBlockPos::from(&block_pos), + ChunkSectionBlockPos::from(block_pos), ChunkSectionBlockPos::new(0, 4, 0) ); } |
