RynthSuite/Plugins/RynthCore.Plugin.RynthAi/ConsumableLookup.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

54 lines
2.9 KiB
C#

using System;
using System.Collections.Generic;
using RynthCore.Plugin.RynthAi.LegacyUi;
namespace RynthCore.Plugin.RynthAi;
/// <summary>
/// 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 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.
///
/// <paramref name="skip"/>: ids the caller knows are gone although the walk may still list them (a kit
/// its result line called used up, inside the walk's 1 s reuse; BuffManager.KitUsedUp). A skipped
/// listed id falls through to the name match, so the next stack of that name is used.
/// </summary>
internal static class ConsumableLookup
{
public static WorldObject? Resolve(WorldObjectCache cache, ConsumableRule rule, Func<int, bool>? skip = null)
{
IReadOnlyList<WorldObject> carried = cache.GetDirectInventory();
if (carried.Count == 0)
return ResolveFromCache(cache, rule, skip); // 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 && !Skipped(skip, wo.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) && !Skipped(skip, wo.Id))
return wo;
return null;
}
private static WorldObject? ResolveFromCache(WorldObjectCache cache, ConsumableRule rule, Func<int, bool>? skip)
{
if (rule.Id != 0 && !Skipped(skip, rule.Id) && cache[rule.Id] is WorldObject byId) return byId;
if (string.IsNullOrWhiteSpace(rule.Name)) return null;
foreach (WorldObject wo in cache.GetInventory())
if (wo != null && wo.Name.Equals(rule.Name, StringComparison.OrdinalIgnoreCase) && !Skipped(skip, wo.Id))
return wo;
return null;
}
private static bool Skipped(Func<int, bool>? skip, int id) => skip != null && skip(id);
}