using System;
namespace RynthCore.Plugin.RynthNav;
///
/// Is the walk stuck (2026-11-07)? Until now only a tick that ran counted: a tick turning in place
/// started the watch again. On 11-07 at Yaraq the walk swung between headings 58, 113, 338 and 119,
/// running a moment and turning a moment, for three seconds against a wall, and the watch never saw
/// 1.2 s of running in a row; then it stood for 25 s and the 30 s progress watchdog stopped the goto.
/// Now every tick the walk wants to move counts (running or turning): moved less than
/// over with some running in it, or over
/// of turning only, is stuck. A tick that doesn't want to move (opening a
/// door, 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 = stuck (a run covers 4+ u/s).
public const double MoveUnits = 0.6;
/// No drive asked for this long: the walk isn't trying to move.
public const long WantGapMs = 150;
private long _winMs;
private double _x, _y;
private bool _ranInWindow;
private long _lastWantMs = long.MinValue / 2;
/// The walk drove this tick: running, or turning in place.
public void Wanted(long now, bool running)
{
_lastWantMs = now;
if (running) _ranInWindow = true;
}
public void Reset()
{
_winMs = 0;
_ranInWindow = false;
}
///
/// Once a walking tick, before steering: true when stuck (the window is over and the character moved
/// less than in it). is how far it went in the window and
/// how long the window was, both 0 while no window has ended this tick.
///
public bool Check(long now, double x, double y, out double moved, out long windowMs)
{
moved = 0;
windowMs = 0;
bool wanted = now - _lastWantMs <= WantGapMs;
if (!wanted || _winMs == 0)
{
_winMs = now; _x = x; _y = y; _ranInWindow = false;
return false;
}
long need = _ranInWindow ? RunWindowMs : TurnWindowMs;
long elapsed = now - _winMs;
if (elapsed < need) return false;
windowMs = elapsed; // only when a window ended
moved = Math.Sqrt((x - _x) * (x - _x) + (y - _y) * (y - _y));
_winMs = now; _x = x; _y = y; _ranInWindow = false;
return moved < MoveUnits;
}
}
///
/// The running jump at a spot the walk is stuck at (2026-11-07). It used to be one tap of the jump
/// key: a tap is the smallest hop AC has (jump height is how long the key is held), and in every
/// log the character had moved 0.0 u 1.2 s after it. Now it charges like a held spacebar
/// (: a medium jump, then a full one), released with forward and run held so it
/// carries the run's momentum (the engine's LaunchJumpWithMotion, as RynthAi's /mt jump does; full
/// jumps only, three of them, when jumping ), then
/// in the air before the stuck watch starts again. Only after both jumps at
/// one spot does the walk step round it. Host-free: the route tests drive it.
///
internal sealed class WallJump
{
/// The charge of each try at one spot (ms; 1000 is a full bar): a strong jump, then a full one.
public static readonly int[] ChargeMs = { 700, 1000 };
///
/// Jumping hard (2026-11-07, "it should use more power to jump if needed"): where the route graph has no
/// walking way to a spot on the same land (a wall or a ledge the navmesh doesn't cross), every jump is a
/// full one, three at each spot.
///
public static readonly int[] HardChargeMs = { 1000, 1000, 1000 };
/// Jumping hard: instead of .
public bool Hard { get; set; }
private int[]? _spot;
/// The charges tried at one spot, in order: this spot's own plan (), else the default.
public int[] Plan => _spot ?? (Hard ? HardChargeMs : ChargeMs);
///
/// This spot's jumps, worked out from the character's jump and what is in the way (JumpModel.PlanFor):
/// empty when no jump clears it (too high, too burdened), so the walk steps round at once.
///
public void SetSpot(int[] plan) { _spot = plan; }
/// Time in the air before the walk looks again.
public const long FlightMs = 900;
public enum Phase { Idle, Charging, Flying }
public enum Step { Hold, Release, Landed }
private long _releaseAt, _landAt;
public Phase Now { get; private set; } = Phase.Idle;
/// Jumps tried at this spot.
public int Tries { get; private set; }
public bool Busy => Now != Phase.Idle;
public bool CanTry => Tries < Plan.Length;
/// The charge of the jump under way (0 for a tap).
public int Charge { get; private set; }
/// A charged jump: the jump key is down from now. Returns the charge (ms).
public int StartCharged(long now)
{
Charge = Plan[Math.Min(Tries, Plan.Length - 1)];
Tries++;
Now = Phase.Charging;
_releaseAt = now + Charge;
return Charge;
}
/// A tap (no charged jump in this engine): already in the air.
public void StartTap(long now)
{
Charge = 0;
Tries++;
Now = Phase.Flying;
_landAt = now + FlightMs;
}
/// Each tick while : Release once (let go of the key), then Landed once.
public Step Tick(long now)
{
if (Now == Phase.Charging && now >= _releaseAt)
{
Now = Phase.Flying;
_landAt = now + FlightMs;
return Step.Release;
}
if (Now == Phase.Flying && now >= _landAt)
{
Now = Phase.Idle;
return Step.Landed;
}
return Step.Hold;
}
/// It moved on: the next stuck spot gets its jumps again.
public void Moved() { if (!Busy) { Tries = 0; _spot = null; } }
public void Reset()
{
Now = Phase.Idle;
Tries = 0;
Charge = 0;
_spot = null;
}
}
///
/// How high this character jumps right now (2026-11-07, Silentkelpie: "the jump on routes should take your
/// buffed jump into account as well as your burden"). AC's jump, as the server works it out (ACE's
/// MovementSystem.GetJumpHeight): height = burden factor x (Jump / (Jump + 1300) x 22.2 + 0.05) x power, at
/// least 0.35 m, power being how full the bar is (held 0 to 1 s). The burden factor is 1 up to 100% burden,
/// falls to 0 at 200%, where no jump leaves the ground. Jump is the buffed skill. Burden is what you carry over
/// what your Strength carries (150 per point, 30 more per point for each carrying augmentation).
/// Host-free: the route tests drive it.
///
internal sealed class JumpModel
{
/// The Jump skill's id.
public const uint JumpSkillId = 22;
/// Feet over the top of what is jumped (m).
public const double Clearance = 0.35;
/// The navmesh climbs 1 m (RynthNav.Baker agentMaxClimb): what stops the walk is higher.
public const double MeshClimb = 1.0;
/// The least charge sent (a shorter press is a tap).
public const int MinChargeMs = 200;
/// The buffed Jump skill (0: not known).
public int Skill { get; }
/// Burden over capacity (1.0 = 100%; NaN: not known).
public double Burden { get; }
public JumpModel(int skill, double burden) { Skill = Math.Max(0, skill); Burden = burden; }
public bool Known => Skill > 0;
public bool BurdenKnown => !double.IsNaN(Burden);
/// Too burdened to jump at all (200% or more).
public bool TooBurdened => BurdenKnown && Burden >= 2.0;
public static double BurdenFactor(double burden) =>
double.IsNaN(burden) || burden < 1.0 ? 1.0 : burden < 2.0 ? 2.0 - burden : 0.0;
/// The jump's height at (0..1), in metres.
public double Height(double power)
{
double p = Math.Clamp(power, 0, 1);
double h = BurdenFactor(Burden) * (Skill / (Skill + 1300.0) * 22.2 + 0.05) * p;
return Math.Max(h, 0.35);
}
/// A full jump's height.
public double FullHeight => Height(1.0);
/// The bar to fill (ms) for feet over something m high; 0 = none does.
public int ChargeFor(double top)
{
double need = top + Clearance;
double full = BurdenFactor(Burden) * (Skill / (Skill + 1300.0) * 22.2 + 0.05);
if (TooBurdened || full <= 0 || need > full) return 0;
return Math.Clamp((int)Math.Ceiling(need / full * 1000.0 / 50.0) * 50, MinChargeMs, 1000);
}
///
/// The jumps for one stuck spot. : how high the thing in the way is (NaN: not known,
/// a live object or no geometry). Known and jumpable: just enough, then a full one. Too high or too
/// burdened: none (step round at once). Not known: from the mesh's climb up, then full ones.
///
public int[] PlanFor(double top, bool hard, out string why)
{
if (TooBurdened) { why = $"too burdened to jump ({Burden * 100:F0}%)"; return Array.Empty(); }
if (!double.IsNaN(top))
{
int c = ChargeFor(top);
if (c == 0) { why = $"{top:F1} m high; your jump clears about {FullHeight - Clearance:F1} m (Jump {Skill}{BurdenNote})"; return Array.Empty(); }
why = $"{top:F1} m high; a {c} ms jump clears it (Jump {Skill}{BurdenNote})";
return c >= 1000 ? new[] { 1000, 1000 } : new[] { c, 1000 };
}
int first = Math.Max(ChargeFor(MeshClimb + 0.6), 700);
if (first == 0 || hard) first = 1000;
why = $"height not known; from {first} ms (Jump {Skill}{BurdenNote}: a full jump clears about {FullHeight - Clearance:F1} m)";
return hard ? new[] { 1000, 1000, 1000 } : new[] { first, 1000 };
}
private string BurdenNote => BurdenKnown ? $", burden {Burden * 100:F0}%" : "";
}