RynthSuite/Plugins/RynthCore.Plugin.RynthAi/RynthAiPlugin.DeathWait.cs

256 lines
14 KiB
C#

// RynthAiPlugin.DeathWait.cs - the death animation, learned per monster and waited out (2026-11-08).
//
// Tom: "the bot is running away from a monster it's killed and then turning around and running back
// because there is a second or two for the corpse to show up due to a death animation", and "with
// looting on I'd like it to wait that duration before running away, and maybe an extra second to let
// the corpse spawn and be looted. As soon as the corpse is looted the bot can run away, if no corpse
// spawns it needs to continue."
//
// Learning: ACE creates the corpse once the death animation has played, so the time from our kill
// notice to its "Corpse of <name>" object (CorpseTimer) is that monster's death. It goes to the
// session's monster stats ("cd", "cn") and to the learned table (MonsterXpTable, kept across logins).
//
// The wait: with the macro and Looting on, our kill starts a wait of the monster's learned death (or
// DeathWaitDefaultMs while it isn't learned) plus DeathWaitExtraMs. While it runs, looting counts as
// work and the arbiter gives Looting the tick (ArbiterInputs.DeathWait, as Boost Loot does), so
// neither nav nor the Outdoor Hunt takes the character away. The next fight is not held (2026-11-15,
// below): a monster combat can lock is fought at once. It ends when the corpse is looted, when no
// corpse came in the time, or DeathWaitDecideMs after a corpse came that the looter hasn't claimed
// (the loot rules don't take it, or the looter leaves it for the fight: then the fight goes on and the
// looter comes back for it; holding combat there stood the character still, both waiting on the other).
//
// No hold on combat's next target (2026-11-15, R-20261115-165857-1de7): the same stand-off, before the
// corpse came. The wait held the next target (a Marae Ursuin in range, beyond 5 yd) for the 3 s death
// and 1 s more, but the looter claims no corpse while combat has targets in range (Boost Loot and a
// starved corpse aside, which have their own rules): "Looting has held the tick 3.0 s with nothing
// claimed", 3.6 s standing until the monster walked within 5 yd and hit. Combat has a candidate only
// when it has targets, so the hold could only ever wait on a looter that waits for the fight. The corpse
// is looted after the fight, as every corpse is with Boost Loot off; the wait still keeps nav and the
// hunt from walking off when no fight is left (the owner's case: running off from the last kill).
//
// No corpse here (2026-11-20): "in threads it shouldn't wait for the death animation since mobs don't have
// corpses, it needs to move as fast as possible". DreamWeave's Thread monsters leave no corpse, so every kill there
// stood the character still for the whole wait ("no corpse came in 3.5 s, carrying on"). Learned in the session,
// per dungeon landblock and per monster: after NoCorpseSkipAfter kills in a row with no corpse, a kill there (or of
// that monster anywhere) starts no wait. A corpse of a kill near the character puts its landblock and its monster
// back to waiting.
// Pump thread throughout.
using System;
using System.Collections.Generic;
using RynthCore.Plugin.RynthAi.LegacyUi;
using RynthCore.Plugin.RynthAi.Loot;
using RynthCore.Plugin.RynthAi.MonsterStats;
namespace RynthCore.Plugin.RynthAi;
public sealed partial class RynthAiPlugin
{
/// <summary>A monster's death while it isn't learned yet (most AC deaths take 1.5-3 s).</summary>
internal const long DeathWaitDefaultMs = 2500;
/// <summary>Waited past the death, for the corpse to come and be seen.</summary>
internal const long DeathWaitExtraMs = 1000;
/// <summary>After the corpse came: this long for the loot rules to say whether it is looted (its ID).</summary>
internal const long DeathWaitDecideMs = 1000;
/// <summary>A corpse being looted holds the wait at most this long past it (never a wedge).</summary>
internal const long DeathWaitLootMaxMs = 20000;
/// <summary>A learned death longer than this is no death animation (the corpse came late for some other reason).</summary>
internal const long DeathWaitMaxDeathMs = 8000;
/// <summary>A corpse farther than this from the character isn't its kill's.</summary>
private const double DeathCorpseNearYd = 80;
private readonly CorpseTimer _corpseTimer = new();
private long _deathWaitAt; // CorpseNowMs of the kill being waited for; 0 = none
private long _deathWaitMs; // how long the wait is
private string _deathWaitName = "";
private int _deathCorpseId; // its corpse, once it came
private long _deathCorpseAt;
private bool _deathWaitHeld; // HasLootWork's answer this tick (the arbiter's DeathWait input)
private uint _deathWaitLandblock; // the dungeon landblock of the kill being waited for (0 outdoors)
/// <summary>Kills in a row with no corpse after which a landblock or a monster gets no corpse wait.</summary>
internal const int NoCorpseSkipAfter = 2;
private readonly Dictionary<uint, int> _noCorpseByLandblock = new();
private readonly Dictionary<string, int> _noCorpseByName = new(StringComparer.OrdinalIgnoreCase);
/// <summary>The kills here (a dungeon landblock), or of this monster, have left no corpse lately: no wait.</summary>
private bool NoCorpseExpected(uint landblock, string name, out string because)
{
if (landblock != 0 && _noCorpseByLandblock.TryGetValue(landblock, out int n) && n >= NoCorpseSkipAfter)
{
because = $"the last {n} kills in landblock 0x{landblock:X4} left no corpse";
return true;
}
if (name.Length > 0 && _noCorpseByName.TryGetValue(name, out int m) && m >= NoCorpseSkipAfter)
{
because = $"its last {m} kills left no corpse";
return true;
}
because = "";
return false;
}
/// <summary>The waited-for kill left no corpse: counted for its landblock and its monster (one line when either stops waiting).</summary>
private void NoteNoCorpse()
{
if (_deathWaitLandblock != 0)
{
int n = _noCorpseByLandblock.TryGetValue(_deathWaitLandblock, out int was) ? was + 1 : 1;
_noCorpseByLandblock[_deathWaitLandblock] = n;
if (n == NoCorpseSkipAfter)
Host.Log($"[RynthAi] Kill: {n} kills in a row in landblock 0x{_deathWaitLandblock:X4} left no corpse: no corpse wait here until one does.");
}
if (_deathWaitName.Length > 0)
{
int m = _noCorpseByName.TryGetValue(_deathWaitName, out int was) ? was + 1 : 1;
_noCorpseByName[_deathWaitName] = m;
if (m == NoCorpseSkipAfter)
Host.Log($"[RynthAi] Kill: '{_deathWaitName}' left no corpse {m} times in a row: no corpse wait for it until one comes.");
}
}
/// <summary>The death wait can run: the macro and Looting on, and the looter able to loot now.</summary>
private bool DeathWaitCanRun(LegacyUiSettings settings)
=> settings.IsMacroRunning && settings.EnableLooting
&& !BoostNavHoldsLoot(settings)
&& !settings.LootOnlyRareCorpses // rare-only: most corpses have nothing to wait for
&& !LootModes.IsRares(settings.LootMode)
&& !IsLootingBlockedByPack(settings)
&& !_useRefusals.HoldsOpens(CorpseNowMs)
&& GetCorpseApproachRangeMaxMeters(settings) > 0.25;
/// <summary>Our kill of <paramref name="name"/> (the kill notice's monster, null when none was named): its corpse is on its way.</summary>
private void StartDeathWait(string? name)
{
var s = _dashboard?.Settings;
if (s == null || !DeathWaitCanRun(s)) return;
uint landblock = CurrentLandblockOrZero();
string trimmed = name?.Trim() ?? "";
if (NoCorpseExpected(landblock, trimmed, out string because))
{
Host.Log($"[RynthAi] Kill: no wait for the corpse of {(trimmed.Length > 0 ? $"'{trimmed}'" : "the kill")}: {because}.");
return;
}
_deathWaitLandblock = landblock;
double? learned = string.IsNullOrWhiteSpace(name) ? null : _xpTable.DeathMs(name);
long death = learned is double d ? (long)Math.Round(Math.Clamp(d, 0, DeathWaitMaxDeathMs)) : DeathWaitDefaultMs;
_deathWaitMs = death + DeathWaitExtraMs;
_deathWaitAt = CorpseNowMs;
_deathWaitName = name?.Trim() ?? "";
_deathCorpseId = 0;
_deathCorpseAt = 0;
string who = _deathWaitName.Length > 0 ? $"'{_deathWaitName}'" : "the kill";
string why = learned != null
? $"its death takes {death / 1000.0:0.0} s, learned over {_xpTable.DeathsTimed(name)} {(_xpTable.DeathsTimed(name) == 1 ? "kill" : "kills")}"
: $"its death isn't learned yet: {DeathWaitDefaultMs / 1000.0:0.0} s";
Host.Log($"[RynthAi] Kill: waiting up to {_deathWaitMs / 1000.0:0.0} s for the corpse of {who} ({why}, and {DeathWaitExtraMs / 1000.0:0.0} s more) before moving on.");
}
/// <summary>The wait is over: says why, once.</summary>
private void EndDeathWait(string why)
{
if (_deathWaitAt == 0) return;
long took = CorpseNowMs - _deathWaitAt;
string who = _deathWaitName.Length > 0 ? $"'{_deathWaitName}'" : "the kill";
Host.Log($"[RynthAi] Kill: done waiting for the corpse of {who} after {took / 1000.0:0.0} s: {why}.");
_deathWaitAt = 0;
_deathCorpseId = 0;
_deathCorpseAt = 0;
_deathWaitName = "";
_deathWaitHeld = false;
}
/// <summary>
/// The wait holds the character now (and ends it, with its line, when it is over). Before the
/// corpse: until the wait's time is up. With the corpse: while it is claimed, open or lootable in
/// range (up to DeathWaitLootMaxMs), else DeathWaitDecideMs for the loot rules' look.
/// </summary>
private bool DeathWaitHolds(LegacyUiSettings settings)
{
if (_deathWaitAt == 0) return false;
if (!DeathWaitCanRun(settings)) { EndDeathWait("looting stopped or can't loot now"); return false; }
long now = CorpseNowMs, since = now - _deathWaitAt;
if (_deathCorpseId == 0)
{
if (since < _deathWaitMs) return true;
NoteNoCorpse();
EndDeathWait($"no corpse came in {_deathWaitMs / 1000.0:0.0} s, carrying on");
return false;
}
if (_completedCorpses.ContainsKey(_deathCorpseId)) { EndDeathWait("its corpse is looted"); return false; }
// Only a corpse the looter has claimed or opened holds the character. One that is merely
// lootable in range while the looter defers to the fight ("waits for combat") must not: the
// wait would hold combat's next target while looting waits for combat, and the character
// stood still until the looter's own 8 s rule (2026-11-08: 16 s for a 1.2 s death).
bool looting = _targetCorpseId == _deathCorpseId || _openedContainerId == _deathCorpseId;
if (looting)
{
if (since < _deathWaitMs + DeathWaitLootMaxMs) return true;
EndDeathWait("looting its corpse is taking too long, carrying on");
return false;
}
if (now - _deathCorpseAt < DeathWaitDecideMs) return true;
EndDeathWait(_lootableInRange.Contains(_deathCorpseId)
? "its corpse came but the looter leaves it for the fight (it comes back for it), carrying on"
: "its corpse came but the loot rules don't take it, carrying on");
return false;
}
/// <summary>A corpse was looted (MarkCorpseCompleted): when it is the kill's (or the first seen since the kill), the wait is over.</summary>
private void EndDeathWaitFor(int corpseId)
{
if (_deathWaitAt == 0) return;
bool kills = _deathCorpseId != 0
? corpseId == _deathCorpseId
: !_corpseWaitingSince.TryGetValue(corpseId, out long seen) || seen >= _deathWaitAt;
if (kills) EndDeathWait("its corpse is looted");
}
/// <summary>
/// OnCreateObject: a "Corpse of X" near the character. Times X's death (CorpseTimer: the session's
/// stats and the learned table), and is the waited-for kill's corpse when the names match.
/// </summary>
private void NoteCorpseCreated(uint objectId)
{
if (_corpseTimer.Waiting == 0 && _deathWaitAt == 0) return;
if (!Host.HasGetObjectName || !Host.TryGetObjectName(objectId, out string name)) return;
string? killed = CorpseTimer.KilledName(name);
if (killed == null || !CorpseNearMe(objectId)) return;
// Corpses come here after all: this landblock and this monster wait for them again.
uint landblock = CurrentLandblockOrZero();
if (landblock != 0) _noCorpseByLandblock.Remove(landblock);
_noCorpseByName.Remove(killed);
if (_corpseTimer.OnCorpse(name, CorpseNowMs) is var (who, ms) && ms <= DeathWaitMaxDeathMs)
{
_monsterStats.OnCorpseTime(who, ms, DateTime.UtcNow);
_xpTable.RecordDeath(who, ms);
}
if (_deathWaitAt != 0 && _deathCorpseId == 0
&& (_deathWaitName.Length == 0 || killed.Equals(_deathWaitName, StringComparison.OrdinalIgnoreCase)))
{
_deathCorpseId = unchecked((int)objectId);
_deathCorpseAt = CorpseNowMs;
}
}
/// <summary>Within DeathCorpseNearYd of the character (true when either position can't be read).</summary>
private bool CorpseNearMe(uint objectId)
{
if (!Host.HasGetObjectPosition || !Host.HasGetPlayerPose) return true;
if (!Host.TryGetObjectPosition(objectId, out uint cell, out float x, out float y, out _) || cell == 0) return true;
if (!Host.TryGetPlayerPose(out uint me, out float mx, out float my, out _, out _, out _, out _, out _) || me == 0) return true;
double ax = ((cell >> 24) & 0xFF) * 192.0 + x, ay = ((cell >> 16) & 0xFF) * 192.0 + y;
double bx = ((me >> 24) & 0xFF) * 192.0 + mx, by = ((me >> 16) & 0xFF) * 192.0 + my;
// A dungeon's cells share one landblock origin: the local positions compare directly there too.
double dx = ax - bx, dy = ay - by;
return Math.Sqrt(dx * dx + dy * dy) <= DeathCorpseNearYd;
}
/// <summary>
/// CombatManager.HoldNewTarget: Boost Loot Priority's hold. The death wait no longer holds the next
/// target (2026-11-15, header): with a target to lock, the looter leaves the corpse for the fight.
/// </summary>
private bool LootHoldsNewTarget() => BoostLootHoldsNewTarget();
/// <summary>CombatManager.HoldNewTargetWhy: the words for its one log line.</summary>
private string LootHoldWhy() => "Boost Loot Priority loots the kill first";
}