sark_pathfinding 0.4.0

A simple implementation of the astar pathfinding algorthim from red blob games https://www.redblobgames.com/pathfinding/a-star/implementation.html and 'Dijkstra Maps' from https://www.roguebasin.com/index.php/Dijkstra_Maps_Visualized
Documentation
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
//! A simple implementation of a "Dijkstra Map" as described in https://www.roguebasin.com/index.php/Dijkstra_Maps_Visualized

use crate::{min_heap::MinHeap, PathMap};
use ahash::{HashSet, HashSetExt};
use arrayvec::{ArrayVec, IntoIter};
use glam::{IVec2, UVec2};
use sark_grids::{BitGrid, FloatGrid, GridPoint, GridSize, SizedGrid};

const INITIAL_VALUE: f32 = 1000.0;
const EXIT_CAP: usize = 8;

/// A simple implementation of a "Dijkstra Map" as described in [Dijsktra Maps Visualized]
///
/// A [PathMap] is used to define the obstacles and movement costs for the map.
///
/// Various 'goals' can be defined with specific values. Once the map is
/// recalculated using a [PathMap] a path to the nearest goal can be
/// followed by querying for the next lowest value exit from any given position.
///
/// # Example
///
/// ```
/// use sark_pathfinding::*;
/// let mut pathmap = PathMap2d::new([20,20]);
/// pathmap.add_obstacle([5,5]);
/// pathmap.add_obstacle([5,6]);
/// pathmap.add_obstacle([5,7]);
///
/// // Ensure the dijsktra map is defined with  the same size as your pathmap.
/// let mut goals = DijkstraMap::new([20,20]);
/// goals.add_goal([10,10], 0.0);
/// goals.add_goal([15,15], 5.0);
/// goals.recalculate(&pathmap);
/// let next_step = goals.next_lowest([13,13], &pathmap).unwrap();
/// // Lower value goals are considered 'closer'.
/// assert_eq!([12,12], next_step.to_array());
/// ```
/// [Dijsktra Maps Visualized]: https://www.roguebasin.com/index.php/Dijkstra_Maps_Visualized
#[derive(Debug, Default, Clone)]
pub struct DijkstraMap {
    values: FloatGrid,
    goals: HashSet<IVec2>,
    /// Obstacles populated during map recalculation. This is only used
    /// when iterating over map values on an already calculated map to skip
    /// 'obstacles'.
    obstacles: BitGrid,
    frontier: MinHeap,
    size: UVec2,
    initial_value: f32,
}

impl DijkstraMap {
    pub fn new(size: impl GridSize) -> Self {
        let mut values = FloatGrid::new(size.to_uvec2());
        values.set_all(INITIAL_VALUE);
        Self {
            values,
            goals: HashSet::new(),
            frontier: MinHeap::with_capacity(size.tile_count()),
            obstacles: BitGrid::new(size.to_uvec2()),
            size: size.to_uvec2(),
            initial_value: INITIAL_VALUE,
        }
    }

    pub fn with_initial_value(mut self, initial_value: f32) -> Self {
        self.initial_value = initial_value;
        self.values.set_all(initial_value);
        self
    }

    pub fn from_string(s: impl AsRef<str>) -> Option<Self> {
        let width = s.as_ref().lines().map(|l| l.len()).max()?;
        let height = s.as_ref().lines().filter(|l| !l.is_empty()).count();
        if width == 0 || height == 0 {
            return None;
        }
        let size = UVec2::new(width as u32, height as u32);
        let mut values = FloatGrid::new(size);
        values.set_all(INITIAL_VALUE);
        let mut goals = HashSet::new();
        let mut obstacles = BitGrid::new(size);
        for (y, line) in s
            .as_ref()
            .lines()
            .filter(|l| !l.is_empty())
            .rev()
            .enumerate()
        {
            for (x, c) in line.chars().enumerate() {
                match c {
                    '#' => {
                        obstacles.set([x, y], true);
                    }
                    _ => {
                        // Attempt to convert the char into a goal value
                        if let Some(v) = c.to_digit(10).map(|d| d as f32) {
                            values.set_value([x, y], v);
                            goals.insert(IVec2::from([x as i32, y as i32]));
                        }
                    }
                }
            }
        }

        Some(Self {
            values,
            goals,
            obstacles,
            frontier: MinHeap::with_capacity(size.tile_count()),
            size,
            initial_value: INITIAL_VALUE,
        })
    }

    /// Add a 'goal' to a map position. `value` determines the desirability of
    /// the goal during pathfinding, where lower value goals are seen as 'closer' than
    /// higher value goals.
    ///
    /// If you add to an existing goal, the value will be added to the previously
    /// set value.
    pub fn add_goal(&mut self, xy: impl GridPoint, value: f32) {
        let xy = xy.to_ivec2();
        if self.goals.contains(&xy) {
            let v = self.values.value(xy);
            self.values.set_value(xy, v + value);
        } else {
            self.values[xy] = value;
            self.goals.insert(xy);
        }
    }

    /// Set the goal value for a position.
    ///
    /// Lower value goals are considered 'closer' during pathfinding.
    pub fn set_goal(&mut self, xy: impl GridPoint, value: f32) {
        self.values[xy] = value;
        self.goals.insert(xy.to_ivec2());
    }

    /// Recalculate the map based on the given pathing.
    ///
    /// This will not clear any currently set map values and will overwrite previous
    /// values. To recalculate a clean map based only on the currently set goals,
    /// call [DijkstraMap::clear_values] to clear any previously set non-goal tiles.
    pub fn recalculate(&mut self, pathing: &impl PathMap) {
        self.obstacles.set_all(true);
        self.frontier.clear();

        for i in 0..self.size.tile_count() {
            let xy = self.transform_itl(i);
            if !self.goals.contains(&xy) && pathing.is_obstacle(xy) {
                continue;
            }
            self.frontier.push(xy, self.values[i] as i32);
        }

        while let Some(curr) = self.frontier.pop() {
            for next in pathing.exits(curr) {
                let new_cost = self.values.value(curr) + pathing.cost(curr, next) as f32;
                self.obstacles.set(next, false);
                if new_cost < self.values.value(next) {
                    self.values.set_value(next, new_cost);
                    self.frontier.push(next, new_cost as i32);
                }
            }
        }
    }

    /// Remove a goal. This will not affect any previously set value
    /// for that goal's tile.
    pub fn remove_goal(&mut self, xy: impl GridPoint) {
        self.goals.remove(&xy.to_ivec2());
    }

    /// An iterator over any goals set for the map, and the value for each goal.
    pub fn goals(&self) -> impl Iterator<Item = (IVec2, f32)> + '_ {
        self.goals.iter().map(|p| (*p, self.values.value(*p)))
    }

    /// Resets the value of all non-goal tiles.
    pub fn clear_values(&mut self) {
        for (p, v) in self.values.iter_grid_points().zip(self.values.values_mut()) {
            if !self.goals.contains(&p) {
                *v = self.initial_value;
            }
        }
    }

    /// Clears all goals and values from the map, resetting all tiles values to 0.
    pub fn clear_all(&mut self) {
        self.clear_values();
        self.obstacles.set_all(true);
        self.goals.clear();
    }

    /// Apply a mathematical operation to every value in the map.
    pub fn apply_operation(&mut self, operation: impl Fn(f32) -> f32) {
        self.values.apply_operation(operation);
    }

    /// A reference to the [DijkstraMap]'s underlying [FloatGrid].
    pub fn float_grid(&self) -> &FloatGrid {
        &self.values
    }

    /// A mutable reference to the [DijkstraMap]'s underlying [FloatGrid].
    pub fn float_grid_mut(&mut self) -> &mut FloatGrid {
        &mut self.values
    }

    /// Retrieve an iterator over the valid exits from a given position on the [DijkstraMap].
    ///
    /// The [DijkstraMap] does not store pathing information so a [PathMap] must be provided.
    ///
    /// The exits are sorted by 'value', so the first exit should be considered
    /// the most 'valuable'. The actual values can be retrieved via [DijkstraMap::exit_values].
    pub fn exits(&self, xy: impl GridPoint, pathing: &impl PathMap) -> impl Iterator<Item = IVec2> {
        self.exit_values(xy, pathing).map(|pv| pv.0)
    }

    /// Retrieve an iterator over the valid exits from a given position on the [DijkstraMap].
    ///
    /// The [DijkstraMap] does not store pathing information so a [PathMap] must be provided.
    ///
    /// Returns an iterator of 2 element tuples, where each tuple contains a position
    /// and it's corresponding 'value' in the [DijkstraMap]. The exits
    /// are sorted by 'most valuable' first before being returned.
    pub fn exit_values(
        &self,
        xy: impl GridPoint,
        pathing: &impl PathMap,
    ) -> IntoIter<(IVec2, i32), EXIT_CAP> {
        let xy = xy.to_ivec2();
        let mut v = ArrayVec::new();
        for next in pathing.exits(xy) {
            let Some(i) = next.get_index(self.size()) else {
                continue;
            };

            v.push((next, self.values[i] as i32));
        }
        v.sort_unstable_by(|a, b| a.1.cmp(&b.1));

        v.into_iter()
    }

    /// Returns the lowest value exit from a position if there is one.
    ///
    /// The [DijkstraMap] does not store pathing information so a [PathMap] must be provided.
    pub fn next_lowest(&self, xy: impl GridPoint, pathing: &impl PathMap) -> Option<IVec2> {
        let xy = xy.to_ivec2();
        let mut v: ArrayVec<(IVec2, i32), EXIT_CAP> = ArrayVec::new();
        for next in pathing.exits(xy) {
            let Some(i) = next.get_index(self.size()) else {
                continue;
            };

            v.push((next, self.values[i] as i32));
        }
        v.sort_unstable_by(|a, b| a.1.cmp(&b.1));

        v.into_iter().next().map(|pv| pv.0)
    }

    /// Returns the highest value exit from a position if there is one.
    ///
    /// The [DijkstraMap] does not store pathing information so a [PathMap] must be provided.
    pub fn next_highest(&self, xy: impl GridPoint, pathing: &impl PathMap) -> Option<IVec2> {
        let xy = xy.to_ivec2();
        let mut v: ArrayVec<(IVec2, i32), EXIT_CAP> = ArrayVec::new();
        for next in pathing.exits(xy) {
            let Some(i) = next.get_index(self.size()) else {
                continue;
            };

            v.push((next, self.values[i] as i32));
        }
        v.sort_unstable_by(|a, b| b.1.cmp(&a.1));

        v.into_iter().next().map(|pv| pv.0)
    }

    pub fn values(&self) -> &[f32] {
        self.values.values()
    }

    /// Iterate over all the values in the map, skipping any obstacles.
    pub fn iter_xy(&self) -> impl Iterator<Item = (IVec2, f32)> + '_ {
        self.values
            .iter_grid_points()
            .zip(self.values.values().iter().copied())
            .filter(|(p, _)| !self.obstacles.get(*p))
    }

    /// Iterate over all the values in the map, skipping any obstacles.
    pub fn iter_xy_mut(&mut self) -> impl Iterator<Item = (IVec2, &mut f32)> + '_ {
        self.values
            .iter_grid_points()
            .zip(self.values.values_mut().iter_mut())
            .filter(|(p, _)| !self.obstacles.get(*p))
    }

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

    pub fn print_grid_values(&self) {
        for y in (0..self.size.y).rev() {
            for x in 0..self.size.x {
                let p = IVec2::new(x as i32, y as i32);
                let v = self.values.value(p);
                if v.abs() >= 999.0 {
                    print!("  ###");
                } else {
                    print!("{:5.1}", v);
                }
            }

            println!();
        }
        println!();
    }
}

impl SizedGrid for DijkstraMap {
    fn size(&self) -> UVec2 {
        self.values.size()
    }
}

#[cfg(test)]
mod tests {
    use glam::UVec2;

    use super::DijkstraMap;
    use crate::PathMap2d;

    #[test]
    #[ignore]
    fn open() {
        let size = UVec2::splat(10);
        let mut map = DijkstraMap::new(size);
        let pathing = PathMap2d::new(size);
        map.add_goal([4, 4], 1.0);
        map.recalculate(&pathing);
        map.print_grid_values();
    }

    #[test]
    #[ignore]
    fn obstacles() {
        let size = UVec2::splat(7);
        let mut map = DijkstraMap::new(size);
        let mut pathing = PathMap2d::new(size);
        let goal = [3, 3];
        pathing.add_obstacle([3, 4]);
        pathing.add_obstacle([4, 3]);
        pathing.add_obstacle([3, 2]);
        map.add_goal(goal, 1.0);
        map.recalculate(&pathing);
        map.print_grid_values();
    }

    #[test]
    #[ignore]
    fn goals() {
        let size = UVec2::splat(9);
        let mut map = DijkstraMap::new(size);
        let pathing = PathMap2d::new(size);
        map.add_goal([2, 2], 1.0);
        map.add_goal([8, 8], 3.0);
        map.recalculate(&pathing);
        map.print_grid_values();
    }

    #[test]
    #[ignore]
    fn flip() {
        let size = UVec2::splat(9);
        let mut map = DijkstraMap::new(size);
        let pathing = PathMap2d::new(size);
        map.add_goal([2, 2], 5.0);
        map.add_goal([8, 8], -3.0);
        map.recalculate(&pathing);
        map.print_grid_values();
        map.apply_operation(|f| f * -1.2);
        map.print_grid_values();
        map.recalculate(&pathing);
        map.print_grid_values();
    }

    #[test]
    #[ignore]
    fn string_map() {
        let map_string = "
###############
#             #
#             ###########
#                       #
#      2      #######   #
#             #     #   #
#             #     #####
###############
        ";
        let pathing = PathMap2d::from_string(map_string, '#').unwrap();
        let mut map = DijkstraMap::from_string(map_string).unwrap();
        map.recalculate(&pathing);
        map.apply_operation(|f| f * -1.2);
        map.recalculate(&pathing);
        map.print_grid_values();
    }
}