diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2024-11-27 19:31:40 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-11-27 19:31:40 -0600 |
| commit | 08958c2278b15ebeac8a964f392ebb792e479b61 (patch) | |
| tree | 4ae3664cea38d7fd1a8f1e95ed06fac04ffe519e /azalea-core/src/position.rs | |
| parent | 139d77d3c2b0922fba5e9d4fa2bd9819d78bd773 (diff) | |
| download | azalea-drasl-08958c2278b15ebeac8a964f392ebb792e479b61.tar.xz | |
Refactor azalea-protocol (#190)
* start updating to 1.21.4
* fix block codegen and stop using block data from burger
* rename packet related modules and structs to be simpler
* ItemSlot -> ItemStack for more consistency with mojmap
* .get() -> .into_packet()
* simplify declare_state_packets by removing packet ids
* rename read_from and write_into to azalea_read and azalea_write
* rename McBufReadable and McBufWritable to AzaleaRead and AzaleaWrite
* McBuf -> AzBuf
* remove most uses of into_variant
* update codegen and use resourcelocation names for packets
* implement #[limit(i)] attribute for AzBuf derive macro
* fixes for 1.21.4
* fix examples
* update some physics code and fix ChatType
* remove unused imports in codegen
* re-add some things to migrate.py and update +mc version numbers automatically
* downgrade to 1.21.3 lol
Diffstat (limited to 'azalea-core/src/position.rs')
| -rwxr-xr-x | azalea-core/src/position.rs | 165 |
1 files changed, 79 insertions, 86 deletions
diff --git a/azalea-core/src/position.rs b/azalea-core/src/position.rs index de0276a3..c280a85a 100755 --- a/azalea-core/src/position.rs +++ b/azalea-core/src/position.rs @@ -8,11 +8,9 @@ use std::{ hash::Hash, io::{Cursor, Write}, ops::{Add, AddAssign, Mul, Rem, Sub}, - str::FromStr, }; -use azalea_buf::{BufReadError, McBuf, McBufReadable, McBufWritable}; -#[cfg(feature = "serde")] +use azalea_buf::{AzBuf, AzaleaRead, AzaleaWrite, BufReadError}; use serde::{Deserialize, Serialize}; use crate::resource_location::ResourceLocation; @@ -28,25 +26,25 @@ macro_rules! vec3_impl { /// Get the distance of this vector to the origin by doing `x^2 + y^2 + /// z^2`. #[inline] - pub fn length_sqr(&self) -> $type { + pub fn length_squared(&self) -> $type { self.x * self.x + self.y * self.y + self.z * self.z } /// Get the squared distance from this position to another position. - /// Equivalent to `(self - other).length_sqr()`. + /// Equivalent to `(self - other).length_squared()`. #[inline] - pub fn distance_to_sqr(&self, other: &Self) -> $type { - (self - other).length_sqr() + pub fn distance_squared_to(&self, other: &Self) -> $type { + (self - other).length_squared() } #[inline] - pub fn horizontal_distance_sqr(&self) -> $type { + pub fn horizontal_distance_squared(&self) -> $type { self.x * self.x + self.z * self.z } #[inline] - pub fn horizontal_distance_to_sqr(&self, other: &Self) -> $type { - (self - other).horizontal_distance_sqr() + pub fn horizontal_distance_squared_to(&self, other: &Self) -> $type { + (self - other).horizontal_distance_squared() } /// Return a new instance of this position with the y coordinate @@ -214,7 +212,7 @@ macro_rules! vec3_impl { /// Used to represent an exact position in the world where an entity could be. /// For blocks, [`BlockPos`] is used instead. -#[derive(Clone, Copy, Debug, Default, PartialEq, McBuf)] +#[derive(Clone, Copy, Debug, Default, PartialEq, AzBuf)] #[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct Vec3 { pub x: f64, @@ -272,6 +270,46 @@ impl BlockPos { pub fn length_manhattan(&self) -> u32 { (self.x.abs() + self.y.abs() + self.z.abs()) as u32 } + + /// Make a new BlockPos with the lower coordinates for each axis. + /// + /// ``` + /// # use azalea_core::position::BlockPos; + /// assert_eq!( + /// BlockPos::min( + /// &BlockPos::new(1, 20, 300), + /// &BlockPos::new(50, 40, 30), + /// ), + /// BlockPos::new(1, 20, 30), + /// ); + /// ``` + pub fn min(&self, other: &Self) -> Self { + Self { + x: self.x.min(other.x), + y: self.y.min(other.y), + z: self.z.min(other.z), + } + } + + /// Make a new BlockPos with the higher coordinates for each axis. + /// + /// ``` + /// # use azalea_core::position::BlockPos; + /// assert_eq!( + /// BlockPos::max( + /// &BlockPos::new(1, 20, 300), + /// &BlockPos::new(50, 40, 30), + /// ), + /// BlockPos::new(50, 40, 300), + /// ); + /// ``` + pub fn max(&self, other: &Self) -> Self { + Self { + x: self.x.max(other.x), + y: self.y.max(other.y), + z: self.z.max(other.z), + } + } } /// Chunk coordinates are used to represent where a chunk is in the world. You @@ -315,15 +353,15 @@ impl From<u64> for ChunkPos { } } } -impl McBufReadable for ChunkPos { - fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { - let long = u64::read_from(buf)?; +impl AzaleaRead for ChunkPos { + fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { + let long = u64::azalea_read(buf)?; Ok(ChunkPos::from(long)) } } -impl McBufWritable for ChunkPos { - fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - u64::from(*self).write_into(buf)?; +impl AzaleaWrite for ChunkPos { + fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + u64::from(*self).azalea_write(buf)?; Ok(()) } } @@ -586,9 +624,9 @@ const PACKED_Z_MASK: u64 = (1 << PACKED_Z_LENGTH) - 1; const Z_OFFSET: u64 = PACKED_Y_LENGTH; const X_OFFSET: u64 = PACKED_Y_LENGTH + PACKED_Z_LENGTH; -impl McBufReadable for BlockPos { - fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { - let val = i64::read_from(buf)?; +impl AzaleaRead for BlockPos { + fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { + let val = i64::azalea_read(buf)?; let x = (val << (64 - X_OFFSET - PACKED_X_LENGTH) >> (64 - PACKED_X_LENGTH)) as i32; let y = (val << (64 - PACKED_Y_LENGTH) >> (64 - PACKED_Y_LENGTH)) as i32; let z = (val << (64 - Z_OFFSET - PACKED_Z_LENGTH) >> (64 - PACKED_Z_LENGTH)) as i32; @@ -596,18 +634,18 @@ impl McBufReadable for BlockPos { } } -impl McBufReadable for GlobalPos { - fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { +impl AzaleaRead for GlobalPos { + fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { Ok(GlobalPos { - world: ResourceLocation::read_from(buf)?, - pos: BlockPos::read_from(buf)?, + world: ResourceLocation::azalea_read(buf)?, + pos: BlockPos::azalea_read(buf)?, }) } } -impl McBufReadable for ChunkSectionPos { - fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { - let long = i64::read_from(buf)?; +impl AzaleaRead for ChunkSectionPos { + fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { + let long = i64::azalea_read(buf)?; Ok(ChunkSectionPos { x: (long >> 42) as i32, y: (long << 44 >> 44) as i32, @@ -616,80 +654,35 @@ impl McBufReadable for ChunkSectionPos { } } -impl McBufWritable for BlockPos { - fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { +impl AzaleaWrite for BlockPos { + fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { let mut val: u64 = 0; val |= ((self.x as u64) & PACKED_X_MASK) << X_OFFSET; val |= (self.y as u64) & PACKED_Y_MASK; val |= ((self.z as u64) & PACKED_Z_MASK) << Z_OFFSET; - val.write_into(buf) + val.azalea_write(buf) } } -impl McBufWritable for GlobalPos { - fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - ResourceLocation::write_into(&self.world, buf)?; - BlockPos::write_into(&self.pos, buf)?; +impl AzaleaWrite for GlobalPos { + fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + ResourceLocation::azalea_write(&self.world, buf)?; + BlockPos::azalea_write(&self.pos, buf)?; Ok(()) } } -impl McBufWritable for ChunkSectionPos { - fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { +impl AzaleaWrite for ChunkSectionPos { + fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { let long = (((self.x & 0x3FFFFF) as i64) << 42) | (self.y & 0xFFFFF) as i64 | (((self.z & 0x3FFFFF) as i64) << 20); - long.write_into(buf)?; + long.azalea_write(buf)?; Ok(()) } } -fn parse_three_values<T>(s: &str) -> Result<[T; 3], &'static str> -where - T: FromStr, - <T as FromStr>::Err: fmt::Debug, -{ - let parts = s.split_whitespace().collect::<Vec<_>>(); - if parts.len() != 3 { - return Err("Expected three values"); - } - - let x = parts[0].parse().map_err(|_| "Invalid X value")?; - let y = parts[1].parse().map_err(|_| "Invalid Y value")?; - let z = parts[2].parse().map_err(|_| "Invalid Z value")?; - - Ok([x, y, z]) -} - -/// Parses a string in the format "X Y Z" into a BlockPos. -/// -/// The input string should contain three integer values separated by spaces, -/// representing the x, y, and z components of the vector respectively. -/// This can be used to parse user input or from `BlockPos::to_string`. -impl FromStr for BlockPos { - type Err = &'static str; - - fn from_str(s: &str) -> Result<Self, Self::Err> { - let [x, y, z] = parse_three_values::<i32>(s)?; - Ok(BlockPos { x, y, z }) - } -} - -/// Parses a string in the format "X Y Z" into a Vec3. -/// -/// The input string should contain three floating-point values separated by -/// spaces, representing the x, y, and z components of the vector respectively. -/// This can be used to parse user input or from `Vec3::to_string`. -impl FromStr for Vec3 { - type Err = &'static str; - - fn from_str(s: &str) -> Result<Self, Self::Err> { - let [x, y, z] = parse_three_values::<f64>(s)?; - Ok(Vec3 { x, y, z }) - } -} - #[cfg(test)] mod tests { use super::*; @@ -733,9 +726,9 @@ mod tests { #[test] fn test_read_blockpos_from() { let mut buf = Vec::new(); - 13743895338965u64.write_into(&mut buf).unwrap(); + 13743895338965u64.azalea_write(&mut buf).unwrap(); let mut buf = Cursor::new(&buf[..]); - let block_pos = BlockPos::read_from(&mut buf).unwrap(); + let block_pos = BlockPos::azalea_read(&mut buf).unwrap(); assert_eq!(block_pos, BlockPos::new(49, -43, -3)); } @@ -751,9 +744,9 @@ mod tests { #[test] fn test_read_chunk_pos_from() { let mut buf = Vec::new(); - ChunkPos::new(2, -1).write_into(&mut buf).unwrap(); + ChunkPos::new(2, -1).azalea_write(&mut buf).unwrap(); let mut buf = Cursor::new(&buf[..]); - let chunk_pos = ChunkPos::from(u64::read_from(&mut buf).unwrap()); + let chunk_pos = ChunkPos::from(u64::azalea_read(&mut buf).unwrap()); assert_eq!(chunk_pos, ChunkPos::new(2, -1)); } } |
