// DungeonPathfinder, explore half: one walk through every room of the dungeon, then stop (2026-11-13). // // Why: Tom wanted a script that explores a dungeon: every room once, what turned up listed at the // end. The patrol is a loop for hunting: it goes round for ever, and a reroute (a hazard sighted, // a portal coming into view: in a dungeon nobody has walked yet that is every few rooms) lays the // whole lap again from where the character stands, so a walk that should end would keep crossing // rooms it has already been through. // // The explore (/ra dunnav-explore, RynthAiPlugin.DunExplore.cs) is a Once route over the same rooms // as the patrol with coverage Everything (DungeonRooms: cells joined by wide openings; each cell its // own room until the dungeon map is loaded), laid by the patrol's own pieces: doorways taken straight // on, rooms crossed directly, big rooms looked into (EmitSteps, lookIn), lava/acid and drops left out, // portals kept out of (LayAroundPortals). What is new is the order: // - from the room the character is in, the next room is a room next door not yet entered; of // several, the one with the fewest new rooms beyond it (a closet or a dead end first, so it is // never walked back to from far away later); // - with none next door, the shortest way (in doorways) to the nearest room not yet entered; // - it ends in the middle of the last new room (no walk back to the start). // The rooms already entered (visitedCells: the cells the character has stood in on this explore) // count as done, so a reroute lays only the walk through the rest. // Shut doors (a door the character can't use: a lever opens it) are left out by the caller (their // openings closed in the graph); when one opens, the rooms behind it (preferCells) are walked first, // since it shuts again on its own (8 s to 5 min in ACE's data). using System; using System.Collections.Generic; using System.Linq; using RynthCore.Plugin.RynthAi.LegacyUi; namespace RynthCore.Plugin.RynthAi; /// The rooms an explore walk was laid over, numbered as its . internal sealed class ExplorePlan { /// Every cell of the graph in a room (the map's rooms, or one cell each without the map). public DungeonRooms Rooms = new(); /// The rooms the walk is meant to enter: every room the character can walk to from where it /// stood (no lava/acid, no drop, no opening a portal closes). public readonly HashSet PlanRooms = new(); /// Of PlanRooms, those entered before this walk was laid (it doesn't go back to them). public readonly HashSet DoneRooms = new(); /// The rooms the walk goes into, in order (the start room not included). public readonly List Order = new(); /// Laid on the dungeon map's rooms (false: the cell walk, each cell a room). public bool ByRoom; /// The room middles the walk goes to (look-ins, the last room's): waited at (DungeonPathfinder.ExploreScanMs). public readonly List ScanMiddles = new(); public int RoomOf(uint cell) => Rooms.RoomOf.TryGetValue(cell, out int r) ? r : -1; } internal static partial class DungeonPathfinder { /// /// A Once route from through every room the character can walk to and /// has not entered yet (: cells it has stood in), ending in the /// middle of the last one. The patrol settings in apply but for the /// coverage, always Everything. Empty when there is nothing left to walk to. /// public static NavRouteParser BuildExploreRoute( Dictionary graph, uint startCell, IReadOnlySet? hazardCells, DungeonGeometry? geo, DungeonPatrolOptions? options, IReadOnlyList? portals, (double X, double Y, double Z)? startAt, IReadOnlySet? visitedCells, out ExplorePlan plan, IReadOnlySet? preferCells = null) { using var holes = CountHoles(); // one build: the note's "no way round" counts every walk round of it (Holes.cs) LastPortalAvoidance = null; var o = ((options ?? DungeonPatrolOptions.Default) with { Coverage = PatrolCoverage.Everything }).Clamped(); NavRouteParser route; if (portals is not { Count: > 0 }) route = LayExplore(graph, startCell, hazardCells, geo, o, visitedCells, out plan, preferCells); else { ExplorePlan? last = null; route = LayAroundPortals(graph, startCell, hazardCells, geo, portals, startAt, o.WallClearance, loop: false, g => { var r = LayExplore(g, startCell, hazardCells, geo, o, visitedCells, out var p, preferCells); last = p; return r; }); plan = last ?? new ExplorePlan(); } // The waits go in last, on the finished walk (the portal step moves points out of a portal's circle; a // middle it moved gets no wait). int scans = ExploreScanMs > 0 && plan.ScanMiddles.Count > 0 ? AddScanPauses(route.Points, plan.ScanMiddles, ExploreScanMs) : 0; if (scans > 0) MainRouteNote = (MainRouteNote == null ? "" : MainRouteNote + "; ") + $"scans: {scans} room middle(s) waited at {ExploreScanMs / 1000.0:0.#} s"; return route; } // ── Steps nav can't walk (2026-11-16) ────────────────────────────────────────────────────────── // DreamWeave Thread 0x0278: the explore crossed a two-level room straight, from its doorway at the foot // (z -42) to the opening on the level above (z -36), 1 m off on the map. Nav takes a point as reached by // the flat distance, so it skipped the upper one and ran from the lower floor at the next point, 50 m off // on the level above: into the wall under it, until it was stopped. A step rising (or dropping) more than // StackedRiseMeters within StackedFlatMeters is no stair (a stair's steps here are 3 m up over 2 m): the // doorway at its end is closed for this walk (all its cell pairs) and the walk laid again. internal const double StackedRiseMeters = 4.5, StackedFlatMeters = 2.5; /// Off: the walk as laid (tests compare the two). internal static bool CloseStackedSteps = true; private const int StackedRetries = 4; /// The walk's steps nav can't walk as laid (see above), and the doorways at their ends (all /// their cell pairs) to close. internal static HashSet StackedStepOpenings(List pts, Dictionary graph, DungeonGeometry geo, out int steps) { var keys = new HashSet(); steps = 0; for (int i = 0; i + 1 < pts.Count; i++) { NavPoint a = pts[i], b = pts[i + 1]; if (!NavRouteParser.IsPlainWaypoint(a.Type) || !NavRouteParser.IsPlainWaypoint(b.Type)) continue; double ax = DungeonGeometry.NavToWorld(a.EW), ay = DungeonGeometry.NavToWorld(a.NS); double bx = DungeonGeometry.NavToWorld(b.EW), by = DungeonGeometry.NavToWorld(b.NS); double rise = Math.Abs(b.Z - a.Z) * 240.0, flat = Math.Sqrt(Sq(bx - ax) + Sq(by - ay)); if (rise <= StackedRiseMeters || flat >= StackedFlatMeters) continue; steps++; var at = DoorwayAt(graph, geo, ax, ay, a.Z * 240.0, 1.5, out _); if (at.Count == 0) at = DoorwayAt(graph, geo, bx, by, b.Z * 240.0, 1.5, out _); keys.UnionWith(at); } return keys; } private static NavRouteParser LayExplore( Dictionary graph, uint startCell, IReadOnlySet? hazardCells, DungeonGeometry? geo, DungeonPatrolOptions o, IReadOnlySet? visitedCells, out ExplorePlan plan, IReadOnlySet? preferCells = null) { string? note = MainRouteNote; var route = LayExploreOnce(graph, startCell, hazardCells, geo, o, visitedCells, out plan, preferCells); if (geo == null || !plan.ByRoom || !CloseStackedSteps) return route; HashSet? cut = null; var cur = graph; int stacked = 0; for (int round = 0; round < StackedRetries; round++) { var keys = StackedStepOpenings(route.Points, cur, geo, out int steps); if (steps == 0) break; stacked += steps; cut ??= new HashSet(); int before = cut.Count; cut.UnionWith(keys); if (cut.Count == before) break; // no doorway to close: the walk stays as it is cur = WithoutOpenings(graph, cut, startCell); string? laid = MainRouteNote; MainRouteNote = note; var again = LayExploreOnce(cur, startCell, hazardCells, geo, o, visitedCells, out var p, preferCells); if (again.Points.Count == 0) { MainRouteNote = laid; break; } // nothing left to walk: keep the first route = again; plan = p; } if (stacked > 0) MainRouteNote = (MainRouteNote == null ? "" : MainRouteNote + "; ") + $"stacked steps: {stacked} found, {cut?.Count ?? 0} opening pair(s) closed for the walk"; return route; } private static NavRouteParser LayExploreOnce( Dictionary graph, uint startCell, IReadOnlySet? hazardCells, DungeonGeometry? geo, DungeonPatrolOptions o, IReadOnlySet? visitedCells, out ExplorePlan plan, IReadOnlySet? preferCells = null) { var route = new NavRouteParser { RouteType = NavRouteType.Once }; bool byRoom = geo != null && geo.DoorwayCount > 0; var rooms = byRoom ? BuildRooms(graph, geo!, hazardCells) : CellRooms(graph); plan = new ExplorePlan { Rooms = rooms, ByRoom = byRoom }; // Coverage Everything: every cell walkable to from the start, dead ends included. foreach (uint c in GetMainRouteNodes(graph, startCell, hazardCells, o, byRoom ? rooms : null)) if (rooms.RoomOf.TryGetValue(c, out int r)) plan.PlanRooms.Add(r); if (!rooms.RoomOf.TryGetValue(startCell, out int startRoom) || !plan.PlanRooms.Contains(startRoom)) return route; var entered = new HashSet { startRoom }; if (visitedCells != null) foreach (uint c in visitedCells) if (rooms.RoomOf.TryGetValue(c, out int vr) && plan.PlanRooms.Contains(vr)) entered.Add(vr); plan.DoneRooms.UnionWith(entered); var adj = RoomAdjacency(graph, rooms, plan.PlanRooms, hazardCells, out _); HashSet? prefer = null; if (preferCells is { Count: > 0 }) { prefer = new HashSet(); foreach (uint c in preferCells) if (rooms.RoomOf.TryGetValue(c, out int pr) && plan.PlanRooms.Contains(pr) && !entered.Contains(pr)) prefer.Add(pr); } var walk = ExploreWalk(adj, startRoom, entered, plan.Order, prefer); if (walk.Count == 0) return route; var steps = new List(walk.Count); foreach (var e in walk) steps.Add(new Step(e.From, e.To, e.Key)); if (byRoom) { // Into the middle of the last room, so the walk ends inside it, not just past its doorway. int lastRoom = walk[walk.Count - 1].Room; var mid = RoomMiddle(graph, geo!, rooms.Cells[lastRoom], o.WallClearance, out uint midCell); if (mid != null && midCell == 0) midCell = walk[walk.Count - 1].To; var middles = plan.ScanMiddles; ScanMiddles = middles; List pts; try { pts = EmitSteps(graph, geo!, rooms, steps, closed: false, null, 0, mid, midCell, o, out _, lookIn: true); } finally { ScanMiddles = null; } if (mid != null) { Append(pts, Tag(mid, "explore-end")); middles.Add(mid); } // Round a hole in a cell's floor, not over it (DungeonPathfinder.Holes.cs): before the // straightening, so its legs keep the clearance the walk round has. (EmitSteps walked its legs // round placed objects already; this is the floor, and an object on the leg into the end too.) int holes = WalkRoundHoles(pts, geo!); if (holes > 0 || HolesLeft > 0) MainRouteNote = (MainRouteNote == null ? "" : MainRouteNote + "; ") + $"holes: {holes} walked round" + (HolesLeft > 0 ? $", {HolesLeft} with no way round" : ""); // Fewer turns: straight legs over the room boundaries a corridor of small rooms is cut into // (DungeonPathfinder.Straighten.cs), every room still walked through. int straightened = StraightenWalk(pts, graph, geo!, rooms, o.WallClearance, middles); if (straightened > 0) MainRouteNote = (MainRouteNote == null ? "" : MainRouteNote + "; ") + $"straightened: {straightened} turn(s) taken out"; route.Points.AddRange(pts); } else { foreach (var s in steps) if (graph.TryGetValue(s.From, out var a) && graph.TryGetValue(s.To, out var b)) AddPortalWaypoints(route, a, b); if (steps.Count > 0 && graph.TryGetValue(steps[steps.Count - 1].To, out var end)) route.Points.Add(new NavPoint { Type = NavPointType.Point, NS = end.NS, EW = end.EW, Z = end.Z / 240.0 }); SimplifyRoute(route); } return route; } /// /// The doorway steps of the explore walk: from , into a room next door not /// yet (the one with the fewest new rooms beyond it first), else the /// shortest way to the nearest room not yet entered, until every room it can reach is entered. /// is added to as it goes; gets each new room. /// private static List ExploreWalk(Dictionary> adj, int startRoom, HashSet entered, List? order = null, HashSet? prefer = null) { var walk = new List(); int cur = startRoom; // Behind a door that just opened first: the shortest way to the nearest of those rooms (it shuts again). if (prefer is { Count: > 0 }) { var first = RoomBfs(adj, cur, r => prefer.Contains(r) && !entered.Contains(r)); if (first != null) { foreach (var e in first) { walk.Add(e); if (entered.Add(e.Room)) order?.Add(e.Room); } cur = first[first.Count - 1].Room; } } // Every pass enters one new room (or stops), so this bounds it. int guard = adj.Count + 16; while (guard-- > 0) { RoomEdge? next = null; int fewest = int.MaxValue; if (adj.TryGetValue(cur, out var doors)) foreach (var e in doors) { if (entered.Contains(e.Room)) continue; int beyond = NewRoomsBeyond(adj, e.Room, cur, entered); if (beyond < fewest) { fewest = beyond; next = e; } } if (next is RoomEdge step) { walk.Add(step); entered.Add(step.Room); order?.Add(step.Room); cur = step.Room; continue; } var hop = RoomBfs(adj, cur, r => !entered.Contains(r)); if (hop == null) break; // every room it can reach is entered walk.AddRange(hop); cur = hop[hop.Count - 1].Room; entered.Add(cur); order?.Add(cur); } return walk; } // The rooms next to `room` (but `from`) not yet entered. private static int NewRoomsBeyond(Dictionary> adj, int room, int from, HashSet entered) { if (!adj.TryGetValue(room, out var doors)) return 0; var seen = new HashSet(); foreach (var e in doors) if (e.Room != from && !entered.Contains(e.Room)) seen.Add(e.Room); return seen.Count; } /// The key the graph's openings are closed by (). internal static ulong OpeningKey(uint a, uint b) => EdgeKey(a, b); // ── Enter and scan (2026-11-16) ───────────────────────────────────────── // A room's monsters reach the client as it comes into view: DreamWeave Thread 0x00B5's boss was known with no // position while the walk touched its room's doorway and turned back, so the fight never started. The explore // walks into every dead end (LooksIn, enterDeadEnds) and waits at each room middle it walks to (and the last // room's), a nav Pause point after it: the room's monsters arrive and the fight starts before it walks on. /// How long the explore waits at a room middle it walks to (0: no waits). internal static int ExploreScanMs = 2000; // The room middles EmitSteps walks to (CrossRoom's look-in), while the explore's walk is laid. [ThreadStatic] private static List? ScanMiddles; /// A Pause after each point of still at one of (the /// clean-up passes keep or drop whole points), once per middle. The number put in. private static int AddScanPauses(List pts, List middles, int ms) { int added = 0; var left = new List(middles); for (int i = 0; i < pts.Count && left.Count > 0; i++) { var p = pts[i]; if (p.Type != NavPointType.Point) continue; int m = left.FindIndex(q => NavPointYd(q, p) < 0.3 && Math.Abs(q.Z - p.Z) * 240.0 < 1.0); if (m < 0) continue; left.RemoveAt(m); pts.Insert(i + 1, new NavPoint { Type = NavPointType.Pause, NS = p.NS, EW = p.EW, Z = p.Z, PauseTimeMs = ms }); i++; added++; } return added; } // Another cell pair's portal is in the same opening when its centre is this close (horizontally, and in // height: 0x5456's 0174>0177 is 3 m over 0173>0177) and it faces the same way. private const double SameOpeningMeters = 1.0, SameOpeningRise = 3.5; /// /// Every cell pair whose portal is in the one opening - is (its key /// first). A cell stacked on another at a room's edge (two cells with one centre, each with its own /// portal in the doorway) gives the doorway a second pair. DreamWeave Thread 0x5456 (2026-11-16): 014F and /// 0150 both join 0148 at (16133, 16492); the explore stuck there closed 014F>0148 only, laid the walk /// again through 0150>0148 (the same gap), and was stuck there again, three times over. /// internal static HashSet OpeningPairs(Dictionary graph, DungeonGeometry? geo, uint a, uint b) { var keys = new HashSet { EdgeKey(a, b) }; if (geo == null || !geo.TryGetDoorway(a, b, out var d)) return keys; foreach (var o in geo.DoorwaysNear(d.X, d.Y, d.Z, SameOpeningMeters, SameOpeningRise)) { if (o.CellA != a && o.CellA != b && o.CellB != a && o.CellB != b) continue; // another opening's if (Math.Abs(o.Nx * d.Nx + o.Ny * d.Ny) < 0.9) continue; // across it, not through it if (!graph.TryGetValue(o.CellA, out var na) || !na.Neighbors.Contains(o.CellB)) continue; keys.Add(EdgeKey(o.CellA, o.CellB)); } return keys; } /// /// The doorway something at (x, y, z) (world metres) stands in: the nearest opening narrower than a room's /// own (not IsWide) whose middle is within of it, 3 m in height, its cells joined /// in the graph; as all its cell pairs (). Empty when it stands in none. /// internal static HashSet DoorwayAt(Dictionary graph, DungeonGeometry geo, double x, double y, double z, double within, out DungeonDoorway? doorway) { doorway = null; double best = double.MaxValue; foreach (var d in geo.DoorwaysNear(x, y, z, within, 3.0)) { if (d.IsWide || !graph.TryGetValue(d.CellA, out var na) || !graph.ContainsKey(d.CellB) || !na.Neighbors.Contains(d.CellB)) continue; double dist = Sq(d.X - x) + Sq(d.Y - y); if (dist < best) { best = dist; doorway = d; } } return doorway == null ? new HashSet() : OpeningPairs(graph, geo, doorway.CellA, doorway.CellB); } /// Every cell walkable to from (no drops, no lava/acid): what a shut /// door's rooms behind it are measured with. internal static HashSet ReachableCells(Dictionary graph, uint startCell, IReadOnlySet? hazardCells) => ReachableAndCore(graph, startCell, hazardCells).Reachable; /// /// The opening(s) a door at (x, y, z) (world metres, in ) stands in: the map's /// doorway nearest it within 3 m on its level (and any within 0.75 m of that one: a double door); /// without the map, the step from its cell to the neighbour whose shared midpoint is nearest it. /// Empty when nothing is near enough (the door isn't in an opening the graph knows). /// internal static HashSet DoorOpenings(Dictionary graph, DungeonGeometry? geo, uint cell, double x, double y, double z) { var edges = new HashSet(); if (geo != null && geo.DoorwayCount > 0) { var near = geo.DoorwaysNear(x, y, z, 3.0) .Where(d => graph.ContainsKey(d.CellA) && graph.ContainsKey(d.CellB)) .Select(d => (d, dist: Math.Sqrt(Sq(d.X - x) + Sq(d.Y - y)))) .OrderBy(t => t.dist).ToList(); if (near.Count > 0) { double best = near[0].dist; foreach (var (d, dist) in near) if (dist <= best + 0.75) edges.Add(EdgeKey(d.CellA, d.CellB)); return edges; } } if (!graph.TryGetValue(cell, out var n)) return edges; double wx = DungeonGeometry.WorldToNav(x), wy = DungeonGeometry.WorldToNav(y); uint pick = 0; double bestD = double.MaxValue; foreach (uint nb in n.Neighbors) { if (!graph.TryGetValue(nb, out var m)) continue; double d = Sq((n.EW + m.EW) / 2 - wx) + Sq((n.NS + m.NS) / 2 - wy); if (d < bestD) { bestD = d; pick = nb; } } if (pick != 0) edges.Add(EdgeKey(cell, pick)); return edges; } /// Each cell its own room: the explore without the dungeon map. private static DungeonRooms CellRooms(Dictionary graph) { var rooms = new DungeonRooms(); foreach (uint c in graph.Keys.OrderBy(k => k)) { rooms.RoomOf[c] = rooms.Cells.Count; rooms.Cells.Add(new List { c }); } return rooms; } }