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
//! Walking a planned route: which waypoint to steer toward, and whether the
//! walk has stalled.
//!
//! Both came from EW, whose AI followed its routes this way before it moved
//! here; both are pure and deterministic, a function of the feet positions and
//! times the caller hands them, with no clock of their own.
//!
//! * [`RouteFollower`]: the next waypoint of a [`GroundRoute`] to steer
//!   toward, and the stop distance to walk to it with. A waypoint short of the
//!   route's last is dropped only once the feet come within
//!   [`FollowDistances::waypoint_reach_m`] of it, so a walk turns where the
//!   route turns — round a doorway's jamb — rather than making straight for a
//!   waypoint past the turn. Dropping every waypoint within the caller's own
//!   stop distance cut such corners into the jamb, and the walker froze there.
//! * [`StallDetector`]: whether a walk has made no headway — got
//!   [`StallDetector::headway_m`] from where it last did — for
//!   [`StallDetector::timeout_s`], counting only time the caller says it was
//!   walking. A per-tick speed floor never fires for an agent jostling against
//!   another, which moves every tick and goes nowhere.
//!
//! Distances here are over the ground, X and Z, ignoring height: feet partway
//! up a stair stand above or below the floor heights a route's waypoints are
//! at, and a waypoint is reached when the body is beside it, not level with it.
//!
//! What a walker does about a stall — back off, re-plan, give the route up, or
//! wait its turn at a doorway — is a game's rule, and stays the game's.

use glam::DVec3;

use crate::grid::GroundRoute;

/// How near the feet come to a waypoint short of a route's last before a
/// [`RouteFollower`] makes for the next: about a route cell, so a walk follows
/// its route round a corner rather than cutting it. EW's value, proven on its
/// generated maps.
pub const WAYPOINT_REACH_M: f64 = 0.4;

/// The stop distance a [`RouteFollower`] walks to a waypoint short of the
/// route's last with: below [`WAYPOINT_REACH_M`], so a walker that stops at it
/// has reached the waypoint. EW's value.
pub const WAYPOINT_STOP_M: f64 = 0.2;

/// How much farther than a walker's stop distance the route's last waypoint
/// counts as reached: a centimetre.
///
/// **A reach equal to the stop distance is a tie the walker can lose.** A
/// walker braking to stop at its stop distance comes to rest a rounding error
/// either side of it, and on the far side the waypoint is never reached: EW
/// saw an AI stand 0.25 m from cover for good, on Linux only, a nanometre past
/// its stop. So the last waypoint is reached within
/// [`FollowDistances::last_reach_m`], this much beyond the stop distance.
///
/// **Small on purpose.** It only has to win the tie, and every caller's arrival
/// moves out by it: the 0.2 m the waypoints short of the last keep would end a
/// pursuit that far short of where its walker stops (EW, 2026-10-29). A
/// centimetre is far above any rounding error and below anything a walker or
/// a cover spot can tell apart.
pub const LAST_REACH_SLACK_M: f64 = 0.01;

/// How far a walk must get from where it last made headway to have made some
/// again: far enough that jostling in place never counts. EW's value.
pub const STALL_HEADWAY_M: f64 = 0.5;

/// How long a walk may make no headway, walking, before a [`StallDetector`]
/// calls it stalled, in seconds. EW's value.
pub const STALL_TIMEOUT_S: f64 = 3.0;

/// The distances a [`RouteFollower`] follows a route by.
///
/// The fields are public and nothing is checked: a reach that is negative or
/// NaN never counts a waypoint reached, so the walk never moves past it.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct FollowDistances {
    /// How near the feet come to a waypoint short of the last before it is
    /// dropped, in metres.
    pub waypoint_reach_m: f64,
    /// The stop distance to walk to a waypoint short of the last with, in
    /// metres; below `waypoint_reach_m`, or a walker that stops at it never
    /// reaches the waypoint.
    pub waypoint_stop_m: f64,
    /// The caller's stop distance for the route's last waypoint, in metres:
    /// what it is walked to with.
    pub last_stop_m: f64,
    /// How near the feet come to the route's last waypoint for the route to
    /// be done, in metres; above `last_stop_m`, or a walker that stops at it
    /// never arrives. See [`LAST_REACH_SLACK_M`].
    ///
    /// **Arrival can come this far out, not only at `last_stop_m`.** A caller
    /// whose spot only works within some tolerance — cover that protects
    /// within so many metres of it — passes a stop distance of that tolerance
    /// less [`LAST_REACH_SLACK_M`], so the route is never done outside it.
    pub last_reach_m: f64,
}

impl FollowDistances {
    /// [`WAYPOINT_REACH_M`] and [`WAYPOINT_STOP_M`] on the way, and
    /// `last_stop_m` at the end, reached [`LAST_REACH_SLACK_M`] beyond it —
    /// so for an end that must be reached within a tolerance, pass the
    /// tolerance less [`LAST_REACH_SLACK_M`]; see
    /// [`last_reach_m`](Self::last_reach_m).
    #[must_use]
    pub const fn new(last_stop_m: f64) -> Self {
        Self {
            waypoint_reach_m: WAYPOINT_REACH_M,
            waypoint_stop_m: WAYPOINT_STOP_M,
            last_stop_m,
            last_reach_m: last_stop_m + LAST_REACH_SLACK_M,
        }
    }

    /// The reach at which waypoint `index` of a route of `count` counts as
    /// reached: [`last_reach_m`](Self::last_reach_m) for the last,
    /// [`waypoint_reach_m`](Self::waypoint_reach_m) for the rest.
    ///
    /// The reach [`RouteFollower`] drops waypoints by, so a forecast of when a
    /// walk arrives somewhere along the route uses the same one per waypoint.
    #[must_use]
    pub fn reach_for(&self, index: usize, count: usize) -> f64 {
        if index + 1 < count {
            self.waypoint_reach_m
        } else {
            self.last_reach_m
        }
    }
}

/// The waypoint a [`RouteFollower`] steers toward now.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Steer {
    /// The waypoint to walk to.
    pub waypoint: DVec3,
    /// The stop distance to walk to it with.
    pub stop_distance_m: f64,
    /// How near the feet must come for it to count as reached — what an
    /// arrival forecast measures to.
    pub reach_m: f64,
    /// Whether it is the route's last waypoint.
    pub is_last: bool,
}

/// A planned route being walked: the waypoints not yet reached, and the
/// distances that decide when one is.
#[derive(Clone, Debug, PartialEq)]
pub struct RouteFollower {
    waypoints: Vec<DVec3>,
    /// The index of the first waypoint not yet reached.
    next: usize,
    distances: FollowDistances,
}

impl RouteFollower {
    /// A follower at the start of `route`, which it walks by `distances`.
    #[must_use]
    pub fn new(route: GroundRoute, distances: FollowDistances) -> Self {
        Self {
            waypoints: route.waypoints,
            next: 0,
            distances,
        }
    }

    /// Drops every waypoint reached from `feet`, in order, and answers the
    /// first one that is not, or `None` once the last is reached — the route
    /// is walked.
    ///
    /// A waypoint is reached when it lies within
    /// [`FollowDistances::reach_for`] of `feet` over the ground. Waypoints are
    /// dropped only from the front, so one the feet passed beside without
    /// reaching still holds the walk until it is reached; and once dropped one
    /// never comes back, whatever `feet` does next.
    pub fn advance(&mut self, feet: DVec3) -> Option<Steer> {
        let count = self.waypoints.len();
        while self.next < count
            && ground_distance(feet, self.waypoints[self.next])
                <= self.distances.reach_for(self.next, count)
        {
            self.next += 1;
        }
        let waypoint = *self.waypoints.get(self.next)?;
        let is_last = self.next + 1 == count;
        Some(Steer {
            waypoint,
            stop_distance_m: if is_last {
                self.distances.last_stop_m
            } else {
                self.distances.waypoint_stop_m
            },
            reach_m: self.distances.reach_for(self.next, count),
            is_last,
        })
    }

    /// The waypoints not yet reached, in order.
    #[must_use]
    pub fn remaining(&self) -> &[DVec3] {
        &self.waypoints[self.next..]
    }

    /// Whether every waypoint has been reached: an empty route is walked from
    /// the start.
    #[must_use]
    pub fn is_finished(&self) -> bool {
        self.next == self.waypoints.len()
    }

    /// The distances the route is followed by.
    #[must_use]
    pub fn distances(&self) -> FollowDistances {
        self.distances
    }

    /// Changes the stop distance for the route's last waypoint, from the next
    /// [`advance`](Self::advance) on, and its reach with it:
    /// [`LAST_REACH_SLACK_M`] beyond the stop, as
    /// [`FollowDistances::new`] sets it.
    ///
    /// For a walker whose stop distance at the end depends on what it is doing
    /// when it gets there — running rather than walking, or making for cover —
    /// and is set again each tick. Raising it can finish the route at the next
    /// `advance` from feet that had not reached the last waypoint before;
    /// waypoints short of the last are reached by
    /// [`waypoint_reach_m`](FollowDistances::waypoint_reach_m) and are not
    /// affected.
    pub fn set_last_stop_m(&mut self, last_stop_m: f64) {
        self.distances.last_stop_m = last_stop_m;
        self.distances.last_reach_m = last_stop_m + LAST_REACH_SLACK_M;
    }
}

/// Whether a walk is making headway, measured from an anchor: where the feet
/// stood when it last made some.
///
/// Each [`tick`](Self::tick) re-anchors at the feet when they are
/// [`headway_m`](Self::headway_m) or farther from the anchor, and also
/// whenever the caller says it is not walking — waiting at a doorway for
/// another to pass, or holding cover — so waiting never counts toward a stall
/// and the clock starts afresh from where the walk resumes. Otherwise the
/// tick's time adds to the time since the last headway, and the walk is
/// stalled once that reaches [`timeout_s`](Self::timeout_s). It stays stalled,
/// tick after tick, until it makes headway, stops walking or is
/// [`reset`](Self::reset).
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct StallDetector {
    /// How far from the anchor the feet must get to have made headway, in
    /// metres.
    pub headway_m: f64,
    /// How long a walk may make no headway before it is stalled, in seconds.
    pub timeout_s: f64,
    anchor: Option<DVec3>,
    since_headway_s: f64,
}

impl Default for StallDetector {
    /// [`STALL_HEADWAY_M`] and [`STALL_TIMEOUT_S`].
    fn default() -> Self {
        Self::new(STALL_HEADWAY_M, STALL_TIMEOUT_S)
    }
}

impl StallDetector {
    /// A detector with no anchor yet, so its first tick anchors.
    #[must_use]
    pub const fn new(headway_m: f64, timeout_s: f64) -> Self {
        Self {
            headway_m,
            timeout_s,
            anchor: None,
            since_headway_s: 0.0,
        }
    }

    /// Measures one tick of `dt_s` seconds that ended with the feet at
    /// `feet`, while the caller was `walking` its route or not, and answers
    /// whether the walk is stalled.
    ///
    /// A `dt_s` that is zero, negative or not finite is no time passed — a
    /// preview of the walk, not a step of it — and changes nothing.
    pub fn tick(&mut self, feet: DVec3, walking: bool, dt_s: f64) -> bool {
        if !(dt_s.is_finite() && dt_s > 0.0) {
            return self.is_stalled();
        }
        let headway = self
            .anchor
            .is_none_or(|anchor| ground_distance(anchor, feet) >= self.headway_m);
        if !walking || headway {
            self.anchor = Some(feet);
            self.since_headway_s = 0.0;
        } else {
            self.since_headway_s += dt_s;
        }
        self.is_stalled()
    }

    /// Whether the walk has made no headway, walking, for the timeout.
    #[must_use]
    pub fn is_stalled(&self) -> bool {
        self.since_headway_s >= self.timeout_s
    }

    /// Where the feet stood when the walk last made headway, or stopped
    /// walking; `None` before the first tick.
    #[must_use]
    pub fn anchor(&self) -> Option<DVec3> {
        self.anchor
    }

    /// How long the walk has made no headway, walking, in seconds.
    #[must_use]
    pub fn since_headway_s(&self) -> f64 {
        self.since_headway_s
    }

    /// Forgets the anchor and the time, as for a new route: the next tick
    /// anchors afresh.
    pub fn reset(&mut self) {
        self.anchor = None;
        self.since_headway_s = 0.0;
    }
}

/// The distance from `a` to `b` over the ground, ignoring height.
fn ground_distance(a: DVec3, b: DVec3) -> f64 {
    (b.x - a.x).hypot(b.z - a.z)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::grid::{GridCell, GroundGrid, GroundGridConfig};

    const CELL_M: f64 = 0.5;

    /// A 5 × 5 field of half-metre cells with a wall along row 2 and a doorway
    /// through it at column 2.
    fn walled_field() -> GroundGrid {
        let mut cells = vec![GridCell::Walkable; 25];
        for column in [0, 1, 3, 4] {
            cells[2 * 5 + column] = GridCell::Blocked;
        }
        GroundGrid::new(GroundGridConfig::new(DVec3::ZERO, CELL_M, 5, 5), cells).unwrap()
    }

    fn centre(column: usize, row: usize) -> DVec3 {
        DVec3::new(
            (column as f64 + 0.5) * CELL_M,
            0.0,
            (row as f64 + 0.5) * CELL_M,
        )
    }

    /// Whether the straight walk from `a` to `b` crosses a blocked cell.
    fn crosses_blocked(grid: &GroundGrid, a: DVec3, b: DVec3) -> bool {
        (0..=200).any(|step| {
            let point = a.lerp(b, f64::from(step) / 200.0);
            grid.plan(point, point).is_err()
        })
    }

    /// **The corner waypoint holds the walk until the reach, not the stop
    /// distance.** EW's motivating case: an L-shaped route along a wall and
    /// through its doorway, walked with a 1 m stop distance. Dropping every
    /// waypoint within that stop distance makes straight for a waypoint past
    /// the doorway, across the jamb; the follower makes for the corner, and
    /// drops it only within [`WAYPOINT_REACH_M`].
    #[test]
    fn an_l_shaped_route_through_a_doorway_turns_at_its_corner() {
        let grid = walled_field();
        let route = grid.plan(centre(0, 1), centre(2, 4)).unwrap();
        let corner = centre(2, 1);
        assert_eq!(
            route.waypoints,
            vec![
                centre(1, 1),
                corner,
                centre(2, 2),
                centre(2, 3),
                centre(2, 4)
            ],
            "along the wall, then through the doorway"
        );
        let stop = 1.0;
        let mut follower = RouteFollower::new(route.clone(), FollowDistances::new(stop));

        // In cell (1, 1), short of the wall: the corner is within the stop
        // distance and past the reach.
        let feet = DVec3::new(0.75, 0.0, 0.9);
        let to_corner = ground_distance(feet, corner);
        assert!(to_corner < stop && to_corner > WAYPOINT_REACH_M);
        let naive = route
            .waypoints
            .iter()
            .copied()
            .find(|&point| ground_distance(feet, point) > stop)
            .unwrap();
        assert!(
            crosses_blocked(&grid, feet, naive),
            "dropping by the stop distance makes for {naive} across the jamb"
        );

        let steer = follower.advance(feet).unwrap();
        assert_eq!(
            steer,
            Steer {
                waypoint: corner,
                stop_distance_m: WAYPOINT_STOP_M,
                reach_m: WAYPOINT_REACH_M,
                is_last: false,
            }
        );
        assert!(!crosses_blocked(&grid, feet, steer.waypoint));
        assert_eq!(follower.remaining(), &route.waypoints[1..]);

        // Just outside the reach the corner still holds; just inside, the
        // doorway is next.
        let outside = corner - DVec3::X * (WAYPOINT_REACH_M + 0.01);
        assert_eq!(follower.advance(outside).unwrap().waypoint, corner);
        let inside = corner - DVec3::X * (WAYPOINT_REACH_M - 0.01);
        let steer = follower.advance(inside).unwrap();
        assert_eq!(steer.waypoint, centre(2, 2));
        assert!(!crosses_blocked(&grid, inside, steer.waypoint));
    }

    /// **The last waypoint is walked to with the caller's stop distance, and
    /// reached [`LAST_REACH_SLACK_M`] beyond it.**
    #[test]
    fn the_last_waypoint_uses_the_callers_stop_distance() {
        let stop = 1.0;
        let first = DVec3::new(1.0, 0.0, 0.0);
        let last = DVec3::new(1.0, 0.0, 3.0);
        let mut follower = RouteFollower::new(
            GroundRoute {
                waypoints: vec![first, last],
            },
            FollowDistances::new(stop),
        );
        assert!(!follower.is_finished());

        let steer = follower.advance(first).unwrap();
        assert_eq!(
            steer,
            Steer {
                waypoint: last,
                stop_distance_m: stop,
                reach_m: stop + LAST_REACH_SLACK_M,
                is_last: true,
            }
        );
        // Past the waypoint reach, inside the stop distance: arrived.
        let short = last - DVec3::Z * (stop * 0.9);
        assert!(ground_distance(short, last) > WAYPOINT_REACH_M);
        assert_eq!(follower.advance(short), None);
        assert!(follower.is_finished());
        assert!(follower.remaining().is_empty());
        assert_eq!(
            follower.advance(DVec3::ZERO),
            None,
            "a dropped waypoint never comes back"
        );
    }

    /// **A stop distance changed mid-route takes effect at the next
    /// `advance`**, for the last waypoint only: raised, feet that were short of
    /// the end have arrived; lowered, they have not, and a waypoint short of the
    /// last still drops by the waypoint reach.
    #[test]
    fn a_last_stop_changed_mid_route_applies_from_the_next_advance() {
        let first = DVec3::new(1.0, 0.0, 0.0);
        let last = DVec3::new(1.0, 0.0, 3.0);
        let mut follower = RouteFollower::new(
            GroundRoute {
                waypoints: vec![first, last],
            },
            FollowDistances::new(0.5),
        );
        let feet = last - DVec3::Z * 1.5;

        follower.set_last_stop_m(0.25);
        let near_first = first + DVec3::Z * (WAYPOINT_REACH_M * 0.5);
        assert_eq!(
            follower.advance(near_first).unwrap().waypoint,
            last,
            "the first waypoint still drops by the waypoint reach"
        );
        let steer = follower.advance(feet).unwrap();
        assert_eq!(
            (steer.stop_distance_m, steer.reach_m),
            (0.25, 0.25 + LAST_REACH_SLACK_M)
        );

        follower.set_last_stop_m(2.0);
        assert_eq!(follower.distances().last_stop_m, 2.0);
        assert_eq!(follower.advance(feet), None, "1.5 m off is inside 2 m");
        assert!(follower.is_finished());
    }

    /// **Distances are over the ground**: feet a stair's height off a
    /// waypoint, beside it, have reached it.
    #[test]
    fn a_waypoint_is_reached_beside_it_whatever_the_height() {
        let below = DVec3::new(0.0, 0.0, 0.0);
        let next = DVec3::new(2.0, 0.0, 0.0);
        let mut follower = RouteFollower::new(
            GroundRoute {
                waypoints: vec![below + DVec3::Y * 2.0, next],
            },
            FollowDistances::new(0.5),
        );
        assert_eq!(follower.advance(below).unwrap().waypoint, next);
    }

    #[test]
    fn an_empty_route_is_walked_from_the_start() {
        let mut follower = RouteFollower::new(
            GroundRoute {
                waypoints: Vec::new(),
            },
            FollowDistances::new(1.0),
        );
        assert!(follower.is_finished());
        assert_eq!(follower.advance(DVec3::ZERO), None);
    }

    /// **A waypoint exactly at the reach is reached**, as in EW, and one the
    /// next `f64` farther out is not; for a waypoint on the way and for the
    /// last. Each waypoint lies along `x` from feet at the origin, so the
    /// ground distance is the reach to the bit.
    #[test]
    fn a_waypoint_exactly_at_the_reach_is_reached() {
        let distances = FollowDistances::new(1.0);
        let follower_to =
            |waypoints: Vec<DVec3>| RouteFollower::new(GroundRoute { waypoints }, distances);
        let far = DVec3::new(10.0, 0.0, 0.0);
        for (reach, last) in [
            (distances.waypoint_reach_m, false),
            (distances.last_reach_m, true),
        ] {
            let at = DVec3::X * reach;
            assert_eq!(ground_distance(DVec3::ZERO, at), reach);
            let beyond = DVec3::X * reach.next_up();
            let route = |waypoint| {
                if last {
                    vec![waypoint]
                } else {
                    vec![waypoint, far]
                }
            };

            let reached = follower_to(route(at)).advance(DVec3::ZERO);
            assert_eq!(
                reached.map(|steer| steer.waypoint),
                (!last).then_some(far),
                "at exactly the reach ({reach} m) the waypoint is reached; last: {last}"
            );
            let held = follower_to(route(beyond)).advance(DVec3::ZERO);
            assert_eq!(
                held.map(|steer| steer.waypoint),
                Some(beyond),
                "one ulp past the reach the waypoint still holds; last: {last}"
            );
        }
    }

    /// The reach a forecast reads is the one the follower drops by: the
    /// waypoint reach on the way, the last reach at the end.
    #[test]
    fn the_reach_per_waypoint_is_the_waypoint_reach_until_the_last() {
        let distances = FollowDistances::new(1.5);
        assert_eq!(distances.reach_for(0, 3), WAYPOINT_REACH_M);
        assert_eq!(distances.reach_for(1, 3), WAYPOINT_REACH_M);
        assert_eq!(distances.reach_for(2, 3), 1.5 + LAST_REACH_SLACK_M);
        assert_eq!(distances.reach_for(0, 1), 1.5 + LAST_REACH_SLACK_M);
        assert_eq!(
            distances,
            FollowDistances {
                waypoint_reach_m: 0.4,
                waypoint_stop_m: 0.2,
                last_stop_m: 1.5,
                last_reach_m: 1.51,
            },
            "EW's values"
        );
    }

    /// **A walker that comes to rest just past its stop distance has
    /// arrived.** EW's frozen AI: braking to stop 0.25 m from cover, it rested
    /// a rounding error farther out, and with the reach equal to the stop
    /// distance the route was never done. Both the stop distance a walker is
    /// handed and the reach that ends the route are asserted, since the fix is
    /// that the two differ.
    #[test]
    fn a_walker_at_rest_just_past_its_stop_distance_has_arrived() {
        const COVER_STOP_M: f64 = 0.25;
        let cover = DVec3::new(3.0, 0.0, 0.0);
        let route = GroundRoute {
            waypoints: vec![cover],
        };
        let mut follower = RouteFollower::new(route, FollowDistances::new(COVER_STOP_M));

        let walking = follower
            .advance(DVec3::ZERO)
            .expect("three metres out is not there yet");
        assert_eq!(walking.stop_distance_m, COVER_STOP_M);
        assert!(walking.reach_m > walking.stop_distance_m, "{walking:?}");

        let at_rest = cover - DVec3::X * (COVER_STOP_M + 1e-9);
        assert_eq!(follower.advance(at_rest), None, "arrived at cover");
        assert!(follower.is_finished());

        // And `set_last_stop_m` keeps the slack, for a walker that sets it
        // again each tick.
        let mut retargeted = RouteFollower::new(
            GroundRoute {
                waypoints: vec![cover],
            },
            FollowDistances::new(2.0),
        );
        retargeted.set_last_stop_m(COVER_STOP_M);
        assert!(
            retargeted.advance(cover - DVec3::X).is_some(),
            "a metre out is past the lowered reach, not inside the old one"
        );
        assert_eq!(retargeted.advance(at_rest), None);
    }

    const TICK_S: f64 = 0.5;

    /// Ticks an anchored `detector` with `feet` and `walking` until just
    /// short of the default timeout, asserting no stall, then once more,
    /// answering it.
    fn tick_through_timeout(
        detector: &mut StallDetector,
        mut feet: impl FnMut(usize) -> DVec3,
        walking: bool,
    ) -> bool {
        let ticks = (STALL_TIMEOUT_S / TICK_S) as usize - 1;
        for tick in 0..ticks {
            assert!(
                !detector.tick(feet(tick), walking, TICK_S),
                "stalled at tick {tick}"
            );
        }
        detector.tick(feet(ticks), walking, TICK_S)
    }

    /// **Jostling in place while walking stalls after the timeout**: the feet
    /// move every tick, a tenth of the headway back and forth, and never get
    /// the headway from the anchor.
    #[test]
    fn jostling_in_place_while_walking_stalls_after_the_timeout() {
        let mut detector = StallDetector::default();
        assert!(
            !detector.tick(DVec3::ZERO, true, TICK_S),
            "the first anchors"
        );
        let jostle = |tick: usize| DVec3::X * (STALL_HEADWAY_M * 0.1 * (tick % 2) as f64);
        assert!(tick_through_timeout(&mut detector, jostle, true));
        assert_eq!(detector.since_headway_s(), STALL_TIMEOUT_S);
        assert_eq!(detector.anchor(), Some(DVec3::ZERO));
        assert!(detector.tick(DVec3::ZERO, true, TICK_S), "it stays stalled");

        detector.reset();
        assert_eq!(detector.anchor(), None);
        assert!(!detector.tick(DVec3::ZERO, true, TICK_S));
    }

    /// **Waiting never stalls**, however long, and walking on afterward counts
    /// from where it resumed.
    #[test]
    fn waiting_never_stalls() {
        let mut detector = StallDetector::default();
        for _ in 0..100 {
            assert!(!detector.tick(DVec3::ZERO, false, TICK_S));
        }
        assert_eq!(detector.since_headway_s(), 0.0);
        let resumed = DVec3::new(0.1, 0.0, 0.0);
        assert!(!detector.tick(resumed, false, TICK_S));
        assert!(tick_through_timeout(&mut detector, |_| resumed, true));
    }

    /// **Steady progress never stalls**, and each headway re-anchors.
    #[test]
    fn steady_progress_never_stalls() {
        let mut detector = StallDetector::default();
        // A slow walk: a fifth of the headway a tick.
        let step = STALL_HEADWAY_M * 0.2;
        for tick in 0..100 {
            let feet = DVec3::Z * (step * f64::from(tick));
            assert!(!detector.tick(feet, true, TICK_S), "stalled at {feet}");
        }
        let anchor = detector.anchor().unwrap();
        assert!(anchor.z > 99.0 * step - STALL_HEADWAY_M, "{anchor}");
    }

    /// **Headway re-anchors and restarts the clock**: a walk that gets the
    /// headway just before the timeout stalls a whole timeout later, measured
    /// from the new anchor.
    #[test]
    fn headway_re_anchors_and_restarts_the_clock() {
        let mut detector = StallDetector::default();
        let ticks = (STALL_TIMEOUT_S / TICK_S) as usize;
        for _ in 0..ticks {
            assert!(!detector.tick(DVec3::ZERO, true, TICK_S));
        }
        let ahead = DVec3::new(0.0, 5.0, STALL_HEADWAY_M);
        assert!(!detector.tick(ahead, true, TICK_S));
        assert_eq!(detector.anchor(), Some(ahead));
        assert_eq!(detector.since_headway_s(), 0.0);

        // Short of the headway from the new anchor, though past it from the
        // old one, and climbing doesn't count.
        let near = ahead + DVec3::new(0.0, -5.0, STALL_HEADWAY_M * 0.9);
        assert!(tick_through_timeout(&mut detector, |_| near, true));
        assert_eq!(detector.anchor(), Some(ahead));
    }

    /// A tick with no time passed — a preview — changes nothing.
    #[test]
    fn a_tick_with_no_time_changes_nothing() {
        let mut detector = StallDetector::default();
        assert!(!detector.tick(DVec3::ZERO, true, TICK_S));
        let before = detector;
        for dt in [0.0, -1.0, f64::NAN, f64::INFINITY] {
            assert!(!detector.tick(DVec3::X * 9.0, false, dt), "{dt}");
            assert_eq!(detector, before, "{dt}");
        }
        assert!(tick_through_timeout(&mut detector, |_| DVec3::ZERO, true));
    }
}