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

217 lines
11 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, 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); not now while it runs", Runs);
r.Add("back away: the run re-aims away, holds attacks, stops outside the radius plus the margin (autorun off) and re-engages", RunEnds);
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.Eq(rig.Ask(), RynthAiHub.BackAwayNotNow, "a second request while running: not now");
Check.True(FakeHost.Logs.Exists(l => l.Contains("[CombatEvade]") && l.Contains("running away")), "logged as a run");
});
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.False(Think(rig), "clear: 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");
});
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.False(orphan.Combat.BackAwayActive, "the poll ended it once clear");
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)");
}
}