diff --git a/src/RynthCore.Engine/ImGui/Panels/ItemsFace.cs b/src/RynthCore.Engine/ImGui/Panels/ItemsFace.cs index 18af241..9e84bf0 100644 --- a/src/RynthCore.Engine/ImGui/Panels/ItemsFace.cs +++ b/src/RynthCore.Engine/ImGui/Panels/ItemsFace.cs @@ -5,6 +5,9 @@ // and mana stone tapping (on/off, keep count, min max-mana to tap). A listed // weapon the character no longer carries (RynthAi's missingIds) shows in amber // with a warning icon and "(missing)", and says what to do in its tooltip. +// A shield row (Action "Shield") from a RynthAi that sends noBuff shows a +// "No buff" toggle in place of its element column: on = RynthAi never buffs +// that shield. Weapon rows are unchanged. // // Edits change a private copy and save the whole payload (ItemsCommands, // pump thread). A newer snapshot replaces the copy; snapshots from before @@ -91,8 +94,15 @@ internal sealed class ItemsFace : IImGuiPanel int i = idx[k]; ItemsPanel.WeaponEntry wpn = _data.Weapons[i]; ImGuiNET.ImGui.PushID("w" + i); - int r = Row(wpn.Name, wpn.Element, shown++, bw, _data.MissingIds.Contains(wpn.Id), k > 0, k < idx.Count - 1); + int r = Row(wpn.Name, wpn.Element, shown++, bw, _data.MissingIds.Contains(wpn.Id), k > 0, k < idx.Count - 1, + wpn.ShowsNoBuff ? wpn.NoBuff : null); ImGuiNET.ImGui.PopID(); + if (r == 5) + { + wpn.NoBuff = !(wpn.NoBuff ?? false); + Save(); + continue; + } // Up / down inside its group: the order is RynthAi's preference (the buff wand, the // first weapon of a kind), and it couldn't be changed but by removing and re-adding. if (r == 3 || r == 4) @@ -273,13 +283,18 @@ internal sealed class ItemsFace : IImGuiPanel // The up / down buttons' width in a row (two 20 px buttons and their gaps). private const float MoveW = 46; + private const string NoBuffTip = "Never buff this shield - for a shield that already carries its buffs."; + /// /// Name | up | down | [value picker] | Del. Returns 1 when the picker was clicked, 2 for Del, - /// 3 for up, 4 for down, else 0. Up / down are drawn dim and do nothing at the ends. - /// : the item isn't carried any more (amber, a warning icon, - /// "(missing)" and a tooltip saying what to do). + /// 3 for up, 4 for down, 5 for the "No buff" toggle, else 0. Up / down are drawn dim and do + /// nothing at the ends. : the item isn't carried any more (amber, a + /// warning icon, "(missing)" and a tooltip saying what to do). + /// non-null (a shield row, RynthAi sending noBuff): the value column is the "No buff" toggle + /// instead of the picker (a shield has no element; its column only said "Shield"). /// - private static int Row(string name, string value, int index, float w, bool missing, bool canUp = false, bool canDown = false) + private static int Row(string name, string value, int index, float w, bool missing, bool canUp = false, bool canDown = false, + bool? noBuff = null) { Vector2 p = ImGuiNET.ImGui.GetCursorScreenPos(); var dl = ImGuiNET.ImGui.GetWindowDrawList(); @@ -311,7 +326,14 @@ internal sealed class ItemsFace : IImGuiPanel if (IconButton("##down", PhosphorIcons.CaretDown, new Vector2(bx + 22, p.Y + 2), new Vector2(20, 20), canDown ? Teal : Mute, BtnFill) && canDown) result = 4; ImGuiNET.ImGui.SetItemTooltip("Move down"); bx += MoveW; - if (Button("##pick", value, new Vector2(bx, p.Y + 2), new Vector2(96, 20), Text, BtnFill)) result = 1; + if (noBuff is bool nb) + { + // The tapping toggle's look: lit while the shield is excluded. + if (Button("##nobuff", (nb ? PhosphorIcons.CheckSquare : PhosphorIcons.Square) + " No buff", new Vector2(bx, p.Y + 2), new Vector2(96, 20), + nb ? OnText : Mute, nb ? OnBg : BtnFill)) result = 5; + ImGuiNET.ImGui.SetItemTooltip(NoBuffTip); + } + else if (Button("##pick", value, new Vector2(bx, p.Y + 2), new Vector2(96, 20), Text, BtnFill)) result = 1; if (IconButton("##del", PhosphorIcons.Trash, new Vector2(bx + 100, p.Y + 2), new Vector2(42, 20), Red, BtnFill)) result = 2; ImGuiNET.ImGui.SetItemTooltip("Remove"); NextLine(p, 24); diff --git a/src/RynthCore.Engine/UI/Panels/ItemsPanel.cs b/src/RynthCore.Engine/UI/Panels/ItemsPanel.cs index fae56c5..1a92c6d 100644 --- a/src/RynthCore.Engine/UI/Panels/ItemsPanel.cs +++ b/src/RynthCore.Engine/UI/Panels/ItemsPanel.cs @@ -82,8 +82,18 @@ internal static class ItemsPanel // Melee, Missile, "War Magic", "Void Magic", Shield, Ammo, or "" (not known). Set by RynthAi for // display (the Items face groups by it); sent back untouched and ignored there. [JsonPropertyName("kind")] public string Kind { get; set; } = string.Empty; + // True = "Exclude from buffing" (RynthAi ItemRule.KeepBuffed off), sent for every entry by + // RynthAi and applied back by it for shield entries only. Null = the plugin + // predates it: no toggle is drawn, and none is written back (RynthAi keeps its saved value). + [JsonPropertyName("noBuff")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public bool? NoBuff { get; set; } public bool IsShield => string.Equals(Action, ItemsCommands.ShieldAction, StringComparison.OrdinalIgnoreCase); + + /// The Items face draws its "No buff" toggle: a shield entry (Action "Shield", the + /// only kind RynthAi applies it to) from a plugin that sends the field. + [JsonIgnore] public bool ShowsNoBuff => IsShield && NoBuff.HasValue; } internal sealed class ConsumableEntry @@ -874,7 +884,8 @@ internal static class ItemsPanel if (a.Count != b.Count) return true; for (int i = 0; i < a.Count; i++) if (a[i].Id != b[i].Id || a[i].Name != b[i].Name || a[i].Element != b[i].Element - || !string.Equals(a[i].Action, b[i].Action, StringComparison.OrdinalIgnoreCase)) return true; + || !string.Equals(a[i].Action, b[i].Action, StringComparison.OrdinalIgnoreCase) + || a[i].NoBuff != b[i].NoBuff) return true; // so this panel never saves back a stale noBuff return false; } diff --git a/tools/ItemsNoBuffTests/ItemsNoBuffTests.csproj b/tools/ItemsNoBuffTests/ItemsNoBuffTests.csproj new file mode 100644 index 0000000..fb885cf --- /dev/null +++ b/tools/ItemsNoBuffTests/ItemsNoBuffTests.csproj @@ -0,0 +1,28 @@ + + + + + Exe + net10.0-windows + x86 + win-x86 + false + x86 + enable + enable + true + ItemsNoBuffTests + + + + + + + diff --git a/tools/ItemsNoBuffTests/Program.cs b/tools/ItemsNoBuffTests/Program.cs new file mode 100644 index 0000000..52d48a3 --- /dev/null +++ b/tools/ItemsNoBuffTests/Program.cs @@ -0,0 +1,79 @@ +using System.Reflection; +using System.Text.Json; +using System.Text.Json.Serialization.Metadata; +using System.Text.RegularExpressions; + +namespace ItemsNoBuffTests +{ + // The engine half of the shield "No buff" toggle, against what RynthAi's Items bridge writes + // (ItemsBridgePayload: PascalCase names, indented, "noBuff" on every weapon entry) and reads + // (case-insensitive; noBuff applied for Action "Shield" entries only, absent = unchanged). + internal static class Program + { + private static int _fails, _asserts; + private static void Check(bool cond, string msg) + { + _asserts++; + if (!cond) { _fails++; Console.WriteLine($" [FAIL] {msg}"); } + } + + private const string Ns = "RynthCore.Engine.UI.Panels."; + private const BindingFlags All = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance; + + private static int Main() + { + Assembly engine = Assembly.Load("RynthCore.Engine"); + Type ctxType = engine.GetType(Ns + "ItemsPanelJsonContext", throwOnError: true)!; + object ctx = ctxType.GetProperty("Default", All)!.GetValue(null)!; + var info = (JsonTypeInfo)ctxType.GetProperty("Payload", All)!.GetValue(ctx)!; + Type payloadType = engine.GetType(Ns + "ItemsPanel+Payload", throwOnError: true)!; + Type entryType = engine.GetType(Ns + "ItemsPanel+WeaponEntry", throwOnError: true)!; + + List Weapons(object payload) => + ((System.Collections.IEnumerable)payloadType.GetProperty("Weapons", All)!.GetValue(payload)!).Cast().ToList(); + bool? NoBuff(object e) => (bool?)entryType.GetProperty("NoBuff", All)!.GetValue(e); + bool Shows(object e) => (bool)entryType.GetProperty("ShowsNoBuff", All)!.GetValue(e)!; + string Name(object e) => (string)entryType.GetProperty("Name", All)!.GetValue(e)!; + + // --- RynthAi -> engine (the shape RynthAi's BuildItemsJson writes) + const string fromPlugin = "{\n \"Weapons\": [\n" + + " { \"Id\": 1, \"Name\": \"Sword\", \"Action\": \"Weapon\", \"Element\": \"Slash\", \"Kind\": \"Melee\", \"KeepBuffed\": false, \"noBuff\": true },\n" + + " { \"Id\": 2, \"Name\": \"Kite Shield\", \"Action\": \"Shield\", \"Element\": \"Shield\", \"Kind\": \"Shield\", \"KeepBuffed\": true, \"noBuff\": false },\n" + + " { \"Id\": 3, \"Name\": \"Rose Shield\", \"Action\": \"Shield\", \"Element\": \"Shield\", \"Kind\": \"Shield\", \"KeepBuffed\": false, \"noBuff\": true },\n" + + " { \"Id\": 4, \"Name\": \"Buckler\", \"Action\": \"Weapon\", \"Element\": \"Shield\", \"Kind\": \"Shield\", \"noBuff\": true }\n" + + " ],\n \"MissingIds\": []\n}"; + object p = JsonSerializer.Deserialize(fromPlugin, info)!; + var w = Weapons(p); + Check(w.Count == 4, "four entries read"); + Check(NoBuff(w[0]) == true && NoBuff(w[1]) == false && NoBuff(w[2]) == true, "noBuff is read per entry"); + Check(!Shows(w[0]), "a weapon row: no toggle (RynthAi ignores noBuff on weapons)"); + Check(Shows(w[1]) && Shows(w[2]), "a shield row (Action Shield): the toggle"); + Check(!Shows(w[3]), "a shield filed as a Weapon (Kind Shield, Action Weapon): no toggle (RynthAi ignores it there)"); + + // --- An older RynthAi: no field -> no toggle, nothing written back + object old = JsonSerializer.Deserialize("{\"Weapons\":[{\"Id\":2,\"Name\":\"Kite Shield\",\"Action\":\"Shield\",\"Element\":\"Shield\"}]}", info)!; + object oldShield = Weapons(old)[0]; + Check(NoBuff(oldShield) == null, "an older RynthAi: noBuff is null"); + Check(!Shows(oldShield), "and the shield row has no toggle"); + string oldBack = JsonSerializer.Serialize(old, info); + Check(!oldBack.Contains("noBuff"), "and the save carries no noBuff (RynthAi keeps its saved value): " + oldBack); + Check(!oldBack.Contains("ShowsNoBuff"), "the face-only ShowsNoBuff is not on the wire"); + + // --- Engine -> RynthAi: the toggle flipped and saved + entryType.GetProperty("NoBuff", All)!.SetValue(w[1], true); // what the face's click does + entryType.GetProperty("NoBuff", All)!.SetValue(w[2], false); + string back = JsonSerializer.Serialize(p, info); + Check(Regex.IsMatch(back, "\"id\":2,[^}]*\"noBuff\":true"), "the Kite Shield goes back noBuff true: " + back); + Check(Regex.IsMatch(back, "\"id\":3,[^}]*\"noBuff\":false"), "the Rose Shield goes back noBuff false"); + Check(Regex.IsMatch(back, "\"id\":1,[^}]*\"noBuff\":true"), "a weapon's value is round-tripped untouched (RynthAi ignores it)"); + Check(Regex.Matches(back, "\"action\":\"Shield\"").Count == 2 && Name(w[1]) == "Kite Shield", "names and actions round-trip"); + + // The field name is exactly what RynthAi's ItemRule.NoBuff ([JsonPropertyName("noBuff")]) reads. + var attr = entryType.GetProperty("NoBuff", All)!.GetCustomAttribute(); + Check(attr?.Name == "noBuff", "the wire name is noBuff"); + + Console.WriteLine(_fails == 0 ? $"All {_asserts} checks passed." : $"{_fails} of {_asserts} checks FAILED."); + return _fails == 0 ? 0 : 1; + } + } +}