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:
parent
fe1eca0ca7
commit
10e2c2eb0a
30 changed files with 3620 additions and 27 deletions
|
|
@ -0,0 +1,104 @@
|
|||
using System;
|
||||
using RynthCore.PluginSdk;
|
||||
using RynthCore.Plugin.RynthAi.LegacyUi;
|
||||
using RynthCore.Plugin.Shared;
|
||||
|
||||
namespace RynthCore.Plugin.RynthAi;
|
||||
|
||||
// The heal seam of the script-provided combat hook (RynthAi.Combat, CombatHookInterface.cs):
|
||||
// CheckVitals asks the script for the heal spell (and its target: the player, or a monster for a
|
||||
// drain) before kits. The cast itself goes through CastVital, the same steps as Heal Self.
|
||||
public partial class BuffManager
|
||||
{
|
||||
/// <summary>
|
||||
/// True when the hook handled this vitals check: waiting for the answer, or the answer was
|
||||
/// cast (or its wand swap is in flight). False = the native order (kit, Heal Self, potion).
|
||||
/// </summary>
|
||||
private bool TryScriptHeal(int healLine, bool inCombat)
|
||||
{
|
||||
// AC just said "You're too busy!": the same pacing as a Heal Self (AttemptVitalCast).
|
||||
if (DateTime.Now < _busyBackoffUntil && (DateTime.Now - _lastCastAttempt).TotalMilliseconds < BusyHealRetryMs)
|
||||
{
|
||||
_vitalWhy = $"AC too busy, waiting {(_busyBackoffUntil - DateTime.Now).TotalMilliseconds:0} ms";
|
||||
_healWaitKind = "too-busy backoff";
|
||||
return true;
|
||||
}
|
||||
|
||||
var slot = CombatHook.Heal;
|
||||
// The request names RynthAi's current fight target (informational: the monster with live
|
||||
// health data, e.g. for a drain); a fight target change re-asks. The answer's target 0
|
||||
// still means the player.
|
||||
var cm = _combatManager;
|
||||
uint fight = cm == null ? 0u : unchecked((uint)cm.activeTargetId);
|
||||
var outcome = CombatHook.Step(slot, fight, () => BuildHealRequest(healLine, inCombat), s => _host.Log(s),
|
||||
out uint spell, out uint target, out _);
|
||||
switch (outcome)
|
||||
{
|
||||
case CombatHook.Outcome.Native:
|
||||
return false;
|
||||
case CombatHook.Outcome.Wait:
|
||||
_vitalWhy = $"waiting for '{CombatHook.Owner}' to choose the heal";
|
||||
_healWaitKind = "script choosing the heal";
|
||||
return true;
|
||||
}
|
||||
|
||||
uint player = (uint)_host.GetPlayerId();
|
||||
if (target == 0) target = player;
|
||||
string? why = ScriptHealProblem(spell, target, player);
|
||||
if (why != null)
|
||||
{
|
||||
CombatHook.Reject(slot, why, s => _host.Log(s));
|
||||
return false;
|
||||
}
|
||||
string name = SpellTableStub.GetById((int)spell)?.Name ?? $"spell {spell}";
|
||||
bool handled = CastVital((int)spell, target, LabelHealing, name + " (script)", out bool sent);
|
||||
if (!handled)
|
||||
{
|
||||
CombatHook.Reject(slot, _vitalWhy.Length > 0 ? _vitalWhy : "it couldn't be cast", s => _host.Log(s));
|
||||
return false;
|
||||
}
|
||||
// A script-chosen heal takes no early nudge (HeldNudgeUrgent): its result can come later
|
||||
// than RynthAi's own heals' (Vitality Siphon: 2.42-2.75 s after the incantation, 2026-10-10
|
||||
// log), or never (a resisted or dead target), and an early nudge steps the character.
|
||||
if (sent && _cast.Current is { } sentCast && sentCast.SpellId == (int)spell) sentCast.Script = true;
|
||||
// Handled: cast, or a swap step to Magic mode is in flight (the next check asks again).
|
||||
CombatHook.Cast(slot, sent ? spell : 0, sent ? target : 0);
|
||||
if (sent) _host.Log($"[RynthAi] Vitals: script heal id={spell} '{name}' at 0x{target:X8} (owner '{CombatHook.Owner}').");
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>Why the script's heal can't be cast, or null when it can.</summary>
|
||||
private string? ScriptHealProblem(uint spell, uint target, uint player)
|
||||
{
|
||||
if (!_spellManager.IsKnownSpellId((int)spell)) return $"spell {spell} isn't known";
|
||||
// The refusal memory RynthAi's own heal honours (AttemptVitalCast): a level the server
|
||||
// said there are no components for.
|
||||
if (LevelLacksComponents((int)spell)) return $"spell {spell} has no components (refused by the server; RynthAi uses a lower level)";
|
||||
if (target == player) return null;
|
||||
var cm = _combatManager;
|
||||
bool listed = cm != null && unchecked((uint)cm.activeTargetId) == target;
|
||||
if (!listed && cm != null)
|
||||
foreach (var t in cm.ScannedTargets)
|
||||
if (unchecked((uint)t.Id) == target) { listed = true; break; }
|
||||
if (!listed) return "the target is not the player, the fight target or a scanned monster";
|
||||
if (_host.HasObjectIsAttackable && !_host.ObjectIsAttackable(target)) return "the target is not attackable";
|
||||
return null;
|
||||
}
|
||||
|
||||
private CombatRequestV1 BuildHealRequest(int healLine, bool inCombat)
|
||||
{
|
||||
bool known = _vitals.MaxHealth > 0;
|
||||
var cm = _combatManager;
|
||||
float fightHealth = cm?.FightTargetHealthRatio ?? -1f;
|
||||
return new CombatRequestV1
|
||||
{
|
||||
TargetHealth = fightHealth,
|
||||
HealthPct = known ? _vitals.HealthPct : -1,
|
||||
StaminaPct = _vitals.MaxStamina > 0 ? _vitals.StaminaPct : -1,
|
||||
ManaPct = _vitals.MaxMana > 0 ? _vitals.ManaPct : -1,
|
||||
HealLine = healLine,
|
||||
InCombat = inCombat ? 1 : 0,
|
||||
CombatMode = CurrentCombatMode,
|
||||
};
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue