RynthSuite/Plugins/RynthCore.Plugin.RynthNav/InsidePlaces.cs

142 lines
7.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
namespace RynthCore.Plugin.RynthNav;
/// <summary>How a go gets to someone inside a dungeon.</summary>
internal enum InsideWayKind
{
/// <summary>No way in is known (no portal on the map leads there, no recall you know lands there).</summary>
None,
/// <summary>Already in that dungeon.</summary>
Here,
/// <summary>A portal on the map into the dungeon: walked to (a route, when it's far) and into.</summary>
Portal,
/// <summary>A recall in your spellbook that lands in the dungeon (Bur Recall into the Bur).</summary>
Recall,
}
/// <summary>The way into the dungeon a place inside it is in.</summary>
internal sealed class InsideWay
{
public InsideWayKind Kind;
public AtlasLocation Place = null!;
public uint Cell;
public string Dungeon = "";
public AtlasLocation? Portal;
public AtlasRecall? Recall;
}
/// <summary>
/// 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.
/// </summary>
internal static class InsidePlaces
{
/// <summary>A landblock's dungeon cells start here; below are the outdoor cells.</summary>
public const uint FirstDungeonCell = 0x100;
/// <summary>
/// The place inside a dungeon <paramref name="name"/> 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.
/// <paramref name="onMap"/>: the map has a whole-word match (an "a || b" list stops there).
/// </summary>
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;
}
/// <summary>
/// "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 <see cref="Prefer"/>) is it.
/// </summary>
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;
}
/// <summary>
/// The way into <paramref name="place"/>'s dungeon from where you are (<paramref name="hereCell"/>; your map
/// position when <paramref name="hasPos"/>). Already there first; then, standing on the map, a portal into
/// it; then a recall you know that lands there (<paramref name="known"/>: 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.
/// </summary>
public static InsideWay WayIn(Atlas atlas, AtlasLocation place, uint hereCell, bool hasPos, double ns, double ew,
ISet<int>? 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;
}
/// <summary>A fixed recall in <paramref name="known"/> (the spellbook) that lands in landblock <paramref name="lb"/>, or null.</summary>
public static AtlasRecall? RecallInto(Atlas atlas, uint lb, ISet<int>? 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;
}
/// <summary>
/// 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".
/// </summary>
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}");
}
/// <summary>" (Brannoc is in cell 009C0270)": the end of the arrival line for someone inside.</summary>
public static string Tail(string name, uint cell) =>
string.Create(CultureInfo.InvariantCulture, $" ({name} is in cell {cell:X8})");
/// <summary>
/// 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; <paramref name="fight"/>
/// (RynthNav "Fight on the way in") walks with the macro as set, fighting and looting on the way.
/// </summary>
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();
/// <summary>The line for a go that has no way in.</summary>
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";
}