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

@ -22,11 +22,20 @@ internal static class Harness
private static FakeWorld W => FakeWorld.W;
/// <summary>Plugins\RynthCore.Plugin.RynthLua\Examples\<paramref name="file"/>, found from this source file's folder.</summary>
private static string ExamplePath(string file, [System.Runtime.CompilerServices.CallerFilePath] string source = "")
{
string tools = System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName(source) ?? string.Empty) ?? string.Empty;
string root = System.IO.Path.GetDirectoryName(tools) ?? string.Empty;
return System.IO.Path.Combine(root, "Plugins", "RynthCore.Plugin.RynthLua", "Examples", System.IO.Path.GetFileName(file));
}
public static void Reset()
{
Checks = 0;
Failures.Clear();
Done = false;
FakeCombat.Reset();
}
public static void Register(Script s, string scriptName)
@ -216,6 +225,53 @@ internal static class Harness
foreach (uint id in W.SalvageItems) t.Append(DynValue.NewNumber(id));
return DynValue.NewTable(t);
});
// RynthAi's combat hook (FakeCombat): turn the table on/off, open a request as RynthAi
// would, and read back what RynthLua claimed, decided and released.
Fn("combatTable", a => { FakeCombat.Enabled = a.Count > 0 && a[0].CastToBool(); return DynValue.Nil; });
Fn("combatOpen", a => DynValue.NewNumber(FakeCombat.Open((uint)Num(a, 0, 1), Id(a, 1), (long)Num(a, 2, 2000))));
Fn("combatScanned", a =>
{
FakeCombat.Scanned.Clear();
if (a.Count > 0 && a[0].Type == DataType.Table)
foreach (DynValue row in a[0].Table.Values)
FakeCombat.Scanned.Add(((uint)row.Table.Get(1).Number, (float)row.Table.Get(2).Number));
return DynValue.Nil;
});
Fn("combatDecisions", a =>
{
var t = new Table(s);
foreach (var d in FakeCombat.Decisions)
{
var row = new Table(s);
row["seq"] = (double)d.Seq; row["kind"] = (double)d.Kind; row["spell"] = (double)d.Spell;
row["target"] = (double)d.Target; row["flags"] = (double)d.Flags; row["taken"] = d.Taken;
t.Append(DynValue.NewTable(row));
}
return DynValue.NewTable(t);
});
Fn("combatState", a =>
{
var t = new Table(s);
t["owner"] = FakeCombat.Owner; t["kinds"] = (double)FakeCombat.Kinds;
t["claims"] = (double)FakeCombat.Claims; t["refusals"] = (double)FakeCombat.Refusals;
t["releases"] = (double)FakeCombat.Releases; t["releasedKinds"] = (double)FakeCombat.ReleasedKinds;
return DynValue.NewTable(t);
});
// A second script in the same plugin (another script competing for a handler).
Fn("runScript", a => { Plugin!.RunCode(Str(a, 0), Str(a, 1)); return DynValue.Nil; });
Fn("stopScript", a => DynValue.NewBoolean(Plugin!.StopScript(Str(a, 0))));
Fn("scriptLoaded", a => DynValue.NewBoolean(Plugin!.ScriptIsLoaded(Str(a, 0))));
Fn("scriptConsole", a => DynValue.NewString(Plugin!.ScriptConsoleText(Str(a, 0))));
Fn("combatReload", a => { FakeCombat.Reload(); return DynValue.Nil; });
// The text of a script in the plugin's Examples folder (so a test can run it).
Fn("exampleCode", a => DynValue.NewString(System.IO.File.ReadAllText(ExamplePath(Str(a, 0)))));
Fn("scriptHooks", a =>
{
var (keepAlive, onStop) = Plugin!.ScriptHookCounts(Str(a, 0));
var t = new Table(s);
t["keepAlive"] = (double)keepAlive; t["onStop"] = (double)onStop;
return DynValue.NewTable(t);
});
s.Globals["harness"] = DynValue.NewTable(h);
}
}