using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; using RynthCore.Plugin.Shared; using RynthNav.Routing; namespace RynthCore.Plugin.RynthNav; // Going to someone INSIDE a dungeon (2026-11-27; InsidePlaces.cs has the choices). /rnav go for a person, // vendor, lifestone or landmark the Atlas has in a dungeon cell: // * already in that dungeon: arrived; // * a portal on the map into it: the usual go into that portal (a route with portals and recalls when far); // * a recall you know that lands in it: cast, as a one-step route. // Arriving, the line ends "( is in cell XXXXXXXX)", and (Walk inside on, the default) the walk to that // cell is handed to RynthAi's dungeon map: /ra dunnav-to cell walkonly , a walk with fighting, // looting, buffing and the meta off, put back after (2026-11-27). "Fight on the way in" (/rnav inside fight on, // off by default) hands it over without walkonly: the macro as set, fighting and looting on the way, as its // other dungeon walks do. RynthNav has no navmesh in dungeons. "/rnav inside a || b" says which of those // people is in the dungeon you're in, and never travels. Tick thread. // Spotted by SilentKelpie (Forgejo rynth PR #12); re-implemented here. public sealed partial class RynthNavPlugin { /// The go's target inside a dungeon: the name (the goto's own name) and its cell (0 = none). private string _insideName = ""; private uint _insideCell; /// Hand the walk inside to RynthAi on arrival (rynthnav.json insideWalk). private volatile bool _insideWalkOn = true; /// The walk inside fights, loots and buffs on the way, the macro as set (rynthnav.json insideFight, off: /// a walk only). private volatile bool _insideFightOn; /// A walk inside waiting to go to RynthAi: once the landing has settled. private string _handoffName = ""; private uint _handoffCell; private long _handoffDueMs, _handoffGiveUpMs; private const long HandoffSettleMs = 2500, HandoffGiveUpMs = 30000; private void LoadInsideSettings() { _insideWalkOn = NavDataConfig.ReadSwitch("insideWalk", true); _insideFightOn = NavDataConfig.ReadSwitch("insideFight", false); } /// "a || b" for go: someone inside a dungeon, when no earlier candidate is on the map. True when handled. private bool TryGoInsideAlternative(string text) { bool hasPos = PlayerMapPos(out double pns, out double pew); AtlasLocation? inside = InsidePlaces.FromAlternatives(_atlas, text, hasPos, pns, pew, CurrentLandblock); return inside != null && GoInside(inside); } /// One name for go: someone inside a dungeon when the map has nothing better for it. True when handled. private bool TryGoInside(string text) { if (text.Length == 0 || _store.FindFavorite(text) != null || NavCoords.TrySplitCoordsAndName(text, out _, out _, out _)) return false; bool hasPos = PlayerMapPos(out double pns, out double pew); AtlasLocation? inside = InsidePlaces.Prefer(_atlas, text, hasPos, pns, pew, CurrentLandblock, out _); return inside != null && GoInside(inside); } /// /// route / arrow for someone inside a dungeon: the portal into it, as the target (its name says who for). /// False when the name isn't someone inside, or there's no portal on the map into their dungeon (said). /// private bool InsideTarget(string text, out string name, out string type, out double ns, out double ew, out bool handled) { name = ""; type = ""; ns = 0; ew = 0; handled = false; if (text.Length == 0 || _store.FindFavorite(text) != null || NavCoords.TrySplitCoordsAndName(text, out _, out _, out _)) return false; bool hasPos = PlayerMapPos(out double pns, out double pew); AtlasLocation? inside = InsidePlaces.Prefer(_atlas, text, hasPos, pns, pew, CurrentLandblock, out _); if (inside == null) return false; handled = true; InsideWay w = InsidePlaces.WayIn(_atlas, inside, _cellId, hasPos, pns, pew, _known, _recallsEnabled); if (w.Portal == null) { Note(w.Kind == InsideWayKind.Here ? $"{inside.Name} is inside {w.Dungeon}, where you are{InsidePlaces.Tail(inside.Name, w.Cell)}" : w.Kind == InsideWayKind.Recall ? $"{inside.Name} is inside {w.Dungeon}; no portal on the map leads there, but {w.Recall!.Name} does: /rnav go {inside.Name}" : InsidePlaces.NoWayText(w)); return false; } name = $"{w.Portal.Name} (into {w.Dungeon}, for {inside.Name})"; type = "Portal"; ns = w.Portal.Ns; ew = w.Portal.Ew; return true; } /// The way into 's dungeon, started. True when handled (gone, or why not said). private bool GoInside(AtlasLocation place) { if (!_hasPose) { Note("goto: no player position yet"); return true; } bool hasPos = PlayerMapPos(out double pns, out double pew); InsideWay w = InsidePlaces.WayIn(_atlas, place, _cellId, hasPos, pns, pew, _known, _recallsEnabled); if (w.Cell == 0) return false; switch (w.Kind) { case InsideWayKind.Here: _insideName = place.Name; _insideCell = w.Cell; _gotoName = place.Name; Host.Log($"[RynthNav] goto {place.Name}: already in {w.Dungeon} (cell {w.Cell:X8})"); Say($"goto: you're in {w.Dungeon}: {place.Name} is inside{InsidePlaces.Tail(place.Name, w.Cell)}"); QueueHandoff(place.Name, w.Cell, 0); return true; case InsideWayKind.Portal: _insideName = place.Name; _insideCell = w.Cell; _nextEnterPortal = w.Portal!.Name; // the portal object a saved nav route names (RynthNavPlugin.SaveRoute.cs) Say($"goto: {place.Name} is inside {w.Dungeon}: going in by {w.Portal.Name} at {NavCoords.Fmt(w.Portal.Ns, w.Portal.Ew)}"); StartGoto(place.Name, place.Type, w.Portal.Ns, w.Portal.Ew, w.Dungeon, "Dungeon"); return true; case InsideWayKind.Recall: StartRecallInto(place, w); return true; default: Note("goto: " + InsidePlaces.NoWayText(w)); return true; } } /// A goto of one recall step into the dungeon: cast, wait for the teleport, arrive (the usual recall step and settle). private void StartRecallInto(AtlasLocation place, InsideWay w) { AtlasRecall r = w.Recall!; if (_gotoActive) { _gotoActive = false; _route = null; _portalWait = false; ResetHub(); AutoRun.Stop(); HideMarkers("a new goto"); } if (_recallWait) EndRecall(); _goJobId = 0; _paused = false; _landReplanned = false; _tripReplans = 0; _enterPortal = ""; _nextEnterPortal = ""; _portalWait = false; _portalWaitTicks = 0; _wasPortaling = false; _settling = false; _enterName = ""; _enterType = ""; _finalTew = _wx; _finalTns = _wy; ResetHub(); _routeRecalls = new List { new() { Name = r.Name, SpellId = r.SpellId, CastSeconds = RecallPlanner.SpellCastSeconds } }; bool hasPos = PlayerMapPos(out double pns, out double pew); _route = new List { new(hasPos ? pns : 0, hasPos ? pew : 0, usePortal: false, useRecall: true, r.Name, 0, double.NaN, double.NaN) }; _routeIdx = 0; _tripEst = double.NaN; _shownRoute = _route; _shownRouteFor = place.Name; _shownRouteEst = 0; _shownRouteLive = true; _routeVersion++; _insideName = place.Name; _insideCell = w.Cell; _gotoActive = true; _gotoName = place.Name; _gotoType = place.Type; _walkHaveLast = false; AutoRun.Forget(); Host.Log($"[RynthNav] goto {place.Name} (inside {w.Dungeon}, cell {w.Cell:X8}) by {r.Name}"); Say($"goto: {place.Name} is inside {w.Dungeon}: casting {r.Name} — /rnav stop to cancel"); BeginRecallStep(); } /// /// FinishGoto: the arrival line's tail for a target inside a dungeon, and the walk inside queued for RynthAi. /// The target is forgotten either way. "" when this goto wasn't for someone inside, or didn't get there. /// private string TakeInsideTail(string status) { string name = _insideName; uint cell = _insideCell; _insideName = ""; _insideCell = 0; if (cell == 0 || !status.StartsWith("arrived", StringComparison.Ordinal) || !name.Equals(_gotoName, StringComparison.Ordinal)) return ""; if (CurrentLandblock != cell >> 16) return ""; QueueHandoff(name, cell, HandoffSettleMs); return InsidePlaces.Tail(name, cell); } private void QueueHandoff(string name, uint cell, long settleMs) { if (!_insideWalkOn) return; _handoffName = name; _handoffCell = cell; _handoffDueMs = NowMs + settleMs; _handoffGiveUpMs = NowMs + HandoffGiveUpMs; } /// The walk inside, handed to RynthAi once the landing has settled (out of portal space). Tick thread. private void TickInsideHandoff() { if (_handoffCell == 0) return; long now = NowMs; if (now > _handoffGiveUpMs || (_hasPose && CurrentLandblock != _handoffCell >> 16 && now > _handoffDueMs)) { _handoffCell = 0; return; } if (now < _handoffDueMs || !_hasPose || (Host.HasIsPortaling && Host.IsPortaling())) return; if (_gotoActive) { _handoffCell = 0; return; } // another goto started meanwhile: it wins string cmd = InsidePlaces.WalkCommand(_handoffCell, _handoffName, _insideFightOn); string name = _handoffName; _handoffCell = 0; if (SubmitToRynthAi(cmd)) Host.Log($"[RynthNav] inside: handed the walk to RynthAi: {cmd}"); else Note($"goto: {name} is further in; RynthAi isn't loaded to walk the dungeon map there ({cmd})"); } /// A command line through RynthAi's "RynthAi.Script" interface (its own commands run at once). False when RynthAi isn't there. private unsafe bool SubmitToRynthAi(string line) { if (!Host.HasGetPluginInterface) return false; var t = (RynthAiScriptApiV1*)Host.GetPluginInterface("RynthAi", RynthAiScriptApiV1.InterfaceName, RynthAiScriptApiV1.InterfaceVersion); if (t == null || t->Version < 1 || t->StructSize < (uint)sizeof(RynthAiScriptApiV1) || t->SubmitCommand == null) return false; byte[] utf8 = new byte[Encoding.UTF8.GetByteCount(line) + 1]; Encoding.UTF8.GetBytes(line, 0, line.Length, utf8, 0); fixed (byte* p = utf8) return t->SubmitCommand(p) != 0; } /// /// /rnav inside a || b: which of these people is in THIS dungeon, and in which cell (a script, already inside, /// before it walks the dungeon). Never travels. /rnav inside walk on|off: hand the walk inside to RynthAi. /// private void CmdInside(string text) { string t = text.Trim(); if (t.StartsWith("walk", StringComparison.OrdinalIgnoreCase) && (t.Length == 4 || t[4] == ' ')) { string a = t.Substring(4).Trim().ToLowerInvariant(); if (a is "on" or "off") { _insideWalkOn = a == "on"; NavDataConfig.WriteSwitch("insideWalk", _insideWalkOn); } else if (a.Length > 0) { SayFail("/rnav inside walk on|off — hand the walk inside a dungeon to RynthAi's dungeon map"); return; } Say(_insideWalkOn ? "walk inside: ON — a go to someone inside a dungeon hands the last stretch to RynthAi's dungeon map (" + (_insideFightOn ? "fighting on the way in: the macro as set)" : "a walk only: no fighting, looting or buffing)") : "walk inside: OFF — a go to someone inside a dungeon stops at the way in"); return; } // /rnav inside fight on|off (2026-11-27): the walk inside fights, loots and buffs on the way (off: a walk only). if (t.StartsWith("fight", StringComparison.OrdinalIgnoreCase) && (t.Length == 5 || t[5] == ' ')) { string a = t.Substring(5).Trim().ToLowerInvariant(); if (a is "on" or "off") { _insideFightOn = a == "on"; NavDataConfig.WriteSwitch("insideFight", _insideFightOn); } else if (a.Length > 0) { SayFail("/rnav inside fight on|off — the walk inside a dungeon fights, loots and buffs on the way (off: a walk only)"); return; } Say(_insideFightOn ? "fight on the way in: ON — RynthAi's walk inside a dungeon runs its macro as set (fights, loots and buffs on the way)" : "fight on the way in: OFF — RynthAi's walk inside a dungeon is a walk only (fighting, looting, buffing and the meta off, put back after)"); return; } if (t.Length == 0) { SayFail("/rnav inside [|| ...] — which of these is in this dungeon | inside walk on|off | inside fight on|off"); return; } uint here = CurrentLandblock; bool inDungeon = (_cellId & 0xFFFF) >= InsidePlaces.FirstDungeonCell; string elsewhere = ""; foreach (string raw in t.Split("||", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)) { AtlasLocation? l = _atlas.FindInside(raw, here); if (l == null || !Atlas.TryCell(l.Cell, out uint cell)) continue; if (inDungeon && cell >> 16 == here) { Say($"inside: {l.Name} is here{InsidePlaces.Tail(l.Name, cell)}"); return; } if (elsewhere.Length == 0) elsewhere = $"{l.Name} is in {InsidePlaces.DungeonName(_atlas, cell >> 16)}, not here"; } SayFail("inside: " + (elsewhere.Length > 0 ? elsewhere : "none of these is someone inside a dungeon: " + t.Replace("||", ","))); } }