-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtile.rs
581 lines (488 loc) · 17 KB
/
tile.rs
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
use core::{fmt::Display, iter::FusedIterator, ops::Add};
use crate::prelude::*;
#[cfg(any(test, feature = "serde"))]
use serde::{Deserialize, Serialize};
/// A tile in 2d space
#[must_use]
#[derive(Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(any(test, feature = "serde"), derive(Serialize, Deserialize))]
pub struct Tile<const WIDTH: u8, const HEIGHT: u8>(u8);
impl<const WIDTH: u8, const HEIGHT: u8> From<Tile<WIDTH, HEIGHT>> for DynamicTile {
fn from(val: Tile<WIDTH, HEIGHT>) -> Self {
DynamicTile(Vector {
x: val.x().try_into().unwrap(),
y: val.y().try_into().unwrap(),
})
}
}
impl<const WIDTH: u8, const HEIGHT: u8, V: AsRef<Vector>> Add<V> for Tile<WIDTH, HEIGHT> {
type Output = Option<Self>;
fn add(self, rhs: V) -> Self::Output {
self.const_add(rhs.as_ref())
}
}
impl<const WIDTH: u8, const HEIGHT: u8> From<Tile<WIDTH, HEIGHT>> for u8 {
fn from(value: Tile<WIDTH, HEIGHT>) -> Self {
value.0
}
}
impl<const WIDTH: u8, const HEIGHT: u8> From<&Tile<WIDTH, HEIGHT>> for u8 {
fn from(value: &Tile<WIDTH, HEIGHT>) -> Self {
value.0
}
}
impl<const WIDTH: u8, const HEIGHT: u8> From<Tile<WIDTH, HEIGHT>> for usize {
fn from(value: Tile<WIDTH, HEIGHT>) -> Self {
value.0.into()
}
}
impl<const WIDTH: u8, const HEIGHT: u8> From<&Tile<WIDTH, HEIGHT>> for usize {
fn from(value: &Tile<WIDTH, HEIGHT>) -> Self {
value.0.into()
}
}
impl<const WIDTH: u8, const HEIGHT: u8> Display for Tile<WIDTH, HEIGHT> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "({},{})", self.x(), self.y())
}
}
impl<const WIDTH: u8, const HEIGHT: u8> core::fmt::Debug for Tile<WIDTH, HEIGHT> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "({},{})", self.x(), self.y())
}
}
impl<const WIDTH: u8, const HEIGHT: u8> Tile<WIDTH, HEIGHT> {
pub const NORTH_WEST: Self = Self(0);
pub const NORTH_EAST: Self = Self::new_unchecked(Self::MAX_COL, 0);
pub const SOUTH_WEST: Self = Self::new_unchecked(0, Self::MAX_ROW);
pub const SOUTH_EAST: Self = Self::new_unchecked(Self::MAX_COL, Self::MAX_ROW);
pub const MAX_COL: u8 = WIDTH - 1;
pub const MAX_ROW: u8 = HEIGHT - 1;
pub const COUNT: usize = WIDTH as usize * HEIGHT as usize;
pub const CENTER: Self = Self::new_unchecked(WIDTH / 2, HEIGHT / 2);
pub const fn new_const<const X: u8, const Y: u8>() -> Self {
Self::new_unchecked(X, Y)
}
pub(crate) const fn new_unchecked(x: u8, y: u8) -> Self {
debug_assert!(x < WIDTH);
debug_assert!(y < HEIGHT);
debug_assert!(Self::COUNT <= u8::MAX as usize);
Self(x + (WIDTH * y))
}
#[must_use]
pub const fn try_new(x: u8, y: u8) -> Option<Self> {
if x >= WIDTH {
return None;
}
if y >= HEIGHT {
return None;
}
let Some(i1) = y.checked_mul(WIDTH) else {
return None;
};
let Some(i2) = i1.checked_add(x) else {
return None;
};
Self::try_from_inner(i2)
}
#[must_use]
pub const fn try_from_dynamic(dynamic_tile: DynamicTile) -> Option<Self> {
if dynamic_tile.0.x.is_negative() || dynamic_tile.0.y.is_negative() {
return None;
}
Self::try_new(
dynamic_tile.0.x.unsigned_abs(),
dynamic_tile.0.y.unsigned_abs(),
)
}
#[must_use]
pub const fn x(&self) -> u8 {
self.0 % WIDTH
}
#[must_use]
pub const fn y(&self) -> u8 {
self.0 / WIDTH
}
#[must_use]
pub const fn inner(&self) -> u8 {
self.0
}
#[must_use]
pub const fn try_from_inner(inner: u8) -> Option<Self> {
if inner <= Self::SOUTH_EAST.inner() {
Some(Self(inner))
} else {
None
}
}
pub(crate) const fn from_inner_unchecked(inner: u8) -> Self {
Self(inner)
}
#[must_use]
#[allow(clippy::cast_possible_truncation)]
pub const fn try_from_usize(value: usize) -> Option<Self> {
if value >= Self::COUNT {
return None;
}
let inner = value as u8;
Some(Self(inner))
}
pub const fn flip(&self, axes: FlipAxes) -> Self {
match axes {
FlipAxes::None => *self,
FlipAxes::Horizontal => Self::new_unchecked(Self::MAX_COL - self.x(), self.y()),
FlipAxes::Vertical => Self::new_unchecked(self.x(), Self::MAX_ROW - self.y()),
FlipAxes::Both => {
Self::new_unchecked(Self::MAX_COL - self.x(), Self::MAX_ROW - self.y())
}
}
}
#[must_use]
pub const fn try_next(&self) -> Option<Self> {
let Some(next) = self.inner().checked_add(1) else {
return None;
};
Self::try_from_inner(next)
}
/// Iterate through all tiles by row
/// This method has better performance than `iter_by_col`
pub fn iter_by_row(
) -> impl FusedIterator<Item = Self> + Clone + ExactSizeIterator + DoubleEndedIterator {
(0..(WIDTH * HEIGHT)).map(Self)
}
/// Iterate through all tiles by column
/// This method has worse performance than `iter_by_row`
pub fn iter_by_col(
) -> impl FusedIterator<Item = Self> + ExactSizeIterator + Clone + DoubleEndedIterator {
Tile::<HEIGHT, WIDTH>::iter_by_row().map(Tile::transpose)
}
/// Return this tile in a transposed grid system (i.e. the height and width are swapped)
///
/// # Panics
/// If the tile is invalid
pub const fn transpose(self) -> Tile<HEIGHT, WIDTH> {
if let Some(r) = Tile::try_new(self.y(), self.x()) {
r
} else {
panic!("Cannot transpose invalid tile")
}
}
/// Iterate through adjacent elements (includes diagonals)
#[must_use]
pub fn iter_adjacent(self) -> impl FusedIterator<Item = Self> + DoubleEndedIterator + Clone {
Vector::UNITS.into_iter().filter_map(move |v| self + v)
}
/// Iterate through contiguous elements (does not include diagonals)
#[must_use]
pub fn iter_contiguous(self) -> impl FusedIterator<Item = Self> + DoubleEndedIterator + Clone {
Vector::CARDINALS.into_iter().filter_map(move |v| self + v)
}
/// Whether two tiles are adjacent (includes diagonals)
#[must_use]
pub const fn is_adjacent_to(&self, rhs: &Self) -> bool {
self.0 != rhs.0 && self.x().abs_diff(rhs.x()) <= 1 && self.y().abs_diff(rhs.y()) <= 1
}
/// Whether two tiles are contiguous (does not include diagonals)
#[must_use]
pub const fn is_contiguous_with(&self, rhs: &Self) -> bool {
if self.0 == rhs.0 {
return false;
}
let c = self.x().abs_diff(rhs.x());
let r = self.y().abs_diff(rhs.y());
if c <= 1 && r <= 1 && (c == 1) ^ (r == 1) {
return true;
}
false
}
#[must_use]
pub const fn const_add(&self, vector: &Vector) -> Option<Self> {
let Some(c) = self.x().checked_add_signed(vector.x) else {
return None;
};
let Some(r) = self.y().checked_add_signed(vector.y) else {
return None;
};
Self::try_new(c, r)
}
#[must_use]
pub const fn get_vertex(&self, corner: &Corner) -> Option<Vertex<WIDTH, HEIGHT>> {
match corner {
Corner::NorthWest => Vertex::try_new(self.x(), self.y()),
Corner::NorthEast => Vertex::try_new(self.x() + 1, self.y()),
Corner::SouthWest => Vertex::try_new(self.x(), self.y() + 1),
Corner::SouthEast => Vertex::try_new(self.x() + 1, self.y() + 1),
}
}
pub const fn get_north_west_vertex(&self) -> Vertex<WIDTH, HEIGHT> {
Vertex::new_unchecked(self.x(), self.y())
}
/// Returns the Manhattan distance between two tiles.
/// Also known as the taxicab distance, the Manhattan distance is the sum of the distances in the two axes.
#[must_use]
pub const fn manhattan_distance(&self, other: &Self) -> u8 {
self.x().abs_diff(other.x()) + self.y().abs_diff(other.y())
}
/// Returns true if this is an edge tile (or corner tile)
#[must_use]
pub const fn is_edge(&self) -> bool {
(self.x() == 0 || self.x() == Self::MAX_COL) || (self.y() == 0 || self.y() == Self::MAX_ROW)
}
/// Returns true if this is a corner tile
#[must_use]
pub const fn is_corner(&self) -> bool {
Self::NORTH_EAST.0 == self.0
|| Self::NORTH_WEST.0 == self.0
|| Self::SOUTH_EAST.0 == self.0
|| Self::SOUTH_WEST.0 == self.0
}
/// Returns the number of adjacent tiles
/// 3 for a corner tile
/// 5 for an edge tile
/// 8 otherwise
#[must_use]
pub const fn adjacent_tile_count(&self) -> u8 {
if self.is_corner() {
3
} else if self.is_edge() {
5
} else {
8
}
}
}
impl<const L: u8> Tile<L, L> {
pub const fn rotate(&self, quarter_turns: QuarterTurns) -> Self {
match quarter_turns {
QuarterTurns::Zero => *self,
QuarterTurns::One => Self::new_unchecked(L - 1 - self.y(), self.x()),
QuarterTurns::Two => Self::new_unchecked(L - 1 - self.x(), L - 1 - self.y()),
QuarterTurns::Three => Self::new_unchecked(self.y(), L - 1 - self.x()),
}
}
}
#[cfg(any(test, feature = "glam"))]
impl<const C: u8, const R: u8> HasCenter for Tile<C, R> {
fn get_center(&self, scale: f32) -> glam::f32::Vec2 {
let x = scale * (f32::from(self.x()) + 0.5);
let y = scale * (f32::from(self.y()) + 0.5);
glam::f32::Vec2 { x, y }
}
}
#[cfg(test)]
mod tests {
use super::*;
use itertools::Itertools;
use serde_test::{assert_tokens, Token};
#[test]
fn test_iter_by_row() {
let str = Tile::<3, 4>::iter_by_row().join("|");
assert_eq!(
str,
"(0,0)|(1,0)|(2,0)|(0,1)|(1,1)|(2,1)|(0,2)|(1,2)|(2,2)|(0,3)|(1,3)|(2,3)",
)
}
#[test]
fn test_iter_by_col() {
let str = Tile::<3, 4>::iter_by_col().join("|");
assert_eq!(
str,
"(0,0)|(0,1)|(0,2)|(0,3)|(1,0)|(1,1)|(1,2)|(1,3)|(2,0)|(2,1)|(2,2)|(2,3)",
)
}
#[test]
fn test_from() {
for tile in Tile::<3, 4>::iter_by_row() {
let n = Tile::try_new(tile.x(), tile.y()).unwrap();
assert_eq!(tile, n)
}
}
#[test]
fn test_flip1() {
let str = Tile::<3, 3>::iter_by_row()
.map(|x| x.flip(FlipAxes::Vertical))
.join("|");
assert_eq!(str, "(0,2)|(1,2)|(2,2)|(0,1)|(1,1)|(2,1)|(0,0)|(1,0)|(2,0)")
}
#[test]
fn test_rotate1() {
let str = Tile::<3, 3>::iter_by_row()
.map(|x| x.rotate(QuarterTurns::One))
.join("|");
assert_eq!(str, "(2,0)|(2,1)|(2,2)|(1,0)|(1,1)|(1,2)|(0,0)|(0,1)|(0,2)")
}
#[test]
fn test_flip2() {
let tile: Tile<4, 4> = Tile::new_const::<1, 2>();
assert_eq!(tile.flip(FlipAxes::None), Tile::new_const::<1, 2>());
assert_eq!(tile.flip(FlipAxes::Horizontal), Tile::new_const::<2, 2>());
assert_eq!(tile.flip(FlipAxes::Vertical), Tile::new_const::<1, 1>());
assert_eq!(tile.flip(FlipAxes::Both), Tile::new_const::<2, 1>());
}
#[test]
fn test_rotate2() {
let tile: Tile<4, 4> = Tile::new_const::<0, 0>();
assert_eq!(tile.rotate(QuarterTurns::Zero), Tile::new_const::<0, 0>());
assert_eq!(tile.rotate(QuarterTurns::One), Tile::new_const::<3, 0>());
assert_eq!(tile.rotate(QuarterTurns::Two), Tile::new_const::<3, 3>());
assert_eq!(tile.rotate(QuarterTurns::Three), Tile::new_const::<0, 3>());
}
#[test]
fn test_serde() {
let tile: Tile<3, 3> = Tile(2);
assert_tokens(
&tile,
&[Token::NewtypeStruct { name: "Tile" }, Token::U8(2)],
);
}
#[test]
fn test_manhattan() {
let a: Tile<3, 3> = Tile::new_const::<0, 0>();
let b: Tile<3, 3> = Tile::new_const::<2, 1>();
assert_eq!(a.manhattan_distance(&b), 3);
assert_eq!(b.manhattan_distance(&a), 3);
}
#[test]
fn test_add() {
let tile: Tile<3, 3> = Tile::new_const::<1, 1>();
assert_eq!(tile + Vector::NORTH, Tile::try_new(1, 0))
}
#[test]
fn test_add_gives_none() {
let tile: Tile<4, 4> = Tile::new_const::<3, 0>();
let r = tile + Vector::new(1, 0);
assert_eq!(r, None)
}
#[test]
fn test_int_from() {
let tile: Tile<3, 3> = Tile::new_const::<1, 1>();
assert_eq!(<Tile<3, 3> as Into<u8>>::into(tile), 4u8);
assert_eq!(<Tile<3, 3> as Into<usize>>::into(tile), 4usize);
assert_eq!(<&Tile<3, 3> as Into<u8>>::into(&tile), 4u8);
assert_eq!(<&Tile<3, 3> as Into<usize>>::into(&tile), 4usize);
}
#[test]
fn test_get_center() {
let tile: Tile<3, 3> = Tile::new_const::<1, 2>();
assert_eq!(tile.get_center(2.0), glam::f32::Vec2::new(3.0, 5.0));
}
#[test]
fn test_debug() {
let tile: Tile<3, 3> = Tile::new_const::<1, 2>();
assert_eq!(format!("{tile:?}"), "(1,2)")
}
#[test]
fn test_try_from() {
assert_eq!(
Tile::<3, 3>::try_from_inner(8),
Some(Tile::new_const::<2, 2>())
);
assert_eq!(
Tile::<3, 3>::try_from_usize(8),
Some(Tile::new_const::<2, 2>())
);
assert_eq!(Tile::<3, 3>::try_from_inner(9), None);
assert_eq!(Tile::<3, 3>::try_from_usize(9), None);
}
#[test]
fn test_try_next() {
let mut tile = Tile::<3, 3>(0);
let mut i = 0;
loop {
assert_eq!(tile.inner(), i);
i += 1;
if let Some(next) = tile.try_next() {
tile = next;
} else {
assert_eq!(i, 9);
break;
}
}
}
#[test]
fn test_get_vertex() {
let tile = Tile::<2, 2>::new_const::<0, 0>();
assert_eq!(
tile.get_vertex(&Corner::NorthWest),
Some(Vertex::new_const::<0, 0>())
);
assert_eq!(
tile.get_vertex(&Corner::NorthEast),
Some(Vertex::new_const::<1, 0>())
);
assert_eq!(
tile.get_vertex(&Corner::SouthWest),
Some(Vertex::new_const::<0, 1>())
);
assert_eq!(
tile.get_vertex(&Corner::SouthEast),
Some(Vertex::new_const::<1, 1>())
);
assert_eq!(tile.get_north_west_vertex(), Vertex::new_const::<0, 0>())
}
#[test]
fn test_adjacent() {
let tile = Tile::<3, 3>::new_const::<0, 0>();
let expected_adjacent_tiles = [
Tile::<3, 3>::new_const::<1, 0>(),
Tile::<3, 3>::new_const::<1, 1>(),
Tile::<3, 3>::new_const::<0, 1>(),
];
assert_eq!(tile.iter_adjacent().collect_vec(), expected_adjacent_tiles);
for rhs in Tile::<3, 3>::iter_by_row() {
let expected = expected_adjacent_tiles.contains(&rhs);
let actual = tile.is_adjacent_to(&rhs);
assert_eq!(expected, actual)
}
}
#[test]
fn test_contiguous() {
let tile = Tile::<3, 3>::new_const::<0, 0>();
let expected_contiguous_tiles = [
Tile::<3, 3>::new_const::<1, 0>(),
Tile::<3, 3>::new_const::<0, 1>(),
];
assert_eq!(
tile.iter_contiguous().collect_vec(),
expected_contiguous_tiles
);
for rhs in Tile::<3, 3>::iter_by_row() {
let expected = expected_contiguous_tiles.contains(&rhs);
let actual = tile.is_contiguous_with(&rhs);
assert_eq!(expected, actual, "{}", rhs)
}
}
#[test]
fn test_from_dynamic() {
let pairs = [
((0i8, 0i8), Some((0u8, 0u8))),
((1i8, 2i8), Some((1u8, 2u8))),
((3i8, 0i8), None),
((0i8, 3i8), None),
((-1i8, 0i8), None),
((0i8, -1i8), None),
];
for ((dyn_x, dyn_y), tile_option) in pairs {
let expected = tile_option.map(|(x, y)| Tile::<3, 3>::try_new(x, y).unwrap());
let dynamic_tile = DynamicTile(Vector { x: dyn_x, y: dyn_y });
let actual = Tile::<3, 3>::try_from_dynamic(dynamic_tile);
assert_eq!(actual, expected);
}
}
#[test]
fn test_is_corner() {
let corners: TileSet16<3, 4, 12> = TileSet16::from_fn(|tile| tile.is_corner());
assert_eq!("*_*\n___\n___\n*_*", corners.to_string())
}
#[test]
fn test_is_edge() {
let edges: TileSet16<3, 4, 12> = TileSet16::from_fn(|tile| tile.is_edge());
assert_eq!("***\n*_*\n*_*\n***", edges.to_string())
}
#[test]
fn test_adjacent_tile_count() {
let adjacencies: TileMap<u8, 3, 4, 12> =
TileMap::from_fn(|tile| tile.adjacent_tile_count());
assert_eq!("3|5|3\n5|8|5\n5|8|5\n3|5|3", adjacencies.to_string())
}
}