RynthAi.Combat v1: let a RynthLua script choose RynthAi's attack and heal spells

Adds a polling hand-off ("RynthAi.Combat" v1 interface table) so a script can
pick the spell RynthAi casts for an attack or an HP heal, while RynthAi keeps
scanning, targeting, the arbiter, nav/loot gating, thresholds, buffs, stance and
wand. Decline, invalid answers, timeouts and lease lapse all fall back to
native selection in the same tick; the emergency heal path is untouched; the
hook is inert when no script registers. RynthAi's mana reserve holds a script's
attack spell as it holds its own (the answer is kept, nothing native is cast
instead, and the script is asked again after the deadline plus 1 s); a spell
costing more than max mana is rejected so RynthAi chooses its own. Lua API:
ai.SetAttackHandler, ai.SetHealHandler, ai.ClearCombatHandlers, ai.CombatHook.
Includes host and compat tests and Examples/combat-handler.lua. No
RynthCore/SDK changes.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Daniel Wallace 2026-10-10 10:26:25 -07:00
commit 10e2c2eb0a
30 changed files with 3620 additions and 27 deletions

View file

@ -13,6 +13,7 @@ Scripts that listen for events keep running until you stop them (`/lua stop <nam
- `hunt-settings.lua`: a settings form saved with `storage` (checkbox, slider, 0-based combo, text box) and a button that sets your combat mode; `/hunt` shows or hides it.
- `salvage-list.lua`: a list with buttons: your salvage, gathered in a spawned loop, a filter box, a scrolling region with `PushID` rows, and Use / Give buttons (Give waits for its result in a thread of its own). The buttons really act.
- `item-icons.lua`: an item list with icons: your pack items drawn with their AC icons (`views.Huds.GetObjectIconTexture`, `ImGui.ImageButton`, `ImGui.Image`), an `InputInt` for how many rows and a `DragFloat` for the icon size. Nothing acts on the game. Needs an engine from 2026-09-30 or later.
- `combat-handler.lua`: RynthAi's combat hook (`rynth.ai` SetAttackHandler / SetHealHandler): an attack handler that casts a set spell while the target's health is under a line and otherwise lets RynthAi choose, a heal handler that heals you under a health line, the refusal reasons printed, and `/combathook` to turn them on and off. Set its spell ids first; it needs RynthAi running.
The window examples need RynthCore API v71 with ImGui on; see "Script windows" in the reference.

View file

@ -0,0 +1,56 @@
-- combat-handler.lua: choose RynthAi's attack and heal spells from a script (the combat hook).
--
-- RynthAi still picks the target, the stance and the pacing. When it is about to cast it asks
-- the handler, waits until ctx.deadline at most, and casts the answer. A handler returns
-- spellId [, targetId [, flags]] targetId nil = ctx.targetId (attack) / yourself (heal)
-- nil let RynthAi choose this time
-- flags: "area" (a ring/wall cast) or "nojudge" (not meant to damage; no miss counting).
-- /combathook turns the handlers on and off. Set the spell ids below to spells you know.
local ai = require("rynth.ai")
local FINISHER_SPELL = 0 -- attack spell id, cast while the target is under FINISH_BELOW
local FINISH_BELOW = 0.30 -- the target's health ratio, 0..1
local HEAL_SPELL = 0 -- heal spell id, cast on yourself under HEAL_BELOW
local HEAL_BELOW = 50 -- your health percent
local function onAttack(ctx)
-- ctx.targetHealth is -1 while RynthAi has no health for the target.
if FINISHER_SPELL ~= 0 and ctx.targetHealth >= 0 and ctx.targetHealth < FINISH_BELOW then
return FINISHER_SPELL
end
return nil -- RynthAi casts its own choice
end
local function onHeal(ctx)
if HEAL_SPELL ~= 0 and ctx.healthPct >= 0 and ctx.healthPct < HEAL_BELOW then
return HEAL_SPELL, 0 -- target 0 = yourself
end
return nil -- kits, Heal Self and potions as usual
end
local enabled = false
local function enable()
local okAttack, whyAttack = ai.SetAttackHandler(onAttack)
if not okAttack then print("attack handler refused: " .. tostring(whyAttack)) end
local okHeal, whyHeal = ai.SetHealHandler(onHeal)
if not okHeal then print("heal handler refused: " .. tostring(whyHeal)) end
enabled = okAttack or okHeal
print("combat handlers " .. (enabled and "on" or "off"))
end
local function disable()
ai.ClearCombatHandlers()
enabled = false
print("combat handlers off: RynthAi chooses every spell")
end
game.RegisterCommand("/combathook", function()
if enabled then disable() else enable() end
end)
-- Stopping the script releases the handlers anyway; clearing them here says so explicitly.
game.OnScriptEnd.Add(function() ai.ClearCombatHandlers() end)
enable()