RynthSuite/Tools/RynthCore.RynthAiHostTests/Tests/LootCorpseTests.cs

705 lines
36 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using RynthCore.Loot;
using RynthCore.Plugin.RynthAi;
using RynthCore.Plugin.RynthAi.LegacyUi;
using RynthCore.Plugin.Shared;
using RynthCore.PluginCore;
using RynthCore.PluginSdk;
using RynthCore.RynthAiHostTests.Fakes;
using RynthCore.RynthAiTests.Harness;
namespace RynthCore.RynthAiHostTests.Tests;
// 2026-10-03, Dargoth Hera on a busy server: RynthAi never looted his own kills, in "My Kills
// Only" and in "Loot All". Two causes:
// 1. A fresh corpse whose name and type (Container) were in the engine's identity snapshot
// before its position was got filed as an inventory Container ("nopos-item") and was never
// looked at again, so it was never a loot candidate. Your own kills hit it most: they are
// created right next to you and classified at once.
// 2. Combat outranks looting, and a busy spawn always has a monster inside MonsterRange, so
// a corpse waited until the area emptied. A lootable corpse in range that has waited
// LootStarveMs now gets a turn while no monster is within LootStarveCloseYards.
internal static class LootCorpseTests
{
private const uint Player = 0x50000D01;
private const uint Corpse = 0x80261100;
private const uint Gem = 0x80261101;
private const uint Monster = 0x80246096;
private const uint ItemTypeContainer = 0x200, ItemTypeGem = 0x800, ItemTypeCreature = 0x10;
private const uint BfCorpse = 0x2000;
private const uint LongDescKey = 16;
private const string Me = "Dargoth Hera";
private const BindingFlags Any = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
public static void Register(Runner r)
{
r.Add("corpse: created before its position is readable, it is still a Corpse on the landscape", CorpseWithoutPositionIsCorpse);
r.Add("corpse: the weenie corpse flag makes a nameless corpse a Corpse", CorpseFlagWithoutName);
r.Add("corpse: one stuck in the inventory as a Container is moved to the landscape", CorpseInInventoryRescued);
r.Add("corpse: a corpse named late (stored nameless) becomes a Corpse on access", CorpseNamedLateOnAccess);
r.Add("loot chain: own corpse appears -> claimed -> opened -> item moved into the pack", FullLootChain);
r.Add("loot chain: 'My Kills Only' loots your kill and skips another player's", OwnershipModes);
r.Add("loot chain: an item the cache never saw created is found in the corpse's contents and looted", UnseenItemRescued);
r.Add("loot chain: without GetContainerContents an unseen item stays unseen (no crash)", UnseenItemOldEngine);
r.Add("loot starvation: a corpse waiting behind far-off combat gets a turn, a close monster keeps combat", LootStarvation);
r.Add("loot logging: a corpse held by combat is logged once, with the reason", HeldByCombatLoggedOnce);
r.Add("monster ignore: a close Priority 0 monster neither holds combat nor starves the corpse", IgnoredMonsterDoesNotHoldLoot);
r.Add("corpse close: a finished corpse is closed once (CloseContainer, API v76)", FinishedCorpseClosedOnce);
r.Add("corpse close: an engine without CloseContainer leaves it to the next open (no close)", FinishedCorpseNotClosedOnOldEngine);
r.Add("corpse close: an abandoned open corpse is closed once", AbandonedCorpseClosed);
r.Add("corpse close: stopping the macro mid-loot closes the open corpse, not marked completed", MacroStopClosesCorpse);
r.Add("loot pacing: a pickup waits for the last bend-down, and no retry is timed while it plays", PickupWaitsForAnimation);
r.Add("loot pacing: no pickup (first or retry) while the server owes an answer to a cast; it goes on the UseDone", PickupWaitsForCastAnswer);
r.Add("corpse close: not while the last item is between the corpse and the pack - it closes once the item is in the inventory", CloseWaitsForInventory);
r.Add("loot pacing: an item merged into a stack in the pack is confirmed without the 4 s arrival wait", MergedPickupConfirmedQuickly);
r.Add("loot pacing: each item taken restarts the corpse timeout, so a corpse with many keeps is looted to the end", ManyKeepsBeatTheTimeout);
}
// ── World set-up ──────────────────────────────────────────────────────────
private static (uint Cell, float X, float Y, float Z) Me0 => (0x1048002Au, 100f, 100f, 0f);
private static void BaseWorld(bool corpsePositioned)
{
FakeHost.Reset();
FakeHost.SalvageCalls = true; // UseObject + MoveItemExternal
FakeHost.WeaponCalls = true; // RequestId, HasAppraisalData
FakeHost.Names[Player] = Me;
FakeHost.Positions[Player] = Me0;
FakeHost.PlayerPose = Me0;
FakeHost.Names[Corpse] = "Corpse of Ancient Water Elemental";
FakeHost.ItemTypes[Corpse] = ItemTypeContainer;
FakeHost.Strings[(Corpse, LongDescKey)] = $"Killed by {Me}.";
FakeHost.NotAttackable.Add(Corpse);
if (corpsePositioned)
FakeHost.Positions[Corpse] = (Me0.Cell, Me0.X + 1.0f, Me0.Y, Me0.Z);
FakeHost.Names[Gem] = "Black Opal";
FakeHost.ItemTypes[Gem] = ItemTypeGem;
FakeHost.Containers[Gem] = Corpse; // also installs GetObjectOwnershipInfo
FakeHost.Appraised.Add(Gem);
}
// ── Classification (cause 1) ──────────────────────────────────────────────
private static void CorpseWithoutPositionIsCorpse()
{
BaseWorld(corpsePositioned: false);
var host = FakeHost.Create(Player);
var cache = new WorldObjectCache(host);
cache.SetPlayerId(Player);
cache.OnCreateObject(Corpse);
cache.Tick();
Check.Eq(cache[unchecked((int)Corpse)]?.ObjectClass, AcObjectClass.Corpse, "a Corpse, not an inventory Container");
Check.True(cache.GetLandscapeObjects().Any(o => o.Id == unchecked((int)Corpse)), "on the landscape (loot candidates come from there)");
Check.False(cache.GetInventory().Any(o => o.Id == unchecked((int)Corpse)), "not in the inventory");
FakeHost.Positions[Corpse] = (Me0.Cell, Me0.X + 1.0f, Me0.Y, Me0.Z);
double d = cache.Distance(unchecked((int)Player), unchecked((int)Corpse));
Check.True(d < 1.5, $"its distance reads once the position lands (d={d:0.00})");
}
private static void CorpseFlagWithoutName()
{
BaseWorld(corpsePositioned: true);
FakeHost.Names.Remove(Corpse);
FakeHost.Bitfields[Corpse] = BfCorpse;
var host = FakeHost.Create(Player);
var cache = new WorldObjectCache(host);
cache.SetPlayerId(Player);
cache.OnCreateObject(Corpse);
cache.Tick();
Check.Eq(cache[unchecked((int)Corpse)]?.ObjectClass, AcObjectClass.Corpse, "the corpse flag alone makes it a Corpse");
FakeHost.Names[Corpse] = "Corpse of Ancient Water Elemental";
Check.Eq(cache[unchecked((int)Corpse)]?.Name, "Corpse of Ancient Water Elemental", "the name fills in later");
}
private static void CorpseInInventoryRescued()
{
// The old trap, reproduced directly: an entry filed as an inventory Container (as
// TryClassify did before the fix when the position wasn't readable yet).
BaseWorld(corpsePositioned: false);
FakeHost.Names.Remove(Corpse);
var host = FakeHost.Create(Player);
var cache = new WorldObjectCache(host);
cache.SetPlayerId(Player);
_ = cache[unchecked((int)Corpse)]; // nameless, positionless: the lazy lookup says "not yet"
// File it as the old code did.
var byId = (Dictionary<int, WorldObject>)typeof(WorldObjectCache).GetField("_byId", Any)!.GetValue(cache)!;
var inv = (HashSet<int>)typeof(WorldObjectCache).GetField("_inventory", Any)!.GetValue(cache)!;
var make = typeof(WorldObjectCache).GetMethod("Make", Any)!;
lock (typeof(WorldObjectCache).GetField("_gate", Any)!.GetValue(cache)!)
{
byId[unchecked((int)Corpse)] = (WorldObject)make.Invoke(cache, new object[] { unchecked((int)Corpse), "", AcObjectClass.Container })!;
inv.Add(unchecked((int)Corpse));
}
FakeHost.Names[Corpse] = "Corpse of Ancient Water Elemental";
FakeHost.Positions[Corpse] = (Me0.Cell, Me0.X + 1.0f, Me0.Y, Me0.Z);
typeof(WorldObjectCache).GetField("_lastReclassifyTime", Any)!.SetValue(cache, DateTime.MinValue);
cache.Tick(); // the 2 s pass
var wo = byId[unchecked((int)Corpse)];
Check.Eq(wo.ObjectClass, AcObjectClass.Corpse, "rescued to Corpse");
Check.True(cache.GetLandscapeObjects().Any(o => o.Id == unchecked((int)Corpse)), "and on the landscape");
Check.True(FakeHost.Logs.Any(l => l.Contains("was filed as Container")), "the rescue is logged");
}
private static void CorpseNamedLateOnAccess()
{
BaseWorld(corpsePositioned: true);
FakeHost.Names.Remove(Corpse);
FakeHost.ItemTypes.Remove(Corpse);
var host = FakeHost.Create(Player);
var cache = new WorldObjectCache(host);
cache.SetPlayerId(Player);
_ = cache[unchecked((int)Corpse)]; // positioned, nameless, typeless: Unknown landscape
Check.Eq(cache[unchecked((int)Corpse)]?.ObjectClass, AcObjectClass.Unknown, "stored as Unknown first");
FakeHost.Names[Corpse] = "Corpse of Ancient Water Elemental";
Check.Eq(cache[unchecked((int)Corpse)]?.ObjectClass, AcObjectClass.Corpse, "a Corpse as soon as its name reads");
}
// ── The plugin, without Initialize (no UI, no disk) ───────────────────────
private sealed class Bot
{
public readonly RynthAiPlugin P = new();
public readonly LegacyUiSettings S = new()
{
IsMacroRunning = true,
EnableLooting = true,
EnableCombat = false,
LootOwnership = 0, // My Kills Only
LootContentSettleMs = 0,
LootEmptyCorpseMs = 0,
StopLootingWhenPackFull = false,
};
public readonly WorldObjectCache Cache;
public Bot(RynthCoreHost host)
{
typeof(RynthPluginBase).GetMethod("Attach", Any)!.Invoke(P, new object[] { FakeHost.LastApi });
var dash = (LegacyDashboardRenderer)RuntimeHelpers.GetUninitializedObject(typeof(LegacyDashboardRenderer));
typeof(LegacyDashboardRenderer).GetField("_settings", Any)!.SetValue(dash, S);
Set("_dashboard", dash);
Cache = new WorldObjectCache(host);
Cache.SetPlayerId(Player);
Set("_objectCache", Cache);
Set("_playerId", Player);
string profile = Path.Combine(Program.TempRoot, "loot-corpse-test.json");
var p = new LootProfile { Name = "gems" };
var rule = new LootRule { Name = "Gems", Action = LootAction.Keep };
rule.Conditions.Add(new ObjectClassCondition { ObjectClass = AcObjectClass.Gem });
p.Rules.Add(rule);
p.Save(profile);
S.CurrentLootPath = profile;
}
public void Set(string f, object? v) => typeof(RynthAiPlugin).GetField(f, Any)!.SetValue(P, v);
public T Get<T>(string f) => (T)typeof(RynthAiPlugin).GetField(f, Any)!.GetValue(P)!;
public object? Call(string m, params object[] a) => typeof(RynthAiPlugin).GetMethod(m, Any)!.Invoke(P, a);
public bool HasLootWork() => (bool)Call("HasLootWork", S, DateTimeOffset.UtcNow.ToUnixTimeMilliseconds())!;
public void Tick(BotActivity act)
{
Set("_activity", act);
Call("TickCorpseOpening");
}
}
private static void FullLootChain()
{
BaseWorld(corpsePositioned: false); // identity before position: the old trap
var host = FakeHost.Create(Player);
var bot = new Bot(host);
bot.Cache.OnCreateObject(Corpse);
bot.Cache.Tick();
Check.Eq(bot.Cache[unchecked((int)Corpse)]?.ObjectClass, AcObjectClass.Corpse, "the fresh corpse is a Corpse");
FakeHost.Positions[Corpse] = (Me0.Cell, Me0.X + 1.0f, Me0.Y, Me0.Z);
Check.True(bot.HasLootWork(), "looting wants the tick: an own corpse in range");
bot.Tick(BotActivity.Looting);
Check.Eq(bot.Get<int>("_targetCorpseId"), unchecked((int)Corpse), "the corpse is claimed");
Check.True(FakeHost.Logs.Any(l => l.Contains("claimed corpse target")), "claim logged");
Check.True(FakeHost.Uses.Contains(Corpse), "the corpse is opened (UseObject on it)");
// The server opens it: the item arrives, then ViewContents.
bot.Cache.OnCreateObject(Gem);
bot.Cache.Tick();
bot.P.OnViewObjectContents(Corpse);
for (int i = 0; i < 100 && !FakeHost.ExternalMoves.Any(m => m.Id == Gem); i++)
{
bot.Tick(BotActivity.Looting);
Thread.Sleep(10);
}
Check.True(FakeHost.ExternalMoves.Contains((Gem, Player, 0)),
"the gem is moved into the main pack (moves: " + string.Join(",", FakeHost.ExternalMoves.Select(m => $"{m.Id:X8}->{m.Target:X8}")) + ")");
Check.False(FakeHost.Uses.Contains(Gem), "never used (a use can wield)");
}
// 2026-10-11, Silentkelpie: a Necrolytic Extract on his own kill, a Keep rule for it, left on
// the corpse with no [LootEval] line. The cache never processed a create for it, so it was
// in neither the inventory nor the landscape set and GetContainedItems never returned it.
private static void UnseenItemRescued()
{
BaseWorld(corpsePositioned: true);
FakeHost.ContentsCalls = true;
var host = FakeHost.Create(Player);
var bot = new Bot(host);
bot.Cache.OnCreateObject(Corpse);
bot.Cache.Tick();
Check.True(bot.HasLootWork(), "looting wants the tick");
bot.Tick(BotActivity.Looting);
Check.Eq(bot.Get<int>("_targetCorpseId"), unchecked((int)Corpse), "the corpse is claimed");
// The gem is in the corpse but its create never reaches the cache.
bot.P.OnViewObjectContents(Corpse);
Check.False(bot.Cache.GetContainedItems(unchecked((int)Corpse)).Any(), "the cache has nothing for the corpse (the bug's state)");
for (int i = 0; i < 100 && !FakeHost.ExternalMoves.Any(m => m.Id == Gem); i++)
{
bot.Tick(BotActivity.Looting);
Thread.Sleep(10);
}
Check.True(FakeHost.Logs.Any(l => l.Contains("the object cache had missed") && l.Contains("Black Opal")), "the rescue is logged with the item");
Check.True(FakeHost.ExternalMoves.Contains((Gem, Player, 0)), "the unseen gem is looted");
}
private static void UnseenItemOldEngine()
{
BaseWorld(corpsePositioned: true);
var host = FakeHost.Create(Player); // no GetContainerContents
var bot = new Bot(host);
bot.Cache.OnCreateObject(Corpse);
bot.Cache.Tick();
bot.Tick(BotActivity.Looting);
bot.P.OnViewObjectContents(Corpse);
for (int i = 0; i < 20; i++) { bot.Tick(BotActivity.Looting); Thread.Sleep(5); }
Check.False(FakeHost.ExternalMoves.Any(m => m.Id == Gem), "nothing to find it with: not looted");
Check.False(FakeHost.Logs.Any(l => l.Contains("the object cache had missed")), "no rescue line");
}
private static void OwnershipModes()
{
BaseWorld(corpsePositioned: true);
const uint Other = 0x80261200;
FakeHost.Names[Other] = "Corpse of Ancient Water Elemental";
FakeHost.ItemTypes[Other] = ItemTypeContainer;
FakeHost.Strings[(Other, LongDescKey)] = "Killed by Charlie.";
FakeHost.Positions[Other] = (Me0.Cell, Me0.X, Me0.Y + 1.0f, Me0.Z);
var host = FakeHost.Create(Player);
var bot = new Bot(host);
bot.Cache.OnCreateObject(Corpse);
bot.Cache.OnCreateObject(Other);
bot.Cache.Tick();
bool Loot(uint id) => (bool)bot.Call("ShouldLootCorpse", bot.Cache[unchecked((int)id)]!)!;
Check.True(Loot(Corpse), "My Kills Only: your own kill is looted");
Check.False(Loot(Other), "My Kills Only: another player's kill is skipped");
bot.S.LootOwnership = 2;
Check.True(Loot(Other), "All Corpses: anyone's kill is looted");
}
private static Bot StarvationWorld(float monsterMeters, out CombatManager combat, Action<LegacyUiSettings>? configure = null)
{
BaseWorld(corpsePositioned: true);
FakeHost.Names[Monster] = "Ancient Water Elemental";
FakeHost.ItemTypes[Monster] = ItemTypeCreature;
FakeHost.Positions[Monster] = (Me0.Cell, Me0.X + monsterMeters, Me0.Y + 0.5f, Me0.Z);
var host = FakeHost.Create(Player);
var bot = new Bot(host);
bot.S.EnableCombat = true;
bot.S.MonsterRange = 50;
configure?.Invoke(bot.S);
bot.Cache.OnCreateObject(Corpse);
_ = bot.Cache[unchecked((int)Monster)];
bot.Cache.OnUpdateHealth(Monster, 1f);
bot.Cache.Tick();
combat = new CombatManager(host, bot.S, bot.Cache);
combat.SetPlayerId(Player);
typeof(CombatManager).GetField("_lastScanTime", Any)?.SetValue(combat, DateTime.MinValue);
combat.ScanNearbyTargets();
bot.Set("_combatManager", combat);
return bot;
}
private static void AgeCorpse(Bot bot, long ms)
{
var waiting = bot.Get<Dictionary<int, long>>("_corpseWaitingSince");
waiting[unchecked((int)Corpse)] = Environment.TickCount64 - ms;
}
private static void LootStarvation()
{
var bot = StarvationWorld(15f, out var combat);
Check.True(combat.HasTargets, "combat has a target in range (15 m)");
Check.True(combat.HasEngageableTarget, "and wants the tick");
Check.True(bot.HasLootWork(), "the corpse is lootable and in range");
Check.False(bot.Get<bool>("_lootStarved"), "fresh: not starved yet");
var noStarve = new ArbiterInputs(true, false, true, true, false, true, combatEngaged: true);
Check.Eq(ActivityArbiter.Decide(in noStarve), BotActivity.Combat, "combat first while the corpse is fresh");
bot.Tick(BotActivity.Combat);
Check.Eq(bot.Get<int>("_targetCorpseId"), 0, "no claim while combat holds the tick");
AgeCorpse(bot, RynthAiPlugin.LootStarveMs + 1000);
Check.True(bot.HasLootWork(), "still lootable");
Check.True(bot.Get<bool>("_lootStarved"), "waited past LootStarveMs: starved");
Check.True((bool)bot.Call("IsLootStarvedTurn", true, true)!, "no monster within 5 yd: looting gets a turn");
var starve = new ArbiterInputs(true, false, true, true, false, true, combatEngaged: true, lootStarved: true);
Check.Eq(ActivityArbiter.Decide(in starve), BotActivity.Looting, "the arbiter gives Looting the tick");
bot.Set("_lootStarveTurn", true);
bot.Tick(BotActivity.Looting);
Check.Eq(bot.Get<int>("_targetCorpseId"), unchecked((int)Corpse), "the starved corpse is claimed despite combat targets");
Check.True(FakeHost.Uses.Contains(Corpse), "and opened");
// A monster on top of you keeps combat in charge.
var close = StarvationWorld(2f, out var combat2);
Check.True(combat2.HasCloseThreat(RynthAiPlugin.LootStarveCloseYards), "a monster within 5 yd");
AgeCorpse(close, RynthAiPlugin.LootStarveMs + 1000);
Check.True(close.HasLootWork(), "corpse lootable");
Check.False((bool)close.Call("IsLootStarvedTurn", true, true)!, "close monster: no loot turn, combat keeps the tick");
var buffing = new ArbiterInputs(true, true, true, true, false, true, lootStarved: true);
Check.Eq(ActivityArbiter.Decide(in buffing), BotActivity.Buffing, "buffing still outranks a starved corpse");
}
private static void IgnoredMonsterDoesNotHoldLoot()
{
// The same close (2 m) Water Elemental that keeps combat in charge above, with its rule at Priority 0.
var bot = StarvationWorld(2f, out var combat, s => s.MonsterRules = new List<MonsterRule>
{
new MonsterRule { Name = "Default", Priority = 1 },
new MonsterRule { Name = "Water Elemental", Priority = 0 },
});
Check.False(combat.HasTargets, "an ignored monster is not a target");
Check.False(combat.HasNearbyMonsters, "and not a monster nearby");
Check.False(combat.HasCloseThreat(RynthAiPlugin.LootStarveCloseYards), "nor a close threat that keeps the starved corpse waiting");
Check.False(combat.HasEngageableTarget, "combat does not want the tick");
AgeCorpse(bot, RynthAiPlugin.LootStarveMs + 1000);
Check.True(bot.HasLootWork(), "the corpse is lootable");
var inputs = new ArbiterInputs(true, false, combat.HasEngageableTarget, true, false, true, combatEngaged: combat.HasEngageableTarget);
Check.Eq(ActivityArbiter.Decide(in inputs), BotActivity.Looting, "so looting has the tick without needing a starved turn");
}
private static void HeldByCombatLoggedOnce()
{
var bot = StarvationWorld(15f, out _);
bot.HasLootWork();
for (int i = 0; i < 5; i++) bot.Tick(BotActivity.Combat);
int lines = FakeHost.Logs.Count(l => l.Contains($"corpse 0x{Corpse:X8} waits for combat"));
Check.Eq(lines, 1, "one line for the held corpse, not one per tick");
}
// ── Closing the corpse when done (2026-10-04: "when finished looting I don't think the
// corpse closes"). API v76 CloseContainer sends the client's own close packet (0x0195). ──
/// <summary>Claims, opens and starts looting the corpse; the gem's move has been sent.
/// With <paramref name="closeSupported"/> the host has CloseContainer and GetGroundContainerId,
/// and the client reports the corpse open (as the engine does after ViewContents).</summary>
private static Bot OpenCorpseAndSendPickup(bool closeSupported)
{
BaseWorld(corpsePositioned: true);
FakeHost.CloseContainerCalls = closeSupported;
var host = FakeHost.Create(Player);
Check.Eq(host.HasCloseContainer, closeSupported, "the host's CloseContainer support");
var bot = new Bot(host);
bot.Cache.OnCreateObject(Corpse);
bot.Cache.Tick();
bot.Tick(BotActivity.Looting);
Check.True(FakeHost.Uses.Contains(Corpse), "the corpse is opened");
bot.Cache.OnCreateObject(Gem);
bot.Cache.Tick();
if (closeSupported)
FakeHost.GroundContainer = Corpse;
bot.P.OnViewObjectContents(Corpse);
for (int i = 0; i < 100 && !FakeHost.ExternalMoves.Any(m => m.Id == Gem); i++)
{
bot.Tick(BotActivity.Looting);
Thread.Sleep(10);
}
Check.True(FakeHost.ExternalMoves.Contains((Gem, Player, 0)), "the gem's move is sent");
Check.Eq(FakeHost.Closes.Count, 0, "nothing closed while an item is still being looted");
return bot;
}
private static bool Completed(Bot bot) =>
bot.Get<Dictionary<int, long>>("_completedCorpses").ContainsKey(unchecked((int)Corpse));
/// <summary>The gem lands in the pack; ticks until the corpse is marked completed.</summary>
private static void FinishLooting(Bot bot)
{
FakeHost.Containers[Gem] = Player;
for (int i = 0; i < 200 && !Completed(bot); i++)
{
bot.Tick(BotActivity.Looting);
Thread.Sleep(10);
}
Check.True(Completed(bot), "the corpse is completed once its last item is in the pack");
}
private static void FinishedCorpseClosedOnce()
{
var bot = OpenCorpseAndSendPickup(closeSupported: true);
FinishLooting(bot);
Check.Eq(string.Join(",", FakeHost.Closes.Select(c => $"{c:X8}")), $"{Corpse:X8}", "the finished corpse is closed, once");
Check.Eq(bot.Get<int>("_busyCount"), 0, "the close adds nothing to the busy count");
Check.Eq(bot.Get<int>("_openedContainerId"), 0, "the bot no longer holds it open");
// Until the server's answer lands the client still reports it open: no second close.
for (int i = 0; i < 5; i++) bot.Tick(BotActivity.Looting);
Check.Eq(FakeHost.Closes.Count, 1, "no re-close while the client still shows it");
Check.Eq(bot.Get<int>("_openedContainerId"), 0, "a completed corpse is not re-claimed from the client's state");
// The server's CloseGroundContainer: the window goes, StopViewingObjectContents follows.
FakeHost.GroundContainer = 0;
bot.P.OnStopViewingObjectContents(Corpse);
for (int i = 0; i < 5; i++) bot.Tick(BotActivity.Looting);
Check.Eq(FakeHost.Closes.Count, 1, "still one close after the server's answer");
Check.Eq(FakeHost.Uses.Count(u => u == Corpse), 1, "never a UseObject to close it");
}
private static void FinishedCorpseNotClosedOnOldEngine()
{
var bot = OpenCorpseAndSendPickup(closeSupported: false);
FinishLooting(bot);
Check.Eq(FakeHost.Closes.Count, 0, "no CloseContainer on an engine before v76");
Check.Eq(FakeHost.Uses.Count(u => u == Corpse), 1, "and no UseObject to close it either");
}
private static void AbandonedCorpseClosed()
{
var bot = OpenCorpseAndSendPickup(closeSupported: true);
bot.Call("AbandonCurrentCorpse", Environment.TickCount64, "timeout");
Check.Eq(string.Join(",", FakeHost.Closes.Select(c => $"{c:X8}")), $"{Corpse:X8}", "the abandoned corpse is closed, once");
Check.True(Completed(bot), "an opened corpse that timed out is completed (as before)");
Check.Eq(bot.Get<int>("_openedContainerId"), 0, "and released");
}
private static void MacroStopClosesCorpse()
{
var bot = OpenCorpseAndSendPickup(closeSupported: true);
bot.S.IsMacroRunning = false;
bot.Call("CloseOpenCorpseOnMacroStop");
Check.Eq(string.Join(",", FakeHost.Closes.Select(c => $"{c:X8}")), $"{Corpse:X8}", "the open corpse is closed when the macro stops");
Check.False(Completed(bot), "not completed: a later run can loot what is left");
// Macro off, ticking on: no further close before the server's answer.
for (int i = 0; i < 5; i++) bot.Tick(BotActivity.Idle);
Check.Eq(FakeHost.Closes.Count, 1, "one close");
FakeHost.GroundContainer = 0;
bot.P.OnStopViewingObjectContents(Corpse);
Check.Eq(bot.Get<int>("_openedContainerId"), 0, "the server's answer clears the open corpse");
// No open corpse: nothing to close.
bot.Call("CloseOpenCorpseOnMacroStop");
Check.Eq(FakeHost.Closes.Count, 1, "a stop with no corpse open closes nothing");
}
// 2026-10-07, Drakkon: picking up an item plays a bend-down, and the engine holds queued
// moves until it ends (about 2.2 s). RynthAi retried every 0.5 s from the send and gave up
// after 3 tries, so items were skipped while their first move had not even gone out.
private static void PickupWaitsForAnimation()
{
BaseWorld(corpsePositioned: true);
FakeHost.ServerGates = true; // GetCastBusyState
var host = FakeHost.Create(Player);
var bot = new Bot(host);
bot.Cache.OnCreateObject(Corpse);
bot.Cache.Tick();
bot.Tick(BotActivity.Looting);
Check.True(FakeHost.Uses.Contains(Corpse), "the corpse is opened");
// The last pickup is still animating when the corpse's contents arrive.
FakeHost.CastBusy = 1;
bot.Cache.OnCreateObject(Gem);
bot.Cache.Tick();
bot.P.OnViewObjectContents(Corpse);
TickFor(bot, 500);
Check.False(FakeHost.ExternalMoves.Any(m => m.Id == Gem), "no pickup sent while the character animates");
FakeHost.CastBusy = 0;
for (int i = 0; i < 100 && !FakeHost.ExternalMoves.Any(m => m.Id == Gem); i++)
{
bot.Tick(BotActivity.Looting);
Thread.Sleep(10);
}
Check.Eq(FakeHost.ExternalMoves.Count(m => m.Id == Gem), 1, "the pickup goes out once the character is still");
// Its own bend-down plays for longer than 3 retry windows: no retry, no give-up.
FakeHost.CastBusy = 1;
TickFor(bot, 1800);
Check.Eq(FakeHost.ExternalMoves.Count(m => m.Id == Gem), 1, "no retry while the pickup animates");
Check.False(FakeHost.Logs.Any(l => l.Contains("pickup failed")), "the item is not given up on");
// Still again and the item still on the corpse: the retry window starts now.
FakeHost.CastBusy = 0;
TickFor(bot, 250);
Check.Eq(FakeHost.ExternalMoves.Count(m => m.Id == Gem), 1, "the retry window counts from when the character is still");
TickFor(bot, 500);
Check.Eq(FakeHost.ExternalMoves.Count(m => m.Id == Gem), 2, "then one retry");
}
// A pickup sent while Bloodstone Bolt VII was unanswered came back
// "too busy" (ACE refuses a pickup while IsBusy); the assess-timeout path skips the busy gate.
private static void PickupWaitsForCastAnswer()
{
BaseWorld(corpsePositioned: true);
FakeHost.ServerGates = true; // GetCastBusyState, GetUseDoneSeq
var host = FakeHost.Create(Player);
var bot = new Bot(host);
long t = 80_000_000;
ActionPacer.Reset(); ActionPacer.Enabled = true; ActionPacer.NowMs = () => t;
try
{
bot.Cache.OnCreateObject(Corpse);
bot.Cache.Tick();
bot.Tick(BotActivity.Looting);
Check.True(FakeHost.Uses.Contains(Corpse), "the corpse is opened");
ActionPacer.Reset(); ActionPacer.Enabled = true; ActionPacer.NowMs = () => t; // the open is answered
// A cast goes out (Combat) and has no answer yet when the contents arrive.
ActionPacer.Begin(host, PacedKind.Cast, 0, "Bloodstone Bolt VII", "Combat", FakeHost.UseDoneSeq);
bot.Cache.OnCreateObject(Gem);
bot.Cache.Tick();
bot.P.OnViewObjectContents(Corpse);
for (int i = 0; i < 10; i++) { t += 50; bot.Tick(BotActivity.Looting); Thread.Sleep(10); }
Check.False(FakeHost.ExternalMoves.Any(m => m.Id == Gem), "no pickup while the cast is unanswered");
Check.Eq(FakeHost.Logs.Count(l => l.Contains("pickup waits - the server still owes an answer")), 1, "the hold is logged once");
FakeHost.UseDone(0); // the cast's answer
for (int i = 0; i < 100 && !FakeHost.ExternalMoves.Any(m => m.Id == Gem); i++) { t += 50; bot.Tick(BotActivity.Looting); Thread.Sleep(10); }
Check.Eq(FakeHost.ExternalMoves.Count(m => m.Id == Gem), 1, "the pickup goes on the cast's UseDone");
// The item stays on the corpse (refused); the next cast is unanswered when a retry is due.
ActionPacer.Begin(host, PacedKind.Cast, 0, "Bloodstone Bolt I", "Combat", FakeHost.UseDoneSeq);
TickFor(bot, 1_500);
Check.Eq(FakeHost.ExternalMoves.Count(m => m.Id == Gem), 1, "no retry while a cast is unanswered");
FakeHost.UseDone(0);
TickFor(bot, 300);
Check.Eq(FakeHost.ExternalMoves.Count(m => m.Id == Gem), 2, "the retry goes once it is answered");
}
finally { ActionPacer.Enabled = false; ActionPacer.Reset(); ActionPacer.NowMs = () => Environment.TickCount64; }
}
// 2026-10-07: the client takes an item off the corpse list as soon as the move is sent; the
// corpse was closed then, and the character picked the item up after the corpse closed.
private static void CloseWaitsForInventory()
{
var bot = OpenCorpseAndSendPickup(closeSupported: true);
FakeHost.Containers[Gem] = 0x7FFFFFF0; // off the corpse, not in a pack yet
TickFor(bot, 1500);
Check.Eq(FakeHost.Closes.Count, 0, "not closed while the item is on its way");
Check.False(Completed(bot), "not completed either");
FakeHost.Containers[Gem] = Player; // in the main pack
for (int i = 0; i < 200 && FakeHost.Closes.Count == 0; i++)
{
bot.Tick(BotActivity.Looting);
Thread.Sleep(10);
}
Check.Eq(FakeHost.Closes.Count, 1, "closed once the item is in the inventory");
Check.True(FakeHost.Logs.Any(l => l.Contains("pickup confirmed")), "and the pickup confirmed");
}
// 2026-10-11, Silentkelpie: Necrolytic Extracts and Coalesced Aetheria merge into stacks
// already in the pack (StackableMerge). The server removes the picked object and the client
// keeps no container for it, so it never "arrived" in the inventory and every such item
// waited out PickupArriveMaxMs (4 s) before the next pickup. With the 12 s corpse timeout,
// a corpse with three or four keeps was closed part way through.
private const uint Gem2 = 0x80261102, Gem3 = 0x80261103;
private static Bot ThreeGemCorpse(int corpseTimeoutMs)
{
BaseWorld(corpsePositioned: true);
foreach (uint g in new[] { Gem2, Gem3 })
{
FakeHost.Names[g] = "Black Opal";
FakeHost.ItemTypes[g] = ItemTypeGem;
FakeHost.Containers[g] = Corpse;
FakeHost.Appraised.Add(g);
}
// Stackable, as the real merging items are: only a stack that ends up with no container
// counts as merged (an unknown item off the corpse with no container is still on its way).
foreach (uint g in new[] { Gem, Gem2, Gem3 })
{
FakeHost.Ints[(g, 11)] = 100; // MaxStackSize
FakeHost.Ints[(g, 12)] = 1; // StackCount
}
FakeHost.CloseContainerCalls = true;
var host = FakeHost.Create(Player);
var bot = new Bot(host);
bot.S.LootCorpseTimeoutMs = corpseTimeoutMs;
bot.Cache.OnCreateObject(Corpse);
bot.Cache.Tick();
bot.Tick(BotActivity.Looting);
Check.True(FakeHost.Uses.Contains(Corpse), "the corpse is opened");
foreach (uint g in new[] { Gem, Gem2, Gem3 })
bot.Cache.OnCreateObject(g);
bot.Cache.Tick();
FakeHost.GroundContainer = Corpse;
bot.P.OnViewObjectContents(Corpse);
return bot;
}
/// <summary>Ticks for up to <paramref name="ms"/>; every item whose move went out merges at once.</summary>
private static long LootWithMerges(Bot bot, int ms, int wantMoves)
{
long start = Environment.TickCount64, end = start + ms;
while (Environment.TickCount64 < end && FakeHost.ExternalMoves.Select(m => m.Id).Distinct().Count() < wantMoves)
{
foreach (var m in FakeHost.ExternalMoves)
FakeHost.Containers[m.Id] = 0; // merged: gone from the corpse, no container
bot.Tick(BotActivity.Looting);
Thread.Sleep(10);
}
foreach (var m in FakeHost.ExternalMoves)
FakeHost.Containers[m.Id] = 0;
return Environment.TickCount64 - start;
}
private static void MergedPickupConfirmedQuickly()
{
var bot = ThreeGemCorpse(corpseTimeoutMs: 60_000);
long took = LootWithMerges(bot, 6000, wantMoves: 2);
Check.Eq(FakeHost.ExternalMoves.Select(m => m.Id).Distinct().Count(), 2, "the second pickup goes out");
Check.True(took < 2800, $"the second pickup follows the first merge in about the settle time, not 4 s more (took {took} ms)");
Check.True(FakeHost.Logs.Any(l => l.Contains("merged into a stack in the pack")), "the confirm says why");
}
private static void ManyKeepsBeatTheTimeout()
{
// Each merged item takes about PickupSettleMs (1 s) to confirm; three in a row take longer
// than this corpse timeout, which only an idle corpse should hit.
var bot = ThreeGemCorpse(corpseTimeoutMs: 2200);
LootWithMerges(bot, 8000, wantMoves: 3);
TickFor(bot, 1500);
var moved = FakeHost.ExternalMoves.Select(m => m.Id).Distinct().ToList();
Check.Eq(moved.Count, 3, "all three gems are taken (moved: " + string.Join(",", moved.Select(i => $"{i:X8}")) + ")");
Check.False(FakeHost.Logs.Any(l => l.Contains("abandoning corpse")), "the corpse is not abandoned on the timeout");
}
private static void TickFor(Bot bot, int ms)
{
long end = Environment.TickCount64 + ms;
while (Environment.TickCount64 < end)
{
bot.Tick(BotActivity.Looting);
Thread.Sleep(10);
}
}
}