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
pub use direction::CardinalDirection;
use direction::CardinalDirections;
use grid_2d::Grid;
pub use grid_2d::{Coord, Size};
pub use grid_search_cardinal_common::can_enter::CanEnter;
use grid_search_cardinal_common::{
    coord::UNIT_COORDS,
    path::Path,
    seen_set::{SeenSet, Visit},
    step::Step,
};
#[cfg(feature = "serialize")]
use serde::{Deserialize, Serialize};
use std::collections::VecDeque;

pub type Distance = u32;

#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
struct Cell {
    count: u64,
    distance: Distance,
}

#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct DistanceMap {
    count: u64,
    grid: Grid<Cell>,
}

#[derive(Debug, Clone)]
struct PopulateNode {
    coord: Coord,
    distance: Distance,
}

#[derive(Default, Debug, Clone)]
pub struct PopulateContext {
    queue: VecDeque<PopulateNode>,
}

#[derive(Debug, Clone)]
struct SearchNode {
    step: Step,
    distance: Distance,
}

#[derive(Debug, Clone)]
pub struct SearchContext {
    seen_set: SeenSet,
    queue: VecDeque<SearchNode>,
}

struct SearchState {
    distance_to_goal: Distance,
    closest_coord: Coord,
}

struct SearchInstance<'a, C: 'a + CanEnter> {
    distance_map: &'a DistanceMap,
    can_enter: &'a C,
    max_distance: Distance,
    search_state: SearchState,
}

struct Prune {
    current_distance: Distance,
    distance_to_goal: Distance,
}

impl<'a, C: CanEnter> SearchInstance<'a, C> {
    fn prune(&self, prune: Prune) -> bool {
        let remaining_distance = self.max_distance - prune.current_distance;
        if let Some(best_possible_distance_through_cell) = prune.distance_to_goal.checked_sub(remaining_distance) {
            if best_possible_distance_through_cell > self.search_state.distance_to_goal {
                return true;
            }
        }
        false
    }
    fn consider(&mut self, context: &mut SearchContext, step: Step, distance: Distance) {
        if let Some(Visit) = context.seen_set.try_visit_step(step, distance) {
            if self.can_enter.can_enter(step.to_coord) {
                if let Some(distance_to_goal) = self.distance_map.distance(step.to_coord) {
                    if distance <= self.max_distance {
                        if self.prune(Prune {
                            current_distance: distance,
                            distance_to_goal,
                        }) {
                            return;
                        }
                        if distance_to_goal < self.search_state.distance_to_goal {
                            self.search_state.closest_coord = step.to_coord;
                            self.search_state.distance_to_goal = distance_to_goal;
                        }
                        context.queue.push_back(SearchNode { step, distance });
                    }
                }
            }
        }
    }
}

#[cfg(feature = "serialize")]
impl Serialize for PopulateContext {
    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        ().serialize(s)
    }
}

#[cfg(feature = "serialize")]
impl<'a> Deserialize<'a> for PopulateContext {
    fn deserialize<D: serde::Deserializer<'a>>(d: D) -> Result<Self, D::Error> {
        let () = Deserialize::deserialize(d)?;
        Ok(Self::default())
    }
}

#[cfg(feature = "serialize")]
impl Serialize for SearchContext {
    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        self.seen_set.size().serialize(s)
    }
}

#[cfg(feature = "serialize")]
impl<'a> Deserialize<'a> for SearchContext {
    fn deserialize<D: serde::Deserializer<'a>>(d: D) -> Result<Self, D::Error> {
        Deserialize::deserialize(d).map(Self::new)
    }
}

impl DistanceMap {
    pub fn new(size: Size) -> Self {
        Self {
            count: 1,
            grid: Grid::new_fn(size, |_| Cell { count: 0, distance: 0 }),
        }
    }

    pub fn clear(&mut self) {
        self.count += 1;
    }

    pub fn size(&self) -> Size {
        self.grid.size()
    }

    pub fn direction_to_best_neighbour(&self, coord: Coord) -> Option<CardinalDirection> {
        let mut shortest_distance = std::u32::MAX;
        let mut direction_to_best_neighbour = None;
        if let Some(distance) = self.distance(coord) {
            shortest_distance = distance;
        }
        for direction in CardinalDirections {
            let neighbour_coord = coord + direction.coord();
            if let Some(distance) = self.distance(neighbour_coord) {
                if distance <= shortest_distance {
                    shortest_distance = distance;
                    direction_to_best_neighbour = Some(direction);
                }
            }
        }
        direction_to_best_neighbour
    }

    pub fn distance(&self, coord: Coord) -> Option<Distance> {
        if let Some(cell) = self.grid.get(coord) {
            if cell.count == self.count {
                return Some(cell.distance);
            }
        }
        None
    }
}

impl PopulateContext {
    pub fn clear(&mut self) {
        self.queue.clear();
    }

    pub fn add(&mut self, coord: Coord) {
        self.queue.push_front(PopulateNode { coord, distance: 0 });
    }

    pub fn populate_approach<C: CanEnter>(
        &mut self,
        can_enter: &C,
        max_distance: Distance,
        distance_map: &mut DistanceMap,
    ) {
        distance_map.clear();
        for node in self.queue.iter() {
            if let Some(cell) = distance_map.grid.get_mut(node.coord) {
                cell.count = distance_map.count;
                cell.distance = 0;
            }
        }
        if max_distance == 0 {
            self.queue.clear();
            return;
        }
        while let Some(PopulateNode { coord, distance }) = self.queue.pop_back() {
            debug_assert!(distance < max_distance);
            let neighbour_distance = distance + 1;
            for direction in CardinalDirections {
                let neighbour_coord = coord + direction.coord();
                if can_enter.can_enter(neighbour_coord) {
                    if let Some(cell) = distance_map.grid.get_mut(neighbour_coord) {
                        if cell.count != distance_map.count {
                            cell.count = distance_map.count;
                            cell.distance = neighbour_distance;
                            if neighbour_distance != max_distance {
                                self.queue.push_front(PopulateNode {
                                    coord: neighbour_coord,
                                    distance: neighbour_distance,
                                });
                            }
                        }
                    }
                }
            }
        }
    }

    pub fn populate_flee<C: CanEnter>(
        &mut self,
        can_enter: &C,
        max_distance: Distance,
        distance_map: &mut DistanceMap,
    ) {
        distance_map.count += 1;
        for node in self.queue.iter() {
            if let Some(cell) = distance_map.grid.get_mut(node.coord) {
                cell.count = distance_map.count;
                cell.distance = 0;
            }
        }
        if max_distance == 0 {
            self.queue.clear();
            return;
        }
        while let Some(PopulateNode { coord, distance }) = self.queue.pop_back() {
            debug_assert!(distance <= max_distance);
            if distance == max_distance {
                self.queue.push_back(PopulateNode { coord, distance });
                break;
            }
            let neighbour_distance = distance + 1;
            for direction in CardinalDirections {
                let neighbour_coord = coord + direction.coord();
                if can_enter.can_enter(neighbour_coord) {
                    if let Some(cell) = distance_map.grid.get_mut(neighbour_coord) {
                        if cell.count != distance_map.count {
                            cell.count = distance_map.count;
                            cell.distance = neighbour_distance;
                            self.queue.push_front(PopulateNode {
                                coord: neighbour_coord,
                                distance: neighbour_distance,
                            });
                        }
                    }
                }
            }
        }
        if self.queue.is_empty() {
            return;
        }
        // at this point we know that all the nodes in the queue have a distance of max_distance
        distance_map.count += 1;
        for node in self.queue.iter_mut() {
            debug_assert!(node.distance <= max_distance);
            node.distance = 0;
            if let Some(cell) = distance_map.grid.get_mut(node.coord) {
                cell.count = distance_map.count;
                cell.distance = 0;
            }
        }
        while let Some(PopulateNode { coord, distance }) = self.queue.pop_back() {
            let neighbour_distance = distance + 1;
            for direction in CardinalDirections {
                let neighbour_coord = coord + direction.coord();
                if let Some(cell) = distance_map.grid.get_mut(neighbour_coord) {
                    if cell.count == distance_map.count - 1 {
                        cell.count += 1;
                        cell.distance = neighbour_distance;
                        self.queue.push_front(PopulateNode {
                            coord: neighbour_coord,
                            distance: neighbour_distance,
                        });
                    }
                }
            }
        }
    }
}

impl SearchContext {
    pub fn new(size: Size) -> Self {
        Self {
            seen_set: SeenSet::new(size),
            queue: VecDeque::new(),
        }
    }

    fn search_core<C: CanEnter>(
        &mut self,
        can_enter: &C,
        start: Coord,
        max_distance: Distance,
        distance_map: &DistanceMap,
    ) -> Option<Coord> {
        let search_state = if let Some(distance_to_goal) = distance_map.distance(start) {
            SearchState {
                distance_to_goal,
                closest_coord: start,
            }
        } else {
            return None;
        };
        self.seen_set.init(start);
        self.queue.clear();
        let mut instance = SearchInstance {
            distance_map,
            can_enter,
            max_distance,
            search_state,
        };
        for &in_direction in &UNIT_COORDS {
            let step = Step {
                to_coord: start + in_direction.to_coord(),
                in_direction,
            };
            instance.consider(self, step, 1);
        }
        while let Some(SearchNode { step, distance }) = self.queue.pop_front() {
            if instance.prune(Prune {
                current_distance: distance,
                distance_to_goal: instance.distance_map.distance(step.to_coord).unwrap(),
            }) {
                continue;
            }
            let next_distance = distance + 1;
            instance.consider(self, step.forward(), next_distance);
            instance.consider(self, step.left(), next_distance);
            instance.consider(self, step.right(), next_distance);
        }
        Some(instance.search_state.closest_coord)
    }

    pub fn search_path<C: CanEnter>(
        &mut self,
        can_enter: &C,
        start: Coord,
        max_distance: Distance,
        distance_map: &DistanceMap,
        path: &mut Path,
    ) {
        if let Some(end) = self.search_core(can_enter, start, max_distance, distance_map) {
            self.seen_set.build_path_to(end, path);
        }
    }

    pub fn search_first<C: CanEnter>(
        &mut self,
        can_enter: &C,
        start: Coord,
        max_distance: Distance,
        distance_map: &DistanceMap,
    ) -> Option<CardinalDirection> {
        if let Some(end) = self.search_core(can_enter, start, max_distance, distance_map) {
            self.seen_set.first_direction_towards(end)
        } else {
            None
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[derive(Clone)]
    enum Cell {
        Solid,
        Traversable,
    }

    struct World {
        grid: Grid<Cell>,
    }

    impl CanEnter for World {
        fn can_enter(&self, coord: Coord) -> bool {
            if let Some(cell) = self.grid.get(coord) {
                match cell {
                    Cell::Solid => false,
                    Cell::Traversable => true,
                }
            } else {
                false
            }
        }
    }

    struct Test {
        world: World,
        goals: Vec<Coord>,
    }

    impl Test {
        fn from_str_slice(str_slice: &[&str]) -> Self {
            let width = str_slice[0].len() as u32;
            let height = str_slice.len() as u32;
            let size = Size::new(width, height);
            let mut grid = Grid::new_clone(size, Cell::Solid);
            let mut goals = Vec::new();
            for (y, line) in str_slice.iter().enumerate() {
                for (x, ch) in line.chars().enumerate() {
                    let coord = Coord::new(x as i32, y as i32);
                    let cell = match ch {
                        '.' => Cell::Traversable,
                        '#' => Cell::Solid,
                        '@' => {
                            goals.push(coord);
                            Cell::Traversable
                        }
                        _ => panic!(),
                    };
                    *grid.get_checked_mut(coord) = cell;
                }
            }
            Self {
                world: World { grid },
                goals,
            }
        }
    }

    const GRID_A: &[&str] = &[
        "..........",
        "..........",
        "..........",
        "..........",
        "..........",
        "..........",
        "####.#####",
        "..........",
        ".@........",
        "..........",
    ];

    #[test]
    fn grid_a() {
        use CardinalDirection::*;
        let mut path = Path::default();
        let Test { world, goals } = Test::from_str_slice(GRID_A);
        let mut populate_context = PopulateContext::default();
        let mut distance_map = DistanceMap::new(world.grid.size());
        let mut search_context = SearchContext::new(distance_map.size());
        for &coord in &goals {
            populate_context.add(coord);
        }
        populate_context.populate_approach(&world, 7, &mut distance_map);
        assert_eq!(distance_map.distance(Coord::new(4, 6)), Some(5));
        assert_eq!(distance_map.distance(Coord::new(4, 5)), Some(6));
        assert_eq!(distance_map.distance(Coord::new(3, 5)), Some(7));
        assert_eq!(distance_map.distance(Coord::new(5, 5)), Some(7));
        assert_eq!(distance_map.distance(Coord::new(4, 4)), Some(7));
        assert_eq!(distance_map.distance(Coord::new(4, 3)), None);
        assert_eq!(
            distance_map.direction_to_best_neighbour(Coord::new(4, 6)),
            Some(CardinalDirection::South)
        );
        search_context.search_path(&world, Coord::new(7, 7), 100, &distance_map, &mut path);
        let directions = path.iter().map(|n| n.in_direction).collect::<Vec<_>>();
        assert_eq!(&directions, &[West, West, West, West, West, West, South]);
        assert_eq!(distance_map.direction_to_best_neighbour(Coord::new(1, 8)), None,);
        for &coord in &goals {
            populate_context.add(coord);
        }
        populate_context.populate_flee(&world, 10, &mut distance_map);
        assert_eq!(distance_map.distance(Coord::new(4, 6)), Some(5));
        assert_eq!(distance_map.distance(Coord::new(9, 7)), Some(11));
        assert_eq!(distance_map.distance(Coord::new(1, 8)), Some(10));
        assert_eq!(
            distance_map.direction_to_best_neighbour(Coord::new(1, 7)),
            Some(CardinalDirection::East)
        );
        search_context.search_path(&world, Coord::new(7, 7), 5, &distance_map, &mut path);
        let directions = path.iter().map(|n| n.in_direction).collect::<Vec<_>>();
        assert_eq!(&directions, &[West, West, West, North, North]);
    }

    const GRID_B: &[&str] = &[
        "..........",
        "..........",
        "..........",
        "..........",
        "..........",
        "..........",
        "..........",
        "..........",
        "..........",
        "..........",
    ];

    #[test]
    fn grid_b() {
        let Test { world, .. } = Test::from_str_slice(GRID_B);
        let mut populate_context = PopulateContext::default();
        let mut distance_map = DistanceMap::new(world.grid.size());
        let mut search_context = SearchContext::new(distance_map.size());
        populate_context.populate_approach(&world, 7, &mut distance_map);
        assert_eq!(distance_map.distance(Coord::new(4, 5)), None);
        populate_context.populate_flee(&world, 7, &mut distance_map);
        assert_eq!(distance_map.distance(Coord::new(4, 5)), None);
        let mut path = Path::default();
        search_context.search_path(&world, Coord::new(7, 7), 5, &distance_map, &mut path);
        let directions = path.iter().map(|n| n.in_direction).collect::<Vec<_>>();
        assert_eq!(&directions, &[]);
    }

    const GRID_C: &[&str] = &[
        "..........",
        "..........",
        "..........",
        "..........",
        "..........",
        "..........",
        "####.#####",
        ".@.......@",
        ".@........",
        "..........",
    ];

    #[test]
    fn grid_c() {
        let Test { world, goals } = Test::from_str_slice(GRID_C);
        let mut populate_context = PopulateContext::default();
        let mut distance_map = DistanceMap::new(world.grid.size());
        for &coord in &goals {
            populate_context.add(coord);
        }
        populate_context.populate_approach(&world, 7, &mut distance_map);
        assert_eq!(distance_map.distance(Coord::new(4, 6)), Some(4));
        assert_eq!(
            distance_map.direction_to_best_neighbour(Coord::new(4, 6)),
            Some(CardinalDirection::South)
        );
        for &coord in &goals {
            populate_context.add(coord);
        }
        populate_context.populate_flee(&world, 10, &mut distance_map);
        assert_eq!(distance_map.distance(Coord::new(4, 6)), Some(6));
        assert_eq!(
            distance_map.direction_to_best_neighbour(Coord::new(1, 7)),
            Some(CardinalDirection::East)
        );
        assert_eq!(
            distance_map.direction_to_best_neighbour(Coord::new(6, 7)),
            Some(CardinalDirection::West)
        );
    }
}