RynthSuite/Plugins/Shared/RynthAiCombatApi.cs
Daniel Wallace 2d8bebb0f4 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>
2026-10-12 17:35:42 -07:00

189 lines
9.5 KiB
C#

// ============================================================================
// RynthSuite - Plugins/Shared/RynthAiCombatApi.cs
// The "RynthAi.Combat" interface: lets a script plugin (RynthLua) choose the
// spell for RynthAi's next attack cast or vitals heal, while RynthAi keeps
// targeting, pacing, stance and the cast itself. Compiled into both plugins
// (linked source), so the layout and the constants can't drift between them.
//
// The exchange is polled, never a callback:
// 1. The script side calls Claim every tick for the kinds it handles (a
// lease: when it stops renewing, RynthAi goes native on its own).
// 2. When RynthAi reaches a cast choice it opens a request (a new Seq) and
// waits, holding that one cast, up to the request's deadline.
// 3. The script side sees it through Poll, decides, and answers with Decide.
// 4. RynthAi validates the answer and casts it, or (spell 0, an invalid
// answer, or no answer by the deadline) makes its own choice that tick.
//
// 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 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).
// - Times are Environment.TickCount64 milliseconds (one clock per process).
// ============================================================================
using System.Runtime.InteropServices;
namespace RynthCore.Plugin.Shared;
/// <summary>Constants of the "RynthAi.Combat" exchange: kinds, decision flags, lease and timeout bounds.</summary>
public static class RynthAiCombat
{
/// <summary>Kind bit: the spell of RynthAi's next attack cast (Magic mode, after its debuffs).</summary>
public const uint KindAttack = 1;
/// <summary>Kind bit: the spell of the next vitals heal (health under Heal At / Top Off HP), asked before kits.</summary>
public const uint KindHeal = 2;
/// <summary>Every kind this version knows.</summary>
public const uint KindsAll = KindAttack | KindHeal;
/// <summary>Decision flag: an area cast (ring, wall, chain): no blacklist or pending-offensive judgement on it.</summary>
public const uint FlagArea = 1;
/// <summary>
/// Decision flag: a single-target cast that is not meant to damage (a debuff, a drain, a
/// utility spell): no no-damage blacklist judgement on it. Flags are append-only; RynthAi
/// ignores bits it does not know.
/// </summary>
public const uint FlagNoJudge = 2;
/// <summary>Lease length when Claim passes 0, and its bounds (clamped).</summary>
public const uint DefaultLeaseMs = 1500;
public const uint MinLeaseMs = 500;
public const uint MaxLeaseMs = 3000;
/// <summary>How long RynthAi waits for an answer before casting its own choice (0 = default), and the bounds.</summary>
public const uint DefaultAttackTimeoutMs = 1000;
public const uint DefaultHealTimeoutMs = 750;
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;
/// <summary>"attack", "heal", "attack+heal" or "none".</summary>
public static string KindNames(uint kinds)
{
kinds &= KindsAll;
if (kinds == 0) return "none";
if (kinds == KindsAll) return "attack+heal";
return kinds == KindAttack ? "attack" : "heal";
}
/// <summary>A lease length within the bounds (0 = the default).</summary>
public static uint ClampLease(uint ms) => ms == 0 ? DefaultLeaseMs : ms < MinLeaseMs ? MinLeaseMs : ms > MaxLeaseMs ? MaxLeaseMs : ms;
/// <summary>A request timeout within the bounds (0 = <paramref name="fallback"/>).</summary>
public static uint ClampTimeout(uint ms, uint fallback) => ms == 0 ? fallback : ms < MinTimeoutMs ? MinTimeoutMs : ms > MaxTimeoutMs ? MaxTimeoutMs : ms;
}
/// <summary>
/// An open request, as Poll hands it out. Blittable; the caller sets StructSize to
/// sizeof(CombatRequestV1) before Poll and RynthAi fills at most that many bytes.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct CombatRequestV1
{
/// <summary>Set by the caller before Poll: the size of the struct it passes.</summary>
public uint StructSize;
/// <summary>The request's id; Decide must quote it. Never 0.</summary>
public uint Seq;
/// <summary>RynthAiCombat.KindAttack or KindHeal.</summary>
public uint Kind;
/// <summary>
/// Attack: RynthAi's locked target. Heal: RynthAi's current fight target, informational (0 when
/// none); a heal decision's target 0 still means the player.
/// </summary>
public uint TargetId;
/// <summary>That target's health, 0..1; -1 unknown or no target.</summary>
public float TargetHealth;
/// <summary>The player's vitals in percent; -1 unknown.</summary>
public int HealthPct;
public int StaminaPct;
public int ManaPct;
/// <summary>Heal: the health line it is under (Heal At in combat, Top Off HP idle). Attack: 0.</summary>
public int HealLine;
/// <summary>1 when a monster is engaged or within Monster Range.</summary>
public int InCombat;
/// <summary>AC's combat mode (1 NonCombat, 2 Melee, 4 Missile, 8 Magic).</summary>
public int CombatMode;
/// <summary>When the request was opened, and when RynthAi stops waiting for it.</summary>
public long IssuedAtMs;
public long DeadlineMs;
/// <summary>The last decision of this kind RynthAi consumed: its Seq, the spell cast (0 = declined or rejected), the target and when.</summary>
public uint LastSeq;
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)]
public unsafe struct RynthAiCombatApiV1
{
public const string InterfaceName = "RynthAi.Combat";
public const uint InterfaceVersion = 1;
public uint Version; // InterfaceVersion of the table RynthAi filled
public uint StructSize; // sizeof(RynthAiCombatApiV1) as RynthAi built it
/// <summary>
/// Claim(kinds, leaseMs, attackTimeoutMs, healTimeoutMs, owner): take or renew the hook for
/// <c>kinds</c> (RynthAiCombat.Kind* bits) until now + leaseMs (clamped; 0 = default). Renew it
/// every tick. The kinds passed replace the ones held. Returns the kinds granted: 0 while another
/// owner's lease is live (the names compare case-sensitively).
/// </summary>
public delegate* unmanaged[Cdecl]<uint, uint, uint, uint, byte*, int> Claim;
/// <summary>Release(kinds): drop those kinds now (an open request of them is cancelled: native that tick).</summary>
public delegate* unmanaged[Cdecl]<uint, void> Release;
/// <summary>Poll(kind, request): 1 when a request of that kind is open and unanswered (filled in), else 0.</summary>
public delegate* unmanaged[Cdecl]<uint, CombatRequestV1*, int> Poll;
/// <summary>
/// Decide(seq, kind, spellId, targetId, flags): the answer to request <c>seq</c>. spellId 0 =
/// decline (RynthAi chooses); targetId 0 = the request's target (the player for a heal);
/// flags RynthAiCombat.Flag*. 1 = taken; 0 = stale (another seq, already answered, past its
/// deadline, or the kind isn't held). Taken is not cast: RynthAi still validates it.
/// </summary>
public delegate* unmanaged[Cdecl]<uint, uint, uint, uint, uint, int> Decide;
/// <summary>
/// GetScanned(ids, distances, capacity): RynthAi's current target candidates (in range,
/// attackable, line of sight), nearest first. Writes up to capacity and returns how many
/// there are in all (ids null: just the count).
/// </summary>
public delegate* unmanaged[Cdecl]<uint*, float*, int, int> GetScanned;
}