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
//! This module defines the `Neighborhood` trait and its implementations for different types of neighborhoods.
use bevy::math::{IVec3, UVec3};
use ndarray::ArrayView3;
use std::sync::Arc;
use crate::{filter::NeighborFilter, grid::NeighborhoodSettings, nav::NavCell};
/// The `Neighborhood` trait defines the interface for different neighborhood types.
/// You can implement this trait to define custom neighborhoods and hueristics.
pub trait Neighborhood: Clone + Default + Sync + Send {
/// Returns the possible directions at a position by the neighborhood.
fn directions(&self) -> &'static [(i32, i32, i32)];
/// Returns a u32 bitmask representing the neighbors of a given position in the grid.
/// If you implement this method, you will need to ensure that the returned bit mask is in the correct order.
/// The default implementation should handle almost all directional movement cases, you can likely use `filter_neighbors` instead
/// if you want customer neighbor logic. It should handle most custom cases.
///
/// You will also need to ensure the neighbors are within the bounds of the grid.
///
/// Bit Mask Format:
/// Index: Offset (x, y, z)
/// ------ -----------------
/// 0 (-1, -1, -1) ← North-West-Down
/// 1 ( 0, -1, -1) ← North-Down
/// 2 ( 1, -1, -1) ← South-West-Down
/// 3 (-1, 0, -1) ← West-Down
/// 4 ( 0, 0, -1) ← Down
/// 5 ( 1, 0, -1) ← East-Down
/// 6 (-1, 1, -1) ← North-East-Down
/// 7 ( 0, 1, -1) ← North-Down
/// 8 ( 1, 1, -1) ← South-East-Down
/// 9 (-1, -1, 0) ← North-West
/// 10 ( 0, -1, 0) ← North
/// 11 ( 1, -1, 0) ← South-West
/// 12 (-1, 0, 0) ← West
/// 13 ( 1, 0, 0) ← East
/// 14 (-1, 1, 0) ← North-East
/// 15 ( 0, 1, 0) ← South
/// 16 ( 1, 1, 0) ← South-East
/// 17 (-1, -1, 1) ← North-West-Up
/// 18 ( 0, -1, 1) ← North-Up
/// 19 ( 1, -1, 1) ← South-West-Up
/// 20 (-1, 0, 1) ← West-Up
/// 21 ( 0, 0, 1) ← Up
/// 22 ( 1, 0, 1) ← East-Up
/// 23 (-1, 1, 1) ← North-East-Up
/// 24 ( 0, 1, 1) ← South-Up
/// 25 ( 1, 1, 1) ← South-East-Up
fn neighbors(&self, grid: &ArrayView3<NavCell>, pos: UVec3) -> u32 {
let shape = grid.shape();
let mut bits = 0;
let origin = pos.as_ivec3();
for &dir in self.directions() {
let offset = IVec3::new(dir.0, dir.1, dir.2);
if let Some(i) = ORDINAL_3D_OFFSETS.iter().position(|&o| o == offset) {
let neighbor = origin + offset;
if neighbor.cmplt(IVec3::ZERO).any() {
continue;
}
let nx = neighbor.x as usize;
let ny = neighbor.y as usize;
let nz = neighbor.z as usize;
if nx < shape[0]
&& ny < shape[1]
&& nz < shape[2]
&& !grid[[nx, ny, nz]].is_impassable()
{
bits |= 1 << i;
}
} else {
panic!("Direction {offset:?} not in ORDINAL_3D_OFFSETS!");
}
}
// Apply filters
for filter in self.filters() {
bits = filter.filter(pos, bits, grid);
}
bits
}
/// Returns the filters applied to the neighborhood.
fn filters(&self) -> &[Arc<dyn NeighborFilter + Send + Sync + 'static>];
/// Returns the heuristic cost from a position to a target.
fn heuristic(&self, pos: UVec3, target: UVec3) -> u32;
/// Returns true if the neighborhood allows ordinal movement.
fn is_ordinal(&self) -> bool {
false
}
/// Returns any settings for the neighborhood.
fn settings(&self) -> Option<NeighborhoodSettings> {
None
}
/// Optional constructor override for settings-based neighborhood
fn from_settings(_settings: &NeighborhoodSettings) -> Self {
Self::default()
}
}
/// Use `CardinalNeighborhood` for standard 2D pathfinding with no diagonal movement.
#[derive(Clone, Default)]
pub struct CardinalNeighborhood {
pub(crate) filters: Vec<Arc<dyn NeighborFilter + Send + Sync + 'static>>,
}
impl Neighborhood for CardinalNeighborhood {
#[inline(always)]
fn directions(&self) -> &'static [(i32, i32, i32)] {
// The third coordinate is always 0 for 2D neighborhoods.
static DIRECTIONS: [(i32, i32, i32); 4] = [
(-1, 0, 0), // North
(1, 0, 0), // South
(0, -1, 0), // West
(0, 1, 0), // East
];
&DIRECTIONS
}
#[inline(always)]
fn heuristic(&self, pos: UVec3, target: UVec3) -> u32 {
((pos.x as i32 - target.x as i32).abs() + (pos.y as i32 - target.y as i32).abs()) as u32
}
fn from_settings(settings: &NeighborhoodSettings) -> Self {
Self {
filters: settings.filters.clone(),
}
}
#[inline(always)]
fn settings(&self) -> Option<NeighborhoodSettings> {
Some(NeighborhoodSettings {
filters: self.filters.clone(),
})
}
fn filters(&self) -> &[Arc<dyn NeighborFilter + Send + Sync + 'static>] {
&self.filters
}
}
/// Use `CardinalNeighborhood3d` for 3D pathfinding with no diagonal movement.
/// This neighborhood allows movement in the cardinal directions in 3D space only in UP or DOWN directions.
#[derive(Clone, Default)]
pub struct CardinalNeighborhood3d {
pub(crate) filters: Vec<Arc<dyn NeighborFilter + Send + Sync + 'static>>,
}
impl Neighborhood for CardinalNeighborhood3d {
#[inline(always)]
fn directions(&self) -> &'static [(i32, i32, i32)] {
// Cardinal directions in 3D: N, S, E, W, UP, DOWN
// The third coordinate is always 0 for 2D neighborhoods.
static DIRECTIONS: [(i32, i32, i32); 6] = [
(-1, 0, 0), // North
(1, 0, 0), // South
(0, -1, 0), // West
(0, 1, 0), // East
(0, 0, -1), // Down
(0, 0, 1), // Up
];
&DIRECTIONS
}
#[inline(always)]
fn heuristic(&self, pos: UVec3, target: UVec3) -> u32 {
let dx = pos.x.max(target.x) - pos.x.min(target.x);
let dy = pos.y.max(target.y) - pos.y.min(target.y);
let dz = pos.z.max(target.z) - pos.z.min(target.z);
dx + dy + dz
}
fn from_settings(settings: &NeighborhoodSettings) -> Self {
Self {
filters: settings.filters.clone(),
}
}
#[inline(always)]
fn settings(&self) -> Option<NeighborhoodSettings> {
Some(NeighborhoodSettings {
filters: self.filters.clone(),
})
}
fn filters(&self) -> &[Arc<dyn NeighborFilter + Send + Sync + 'static>] {
&self.filters
}
}
/// Use `OrdinalNeighborhood` for 2D pathfinding with diagonal movement.
/// This neighborhood allows movement in all 8 directions.
#[derive(Clone, Default)]
pub struct OrdinalNeighborhood {
pub(crate) filters: Vec<Arc<dyn NeighborFilter + Send + Sync + 'static>>,
}
impl Neighborhood for OrdinalNeighborhood {
#[inline(always)]
fn directions(&self) -> &'static [(i32, i32, i32)] {
// Ordinal directions in 2D: N, S, E, W, NE, SE, SW, NW
// The third coordinate is always 0 for 2D neighborhoods.
static DIRECTIONS: [(i32, i32, i32); 8] = [
(-1, 0, 0), // North
(1, 0, 0), // South
(0, -1, 0), // West
(0, 1, 0), // East
(-1, -1, 0), // North-East
(1, -1, 0), // South-East
(1, 1, 0), // South-West
(-1, 1, 0), // North-West
];
&DIRECTIONS
}
/*#[inline(always)]
fn heuristic(&self, pos: UVec3, target: UVec3) -> u32 {
let dx = pos.x.abs_diff(target.x);
let dy = pos.y.abs_diff(target.y);
dx.max(dy)
}*/
#[inline(always)]
fn heuristic(&self, pos: UVec3, target: UVec3) -> u32 {
let dx = (target.x as i32 - pos.x as i32).unsigned_abs();
let dy = (target.y as i32 - pos.y as i32).unsigned_abs();
let dz = (target.z as i32 - pos.z as i32).unsigned_abs();
let base = dx.max(dy).max(dz) * 1000;
let tie_breaker = dx + dy + dz;
base + tie_breaker // returns scaled u32
}
#[inline(always)]
fn is_ordinal(&self) -> bool {
true
}
fn from_settings(settings: &NeighborhoodSettings) -> Self {
Self {
filters: settings.filters.clone(),
}
}
#[inline(always)]
fn settings(&self) -> Option<NeighborhoodSettings> {
Some(NeighborhoodSettings {
filters: self.filters.clone(),
})
}
fn filters(&self) -> &[Arc<dyn NeighborFilter + Send + Sync + 'static>] {
&self.filters
}
}
/// Use `OrdinalNeighborhood3d` for 3D pathfinding with diagonal movement.
/// This neighborhood allows movement in all 26 directions.
/// It's the 3D version of `OrdinalNeighborhood`.
#[derive(Clone, Default)]
pub struct OrdinalNeighborhood3d {
pub(crate) filters: Vec<Arc<dyn NeighborFilter + Send + Sync + 'static>>,
}
impl Neighborhood for OrdinalNeighborhood3d {
#[inline(always)]
fn directions(&self) -> &'static [(i32, i32, i32)] {
// Ordinal directions in 3D: N, S, E, W, NE, SE, SW, NW, UP, DOWN
// The third coordinate is always 0 for 2D neighborhoods.
static DIRECTIONS: [(i32, i32, i32); 26] = [
(-1, -1, -1), // North-West-Down
(-1, -1, 0), // North-West
(-1, -1, 1), // North-West-Up
(-1, 0, -1), // North-Down
(-1, 0, 0), // North
(-1, 0, 1), // North-Up
(-1, 1, -1), // North-East-Down
(-1, 1, 0), // North-East
(-1, 1, 1), // North-East-Up
(0, -1, -1), // West-Down
(0, -1, 0), // West
(0, -1, 1), // West-Up
(0, 0, -1), // Down
(0, 0, 1), // Up
(0, 1, -1), // East-Down
(0, 1, 0), // East
(0, 1, 1), // East-Up
(1, -1, -1), // South-West-Down
(1, -1, 0), // South-West
(1, -1, 1), // South-West-Up
(1, 0, -1), // South-Down
(1, 0, 0), // South
(1, 0, 1), // South-Up
(1, 1, -1), // South-East-Down
(1, 1, 0), // South-East
(1, 1, 1), // South-East-Up
];
&DIRECTIONS
}
#[inline(always)]
fn heuristic(&self, pos: UVec3, target: UVec3) -> u32 {
/* let dx = (target.x as i32 - pos.x as i32).abs() as u32;
let dy = (target.y as i32 - pos.y as i32).abs() as u32;
let dz = (target.z as i32 - pos.z as i32).abs() as u32;
let base = dx + dy + dz;
// Tie breaker: prefer straighter (less "jittery") paths
let max_axis = dx.max(dy).max(dz);
// Scaled to preserve integer math
base * 1000 + max_axis */
let dx = pos.x.abs_diff(target.x);
let dy = pos.y.abs_diff(target.y);
let dz = pos.z.abs_diff(target.z);
dx.max(dy).max(dz)
}
#[inline(always)]
fn is_ordinal(&self) -> bool {
true
}
fn from_settings(settings: &NeighborhoodSettings) -> Self {
Self {
filters: settings.filters.clone(),
}
}
#[inline(always)]
fn settings(&self) -> Option<NeighborhoodSettings> {
Some(NeighborhoodSettings {
filters: self.filters.clone(),
})
}
fn filters(&self) -> &[Arc<dyn NeighborFilter + Send + Sync + 'static>] {
&self.filters
}
}
pub(crate) const ORDINAL_3D_OFFSETS: [IVec3; 26] = {
let mut offsets = [IVec3::ZERO; 26];
let mut index = 0;
let mut dz = -1;
while dz <= 1 {
let mut dy = -1;
while dy <= 1 {
let mut dx = -1;
while dx <= 1 {
if dx != 0 || dy != 0 || dz != 0 {
offsets[index] = IVec3::new(dx, dy, dz);
index += 1;
}
dx += 1;
}
dy += 1;
}
dz += 1;
}
offsets
};
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cardinal_neighbors() {
let neighborhood = CardinalNeighborhood {
filters: Vec::new(),
};
let cells: [NavCell; 9] = std::array::from_fn(|_| NavCell::default());
let grid = ArrayView3::from_shape((3, 3, 1), &cells).unwrap();
let neighbors = neighborhood.neighbors(&grid, UVec3::new(1, 1, 0));
assert_eq!(neighbors.count_ones(), 4);
}
#[test]
fn test_cardinal_neighbors_3d() {
let neighborhood = CardinalNeighborhood3d {
filters: Vec::new(),
};
let cells: [NavCell; 27] = std::array::from_fn(|_| NavCell::default());
let grid = ArrayView3::from_shape((3, 3, 3), &cells).unwrap();
let neighbors = neighborhood.neighbors(&grid, UVec3::new(1, 1, 1));
assert_eq!(neighbors.count_ones(), 6);
}
#[test]
fn test_ordinal_neighbors_3d() {
let neighborhood = OrdinalNeighborhood3d {
filters: Vec::new(),
};
let cells: [NavCell; 27] = std::array::from_fn(|_| NavCell::default());
let grid = ArrayView3::from_shape((3, 3, 3), &cells).unwrap();
let neighbors_mask = neighborhood.neighbors(&grid, UVec3::new(1, 1, 1));
// All 26 neighbors should be available (all bits set)
assert_eq!(neighbors_mask.count_ones(), 26);
// Check that each bit corresponds to a valid neighbor position
let mut expected_positions = Vec::new();
let center = UVec3::new(1, 1, 1);
let mut bit = 0;
for dz in -1..=1 {
for dy in -1..=1 {
for dx in -1..=1 {
if dx == 0 && dy == 0 && dz == 0 {
continue; // skip center
}
let pos = UVec3::new(
(center.x as i32 + dx) as u32,
(center.y as i32 + dy) as u32,
(center.z as i32 + dz) as u32,
);
// The bit at position `bit` should be set
assert!(
(neighbors_mask & (1 << bit)) != 0,
"Neighbor bit {bit} (pos {pos:?}) not set"
);
expected_positions.push(pos);
bit += 1;
}
}
}
assert_eq!(bit, 26);
}
#[test]
#[allow(clippy::identity_op)]
fn test_ordinal_neighbors_at_0() {
let neighborhood = OrdinalNeighborhood3d {
filters: Vec::new(),
};
let cells: [NavCell; 27] = std::array::from_fn(|_| NavCell::default());
let grid = ArrayView3::from_shape((3, 3, 3), &cells).unwrap();
let neighbors = neighborhood.neighbors(&grid, UVec3::new(0, 0, 0));
// Count the number of set bits (neighbors)
assert_eq!(neighbors.count_ones(), 7);
// Check that the correct neighbor bits are set
let expected_positions = [
(0, 0, 1),
(0, 1, 0),
(0, 1, 1),
(1, 0, 0),
(1, 0, 1),
(1, 1, 0),
(1, 1, 1),
];
let mut bit = 0;
for dz in -1..=1 {
for dy in -1..=1 {
for dx in -1..=1 {
if dx == 0 && dy == 0 && dz == 0 {
continue;
}
let pos = (
(0_i32 + dx) as u32,
(0_i32 + dy) as u32,
(0_i32 + dz) as u32,
);
if expected_positions.contains(&pos) {
assert!(
(neighbors & (1 << bit)) != 0,
"Expected neighbor at {pos:?} (bit {bit}) to be set"
);
} else {
assert!(
(neighbors & (1 << bit)) == 0,
"Unexpected neighbor at {pos:?} (bit {bit}) set"
);
}
bit += 1;
}
}
}
}
#[test]
#[allow(clippy::identity_op)]
fn test_ordinal_neighbors_no_depth() {
let neighborhood = OrdinalNeighborhood3d {
filters: Vec::new(),
};
let cells: [NavCell; 9] = std::array::from_fn(|_| NavCell::default());
let grid = ArrayView3::from_shape((3, 3, 1), &cells).unwrap();
let neighbors = neighborhood.neighbors(&grid, UVec3::new(1, 1, 0));
// There should be 8 neighbors (all 2D surrounding positions)
assert_eq!(neighbors.count_ones(), 8);
let expected_positions = [
(0, 0, 0),
(0, 1, 0),
(0, 2, 0),
(1, 0, 0),
(1, 2, 0),
(2, 0, 0),
(2, 1, 0),
(2, 2, 0),
];
let mut bit = 0;
for dz in -1..=1 {
for dy in -1..=1 {
for dx in -1..=1 {
if dx == 0 && dy == 0 && dz == 0 {
continue;
}
let pos = (
(1_i32 + dx) as u32,
(1_i32 + dy) as u32,
(0_i32 + dz) as u32,
);
if expected_positions.contains(&pos) {
assert!(
(neighbors & (1 << bit)) != 0,
"Expected neighbor at {pos:?} (bit {bit}) to be set"
);
} else {
assert!(
(neighbors & (1 << bit)) == 0,
"Unexpected neighbor at {pos:?} (bit {bit}) set"
);
}
bit += 1;
}
}
}
assert_eq!(bit, 26);
}
#[test]
fn test_ordinal_heuristic() {
let neighborhood = OrdinalNeighborhood3d {
filters: Vec::new(),
};
assert_eq!(
neighborhood.heuristic(UVec3::new(0, 0, 0), UVec3::new(1, 1, 1)),
1
);
assert_eq!(
neighborhood.heuristic(UVec3::new(0, 0, 0), UVec3::new(1, 0, 0)),
1
);
assert_eq!(
neighborhood.heuristic(UVec3::new(0, 0, 0), UVec3::new(0, 0, 0)),
0
);
assert_eq!(
neighborhood.heuristic(UVec3::new(0, 0, 0), UVec3::new(2, 2, 2)),
2
);
assert_eq!(
neighborhood.heuristic(UVec3::new(0, 0, 0), UVec3::new(7, 7, 7)),
7
);
}
}