RynthSuite/Plugins/RynthCore.Plugin.RynthAi/Combat/BuffManager.KitUsedUp.cs
Daniel Wallace 493cca6bea Vitals: a kit its result line calls used up is not picked again
A kit's last heal ends "... Your Treated Healing Kit is used up." (ACE
Healer.DoHealing) and the server removes the kit. A use of an item the
player no longer has is answered with a bare UseDone and no chat, so a
used-up kit picked again is a heal lost.

Kit picks read the client's live pack walk (ConsumableLookup.Resolve,
BestPackHealingKit, BestPackStaminaKit), which drops the removed kit, but
a walk is reused for up to 1 s (DirectInventoryCooldownMs), and the next
kit goes out 700 ms after the line (KitLandGraceMs): inside that reuse
the used-up kit was picked again. An engine without GetContainerContents
has no walk at all; its inventory view is the object cache, which can
keep the removed kit for good.

Now the "is used up" line, when it names the kit in flight, makes every
kit pick skip that kit: for 3 s (UsedUpKitSkipMs) with the live walk, long
enough for a fresh walk to have dropped it, and for the session without
it (the server deleted the object; its id never comes back). A line
naming another kit (a late line from a kit that timed out) is logged and
ignored. ConsumableLookup.Resolve takes an optional skip predicate; a
skipped listed id falls through to the name match. Healing and stamina
kits.

WorldObjectCache.WalkClock: the clock GetDirectInventory's reuse runs on
(RynthClock unless a test sets it), so the tests can hold the walk still
and prove the skip, not a refreshed walk, keeps the kit out.

Ported from the earlier kit-drop work onto the live-walk lookup: the rest
of that work (dropping a kit after two bare answers, the 5 minute drop,
the emergency last resort for dropped kits, dropped potion stacks) is
covered by the live walk and is not carried.

Tests: KitUsedUpTests (new, 6). The walk-window cases fail on main and
with KitUsedUp stubbed to false; the no-walk case fails with a 3 s skip.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 09:32:29 -08:00

75 lines
4.1 KiB
C#

// ============================================================================
// RynthSuite - Combat/BuffManager.KitUsedUp.cs
// A kit its own result line says is used up is not picked again.
//
// A kit's last heal ends "... Your Treated Healing Kit is used up." (ACE Healer.DoHealing), and the
// server removes the kit. A use of an item the player no longer has is answered with a bare UseDone
// and no chat, so no heal lands: a used-up kit picked again is a heal lost.
//
// Kit picks read the client's live pack walk (ConsumableLookup.Resolve, BestPackHealingKit,
// BestPackStaminaKit: WorldObjectCache.GetDirectInventory), which drops a removed kit, but that walk
// is reused for up to a second (DirectInventoryCooldownMs) and the next kit goes out KitLandGraceMs
// (700 ms) after the line, inside that reuse. An engine without GetContainerContents has no walk at
// all: its inventory view is the object cache, which can keep a removed kit for good.
//
// So: the line names the kit in flight -> every kit pick skips that kit. With the live walk the skip
// lasts UsedUpKitSkipMs, long enough for a fresh walk to have dropped it; without the walk it lasts
// for the session (the server deleted the object, and its id never comes back). Only a line naming
// the kit in flight counts: a late line from a kit that timed out can land while the next kit is out,
// and must not mark that one. Kit use itself is unchanged.
// ============================================================================
using System;
using System.Collections.Generic;
namespace RynthCore.Plugin.RynthAi;
public partial class BuffManager
{
/// <summary>How long a kit its result line called used up is skipped while the live pack walk runs
/// (the walk's 1 s reuse, with margin). Without the walk the skip does not end.</summary>
internal const double UsedUpKitSkipMs = 3000;
// The kit in flight (its id and name), from the use until the next one.
private int _kitInFlightId;
private string _kitInFlightName = "";
// Kits called used up by their result line, and until when kit picks skip each.
private readonly Dictionary<int, DateTime> _usedUpKitUntil = new();
/// <summary>Kits skipped because their result line said they are used up (tests, status).</summary>
internal int KitsUsedUp { get; private set; }
/// <summary>Records which kit went out (AttemptHealthKitUse, AttemptStaminaKitUse).</summary>
private void NoteKitInFlight(WorldObject kit)
{
_kitInFlightId = kit.Id;
_kitInFlightName = kit.Name ?? "";
}
/// <summary>True while <paramref name="id"/> is a kit its result line called used up (see the header).</summary>
internal bool KitUsedUp(int id)
{
if (id == 0 || !_usedUpKitUntil.TryGetValue(id, out DateTime until)) return false;
if (until > Now) return true;
_usedUpKitUntil.Remove(id);
return false;
}
/// <summary>A kit result line arrived (OnChatWindowText): an "is used up" line naming the kit in flight marks it.</summary>
private void NoteKitResultLine(string text, string lower)
{
if (!lower.Contains(" is used up", StringComparison.Ordinal) || _kitInFlightId == 0) return;
if (_kitInFlightName.Length > 0 && lower.Contains(_kitInFlightName.ToLowerInvariant(), StringComparison.Ordinal))
{
bool walk = _host.HasGetContainerContents;
_usedUpKitUntil[_kitInFlightId] = walk ? Now.AddMilliseconds(UsedUpKitSkipMs) : DateTime.MaxValue;
KitsUsedUp++;
_host.Log($"[RynthAi] Vitals: kit 0x{(uint)_kitInFlightId:X8} '{_kitInFlightName}' is used up (its result line): " +
(walk ? $"kit picks skip it for {UsedUpKitSkipMs / 1000:0} s, until the pack walk has dropped it."
: "kit picks skip it from now on (no pack walk on this engine)."));
}
else
_host.Log($"[RynthAi] Vitals: an 'is used up' line that doesn't name the kit in flight " +
$"(0x{(uint)_kitInFlightId:X8} '{_kitInFlightName}') ignored: \"{text}\"");
}
}