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,147 @@
1
+ // The place's sounds (contract §6, v1.6, `hub.sound`), the client's half: the
2
+ // loops the snapshot says are playing (`hubSounds`) and the one-shots the place
3
+ // broadcast (`hub-sound`), each from one of the deck's own sound files at a
4
+ // scene actor, louder the nearer this player stands (silent `radius` metres
5
+ // away).
6
+ //
7
+ // Each file from the content the place is drawn from, never another's: a
8
+ // place of this deck plays the deck's own media, bundled into its page
9
+ // (physics-2d `initialMedia`); a place mounted from another deck's content
10
+ // pack plays only that pack's `audio` (content.audio, data: URLs). A town file
11
+ // of the same name is never played for a pack place, nor a pack's for the
12
+ // town: two decks' `sounds/horn.mp3` are different sounds. A pack without
13
+ // `audio` (built before it existed) is silent.
14
+ //
15
+ // Browsers start audio only after a gesture: a loop asked for before one is
16
+ // queued and starts on the player's first tap or key (physics-2d `playMedia`);
17
+ // a one-shot that cannot play now is dropped rather than played late.
18
+
19
+ import { initialMedia } from '@imports/castle.physics-2d/engine/files';
20
+ import { playMedia, stopMedia } from '@imports/castle.physics-2d/engine/media';
21
+ import { hub, simNow } from './state.js';
22
+
23
+ // One-shots sounding at once on this client; more are dropped.
24
+ const MAX_SHOTS = 6;
25
+
26
+ // How loud a sound is `d` metres from its actor: its volume, falling off in a
27
+ // straight line to nothing at `radius`.
28
+ export function gainAt(volume, radius, d) {
29
+ const v = typeof volume === 'number' && Number.isFinite(volume) ? Math.min(1, Math.max(0, volume)) : 1;
30
+ const r = typeof radius === 'number' && radius > 0 ? radius : 1;
31
+ return v * Math.max(0, 1 - d / r);
32
+ }
33
+
34
+ // The URL `file` plays from, or null: only a file the place lists, and only
35
+ // from the content the place is drawn from (`content` is the mounted pack, or
36
+ // null for this deck's own).
37
+ export function soundUrl(file, listed, content, ownMedia = initialMedia) {
38
+ if (!listed.includes(file)) {
39
+ return null;
40
+ }
41
+ const source = content ? (content.audio ?? {}) : ownMedia;
42
+ return Object.hasOwn(source, file) ? source[file] : null;
43
+ }
44
+
45
+ export function makeSound() {
46
+ // actorId -> { key, el, volume, radius }
47
+ const loops = new Map();
48
+ // { el, actor, volume, radius }
49
+ const shots = new Set();
50
+
51
+ function stop(el) {
52
+ try {
53
+ // Also out of the engine's queue of plays waiting for a gesture. A play
54
+ // still pending when it is stopped rejects afterwards and would put the
55
+ // element back in that queue, so it is taken out again once that has
56
+ // had time to happen.
57
+ stopMedia(el);
58
+ el.pause();
59
+ el.removeAttribute('src');
60
+ el.load();
61
+ for (const ms of [0, 250, 2000]) {
62
+ setTimeout(() => stopMedia(el), ms);
63
+ }
64
+ } catch {
65
+ // An element that never loaded has nothing to stop.
66
+ }
67
+ }
68
+
69
+ function stopAll() {
70
+ for (const loop of loops.values()) {
71
+ stop(loop.el);
72
+ }
73
+ loops.clear();
74
+ for (const shot of shots) {
75
+ stop(shot.el);
76
+ }
77
+ shots.clear();
78
+ }
79
+
80
+ function distance(scene, me, actorId) {
81
+ const at = scene.getActor(actorId)?.components.Transform;
82
+ return me && at ? Math.hypot(at.x - me.x, at.y - me.y, at.z - me.z) : Infinity;
83
+ }
84
+
85
+ return {
86
+ // `on`: whether the place may sound now (not during a trip).
87
+ frame(scene, local, on) {
88
+ const files = hub.place?.idle?.sounds ?? [];
89
+ const events = hub.soundEvents.splice(0);
90
+ if (!on || !files.length || typeof Audio === 'undefined') {
91
+ stopAll();
92
+ return;
93
+ }
94
+ const url = (file) => soundUrl(file, files, scene.content);
95
+ const me = local?.components.Transform ?? null;
96
+ for (const [actorId, loop] of [...loops]) {
97
+ const want = hub.sounds[actorId];
98
+ if (!want || `${want.file}|${want.since}` !== loop.key) {
99
+ stop(loop.el);
100
+ loops.delete(actorId);
101
+ }
102
+ }
103
+ for (const [actorId, want] of Object.entries(hub.sounds)) {
104
+ let loop = loops.get(actorId);
105
+ if (!loop) {
106
+ const src = url(want.file);
107
+ if (!src) {
108
+ continue;
109
+ }
110
+ const el = new Audio(src);
111
+ el.loop = true;
112
+ el.volume = 0;
113
+ // Join the loop where everyone else is in it.
114
+ el.addEventListener('loadedmetadata', () => {
115
+ const d = el.duration;
116
+ if (Number.isFinite(d) && d > 0) {
117
+ el.currentTime = (Math.max(0, simNow() - want.since) / 1000) % d;
118
+ }
119
+ });
120
+ loop = { key: `${want.file}|${want.since}`, el, volume: want.volume, radius: want.radius };
121
+ loops.set(actorId, loop);
122
+ playMedia(el);
123
+ }
124
+ loop.el.volume = gainAt(loop.volume, loop.radius, distance(scene, me, actorId));
125
+ }
126
+ for (const event of events) {
127
+ const src = typeof event.file === 'string' ? url(event.file) : null;
128
+ const d = distance(scene, me, event.actor);
129
+ if (!src || shots.size >= MAX_SHOTS || gainAt(event.volume, event.radius, d) <= 0) {
130
+ continue;
131
+ }
132
+ const shot = { el: new Audio(src), actor: event.actor, volume: event.volume, radius: event.radius };
133
+ shot.el.volume = gainAt(shot.volume, shot.radius, d);
134
+ shots.add(shot);
135
+ const done = () => {
136
+ shots.delete(shot);
137
+ };
138
+ shot.el.addEventListener('ended', done);
139
+ Promise.resolve(shot.el.play()).catch(done);
140
+ }
141
+ for (const shot of shots) {
142
+ shot.el.volume = gainAt(shot.volume, shot.radius, distance(scene, me, shot.actor));
143
+ }
144
+ },
145
+ dispose: stopAll,
146
+ };
147
+ }
@@ -0,0 +1,143 @@
1
+ // What the hub client knows about the place it is in, filled by the hook layer
2
+ // (`hookLayer.js`) from the place's server and read by the hub system
3
+ // (`code/systems/hub.js`). One module instance per page; `clear` runs on every
4
+ // session switch.
5
+
6
+ // What the deck's own castle.json asks of the hub client, read once by the
7
+ // hook layer (`hookLayer.js`) as the page loads. Not cleared on a session
8
+ // switch: it is the page's deck, whichever place is playing.
9
+ export const deck = {
10
+ // `hub.tiers`: a cheaper picture on a slow device (`tiers.js`).
11
+ tiers: false,
12
+ };
13
+
14
+ export const hub = {
15
+ // the place's public manifest (`hubPlace`), or null before the first snapshot
16
+ place: null,
17
+ // playerId -> avatar index: everyone's (`hubAv` in the snapshot) with this
18
+ // player's own (`hub-av`: players a block hides from someone here, sent to
19
+ // those who may see them) laid over it
20
+ avatars: new Map(),
21
+ avatarsAll: new Map(),
22
+ avatarsMe: new Map(),
23
+ // playerId -> { kind, color, rarity }: what each player wears on their
24
+ // avatar (`hubWear`, v1.4), with this player's own (`hub-wear`) over it, as
25
+ // the avatars are
26
+ wearing: new Map(),
27
+ wearAll: new Map(),
28
+ wearMe: new Map(),
29
+ // the declared HUD, this player's override of it, and the page time each
30
+ // arrived (a timer is read against the simulation clock below)
31
+ hud: null,
32
+ hudMe: null,
33
+ // the newest simulation time seen and the page time it arrived
34
+ lastT: 0,
35
+ lastTAt: 0,
36
+ // scene actor ids the place hid
37
+ hidden: new Set(),
38
+ // scene actor id -> '#rrggbb' the place painted it
39
+ tints: {},
40
+ // The town's doors a player may jump to (contract §5): [{ id, title }].
41
+ doors: [],
42
+ // playerId -> { x, z, where, door, on }: where the players this client
43
+ // asked about are (`hub-where`), and the page time the answer arrived
44
+ whereAt: {},
45
+ whereAtAt: 0,
46
+ // the player the place last put this one beside (`hub-jumped` with `to`),
47
+ // until the hub system has read it
48
+ jumpedTo: null,
49
+ // This player's party in a town (`hub-party`, v1.10): the other members
50
+ // here (player ids, or null for no party), how many are away through a
51
+ // door, the invitation waiting on this player ({ from, username }) and the
52
+ // door their party proposed ({ door, by, username, asked })
53
+ party: { members: null, away: 0, invite: null, proposal: null },
54
+ // what players said since the hub system last looked (`hub-said`, v1.11):
55
+ // { player, r } (a reaction) or { player, p, door } (a phrase)
56
+ saidEvents: [],
57
+ // who went through which door since the hub system last looked
58
+ // (`hub-went`, v1.15): { players, door }
59
+ wentEvents: [],
60
+ // playerId -> { text, color }: the tag the place put over that player
61
+ marks: {},
62
+ // copy id -> { from, x, y, z, rotationY }: actors the place spawned, and
63
+ // template id -> its authored components
64
+ spawned: {},
65
+ spawnTemplates: {},
66
+ // this player's own controller tuning, { speed, jump } multipliers, or null
67
+ tune: null,
68
+ // player ids the place is holding still
69
+ held: new Set(),
70
+ // actor id -> { text, style }, and this player's own over them (an NPC's
71
+ // reply to this player)
72
+ labels: {},
73
+ labelsMe: {},
74
+ // the facing the place gave this player on arrival, or null until it has,
75
+ // and whether the camera has been turned to it
76
+ arrivedFacing: null,
77
+ faced: false,
78
+ // a command for the hub system: { kind: 'go' | 'end' | 'result', ... }
79
+ commands: [],
80
+ // NPC conversation messages for the talk panel (`talk.js`)
81
+ npcEvents: [],
82
+ // the place's answers about a hold (`grab.js`, v1.3): hub-grab-deny, hub-drop
83
+ grabEvents: [],
84
+ // Idle play (v1.6, `idle.js`, `sound.js`): playerId -> { id, at, until } (an
85
+ // emote playing, simulation ms), playerId -> the seat actor they sit on,
86
+ // actorId -> { file, volume, radius, since } (a loop playing there), and the
87
+ // one-shot sounds not yet played
88
+ emotes: {},
89
+ seated: {},
90
+ sounds: {},
91
+ soundEvents: [],
92
+ // Arcade cabinets (v1.7, `cabinets.js`): door actor id -> { door, card, rev },
93
+ // the cards this player has been sent, and a count of card changes
94
+ cabinets: {},
95
+ cabinetsRev: 0,
96
+ };
97
+
98
+ export function clear() {
99
+ hub.place = null;
100
+ hub.avatars.clear();
101
+ hub.avatarsAll.clear();
102
+ hub.avatarsMe.clear();
103
+ hub.wearing.clear();
104
+ hub.wearAll.clear();
105
+ hub.wearMe.clear();
106
+ hub.hud = null;
107
+ hub.hudMe = null;
108
+ hub.hidden.clear();
109
+ hub.tints = {};
110
+ hub.doors = [];
111
+ hub.whereAt = {};
112
+ hub.whereAtAt = 0;
113
+ hub.jumpedTo = null;
114
+ hub.party = { members: null, away: 0, invite: null, proposal: null };
115
+ hub.marks = {};
116
+ hub.spawned = {};
117
+ hub.spawnTemplates = {};
118
+ hub.tune = null;
119
+ hub.held.clear();
120
+ hub.labels = {};
121
+ hub.labelsMe = {};
122
+ hub.npcEvents.length = 0;
123
+ hub.saidEvents.length = 0;
124
+ hub.wentEvents.length = 0;
125
+ hub.grabEvents.length = 0;
126
+ hub.emotes = {};
127
+ hub.seated = {};
128
+ hub.sounds = {};
129
+ hub.soundEvents.length = 0;
130
+ hub.cabinets = {};
131
+ hub.arrivedFacing = null;
132
+ hub.faced = false;
133
+ hub.commands.length = 0;
134
+ }
135
+
136
+ export function avatarOf(playerId) {
137
+ return hub.avatars.has(playerId) ? hub.avatars.get(playerId) : null;
138
+ }
139
+
140
+ // The simulation time now, extrapolated from the newest snapshot.
141
+ export function simNow() {
142
+ return hub.lastT + (performance.now() - hub.lastTAt);
143
+ }
@@ -0,0 +1,233 @@
1
+ // Talking to a place's NPCs (contract §13), drawn by the hub chrome because a
2
+ // Tier 0 game has no client code of its own. Near a `HubNpc` actor the player
3
+ // gets a Talk button; the place's server decides whether the conversation
4
+ // opens (one player per NPC at a time) and whether it goes to the character's
5
+ // mind or to the place's authored lines.
6
+ //
7
+ // With a mind, the player's words are a character turn on the world's socket
8
+ // (`requestCharacterTurn`) that names no listener: the server admits it or
9
+ // refuses it, and the reply is read back with a status read, which is also
10
+ // when the server shows it over the NPC to this player alone. What a player
11
+ // says to a character and what it answers is theirs: nobody else is sent
12
+ // either. Without one, the words are a plain
13
+ // message and the place answers. The place's notes for the character (what it
14
+ // knows right now: a ledger, a pad) come from the server as a prefix sent
15
+ // ahead of the player's words; the server admits only notes it issued, and the
16
+ // player sees only their own words.
17
+
18
+ import { hub } from './state.js';
19
+
20
+ const TALK_RANGE_M = 3.5;
21
+ const POLL_MS = 1200;
22
+ const POLL_LIMIT_MS = 90_000;
23
+ const INPUT_MAX = 280;
24
+ // A turn refused for stale notes is sent again once, with the newest.
25
+ const NOTES_RETRY_MS = 400;
26
+ // Lines the place said because the mind was not answering, and how the panel
27
+ // says so.
28
+ const SCRIPTED = {
29
+ busy: 'is busy with someone else',
30
+ budget: 'has a crowd to answer',
31
+ unavailable: "can't answer right now",
32
+ };
33
+
34
+ // The server strips a reply's wrapping quotes the same way for its bubble.
35
+ const unquote = (text) => {
36
+ const t = text.trim();
37
+ return t.length > 1 && /^["“]/.test(t) && /["”]$/.test(t) ? t.slice(1, -1).trim() : t;
38
+ };
39
+
40
+ const newId = () => globalThis.crypto?.randomUUID?.() ?? `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 12)}`;
41
+
42
+ export function makeTalk(chrome) {
43
+ const t = {
44
+ chrome,
45
+ // { npc, name, mind, character, prefix } while a panel is open
46
+ open: null,
47
+ asking: null,
48
+ net: null,
49
+ // the nets whose participant this player has registered with an inspect read
50
+ inspected: new WeakSet(),
51
+ waiting: false,
52
+ };
53
+ chrome.onTalk({
54
+ open: () => ask(t),
55
+ send: (text) => send(t, text),
56
+ end: () => end(t),
57
+ });
58
+ return {
59
+ frame: (scene, mp, idle) => frame(t, scene, mp, idle),
60
+ typing: () => t.open !== null && chrome.talkFocused(),
61
+ talking: () => t.open !== null,
62
+ // The NPC this player is talking with: its words are in the panel.
63
+ talkingWith: () => t.open?.npc ?? null,
64
+ };
65
+ }
66
+
67
+ function frame(t, scene, mp, idle) {
68
+ const net = mp.net;
69
+ if (net !== t.net) {
70
+ // A session switch ends whatever was being said in the old one.
71
+ t.net = net;
72
+ t.open = null;
73
+ t.asking = null;
74
+ t.waiting = false;
75
+ t.chrome.closeTalk();
76
+ }
77
+ for (const msg of hub.npcEvents.splice(0)) {
78
+ if (msg.k === 'hub-npc') {
79
+ if (msg.open) {
80
+ t.open = { npc: msg.npc, name: msg.name, mind: Boolean(msg.mind), character: msg.character ?? null, prefix: null };
81
+ t.chrome.openTalk(msg.name);
82
+ if (t.open.mind) {
83
+ registerParticipant(t, net);
84
+ }
85
+ } else if (!t.open || t.open.npc === msg.npc) {
86
+ t.open = null;
87
+ t.waiting = false;
88
+ t.chrome.closeTalk();
89
+ }
90
+ if (t.asking === msg.npc) {
91
+ t.asking = null;
92
+ }
93
+ } else if (msg.k === 'hub-npc-line' && t.open?.npc === msg.npc) {
94
+ // With a mind, a line the place said instead is marked as scripted.
95
+ const scripted = t.open.mind ? SCRIPTED[msg.why] : null;
96
+ t.chrome.talkLine('them', msg.text, scripted ? `Scripted reply: ${t.open.name} ${scripted}` : null);
97
+ t.waiting = false;
98
+ } else if (msg.k === 'hub-npc-notes' && t.open?.npc === msg.npc) {
99
+ t.open.prefix = typeof msg.prefix === 'string' ? msg.prefix : null;
100
+ }
101
+ }
102
+ t.chrome.talkWaiting(t.waiting);
103
+
104
+ const near = idle && net && !t.open ? nearestNpc(scene) : null;
105
+ t.near = near;
106
+ t.chrome.talkPrompt(near ? near.name : null);
107
+ }
108
+
109
+ function nearestNpc(scene) {
110
+ const local = scene.actorWith('Player')?.components.Transform;
111
+ if (!local) {
112
+ return null;
113
+ }
114
+ let best = null;
115
+ for (const actor of scene.getActors()) {
116
+ const props = actor.components?.HubNpc;
117
+ const at = actor.components?.Transform;
118
+ if (!props || !at) {
119
+ continue;
120
+ }
121
+ const d = Math.hypot(at.x - local.x, at.z - local.z);
122
+ const range = typeof props.talkRange === 'number' ? props.talkRange : TALK_RANGE_M;
123
+ if (d <= range && (!best || d < best.d)) {
124
+ best = { id: actor.id, name: typeof props.name === 'string' && props.name ? props.name : actor.id, d };
125
+ }
126
+ }
127
+ return best;
128
+ }
129
+
130
+ function ask(t) {
131
+ if (!t.near || !t.net || t.asking) {
132
+ return;
133
+ }
134
+ t.asking = t.near.id;
135
+ t.net.send({ k: 'hub-npc-open', npc: t.near.id });
136
+ }
137
+
138
+ function end(t) {
139
+ if (t.open && t.net) {
140
+ t.net.send({ k: 'hub-npc-end', npc: t.open.npc });
141
+ }
142
+ t.open = null;
143
+ t.waiting = false;
144
+ t.chrome.closeTalk();
145
+ }
146
+
147
+ // The world learns which player here is which participant from that player's
148
+ // own read; the place needs it to tell who a character chose to follow.
149
+ function registerParticipant(t, net) {
150
+ const characters = net?.characters;
151
+ if (!characters || t.inspected.has(net)) {
152
+ return;
153
+ }
154
+ t.inspected.add(net);
155
+ characters.readCharacters({ requestId: newId(), query: { type: 'inspect' } }).catch((err) => {
156
+ console.log(`[hub] NPC inspect failed: ${err?.code ?? err?.message ?? err}`);
157
+ });
158
+ }
159
+
160
+ function send(t, raw) {
161
+ const text = String(raw ?? '').trim().slice(0, INPUT_MAX);
162
+ const open = t.open;
163
+ if (!text || !open || t.waiting || !t.net) {
164
+ return;
165
+ }
166
+ t.chrome.talkLine('me', text);
167
+ if (!open.mind) {
168
+ t.net.send({ k: 'hub-npc-say', npc: open.npc, text });
169
+ return;
170
+ }
171
+ t.waiting = true;
172
+ void turn(t, open, text);
173
+ }
174
+
175
+ async function turn(t, open, text) {
176
+ const net = t.net;
177
+ const characters = net?.characters;
178
+ let operationId = null;
179
+ const request = () => {
180
+ operationId = newId();
181
+ return characters.requestCharacterTurn({
182
+ requestId: newId(),
183
+ operationId,
184
+ characterId: open.character,
185
+ input: `${open.prefix ?? ''}${text}`,
186
+ // Nobody else hears a player's conversation with a character.
187
+ audience: { disclosure: 'participants', playerIds: [], characterIds: [] },
188
+ });
189
+ };
190
+ try {
191
+ try {
192
+ await request();
193
+ } catch (err) {
194
+ // The notes changed under the words (a pad filled): once more, with the
195
+ // newest the server sent.
196
+ if (err?.code !== 'HUB_NPC_NOTES' || t.open !== open) {
197
+ throw err;
198
+ }
199
+ await new Promise((resolve) => setTimeout(resolve, NOTES_RETRY_MS));
200
+ await request();
201
+ }
202
+ } catch (err) {
203
+ // A refusal (busy, over budget, too far) comes with the place's own line,
204
+ // which ends the wait; this only covers a refusal that brought none.
205
+ console.log(`[hub] NPC turn refused: ${err?.code ?? err?.message ?? err}`);
206
+ setTimeout(() => {
207
+ if (t.open === open) {
208
+ t.waiting = false;
209
+ }
210
+ }, 1500);
211
+ return;
212
+ }
213
+ const started = performance.now();
214
+ while (t.open === open && t.net === net && performance.now() - started < POLL_LIMIT_MS) {
215
+ await new Promise((resolve) => setTimeout(resolve, POLL_MS));
216
+ let status = null;
217
+ try {
218
+ status = await characters.readCharacters({ requestId: newId(), query: { type: 'status', operationId } });
219
+ } catch (err) {
220
+ console.log(`[hub] NPC status read failed: ${err?.code ?? err?.message ?? err}`);
221
+ continue;
222
+ }
223
+ if (status?.status === 'completed' || status?.status === 'interrupted') {
224
+ if (t.open === open && typeof status.response === 'string' && status.response) {
225
+ t.chrome.talkLine('them', unquote(status.response));
226
+ }
227
+ break;
228
+ }
229
+ }
230
+ if (t.open === open) {
231
+ t.waiting = false;
232
+ }
233
+ }