using System;
namespace RynthCore.Plugin.RynthAi;
///
/// Stuck-state backstop for the engine's CMotionInterp cast-gesture gate
/// (_host.CanCastNow). That gate reads [CMI+0x80]; if it ever
/// stays non-zero indefinitely — a server-refused "You're too busy!" can leave
/// a one-shot motion node that never pops, and the +0x80 offset was never 100%
/// pinned offline — BOTH the buff (BuffManager.OnHeartbeat) and combat
/// (CombatManager magic) paths hard-return on !CanCastNow every
/// tick and the bot freezes standing in place. The engine has no self-recovery
/// for this and there is no BusyCount-style watchdog.
///
/// Logic: pass the gate through untouched while it behaves; if it has been
/// continuously closed longer than any legitimate cast wind-up+release, declare
/// it stuck and report "clear", then keep reporting clear until the engine gate
/// genuinely recovers (raw == true again). When overriding we are exactly in
/// the documented "engine without the gate" mode (CanCastNow defaults true) —
/// the SpellCastIntervalMs throttle and the "too busy" PARK path remain
/// the anti-spam guarantees, so this cannot reintroduce the 0x00460D1D
/// refusal-loop AV.
///
internal static class CastGateWatchdog
{
// Longer than any real AC cast gesture (~1–3s). Continuously closed past
// this ⇒ the gate is stuck, not a legitimate in-progress gesture.
private const double StuckMs = 4000;
private static DateTime _closedSince = DateTime.MinValue;
private static bool _overriding;
///
/// Effective "clear to cast": the raw engine gate plus a stuck backstop.
/// Call with _host.CanCastNow; optionally pass a logger for the
/// one-shot stuck/recovered transitions.
///
public static bool CanCastNow(bool rawCanCastNow, Action? log = null)
{
if (rawCanCastNow)
{
if (_overriding)
{
log?.Invoke("[CastGate] engine gate recovered — stuck override cleared.");
_overriding = false;
}
_closedSince = DateTime.MinValue;
return true;
}
// Raw gate reports a gesture in progress.
if (_overriding)
return true; // already declared stuck; stay open until the engine gate recovers
if (_closedSince == DateTime.MinValue)
_closedSince = RynthClock.Now;
if ((RynthClock.Now - _closedSince).TotalMilliseconds > StuckMs)
{
_overriding = true;
log?.Invoke($"[CastGate] CanCastNow stuck false >{StuckMs:F0}ms — overriding gate open " +
"(SpellCastIntervalMs throttle + 'too busy' PARK remain the anti-spam backstop).");
return true;
}
return false;
}
}