From c2fb99159556e1d55552aac0a782dbc69be338f3 Mon Sep 17 00:00:00 2001 From: mat Date: Wed, 4 Oct 2023 19:49:30 -0500 Subject: BLAZINGLY FAST 🚀🚀🚀 pathfinding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- azalea-core/src/position.rs | 93 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 81 insertions(+), 12 deletions(-) (limited to 'azalea-core/src') 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(&self, state: &mut H) { + u64::from(*self).hash(state); + } +} +impl From 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 for ChunkSectionPos { ) } } +impl Hash for ChunkSectionBlockPos { + // optimized hash that only calls hash once + #[inline] + fn hash(&self, state: &mut H) { + u16::from(*self).hash(state); + } +} + +impl From 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 for ChunkPos { + #[inline] + fn from(pos: BlockPos) -> Self { + ChunkPos { + x: pos.x.div_floor(16), + z: pos.z.div_floor(16), + } + } +} impl From 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 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 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) ); } -- cgit v1.2.3