@altnovatrail/picture-kit 0.0.0-stage → 0.1.1

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.
Files changed (122) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +7 -0
  3. package/README.md +28 -2
  4. package/lib/emoji.d.ts +16 -0
  5. package/lib/emoji.js +64 -0
  6. package/lib/index.d.ts +9 -0
  7. package/lib/index.js +10 -0
  8. package/lib/kind.d.ts +22 -0
  9. package/lib/kind.js +2 -0
  10. package/lib/kinds/balance.d.ts +34 -0
  11. package/lib/kinds/balance.js +217 -0
  12. package/lib/kinds/branches.d.ts +33 -0
  13. package/lib/kinds/branches.js +473 -0
  14. package/lib/kinds/calendar.d.ts +55 -0
  15. package/lib/kinds/calendar.js +532 -0
  16. package/lib/kinds/chart.d.ts +48 -0
  17. package/lib/kinds/chart.js +645 -0
  18. package/lib/kinds/choices.d.ts +14 -0
  19. package/lib/kinds/choices.js +53 -0
  20. package/lib/kinds/clock.d.ts +36 -0
  21. package/lib/kinds/clock.js +219 -0
  22. package/lib/kinds/coins.d.ts +37 -0
  23. package/lib/kinds/coins.js +550 -0
  24. package/lib/kinds/column.d.ts +37 -0
  25. package/lib/kinds/column.js +410 -0
  26. package/lib/kinds/commands-parts.d.ts +12 -0
  27. package/lib/kinds/commands-parts.js +24 -0
  28. package/lib/kinds/commands.d.ts +66 -0
  29. package/lib/kinds/commands.js +789 -0
  30. package/lib/kinds/containers.d.ts +52 -0
  31. package/lib/kinds/containers.js +758 -0
  32. package/lib/kinds/countfig.d.ts +41 -0
  33. package/lib/kinds/countfig.js +372 -0
  34. package/lib/kinds/cubes.d.ts +70 -0
  35. package/lib/kinds/cubes.js +371 -0
  36. package/lib/kinds/die.d.ts +32 -0
  37. package/lib/kinds/die.js +301 -0
  38. package/lib/kinds/domino.d.ts +37 -0
  39. package/lib/kinds/domino.js +450 -0
  40. package/lib/kinds/dotgrid.d.ts +38 -0
  41. package/lib/kinds/dotgrid.js +411 -0
  42. package/lib/kinds/figure.d.ts +62 -0
  43. package/lib/kinds/figure.js +687 -0
  44. package/lib/kinds/fold-parts.d.ts +73 -0
  45. package/lib/kinds/fold-parts.js +315 -0
  46. package/lib/kinds/fold.d.ts +52 -0
  47. package/lib/kinds/fold.js +650 -0
  48. package/lib/kinds/fraction.d.ts +50 -0
  49. package/lib/kinds/fraction.js +568 -0
  50. package/lib/kinds/gauges.d.ts +53 -0
  51. package/lib/kinds/gauges.js +880 -0
  52. package/lib/kinds/gears.d.ts +40 -0
  53. package/lib/kinds/gears.js +787 -0
  54. package/lib/kinds/grid.d.ts +45 -0
  55. package/lib/kinds/grid.js +591 -0
  56. package/lib/kinds/groups.d.ts +23 -0
  57. package/lib/kinds/groups.js +335 -0
  58. package/lib/kinds/index.d.ts +3 -0
  59. package/lib/kinds/index.js +87 -0
  60. package/lib/kinds/lanes.d.ts +36 -0
  61. package/lib/kinds/lanes.js +220 -0
  62. package/lib/kinds/layers.d.ts +31 -0
  63. package/lib/kinds/layers.js +419 -0
  64. package/lib/kinds/line.d.ts +38 -0
  65. package/lib/kinds/line.js +739 -0
  66. package/lib/kinds/magic-parts.d.ts +72 -0
  67. package/lib/kinds/magic-parts.js +186 -0
  68. package/lib/kinds/magic.d.ts +66 -0
  69. package/lib/kinds/magic.js +664 -0
  70. package/lib/kinds/map.d.ts +32 -0
  71. package/lib/kinds/map.js +370 -0
  72. package/lib/kinds/matchsticks.d.ts +26 -0
  73. package/lib/kinds/matchsticks.js +434 -0
  74. package/lib/kinds/necklace.d.ts +27 -0
  75. package/lib/kinds/necklace.js +342 -0
  76. package/lib/kinds/net.d.ts +47 -0
  77. package/lib/kinds/net.js +241 -0
  78. package/lib/kinds/overlay.d.ts +50 -0
  79. package/lib/kinds/overlay.js +519 -0
  80. package/lib/kinds/pieces.d.ts +41 -0
  81. package/lib/kinds/pieces.js +420 -0
  82. package/lib/kinds/pipes.d.ts +46 -0
  83. package/lib/kinds/pipes.js +657 -0
  84. package/lib/kinds/plane-parts.d.ts +105 -0
  85. package/lib/kinds/plane-parts.js +372 -0
  86. package/lib/kinds/plane.d.ts +35 -0
  87. package/lib/kinds/plane.js +755 -0
  88. package/lib/kinds/pyramid.d.ts +26 -0
  89. package/lib/kinds/pyramid.js +228 -0
  90. package/lib/kinds/ropes.d.ts +30 -0
  91. package/lib/kinds/ropes.js +571 -0
  92. package/lib/kinds/shapes.d.ts +59 -0
  93. package/lib/kinds/shapes.js +490 -0
  94. package/lib/kinds/solids.d.ts +78 -0
  95. package/lib/kinds/solids.js +1058 -0
  96. package/lib/kinds/tangram.d.ts +52 -0
  97. package/lib/kinds/tangram.js +713 -0
  98. package/lib/kinds/target.d.ts +39 -0
  99. package/lib/kinds/target.js +761 -0
  100. package/lib/kinds/tiles.d.ts +55 -0
  101. package/lib/kinds/tiles.js +606 -0
  102. package/lib/kinds/venn.d.ts +38 -0
  103. package/lib/kinds/venn.js +710 -0
  104. package/lib/noto.d.ts +2 -0
  105. package/lib/noto.js +212 -0
  106. package/lib/objects.d.ts +7 -0
  107. package/lib/objects.js +226 -0
  108. package/lib/pen.d.ts +32 -0
  109. package/lib/pen.js +60 -0
  110. package/lib/read.d.ts +9 -0
  111. package/lib/read.js +49 -0
  112. package/lib/render.d.ts +24 -0
  113. package/lib/render.js +68 -0
  114. package/lib/solve/balance.d.ts +46 -0
  115. package/lib/solve/balance.js +118 -0
  116. package/lib/style.d.ts +70 -0
  117. package/lib/style.js +39 -0
  118. package/lib/svg.d.ts +22 -0
  119. package/lib/svg.js +48 -0
  120. package/lib/things.d.ts +40 -0
  121. package/lib/things.js +269 -0
  122. package/package.json +28 -4
@@ -0,0 +1,45 @@
1
+ import type { Kind } from "../kind.ts";
2
+ import { type ColorName } from "../style.ts";
3
+ import { type Thing } from "../things.ts";
4
+ /**
5
+ * A grid of square cells, up to 10 by 10, given row by row from the top: a
6
+ * cell is a text ("" for an empty one), null for a cell left out of the
7
+ * board, or an object with a fill of the palette and one of a text, a thing
8
+ * or ask. Walls are thick lines on the sides of cells, every side with
9
+ * "all", and doors are gaps in them; a path runs over the cells, as a line
10
+ * through their centres or as jumps from cell to cell with labels. A place
11
+ * is [row, column], counted from 1 at the top left, so cells[0][0] is [1, 1].
12
+ */
13
+ export type At = [number, number];
14
+ export type CellObj = {
15
+ fill?: ColorName;
16
+ text?: string;
17
+ thing?: Thing;
18
+ ask?: true;
19
+ };
20
+ export type Cell = string | null | CellObj;
21
+ export declare const sides: readonly ["top", "right", "bottom", "left"];
22
+ export type Side = (typeof sides)[number];
23
+ export type Edge = {
24
+ cell: At;
25
+ side: Side;
26
+ };
27
+ export declare const pathStyles: readonly ["line", "jumps"];
28
+ export type GridPath = {
29
+ through: At[];
30
+ style?: (typeof pathStyles)[number];
31
+ labels?: string[];
32
+ color?: ColorName;
33
+ };
34
+ export type GridScene = {
35
+ kind: "grid";
36
+ cells: Cell[][];
37
+ walls?: "all" | Edge[];
38
+ doors?: Edge[];
39
+ path?: GridPath;
40
+ rowLabels?: string[];
41
+ colLabels?: string[];
42
+ };
43
+ /** edgeKey names the line a side of a cell lies on: h<line>,<column> or v<row>,<line>. */
44
+ export declare function edgeKey(at: At, side: Side): string;
45
+ export declare const grid: Kind;
@@ -0,0 +1,591 @@
1
+ import { margin } from "../kind.js";
2
+ import { textWidth } from "../pen.js";
3
+ import { int, isObj, list, oneOf, only, text } from "../read.js";
4
+ import { colorNames, colors, font, ground, ink, isColor, outline, textOn, } from "../style.js";
5
+ import { esc, n, poly } from "../svg.js";
6
+ import { checkThing, drawThing, wordOf } from "../things.js";
7
+ export const sides = ["top", "right", "bottom", "left"];
8
+ export const pathStyles = ["line", "jumps"];
9
+ const most = 10;
10
+ const wallWidth = 7;
11
+ /** edgeKey names the line a side of a cell lies on: h<line>,<column> or v<row>,<line>. */
12
+ export function edgeKey(at, side) {
13
+ const r = at[0] - 1;
14
+ const c = at[1] - 1;
15
+ if (side === "top")
16
+ return `h${r},${c}`;
17
+ if (side === "bottom")
18
+ return `h${r + 1},${c}`;
19
+ if (side === "left")
20
+ return `v${r},${c}`;
21
+ return `v${r},${c + 1}`;
22
+ }
23
+ function inBoard(cells, r, c) {
24
+ return (r >= 0 && r < cells.length && c >= 0 && c < (cells[0]?.length ?? 0) && cells[r][c] !== null);
25
+ }
26
+ /** linesOf lists every line of the board once, with the cells on its two sides. */
27
+ function linesOf(cells, s) {
28
+ const out = new Map();
29
+ const at = (r, c) => inBoard(cells, r, c) ? [r + 1, c + 1] : undefined;
30
+ cells.forEach((row, r) => {
31
+ row.forEach((cell, c) => {
32
+ if (cell === null)
33
+ return;
34
+ for (const side of sides) {
35
+ const key = edgeKey([r + 1, c + 1], side);
36
+ if (out.has(key))
37
+ continue;
38
+ const h = key[0] === "h";
39
+ const [i, j] = key.slice(1).split(",").map(Number);
40
+ out.set(key, {
41
+ key,
42
+ a: { x: j * s, y: i * s },
43
+ b: h ? { x: (j + 1) * s, y: i * s } : { x: j * s, y: (i + 1) * s },
44
+ cells: h ? [at(i - 1, j), at(i, j)] : [at(i, j - 1), at(i, j)],
45
+ });
46
+ }
47
+ });
48
+ });
49
+ return out;
50
+ }
51
+ function cellObj(cell) {
52
+ if (cell === null)
53
+ return {};
54
+ if (typeof cell === "string")
55
+ return cell === "" ? {} : { text: cell };
56
+ return cell;
57
+ }
58
+ function checkCell(cell, path, problems) {
59
+ if (cell === null)
60
+ return;
61
+ if (typeof cell === "string") {
62
+ if ([...cell].length > 3)
63
+ problems.push(`${path}: at most 3 characters`);
64
+ if (cell === "?")
65
+ problems.push(`${path}: write {"ask": true} for the place the question asks`);
66
+ return;
67
+ }
68
+ if (!isObj(cell)) {
69
+ problems.push(`${path}: must be a text, null for a cell left out, or a cell object`);
70
+ return;
71
+ }
72
+ only(cell, ["fill", "text", "thing", "ask"], path, problems);
73
+ if (cell.fill !== undefined && !isColor(cell.fill))
74
+ problems.push(`${path}.fill: must be a colour: ${colorNames.join(", ")}`);
75
+ const contents = ["text", "thing", "ask"].filter((m) => cell[m] !== undefined);
76
+ if (contents.length > 1)
77
+ problems.push(`${path}: holds one of text, thing and ask, not ${contents.join(" and ")}`);
78
+ if (cell.text !== undefined) {
79
+ text(cell.text, `${path}.text`, problems, 3);
80
+ if (cell.text === "?")
81
+ problems.push(`${path}.text: write "ask": true for the place the question asks`);
82
+ }
83
+ if (cell.thing !== undefined)
84
+ checkThing(cell.thing, `${path}.thing`, problems);
85
+ if (cell.ask !== undefined && cell.ask !== true)
86
+ problems.push(`${path}.ask: must be true`);
87
+ }
88
+ function checkAt(value, path, problems, cells) {
89
+ if (!Array.isArray(value) || value.length !== 2) {
90
+ problems.push(`${path}: must be [row, column], counted from 1 at the top left`);
91
+ return undefined;
92
+ }
93
+ const before = problems.length;
94
+ const r = int(value[0], `${path}[0]`, problems, 1, cells.length);
95
+ const c = int(value[1], `${path}[1]`, problems, 1, cells[0].length);
96
+ if (problems.length > before)
97
+ return undefined;
98
+ if (cells[r - 1][c - 1] === null) {
99
+ problems.push(`${path}: row ${r}, column ${c} is left out of the board`);
100
+ return undefined;
101
+ }
102
+ return [r, c];
103
+ }
104
+ function checkEdges(value, path, problems, cells, most_) {
105
+ const keys = [];
106
+ list(value, path, problems, 1, most_).forEach((e, i) => {
107
+ const at = `${path}[${i}]`;
108
+ if (!isObj(e)) {
109
+ problems.push(`${at}: must be a cell and a side`);
110
+ return;
111
+ }
112
+ only(e, ["cell", "side"], at, problems);
113
+ const cell = checkAt(e.cell, `${at}.cell`, problems, cells);
114
+ const before = problems.length;
115
+ const side = oneOf(e.side, sides, `${at}.side`, problems);
116
+ if (!cell || problems.length > before)
117
+ return;
118
+ const key = edgeKey(cell, side);
119
+ if (keys.includes(key))
120
+ problems.push(`${at}: names the same line as an earlier one`);
121
+ keys.push(key);
122
+ });
123
+ return keys;
124
+ }
125
+ /** cellSize is the side of a cell: 64 px, smaller for a grid of 9 or 10 columns. */
126
+ function cellSize(cols) {
127
+ return Math.min(64, Math.floor(560 / cols));
128
+ }
129
+ function haloText(pen, x, y, words, size, fill, halo) {
130
+ pen.add(`<text x="${n(x)}" y="${n(y + size * 0.36)}" font-size="${n(size)}" font-weight="700" text-anchor="middle" fill="${fill}" stroke="${halo}" stroke-width="6" stroke-linejoin="round" paint-order="stroke" font-family="${esc(font)}">${esc(words)}</text>`);
131
+ }
132
+ function arrowHead(pen, tip, dir, color, size) {
133
+ const len = Math.hypot(dir.x, dir.y) || 1;
134
+ const u = { x: dir.x / len, y: dir.y / len };
135
+ const back = { x: tip.x - u.x * size, y: tip.y - u.y * size };
136
+ pen.el("path", {
137
+ d: poly([
138
+ tip,
139
+ { x: back.x - u.y * size * 0.62, y: back.y + u.x * size * 0.62 },
140
+ { x: back.x + u.y * size * 0.62, y: back.y - u.x * size * 0.62 },
141
+ ]),
142
+ fill: color,
143
+ stroke: ink,
144
+ "stroke-width": 2,
145
+ "stroke-linejoin": "round",
146
+ });
147
+ }
148
+ function around(p, rx, ry) {
149
+ return { x0: p.x - rx, y0: p.y - ry, x1: p.x + rx, y1: p.y + ry };
150
+ }
151
+ function overlap(a, b) {
152
+ return (Math.max(0, Math.min(a.x1, b.x1) - Math.max(a.x0, b.x0)) *
153
+ Math.max(0, Math.min(a.y1, b.y1) - Math.max(a.y0, b.y0)));
154
+ }
155
+ /**
156
+ * labelPlace finds a spot on a jump's arc, or near it, where a label covers
157
+ * nothing taken; failing that, the spot where it covers least.
158
+ */
159
+ function labelPlace(j, w, h, taken) {
160
+ const mid = { x: (j.p0.x + j.p2.x) / 2, y: (j.p0.y + j.p2.y) / 2 };
161
+ const len = Math.hypot(j.p1.x - mid.x, j.p1.y - mid.y) || 1;
162
+ const out = { x: (j.p1.x - mid.x) / len, y: (j.p1.y - mid.y) / len };
163
+ let best = j.apex;
164
+ let least = Number.POSITIVE_INFINITY;
165
+ for (const push of [0, 8, 16, -8, 26, -16, 36])
166
+ for (const t of [0.5, 0.4, 0.6, 0.3, 0.7, 0.2, 0.8]) {
167
+ const q = 1 - t;
168
+ const p = {
169
+ x: q * q * j.p0.x + 2 * q * t * j.p1.x + t * t * j.p2.x + out.x * push,
170
+ y: q * q * j.p0.y + 2 * q * t * j.p1.y + t * t * j.p2.y + out.y * push,
171
+ };
172
+ const box = around(p, w / 2, h / 2);
173
+ const covered = taken.reduce((sum, b) => sum + overlap(b, box), 0);
174
+ if (covered === 0)
175
+ return p;
176
+ if (covered < least) {
177
+ least = covered;
178
+ best = p;
179
+ }
180
+ }
181
+ return best;
182
+ }
183
+ /** jumpOf is the arc of a jump from one cell's centre to another's, bowed up, or right when it goes mostly up or down. */
184
+ function jumpOf(a, b, s) {
185
+ const dx = b.x - a.x;
186
+ const dy = b.y - a.y;
187
+ const len = Math.hypot(dx, dy);
188
+ const u = { x: dx / len, y: dy / len };
189
+ const left = { x: u.y, y: -u.x };
190
+ const up = Math.abs(dx) >= Math.abs(dy) ? left.y < 0 : left.x > 0;
191
+ const side = up ? left : { x: -left.x, y: -left.y };
192
+ const e = 0.3 * s;
193
+ const h = 0.8 * s + 0.22 * len;
194
+ const p0 = { x: a.x + side.x * e + u.x * 0.1 * s, y: a.y + side.y * e + u.y * 0.1 * s };
195
+ const p2 = { x: b.x + side.x * e - u.x * 0.1 * s, y: b.y + side.y * e - u.y * 0.1 * s };
196
+ const mid = { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 };
197
+ const p1 = { x: mid.x + side.x * h, y: mid.y + side.y * h };
198
+ const apex = {
199
+ x: 0.25 * p0.x + 0.5 * p1.x + 0.25 * p2.x,
200
+ y: 0.25 * p0.y + 0.5 * p1.y + 0.25 * p2.y,
201
+ };
202
+ return { p0, p1, p2, apex };
203
+ }
204
+ function name(g, at) {
205
+ return `row ${g.rowLabels?.[at[0] - 1] ?? at[0]}, column ${g.colLabels?.[at[1] - 1] ?? at[1]}`;
206
+ }
207
+ function sayCell(cell) {
208
+ if (cell === null)
209
+ return "no cell";
210
+ const o = cellObj(cell);
211
+ const what = o.ask
212
+ ? "?"
213
+ : o.text !== undefined
214
+ ? `"${o.text}"`
215
+ : o.thing !== undefined
216
+ ? wordOf(o.thing)
217
+ : undefined;
218
+ if (what && o.fill)
219
+ return `${what} on ${colors[o.fill].word}`;
220
+ return what ?? (o.fill ? colors[o.fill].word : "empty");
221
+ }
222
+ function sayLine(g, line) {
223
+ const [a, b] = line.cells;
224
+ if (a && b)
225
+ return `between ${name(g, a)} and ${name(g, b)}`;
226
+ const at = (a ?? b);
227
+ const h = line.key[0] === "h";
228
+ const side = h ? (a ? "bottom" : "top") : a ? "right" : "left";
229
+ return `along the ${side} of ${name(g, at)}`;
230
+ }
231
+ export const grid = {
232
+ kind: "grid",
233
+ members: ["kind", "cells", "walls", "doors", "path", "rowLabels", "colLabels"],
234
+ check(scene, problems) {
235
+ only(scene, this.members, "scene", problems);
236
+ const before = problems.length;
237
+ const rows = list(scene.cells, "cells", problems, 1, most);
238
+ let cols = 0;
239
+ rows.forEach((row, r) => {
240
+ const cells = list(row, `cells[${r}]`, problems, 1, most);
241
+ if (r === 0)
242
+ cols = cells.length;
243
+ else if (cells.length !== cols)
244
+ problems.push(`cells[${r}]: must hold ${cols} cells, as cells[0] does`);
245
+ cells.forEach((cell, c) => {
246
+ checkCell(cell, `cells[${r}][${c}]`, problems);
247
+ });
248
+ });
249
+ if (problems.length > before || rows.length === 0)
250
+ return;
251
+ const cells = scene.cells;
252
+ if (cells.every((row) => row.every((c) => c === null)))
253
+ problems.push("cells: at least one cell must be on the board");
254
+ let walls = [];
255
+ if (scene.walls === "all")
256
+ walls = "all";
257
+ else if (scene.walls !== undefined)
258
+ walls = checkEdges(scene.walls, "walls", problems, cells, 220);
259
+ if (scene.doors !== undefined) {
260
+ const doors = checkEdges(scene.doors, "doors", problems, cells, 60);
261
+ if (scene.walls === undefined)
262
+ problems.push('doors: a door needs walls, a list or "all"');
263
+ else if (walls !== "all")
264
+ doors.forEach((key, i) => {
265
+ if (!walls.includes(key))
266
+ problems.push(`doors[${i}]: must be on a wall`);
267
+ });
268
+ }
269
+ if (scene.path !== undefined) {
270
+ const p = scene.path;
271
+ if (!isObj(p))
272
+ problems.push("path: must be an object with through");
273
+ else {
274
+ only(p, ["through", "style", "labels", "color"], "path", problems);
275
+ const through = list(p.through, "path.through", problems, 2, 40).map((at, i) => checkAt(at, `path.through[${i}]`, problems, cells));
276
+ through.forEach((at, i) => {
277
+ const prev = through[i - 1];
278
+ if (i > 0 && at && prev && at[0] === prev[0] && at[1] === prev[1])
279
+ problems.push(`path.through[${i}]: a step must go to another cell`);
280
+ });
281
+ if (p.style !== undefined)
282
+ oneOf(p.style, pathStyles, "path.style", problems);
283
+ if (p.color !== undefined && !isColor(p.color))
284
+ problems.push(`path.color: must be a colour: ${colorNames.join(", ")}`);
285
+ if (p.labels !== undefined) {
286
+ if (p.style !== "jumps")
287
+ problems.push('path.labels: only a path of style "jumps" has labels');
288
+ const labels = list(p.labels, "path.labels", problems, 1, 39);
289
+ if (Array.isArray(p.through) && labels.length !== p.through.length - 1)
290
+ problems.push(`path.labels: must hold one label for each jump, ${p.through.length - 1}`);
291
+ labels.forEach((l, i) => {
292
+ text(l, `path.labels[${i}]`, problems, 4);
293
+ });
294
+ }
295
+ }
296
+ }
297
+ for (const [member, count] of [
298
+ ["rowLabels", cells.length],
299
+ ["colLabels", cols],
300
+ ]) {
301
+ if (scene[member] === undefined)
302
+ continue;
303
+ const labels = list(scene[member], member, problems, count, count);
304
+ labels.forEach((l, i) => {
305
+ text(l, `${member}[${i}]`, problems, 3);
306
+ });
307
+ }
308
+ },
309
+ draw(scene, pen) {
310
+ const g = scene;
311
+ const rows = g.cells.length;
312
+ const cols = g.cells[0].length;
313
+ const s = cellSize(cols);
314
+ const sub = pen.sub();
315
+ const lines = linesOf(g.cells, s);
316
+ const walls = new Set(g.walls === "all" ? lines.keys() : (g.walls ?? []).map((w) => edgeKey(w.cell, w.side)));
317
+ const doors = new Set((g.doors ?? []).map((d) => edgeKey(d.cell, d.side)));
318
+ const centre = (at) => ({ x: (at[1] - 0.5) * s, y: (at[0] - 0.5) * s });
319
+ const box = { x0: 0, y0: 0, x1: cols * s, y1: rows * s };
320
+ const grow = (p, rx = 0, ry = rx) => {
321
+ box.x0 = Math.min(box.x0, p.x - rx);
322
+ box.x1 = Math.max(box.x1, p.x + rx);
323
+ box.y0 = Math.min(box.y0, p.y - ry);
324
+ box.y1 = Math.max(box.y1, p.y + ry);
325
+ };
326
+ // fills, one rect for each cell of the board
327
+ g.cells.forEach((row, r) => {
328
+ row.forEach((cell, c) => {
329
+ if (cell === null)
330
+ return;
331
+ const fill = cellObj(cell).fill;
332
+ sub.el("rect", {
333
+ class: "cell",
334
+ x: c * s,
335
+ y: r * s,
336
+ width: s,
337
+ height: s,
338
+ fill: fill ? colors[fill].fill : ground,
339
+ });
340
+ });
341
+ });
342
+ // the thin lines between cells
343
+ for (const line of lines.values()) {
344
+ if (walls.has(line.key) || !line.cells[0] || !line.cells[1])
345
+ continue;
346
+ sub.el("path", {
347
+ d: poly([line.a, line.b], false),
348
+ stroke: ink,
349
+ "stroke-width": 1.8,
350
+ });
351
+ }
352
+ const path = g.path;
353
+ const color = colors[path?.color ?? "red"].fill;
354
+ const style = path?.style ?? "line";
355
+ if (path && style === "line") {
356
+ const points = path.through.map(centre);
357
+ const last = path.through[path.through.length - 1];
358
+ const end = points[points.length - 1];
359
+ const prev = points[points.length - 2];
360
+ const dir = { x: end.x - prev.x, y: end.y - prev.y };
361
+ const len = Math.hypot(dir.x, dir.y);
362
+ const lastCell = cellObj(g.cells[last[0] - 1][last[1] - 1]);
363
+ const filled = lastCell.text !== undefined || lastCell.thing !== undefined || lastCell.ask;
364
+ const back = filled ? 0.3 * s : 0.08 * s;
365
+ const tip = { x: end.x - (dir.x / len) * back, y: end.y - (dir.y / len) * back };
366
+ const shaft = { x: tip.x - (dir.x / len) * 9, y: tip.y - (dir.y / len) * 9 };
367
+ const d = poly([...points.slice(0, -1), shaft], false);
368
+ sub.el("path", {
369
+ d,
370
+ fill: "none",
371
+ stroke: ink,
372
+ "stroke-width": 8,
373
+ "stroke-linejoin": "round",
374
+ "stroke-linecap": "round",
375
+ });
376
+ sub.el("path", {
377
+ d,
378
+ fill: "none",
379
+ stroke: color,
380
+ "stroke-width": 4.5,
381
+ "stroke-linejoin": "round",
382
+ "stroke-linecap": "round",
383
+ });
384
+ sub.el("circle", {
385
+ cx: points[0].x,
386
+ cy: points[0].y,
387
+ r: 7,
388
+ fill: color,
389
+ stroke: ink,
390
+ "stroke-width": 2,
391
+ });
392
+ arrowHead(sub, tip, dir, color, 15);
393
+ }
394
+ // the border and the walls, doors left open
395
+ for (const line of lines.values()) {
396
+ const wall = walls.has(line.key);
397
+ const border = !line.cells[0] || !line.cells[1];
398
+ if (!wall && !border)
399
+ continue;
400
+ const at = (t) => ({
401
+ x: line.a.x + (line.b.x - line.a.x) * t,
402
+ y: line.a.y + (line.b.y - line.a.y) * t,
403
+ });
404
+ const width = wall ? wallWidth : outline + 0.5;
405
+ const cap = wall ? "round" : "square";
406
+ const parts = doors.has(line.key)
407
+ ? [
408
+ [0, 0.22],
409
+ [0.78, 1],
410
+ ]
411
+ : [[0, 1]];
412
+ for (const [t0, t1] of parts)
413
+ sub.el("path", {
414
+ d: poly([at(t0), at(t1)], false),
415
+ stroke: ink,
416
+ "stroke-width": width,
417
+ "stroke-linecap": cap,
418
+ });
419
+ }
420
+ // jumps, under the cells' contents
421
+ const jumps = path && style === "jumps"
422
+ ? path.through.slice(1).map((at, i) => jumpOf(centre(path.through[i]), centre(at), s))
423
+ : [];
424
+ for (const j of jumps) {
425
+ const d = `M${n(j.p0.x)} ${n(j.p0.y)}Q${n(j.p1.x)} ${n(j.p1.y)} ${n(j.p2.x)} ${n(j.p2.y)}`;
426
+ sub.el("path", {
427
+ d,
428
+ fill: "none",
429
+ stroke: ink,
430
+ "stroke-width": 6.5,
431
+ "stroke-linecap": "round",
432
+ });
433
+ sub.el("path", {
434
+ d,
435
+ fill: "none",
436
+ stroke: color,
437
+ "stroke-width": 3.5,
438
+ "stroke-linecap": "round",
439
+ });
440
+ arrowHead(sub, j.p2, { x: j.p2.x - j.p1.x, y: j.p2.y - j.p1.y }, color, 13);
441
+ for (let t = 0; t <= 1; t += 0.1) {
442
+ const q = 1 - t;
443
+ grow({
444
+ x: q * q * j.p0.x + 2 * q * t * j.p1.x + t * t * j.p2.x,
445
+ y: q * q * j.p0.y + 2 * q * t * j.p1.y + t * t * j.p2.y,
446
+ }, 5);
447
+ }
448
+ }
449
+ // the contents of the cells
450
+ const halo = path !== undefined;
451
+ const big = s * 0.44;
452
+ g.cells.forEach((row, r) => {
453
+ row.forEach((cell, c) => {
454
+ if (cell === null)
455
+ return;
456
+ const o = cellObj(cell);
457
+ const p = centre([r + 1, c + 1]);
458
+ if (o.ask)
459
+ drawThing(sub, { ask: true }, p.x, p.y, s * 0.74);
460
+ else if (o.thing !== undefined)
461
+ drawThing(sub, o.thing, p.x, p.y, s * 0.72);
462
+ else if (o.text !== undefined) {
463
+ const size = [...o.text].length > 2 ? s * 0.34 : big;
464
+ const ink_ = o.fill ? textOn(o.fill) : ink;
465
+ if (halo)
466
+ haloText(sub, p.x, p.y, o.text, size, ink_, o.fill ? colors[o.fill].fill : ground);
467
+ else
468
+ sub.text(p.x, p.y, o.text, { size, fill: ink_ });
469
+ }
470
+ });
471
+ });
472
+ // the jumps' labels, on top, each where it covers no cell's content
473
+ const taken = [];
474
+ g.cells.forEach((row, r) => {
475
+ row.forEach((cell, c) => {
476
+ const o = cell === null ? {} : cellObj(cell);
477
+ const p = centre([r + 1, c + 1]);
478
+ if (o.ask || o.thing !== undefined)
479
+ taken.push(around(p, 0.37 * s, 0.37 * s));
480
+ else if (o.text !== undefined) {
481
+ const size = [...o.text].length > 2 ? s * 0.34 : big;
482
+ taken.push(around(p, textWidth(o.text, size) / 2 + 4, size * 0.45));
483
+ }
484
+ });
485
+ });
486
+ jumps.forEach((j, i) => {
487
+ const label = path?.labels?.[i];
488
+ if (!label)
489
+ return;
490
+ const size = 17;
491
+ const w = textWidth(label, size) + 14;
492
+ const h = 25;
493
+ const at = labelPlace(j, w, h, taken);
494
+ taken.push(around(at, w / 2, h / 2));
495
+ sub.el("rect", {
496
+ x: at.x - w / 2,
497
+ y: at.y - h / 2,
498
+ width: w,
499
+ height: h,
500
+ rx: h / 2,
501
+ fill: ground,
502
+ stroke: ink,
503
+ "stroke-width": 2,
504
+ });
505
+ sub.text(at.x, at.y, label, { size });
506
+ grow(at, w / 2 + 2, h / 2 + 2);
507
+ });
508
+ // labels of rows on the left, of columns above everything else
509
+ g.rowLabels?.forEach((label, r) => {
510
+ sub.text(-20, (r + 0.5) * s, label, { size: 20 });
511
+ grow({ x: -20, y: (r + 0.5) * s }, textWidth(label, 20) / 2 + 10, 12);
512
+ });
513
+ const top = Math.min(box.y0, -wallWidth / 2) - 18;
514
+ g.colLabels?.forEach((label, c) => {
515
+ sub.text((c + 0.5) * s, top, label, { size: 20 });
516
+ grow({ x: (c + 0.5) * s, y: top }, 10, 14);
517
+ });
518
+ const pad = wallWidth / 2;
519
+ box.x0 = Math.min(box.x0, -pad);
520
+ box.y0 = Math.min(box.y0, -pad);
521
+ box.x1 = Math.max(box.x1, cols * s + pad);
522
+ box.y1 = Math.max(box.y1, rows * s + pad);
523
+ pen.place(sub, margin - box.x0, margin - box.y0);
524
+ return { width: box.x1 - box.x0 + 2 * margin, height: box.y1 - box.y0 + 2 * margin };
525
+ },
526
+ describe(scene) {
527
+ const g = scene;
528
+ const rows = g.cells.length;
529
+ const cols = g.cells[0].length;
530
+ const out = [];
531
+ const left = [];
532
+ g.cells.forEach((row, r) => {
533
+ row.forEach((cell, c) => {
534
+ if (cell === null)
535
+ left.push(name(g, [r + 1, c + 1]));
536
+ });
537
+ });
538
+ out.push(`A grid of ${rows} row${rows === 1 ? "" : "s"} and ${cols} column${cols === 1 ? "" : "s"}${left.length > 0
539
+ ? `, ${left.length} of its places left out of the board (${left.join("; ")})`
540
+ : ""}.`);
541
+ if (g.rowLabels)
542
+ out.push(`The rows are labelled ${g.rowLabels.join(", ")} from the top.`);
543
+ if (g.colLabels)
544
+ out.push(`The columns are labelled ${g.colLabels.join(", ")} from the left.`);
545
+ const shown = g.cells.some((row) => row.some((c) => c !== null && sayCell(c) !== "empty"));
546
+ if (shown)
547
+ g.cells.forEach((row, r) => {
548
+ out.push(`Row ${g.rowLabels?.[r] ?? r + 1} from the left: ${row.map(sayCell).join(", ")}.`);
549
+ });
550
+ else
551
+ out.push("Every cell is empty.");
552
+ const fills = new Map();
553
+ let plain = 0;
554
+ for (const row of g.cells)
555
+ for (const cell of row) {
556
+ if (cell === null)
557
+ continue;
558
+ const fill = cellObj(cell).fill;
559
+ if (fill)
560
+ fills.set(fill, (fills.get(fill) ?? 0) + 1);
561
+ else
562
+ plain++;
563
+ }
564
+ if (fills.size > 0)
565
+ out.push(`Coloured cells: ${[...fills].map(([c, k]) => `${k} ${colors[c].word}`).join(", ")}${plain > 0 ? `; ${plain} not coloured` : ""}.`);
566
+ const lines = linesOf(g.cells, 1);
567
+ if (g.walls === "all")
568
+ out.push("Every cell is a room with walls on all its sides.");
569
+ else if (g.walls)
570
+ out.push(`Thick walls: ${g.walls.map((w) => sayLine(g, lines.get(edgeKey(w.cell, w.side)))).join("; ")}.`);
571
+ if (g.doors)
572
+ out.push(`Openings in the walls: ${g.doors.map((d) => sayLine(g, lines.get(edgeKey(d.cell, d.side)))).join("; ")}.`);
573
+ if (g.path) {
574
+ const color = colors[g.path.color ?? "red"].word;
575
+ const through = g.path.through;
576
+ if ((g.path.style ?? "line") === "line")
577
+ out.push(`A ${color} line with an arrow goes from ${name(g, through[0])}${through.length > 2
578
+ ? ` through ${through
579
+ .slice(1, -1)
580
+ .map((at) => name(g, at))
581
+ .join(", then ")}`
582
+ : ""} to ${name(g, through[through.length - 1])}.`);
583
+ else
584
+ out.push(`${through.length - 1} ${color} jump${through.length > 2 ? "s" : ""}: ${through
585
+ .slice(1)
586
+ .map((at, i) => `from ${name(g, through[i])} to ${name(g, at)}${g.path?.labels ? `, labelled "${g.path.labels[i]}"` : ""}`)
587
+ .join("; ")}.`);
588
+ }
589
+ return out.join(" ");
590
+ },
591
+ };
@@ -0,0 +1,23 @@
1
+ import type { Kind } from "../kind.ts";
2
+ import { type Thing } from "../things.ts";
3
+ /**
4
+ * Groups: things in groups, each group in a holder the picture draws itself —
5
+ * a tree, a bush, a plate, a basket, a box, a pond, a jar — or in none. The
6
+ * holder has places, and the picture puts each thing in a place of its own,
7
+ * so a group shows exactly as many things as the scene says.
8
+ */
9
+ export declare const holders: readonly ["none", "tree", "bush", "plate", "basket", "box", "pond", "jar"];
10
+ export type Holder = (typeof holders)[number];
11
+ export type Group = {
12
+ holder?: Holder;
13
+ items: {
14
+ thing: Thing;
15
+ count: number;
16
+ }[];
17
+ label?: string;
18
+ };
19
+ export type GroupsScene = {
20
+ kind: "groups";
21
+ groups: Group[];
22
+ };
23
+ export declare const groups: Kind;