326 lines
16 KiB
C#
326 lines
16 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Text;
|
|
using RynthNav.Routing;
|
|
|
|
namespace RynthCore.Plugin.RynthNav;
|
|
|
|
// Portals a server added that the world data (NavData's portals.tsv) doesn't have (2026-11-27). Custom servers
|
|
// add their own, so routes never used them. Found two ways:
|
|
// * /rnav portals scan [yd] (or the panel's Scan): every portal object the client knows around you is written
|
|
// down where it stands;
|
|
// * taking one: the teleport that follows says where it lands. A scanned portal becomes a route link the first
|
|
// time it's used; one you take without scanning is added then.
|
|
// Kept per server (the world name the client logged into), so one server's portals never route a character on
|
|
// another. A portal on the map that lands on the map is a plain PortalLink for the route planner (PortalRoute,
|
|
// our own graph: the same Dijkstra and costs as portals.tsv's). A landing inside a dungeon is kept and listed but
|
|
// not routed: there is no map to walk on from it. Portals standing inside dungeons aren't written down.
|
|
// Saved in NavData\custom-portals.txt, one tab-separated line per portal, the server first. No host calls: the
|
|
// route tests compile this file. Spotted by SilentKelpie (Forgejo rynth PR #12); re-implemented here.
|
|
|
|
/// <summary>One server's portal that the world data doesn't have.</summary>
|
|
internal sealed class CustomPortal
|
|
{
|
|
public string Server = "", Name = "";
|
|
public uint Wcid;
|
|
/// <summary>Where it stands: its cell and landblock-local x, y, z, and the map coordinates.</summary>
|
|
public uint Cell;
|
|
public double X, Y, Z;
|
|
public double Ns, Ew;
|
|
/// <summary>Where it lands, learned by taking it. DestKnown false = only scanned.</summary>
|
|
public bool DestKnown, DestOnMap;
|
|
public double DestNs, DestEw;
|
|
public uint DestCell;
|
|
public DateTime SeenUtc, LearnedUtc;
|
|
|
|
/// <summary>Both ends on the map: a route link.</summary>
|
|
public bool Routable => DestKnown && DestOnMap;
|
|
|
|
/// <summary>The route planner's link: open to anyone (the server's rules aren't known; a refusal is learned as for any portal).</summary>
|
|
public PortalLink ToLink() => new(Ns, Ew, DestNs, DestEw, Name, 0, 0, PortalRules.Unrestricted, Cell, X, Y, Z);
|
|
|
|
public string Where => DestKnown
|
|
? DestOnMap ? NavCoords.Fmt(DestNs, DestEw) : string.Create(CultureInfo.InvariantCulture, $"a dungeon (landblock {DestCell >> 16:X4}, not routed)")
|
|
: "not taken yet";
|
|
|
|
internal string Key => CustomPortalStore.KeyOf(Server, Name, Ns, Ew);
|
|
}
|
|
|
|
/// <summary>What a scan did with one portal object.</summary>
|
|
internal enum CustomSeen { Added, Refreshed, BuiltIn }
|
|
|
|
/// <summary>What taking a portal told the store.</summary>
|
|
internal enum CustomLearn { New, LandingChanged, AlreadyKnown, BuiltIn, NotAPortal }
|
|
|
|
internal sealed class CustomPortalStore
|
|
{
|
|
/// <summary>A portal object of the same name this close (yards) to a saved one is that one.</summary>
|
|
public const double SameUnits = 8.0;
|
|
/// <summary>A portals.tsv portal this close already routes it (any name within half of it).</summary>
|
|
public const double BuiltInUnits = 12.0;
|
|
/// <summary>A map landing this close (yards) to the known one is the same landing (landings scatter a little).</summary>
|
|
public const double SameLandingUnits = 30.0;
|
|
/// <summary>A "landing" this close to where the portal stands isn't a teleport through it.</summary>
|
|
public const double TooCloseUnits = 30.0;
|
|
public const int MaxPerServer = 2000;
|
|
|
|
private readonly List<CustomPortal> _all = new();
|
|
private readonly HashSet<string> _forgotten = new(StringComparer.OrdinalIgnoreCase);
|
|
|
|
/// <summary>Bumped on every change (the plugin saves and rebuilds its route links when it moves).</summary>
|
|
public int Version { get; private set; }
|
|
|
|
public IReadOnlyList<CustomPortal> All => _all;
|
|
|
|
public List<CustomPortal> For(string server)
|
|
{
|
|
string k = Server(server);
|
|
return _all.FindAll(p => p.Server.Equals(k, StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
|
|
/// <summary>This server's route links (both ends on the map).</summary>
|
|
public List<PortalLink> Links(string server)
|
|
{
|
|
var links = new List<PortalLink>();
|
|
foreach (CustomPortal p in For(server)) if (p.Routable) links.Add(p.ToLink());
|
|
return links;
|
|
}
|
|
|
|
public (int Saved, int Routable) Counts(string server)
|
|
{
|
|
int saved = 0, routable = 0;
|
|
foreach (CustomPortal p in For(server)) { saved++; if (p.Routable) routable++; }
|
|
return (saved, routable);
|
|
}
|
|
|
|
public static string Server(string server) => string.IsNullOrWhiteSpace(server) ? "?" : server.Trim();
|
|
|
|
internal static string KeyOf(string server, string name, double ns, double ew) =>
|
|
string.Create(CultureInfo.InvariantCulture, $"{Server(server)}|{name}|{Math.Round(ns, 1):0.0}|{Math.Round(ew, 1):0.0}");
|
|
|
|
/// <summary>The saved portal of that name standing within SameUnits of (ns, ew), or null.</summary>
|
|
public CustomPortal? Find(string server, string name, double ns, double ew)
|
|
{
|
|
string k = Server(server);
|
|
foreach (CustomPortal p in _all)
|
|
if (p.Server.Equals(k, StringComparison.OrdinalIgnoreCase) && p.Name.Equals(name, StringComparison.OrdinalIgnoreCase)
|
|
&& NavCoords.Distance(p.Ns, p.Ew, ns, ew) <= SameUnits) return p;
|
|
return null;
|
|
}
|
|
|
|
/// <summary>A portals.tsv portal at that spot: the same name within BuiltInUnits, or any within half of it.</summary>
|
|
public static bool IsBuiltIn(IReadOnlyList<PortalLink>? builtIn, string name, double ns, double ew)
|
|
{
|
|
if (builtIn == null) return false;
|
|
foreach (PortalLink b in builtIn)
|
|
{
|
|
double d = NavCoords.Distance(b.SrcNs, b.SrcEw, ns, ew);
|
|
if (d > BuiltInUnits) continue;
|
|
if (d <= BuiltInUnits / 2) return true;
|
|
if (b.Name.Length > 0 && name.Length > 0
|
|
&& (b.Name.Contains(name, StringComparison.OrdinalIgnoreCase) || name.Contains(b.Name, StringComparison.OrdinalIgnoreCase)))
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>A portal object on the map, seen by a scan: written down, or its sighting refreshed.</summary>
|
|
public CustomSeen Saw(string server, string name, uint wcid, uint cell, double x, double y, double z, double ns, double ew,
|
|
IReadOnlyList<PortalLink>? builtIn, DateTime nowUtc)
|
|
{
|
|
name = Clean(name);
|
|
CustomPortal? p = Find(server, name, ns, ew);
|
|
if (p == null && IsBuiltIn(builtIn, name, ns, ew)) return CustomSeen.BuiltIn;
|
|
if (p != null)
|
|
{
|
|
bool moved = p.Cell != cell || Math.Abs(p.X - x) > 0.05 || Math.Abs(p.Y - y) > 0.05 || Math.Abs(p.Z - z) > 0.05 || (wcid != 0 && p.Wcid != wcid);
|
|
p.SeenUtc = nowUtc;
|
|
if (moved) { Place(p, wcid, cell, x, y, z, ns, ew); Version++; }
|
|
return CustomSeen.Refreshed;
|
|
}
|
|
p = new CustomPortal { Server = Server(server), Name = name, SeenUtc = nowUtc };
|
|
Place(p, wcid, cell, x, y, z, ns, ew);
|
|
Add(p);
|
|
return CustomSeen.Added;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The map portal you stood at (name, where it stands) took you to the landing given (on the map, or a
|
|
/// dungeon cell). A portals.tsv portal is left alone (BuiltIn); a "landing" right beside it isn't a teleport
|
|
/// through it (NotAPortal). Otherwise it is added if new and its landing set; AlreadyKnown when the landing is
|
|
/// the one it had (nothing written: taking a known portal says nothing).
|
|
/// </summary>
|
|
public CustomLearn Learned(string server, string name, uint wcid, uint cell, double x, double y, double z, double ns, double ew,
|
|
bool destOnMap, double destNs, double destEw, uint destCell, IReadOnlyList<PortalLink>? builtIn, DateTime nowUtc,
|
|
out CustomPortal? portal)
|
|
{
|
|
portal = null;
|
|
name = Clean(name);
|
|
if (destOnMap && NavCoords.Distance(ns, ew, destNs, destEw) < TooCloseUnits) return CustomLearn.NotAPortal;
|
|
CustomPortal? p = Find(server, name, ns, ew);
|
|
bool added = false;
|
|
if (p == null)
|
|
{
|
|
if (IsBuiltIn(builtIn, name, ns, ew)) return CustomLearn.BuiltIn;
|
|
p = new CustomPortal { Server = Server(server), Name = name, SeenUtc = nowUtc };
|
|
Place(p, wcid, cell, x, y, z, ns, ew);
|
|
Add(p);
|
|
added = true;
|
|
}
|
|
portal = p;
|
|
p.SeenUtc = nowUtc;
|
|
if (!added && SameLanding(p, destOnMap, destNs, destEw, destCell)) return CustomLearn.AlreadyKnown;
|
|
CustomLearn result = added || !p.DestKnown ? CustomLearn.New : CustomLearn.LandingChanged;
|
|
p.DestKnown = true;
|
|
p.DestOnMap = destOnMap;
|
|
p.DestNs = destOnMap ? Math.Round(destNs, 2) : 0;
|
|
p.DestEw = destOnMap ? Math.Round(destEw, 2) : 0;
|
|
p.DestCell = destCell;
|
|
p.LearnedUtc = nowUtc;
|
|
Version++;
|
|
return result;
|
|
}
|
|
|
|
/// <summary>The landing <paramref name="p"/> has is this one: map, within SameLandingUnits; a dungeon, the same landblock.</summary>
|
|
internal static bool SameLanding(CustomPortal p, bool destOnMap, double destNs, double destEw, uint destCell)
|
|
{
|
|
if (!p.DestKnown || p.DestOnMap != destOnMap) return false;
|
|
return destOnMap ? NavCoords.Distance(p.DestNs, p.DestEw, destNs, destEw) <= SameLandingUnits : (p.DestCell >> 16) == (destCell >> 16);
|
|
}
|
|
|
|
/// <summary>Forgets this server's portals whose name is <paramref name="name"/> (or, with none exact, contains it). How many.</summary>
|
|
public int Forget(string server, string name)
|
|
{
|
|
string k = Server(server), n = name.Trim();
|
|
if (n.Length == 0) return 0;
|
|
bool exact = _all.Exists(p => p.Server.Equals(k, StringComparison.OrdinalIgnoreCase) && p.Name.Equals(n, StringComparison.OrdinalIgnoreCase));
|
|
int removed = 0;
|
|
for (int i = _all.Count - 1; i >= 0; i--)
|
|
{
|
|
CustomPortal p = _all[i];
|
|
if (!p.Server.Equals(k, StringComparison.OrdinalIgnoreCase)) continue;
|
|
if (exact ? !p.Name.Equals(n, StringComparison.OrdinalIgnoreCase) : !p.Name.Contains(n, StringComparison.OrdinalIgnoreCase)) continue;
|
|
_forgotten.Add(p.Key);
|
|
_all.RemoveAt(i);
|
|
removed++;
|
|
}
|
|
if (removed > 0) Version++;
|
|
return removed;
|
|
}
|
|
|
|
/// <summary>Forgets every portal of this server. How many.</summary>
|
|
public int Clear(string server)
|
|
{
|
|
string k = Server(server);
|
|
int removed = 0;
|
|
for (int i = _all.Count - 1; i >= 0; i--)
|
|
{
|
|
if (!_all[i].Server.Equals(k, StringComparison.OrdinalIgnoreCase)) continue;
|
|
_forgotten.Add(_all[i].Key);
|
|
_all.RemoveAt(i);
|
|
removed++;
|
|
}
|
|
if (removed > 0) Version++;
|
|
return removed;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Every client writes the one file: what another client saved since is taken in before a save (a portal this
|
|
/// client doesn't have, or a landing learned later than ours), except what this client forgot.
|
|
/// </summary>
|
|
public void MergeFrom(CustomPortalStore disk)
|
|
{
|
|
foreach (CustomPortal d in disk._all)
|
|
{
|
|
if (_forgotten.Contains(d.Key)) continue;
|
|
CustomPortal? mine = Find(d.Server, d.Name, d.Ns, d.Ew);
|
|
if (mine == null) { Add(d); continue; }
|
|
if (d.DestKnown && (!mine.DestKnown || d.LearnedUtc > mine.LearnedUtc))
|
|
{
|
|
mine.DestKnown = true; mine.DestOnMap = d.DestOnMap; mine.DestNs = d.DestNs; mine.DestEw = d.DestEw;
|
|
mine.DestCell = d.DestCell; mine.LearnedUtc = d.LearnedUtc;
|
|
Version++;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void Add(CustomPortal p)
|
|
{
|
|
if (For(p.Server).Count >= MaxPerServer)
|
|
{
|
|
int oldest = _all.FindIndex(o => o.Server.Equals(p.Server, StringComparison.OrdinalIgnoreCase));
|
|
if (oldest >= 0) _all.RemoveAt(oldest);
|
|
}
|
|
_all.Add(p);
|
|
_forgotten.Remove(p.Key);
|
|
Version++;
|
|
}
|
|
|
|
private static void Place(CustomPortal p, uint wcid, uint cell, double x, double y, double z, double ns, double ew)
|
|
{
|
|
if (wcid != 0) p.Wcid = wcid;
|
|
p.Cell = cell; p.X = Math.Round(x, 2); p.Y = Math.Round(y, 2); p.Z = Math.Round(z, 2);
|
|
p.Ns = Math.Round(ns, 3); p.Ew = Math.Round(ew, 3);
|
|
}
|
|
|
|
/// <summary>A name as one field: no tabs or line breaks, trimmed.</summary>
|
|
internal static string Clean(string name)
|
|
{
|
|
var sb = new StringBuilder(name.Length);
|
|
foreach (char c in name) sb.Append(c is '\t' or '\r' or '\n' ? ' ' : c);
|
|
return sb.ToString().Trim();
|
|
}
|
|
|
|
// ── custom-portals.txt ───────────────────────────────────────────────
|
|
// server, name, wcid, cell (hex), x, y, z, ns, ew, landing ("map" | "dungeon" | "-"), destNs, destEw,
|
|
// destCell (hex), seen, learned (UTC, round-trip). Lines starting with # are notes.
|
|
|
|
public string Serialize()
|
|
{
|
|
var ci = CultureInfo.InvariantCulture;
|
|
var sb = new StringBuilder();
|
|
sb.Append("# RynthNav: custom servers' own portals (server, name, wcid, cell, x, y, z, ns, ew, landing, destNs, destEw, destCell, seen, learned)\n");
|
|
foreach (CustomPortal p in _all)
|
|
{
|
|
string landing = !p.DestKnown ? "-" : p.DestOnMap ? "map" : "dungeon";
|
|
sb.Append(p.Server).Append('\t').Append(p.Name).Append('\t').Append(p.Wcid.ToString(ci)).Append('\t')
|
|
.Append(p.Cell.ToString("X8", ci)).Append('\t').Append(p.X.ToString("0.##", ci)).Append('\t')
|
|
.Append(p.Y.ToString("0.##", ci)).Append('\t').Append(p.Z.ToString("0.##", ci)).Append('\t')
|
|
.Append(p.Ns.ToString("0.###", ci)).Append('\t').Append(p.Ew.ToString("0.###", ci)).Append('\t')
|
|
.Append(landing).Append('\t').Append(p.DestNs.ToString("0.##", ci)).Append('\t').Append(p.DestEw.ToString("0.##", ci)).Append('\t')
|
|
.Append(p.DestCell.ToString("X8", ci)).Append('\t').Append(p.SeenUtc.ToString("o", ci)).Append('\t')
|
|
.Append(p.LearnedUtc.ToString("o", ci)).Append('\n');
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
|
|
public static CustomPortalStore Parse(string text)
|
|
{
|
|
var store = new CustomPortalStore();
|
|
var ci = CultureInfo.InvariantCulture;
|
|
const NumberStyles num = NumberStyles.Float;
|
|
foreach (string raw in text.Split('\n'))
|
|
{
|
|
string line = raw.TrimEnd('\r');
|
|
if (line.Length == 0 || line[0] == '#') continue;
|
|
string[] f = line.Split('\t');
|
|
if (f.Length < 15) continue;
|
|
var p = new CustomPortal { Server = Server(f[0]), Name = Clean(f[1]) };
|
|
if (p.Name.Length == 0
|
|
|| !uint.TryParse(f[2], NumberStyles.Integer, ci, out p.Wcid)
|
|
|| !uint.TryParse(f[3], NumberStyles.HexNumber, ci, out p.Cell)
|
|
|| !double.TryParse(f[4], num, ci, out p.X) || !double.TryParse(f[5], num, ci, out p.Y) || !double.TryParse(f[6], num, ci, out p.Z)
|
|
|| !double.TryParse(f[7], num, ci, out p.Ns) || !double.TryParse(f[8], num, ci, out p.Ew)
|
|
|| !double.TryParse(f[10], num, ci, out p.DestNs) || !double.TryParse(f[11], num, ci, out p.DestEw)
|
|
|| !uint.TryParse(f[12], NumberStyles.HexNumber, ci, out p.DestCell)) continue;
|
|
p.DestKnown = f[9] is "map" or "dungeon";
|
|
p.DestOnMap = f[9] == "map";
|
|
DateTime.TryParse(f[13], ci, DateTimeStyles.RoundtripKind, out p.SeenUtc);
|
|
DateTime.TryParse(f[14], ci, DateTimeStyles.RoundtripKind, out p.LearnedUtc);
|
|
if (store.Find(p.Server, p.Name, p.Ns, p.Ew) != null) continue; // one line per portal
|
|
store._all.Add(p);
|
|
}
|
|
return store;
|
|
}
|
|
}
|