1use std::{
7 fmt::{self, Display},
8 hash::{Hash, Hasher},
9 io::{self, Cursor, Write},
10 ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, Sub},
11 str::FromStr,
12};
13
14use azalea_buf::{AzBuf, BufReadError};
15use azalea_codec::Codec;
16
17use crate::{direction::Direction, identifier::Identifier, math};
18
19macro_rules! vec3_impl {
20 ($name:ident, $type:ty) => {
21 impl $name {
22 pub const ZERO: Self = Self {
24 x: 0 as $type,
25 y: 0 as $type,
26 z: 0 as $type,
27 };
28
29 #[inline]
30 pub const fn new(x: $type, y: $type, z: $type) -> Self {
31 Self { x, y, z }
32 }
33
34 #[inline]
37 pub fn length_squared(&self) -> $type {
38 self.x * self.x + self.y * self.y + self.z * self.z
39 }
40
41 #[inline]
44 pub fn distance_squared_to(self, other: Self) -> $type {
45 (self - other).length_squared()
46 }
47
48 #[inline]
49 pub fn horizontal_distance_squared(&self) -> $type {
50 self.x * self.x + self.z * self.z
51 }
52
53 #[inline]
54 pub fn horizontal_distance_squared_to(self, other: Self) -> $type {
55 (self - other).horizontal_distance_squared()
56 }
57
58 #[inline]
61 pub fn down(&self, y: $type) -> Self {
62 Self {
63 x: self.x,
64 y: self.y - y,
65 z: self.z,
66 }
67 }
68 #[inline]
71 pub fn up(&self, y: $type) -> Self {
72 Self {
73 x: self.x,
74 y: self.y + y,
75 z: self.z,
76 }
77 }
78
79 #[inline]
82 pub fn north(&self, z: $type) -> Self {
83 Self {
84 x: self.x,
85 y: self.y,
86 z: self.z - z,
87 }
88 }
89 #[inline]
92 pub fn east(&self, x: $type) -> Self {
93 Self {
94 x: self.x + x,
95 y: self.y,
96 z: self.z,
97 }
98 }
99 #[inline]
102 pub fn south(&self, z: $type) -> Self {
103 Self {
104 x: self.x,
105 y: self.y,
106 z: self.z + z,
107 }
108 }
109 #[inline]
112 pub fn west(&self, x: $type) -> Self {
113 Self {
114 x: self.x - x,
115 y: self.y,
116 z: self.z,
117 }
118 }
119
120 #[inline]
121 pub fn dot(&self, other: Self) -> $type {
122 self.x * other.x + self.y * other.y + self.z * other.z
123 }
124
125 #[inline]
126 pub fn cross(&self, other: Self) -> Self {
127 Self::new(
128 self.y * other.z - self.z * other.y,
129 self.z * other.x - self.x * other.z,
130 self.x * other.y - self.y * other.x,
131 )
132 }
133
134 pub fn min(&self, other: Self) -> Self {
136 Self {
137 x: self.x.min(other.x),
138 y: self.x.min(other.y),
139 z: self.x.min(other.z),
140 }
141 }
142 pub fn max(&self, other: Self) -> Self {
144 Self {
145 x: self.x.max(other.x),
146 y: self.x.max(other.y),
147 z: self.x.max(other.z),
148 }
149 }
150
151 #[inline]
153 pub fn xz(&self) -> Self {
154 Self {
155 x: self.x,
156 y: <$type>::default(),
157 z: self.z,
158 }
159 }
160
161 pub fn with_x(&self, x: $type) -> Self {
162 Self { x, ..*self }
163 }
164 pub fn with_y(&self, y: $type) -> Self {
165 Self { y, ..*self }
166 }
167 pub fn with_z(&self, z: $type) -> Self {
168 Self { z, ..*self }
169 }
170 }
171
172 impl Add for &$name {
173 type Output = $name;
174
175 #[inline]
176 fn add(self, rhs: Self) -> Self::Output {
177 $name {
178 x: self.x + rhs.x,
179 y: self.y + rhs.y,
180 z: self.z + rhs.z,
181 }
182 }
183 }
184 impl Add for $name {
185 type Output = $name;
186
187 #[inline]
188 fn add(self, rhs: Self) -> Self::Output {
189 (&self).add(&rhs)
190 }
191 }
192 impl Add<$type> for $name {
193 type Output = Self;
194
195 #[inline]
196 fn add(self, rhs: $type) -> Self::Output {
197 Self {
198 x: self.x + rhs,
199 y: self.y + rhs,
200 z: self.z + rhs,
201 }
202 }
203 }
204
205 impl AddAssign for $name {
206 #[inline]
207 fn add_assign(&mut self, rhs: Self) {
208 self.x += rhs.x;
209 self.y += rhs.y;
210 self.z += rhs.z;
211 }
212 }
213 impl Rem<$type> for $name {
214 type Output = Self;
215
216 #[inline]
217 fn rem(self, rhs: $type) -> Self::Output {
218 Self {
219 x: self.x % rhs,
220 y: self.y % rhs,
221 z: self.z % rhs,
222 }
223 }
224 }
225
226 impl Sub for &$name {
227 type Output = $name;
228
229 #[inline]
231 fn sub(self, other: Self) -> Self::Output {
232 Self::Output {
233 x: self.x - other.x,
234 y: self.y - other.y,
235 z: self.z - other.z,
236 }
237 }
238 }
239 impl Sub for $name {
240 type Output = Self;
241
242 #[inline]
243 fn sub(self, other: Self) -> Self::Output {
244 (&self).sub(&other)
245 }
246 }
247
248 impl Mul<$type> for $name {
249 type Output = Self;
250
251 #[inline]
252 fn mul(self, multiplier: $type) -> Self::Output {
253 Self {
254 x: self.x * multiplier,
255 y: self.y * multiplier,
256 z: self.z * multiplier,
257 }
258 }
259 }
260 impl MulAssign<$type> for $name {
261 #[inline]
262 fn mul_assign(&mut self, multiplier: $type) {
263 self.x *= multiplier;
264 self.y *= multiplier;
265 self.z *= multiplier;
266 }
267 }
268
269 impl Div<$type> for $name {
270 type Output = Self;
271
272 #[inline]
273 fn div(self, divisor: $type) -> Self::Output {
274 Self {
275 x: self.x / divisor,
276 y: self.y / divisor,
277 z: self.z / divisor,
278 }
279 }
280 }
281 impl DivAssign<$type> for $name {
282 #[inline]
283 fn div_assign(&mut self, divisor: $type) {
284 self.x /= divisor;
285 self.y /= divisor;
286 self.z /= divisor;
287 }
288 }
289
290 impl From<($type, $type, $type)> for $name {
291 #[inline]
292 fn from(pos: ($type, $type, $type)) -> Self {
293 Self::new(pos.0, pos.1, pos.2)
294 }
295 }
296 impl From<&($type, $type, $type)> for $name {
297 #[inline]
298 fn from(pos: &($type, $type, $type)) -> Self {
299 Self::new(pos.0, pos.1, pos.2)
300 }
301 }
302 impl From<$name> for ($type, $type, $type) {
303 #[inline]
304 fn from(pos: $name) -> Self {
305 (pos.x, pos.y, pos.z)
306 }
307 }
308
309 impl<O: azalea_codec::Ops> Codec<O> for $name {
310 fn codec_read(ops: &O) -> azalea_codec::error::ReadResult<Self> {
311 let [x, y, z] = <[$type; 3]>::codec_read(ops)?;
312 Ok(Self { x, y, z })
313 }
314 fn codec_write(&self) -> azalea_codec::error::WriteResult<O> {
315 [self.x, self.y, self.z].codec_write()
316 }
317 }
318
319 azalea_codec::impl_defaultable!($name);
320 #[cfg(feature = "serde")]
321 azalea_codec::impl_serde!($name);
322 };
323}
324
325#[derive(AzBuf, Clone, Copy, Debug, Default, PartialEq)]
329pub struct Vec3 {
330 pub x: f64,
331 pub y: f64,
332 pub z: f64,
333}
334vec3_impl!(Vec3, f64);
335
336impl Vec3 {
337 #[doc(alias = "modulus")]
340 pub fn length(&self) -> f64 {
341 f64::sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
342 }
343
344 pub fn distance_to(self, other: Self) -> f64 {
347 (self - other).length()
348 }
349
350 pub fn horizontal_distance_to(self, other: Self) -> f64 {
351 self.horizontal_distance_squared_to(other).sqrt()
352 }
353 pub fn horizontal_distance(self) -> f64 {
354 self.horizontal_distance_squared().sqrt()
355 }
356
357 pub fn x_rot(self, radians: f32) -> Vec3 {
358 let x_delta = math::cos(radians);
359 let y_delta = math::sin(radians);
360 let x = self.x;
361 let y = self.y * (x_delta as f64) + self.z * (y_delta as f64);
362 let z = self.z * (x_delta as f64) - self.y * (y_delta as f64);
363 Vec3 { x, y, z }
364 }
365 pub fn y_rot(self, radians: f32) -> Vec3 {
366 let x_delta = math::cos(radians);
367 let y_delta = math::sin(radians);
368 let x = self.x * (x_delta as f64) + self.z * (y_delta as f64);
369 let y = self.y;
370 let z = self.z * (x_delta as f64) - self.x * (y_delta as f64);
371 Vec3 { x, y, z }
372 }
373
374 pub fn to_block_pos_floor(&self) -> BlockPos {
375 BlockPos {
376 x: self.x.floor() as i32,
377 y: self.y.floor() as i32,
378 z: self.z.floor() as i32,
379 }
380 }
381 pub fn to_block_pos_ceil(&self) -> BlockPos {
382 BlockPos {
383 x: self.x.ceil() as i32,
384 y: self.y.ceil() as i32,
385 z: self.z.ceil() as i32,
386 }
387 }
388
389 pub fn closer_than(&self, other: Vec3, range: f64) -> bool {
392 self.distance_squared_to(other) < range.powi(2)
393 }
394}
395
396#[derive(AzBuf, Clone, Copy, Debug, Default, PartialEq)]
398pub struct Vec3f32 {
399 pub x: f32,
400 pub y: f32,
401 pub z: f32,
402}
403impl From<Vec3f32> for Vec3 {
404 fn from(v: Vec3f32) -> Self {
405 Vec3 {
406 x: v.x as f64,
407 y: v.y as f64,
408 z: v.z as f64,
409 }
410 }
411}
412impl From<Vec3> for Vec3f32 {
413 fn from(v: Vec3) -> Self {
414 Vec3f32 {
415 x: v.x as f32,
416 y: v.y as f32,
417 z: v.z as f32,
418 }
419 }
420}
421vec3_impl!(Vec3f32, f32);
422
423#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
428pub struct BlockPos {
429 pub x: i32,
430 pub y: i32,
431 pub z: i32,
432}
433vec3_impl!(BlockPos, i32);
434
435impl BlockPos {
436 pub fn center(&self) -> Vec3 {
439 Vec3 {
440 x: self.x as f64 + 0.5,
441 y: self.y as f64 + 0.5,
442 z: self.z as f64 + 0.5,
443 }
444 }
445
446 pub fn center_bottom(&self) -> Vec3 {
449 Vec3 {
450 x: self.x as f64 + 0.5,
451 y: self.y as f64,
452 z: self.z as f64 + 0.5,
453 }
454 }
455
456 pub fn to_vec3_floored(&self) -> Vec3 {
458 Vec3 {
459 x: self.x as f64,
460 y: self.y as f64,
461 z: self.z as f64,
462 }
463 }
464
465 pub fn length_manhattan(&self) -> u32 {
467 (self.x.abs() + self.y.abs() + self.z.abs()) as u32
468 }
469
470 pub fn offset_with_direction(self, direction: Direction) -> Self {
482 self + direction.normal()
483 }
484
485 pub fn length(&self) -> f64 {
488 f64::sqrt((self.x * self.x + self.y * self.y + self.z * self.z) as f64)
489 }
490
491 pub fn distance_to(self, other: Self) -> f64 {
498 (self - other).length()
499 }
500}
501
502#[derive(AzBuf, Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
507pub struct Vec3i {
508 #[var]
509 pub x: i32,
510 #[var]
511 pub y: i32,
512 #[var]
513 pub z: i32,
514}
515vec3_impl!(Vec3i, i32);
516
517#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
521pub struct ChunkPos {
522 pub x: i32,
523 pub z: i32,
524}
525impl ChunkPos {
526 pub fn new(x: i32, z: i32) -> Self {
527 ChunkPos { x, z }
528 }
529}
530impl Add<ChunkPos> for ChunkPos {
531 type Output = Self;
532
533 fn add(self, rhs: Self) -> Self::Output {
534 Self {
535 x: self.x + rhs.x,
536 z: self.z + rhs.z,
537 }
538 }
539}
540impl Add<ChunkBlockPos> for ChunkPos {
541 type Output = BlockPos;
542
543 fn add(self, rhs: ChunkBlockPos) -> Self::Output {
544 BlockPos {
545 x: self.x * 16 + rhs.x as i32,
546 y: rhs.y,
547 z: self.z * 16 + rhs.z as i32,
548 }
549 }
550}
551
552impl From<ChunkPos> for u64 {
556 #[inline]
557 fn from(pos: ChunkPos) -> Self {
558 (pos.x as u64) | ((pos.z as u64) << 32)
559 }
560}
561impl From<u64> for ChunkPos {
562 #[inline]
563 fn from(pos: u64) -> Self {
564 ChunkPos {
565 x: (pos) as i32,
566 z: (pos >> 32) as i32,
567 }
568 }
569}
570impl AzBuf for ChunkPos {
571 fn azalea_read(ctx: azalea_buf::Ctx, buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
572 let long = u64::azalea_read(ctx, buf)?;
573 Ok(ChunkPos::from(long))
574 }
575 fn azalea_write(&self, ctx: azalea_buf::Ctx, buf: &mut impl Write) -> io::Result<()> {
576 u64::from(*self).azalea_write(ctx, buf)?;
577 Ok(())
578 }
579}
580
581impl Hash for ChunkPos {
582 #[inline]
583 fn hash<H: Hasher>(&self, state: &mut H) {
584 u64::from(*self).hash(state);
586 }
587}
588impl nohash_hasher::IsEnabled for ChunkPos {}
591
592#[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd)]
594pub struct ChunkSectionPos {
595 pub x: i32,
596 pub y: i32,
597 pub z: i32,
598}
599vec3_impl!(ChunkSectionPos, i32);
600
601impl ChunkSectionPos {
602 pub fn block_to_section_coord(block: i32) -> i32 {
603 block >> 4
604 }
605}
606
607#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
609pub struct ChunkBlockPos {
610 pub x: u8,
611 pub y: i32,
612 pub z: u8,
613}
614
615impl ChunkBlockPos {
616 pub fn new(x: u8, y: i32, z: u8) -> Self {
617 ChunkBlockPos { x, y, z }
618 }
619}
620
621impl Hash for ChunkBlockPos {
622 #[inline]
624 fn hash<H: Hasher>(&self, state: &mut H) {
625 u64::from(*self).hash(state);
626 }
627}
628impl From<ChunkBlockPos> for u64 {
629 #[inline]
630 fn from(pos: ChunkBlockPos) -> Self {
631 let mut val: u64 = 0;
633 val |= pos.y as u64;
635 val |= (pos.z as u64) << 32;
637 val |= (pos.x as u64) << 40;
639 val
640 }
641}
642impl nohash_hasher::IsEnabled for ChunkBlockPos {}
643
644#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
648pub struct ChunkSectionBlockPos {
649 pub x: u8,
650 pub y: u8,
651 pub z: u8,
652}
653vec3_impl!(ChunkSectionBlockPos, u8);
654
655#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
659pub struct ChunkSectionBiomePos {
660 pub x: u8,
661 pub y: u8,
662 pub z: u8,
663}
664impl From<&ChunkBiomePos> for ChunkSectionBiomePos {
665 #[inline]
666 fn from(pos: &ChunkBiomePos) -> Self {
667 ChunkSectionBiomePos {
668 x: pos.x,
669 y: (pos.y & 0b11) as u8,
670 z: pos.z,
671 }
672 }
673}
674impl From<ChunkBiomePos> for ChunkSectionBiomePos {
675 #[inline]
676 fn from(pos: ChunkBiomePos) -> Self {
677 Self::from(&pos)
678 }
679}
680vec3_impl!(ChunkSectionBiomePos, u8);
681
682#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
686pub struct ChunkBiomePos {
687 pub x: u8,
688 pub y: i32,
689 pub z: u8,
690}
691impl From<&BlockPos> for ChunkBiomePos {
692 #[inline]
693 fn from(pos: &BlockPos) -> Self {
694 ChunkBiomePos::from(&ChunkBlockPos::from(pos))
695 }
696}
697impl From<BlockPos> for ChunkBiomePos {
698 #[inline]
699 fn from(pos: BlockPos) -> Self {
700 ChunkBiomePos::from(&ChunkBlockPos::from(pos))
701 }
702}
703impl From<&ChunkBlockPos> for ChunkBiomePos {
704 #[inline]
705 fn from(pos: &ChunkBlockPos) -> Self {
706 ChunkBiomePos {
707 x: pos.x >> 2,
708 y: pos.y >> 2,
709 z: pos.z >> 2,
710 }
711 }
712}
713
714impl Add<ChunkSectionBlockPos> for ChunkSectionPos {
715 type Output = BlockPos;
716
717 fn add(self, rhs: ChunkSectionBlockPos) -> Self::Output {
718 BlockPos::new(
719 self.x * 16 + rhs.x as i32,
720 self.y * 16 + rhs.y as i32,
721 self.z * 16 + rhs.z as i32,
722 )
723 }
724}
725impl Hash for ChunkSectionBlockPos {
726 #[inline]
728 fn hash<H: Hasher>(&self, state: &mut H) {
729 u16::from(*self).hash(state);
730 }
731}
732
733impl From<ChunkSectionBlockPos> for u16 {
734 #[inline]
735 fn from(pos: ChunkSectionBlockPos) -> Self {
736 ((((pos.y as u16) << 4) | pos.z as u16) << 4) | pos.x as u16
738 }
739}
740impl nohash_hasher::IsEnabled for ChunkSectionBlockPos {}
741
742#[derive(Clone, Debug, PartialEq, Codec)]
744pub struct GlobalPos {
745 pub dimension: Identifier,
748 pub pos: BlockPos,
749}
750#[cfg(feature = "serde")]
751azalea_codec::impl_serde!(GlobalPos);
752
753impl From<&BlockPos> for ChunkPos {
754 #[inline]
755 fn from(pos: &BlockPos) -> Self {
756 ChunkPos {
757 x: pos.x >> 4,
758 z: pos.z >> 4,
759 }
760 }
761}
762impl From<BlockPos> for ChunkPos {
763 #[inline]
764 fn from(pos: BlockPos) -> Self {
765 ChunkPos {
766 x: pos.x >> 4,
767 z: pos.z >> 4,
768 }
769 }
770}
771
772impl From<BlockPos> for ChunkSectionPos {
773 #[inline]
774 fn from(pos: BlockPos) -> Self {
775 Self {
776 x: pos.x >> 4,
777 y: pos.y >> 4,
778 z: pos.z >> 4,
779 }
780 }
781}
782impl From<&BlockPos> for ChunkSectionPos {
783 #[inline]
784 fn from(pos: &BlockPos) -> Self {
785 Self {
786 x: pos.x >> 4,
787 y: pos.y >> 4,
788 z: pos.z >> 4,
789 }
790 }
791}
792
793impl From<ChunkSectionPos> for ChunkPos {
794 fn from(pos: ChunkSectionPos) -> Self {
795 ChunkPos { x: pos.x, z: pos.z }
796 }
797}
798impl From<&Vec3> for ChunkSectionPos {
799 fn from(pos: &Vec3) -> Self {
800 ChunkSectionPos::from(&BlockPos::from(pos))
801 }
802}
803impl From<Vec3> for ChunkSectionPos {
804 fn from(pos: Vec3) -> Self {
805 ChunkSectionPos::from(&pos)
806 }
807}
808
809impl From<&BlockPos> for ChunkBlockPos {
810 #[inline]
811 fn from(pos: &BlockPos) -> Self {
812 ChunkBlockPos {
813 x: (pos.x & 0xF) as u8,
814 y: pos.y,
815 z: (pos.z & 0xF) as u8,
816 }
817 }
818}
819impl From<BlockPos> for ChunkBlockPos {
820 #[inline]
821 fn from(pos: BlockPos) -> Self {
822 ChunkBlockPos {
823 x: (pos.x & 0xF) as u8,
824 y: pos.y,
825 z: (pos.z & 0xF) as u8,
826 }
827 }
828}
829
830impl From<BlockPos> for ChunkSectionBlockPos {
831 #[inline]
832 fn from(pos: BlockPos) -> Self {
833 ChunkSectionBlockPos {
834 x: (pos.x & 0xF) as u8,
835 y: (pos.y & 0xF) as u8,
836 z: (pos.z & 0xF) as u8,
837 }
838 }
839}
840
841impl From<&ChunkBlockPos> for ChunkSectionBlockPos {
842 #[inline]
843 fn from(pos: &ChunkBlockPos) -> Self {
844 ChunkSectionBlockPos {
845 x: pos.x,
846 y: (pos.y & 0xF) as u8,
847 z: pos.z,
848 }
849 }
850}
851impl From<&Vec3> for BlockPos {
852 #[inline]
853 fn from(pos: &Vec3) -> Self {
854 BlockPos {
855 x: pos.x.floor() as i32,
856 y: pos.y.floor() as i32,
857 z: pos.z.floor() as i32,
858 }
859 }
860}
861impl From<Vec3> for BlockPos {
862 #[inline]
863 fn from(pos: Vec3) -> Self {
864 BlockPos::from(&pos)
865 }
866}
867
868impl From<&Vec3> for ChunkPos {
869 fn from(pos: &Vec3) -> Self {
870 ChunkPos::from(&BlockPos::from(pos))
871 }
872}
873impl From<Vec3> for ChunkPos {
874 fn from(pos: Vec3) -> Self {
875 ChunkPos::from(&pos)
876 }
877}
878
879impl From<&Vec3> for ChunkBlockPos {
880 fn from(pos: &Vec3) -> Self {
881 ChunkBlockPos::from(&BlockPos::from(pos))
882 }
883}
884impl From<Vec3> for ChunkBlockPos {
885 fn from(pos: Vec3) -> Self {
886 ChunkBlockPos::from(&pos)
887 }
888}
889
890impl Display for BlockPos {
891 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
893 write!(f, "{} {} {}", self.x, self.y, self.z)
894 }
895}
896impl Display for Vec3 {
897 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
899 write!(f, "{} {} {}", self.x, self.y, self.z)
900 }
901}
902impl Display for ChunkPos {
903 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
905 write!(f, "{} {}", self.x, self.z)
906 }
907}
908
909#[derive(AzBuf, Clone, Copy, Debug, Default, PartialEq)]
911pub struct Vec2 {
912 pub x: f32,
913 pub y: f32,
914}
915impl Vec2 {
916 const ZERO: Vec2 = Vec2 { x: 0.0, y: 0.0 };
917
918 #[inline]
919 pub fn new(x: f32, y: f32) -> Self {
920 Vec2 { x, y }
921 }
922 #[inline]
923 pub fn scale(&self, amount: f32) -> Self {
924 Vec2 {
925 x: self.x * amount,
926 y: self.y * amount,
927 }
928 }
929 #[inline]
930 pub fn dot(&self, other: Vec2) -> f32 {
931 self.x * other.x + self.y * other.y
932 }
933 #[inline]
934 pub fn normalized(&self) -> Self {
935 let length = (self.x * self.x + self.y * self.y).sqrt();
936 if length < 1e-4 {
937 return Vec2::ZERO;
938 }
939 Vec2 {
940 x: self.x / length,
941 y: self.y / length,
942 }
943 }
944 #[inline]
945 pub fn length_squared(&self) -> f32 {
946 self.x * self.x + self.y * self.y
947 }
948 #[inline]
949 pub fn length(&self) -> f32 {
950 self.length_squared().sqrt()
951 }
952}
953impl Mul<f32> for Vec2 {
954 type Output = Self;
955
956 #[inline]
957 fn mul(self, rhs: f32) -> Self::Output {
958 self.scale(rhs)
959 }
960}
961impl MulAssign<f32> for Vec2 {
962 #[inline]
963 fn mul_assign(&mut self, rhs: f32) {
964 *self = self.scale(rhs);
965 }
966}
967
968const PACKED_X_LENGTH: u64 = 1 + 25; const PACKED_Z_LENGTH: u64 = PACKED_X_LENGTH;
970const PACKED_Y_LENGTH: u64 = 64 - PACKED_X_LENGTH - PACKED_Z_LENGTH;
971const PACKED_X_MASK: u64 = (1 << PACKED_X_LENGTH) - 1;
972const PACKED_Y_MASK: u64 = (1 << PACKED_Y_LENGTH) - 1;
973const PACKED_Z_MASK: u64 = (1 << PACKED_Z_LENGTH) - 1;
974const Z_OFFSET: u64 = PACKED_Y_LENGTH;
975const X_OFFSET: u64 = PACKED_Y_LENGTH + PACKED_Z_LENGTH;
976
977impl AzBuf for BlockPos {
978 fn azalea_read(ctx: azalea_buf::Ctx, buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
979 let val = i64::azalea_read(ctx, buf)?;
980 let x = (val << (64 - X_OFFSET - PACKED_X_LENGTH) >> (64 - PACKED_X_LENGTH)) as i32;
981 let y = (val << (64 - PACKED_Y_LENGTH) >> (64 - PACKED_Y_LENGTH)) as i32;
982 let z = (val << (64 - Z_OFFSET - PACKED_Z_LENGTH) >> (64 - PACKED_Z_LENGTH)) as i32;
983 Ok(BlockPos { x, y, z })
984 }
985 fn azalea_write(&self, ctx: azalea_buf::Ctx, buf: &mut impl Write) -> io::Result<()> {
986 let mut val: u64 = 0;
987 val |= ((self.x as u64) & PACKED_X_MASK) << X_OFFSET;
988 val |= (self.y as u64) & PACKED_Y_MASK;
989 val |= ((self.z as u64) & PACKED_Z_MASK) << Z_OFFSET;
990 val.azalea_write(ctx, buf)
991 }
992}
993
994impl AzBuf for GlobalPos {
995 fn azalea_read(ctx: azalea_buf::Ctx, buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
996 Ok(GlobalPos {
997 dimension: Identifier::azalea_read(ctx, buf)?,
998 pos: BlockPos::azalea_read(ctx, buf)?,
999 })
1000 }
1001 fn azalea_write(&self, ctx: azalea_buf::Ctx, buf: &mut impl Write) -> io::Result<()> {
1002 Identifier::azalea_write(&self.dimension, ctx, buf)?;
1003 BlockPos::azalea_write(&self.pos, ctx, buf)?;
1004
1005 Ok(())
1006 }
1007}
1008
1009impl AzBuf for ChunkSectionPos {
1010 fn azalea_read(ctx: azalea_buf::Ctx, buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
1011 let long = i64::azalea_read(ctx, buf)?;
1012 Ok(ChunkSectionPos {
1013 x: (long >> 42) as i32,
1014 y: (long << 44 >> 44) as i32,
1015 z: (long << 22 >> 42) as i32,
1016 })
1017 }
1018 fn azalea_write(&self, ctx: azalea_buf::Ctx, buf: &mut impl Write) -> io::Result<()> {
1019 let long = (((self.x & 0x3FFFFF) as i64) << 42)
1020 | (self.y & 0xFFFFF) as i64
1021 | (((self.z & 0x3FFFFF) as i64) << 20);
1022 long.azalea_write(ctx, buf)?;
1023 Ok(())
1024 }
1025}
1026
1027fn parse_three_values<T>(s: &str) -> Result<[T; 3], &'static str>
1028where
1029 T: FromStr,
1030 <T as FromStr>::Err: fmt::Debug,
1031{
1032 let parts = s.split_whitespace().collect::<Vec<_>>();
1033 if parts.len() != 3 {
1034 return Err("Expected three values");
1035 }
1036
1037 let x = parts[0].parse().map_err(|_| "Invalid X value")?;
1038 let y = parts[1].parse().map_err(|_| "Invalid Y value")?;
1039 let z = parts[2].parse().map_err(|_| "Invalid Z value")?;
1040
1041 Ok([x, y, z])
1042}
1043
1044impl FromStr for BlockPos {
1050 type Err = &'static str;
1051
1052 fn from_str(s: &str) -> Result<Self, Self::Err> {
1053 let [x, y, z] = parse_three_values::<i32>(s)?;
1054 Ok(BlockPos { x, y, z })
1055 }
1056}
1057
1058impl FromStr for Vec3 {
1064 type Err = &'static str;
1065
1066 fn from_str(s: &str) -> Result<Self, Self::Err> {
1067 let [x, y, z] = parse_three_values::<f64>(s)?;
1068 Ok(Vec3 { x, y, z })
1069 }
1070}
1071
1072#[cfg(test)]
1073mod tests {
1074 use super::*;
1075
1076 #[test]
1077 fn test_from_block_pos_to_chunk_pos() {
1078 let block_pos = BlockPos::new(5, 78, -2);
1079 let chunk_pos = ChunkPos::from(&block_pos);
1080 assert_eq!(chunk_pos, ChunkPos::new(0, -1));
1081 }
1082
1083 #[test]
1084 fn test_from_block_pos_to_chunk_block_pos() {
1085 let block_pos = BlockPos::new(5, 78, -2);
1086 let chunk_block_pos = ChunkBlockPos::from(&block_pos);
1087 assert_eq!(chunk_block_pos, ChunkBlockPos::new(5, 78, 14));
1088 }
1089
1090 #[test]
1091 fn test_from_entity_pos_to_block_pos() {
1092 let entity_pos = Vec3 {
1093 x: 31.5,
1094 y: 80.0,
1095 z: -16.1,
1096 };
1097 let block_pos = BlockPos::from(&entity_pos);
1098 assert_eq!(block_pos, BlockPos::new(31, 80, -17));
1099 }
1100
1101 #[test]
1102 fn test_from_entity_pos_to_chunk_pos() {
1103 let entity_pos = Vec3 {
1104 x: 31.5,
1105 y: 80.0,
1106 z: -16.1,
1107 };
1108 let chunk_pos = ChunkPos::from(&entity_pos);
1109 assert_eq!(chunk_pos, ChunkPos::new(1, -2));
1110 }
1111
1112 #[test]
1113 fn test_read_blockpos_from() {
1114 let mut buf = Vec::new();
1115 13743895338965u64
1116 .azalea_write(Default::default(), &mut buf)
1117 .unwrap();
1118 let mut buf = Cursor::new(&buf[..]);
1119 let block_pos = BlockPos::azalea_read(Default::default(), &mut buf).unwrap();
1120 assert_eq!(block_pos, BlockPos::new(49, -43, -3));
1121 }
1122
1123 #[test]
1124 fn test_into_chunk_section_block_pos() {
1125 let block_pos = BlockPos::new(0, -60, 0);
1126 assert_eq!(
1127 ChunkSectionBlockPos::from(block_pos),
1128 ChunkSectionBlockPos::new(0, 4, 0)
1129 );
1130 }
1131
1132 #[test]
1133 fn test_read_chunk_pos_from() {
1134 let mut buf = Vec::new();
1135 ChunkPos::new(2, -1)
1136 .azalea_write(Default::default(), &mut buf)
1137 .unwrap();
1138 let mut buf = Cursor::new(&buf[..]);
1139 let chunk_pos = ChunkPos::from(u64::azalea_read(Default::default(), &mut buf).unwrap());
1140 assert_eq!(chunk_pos, ChunkPos::new(2, -1));
1141 }
1142}