RynthSuite/Tools/RynthCore.RynthAiHostTests/Tests/BuffTests.cs
Daniel Wallace 36565dca33 Buff order priority, unusable-buff filter, Force Buff button and /ra buff
- Life Magic Mastery joins Creature Enchantment Mastery, Focus and Willpower at the
  head of the buff list (all four raise casting skill or mana); the rest keeps its order.
- A mastery for an untrained skill is never cast.
- New setting SkipUnusableBuffs (default on; profile, bridge, copy, settings map, UI)
  leaves out the weapon enchantments the character has no use for: Blood Drinker /
  Heart Seeker / Swift Killer without a melee or missile weapon (listed or in hand),
  Spirit Drinker / Hermetic Link without a wand (listed, in hand, or the pack wand
  buffing would wield when WieldUnlistedWandWhenNoneListed is on), Defender without
  either. Unclassifiable listed items filter nothing; no cache means no filtering;
  ammo is no weapon. The weapon kit is cached for 1 s and rebuilt when the Items
  list, the unlisted-wand setting or the cache changes.
- Skipped buffs are reported once per run (Force Rebuff or the automatic batch),
  grouped by reason and capped at 5 names per reason, printed when the content
  changes or every 30 s, in chat and the log, in /ra buff skipped, and on the
  Buffing settings page.
- Force Buff and Cancel buttons on the Buffing settings page, and /ra buff
  force|cancel|skipped, both reusing the existing queued Force Rebuff request
  (FR button, /ra forcebuff).

Tests: priority order, force suite vs idle, skill and weapon filters (pack wand,
wielded weapons, ammo, unclassified items), visibility, the setting, and the
/ra buff command handler.

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

1226 lines
72 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text.Json;
using RynthCore.Loot;
using RynthCore.Plugin.RynthAi;
using RynthCore.Plugin.RynthAi.LegacyUi;
using RynthCore.PluginCore;
using RynthCore.PluginSdk;
using RynthCore.RynthAiHostTests.Fakes;
using RynthCore.RynthAiTests.Harness;
namespace RynthCore.RynthAiHostTests.Tests;
// Buff and spell selection: which tier a buffed skill allows (SpellManager), which spell id a
// buff resolves to at each tier (lore names, Incantations, auras, banes, the spellbook
// snapshot), how long the bot thinks a buff lasts, and when it recasts (BuffManager.NeedsAnyBuff
// over the live enchantment registry and chat-confirmed item spells). Expectations about spell
// names, tiers and durations are read from the plugin's own SpellData.txt (Fakes\SpellTable).
internal static class BuffTests
{
private const uint Player = 0x50000B01;
private const uint STypeCreature = 31, STypeItem = 32, STypeLife = 33;
/// <summary>Buff/tier pairs the name tables used to miss (fixed by 940cb21); NameGapsResolve checks them by name.</summary>
private static readonly HashSet<(string Buff, int Tier)> NameGaps = new()
{
("Bludgeoning Bane", 1), ("Bludgeoning Bane", 2), ("Bludgeoning Bane", 3), ("Bludgeoning Bane", 4),
("Bludgeoning Bane", 5), ("Bludgeoning Bane", 6), ("Bludgeoning Bane", 8),
("Mana Conversion Mastery", 7), ("Two Handed Combat Mastery", 7), ("Magic Item Tinkering Expertise", 7),
};
private static readonly string[] Auras =
{ "Blood Drinker Self", "Hermetic Link Self", "Heart Seeker Self", "Spirit Drinker Self", "Swift Killer Self", "Defender Self" };
public static void Register(Runner r)
{
r.Add("buff tier: highest tier for a buffed skill, at every threshold", TierThresholds);
r.Add("buff tier: a skill that can't be read doesn't collapse to tier 1", TierUnreadableSkills);
r.Add("buff choice: the highest known tier, lower when a tier isn't known", KnownTierWalk);
r.Add("buff choice: a cold spellbook uses the no-chat blacklist; a warm one overrides it", ColdAndWarm);
r.Add("buff choice: every buff in the list resolves at every tier (checked against the spell data)", EveryBuffEveryTier);
r.Add("buff choice: tier 7 lore names and tier 8 forms", LoreAndIncantations);
r.Add("buff level: spell level from the name", SpellLevels);
r.Add("buff timers: the duration the bot assumes matches the spell data", DurationsMatchData);
r.Add("buff timers: Archmage's Endurance adds 20% a rank, at most 5", ArchmageEndurance);
r.Add("buff school: which magic skill casts each buff", SkillForBuff);
r.Add("buff school: which buffs live on an item", ItemEnchantments);
r.Add("rebuff: recast only under the rebuff threshold", RebuffThreshold);
r.Add("rebuff: permanent, expired, and two enchantments in one family", RebuffPermanentAndDuplicates);
r.Add("rebuff: a higher tier is only chased above the tier that has landed", RebuffTierUpgrade);
r.Add("rebuff: a chat-confirmed item spell counts as on", ItemSpellFromChat);
r.Add("rebuff: our own lower tier under an expiring higher one counts as on", RebuffUnderExpiringHigherTier);
r.Add("rebuff: cast over item-granted buffs (CastBuffsOverItemBuffs on)", RebuffOverItemBuffs);
r.Add("rebuff: CastBuffsOverItemBuffs defaults on, loads, saves, bridges", OverItemBuffsSetting);
r.Add("buff toggles: by default every prot and bane is cast", TogglesDefaultAllOn);
r.Add("buff toggles: an unticked prot or bane is never cast and never holds NeedsAnyBuff", UntickedSkipped);
r.Add("buff toggles: an old profile loads all on; the list saves, copies and bridges", TogglesSetting);
r.Add("buff order: Creature Enchantment, Focus, Willpower, Life Magic come first", PriorityOrder);
r.Add("buff order: a run casts them first, and a lone expiring buff is still just recast", PriorityCastOrderAndLoneExpiry);
r.Add("force buff: recasts the whole filtered suite in order; without it nothing is cast", ForceBuffSuite);
r.Add("force buff: /ra buff force and the button reach the same request", ForceBuffEntryPoints);
r.Add("buff filter: a mastery for an untrained skill is never cast, trained is; the setting only controls the report", SkillFilter);
r.Add("buff filter: weapon auras follow the registered melee/missile weapon or wand", WeaponFilter);
r.Add("buff filter: an unlisted pack wand counts as a caster when WieldUnlistedWandWhenNoneListed is on", UnlistedWandCounts);
r.Add("buff filter: an unclassifiable listed item, no cache, ammo, wielded weapons", KitEdgeCases);
r.Add("buff filter: the skip line groups by reason, is capped, and repeats only when it changes", SkipLineGroupedAndCapped);
r.Add("buff filter: the weapon kit is cached for a second, and rebuilt when its inputs change", KitCached);
r.Add("buff filter: skipped buffs are named, once per run", FilterVisibility);
r.Add("buff filter: SkipUnusableBuffs defaults on, loads, saves, bridges, is in the settings map", SkipUnusableSetting);
r.Add("buff choice: spell names the tables miss (Bludgeon Bane, Nuhmudira's, T'ing, Celdiseth's)", NameGapsResolve); // was a known failure; fixed by 940cb21
r.Add("buff choice: a mastery whose name has several spell ids", MultiIdMastery); // was a known failure; fixed by 0aedcee
r.Add("buff timers: tier-8 weapon auras last 90 minutes", TierEightAuras); // was a known failure; fixed by 9a317a8
r.Add("buff chat: another player's speech can't record a buff timer", SpeechIsNotACast); // was a known failure; fixed by 0e384d2
r.Add("buff school: Armor Tinkering Expertise is a Creature Enchantment spell", ArmorTinkeringSchool); // was a known failure
}
// ── Rig ───────────────────────────────────────────────────────────────────
private static int[] Ids(string name) => SpellTable.Named(name).Select(r => r.Id).ToArray();
private static int Id(string name) => Ids(name).FirstOrDefault();
private static string NameOf(int id) => SpellTable.ById(id)?.Name ?? (id == 0 ? "(none)" : $"#{id}");
/// <summary>The tier the spell data gives a spell: its numeral, else 7 for power 300, 8 for 350+.</summary>
private static int DataTier(SpellTable.Row row)
{
string last = row.Name.Split(' ').Last();
int roman = last switch { "I" => 1, "II" => 2, "III" => 3, "IV" => 4, "V" => 5, "VI" => 6, "VII" => 7, "VIII" => 8, _ => 0 };
if (roman > 0) return roman;
if (row.Power >= 350 || row.Name.StartsWith("Incantation", StringComparison.Ordinal)) return 8;
return row.Power == 300 ? 7 : 0;
}
/// <summary>Buff thresholds that make a 250 skill (the no-skills stub) cast exactly <paramref name="tier"/>.</summary>
private static void ForceBuffTier(LegacyUiSettings s, int tier)
{
int T(int k) => k <= tier ? 0 : 10000;
s.BuffMinSkillLevelTier2 = T(2); s.BuffMinSkillLevelTier3 = T(3); s.BuffMinSkillLevelTier4 = T(4);
s.BuffMinSkillLevelTier5 = T(5); s.BuffMinSkillLevelTier6 = T(6); s.BuffMinSkillLevelTier7 = T(7);
s.BuffMinSkillLevelTier8 = T(8);
}
private static SpellManager NewSpells(RynthCoreHost host, LegacyUiSettings s, CharacterSkills? skills = null)
{
var sm = new SpellManager(host, s);
sm.InitializeNatively();
if (skills != null) sm.SetCharacterSkills(skills);
return sm;
}
private static CharacterSkills Skills(RynthCoreHost host)
{
var cs = new CharacterSkills(host);
cs.SetPlayerId(Player);
return cs;
}
private static void KnowAll()
{
foreach (var row in SpellTable.Rows) FakeHost.Spellbook.Add((uint)row.Id);
}
private static void Know(params string[] names)
{
foreach (string n in names) foreach (int id in Ids(n)) FakeHost.Spellbook.Add((uint)id);
}
private sealed class BuffRig
{
public readonly LegacyUiSettings S = new() { EnableBuffing = true };
public readonly BuffManager B;
public readonly SpellManager Sm;
public readonly RynthCoreHost Host;
/// <summary>Call FakeHost.Reset(), set skills and the spellbook first.</summary>
public BuffRig()
{
var host = FakeHost.Create();
Host = host;
var skills = Skills(host);
Sm = NewSpells(host, S, skills);
B = new BuffManager(host, S, Sm, new PlayerVitalsCache());
B.SetCharacterSkills(skills);
FakeHost.ServerTime = 100_000;
}
/// <summary>Sets the live enchantment registry and reads it (remaining seconds; MaxValue = permanent).</summary>
public void Live(params (string Name, double Remaining)[] enchantments)
{
FakeHost.Enchantments.Clear();
foreach (var (name, remaining) in enchantments)
FakeHost.Enchantments.Add(((uint)Id(name), remaining == double.MaxValue ? double.MaxValue : FakeHost.ServerTime + remaining));
B.RefreshFromLiveMemory();
}
}
private static int BuffTier(int buffed, Action<LegacyUiSettings>? tweak = null, bool combat = false)
{
FakeHost.Reset();
FakeHost.Skills[(Player, STypeCreature)] = (buffed, 2);
var host = FakeHost.Create();
var s = new LegacyUiSettings();
tweak?.Invoke(s);
var sm = NewSpells(host, s, Skills(host));
return combat ? sm.GetHighestSpellTier(AcSkillType.CreatureEnchantment) : sm.GetHighestBuffSpellTier(AcSkillType.CreatureEnchantment);
}
// ── Tiers ─────────────────────────────────────────────────────────────────
private static void TierThresholds()
{
// Default thresholds: 85 / 135 / 185 / 235 / 285 / 335 / 435 for tiers 2-8.
var cases = new[] { (10, 1), (84, 1), (85, 2), (134, 2), (135, 3), (184, 3), (185, 4), (234, 4), (235, 5),
(284, 5), (285, 6), (334, 6), (335, 7), (434, 7), (435, 8), (600, 8) };
foreach (var (buffed, tier) in cases)
Check.Eq(BuffTier(buffed), tier, $"buffed {buffed}");
Check.Eq(BuffTier(450, s => s.MinSkillLevelTier8 = 500, combat: true), 7, "combat uses its own thresholds (tier 8 at 500)");
Check.Eq(BuffTier(450, s => s.MinSkillLevelTier8 = 500), 8, "... while buffing keeps its own (435)");
Check.Eq(BuffTier(300, s => s.BuffMinSkillLevelTier6 = 400), 5, "raising the tier-6 threshold drops a 300 to tier 5");
// Pinned: the tier-1 threshold is never read; anything below tier 2 is tier 1.
Check.Eq(BuffTier(10, s => s.BuffMinSkillLevelTier1 = 400), 1, "the tier-1 threshold is not read (pinned)");
}
private static void TierUnreadableSkills()
{
FakeHost.Reset();
var host = FakeHost.Create();
var s = new LegacyUiSettings();
Check.Eq(NewSpells(host, s).GetHighestBuffSpellTier(AcSkillType.CreatureEnchantment), 5, "no skills object: assumes 250 (tier 5)");
Check.Eq(NewSpells(host, s, Skills(host)).GetHighestBuffSpellTier(AcSkillType.CreatureEnchantment), 5,
"the skill read fails: assumes 250 (tier 5), not tier 1");
FakeHost.Skills[(Player, STypeCreature)] = (0, 2);
Check.Eq(NewSpells(host, s, Skills(host)).GetHighestBuffSpellTier(AcSkillType.CreatureEnchantment), 5,
"a trained skill reading 0 is an engine glitch: assumes 250");
var skills = Skills(host);
var sm = NewSpells(host, s, skills);
FakeHost.Skills[(Player, STypeCreature)] = (400, 2);
Check.Eq(sm.GetHighestBuffSpellTier(AcSkillType.CreatureEnchantment), 7, "a real read of 400: tier 7");
FakeHost.Skills[(Player, STypeCreature)] = (0, 2);
Check.Eq(sm.GetHighestBuffSpellTier(AcSkillType.CreatureEnchantment), 7, "then a 0 read keeps the last good one");
FakeHost.Skills[(Player, STypeCreature)] = (0, 1);
Check.Eq(NewSpells(host, s, Skills(host)).GetHighestBuffSpellTier(AcSkillType.CreatureEnchantment), 1,
"an untrained skill reading 0 is real: tier 1");
}
private static int Resolve(string buff, int buffedCreature, params string[] known)
{
FakeHost.Reset();
Know(known);
FakeHost.Skills[(Player, STypeCreature)] = (buffedCreature, 2);
var host = FakeHost.Create();
return NewSpells(host, new LegacyUiSettings(), Skills(host)).GetDynamicSelfBuffId(buff, AcSkillType.CreatureEnchantment);
}
private static void KnownTierWalk()
{
string[] all = { "Strength Self I", "Strength Self II", "Strength Self III", "Strength Self IV", "Strength Self V",
"Strength Self VI", "Might of the Lugians", "Incantation of Strength Self" };
Check.Eq(NameOf(Resolve("Strength Self", 500, all)), "Incantation of Strength Self", "skill 500, all known: tier 8");
Check.Eq(NameOf(Resolve("Strength Self", 500, all[..7])), "Might of the Lugians", "tier 8 not known: tier 7");
Check.Eq(NameOf(Resolve("Strength Self", 500, all[..5])), "Strength Self V", "only I-V known: V");
Check.Eq(NameOf(Resolve("Strength Self", 250, all)), "Strength Self V", "skill 250: tier 5 even with more known");
Check.Eq(NameOf(Resolve("Strength Self", 500, "Strength Self I", "Strength Self III")), "Strength Self III", "gaps are skipped");
Check.Eq(Resolve("Strength Self", 500, "Focus Self VI"), 0, "no Strength spell known: 0 (the buff is skipped)");
Check.Eq(NameOf(Resolve("Strength", 500, all)), "Incantation of Strength Self", "the base name works without ' Self'");
}
private static void ColdAndWarm()
{
FakeHost.Reset(); // empty spellbook: the snapshot is cold
FakeHost.Skills[(Player, STypeCreature)] = (500, 2);
var host = FakeHost.Create();
var sm = NewSpells(host, new LegacyUiSettings(), Skills(host));
Check.False(sm.IsKnownSnapshotWarm, "cold");
Check.Eq(NameOf(sm.GetDynamicSelfBuffId("Strength Self", AcSkillType.CreatureEnchantment)), "Incantation of Strength Self",
"cold, no player id: every spell in the data counts as known");
sm.MarkSpellUnresolvable(Id("Incantation of Strength Self"));
Check.Eq(NameOf(sm.GetDynamicSelfBuffId("Strength Self", AcSkillType.CreatureEnchantment)), "Might of the Lugians",
"a spell that got no chat back is skipped while cold");
Check.False(sm.TryResolveKnownSpellId("Strength Self VI", out _), "combat never guesses while cold");
FakeHost.Reset();
Know("Incantation of Strength Self", "Strength Self VI");
FakeHost.Skills[(Player, STypeCreature)] = (500, 2);
host = FakeHost.Create();
var warm = NewSpells(host, new LegacyUiSettings(), Skills(host));
warm.MarkSpellUnresolvable(Id("Incantation of Strength Self"));
Check.Eq(NameOf(warm.GetDynamicSelfBuffId("Strength Self", AcSkillType.CreatureEnchantment)), "Incantation of Strength Self",
"warm: the spellbook overrides the blacklist");
Check.True(warm.IsKnownSnapshotWarm, "warm");
Check.Eq(warm.UnresolvableSpellIds.Count, 0, "the known spell is purged from the blacklist");
Check.True(warm.TryResolveKnownSpellId("Strength Self VI", out int vi) && vi == Id("Strength Self VI"), "combat resolves a known spell");
Check.False(warm.TryResolveKnownSpellId("Strength Self V", out _), "and refuses an unknown one");
}
/// <summary>Resolves every buff in the list at tiers 1-8, with the whole spell data known.</summary>
private static List<(string Buff, int Tier, int Id)> ResolveEverything(out BuffManager bm)
{
FakeHost.Reset();
KnowAll();
var host = FakeHost.Create();
var byTier = new SpellManager[9];
for (int t = 1; t <= 8; t++)
{
var s = new LegacyUiSettings();
ForceBuffTier(s, t);
byTier[t] = NewSpells(host, s);
}
// The whole list, whatever this character could use (the unusable-buff filter is its own tests).
bm = new BuffManager(host, new LegacyUiSettings { SkipUnusableBuffs = false }, byTier[8], new PlayerVitalsCache());
var result = new List<(string, int, int)>();
foreach (string buff in bm.BuildDynamicBuffList())
{
var school = BuffManager.SkillForBuff(buff);
for (int t = 1; t <= 8; t++)
result.Add((buff, t, byTier[t].GetDynamicSelfBuffId(buff, school)));
}
return result;
}
private static void EveryBuffEveryTier()
{
var all = ResolveEverything(out var bm);
Check.True(bm.BuildDynamicBuffList().Count >= 60, $"the full buff list ({bm.BuildDynamicBuffList().Count} buffs)");
foreach (var (buff, tier, id) in all)
{
var row = SpellTable.ById(id);
Check.True(row != null && DataTier(row) == tier, $"{buff} at tier {tier}: got {NameOf(id)}");
}
}
private static void NameGapsResolve()
{
var all = ResolveEverything(out _);
foreach (var (buff, tier, id) in all)
{
if (!NameGaps.Contains((buff, tier))) continue;
var row = SpellTable.ById(id);
Check.True(row != null && DataTier(row) == tier, $"{buff} at tier {tier}: got {NameOf(id)}");
}
}
private static void LoreAndIncantations()
{
var all = ResolveEverything(out _);
string At(string buff, int tier) => NameOf(all.First(x => x.Buff == buff && x.Tier == tier).Id);
Check.Eq(At("Strength Self", 7), "Might of the Lugians", "Strength tier 7");
Check.Eq(At("Endurance Self", 7), "Perseverance", "Endurance tier 7 (the misspelled 'Preservance' is tried first and isn't in the data)");
Check.Eq(At("Invulnerability Self", 7), "Aura of Defense", "Invulnerability tier 7");
Check.Eq(At("Armor Self", 7), "Executor's Blessing", "Armor tier 7");
Check.Eq(At("Blood Drinker Self", 6), "Aura of Blood Drinker Self VI", "weapon aura tier 6");
Check.Eq(At("Blood Drinker Self", 7), "Aura of Infected Caress", "weapon aura tier 7");
Check.Eq(At("Blood Drinker Self", 8), "Aura of Incantation of Blood Drinker Self", "weapon aura tier 8");
Check.Eq(At("Spirit Drinker Self", 7), "Aura of Infected Spirit Caress", "Spirit Drinker tier 7");
Check.Eq(At("Impenetrability", 6), "Impenetrability VI", "armor spell tier 6 (no 'Self')");
Check.Eq(At("Impenetrability", 7), "Brogard's Defiance", "armor spell tier 7");
Check.Eq(At("Impenetrability", 8), "Incantation of Impenetrability", "armor spell tier 8");
Check.Eq(At("Blade Bane", 7), "Swordsman's Bane", "Blade Bane tier 7");
Check.Eq(At("Heavy Weapon Mastery", 7), "Heavy Weapon Mastery Self VII", "masteries with a real VII use it");
Check.Eq(At("Arcanum Salvaging", 7), "Arcanum Salvaging VII", "'X VII' without Self");
}
private static int Level(string name) => BuffManager.GetSpellLevel(new SpellInfo(1, name));
private static void SpellLevels()
{
Check.Eq(Level("Strength Self I"), 1, "I");
Check.Eq(Level("Strength Self II"), 2, "II");
Check.Eq(Level("Strength Self III"), 3, "III");
Check.Eq(Level("Strength Self IV"), 4, "IV");
Check.Eq(Level("Strength Self V"), 5, "V");
Check.Eq(Level("Strength Self VI"), 6, "VI");
Check.Eq(Level("Heavy Weapon Mastery Self VII"), 7, "VII");
Check.Eq(Level("Strength Self VIII"), 8, "VIII (not read as VII)");
Check.Eq(Level("Incantation of Strength Self"), 8, "Incantation");
Check.Eq(Level("Incantation of Impenetrability"), 8, "Incantation, item spell");
foreach (string lore in new[] { "Might of the Lugians", "Brogard's Defiance", "Tusker's Bane", "Aura of Infected Caress",
"Adja's Blessing", "Robustify", "Unflinching Persistence", "Aura of Cragstone's Will" })
Check.Eq(Level(lore), 7, lore);
Check.Eq(Level("Frobnicate"), 1, "an unknown name reads as 1");
}
private static void DurationsMatchData()
{
var all = ResolveEverything(out var bm);
foreach (var (buff, tier, id) in all)
{
var row = SpellTable.ById(id);
if (row == null) continue;
double assumed = bm.GetCustomSpellDuration(BuffManager.GetSpellLevel(new SpellInfo(id, row.Name)));
Check.Near(assumed, row.Duration, 0.5, $"{row.Name}: timer length");
}
}
private static void TierEightAuras()
{
foreach (string aura in Auras)
{
string name = "Aura of Incantation of " + aura;
Check.Eq(Level(name), 8, $"{name} is level 8");
}
// A chat-confirmed tier-8 aura, with the rebuff threshold at 62 minutes: a 90-minute
// timer leaves it on; a 60-minute one asks for a recast at once.
FakeHost.Reset();
Know("Aura of Incantation of Blood Drinker Self");
FakeHost.Skills[(Player, STypeItem)] = (500, 2);
var rig = new BuffRig();
rig.S.RebuffSecondsRemaining = 3720;
rig.B.OnChatWindowText("You cast Aura of Incantation of Blood Drinker Self on Rune Sword, refreshing Aura of Infected Caress", 7);
Check.False(rig.B.NeedsAnyBuff(), "just cast: 90 minutes left, not under 62");
}
private static void ArchmageEndurance()
{
foreach (var (augs, factor) in new[] { (0, 1.0), (1, 1.2), (2, 1.4), (5, 2.0), (7, 2.0), (-3, 1.0) })
{
FakeHost.Reset();
FakeHost.Ints[(Player, 238u)] = augs; // AugmentationIncreasedSpellDuration
var host = FakeHost.Create(playerId: Player);
var s = new LegacyUiSettings();
var bm = new BuffManager(host, s, NewSpells(host, s), new PlayerVitalsCache());
Check.Near(bm.GetCustomSpellDuration(6), 2700 * factor, 1e-6, $"{augs} rank(s): tier 6 lasts {2700 * factor} s");
Check.Near(bm.GetCustomSpellDuration(8), 5400 * factor, 1e-6, $"{augs} rank(s): tier 8 lasts {5400 * factor} s");
}
FakeHost.Reset();
var noId = FakeHost.Create();
var s0 = new LegacyUiSettings();
var unread = new BuffManager(noId, s0, NewSpells(noId, s0), new PlayerVitalsCache());
Check.Near(unread.GetCustomSpellDuration(1), 1800, 1e-6, "ranks unreadable: no bonus");
Check.Near(unread.GetCustomSpellDuration(7), 3600, 1e-6, "tier 7: an hour");
}
private static void SkillForBuff()
{
var creature = new[] { "Strength Self", "Focus Self", "Invulnerability Self", "Impregnability Self", "Magic Resistance Self",
"Heavy Weapon Mastery", "Missile Weapon Mastery", "Life Magic Mastery", "War Magic Mastery",
"Sprint", "Fealty", "Arcanum Salvaging", "Item Tinkering Expertise" };
var life = new[] { "Regeneration Self", "Rejuvenation Self", "Mana Renewal Self", "Armor Self", "Acid Protection Self",
"Blade Protection Self", "Bludgeoning Protection Self", "Heal Self", "Revitalize Self" };
var item = new[] { "Blood Drinker Self", "Hermetic Link Self", "Heart Seeker Self", "Spirit Drinker Self",
"Swift Killer Self", "Defender Self", "Impenetrability", "Acid Bane", "Bludgeoning Bane", "Piercing Bane" };
foreach (string b in creature) Check.Eq(BuffManager.SkillForBuff(b), AcSkillType.CreatureEnchantment, b);
foreach (string b in life) Check.Eq(BuffManager.SkillForBuff(b), AcSkillType.LifeMagic, b);
foreach (string b in item) Check.Eq(BuffManager.SkillForBuff(b), AcSkillType.ItemEnchantment, b);
}
private static void ArmorTinkeringSchool()
{
foreach (string n in new[] { "Armor Tinkering Expertise", "Item Tinkering Expertise", "Weapon Tinkering Expertise",
"Magic Item Tinkering Expertise", "Prodigal Armor Expertise", "Incantation of Armor Tinkering Expertise" })
Check.Eq(BuffManager.SkillForBuff(n), AcSkillType.CreatureEnchantment, n);
// The "Armor" keyword still files real Life Magic armor buffs under Life.
foreach (string n in new[] { "Armor", "Incantation of Armor", "Prodigal Armor", "Blade Protection", "Regeneration" })
Check.Eq(BuffManager.SkillForBuff(n), AcSkillType.LifeMagic, n);
}
private static void ItemEnchantments()
{
foreach (string n in new[] { "Impenetrability VI", "Brogard's Defiance", "Incantation of Acid Bane", "Tusker's Bane",
"Aura of Blood Drinker Self VI", "Aura of Infected Caress", "Aura of Cragstone's Will",
"Aura of Incantation of Defender Self", "Swordman's Bane" })
Check.True(BuffManager.IsItemEnchantment(n), $"{n}: on an item");
foreach (string n in new[] { "Strength Self VI", "Impregnability Self VI", "Aura of Defense", "Aura of Deflection",
"Armor Self VI", "Blade Protection Self VI", "Heal Self VI", "Might of the Lugians" })
Check.False(BuffManager.IsItemEnchantment(n), $"{n}: on the player");
}
// ── Rebuff timing ─────────────────────────────────────────────────────────
private static BuffRig StrengthRig(int buffed, params string[] known)
{
FakeHost.Reset();
Know(known);
FakeHost.Skills[(Player, STypeCreature)] = (buffed, 2);
return new BuffRig();
}
private static void RebuffThreshold()
{
var rig = StrengthRig(300, "Strength Self VI"); // 300 buffed: tier 6
rig.Live();
Check.True(rig.B.NeedsAnyBuff(), "not on: needs a buff");
rig.Live(("Strength Self VI", 1000));
Check.False(rig.B.NeedsAnyBuff(), "1000 s left, threshold 300: fine");
rig.Live(("Strength Self VI", 310));
Check.False(rig.B.NeedsAnyBuff(), "310 s left: fine");
rig.Live(("Strength Self VI", 290));
Check.True(rig.B.NeedsAnyBuff(), "290 s left: recast");
rig.S.RebuffSecondsRemaining = 1200;
rig.Live(("Strength Self VI", 1000));
Check.True(rig.B.NeedsAnyBuff(), "threshold raised to 1200: 1000 s left is under it");
rig.S.EnableBuffing = false;
Check.False(rig.B.NeedsAnyBuff(), "buffing off: never");
rig.S.EnableBuffing = true;
rig.S.RebuffSecondsRemaining = 300;
rig.B.ForceFullRebuff();
Check.True(rig.B.NeedsAnyBuff(), "Force Rebuff: everything is due");
}
// CastBuffsOverItemBuffs off: the behaviour before 2026-10-05 (an item-granted permanent
// entry at the tier we'd cast satisfies the family). The setting's own cases are below.
private static void RebuffPermanentAndDuplicates()
{
var rig = StrengthRig(300, "Strength Self VI");
rig.S.CastBuffsOverItemBuffs = false;
rig.Live(("Strength Self VI", double.MaxValue));
Check.False(rig.B.NeedsAnyBuff(), "a permanent enchantment counts as on");
rig.Live(("Strength Self VI", -5));
Check.True(rig.B.NeedsAnyBuff(), "an expired entry is dropped: recast");
var t7 = StrengthRig(340, "Strength Self VI", "Might of the Lugians"); // 340 buffed: tier 7
t7.S.CastBuffsOverItemBuffs = false;
t7.Live(("Strength Self VI", double.MaxValue), ("Might of the Lugians", 3000));
Check.False(t7.B.NeedsAnyBuff(), "a permanent VI and a timed VII: the VII owns the family");
t7.Live(("Might of the Lugians", 3000), ("Strength Self VI", double.MaxValue));
Check.False(t7.B.NeedsAnyBuff(), "same in the other order");
var item = StrengthRig(340, "Strength Self VI", "Might of the Lugians");
item.S.CastBuffsOverItemBuffs = false;
item.Live(("Strength Self VI", double.MaxValue));
Check.True(item.B.NeedsAnyBuff(), "only a permanent (item-granted) VI while VII is castable: cast the VII");
}
private static void RebuffTierUpgrade()
{
var rig = StrengthRig(300, "Strength Self V", "Strength Self VI");
rig.Live(("Strength Self V", 3000));
// Pinned: the landed tier caps the target, so a V from before a skill gain is kept
// until it runs low (this is the guard against Incantations that land a tier low).
Check.False(rig.B.NeedsAnyBuff(), "only V has ever landed: V with 3000 s left is kept (pinned)");
rig.Live(("Strength Self VI", 3000));
rig.Live(("Strength Self V", 3000));
Check.True(rig.B.NeedsAnyBuff(), "VI has landed before: a V on now is upgraded");
var inc = StrengthRig(500, "Incantation of Strength Self");
inc.Live(("Strength Self VI", 3000));
Check.False(inc.B.NeedsAnyBuff(), "an Incantation that lands as VI isn't recast for ever");
}
// A buff bot's level 8 that runs out while our own lower tier is already under it
// (Drakkon, 2026-10-05): the 8 owns the family timer, so the family read "due"
// right after our VI landed and every pass recast it until the batch audit parked it.
private static void RebuffUnderExpiringHigherTier()
{
var rig = StrengthRig(300, "Strength Self VI"); // 300 buffed: tier 6
rig.Live(("Incantation of Strength Self", 3000));
Check.False(rig.B.NeedsAnyBuff(), "a bot's 8 with 3000 s left: fine");
rig.Live(("Incantation of Strength Self", 200));
Check.True(rig.B.NeedsAnyBuff(), "the 8 is running out and nothing is under it: cast our VI");
rig.Live(("Incantation of Strength Self", 200), ("Strength Self VI", 1800));
Check.False(rig.B.NeedsAnyBuff(), "our VI is under the expiring 8: covered, no recast");
rig.Live(("Strength Self VI", 1800), ("Incantation of Strength Self", 200));
Check.False(rig.B.NeedsAnyBuff(), "same in the other order");
rig.Live(("Incantation of Strength Self", 200), ("Strength Self VI", 250));
Check.True(rig.B.NeedsAnyBuff(), "both under the threshold: recast");
var low = StrengthRig(300, "Strength Self V", "Strength Self VI");
low.Live(("Incantation of Strength Self", 200), ("Strength Self V", 1800));
Check.True(low.B.NeedsAnyBuff(), "only a V under it while VI is castable: still cast the VI");
low.S.CastBuffsOverItemBuffs = false;
low.Live(("Incantation of Strength Self", 200), ("Strength Self VI", double.MaxValue));
Check.False(low.B.NeedsAnyBuff(), "setting off: a permanent VI under it covers it");
low.S.CastBuffsOverItemBuffs = true;
low.Live(("Incantation of Strength Self", 200), ("Strength Self VI", double.MaxValue));
Check.True(low.B.NeedsAnyBuff(), "setting on: a permanent (item) VI under it doesn't cover it: cast ours");
}
// "Cast buffs even when an item already gives that buff" (CastBuffsOverItemBuffs, default
// on, 2026-10-05). On servers with buff augments a player's own cast is stronger or longer
// than the item's, so a permanent item-granted entry must not satisfy the family; the timer
// follows our own timed entry, and once that lands it reads on until it runs low.
private static void RebuffOverItemBuffs()
{
var rig = StrengthRig(300, "Strength Self VI"); // tier 6
Check.True(rig.S.CastBuffsOverItemBuffs, "on by default");
rig.Live(("Strength Self VI", double.MaxValue));
Check.True(rig.B.NeedsAnyBuff(), "only an item's permanent VI: cast our own");
rig.Live(("Strength Self VI", double.MaxValue), ("Strength Self VI", 3000));
Check.False(rig.B.NeedsAnyBuff(), "our VI landed (3000 s) over the item's VI: on, no recast loop");
rig.Live(("Strength Self VI", 3000), ("Strength Self VI", double.MaxValue));
Check.False(rig.B.NeedsAnyBuff(), "same in the other order");
rig.Live(("Strength Self VI", double.MaxValue), ("Strength Self VI", 200));
Check.True(rig.B.NeedsAnyBuff(), "ours running low (200 s) with the item's VI still on: recast");
rig.Live(("Strength Self VI", 200), ("Strength Self VI", double.MaxValue));
Check.True(rig.B.NeedsAnyBuff(), "same in the other order");
rig.Live(("Strength Self VI", double.MaxValue), ("Strength Self VI", 2700));
Check.False(rig.B.NeedsAnyBuff(), "recast landed: on again");
// A higher item tier than we can cast: ours is still cast and tracked.
var high = StrengthRig(300, "Strength Self VI");
high.Live(("Might of the Lugians", double.MaxValue));
Check.True(high.B.NeedsAnyBuff(), "an item's permanent VII, we cast VI: cast it");
high.Live(("Might of the Lugians", double.MaxValue), ("Strength Self VI", 3000));
Check.False(high.B.NeedsAnyBuff(), "our VI under the item's VII: on until it runs low");
high.Live(("Might of the Lugians", double.MaxValue), ("Strength Self VI", 100));
Check.True(high.B.NeedsAnyBuff(), "our VI running low: recast");
// The tier-upgrade rule still works on our timed entries.
var up = StrengthRig(340, "Strength Self VI", "Might of the Lugians"); // tier 7
up.Live(("Might of the Lugians", 3000));
up.Live(("Strength Self VI", double.MaxValue), ("Strength Self V", 3000));
Check.True(up.B.NeedsAnyBuff(), "VII has landed before: our V is upgraded, the item's VI doesn't stop it");
up.Live(("Strength Self VI", double.MaxValue), ("Might of the Lugians", 3000));
Check.False(up.B.NeedsAnyBuff(), "our VII on: fine");
// Switched on between live reads: a permanent that owns the timer no longer counts.
var toggled = StrengthRig(300, "Strength Self VI");
toggled.S.CastBuffsOverItemBuffs = false;
toggled.Live(("Strength Self VI", double.MaxValue));
Check.False(toggled.B.NeedsAnyBuff(), "off: the item's VI counts");
toggled.S.CastBuffsOverItemBuffs = true;
Check.True(toggled.B.NeedsAnyBuff(), "switched on before the next read: cast ours");
toggled.S.CastBuffsOverItemBuffs = false;
toggled.Live(("Strength Self VI", double.MaxValue));
Check.False(toggled.B.NeedsAnyBuff(), "switched back off: the item's VI counts again");
}
private static void OverItemBuffsSetting()
{
Check.True(new LegacyUiSettings().CastBuffsOverItemBuffs, "a new profile: on");
var old = JsonSerializer.Deserialize("{\"EnableBuffing\":true,\"RebuffSecondsRemaining\":300}", RynthAiJsonContext.Default.LegacyUiSettings);
Check.True(old != null && old.CastBuffsOverItemBuffs, "a profile saved before the setting: on");
var off = JsonSerializer.Deserialize(
JsonSerializer.Serialize(new LegacyUiSettings { CastBuffsOverItemBuffs = false }, RynthAiJsonContext.Default.LegacyUiSettings),
RynthAiJsonContext.Default.LegacyUiSettings);
Check.True(off != null && !off.CastBuffsOverItemBuffs, "off is saved and loaded");
// Profile switch/load copies it (CopySettings is the hand-written field list).
var copy = typeof(LegacyDashboardRenderer).GetMethod("CopySettings", BindingFlags.NonPublic | BindingFlags.Static)!;
var dst = new LegacyUiSettings();
copy.Invoke(null, new object[] { new LegacyUiSettings { CastBuffsOverItemBuffs = false }, dst });
Check.False(dst.CastBuffsOverItemBuffs, "a profile with it off loads off");
copy.Invoke(null, new object[] { old!, dst });
Check.True(dst.CastBuffsOverItemBuffs, "an old profile loads on");
// Engine Settings face: the payload carries it; an engine without the field leaves it alone.
var payload = JsonSerializer.Deserialize("{\"PatrolOnLogin\":false}", RynthAiJsonContext.Default.SettingsBridgePayload);
Check.True(payload != null && payload.CastBuffsOverItemBuffs == null, "an older engine's payload has no value");
var s = new LegacyUiSettings { CastBuffsOverItemBuffs = false };
var dash = (LegacyDashboardRenderer)RuntimeHelpers.GetUninitializedObject(typeof(LegacyDashboardRenderer));
typeof(LegacyDashboardRenderer).GetField("_settings", BindingFlags.NonPublic | BindingFlags.Instance)!.SetValue(dash, s);
typeof(LegacyDashboardRenderer).GetField("_advancedSettingsUi", BindingFlags.NonPublic | BindingFlags.Instance)!
.SetValue(dash, new LegacyAdvancedSettingsUi(s)); // BuildSettingsJson reads its crafting state
var sent = JsonSerializer.Deserialize(dash.BuildSettingsJson(), RynthAiJsonContext.Default.SettingsBridgePayload);
Check.True(sent != null && sent.CastBuffsOverItemBuffs == false, "sent to the engine face");
dash.ApplySettingsJson("{\"PatrolOnLogin\":false}");
Check.False(s.CastBuffsOverItemBuffs, "a save without the field leaves it off");
dash.ApplySettingsJson("{\"CastBuffsOverItemBuffs\":true}");
Check.True(s.CastBuffsOverItemBuffs, "a save with it on turns it on");
}
// ── Prot and bane toggles (Buffing settings, DisabledBuffs) ───────────────
private static readonly MethodInfo CastSelfBuffs =
typeof(BuffManager).GetMethod("CheckAndCastSelfBuffs", BindingFlags.NonPublic | BindingFlags.Instance)!;
/// <summary>Tier 6 Life and Item Magic, in Magic stance, knowing two prots and two banes.</summary>
private static BuffRig ProtBaneRig()
{
FakeHost.Reset();
Know("Fire Protection Self VI", "Cold Protection Self VI", "Flame Bane VI", "Frost Bane VI");
FakeHost.Skills[(Player, STypeLife)] = (300, 2);
FakeHost.Skills[(Player, STypeItem)] = (300, 2);
FakeHost.WeaponCalls = true; // GetCurrentCombatMode answers CombatModeValue
FakeHost.CombatModeValue = CombatMode.Magic;
return new BuffRig();
}
/// <summary>Runs the buff cast loop until it casts nothing, confirming each cast by chat as the
/// server would. Returns the spells cast in order; <paramref name="done"/> = the loop's last
/// answer was "nothing to do" (what ends a batch in the heartbeat).</summary>
private static List<string> CastAll(BuffRig rig, out bool done)
{
var cast = new List<string>();
done = false;
for (int i = 0; i < 20; i++)
{
int before = FakeHost.Casts.Count;
bool acted = (bool)CastSelfBuffs.Invoke(rig.B, null)!;
if (FakeHost.Casts.Count == before) { done = !acted; break; }
string name = NameOf(FakeHost.Casts.Last().SpellId);
cast.Add(name);
rig.B.OnChatWindowText($"You cast {name} on {(BuffManager.IsItemEnchantment(name) ? "Rune Robe" : "yourself")}", 7);
}
return cast;
}
private static void TogglesDefaultAllOn()
{
var rig = ProtBaneRig();
Check.Eq(rig.S.DisabledBuffs.Count, 0, "a new profile unticks nothing");
foreach (var (buff, _) in BuffManager.ToggleProtections.Concat(BuffManager.ToggleBanes))
Check.True(rig.B.BuildDynamicBuffList().Contains(buff), $"every toggle names a buff in the list: {buff}");
rig.Live(("Strength Self I", 3000)); // a registry read, with none of ours on
Check.True(rig.B.NeedsAnyBuff(), "nothing on: needs buffs");
var cast = CastAll(rig, out bool done);
Check.Eq(string.Join(", ", cast), "Fire Protection Self VI, Cold Protection Self VI, Flame Bane VI, Frost Bane VI", $"all four are cast (last skip: {rig.B.LastBuffSkipReason})");
Check.True(done, "then the loop has nothing left");
}
private static void UntickedSkipped()
{
var rig = ProtBaneRig();
rig.S.SetBuffEnabled("Fire Protection Self", false);
rig.S.SetBuffEnabled("flame bane", false); // names match case-insensitively
Check.False(rig.B.BuildDynamicBuffList().Contains("Fire Protection Self"), "an unticked prot leaves the list");
Check.False(rig.B.BuildDynamicBuffList().Contains("Flame Bane"), "an unticked bane leaves the list");
rig.Live(("Strength Self I", 3000));
var cast = CastAll(rig, out bool done);
Check.Eq(string.Join(", ", cast), "Cold Protection Self VI, Frost Bane VI", "only the ticked ones are cast");
Check.True(done, "the loop ends with the unticked ones still off: the batch can finish");
// A fresh look, the ticked ones on and the unticked ones never cast: nothing is due.
var idle = ProtBaneRig();
idle.S.SetBuffEnabled("Fire Protection Self", false);
idle.S.SetBuffEnabled("Flame Bane", false);
idle.Live(("Cold Protection Self VI", 3000));
idle.B.OnChatWindowText("You cast Frost Bane VI on Rune Robe", 7);
Check.False(idle.B.NeedsAnyBuff(), "unticked and off doesn't hold Buffing");
idle.S.SetBuffEnabled("Fire Protection Self", true);
Check.True(idle.B.NeedsAnyBuff(), "ticked again: Fire Protection is due");
Check.False(idle.S.IsBuffDisabled("Fire Protection Self"), "and leaves the list");
// Force Rebuff recasts everything, still not the unticked ones.
var fr = ProtBaneRig();
fr.S.SetBuffEnabled("Cold Protection Self", false);
fr.S.SetBuffEnabled("Frost Bane", false);
fr.Live(("Strength Self I", 3000));
fr.B.ForceFullRebuff();
cast = CastAll(fr, out done);
Check.Eq(string.Join(", ", cast), "Fire Protection Self VI, Flame Bane VI", "Force Rebuff skips the unticked ones too");
Check.True(done, "and finishes");
Check.False(FakeHost.Casts.Any(c => NameOf(c.SpellId) is "Cold Protection Self VI" or "Frost Bane VI"), "never cast");
}
private static void TogglesSetting()
{
var old = JsonSerializer.Deserialize("{\"EnableBuffing\":true,\"RebuffSecondsRemaining\":300}", RynthAiJsonContext.Default.LegacyUiSettings);
Check.True(old != null && old.DisabledBuffs.Count == 0, "a profile saved before the setting: all on");
var nul = JsonSerializer.Deserialize("{\"DisabledBuffs\":null}", RynthAiJsonContext.Default.LegacyUiSettings);
Check.True(nul != null && nul.DisabledBuffs.Count == 0, "a null list: all on");
var some = new LegacyUiSettings();
some.SetBuffEnabled("Acid Bane", false);
some.SetBuffEnabled("Blade Protection Self", false);
var back = JsonSerializer.Deserialize(JsonSerializer.Serialize(some, RynthAiJsonContext.Default.LegacyUiSettings),
RynthAiJsonContext.Default.LegacyUiSettings);
Check.True(back != null && back.IsBuffDisabled("Acid Bane") && back.IsBuffDisabled("Blade Protection Self") && back.DisabledBuffs.Count == 2,
"unticked ones are saved and loaded");
// Profile switch/load copies it (CopySettings is the hand-written field list).
var copy = typeof(LegacyDashboardRenderer).GetMethod("CopySettings", BindingFlags.NonPublic | BindingFlags.Static)!;
var dst = new LegacyUiSettings();
copy.Invoke(null, new object[] { some, dst });
Check.True(dst.IsBuffDisabled("Acid Bane") && dst.DisabledBuffs.Count == 2, "a profile with unticked ones loads them");
copy.Invoke(null, new object[] { old!, dst });
Check.Eq(dst.DisabledBuffs.Count, 0, "an old profile loads all on");
// Engine Settings face: the payload carries the list; an engine without the field leaves it alone.
var payload = JsonSerializer.Deserialize("{\"PatrolOnLogin\":false}", RynthAiJsonContext.Default.SettingsBridgePayload);
Check.True(payload != null && payload.DisabledBuffs == null, "an older engine's payload has no list");
var s = new LegacyUiSettings();
s.SetBuffEnabled("Frost Bane", false);
var dash = (LegacyDashboardRenderer)RuntimeHelpers.GetUninitializedObject(typeof(LegacyDashboardRenderer));
typeof(LegacyDashboardRenderer).GetField("_settings", BindingFlags.NonPublic | BindingFlags.Instance)!.SetValue(dash, s);
typeof(LegacyDashboardRenderer).GetField("_advancedSettingsUi", BindingFlags.NonPublic | BindingFlags.Instance)!
.SetValue(dash, new LegacyAdvancedSettingsUi(s));
var sent = JsonSerializer.Deserialize(dash.BuildSettingsJson(), RynthAiJsonContext.Default.SettingsBridgePayload);
Check.True(sent?.DisabledBuffs != null && sent.DisabledBuffs.SequenceEqual(new[] { "Frost Bane" }), "sent to the engine face");
dash.ApplySettingsJson("{\"PatrolOnLogin\":false}");
Check.True(s.IsBuffDisabled("Frost Bane"), "a save without the list leaves it alone");
dash.ApplySettingsJson("{\"DisabledBuffs\":[]}");
Check.Eq(s.DisabledBuffs.Count, 0, "a save with an empty list ticks everything");
}
private static BuffRig ArmorRig()
{
FakeHost.Reset();
Know("Impenetrability VI");
FakeHost.Skills[(Player, STypeItem)] = (300, 2); // tier 6
return new BuffRig();
}
private static void ItemSpellFromChat()
{
var rig = ArmorRig();
Check.True(rig.B.NeedsAnyBuff(), "no armor spell yet");
rig.B.OnChatWindowText("You cast Impenetrability VI on Rune Robe, refreshing Impenetrability V", 7);
Check.False(rig.B.NeedsAnyBuff(), "confirmed by chat: on for 45 minutes");
var stripped = ArmorRig();
stripped.B.OnChatWindowText("ou cast Impenetrability VI on Rune Robe", 7); // AC's glyph layer eats the Y
Check.False(stripped.B.NeedsAnyBuff(), "the 'ou cast' form counts too");
var fizzle = ArmorRig();
fizzle.B.OnChatWindowText("Your spell fizzled.", 7);
Check.True(fizzle.B.NeedsAnyBuff(), "a fizzle records nothing");
var dead = ArmorRig();
dead.B.OnChatWindowText("You cast Impenetrability VI on Rune Robe", 7);
dead.Live(("Strength Self I", 600)); // some player enchantment ...
dead.Live(); // ... then the registry drops to zero: a death
Check.True(dead.B.NeedsAnyBuff(), "after a death wipes the registry, item spells are recast too");
}
private static void SpeechIsNotACast()
{
var rig = ArmorRig();
rig.B.OnChatWindowText("Vessa says, \"you cast Impenetrability VI on your robe?\"", 2);
Check.True(rig.B.NeedsAnyBuff(), "someone else's speech is not our cast");
}
private static void MultiIdMastery()
{
FakeHost.Reset();
foreach (int id in new[] { 491, 492, 493, 494, 495, 496 }) FakeHost.Spellbook.Add((uint)id); // the second line
FakeHost.Skills[(Player, STypeCreature)] = (300, 2);
var host = FakeHost.Create();
var sm = NewSpells(host, new LegacyUiSettings(), Skills(host));
Check.Eq(SpellTable.ById(496)?.Name, "Missile Weapon Mastery Self VI", "496 is a Missile Weapon Mastery Self VI");
Check.Eq(sm.GetDynamicSelfBuffId("Missile Weapon Mastery", AcSkillType.CreatureEnchantment), 496, "resolves to the id the character knows");
}
// -- Priority order, Force Buff and the unusable-buff filter ----
private const uint SkillHeavy = 44, SkillMissile = 47, SkillMagicDefense = 15;
private const uint Sword = 0x80000401, Bow = 0x80000406, Wand = 0x80000408, Shield = 0x80000404;
/// <summary>Tier 6 Creature/Life/Item Magic, in Magic stance, knowing the head of the list, a
/// couple of ordinary buffs and all six weapon auras. <paramref name="listed"/> are the items on
/// the Items list (id, name, ItemType: 0x1 melee, 0x100 missile, 0x8000 caster, 0x2 shield).</summary>
private static BuffRig SuiteRig(params (uint Id, string Name, uint ItemType)[] listed)
{
FakeHost.Reset();
Know("Creature Enchantment Mastery Self VI", "Focus Self VI", "Willpower Self VI", "Life Magic Mastery Self VI",
"Strength Self VI", "Heavy Weapon Mastery Self VI", "Armor Self VI",
"Aura of Blood Drinker Self VI", "Aura of Hermetic Link Self VI", "Aura of Heart Seeker Self VI",
"Aura of Spirit Drinker Self VI", "Aura of Swift Killer Self VI", "Aura of Defender Self VI");
FakeHost.Skills[(Player, STypeCreature)] = (300, 2);
FakeHost.Skills[(Player, STypeLife)] = (300, 2);
FakeHost.Skills[(Player, STypeItem)] = (300, 2);
FakeHost.WeaponCalls = true;
FakeHost.CombatModeValue = CombatMode.Magic;
foreach (var (id, name, type) in listed)
{
FakeHost.Names[id] = name;
FakeHost.ItemTypes[id] = type;
FakeHost.Ints[(id, 9)] = type == 0x2 ? 0x00200000 : type == 0x8000 ? 0x01000000 : type == 0x100 ? 0x00400000 : 0x00100000;
}
var rig = new BuffRig();
rig.S.ItemRules = listed.Select(l => new ItemRule
{
Id = unchecked((int)l.Id), Name = l.Name, Element = "", ElementSource = "",
Action = l.ItemType == 0x2 ? WeaponList.ShieldAction : "Weapon",
}).ToList();
rig.B.SetWorldObjectCache(FakeHost.MakeCache(rig.Host, Player, listed.Select(l => l.Id))); // even with nothing listed: the empty-pack path runs
return rig;
}
private static readonly (uint, string, uint) MeleeItem = (Sword, "Long Sword", 0x1);
private static readonly (uint, string, uint) BowItem = (Bow, "Longbow", 0x100);
private static readonly (uint, string, uint) WandItem = (Wand, "Wand", 0x8000);
private static readonly (uint, string, uint) ShieldItem = (Shield, "Kite Shield", 0x2);
private static readonly string[] Head =
{ "Creature Enchantment Mastery", "Focus Self", "Willpower Self", "Life Magic Mastery" };
/// <summary>Every buff the rig knows, on with plenty of time left (item auras by chat).</summary>
private static void AllOn(BuffRig rig, params string[] except)
{
var live = new[] { "Creature Enchantment Mastery Self VI", "Focus Self VI", "Willpower Self VI", "Life Magic Mastery Self VI",
"Strength Self VI", "Heavy Weapon Mastery Self VI", "Armor Self VI" }
.Where(n => !except.Contains(n)).Select(n => (n, 3000.0)).ToArray();
rig.Live(live);
foreach (string aura in new[] { "Blood Drinker", "Hermetic Link", "Heart Seeker", "Spirit Drinker", "Swift Killer", "Defender" })
rig.B.OnChatWindowText($"You cast Aura of {aura} Self VI on Rune Sword", 7);
}
private static void PriorityOrder()
{
var rig = SuiteRig(MeleeItem, WandItem);
var list = rig.B.BuildDynamicBuffList();
Check.Eq(string.Join(", ", list.Take(4)), string.Join(", ", Head), "Creature Enchantment, Focus, Willpower, Life Magic first");
int strength = list.IndexOf("Strength Self"), heavy = list.IndexOf("Heavy Weapon Mastery"), armor = list.IndexOf("Armor Self");
Check.True(strength == 4, $"everything else follows, Strength next (at {strength})");
Check.True(strength < heavy && heavy < armor, "and keeps its old order (attributes, masteries, prots)");
Check.True(list.IndexOf("Blood Drinker Self") > armor, "weapon auras stay after the prots");
// The head is the head with every skill trained or not: an untrained Life Magic just isn't there.
var noLifeRig = SuiteRig(MeleeItem);
FakeHost.Skills[(Player, STypeLife)] = (10, 1);
var noLife = noLifeRig.B.BuildDynamicBuffList();
Check.Eq(string.Join(", ", noLife.Take(3)), string.Join(", ", Head.Take(3)), "head without Life Magic");
Check.False(noLife.Contains("Life Magic Mastery"), "untrained Life Magic: no Life Magic Mastery");
}
private static void PriorityCastOrderAndLoneExpiry()
{
// A full run on a character with nothing on: the head goes out first, in order.
var rig = SuiteRig(MeleeItem);
rig.Live(("Strength Self I", 3000)); // a registry read, with none of the suite on
rig.B.ForceFullRebuff();
var cast = CastAll(rig, out bool done);
Check.Eq(string.Join(", ", cast.Take(4)),
"Creature Enchantment Mastery Self VI, Focus Self VI, Willpower Self VI, Life Magic Mastery Self VI",
$"the four first (last skip: {rig.B.LastBuffSkipReason})");
Check.Eq(cast.Count, 11, $"then the rest: {string.Join(", ", cast)}");
Check.True(done, "and the run ends");
// The automatic batch recasts a lone expiring buff alone: timers still drive it.
var lone = SuiteRig(MeleeItem);
AllOn(lone, "Strength Self VI");
lone.Live(("Creature Enchantment Mastery Self VI", 3000), ("Focus Self VI", 3000), ("Willpower Self VI", 3000),
("Life Magic Mastery Self VI", 3000), ("Strength Self VI", 200), ("Heavy Weapon Mastery Self VI", 3000), ("Armor Self VI", 3000));
Check.True(lone.B.NeedsAnyBuff(), "Strength is under the rebuff line");
cast = CastAll(lone, out done);
Check.Eq(string.Join(", ", cast), "Strength Self VI", "only the expiring buff is recast");
// ... and when a priority buff is the one expiring, it is the one cast.
var head = SuiteRig(MeleeItem);
AllOn(head);
head.Live(("Creature Enchantment Mastery Self VI", 200), ("Focus Self VI", 3000), ("Willpower Self VI", 3000),
("Life Magic Mastery Self VI", 3000), ("Strength Self VI", 3000), ("Heavy Weapon Mastery Self VI", 3000), ("Armor Self VI", 3000));
cast = CastAll(head, out done);
Check.Eq(string.Join(", ", cast), "Creature Enchantment Mastery Self VI", "a lone expiring priority buff: just it");
}
private static void ForceBuffSuite()
{
var rig = SuiteRig(MeleeItem);
AllOn(rig);
Check.False(rig.B.NeedsAnyBuff(), "everything has long timers: nothing due");
var none = CastAll(rig, out bool done);
Check.Eq(none.Count, 0, $"without force nothing is cast ({string.Join(", ", none)})");
Check.True(done, "and the loop is idle");
rig.B.ForceFullRebuff();
Check.True(rig.B.NeedsAnyBuff(), "force: everything is due");
var cast = CastAll(rig, out done);
Check.Eq(string.Join(", ", cast),
"Creature Enchantment Mastery Self VI, Focus Self VI, Willpower Self VI, Life Magic Mastery Self VI, " +
"Strength Self VI, Heavy Weapon Mastery Self VI, Armor Self VI, " +
"Aura of Blood Drinker Self VI, Aura of Heart Seeker Self VI, Aura of Swift Killer Self VI, Aura of Defender Self VI",
"the whole filtered suite, in priority order; Spirit Drinker and Hermetic Link (no wand) are not in it");
Check.True(done, "and finishes");
// Cancel ends a run in progress.
var c = SuiteRig(MeleeItem);
AllOn(c);
c.B.ForceFullRebuff();
Check.True(c.B.GetStateSnapshot().IsForceRebuffing, "force run under way");
c.B.CancelBuffing();
Check.False(c.B.GetStateSnapshot().IsForceRebuffing, "cancelled: the run is over");
}
private static void ForceBuffEntryPoints()
{
// /ra buff force runs the command handler: one request, applied on the next heartbeat.
var rig = SuiteRig(MeleeItem);
AllOn(rig);
var plugin = new RynthAiPlugin();
typeof(RynthPluginBase).GetMethod("Attach", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)!
.Invoke(plugin, new object[] { FakeHost.LastApi });
typeof(RynthAiPlugin).GetField("_buffManager", BindingFlags.Instance | BindingFlags.NonPublic)!.SetValue(plugin, rig.B);
void Cmd(params string[] words) => typeof(RynthAiPlugin).GetMethod("HandleBuffCommand", BindingFlags.Instance | BindingFlags.NonPublic)!
.Invoke(plugin, new object[] { new[] { "/ra", "buff" }.Concat(words).ToArray() });
rig.B.MarkLiveBuffsReadyForTests();
Cmd("force");
Check.False(rig.B.GetStateSnapshot().IsForceRebuffing, "the command only queues the request");
rig.B.OnHeartbeat();
Check.True(rig.B.GetStateSnapshot().IsForceRebuffing, "the next heartbeat starts the force run");
Check.True(FakeHost.Chat.Any(c => c.Contains("Starting Force Rebuff")), "and says so");
Cmd("cancel");
rig.B.OnHeartbeat();
Check.False(rig.B.GetStateSnapshot().IsForceRebuffing, "/ra buff cancel ends it");
FakeHost.Chat.Clear();
Cmd("skipped");
Check.True(FakeHost.Chat.Any(c => c.Contains("Buff filter")), "/ra buff skipped answers");
FakeHost.Chat.Clear();
Cmd();
Check.True(FakeHost.Chat.Any(c => c.Contains("/ra buff force")), "no word: the help");
}
private static void SkillFilter()
{
var trained = SuiteRig(MeleeItem);
Check.True(trained.B.BuildDynamicBuffList().Contains("Heavy Weapon Mastery"), "Heavy Weapons trained: its mastery is cast");
FakeHost.Reset();
var rig = SuiteRig(MeleeItem);
FakeHost.Skills[(Player, SkillHeavy)] = (10, 1); // untrained
FakeHost.Skills[(Player, SkillMagicDefense)] = (5, 1);
var list = rig.B.BuildDynamicBuffList();
Check.False(list.Contains("Heavy Weapon Mastery"), "Heavy Weapons untrained: its mastery is skipped");
Check.True(list.Contains("Strength Self") && list.Contains("Armor Self"), "attribute and vital/prot buffs are untouched");
Check.True(list.Contains("Magic Resistance Self"), "magic defense is not filtered even when untrained");
Check.True(rig.B.FilterSkips.Any(s => s.Buff == "Heavy Weapon Mastery" && s.Reason == "skill untrained"), "and the skip is recorded");
rig.S.SkipUnusableBuffs = false;
Check.False(rig.B.BuildDynamicBuffList().Contains("Heavy Weapon Mastery"), "filter off: an untrained skill' mastery is still never cast");
Check.Eq(rig.B.FilterSkips.Count, 0, "filter off: nothing is reported either");
// The cast loop agrees: untrained and filtered, the mastery is never cast.
var run = SuiteRig(MeleeItem);
FakeHost.Skills[(Player, SkillHeavy)] = (10, 1);
run.Live(("Strength Self I", 3000));
run.B.ForceFullRebuff();
Check.False(CastAll(run, out _).Contains("Heavy Weapon Mastery Self VI"), "the cast loop skips it too");
var off = SuiteRig(MeleeItem);
off.S.SkipUnusableBuffs = false;
FakeHost.Skills[(Player, SkillHeavy)] = (10, 1);
off.Live(("Strength Self I", 3000));
off.B.ForceFullRebuff();
Check.False(CastAll(off, out _).Contains("Heavy Weapon Mastery Self VI"), "filter off: the cast loop still skips an untrained mastery");
}
private static string[] AuraList(BuffRig rig) =>
rig.B.BuildDynamicBuffList().Where(b => b is "Blood Drinker Self" or "Heart Seeker Self" or "Swift Killer Self"
or "Spirit Drinker Self" or "Hermetic Link Self" or "Defender Self").ToArray();
private static void WeaponFilter()
{
const string phys = "Blood Drinker Self, Heart Seeker Self, Swift Killer Self";
string Join(IEnumerable<string> a) => string.Join(", ", a);
var wand = SuiteRig(WandItem);
Check.Eq(Join(AuraList(wand)), "Hermetic Link Self, Spirit Drinker Self, Defender Self", "only a wand: Spirit Drinker, Hermetic Link, Defender");
var melee = SuiteRig(MeleeItem);
Check.Eq(Join(AuraList(melee)), "Blood Drinker Self, Heart Seeker Self, Swift Killer Self, Defender Self", "a melee weapon: Blood Drinker, Heart Seeker, Swift Killer, Defender");
Check.True(Join(AuraList(melee)).StartsWith(phys), "Blood Drinker is cast with a melee weapon");
var bow = SuiteRig(BowItem);
Check.Eq(Join(AuraList(bow)), "Blood Drinker Self, Heart Seeker Self, Swift Killer Self, Defender Self", "a missile weapon counts as physical");
var both = SuiteRig(MeleeItem, WandItem);
Check.Eq(AuraList(both).Length, 6, "melee and a wand: all six");
var none = SuiteRig();
Check.Eq(AuraList(none).Length, 0, "nothing registered: every weapon aura is skipped");
var shield = SuiteRig(ShieldItem);
Check.Eq(AuraList(shield).Length, 0, "a shield alone is no weapon");
none.S.SkipUnusableBuffs = false;
Check.Eq(AuraList(none).Length, 6, "filter off: all six again");
// A listed item the object cache can't place yet doesn't hide anything.
var unknown = SuiteRig();
unknown.S.ItemRules = new List<ItemRule> { new() { Id = 0x7FFFFFF1, Name = "Mystery", Action = "Weapon", Element = "", ElementSource = "" } };
Check.Eq(AuraList(unknown).Length, 6, "a listed weapon we can't classify yet: nothing filtered");
// The reasons, named.
var kit = new BuffManager.ListedWeaponKit(false, true, false);
Check.Eq(BuffManager.WeaponAuraSkipReason("Blood Drinker Self", kit), "no melee or missile weapon listed", "reason, physical aura");
Check.Eq(BuffManager.WeaponAuraSkipReason("Spirit Drinker Self", kit), "", "wand: Spirit Drinker stays");
Check.Eq(BuffManager.WeaponAuraSkipReason("Impenetrability", kit), "", "other buffs have no weapon rule");
}
private static void UnlistedWandCounts()
{
string Join(IEnumerable<string> a) => string.Join(", ", a);
// A wand in the pack, the Items list empty.
var rig = SuiteRig(WandItem);
rig.S.ItemRules = new List<ItemRule>();
rig.S.WieldUnlistedWandWhenNoneListed = true;
Check.Eq(Join(AuraList(rig)), "Hermetic Link Self, Spirit Drinker Self, Defender Self", "unlisted wand mode on, wand in the pack: caster auras cast");
rig.S.WieldUnlistedWandWhenNoneListed = false;
Check.Eq(AuraList(rig).Length, 0, "setting off: every weapon aura skipped");
// Physical auras still need a listed melee or missile weapon.
var mixed = SuiteRig(MeleeItem, WandItem);
mixed.S.ItemRules = mixed.S.ItemRules.Where(x => x.Id == unchecked((int)Sword)).ToList(); // wand in the pack, only the sword listed
mixed.S.WieldUnlistedWandWhenNoneListed = true;
Check.Eq(AuraList(mixed).Length, 6, "sword listed + unlisted pack wand: all six");
mixed.S.WieldUnlistedWandWhenNoneListed = false;
Check.Eq(Join(AuraList(mixed)), "Blood Drinker Self, Heart Seeker Self, Swift Killer Self, Defender Self", "setting off: the sword's auras only");
// On, but no wand anywhere: nothing to wield, so nothing counts.
var empty = SuiteRig();
empty.S.WieldUnlistedWandWhenNoneListed = true;
Check.Eq(AuraList(empty).Length, 0, "on, but no wand available: skipped");
}
private static void FilterVisibility()
{
var rig = SuiteRig(WandItem);
FakeHost.Skills[(Player, SkillHeavy)] = (10, 1);
rig.Live(("Strength Self I", 3000));
rig.B.ForceFullRebuff();
rig.B.ForceFullRebuff(); // a restart straight away
var lines = FakeHost.Logs.Where(l => l.Contains("[Buff] filter skips:")).ToList();
Check.Eq(lines.Count, 1, "one skip line for two runs inside the rate limit");
Check.True(lines[0].Contains("skill untrained: ") && lines[0].Contains("Heavy Weapon"), "it names the untrained mastery and why: " + lines[0]);
Check.True(lines[0].Contains("no melee or missile weapon listed: Blood Drinker"), "and the weapon aura and why: " + lines[0]);
Check.False(lines[0].Contains("Spirit Drinker"), "but not what stays");
Check.True(FakeHost.Chat.Any(c => c.Contains("Buff run skips")), "the same line goes to chat");
Check.True(rig.B.FilterSkipSummary().Contains("Swift Killer"), "the settings page reads the same list");
// The automatic batch logs it too, once.
var auto = SuiteRig(WandItem);
FakeHost.Skills[(Player, SkillHeavy)] = (10, 1);
AllOn(auto, "Strength Self VI");
auto.Live(("Creature Enchantment Mastery Self VI", 3000), ("Strength Self VI", 200));
CastAll(auto, out _);
Check.Eq(FakeHost.Logs.Count(l => l.Contains("[Buff] filter skips:")), 1, "an automatic run logs the skips once");
// Nothing to skip, nothing said.
var clean = SuiteRig(MeleeItem, WandItem);
clean.Live(("Strength Self I", 3000));
clean.B.ForceFullRebuff();
Check.Eq(FakeHost.Logs.Count(l => l.Contains("[Buff] filter skips:")), 0, "nothing skipped: no line");
}
private static void KitEdgeCases()
{
// A listed item the cache keeps unclassified (a Misc-typed thing): nothing is filtered.
const uint Odd = 0x80000411;
FakeHost.Reset();
var odd = SuiteRig();
FakeHost.Names[Odd] = "Mystery Thing";
FakeHost.ItemTypes[Odd] = 0;
odd.B.SetWorldObjectCache(FakeHost.MakeCache(odd.Host, Player, new[] { Odd }));
odd.S.ItemRules = new List<ItemRule> { new() { Id = unchecked((int)Odd), Name = "Mystery Thing", Action = "Weapon", Element = "", ElementSource = "" } };
var cls = odd.B.ListedWeapons();
Check.True(cls.Unknown, $"the odd item stays unclassified (kit {cls})");
Check.Eq(AuraList(odd).Length, 6, "an unclassifiable listed item: all six auras kept");
// No cache at all, nothing listed: can't tell, so nothing is filtered.
FakeHost.Reset();
Know("Aura of Blood Drinker Self VI");
var bare = new BuffRig();
Check.Eq(AuraList(bare).Length, 6, "no object cache: nothing filtered");
// Only ammo listed: not a weapon.
const uint Arrows = 0x80000407;
FakeHost.Reset();
var ammo = SuiteRig();
FakeHost.Names[Arrows] = "Arrow";
FakeHost.ItemTypes[Arrows] = 0x100;
FakeHost.Ints[(Arrows, 9)] = 0x00800000;
ammo.B.SetWorldObjectCache(FakeHost.MakeCache(ammo.Host, Player, new[] { Arrows }));
ammo.S.ItemRules = new List<ItemRule> { new() { Id = unchecked((int)Arrows), Name = "Arrow", Action = "Weapon", Element = "", ElementSource = "" } };
var k = ammo.B.ListedWeapons();
Check.False(k.Physical, "ammo is no physical weapon");
Check.Eq(AuraList(ammo).Length, 0, "ammo-only list (and nothing else): every weapon aura skipped");
// Wielded weapons count with an empty Items list.
var sword = SuiteRig(MeleeItem);
sword.S.ItemRules = new List<ItemRule>();
sword.S.WieldUnlistedWandWhenNoneListed = false;
Check.Eq(AuraList(sword).Length, 0, "control: sword in the pack, not wielded, nothing listed: skipped");
sword = SuiteRig(MeleeItem);
sword.S.ItemRules = new List<ItemRule>();
sword.S.WieldUnlistedWandWhenNoneListed = false;
FakeHost.Ints[(Sword, 10)] = 0x00100000; // wielded in the melee slot
Check.Eq(string.Join(", ", AuraList(sword)), "Blood Drinker Self, Heart Seeker Self, Swift Killer Self, Defender Self", "wielded sword, empty Items list: physical auras and Defender");
var wand = SuiteRig(WandItem);
wand.S.ItemRules = new List<ItemRule>();
wand.S.WieldUnlistedWandWhenNoneListed = false;
FakeHost.Ints[(Wand, 10)] = 0x01000000; // wielded in the held slot
Check.Eq(string.Join(", ", AuraList(wand)), "Hermetic Link Self, Spirit Drinker Self, Defender Self", "wielded wand, empty Items list: caster auras and Defender");
}
private static void SkipLineGroupedAndCapped()
{
var rig = SuiteRig(WandItem);
foreach (AcSkillType sk in Enum.GetValues<AcSkillType>())
{
uint st = CharacterSkills.AcSkillTypeToSTypeSkill(sk);
if (st == 0 || st == STypeCreature || st == STypeLife || st == STypeItem) continue;
FakeHost.Skills[(Player, st)] = (5, 1);
}
rig.Live(("Strength Self I", 3000));
rig.B.ForceFullRebuff();
var lines = FakeHost.Logs.Where(l => l.Contains("[Buff] filter skips:")).ToList();
Check.Eq(lines.Count, 1, "one line");
string line = lines[0].Substring(lines[0].IndexOf("skips:", StringComparison.Ordinal) + 7);
Check.True(line.Length <= 200, $"{line.Length} chars: {line}");
Check.True(line.Contains("more)"), "the long group is capped: " + line);
Check.True(line.Contains("no melee or missile weapon listed: Blood Drinker, Heart Seeker, Swift Killer"), "the weapon-aura skip is still named: " + line);
// The same content again inside 30 s: held back. A change prints at once.
rig.B.ForceFullRebuff();
Check.Eq(FakeHost.Logs.Count(l => l.Contains("[Buff] filter skips:")), 1, "same content: no second line");
rig.S.ItemRules = new List<ItemRule> { new() { Id = unchecked((int)Sword), Name = "Long Sword", Action = "Weapon", Element = "", ElementSource = "" } };
FakeHost.Ints[(Sword, 9)] = 0x00100000; FakeHost.Names[Sword] = "Long Sword"; FakeHost.ItemTypes[Sword] = 0x1;
rig.B.SetWorldObjectCache(FakeHost.MakeCache(rig.Host, Player, new[] { Sword, Wand }));
rig.B.ForceFullRebuff();
Check.Eq(FakeHost.Logs.Count(l => l.Contains("[Buff] filter skips:")), 2, "different content prints at once");
// The summary string is built once per change.
string a = rig.B.FilterSkipSummary(), b = rig.B.FilterSkipSummary();
Check.True(ReferenceEquals(a, b), "cached summary");
}
private static void KitCached()
{
var rig = SuiteRig(MeleeItem);
long now = 10_000;
rig.B.KitClockMs = () => now;
for (int i = 0; i < 100; i++) rig.B.BuildDynamicBuffList();
Check.Eq(rig.B.KitComputations, 1, "100 passes inside a second: one computation");
now += 1_500;
rig.B.BuildDynamicBuffList();
Check.Eq(rig.B.KitComputations, 2, "after the TTL: again");
rig.S.ItemRules = new List<ItemRule>(rig.S.ItemRules);
rig.B.BuildDynamicBuffList();
Check.Eq(rig.B.KitComputations, 3, "a new Items list: again, before the TTL");
rig.S.ItemRules.Add(new ItemRule { Id = 1, Name = "x", Action = "Weapon" });
rig.B.BuildDynamicBuffList();
Check.Eq(rig.B.KitComputations, 4, "an item added in place: again");
rig.S.WieldUnlistedWandWhenNoneListed = !rig.S.WieldUnlistedWandWhenNoneListed;
rig.B.BuildDynamicBuffList();
Check.Eq(rig.B.KitComputations, 5, "the unlisted-wand setting flipped: again");
rig.B.BuildDynamicBuffList();
Check.Eq(rig.B.KitComputations, 5, "and then cached");
}
private static void SkipUnusableSetting()
{
Check.True(new LegacyUiSettings().SkipUnusableBuffs, "a new profile: on");
var old = JsonSerializer.Deserialize("{\"EnableBuffing\":true,\"RebuffSecondsRemaining\":300}", RynthAiJsonContext.Default.LegacyUiSettings);
Check.True(old != null && old.SkipUnusableBuffs, "a profile saved before the setting: on");
var off = JsonSerializer.Deserialize(
JsonSerializer.Serialize(new LegacyUiSettings { SkipUnusableBuffs = false }, RynthAiJsonContext.Default.LegacyUiSettings),
RynthAiJsonContext.Default.LegacyUiSettings);
Check.True(off != null && !off.SkipUnusableBuffs, "off is saved and loaded");
var copy = typeof(LegacyDashboardRenderer).GetMethod("CopySettings", BindingFlags.NonPublic | BindingFlags.Static)!;
var dst = new LegacyUiSettings();
copy.Invoke(null, new object[] { new LegacyUiSettings { SkipUnusableBuffs = false }, dst });
Check.False(dst.SkipUnusableBuffs, "a profile with it off loads off");
copy.Invoke(null, new object[] { old!, dst });
Check.True(dst.SkipUnusableBuffs, "an old profile loads on");
var payload = JsonSerializer.Deserialize("{\"PatrolOnLogin\":false}", RynthAiJsonContext.Default.SettingsBridgePayload);
Check.True(payload != null && payload.SkipUnusableBuffs == null, "an older engine's payload has no value");
var s = new LegacyUiSettings { SkipUnusableBuffs = false };
var dash = (LegacyDashboardRenderer)RuntimeHelpers.GetUninitializedObject(typeof(LegacyDashboardRenderer));
typeof(LegacyDashboardRenderer).GetField("_settings", BindingFlags.NonPublic | BindingFlags.Instance)!.SetValue(dash, s);
typeof(LegacyDashboardRenderer).GetField("_advancedSettingsUi", BindingFlags.NonPublic | BindingFlags.Instance)!
.SetValue(dash, new LegacyAdvancedSettingsUi(s));
var sent = JsonSerializer.Deserialize(dash.BuildSettingsJson(), RynthAiJsonContext.Default.SettingsBridgePayload);
Check.True(sent != null && sent.SkipUnusableBuffs == false, "sent to the engine face");
dash.ApplySettingsJson("{\"PatrolOnLogin\":false}");
Check.False(s.SkipUnusableBuffs, "a save without the field leaves it off");
dash.ApplySettingsJson("{\"SkipUnusableBuffs\":true}");
Check.True(s.SkipUnusableBuffs, "a save with it on turns it on");
// The settings map the meta engine (and /ra settings) reads.
var live = new LegacyUiSettings();
var map = ExpressionTests.Engine(settings: live).BuildSettingsMapPublic();
Check.True(map.TryGetValue("SkipUnusableBuffs", out var entry), "SkipUnusableBuffs is in the settings map");
Check.Eq(entry.Get(), "1", "reads on");
entry.Set("0");
Check.False(live.SkipUnusableBuffs, "and sets off");
}
}