@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.
- package/LICENSE +201 -0
- package/NOTICE +7 -0
- package/README.md +27 -2
- package/lib/emoji.d.ts +16 -0
- package/lib/emoji.js +64 -0
- package/lib/index.d.ts +9 -0
- package/lib/index.js +10 -0
- package/lib/kind.d.ts +22 -0
- package/lib/kind.js +2 -0
- package/lib/kinds/balance.d.ts +34 -0
- package/lib/kinds/balance.js +217 -0
- package/lib/kinds/branches.d.ts +33 -0
- package/lib/kinds/branches.js +473 -0
- package/lib/kinds/calendar.d.ts +55 -0
- package/lib/kinds/calendar.js +532 -0
- package/lib/kinds/chart.d.ts +48 -0
- package/lib/kinds/chart.js +645 -0
- package/lib/kinds/choices.d.ts +14 -0
- package/lib/kinds/choices.js +53 -0
- package/lib/kinds/clock.d.ts +36 -0
- package/lib/kinds/clock.js +219 -0
- package/lib/kinds/coins.d.ts +37 -0
- package/lib/kinds/coins.js +550 -0
- package/lib/kinds/column.d.ts +37 -0
- package/lib/kinds/column.js +410 -0
- package/lib/kinds/commands-parts.d.ts +12 -0
- package/lib/kinds/commands-parts.js +24 -0
- package/lib/kinds/commands.d.ts +66 -0
- package/lib/kinds/commands.js +789 -0
- package/lib/kinds/containers.d.ts +52 -0
- package/lib/kinds/containers.js +758 -0
- package/lib/kinds/countfig.d.ts +41 -0
- package/lib/kinds/countfig.js +372 -0
- package/lib/kinds/cubes.d.ts +70 -0
- package/lib/kinds/cubes.js +371 -0
- package/lib/kinds/die.d.ts +32 -0
- package/lib/kinds/die.js +301 -0
- package/lib/kinds/domino.d.ts +37 -0
- package/lib/kinds/domino.js +450 -0
- package/lib/kinds/dotgrid.d.ts +38 -0
- package/lib/kinds/dotgrid.js +411 -0
- package/lib/kinds/figure.d.ts +62 -0
- package/lib/kinds/figure.js +687 -0
- package/lib/kinds/fold-parts.d.ts +73 -0
- package/lib/kinds/fold-parts.js +315 -0
- package/lib/kinds/fold.d.ts +52 -0
- package/lib/kinds/fold.js +650 -0
- package/lib/kinds/fraction.d.ts +50 -0
- package/lib/kinds/fraction.js +568 -0
- package/lib/kinds/gauges.d.ts +53 -0
- package/lib/kinds/gauges.js +880 -0
- package/lib/kinds/gears.d.ts +40 -0
- package/lib/kinds/gears.js +787 -0
- package/lib/kinds/grid.d.ts +45 -0
- package/lib/kinds/grid.js +591 -0
- package/lib/kinds/groups.d.ts +23 -0
- package/lib/kinds/groups.js +335 -0
- package/lib/kinds/index.d.ts +3 -0
- package/lib/kinds/index.js +87 -0
- package/lib/kinds/lanes.d.ts +36 -0
- package/lib/kinds/lanes.js +220 -0
- package/lib/kinds/layers.d.ts +31 -0
- package/lib/kinds/layers.js +419 -0
- package/lib/kinds/line.d.ts +38 -0
- package/lib/kinds/line.js +739 -0
- package/lib/kinds/magic-parts.d.ts +72 -0
- package/lib/kinds/magic-parts.js +186 -0
- package/lib/kinds/magic.d.ts +66 -0
- package/lib/kinds/magic.js +664 -0
- package/lib/kinds/map.d.ts +32 -0
- package/lib/kinds/map.js +370 -0
- package/lib/kinds/matchsticks.d.ts +26 -0
- package/lib/kinds/matchsticks.js +434 -0
- package/lib/kinds/necklace.d.ts +27 -0
- package/lib/kinds/necklace.js +342 -0
- package/lib/kinds/net.d.ts +47 -0
- package/lib/kinds/net.js +241 -0
- package/lib/kinds/overlay.d.ts +50 -0
- package/lib/kinds/overlay.js +519 -0
- package/lib/kinds/pieces.d.ts +41 -0
- package/lib/kinds/pieces.js +420 -0
- package/lib/kinds/pipes.d.ts +46 -0
- package/lib/kinds/pipes.js +657 -0
- package/lib/kinds/plane-parts.d.ts +105 -0
- package/lib/kinds/plane-parts.js +372 -0
- package/lib/kinds/plane.d.ts +35 -0
- package/lib/kinds/plane.js +755 -0
- package/lib/kinds/pyramid.d.ts +26 -0
- package/lib/kinds/pyramid.js +228 -0
- package/lib/kinds/ropes.d.ts +30 -0
- package/lib/kinds/ropes.js +571 -0
- package/lib/kinds/shapes.d.ts +59 -0
- package/lib/kinds/shapes.js +490 -0
- package/lib/kinds/solids.d.ts +78 -0
- package/lib/kinds/solids.js +1058 -0
- package/lib/kinds/tangram.d.ts +52 -0
- package/lib/kinds/tangram.js +713 -0
- package/lib/kinds/target.d.ts +39 -0
- package/lib/kinds/target.js +761 -0
- package/lib/kinds/tiles.d.ts +55 -0
- package/lib/kinds/tiles.js +606 -0
- package/lib/kinds/venn.d.ts +38 -0
- package/lib/kinds/venn.js +710 -0
- package/lib/noto.d.ts +2 -0
- package/lib/noto.js +212 -0
- package/lib/objects.d.ts +7 -0
- package/lib/objects.js +226 -0
- package/lib/pen.d.ts +32 -0
- package/lib/pen.js +60 -0
- package/lib/read.d.ts +9 -0
- package/lib/read.js +49 -0
- package/lib/render.d.ts +24 -0
- package/lib/render.js +68 -0
- package/lib/solve/balance.d.ts +46 -0
- package/lib/solve/balance.js +118 -0
- package/lib/style.d.ts +70 -0
- package/lib/style.js +39 -0
- package/lib/svg.d.ts +22 -0
- package/lib/svg.js +48 -0
- package/lib/things.d.ts +40 -0
- package/lib/things.js +269 -0
- package/package.json +28 -4
|
@@ -0,0 +1,789 @@
|
|
|
1
|
+
import { margin } from "../kind.js";
|
|
2
|
+
import { isObject, objects } from "../objects.js";
|
|
3
|
+
import { int, isObj, list, oneOf, only, text } from "../read.js";
|
|
4
|
+
import { colors, ground, ink, outline, soft } from "../style.js";
|
|
5
|
+
import { n, poly } from "../svg.js";
|
|
6
|
+
import { checkThing, drawThing, sameThing, wordOf } from "../things.js";
|
|
7
|
+
import { smallText } from "./commands-parts.js";
|
|
8
|
+
export const facings = ["up", "right", "down", "left"];
|
|
9
|
+
/** arrows move one cell the way they point, whatever way the walker faces. */
|
|
10
|
+
export const arrows = ["up", "down", "left", "right"];
|
|
11
|
+
/** blocks act from the way the walker faces. */
|
|
12
|
+
export const blocks = ["step", "jump", "turn-left", "turn-right"];
|
|
13
|
+
const tile = 44;
|
|
14
|
+
const gap = 6;
|
|
15
|
+
const pad = 7;
|
|
16
|
+
const badge = 38;
|
|
17
|
+
const diamond = 50;
|
|
18
|
+
/** ifThing is the size of the thing an if checks for, in its diamond. */
|
|
19
|
+
const ifThing = 27;
|
|
20
|
+
const mark = 22;
|
|
21
|
+
const lineGap = 12;
|
|
22
|
+
/** lead is the room the walker takes before a program that has no board. */
|
|
23
|
+
const lead = tile + 14;
|
|
24
|
+
/** wide is the widest the contents go: about 640 px less the margins. */
|
|
25
|
+
const wide = 616;
|
|
26
|
+
const mostCommands = 24;
|
|
27
|
+
const mostInRepeat = 6;
|
|
28
|
+
const mostInBranch = 4;
|
|
29
|
+
const rock = "nature.rock";
|
|
30
|
+
const unit = {
|
|
31
|
+
up: { x: 0, y: -1 },
|
|
32
|
+
right: { x: 1, y: 0 },
|
|
33
|
+
down: { x: 0, y: 1 },
|
|
34
|
+
left: { x: -1, y: 0 },
|
|
35
|
+
};
|
|
36
|
+
function isAsk(value) {
|
|
37
|
+
return isObj(value) && value.ask === true;
|
|
38
|
+
}
|
|
39
|
+
function isArrow(c) {
|
|
40
|
+
return typeof c === "string" && arrows.includes(c);
|
|
41
|
+
}
|
|
42
|
+
function isBlock(c) {
|
|
43
|
+
return typeof c === "string" && blocks.includes(c);
|
|
44
|
+
}
|
|
45
|
+
/** each visits every command of a program, those inside repeats and ifs too. */
|
|
46
|
+
function each(cmds, visit) {
|
|
47
|
+
for (const c of cmds) {
|
|
48
|
+
visit(c);
|
|
49
|
+
if (typeof c === "string" || isAsk(c))
|
|
50
|
+
continue;
|
|
51
|
+
if ("repeat" in c)
|
|
52
|
+
each(c.do, visit);
|
|
53
|
+
else {
|
|
54
|
+
each(c.yes, visit);
|
|
55
|
+
if (c.no)
|
|
56
|
+
each(c.no, visit);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/** measure is the size a command's tile, or a block's frame, takes in the strip. */
|
|
61
|
+
function measure(c) {
|
|
62
|
+
if (typeof c === "string" || isAsk(c))
|
|
63
|
+
return { w: tile, h: tile };
|
|
64
|
+
if ("repeat" in c) {
|
|
65
|
+
const row = measureRow(c.do);
|
|
66
|
+
return { w: pad + badge + gap + row.w + pad, h: Math.max(row.h, badge) + 2 * pad };
|
|
67
|
+
}
|
|
68
|
+
const yes = measureRow(c.yes);
|
|
69
|
+
const no = c.no ? measureRow(c.no) : undefined;
|
|
70
|
+
const rows = no ? yes.h + gap + no.h : yes.h;
|
|
71
|
+
return {
|
|
72
|
+
w: pad + diamond + gap + mark + gap + Math.max(yes.w, no?.w ?? 0) + pad,
|
|
73
|
+
h: Math.max(rows, diamond) + 2 * pad,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
function measureRow(cmds) {
|
|
77
|
+
const boxes = cmds.map(measure);
|
|
78
|
+
return {
|
|
79
|
+
w: boxes.reduce((sum, b) => sum + b.w, 0) + gap * (boxes.length - 1),
|
|
80
|
+
h: Math.max(...boxes.map((b) => b.h)),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/** lay breaks a program into lines no wider than width, never inside a block. */
|
|
84
|
+
function lay(cmds, width, first = 0) {
|
|
85
|
+
const lines = [];
|
|
86
|
+
let line = { items: [], w: first, h: tile };
|
|
87
|
+
for (const c of cmds) {
|
|
88
|
+
const box = measure(c);
|
|
89
|
+
const w = line.items.length === 0 ? line.w + box.w : line.w + gap + box.w;
|
|
90
|
+
if (line.items.length > 0 && w > width) {
|
|
91
|
+
lines.push(line);
|
|
92
|
+
line = { items: [{ c, box }], w: box.w, h: box.h };
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
line.items.push({ c, box });
|
|
96
|
+
line.w = w;
|
|
97
|
+
line.h = Math.max(line.h, box.h);
|
|
98
|
+
}
|
|
99
|
+
lines.push(line);
|
|
100
|
+
return lines;
|
|
101
|
+
}
|
|
102
|
+
/** layEven breaks a program into as few lines as width allows, made as even as they can be. */
|
|
103
|
+
function layEven(cmds, width, first = 0) {
|
|
104
|
+
const lines = lay(cmds, width, first);
|
|
105
|
+
if (lines.length === 1)
|
|
106
|
+
return lines;
|
|
107
|
+
let low = Math.max(first + (cmds[0] ? measure(cmds[0]).w : 0), ...cmds.map((c) => measure(c).w));
|
|
108
|
+
let high = width;
|
|
109
|
+
while (high - low > 1) {
|
|
110
|
+
const mid = Math.floor((low + high) / 2);
|
|
111
|
+
if (lay(cmds, mid, first).length > lines.length)
|
|
112
|
+
low = mid;
|
|
113
|
+
else
|
|
114
|
+
high = mid;
|
|
115
|
+
}
|
|
116
|
+
return lay(cmds, high, first);
|
|
117
|
+
}
|
|
118
|
+
function widthOf(lines) {
|
|
119
|
+
return Math.max(...lines.map((l) => l.w));
|
|
120
|
+
}
|
|
121
|
+
function heightOf(lines) {
|
|
122
|
+
return lines.reduce((sum, l) => sum + l.h, 0) + lineGap * (lines.length - 1);
|
|
123
|
+
}
|
|
124
|
+
function frame(pen, x, y, w, h, fill, r) {
|
|
125
|
+
pen.el("rect", { x, y, width: w, height: h, rx: r, fill, stroke: ink, "stroke-width": outline });
|
|
126
|
+
}
|
|
127
|
+
function turned(points, dir, c) {
|
|
128
|
+
const quarter = { up: 0, right: 1, down: 2, left: 3 }[dir];
|
|
129
|
+
return points.map((p) => {
|
|
130
|
+
let { x, y } = p;
|
|
131
|
+
for (let i = 0; i < quarter; i++)
|
|
132
|
+
[x, y] = [-y, x];
|
|
133
|
+
return { x: c.x + x, y: c.y + y };
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
/** arrowGlyph is a fat arrow pointing one way, centred at c. */
|
|
137
|
+
function arrowGlyph(pen, dir, c, fill) {
|
|
138
|
+
const up = [
|
|
139
|
+
{ x: 0, y: -15 },
|
|
140
|
+
{ x: 12.5, y: -1.5 },
|
|
141
|
+
{ x: 4.8, y: -1.5 },
|
|
142
|
+
{ x: 4.8, y: 15 },
|
|
143
|
+
{ x: -4.8, y: 15 },
|
|
144
|
+
{ x: -4.8, y: -1.5 },
|
|
145
|
+
{ x: -12.5, y: -1.5 },
|
|
146
|
+
];
|
|
147
|
+
pen.el("path", {
|
|
148
|
+
d: poly(turned(up, dir, c)),
|
|
149
|
+
fill,
|
|
150
|
+
stroke: ink,
|
|
151
|
+
"stroke-width": 2,
|
|
152
|
+
"stroke-linejoin": "round",
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
/** thick draws a line of a colour with an ink edge, as a felt pen would. */
|
|
156
|
+
function thick(pen, d, fill, width = 5.5) {
|
|
157
|
+
for (const [stroke, w] of [
|
|
158
|
+
[ink, width + 4],
|
|
159
|
+
[fill, width],
|
|
160
|
+
])
|
|
161
|
+
pen.el("path", {
|
|
162
|
+
d,
|
|
163
|
+
fill: "none",
|
|
164
|
+
stroke,
|
|
165
|
+
"stroke-width": w,
|
|
166
|
+
"stroke-linecap": "round",
|
|
167
|
+
"stroke-linejoin": "round",
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
function head(pen, tip, dir, fill, size = 9.5) {
|
|
171
|
+
const len = Math.hypot(dir.x, dir.y) || 1;
|
|
172
|
+
const u = { x: dir.x / len, y: dir.y / len };
|
|
173
|
+
const back = { x: tip.x - u.x * size * 1.15, y: tip.y - u.y * size * 1.15 };
|
|
174
|
+
pen.el("path", {
|
|
175
|
+
d: poly([
|
|
176
|
+
tip,
|
|
177
|
+
{ x: back.x - u.y * size, y: back.y + u.x * size },
|
|
178
|
+
{ x: back.x + u.y * size, y: back.y - u.x * size },
|
|
179
|
+
]),
|
|
180
|
+
fill,
|
|
181
|
+
stroke: ink,
|
|
182
|
+
"stroke-width": 2,
|
|
183
|
+
"stroke-linejoin": "round",
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
/** turnGlyph is an arrow that goes up and bends to one side. */
|
|
187
|
+
function turnGlyph(pen, side, c) {
|
|
188
|
+
const x = (v) => n(c.x + side * v);
|
|
189
|
+
const fill = colors.orange.fill;
|
|
190
|
+
thick(pen, `M${x(-7)} ${n(c.y + 14)}L${x(-7)} ${n(c.y + 1)}Q${x(-7)} ${n(c.y - 9)} ${x(3)} ${n(c.y - 9)}L${x(5)} ${n(c.y - 9)}`, fill);
|
|
191
|
+
head(pen, { x: c.x + side * 16, y: c.y - 9 }, { x: side, y: 0 }, fill);
|
|
192
|
+
}
|
|
193
|
+
/** jumpGlyph is an arrow that hops over a little bar. */
|
|
194
|
+
function jumpGlyph(pen, c) {
|
|
195
|
+
const fill = colors.purple.fill;
|
|
196
|
+
pen.el("rect", {
|
|
197
|
+
x: c.x - 5,
|
|
198
|
+
y: c.y + 4,
|
|
199
|
+
width: 8,
|
|
200
|
+
height: 11,
|
|
201
|
+
rx: 2,
|
|
202
|
+
fill: colors.gray.fill,
|
|
203
|
+
stroke: ink,
|
|
204
|
+
"stroke-width": 2,
|
|
205
|
+
});
|
|
206
|
+
thick(pen, `M${n(c.x - 14)} ${n(c.y + 14)}C${n(c.x - 14)} ${n(c.y - 17)} ${n(c.x + 13)} ${n(c.y - 17)} ${n(c.x + 13)} ${n(c.y + 3)}`, fill, 5);
|
|
207
|
+
head(pen, { x: c.x + 13, y: c.y + 16 }, { x: 0, y: 1 }, fill, 8);
|
|
208
|
+
}
|
|
209
|
+
function drawTile(pen, c, x, cy) {
|
|
210
|
+
const centre = { x: x + tile / 2, y: cy };
|
|
211
|
+
const ask = isAsk(c);
|
|
212
|
+
pen.el("rect", {
|
|
213
|
+
x,
|
|
214
|
+
y: cy - tile / 2,
|
|
215
|
+
width: tile,
|
|
216
|
+
height: tile,
|
|
217
|
+
rx: 9,
|
|
218
|
+
fill: ground,
|
|
219
|
+
stroke: ink,
|
|
220
|
+
"stroke-width": outline,
|
|
221
|
+
"stroke-dasharray": ask ? "5 4" : undefined,
|
|
222
|
+
class: "command",
|
|
223
|
+
});
|
|
224
|
+
if (ask)
|
|
225
|
+
pen.text(centre.x, cy, "?", { size: 26 });
|
|
226
|
+
else if (c === "step")
|
|
227
|
+
arrowGlyph(pen, "up", centre, colors.green.fill);
|
|
228
|
+
else if (c === "turn-left")
|
|
229
|
+
turnGlyph(pen, -1, centre);
|
|
230
|
+
else if (c === "turn-right")
|
|
231
|
+
turnGlyph(pen, 1, centre);
|
|
232
|
+
else if (c === "jump")
|
|
233
|
+
jumpGlyph(pen, centre);
|
|
234
|
+
else
|
|
235
|
+
arrowGlyph(pen, c, centre, colors.blue.fill);
|
|
236
|
+
}
|
|
237
|
+
/** loopBadge is a round arrow with the count of a repeat inside it. */
|
|
238
|
+
function loopBadge(pen, c, times) {
|
|
239
|
+
const r = 15;
|
|
240
|
+
const at = (deg) => ({
|
|
241
|
+
x: c.x + r * Math.cos((deg * Math.PI) / 180),
|
|
242
|
+
y: c.y + r * Math.sin((deg * Math.PI) / 180),
|
|
243
|
+
});
|
|
244
|
+
const a = at(292);
|
|
245
|
+
const b = at(218);
|
|
246
|
+
thick(pen, `M${n(a.x)} ${n(a.y)}A${r} ${r} 0 1 1 ${n(b.x)} ${n(b.y)}`, colors.orange.fill, 3.5);
|
|
247
|
+
const t = (218 * Math.PI) / 180;
|
|
248
|
+
head(pen, at(234), { x: -Math.sin(t), y: Math.cos(t) }, colors.orange.fill, 6.5);
|
|
249
|
+
if (isAsk(times)) {
|
|
250
|
+
pen.el("circle", {
|
|
251
|
+
cx: c.x,
|
|
252
|
+
cy: c.y,
|
|
253
|
+
r: 9.5,
|
|
254
|
+
fill: ground,
|
|
255
|
+
stroke: ink,
|
|
256
|
+
"stroke-width": 1.8,
|
|
257
|
+
"stroke-dasharray": "3 2.5",
|
|
258
|
+
});
|
|
259
|
+
pen.text(c.x, c.y, "?", { size: 14 });
|
|
260
|
+
}
|
|
261
|
+
else
|
|
262
|
+
pen.text(c.x, c.y + 1, String(times), { size: 17 });
|
|
263
|
+
}
|
|
264
|
+
function yesNo(pen, c, yes) {
|
|
265
|
+
if (yes)
|
|
266
|
+
thick(pen, `M${n(c.x - 7)} ${n(c.y + 1)}L${n(c.x - 2)} ${n(c.y + 6)}L${n(c.x + 7)} ${n(c.y - 6)}`, colors.green.fill, 3.5);
|
|
267
|
+
else
|
|
268
|
+
thick(pen, `M${n(c.x - 6)} ${n(c.y - 6)}L${n(c.x + 6)} ${n(c.y + 6)}M${n(c.x + 6)} ${n(c.y - 6)}L${n(c.x - 6)} ${n(c.y + 6)}`, colors.red.fill, 3.5);
|
|
269
|
+
}
|
|
270
|
+
function drawRow(pen, cmds, x, cy) {
|
|
271
|
+
for (const c of cmds) {
|
|
272
|
+
const box = measure(c);
|
|
273
|
+
drawCommand(pen, c, x, cy, box);
|
|
274
|
+
x += box.w + gap;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
function drawCommand(pen, c, x, cy, box) {
|
|
278
|
+
if (typeof c === "string" || isAsk(c)) {
|
|
279
|
+
drawTile(pen, c, x, cy);
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
const top = cy - box.h / 2;
|
|
283
|
+
if ("repeat" in c) {
|
|
284
|
+
pen.add('<g class="repeat">');
|
|
285
|
+
frame(pen, x, top, box.w, box.h, colors.yellow.fill, 12);
|
|
286
|
+
pen.add("</g>");
|
|
287
|
+
loopBadge(pen, { x: x + pad + badge / 2, y: cy }, c.repeat);
|
|
288
|
+
drawRow(pen, c.do, x + pad + badge + gap, cy);
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
pen.add('<g class="if">');
|
|
292
|
+
frame(pen, x, top, box.w, box.h, soft, 12);
|
|
293
|
+
pen.add("</g>");
|
|
294
|
+
const dc = { x: x + pad + diamond / 2, y: cy };
|
|
295
|
+
pen.el("path", {
|
|
296
|
+
d: poly([
|
|
297
|
+
{ x: dc.x, y: dc.y - diamond / 2 },
|
|
298
|
+
{ x: dc.x + diamond / 2, y: dc.y },
|
|
299
|
+
{ x: dc.x, y: dc.y + diamond / 2 },
|
|
300
|
+
{ x: dc.x - diamond / 2, y: dc.y },
|
|
301
|
+
]),
|
|
302
|
+
fill: ground,
|
|
303
|
+
stroke: ink,
|
|
304
|
+
"stroke-width": outline,
|
|
305
|
+
"stroke-linejoin": "round",
|
|
306
|
+
});
|
|
307
|
+
drawThing(pen, c.if, dc.x, dc.y, ifThing);
|
|
308
|
+
const yes = measureRow(c.yes);
|
|
309
|
+
const no = c.no ? measureRow(c.no) : undefined;
|
|
310
|
+
const rows = no ? yes.h + gap + no.h : yes.h;
|
|
311
|
+
const yesY = cy - rows / 2 + yes.h / 2;
|
|
312
|
+
const markX = x + pad + diamond + gap + mark / 2;
|
|
313
|
+
const rowX = x + pad + diamond + gap + mark + gap;
|
|
314
|
+
yesNo(pen, { x: markX, y: yesY }, true);
|
|
315
|
+
drawRow(pen, c.yes, rowX, yesY);
|
|
316
|
+
if (c.no && no) {
|
|
317
|
+
const noY = cy + rows / 2 - no.h / 2;
|
|
318
|
+
yesNo(pen, { x: markX, y: noY }, false);
|
|
319
|
+
drawRow(pen, c.no, rowX, noY);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
/** pointer is a small arrowhead on the side the walker faces. */
|
|
323
|
+
function pointer(pen, c, facing, reach, size) {
|
|
324
|
+
const u = unit[facing];
|
|
325
|
+
const tip = { x: c.x + u.x * reach, y: c.y + u.y * reach };
|
|
326
|
+
const back = { x: tip.x - u.x * size, y: tip.y - u.y * size };
|
|
327
|
+
pen.el("path", {
|
|
328
|
+
d: poly([
|
|
329
|
+
tip,
|
|
330
|
+
{ x: back.x - u.y * size * 0.85, y: back.y + u.x * size * 0.85 },
|
|
331
|
+
{ x: back.x + u.y * size * 0.85, y: back.y - u.x * size * 0.85 },
|
|
332
|
+
]),
|
|
333
|
+
fill: colors.orange.fill,
|
|
334
|
+
stroke: ink,
|
|
335
|
+
"stroke-width": 2,
|
|
336
|
+
"stroke-linejoin": "round",
|
|
337
|
+
class: "facing",
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
function cellSize(board) {
|
|
341
|
+
return Math.min(56, Math.floor(400 / Math.max(board.rows, board.cols)));
|
|
342
|
+
}
|
|
343
|
+
/** thingSize is the size a thing on a board is drawn at. */
|
|
344
|
+
function thingSize(board) {
|
|
345
|
+
return cellSize(board) * 0.7;
|
|
346
|
+
}
|
|
347
|
+
function name(at) {
|
|
348
|
+
return `row ${at[0]}, column ${at[1]}`;
|
|
349
|
+
}
|
|
350
|
+
function key(at) {
|
|
351
|
+
return `${at[0]},${at[1]}`;
|
|
352
|
+
}
|
|
353
|
+
function walkerOf(s) {
|
|
354
|
+
return s.walker ?? "toy.robot";
|
|
355
|
+
}
|
|
356
|
+
/** drawBoard draws the board with its top left at x, y. */
|
|
357
|
+
function drawBoard(pen, s, board, x, y) {
|
|
358
|
+
const c = cellSize(board);
|
|
359
|
+
const w = board.cols * c;
|
|
360
|
+
const h = board.rows * c;
|
|
361
|
+
pen.el("rect", { x, y, width: w, height: h, fill: ground });
|
|
362
|
+
for (let r = 1; r < board.rows; r++)
|
|
363
|
+
pen.el("path", { d: `M${n(x)} ${n(y + r * c)}H${n(x + w)}`, stroke: ink, "stroke-width": 1.6 });
|
|
364
|
+
for (let k = 1; k < board.cols; k++)
|
|
365
|
+
pen.el("path", { d: `M${n(x + k * c)} ${n(y)}V${n(y + h)}`, stroke: ink, "stroke-width": 1.6 });
|
|
366
|
+
pen.el("rect", {
|
|
367
|
+
x,
|
|
368
|
+
y,
|
|
369
|
+
width: w,
|
|
370
|
+
height: h,
|
|
371
|
+
fill: "none",
|
|
372
|
+
stroke: ink,
|
|
373
|
+
"stroke-width": outline + 0.5,
|
|
374
|
+
});
|
|
375
|
+
const centre = (at) => ({ x: x + (at[1] - 0.5) * c, y: y + (at[0] - 0.5) * c });
|
|
376
|
+
const start = s.start?.at;
|
|
377
|
+
if (board.path && start) {
|
|
378
|
+
const points = [start, ...board.path].map(centre);
|
|
379
|
+
const end = points[points.length - 1];
|
|
380
|
+
const prev = points[points.length - 2];
|
|
381
|
+
const dir = { x: end.x - prev.x, y: end.y - prev.y };
|
|
382
|
+
const len = Math.hypot(dir.x, dir.y);
|
|
383
|
+
const last = key(board.path[board.path.length - 1]);
|
|
384
|
+
const filled = [board.target, ...(board.things ?? []), ...(board.marks ?? [])].some((spot) => spot !== undefined && key(spot.at) === last);
|
|
385
|
+
const back = filled ? 0.32 * c : 0.12 * c;
|
|
386
|
+
const tip = { x: end.x - (dir.x / len) * back, y: end.y - (dir.y / len) * back };
|
|
387
|
+
const shaft = { x: tip.x - (dir.x / len) * 9, y: tip.y - (dir.y / len) * 9 };
|
|
388
|
+
pen.add('<g class="path">');
|
|
389
|
+
thick(pen, poly([...points.slice(0, -1), shaft], false), colors.red.fill, 4.5);
|
|
390
|
+
head(pen, tip, dir, colors.red.fill, 8);
|
|
391
|
+
pen.add("</g>");
|
|
392
|
+
}
|
|
393
|
+
const size = thingSize(board);
|
|
394
|
+
const place = (at, thing, what) => {
|
|
395
|
+
const p = centre(at);
|
|
396
|
+
pen.add(`<g class="${what}">`);
|
|
397
|
+
drawThing(pen, thing, p.x, p.y, size);
|
|
398
|
+
pen.add("</g>");
|
|
399
|
+
};
|
|
400
|
+
for (const o of board.obstacles ?? [])
|
|
401
|
+
place(o.at, o.thing ?? rock, "obstacle");
|
|
402
|
+
for (const t of board.things ?? [])
|
|
403
|
+
place(t.at, t.thing, "thing");
|
|
404
|
+
if (board.target)
|
|
405
|
+
place(board.target.at, board.target.thing, "target");
|
|
406
|
+
for (const m of board.marks ?? []) {
|
|
407
|
+
const p = centre(m.at);
|
|
408
|
+
pen.add('<g class="mark">');
|
|
409
|
+
pen.text(p.x, p.y, m.label, { size: Math.round(c * 0.44) });
|
|
410
|
+
pen.add("</g>");
|
|
411
|
+
}
|
|
412
|
+
if (start) {
|
|
413
|
+
const p = centre(start);
|
|
414
|
+
const facing = s.start?.facing;
|
|
415
|
+
const u = facing ? unit[facing] : { x: 0, y: 0 };
|
|
416
|
+
const code = objects[walkerOf(s)]?.code ?? "";
|
|
417
|
+
pen.emoji(code, p.x - u.x * c * 0.07, p.y - u.y * c * 0.07, c * (facing ? 0.62 : 0.7));
|
|
418
|
+
if (facing)
|
|
419
|
+
pointer(pen, p, facing, c * 0.47, c * 0.17);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
/** startTile shows the walker before a program that has no board. */
|
|
423
|
+
function startTile(pen, s, x, cy) {
|
|
424
|
+
const code = objects[walkerOf(s)]?.code ?? "";
|
|
425
|
+
const facing = s.start?.facing;
|
|
426
|
+
const c = { x: x + tile / 2, y: cy };
|
|
427
|
+
const u = facing ? unit[facing] : { x: 0, y: 0 };
|
|
428
|
+
pen.emoji(code, c.x - u.x * 3, c.y - u.y * 3, 36);
|
|
429
|
+
if (facing)
|
|
430
|
+
pointer(pen, c, facing, 25, 8);
|
|
431
|
+
}
|
|
432
|
+
function leads(s) {
|
|
433
|
+
return !s.board && (s.walker !== undefined || s.start?.facing !== undefined);
|
|
434
|
+
}
|
|
435
|
+
/** layout places the board and the strip: side by side when that fits, else the strip below. */
|
|
436
|
+
function layout(s) {
|
|
437
|
+
const cmds = s.commands ?? [];
|
|
438
|
+
if (!s.board) {
|
|
439
|
+
const lead_ = leads(s);
|
|
440
|
+
const lines = layEven(cmds, wide, lead_ ? lead : 0);
|
|
441
|
+
return {
|
|
442
|
+
strip: { x: margin, y: margin, lines, lead: lead_ },
|
|
443
|
+
width: widthOf(lines) + 2 * margin,
|
|
444
|
+
height: heightOf(lines) + 2 * margin,
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
const c = cellSize(s.board);
|
|
448
|
+
const bw = s.board.cols * c;
|
|
449
|
+
const bh = s.board.rows * c;
|
|
450
|
+
if (cmds.length === 0)
|
|
451
|
+
return { board: { x: margin, y: margin }, width: bw + 2 * margin, height: bh + 2 * margin };
|
|
452
|
+
const room = wide - bw - 28;
|
|
453
|
+
const beside = layEven(cmds, room);
|
|
454
|
+
if (widthOf(beside) <= room && heightOf(beside) <= bh) {
|
|
455
|
+
return {
|
|
456
|
+
board: { x: margin, y: margin },
|
|
457
|
+
strip: {
|
|
458
|
+
x: margin + bw + 28,
|
|
459
|
+
y: margin + (bh - heightOf(beside)) / 2,
|
|
460
|
+
lines: beside,
|
|
461
|
+
lead: false,
|
|
462
|
+
},
|
|
463
|
+
width: bw + 28 + widthOf(beside) + 2 * margin,
|
|
464
|
+
height: bh + 2 * margin,
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
const lines = layEven(cmds, wide);
|
|
468
|
+
const sw = widthOf(lines);
|
|
469
|
+
const width = Math.max(bw, sw);
|
|
470
|
+
return {
|
|
471
|
+
board: { x: margin + (width - bw) / 2, y: margin },
|
|
472
|
+
strip: { x: margin + (width - sw) / 2, y: margin + bh + 22, lines, lead: false },
|
|
473
|
+
width: width + 2 * margin,
|
|
474
|
+
height: bh + 22 + heightOf(lines) + 2 * margin,
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
function checkAt(value, path, problems, board) {
|
|
478
|
+
if (!Array.isArray(value) || value.length !== 2) {
|
|
479
|
+
problems.push(`${path}: must be [row, column], counted from 1 at the top left`);
|
|
480
|
+
return undefined;
|
|
481
|
+
}
|
|
482
|
+
const before = problems.length;
|
|
483
|
+
const r = int(value[0], `${path}[0]`, problems, 1, board.rows);
|
|
484
|
+
const c = int(value[1], `${path}[1]`, problems, 1, board.cols);
|
|
485
|
+
return problems.length > before ? undefined : [r, c];
|
|
486
|
+
}
|
|
487
|
+
function checkCommands(value, path, problems, depth, most) {
|
|
488
|
+
list(value, path, problems, 1, most).forEach((c, i) => {
|
|
489
|
+
checkCommand(c, `${path}[${i}]`, problems, depth);
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
function checkCommand(c, path, problems, depth) {
|
|
493
|
+
const moves = [...arrows, ...blocks];
|
|
494
|
+
if (typeof c === "string") {
|
|
495
|
+
if (!moves.includes(c))
|
|
496
|
+
problems.push(`${path}: "${c}" is not a command: ${moves.join(", ")}, {"ask": true}, a repeat or an if`);
|
|
497
|
+
return;
|
|
498
|
+
}
|
|
499
|
+
if (!isObj(c)) {
|
|
500
|
+
problems.push(`${path}: must be a command, {"ask": true}, a repeat or an if`);
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
if ("repeat" in c || "if" in c) {
|
|
504
|
+
if (depth >= 2)
|
|
505
|
+
problems.push(`${path}: a block inside a block holds no block of its own`);
|
|
506
|
+
}
|
|
507
|
+
if ("repeat" in c) {
|
|
508
|
+
only(c, ["repeat", "do"], path, problems);
|
|
509
|
+
if (!isAsk(c.repeat))
|
|
510
|
+
int(c.repeat, `${path}.repeat`, problems, 2, 9);
|
|
511
|
+
checkCommands(c.do, `${path}.do`, problems, depth + 1, mostInRepeat);
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
if ("if" in c) {
|
|
515
|
+
only(c, ["if", "yes", "no"], path, problems);
|
|
516
|
+
const before = problems.length;
|
|
517
|
+
if (isObj(c.if) && c.if.empty === true)
|
|
518
|
+
problems.push(`${path}.if: must be a thing to check, or {"ask": true}`);
|
|
519
|
+
else
|
|
520
|
+
checkThing(c.if, `${path}.if`, problems);
|
|
521
|
+
const small = problems.length > before ? undefined : smallText(c.if, ifThing, `${path}.if`);
|
|
522
|
+
if (small)
|
|
523
|
+
problems.push(`${small}; check for a thing with no text on it`);
|
|
524
|
+
checkCommands(c.yes, `${path}.yes`, problems, depth + 1, mostInBranch);
|
|
525
|
+
if (c.no !== undefined)
|
|
526
|
+
checkCommands(c.no, `${path}.no`, problems, depth + 1, mostInBranch);
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
if ("ask" in c) {
|
|
530
|
+
only(c, ["ask"], path, problems);
|
|
531
|
+
if (c.ask !== true)
|
|
532
|
+
problems.push(`${path}.ask: must be true`);
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
problems.push(`${path}: must be a command, {"ask": true}, a repeat or an if`);
|
|
536
|
+
}
|
|
537
|
+
function checkBoard(b, start, problems) {
|
|
538
|
+
only(b, ["rows", "cols", "target", "obstacles", "things", "marks", "path"], "board", problems);
|
|
539
|
+
const before = problems.length;
|
|
540
|
+
const board = {
|
|
541
|
+
rows: int(b.rows, "board.rows", problems, 1, 8),
|
|
542
|
+
cols: int(b.cols, "board.cols", problems, 1, 8),
|
|
543
|
+
};
|
|
544
|
+
if (problems.length > before)
|
|
545
|
+
return;
|
|
546
|
+
if (board.rows * board.cols < 2)
|
|
547
|
+
problems.push("board: must hold at least 2 cells");
|
|
548
|
+
const size = thingSize(board);
|
|
549
|
+
const readable = (thing, path) => {
|
|
550
|
+
const known = problems.length;
|
|
551
|
+
checkThing(thing, path, problems);
|
|
552
|
+
const small = problems.length > known ? undefined : smallText(thing, size, path);
|
|
553
|
+
if (small)
|
|
554
|
+
problems.push(`${small}; write it on a card of one or two characters instead`);
|
|
555
|
+
};
|
|
556
|
+
const at = isObj(start) ? start.at : undefined;
|
|
557
|
+
const walker = at === undefined ? undefined : checkAt(at, "start.at", problems, board);
|
|
558
|
+
if (at === undefined)
|
|
559
|
+
problems.push("start.at: a board needs the walker's place, [row, column]");
|
|
560
|
+
const taken = new Map();
|
|
561
|
+
if (walker)
|
|
562
|
+
taken.set(key(walker), "the walker");
|
|
563
|
+
const put = (place, path, what) => {
|
|
564
|
+
if (!place)
|
|
565
|
+
return;
|
|
566
|
+
const there = taken.get(key(place));
|
|
567
|
+
if (there)
|
|
568
|
+
problems.push(`${path}: ${name(place)} holds ${there} already; a cell holds one thing`);
|
|
569
|
+
else
|
|
570
|
+
taken.set(key(place), what);
|
|
571
|
+
};
|
|
572
|
+
if (b.target !== undefined) {
|
|
573
|
+
if (!isObj(b.target))
|
|
574
|
+
problems.push("board.target: must be an object with at and thing");
|
|
575
|
+
else {
|
|
576
|
+
only(b.target, ["at", "thing"], "board.target", problems);
|
|
577
|
+
readable(b.target.thing, "board.target.thing");
|
|
578
|
+
put(checkAt(b.target.at, "board.target.at", problems, board), "board.target.at", "the target");
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
for (const member of ["obstacles", "things", "marks"]) {
|
|
582
|
+
if (b[member] === undefined)
|
|
583
|
+
continue;
|
|
584
|
+
list(b[member], `board.${member}`, problems, 1, member === "marks" ? 8 : 20).forEach((o, i) => {
|
|
585
|
+
const path = `board.${member}[${i}]`;
|
|
586
|
+
if (!isObj(o)) {
|
|
587
|
+
problems.push(`${path}: must be an object with at`);
|
|
588
|
+
return;
|
|
589
|
+
}
|
|
590
|
+
if (member === "marks") {
|
|
591
|
+
only(o, ["at", "label"], path, problems);
|
|
592
|
+
if (text(o.label, `${path}.label`, problems, 2) === "?")
|
|
593
|
+
problems.push(`${path}.label: a mark is a letter or a number, not "?"`);
|
|
594
|
+
}
|
|
595
|
+
else {
|
|
596
|
+
only(o, ["at", "thing"], path, problems);
|
|
597
|
+
if (member === "things" || o.thing !== undefined)
|
|
598
|
+
readable(o.thing, `${path}.thing`);
|
|
599
|
+
}
|
|
600
|
+
put(checkAt(o.at, `${path}.at`, problems, board), `${path}.at`, path);
|
|
601
|
+
});
|
|
602
|
+
}
|
|
603
|
+
if (b.path === undefined)
|
|
604
|
+
return;
|
|
605
|
+
const obstacles = new Set((Array.isArray(b.obstacles) ? b.obstacles : [])
|
|
606
|
+
.filter((o) => isObj(o) && Array.isArray(o.at))
|
|
607
|
+
.map((o) => key(o.at)));
|
|
608
|
+
const seen = new Set(walker ? [key(walker)] : []);
|
|
609
|
+
let prev = walker;
|
|
610
|
+
list(b.path, "board.path", problems, 1, 30).forEach((place, i) => {
|
|
611
|
+
const path = `board.path[${i}]`;
|
|
612
|
+
const here = checkAt(place, path, problems, board);
|
|
613
|
+
if (!here) {
|
|
614
|
+
prev = undefined;
|
|
615
|
+
return;
|
|
616
|
+
}
|
|
617
|
+
if (prev && Math.abs(prev[0] - here[0]) + Math.abs(prev[1] - here[1]) !== 1)
|
|
618
|
+
problems.push(`${path}: ${name(here)} is not beside ${name(prev)}; a path goes from a cell to one beside it`);
|
|
619
|
+
if (obstacles.has(key(here)))
|
|
620
|
+
problems.push(`${path}: ${name(here)} holds an obstacle; a path goes round obstacles`);
|
|
621
|
+
if (seen.has(key(here)))
|
|
622
|
+
problems.push(`${path}: the path came to ${name(here)} before; it visits a cell once`);
|
|
623
|
+
seen.add(key(here));
|
|
624
|
+
prev = here;
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
function article(words) {
|
|
628
|
+
return /^(?:uni|use|eu)/i.test(words) ? "a" : /^[aeiou]/i.test(words) ? "an" : "a";
|
|
629
|
+
}
|
|
630
|
+
function capital(words) {
|
|
631
|
+
return `${words[0]?.toUpperCase() ?? ""}${words.slice(1)}`;
|
|
632
|
+
}
|
|
633
|
+
function sayOne(c) {
|
|
634
|
+
if (isAsk(c))
|
|
635
|
+
return "a question mark";
|
|
636
|
+
if (typeof c === "string") {
|
|
637
|
+
if (c === "step")
|
|
638
|
+
return "step forward";
|
|
639
|
+
if (c === "jump")
|
|
640
|
+
return "jump";
|
|
641
|
+
if (c === "turn-left")
|
|
642
|
+
return "turn left";
|
|
643
|
+
if (c === "turn-right")
|
|
644
|
+
return "turn right";
|
|
645
|
+
return `arrow ${c}`;
|
|
646
|
+
}
|
|
647
|
+
if ("repeat" in c)
|
|
648
|
+
return isAsk(c.repeat)
|
|
649
|
+
? `a repeat whose count is a question mark (${sayAll(c.do)})`
|
|
650
|
+
: `repeat ${c.repeat} times (${sayAll(c.do)})`;
|
|
651
|
+
const thing = isAsk(c.if) ? "a question mark" : `${article(wordOf(c.if))} ${wordOf(c.if)}`;
|
|
652
|
+
return `check for ${thing}: if yes (${sayAll(c.yes)}), if no (${c.no ? sayAll(c.no) : "nothing"})`;
|
|
653
|
+
}
|
|
654
|
+
function sayAll(cmds) {
|
|
655
|
+
return cmds.map(sayOne).join(", ");
|
|
656
|
+
}
|
|
657
|
+
/** groupsOf gathers the places of the same thing, in the order the scene lists them. */
|
|
658
|
+
function groupsOf(spots) {
|
|
659
|
+
const out = [];
|
|
660
|
+
for (const spot of spots) {
|
|
661
|
+
const same = out.find((g) => sameThing(g.thing, spot.thing));
|
|
662
|
+
if (same)
|
|
663
|
+
same.at.push(spot.at);
|
|
664
|
+
else
|
|
665
|
+
out.push({ thing: spot.thing, at: [spot.at] });
|
|
666
|
+
}
|
|
667
|
+
return out;
|
|
668
|
+
}
|
|
669
|
+
function sayGroups(spots) {
|
|
670
|
+
return groupsOf(spots)
|
|
671
|
+
.map((g) => {
|
|
672
|
+
const word = wordOf(g.thing, g.at.length);
|
|
673
|
+
return g.at.length === 1
|
|
674
|
+
? `${article(word)} ${word} at ${name(g.at[0])}`
|
|
675
|
+
: `${word} at ${g.at.map(name).join("; ")}`;
|
|
676
|
+
})
|
|
677
|
+
.join("; ");
|
|
678
|
+
}
|
|
679
|
+
export const commands = {
|
|
680
|
+
kind: "commands",
|
|
681
|
+
members: ["kind", "walker", "start", "board", "commands"],
|
|
682
|
+
check(scene, problems) {
|
|
683
|
+
only(scene, this.members, "scene", problems);
|
|
684
|
+
if (scene.walker !== undefined && !isObject(scene.walker))
|
|
685
|
+
problems.push('walker: must be an object of the list, such as "toy.robot", "animal.rabbit" or "animal.turtle"');
|
|
686
|
+
if (scene.board === undefined && scene.commands === undefined)
|
|
687
|
+
problems.push("scene: needs commands, a board, or both");
|
|
688
|
+
if (scene.start !== undefined) {
|
|
689
|
+
if (!isObj(scene.start))
|
|
690
|
+
problems.push("start: must be an object with at, facing or both");
|
|
691
|
+
else {
|
|
692
|
+
only(scene.start, ["at", "facing"], "start", problems);
|
|
693
|
+
if (scene.start.facing !== undefined)
|
|
694
|
+
oneOf(scene.start.facing, facings, "start.facing", problems);
|
|
695
|
+
if (scene.start.at !== undefined && scene.board === undefined)
|
|
696
|
+
problems.push("start.at: only a board has places; add a board or leave at out");
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
if (scene.board !== undefined) {
|
|
700
|
+
if (!isObj(scene.board))
|
|
701
|
+
problems.push("board: must be an object with rows and cols");
|
|
702
|
+
else
|
|
703
|
+
checkBoard(scene.board, scene.start, problems);
|
|
704
|
+
}
|
|
705
|
+
if (scene.commands !== undefined)
|
|
706
|
+
checkCommands(scene.commands, "commands", problems, 0, mostCommands);
|
|
707
|
+
if (problems.length > 0)
|
|
708
|
+
return;
|
|
709
|
+
const s = scene;
|
|
710
|
+
const cmds = s.commands ?? [];
|
|
711
|
+
let tiles = 0;
|
|
712
|
+
let usesArrows = false;
|
|
713
|
+
let usesBlocks = false;
|
|
714
|
+
each(cmds, (c) => {
|
|
715
|
+
if (typeof c === "string" || isAsk(c))
|
|
716
|
+
tiles++;
|
|
717
|
+
if (isArrow(c))
|
|
718
|
+
usesArrows = true;
|
|
719
|
+
if (isBlock(c))
|
|
720
|
+
usesBlocks = true;
|
|
721
|
+
});
|
|
722
|
+
if (tiles > mostCommands)
|
|
723
|
+
problems.push(`commands: at most ${mostCommands} commands in all, those inside blocks counted; there are ${tiles}`);
|
|
724
|
+
if (usesArrows && usesBlocks)
|
|
725
|
+
problems.push("commands: use arrows (up, down, left, right) or blocks (step, jump, turn-left, turn-right), not both");
|
|
726
|
+
if (usesArrows && s.start?.facing !== undefined)
|
|
727
|
+
problems.push("start.facing: arrows move the same way whatever way the walker faces; leave facing out");
|
|
728
|
+
if (usesBlocks && s.board !== undefined && s.start?.facing === undefined)
|
|
729
|
+
problems.push("start.facing: steps and turns need the way the walker faces: up, right, down or left");
|
|
730
|
+
const first = leads(s) ? lead : 0;
|
|
731
|
+
cmds.forEach((c, i) => {
|
|
732
|
+
const w = measure(c).w + (i === 0 ? first : 0);
|
|
733
|
+
if (w > wide)
|
|
734
|
+
problems.push(`commands[${i}]: ${Math.round(w)} px wide, more than ${wide}; put fewer commands in this block`);
|
|
735
|
+
});
|
|
736
|
+
},
|
|
737
|
+
draw(scene, pen) {
|
|
738
|
+
const s = scene;
|
|
739
|
+
const l = layout(s);
|
|
740
|
+
if (s.board && l.board)
|
|
741
|
+
drawBoard(pen, s, s.board, l.board.x, l.board.y);
|
|
742
|
+
const strip = l.strip;
|
|
743
|
+
if (strip) {
|
|
744
|
+
let y = strip.y;
|
|
745
|
+
strip.lines.forEach((line, i) => {
|
|
746
|
+
const cy = y + line.h / 2;
|
|
747
|
+
let x = strip.x;
|
|
748
|
+
if (i === 0 && strip.lead) {
|
|
749
|
+
startTile(pen, s, x, cy);
|
|
750
|
+
x += lead;
|
|
751
|
+
}
|
|
752
|
+
drawRow(pen, line.items.map((it) => it.c), x, cy);
|
|
753
|
+
y += line.h + lineGap;
|
|
754
|
+
});
|
|
755
|
+
}
|
|
756
|
+
return { width: l.width, height: l.height };
|
|
757
|
+
},
|
|
758
|
+
describe(scene) {
|
|
759
|
+
const s = scene;
|
|
760
|
+
const out = [];
|
|
761
|
+
const walker = wordOf(walkerOf(s));
|
|
762
|
+
const facing = s.start?.facing;
|
|
763
|
+
const b = s.board;
|
|
764
|
+
if (b) {
|
|
765
|
+
out.push(`A board of ${b.rows} row${b.rows === 1 ? "" : "s"} and ${b.cols} column${b.cols === 1 ? "" : "s"}.`);
|
|
766
|
+
if (s.start?.at)
|
|
767
|
+
out.push(`The ${walker} stands at ${name(s.start.at)}${facing ? `, facing ${facing}` : ""}.`);
|
|
768
|
+
if (b.target) {
|
|
769
|
+
const word = wordOf(b.target.thing);
|
|
770
|
+
out.push(`The target, ${article(word)} ${word}, is at ${name(b.target.at)}.`);
|
|
771
|
+
}
|
|
772
|
+
if (b.obstacles)
|
|
773
|
+
out.push(`Obstacles: ${sayGroups(b.obstacles.map((o) => ({ at: o.at, thing: o.thing ?? rock })))}.`);
|
|
774
|
+
if (b.things)
|
|
775
|
+
out.push(`${capital(sayGroups(b.things))}.`);
|
|
776
|
+
if (b.marks)
|
|
777
|
+
out.push(`Cells marked with letters: ${b.marks.map((m) => `${m.label} at ${name(m.at)}`).join("; ")}.`);
|
|
778
|
+
if (b.path && s.start?.at) {
|
|
779
|
+
const steps = b.path;
|
|
780
|
+
out.push(`A red line with an arrow goes from the ${walker}${steps.length > 1 ? ` through ${steps.slice(0, -1).map(name).join(", then ")}` : ""} to ${name(steps[steps.length - 1])}.`);
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
else if (leads(s))
|
|
784
|
+
out.push(`The ${walker}${facing ? `, facing ${facing},` : ""} runs the program.`);
|
|
785
|
+
if (s.commands)
|
|
786
|
+
out.push(`The program, read from the left: ${sayAll(s.commands)}.`);
|
|
787
|
+
return out.join(" ");
|
|
788
|
+
},
|
|
789
|
+
};
|