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,666 @@
1
+ // Server-declared actor motion and colour (contract §10, v1.1): `hub.move`
2
+ // drives a kinematic scene actor and refuses anything else or anything out
3
+ // of bounds; `hub.tint` paints a Shape actor under a per-second budget;
4
+ // `hub.actorsWith` hands back the actor's shape. `npm run check` runs it; it
5
+ // needs nothing installed.
6
+
7
+ import assert from 'node:assert/strict';
8
+ import { register } from 'node:module';
9
+
10
+ const sceneStub = 'export const readScene = () => globalThis.__hubTestActors ?? [];';
11
+ register(
12
+ `data:text/javascript,${encodeURIComponent(`
13
+ export async function resolve(specifier, context, next) {
14
+ if (specifier === '@imports/castle.multiplayer-3d/code/server/sceneBodies.js') {
15
+ return { url: 'data:text/javascript,${encodeURIComponent(sceneStub)}', shortCircuit: true };
16
+ }
17
+ return next(specifier, context);
18
+ }
19
+ `)}`,
20
+ );
21
+ const { layer } = await import('../code/server/hub.js');
22
+ const Actors = await import('../code/server/actors.js');
23
+ const { pieces } = await import('../code/client/wear.js');
24
+ const Wear = await import('../code/server/wear.js');
25
+
26
+ const box = { kind: 'box', width: 4, height: 0.5, depth: 2, color: '#808080' };
27
+ globalThis.__hubTestActors = [
28
+ { id: 'spawn', components: { Player: {}, Transform: { x: 0, y: 1, z: 0 } } },
29
+ {
30
+ id: 'sweeper',
31
+ components: { Transform: { x: 0, y: 1, z: 5, rotationY: 0 }, Shape: box, Body: { type: 'kinematic' }, HubHazard: { kind: 'sweep' } },
32
+ },
33
+ { id: 'wall', components: { Transform: { x: 3, y: 1, z: 0 }, Shape: box, Solid: {} } },
34
+ { id: 'zone-a', components: { Transform: { x: -6, y: 0, z: 0 }, Shape: { kind: 'cylinder', radius: 3, height: 0.1 }, HubZone: { name: 'A' } } },
35
+ { id: 'marker', components: { Transform: { x: 9, y: 0, z: 9 }, HubZone: { name: 'B' } } },
36
+ { id: 'statue', components: { Transform: { x: 0, y: 0, z: 9 }, Shape: box, Model: { file: 'models/statue.pxmodel' } } },
37
+ { id: 'crate', components: { Transform: { x: 1, y: 0, z: 9 }, Shape: box, Body: { type: 'dynamic' } } },
38
+ ];
39
+ const castleJson = { server: { maxPlayers: 8 }, hub: { contract: 1, title: 'Arena', result: {} } };
40
+ const sceneExists = () => true;
41
+
42
+ function body() {
43
+ const calls = [];
44
+ return {
45
+ calls,
46
+ setEnabled() {},
47
+ setNextKinematicTranslation: (at) => calls.push(['at', { ...at }]),
48
+ setNextKinematicRotation: (q) => calls.push(['rot', { ...q }]),
49
+ };
50
+ }
51
+
52
+ function boot() {
53
+ let ctx = null;
54
+ const h = layer(castleJson, { ready: (hub) => (ctx = hub) }, {}, sceneExists);
55
+ const session = { sessionId: 's-1', players: [], hub: null, send() {}, broadcast() {} };
56
+ h.sceneFor(session);
57
+ const bodies = new Map([
58
+ ['sweeper', body()],
59
+ ['wall', body()],
60
+ ]);
61
+ const world = { timeMs: 0, players: new Map(), bodies, scenePath: 'scenes/main.scene' };
62
+ h.ready(world);
63
+ return { h, world, bodies, hub: ctx };
64
+ }
65
+
66
+ const warn = console.warn;
67
+ console.warn = () => {};
68
+ let n = 0;
69
+ function test(name, fn) {
70
+ fn();
71
+ n += 1;
72
+ }
73
+
74
+ test('actorsWith hands back the shape an actor was authored with', () => {
75
+ const { hub } = boot();
76
+ const zones = hub.actorsWith('HubZone');
77
+ assert.deepEqual(zones[0].shape, { kind: 'cylinder', width: undefined, height: 0.1, depth: undefined, radius: 3 });
78
+ assert.equal(zones[0].props.name, 'A');
79
+ assert.equal(zones[0].t.x, -6);
80
+ assert.equal(zones[1].shape, null);
81
+ assert.equal(hub.actorsWith('HubHazard')[0].body, 'kinematic');
82
+ });
83
+
84
+ test('move: a kinematic actor goes at once, or in a straight line over overMs', () => {
85
+ const { h, world, bodies, hub } = boot();
86
+ assert.equal(hub.move('sweeper', { x: 2 }), true);
87
+ h.place(world);
88
+ assert.deepEqual(bodies.get('sweeper').calls.at(-2), ['at', { x: 2, y: 1, z: 5 }]);
89
+ // A finished move is dropped; its body stays.
90
+ const calls = bodies.get('sweeper').calls.length;
91
+ h.place(world);
92
+ assert.equal(bodies.get('sweeper').calls.length, calls);
93
+ assert.equal(hub.move('sweeper', { x: 6, rotationY: 90 }, { overMs: 1000 }), true);
94
+ world.timeMs = 500;
95
+ h.place(world);
96
+ assert.deepEqual(bodies.get('sweeper').calls.at(-2), ['at', { x: 4, y: 1, z: 5 }]);
97
+ const q = bodies.get('sweeper').calls.at(-1)[1];
98
+ assert.ok(Math.abs(q.y - Math.sin((45 * Math.PI) / 360)) < 1e-9);
99
+ // A new move starts from where the last one has got to.
100
+ assert.equal(hub.move('sweeper', { x: 0 }, { overMs: 1000 }), true);
101
+ world.timeMs = 1000;
102
+ h.place(world);
103
+ assert.deepEqual(bodies.get('sweeper').calls.at(-2), ['at', { x: 2, y: 1, z: 5 }]);
104
+ world.timeMs = 1500;
105
+ h.place(world);
106
+ assert.deepEqual(bodies.get('sweeper').calls.at(-2), ['at', { x: 0, y: 1, z: 5 }]);
107
+ });
108
+
109
+ test('move refuses what is not a kinematic scene actor, bad numbers and the world edge', () => {
110
+ const { hub, h, world, bodies } = boot();
111
+ assert.equal(hub.move('wall', { x: 1 }), false);
112
+ assert.equal(hub.move('nobody', { x: 1 }), false);
113
+ assert.equal(hub.move('spawn', { x: 1 }), false);
114
+ assert.equal(hub.move('sweeper', { x: Number.NaN }), false);
115
+ assert.equal(hub.move('sweeper', { x: '3' }), false);
116
+ assert.equal(hub.move('sweeper', { x: 500 }), false);
117
+ assert.equal(hub.move('sweeper', { y: -100 }), false);
118
+ assert.equal(hub.move('sweeper', null), false);
119
+ // A turn past a hundred is refused: interpolating 1e308 overflows.
120
+ assert.equal(hub.move('sweeper', { rotationY: 1e308 }), false);
121
+ h.place(world);
122
+ assert.equal(bodies.get('sweeper').calls.length, 0);
123
+ // A long move is cut to the longest one.
124
+ assert.equal(hub.move('sweeper', { x: 10 }, { overMs: 1e12 }), true);
125
+ world.timeMs = Actors.MOVE_MAX_MS / 2;
126
+ h.place(world);
127
+ assert.deepEqual(bodies.get('sweeper').calls.at(-2), ['at', { x: 5, y: 1, z: 5 }]);
128
+ });
129
+
130
+ test('tint: a Shape actor is painted for everyone, null puts it back', () => {
131
+ const { h, world, hub } = boot();
132
+ assert.equal(hub.tint('wall', '#FF0000'), true);
133
+ assert.equal(hub.tint('marker', '#ff0000'), false);
134
+ assert.equal(hub.tint('wall', 'red'), false);
135
+ assert.equal(hub.tint('wall', '#ff00001'), false);
136
+ // A Model would be rebuilt whole by every client; a dynamic body reset.
137
+ assert.equal(hub.tint('statue', '#ff0000'), false);
138
+ assert.equal(hub.tint('crate', '#ff0000'), false);
139
+ assert.deepEqual(h.join().tints, {});
140
+ h.step(world);
141
+ assert.deepEqual(h.join().tints, { wall: '#ff0000' });
142
+ assert.deepEqual(h.delta().tints, { wall: '#ff0000' });
143
+ assert.equal(h.delta()?.tints, undefined);
144
+ hub.tint('wall', null);
145
+ h.step(world);
146
+ assert.deepEqual(h.delta().tints, {});
147
+ });
148
+
149
+ test('tint changes past the budget wait, and the newest colour wins', () => {
150
+ const flips = Actors.TINT_BURST + 20;
151
+ const extra = Array.from({ length: flips }, (_, i) => ({ id: `a${i}`, components: { Shape: box } }));
152
+ globalThis.__hubTestActors.push(...extra);
153
+ const { h, world, hub } = boot();
154
+ for (const { id } of extra) {
155
+ hub.tint(id, '#00ff00');
156
+ }
157
+ h.step(world);
158
+ assert.equal(Object.keys(h.join().tints).length, Actors.TINT_BURST);
159
+ world.timeMs += 1000;
160
+ h.step(world);
161
+ assert.equal(Object.keys(h.join().tints).length, flips);
162
+ hub.tint('wall', '#111111');
163
+ hub.tint('wall', '#222222');
164
+ // The budget is spent: the change waits for the next token.
165
+ h.step(world);
166
+ assert.equal(h.join().tints.wall, undefined);
167
+ world.timeMs += 100;
168
+ h.step(world);
169
+ assert.equal(h.join().tints.wall, '#222222');
170
+ });
171
+
172
+ test('a spin keeps turning from within one turn', () => {
173
+ const { h, world, bodies, hub } = boot();
174
+ for (let turn = 1; turn <= 3; turn += 1) {
175
+ assert.equal(hub.move('sweeper', { rotationY: 350 + 360 }, { overMs: 100 }), true);
176
+ world.timeMs += 100;
177
+ h.place(world);
178
+ }
179
+ const q = bodies.get('sweeper').calls.at(-1)[1];
180
+ assert.ok(Number.isFinite(q.w) && Math.abs(Math.abs(q.y) - Math.abs(Math.sin((350 * Math.PI) / 360))) < 1e-9);
181
+ });
182
+
183
+ test('clearing and repainting cannot get past the painted cap', () => {
184
+ const extra = Array.from({ length: Actors.MAX_TINTS + 10 }, (_, i) => ({ id: `c${i}`, components: { Shape: box } }));
185
+ globalThis.__hubTestActors.push(...extra);
186
+ const { h, world, hub } = boot();
187
+ let taken = 0;
188
+ for (const { id } of extra) {
189
+ hub.tint(id, null);
190
+ taken += hub.tint(id, '#ff0000') ? 1 : 0;
191
+ }
192
+ assert.equal(taken, Actors.MAX_TINTS);
193
+ for (let i = 0; i < 10; i += 1) {
194
+ world.timeMs += 1000;
195
+ h.step(world);
196
+ }
197
+ assert.equal(Object.keys(h.join().tints).length, Actors.MAX_TINTS);
198
+ // Swapping the painted set: queue the new ones, then clear the old, then
199
+ // paint the new. Never more than the cap at once.
200
+ const painted = Object.keys(h.join().tints);
201
+ const fresh = extra.map((a) => a.id).filter((id) => !painted.includes(id));
202
+ for (const id of fresh) hub.tint(id, null);
203
+ for (const id of painted.slice(0, fresh.length)) hub.tint(id, null);
204
+ for (const id of fresh) hub.tint(id, '#00ff00');
205
+ for (let i = 0; i < 10; i += 1) {
206
+ world.timeMs += 1000;
207
+ h.step(world);
208
+ assert.ok(Object.keys(h.join().tints).length <= Actors.MAX_TINTS);
209
+ }
210
+ assert.equal(h.join().tints[fresh[0]], '#00ff00');
211
+ });
212
+
213
+ test('mark: a coloured tag over a player who is here, for everyone, until cleared or gone', () => {
214
+ const { h, world, hub } = boot();
215
+ h.message({ sessionId: 's-1', players: [] }, { playerId: 'p1' }, { k: 'hub-hello', avatar: 0 });
216
+ assert.equal(hub.mark('p1', { text: 'Blue team, the best team', color: '#3B6FD1' }), true);
217
+ assert.deepEqual(h.join().marks, { p1: { text: 'Blue team, the b', color: '#3b6fd1' } });
218
+ assert.deepEqual(h.delta().marks, { p1: { text: 'Blue team, the b', color: '#3b6fd1' } });
219
+ assert.equal(hub.mark('p1', { text: 'IT' }), false);
220
+ assert.equal(hub.mark('p1', { color: 'red' }), false);
221
+ assert.equal(hub.mark('nobody', { color: '#ff0000' }), false);
222
+ assert.equal(hub.mark('p1', { color: '#ff0000' }), true);
223
+ assert.deepEqual(h.join().marks.p1, { text: '', color: '#ff0000' });
224
+ // A player who leaves takes their mark with them.
225
+ h.place(world);
226
+ assert.deepEqual(h.join().marks, {});
227
+ assert.equal(hub.mark('p1', null), false);
228
+ });
229
+
230
+ test('a scene with no ground catches a fallen player at the arrival, after the game may', () => {
231
+ const falls = [];
232
+ for (const [label, sceneGround, fallHook, fallY] of [
233
+ ['no ground', () => ({ none: true }), null, undefined],
234
+ ['fallY', () => null, null, 3],
235
+ ['the game places them', () => ({ none: true }), (hub, id) => hub.placeAt(id, { x: 7, y: 2, z: 7 }), undefined],
236
+ ]) {
237
+ const json = { server: { maxPlayers: 8 }, hub: { contract: 1, title: 'Pits', ...(fallY === undefined ? {} : { fallY }) } };
238
+ const hooks = fallHook ? { fall: (hub, id) => (falls.push(id), fallHook(hub, id)) } : {};
239
+ const h = layer(json, hooks, {}, sceneExists, sceneGround);
240
+ const session = { sessionId: 's-1', players: [{ playerId: 'p1', username: 'ada' }], hub: null, send() {}, broadcast() {} };
241
+ h.sceneFor(session);
242
+ const body = { at: { x: 0, y: 1, z: 0 }, facing: 0, body: { setTranslation() {} } };
243
+ const world = { timeMs: 0, players: new Map([['p1', body]]), bodies: new Map(), scenePath: 'scenes/main.scene' };
244
+ h.ready(world);
245
+ h.message(session, { playerId: 'p1' }, { k: 'hub-hello', avatar: 0 });
246
+ h.place(world);
247
+ body.at = { x: 4, y: 5, z: 4 };
248
+ h.place(world);
249
+ assert.equal(body.at.x, 4, `${label}: standing is left alone`);
250
+ body.at = { x: 4, y: -21, z: 4 };
251
+ h.place(world);
252
+ if (fallHook) {
253
+ assert.deepEqual(body.at, { x: 7, y: 2, z: 7 }, label);
254
+ } else {
255
+ assert.deepEqual([body.at.x, body.at.z], [0, 0], label);
256
+ assert.ok(body.at.y >= 1.2, label);
257
+ }
258
+ }
259
+ assert.deepEqual(falls, ['p1']);
260
+ // A scene with its ground and no fallY never catches anyone.
261
+ const h = layer(castleJson, {}, {}, sceneExists, () => null);
262
+ const session = { sessionId: 's-1', players: [{ playerId: 'p1', username: 'ada' }], hub: null, send() {}, broadcast() {} };
263
+ h.sceneFor(session);
264
+ const body = { at: { x: 0, y: 1, z: 0 }, facing: 0, body: { setTranslation() {} } };
265
+ const world = { timeMs: 0, players: new Map([['p1', body]]), bodies: new Map(), scenePath: 'scenes/main.scene' };
266
+ h.ready(world);
267
+ h.message(session, { playerId: 'p1' }, { k: 'hub-hello', avatar: 0 });
268
+ h.place(world);
269
+ body.at = { x: 4, y: -25, z: 4 };
270
+ h.place(world);
271
+ assert.equal(body.at.y, -25);
272
+ });
273
+
274
+ test("a dev deck door reads the destination's castle.json, its props only without one", () => {
275
+ globalThis.__hubTestActors.push({
276
+ id: 'door-race',
277
+ components: { Transform: { x: 0, y: 0, z: 0 }, HubDoor: { id: 'race', to: 'deck:race', title: 'Old Race', minPlayers: 1, maxPlayers: 3 } },
278
+ });
279
+ const race = { server: { maxPlayers: 8 }, hub: { contract: 1, title: 'Foot Race', minPlayers: 2, maxPlayers: 8, lateJoin: true, result: {} } };
280
+ const doorOf = (devDecks) => {
281
+ let ctx = null;
282
+ const h = layer(castleJson, { ready: (hub) => (ctx = hub) }, {}, sceneExists, () => null, devDecks);
283
+ h.sceneFor({ sessionId: 'dev', players: [], hub: null, send() {}, broadcast() {} });
284
+ h.ready({ timeMs: 0, players: new Map(), bodies: new Map(), scenePath: 'scenes/main.scene' });
285
+ const d = ctx.doors().find((x) => x.id === 'race');
286
+ return [d.title, d.minPlayers, d.maxPlayers, d.lateJoin];
287
+ };
288
+ assert.deepEqual(doorOf({ race }), ['Foot Race', 2, 8, true]);
289
+ assert.deepEqual(doorOf(null), ['Old Race', 1, 3, false]);
290
+ // A destination whose manifest does not read falls back to the props.
291
+ assert.deepEqual(doorOf({ race: { hub: { contract: 9 } } }), ['Old Race', 1, 3, false]);
292
+ });
293
+
294
+ test('a copy id never collides with an authored actor', () => {
295
+ globalThis.__hubTestActors.push({ id: 'post', components: { Transform: { x: 0, y: 0, z: 0 }, Shape: box } });
296
+ globalThis.__hubTestActors.push({ id: 'post~1', components: { Transform: { x: 1, y: 0, z: 0 }, Shape: box } });
297
+ globalThis.__hubTestActors.push({ id: 'gem', components: { Transform: { x: 1, y: 0, z: 0 }, Shape: box, Pickup: {} } });
298
+ globalThis.__hubTestActors.push({ id: 'constructor', components: { Transform: { x: 2, y: 0, z: 0 }, Shape: box } });
299
+ globalThis.__hubTestActors.push({ id: '__proto__', components: { Transform: { x: 3, y: 0, z: 0 }, Shape: box } });
300
+ const engine = { createBody: () => body(), removeActor: (sim, id) => sim.bodies.delete(id) };
301
+ let hub = null;
302
+ const h = layer(castleJson, { ready: (x) => (hub = x) }, {}, sceneExists, () => null, null, engine);
303
+ h.sceneFor({ sessionId: 's-1', players: [], hub: null, send() {}, broadcast() {} });
304
+ const authored = body();
305
+ const world = { timeMs: 0, players: new Map(), bodies: new Map([['post~1', authored]]), objects: new Map(), scenePath: 'scenes/main.scene' };
306
+ h.ready(world);
307
+ assert.equal(hub.spawn('post', {}), 'post~2');
308
+ assert.equal(world.bodies.get('post~1'), authored);
309
+ assert.equal(hub.spawn('gem', {}), null);
310
+ // An id that names an inherited property is still an id.
311
+ assert.ok(hub.spawn('constructor', {}));
312
+ assert.equal(h.join().spawnTemplates.constructor.Shape.kind, 'box');
313
+ assert.ok(hub.spawn('__proto__', {}));
314
+ assert.equal(JSON.parse(JSON.stringify(h.join().spawnTemplates)).__proto__.Shape.kind, 'box');
315
+ });
316
+
317
+ test('spawn copies a scene actor for everyone, under its caps; despawn removes it', () => {
318
+ const engine = {
319
+ createBody: (sim, actor) => body(),
320
+ removeActor: (sim, id) => {
321
+ sim.bodies.delete(id);
322
+ sim.objects.delete(id);
323
+ },
324
+ };
325
+ let hub = null;
326
+ const h = layer(castleJson, { ready: (x) => (hub = x) }, {}, sceneExists, () => null, null, engine);
327
+ h.sceneFor({ sessionId: 's-1', players: [], hub: null, send() {}, broadcast() {} });
328
+ const world = { timeMs: 0, players: new Map(), bodies: new Map(), objects: new Map(), scenePath: 'scenes/main.scene' };
329
+ h.ready(world);
330
+ const a = hub.spawn('sweeper', { x: 3, rotationY: 450 });
331
+ assert.equal(a, 'sweeper~1');
332
+ assert.deepEqual({ ...h.join().spawned }, { 'sweeper~1': { from: 'sweeper', x: 3, y: 1, z: 5, rotationY: 90 } });
333
+ // The template's authored components travel with it, whatever happens to it.
334
+ assert.deepEqual(Object.keys(h.join().spawnTemplates), ['sweeper']);
335
+ assert.equal(h.join().spawnTemplates.sweeper.Body.type, 'kinematic');
336
+ assert.ok(world.bodies.has(a) && world.objects.get(a).claimable === false);
337
+ // A copy is an actor: move, tint, hide and label it.
338
+ assert.equal(hub.move(a, { x: 4 }), true);
339
+ assert.equal(hub.tint(a, '#ff0000'), true);
340
+ hub.hide(a);
341
+ hub.label(a, 'here');
342
+ h.step(world);
343
+ assert.equal(h.join().tints[a], '#ff0000');
344
+ assert.deepEqual(h.join().hidden, [a]);
345
+ // Not a dynamic body, a player, a door, a copy, nor anything unknown or out of bounds.
346
+ for (const from of ['crate', 'spawn', 'door-race', a, 'nobody']) {
347
+ assert.equal(hub.spawn(from, {}), null, from);
348
+ }
349
+ assert.equal(hub.spawn('wall', { x: 900 }), null);
350
+ assert.equal(hub.spawn('wall', { y: Number.NaN }), null);
351
+ // Ids are never reused.
352
+ assert.equal(hub.despawn(a), true);
353
+ assert.equal(hub.despawn(a), false);
354
+ assert.ok(!world.bodies.has(a));
355
+ assert.deepEqual({ ...h.join().spawned }, {});
356
+ assert.deepEqual(h.join().tints, {});
357
+ assert.deepEqual(h.join().hidden, []);
358
+ assert.equal(hub.spawn('wall', {}), 'wall~2');
359
+ // The burst (two spent above), then the rate.
360
+ let taken = 2;
361
+ while (hub.spawn('wall', {})) taken += 1;
362
+ assert.equal(taken, Actors.SPAWN_BURST);
363
+ world.timeMs += 1000;
364
+ assert.ok(hub.spawn('wall', {}));
365
+ // A collected thing, or an actor with no Shape, is no template.
366
+ assert.equal(hub.spawn('marker', {}), null);
367
+ // A copy whose body a client reported gone is gone for the game too.
368
+ const lost = hub.spawn('wall', {});
369
+ world.bodies.delete(lost);
370
+ h.place(world);
371
+ assert.ok(!(lost in h.join().spawned));
372
+ // The cap on copies alive.
373
+ for (let i = 0; i < 20; i += 1) {
374
+ world.timeMs += 10000;
375
+ while (hub.spawn('wall', {})) {}
376
+ }
377
+ assert.equal(Object.keys(h.join().spawned).length, Actors.MAX_SPAWNED);
378
+ });
379
+
380
+ test("tune sets one player's controller within bounds, and the server clamp follows it", () => {
381
+ const sent = [];
382
+ let hub = null;
383
+ const h = layer(castleJson, { ready: (x) => (hub = x) }, {}, sceneExists, () => null, null, { maxSpeed: () => 12 });
384
+ const session = { sessionId: 's-1', players: [{ playerId: 'p1' }, { playerId: 'p2' }], hub: null, send: (id, d) => sent.push([id, d]), broadcast() {} };
385
+ h.sceneFor(session);
386
+ const p1 = { at: { x: 0, y: 1, z: 0 }, facing: 0, body: { setTranslation() {} } };
387
+ const p2 = { at: { x: 2, y: 1, z: 0 }, facing: 0, body: { setTranslation() {} } };
388
+ const world = { timeMs: 0, players: new Map([['p1', p1], ['p2', p2]]), bodies: new Map(), scenePath: 'scenes/main.scene' };
389
+ h.ready(world);
390
+ h.message(session, { playerId: 'p1' }, { k: 'hub-hello', avatar: 0 });
391
+ h.message(session, { playerId: 'p2' }, { k: 'hub-hello', avatar: 0 });
392
+ assert.equal(hub.tune('p1', { speed: 9, jump: 0.1 }), true);
393
+ assert.equal(hub.tune('nobody', { speed: 2 }), false);
394
+ assert.equal(hub.tune('p1', { speed: '2' }), false);
395
+ h.step(world);
396
+ assert.equal(p1.maxSpeed, 36);
397
+ assert.equal(p2.maxSpeed, undefined);
398
+ const tunes = () => sent.filter(([, d]) => d.k === 'hub-tune');
399
+ assert.deepEqual(tunes(), [['p1', { k: 'hub-tune', tune: { speed: 3, jump: 0.5 } }]]);
400
+ h.step(world);
401
+ assert.equal(tunes().length, 1);
402
+ // A respawned body gets it again; null puts the preset back.
403
+ const again = { ...p1 };
404
+ world.players.set('p1', again);
405
+ h.step(world);
406
+ assert.equal(again.maxSpeed, 36);
407
+ hub.tune('p1', null);
408
+ h.step(world);
409
+ assert.equal(again.maxSpeed, undefined);
410
+ assert.deepEqual(tunes().at(-1), ['p1', { k: 'hub-tune', tune: null }]);
411
+ });
412
+
413
+ await (async () => {
414
+ // Per-player progression (v1.2): user ids on the records and in leave; a
415
+ // result from leave; shutdown runs pending leaves and drains the game.
416
+ const calls = [];
417
+ const resultShape = { server: { maxPlayers: 8 }, hub: { contract: 1, title: 'Lots', result: { score: { min: 0, max: 1e9 } } } };
418
+ let hub = null;
419
+ let drained = false;
420
+ const hooks = {
421
+ ready: (x) => (hub = x),
422
+ arrive: (x, group) => calls.push(['arrive', group.map((g) => [g.playerId, g.userId])]),
423
+ leave: (x, playerId, who) => {
424
+ calls.push(['leave', playerId, who]);
425
+ calls.push(['result', x.result(playerId, { score: 7 })]);
426
+ },
427
+ shutdown: async () => {
428
+ await new Promise((r) => setTimeout(r, 20));
429
+ drained = true;
430
+ },
431
+ };
432
+ const h = layer(resultShape, hooks, {}, sceneExists);
433
+ const players = [
434
+ { playerId: 'p1', userId: '101', username: 'ada', isAnonymous: false },
435
+ { playerId: 'p2', userId: 'anon-3', username: 'guest', isAnonymous: true },
436
+ ];
437
+ const session = { sessionId: 's-1', players: [...players], hub: null, send() {}, broadcast() {} };
438
+ h.sceneFor(session);
439
+ const body = () => ({ at: { x: 0, y: 1, z: 0 }, facing: 0, body: { setTranslation() {} } });
440
+ const world = { timeMs: 0, players: new Map([['p1', body()], ['p2', body()]]), bodies: new Map(), scenePath: 'scenes/main.scene' };
441
+ h.ready(world);
442
+ h.message(session, players[0], { k: 'hub-hello', avatar: 0, party: 2 });
443
+ h.message(session, players[1], { k: 'hub-hello', avatar: 0, party: 2 });
444
+ h.place(world);
445
+ assert.deepEqual(calls[0], ['arrive', [['p1', '101'], ['p2', null]]]);
446
+ assert.deepEqual(hub.players().map((p) => p.userId), ['101', null]);
447
+ // p1 leaves (by Back): the session forgets them before the kit does.
448
+ session.players = [players[1]];
449
+ world.players.delete('p1');
450
+ h.place(world);
451
+ assert.deepEqual(calls[1], ['leave', 'p1', { userId: '101', username: 'ada' }]);
452
+ assert.deepEqual(calls[2], ['result', true]);
453
+ // Shutdown: whoever is left leaves now, and the game drains.
454
+ await h.shutdown();
455
+ assert.deepEqual(calls[3], ['leave', 'p2', { userId: null, username: 'guest' }]);
456
+ assert.equal(drained, true);
457
+ n += 1;
458
+ })();
459
+
460
+ await (async () => {
461
+ // A leave hook that throws still lets the player go, once, and shutdown
462
+ // still drains; dev guests keep their dev storage subject.
463
+ let leaves = 0;
464
+ let drained = false;
465
+ let hub = null;
466
+ const hooks = {
467
+ ready: (x) => (hub = x),
468
+ leave: () => {
469
+ leaves += 1;
470
+ throw new Error('boom');
471
+ },
472
+ shutdown: () => {
473
+ drained = true;
474
+ },
475
+ };
476
+ const h = layer(castleJson, hooks, {}, sceneExists);
477
+ const players = [
478
+ { playerId: 'g1', userId: 'dev-guest-1', username: 'Guest 1', isAnonymous: true },
479
+ { playerId: 'g2', userId: 'dev-guest-2', username: 'Guest 2', isAnonymous: true },
480
+ ];
481
+ const session = { sessionId: 'dev:hub.main.x1', players: [...players], hub: null, send() {}, broadcast() {} };
482
+ h.sceneFor(session);
483
+ const body = () => ({ at: { x: 0, y: 1, z: 0 }, facing: 0, body: { setTranslation() {} } });
484
+ const world = { timeMs: 0, players: new Map([['g1', body()], ['g2', body()]]), bodies: new Map(), scenePath: 'scenes/main.scene' };
485
+ h.ready(world);
486
+ h.message(session, players[0], { k: 'hub-hello', avatar: 0, party: 2 });
487
+ h.message(session, players[1], { k: 'hub-hello', avatar: 0, party: 2 });
488
+ h.place(world);
489
+ assert.deepEqual(hub.players().map((p) => p.userId), ['dev-guest-1', 'dev-guest-2']);
490
+ session.players = [players[1]];
491
+ world.players.delete('g1');
492
+ h.place(world);
493
+ h.place(world);
494
+ assert.equal(leaves, 1);
495
+ await h.shutdown();
496
+ assert.equal(leaves, 2);
497
+ assert.equal(drained, true);
498
+ n += 1;
499
+ })();
500
+
501
+ test('pose reads a body as the server has it; reset takes a loose object back from its owner and places it at rest', () => {
502
+ const released = [];
503
+ const engine = {
504
+ release: (sim, owner, ids, handovers) => {
505
+ released.push([owner, ...ids]);
506
+ for (const id of ids) {
507
+ const e = sim.objects.get(id);
508
+ e.owner = null;
509
+ e.epoch += 1;
510
+ handovers.push([id, 0, 3, 0, 1, 0, 0, 0, e.epoch]);
511
+ }
512
+ },
513
+ };
514
+ let hub = null;
515
+ const h = layer(castleJson, { ready: (x) => (hub = x) }, {}, sceneExists, () => null, null, engine);
516
+ h.sceneFor({ sessionId: 's-1', players: [], hub: null, send() {}, broadcast() {} });
517
+ const state = { at: { x: 4, y: 0.5, z: 8 }, rot: { x: 0, y: Math.sin(Math.PI / 4), z: 0, w: Math.cos(Math.PI / 4) }, v: { x: 2, y: 0, z: 0 } };
518
+ const crateBody = {
519
+ translation: () => ({ ...state.at }),
520
+ rotation: () => ({ ...state.rot }),
521
+ linvel: () => ({ ...state.v }),
522
+ setTranslation: (at) => (state.at = { ...at }),
523
+ setRotation: (q) => (state.rot = { ...q }),
524
+ setLinvel: (v) => (state.v = { ...v }),
525
+ setAngvel() {},
526
+ setEnabled() {},
527
+ };
528
+ const entry = { body: crateBody, home: { x: 1, y: 0, z: 9 }, claimable: true, owner: 'p1', epoch: 2 };
529
+ const world = { timeMs: 0, players: new Map(), bodies: new Map([['crate', crateBody]]), objects: new Map([['crate', entry]]), scenePath: 'scenes/main.scene' };
530
+ h.ready(world);
531
+ const handovers = [];
532
+ h.place(world, handovers);
533
+ const p = hub.pose('crate');
534
+ assert.deepEqual([p.x, p.z, Math.round(p.rotationY), p.velocity.x, p.owner], [4, 8, 90, 2, 'p1']);
535
+ // Heading of a tipped body: its forward on the ground.
536
+ const tilt = (deg) => ({ x: Math.sin((deg * Math.PI) / 360), y: 0, z: 0, w: Math.cos((deg * Math.PI) / 360) });
537
+ state.rot = tilt(60);
538
+ assert.equal(Math.round(hub.pose('crate').rotationY), 0);
539
+ assert.equal(hub.pose('wall'), null);
540
+ assert.equal(hub.pose('nobody'), null);
541
+ // Back where it was authored, at rest; its owner told by a handover at rest,
542
+ // in the session's list at the next tick however the reset was called.
543
+ assert.equal(hub.reset('crate'), true);
544
+ assert.deepEqual(released, [['p1', 'crate']]);
545
+ assert.deepEqual([state.at.x, state.at.z, state.v.x], [1, 9, 0]);
546
+ assert.equal(entry.claimable, false);
547
+ const next = [];
548
+ h.place(world, next);
549
+ assert.deepEqual(next, [['crate', 0, 0, 0, 0, 0, 0, 0, 3]]);
550
+ assert.deepEqual(handovers, []);
551
+ // Claimable again once every client has had the reset pose.
552
+ world.timeMs += Actors.RESET_HOLD_MS;
553
+ h.place(world, []);
554
+ assert.equal(entry.claimable, true);
555
+ // Somewhere else; not a kinematic or fixed actor; not past the world's edge.
556
+ assert.equal(hub.reset('crate', { x: 0, z: 0, rotationY: 450 }), true);
557
+ assert.deepEqual([state.at.x, state.at.z], [0, 0]);
558
+ assert.equal(hub.reset('sweeper'), false);
559
+ assert.equal(hub.reset('wall'), false);
560
+ assert.equal(hub.reset('crate', { x: 900 }), false);
561
+ // The rate: a burst, then nothing until time passes.
562
+ let n = 0;
563
+ while (hub.reset('crate')) n += 1;
564
+ assert.ok(n > 0 && n <= Actors.RESET_BURST);
565
+ assert.equal(hub.reset('crate'), false);
566
+ world.timeMs += 1000;
567
+ assert.equal(hub.reset('crate'), true);
568
+ });
569
+
570
+ test('wear: a wearable on a player who is here, latched for everyone, until cleared or gone', () => {
571
+ const { h, world, hub } = boot();
572
+ const session = { sessionId: 's-1', players: [], send() {}, broadcast() {} };
573
+ // Nobody wears anything: the snapshot is as it always was.
574
+ assert.equal('hubWear' in h.join(), false);
575
+ assert.equal(h.delta()?.hubWear, undefined);
576
+ h.message(session, { playerId: 'p1' }, { k: 'hub-hello', avatar: 0 });
577
+ assert.equal(hub.wear('p1', { kind: 'halo', color: '#FFD700', rarity: 4 }), true);
578
+ assert.deepEqual(h.join().hubWear, { p1: { kind: 'halo', color: '#ffd700', rarity: 4 } });
579
+ assert.deepEqual(h.delta().hubWear, { p1: { kind: 'halo', color: '#ffd700', rarity: 4 } });
580
+ assert.equal(h.delta()?.hubWear, undefined, 'latched: sent only on change');
581
+ const avBefore = JSON.stringify(h.join().hubAv);
582
+ // Refused: not a wearable, unknown keys, out of range, not a player here.
583
+ for (const bad of [
584
+ { kind: 'halo', color: 'gold', rarity: 1 },
585
+ { kind: 'Halo', color: '#ffd700', rarity: 1 },
586
+ { kind: 'halo', color: '#ffd700', rarity: 10 },
587
+ { kind: 'halo', color: '#ffd700', rarity: 1.5 },
588
+ { kind: 'halo', color: '#ffd700', rarity: 1, text: 'free text' },
589
+ 'halo',
590
+ [],
591
+ ]) {
592
+ assert.equal(hub.wear('p1', bad), false, JSON.stringify(bad));
593
+ }
594
+ assert.equal(hub.wear('nobody', { kind: 'halo', color: '#ffd700', rarity: 1 }), false);
595
+ assert.deepEqual(h.join().hubWear.p1.kind, 'halo', 'a refused one changes nothing');
596
+ // Latest wins; a kind the kit does not draw is still kept.
597
+ assert.equal(hub.wear('p1', { kind: 'crown', color: '#00ff00', rarity: 9 }), true);
598
+ assert.deepEqual(h.delta().hubWear, { p1: { kind: 'crown', color: '#00ff00', rarity: 9 } });
599
+ // Null clears it, and the empty map goes out once.
600
+ assert.equal(hub.wear('p1', null), true);
601
+ assert.deepEqual(h.delta().hubWear, {});
602
+ assert.equal('hubWear' in h.join(), false);
603
+ assert.equal(JSON.stringify(h.join().hubAv), avBefore, 'the avatar index map is untouched');
604
+ // A player who leaves takes theirs with them.
605
+ hub.wear('p1', { kind: 'aura', color: '#123456', rarity: 0 });
606
+ assert.deepEqual(h.delta().hubWear, { p1: { kind: 'aura', color: '#123456', rarity: 0 } });
607
+ h.place(world);
608
+ assert.deepEqual(h.delta().hubWear, {});
609
+ assert.equal(hub.wear('p1', null), false);
610
+ });
611
+
612
+ test('wear: a wearable is drawn as pieces of the kinds the kit knows', () => {
613
+ assert.equal(pieces({ kind: 'halo', color: '#ffd700', rarity: 0 }).length, 1);
614
+ assert.equal(pieces({ kind: 'aura', color: '#ffd700', rarity: 9 })[0].shape.kind, 'cylinder');
615
+ assert.equal(pieces({ kind: 'orbit', color: '#ffd700', rarity: 0 }).length, 1);
616
+ assert.equal(pieces({ kind: 'orbit', color: '#ffd700', rarity: 9 }).length, 4);
617
+ assert.equal(pieces({ kind: 'crown', color: '#ffd700', rarity: 1 }), null);
618
+ assert.equal(pieces({ kind: 'halo', color: 'javascript:', rarity: 1 }), null);
619
+ const small = pieces({ kind: 'halo', color: '#ffd700', rarity: 0 })[0].shape;
620
+ const big = pieces({ kind: 'halo', color: '#FFD700', rarity: 99 })[0].shape;
621
+ assert.ok(big.radius > small.radius && big.emissiveIntensity > small.emissiveIntensity);
622
+ assert.equal(big.emissive, '#ffd700');
623
+ assert.equal(big.solid, false);
624
+ // A halo and an orbit sit over the avatar's own top; an aura stays at its feet.
625
+ const y = (kind, top) => pieces({ kind, color: '#ffd700', rarity: 0 }, top)[0].at(0).y;
626
+ assert.ok(Math.abs(y('halo', 1.3) - (y('halo', 1.7) - 0.4)) < 1e-9);
627
+ assert.ok(Math.abs(y('orbit', 1.3) - (y('orbit', 1.7) - 0.4)) < 1e-9 && y('orbit', 1.3) < y('halo', 1.3));
628
+ assert.equal(y('aura', 1.3), y('aura', 1.7));
629
+ // An avatar this kit does not know wears them where they always were.
630
+ assert.ok(Math.abs(y('halo') - 1.1) < 1e-9 && Math.abs(y('orbit') - 0.95) < 1e-9);
631
+ });
632
+
633
+ test('wear: a block hides a wearable as it hides the avatar, sent alone to who may see it', () => {
634
+ const sent = [];
635
+ const h = {
636
+ ...Wear.newWearState(),
637
+ players: new Map(['a', 'b', 'c'].map((id) => [id, { hello: true }])),
638
+ sim: { players: new Map(['a', 'b', 'c'].map((id) => [id, {}])) },
639
+ session: { send: (to, msg) => sent.push([to, msg]) },
640
+ };
641
+ const halo = { kind: 'halo', color: '#ffd700', rarity: 1 };
642
+ h.wearing.set('a', halo);
643
+ h.wearing.set('c', halo);
644
+ // a and b hide each other.
645
+ const hidden = new Map([
646
+ ['a', new Set(['b'])],
647
+ ['b', new Set(['a'])],
648
+ ]);
649
+ assert.deepEqual(Wear.wearingOf(h, hidden), { c: halo });
650
+ Wear.sendOverrides(h, hidden);
651
+ // a sees its own, c sees a's, b never learns of it.
652
+ assert.deepEqual(sent, [
653
+ ['a', { k: 'hub-wear', hubWear: { a: halo } }],
654
+ ['c', { k: 'hub-wear', hubWear: { a: halo } }],
655
+ ]);
656
+ sent.length = 0;
657
+ Wear.sendOverrides(h, hidden);
658
+ assert.deepEqual(sent, [], 'sent only on change');
659
+ // No blocks: nothing goes to anyone alone.
660
+ const plain = { ...h, sentWearMe: new Map() };
661
+ Wear.sendOverrides(plain, null);
662
+ assert.deepEqual(sent, []);
663
+ });
664
+
665
+ console.warn = warn;
666
+ console.log(`test-actors: ${n} passed`);