@spellkave/world-map 0.0.0-bootstrap

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,486 @@
1
+ import { countMapPlaces } from "./inventory.ts";
2
+ import { mapBounds } from "./layout.ts";
3
+ import type { SpatialGraphLayout, SpatialGraphModel, SpatialGraphNode } from "./types.ts";
4
+
5
+ export interface RenderAsciiOptions {
6
+ /** Max character columns for the plot (default 72). */
7
+ width?: number;
8
+ /** Max character rows for the plot (default 28). */
9
+ height?: number;
10
+ /**
11
+ * Character set:
12
+ * - true / omitted: full UTF-8 box-drawing + curves (default)
13
+ * - false: 7-bit ASCII only
14
+ */
15
+ unicode?: boolean;
16
+ /** ANSI color (default true for utf-8 unless NO_COLOR). */
17
+ color?: boolean;
18
+ /** Hide occupancy markers (default false). */
19
+ hideOccupancy?: boolean;
20
+ /** Include title/legend chrome (default true). */
21
+ chrome?: boolean;
22
+ }
23
+
24
+ // Direction bits for orthogonal stroke merging.
25
+ const N = 1;
26
+ const E = 2;
27
+ const S = 4;
28
+ const W = 8;
29
+
30
+ /** Sharp junctions (routes). */
31
+ const BOX: Record<number, string> = {
32
+ [N]: "│",
33
+ [S]: "│",
34
+ [N | S]: "│",
35
+ [E]: "─",
36
+ [W]: "─",
37
+ [E | W]: "─",
38
+ [N | E]: "└",
39
+ [N | W]: "┘",
40
+ [S | E]: "┌",
41
+ [S | W]: "┐",
42
+ [N | S | E]: "├",
43
+ [N | S | W]: "┤",
44
+ [N | E | W]: "┴",
45
+ [S | E | W]: "┬",
46
+ [N | S | E | W]: "┼",
47
+ };
48
+
49
+ /** Rounded corners where a 2-way bend is pure corner (prettier paths). */
50
+ const BOX_ROUND: Record<number, string> = {
51
+ ...BOX,
52
+ [N | E]: "╰",
53
+ [N | W]: "╯",
54
+ [S | E]: "╭",
55
+ [S | W]: "╮",
56
+ };
57
+
58
+ type Ink = "route" | "stairs" | "panel" | "place" | "occ" | "label" | "chrome" | "dim";
59
+
60
+ interface Cell {
61
+ ch: string;
62
+ ink: Ink;
63
+ prio: number;
64
+ }
65
+
66
+ const ANSI: Record<Ink, string> = {
67
+ // Match SVG palette loosely: route gold, stairs purple, place teal, occ blue
68
+ route: "\x1b[38;5;178m",
69
+ stairs: "\x1b[38;5;141m",
70
+ panel: "\x1b[38;5;244m",
71
+ place: "\x1b[38;5;37m",
72
+ occ: "\x1b[38;5;33m\x1b[1m",
73
+ label: "\x1b[38;5;252m",
74
+ chrome: "\x1b[38;5;250m",
75
+ dim: "\x1b[38;5;245m",
76
+ };
77
+ const RESET = "\x1b[0m";
78
+
79
+ /**
80
+ * Terminal map from a laid-out spatial graph.
81
+ * UTF-8 default: junction-aware box drawing, rounded bends, ANSI color,
82
+ * curved stairs (quadratic samples). `unicode: false` → 7-bit ASCII, no color.
83
+ */
84
+ export function renderSpatialGraphAscii(
85
+ model: SpatialGraphModel,
86
+ layout: SpatialGraphLayout,
87
+ options: RenderAsciiOptions = {},
88
+ ): string {
89
+ const maxW = Math.max(24, Math.min(options.width ?? 72, 120));
90
+ const maxH = Math.max(10, Math.min(options.height ?? 28, 60));
91
+ const utf8 = options.unicode !== false;
92
+ const color =
93
+ options.color !== undefined
94
+ ? options.color
95
+ : utf8 && process.env.NO_COLOR === undefined && process.env.FORCE_COLOR !== "0";
96
+ const hideOcc = options.hideOccupancy === true;
97
+ const chromeOn = options.chrome !== false;
98
+
99
+ if (layout.nodes.length === 0) {
100
+ return chromeOn ? `Spatial map (${utf8 ? "utf-8" : "ascii"}): empty layout.` : "";
101
+ }
102
+
103
+ const occ = new Map(model.actorClusters.map((c) => [c.placeNodeId, c.count] as const));
104
+ const cross = new Set(layout.crossFloorEdgeKeys ?? []);
105
+ const box = utf8 ? BOX_ROUND : null;
106
+
107
+ const pad = 1;
108
+ const plotW = maxW - pad * 2;
109
+ const plotH = maxH - pad * 2;
110
+
111
+ const bounds = mapBounds(layout.nodes);
112
+ if (bounds.maxX - bounds.minX < 1e-6) {
113
+ bounds.minX -= 1;
114
+ bounds.maxX += 1;
115
+ }
116
+ if (bounds.maxY - bounds.minY < 1e-6) {
117
+ bounds.minY -= 1;
118
+ bounds.maxY += 1;
119
+ }
120
+
121
+ const grid: Cell[][] = Array.from({ length: maxH }, () =>
122
+ Array.from({ length: maxW }, () => ({ ch: " ", ink: "dim" as Ink, prio: 0 })),
123
+ );
124
+
125
+ // Orthogonal masks per ink layer so stairs don't merge into routes.
126
+ const routeMask = zeros(maxH, maxW);
127
+ const stairsMask = zeros(maxH, maxW);
128
+ const diag = blankStr(maxH, maxW);
129
+ const diagInk: Ink[][] = Array.from({ length: maxH }, () =>
130
+ Array.from({ length: maxW }, () => "route" as Ink),
131
+ );
132
+
133
+ // Floor panels — double-line rounded feel
134
+ for (const p of layout.floorPanels ?? []) {
135
+ const corners = [
136
+ project(p.minX, p.minY, bounds, plotW, plotH, pad),
137
+ project(p.maxX, p.minY, bounds, plotW, plotH, pad),
138
+ project(p.maxX, p.maxY, bounds, plotW, plotH, pad),
139
+ project(p.minX, p.maxY, bounds, plotW, plotH, pad),
140
+ ];
141
+ drawPanel(grid, corners, utf8);
142
+ const title = p.title.length > plotW - 6 ? `${p.title.slice(0, plotW - 7)}…` : p.title;
143
+ const [tx, ty] = corners[0]!;
144
+ putStr(grid, tx + 2, ty, ` ${title} `, "panel", 2);
145
+ }
146
+
147
+ const nodePx = new Map<string, { x: number; y: number; node: SpatialGraphNode }>();
148
+ for (const node of layout.nodes) {
149
+ const [x, y] = project(node.x, node.y, bounds, plotW, plotH, pad);
150
+ nodePx.set(node.id, { x, y, node });
151
+ }
152
+
153
+ const seen = new Set<string>();
154
+ const stairMids: Array<{ x: number; y: number }> = [];
155
+ for (const edge of layout.edges) {
156
+ const a = nodePx.get(edge.from);
157
+ const b = nodePx.get(edge.to);
158
+ if (a === undefined || b === undefined) continue;
159
+ // Composed tactical cells are already represented by their marks; their
160
+ // dense lattice of adjacency routes adds noise rather than information.
161
+ if (a.node.unlabeled && b.node.unlabeled) continue;
162
+ const key = edge.from < edge.to ? `${edge.from}|${edge.to}` : `${edge.to}|${edge.from}`;
163
+ if (seen.has(key)) continue;
164
+ seen.add(key);
165
+ const stairs = cross.has(key);
166
+ if (stairs) {
167
+ const mid = strokeCurve(a.x, a.y, b.x, b.y, stairsMask, diag, diagInk, "stairs", utf8);
168
+ if (mid) stairMids.push(mid);
169
+ } else {
170
+ strokeStraight(a.x, a.y, b.x, b.y, routeMask, diag, diagInk, "route", utf8);
171
+ }
172
+ }
173
+
174
+ // Resolve masks → glyphs
175
+ for (let y = 0; y < maxH; y++) {
176
+ for (let x = 0; x < maxW; x++) {
177
+ const sm = stairsMask[y]![x]!;
178
+ const rm = routeMask[y]![x]!;
179
+ if (sm !== 0) {
180
+ const ch = utf8 ? (BOX[sm] ?? "│") : asciiFromMask(sm);
181
+ // Prefer wavy/double feel for pure ortho stairs
182
+ const pretty = utf8 && sm === (N | S) ? "║" : utf8 && sm === (E | W) ? "═" : ch;
183
+ put(grid, x, y, pretty, "stairs", 1);
184
+ } else if (rm !== 0) {
185
+ const table = box ?? null;
186
+ const ch = utf8 ? (table?.[rm] ?? BOX[rm] ?? "│") : asciiFromMask(rm);
187
+ put(grid, x, y, ch, "route", 1);
188
+ } else if (diag[y]![x] !== " ") {
189
+ put(grid, x, y, diag[y]![x]!, diagInk[y]![x]!, 1);
190
+ }
191
+ }
192
+ }
193
+
194
+ // Stairs captions at curve midpoints
195
+ if (utf8) {
196
+ for (const m of stairMids) {
197
+ putStr(grid, m.x - 2, m.y, "↕", "stairs", 3);
198
+ putStr(grid, m.x, m.y, "stairs", "dim", 2);
199
+ }
200
+ }
201
+
202
+ // Nodes + labels
203
+ for (const { x, y, node } of nodePx.values()) {
204
+ const count = hideOcc ? 0 : (occ.get(node.id) ?? 0);
205
+ const mark = utf8 ? (count > 0 ? "●" : "○") : count > 0 ? "@" : "o";
206
+ put(grid, x, y, mark, count > 0 ? "occ" : "place", 4);
207
+ if (count > 0 && count < 10) {
208
+ put(grid, x + 1, y - 1, String(count), "occ", 4);
209
+ }
210
+ if (!node.unlabeled) {
211
+ const label = shortLabel(node.label);
212
+ const lx = x + 2 + label.length < maxW ? x + 2 : Math.max(0, x - 1 - label.length);
213
+ putStr(grid, lx, y, label, "label", 3);
214
+ }
215
+ }
216
+
217
+ const body = grid
218
+ .map((row) => {
219
+ let line = "";
220
+ let cur: Ink | null = null;
221
+ for (const cell of row) {
222
+ if (cell.ch === " " && cell.prio === 0) {
223
+ if (color && cur !== null) {
224
+ line += RESET;
225
+ cur = null;
226
+ }
227
+ line += " ";
228
+ continue;
229
+ }
230
+ if (color) {
231
+ if (cur !== cell.ink) {
232
+ if (cur !== null) line += RESET;
233
+ line += ANSI[cell.ink];
234
+ cur = cell.ink;
235
+ }
236
+ line += cell.ch;
237
+ } else {
238
+ line += cell.ch;
239
+ }
240
+ }
241
+ if (color && cur !== null) line += RESET;
242
+ return line.replace(/\s+$/g, "");
243
+ })
244
+ .join("\n");
245
+
246
+ if (!chromeOn) return body;
247
+
248
+ const title =
249
+ model.graphs[0]?.displayName?.trim() ||
250
+ model.graphs[0]?.graphId ||
251
+ model.root ||
252
+ "spatial graph";
253
+ const sub = model.sliceLabel ?? "";
254
+ const mode = utf8 ? "utf-8" : "ascii";
255
+ const paint = (ink: Ink, s: string) => (color ? `${ANSI[ink]}${s}${RESET}` : s);
256
+ const fontHint = process.env.SPELLKAVE_MAP_FONT_HINT?.trim() || "JetBrainsMono Nerd Font Mono";
257
+ const header = [
258
+ paint("chrome", `Spatial map (${mode}) — ${title}${sub ? ` · ${sub}` : ""}`),
259
+ paint(
260
+ "dim",
261
+ `${countMapPlaces(model)} places · ${model.edges.length} routes` +
262
+ (hideOcc
263
+ ? ""
264
+ : ` · ${paint("occ", utf8 ? "●" : "@")}${paint("dim", "=occ ")}` +
265
+ `${paint("place", utf8 ? "○" : "o")}${paint("dim", "=empty")}`),
266
+ ),
267
+ paint("route", utf8 ? "─│╭╮ routes" : "-| routes") +
268
+ (cross.size > 0
269
+ ? paint("dim", " · ") + paint("stairs", utf8 ? "═║↕ stairs / floor-link" : "~ stairs")
270
+ : ""),
271
+ paint("dim", `font: ${fontHint} (just map-font-check)`),
272
+ ].join("\n");
273
+ const rule = paint("dim", (utf8 ? "─" : "-").repeat(Math.min(maxW, 40)));
274
+ return `${header}\n${rule}\n${body}`;
275
+ }
276
+
277
+ function zeros(h: number, w: number): number[][] {
278
+ return Array.from({ length: h }, () => Array.from({ length: w }, () => 0));
279
+ }
280
+
281
+ function blankStr(h: number, w: number): string[][] {
282
+ return Array.from({ length: h }, () => Array.from({ length: w }, () => " "));
283
+ }
284
+
285
+ function shortLabel(name: string): string {
286
+ const t = name.trim();
287
+ if (t.length <= 14) return t;
288
+ return `${t.slice(0, 12)}…`;
289
+ }
290
+
291
+ function project(
292
+ x: number,
293
+ y: number,
294
+ bounds: { minX: number; minY: number; maxX: number; maxY: number },
295
+ plotW: number,
296
+ plotH: number,
297
+ pad: number,
298
+ ): [number, number] {
299
+ const dx = bounds.maxX - bounds.minX;
300
+ const dy = bounds.maxY - bounds.minY;
301
+ const px = pad + Math.round(((x - bounds.minX) / dx) * (plotW - 1));
302
+ const py = pad + Math.round(((y - bounds.minY) / dy) * (plotH - 1));
303
+ return [px, py];
304
+ }
305
+
306
+ function put(grid: Cell[][], x: number, y: number, ch: string, ink: Ink, prio: number): void {
307
+ if (y < 0 || y >= grid.length || x < 0 || x >= (grid[0]?.length ?? 0)) return;
308
+ const cell = grid[y]![x]!;
309
+ if (prio < cell.prio) return;
310
+ cell.ch = ch[0] ?? " ";
311
+ cell.ink = ink;
312
+ cell.prio = prio;
313
+ }
314
+
315
+ function putStr(grid: Cell[][], x: number, y: number, text: string, ink: Ink, prio: number): void {
316
+ for (let i = 0; i < text.length; i++) put(grid, x + i, y, text[i]!, ink, prio);
317
+ }
318
+
319
+ function orMask(mask: number[][], x: number, y: number, bit: number): void {
320
+ if (y < 0 || y >= mask.length || x < 0 || x >= (mask[0]?.length ?? 0)) return;
321
+ mask[y]![x] = (mask[y]![x] ?? 0) | bit;
322
+ }
323
+
324
+ function asciiFromMask(mask: number): string {
325
+ if ((mask & (N | S)) !== 0 && (mask & (E | W)) === 0) return "|";
326
+ if ((mask & (E | W)) !== 0 && (mask & (N | S)) === 0) return "-";
327
+ return "+";
328
+ }
329
+
330
+ function strokeStraight(
331
+ x0: number,
332
+ y0: number,
333
+ x1: number,
334
+ y1: number,
335
+ mask: number[][],
336
+ diag: string[][],
337
+ diagInk: Ink[][],
338
+ ink: Ink,
339
+ utf8: boolean,
340
+ ): void {
341
+ const pts = bresenham(x0, y0, x1, y1);
342
+ paintPolyline(pts, mask, diag, diagInk, ink, utf8);
343
+ }
344
+
345
+ /** Quadratic curve for stairs/floor-links — gentle bulge, not a dashed slash. */
346
+ function strokeCurve(
347
+ x0: number,
348
+ y0: number,
349
+ x1: number,
350
+ y1: number,
351
+ mask: number[][],
352
+ diag: string[][],
353
+ diagInk: Ink[][],
354
+ ink: Ink,
355
+ utf8: boolean,
356
+ ): { x: number; y: number } | null {
357
+ const dx = x1 - x0;
358
+ const dy = y1 - y0;
359
+ const len = Math.hypot(dx, dy) || 1;
360
+ // Perpendicular bulge ~22% of span (readable S-curve without chaos).
361
+ const ox = (-dy / len) * len * 0.22;
362
+ const oy = (dx / len) * len * 0.22;
363
+ const cx = (x0 + x1) / 2 + ox;
364
+ const cy = (y0 + y1) / 2 + oy;
365
+ const samples = Math.max(12, Math.ceil(len * 1.5));
366
+ const pts: Array<[number, number]> = [];
367
+ for (let i = 0; i <= samples; i++) {
368
+ const t = i / samples;
369
+ const u = 1 - t;
370
+ const x = u * u * x0 + 2 * u * t * cx + t * t * x1;
371
+ const y = u * u * y0 + 2 * u * t * cy + t * t * y1;
372
+ pts.push([Math.round(x), Math.round(y)]);
373
+ }
374
+ // Dedup consecutive
375
+ const slim: Array<[number, number]> = [];
376
+ for (const p of pts) {
377
+ const last = slim[slim.length - 1];
378
+ if (!last || last[0] !== p[0] || last[1] !== p[1]) slim.push(p);
379
+ }
380
+ paintPolyline(slim, mask, diag, diagInk, ink, utf8);
381
+ const mid = slim[Math.floor(slim.length / 2)];
382
+ return mid ? { x: mid[0], y: mid[1] } : null;
383
+ }
384
+
385
+ function bresenham(x0: number, y0: number, x1: number, y1: number): Array<[number, number]> {
386
+ const out: Array<[number, number]> = [];
387
+ let x = x0;
388
+ let y = y0;
389
+ const dx = Math.abs(x1 - x0);
390
+ const dy = Math.abs(y1 - y0);
391
+ const sx = x0 < x1 ? 1 : -1;
392
+ const sy = y0 < y1 ? 1 : -1;
393
+ let err = dx - dy;
394
+ for (;;) {
395
+ out.push([x, y]);
396
+ if (x === x1 && y === y1) break;
397
+ const e2 = 2 * err;
398
+ if (e2 > -dy) {
399
+ err -= dy;
400
+ x += sx;
401
+ }
402
+ if (e2 < dx) {
403
+ err += dx;
404
+ y += sy;
405
+ }
406
+ }
407
+ return out;
408
+ }
409
+
410
+ function paintPolyline(
411
+ pts: Array<[number, number]>,
412
+ mask: number[][],
413
+ diag: string[][],
414
+ diagInk: Ink[][],
415
+ ink: Ink,
416
+ utf8: boolean,
417
+ ): void {
418
+ if (pts.length === 0) return;
419
+ // Skip pure endpoints so node glyphs win
420
+ for (let i = 1; i < pts.length; i++) {
421
+ const [x0, y0] = pts[i - 1]!;
422
+ const [x1, y1] = pts[i]!;
423
+ const isLast = i === pts.length - 1;
424
+ const stepDx = x1 - x0;
425
+ const stepDy = y1 - y0;
426
+ if (stepDx === 0 && stepDy === 0) continue;
427
+
428
+ const ortho = stepDx === 0 || stepDy === 0;
429
+ if (ortho) {
430
+ if (stepDx === 1) {
431
+ orMask(mask, x0, y0, E);
432
+ orMask(mask, x1, y1, W);
433
+ } else if (stepDx === -1) {
434
+ orMask(mask, x0, y0, W);
435
+ orMask(mask, x1, y1, E);
436
+ } else if (stepDy === 1) {
437
+ orMask(mask, x0, y0, S);
438
+ orMask(mask, x1, y1, N);
439
+ } else if (stepDy === -1) {
440
+ orMask(mask, x0, y0, N);
441
+ orMask(mask, x1, y1, S);
442
+ } else {
443
+ // long ortho jump (shouldn't after bresenham) — fill
444
+ for (const [x, y] of bresenham(x0, y0, x1, y1)) {
445
+ if (stepDx !== 0) orMask(mask, x, y, E | W);
446
+ if (stepDy !== 0) orMask(mask, x, y, N | S);
447
+ }
448
+ }
449
+ } else {
450
+ // Diagonal step: use ╱ ╲ — skip polyline endpoints so node glyphs win.
451
+ const ch = utf8 ? (stepDx === stepDy ? "╲" : "╱") : stepDx === stepDy ? "\\" : "/";
452
+ if (isLast) continue;
453
+ if (y1 >= 0 && y1 < diag.length && x1 >= 0 && x1 < (diag[0]?.length ?? 0)) {
454
+ diag[y1]![x1] = ch;
455
+ diagInk[y1]![x1] = ink;
456
+ }
457
+ }
458
+ }
459
+ }
460
+
461
+ function drawPanel(grid: Cell[][], corners: Array<[number, number]>, utf8: boolean): void {
462
+ const xs = corners.map((c) => c[0]);
463
+ const ys = corners.map((c) => c[1]);
464
+ const minX = Math.min(...xs);
465
+ const maxX = Math.max(...xs);
466
+ const minY = Math.min(...ys);
467
+ const maxY = Math.max(...ys);
468
+ const h = utf8 ? "─" : "-";
469
+ const v = utf8 ? "│" : "|";
470
+ const tl = utf8 ? "╭" : "+";
471
+ const tr = utf8 ? "╮" : "+";
472
+ const bl = utf8 ? "╰" : "+";
473
+ const br = utf8 ? "╯" : "+";
474
+ for (let x = minX; x <= maxX; x++) {
475
+ put(grid, x, minY, h, "panel", 1);
476
+ put(grid, x, maxY, h, "panel", 1);
477
+ }
478
+ for (let y = minY; y <= maxY; y++) {
479
+ put(grid, minX, y, v, "panel", 1);
480
+ put(grid, maxX, y, v, "panel", 1);
481
+ }
482
+ put(grid, minX, minY, tl, "panel", 1);
483
+ put(grid, maxX, minY, tr, "panel", 1);
484
+ put(grid, minX, maxY, bl, "panel", 1);
485
+ put(grid, maxX, maxY, br, "panel", 1);
486
+ }
@@ -0,0 +1,130 @@
1
+ /**
2
+ * Braille terminal map via termdot (modern drawille successor).
3
+ * Dense geometry from the same layout as SVG/UTF-8; labels via setText.
4
+ */
5
+ import { createCanvas, frame, line, set, setText } from "termdot";
6
+
7
+ import { countMapPlaces } from "./inventory.ts";
8
+ import { mapBounds } from "./layout.ts";
9
+ import type { SpatialGraphLayout, SpatialGraphModel } from "./types.ts";
10
+
11
+ export interface RenderBrailleOptions {
12
+ /** Terminal character columns (default 56). Braille pixels = cols * 2. */
13
+ width?: number;
14
+ /** Terminal character rows (default 22). Braille pixels = rows * 4. */
15
+ height?: number;
16
+ hideOccupancy?: boolean;
17
+ chrome?: boolean;
18
+ }
19
+
20
+ /**
21
+ * High-density UTF-8 braille map. Prefer box-drawing UTF-8 for labeled
22
+ * agent readability; use braille when geometry density matters.
23
+ */
24
+ export function renderSpatialGraphBraille(
25
+ model: SpatialGraphModel,
26
+ layout: SpatialGraphLayout,
27
+ options: RenderBrailleOptions = {},
28
+ ): string {
29
+ const cols = Math.max(20, Math.min(options.width ?? 56, 100));
30
+ const rows = Math.max(8, Math.min(options.height ?? 22, 48));
31
+ const hideOcc = options.hideOccupancy === true;
32
+ const chrome = options.chrome !== false;
33
+
34
+ if (layout.nodes.length === 0) {
35
+ return chrome ? "Spatial map (braille/termdot): empty layout." : "";
36
+ }
37
+
38
+ // Braille cell = 2×4 dots → pixel canvas size
39
+ const pxW = cols * 2;
40
+ const pxH = rows * 4;
41
+ const pad = 3;
42
+
43
+ const bounds = mapBounds(layout.nodes);
44
+ if (bounds.maxX - bounds.minX < 1e-6) {
45
+ bounds.minX -= 1;
46
+ bounds.maxX += 1;
47
+ }
48
+ if (bounds.maxY - bounds.minY < 1e-6) {
49
+ bounds.minY -= 1;
50
+ bounds.maxY += 1;
51
+ }
52
+
53
+ const toPx = (x: number, y: number): [number, number] => {
54
+ const dx = bounds.maxX - bounds.minX;
55
+ const dy = bounds.maxY - bounds.minY;
56
+ const px = pad + Math.round(((x - bounds.minX) / dx) * (pxW - pad * 2 - 1));
57
+ const py = pad + Math.round(((y - bounds.minY) / dy) * (pxH - pad * 2 - 1));
58
+ return [px, py];
59
+ };
60
+
61
+ const c = createCanvas();
62
+ const occ = new Map(model.actorClusters.map((cl) => [cl.placeNodeId, cl.count] as const));
63
+ const cross = new Set(layout.crossFloorEdgeKeys ?? []);
64
+
65
+ // Floor panel outlines
66
+ for (const panel of layout.floorPanels ?? []) {
67
+ const [x0, y0] = toPx(panel.minX, panel.minY);
68
+ const [x1, y1] = toPx(panel.maxX, panel.maxY);
69
+ for (const pt of line(x0, y0, x1, y0)) set(c, pt.x, pt.y);
70
+ for (const pt of line(x1, y0, x1, y1)) set(c, pt.x, pt.y);
71
+ for (const pt of line(x1, y1, x0, y1)) set(c, pt.x, pt.y);
72
+ for (const pt of line(x0, y1, x0, y0)) set(c, pt.x, pt.y);
73
+ }
74
+
75
+ const nodePx = new Map<string, { x: number; y: number; id: string; label: string }>();
76
+ for (const n of layout.nodes) {
77
+ const [x, y] = toPx(n.x, n.y);
78
+ nodePx.set(n.id, { x, y, id: n.id, label: n.label });
79
+ }
80
+
81
+ const seen = new Set<string>();
82
+ for (const e of layout.edges) {
83
+ const a = nodePx.get(e.from);
84
+ const b = nodePx.get(e.to);
85
+ if (a === undefined || b === undefined) continue;
86
+ const key = e.from < e.to ? `${e.from}|${e.to}` : `${e.to}|${e.from}`;
87
+ if (seen.has(key)) continue;
88
+ seen.add(key);
89
+ const stairs = cross.has(key);
90
+ // Stairs: dashed by skipping every other segment point
91
+ let i = 0;
92
+ for (const pt of line(a.x, a.y, b.x, b.y)) {
93
+ if (!stairs || i % 3 !== 1) set(c, pt.x, pt.y);
94
+ i++;
95
+ }
96
+ }
97
+
98
+ // Node blobs + labels
99
+ for (const n of nodePx.values()) {
100
+ const count = hideOcc ? 0 : (occ.get(n.id) ?? 0);
101
+ const r = count > 0 ? 2 : 1;
102
+ for (let dy = -r; dy <= r; dy++) {
103
+ for (let dx = -r; dx <= r; dx++) {
104
+ if (dx * dx + dy * dy <= r * r + 0.5) set(c, n.x + dx, n.y + dy);
105
+ }
106
+ }
107
+ const label = n.label.length > 12 ? `${n.label.slice(0, 10)}…` : n.label;
108
+ const tag = count > 0 ? `${label}·${count}` : label;
109
+ // setText uses pixel coords; place to the right of the blob
110
+ setText(c, n.x + r + 2, n.y - 1, tag);
111
+ }
112
+
113
+ const body = frame(c);
114
+ if (!chrome) return body;
115
+
116
+ const title =
117
+ model.graphs[0]?.displayName?.trim() ||
118
+ model.graphs[0]?.graphId ||
119
+ model.root ||
120
+ "spatial graph";
121
+ const sub = model.sliceLabel ?? "";
122
+ const fontHint = process.env.SPELLKAVE_MAP_FONT_HINT?.trim() || "JetBrainsMono Nerd Font Mono";
123
+ const header = [
124
+ `Spatial map (braille/termdot) — ${title}${sub ? ` · ${sub}` : ""}`,
125
+ `${countMapPlaces(model)} places · ${model.edges.length} routes · dense UTF-8 dots`,
126
+ "Labels via termdot setText; stairs dashed.",
127
+ `font: ${fontHint} (just map-font-check)`,
128
+ ].join("\n");
129
+ return `${header}\n${"─".repeat(40)}\n${body}`;
130
+ }