Pace scroll reads and the stance restore from the read's UseDone

ACE sends "The scroll is destroyed." before the return-to-ready animation and
the UseDone after it (Scroll.ActOnUse, Player.TryUseItem); in a client log they
were ~0.7 s apart, and the next read went ~0.8 s after the UseDone.

- The first UseDone after a read's send is that read's. The next read goes
  250 ms after it; with no UseDone seen, 1.5 s after the read's end as before.
- The stance restore goes at the last read's UseDone when no scroll is pending,
  instead of 3.5 s after the read; a pending scroll keeps the settle wait.
- Tests: ReadGapFromUseDone, StanceRestoredAtLastUseDone.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Daniel Wallace 2026-10-12 16:20:31 -07:00
commit bb8ac3e60f
2 changed files with 118 additions and 3 deletions

View file

@ -84,8 +84,14 @@ internal sealed class ScrollLearner
internal const long ArrivalGraceMs = 15_000;
/// <summary>Read animation + 1 s read + return to ready is ~3-4 s on ACE; give up after this.</summary>
internal const long ReadTimeoutMs = 8_000;
/// <summary>Pause between two reads.</summary>
/// <summary>
/// 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.
/// </summary>
internal const long ReadGapMs = 1_500;
/// <summary>Pause between two reads, from the last read's UseDone (the server is free then).</summary>
internal const long ReadGapAfterUseDoneMs = 250;
/// <summary>How often the pack is looked through for unknown scrolls.</summary>
internal const long PackSweepMs = 5_000;
/// <summary>A scroll approved for looting holds its spell this long (one copy per spell).</summary>
@ -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;
}
/// <summary>Notes the first UseDone after the last read's send (the server's end of that read).</summary>
private void PollReadUseDone(long now)
{
if (!_readUseDoneWatch) return;
try
{
if (unchecked(_host.GetUseDoneSeq() - _readUseDoneSeqAtSend) > 0)
{
_readUseDoneWatch = false;
_readUseDoneAt = now;
}
}
catch { _readUseDoneWatch = false; }
}
/// <summary>The last read's UseDone was seen (after its chat answer, or at any time for this read).</summary>
private bool LastReadUseDoneSeen => _readUseDoneAt != 0;
/// <summary>
/// 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).
/// </summary>
private bool ReadGapOver(long now) =>
(LastReadUseDoneSeen && now - _readUseDoneAt >= ReadGapAfterUseDoneMs)
|| now - _lastReadEndedAt >= ReadGapMs;
/// <summary>True when the read in flight is over (learned, refused, or timed out).</summary>
private bool ResolveRead(long now)
{

View file

@ -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.") ──
/// <summary>As FinishARead, with the read taking ~2 s like ACE's (stance, read motion, 1 s read).</summary>
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()