diff --git a/Plugins/RynthCore.Plugin.RynthAi/Combat/BuffManager.cs b/Plugins/RynthCore.Plugin.RynthAi/Combat/BuffManager.cs index 4275855..385140f 100644 --- a/Plugins/RynthCore.Plugin.RynthAi/Combat/BuffManager.cs +++ b/Plugins/RynthCore.Plugin.RynthAi/Combat/BuffManager.cs @@ -2691,7 +2691,7 @@ public partial class BuffManager : IDisposable if (KitRetryWait(out string kitWait)) return VitalNo(kitWait); // Too likely to fail on what's missing (KitMinSuccessPct): let the spell have it. uint missing = _vitals.MaxHealth > _vitals.CurrentHealth ? _vitals.MaxHealth - _vitals.CurrentHealth : 0; - if (!ignoreMinSuccess && !KitChanceOk(kit.Id, kit.Name ?? "", missing, _settings.PeaceModeForKits)) return VitalNo($"{kit.Name} under Kit Min Success %"); + if (!ignoreMinSuccess && !KitChanceOk(kit.Id, kit.Name ?? "", missing, _settings.PeaceModeForKits, _settings.KitMinSuccessPct)) return VitalNo($"{kit.Name} under Kit Min Success %"); if (_settings.PeaceModeForKits && mode != CombatMode.NonCombat && _host.HasChangeCombatMode) { // Only a request that went out starts the wait: a refusal (the engine turns stance changes away @@ -2728,7 +2728,8 @@ public partial class BuffManager : IDisposable if (kit == null) return false; if (KitRetryWait(out _)) return false; uint missing = _vitals.MaxStamina > _vitals.CurrentStamina ? _vitals.MaxStamina - _vitals.CurrentStamina : 0; - if (!KitChanceOk(kit.Id, kit.Name ?? "", missing, _settings.PeaceModeForKits)) return false; + // Its own line (Stamina Kit Min Success %): below it Revitalize Self or a potion goes. + if (!KitChanceOk(kit.Id, kit.Name ?? "", missing, _settings.PeaceModeForKits, _settings.StaminaKitMinSuccessPct)) return false; if (_settings.PeaceModeForKits && mode != CombatMode.NonCombat && _host.HasChangeCombatMode) { if (_host.ChangeCombatModeFor(CombatMode.NonCombat, "Buff", "peace mode for the stamina kit")) @@ -2850,13 +2851,13 @@ public partial class BuffManager : IDisposable internal const int WeaponOutKitMinPct = 30; /// - /// True when a kit may be used: KitMinSuccessPct is 0, or the kit's chance of working on + /// True when a kit may be used: (Kit Min Success % for healing kits, + /// Stamina Kit Min Success % for stamina kits) is 0, or the kit's chance of working on /// points is at least that. Below it the caller falls through /// to the spell, as VTank does. /// - private bool KitChanceOk(int kitId, string kitName, uint missing, bool willBePeace) + private bool KitChanceOk(int kitId, string kitName, uint missing, bool willBePeace, int minPct) { - int minPct = _settings.KitMinSuccessPct; // A weapon out: the spell is a wand swap, the Magic stance and the cast first (4 s and more // from melee), so a kit gets a lower bar. Drakkon, 2026-10-14 13:50: a 51% kit was skipped // for Heal Self from melee and he died before the cast went out. diff --git a/Plugins/RynthCore.Plugin.RynthAi/Combat/WorldObjectCache.cs b/Plugins/RynthCore.Plugin.RynthAi/Combat/WorldObjectCache.cs index 257204d..7234850 100644 --- a/Plugins/RynthCore.Plugin.RynthAi/Combat/WorldObjectCache.cs +++ b/Plugins/RynthCore.Plugin.RynthAi/Combat/WorldObjectCache.cs @@ -1475,6 +1475,7 @@ public class WorldObjectCache UpsertDirectInventoryItem(cachedItem); } + if (_directInventory.Count > 0) DropVanishedInventory(); return _directInventory.ToList(); } } @@ -2254,6 +2255,42 @@ public class WorldObjectCache return true; } + /// Inventory entries dropped by (for the log and the tests). + internal int VanishedDropped { get; private set; } + + /// + /// After a live walk: an inventory entry the walk didn't find and the client can't even name any + /// more is gone (its removal was missed), so it leaves the inventory set. Wielded gear (never in + /// the walk) and anything the client still knows stay: a partial walk can't empty the pack. + /// A used-up Treated Healing Kit stayed in the set and was "used" 70 times after a respawn, the + /// server answering at once with no heal (2026-11-03); every reader of GetInventory (kits, + /// potions, tinctures, mana stones, component counts, meta item counts, /mt use) saw it. Under _gate. + /// + private void DropVanishedInventory() + { + List? gone = null; + foreach (int id in _inventory) + { + if (_directInventoryIds.Contains(id)) continue; + uint uid = unchecked((uint)id); + if (_host.TryGetObjectName(uid, out string name) && !string.IsNullOrWhiteSpace(name)) continue; + (gone ??= new List()).Add(id); + } + if (gone == null) return; + foreach (int id in gone) + { + string name = _byId.TryGetValue(id, out var wo) ? wo.Name : string.Empty; + _inventory.Remove(id); + _byId.Remove(id); + _classifyRetry.Remove(unchecked((uint)id)); + _slowRetry.Remove(unchecked((uint)id)); + _reclassifySkipState.Remove(unchecked((uint)id)); + VanishedDropped++; + _host.Log($"[RynthAi] Inventory: 0x{unchecked((uint)id):X8} '{name}' is gone from the client (used up, given or dropped; its removal was missed): dropped from the cache."); + } + _inventoryDirty = true; + } + private void UpsertDirectInventoryItem(WorldObject item) { if (_directInventoryIndex.TryGetValue(item.Id, out int index)) diff --git a/Plugins/RynthCore.Plugin.RynthAi/ConsumableLookup.cs b/Plugins/RynthCore.Plugin.RynthAi/ConsumableLookup.cs index a7be127..0304562 100644 --- a/Plugins/RynthCore.Plugin.RynthAi/ConsumableLookup.cs +++ b/Plugins/RynthCore.Plugin.RynthAi/ConsumableLookup.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using RynthCore.Plugin.RynthAi.LegacyUi; namespace RynthCore.Plugin.RynthAi; @@ -7,13 +8,35 @@ namespace RynthCore.Plugin.RynthAi; /// The item a Consumables rule means right now. A rule is saved with the stack it was added /// from (its object id), and that stack disappears when it runs out: 200 Stamina Tinctures /// used up, and the next stack was never touched until the player added it again (report -/// 2026-10-10). So: the listed stack while it is still known, else any carried item with the +/// 2026-10-10). So: the listed stack while it is still carried, else any carried item with the /// same name. Potions, healing kits and lockpicks go through here; pets and mana stones /// already matched by name. +/// +/// "Carried" is the live container walk (WorldObjectCache.GetDirectInventory: the client's own +/// pack contents, read at most once a second), not the cache's inventory set: a used-up kit +/// can stay in that set when its removal is missed, and was then "used" over and over with +/// nothing happening (2026-11-03, DreamWeave: a used-up Treated Healing Kit was picked 70 times +/// after a respawn, every use answered at once with no heal, and only natural regeneration +/// raised health). Without the walk (an engine without GetContainerContents) the cache is used. /// internal static class ConsumableLookup { public static WorldObject? Resolve(WorldObjectCache cache, ConsumableRule rule) + { + IReadOnlyList carried = cache.GetDirectInventory(); + if (carried.Count == 0) + return ResolveFromCache(cache, rule); // no walk yet (or none possible): the cache's view + if (rule.Id != 0) + foreach (WorldObject wo in carried) + if (wo != null && wo.Id == rule.Id) return wo; + if (string.IsNullOrWhiteSpace(rule.Name)) return null; + foreach (WorldObject wo in carried) + if (wo != null && wo.Name.Equals(rule.Name, StringComparison.OrdinalIgnoreCase)) + return wo; + return null; + } + + private static WorldObject? ResolveFromCache(WorldObjectCache cache, ConsumableRule rule) { if (rule.Id != 0 && cache[rule.Id] is WorldObject byId) return byId; if (string.IsNullOrWhiteSpace(rule.Name)) return null; diff --git a/Plugins/RynthCore.Plugin.RynthAi/LegacyUi/LegacyDashboardRenderer.cs b/Plugins/RynthCore.Plugin.RynthAi/LegacyUi/LegacyDashboardRenderer.cs index 363b8cd..8fc0906 100644 --- a/Plugins/RynthCore.Plugin.RynthAi/LegacyUi/LegacyDashboardRenderer.cs +++ b/Plugins/RynthCore.Plugin.RynthAi/LegacyUi/LegacyDashboardRenderer.cs @@ -1260,6 +1260,7 @@ internal sealed partial class LegacyDashboardRenderer UseKitsInMagicMode = s.UseKitsInMagicMode, PeaceModeForKits = s.PeaceModeForKits, KitMinSuccessPct = s.KitMinSuccessPct, + StaminaKitMinSuccessPct = s.StaminaKitMinSuccessPct, EmergencyHealAt = s.EmergencyHealAt, StaminaToHealthAt = s.StaminaToHealthAt, StaminaToHealthMinStamina = s.StaminaToHealthMinStamina, @@ -1524,6 +1525,7 @@ internal sealed partial class LegacyDashboardRenderer s.UseKitsInMagicMode = p.UseKitsInMagicMode; s.PeaceModeForKits = p.PeaceModeForKits; s.KitMinSuccessPct = Math.Clamp(p.KitMinSuccessPct, 0, 100); + s.StaminaKitMinSuccessPct = Math.Clamp(p.StaminaKitMinSuccessPct, 0, 100); s.EmergencyHealAt = Math.Clamp(p.EmergencyHealAt, 0, 100); s.StaminaToHealthAt = Math.Clamp(p.StaminaToHealthAt, 0, 100); s.StaminaToHealthMinStamina = Math.Clamp(p.StaminaToHealthMinStamina, 0, 100); @@ -2081,6 +2083,7 @@ internal sealed partial class LegacyDashboardRenderer dst.UseKitsInMagicMode = tmp.UseKitsInMagicMode; dst.PeaceModeForKits = tmp.PeaceModeForKits; dst.KitMinSuccessPct = tmp.KitMinSuccessPct; + dst.StaminaKitMinSuccessPct = tmp.StaminaKitMinSuccessPct; dst.EmergencyHealAt = tmp.EmergencyHealAt; dst.StaminaToHealthAt = tmp.StaminaToHealthAt; dst.StaminaToHealthMinStamina= tmp.StaminaToHealthMinStamina; diff --git a/Plugins/RynthCore.Plugin.RynthAi/LegacyUi/LegacyUiSettings.cs b/Plugins/RynthCore.Plugin.RynthAi/LegacyUi/LegacyUiSettings.cs index 5f7f3d4..4a85064 100644 --- a/Plugins/RynthCore.Plugin.RynthAi/LegacyUi/LegacyUiSettings.cs +++ b/Plugins/RynthCore.Plugin.RynthAi/LegacyUi/LegacyUiSettings.cs @@ -180,9 +180,12 @@ public sealed class LegacyUiSettings public int MakeRationsBelow = 5; // 0 = don't cook field rations public bool UseKitsInMagicMode = true; public bool PeaceModeForKits = false; - // Use a healing/stamina kit only when its success chance (ACE's own skill check) is at + // Use a healing kit only when its success chance (ACE's own skill check) is at // least this %, else cast the spell instead, as VTank does. 0 = always use kits. public int KitMinSuccessPct = 70; + // The same for stamina kits (2026-11-03, its own setting): below it Revitalize Self (or a + // stamina potion) goes instead. 0 = always use stamina kits. + public int StaminaKitMinSuccessPct = 70; // Emergency heal: at or under this health %, a healing kit (out of Magic mode, ahead of Heal // At's chain) and, while a cast is pending, a kit or potion. 0 = off. public int EmergencyHealAt = 30; @@ -1212,6 +1215,7 @@ public sealed class SettingsBridgePayload public bool UseKitsInMagicMode { get; set; } public bool PeaceModeForKits { get; set; } public int KitMinSuccessPct { get; set; } = 70; + public int StaminaKitMinSuccessPct { get; set; } = 70; // an engine that doesn't know it keeps 70 public int EmergencyHealAt { get; set; } = 30; public int StaminaToHealthAt { get; set; } = 30; public int StaminaToHealthMinStamina { get; set; } = 20; diff --git a/Plugins/RynthCore.Plugin.RynthAi/Meta/ExpressionEngine.cs b/Plugins/RynthCore.Plugin.RynthAi/Meta/ExpressionEngine.cs index 2806eb0..a78718f 100644 --- a/Plugins/RynthCore.Plugin.RynthAi/Meta/ExpressionEngine.cs +++ b/Plugins/RynthCore.Plugin.RynthAi/Meta/ExpressionEngine.cs @@ -3626,6 +3626,7 @@ internal sealed partial class ExpressionEngine ["UseKitsInMagicMode"] = (() => B(s.UseKitsInMagicMode), v => s.UseKitsInMagicMode = ToDouble(v) != 0), ["PeaceModeForKits"] = (() => B(s.PeaceModeForKits), v => s.PeaceModeForKits = ToDouble(v) != 0), ["KitMinSuccessPct"] = (() => s.KitMinSuccessPct.ToString(CultureInfo.InvariantCulture), v => s.KitMinSuccessPct = Math.Clamp((int)ToDouble(v), 0, 100)), + ["StaminaKitMinSuccessPct"] = (() => s.StaminaKitMinSuccessPct.ToString(CultureInfo.InvariantCulture), v => s.StaminaKitMinSuccessPct = Math.Clamp((int)ToDouble(v), 0, 100)), ["EmergencyHealAt"] = (() => s.EmergencyHealAt.ToString(CultureInfo.InvariantCulture), v => s.EmergencyHealAt = Math.Clamp((int)ToDouble(v), 0, 100)), ["StaminaToHealthAt"] = (() => s.StaminaToHealthAt.ToString(CultureInfo.InvariantCulture), v => s.StaminaToHealthAt = Math.Clamp((int)ToDouble(v), 0, 100)), ["StaminaToHealthMinStamina"] = (() => s.StaminaToHealthMinStamina.ToString(CultureInfo.InvariantCulture), v => s.StaminaToHealthMinStamina = Math.Clamp((int)ToDouble(v), 0, 100)), diff --git a/Tools/RynthCore.RynthAiHostTests/Program.cs b/Tools/RynthCore.RynthAiHostTests/Program.cs index 51a02f7..f72a038 100644 --- a/Tools/RynthCore.RynthAiHostTests/Program.cs +++ b/Tools/RynthCore.RynthAiHostTests/Program.cs @@ -99,6 +99,7 @@ internal static class Program LootNothingLineTests.Register(runner); ChestLootTests.Register(runner); ItemToolTests.Register(runner); + VanishedItemTests.Register(runner); OwnCorpseTests.Register(runner); UseRefusalTests.Register(runner); CorpseKillerTests.Register(runner); diff --git a/Tools/RynthCore.RynthAiHostTests/Tests/VanishedItemTests.cs b/Tools/RynthCore.RynthAiHostTests/Tests/VanishedItemTests.cs new file mode 100644 index 0000000..82c2143 --- /dev/null +++ b/Tools/RynthCore.RynthAiHostTests/Tests/VanishedItemTests.cs @@ -0,0 +1,134 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Threading; +using RynthCore.Plugin.RynthAi; +using RynthCore.Plugin.RynthAi.LegacyUi; +using RynthCore.Plugin.Shared; +using RynthCore.PluginSdk; +using RynthCore.RynthAiHostTests.Fakes; +using RynthCore.RynthAiTests.Harness; + +namespace RynthCore.RynthAiHostTests.Tests; + +// 2026-11-03, DreamWeave (found by the owner): a Treated Healing Kit was used up, but the cache kept it +// in the inventory (its removal was missed), so the vitals step kept "using" it: after a respawn 70 +// times, every use answered at once with no heal, health rising only by natural regeneration. Kits +// and potions are now chosen from the live container walk, and the cache drops an inventory entry the +// walk doesn't find and the client can't name any more (every reader: stamina kits, tinctures, mana +// stones, component counts, meta item counts). And stamina kits have their own Stamina Kit Min Success %. +internal static class VanishedItemTests +{ + private const uint Player = 0x50000D01; + private const uint KitA = 0x80005D01, KitB = 0x80005D02, Sword = 0x80005D03, Pack = 0x80005D04, Given = 0x80005D05, StamKit = 0x80005D06; + private const uint STypeHealing = 6; + private const BindingFlags Any = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public; + + public static void Register(Runner r) + { + r.Add("vanished items: a used-up kit the client no longer has leaves the cache's inventory after a live walk; wielded and still-known items stay", CacheDropsVanished); + r.Add("vanished items: a Consumables rule's stack that is gone resolves to another carried stack of that name, never the stale one", RuleSkipsVanishedStack); + r.Add("vanished items: without a live walk (no GetContainerContents) the cache's view is used as before", NoWalkFallsBack); + r.Add("stamina kits: Stamina Kit Min Success % decides them, apart from Kit Min Success % (0 = always)", StaminaKitOwnLine); + } + + private static WorldObjectCache Rig(out RynthCoreHost host, bool contents = true) + { + FakeHost.Reset(); + FakeHost.ContentsCalls = contents; + FakeHost.Names[KitA] = "Treated Healing Kit"; + FakeHost.Names[KitB] = "Treated Healing Kit"; + FakeHost.Names[Sword] = "Acid Hand Wraps"; + FakeHost.Names[Given] = "Sapphire"; + FakeHost.Containers[KitA] = Player; + FakeHost.Containers[KitB] = Player; + FakeHost.Containers[Given] = Player; + FakeHost.Wielders[Sword] = Player; + host = FakeHost.Create(Player); + return FakeHost.MakeCache(host, Player, new[] { KitA, KitB, Sword, Given }); + } + + private static bool InInventory(WorldObjectCache c, uint id) => c.GetInventory().Any(w => w.Id == unchecked((int)id)); + + private static void CacheDropsVanished() + { + WorldObjectCache cache = Rig(out _); + Check.True(InInventory(cache, KitA) && InInventory(cache, KitB) && InInventory(cache, Given), "both kits and the gem start in the inventory"); + cache.GetDirectInventory(forceRefresh: true); + Check.Eq(cache.VanishedDropped, 0, "nothing dropped while everything is there"); + + // KitA is used up: the client deletes it (no name, in no pack), and the delete event never reached the cache. + FakeHost.Containers.Remove(KitA); + FakeHost.Names.Remove(KitA); + // The gem was given away: out of the pack, but the client still knows it (it may come back). + FakeHost.Containers.Remove(Given); + Thread.Sleep(300); // past the forced walk's 250 ms floor + cache.GetDirectInventory(forceRefresh: true); + Check.False(InInventory(cache, KitA), "the used-up kit left the inventory"); + Check.True(InInventory(cache, KitB), "the other kit stays"); + Check.True(InInventory(cache, Given), "an item the client still knows stays (only a vanished one is dropped)"); + Check.Eq(cache.VanishedDropped, 1, "one dropped"); + Check.True(FakeHost.Logs.Any(l => l.Contains("0x80005D01 'Treated Healing Kit' is gone from the client")), "and the log says so"); + Check.True(cache.GetInventory().Count() >= 2, "the walk didn't empty the inventory"); + } + + private static void RuleSkipsVanishedStack() + { + WorldObjectCache cache = Rig(out _); + var rule = new ConsumableRule { Id = unchecked((int)KitA), Name = "Treated Healing Kit", Type = "HealthKit" }; + Check.Eq(ConsumableLookup.Resolve(cache, rule)?.Id, unchecked((int)KitA), "the listed stack while it's carried"); + FakeHost.Containers.Remove(KitA); + FakeHost.Names.Remove(KitA); + Thread.Sleep(1100); // past the walk's 1 s reuse + Check.Eq(ConsumableLookup.Resolve(cache, rule)?.Id, unchecked((int)KitB), "gone: the other carried kit of that name"); + FakeHost.Containers.Remove(KitB); + FakeHost.Names.Remove(KitB); + Thread.Sleep(1100); + Check.Eq(ConsumableLookup.Resolve(cache, rule), null, "none carried: nothing (the spell goes), never a stale kit"); + } + + private static void NoWalkFallsBack() + { + WorldObjectCache cache = Rig(out _, contents: false); + var rule = new ConsumableRule { Id = unchecked((int)KitA), Name = "Treated Healing Kit", Type = "HealthKit" }; + Check.Eq(ConsumableLookup.Resolve(cache, rule)?.Id, unchecked((int)KitA), "no walk: the cache's listed stack, as before"); + Check.Eq(cache.VanishedDropped, 0, "and nothing is dropped without a walk"); + } + + private static void StaminaKitOwnLine() + { + bool Uses(int staminaPct, int healthPct) + { + FakeHost.Reset(); + FakeHost.WeaponCalls = true; + FakeHost.UseOnCalls = true; + FakeHost.CombatModeValue = CombatMode.NonCombat; + FakeHost.ContentsCalls = true; + FakeHost.Names[StamKit] = "Greater Stamina Kit"; + FakeHost.Containers[StamKit] = Player; + FakeHost.Skills[(Player, STypeHealing)] = (150, 2); + var host = FakeHost.Create(Player); + var s = new LegacyUiSettings + { + IsMacroRunning = true, PeaceModeForKits = false, UseKitsInMagicMode = true, + KitMinSuccessPct = healthPct, StaminaKitMinSuccessPct = staminaPct, + }; + var skills = new CharacterSkills(host); + skills.SetPlayerId(Player); + var sm = new SpellManager(host, s); + sm.SetCharacterSkills(skills); + // 900 stamina missing: (150 + 200) x 1.1 = 385 against 1800: the kit would almost never work. + var v = new PlayerVitalsCache { MaxHealth = 100, CurrentHealth = 100, MaxStamina = 1000, CurrentStamina = 100, MaxMana = 100, CurrentMana = 100 }; + var b = new BuffManager(host, s, sm, v); + b.SetCharacterSkills(skills); + b.SetWorldObjectCache(FakeHost.MakeCache(host, Player, new[] { StamKit })); + ActionPacer.Reset(); + typeof(BuffManager).GetMethod("AttemptStaminaKitUse", Any)!.Invoke(b, null); + return FakeHost.UsesOn.Any(u => u.Source == StamKit && u.Target == Player); + } + Check.True(new LegacyUiSettings().StaminaKitMinSuccessPct == 70, "Stamina Kit Min Success % starts at 70, like Kit Min Success %"); + Check.False(Uses(staminaPct: 70, healthPct: 0), "a near-0% stamina kit under Stamina Kit Min Success 70 is not used (Kit Min Success 0 doesn't let it)"); + Check.True(Uses(staminaPct: 0, healthPct: 90), "Stamina Kit Min Success 0: always used (Kit Min Success 90 doesn't stop it)"); + } +}