using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using RynthNav.Routing;
namespace RynthCore.Plugin.RynthNav;
// The Town Network on a goto: the route planner takes it like a portal hub (entry portal → an
// arrival point inside → the walk to an exit portal → out), and here the walker follows the stored
// walk inside (NavData\townnet.json), where there is no navmesh. The entry portal is an ordinary
// portal step (StepPortalWait); the hub step is everything from landing inside to landing at the
// exit's destination. Without townnet.json (or with one that fails its checks) routes don't use the
// network and everything walks as before. Tick thread, except the background load.
public sealed partial class RynthNavPlugin
{
private const uint StypeLevel = 25, StypeHeritageGroup = 188;
private const int HeritageOlthoi = 12, HeritageOlthoiAcid = 13;
private const double HubOffRouteUnits = HubWalker.OffRouteUnits;
private const long HubLandTimeoutMs = 20000; // the entry portal's teleport never lands inside
private const long HubPortalSpaceTimeoutMs = 60000; // ...or never ends
private const long HubSettleMs = 1500; // out of portal space and inside this long = landed
/// townnet.json as loaded (null: none, or it failed its checks).
private volatile TownNet? _baseTownNet;
/// What routes and the walker use: townnet.json plus this server's learned dungeons (TownNet.Merge). Null: neither.
private volatile TownNet? _townNet;
private (TownNet? Base, int Version, string Server, Atlas? Atlas, bool Dats, int Retry, double Drop, int Locked) _mergedKey = (null, -1, "", null, false, 0, -1, -1);
private int _mergeRetry; // bumped to plan again (the dats weren't loaded the last time)
private long _mergeRetryAtMs;
private volatile string _townNetStatus = "loading…";
private HubLinks? _hubLinks;
private (TownNet? Net, int Level, bool Olthoi) _hubLinksKey;
// The hub step being walked (tick thread).
private bool _hubActive, _hubLanded, _hubWaitSaid;
// Leaving by a portal into another dungeon (a chain): the jump and portal space seen, still inside a hub landblock.
private bool _hubLeaving, _hubLeftPortal;
private long _hubLeaveMs;
private uint _hubLb;
private double _hubLastX, _hubLastY;
private long _hubSettleSince; // when the landing settled (inside, out of portal space); 0 = not yet
private long _hubStepMs;
private HubWalker? _hubWalker;
private bool HubIsWaiting => _hubWalker?.Waiting == true;
private void LoadTownNet()
{
string path = Path.Combine(NavDataDir, TownNet.FileName);
Task.Run(() =>
{
try
{
if (!File.Exists(path)) { _townNetStatus = "no townnet.json: routes don't use the Town Network"; return; }
TownNet? t = TownNet.Parse(File.ReadAllText(path), out string why);
if (t == null)
{
_townNetStatus = $"townnet.json ignored ({why}): routes don't use the Town Network";
Host.Log($"[RynthNav] {_townNetStatus}");
return;
}
_baseTownNet = t;
_townNetStatus = $"{t.Entries.Count} entry portals, {t.Exits.Count} exits (set {t.SetId[..Math.Min(8, t.SetId.Length)]})";
Host.Log($"[RynthNav] Town Network: {t}");
}
catch (Exception ex)
{
_townNetStatus = $"townnet.json failed to load ({ex.GetType().Name}: {ex.Message}): routes don't use the Town Network";
Host.Log($"[RynthNav] {_townNetStatus}");
}
});
}
///
/// Rebuilds the route hub when townnet.json loads, the custom portals change, or the server does:
/// the Town Network plus this server's learned dungeons. A running hub step keeps its exit number
/// (learned exits are only ever appended while walking). Tick thread.
///
private void RefreshMergedNet()
{
if (!_customLoaded) { if (_townNet == null && _baseTownNet != null) _townNet = _baseTownNet; return; }
TownNet? b = _baseTownNet;
string server = _server;
Atlas atlas = _atlas;
bool dats = DungeonPathAvailable();
if (_mergeRetryAtMs != 0 && NowMs >= _mergeRetryAtMs) { _mergeRetryAtMs = 0; _mergeRetry++; }
// The drop setting and the locked doors found change the planned walks (2026-10-30).
var key = (b, _custom.Version, server, (Atlas?)atlas, dats, _mergeRetry, _dropMeters, _lockedDoorVersion);
if (key == _mergedKey) return;
_mergedKey = key;
bool anyDungeonPortal = false;
if (server.Length > 0) foreach (CustomPortal p in _custom.For(server)) if (!p.OnMap && p.DestKnown) { anyDungeonPortal = true; break; }
if (server.Length == 0 || (_custom.WalksFor(server).Count == 0 && !(dats && anyDungeonPortal))) { _townNet = b; return; }
_dunPathNoData = false;
TownNet merged = TownNet.Merge(b, _custom, server, atlas, dats ? DungeonPath : null);
var (e, x, w) = merged.LearnedCounts();
int planned = merged.PlannedWalks();
_townNet = merged.Exits.Count == 0 ? b : merged;
Host.Log($"[RynthNav] learned dungeons for {server}: {e} entry portal(s), {x} exit portal(s), {w - planned} walk(s) inside"
+ (dats ? $" + {planned} planned from the dats (RynthAi's dungeon pathfinder)" : " (RynthAi's dungeon pathfinder not available: walked ones only)"));
if (_dunPathNoData) _mergeRetryAtMs = NowMs + 15000; // some dungeon wasn't readable yet: plan again shortly
}
/// The character's level (0 = not known) and whether they're an Olthoi. Tick thread.
private void ReadCharacterRules(out int level, out bool olthoi)
{
level = 0; olthoi = false;
uint me = Host.HasGetPlayerId ? Host.GetPlayerId() : 0;
if (me == 0 || !Host.HasGetObjectIntProperty) return;
if (Host.TryGetObjectIntProperty(me, StypeLevel, out int lv) && lv > 0) level = lv;
if (Host.TryGetObjectIntProperty(me, StypeHeritageGroup, out int h)) olthoi = h is HeritageOlthoi or HeritageOlthoiAcid;
}
/// The Town Network as this character's route planner sees it; null without townnet.json.
private HubLinks? CurrentHubLinks(List? notes)
{
TownNet? t = _townNet;
if (t == null) return null;
ReadCharacterRules(out int level, out bool olthoi);
if (notes == null && _hubLinks != null && ReferenceEquals(_hubLinksKey.Net, t) && _hubLinksKey.Level == level && _hubLinksKey.Olthoi == olthoi)
return _hubLinks;
HubLinks links = t.Links(level, olthoi, notes);
_hubLinks = links;
_hubLinksKey = (t, level, olthoi);
return links;
}
private bool CurrentStepIsHub => _route != null && _routeIdx < _route.Count && _route[_routeIdx].IsHub;
/// The exit the route's step takes (a hub step), or null.
private TownNetExit? HubExitOf(int i)
{
TownNet? t = _townNet;
if (t == null || _route == null || i < 0 || i >= _route.Count || !_route[i].IsHub) return null;
int x = _route[i].HubExit;
return x >= 0 && x < t.Exits.Count ? t.Exits[x] : null;
}
/// The next step takes the Town Network (this one walks to its entry portal).
private bool NextStepIsHub => _route != null && _routeIdx + 1 < _route.Count && _route[_routeIdx + 1].IsHub;
private static string HubWhere(TownNetExit? x) => x?.Where ?? "the next town";
/// "the Town Network", "the Abandoned Mine": the place a hub step walks through, for what RynthNav says.
private static string TheHub(TownNetExit? x) => TheHub(x?.Hub ?? "Town Network");
private static string TheHub(string hub) =>
hub.StartsWith("the ", StringComparison.OrdinalIgnoreCase) || hub.StartsWith("dungeon ", StringComparison.Ordinal) ? hub : "the " + hub;
/// The exit of hub step in the route hub, or null.
private TownNetExit? HubExitOf(RouteStep s)
{
TownNet? t = _townNet;
return t != null && s.IsHub && s.HubExit >= 0 && s.HubExit < t.Exits.Count ? t.Exits[s.HubExit] : null;
}
/// Where a hub exit takes you: a spot on the map, or the dungeon it leads into.
private string HubExitDest(TownNetExit? x)
{
if (x == null) return "the next place";
if (x.DestOnMap) return NavCoords.Fmt(x.DestNs, x.DestEw) + (x.DestTown.Length > 0 ? $" ({x.DestTown})" : "");
return TheHub(DungeonName(x.DestLandblock));
}
/// A dungeon landblock's name: the route hub's (learned dungeons, the Atlas's), else "dungeon LLLL".
private string DungeonName(uint lb)
{
TownNet? t = _townNet;
if (t != null)
foreach (TownNetExit e in t.Exits)
if ((e.Cell >> 16) == lb && e.Hub.Length > 0 && !e.Hub.StartsWith("dungeon ", StringComparison.Ordinal)) return e.Hub;
if (_baseTownNet?.Contains(lb) == true) return "Town Network";
string hex = lb.ToString("X4", System.Globalization.CultureInfo.InvariantCulture);
foreach (AtlasLocation l in _atlas.Locations)
if (l.Type.Equals("Dungeon", StringComparison.Ordinal) && l.Lb.Equals(hex, StringComparison.OrdinalIgnoreCase)) return l.Name;
return $"dungeon {hex}";
}
private void ResetHub()
{
_hubActive = false; _hubLanded = false; _hubWaitSaid = false; _hubSettleSince = 0;
_hubWalker = null;
_hubLeaving = false; _hubLeftPortal = false; _hubLeaveMs = 0; _hubLb = 0;
ResetDoor();
}
private double LocalX => _wx - ((_cellId >> 24) & 0xFF) * 192.0;
private double LocalY => _wy - ((_cellId >> 16) & 0xFF) * 192.0;
private void HubHalt() => AutoRun.Halt();
/// One tick of a hub step: wait for the landing inside, walk to the exit, take it.
private void StepHub()
{
long now = NowMs;
TownNet? t = _townNet;
TownNetExit? exit = HubExitOf(_routeIdx);
if (t == null || exit == null) { FinishGoto("stopped: the walks inside aren't loaded (townnet.json or the learned dungeons)"); return; }
string hubName = exit.Hub;
if (!_hubActive)
{
ResetHub();
_hubActive = true;
_hubStepMs = now;
lock (_gate) _status = $"{hubName} → {exit.Where}";
}
bool portaling = Host.HasIsPortaling && Host.IsPortaling();
uint lb = CurrentLandblock;
bool inside = t.Contains(lb);
if (!_hubLanded)
{
// Landed = inside the network, out of portal space, and settled for HubSettleMs. The
// landblock jump comes BEFORE the client's portal space (10-01 20:19: jump at 38.7 s,
// portal space 38.9-45.9 s): starting the walk at the jump ran its stuck clock through
// seven seconds of portal space. RynthAi's nav waits for portal space to end the same way.
if (portaling || !inside) _hubSettleSince = 0;
else if (_hubSettleSince == 0) _hubSettleSince = now;
if (portaling || !inside || now - _hubSettleSince < HubSettleMs)
{
HubHalt();
if (!portaling && now - _hubStepMs > HubLandTimeoutMs)
FinishGoto($"stopped: the portal didn't take you into {hubName} (you're in landblock 0x{lb:X4})");
else if (now - _hubStepMs > HubPortalSpaceTimeoutMs)
FinishGoto($"stopped: still in portal space a minute after taking the portal into {hubName}");
return;
}
_hubLanded = true;
_hubLb = lb;
_hubLastX = LocalX; _hubLastY = LocalY;
// Normally the landing is now. A goto started inside the network: the last teleport we
// saw (none seen: assume the cooldown is long over).
long landMs = _routeIdx > 0 ? _hubSettleSince : _trkLastTeleportMs > 0 ? Math.Min(now, _trkLastTeleportMs) : now - HubWalker.CooldownMs;
double x = LocalX, y = LocalY;
List? way = t.WayTo(_route![_routeIdx].HubExit, x, y, HubOffRouteUnits, out string from);
if (way == null && exit.Learned)
{
// Away from every walk RynthNav knows to it: RynthAi's dungeon pathfinder (the dats) finds the way.
way = DungeonWayHere(exit.X, exit.Y, exit.Z);
if (way != null) from = "RynthAi's dungeon pathfinder (not walked before)";
}
if (way == null)
{
FinishGoto($"stopped in {hubName}: you landed at {x:F1}, {y:F1}, away from every walk to the {exit.Name}"
+ (exit.Learned && !_dunPathAvailable ? $" ({_dunPathWhy}: the dungeon pathfinder can't find one)" : ""));
return;
}
_hubWalker = new HubWalker(way, landMs, now);
Say($"{hubName}: walking to the {exit.Name} ({_hubWalker.Length:F0} m{(_hubWalker.Levels ? ", over more than one level" : "")}).");
Host.Log($"[RynthNav] {hubName}: landed at {x:F1},{y:F1} (lb 0x{lb:X4}), walk from {from}: {way.Count} points, {_hubWalker.Length:F1} m to {exit.Name}");
}
// An exit into another learned dungeon: still "inside" afterwards, so the teleport is the
// landblock changing, a jump, or portal space (the jump can come a moment before it).
bool chain = !exit.DestOnMap && exit.DestArrival >= 0;
if (chain && inside && !_hubLeaving)
{
double jump = Math.Sqrt((LocalX - _hubLastX) * (LocalX - _hubLastX) + (LocalY - _hubLastY) * (LocalY - _hubLastY));
if (portaling || lb != _hubLb || jump > 15.0) { _hubLeaving = true; _hubLeaveMs = now; }
}
_hubLastX = LocalX; _hubLastY = LocalY;
// The exit portal's teleport (portal space can't last for ever: the walker's limit still runs).
if (portaling)
{
HubHalt();
_hubWalker!.Pause(now); // portal space isn't being stuck
_hubLeftPortal |= _hubLeaving;
if (now > _hubWalker!.LimitMs + HubPortalSpaceTimeoutMs) FinishGoto($"stopped: still in portal space a minute after {hubName}'s exit");
return;
}
if (_hubLeaving)
{
HubHalt();
_hubWalker!.Pause(now);
// Out of portal space (or none came in 3 s): landed at the next dungeon's arrival.
if (!_hubLeftPortal && now - _hubLeaveMs < 3000) return;
if (exit.DestCell != 0 && lb != exit.DestLandblock)
{
FinishGoto($"stopped: the {exit.Name} sent you to landblock 0x{lb:X4}, not to 0x{exit.DestLandblock:X4}: wrong portal");
return;
}
Say($"{hubName}: took the {exit.Name}.");
Host.Log($"[RynthNav] {hubName}: took {exit.Name} into lb 0x{lb:X4} (a chained dungeon)");
ResetHub();
_lastLandMs = NowMs;
if (_routeIdx >= _route!.Count - 1) { FinishGoto("arrived"); return; }
AdvanceRoute();
return;
}
if (!inside)
{
HubHalt();
RefreshTiles(lb);
if (exit.DestCell != 0 && lb != exit.DestLandblock)
{
FinishGoto($"stopped: {hubName} sent you to landblock 0x{lb:X4}, not to {exit.Where} (0x{exit.DestLandblock:X4}): wrong portal");
return;
}
Say($"{hubName}: took the {exit.Name}.");
Host.Log($"[RynthNav] {hubName}: took {exit.Name} (lb 0x{lb:X4})");
ResetHub();
_lastLandMs = NowMs; // the exit's own portal space may still follow (see StepGoto)
if (_routeIdx >= _route!.Count - 1) { FinishGoto("arrived"); return; }
AdvanceRoute();
return;
}
// A closed door across the walk: open it (or unlock it), else go round it (RynthNavPlugin.Doors.cs).
switch (WalkDoor(_hubWalker!, now, out string doorWhy))
{
case DoorAct.Busy:
return;
case DoorAct.Blocked:
if (exit.Learned && HubWalkRound(exit, now)) return;
HubStop(exit, doorWhy);
return;
}
switch (_hubWalker!.Step(LocalX, LocalY, _wz, now, out double heading, out string why))
{
case HubWalker.Act.Run:
DriveTo(heading);
return;
case HubWalker.Act.Jump:
WalkJump(heading, _hubWalker);
return;
case HubWalker.Act.Hold:
HubHalt();
if (_hubWalker.JustStartedWaiting && !_hubWaitSaid)
{
_hubWaitSaid = true;
Say($"{hubName}: waiting {_hubWalker.WaitSeconds} s for the portal to let you through.");
}
return;
case HubWalker.Act.NoTeleport:
{
string need = exit.Rules.MinLevel > 0 ? $" (it needs level {exit.Rules.MinLevel})" : " (level or quest requirement?)";
FinishGoto($"stopped in {hubName}: the {exit.Name} didn't take you{need}");
return;
}
default:
HubStop(exit, why);
return;
}
}
///
/// A door on the walk nothing opens: the walk planned again from here (RynthAi's dungeon
/// pathfinder, the locked doors' doorways left out). False when there is no other way.
///
private bool HubWalkRound(TownNetExit exit, long now)
{
List? way = DungeonWayHere(exit.X, exit.Y, exit.Z);
if (way == null) return false;
_hubWalker = new HubWalker(way, now - HubWalker.CooldownMs, now);
Say($"{exit.Hub}: a door on the way won't open; going round it ({_hubWalker.Length:F0} m to the {exit.Name}).");
Host.Log($"[RynthNav] {exit.Hub}: walk planned again round {_lockedDoors.Count} locked door(s): {way.Count} points, {_hubWalker.Length:F1} m");
return true;
}
private void HubStop(TownNetExit exit, string why) =>
FinishGoto($"stopped in {exit.Hub}: {why} on the way to the {exit.Name}");
/// Metres left on the walk inside (to the exit's centre); -1 before landing.
private double HubLeft() => _hubLanded && _hubWalker != null ? _hubWalker.Left(LocalX, LocalY) : -1;
/// The panel's status line during a hub step.
private string HubLine(double left)
{
TownNetExit? exit = HubExitOf(_routeIdx);
string name = exit?.Name ?? "exit portal", hub = TheHub(exit);
if (!_hubLanded) return $"going into {hub} (then the {name})";
if (HubIsWaiting) return $"in {hub}: waiting for the {name} to let you through";
return $"in {hub}: walking to the {name}{(left >= 0 ? $", {left:F0} yd left" : "")}";
}
/// The route's first Town Network leg, for the goto's start line ("" = none).
private string HubStartText()
{
if (_route == null) return "";
for (int i = 0; i < _route.Count; i++)
{
if (!_route[i].IsHub) continue;
TownNetExit? exit = HubExitOf(i);
if (exit == null) return "";
string hub = TheHub(exit), on = HubChainText(i);
if (i == 0) return $"through {hub}: walking to the {exit.Name}{on}";
RouteStep entry = _route[i - 1];
if (entry.IsHub) return "";
if (entry.UseRecall) return $"{entry.Label} into {hub}, then the {exit.Name}{on}";
return $"through {hub}: its portal at {NavCoords.Fmt(entry.Ns, entry.Ew)}, then the {exit.Name} inside{on}";
}
return "";
}
///
/// The rest of a run of hub steps from : ", into the Vault, then the Vault Exit to 40S, 40W"
/// (each dungeon it goes on into), or " to <where the exit lands>" for one step.
///
private string HubChainText(int i)
{
if (_route == null) return "";
var sb = new System.Text.StringBuilder();
for (int k = i; k < _route.Count && _route[k].IsHub; k++)
{
TownNetExit? x = HubExitOf(_route[k]);
if (k > i) sb.Append($", then the {x?.Name ?? _route[k].Label}");
if (x != null && !x.DestOnMap && k + 1 < _route.Count && _route[k + 1].IsHub) sb.Append($" into {HubExitDest(x)}");
else if (x != null) sb.Append($" to {HubExitDest(x)}");
}
return sb.ToString();
}
///
/// Standing in a learned dungeon: per exit of (its order), the walk from
/// where you stand to that portal (a learned walk passing within HubOffRouteUnits), NaN for none.
///
private double[]? StartHubWalks(HubLinks hub, out int walkable)
{
walkable = 0;
TownNet? t = _townNet;
if (t == null || !t.Contains(CurrentLandblock) || hub.Exits.Count == 0) return null;
var walks = new double[hub.Exits.Count];
double x = LocalX, y = LocalY;
for (int i = 0; i < hub.Exits.Count; i++)
{
List? way = t.WayTo(hub.Exits[i].Id, x, y, HubOffRouteUnits, out _);
walks[i] = way == null ? double.NaN : TownNet.PathLength(way, 0);
if (way != null) walkable++;
}
return walkable > 0 ? walks : null;
}
///
/// A goto started inside the Town Network: there is no navmesh, so the only way on is an exit.
/// Picks the exit that leaves the least to do (the walk inside, the teleport, then what it costs
/// from its landing: a route with portals when portal routing is on, else the straight walk).
/// Null when no exit is usable.
///
private List? PlanFromInsideHub(double goalNs, double goalEw, out double est, out int teleports)
{
est = 0; teleports = 0;
TownNet? t = _townNet;
HubLinks? links = CurrentHubLinks(null);
if (t == null || links == null) return null;
double px = LocalX, py = LocalY;
List? best = null;
double bestCost = double.MaxValue;
int bestUsed = 0;
if (PortalsRouting) EnsurePortalsLoaded();
foreach (HubExit hx in links.Exits)
{
if (hx.ToArrival >= 0) continue; // into another dungeon: only planned from a landing
List? way = t.WayTo(hx.Id, px, py, HubOffRouteUnits, out _);
if (way == null) continue;
double inside = TownNet.PathLength(way, 0) + PortalRoute.PortalPenaltyUnits;
List rest;
double restCost;
int used = 0;
if (PortalsRouting && RoutePortals() is { Count: > 0 } routePortals)
rest = PortalRoute.Plan(routePortals, null, hx.DstNs, hx.DstEw, goalNs, goalEw, out restCost, out used);
else
{
restCost = NavCoords.Distance(hx.DstNs, hx.DstEw, goalNs, goalEw);
rest = new List { new(goalNs, goalEw, false, false, "walk") };
}
if (inside + restCost >= bestCost) continue;
bestCost = inside + restCost;
best = new List { RouteStep.Hub(hx.DstNs, hx.DstEw, hx.Name, hx.Id) };
best.AddRange(rest);
bestUsed = used + 1;
}
est = bestCost;
teleports = bestUsed;
return best;
}
}