squiffy-compiler 7.0.0-beta.4 → 7.0.0-beta.6

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 (43) hide show
  1. package/README.md +10 -2
  2. package/dist/analyse.d.ts +3 -0
  3. package/dist/analyse.js +73 -12
  4. package/dist/compiler.d.ts +12 -2
  5. package/dist/compiler.js +6 -2
  6. package/dist/emit.d.ts +6 -0
  7. package/dist/emit.js +17 -1
  8. package/dist/language-service.js +36 -5
  9. package/dist/node.d.ts +8 -1
  10. package/dist/node.js +19 -5
  11. package/dist/parse.js +18 -2
  12. package/dist/project.d.ts +56 -0
  13. package/dist/project.js +159 -17
  14. package/dist/quest/aslx.d.ts +50 -0
  15. package/dist/quest/aslx.js +181 -0
  16. package/dist/quest/convert.d.ts +20 -0
  17. package/dist/quest/convert.js +475 -0
  18. package/dist/quest/script.d.ts +92 -0
  19. package/dist/quest/script.js +453 -0
  20. package/dist/quest/scripts.d.ts +24 -0
  21. package/dist/quest/scripts.js +501 -0
  22. package/dist/quest/text.d.ts +17 -0
  23. package/dist/quest/text.js +305 -0
  24. package/dist/setters.d.ts +6 -0
  25. package/dist/setters.js +43 -0
  26. package/dist/story-graph.d.ts +3 -1
  27. package/dist/story-graph.js +12 -3
  28. package/dist/syntax.d.ts +22 -2
  29. package/dist/text.d.ts +4 -0
  30. package/dist/text.js +28 -21
  31. package/dist/twine/convert.d.ts +82 -0
  32. package/dist/twine/convert.js +744 -0
  33. package/dist/twine/expression.d.ts +68 -0
  34. package/dist/twine/expression.js +215 -0
  35. package/dist/twine/harlowe.d.ts +18 -0
  36. package/dist/twine/harlowe.js +1912 -0
  37. package/dist/twine/story.d.ts +24 -0
  38. package/dist/twine/story.js +231 -0
  39. package/dist/twine/sugarcube.d.ts +34 -0
  40. package/dist/twine/sugarcube.js +1621 -0
  41. package/dist/v5.js +2 -1
  42. package/package.json +3 -2
  43. package/syntaxes/squiffy.tmLanguage.json +1 -1
@@ -0,0 +1,501 @@
1
+ // Converts a gamebook's scripts into Squiffy text. A page's script runs before its text is shown,
2
+ // so it becomes text at the start of its section: msg becomes text, flags and counters become
3
+ // {{set}}, {{inc}} and {{dec}}, if becomes {{#if}}, and MovePlayer becomes {{goto}}. The helpers
4
+ // run in order as the text is shown, so the page's text and links see what the script changed.
5
+ import { display, print, quote, randomInteger, Unconvertible } from "../twine/expression.js";
6
+ import { sectionLink, urlLink } from "../twine/convert.js";
7
+ import { convertQuestText, objectAttribute } from "./text.js";
8
+ // The attribute that a GetInput callback's result goes in
9
+ export const INPUT_ATTRIBUTE = "input";
10
+ export function convertScript(statements, ctx) {
11
+ const converter = new Converter(ctx);
12
+ const lines = converter.statements(statements, true);
13
+ return { directives: converter.directives, lines };
14
+ }
15
+ class Converter {
16
+ constructor(ctx) {
17
+ Object.defineProperty(this, "ctx", {
18
+ enumerable: true,
19
+ configurable: true,
20
+ writable: true,
21
+ value: ctx
22
+ });
23
+ Object.defineProperty(this, "directives", {
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true,
27
+ value: []
28
+ });
29
+ // Whether anything has been shown yet, so a ClearScreen can be @clear
30
+ Object.defineProperty(this, "shown", {
31
+ enumerable: true,
32
+ configurable: true,
33
+ writable: true,
34
+ value: false
35
+ });
36
+ }
37
+ statements(statements, topLevel) {
38
+ const lines = [];
39
+ for (const s of statements) {
40
+ try {
41
+ lines.push(...this.statement(s, topLevel));
42
+ }
43
+ catch (e) {
44
+ if (!(e instanceof Unconvertible))
45
+ throw e;
46
+ lines.push(this.ctx.unconvertible(s.src, `This couldn't be converted: ${e.message}.`));
47
+ }
48
+ }
49
+ return lines;
50
+ }
51
+ show(lines) {
52
+ this.shown = true;
53
+ return lines;
54
+ }
55
+ statement(s, topLevel) {
56
+ switch (s.k) {
57
+ case "raw":
58
+ throw new Unconvertible("Squiffy couldn't read this Quest script");
59
+ case "if": {
60
+ // Each part is converted first, so if one can't be, the whole if is kept as it was
61
+ const conditions = s.branches.map(b => print(this.expr(b.cond)));
62
+ const lines = [];
63
+ s.branches.forEach((b, i) => {
64
+ lines.push(i == 0 ? `{{#if ${conditions[i]}}}` : `{{else if ${conditions[i]}}}`);
65
+ lines.push(...this.statements(b.body, false));
66
+ });
67
+ if (s.otherwise)
68
+ lines.push("{{else}}", ...this.statements(s.otherwise, false));
69
+ lines.push("{{/if}}");
70
+ return lines;
71
+ }
72
+ case "firsttime": {
73
+ // At the top of a page's script, it's the first time the page is shown. Anywhere
74
+ // else, an attribute remembers whether it's run.
75
+ const lines = [];
76
+ if (topLevel && this.ctx.page !== undefined && !this.ctx.inCallback) {
77
+ lines.push(`{{#if (visits ${quote(this.ctx.section)}) = 1}}`, ...this.statements(s.body, false));
78
+ }
79
+ else {
80
+ const attribute = this.ctx.newFirstTime();
81
+ lines.push(`{{#unless ${attribute}}}`, `{{set ${quote(attribute)} true}}`, ...this.statements(s.body, false));
82
+ }
83
+ if (s.otherwise)
84
+ lines.push("{{else}}", ...this.statements(s.otherwise, false));
85
+ lines.push(topLevel && this.ctx.page !== undefined && !this.ctx.inCallback ? "{{/if}}" : "{{/unless}}");
86
+ return lines;
87
+ }
88
+ case "switch": {
89
+ const value = this.expr(s.value);
90
+ const conditions = s.cases.map(c => print(c.values.map(v => ({ k: "bin", op: "=", l: value, r: this.expr(v) }))
91
+ .reduce((a, b) => ({ k: "bin", op: "or", l: a, r: b }))));
92
+ if (!s.cases.length)
93
+ return s.otherwise ? this.statements(s.otherwise, false) : [];
94
+ const lines = [];
95
+ s.cases.forEach((c, i) => {
96
+ lines.push(i == 0 ? `{{#if ${conditions[i]}}}` : `{{else if ${conditions[i]}}}`);
97
+ lines.push(...this.statements(c.body, false));
98
+ });
99
+ if (s.otherwise)
100
+ lines.push("{{else}}", ...this.statements(s.otherwise, false));
101
+ lines.push("{{/if}}");
102
+ return lines;
103
+ }
104
+ case "assign":
105
+ return this.assign(s.target, s.value);
106
+ case "call":
107
+ return this.call(s, topLevel);
108
+ }
109
+ }
110
+ assign(target, value) {
111
+ if (target.k != "path")
112
+ throw new Unconvertible("an assignment to something that isn't an attribute");
113
+ const parts = target.parts;
114
+ if (parts.length == 2 && parts[0] == "player" && parts[1] == "parent")
115
+ return this.goto(value);
116
+ if (parts.length == 1 && parts[0] == "this")
117
+ throw new Unconvertible("this isn't an attribute");
118
+ return [`{{set ${quote(this.attributeFor(parts))} ${print(this.expr(value))}}}`];
119
+ }
120
+ // The Squiffy attribute for a Quest attribute, or a variable in the script
121
+ attributeFor(parts) {
122
+ if (parts.length == 1) {
123
+ if (this.ctx.inCallback && parts[0] == "result")
124
+ return INPUT_ATTRIBUTE;
125
+ if (this.ctx.isPage(parts[0]) || parts[0] == "game" || parts[0] == "player")
126
+ throw new Unconvertible(`${parts[0]} isn't an attribute`);
127
+ return this.ctx.attribute(parts[0]);
128
+ }
129
+ if (parts.length == 2) {
130
+ const [object, attribute] = parts;
131
+ if (object == "game")
132
+ return this.ctx.attribute(attribute);
133
+ if (object == "player" || this.ctx.isPage(object))
134
+ return this.ctx.attribute(objectAttribute(object, attribute));
135
+ throw new Unconvertible(`there's nothing called "${object}" in the game`);
136
+ }
137
+ throw new Unconvertible(`Squiffy can't read ${parts.join(".")}`);
138
+ }
139
+ // Moving to a page. In Quest, moving to the page the player is already on does nothing, so the
140
+ // script that runs on every page only goes somewhere if it's somewhere else.
141
+ goto(page) {
142
+ let section;
143
+ // A page kept in an attribute, such as game.next = Page2, is the name of its section
144
+ if (page.k == "path" && page.parts.length > 1 && page.parts.join(".") != "player.parent") {
145
+ section = this.expr(page);
146
+ }
147
+ else {
148
+ const name = this.page(page);
149
+ const target = this.ctx.target(name);
150
+ if (!target)
151
+ throw new Unconvertible(`there's no page called "${name}"`);
152
+ if ("url" in target)
153
+ throw new Unconvertible("moving to a link page");
154
+ if (name == this.ctx.page)
155
+ return [];
156
+ section = { k: "str", v: target.section };
157
+ }
158
+ const goto = `{{goto ${print(section)}}}`;
159
+ if (this.ctx.page !== undefined && section.k == "str")
160
+ return [goto];
161
+ return [`{{#unless ${print({ k: "call", fn: "at", args: [section] })}}}${goto}{{/unless}}`];
162
+ }
163
+ // The name of the page an expression refers to: Page2, player.parent, or GetObject ("Page2")
164
+ page(e) {
165
+ if (e.k == "path" && e.parts.length == 1 && this.ctx.isPage(e.parts[0]))
166
+ return e.parts[0];
167
+ if (e.k == "path" && e.parts.join(".") == "player.parent" && this.ctx.page !== undefined)
168
+ return this.ctx.page;
169
+ // In a page's script, this is the page
170
+ if (e.k == "path" && e.parts.length == 1 && e.parts[0] == "this" && this.ctx.page !== undefined)
171
+ return this.ctx.page;
172
+ if (e.k == "call" && e.fn == "GetObject" && e.args.length == 1 && e.args[0].k == "str" && this.ctx.isPage(e.args[0].v))
173
+ return e.args[0].v;
174
+ if (e.k == "path" && e.parts.length == 1)
175
+ throw new Unconvertible(`there's no page called "${e.parts[0]}"`);
176
+ throw new Unconvertible("a page that's worked out as the game runs");
177
+ }
178
+ // A string argument that has to be fixed text, such as a flag's name
179
+ text(e, what) {
180
+ if (e?.k != "str" || !e.v.trim())
181
+ throw new Unconvertible(`${what} that isn't fixed text`);
182
+ return e.v;
183
+ }
184
+ call(s, topLevel) {
185
+ const ctx = this.ctx;
186
+ const args = s.args;
187
+ const set = (name, value) => [`{{set ${quote(ctx.attribute(name))} ${value}}}`];
188
+ switch (s.fn) {
189
+ case "msg":
190
+ case "OutputText":
191
+ case "OutputTextNoBr":
192
+ return this.show([this.output(args[0])]);
193
+ case "PrintCentered":
194
+ // A span, so its text can have Markdown in it, as a div can't
195
+ return this.show([`<span style="display: block; text-align: center">${this.output(args[0])}</span>`]);
196
+ case "SetFlagOn":
197
+ return set(this.text(args[0], "a flag"), "true");
198
+ case "SetFlagOff":
199
+ return set(this.text(args[0], "a flag"), "false");
200
+ case "SetCounter":
201
+ return set(this.text(args[0], "a counter"), print(this.expr(args[1])));
202
+ case "IncreaseCounter":
203
+ return [`{{inc ${quote(ctx.attribute(this.text(args[0], "a counter")))}}}`];
204
+ case "DecreaseCounter":
205
+ return [`{{dec ${quote(ctx.attribute(this.text(args[0], "a counter")))}}}`];
206
+ case "ChangeCounter":
207
+ return [`{{inc ${quote(ctx.attribute(this.text(args[0], "a counter")))} ${print(this.expr(args[1]))}}}`];
208
+ case "set": {
209
+ if (args.length != 3 || args[0].k != "path" || args[0].parts.length != 1)
210
+ throw new Unconvertible("set with something other than an object, a name and a value");
211
+ return this.assign({ k: "path", parts: [args[0].parts[0], this.text(args[1], "an attribute's name")] }, args[2]);
212
+ }
213
+ case "MovePlayer":
214
+ if (args.length != 1)
215
+ throw new Unconvertible("MovePlayer with no page");
216
+ return this.goto(args[0]);
217
+ case "AddPageLink":
218
+ case "RemovePageLink": {
219
+ if (args.length != (s.fn == "AddPageLink" ? 3 : 2))
220
+ throw new Unconvertible(`${s.fn} with the wrong number of values`);
221
+ const source = this.page(args[0]);
222
+ const destination = this.page(args[1]);
223
+ const attribute = ctx.linkAttribute(source, destination);
224
+ const value = s.fn == "AddPageLink" ? print(ctx.linkValue(source, destination, this.text(args[2], "a link's text"))) : "false";
225
+ return [`{{set ${quote(attribute)} ${value}}}`];
226
+ }
227
+ case "ClearScreen":
228
+ if (topLevel && !this.shown && !ctx.inCallback) {
229
+ if (!this.directives.includes("@clear"))
230
+ this.directives.push("@clear");
231
+ return [];
232
+ }
233
+ throw new Unconvertible("Squiffy can only clear the screen before a section is shown");
234
+ case "on ready":
235
+ // Waits for anything still going on, such as a sound, before running the rest
236
+ return this.statements(s.callback ?? [], topLevel);
237
+ case "picture": {
238
+ // Quest shows nothing for no picture, or no sound
239
+ if (args[0]?.k == "str" && !args[0].v.trim())
240
+ return [];
241
+ const file = this.text(args[0], "a picture");
242
+ ctx.asset(file);
243
+ return this.show([`<img src="${escapeAttribute(file)}" alt="">`]);
244
+ }
245
+ case "play sound": {
246
+ if (args[0]?.k == "str" && !args[0].v.trim())
247
+ return [];
248
+ const file = this.text(args[0], "a sound");
249
+ ctx.asset(file);
250
+ const loop = args[2]?.k == "bool" && args[2].v ? " loop" : "";
251
+ return [`<audio src="${escapeAttribute(file)}" autoplay${loop}></audio>`];
252
+ }
253
+ case "stop sound":
254
+ throw new Unconvertible("Squiffy can't stop a sound");
255
+ case "ShowYouTube":
256
+ return this.show([`<iframe class="quest-youtube" width="560" height="315" src="https://www.youtube.com/embed/${encodeURIComponent(this.text(args[0], "a video"))}" allowfullscreen></iframe>`]);
257
+ case "DisplayHttpLink": {
258
+ // As Quest: the address's scheme is replaced with http or https
259
+ const address = this.text(args[1], "a web address").replace(/^.*?:\/\//, "");
260
+ const secure = args[2]?.k == "bool" && args[2].v;
261
+ return this.show([urlLink(this.output(args[0]), `${secure ? "https" : "http"}://${address}`)]);
262
+ }
263
+ case "TextFX_Typewriter":
264
+ case "TextFX_Unscramble": {
265
+ if (s.fn == "TextFX_Unscramble")
266
+ ctx.approximate(s.src, "Squiffy can't unscramble text, so it's typed out instead.");
267
+ const speed = args[1]?.k == "num" ? ` interval=${args[1].v}` : "";
268
+ return this.show([`{{#animate "typewriter"${speed}}}${this.output(args[0])}{{/animate}}`]);
269
+ }
270
+ case "SetBackgroundColour":
271
+ return this.style(`--squiffy-background: ${cssValue(this.text(args[0], "a colour"))};`);
272
+ case "SetForegroundColour":
273
+ return this.style(`--squiffy-text: ${cssValue(this.text(args[0], "a colour"))};`);
274
+ case "SetLinkColour":
275
+ return this.style(`--squiffy-link: ${cssValue(this.text(args[0], "a colour"))}; --squiffy-link-hover: ${cssValue(this.text(args[0], "a colour"))};`);
276
+ case "SetFontName":
277
+ return this.style(`--squiffy-font-family: ${cssValue(this.text(args[0], "a font"))};`);
278
+ case "SetWebFontName": {
279
+ const font = this.text(args[0], "a font");
280
+ const url = `https://fonts.googleapis.com/css2?family=${encodeURIComponent(font).replace(/%20/g, "+")}&display=swap`;
281
+ return this.style(`--squiffy-font-family: "${font.replace(/["\\<>{};]/g, "")}";`, `@import url("${url}");`);
282
+ }
283
+ case "SetFontSize": {
284
+ const size = this.expr(args[0]);
285
+ if (size.k != "num")
286
+ throw new Unconvertible("a font size that isn't a number");
287
+ return this.style(`--squiffy-font-size: ${size.v}pt;`);
288
+ }
289
+ case "GetInput": {
290
+ // The rest of the script runs in a section of its own, which shows the page's links
291
+ // again, as they're still there in Quest
292
+ if (!ctx.continueSection || ctx.inCallback)
293
+ throw new Unconvertible("asking for input here");
294
+ const callback = convertScript(s.callback ?? [], { ...ctx, inCallback: true });
295
+ if (callback.directives.length)
296
+ throw new Unconvertible("clearing the screen after asking for input");
297
+ const section = ctx.continueSection(callback.lines.join("\n"));
298
+ return this.show([`<input type="text" data-attribute="${INPUT_ATTRIBUTE}">`, sectionLink("Continue", section)]);
299
+ }
300
+ }
301
+ if (s.fn.startsWith("JS"))
302
+ throw new Unconvertible("Squiffy can't run Quest's JavaScript");
303
+ throw new Unconvertible(`Squiffy has nothing like ${s.fn}`);
304
+ }
305
+ // A change to how the story looks, as a stylesheet in the story's text, which applies from
306
+ // when it's shown
307
+ style(rule, imports = "") {
308
+ this.ctx.noteStyle();
309
+ return [`<style>${imports}:root { color-scheme: light; ${rule} }</style>`];
310
+ }
311
+ // The text a msg shows: Quest's text processor runs on it, and the values joined to it are
312
+ // shown as they are
313
+ output(e) {
314
+ if (!e)
315
+ return "";
316
+ const parts = [];
317
+ const flatten = (x) => {
318
+ if (x.k == "bin" && x.op == "+" && hasText(x)) {
319
+ flatten(x.l);
320
+ flatten(x.r);
321
+ }
322
+ else {
323
+ parts.push(x);
324
+ }
325
+ };
326
+ flatten(e);
327
+ return parts.map(p => p.k == "str"
328
+ ? convertQuestText(p.v, this.ctx, "block").replace(/\n+/g, m => m.length > 1 ? "<br><br>" : "<br>")
329
+ : display(this.expr(p))).join("");
330
+ }
331
+ expr(e) {
332
+ switch (e.k) {
333
+ case "num": return { k: "num", v: e.v };
334
+ case "str": return { k: "str", v: e.v };
335
+ case "bool": return { k: "bool", v: e.v };
336
+ case "null": return { k: "null" };
337
+ case "not": return { k: "not", e: this.expr(e.e) };
338
+ case "neg": return { k: "neg", e: this.expr(e.e) };
339
+ case "path": {
340
+ const parts = e.parts;
341
+ if (parts.length == 2 && this.ctx.isPage(parts[0]) && parts[1] == "visited")
342
+ return this.seen(parts[0]);
343
+ // A page, or the current page, as a value is its section's name
344
+ if (parts.join(".") == "player.parent") {
345
+ return this.ctx.page === undefined ? { k: "var", name: "_section" } : { k: "str", v: this.ctx.section };
346
+ }
347
+ if ((parts.length == 1 && this.ctx.isPage(parts[0])) || (parts.length == 1 && parts[0] == "this" && this.ctx.page !== undefined)) {
348
+ const target = this.ctx.target(this.page(e));
349
+ if (!target || !("section" in target))
350
+ throw new Unconvertible(`${parts[0]} isn't a page in the story`);
351
+ return { k: "str", v: target.section };
352
+ }
353
+ return { k: "var", name: this.attributeFor(parts) };
354
+ }
355
+ case "bin": {
356
+ // player.parent = Page2: whether the player is on that page
357
+ for (const [a, b] of [[e.l, e.r], [e.r, e.l]]) {
358
+ if ((e.op == "=" || e.op == "<>") && a.k == "path" && a.parts.join(".") == "player.parent") {
359
+ const target = this.ctx.target(this.page(b));
360
+ if (!target || !("section" in target))
361
+ throw new Unconvertible("comparing the current page with something that isn't a page");
362
+ const at = { k: "call", fn: "at", args: [{ k: "str", v: target.section }] };
363
+ return e.op == "=" ? at : { k: "not", e: at };
364
+ }
365
+ }
366
+ const ops = {
367
+ "or": "or", "and": "and", "=": "=", "<>": "!=", "<": "<", ">": ">", "<=": "<=", ">=": ">=",
368
+ "+": "+", "-": "-", "*": "*", "/": "/", "%": "%",
369
+ };
370
+ const op = ops[e.op];
371
+ if (!op)
372
+ throw new Unconvertible(`Squiffy has nothing like ${e.op}`);
373
+ return { k: "bin", op, l: this.expr(e.l), r: this.expr(e.r) };
374
+ }
375
+ case "call":
376
+ return this.callExpr(e);
377
+ }
378
+ }
379
+ seen(page) {
380
+ const target = this.ctx.target(page);
381
+ if (!target || !("section" in target))
382
+ throw new Unconvertible(`"${page}" isn't a page in the story`);
383
+ return { k: "call", fn: "seen", args: [{ k: "str", v: target.section }] };
384
+ }
385
+ callExpr(e) {
386
+ const args = e.args;
387
+ switch (e.fn) {
388
+ case "GetBoolean":
389
+ case "GetInt":
390
+ case "GetDouble":
391
+ case "GetString":
392
+ case "GetAttribute": {
393
+ if (args.length != 2 || args[0].k != "path" || args[0].parts.length != 1)
394
+ throw new Unconvertible(`${e.fn} of something other than an object`);
395
+ const object = args[0].parts[0];
396
+ const attribute = this.text(args[1], "an attribute's name");
397
+ if (attribute == "visited" && this.ctx.isPage(object))
398
+ return this.seen(object);
399
+ return { k: "var", name: this.attributeFor([object, attribute]) };
400
+ }
401
+ case "HasSeenPage":
402
+ if (args.length != 1)
403
+ throw new Unconvertible("HasSeenPage with no page");
404
+ return this.seen(this.page(args[0]));
405
+ case "RandomChance": {
406
+ const chance = args[0] ? this.expr(args[0]) : undefined;
407
+ if (chance?.k != "num")
408
+ throw new Unconvertible("a random chance that isn't a number");
409
+ return randomChance(chance.v);
410
+ }
411
+ case "GetRandomInt":
412
+ if (args.length != 2)
413
+ throw new Unconvertible("GetRandomInt without two numbers");
414
+ return randomInteger(this.expr(args[0]), this.expr(args[1]));
415
+ case "GetFileURL": {
416
+ const file = this.text(args[0], "a file");
417
+ this.ctx.asset(file);
418
+ return { k: "str", v: file };
419
+ }
420
+ case "ToString":
421
+ case "ToInt":
422
+ case "ToDouble":
423
+ if (args.length != 1)
424
+ throw new Unconvertible(`${e.fn} without one value`);
425
+ return this.expr(args[0]);
426
+ }
427
+ throw new Unconvertible(`Squiffy has nothing like ${e.fn}`);
428
+ }
429
+ }
430
+ function hasText(e) {
431
+ if (e.k == "str")
432
+ return true;
433
+ if (e.k == "bin" && e.op == "+")
434
+ return hasText(e.l) || hasText(e.r);
435
+ return false;
436
+ }
437
+ // RandomChance(35): true 35% of the time, as a random pick from 1 to 20 being 7 or less
438
+ function randomChance(percent) {
439
+ if (percent <= 0)
440
+ return { k: "bool", v: false };
441
+ if (percent >= 100)
442
+ return { k: "bool", v: true };
443
+ const whole = Math.round(percent);
444
+ const gcd = (a, b) => b == 0 ? a : gcd(b, a % b);
445
+ const divisor = gcd(whole, 100);
446
+ return {
447
+ k: "bin", op: "<=",
448
+ l: randomInteger({ k: "num", v: 1 }, { k: "num", v: 100 / divisor }),
449
+ r: { k: "num", v: whole / divisor },
450
+ };
451
+ }
452
+ function escapeAttribute(s) {
453
+ return s.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/\[/g, "&#91;").replace(/\]/g, "&#93;");
454
+ }
455
+ function cssValue(value) {
456
+ return value.replace(/[;{}<>]/g, "").trim();
457
+ }
458
+ // The pages a script adds links to or removes them from, with each link's text, so the pages can
459
+ // show them depending on an attribute
460
+ export function* linkChanges(statements, currentPage, isPage) {
461
+ const page = (e) => {
462
+ if (e?.k == "path" && e.parts.length == 1 && isPage(e.parts[0]))
463
+ return e.parts[0];
464
+ if (e?.k == "path" && (e.parts.join(".") == "player.parent" || e.parts.join(".") == "this"))
465
+ return currentPage;
466
+ if (e?.k == "call" && e.fn == "GetObject" && e.args[0]?.k == "str" && isPage(e.args[0].v))
467
+ return e.args[0].v;
468
+ return undefined;
469
+ };
470
+ for (const s of statements) {
471
+ if (s.k == "call") {
472
+ if ((s.fn == "AddPageLink" && s.args.length == 3) || (s.fn == "RemovePageLink" && s.args.length == 2)) {
473
+ const source = page(s.args[0]);
474
+ const destination = page(s.args[1]);
475
+ if (source !== undefined && destination !== undefined) {
476
+ const text = s.fn == "AddPageLink" && s.args[2].k == "str" ? s.args[2].v : undefined;
477
+ yield { source, destination, text };
478
+ }
479
+ }
480
+ if (s.callback)
481
+ yield* linkChanges(s.callback, currentPage, isPage);
482
+ }
483
+ else if (s.k == "if") {
484
+ for (const b of s.branches)
485
+ yield* linkChanges(b.body, currentPage, isPage);
486
+ if (s.otherwise)
487
+ yield* linkChanges(s.otherwise, currentPage, isPage);
488
+ }
489
+ else if (s.k == "firsttime") {
490
+ yield* linkChanges(s.body, currentPage, isPage);
491
+ if (s.otherwise)
492
+ yield* linkChanges(s.otherwise, currentPage, isPage);
493
+ }
494
+ else if (s.k == "switch") {
495
+ for (const c of s.cases)
496
+ yield* linkChanges(c.body, currentPage, isPage);
497
+ if (s.otherwise)
498
+ yield* linkChanges(s.otherwise, currentPage, isPage);
499
+ }
500
+ }
501
+ }
@@ -0,0 +1,17 @@
1
+ export interface TextContext {
2
+ readonly section: string;
3
+ target(page: string): {
4
+ section: string;
5
+ } | {
6
+ url: string;
7
+ } | undefined;
8
+ attribute(name: string): string;
9
+ unconvertible(text: string, reason: string): string;
10
+ approximate(text: string, reason: string): void;
11
+ addPassage?(nameHint: string, content: string): string;
12
+ asset(file: string): void;
13
+ }
14
+ export type TextMode = "block" | "inline";
15
+ export declare function convertQuestText(text: string, ctx: TextContext, mode: TextMode): string;
16
+ export declare function objectAttribute(object: string, attribute: string): string;
17
+ export declare function escapeLineStarts(text: string): string;