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
@@ -0,0 +1,533 @@
1
+ // The Player's follow camera, with and without `"engine": { "camera": {
2
+ // "follow": "rigid" } }`, on frames of uneven length (1/60, 1/30, 1/60, ...,
3
+ // the pattern of a page that misses every other vsync).
4
+ //
5
+ // The real Player behavior drives a stand-in body that the stand-in physics
6
+ // steps the way systems/physics3d.js does: a fixed 1/60 step from an
7
+ // accumulator, the position written back blended between the last two steps,
8
+ // behaviors before physics, and `placeFollowCamera` last. What is measured is
9
+ // the camera-minus-player vector on each drawn frame (the player where the
10
+ // frame draws it): how much it shakes is `jerkRms` below.
11
+ //
12
+ // - Without the key the camera is where main's camera code puts it, number
13
+ // for number (`mainUpdateCamera` is main's `updateCamera`, copied).
14
+ // - With it a steady walk does not move the player on screen at all, a turn
15
+ // and a jump shake less than without it, and a teleport still snaps.
16
+ //
17
+ // test-static-actors.mjs runs it (`npm run check`).
18
+ // node scripts/test-camera-follow.mjs [--table]
19
+ import assert from 'node:assert/strict';
20
+ import { register } from 'node:module';
21
+ import * as THREE from 'three';
22
+
23
+ // How much the view shakes, two ways; swap either for another measure and
24
+ // every number below follows it. `frames` is [{ cam: {x,y,z}, player:
25
+ // {x,y,z}, dt }], one per drawn frame, and both measure the camera-minus-player
26
+ // vector `d` (`axes` picks its components: 0 x, 1 y, 2 z).
27
+ //
28
+ // `jerkRms`: the root mean square of d's second difference from one drawn
29
+ // frame to the next, in metres per frame per frame. A camera that trails a
30
+ // steady walk by a steady distance scores 0 whatever the frame lengths. It
31
+ // takes no account of time, so a turn that is perfectly smooth in time, drawn
32
+ // on frames of uneven length, scores high too.
33
+ export function jerkRms(frames, axes = [0, 1, 2]) {
34
+ const d = frames.map(({ cam, player }) => [cam.x - player.x, cam.y - player.y, cam.z - player.z]);
35
+ let sum = 0;
36
+ let n = 0;
37
+ for (let i = 1; i + 1 < d.length; i++) {
38
+ for (const k of axes) sum += (d[i + 1][k] - 2 * d[i][k] + d[i - 1][k]) ** 2;
39
+ n += 1;
40
+ }
41
+ return n ? Math.sqrt(sum / n) : 0;
42
+ }
43
+
44
+ // `shakeRms`: the same vector's acceleration in time (its second divided
45
+ // difference over each frame's real length, m/s^2), and the root mean square
46
+ // of how much that changes from one frame to the next. Motion that is smooth
47
+ // in time keeps its acceleration from frame to frame, however the frames are
48
+ // cut, so this is what separates a smooth turn from a juddering one.
49
+ export function shakeRms(frames, axes = [0, 1, 2]) {
50
+ const d = frames.map(({ cam, player }) => [cam.x - player.x, cam.y - player.y, cam.z - player.z]);
51
+ const accel = [];
52
+ for (let i = 1; i + 1 < d.length; i++) {
53
+ const h1 = frames[i].dt;
54
+ const h2 = frames[i + 1].dt;
55
+ accel.push(d[i].map((v, k) => (2 * ((d[i + 1][k] - v) / h2 - (v - d[i - 1][k]) / h1)) / (h1 + h2)));
56
+ }
57
+ let sum = 0;
58
+ for (let i = 1; i < accel.length; i++) for (const k of axes) sum += (accel[i][k] - accel[i - 1][k]) ** 2;
59
+ return accel.length > 1 ? Math.sqrt(sum / (accel.length - 1)) : 0;
60
+ }
61
+
62
+ // `screenJerk`: #881's measure. A point on the ground fixed 25 m ahead of
63
+ // the player when the walk starts is projected through the camera (1280x800,
64
+ // as its browser run), and the root mean square of its screen y's second
65
+ // difference per frame is taken, in its units (NDC y * 400, about pixels),
66
+ // over the frames on which the point is still 10 m or more ahead.
67
+ export function screenJerk(frames) {
68
+ const y = frames.filter((f) => f.ahead >= 10).map((f) => f.screenY);
69
+ let sum = 0;
70
+ for (let i = 1; i + 1 < y.length; i++) sum += (y[i + 1] - 2 * y[i] + y[i - 1]) ** 2;
71
+ return y.length > 2 ? Math.sqrt(sum / (y.length - 2)) : 0;
72
+ }
73
+
74
+ // `screenTime`: that point's acceleration on screen in time (its second
75
+ // divided difference over the frames' real lengths), in the same units per
76
+ // 1/60 s frame. A point moving steadily in time scores 0 here however the
77
+ // frames are cut; `screenJerk` scores it for the frames being uneven.
78
+ export function screenTime(frames) {
79
+ const f = frames.filter((x) => x.ahead >= 10);
80
+ let sum = 0;
81
+ for (let i = 1; i + 1 < f.length; i++) {
82
+ const a = (2 * ((f[i + 1].screenY - f[i].screenY) / f[i + 1].dt - (f[i].screenY - f[i - 1].screenY) / f[i].dt)) / (f[i].dt + f[i + 1].dt);
83
+ sum += (a / 3600) ** 2;
84
+ }
85
+ return f.length > 2 ? Math.sqrt(sum / (f.length - 2)) : 0;
86
+ }
87
+
88
+ const stub = `
89
+ const any = () => new Proxy(function () {}, {
90
+ get: (t, k) => (k === 'then' ? undefined : k === Symbol.toPrimitive ? () => 0 : any()),
91
+ apply: () => any(),
92
+ construct: () => any(),
93
+ });
94
+ export const init = async () => {};
95
+ export const World = any();
96
+ export const EventQueue = any();
97
+ export const RigidBodyDesc = any();
98
+ export const ColliderDesc = any();
99
+ export const RigidBodyType = { Dynamic: 0, Fixed: 1, KinematicPositionBased: 2, KinematicVelocityBased: 3 };
100
+ export const ActiveEvents = any();
101
+ export const ActiveCollisionTypes = any();
102
+ export const Ray = any();
103
+ export const Quaternion = any();
104
+ export const Vector3 = any();
105
+ `;
106
+ const hooks = `
107
+ import { existsSync, readFileSync } from 'node:fs';
108
+ import { fileURLToPath } from 'node:url';
109
+ export async function resolve(specifier, context, next) {
110
+ const parent = context.parentURL ?? '';
111
+ if (specifier === '@dimforge/rapier3d-compat') {
112
+ return { url: 'data:text/javascript,' + encodeURIComponent(${JSON.stringify(stub)}), shortCircuit: true };
113
+ }
114
+ if (specifier.startsWith('@imports/castle.physics-2d/')) {
115
+ specifier = parent.replace(/\\/kits\\/.*/, '/kits/physics-2d/') + specifier.slice(27);
116
+ }
117
+ if ((specifier.startsWith('.') || specifier.startsWith('file:')) && parent.includes('/kits/') && !/\\.(m?js|jsx|json)$/.test(specifier)) {
118
+ const base = new URL(specifier, parent).href;
119
+ for (const ext of ['.js', '.jsx']) if (existsSync(fileURLToPath(base + ext))) return next(base + ext, context);
120
+ }
121
+ return next(specifier, context);
122
+ }
123
+ export async function load(url, context, next) {
124
+ if (url.endsWith('/behaviors/Player.jsx')) {
125
+ return { format: 'module', source: readFileSync(fileURLToPath(url), 'utf8'), shortCircuit: true };
126
+ }
127
+ return next(url, context);
128
+ }`;
129
+ register(`data:text/javascript,${encodeURIComponent(hooks)}`);
130
+ const { Player } = await import('../behaviors/Player.jsx');
131
+ const { Physics3DSystem, placeFollowCamera } = await import('../systems/physics3d.js');
132
+
133
+ // main's `updateCamera`, as it was before the key.
134
+ const TELEPORT_PER_FRAME = 3;
135
+ function mainUpdateCamera(scene, t, rt, props, dt) {
136
+ const cam = scene.three?.camera;
137
+ if (!cam) return;
138
+ const targetX = t.x - props.camDistance * Math.sin(rt.camAngle);
139
+ const targetY = t.y + props.camHeight;
140
+ const targetZ = t.z + props.camDistance * Math.cos(rt.camAngle);
141
+ const last = rt.camLastAt;
142
+ const jumped = last && Math.hypot(t.x - last.x, t.z - last.z) > TELEPORT_PER_FRAME;
143
+ rt.camLastAt = { x: t.x, z: t.z };
144
+ if (jumped || Math.hypot(targetX - cam.position.x, targetZ - cam.position.z) > 20) {
145
+ cam.position.set(targetX, targetY, targetZ);
146
+ } else {
147
+ const lerp = 1 - Math.exp(-12 * dt);
148
+ cam.position.x += (targetX - cam.position.x) * lerp;
149
+ cam.position.y += (targetY - cam.position.y) * lerp;
150
+ cam.position.z += (targetZ - cam.position.z) * lerp;
151
+ }
152
+ cam.lookAt(t.x, t.y + 0.5, t.z);
153
+ }
154
+
155
+ const FIXED = 1 / 60;
156
+ const PROPS = { ...Player.defaultProps };
157
+ // The capsule's centre rests this high: the hover spring's height (Player.jsx
158
+ // applyHover: half the height plus 0.15) less a little it sags under gravity.
159
+ const FLOOR = 0.7;
160
+
161
+ // A world of one capsule on flat ground. `mode` is 'off' (no key), 'rigid'
162
+ // (the key) or 'main' (no key, and main's camera code placing the camera
163
+ // where Player.update would, from a Player that has no camera of its own).
164
+ function makeWorld(mode) {
165
+ const camera = new THREE.PerspectiveCamera(50, 1280 / 800, 0.1, 1000);
166
+ camera.position.set(8, 8, 8);
167
+ camera.lookAt(0, 0, 0);
168
+ const body = { pos: { x: 0, y: 1, z: 0 }, vel: { x: 0, y: 0, z: 0 }, prev: null, acc: 0 };
169
+ const actor = {
170
+ id: 'player',
171
+ components: { Transform: { x: 0, y: 1, z: 0, rotationY: 0 }, Shape: { radius: 0.35, height: 1.4 }, Player: PROPS },
172
+ runtime: {},
173
+ };
174
+ const phys = {
175
+ ready: () => world.ready,
176
+ mass: () => 1,
177
+ castRay: (from, dir, max) => (from.y <= max ? { distance: from.y } : null),
178
+ getVelocity: () => ({ ...body.vel }),
179
+ setVelocity: (_, v) => Object.assign(body.vel, v),
180
+ applyImpulse: (_, i) => {
181
+ body.vel.x += i.x;
182
+ body.vel.y += i.y;
183
+ body.vel.z += i.z;
184
+ },
185
+ };
186
+ const scene = {
187
+ physics3d: phys,
188
+ keys: new Set(),
189
+ moveInput: null,
190
+ persistent: {},
191
+ three: { camera: mode === 'main' ? null : camera },
192
+ cameraFollowsRigidly: mode === 'rigid',
193
+ actorsWith: () => [],
194
+ data: {},
195
+ actors: [actor],
196
+ getActor: (id) => scene.actors.find((a) => a.id === id),
197
+ };
198
+ const t = actor.components.Transform;
199
+ const frames = [];
200
+ // `ready` is the physics' readiness; `callback` runs where the physics
201
+ // system runs collision callbacks, after the write-back and before the
202
+ // follow camera is placed.
203
+ const world = { scene, body, actor, camera, frames, ready: true, callback: null };
204
+ // Steps as physics3d.js `step` and `writeBack` do (gravityScale 2, as the
205
+ // hub's player has), with the ground as a floor the body cannot go through.
206
+ const stepPhysics = (dt) => {
207
+ body.acc += dt;
208
+ let steps = 0;
209
+ while (body.acc >= FIXED && steps < 4) {
210
+ body.prev = { ...body.pos };
211
+ body.vel.y += -9.81 * 2 * FIXED;
212
+ for (const k of ['x', 'y', 'z']) body.pos[k] += body.vel[k] * FIXED;
213
+ if (body.pos.y < FLOOR) {
214
+ body.pos.y = FLOOR;
215
+ body.vel.y = Math.max(0, body.vel.y);
216
+ }
217
+ body.acc -= FIXED;
218
+ steps += 1;
219
+ }
220
+ if (steps === 4) body.acc = 0;
221
+ const alpha = Math.max(0, Math.min(1, body.acc / FIXED));
222
+ const prev = body.prev ?? body.pos;
223
+ for (const k of ['x', 'y', 'z']) t[k] = prev[k] + (body.pos[k] - prev[k]) * alpha;
224
+ };
225
+ world.frame = (dt, keys = []) => {
226
+ scene.keys = new Set(keys);
227
+ new Player(PROPS).update(actor, scene, dt);
228
+ if (mode === 'main') mainUpdateCamera({ three: { camera } }, t, scene.persistent, PROPS, dt);
229
+ if (world.ready) stepPhysics(dt);
230
+ world.callback?.(scene);
231
+ world.callback = null;
232
+ placeFollowCamera(scene);
233
+ const p = camera.position;
234
+ camera.updateMatrixWorld();
235
+ const screenY = world.fixed ? new THREE.Vector3(world.fixed.x, 0, world.fixed.z).project(camera).y * 400 : 0;
236
+ const a = scene.persistent.camAngle;
237
+ const ahead = world.fixed ? (world.fixed.x - t.x) * Math.sin(a) - (world.fixed.z - t.z) * Math.cos(a) : 0;
238
+ frames.push({ screenY, ahead, cam: { x: p.x, y: p.y, z: p.z }, player: { x: t.x, y: t.y, z: t.z }, dt, q: camera.quaternion.toArray() });
239
+ };
240
+ world.teleport = (dx, dz) => {
241
+ body.pos.x += dx;
242
+ body.pos.z += dz;
243
+ body.prev = null;
244
+ };
245
+ return world;
246
+ }
247
+
248
+ const UNEVEN = (i) => (i % 2 === 0 ? 1 / 60 : 1 / 30);
249
+ const EVEN = () => 1 / 60;
250
+
251
+ // What each run does, frame by frame: the keys held on frame `i`. A second
252
+ // of standing still first, so the camera has settled behind the player.
253
+ const RUNS = {
254
+ walk: () => ['KeyW'],
255
+ turn: () => ['KeyW', 'KeyD'],
256
+ orbit: () => ['KeyD'], // turning on the spot
257
+ jump: (i) => (i % 50 === 0 ? ['KeyW', 'Space'] : ['KeyW']),
258
+ };
259
+ const SETTLE = 60;
260
+ const FRAMES = 240;
261
+
262
+ function play(mode, run, dtOf) {
263
+ const world = makeWorld(mode);
264
+ for (let i = 0; i < SETTLE; i++) world.frame(FIXED);
265
+ const { x, z } = world.actor.components.Transform;
266
+ const a = world.scene.persistent.camAngle;
267
+ world.fixed = { x: x + 25 * Math.sin(a), z: z - 25 * Math.cos(a) };
268
+ for (let i = 0; i < FRAMES; i++) world.frame(dtOf(i), RUNS[run](i));
269
+ // The first half second of the run is the camera catching up with a walk
270
+ // that just started; the shake is measured on the rest.
271
+ return { world, measured: world.frames.slice(SETTLE + 30) };
272
+ }
273
+
274
+ export async function run({ table = false } = {}) {
275
+ let checks = 0;
276
+ const rows = [];
277
+ for (const run of Object.keys(RUNS)) {
278
+ const row = { run };
279
+ for (const [pattern, dtOf] of [
280
+ ['uneven', UNEVEN],
281
+ ['even', EVEN],
282
+ ]) {
283
+ const main = play('main', run, dtOf);
284
+ const off = play('off', run, dtOf);
285
+ const rigid = play('rigid', run, dtOf);
286
+ // Without the key, every drawn frame is main's: camera position and
287
+ // orientation, number for number.
288
+ assert.deepEqual(
289
+ off.world.frames.map((f) => [f.cam, f.q]),
290
+ main.world.frames.map((f) => [f.cam, f.q]),
291
+ `${run}/${pattern}: without the key the camera is main's`
292
+ );
293
+ checks += 1;
294
+ for (const [mode, played] of [
295
+ ['main', main],
296
+ ['off', off],
297
+ ['rigid', rigid],
298
+ ]) {
299
+ row[`${pattern}.${mode}`] = {
300
+ jerk: jerkRms(played.measured),
301
+ shake: shakeRms(played.measured),
302
+ flat: jerkRms(played.measured, [0, 2]),
303
+ screen: screenJerk(played.measured),
304
+ screenTime: screenTime(played.measured),
305
+ };
306
+ }
307
+ // The player is where the camera looks, on the frame it is drawn.
308
+ const last = rigid.world.frames.at(-1);
309
+ const look = new THREE.Vector3(0, 0, -1).applyQuaternion(new THREE.Quaternion().fromArray(last.q));
310
+ const to = new THREE.Vector3(last.player.x - last.cam.x, last.player.y + 0.5 - last.cam.y, last.player.z - last.cam.z).normalize();
311
+ assert.ok(look.dot(to) > 1 - 1e-9, `${run}/${pattern}: the rigid camera looks at the drawn player`);
312
+ checks += 1;
313
+ }
314
+ rows.push(row);
315
+ }
316
+ const by = Object.fromEntries(rows.map((row) => [row.run, row]));
317
+ // A steady walk: the camera moves with the player along the ground,
318
+ // whatever the frames. What is left is the player's height, eased (the
319
+ // hover spring bobs a millimetre or two on uneven frames).
320
+ assert.ok(by.walk['uneven.main'].jerk > 0.005, `main shakes walking on uneven frames (${by.walk['uneven.main'].jerk})`);
321
+ for (const pattern of ['uneven', 'even']) {
322
+ const { flat, jerk, shake } = by.walk[`${pattern}.rigid`];
323
+ assert.ok(flat < 1e-9, `walk/${pattern}: the rigid camera does not move along the ground (${flat})`);
324
+ assert.ok(jerk <= by.walk[`${pattern}.main`].jerk / 3 + 1e-9, `walk/${pattern}: and shakes a third of main's or less (${jerk})`);
325
+ assert.ok(shake <= by.walk[`${pattern}.main`].shake / 3 + 1e-9, `walk/${pattern}: in time too (${shake})`);
326
+ checks += 3;
327
+ }
328
+ // On screen, #881's way: frames of uneven length move the ground ahead far
329
+ // less than main moves it (on even frames both are a perspective's own
330
+ // steady drift, well under a hundredth).
331
+ assert.ok(by.walk['uneven.rigid'].screen < by.walk['uneven.main'].screen / 3, `walk/uneven: on screen ${by.walk['uneven.rigid'].screen}`);
332
+ assert.ok(by.walk['uneven.rigid'].screenTime < by.walk['uneven.main'].screenTime / 3, `walk/uneven: on screen in time ${by.walk['uneven.rigid'].screenTime}`);
333
+ checks += 2;
334
+ // Turning, orbiting, jumping: no more shake in time than main has, on
335
+ // either pattern, and far less on uneven frames for a turn.
336
+ for (const run of ['turn', 'orbit', 'jump']) {
337
+ for (const pattern of ['uneven', 'even']) {
338
+ const rigid = by[run][`${pattern}.rigid`].shake;
339
+ const main = by[run][`${pattern}.main`].shake;
340
+ assert.ok(rigid <= main * 1.01 + 1e-6, `${run}/${pattern}: rigid ${rigid} shakes no more than main ${main}`);
341
+ checks += 1;
342
+ }
343
+ }
344
+ for (const run of ['turn', 'orbit']) {
345
+ assert.ok(by[run]['uneven.rigid'].shake < by[run]['uneven.main'].shake / 5, `${run}/uneven: far less than main`);
346
+ checks += 1;
347
+ }
348
+
349
+ // A teleport snaps: the next frame is placed exactly behind the player.
350
+ for (const mode of ['off', 'rigid']) {
351
+ const world = makeWorld(mode);
352
+ for (let i = 0; i < SETTLE; i++) world.frame(FIXED);
353
+ for (let i = 0; i < 30; i++) world.frame(UNEVEN(i), ['KeyW']);
354
+ world.teleport(40, -25);
355
+ world.frame(FIXED, ['KeyW']);
356
+ if (mode === 'off') world.frame(FIXED, ['KeyW']); // main's camera sees it a frame later
357
+ const f = world.frames.at(-1);
358
+ const a = world.scene.persistent.camAngle;
359
+ const want = { x: f.player.x - PROPS.camDistance * Math.sin(a), y: f.player.y + PROPS.camHeight, z: f.player.z + PROPS.camDistance * Math.cos(a) };
360
+ const err = Math.hypot(f.cam.x - want.x, f.cam.y - want.y, f.cam.z - want.z);
361
+ // main's snap is to where the player was when Player.update ran (a
362
+ // frame's walk behind); the rigid one is to where the frame draws it.
363
+ assert.ok(err < (mode === 'rigid' ? 1e-9 : 0.2), `${mode}: a teleport snaps the camera (${err} m off)`);
364
+ checks += 1;
365
+ }
366
+
367
+ // Something else placing the camera (a hub trip's reveal): the rigid
368
+ // camera goes on from there, eased, rather than jumping back.
369
+ {
370
+ const world = makeWorld('rigid');
371
+ for (let i = 0; i < SETTLE; i++) world.frame(FIXED);
372
+ const before = world.frames.at(-1).cam;
373
+ world.camera.position.set(before.x + 3, before.y + 2, before.z);
374
+ world.frame(FIXED);
375
+ const { cam: after, player } = world.frames.at(-1);
376
+ const a = world.scene.persistent.camAngle;
377
+ const target = { x: -PROPS.camDistance * Math.sin(a), y: PROPS.camHeight, z: PROPS.camDistance * Math.cos(a) };
378
+ const e = Math.exp(-12 * FIXED);
379
+ // Taken up from where it was put, relative to the player, and eased
380
+ // toward its place behind them as updateCamera's lerp eases (the player
381
+ // stands still; its height settles by a hair, hence the tolerance).
382
+ const moved = { x: 3, y: 2, z: 0 };
383
+ const was = world.frames.at(-2);
384
+ for (const k of ['x', 'y', 'z']) {
385
+ const put = was.cam[k] - was.player[k] + moved[k];
386
+ const want = target[k] + (put - target[k]) * e;
387
+ assert.ok(Math.abs(after[k] - player[k] - want) < 1e-4, `eased back from where it was put (${k}: ${after[k] - player[k] - want})`);
388
+ }
389
+ assert.ok(Math.hypot(after.x - before.x, after.y - before.y) > 2, 'not jumped back');
390
+ checks += 1;
391
+ }
392
+
393
+ // Where a follow camera is placed from, as of the end of the frame (review
394
+ // of the first version: the request held the player it was made for).
395
+ const behind = (world) => {
396
+ const t = world.actor.components.Transform;
397
+ const a = world.scene.persistent.camAngle;
398
+ return { x: t.x - PROPS.camDistance * Math.sin(a), y: t.y + PROPS.camHeight, z: t.z + PROPS.camDistance * Math.cos(a) };
399
+ };
400
+ const at = (camera) => ({ x: camera.position.x, y: camera.position.y, z: camera.position.z });
401
+ const settled = () => {
402
+ const world = makeWorld('rigid');
403
+ for (let i = 0; i < SETTLE; i++) world.frame(FIXED, ['KeyW']);
404
+ return world;
405
+ };
406
+ // A collision callback despawns the player: no camera from the player
407
+ // that is gone, and nothing written down for it.
408
+ {
409
+ const world = settled();
410
+ const before = at(world.camera);
411
+ const lastAt = world.scene.persistent.camLastAt;
412
+ world.callback = (scene) => {
413
+ scene.actors = [];
414
+ };
415
+ world.frame(FIXED, ['KeyW']);
416
+ assert.deepEqual(at(world.camera), before, 'a despawned player places no camera');
417
+ assert.equal(world.scene.persistent.camLastAt, lastAt, 'and writes nothing down');
418
+ checks += 1;
419
+ }
420
+ // A collision callback loads another keyed scene, whose player has the
421
+ // same id: the request was for the scene that is gone.
422
+ {
423
+ const world = settled();
424
+ const before = at(world.camera);
425
+ world.callback = (scene) => {
426
+ const other = { id: 'player', components: { Transform: { x: 50, y: 1, z: 50, rotationY: 0 }, Player: { ...PROPS } }, runtime: {} };
427
+ scene.data = { actors: [other] };
428
+ scene.actors = [other];
429
+ };
430
+ world.frame(FIXED, ['KeyW']);
431
+ assert.deepEqual(at(world.camera), before, 'a request does not cross a load');
432
+ const other = world.scene.actors[0];
433
+ new Player(other.components.Player).update(other, world.scene, FIXED);
434
+ placeFollowCamera(world.scene);
435
+ const want = behind({ actor: other, scene: world.scene });
436
+ assert.ok(Math.hypot(world.camera.position.x - want.x, world.camera.position.z - want.z) < 1e-9, "the new scene's player gets its camera the next frame (snapped: it is far)");
437
+ checks += 1;
438
+ }
439
+ // Physics not ready (rapier loading; and on the first frame after a load,
440
+ // before physics has made its world): the camera is placed every frame.
441
+ {
442
+ const world = makeWorld('rigid');
443
+ world.ready = false;
444
+ const t = world.actor.components.Transform;
445
+ // It starts where the world made it and is eased behind the player as
446
+ // on any first frames, following the player as it is moved.
447
+ for (let i = 0; i < 120; i++) {
448
+ const before = at(world.camera);
449
+ t.x += 0.1;
450
+ world.frame(FIXED);
451
+ assert.notDeepEqual(at(world.camera), before, `placed on unready frame ${i}`);
452
+ }
453
+ const want = behind(world);
454
+ assert.ok(Math.hypot(world.camera.position.x - want.x, world.camera.position.z - want.z) < 1e-6, 'and is behind the player');
455
+ checks += 1;
456
+ }
457
+
458
+ // The physics system places the camera at the end of its frame, with the
459
+ // key only, whether or not it could step; a load and a dispose drop a
460
+ // request, and so does a request for another scene's data.
461
+ {
462
+ const system = new Physics3DSystem();
463
+ system.attach({});
464
+ await new Promise((resolve) => setTimeout(resolve, 0));
465
+ const scene = (keyed, skip) => {
466
+ const actors = [{ id: 'p', components: { Transform: { x: 0, y: 1, z: 0 } }, runtime: {} }];
467
+ const s = { cameraFollowsRigidly: keyed, skipsUnchangedActors: skip, data: {}, actors, getActors: () => actors, getActor: (id) => actors.find((a) => a.id === id) };
468
+ s.placed = [];
469
+ s.ask = () => {
470
+ s.followCamera = { id: 'p', data: s.data, dt: FIXED, place: (sc, id, dt) => s.placed.push([sc, id, dt]) };
471
+ };
472
+ return s;
473
+ };
474
+ for (const [keyed, skip] of [
475
+ [true, false],
476
+ [true, true],
477
+ [false, false],
478
+ [false, true],
479
+ ]) {
480
+ const s = scene(keyed, skip);
481
+ s.ask();
482
+ system.afterBehaviors(s, FIXED);
483
+ assert.equal(s.placed.length, keyed ? 1 : 0, `placed with the key only (key ${keyed}, skipping ${skip})`);
484
+ assert.equal(s.followCamera, null, 'and the request is gone after the frame');
485
+ system.afterBehaviors(s, FIXED);
486
+ assert.equal(s.placed.length, keyed ? 1 : 0, 'once per request');
487
+ checks += 1;
488
+ }
489
+ {
490
+ const s = scene(true, false);
491
+ // The first frame after a load: the world is not made yet when the
492
+ // behaviors run.
493
+ system.reset(s);
494
+ assert.equal(s.physics3d, undefined);
495
+ s.ask();
496
+ system.afterBehaviors(s, FIXED);
497
+ assert.equal(s.placed.length, 1, 'placed on the first frame after a load');
498
+ s.ask();
499
+ system.reset(s);
500
+ assert.equal(s.followCamera, null, 'a load drops a request');
501
+ s.ask();
502
+ system.dispose(s);
503
+ assert.equal(s.followCamera, null, 'and so does a dispose');
504
+ s.ask();
505
+ s.data = {};
506
+ system.afterBehaviors(s, FIXED);
507
+ assert.equal(s.placed.length, 1, "a request for another scene's data is not placed");
508
+ checks += 1;
509
+ }
510
+ }
511
+
512
+ if (table) {
513
+ const fmt = (v, scale, w) => (v * scale).toFixed(w === 9 ? 3 : 1).padStart(w);
514
+ for (const [what, unit, scale, w] of [
515
+ ['jerk', 'jerk rms, cm/frame^2 (second difference per frame)', 100, 9],
516
+ ['shake', 'shake rms, m/s^2 per frame (change of acceleration in time)', 1, 9],
517
+ ['screen', "screen jerk rms, NDC y * 400 per frame^2 (#881's, a ground point 25 m ahead)", 1, 9],
518
+ ['screenTime', 'the same point in time, NDC y * 400 per (1/60 s)^2', 1, 9],
519
+ ]) {
520
+ console.log(`${unit}\n ${''.padEnd(8)}uneven: main off rigid | even: main off rigid`);
521
+ for (const row of rows) {
522
+ const cells = ['uneven', 'even'].map((p) => ['main', 'off', 'rigid'].map((m) => fmt(row[`${p}.${m}`][what], scale, w)).join(''));
523
+ console.log(` ${row.run.padEnd(15)}${cells[0]} | ${cells[1]}`);
524
+ }
525
+ }
526
+ }
527
+ return { checks, rows };
528
+ }
529
+
530
+ if (import.meta.url === `file://${process.argv[1]}`) {
531
+ const { checks } = await run({ table: true });
532
+ console.log(`camera follow: ${checks} checks`);
533
+ }