castle-web-cli 0.4.228 → 0.4.230

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 (236) hide show
  1. package/dist/agent-prompts.d.ts +11 -3
  2. package/dist/agent-prompts.js +153 -53
  3. package/dist/agent.d.ts +2 -0
  4. package/dist/agent.js +43 -33
  5. package/dist/api.d.ts +47 -2
  6. package/dist/api.js +85 -16
  7. package/dist/castle-host/host.d.ts +91 -0
  8. package/dist/castle-host/host.js +779 -3
  9. package/dist/castleJson.d.ts +59 -0
  10. package/dist/castleJson.js +267 -3
  11. package/dist/check.js +27 -1
  12. package/dist/cloudGit.d.ts +20 -1
  13. package/dist/cloudGit.js +267 -10
  14. package/dist/contentPack.d.ts +49 -0
  15. package/dist/contentPack.js +273 -0
  16. package/dist/devExperiments.d.ts +21 -0
  17. package/dist/devExperiments.js +139 -0
  18. package/dist/devHub.d.ts +55 -0
  19. package/dist/devHub.js +187 -0
  20. package/dist/devSessionChild.js +3 -1
  21. package/dist/devSessionServer.d.ts +14 -1
  22. package/dist/devSessionServer.js +229 -44
  23. package/dist/experimentAssignment.d.ts +15 -0
  24. package/dist/experimentAssignment.js +41 -0
  25. package/dist/importCheckHook.d.ts +40 -0
  26. package/dist/importCheckHook.js +171 -0
  27. package/dist/importRegression.d.ts +15 -0
  28. package/dist/importRegression.js +100 -0
  29. package/dist/importTrust.d.ts +2 -0
  30. package/dist/importTrust.js +27 -0
  31. package/dist/importUpdateHook.d.ts +18 -0
  32. package/dist/importUpdateHook.js +31 -8
  33. package/dist/importUpdateHookRunner.js +19 -7
  34. package/dist/imports.d.ts +16 -0
  35. package/dist/imports.js +145 -33
  36. package/dist/index.js +8 -1
  37. package/dist/init.d.ts +3 -0
  38. package/dist/init.js +125 -8
  39. package/dist/install.js +4 -1
  40. package/dist/localPaths.js +4 -0
  41. package/dist/login.js +2 -2
  42. package/dist/platformDoc.js +3 -8
  43. package/dist/playPagePolicy.js +3 -1
  44. package/dist/remix.js +5 -0
  45. package/dist/routerTaskReport.d.ts +28 -0
  46. package/dist/routerTaskReport.js +56 -0
  47. package/dist/save-deck.d.ts +18 -0
  48. package/dist/save-deck.js +145 -6
  49. package/dist/serve.d.ts +8 -0
  50. package/dist/serve.js +94 -13
  51. package/dist/serverBundle.d.ts +1 -1
  52. package/dist/serverBundle.js +5 -1
  53. package/dist/sourceLineage.d.ts +42 -0
  54. package/dist/sourceLineage.js +380 -0
  55. package/dist/webSourceSync.d.ts +11 -0
  56. package/dist/webSourceSync.js +26 -9
  57. package/kits/base/CLAUDE.md +2 -3
  58. package/kits/base/castle.json +1 -1
  59. package/kits/base/sdk/README.md +301 -52
  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 +6 -1
  64. package/kits/base/sdk/castle.js +3 -0
  65. package/kits/base/sdk/commands.d.ts +208 -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 +86 -6
  73. package/kits/base/sdk/multiplayer.js +157 -4
  74. package/kits/base/sdk/server/hub.d.ts +169 -0
  75. package/kits/base/sdk/server/hub.js +125 -0
  76. package/kits/base/sdk/server/platformHandle.d.ts +49 -2
  77. package/kits/base/sdk/server/storage.d.ts +55 -2
  78. package/kits/base/sdk/server/wrapper.d.ts +85 -6
  79. package/kits/base/sdk/server/wrapper.js +393 -30
  80. package/kits/base/sdk/transport.d.ts +2 -1
  81. package/kits/base/sdk/transport.js +13 -1
  82. package/kits/hub-game/.prettierrc +5 -0
  83. package/kits/hub-game/CLAUDE.md +146 -0
  84. package/kits/hub-game/blueprints/avatar-0.scene +28 -0
  85. package/kits/hub-game/blueprints/avatar-1.scene +28 -0
  86. package/kits/hub-game/blueprints/avatar-2.scene +28 -0
  87. package/kits/hub-game/blueprints/avatar-3.scene +28 -0
  88. package/kits/hub-game/blueprints/bean/avatar-0.scene +28 -0
  89. package/kits/hub-game/blueprints/bean/avatar-1.scene +28 -0
  90. package/kits/hub-game/blueprints/bean/avatar-2.scene +28 -0
  91. package/kits/hub-game/blueprints/bean/avatar-3.scene +28 -0
  92. package/kits/hub-game/blueprints/blocky/avatar-0.scene +28 -0
  93. package/kits/hub-game/blueprints/blocky/avatar-1.scene +28 -0
  94. package/kits/hub-game/blueprints/blocky/avatar-2.scene +28 -0
  95. package/kits/hub-game/blueprints/blocky/avatar-3.scene +28 -0
  96. package/kits/hub-game/blueprints/pixel/avatar-0.scene +28 -0
  97. package/kits/hub-game/blueprints/pixel/avatar-1.scene +28 -0
  98. package/kits/hub-game/blueprints/pixel/avatar-2.scene +28 -0
  99. package/kits/hub-game/blueprints/pixel/avatar-3.scene +28 -0
  100. package/kits/hub-game/castle.json +96 -0
  101. package/kits/hub-game/code/client/attention.js +133 -0
  102. package/kits/hub-game/code/client/avatar.js +288 -0
  103. package/kits/hub-game/code/client/cabinets.js +90 -0
  104. package/kits/hub-game/code/client/chat.js +135 -0
  105. package/kits/hub-game/code/client/chrome.js +1001 -0
  106. package/kits/hub-game/code/client/doorFrame.js +15 -0
  107. package/kits/hub-game/code/client/firstJoin.js +14 -0
  108. package/kits/hub-game/code/client/grab.js +241 -0
  109. package/kits/hub-game/code/client/hookLayer.js +230 -0
  110. package/kits/hub-game/code/client/idle.js +326 -0
  111. package/kits/hub-game/code/client/keys.js +48 -0
  112. package/kits/hub-game/code/client/pack.js +232 -0
  113. package/kits/hub-game/code/client/packFormat.js +397 -0
  114. package/kits/hub-game/code/client/say.js +127 -0
  115. package/kits/hub-game/code/client/sha256.js +80 -0
  116. package/kits/hub-game/code/client/sound.js +147 -0
  117. package/kits/hub-game/code/client/state.js +131 -0
  118. package/kits/hub-game/code/client/talk.js +259 -0
  119. package/kits/hub-game/code/client/together.js +365 -0
  120. package/kits/hub-game/code/client/wear.js +149 -0
  121. package/kits/hub-game/code/server/actors.js +627 -0
  122. package/kits/hub-game/code/server/blocks.js +103 -0
  123. package/kits/hub-game/code/server/cabinets.js +309 -0
  124. package/kits/hub-game/code/server/game.js +137 -0
  125. package/kits/hub-game/code/server/grab.js +247 -0
  126. package/kits/hub-game/code/server/history.js +96 -0
  127. package/kits/hub-game/code/server/hub.js +1904 -0
  128. package/kits/hub-game/code/server/idle.js +412 -0
  129. package/kits/hub-game/code/server/index.js +64 -0
  130. package/kits/hub-game/code/server/labels.js +59 -0
  131. package/kits/hub-game/code/server/manifest.js +527 -0
  132. package/kits/hub-game/code/server/npcs.js +745 -0
  133. package/kits/hub-game/code/server/party.js +381 -0
  134. package/kits/hub-game/code/server/place.js +43 -0
  135. package/kits/hub-game/code/server/say.js +83 -0
  136. package/kits/hub-game/code/server/wear.js +142 -0
  137. package/kits/hub-game/code/systems/hub.js +2230 -0
  138. package/kits/hub-game/docs/content-pack.md +151 -0
  139. package/kits/hub-game/docs/contract.md +1139 -0
  140. package/kits/hub-game/docs/style.md +137 -0
  141. package/kits/hub-game/drawings/blocky/castle-eye.sprite +58 -0
  142. package/kits/hub-game/drawings/blocky/dash.sprite +59 -0
  143. package/kits/hub-game/drawings/blocky/pom.sprite +59 -0
  144. package/kits/hub-game/drawings/blocky/tabby.sprite +59 -0
  145. package/kits/hub-game/drawings/pixel/castle-arm.sprite +13 -0
  146. package/kits/hub-game/drawings/pixel/castle-body.sprite +23 -0
  147. package/kits/hub-game/drawings/pixel/castle-leg.sprite +12 -0
  148. package/kits/hub-game/drawings/pixel/kai-arm.sprite +14 -0
  149. package/kits/hub-game/drawings/pixel/kai-head.sprite +24 -0
  150. package/kits/hub-game/drawings/pixel/kai-leg.sprite +15 -0
  151. package/kits/hub-game/drawings/pixel/kai-torso.sprite +18 -0
  152. package/kits/hub-game/drawings/pixel/mimi-arm.sprite +13 -0
  153. package/kits/hub-game/drawings/pixel/mimi-head.sprite +23 -0
  154. package/kits/hub-game/drawings/pixel/mimi-leg.sprite +15 -0
  155. package/kits/hub-game/drawings/pixel/mimi-torso.sprite +18 -0
  156. package/kits/hub-game/drawings/pixel/nox-arm.sprite +13 -0
  157. package/kits/hub-game/drawings/pixel/nox-head.sprite +21 -0
  158. package/kits/hub-game/drawings/pixel/nox-leg.sprite +14 -0
  159. package/kits/hub-game/drawings/pixel/nox-torso.sprite +18 -0
  160. package/kits/hub-game/eslint.config.js +20 -0
  161. package/kits/hub-game/fonts.generated.js +9 -0
  162. package/kits/hub-game/index.html +22 -0
  163. package/kits/hub-game/main.jsx +6 -0
  164. package/kits/hub-game/models/avatar-0.pxmodel +21 -0
  165. package/kits/hub-game/models/avatar-1.pxmodel +24 -0
  166. package/kits/hub-game/models/avatar-2.pxmodel +27 -0
  167. package/kits/hub-game/models/avatar-3.pxmodel +22 -0
  168. package/kits/hub-game/models/bean/avatar-0.pxmodel +19 -0
  169. package/kits/hub-game/models/bean/avatar-1.pxmodel +24 -0
  170. package/kits/hub-game/models/bean/avatar-2.pxmodel +22 -0
  171. package/kits/hub-game/models/bean/avatar-3.pxmodel +24 -0
  172. package/kits/hub-game/models/blocky/avatar-0.pxmodel +21 -0
  173. package/kits/hub-game/models/blocky/avatar-1.pxmodel +30 -0
  174. package/kits/hub-game/models/blocky/avatar-2.pxmodel +28 -0
  175. package/kits/hub-game/models/blocky/avatar-3.pxmodel +27 -0
  176. package/kits/hub-game/models/pixel/avatar-0.pxmodel +24 -0
  177. package/kits/hub-game/models/pixel/avatar-1.pxmodel +16 -0
  178. package/kits/hub-game/models/pixel/avatar-2.pxmodel +19 -0
  179. package/kits/hub-game/models/pixel/avatar-3.pxmodel +23 -0
  180. package/kits/hub-game/package-lock.json +365 -0
  181. package/kits/hub-game/package.json +32 -0
  182. package/kits/hub-game/scenes/main.scene +318 -0
  183. package/kits/hub-game/scripts/check.mjs +339 -0
  184. package/kits/hub-game/scripts/fixtures/sdk-stub.mjs +3 -0
  185. package/kits/hub-game/scripts/gen-avatars-bean.mjs +748 -0
  186. package/kits/hub-game/scripts/gen-avatars-blocky.mjs +607 -0
  187. package/kits/hub-game/scripts/gen-avatars-pixel.mjs +771 -0
  188. package/kits/hub-game/scripts/gen-avatars.mjs +621 -0
  189. package/kits/hub-game/scripts/pack.mjs +192 -0
  190. package/kits/hub-game/scripts/test-actors.mjs +666 -0
  191. package/kits/hub-game/scripts/test-attention.mjs +103 -0
  192. package/kits/hub-game/scripts/test-avatar.mjs +472 -0
  193. package/kits/hub-game/scripts/test-ban-join.mjs +194 -0
  194. package/kits/hub-game/scripts/test-blocks.mjs +351 -0
  195. package/kits/hub-game/scripts/test-chat.mjs +98 -0
  196. package/kits/hub-game/scripts/test-classic.mjs +590 -0
  197. package/kits/hub-game/scripts/test-door-jump.mjs +131 -0
  198. package/kits/hub-game/scripts/test-door.mjs +56 -0
  199. package/kits/hub-game/scripts/test-grab.mjs +331 -0
  200. package/kits/hub-game/scripts/test-hub-ops.mjs +835 -0
  201. package/kits/hub-game/scripts/test-hub-trip.mjs +1310 -0
  202. package/kits/hub-game/scripts/test-idle.mjs +518 -0
  203. package/kits/hub-game/scripts/test-interest.mjs +413 -0
  204. package/kits/hub-game/scripts/test-keys.mjs +50 -0
  205. package/kits/hub-game/scripts/test-manifest.mjs +224 -0
  206. package/kits/hub-game/scripts/test-no-forms.mjs +16 -0
  207. package/kits/hub-game/scripts/test-npcs.mjs +714 -0
  208. package/kits/hub-game/scripts/test-pack.mjs +424 -0
  209. package/kits/hub-game/scripts/test-party.mjs +286 -0
  210. package/kits/hub-game/scripts/test-say.mjs +149 -0
  211. package/kits/hub-game/scripts/test-together.mjs +183 -0
  212. package/kits/hub-game/theme.style +3 -0
  213. package/kits/multiplayer-3d/castle.json +5 -5
  214. package/kits/multiplayer-3d/code/client/avatars.js +101 -1
  215. package/kits/multiplayer-3d/code/server/players.js +4 -1
  216. package/kits/multiplayer-3d/code/server/sceneBodies.js +27 -6
  217. package/kits/multiplayer-3d/code/server/world.js +20 -7
  218. package/kits/multiplayer-3d/code/systems/multiplayer.js +222 -11
  219. package/kits/physics-2d/castle.json +2 -2
  220. package/kits/physics-2d/engine/art.js +13 -0
  221. package/kits/physics-2d/engine/assets.js +4 -2
  222. package/kits/physics-2d/engine/scene.js +25 -5
  223. package/kits/physics-3d/CLAUDE.md +6 -1
  224. package/kits/physics-3d/castle.json +3 -3
  225. package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +32 -4
  226. package/kits/physics-3d/engine3d/artobjects.js +2 -2
  227. package/kits/physics-3d/engine3d/materials.js +29 -1
  228. package/kits/physics-3d/engine3d/world3d.js +65 -1
  229. package/kits/physics-3d/systems/physics3d.js +5 -2
  230. package/kits/real-time/castle.json +2 -2
  231. package/kits/real-time/code/client/connection.js +77 -7
  232. package/kits/real-time/code/client/gameHooks.js +63 -3
  233. package/kits/real-time/code/client/messages.js +7 -0
  234. package/kits/real-time/code/server/gameHooks.js +24 -0
  235. package/kits/real-time/code/server/session.js +390 -22
  236. 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,241 @@
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
+ import { hub } from './state.js';
15
+
16
+ const DEG = Math.PI / 180;
17
+ // An object is offered when its near side is this close to the player
18
+ // (flat metres), within 60 degrees of where they face, and not far above or
19
+ // below them.
20
+ const REACH_M = 1.6;
21
+ const FRONT_COS = 0.5;
22
+ const RISE_M = 2;
23
+ // Objects wider than this (metres from centre to edge) are not offered.
24
+ const MAX_EXTENT = 2;
25
+ // How fast a held object follows its hold point: a pickup eases in rather
26
+ // than jumping, so each owner report stays far inside the server's limit on
27
+ // how far an owned object may move between reports.
28
+ const CARRY_SPEED = 20;
29
+ // A throw's upward part, as a share of its speed.
30
+ const THROW_UP = 0.25;
31
+ // After a throw, nothing is offered for this long: the object is still in
32
+ // front of the player as it leaves.
33
+ const REGRAB_MS = 400;
34
+ // Where the walk preset's capsule stands (physics-3d `Player`): its radius and
35
+ // height, and how far it hovers.
36
+ const PLAYER = { radius: 0.35, height: 1.4, hover: 0.15 };
37
+
38
+ export function makeGrab(chrome) {
39
+ const t = { chrome, held: null, net: null, pressed: false, quietUntil: 0, lastAt: 0 };
40
+ chrome.onGrab?.(() => {
41
+ t.pressed = true;
42
+ });
43
+ // E or F on a keyboard. Typing in the talk panel never reaches here (its
44
+ // input stops its keys), nor does any other text field.
45
+ const onKey = (event) => {
46
+ if (event.repeat || (event.code !== 'KeyE' && event.code !== 'KeyF')) {
47
+ return;
48
+ }
49
+ const target = event.target;
50
+ if (
51
+ target?.isContentEditable ||
52
+ target?.tagName === 'INPUT' ||
53
+ target?.tagName === 'TEXTAREA'
54
+ ) {
55
+ return;
56
+ }
57
+ t.pressed = true;
58
+ };
59
+ globalThis.addEventListener?.('keydown', onKey);
60
+ return {
61
+ frame: (scene, mp, local, idle) => frame(t, scene, mp, local, idle),
62
+ holding: () => t.held?.id ?? null,
63
+ dispose: () => globalThis.removeEventListener?.('keydown', onKey),
64
+ };
65
+ }
66
+
67
+ function frame(t, scene, mp, local, idle) {
68
+ const now = performance.now();
69
+ const dt = Math.min(0.1, Math.max(0, (now - (t.lastAt || now)) / 1000));
70
+ t.lastAt = now;
71
+ // A press counts in the frame it lands in, and only for what is offered then.
72
+ const pressed = t.pressed;
73
+ t.pressed = false;
74
+ const events = hub.grabEvents.splice(0);
75
+ const net = mp.net ?? null;
76
+ if (net !== t.net) {
77
+ // A new session: whatever was held belonged to the last one.
78
+ t.net = net;
79
+ t.held = null;
80
+ }
81
+ const config = hub.place?.grab ?? null;
82
+ const phys = scene.physics3d;
83
+ const self = net?.selfId?.();
84
+ const can = Boolean(
85
+ config && idle && net && local?.components.Transform && phys?.ready?.() && !hub.held.has(self),
86
+ );
87
+
88
+ if (t.held) {
89
+ const id = t.held.id;
90
+ const body = phys?.bodyOf?.(id) ?? null;
91
+ const told = events.some((e) => e.k === 'hub-drop' || e.actor === id);
92
+ // Holding ends when the place says so, when the multiplayer kit hands the
93
+ // object to someone else or back to the server, when the place hides it,
94
+ // or when the player cannot act (a trip, held still, a game's end).
95
+ if (
96
+ !can ||
97
+ told ||
98
+ !mp.own?.mine?.has(id) ||
99
+ hub.hidden.has(id) ||
100
+ !body ||
101
+ !scene.getActor(id)
102
+ ) {
103
+ t.held = null;
104
+ } else if (pressed) {
105
+ throwHeld(t, scene, body, local, config, net, now);
106
+ } else {
107
+ carry(t, scene, body, local, config, dt);
108
+ }
109
+ }
110
+ if (t.held) {
111
+ t.chrome.grabPrompt?.('Throw');
112
+ return;
113
+ }
114
+ const target = can && now >= t.quietUntil ? grabbable(scene, mp, local) : null;
115
+ if (target && pressed) {
116
+ const at = phys.bodyOf(target.id).translation();
117
+ t.held = {
118
+ id: target.id,
119
+ extent: target.extent,
120
+ half: target.half,
121
+ at: { x: at.x, y: at.y, z: at.z },
122
+ };
123
+ net.send({ k: 'hub-grab', actor: target.id, op: 'grab' });
124
+ carry(t, scene, phys.bodyOf(target.id), local, config, dt);
125
+ t.chrome.grabPrompt?.('Throw');
126
+ return;
127
+ }
128
+ t.chrome.grabPrompt?.(target ? 'Grab' : null);
129
+ }
130
+
131
+ // The nearest object this client owns in front of the player, or null.
132
+ function grabbable(scene, mp, local) {
133
+ const mine = mp.own?.mine;
134
+ if (!mine?.size || typeof mp.claimable !== 'function') {
135
+ return null;
136
+ }
137
+ const me = local.components.Transform;
138
+ const f = (me.rotationY ?? 0) * DEG;
139
+ const fx = Math.sin(f);
140
+ const fz = Math.cos(f);
141
+ let best = null;
142
+ for (const [id, at] of mp.claimable(scene)) {
143
+ if (!mine.has(id) || hub.hidden.has(id)) {
144
+ continue;
145
+ }
146
+ const actor = scene.getActor(id);
147
+ if (actor?.components.Body?.type !== 'dynamic' || !scene.physics3d.bodyOf(id)?.isDynamic?.()) {
148
+ continue;
149
+ }
150
+ const size = sizeOf(actor.components.Shape);
151
+ const dx = at.x - me.x;
152
+ const dz = at.z - me.z;
153
+ const d = Math.hypot(dx, dz);
154
+ if (size.extent > MAX_EXTENT || d - size.extent > REACH_M || Math.abs(at.y - me.y) > RISE_M) {
155
+ continue;
156
+ }
157
+ if (d > 0.01 && (dx * fx + dz * fz) / d < FRONT_COS) {
158
+ continue;
159
+ }
160
+ if (!best || d < best.d) {
161
+ best = { id, d, ...size };
162
+ }
163
+ }
164
+ return best;
165
+ }
166
+
167
+ // How far an object reaches out from its centre across the ground, and up.
168
+ function sizeOf(shape) {
169
+ const kind = shape?.kind ?? 'box';
170
+ const radius = typeof shape?.radius === 'number' ? shape.radius : 0.5;
171
+ const w = typeof shape?.width === 'number' ? shape.width : 1;
172
+ const h = typeof shape?.height === 'number' ? shape.height : 1;
173
+ const d = typeof shape?.depth === 'number' ? shape.depth : 1;
174
+ if (kind === 'sphere') {
175
+ return { extent: radius, half: radius };
176
+ }
177
+ if (kind === 'cylinder' || kind === 'capsule') {
178
+ return { extent: radius, half: kind === 'capsule' ? h / 2 + radius : h / 2 };
179
+ }
180
+ return { extent: Math.hypot(w, d) / 2, half: h / 2 };
181
+ }
182
+
183
+ // Where a held object goes: `holdDistance` in front of the player (never so
184
+ // close that it touches them) and `holdHeight` above their feet (never so low
185
+ // that it is in the ground).
186
+ function holdPoint(local, config, held) {
187
+ const t = local.components.Transform;
188
+ const shape = local.components.Shape;
189
+ const radius = typeof shape?.radius === 'number' ? shape.radius : PLAYER.radius;
190
+ const height = typeof shape?.height === 'number' ? shape.height : PLAYER.height;
191
+ const feet = t.y - (height / 2 + PLAYER.hover);
192
+ const f = (t.rotationY ?? 0) * DEG;
193
+ const distance = Math.max(config.holdDistance, radius + held.extent + 0.15);
194
+ return {
195
+ x: t.x + Math.sin(f) * distance,
196
+ y: feet + Math.max(config.holdHeight, held.half + 0.05),
197
+ z: t.z + Math.cos(f) * distance,
198
+ };
199
+ }
200
+
201
+ // Move the held object toward its hold point, at most CARRY_SPEED. It keeps
202
+ // the velocity it is carried at, which the multiplayer kit reports with its
203
+ // pose, so other players draw it moving smoothly.
204
+ function carry(t, scene, body, local, config, dt) {
205
+ const held = t.held;
206
+ const to = holdPoint(local, config, held);
207
+ const from = held.at;
208
+ const dx = to.x - from.x;
209
+ const dy = to.y - from.y;
210
+ const dz = to.z - from.z;
211
+ const d = Math.hypot(dx, dy, dz);
212
+ const most = CARRY_SPEED * dt;
213
+ const k = d > most && d > 0 ? most / d : 1;
214
+ const next = { x: from.x + dx * k, y: from.y + dy * k, z: from.z + dz * k };
215
+ body.setTranslation(next, true);
216
+ body.setLinvel(
217
+ dt > 0
218
+ ? { x: (next.x - from.x) / dt, y: (next.y - from.y) / dt, z: (next.z - from.z) / dt }
219
+ : { x: 0, y: 0, z: 0 },
220
+ true,
221
+ );
222
+ body.setAngvel({ x: 0, y: 0, z: 0 }, true);
223
+ held.at = next;
224
+ // Drawn where it is this frame; physics writes the Transform after its step.
225
+ const transform = scene.getActor(held.id)?.components.Transform;
226
+ if (transform) {
227
+ transform.x = next.x;
228
+ transform.y = next.y;
229
+ transform.z = next.z;
230
+ }
231
+ }
232
+
233
+ // Let go along the player's facing at `throwSpeed`, a little upward.
234
+ function throwHeld(t, scene, body, local, config, net, now) {
235
+ const f = (local.components.Transform.rotationY ?? 0) * DEG;
236
+ const speed = config.throwSpeed;
237
+ body.setLinvel({ x: Math.sin(f) * speed, y: speed * THROW_UP, z: Math.cos(f) * speed }, true);
238
+ net.send({ k: 'hub-grab', actor: t.held.id, op: 'throw' });
239
+ t.held = null;
240
+ t.quietUntil = now + REGRAB_MS;
241
+ }
@@ -0,0 +1,230 @@
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 { 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
+
17
+ let files = null;
18
+ // The scene and multiplayer system of the newest snapshot, for a message that
19
+ // changes avatars between snapshots.
20
+ let netSystem = null;
21
+ // The avatar set the other players were last drawn from (`Avatar.style`).
22
+ let drawnStyle = null;
23
+
24
+ // Everyone's avatars with this player's own over them. A player whose avatar
25
+ // changed is dropped and respawns in the new one.
26
+ function setAvatars(scene, system) {
27
+ const next = new Map([...hub.avatarsAll, ...hub.avatarsMe]);
28
+ if (system) {
29
+ const keep = new Map(system.net?.others() ?? []);
30
+ let changed = false;
31
+ for (const [playerId, avatar] of next) {
32
+ if (hub.avatars.has(playerId) && hub.avatars.get(playerId) !== avatar) {
33
+ keep.delete(playerId);
34
+ changed = true;
35
+ }
36
+ }
37
+ if (changed) {
38
+ Avatars.dropMissing(system.avatars, scene, keep);
39
+ }
40
+ }
41
+ hub.avatars = next;
42
+ }
43
+
44
+ // Everyone's wearables with this player's own over them (v1.4).
45
+ function setWearing() {
46
+ hub.wearing = new Map([...hub.wearAll, ...hub.wearMe]);
47
+ }
48
+
49
+ function wearMap(value) {
50
+ return new Map(Object.entries(value && typeof value === 'object' ? value : {}));
51
+ }
52
+
53
+ export default {
54
+ // A place whose NPCs have minds (`hub.npcs`, contract §13) is joined like any
55
+ // other, on one of its public layers, when the SDK can address the place's
56
+ // characters from a layer (`Multiplayer.placeCharacters`): ghost and the host
57
+ // decide whether that layer gets them. On an older SDK it is a world joined
58
+ // through its destination route; where that cannot be placed (the dev serve,
59
+ // an account the route is not open to) it is the public session and its NPCs
60
+ // speak authored lines. Only the main place: every other place is reached
61
+ // through a door, which names its own session.
62
+ firstJoin() {
63
+ return firstJoinFor(castleJson, castleSdk.Multiplayer);
64
+ },
65
+
66
+ message(scene, msg) {
67
+ files = scene.files ?? files;
68
+ switch (msg.k) {
69
+ case 'hub-go':
70
+ case 'hub-end':
71
+ case 'hub-result':
72
+ hub.commands.push(msg);
73
+ break;
74
+ case 'hub-tune':
75
+ hub.tune = msg.tune && typeof msg.tune === 'object' ? msg.tune : null;
76
+ break;
77
+ case 'hub-hud':
78
+ hub.hudMe = msg.hud ?? null;
79
+ break;
80
+ case 'hub-arrived':
81
+ hub.arrivedFacing = typeof msg.facing === 'number' ? msg.facing : 0;
82
+ break;
83
+ case 'hub-jumped':
84
+ // A jump to a door: the camera turns to look at it.
85
+ if (typeof msg.facing === 'number') {
86
+ (scene.persistent ??= {}).camAngle = Math.PI - msg.facing;
87
+ }
88
+ // Beside a player: the hub system says whom (a start next to a friend).
89
+ if (typeof msg.to === 'string') {
90
+ hub.jumpedTo = msg.to;
91
+ }
92
+ break;
93
+ case 'hub-where':
94
+ // Laid over what is known: one answer covers at most twelve players.
95
+ Object.assign(hub.whereAt, msg.at && typeof msg.at === 'object' ? msg.at : {});
96
+ hub.whereAtAt = performance.now();
97
+ break;
98
+ case 'hub-said':
99
+ if (hub.saidEvents.length < 64) {
100
+ hub.saidEvents.push(msg);
101
+ }
102
+ break;
103
+ case 'hub-party':
104
+ hub.party = {
105
+ members: Array.isArray(msg.members) ? msg.members : null,
106
+ away: Number(msg.away) || 0,
107
+ invite: msg.invite ?? null,
108
+ proposal: msg.proposal ?? null,
109
+ };
110
+ break;
111
+ case 'hub-av':
112
+ hub.avatarsMe = new Map(Object.entries(msg.hubAv && typeof msg.hubAv === 'object' ? msg.hubAv : {}));
113
+ setAvatars(netSystem?.scene ?? scene, netSystem?.system ?? null);
114
+ break;
115
+ case 'hub-wear':
116
+ hub.wearMe = wearMap(msg.hubWear);
117
+ setWearing();
118
+ break;
119
+ case 'hub-labels':
120
+ hub.labelsMe = msg.labels && typeof msg.labels === 'object' ? msg.labels : {};
121
+ break;
122
+ case 'hub-npc':
123
+ case 'hub-npc-line':
124
+ case 'hub-npc-notes':
125
+ hub.npcEvents.push(msg);
126
+ break;
127
+ case 'hub-grab-deny':
128
+ case 'hub-drop':
129
+ hub.grabEvents.push(msg);
130
+ break;
131
+ case 'hub-cabinet':
132
+ // A cabinet's card, sent as this player came near it (v1.7).
133
+ Cabinets.receive(msg);
134
+ break;
135
+ case 'hub-sound':
136
+ // At most a few unplayed one-shots wait: a backgrounded tab plays none late.
137
+ if (hub.soundEvents.length < 8) {
138
+ hub.soundEvents.push(msg);
139
+ }
140
+ break;
141
+ default:
142
+ break;
143
+ }
144
+ },
145
+
146
+ state(scene, msg, system) {
147
+ files = scene.files ?? files;
148
+ if (typeof msg.t === 'number') {
149
+ hub.lastT = msg.t;
150
+ hub.lastTAt = msg.localMs ?? performance.now();
151
+ }
152
+ if (msg.hubPlace) {
153
+ hub.place = msg.hubPlace;
154
+ }
155
+ netSystem = { scene, system };
156
+ // The viewer's set changed under a running page (the experiment answered
157
+ // after boot): everyone is dropped and respawns from the new set.
158
+ const style = Avatar.style();
159
+ if (drawnStyle !== null && drawnStyle !== style && system?.avatars) {
160
+ Avatars.dropMissing(system.avatars, scene, new Map());
161
+ // One still fading out would come back as it was if its player returned.
162
+ for (const { actorId } of system.avatars.leaving.values()) {
163
+ if (scene.getActor(actorId)) {
164
+ system.avatars.retired?.add(actorId);
165
+ scene.despawnActor(actorId);
166
+ }
167
+ }
168
+ system.avatars.leaving.clear();
169
+ }
170
+ drawnStyle = style;
171
+ if (msg.hubAv) {
172
+ hub.avatarsAll = new Map(Object.entries(msg.hubAv));
173
+ setAvatars(scene, system);
174
+ }
175
+ if (msg.hubWear) {
176
+ hub.wearAll = wearMap(msg.hubWear);
177
+ setWearing();
178
+ }
179
+ if ('hud' in msg) {
180
+ hub.hud = msg.hud ?? null;
181
+ }
182
+ if (msg.hidden) {
183
+ hub.hidden = new Set(msg.hidden);
184
+ }
185
+ if (msg.tints) {
186
+ hub.tints = msg.tints;
187
+ }
188
+ if (msg.hubDoors) {
189
+ hub.doors = msg.hubDoors;
190
+ }
191
+ if (msg.marks) {
192
+ hub.marks = msg.marks;
193
+ }
194
+ if (msg.spawnTemplates) {
195
+ hub.spawnTemplates = msg.spawnTemplates;
196
+ }
197
+ if (msg.spawned) {
198
+ hub.spawned = msg.spawned;
199
+ }
200
+ if (msg.held) {
201
+ hub.held = new Set(msg.held);
202
+ }
203
+ if (msg.labels) {
204
+ hub.labels = msg.labels;
205
+ }
206
+ // v1.6: only a place that turns idle play on sends these.
207
+ if (msg.hubEmotes && typeof msg.hubEmotes === 'object') {
208
+ hub.emotes = msg.hubEmotes;
209
+ }
210
+ if (msg.hubSeated && typeof msg.hubSeated === 'object') {
211
+ hub.seated = msg.hubSeated;
212
+ }
213
+ if (msg.hubSounds && typeof msg.hubSounds === 'object') {
214
+ hub.sounds = msg.hubSounds;
215
+ }
216
+ },
217
+
218
+ // Everyone is drawn as their avatar; until a player's hello has reached the
219
+ // place they are not drawn, so nobody flashes in a default body. The set
220
+ // is this viewer's own (`Avatar.style`).
221
+ avatar(playerId, username) {
222
+ const index = hub.avatars.has(playerId) ? hub.avatars.get(playerId) : null;
223
+ const blueprint = index === null ? null : Avatar.blueprint(files, index);
224
+ return blueprint ? { blueprint, username } : null;
225
+ },
226
+
227
+ presence(status) {
228
+ return status.solo ? 'solo' : `${status.others + 1} here`;
229
+ },
230
+ };