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,131 @@
1
+ // Jump-to-door (contract §5): a town tells its clients its doors (`hubDoors`)
2
+ // and puts a player who picks one in front of it, facing it, in the door's own
3
+ // frame; a game has no list, and a held player, an unknown door or a second
4
+ // jump within a second is ignored. `npm run check` runs it; it needs nothing
5
+ // 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 { doorPoint } = await import('../code/client/doorFrame.js');
23
+
24
+ globalThis.__hubTestActors = [
25
+ { id: 'spawn', components: { Player: {}, Transform: { x: 0, y: 1, z: 0 } } },
26
+ // The west wall's door, turned to face +x (the square).
27
+ { id: 'door-race', components: { HubDoor: { id: 'race', to: 'race' }, Transform: { x: -28, y: 1.4, z: 0, rotationY: 90 } } },
28
+ { id: 'door-gems', components: { HubDoor: { id: 'gems', to: 'deck:gems', title: 'Gem Rush' }, Transform: { x: 0, y: 1.4, z: -28, rotationY: 0 } } },
29
+ ];
30
+ const castleJson = {
31
+ server: { maxPlayers: 8 },
32
+ hub: {
33
+ contract: 1,
34
+ title: 'Town',
35
+ places: {
36
+ race: {
37
+ title: 'Foot Race',
38
+ scene: 'scenes/race.scene',
39
+ result: { outcomes: ['win', 'lose'], score: { unit: 'ms', better: 'lower', min: 0, max: 600000 } },
40
+ },
41
+ },
42
+ },
43
+ };
44
+ const sceneExists = (scene) => ['scenes/main.scene', 'scenes/race.scene'].includes(scene);
45
+ const PLAYERS = [{ playerId: 'p1', userId: '101', username: 'ada' }, { playerId: 'p2', userId: '102', username: 'bo' }];
46
+
47
+ function boot(sessionId = 'dev:test') {
48
+ const h = layer(castleJson, {}, {}, sceneExists);
49
+ const session = { sessionId, players: PLAYERS, send() {}, broadcast() {} };
50
+ h.sceneFor(session);
51
+ const players = new Map();
52
+ for (const { playerId } of PLAYERS) {
53
+ players.set(playerId, { at: { x: 0, y: 1, z: 0 }, facing: 0, body: { setTranslation() {} } });
54
+ }
55
+ const world = { timeMs: 0, players, bodies: new Map(), scenePath: 'scenes/main.scene' };
56
+ h.ready(world);
57
+ return { h, session, world };
58
+ }
59
+ const jump = (b, playerId, door) => b.h.message(b.session, { playerId }, { k: 'hub-door-jump', door });
60
+
61
+ let n = 0;
62
+
63
+ // The town lists its doors.
64
+ {
65
+ const b = boot();
66
+ assert.deepEqual(b.h.join().hubDoors, [
67
+ { id: 'race', title: 'Foot Race' },
68
+ { id: 'gems', title: 'Gem Rush' },
69
+ ]);
70
+ n += 1;
71
+
72
+ // A jump puts the player 7 m in front of the turned door, facing it.
73
+ jump(b, 'p1', 'race');
74
+ const body = b.world.players.get('p1');
75
+ const front = doorPoint({ x: -28, z: 0, rotationY: 90 }, 0, 7);
76
+ assert.ok(Math.abs(body.at.x - front.x) < 1e-9, `x ${body.at.x}`);
77
+ assert.ok(Math.abs(body.at.z - front.z) <= 1, `z ${body.at.z} within the spread`);
78
+ assert.ok(Math.abs(body.facing - (270 * Math.PI) / 180) < 1e-9, 'faces the door (-x)');
79
+ assert.equal(body.clamped, true, 'reaches the client as a clamp');
80
+ n += 1;
81
+
82
+ // A second jump within a second is ignored.
83
+ jump(b, 'p1', 'gems');
84
+ assert.ok(Math.abs(body.at.x - front.x) < 1e-9, 'still at the race door');
85
+ n += 1;
86
+
87
+ // An unknown door is ignored.
88
+ jump(b, 'p2', 'nowhere');
89
+ assert.deepEqual(b.world.players.get('p2').at, { x: 0, y: 1, z: 0 });
90
+ n += 1;
91
+ }
92
+
93
+ // A held player stays where the place holds them.
94
+ {
95
+ const b = boot();
96
+ let ctx = null;
97
+ const h = layer(castleJson, { ready: (hub) => (ctx = hub) }, {}, sceneExists);
98
+ h.sceneFor(b.session);
99
+ h.ready(b.world);
100
+ ctx.hold('p2', { x: 3, y: 1, z: 3 });
101
+ h.message(b.session, { playerId: 'p2' }, { k: 'hub-door-jump', door: 'gems' });
102
+ assert.equal(b.world.players.get('p2').at.z !== doorPoint({ x: 0, z: -28, rotationY: 0 }, 0, 7).z, true);
103
+ n += 1;
104
+ }
105
+
106
+ // A door the place hides leaves the list and takes no jumps; shown, it is back.
107
+ {
108
+ const b = boot();
109
+ let ctx = null;
110
+ const h = layer(castleJson, { ready: (hub) => (ctx = hub) }, {}, sceneExists);
111
+ h.sceneFor(b.session);
112
+ h.ready(b.world);
113
+ ctx.hide('door-race');
114
+ assert.deepEqual(h.join().hubDoors, [{ id: 'gems', title: 'Gem Rush' }]);
115
+ h.message(b.session, { playerId: 'p1' }, { k: 'hub-door-jump', door: 'race' });
116
+ assert.deepEqual(b.world.players.get('p1').at, { x: 0, y: 1, z: 0 }, 'no jump to a hidden door');
117
+ ctx.show('door-race');
118
+ assert.equal(h.join().hubDoors.length, 2);
119
+ n += 1;
120
+ }
121
+
122
+ // A game has no door list and no jumps.
123
+ {
124
+ const b = boot('dev:hub.race.abc123');
125
+ assert.deepEqual(b.h.join().hubDoors, []);
126
+ jump(b, 'p1', 'race');
127
+ assert.deepEqual(b.world.players.get('p1').at, { x: 0, y: 1, z: 0 });
128
+ n += 1;
129
+ }
130
+
131
+ console.log(`test-door-jump: ${n} passed`);
@@ -0,0 +1,56 @@
1
+ // The door frame (code/client/doorFrame.js): a door's points land where the
2
+ // engine's own transform puts them for any rotationY, and a door facing +z
3
+ // swings exactly as it did before doors could face other ways.
4
+ // `npm run check` runs it.
5
+
6
+ import assert from 'node:assert/strict';
7
+ import * as THREE from 'three';
8
+ import { doorPoint } from '../code/client/doorFrame.js';
9
+
10
+ const near = (a, b, what) => assert.ok(Math.abs(a - b) < 1e-9, `${what}: ${a} vs ${b}`);
11
+
12
+ // Where the renderer puts a door-local point: world3d.js sets a node's
13
+ // position and rotation.y (rotationY in radians) and three does the rest.
14
+ function engine(base, x, z) {
15
+ const node = new THREE.Object3D();
16
+ node.position.set(base.x, 0, base.z);
17
+ node.rotation.set(0, (base.rotationY * Math.PI) / 180, 0);
18
+ node.updateMatrixWorld();
19
+ const p = node.localToWorld(new THREE.Vector3(x, 0, z));
20
+ return { x: p.x, z: p.z };
21
+ }
22
+
23
+ let n = 0;
24
+ for (const rotationY of [0, 90, 180, 270, -45, 30]) {
25
+ const base = { x: 3, z: -7, rotationY };
26
+ for (const [along, out] of [[1.2, 0], [0, 0.3], [0, -6], [-1.2, 2]]) {
27
+ const got = doorPoint(base, along, out);
28
+ const want = engine(base, along, out);
29
+ near(got.x, want.x, `x r=${rotationY}`);
30
+ near(got.z, want.z, `z r=${rotationY}`);
31
+ n += 1;
32
+ }
33
+ }
34
+
35
+ // Facing +z: in front is +z, beyond is -z, the hinge is the -x edge.
36
+ assert.deepEqual(doorPoint({ x: 0, z: 0, rotationY: 0 }, 0, 1), { x: 0, z: 1 });
37
+ // Facing +x (rotationY 90): in front is +x.
38
+ const east = doorPoint({ x: 0, z: 0, rotationY: 90 }, 0, 1);
39
+ near(east.x, 1, 'east x');
40
+ near(east.z, 0, 'east z');
41
+ n += 2;
42
+
43
+ // The swing at rotationY 0 is the one the town's north-wall doors always had.
44
+ const SWING = 100;
45
+ const half = 1.2;
46
+ for (const amount of [0, 0.3, 1]) {
47
+ const base = { x: 4, z: -10, rotationY: 0 };
48
+ const hinge = doorPoint(base, -half, 0);
49
+ const turned = ((base.rotationY + amount * SWING) * Math.PI) / 180;
50
+ const leaf = { x: hinge.x + Math.cos(turned) * half, z: hinge.z - Math.sin(turned) * half };
51
+ const theta = (amount * SWING * Math.PI) / 180;
52
+ near(leaf.x, base.x - half + Math.cos(theta) * half, 'old swing x');
53
+ near(leaf.z, base.z - Math.sin(theta) * half, 'old swing z');
54
+ n += 1;
55
+ }
56
+ console.log(`test-door: ${n} passed`);
@@ -0,0 +1,328 @@
1
+ // Grabbing and throwing (contract §6), the client's offer (code/client/grab.js)
2
+ // on a fake scene, multiplayer kit and chrome: a place without `grab.reach`
3
+ // or `grab.steady` is offered exactly what it was (the 1.6 m reach, the 60
4
+ // degree cone, an object the moment it is this client's and not a frame
5
+ // longer); v1.22's `reach` lengthens the reach; `steady` keeps the same object
6
+ // on offer through a frame out of the cone or out of this client's hands, holds
7
+ // the button up for its grace and no longer, lets a press in the grace wait
8
+ // for the object to be this client's again, and never sends a grab for an
9
+ // object this client does not own. `npm run check` runs it; it needs nothing
10
+ // installed.
11
+
12
+ import assert from 'node:assert/strict';
13
+ import {
14
+ KEEP_MARGIN_M,
15
+ OFFER_CENTRE_M,
16
+ offerCap,
17
+ PRESS_WAIT_MS,
18
+ STEADY_GRACE_MS,
19
+ makeGrab,
20
+ } from '../code/client/grab.js';
21
+ import { hub } from '../code/client/state.js';
22
+ import { GRAB_REACH } from '../code/server/grab.js';
23
+ import { GRAB_DEFAULTS } from '../code/server/manifest.js';
24
+
25
+ let clock = 1000;
26
+ Object.defineProperty(globalThis, 'performance', {
27
+ value: { now: () => clock },
28
+ configurable: true,
29
+ });
30
+
31
+ const DEG = Math.PI / 180;
32
+ // A pillow: a sphere 0.3 m from its centre to its edge.
33
+ const PILLOW = { kind: 'sphere', radius: 0.3 };
34
+
35
+ // One client in a place: the player at the origin facing +z, and loose
36
+ // objects `{ id: { x, z, y?, shape? } }`, all this client's to begin with.
37
+ function rig(grab, objects) {
38
+ hub.place = grab === null ? {} : { grab };
39
+ hub.hidden.clear();
40
+ hub.held.clear();
41
+ hub.grabEvents.length = 0;
42
+ const prompts = [];
43
+ const sent = [];
44
+ let press = null;
45
+ const chrome = {
46
+ onGrab: (fn) => (press = fn),
47
+ grabPrompt: (text) => prompts.push(text),
48
+ };
49
+ const g = makeGrab(chrome);
50
+ const actors = new Map();
51
+ const bodies = new Map();
52
+ for (const [id, o] of Object.entries(objects)) {
53
+ const transform = { x: o.x, y: o.y ?? 0.85, z: o.z };
54
+ actors.set(id, {
55
+ components: { Transform: transform, Shape: o.shape ?? PILLOW, Body: { type: 'dynamic' } },
56
+ });
57
+ bodies.set(id, {
58
+ translation: () => ({ x: transform.x, y: transform.y, z: transform.z }),
59
+ isDynamic: () => true,
60
+ setTranslation(p) {
61
+ Object.assign(transform, p);
62
+ },
63
+ setLinvel() {},
64
+ setAngvel() {},
65
+ });
66
+ }
67
+ const scene = {
68
+ getActor: (id) => actors.get(id) ?? null,
69
+ physics3d: { ready: () => true, bodyOf: (id) => bodies.get(id) ?? null },
70
+ };
71
+ const mine = new Set(actors.keys());
72
+ const mp = {
73
+ net: { selfId: () => 'me', send: (d) => sent.push(d) },
74
+ own: { mine },
75
+ claimable: () => [...actors].map(([id, a]) => [id, a.components.Transform]),
76
+ };
77
+ const me = { x: 0, y: 0.85, z: 0, rotationY: 0 };
78
+ const local = { components: { Transform: me } };
79
+ // One frame `ms` after the last, with a press in it or not; the button's word.
80
+ const step = (ms = 16, pressed = false, idle = true) => {
81
+ clock += ms;
82
+ if (pressed) {
83
+ press();
84
+ }
85
+ g.frame(scene, mp, local, idle);
86
+ return prompts.at(-1);
87
+ };
88
+ const place = (id, x, z) => {
89
+ const t = actors.get(id).components.Transform;
90
+ t.x = x;
91
+ t.z = z;
92
+ };
93
+ const grabs = () => sent.filter((d) => d.k === 'hub-grab' && d.op === 'grab').map((d) => d.actor);
94
+ return { g, step, me, mine, place, grabs, sent, prompts, actors };
95
+ }
96
+
97
+ // Where an object at `distance` (centre, flat) and `deg` off facing stands.
98
+ function at(distance, deg) {
99
+ return { x: Math.sin(deg * DEG) * distance, z: Math.cos(deg * DEG) * distance };
100
+ }
101
+
102
+ let n = 0;
103
+ function test(name, fn) {
104
+ try {
105
+ fn();
106
+ } catch (err) {
107
+ console.error(`test-grab-client: ${name}`);
108
+ throw err;
109
+ }
110
+ n += 1;
111
+ }
112
+
113
+ test('without reach or steady: the 1.6 m reach, the 60 degree cone, and ownership each frame, as before', () => {
114
+ const { step, place, mine, me } = rig({ ...GRAB_DEFAULTS }, { a: at(1.85, 0) });
115
+ // Near side 1.55 m: offered; 1.65 m: not.
116
+ assert.equal(step(), 'Grab');
117
+ place('a', 0, 1.95);
118
+ assert.equal(step(), null);
119
+ // 59 degrees off facing: offered; 61: not.
120
+ place('a', at(1.5, 59).x, at(1.5, 59).z);
121
+ assert.equal(step(), 'Grab');
122
+ place('a', at(1.5, 61).x, at(1.5, 61).z);
123
+ assert.equal(step(), null);
124
+ // A frame out of this client's hands takes the button down at once.
125
+ place('a', 0, 1.5);
126
+ assert.equal(step(), 'Grab');
127
+ mine.delete('a');
128
+ assert.equal(step(), null);
129
+ mine.add('a');
130
+ assert.equal(step(), 'Grab');
131
+ // So does a turn out of the cone.
132
+ me.rotationY = 70;
133
+ assert.equal(step(), null);
134
+ // And nothing farther than 1.6 m is offered, however steady the player.
135
+ me.rotationY = 0;
136
+ place('a', 0, 2.4);
137
+ for (let i = 0; i < 5; i += 1) {
138
+ assert.equal(step(), null);
139
+ }
140
+ });
141
+
142
+ test('without reach or steady: a big object is offered past 3.5 m centre to centre, as before', () => {
143
+ // A wide box (1.84 m from centre to edge) whose centre is 3.4 m out and
144
+ // 1.8 m up: the v1.22 centre bound applies only to a place that asks.
145
+ const box = { kind: 'box', width: 2.6, height: 1, depth: 2.6 };
146
+ const before = rig({ ...GRAB_DEFAULTS }, { a: { x: 0, z: 3.4, y: 2.65, shape: box } });
147
+ assert.ok(Math.hypot(3.4, 1.8) > OFFER_CENTRE_M);
148
+ assert.equal(before.step(), 'Grab');
149
+ const asked = rig({ ...GRAB_DEFAULTS, reach: 1.6 }, { a: { x: 0, z: 3.4, y: 2.65, shape: box } });
150
+ assert.equal(asked.step(), null);
151
+ });
152
+
153
+ test('a place without grab draws no button and sends nothing', () => {
154
+ const { step, grabs } = rig(null, { a: at(1, 0) });
155
+ assert.equal(step(0, true), null);
156
+ assert.deepEqual(grabs(), []);
157
+ });
158
+
159
+ test('a press grabs what is offered, the same with or without steady', () => {
160
+ for (const grab of [{ ...GRAB_DEFAULTS }, { ...GRAB_DEFAULTS, steady: true }]) {
161
+ const { step, grabs, g } = rig(grab, { a: at(1.2, 0), b: at(1.5, 10) });
162
+ assert.equal(step(), 'Grab');
163
+ assert.equal(step(16, true), 'Throw');
164
+ assert.deepEqual(grabs(), ['a']);
165
+ assert.equal(g.holding(), 'a');
166
+ }
167
+ });
168
+
169
+ test('reach: objects are offered farther away, and never past 3 m centre to centre', () => {
170
+ const { step, place } = rig({ ...GRAB_DEFAULTS, reach: 2.2 }, { a: at(2.45, 0) });
171
+ assert.equal(step(), 'Grab');
172
+ place('a', 0, 2.55);
173
+ assert.equal(step(), null);
174
+ // Without steady, the reach has no hysteresis.
175
+ place('a', 0, 2.45);
176
+ assert.equal(step(), 'Grab');
177
+ place('a', 0, 2.55);
178
+ assert.equal(step(), null);
179
+ // At the top of the reach, a wide object whose near side is in reach but
180
+ // whose centre is past the cap (3 m at most, 2.55 m for a walking player) is not offered.
181
+ const box = { kind: 'box', width: 2, height: 1, depth: 2 };
182
+ const far = rig({ ...GRAB_DEFAULTS, reach: 3 }, { a: { x: 0, z: 3.9, shape: box } });
183
+ assert.equal(far.step(), null);
184
+ far.place('a', 0, 2.5);
185
+ assert.equal(far.step(), 'Grab');
186
+ assert.ok(OFFER_CENTRE_M < GRAB_REACH);
187
+ });
188
+
189
+ test('reach: a faster-moving player is offered less far, so a running grab stays inside the server reach', () => {
190
+ // The server judges a grab from a pose that may trail the player's own;
191
+ // the offer keeps inside 4 m by what they travel in 0.2 s at the speed
192
+ // they move (their body's velocity, jumps included) or are tuned to.
193
+ const lag = 0.2;
194
+ for (const [velocity, tune, base] of [
195
+ [null, null, undefined],
196
+ [{ x: 6, y: 0, z: 0 }, null, undefined],
197
+ [{ x: 18, y: 0, z: 0 }, null, undefined],
198
+ [{ x: 0, y: 20, z: 0 }, { speed: 1, jump: 2.5 }, undefined],
199
+ [null, { speed: 3 }, 36], // an authored 12 m/s preset, tuned three times (Player.speed is already tuned)
200
+ [{ x: 18, y: 0, z: 0 }, null, 6], // a tune just lowered: the body still runs
201
+ ]) {
202
+ const cap = offerCap(velocity, tune, base);
203
+ const speed = Math.max(velocity ? Math.hypot(velocity.x, velocity.y, velocity.z) : 0, base ?? 6 * Math.max(1, tune?.speed ?? 1));
204
+ assert.ok(cap >= 0 && cap <= OFFER_CENTRE_M);
205
+ assert.ok(cap === 0 || cap + speed * lag < GRAB_REACH, `${JSON.stringify([velocity, tune, base])}: ${cap}`);
206
+ }
207
+ assert.ok(offerCap({ x: 18, y: 0, z: 0 }) < offerCap(null));
208
+ // The tune is counted once: a walk preset tuned twice as fast (Player.speed 12) still offers.
209
+ assert.ok(offerCap(null, { speed: 2 }, 12) > 0);
210
+ assert.equal(offerCap(null, { speed: 2 }, 12), offerCap(null, { speed: 2 }));
211
+ });
212
+
213
+ test('steady: the same object stays offered through a frame out of the cone or out of this client’s hands', () => {
214
+ const { step, me, mine, place, grabs } = rig(
215
+ { ...GRAB_DEFAULTS, steady: true, reach: 2.2 },
216
+ {
217
+ a: at(2.2, 0),
218
+ b: at(4, 0),
219
+ },
220
+ );
221
+ assert.equal(step(), 'Grab');
222
+ // Another object comes nearer: the offer stays on the first.
223
+ place('b', 0, 1);
224
+ assert.equal(step(), 'Grab');
225
+ // Out of the 60 degree cone, inside 90: still offered, no grace needed.
226
+ me.rotationY = 75;
227
+ assert.equal(step(), 'Grab');
228
+ // (Through a lost frame another object in reach would take the offer over:
229
+ // the next test. Here there is none.)
230
+ place('b', 0, 4);
231
+ // A step back past the reach, inside its margin: still offered.
232
+ me.rotationY = 0;
233
+ place('a', 0, 2.2 + 0.3 + 0.03); // past the reach, inside its margin and the walking cap
234
+ assert.equal(step(), 'Grab');
235
+ // A frame where the multiplayer kit hands it to someone else.
236
+ mine.delete('a');
237
+ assert.equal(step(), 'Grab');
238
+ mine.add('a');
239
+ assert.equal(step(), 'Grab');
240
+ // A frame turned right away from it.
241
+ me.rotationY = 180;
242
+ assert.equal(step(), 'Grab');
243
+ me.rotationY = 0;
244
+ // The press grabs the kept object, not the nearer one.
245
+ assert.equal(step(16, true), 'Throw');
246
+ assert.deepEqual(grabs(), ['a']);
247
+ });
248
+
249
+ test('steady: the grace ends after 600 ms and the button goes; another object takes the offer over meanwhile', () => {
250
+ const { step, mine, place } = rig(
251
+ { ...GRAB_DEFAULTS, steady: true },
252
+ { a: at(1.5, 0), b: at(5, 0) },
253
+ );
254
+ assert.equal(step(), 'Grab');
255
+ mine.delete('a');
256
+ let t = 0;
257
+ while (t + 100 <= STEADY_GRACE_MS) {
258
+ assert.equal(step(100), 'Grab');
259
+ t += 100;
260
+ }
261
+ assert.equal(step(STEADY_GRACE_MS - t + 1), null);
262
+ assert.equal(step(), null);
263
+ // Back in this client's hands: offered again.
264
+ mine.add('a');
265
+ assert.equal(step(), 'Grab');
266
+ // Out of hands again, and another object comes into reach: it is offered.
267
+ mine.delete('a');
268
+ place('b', 0, 1);
269
+ assert.equal(step(), 'Grab');
270
+ assert.equal(step(STEADY_GRACE_MS + 100), 'Grab');
271
+ });
272
+
273
+ test('steady: a press in the grace waits for the object to be this client’s again, then grabs it', () => {
274
+ const { step, mine, grabs, sent } = rig({ ...GRAB_DEFAULTS, steady: true }, { a: at(1.5, 0) });
275
+ assert.equal(step(), 'Grab');
276
+ mine.delete('a');
277
+ assert.equal(step(16, true), 'Grab');
278
+ assert.equal(step(100), 'Grab');
279
+ assert.deepEqual(sent, []);
280
+ mine.add('a');
281
+ assert.equal(step(100), 'Throw');
282
+ assert.deepEqual(grabs(), ['a']);
283
+ });
284
+
285
+ test('steady: a press is dropped when the object never comes back, and no grab is ever sent for one this client does not own', () => {
286
+ const { step, mine, sent } = rig({ ...GRAB_DEFAULTS, steady: true }, { a: at(1.5, 0) });
287
+ assert.equal(step(), 'Grab');
288
+ mine.delete('a');
289
+ assert.equal(step(16, true), 'Grab');
290
+ for (let i = 0; i < 30; i += 1) {
291
+ step(50, i % 3 === 0);
292
+ }
293
+ assert.deepEqual(sent, []);
294
+ assert.equal(step(), null);
295
+ // Back after the press's wait: offered, but the old press does not grab it.
296
+ assert.ok(30 * 50 > PRESS_WAIT_MS + STEADY_GRACE_MS);
297
+ mine.add('a');
298
+ assert.equal(step(), 'Grab');
299
+ assert.equal(step(), 'Grab');
300
+ assert.deepEqual(sent, []);
301
+ });
302
+
303
+ test('steady: a hidden or removed object, or a player who cannot act, ends the offer at once', () => {
304
+ const { step, actors } = rig({ ...GRAB_DEFAULTS, steady: true }, { a: at(1.5, 0) });
305
+ assert.equal(step(), 'Grab');
306
+ hub.hidden.add('a');
307
+ assert.equal(step(), null);
308
+ hub.hidden.delete('a');
309
+ assert.equal(step(), 'Grab');
310
+ assert.equal(step(16, false, false), null);
311
+ assert.equal(step(), 'Grab');
312
+ actors.delete('a');
313
+ assert.equal(step(), null);
314
+ });
315
+
316
+ test('steady: nothing past the reach and its margin is kept, and nothing is first offered past the reach', () => {
317
+ const { step, place } = rig({ ...GRAB_DEFAULTS, steady: true }, { a: at(2.05, 0) });
318
+ // Near side 1.75 m: past the 1.6 m reach, never offered.
319
+ assert.equal(step(), null);
320
+ place('a', 0, 1.8);
321
+ assert.equal(step(), 'Grab');
322
+ // Past the reach and its margin: the grace runs out.
323
+ place('a', 0, 1.6 + 0.3 + KEEP_MARGIN_M + 0.1);
324
+ assert.equal(step(), 'Grab');
325
+ assert.equal(step(STEADY_GRACE_MS + 1), null);
326
+ });
327
+
328
+ console.log(`test-grab-client: ${n} passed`);