72 lines
2.6 KiB
C#
72 lines
2.6 KiB
C#
using System;
|
|
|
|
namespace RynthCore.Plugin.RynthNav;
|
|
|
|
/// <summary>
|
|
/// 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 <see cref="RunWindowMs"/> 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 <see cref="MoveUnits"/> over <see cref="RunWindowMs"/> with
|
|
/// some running in it, or over <see cref="TurnWindowMs"/> 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.
|
|
/// </summary>
|
|
internal sealed class StuckWatch
|
|
{
|
|
/// <summary>A window with running in it: this long...</summary>
|
|
public const long RunWindowMs = 1200;
|
|
/// <summary>...or this long turning only (a half turn on the spot takes about a second)...</summary>
|
|
public const long TurnWindowMs = 3000;
|
|
/// <summary>...and moved less than this = blocked (a run covers 4+ u/s).</summary>
|
|
public const double MoveUnits = 0.6;
|
|
|
|
private long _winMs;
|
|
private double _x, _y;
|
|
private bool _drove, _ranInWindow;
|
|
|
|
/// <summary>The walk drove this tick: running, or turning in place (<paramref name="running"/> false).</summary>
|
|
public void Drove(bool running)
|
|
{
|
|
_drove = true;
|
|
if (running) _ranInWindow = true;
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
_winMs = 0;
|
|
_drove = false;
|
|
_ranInWindow = false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Once a walking tick, before steering: true when blocked (a window ended and the character
|
|
/// moved less than <see cref="MoveUnits"/> in it). <paramref name="moved"/> and
|
|
/// <paramref name="windowMs"/> describe the window that ended; both 0 when none did.
|
|
/// </summary>
|
|
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;
|
|
}
|
|
}
|