diff --git a/Plugins/RynthCore.Plugin.RynthAi/Loot/ScrollLearner.cs b/Plugins/RynthCore.Plugin.RynthAi/Loot/ScrollLearner.cs index 2332fb1..5f85e98 100644 --- a/Plugins/RynthCore.Plugin.RynthAi/Loot/ScrollLearner.cs +++ b/Plugins/RynthCore.Plugin.RynthAi/Loot/ScrollLearner.cs @@ -84,8 +84,14 @@ internal sealed class ScrollLearner internal const long ArrivalGraceMs = 15_000; /// Read animation + 1 s read + return to ready is ~3-4 s on ACE; give up after this. internal const long ReadTimeoutMs = 8_000; - /// Pause between two reads. + /// + /// Pause between two reads, from the read's end (its chat answer), when the read's UseDone is not + /// seen. ACE sends "The scroll is destroyed." before the return-to-ready animation and the UseDone + /// after it (Scroll.ActOnUse, Player.TryUseItem): about 0.7 s apart in a client log. + /// internal const long ReadGapMs = 1_500; + /// Pause between two reads, from the last read's UseDone (the server is free then). + internal const long ReadGapAfterUseDoneMs = 250; /// How often the pack is looked through for unknown scrolls. internal const long PackSweepMs = 5_000; /// A scroll approved for looting holds its spell this long (one copy per spell). @@ -146,6 +152,11 @@ internal sealed class ScrollLearner private string _chatText = ""; private bool _readItemDeleted; + // The UseDone of the last read sent: the first UseDone after its send. 0 = not seen (yet). + private bool _readUseDoneWatch; + private int _readUseDoneSeqAtSend; + private long _readUseDoneAt; + public ScrollLearner(RynthCoreHost host, LegacyUiSettings settings, WorldObjectCache? cache) { _host = host; @@ -352,6 +363,7 @@ internal sealed class ScrollLearner public bool Tick(bool safe, int busyCount) { long now = NowMs(); + PollReadUseDone(now); if (IsReading) { if (ResolveRead(now)) return false; @@ -376,7 +388,7 @@ internal sealed class ScrollLearner } if (!safe || busyCount > 0 || _pending.Count == 0) return false; - if (now - _lastReadEndedAt < ReadGapMs) return false; + if (!ReadGapOver(now)) return false; if (inv == null) { if (now - _lastStartCheckAt < StartCheckMs) return false; // one pack walk a second at most @@ -466,7 +478,10 @@ internal sealed class ScrollLearner if (_settings.PeaceModeWhenIdle) { CloseSeries(""); return; } // switched on since: Peace is wanted long age = now - _lastReadEndedAt; if (_restoreMode == 0) { if (age >= RestoreSettleMs) CloseSeries(""); return; } - if (age < RestoreSettleMs) return; // the next scroll may be about to start + // The next scroll may be about to start: wait the settle time, unless the last read's UseDone + // is in and no scroll is pending (nothing left to read, so the stance can go back now). + bool lastReadDone = LastReadUseDoneSeen && _pending.Count == 0; + if (age < RestoreSettleMs && !lastReadDone) return; if (age > RestoreSettleMs + RestoreGiveUpMs) { CloseSeries("too late"); return; } int cur = CurrentMode(); if (cur == _restoreMode) { CloseSeries(""); return; } // back in stance @@ -593,6 +608,9 @@ internal sealed class ScrollLearner _chatText = ""; _readItemDeleted = false; + int seqBefore = 0; + bool haveSeq = false; + try { if (_host.HasUseDoneSeq) { seqBefore = _host.GetUseDoneSeq(); haveSeq = true; } } catch { } bool sent = false; try { sent = _host.UseFor(unchecked((uint)pickId), "Learn", $"read the scroll to learn {j.SpellName} ({j.Why})"); } catch { } @@ -603,10 +621,39 @@ internal sealed class ScrollLearner else pp.NotBefore = now + 3_000; return false; } + _readUseDoneWatch = haveSeq; + _readUseDoneSeqAtSend = seqBefore; + _readUseDoneAt = 0; LogLine($"[Learn] reading '{pick.Name}' ({j.SpellName}: {j.Why})"); return true; } + /// Notes the first UseDone after the last read's send (the server's end of that read). + private void PollReadUseDone(long now) + { + if (!_readUseDoneWatch) return; + try + { + if (unchecked(_host.GetUseDoneSeq() - _readUseDoneSeqAtSend) > 0) + { + _readUseDoneWatch = false; + _readUseDoneAt = now; + } + } + catch { _readUseDoneWatch = false; } + } + + /// The last read's UseDone was seen (after its chat answer, or at any time for this read). + private bool LastReadUseDoneSeen => _readUseDoneAt != 0; + + /// + /// May the next read start? ReadGapAfterUseDoneMs after the last read's UseDone, or ReadGapMs + /// after its end when no UseDone is seen (an engine without UseDone, or a lost one). + /// + private bool ReadGapOver(long now) => + (LastReadUseDoneSeen && now - _readUseDoneAt >= ReadGapAfterUseDoneMs) + || now - _lastReadEndedAt >= ReadGapMs; + /// True when the read in flight is over (learned, refused, or timed out). private bool ResolveRead(long now) { diff --git a/Tools/RynthCore.RynthAiHostTests/Tests/LearnSpellsTests.cs b/Tools/RynthCore.RynthAiHostTests/Tests/LearnSpellsTests.cs index 729aa37..79a9420 100644 --- a/Tools/RynthCore.RynthAiHostTests/Tests/LearnSpellsTests.cs +++ b/Tools/RynthCore.RynthAiHostTests/Tests/LearnSpellsTests.cs @@ -67,6 +67,8 @@ internal static class LearnSpellsTests r.Add("learn: a restore that never lands is tried 3 times, then given up", StanceNeverLands); r.Add("learn: Reset and the macro stopping close the series", StanceResetAndMacroStop); r.Add("learn: settings - on by default, old files keep it on, /vt opt and metas reach ReadUnknownScrolls", Settings); + r.Add("learn: the gap to the next read counts from the last read's UseDone (no UseDone: 1.5 s from its end)", ReadGapFromUseDone); + r.Add("learn: the stance goes back at the last read's UseDone when no scroll is pending; a pending scroll waits", StanceRestoredAtLastUseDone); } // ── World ───────────────────────────────────────────────────────────────── @@ -494,6 +496,72 @@ internal static class LearnSpellsTests Done(); } + // ── Read pacing from UseDone (UseDone ~0.7 s after "The scroll is destroyed.") ── + + /// As FinishARead, with the read taking ~2 s like ACE's (stance, read motion, 1 s read). + private static void FinishARealRead(ScrollLearner l, Clock c) + { + TickUntilRead(l, c); + Check.True(l.IsReading, "a read started"); + FakeHost.CombatModeValue = CombatMode.NonCombat; + c.Now += 1_900; + Learned(); + l.OnChat("The scroll is destroyed."); + c.Now += 100; + l.Tick(true, 0); + Check.False(l.IsReading, "the read is over"); + } + private static void ReadGapFromUseDone() + { + // UseDone seen: the next read goes 250 ms after it, well inside the old 1.5 s from the chat. + var l = StanceRig(CombatMode.NonCombat, false, 2, out var c, out _, () => FakeHost.ServerGates = true); + FinishARealRead(l, c); // chat answer at t, ticked at t+100 + c.Now += 600; FakeHost.UseDone(0); // the server's UseDone ~0.7 s after the chat + l.Tick(true, 0); + Check.False(l.IsReading, "not at the UseDone itself"); + c.Now += ScrollLearner.ReadGapAfterUseDoneMs; + l.Tick(true, 0); + Check.True(l.IsReading, "the next read starts 250 ms after the UseDone (950 ms after the chat)"); + Check.Eq(FakeHost.Uses.Count, 2, "two reads sent"); + Done(); + + // No UseDone (an older engine or a lost one): 1.5 s from the read's end, as before. + var n = StanceRig(CombatMode.NonCombat, false, 2, out var cn, out _, () => FakeHost.ServerGates = true); + FinishARealRead(n, cn); + cn.Now += 850; n.Tick(true, 0); + Check.False(n.IsReading, "no UseDone: not at 950 ms"); + cn.Now += 700; n.Tick(true, 0); + Check.True(n.IsReading, "no UseDone: 1.55 s after the read ended (its chat, seen on the next tick)"); + + // A UseDone that came before the read's send does not count for it. + var o = StanceRig(CombatMode.NonCombat, false, 2, out var co, out _, () => FakeHost.ServerGates = true); + FakeHost.UseDone(0); // an older action's answer + FinishARealRead(o, co); + co.Now += 850; o.Tick(true, 0); + Check.False(o.IsReading, "an earlier UseDone is not this read's"); + Done(); + } + + private static void StanceRestoredAtLastUseDone() + { + // The last scroll: the stance goes back at its UseDone, not 3.5 s after the chat. + var l = StanceRig(CombatMode.Magic, false, 1, out var c, out _, () => FakeHost.ServerGates = true); + FinishARealRead(l, c); + l.Tick(true, 0); + Check.Eq(FakeHost.CombatModeRequests.Count, 0, "not before the UseDone"); + c.Now += 600; FakeHost.UseDone(0); + l.Tick(true, 0); + Check.Eq(string.Join(",", FakeHost.CombatModeRequests), CombatMode.Magic.ToString(), "Magic requested at the UseDone"); + Done(); + + // Another scroll is pending: no restore at the first read's UseDone; that read comes next. + var s = StanceRig(CombatMode.Magic, false, 2, out var cs, out _, () => FakeHost.ServerGates = true); + FinishARealRead(s, cs); + cs.Now += 600; FakeHost.UseDone(0); + s.Tick(true, 0); + Check.Eq(FakeHost.CombatModeRequests.Count, 0, "a scroll is pending: no restore at the UseDone"); + Done(); + } // ── Tests ───────────────────────────────────────────────────────────────── private static void AceRule()