@@ -4,7 +4,9 @@ use std::fs::File;
44use std:: io;
55use std:: path:: Path ;
66
7+ use derive_more:: Deref ;
78use foldhash:: fast:: RandomState ;
9+ use glam:: { IVec2 , IVec3 } ;
810use indexmap:: IndexMap ;
911use ndarray:: { self , Array3 , Axis } ;
1012
@@ -28,60 +30,41 @@ pub struct Key(KeyType);
2830/// An XY coordinate pair in the BYOND coordinate system.
2931///
3032/// The lower-left corner is `{ x: 1, y: 1 }`.
31- #[ derive( Clone , Copy , Debug , Eq , PartialEq , Ord , PartialOrd , Hash ) ]
32- pub struct Coord2 {
33- pub x : i32 ,
34- pub y : i32 ,
35- }
33+ #[ derive( Deref ) ]
34+ pub struct Coord2 ( IVec2 ) ;
3635
3736impl Coord2 {
38- #[ inline]
39- pub fn new ( x : i32 , y : i32 ) -> Coord2 {
40- Coord2 { x, y }
41- }
42-
4337 #[ inline]
4438 pub fn z ( self , z : i32 ) -> Coord3 {
45- Coord3 {
46- x : self . x ,
47- y : self . y ,
48- z,
49- }
39+ self . extend ( z) . into ( )
5040 }
5141
42+ #[ inline]
5243 fn to_raw ( self , ( dim_y, dim_x) : ( usize , usize ) ) -> ( usize , usize ) {
5344 assert ! (
54- self . x >= 1 && self . x <= dim_x as i32 ,
45+ ( 1 ..= ( dim_x as i32 ) ) . contains ( & self . x ) ,
5546 "x={} not in [1, {}]" ,
5647 self . x,
5748 dim_x
5849 ) ;
5950 assert ! (
60- self . y >= 1 && self . y <= dim_y as i32 ,
51+ ( 1 ..= ( dim_y as i32 ) ) . contains ( & self . y ) ,
6152 "y={} not in [1, {}]" ,
6253 self . y,
6354 dim_y
6455 ) ;
6556 ( dim_y - self . y as usize , self . x as usize - 1 )
6657 }
6758
59+ #[ inline]
6860 fn from_raw ( ( y, x) : ( usize , usize ) , ( dim_y, _dim_x) : ( usize , usize ) ) -> Coord2 {
69- Coord2 {
70- x : x as i32 + 1 ,
71- y : ( dim_y - y) as i32 ,
72- }
61+ Coord2 ( IVec2 :: new ( x as i32 + 1 , ( dim_y - y) as i32 ) )
7362 }
7463}
7564
76- impl std:: ops:: Add < Dir > for Coord2 {
77- type Output = Coord2 ;
78-
79- fn add ( self , rhs : Dir ) -> Coord2 {
80- let ( x, y) = rhs. offset ( ) ;
81- Coord2 {
82- x : self . x + x,
83- y : self . y + y,
84- }
65+ impl From < IVec2 > for Coord2 {
66+ fn from ( value : IVec2 ) -> Self {
67+ Self ( value)
8568 }
8669}
8770
@@ -91,42 +74,26 @@ impl std::ops::Add<Dir> for Coord2 {
9174///
9275/// Note that BYOND by default considers "UP" to be Z+1, but this does not
9376/// necessarily apply to a given game's logic.
94- #[ derive( Clone , Copy , Debug , Eq , PartialEq , Ord , PartialOrd , Hash ) ]
95- pub struct Coord3 {
96- pub x : i32 ,
97- pub y : i32 ,
98- pub z : i32 ,
99- }
77+ #[ derive( Deref ) ]
78+ pub struct Coord3 ( IVec3 ) ;
10079
10180impl Coord3 {
10281 #[ inline]
103- pub fn new ( x : i32 , y : i32 , z : i32 ) -> Coord3 {
104- Coord3 { x, y, z }
105- }
106-
107- #[ inline]
108- pub fn xy ( self ) -> Coord2 {
109- Coord2 {
110- x : self . x ,
111- y : self . y ,
112- }
113- }
114-
11582 fn to_raw ( self , ( dim_z, dim_y, dim_x) : ( usize , usize , usize ) ) -> ( usize , usize , usize ) {
11683 assert ! (
117- self . x >= 1 && self . x <= dim_x as i32 ,
84+ ( 1 ..= ( dim_x as i32 ) ) . contains ( & self . x ) ,
11885 "x={} not in [1, {}]" ,
11986 self . x,
12087 dim_x
12188 ) ;
12289 assert ! (
123- self . y >= 1 && self . y <= dim_y as i32 ,
90+ ( 1 ..= ( dim_y as i32 ) ) . contains ( & self . y ) ,
12491 "y={} not in [1, {}]" ,
12592 self . y,
12693 dim_y
12794 ) ;
12895 assert ! (
129- self . z >= 1 && self . z <= dim_z as i32 ,
96+ ( 1 ..= ( dim_z as i32 ) ) . contains ( & self . z ) ,
13097 "y={} not in [1, {}]" ,
13198 self . z,
13299 dim_z
@@ -139,15 +106,18 @@ impl Coord3 {
139106 }
140107
141108 #[ allow( dead_code) ]
109+ #[ inline]
142110 fn from_raw (
143111 ( z, y, x) : ( usize , usize , usize ) ,
144112 ( _dim_z, dim_y, _dim_x) : ( usize , usize , usize ) ,
145113 ) -> Coord3 {
146- Coord3 {
147- x : x as i32 + 1 ,
148- y : ( dim_y - y) as i32 ,
149- z : z as i32 + 1 ,
150- }
114+ Coord3 ( IVec3 :: new ( x as i32 + 1 , ( dim_y - y) as i32 , z as i32 + 1 ) )
115+ }
116+ }
117+
118+ impl From < IVec3 > for Coord3 {
119+ fn from ( value : IVec3 ) -> Self {
120+ Self ( value)
151121 }
152122}
153123
0 commit comments