diff options
Diffstat (limited to 'azalea-core/src')
| -rw-r--r-- | azalea-core/src/math.rs | 28 | ||||
| -rwxr-xr-x | azalea-core/src/position.rs | 45 |
2 files changed, 69 insertions, 4 deletions
diff --git a/azalea-core/src/math.rs b/azalea-core/src/math.rs index 83e6020e..aa9d88c8 100644 --- a/azalea-core/src/math.rs +++ b/azalea-core/src/math.rs @@ -13,15 +13,15 @@ pub static SIN: LazyLock<[f32; 65536]> = LazyLock::new(|| { /// A sine function that uses a lookup table. pub fn sin(x: f32) -> f32 { let x = x * 10430.378; - let x = x as usize; - SIN[x & 65535] + let x = x as i32 as usize & 65535; + SIN[x] } /// A cosine function that uses a lookup table. pub fn cos(x: f32) -> f32 { let x = x * 10430.378 + 16384.0; - let x = x as usize; - SIN[x & 65535] + let x = x as i32 as usize & 65535; + SIN[x] } // TODO: make this generic @@ -83,4 +83,24 @@ mod tests { assert_eq!(gcd(12, 7), 1); assert_eq!(gcd(7, 12), 1); } + + #[test] + fn test_sin() { + const PI: f32 = std::f32::consts::PI; + // check that they're close enough + fn assert_sin_eq_enough(number: f32) { + let a = sin(number); + let b = f32::sin(number); + assert!((a - b).abs() < 0.01, "sin({number}) failed, {a} != {b}"); + } + assert_sin_eq_enough(0.0); + assert_sin_eq_enough(PI / 2.0); + assert_sin_eq_enough(PI); + assert_sin_eq_enough(PI * 2.0); + assert_sin_eq_enough(PI * 3.0 / 2.0); + assert_sin_eq_enough(-PI / 2.0); + assert_sin_eq_enough(-PI); + assert_sin_eq_enough(-PI * 2.0); + assert_sin_eq_enough(-PI * 3.0 / 2.0); + } } diff --git a/azalea-core/src/position.rs b/azalea-core/src/position.rs index 4cdf3f18..e9864035 100755 --- a/azalea-core/src/position.rs +++ b/azalea-core/src/position.rs @@ -5,6 +5,7 @@ use azalea_buf::{BufReadError, McBuf, McBufReadable, McBufWritable}; use std::{ + fmt, hash::Hash, io::{Cursor, Write}, ops::{Add, AddAssign, Mul, Rem, Sub}, @@ -65,6 +66,43 @@ macro_rules! vec3_impl { } } + /// Return a new instance of this position with the z coordinate subtracted + /// by the given number. + pub fn north(&self, z: $type) -> Self { + Self { + x: self.x, + y: self.y, + z: self.z - z, + } + } + /// Return a new instance of this position with the x coordinate increased + /// by the given number. + pub fn east(&self, x: $type) -> Self { + Self { + x: self.x + x, + y: self.y, + z: self.z, + } + } + /// Return a new instance of this position with the z coordinate increased + /// by the given number. + pub fn south(&self, z: $type) -> Self { + Self { + x: self.x, + y: self.y, + z: self.z + z, + } + } + /// Return a new instance of this position with the x coordinate subtracted + /// by the given number. + pub fn west(&self, x: $type) -> Self { + Self { + x: self.x - x, + y: self.y, + z: self.z, + } + } + #[inline] pub fn dot(&self, other: Self) -> $type { self.x * other.x + self.y * other.y + self.z * other.z @@ -501,6 +539,13 @@ impl From<Vec3> for ChunkBlockPos { } } +impl fmt::Display for BlockPos { + /// Display a block position as `x y z`. + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{} {} {}", self.x, self.y, self.z) + } +} + const PACKED_X_LENGTH: u64 = 1 + 25; // minecraft does something a bit more complicated to get this 25 const PACKED_Z_LENGTH: u64 = PACKED_X_LENGTH; const PACKED_Y_LENGTH: u64 = 64 - PACKED_X_LENGTH - PACKED_Z_LENGTH; |
