diff --git a/Plugins/RynthCore.Plugin.RynthAi/Combat/BuffManager.cs b/Plugins/RynthCore.Plugin.RynthAi/Combat/BuffManager.cs index 17de8bb..27e4f6b 100644 --- a/Plugins/RynthCore.Plugin.RynthAi/Combat/BuffManager.cs +++ b/Plugins/RynthCore.Plugin.RynthAi/Combat/BuffManager.cs @@ -505,6 +505,56 @@ public partial class BuffManager : IDisposable } private DateTime _buffManaWaitLoggedAt = DateTime.MinValue; + // Since when buffs have waited for Stamina to Mana that can't go yet (BuffWaitsForMana). + private DateTime _reserveWaitSince = DateTime.MinValue; + private DateTime _reserveOverLoggedAt = DateTime.MinValue; + /// The longest buffs wait under the reserve for a Stamina to Mana that can't go yet + /// (stamina under its line with nothing to refill it): then the buff goes, as before. + internal const double ReserveWaitMaxSec = 60; + + // A mana reading that does not move from one buff cast to the next: every buff costs mana, so + // the reading is stale and the floor and the reserve would judge a number that is gone. Drakkon's + // rebuff after a death (2026-10-15) sent a dozen buffs with neither of them holding, then read + // 5%. After StuckManaCasts casts at the same reading, buffs wait for it to move (at most + // StuckManaHoldSec). + private uint _lastBuffCastMana = uint.MaxValue; + private int _sameManaCasts; + private DateTime _stuckManaSince = DateTime.MinValue; + internal const int StuckManaCasts = 2; + internal const double StuckManaHoldSec = 30; + + /// Notes the mana reading a buff cast goes out at (see _lastBuffCastMana). + internal void NoteBuffCastMana(int cost) + { + uint cur = _vitals.CurrentMana; + if (cost > 0 && cur == _lastBuffCastMana && cur < _vitals.MaxMana) + { + _sameManaCasts++; + if (_sameManaCasts == StuckManaCasts) + { + _stuckManaSince = DateTime.Now; + _host.Log($"[RynthAi] Vitals: the mana reading has not moved over {StuckManaCasts + 1} buff casts ({cur}/{_vitals.MaxMana}); buffs wait for a fresh reading (up to {StuckManaHoldSec:0} s)."); + } + } + else + _sameManaCasts = 0; + _lastBuffCastMana = cur; + } + + /// True while buffs wait for a stale mana reading to move (NoteBuffCastMana). + internal bool ManaReadingStuck() + { + if (_sameManaCasts < StuckManaCasts) return false; + if (_vitals.CurrentMana != _lastBuffCastMana + || (DateTime.Now - _stuckManaSince).TotalSeconds >= StuckManaHoldSec) + { + _host.Log($"[RynthAi] Vitals: mana reading {(_vitals.CurrentMana != _lastBuffCastMana ? "moved" : "still the same after " + StuckManaHoldSec.ToString("0") + " s")}: {_lastBuffCastMana} -> {_vitals.CurrentMana}/{_vitals.MaxMana}; buffs go on."); + _sameManaCasts = 0; + _lastBuffCastMana = uint.MaxValue; + return false; + } + return true; + } /// /// Before a buff: true when it waits for mana. Casting it would leave mana under the reserve @@ -513,17 +563,42 @@ public partial class BuffManager : IDisposable /// internal bool BuffWaitsForMana(int spellId, string buffName) { - if (!BuffNeedsManaFirst(spellId, out bool cantPay, out int cost)) return false; + if (!BuffNeedsManaFirst(spellId, out bool cantPay, out int cost)) { _reserveWaitSince = DateTime.MinValue; return false; } uint cur = _vitals.CurrentMana; _vitalWhy = ""; if ((StaminaAllowsStaminaToMana() && AttemptVitalCast("Stamina to Mana Self")) || AttemptPotion("ManaPotion")) { _isRechargingMana = true; + _reserveWaitSince = DateTime.MinValue; LastBuffSkipReason = $"mana {cur}/{_vitals.MaxMana} before {buffName}: Stamina to Mana first"; return true; } - if (!cantPay) return false; // under the reserve after it, but nothing refills: cast the buff anyway + if (!cantPay) + { + // Under the reserve after it and Stamina to Mana can't go right now (stamina under its line + // with nothing to refill it, the server finishing a cast). Spending on regardless is how a + // rebuff ran mana under Stamina to Mana's own cost (2026-10-15): wait for it instead, up to + // ReserveWaitMaxSec. With no Stamina to Mana at all nothing refills: the buff goes. + if (TopStaminaToManaId() == 0) return false; + if (_reserveWaitSince == DateTime.MinValue) _reserveWaitSince = DateTime.Now; + if ((DateTime.Now - _reserveWaitSince).TotalSeconds >= ReserveWaitMaxSec) + { + if ((DateTime.Now - _reserveOverLoggedAt).TotalSeconds >= 30) + { + _reserveOverLoggedAt = DateTime.Now; + _host.Log($"[RynthAi] Vitals: {buffName} goes under the mana reserve ({cur}/{_vitals.MaxMana}, reserve {ManaReserve()}): Stamina to Mana could not go for {ReserveWaitMaxSec:0} s ({Why(_vitalWhy)})."); + } + return false; + } + LastBuffSkipReason = $"mana {cur}/{_vitals.MaxMana}: {buffName} would leave it under the reserve ({ManaReserve()}); waiting for Stamina to Mana"; + if ((DateTime.Now - _buffManaWaitLoggedAt).TotalSeconds >= 30) + { + _buffManaWaitLoggedAt = DateTime.Now; + _host.Log($"[RynthAi] Vitals: buffing waits: {LastBuffSkipReason} (Stamina to Mana: {Why(_vitalWhy)})"); + } + return true; + } LastBuffSkipReason = $"mana {cur}/{_vitals.MaxMana} is under {buffName}'s cost ({cost}); waiting for regen"; if ((DateTime.Now - _buffManaWaitLoggedAt).TotalSeconds >= 30) { @@ -1545,6 +1620,14 @@ public partial class BuffManager : IDisposable return; } + // A stale mana reading: the floor below would judge a number that is gone. + if (ManaReadingStuck()) + { + LastBuffSkipReason = $"mana reading stuck at {_vitals.CurrentMana}/{_vitals.MaxMana}"; + Hold("mana reading stuck"); + return; + } + // Mana floor: no buff while mana is under Get Mana At. CheckVitals above already tried // Stamina to Mana and a mana potion; when neither could run (stamina too low, none // carried) buffing used to carry on and run mana to 0. Wait for it instead. @@ -2709,6 +2792,9 @@ public partial class BuffManager : IDisposable // buffed when it wasn't and never retried the known tier. if (diagnose) _host.Log($"[FR] CAST '{buffBaseName}' resolvedSpellId={spellId} (pending now set)"); + // The mana each buff went out at: the reading the floor and the reserve judged. + NoteBuffCastMana(SpellManaCost(spellId)); + _host.Log($"[Buff] mana {_vitals.CurrentMana}/{_vitals.MaxMana} ({_vitals.ManaPct}%) before {spellInfo?.Name ?? buffBaseName} (cost {SpellManaCost(spellId)}, reserve {ManaReserve()}, Get Mana At {_settings.GetManaAt}%)"); bool fast = _fastChainTick; if (fast) _host.Log($"[BuffChain] '{spellInfo?.Name ?? buffBaseName}' goes without waiting for the animation: {_fastChainWhy}"); diff --git a/Plugins/RynthCore.Plugin.RynthAi/RynthAiPlugin.cs b/Plugins/RynthCore.Plugin.RynthAi/RynthAiPlugin.cs index d8d496f..afd4360 100644 --- a/Plugins/RynthCore.Plugin.RynthAi/RynthAiPlugin.cs +++ b/Plugins/RynthCore.Plugin.RynthAi/RynthAiPlugin.cs @@ -1630,6 +1630,11 @@ public sealed partial class RynthAiPlugin : RynthPluginBase _vitals.CurrentStamina = st; _vitals.MaxStamina = maxSt; _vitals.CurrentMana = mp; + // A big move in the max-mana reading (a death, buffs landing or dropping) is logged: + // the floor and the reserve judge mana by it (2026-10-15). + if (_vitals.MaxMana > 0 && maxMp > 0 + && Math.Abs((long)maxMp - _vitals.MaxMana) * 100 >= 15L * _vitals.MaxMana) + Host.Log($"[RynthAi] Vitals: max mana reading {_vitals.MaxMana} -> {maxMp} (mana {mp})."); _vitals.MaxMana = maxMp; } } diff --git a/Tools/RynthCore.RynthAiHostTests/Tests/ManaReserveTests.cs b/Tools/RynthCore.RynthAiHostTests/Tests/ManaReserveTests.cs index 0f2b38f..4774050 100644 --- a/Tools/RynthCore.RynthAiHostTests/Tests/ManaReserveTests.cs +++ b/Tools/RynthCore.RynthAiHostTests/Tests/ManaReserveTests.cs @@ -33,6 +33,8 @@ internal static class ManaReserveTests r.Add("mana reserve: a melee fighter's attacks aren't held; Get Mana At off means no reserve", MeleeAndOff); r.Add("mana reserve: the dat's BaseMana is read (when this PC has a portal.dat)", DatBaseMana); r.Add("mana reserve: a buff that would leave mana under the reserve waits for Stamina to Mana (melee too)", BuffWaitsForReserve); + r.Add("mana reserve: Stamina to Mana can't go yet (stamina low): the buff waits instead of spending the reserve, up to a minute", BuffWaitsWhenStaminaToManaCantGo); + r.Add("mana reading: unchanged over three buff casts, buffs wait for it to move", StuckManaReadingHoldsBuffs); } private static int Family(string name) => SpellTableStub.GetById(SpellTable.Named(name).First().Id)!.Family; @@ -107,6 +109,39 @@ internal static class ManaReserveTests Check.Eq(FakeHost.Casts.Count, 0, "nothing the server would refuse is sent"); } + // 2026-10-15, Drakkon after a death: under the reserve with Stamina to Mana unable to go, buffs went + // anyway, down to 5% - under Stamina to Mana's own cost. + private static void BuffWaitsWhenStaminaToManaCantGo() + { + var r = new Rig(mana: 300, stamina: 10); // 10/383 stamina: under Stamina to Mana's 15% line, no kit or potion + int bane = Id("Blade Bane VI"); + r.V.CurrentMana = (uint)(r.B.ManaReserve() + 30); + Check.True(r.B.BuffWaitsForMana(bane, "Blade Bane"), "the buff waits: it would spend the reserve"); + Check.Eq(FakeHost.Casts.Count, 0, "Stamina to Mana can't go, and the buff doesn't either"); + Check.True(r.B.BuffWaitsForMana(bane, "Blade Bane"), "still waiting"); + typeof(BuffManager).GetField("_reserveWaitSince", BindingFlags.NonPublic | BindingFlags.Instance)! + .SetValue(r.B, DateTime.Now.AddSeconds(-BuffManager.ReserveWaitMaxSec - 1)); + Check.False(r.B.BuffWaitsForMana(bane, "Blade Bane"), "after a minute the buff goes (no stall for good)"); + Check.True(r.LogCount("goes under the mana reserve") == 1, "and that is logged"); + } + + private static void StuckManaReadingHoldsBuffs() + { + var r = new Rig(mana: 300); + r.B.NoteBuffCastMana(60); + r.V.CurrentMana = 240; + r.B.NoteBuffCastMana(60); + Check.False(r.B.ManaReadingStuck(), "the reading moves with each cast"); + r.B.NoteBuffCastMana(60); + Check.False(r.B.ManaReadingStuck(), "one cast at the same reading is not enough"); + r.B.NoteBuffCastMana(60); + Check.True(r.B.ManaReadingStuck(), "three casts at 240: the reading is stale, buffs wait"); + Check.True(r.LogCount("has not moved") == 1, "logged once"); + r.V.CurrentMana = 60; + Check.False(r.B.ManaReadingStuck(), "a fresh reading: buffs go on (and judge 60, not 240)"); + Check.False(r.B.ManaReadingStuck(), "and stay going"); + } + private static void ReserveChoosesStaminaToMana() { // Over Get Mana At 20% (77 of 383), but under 1.5x the top Stamina to Mana's cost.