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

277 lines
14 KiB
C#

using System;
using RynthCore.Plugin.RynthAi;
using RynthCore.Plugin.RynthAi.LegacyUi;
using RynthCore.Plugin.Shared;
using RynthCore.RynthAiHostTests.Fakes;
using RynthCore.RynthAiTests.Harness;
namespace RynthCore.RynthAiHostTests.Tests;
// The back-away combat does on a server plugin's request (CombatManager.BackAwayFrom, RynthAi.Hub
// BackAwayFrom). It was the Empyrean Handler lethal-ring evade (Silentkelpie's PR 12) until 2026-10-20,
// when the Handler's detection and its setting went to RynthILT (Infinite Leaftide content); RynthAi
// keeps the generic part: stop attacking, run away (face directly away, autorun until outside the radius
// plus a margin, re-aiming as it goes), hold attacks until clear and the stay-out is over (the danger goes
// off a few seconds after the request; R-20261204-122802-7806), then re-engage. A single back-jump until
// 2026-11-27 (it covers a yard or two); it stays only for an engine without heading snap / autorun
// (spotted by SilentKelpie, Forgejo rynth PR #12). Gated: only while the macro is running with combat on,
// and only inside the radius (at least Ring Range). RynthAi alone never reacts to the Handler's line.
internal static class BackAwayTests
{
private const uint Player = 0x50000D11;
private const uint Danger = 0x80300110;
private const uint Lb = 0xA9B40000;
private const string RingLine = "The handler briefly channels infused Empyrean hatred to release a lethal ring.";
public static void Register(Runner r)
{
r.Add("back away: inside the radius with the macro running and combat on, runs away (faces away, autorun on, no back-jump); the same danger again renews it", Runs);
r.Add("back away: the run re-aims away, holds attacks, stops outside the radius plus the margin (autorun off) and re-engages after the stay-out", RunEnds);
r.Add("back away: clear early, it stands out until the danger has gone off, runs again if it comes closer, and a second request renews the stay-out", StaysOut);
r.Add("back away: the run ends with autorun off when the macro stops, combat goes off, the danger is gone or it times out", RunStops);
r.Add("back away: an engine without heading snap or autorun gets the old back-jump", JumpFallback);
r.Add("back away: not with the macro stopped or combat off", Gated);
r.Add("back away: outside the radius (Ring Range widens it), or an object without a position: no jump", NotThese);
r.Add("back away: RynthAi alone ignores the Empyrean Handler's ring line (RynthILT's now)", HandlerLineIgnored);
}
private sealed class Rig
{
public readonly LegacyUiSettings S = new() { MonsterRange = 40, EnableCombat = true };
public readonly CombatManager Combat;
public int Jumps;
public float Heading = -1;
public bool Busy;
public Rig(float dangerYards, bool place = true, Action? beforeCreate = null)
{
FakeHost.Reset();
FakeHost.Names[Player] = "Evader";
FakeHost.Positions[Player] = (Lb | 0x1C, 96f, 96f, 0f);
FakeHost.PlayerPose = (Lb | 0x1C, 96f, 96f, 0f);
FakeHost.Names[Danger] = "Empyrean Handler";
if (place) FakeHost.Positions[Danger] = (Lb | 0x1C, 96f, 96f + dangerYards, 0f);
FakeHost.ItemTypes[Danger] = 0x10;
beforeCreate?.Invoke();
var host = FakeHost.Create(Player);
var cache = FakeHost.MakeCache(host, Player, new[] { Danger });
S.IsMacroRunning = true;
Combat = new CombatManager(host, S, cache);
Combat.SetPlayerId(Player);
Combat.ScanNearbyTargets();
Combat.RequestBackJump = (h, ms) => { Jumps++; Heading = h; Busy = true; return true; };
Combat.JumpBusy = () => Busy;
}
public int Ask(double radius = 12, int holdMs = 750) => Combat.BackAwayFrom(unchecked((int)Danger), radius, holdMs, "test");
}
private static readonly System.Reflection.BindingFlags Any = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic;
private static DateTime _now = new(2026, 11, 27, 12, 0, 0, DateTimeKind.Utc);
/// <summary>Runs <paramref name="body"/> on a test clock (RynthClock.TestNow), restored after.</summary>
private static void OnClock(Action body)
{
RynthClock.TestNow = () => _now;
try { body(); }
finally { RynthClock.TestNow = null; }
}
private static bool Think(Rig rig) => (bool)typeof(CombatManager).GetMethod("TickBackAway", Any)!.Invoke(rig.Combat, null)!;
/// <summary>Puts the character <paramref name="south"/> yards south of where it started.</summary>
private static void MoveSouth(float south)
{
FakeHost.Positions[Player] = (Lb | 0x1C, 96f, 96f - south, 0f);
FakeHost.PlayerPose = (Lb | 0x1C, 96f, 96f - south, 0f);
}
private static void Runs() => OnClock(() =>
{
var rig = new Rig(dangerYards: 5);
Check.Eq(rig.Ask(), RynthAiHub.BackAwayStarted, "5 yards inside 12: started");
Check.Eq(rig.Jumps, 0, "no back-jump (a jump covers a yard or two)");
Check.True(FakeHost.AutoRun, "autorun on");
Check.True(Math.Abs(FakeHost.HeadingDeg - 180.0) < 1.0, $"faces directly away (the danger is north; heading {FakeHost.HeadingDeg:0})");
Check.True(rig.Combat.BackAwayActive, "combat holds while it runs");
Check.True(FakeHost.Logs.Exists(l => l.Contains("[CombatEvade]") && l.Contains("running away")), "logged as a run");
Check.Eq(rig.Ask(), RynthAiHub.BackAwayStarted, "the same danger again while running: renewed");
Check.True(FakeHost.Logs.Exists(l => l.Contains("again") && l.Contains("staying out")), "logged as renewed");
Check.Eq(rig.Jumps, 0, "still no back-jump");
});
private static void RunEnds() => OnClock(() =>
{
var rig = new Rig(dangerYards: 5);
rig.Ask(radius: 12);
_now = _now.AddMilliseconds(100);
Check.True(Think(rig), "Think holds attacks while it runs");
Check.Eq(rig.Combat.LastCombatSkipReason, "back-away-run", "and says why");
// Halfway, a little to the east: it re-aims away from the danger.
FakeHost.Positions[Player] = (Lb | 0x1C, 100f, 90f, 0f);
FakeHost.PlayerPose = (Lb | 0x1C, 100f, 90f, 0f);
FakeHost.HeadingDeg = 0;
_now = _now.AddMilliseconds(300);
Check.True(Think(rig), "still inside 12 + 4: holding");
Check.True(FakeHost.HeadingDeg > 150 && FakeHost.HeadingDeg < 180, $"re-aimed away from the danger (heading {FakeHost.HeadingDeg:0})");
Check.True(FakeHost.AutoRun, "still running");
MoveSouth(12f); // 17 yards from it: outside 12 + 4
_now = _now.AddMilliseconds(300);
Check.True(Think(rig), "clear early: still holding (the danger hasn't gone off yet)");
Check.False(FakeHost.AutoRun, "standing: autorun off");
Check.True(rig.Combat.BackAwayActive, "still held");
_now = _now.AddMilliseconds(CombatManager.BackAwayStayOutMs);
Check.False(Think(rig), "clear and the stay-out is over: Think re-engages");
Check.False(FakeHost.AutoRun, "autorun off");
Check.False(rig.Combat.BackAwayActive, "no longer held");
Check.True(FakeHost.Logs.Exists(l => l.Contains("Back away done") && l.Contains("clear at")), "logged as clear");
Check.Eq(rig.Ask(), RynthAiHub.BackAwayOutside, "a new request out here: outside");
});
// R-20261204-122802-7806: clear 1.5 s after the request, re-engaged at once, walked back into the ring
// and died at 4 s. Now it stands clear until the stay-out is over.
private static void StaysOut() => OnClock(() =>
{
var rig = new Rig(dangerYards: 5);
DateTime start = _now;
rig.Ask(radius: 12);
MoveSouth(12.2f); // 17.2 yards from it at 1.5 s
_now = start.AddMilliseconds(1_540);
Check.True(Think(rig), "clear at 1.5 s: attacks still held");
Check.False(FakeHost.AutoRun, "standing clear: autorun off");
Check.True(FakeHost.Logs.Exists(l => l.Contains("standing clear")), "logged as standing");
_now = start.AddMilliseconds(2_500);
Check.True(Think(rig), "2.5 s: still standing out");
Check.False(FakeHost.AutoRun, "autorun stays off while it stands");
// The danger comes closer: 15.6 yards is inside 16 but not 1 yard inside: still standing.
FakeHost.Positions[Danger] = (Lb | 0x1C, 96f, 96f - 12.2f + 15.6f, 0f);
_now = start.AddMilliseconds(2_800);
Check.True(Think(rig), "a little closer: still held");
Check.False(FakeHost.AutoRun, "less than a yard inside: still standing");
// 14 yards: runs again, away from it.
FakeHost.Positions[Danger] = (Lb | 0x1C, 96f, 96f - 12.2f + 14f, 0f);
FakeHost.HeadingDeg = 0;
_now = start.AddMilliseconds(3_100);
Check.True(Think(rig), "closer: still held");
Check.True(FakeHost.AutoRun, "runs again");
Check.True(Math.Abs(FakeHost.HeadingDeg - 180.0) < 1.0, $"away from it (heading {FakeHost.HeadingDeg:0})");
Check.True(FakeHost.Logs.Exists(l => l.Contains("running again")), "logged");
// Clear again at 4 s, then the same danger is asked about again at 4.5 s: the stay-out starts over.
MoveSouth(12.2f + 4f); // 18 yards
_now = start.AddMilliseconds(4_000);
Check.True(Think(rig), "clear again: held");
Check.False(FakeHost.AutoRun, "standing");
_now = start.AddMilliseconds(4_500);
Check.Eq(rig.Ask(radius: 12), RynthAiHub.BackAwayStarted, "a second request while standing out: renewed");
_now = start.AddMilliseconds(6_000);
Check.True(Think(rig), "6 s: the renewed stay-out still holds");
_now = start.AddMilliseconds(9_600);
Check.False(Think(rig), "past the renewed stay-out: re-engages");
Check.False(rig.Combat.BackAwayActive, "over");
Check.False(FakeHost.AutoRun, "autorun off");
});
private static void RunStops() => OnClock(() =>
{
var macro = new Rig(5);
macro.Ask();
macro.S.IsMacroRunning = false;
macro.Combat.PollBackAway();
Check.False(FakeHost.AutoRun, "macro stopped: autorun off");
Check.False(macro.Combat.BackAwayActive, "macro stopped: over");
var combat = new Rig(5);
combat.Ask();
combat.S.EnableCombat = false;
combat.Combat.PollBackAway();
Check.False(FakeHost.AutoRun, "combat off: autorun off");
Check.False(combat.Combat.BackAwayActive, "combat off: over");
var gone = new Rig(5);
gone.Ask();
FakeHost.Positions.Remove(Danger);
_now = _now.AddMilliseconds(100);
Check.False(Think(gone), "the danger is gone: re-engage");
Check.False(FakeHost.AutoRun, "gone: autorun off");
var stuck = new Rig(5);
stuck.Ask();
_now = _now.AddMilliseconds(CombatManager.BackAwayRunMaxMs + 100);
Check.False(Think(stuck), "blocked (no progress): it times out");
Check.False(FakeHost.AutoRun, "timed out: autorun off");
Check.True(FakeHost.Logs.Exists(l => l.Contains("run timed out")), "logged");
// Think not driving it (another activity has the tick): the poll steps it, so it still ends when clear.
var orphan = new Rig(5);
orphan.Ask();
MoveSouth(12f);
_now = _now.AddMilliseconds(500);
orphan.Combat.PollBackAway();
Check.True(orphan.Combat.BackAwayActive, "the poll stood it clear (stay-out not over)");
Check.False(FakeHost.AutoRun, "standing: autorun off");
_now = _now.AddMilliseconds(CombatManager.BackAwayStayOutMs);
orphan.Combat.PollBackAway();
Check.False(orphan.Combat.BackAwayActive, "the poll ended it once clear and the stay-out was over");
Check.False(FakeHost.AutoRun, "autorun off");
});
private static void JumpFallback() => OnClock(() =>
{
FakeHost.Reset();
var rig = new Rig(5, beforeCreate: () => FakeHost.NoTurnOrAutoRun = true);
Check.Eq(rig.Ask(), RynthAiHub.BackAwayStarted, "started");
Check.Eq(rig.Jumps, 1, "one back-jump");
Check.False(FakeHost.AutoRun, "no autorun");
Check.True(rig.Combat.BackAwayActive, "combat holds while it jumps");
Check.Eq(rig.Ask(), RynthAiHub.BackAwayNotNow, "a second request while backing away: not now");
Check.Eq(rig.Jumps, 1, "still one jump");
var refused = new Rig(5, beforeCreate: () => FakeHost.NoTurnOrAutoRun = true);
refused.Combat.RequestBackJump = (_, _) => false;
Check.Eq(refused.Ask(), RynthAiHub.BackAwayJumpRefused, "the jumper refused: said so");
Check.False(refused.Combat.BackAwayActive, "and combat isn't held");
});
private static void Gated()
{
var stopped = new Rig(5);
stopped.S.IsMacroRunning = false;
Check.Eq(stopped.Ask(), RynthAiHub.BackAwayNotNow, "macro stopped: not now");
Check.Eq(stopped.Jumps, 0, "no jump");
var noCombat = new Rig(5);
noCombat.S.EnableCombat = false;
Check.Eq(noCombat.Ask(), RynthAiHub.BackAwayNotNow, "combat off: not now");
Check.Eq(noCombat.Jumps, 0, "no jump");
}
private static void NotThese()
{
var far = new Rig(30);
Check.Eq(far.Ask(), RynthAiHub.BackAwayOutside, "30 yards away is outside 12");
Check.Eq(far.Jumps, 0, "no jump");
far.S.RingRange = 40;
Check.Eq(far.Ask(), RynthAiHub.BackAwayStarted, "Ring Range 40 widens the radius: 30 yards is inside");
var unknown = new Rig(5, place: false);
Check.Eq(unknown.Ask(), RynthAiHub.BackAwayUnknownObject, "no position for the object");
Check.Eq(unknown.Jumps, 0, "no jump");
}
private static void HandlerLineIgnored()
{
var rig = new Rig(5);
rig.Combat.HandleChatForDamage(RingLine, 0x0C);
rig.Combat.HandleChatForDebuffs(RingLine);
Check.Eq(rig.Jumps, 0, "the ring line alone does nothing in RynthAi (no server plugin asked)");
Check.False(rig.Combat.BackAwayActive, "and combat isn't held");
Check.True(new LegacyUiSettings().HandlerRingEvade, "the old profile field still loads (on by default; unused, for RynthILT's import)");
}
}