1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
use crate::UnitType;
use core::ops::{Add, Div, DivAssign, Index, IndexMut, Mul, MulAssign, Sub};
use derive_more::{Add, AddAssign, Display, From, Sub, SubAssign};

#[derive(
    Default, Debug, Display, Copy, Clone, Eq, PartialEq, Add, Sub, AddAssign, SubAssign, From, Hash,
)]
#[display(fmt = "Position<{}> ({}, {})", N, x, y)]
pub struct ScaledPosition<const N: i32> {
    pub x: i32,
    pub y: i32,
}

pub type Position = ScaledPosition<1>;
pub type WalkPosition = ScaledPosition<8>;
pub type TilePosition = ScaledPosition<32>;

#[derive(Debug, Copy, Clone, PartialEq, Add, Sub, AddAssign, SubAssign, From)]
pub struct Vector2D {
    pub x: f64,
    pub y: f64,
}

pub type PositionTuple = (i32, i32);
pub const ORIGIN: Position = Position { x: 0, y: 0 };
pub const WALK_POSITION_8_DIR: [WalkPosition; 8] = dir_8(1, 1);
pub const WALK_POSITION_4_DIR: [WalkPosition; 4] = dir_4();

pub const fn dir_4<const N: i32>() -> [ScaledPosition<N>; 4] {
    [
        ScaledPosition::<N>::new(0, -1),
        ScaledPosition::<N>::new(-1, 0),
        ScaledPosition::<N>::new(1, 0),
        ScaledPosition::<N>::new(0, 1),
    ]
}

pub const fn dir_8<const N: i32>(dx: i32, dy: i32) -> [ScaledPosition<N>; 8] {
    [
        ScaledPosition::<N>::new(-dx, -dy),
        ScaledPosition::<N>::new(0, -dy),
        ScaledPosition::<N>::new(dx, -dy),
        ScaledPosition::<N>::new(dx, 0),
        ScaledPosition::<N>::new(dx, dy),
        ScaledPosition::<N>::new(0, dy),
        ScaledPosition::<N>::new(-dx, dy),
        ScaledPosition::<N>::new(-dx, 0),
    ]
}

const fn pos_to_pos<const I: i32, const O: i32>(pos: ScaledPosition<I>) -> ScaledPosition<O> {
    ScaledPosition {
        x: pos.x * I / O,
        y: pos.y * I / O,
    }
}

impl Position {
    pub const fn to_tile_position(self) -> TilePosition {
        pos_to_pos(self)
    }

    pub const fn to_walk_position(self) -> WalkPosition {
        pos_to_pos(self)
    }

    pub fn get_approx_distance<P: Into<Position>>(&self, other: P) -> i32 {
        let p = other.into();
        let mut max = (self.x - p.x).abs();
        let mut min = (self.y - p.y).abs();

        if max < min {
            core::mem::swap(&mut max, &mut min);
        }

        if min <= (max >> 2) {
            return max;
        }

        let min_calc = (3 * min) >> 3;
        (min_calc >> 5) + min_calc + max - (max >> 4) - (max >> 6)
    }
}

impl TilePosition {
    pub const fn to_position(self) -> Position {
        pos_to_pos(self)
    }

    pub const fn to_walk_position(self) -> WalkPosition {
        pos_to_pos(self)
    }

    pub const fn center(self) -> Position {
        Position::new(self.x * 32 + 16, self.y * 32 + 16)
    }
}

impl WalkPosition {
    pub const fn to_tile_position(self) -> TilePosition {
        pos_to_pos(self)
    }

    pub const fn to_position(self) -> Position {
        pos_to_pos(self)
    }

    pub const fn center(self) -> Position {
        Position::new(self.x * 8 + 4, self.y * 8 + 4)
    }
}

impl Vector2D {
    pub const fn new(x: f64, y: f64) -> Self {
        Self { x, y }
    }
}

pub trait PositionValidator {
    fn is_valid<const N: i32>(&self, pos: ScaledPosition<N>) -> bool;
}

impl<const N: i32> ScaledPosition<N> {
    pub fn new_checked(validator: impl PositionValidator, x: i32, y: i32) -> Option<Self> {
        let pos = Self::new(x, y);
        if validator.is_valid(pos) {
            Some(pos)
        } else {
            None
        }
    }

    pub const fn new(x: i32, y: i32) -> Self {
        Self { x, y }
    }

    pub fn is_valid(self, validator: &impl PositionValidator) -> bool {
        validator.is_valid(self)
    }

    pub const fn distance_squared(&self, other: Self) -> u32 {
        let dx = self.x - other.x;
        let dy = self.y - other.y;
        (dx * dx + dy * dy) as u32
    }

    pub fn distance(&self, other: Self) -> f64 {
        (self.distance_squared(other) as f64).sqrt()
    }

    pub fn chebyshev_distance(&self, other: Self) -> u32 {
        (self.x - other.x).abs().max((self.y - other.y).abs()) as u32
    }
}

impl From<UnitType> for TilePosition {
    fn from(ut: UnitType) -> Self {
        ut.tile_size()
    }
}

impl<const N: i32> From<ScaledPosition<N>> for PositionTuple {
    fn from(pos: ScaledPosition<N>) -> Self {
        (pos.x, pos.y)
    }
}

impl<const N: i32> Mul<i32> for ScaledPosition<N> {
    type Output = Self;

    fn mul(self, other: i32) -> Self::Output {
        Self::Output {
            x: self.x * other,
            y: self.y * other,
        }
    }
}

impl<const N: i32> Mul<ScaledPosition<N>> for i32 {
    type Output = ScaledPosition<N>;

    fn mul(self, other: ScaledPosition<N>) -> Self::Output {
        Self::Output {
            x: self * other.x,
            y: self * other.y,
        }
    }
}

impl<const N: i32> MulAssign<i32> for ScaledPosition<N> {
    fn mul_assign(&mut self, rhs: i32) {
        self.x *= rhs;
        self.y *= rhs;
    }
}

impl<const N: i32> DivAssign<i32> for ScaledPosition<N> {
    fn div_assign(&mut self, rhs: i32) {
        self.x /= rhs;
        self.y /= rhs;
    }
}

impl<const N: i32> Div<i32> for ScaledPosition<N> {
    type Output = Self;
    fn div(self, other: i32) -> Self::Output {
        Self::Output {
            x: self.x / other,
            y: self.y / other,
        }
    }
}

impl<const N: i32> Sub<PositionTuple> for ScaledPosition<N> {
    type Output = Self;

    fn sub(self, other: PositionTuple) -> Self::Output {
        Self::Output {
            x: self.x - other.0,
            y: self.y - other.1,
        }
    }
}

impl<const N: i32> Add<PositionTuple> for ScaledPosition<N> {
    type Output = Self;

    fn add(self, other: PositionTuple) -> Self::Output {
        Self::Output {
            x: self.x + other.0,
            y: self.y + other.1,
        }
    }
}

impl<const N: i32> Add<i32> for ScaledPosition<N> {
    type Output = Self;

    fn add(self, other: i32) -> Self::Output {
        Self::Output {
            x: self.x + other,
            y: self.y + other,
        }
    }
}

impl<const N: i32> Sub<i32> for ScaledPosition<N> {
    type Output = Self;

    fn sub(self, other: i32) -> Self::Output {
        Self::Output {
            x: self.x - other,
            y: self.y - other,
        }
    }
}

pub trait PositionIndexed<const N: i32> {
    // empty
}

impl<T: PositionIndexed<N>, const N: i32, const M: usize> Index<ScaledPosition<N>> for Vec<[T; M]> {
    type Output = T;

    fn index(&self, index: ScaledPosition<N>) -> &Self::Output {
        &(self as &Vec<[T; M]>)[index.y as usize][index.x as usize]
    }
}

impl<T: PositionIndexed<N>, const N: i32, const M: usize> IndexMut<ScaledPosition<N>>
    for Vec<[T; M]>
{
    fn index_mut(&mut self, index: ScaledPosition<N>) -> &mut Self::Output {
        &mut (self as &mut Vec<[T; M]>)[index.y as usize][index.x as usize]
    }
}

impl<T: PositionIndexed<N>, const N: i32, const M: usize> Index<ScaledPosition<N>> for [[T; M]] {
    type Output = T;

    fn index(&self, index: ScaledPosition<N>) -> &Self::Output {
        &(self as &[[T; M]])[index.y as usize][index.x as usize]
    }
}

impl<T: PositionIndexed<N>, const N: i32, const M: usize> IndexMut<ScaledPosition<N>>
    for [[T; M]]
{
    fn index_mut(&mut self, index: ScaledPosition<N>) -> &mut Self::Output {
        &mut (self as &mut [[T; M]])[index.y as usize][index.x as usize]
    }
}