RynthAi: stuck-cast breaker, no re-send into a busy server, recovery retry, HP failsafe

Two casts were held open 40.8 s and 37 s by ACE's fast-tick turn-to-cast
wait; every later send was answered UseDone 0x1D and a heal was re-sent
every 2.5 s.

- ActionPacer: after a 0x1D UseDone nothing starts until the server answers;
  with no cast of ours open, one probe after 1.5 s, then doubling (max 6 s).
  Attack repeats are not held (ACE sends no UseDone for a swing), and a latch
  with no cast of ours open ends after LatchMaxMs (15 s). A cast still
  unanswered 1 s past its normal cycle (learned per spell, at least 3 s) is
  turned to face its target; 2 s past it (capped by Stuck Cast Break, default
  8000 ms) or on a 0x1D while it has been open 3 s, it is broken with a jump
  (TapJump; ACE fails a cast on a fast-tick jump). Starts wait until landed;
  no UseDone 3 s after landing -> Peace mode; 3 s more -> going on. Every
  hold is bounded: a cast StaleCastMs old, or sent before a frame gap of 5 s
  (keyed on the gap's end), is dropped with the latch, never broken. No
  breaker step in portal space; Forget on portal-enter and the death hold.
  A burst of 5 'too busy' answers in a second holds every start except a
  heal. A refused forced jump with no cast open leaves no breaker stage.
  A cast a script chose (the combat hook) is not jumped before 6 s from its
  send (ScriptCastMinBreakMs, the held-cast nudge's HeldAfterIncantMs).
  Stuck Cast Break is read from the settings once per tick.
- MoveAudit: no start in the tick of a movement change; 150 ms and a clear
  motion queue (or 600 ms) let it go. A held stop sent later by
  FlushPendingStops counts as a movement change too; the forced flush in
  CastSpellFor still goes out in the same tick as the cast that follows it.
- Blacklist: casts answered 0x1D (or unanswered while the server is busy) are
  not no-damage casts.
- Combat hook: ctx.pending is the cast in flight; ctx.last is the most recent
  RESOLVED cast, its err final (the first UseDone after its send; nil only
  when none could be read: 7 s timeout, or another answer came first). The
  casts in flight of both kinds resolve in send order through one queue.
  The send-time UseDone count comes from the pacer (LastCastSeqAtSend).
  CombatRequestV1 appends Resolved*/Pending* fields (StructSize-gated, no
  Version bump); an older RynthAi keeps the old ctx.last.
- HP failsafe (BuffManager.HpFailsafe.cs): at or under Emergency Heal At with
  no heal landing for 3 rounds of 2.5 s, a forced jump; 3 more rounds, the
  macro stops and /lifestone goes out (HP Failsafe Recall, default on).
  Nothing counts in portal space; a pending recall is dropped on death; a
  recall that never starts turns the macro back on (only one the failsafe
  stopped, never past the VTank yield) and re-arms after a 30 s cooldown.

Recovery retry: recovery vitals (Heal Self, Stamina to Health, Revitalize,
Stamina to Mana) refused for "not enough mana" retry after Recovery Retry
(200 ms, a setting) instead of resting the family: ACE rolls Mana Conversion
on every attempt before the windup and a refusal costs nothing. At most 5
recovery casts a second, 20 refusals in a row, stamina and Mana Conversion
checks, an episode with no refusal for 7 s ends; an episode summary line.
Stamina to Mana and Revitalize are still held under their cost by the mana
reserve (AttemptVitalCast), so for them the retry applies when the mana
reading is stale or costs are unknown; Heal Self and Stamina to Health,
which are tried below cost, gain the most.
When the retry stops, the vital rests as the existing mana reserve has it
(LowManaHealRestSec, 2 s). Script heals and other casts are unchanged.

Tests: StuckCastTests (incl. the script cast's break floor), PacerEdgeTests,
RecoveryRetryTests (incl. costs known: Stamina to Mana held), HpFailsafeTests,
CombatHookTests (ctx.last/ctx.pending, send order, an answer during the send,
0x1D blacklist, script heal keeps its rest, script attack and heal not jumped
before 6 s with or without a 'too busy' answer); HealLatencyTests' and ManaReserveTests' low-mana rest
cases pin untrained Mana Conversion; RynthLua compat combathook.lua.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Daniel Wallace 2026-10-12 16:23:37 -07:00
commit 2d8bebb0f4
30 changed files with 3156 additions and 53 deletions

View file

@ -34,7 +34,7 @@
// queue (CMotionInterp pending motions, the engine's cast gate: GetCastBusyState) must
// be empty, for uses, kits, potions, casts, stance changes, wields and the item
// managers' moves (GestureHolds). Melee/missile attack repeats are exempt (MayStart
// with gestureGate false). Time limit GestureHoldMaxMs (GestureEmergencyMaxMs for an
// with animationGate false). Time limit GestureHoldMaxMs (GestureEmergencyMaxMs for an
// emergency heal), logged, like Combat/CastGateWatchdog for casts.
//
// Why the animation gate (2026-10-06 afternoon, ops/overnight/2026-10-06-turnto-stall.md,
@ -92,6 +92,8 @@ internal sealed class PacedAction
public long SentMs;
public int SeqAtSend;
public int ModeTarget;
public uint Target; // a cast's target (the held-cast turn faces it)
public bool Script; // a cast a script chose (the combat hook): the breaker waits ScriptCastMinBreakMs
public bool WieldedAtSend;
public bool ResultSeen; // a cast's result is in (enchantment / chat); the server is in its recoil
public bool HealQueued; // the one heal allowed behind that recoil went out
@ -175,13 +177,43 @@ internal static class ActionPacer
_maxWaitWhat = "";
_holdsByWho.Clear();
_lastTimeout = "";
ClearBusyLocked();
_stuckBreaks = 0;
_refusalHolds = 0;
_heldTurns = 0;
_forcedBreaks = 0;
_jumpAt = 0;
_cycleBySpell.Clear();
_lastSeenSeq = int.MinValue;
_lastFrameMs = 0;
_gapEnd = 0;
_gapLen = 0;
_errorAnswers.Clear();
_staleDrops = _latchExpiries = _burstHolds = 0;
}
}
/// <summary>
/// The plugin's heartbeat, every frame whatever the macro does. A gap of PollGapMs or more between
/// two calls (the client froze or loaded) makes a cast sent before the gap ENDED stale at the next
/// check (a cast sent in the tick just before a freeze is sent after that tick's NoteFrame), so
/// a teleport's or a freeze's gap is never mistaken for a stuck cast and broken by a jump. Never
/// called (a plugin without it, the tests that don't): no gap detection, only StaleCastMs.
/// </summary>
public static void NoteFrame()
{
long now = NowMs();
lock (Gate)
{
if (_lastFrameMs != 0 && now - _lastFrameMs >= PollGapMs) { _gapEnd = now; _gapLen = now - _lastFrameMs; }
_lastFrameMs = now;
}
}
/// <summary>Forget the outstanding action (logout, death, portal): the server dropped it.</summary>
public static void Forget(string why)
{
lock (Gate) { _cur = null; }
lock (Gate) { _cur = null; ClearBusyLocked(); }
}
// ── Recording what went out ──────────────────────────────────────────────
@ -209,6 +241,8 @@ internal static class ActionPacer
{
_sentOverOutstanding++;
over = $"[Pace] {who}: {Describe(kind, label)} went out while {Describe(prev.Kind, prev.Label)} ({prev.Who}) was unanswered for {now - prev.SentMs} ms";
// Our cast is still the server's: a 0x1D to this send says it is open (the stuck-cast rules).
if (prev.Kind == PacedKind.Cast && _shadow == null && _hung == null) _shadow = prev;
}
_cur = a;
}
@ -254,6 +288,12 @@ internal static class ActionPacer
if (hasSeq && seq != a.SeqAtSend)
{
answeredHow = "UseDone";
if (a.Kind == PacedKind.Cast)
{
uint err = TooBusyErr;
try { if (host.TryGetLastUseDone(out int ls, out uint le) && ls == a.SeqAtSend + 1) err = le; } catch { }
if (err != TooBusyErr) NoteCycle(a.Id, age);
}
}
else if (a.Kind == PacedKind.ItemUse && IsWieldedByPlayer(host, a.Id) != a.WieldedAtSend)
{
@ -293,6 +333,7 @@ internal static class ActionPacer
{
_timedOut++;
_lastTimeout = $"{Describe(a.Kind, a.Label)} ({a.Who}) after {age} ms";
if (a.Kind == PacedKind.Cast && _hung == null) _hung = a; // still the server's: see the stuck-cast hold
line = $"[Pace] no answer from the server to {Describe(a.Kind, a.Label)} ({a.Who}) in {age} ms - going on (nothing cleared)";
}
else
@ -317,14 +358,31 @@ internal static class ActionPacer
/// May <paramref name="who"/> send a use, a cast or a mode change now? False (with why) while
/// another action waits for the server, except the heal rules in the file header, and while
/// the character is still animating (<see cref="GestureHolds"/>; skipped with
/// <paramref name="gestureGate"/> false: melee/missile attack repeats). One log line per
/// <paramref name="animationGate"/> false: melee/missile attack repeats). One log line per
/// outstanding action (or animation) and subsystem.
/// </summary>
public static bool MayStart(RynthCoreHost host, string who, HealNeed heal, out string why, bool gestureGate = true)
/// <param name="animationGate">False: skip <see cref="GestureHolds"/> (melee/missile attack repeats).</param>
/// <param name="settleGate">False: skip the movement settle (<see cref="MoveAudit.SettleHolds"/>; attack repeats only).</param>
/// <param name="attackRepeat">
/// A melee/missile attack repeat: never held by the 'too busy' latch. ACE sends no UseDone for a
/// swing, so a latch set by anything else (a refused pickup) would otherwise leave combat only the
/// probe slots.
/// </param>
public static bool MayStart(RynthCoreHost host, string who, HealNeed heal, out string why,
bool animationGate = true, bool settleGate = true, bool attackRepeat = false)
{
if (!Enabled) { why = ""; return true; }
if (ServerBusyHolds(host, who, attackRepeat, out why, healStart: heal != HealNeed.None)) return false;
if (settleGate && MoveAudit.SettleHolds(host, out why))
{
bool log;
lock (Gate) { _settleHolds++; log = _settleLogged.Add(who); }
if (log) Log(host, $"[Pace] {who} waits: {why}");
return false;
}
lock (Gate) _settleLogged.Clear();
if (!OutstandingAllows(host, who, heal, out why)) return false;
if (gestureGate && GestureHolds(host, who, heal, out why)) return false;
if (animationGate && GestureHolds(host, who, heal, out why)) return false;
return true;
}
@ -495,8 +553,471 @@ internal static class ActionPacer
return true;
}
// -- A cast the server holds open, and a server that answers "too busy" ----------------
// Two hangs in a client log, both ACE's fast-tick turn-to-cast wait (Player_Magic
// TurnTo_Magic / PendingTurnRelease: the cast waits until the character faces its target within
// spellcast_max_angle, with no time limit): a Bolt VII sent in the same tick as nav's autorun stop
// (answered 40.8 s later) and a Drain at a moving target (its
// incantation 2865 ms after the send, fizzled 37 s later on a client mode change). The server
// kept IsBusy, so every cast after it came back UseDone 0x1D, and the vitals path re-sent a heal
// every 2.5 s. So:
// * a cast that times out unanswered (CastMaxMs) is kept as the server's (hung);
// * a UseDone with 0x1D latches "server busy": nothing starts (MayStart) until a UseDone with
// another error arrives. With no cast of ours open, one probe goes after BusyProbeMs, then
// after twice that each time, up to BusyProbeMaxMs (the busy thing may have no UseDone of its
// own, e.g. a pickup). No timer re-sends into a server busy with our own cast;
// * a cast of ours still unanswered TurnAfterMs past its normal cycle: turn to face its target
// (the server's angle check then lets it finish), once, when RynthAi is not moving us;
// * still unanswered JumpAfterMs past its normal cycle (capped by StuckCastMs), or a 0x1D to
// another send while it is open past RefusalBreakMinMs: the breaker, a jump (0xF61B, TapJump):
// on fast tick ACE's HandleActionJump clears the pending motions and calls FailCast (origin/
// master Player.cs ~1195-1214), a 0x402 fizzle. Nothing starts until the character has landed
// (a cast in the air is refused). No UseDone BreakerWaitMaxMs after landing: a Peace mode
// request (HandleActionChangeCombatMode_Inner also calls FailCast, fast tick or not); then
// BreakerWaitMaxMs more and it goes on (logged).
/// <summary>ACE's YoureTooBusy, as a UseDone error.</summary>
public const uint TooBusyErr = 0x1D;
/// <summary>With no cast of ours open, one start is let through this long after a 0x1D (doubling per probe).</summary>
public const long BusyProbeMs = 1500;
public const long BusyProbeMaxMs = 6000;
/// <summary>A cast's normal send-to-UseDone time when none was seen for its spell, and the floor of a learned one.</summary>
public const long DefaultCycleMs = 3000;
/// <summary>Past its normal cycle: turn to face the target after this, jump after this.</summary>
public const long TurnAfterMs = 1000;
public const long JumpAfterMs = 2000;
/// <summary>A 0x1D to another send breaks our open cast only once it is this old (a recoil refusal is normal).</summary>
public const long RefusalBreakMinMs = 3000;
/// <summary>After the jump: landed once the height is back (and this long passed), or at the max.</summary>
public const long JumpLandMinMs = 400;
public const long JumpLandMaxMs = 1500;
/// <summary>After landing (or after the Peace request), the longest wait for a UseDone before the next step.</summary>
public const long BreakerWaitMaxMs = 3000;
/// <summary>The stuck-cast limit (ms from the send; 0 = no turn, no breaker). RynthAi sets it from its settings.</summary>
public static long StuckCastMs = 8000;
/// <summary>
/// A script-chosen cast is not broken by a jump before this (ms from the send), whatever its
/// cycle, Stuck Cast Break or a 'too busy' answer say: a script's slower spells (a siphon
/// answered about 5.5 s after the send) get 6 s from the send before the first jump; the
/// held-cast nudge, which counts from the incantation, comes later.
/// </summary>
public const long ScriptCastMinBreakMs = 6000;
/// <summary>
/// A cast of ours this old (or one sent before a gap of PollGapMs between NoteFrame calls: a
/// client freeze, a long load) is stale: dropped with the latch, never broken. Bounds every hold,
/// the breaker off (StuckCastMs 0) included.
/// </summary>
public const long StaleCastMs = 3 * (CastMaxMs > BusyProbeMaxMs ? CastMaxMs : BusyProbeMaxMs);
public const long PollGapMs = 5000;
/// <summary>A 'too busy' latch with no cast of ours open is cleared after this long (logged).</summary>
public const long LatchMaxMs = 15000;
/// <summary>'Too busy' UseDones in the last second at which every start but a heal waits (bounds refusal chat lines).</summary>
public const int RefusalBurstMax = 5;
public const int PeaceMode = 1, MagicMode = 8;
private enum BreakStage { None, Turned, Jumped, Landed, ModeSent }
private static PacedAction? _hung; // a cast past CastMaxMs with no UseDone
private static PacedAction? _shadow; // our cast still unanswered when another action went out over it
private static long _busySince; // first 0x1D of the latch (0 = no latch)
private static int _busyRefusals; // 0x1D answers in this latch
private static int _probes;
private static long _lastProbeAt;
private static BreakStage _stage;
private static long _stageAt;
private static PacedAction? _stageFor;
private static long _jumpAt; // 0 = not in the air after a breaker jump
private static float _jumpZ;
private static int _lastSeenSeq = int.MinValue;
private static long _lastFrameMs; // the last NoteFrame (gap detection; 0 = never)
private static long _gapEnd, _gapLen; // the frame that ended the last gap, and its length (0 = none)
private static readonly Queue<long> _errorAnswers = new(); // times of error UseDones (burst limiter)
private static long _staleDrops, _latchExpiries, _burstHolds;
public static long StaleDrops => _staleDrops;
public static long LatchExpiries => _latchExpiries;
public static long BurstHolds => _burstHolds;
private static long _stuckBreaks, _refusalHolds, _heldTurns, _forcedBreaks, _settleHolds;
private static readonly HashSet<string> _settleLogged = new();
public static long SettleHoldCount => _settleHolds;
private static readonly HashSet<string> _busyLogged = new();
private static readonly Dictionary<uint, long> _cycleBySpell = new();
public static long StuckBreaks => _stuckBreaks;
public static long RefusalHolds => _refusalHolds;
public static long HeldCastTurns => _heldTurns;
/// <summary>True while the server is taken as busy (a 0x1D latch or a hung cast).</summary>
public static bool ServerBusy { get { lock (Gate) return _busySince != 0 || _hung != null; } }
/// <summary>True while the character is in the air after a breaker jump.</summary>
public static bool BreakerAirborne { get { lock (Gate) return _jumpAt != 0; } }
private static void ClearBusyLocked()
{
_hung = null;
_shadow = null;
_busySince = 0;
_busyRefusals = 0;
_probes = 0;
_lastProbeAt = 0;
_stage = BreakStage.None;
_stageAt = 0;
_stageFor = null;
_busyLogged.Clear();
}
/// <summary>The normal send-to-UseDone time of <paramref name="spellId"/>: the longest of its last answers, at least DefaultCycleMs.</summary>
internal static long NormalCycleMs(uint spellId)
{
lock (Gate) return _cycleBySpell.TryGetValue(spellId, out long c) ? Math.Max(DefaultCycleMs, c) : DefaultCycleMs;
}
/// <summary>A cast of <paramref name="spellId"/> was answered (not 0x1D) <paramref name="ms"/> after its send.</summary>
private static void NoteCycle(uint spellId, long ms)
{
if (ms <= 0 || ms > CastMaxMs) return;
lock (Gate)
{
// The longest recent answer, easing down: a slow one counts at once, old slow ones fade.
_cycleBySpell[spellId] = _cycleBySpell.TryGetValue(spellId, out long c) ? Math.Max(ms, (c * 7 + ms) / 8) : ms;
}
}
/// <summary>Reads the last UseDone: a 0x1D starts or feeds the latch, any other answer clears it.</summary>
private static void ObserveUseDone(RynthCoreHost host, long now, out string? line)
{
line = null;
int seq; uint err;
try
{
if (!host.HasUseDoneSeq || !host.TryGetLastUseDone(out seq, out err)) return;
}
catch { return; }
lock (Gate)
{
if (_lastSeenSeq == int.MinValue) { _lastSeenSeq = seq; return; }
if (seq == _lastSeenSeq) return;
_lastSeenSeq = seq;
if (err == TooBusyErr) _errorAnswers.Enqueue(now); // the burst counts 'too busy' only (0x401 retries are paced on their own)
if (err == TooBusyErr)
{
if (_busySince == 0) { _busySince = now; if (_hung == null) _busyLogged.Clear(); }
_busyRefusals++;
return;
}
if (_busySince != 0 || _hung != null || _stage != BreakStage.None)
line = $"[Pace] the server answered again (UseDone 0x{err:X}): casting resumes" +
(_hung != null ? $" ({Describe(_hung.Kind, _hung.Label)} ended {now - _hung.SentMs} ms after its send)" : "");
ClearBusyLocked();
}
}
/// <summary>
/// True (with why) while the server is taken as busy (a 0x1D answer with no other answer since,
/// a hung cast) or the character is in the air after a breaker jump. Runs the held-cast turn and
/// the stuck-cast breaker. Called by MayStart.
/// </summary>
private static bool ServerBusyHolds(RynthCoreHost host, string who, bool attackRepeat, out string why, bool healStart = false)
{
why = "";
long now = NowMs();
long gapEnd, gapLen;
lock (Gate) { gapEnd = _gapEnd; gapLen = _gapLen; }
ObserveUseDone(host, now, out string? line);
if (line != null) Log(host, line);
if (AirborneHolds(host, now, out why)) return true;
PacedAction? open = Current(host);
PacedAction? stuck;
bool probe = false;
long age = 0;
string? staleLine = null;
lock (Gate)
{
if (_shadow != null && _hung == null && now - _shadow.SentMs >= CastMaxMs) { _hung = _shadow; _shadow = null; }
stuck = _hung ?? _shadow ?? (open is { Kind: PacedKind.Cast } ? open : null);
if (stuck != null) age = now - stuck.SentMs;
// Stale: too old to still be the server's, or sent before a gap with no checks (a freeze,
// portal space, the macro off). Dropped with the latch, never broken.
bool beforeGap = stuck != null && gapEnd != 0 && stuck.SentMs < gapEnd;
if (stuck != null && (age >= StaleCastMs || beforeGap))
{
staleLine = $"[Pace] {Describe(stuck.Kind, stuck.Label)} ({stuck.Who}) is stale ({age} ms" +
(beforeGap ? $", sent before a {gapLen} ms gap with no check" : "") + "): dropped, the server is taken as free";
if (ReferenceEquals(_cur, stuck)) _cur = null;
ClearBusyLocked();
_staleDrops++;
stuck = null;
}
// A 'too busy' latch with no cast of ours open: bounded, not probed for ever.
else if (stuck == null && _busySince != 0 && now - _busySince >= LatchMaxMs && _stage == BreakStage.None)
{
staleLine = $"[Pace] no answer cleared the 'too busy' latch in {now - _busySince} ms and no cast of ours is open: going on";
ClearBusyLocked();
_latchExpiries++;
}
if (_stageFor != null && !ReferenceEquals(_stageFor, stuck)) { _stage = BreakStage.None; _stageAt = 0; _stageFor = null; }
// A forced break with no cast of ours open (stage for nothing): once on the ground, it is
// over. Left set it would block the latch expiry and the probe for good.
else if (_stageFor == null && _stage != BreakStage.None && _jumpAt == 0) { _stage = BreakStage.None; _stageAt = 0; }
if (!attackRepeat && stuck == null && _busySince != 0 && _stage == BreakStage.None
&& now - Math.Max(_busySince, _lastProbeAt) >= Math.Min(BusyProbeMs << Math.Min(_probes, 8), BusyProbeMaxMs))
{
probe = true;
_probes++;
_lastProbeAt = now;
}
}
if (staleLine != null) Log(host, staleLine);
if (stuck != null && !IsPortaling(host)) StepBreaker(host, stuck, age, now);
if (AirborneHolds(host, now, out why)) return true;
// Any source: RefusalBurstMax 'too busy' answers in the last second hold every start but a heal for the rest
// of that second (each refusal is a chat line; a burst of them is the 0x00460D1D crash class).
int burst;
lock (Gate)
{
while (_errorAnswers.Count > 0 && now - _errorAnswers.Peek() >= 1000) _errorAnswers.Dequeue();
burst = _errorAnswers.Count;
if (burst >= RefusalBurstMax && !healStart) _burstHolds++;
}
// A heal is never held by the burst (an emergency must not wait on refusals of other starts).
if (burst >= RefusalBurstMax && !healStart)
{
why = $"{burst} refusals from the server in the last second";
return true;
}
// Attack repeats are never held by the latch (no UseDone ever comes for a swing).
if (attackRepeat) return false;
bool latched;
lock (Gate) latched = _busySince != 0 || _hung != null;
// Our cast still inside CastMaxMs with no 0x1D: OutstandingAllows holds it (heal rules included).
if (!latched) return false;
if (probe)
{
Log(host, $"[Pace] {who}: one try (#{_probes}) after the server's 'too busy' answer; no cast of ours is open");
return false;
}
int refusals;
BreakStage stage;
lock (Gate)
{
_refusalHolds++;
_holdsByWho[who] = _holdsByWho.TryGetValue(who, out int n) ? n + 1 : 1;
refusals = _busyRefusals;
stage = _stage;
}
string what = stuck != null ? $"{Describe(stuck.Kind, stuck.Label)} ({stuck.Who}) has had no answer for {age} ms" : "the server answered 'too busy'";
why = stage >= BreakStage.Jumped
? $"{what}; stuck-cast break sent, waiting for its UseDone"
: $"{what}{(refusals > 0 ? $" ({refusals} 'too busy' answer(s))" : "")}: nothing goes until the server answers";
bool log;
lock (Gate) log = _busyLogged.Add(who);
if (log) Log(host, $"[Pace] {who} waits: {why}");
return true;
}
/// <summary>The held-cast turn and the breaker steps for <paramref name="stuck"/> (one step per call at most).</summary>
private static void StepBreaker(RynthCoreHost host, PacedAction stuck, long age, long now)
{
if (StuckCastMs <= 0) return;
long cycle = NormalCycleMs(stuck.Id);
long jumpAt = Math.Min(cycle + JumpAfterMs, StuckCastMs);
long turnAt = Math.Min(cycle + TurnAfterMs, jumpAt);
long refusalBreakAt = RefusalBreakMinMs;
if (stuck.Script) { jumpAt = Math.Max(jumpAt, ScriptCastMinBreakMs); refusalBreakAt = Math.Max(refusalBreakAt, ScriptCastMinBreakMs); }
BreakStage stage; long stageAt; int refusals;
lock (Gate) { stage = _stage; stageAt = _stageAt; refusals = _busyRefusals; }
if (stage == BreakStage.None && age >= turnAt && age < jumpAt)
{
SetStage(BreakStage.Turned, now, stuck);
TurnToFace(host, stuck, age);
return;
}
if (stage <= BreakStage.Turned && (age >= jumpAt || (refusals > 0 && age >= refusalBreakAt)))
{
string why = age >= jumpAt ? $"{age} ms with no answer ({cycle} ms is normal for it)" : $"a send was refused 'too busy' while it was open {age} ms";
Jump(host, stuck, age, why, forced: false);
return;
}
if (stage == BreakStage.Landed && now - stageAt >= BreakerWaitMaxMs)
{
SetStage(BreakStage.ModeSent, now, stuck);
PeaceBreak(host, stuck, age);
return;
}
if (stage == BreakStage.ModeSent && now - stageAt >= BreakerWaitMaxMs)
{
lock (Gate) ClearBusyLocked();
Log(host, $"[Pace] no UseDone after the stuck-cast break ({Describe(stuck.Kind, stuck.Label)}, {age} ms) - going on");
}
}
private static void SetStage(BreakStage stage, long now, PacedAction? forAction)
{
lock (Gate) { _stage = stage; _stageAt = now; _stageFor = forAction; }
}
/// <summary>In the air after a breaker jump: hold until landed (height back, or JumpLandMaxMs).</summary>
private static bool AirborneHolds(RynthCoreHost host, long now, out string why)
{
why = "";
long jumpAt; float z0;
lock (Gate) { jumpAt = _jumpAt; z0 = _jumpZ; }
if (jumpAt == 0) return false;
long air = now - jumpAt;
bool landed = air >= JumpLandMaxMs;
if (!landed && air >= JumpLandMinMs)
{
try { landed = !host.HasGetPlayerPose || (host.TryGetPlayerPose(out _, out _, out _, out float z, out _, out _, out _, out _) && Math.Abs(z - z0) < 0.1f); }
catch { landed = true; }
}
if (!landed)
{
why = $"in the air after the stuck-cast jump ({air} ms)";
return true;
}
lock (Gate)
{
_jumpAt = 0;
if (_stage == BreakStage.Jumped)
{
if (_stageFor == null) { _stage = BreakStage.None; _stageAt = 0; } // a forced break with no cast of ours open
else { _stage = BreakStage.Landed; _stageAt = now; }
}
}
return false;
}
/// <summary>The breaker jump (TapJump: the keyboard jump, 0xF61B). No TapJump: the Peace request instead.</summary>
private static void Jump(RynthCoreHost host, PacedAction? stuck, long age, string why, bool forced)
{
long now = NowMs();
string what = stuck != null ? Describe(stuck.Kind, stuck.Label) : "the server's busy action";
if (!host.HasTapJump)
{
SetStage(BreakStage.ModeSent, now, stuck);
Log(host, $"[Pace] stuck cast: no TapJump on this engine; {what}: {why}");
PeaceBreak(host, stuck, age);
return;
}
float z = 0;
try { if (host.HasGetPlayerPose) host.TryGetPlayerPose(out _, out _, out _, out z, out _, out _, out _, out _); } catch { }
bool ok;
try { ok = host.TapJump(); } catch { ok = false; }
lock (Gate)
{
_stuckBreaks++;
if (forced) _forcedBreaks++;
_stage = BreakStage.Jumped;
_stageAt = now;
_stageFor = stuck;
if (ok) { _jumpAt = now; _jumpZ = z; }
}
Log(host, $"[Pace] stuck cast broken by jump after {age} ms: {what} - {why}{(ok ? "" : " (the jump was refused by the client)")}; nothing starts until landed and answered");
// Refused by the client: nothing in the air. For no cast of ours, nothing to wait for either.
if (!ok) { SetStage(stuck == null ? BreakStage.None : BreakStage.Landed, now, stuck); }
}
/// <summary>The Peace mode request (ACE fails a cast in progress on a mode change). Only from Magic.</summary>
private static void PeaceBreak(RynthCoreHost host, PacedAction? stuck, long age)
{
int mode = -1;
try { if (host.HasGetCurrentCombatMode) mode = host.GetCurrentCombatMode(); } catch { mode = -1; }
string what = stuck != null ? Describe(stuck.Kind, stuck.Label) : "the server's busy action";
if (mode == MagicMode && host.HasChangeCombatMode)
{
int seq = UseDoneSeqNow(host);
bool ok;
try { ok = host.ChangeCombatMode(PeaceMode); } catch { ok = false; }
lock (Gate) _stuckBreaks++;
if (ok)
{
PacedAction? keep;
lock (Gate) keep = _hung;
Begin(host, PacedKind.CombatMode, 0, ModeName(PeaceMode), "StuckCast", seq, PeaceMode);
lock (Gate) _hung = keep;
}
Log(host, $"[Pace] stuck cast broken by Peace mode after {age} ms: {what} - no answer after the jump; Peace {(ok ? "requested" : "refused by the client")}");
}
else
{
Log(host, $"[Pace] stuck cast: {what} still unanswered after the jump, not in Magic mode ({ModeName(mode)}): nothing more to cancel");
}
}
/// <summary>
/// Turn to face a held cast's target (once per cast), when RynthAi is not moving the character:
/// ACE's fast-tick cast waits for the facing to come within spellcast_max_angle.
/// </summary>
private static void TurnToFace(RynthCoreHost host, PacedAction stuck, long age)
{
uint target = stuck.Target;
uint player = 0;
try { if (host.HasGetPlayerId) player = host.GetPlayerId(); } catch { }
if (target == 0 || target == player || !host.HasTurnToHeading || MoveAudit.OwnMovementActive) return;
if (!TryHeadingTo(host, target, out double want, out double off)) return;
if (off < 10.0) return; // already facing it: the turn is not what holds it
try { host.TurnToHeading((float)want); } catch { return; }
lock (Gate) _heldTurns++;
Log(host, $"[Pace] held cast: {Describe(stuck.Kind, stuck.Label)} unanswered {age} ms; turning {off:0} deg to face 0x{target:X8} (heading {want:0})");
}
/// <summary>The heading to <paramref name="target"/> (AC degrees: 0 north, 90 east) and how far off the player faces.</summary>
internal static bool TryHeadingTo(RynthCoreHost host, uint target, out double heading, out double offDeg)
{
heading = 0; offDeg = 180;
try
{
if (!host.HasGetPlayerPose || !host.HasGetObjectPosition) return false;
if (!host.TryGetPlayerPose(out uint pcell, out float px, out float py, out _, out float qw, out _, out _, out float qz)) return false;
if (!host.TryGetObjectPosition(target, out uint tcell, out float tx, out float ty, out _)) return false;
double dx = (((tcell >> 24) & 0xFF) - (double)((pcell >> 24) & 0xFF)) * 192.0 + (tx - px);
double dy = (((tcell >> 16) & 0xFF) - (double)((pcell >> 16) & 0xFF)) * 192.0 + (ty - py);
if (Math.Abs(dx) < 0.01 && Math.Abs(dy) < 0.01) return false;
heading = Math.Atan2(dx, dy) * (180.0 / Math.PI);
if (heading < 0) heading += 360.0;
double yaw = 2.0 * Math.Atan2(qz, qw) * (180.0 / Math.PI);
double cur = ((-yaw) % 360.0 + 720.0) % 360.0;
double e = heading - cur;
while (e > 180) e -= 360;
while (e < -180) e += 360;
offDeg = Math.Abs(e);
return true;
}
catch { return false; }
}
/// <summary>
/// The HP failsafe's break (BuffManager): a jump now, whatever is open (on fast tick it fails a
/// cast in progress and clears the pending motions). False when one is already under way or
/// this engine has no TapJump.
/// </summary>
public static bool ForceBreak(RynthCoreHost host, string why)
{
if (!Enabled || !host.HasTapJump || IsPortaling(host)) return false;
PacedAction? stuck;
lock (Gate)
{
if (_jumpAt != 0 || _stage == BreakStage.Jumped) return false;
stuck = _hung ?? _shadow ?? (_cur is { Kind: PacedKind.Cast } ? _cur : null);
}
long age = stuck != null ? NowMs() - stuck.SentMs : 0;
Jump(host, stuck, age, why, forced: true);
return true;
}
// ── Helpers ─────────────────────────────────────────────────────────────
/// <summary>In portal space: no breaker step, no forced break (the server is moving us).</summary>
internal static bool IsPortaling(RynthCoreHost host)
{
try { return host.HasIsPortaling && host.IsPortaling(); } catch { return false; }
}
internal static string Describe(PacedKind kind, string label) => kind switch
{
PacedKind.WorldUse => $"the use of {label}",
@ -560,6 +1081,7 @@ internal static class ActionPacer
lines.Add($" Starts held {_heldStarts}, moves held {_heldMoves}, heals let ahead {_healsAhead}, sent over an unanswered one {_sentOverOutstanding}");
lines.Add($" Mode changes refused while waiting {_modeRefused}, skipped (already in that mode) {_modeSkipped}");
lines.Add($" Held while the character was animating {_gestureHolds}, animation time limits {_gestureLimits}");
lines.Add($" Held after a 'too busy' answer {_refusalHolds}, held casts turned to {_heldTurns}, stuck casts broken {_stuckBreaks} ({_forcedBreaks} by the HP failsafe; limit {StuckCastMs} ms)");
if (_maxWaitMs > 0) lines.Add($" Longest wait seen: {_maxWaitMs} ms ({_maxWaitWhat})");
if (_holdsByWho.Count > 0)
{
@ -588,11 +1110,25 @@ internal static class ActionPacer
if (ok)
{
CastsSent++;
LastCastSeqAtSend = host.HasUseDoneSeq ? seq : -1;
Begin(host, PacedKind.Cast, unchecked((uint)spellId), label, who, seq);
lock (Gate) { if (_cur is { Kind: PacedKind.Cast } c && c.Id == unchecked((uint)spellId)) c.Target = targetId; }
}
return ok;
}
/// <summary>The open cast of <paramref name="spellId"/> was chosen by a script (see <see cref="ScriptCastMinBreakMs"/>).</summary>
public static void MarkScriptCast(int spellId)
{
lock (Gate) { if (_cur is { Kind: PacedKind.Cast } c && c.Id == unchecked((uint)spellId)) c.Script = true; }
}
/// <summary>
/// The UseDone count read just before the last cast <see cref="CastSpellFor"/> sent (its answer is
/// the next UseDone after it); -1 when the engine has no UseDone count.
/// </summary>
public static int LastCastSeqAtSend { get; private set; } = -1;
/// <summary>
/// Casts sent through <see cref="CastSpellFor"/> in this plugin (every caller: combat, buffs,
/// nav, commands, meta): a caller that remembers the count at its own cast can tell whether

View file

@ -55,6 +55,7 @@ internal static class MoveAudit
if (on) FlushPendingStops(host, "before a start");
Note(host, who, "SetAutoRun", on ? "True" : "False", collapseByKindOnly: false);
lock (Gate) { _autoRunOn = on; if (on) _lastStartMs = NowMs(); else _pendingAutoRunOff = null; }
Changed(on ? "SetAutoRun(True)" : "SetAutoRun(False)");
return host.SetAutoRun(on);
}
@ -63,6 +64,7 @@ internal static class MoveAudit
if (HeldForAction(host, who, "StopCompletely()", isStop: true, deferAs: PendingStop.Complete())) return false;
Note(host, who, "StopCompletely", "", collapseByKindOnly: false);
lock (Gate) { _autoRunOn = false; _motionsOn.Clear(); ClearPendingLocked(); } // clears everything AC has on
Changed("StopCompletely()");
return host.StopCompletely();
}
@ -73,6 +75,7 @@ internal static class MoveAudit
if (on) FlushPendingStops(host, "before a start");
Note(host, who, "SetMotion", $"0x{motion:X8}, {(on ? "True" : "False")}", collapseByKindOnly: false);
lock (Gate) { if (on) { _motionsOn.Add(motion); _lastStartMs = NowMs(); } else { _motionsOn.Remove(motion); _pendingMotionOff.Remove(motion); } }
Changed($"SetMotion(0x{motion:X8}, {(on ? "True" : "False")})");
return host.SetMotion(motion, on);
}
@ -83,6 +86,7 @@ internal static class MoveAudit
FlushPendingStops(host, "before a start");
Note(host, who, "DoMovement", $"0x{motion:X8}, {speed:0.##}, {holdKey}", collapseByKindOnly: false);
lock (Gate) { _motionsOn.Add(motion); _lastStartMs = NowMs(); }
Changed($"DoMovement(0x{motion:X8})");
return host.DoMovement(motion, speed, holdKey);
}
@ -290,6 +294,9 @@ internal static class MoveAudit
}
catch { }
// A held stop sent now is a movement change like any other: the settle wait applies to it.
if (sent.Count > 0) Changed(sent[sent.Count - 1]);
if (sent.Count > 0 && Enabled)
{
try { host.Log($"[Move] {PluginName} held stop(s) sent {why} after {waited} ms: {string.Join(", ", sent)}"); }
@ -332,6 +339,7 @@ internal static class MoveAudit
if (HeldForAction(host, who, $"StopMovement(0x{motion:X8})", isStop: true, motion, PendingStop.StopMovement(motion, holdKey))) return false;
Note(host, who, "StopMovement", $"0x{motion:X8}, {holdKey}", collapseByKindOnly: false);
lock (Gate) { _motionsOn.Remove(motion); _pendingStopMovement.Remove(motion); }
Changed($"StopMovement(0x{motion:X8})");
return host.StopMovement(motion, holdKey);
}
@ -341,6 +349,7 @@ internal static class MoveAudit
if (HeldForUse(host, who, "TurnToHeading")) return false;
if (HeldForAction(host, who, "TurnToHeading", isStop: false)) return false;
Note(host, who, "TurnToHeading", headingDegrees.ToString("0.0", System.Globalization.CultureInfo.InvariantCulture), collapseByKindOnly: true);
Changed("TurnToHeading");
return host.TurnToHeading(headingDegrees);
}
@ -400,6 +409,38 @@ internal static class MoveAudit
ClearPendingLocked();
_pendingSinceMs = 0;
_deferredStopsSent = 0;
_lastChangeMs = long.MinValue / 2;
_lastChangeWhat = "";
}
}
// -- Settle after a movement change -------------------------------------------------
// A Bolt VII went out in the same tick as nav's SetAutoRun(False), after nav's turns; on ACE's
// fast tick the movement packet cancelled the cast's turn and the server held the cast 40.8 s
// (ActionPacer's stuck-cast notes). After any movement call RynthAi sends (a start, a stop, a
// turn), nothing starts until MoveSettleMinMs has passed and the client's motion queue reads
// clear (GetCastBusyState), or MoveSettleMaxMs. ActionPacer.MayStart asks (not for attack repeats).
internal const long MoveSettleMinMs = 150;
internal const long MoveSettleMaxMs = 600;
private static long _lastChangeMs = long.MinValue / 2;
private static string _lastChangeWhat = "";
private static void Changed(string what)
{
lock (Gate) { _lastChangeMs = NowMs(); _lastChangeWhat = what; }
}
/// <summary>True (with why) while the last movement change has not settled.</summary>
internal static bool SettleHolds(RynthCoreHost host, out string why)
{
why = "";
long age; string what;
lock (Gate) { age = NowMs() - _lastChangeMs; what = _lastChangeWhat; }
if (age >= MoveSettleMaxMs) return false;
bool queueBusy;
try { queueBusy = host.HasGetCastBusyState && host.GetCastBusyState() != 0; } catch { queueBusy = false; }
if (age >= MoveSettleMinMs && !queueBusy) return false;
why = $"the movement change {what} sent {age} ms ago has not settled";
return true;
}
}

View file

@ -17,9 +17,11 @@
// Rules (as RynthAi.Script, Shared/RynthAiScriptApi.cs):
// - Its own interface, so RynthAi.Script v1 (exact-version checked) and
// RynthAi.Status stay as they are.
// - Append-only: new functions and request fields go at the end, and Version
// goes up. CombatRequestV1.StructSize is set by the caller before Poll;
// RynthAi never writes past it.
// - Append-only: new functions go at the end, and Version goes up. New
// request fields also go at the end but do not bump Version: they are gated
// by StructSize. The caller sets CombatRequestV1.StructSize before Poll;
// RynthAi never writes past it and writes back how much it filled, so a
// reader uses an appended field only when that StructSize covers it.
// - Strings are UTF-8, NUL-terminated.
// - Call only from the plugin pump thread (OnTick), never from AC's thread or Render.
// - Every function returns safely (no exception crosses the boundary).
@ -61,6 +63,12 @@ public static class RynthAiCombat
public const uint MinTimeoutMs = 100;
public const uint MaxTimeoutMs = 3000;
/// <summary>
/// A cast with no answer (UseDone) this long after its send is resolved with no answer (error -1).
/// The pacing's own cast limit (ActionPacer.CastMaxMs).
/// </summary>
public const long ResolveTimeoutMs = 7000;
/// <summary>Longest owner name RynthAi keeps (characters).</summary>
public const int MaxOwnerLength = 64;
@ -118,6 +126,27 @@ public struct CombatRequestV1
public uint LastSpell;
public uint LastTarget;
public long LastAtMs;
// Appended fields (present only when the StructSize RynthAi wrote back covers them; an older
// RynthAi does not). A cast of this kind is IN FLIGHT from its send until its answer, the first
// UseDone after the send, and RESOLVED after it. Declines and rejects are not casts.
/// <summary>The most recent resolved cast: Seq of its request, spell, target, send time (0s before the first).</summary>
public uint ResolvedSeq;
public uint ResolvedSpell;
public uint ResolvedTarget;
public long ResolvedAtMs;
/// <summary>
/// Its answer, final: the UseDone error (0 = done, 0x1D = refused 'too busy', 0x401 = not enough
/// mana, 0x402 = fizzled, ...); -1 = no answer could be read: no UseDone within
/// RynthAiCombat.ResolveTimeoutMs of the send, or another answer came first.
/// </summary>
public int ResolvedErr;
/// <summary>The cast still in flight (sent, no answer yet): Seq, spell, target, send time; Spell 0 = none.</summary>
public uint PendingSeq;
public uint PendingSpell;
public uint PendingTarget;
public long PendingAtMs;
}
[StructLayout(LayoutKind.Sequential)]