crcbl-nav 0.20.0

Crucible navigation: deterministic ground and span grid routing and bounded reach
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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
//! Bounded, deterministic routing over a ground grid of walkable and blocked
//! cells.
//!
//! A [`GroundGrid`] is a [`GroundGridConfig`] — where the grid sits, how big a
//! cell is and how many there are — and one [`GridCell`] per cell, row by row
//! along +X, rows stacked along +Z. It answers two questions about a world
//! position on the ground:
//!
//! * [`GroundGrid::plan`]: the cell centres of a shortest four-neighbour walk to
//!   another position, or why there is none.
//! * [`GroundGrid::reachable_within`]: every walkable cell centre within a step
//!   budget, with how many steps each one took.
//!
//! And one about a position on a blocked cell, where both answer
//! [`GroundRouteError::NoRoute`]: [`GroundGrid::nearest_walkable`], the
//! walkable cell centre to plan from instead.
//!
//! # The grid is flat
//!
//! Only a position's X and Z pick its cell; its Y is read for finiteness and
//! otherwise ignored, and every cell centre handed back sits at the origin's
//! Y. The grid describes one floor. A caller whose ground is not flat keeps its
//! own heights and uses these answers for X and Z.
//!
//! # Why breadth-first search, and how ties are broken
//!
//! Every step between neighbouring cells costs the same, so a breadth-first
//! search already finds a shortest route: the first time it reaches a cell is
//! over the fewest steps. A\* with a distance heuristic finds a route of the
//! same length while visiting fewer cells, but which of several equally short
//! routes it returns depends on how its priority queue orders equal keys — one
//! more thing two implementations can disagree on. Breadth-first has no such
//! queue. [`MAX_GRID_CELL_COUNT`] bounds the whole grid, so the cells it
//! visits are bounded too.
//!
//! **The tie-break is the neighbour order, +Z, then +X, then -Z, then -X**,
//! over a first-in first-out frontier. A cell's predecessor is the first
//! frontier cell to reach it, so when several shortest routes exist the one
//! returned leans +Z first and +X second: on an open grid it goes all the way
//! +Z before it turns +X. The same grid and the same two positions always
//! return the same route, on every machine, and
//! `tests::identical_grids_give_identical_routes_that_lean_plus_z_then_plus_x`
//! is what holds the order to that.
//!
//! # Validation is at the boundary
//!
//! [`GroundGrid::new`] refuses a non-finite origin, a cell size that is not
//! finite and positive, a zero width or height, a cell count past
//! [`MAX_GRID_CELL_COUNT`] (or past `usize`), and a cell map whose length is
//! not that count — each with its own [`GroundGridConfigError`]. After that
//! every index the queries compute is in range by construction. A query
//! refuses a non-finite position, a position outside the grid, and a start or
//! goal on a blocked cell, with a [`GroundRouteError`].
//!
//! # Cost
//!
//! Each [`plan`](GroundGrid::plan) and
//! [`reachable_within`](GroundGrid::reachable_within) allocates one scratch
//! entry per cell of the grid, and visits each cell at most once. Nothing is
//! kept between calls, so a grid is shared by reference and never mutated by
//! a query.

use glam::DVec3;
use std::collections::VecDeque;

/// The most cells a [`GroundGrid`] may hold.
///
/// Every query allocates scratch space proportional to the cell count, so this
/// is what bounds a query's memory and time as well as the grid's own. A config
/// past it is refused with [`GroundGridConfigError::TooManyCells`] rather than
/// clamped, because a clamped grid would describe a different floor.
pub const MAX_GRID_CELL_COUNT: usize = 262_144;

/// Where a ground grid sits and how it is cut up.
///
/// `origin` is the grid's minimum X/Z corner, and its Y is the height every
/// cell centre is reported at. Cell `(x, z)` covers
/// `[origin.x + x·cell_size_m, origin.x + (x+1)·cell_size_m)` along X and the
/// same along Z, for `x < width` and `z < height`.
///
/// The fields are public and nothing is checked here, so a config can be a
/// `const`; [`GroundGrid::new`] is where it is validated.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct GroundGridConfig {
    /// The grid's minimum X/Z corner, and the Y of every cell centre.
    pub origin: DVec3,
    /// The side of one square cell, in metres.
    pub cell_size_m: f64,
    /// Cells along +X.
    pub width: usize,
    /// Cells along +Z.
    pub height: usize,
}

impl GroundGridConfig {
    /// A config from its four fields, unchecked — see [`GroundGrid::new`] for
    /// what is refused.
    #[must_use]
    pub const fn new(origin: DVec3, cell_size_m: f64, width: usize, height: usize) -> Self {
        Self {
            origin,
            cell_size_m,
            width,
            height,
        }
    }

    /// The number of cells, or which rule the config breaks.
    fn cell_count(self) -> Result<usize, GroundGridConfigError> {
        if !self.origin.is_finite() {
            return Err(GroundGridConfigError::InvalidOrigin);
        }
        if !self.cell_size_m.is_finite() || self.cell_size_m <= 0.0 {
            return Err(GroundGridConfigError::InvalidCellSize);
        }
        if self.width == 0 || self.height == 0 {
            return Err(GroundGridConfigError::InvalidDimensions);
        }
        let count = self
            .width
            .checked_mul(self.height)
            .ok_or(GroundGridConfigError::TooManyCells)?;
        if count > MAX_GRID_CELL_COUNT {
            return Err(GroundGridConfigError::TooManyCells);
        }
        Ok(count)
    }
}

/// Whether an agent may stand on one ground-grid cell.
///
/// Authored by the caller: this crate does not decide what is walkable, only
/// how to cross what it is told is.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum GridCell {
    /// An agent may stand here and step through.
    Walkable,
    /// An agent may neither stand here nor step through.
    Blocked,
}

/// Why [`GroundGrid::new`] refused a config or a cell map.
#[derive(Clone, Copy, Debug, Eq, PartialEq, thiserror::Error)]
pub enum GroundGridConfigError {
    /// A component of the origin is NaN or infinite.
    #[error("the grid origin is not a finite position")]
    InvalidOrigin,
    /// The cell size is NaN, infinite, zero or negative.
    #[error("the cell size is not a finite positive length")]
    InvalidCellSize,
    /// The width or the height is zero.
    #[error("the grid has a zero width or height")]
    InvalidDimensions,
    /// `width × height` overflows `usize` or exceeds [`MAX_GRID_CELL_COUNT`].
    #[error("the grid has more cells than MAX_GRID_CELL_COUNT allows")]
    TooManyCells,
    /// The cell map does not hold exactly `width × height` cells.
    #[error("the cell map's length is not the grid's width times its height")]
    CellCountMismatch,
}

/// Why a query on a [`GroundGrid`] produced no answer.
#[derive(Clone, Copy, Debug, Eq, PartialEq, thiserror::Error)]
pub enum GroundRouteError {
    /// A position has a NaN or infinite component.
    #[error("a position is not finite")]
    InvalidWorldPosition,
    /// A position's X or Z falls outside the grid.
    #[error("a position is outside the grid")]
    OutsideGrid,
    /// The start or the goal is on a blocked cell, or no walkable route joins
    /// them.
    #[error("no walkable route joins the start and the goal")]
    NoRoute,
}

/// A route across a [`GroundGrid`], as the cell centres to walk to.
#[derive(Clone, Debug, PartialEq)]
pub struct GroundRoute {
    /// The centre of every cell the walk enters, in order, ending with the
    /// goal's. The start cell is not in it — the agent is already there — so a
    /// start and goal in the same cell give an empty list.
    pub waypoints: Vec<DVec3>,
}

/// A validated ground grid: its config and one [`GridCell`] per cell.
#[derive(Clone, Debug, PartialEq)]
pub struct GroundGrid {
    config: GroundGridConfig,
    cells: Vec<GridCell>,
}

impl GroundGrid {
    /// A grid from its config and its cells, row by row along +X with rows
    /// stacked along +Z: cell `(x, z)` is `cells[z * width + x]`.
    ///
    /// # Errors
    ///
    /// The config's first broken rule, checked in [`GroundGridConfigError`]'s
    /// declaration order, then [`GroundGridConfigError::CellCountMismatch`] if
    /// `cells` is not `width × height` long.
    pub fn new(
        config: GroundGridConfig,
        cells: Vec<GridCell>,
    ) -> Result<Self, GroundGridConfigError> {
        let cell_count = config.cell_count()?;
        if cells.len() != cell_count {
            return Err(GroundGridConfigError::CellCountMismatch);
        }
        Ok(Self { config, cells })
    }

    /// A shortest four-neighbour route from the cell holding
    /// `start_world_position` to the cell holding `goal_world_position`.
    ///
    /// Among equally short routes, the one the +Z, +X, -Z, -X neighbour order
    /// reaches first — see the module docs. A start and goal in the same
    /// walkable cell give an empty route.
    ///
    /// # Errors
    ///
    /// [`GroundRouteError::InvalidWorldPosition`] or
    /// [`GroundRouteError::OutsideGrid`] for the start, then for the goal;
    /// [`GroundRouteError::NoRoute`] if either is on a blocked cell or no
    /// walkable route joins them.
    pub fn plan(
        &self,
        start_world_position: DVec3,
        goal_world_position: DVec3,
    ) -> Result<GroundRoute, GroundRouteError> {
        let start = self.world_to_cell(start_world_position)?;
        let goal = self.world_to_cell(goal_world_position)?;
        if self.cells[start] == GridCell::Blocked || self.cells[goal] == GridCell::Blocked {
            return Err(GroundRouteError::NoRoute);
        }
        if start == goal {
            return Ok(GroundRoute {
                waypoints: Vec::new(),
            });
        }

        let mut predecessors = vec![None; self.cells.len()];
        let mut frontier = VecDeque::new();
        predecessors[start] = Some(start);
        frontier.push_back(start);

        while let Some(current) = frontier.pop_front() {
            for neighbor in self.neighbors(current).into_iter().flatten() {
                if self.cells[neighbor] == GridCell::Blocked || predecessors[neighbor].is_some() {
                    continue;
                }
                predecessors[neighbor] = Some(current);
                if neighbor == goal {
                    return Ok(self.build_route(start, goal, &predecessors));
                }
                frontier.push_back(neighbor);
            }
        }

        Err(GroundRouteError::NoRoute)
    }

    /// Every walkable cell centre reachable from `start_world_position` within
    /// `max_steps` four-neighbour steps, with its step count.
    ///
    /// The start cell comes first, at zero steps. After it the list is in
    /// breadth-first order — step counts never decrease — and cells at the same
    /// count are in the order the +Z, +X, -Z, -X neighbour order reached them,
    /// so the list is deterministic. A cell's count is its fewest steps from
    /// the start; no cell appears twice, and none past `max_steps` appears at
    /// all. `max_steps == 0` gives the start alone.
    ///
    /// # Errors
    ///
    /// [`GroundRouteError::InvalidWorldPosition`] or
    /// [`GroundRouteError::OutsideGrid`] for the start;
    /// [`GroundRouteError::NoRoute`] if it is on a blocked cell.
    pub fn reachable_within(
        &self,
        start_world_position: DVec3,
        max_steps: usize,
    ) -> Result<Vec<(DVec3, usize)>, GroundRouteError> {
        let start = self.world_to_cell(start_world_position)?;
        if self.cells[start] == GridCell::Blocked {
            return Err(GroundRouteError::NoRoute);
        }
        let mut steps = vec![None; self.cells.len()];
        let mut frontier = VecDeque::new();
        let mut reached = Vec::new();
        steps[start] = Some(0);
        frontier.push_back(start);
        while let Some(current) = frontier.pop_front() {
            let current_steps = steps[current].expect("a queued cell has its step count");
            reached.push((self.cell_center(current), current_steps));
            if current_steps == max_steps {
                continue;
            }
            for neighbor in self.neighbors(current).into_iter().flatten() {
                if self.cells[neighbor] == GridCell::Walkable && steps[neighbor].is_none() {
                    steps[neighbor] = Some(current_steps + 1);
                    frontier.push_back(neighbor);
                }
            }
        }
        Ok(reached)
    }

    /// The centre of the walkable cell nearest `world_position`, among the
    /// cells within `max_rings` rings of the one holding it, or `None` if every
    /// one of them is blocked.
    ///
    /// For a start on a blocked cell — an agent pushed into a door leaf or by
    /// a squadmate — where [`plan`](Self::plan) and
    /// [`reachable_within`](Self::reachable_within) answer
    /// [`GroundRouteError::NoRoute`]: plan from this centre instead, and walk
    /// to it first.
    ///
    /// A walkable cell answers its own centre. Otherwise the nearest is by the
    /// distance across X and Z from `world_position` to each centre, ties going
    /// to the lower row and then the lower column, so the answer is the same
    /// every time. Ring `r` is the cells `r` steps away with a diagonal step
    /// counted as one, so `max_rings == 0` searches the position's own cell
    /// alone. The search visits every cell of the square the rings cover that
    /// lies on the grid.
    ///
    /// # Errors
    ///
    /// [`GroundRouteError::InvalidWorldPosition`] or
    /// [`GroundRouteError::OutsideGrid`] for the position.
    pub fn nearest_walkable(
        &self,
        world_position: DVec3,
        max_rings: usize,
    ) -> Result<Option<DVec3>, GroundRouteError> {
        let cell = self.world_to_cell(world_position)?;
        if self.cells[cell] == GridCell::Walkable {
            return Ok(Some(self.cell_center(cell)));
        }
        let (width, height) = (self.config.width, self.config.height);
        let (x, z) = (cell % width, cell / width);
        let mut nearest: Option<(f64, usize)> = None;
        for row in z.saturating_sub(max_rings)..=z.saturating_add(max_rings).min(height - 1) {
            for column in x.saturating_sub(max_rings)..=x.saturating_add(max_rings).min(width - 1) {
                let index = row * width + column;
                if self.cells[index] == GridCell::Blocked {
                    continue;
                }
                let center = self.cell_center(index);
                let distance = (center.x - world_position.x).hypot(center.z - world_position.z);
                // Strictly nearer, so a tie keeps the earlier cell in row order.
                if nearest.is_none_or(|(best, _)| distance < best) {
                    nearest = Some((distance, index));
                }
            }
        }
        Ok(nearest.map(|(_, index)| self.cell_center(index)))
    }

    /// The side of one grid cell, in metres — what one step of
    /// [`reachable_within`](Self::reachable_within) is worth.
    #[must_use]
    pub fn cell_size_m(&self) -> f64 {
        self.config.cell_size_m
    }

    /// The index of the cell holding `position`'s X and Z.
    fn world_to_cell(&self, position: DVec3) -> Result<usize, GroundRouteError> {
        if !position.is_finite() {
            return Err(GroundRouteError::InvalidWorldPosition);
        }
        let relative_x = (position.x - self.config.origin.x) / self.config.cell_size_m;
        let relative_z = (position.z - self.config.origin.z) / self.config.cell_size_m;
        // Finite inputs can still overflow here — a position near `f64::MAX`
        // over a small cell — and an infinite offset is certainly off the grid.
        if !relative_x.is_finite() || !relative_z.is_finite() {
            return Err(GroundRouteError::OutsideGrid);
        }
        let x = relative_x.floor();
        let z = relative_z.floor();
        if x < 0.0 || z < 0.0 || x >= self.config.width as f64 || z >= self.config.height as f64 {
            return Err(GroundRouteError::OutsideGrid);
        }
        Ok(z as usize * self.config.width + x as usize)
    }

    /// The in-grid neighbours of `cell`, in the tie-break order the module
    /// docs name: +Z, +X, -Z, -X. `None` is an edge of the grid.
    fn neighbors(&self, cell: usize) -> [Option<usize>; 4] {
        let x = cell % self.config.width;
        let z = cell / self.config.width;
        [
            if z + 1 < self.config.height {
                Some(cell + self.config.width)
            } else {
                None
            },
            if x + 1 < self.config.width {
                Some(cell + 1)
            } else {
                None
            },
            if z > 0 {
                Some(cell - self.config.width)
            } else {
                None
            },
            if x > 0 { Some(cell - 1) } else { None },
        ]
    }

    /// The route from `start` to `goal` read back along `predecessors`.
    fn build_route(
        &self,
        start: usize,
        goal: usize,
        predecessors: &[Option<usize>],
    ) -> GroundRoute {
        let mut cells = Vec::new();
        let mut current = goal;
        while current != start {
            cells.push(current);
            current = predecessors[current].expect("visited route cell has a predecessor");
        }
        cells.reverse();
        GroundRoute {
            waypoints: cells
                .into_iter()
                .map(|cell| self.cell_center(cell))
                .collect(),
        }
    }

    /// The centre of `cell`, at the origin's height.
    fn cell_center(&self, cell: usize) -> DVec3 {
        let x = cell % self.config.width;
        let z = cell / self.config.width;
        DVec3::new(
            self.config.origin.x + (x as f64 + 0.5) * self.config.cell_size_m,
            self.config.origin.y,
            self.config.origin.z + (z as f64 + 0.5) * self.config.cell_size_m,
        )
    }
}

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

    fn grid(width: usize, height: usize, cells: Vec<GridCell>) -> GroundGrid {
        GroundGrid::new(
            GroundGridConfig::new(DVec3::ZERO, 1.0, width, height),
            cells,
        )
        .unwrap()
    }

    #[test]
    fn straight_route_returns_cell_centres_after_the_start_cell() {
        let grid = grid(4, 1, vec![GridCell::Walkable; 4]);

        let route = grid
            .plan(DVec3::new(0.1, 0.0, 0.1), DVec3::new(3.9, 0.0, 0.1))
            .unwrap();

        assert_eq!(route.waypoints.len(), 3);
        assert_eq!(
            route.waypoints,
            vec![
                DVec3::new(1.5, 0.0, 0.5),
                DVec3::new(2.5, 0.0, 0.5),
                DVec3::new(3.5, 0.0, 0.5),
            ]
        );
    }

    #[test]
    fn route_around_a_wall_uses_the_only_gap() {
        let grid = grid(
            5,
            3,
            vec![
                GridCell::Walkable,
                GridCell::Walkable,
                GridCell::Blocked,
                GridCell::Walkable,
                GridCell::Walkable,
                GridCell::Walkable,
                GridCell::Walkable,
                GridCell::Walkable,
                GridCell::Walkable,
                GridCell::Walkable,
                GridCell::Walkable,
                GridCell::Walkable,
                GridCell::Blocked,
                GridCell::Walkable,
                GridCell::Walkable,
            ],
        );

        let route = grid
            .plan(DVec3::new(0.1, 0.0, 0.1), DVec3::new(4.1, 0.0, 0.1))
            .unwrap();

        assert_eq!(route.waypoints.len(), 6);
        assert!(route.waypoints.contains(&DVec3::new(2.5, 0.0, 1.5)));
        assert_eq!(route.waypoints.last(), Some(&DVec3::new(4.5, 0.0, 0.5)));
    }

    #[test]
    fn sealed_wall_has_no_route() {
        let grid = grid(
            3,
            2,
            vec![
                GridCell::Walkable,
                GridCell::Blocked,
                GridCell::Walkable,
                GridCell::Walkable,
                GridCell::Blocked,
                GridCell::Walkable,
            ],
        );

        assert_eq!(
            grid.plan(DVec3::new(0.1, 0.0, 0.1), DVec3::new(2.1, 0.0, 0.1)),
            Err(GroundRouteError::NoRoute)
        );
    }

    #[test]
    fn positions_outside_the_grid_are_rejected() {
        let grid = grid(1, 1, vec![GridCell::Walkable]);

        assert_eq!(
            grid.plan(DVec3::new(-0.01, 0.0, 0.5), DVec3::new(0.5, 0.0, 0.5)),
            Err(GroundRouteError::OutsideGrid)
        );
        assert_eq!(
            grid.plan(DVec3::new(0.5, 0.0, 0.5), DVec3::new(1.0, 0.0, 0.5)),
            Err(GroundRouteError::OutsideGrid)
        );
    }

    #[test]
    fn equal_cost_routes_use_the_stable_north_then_east_tie_break() {
        let grid = grid(2, 2, vec![GridCell::Walkable; 4]);

        let first = grid
            .plan(DVec3::new(0.1, 0.0, 0.1), DVec3::new(1.1, 0.0, 1.1))
            .unwrap();
        let second = grid
            .plan(DVec3::new(0.1, 0.0, 0.1), DVec3::new(1.1, 0.0, 1.1))
            .unwrap();

        assert_eq!(first, second);
        assert_eq!(
            first.waypoints,
            vec![DVec3::new(0.5, 0.0, 1.5), DVec3::new(1.5, 0.0, 1.5)]
        );
    }

    #[test]
    fn invalid_and_overflow_dimensions_are_rejected() {
        assert_eq!(
            GroundGrid::new(
                GroundGridConfig::new(DVec3::new(f64::NAN, 0.0, 0.0), 1.0, 1, 1),
                vec![],
            ),
            Err(GroundGridConfigError::InvalidOrigin)
        );
        assert_eq!(
            GroundGrid::new(
                GroundGridConfig::new(DVec3::ZERO, 0.0, 1, 1),
                vec![GridCell::Walkable],
            ),
            Err(GroundGridConfigError::InvalidCellSize)
        );
        assert_eq!(
            GroundGrid::new(GroundGridConfig::new(DVec3::ZERO, 1.0, 0, 1), vec![]),
            Err(GroundGridConfigError::InvalidDimensions)
        );
        assert_eq!(
            GroundGrid::new(
                GroundGridConfig::new(DVec3::ZERO, 1.0, usize::MAX, 2),
                vec![],
            ),
            Err(GroundGridConfigError::TooManyCells)
        );
        assert_eq!(
            GroundGrid::new(GroundGridConfig::new(DVec3::ZERO, 1.0, 2, 2), vec![]),
            Err(GroundGridConfigError::CellCountMismatch)
        );
    }

    #[test]
    fn reachable_cells_stop_at_walls_and_the_step_budget() {
        use GridCell::{Blocked, Walkable};
        // A 4×1 corridor with a wall in the third cell.
        let corridor = grid(4, 1, vec![Walkable, Walkable, Blocked, Walkable]);
        let reached = corridor
            .reachable_within(DVec3::new(0.5, 0.0, 0.5), 5)
            .unwrap();
        assert_eq!(
            reached,
            vec![
                (DVec3::new(0.5, 0.0, 0.5), 0),
                (DVec3::new(1.5, 0.0, 0.5), 1)
            ]
        );
        let open = grid(4, 1, vec![Walkable; 4]);
        let reached = open.reachable_within(DVec3::new(0.5, 0.0, 0.5), 2).unwrap();
        assert_eq!(reached.len(), 3, "the budget stops the fill at two steps");
    }

    // The cases below are the engine's own, beyond what EW's suite held.

    /// **Two grids built apart from the same inputs route identically**, and the
    /// route is the one the documented order picks: on an open 3×3 the walk goes
    /// all the way +Z before it turns +X. Swapping the neighbour order would
    /// still find a shortest route, just a different one — which is exactly
    /// the change this is here to catch.
    #[test]
    fn identical_grids_give_identical_routes_that_lean_plus_z_then_plus_x() {
        let a = grid(3, 3, vec![GridCell::Walkable; 9]);
        let b = grid(3, 3, vec![GridCell::Walkable; 9]);
        let start = DVec3::new(0.5, 0.0, 0.5);
        let goal = DVec3::new(2.5, 0.0, 2.5);

        let from_a = a.plan(start, goal).unwrap();
        let from_b = b.plan(start, goal).unwrap();

        assert_eq!(from_a, from_b);
        assert_eq!(
            from_a.waypoints,
            vec![
                DVec3::new(0.5, 0.0, 1.5),
                DVec3::new(0.5, 0.0, 2.5),
                DVec3::new(1.5, 0.0, 2.5),
                DVec3::new(2.5, 0.0, 2.5),
            ]
        );
        assert_eq!(
            a.reachable_within(start, 4).unwrap(),
            b.reachable_within(start, 4).unwrap()
        );
    }

    /// **The flood fill honours its budget exactly**: on an open 5×5 from the
    /// centre, a budget of two reaches the Manhattan diamond of radius two —
    /// every cell in it, each at its Manhattan distance, and nothing outside.
    #[test]
    fn reachable_within_returns_exactly_the_cells_inside_the_budget() {
        let open = grid(5, 5, vec![GridCell::Walkable; 25]);
        let centre = DVec3::new(2.5, 0.0, 2.5);
        let budget = 2;

        let reached = open.reachable_within(centre, budget).unwrap();

        let manhattan = |p: DVec3| ((p.x - centre.x).abs() + (p.z - centre.z).abs()) as usize;
        let inside = (0..5)
            .flat_map(|z: usize| (0..5_usize).map(move |x| (x, z)))
            .filter(|&(x, z)| x.abs_diff(2) + z.abs_diff(2) <= budget)
            .count();
        assert_eq!(reached.len(), inside);
        for &(cell, steps) in &reached {
            assert!(
                steps <= budget,
                "{cell} at {steps} steps is past the budget"
            );
            assert_eq!(steps, manhattan(cell), "{cell} is not at its fewest steps");
        }
        let mut distinct: Vec<_> = reached
            .iter()
            .map(|(cell, _)| (cell.x.to_bits(), cell.z.to_bits()))
            .collect();
        distinct.sort_unstable();
        distinct.dedup();
        assert_eq!(distinct.len(), reached.len(), "a cell was reported twice");

        assert_eq!(
            open.reachable_within(centre, 0).unwrap(),
            vec![(centre, 0)],
            "a zero budget is the start alone"
        );
    }

    /// **The start comes first and the counts never go down**, walls or not.
    #[test]
    fn reachable_within_lists_the_start_first_in_breadth_first_order() {
        use GridCell::{Blocked, Walkable};
        #[rustfmt::skip]
        let cells = vec![
            Walkable, Walkable, Walkable, Walkable,
            Walkable, Blocked,  Blocked,  Walkable,
            Walkable, Walkable, Walkable, Walkable,
        ];
        let grid = grid(4, 3, cells);
        let start = DVec3::new(1.5, 0.0, 0.5);

        let reached = grid.reachable_within(start, usize::MAX).unwrap();

        assert_eq!(reached.first(), Some(&(start, 0)));
        assert!(
            reached.windows(2).all(|pair| pair[0].1 <= pair[1].1),
            "step counts went down: {reached:?}"
        );
        assert_eq!(reached.len(), 10, "every walkable cell is reachable");
    }

    #[test]
    fn a_start_and_goal_in_one_cell_is_an_empty_route() {
        let grid = grid(2, 2, vec![GridCell::Walkable; 4]);

        let route = grid
            .plan(DVec3::new(1.1, 0.0, 1.1), DVec3::new(1.9, 7.0, 1.9))
            .unwrap();

        assert!(route.waypoints.is_empty());
    }

    #[test]
    fn a_blocked_start_or_goal_has_no_route() {
        use GridCell::{Blocked, Walkable};
        let grid = grid(3, 1, vec![Blocked, Walkable, Blocked]);
        let middle = DVec3::new(1.5, 0.0, 0.5);

        assert_eq!(
            grid.plan(DVec3::new(0.5, 0.0, 0.5), middle),
            Err(GroundRouteError::NoRoute)
        );
        assert_eq!(
            grid.plan(middle, DVec3::new(2.5, 0.0, 0.5)),
            Err(GroundRouteError::NoRoute)
        );
        assert_eq!(
            grid.reachable_within(DVec3::new(0.5, 0.0, 0.5), 3),
            Err(GroundRouteError::NoRoute)
        );
    }

    /// A goal walled into its own pocket, with open ground all round the start.
    #[test]
    fn an_enclosed_goal_is_unreachable() {
        use GridCell::{Blocked, Walkable};
        #[rustfmt::skip]
        let cells = vec![
            Walkable, Walkable, Walkable, Walkable, Walkable,
            Walkable, Walkable, Walkable, Blocked,  Walkable,
            Walkable, Walkable, Blocked,  Walkable, Blocked,
            Walkable, Walkable, Walkable, Blocked,  Walkable,
        ];
        let grid = grid(5, 4, cells);

        assert_eq!(
            grid.plan(DVec3::new(0.5, 0.0, 0.5), DVec3::new(3.5, 0.0, 2.5)),
            Err(GroundRouteError::NoRoute)
        );
    }

    #[test]
    fn non_finite_and_overflowing_positions_are_rejected() {
        let grid = GroundGrid::new(
            GroundGridConfig::new(DVec3::new(-1.0, 0.0, -1.0), 0.5, 4, 4),
            vec![GridCell::Walkable; 16],
        )
        .unwrap();
        let inside = DVec3::ZERO;

        for bad in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
            assert_eq!(
                grid.plan(DVec3::new(bad, 0.0, 0.0), inside),
                Err(GroundRouteError::InvalidWorldPosition)
            );
            assert_eq!(
                grid.plan(inside, DVec3::new(0.0, bad, 0.0)),
                Err(GroundRouteError::InvalidWorldPosition)
            );
            assert_eq!(
                grid.reachable_within(DVec3::new(0.0, 0.0, bad), 1),
                Err(GroundRouteError::InvalidWorldPosition)
            );
        }
        // Finite, but its offset from the origin over the cell size is not.
        assert_eq!(
            grid.plan(DVec3::new(f64::MAX, 0.0, 0.0), inside),
            Err(GroundRouteError::OutsideGrid)
        );
    }

    #[test]
    fn every_non_finite_or_non_positive_cell_size_is_rejected() {
        for size in [f64::NAN, f64::INFINITY, -1.0, -0.0] {
            assert_eq!(
                GroundGrid::new(
                    GroundGridConfig::new(DVec3::ZERO, size, 1, 1),
                    vec![GridCell::Walkable],
                ),
                Err(GroundGridConfigError::InvalidCellSize),
                "{size}"
            );
        }
        assert_eq!(
            GroundGrid::new(
                GroundGridConfig::new(DVec3::new(0.0, f64::INFINITY, 0.0), 1.0, 1, 1),
                vec![GridCell::Walkable],
            ),
            Err(GroundGridConfigError::InvalidOrigin)
        );
    }

    /// The cap is inclusive: a grid of exactly [`MAX_GRID_CELL_COUNT`] cells is
    /// accepted and one row more is not.
    #[test]
    fn the_cell_cap_admits_its_own_size_and_nothing_past_it() {
        let width = 1024;
        let height = MAX_GRID_CELL_COUNT / width;
        assert_eq!(width * height, MAX_GRID_CELL_COUNT);

        let at_cap = GroundGrid::new(
            GroundGridConfig::new(DVec3::ZERO, 1.0, width, height),
            vec![GridCell::Walkable; MAX_GRID_CELL_COUNT],
        );
        assert!(at_cap.is_ok());
        assert_eq!(
            GroundGrid::new(
                GroundGridConfig::new(DVec3::ZERO, 1.0, width, height + 1),
                vec![GridCell::Walkable; MAX_GRID_CELL_COUNT + width],
            ),
            Err(GroundGridConfigError::TooManyCells)
        );
    }

    #[test]
    fn cell_size_is_what_the_config_said() {
        let grid = GroundGrid::new(
            GroundGridConfig::new(DVec3::ZERO, 0.25, 2, 2),
            vec![GridCell::Walkable; 4],
        )
        .unwrap();

        assert_eq!(grid.cell_size_m(), 0.25);
    }

    /// The nearest walkable cell to a position on a blocked one: the nearer
    /// centre across X and Z, the lower row then column on a tie, nothing
    /// outside the rings asked for, and the position's own cell when it is
    /// walkable — and a plan from the answer succeeds where one from the
    /// position did not.
    #[test]
    fn nearest_walkable_snaps_a_blocked_position_to_the_closest_walkable_centre() {
        use GridCell::{Blocked as B, Walkable as W};
        // Row z = 0: walkable, blocked, blocked, walkable. Row z = 1 all
        // blocked but its last cell.
        let grid = grid(4, 2, vec![W, B, B, W, B, B, B, W]);

        let near_minus_x = DVec3::new(1.2, 0.0, 0.5);
        assert_eq!(
            grid.plan(near_minus_x, DVec3::new(3.5, 0.0, 0.5)),
            Err(GroundRouteError::NoRoute)
        );
        let snapped = grid.nearest_walkable(near_minus_x, 1).unwrap();
        assert_eq!(snapped, Some(DVec3::new(0.5, 0.0, 0.5)));
        assert_eq!(
            grid.nearest_walkable(DVec3::new(2.8, 0.0, 0.5), 1).unwrap(),
            Some(DVec3::new(3.5, 0.0, 0.5)),
            "the nearer side wins"
        );
        assert!(
            grid.plan(snapped.unwrap(), DVec3::new(0.5, 0.0, 0.5))
                .is_ok(),
            "the answer is a start plan accepts"
        );

        // Only the last column walkable, so (2.5, 1.0) — in the blocked cell
        // (2, 1) — is equally far from both rows' walkable centres.
        let between = DVec3::new(3.5, 0.0, 1.0);
        let grid_tied = self::grid(4, 2, vec![B, B, B, W, B, B, B, W]);
        assert_eq!(
            grid_tied
                .nearest_walkable(DVec3::new(2.5, 0.0, 1.0), 1)
                .unwrap(),
            Some(DVec3::new(3.5, 0.0, 0.5)),
            "a tie goes to the lower row"
        );
        assert_eq!(
            grid_tied.nearest_walkable(between, 1).unwrap(),
            Some(DVec3::new(3.5, 0.0, 1.5)),
            "on the row boundary the position is in the upper row's walkable cell, which wins \
             over the tie-break"
        );

        assert_eq!(
            grid.nearest_walkable(DVec3::new(1.5, 0.0, 1.5), 0).unwrap(),
            None,
            "no rings is the position's own cell alone"
        );
        assert_eq!(
            grid.nearest_walkable(DVec3::new(1.5, 0.0, 1.5), 1).unwrap(),
            Some(DVec3::new(0.5, 0.0, 0.5)),
            "one ring reaches the diagonal"
        );
        assert_eq!(
            grid.nearest_walkable(DVec3::new(3.2, 0.0, 0.5), 0).unwrap(),
            Some(DVec3::new(3.5, 0.0, 0.5)),
            "a walkable cell answers its own centre"
        );
        assert_eq!(
            grid.nearest_walkable(DVec3::new(f64::NAN, 0.0, 0.5), 1),
            Err(GroundRouteError::InvalidWorldPosition)
        );
        assert_eq!(
            grid.nearest_walkable(DVec3::new(9.0, 0.0, 0.5), 1),
            Err(GroundRouteError::OutsideGrid)
        );
    }
}