360 lines
17 KiB
C#
360 lines
17 KiB
C#
// LegacyDashboardRenderer.NavEdit.cs — route editor commands from the engine Nav panel:
|
|
// (2026-11-29 also: Vendor, NPC and Jump steps, a Jump edited in place, Undo, New route.)
|
|
// multi-select move / duplicate / delete, set active, move a step to the player, nearest,
|
|
// reverse, snap to path, pause / portal / point-at-selection steps, simplify, recording and
|
|
// trail -> route.
|
|
//
|
|
// Commands arrive with indices into the route the panel last saw. Structural edits go
|
|
// through NavRouteEditing.Apply (copy-on-write) so the nav engine never sees a half-edited list.
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace RynthCore.Plugin.RynthAi.LegacyUi;
|
|
|
|
internal sealed partial class LegacyDashboardRenderer
|
|
{
|
|
/// <summary>How long an edit's result stays in the Nav panel's status line.</summary>
|
|
private const long NavEditStatusMs = 8_000;
|
|
|
|
/// <summary>Spot closer than this to an existing waypoint is refused by "Snap to path", yards.</summary>
|
|
private const double SnapMinGapYards = 0.5;
|
|
|
|
/// <summary>Decal ObjectClass.Portal, as VTank writes it in Portal steps.</summary>
|
|
private const int PortalObjectClass = 14;
|
|
|
|
/// <summary>Decal ObjectClass.Npc, for NPC steps added from the Nav panel.</summary>
|
|
private const int NpcObjectClass = 37;
|
|
|
|
/// <summary>A Jump step's hold when the panel doesn't say (as /ra nav addjump: 300 ms, 30% power).</summary>
|
|
internal const int AddJumpDefaultMs = 300;
|
|
|
|
private static double NormalizeDegrees(double d) => ((d % 360.0) + 360.0) % 360.0;
|
|
|
|
private string _navEditStatus = string.Empty;
|
|
private long _navEditStatusAt;
|
|
|
|
/// <summary>Breadcrumb trail (set by the plugin once the nav overlay exists; null before login).</summary>
|
|
internal NavBreadcrumbTracker? NavBreadcrumbs { get; set; }
|
|
|
|
private List<NavPoint> RoutePoints => _settings.CurrentRoute.Points;
|
|
|
|
// Undo for the Nav panel's ✕, Delete and Clear all steps (2026-11-29): the steps the last one took
|
|
// out, each with its place, for the route they came out of. Another route loaded: nothing to undo.
|
|
private List<(int Index, NavPoint Point)>? _navUndo;
|
|
private NavRouteParser? _navUndoRoute;
|
|
|
|
private int NavUndoCount =>
|
|
_navUndo != null && ReferenceEquals(_navUndoRoute, _settings.CurrentRoute) ? _navUndo.Count : 0;
|
|
|
|
/// <summary>Keeps the steps at <paramref name="indices"/> (before they are taken out) for Undo.</summary>
|
|
private void RememberNavDelete(IEnumerable<int> indices)
|
|
{
|
|
var pts = RoutePoints;
|
|
var list = new List<(int, NavPoint)>();
|
|
foreach (int i in indices)
|
|
if (i >= 0 && i < pts.Count) list.Add((i, pts[i]));
|
|
list.Sort((a, b) => a.Item1.CompareTo(b.Item1));
|
|
_navUndo = list.Count > 0 ? list : null;
|
|
_navUndoRoute = _settings.CurrentRoute;
|
|
}
|
|
|
|
/// <summary>Puts the last deleted steps back where they were (lowest index first, so each lands in its place).</summary>
|
|
private void UndoNavDelete()
|
|
{
|
|
if (NavUndoCount == 0) { SetNavEditStatus("Nothing to undo."); return; }
|
|
var undo = _navUndo!;
|
|
_navUndo = null;
|
|
EditRoute(l =>
|
|
{
|
|
foreach (var (i, pt) in undo) l.Insert(Math.Min(i, l.Count), pt);
|
|
return l;
|
|
});
|
|
SetNavEditStatus(undo.Count == 1 ? $"Step {undo[0].Index} is back." : $"{undo.Count} steps are back.");
|
|
}
|
|
|
|
/// <summary>The last edit's result while it is fresh, else empty.</summary>
|
|
private string CurrentNavEditStatus() =>
|
|
_navEditStatus.Length > 0 && Environment.TickCount64 - _navEditStatusAt < NavEditStatusMs ? _navEditStatus : string.Empty;
|
|
|
|
private void SetNavEditStatus(string text)
|
|
{
|
|
_navEditStatus = text;
|
|
_navEditStatusAt = Environment.TickCount64;
|
|
RynthLog.Trace(LogCat.Navigation, "[NavEdit] " + text);
|
|
}
|
|
|
|
private void HandleNavEditCommand(NavCommand cmd)
|
|
{
|
|
switch (cmd.Cmd)
|
|
{
|
|
case "movePoints":
|
|
{
|
|
var sel = PointsAt(cmd.Indices);
|
|
if (sel.Count == 0 || cmd.Delta == 0) return;
|
|
EditRoute(l => NavRouteEditing.Move(l, sel, Math.Sign(cmd.Delta)));
|
|
break;
|
|
}
|
|
case "duplicatePoints":
|
|
{
|
|
var sel = PointsAt(cmd.Indices);
|
|
if (sel.Count == 0) return;
|
|
var copies = new List<NavPoint>();
|
|
EditRoute(l => NavRouteEditing.Duplicate(l, sel, copies));
|
|
SetNavEditStatus($"Duplicated {copies.Count} step(s).");
|
|
break;
|
|
}
|
|
case "deletePoints":
|
|
{
|
|
var sel = PointsAt(cmd.Indices);
|
|
if (sel.Count == 0) return;
|
|
RememberNavDelete(cmd.Indices!);
|
|
EditRoute(l => NavRouteEditing.Remove(l, sel));
|
|
SetNavEditStatus(sel.Count == 1 && cmd.Indices!.Count == 1 ? $"Deleted step {cmd.Indices[0]}." : $"Deleted {sel.Count} steps.");
|
|
break;
|
|
}
|
|
case "undoDelete":
|
|
UndoNavDelete();
|
|
break;
|
|
case "setActive":
|
|
if (cmd.Index < 0 || cmd.Index >= RoutePoints.Count) return;
|
|
_settings.ActiveNavIndex = cmd.Index;
|
|
SaveSettings();
|
|
SetNavEditStatus($"Active waypoint: {cmd.Index}.");
|
|
break;
|
|
|
|
case "movePointHere":
|
|
{
|
|
if (cmd.Index < 0 || cmd.Index >= RoutePoints.Count || !TryNavHere(out NavPoint here)) return;
|
|
NavPoint p = RoutePoints[cmd.Index];
|
|
p.NS = here.NS; p.EW = here.EW; p.Z = here.Z;
|
|
_navigationUi.TryAutoSaveNav();
|
|
SetNavEditStatus($"Step {cmd.Index} moved to your position.");
|
|
break;
|
|
}
|
|
case "nearestActive":
|
|
{
|
|
int i = FindNearestWaypoint(_settings.CurrentRoute);
|
|
if (RoutePoints.Count == 0) return;
|
|
_settings.ActiveNavIndex = i;
|
|
SaveSettings();
|
|
SetNavEditStatus($"Active waypoint: {i} (nearest).");
|
|
break;
|
|
}
|
|
case "reverseRoute":
|
|
if (RoutePoints.Count < 2) return;
|
|
EditRoute(NavRouteEditing.Reverse);
|
|
SetNavEditStatus("Route reversed.");
|
|
break;
|
|
|
|
case "snapToPath":
|
|
{
|
|
if (!TryNavHere(out NavPoint here)) return;
|
|
NavPoint? inserted = null;
|
|
string error = string.Empty;
|
|
EditRoute(l => { NavRouteEditing.TrySnapInsert(l, here.NS, here.EW, SnapMinGapYards, out inserted, out error); return l; });
|
|
SetNavEditStatus(inserted != null ? $"Inserted a point on the path at step {RoutePoints.IndexOf(inserted)}." : error);
|
|
break;
|
|
}
|
|
case "addPause":
|
|
{
|
|
if (!TryNavHere(out NavPoint p)) return;
|
|
p.Type = NavPointType.Pause;
|
|
p.PauseTimeMs = (int)(Math.Clamp(cmd.Seconds, 0.1, 600.0) * 1000);
|
|
InsertNavPoint(p, cmd.AddMode, cmd.InsertAt);
|
|
SetNavEditStatus($"Added a {p.PauseTimeMs / 1000.0:0.#} s pause.");
|
|
break;
|
|
}
|
|
case "addPortal":
|
|
{
|
|
if (!TryDescribeNavSelection(out string name, out double ons, out double oew, out double oz) || !TryNavHere(out NavPoint p)) return;
|
|
p.Type = NavPointType.PortalNPC;
|
|
p.TargetName = name;
|
|
p.ObjectClass = PortalObjectClass;
|
|
p.PortalExitNS = ons; p.PortalExitEW = oew; p.PortalExitZ = oz;
|
|
InsertNavPoint(p, cmd.AddMode, cmd.InsertAt);
|
|
SetNavEditStatus($"Added Portal step '{name}'.");
|
|
break;
|
|
}
|
|
case "addPointAtSelected":
|
|
{
|
|
if (!TryDescribeNavSelection(out string name, out double ns, out double ew, out double z)) return;
|
|
InsertNavPoint(new NavPoint { Type = NavPointType.Point, NS = ns, EW = ew, Z = z }, cmd.AddMode, cmd.InsertAt);
|
|
SetNavEditStatus($"Added a point at '{name}'.");
|
|
break;
|
|
}
|
|
// ── The new Nav panel (2026-11-29): Vendor, NPC and Jump buttons beside Add waypoint here,
|
|
// a Jump step's facing / power / walk edited in place, and New route. ──
|
|
case "addVendor":
|
|
{
|
|
if (!TryDescribeNavSelection(out string name, out _, out _, out _) || !TryNavHere(out NavPoint p)) return;
|
|
p.Type = NavPointType.OpenVendor;
|
|
p.TargetName = name;
|
|
p.VendorId = _host.HasGetSelectedItemId ? _host.GetSelectedItemId() : 0;
|
|
InsertNavPoint(p, cmd.AddMode, cmd.InsertAt);
|
|
SetNavEditStatus($"Added a Vendor step: open '{name}'.");
|
|
break;
|
|
}
|
|
case "addNpc":
|
|
{
|
|
if (!TryDescribeNavSelection(out string name, out double ons, out double oew, out double oz) || !TryNavHere(out NavPoint p)) return;
|
|
p.Type = NavPointType.Npc;
|
|
p.TargetName = name;
|
|
p.ObjectClass = NpcObjectClass;
|
|
p.PortalExitNS = ons; p.PortalExitEW = oew; p.PortalExitZ = oz;
|
|
InsertNavPoint(p, cmd.AddMode, cmd.InsertAt);
|
|
SetNavEditStatus($"Added an NPC step: talk to '{name}'.");
|
|
break;
|
|
}
|
|
case "addJump":
|
|
{
|
|
// Where you stand, facing the way you face unless the panel says (as /ra nav addjump).
|
|
if (!TryNavHere(out NavPoint p)) return;
|
|
double heading = cmd.Heading;
|
|
if (heading < 0)
|
|
{
|
|
if (!PlayerCompass.TryGet(_host, out float face)) { SetNavEditStatus("Could not read which way you face."); return; }
|
|
heading = face;
|
|
}
|
|
p.Type = NavPointType.Jump;
|
|
p.JumpHeading = Math.Round(NormalizeDegrees(heading), 1);
|
|
p.JumpMs = cmd.Ms < 0 ? AddJumpDefaultMs : Math.Clamp(Math.Round(cmd.Ms), 0, 1000);
|
|
p.JumpShift = cmd.Shift;
|
|
InsertNavPoint(p, cmd.AddMode, cmd.InsertAt);
|
|
if (_settings.IsRecordingNav) NavBreadcrumbs?.JumpAdded(); // recording waits until you land
|
|
SetNavEditStatus($"Added a Jump step: facing {p.JumpHeading:0}°, {p.JumpMs / 10:0}% power{(p.JumpShift ? ", walking" : "")}.");
|
|
break;
|
|
}
|
|
case "setJump":
|
|
{
|
|
if (cmd.Index < 0 || cmd.Index >= RoutePoints.Count || RoutePoints[cmd.Index].Type != NavPointType.Jump) return;
|
|
NavPoint p = RoutePoints[cmd.Index];
|
|
// "Mine": the way the character faces now.
|
|
if (cmd.Text == "face")
|
|
{
|
|
if (!PlayerCompass.TryGet(_host, out float face)) { SetNavEditStatus("Could not read which way you face."); return; }
|
|
p.JumpHeading = Math.Round(NormalizeDegrees(face), 1);
|
|
}
|
|
else if (cmd.Heading >= 0) p.JumpHeading = Math.Round(NormalizeDegrees(cmd.Heading), 1);
|
|
if (cmd.Ms >= 0) p.JumpMs = Math.Clamp(Math.Round(cmd.Ms), 0, 1000);
|
|
p.JumpShift = cmd.Shift;
|
|
_navigationUi.TryAutoSaveNav();
|
|
SetNavEditStatus($"Step {cmd.Index}: jump facing {p.JumpHeading:0}°, {p.JumpMs / 10:0}% power{(p.JumpShift ? ", walking" : "")}.");
|
|
break;
|
|
}
|
|
case "newRoute":
|
|
// An empty, unsaved route: the loaded file on disk is not changed.
|
|
_settings.IsRecordingNav = false;
|
|
_settings.CurrentRoute = new NavRouteParser { RouteType = NavRouteType.Once };
|
|
_settings.CurrentNavPath = string.Empty;
|
|
_settings.ActiveNavIndex = 0;
|
|
SaveSettings();
|
|
SetNavEditStatus("New route: empty and unsaved. Add steps, then Save as.");
|
|
break;
|
|
|
|
case "simplifyRoute":
|
|
{
|
|
double tol = Math.Clamp(cmd.Yards, 0.2, 10.0);
|
|
int ai = _settings.ActiveNavIndex;
|
|
NavPoint? keep = ai >= 0 && ai < RoutePoints.Count ? RoutePoints[ai] : null;
|
|
int removed = 0;
|
|
EditRoute(l => { removed = NavRouteEditing.Simplify(l, tol, keep); return l; });
|
|
SetNavEditStatus($"Simplify ({tol:0.#} yd) removed {removed} point(s).");
|
|
break;
|
|
}
|
|
case "setRecording":
|
|
_settings.IsRecordingNav = cmd.On;
|
|
SetNavEditStatus(cmd.On ? "Recording: walk the route; points are added for you." : "Recording stopped.");
|
|
break;
|
|
|
|
case "trailToRoute":
|
|
SetNavEditStatus(TrailToRoute(cmd.Reverse, null, out _));
|
|
break;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// The breadcrumb trail (its newest unbroken stretch) becomes the current route, walked again or
|
|
/// backwards (<paramref name="reverse"/>), and with <paramref name="saveAs"/> it is saved as
|
|
/// NavProfiles\<name>.nav (report R-20261129-210845-0574: "a nav route made from your
|
|
/// breadcrumbs"; the Nav panel's Tools menu had it, /ra navtrail route|back gives it a name too).
|
|
/// Returns the line to show; <paramref name="made"/> is false when no route was made.
|
|
/// </summary>
|
|
internal string TrailToRoute(bool reverse, string? saveAs, out bool made)
|
|
{
|
|
made = false;
|
|
NavCrumb[] trail = NavBreadcrumbs?.Trail ?? Array.Empty<NavCrumb>();
|
|
List<NavPoint> pts = NavBreadcrumbTracker.TrailToPoints(trail, _settings.NavOverlay.RecordSpacingYards, reverse);
|
|
if (pts.Count < 2)
|
|
return _settings.NavOverlay.TrackBreadcrumbs
|
|
? "The trail is too short to make a route: walk a while first."
|
|
: "No trail to make a route from: turn Breadcrumbs on (/ra navtrail on) and walk a while.";
|
|
_settings.IsRecordingNav = false;
|
|
// A new unsaved route: the loaded file on disk is not changed (Save As keeps it).
|
|
_settings.CurrentRoute = new NavRouteParser { RouteType = NavRouteType.Once, Points = pts };
|
|
_settings.CurrentNavPath = string.Empty;
|
|
_settings.ActiveNavIndex = 0;
|
|
made = true;
|
|
string what = $"{(reverse ? "Backtrack" : "Trail")} route created: {pts.Count} points";
|
|
if (string.IsNullOrWhiteSpace(saveAs))
|
|
{
|
|
RefreshNavFiles();
|
|
SaveSettings();
|
|
return what + " (unsaved).";
|
|
}
|
|
return what + ". " + SaveRoute(saveAs).Replace("[RynthAi] ", "");
|
|
}
|
|
|
|
/// <summary>Copy-on-write route edit, then the route file autosave.</summary>
|
|
private void EditRoute(Func<List<NavPoint>, List<NavPoint>> edit)
|
|
{
|
|
NavRouteEditing.Apply(_settings, edit);
|
|
_navigationUi.TryAutoSaveNav();
|
|
}
|
|
|
|
/// <summary>The route's points at <paramref name="indices"/>; out-of-range indices are skipped.</summary>
|
|
private HashSet<NavPoint> PointsAt(List<int>? indices)
|
|
{
|
|
var sel = new HashSet<NavPoint>();
|
|
if (indices == null) return sel;
|
|
var pts = RoutePoints;
|
|
foreach (int i in indices)
|
|
if (i >= 0 && i < pts.Count) sel.Add(pts[i]);
|
|
return sel;
|
|
}
|
|
|
|
/// <summary>A Point at the player's position (Z in nav units), or false when the position is unknown.</summary>
|
|
private bool TryNavHere(out NavPoint p)
|
|
{
|
|
p = new NavPoint();
|
|
if (!NavCoordinateHelper.TryGetNavPosition(_host, out double ns, out double ew, out double z))
|
|
{
|
|
SetNavEditStatus("Position unavailable.");
|
|
return false;
|
|
}
|
|
p.NS = ns; p.EW = ew; p.Z = z;
|
|
return true;
|
|
}
|
|
|
|
/// <summary>Name and nav position of the selected world object.</summary>
|
|
private bool TryDescribeNavSelection(out string name, out double ns, out double ew, out double navZ)
|
|
{
|
|
name = string.Empty; ns = ew = navZ = 0;
|
|
if (!_host.HasGetSelectedItemId) { SetNavEditStatus("Selection unavailable."); return false; }
|
|
uint sel = _host.GetSelectedItemId();
|
|
if (sel == 0) { SetNavEditStatus("Select an object first."); return false; }
|
|
if (!_host.HasGetObjectName || !_host.TryGetObjectName(sel, out name) || string.IsNullOrWhiteSpace(name))
|
|
{
|
|
SetNavEditStatus("The selected object has no name yet.");
|
|
return false;
|
|
}
|
|
if (_host.HasGetObjectPosition
|
|
&& _host.TryGetObjectPosition(sel, out uint cell, out float x, out float y, out float z)
|
|
&& NavCoordinateHelper.TryConvertPoseToCoords(cell, x, y, out ns, out ew))
|
|
{
|
|
navZ = z / NavCoordinateHelper.NavZScale;
|
|
return true;
|
|
}
|
|
SetNavEditStatus($"Could not read the position of '{name}'.");
|
|
return false;
|
|
}
|
|
}
|