// ============================================================================ // 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; /// Constants of the "RynthAi.Combat" exchange: kinds, decision flags, lease and timeout bounds. public static class RynthAiCombat { /// Kind bit: the spell of RynthAi's next attack cast (Magic mode, after its debuffs). public const uint KindAttack = 1; /// Kind bit: the spell of the next vitals heal (health under Heal At / Top Off HP), asked before kits. public const uint KindHeal = 2; /// Every kind this version knows. public const uint KindsAll = KindAttack | KindHeal; /// Decision flag: an area cast (ring, wall, chain): no blacklist or pending-offensive judgement on it. public const uint FlagArea = 1; /// /// 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. /// public const uint FlagNoJudge = 2; /// Lease length when Claim passes 0, and its bounds (clamped). public const uint DefaultLeaseMs = 1500; public const uint MinLeaseMs = 500; public const uint MaxLeaseMs = 3000; /// How long RynthAi waits for an answer before casting its own choice (0 = default), and the bounds. public const uint DefaultAttackTimeoutMs = 1000; public const uint DefaultHealTimeoutMs = 750; public const uint MinTimeoutMs = 100; public const uint MaxTimeoutMs = 3000; /// /// 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). /// public const long ResolveTimeoutMs = 7000; /// Longest owner name RynthAi keeps (characters). public const int MaxOwnerLength = 64; /// "attack", "heal", "attack+heal" or "none". public static string KindNames(uint kinds) { kinds &= KindsAll; if (kinds == 0) return "none"; if (kinds == KindsAll) return "attack+heal"; return kinds == KindAttack ? "attack" : "heal"; } /// A lease length within the bounds (0 = the default). public static uint ClampLease(uint ms) => ms == 0 ? DefaultLeaseMs : ms < MinLeaseMs ? MinLeaseMs : ms > MaxLeaseMs ? MaxLeaseMs : ms; /// A request timeout within the bounds (0 = ). public static uint ClampTimeout(uint ms, uint fallback) => ms == 0 ? fallback : ms < MinTimeoutMs ? MinTimeoutMs : ms > MaxTimeoutMs ? MaxTimeoutMs : ms; } /// /// 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. /// [StructLayout(LayoutKind.Sequential)] public struct CombatRequestV1 { /// Set by the caller before Poll: the size of the struct it passes. public uint StructSize; /// The request's id; Decide must quote it. Never 0. public uint Seq; /// RynthAiCombat.KindAttack or KindHeal. public uint Kind; /// /// 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. /// public uint TargetId; /// That target's health, 0..1; -1 unknown or no target. public float TargetHealth; /// The player's vitals in percent; -1 unknown. public int HealthPct; public int StaminaPct; public int ManaPct; /// Heal: the health line it is under (Heal At in combat, Top Off HP idle). Attack: 0. public int HealLine; /// 1 when a monster is engaged or within Monster Range. public int InCombat; /// AC's combat mode (1 NonCombat, 2 Melee, 4 Missile, 8 Magic). public int CombatMode; /// When the request was opened, and when RynthAi stops waiting for it. public long IssuedAtMs; public long DeadlineMs; /// The last decision of this kind RynthAi consumed: its Seq, the spell cast (0 = declined or rejected), the target and when. 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. /// The most recent resolved cast: Seq of its request, spell, target, send time (0s before the first). public uint ResolvedSeq; public uint ResolvedSpell; public uint ResolvedTarget; public long ResolvedAtMs; /// /// 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. /// public int ResolvedErr; /// The cast still in flight (sent, no answer yet): Seq, spell, target, send time; Spell 0 = none. 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 /// /// Claim(kinds, leaseMs, attackTimeoutMs, healTimeoutMs, owner): take or renew the hook for /// kinds (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). /// public delegate* unmanaged[Cdecl] Claim; /// Release(kinds): drop those kinds now (an open request of them is cancelled: native that tick). public delegate* unmanaged[Cdecl] Release; /// Poll(kind, request): 1 when a request of that kind is open and unanswered (filled in), else 0. public delegate* unmanaged[Cdecl] Poll; /// /// Decide(seq, kind, spellId, targetId, flags): the answer to request seq. 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. /// public delegate* unmanaged[Cdecl] Decide; /// /// 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). /// public delegate* unmanaged[Cdecl] GetScanned; }