Two casts were held open 40.8 s and 37 s by ACE's fast-tick turn-to-cast wait; every later send was answered UseDone 0x1D and a heal was re-sent every 2.5 s. - ActionPacer: after a 0x1D UseDone nothing starts until the server answers; with no cast of ours open, one probe after 1.5 s, then doubling (max 6 s). Attack repeats are not held (ACE sends no UseDone for a swing), and a latch with no cast of ours open ends after LatchMaxMs (15 s). A cast still unanswered 1 s past its normal cycle (learned per spell, at least 3 s) is turned to face its target; 2 s past it (capped by Stuck Cast Break, default 8000 ms) or on a 0x1D while it has been open 3 s, it is broken with a jump (TapJump; ACE fails a cast on a fast-tick jump). Starts wait until landed; no UseDone 3 s after landing -> Peace mode; 3 s more -> going on. Every hold is bounded: a cast StaleCastMs old, or sent before a frame gap of 5 s (keyed on the gap's end), is dropped with the latch, never broken. No breaker step in portal space; Forget on portal-enter and the death hold. A burst of 5 'too busy' answers in a second holds every start except a heal. A refused forced jump with no cast open leaves no breaker stage. A cast a script chose (the combat hook) is not jumped before 6 s from its send (ScriptCastMinBreakMs, the held-cast nudge's HeldAfterIncantMs). Stuck Cast Break is read from the settings once per tick. - MoveAudit: no start in the tick of a movement change; 150 ms and a clear motion queue (or 600 ms) let it go. A held stop sent later by FlushPendingStops counts as a movement change too; the forced flush in CastSpellFor still goes out in the same tick as the cast that follows it. - Blacklist: casts answered 0x1D (or unanswered while the server is busy) are not no-damage casts. - Combat hook: ctx.pending is the cast in flight; ctx.last is the most recent RESOLVED cast, its err final (the first UseDone after its send; nil only when none could be read: 7 s timeout, or another answer came first). The casts in flight of both kinds resolve in send order through one queue. The send-time UseDone count comes from the pacer (LastCastSeqAtSend). CombatRequestV1 appends Resolved*/Pending* fields (StructSize-gated, no Version bump); an older RynthAi keeps the old ctx.last. - HP failsafe (BuffManager.HpFailsafe.cs): at or under Emergency Heal At with no heal landing for 3 rounds of 2.5 s, a forced jump; 3 more rounds, the macro stops and /lifestone goes out (HP Failsafe Recall, default on). Nothing counts in portal space; a pending recall is dropped on death; a recall that never starts turns the macro back on (only one the failsafe stopped, never past the VTank yield) and re-arms after a 30 s cooldown. Recovery retry: recovery vitals (Heal Self, Stamina to Health, Revitalize, Stamina to Mana) refused for "not enough mana" retry after Recovery Retry (200 ms, a setting) instead of resting the family: ACE rolls Mana Conversion on every attempt before the windup and a refusal costs nothing. At most 5 recovery casts a second, 20 refusals in a row, stamina and Mana Conversion checks, an episode with no refusal for 7 s ends; an episode summary line. Stamina to Mana and Revitalize are still held under their cost by the mana reserve (AttemptVitalCast), so for them the retry applies when the mana reading is stale or costs are unknown; Heal Self and Stamina to Health, which are tried below cost, gain the most. When the retry stops, the vital rests as the existing mana reserve has it (LowManaHealRestSec, 2 s). Script heals and other casts are unchanged. Tests: StuckCastTests (incl. the script cast's break floor), PacerEdgeTests, RecoveryRetryTests (incl. costs known: Stamina to Mana held), HpFailsafeTests, CombatHookTests (ctx.last/ctx.pending, send order, an answer during the send, 0x1D blacklist, script heal keeps its rest, script attack and heal not jumped before 6 s with or without a 'too busy' answer); HealLatencyTests' and ManaReserveTests' low-mana rest cases pin untrained Mana Conversion; RynthLua compat combathook.lua. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
313 lines
16 KiB
Lua
313 lines
16 KiB
Lua
-- RynthAi's combat hook from a script (rynth.ai SetAttackHandler / SetHealHandler, RynthLua's
|
|
-- CombatHookPump against a fake "RynthAi.Combat" table): the handler's answer reaches Decide, a
|
|
-- yielding handler still answers, an error declines, three in a row unregister, one script per
|
|
-- kind, a stopped script releases its kinds, and no table means a refusal with the reason.
|
|
local ai = require("rynth.ai")
|
|
local ATTACK, HEAL = 1, 2
|
|
|
|
local function decisions() return harness.combatDecisions() end
|
|
local function waitDecisions(n)
|
|
return waitfor(function() return #decisions() >= n end, 3000)
|
|
end
|
|
|
|
-- No table: RynthAi without the hook (or not loaded).
|
|
harness.combatTable(false)
|
|
local ok, why = ai.SetAttackHandler(function() return 1 end)
|
|
eq(ok, false, "no combat table: refused")
|
|
eq(why, "RynthAi has no combat hook", "and says why")
|
|
eq(ai.CombatHook.Supported, false, "CombatHook.Supported is false")
|
|
eq(ai.CombatHook.AttackOwner, nil, "no owner")
|
|
|
|
harness.combatTable(true)
|
|
sleep(10) -- RynthLua looks the table up again on its next tick
|
|
eq(ai.CombatHook.Supported, true, "table on: supported")
|
|
|
|
-- 1. A decision is posted, with the request's context.
|
|
local seen
|
|
ok = ai.SetAttackHandler(function(ctx)
|
|
seen = ctx
|
|
return 1234, ctx.monsters[2].id
|
|
end)
|
|
eq(ok, true, "attack handler registered")
|
|
eq(ai.CombatHook.AttackOwner, "combathook", "the owner is this script")
|
|
check(waitfor(function() return harness.combatState().kinds == ATTACK end, 2000), "the pump claims the attack kind")
|
|
eq(harness.combatState().owner, "RynthLua/combathook", "claimed under RynthLua/<script>")
|
|
eq(ai.CombatHook.Claimed, true, "Claimed")
|
|
|
|
harness.combatScanned({ { 0x80000101, 3.5 }, { 0x80000102, 7.25 } })
|
|
local seq1 = harness.combatOpen(ATTACK, 0x80000101)
|
|
check(waitDecisions(1), "the handler answered")
|
|
local d = decisions()[1]
|
|
eq(d.seq, seq1, "decided for the open seq")
|
|
eq(d.kind, ATTACK, "attack kind")
|
|
eq(d.spell, 1234, "the handler's spell")
|
|
eq(d.target, 0x80000102, "the handler's target")
|
|
eq(d.flags, 0, "no flags")
|
|
eq(d.taken, true, "taken by RynthAi")
|
|
eq(seen.seq, seq1, "ctx.seq")
|
|
eq(seen.kind, "attack", "ctx.kind")
|
|
eq(seen.targetId, 0x80000101, "ctx.targetId")
|
|
eq(seen.targetHealth, 0.5, "ctx.targetHealth")
|
|
eq(seen.healthPct, 80, "ctx.healthPct")
|
|
eq(seen.inCombat, true, "ctx.inCombat")
|
|
eq(seen.combatMode, 8, "ctx.combatMode")
|
|
check(seen.deadline > seen.issuedAt, "ctx.deadline after ctx.issuedAt")
|
|
check(seen.deadline > clock(), "ctx.deadline is on clock()'s scale and still ahead")
|
|
eq(#seen.monsters, 2, "ctx.monsters")
|
|
eq(seen.monsters[1].distance, 3.5, "ctx.monsters[1].distance")
|
|
eq(seen.last.seq, 0, "no resolved cast yet")
|
|
eq(seen.pending, nil, "nothing in flight yet")
|
|
|
|
-- ctx.pending: the cast in flight; ctx.last: the most recent RESOLVED cast, err final.
|
|
local seq2 = harness.combatOpen(ATTACK, 0x80000101)
|
|
check(waitDecisions(2), "second request answered")
|
|
eq(seen.pending.seq, seq1, "ctx.pending.seq: the first cast, no answer yet")
|
|
eq(seen.pending.spell, 1234, "ctx.pending.spell")
|
|
eq(seen.pending.target, 0x80000102, "ctx.pending.target")
|
|
eq(seen.last.seq, 0, "ctx.last: still no resolved cast")
|
|
eq(seen.last.err, nil, "ctx.last.err: nil before the first resolved cast")
|
|
|
|
harness.combatResolve(0x1D) -- the first cast's UseDone: 0x1D
|
|
local seqE = harness.combatOpen(ATTACK, 0x80000101)
|
|
check(waitDecisions(3), "third request answered")
|
|
eq(seen.last.seq, seq1, "ctx.last: the first cast, now resolved")
|
|
eq(seen.last.spell, 1234, "ctx.last.spell")
|
|
eq(seen.last.target, 0x80000102, "ctx.last.target")
|
|
eq(seen.last.err, 29, "ctx.last.err = 0x1D (refused 'too busy')")
|
|
eq(seen.pending.seq, seq2, "ctx.pending: the second cast")
|
|
|
|
-- No answer for the second before the third went out: resolved with no answer (nil).
|
|
harness.combatOpen(ATTACK, 0x80000101)
|
|
check(waitDecisions(4), "fourth request answered")
|
|
eq(seen.last.seq, seq2, "ctx.last: the second cast")
|
|
eq(seen.last.err, nil, "no answer read for it: nil")
|
|
eq(seen.pending.seq, seqE, "ctx.pending: the third cast")
|
|
|
|
harness.combatResolve(0)
|
|
harness.combatOpen(ATTACK, 0x80000101)
|
|
check(waitDecisions(5), "fifth request answered")
|
|
eq(seen.last.seq, seqE, "ctx.last: the third cast")
|
|
eq(seen.last.err, 0, "ctx.last.err = 0 (done)")
|
|
|
|
harness.combatResolve(0x1D)
|
|
harness.combatOlderRynthAi(true)
|
|
harness.combatOpen(ATTACK, 0x80000101)
|
|
check(waitDecisions(6), "older RynthAi request answered")
|
|
eq(seen.last.err, nil, "an older RynthAi: no err")
|
|
eq(seen.pending, nil, "an older RynthAi: no pending")
|
|
check(seen.last.seq > seqE, "an older RynthAi: ctx.last is its last decision")
|
|
harness.combatOlderRynthAi(false)
|
|
|
|
-- 2. A yielding handler (the same script registering again replaces its handler), area flag.
|
|
ok = ai.SetAttackHandler(function(ctx)
|
|
sleep(60)
|
|
return 55, nil, "area"
|
|
end)
|
|
eq(ok, true, "the same script replaces its handler")
|
|
local seq3 = harness.combatOpen(ATTACK, 0x80000101)
|
|
check(waitDecisions(7), "the yielding handler still answers")
|
|
d = decisions()[7]
|
|
eq(d.seq, seq3, "for its seq")
|
|
eq(d.spell, 55, "its spell")
|
|
eq(d.target, 0, "nil target = the request's target")
|
|
eq(d.flags, 1, "\"area\" sets the AREA flag")
|
|
eq(d.taken, true, "taken")
|
|
|
|
-- 3. nil declines.
|
|
ai.SetAttackHandler(function(ctx) return nil end)
|
|
harness.combatOpen(ATTACK, 0x80000101)
|
|
check(waitDecisions(8), "a nil answer is still an answer")
|
|
eq(decisions()[8].spell, 0, "nil = decline (spell 0)")
|
|
|
|
-- 4. An error declines at once; three in a row unregister.
|
|
ai.SetAttackHandler(function(ctx) error("boom (expected)") end)
|
|
for i = 1, 3 do
|
|
harness.combatOpen(ATTACK, 0x80000101)
|
|
check(waitDecisions(8 + i), "error " .. i .. ": declined at once")
|
|
eq(decisions()[8 + i].spell, 0, "error " .. i .. ": spell 0")
|
|
eq(decisions()[8 + i].taken, true, "error " .. i .. ": taken (no wait for the deadline)")
|
|
end
|
|
eq(ai.CombatHook.AttackOwner, nil, "three errors: the handler is removed")
|
|
check(waitfor(function() return harness.combatState().kinds == 0 end, 2000), "and the kind released in RynthAi")
|
|
local unregistered = false
|
|
for _, line in ipairs(harness.chatLog()) do
|
|
if line:find("failed 3 times in a row", 1, true) then unregistered = true end
|
|
end
|
|
check(unregistered, "a chat warning says so")
|
|
check(harness.scriptConsole("combathook"):find("combat attack handler:", 1, true) ~= nil, "each error is in the console")
|
|
|
|
-- A bad answer counts as an error too (and declines).
|
|
ai.SetAttackHandler(function(ctx) return "fireball" end)
|
|
harness.combatOpen(ATTACK, 0x80000101)
|
|
check(waitDecisions(12), "a bad answer declines")
|
|
eq(decisions()[12].spell, 0, "spell 0")
|
|
check(harness.scriptConsole("combathook"):find("expected a spell id", 1, true) ~= nil, "and says what was wrong")
|
|
ai.ClearCombatHandlers()
|
|
eq(ai.CombatHook.AttackOwner, nil, "ClearCombatHandlers drops it")
|
|
|
|
-- 5. One script per kind; a stopped script releases its kinds.
|
|
ok = ai.SetHealHandler(function(ctx) return 2001, 0 end)
|
|
eq(ok, true, "heal handler registered")
|
|
harness.runScript("rival", [[
|
|
local ai = require("rynth.ai")
|
|
local ok, why = ai.SetHealHandler(function(ctx) return 9 end)
|
|
eq(ok, false, "rival: the heal kind is taken")
|
|
eq(why, "the heal handler is already set by combathook", "rival: and by whom")
|
|
ok = ai.SetAttackHandler(function(ctx) return 777 end)
|
|
eq(ok, true, "rival: the attack kind is free")
|
|
rivalReady = true
|
|
]])
|
|
check(waitfor(function() return ai.CombatHook.AttackOwner == "rival" end, 2000), "the rival holds attack")
|
|
eq(ai.CombatHook.HealOwner, "combathook", "this script still holds heal")
|
|
check(waitfor(function() return harness.combatState().kinds == ATTACK + HEAL end, 2000), "both kinds claimed")
|
|
eq(harness.combatState().owner, "RynthLua/combathook+rival", "under both scripts' names")
|
|
check(harness.scriptLoaded("rival"), "the rival's main chunk ended but its handler keeps it running")
|
|
|
|
local healSeq = harness.combatOpen(HEAL, 0)
|
|
check(waitDecisions(13), "heal answered")
|
|
eq(decisions()[13].seq, healSeq, "heal seq")
|
|
eq(decisions()[13].kind, HEAL, "heal kind")
|
|
eq(decisions()[13].spell, 2001, "heal spell")
|
|
|
|
local releasesBefore = harness.combatState().releases
|
|
check(harness.stopScript("rival"), "stop the rival")
|
|
check(not harness.scriptLoaded("rival"), "stopped")
|
|
check(harness.combatState().releases > releasesBefore, "stopping it released in RynthAi")
|
|
check(harness.combatState().releasedKinds % 2 == 1, "the attack kind among the released")
|
|
eq(ai.CombatHook.AttackOwner, nil, "attack is free again")
|
|
eq(ai.CombatHook.HealOwner, "combathook", "heal untouched")
|
|
check(waitfor(function() return harness.combatState().kinds == HEAL end, 2000), "only heal claimed now")
|
|
|
|
-- SetHealHandler(nil) clears this script's heal handler.
|
|
ai.SetHealHandler(nil)
|
|
eq(ai.CombatHook.HealOwner, nil, "nil clears it")
|
|
check(waitfor(function() return harness.combatState().kinds == 0 end, 2000), "nothing claimed")
|
|
|
|
-- 6. Flags: "nojudge", a table of flags, true = area; an unknown flag is an error.
|
|
local function answerWith(fn)
|
|
ai.SetAttackHandler(fn)
|
|
local n = #decisions()
|
|
harness.combatOpen(ATTACK, 0x80000101)
|
|
check(waitDecisions(n + 1), "answered")
|
|
return decisions()[n + 1]
|
|
end
|
|
eq(answerWith(function() return 55, nil, "nojudge" end).flags, 2, "\"nojudge\" sets the NOJUDGE flag")
|
|
eq(answerWith(function() return 55, nil, { area = true, nojudge = true } end).flags, 3, "a table sets both")
|
|
eq(answerWith(function() return 55, nil, { nojudge = true, area = false } end).flags, 2, "a false field is no flag")
|
|
eq(answerWith(function() return 55, nil, true end).flags, 1, "true is still \"area\"")
|
|
d = answerWith(function() return 55, nil, "aera" end)
|
|
eq(d.spell, 0, "an unknown flag declines")
|
|
check(harness.scriptConsole("combathook"):find('expected "area", "nojudge"', 1, true) ~= nil, "and says what it expected")
|
|
ai.ClearCombatHandlers()
|
|
|
|
-- 7. Set/clear cycles register the script's KeepAlive / OnStop hooks once, not once per cycle.
|
|
ai.SetAttackHandler(function() return nil end)
|
|
local hooks = harness.scriptHooks("combathook")
|
|
check(hooks.keepAlive >= 1 and hooks.onStop >= 1, "the hooks are registered")
|
|
for i = 1, 3 do
|
|
ai.ClearCombatHandlers()
|
|
ai.SetAttackHandler(function() return nil end)
|
|
ai.SetHealHandler(function() return nil end)
|
|
end
|
|
local after = harness.scriptHooks("combathook")
|
|
eq(after.keepAlive, hooks.keepAlive, "no KeepAlive hook added by the cycles")
|
|
eq(after.onStop, hooks.onStop, "no OnStop hook added by the cycles")
|
|
ai.ClearCombatHandlers()
|
|
check(waitfor(function() return harness.combatState().kinds == 0 end, 2000), "nothing claimed")
|
|
|
|
-- 8. A handler that never returns is aborted after the deadline plus the stall budget and counts
|
|
-- as an error; three in a row remove it and release the kind.
|
|
ai.SetAttackHandler(function(ctx) while true do sleep(50) end end)
|
|
check(waitfor(function() return harness.combatState().kinds == ATTACK end, 2000), "the stalling handler's kind is claimed")
|
|
for i = 1, 3 do
|
|
local n = #decisions()
|
|
local t0 = clock()
|
|
harness.combatOpen(ATTACK, 0x80000101, 100)
|
|
check(waitDecisions(n + 1), "stall " .. i .. ": the stuck handler is aborted and declines")
|
|
eq(decisions()[n + 1].spell, 0, "stall " .. i .. ": spell 0")
|
|
check(clock() - t0 >= 1000, "stall " .. i .. ": only after the deadline plus the stall budget")
|
|
end
|
|
eq(ai.CombatHook.AttackOwner, nil, "three stalls: the handler is removed")
|
|
check(waitfor(function() return harness.combatState().kinds == 0 end, 2000), "and the kind released in RynthAi")
|
|
local stalledLine = false
|
|
for _, line in ipairs(harness.chatLog()) do
|
|
if line:find("did not answer within the deadline 3 times in a row", 1, true) then stalledLine = true end
|
|
end
|
|
check(stalledLine, "a chat line says the handler did not answer within the deadline")
|
|
check(harness.scriptConsole("combathook"):find("the handler did not answer within the deadline", 1, true) ~= nil, "each stall is in the console")
|
|
|
|
-- 9. RynthAi reloads (its seq restarts): a new request whose seq repeats the last one started is
|
|
-- still answered.
|
|
ai.SetAttackHandler(function(ctx) return 4242 end)
|
|
harness.combatReload()
|
|
local before = #decisions()
|
|
eq(harness.combatOpen(ATTACK, 0x80000101), 1, "after a reload the first request is seq 1")
|
|
check(waitDecisions(before + 1), "answered")
|
|
harness.combatReload() -- again: the next request is seq 1 too, the same seq as the last one started
|
|
before = #decisions()
|
|
eq(harness.combatOpen(ATTACK, 0x80000101), 1, "seq 1 again")
|
|
check(waitDecisions(before + 1), "the repeated seq is still answered (it is a new request)")
|
|
eq(decisions()[before + 1].spell, 4242, "with the handler's spell")
|
|
eq(decisions()[before + 1].taken, true, "taken")
|
|
ai.ClearCombatHandlers()
|
|
|
|
-- 10. Replacing a handler while it is still answering: the new one answers the open request.
|
|
local slowStarted = false
|
|
ai.SetAttackHandler(function(ctx) slowStarted = true; sleep(400); return 11 end)
|
|
check(waitfor(function() return harness.combatState().kinds == ATTACK end, 2000), "claimed")
|
|
before = #decisions()
|
|
harness.combatOpen(ATTACK, 0x80000101)
|
|
check(waitfor(function() return slowStarted end, 2000), "the slow handler is running")
|
|
ai.SetAttackHandler(function(ctx) return 77 end)
|
|
check(waitDecisions(before + 1), "the replacement answers the open request")
|
|
eq(decisions()[before + 1].spell, 77, "the new handler's answer")
|
|
eq(decisions()[before + 1].taken, true, "taken")
|
|
sleep(500)
|
|
eq(#decisions(), before + 1, "the replaced handler's answer never arrives")
|
|
ai.ClearCombatHandlers()
|
|
check(waitfor(function() return harness.combatState().kinds == 0 end, 2000), "nothing claimed")
|
|
|
|
-- 11. Examples\combat-handler.lua, with its spell ids and lines set.
|
|
local example = harness.exampleCode("combat-handler.lua")
|
|
local code = example
|
|
local n1, n2, n3, n4
|
|
code, n1 = code:gsub("local FINISHER_SPELL = 0", "local FINISHER_SPELL = 3001", 1)
|
|
code, n2 = code:gsub("local FINISH_BELOW = 0%.30", "local FINISH_BELOW = 0.60", 1)
|
|
code, n3 = code:gsub("local HEAL_SPELL = 0", "local HEAL_SPELL = 3002", 1)
|
|
code, n4 = code:gsub("local HEAL_BELOW = 50", "local HEAL_BELOW = 90", 1)
|
|
eq(n1 + n2 + n3 + n4, 4, "example: its four settings found")
|
|
harness.runScript("combat-handler", code)
|
|
check(waitfor(function() return ai.CombatHook.AttackOwner == "combat-handler" and ai.CombatHook.HealOwner == "combat-handler" end, 2000),
|
|
"example: holds attack and heal")
|
|
check(harness.scriptConsole("combat-handler"):find("combat handlers on", 1, true) ~= nil, "example: says it is on")
|
|
before = #decisions()
|
|
harness.combatOpen(ATTACK, 0x80000101) -- target health 0.5, under its 0.60
|
|
check(waitDecisions(before + 1), "example: attack answered")
|
|
eq(decisions()[before + 1].spell, 3001, "example: its finisher under the line")
|
|
eq(decisions()[before + 1].target, 0, "example: at RynthAi's target")
|
|
harness.combatOpen(HEAL, 0) -- health 80%, under its 90
|
|
check(waitDecisions(before + 2), "example: heal answered")
|
|
eq(decisions()[before + 2].spell, 3002, "example: its heal under the line")
|
|
eq(decisions()[before + 2].target, 0, "example: on yourself")
|
|
harness.chatInput("/combathook")
|
|
check(waitfor(function() return ai.CombatHook.AttackOwner == nil and harness.combatState().kinds == 0 end, 2000), "example: /combathook turns it off")
|
|
harness.chatInput("/combathook")
|
|
check(waitfor(function() return ai.CombatHook.AttackOwner == "combat-handler" end, 2000), "example: and on again")
|
|
check(harness.stopScript("combat-handler"), "example: stopped")
|
|
eq(ai.CombatHook.AttackOwner, nil, "example: stopping releases attack")
|
|
eq(ai.CombatHook.HealOwner, nil, "example: and heal")
|
|
|
|
-- As shipped (ids 0) it declines; a kind held by another script is refused with the reason.
|
|
ai.SetAttackHandler(function() return nil end)
|
|
harness.runScript("combat-handler", example)
|
|
check(waitfor(function() return ai.CombatHook.HealOwner == "combat-handler" end, 2000), "shipped example: holds heal")
|
|
check(harness.scriptConsole("combat-handler"):find("attack handler refused: the attack handler is already set by combathook", 1, true) ~= nil,
|
|
"shipped example: prints why attack was refused")
|
|
before = #decisions()
|
|
harness.combatOpen(HEAL, 0)
|
|
check(waitDecisions(before + 1), "shipped example: heal answered")
|
|
eq(decisions()[before + 1].spell, 0, "shipped example: no heal id set, so it declines")
|
|
harness.stopScript("combat-handler")
|
|
ai.ClearCombatHandlers()
|
|
|
|
done()
|