using System; using RynthCore.Plugin.RynthNav; namespace RynthNav.RouteTests; // Is the walk blocked (StuckWatch)? A walk against a wall that ran a moment and turned a moment, // swinging between headings, never ran 1.2 s in a row, so it was never blocked; it stood until the // 30 s progress watchdog stopped the goto. Turning ticks now count too. internal static partial class Program { private static void RunStuckWatch() { Run(nameof(StuckRunningIntoAWall), StuckRunningIntoAWall); Run(nameof(StuckRunTurnFlipFlop), StuckRunTurnFlipFlop); Run(nameof(StuckTurningOnly), StuckTurningOnly); Run(nameof(StuckNotWhileMoving), StuckNotWhileMoving); Run(nameof(StuckNotWhenNotDriving), StuckNotWhenNotDriving); } private const long StuckTickMs = 30; /// Ticks for : Check, then drive (running or not) as the walk does. True once blocked. private static bool StuckTicks(StuckWatch w, ref long now, long ms, Func running, Func at, bool drive = true) { long end = now + ms; for (; now < end; now += StuckTickMs) { var (x, y) = at(now); if (w.Check(now, x, y, out _, out _)) return true; if (drive) w.Drove(running(now)); } return false; } private static void StuckRunningIntoAWall() { var w = new StuckWatch(); long now = 1_000_000; bool stuck = StuckTicks(w, ref now, 1500, _ => true, _ => (10, 10)); Check(stuck, "running and not moving for 1.2 s is blocked"); } private static void StuckRunTurnFlipFlop() { // Run 300 ms, turn 300 ms, over and over, against a wall: it used to start the watch again at // every turn and was never blocked. var w = new StuckWatch(); long now = 1_000_000, start = now; bool stuck = StuckTicks(w, ref now, 4000, t => (t - start) / 300 % 2 == 0, _ => (10, 10)); Check(stuck, "running and turning by turns against a wall is blocked"); Check(now - start <= StuckWatch.RunWindowMs + 2 * StuckTickMs, $"within a run window ({now - start} ms)"); } private static void StuckTurningOnly() { var w = new StuckWatch(); long now = 1_000_000, start = now; bool early = StuckTicks(w, ref now, 1500, _ => false, _ => (10, 10)); Check(!early, "turning on the spot for 1.5 s is not blocked (a half turn takes about a second)"); bool stuck = StuckTicks(w, ref now, 3000, _ => false, _ => (10, 10)); Check(stuck, "turning on the spot for 3 s is blocked"); Check(now - start >= StuckWatch.TurnWindowMs, $"only after the turn window ({now - start} ms)"); } private static void StuckNotWhileMoving() { var w = new StuckWatch(); long now = 1_000_000, start = now; // 4 u/s, running and turning by turns: always getting somewhere. bool stuck = StuckTicks(w, ref now, 5000, t => (t - start) / 300 % 2 == 0, t => (10 + (t - start) * 0.004, 10)); Check(!stuck, "a walk that moves is never blocked"); } private static void StuckNotWhenNotDriving() { // Standing (waiting for a plan) between drives starts the watch again each time. var w = new StuckWatch(); long now = 1_000_000; bool stuck = false; for (int i = 0; i < 10 && !stuck; i++) { stuck |= StuckTicks(w, ref now, 600, _ => true, _ => (10, 10)); stuck |= StuckTicks(w, ref now, 300, _ => true, _ => (10, 10), drive: false); } Check(!stuck, "short drives with standing between them are not blocked"); w.Reset(); Check(!w.Check(now, 10, 10, out double moved, out long windowMs) && moved == 0 && windowMs == 0, "after a reset: no window"); } }