bevy_northstar 0.3.2

A Bevy plugin for Hierarchical Pathfinding
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
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
//! This module defines pathfinding functions which can be called directly.

use bevy::{
    ecs::entity::Entity,
    log,
    math::{IVec3, UVec3},
    platform::collections::{HashMap, HashSet},
};
use ndarray::ArrayView3;

use crate::{
    astar::{astar_graph, astar_grid},
    chunk::Chunk,
    dijkstra::dijkstra_grid,
    grid::Grid,
    nav::NavCell,
    node::Node,
    path::Path,
    prelude::Neighborhood,
    raycast::{bresenham_path, bresenham_path_filtered},
};

/// AStar pathfinding
///
/// This function is provided if you want to supply your own grid.
/// If you're using the built in [`Grid`] you can use the pathfinding helper functions
/// provided in the [`Grid`] struct.
///
/// # Arguments
/// * `neighborhood` - The [`Neighborhood`] to use for the pathfinding.
/// * `grid` - The [`ArrayView3`] of [`NavCell`]s to use for the pathfinding.
/// * `start` - The starting position.
/// * `goal` - The goal position.
/// * `blocking` - A hashmap of blocked positions for dynamic obstacles.
/// * `partial` - If true, the pathfinding will return a partial path if the goal is blocked.
#[inline(always)]
// This has to be moved internally since the base A* and Djikstra algorithms use precomputed neighbors now.
pub(crate) fn pathfind_astar<N: Neighborhood>(
    neighborhood: &N,
    grid: &ArrayView3<NavCell>,
    start: UVec3,
    goal: UVec3,
    blocking: &HashMap<UVec3, Entity>,
    partial: bool,
) -> Option<Path> {
    // Ensure the goal is within bounds of the grid
    let shape = grid.shape();
    if start.x as usize >= shape[0] || start.y as usize >= shape[1] || start.z as usize >= shape[2]
    {
        log::warn!("Start is out of bounds: {:?}", start);
        return None;
    }

    if goal.x as usize >= shape[0] || goal.y as usize >= shape[1] || goal.z as usize >= shape[2] {
        log::warn!("Goal is out of bounds: {:?}", goal);
        return None;
    }

    // If the goal is impassibe and partial isn't set, return none
    if grid[[start.x as usize, start.y as usize, start.z as usize]].is_impassable()
        || grid[[goal.x as usize, goal.y as usize, goal.z as usize]].is_impassable() && !partial
    {
        return None;
    }

    // if goal is in the blocking map, return None
    if blocking.contains_key(&goal) && !partial {
        //log::error!("Goal is in the blocking map");
        return None;
    }

    let path = astar_grid(neighborhood, grid, start, goal, 1024, partial, blocking);

    if let Some(mut path) = path {
        path.path.pop_front();
        Some(path)
    } else {
        None
    }
}

/// HPA* pathfinding.
// Keeping this internal for now since Grid has it's own helper function to call this
// and [`Grid`] is required for it.
#[inline(always)]
pub(crate) fn pathfind<N: Neighborhood>(
    grid: &Grid<N>,
    start: UVec3,
    goal: UVec3,
    blocking: &HashMap<UVec3, Entity>,
    partial: bool,
    refined: bool,
) -> Option<Path> {
    if !grid.in_bounds(start) {
        log::warn!("Start is out of bounds: {:?}", start);
        return None;
    }

    // Make sure the goal is in grid bounds
    if !grid.in_bounds(goal) {
        log::warn!("Goal is out of bounds: {:?}", goal);
        return None;
    }

    // If the goal is impassable and partial isn't set, return none
    if grid.view()[[start.x as usize, start.y as usize, start.z as usize]].is_impassable()
        || grid.view()[[goal.x as usize, goal.y as usize, goal.z as usize]].is_impassable()
            && !partial
    {
        return None;
    }

    let start_chunk = grid.chunk_at_position(start)?;
    let goal_chunk = grid.chunk_at_position(goal)?;

    // If the start and goal are in the same chunk, use AStar directly
    if start_chunk == goal_chunk {
        let path = astar_grid(
            &grid.neighborhood,
            &grid.view(),
            start,
            goal,
            100,
            partial,
            blocking,
        );

        if let Some(mut path) = path {
            path.path.pop_front();
            return Some(path);
        } else {
            return None;
        }
    }

    // Find viable nodes in the start and goal chunks
    let (start_nodes, start_paths) =
        filter_and_rank_chunk_nodes(grid, start_chunk, start, goal, blocking)?;
    let (goal_nodes, goal_paths) =
        filter_and_rank_chunk_nodes(grid, goal_chunk, goal, start, blocking)?;

    let mut path: Vec<UVec3> = Vec::new();
    let mut cost = 0;

    for start_node in &start_nodes {
        for goal_node in goal_nodes.clone() {
            let node_path = astar_graph(
                &grid.neighborhood,
                grid.graph(),
                start_node.pos,
                goal_node.pos,
                100,
            );

            if let Some(mut node_path) = node_path {
                let start_keys: HashSet<_> = start_paths.keys().copied().collect();
                let goal_keys: HashSet<_> = goal_paths.keys().copied().collect();

                trim_path(
                    &mut node_path,
                    &start_keys,
                    &goal_keys,
                    start_chunk,
                    goal_chunk,
                );

                let start_pos = node_path.path.front().unwrap();
                let goal_pos = node_path.path.back().unwrap();

                // Add start_path to the node_path
                let start_path = start_paths.get(&(start_pos - start_chunk.min())).unwrap();
                path.extend(start_path.path().iter().map(|pos| *pos + start_chunk.min()));
                cost += start_path.cost();

                // Add node_path paths to path
                for (node, next_node) in
                    node_path.path().iter().zip(node_path.path().iter().skip(1))
                {
                    // Get the cached edge path between node and next node
                    let cached_path = grid.graph().node_at(*node).unwrap().edges[next_node].clone();
                    path.extend(cached_path.path().iter().skip(1));
                    cost += cached_path.cost();
                }

                // Add end path to path
                let end_path = goal_paths.get(&(goal_pos - goal_chunk.min())).unwrap();
                path.extend(
                    end_path
                        .path()
                        .iter()
                        .rev()
                        .map(|pos| *pos + goal_chunk.min()),
                );
                cost += end_path.cost();

                if path.is_empty() {
                    return None;
                }

                // On some occassions extending the goal path can add in a duplicate goal position at the end.
                // It's cheaper/cleaner to just clean up after it.
                if path.len() >= 2 && path[path.len() - 1] == path[path.len() - 2] {
                    path.pop();
                }

                // Same with the start
                if path.len() >= 2 && path[0] == path[1] {
                    log::warn!("Start contains duplicate nodes: {:?}", path);
                }

                if !refined {
                    // If we're not refining, return the path as is
                    let mut path = Path::new(path, cost);
                    path.graph_path = node_path.path;
                    return Some(path);
                }

                let mut refined_path = optimize_path(
                    &grid.neighborhood,
                    &grid.view(),
                    &Path::from_slice(&path, cost),
                );

                // remove the starting position from the refined path
                refined_path.path.pop_front();

                // add the graph path to the refined path
                refined_path.graph_path = node_path.path;

                return Some(refined_path);
            }
        }
    }

    None
}

// Some times the Graph A* will return a path that has valid but redundant nodes at the start and end
// of the path. Leading to awkward paths where the agent appears to veers off before heading to the goal.
// This trims the path to ensure that only one entrance and exit node is used for the start and goal chunks.
pub(crate) fn trim_path(
    path: &mut Path,
    start_keys: &HashSet<UVec3>, // local positions in start_paths
    goal_keys: &HashSet<UVec3>,  // local positions in goal_paths
    start_chunk: &Chunk,
    goal_chunk: &Chunk,
) {
    // === Trim start ===
    if let Some(i) = path
        .path
        .iter()
        .position(|pos| start_keys.contains(&(*pos - start_chunk.min())))
    {
        // Remove everything before this index
        for _ in 0..i {
            path.path.pop_front();
        }
    }

    // === Trim end ===
    if let Some(i) = path
        .path
        .iter()
        .rposition(|pos| goal_keys.contains(&(*pos - goal_chunk.min())))
    {
        // Remove everything after this index
        let len = path.path.len();
        for _ in (i + 1)..len {
            path.path.pop_back();
        }
    }

    assert!(
        !path.path.is_empty(),
        "BUG: trim_path() removed all nodes — this should never happen"
    );
}

/*#[inline(always)]
pub(crate) fn trim_path(
    path: &mut Path,
    starts: Vec<UVec3>,
    goals: Vec<UVec3>,
) {
    // Trim out reduntant nodes in the viable starts
    if let Some(last_start_node) = path
        .path
        .iter()
        .rev()
        .find(|&&pos| starts.contains(&pos))
        .cloned()
    {
        while let Some(first) = path.path.front() {
            // Trim only if it's a different node in the start chunk
            if starts.contains(first) && *first != last_start_node {
                path.path.pop_front();
            } else {
                break;
            }
        }
    }

    // Trim out redundant nodes in the viable goals
    if let Some(first_goal_node) = path
        .path
        .iter()
        .find(|&&pos| goals.contains(&pos))
        .cloned()
    {
        while let Some(last) = path.path.back() {
            if goals.contains(last) && *last != first_goal_node {
                path.path.pop_back();
            } else {
                break;
            }
        }
    }

    assert!(
        !path.path.is_empty(),
        "BUG: trim_path() removed all nodes — this should never happen"
    );
}*/

/// Optimize a path by using line of sight checks to skip waypoints.
///
/// This is used to optimize paths generated by the HPA* algorithms.
/// [`Grid::pathfind`] uses this internally so this is only needed if you want to
/// optimize a path that was generated by a different method.
///
/// # Arguments
///
/// * `neighborhood` - The [`Neighborhood`] to use for the pathfinding.
/// * `grid` - The [`ArrayView3`] of the grid.
/// * `path` - The [`Path`] to optimize.
/// * `ordinal` - If true, use ordinal movement. If false, use cardinal movement.
///
#[inline(always)]
pub(crate) fn optimize_path<N: Neighborhood>(
    neighborhood: &N,
    grid: &ArrayView3<NavCell>,
    path: &Path,
) -> Path {
    if path.is_empty() {
        return path.clone();
    }

    let filtered = !neighborhood.filters().is_empty();

    let mut refined_path = Vec::with_capacity(path.len());
    let mut i = 0;

    let mut last_dir: Option<IVec3> = None;

    refined_path.push(path.path[i]); // Always keep the first node

    while i < path.len() {
        let mut shortcut_taken = false;

        for farthest in (i + 1..path.len()).rev() {
            let candidate = path.path[farthest];
            let dir = (candidate.as_ivec3() - path.path[i].as_ivec3()).signum();

            // Reject if direction changes drastically
            if let Some(prev_dir) = last_dir {
                if dir != prev_dir && dir.dot(prev_dir) < 0 {
                    continue; // Skip this candidate
                }
            }

            let maybe_shortcut = if filtered {
                bresenham_path_filtered(grid, path.path[i], candidate, neighborhood.is_ordinal())
            } else {
                bresenham_path(grid, path.path[i], candidate, neighborhood.is_ordinal())
            };

            if let Some(shortcut) = maybe_shortcut {
                refined_path.extend(shortcut.into_iter().skip(1));
                i = farthest;
                shortcut_taken = true;
                last_dir = Some(dir);
                break;
            }
        }

        if !shortcut_taken {
            i += 1;
            if i < path.len() {
                let dir = if let Some(prev) = refined_path.last() {
                    (path.path[i].as_ivec3() - prev.as_ivec3()).signum()
                } else {
                    IVec3::ZERO
                };
                refined_path.push(path.path[i]);
                last_dir = Some(dir);
            }
        }
    }

    // Recompute cost of new path
    let cost = refined_path
        .iter()
        .map(|pos| grid[[pos.x as usize, pos.y as usize, pos.z as usize]].cost)
        .sum();

    let mut path = Path::new(refined_path.clone(), cost);
    path.graph_path = refined_path.into();
    path
}

/*pub(crate) fn optimize_path_old<N: Neighborhood>(
    neighborhood: &N,
    grid: &ArrayView3<NavCell>,
    path: &Path,
) -> Path {
    if path.is_empty() {
        return path.clone();
    }

    let filtered = !neighborhood.filters().is_empty();

    let goal = *path.path.back().unwrap();
    let mut refined_path = Vec::with_capacity(path.len());
    let mut i = 0;

    refined_path.push(path.path[i]); // Always keep the first node

    while i < path.len() {
        if !filtered {
            if let Some(direct_path) = bresenham_path(grid, path.path[i], goal, neighborhood.is_ordinal()) {
                // If we can reach the goal directly, add it and break
                refined_path.extend(direct_path.into_iter().skip(1));
                break;
            }
        } else {
            if let Some(direct_path) = bresenham_path_filtered(grid, path.path[i], goal, neighborhood.is_ordinal()) {
                // If we can reach the goal directly, add it and break
                refined_path.extend(direct_path.into_iter().skip(1));
                break;
            }
        }

        // Try to find the farthest reachable waypoint from i (greedy)
        let mut shortcut_taken = false;
        for farthest in (i + 1..path.len()).rev() {
            if !filtered {
                if let Some(shortcut) = bresenham_path(
                    grid,
                    path.path[i],
                    path.path[farthest],
                    neighborhood.is_ordinal(),
                ) {
                    refined_path.extend(shortcut.into_iter().skip(1));
                    i = farthest;
                    shortcut_taken = true;
                    break;
                }
            } else if let Some(shortcut) = bresenham_path_filtered(
                grid,
                path.path[i],
                path.path[farthest],
                neighborhood.is_ordinal(),
            ) {
                refined_path.extend(shortcut.into_iter().skip(1));
                i = farthest;
                shortcut_taken = true;
                break;
            }
        }

        if !shortcut_taken {
            // No shortcut found — advance by one
            i += 1;
            if i < path.len() {
                refined_path.push(path.path[i]);
            }
        }
    }

    // Recompute cost of new path
    let cost = refined_path
        .iter()
        .map(|pos| grid[[pos.x as usize, pos.y as usize, pos.z as usize]].cost)
        .sum();

    let mut path = Path::new(refined_path.clone(), cost);
    path.graph_path = refined_path.into();
    path
}*/

// Filters and ranks nodes within a chunk based on reachability and proximity.
#[inline(always)]
fn filter_and_rank_chunk_nodes<'a, N: Neighborhood>(
    grid: &'a Grid<N>,
    chunk: &Chunk,
    source: UVec3,
    target: UVec3,
    blocking: &HashMap<UVec3, Entity>,
) -> Option<(Vec<&'a Node>, HashMap<UVec3, Path>)> {
    let nodes = grid.graph().nodes_in_chunk(chunk);

    // Adjust the blocking map to the local chunk coordinates
    let adjusted_blocking = blocking
        .iter()
        .map(|(pos, entity)| (chunk.to_local(pos), *entity))
        .collect::<HashMap<_, _>>();

    // Get paths from source to all nodes in this chunk
    let paths = dijkstra_grid(
        &grid.chunk_view(chunk),
        source - chunk.min(),
        &nodes
            .iter()
            .map(|node| node.pos - chunk.min())
            .collect::<Vec<_>>(),
        false,
        100,
        &adjusted_blocking,
    );

    let filtered_nodes = nodes
        .iter()
        .filter(|node| paths.contains_key(&(node.pos - chunk.min())))
        .collect::<Vec<_>>();

    if filtered_nodes.is_empty() {
        return None;
    }

    let mut ranked_nodes = filtered_nodes
        .iter()
        .map(|node| {
            let d_start = manhattan_distance(node.pos, source);
            let d_goal = manhattan_distance(node.pos, target);
            (*node, d_start + d_goal)
        })
        .collect::<Vec<_>>();

    ranked_nodes.sort_by_key(|(_, dist)| *dist);
    Some((ranked_nodes.into_iter().map(|(n, _)| *n).collect(), paths))
}

#[inline(always)]
fn manhattan_distance(a: UVec3, b: UVec3) -> i32 {
    (a.x as i32 - b.x as i32).abs()
        + (a.y as i32 - b.y as i32).abs()
        + (a.z as i32 - b.z as i32).abs()
}

/// Recursively reroutes a path by astar pathing to further chunks until a path can be found.
///
/// Useful if local collision avoidance is failing.
///
/// If you're using the plugin pathing systems, you shouldn't need to call this directly.
///
/// # Arguments
#[inline(always)]
pub(crate) fn reroute_path<N: Neighborhood>(
    grid: &Grid<N>,
    path: &Path,
    start: UVec3,
    goal: UVec3,
    blocking: &HashMap<UVec3, Entity>,
    refined: bool,
) -> Option<Path> {
    // When the starting chunks entrances are all blocked, this will try astar path to the NEXT chunk in the graph path
    // recursively until it can find a path out.
    // If it can't find a path out, it will return None.

    if !grid.in_bounds(start) {
        log::warn!("Start is out of bounds: {:?}", start);
        return None;
    }

    if !grid.in_bounds(goal) {
        log::warn!("Goal is out of bounds: {:?}", goal);
        return None;
    }

    if path.graph_path.is_empty() {
        // Our only option here is to astar path to the goal
        return pathfind_astar(
            &grid.neighborhood,
            &grid.view(),
            start,
            goal,
            blocking,
            false,
        );
    }

    let new_path = path.graph_path.iter().find_map(|pos| {
        let new_path = pathfind_astar(
            &grid.neighborhood,
            &grid.view(),
            start,
            *pos,
            blocking,
            false,
        );
        if new_path.is_some() && !new_path.as_ref().unwrap().is_empty() {
            new_path
        } else {
            None
        }
    });

    // HPA the rest of the way to the goal using get_path from the last position in the new path to the goal
    if let Some(new_path) = new_path {
        let mut hpa_path = Vec::new();

        for pos in new_path.path() {
            hpa_path.push(*pos);
        }

        let last_pos = *new_path.path().last().unwrap();

        let hpa = pathfind(grid, last_pos, goal, blocking, false, refined);

        if let Some(hpa) = hpa {
            for pos in hpa.path() {
                hpa_path.push(*pos);
            }

            let mut path = Path::new(hpa_path, new_path.cost() + hpa.cost());
            path.graph_path = hpa.graph_path.clone();
            return Some(path);
        }
    }

    None
}