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,15 @@
1
+ // Where things are around a door, in its own frame. A door faces its local +z
2
+ // (players walk up to it from there) and is as wide as its Shape along local
3
+ // x. Kept apart from the hub system so the server, tests and the client agree.
4
+
5
+ // A point in a door's frame -- `along` its width (+x right as you face it),
6
+ // `out` in front of it (+z) -- in world x/z. The engine turns a Transform by
7
+ // rotationY degrees about +y, so local +x lands on (cos, -sin) and +z on
8
+ // (sin, cos).
9
+ export function doorPoint(base, along, out) {
10
+ const r = ((base.rotationY ?? 0) * Math.PI) / 180;
11
+ return {
12
+ x: base.x + Math.cos(r) * along + Math.sin(r) * out,
13
+ z: base.z - Math.sin(r) * along + Math.cos(r) * out,
14
+ };
15
+ }
@@ -0,0 +1,14 @@
1
+ // What the main place's first join asks for (contract §13), apart from the
2
+ // hook so it can be tested without a bundler. `castleJson` is the deck's,
3
+ // `multiplayer` the SDK's `Multiplayer`.
4
+ //
5
+ // undefined is the public session: a layer of the place. A place whose NPCs
6
+ // have minds (`hub.npcs`) joins its `npcs.route` world instead only on an SDK
7
+ // that cannot address the place's characters from a layer.
8
+ export function firstJoinFor(castleJson, multiplayer) {
9
+ const npcs = castleJson?.hub?.npcs;
10
+ if (typeof npcs?.route !== 'string' || multiplayer?.placeCharacters === true) {
11
+ return undefined;
12
+ }
13
+ return { destination: { routeId: npcs.route, slot: typeof npcs.slot === 'string' ? npcs.slot : 'main' }, fallback: { mode: 'public' } };
14
+ }
@@ -0,0 +1,376 @@
1
+ // Grabbing and throwing (contract §6, v1.3), the client's half: a Tier 0 game
2
+ // has no client code, so the hub kit offers the input, holds the object and
3
+ // lets it go. Only in a place whose manifest says `grab`.
4
+ //
5
+ // What can be grabbed is what castle.multiplayer-3d already lets this client
6
+ // simulate: a loose scene object (`claimable`) it owns (`own.mine`), because
7
+ // the player is the nearest one in reach. A held object is put in front of the
8
+ // player every frame, so it stays in reach and this client stays its owner;
9
+ // the multiplayer kit reports its pose as it reports any owned object, and the
10
+ // server bounds those reports as before. A throw is the velocity it is let go
11
+ // with. The place's server hears each grab and throw (`hub-grab`) and may
12
+ // refuse a grab or end a hold (`hub-grab-deny`, `hub-drop`).
13
+ //
14
+ // A place may ask for a steadier offer (v1.22, `grab.reach` and
15
+ // `grab.steady`): a longer reach, and an offer that does not flicker at its
16
+ // edges. A place that asks for neither runs exactly the code it always did.
17
+
18
+ import { hub } from './state.js';
19
+
20
+ const DEG = Math.PI / 180;
21
+ // An object is offered when its near side is this close to the player
22
+ // (flat metres), within 60 degrees of where they face, and not far above or
23
+ // below them.
24
+ const REACH_M = 1.6;
25
+ const FRONT_COS = 0.5;
26
+ const RISE_M = 2;
27
+ // Objects wider than this (metres from centre to edge) are not offered.
28
+ const MAX_EXTENT = 2;
29
+ // How fast a held object follows its hold point: a pickup eases in rather
30
+ // than jumping, so each owner report stays far inside the server's limit on
31
+ // how far an owned object may move between reports.
32
+ const CARRY_SPEED = 20;
33
+ // A throw's upward part, as a share of its speed.
34
+ const THROW_UP = 0.25;
35
+ // After a throw, nothing is offered for this long: the object is still in
36
+ // front of the player as it leaves.
37
+ const REGRAB_MS = 400;
38
+ // v1.22, only where the place sets `grab.reach` or `grab.steady`: the centre
39
+ // of an offered object is never farther than this from the player's centre.
40
+ // The server refuses a grab past its GRAB_REACH (4 m, from the poses it has,
41
+ // which trail a moving player's: a grab can be heard before that frame's
42
+ // movement report). With `reach` or `steady`, the client keeps inside it by
43
+ // what the player may travel in that time at the speed they are moving now
44
+ // (their body's own velocity, so an authored speed, a tune, a jump and a tune
45
+ // not yet applied are all counted) or the speed they are tuned to, whichever
46
+ // is more. It is a margin, not a promise: a grab the server still refuses is
47
+ // let go, as any refused grab is (`hub-grab-deny`).
48
+ export const OFFER_CENTRE_M = 3;
49
+ const WALK_SPEED = 6; // the walk preset's speed, metres a second
50
+ const POSE_LAG_S = 0.2;
51
+ const LAG_SLACK_M = 0.25;
52
+ // `speed` is the controller's own (the kit's applyTune has already multiplied
53
+ // it by the tune); without one, the walk preset's times the tune.
54
+ export function offerCap(velocity = null, tune = hub.tune, speed = null) {
55
+ const moving = velocity ? Math.hypot(velocity.x ?? 0, velocity.y ?? 0, velocity.z ?? 0) : 0;
56
+ const tuned = Number(speed) > 0 ? Number(speed) : WALK_SPEED * Math.max(1, Number(tune?.speed) || 1);
57
+ return Math.max(0, Math.min(OFFER_CENTRE_M, 4 - LAG_SLACK_M - Math.max(moving, tuned) * POSE_LAG_S));
58
+ }
59
+ // The cap for this player now: their body's velocity and authored speed.
60
+ function capFor(scene, local) {
61
+ const body = scene.physics3d?.bodyOf?.(local.id);
62
+ return offerCap(body?.linvel?.() ?? null, hub.tune, local.components?.Player?.speed);
63
+ }
64
+ // v1.22 `grab.steady`: once an object is offered it stays the one offered
65
+ // while its near side is within the reach and this much more, and within 90
66
+ // degrees of facing. Through a moment where it is not (its owner briefly
67
+ // someone else, a turn past the cone, a step back), the button stays up for
68
+ // STEADY_GRACE_MS; a press then waits up to PRESS_WAIT_MS for the object to
69
+ // be this client's again, and is dropped if it never is.
70
+ export const KEEP_MARGIN_M = 0.4;
71
+ const KEEP_COS = 0;
72
+ export const STEADY_GRACE_MS = 600;
73
+ export const PRESS_WAIT_MS = 500;
74
+ // Where the walk preset's capsule stands (physics-3d `Player`): its radius and
75
+ // height, and how far it hovers.
76
+ const PLAYER = { radius: 0.35, height: 1.4, hover: 0.15 };
77
+
78
+ export function makeGrab(chrome) {
79
+ const t = {
80
+ chrome,
81
+ held: null,
82
+ net: null,
83
+ pressed: false,
84
+ quietUntil: 0,
85
+ lastAt: 0,
86
+ // v1.22 `steady` only: the object kept on offer `{ id, okAt }`, and a press
87
+ // waiting for it `{ id, until }`.
88
+ offer: null,
89
+ waiting: null,
90
+ };
91
+ chrome.onGrab?.(() => {
92
+ t.pressed = true;
93
+ });
94
+ // E or F on a keyboard. Typing in the talk panel never reaches here (its
95
+ // input stops its keys), nor does any other text field.
96
+ const onKey = (event) => {
97
+ if (event.repeat || (event.code !== 'KeyE' && event.code !== 'KeyF')) {
98
+ return;
99
+ }
100
+ const target = event.target;
101
+ if (
102
+ target?.isContentEditable ||
103
+ target?.tagName === 'INPUT' ||
104
+ target?.tagName === 'TEXTAREA'
105
+ ) {
106
+ return;
107
+ }
108
+ t.pressed = true;
109
+ };
110
+ globalThis.addEventListener?.('keydown', onKey);
111
+ return {
112
+ frame: (scene, mp, local, idle) => frame(t, scene, mp, local, idle),
113
+ holding: () => t.held?.id ?? null,
114
+ dispose: () => globalThis.removeEventListener?.('keydown', onKey),
115
+ };
116
+ }
117
+
118
+ function frame(t, scene, mp, local, idle) {
119
+ const now = performance.now();
120
+ const dt = Math.min(0.1, Math.max(0, (now - (t.lastAt || now)) / 1000));
121
+ t.lastAt = now;
122
+ // A press counts in the frame it lands in, and only for what is offered then.
123
+ const pressed = t.pressed;
124
+ t.pressed = false;
125
+ const events = hub.grabEvents.splice(0);
126
+ const net = mp.net ?? null;
127
+ if (net !== t.net) {
128
+ // A new session: whatever was held belonged to the last one.
129
+ t.net = net;
130
+ t.held = null;
131
+ t.offer = null;
132
+ t.waiting = null;
133
+ }
134
+ const config = hub.place?.grab ?? null;
135
+ const phys = scene.physics3d;
136
+ const self = net?.selfId?.();
137
+ const can = Boolean(
138
+ config && idle && net && local?.components.Transform && phys?.ready?.() && !hub.held.has(self),
139
+ );
140
+
141
+ if (t.held) {
142
+ const id = t.held.id;
143
+ const body = phys?.bodyOf?.(id) ?? null;
144
+ const told = events.some((e) => e.k === 'hub-drop' || e.actor === id);
145
+ // Holding ends when the place says so, when the multiplayer kit hands the
146
+ // object to someone else or back to the server, when the place hides it,
147
+ // or when the player cannot act (a trip, held still, a game's end).
148
+ if (
149
+ !can ||
150
+ told ||
151
+ !mp.own?.mine?.has(id) ||
152
+ hub.hidden.has(id) ||
153
+ !body ||
154
+ !scene.getActor(id)
155
+ ) {
156
+ t.held = null;
157
+ } else if (pressed) {
158
+ throwHeld(t, scene, body, local, config, net, now);
159
+ } else {
160
+ carry(t, scene, body, local, config, dt);
161
+ }
162
+ }
163
+ if (t.held) {
164
+ t.offer = null;
165
+ t.waiting = null;
166
+ t.chrome.grabPrompt?.('Throw');
167
+ return;
168
+ }
169
+ if (config?.steady === true) {
170
+ steadyFrame(t, scene, mp, local, config, net, can && now >= t.quietUntil, pressed, now, dt);
171
+ return;
172
+ }
173
+ const reach = config?.reach;
174
+ const target =
175
+ can && now >= t.quietUntil
176
+ ? reach === undefined
177
+ ? grabbable(scene, mp, local)
178
+ : grabbable(scene, mp, local, reach, FRONT_COS, capFor(scene, local))
179
+ : null;
180
+ if (target && pressed) {
181
+ const at = phys.bodyOf(target.id).translation();
182
+ t.held = {
183
+ id: target.id,
184
+ extent: target.extent,
185
+ half: target.half,
186
+ at: { x: at.x, y: at.y, z: at.z },
187
+ };
188
+ net.send({ k: 'hub-grab', actor: target.id, op: 'grab' });
189
+ carry(t, scene, phys.bodyOf(target.id), local, config, dt);
190
+ t.chrome.grabPrompt?.('Throw');
191
+ return;
192
+ }
193
+ t.chrome.grabPrompt?.(target ? 'Grab' : null);
194
+ }
195
+
196
+ // v1.22 `grab.steady`: the offer with hysteresis. Only an object that passes
197
+ // the full test this frame -- this client's own, in reach, in front -- is ever
198
+ // grabbed; the grace only keeps the button up and a press waiting.
199
+ function steadyFrame(t, scene, mp, local, config, net, active, pressed, now, dt) {
200
+ if (!active) {
201
+ t.offer = null;
202
+ t.waiting = null;
203
+ t.chrome.grabPrompt?.(null);
204
+ return;
205
+ }
206
+ const reach = config.reach ?? REACH_M;
207
+ const kept = t.offer;
208
+ // The kept object holds the offer on the wider test; when it fails this
209
+ // frame (or there is none), one passing the ordinary test takes over.
210
+ const ready =
211
+ (kept &&
212
+ grabbable(scene, mp, local, reach + KEEP_MARGIN_M, KEEP_COS, capFor(scene, local), kept.id)) ||
213
+ grabbable(scene, mp, local, reach, FRONT_COS, capFor(scene, local));
214
+ if (ready) {
215
+ t.offer = { id: ready.id, okAt: now };
216
+ } else if (kept && (now - kept.okAt > STEADY_GRACE_MS || !stillThere(scene, kept.id))) {
217
+ t.offer = null;
218
+ }
219
+ if (t.waiting && (now > t.waiting.until || !stillThere(scene, t.waiting.id))) {
220
+ t.waiting = null;
221
+ }
222
+ if (pressed && (ready || t.offer)) {
223
+ const id = ready?.id ?? t.offer.id;
224
+ t.waiting = { id, until: now + PRESS_WAIT_MS };
225
+ }
226
+ if (!ready || t.waiting?.id !== ready.id) {
227
+ t.chrome.grabPrompt?.(t.offer || t.waiting ? 'Grab' : null);
228
+ return;
229
+ }
230
+ t.waiting = null;
231
+ t.offer = null;
232
+ const body = scene.physics3d.bodyOf(ready.id);
233
+ const at = body.translation();
234
+ t.held = {
235
+ id: ready.id,
236
+ extent: ready.extent,
237
+ half: ready.half,
238
+ at: { x: at.x, y: at.y, z: at.z },
239
+ };
240
+ net.send({ k: 'hub-grab', actor: ready.id, op: 'grab' });
241
+ carry(t, scene, body, local, config, dt);
242
+ t.chrome.grabPrompt?.('Throw');
243
+ }
244
+
245
+ // Whether an object kept on offer can still come back: in the scene, not
246
+ // hidden by the place. Anything else may change within a grace.
247
+ function stillThere(scene, id) {
248
+ return Boolean(scene.getActor(id)) && !hub.hidden.has(id);
249
+ }
250
+
251
+ // The nearest object this client owns in front of the player, or null. With
252
+ // no more arguments it is the test every place had before v1.22; `reach`,
253
+ // `cos` and `centre` (the farthest an object's centre may be from the
254
+ // player's) carry a place's `grab.reach` and `grab.steady`, and `only` limits
255
+ // the test to one object.
256
+ function grabbable(
257
+ scene,
258
+ mp,
259
+ local,
260
+ reach = REACH_M,
261
+ cos = FRONT_COS,
262
+ centre = Infinity,
263
+ only = null,
264
+ ) {
265
+ const mine = mp.own?.mine;
266
+ if (!mine?.size || typeof mp.claimable !== 'function') {
267
+ return null;
268
+ }
269
+ const me = local.components.Transform;
270
+ const f = (me.rotationY ?? 0) * DEG;
271
+ const fx = Math.sin(f);
272
+ const fz = Math.cos(f);
273
+ let best = null;
274
+ for (const [id, at] of mp.claimable(scene)) {
275
+ if ((only !== null && id !== only) || !mine.has(id) || hub.hidden.has(id)) {
276
+ continue;
277
+ }
278
+ const actor = scene.getActor(id);
279
+ if (actor?.components.Body?.type !== 'dynamic' || !scene.physics3d.bodyOf(id)?.isDynamic?.()) {
280
+ continue;
281
+ }
282
+ const size = sizeOf(actor.components.Shape);
283
+ const dx = at.x - me.x;
284
+ const dz = at.z - me.z;
285
+ const d = Math.hypot(dx, dz);
286
+ if (size.extent > MAX_EXTENT || d - size.extent > reach || Math.abs(at.y - me.y) > RISE_M) {
287
+ continue;
288
+ }
289
+ if (d > 0.01 && (dx * fx + dz * fz) / d < cos) {
290
+ continue;
291
+ }
292
+ if (Math.hypot(d, at.y - me.y) > centre) {
293
+ continue;
294
+ }
295
+ if (!best || d < best.d) {
296
+ best = { id, d, ...size };
297
+ }
298
+ }
299
+ return best;
300
+ }
301
+
302
+ // How far an object reaches out from its centre across the ground, and up.
303
+ function sizeOf(shape) {
304
+ const kind = shape?.kind ?? 'box';
305
+ const radius = typeof shape?.radius === 'number' ? shape.radius : 0.5;
306
+ const w = typeof shape?.width === 'number' ? shape.width : 1;
307
+ const h = typeof shape?.height === 'number' ? shape.height : 1;
308
+ const d = typeof shape?.depth === 'number' ? shape.depth : 1;
309
+ if (kind === 'sphere') {
310
+ return { extent: radius, half: radius };
311
+ }
312
+ if (kind === 'cylinder' || kind === 'capsule') {
313
+ return { extent: radius, half: kind === 'capsule' ? h / 2 + radius : h / 2 };
314
+ }
315
+ return { extent: Math.hypot(w, d) / 2, half: h / 2 };
316
+ }
317
+
318
+ // Where a held object goes: `holdDistance` in front of the player (never so
319
+ // close that it touches them) and `holdHeight` above their feet (never so low
320
+ // that it is in the ground).
321
+ function holdPoint(local, config, held) {
322
+ const t = local.components.Transform;
323
+ const shape = local.components.Shape;
324
+ const radius = typeof shape?.radius === 'number' ? shape.radius : PLAYER.radius;
325
+ const height = typeof shape?.height === 'number' ? shape.height : PLAYER.height;
326
+ const feet = t.y - (height / 2 + PLAYER.hover);
327
+ const f = (t.rotationY ?? 0) * DEG;
328
+ const distance = Math.max(config.holdDistance, radius + held.extent + 0.15);
329
+ return {
330
+ x: t.x + Math.sin(f) * distance,
331
+ y: feet + Math.max(config.holdHeight, held.half + 0.05),
332
+ z: t.z + Math.cos(f) * distance,
333
+ };
334
+ }
335
+
336
+ // Move the held object toward its hold point, at most CARRY_SPEED. It keeps
337
+ // the velocity it is carried at, which the multiplayer kit reports with its
338
+ // pose, so other players draw it moving smoothly.
339
+ function carry(t, scene, body, local, config, dt) {
340
+ const held = t.held;
341
+ const to = holdPoint(local, config, held);
342
+ const from = held.at;
343
+ const dx = to.x - from.x;
344
+ const dy = to.y - from.y;
345
+ const dz = to.z - from.z;
346
+ const d = Math.hypot(dx, dy, dz);
347
+ const most = CARRY_SPEED * dt;
348
+ const k = d > most && d > 0 ? most / d : 1;
349
+ const next = { x: from.x + dx * k, y: from.y + dy * k, z: from.z + dz * k };
350
+ body.setTranslation(next, true);
351
+ body.setLinvel(
352
+ dt > 0
353
+ ? { x: (next.x - from.x) / dt, y: (next.y - from.y) / dt, z: (next.z - from.z) / dt }
354
+ : { x: 0, y: 0, z: 0 },
355
+ true,
356
+ );
357
+ body.setAngvel({ x: 0, y: 0, z: 0 }, true);
358
+ held.at = next;
359
+ // Drawn where it is this frame; physics writes the Transform after its step.
360
+ const transform = scene.getActor(held.id)?.components.Transform;
361
+ if (transform) {
362
+ transform.x = next.x;
363
+ transform.y = next.y;
364
+ transform.z = next.z;
365
+ }
366
+ }
367
+
368
+ // Let go along the player's facing at `throwSpeed`, a little upward.
369
+ function throwHeld(t, scene, body, local, config, net, now) {
370
+ const f = (local.components.Transform.rotationY ?? 0) * DEG;
371
+ const speed = config.throwSpeed;
372
+ body.setLinvel({ x: Math.sin(f) * speed, y: speed * THROW_UP, z: Math.cos(f) * speed }, true);
373
+ net.send({ k: 'hub-grab', actor: t.held.id, op: 'throw' });
374
+ t.held = null;
375
+ t.quietUntil = now + REGRAB_MS;
376
+ }
@@ -0,0 +1,236 @@
1
+ // The hub's client hooks, layered under the deck's by `castle.real-time`
2
+ // (`code/client/gameHooks.js` composes every import's `hookLayer.js`). A hub
3
+ // game ships no client code, so these are the only hooks its runtime has: they
4
+ // read what the place's server declares into `state.js`, and the hub system
5
+ // draws it.
6
+
7
+ import * as castleSdk from 'castle-web-sdk';
8
+ import * as Avatars from '@imports/castle.multiplayer-3d/code/client/avatars.js';
9
+ import * as Avatar from './avatar.js';
10
+ import * as Cabinets from './cabinets.js';
11
+ import { firstJoinFor } from './firstJoin.js';
12
+ import { deck, hub } from './state.js';
13
+
14
+ // Root-anchored: the deck's own castle.json once this kit is imported.
15
+ const castleJson = Object.values(import.meta.glob('/castle.json', { eager: true, import: 'default' }))[0] ?? {};
16
+ deck.tiers = castleJson?.hub?.tiers === true;
17
+
18
+ let files = null;
19
+ // The scene and multiplayer system of the newest snapshot, for a message that
20
+ // changes avatars between snapshots.
21
+ let netSystem = null;
22
+ // The avatar set the other players were last drawn from (`Avatar.style`).
23
+ let drawnStyle = null;
24
+
25
+ // Everyone's avatars with this player's own over them. A player whose avatar
26
+ // changed is dropped and respawns in the new one.
27
+ function setAvatars(scene, system) {
28
+ const next = new Map([...hub.avatarsAll, ...hub.avatarsMe]);
29
+ if (system) {
30
+ const keep = new Map(system.net?.others() ?? []);
31
+ let changed = false;
32
+ for (const [playerId, avatar] of next) {
33
+ if (hub.avatars.has(playerId) && hub.avatars.get(playerId) !== avatar) {
34
+ keep.delete(playerId);
35
+ changed = true;
36
+ }
37
+ }
38
+ if (changed) {
39
+ Avatars.dropMissing(system.avatars, scene, keep);
40
+ }
41
+ }
42
+ hub.avatars = next;
43
+ }
44
+
45
+ // Everyone's wearables with this player's own over them (v1.4).
46
+ function setWearing() {
47
+ hub.wearing = new Map([...hub.wearAll, ...hub.wearMe]);
48
+ }
49
+
50
+ function wearMap(value) {
51
+ return new Map(Object.entries(value && typeof value === 'object' ? value : {}));
52
+ }
53
+
54
+ export default {
55
+ // A place whose NPCs have minds (`hub.npcs`, contract §13) is joined like any
56
+ // other, on one of its public layers, when the SDK can address the place's
57
+ // characters from a layer (`Multiplayer.placeCharacters`): ghost and the host
58
+ // decide whether that layer gets them. On an older SDK it is a world joined
59
+ // through its destination route; where that cannot be placed (the dev serve,
60
+ // an account the route is not open to) it is the public session and its NPCs
61
+ // speak authored lines. Only the main place: every other place is reached
62
+ // through a door, which names its own session.
63
+ firstJoin() {
64
+ return firstJoinFor(castleJson, castleSdk.Multiplayer);
65
+ },
66
+
67
+ message(scene, msg) {
68
+ files = scene.files ?? files;
69
+ switch (msg.k) {
70
+ case 'hub-go':
71
+ case 'hub-end':
72
+ case 'hub-result':
73
+ hub.commands.push(msg);
74
+ break;
75
+ case 'hub-tune':
76
+ hub.tune = msg.tune && typeof msg.tune === 'object' ? msg.tune : null;
77
+ break;
78
+ case 'hub-hud':
79
+ hub.hudMe = msg.hud ?? null;
80
+ break;
81
+ case 'hub-arrived':
82
+ hub.arrivedFacing = typeof msg.facing === 'number' ? msg.facing : 0;
83
+ break;
84
+ case 'hub-jumped':
85
+ // A jump to a door: the camera turns to look at it.
86
+ if (typeof msg.facing === 'number') {
87
+ (scene.persistent ??= {}).camAngle = Math.PI - msg.facing;
88
+ }
89
+ // Beside a player: the hub system says whom (a start next to a friend).
90
+ if (typeof msg.to === 'string') {
91
+ hub.jumpedTo = msg.to;
92
+ }
93
+ break;
94
+ case 'hub-where':
95
+ // Laid over what is known: one answer covers at most twelve players.
96
+ Object.assign(hub.whereAt, msg.at && typeof msg.at === 'object' ? msg.at : {});
97
+ hub.whereAtAt = performance.now();
98
+ break;
99
+ case 'hub-went':
100
+ if (hub.wentEvents.length < 32 && Array.isArray(msg.players) && typeof msg.door === 'string') {
101
+ hub.wentEvents.push(msg);
102
+ }
103
+ break;
104
+ case 'hub-said':
105
+ if (hub.saidEvents.length < 64) {
106
+ hub.saidEvents.push(msg);
107
+ }
108
+ break;
109
+ case 'hub-party':
110
+ hub.party = {
111
+ members: Array.isArray(msg.members) ? msg.members : null,
112
+ away: Number(msg.away) || 0,
113
+ invite: msg.invite ?? null,
114
+ proposal: msg.proposal ?? null,
115
+ };
116
+ break;
117
+ case 'hub-av':
118
+ hub.avatarsMe = new Map(Object.entries(msg.hubAv && typeof msg.hubAv === 'object' ? msg.hubAv : {}));
119
+ setAvatars(netSystem?.scene ?? scene, netSystem?.system ?? null);
120
+ break;
121
+ case 'hub-wear':
122
+ hub.wearMe = wearMap(msg.hubWear);
123
+ setWearing();
124
+ break;
125
+ case 'hub-labels':
126
+ hub.labelsMe = msg.labels && typeof msg.labels === 'object' ? msg.labels : {};
127
+ break;
128
+ case 'hub-npc':
129
+ case 'hub-npc-line':
130
+ case 'hub-npc-notes':
131
+ hub.npcEvents.push(msg);
132
+ break;
133
+ case 'hub-grab-deny':
134
+ case 'hub-drop':
135
+ hub.grabEvents.push(msg);
136
+ break;
137
+ case 'hub-cabinet':
138
+ // A cabinet's card, sent as this player came near it (v1.7).
139
+ Cabinets.receive(msg);
140
+ break;
141
+ case 'hub-sound':
142
+ // At most a few unplayed one-shots wait: a backgrounded tab plays none late.
143
+ if (hub.soundEvents.length < 8) {
144
+ hub.soundEvents.push(msg);
145
+ }
146
+ break;
147
+ default:
148
+ break;
149
+ }
150
+ },
151
+
152
+ state(scene, msg, system) {
153
+ files = scene.files ?? files;
154
+ if (typeof msg.t === 'number') {
155
+ hub.lastT = msg.t;
156
+ hub.lastTAt = msg.localMs ?? performance.now();
157
+ }
158
+ if (msg.hubPlace) {
159
+ hub.place = msg.hubPlace;
160
+ }
161
+ netSystem = { scene, system };
162
+ // The viewer's set changed under a running page (the experiment answered
163
+ // after boot): everyone is dropped and respawns from the new set.
164
+ const style = Avatar.style();
165
+ if (drawnStyle !== null && drawnStyle !== style && system?.avatars) {
166
+ Avatars.dropMissing(system.avatars, scene, new Map());
167
+ // One still fading out would come back as it was if its player returned.
168
+ for (const { actorId } of system.avatars.leaving.values()) {
169
+ if (scene.getActor(actorId)) {
170
+ system.avatars.retired?.add(actorId);
171
+ scene.despawnActor(actorId);
172
+ }
173
+ }
174
+ system.avatars.leaving.clear();
175
+ }
176
+ drawnStyle = style;
177
+ if (msg.hubAv) {
178
+ hub.avatarsAll = new Map(Object.entries(msg.hubAv));
179
+ setAvatars(scene, system);
180
+ }
181
+ if (msg.hubWear) {
182
+ hub.wearAll = wearMap(msg.hubWear);
183
+ setWearing();
184
+ }
185
+ if ('hud' in msg) {
186
+ hub.hud = msg.hud ?? null;
187
+ }
188
+ if (msg.hidden) {
189
+ hub.hidden = new Set(msg.hidden);
190
+ }
191
+ if (msg.tints) {
192
+ hub.tints = msg.tints;
193
+ }
194
+ if (msg.hubDoors) {
195
+ hub.doors = msg.hubDoors;
196
+ }
197
+ if (msg.marks) {
198
+ hub.marks = msg.marks;
199
+ }
200
+ if (msg.spawnTemplates) {
201
+ hub.spawnTemplates = msg.spawnTemplates;
202
+ }
203
+ if (msg.spawned) {
204
+ hub.spawned = msg.spawned;
205
+ }
206
+ if (msg.held) {
207
+ hub.held = new Set(msg.held);
208
+ }
209
+ if (msg.labels) {
210
+ hub.labels = msg.labels;
211
+ }
212
+ // v1.6: only a place that turns idle play on sends these.
213
+ if (msg.hubEmotes && typeof msg.hubEmotes === 'object') {
214
+ hub.emotes = msg.hubEmotes;
215
+ }
216
+ if (msg.hubSeated && typeof msg.hubSeated === 'object') {
217
+ hub.seated = msg.hubSeated;
218
+ }
219
+ if (msg.hubSounds && typeof msg.hubSounds === 'object') {
220
+ hub.sounds = msg.hubSounds;
221
+ }
222
+ },
223
+
224
+ // Everyone is drawn as their avatar; until a player's hello has reached the
225
+ // place they are not drawn, so nobody flashes in a default body. The set
226
+ // is this viewer's own (`Avatar.style`).
227
+ avatar(playerId, username) {
228
+ const index = hub.avatars.has(playerId) ? hub.avatars.get(playerId) : null;
229
+ const blueprint = index === null ? null : Avatar.blueprint(files, index);
230
+ return blueprint ? { blueprint, username } : null;
231
+ },
232
+
233
+ presence(status) {
234
+ return status.solo ? 'solo' : `${status.others + 1} here`;
235
+ },
236
+ };