RynthSuite/Plugins/RynthCore.Plugin.RynthNav/RynthNavPlugin.Follow.cs

74 lines
3.8 KiB
C#

using System;
namespace RynthCore.Plugin.RynthNav;
// Walks keep to the trip's own path (TripFollow.cs, 2026-11-27): each re-plan of a walking step aims a few yards
// along the walk the path markers draw, rather than at the route graph's next crossing. On by default;
// /rnav follow on|off (rynthnav.json followTrip; the status JSON's "follow"). The trip's path is worked out for a
// goto with follow on even when the markers are off. Tick thread.
// Spotted by SilentKelpie (Forgejo rynth PR #12); re-implemented here.
public sealed partial class RynthNavPlugin
{
private volatile bool _followTrip = true;
private TripFollow? _follow;
private bool _offTripSaid;
private void LoadFollowSettings() => _followTrip = NavDataConfig.ReadSwitch("followTrip", true);
/// <summary>The trip's path is wanted for walking (follow on, a goto's own trip).</summary>
private bool FollowWantsPath(PlannedTrip? trip) => _followTrip && trip != null && trip.Live;
/// <summary>/rnav follow [on|off]</summary>
private void CmdFollow(string rest)
{
string a = rest.Trim().ToLowerInvariant();
if (a is "on" or "off")
{
_followTrip = a == "on";
NavDataConfig.WriteSwitch("followTrip", _followTrip);
_follow = null;
lock (_gotoGate) _gotoPath = null; // re-plan the walk in hand the new way
if (_followTrip && _gotoActive) StartPathBuild();
}
else if (a.Length > 0) { SayFail("/rnav follow on|off — walks keep to the trip's path (the markers), or plan their own way"); return; }
Say(_followTrip ? "follow ON: walks keep to the trip's path (the markers)"
: "follow OFF: walks plan their own way to the route's crossings");
}
/// <summary>
/// The walk to follow for the running goto's step, or null: follow off, no live trip, its path not worked out
/// yet, not on the map, no walk for this step, or a walk that is only a straight line (nothing to keep to).
/// </summary>
private TripFollow? CurrentFollow()
{
PlannedTrip? trip = _trip;
TripPath? path = _tripPath;
if (!_followTrip || trip == null || !trip.Live || path == null || path.TripId != trip.Id || !IsOnMap(_cellId)) return null;
int step = _route != null ? _routeIdx : 0;
if (step < 0 || step >= path.Walks.Length || path.Walks[step] is not { Length: >= 2 } walk) return null;
if (_follow != null && _follow.TripId == trip.Id && _follow.Step == step) return _follow;
string tag = $"step {step + 1}:";
foreach (string n in path.Notes)
if (n.StartsWith(tag, StringComparison.Ordinal) && n.Contains("straight line", StringComparison.Ordinal)) return null;
_follow = new TripFollow(trip.Id, step, walk);
_offTripSaid = false;
Host.Log($"[RynthNav] walk: keeping to the trip's path for step {step + 1} ({_follow.Length:F0}u, {walk.Length} points)");
return _follow;
}
/// <summary>Replan: the steer along the trip's path, when this step has one and its end isn't next. Null = plan as before.</summary>
private (float Ew, float Up, float Ns)? FollowSteer()
{
if (CurrentFollow() is not TripFollow follow) return null;
WorldPt aim = follow.Aim(_wx, _wy, out double off, out bool atEnd);
if (atEnd) return null;
if (off > TripFollow.OffUnits && !_offTripSaid)
{
_offTripSaid = true;
Host.Log($"[RynthNav] walk: {off:F0}u off the trip's path at {NavCoords.Fmt(NavCoords.NsFromWorld(_wy), NavCoords.EwFromWorld(_wx))}: heading back to it");
}
else if (off <= TripFollow.OffUnits) _offTripSaid = false;
double up = double.IsNaN(aim.H) ? _wz : aim.H;
return ((float)aim.X, (float)up, (float)aim.Y);
}
}