RynthSuite/Plugins/RynthCore.Plugin.RynthAi/ItemTool/ChestKeyLooter.cs

688 lines
36 KiB
C#

// ============================================================================
// ChestKeyLooter.cs - the Item Tool's chest looter.
//
// "Pick a chest. Pick a key. It just loots the chest based on the current loot profile until it's
// out of keys." A pure state machine: everything it touches goes through IChestKeyEnv (the plugin
// implements it, RynthAiPlugin.ItemTool.cs; the host tests fake it).
//
// One cycle:
// Ready -> KeyPending -> KeyOut -> OpenPending -> OpenOut -> Looting -> Waiting -> Ready ...
// * KeyPending/KeyOut: the key is used on the chest (AC's use-with-target). ACE walks the
// character to the chest first (Player.HandleActionUseWithTarget -> CreateMoveToChain), so
// there is no walking code here; the chest just has to be loaded and near (MaxStartMeters).
// The server's answer (UseDone + its WeenieError) says what happened: 0 and "The X has been
// unlocked." = unlocked (one use of the key is spent); 0x4AF LockAlreadyUnlocked (nothing
// spent); 0x4B2 KeyDoesntFitThisLock; 0x4B1 the chest is open; 0x4B0 not a lock.
// * OpenPending/OpenOut: the chest is used (opened). The server's ViewContents for it is the
// proof (ChestOpenedAt).
// * Looting: the chest is handed to the corpse looter (the CURRENT loot profile's rules, the
// same TickOpenCorpseLooting as a corpse) and this waits for it to finish and close it.
// * Waiting: a chest re-locks and regenerates on a timer after it was opened (ACE Chest.Open
// queues Chest.Reset ResetInterval seconds later; default 120 s, never under 15 s, and a
// ChestRegenOnClose chest resets as it closes). The next cycle starts RelockWaitSec after
// the open (the Item Tool's "Chest re-lock wait (s)", 2026-11-05; default 125 s, which
// covers ACE's default); where the client reads the chest as locked again (Reset's
// Locked=true public update) it starts earlier. A key use that answers "already unlocked"
// on a later cycle means the timer has not run yet (nothing is spent): it waits ProbeEveryMs
// (the wait, 5..30 s) and asks again, for at most MaxProbes * ProbeMs (4 min) of asking. So
// a wait shorter than the chest's own timer costs one unspent key use per probe, never a key.
// * The re-lock seen (2026-11-28: the 5 s between chests was far too long; a
// DreamWeave Legendary Chest re-locked and refilled as it closed, and every cycle sat out the
// 5 s floor): the server says Locked=false when the key unlocks the chest (LockHelper.Unlock)
// and Locked=true when it resets (Chest.Reset, which a ChestRegenOnClose chest runs inside its
// close). A chest the client read unlocked after this cycle's key and now reads locked has
// reset, whatever the wait: the next key goes once the client shows it closed (CloseSettleMs).
// A Locked=true never seen after an unlocked read may be stale, so it still counts only past
// MinRelockMs, as before. The after-loot floor is the close seen + CloseSettleMs (AfterLootMaxMs
// where the client can't tell), and the wait itself goes down to 1 s.
//
// Out of keys: the key item is gone (used up: "Your key is used up."; no other carried item has
// its name) after a cycle, or before one. A key with unlimited uses never runs out: MaxCycles
// ends the run. Until 2026-11-28 MaxCycles (300) counted every chest, so a long run on keys that
// do run out stopped at 300 ("after 300 keys it stops"); it now counts chests in a row where no
// key use was seen spent ("Your key has N uses left" / "is used up", the key changing or going).
// ACE sends the unlock and the uses-left text as ONE two-line message, which the old parser read
// only to the first line, so OnChat now reads every line.
//
// Nothing taken (2026-11-28, "after a certain amount of time it stops picking up items, chest
// opens, but items don't get picked up"): the corpse looter's tally says, per chest, how many
// items the loot profile wanted and how many pickups landed. MaxStarvedChests chests in a row
// that had wanted loot and gave none end the run with the item and the reason in chat (too heavy,
// packs full, the server refusing), so no more keys go on loot that is left behind.
//
// Never spams the server: one request at a time, each waits for its UseDone (AnswerMaxMs), the
// client must be free (HoldReason) and a failed step is retried a few times at most.
// ============================================================================
using System;
using System.Text.RegularExpressions;
namespace RynthCore.Plugin.RynthAi.ItemTool;
internal interface IChestKeyEnv
{
/// <summary>Clock (ms).</summary>
long Now { get; }
string NameOf(int id);
/// <summary>The chest is in the client's object cache; meters = distance from the player (-1 unknown).</summary>
bool ChestLoaded(int chestId, out double meters);
/// <summary>
/// The key to use: <paramref name="keyId"/> while it is still carried, else another carried item
/// named <paramref name="keyName"/> (never worn, never <paramref name="excludeId"/>). 0 = none.
/// </summary>
int FindKey(int keyId, string keyName, int excludeId);
/// <summary>Why nothing may be sent now (combat, the client is busy, portal space, the server owes an answer); null = free.</summary>
string? HoldReason();
/// <summary>
/// Why no key should be spent now: the loot would have nowhere to go (the main pack is full; loot
/// goes only there). Null = room. Asked before each key use only.
/// </summary>
string? NoRoomReason();
bool HasUseDone { get; }
int UseDoneSeq();
bool LastUseDone(out int seq, out uint error);
bool SendKey(int keyId, int chestId);
bool SendOpen(int chestId);
/// <summary>When the server last opened this chest for us (its ViewContents arrived); 0 = not since the plugin started.</summary>
long ChestOpenedAt(int chestId);
/// <summary>Hands the open chest to the corpse looter (the current loot profile).</summary>
void StartLooting(int chestId);
/// <summary>The corpse looter still has the chest (asked for, or its loot session is open).</summary>
bool Looting(int chestId);
/// <summary>Ends the loot session and closes the chest.</summary>
void AbortLooting(int chestId);
/// <summary>
/// The last loot session on this chest: items the loot profile wanted (left at a keep count not
/// counted), pickups that landed, and the first wanted item's name. False = not known.
/// </summary>
bool LastLootTally(int chestId, out int wanted, out int taken, out string firstWanted);
/// <summary>The client reads the chest as locked (true), unlocked (false) or doesn't know (null).</summary>
bool? ChestLockedHint(int chestId);
/// <summary>The client shows the chest open (its ViewContents, until the server's close): true, false, or null = can't tell.</summary>
bool? ChestShownOpen(int chestId);
void Say(string text);
void Log(string text);
}
internal sealed class ChestKeyLooter
{
public enum Phase { Idle, Ready, KeyPending, KeyOut, OpenPending, OpenOut, Looting, Waiting }
// ACE WeenieError codes of the key answers (UnlockerHelper.UseUnlocker).
internal const uint ErrTooBusy = 0x1D, ErrLockAlreadyUnlocked = 0x4AF, ErrCannotLockOrUnlock = 0x4B0,
ErrCannotLockWhatIsOpen = 0x4B1, ErrKeyDoesntFit = 0x4B2, ErrCannotUseThatItem = 0x58D;
/// <summary>The chest must be loaded and no farther than this to start (the server walks the rest).</summary>
public const double MaxStartMeters = 25;
/// <summary>A step waits this long for the server's answer (the walk to the chest is part of it).</summary>
public const long AnswerMaxMs = 12_000;
/// <summary>After an answer, the chat line that goes with it gets this long to arrive.</summary>
public const long ChatSettleMs = 250;
/// <summary>After the chest's answer, it gets this long to read open before the open counts as failed.</summary>
public const long OpenSettleMs = 1500;
/// <summary>
/// "Chest re-lock wait (s)": the next cycle starts this long after the chest was opened (sooner
/// when the client sees it re-lock). The default covers ACE's default chest reset (120 s). The
/// least, 1 s (5 s until 2026-11-28), is for a chest that resets as it closes
/// (ChestRegenOnClose): ACE floors every timed reset at 15 s. A too-early key use answers
/// "already unlocked" and spends nothing, and those asks stay MinProbeMs apart and bounded
/// (MaxProbes * ProbeMs).
/// </summary>
public const int DefaultRelockWaitSec = 125, MinRelockWaitSec = 1, MaxRelockWaitSec = 600;
/// <summary>The default wait in ms (DefaultRelockWaitSec).</summary>
public const long RegenWaitMs = DefaultRelockWaitSec * 1000L;
/// <summary>A chest the client reads as locked, never read unlocked this cycle (the value may be stale), is tried again this soon after it was opened.</summary>
public const long MinRelockMs = 15_000;
/// <summary>Asking a chest that was still unlocked again: at most this long apart (ProbeEveryMs follows a shorter wait).</summary>
public const long ProbeMs = 30_000;
/// <summary>... and at least this long apart, however short the wait.</summary>
public const long MinProbeMs = 5_000;
/// <summary>After the loot, the next key goes no sooner than this after the client shows the chest closed (the server's close answered).</summary>
public const long CloseSettleMs = 300;
/// <summary>... or this long after the loot ended, where the client can't tell (or still shows it open).</summary>
public const long AfterLootMaxMs = 2000;
/// <summary>The chest is opened this long after the key's answer (the unlock's Locked=false reaches the client first).</summary>
public const long UnlockSettleMs = 400;
/// <summary>The asking lasts MaxProbes * ProbeMs (MaxProbes asks at the default wait).</summary>
public const int MaxProbes = 8;
public static int ClampRelockWaitSec(int seconds) => Math.Clamp(seconds, MinRelockWaitSec, MaxRelockWaitSec);
/// <summary>Nothing may go out for this long (combat, busy client...) before the run stops.</summary>
public const long HoldMaxMs = 90_000;
/// <summary>The loot session may take this long.</summary>
public const long LootMaxMs = 300_000;
/// <summary>The looter has this long to take the chest.</summary>
public const long LootStartMs = 8_000;
/// <summary>The chest is out of the object cache this long: it is gone.</summary>
public const long GoneMs = 5_000;
/// <summary>Chests in a row with no key use seen spent (a key that never runs out) before the run ends.</summary>
public const int MaxCycles = 300;
/// <summary>Chests in a row whose wanted loot could not be picked up before the run ends.</summary>
public const int MaxStarvedChests = 3;
public const int MaxTimeouts = 3, MaxBusy = 8, MaxOpenFails = 3, MaxSendFails = 5, MaxRefusals = 3, MaxOpenElsewhere = 3;
private const long ThinkMs = 100, RetryMs = 1500, SendRetryMs = 1000, BusyBackoffMs = 2000;
private const long NoUseDoneFallbackMs = 3000;
private static readonly Regex UnlockedText = new(@"\bhas been unlocked\b", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
private static readonly Regex KeyUsedUpText = new(@"^\s*Your key (?:is used up|has (?<n>\d+) uses? left)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
private readonly IChestKeyEnv _env;
public ChestKeyLooter(IChestKeyEnv env) => _env = env;
public int ChestId { get; private set; }
public string ChestName { get; private set; } = "";
public int KeyId { get; private set; }
public string KeyName { get; private set; } = "";
public Phase State { get; private set; } = Phase.Idle;
public bool Running => State != Phase.Idle;
/// <summary>Chests looted since the run started.</summary>
public int Looted { get; private set; }
/// <summary>The key's uses left, as the server last said (-1 unknown).</summary>
public int UsesLeft { get; private set; } = -1;
/// <summary>What the run did last, for the status line when it is not running.</summary>
public string LastResult { get; private set; } = "";
/// <summary>
/// This run saw the chest re-lock (unlocked after the key, locked again once closed), so the
/// re-lock wait isn't what times it: the panel hides the wait's control and the status shows no
/// countdown (2026-11-28: the wait's timer bar showed although the looter no longer went by it).
/// </summary>
public bool RelockSeen { get; private set; }
/// <summary>"Chest re-lock wait (s)" (the host sets it from the settings each tick; clamped). A change applies to a wait already running.</summary>
public int RelockWaitSec
{
get => _relockWaitSec;
set => _relockWaitSec = ClampRelockWaitSec(value);
}
private int _relockWaitSec = DefaultRelockWaitSec;
private long RelockWaitMs => _relockWaitSec * 1000L;
/// <summary>How far apart a chest that was still unlocked is asked again: the wait, 5..30 s.</summary>
internal long ProbeEveryMs => Math.Clamp(RelockWaitMs, MinProbeMs, ProbeMs);
/// <summary>How many asks fit in MaxProbes * ProbeMs (MaxProbes at the default wait).</summary>
internal int ProbeLimit => (int)((MaxProbes * ProbeMs + ProbeEveryMs - 1) / ProbeEveryMs);
// Run state.
private long _lastThink, _nextTryAt, _goneSince, _holdSince;
private long _keySentAt, _openSentAt, _answeredAt, _lootStartedAt, _waitUntil, _openedAt, _lootDoneAt;
private int _seqAtSend;
private bool _answerSeen, _sawUnlocked, _keyUsedUp, _lootSeen, _relockExpected, _regenWait;
private int _timeouts, _busy, _openFails, _sendFails, _probes, _refusals, _openElsewhere, _cycles, _keyUsesThisCycle;
private string _holdWhy = "";
// A key use seen spent this cycle; chests in a row without one (MaxCycles).
private bool _spentThisCycle;
private int _unspentCycles;
// The client read the chest unlocked after this cycle's key (a later Locked=true is the re-lock).
private bool _hintSawUnlocked;
// When the client was first seen with the chest closed after the loot (0 = not yet).
private long _closedSeenAt;
// Chests in a row with wanted loot and nothing taken (MaxStarvedChests).
private int _starved;
public void SetChest(int id, string name) { if (!Running) { ChestId = id; ChestName = name; } }
public void SetKey(int id, string name) { if (!Running) { KeyId = id; KeyName = name; UsesLeft = -1; } }
public void Clear() { if (!Running) { ChestId = 0; ChestName = ""; KeyId = 0; KeyName = ""; UsesLeft = -1; LastResult = ""; } }
/// <summary>Begins the run. False (with why, already said in chat) when it can't.</summary>
public bool Start(out string why)
{
why = "";
if (Running) { why = "the chest looter is already running"; return false; }
if (ChestId == 0) { why = "pick a chest first (select it in the world, then 'Use selected as chest')"; return Refuse(why); }
if (KeyId == 0) { why = "pick a key first (select it in your pack, then 'Use selected as key')"; return Refuse(why); }
if (!_env.ChestLoaded(ChestId, out double meters)) { why = $"{ChestName} isn't loaded (out of view?); walk closer"; return Refuse(why); }
if (meters > MaxStartMeters) { why = $"{ChestName} is {meters:0} m away (more than {MaxStartMeters:0} m); walk closer"; return Refuse(why); }
int key = _env.FindKey(KeyId, KeyName, 0);
if (key == 0) { why = $"no '{KeyName}' in your packs"; return Refuse(why); }
KeyId = key;
Looted = 0; _cycles = 0; _probes = 0; _relockExpected = false; _goneSince = 0; _holdSince = 0;
_timeouts = _busy = _openFails = _sendFails = _refusals = _openElsewhere = 0;
_unspentCycles = 0; _starved = 0; _spentThisCycle = false; _hintSawUnlocked = false; _closedSeenAt = 0;
RelockSeen = false;
_lastThink = 0; _nextTryAt = 0; LastResult = "";
State = Phase.Ready;
_env.Say($"[RynthAi] Chest looter: starting on {ChestName} with {KeyName}. It uses the key, opens the chest, loots it with the current loot profile and repeats until the key is used up. Stop: /ra itemtool chest stop.");
_env.Log($"[RynthAi] Chest looter: start chest 0x{(uint)ChestId:X8} '{ChestName}' key 0x{(uint)KeyId:X8} '{KeyName}' ({meters:0.0} m)");
return true;
}
private bool Refuse(string why)
{
_env.Say($"[RynthAi] Chest looter: can't start - {why}.");
return false;
}
/// <summary>The player asked to stop (always works).</summary>
public void Stop(string why = "stopped")
{
if (!Running) { _env.Say("[RynthAi] Chest looter: not running."); return; }
Finish(why);
}
private void Finish(string why)
{
Phase was = State;
State = Phase.Idle;
if (was == Phase.Looting && ChestId != 0)
{
try { _env.AbortLooting(ChestId); } catch { /* the stop must not throw */ }
}
LastResult = $"stopped: {why} ({Looted} chest(s) looted)";
_env.Say($"[RynthAi] Chest looter {LastResult}.");
_env.Log($"[RynthAi] Chest looter: finish - {why}; looted {Looted}, cycles {_cycles}");
}
/// <summary>A chat line from the server (the key's answers).</summary>
public void OnChat(string? text)
{
if (!Running || string.IsNullOrEmpty(text) || text.Length > 320) return;
if (State is not (Phase.KeyOut or Phase.KeyPending)) return;
// ACE: "The X has been unlocked.\nYour key has N uses left." comes as one message.
foreach (string line in text.Split('\n'))
{
if (line.Length == 0 || line.Length > 160) continue;
if (UnlockedText.IsMatch(line)) { _sawUnlocked = true; continue; }
Match m = KeyUsedUpText.Match(line);
if (!m.Success) continue;
_spentThisCycle = true;
if (m.Groups["n"].Success && int.TryParse(m.Groups["n"].Value, out int n)) UsesLeft = n;
else { _keyUsedUp = true; UsesLeft = 0; }
}
}
/// <summary>One line for the panel.</summary>
public string Status
{
get
{
long now = _env.Now;
string key = KeyName.Length == 0 ? "no key" : UsesLeft >= 0 ? $"{KeyName} ({UsesLeft} use{(UsesLeft == 1 ? "" : "s")} left)" : KeyName;
string tail = $"{Looted} looted";
if (_holdWhy.Length > 0 && State is Phase.KeyPending or Phase.OpenPending)
return $"Waiting: {_holdWhy} - {tail}.";
switch (State)
{
case Phase.Idle: return LastResult.Length > 0 ? $"Chest looter {LastResult}." : "Idle.";
case Phase.Waiting:
return RelockSeen && _probes == 0
? $"Waiting for {ChestName} to re-lock (it re-locks as it closes) - {key}, {tail}."
: $"Waiting for {ChestName} to re-lock ({Math.Max(0, (_waitUntil - now + 999) / 1000)} s) - {key}, {tail}.";
case Phase.Looting: return $"Looting {ChestName} - {key}, {tail}.";
case Phase.KeyPending or Phase.KeyOut: return $"Using {KeyName} on {ChestName} - {tail}.";
case Phase.OpenPending or Phase.OpenOut: return $"Opening {ChestName} - {key}, {tail}.";
default: return $"Starting - {tail}.";
}
}
}
/// <summary>Call every plugin tick.</summary>
public void Tick()
{
if (State == Phase.Idle) return;
long now = _env.Now;
if (now - _lastThink < ThinkMs) return;
_lastThink = now;
if (!_env.ChestLoaded(ChestId, out double meters))
{
if (_goneSince == 0) _goneSince = now;
else if (now - _goneSince >= GoneMs) Finish($"{ChestName} is gone (out of view, or you left)");
return;
}
_goneSince = 0;
// From the open on, an unlocked read is this cycle's unlock (the server's Locked=false), so a
// locked read after it is the re-lock (TickWaiting).
if (State is Phase.OpenPending or Phase.OpenOut or Phase.Looting or Phase.Waiting
&& _env.ChestLockedHint(ChestId) == false)
_hintSawUnlocked = true;
switch (State)
{
case Phase.Ready: TickReady(now, meters); break;
case Phase.KeyPending: TickKeyPending(now); break;
case Phase.KeyOut: TickKeyOut(now); break;
case Phase.OpenPending: TickOpenPending(now); break;
case Phase.OpenOut: TickOpenOut(now); break;
case Phase.Looting: TickLooting(now); break;
case Phase.Waiting: TickWaiting(now); break;
}
}
// ── Phases ───────────────────────────────────────────────────────────────
private void TickReady(long now, double meters)
{
if (_unspentCycles >= MaxCycles) { Finish($"{MaxCycles} chests in a row without a key use spent (a key with unlimited uses never runs out)"); return; }
int key = _env.FindKey(KeyId, KeyName, 0);
if (key == 0) { Finish($"out of keys - no '{KeyName}' left"); return; }
if (key != KeyId)
{
_env.Log($"[RynthAi] Chest looter: key 0x{(uint)KeyId:X8} is gone; using 0x{(uint)key:X8} '{KeyName}'");
KeyId = key;
UsesLeft = -1;
_unspentCycles = 0; // the last key was used up: keys are being spent
}
if (meters > MaxStartMeters) { Finish($"{ChestName} is {meters:0} m away (more than {MaxStartMeters:0} m)"); return; }
_keyUsesThisCycle = 0;
_openFails = 0;
_sawUnlocked = false;
_keyUsedUp = false;
_spentThisCycle = false;
_hintSawUnlocked = false;
State = Phase.KeyPending;
}
/// <summary>True while something holds the send; the run stops after HoldMaxMs of it.</summary>
private bool Held(long now)
{
string? why = _env.HoldReason();
if (why == null) { _holdSince = 0; _holdWhy = ""; return false; }
_holdWhy = why;
if (_holdSince == 0) { _holdSince = now; _env.Log($"[RynthAi] Chest looter: waits - {why}"); }
else if (now - _holdSince >= HoldMaxMs) Finish($"waited {HoldMaxMs / 1000} s: {why}");
return true;
}
private bool Answered(long sentAt, out uint error)
{
error = 0;
long now = _env.Now;
if (!_env.HasUseDone) return now - sentAt >= NoUseDoneFallbackMs;
if (_env.UseDoneSeq() == _seqAtSend) return false;
if (_env.LastUseDone(out int seq, out uint err) && seq == _seqAtSend + 1) error = err;
return true;
}
// ── No room for the loot (2026-11-24, #rynth-issues "Inventory Autocram does not actively run while
// looting chests with ItemTool": "when the pack is full it keeps unlocking chests and using keys but
// fails to loot them ... continues to burn keys anyway") ──
// Before each key use the looter asks whether the loot has anywhere to go. With the main pack full it
// waits (Auto Cram, which runs during a chest run, may make room), and after NoRoomMaxMs of it the
// run ends: no key is spent on a chest whose loot would only be left behind.
public const long NoRoomMaxMs = 30_000;
private long _noRoomSince;
/// <summary>True while the loot has nowhere to go; ends the run after NoRoomMaxMs of it.</summary>
private bool NoRoom(long now)
{
string? why = _env.NoRoomReason();
if (why == null) { _noRoomSince = 0; return false; }
_holdWhy = why;
if (_noRoomSince == 0) { _noRoomSince = now; _env.Log($"[RynthAi] Chest looter: waits before the next key - {why}"); }
else if (now - _noRoomSince >= NoRoomMaxMs) Finish($"stopped before the next key: {why} (no key spent on a chest whose loot would be left)");
return true;
}
private void TickKeyPending(long now)
{
if (now < _nextTryAt) return;
if (Held(now)) return;
if (_keyUsesThisCycle == 0 && NoRoom(now)) return;
int key = _env.FindKey(KeyId, KeyName, _keyUsedUp ? KeyId : 0);
if (key == 0) { Finish($"out of keys - no '{KeyName}' left"); return; }
KeyId = key;
_keyUsedUp = false;
_seqAtSend = _env.UseDoneSeq();
_sawUnlocked = false;
if (!_env.SendKey(KeyId, ChestId))
{
if (++_sendFails >= MaxSendFails) Finish("the client wouldn't send the key use");
else _nextTryAt = now + SendRetryMs;
return;
}
_sendFails = 0;
_keySentAt = now;
_answerSeen = false;
State = Phase.KeyOut;
}
private void TickKeyOut(long now)
{
if (!_answerSeen)
{
if (!Answered(_keySentAt, out uint e0))
{
if (now - _keySentAt >= AnswerMaxMs) Retry(now, Phase.KeyPending, "no answer from the server to the key use");
return;
}
_answerSeen = true;
_answeredAt = now;
_lastErr = e0;
return; // the chat line of this answer gets ChatSettleMs
}
if (now - _answeredAt < ChatSettleMs) return;
_answerSeen = false;
OnKeyAnswer(now, _lastErr);
}
private uint _lastErr;
private void OnKeyAnswer(long now, uint err)
{
switch (err)
{
case 0:
_keyUsesThisCycle++;
_busy = 0;
if (!_sawUnlocked)
_env.Log("[RynthAi] Chest looter: the key use was answered with no 'has been unlocked' line; trying the chest anyway");
State = Phase.OpenPending;
// Counted from the answer (the chat settle is part of it), not from now.
_nextTryAt = Math.Max(now, _answeredAt + UnlockSettleMs);
return;
case ErrLockAlreadyUnlocked:
// Nothing was spent. First time: the chest simply isn't locked - open it. After a
// looted chest: it has not re-locked (and regenerated) yet - ask again later.
if (!_relockExpected)
{
State = Phase.OpenPending;
_nextTryAt = Math.Max(now, _answeredAt + UnlockSettleMs);
return;
}
int limit = ProbeLimit;
if (++_probes > limit)
{
Finish($"{ChestName} hasn't re-locked after {limit} tries ({Math.Max(1, (MaxProbes * ProbeMs + RelockWaitMs) / 60000)} min): its reset timer may be longer, or it doesn't lock");
return;
}
_env.Log($"[RynthAi] Chest looter: {ChestName} is still unlocked (not reset yet); asking again in {ProbeEveryMs / 1000} s (try {_probes} of {limit})");
EnterWaiting(now + ProbeEveryMs);
return;
case ErrKeyDoesntFit:
Finish($"that key doesn't fit {ChestName}");
return;
case ErrCannotLockOrUnlock:
Finish($"the server says {ChestName} can't be unlocked with a key");
return;
case ErrCannotLockWhatIsOpen:
if (++_openElsewhere >= MaxOpenElsewhere) { Finish($"{ChestName} stays open (someone else has it?)"); return; }
_env.Log($"[RynthAi] Chest looter: {ChestName} is open; trying again in 5 s");
EnterWaiting(now + 5000);
return;
case ErrCannotUseThatItem:
// A key with no uses left: ACE deletes it.
_keyUsedUp = true;
_spentThisCycle = true;
_env.Log("[RynthAi] Chest looter: the server says the key can't be used (spent); looking for another");
State = Phase.KeyPending;
_nextTryAt = now + SendRetryMs;
return;
case ErrTooBusy:
if (++_busy >= MaxBusy) { Finish("the server keeps answering 'too busy'"); return; }
_nextTryAt = now + BusyBackoffMs;
State = Phase.KeyPending;
return;
default:
if (++_refusals >= MaxRefusals) { Finish($"the server refused the key use (0x{err:X})"); return; }
_env.Log($"[RynthAi] Chest looter: the server answered the key use with error 0x{err:X}; trying again");
_nextTryAt = now + RetryMs;
State = Phase.KeyPending;
return;
}
}
private void Retry(long now, Phase back, string why)
{
if (++_timeouts >= MaxTimeouts) { Finish(why); return; }
_env.Log($"[RynthAi] Chest looter: {why}; trying again ({_timeouts} of {MaxTimeouts})");
State = back;
_nextTryAt = now + RetryMs;
}
private void TickOpenPending(long now)
{
if (now < _nextTryAt) return;
// Another opener already put it in front of us (the player, or the looter's own take).
if (_env.ChestOpenedAt(ChestId) > _keySentAt) { OnOpened(now); return; }
if (Held(now)) return;
_seqAtSend = _env.UseDoneSeq();
if (!_env.SendOpen(ChestId))
{
if (++_sendFails >= MaxSendFails) Finish("the client wouldn't send the open");
else _nextTryAt = now + SendRetryMs;
return;
}
_sendFails = 0;
_openSentAt = now;
_answerSeen = false;
State = Phase.OpenOut;
}
private void TickOpenOut(long now)
{
if (_env.ChestOpenedAt(ChestId) >= _openSentAt) { OnOpened(now); return; }
if (!_answerSeen && Answered(_openSentAt, out _)) { _answerSeen = true; _answeredAt = now; }
bool failed = _answerSeen ? now - _answeredAt >= OpenSettleMs : now - _openSentAt >= AnswerMaxMs;
if (!failed) return;
if (++_openFails >= MaxOpenFails)
{
Finish($"{ChestName} wouldn't open ({MaxOpenFails} tries: still locked, or someone else is using it)");
return;
}
_env.Log($"[RynthAi] Chest looter: {ChestName} didn't open (try {_openFails} of {MaxOpenFails})");
State = _keyUsesThisCycle == 0 || !_sawUnlocked ? Phase.KeyPending : Phase.OpenPending;
_nextTryAt = now + 2000;
}
private void OnOpened(long now)
{
_openedAt = _env.ChestOpenedAt(ChestId);
if (_openedAt == 0) _openedAt = now;
_env.StartLooting(ChestId);
_lootSeen = _env.Looting(ChestId);
_lootStartedAt = now;
_timeouts = 0;
State = Phase.Looting;
}
private void TickLooting(long now)
{
if (_env.Looting(ChestId))
{
_lootSeen = true;
if (now - _lootStartedAt >= LootMaxMs) Finish($"looting {ChestName} took more than {LootMaxMs / 60000} minutes");
return;
}
if (!_lootSeen && now - _lootStartedAt < LootStartMs) return;
if (!_lootSeen) { Finish($"the loot window never took {ChestName}"); return; }
// The loot session ended and the chest is closed.
Looted++;
_cycles++;
_probes = 0;
_openElsewhere = 0; // "stays open" means that many in a row, not in the whole run
_relockExpected = true;
if (_spentThisCycle || _keyUsedUp) _unspentCycles = 0;
else _unspentCycles++;
// What the loot profile wanted from this chest, and what landed.
string tally = "";
if (_env.LastLootTally(ChestId, out int wanted, out int taken, out string firstWanted))
{
tally = wanted > 0 ? $" - took {taken} of {wanted} wanted" : " - nothing the profile wants";
if (wanted > 0 && taken == 0)
{
_starved++;
_env.Log($"[RynthAi] Chest looter: {ChestName}: the profile wanted {wanted} item(s) (first '{firstWanted}') and none was picked up ({_starved} of {MaxStarvedChests} in a row)");
if (_starved >= MaxStarvedChests)
{
_env.Say($"[RynthAi] Chest looter: looted {ChestName} ({Looted} so far){tally}.");
Finish($"{MaxStarvedChests} chests in a row had loot your profile wants (e.g. '{firstWanted}') and none of it could be picked up - too heavy, no pack room, or the server refused it (see the loot lines in chat); no more keys spent");
return;
}
}
else _starved = 0;
}
_env.Say($"[RynthAi] Chest looter: looted {ChestName} ({Looted} so far){tally}.");
int key = _env.FindKey(KeyId, KeyName, _keyUsedUp ? KeyId : 0);
if (key == 0) { Finish($"out of keys after {Looted} chest(s)"); return; }
if (key != KeyId) _unspentCycles = 0; // the key went: it was used up
if (_unspentCycles >= MaxCycles) { Finish($"{MaxCycles} chests in a row without a key use spent (a key with unlimited uses never runs out)"); return; }
KeyId = key;
_keyUsedUp = false; // that was the old key; the one chosen now has not been used
_lootDoneAt = now;
_closedSeenAt = _env.ChestShownOpen(ChestId) == false ? now : 0;
EnterWaiting(RegenWaitUntil());
_regenWait = true;
}
/// <summary>
/// The re-lock wait's end: RelockWaitSec after the open, and never before the chest is closed:
/// CloseSettleMs after the client showed it closed, or AfterLootMaxMs after the loot ended when
/// that isn't seen (an engine that can't tell, or a close that isn't answered).
/// </summary>
private long RegenWaitUntil()
{
long closed = _closedSeenAt != 0
? Math.Min(_closedSeenAt + CloseSettleMs, _lootDoneAt + AfterLootMaxMs)
: _lootDoneAt + AfterLootMaxMs;
return Math.Max(closed, _openedAt + RelockWaitMs);
}
/// <summary>The loot is over and the client shows the chest closed (or can't tell, AfterLootMaxMs on): a key may go on it.</summary>
private bool ClosedAfterLoot(long now)
{
if (_closedSeenAt != 0) return now >= _closedSeenAt + CloseSettleMs;
return now >= _lootDoneAt + AfterLootMaxMs;
}
private void EnterWaiting(long until)
{
_waitUntil = until;
_regenWait = false;
State = Phase.Waiting;
}
private void TickWaiting(long now)
{
// The key may leave the pack meanwhile (traded, dropped): nothing to wait for then.
if (_env.FindKey(KeyId, KeyName, _keyUsedUp ? KeyId : 0) == 0) { Finish($"out of keys - no '{KeyName}' left"); return; }
if (_closedSeenAt == 0 && _env.ChestShownOpen(ChestId) == false) _closedSeenAt = now;
if (_regenWait) _waitUntil = RegenWaitUntil(); // the setting (or the close) may have changed meanwhile
if (now >= _waitUntil) { State = Phase.Ready; return; }
bool? locked = _env.ChestLockedHint(ChestId);
if (locked != true) return;
// Read unlocked after this cycle's key, now locked: the server re-locked (reset) it.
if (_hintSawUnlocked && ClosedAfterLoot(now))
{
if (!RelockSeen) _env.Say($"[RynthAi] Chest looter: {ChestName} re-locks as it closes; the next key goes on it as soon as it does (the re-lock wait isn't needed for it).");
RelockSeen = true;
_env.Log($"[RynthAi] Chest looter: {ChestName} re-locked {Math.Max(0, now - _openedAt) / 1000.0:0.0} s after the open; next cycle");
State = Phase.Ready;
return;
}
// A locked read never preceded by an unlocked one may be stale: only past ACE's timer floor.
if (_probes == 0 && now >= _openedAt + MinRelockMs)
{
_env.Log($"[RynthAi] Chest looter: {ChestName} reads locked again; next cycle");
State = Phase.Ready;
}
}
}