@nodetool-ai/game-runtime 0.8.1 → 0.8.3

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 (238) hide show
  1. package/README.md +43 -0
  2. package/dist/.nodetool-build-stamp +1 -1
  3. package/dist/audio-mixer-references.d.ts +10 -0
  4. package/dist/audio-mixer-references.d.ts.map +1 -0
  5. package/dist/audio-mixer-references.js +21 -0
  6. package/dist/audio-mixer-references.js.map +1 -0
  7. package/dist/audio-source-ops.d.ts +24 -0
  8. package/dist/audio-source-ops.d.ts.map +1 -0
  9. package/dist/audio-source-ops.js +27 -0
  10. package/dist/audio-source-ops.js.map +1 -0
  11. package/dist/authoring-identity.d.ts +3 -0
  12. package/dist/authoring-identity.d.ts.map +1 -0
  13. package/dist/authoring-identity.js +12 -0
  14. package/dist/authoring-identity.js.map +1 -0
  15. package/dist/authoring-reconcile.d.ts +23 -0
  16. package/dist/authoring-reconcile.d.ts.map +1 -0
  17. package/dist/authoring-reconcile.js +375 -0
  18. package/dist/authoring-reconcile.js.map +1 -0
  19. package/dist/autoplay.d.ts.map +1 -1
  20. package/dist/autoplay.js +32 -5
  21. package/dist/autoplay.js.map +1 -1
  22. package/dist/document-ops.d.ts +2657 -368
  23. package/dist/document-ops.d.ts.map +1 -1
  24. package/dist/document-ops.js +135 -16
  25. package/dist/document-ops.js.map +1 -1
  26. package/dist/document-ops3d.d.ts +13390 -0
  27. package/dist/document-ops3d.d.ts.map +1 -0
  28. package/dist/document-ops3d.js +493 -0
  29. package/dist/document-ops3d.js.map +1 -0
  30. package/dist/gameplay/lifecycle.d.ts +91 -0
  31. package/dist/gameplay/lifecycle.d.ts.map +1 -0
  32. package/dist/gameplay/lifecycle.js +135 -0
  33. package/dist/gameplay/lifecycle.js.map +1 -0
  34. package/dist/index.d.ts +10 -0
  35. package/dist/index.d.ts.map +1 -1
  36. package/dist/index.js +10 -0
  37. package/dist/index.js.map +1 -1
  38. package/dist/open-session.d.ts +32 -0
  39. package/dist/open-session.d.ts.map +1 -0
  40. package/dist/open-session.js +51 -0
  41. package/dist/open-session.js.map +1 -0
  42. package/dist/ownership-ops.d.ts +45 -0
  43. package/dist/ownership-ops.d.ts.map +1 -0
  44. package/dist/ownership-ops.js +173 -0
  45. package/dist/ownership-ops.js.map +1 -0
  46. package/dist/prepared-collider3d.d.ts +11 -0
  47. package/dist/prepared-collider3d.d.ts.map +1 -0
  48. package/dist/prepared-collider3d.js +41 -0
  49. package/dist/prepared-collider3d.js.map +1 -0
  50. package/dist/sample3d.d.ts +4 -0
  51. package/dist/sample3d.d.ts.map +1 -0
  52. package/dist/sample3d.js +62 -0
  53. package/dist/sample3d.js.map +1 -0
  54. package/dist/script-clock.d.ts +19 -0
  55. package/dist/script-clock.d.ts.map +1 -0
  56. package/dist/script-clock.js +32 -0
  57. package/dist/script-clock.js.map +1 -0
  58. package/dist/script-declarations.d.ts +7 -0
  59. package/dist/script-declarations.d.ts.map +1 -0
  60. package/dist/script-declarations.js +99 -0
  61. package/dist/script-declarations.js.map +1 -0
  62. package/dist/script-lifecycle.d.ts +108 -0
  63. package/dist/script-lifecycle.d.ts.map +1 -0
  64. package/dist/script-lifecycle.js +253 -0
  65. package/dist/script-lifecycle.js.map +1 -0
  66. package/dist/script-params.d.ts +25 -0
  67. package/dist/script-params.d.ts.map +1 -0
  68. package/dist/script-params.js +63 -0
  69. package/dist/script-params.js.map +1 -0
  70. package/dist/script-persistence.d.ts +3 -0
  71. package/dist/script-persistence.d.ts.map +1 -0
  72. package/dist/script-persistence.js +67 -0
  73. package/dist/script-persistence.js.map +1 -0
  74. package/dist/script-props.d.ts +21 -0
  75. package/dist/script-props.d.ts.map +1 -0
  76. package/dist/script-props.js +42 -0
  77. package/dist/script-props.js.map +1 -0
  78. package/dist/script-transport.d.ts +5 -0
  79. package/dist/script-transport.d.ts.map +1 -0
  80. package/dist/script-transport.js +64 -0
  81. package/dist/script-transport.js.map +1 -0
  82. package/dist/script-types.d.ts +5 -1
  83. package/dist/script-types.d.ts.map +1 -1
  84. package/dist/script-types.js +13 -11
  85. package/dist/script-types.js.map +1 -1
  86. package/dist/script-types3d.d.ts +6 -0
  87. package/dist/script-types3d.d.ts.map +1 -0
  88. package/dist/script-types3d.js +28 -0
  89. package/dist/script-types3d.js.map +1 -0
  90. package/dist/script-world.d.ts +41 -0
  91. package/dist/script-world.d.ts.map +1 -0
  92. package/dist/script-world.js +169 -0
  93. package/dist/script-world.js.map +1 -0
  94. package/dist/scripts.d.ts +107 -4
  95. package/dist/scripts.d.ts.map +1 -1
  96. package/dist/scripts.js +513 -111
  97. package/dist/scripts.js.map +1 -1
  98. package/dist/scripts3d.d.ts +34 -0
  99. package/dist/scripts3d.d.ts.map +1 -0
  100. package/dist/scripts3d.js +14 -0
  101. package/dist/scripts3d.js.map +1 -0
  102. package/dist/session.d.ts +8 -3
  103. package/dist/session.d.ts.map +1 -1
  104. package/dist/session.js +249 -795
  105. package/dist/session.js.map +1 -1
  106. package/dist/session3d.d.ts +30 -0
  107. package/dist/session3d.d.ts.map +1 -0
  108. package/dist/session3d.js +603 -0
  109. package/dist/session3d.js.map +1 -0
  110. package/dist/spatial3d/camera.d.ts +7 -0
  111. package/dist/spatial3d/camera.d.ts.map +1 -0
  112. package/dist/spatial3d/camera.js +43 -0
  113. package/dist/spatial3d/camera.js.map +1 -0
  114. package/dist/spatial3d/digest.d.ts +4 -0
  115. package/dist/spatial3d/digest.d.ts.map +1 -0
  116. package/dist/spatial3d/digest.js +30 -0
  117. package/dist/spatial3d/digest.js.map +1 -0
  118. package/dist/spatial3d/layers.d.ts +30 -0
  119. package/dist/spatial3d/layers.d.ts.map +1 -0
  120. package/dist/spatial3d/layers.js +93 -0
  121. package/dist/spatial3d/layers.js.map +1 -0
  122. package/dist/spatial3d/math.d.ts +23 -0
  123. package/dist/spatial3d/math.d.ts.map +1 -0
  124. package/dist/spatial3d/math.js +39 -0
  125. package/dist/spatial3d/math.js.map +1 -0
  126. package/dist/spatial3d/state.d.ts +37 -0
  127. package/dist/spatial3d/state.d.ts.map +1 -0
  128. package/dist/spatial3d/state.js +4 -0
  129. package/dist/spatial3d/state.js.map +1 -0
  130. package/dist/spatial3d/world.d.ts +68 -0
  131. package/dist/spatial3d/world.d.ts.map +1 -0
  132. package/dist/spatial3d/world.js +446 -0
  133. package/dist/spatial3d/world.js.map +1 -0
  134. package/dist/systems/animation-graph3d.d.ts +24 -0
  135. package/dist/systems/animation-graph3d.d.ts.map +1 -0
  136. package/dist/systems/animation-graph3d.js +262 -0
  137. package/dist/systems/animation-graph3d.js.map +1 -0
  138. package/dist/systems/animation3d.d.ts +3 -0
  139. package/dist/systems/animation3d.d.ts.map +1 -0
  140. package/dist/systems/animation3d.js +14 -0
  141. package/dist/systems/animation3d.js.map +1 -0
  142. package/dist/systems/character3d.d.ts +3 -0
  143. package/dist/systems/character3d.d.ts.map +1 -0
  144. package/dist/systems/character3d.js +6 -0
  145. package/dist/systems/character3d.js.map +1 -0
  146. package/dist/systems/collision2d.d.ts +27 -0
  147. package/dist/systems/collision2d.d.ts.map +1 -0
  148. package/dist/systems/collision2d.js +154 -0
  149. package/dist/systems/collision2d.js.map +1 -0
  150. package/dist/systems/contacts2d.d.ts +3 -0
  151. package/dist/systems/contacts2d.d.ts.map +1 -0
  152. package/dist/systems/contacts2d.js +36 -0
  153. package/dist/systems/contacts2d.js.map +1 -0
  154. package/dist/systems/contacts3d.d.ts +3 -0
  155. package/dist/systems/contacts3d.d.ts.map +1 -0
  156. package/dist/systems/contacts3d.js +55 -0
  157. package/dist/systems/contacts3d.js.map +1 -0
  158. package/dist/systems/context2d.d.ts +41 -0
  159. package/dist/systems/context2d.d.ts.map +1 -0
  160. package/dist/systems/context2d.js +2 -0
  161. package/dist/systems/context2d.js.map +1 -0
  162. package/dist/systems/context3d.d.ts +67 -0
  163. package/dist/systems/context3d.d.ts.map +1 -0
  164. package/dist/systems/context3d.js +2 -0
  165. package/dist/systems/context3d.js.map +1 -0
  166. package/dist/systems/gameplay2d.d.ts +3 -0
  167. package/dist/systems/gameplay2d.d.ts.map +1 -0
  168. package/dist/systems/gameplay2d.js +88 -0
  169. package/dist/systems/gameplay2d.js.map +1 -0
  170. package/dist/systems/gameplay3d.d.ts +4 -0
  171. package/dist/systems/gameplay3d.d.ts.map +1 -0
  172. package/dist/systems/gameplay3d.js +170 -0
  173. package/dist/systems/gameplay3d.js.map +1 -0
  174. package/dist/systems/hierarchy3d.d.ts +3 -0
  175. package/dist/systems/hierarchy3d.d.ts.map +1 -0
  176. package/dist/systems/hierarchy3d.js +27 -0
  177. package/dist/systems/hierarchy3d.js.map +1 -0
  178. package/dist/systems/input2d.d.ts +3 -0
  179. package/dist/systems/input2d.d.ts.map +1 -0
  180. package/dist/systems/input2d.js +6 -0
  181. package/dist/systems/input2d.js.map +1 -0
  182. package/dist/systems/input3d.d.ts +3 -0
  183. package/dist/systems/input3d.d.ts.map +1 -0
  184. package/dist/systems/input3d.js +16 -0
  185. package/dist/systems/input3d.js.map +1 -0
  186. package/dist/systems/physics2d.d.ts +3 -0
  187. package/dist/systems/physics2d.d.ts.map +1 -0
  188. package/dist/systems/physics2d.js +244 -0
  189. package/dist/systems/physics2d.js.map +1 -0
  190. package/dist/systems/physics3d.d.ts +3 -0
  191. package/dist/systems/physics3d.d.ts.map +1 -0
  192. package/dist/systems/physics3d.js +4 -0
  193. package/dist/systems/physics3d.js.map +1 -0
  194. package/dist/systems/pipeline.d.ts +23 -0
  195. package/dist/systems/pipeline.d.ts.map +1 -0
  196. package/dist/systems/pipeline.js +65 -0
  197. package/dist/systems/pipeline.js.map +1 -0
  198. package/dist/systems/presentation2d.d.ts +9 -0
  199. package/dist/systems/presentation2d.d.ts.map +1 -0
  200. package/dist/systems/presentation2d.js +240 -0
  201. package/dist/systems/presentation2d.js.map +1 -0
  202. package/dist/systems/presentation3d.d.ts +9 -0
  203. package/dist/systems/presentation3d.d.ts.map +1 -0
  204. package/dist/systems/presentation3d.js +115 -0
  205. package/dist/systems/presentation3d.js.map +1 -0
  206. package/dist/systems/scripts2d.d.ts +7 -0
  207. package/dist/systems/scripts2d.d.ts.map +1 -0
  208. package/dist/systems/scripts2d.js +274 -0
  209. package/dist/systems/scripts2d.js.map +1 -0
  210. package/dist/systems/scripts3d.d.ts +7 -0
  211. package/dist/systems/scripts3d.d.ts.map +1 -0
  212. package/dist/systems/scripts3d.js +262 -0
  213. package/dist/systems/scripts3d.js.map +1 -0
  214. package/dist/systems/state2d.d.ts +65 -0
  215. package/dist/systems/state2d.d.ts.map +1 -0
  216. package/dist/systems/state2d.js +2 -0
  217. package/dist/systems/state2d.js.map +1 -0
  218. package/dist/systems/tick2d.d.ts +7 -0
  219. package/dist/systems/tick2d.d.ts.map +1 -0
  220. package/dist/systems/tick2d.js +15 -0
  221. package/dist/systems/tick2d.js.map +1 -0
  222. package/dist/systems/tick3d.d.ts +7 -0
  223. package/dist/systems/tick3d.d.ts.map +1 -0
  224. package/dist/systems/tick3d.js +17 -0
  225. package/dist/systems/tick3d.js.map +1 -0
  226. package/dist/validate-animation-graph3d.d.ts +8 -0
  227. package/dist/validate-animation-graph3d.d.ts.map +1 -0
  228. package/dist/validate-animation-graph3d.js +115 -0
  229. package/dist/validate-animation-graph3d.js.map +1 -0
  230. package/dist/validate.d.ts +5 -0
  231. package/dist/validate.d.ts.map +1 -1
  232. package/dist/validate.js +61 -2
  233. package/dist/validate.js.map +1 -1
  234. package/dist/validate3d.d.ts +20 -0
  235. package/dist/validate3d.d.ts.map +1 -0
  236. package/dist/validate3d.js +325 -0
  237. package/dist/validate3d.js.map +1 -0
  238. package/package.json +10 -3
@@ -0,0 +1,253 @@
1
+ import { gameEventReaches } from "@nodetool-ai/protocol";
2
+ /**
3
+ * Lifecycle-object scripts: a script source may evaluate to an object of hooks instead of a function.
4
+ * The guest dispatcher below runs the hooks inside the behavior's single call per tick, so the call,
5
+ * batch, input and output budgets apply unchanged. Its runtime state, including timers, is a JSON record
6
+ * stored in the snapshot's `scriptState`, which makes hooks and timers replay from any snapshot.
7
+ */
8
+ /** Hook names in the order the dispatcher runs them within one tick. */
9
+ export const GAME_SCRIPT_HOOKS = [
10
+ "onStart", "onSceneEnter", "onTriggerEnter", "onTriggerExit", "onContact", "onEvent", "onFixedUpdate", "onUpdate", "onDestroy"
11
+ ];
12
+ export const MAX_GAME_SCRIPT_TIMERS = 32;
13
+ /** About 4.6 hours at 60 ticks per second. */
14
+ export const MAX_GAME_SCRIPT_TIMER_TICKS = 1_000_000;
15
+ export function isScriptLifecycleRecord(value) {
16
+ return typeof value === "object" && value !== null && !Array.isArray(value) && value.$lifecycle === 1;
17
+ }
18
+ /** A lifecycle record that still owes an `onDestroy` call. */
19
+ export function awaitsDestroy(value) {
20
+ return isScriptLifecycleRecord(value) && value.destroyed !== true;
21
+ }
22
+ function parseKey(key) {
23
+ if (typeof key !== "string") {
24
+ return undefined;
25
+ }
26
+ try {
27
+ const parsed = JSON.parse(key);
28
+ return Array.isArray(parsed) && parsed.length === 3 && typeof parsed[0] === "string" && typeof parsed[1] === "string"
29
+ && Number.isInteger(parsed[2]) ? parsed : undefined;
30
+ }
31
+ catch {
32
+ // Not a script key, so not a record the despawn path wrote.
33
+ return undefined;
34
+ }
35
+ }
36
+ /**
37
+ * The `onDestroy` call for a removed instance's record, or undefined when the record is not one the despawn path
38
+ * could have written. `scriptState` is guest-written and snapshot-restored, so the record supplies only the entity's
39
+ * last observable fields. The key, the source, and every limit come from the host: the record's key must name an
40
+ * entity of the current scene that no longer exists, its source must be a lifecycle source for the same behavior
41
+ * index, and `limitsOf` reads `maxCommands` and `maxTickMs` from the document's behavior definition.
42
+ */
43
+ export function removedDestroyCall(stateKey, record, context) {
44
+ if (!awaitsDestroy(record)) {
45
+ return undefined;
46
+ }
47
+ const { removed, ...rest } = record;
48
+ if (typeof removed !== "object" || removed === null || Array.isArray(removed)) {
49
+ return undefined;
50
+ }
51
+ const key = parseKey(stateKey);
52
+ const sourceKey = removed.sourceKey;
53
+ const source = parseKey(sourceKey);
54
+ if (!key || !source || typeof sourceKey !== "string" || key[0] !== context.sceneId || context.exists(key[1])
55
+ || source[2] !== key[2] || !context.hookSources.has(sourceKey)) {
56
+ return undefined;
57
+ }
58
+ const limits = context.limitsOf(sourceKey);
59
+ if (!limits) {
60
+ return undefined;
61
+ }
62
+ const fields = {};
63
+ for (const field of context.fields) {
64
+ if (Object.hasOwn(removed, field)) {
65
+ fields[field] = removed[field];
66
+ }
67
+ }
68
+ return { ...fields, active: false, sourceKey, stateKey, entityId: key[1], source: source[1],
69
+ maxCommands: limits.maxCommands, maxTickMs: limits.maxTickMs, state: rest, lifecycle: { destroy: true } };
70
+ }
71
+ /** Commands an `onDestroy` call may return. The entity is gone, so commands that act on it are rejected. */
72
+ const DESTROY_COMMANDS = new Set(["hud", "emit", "spawn", "despawn", "sceneTransition"]);
73
+ export function assertDestroyCommands(commands, entityId, tick) {
74
+ for (const command of commands) {
75
+ if (!DESTROY_COMMANDS.has(command.kind)) {
76
+ throw new Error(`Game script onDestroy for ${entityId} at tick ${tick} cannot use ${command.kind}`);
77
+ }
78
+ }
79
+ }
80
+ /** Checks the own keys of a lifecycle object. `functions` lists the keys whose values are functions. */
81
+ export function scriptHookIssue(keys, functions) {
82
+ const known = new Set(GAME_SCRIPT_HOOKS);
83
+ const callable = new Set(functions);
84
+ for (const key of keys) {
85
+ if (/^on[A-Z]/.test(key) && !known.has(key)) {
86
+ return `unknown lifecycle hook ${key}; use ${GAME_SCRIPT_HOOKS.join(", ")}`;
87
+ }
88
+ if (known.has(key) && !callable.has(key)) {
89
+ return `lifecycle hook ${key} must be a function`;
90
+ }
91
+ }
92
+ if (callable.has("onUpdate") && callable.has("onFixedUpdate")) {
93
+ return "onFixedUpdate is an alias of onUpdate; define one of them";
94
+ }
95
+ if (!GAME_SCRIPT_HOOKS.some((hook) => callable.has(hook))) {
96
+ return "a lifecycle object must define at least one hook";
97
+ }
98
+ return undefined;
99
+ }
100
+ /** Contacts involving `entityId` in the previous tick's events, in event order. */
101
+ export function scriptContacts(entityId, events, sensorOf) {
102
+ const contacts = [];
103
+ for (const value of events) {
104
+ const event = value;
105
+ if (event.kind !== "contact" || (event.entityId !== entityId && event.otherId !== entityId)) {
106
+ continue;
107
+ }
108
+ contacts.push({ otherId: event.entityId === entityId ? event.otherId : event.entityId, phase: event.phase ?? "enter", sensor: sensorOf(event) });
109
+ }
110
+ return contacts;
111
+ }
112
+ /**
113
+ * Indices of the previous tick's trigger events that reach an entity, in event order. Only calls of scripts that
114
+ * define `onEvent` carry them, so the input of every other call stays as it was.
115
+ */
116
+ export function scriptEvents(entityId, tags, events) {
117
+ const indices = [];
118
+ events.forEach((value, index) => {
119
+ const event = value;
120
+ if (event.kind === "trigger" && gameEventReaches(event, entityId, tags)) {
121
+ indices.push(index);
122
+ }
123
+ });
124
+ return indices;
125
+ }
126
+ /** The lifecycle facts for a call, or undefined when there are none. */
127
+ export function scriptLifecycle(sceneEnter, contacts, events = []) {
128
+ if (!sceneEnter && contacts.length === 0 && events.length === 0) {
129
+ return undefined;
130
+ }
131
+ return { ...(sceneEnter ? { sceneEnter: true } : undefined), ...(contacts.length > 0 ? { contacts } : undefined),
132
+ ...(events.length > 0 ? { events } : undefined) };
133
+ }
134
+ /**
135
+ * Orders the hooks of one call. Timers due at the start of the tick fire in schedule order after the contact
136
+ * and event hooks. A timer scheduled during the tick fires on a later tick.
137
+ */
138
+ export function planScriptHooks(record, lifecycle, tick) {
139
+ const first = record === null || record === undefined;
140
+ if (!first && !isScriptLifecycleRecord(record)) {
141
+ throw new Error("lifecycle script state is not a lifecycle record");
142
+ }
143
+ if (!first && (!Array.isArray(record.timers) || record.timers.length > MAX_GAME_SCRIPT_TIMERS || !record.timers.every((timer) => typeof timer === "object" && timer !== null && validTimerName(timer.name) && Number.isInteger(timer.at)
144
+ && (timer.every === undefined || validTimerTicks(timer.every))))) {
145
+ throw new Error("lifecycle script state has invalid timers");
146
+ }
147
+ const state = first ? null : record.state;
148
+ if (lifecycle?.destroy) {
149
+ return { state, steps: [["onDestroy"]], timers: [], destroy: true };
150
+ }
151
+ const steps = [];
152
+ if (first) {
153
+ steps.push(["onStart"]);
154
+ }
155
+ if (lifecycle?.sceneEnter) {
156
+ steps.push(["onSceneEnter"]);
157
+ }
158
+ for (const contact of lifecycle?.contacts ?? []) {
159
+ if (!contact.sensor) {
160
+ steps.push(["onContact", contact]);
161
+ }
162
+ else if (contact.phase === "enter") {
163
+ steps.push(["onTriggerEnter", contact]);
164
+ }
165
+ else if (contact.phase === "exit") {
166
+ steps.push(["onTriggerExit", contact]);
167
+ }
168
+ }
169
+ for (const index of lifecycle?.events ?? []) {
170
+ steps.push(["onEvent", index]);
171
+ }
172
+ const timers = [];
173
+ for (const timer of first ? [] : record.timers) {
174
+ if (timer.at > tick) {
175
+ timers.push(timer);
176
+ continue;
177
+ }
178
+ steps.push([timer.name]);
179
+ if (timer.every !== undefined) {
180
+ timers.push({ name: timer.name, at: tick + timer.every, every: timer.every });
181
+ }
182
+ }
183
+ steps.push(["onFixedUpdate"], ["onUpdate"]);
184
+ return { state, steps, timers, destroy: false };
185
+ }
186
+ function validTimerTicks(ticks) {
187
+ return Number.isInteger(ticks) && ticks >= 1 && ticks <= MAX_GAME_SCRIPT_TIMER_TICKS;
188
+ }
189
+ function validTimerName(name) {
190
+ return typeof name === "string" && name.length > 0 && name.length <= 128 && !GAME_SCRIPT_HOOKS.includes(name)
191
+ && !["__proto__", "constructor", "prototype"].includes(name);
192
+ }
193
+ /** Builds the next record from the dispatcher's `[state, scheduled]` result. Scheduling a name replaces its timer. */
194
+ export function commitScriptHooks(value, plan, tick) {
195
+ if (!Array.isArray(value) || value.length !== 2 || !Array.isArray(value[1])) {
196
+ throw new Error("lifecycle dispatcher returned an invalid result");
197
+ }
198
+ const [state, scheduled] = value;
199
+ if (plan.destroy) {
200
+ return { $lifecycle: 1, state, timers: [], destroyed: true };
201
+ }
202
+ const timers = [...plan.timers];
203
+ for (const entry of scheduled) {
204
+ // The dispatcher runs in the guest, which can replace the built-ins it uses, so every timer is checked again here.
205
+ if (!Array.isArray(entry) || entry.length !== 3 || !validTimerName(entry[0]) || !validTimerTicks(entry[1]) || typeof entry[2] !== "boolean") {
206
+ throw new Error("lifecycle dispatcher returned an invalid timer");
207
+ }
208
+ const [name, ticks, repeat] = entry;
209
+ const timer = repeat ? { name, at: tick + ticks, every: ticks } : { name, at: tick + ticks };
210
+ const index = timers.findIndex((candidate) => candidate.name === name);
211
+ if (index >= 0) {
212
+ timers[index] = timer;
213
+ }
214
+ else {
215
+ timers.push(timer);
216
+ }
217
+ }
218
+ if (timers.length > MAX_GAME_SCRIPT_TIMERS) {
219
+ throw new Error(`a script may hold at most ${MAX_GAME_SCRIPT_TIMERS} timers`);
220
+ }
221
+ return { $lifecycle: 1, state, timers };
222
+ }
223
+ /**
224
+ * Guest code for the dispatcher, compiled into each lifecycle-object context, so it stays small. It runs the
225
+ * planned hooks and returns `{state: [state, scheduled], commands}`, which `commitScriptHooks` turns into a record.
226
+ */
227
+ export const SCRIPT_LIFECYCLE_DISPATCH = `((hasOwn, isArray, isInteger, defineProperty, copy, hookNames) => (hooks, payload, steps) => {
228
+ let state = payload.state;
229
+ // A shallow copy, so a hook that reorders or empties input.events cannot change what a later onEvent receives.
230
+ const commands = [], scheduled = [], events = isArray(payload.events) ? copy(payload.events) : [];
231
+ const schedule = (repeat) => (ticks, name) => {
232
+ if (!isInteger(ticks) || ticks < 1 || ticks > ${MAX_GAME_SCRIPT_TIMER_TICKS}) { throw new Error("timer ticks must be an integer from 1 to ${MAX_GAME_SCRIPT_TIMER_TICKS}"); }
233
+ if (typeof name !== "string" || hookNames.includes(name) || typeof hooks[name] !== "function") { throw new Error("timer " + String(name) + " must name a method of the script object that is not a hook"); }
234
+ scheduled.push([name, ticks, repeat]);
235
+ };
236
+ defineProperty(globalThis, "after", { value: schedule(false), writable: true, configurable: true });
237
+ defineProperty(globalThis, "every", { value: schedule(true), writable: true, configurable: true });
238
+ for (const [name, argument] of steps) {
239
+ const hook = hooks[name];
240
+ if (typeof hook !== "function") { continue; }
241
+ payload.state = state;
242
+ const result = argument === undefined ? hook.call(hooks, payload) : hook.call(hooks, payload, name === "onEvent" ? events[argument] : argument);
243
+ if (result === undefined) { continue; }
244
+ if (result === null || typeof result !== "object" || isArray(result)) { throw new Error(name + " must return an object or undefined"); }
245
+ if (hasOwn(result, "state")) { state = result.state; }
246
+ if (result.commands !== undefined) {
247
+ if (!isArray(result.commands)) { throw new Error(name + " commands must be an array"); }
248
+ for (const command of result.commands) { commands.push(command); }
249
+ }
250
+ }
251
+ return { state: [state, scheduled], commands };
252
+ })(Object.hasOwn, Array.isArray, Number.isInteger, Object.defineProperty, Function.prototype.call.bind(Array.prototype.slice), ${JSON.stringify(GAME_SCRIPT_HOOKS)})`;
253
+ //# sourceMappingURL=script-lifecycle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"script-lifecycle.js","sourceRoot":"","sources":["../src/script-lifecycle.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAwB,MAAM,uBAAuB,CAAC;AAE/E;;;;;GAKG;AAEH,wEAAwE;AACxE,MAAM,CAAC,MAAM,iBAAiB,GAAG;IAC/B,SAAS,EAAE,cAAc,EAAE,gBAAgB,EAAE,eAAe,EAAE,WAAW,EAAE,SAAS,EAAE,eAAe,EAAE,UAAU,EAAE,WAAW;CACtH,CAAC;AAIX,MAAM,CAAC,MAAM,sBAAsB,GAAG,EAAE,CAAC;AACzC,8CAA8C;AAC9C,MAAM,CAAC,MAAM,2BAA2B,GAAG,SAAS,CAAC;AAoCrD,MAAM,UAAU,uBAAuB,CAAC,KAAc;IACpD,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAK,KAAkC,CAAC,UAAU,KAAK,CAAC,CAAC;AACtI,CAAC;AAED,8DAA8D;AAC9D,MAAM,UAAU,aAAa,CAAC,KAA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EAAE,EAAE,IAAI,GAAG,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,IAAI,GAAG,KAAK,EAAE,CAAC;QAC9G,MAAM,KAAK,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,SAAS,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC;QACvE,IAAI,KAAK,IAAI,CAAC,EAAE,CAAC;YAAC,MAAM,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC;QAAC,CAAC;aAAM,CAAC;YAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAAC,CAAC;IACzE,CAAC;IACD,IAAI,MAAM,CAAC,MAAM,GAAG,sBAAsB,EAAE,CAAC;QAC3C,MAAM,IAAI,KAAK,CAAC,6BAA6B,sBAAsB,SAAS,CAAC,CAAC;IAChF,CAAC;IACD,OAAO,EAAE,UAAU,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;AAC1C,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG;;;;;oDAKW,2BAA2B,iEAAiE,2BAA2B;;;;;;;;;;;;;;;;;;;;iIAoB1C,IAAI,CAAC,SAAS,CAAC,iBAAiB,CAAC,GAAG,CAAC"}
@@ -0,0 +1,25 @@
1
+ import { type GameScriptParam, type GameScriptParamValue } from "@nodetool-ai/protocol";
2
+ interface ScriptParamBehavior {
3
+ readonly params?: Readonly<Record<string, GameScriptParam>>;
4
+ readonly values?: Readonly<Record<string, GameScriptParamValue>>;
5
+ }
6
+ export interface ScriptParamReferenceIssue {
7
+ /** Relative to the behavior: the stored value, or the declared default it falls back to. */
8
+ readonly path: readonly string[];
9
+ readonly message: string;
10
+ }
11
+ /**
12
+ * Checks the entity and asset references a script's params resolve to. `entityExists` is null
13
+ * where entity references cannot resolve at runtime, such as inside a 3D prefab.
14
+ */
15
+ export declare function scriptParamReferenceIssues(behavior: ScriptParamBehavior, entityExists: ((id: string) => boolean) | null, assetKind: (slot: string) => string | undefined): ScriptParamReferenceIssue[];
16
+ export interface ScriptParamsEdit {
17
+ /** Replaces the declarations. Null removes params and values. Values for removed params are dropped. */
18
+ readonly params?: Readonly<Record<string, GameScriptParam>> | null;
19
+ /** Merges stored values. A null value returns that param to its default. */
20
+ readonly values?: Readonly<Record<string, GameScriptParamValue | null>>;
21
+ }
22
+ /** Applies a `set_script_params` edit to a script behavior. The caller parses the result with the behavior schema. */
23
+ export declare function editScriptParams<Behavior extends ScriptParamBehavior>(behavior: Behavior, edit: ScriptParamsEdit): Record<string, unknown>;
24
+ export {};
25
+ //# sourceMappingURL=script-params.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"script-params.d.ts","sourceRoot":"","sources":["../src/script-params.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0B,KAAK,eAAe,EAAE,KAAK,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAEhH,UAAU,mBAAmB;IAC3B,QAAQ,CAAC,MAAM,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC,CAAC;IAC5D,QAAQ,CAAC,MAAM,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,oBAAoB,CAAC,CAAC,CAAC;CAClE;AAED,MAAM,WAAW,yBAAyB;IACxC,4FAA4F;IAC5F,QAAQ,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,CAAC;IACjC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AAED;;;GAGG;AACH,wBAAgB,0BAA0B,CAAC,QAAQ,EAAE,mBAAmB,EAAE,YAAY,EAAE,CAAC,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC,GAAG,IAAI,EACtH,SAAS,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,GAAG,SAAS,GAAG,yBAAyB,EAAE,CAwB9E;AAED,MAAM,WAAW,gBAAgB;IAC/B,wGAAwG;IACxG,QAAQ,CAAC,MAAM,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC,GAAG,IAAI,CAAC;IACnE,4EAA4E;IAC5E,QAAQ,CAAC,MAAM,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,oBAAoB,GAAG,IAAI,CAAC,CAAC,CAAC;CACzE;AAED,sHAAsH;AACtH,wBAAgB,gBAAgB,CAAC,QAAQ,SAAS,mBAAmB,EAAE,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,gBAAgB,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAgB1I"}
@@ -0,0 +1,63 @@
1
+ import { gameScriptParamValueOf } from "@nodetool-ai/protocol";
2
+ /**
3
+ * Checks the entity and asset references a script's params resolve to. `entityExists` is null
4
+ * where entity references cannot resolve at runtime, such as inside a 3D prefab.
5
+ */
6
+ export function scriptParamReferenceIssues(behavior, entityExists, assetKind) {
7
+ const issues = [];
8
+ for (const [name, param] of Object.entries(behavior.params ?? {})) {
9
+ if (param.type !== "entity" && param.type !== "asset") {
10
+ continue;
11
+ }
12
+ const stored = behavior.values !== undefined && Object.hasOwn(behavior.values, name);
13
+ const value = gameScriptParamValueOf(param, stored ? behavior.values?.[name] : undefined);
14
+ if (typeof value !== "string") {
15
+ continue;
16
+ }
17
+ const path = stored ? ["values", name] : ["params", name, "default"];
18
+ if (param.type === "entity") {
19
+ if (entityExists === null) {
20
+ issues.push({ path, message: `Script parameter ${name} cannot reference an entity inside a prefab` });
21
+ }
22
+ else if (!entityExists(value)) {
23
+ issues.push({ path, message: `Script parameter ${name} references missing entity ${value}` });
24
+ }
25
+ continue;
26
+ }
27
+ const kind = assetKind(value);
28
+ if (kind === undefined) {
29
+ issues.push({ path, message: `Script parameter ${name} references missing asset ${value}` });
30
+ }
31
+ else if (param.kind !== undefined && kind !== param.kind) {
32
+ issues.push({ path, message: `Script parameter ${name} requires a ${param.kind} asset, but ${value} is ${kind}` });
33
+ }
34
+ }
35
+ return issues;
36
+ }
37
+ /** Applies a `set_script_params` edit to a script behavior. The caller parses the result with the behavior schema. */
38
+ export function editScriptParams(behavior, edit) {
39
+ const { params: previousParams, values: previousValues, ...rest } = behavior;
40
+ const params = edit.params === undefined ? previousParams : edit.params ?? undefined;
41
+ if (params === undefined) {
42
+ if (edit.values && Object.keys(edit.values).length > 0) {
43
+ return { ...rest, values: edit.values };
44
+ }
45
+ return rest;
46
+ }
47
+ const values = {};
48
+ for (const [name, value] of Object.entries(previousValues ?? {})) {
49
+ if (Object.hasOwn(params, name)) {
50
+ values[name] = value;
51
+ }
52
+ }
53
+ for (const [name, value] of Object.entries(edit.values ?? {})) {
54
+ if (value === null) {
55
+ delete values[name];
56
+ }
57
+ else {
58
+ Object.defineProperty(values, name, { value, writable: true, enumerable: true, configurable: true });
59
+ }
60
+ }
61
+ return Object.keys(values).length > 0 ? { ...rest, params, values } : { ...rest, params };
62
+ }
63
+ //# sourceMappingURL=script-params.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"script-params.js","sourceRoot":"","sources":["../src/script-params.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAmD,MAAM,uBAAuB,CAAC;AAahH;;;GAGG;AACH,MAAM,UAAU,0BAA0B,CAAC,QAA6B,EAAE,YAA8C,EACtH,SAA+C;IAC/C,MAAM,MAAM,GAAgC,EAAE,CAAC;IAC/C,KAAK,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,MAAM,IAAI,EAAE,CAAC,EAAE,CAAC;QAClE,IAAI,KAAK,CAAC,IAAI,KAAK,QAAQ,IAAI,KAAK,CAAC,IAAI,KAAK,OAAO,EAAE,CAAC;YAAC,SAAS;QAAC,CAAC;QACpE,MAAM,MAAM,GAAG,QAAQ,CAAC,MAAM,KAAK,SAAS,IAAI,MAA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CAAC,CAAC;QAAC,CAAC;aACvC,CAAC;YAAC,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,CAAC,CAAC;QAAC,CAAC;IAChH,CAAC;IACD,OAAO,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,GAAG,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,GAAG,IAAI,EAAE,MAAM,EAAE,CAAC;AAC5F,CAAC"}
@@ -0,0 +1,3 @@
1
+ /** A positive grammar: every accepted expression can only read its JSON input. */
2
+ export declare function canPersistGameScript(source: string): boolean;
3
+ //# sourceMappingURL=script-persistence.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"script-persistence.d.ts","sourceRoot":"","sources":["../src/script-persistence.ts"],"names":[],"mappings":"AAMA,kFAAkF;AAClF,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CA2D5D"}
@@ -0,0 +1,67 @@
1
+ import { parse } from "acorn";
2
+ const forbiddenProperties = new Set(["__proto__", "prototype", "constructor", "random"]);
3
+ const unaryOperators = new Set(["!", "+", "-", "~", "typeof", "void"]);
4
+ const binaryOperators = new Set(["+", "-", "*", "/", "%", "**", "==", "!=", "===", "!==", "<", "<=", ">", ">=", "|", "&", "^", "<<", ">>", ">>>"]);
5
+ /** A positive grammar: every accepted expression can only read its JSON input. */
6
+ export function canPersistGameScript(source) {
7
+ try {
8
+ const program = parse(`(${source})`, { ecmaVersion: "latest" });
9
+ const statement = program.body[0];
10
+ if (program.body.length !== 1 || statement?.type !== "ExpressionStatement") {
11
+ return false;
12
+ }
13
+ const fn = statement.expression;
14
+ if ((fn.type !== "ArrowFunctionExpression" && fn.type !== "FunctionExpression") || fn.async || fn.generator || fn.id || fn.params.length !== 1) {
15
+ return false;
16
+ }
17
+ const parameter = fn.params[0];
18
+ if (parameter.type !== "Identifier") {
19
+ return false;
20
+ }
21
+ const readsInput = (node) => {
22
+ if (node.type === "Identifier") {
23
+ return node.name === parameter.name;
24
+ }
25
+ return node.type === "MemberExpression" && !node.computed && !node.optional && node.property.type === "Identifier"
26
+ && !forbiddenProperties.has(node.property.name) && node.object.type !== "Super" && readsInput(node.object);
27
+ };
28
+ const accepts = (node) => {
29
+ switch (node.type) {
30
+ case "Literal":
31
+ return node.value === null || typeof node.value === "string" || typeof node.value === "boolean"
32
+ || (typeof node.value === "number" && Number.isFinite(node.value));
33
+ case "Identifier":
34
+ case "MemberExpression":
35
+ return readsInput(node);
36
+ case "ArrayExpression":
37
+ return node.elements.every((element) => element !== null && element.type !== "SpreadElement" && accepts(element));
38
+ case "ObjectExpression":
39
+ return node.properties.every((property) => property.type === "Property" && property.kind === "init" && !property.method
40
+ && !property.computed && !property.shorthand
41
+ && ((property.key.type === "Identifier" && !forbiddenProperties.has(property.key.name))
42
+ || (property.key.type === "Literal" && typeof property.key.value === "string" && !forbiddenProperties.has(property.key.value)))
43
+ && accepts(property.value));
44
+ case "UnaryExpression":
45
+ return unaryOperators.has(node.operator) && accepts(node.argument);
46
+ case "BinaryExpression":
47
+ return binaryOperators.has(node.operator) && node.left.type !== "PrivateIdentifier" && accepts(node.left) && accepts(node.right);
48
+ case "LogicalExpression":
49
+ return (node.operator === "&&" || node.operator === "||" || node.operator === "??") && accepts(node.left) && accepts(node.right);
50
+ case "ConditionalExpression":
51
+ return accepts(node.test) && accepts(node.consequent) && accepts(node.alternate);
52
+ default:
53
+ return false;
54
+ }
55
+ };
56
+ if (fn.body.type !== "BlockStatement") {
57
+ return accepts(fn.body);
58
+ }
59
+ const returned = fn.body.body[0];
60
+ return fn.body.body.length === 1 && returned?.type === "ReturnStatement" && returned.argument != null && accepts(returned.argument);
61
+ }
62
+ catch {
63
+ // Sources outside this grammar keep the existing fresh-context execution.
64
+ return false;
65
+ }
66
+ }
67
+ //# sourceMappingURL=script-persistence.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"script-persistence.js","sourceRoot":"","sources":["../src/script-persistence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAmB,MAAM,OAAO,CAAC;AAE/C,MAAM,mBAAmB,GAAG,IAAI,GAAG,CAAC,CAAC,WAAW,EAAE,WAAW,EAAE,aAAa,EAAE,QAAQ,CAAC,CAAC,CAAC;AACzF,MAAM,cAAc,GAAG,IAAI,GAAG,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC;AACvE,MAAM,eAAe,GAAG,IAAI,GAAG,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC;AAEnJ,kFAAkF;AAClF,MAAM,UAAU,oBAAoB,CAAC,MAAc;IACjD,IAAI,CAAC;QACH,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,MAAM,GAAG,EAAE,EAAE,WAAW,EAAE,QAAQ,EAAE,CAAC,CAAC;QAChE,MAAM,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAClC,IAAI,OAAO,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,IAAI,SAAS,EAAE,IAAI,KAAK,qBAAqB,EAAE,CAAC;YAC3E,OAAO,KAAK,CAAC;QACf,CAAC;QACD,MAAM,EAAE,GAAG,SAAS,CAAC,UAAU,CAAC;QAChC,IAAI,CAAC,EAAE,CAAC,IAAI,KAAK,yBAAyB,IAAI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KAAK,mBAAmB,IAAI,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;gBACnI,KAAK,mBAAmB;oBACtB,OAAO,CAAC,IAAI,CAAC,QAAQ,KAAK,IAAI,IAAI,IAAI,CAAC,QAAQ,KAAK,IAAI,IAAI,IAAI,CAAC,QAAQ,KAAK,IAAI,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;gBACnI,KAAK,uBAAuB;oBAC1B,OAAO,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;gBACnF;oBACE,OAAO,KAAK,CAAC;YACjB,CAAC;QACH,CAAC,CAAC;QACF,IAAI,EAAE,CAAC,IAAI,CAAC,IAAI,KAAK,gBAAgB,EAAE,CAAC;YACtC,OAAO,OAAO,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC;QAC1B,CAAC;QACD,MAAM,QAAQ,GAAG,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACjC,OAAO,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,IAAI,QAAQ,EAAE,IAAI,KAAK,iBAAiB,IAAI,QAAQ,CAAC,QAAQ,IAAI,IAAI,IAAI,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;IACtI,CAAC;IAAC,MAAM,CAAC;QACP,0EAA0E;QAC1E,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC"}
@@ -0,0 +1,21 @@
1
+ import { type GameEntityProps } from "@nodetool-ai/protocol";
2
+ interface ScriptPropResult {
3
+ readonly entityId: string;
4
+ readonly commands: readonly ({
5
+ readonly kind: "setProp";
6
+ readonly key: string;
7
+ readonly value: unknown;
8
+ } | {
9
+ readonly kind: "removeProp";
10
+ readonly key: string;
11
+ } | {
12
+ readonly kind: string;
13
+ })[];
14
+ }
15
+ /**
16
+ * Validates every `setProp` and `removeProp` of a script batch before any command mutates state.
17
+ * Returns the final props of each entity the batch changes. Errors name the entity and tick.
18
+ */
19
+ export declare function planScriptProps(results: readonly ScriptPropResult[], currentProps: (entityId: string) => GameEntityProps | undefined, tick: number, supported: boolean): ReadonlyMap<string, GameEntityProps>;
20
+ export {};
21
+ //# sourceMappingURL=script-props.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"script-props.d.ts","sourceRoot":"","sources":["../src/script-props.ts"],"names":[],"mappings":"AAAA,OAAO,EAA4C,KAAK,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAEvG,UAAU,gBAAgB;IACxB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,QAAQ,EAAE,SAAS,CAAC;QAAE,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;QAAC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAA;KAAE,GACpG;QAAE,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;QAAC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAA;KAAE,GAAG;QAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC,EAAE,CAAC;CAC1F;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAC7B,OAAO,EAAE,SAAS,gBAAgB,EAAE,EACpC,YAAY,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,eAAe,GAAG,SAAS,EAC/D,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,OAAO,GACjB,WAAW,CAAC,MAAM,EAAE,eAAe,CAAC,CA4BtC"}
@@ -0,0 +1,42 @@
1
+ import { gameEntityPropertyValue, gameEntityProps } from "@nodetool-ai/protocol";
2
+ /**
3
+ * Validates every `setProp` and `removeProp` of a script batch before any command mutates state.
4
+ * Returns the final props of each entity the batch changes. Errors name the entity and tick.
5
+ */
6
+ export function planScriptProps(results, currentProps, tick, supported) {
7
+ const pending = new Map();
8
+ for (const result of results) {
9
+ for (const command of result.commands) {
10
+ if (!("key" in command) || (command.kind !== "setProp" && command.kind !== "removeProp")) {
11
+ continue;
12
+ }
13
+ const label = `Game script ${command.kind} ${JSON.stringify(command.key)} for ${result.entityId} at tick ${tick}`;
14
+ if (!supported) {
15
+ throw new Error(`${label} requires schema version 4`);
16
+ }
17
+ const props = pending.get(result.entityId) ?? { ...currentProps(result.entityId) };
18
+ if ("value" in command) {
19
+ const value = gameEntityPropertyValue.safeParse(command.value);
20
+ if (!value.success) {
21
+ throw new Error(`${label} is invalid: ${value.error.issues[0]?.message ?? "not JSON"}`);
22
+ }
23
+ // An own property, so a reserved key is rejected below rather than changing the prototype.
24
+ Object.defineProperty(props, command.key, { value: value.data, writable: true, enumerable: true, configurable: true });
25
+ }
26
+ else {
27
+ delete props[command.key];
28
+ }
29
+ pending.set(result.entityId, props);
30
+ }
31
+ }
32
+ const planned = new Map();
33
+ for (const [entityId, props] of pending) {
34
+ const parsed = gameEntityProps.safeParse(props);
35
+ if (!parsed.success) {
36
+ throw new Error(`Game script props for ${entityId} at tick ${tick} are invalid: ${parsed.error.issues[0]?.message ?? "invalid"}`);
37
+ }
38
+ planned.set(entityId, parsed.data);
39
+ }
40
+ return planned;
41
+ }
42
+ //# sourceMappingURL=script-props.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"script-props.js","sourceRoot":"","sources":["../src/script-props.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,eAAe,EAAwB,MAAM,uBAAuB,CAAC;AAQvG;;;GAGG;AACH,MAAM,UAAU,eAAe,CAC7B,OAAoC,EACpC,YAA+D,EAC/D,IAAY,EACZ,SAAkB;IAElB,MAAM,OAAO,GAAG,IAAI,GAAG,EAAmC,CAAC;IAC3D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AC;iBAAM,CAAC;gBACN,OAAO,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;YAC5B,CAAC;YACD,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;QACtC,CAAC;IACH,CAAC;IACD,MAAM,OAAO,GAAG,IAAI,GAAG,EAA2B,CAAC;IACnD,KAAK,MAAM,CAAC,QAAQ,EAAE,KAAK,CAAC,IAAI,OAAO,EAAE,CAAC;QACxC,MAAM,MAAM,GAAG,eAAe,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;QAChD,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;YACpB,MAAM,IAAI,KAAK,CAAC,yBAAyB,QAAQ,YAAY,IAAI,iBAAiB,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,OAAO,IAAI,SAAS,EAAE,CAAC,CAAC;QACpI,CAAC;QACD,OAAO,CAAC,GAAG,CAAC,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC;IACrC,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC"}
@@ -0,0 +1,5 @@
1
+ import type { QuickJSContext, QuickJSHandle } from "quickjs-emscripten-core";
2
+ /** Transfer already-normalized JSON values without compiling input as source. */
3
+ export declare function gameScriptValue(context: QuickJSContext, value: unknown, defineData: QuickJSHandle, pristine?: boolean): QuickJSHandle;
4
+ export declare function scriptHandleResult(context: QuickJSContext, result: ReturnType<QuickJSContext["callFunction"]>): QuickJSHandle;
5
+ //# sourceMappingURL=script-transport.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"script-transport.d.ts","sourceRoot":"","sources":["../src/script-transport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAE7E,iFAAiF;AACjF,wBAAgB,eAAe,CAAC,OAAO,EAAE,cAAc,EAAE,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,QAAQ,UAAQ,GAAG,aAAa,CA4BnI;AAED,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,cAAc,EAAE,MAAM,EAAE,UAAU,CAAC,cAAc,CAAC,cAAc,CAAC,CAAC,GAAG,aAAa,CAQ7H"}
@@ -0,0 +1,64 @@
1
+ /** Transfer already-normalized JSON values without compiling input as source. */
2
+ export function gameScriptValue(context, value, defineData, pristine = false) {
3
+ if (value === null) {
4
+ return context.null.dup();
5
+ }
6
+ if (value === undefined) {
7
+ return context.undefined.dup();
8
+ }
9
+ if (typeof value === "boolean") {
10
+ return (value ? context.true : context.false).dup();
11
+ }
12
+ if (typeof value === "number") {
13
+ return context.newNumber(value);
14
+ }
15
+ if (typeof value === "string") {
16
+ return context.newString(value);
17
+ }
18
+ if (typeof value !== "object") {
19
+ throw new Error("Script transport requires JSON data");
20
+ }
21
+ const handle = Array.isArray(value) ? context.newArray() : context.newObject();
22
+ try {
23
+ for (const [key, item] of Object.entries(value)) {
24
+ const child = gameScriptValue(context, item, defineData, pristine);
25
+ try {
26
+ if (!pristine || key === "__proto__") {
27
+ const name = context.newString(key);
28
+ try {
29
+ scriptHandleResult(context, context.callFunction(defineData, context.undefined, handle, name, child)).dispose();
30
+ }
31
+ finally {
32
+ name.dispose();
33
+ }
34
+ }
35
+ else {
36
+ context.setProp(handle, key, child);
37
+ }
38
+ }
39
+ finally {
40
+ child.dispose();
41
+ }
42
+ }
43
+ return handle;
44
+ }
45
+ catch (error) {
46
+ handle.dispose();
47
+ throw error;
48
+ }
49
+ }
50
+ export function scriptHandleResult(context, result) {
51
+ if (result.error) {
52
+ let error;
53
+ try {
54
+ error = context.dump(result.error);
55
+ }
56
+ finally {
57
+ result.error.dispose();
58
+ }
59
+ const message = error !== null && typeof error === "object" && "message" in error && error.message != null ? String(error.message) : "unknown error";
60
+ throw new Error(`Game script failed: ${message}`);
61
+ }
62
+ return result.value;
63
+ }
64
+ //# sourceMappingURL=script-transport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AO,IAAI,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,eAAe,CAAC;QACrJ,MAAM,IAAI,KAAK,CAAC,uBAAuB,OAAO,EAAE,CAAC,CAAC;IACpD,CAAC;IACD,OAAO,MAAM,CAAC,KAAK,CAAC;AACtB,CAAC"}
@@ -1,3 +1,7 @@
1
1
  /** Monaco declarations for the native game function-expression contract. */
2
- export declare const GAME_SCRIPT_TYPES = "\ntype GameScriptCommand =\n | { kind: \"setVelocity\"; x: number; y: number }\n | { kind: \"setPosition\"; x: number; y: number }\n | { kind: \"setVisual\"; tint?: string; opacity?: number; rotation?: number; scaleX?: number; scaleY?: number; flipX?: boolean }\n | { kind: \"playAnimation\"; clip: string }\n | { kind: \"hud\"; id: string; text: string; x: number; y: number; size?: number; color?: string; align?: \"left\" | \"center\" | \"right\"; fontId?: string }\n | { kind: \"emit\"; event: string }\n | { kind: \"spawn\"; prefabId: string; x?: number; y?: number; velocityX?: number; velocityY?: number }\n | { kind: \"despawn\"; entityId: string }\n | { kind: \"sceneTransition\"; sceneId: string };\n\ntype GameScriptInput = {\n tick: number;\n pressed: readonly string[];\n justPressed: readonly string[];\n events: readonly unknown[];\n entity: { id: string; x: number; y: number; velocityX: number; velocityY: number;\n touching: { down: boolean; up: boolean; left: boolean; right: boolean } };\n world: readonly { id: string; source: string; x: number; y: number }[];\n state: unknown;\n random: () => number;\n};\n\ntype GameScriptResult = { state: unknown; commands: GameScriptCommand[] };\ntype GameScript = (input: GameScriptInput) => GameScriptResult;\n";
2
+ export declare const GAME_SCRIPT_TYPES: string;
3
+ /** Global world queries available during a script call. */
4
+ export declare const GAME_SCRIPT_WORLD_TYPES: string;
5
+ /** Lifecycle-object scripts and timers, composed after the compatibility-pinned input types. */
6
+ export declare const GAME_SCRIPT_LIFECYCLE_TYPES: string;
3
7
  //# sourceMappingURL=script-types.d.ts.map