RynthSuite/Plugins/RynthCore.Plugin.RynthAi/Combat/SpellManager.cs
Daniel Wallace 88502d4efa Recast a buff at a higher level once one becomes castable
The buff level comes from the live buffed skill (SpellManager.ComputeTier over
BuffMinSkillLevelTier1..8) and the spellbook. Inside a force or batch run a family
cast earlier in the run read active, so Creature Enchantment Mastery, Focus and
Willpower cast at the level the skill allowed before they landed stayed there for
the whole run; between passes a failed upgrade could arm the landed-tier cap and
lose the upgrade until the plugin reloaded.

- In a run, a family already cast reads active only while the level castable now is
  not strictly higher than the level the run CONFIRMED for it; a higher level is cast
  again right away (after the priority buffs). Never lower.
- The attempted tier kept since load is written on confirmation, so a fizzled or refused
  upgrade is cast again at the next opportunity, in the run or on the next pass.
- No try cap, and no rest after a fizzle, a client refusal or a no-show; a server mana or
  component refusal still rests the family as it does for any buff. Casting thresholds
  are consistent, so the bot keeps trying until it lands. Pacing stays: a client-refused cast waits
  SpellCastIntervalMs, and a fizzled upgrade is sent once per server answer, never
  while one is unanswered.
- While the pending cast is an upgrade, the registry counts the family as landed only
  when its entry moved (a later expiry, or the pending level or higher). A late "You
  cast" line for a lower level, or one the cast tracker credits to an earlier send,
  does not confirm the pending upgrade.
- An upgrade's no-show is never a silent-no-show strike, a 30 min park or a
  blacklist entry, so the lower level it was cast over is still recast when it runs low.
- One "[Buff] upgrade: <buff> level a -> b" line per cast that replaces an active
  lower level (BuffUpgrades counts them), at the same effective level IsBuffActive
  uses: refreshing an Incantation that lands a level low is not an upgrade.
- A scroll read that teaches a spell ("The scroll is destroyed.") makes the next buff
  check read the spellbook at once.
- Integrates with the mana reserve hold (BuffWaitsForMana): the hold runs first, on
  the upgrade's own spell id, so an upgrade that would leave mana under the reserve
  is not sent, counted or logged; Stamina to Mana goes and the upgrade follows.

Tests: in-run order, next-pass upgrade with equal-level and lower-skill controls,
the scroll case, fizzle and refusal retries (run, next pass, heartbeat, client
refusal pacing), registry no-shows, late lines, no park on no-shows (self and item
spells), and an upgrade held by the mana reserve (fails with the hold disabled).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-12 17:14:02 -07:00

428 lines
21 KiB
C#

using System;
using System.Collections.Generic;
using RynthCore.Plugin.RynthAi.LegacyUi;
using RynthCore.PluginSdk;
namespace RynthCore.Plugin.RynthAi;
public class SpellManager
{
private readonly RynthCoreHost _host;
private readonly LegacyUiSettings _settings;
private CharacterSkills? _charSkills;
private uint _playerId;
// Spell ids a cast empirically proved the char does NOT know (AC sent zero
// chat back → silently dropped). This is the trustworthy "unknown" signal:
// the engine IsSpellKnown oracle is known-broken (hardcoded VA, returns
// true for unknown spells), so tier-down can't rely on it. BuffManager
// feeds these in on a no-chat timeout; TryGetId then skips them so
// GetDynamicSelfBuffId falls to the next lower (known) tier and caches it.
private readonly HashSet<int> _unresolvableSpellIds = new();
// Authoritative known-spell inventory: snapshot of the char's actual
// spellbook, read by the engine ON AC's main thread (the per-spell
// IsSpellKnown function is mis-bound and lies "true" for unknown spells;
// the off-thread pump can't read AC live). Populated via host
// ReadKnownSpells. Empty = snapshot cold → TryGetId degrades to the
// legacy path (no regression).
private readonly HashSet<int> _knownSpellIds = new();
private DateTime _lastKnownRefreshUtc = DateTime.MinValue;
private const double KnownRefreshThrottleMs = 3000;
public Dictionary<string, int> SpellDictionary { get; private set; } =
new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
// All ids per name (SpellDictionary keeps one). Lets TryGetId find the id this
// character actually knows when a name has several (weapon masteries, Item Tinkering).
private Dictionary<string, List<int>> _nameToIds = new(StringComparer.OrdinalIgnoreCase);
// Map base spell names to their exact Level 7 Lore counterparts
private readonly Dictionary<string, string[]> LoreNames = new(StringComparer.OrdinalIgnoreCase)
{
{ "Strength", new[] { "Might of the Lugians" } },
{ "Endurance", new[] { "Preservance", "Perseverance" } },
{ "Coordination", new[] { "Honed Control" } },
{ "Quickness", new[] { "Hastening" } },
{ "Focus", new[] { "Inner Calm" } },
{ "Willpower", new[] { "Mind Blossom" } },
{ "Invulnerability", new[] { "Aura of Defense" } },
{ "Impregnability", new[] { "Aura of Deflection" } },
{ "Magic Resistance", new[] { "Aura of Resistance" } },
{ "Armor", new[] { "Executor's Blessing" } },
{ "Acid Protection", new[] { "Caustic Blessing" } },
{ "Blade Protection", new[] { "Blessing of the Blade Turner" } },
{ "Bludgeoning Protection", new[] { "Blessing of the Mace Turner" } },
{ "Cold Protection", new[] { "Icy Blessing" } },
{ "Fire Protection", new[] { "Fiery Blessing" } },
{ "Lightning Protection", new[] { "Storm's Blessing" } },
{ "Mana Renewal", new[] { "Battlemage's Blessing" } },
{ "Piercing Protection", new[] { "Blessing of the Arrow Turner" } },
{ "Regeneration", new[] { "Robustify" } },
{ "Rejuvenation", new[] { "Unflinching Persistence", "Unfinching Persistance" } },
{ "Heal", new[] { "Adja's Intervention" } },
{ "Revitalize", new[] { "Robustification" } },
{ "Stamina to Mana", new[] { "Meditative Trance" } },
{ "Stamina to Health", new[] { "Rushed Recovery" } },
{ "Monster Attunement", new[] { "Topheron's Blessing" } },
{ "Person Attunement", new[] { "Kaluhc's Blessing" } },
{ "Arcane Enlightenment", new[] { "Aliester's Blessing" } },
{ "Armor Tinkering Expertise", new[] { "Jibril's Blessing" } },
{ "Item Tinkering Expertise", new[] { "Yoshi's Blessing" } },
{ "Weapon Tinkering Expertise", new[] { "Koga's Blessing" } },
{ "Mana Conversion Mastery", new[] { "Nuhmudira's Blessing" } },
{ "Two Handed Combat Mastery", new[] { "Blessing of T'ing" } },
{ "Magic Item Tinkering Expertise", new[] { "Celdiseth's Blessing" } },
{ "Sprint", new[] { "Saladur's Blessing" } },
{ "Jumping Mastery", new[] { "Jahannan's Blessing" } },
{ "Fealty", new[] { "Odif's Blessing", "Odif's Boon" } },
{ "Leadership Mastery", new[] { "Ar-Pei's Blessing" } },
{ "Deception Mastery", new[] { "Ketnan's Blessing" } },
{ "Healing Mastery", new[] { "Avalenne's Blessing" } },
{ "Lockpick Mastery", new[] { "Oswald's Blessing" } },
{ "Cooking Mastery", new[] { "Morimoto's Blessing" } },
{ "Fletching Mastery", new[] { "Lilitha's Blessing" } },
{ "Alchemy Mastery", new[] { "Silencia's Blessing" } },
{ "Creature Enchantment Mastery", new[] { "Adja's Blessing" } },
{ "Item Enchantment Mastery", new[] { "Celcynd's Blessing" } },
{ "Life Magic Mastery", new[] { "Harlune's Blessing" } },
{ "War Magic Mastery", new[] { "Hieromancer's Blessing" } },
{ "Blood Drinker", new[] { "Aura of Infected Caress" } },
{ "Hermetic Link", new[] { "Aura of Mystic's Blessing" } },
{ "Heart Seeker", new[] { "Aura of Elysa's Sight" } },
{ "Spirit Drinker", new[] { "Aura of Infected Spirit Carress", "Aura of Infected Spirit Caress" } },
{ "Swift Killer", new[] { "Aura of Atlan's Alacrity" } },
{ "Defender", new[] { "Aura of Cragstone's Will" } },
{ "Impenetrability", new[] { "Brogard's Defiance" } },
{ "Acid Bane", new[] { "Olthoi's Bane" } },
{ "Blade Bane", new[] { "Swordsman's Bane", "Swordman's Bane" } },
{ "Bludgeoning Bane", new[] { "Tusker's Bane" } },
{ "Flame Bane", new[] { "Inferno's Bane" } },
{ "Frost Bane", new[] { "Gelidite's Bane" } },
{ "Lightning Bane", new[] { "Astyrrian's Bane" } },
{ "Piercing Bane", new[] { "Archer's Bane" } },
};
public SpellManager(RynthCoreHost host, LegacyUiSettings settings)
{
_host = host;
_settings = settings;
}
public void SetCharacterSkills(CharacterSkills skills) => _charSkills = skills;
public void SetPlayerId(uint playerId) => _playerId = playerId;
public void InitializeNatively()
{
try
{
if (!SpellDatabase.IsLoaded)
SpellDatabase.Load(msg => _host.WriteToChat(msg, 2));
SpellDictionary = SpellDatabase.BuildNameToIdMap();
_nameToIds = SpellDatabase.BuildNameToIdsMap();
_host.WriteToChat($"[RynthAi] Magic System Online: {SpellDictionary.Count} spells loaded.", 1);
}
catch (Exception ex)
{
_host.WriteToChat("[RynthAi] Spell init error: " + ex.Message, 2);
}
}
/// <summary>
/// Returns the highest castable spell tier for combat (offensive/debuff) casts.
/// Uses the combat thresholds in LegacyUiSettings (MinSkillLevelTier1..8) which are
/// padded above AC's hard minimums (1/50/100/150/200/250/300/350) to avoid fizzles
/// during fights.
/// </summary>
public int GetHighestSpellTier(AcSkillType skill)
=> ComputeTier(skill,
_settings.MinSkillLevelTier1, _settings.MinSkillLevelTier2,
_settings.MinSkillLevelTier3, _settings.MinSkillLevelTier4,
_settings.MinSkillLevelTier5, _settings.MinSkillLevelTier6,
_settings.MinSkillLevelTier7, _settings.MinSkillLevelTier8);
/// <summary>
/// Same as <see cref="GetHighestSpellTier"/> but uses the Buffing-specific thresholds
/// (BuffMinSkillLevelTier1..8). Buffing fizzles are inexpensive — just retry next
/// heartbeat — so these defaults sit closer to AC's hard minimums, ensuring a
/// 390-buffed caster actually receives Incantation-tier self buffs.
/// </summary>
public int GetHighestBuffSpellTier(AcSkillType skill)
=> ComputeTier(skill,
_settings.BuffMinSkillLevelTier1, _settings.BuffMinSkillLevelTier2,
_settings.BuffMinSkillLevelTier3, _settings.BuffMinSkillLevelTier4,
_settings.BuffMinSkillLevelTier5, _settings.BuffMinSkillLevelTier6,
_settings.BuffMinSkillLevelTier7, _settings.BuffMinSkillLevelTier8);
private int ComputeTier(AcSkillType skill, int t1, int t2, int t3, int t4, int t5, int t6, int t7, int t8)
{
int buffed = _charSkills != null ? _charSkills[skill].Buffed : 250;
if (buffed >= t8) return 8;
if (buffed >= t7) return 7;
if (buffed >= t6) return 6;
if (buffed >= t5) return 5;
if (buffed >= t4) return 4;
if (buffed >= t3) return 3;
if (buffed >= t2) return 2;
return 1;
}
private static string GetRomanNumeral(int tier) => tier switch
{
1 => "I", 2 => "II", 3 => "III", 4 => "IV",
5 => "V", 6 => "VI", 7 => "VII", 8 => "VIII",
_ => "I"
};
/// <summary>
/// The LOWEST known tier of <paramref name="baseSpellName"/> above the buff tier the skill
/// allows, 0 when none. For heals when nothing at or under the allowed tier is known: vitae
/// can drop Life Magic under a tier line while the character only knows higher tiers, and a
/// heal that may fizzle beats none (Drakkon died twice, 2026-10-06).
/// </summary>
public int GetSelfSpellIdAboveAllowedTier(string baseSpellName, AcSkillType magicSkill)
{
RefreshKnownSpells();
int maxTier = GetHighestBuffSpellTier(magicSkill);
string cleanBase = baseSpellName.Replace(" Self", "").Trim();
BaseNameAliases.TryGetValue(cleanBase, out string? alias);
for (int tier = maxTier + 1; tier <= 8; tier++)
{
if (TryGetTierId(cleanBase, tier, out int id)) return id;
if (alias != null && TryGetTierId(alias, tier, out int idAlias)) return idAlias;
}
return 0;
}
/// <param name="skip">Levels to pass over (a level the server refused for missing components):
/// the next lower known level is returned instead.</param>
public int GetDynamicSelfBuffId(string baseSpellName, AcSkillType magicSkill, Func<int, bool>? skip = null)
{
RefreshKnownSpells(); // keep the authoritative inventory current (throttled)
// Self-buffs use the buffing-specific thresholds so the user can tune
// buffing tier independently from combat tier.
int maxTier = GetHighestBuffSpellTier(magicSkill);
string cleanBase = baseSpellName.Replace(" Self", "").Trim();
BaseNameAliases.TryGetValue(cleanBase, out string? alias);
for (int tier = maxTier; tier >= 1; tier--)
{
if (TryGetTierId(cleanBase, tier, out int id) && (skip == null || !skip(id))) return id;
if (alias != null && TryGetTierId(alias, tier, out int idAlias) && (skip == null || !skip(idAlias))) return idAlias;
}
return 0;
}
// The spell data spells some families differently from the buff list: the
// bludgeon bane is "Bludgeon Bane I-VI" / "Incantation of Bludgeon Bane", so
// "Bludgeoning Bane" only resolved at tier 7 (Tusker's Bane).
private static readonly Dictionary<string, string> BaseNameAliases = new(StringComparer.OrdinalIgnoreCase)
{
{ "Bludgeoning Bane", "Bludgeon Bane" },
};
private bool TryGetTierId(string cleanBase, int tier, out int id)
{
if (tier == 8)
{
if (TryGetId($"Incantation of {cleanBase} Self", out id)) return true;
if (TryGetId($"Incantation of {cleanBase}", out id)) return true;
if (TryGetId($"Aura of Incantation of {cleanBase} Self", out id)) return true;
if (TryGetId($"Aura of Incantation of {cleanBase}", out id)) return true;
}
else if (tier == 7)
{
if (LoreNames.TryGetValue(cleanBase, out string[]? lores))
foreach (string lore in lores)
if (TryGetId(lore, out id)) return true;
if (TryGetId($"{cleanBase} Self VII", out id)) return true;
if (TryGetId($"{cleanBase} VII", out id)) return true;
if (TryGetId($"Aura of {cleanBase} Self VII", out id)) return true;
if (TryGetId($"Aura of {cleanBase} VII", out id)) return true;
}
else
{
string numeral = GetRomanNumeral(tier);
if (TryGetId($"{cleanBase} Self {numeral}", out id)) return true;
if (TryGetId($"{cleanBase} {numeral}", out id)) return true;
if (TryGetId($"Aura of {cleanBase} Self {numeral}", out id)) return true;
if (TryGetId($"Aura of {cleanBase} {numeral}", out id)) return true;
}
id = 0;
return false;
}
/// <summary>
/// Record that a spell id is not actually castable by this character
/// (empirically: a cast of it got zero chat back). Resolution will skip
/// it from now on so the family falls to the next lower tier.
/// </summary>
public void MarkSpellUnresolvable(int spellId)
{
if (spellId > 0) _unresolvableSpellIds.Add(spellId);
}
/// <summary>Persisted by BuffManager so the tier-7→6 drop is learned once
/// (first rebuff per char) then instant on every later session/RL.</summary>
public IReadOnlyCollection<int> UnresolvableSpellIds => _unresolvableSpellIds;
/// <summary>
/// Public, authoritative name→id resolver shared by the combat war/void
/// path so it gets the SAME logic the buff path uses: unresolvable skip +
/// the main-thread <c>_knownSpellIds</c> snapshot (the engine IsSpellKnown
/// oracle is mis-bound and lies "true" for unknown spells). A char that
/// doesn't know e.g. the tier-8 Incantation resolves false here, so the
/// caller's tier loop drops to a tier it actually knows. Call
/// <see cref="RefreshKnownSpells"/> on the consuming path first to keep the
/// snapshot warm even when not buffing.
/// </summary>
public bool TryResolveSpellId(string exactName, out int spellId)
=> TryGetId(exactName, out spellId);
/// <summary>
/// Combat-facing resolver. Deterministic: name → id via the dictionary,
/// then REQUIRES membership in the authoritative warm spellbook snapshot.
/// Deliberately does NOT consult <c>_unresolvableSpellIds</c> (the
/// empirical no-chat blacklist) or the mis-bound engine oracle — combat
/// selection is purely predictive (known ∧ scarab ∧ tier ∧ shape), so
/// nothing here can self-poison. Cold snapshot ⇒ false (combat waits for
/// the snapshot to warm rather than blind-casting an unknown spell).
/// </summary>
public bool TryResolveKnownSpellId(string exactName, out int spellId)
{
spellId = 0;
if (!SpellDictionary.TryGetValue(exactName, out int id)) return false;
if (_knownSpellIds.Count == 0) return false; // cold — don't guess
if (!_knownSpellIds.Contains(id))
{
// Same name, another id (as TryGetId): the map keeps the LAST id of a name, and
// war names have duplicates, e.g. "Acid Blast III" is 99 (the one players learn)
// and 3653, "Flame Bolt I" is 27 and four others. A char who knew 99 never cast it.
if (!_nameToIds.TryGetValue(exactName, out List<int>? ids)) return false;
id = 0;
foreach (int alt in ids)
if (_knownSpellIds.Contains(alt)) { id = alt; break; }
if (id == 0) return false; // char doesn't know it
}
spellId = id;
return true;
}
/// <summary>
/// True once the authoritative main-thread spellbook snapshot has been
/// populated. While false (cold: pre-login, fast cold-login before the
/// qualities ptr seeds, or an engine without ReadKnownSpells) TryGetId
/// degrades to the mis-bound IsSpellKnown oracle, which lies "true" for
/// unknown spells. The combat resolver uses this to refuse to blind-pick
/// the tier-8 Incantation during a cold window.
/// </summary>
public bool IsKnownSnapshotWarm => _knownSpellIds.Count > 0;
/// <summary>
/// True if the authoritative main-thread spellbook snapshot contains this
/// id. Only meaningful when <see cref="IsKnownSnapshotWarm"/> is true.
/// Used by the combat no-chat valve to refuse to blacklist a spell the
/// char demonstrably KNOWS — no chat for a known spell means the cast
/// didn't execute (off-thread busy-count leak), NOT "unknown/no comps".
/// </summary>
public bool IsKnownSpellId(int id) => _knownSpellIds.Contains(id);
/// <summary>The next RefreshKnownSpells reads the spellbook at once (a spell was just learned).</summary>
public void InvalidateKnownSpells() => _lastKnownRefreshUtc = DateTime.MinValue;
/// <summary>
/// Refresh the known-spell inventory from the engine's main-thread
/// spellbook snapshot (host ReadKnownSpells). Throttled. Safe no-op if the
/// host API is absent (old engine) or the snapshot is still cold — the set
/// just stays empty and TryGetId keeps using the legacy path.
/// </summary>
public void RefreshKnownSpells()
{
if ((DateTime.UtcNow - _lastKnownRefreshUtc).TotalMilliseconds < KnownRefreshThrottleMs)
return;
_lastKnownRefreshUtc = DateTime.UtcNow;
try
{
if (!_host.HasReadKnownSpells) return;
// Grow the buffer until it is NOT completely filled. A full buffer means
// the spellbook was TRUNCATED — and because higher spell tiers carry
// higher ids, the dropped entries are exactly the high-tier self-buffs
// (Strength Self VI/VII/VIII, Incantations…). That collapses the tier
// walk to the low-id tier that survived (Strength Self I = id 2). A full
// buffing mage knows well over 2048 spells, so a fixed 2048 cap is the
// real "casts level 1" bug. Cap at 65536 as a sanity bound.
uint[] buf;
int n;
int cap = 8192;
while (true)
{
buf = new uint[cap];
n = _host.ReadKnownSpells(buf, buf.Length);
if (n < cap || cap >= 65536) break; // got the whole spellbook (or hit sanity cap)
cap *= 2;
}
if (n <= 0) return; // cold/unavailable — keep whatever we have
var fresh = new HashSet<int>();
int take = Math.Min(n, buf.Length);
for (int i = 0; i < take; i++)
if (buf[i] != 0) fresh.Add(unchecked((int)buf[i]));
if (fresh.Count == 0) return;
_knownSpellIds.Clear();
foreach (int id in fresh) _knownSpellIds.Add(id);
// Purge stale blacklist entries. The empirical blacklist accumulates
// false positives from lag-induced no-chat timeouts and persists to
// disk across sessions — higher tiers get permanently suppressed,
// causing tier walk-down to level 1. Now that the snapshot is
// authoritative, any blacklisted spell the char demonstrably knows
// was a false positive; remove it so the correct tier resolves.
_unresolvableSpellIds.RemoveWhere(id => _knownSpellIds.Contains(id));
}
catch { }
}
private bool TryGetId(string exactName, out int spellId)
{
if (!SpellDictionary.TryGetValue(exactName, out spellId))
return false;
// Authoritative snapshot wins unconditionally when warm: if the char
// demonstrably knows the spell, trust that over the empirical blacklist
// (which can be poisoned by lag-induced no-chat timeouts).
if (_knownSpellIds.Count > 0)
{
if (_knownSpellIds.Contains(spellId)) return true;
// Same name, another id: an archer who knows the bow line of Missile
// Weapon Mastery (472) failed every tier because the map held the
// thrown line (544).
if (_nameToIds.TryGetValue(exactName, out List<int>? ids))
foreach (int alt in ids)
if (_knownSpellIds.Contains(alt)) { spellId = alt; return true; }
spellId = 0;
return false;
}
// Snapshot cold — fall back to the empirical blacklist. The mis-bound
// engine IsSpellKnown is still bounded by _unresolvableSpellIds so the
// tier walk doesn't spin on silently-dropped unknown spells.
if (_unresolvableSpellIds.Contains(spellId))
{
spellId = 0;
return false;
}
if (_playerId != 0 && _host.HasIsSpellKnown)
{
_host.IsSpellKnown(_playerId, unchecked((uint)spellId), out bool known);
if (!known) { spellId = 0; return false; }
return true;
}
if (SpellDatabase.IsSpellKnown(spellId)) return true;
spellId = 0;
return false;
}
}