using System;
namespace RynthCore.Plugin.RynthNav;
///
/// Is the walk blocked? Until 2026-11-27 only a tick that ran counted, and a tick turning in place
/// started the watch again. A walk swinging between headings against a wall (run a moment, turn a
/// moment) never ran in a row, so it was never seen as blocked and stood
/// there until the 30 s progress watchdog stopped the goto. Now every tick the walk drives counts,
/// running or turning: moved less than over with
/// some running in it, or over of turning only, is blocked. A tick that
/// doesn't drive (standing, waiting for a plan) starts the watch again. Host-free: the route tests
/// drive it.
///
internal sealed class StuckWatch
{
/// A window with running in it: this long...
public const long RunWindowMs = 1200;
/// ...or this long turning only (a half turn on the spot takes about a second)...
public const long TurnWindowMs = 3000;
/// ...and moved less than this = blocked (a run covers 4+ u/s).
public const double MoveUnits = 0.6;
private long _winMs;
private double _x, _y;
private bool _drove, _ranInWindow;
/// The walk drove this tick: running, or turning in place ( false).
public void Drove(bool running)
{
_drove = true;
if (running) _ranInWindow = true;
}
public void Reset()
{
_winMs = 0;
_drove = false;
_ranInWindow = false;
}
///
/// Once a walking tick, before steering: true when blocked (a window ended and the character
/// moved less than in it). and
/// describe the window that ended; both 0 when none did.
///
public bool Check(long now, double x, double y, out double moved, out long windowMs)
{
moved = 0;
windowMs = 0;
bool drove = _drove;
_drove = false;
if (!drove || _winMs == 0)
{
Start(now, x, y);
return false;
}
long elapsed = now - _winMs;
if (elapsed < (_ranInWindow ? RunWindowMs : TurnWindowMs)) return false;
windowMs = elapsed;
moved = Math.Sqrt((x - _x) * (x - _x) + (y - _y) * (y - _y));
Start(now, x, y);
return moved < MoveUnits;
}
private void Start(long now, double x, double y)
{
_winMs = now;
_x = x; _y = y;
_ranInWindow = false;
}
}