using System;
using System.Collections.Generic;
using RynthNav.Routing;
namespace RynthCore.Plugin.RynthNav;
///
/// What a goto (or the panel's Route, or the arrow's route) may use, and the plan: the switches,
/// where you stand, and what the land allows, in one place. Host-free: the route tests drive it.
/// - "Use portals and recalls" off: walking only. Route and Go both respect it (Route used to plan
/// with portals either way).
/// - "Recalls" is part of that: it only counts when "Use portals and recalls" is on.
/// - In a dungeon (no map position) there is no walking start: the route must begin with a recall
/// (your dungeon's coordinates aren't map coordinates; 10-01 a route "walked" from 0,0 to a
/// portal at 86S). With no recall to cast, it refuses and says why.
/// - Walks between landmasses are left out (canWalk); a target only reachable over water needs a
/// portal, a recall or the Town Network.
///
internal static class TravelPlan
{
public sealed class Result
{
/// The legs; null = walk straight there (no teleport helps, or none may be used).
public List? Steps;
public double Est;
public int Teleports;
/// Why there is no way at all ("" = there is one: Steps, or a plain walk).
public string Refusal = "";
/// One line on what was allowed, for the log.
public string Basis = "";
}
public static Result Plan(IReadOnlyList? portals, IReadOnlyList? recalls, HubLinks? hub,
bool portalsOn, bool recallsOn, bool startOnMap,
double startNs, double startEw, double goalNs, double goalEw,
Func? canWalk, bool? landReachable, string goalName,
IReadOnlyList? startHubWalks = null)
{
var r = new Result { Est = NavCoords.Distance(startNs, startEw, goalNs, goalEw) };
recalls ??= Array.Empty();
var usableRecalls = portalsOn && recallsOn ? recalls : Array.Empty();
r.Basis = $"portals {(portalsOn ? "on" : "off")}, recalls {(portalsOn && recallsOn ? $"on ({recalls.Count} known)" : recallsOn ? "on but unused (portals off)" : "off")}, "
+ $"start {(startOnMap ? "on the map" : "in a dungeon")}, over land {(landReachable == null ? "unknown" : landReachable.Value ? "yes" : "no")}";
if (!startOnMap)
{
// No walking start: a recall gets you out, or a walk RynthNav learned in this dungeon to one of its portals.
bool walkOut = false;
if (startHubWalks != null) foreach (double w in startHubWalks) if (!double.IsNaN(w) && w >= 0) { walkOut = true; break; }
if (!portalsOn) { r.Refusal = "you're in a dungeon, so the way out is a recall or a portal: turn on Use portals and recalls"; return r; }
if (!walkOut && !recallsOn) { r.Refusal = "you're in a dungeon, so the way out is a recall: turn on Recalls"; return r; }
if (!walkOut && usableRecalls.Count == 0) { r.Refusal = "you're in a dungeon and no recall is ready to use (/rnav recalls says why)"; return r; }
r.Steps = PortalRoute.Plan(portals ?? Array.Empty(), usableRecalls, hub, startNs, startEw, goalNs, goalEw,
out r.Est, out r.Teleports, canWalk, startOnMap: false, startHubWalks: startHubWalks);
if (double.IsPositiveInfinity(r.Est) || r.Steps.Count == 0)
{
r.Steps = null;
r.Refusal = walkOut ? $"you're in a dungeon and neither its learned portals nor your recalls lead on to {goalName}"
: $"you're in a dungeon and none of your recalls leads on to {goalName}";
}
return r;
}
if (!portalsOn)
{
if (landReachable == false) r.Refusal = $"no walking route to {goalName}: it's on another landmass (water or cliffs in the way); turn on Use portals and recalls";
return r; // walk
}
var steps = PortalRoute.Plan(portals ?? Array.Empty(), usableRecalls, hub, startNs, startEw, goalNs, goalEw,
out double est, out int used, canWalk);
if (double.IsPositiveInfinity(est))
{
r.Refusal = $"no walking route to {goalName}: it's on another landmass (water or cliffs in the way), and none of your portals, "
+ (recallsOn ? "recalls " : "") + "or the Town Network gets there" + (recallsOn ? "" : " (Recalls are off)");
return r;
}
r.Est = est;
if (used == 0)
{
if (landReachable == false) r.Refusal = $"no walking route to {goalName}: it's on another landmass (water or cliffs in the way)";
return r; // walk
}
r.Steps = steps;
r.Teleports = used;
return r;
}
}