@team-antioch/sc2-adjutant 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. package/README.md +2 -0
  2. package/data/blizzard-baseline.json +3061 -0
  3. package/data/button-hotkeys.json +5722 -0
  4. package/data/game-assets.json +50909 -0
  5. package/data/package-entries.json +32361 -2
  6. package/dist/capture/bank-fixtures.d.ts +13 -0
  7. package/dist/capture/bank-fixtures.js +35 -0
  8. package/dist/capture/bank-snapshots.d.ts +95 -0
  9. package/dist/capture/bank-snapshots.js +302 -0
  10. package/dist/capture/editor-broker-client.d.ts +27 -4
  11. package/dist/capture/editor-broker-client.js +3 -3
  12. package/dist/capture/editor-broker.js +154 -9
  13. package/dist/capture/effect-tour.d.ts +113 -0
  14. package/dist/capture/effect-tour.js +283 -0
  15. package/dist/capture/effect-video.d.ts +74 -0
  16. package/dist/capture/effect-video.js +173 -0
  17. package/dist/capture/exemplar-capture.d.ts +4 -0
  18. package/dist/capture/exemplar-capture.js +4 -1
  19. package/dist/capture/shots.d.ts +6 -1
  20. package/dist/capture/shots.js +1 -1
  21. package/dist/capture/stop-tour.d.ts +3 -1
  22. package/dist/capture/stop-tour.js +1 -1
  23. package/dist/capture/style-pack-captures.d.ts +67 -0
  24. package/dist/capture/style-pack-captures.js +172 -0
  25. package/dist/catalog/stock-catalog.d.ts +8 -0
  26. package/dist/catalog/stock-catalog.js +13 -0
  27. package/dist/cli/capture-exemplars.js +18 -32
  28. package/dist/cli/effect-tour.d.ts +2 -0
  29. package/dist/cli/effect-tour.js +378 -0
  30. package/dist/cli/make-frames.d.ts +1 -0
  31. package/dist/cli/make-frames.js +21 -0
  32. package/dist/cli/sound-mix.d.ts +1 -0
  33. package/dist/cli/sound-mix.js +75 -0
  34. package/dist/cli/study.js +6 -26
  35. package/dist/domain/blueprint-trigger.d.ts +355 -3
  36. package/dist/domain/blueprint-trigger.js +149 -8
  37. package/dist/domain/library-interface.d.ts +50 -29
  38. package/dist/domain/library-interface.js +54 -41
  39. package/dist/domain/mission-blueprint.d.ts +13 -6
  40. package/dist/domain/mission-blueprint.js +6 -0
  41. package/dist/domain/mod-blueprint.d.ts +4 -4
  42. package/dist/host/game-logs.d.ts +63 -0
  43. package/dist/host/game-logs.js +178 -0
  44. package/dist/host/game-ownership.d.ts +37 -0
  45. package/dist/host/game-ownership.js +84 -0
  46. package/dist/host/game-run-report.d.ts +63 -0
  47. package/dist/host/game-run-report.js +119 -0
  48. package/dist/host/known-errors.d.ts +34 -0
  49. package/dist/host/known-errors.js +73 -0
  50. package/dist/init/init-workspace.d.ts +3 -1
  51. package/dist/init/init-workspace.js +10 -1
  52. package/dist/map/convert-mission-triggers.d.ts +115 -7
  53. package/dist/map/convert-mission-triggers.js +527 -47
  54. package/dist/map/editor-changes.d.ts +1 -1
  55. package/dist/map/editor-form.d.ts +5 -0
  56. package/dist/map/editor-form.js +72 -0
  57. package/dist/map/game-packages.d.ts +3 -2
  58. package/dist/map/game-packages.js +5 -2
  59. package/dist/map/kept-references.js +47 -1
  60. package/dist/map/kept-session.js +3 -1
  61. package/dist/map/outline-diff.d.ts +24 -0
  62. package/dist/map/outline-diff.js +95 -0
  63. package/dist/map/outline-trigger.d.ts +7 -0
  64. package/dist/map/outline-trigger.js +29 -20
  65. package/dist/map/ownership-plan.d.ts +6 -2
  66. package/dist/map/ownership-plan.js +1 -1
  67. package/dist/map/package-output.js +13 -8
  68. package/dist/map/tune-kept-triggers.d.ts +7 -0
  69. package/dist/map/tune-kept-triggers.js +33 -6
  70. package/dist/map/write-blueprint-trigger.d.ts +9 -4
  71. package/dist/map/write-blueprint-trigger.js +46 -13
  72. package/dist/map/write-map-package.js +20 -9
  73. package/dist/mcp/capture-jobs.d.ts +2 -0
  74. package/dist/mcp/capture-jobs.js +2 -0
  75. package/dist/mcp/server.d.ts +11 -2
  76. package/dist/mcp/server.js +17 -4
  77. package/dist/mcp/tools/bank-report.d.ts +19 -0
  78. package/dist/mcp/tools/bank-report.js +51 -0
  79. package/dist/mcp/tools/blueprint.js +18 -14
  80. package/dist/mcp/tools/capture.d.ts +4 -0
  81. package/dist/mcp/tools/capture.js +22 -5
  82. package/dist/mcp/tools/catalog.d.ts +1 -1
  83. package/dist/mcp/tools/catalog.js +30 -1
  84. package/dist/mcp/tools/compile.d.ts +1 -1
  85. package/dist/mcp/tools/compile.js +100 -9
  86. package/dist/mcp/tools/editor-handoff.d.ts +4 -1
  87. package/dist/mcp/tools/editor-handoff.js +6 -3
  88. package/dist/mcp/tools/style-pack.d.ts +11 -0
  89. package/dist/mcp/tools/style-pack.js +245 -0
  90. package/dist/mcp/tools/tool-group.d.ts +9 -0
  91. package/dist/mods/button-hotkeys.d.ts +45 -0
  92. package/dist/mods/button-hotkeys.js +185 -0
  93. package/dist/mods/compile-mod.js +19 -5
  94. package/dist/mods/custom-doodads.d.ts +21 -1
  95. package/dist/mods/custom-doodads.js +53 -7
  96. package/dist/mods/data-rules.js +8 -1
  97. package/dist/mods/effective-entry.d.ts +18 -0
  98. package/dist/mods/effective-entry.js +52 -0
  99. package/dist/mods/function-search.js +12 -0
  100. package/dist/mods/library-xml.d.ts +34 -5
  101. package/dist/mods/library-xml.js +94 -22
  102. package/dist/mods/sound-mix.d.ts +92 -0
  103. package/dist/mods/sound-mix.js +195 -0
  104. package/dist/plugins/check-workspace.js +22 -3
  105. package/dist/plugins/plugin-api.d.ts +59 -5
  106. package/dist/plugins/plugin-api.js +6 -0
  107. package/dist/plugins/plugin-sdk.d.ts +13 -1
  108. package/dist/plugins/plugin-sdk.js +16 -1
  109. package/dist/plugins/plugin-sources.js +1 -0
  110. package/dist/plugins/registry.d.ts +4 -0
  111. package/dist/plugins/registry.js +6 -2
  112. package/dist/plugins/workspace.d.ts +5 -0
  113. package/dist/plugins/workspace.js +10 -2
  114. package/dist/study/blizzard-baseline.d.ts +76 -0
  115. package/dist/study/blizzard-baseline.js +137 -0
  116. package/dist/study/exemplar-report.d.ts +71 -0
  117. package/dist/study/exemplar-report.js +163 -0
  118. package/dist/study/find-places.d.ts +127 -0
  119. package/dist/study/find-places.js +306 -0
  120. package/dist/study/load-map-package.d.ts +3 -0
  121. package/dist/study/load-map-package.js +11 -0
  122. package/dist/study/map-study.d.ts +1 -0
  123. package/dist/study/map-study.js +3 -0
  124. package/dist/study/measure-lighting.d.ts +6 -0
  125. package/dist/study/measure-lighting.js +1 -1
  126. package/dist/study/measures.d.ts +225 -0
  127. package/dist/study/measures.js +366 -0
  128. package/dist/study/scaffold-style-pack.d.ts +58 -0
  129. package/dist/study/scaffold-style-pack.js +261 -0
  130. package/dist/study/study-exemplars.d.ts +67 -0
  131. package/dist/study/study-exemplars.js +133 -0
  132. package/dist/triggers/blueprint-triggers.d.ts +71 -11
  133. package/dist/triggers/blueprint-triggers.js +403 -48
  134. package/dist/triggers/kept-trigger-galaxy.d.ts +29 -0
  135. package/dist/triggers/kept-trigger-galaxy.js +103 -7
  136. package/dist/triggers/splice-map-script.d.ts +2 -0
  137. package/dist/triggers/splice-map-script.js +309 -8
  138. package/dist/triggers/write-triggers-xml.js +20 -5
  139. package/dist/ui/frame-path-check.d.ts +43 -4
  140. package/dist/ui/frame-path-check.js +121 -13
  141. package/dist/ui/frame-paths.d.ts +21 -1
  142. package/dist/ui/frame-paths.js +26 -6
  143. package/dist/ui/toast.d.ts +4 -5
  144. package/dist/ui/toast.js +6 -10
  145. package/examples/example-campaign/README.md +27 -0
  146. package/examples/example-campaign/missions/ashfall-relay/blueprint.json +11 -1
  147. package/examples/example-campaign/missions/ashfall-relay/triggers/ash-tremors.json +69 -0
  148. package/examples/example-campaign/missions/ashfall-relay/triggers/start-ash-tremors.json +31 -0
  149. package/package.json +3 -3
@@ -28,6 +28,8 @@ export declare const SWITCH: {
28
28
  readonly caseValue: "9A8F1093";
29
29
  readonly caseActions: "363CE97C";
30
30
  };
31
+ /** The types a local variable's declaration is written for: any other is refused. */
32
+ export declare const LOCAL_VARIABLE_TYPES: ReadonlySet<string>;
31
33
  /**
32
34
  * The `Param/Value/…` and `Param/Expression/…` keys that hold text in these strings files' texts
33
35
  * (GameStrings, each locale's), with their words: a key holding nothing is an empty text, as an
@@ -109,6 +111,26 @@ export declare class KeptGalaxyRenderer {
109
111
  private identifier;
110
112
  triggerName(id: string): string;
111
113
  functionName(id: string): string;
114
+ /** A record's (Structure's) Galaxy name: gs_ and its name's identifier. */
115
+ structureName(id: string): string;
116
+ /** The Galaxy names of a preset type's values, in order, each its constant (ge_<preset>_<value>). */
117
+ presetValueNames(id: string): string[];
118
+ /**
119
+ * A record's struct as the Editor writes it in Global Structures (CRLF), through the blank line
120
+ * after it: each member declared as a local variable is, with no initial value (each variable of
121
+ * the record starts its members itself). Undefined, with `unknown` filled in, when it can't be.
122
+ */
123
+ renderStructure(id: string): string | undefined;
124
+ /**
125
+ * A preset type's constants as the Editor writes them in Global Preset Declarations (CRLF lines):
126
+ * one for each value, numbered from 0 in order, by name. Only an integer preset type of base type
127
+ * int whose values have none of their own is written, the form every map save shows; undefined,
128
+ * with `unknown` filled in, for any other.
129
+ */
130
+ renderPreset(id: string): {
131
+ readonly name: string;
132
+ readonly line: string;
133
+ }[] | undefined;
112
134
  /** A map variable's Galaxy name: gv_ for a global, lv_ for a trigger's or function's own. */
113
135
  variableGalaxyName(id: string): string;
114
136
  private variableName;
@@ -144,6 +166,13 @@ export declare class KeptGalaxyRenderer {
144
166
  * (`int[4]` for 3, `[gv_count + 1]` for a constant variable's).
145
167
  */
146
168
  private localVariable;
169
+ /**
170
+ * A variable's Galaxy type, from its `<VariableType>` of `type`: a plain type's, a preset type's
171
+ * base type's (the map's own or a library's in reach), and, for a record's member, a record of the
172
+ * map's own as its struct (gs_). A variable of a record type is refused: its Galaxy initializes
173
+ * each member, which this doesn't write yet.
174
+ */
175
+ private variableType;
147
176
  /** A `<VariableType>`'s array sizes, read as the library reader reads them (arrayBoundOf): none for a variable that isn't an array. */
148
177
  private arrayBounds;
149
178
  /** A function's Galaxy return type: `void` for an action, which has none; a value's or a condition's its `<ReturnType>`'s, a plain type. */
@@ -48,7 +48,14 @@ const GALAXY_TYPES = {
48
48
  revealer: "revealer",
49
49
  order: "order",
50
50
  actor: "actor",
51
+ // Handles the game numbers, as every Editor save declares them (a record's members, Missions' globals).
52
+ objective: "int",
53
+ ping: "int",
54
+ dialog: "int",
55
+ control: "int",
51
56
  };
57
+ /** The types a local variable's declaration is written for: any other is refused. */
58
+ export const LOCAL_VARIABLE_TYPES = new Set(Object.keys(GALAXY_TYPES));
52
59
  const lowerFirst = (text) => text.charAt(0).toLowerCase() + text.slice(1);
53
60
  /** A placeholder for a library element's Galaxy name, until `bindLibraryNames` learns it. */
54
61
  const placeholder = (key) => `\u0001${key}\u0001`;
@@ -354,6 +361,64 @@ export class KeptGalaxyRenderer {
354
361
  functionName(id) {
355
362
  return `gf_${this.identifier(this.element(id, "FunctionDef"))}`;
356
363
  }
364
+ /** A record's (Structure's) Galaxy name: gs_ and its name's identifier. */
365
+ structureName(id) {
366
+ return `gs_${this.identifier(this.element(id, "Structure"))}`;
367
+ }
368
+ /** The Galaxy names of a preset type's values, in order, each its constant (ge_<preset>_<value>). */
369
+ presetValueNames(id) {
370
+ const owner = this.element(id, "Preset");
371
+ return children(owner, "Item")
372
+ .filter((item) => item.attrs.Type === "PresetValue" && !item.attrs.Library)
373
+ .map((item) => presetConstant(undefined, this.identifier(owner), this.identifier(this.element(item.attrs.Id ?? "", "PresetValue"))));
374
+ }
375
+ /**
376
+ * A record's struct as the Editor writes it in Global Structures (CRLF), through the blank line
377
+ * after it: each member declared as a local variable is, with no initial value (each variable of
378
+ * the record starts its members itself). Undefined, with `unknown` filled in, when it can't be.
379
+ */
380
+ renderStructure(id) {
381
+ this.unknown.length = 0;
382
+ try {
383
+ const element = this.element(id, "Structure");
384
+ this.checkTags(element, ["Variable"]);
385
+ const members = children(element, "Variable").map((node) => ` ${this.localVariable(node.attrs.Id ?? "", { member: true }).declaration}`);
386
+ const lines = [`struct ${this.structureName(id)} {`, ...members, "};", "", ""];
387
+ return this.unknown.length > 0 ? undefined : lines.join("\r\n");
388
+ }
389
+ catch (error) {
390
+ if (error instanceof Unknown)
391
+ return undefined;
392
+ throw error;
393
+ }
394
+ }
395
+ /**
396
+ * A preset type's constants as the Editor writes them in Global Preset Declarations (CRLF lines):
397
+ * one for each value, numbered from 0 in order, by name. Only an integer preset type of base type
398
+ * int whose values have none of their own is written, the form every map save shows; undefined,
399
+ * with `unknown` filled in, for any other.
400
+ */
401
+ renderPreset(id) {
402
+ this.unknown.length = 0;
403
+ try {
404
+ const element = this.element(id, "Preset");
405
+ this.checkTags(element, ["PresetInteger", "BaseType", "PresetGenConstVar", "Item"]);
406
+ if (!child(element, "PresetInteger") || child(element, "BaseType")?.attrs.Value !== "int")
407
+ throw this.fail("form", "a preset type other than an integer one of base type int, whose Galaxy no map save shows");
408
+ const values = children(element, "Item").filter((item) => item.attrs.Type === "PresetValue");
409
+ for (const item of values) {
410
+ const value = this.element(item.attrs.Id ?? "", "PresetValue");
411
+ if (item.attrs.Library || value.children.length > 0)
412
+ throw this.fail("form", `a preset value ${value.id} with a value of its own, whose Galaxy no map save shows`);
413
+ }
414
+ return this.presetValueNames(id).map((name, index) => ({ name, line: `const int ${name} = ${index};\r\n` }));
415
+ }
416
+ catch (error) {
417
+ if (error instanceof Unknown)
418
+ return undefined;
419
+ throw error;
420
+ }
421
+ }
357
422
  /** A map variable's Galaxy name: gv_ for a global, lv_ for a trigger's or function's own. */
358
423
  variableGalaxyName(id) {
359
424
  return this.variableName(refNode(id));
@@ -600,18 +665,18 @@ export class KeptGalaxyRenderer {
600
665
  * constant is declared with its value; an array with its sizes, each one more than the Editor's
601
666
  * (`int[4]` for 3, `[gv_count + 1]` for a constant variable's).
602
667
  */
603
- localVariable(id) {
668
+ localVariable(id, { member = false } = {}) {
604
669
  const element = this.element(id, "Variable");
605
670
  const variableType = child(element, "VariableType") ?? { tag: "VariableType", attrs: {}, text: "", children: [] };
606
- const other = variableType.children.find((node) => !["Type", "Constant", "ArraySize"].includes(node.tag));
671
+ const type = child(variableType, "Type")?.attrs.Value ?? "";
672
+ // A game link names the catalog it links into (GameType), which its Galaxy, a string, doesn't show.
673
+ const other = variableType.children.find((node) => !["Type", "Constant", "ArraySize", "TypeElement", ...(type === "gamelink" ? ["GameType"] : [])].includes(node.tag));
607
674
  if (other)
608
675
  throw this.fail("form", `a local variable's <${other.tag}>`);
609
- const type = child(variableType, "Type")?.attrs.Value ?? "";
610
- const galaxy = GALAXY_TYPES[type];
611
- if (!galaxy)
612
- throw this.fail("form", `a local variable of type ${type || "(none)"}`);
676
+ const galaxy = this.variableType(variableType, type, member);
613
677
  const name = this.variableName(refNode(id));
614
- const value = child(element, "Value");
678
+ const value = member ? undefined : child(element, "Value");
679
+ // A record's member starts where each variable of the record does, in its own initialization, not the record's Galaxy.
615
680
  const initial = value ? this.value(value.attrs.Id ?? "") : undefined;
616
681
  const bounds = this.arrayBounds(variableType).map((bound) => `[${"size" in bound ? bound.size + 1 : `${this.variableName(refNode(bound.variable))} + 1`}]`);
617
682
  if (child(variableType, "Constant")) {
@@ -625,6 +690,37 @@ export class KeptGalaxyRenderer {
625
690
  throw this.fail("form", "an array local variable with an initial value");
626
691
  return { declaration: `${galaxy}${bounds.join("")} ${name};`, ...(starts ? { initial: `${name} = ${initial};` } : {}) };
627
692
  }
693
+ /**
694
+ * A variable's Galaxy type, from its `<VariableType>` of `type`: a plain type's, a preset type's
695
+ * base type's (the map's own or a library's in reach), and, for a record's member, a record of the
696
+ * map's own as its struct (gs_). A variable of a record type is refused: its Galaxy initializes
697
+ * each member, which this doesn't write yet.
698
+ */
699
+ variableType(variableType, type, member) {
700
+ const element = child(variableType, "TypeElement");
701
+ if (element && type !== "preset" && type !== "struct")
702
+ throw this.fail("form", "a local variable's <TypeElement>");
703
+ if (type === "preset") {
704
+ if (!element)
705
+ throw this.fail("form", "a local variable of a preset type it doesn't name");
706
+ if (!element.attrs.Library)
707
+ return this.ownPresetBase(element.attrs.Id ?? "");
708
+ return this.presetBaseGalaxy(this.reach.libraries[element.attrs.Library]?.presets[element.attrs.Id ?? ""]?.base, `a variable of the preset type ${refKey(element)}`);
709
+ }
710
+ if (type === "struct") {
711
+ if (!member)
712
+ throw this.fail("form", "a variable of a record type, whose members' initialization this doesn't write yet");
713
+ if (!element || element.attrs.Library)
714
+ throw this.fail("form", `a record's member of ${element ? `the library record ${refKey(element)}` : "a record type it doesn't name"}`);
715
+ return this.structureName(element.attrs.Id ?? "");
716
+ }
717
+ if (type === "gamelink")
718
+ return "string";
719
+ const galaxy = GALAXY_TYPES[type];
720
+ if (!galaxy)
721
+ throw this.fail("form", `a local variable of type ${type || "(none)"}`);
722
+ return galaxy;
723
+ }
628
724
  /** A `<VariableType>`'s array sizes, read as the library reader reads them (arrayBoundOf): none for a variable that isn't an array. */
629
725
  arrayBounds(variableType) {
630
726
  return children(variableType, "ArraySize").map((bound, dim) => {
@@ -5,6 +5,8 @@ export type GalaxyBlock = {
5
5
  };
6
6
  /** A name with a global's first letter lower case, as the Editor and the compile write it, to compare a name in a record or package from before #902 (gv_Count) or a Kept Identifier with another. */
7
7
  export declare const editorForm: (name: string) => string;
8
+ /** The text of the block `id` (as the record names one: `function-declaration:gf_<name>`, `trigger:gt_<name>`, …) in `script`, or undefined where it has none. */
9
+ export declare function blockText(script: string, id: string): string | undefined;
8
10
  /**
9
11
  * The blocks of `generated` (a whole script as writeMapScript writes it) that the Editor's script
10
12
  * `existing` already has a block of the same id for, each with both texts: a Kept trigger's or
@@ -13,9 +13,29 @@
13
13
  // trigger-variable:gt_<name> a trigger's declaration, in Trigger Variables
14
14
  // trigger:gt_<name> its section (its _Func and _Init), among the trigger sections
15
15
  // trigger-init:gt_<name> its _Init call, in InitTriggers
16
+ // function-declarations an empty Global Function Declarations section, when the Kept
17
+ // script has none, before Trigger Variables
18
+ // function-declaration:gf_<name> a Blueprint function's declaration, in it (ADR-0026)
19
+ // functions an empty Global Functions section, when the Kept script has none,
20
+ // before the first trigger's section
21
+ // function:gf_<name> its definition, in it
22
+ // structures an empty Global Structures section, when the Kept script has none,
23
+ // after the libraries and Constants
24
+ // structure:gs_<name> a Blueprint record's struct, in it
25
+ // presets an empty Global Presets section, when the Kept script has none,
26
+ // after Trigger Variables, as the Editor writes it beside presets
27
+ // preset-declarations an empty Global Preset Declarations section, when the Kept script
28
+ // has none, before Global Variables
29
+ // preset-value:ge_<name> a Blueprint preset type's value's constant, in it
30
+ // custom-script-init-call InitMap's InitCustomScript() call, and
31
+ // custom-scripts an empty Custom Script section ending in InitCustomScript, when
32
+ // the Kept script has none, before Global Functions
33
+ // custom-script:<name> a Blueprint custom script's block, in it
34
+ // custom-script-init:<function> its init function's call, in InitCustomScript
16
35
  //
17
- // The Editor writes globals and triggers in the depth-first order of Triggers' folder tree, so a
18
- // global's or trigger's block goes before the block of the next one in that order the script has
36
+ // The Editor writes globals, functions, triggers, records, preset values and custom script in the
37
+ // depth-first order of Triggers' folder tree, so each one's block goes before the block of the next
38
+ // one of its kind in that order the script has
19
39
  // (`order`, the spliced Triggers' items by name), or at the end of its section when none follows: a
20
40
  // Kept trigger the author files among Compiled ones keeps its place when the Editor next saves.
21
41
  // A disabled trigger, which the Editor leaves out of the script, is passed over.
@@ -118,10 +138,74 @@ function namedBlockSpan(script, id) {
118
138
  const triggers = functionSpan(script, "void InitTriggers () {");
119
139
  return triggers && lineMatching(script, ` ${escape(name)}_Init\\(\\);`, triggers.start, triggers.end);
120
140
  }
141
+ case "function-declaration": {
142
+ const section = sectionSpan(script, "Global Function Declarations");
143
+ return section && lineMatching(script, `\\S[^\\r\\n(]* ${escape(name)} \\([^\\r\\n]*\\);`, section.start, section.end);
144
+ }
145
+ case "function": {
146
+ const section = sectionSpan(script, "Global Functions");
147
+ const span = section && keptElementSpan(script, name);
148
+ return span && span.start >= section.start && span.start < section.end ? span : undefined;
149
+ }
150
+ case "structure": {
151
+ // From its `struct` line through its `};` and the blank line after it.
152
+ const section = sectionSpan(script, "Global Structures");
153
+ const open = section && lineMatching(script, `struct ${escape(name)} \\{`, section.start, section.end);
154
+ if (!section || !open)
155
+ return undefined;
156
+ const close = lineMatching(script, "\\};", open.end, section.end);
157
+ if (!close)
158
+ return undefined;
159
+ const blank = /^\r?\n/.exec(script.slice(close.end));
160
+ return { start: open.start, end: close.end + (blank ? blank[0].length : 0) };
161
+ }
162
+ case "preset-value": {
163
+ const section = sectionSpan(script, "Global Preset Declarations");
164
+ return section && lineMatching(script, `const \\S+ ${escape(name)} = [^\\r\\n]*;`, section.start, section.end);
165
+ }
166
+ case "custom-script": {
167
+ // From the rule before its "// Custom Script: <name>" heading to the next block's rule, or InitCustomScript.
168
+ const section = customScriptSection(script);
169
+ const heading = section && new RegExp(`^${escape(RULE)}\\r?\\n// Custom Script: ${escape(name)}\\r?\\n${escape(RULE)}\\r?\\n`, "m").exec(script.slice(section.start, section.end));
170
+ if (!section || !heading)
171
+ return undefined;
172
+ const start = section.start + heading.index;
173
+ const rest = script.slice(start + heading[0].length, section.end);
174
+ const next = rest.search(new RegExp(`^(?:${escape(RULE)}\\r?\\n// Custom Script: |void InitCustomScript \\(\\) \\{)`, "m"));
175
+ return { start, end: next < 0 ? section.end : start + heading[0].length + next };
176
+ }
177
+ case "custom-script-init": {
178
+ const init = functionSpan(script, "void InitCustomScript () {");
179
+ return init && lineMatching(script, ` ${escape(name)}\\(\\);`, init.start, init.end);
180
+ }
121
181
  default:
122
182
  return undefined;
123
183
  }
124
184
  }
185
+ /**
186
+ * The span of the map's Custom Script section: from after its rules past its blocks (each under a
187
+ * "Custom Script: <name>" rule pair of its own, its script as it stands, which may open with
188
+ * comments of its own) through the InitCustomScript the Editor ends it with and the blank line
189
+ * after; with no InitCustomScript, to the next section's rules.
190
+ */
191
+ function customScriptSection(script) {
192
+ const match = new RegExp(`^${escape(RULE)}\\r?\\n// Custom Script\\r?\\n${escape(RULE)}\\r?\\n`, "m").exec(script);
193
+ if (!match)
194
+ return undefined;
195
+ const start = match.index + match[0].length;
196
+ const init = functionSpan(script.slice(start), "void InitCustomScript () {");
197
+ if (init) {
198
+ const blank = /^\r?\n/.exec(script.slice(start + init.end));
199
+ return { header: match.index, start, end: start + init.end + (blank ? blank[0].length : 0) };
200
+ }
201
+ const next = script.slice(start).search(new RegExp(`^${escape(RULE)}\\r?\\n// (?!Custom Script: )[^\\r\\n]*\\r?\\n${escape(RULE)}\\r?$`, "m"));
202
+ return { header: match.index, start, end: next < 0 ? script.length : start + next };
203
+ }
204
+ /** The text of the block `id` (as the record names one: `function-declaration:gf_<name>`, `trigger:gt_<name>`, …) in `script`, or undefined where it has none. */
205
+ export function blockText(script, id) {
206
+ const span = namedBlockSpan(script, id);
207
+ return span && script.slice(span.start, span.end);
208
+ }
125
209
  /**
126
210
  * The blocks of `generated` (a whole script as writeMapScript writes it) that the Editor's script
127
211
  * `existing` already has a block of the same id for, each with both texts: a Kept trigger's or
@@ -146,7 +230,45 @@ function removeBlock(script, id) {
146
230
  case "trigger-variable":
147
231
  case "trigger":
148
232
  case "trigger-init":
233
+ case "function-declaration":
234
+ case "function":
235
+ case "structure":
236
+ case "preset-value":
237
+ case "custom-script":
238
+ case "custom-script-init":
149
239
  return remove(namedBlockSpan(script, id));
240
+ case "structures": {
241
+ const section = sectionSpan(script, "Global Structures");
242
+ return remove(section && !/^struct /m.test(script.slice(section.start, section.end)) ? { start: section.header, end: section.end } : undefined);
243
+ }
244
+ case "preset-declarations": {
245
+ const section = sectionSpan(script, "Global Preset Declarations");
246
+ return remove(section && !/^\S/m.test(script.slice(section.start, section.end)) ? { start: section.header, end: section.end } : undefined);
247
+ }
248
+ case "presets": {
249
+ // The Editor writes an empty Global Presets wherever it declares a preset type's constants: it goes once they have.
250
+ const section = sectionSpan(script, "Global Presets");
251
+ const declarations = sectionSpan(script, "Global Preset Declarations");
252
+ const declares = declarations !== undefined && /^\S/m.test(script.slice(declarations.start, declarations.end));
253
+ return remove(section && !declares && !/^\S/m.test(script.slice(section.start, section.end)) ? { start: section.header, end: section.end } : undefined);
254
+ }
255
+ case "custom-scripts": {
256
+ // With no block left, the section goes, and its InitCustomScript with it when that calls nothing.
257
+ const section = customScriptSection(script);
258
+ return remove(section && !hasCustomScripts(script) && /^void InitCustomScript \(\) \{\r?\n\}\r?\n(?:\r?\n)?$/.test(script.slice(section.start, section.end)) ? { start: section.header, end: section.end } : undefined);
259
+ }
260
+ case "custom-script-init-call": {
261
+ const map = functionSpan(script, "void InitMap () {");
262
+ return remove(map && !customScriptSection(script) ? lineMatching(script, " InitCustomScript\\(\\);", map.start, map.end) : undefined);
263
+ }
264
+ case "function-declarations": {
265
+ const section = sectionSpan(script, "Global Function Declarations");
266
+ return remove(section && !/^\S/m.test(script.slice(section.start, section.end)) ? { start: section.header, end: section.end } : undefined);
267
+ }
268
+ case "functions": {
269
+ const section = sectionSpan(script, "Global Functions");
270
+ return remove(section && !/^\S/m.test(script.slice(section.start, section.end)) ? { start: section.header, end: section.end } : undefined);
271
+ }
150
272
  case "global-variables": {
151
273
  const section = sectionSpan(script, "Global Variables");
152
274
  return remove(section && !declaresGlobals(script) ? { start: section.header, end: section.end } : undefined);
@@ -159,6 +281,11 @@ function removeBlock(script, id) {
159
281
  throw new Error(`Unknown Galaxy block ${id} on the compile record`);
160
282
  }
161
283
  }
284
+ /** Whether the script's Custom Script section holds a block. */
285
+ function hasCustomScripts(script) {
286
+ const section = customScriptSection(script);
287
+ return section !== undefined && /^\/\/ Custom Script: /m.test(script.slice(section.start, section.end));
288
+ }
162
289
  /** Whether the script's Global Variables section declares anything. */
163
290
  function declaresGlobals(script) {
164
291
  const section = sectionSpan(script, "Global Variables");
@@ -179,6 +306,27 @@ function generatedPieces(generated, kept) {
179
306
  if (name && !(keptLibs && lineMatching(kept, ` ${name}\\(\\);`, keptLibs.start, keptLibs.end)))
180
307
  pieces.push({ id: `lib-init:${name}`, text: line });
181
308
  }
309
+ const eolGenerated = eolOf(generated);
310
+ const structures = sectionSpan(generated, "Global Structures");
311
+ if (structures) {
312
+ if (!sectionSpan(kept, "Global Structures"))
313
+ pieces.push({ id: "structures", text: [RULE, "// Global Structures", RULE, ""].join(eolGenerated) });
314
+ for (const match of generated.slice(structures.start, structures.end).matchAll(/^struct (gs_\w+) \{\r?$/gm)) {
315
+ const span = namedBlockSpan(generated, `structure:${match[1]}`);
316
+ if (span)
317
+ pieces.push({ id: `structure:${match[1]}`, text: generated.slice(span.start, span.end) });
318
+ }
319
+ }
320
+ const presets = sectionSpan(generated, "Global Preset Declarations");
321
+ if (presets) {
322
+ // Global Presets first: it goes only once the declarations have, which a later splice takes out in reverse.
323
+ if (!sectionSpan(kept, "Global Presets") && sectionSpan(generated, "Global Presets"))
324
+ pieces.push({ id: "presets", text: [RULE, "// Global Presets", RULE, "", ""].join(eolGenerated) });
325
+ if (!sectionSpan(kept, "Global Preset Declarations"))
326
+ pieces.push({ id: "preset-declarations", text: [RULE, "// Global Preset Declarations", RULE, "", ""].join(eolGenerated) });
327
+ for (const match of generated.slice(presets.start, presets.end).matchAll(/^const \S+ (ge_\w+) = [^\r\n]*;\r?\n/gm))
328
+ pieces.push({ id: `preset-value:${match[1]}`, text: match[0] });
329
+ }
182
330
  const globals = sectionSpan(generated, "Global Variables");
183
331
  if (globals) {
184
332
  const body = generated.slice(globals.start, globals.end);
@@ -196,6 +344,45 @@ function generatedPieces(generated, kept) {
196
344
  for (const line of inits)
197
345
  pieces.push({ id: `variable-init:${/^ (gv_\w+) = /.exec(line)[1]}`, text: line });
198
346
  }
347
+ const functionDeclarations = sectionSpan(generated, "Global Function Declarations");
348
+ if (functionDeclarations) {
349
+ const eol = eolOf(generated);
350
+ const declarations = [...generated.slice(functionDeclarations.start, functionDeclarations.end).matchAll(/^\S[^\r\n(]* (gf_\w+) \([^\r\n]*\);\r?\n/gm)];
351
+ if (!sectionSpan(kept, "Global Function Declarations"))
352
+ pieces.push({ id: "function-declarations", text: [RULE, "// Global Function Declarations", RULE, "", ""].join(eol) });
353
+ for (const match of declarations)
354
+ pieces.push({ id: `function-declaration:${match[1]}`, text: match[0] });
355
+ if (!sectionSpan(kept, "Global Functions"))
356
+ pieces.push({ id: "functions", text: [RULE, "// Global Functions", RULE, ""].join(eol) });
357
+ for (const match of declarations) {
358
+ const span = keptElementSpan(generated, match[1]);
359
+ if (span)
360
+ pieces.push({ id: `function:${match[1]}`, text: generated.slice(span.start, span.end) });
361
+ }
362
+ }
363
+ const customScripts = customScriptSection(generated);
364
+ if (customScripts) {
365
+ // The call first: it goes only once the section has, which a later splice takes out in reverse.
366
+ if (!customScriptSection(kept)) {
367
+ const call = lineMatching(generated, " InitCustomScript\\(\\);");
368
+ if (!call)
369
+ throw new Error("The compile's script has a Custom Script section but no InitCustomScript() call in InitMap");
370
+ pieces.push({ id: "custom-script-init-call", text: call.text });
371
+ pieces.push({ id: "custom-scripts", text: [RULE, "// Custom Script", RULE, "void InitCustomScript () {", "}", "", ""].join(eolGenerated) });
372
+ }
373
+ const body = generated.slice(customScripts.start, customScripts.end);
374
+ for (const match of body.matchAll(/^\/\/ Custom Script: ([^\r\n]*)\r?$/gm)) {
375
+ const span = namedBlockSpan(generated, `custom-script:${match[1]}`);
376
+ if (span)
377
+ pieces.push({ id: `custom-script:${match[1]}`, text: generated.slice(span.start, span.end) });
378
+ }
379
+ const init = functionSpan(generated, "void InitCustomScript () {");
380
+ for (const line of init ? generated.slice(init.start, init.close).split(/(?<=\n)/).slice(1) : []) {
381
+ const name = /^ {4}(\w+)\(\);/.exec(line)?.[1];
382
+ if (name)
383
+ pieces.push({ id: `custom-script-init:${name}`, text: line });
384
+ }
385
+ }
199
386
  const triggerVariables = sectionSpan(generated, "Trigger Variables");
200
387
  const names = triggerVariables ? [...generated.slice(triggerVariables.start, triggerVariables.end).matchAll(/^trigger (gt_\w+);\r?\n/gm)] : [];
201
388
  for (const match of names)
@@ -222,10 +409,11 @@ function following(order, name) {
222
409
  */
223
410
  function nextBlock(piece, order, find) {
224
411
  const name = piece.id.split(/:(.*)/s)[1] ?? "";
225
- const prefix = name.slice(0, 3);
412
+ // A trigger's, global's, function's, record's or preset value's name by its prefix (gt_, gv_, gf_, gs_, ge_); a custom script's name and its init function's by none.
413
+ const kindOf = (text) => (/^g[tvfse]_/.test(text) ? text.slice(0, 3) : "");
226
414
  for (const names of following(order, name)) {
227
415
  for (const next of names) {
228
- if (!next.startsWith(prefix))
416
+ if (kindOf(next) !== kindOf(name))
229
417
  continue;
230
418
  const span = find(next);
231
419
  if (span)
@@ -291,6 +479,75 @@ function insertPiece(script, piece, eol, order) {
291
479
  const triggers = need(functionSpan(script, "void InitTriggers () {"), "InitTriggers");
292
480
  return insertAt(script, before((name) => lineMatching(script, ` ${escape(name)}_Init\\(\\);`, triggers.start, triggers.close)) ?? triggers.close, text);
293
481
  }
482
+ case "function-declarations": {
483
+ // Just before Trigger Variables, after the globals.
484
+ const next = need(/^\/\/-+\r?\n\/\/ Trigger Variables\r?\n/m.exec(script), "Trigger Variables section");
485
+ return insertAt(script, next.index, text);
486
+ }
487
+ case "function-declaration": {
488
+ const section = need(sectionSpan(script, "Global Function Declarations"), "Global Function Declarations section");
489
+ const next = before((name) => namedBlockSpan(script, `function-declaration:${name}`));
490
+ if (next !== undefined)
491
+ return insertAt(script, next, text);
492
+ const declarations = [...script.slice(section.start, section.end).matchAll(/^\S[^\r\n]*;\r?\n/gm)];
493
+ const last = declarations.at(-1);
494
+ return insertAt(script, last ? section.start + last.index + last[0].length : section.start, text);
495
+ }
496
+ case "functions": {
497
+ // Before the first trigger's section, after Trigger Variables and any custom script.
498
+ const next = need(/^\/\/-+\r?\n\/\/ (Trigger: |Trigger Initialization\r?\n)/m.exec(script), "Trigger Initialization section");
499
+ return insertAt(script, next.index, text);
500
+ }
501
+ case "function": {
502
+ const section = need(sectionSpan(script, "Global Functions"), "Global Functions section");
503
+ return insertAt(script, before((name) => namedBlockSpan(script, `function:${name}`)) ?? section.end, text);
504
+ }
505
+ case "structures": {
506
+ // After the libraries and constants, before the preset declarations and the globals.
507
+ const next = need(/^\/\/-+\r?\n\/\/ (Global Preset Declarations|Global Variables|Global Function Declarations|Trigger Variables)\r?\n/m.exec(script), "Trigger Variables section");
508
+ return insertAt(script, next.index, text);
509
+ }
510
+ case "structure": {
511
+ const section = need(sectionSpan(script, "Global Structures"), "Global Structures section");
512
+ return insertAt(script, before((name) => namedBlockSpan(script, `structure:${name}`)) ?? section.end, text);
513
+ }
514
+ case "preset-declarations": {
515
+ const next = need(/^\/\/-+\r?\n\/\/ (Global Variables|Global Function Declarations|Trigger Variables)\r?\n/m.exec(script), "Trigger Variables section");
516
+ return insertAt(script, next.index, text);
517
+ }
518
+ case "preset-value": {
519
+ const section = need(sectionSpan(script, "Global Preset Declarations"), "Global Preset Declarations section");
520
+ const next = before((name) => namedBlockSpan(script, `preset-value:${name}`));
521
+ if (next !== undefined)
522
+ return insertAt(script, next, text);
523
+ const declarations = [...script.slice(section.start, section.end).matchAll(/^\S[^\r\n]*;\r?\n/gm)];
524
+ const last = declarations.at(-1);
525
+ return insertAt(script, last ? section.start + last.index + last[0].length : section.start, text);
526
+ }
527
+ case "presets": {
528
+ // After Trigger Variables, before custom script, the functions and the triggers.
529
+ const next = need(/^\/\/-+\r?\n\/\/ (Custom Script\r?\n|Global Functions\r?\n|Trigger: |Trigger Initialization\r?\n)/m.exec(script), "Trigger Initialization section");
530
+ return insertAt(script, next.index, text);
531
+ }
532
+ case "custom-scripts": {
533
+ const next = need(/^\/\/-+\r?\n\/\/ (Global Functions\r?\n|Trigger: |Trigger Initialization\r?\n)/m.exec(script), "Trigger Initialization section");
534
+ return insertAt(script, next.index, text);
535
+ }
536
+ case "custom-script-init-call": {
537
+ // After InitGlobals' call, or InitLibs' where there is none.
538
+ const map = need(functionSpan(script, "void InitMap () {"), "InitMap");
539
+ const after = lineMatching(script, " InitGlobals\\(\\);", map.start, map.end) ?? need(lineMatching(script, " InitLibs\\(\\);", map.start, map.end), "InitLibs() call in InitMap");
540
+ return insertAt(script, after.end, text);
541
+ }
542
+ case "custom-script": {
543
+ const section = need(customScriptSection(script), "Custom Script section");
544
+ const init = lineMatching(script, "void InitCustomScript \\(\\) \\{", section.start, section.end);
545
+ return insertAt(script, before((name) => namedBlockSpan(script, `custom-script:${name}`)) ?? init?.start ?? section.end, text);
546
+ }
547
+ case "custom-script-init": {
548
+ const init = need(functionSpan(script, "void InitCustomScript () {"), "InitCustomScript");
549
+ return insertAt(script, before((name) => namedBlockSpan(script, `custom-script-init:${name}`)) ?? init.close, text);
550
+ }
294
551
  default:
295
552
  throw new Error(`Unknown Galaxy block ${piece.id}`);
296
553
  }
@@ -305,6 +562,18 @@ function declaredNames(script) {
305
562
  for (const match of script.slice(section.start, section.end).matchAll(/^\S[^\r\n]* (g[tv]_\w+);/gm))
306
563
  names.add(editorForm(match[1]));
307
564
  }
565
+ const functions = sectionSpan(script, "Global Function Declarations");
566
+ if (functions)
567
+ for (const match of script.slice(functions.start, functions.end).matchAll(/^\S[^\r\n(]* (gf_\w+) \(/gm))
568
+ names.add(match[1]);
569
+ const structures = sectionSpan(script, "Global Structures");
570
+ if (structures)
571
+ for (const match of script.slice(structures.start, structures.end).matchAll(/^struct (gs_\w+) \{/gm))
572
+ names.add(match[1]);
573
+ const presets = sectionSpan(script, "Global Preset Declarations");
574
+ if (presets)
575
+ for (const match of script.slice(presets.start, presets.end).matchAll(/^const \S+ (ge_\w+) = /gm))
576
+ names.add(match[1]);
308
577
  return names;
309
578
  }
310
579
  /**
@@ -326,19 +595,30 @@ order = []) {
326
595
  const eol = eolOf(existing);
327
596
  const skipped = new Set([...skip].map(editorForm));
328
597
  const pieces = generatedPieces(generated, script).filter((piece) => {
329
- const name = /^(?:trigger-variable|trigger|trigger-init|variable|variable-init):(.*)$/.exec(piece.id)?.[1];
598
+ const name = /^(?:trigger-variable|trigger|trigger-init|variable|variable-init|function-declaration|function):(.*)$/.exec(piece.id)?.[1];
330
599
  return name === undefined || !skipped.has(editorForm(name));
331
600
  });
332
601
  const taken = declaredNames(script);
333
602
  for (const piece of pieces) {
334
- const name = /^(?:trigger-variable|variable):(.*)$/.exec(piece.id)?.[1];
603
+ const name = /^(?:trigger-variable|variable|function-declaration|structure|preset-value):(.*)$/.exec(piece.id)?.[1];
604
+ const kind = name?.startsWith("gt_") ? "trigger" : name?.startsWith("gf_") ? "function" : name?.startsWith("gs_") ? "record" : name?.startsWith("ge_") ? "preset value" : "variable";
335
605
  if (name && taken.has(editorForm(name)))
336
- throw new Error(`MapScript.galaxy already declares ${name}, a Kept ${name.startsWith("gt_") ? "trigger" : "variable"}; the compile's would have the same script name`);
606
+ throw new Error(`MapScript.galaxy already declares ${name}, a Kept ${kind}; the compile's would have the same script name`);
337
607
  }
338
608
  for (const piece of pieces)
339
609
  script = insertPiece(script, piece, eol, order);
340
610
  return { content: script, blocks: pieces.map((piece) => ({ id: piece.id, fingerprint: fingerprint(piece.text.replace(/\r?\n/g, eol)) })) };
341
611
  }
612
+ /** The sections the compile writes only to hold a Blueprint's elements, each by the kind of block it holds, which it is recorded before. */
613
+ const OWN_SECTIONS = {
614
+ "function-declarations": "function-declaration",
615
+ functions: "function",
616
+ structures: "structure",
617
+ presets: "preset-value",
618
+ "preset-declarations": "preset-value",
619
+ "custom-script-init-call": "custom-script",
620
+ "custom-scripts": "custom-script",
621
+ };
342
622
  /**
343
623
  * The blocks of a whole script the compile wrote (no Kept triggers beside it): each trigger's and
344
624
  * global's, as a later splice would find them once the Editor has saved the script with Kept
@@ -346,7 +626,28 @@ order = []) {
346
626
  */
347
627
  export function scriptBlocks(script) {
348
628
  const eol = eolOf(script);
349
- return generatedPieces(script, script).map((piece) => ({ id: piece.id, fingerprint: fingerprint(piece.text.replace(/\r?\n/g, eol)) }));
629
+ const pieces = generatedPieces(script, script);
630
+ // The sections the compile writes only for a Blueprint's elements are its own (no Editor-saved script was there to hold them): recorded before
631
+ // what they hold, in the order a splice gives them, so a later splice takes them out once empty.
632
+ const sections = generatedPieces(script, "").filter((piece) => piece.id in OWN_SECTIONS);
633
+ const out = [];
634
+ const placed = new Set();
635
+ const place = (kind) => {
636
+ for (const section of sections) {
637
+ if (OWN_SECTIONS[section.id] !== kind || placed.has(section.id))
638
+ continue;
639
+ out.push(section);
640
+ placed.add(section.id);
641
+ }
642
+ };
643
+ for (const piece of pieces) {
644
+ place(piece.id.split(":")[0]);
645
+ out.push(piece);
646
+ }
647
+ for (const section of sections)
648
+ if (!placed.has(section.id))
649
+ out.push(section);
650
+ return out.map((piece) => ({ id: piece.id, fingerprint: fingerprint(piece.text.replace(/\r?\n/g, eol)) }));
350
651
  }
351
652
  /**
352
653
  * Where a Kept element's Galaxy is in the Editor's script: a trigger's section (`gt_<name>`), from