RynthAi: exclude a listed shield from buffing (noBuff on the Items bridge)

A shield listed in Items can be marked noBuff so the buffer skips it. The exclusion is shield-only and carried as noBuff on the Items bridge.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Daniel Wallace 2026-11-07 08:26:37 -08:00
commit 0ddd091de9
5 changed files with 378 additions and 15 deletions

View file

@ -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 &lt;shield&gt;" 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<ItemRule>? _shieldChoiceRules;
private int _shieldChoiceRuleCount;
private long _shieldChoiceExcluded;
private WorldObjectCache? _shieldChoiceCache;
private string _shieldChoiceLogged = "";
@ -89,26 +93,40 @@ public partial class BuffManager
OffhandStowed?.Invoke(id);
}
/// <summary>The Items list's shields the cache knows, in list order.</summary>
private List<int> ListedShields()
/// <summary>
/// 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.
/// <paramref name="excludedIds"/> are the listed shields left out for that.
/// </summary>
private List<int> ListedShields(out List<int> excludedIds)
{
excludedIds = new List<int>();
var found = new List<int>();
var cache = _worldObjectCache;
if (cache == null) return found;
List<ItemRule> rules;
try { rules = new List<ItemRule>(_settings.ItemRules); }
catch { return found; } // the list edited mid-copy: next read
var excludedRule = new HashSet<int>();
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<int> 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;
}
/// <summary>
/// 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.
/// </summary>
private static long ExcludedSignature(List<ItemRule> 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<int> 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";

View file

@ -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" (<see cref="ItemRule.NoBuff"/>) sets KeepBuffed on a
/// shield entry only; on a weapon it is ignored, and absent (an older engine) it changes
/// nothing.
/// </summary>
public static List<ItemRule> MergeEdited(IReadOnlyList<ItemRule> saved, IEnumerable<ItemRule> 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);
}
}

View file

@ -1035,8 +1035,29 @@ public sealed class ItemRule
/// <summary>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.</summary>
public string Kind { get; set; } = "";
/// <summary>
/// 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
/// <see cref="NoBuff"/>.
/// </summary>
public bool KeepBuffed { get; set; } = true;
/// <summary>
/// Items bridge wire field only (ItemsBridgePayload.Weapons), never saved in a profile: true =
/// excluded from buffing (<see cref="KeepBuffed"/> 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.
/// </summary>
[JsonPropertyName("noBuff")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public bool? NoBuff { get; set; }
/// <summary>True when buffing leaves this item alone (<see cref="KeepBuffed"/> off).</summary>
public bool ExcludedFromBuffing() => !KeepBuffed;
/// <summary>True when this row is a shield (off hand), not a main-hand weapon.</summary>
public bool IsShield() => string.Equals(Action, ShieldAction, StringComparison.OrdinalIgnoreCase);
}
@ -1085,6 +1106,8 @@ public sealed class ConsumableRule
/// <summary>Bridge payload for the engine-side Avalonia ItemsPanel.</summary>
public sealed class ItemsBridgePayload
{
/// <summary>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.</summary>
public List<ItemRule> Weapons { get; set; } = new();
public List<ConsumableRule> Consumables { get; set; } = new();
public bool EnableManaTapping { get; set; } = true; // absent from an old profile = the new default

View file

@ -294,6 +294,19 @@ internal sealed class VTankUsdImporter
}
}
/// <summary>
/// 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).
/// </summary>
internal static void ReplaceItemRule(List<ItemRule> 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);
}
/// <summary>Writes the previewed rules into the live settings and saves them.</summary>
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);