RynthSuite/Plugins/RynthCore.Plugin.RynthAi/RynthAiPlugin.WalkTrip.cs

111 lines
5.1 KiB
C#

using System;
using RynthCore.Plugin.RynthAi.LegacyUi;
namespace RynthCore.Plugin.RynthAi;
/// <summary>
/// A walk-only trip (2026-11-27): "/ra dunnav-to ... walkonly". RynthAi's navigation only walks while the macro
/// runs, so the trip turns the macro on with fighting, looting, buffing and the meta OFF, and puts those four
/// switches (and the macro, when it was off) back exactly as they were when the trip ends: arrived, the macro
/// stopped, nav turned off, another route loaded, or the character left the dungeon. RynthNav's go to someone
/// inside a dungeon hands its last stretch over this way unless "Fight on the way in" is on (RynthNav insideFight).
/// Heals still run (a vital recharge isn't buffing). Tick thread, like the dungeon walks.
/// </summary>
public sealed partial class RynthAiPlugin
{
internal sealed class WalkOnlyTrip
{
public NavRouteParser Route = new();
public string Label = "";
public uint Landblock;
public bool Combat, Looting, Buffing, Meta, MacroWasOn;
}
private WalkOnlyTrip? _walkTrip;
internal const string WalkOnlyWord = "walkonly";
/// <summary>Starts the trip: the player's switches remembered, then fighting, looting, buffing and the meta off.
/// Call before the macro is switched on, so the macro's state remembered is the player's.</summary>
private void BeginWalkTrip(LegacyUiSettings s, NavRouteParser route, uint landblock, string label)
{
// A trip over a trip: the player's switches and macro from before the first, not the first trip's offs.
bool macroWasOn = s.IsMacroRunning;
if (_walkTrip != null) { macroWasOn = _walkTrip.MacroWasOn; RestoreWalkTripSwitches(s, _walkTrip); _walkTrip = null; }
_walkTrip = new WalkOnlyTrip
{
Route = route, Label = label, Landblock = landblock,
Combat = s.EnableCombat, Looting = s.EnableLooting, Buffing = s.EnableBuffing, Meta = s.EnableMeta,
MacroWasOn = macroWasOn,
};
s.EnableCombat = false;
s.EnableLooting = false;
s.EnableBuffing = false;
s.EnableMeta = false;
Host.Log($"[RynthAi] Walk-only trip to {label}: combat {TripOn(_walkTrip.Combat)}, looting {TripOn(_walkTrip.Looting)}, buffing {TripOn(_walkTrip.Buffing)}, meta {TripOn(_walkTrip.Meta)}, macro {TripOn(_walkTrip.MacroWasOn)} remembered; all off for the walk.");
}
/// <summary>Why the trip is over, or null while it carries on. Host-free.</summary>
internal static string? WalkTripEndReason(LegacyUiSettings s, WalkOnlyTrip t, bool havePose, uint landblock)
{
if (!ReferenceEquals(s.CurrentRoute, t.Route)) return "another route was loaded";
if (s.NavRouteFinished()) return "arrived";
if (!s.IsMacroRunning) return "the macro was stopped";
if (!s.EnableNavigation) return "navigation was turned off";
if (havePose && landblock != t.Landblock) return "you left the dungeon";
return null;
}
/// <summary>The trip's end, once a tick (no-op unless one runs).</summary>
private void TickWalkTrip()
{
var t = _walkTrip;
var s = _dashboard?.Settings;
if (t == null || s == null) return;
uint cell = 0;
bool pose = Host.HasGetPlayerPose && Host.TryGetPlayerPose(out cell, out _, out _, out _, out _, out _, out _, out _);
string? why = WalkTripEndReason(s, t, pose, cell >> 16);
if (why == null) return;
// Someone else's route now (a meta, a script, the player): the macro is theirs; only the switches go back.
EndWalkTrip(s, why, stopMacro: why != "another route was loaded");
}
/// <summary>Puts the remembered switches back; the macro goes off again when it was off before the trip.</summary>
private void EndWalkTrip(LegacyUiSettings s, string why, bool stopMacro)
{
var t = _walkTrip;
if (t == null) return;
_walkTrip = null;
RestoreWalkTripSwitches(s, t);
bool stopped = false;
if (stopMacro && !t.MacroWasOn && s.IsMacroRunning)
{
s.SetMacroRunning(false, "the walk-only trip's end (the macro was off before it)");
_navigationEngine?.Stop();
stopped = true;
}
string line = $"[RynthAi] Walk-only trip to {t.Label} ended ({why}): combat {TripOn(t.Combat)}, looting {TripOn(t.Looting)}, buffing {TripOn(t.Buffing)}, meta {TripOn(t.Meta)} as before"
+ (stopped ? ", macro off again." : ".");
Host.Log(line);
ChatLine(line);
}
/// <summary>Shutdown / logout: the switches back before the profile is saved, quietly.</summary>
private void ForgetWalkTrip()
{
var t = _walkTrip;
var s = _dashboard?.Settings;
_walkTrip = null;
if (t != null && s != null) RestoreWalkTripSwitches(s, t);
}
private static void RestoreWalkTripSwitches(LegacyUiSettings s, WalkOnlyTrip t)
{
s.EnableCombat = t.Combat;
s.EnableLooting = t.Looting;
s.EnableBuffing = t.Buffing;
s.EnableMeta = t.Meta;
}
private static string TripOn(bool b) => b ? "on" : "off";
}