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,412 @@
1
+ // Idle play (contract §6, v1.6), the server's half: emotes, seats and the
2
+ // place's own sounds, for a place whose manifest says `idle`. A Tier 0 place
3
+ // has no client code, so the hub kit offers the input (an emote picker, Sit
4
+ // and Stand), the place's server checks it, and every client draws it.
5
+ //
6
+ // { k: 'hub-emote', id } client -> server: play an emote on my avatar
7
+ // { k: 'hub-sit', seat } client -> server: sit on this seat
8
+ // { k: 'hub-stand' } client -> server: get up
9
+ // { k: 'hub-sound', actor, file, volume, radius } server -> everyone: a one-shot
10
+ //
11
+ // What everyone sees rides the snapshot, latched: `hubEmotes` (player id ->
12
+ // `{ id, at, until }` in simulation ms), `hubSeated` (player id -> seat actor
13
+ // id) and `hubSounds` (actor id -> `{ file, volume, radius, since }`, the
14
+ // loops playing). Each is sent only by a place that turned that part on, so a
15
+ // place without `idle` is sent exactly what it was.
16
+ //
17
+ // A seat is a hold (`h.held`, contract §8) that carries the seat's id: the
18
+ // kit's hold mechanics keep the sitter there, and the place's own `hub.hold`
19
+ // or `hub.release` replaces or ends it, which stands them (`tick`).
20
+
21
+ import { warnOnce, isActor } from './actors.js';
22
+
23
+ const DEG = Math.PI / 180;
24
+
25
+ // The kit's emotes and how long each plays (ms). Drawn by the hub client
26
+ // (`code/client/idle.js`) as a motion of the avatar plus an emoji over it.
27
+ export const EMOTES = { wave: 2000, dance: 2400, cheer: 1600, bow: 1600, spin: 1200 };
28
+ // One emote a player may start every this many simulation ms.
29
+ export const EMOTE_EVERY_MS = 2000;
30
+ // How far (flat metres) from a seat's footprint a player may be when they ask
31
+ // to sit: the client offers one within 1.5 m of it, and the server's poses lag a
32
+ // moving player.
33
+ export const SIT_REACH = 2.5;
34
+ // A standing body's centre above its feet (the walk preset's capsule and
35
+ // hover), which is where a sitter is held over the seat's top by default.
36
+ export const SIT_BODY = 0.85;
37
+ // hub-sit and hub-stand messages each player may send a second, with a burst.
38
+ const SIT_RATE = 3;
39
+ const SIT_BURST = 3;
40
+ // Sounds: loops playing at once in a place, one-shots a second (with a burst),
41
+ // and the bounds of a sound's falloff radius (metres).
42
+ export const MAX_LOOPS = 4;
43
+ export const SHOT_RATE = 4;
44
+ export const SHOT_BURST = 8;
45
+ export const RADIUS = { min: 1, max: 60, default: 20 };
46
+
47
+ export function newIdleState() {
48
+ return {
49
+ // playerId -> { id, at, until }
50
+ emotes: new Map(),
51
+ // playerId -> when their last emote began (simulation ms)
52
+ emoteAt: new Map(),
53
+ // playerId -> seat actor id
54
+ seated: new Map(),
55
+ // playerId -> { tokens, at }
56
+ sitBudget: new Map(),
57
+ // actorId -> { file, volume, radius, since }
58
+ loops: new Map(),
59
+ shotBudget: { tokens: SHOT_BURST, at: null },
60
+ };
61
+ }
62
+
63
+ function idleOf(h) {
64
+ return h.place?.idle ?? null;
65
+ }
66
+
67
+ function present(h, playerId) {
68
+ const entry = h.players.get(playerId);
69
+ return Boolean(entry?.arrived && !entry.ended && h.sim?.players.has(playerId));
70
+ }
71
+
72
+ // A place hook that may veto; one that throws vetoes too, so a broken rule
73
+ // errs toward nothing happening.
74
+ function ask(h, name, ...args) {
75
+ if (typeof h.hooks[name] !== 'function') {
76
+ return true;
77
+ }
78
+ try {
79
+ return h.hooks[name](h.ctx, ...args) !== false;
80
+ } catch (err) {
81
+ console.warn(`[hub] ${name} hook failed: ${err?.message ?? err}`);
82
+ return false;
83
+ }
84
+ }
85
+
86
+ // --- emotes ---------------------------------------------------------------
87
+
88
+ function startEmote(h, playerId, id) {
89
+ const now = h.sim.timeMs;
90
+ h.emotes.set(playerId, { id, at: now, until: now + EMOTES[id] });
91
+ h.emoteAt.set(playerId, now);
92
+ }
93
+
94
+ // `{ k: 'hub-emote', id }` from a client: a kit emote, from a player who is
95
+ // here, at most one every EMOTE_EVERY_MS, and not vetoed by the place's
96
+ // `emote` hook. Anything else is dropped.
97
+ export function emoteMessage(h, playerId, data) {
98
+ const id = data?.id;
99
+ if (!idleOf(h)?.emotes || !h.sim || typeof id !== 'string' || !Object.hasOwn(EMOTES, id)) {
100
+ return;
101
+ }
102
+ if (!present(h, playerId)) {
103
+ return;
104
+ }
105
+ const last = h.emoteAt.get(playerId);
106
+ if (last !== undefined && h.sim.timeMs - last < EMOTE_EVERY_MS) {
107
+ return;
108
+ }
109
+ // The cooldown is spent before the hook is asked, so a place that refuses
110
+ // is not asked more often than an emote could play.
111
+ h.emoteAt.set(playerId, h.sim.timeMs);
112
+ if (!ask(h, 'emote', playerId, id)) {
113
+ return;
114
+ }
115
+ startEmote(h, playerId, id);
116
+ }
117
+
118
+ // `hub.emote(playerId, id)`: the place plays an emote on a player (an NPC
119
+ // greeting them, a goal). Not rate-limited and not offered to the hook: it is
120
+ // the place's own call. True when it took.
121
+ export function emote(h, playerId, id) {
122
+ if (!idleOf(h)?.emotes) {
123
+ warnOnce(h, 'emote:off', 'hub.emote: this place does not turn on idle.emotes');
124
+ return false;
125
+ }
126
+ if (typeof id !== 'string' || !Object.hasOwn(EMOTES, id)) {
127
+ warnOnce(h, `emote:${id}`, `hub.emote: "${id}" is not an emote (${Object.keys(EMOTES).join(', ')})`);
128
+ return false;
129
+ }
130
+ if (!h.sim?.players.has(playerId)) {
131
+ warnOnce(h, `emote:${playerId}`, `hub.emote: "${playerId}" is not a player here`);
132
+ return false;
133
+ }
134
+ startEmote(h, playerId, id);
135
+ return true;
136
+ }
137
+
138
+ function emotesNow(h) {
139
+ const now = h.sim?.timeMs ?? 0;
140
+ const out = {};
141
+ for (const [playerId, e] of h.emotes) {
142
+ if (now >= e.until) {
143
+ h.emotes.delete(playerId);
144
+ continue;
145
+ }
146
+ out[playerId] = { ...e };
147
+ }
148
+ return out;
149
+ }
150
+
151
+ // --- seats ----------------------------------------------------------------
152
+
153
+ // A seat of the place: a scene actor carrying `HubSeat` (`{ face, height }`),
154
+ // not hidden. `at` is where its sitter's body is held, `facing` radians. A
155
+ // seat stays where it was authored: an actor with a dynamic or kinematic
156
+ // `Body` (one that can move) is not a seat, since its sitter would be held
157
+ // where it no longer is.
158
+ export function isMovable(actor) {
159
+ const type = actor?.components?.Body?.type;
160
+ return type === 'dynamic' || type === 'kinematic';
161
+ }
162
+
163
+ export function seatOf(h, seatId) {
164
+ if (typeof seatId !== 'string' || seatId.length > 128 || h.hidden.has(seatId)) {
165
+ return null;
166
+ }
167
+ const actor = h.actors.find((a) => a.id === seatId);
168
+ const props = actor?.components.HubSeat;
169
+ if (!props || isMovable(actor)) {
170
+ return null;
171
+ }
172
+ const seat = typeof props === 'object' ? props : {};
173
+ const t = actor.components.Transform ?? {};
174
+ const shape = actor.components.Shape;
175
+ const top = !shape ? 0 : shape.kind === 'sphere' ? (shape.radius ?? 0.5) : (shape.height ?? 1) / 2;
176
+ const height = typeof seat.height === 'number' && Number.isFinite(seat.height) ? seat.height : top + SIT_BODY;
177
+ const face = typeof seat.face === 'number' && Number.isFinite(seat.face) ? seat.face : (t.rotationY ?? 0);
178
+ return {
179
+ id: seatId,
180
+ at: { x: t.x ?? 0, y: (t.y ?? 0) + height, z: t.z ?? 0 },
181
+ facing: face * DEG,
182
+ };
183
+ }
184
+
185
+ // Who sits where: only sitters whose hold is still their seat's.
186
+ function sitting(h, playerId) {
187
+ const seatId = h.seated.get(playerId);
188
+ return seatId !== undefined && h.held.get(playerId)?.seat === seatId ? seatId : null;
189
+ }
190
+
191
+ function taken(h, seatId, by) {
192
+ for (const playerId of h.seated.keys()) {
193
+ if (playerId !== by && sitting(h, playerId) === seatId) {
194
+ return true;
195
+ }
196
+ }
197
+ return false;
198
+ }
199
+
200
+ function spendSit(h, playerId) {
201
+ const now = h.sim.timeMs;
202
+ const budget = h.sitBudget.get(playerId) ?? { tokens: SIT_BURST, at: now };
203
+ budget.tokens = Math.min(SIT_BURST, budget.tokens + ((now - budget.at) / 1000) * SIT_RATE);
204
+ budget.at = now;
205
+ h.sitBudget.set(playerId, budget);
206
+ if (budget.tokens < 1) {
207
+ return false;
208
+ }
209
+ budget.tokens -= 1;
210
+ return true;
211
+ }
212
+
213
+ // `{ k: 'hub-sit', seat }` from a client. Refused (dropped): a place without
214
+ // seats, past the rate, a seat that is not one or is taken, a player not
215
+ // here, held still by the place, or too far, or the place's `sit` hook said
216
+ // no. Otherwise the player is held on the seat, facing its way.
217
+ // Flat distance from a point to a seat's Shape footprint (its box turned by
218
+ // rotationY, or its circle): the client offers a seat along its whole length.
219
+ function footprintDistance(actor, p) {
220
+ const t = actor.components.Transform ?? {};
221
+ const shape = actor.components.Shape ?? {};
222
+ const dx = p.x - (t.x ?? 0);
223
+ const dz = p.z - (t.z ?? 0);
224
+ if (shape.kind !== 'box' && shape.kind !== undefined) {
225
+ return Math.max(0, Math.hypot(dx, dz) - (shape.radius ?? 0));
226
+ }
227
+ const r = ((t.rotationY ?? 0) * Math.PI) / 180;
228
+ const lx = dx * Math.cos(r) - dz * Math.sin(r);
229
+ const lz = dx * Math.sin(r) + dz * Math.cos(r);
230
+ return Math.hypot(Math.max(0, Math.abs(lx) - (shape.width ?? 0) / 2), Math.max(0, Math.abs(lz) - (shape.depth ?? 0) / 2));
231
+ }
232
+
233
+ export function sitMessage(h, placeAt, playerId, data) {
234
+ if (!idleOf(h)?.seats || !h.sim || !spendSit(h, playerId)) {
235
+ return;
236
+ }
237
+ const seat = seatOf(h, data?.seat);
238
+ const body = h.sim.players.get(playerId);
239
+ if (!seat || !body || !present(h, playerId)) {
240
+ return;
241
+ }
242
+ const hold = h.held.get(playerId);
243
+ if (hold && !hold.seat) {
244
+ return;
245
+ }
246
+ if (sitting(h, playerId) === seat.id || taken(h, seat.id, playerId)) {
247
+ return;
248
+ }
249
+ const actor = h.actors.find((x) => x.id === seat.id);
250
+ if (!actor || footprintDistance(actor, body.at) > SIT_REACH || Math.abs(body.at.y - seat.at.y) > 3) {
251
+ return;
252
+ }
253
+ if (!ask(h, 'sit', playerId, seat.id)) {
254
+ return;
255
+ }
256
+ // The hook may have changed things: held or ended the player, hidden the
257
+ // seat, seated someone else there. Only a sit that still stands is taken.
258
+ const after = h.held.get(playerId);
259
+ if (
260
+ !present(h, playerId) ||
261
+ (after && !after.seat) ||
262
+ !seatOf(h, seat.id) ||
263
+ taken(h, seat.id, playerId)
264
+ ) {
265
+ return;
266
+ }
267
+ h.held.set(playerId, { at: { ...seat.at }, facing: seat.facing, seat: seat.id });
268
+ h.seated.set(playerId, seat.id);
269
+ placeAt(h, playerId, seat.at, seat.facing);
270
+ }
271
+
272
+ // `{ k: 'hub-stand' }` from a client: a sitter gets up where they are.
273
+ export function standMessage(h, playerId) {
274
+ if (!idleOf(h)?.seats || !h.sim || !spendSit(h, playerId)) {
275
+ return;
276
+ }
277
+ stand(h, playerId);
278
+ }
279
+
280
+ // Ends a player's seat (and the hold that is it); true when they were sitting.
281
+ export function stand(h, playerId) {
282
+ if (sitting(h, playerId) === null) {
283
+ return false;
284
+ }
285
+ h.held.delete(playerId);
286
+ h.seated.delete(playerId);
287
+ return true;
288
+ }
289
+
290
+ // `hub.seat(playerId)`: the seat actor id they sit on, or null.
291
+ export function seat(h, playerId) {
292
+ return sitting(h, playerId);
293
+ }
294
+
295
+ function seatedNow(h) {
296
+ const out = {};
297
+ for (const playerId of h.seated.keys()) {
298
+ const seatId = sitting(h, playerId);
299
+ if (seatId !== null) {
300
+ out[playerId] = seatId;
301
+ }
302
+ }
303
+ return out;
304
+ }
305
+
306
+ // --- sounds ---------------------------------------------------------------
307
+
308
+ function spendShot(h) {
309
+ const now = h.sim?.timeMs ?? 0;
310
+ const b = h.shotBudget;
311
+ b.tokens = b.at === null ? b.tokens : Math.min(SHOT_BURST, b.tokens + ((now - b.at) / 1000) * SHOT_RATE);
312
+ b.at = now;
313
+ if (b.tokens < 1) {
314
+ return false;
315
+ }
316
+ b.tokens -= 1;
317
+ return true;
318
+ }
319
+
320
+ // `hub.sound(actorId, file, { loop, volume, radius })`: one of the place's
321
+ // declared sound files, played at a scene actor for everyone, fading out to
322
+ // nothing `radius` metres from it. A loop plays until `hub.sound(actorId,
323
+ // null)` or another sound replaces it at that actor; a one-shot plays once.
324
+ // True when it took.
325
+ export function sound(h, actorId, file, options = {}) {
326
+ const sounds = idleOf(h)?.sounds ?? [];
327
+ if (!sounds.length) {
328
+ warnOnce(h, 'sound:off', 'hub.sound: this place declares no idle.sounds');
329
+ return false;
330
+ }
331
+ if (!isActor(h, actorId)) {
332
+ warnOnce(h, `sound:${actorId}`, `hub.sound: "${actorId}" is not an actor of this place's scene`);
333
+ return false;
334
+ }
335
+ if (file === null || file === undefined) {
336
+ h.loops.delete(actorId);
337
+ return true;
338
+ }
339
+ if (!sounds.includes(file)) {
340
+ warnOnce(h, `sound:${file}`, `hub.sound: "${file}" is not one of this place's idle.sounds`);
341
+ return false;
342
+ }
343
+ const o = options && typeof options === 'object' ? options : {};
344
+ const volume = typeof o.volume === 'number' && Number.isFinite(o.volume) ? Math.min(1, Math.max(0, o.volume)) : 1;
345
+ const radius =
346
+ typeof o.radius === 'number' && Number.isFinite(o.radius)
347
+ ? Math.min(RADIUS.max, Math.max(RADIUS.min, o.radius))
348
+ : RADIUS.default;
349
+ if (o.loop === true) {
350
+ if (!h.loops.has(actorId) && h.loops.size >= MAX_LOOPS) {
351
+ warnOnce(h, 'sound:loops', `hub.sound: at most ${MAX_LOOPS} loops play at once in a place`);
352
+ return false;
353
+ }
354
+ h.loops.set(actorId, { file, volume, radius, since: h.sim?.timeMs ?? 0 });
355
+ return true;
356
+ }
357
+ if (!spendShot(h)) {
358
+ warnOnce(h, 'sound:rate', `hub.sound: at most ${SHOT_RATE} one-shot sounds a second`);
359
+ return false;
360
+ }
361
+ // Another sound at an actor replaces the loop playing there.
362
+ h.loops.delete(actorId);
363
+ h.session?.broadcast({ k: 'hub-sound', actor: actorId, file, volume, radius });
364
+ return true;
365
+ }
366
+
367
+ function loopsNow(h) {
368
+ const out = {};
369
+ for (const [actorId, loop] of h.loops) {
370
+ // A copy the place despawned takes its loop with it.
371
+ if (!isActor(h, actorId)) {
372
+ h.loops.delete(actorId);
373
+ continue;
374
+ }
375
+ out[actorId] = { ...loop };
376
+ }
377
+ return out;
378
+ }
379
+
380
+ // --- each tick, the snapshot, and leaving ---------------------------------
381
+
382
+ // A seat ends when its hold does: the place released the player, held them
383
+ // itself, ended their visit, or they left; or the place hid the seat.
384
+ export function tick(h) {
385
+ for (const [playerId, seatId] of [...h.seated]) {
386
+ if (!h.sim.players.has(playerId) || h.held.get(playerId)?.seat !== seatId) {
387
+ h.seated.delete(playerId);
388
+ } else if (h.hidden.has(seatId)) {
389
+ stand(h, playerId);
390
+ }
391
+ }
392
+ }
393
+
394
+ // The idle fields for a snapshot: only the parts this place turned on.
395
+ export function fields(h) {
396
+ const idle = idleOf(h);
397
+ if (!idle) {
398
+ return {};
399
+ }
400
+ return {
401
+ ...(idle.emotes ? { hubEmotes: emotesNow(h) } : {}),
402
+ ...(idle.seats ? { hubSeated: seatedNow(h) } : {}),
403
+ ...(idle.sounds.length ? { hubSounds: loopsNow(h) } : {}),
404
+ };
405
+ }
406
+
407
+ export function forget(h, playerId) {
408
+ h.emotes.delete(playerId);
409
+ h.emoteAt.delete(playerId);
410
+ h.seated.delete(playerId);
411
+ h.sitBudget.delete(playerId);
412
+ }
@@ -0,0 +1,64 @@
1
+ // The entry point for a hub game's session process: multiplayer-3d's session,
2
+ // with the hub layer (`hub.js`) wrapped around the deck's own hooks. A hub
3
+ // game's `castle.json` points `server.main` here and never forks it; the game
4
+ // is `code/server/game.js`, or `code/server/places/<id>.js` per place.
5
+
6
+ import { makeSession } from '@imports/castle.real-time/code/server/session.js';
7
+ import { game as deckGame } from '@imports/castle.real-time/code/server/gameHooks.js';
8
+ import * as Poses from '@imports/castle.multiplayer-3d/code/client/poses.js';
9
+ import * as Ownership from '@imports/castle.multiplayer-3d/code/server/ownership.js';
10
+ import * as Players from '@imports/castle.multiplayer-3d/code/server/players.js';
11
+ import * as World from '@imports/castle.multiplayer-3d/code/server/world.js';
12
+ import * as Limits from '@imports/castle.multiplayer-3d/code/server/limits.js';
13
+ import { resolveScenePath } from '@imports/castle.multiplayer-3d/code/server/sceneBodies.js';
14
+ import * as Hub from './hub.js';
15
+
16
+ // Root-anchored, so these are the deck's files once this kit is imported.
17
+ const castleJson = Object.values(import.meta.glob('/castle.json', { eager: true, import: 'default' }))[0] ?? {};
18
+ const placeModules = import.meta.glob('/code/server/places/*.js', { eager: true });
19
+ const sceneTexts = import.meta.glob('/scenes/*.scene', { eager: true, query: '?raw', import: 'default' });
20
+
21
+ // A scene's own `ground` setting: a scene with `{ none: true }` has no ground,
22
+ // so the hub catches whoever falls (contract §5, `fallY`).
23
+ function sceneGround(path) {
24
+ const text = sceneTexts[`/${path}`];
25
+ try {
26
+ return text ? (JSON.parse(text).ground ?? null) : null;
27
+ } catch {
28
+ return null;
29
+ }
30
+ }
31
+
32
+ const placeHooks = {};
33
+ for (const [path, mod] of Object.entries(placeModules)) {
34
+ const id = path.slice('/code/server/places/'.length, -'.js'.length);
35
+ placeHooks[id] = mod.default ?? {};
36
+ }
37
+
38
+ // On `castle-web serve` only: the castle.json of each deck this deck's
39
+ // `devDecks` names, so a `deck:<name>` door reads the destination's own
40
+ // manifest rather than repeating it in its props.
41
+ const devDecks = import.meta.env.CASTLE_DEV_DECKS ?? null;
42
+
43
+ const engine = {
44
+ createBody: World.createBody,
45
+ removeActor: World.removeActor,
46
+ maxSpeed: () => Limits.MAX_SPEED,
47
+ release: Ownership.release,
48
+ };
49
+ const hub = Hub.layer(castleJson, deckGame, placeHooks, (path) => resolveScenePath(path) === path, sceneGround, devDecks, engine);
50
+
51
+ export default {
52
+ ...makeSession({
53
+ label: 'hub',
54
+ poses: Poses,
55
+ world: World,
56
+ players: Players,
57
+ ownership: Ownership,
58
+ game: hub,
59
+ }),
60
+ // A place's NPCs answer the shared-character requests of a session the host
61
+ // brokers the place's characters into (a layer of the place, or a world
62
+ // joined through its `npcs.route`); the platform calls these only there.
63
+ ...hub.worldCharacters,
64
+ };
@@ -0,0 +1,59 @@
1
+ // Words over a scene actor (contract §10, §13), kept apart so they can be
2
+ // tested without the engine. `h` is the hub layer's state: `labels` for
3
+ // everyone, `labelsMe` per player, `sim` for the clock.
4
+
5
+ const LABEL_MAX = 64;
6
+ // Speech carries an NPC's whole reply.
7
+ const SPEECH_MAX = 240;
8
+
9
+ // Words over an actor. An NPC's own speech (`npc`, from `npcs.js`) runs to
10
+ // SPEECH_MAX and its bubble is kept on screen; everything said through
11
+ // `hub.label` is drawn exactly as before NPCs existed.
12
+ // `about` names the player the words concern (an NPC greeting them by name):
13
+ // nobody a block hides from that player is ever sent it (`blocks.js`).
14
+ export function setLabel(h, actorId, text, { style = 'tag', ttlMs, to, about } = {}, npc) {
15
+ if (!actorId) {
16
+ return;
17
+ }
18
+ const targets = Array.isArray(to) ? to.filter((id) => typeof id === 'string') : null;
19
+ const clear = text === null || text === undefined || text === '';
20
+ const speech = style === 'speech';
21
+ const label = clear
22
+ ? null
23
+ : {
24
+ text: String(text).slice(0, npc && speech ? SPEECH_MAX : LABEL_MAX),
25
+ // A sign (a street's or a building's name) is drawn at any distance
26
+ // in a town, where other labels wait until the player is near.
27
+ // A goal (v1.13) is one player's own: where the place is sending them,
28
+ // pointed at from the screen's edge while it is out of view.
29
+ style: speech ? 'speech' : style === 'goal' && targets ? 'goal' : style === 'sign' || style === 'goal' ? 'sign' : 'tag',
30
+ until: ttlMs ? (h.sim?.timeMs ?? 0) + ttlMs : null,
31
+ npc: Boolean(npc && speech),
32
+ ...(typeof about === 'string' ? { about } : {}),
33
+ };
34
+ if (!targets) {
35
+ if (label) {
36
+ h.labels.set(actorId, label);
37
+ } else {
38
+ h.labels.delete(actorId);
39
+ }
40
+ return;
41
+ }
42
+ for (const playerId of targets) {
43
+ let mine = h.labelsMe.get(playerId);
44
+ if (!mine) {
45
+ mine = new Map();
46
+ h.labelsMe.set(playerId, mine);
47
+ }
48
+ if (label) {
49
+ mine.set(actorId, label);
50
+ } else {
51
+ mine.delete(actorId);
52
+ }
53
+ }
54
+ }
55
+
56
+ export function wireLabel(label) {
57
+ return label.npc ? { text: label.text, style: label.style, npc: true } : { text: label.text, style: label.style };
58
+ }
59
+