@dynamicagents/plugins 0.12.0 → 0.14.0
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.
- package/README.md +58 -77
- package/dist/browser/index.d.ts +0 -2
- package/dist/browser/index.js +21 -27
- package/dist/claude-code/cards.d.ts +55 -0
- package/dist/claude-code/cards.js +283 -0
- package/dist/claude-code/config.d.ts +28 -63
- package/dist/claude-code/config.js +0 -11
- package/dist/claude-code/egress.d.ts +8 -1
- package/dist/claude-code/egress.js +42 -4
- package/dist/claude-code/events.d.ts +106 -9
- package/dist/claude-code/events.js +201 -56
- package/dist/claude-code/github.d.ts +39 -0
- package/dist/claude-code/github.js +82 -0
- package/dist/claude-code/index.d.ts +16 -90
- package/dist/claude-code/index.js +14 -276
- package/dist/claude-code/model.d.ts +152 -0
- package/dist/claude-code/model.js +366 -0
- package/dist/claude-code/run.d.ts +230 -116
- package/dist/claude-code/run.js +380 -167
- package/dist/claude-code/session.d.ts +131 -0
- package/dist/claude-code/session.js +288 -0
- package/dist/claude-code/spec.d.ts +46 -0
- package/dist/claude-code/spec.js +83 -0
- package/dist/claude-code/verified.d.ts +15 -0
- package/dist/claude-code/verified.js +15 -0
- package/dist/computer/context.d.ts +1 -5
- package/dist/computer/context.js +7 -10
- package/dist/computer/file-lock.js +5 -4
- package/dist/computer/gate.d.ts +4 -4
- package/dist/computer/gate.js +5 -5
- package/dist/computer/index.d.ts +40 -258
- package/dist/computer/index.js +71 -324
- package/dist/computer/paths.d.ts +36 -16
- package/dist/computer/paths.js +42 -29
- package/dist/computer/proxy.d.ts +45 -0
- package/dist/computer/proxy.js +234 -0
- package/dist/computer/render.d.ts +1 -40
- package/dist/computer/render.js +2 -68
- package/dist/computer/tools-exec.js +12 -13
- package/dist/computer/tools-file.d.ts +9 -1
- package/dist/computer/tools-file.js +22 -82
- package/dist/computer/{tools-find.d.ts → tools-grep.d.ts} +1 -1
- package/dist/computer/tools-grep.js +129 -0
- package/dist/repo/checkout.d.ts +4 -5
- package/dist/repo/checkout.js +31 -8
- package/dist/repo/context.d.ts +35 -9
- package/dist/repo/context.js +101 -49
- package/dist/repo/index.d.ts +73 -28
- package/dist/repo/index.js +112 -96
- package/dist/repo/tools-checks.d.ts +3 -0
- package/dist/repo/tools-checks.js +112 -0
- package/dist/repo/tools-clone.js +1 -1
- package/dist/repo/tools-pr.d.ts +14 -0
- package/dist/repo/{tools-forge.js → tools-pr.js} +53 -36
- package/dist/repo/tools-review.d.ts +9 -0
- package/dist/repo/tools-review.js +67 -47
- package/dist/repo/tools-worktree.js +144 -60
- package/dist/repo/tools-worktrees.d.ts +12 -0
- package/dist/repo/tools-worktrees.js +48 -0
- package/dist/repo/url.d.ts +2 -2
- package/dist/repo/url.js +7 -7
- package/dist/scratch/index.d.ts +4 -4
- package/dist/scratch/index.js +12 -11
- package/dist/{computer → workspace}/advisory.d.ts +4 -4
- package/dist/{computer → workspace}/advisory.js +2 -2
- package/dist/{computer/host → workspace}/ca-trust.d.ts +8 -1
- package/dist/{computer/host → workspace}/ca-trust.js +12 -3
- package/dist/{computer/host → workspace}/container-deps.d.ts +4 -0
- package/dist/{computer/host → workspace}/container-deps.js +26 -1
- package/dist/{computer/host → workspace}/container-fault.d.ts +3 -3
- package/dist/{computer/host → workspace}/container-fault.js +23 -8
- package/dist/workspace/deps-snapshot.d.ts +110 -0
- package/dist/workspace/deps-snapshot.js +424 -0
- package/dist/workspace/exec.d.ts +94 -0
- package/dist/workspace/exec.js +153 -0
- package/dist/workspace/format.d.ts +36 -0
- package/dist/workspace/format.js +57 -0
- package/dist/{computer/host → workspace}/git-host.d.ts +21 -4
- package/dist/{computer/host → workspace}/git-host.js +72 -2
- package/dist/{computer/host → workspace}/git-identity.d.ts +1 -1
- package/dist/{computer/host → workspace}/git-identity.js +1 -1
- package/dist/workspace/index.d.ts +44 -47
- package/dist/workspace/index.js +52 -112
- package/dist/{computer/host → workspace}/install-job.d.ts +53 -4
- package/dist/{computer/host → workspace}/install-job.js +346 -178
- package/dist/{computer → workspace}/install.d.ts +17 -8
- package/dist/{computer → workspace}/install.js +24 -6
- package/dist/{computer/host/workspace.d.ts → workspace/object.d.ts} +125 -27
- package/dist/{computer/host/workspace.js → workspace/object.js} +341 -58
- package/dist/workspace/open.d.ts +95 -0
- package/dist/workspace/open.js +63 -0
- package/dist/workspace/read.d.ts +16 -0
- package/dist/workspace/read.js +22 -0
- package/dist/{computer → workspace}/shell.d.ts +5 -5
- package/dist/{computer → workspace}/shell.js +6 -6
- package/dist/{computer/host → workspace}/wake.d.ts +3 -1
- package/package.json +17 -37
- package/dist/arc-agi/analysis.d.ts +0 -322
- package/dist/arc-agi/analysis.js +0 -802
- package/dist/arc-agi/client.d.ts +0 -82
- package/dist/arc-agi/client.js +0 -113
- package/dist/arc-agi/game-tools.d.ts +0 -24
- package/dist/arc-agi/game-tools.js +0 -26
- package/dist/arc-agi/index.d.ts +0 -40
- package/dist/arc-agi/index.js +0 -175
- package/dist/arc-agi/main-agent.d.ts +0 -24
- package/dist/arc-agi/main-agent.js +0 -62
- package/dist/arc-agi/recipe.d.ts +0 -48
- package/dist/arc-agi/recipe.js +0 -97
- package/dist/arc-agi/scorecard.d.ts +0 -118
- package/dist/arc-agi/scorecard.js +0 -163
- package/dist/arc-agi/soul.d.ts +0 -29
- package/dist/arc-agi/soul.js +0 -79
- package/dist/arc-agi/store.d.ts +0 -153
- package/dist/arc-agi/store.js +0 -215
- package/dist/arc-agi/tools.d.ts +0 -36
- package/dist/arc-agi/tools.js +0 -574
- package/dist/arc-agi/types.d.ts +0 -211
- package/dist/arc-agi/types.js +0 -19
- package/dist/claude-code/recipe.d.ts +0 -53
- package/dist/claude-code/recipe.js +0 -100
- package/dist/computer/read.d.ts +0 -133
- package/dist/computer/read.js +0 -200
- package/dist/computer/tools-find.js +0 -216
- package/dist/recall/index.d.ts +0 -105
- package/dist/recall/index.js +0 -181
- package/dist/repo/tools-forge.d.ts +0 -4
- package/dist/triage/index.d.ts +0 -360
- package/dist/triage/index.js +0 -311
- /package/dist/{computer/host → workspace}/sync.d.ts +0 -0
- /package/dist/{computer/host → workspace}/sync.js +0 -0
- /package/dist/{computer/host → workspace}/wake.js +0 -0
package/dist/arc-agi/analysis.js
DELETED
|
@@ -1,802 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pure grid-analysis helpers for ARC-AGI-3 frames. No I/O — every function is a
|
|
3
|
-
* deterministic transform of a 64×64 grid (color values 0–15), unit-tested in
|
|
4
|
-
* isolation. The tool family calls these to render compact observations for the
|
|
5
|
-
* model instead of dumping raw grids into context.
|
|
6
|
-
*
|
|
7
|
-
* Two rendering registers, deliberately kept apart:
|
|
8
|
-
*
|
|
9
|
-
* - **Prose** (diffs, shapes, histogram) names its colors: `blue->white` carries
|
|
10
|
-
* structure a bare `9->0` hides, and no tool input ever takes a color, so the
|
|
11
|
-
* index is never needed as an argument.
|
|
12
|
-
* - **Grids** ({@link renderGrid}, {@link renderRegion}) stay one character per
|
|
13
|
-
* cell and carry a {@link renderLegend} instead. One char per cell is the only
|
|
14
|
-
* reason a 64×64 board is legible at all; a named cell would be unreadable and
|
|
15
|
-
* cost ~50× the tokens.
|
|
16
|
-
*/
|
|
17
|
-
/**
|
|
18
|
-
* The ARC-AGI-3 16-color palette, index → name.
|
|
19
|
-
*
|
|
20
|
-
* Source: `agents/templates/multimodal.py` in `arcprize/ARC-AGI-3-Agents` (ARC
|
|
21
|
-
* Prize's own agent template), which maps these indices to RGBA for multimodal
|
|
22
|
-
* play. Taken from there rather than assumed, because **ARC-AGI-3's palette is
|
|
23
|
-
* unrelated to ARC-AGI-1's**: here 0 is *white* and 1–5 are a greyscale ramp,
|
|
24
|
-
* where the familiar ARC-AGI-1 mapping would say 0=black, 1=blue, 2=red. Note
|
|
25
|
-
* that same repo carries a second, contradictory `COLOR_PALETTE` in
|
|
26
|
-
* `langgraph_thinking/vision.py` — that one is incomplete and does not match the
|
|
27
|
-
* engine; do not use it.
|
|
28
|
-
*
|
|
29
|
-
* The pairs matter to a player: 6/7 and 9/10 and 2/3 are related shades, which
|
|
30
|
-
* the names convey and the indices do not.
|
|
31
|
-
*/
|
|
32
|
-
export const COLOR_NAMES = [
|
|
33
|
-
"white", // 0 #FFFFFF
|
|
34
|
-
"off-white", // 1 #CCCCCC
|
|
35
|
-
"neutral-light", // 2 #999999
|
|
36
|
-
"neutral", // 3 #666666
|
|
37
|
-
"off-black", // 4 #333333
|
|
38
|
-
"black", // 5 #000000
|
|
39
|
-
"magenta", // 6 #E53AA3
|
|
40
|
-
"magenta-light", // 7 #FF7BCC
|
|
41
|
-
"red", // 8 #F93C31
|
|
42
|
-
"blue", // 9 #1E93FF
|
|
43
|
-
"blue-light", // 10 #88D8F1
|
|
44
|
-
"yellow", // 11 #FFDC00
|
|
45
|
-
"orange", // 12 #FF851B
|
|
46
|
-
"maroon", // 13 #921231
|
|
47
|
-
"green", // 14 #4FCC30
|
|
48
|
-
"purple" // 15 #A356D6
|
|
49
|
-
];
|
|
50
|
-
/** Color 0 (white) — treated as background by {@link locateComponents}. */
|
|
51
|
-
export const BACKGROUND_COLOR = 0;
|
|
52
|
-
/**
|
|
53
|
-
* Name of a color index. An out-of-range value renders as `color <n>` rather
|
|
54
|
-
* than throwing: a malformed frame should degrade the wording, not fail the turn.
|
|
55
|
-
*/
|
|
56
|
-
export function colorName(color) {
|
|
57
|
-
return COLOR_NAMES[color] ?? `color ${color}`;
|
|
58
|
-
}
|
|
59
|
-
/** The single character a color occupies in a grid render. */
|
|
60
|
-
function colorChar(color) {
|
|
61
|
-
return color >= 0 && color < 16 ? color.toString(16) : "?";
|
|
62
|
-
}
|
|
63
|
-
/** The current board: the LAST grid of a frame response's grid array. */
|
|
64
|
-
export function lastGrid(frame) {
|
|
65
|
-
return frame.length === 0 ? [] : frame[frame.length - 1];
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Bare hex rows, one char per cell, no labels — the round-trippable **storage**
|
|
69
|
-
* form, paired with {@link parseGrid}. Deliberately distinct from
|
|
70
|
-
* {@link renderGrid}, which adds a legend, rulers and row collapsing and so
|
|
71
|
-
* cannot be parsed back.
|
|
72
|
-
*/
|
|
73
|
-
export function serializeGrid(grid) {
|
|
74
|
-
return grid.map((row) => row.map(colorChar).join("")).join("\n");
|
|
75
|
-
}
|
|
76
|
-
/** Inverse of {@link serializeGrid}. */
|
|
77
|
-
export function parseGrid(hex) {
|
|
78
|
-
if (hex === "")
|
|
79
|
-
return [];
|
|
80
|
-
return hex
|
|
81
|
-
.split("\n")
|
|
82
|
-
.map((line) => [...line].map((ch) => parseInt(ch, 16) || 0));
|
|
83
|
-
}
|
|
84
|
-
/** Cell-by-cell diff of two grids. `cap` bounds the returned cell list. */
|
|
85
|
-
export function diffGrids(a, b, cap = 12) {
|
|
86
|
-
if (a === null)
|
|
87
|
-
return { changed: -1, cells: [], box: null };
|
|
88
|
-
let changed = 0;
|
|
89
|
-
const cells = [];
|
|
90
|
-
let top = Infinity;
|
|
91
|
-
let left = Infinity;
|
|
92
|
-
let bottom = -Infinity;
|
|
93
|
-
let right = -Infinity;
|
|
94
|
-
for (let r = 0; r < b.length; r++) {
|
|
95
|
-
const rowB = b[r];
|
|
96
|
-
const rowA = a[r] ?? [];
|
|
97
|
-
for (let c = 0; c < rowB.length; c++) {
|
|
98
|
-
if (rowA[c] !== rowB[c]) {
|
|
99
|
-
changed++;
|
|
100
|
-
// The box spans every change, so it stays informative even when the
|
|
101
|
-
// cell list is capped away to nothing.
|
|
102
|
-
if (r < top)
|
|
103
|
-
top = r;
|
|
104
|
-
if (r > bottom)
|
|
105
|
-
bottom = r;
|
|
106
|
-
if (c < left)
|
|
107
|
-
left = c;
|
|
108
|
-
if (c > right)
|
|
109
|
-
right = c;
|
|
110
|
-
if (cells.length < cap) {
|
|
111
|
-
cells.push({ row: r, col: c, from: rowA[c] ?? -1, to: rowB[c] });
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
return {
|
|
117
|
-
changed,
|
|
118
|
-
cells,
|
|
119
|
-
box: changed === 0 ? null : { top, left, bottom, right }
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
/** `rows 3-5, cols 10-12` — or a single row/col rendered without the range. */
|
|
123
|
-
export function describeBox(box) {
|
|
124
|
-
const rows = box.top === box.bottom ? `row ${box.top}` : `rows ${box.top}-${box.bottom}`;
|
|
125
|
-
const cols = box.left === box.right
|
|
126
|
-
? `col ${box.left}`
|
|
127
|
-
: `cols ${box.left}-${box.right}`;
|
|
128
|
-
return `${rows}, ${cols}`;
|
|
129
|
-
}
|
|
130
|
-
/** `(3,10) blue->white` — one changed cell, colors named. */
|
|
131
|
-
export function describeCell(cell) {
|
|
132
|
-
const from = cell.from < 0 ? "off-grid" : colorName(cell.from);
|
|
133
|
-
return `(${cell.row},${cell.col}) ${from}->${colorName(cell.to)}`;
|
|
134
|
-
}
|
|
135
|
-
/** Count of each color present, descending by count. */
|
|
136
|
-
export function colorHistogram(grid) {
|
|
137
|
-
const counts = new Map();
|
|
138
|
-
for (const row of grid) {
|
|
139
|
-
for (const c of row)
|
|
140
|
-
counts.set(c, (counts.get(c) ?? 0) + 1);
|
|
141
|
-
}
|
|
142
|
-
return [...counts.entries()]
|
|
143
|
-
.map(([color, count]) => ({ color, count }))
|
|
144
|
-
.sort((x, y) => y.count - x.count);
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* Every 4-connected same-color region, with its bounding box, largest first.
|
|
148
|
-
* Skips {@link BACKGROUND_COLOR}.
|
|
149
|
-
*
|
|
150
|
-
* The bounding boxes are the point: a rollup of counts alone tells the model
|
|
151
|
-
* *that* there are three shapes and never *where* they are, which leaves dumping
|
|
152
|
-
* the whole 64×64 grid as the only way to locate anything. The flood fill has to
|
|
153
|
-
* visit every cell regardless, so tracking min/max row/col is free.
|
|
154
|
-
*/
|
|
155
|
-
export function locateComponents(grid) {
|
|
156
|
-
const rows = grid.length;
|
|
157
|
-
const cols = rows === 0 ? 0 : grid[0].length;
|
|
158
|
-
const seen = Array.from({ length: rows }, () => new Array(cols).fill(false));
|
|
159
|
-
const found = [];
|
|
160
|
-
for (let r = 0; r < rows; r++) {
|
|
161
|
-
for (let c = 0; c < cols; c++) {
|
|
162
|
-
const color = grid[r][c];
|
|
163
|
-
if (color === BACKGROUND_COLOR || seen[r][c])
|
|
164
|
-
continue;
|
|
165
|
-
// Flood-fill this component (iterative, 4-connectivity).
|
|
166
|
-
let size = 0;
|
|
167
|
-
let top = r;
|
|
168
|
-
let left = c;
|
|
169
|
-
let bottom = r;
|
|
170
|
-
let right = c;
|
|
171
|
-
const stack = [[r, c]];
|
|
172
|
-
seen[r][c] = true;
|
|
173
|
-
while (stack.length > 0) {
|
|
174
|
-
const [cr, cc] = stack.pop();
|
|
175
|
-
size++;
|
|
176
|
-
if (cr < top)
|
|
177
|
-
top = cr;
|
|
178
|
-
if (cr > bottom)
|
|
179
|
-
bottom = cr;
|
|
180
|
-
if (cc < left)
|
|
181
|
-
left = cc;
|
|
182
|
-
if (cc > right)
|
|
183
|
-
right = cc;
|
|
184
|
-
const neighbors = [
|
|
185
|
-
[cr - 1, cc],
|
|
186
|
-
[cr + 1, cc],
|
|
187
|
-
[cr, cc - 1],
|
|
188
|
-
[cr, cc + 1]
|
|
189
|
-
];
|
|
190
|
-
for (const [nr, nc] of neighbors) {
|
|
191
|
-
if (nr >= 0 &&
|
|
192
|
-
nr < rows &&
|
|
193
|
-
nc >= 0 &&
|
|
194
|
-
nc < cols &&
|
|
195
|
-
!seen[nr][nc] &&
|
|
196
|
-
grid[nr][nc] === color) {
|
|
197
|
-
seen[nr][nc] = true;
|
|
198
|
-
stack.push([nr, nc]);
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
found.push({ color, size, top, left, bottom, right });
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
return found.sort((x, y) => y.size - x.size);
|
|
206
|
-
}
|
|
207
|
-
/**
|
|
208
|
-
* How much of its bounding box a region actually occupies, 0–1.
|
|
209
|
-
*
|
|
210
|
-
* Every caller passes a {@link Component} from {@link locateComponents} or a
|
|
211
|
-
* {@link unionBox} of those, and a flood fill cannot produce `bottom < top`, so
|
|
212
|
-
* `area` is at least 1 and there is nothing to guard against dividing by.
|
|
213
|
-
*
|
|
214
|
-
* The result is deliberately **not** clamped into 0–1. An out-of-range value here
|
|
215
|
-
* could only mean a caller broke that invariant, and the way to learn that is a
|
|
216
|
-
* region classified absurdly, not a number quietly corrected into looking sane.
|
|
217
|
-
*/
|
|
218
|
-
export function fillRatio(box, size) {
|
|
219
|
-
return size / ((box.bottom - box.top + 1) * (box.right - box.left + 1));
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* The rectangle enclosing several boxes. Takes a **non-empty** list — the reduce
|
|
223
|
-
* has no seed, since there is no identity box to start from, so `[]` throws. Its
|
|
224
|
-
* one caller filters components for a color it is already holding one of, and so
|
|
225
|
-
* cannot pass an empty list.
|
|
226
|
-
*/
|
|
227
|
-
function unionBox(boxes) {
|
|
228
|
-
return boxes.reduce((a, b) => ({
|
|
229
|
-
top: Math.min(a.top, b.top),
|
|
230
|
-
left: Math.min(a.left, b.left),
|
|
231
|
-
bottom: Math.max(a.bottom, b.bottom),
|
|
232
|
-
right: Math.max(a.right, b.right)
|
|
233
|
-
}));
|
|
234
|
-
}
|
|
235
|
-
/** Runs of `color` in one row, as inclusive `[first, last]` column pairs. */
|
|
236
|
-
function rowRuns(row, color) {
|
|
237
|
-
const runs = [];
|
|
238
|
-
let start = -1;
|
|
239
|
-
for (let c = 0; c <= row.length; c++) {
|
|
240
|
-
const hit = c < row.length && row[c] === color;
|
|
241
|
-
if (hit && start < 0)
|
|
242
|
-
start = c;
|
|
243
|
-
else if (!hit && start >= 0) {
|
|
244
|
-
runs.push([start, c - 1]);
|
|
245
|
-
start = -1;
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
return runs;
|
|
249
|
-
}
|
|
250
|
-
/**
|
|
251
|
-
* Where one color sits, row band by row band — the view a bounding box cannot
|
|
252
|
-
* give for a maze, a frame, or any other structure that is mostly holes.
|
|
253
|
-
*
|
|
254
|
-
* Keyed by **color rather than component** on purpose. A wall that a doorway
|
|
255
|
-
* splits into three components is one thing to a player, and three boxes describe
|
|
256
|
-
* it worse than one span list does; the flood fill's notion of connectedness is an
|
|
257
|
-
* implementation detail here, not something the model should have to reassemble.
|
|
258
|
-
*
|
|
259
|
-
* Rows whose runs are identical collapse into one band, the same trick
|
|
260
|
-
* {@link renderGrid} uses — and for the same reason, since ARC boards are banded.
|
|
261
|
-
* On a real 64×64 maze it turns 64 rows into ~20 bands.
|
|
262
|
-
*/
|
|
263
|
-
export function colorSpans(grid, color) {
|
|
264
|
-
const perRow = grid.map((row) => rowRuns(row, color));
|
|
265
|
-
const keys = perRow.map((runs) => runs.map(([a, b]) => `${a}:${b}`).join(" "));
|
|
266
|
-
const bands = [];
|
|
267
|
-
for (let r = 0; r < perRow.length;) {
|
|
268
|
-
let end = r;
|
|
269
|
-
while (end + 1 < perRow.length && keys[end + 1] === keys[r])
|
|
270
|
-
end++;
|
|
271
|
-
if (perRow[r].length > 0) {
|
|
272
|
-
bands.push({
|
|
273
|
-
top: r,
|
|
274
|
-
bottom: end,
|
|
275
|
-
cols: perRow[r].slice(0, MAX_SPAN_RUNS),
|
|
276
|
-
hidden: Math.max(0, perRow[r].length - MAX_SPAN_RUNS)
|
|
277
|
-
});
|
|
278
|
-
}
|
|
279
|
-
r = end + 1;
|
|
280
|
-
}
|
|
281
|
-
return bands;
|
|
282
|
-
}
|
|
283
|
-
/**
|
|
284
|
-
* Runs named per band, one line each. Both caps exist to bound the render on a
|
|
285
|
-
* board this encoding suits badly — a checkerboard is 32 runs in every one of 64
|
|
286
|
-
* rows — and both announce themselves, so a clipped view is never mistaken for
|
|
287
|
-
* the whole of one.
|
|
288
|
-
*/
|
|
289
|
-
export function renderSpans(bands) {
|
|
290
|
-
const lines = bands.slice(0, MAX_SPAN_BANDS).map((band) => {
|
|
291
|
-
const rows = band.top === band.bottom
|
|
292
|
-
? `row ${band.top}`
|
|
293
|
-
: `rows ${band.top}-${band.bottom}`;
|
|
294
|
-
const cols = band.cols
|
|
295
|
-
.map(([a, b]) => (a === b ? `${a}` : `${a}-${b}`))
|
|
296
|
-
.join(", ");
|
|
297
|
-
const more = band.hidden === 0 ? "" : `, +${band.hidden} more run(s)`;
|
|
298
|
-
return ` ${rows}: cols ${cols}${more}`;
|
|
299
|
-
});
|
|
300
|
-
if (bands.length > MAX_SPAN_BANDS) {
|
|
301
|
-
lines.push(` +${bands.length - MAX_SPAN_BANDS} more row band(s)`);
|
|
302
|
-
}
|
|
303
|
-
return lines.join("\n");
|
|
304
|
-
}
|
|
305
|
-
/** Column runs named per band before {@link renderSpans} stops. */
|
|
306
|
-
const MAX_SPAN_RUNS = 8;
|
|
307
|
-
/**
|
|
308
|
-
* Row bands named per color before {@link renderSpans} stops. Set from the real
|
|
309
|
-
* `ls20` board, whose floor needs 25: a cap that clips a terrain layer three
|
|
310
|
-
* quarters of the way down the board hides exactly the part a play reaches last.
|
|
311
|
-
*/
|
|
312
|
-
const MAX_SPAN_BANDS = 28;
|
|
313
|
-
/**
|
|
314
|
-
* A region this big whose box it fills less than {@link SPRAWL_MAX_FILL} of is
|
|
315
|
-
* described by spans instead. Both gates matter: fill alone would spend lines on
|
|
316
|
-
* every small hollow glyph (a 3×3 donut fills 8/9 of its box, a 6×6 outline just
|
|
317
|
-
* over half), and size alone would spend them on solid blocks that a box already
|
|
318
|
-
* describes exactly.
|
|
319
|
-
*/
|
|
320
|
-
const SPRAWL_MIN_CELLS = 64;
|
|
321
|
-
const SPRAWL_MAX_FILL = 0.7;
|
|
322
|
-
/** How many colors {@link renderShapes} will describe by spans. */
|
|
323
|
-
const SPRAWL_COLORS = 3;
|
|
324
|
-
/**
|
|
325
|
-
* Named regions with their positions, largest first — the locational view.
|
|
326
|
-
*
|
|
327
|
-
* A bounding box describes a region only when the region roughly fills it. That
|
|
328
|
-
* holds for the movable pieces and fails for exactly the two things a route
|
|
329
|
-
* depends on: on a real `ls20` board the maze boxes as `off-black: rows 0-63,
|
|
330
|
-
* cols 0-63 (2129 cells)` and the floor as `neutral: rows 5-54, cols 9-58 (1354
|
|
331
|
-
* cells)`, both a shade over half-filled. The first says nothing; the second is
|
|
332
|
-
* worse, asserting a 50×50 open arena of which 46% is wall — and a play reading
|
|
333
|
-
* its route off those boxes walks into walls, then spends a dozen turns
|
|
334
|
-
* rebuilding the maze through 11×11 `region` peepholes.
|
|
335
|
-
*
|
|
336
|
-
* So a region that sprawls is rendered as {@link colorSpans} instead — every row
|
|
337
|
-
* band and the columns it occupies, which is the same fact at a resolution that
|
|
338
|
-
* can be acted on. Sprawl is judged per color and the whole color is then spanned
|
|
339
|
-
* at once, so a wall and the doorway splitting it read as one terrain layer rather
|
|
340
|
-
* than as two boxes that overlap everything.
|
|
341
|
-
*
|
|
342
|
-
* `cap` bounds the boxed list; anything beyond it is reported as a count so the
|
|
343
|
-
* model knows the view is truncated rather than complete.
|
|
344
|
-
*/
|
|
345
|
-
export function renderShapes(grid, cap = 20) {
|
|
346
|
-
const comps = locateComponents(grid);
|
|
347
|
-
if (comps.length === 0)
|
|
348
|
-
return "no shapes (board is entirely white).";
|
|
349
|
-
// Judged on the largest component of each color, walking largest-first: a color
|
|
350
|
-
// whose biggest region is a sprawl is a terrain layer whatever its offcuts do.
|
|
351
|
-
const sprawling = new Set();
|
|
352
|
-
const judged = new Set();
|
|
353
|
-
for (const s of comps) {
|
|
354
|
-
if (judged.has(s.color))
|
|
355
|
-
continue;
|
|
356
|
-
judged.add(s.color);
|
|
357
|
-
if (sprawling.size >= SPRAWL_COLORS)
|
|
358
|
-
break;
|
|
359
|
-
if (s.size >= SPRAWL_MIN_CELLS && fillRatio(s, s.size) < SPRAWL_MAX_FILL) {
|
|
360
|
-
sprawling.add(s.color);
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
const lines = [];
|
|
364
|
-
const spanned = new Set();
|
|
365
|
-
let boxed = 0;
|
|
366
|
-
for (const s of comps) {
|
|
367
|
-
if (sprawling.has(s.color)) {
|
|
368
|
-
if (spanned.has(s.color))
|
|
369
|
-
continue;
|
|
370
|
-
spanned.add(s.color);
|
|
371
|
-
const parts = comps.filter((c) => c.color === s.color);
|
|
372
|
-
const cells = parts.reduce((n, c) => n + c.size, 0);
|
|
373
|
-
const box = unionBox(parts);
|
|
374
|
-
const pct = Math.round(fillRatio(box, cells) * 100);
|
|
375
|
-
lines.push(`${colorName(s.color)}: ${cells} cells in ${parts.length} region(s), ` +
|
|
376
|
-
`${pct}% of ${describeBox(box)} — too sparse for a box, so by row:`);
|
|
377
|
-
lines.push(renderSpans(colorSpans(grid, s.color)));
|
|
378
|
-
continue;
|
|
379
|
-
}
|
|
380
|
-
if (boxed >= cap)
|
|
381
|
-
continue;
|
|
382
|
-
boxed++;
|
|
383
|
-
lines.push(`${colorName(s.color)}: ${describeBox(s)} (${s.size} cell${s.size === 1 ? "" : "s"})`);
|
|
384
|
-
}
|
|
385
|
-
const rest = comps.filter((c) => !sprawling.has(c.color)).length - boxed;
|
|
386
|
-
if (rest > 0)
|
|
387
|
-
lines.push(`+${rest} more shape(s), smaller than these.`);
|
|
388
|
-
return lines.join("\n");
|
|
389
|
-
}
|
|
390
|
-
/**
|
|
391
|
-
* Above this many changed shapes, a frame is a repaint (a level change, a
|
|
392
|
-
* transform) rather than a board where things moved, and naming twenty shapes
|
|
393
|
-
* that all "appeared" says less than the cell diff does. {@link renderShapeDelta}
|
|
394
|
-
* declines, and its caller falls back.
|
|
395
|
-
*/
|
|
396
|
-
export const MAX_SHAPE_CHANGES = 10;
|
|
397
|
-
/** Manhattan distance between two boxes' top-left corners. */
|
|
398
|
-
const originDistance = (a, b) => Math.abs(a.top - b.top) + Math.abs(a.left - b.left);
|
|
399
|
-
/**
|
|
400
|
-
* Pair each `from` with its nearest unclaimed `to`, closest pair first — a greedy
|
|
401
|
-
* assignment, which is right here because the alternative (a full optimal
|
|
402
|
-
* matching) buys nothing: within one group every candidate is the same color and
|
|
403
|
-
* the same size, and one action moves things by a few cells, so the nearest
|
|
404
|
-
* candidate is the same object in all but contrived cases.
|
|
405
|
-
*/
|
|
406
|
-
function pairByProximity(from, to) {
|
|
407
|
-
const candidates = [];
|
|
408
|
-
for (const [a, fromShape] of from.entries()) {
|
|
409
|
-
for (const [b, toShape] of to.entries()) {
|
|
410
|
-
candidates.push({ a, b, d: originDistance(fromShape, toShape) });
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
candidates.sort((x, y) => x.d - y.d);
|
|
414
|
-
const claimedFrom = new Set();
|
|
415
|
-
const claimedTo = new Set();
|
|
416
|
-
const pairs = [];
|
|
417
|
-
for (const { a, b } of candidates) {
|
|
418
|
-
if (claimedFrom.has(a) || claimedTo.has(b))
|
|
419
|
-
continue;
|
|
420
|
-
claimedFrom.add(a);
|
|
421
|
-
claimedTo.add(b);
|
|
422
|
-
pairs.push([from[a], to[b]]);
|
|
423
|
-
}
|
|
424
|
-
return {
|
|
425
|
-
pairs,
|
|
426
|
-
unpairedFrom: from.filter((_, i) => !claimedFrom.has(i)),
|
|
427
|
-
unpairedTo: to.filter((_, i) => !claimedTo.has(i))
|
|
428
|
-
};
|
|
429
|
-
}
|
|
430
|
-
/** Group shapes under a key, preserving each group's input order. */
|
|
431
|
-
function groupShapes(shapes, key) {
|
|
432
|
-
const groups = new Map();
|
|
433
|
-
for (const shape of shapes) {
|
|
434
|
-
const k = key(shape);
|
|
435
|
-
const group = groups.get(k);
|
|
436
|
-
if (group)
|
|
437
|
-
group.push(shape);
|
|
438
|
-
else
|
|
439
|
-
groups.set(k, [shape]);
|
|
440
|
-
}
|
|
441
|
-
return groups;
|
|
442
|
-
}
|
|
443
|
-
/**
|
|
444
|
-
* Match the shapes of two frames: what moved, what changed size, what came and
|
|
445
|
-
* went. Null when there is no earlier frame to compare against.
|
|
446
|
-
*
|
|
447
|
-
* Three passes, and the order is the whole design. Color **and** cell count
|
|
448
|
-
* first, because a rigid object that travelled keeps both — that pass is what
|
|
449
|
-
* finds a move, and its pairs are never reconsidered. Color alone second, over
|
|
450
|
-
* what is left, which is what a bar losing cells or a trail growing looks like.
|
|
451
|
-
* **Footprint** alone last, over what neither pass claimed, which is a shape that
|
|
452
|
-
* changed color without moving — see {@link matchRepaints}.
|
|
453
|
-
*/
|
|
454
|
-
export function diffShapes(before, after) {
|
|
455
|
-
if (before === null)
|
|
456
|
-
return null;
|
|
457
|
-
return matchShapes(locateComponents(before), locateComponents(after));
|
|
458
|
-
}
|
|
459
|
-
/** {@link diffShapes} over already-located components. Exported for tests. */
|
|
460
|
-
export function matchShapes(before, after) {
|
|
461
|
-
const moved = [];
|
|
462
|
-
const other = [];
|
|
463
|
-
const leftFrom = [];
|
|
464
|
-
const leftTo = [];
|
|
465
|
-
const rigid = (s) => `${s.color}:${s.size}`;
|
|
466
|
-
const beforeGroups = groupShapes(before, rigid);
|
|
467
|
-
const afterGroups = groupShapes(after, rigid);
|
|
468
|
-
for (const key of new Set([...beforeGroups.keys(), ...afterGroups.keys()])) {
|
|
469
|
-
const result = pairByProximity(beforeGroups.get(key) ?? [], afterGroups.get(key) ?? []);
|
|
470
|
-
for (const [from, to] of result.pairs) {
|
|
471
|
-
// Same color, same size, same corner: either it did not move, or it turned
|
|
472
|
-
// in place. Both are silence here — a turn shows up in the cell diff, which
|
|
473
|
-
// is what the caller falls back to when this delta explains nothing.
|
|
474
|
-
if (from.top === to.top && from.left === to.left)
|
|
475
|
-
continue;
|
|
476
|
-
moved.push({
|
|
477
|
-
kind: "moved",
|
|
478
|
-
from,
|
|
479
|
-
to,
|
|
480
|
-
dRow: to.top - from.top,
|
|
481
|
-
dCol: to.left - from.left
|
|
482
|
-
});
|
|
483
|
-
}
|
|
484
|
-
leftFrom.push(...result.unpairedFrom);
|
|
485
|
-
leftTo.push(...result.unpairedTo);
|
|
486
|
-
}
|
|
487
|
-
const byColor = (s) => String(s.color);
|
|
488
|
-
const colorBefore = groupShapes(leftFrom, byColor);
|
|
489
|
-
const colorAfter = groupShapes(leftTo, byColor);
|
|
490
|
-
const gone = [];
|
|
491
|
-
const appeared = [];
|
|
492
|
-
for (const key of new Set([...colorBefore.keys(), ...colorAfter.keys()])) {
|
|
493
|
-
const result = pairByProximity(colorBefore.get(key) ?? [], colorAfter.get(key) ?? []);
|
|
494
|
-
for (const [from, to] of result.pairs) {
|
|
495
|
-
other.push({ kind: "resized", from, to });
|
|
496
|
-
}
|
|
497
|
-
gone.push(...result.unpairedFrom);
|
|
498
|
-
appeared.push(...result.unpairedTo);
|
|
499
|
-
}
|
|
500
|
-
other.push(...matchRepaints(gone, appeared));
|
|
501
|
-
moved.sort((x, y) => y.from.size - x.from.size);
|
|
502
|
-
other.sort((x, y) => shapeOf(y).size - shapeOf(x).size);
|
|
503
|
-
return { moved, other };
|
|
504
|
-
}
|
|
505
|
-
/**
|
|
506
|
-
* Pair off the shapes neither earlier pass claimed, by **footprint**: one that
|
|
507
|
-
* vanished and one that arrived on the same box with the same cell count are one
|
|
508
|
-
* shape repainted, not two events.
|
|
509
|
-
*
|
|
510
|
-
* A click game is the case this exists for. Nothing on such a board ever travels,
|
|
511
|
-
* so both earlier passes — which pair within a color — leave every changed block
|
|
512
|
-
* as a `gone` and an `appeared` at the same coordinates: `blue 6×6 at rows 36-41,
|
|
513
|
-
* cols 36-41 is gone; red 6×6 appeared at rows 36-41, cols 36-41`, twenty times
|
|
514
|
-
* over, with the model left to work out for itself that a click toggles a colour.
|
|
515
|
-
*
|
|
516
|
-
* Box and size together are strong evidence and not proof — two different shapes
|
|
517
|
-
* can share a bounding box and a cell count without sharing a cell — but the
|
|
518
|
-
* claim made from them is only that those cells changed color, which is true
|
|
519
|
-
* either way. Colors necessarily differ: same-color leftovers were already paired
|
|
520
|
-
* by {@link pairByProximity}, which leaves one side of each color empty.
|
|
521
|
-
*/
|
|
522
|
-
function matchRepaints(gone, appeared) {
|
|
523
|
-
const footprint = (s) => `${s.top},${s.left},${s.bottom},${s.right}:${s.size}`;
|
|
524
|
-
const unclaimed = groupShapes(gone, footprint);
|
|
525
|
-
const changes = [];
|
|
526
|
-
for (const shape of appeared) {
|
|
527
|
-
const from = unclaimed.get(footprint(shape))?.shift();
|
|
528
|
-
changes.push(from === undefined
|
|
529
|
-
? { kind: "appeared", shape }
|
|
530
|
-
: { kind: "recolored", from, to: shape });
|
|
531
|
-
}
|
|
532
|
-
for (const leftover of unclaimed.values()) {
|
|
533
|
-
for (const shape of leftover)
|
|
534
|
-
changes.push({ kind: "gone", shape });
|
|
535
|
-
}
|
|
536
|
-
return changes;
|
|
537
|
-
}
|
|
538
|
-
/** The component a change is about, whichever field carries it. */
|
|
539
|
-
function shapeOf(change) {
|
|
540
|
-
return change.kind === "appeared" || change.kind === "gone"
|
|
541
|
-
? change.shape
|
|
542
|
-
: change.from;
|
|
543
|
-
}
|
|
544
|
-
/** `orange 2×5` — a shape's color and how much board it spans. */
|
|
545
|
-
function shapeLabel(s) {
|
|
546
|
-
return `${colorName(s.color)} ${s.bottom - s.top + 1}×${s.right - s.left + 1}`;
|
|
547
|
-
}
|
|
548
|
-
/** `down 5, left 10` — a displacement in board directions, zero terms dropped. */
|
|
549
|
-
export function describeShift(dRow, dCol) {
|
|
550
|
-
const parts = [];
|
|
551
|
-
if (dRow !== 0)
|
|
552
|
-
parts.push(`${dRow > 0 ? "down" : "up"} ${Math.abs(dRow)}`);
|
|
553
|
-
if (dCol !== 0)
|
|
554
|
-
parts.push(`${dCol > 0 ? "right" : "left"} ${Math.abs(dCol)}`);
|
|
555
|
-
return parts.length === 0 ? "no shift" : parts.join(", ");
|
|
556
|
-
}
|
|
557
|
-
/** Destination of a move, spelling out only the axis that changed. */
|
|
558
|
-
function describeDestination(change) {
|
|
559
|
-
if (change.dCol === 0)
|
|
560
|
-
return `rows ${change.to.top}-${change.to.bottom}`;
|
|
561
|
-
if (change.dRow === 0)
|
|
562
|
-
return `cols ${change.to.left}-${change.to.right}`;
|
|
563
|
-
return describeBox(change.to);
|
|
564
|
-
}
|
|
565
|
-
/**
|
|
566
|
-
* One action's effect as movement: `nothing moved` when the board's objects
|
|
567
|
-
* stayed put, whatever a counter did alongside them.
|
|
568
|
-
*
|
|
569
|
-
* That phrase is spent carefully, because the soul teaches it as the signal for
|
|
570
|
-
* *blocked or refused*: it leads a step only when everything else the delta holds
|
|
571
|
-
* is a {@link ShapeChange} of kind `resized`, which is a counter or a bar doing
|
|
572
|
-
* its own bookkeeping. A step that repainted a block, or made one appear, changed
|
|
573
|
-
* the board — no object travelled, and reporting that as `nothing moved` tells a
|
|
574
|
-
* click game that twenty clicks which each toggled a 36-cell block were refused.
|
|
575
|
-
*
|
|
576
|
-
* Null means *this view has nothing to say* — either no shape can be paired
|
|
577
|
-
* across the frames, or so many changed that they are a repaint
|
|
578
|
-
* ({@link MAX_SHAPE_CHANGES}). Both leave the cell diff as the better witness, so
|
|
579
|
-
* the caller renders that instead rather than printing a confident "nothing
|
|
580
|
-
* moved" over a board that changed completely.
|
|
581
|
-
*/
|
|
582
|
-
export function renderShapeDelta(delta, cap = 6) {
|
|
583
|
-
const total = delta.moved.length + delta.other.length;
|
|
584
|
-
if (total === 0 || total > MAX_SHAPE_CHANGES)
|
|
585
|
-
return null;
|
|
586
|
-
const lines = [];
|
|
587
|
-
for (const change of delta.moved.slice(0, cap)) {
|
|
588
|
-
lines.push(`${shapeLabel(change.from)} ${describeBox(change.from)} → ` +
|
|
589
|
-
`${describeDestination(change)} (${describeShift(change.dRow, change.dCol)})`);
|
|
590
|
-
}
|
|
591
|
-
if (delta.moved.length === 0) {
|
|
592
|
-
lines.push(delta.other.every((change) => change.kind === "resized")
|
|
593
|
-
? "nothing moved"
|
|
594
|
-
: "no shape travelled, but the board changed");
|
|
595
|
-
}
|
|
596
|
-
const room = Math.max(0, cap - lines.length);
|
|
597
|
-
for (const change of delta.other.slice(0, room)) {
|
|
598
|
-
switch (change.kind) {
|
|
599
|
-
case "resized":
|
|
600
|
-
lines.push(`${colorName(change.from.color)} ${describeBox(change.from)} → ` +
|
|
601
|
-
`${describeBox(change.to)} (${change.from.size}→${change.to.size} cells)`);
|
|
602
|
-
break;
|
|
603
|
-
case "recolored":
|
|
604
|
-
lines.push(`${shapeLabel(change.from)} at ${describeBox(change.from)} ` +
|
|
605
|
-
`turned ${colorName(change.to.color)}`);
|
|
606
|
-
break;
|
|
607
|
-
case "appeared":
|
|
608
|
-
lines.push(`${shapeLabel(change.shape)} appeared at ${describeBox(change.shape)}`);
|
|
609
|
-
break;
|
|
610
|
-
case "gone":
|
|
611
|
-
lines.push(`${shapeLabel(change.shape)} at ${describeBox(change.shape)} is gone`);
|
|
612
|
-
break;
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
const shown = Math.min(delta.moved.length, cap) + Math.min(delta.other.length, room);
|
|
616
|
-
if (total > shown)
|
|
617
|
-
lines.push(`+${total - shown} more change(s)`);
|
|
618
|
-
return lines.join("; ");
|
|
619
|
-
}
|
|
620
|
-
/**
|
|
621
|
-
* A shape's identity within a batch: what it is, and where it sits right now.
|
|
622
|
-
* The box is passed apart from the color and cell count because a move's
|
|
623
|
-
* destination carries only a box — same color, same size, by construction.
|
|
624
|
-
*/
|
|
625
|
-
const travelKey = (shape, at) => `${shape.color}:${shape.size}:${at.top},${at.left}`;
|
|
626
|
-
/**
|
|
627
|
-
* Fold one step's moves into a batch's running totals.
|
|
628
|
-
*
|
|
629
|
-
* Identity is *position*, not appearance. Boards carrying two identical shapes
|
|
630
|
-
* are the ordinary case, not the contrived one — a pair of blocks, a player and
|
|
631
|
-
* its twin — and summing them under one color-and-size key reports two shapes
|
|
632
|
-
* that passed each other as one shape that never moved, which is the single
|
|
633
|
-
* worst thing this line could say.
|
|
634
|
-
*
|
|
635
|
-
* Each entry is re-keyed to where its shape landed, which is exactly where the
|
|
636
|
-
* next step's diff will find it: the frames chain, step N's board being step
|
|
637
|
-
* N+1's starting board, so a position is a stable handle for as long as the
|
|
638
|
-
* shape keeps its color and size. Sources are cleared before any destination is
|
|
639
|
-
* written, so a shape sliding into the cell another just vacated inherits its
|
|
640
|
-
* own history rather than the vacater's.
|
|
641
|
-
*/
|
|
642
|
-
export function trackTravel(travels, moved) {
|
|
643
|
-
const started = moved.map((move) => {
|
|
644
|
-
const key = travelKey(move.from, move.from);
|
|
645
|
-
const travel = travels.get(key) ?? {
|
|
646
|
-
shape: move.from,
|
|
647
|
-
dRow: 0,
|
|
648
|
-
dCol: 0,
|
|
649
|
-
moves: 0
|
|
650
|
-
};
|
|
651
|
-
travels.delete(key);
|
|
652
|
-
return { move, travel };
|
|
653
|
-
});
|
|
654
|
-
for (const { move, travel } of started) {
|
|
655
|
-
travel.dRow += move.dRow;
|
|
656
|
-
travel.dCol += move.dCol;
|
|
657
|
-
travel.moves++;
|
|
658
|
-
travels.set(travelKey(move.from, move.to), travel);
|
|
659
|
-
}
|
|
660
|
-
}
|
|
661
|
-
/**
|
|
662
|
-
* A shape's net travel over a batch, counted in **moves** rather than cells.
|
|
663
|
-
*
|
|
664
|
-
* Cells are the misleading unit for a sequence: two presses that each slide a
|
|
665
|
-
* selector five cells are not "ten cells down", they are two moves of five, and a
|
|
666
|
-
* model reading the former will place itself ten cells from where it is. So the
|
|
667
|
-
* per-move stride is stated whenever the total divides by the moves that produced
|
|
668
|
-
* it — observed from the steps, never assumed.
|
|
669
|
-
*/
|
|
670
|
-
export function renderTravel(travel) {
|
|
671
|
-
const label = shapeLabel(travel.shape);
|
|
672
|
-
if (travel.dRow === 0 && travel.dCol === 0) {
|
|
673
|
-
return `${label} is back where it started after ${travel.moves} move(s)`;
|
|
674
|
-
}
|
|
675
|
-
const shift = describeShift(travel.dRow, travel.dCol);
|
|
676
|
-
if (travel.moves <= 1)
|
|
677
|
-
return `${label} ${shift}`;
|
|
678
|
-
const even = travel.dRow % travel.moves === 0 && travel.dCol % travel.moves === 0;
|
|
679
|
-
const stride = even
|
|
680
|
-
? `, ${describeShift(travel.dRow / travel.moves, travel.dCol / travel.moves)} per move`
|
|
681
|
-
: "";
|
|
682
|
-
return `${label} ${shift} over ${travel.moves} moves${stride}`;
|
|
683
|
-
}
|
|
684
|
-
/** `0=white 9=blue b=yellow` — maps grid characters to names, present colors only. */
|
|
685
|
-
export function renderLegend(grid) {
|
|
686
|
-
const present = colorHistogram(grid).sort((a, b) => a.color - b.color);
|
|
687
|
-
return ("colors: " +
|
|
688
|
-
present.map((h) => `${colorChar(h.color)}=${colorName(h.color)}`).join(" "));
|
|
689
|
-
}
|
|
690
|
-
/**
|
|
691
|
-
* A column ruler for a labeled grid: a line labelling every `step`-th column and
|
|
692
|
-
* a tick line beneath it, both indented past the row-label gutter.
|
|
693
|
-
*
|
|
694
|
-
* Labels are **absolute** column numbers — `firstCol` is where the drawn area
|
|
695
|
-
* starts, so a clipped window rules to the same coordinates the rest of the
|
|
696
|
-
* output uses. `step` narrows for a window, where every-tenth might not land at
|
|
697
|
-
* all.
|
|
698
|
-
*/
|
|
699
|
-
function columnRuler(cols, gutter, firstCol = 0, step = 10) {
|
|
700
|
-
let marks = "";
|
|
701
|
-
let ticks = "";
|
|
702
|
-
for (let c = 0; c < cols; c++) {
|
|
703
|
-
const label = String(firstCol + c);
|
|
704
|
-
// A label that would run off the end is dropped rather than widening the
|
|
705
|
-
// ruler past the body it rules.
|
|
706
|
-
if ((firstCol + c) % step === 0 && c + label.length <= cols) {
|
|
707
|
-
marks += label;
|
|
708
|
-
ticks += "|";
|
|
709
|
-
// The multi-char label has already consumed the next columns' slots.
|
|
710
|
-
c += label.length - 1;
|
|
711
|
-
for (let i = 1; i < label.length; i++)
|
|
712
|
-
ticks += " ";
|
|
713
|
-
}
|
|
714
|
-
else {
|
|
715
|
-
marks += " ";
|
|
716
|
-
ticks += " ";
|
|
717
|
-
}
|
|
718
|
-
}
|
|
719
|
-
// A window too narrow to contain a single mark gets no ruler at all, rather
|
|
720
|
-
// than two blank lines the caller would have to recognize as empty.
|
|
721
|
-
if (marks.trim() === "")
|
|
722
|
-
return [];
|
|
723
|
-
const pad = " ".repeat(gutter);
|
|
724
|
-
return [(pad + marks).trimEnd(), (pad + ticks).trimEnd()];
|
|
725
|
-
}
|
|
726
|
-
/** Ruler spacing for {@link renderRegion}, whose windows are at most 41 wide. */
|
|
727
|
-
const REGION_RULER_STEP = 5;
|
|
728
|
-
/**
|
|
729
|
-
* The full board: a legend, a column ruler, then one labeled line per row —
|
|
730
|
-
* with **runs of identical rows collapsed** into a single `12-31 |` band.
|
|
731
|
-
*
|
|
732
|
-
* Labels exist because a model cannot reliably count to column 37 across 64
|
|
733
|
-
* undelimited characters; miscounting there costs a wrong click and two more
|
|
734
|
-
* turns. Collapsing exploits how banded ARC boards are without run-length
|
|
735
|
-
* encoding *within* a row, which would destroy the 2-D structure that this view
|
|
736
|
-
* exists to convey.
|
|
737
|
-
*/
|
|
738
|
-
export function renderGrid(grid) {
|
|
739
|
-
if (grid.length === 0)
|
|
740
|
-
return "empty grid.";
|
|
741
|
-
const rendered = grid.map((row) => row.map(colorChar).join(""));
|
|
742
|
-
const cols = grid[0].length;
|
|
743
|
-
// Gutter fits the widest row label, e.g. "62-63 | ".
|
|
744
|
-
const widest = String(grid.length - 1).length;
|
|
745
|
-
const gutter = widest * 2 + 1 + 3;
|
|
746
|
-
const lines = [renderLegend(grid), ...columnRuler(cols, gutter)];
|
|
747
|
-
for (let r = 0; r < rendered.length;) {
|
|
748
|
-
let end = r;
|
|
749
|
-
while (end + 1 < rendered.length && rendered[end + 1] === rendered[r])
|
|
750
|
-
end++;
|
|
751
|
-
const label = end === r ? String(r) : `${r}-${end}`;
|
|
752
|
-
lines.push(`${label.padStart(gutter - 3)} | ${rendered[r]}`);
|
|
753
|
-
r = end + 1;
|
|
754
|
-
}
|
|
755
|
-
return lines.join("\n");
|
|
756
|
-
}
|
|
757
|
-
/**
|
|
758
|
-
* A localized view: a `radius`-cell square around (centerRow, centerCol), with
|
|
759
|
-
* **absolute** row and column labels. The labels are what make the view usable —
|
|
760
|
-
* an unlabeled block cannot be mapped back to a coordinate to click.
|
|
761
|
-
*
|
|
762
|
-
* The window is **clipped** on every side, never padded, which is what makes the
|
|
763
|
-
* header's `cols` anchor true: character `i` of a body line is column
|
|
764
|
-
* `firstCol + i`. Padding off-grid columns with spaces instead would shift that
|
|
765
|
-
* mapping by up to `radius` near the left edge.
|
|
766
|
-
*
|
|
767
|
-
* **The window is ruled, not just headed.** A model asked to place a cell in an
|
|
768
|
-
* 11-wide window counts the characters by hand and gets them wrong, then reasons
|
|
769
|
-
* on the columns it miscounted — so narrow is exactly where the ruler is needed.
|
|
770
|
-
* It steps at {@link REGION_RULER_STEP} rather than {@link renderGrid}'s tens,
|
|
771
|
-
* which a window this size can miss entirely.
|
|
772
|
-
*/
|
|
773
|
-
export function renderRegion(grid, centerRow, centerCol, radius = 5) {
|
|
774
|
-
if (grid.length === 0)
|
|
775
|
-
return "empty grid.";
|
|
776
|
-
const firstRow = Math.max(0, centerRow - radius);
|
|
777
|
-
const lastRow = Math.min(grid.length - 1, centerRow + radius);
|
|
778
|
-
const firstCol = Math.max(0, centerCol - radius);
|
|
779
|
-
const gutter = String(lastRow).length;
|
|
780
|
-
const window = [];
|
|
781
|
-
const body = [];
|
|
782
|
-
// Widest extent actually drawn, so the header says where the window ends as
|
|
783
|
-
// well as where it starts. Each row is clipped on its own in case a frame
|
|
784
|
-
// arrives ragged: that shortens a line, it never shifts one.
|
|
785
|
-
let lastCol = firstCol;
|
|
786
|
-
for (let r = firstRow; r <= lastRow; r++) {
|
|
787
|
-
const end = Math.min(grid[r].length - 1, centerCol + radius);
|
|
788
|
-
const cells = grid[r].slice(firstCol, end + 1);
|
|
789
|
-
window.push(cells);
|
|
790
|
-
body.push(`${String(r).padStart(gutter)} | ${cells.map(colorChar).join("")}`);
|
|
791
|
-
lastCol = Math.max(lastCol, end);
|
|
792
|
-
}
|
|
793
|
-
return [
|
|
794
|
-
renderLegend(window),
|
|
795
|
-
`rows ${firstRow}-${lastRow}, cols ${firstCol}-${lastCol} ` +
|
|
796
|
-
`(centered on row ${centerRow}, col ${centerCol})`,
|
|
797
|
-
// `gutter + 3` is the body's `<label> | ` prefix, so the ruler sits over the
|
|
798
|
-
// cells rather than over the labels.
|
|
799
|
-
...columnRuler(lastCol - firstCol + 1, gutter + 3, firstCol, REGION_RULER_STEP),
|
|
800
|
-
...body
|
|
801
|
-
].join("\n");
|
|
802
|
-
}
|