using System;
using System.Collections.Generic;
using RynthCore.Plugin.RynthAi.LegacyUi;
namespace RynthCore.Plugin.RynthNav;
///
/// The route maker's geometry that needs no navmesh (RynthNavPlugin.RouteMaker.cs, LoopMaker.cs):
/// how many points a loop of a radius gets, the order a loop's points are walked in (round the
/// centre, then untangled so no two legs cross), and the joined legs as a RynthAi nav route (thinned,
/// heights as nav Z, a circular route not repeating its first point). Host-free and Detour-free:
/// the route tests drive it.
///
internal static class RouteShape
{
/// A loop's legs get a point at least this often (yards), so RynthAi's nav follows the ground.
public const double LoopSpacing = 30.0;
/// A loop's point count when none is asked for: about one every 45 yd of its circle, 4 to 12.
public static int DefaultPoints(double radius) =>
Math.Clamp((int)Math.Round(2 * Math.PI * radius / 45.0), 4, 12);
/// Compass bearing (0 = north, clockwise) from to , 0..360.
public static double Bearing(WorldPt c, WorldPt p)
{
double b = Math.Atan2(p.X - c.X, p.Y - c.Y) * 180.0 / Math.PI;
return b < 0 ? b + 360.0 : b;
}
///
/// The order to walk a loop's points in: clockwise round from the first
/// point north of it, then untangled (2-opt: two legs that cross are uncrossed by walking the
/// stretch between them the other way) until no two straight legs cross. Indexes into
/// .
///
public static int[] OrderLoop(IReadOnlyList pts, WorldPt centre)
{
int n = pts.Count;
var order = new List(n);
for (int i = 0; i < n; i++) order.Add(i);
order.Sort((a, b) =>
{
int c = Bearing(centre, pts[a]).CompareTo(Bearing(centre, pts[b]));
return c != 0 ? c : pts[a].FlatDistance(centre).CompareTo(pts[b].FlatDistance(centre));
});
if (n < 4) return order.ToArray();
int first = order[0];
for (int pass = 0; pass < n * n; pass++)
{
bool changed = false;
for (int i = 0; i < n && !changed; i++)
for (int j = i + 2; j < n && !changed; j++)
{
if (i == 0 && j == n - 1) continue; // the two legs meet at order[0]
WorldPt a = pts[order[i]], b = pts[order[i + 1]], c = pts[order[j]], d = pts[order[(j + 1) % n]];
if (!SegmentsCross(a, b, c, d)) continue;
order.Reverse(i + 1, j - i);
changed = true;
}
if (!changed) break;
}
int at = order.IndexOf(first);
var o = new int[n];
for (int k = 0; k < n; k++) o[k] = order[(at + k) % n];
return o;
}
/// How many pairs of straight legs of the path cross (: the last point joins the first).
public static int Crossings(IReadOnlyList pts, bool closed)
{
int n = pts.Count, legs = closed ? n : n - 1, count = 0;
for (int i = 0; i < legs; i++)
for (int j = i + 1; j < legs; j++)
{
if (j == i + 1 || (closed && i == 0 && j == n - 1)) continue; // neighbours share a point
if (SegmentsCross(pts[i], pts[(i + 1) % n], pts[j], pts[(j + 1) % n])) count++;
}
return count;
}
/// True when the segments a-b and c-d cross (touching at an end doesn't count).
public static bool SegmentsCross(WorldPt a, WorldPt b, WorldPt c, WorldPt d)
{
double d1 = Cross(c, d, a), d2 = Cross(c, d, b), d3 = Cross(a, b, c), d4 = Cross(a, b, d);
return ((d1 > 1e-9 && d2 < -1e-9) || (d1 < -1e-9 && d2 > 1e-9)) && ((d3 > 1e-9 && d4 < -1e-9) || (d3 < -1e-9 && d4 > 1e-9));
}
private static double Cross(WorldPt o, WorldPt a, WorldPt b) => (a.X - o.X) * (b.Y - o.Y) - (a.Y - o.Y) * (b.X - o.X);
///
/// The joined legs as a nav route of : leg k runs from point k to point k+1
/// (first and last points included), each thinned (corners that matter, a point at least every
/// yd). A circular route's last leg ends where the first starts, so that
/// point isn't written twice (RynthAi walks from the last point back to the first).
/// is the walk, a circular route's closing leg included.
///
public static NavRouteParser Assemble(IReadOnlyList> legs, NavRouteType type, double spacing, out double yards)
{
var route = new NavRouteParser { RouteType = type };
yards = 0;
var pts = new List();
foreach (IReadOnlyList leg in legs)
{
if (leg.Count == 0) continue;
List thin = TripPathBuilder.Thin(leg, TripPathBuilder.ThinTolerance, spacing);
for (int i = 1; i < thin.Count; i++) yards += thin[i - 1].FlatDistance(thin[i]);
for (int i = pts.Count == 0 ? 0 : 1; i < thin.Count; i++) pts.Add(thin[i]);
}
if (type == NavRouteType.Circular && pts.Count > 2 && pts[^1].FlatDistance(pts[0]) < TripPathBuilder.DuplicateUnits * 4)
pts.RemoveAt(pts.Count - 1);
double lastH = 0;
foreach (WorldPt p in pts)
{
if (!double.IsNaN(p.H)) lastH = p.H;
route.Points.Add(new NavPoint { Type = NavPointType.Point, NS = p.Ns, EW = p.Ew, Z = lastH / 240.0 });
}
return route;
}
}