castle-web-cli 0.4.229 → 0.4.231

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 (259) hide show
  1. package/dist/agent-prompts.d.ts +2 -0
  2. package/dist/agent-prompts.js +16 -8
  3. package/dist/agent.d.ts +2 -0
  4. package/dist/agent.js +21 -21
  5. package/dist/api.d.ts +28 -2
  6. package/dist/api.js +74 -16
  7. package/dist/castle-host/host.d.ts +91 -0
  8. package/dist/castle-host/host.js +860 -3
  9. package/dist/castleJson.d.ts +44 -0
  10. package/dist/castleJson.js +131 -0
  11. package/dist/check.js +27 -1
  12. package/dist/contentPack.d.ts +49 -0
  13. package/dist/contentPack.js +273 -0
  14. package/dist/devExperiments.d.ts +21 -0
  15. package/dist/devExperiments.js +139 -0
  16. package/dist/devHub.d.ts +55 -0
  17. package/dist/devHub.js +187 -0
  18. package/dist/devSaveStorage.d.ts +36 -0
  19. package/dist/devSaveStorage.js +469 -0
  20. package/dist/devSessionChild.js +25 -13
  21. package/dist/devSessionServer.d.ts +14 -1
  22. package/dist/devSessionServer.js +237 -46
  23. package/dist/devSessionStorage.d.ts +9 -0
  24. package/dist/devSessionStorage.js +66 -1
  25. package/dist/experimentAssignment.d.ts +15 -0
  26. package/dist/experimentAssignment.js +41 -0
  27. package/dist/importCheckHook.d.ts +40 -0
  28. package/dist/importCheckHook.js +171 -0
  29. package/dist/importRegression.d.ts +15 -0
  30. package/dist/importRegression.js +100 -0
  31. package/dist/importTrust.d.ts +2 -0
  32. package/dist/importTrust.js +27 -0
  33. package/dist/importUpdateHook.d.ts +18 -0
  34. package/dist/importUpdateHook.js +31 -8
  35. package/dist/importUpdateHookRunner.js +19 -7
  36. package/dist/imports.d.ts +16 -0
  37. package/dist/imports.js +141 -31
  38. package/dist/index.js +3 -1
  39. package/dist/init.d.ts +3 -0
  40. package/dist/init.js +126 -9
  41. package/dist/install.js +4 -1
  42. package/dist/localPaths.js +4 -0
  43. package/dist/login.js +2 -2
  44. package/dist/platformDoc.js +3 -8
  45. package/dist/playPagePolicy.js +3 -1
  46. package/dist/remix.js +5 -0
  47. package/dist/routerTaskReport.d.ts +28 -0
  48. package/dist/routerTaskReport.js +56 -0
  49. package/dist/save-deck.d.ts +18 -0
  50. package/dist/save-deck.js +142 -6
  51. package/dist/serve.d.ts +8 -0
  52. package/dist/serve.js +99 -14
  53. package/dist/serverBundle.d.ts +1 -1
  54. package/dist/serverBundle.js +5 -1
  55. package/dist/webSourceSync.d.ts +2 -0
  56. package/dist/webSourceSync.js +2 -0
  57. package/kits/base/CLAUDE.md +2 -3
  58. package/kits/base/castle.json +1 -1
  59. package/kits/base/sdk/README.md +406 -53
  60. package/kits/base/sdk/agent.js +4 -4
  61. package/kits/base/sdk/avatar.d.ts +12 -0
  62. package/kits/base/sdk/avatar.js +23 -0
  63. package/kits/base/sdk/castle.d.ts +9 -0
  64. package/kits/base/sdk/castle.js +5 -0
  65. package/kits/base/sdk/commands.d.ts +223 -0
  66. package/kits/base/sdk/commands.js +16 -0
  67. package/kits/base/sdk/context.d.ts +6 -0
  68. package/kits/base/sdk/experiments.d.ts +15 -0
  69. package/kits/base/sdk/experiments.js +83 -0
  70. package/kits/base/sdk/hub.d.ts +85 -0
  71. package/kits/base/sdk/hub.js +107 -0
  72. package/kits/base/sdk/multiplayer.d.ts +45 -1
  73. package/kits/base/sdk/multiplayer.js +158 -8
  74. package/kits/base/sdk/save.d.ts +197 -0
  75. package/kits/base/sdk/save.js +525 -0
  76. package/kits/base/sdk/saveCoordinator.d.ts +39 -0
  77. package/kits/base/sdk/saveCoordinator.js +252 -0
  78. package/kits/base/sdk/server/hub.d.ts +169 -0
  79. package/kits/base/sdk/server/hub.js +125 -0
  80. package/kits/base/sdk/server/platformHandle.d.ts +42 -1
  81. package/kits/base/sdk/server/storage.d.ts +58 -4
  82. package/kits/base/sdk/server/storage.js +25 -5
  83. package/kits/base/sdk/server/wrapper.d.ts +36 -2
  84. package/kits/base/sdk/server/wrapper.js +92 -11
  85. package/kits/base/sdk/store.d.ts +3 -2
  86. package/kits/base/sdk/store.js +5 -0
  87. package/kits/base/sdk/transport.d.ts +2 -1
  88. package/kits/base/sdk/transport.js +13 -1
  89. package/kits/hub-game/.prettierrc +5 -0
  90. package/kits/hub-game/CLAUDE.md +149 -0
  91. package/kits/hub-game/blueprints/avatar-0.scene +28 -0
  92. package/kits/hub-game/blueprints/avatar-1.scene +28 -0
  93. package/kits/hub-game/blueprints/avatar-2.scene +28 -0
  94. package/kits/hub-game/blueprints/avatar-3.scene +28 -0
  95. package/kits/hub-game/blueprints/bean/avatar-0.scene +28 -0
  96. package/kits/hub-game/blueprints/bean/avatar-1.scene +28 -0
  97. package/kits/hub-game/blueprints/bean/avatar-2.scene +28 -0
  98. package/kits/hub-game/blueprints/bean/avatar-3.scene +28 -0
  99. package/kits/hub-game/blueprints/blocky/avatar-0.scene +28 -0
  100. package/kits/hub-game/blueprints/blocky/avatar-1.scene +28 -0
  101. package/kits/hub-game/blueprints/blocky/avatar-2.scene +28 -0
  102. package/kits/hub-game/blueprints/blocky/avatar-3.scene +28 -0
  103. package/kits/hub-game/blueprints/pixel/avatar-0.scene +28 -0
  104. package/kits/hub-game/blueprints/pixel/avatar-1.scene +28 -0
  105. package/kits/hub-game/blueprints/pixel/avatar-2.scene +28 -0
  106. package/kits/hub-game/blueprints/pixel/avatar-3.scene +28 -0
  107. package/kits/hub-game/castle.json +96 -0
  108. package/kits/hub-game/code/client/attention.js +133 -0
  109. package/kits/hub-game/code/client/avatar.js +288 -0
  110. package/kits/hub-game/code/client/cabinets.js +90 -0
  111. package/kits/hub-game/code/client/chat.js +135 -0
  112. package/kits/hub-game/code/client/chrome.js +1035 -0
  113. package/kits/hub-game/code/client/doorFrame.js +15 -0
  114. package/kits/hub-game/code/client/firstJoin.js +14 -0
  115. package/kits/hub-game/code/client/grab.js +376 -0
  116. package/kits/hub-game/code/client/hookLayer.js +236 -0
  117. package/kits/hub-game/code/client/idle.js +326 -0
  118. package/kits/hub-game/code/client/keys.js +48 -0
  119. package/kits/hub-game/code/client/pack.js +232 -0
  120. package/kits/hub-game/code/client/packFormat.js +397 -0
  121. package/kits/hub-game/code/client/say.js +127 -0
  122. package/kits/hub-game/code/client/sha256.js +80 -0
  123. package/kits/hub-game/code/client/sound.js +147 -0
  124. package/kits/hub-game/code/client/state.js +143 -0
  125. package/kits/hub-game/code/client/talk.js +233 -0
  126. package/kits/hub-game/code/client/tiers.js +480 -0
  127. package/kits/hub-game/code/client/together.js +440 -0
  128. package/kits/hub-game/code/client/wear.js +149 -0
  129. package/kits/hub-game/code/server/actors.js +627 -0
  130. package/kits/hub-game/code/server/blocks.js +103 -0
  131. package/kits/hub-game/code/server/cabinets.js +309 -0
  132. package/kits/hub-game/code/server/game.js +137 -0
  133. package/kits/hub-game/code/server/grab.js +248 -0
  134. package/kits/hub-game/code/server/history.js +96 -0
  135. package/kits/hub-game/code/server/hub.js +1998 -0
  136. package/kits/hub-game/code/server/idle.js +412 -0
  137. package/kits/hub-game/code/server/index.js +64 -0
  138. package/kits/hub-game/code/server/labels.js +59 -0
  139. package/kits/hub-game/code/server/manifest.js +554 -0
  140. package/kits/hub-game/code/server/npcs.js +733 -0
  141. package/kits/hub-game/code/server/party.js +439 -0
  142. package/kits/hub-game/code/server/place.js +43 -0
  143. package/kits/hub-game/code/server/say.js +83 -0
  144. package/kits/hub-game/code/server/wear.js +142 -0
  145. package/kits/hub-game/code/systems/hub.js +2347 -0
  146. package/kits/hub-game/docs/content-pack.md +151 -0
  147. package/kits/hub-game/docs/contract.md +1209 -0
  148. package/kits/hub-game/docs/style.md +137 -0
  149. package/kits/hub-game/drawings/blocky/castle-eye.sprite +58 -0
  150. package/kits/hub-game/drawings/blocky/dash.sprite +59 -0
  151. package/kits/hub-game/drawings/blocky/pom.sprite +59 -0
  152. package/kits/hub-game/drawings/blocky/tabby.sprite +59 -0
  153. package/kits/hub-game/drawings/pixel/castle-arm.sprite +13 -0
  154. package/kits/hub-game/drawings/pixel/castle-body.sprite +23 -0
  155. package/kits/hub-game/drawings/pixel/castle-leg.sprite +12 -0
  156. package/kits/hub-game/drawings/pixel/kai-arm.sprite +14 -0
  157. package/kits/hub-game/drawings/pixel/kai-head.sprite +24 -0
  158. package/kits/hub-game/drawings/pixel/kai-leg.sprite +15 -0
  159. package/kits/hub-game/drawings/pixel/kai-torso.sprite +18 -0
  160. package/kits/hub-game/drawings/pixel/mimi-arm.sprite +13 -0
  161. package/kits/hub-game/drawings/pixel/mimi-head.sprite +23 -0
  162. package/kits/hub-game/drawings/pixel/mimi-leg.sprite +15 -0
  163. package/kits/hub-game/drawings/pixel/mimi-torso.sprite +18 -0
  164. package/kits/hub-game/drawings/pixel/nox-arm.sprite +13 -0
  165. package/kits/hub-game/drawings/pixel/nox-head.sprite +21 -0
  166. package/kits/hub-game/drawings/pixel/nox-leg.sprite +14 -0
  167. package/kits/hub-game/drawings/pixel/nox-torso.sprite +18 -0
  168. package/kits/hub-game/eslint.config.js +20 -0
  169. package/kits/hub-game/fonts.generated.js +9 -0
  170. package/kits/hub-game/index.html +22 -0
  171. package/kits/hub-game/main.jsx +6 -0
  172. package/kits/hub-game/models/avatar-0.pxmodel +21 -0
  173. package/kits/hub-game/models/avatar-1.pxmodel +24 -0
  174. package/kits/hub-game/models/avatar-2.pxmodel +27 -0
  175. package/kits/hub-game/models/avatar-3.pxmodel +22 -0
  176. package/kits/hub-game/models/bean/avatar-0.pxmodel +19 -0
  177. package/kits/hub-game/models/bean/avatar-1.pxmodel +24 -0
  178. package/kits/hub-game/models/bean/avatar-2.pxmodel +22 -0
  179. package/kits/hub-game/models/bean/avatar-3.pxmodel +24 -0
  180. package/kits/hub-game/models/blocky/avatar-0.pxmodel +21 -0
  181. package/kits/hub-game/models/blocky/avatar-1.pxmodel +30 -0
  182. package/kits/hub-game/models/blocky/avatar-2.pxmodel +28 -0
  183. package/kits/hub-game/models/blocky/avatar-3.pxmodel +27 -0
  184. package/kits/hub-game/models/pixel/avatar-0.pxmodel +24 -0
  185. package/kits/hub-game/models/pixel/avatar-1.pxmodel +16 -0
  186. package/kits/hub-game/models/pixel/avatar-2.pxmodel +19 -0
  187. package/kits/hub-game/models/pixel/avatar-3.pxmodel +23 -0
  188. package/kits/hub-game/package-lock.json +365 -0
  189. package/kits/hub-game/package.json +32 -0
  190. package/kits/hub-game/scenes/main.scene +318 -0
  191. package/kits/hub-game/scripts/check.mjs +339 -0
  192. package/kits/hub-game/scripts/fixtures/sdk-stub.mjs +3 -0
  193. package/kits/hub-game/scripts/gen-avatars-bean.mjs +748 -0
  194. package/kits/hub-game/scripts/gen-avatars-blocky.mjs +607 -0
  195. package/kits/hub-game/scripts/gen-avatars-pixel.mjs +771 -0
  196. package/kits/hub-game/scripts/gen-avatars.mjs +621 -0
  197. package/kits/hub-game/scripts/pack.mjs +192 -0
  198. package/kits/hub-game/scripts/test-actors.mjs +666 -0
  199. package/kits/hub-game/scripts/test-attention.mjs +103 -0
  200. package/kits/hub-game/scripts/test-avatar.mjs +472 -0
  201. package/kits/hub-game/scripts/test-ban-join.mjs +194 -0
  202. package/kits/hub-game/scripts/test-blocks.mjs +351 -0
  203. package/kits/hub-game/scripts/test-chat.mjs +98 -0
  204. package/kits/hub-game/scripts/test-classic.mjs +590 -0
  205. package/kits/hub-game/scripts/test-door-jump.mjs +131 -0
  206. package/kits/hub-game/scripts/test-door.mjs +56 -0
  207. package/kits/hub-game/scripts/test-grab-client.mjs +328 -0
  208. package/kits/hub-game/scripts/test-grab.mjs +331 -0
  209. package/kits/hub-game/scripts/test-hub-ops.mjs +835 -0
  210. package/kits/hub-game/scripts/test-hub-trip.mjs +1518 -0
  211. package/kits/hub-game/scripts/test-idle.mjs +518 -0
  212. package/kits/hub-game/scripts/test-interest.mjs +413 -0
  213. package/kits/hub-game/scripts/test-keys.mjs +50 -0
  214. package/kits/hub-game/scripts/test-manifest.mjs +270 -0
  215. package/kits/hub-game/scripts/test-no-forms.mjs +16 -0
  216. package/kits/hub-game/scripts/test-npcs.mjs +724 -0
  217. package/kits/hub-game/scripts/test-pack.mjs +424 -0
  218. package/kits/hub-game/scripts/test-party.mjs +369 -0
  219. package/kits/hub-game/scripts/test-say.mjs +149 -0
  220. package/kits/hub-game/scripts/test-tiers.mjs +666 -0
  221. package/kits/hub-game/scripts/test-together.mjs +215 -0
  222. package/kits/hub-game/theme.style +3 -0
  223. package/kits/multiplayer-3d/castle.json +5 -5
  224. package/kits/multiplayer-3d/code/client/avatars.js +101 -1
  225. package/kits/multiplayer-3d/code/server/players.js +4 -1
  226. package/kits/multiplayer-3d/code/server/sceneBodies.js +27 -6
  227. package/kits/multiplayer-3d/code/server/world.js +20 -7
  228. package/kits/multiplayer-3d/code/systems/multiplayer.js +222 -11
  229. package/kits/physics-2d/CLAUDE.md +2 -0
  230. package/kits/physics-2d/castle.json +2 -2
  231. package/kits/physics-2d/engine/art.js +13 -0
  232. package/kits/physics-2d/engine/assets.js +4 -2
  233. package/kits/physics-2d/engine/scene.js +117 -5
  234. package/kits/physics-2d/package.json +1 -1
  235. package/kits/physics-2d/scripts/test-skip-unchanged.mjs +254 -0
  236. package/kits/physics-3d/CLAUDE.md +285 -1
  237. package/kits/physics-3d/behaviors/Player.jsx +74 -1
  238. package/kits/physics-3d/castle.json +3 -3
  239. package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +33 -5
  240. package/kits/physics-3d/engine3d/actornode.js +253 -0
  241. package/kits/physics-3d/engine3d/artobjects.js +2 -2
  242. package/kits/physics-3d/engine3d/materials.js +29 -1
  243. package/kits/physics-3d/engine3d/nearshadows.js +600 -0
  244. package/kits/physics-3d/engine3d/unchanged.js +153 -0
  245. package/kits/physics-3d/engine3d/world3d.js +527 -5
  246. package/kits/physics-3d/package.json +1 -1
  247. package/kits/physics-3d/scripts/test-camera-follow.mjs +533 -0
  248. package/kits/physics-3d/scripts/test-near-shadows.mjs +1073 -0
  249. package/kits/physics-3d/scripts/test-static-actors.mjs +662 -0
  250. package/kits/physics-3d/scripts/test-static-passes.mjs +459 -0
  251. package/kits/physics-3d/scripts/test-static-physics.mjs +128 -0
  252. package/kits/physics-3d/systems/physics3d.js +187 -5
  253. package/kits/real-time/castle.json +2 -2
  254. package/kits/real-time/code/client/connection.js +77 -7
  255. package/kits/real-time/code/client/gameHooks.js +63 -3
  256. package/kits/real-time/code/client/messages.js +7 -0
  257. package/kits/real-time/code/server/gameHooks.js +24 -0
  258. package/kits/real-time/code/server/session.js +390 -22
  259. package/package.json +2 -2
@@ -39,6 +39,9 @@ export function Scene3DPlayer({
39
39
  // camera angle live here so they cross scene switches.
40
40
  runtime.persistent = {};
41
41
  runtime.currentScenePath = scenePath ?? 'scenes/main.scene';
42
+ // This loop applies `requestedContent` with `requestedScene` (another
43
+ // deck's content, mounted as data); a caller checks before asking.
44
+ runtime.mountsContent = true;
42
45
 
43
46
  const detachInput = attachInput(canvas, runtime, world);
44
47
  const stopResize = attachResize(canvas, world);
@@ -205,15 +208,13 @@ function startLoop(runtime, world, sprites, hudRefs, onFirstFrame, onError) {
205
208
  // registered systems -- the rapier world frees and lazily rebuilds --
206
209
  // while `runtime.persistent` carries the player's cross-scene state.
207
210
  if (runtime.requestedScene) {
208
- const next = runtime.requestedScene;
209
- runtime.requestedScene = null;
210
- runtime.loadFromFile(next);
211
+ const next = loadRequestedScene(runtime);
211
212
  runtime.currentScenePath = next;
212
213
  runtime.persistent.sceneChangeGraceUntil = runtime.time + 1;
213
214
  }
214
- syncActors(world, runtime, sprites, dt);
215
+ syncActors(world, runtime, runtime.sprites ?? sprites, dt);
215
216
  if (world.debugLines.visible) updatePhysicsDebug(world, runtime.physics3d?.debugRender());
216
- renderWorld(world);
217
+ renderWorld(world, runtime);
217
218
  updateHud(runtime, hudRefs);
218
219
  } catch (err) {
219
220
  onError?.(err);
@@ -229,6 +230,33 @@ function startLoop(runtime, world, sprites, hudRefs, onFirstFrame, onError) {
229
230
  return () => cancelAnimationFrame(raf);
230
231
  }
231
232
 
233
+ // Load the scene a behavior asked for, with the content it comes from when
234
+ // that is another deck's (a hub content pack; `null` puts this deck's own
235
+ // back). Content that fails to load puts the scene that was running back and
236
+ // leaves the error on `runtime.mountError` for the asker. Returns the path now
237
+ // loaded.
238
+ function loadRequestedScene(runtime) {
239
+ const next = runtime.requestedScene;
240
+ runtime.requestedScene = null;
241
+ if (runtime.requestedContent === undefined) {
242
+ runtime.loadFromFile(next);
243
+ return next;
244
+ }
245
+ const before = { content: runtime.content ?? null, path: runtime.currentScenePath };
246
+ runtime.setContent(runtime.requestedContent);
247
+ runtime.requestedContent = undefined;
248
+ try {
249
+ runtime.loadFromFile(next);
250
+ return next;
251
+ } catch (err) {
252
+ console.error('[engine] content failed to load; staying where we were', err);
253
+ runtime.setContent(before.content);
254
+ runtime.loadFromFile(before.path);
255
+ runtime.mountError = err;
256
+ return before.path;
257
+ }
258
+ }
259
+
232
260
  // Behaviors write `scene.hud = { score, look }`; the loop pushes it into the
233
261
  // overlay divs imperatively (no react re-render at frame rate).
234
262
  function updateHud(runtime, hudRefs) {
@@ -0,0 +1,253 @@
1
+ // The terms of `"engine": { "skipUnchangedActors": true }`:
2
+ //
3
+ // With the key absent, a scene runs the engine exactly as before. With the key
4
+ // present, each frame equals what the engine would have produced without it
5
+ // PROVIDED the scene's components are plain stored data (no accessors, Proxies,
6
+ // toJSON, custom iterators) and three.js objects under actors are changed only
7
+ // through position/rotation/quaternion/scale/visible/parenting. A scene that does
8
+ // not meet that is unsupported under the key and must not set it.
9
+ //
10
+ // The three.js group an actor is drawn under in a scene that set
11
+ // `"engine": { "skipUnchangedActors": true }`. It is a THREE.Group whose
12
+ // `updateMatrixWorld` does nothing when doing it would change nothing.
13
+ //
14
+ // three.js recomputes the local and the world matrix of every object under
15
+ // the scene on every render, whether or not anything moved. For a scene of
16
+ // several hundred actors that stand still that is most of a frame's script
17
+ // time. An ActorNode remembers, as numbers, what its subtree's matrices were
18
+ // last computed from and what they came out as: its parent's id and world
19
+ // matrix, the ids of the objects under it in order, each one's position,
20
+ // quaternion and scale, and each one's world matrix. While all of that reads
21
+ // the same, the matrices three.js would compute are the ones already there,
22
+ // so the walk is skipped. The moment any of it differs, by anyone's hand,
23
+ // three.js's own `updateMatrixWorld` runs for the whole subtree, exactly as it
24
+ // always did. That covers a world matrix written directly, a child moved to
25
+ // another parent and back between two renders, and a transform changed, used
26
+ // and put back: each leaves a matrix that is not the remembered one.
27
+ //
28
+ // Nothing about the objects is changed: `matrixAutoUpdate` stays on, so every
29
+ // other three.js path (`updateWorldMatrix`, a clone, a child moved under
30
+ // another parent) behaves as it does under a plain Group. A subtree holding
31
+ // anything that manages its own matrices (`matrixAutoUpdate` or
32
+ // `matrixWorldAutoUpdate` off, an `updateMatrix` or `updateMatrixWorld` of its
33
+ // own such as a camera's or a skinned mesh's, an object of another copy of
34
+ // three.js) is never skipped.
35
+ //
36
+ // `skipping` is the world's `{ on }`, shared by every node of the world: while
37
+ // it is off (the scene now playing did not ask), a node left from a scene
38
+ // that did is a plain Group again, after one test, and drops its record.
39
+ import * as THREE from 'three';
40
+
41
+ const baseUpdateMatrix = THREE.Object3D.prototype.updateMatrix;
42
+ const baseUpdateMatrixWorld = THREE.Object3D.prototype.updateMatrixWorld;
43
+
44
+ // Numbers remembered per object: position, quaternion and scale, then the
45
+ // local matrix and the world matrix they came out as.
46
+ const INPUTS = 10;
47
+ const STRIDE = INPUTS + 16;
48
+
49
+ const OFF = { on: false };
50
+ const NO_PARENT = -1;
51
+
52
+ export class ActorNode extends THREE.Group {
53
+ constructor(skipping = OFF) {
54
+ super();
55
+ this.skipping = skipping ?? OFF;
56
+ // What the subtree's matrices were last computed from, or null when they
57
+ // must be computed again whatever reads the same.
58
+ this.computedFrom = null;
59
+ }
60
+
61
+ updateMatrixWorld(force) {
62
+ // While the scene playing did not ask it is Group's method, after one
63
+ // test; a record left from a scene that did is dropped on the way.
64
+ if (!this.skipping.on) {
65
+ if (this.computedFrom !== null) this.computedFrom = null;
66
+ super.updateMatrixWorld(force);
67
+ return;
68
+ }
69
+ const from = this.computedFrom;
70
+ if (from !== null && unchanged(this, from)) {
71
+ return;
72
+ }
73
+ super.updateMatrixWorld(force);
74
+ this.computedFrom = remember(this, from);
75
+ }
76
+ }
77
+
78
+ // The record (`computedFrom`) holds numbers and nothing else: the parent's id
79
+ // and world matrix, and for each object of the subtree, in the order three.js
80
+ // walks it, its id, how many objects its own subtree has, and its position,
81
+ // quaternion, scale and world matrix. It refers to no object, so it can keep
82
+ // nothing alive, whoever holds the node and for however long.
83
+
84
+ // Where the walk below has got to in the record.
85
+ let at = 0;
86
+
87
+ // Whether three.js's walk of the subtree would leave every matrix as it is.
88
+ function unchanged(node, from) {
89
+ if (!sameParent(node.parent, from)) {
90
+ return false;
91
+ }
92
+ at = 0;
93
+ return sameSubtree(node, true, from) && at === from.ids.length;
94
+ }
95
+
96
+ // Whether `object` is the next object of the record, reads as recorded, and
97
+ // has under it exactly the subtree recorded for it.
98
+ function sameSubtree(object, isRoot, from) {
99
+ const index = at;
100
+ if (index >= from.ids.length || from.ids[index] !== object.id) {
101
+ return false;
102
+ }
103
+ if (
104
+ !computesItself(object, isRoot) ||
105
+ object.matrixWorldNeedsUpdate !== false ||
106
+ !sameNumbers(object, from.numbers, index * STRIDE)
107
+ ) {
108
+ return false;
109
+ }
110
+ at = index + 1;
111
+ const children = object.children;
112
+ for (let c = 0; c < children.length; c++) {
113
+ if (!sameSubtree(children[c], false, from)) {
114
+ return false;
115
+ }
116
+ }
117
+ return at === index + from.sizes[index];
118
+ }
119
+
120
+ function sameParent(parent, from) {
121
+ if ((parent === null ? NO_PARENT : parent.id) !== from.parentId) {
122
+ return false;
123
+ }
124
+ if (parent === null) {
125
+ return true;
126
+ }
127
+ const now = parent.matrixWorld.elements;
128
+ const was = from.parentWorld;
129
+ for (let i = 0; i < 16; i++) {
130
+ if (!Object.is(now[i], was[i])) {
131
+ return false;
132
+ }
133
+ }
134
+ return true;
135
+ }
136
+
137
+ // `Object.is`, not `===`: a matrix composed from -0 differs from one composed
138
+ // from 0 in the sign of its zeros, and the two must not pass for each other.
139
+ function sameNumbers(object, numbers, k) {
140
+ const p = object.position;
141
+ const q = object.quaternion;
142
+ const s = object.scale;
143
+ return (
144
+ sameMatrix(object.matrixWorld.elements, numbers, k + INPUTS) &&
145
+ Object.is(p.x, numbers[k]) &&
146
+ Object.is(p.y, numbers[k + 1]) &&
147
+ Object.is(p.z, numbers[k + 2]) &&
148
+ Object.is(q.x, numbers[k + 3]) &&
149
+ Object.is(q.y, numbers[k + 4]) &&
150
+ Object.is(q.z, numbers[k + 5]) &&
151
+ Object.is(q.w, numbers[k + 6]) &&
152
+ Object.is(s.x, numbers[k + 7]) &&
153
+ Object.is(s.y, numbers[k + 8]) &&
154
+ Object.is(s.z, numbers[k + 9])
155
+ );
156
+ }
157
+
158
+ // Whether the object's world matrix is still the one the last walk left.
159
+ function sameMatrix(world, numbers, k) {
160
+ for (let i = 0; i < 16; i++) {
161
+ if (!Object.is(world[i], numbers[k + i])) {
162
+ return false;
163
+ }
164
+ }
165
+ return true;
166
+ }
167
+
168
+ // An object whose matrices three.js's plain walk computes from its position,
169
+ // quaternion and scale, and nothing else.
170
+ function computesItself(object, isRoot) {
171
+ return (
172
+ object.matrixAutoUpdate === true &&
173
+ object.matrixWorldAutoUpdate === true &&
174
+ object.updateMatrix === baseUpdateMatrix &&
175
+ (isRoot || object.updateMatrixWorld === baseUpdateMatrixWorld)
176
+ );
177
+ }
178
+
179
+ function everyObjectComputesItself(object, isRoot) {
180
+ if (!computesItself(object, isRoot)) {
181
+ return false;
182
+ }
183
+ const children = object.children;
184
+ for (let i = 0; i < children.length; i++) {
185
+ if (!everyObjectComputesItself(children[i], false)) {
186
+ return false;
187
+ }
188
+ }
189
+ return true;
190
+ }
191
+
192
+ function countObjects(object) {
193
+ let count = 1;
194
+ const children = object.children;
195
+ for (let i = 0; i < children.length; i++) {
196
+ count += countObjects(children[i]);
197
+ }
198
+ return count;
199
+ }
200
+
201
+ // What the walk that just ran computed the subtree from. `from` is the last
202
+ // record; its arrays are written over when the subtree has as many objects as
203
+ // before, so an actor that moves every frame allocates nothing.
204
+ function remember(node, from) {
205
+ if (!everyObjectComputesItself(node, true)) {
206
+ return null;
207
+ }
208
+ const count = countObjects(node);
209
+ const record =
210
+ from !== null && from.ids.length === count
211
+ ? from
212
+ : {
213
+ parentId: NO_PARENT,
214
+ parentWorld: new Float64Array(16),
215
+ ids: new Int32Array(count),
216
+ sizes: new Int32Array(count),
217
+ numbers: new Float64Array(count * STRIDE),
218
+ };
219
+ record.parentId = node.parent === null ? NO_PARENT : node.parent.id;
220
+ if (node.parent !== null) {
221
+ record.parentWorld.set(node.parent.matrixWorld.elements);
222
+ }
223
+ at = 0;
224
+ write(node, record);
225
+ return record;
226
+ }
227
+
228
+ function write(object, record) {
229
+ const index = at;
230
+ at = index + 1;
231
+ const { numbers } = record;
232
+ const k = index * STRIDE;
233
+ const p = object.position;
234
+ const q = object.quaternion;
235
+ const s = object.scale;
236
+ record.ids[index] = object.id;
237
+ numbers[k] = p.x;
238
+ numbers[k + 1] = p.y;
239
+ numbers[k + 2] = p.z;
240
+ numbers[k + 3] = q.x;
241
+ numbers[k + 4] = q.y;
242
+ numbers[k + 5] = q.z;
243
+ numbers[k + 6] = q.w;
244
+ numbers[k + 7] = s.x;
245
+ numbers[k + 8] = s.y;
246
+ numbers[k + 9] = s.z;
247
+ numbers.set(object.matrixWorld.elements, k + INPUTS);
248
+ const children = object.children;
249
+ for (let c = 0; c < children.length; c++) {
250
+ write(children[c], record);
251
+ }
252
+ record.sizes[index] = at - index;
253
+ }
@@ -7,7 +7,7 @@
7
7
  // change a part's materials after they are built.
8
8
 
9
9
  import * as THREE from 'three';
10
- import { makeMaterial, resolveArtTexture } from './materials';
10
+ import { makeMaterial, resolveArtTexture, trackDerivedTexture } from './materials';
11
11
  import { needsModelGeometry } from './partgeometry';
12
12
 
13
13
  const DEG = Math.PI / 180;
@@ -117,7 +117,7 @@ function specTexture(spec, part, sprites) {
117
117
  texture.center.set(0.5, 0.5);
118
118
  texture.rotation = rotation * DEG;
119
119
  texture.needsUpdate = true;
120
- variants.set(key, texture);
120
+ variants.set(key, trackDerivedTexture(base, texture));
121
121
  }
122
122
  return texture;
123
123
  }
@@ -13,6 +13,20 @@ import { renderArtFrame, artReady, artFrameCount } from '@imports/castle.physics
13
13
  // Baked textures cached per art object per frame (sprites are re-parsed only
14
14
  // when the file changes, so object identity is a good key).
15
15
  const textureCache = new WeakMap();
16
+ // Textures cloned from a cached one (tiling here, per-part variants in
17
+ // artobjects.js), by the texture they came from, so releasing an art's
18
+ // textures releases these too.
19
+ const derivedTextures = new WeakMap();
20
+
21
+ export function trackDerivedTexture(base, derived) {
22
+ let set = derivedTextures.get(base);
23
+ if (!set) {
24
+ set = new Set();
25
+ derivedTextures.set(base, set);
26
+ }
27
+ set.add(derived);
28
+ return derived;
29
+ }
16
30
 
17
31
  function albedoTexture(art, frame) {
18
32
  if (!art || !artReady(art)) return null;
@@ -35,6 +49,20 @@ function albedoTexture(art, frame) {
35
49
  return texture;
36
50
  }
37
51
 
52
+ // Dispose the textures baked for one art object, for art that is going away
53
+ // for good (a mounted content pack being dropped). A live mesh still using one
54
+ // would draw black, so callers release only art nothing draws any more.
55
+ export function releaseArtTextures(art) {
56
+ const byFrame = art ? textureCache.get(art) : null;
57
+ if (!byFrame) return;
58
+ for (const texture of byFrame.values()) {
59
+ for (const derived of derivedTextures.get(texture) ?? []) derived.dispose();
60
+ derivedTextures.delete(texture);
61
+ texture.dispose();
62
+ }
63
+ textureCache.delete(art);
64
+ }
65
+
38
66
  // 'drawings/door.sprite#1' -> { path, frame }
39
67
  function parseArtRef(ref) {
40
68
  const hash = ref.lastIndexOf('#');
@@ -56,7 +84,7 @@ export function resolveArtTexture(ref, sprites, repeat) {
56
84
  tiled.wrapT = THREE.RepeatWrapping;
57
85
  tiled.repeat.set(repeat[0] ?? 1, repeat[1] ?? 1);
58
86
  tiled.needsUpdate = true;
59
- return tiled;
87
+ return trackDerivedTexture(texture, tiled);
60
88
  }
61
89
 
62
90
  function artRefsOf(shape) {