diff --git a/Plugins/RynthCore.Plugin.RynthAi/Combat/BuffManager.Shield.cs b/Plugins/RynthCore.Plugin.RynthAi/Combat/BuffManager.Shield.cs index 4e896a5..c02755a 100644 --- a/Plugins/RynthCore.Plugin.RynthAi/Combat/BuffManager.Shield.cs +++ b/Plugins/RynthCore.Plugin.RynthAi/Combat/BuffManager.Shield.cs @@ -23,6 +23,8 @@ namespace RynthCore.Plugin.RynthAi; /// default) turns it off. /// * When: on the armor's rules (Rebuff With, Also Refresh Under in a batch, Force Rebuff), on the /// shield's own timers, recorded from the "You cast X on <shield>" line. +/// * A shield marked "Exclude from buffing" in the Items list (ItemRule.KeepBuffed off) is +/// treated as an unlisted one: nothing is aimed at it, and it never makes buffing due. /// * Never at a shield being moved (its pack move or wield not settled, a swap under way). /// * A cast at the shield that gets no answer twice in a row, or that the server says can't be /// cast on it, stops shield buffing for ShieldParkTime; the armor is never held up by it. @@ -57,13 +59,15 @@ public partial class BuffManager private const double StowedSightingGraceMs = 10000; // ShieldToBuff walks the inventory: its answer is kept ShieldReadMs (the arbiter asks every tick), - // and worked out again at once when the Items list or the cache changes. Tests set ShieldReadMs 0. + // and worked out again at once when the Items list or the cache changes, or an entry's + // "Exclude from buffing" is flipped in place (ExcludedSignature). Tests set ShieldReadMs 0. internal long ShieldReadMs = 1000; private int _shieldChoice; private string _shieldChoiceWhy = ""; private long _shieldChoiceAt = long.MinValue / 2; private List? _shieldChoiceRules; private int _shieldChoiceRuleCount; + private long _shieldChoiceExcluded; private WorldObjectCache? _shieldChoiceCache; private string _shieldChoiceLogged = ""; @@ -89,26 +93,40 @@ public partial class BuffManager OffhandStowed?.Invoke(id); } - /// The Items list's shields the cache knows, in list order. - private List ListedShields() + /// + /// The Items list's shields the cache knows, in list order. A shield marked "Exclude from + /// buffing" (ItemRule.KeepBuffed off) is left out, so it is treated as an unlisted shield is: + /// never a target. An id with any of its rules excluded is excluded, whatever the rules' order. + /// are the listed shields left out for that. + /// + private List ListedShields(out List excludedIds) { + excludedIds = new List(); var found = new List(); var cache = _worldObjectCache; if (cache == null) return found; List rules; try { rules = new List(_settings.ItemRules); } catch { return found; } // the list edited mid-copy: next read + var excludedRule = new HashSet(); + foreach (var r in rules) + if (r.Id != 0 && r.ExcludedFromBuffing()) excludedRule.Add(r.Id); foreach (var r in rules) { - if (r.Id == 0 || found.Contains(r.Id)) continue; + if (r.Id == 0 || found.Contains(r.Id) || excludedIds.Contains(r.Id)) continue; var wo = cache[r.Id]; if (wo == null) continue; if (CombatManager.IsShieldItem(wo) || (WeaponList.IsShieldRule(r) && wo.ObjectClass == AcObjectClass.Armor)) + { + if (excludedRule.Contains(r.Id)) { excludedIds.Add(r.Id); continue; } found.Add(r.Id); + } } return found; } + private List ListedShields() => ListedShields(out _); + private bool InInventory(int id) { if (_worldObjectCache == null || id == 0) return false; @@ -125,7 +143,8 @@ public partial class BuffManager { long now = KitClockMs(); var rules = _settings.ItemRules; - if (!fresh && ReferenceEquals(rules, _shieldChoiceRules) && rules.Count == _shieldChoiceRuleCount + long excludedSig = ExcludedSignature(rules); + if (!fresh && ReferenceEquals(rules, _shieldChoiceRules) && rules.Count == _shieldChoiceRuleCount && excludedSig == _shieldChoiceExcluded && ReferenceEquals(_worldObjectCache, _shieldChoiceCache) && now >= _shieldChoiceAt && now - _shieldChoiceAt < ShieldReadMs) { where = _shieldChoiceWhy; @@ -137,6 +156,7 @@ public partial class BuffManager _shieldChoiceAt = now; _shieldChoiceRules = rules; _shieldChoiceRuleCount = rules.Count; + _shieldChoiceExcluded = excludedSig; _shieldChoiceCache = _worldObjectCache; // One log line when the answer changes, only for a character with a shield listed. string line = anyListed ? $"{id:X8}|{where}" : ""; @@ -151,13 +171,34 @@ public partial class BuffManager return id; } + /// + /// Which entries are excluded from buffing, as one number (count and ids): ShieldToBuff's kept + /// answer is read again when it changes, so a flag flipped on an entry in place takes effect on + /// the next tick, not after ShieldReadMs. A list walk only (no inventory); -1 if it is edited mid-walk. + /// The Items face (LegacyDashboardRenderer.ApplyItemsJson) and a profile load (CopySettings) + /// replace the list, which the reference check already catches; the VTank importer + /// (VTankUsdImporter.Apply) edits it in place, remove then add, so the count can stay the same. + /// This is the cover for that and any later in-place edit. + /// + private static long ExcludedSignature(List rules) + { + try + { + long count = 0, ids = 0; + foreach (var r in rules) + if (r.ExcludedFromBuffing()) { count++; ids = unchecked(ids * 31 + (uint)r.Id); } + return unchecked((count << 48) ^ ids); + } + catch { return -1; } + } + private int FindShieldToBuff(out string where, out bool anyListed) { anyListed = false; if (!_settings.BuffShield) { where = "Buff the shield is off"; return 0; } - var listed = ListedShields(); - anyListed = listed.Count > 0; - if (!anyListed) { where = "no shield listed"; return 0; } + var listed = ListedShields(out List excludedIds); + anyListed = listed.Count > 0 || excludedIds.Count > 0; + if (listed.Count == 0) { where = excludedIds.Count > 0 ? "the listed shield is excluded from buffing" : "no shield listed"; return 0; } foreach (int id in listed) { if (!IsWieldedLive(id)) continue; @@ -167,6 +208,15 @@ public partial class BuffManager where = "in the off hand"; return id; } + // An excluded shield in the off hand is the one in use: no shield is buffed, + // not one put away for the wand some time before (a stale stow). + foreach (int id in excludedIds) + { + if (!IsWieldedLive(id)) continue; + _stowedShieldId = 0; + where = "an excluded shield is in the off hand"; + return 0; + } if (_stowedShieldId != 0 && listed.Contains(_stowedShieldId) && InInventory(_stowedShieldId)) { where = "put into a pack for the wand"; diff --git a/Plugins/RynthCore.Plugin.RynthAi/Combat/WeaponList.cs b/Plugins/RynthCore.Plugin.RynthAi/Combat/WeaponList.cs index a9a56ce..d163643 100644 --- a/Plugins/RynthCore.Plugin.RynthAi/Combat/WeaponList.cs +++ b/Plugins/RynthCore.Plugin.RynthAi/Combat/WeaponList.cs @@ -141,6 +141,7 @@ internal static class WeaponList list.Add(new ItemRule { Id = r.Id, Name = fullName(r), Action = r.Action, KeepBuffed = r.KeepBuffed, + NoBuff = !r.KeepBuffed, // the face's "no buff" toggle (drawn on shield rows only) Kind = kind?.Invoke(r) ?? "", // A shield shows "Shield" in the element column (it has no element). Element = IsShieldRule(r) ? ShieldAction : r.Element.Length > 0 ? r.Element : UnknownLabel, @@ -181,7 +182,9 @@ internal static class WeaponList /// The Items panel's edited list, mapped onto the saved one by id: the saved name stays /// (the panel shows the full name; entries are matched and saved by id), an element the /// user changed becomes theirs ("set"), and "Unknown" stays unknown. Order and deletions - /// follow the panel. + /// follow the panel. The face's "noBuff" () sets KeepBuffed on a + /// shield entry only; on a weapon it is ignored, and absent (an older engine) it changes + /// nothing. /// public static List MergeEdited(IReadOnlyList saved, IEnumerable edited) { @@ -197,8 +200,9 @@ internal static class WeaponList bool changed = !elem.Equals(old.Element, StringComparison.OrdinalIgnoreCase); list.Add(new ItemRule { - // The panel carries only id, name and element: the rest stays as saved. - Id = old.Id, Name = old.Name, Action = old.Action, KeepBuffed = old.KeepBuffed, + // The panel carries id, name, element and (shields) noBuff: the rest stays as saved. + Id = old.Id, Name = old.Name, Action = old.Action, + KeepBuffed = IsShieldRule(old) && e.NoBuff is bool noBuff ? !noBuff : old.KeepBuffed, Element = changed ? elem : old.Element, ElementSource = changed ? (elem.Length > 0 ? SourceSet : "") : old.ElementSource, }); @@ -206,6 +210,8 @@ internal static class WeaponList else { e.Element = elem; + if (IsShieldRule(e) && e.NoBuff is bool noBuff) e.KeepBuffed = !noBuff; + e.NoBuff = null; // wire only: never saved list.Add(e); } } diff --git a/Plugins/RynthCore.Plugin.RynthAi/LegacyUi/LegacyUiSettings.cs b/Plugins/RynthCore.Plugin.RynthAi/LegacyUi/LegacyUiSettings.cs index 39d3ab4..f711b1f 100644 --- a/Plugins/RynthCore.Plugin.RynthAi/LegacyUi/LegacyUiSettings.cs +++ b/Plugins/RynthCore.Plugin.RynthAi/LegacyUi/LegacyUiSettings.cs @@ -1035,8 +1035,29 @@ public sealed class ItemRule /// Display only (filled by WeaponList.ForDisplay for the Items panel, never read back): /// Melee, Missile, "War Magic", "Void Magic", Shield, Ammo, or "" when the item isn't known. public string Kind { get; set; } = ""; + /// + /// False = "Exclude from buffing", read for a listed SHIELD only: none of the armor + /// spells are aimed at it (BuffManager.Shield.cs), and its timers never make buffing due. On a + /// weapon or wand it does nothing: weapon auras and the wand are as + /// they always were. True for a new entry and for every profile saved before the setting was + /// read (the field was always written as true). Set from the engine's Items face through + /// . + /// public bool KeepBuffed { get; set; } = true; + /// + /// Items bridge wire field only (ItemsBridgePayload.Weapons), never saved in a profile: true = + /// excluded from buffing ( off). WeaponList.ForDisplay fills it for + /// every entry; WeaponList.MergeEdited applies it back for a shield entry only, and an absent + /// field (an engine that predates it) leaves KeepBuffed as saved. + /// + [JsonPropertyName("noBuff")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public bool? NoBuff { get; set; } + + /// True when buffing leaves this item alone ( off). + public bool ExcludedFromBuffing() => !KeepBuffed; + /// True when this row is a shield (off hand), not a main-hand weapon. public bool IsShield() => string.Equals(Action, ShieldAction, StringComparison.OrdinalIgnoreCase); } @@ -1085,6 +1106,8 @@ public sealed class ConsumableRule /// Bridge payload for the engine-side Avalonia ItemsPanel. public sealed class ItemsBridgePayload { + /// Copies for the face (WeaponList.ForDisplay). Each carries "noBuff" (ItemRule.NoBuff, + /// true = excluded from buffing); on write-back it is applied for shield entries only. public List Weapons { get; set; } = new(); public List Consumables { get; set; } = new(); public bool EnableManaTapping { get; set; } = true; // absent from an old profile = the new default diff --git a/Plugins/RynthCore.Plugin.RynthAi/MiniRemote/VTankUsdImporter.cs b/Plugins/RynthCore.Plugin.RynthAi/MiniRemote/VTankUsdImporter.cs index 2fd8e2a..274baca 100644 --- a/Plugins/RynthCore.Plugin.RynthAi/MiniRemote/VTankUsdImporter.cs +++ b/Plugins/RynthCore.Plugin.RynthAi/MiniRemote/VTankUsdImporter.cs @@ -294,6 +294,19 @@ internal sealed class VTankUsdImporter } } + /// + /// An imported weapon or shield replaces the saved entries with its id. A shield the player + /// marked "Exclude from buffing" stays excluded: VTank's .usd has no such setting, so a + /// re-import must not quietly turn buffing back on for it (ItemRule.KeepBuffed). + /// + internal static void ReplaceItemRule(List rules, ItemRule imported) + { + if (rules.Exists(r => r.Id == imported.Id && WeaponList.IsShieldRule(r) && r.ExcludedFromBuffing())) + imported.KeepBuffed = false; + rules.RemoveAll(r => r.Id == imported.Id); + rules.Add(imported); + } + /// Writes the previewed rules into the live settings and saves them. public void Apply() { @@ -307,10 +320,7 @@ internal sealed class VTankUsdImporter s.MonsterRules.Add(m); } foreach (var w in plan.Weapons) - { - s.ItemRules.RemoveAll(r => r.Id == w.Id); - s.ItemRules.Add(w); - } + ReplaceItemRule(s.ItemRules, w); foreach (var c in plan.Consumables) { s.ConsumableRules.RemoveAll(r => r.Id == c.Id); diff --git a/Tools/RynthCore.RynthAiHostTests/Tests/ShieldBuffTests.cs b/Tools/RynthCore.RynthAiHostTests/Tests/ShieldBuffTests.cs index 09f285f..d375d7e 100644 --- a/Tools/RynthCore.RynthAiHostTests/Tests/ShieldBuffTests.cs +++ b/Tools/RynthCore.RynthAiHostTests/Tests/ShieldBuffTests.cs @@ -44,6 +44,15 @@ internal static class ShieldBuffTests r.Add("shield buffs: casts at the shield with no answer, or 'cannot be cast on', rest shield buffing; the armor carries on", NoAnswerParks); r.Add("shield buffs: the shield's timers are saved and loaded with the others", TimersSaved); r.Add("shield buffs: Buff the shield defaults on, loads, saves, copies, is in the settings map", Setting); + r.Add("shield buffs: a shield marked Exclude from buffing gets nothing (rebuff batch, Force Rebuff, expiring), never makes buffing due; the armor as always", ExcludedShield); + r.Add("shield buffs: two shields listed, one excluded: the other is still buffed", ExcludedShieldOtherStillBuffed); + r.Add("shield buffs: cast lines naming an excluded shield (self-cast reaching it, late line, an in-flight cast) never make a rebuff due", ExcludedShieldCastLinesNeverDue); + r.Add("shield buffs: the kept shield choice is read again at once when an entry's exclusion flips in place", ExcludedFlipInvalidatesShieldChoice); + r.Add("shield buffs: Exclude from buffing on a weapon (non-shield) does nothing: the weapon auras as always", ExcludedWeaponDoesNothing); + r.Add("shield buffs: the Items bridge carries noBuff for every entry, applies it back for shields only, absent = unchanged, never saved", NoBuffPayloadContract); + r.Add("shield buffs: an excluded shield in the off hand: no shield is buffed, not one put away for the wand before", ExcludedWornBeatsStaleStow); + r.Add("shield buffs: a shield listed twice, one rule excluded: excluded, in either order", ExcludedAnyRuleEitherOrder); + r.Add("shield buffs: a VTank re-import keeps a shield's Exclude from buffing", ReimportKeepsShieldExclusion); } // ── Rig ─────────────────────────────────────────────────────────────────── @@ -537,6 +546,271 @@ internal static class ShieldBuffTests Check.Eq(again.CastAll().Count, 0, "nothing due after the reload"); } + private static void ExcludedShield() + { + static void Exclude(Rig rig) => rig.S.ItemRules.First(x => x.Id == unchecked((int)Kite)).KeepBuffed = false; + + // Control: the same rig, not excluded, casts at the shield (StowedShieldBuffed has the detail). + var control = new Rig(worn: true); + control.PutAwayForWand(); + Check.Eq(At(control.CastAll(), "Kite Shield").Count(), Armor.Length, "control: the listed shield gets its armor spells"); + + // Put away for the wand and worn: excluded either way. + foreach (bool stowed in new[] { true, false }) + { + var rig = new Rig(worn: true); + Exclude(rig); + if (stowed) rig.PutAwayForWand(); + string how = stowed ? "put away for the wand" : "in the off hand"; + Check.Eq(rig.B.ShieldToBuff(out string where), 0, $"{how}: no shield to buff ({where})"); + Check.Eq(where, "the listed shield is excluded from buffing", $"{how}: and the reason says why"); + var sent = rig.CastAll(); + Check.Eq(At(sent, "Kite Shield").Count(), 0, $"{how}: nothing aimed at the excluded shield"); + Check.Eq(string.Join(", ", At(sent, "player")), $"{Aura}, {ArmorAtPlayer}", $"{how}: the character's buffs as always"); + Check.False(rig.B.NeedsAnyBuff(), $"{how}: nothing due after the run"); + + // Force Rebuff (and /ra buff, which runs it): still nothing at the shield. + rig.B.ForceFullRebuff(); + sent = rig.CastAll(); + Check.Eq(At(sent, "Kite Shield").Count(), 0, $"{how}: Force Rebuff casts nothing at it"); + Check.Eq(string.Join(", ", At(sent, "player")), $"{Aura}, {ArmorAtPlayer}", $"{how}: Force Rebuff still renews the character's"); + Check.False(rig.B.NeedsAnyBuff(), $"{how}: the run ends done"); + Check.False(FakeHost.Casts.Any(c => c.Target == Kite), $"{how}: no cast ever went at the shield"); + } + + // A shield timer from before it was excluded running low makes nothing due. + var was = new Rig(worn: true); + was.PutAwayForWand(); + was.CastAll(); + Check.Eq(was.B.ShieldSpellCount, Armor.Length, "buffed before it was excluded"); + was.SetShieldLeft("Flame Bane VI", 10); + Check.True(was.B.NeedsAnyBuff(), "control: its bane running out is due while it is buffed"); + Exclude(was); + was.S.ItemRules = new List(was.S.ItemRules); // the Items face saves a new list + Check.False(was.B.NeedsAnyBuff(), "excluded: its expiring bane is not due"); + Check.Eq(was.CastAll().Count, 0, "and nothing is cast for it"); + + // Two shields listed, the worn one excluded: the other (in a pack, not in use) is no target either. + var two = new Rig(worn: true); + two.List(Sword, Kite, Round); + Exclude(two); + Check.Eq(two.B.ShieldToBuff(out _), 0, "the worn shield excluded, the other in a pack: none"); + Check.Eq(At(two.CastAll(), "Round Shield").Count(), 0, "nothing goes to the other shield instead"); + } + + private static void ExcludeId(Rig rig, uint id) => rig.S.ItemRules.First(x => x.Id == unchecked((int)id)).KeepBuffed = false; + + private static void ExcludedShieldOtherStillBuffed() + { + // The realistic case (a pin against an over-broad exclusion, e.g. "an excluded shield turns + // shield buffing off"): the Kite excluded and in a pack, the Round worn: the Round is buffed. + var pin = new Rig(); + Wear(Round); + pin.List(Sword, Kite, Round); + ExcludeId(pin, Kite); + Check.Eq(pin.B.ShieldToBuff(out string where), unchecked((int)Round), "the other listed shield, worn: " + where); + var sent = pin.CastAll(); + Check.Eq(string.Join(", ", At(sent, "Round Shield")), ArmorAtPlayer, "the other shield gets the armor spells"); + Check.Eq(At(sent, "Kite Shield").Count(), 0, "nothing at the excluded one"); + + // Discriminating: both read as in hand, the excluded Kite listed FIRST. Ignoring the flag, + // the first listed (Kite) would be picked; with it, the Round is. + var both = new Rig(worn: true); + Wear(Round); + both.List(Sword, Kite, Round); + Check.Eq(both.B.ShieldToBuff(out _), unchecked((int)Kite), "control: nothing excluded, the first listed in hand is picked"); + ExcludeId(both, Kite); + Check.Eq(both.B.ShieldToBuff(out where, fresh: true), unchecked((int)Round), "the Kite excluded: the Round is picked (" + where + ")"); + sent = both.CastAll(); + Check.Eq(string.Join(", ", At(sent, "Round Shield")), ArmorAtPlayer, "and buffed"); + Check.Eq(At(sent, "Kite Shield").Count(), 0, "never the excluded one"); + Check.False(both.B.NeedsAnyBuff(), "nothing due after the run"); + } + + private static void ExcludedShieldCastLinesNeverDue() + { + // 1. The self-cast reached the worn, excluded shield ("You cast X on Kite Shield"): no timer + // is kept for it, so nothing on it can ever run low and make buffing due. + var self = new Rig(worn: true); + ExcludeId(self, Kite); + var sent = self.CastAll(alsoOnShield: _ => true); + Check.Eq(At(sent, "Kite Shield").Count(), 0, "nothing aimed at it"); + Check.Eq(self.B.ShieldSpellCount, 0, "its self-cast lines record no shield timer"); + Check.True(Armor.All(self.HasArmor), "the armor's are recorded as always"); + Check.False(self.B.NeedsAnyBuff(), "nothing due"); + + // 2. A late line naming it, nothing pending. + self.B.OnChatWindowText("You cast Flame Bane VI on Kite Shield", 7); + Check.Eq(self.B.ShieldSpellCount, 0, "a late line names it: still no shield timer"); + Check.False(self.B.NeedsAnyBuff(), "and nothing due"); + + // 3. A cast at it already in flight when it is excluded: its line may record the timer, + // but neither that nor the timer running low makes anything due. + var flight = new Rig(worn: true); + flight.PutAwayForWand(); + flight.B.ForceFullRebuff(); + string first = ToFirstShieldCast(flight); + Check.Eq(first, "Impenetrability VI", "a cast at the shield is pending"); + ExcludeId(flight, Kite); + flight.S.ItemRules = new List(flight.S.ItemRules); // the Items face saves a new list + flight.Answer(Kite, first); + Check.Eq(flight.B.PendingSpellId, 0, "its line resolves the cast"); + Check.Eq(flight.CastAll().Count(s => s.EndsWith("@Kite Shield", StringComparison.Ordinal)), 0, "nothing more at it"); + Check.False(flight.B.NeedsAnyBuff(), "nothing due after the run"); + if (flight.HasShield("Impenetrability VI")) + { + flight.SetShieldLeft("Impenetrability VI", 10); + Check.False(flight.B.NeedsAnyBuff(), "its recorded timer running low is not due"); + } + Check.Eq(flight.CastAll().Count, 0, "and nothing is cast for it"); + + // Control for 1: the same lines with it not excluded do record the shield's timers. + var control = new Rig(worn: true); + control.CastAll(alsoOnShield: _ => true); + Check.Eq(control.B.ShieldSpellCount, Armor.Length, "control: not excluded, the lines record its timers"); + } + + private static void ExcludedWornBeatsStaleStow() + { + // The Kite was put away for the wand some time ago; the Round is now in the off hand. + Rig Make(bool excludeRound) + { + var made = new Rig(worn: true); + made.List(Sword, Kite, Round); + made.PutAwayForWand(); // the Kite into a pack, remembered as stowed + Wear(Round); + if (excludeRound) ExcludeId(made, Round); + return made; + } + + var control = Make(excludeRound: false); + Check.Eq(control.B.ShieldToBuff(out string where), unchecked((int)Round), "control: the Round in the off hand is picked (" + where + ")"); + + var rig = Make(excludeRound: true); + Check.Eq(rig.B.ShieldToBuff(out where), 0, "the excluded Round in the off hand: no shield"); + Check.Eq(where, "an excluded shield is in the off hand", "and the reason says so"); + var sent = rig.CastAll(); + Check.Eq(At(sent, "Kite Shield").Count() + At(sent, "Round Shield").Count(), 0, "no cast names either shield: " + string.Join(", ", sent)); + Check.Eq(string.Join(", ", At(sent, "player")), $"{Aura}, {ArmorAtPlayer}", "the character's buffs as always"); + Check.False(rig.B.NeedsAnyBuff(), "nothing due"); + } + + private static void ExcludedAnyRuleEitherOrder() + { + foreach (bool excludedFirst in new[] { false, true }) + { + var rig = new Rig(worn: true); + var keep = new ItemRule { Id = unchecked((int)Kite), Name = "Kite Shield", Action = WeaponList.ShieldAction, Element = "", ElementSource = "" }; + var off = new ItemRule { Id = unchecked((int)Kite), Name = "Kite Shield", Action = WeaponList.ShieldAction, Element = "", ElementSource = "", KeepBuffed = false }; + var sword = rig.S.ItemRules.First(x => x.Id == unchecked((int)Sword)); + rig.S.ItemRules = excludedFirst ? new List { sword, off, keep } : new List { sword, keep, off }; + string order = excludedFirst ? "the excluded rule first" : "the excluded rule second"; + Check.Eq(rig.B.ShieldToBuff(out string where), 0, $"{order}: no shield ({where})"); + Check.Eq(At(rig.CastAll(), "Kite Shield").Count(), 0, $"{order}: nothing at it"); + } + } + + private static void ReimportKeepsShieldExclusion() + { + ItemRule Rule(int id, string action, bool keep) => new() { Id = id, Name = "x" + id, Action = action, Element = "Slash", KeepBuffed = keep }; + var rules = new List + { + Rule(1, WeaponList.ShieldAction, false), // an excluded shield + Rule(2, WeaponList.ShieldAction, true), + Rule(3, "Weapon", false), // an "excluded" weapon (the flag does nothing there) + }; + foreach (int id in new[] { 1, 2, 3, 4 }) + RynthCore.Plugin.RynthAi.MiniRemote.VTankUsdImporter.ReplaceItemRule(rules, Rule(id, id <= 2 ? WeaponList.ShieldAction : "Weapon", true)); + Check.Eq(rules.Count, 4, "each id replaced once, the new one added"); + Check.True(rules.Single(x => x.Id == 1).ExcludedFromBuffing(), "the excluded shield stays excluded after a re-import"); + Check.False(rules.Single(x => x.Id == 2).ExcludedFromBuffing(), "a buffed shield stays buffed"); + Check.False(rules.Single(x => x.Id == 3).ExcludedFromBuffing(), "a weapon takes the import as it is"); + Check.False(rules.Single(x => x.Id == 4).ExcludedFromBuffing(), "a new entry is buffed"); + } + + private static void ExcludedFlipInvalidatesShieldChoice() + { + var rig = new Rig(worn: true); + long ms = 50_000; + rig.B.KitClockMs = () => ms; + rig.B.ShieldReadMs = 1000; // the shipped value + Check.Eq(rig.B.ShieldToBuff(out _), unchecked((int)Kite), "the worn shield, kept for a second"); + ms += 10; + Check.Eq(rig.B.ShieldToBuff(out _), unchecked((int)Kite), "control: 10 ms later the kept answer"); + ExcludeId(rig, Kite); // flipped in place: same list, same count + ms += 10; + Check.Eq(rig.B.ShieldToBuff(out string where), 0, "excluded in place: read again at once, none (" + where + ")"); + rig.S.ItemRules.First(x => x.Id == unchecked((int)Kite)).KeepBuffed = true; + ms += 10; + Check.Eq(rig.B.ShieldToBuff(out _), unchecked((int)Kite), "and back on: at once"); + } + + private static void ExcludedWeaponDoesNothing() + { + // Excluding a weapon does nothing: the excluded sword's own aura (Blood Drinker) is still + // due. Only a shield is ever left out of buffing. + var rig = new Rig(worn: true, listShield: false); + ExcludeId(rig, Sword); + var sent = rig.CastAll(); + Check.Eq(string.Join(", ", At(sent, "player")), $"{Aura}, {ArmorAtPlayer}", "the excluded sword still makes its aura due"); + Check.Eq(rig.B.ShieldToBuff(out _), 0, "and no shield is involved"); + } + + private static void NoBuffPayloadContract() + { + const int SwordId = 1, ShieldId = 3; + List Saved(bool swordKeep, bool shieldKeep) => new() + { + new() { Id = SwordId, Name = "Sword", Action = "Weapon", Element = "Slash", KeepBuffed = swordKeep }, + new() { Id = ShieldId, Name = "Shield", Action = WeaponList.ShieldAction, Element = "", KeepBuffed = shieldKeep }, + }; + + // Out: every entry carries noBuff, shields and weapons alike. + var shown = WeaponList.ForDisplay(Saved(false, false), x => x.Name); + Check.True(shown.All(x => x.NoBuff == true), "an excluded entry is sent noBuff true (weapon and shield)"); + shown = WeaponList.ForDisplay(Saved(true, true), x => x.Name); + Check.True(shown.All(x => x.NoBuff == false), "a buffed entry is sent noBuff false"); + string json = JsonSerializer.Serialize(new ItemsBridgePayload { Weapons = WeaponList.ForDisplay(Saved(true, false), x => x.Name) }, + RynthAiJsonContext.Default.ItemsBridgePayload); + Check.Eq(System.Text.RegularExpressions.Regex.Matches(json, "\"noBuff\":").Count, 2, "the wire field is \"noBuff\", once per entry: " + json); + Check.True(System.Text.RegularExpressions.Regex.IsMatch(json, "\"noBuff\":\\s*false") && System.Text.RegularExpressions.Regex.IsMatch(json, "\"noBuff\":\\s*true"), + "with its value per entry: " + json); + + // Back, in the engine's shape (lower-case names, only the fields WeaponEntry knows). + List Back(string weapons) => + WeaponList.MergeEdited(Saved(true, true), + JsonSerializer.Deserialize("{\"weapons\":[" + weapons + "]}", RynthAiJsonContext.Default.ItemsBridgePayload)!.Weapons); + const string Sw = "{\"id\":1,\"name\":\"Sword\",\"element\":\"Slash\",\"action\":\"Weapon\""; + const string Sh = "{\"id\":3,\"name\":\"Shield\",\"element\":\"Shield\",\"action\":\"Shield\""; + var m = Back(Sw + "}," + Sh + ",\"noBuff\":true}"); + Check.True(m.Single(x => x.Id == ShieldId).ExcludedFromBuffing(), "a shield ticked noBuff is excluded"); + m = Back(Sw + ",\"noBuff\":true}," + Sh + "}"); + Check.False(m.Single(x => x.Id == SwordId).ExcludedFromBuffing(), "noBuff on a weapon is ignored"); + m = WeaponList.MergeEdited(Saved(true, false), + JsonSerializer.Deserialize("{\"weapons\":[" + Sw + "}," + Sh + ",\"noBuff\":false}]}", RynthAiJsonContext.Default.ItemsBridgePayload)!.Weapons); + Check.False(m.Single(x => x.Id == ShieldId).ExcludedFromBuffing(), "a shield unticked is buffed again"); + m = WeaponList.MergeEdited(Saved(false, false), + JsonSerializer.Deserialize("{\"weapons\":[" + Sw + "}," + Sh + "}]}", RynthAiJsonContext.Default.ItemsBridgePayload)!.Weapons); + Check.True(m.All(x => x.ExcludedFromBuffing()), "noBuff absent (an older engine): both keep their saved exclusion"); + m = WeaponList.MergeEdited(Saved(true, true), + JsonSerializer.Deserialize("{\"weapons\":[" + Sw + "}," + Sh + ",\"noBuff\":null}]}", RynthAiJsonContext.Default.ItemsBridgePayload)!.Weapons); + Check.False(m.Single(x => x.Id == ShieldId).ExcludedFromBuffing(), "noBuff null: unchanged"); + Check.True(m.All(x => x.NoBuff == null), "merged entries carry no wire field"); + var added = WeaponList.MergeEdited(new List(), new[] { new ItemRule { Id = 9, Name = "New", Action = WeaponList.ShieldAction, NoBuff = true } }); + Check.True(added.Single().ExcludedFromBuffing() && added.Single().NoBuff == null, "a new shield entry takes noBuff, and drops the wire field"); + + // Saved profiles: the field is never written; old profiles load with nothing excluded. + var settings = new LegacyUiSettings { ItemRules = m }; + string profile = JsonSerializer.Serialize(settings, RynthAiJsonContext.Default.LegacyUiSettings); + Check.False(profile.Contains("noBuff", StringComparison.OrdinalIgnoreCase), "a saved profile has no noBuff"); + var old = JsonSerializer.Deserialize("{\"ItemRules\":[{\"Id\":1,\"Name\":\"Sword\",\"Action\":\"Weapon\"},{\"Id\":3,\"Name\":\"Shield\",\"Action\":\"Shield\",\"KeepBuffed\":true}]}", + RynthAiJsonContext.Default.LegacyUiSettings); + Check.True(old != null && old.ItemRules.Count == 2 && old.ItemRules.All(x => !x.ExcludedFromBuffing()), "old profiles load with nothing excluded"); + var round = JsonSerializer.Deserialize(JsonSerializer.Serialize(new LegacyUiSettings { ItemRules = { new ItemRule { Id = 3, Name = "Shield", Action = WeaponList.ShieldAction, KeepBuffed = false } } }, + RynthAiJsonContext.Default.LegacyUiSettings), RynthAiJsonContext.Default.LegacyUiSettings); + Check.True(round != null && round.ItemRules.Single().ExcludedFromBuffing(), "excluded is saved and loaded"); + } + private static void Setting() { Check.True(new LegacyUiSettings().BuffShield, "a new profile: on");