using System; namespace RynthCore.Plugin.RynthAi; /// /// Bookkeeping for the casts BuffManager sends (buffs and vitals): when each one went out, /// when its incantation ("You say, ...") arrived, and whether the server has finished it. /// /// Why (Drakkon on DreamWeave, 2026-10-05, ops\overnight\2026-10-05-stuck-cast.md): /// * The give-up timers ran from the SEND. A healthy DreamWeave vital takes 2.2-2.5 s from /// its incantation to its result, and the incantation lags the send by up to 1.2 s, so a /// cast the server was still finishing was given up at 2.5 s and the next one drew /// "You're too busy!". Now the give-up runs from the incantation when one arrives /// (), capped, and from the send when none does. /// * "Has the server finished?" cleared on ANY UseDone, including the 0x1D refusal of the /// next cast. Now only a completion that belongs to the outstanding cast counts: its own /// result line, or a UseDone with no error after its incantation (). /// A refusal is a refusal, not "done". /// * A cast the server held open (incantation, no result) was only released when someone /// moved the character. says when to send one movement nudge. /// * Each cast's time from incantation to result is logged ([CastTime]), so a slow or held /// cast shows in the log. /// /// Pure bookkeeping: it never calls the game. The clock is injectable for the tests. /// internal sealed class CastTracker { internal sealed class Cast { public int SpellId; public string Name = ""; public DateTime SentAt; public DateTime IncantAt = DateTime.MinValue; public int SeqAtSend; public int SeqAtIncant; public int SeqAtRefusal; public bool Refused; // AC answered this cast with "You're too busy!" public bool Done; // its result line, or UseDone(0) after its incantation public bool Nudged; // the one movement nudge for this held cast has gone out public bool StuckLogged; public bool HasIncantation => IncantAt != DateTime.MinValue; } /// Incantation to result, at most. Healthy DreamWeave vitals: 2.21-2.52 s. public const double IncantToResultMs = 3000; /// Never wait longer than this after the send, whatever the incantation said. public const double GiveUpCapMs = 6000; /// An incantation with no result for this long is a held cast. public const double HeldAfterIncantMs = 6000; /// /// The same, when the held cast is a heal or health is under a heal line (2026-10-05: Drakkon /// died 4 s after a Heal Self the server held open). Past the healthy 2.2-2.5 s from /// incantation to result, so a cast still finishing normally is not nudged. /// public const double UrgentHeldAfterIncantMs = 2800; /// An incantation this long after the send isn't credited to it. private const double IncantWindowMs = 15000; private readonly Action _log; public CastTracker(Action log) => _log = log; public Func Clock = () => DateTime.Now; public DateTime Now => Clock(); /// The last cast sent. public Cast? Current { get; private set; } /// The cast whose incantation arrived and whose result hasn't (may be older than Current). public Cast? Open { get; private set; } /// When to stop waiting for : after /// the send, or later when its incantation came late (incantation + IncantToResultMs), but /// never past max(fallbackMs, GiveUpCapMs) after the send. public static DateTime GiveUpAt(Cast c, double fallbackMs) { DateTime basis = c.SentAt.AddMilliseconds(fallbackMs); if (!c.HasIncantation) return basis; DateTime byIncant = c.IncantAt.AddMilliseconds(IncantToResultMs); DateTime cap = c.SentAt.AddMilliseconds(Math.Max(fallbackMs, GiveUpCapMs)); DateTime d = byIncant > basis ? byIncant : basis; return d < cap ? d : cap; } /// True when the pending cast is past its give-up time. /// Null when the tracker doesn't hold that cast (the caller uses its old timer). public bool? GiveUpReached(int spellId, double fallbackMs) { Cast? c = Current; if (c == null || c.SpellId != spellId) return null; return Now >= GiveUpAt(c, fallbackMs); } public void Sent(int spellId, string name, int useDoneSeq) { DateTime now = Now; // An earlier cast that is still held open stays Open; one with no incantation is simply replaced. Current = new Cast { SpellId = spellId, Name = name, SentAt = now, SeqAtSend = useDoneSeq }; } /// "You say, ..." arrived. Credited to the last cast sent when it has none yet. /// A new incantation also means the server let go of any older open cast. public Cast? Incantation(int useDoneSeq) { DateTime now = Now; Cast? c = Current; if (c == null || c.HasIncantation || c.Refused || c.Done || (now - c.SentAt).TotalMilliseconds > IncantWindowMs) return null; if (Open != null && Open != c && !Open.Done) { _log($"[CastTime] '{Open.Name}': no result {Secs(now - Open.IncantAt)} after its incantation; the server started the next cast ('{c.Name}'), so it is over."); Open.Done = true; } c.IncantAt = now; c.SeqAtIncant = useDoneSeq; Open = c; _log($"[CastTime] '{c.Name}': incantation {Ms(now - c.SentAt)} after the send."); return c; } /// A result line ("You cast X ...", a fizzle, a hard refusal). /// is what followed "You cast " (null for lines that don't name the spell). : /// an unnamed refusal the server gives before the cast starts (no components, not enough mana), /// which belongs to the last cast sent. Returns the cast it belongs to, or null. public Cast? Result(string? castText, string kind, bool atStart = false) { Cast? c = null; bool currentLive = Current != null && !Current.Done && !Current.Refused; bool openLive = Open != null && !Open.Done; if (!string.IsNullOrEmpty(castText)) { if (Current != null && !Current.Done && Matches(castText, Current.Name)) c = Current; else if (openLive && Matches(castText, Open!.Name)) c = Open; } else if (atStart) c = currentLive && !Current!.HasIncantation ? Current : (openLive ? Open : null); else // Unnamed, after the windup (a fizzle): the cast the server is running, else the last one sent. c = openLive ? Open : (currentLive ? Current : null); if (c == null) return null; Close(c, kind); return c; } private static bool Matches(string castText, string name) => name.Length > 0 && castText.StartsWith(name, StringComparison.OrdinalIgnoreCase); /// The cast's own enchantment landed (self-buff confirmed by event or registry). public void Confirmed(int spellId, string kind) { if (Current != null && !Current.Done && Current.SpellId == spellId) Close(Current, kind); else if (Open != null && !Open.Done && Open.SpellId == spellId) Close(Open, kind); } private void Close(Cast c, string kind) { c.Done = true; DateTime now = Now; string nudged = c.Nudged ? " (after the movement nudge)" : ""; if (c.HasIncantation) _log($"[CastTime] '{c.Name}': {kind} {Secs(now - c.IncantAt)} after its incantation (send to incantation {Ms(c.IncantAt - c.SentAt)}){nudged}."); else _log($"[CastTime] '{c.Name}': {kind} {Secs(now - c.SentAt)} after the send (no incantation seen){nudged}."); if (Open == c) Open = null; } /// The last UseDone the engine saw. A UseDone with no error after a cast's incantation /// is that cast finishing; anything else (0x1D too busy, a UseDone before the incantation) is not. public void ObserveUseDone(int lastSeq, uint lastError) { if (lastError != 0) return; foreach (Cast? c in new[] { Open, Current }) { if (c == null || c.Done || !c.HasIncantation || lastSeq <= c.SeqAtIncant) continue; Close(c, $"UseDone #{lastSeq} (no error)"); } } /// /// True while the server hasn't finished the last cast sent. Clears on that cast's result /// line, on a UseDone(0) after its incantation, after a refusal once a UseDone(0) shows the /// server free again, or at its give-up time ( from the send, /// later with a late incantation). false (an engine before v70) /// keeps the old rule: any new UseDone clears it. /// public bool AwaitingServer(double fallbackMs, int useDoneSeq, bool hasLast, int lastSeq, uint lastError) { Cast? c = Current; if (c == null || c.Done) return false; if (Now >= GiveUpAt(c, fallbackMs)) return false; if (!hasLast) return useDoneSeq == c.SeqAtSend; ObserveUseDone(lastSeq, lastError); if (c.Done) return false; if (c.Refused && lastError == 0 && lastSeq > c.SeqAtRefusal) return false; return true; } /// /// "You're too busy!" arrived. The last cast sent, if it had no incantation yet, was refused. /// Returns true exactly once per held cast: an incantation more than HeldAfterIncantMs ago, /// no result, and AC still refusing casts. The caller sends one movement nudge. /// public bool Refusal(int useDoneSeq, double heldAfterMs = HeldAfterIncantMs) { Cast? c = Current; if (c != null && !c.HasIncantation && !c.Done && !c.Refused) { c.Refused = true; c.SeqAtRefusal = useDoneSeq; } return TakeNudge(heldAfterMs); } /// /// True exactly once per held cast: open (incantation, no result) for more than /// and not nudged yet. Marks it nudged. The refusal path uses /// it with HeldAfterIncantMs; a heal (or health under a heal line) polls it each tick with /// UrgentHeldAfterIncantMs, without waiting for a refusal. /// public bool TakeNudge(double heldAfterMs) { Cast? held = Open; if (held == null || held.Done || held.Nudged || !held.HasIncantation) return false; if ((Now - held.IncantAt).TotalMilliseconds <= heldAfterMs) return false; held.Nudged = true; return true; } /// The cast the server holds open (incantation, no result), or null. public Cast? Held => Open is { Done: false } o && o.HasIncantation ? o : null; /// One log line the first time a cast has been open past HeldAfterIncantMs (polled each tick). public void Poll() { Cast? held = Open; if (held == null || held.Done || held.StuckLogged) return; if ((Now - held.IncantAt).TotalMilliseconds <= HeldAfterIncantMs) return; held.StuckLogged = true; _log($"[CastTime] '{held.Name}': HELD - incantation {Secs(Now - held.IncantAt)} ago and no result yet (the server is holding the cast open)."); } /// Seconds a cast has been held open (incantation, no result), or 0. public double HeldSeconds { get { Cast? held = Open; return held == null || held.Done ? 0 : (Now - held.IncantAt).TotalSeconds; } } private static string Secs(TimeSpan t) => $"{t.TotalSeconds:0.00} s"; private static string Ms(TimeSpan t) => $"{t.TotalMilliseconds:0} ms"; }