@altnovatrail/picture-kit 0.0.0-stage → 0.1.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.
Files changed (122) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +7 -0
  3. package/README.md +27 -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,664 @@
1
+ import { margin } from "../kind.js";
2
+ import { int, isObj, list, oneOf, only } from "../read.js";
3
+ import { colors, ink, outline } from "../style.js";
4
+ import { n } from "../svg.js";
5
+ import { cardHeight, cardWidth, checkEntry, contentOf, drawCard, drawContent, drawTag, numberOf, ordinal, plural, sayEntry, sayList, sayText, tagHeight, tagWidth, } from "./magic-parts.js";
6
+ /**
7
+ * Magic: a figure of numbers. Its places are slots — circles or boxes along
8
+ * the lines of a triangle, a square, a star or a wheel — or the cells of a
9
+ * cross or a grid; each holds an entry: a number, a short text, nothing, or
10
+ * the "?" of the question. The lines whose sums matter are the figure's own,
11
+ * and the picture draws them:
12
+ *
13
+ * - triangle: perSide (3–5) slots on each side, corners included, slots
14
+ * clockwise from the top corner; with centre (perSide odd) one more slot in
15
+ * the middle, on the three lines from each corner to the middle of the side
16
+ * across. Lines: the right, bottom and left sides, then the three lines
17
+ * from the top, bottom-right and bottom-left corners.
18
+ * - square: perSide (3–5) slots on each side, clockwise from the top-left
19
+ * corner. Lines: the top, right, bottom and left sides.
20
+ * - star: points 5 or 6, a slot at each point and at each crossing of its
21
+ * lines, clockwise round its outline from the top point, a point and a
22
+ * crossing in turn. Lines: each straight line, from each point clockwise.
23
+ * - wheel: a middle slot and lines (2–4) straight lines through it, perArm
24
+ * (1–2) slots on each side of the middle; the middle first, then each arm
25
+ * clockwise from the top, from the middle outwards. Lines: from each end
26
+ * clockwise from the top, through the middle.
27
+ * - cross: a row and a column of cells sharing the middle one, arm (1–3)
28
+ * cells on each side of it; cells row by row from the top. Lines: the row,
29
+ * the column.
30
+ * - grid: rows by columns (2–5) cells, row by row from the top. Lines: the
31
+ * rows, the columns, and with diagonals (a square grid) both diagonals,
32
+ * from the top-left corner and from the top-right one.
33
+ *
34
+ * sums, one for each line in that order, puts a number (or "?") in a tag at
35
+ * the line's end; null shows none. pool shows number cards below the figure.
36
+ * facts may give every slot's value: the check then proves that the shown
37
+ * numbers agree, that the lines add up to their tags — or, with no sums, all
38
+ * to one sum — and that the open slots take cards of the pool.
39
+ */
40
+ export const magicShapes = ["triangle", "square", "star", "wheel", "cross", "grid"];
41
+ export const slotLooks = ["circle", "box"];
42
+ const limits = { biggest: 999, chars: 3 };
43
+ /** A circle slot's radius; a box slot is as wide as a circle. */
44
+ const radius = 24;
45
+ /** A cell's side, in a cross or a grid. */
46
+ const cell = 58;
47
+ // The members only some shapes have, and the shapes that have them.
48
+ const owners = {
49
+ perSide: ["triangle", "square"],
50
+ centre: ["triangle"],
51
+ points: ["star"],
52
+ lines: ["wheel"],
53
+ perArm: ["wheel"],
54
+ arm: ["cross"],
55
+ rows: ["grid"],
56
+ columns: ["grid"],
57
+ diagonals: ["grid"],
58
+ slot: ["triangle", "square", "star", "wheel"],
59
+ sums: ["triangle", "square", "wheel", "cross", "grid"],
60
+ };
61
+ const order = {
62
+ triangle: "clockwise from the top corner, the middle last",
63
+ square: "clockwise from the top-left corner",
64
+ star: "clockwise round the outline from the top point, a point and a crossing in turn",
65
+ wheel: "the middle first, then each arm clockwise from the top, from the middle outwards",
66
+ cross: "row by row from the top, each row from the left",
67
+ grid: "row by row from the top, each row from the left",
68
+ };
69
+ const lerp = (a, b, t) => ({
70
+ x: a.x + (b.x - a.x) * t,
71
+ y: a.y + (b.y - a.y) * t,
72
+ });
73
+ const polar = (r, degrees) => {
74
+ const a = (degrees * Math.PI) / 180;
75
+ return { x: r * Math.cos(a), y: r * Math.sin(a) };
76
+ };
77
+ const unit = (p) => {
78
+ const len = Math.hypot(p.x, p.y) || 1;
79
+ return { x: p.x / len, y: p.y / len };
80
+ };
81
+ /** sayOr joins words with commas and a final "or". */
82
+ function sayOr(words) {
83
+ return words.length <= 1
84
+ ? words.join("")
85
+ : `${words.slice(0, -1).join(", ")} or ${words[words.length - 1]}`;
86
+ }
87
+ /** direction names the way an angle on the screen points, one of eight. */
88
+ function direction(degrees) {
89
+ const words = [
90
+ "right",
91
+ "lower right",
92
+ "bottom",
93
+ "lower left",
94
+ "left",
95
+ "upper left",
96
+ "top",
97
+ "upper right",
98
+ ];
99
+ return words[Math.round((((degrees % 360) + 360) % 360) / 45) % 8];
100
+ }
101
+ /** frame lays slots along the sides of a polygon centred at 0, 0, corners clockwise. */
102
+ function frame(corners, perSide, sides) {
103
+ const k = corners.length;
104
+ const slots = [];
105
+ corners.forEach((c, s) => {
106
+ for (let j = 0; j < perSide - 1; j++)
107
+ slots.push(lerp(c, corners[(s + 1) % k], j / (perSide - 1)));
108
+ });
109
+ const ring = slots.length;
110
+ const lines = corners.map((c, s) => {
111
+ const mid = lerp(c, corners[(s + 1) % k], 0.5);
112
+ return {
113
+ at: Array.from({ length: perSide }, (_, j) => (s * (perSide - 1) + j) % ring),
114
+ name: sides[s][0],
115
+ from: sides[s][1],
116
+ anchor: mid,
117
+ out: unit(mid),
118
+ clear: radius + 10,
119
+ };
120
+ });
121
+ const edges = corners.map((c, s) => [c, corners[(s + 1) % k]]);
122
+ return { slots, lines, edges, cells: false };
123
+ }
124
+ function triangle(perSide, centre) {
125
+ const side = Math.max((perSide - 1) * (perSide === 3 ? 100 : perSide === 4 ? 80 : 74), centre ? 236 : 0);
126
+ const h = (side * Math.sqrt(3)) / 2;
127
+ const corners = [
128
+ { x: 0, y: (-2 * h) / 3 },
129
+ { x: side / 2, y: h / 3 },
130
+ { x: -side / 2, y: h / 3 },
131
+ ];
132
+ const f = frame(corners, perSide, [
133
+ ["the right side", "from the top corner"],
134
+ ["the bottom side", "from the bottom-right corner"],
135
+ ["the left side", "from the bottom-left corner"],
136
+ ]);
137
+ if (!centre)
138
+ return f;
139
+ const middle = f.slots.length;
140
+ f.slots.push({ x: 0, y: 0 });
141
+ const names = ["the top corner", "the bottom-right corner", "the bottom-left corner"];
142
+ const across = ["the bottom side", "the left side", "the right side"];
143
+ corners.forEach((c, s) => {
144
+ const mid = ((s + 1) % 3) * (perSide - 1) + (perSide - 1) / 2;
145
+ f.lines.push({
146
+ at: [s * (perSide - 1), middle, mid],
147
+ name: `the line from ${names[s]}`,
148
+ from: `through the middle to the middle of ${across[s]}`,
149
+ anchor: c,
150
+ out: unit(c),
151
+ clear: radius + 10,
152
+ });
153
+ f.edges.push([c, f.slots[mid]]);
154
+ });
155
+ return f;
156
+ }
157
+ function square(perSide) {
158
+ const a = ((perSide - 1) * (perSide === 3 ? 96 : perSide === 4 ? 80 : 74)) / 2;
159
+ return frame([
160
+ { x: -a, y: -a },
161
+ { x: a, y: -a },
162
+ { x: a, y: a },
163
+ { x: -a, y: a },
164
+ ], perSide, [
165
+ ["the top side", "from the top-left corner"],
166
+ ["the right side", "from the top-right corner"],
167
+ ["the bottom side", "from the bottom-right corner"],
168
+ ["the left side", "from the bottom-left corner"],
169
+ ]);
170
+ }
171
+ function star(points) {
172
+ const outer = points === 5 ? 150 : 140;
173
+ const inner = points === 5 ? outer * 0.381966 : outer / Math.sqrt(3);
174
+ const turn = (i) => -90 + (i * 360) / points;
175
+ const slots = [];
176
+ for (let i = 0; i < points; i++) {
177
+ slots.push(polar(outer, turn(i)));
178
+ slots.push(polar(inner, turn(i + 0.5)));
179
+ }
180
+ const lines = Array.from({ length: points }, (_, i) => ({
181
+ at: [2 * i, 2 * i + 1, 2 * i + 3, 2 * i + 4].map((j) => j % (2 * points)),
182
+ name: `the line from the ${direction(turn(i))} point`,
183
+ from: `to the ${direction(turn(i + 2))} point`,
184
+ anchor: slots[2 * i],
185
+ out: unit(slots[2 * i]),
186
+ clear: radius + 10,
187
+ }));
188
+ const edges = lines.map((l) => [slots[l.at[0]], slots[l.at[3]]]);
189
+ return { slots, lines, edges, cells: false };
190
+ }
191
+ function wheel(count, perArm) {
192
+ // Four lines put eight arms 45° apart: boxes on them need a longer step to stay clear.
193
+ const step = perArm === 1 ? (count === 4 ? 100 : 84) : count === 4 ? 92 : 80;
194
+ const turn = (a) => -90 + (a * 180) / count;
195
+ const slots = [{ x: 0, y: 0 }];
196
+ for (let a = 0; a < 2 * count; a++)
197
+ for (let j = 1; j <= perArm; j++)
198
+ slots.push(polar(j * step, turn(a)));
199
+ const arm = (a) => Array.from({ length: perArm }, (_, j) => 1 + a * perArm + j);
200
+ const lines = Array.from({ length: count }, (_, i) => {
201
+ const end = slots[1 + i * perArm + perArm - 1];
202
+ return {
203
+ at: [...arm(i).reverse(), 0, ...arm(i + count)],
204
+ name: `the line from the ${direction(turn(i))} end`,
205
+ from: `through the middle to the ${direction(turn(i + count))} end`,
206
+ anchor: end,
207
+ out: unit(end),
208
+ clear: radius + 10,
209
+ };
210
+ });
211
+ const edges = lines.map((l) => [slots[l.at[0]], slots[l.at[l.at.length - 1]]]);
212
+ return { slots, lines, edges, cells: false };
213
+ }
214
+ function cross(arm) {
215
+ const slots = [];
216
+ for (let j = 0; j < arm; j++)
217
+ slots.push({ x: 0, y: (j - arm) * cell });
218
+ for (let i = -arm; i <= arm; i++)
219
+ slots.push({ x: i * cell, y: 0 });
220
+ for (let j = 1; j <= arm; j++)
221
+ slots.push({ x: 0, y: j * cell });
222
+ const side = { clear: cell / 2 + 10, square: true };
223
+ return {
224
+ slots,
225
+ lines: [
226
+ {
227
+ at: Array.from({ length: 2 * arm + 1 }, (_, i) => arm + i),
228
+ name: "the row",
229
+ from: "from left to right",
230
+ anchor: { x: arm * cell, y: 0 },
231
+ out: { x: 1, y: 0 },
232
+ ...side,
233
+ },
234
+ {
235
+ at: [
236
+ ...Array.from({ length: arm }, (_, j) => j),
237
+ 2 * arm,
238
+ ...Array.from({ length: arm }, (_, j) => 3 * arm + 1 + j),
239
+ ],
240
+ name: "the column",
241
+ from: "from top to bottom",
242
+ anchor: { x: 0, y: arm * cell },
243
+ out: { x: 0, y: 1 },
244
+ ...side,
245
+ },
246
+ ],
247
+ edges: [],
248
+ cells: true,
249
+ };
250
+ }
251
+ function grid(rows, columns, diagonals) {
252
+ const slots = [];
253
+ for (let i = 0; i < rows; i++)
254
+ for (let j = 0; j < columns; j++)
255
+ slots.push({ x: (j - (columns - 1) / 2) * cell, y: (i - (rows - 1) / 2) * cell });
256
+ const at = (i, j) => i * columns + j;
257
+ const side = { clear: cell / 2 + 10, square: true };
258
+ const lines = [];
259
+ for (let i = 0; i < rows; i++)
260
+ lines.push({
261
+ at: Array.from({ length: columns }, (_, j) => at(i, j)),
262
+ name: `row ${i + 1}`,
263
+ from: "from left to right",
264
+ anchor: slots[at(i, columns - 1)],
265
+ out: { x: 1, y: 0 },
266
+ ...side,
267
+ });
268
+ for (let j = 0; j < columns; j++)
269
+ lines.push({
270
+ at: Array.from({ length: rows }, (_, i) => at(i, j)),
271
+ name: `column ${j + 1}`,
272
+ from: "from top to bottom",
273
+ anchor: slots[at(rows - 1, j)],
274
+ out: { x: 0, y: 1 },
275
+ ...side,
276
+ });
277
+ if (diagonals) {
278
+ lines.push({
279
+ at: Array.from({ length: rows }, (_, i) => at(i, i)),
280
+ name: "the diagonal from the top-left corner",
281
+ from: "down to the bottom-right corner",
282
+ anchor: slots[at(rows - 1, columns - 1)],
283
+ out: { x: 1, y: 1 },
284
+ ...side,
285
+ diagonal: true,
286
+ });
287
+ lines.push({
288
+ at: Array.from({ length: rows }, (_, i) => at(i, columns - 1 - i)),
289
+ name: "the diagonal from the top-right corner",
290
+ from: "down to the bottom-left corner",
291
+ anchor: slots[at(rows - 1, 0)],
292
+ out: { x: -1, y: 1 },
293
+ ...side,
294
+ diagonal: true,
295
+ });
296
+ }
297
+ return { slots, lines, edges: [], cells: true };
298
+ }
299
+ function figureOf(s) {
300
+ switch (s.shape) {
301
+ case "triangle":
302
+ return triangle(s.perSide ?? 3, s.centre === true);
303
+ case "square":
304
+ return square(s.perSide ?? 3);
305
+ case "star":
306
+ return star(s.points ?? 5);
307
+ case "wheel":
308
+ return wheel(s.lines ?? 3, s.perArm ?? 1);
309
+ case "cross":
310
+ return cross(s.arm ?? 1);
311
+ case "grid":
312
+ return grid(s.rows ?? 3, s.columns ?? 3, s.diagonals === true);
313
+ }
314
+ }
315
+ /** arrowFrom draws a short arrow from a to b, its head at b. */
316
+ function arrowFrom(pen, a, b) {
317
+ const u = unit({ x: b.x - a.x, y: b.y - a.y });
318
+ const back = { x: b.x - u.x * 8, y: b.y - u.y * 8 };
319
+ pen.el("path", {
320
+ d: `M${n(a.x)} ${n(a.y)}L${n(back.x)} ${n(back.y)}`,
321
+ stroke: ink,
322
+ "stroke-width": 2.5,
323
+ "stroke-linecap": "round",
324
+ });
325
+ pen.el("path", {
326
+ d: `M${n(b.x)} ${n(b.y)}L${n(back.x - u.y * 5)} ${n(back.y + u.x * 5)}L${n(back.x + u.y * 5)} ${n(back.y - u.x * 5)}Z`,
327
+ fill: ink,
328
+ stroke: ink,
329
+ "stroke-width": 1.5,
330
+ "stroke-linejoin": "round",
331
+ });
332
+ }
333
+ /** magicLines lists the lines of a checked scene, each as the indices of its slots. */
334
+ export function magicLines(scene) {
335
+ return figureOf(scene).lines.map((l) => l.at);
336
+ }
337
+ /** tagAt is the centre of the tag of a line, w pixels wide; a diagonal's arrow is drawn on the way. */
338
+ function tagAt(line, w, pen) {
339
+ const h = tagHeight;
340
+ if (line.diagonal) {
341
+ const sx = line.out.x;
342
+ const corner = { x: line.anchor.x + (sx * cell) / 2, y: line.anchor.y + cell / 2 };
343
+ const tip = { x: corner.x + sx * 26, y: corner.y + 26 };
344
+ if (pen)
345
+ arrowFrom(pen, { x: corner.x + sx * 6, y: corner.y + 6 }, tip);
346
+ return { x: tip.x + sx * (w / 2 - 6), y: tip.y + h / 2 + 1 };
347
+ }
348
+ if (line.square)
349
+ return {
350
+ x: line.anchor.x + line.out.x * (line.clear + w / 2),
351
+ y: line.anchor.y + line.out.y * (line.clear + h / 2),
352
+ };
353
+ const reach = line.clear + Math.abs(line.out.x) * (w / 2) + Math.abs(line.out.y) * (h / 2);
354
+ return { x: line.anchor.x + line.out.x * reach, y: line.anchor.y + line.out.y * reach };
355
+ }
356
+ const sumText = (sum) => sum === null ? undefined : typeof sum === "number" ? String(sum) : "?";
357
+ const plainText = (value) => String(value);
358
+ function isFlag(value) {
359
+ return value === undefined || typeof value === "boolean";
360
+ }
361
+ function lookOf(s) {
362
+ return s.shape === "cross" || s.shape === "grid" ? "cell" : (s.slot ?? "circle");
363
+ }
364
+ /** drawSlot draws one place of the figure with what it holds, centred at p. */
365
+ function drawSlot(pen, entry, p, look) {
366
+ const c = contentOf(entry);
367
+ const dashed = c.ask === true;
368
+ const paint = {
369
+ fill: colors[c.fill ?? "white"].fill,
370
+ stroke: ink,
371
+ "stroke-width": outline,
372
+ "stroke-dasharray": dashed ? "6 5" : undefined,
373
+ };
374
+ if (look === "circle")
375
+ pen.el("circle", { class: "slot", cx: p.x, cy: p.y, r: radius, ...paint });
376
+ else
377
+ pen.el("rect", {
378
+ class: "slot",
379
+ x: p.x - radius,
380
+ y: p.y - radius,
381
+ width: 2 * radius,
382
+ height: 2 * radius,
383
+ rx: 8,
384
+ ...paint,
385
+ });
386
+ drawContent(pen, entry, p.x, p.y, 2 * radius, { dashed });
387
+ }
388
+ export const magic = {
389
+ kind: "magic",
390
+ members: [
391
+ "kind",
392
+ "shape",
393
+ "perSide",
394
+ "centre",
395
+ "points",
396
+ "lines",
397
+ "perArm",
398
+ "arm",
399
+ "rows",
400
+ "columns",
401
+ "diagonals",
402
+ "slot",
403
+ "slots",
404
+ "sums",
405
+ "pool",
406
+ "facts",
407
+ ],
408
+ check(scene, problems) {
409
+ const start = problems.length;
410
+ only(scene, this.members, "scene", problems);
411
+ const before = problems.length;
412
+ const shape = oneOf(scene.shape, magicShapes, "shape", problems);
413
+ if (problems.length > before)
414
+ return;
415
+ for (const [member, shapes] of Object.entries(owners))
416
+ if (scene[member] !== undefined && !shapes.includes(shape))
417
+ problems.push(`${member}: is for ${sayOr(shapes.map((s) => `a ${s}`))}, not a ${shape}`);
418
+ const sized = problems.length;
419
+ for (const flag of ["centre", "diagonals"])
420
+ if (!isFlag(scene[flag]))
421
+ problems.push(`${flag}: must be true or false`);
422
+ if (scene.perSide !== undefined)
423
+ int(scene.perSide, "perSide", problems, 3, 5);
424
+ if (scene.points !== undefined)
425
+ int(scene.points, "points", problems, 5, 6);
426
+ if (scene.lines !== undefined)
427
+ int(scene.lines, "lines", problems, 2, 4);
428
+ if (scene.perArm !== undefined)
429
+ int(scene.perArm, "perArm", problems, 1, 2);
430
+ if (scene.arm !== undefined)
431
+ int(scene.arm, "arm", problems, 1, 3);
432
+ if (scene.rows !== undefined)
433
+ int(scene.rows, "rows", problems, 2, 5);
434
+ if (scene.columns !== undefined)
435
+ int(scene.columns, "columns", problems, 2, 5);
436
+ if (scene.slot !== undefined)
437
+ oneOf(scene.slot, slotLooks, "slot", problems);
438
+ if (scene.centre === true && shape === "triangle" && (scene.perSide ?? 3) % 2 === 0)
439
+ problems.push("centre: needs an odd perSide, 3 or 5, so that each line from a corner ends in the middle slot of the side across");
440
+ if (scene.diagonals === true && shape === "grid" && (scene.rows ?? 3) !== (scene.columns ?? 3))
441
+ problems.push("diagonals: only a square grid, as many rows as columns, has diagonals");
442
+ if (problems.length > sized)
443
+ return;
444
+ const typed = scene;
445
+ const f = figureOf(typed);
446
+ const count = f.slots.length;
447
+ const slots = Array.isArray(scene.slots) ? scene.slots : [];
448
+ if (!Array.isArray(scene.slots) || slots.length !== count)
449
+ problems.push(`slots: must hold ${count} entries, one for each place, ${order[shape]}`);
450
+ slots.forEach((e, i) => {
451
+ checkEntry(e, `slots[${i}]`, problems, limits);
452
+ });
453
+ if (scene.sums !== undefined) {
454
+ const k = f.lines.length;
455
+ const sums = Array.isArray(scene.sums) ? scene.sums : [];
456
+ if (!Array.isArray(scene.sums) || sums.length !== k)
457
+ problems.push(`sums: must hold ${k}, one for each line: ${f.lines.map((l) => l.name).join(", ")}; null where no tag is shown`);
458
+ sums.forEach((sum, i) => {
459
+ const path = `sums[${i}]`;
460
+ if (sum === null)
461
+ return;
462
+ if (isObj(sum)) {
463
+ if (sum.ask !== true || Object.keys(sum).length !== 1)
464
+ problems.push(`${path}: must be a whole number, null or {"ask": true}`);
465
+ return;
466
+ }
467
+ int(sum, path, problems, 0, 9999);
468
+ });
469
+ }
470
+ if (scene.pool !== undefined) {
471
+ list(scene.pool, "pool", problems, 1, 12).forEach((card, i) => {
472
+ const path = `pool[${i}]`;
473
+ if (typeof card === "number")
474
+ int(card, path, problems, 0, 999);
475
+ else if (typeof card !== "string" || card.trim() === "" || card.trim() === "?")
476
+ problems.push(`${path}: must be a whole number or a short text`);
477
+ else if ([...card].length > 3)
478
+ problems.push(`${path}: at most 3 characters`);
479
+ });
480
+ }
481
+ if (scene.facts === undefined || problems.length > start)
482
+ return;
483
+ if (!isObj(scene.facts)) {
484
+ problems.push("facts: must hold values, a number for each slot");
485
+ return;
486
+ }
487
+ only(scene.facts, ["values"], "facts", problems);
488
+ const values = list(scene.facts.values, "facts.values", problems, count, count);
489
+ values.forEach((v, i) => {
490
+ int(v, `facts.values[${i}]`, problems, 0, 9999);
491
+ });
492
+ if (problems.length > start)
493
+ return;
494
+ const known = values;
495
+ typed.slots.forEach((e, i) => {
496
+ const shown = numberOf(e);
497
+ if (shown !== undefined && shown !== known[i])
498
+ problems.push(`facts.values[${i}]: slots[${i}] shows ${shown}, not ${known[i]}`);
499
+ });
500
+ const totals = f.lines.map((l) => l.at.reduce((sum, j) => sum + known[j], 0));
501
+ if (typed.sums)
502
+ typed.sums.forEach((sum, i) => {
503
+ if (typeof sum === "number" && totals[i] !== sum)
504
+ problems.push(`sums[${i}]: ${f.lines[i].name} adds up to ${totals[i]} by facts.values, not ${sum}`);
505
+ });
506
+ else
507
+ totals.forEach((t, i) => {
508
+ if (t !== totals[0])
509
+ problems.push(`facts.values: ${f.lines[i].name} adds up to ${t}, ${f.lines[0].name} to ${totals[0]}; with no sums, every line has one sum`);
510
+ });
511
+ if (typed.pool) {
512
+ const left = typed.pool.filter((c) => typeof c === "number");
513
+ typed.slots.forEach((e, i) => {
514
+ if (numberOf(e) !== undefined)
515
+ return;
516
+ const k = left.indexOf(known[i]);
517
+ if (k < 0)
518
+ problems.push(`facts.values[${i}]: ${known[i]} goes in an open slot, but no card of the pool is left with it`);
519
+ else
520
+ left.splice(k, 1);
521
+ });
522
+ }
523
+ },
524
+ draw(scene, pen) {
525
+ const s = scene;
526
+ const f = figureOf(s);
527
+ const look = lookOf(s);
528
+ const half = look === "cell" ? cell / 2 : radius;
529
+ const box = {
530
+ x0: Number.POSITIVE_INFINITY,
531
+ y0: Number.POSITIVE_INFINITY,
532
+ x1: Number.NEGATIVE_INFINITY,
533
+ y1: Number.NEGATIVE_INFINITY,
534
+ };
535
+ const grow = (x, y, rx, ry) => {
536
+ box.x0 = Math.min(box.x0, x - rx);
537
+ box.y0 = Math.min(box.y0, y - ry);
538
+ box.x1 = Math.max(box.x1, x + rx);
539
+ box.y1 = Math.max(box.y1, y + ry);
540
+ };
541
+ for (const p of f.slots)
542
+ grow(p.x, p.y, half, half);
543
+ const sub = pen.sub();
544
+ for (const [a, b] of f.edges)
545
+ sub.el("path", {
546
+ d: `M${n(a.x)} ${n(a.y)}L${n(b.x)} ${n(b.y)}`,
547
+ stroke: ink,
548
+ "stroke-width": 3,
549
+ "stroke-linecap": "round",
550
+ });
551
+ if (look === "cell") {
552
+ // every fill first, then every outline, so that shared sides are one line
553
+ s.slots.forEach((e, i) => {
554
+ const p = f.slots[i];
555
+ sub.el("rect", {
556
+ class: "slot",
557
+ x: p.x - cell / 2,
558
+ y: p.y - cell / 2,
559
+ width: cell,
560
+ height: cell,
561
+ fill: colors[contentOf(e).fill ?? "white"].fill,
562
+ });
563
+ });
564
+ for (const p of f.slots)
565
+ sub.el("rect", {
566
+ x: p.x - cell / 2,
567
+ y: p.y - cell / 2,
568
+ width: cell,
569
+ height: cell,
570
+ fill: "none",
571
+ stroke: ink,
572
+ "stroke-width": outline,
573
+ "stroke-linejoin": "round",
574
+ });
575
+ s.slots.forEach((e, i) => {
576
+ drawContent(sub, e, f.slots[i].x, f.slots[i].y, cell);
577
+ });
578
+ }
579
+ else
580
+ s.slots.forEach((e, i) => {
581
+ drawSlot(sub, e, f.slots[i], look);
582
+ });
583
+ s.sums?.forEach((sum, i) => {
584
+ const text = sumText(sum);
585
+ if (text === undefined)
586
+ return;
587
+ const w = tagWidth(text);
588
+ const at = tagAt(f.lines[i], w, sub);
589
+ drawTag(sub, at.x, at.y, text);
590
+ grow(at.x, at.y, w / 2, tagHeight / 2);
591
+ });
592
+ if (s.pool) {
593
+ const k = s.pool.length;
594
+ const step = cardWidth + 10;
595
+ const mid = (box.x0 + box.x1) / 2;
596
+ const y = box.y1 + 24 + cardHeight / 2;
597
+ s.pool.forEach((card, i) => {
598
+ drawCard(sub, mid + (i - (k - 1) / 2) * step, y, plainText(card));
599
+ });
600
+ grow(mid, y, ((k - 1) * step + cardWidth) / 2, cardHeight / 2);
601
+ }
602
+ pen.place(sub, margin - box.x0, margin - box.y0);
603
+ return { width: box.x1 - box.x0 + 2 * margin, height: box.y1 - box.y0 + 2 * margin };
604
+ },
605
+ describe(scene) {
606
+ const s = scene;
607
+ const f = figureOf(s);
608
+ const look = lookOf(s);
609
+ const noun = look === "cell" ? "cell" : look === "box" ? "box" : "circle";
610
+ const nouns = noun === "box" ? "boxes" : `${noun}s`;
611
+ const count = f.slots.length;
612
+ const say = (i) => sayEntry(s.slots[i]);
613
+ const read = (l) => `${l.name[0].toUpperCase()}${l.name.slice(1)}, ${l.from}: ${l.at.map(say).join(", ")}.`;
614
+ const out = [];
615
+ switch (s.shape) {
616
+ case "triangle":
617
+ case "square": {
618
+ const p = s.perSide ?? 3;
619
+ const what = s.shape === "triangle" ? "A triangle" : "A square";
620
+ out.push(s.centre
621
+ ? `${what} of ${plural(count, noun, nouns)}: ${p} on each side, each corner ${noun} on two sides, and 1 more in the middle, on the three lines that join each corner to the middle of the side across.`
622
+ : `${what} of ${plural(count, noun, nouns)}, ${p} on each side; each corner ${noun} is on two sides.`);
623
+ out.push(...f.lines.map(read));
624
+ break;
625
+ }
626
+ case "star": {
627
+ const k = s.points ?? 5;
628
+ out.push(`A ${k === 5 ? "five" : "six"}-pointed star of ${count} ${nouns}: ${k} at its points and ${k} where its lines cross. Clockwise round the star from the top point, a point and then a crossing in turn, they hold: ${s.slots.map((_, i) => say(i)).join(", ")}.`);
629
+ out.push(`Each of its ${k} straight lines runs through 4 of them: ${f.lines
630
+ .map((l) => sayList(l.at.map((j) => ordinal(j + 1))))
631
+ .join("; ")}.`);
632
+ break;
633
+ }
634
+ case "wheel": {
635
+ const k = s.lines ?? 3;
636
+ const per = s.perArm ?? 1;
637
+ out.push(`A wheel of ${plural(count, noun, nouns)}: 1 in the middle and ${k} straight lines through it, with ${per} ${per === 1 ? noun : nouns} on each side of the middle on every line. The middle ${noun}, on every line, holds ${say(0)}.`);
638
+ out.push(...f.lines.map(read));
639
+ break;
640
+ }
641
+ case "cross": {
642
+ const a = s.arm ?? 1;
643
+ out.push(`A cross of ${count} cells: a row of ${2 * a + 1} and a column of ${2 * a + 1} that share the middle cell.`);
644
+ out.push(...f.lines.map(read));
645
+ break;
646
+ }
647
+ case "grid": {
648
+ const rows = s.rows ?? 3;
649
+ const columns = s.columns ?? 3;
650
+ out.push(`A grid of cells, ${plural(rows, "row")} of ${columns}.`);
651
+ out.push(...f.lines.slice(0, rows).map(read));
652
+ break;
653
+ }
654
+ }
655
+ if (s.sums?.some((sum) => sum !== null))
656
+ out.push(`Tags at the ends of the lines show numbers: ${sayList(s.sums.flatMap((sum, i) => {
657
+ const text = sumText(sum);
658
+ return text === undefined ? [] : [`${text} at ${f.lines[i].name}`];
659
+ }))}.`);
660
+ if (s.pool)
661
+ out.push(`Below are number cards: ${sayList(s.pool.map((c) => sayText(String(c))))}.`);
662
+ return out.join(" ");
663
+ },
664
+ };
@@ -0,0 +1,32 @@
1
+ import type { Kind } from "../kind.ts";
2
+ import { type ColorName } from "../style.ts";
3
+ import { type Thing } from "../things.ts";
4
+ /**
5
+ * A map: places at the crossings of a grid, each a dot, a labelled circle or
6
+ * a thing (an island, a house), joined by roads — or, on the sea, by sea
7
+ * routes — that may carry a distance and an arrow. A route through the roads
8
+ * may be highlighted. Every place and every road of the scene is drawn once,
9
+ * whole, and no road passes over a place it does not join.
10
+ */
11
+ export type Look = "dot" | "circle" | Thing;
12
+ export type Place = {
13
+ id: string;
14
+ at: [number, number];
15
+ look?: Look;
16
+ label?: string;
17
+ color?: ColorName;
18
+ };
19
+ export type Road = {
20
+ from: string;
21
+ to: string;
22
+ label?: string;
23
+ arrow?: boolean;
24
+ };
25
+ export type MapScene = {
26
+ kind: "map";
27
+ places: Place[];
28
+ roads: Road[];
29
+ route?: string[];
30
+ sea?: boolean;
31
+ };
32
+ export declare const map: Kind;