RynthSuite/Tools/RynthCore.RynthAiHostTests/Tests/LootJumpTests.cs
Daniel Wallace f83eadb4ba RynthAi jump loot: hold combat and buffs for the landing, no second jump in the air
ACE refuses a cast, a swing, a shot, a kit, food or a potion while the
character is in the air (Player_Magic/Melee/Missile, Healer, Food:
IsJumping -> YouCantDoThatWhileInTheAir, 0x4EB). Nothing held combat or
buffs for a loot jump, so an attack or heal sent during the pickup's jump
was refused, and a refused attack was judged a no-damage miss.

ActionPacer now keeps a loot-jump air hold (NoteLootJump /
LootJumpAirborne / ForgetLootJump), with the landing check the stuck-cast
jump already used (height back from JumpLandMinMs, JumpLandMaxMs at most,
so a missed landing cannot stall anything). CombatManager.Think checks it
right before the attack send (target, approach and stance go on; nothing
is stamped, counted or judged for a held tick); MayStart holds "Buff"
starts (buffs, heals, kits, potions, the emergency step). "Loot" and doors
are never held: ACE takes a pickup and a corpse use in the air. The hold
ends at death, portal space and logout.

Belt and braces: a cast answered UseDone 0x4EB, and a swing or shot
followed by WeenieError 0x4EB, is not judged as a no-damage attack.

No second tap jump while the last one is still in the air: the move
arrives in the air anyway, and ACE HandleActionJump has no ground check
(an air jump only costs stamina). A jump the client refused is not noted.

The hold is only ever set by a loot jump, which needs Jump When Looting
(LootJumpEnabled, default off). The 0x4EB handling reacts only to the
server's own in-the-air refusal.

Host tests: no second jump while in the air, a fresh one after the
landing; the jump holds buffs and emergency heals but never loot, until
the landing or the cap; portal space and a death drop the hold; Think
sends no cast in the air and casts on the landing or at the cap; a swing
refused in the air is not a miss.

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

336 lines
17 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
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;
// Jump-loot: the jump is the keyboard one (TapJump, 0xF61B Jump, which ACE handles), never
// JumpNonAutonomous (0xF7C9, no handler on ACE). It goes BEFORE the move, on every pickup (a
// corpse's only match included), only with the setting on and stamina above 5.
// The loot clock (RynthAiPlugin.CorpseClock) is moved by hand here.
internal static class LootJumpTests
{
private const uint Player = 0x50000F31;
private const uint CorpseA = 0x80263100;
private const uint GemA = 0x80263101;
private const uint GemA2 = 0x80263102;
private const uint GemA3 = 0x80263103;
private const uint PackStack = 0x80263120;
private const uint KeyMaxStack = 11, KeyStack = 12;
private const uint CorpseB = 0x80263110;
private const uint GemB = 0x80263111;
private const uint ItemTypeContainer = 0x200, ItemTypeGem = 0x800;
private const uint LongDescKey = 16;
private const string Me = "Test Looter";
private const BindingFlags Any = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
private static (uint Cell, float X, float Y, float Z) Me0 => (0x1048002Au, 100f, 100f, 0f);
public static void Register(Runner r)
{
r.Add("jump loot: two-item corpse - the keyboard jump (0xF61B) goes out before the move; never the unhandled 0xF7C9", TwoItemJumpsBeforeMove);
r.Add("jump loot: a corpse's only match jumps too (one tap jump)", SingleItemJumps);
r.Add("jump loot: the setting off sends no jump", SettingOffNoJump);
r.Add("jump loot: stamina of 5 or less sends no jump", LowStaminaNoJump);
r.Add("jump loot: a pickup sent while still in the air from the last loot jump takes no second jump; after the landing the next one jumps", NoSecondJumpInTheAir);
r.Add("jump loot: the pickup's jump puts combat and buffs on hold until the landing (or the cap); loot never", JumpHoldsCombatNotLoot);
r.Add("jump loot: portal space and a death drop the combat hold (no landing is coming)", PortalAndDeathEndJumpHold);
}
private static long _now;
private static void Clocked(Action body)
{
var old = RynthAiPlugin.CorpseClock;
var oldPace = ActionPacer.NowMs;
_now = 50_000_000;
RynthAiPlugin.CorpseClock = () => _now;
ActionPacer.NowMs = () => _now; // the loot jump's air hold (ActionPacer.LootJumpAirborne) on the same clock
ActionPacer.ForgetLootJump();
try { body(); }
finally { RynthAiPlugin.CorpseClock = old; ActionPacer.NowMs = oldPace; ActionPacer.ForgetLootJump(); }
}
private static RynthAiPlugin _bot = null!; // the bot LootUntilFirstMove made
private static RynthCoreHost _host; // and its host
private static T Field<T>(string name) => (T)typeof(RynthAiPlugin).GetField(name, Any)!.GetValue(_bot)!;
private static void SetField(string name, object v) => typeof(RynthAiPlugin).GetField(name, Any)!.SetValue(_bot, v);
/// <summary>The character's height: up in the air, or back on the ground (ActionPacer's landing check reads it).</summary>
private static void Air(bool up) => FakeHost.PlayerPose = (Me0.Cell, Me0.X, Me0.Y, up ? Me0.Z + 1.0f : Me0.Z);
private static void Gem(uint id, uint container = CorpseA)
{
FakeHost.Names[id] = "Black Opal";
FakeHost.ItemTypes[id] = ItemTypeGem;
FakeHost.Containers[id] = container;
FakeHost.Appraised.Add(id);
}
private static void Corpse(uint id, float dx)
{
FakeHost.Names[id] = "Corpse of Olthoi Swarm Drone";
FakeHost.ItemTypes[id] = ItemTypeContainer;
FakeHost.Strings[(id, LongDescKey)] = $"Killed by {Me}.";
FakeHost.NotAttackable.Add(id);
FakeHost.Positions[id] = (Me0.Cell, Me0.X + dx, Me0.Y, Me0.Z);
}
private static void World(bool secondGem, bool secondCorpse, bool serverGates, bool thirdGem = false, bool packStack = false)
{
FakeHost.Reset();
FakeHost.SalvageCalls = true; // UseObject + MoveItemExternal
FakeHost.WeaponCalls = true; // RequestId, HasAppraisalData
FakeHost.CloseContainerCalls = true; // CloseContainer + GetGroundContainerId
FakeHost.TapJumpCalls = true;
FakeHost.ServerGates = serverGates; // GetCastBusyState: the client's motion queue
FakeHost.LastUseDoneCalls = serverGates; // GetLastUseDone: the open's answer
FakeHost.WeenieErrorCalls = true; // GetLastWeenieError: "You're too busy"
FakeHost.Names[Player] = Me;
FakeHost.Positions[Player] = Me0;
FakeHost.PlayerPose = Me0;
Corpse(CorpseA, 1.0f);
Gem(GemA);
if (secondGem) Gem(GemA2);
if (thirdGem) Gem(GemA3);
if (secondCorpse)
{
Corpse(CorpseB, 1.5f);
Gem(GemB, CorpseB);
}
if (packStack)
{
// GemA is a stack of 1, and the pack holds a same-name stack of 3 it can merge into.
FakeHost.Ints[(GemA, KeyMaxStack)] = 100;
FakeHost.Ints[(GemA, KeyStack)] = 1;
Gem(PackStack, Player);
FakeHost.Ints[(PackStack, KeyMaxStack)] = 100;
FakeHost.Ints[(PackStack, KeyStack)] = 3;
}
}
/// <summary>Opens the corpse and ticks until the first pickup's move has gone out. Returns the tick.</summary>
private static Action LootUntilFirstMove(bool jumpEnabled, bool secondGem, uint stamina, bool secondCorpse = false, bool serverGates = false, bool thirdGem = false, bool packStack = false)
{
World(secondGem, secondCorpse, serverGates, thirdGem, packStack);
var host = FakeHost.Create(Player);
_host = host;
var bot = new RynthAiPlugin();
_bot = bot;
var s = new LegacyUiSettings
{
IsMacroRunning = true, EnableLooting = true, EnableCombat = false, LootOwnership = 0,
LootContentSettleMs = 0, LootEmptyCorpseMs = 0, StopLootingWhenPackFull = false,
LootJumpEnabled = jumpEnabled,
};
typeof(RynthPluginBase).GetMethod("Attach", Any)!.Invoke(bot, new object[] { FakeHost.LastApi });
var dash = (LegacyDashboardRenderer)RuntimeHelpers.GetUninitializedObject(typeof(LegacyDashboardRenderer));
typeof(LegacyDashboardRenderer).GetField("_profileLoaded", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)!.SetValue(dash, true); // a stand-in with its profile in memory
typeof(LegacyDashboardRenderer).GetField("_settings", Any)!.SetValue(dash, s);
void Set(string f, object? v) => typeof(RynthAiPlugin).GetField(f, Any)!.SetValue(bot, v);
Set("_dashboard", dash);
var cache = new WorldObjectCache(host);
cache.SetPlayerId(Player);
Set("_objectCache", cache);
Set("_playerId", Player);
Set("_vitals", new PlayerVitalsCache { MaxHealth = 100, CurrentHealth = 100, MaxStamina = 100, CurrentStamina = stamina, MaxMana = 100, CurrentMana = 100 });
string profile = Path.Combine(Program.TempRoot, "loot-jump-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;
void Tick()
{
Set("_activity", BotActivity.Looting);
typeof(RynthAiPlugin).GetMethod("TickCorpseOpening", Any)!.Invoke(bot, null);
}
var ids = new List<uint> { CorpseA, GemA };
if (secondGem) ids.Add(GemA2);
if (thirdGem) ids.Add(GemA3);
if (packStack) ids.Add(PackStack);
if (secondCorpse) { ids.Add(CorpseB); ids.Add(GemB); }
foreach (uint id in ids) cache.OnCreateObject(id);
for (int i = 0; i < 50; i++) cache.Tick();
Tick();
Check.True(FakeHost.Uses.Contains(CorpseA), "the corpse is opened");
FakeHost.UseDone(0);
FakeHost.GroundContainer = CorpseA;
bot.OnViewObjectContents(CorpseA);
_openedAt = _now;
for (int i = 0; i < 200 && FakeHost.ExternalMoves.Count == 0; i++) { _now += 20; Tick(); }
Check.True(FakeHost.ExternalMoves.Count > 0, "a gem's move is sent");
return Tick;
}
private const long TickMs = 20;
private static long _openedAt; // CorpseA's contents arrived (LootUntilFirstMove)
/// <summary>
/// The latest tick the next corpse's open may go out after the last item's confirm at
/// <paramref name="confirmed"/>: at once, except that a corpse emptied sooner than the
/// Loot Empty Corpse floor (max(200 ms, LootEmptyCorpseMs) from its open, a user setting, not
/// a landing wait) is completed only then; a tick or two of slack.
/// </summary>
private static long NextOpenDue(long confirmed) => Math.Max(confirmed, _openedAt + 200) + 3 * TickMs;
private static bool Confirmed(uint id) => FakeHost.Logs.Any(l => l.Contains($"pickup confirmed for 0x{id:X8}"));
/// <summary>Ticks (TickMs each) until <paramref name="done"/> or <paramref name="maxMs"/> has passed.</summary>
private static void TickUntil(Action tick, Func<bool> done, long maxMs)
{
long end = _now + maxMs;
while (_now < end && !done()) { _now += TickMs; tick(); }
}
private static void TickFor(Action tick, long ms)
{
long end = _now + ms;
while (_now < end) { _now += TickMs; tick(); }
}
// A pickup sent while the character is still in the air from the last loot jump takes no second
// jump: the move arrives in the air anyway, and ACE HandleActionJump has no ground check, so an
// air jump only costs stamina. Once the character has landed, the next pickup jumps again.
private static void NoSecondJumpInTheAir() => Clocked(() =>
{
var tick = LootUntilFirstMove(jumpEnabled: true, secondGem: true, stamina: 100u, serverGates: true, thirdGem: true);
long jumpAt = _now;
uint first = FakeHost.ExternalMoves[0].Id;
Check.Eq(FakeHost.TapJumps, 1, "the first pickup jumps");
Air(up: true); // and stays up: no landing is seen
TickFor(tick, 100);
FakeHost.Containers[first] = Player; // the answer
TickUntil(tick, () => FakeHost.ExternalMoves.Count >= 2, ActionPacer.JumpLandMaxMs);
Check.Eq(FakeHost.ExternalMoves.Count, 2, "the second move is sent");
Check.True(_now - jumpAt < ActionPacer.JumpLandMaxMs, $"(inside the first jump's flight: {_now - jumpAt} ms after it)");
Check.True(ActionPacer.LootJumpAirborne(_host, out _), "(still in the air at the second send)");
Check.Eq(FakeHost.TapJumps, 1, "no second jump in the air: " + Order());
uint second = FakeHost.ExternalMoves[1].Id;
Air(up: false); // landed
TickFor(tick, ActionPacer.JumpLandMinMs); // the landing check looks from JumpLandMinMs on
FakeHost.Containers[second] = Player;
TickUntil(tick, () => FakeHost.ExternalMoves.Count >= 3, 3000);
Check.Eq(FakeHost.ExternalMoves.Count, 3, "the third move is sent");
int at = FakeHost.Order.IndexOf($"move:{FakeHost.ExternalMoves[2].Id:X8}");
Check.True(at > 0 && FakeHost.Order[at - 1] == "tapjump", "landed: the third jumps again: " + Order());
Check.Eq(FakeHost.TapJumps, 2, "two jumps in all");
});
// The loot half: the pickup's jump is what holds combat and buffs (ActionPacer); loot is not held.
// CombatHookTests has the combat half (Think sends no attack while it holds).
private static void JumpHoldsCombatNotLoot() => Clocked(() =>
{
bool pacer = ActionPacer.Enabled;
try
{
LootUntilFirstMove(jumpEnabled: true, secondGem: false, stamina: 100u, serverGates: true);
Check.Eq(FakeHost.TapJumps, 1, "the pickup jumped");
Check.True(ActionPacer.LootJumpAirborne(_host, out _), "and the loot code noted the jump for combat and buffs");
// The pacer from here on, on this clock, with nothing of other tests' left in it
// (CorpseCloseTests.PacerOn): only the jump can hold anything.
ActionPacer.Reset(); ActionPacer.Enabled = true;
MoveAudit.Reset(); MoveAudit.NowMs = () => _now;
UseAudit.NowMs = () => _now;
ActionPacer.NoteLootJump(_host); // (Reset dropped the pickup's; the same jump, noted again)
var host = _host;
Air(up: true);
Check.True(ActionPacer.LootJumpAirborne(host, out string why), "in the air after the pickup's jump: " + why);
Check.False(ActionPacer.MayStart(host, "Buff", HealNeed.None, out _), "a buff or heal waits");
Check.False(ActionPacer.MayStart(host, "Buff", HealNeed.Emergency, out _), "an emergency heal too (ACE refuses it in the air)");
Check.True(ActionPacer.MayStart(host, "Loot", HealNeed.None, out _), "loot does not");
_now += ActionPacer.JumpLandMinMs;
Check.True(ActionPacer.LootJumpAirborne(host, out _), "still up at the height check");
Air(up: false);
Check.False(ActionPacer.LootJumpAirborne(host, out _), "landed: the hold ends");
Check.True(ActionPacer.MayStart(host, "Buff", HealNeed.None, out _), "the buff goes");
// The cap: a landing never seen still ends the hold at JumpLandMaxMs.
ActionPacer.NoteLootJump(host);
Air(up: true);
_now += ActionPacer.JumpLandMaxMs - 1;
Check.True(ActionPacer.LootJumpAirborne(host, out _), "up, under the cap");
_now += 1;
Check.False(ActionPacer.LootJumpAirborne(host, out _), "the cap ends it");
}
finally
{
ActionPacer.Enabled = pacer; ActionPacer.Reset();
MoveAudit.Reset(); MoveAudit.NowMs = () => Environment.TickCount64;
UseAudit.NowMs = () => Environment.TickCount64;
}
});
// The plugin's portal-space and death paths drop the hold: no landing is coming to end it.
private static void PortalAndDeathEndJumpHold() => Clocked(() =>
{
var oldVitals = FakeHost.Vitals;
try
{
LootUntilFirstMove(jumpEnabled: true, secondGem: false, stamina: 100u, serverGates: true);
var host = _host;
const System.Reflection.BindingFlags Inst = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic;
Air(up: true);
Check.True(ActionPacer.LootJumpAirborne(host, out _), "the pickup's jump holds combat");
FakeHost.Portaling = true;
typeof(RynthAiPlugin).GetMethod("NotePortalForPacer", Inst)!.Invoke(_bot, null);
Check.False(ActionPacer.LootJumpAirborne(host, out _), "portal space ends the hold");
FakeHost.Portaling = false;
typeof(RynthAiPlugin).GetMethod("NotePortalForPacer", Inst)!.Invoke(_bot, null);
ActionPacer.NoteLootJump(host);
Check.True(ActionPacer.LootJumpAirborne(host, out _), "a second jump holds again");
FakeHost.Vitals = (0, 100, 0, 100, 0, 100);
typeof(RynthAiPlugin).GetMethod("HoldForDeath", Inst)!.Invoke(_bot, null);
Check.False(ActionPacer.LootJumpAirborne(host, out _), "the death ends the hold");
}
finally { FakeHost.Vitals = oldVitals; FakeHost.Portaling = false; Air(up: false); }
});
private static string Order() => string.Join(",", FakeHost.Order);
private static void TwoItemJumpsBeforeMove() => Clocked(() =>
{
LootUntilFirstMove(jumpEnabled: true, secondGem: true, stamina: 100u);
Check.Eq(FakeHost.Order.FirstOrDefault() ?? "", "tapjump", "the keyboard jump goes first: " + Order());
Check.True(FakeHost.Order.Count >= 2 && FakeHost.Order[1].StartsWith("move:", StringComparison.Ordinal), "then the move: " + Order());
Check.Eq(FakeHost.Jumps, 0, "never JumpNonAutonomous (0xF7C9, no handler on ACE)");
});
private static void SingleItemJumps() => Clocked(() =>
{
LootUntilFirstMove(jumpEnabled: true, secondGem: false, stamina: 100u);
Check.Eq(FakeHost.Order.FirstOrDefault() ?? "", "tapjump", "single item: the jump goes first: " + Order());
Check.True(FakeHost.Order.Count >= 2 && FakeHost.Order[1].StartsWith("move:", StringComparison.Ordinal), "single item: then the move: " + Order());
Check.Eq(FakeHost.TapJumps, 1, "single item: exactly one tap jump");
Check.Eq(FakeHost.Jumps, 0, "single item: never JumpNonAutonomous");
});
private static void SettingOffNoJump() => Clocked(() =>
{
LootUntilFirstMove(jumpEnabled: false, secondGem: true, stamina: 100u);
Check.Eq(FakeHost.TapJumps + FakeHost.Jumps, 0, "jump-loot off: no jump: " + Order());
});
private static void LowStaminaNoJump() => Clocked(() =>
{
LootUntilFirstMove(jumpEnabled: true, secondGem: true, stamina: 5u);
Check.Eq(FakeHost.TapJumps + FakeHost.Jumps, 0, "stamina 5: no jump: " + Order());
});
}