using System; using System.Linq; using RynthCore.Plugin.RynthAi; using RynthCore.Plugin.RynthAi.CreatureData; using RynthCore.Plugin.RynthAi.LegacyUi; using RynthCore.Plugin.Shared; using RynthCore.RynthAiHostTests.Fakes; using RynthCore.RynthAiTests.Harness; namespace RynthCore.RynthAiHostTests.Tests; // Casts that land nothing (player log, 2026-10-11): two Rampagers side by side, every Nether Arc // VII a "predicted kill" on cast #1 (the learned casts-to-kill was 1), so the bot swapped to the // other one and back for 30 minutes and the no-damage judge, which every swap resets, never ran. // A monster swapped off as a predicted kill and then fought again is alive: no more predicted // swaps off it. And each combat cast logs what the server said in the next few seconds. internal static class CastNoDamageTests { private const uint Player = 0x50000D01; private const uint Lb = 0x5B480000; private const uint RampagerWcid = 16916; private const uint Wand = 0x8008AB76; private const int NetherArc7 = 5367; public static void Register(Runner r) { r.Add("no-damage casts: a monster that survives a predicted kill is not predicted again", SurvivorNotPredictedAgain); r.Add("no-damage casts: other monsters still get predicted kills; the memory lapses after 10 min", OthersStillPredicted); r.Add("no-damage casts: the chat after a combat cast is logged, swings at us left out, capped, 4 s", CastChatTrace); r.Add("no-damage casts (flight 63260): 3 magic casts landing nothing -> one short real sidestep, no cast meanwhile; a hit resets; 30 s apart", ResyncSidestep); } private sealed class Rig { public readonly LegacyUiSettings S = new() { MonsterRange = 30 }; public readonly MonsterDamageStore Store = new(); public readonly CombatManager C; public readonly uint A = 0x80165DA5, B = 0x80165DA6, D = 0x80165DA7, E = 0x801753EC; public DateTime Now = new(2026, 10, 11, 10, 55, 34); public Rig() { FakeHost.Reset(); FakeHost.WeaponCalls = true; FakeHost.CombatModeValue = CombatMode.Magic; FakeHost.Names[Player] = "Me"; FakeHost.Positions[Player] = (Lb | 0x018D, 96f, 96f, 0f); FakeHost.PlayerPose = (Lb | 0x018D, 96f, 96f, 0f); uint[] mobs = { A, B, D, E }; for (int i = 0; i < mobs.Length; i++) { FakeHost.Names[mobs[i]] = "Rampager"; FakeHost.Positions[mobs[i]] = (Lb | 0x018D, 96f + i, 98.7f, 0f); FakeHost.ItemTypes[mobs[i]] = 0x10; FakeHost.Wcids[mobs[i]] = RampagerWcid; } var host = FakeHost.Create(); var cache = FakeHost.MakeCache(host, Player, mobs, null); C = new CombatManager(host, S, cache); C.SetPlayerId(Player); C.SetDamageStores(null, Store); C.TargetClock = () => Now; // What the player's store had learned: one Nether Arc VII kills a Rampager. for (int i = 0; i < 3; i++) Store.RecordKill(Wand, RampagerWcid, "Rampager", "Nether", 7, 1, 435); Scan(mobs); } public void Scan(params uint[] ids) => C.SetScannedTargetsForTests(ids.Select(id => new CombatManager.ScannedTarget { Id = unchecked((int)id), Distance = 2.7, Angle = 0, Bearing = double.NaN, Name = "Rampager", })); public void Engage(uint id) => C.EngageForTest(unchecked((int)id)); public bool Cast() => C.CastAndPredictForTest(Wand, "Nether", 7); public void Advance(double seconds) => Now = Now.AddSeconds(seconds); public int LogCount(string part) => FakeHost.Logs.Count(l => l.Contains(part, StringComparison.Ordinal)); } private static void ResyncSidestep() { var rig = new Rig(); FakeHost.SetMotionCalls = true; var host = FakeHost.Create(); var c = new CombatManager(host, rig.S, FakeHost.MakeCache(host, Player, new[] { rig.A, rig.B }, null)); c.SetPlayerId(Player); c.TargetClock = () => rig.Now; MoveAudit.Reset(); c.MagicMissForTest(unchecked((int)rig.A)); c.MagicMissForTest(unchecked((int)rig.B)); // a swap to the other monster doesn't reset the count Check.False(c.ResyncActive, "two misses: nothing yet"); c.ReportDamageOnTarget(unchecked((int)rig.A)); c.MagicMissForTest(unchecked((int)rig.A)); c.MagicMissForTest(unchecked((int)rig.A)); Check.False(c.ResyncActive, "a hit reset the count"); c.MagicMissForTest(unchecked((int)rig.A)); Check.True(c.ResyncActive, "three in a row: a re-sync is wanted"); Check.Eq(rig.LogCount("one short sidestep to re-sync"), 1, "said once"); Check.True(c.TickResyncStep(), "first tick: the step starts, no cast"); Check.True(FakeHost.MotionCalls.SequenceEqual(new[] { (0x6500000Fu, true) }), "sidestep right key down"); rig.Advance(0.2); Check.True(c.TickResyncStep(), "still stepping at 200 ms"); rig.Advance(0.1); Check.True(c.TickResyncStep(), "300 ms: key up, settling"); Check.True(FakeHost.MotionCalls.SequenceEqual(new[] { (0x6500000Fu, true), (0x6500000Fu, false) }), "key up"); rig.Advance(0.4); Check.False(c.TickResyncStep(), "settled: casting may go on"); Check.Eq(c.ResyncSteps, 1, "one step"); for (int i = 0; i < 3; i++) c.MagicMissForTest(unchecked((int)rig.A)); Check.False(c.ResyncActive, "within 30 s: no second step"); rig.Advance(30); for (int i = 0; i < 3; i++) c.MagicMissForTest(unchecked((int)rig.A)); Check.True(c.ResyncActive, "30 s later: another one"); c.TickResyncStep(); Check.True(FakeHost.MotionCalls[2] == (0x65000010u, true), "the other side this time"); } private static void SurvivorNotPredictedAgain() { var r = new Rig(); // The ping-pong as it ran: A predicted dead and swapped off, then B. r.Engage(r.A); Check.True(r.Cast(), "cast #1 at A: predicted kill (learned casts-to-kill 1)"); r.C.PredictedSwapForTest(); Check.Eq(r.LogCount("Predicted kill shot 0x80165DA5"), 1, "the swap off A is logged"); r.Advance(6.3); r.Engage(r.B); Check.True(r.Cast(), "cast #1 at B: predicted kill"); r.C.PredictedSwapForTest(); // A is fought again 13 s later: it didn't die. r.Advance(6.3); r.Engage(r.A); Check.False(r.Cast(), "A survived its predicted kill: no predicted swap off it this time"); Check.Eq(r.LogCount("Predicted kill of 0x80165DA5 'Rampager' didn't happen"), 1, "said once, naming A"); Check.True(r.LogCount("No more predicted swaps off it") == 1, "the line says what changes"); Check.False(r.Cast(), "cast #2 at A: still no predicted swap"); Check.False(r.Cast(), "cast #3 at A: still none"); Check.Eq(r.LogCount("didn't happen"), 1, "the line is not repeated per cast"); // B the same when it comes round. r.Engage(r.B); Check.False(r.Cast(), "B survived too: no predicted swap off it"); Check.Eq(r.LogCount("Predicted kill of 0x80165DA6 'Rampager' didn't happen"), 1, "said once for B"); } private static void OthersStillPredicted() { var r = new Rig(); r.Engage(r.A); r.Cast(); r.C.PredictedSwapForTest(); r.Advance(6); r.Engage(r.A); Check.False(r.Cast(), "A survived: not predicted"); // D was never swapped off: its cast #1 is a predicted kill as before (E is there to swap to). r.Engage(r.D); Check.True(r.Cast(), "a monster that never survived a prediction still gets one"); Check.Eq(r.LogCount("Predicted kill of 0x80165DA7"), 0, "nothing said about D"); // Ten minutes on, A is forgotten (a respawn can reuse the id). r.Advance(11 * 60); r.Engage(r.A); Check.True(r.Cast(), "after 10 min A gets predicted kills again"); } private static void CastChatTrace() { var r = new Rig(); r.C.StartCastChatTrace(NetherArc7, unchecked((int)r.A)); r.Advance(0.25); r.C.TraceCastChat("You say, \"Zhavik Malar\"", 17); r.C.TraceCastChat("You evaded Rampager!", 21); r.C.TraceCastChat("Rampager grazes your lower leg for 9 points of bludgeoning damage!", 21); r.Advance(1.0); r.C.TraceCastChat("Rampager resists your spell", 7); Check.Eq(r.LogCount("[CombatChat]"), 2, "the incantation and the resist are logged, the swing and the evade are not"); Check.True(FakeHost.Logs.Any(l => l.Contains("[CombatChat] +250 ms after", StringComparison.Ordinal) && l.Contains("at 0x80165DA5: type=17", StringComparison.Ordinal)), "the line has the time since the send, the target and the chat type"); Check.True(FakeHost.Logs.Any(l => l.Contains("+1250 ms", StringComparison.Ordinal) && l.Contains("'Rampager resists your spell'", StringComparison.Ordinal)), "the server's answer is quoted"); for (int i = 0; i < 20; i++) r.C.TraceCastChat($"line {i}", 0); Check.Eq(r.LogCount("[CombatChat]"), 8, "at most 8 lines per cast"); // A new cast starts a new trace; nothing is logged once its 4 s are up. r.C.StartCastChatTrace(NetherArc7, unchecked((int)r.B)); r.Advance(4.1); r.C.TraceCastChat("Your spell fizzled.", 7); Check.Eq(r.LogCount("[CombatChat]"), 8, "a line 4.1 s after the send is not logged"); r.C.TraceCastChat("You cast Corruption VII on Rampager", 7); Check.Eq(r.LogCount("[CombatChat]"), 8, "nor any later one until the next cast"); } }