using System; using System.Collections.Generic; using System.Linq; using KernelPanic.Combat; using KernelPanic.Run; using UnityEngine; using UnityEngine.UIElements; namespace KernelPanic.UI { internal static class RunMapView { public static VisualElement Build(RunManager run) { var layout = new VisualElement { name = "RunMap" }; layout.AddToClassList("route-map-layout"); var map = new VisualElement { name = "RouteGraph" }; map.AddToClassList("route-map-graph"); if (run.Journey.mapStartWave > 2) map.Add(Text("Earlier decisions are unavailable for this older save. Your map starts here.", "route-map-legend")); int current = run.CurrentWaveNumber, seed = run.CurrentConfig.RunSeed; int revision = run.Journey.mapRevision, startWave = run.Journey.mapStartWave; int first = run.Journey.endless ? Math.Max(startWave, current - 3) : startWave; int last = run.Journey.endless ? current + 5 : run.Journey.FinalWave; IReadOnlyList Row(int wave) => RunRouteMap.Row(seed, wave, startWave, revision); bool Connected(RouteNode a, RouteNode b) => RunRouteMap.Connected(seed, a, b, startWave, revision); var detail = new VisualElement { name = "RouteDetails" }; detail.AddToClassList("route-map-detail"); var preview = new VisualElement { name = "RoutePreview" }; detail.Add(preview); void Preview(RouteNode node) { if (node == null) return; preview.Clear(); bool boss = node.Wave == run.Journey.FinalWave && !run.Journey.endless; string phase = run.Journey.endless ? "SURVIVAL" : node.Wave <= 3 ? "BOOT SECTOR" : node.Wave <= 6 ? "ESCALATION" : boss ? "FINAL ENCOUNTER" : "KERNEL PERIMETER"; string progress = run.Journey.endless ? $"WAVE {node.Wave:00}" : $"WAVE {node.Wave:00} OF {run.Journey.FinalWave:00}"; preview.Add(Text($"{progress}\n{phase}", "route-map-eyebrow")); string state = node.Wave < current ? run.Journey.visits.Any(v => v.nodeId == node.Id) ? "CLEARED" : "NOT TAKEN" : node.Wave > current ? "FUTURE STOP" : node.Id == run.Journey.selectedNodeId ? "SELECTED DESTINATION" : run.AvailableRouteNodes.Any(n => n.Id == node.Id) ? "AVAILABLE" : "UNREACHABLE FROM HERE"; preview.Add(Text(state, "route-preview-state")); preview.Add(Text(boss ? "Kernel Overseer" : RunRouteMap.Title(node.Route), "route-detail-title")); preview.Add(Text(boss ? "Defeat the Overseer to complete the route. Bank your rewards, or continue this build in endless survival." : RunRouteMap.Detail(node.Route), "route-detail-copy")); var encounter = EncounterDirector.Plan(node.Wave, node.Route, seed, run.Journey.encounterRevision); preview.Add(Text("BATTLE: " + encounter.Title, "route-map-eyebrow")); preview.Add(Text(encounter.Advice, "route-detail-copy")); if (node.Wave == current && run.Journey.resolvedEventWave == current && node.Id == run.Journey.selectedNodeId) preview.Add(Text("[ resolved ]\n" + run.Journey.eventOutcome, "route-map-history")); } void SelectedPreview() => Preview(Row(current).FirstOrDefault(n => n.Id == run.Journey.selectedNodeId)); var available = run.AvailableRouteNodes.Select(n => n.Id).ToHashSet(); var chosen = run.Journey.visits.ToDictionary(v => v.wave, v => v.nodeId); chosen[current] = run.Journey.selectedNodeId; var potential = new HashSet { run.Journey.selectedNodeId }; var lit = new HashSet(potential); for (int wave = current + 1; wave <= last; wave++) { var before = Row(wave - 1); var after = Row(wave); potential = after.Where(to => before.Any(from => potential.Contains(from.Id) && Connected(from, to))).Select(n => n.Id).ToHashSet(); lit.UnionWith(potential); } // One coordinate space for both nodes and trails. Future is up; travelled ground is below. bool origin = first == startWave; int bottom = origin ? first - 1 : first; const float pitch = 96, radius = 30, top = 48; var scroll = new ScrollView(ScrollViewMode.Vertical) { name = "RouteMapScroll" }; scroll.AddToClassList("route-map-scroll"); scroll.horizontalScrollerVisibility = ScrollerVisibility.Hidden; var canvas = new VisualElement { name = "RouteCanvas" }; canvas.AddToClassList("route-map-canvas"); canvas.style.height = top + (last - bottom) * pitch + 62; var nodesById = new Dictionary(); var points = new Dictionary(); var rows = new Dictionary>(); void AddMarker(int id, int wave, float position, VisualElement marker) { marker.style.position = Position.Absolute; marker.style.top = top + (last - wave) * pitch - radius; marker.style.left = new Length(12 + position * 76, LengthUnit.Percent); marker.style.marginLeft = -radius; marker.userData = id; nodesById.Add(id, marker); canvas.Add(marker); } for (int wave = first; wave <= last; wave++) { var nodes = Row(wave); rows.Add(wave, nodes); var waveLabel = Text(wave.ToString("00"), "route-map-wave"); waveLabel.style.top = top + (last - wave) * pitch - 42; canvas.Add(waveLabel); foreach (var node in nodes) { bool selected = chosen.TryGetValue(wave, out int id) && id == node.Id; bool reachable = wave == current ? available.Contains(node.Id) : wave > current && lit.Contains(node.Id); VisualElement tile; if (wave == current) { var button = new Button(() => run.SelectRouteNode(node.Id)) { name = "Route" + node.Route }; button.SetEnabled(reachable && run.Journey.resolvedEventWave != current); tile = button; } else tile = new VisualElement { name = "MapNode" + node.Id }; tile.AddToClassList("route-map-node"); tile.EnableInClassList("map-choice", wave == current && reachable); tile.EnableInClassList("map-selected", wave == current && selected); tile.EnableInClassList("map-taken", wave < current && selected); tile.EnableInClassList("map-muted", !selected && !reachable); tile.EnableInClassList("map-future", wave > current); tile.EnableInClassList("map-boss", wave == run.Journey.FinalWave && !run.Journey.endless); tile.Add(Glyph(node.Route)); tile.Add(Text(wave == run.Journey.FinalWave && !run.Journey.endless ? "BOSS" : CompactTitle(node.Route), "route-map-name")); tile.tooltip = $"Wave {wave}: {RunRouteMap.Title(node.Route)}\n{RunRouteMap.Detail(node.Route)}"; tile.RegisterCallback(_ => Preview(node)); tile.RegisterCallback(_ => SelectedPreview()); tile.RegisterCallback(_ => Preview(node)); tile.RegisterCallback(_ => SelectedPreview()); AddMarker(node.Id, wave, nodes.Count == 1 ? .5f : (float)nodes.ToList().IndexOf(node) / (nodes.Count - 1), tile); if (selected && wave == current - 1) { tile.name = "CurrentMapPosition"; var here = Text("YOU ARE HERE", "route-map-here"); tile.Add(here); } } } if (origin) { var start = new VisualElement { name = current == startWave ? "CurrentMapPosition" : "RouteStart" }; start.AddToClassList("route-map-node"); start.AddToClassList("map-taken"); start.Add(Text("OK", "route-map-symbol")); start.Add(Text("Cleared", "route-map-name")); if (current == startWave) start.Add(Text("YOU ARE HERE", "route-map-here")); start.tooltip = $"Wave {startWave - 1} cleared. The route begins here."; AddMarker(-1, bottom, .5f, start); } var edges = new List<(int from, int to, bool taken, bool active, bool future)>(); foreach (var row in rows) { if (!rows.TryGetValue(row.Key + 1, out var next)) continue; foreach (var a in row.Value) foreach (var b in next) { if (!Connected(a, b)) continue; bool taken = b.Wave < current && chosen.TryGetValue(a.Wave, out int aId) && aId == a.Id && chosen.TryGetValue(b.Wave, out int bId) && bId == b.Id; bool active = a.Wave == current - 1 ? chosen.GetValueOrDefault(a.Wave) == a.Id && b.Id == run.Journey.selectedNodeId : a.Wave >= current && lit.Contains(a.Id); edges.Add((a.Id, b.Id, taken, active, a.Wave >= current)); } } if (origin) foreach (var node in rows[first]) edges.Add((-1, node.Id, first < current && chosen.GetValueOrDefault(first) == node.Id, first == current && node.Id == run.Journey.selectedNodeId, false)); canvas.generateVisualContent += context => { points.Clear(); foreach (var entry in nodesById) points[entry.Key] = entry.Value.layout.center; var painter = context.painter2D; // Muted trails first, then the player's path. At crossings the active trail stays legible. foreach (var edge in edges.OrderBy(e => e.taken || e.active)) { Vector2 a = points[edge.from], b = points[edge.to]; var direction = (b - a).normalized; a += direction * radius; b -= direction * radius; Color color = edge.taken ? new Color(.48f, .76f, .59f) : edge.active ? new Color(.91f, .72f, .43f) : new Color(.24f, .33f, .28f); // A dark under-stroke separates real crossings without inventing junctions. Trail(painter, a, b, new Color(.055f, .085f, .067f), 7, false); Trail(painter, a, b, color, edge.active || edge.taken ? 2.5f : 1.5f, edge.future); } }; canvas.RegisterCallback(_ => canvas.MarkDirtyRepaint()); scroll.Add(canvas); map.Add(scroll); bool positioned = false; scroll.RegisterCallback(_ => { if (positioned || scroll.contentViewport.layout.height < 20) return; positioned = true; scroll.schedule.Execute(() => { var here = canvas.Q("CurrentMapPosition"); if (here != null) scroll.scrollOffset = new Vector2(0, Mathf.Max(0, here.layout.yMax + 36 - scroll.contentViewport.layout.height)); }); }); var choices = nodesById.Values.OfType