// ============================================================================ // RynthDefective - Use/BusyGate.cs // Whether the client's "busy" readings really hold a use back (2026-10-28). // // Why: the busy count the engine reports (GetBusyState) can stay above zero // with nothing at all in flight: after a death (the engine's own resync waits // out the death portal and 5 s more), after a logout and a login (the engine // keeps its count across the session and can't clear it again), after an // interrupted action. Holding every use on it made the bar "not work at all // after logging in" until the client was restarted, and kits wait seconds // after every death. RynthAi copes the same way (Combat/CombatBusyGate: a // leftover count doesn't hold a heal), and RynthAi's "waiting on the server" // bit includes its copy of the same count, so it sticks with it. // // The rule (pure: BusyRule.Decide; BusyGate keeps the clocks): // * a gesture animating holds, for at most AnimationMaxMs without a break // (RynthAi's ActionPacer.GestureHoldMaxMs: no real gesture lasts longer); // * the busy count holds while it is above the count already judged a // leftover AND it moved within CountTrustMs or our own last use went out // within OwnTrustMs. A count that has read the same for longer, with // nothing of ours in flight, is a leftover: it holds nothing, and only a // count above it holds again (our own next use raises it and the server's // answer brings it back down to the leftover); // * RynthAi's "waiting on the server" bit holds while it is fresh: // BotTrustMs since it came on (RynthAi's own actions are answered or let // go well inside it), or only CountTrustMs when a leftover count explains it. // After that the use is sent: if the server really is busy it answers // "You're too busy" (UseDone 0x1D) and the use controller tries once more. // // With RynthAi holding for the press (RynthAi.Yield, 2026-10-29), RynthAi says // exactly what it has in flight (its pacer's one outstanding action), so the // rule is simpler and doesn't wait seconds for the count to sit still: // * RynthAi's own unanswered action holds, for at most BotTrustMs; // * the count holds only right after our own use (OwnTrustMs); // * anything else in the count is a leftover (a death, a login, a corpse // open's +1) or a click in the game's own windows: the use goes. // After a death or a login RynthAi's stream of rebuffs kept the count moving, // so it never read "steady" and a press waited until it gave up. // // RynthDefective doesn't reset the client's count itself (ForceResetBusyCount) // unless the player asks: the count is the engine's (its reconciler and // watchdogs own it), RynthAi stopped resetting it on 2026-10-06 because a reset // zeroes actions the server hasn't answered yet (each later answer then drives // it negative), and nothing here needs it at zero: ignoring a leftover is // enough to send. // // "Shortcuts clear a stuck busy state" (DefSettings.ClearStuckBusy, 2026-11-04, // off by default; a player's ask): a press held ONLY by the count (BusyHold.Busy // with nothing of ours in flight or unanswered, no gesture, RynthAi with no // action of its own, no "You're too busy" from the server in ServerBusyRecentMs) // sends the reset RynthAi's /ra clearbusy sends (the engine's // ForceResetBusyCount: the count to 0, the cursor re-read, every plugin's // mirror told), once per press, then the press goes (BusyClearRule; the use // controller waits ClearSettleMs for the reset to land on the game's tick, // then takes what is left as a leftover). Not /ra clearbusy's StopCompletely: // a press must not stop a run. Never with a real action in flight. // ============================================================================ using System; namespace RynthCore.Plugin.RynthDefective.Use; /// What holds a use back now; None = send. internal enum BusyHold { None, Busy, Animation, Bot } /// The readings that were ignored as leftovers (each logged once). [Flags] internal enum BusyStale { None = 0, Count = 1, Animation = 2, Bot = 4 } /// The readings at one moment, and how long each has read so. Pure data. /// The client's busy count (GetBusyState). /// How long it has read this value. /// The count already judged a leftover (0 = none): a reading at or under it holds nothing. /// Since RynthDefective's own last use or cast went out. /// A cast or use gesture is animating. /// How long it has animated without a break. /// RynthAi is waiting on the server (null = RynthAi isn't there). /// How long that bit has read its current value. /// /// RynthAi holds for the press and reports its own unanswered action (RynthAi.Yield); null = it /// doesn't (absent, from before, its pacing off): the rules above apply. /// /// How long RynthAi's own action has been unanswered (0 when none). internal readonly record struct BusyFacts( int Count, long CountSteadyMs, int Leftover, long SinceOwnSendMs, bool Animating, long AnimatingMs, bool? BotWaiting, long BotSteadyMs, bool? BotPaced = null, long BotPacedMs = 0); internal static class BusyRule { /// A count that moved this recently is an action under way (a kit 2.5 s, a corpse use 3.7 s at the most seen). public const long CountTrustMs = 4000; /// After our own use went out the count is ours this long. public const long OwnTrustMs = 4000; /// A gesture that lasts longer is a stuck motion queue (RynthAi's ActionPacer.GestureHoldMaxMs). public const long AnimationMaxMs = 4000; /// RynthAi's bit this long: its corpse uses are let go at 5 s (ActionPacer.WorldUseMaxMs). public const long BotTrustMs = 5500; /// What holds a use back; names the readings ignored as leftovers. public static BusyHold Decide(in BusyFacts f, out BusyStale stale) { stale = BusyStale.None; if (f.Animating) { if (f.AnimatingMs < AnimationMaxMs) return BusyHold.Animation; stale |= BusyStale.Animation; } if (f.BotPaced is bool paced) { // RynthAi holds for the press and says what it has in flight: that, and our own last use, // are all that hold it. The rest of the count is a leftover or a click in the game's own // windows; a really busy server answers "You're too busy" (one more try). if (paced) { if (f.BotPacedMs < BotTrustMs) return BusyHold.Bot; stale |= BusyStale.Bot; } if (f.Count > 0) { if (f.Count > f.Leftover && f.SinceOwnSendMs < OwnTrustMs) return BusyHold.Busy; stale |= BusyStale.Count; } return BusyHold.None; } if (f.Count > 0) { bool fresh = f.CountSteadyMs < CountTrustMs || f.SinceOwnSendMs < OwnTrustMs; if (f.Count > f.Leftover && fresh) return BusyHold.Busy; stale |= BusyStale.Count; } if (f.BotWaiting == true) { // RynthAi's bit includes its copy of the same count: a leftover count explains it. long trust = (stale & BusyStale.Count) != 0 ? CountTrustMs : BotTrustMs; if (f.BotSteadyMs < trust) return BusyHold.Bot; stale |= BusyStale.Bot; } return BusyHold.None; } } /// /// Whether a press held by the busy count clears it first (DefSettings.ClearStuckBusy, off by default). /// Pure: the use controller asks, the tests check every case. /// internal static class BusyClearRule { /// The server answered "You're too busy" this recently: it really is busy, nothing is cleared. public const long ServerBusyRecentMs = 5000; /// The player's option. /// What says holds the press. /// The readings it said it from. /// A use or cast of ours is in flight (sent, not answered or let go). /// Our own last use or cast was answered (UseDone, the item in hand, a refusal); false while one went unanswered. /// Since the server last answered "You're too busy". public static bool ShouldClear(bool enabled, BusyHold hold, in BusyFacts f, bool ownInFlight, bool ownAnswered, long sinceServerBusyMs) { if (!enabled || hold != BusyHold.Busy || f.Count <= 0) return false; // only the count, and only when it holds if (ownInFlight) return false; // ours, under way if (f.SinceOwnSendMs < BusyRule.OwnTrustMs && !ownAnswered) return false; // ours, never answered: it may still be if (f.Animating) return false; // a gesture: the character is acting if (f.BotPaced == true) return false; // RynthAi's own action, unanswered // A RynthAi that doesn't say what it has in flight: its "waiting on the server" bit while fresh. if (f.BotPaced == null && f.BotWaiting == true && f.BotSteadyMs < BusyRule.BotTrustMs) return false; return sinceServerBusyMs >= ServerBusyRecentMs; // the server said busy just now } } /// /// The clocks behind : how long each reading has read the same, our own last /// send, and the leftover count. every tick (in or out of the world: a count /// left by a death has read the same all through the death portal), when a use /// wants to go. Plugin pump thread only. /// internal sealed class BusyGate { private bool _started; private int _count, _leftover; private long _countSinceMs; private bool _animating; private long _animatingSinceMs; private bool? _bot; private long _botSinceMs; private bool _paced; private long _pacedSinceMs; private long _ownSendMs = long.MinValue / 4; private BusyStale _logged; /// The count judged a leftover (0 = none). public int Leftover => _leftover; public void Observe(int count, bool animating, bool? botWaiting, long now) => Observe(count, animating, botWaiting, null, now); /// RynthAi's own action is unanswered (null = it can't say). public void Observe(int count, bool animating, bool? botWaiting, bool? botPaced, long now) { bool paced = botPaced == true; if (paced && !_paced) _pacedSinceMs = now; _paced = paced; if (count < 0) count = 0; if (!_started) { _started = true; _count = count; _countSinceMs = now; _animating = animating; _animatingSinceMs = now; _bot = botWaiting; _botSinceMs = now; } if (count != _count) { _count = count; _countSinceMs = now; } if (_leftover > count) _leftover = count; // part of it was answered or cleared: what is left is the leftover if (count == 0) _logged &= ~BusyStale.Count; if (animating != _animating) { _animating = animating; _animatingSinceMs = now; if (!animating) _logged &= ~BusyStale.Animation; } if (botWaiting != _bot) { _bot = botWaiting; _botSinceMs = now; if (botWaiting != true) _logged &= ~BusyStale.Bot; } } /// Our own use or cast went out now. public void NoteOwnSend(long now) => _ownSendMs = now; /// /// The count as it reads now is a leftover (after the player's clear: what the reset didn't take holds /// this press no more). Only a count above it holds again. /// public void AcceptLeftover() { if (_count > _leftover) _leftover = _count; } public BusyFacts Facts(long now) => Facts(now, false); /// RynthAi holds for the press (its PacedAction bit is meaningful). public BusyFacts Facts(long now, bool pacedKnown) => new( _count, now - _countSinceMs, _leftover, now - _ownSendMs, _animating, _animating ? now - _animatingSinceMs : 0, _bot, now - _botSinceMs, pacedKnown ? _paced : null, pacedKnown && _paced ? now - _pacedSinceMs : 0); /// /// What holds a use back now. A count found to be a leftover becomes the new leftover; /// names the leftovers not reported before (log them once). /// public BusyHold Judge(long now, out BusyStale newlyIgnored) => Judge(now, false, out newlyIgnored); /// RynthAi holds for the press (RynthAi.Yield): its own action is what it says. public BusyHold Judge(long now, bool pacedKnown, out BusyStale newlyIgnored) { BusyHold hold = BusyRule.Decide(Facts(now, pacedKnown), out BusyStale stale); if ((stale & BusyStale.Count) != 0 && _count > _leftover) { _leftover = _count; _logged &= ~BusyStale.Count; // a new leftover is worth its own line } newlyIgnored = stale & ~_logged; _logged |= stale; return hold; } /// The words for the log line when a leftover is first ignored. public string Describe(BusyStale what, long now) => Describe(what, now, false); public string Describe(BusyStale what, long now, bool pacedKnown) { BusyFacts f = Facts(now, pacedKnown); var parts = new System.Collections.Generic.List(3); if ((what & BusyStale.Count) != 0) parts.Add(f.BotPaced != null ? $"the client's busy count reads {f.Count} with nothing of ours or RynthAi's in flight" : $"the client's busy count has read {f.Count} for {f.CountSteadyMs / 1000.0:0.0} s with nothing of ours in flight"); if ((what & BusyStale.Animation) != 0) parts.Add($"the character has read 'animating' for {f.AnimatingMs / 1000.0:0.0} s"); if ((what & BusyStale.Bot) != 0 && f.BotPaced != null) parts.Add($"RynthAi's own action has had no answer for {f.BotPacedMs / 1000.0:0.0} s"); else if ((what & BusyStale.Bot) != 0) parts.Add($"RynthAi has read 'waiting on the server' for {f.BotSteadyMs / 1000.0:0.0} s" + ((what & BusyStale.Count) != 0 || (_logged & BusyStale.Count) != 0 ? " (its copy of the same count)" : "")); return "[RynthDefective] " + string.Join("; ", parts) + ": a leftover (a death, a login or an interrupted action), not waited on. The count itself is left alone."; } public void Reset() { _started = false; _leftover = 0; _paced = false; _logged = BusyStale.None; _ownSendMs = long.MinValue / 4; } }