using System;
using System.Collections.Generic;
using System.Globalization;
namespace RynthCore.Plugin.RynthNav;
/// How a go gets to someone inside a dungeon.
internal enum InsideWayKind
{
/// No way in is known (no portal on the map leads there, no recall you know lands there).
None,
/// Already in that dungeon.
Here,
/// A portal on the map into the dungeon: walked to (a route, when it's far) and into.
Portal,
/// A recall in your spellbook that lands in the dungeon (Bur Recall into the Bur).
Recall,
}
/// The way into the dungeon a place inside it is in.
internal sealed class InsideWay
{
public InsideWayKind Kind;
public AtlasLocation Place = null!;
public uint Cell;
public string Dungeon = "";
public AtlasLocation? Portal;
public AtlasRecall? Recall;
}
///
/// Going to someone (or something) INSIDE a dungeon (2026-11-27). The Atlas has about 3,100 people, vendors,
/// lifestones and landmarks in dungeon cells, with no map position; a go by their name used to say "no place
/// called ... on the map". This finds the way in: already there, a portal on the map into that dungeon's
/// landblock, or a recall you know that lands in it. The walk inside, to the person's cell, is RynthAi's
/// dungeon map (/ra dunnav-to cell): RynthNav has no navmesh in dungeons. Host-free: the route tests compile it.
/// Spotted by SilentKelpie (Forgejo rynth PR #12); re-implemented here.
///
internal static class InsidePlaces
{
/// A landblock's dungeon cells start here; below are the outdoor cells.
public const uint FirstDungeonCell = 0x100;
///
/// The place inside a dungeon means, or null when the map answers it as well or
/// better: a place on the map with the exact name always wins; someone inside wins over a map place that
/// only half matches when their name is exact, and over no whole-word match on the map at all.
/// : the map has a whole-word match (an "a || b" list stops there).
///
public static AtlasLocation? Prefer(Atlas atlas, string name, bool hasPos, double ns, double ew, uint hereLb, out bool onMap)
{
AtlasLocation? map = atlas.FindBest(name, hasPos, ns, ew);
int mapScore = map == null ? 0 : Atlas.MatchScore(map.Name, name.Trim());
onMap = mapScore >= 60;
if (mapScore >= 100) return null;
AtlasLocation? inside = atlas.FindInside(name, hereLb);
if (inside == null) return null;
bool exact = inside.Name.Equals(name.Trim(), StringComparison.OrdinalIgnoreCase);
if (onMap && !exact) return null;
return inside;
}
///
/// "a || b || c" for a go: the candidates in order. Coordinates, or a place the map answers, leave it to
/// the usual go (null); the first candidate that is someone inside a dungeon (by ) is it.
///
public static AtlasLocation? FromAlternatives(Atlas atlas, string text, bool hasPos, double ns, double ew, uint hereLb)
{
foreach (string raw in text.Split("||", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
{
if (NavCoords.TrySplitCoordsAndName(raw, out _, out _, out _)) return null;
AtlasLocation? inside = Prefer(atlas, raw, hasPos, ns, ew, hereLb, out bool onMap);
if (inside != null) return inside;
if (onMap) return null;
}
return null;
}
///
/// The way into 's dungeon from where you are (; your map
/// position when ). Already there first; then, standing on the map, a portal into
/// it; then a recall you know that lands there (: your spellbook; null = not read
/// yet); then, from inside another dungeon, a portal (the route out starts with a recall). Kind None when
/// none is known, or the place has no cell.
///
public static InsideWay WayIn(Atlas atlas, AtlasLocation place, uint hereCell, bool hasPos, double ns, double ew,
ISet? known, bool recallsOn)
{
var way = new InsideWay { Place = place };
if (!Atlas.TryCell(place.Cell, out uint cell)) return way;
uint lb = cell >> 16;
way.Cell = cell;
way.Dungeon = DungeonName(atlas, lb);
if (hereCell >> 16 == lb && (hereCell & 0xFFFF) >= FirstDungeonCell) { way.Kind = InsideWayKind.Here; return way; }
AtlasLocation? portal = Entrance.PortalInto(atlas, lb, hasPos, ns, ew);
way.Portal = portal;
if (portal != null && hasPos) { way.Kind = InsideWayKind.Portal; return way; }
AtlasRecall? recall = recallsOn ? RecallInto(atlas, lb, known) : null;
if (recall != null) { way.Kind = InsideWayKind.Recall; way.Recall = recall; return way; }
if (portal != null) way.Kind = InsideWayKind.Portal;
return way;
}
/// A fixed recall in (the spellbook) that lands in landblock , or null.
public static AtlasRecall? RecallInto(Atlas atlas, uint lb, ISet? known)
{
if (known == null) return null;
foreach (AtlasRecall r in atlas.Recalls)
if (!r.OnMap && Atlas.TryCell(r.Cell, out uint c) && c >> 16 == lb && known.Contains(r.SpellId)) return r;
return null;
}
///
/// A dungeon's name: where the portals into it say they lead, else what a recall into it says, else the
/// recall's own name ("where Bur Recall lands"), else "dungeon 009C".
///
public static string DungeonName(Atlas atlas, uint lb)
{
foreach (AtlasLocation l in atlas.Locations)
if (l.HasDest && !l.DestOnMap && l.DestName.Length > 0 && Atlas.TryCell(l.DestCell, out uint dc) && dc >> 16 == lb) return l.DestName;
foreach (AtlasRecall r in atlas.Recalls)
if (!r.OnMap && Atlas.TryCell(r.Cell, out uint c) && c >> 16 == lb)
return r.In.Length > 0 ? r.In : $"where {r.Name} lands";
return string.Create(CultureInfo.InvariantCulture, $"dungeon {lb:X4}");
}
/// " (Brannoc is in cell 009C0270)": the end of the arrival line for someone inside.
public static string Tail(string name, uint cell) =>
string.Create(CultureInfo.InvariantCulture, $" ({name} is in cell {cell:X8})");
///
/// The RynthAi command that walks the dungeon map to that cell. Walk only (the default, 2026-11-27): RynthAi turns
/// fighting, looting, buffing and the meta off for the walk and puts them back after;
/// (RynthNav "Fight on the way in") walks with the macro as set, fighting and looting on the way.
///
public static string WalkCommand(uint cell, string name, bool fight = false) =>
string.Create(CultureInfo.InvariantCulture, $"/ra dunnav-to cell {cell:X8}{(fight ? "" : " walkonly")} {name}").TrimEnd();
/// The line for a go that has no way in.
public static string NoWayText(InsideWay w) =>
$"{w.Place.Name} is inside {w.Dungeon}, and no portal on the map or recall you know leads in there";
}