RynthAi: a refused combat mode change is not a sent request
ChangeCombatModeFor called host.ChangeCombatMode, began a paced action only on success, and then returned true either way. BuffManager and the Combat stance paths read that true as a sent request: they advanced their attempt clocks, counted a failed try and logged a retry. With the engine turning non-teardown actions away around a teleport or a death, chained refusals could reach NoWandFlipMaxFails and set _magicModeBlocked / _wandSwapExhausted. - ChangeCombatModeFor returns false on a refusal, without Begin. The other *For helpers already return the host result. The no-ChangeCombatMode host case still returns true (nothing to send or pace). - Callers stamp their bookkeeping only on true: BuffManager peace-for-kit paths (_lastCastAttempt, _healWaitKind); CorpseOpenController's peace before an open and MissileCraftingManager's peace before the first combine are asked again until accepted, inside their 2.5 s ceiling counted from the first ask; the stuck-stance clocks (BuffManager, CombatManager) start only once a stance request has gone out; ExpressionEngine's cast-stance flip throttle; the CombatManager stall-watchdog toggle, whose ladder step is taken back (CombatStallWatchdog.Unfire) when the toggle is refused. - ActionPacer tracks a streak of consecutive engine refusals (one log line when it starts, one when an accepted call ends it). After EngineRefusalFailOpenMs (30 s) ChangeCombatModeFor returns true again, so a permanent refusal (missing hook, full ring) is counted by the callers' own failure paths instead of retrying forever. A gap of more than RefuseGapMs (10 s) starts a new streak; Reset() and Forget() end it. - ActionPacer.LastEngineRefusalMs: a refusal newer than a stuck-stance clock's start restarts that clock, so a request accepted and then dropped by a teleport does not end in a re-equip UseObject on the wielded weapon. Tests: FakeHost.CombatModeRefuse; ActionPacingTests (refusals burn no try or backoff, streak, fail-open, expiry and Forget, stuck clock restart, 20 s no-wand loop never reaches _magicModeBlocked, peace-before-open retry, MissileCraftingManager), WeaponTests (equip pass keeps its backoff), CombatHookTests, CombatStallWatchdogTests (Unfire). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
2d8bebb0f4
commit
2e8258b079
12 changed files with 570 additions and 34 deletions
|
|
@ -144,6 +144,10 @@ internal static unsafe class FakeHost
|
|||
/// <summary>Opt-in (set before Create): ChangeCombatMode, recorded in <see cref="CombatModeRequests"/>.</summary>
|
||||
public static bool CombatModeCalls;
|
||||
public static readonly List<int> CombatModeRequests = new();
|
||||
/// <summary>True: ChangeCombatMode returns 0 (the engine refused, e.g. around a teleport) and records nothing in <see cref="CombatModeRequests"/>.</summary>
|
||||
public static bool CombatModeRefuse;
|
||||
/// <summary>ChangeCombatMode calls refused while <see cref="CombatModeRefuse"/> was set.</summary>
|
||||
public static int CombatModeRefusals;
|
||||
/// <summary>Opt-in (set before Create): WieldItem (a wield into a named slot, API v70+). The
|
||||
/// host then reports API version 73, so only tests that ask for it see the newer calls.</summary>
|
||||
public static bool WieldCalls;
|
||||
|
|
@ -227,7 +231,7 @@ internal static unsafe class FakeHost
|
|||
SelectedItem = 0;
|
||||
UseOnCalls = false; UsesOn.Clear();
|
||||
PluginCommandCalls = false; PluginCommandDelivered = true; PluginCommands.Clear();
|
||||
CombatModeCalls = false; CombatModeRequests.Clear();
|
||||
CombatModeCalls = false; CombatModeRequests.Clear(); CombatModeRefuse = false; CombatModeRefusals = 0;
|
||||
BusyCalls = false; ForceResets = 0; BusyStateValue = 0;
|
||||
}
|
||||
|
||||
|
|
@ -676,7 +680,7 @@ internal static unsafe class FakeHost
|
|||
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
|
||||
private static int GetCurrentCombatMode() => CombatModeValue;
|
||||
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
|
||||
private static int ChangeCombatMode(int mode) { CombatModeRequests.Add(mode); return 1; }
|
||||
private static int ChangeCombatMode(int mode) { if (CombatModeRefuse) { CombatModeRefusals++; return 0; } CombatModeRequests.Add(mode); return 1; }
|
||||
|
||||
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
|
||||
private static int UseObject(uint id) { Uses.Add(id); return 1; }
|
||||
|
|
|
|||
|
|
@ -54,6 +54,16 @@ internal static class ActionPacingTests
|
|||
r.Add("pacing: no answer - each kind's time limit lets the next action go, logged, and nothing is force-cleared", TimeLimits);
|
||||
r.Add("pacing: heals (a plain heal waits for a corpse use's answer, 300 ms at Emergency, one behind a cast's result, 300 ms after the stance reads)", HealRules);
|
||||
r.Add("pacing (vitals): Drakkon 07:03 - a cast whose result is in waits for its UseDone: one cast, not a too-busy flood", CastWaitsForUseDone);
|
||||
r.Add("refusal: a refused mode change begins no paced action, and the next one is not held by it", RefusedBeginsNothing);
|
||||
r.Add("refusal: the engine refusing for 5 s burns nothing; a streak past 30 s counts as a failure again, with one log line each end", RefusalStreakFailsOpen);
|
||||
r.Add("refusal: 20 s of refusals with no wand never reach _magicModeBlocked; past 30 s the failure counting resumes and the vitals fall to the potion path", NoWandRefusalLoop);
|
||||
r.Add("refusal: wand wielded, 9 s of refused stance changes - no re-equip UseObject, no burnt re-equip; the first accepted request starts the clock", WandWieldedRefusalNoReEquip);
|
||||
r.Add("refusal: a refused peace-before-open is asked again inside the 2.5 s ceiling; exactly one accepted request goes out before the open", PeaceBeforeOpenRefusedRetried);
|
||||
r.Add("refusal: a streak that went quiet for 10 s+ (a death, a logout) is over - 600 s later a refusal starts a fresh streak, and Forget ends one", RefusalStreakExpires);
|
||||
r.Add("refusal: wand wielded, a request accepted then dropped (a teleport), 9 s of refusals - the stuck clock restarts, no re-equip UseObject", StuckClockRestartsOnRefusal);
|
||||
r.Add("refusal: the stall watchdog's refused mode toggle spends no ladder step (Unfire) and is asked again next tick", StallToggleRefusedSpendsNoStep);
|
||||
r.Add("refusal: missile crafting asks for peace until accepted, and combines after the 2.5 s ceiling; already in peace it combines on the first tick with no mode request", CraftingPeaceRefusal);
|
||||
r.Add("pacing (stance): a refused ChangeCombatMode burns no try and no backoff step, and the next tick asks again (pacing on and off)", RefusedStanceBurnsNothing);
|
||||
r.Add("pacing (vitals): a heal waits for an unanswered corpse use's answer, then goes at once; the emergency stance settle is unchanged", HealLatencyKept);
|
||||
r.Add("pacing (looting): one open per server answer, never re-sent on top of an unanswered one", LootOneOpenPerAnswer);
|
||||
r.Add("pacing (looting): Drakkon's stall is still found and said once with pacing on", LootStallStillDetected);
|
||||
|
|
@ -468,6 +478,330 @@ internal static class ActionPacingTests
|
|||
finally { PaceOff(); }
|
||||
}
|
||||
|
||||
private static void RefusedStanceBurnsNothing()
|
||||
{
|
||||
foreach (bool pacing in new[] { true, false })
|
||||
{
|
||||
PaceOff();
|
||||
if (pacing) PaceOn();
|
||||
try
|
||||
{
|
||||
var r = new BuffRig(CombatMode.NonCombat);
|
||||
r.S.EmergencyHealAt = 30; r.S.HealAt = 60; r.S.TopOffHP = 60; r.S.GetManaAt = 0; r.S.TopOffMana = 0;
|
||||
r.V.CurrentHealth = 20; r.V.CurrentMana = 100;
|
||||
var fails = typeof(BuffManager).GetField("_buffStanceConsecutiveFails", Any)!;
|
||||
var last = typeof(BuffManager).GetField("_lastBuffStanceAttempt", Any)!;
|
||||
DateTime lastBefore = (DateTime)last.GetValue(r.B)!;
|
||||
FakeHost.CombatModeRefuse = true; // the engine turns the stance change away (a teleport, a death)
|
||||
|
||||
for (int i = 0; i < 6; i++) { r.At(i * 0.2); r.B.OnHeartbeat(); }
|
||||
Check.Eq(FakeHost.CombatModeRefusals, 6, $"pacing={pacing}: every tick asks again - a refusal is no try and sets no backoff");
|
||||
Check.Eq((int)fails.GetValue(r.B)!, 0, $"pacing={pacing}: _buffStanceConsecutiveFails unchanged");
|
||||
Check.True((DateTime)last.GetValue(r.B)! == lastBefore, $"pacing={pacing}: _lastBuffStanceAttempt unchanged");
|
||||
Check.Eq(FakeHost.CombatModeRequests.Count, 0, $"pacing={pacing}: nothing reached the server");
|
||||
|
||||
FakeHost.CombatModeRefuse = false;
|
||||
r.At(1.4); r.B.OnHeartbeat();
|
||||
Check.Eq(string.Join(",", FakeHost.CombatModeRequests), "8", $"pacing={pacing}: the engine accepts again and the very next tick sends Magic");
|
||||
Check.Eq((int)fails.GetValue(r.B)!, 1, $"pacing={pacing}: only a sent request counts as a try");
|
||||
}
|
||||
finally { PaceOff(); }
|
||||
}
|
||||
}
|
||||
|
||||
private static void RefusedBeginsNothing() => Paced(() =>
|
||||
{
|
||||
var host = World(combatMode: 2);
|
||||
FakeHost.CombatModeRefuse = true;
|
||||
Check.False(host.ChangeCombatModeFor(CombatMode.Magic, "Buff", "refused"), "the engine refused: false");
|
||||
Check.True(ActionPacer.Current(host) == null, "no paced action was begun");
|
||||
Check.True(ActionPacer.MayStart(host, "Buff", HealNeed.None, out _), "so nothing is held behind it");
|
||||
FakeHost.CombatModeRefuse = false;
|
||||
Check.True(host.ChangeCombatModeFor(CombatMode.Magic, "Buff", "accepted"), "accepted: true");
|
||||
Check.True(ActionPacer.Current(host)?.Kind == PacedKind.CombatMode, "and now the stance change is outstanding");
|
||||
});
|
||||
|
||||
private static void RefusalStreakFailsOpen() => Paced(() =>
|
||||
{
|
||||
var host = World(combatMode: 2);
|
||||
FakeHost.CombatModeRefuse = true;
|
||||
for (int s = 0; s <= 5; s++) { _t = 10_000_000 + s * 1000; Check.False(host.ChangeCombatModeFor(CombatMode.Magic, "Buff", "x"), $"{s} s into a refusal streak: false"); }
|
||||
Check.Eq(FakeHost.Logs.Count(l => l.Contains("the engine refused the change")), 1, "one line when the streak starts");
|
||||
FakeHost.CombatModeRefuse = false;
|
||||
_t = 10_000_000 + 6_000;
|
||||
Check.True(host.ChangeCombatModeFor(CombatMode.Magic, "Buff", "x"), "accepted");
|
||||
Check.Eq(FakeHost.Logs.Count(l => l.Contains("the engine accepts mode changes again")), 1, "one line when it ends");
|
||||
FakeHost.UseDone(0); ActionPacer.Reset(); ActionPacer.Enabled = true; ActionPacer.NowMs = () => _t;
|
||||
|
||||
FakeHost.CombatModeRefuse = true;
|
||||
long start = _t + 1000;
|
||||
bool sawOpen = false;
|
||||
for (int s = 0; s <= 40; s++)
|
||||
{
|
||||
_t = start + s * 1000L;
|
||||
bool r = host.ChangeCombatModeFor(CombatMode.Magic, "Buff", "x");
|
||||
if (s <= 30) Check.False(r, $"{s} s into the streak: still false");
|
||||
else sawOpen |= r;
|
||||
}
|
||||
Check.True(sawOpen, "past 30 s a refusal counts as a failure again (true)");
|
||||
Check.Eq(FakeHost.Logs.Count(l => l.Contains("the engine refused the change")), 2, "the second streak logged once");
|
||||
FakeHost.CombatModeRefuse = false;
|
||||
_t += 1000;
|
||||
Check.True(host.ChangeCombatModeFor(CombatMode.Magic, "Buff", "x"), "any accepted call ends the streak");
|
||||
FakeHost.CombatModeRefuse = true;
|
||||
_t += 5000; ActionPacer.Reset(); ActionPacer.Enabled = true; ActionPacer.NowMs = () => _t;
|
||||
Check.False(host.ChangeCombatModeFor(CombatMode.Magic, "Buff", "x"), "a fresh streak starts at false again");
|
||||
});
|
||||
|
||||
private static readonly BindingFlags Priv = BindingFlags.Instance | BindingFlags.NonPublic;
|
||||
|
||||
private static void SetupBuffRigVitals(BuffRig r)
|
||||
{
|
||||
r.S.EmergencyHealAt = 30; r.S.HealAt = 60; r.S.TopOffHP = 60; r.S.GetManaAt = 0; r.S.TopOffMana = 0;
|
||||
r.V.CurrentHealth = 20; r.V.CurrentMana = 100;
|
||||
}
|
||||
|
||||
private static void NoWandRefusalLoop()
|
||||
{
|
||||
PaceOff();
|
||||
try
|
||||
{
|
||||
var r = new BuffRig(CombatMode.NonCombat);
|
||||
SetupBuffRigVitals(r);
|
||||
var blocked = typeof(BuffManager).GetField("_magicModeBlocked", Priv)!;
|
||||
var fails = typeof(BuffManager).GetField("_buffStanceConsecutiveFails", Priv)!;
|
||||
var swapUntil = typeof(BuffManager).GetField("_vitalSwapBlockedUntil", Priv)!;
|
||||
FakeHost.CombatModeRefuse = true;
|
||||
bool everBlocked = false;
|
||||
int ticks = 0;
|
||||
for (double s = 0; s <= 20; s += 0.5) { r.At(s); r.B.OnHeartbeat(); ticks++; everBlocked |= (bool)blocked.GetValue(r.B)!; }
|
||||
Check.True(FakeHost.CombatModeRefusals >= ticks / 2, $"20 s: the stance is asked again throughout ({FakeHost.CombatModeRefusals} refusals in {ticks} heartbeats; other vitals gates skip some ticks)");
|
||||
Check.False(everBlocked, "_magicModeBlocked never set by refusals");
|
||||
Check.Eq((int)fails.GetValue(r.B)!, 0, "no failure counted");
|
||||
Check.True((DateTime)swapUntil.GetValue(r.B)! < DateTime.Now, "and the vitals did not fall through to the potion path");
|
||||
|
||||
// Past 30 s of refusals the call counts again. The backoff gates run on the real clock, so
|
||||
// stand in for three earlier counted tries: the very next counted try (the first past 30 s)
|
||||
// is the fourth, and the one after it finds the flip blocked and hands over to the potion.
|
||||
fails.SetValue(r.B, 3);
|
||||
for (double s = 20.5; s <= 40; s += 0.5) { r.At(s); r.B.OnHeartbeat(); everBlocked |= (bool)blocked.GetValue(r.B)!; }
|
||||
Check.True(everBlocked, "40 s: the failure counting resumed and _magicModeBlocked was reached");
|
||||
Check.True((DateTime)swapUntil.GetValue(r.B)! > DateTime.Now, "the vitals chain reached the potion/kit fallback (swap blocked, retry later)");
|
||||
Check.Eq(FakeHost.Logs.Count(l => l.Contains("the engine refused the change")), 1, "one streak, one line");
|
||||
}
|
||||
finally { PaceOff(); }
|
||||
}
|
||||
|
||||
private static void WandWieldedRefusalNoReEquip()
|
||||
{
|
||||
PaceOff();
|
||||
try
|
||||
{
|
||||
var r = new BuffRig(CombatMode.NonCombat);
|
||||
SetupBuffRigVitals(r);
|
||||
FakeHost.Names[Wand] = "Academy Wand";
|
||||
FakeHost.ItemTypes[Wand] = 0x8000;
|
||||
FakeHost.Ints[(Wand, 10)] = 0x01000000; // CurrentWieldedLocation: in hand
|
||||
r.S.WieldUnlistedWandWhenNoneListed = true;
|
||||
r.B.SetWorldObjectCache(FakeHost.MakeCache(r.Host, Player, new[] { Wand }));
|
||||
var reEquips = typeof(BuffManager).GetField("_buffStanceReEquips", Priv)!;
|
||||
var since = typeof(BuffManager).GetField("_buffStanceStuckSince", Priv)!;
|
||||
FakeHost.CombatModeRefuse = true;
|
||||
r.At(0); r.B.OnHeartbeat();
|
||||
Check.True(FakeHost.CombatModeRefusals >= 1, "set-up: the wielded-wand branch asked for the stance and was refused");
|
||||
Check.True((DateTime)since.GetValue(r.B)! == DateTime.MinValue, "the stuck clock has not started");
|
||||
System.Threading.Thread.Sleep(9_000); // BuffStanceStuckRecoverMs (8 s) runs on the real clock
|
||||
r.At(9); r.B.OnHeartbeat();
|
||||
r.At(9.3); r.B.OnHeartbeat();
|
||||
Check.Eq((int)reEquips.GetValue(r.B)!, 0, "9 s of refusals: _buffStanceReEquips stays 0");
|
||||
Check.False(FakeHost.Uses.Contains(Wand), "and no UseObject(wand) - a MOVE on a wielded wand - went out");
|
||||
Check.True((DateTime)since.GetValue(r.B)! == DateTime.MinValue, "the clock still has not started");
|
||||
|
||||
FakeHost.CombatModeRefuse = false;
|
||||
r.At(9.6); r.B.OnHeartbeat();
|
||||
Check.Eq(string.Join(",", FakeHost.CombatModeRequests), "8", "accepted: the Magic request goes out");
|
||||
Check.True((DateTime)since.GetValue(r.B)! != DateTime.MinValue, "and only now the stuck clock starts");
|
||||
Check.Eq((int)reEquips.GetValue(r.B)!, 0, "still no re-equip");
|
||||
Check.False(FakeHost.Uses.Contains(Wand), "still no UseObject(wand)");
|
||||
}
|
||||
finally { PaceOff(); }
|
||||
}
|
||||
|
||||
private static void PeaceBeforeOpenRefusedRetried()
|
||||
{
|
||||
LootWorld();
|
||||
FakeHost.CombatModeCalls = true;
|
||||
FakeHost.CombatModeValue = CombatMode.Melee;
|
||||
var host = FakeHost.Create(Player);
|
||||
var bot = new Bot(host);
|
||||
var cache = bot.Get<WorldObjectCache>("_objectCache");
|
||||
var cm = new CombatManager(host, bot.S, cache);
|
||||
cm.SetPlayerId(Player);
|
||||
bot.Set("_combatManager", cm);
|
||||
bot.Set("_targetCorpseId", unchecked((int)Corpse));
|
||||
bot.S.PeaceModeWhenIdle = true;
|
||||
bool Wants(long now) => (bool)bot.Call("WantsPeaceBeforeOpen", bot.S, now)!;
|
||||
FakeHost.CombatModeRefuse = true;
|
||||
Check.True(Wants(1_000), "first ask (refused): the open waits");
|
||||
Check.True(Wants(1_500), "asked again (refused): still waiting");
|
||||
Check.Eq(FakeHost.CombatModeRefusals, 2, "both asks reached the engine: the refusal did not use up the one shot");
|
||||
FakeHost.CombatModeRefuse = false;
|
||||
Check.True(Wants(2_000), "accepted within the ceiling: waiting for the swap");
|
||||
Check.True(Wants(2_300), "waiting");
|
||||
Check.True(Wants(3_000), "waiting");
|
||||
Check.Eq(string.Join(",", FakeHost.CombatModeRequests), "1", "exactly one peace request went out");
|
||||
Check.False(Wants(3_600), "the ceiling counts from the FIRST ask (1000 + 2500): the open goes on");
|
||||
Check.Eq(FakeHost.CombatModeRequests.Count, 1, "and still exactly one");
|
||||
}
|
||||
private static void RefusalStreakExpires() => Paced(() =>
|
||||
{
|
||||
var host = World(combatMode: 2);
|
||||
FakeHost.CombatModeRefuse = true;
|
||||
Check.False(host.ChangeCombatModeFor(CombatMode.Magic, "Buff", "x"), "t0: a refusal opens a streak (false)");
|
||||
_t += 600_000; // a death, a logout: ten minutes of nothing, no accepted call in between
|
||||
Check.False(host.ChangeCombatModeFor(CombatMode.Magic, "Buff", "x"), "a refusal 600 s later is a NEW streak (false), not a 600 s streak failing open");
|
||||
Check.Eq(FakeHost.Logs.Count(l => l.Contains("the engine refused the change")), 2, "and it logged its own start line");
|
||||
|
||||
// Refusals closer than the gap are one streak: it still fails open at 30 s.
|
||||
ActionPacer.Reset(); ActionPacer.Enabled = true; ActionPacer.NowMs = () => _t;
|
||||
long start = _t;
|
||||
bool open = false;
|
||||
for (int s = 0; s <= 40; s += 5) { _t = start + s * 1000L; open |= host.ChangeCombatModeFor(CombatMode.Magic, "Buff", "x"); }
|
||||
Check.True(open, "refusals 5 s apart stay one streak and fail open past 30 s");
|
||||
|
||||
// Forget (portal / death / logout) ends the streak too.
|
||||
ActionPacer.Reset(); ActionPacer.Enabled = true; ActionPacer.NowMs = () => _t;
|
||||
// Refusals 9 s apart are one streak (the gap is 10 s). Without the Forget the refusal at +36 s would be 36 s into it and fail open.
|
||||
Check.False(host.ChangeCombatModeFor(CombatMode.Magic, "Buff", "x"), "streak begins");
|
||||
_t += 9_000; Check.False(host.ChangeCombatModeFor(CombatMode.Magic, "Buff", "x"), "+9 s: same streak");
|
||||
ActionPacer.Forget("death");
|
||||
for (int s = 0; s < 3; s++) { _t += 9_000; Check.False(host.ChangeCombatModeFor(CombatMode.Magic, "Buff", "x"), $"after Forget a fresh streak, refusal {s + 1} (false)"); }
|
||||
Check.True(ActionPacer.LastEngineRefusalMs > 0, "LastEngineRefusalMs records the newest refusal");
|
||||
ActionPacer.Reset();
|
||||
Check.Eq(ActionPacer.LastEngineRefusalMs, 0L, "Reset clears LastEngineRefusalMs");
|
||||
});
|
||||
|
||||
private static void StuckClockRestartsOnRefusal()
|
||||
{
|
||||
PaceOff();
|
||||
try
|
||||
{
|
||||
var r = new BuffRig(CombatMode.NonCombat);
|
||||
SetupBuffRigVitals(r);
|
||||
FakeHost.Names[Wand] = "Academy Wand";
|
||||
FakeHost.ItemTypes[Wand] = 0x8000;
|
||||
FakeHost.Ints[(Wand, 10)] = 0x01000000; // in hand
|
||||
r.S.WieldUnlistedWandWhenNoneListed = true;
|
||||
r.B.SetWorldObjectCache(FakeHost.MakeCache(r.Host, Player, new[] { Wand }));
|
||||
var reEquips = typeof(BuffManager).GetField("_buffStanceReEquips", Priv)!;
|
||||
var since = typeof(BuffManager).GetField("_buffStanceStuckSince", Priv)!;
|
||||
var lastTry = typeof(BuffManager).GetField("_lastBuffStanceAttempt", Priv)!;
|
||||
|
||||
r.At(0); r.B.OnHeartbeat();
|
||||
Check.Eq(string.Join(",", FakeHost.CombatModeRequests), "8", "set-up: the first request is accepted and starts the stuck clock");
|
||||
Check.True((DateTime)since.GetValue(r.B)! != DateTime.MinValue, "set-up: the clock is running");
|
||||
|
||||
// The request was dropped by a teleport; the engine now refuses. The backoff gate (real clock) is opened by back-dating.
|
||||
lastTry.SetValue(r.B, DateTime.Now.AddSeconds(-3));
|
||||
FakeHost.CombatModeRefuse = true;
|
||||
r.At(2); r.B.OnHeartbeat();
|
||||
Check.True(FakeHost.CombatModeRefusals >= 1, "set-up: a later request was refused");
|
||||
|
||||
// 9 s of that: back-date the clock as if the real time had passed (the re-equip rule reads the real clock).
|
||||
since.SetValue(r.B, DateTime.Now.AddSeconds(-9));
|
||||
r.At(9); r.B.OnHeartbeat();
|
||||
r.At(9.3); r.B.OnHeartbeat();
|
||||
Check.Eq((int)reEquips.GetValue(r.B)!, 0, "a refusal newer than the clock's start restarted it: _buffStanceReEquips stays 0");
|
||||
Check.False(FakeHost.Uses.Contains(Wand), "and no UseObject(wand) - a MOVE on a wielded wand - went out");
|
||||
Check.True((DateTime.Now - (DateTime)since.GetValue(r.B)!).TotalSeconds < 3, "the clock was restarted at now");
|
||||
|
||||
FakeHost.CombatModeRefuse = false;
|
||||
r.At(9.6); r.B.OnHeartbeat();
|
||||
Check.Eq(string.Join(",", FakeHost.CombatModeRequests), "8,8", "accepted again");
|
||||
Check.Eq((int)reEquips.GetValue(r.B)!, 0, "still no re-equip");
|
||||
Check.False(FakeHost.Uses.Contains(Wand), "still no UseObject(wand)");
|
||||
}
|
||||
finally { PaceOff(); }
|
||||
}
|
||||
|
||||
private static void StallToggleRefusedSpendsNoStep()
|
||||
{
|
||||
PaceOff();
|
||||
var host = World(combatMode: 2);
|
||||
var s = new LegacyUiSettings { BotAction = "Combat" };
|
||||
var cm = new CombatManager(host, s, new WorldObjectCache(host));
|
||||
cm.SetPlayerId(Player);
|
||||
const BindingFlags F = BindingFlags.Instance | BindingFlags.NonPublic;
|
||||
typeof(CombatManager).GetField("_activeTargetId", F)!.SetValue(cm, unchecked((int)Corpse));
|
||||
var watch = typeof(CombatManager).GetField("_stallWatch", F)!.GetValue(cm)!;
|
||||
var wt = watch.GetType();
|
||||
DateTime now = DateTime.Now;
|
||||
// The ladder stands at step 2 (re-select done 4 s ago); the next step due is the mode toggle.
|
||||
wt.GetField("_eligibleSince", F)!.SetValue(watch, now.AddSeconds(-12));
|
||||
wt.GetField("_lastTick", F)!.SetValue(watch, now.AddMilliseconds(-100));
|
||||
wt.GetField("_step", F)!.SetValue(watch, 2);
|
||||
wt.GetField("_lastStepAt", F)!.SetValue(watch, now.AddSeconds(-4));
|
||||
wt.GetField("_stallRef", F)!.SetValue(watch, now.AddSeconds(-12));
|
||||
int Step() => (int)wt.GetProperty("CurrentStep")!.GetValue(watch)!;
|
||||
var run = typeof(CombatManager).GetMethod("RunStallWatchdog", F)!;
|
||||
var lastAttack = typeof(CombatManager).GetField("lastAttackCmd", F)!;
|
||||
lastAttack.SetValue(cm, DateTime.MinValue);
|
||||
|
||||
FakeHost.CombatModeRefuse = true;
|
||||
Check.True((bool)run.Invoke(cm, new object?[] { null, 1.0 })!, "the toggle step holds the tick");
|
||||
Check.Eq(FakeHost.CombatModeRefusals, 1, "the toggle reached the engine and was refused");
|
||||
Check.Eq(Step(), 2, "a refusal spends no ladder step: still at step 2");
|
||||
Check.Eq(FakeHost.CombatModeRequests.Count, 0, "nothing was sent");
|
||||
Check.True((DateTime)lastAttack.GetValue(cm)! == DateTime.MinValue, "a refusal does not stamp the attack clock");
|
||||
|
||||
FakeHost.CombatModeRefuse = false;
|
||||
Check.True((bool)run.Invoke(cm, new object?[] { null, 1.0 })!, "next tick: the toggle step is due again at once (not 3 s later)");
|
||||
Check.Eq(string.Join(",", FakeHost.CombatModeRequests), "1", "and the accepted toggle sent Peace");
|
||||
Check.Eq(Step(), 3, "now the ladder is at step 3");
|
||||
Check.True((DateTime)lastAttack.GetValue(cm)! != DateTime.MinValue, "the accepted toggle stamps the attack clock");
|
||||
}
|
||||
|
||||
private static void CraftingPeaceRefusal()
|
||||
{
|
||||
PaceOff();
|
||||
const BindingFlags F = BindingFlags.Instance | BindingFlags.NonPublic;
|
||||
MissileCraftingManager Rig(int mode, bool refuse, out Action<double> backdateAsk)
|
||||
{
|
||||
var host = World(combatMode: mode);
|
||||
FakeHost.CombatModeRefuse = refuse;
|
||||
var cm = new MissileCraftingManager(host, new LegacyUiSettings { EnableMissileCrafting = true });
|
||||
var t = typeof(MissileCraftingManager);
|
||||
t.GetField("_currentRecipe", F)!.SetValue(cm, AmmoRecipes.All[0]);
|
||||
t.GetField("_headBundleId", F)!.SetValue(cm, 0x5001);
|
||||
t.GetField("_shaftBundleId", F)!.SetValue(cm, 0x5002);
|
||||
t.GetField("_peaceSent", F)!.SetValue(cm, false);
|
||||
t.GetField("_peaceAskedAt", F)!.SetValue(cm, DateTime.Now);
|
||||
t.GetMethod("TryPeaceForCrafting", F)!.Invoke(cm, null); // StartCrafting's first step
|
||||
t.GetMethod("SetState", F)!.Invoke(cm, new object[] { MissileCraftingManager.CraftState.Combining });
|
||||
backdateAsk = sec => t.GetField("_peaceAskedAt", F)!.SetValue(cm, DateTime.Now.AddSeconds(-sec));
|
||||
return cm;
|
||||
}
|
||||
|
||||
// (a) the engine always refuses: no combine inside the ceiling, the combine after it.
|
||||
var a = Rig(CombatMode.Melee, true, out var backdate);
|
||||
a.ProcessCrafting();
|
||||
Check.Eq(FakeHost.UsesOn.Count, 0, "(a) refused, inside 2.5 s: the combine waits");
|
||||
a.ProcessCrafting();
|
||||
Check.Eq(FakeHost.UsesOn.Count, 0, "(a) still waiting");
|
||||
Check.True(FakeHost.CombatModeRefusals >= 2, $"(a) peace was asked again each tick ({FakeHost.CombatModeRefusals} refusals)");
|
||||
backdate(2.6);
|
||||
a.ProcessCrafting();
|
||||
Check.Eq(FakeHost.UsesOn.Count, 1, "(a) past the 2.5 s ceiling the combine goes out anyway");
|
||||
Check.Eq(FakeHost.CombatModeRequests.Count, 0, "(a) no mode request ever reached the server");
|
||||
|
||||
// (b) already in Peace: no request at all, the combine on the first tick.
|
||||
var b = Rig(CombatMode.NonCombat, true, out _);
|
||||
b.ProcessCrafting();
|
||||
Check.Eq(FakeHost.UsesOn.Count, 1, "(b) already in Peace: the combine goes out on the first tick");
|
||||
Check.Eq(FakeHost.CombatModeRequests.Count, 0, "(b) and no mode request was made");
|
||||
Check.Eq(FakeHost.CombatModeRefusals, 0, "(b) nor asked");
|
||||
}
|
||||
|
||||
// ── Looting ──────────────────────────────────────────────────────────────────
|
||||
|
||||
private static void LootWorld()
|
||||
|
|
|
|||
|
|
@ -41,6 +41,7 @@ internal static class CombatHookTests
|
|||
r.Add("combat hook: Think holds the tick without stamping the attack time; the answer's cast stamps it", ThinkHoldsWithoutStamp);
|
||||
r.Add("combat hook: the heal handler is asked before kits; a drain at a monster goes through CastVital", HealBeforeKit);
|
||||
r.Add("combat hook: a declined, invalid or timed-out heal uses the kit the same check", HealFallsBack);
|
||||
r.Add("refusal: a refused peace-for-the-kit change stamps no cast attempt or wait, and is asked again each tick", PeaceForKitRefused);
|
||||
r.Add("combat hook: the emergency step stays native (kit first, the handler not asked)", EmergencyStaysNative);
|
||||
r.Add("combat hook: the native table - version check, Claim/Poll/Decide/Release, Poll respects StructSize", NativeTable);
|
||||
r.Add("combat hook: a script attack spell the server refused is rejected next time (RynthAi's bolt instead)", AttackRefusalRemembered);
|
||||
|
|
@ -595,10 +596,11 @@ internal static class CombatHookTests
|
|||
public readonly WorldObjectCache Cache;
|
||||
public readonly RynthCoreHost Host;
|
||||
|
||||
public HealRig(int healthPct = 40, int mode = CombatMode.Magic, int emergencyAt = 0, Action? setup = null)
|
||||
public HealRig(int healthPct = 40, int mode = CombatMode.Magic, int emergencyAt = 0, bool peaceKits = false, Action? setup = null)
|
||||
{
|
||||
FakeHost.Reset();
|
||||
FakeHost.WeaponCalls = true;
|
||||
FakeHost.CombatModeCalls = peaceKits; // the peace-for-kits path needs ChangeCombatMode
|
||||
FakeHost.UseOnCalls = true;
|
||||
FakeHost.CombatModeValue = mode;
|
||||
foreach (var row in SpellTable.Rows) FakeHost.Spellbook.Add((uint)row.Id);
|
||||
|
|
@ -615,7 +617,7 @@ internal static class CombatHookTests
|
|||
{
|
||||
IsMacroRunning = true, HealAt = 60, TopOffHP = 60, EmergencyHealAt = emergencyAt, StaminaToHealthAt = 0,
|
||||
GetManaAt = 0, TopOffMana = 0, RestamAt = 0, TopOffStam = 0, UsePotions = false, EnableBuffing = false,
|
||||
UseKitsInMagicMode = true, PeaceModeForKits = false, KitMinSuccessPct = 0, MonsterRange = 20,
|
||||
UseKitsInMagicMode = true, PeaceModeForKits = peaceKits, KitMinSuccessPct = 0, MonsterRange = 20,
|
||||
};
|
||||
var skills = new CharacterSkills(host);
|
||||
skills.SetPlayerId(Player);
|
||||
|
|
@ -674,6 +676,25 @@ internal static class CombatHookTests
|
|||
Check.Eq(FakeHost.Casts.Single(), (Player, Id("Heal Self V")), "target 0: the player");
|
||||
});
|
||||
|
||||
private static void PeaceForKitRefused() => Guard(() =>
|
||||
{
|
||||
var rig = new HealRig(mode: CombatMode.Melee, peaceKits: true);
|
||||
Check.True(rig.B.LastCastAttemptAt == DateTime.MinValue, "set-up: no cast attempt yet");
|
||||
FakeHost.CombatModeRefuse = true; // the engine turns the mode change away
|
||||
const int N = 6;
|
||||
for (int i = 0; i < N; i++) rig.B.CheckVitals();
|
||||
Check.Eq(FakeHost.CombatModeRefusals, N, "the peace request is asked again on every tick");
|
||||
Check.True(rig.B.LastCastAttemptAt == DateTime.MinValue, "_lastCastAttempt stays MinValue: a refusal is not an attempt");
|
||||
var wait = typeof(BuffManager).GetField("_healWaitKind", BindingFlags.Instance | BindingFlags.NonPublic)!;
|
||||
Check.False(((string?)wait.GetValue(rig.B) ?? "").Contains("peace mode"), "no peace-mode wait is recorded");
|
||||
Check.False(rig.KitUsed, "and the kit is not used out of peace");
|
||||
|
||||
FakeHost.CombatModeRefuse = false; // accepted: now it counts
|
||||
rig.B.CheckVitals();
|
||||
Check.Eq(string.Join(",", FakeHost.CombatModeRequests), "1", "the peace request goes out");
|
||||
Check.True(rig.B.LastCastAttemptAt != DateTime.MinValue, "and only now the attempt is stamped");
|
||||
});
|
||||
|
||||
private static void HealFallsBack() => Guard(() =>
|
||||
{
|
||||
void Case(string what, Action<uint> answer, string? log = null)
|
||||
|
|
|
|||
|
|
@ -48,6 +48,7 @@ internal static class WeaponTests
|
|||
r.Add("slayer: creature type unknown, the old choice", SlayerUnknownType);
|
||||
r.Add("slayer: the element follows the chosen weapon", SlayerElement);
|
||||
r.Add("slayer: read through the host (int 166, float 138, monster int 2), cached by wcid", SlayerFromHost);
|
||||
r.Add("refusal: a refused stance change in combat's equip pass sets no backoff and starts no stuck clock", StanceRefusedKeepsBackoff);
|
||||
r.Add("weapon choice: an unlisted wand is never picked (Damage tab, learned, pack, name)", UnlistedNeverPicked);
|
||||
r.Add("Items list: a listed item no longer carried is reported missing (pack, hand, unknown)", MissingFromPack);
|
||||
r.Add("weapon swap: one in flight, confirmed, retry cap, then what's in hand", SwapOneInFlight);
|
||||
|
|
@ -645,10 +646,11 @@ internal static class WeaponTests
|
|||
public readonly MonsterDamageStore Store = new();
|
||||
public WorldObject Target;
|
||||
|
||||
public Rig(uint wcid, string monsterName)
|
||||
public Rig(uint wcid, string monsterName, bool modeCalls = false)
|
||||
{
|
||||
FakeHost.Reset();
|
||||
FakeHost.WeaponCalls = true;
|
||||
FakeHost.CombatModeCalls = modeCalls;
|
||||
FakeHost.CombatModeValue = CombatMode.Magic;
|
||||
FakeHost.Names[ListedWand] = "Wand"; FakeHost.ItemTypes[ListedWand] = 0x8000;
|
||||
FakeHost.Ints[(ListedWand, 45)] = 0x40; // identified: Lightning
|
||||
|
|
@ -698,6 +700,26 @@ internal static class WeaponTests
|
|||
Check.Eq(missing.Count, 0, "inventory not known yet (empty, logging in): nothing reported missing");
|
||||
}
|
||||
|
||||
private static void StanceRefusedKeepsBackoff()
|
||||
{
|
||||
var rig = new Rig(1535, "Ethereal Wisp", modeCalls: true);
|
||||
rig.C.InHandOverride = () => unchecked((int)ListedWand); // the wand is in hand
|
||||
FakeHost.CombatModeValue = CombatMode.NonCombat; // but the stance is not Magic yet
|
||||
FakeHost.Ints[(ListedWand, 10)] = 0x01000000; // CurrentWieldedLocation: reads wielded (the stuck-clock branch)
|
||||
var equip = typeof(CombatManager).GetMethod("EquipWeaponAndSetStance", BindingFlags.Instance | BindingFlags.NonPublic)!;
|
||||
var last = typeof(CombatManager).GetField("lastStanceAttempt", BindingFlags.Instance | BindingFlags.NonPublic)!;
|
||||
var stuck = typeof(CombatManager).GetField("_stanceStuckSince", BindingFlags.Instance | BindingFlags.NonPublic)!;
|
||||
FakeHost.CombatModeRefuse = true;
|
||||
for (int i = 0; i < 5; i++) equip.Invoke(rig.C, new object[] { rig.Target, "Auto" });
|
||||
Check.Eq(FakeHost.CombatModeRefusals, 5, "every pass asks again: a refusal sets no backoff");
|
||||
Check.True((DateTime)last.GetValue(rig.C)! == DateTime.MinValue, "lastStanceAttempt stays put");
|
||||
Check.True((DateTime)stuck.GetValue(rig.C)! == DateTime.MinValue, "the stuck clock has not started");
|
||||
Check.Eq(FakeHost.Uses.Count, 0, "no re-equip UseObject");
|
||||
FakeHost.CombatModeRefuse = false;
|
||||
equip.Invoke(rig.C, new object[] { rig.Target, "Auto" });
|
||||
Check.Eq(string.Join(",", FakeHost.CombatModeRequests), CombatMode.Magic.ToString(), "accepted: the request goes out");
|
||||
Check.True((DateTime)last.GetValue(rig.C)! != DateTime.MinValue, "and only now lastStanceAttempt is stamped");
|
||||
}
|
||||
private static void UnlistedNeverPicked()
|
||||
{
|
||||
var rig = new Rig(1535, "Ethereal Wisp");
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ internal static class CombatStallWatchdogTests
|
|||
r.Add("stall: a swing after a step is a recovery", Recovery);
|
||||
r.Add("stall: a tick gap starts the count over", TickGapResets);
|
||||
r.Add("stall: ineligible never acts", Ineligible);
|
||||
r.Add("stall: Unfire takes a step back - the same step is due again next tick, not 3 s later", UnfireRetries);
|
||||
}
|
||||
|
||||
/// <summary>Ticks every 500 ms from <paramref name="fromMs"/> to <paramref name="toMs"/>
|
||||
|
|
@ -95,4 +96,27 @@ internal static class CombatStallWatchdogTests
|
|||
Check.Eq(acted, 0, "no action in a minute of ineligible ticks");
|
||||
Check.Eq(w.Episodes, 0, "no episodes");
|
||||
}
|
||||
}
|
||||
|
||||
private static void UnfireRetries()
|
||||
{
|
||||
var w = new CombatStallWatchdog();
|
||||
Check.Eq(RunUntilAction(w, 0, 20_000, NoEvidence), (Act.Reissue, 6000), "step 1");
|
||||
Check.Eq(RunUntilAction(w, 6500, 20_000, NoEvidence), (Act.Reselect, 9000), "step 2");
|
||||
Check.Eq(RunUntilAction(w, 9500, 20_000, NoEvidence), (Act.ToggleMode, 12000), "step 3 fires");
|
||||
|
||||
w.Unfire(); // the engine refused the toggle
|
||||
Check.Eq(w.CurrentStep, 2, "Unfire: back at step 2");
|
||||
Check.Eq(RunUntilAction(w, 12_500, 20_000, NoEvidence), (Act.ToggleMode, 12_500), "the toggle is due again on the very next tick");
|
||||
Check.Eq(RunUntilAction(w, 13_000, 20_000, NoEvidence), (Act.Rewield, 15_500), "an accepted toggle then spaces step 4 a full 3 s after it");
|
||||
|
||||
// Unfire at step 1 gives the episode back too.
|
||||
var v = new CombatStallWatchdog();
|
||||
Check.Eq(RunUntilAction(v, 0, 10_000, NoEvidence), (Act.Reissue, 6000), "step 1");
|
||||
Check.Eq(v.Episodes, 1, "one episode");
|
||||
v.Unfire();
|
||||
Check.Eq(v.CurrentStep, 0, "back to no step");
|
||||
Check.Eq(v.Episodes, 0, "and the episode is not counted until a step really goes out");
|
||||
Check.Eq(RunUntilAction(v, 6500, 10_000, NoEvidence), (Act.Reissue, 6500), "step 1 again next tick");
|
||||
Check.Eq(v.Episodes, 1, "one episode");
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue