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

191 lines
8.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using RynthCore.Plugin.RynthAi;
using RynthCore.Plugin.RynthAi.CreatureData;
using RynthCore.Plugin.RynthAi.LegacyUi;
using RynthCore.RynthAiHostTests.Fakes;
using RynthCore.RynthAiTests.Harness;
namespace RynthCore.RynthAiHostTests.Tests;
// R-20261127-144058-8cb2: "when a monster is close to death it will switch to another prior to
// finishing off its target, then once the next monster is dead it will finish off the one it was
// originally targetting". The third Force Bolt at a Guardian Armoredillo was the predicted kill
// (hp~1800, dealt~1393): the swap locked a fresh Tamed Armoredillo, the bolt was resisted 640 ms
// later, and with Target Lock on the bot fought the fresh one for 13 s before it went back to the
// Guardian, at 23%. Now the survivor takes the lock back once it is known to be alive.
internal static class PredictedKillReclaimTests
{
private const uint Player = 0x50000F41;
private const uint Lb = 0x88650000;
private const uint GuardianWcid = 3101, TamedWcid = 3102;
private const string Guardian = "Guardian Armoredillo", Tamed = "Tamed Armoredillo";
private const BindingFlags Any = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
public static void Register(Runner r)
{
r.Add("predicted kill (R-20261127 8cb2): resisted after the swap -> the lock goes back to it", ResistedTakesLockBack);
r.Add("predicted kill (R-20261127 8cb2): still alive when the swap's window ends -> back to it", AliveAtWindowEndTakesLockBack);
r.Add("predicted kill (R-20261127 8cb2): it died after all -> the new target is kept", DiedKeepsNewTarget);
r.Add("predicted kill (R-20261127 8cb2): alive only after 10 s -> the new target is kept", LateSurvivalKeepsNewTarget);
r.Add("predicted kill (R-20261127 8cb2): the armed cast resisted calls the swap off; a same-named target's miss doesn't reclaim", ArmedResistAndSameName);
}
private sealed class Rig
{
public readonly LegacyUiSettings S = new() { MonsterRange = 10, TargetSelection = 1, TargetLock = true, EnableCombat = true };
public CombatManager C = null!;
public WorldObjectCache Cache = null!;
public DateTime Now = RynthClock.Now;
private readonly List<uint> _ids = new();
private uint _next = 0x80000601;
public Rig()
{
FakeHost.Reset();
FakeHost.WeaponCalls = true;
FakeHost.CombatModeValue = CombatMode.Magic;
FakeHost.Names[Player] = "Me";
FakeHost.Positions[Player] = (Lb | 0x1E, 96f, 96f, 0f);
FakeHost.PlayerPose = (Lb | 0x1E, 96f, 96f, 0f);
}
public uint Mob(string name, uint wcid, float north, float east = 0)
{
uint id = _next++;
FakeHost.Names[id] = name;
FakeHost.Positions[id] = (Lb | 0x1E, 96f + east, 96f + north, 0f);
FakeHost.ItemTypes[id] = 0x10;
FakeHost.Wcids[id] = wcid;
_ids.Add(id);
return id;
}
public void Build(uint fightTarget)
{
var host = FakeHost.Create();
Cache = FakeHost.MakeCache(host, Player, _ids);
C = new CombatManager(host, S, Cache);
C.SetPlayerId(Player);
C.SetDamageStores(null, new MonsterDamageStore());
C.TargetClock = () => Now;
C.EngageForTest(unchecked((int)fightTarget));
}
public void Rescan()
{
typeof(CombatManager).GetField("_lastScanTime", Any)!.SetValue(C, DateTime.MinValue);
C.ScanNearbyTargets();
}
/// <summary>The predicted kill's cast was accepted: the swap off the target, then the next lock.</summary>
public void SwapAndLockNext()
{
C.ArmPredictedSwapForTest(castAgoMs: 2000);
C.PredictedSwapForTest();
Rescan();
C.HandleCombatTrigger();
}
public void Tick() { Rescan(); C.HandleCombatTrigger(); }
public int Target => C.activeTargetId;
public int LogCount(string part) => FakeHost.Logs.Count(l => l.Contains(part, StringComparison.Ordinal));
}
private static int Id(uint id) => unchecked((int)id);
private static void ResistedTakesLockBack()
{
var r = new Rig();
uint guardian = r.Mob(Guardian, GuardianWcid, 3);
uint tamed = r.Mob(Tamed, TamedWcid, 3.5f, 0.5f);
r.Build(guardian);
r.SwapAndLockNext();
Check.Eq(r.Target, Id(tamed), "the swap locks the Tamed Armoredillo");
r.Now = r.Now.AddMilliseconds(640);
r.C.HandleChatForDamage("Guardian Armoredillo resists your spell", 7);
Check.Eq(r.LogCount("Predicted kill of 0x80000601 'Guardian Armoredillo' resisted an attack after the swap"), 1, "the resist says it lives");
r.Tick();
Check.Eq(r.Target, Id(guardian), "the Guardian takes the lock back (Target Lock on)");
Check.Eq(r.LogCount("survived: back to it"), 1, "the switch says why");
r.Tick();
Check.Eq(r.Target, Id(guardian), "and keeps it: no trading back");
}
private static void AliveAtWindowEndTakesLockBack()
{
var r = new Rig();
uint guardian = r.Mob(Guardian, GuardianWcid, 3);
uint tamed = r.Mob(Tamed, TamedWcid, 3.5f, 0.5f);
r.Build(guardian);
r.SwapAndLockNext();
Check.Eq(r.Target, Id(tamed), "the swap locks the Tamed Armoredillo");
r.Now = r.Now.AddMilliseconds(1500);
r.Tick();
Check.Eq(r.Target, Id(tamed), "inside the swap's window nothing is known yet");
r.Now = r.Now.AddMilliseconds(600);
r.Tick();
Check.Eq(r.LogCount("still alive: it is a target again"), 1, "alive when the window ends");
Check.Eq(r.Target, Id(guardian), "the Guardian takes the lock back");
}
private static void DiedKeepsNewTarget()
{
var r = new Rig();
uint guardian = r.Mob(Guardian, GuardianWcid, 3);
uint tamed = r.Mob(Tamed, TamedWcid, 3.5f, 0.5f);
r.Build(guardian);
r.SwapAndLockNext();
r.Cache.OnUpdateHealth(guardian, 0f); // the server says it died
r.Now = r.Now.AddMilliseconds(2100);
r.Tick();
Check.Eq(r.Target, Id(tamed), "a dead prediction: the Tamed Armoredillo stays the target");
Check.Eq(r.LogCount("survived: back to it"), 0, "no switch");
}
private static void LateSurvivalKeepsNewTarget()
{
var r = new Rig();
uint guardian = r.Mob(Guardian, GuardianWcid, 3);
uint tamed = r.Mob(Tamed, TamedWcid, 3.5f, 0.5f);
r.Build(guardian);
r.SwapAndLockNext();
r.Now = r.Now.AddMilliseconds(CombatManager.ReclaimWindowMs + 500);
r.Tick();
Check.Eq(r.Target, Id(tamed), "10 s on, the fight with the Tamed Armoredillo is kept (Target Lock on)");
Check.Eq(r.LogCount("survived: back to it"), 0, "no switch");
}
private static void ArmedResistAndSameName()
{
var r = new Rig();
uint guardian = r.Mob(Guardian, GuardianWcid, 3);
r.Mob(Tamed, TamedWcid, 3.5f, 0.5f);
r.Build(guardian);
r.C.ArmPredictedSwapForTest(castAgoMs: 2600);
r.C.HandleChatForDamage("Guardian Armoredillo resists your spell", 7);
Check.False(r.C.PredictedSwapArmed, "its own cast was resisted: the swap is called off");
Check.Eq(r.LogCount("called off: it resisted the cast"), 1, "and logged");
Check.Eq(r.Target, Id(guardian), "still on the Guardian");
// Two of a name: the swap went from one Tamed Armoredillo to the other; a resist naming
// "Tamed Armoredillo" may be the new target's, so it proves nothing about the first.
var s = new Rig();
uint first = s.Mob(Tamed, TamedWcid, 3);
uint second = s.Mob(Tamed, TamedWcid, 3.5f, 0.5f);
s.Build(first);
s.SwapAndLockNext();
Check.Eq(s.Target, Id(second), "the swap locks the other one");
s.C.HandleChatForDamage("Tamed Armoredillo resists your spell", 7);
s.Tick();
Check.Eq(s.Target, Id(second), "a same-named miss takes nothing back");
Check.Eq(s.LogCount("resisted an attack after the swap"), 0, "and isn't taken as the first one's");
}
}