diff --git a/Plugins/RynthCore.Plugin.RynthAi/Combat/BuffCastChain.cs b/Plugins/RynthCore.Plugin.RynthAi/Combat/BuffCastChain.cs index 8ce2274..d245173 100644 --- a/Plugins/RynthCore.Plugin.RynthAi/Combat/BuffCastChain.cs +++ b/Plugins/RynthCore.Plugin.RynthAi/Combat/BuffCastChain.cs @@ -19,6 +19,9 @@ // * the very next UseDone after it is the last one (no other UseDone since) and its // error is 0; // * that UseDone was seen less than WindowMs ago; +// * no movement call reached the client since the buff went out (MoveAudit.MovementSends +// unchanged): a stop, a start or a turn starts a motion the client still has to play, so +// the next buff waits on the animation gate for it (a buff went out 50 ms after a nav stop); // * no refusal has turned the fast path off for this buff pass. // Every other gate stays: the pacer's outstanding-action gate, the pending cast, the // server-resolution wait, the busy count, the cast interval, the stance (EnsureMagicMode), @@ -50,6 +53,8 @@ internal sealed class BuffCastChain private int _seqAtSend; private long _startedAfter; private long _attacksAfter; + private long _movesAfter; + private bool _moveCounted; // MovementWaits counted for this send private bool _wasFast; private bool _heldForAnimation; // the next buff goes through the animation gate (HoldForAnimation) private bool _answerSeen; // the first UseDone after it was read @@ -68,6 +73,8 @@ internal sealed class BuffCastChain public int FastCasts { get; private set; } public int BuffCasts { get; private set; } public int PassesSwitchedOff { get; private set; } + /// Times the fast path was refused because a movement call went out after the last buff. + public int MovementWaits { get; private set; } /// True when a refusal turned the fast path off for the rest of this pass. public bool OffThisPass => _offThisPass; @@ -94,14 +101,17 @@ internal sealed class BuffCastChain } /// A buff cast went out. is the UseDone count read before the send; - /// / are ActionPacer's counts read after it. - public void Sent(int spellId, string name, int seqAtSend, long started, long attacks, bool fast) + /// / are ActionPacer's counts and + /// MoveAudit.MovementSends, all read after it. + public void Sent(int spellId, string name, int seqAtSend, long started, long attacks, long moves, bool fast) { _spellId = spellId; _name = name; _seqAtSend = seqAtSend; _startedAfter = started; _attacksAfter = attacks; + _movesAfter = moves; + _moveCounted = false; _wasFast = fast; _heldForAnimation = false; _answerSeen = false; @@ -173,7 +183,7 @@ internal sealed class BuffCastChain /// May the next buff skip the animation gate? says why it may (for the log). /// The caller has checked the setting, the pacing and the vitals. /// - public bool Eligible(long started, long attacks, bool hasLast, int lastSeq, uint lastErr, int seqNow, DateTime now, out string why) + public bool Eligible(long started, long attacks, long moves, bool hasLast, int lastSeq, uint lastErr, int seqNow, DateTime now, out string why) { why = ""; if (_offThisPass || _heldForAnimation || _spellId == 0 || !_answerSeen || !_answerOk) return false; @@ -181,11 +191,16 @@ internal sealed class BuffCastChain if (!hasLast || lastSeq != _seqAtSend + 1 || seqNow != lastSeq || lastErr != 0) return false; // another UseDone since double age = (now - _answerSeenAt).TotalMilliseconds; if (age > WindowMs) return false; + if (moves != _movesAfter) // a stop, start or turn went out since: its motion must play first + { + if (!_moveCounted) { _moveCounted = true; MovementWaits++; } + return false; + } why = $"'{_name}' answered UseDone(0) {age:0} ms ago"; return true; } public string Describe() => $"Buff chain: {FastCasts} of {BuffCasts} buff casts on the fast path; refusals client {ClientRefusals}, " + - $"server {ServerRefusals}, too-busy lines {TooBusyLines}; passes switched off {PassesSwitchedOff}"; + $"server {ServerRefusals}, too-busy lines {TooBusyLines}; passes switched off {PassesSwitchedOff}; movement waits {MovementWaits}"; } diff --git a/Plugins/RynthCore.Plugin.RynthAi/Combat/BuffManager.cs b/Plugins/RynthCore.Plugin.RynthAi/Combat/BuffManager.cs index f3a525f..0983a71 100644 --- a/Plugins/RynthCore.Plugin.RynthAi/Combat/BuffManager.cs +++ b/Plugins/RynthCore.Plugin.RynthAi/Combat/BuffManager.cs @@ -2625,7 +2625,7 @@ public class BuffManager : IDisposable if (castOk) { NoteCastIssued(spellId); - _chain.Sent(spellId, spellInfo?.Name ?? buffBaseName, seqAtSend, ActionPacer.Started, ActionPacer.AttacksSent, fast); + _chain.Sent(spellId, spellInfo?.Name ?? buffBaseName, seqAtSend, ActionPacer.Started, ActionPacer.AttacksSent, MoveAudit.MovementSends, fast); } else { @@ -4240,8 +4240,11 @@ public class BuffManager : IDisposable if (!_settings.FastBuffChain || !_settings.EnableBuffing || !ActionPacer.Enabled) return false; if (!_host.HasUseDoneSeq || CurrentCombatMode != CombatMode.Magic) return false; if (!VitalsClearOfEveryLine()) return false; + // A held stop is still to be sent: CastSpellFor would flush it inside this very cast (after the + // movement count the chain compares), so the buff waits for the animation gate instead. + if (MoveAudit.HasPendingStop) return false; bool hasLast = _host.TryGetLastUseDone(out int lastSeq, out uint lastErr); - return _chain.Eligible(ActionPacer.Started, ActionPacer.AttacksSent, hasLast, lastSeq, lastErr, + return _chain.Eligible(ActionPacer.Started, ActionPacer.AttacksSent, MoveAudit.MovementSends, hasLast, lastSeq, lastErr, _host.GetUseDoneSeq(), Now, out _fastChainWhy); } diff --git a/Plugins/Shared/MoveAudit.cs b/Plugins/Shared/MoveAudit.cs index 15ecdce..4b4b9df 100644 --- a/Plugins/Shared/MoveAudit.cs +++ b/Plugins/Shared/MoveAudit.cs @@ -45,6 +45,14 @@ internal static class MoveAudit private static string _lastKey = ""; private static long _lastWrittenMs = long.MinValue; private static int _repeats; + + // Every movement call that reached the client (a start, a stop, a turn, a held stop sent later). + private static long _movementSends; + + /// Movement calls that reached the client so far: a start, a stop, a turn, or a held stop + /// sent by FlushPendingStops. Only ever grows (Reset aside); a reader compares two values to know + /// whether anything moved in between (BuffCastChain: no fast-path buff right after a movement change). + internal static long MovementSends { get { lock (Gate) return _movementSends; } } private static string _repeatLastArg = ""; private static long _repeatFirstMs; @@ -54,7 +62,7 @@ internal static class MoveAudit if (HeldForAction(host, who, on ? "SetAutoRun(True)" : "SetAutoRun(False)", isStop: !on, deferAs: on ? null : PendingStop.AutoRunOff())) return false; 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; } + lock (Gate) { _autoRunOn = on; if (on) _lastStartMs = NowMs(); else _pendingAutoRunOff = null; _movementSends++; } return host.SetAutoRun(on); } @@ -62,7 +70,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 + lock (Gate) { _autoRunOn = false; _motionsOn.Clear(); ClearPendingLocked(); _movementSends++; } // clears everything AC has on return host.StopCompletely(); } @@ -72,7 +80,7 @@ internal static class MoveAudit if (HeldForAction(host, who, $"SetMotion(0x{motion:X8}, {(on ? "True" : "False")})", isStop: !on, motion, on ? null : PendingStop.MotionOff(motion))) return false; 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); } } + lock (Gate) { if (on) { _motionsOn.Add(motion); _lastStartMs = NowMs(); } else { _motionsOn.Remove(motion); _pendingMotionOff.Remove(motion); } _movementSends++; } return host.SetMotion(motion, on); } @@ -82,7 +90,7 @@ internal static class MoveAudit if (HeldForAction(host, who, $"DoMovement(0x{motion:X8})", isStop: false)) return false; FlushPendingStops(host, "before a start"); Note(host, who, "DoMovement", $"0x{motion:X8}, {speed:0.##}, {holdKey}", collapseByKindOnly: false); - lock (Gate) { _motionsOn.Add(motion); _lastStartMs = NowMs(); } + lock (Gate) { _motionsOn.Add(motion); _lastStartMs = NowMs(); _movementSends++; } return host.DoMovement(motion, speed, holdKey); } @@ -256,7 +264,7 @@ internal static class MoveAudit if (host.HasStopCompletely) { host.StopCompletely(); - lock (Gate) { _autoRunOn = false; _motionsOn.Clear(); _deferredStopsSent++; } + lock (Gate) { _autoRunOn = false; _motionsOn.Clear(); _deferredStopsSent++; _movementSends++; } sent.Add($"StopCompletely() for {complete}"); } else @@ -269,21 +277,21 @@ internal static class MoveAudit if (autoRunOff != null) { host.SetAutoRun(false); - lock (Gate) { _autoRunOn = false; _deferredStopsSent++; } + lock (Gate) { _autoRunOn = false; _deferredStopsSent++; _movementSends++; } sent.Add($"SetAutoRun(False) for {autoRunOff}"); } foreach (var m in motionOff) { if (!host.HasSetMotion) break; host.SetMotion(m.Key, false); - lock (Gate) { _motionsOn.Remove(m.Key); _deferredStopsSent++; } + lock (Gate) { _motionsOn.Remove(m.Key); _deferredStopsSent++; _movementSends++; } sent.Add($"SetMotion(0x{m.Key:X8}, False) for {m.Value}"); } foreach (var s in stopMovement) { if (!host.HasStopMovement) break; host.StopMovement(s.Key, s.Value.holdKey); - lock (Gate) { _motionsOn.Remove(s.Key); _deferredStopsSent++; } + lock (Gate) { _motionsOn.Remove(s.Key); _deferredStopsSent++; _movementSends++; } sent.Add($"StopMovement(0x{s.Key:X8}) for {s.Value.who}"); } } @@ -331,7 +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); } + lock (Gate) { _motionsOn.Remove(motion); _pendingStopMovement.Remove(motion); _movementSends++; } 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); + lock (Gate) _movementSends++; return host.TurnToHeading(headingDegrees); } @@ -400,6 +409,7 @@ internal static class MoveAudit ClearPendingLocked(); _pendingSinceMs = 0; _deferredStopsSent = 0; + _movementSends = 0; } } } diff --git a/Tools/RynthCore.RynthAiHostTests/Tests/BuffChainTests.cs b/Tools/RynthCore.RynthAiHostTests/Tests/BuffChainTests.cs index e3aa3f0..a6305d8 100644 --- a/Tools/RynthCore.RynthAiHostTests/Tests/BuffChainTests.cs +++ b/Tools/RynthCore.RynthAiHostTests/Tests/BuffChainTests.cs @@ -29,8 +29,64 @@ internal static class BuffChainTests r.Add("buff chain: a refused fast-path cast turns it off for the rest of the pass; refusals counted and summed up", RefusalFallsBack); r.Add("buff chain: FastBuffChain defaults on, loads on from an old profile and copies", SettingDefaults); r.Add("buff chain: no Stamina to Mana for the mana reserve on the fast path; it waits for the animation, then goes", ManaReserveWaitsForAnimation); + r.Add("buff chain: a stop or turn after the last buff sends the next one through the animation gate (no buff 50 ms after a nav stop); no movement = fast path", MovementWaitsForAnimation); } + // A buff went out 50 ms after StopCompletelyBy("Nav"): the stop starts a motion the client + // still has to play, so a movement call after the last buff turns the fast path off for the + // next buff, which then waits on the animation gate like any other. + private static void MovementWaitsForAnimation() => Paced(() => + { + // Control: the same sequence with no movement in between takes the fast path. + var c = FirstBuffOut(); + c.ServerRuns(0); + c.Tick(1.55); + Check.Eq(FakeHost.Casts.Count, 2, "no movement: the next buff goes on the UseDone(0)"); + Check.Eq(c.B.Chain.FastCasts, 1, "no movement: on the fast path"); + Check.Eq(c.B.Chain.MovementWaits, 0, "no movement: no movement wait"); + + var r = FirstBuffOut(); + r.ServerRuns(0); + r.At(1.5); + Check.True(r.Host.StopCompletelyBy("Nav"), "nav's stop goes out"); + r.Tick(1.55); + Check.Eq(FakeHost.Casts.Count, 1, "50 ms after the stop: the next buff waits for the animation"); + Check.Eq(r.B.Chain.FastCasts, 0, "not on the fast path"); + Check.Eq(r.B.Chain.MovementWaits, 1, "one movement wait counted"); + Check.True(Logged("[Pace] Buff waits: the character is still animating"), "held by the animation gate"); + r.Tick(1.65); + Check.Eq(r.B.Chain.MovementWaits, 1, "counted once per buff, not per tick"); + FakeHost.CastBusy = 0; + r.Tick(1.75); + Check.Eq(FakeHost.Casts.Count, 2, "the animation over: the buff goes through the normal gate"); + Check.Eq(r.B.Chain.FastCasts, 0, "still nothing on the fast path"); + + // A turn counts the same as a stop. + var t = FirstBuffOut(); + t.ServerRuns(0); + t.At(1.5); + Check.True(t.Host.TurnToHeadingBy("Nav", 90f), "nav's turn goes out"); + t.Tick(1.55); + Check.Eq(FakeHost.Casts.Count, 1, "50 ms after a turn: the next buff waits for the animation"); + Check.Eq(t.B.Chain.MovementWaits, 1, "a movement wait"); + + // Stops sent while the buff is still unanswered are held (MoveAudit pending stops), not sent: + // no movement is counted yet, and the next cast would flush them inside itself. A held stop + // keeps the fast path off; the buff waits for the animation. + var h = FirstBuffOut(); + h.At(0.5); + for (int i = 0; i < 5; i++) h.Host.StopCompletelyBy("Nav"); + Check.True(MoveAudit.HasPendingStop, "the stops are held while the buff is unanswered"); + h.ServerRuns(0); + h.Tick(1.55); + Check.Eq(FakeHost.Casts.Count, 1, "a held stop: the next buff waits for the animation"); + Check.Eq(h.B.Chain.FastCasts, 0, "a held stop: not on the fast path"); + FakeHost.CastBusy = 0; + h.Tick(1.75); + Check.Eq(FakeHost.Casts.Count, 2, "the animation over: the buff goes"); + Check.Eq(h.B.Chain.FastCasts, 0, "and not on the fast path"); + }); + // The fast path skips the animation gate, so only the buff may ride it. Mana spent by the last // buff can leave the next one under the mana reserve (BuffWaitsForMana): Stamina to Mana is then // cast first, and it must wait for the recoil like any vital cast.