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,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,131 @@
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
+ export const hub = {
7
+ // the place's public manifest (`hubPlace`), or null before the first snapshot
8
+ place: null,
9
+ // playerId -> avatar index: everyone's (`hubAv` in the snapshot) with this
10
+ // player's own (`hub-av`: players a block hides from someone here, sent to
11
+ // those who may see them) laid over it
12
+ avatars: new Map(),
13
+ avatarsAll: new Map(),
14
+ avatarsMe: new Map(),
15
+ // playerId -> { kind, color, rarity }: what each player wears on their
16
+ // avatar (`hubWear`, v1.4), with this player's own (`hub-wear`) over it, as
17
+ // the avatars are
18
+ wearing: new Map(),
19
+ wearAll: new Map(),
20
+ wearMe: new Map(),
21
+ // the declared HUD, this player's override of it, and the page time each
22
+ // arrived (a timer is read against the simulation clock below)
23
+ hud: null,
24
+ hudMe: null,
25
+ // the newest simulation time seen and the page time it arrived
26
+ lastT: 0,
27
+ lastTAt: 0,
28
+ // scene actor ids the place hid
29
+ hidden: new Set(),
30
+ // scene actor id -> '#rrggbb' the place painted it
31
+ tints: {},
32
+ // The town's doors a player may jump to (contract §5): [{ id, title }].
33
+ doors: [],
34
+ // playerId -> { x, z, where, door, on }: where the players this client
35
+ // asked about are (`hub-where`), and the page time the answer arrived
36
+ whereAt: {},
37
+ whereAtAt: 0,
38
+ // the player the place last put this one beside (`hub-jumped` with `to`),
39
+ // until the hub system has read it
40
+ jumpedTo: null,
41
+ // This player's party in a town (`hub-party`, v1.10): the other members
42
+ // here (player ids, or null for no party), how many are away through a
43
+ // door, the invitation waiting on this player ({ from, username }) and the
44
+ // door their party proposed ({ door, by, username, asked })
45
+ party: { members: null, away: 0, invite: null, proposal: null },
46
+ // what players said since the hub system last looked (`hub-said`, v1.11):
47
+ // { player, r } (a reaction) or { player, p, door } (a phrase)
48
+ saidEvents: [],
49
+ // playerId -> { text, color }: the tag the place put over that player
50
+ marks: {},
51
+ // copy id -> { from, x, y, z, rotationY }: actors the place spawned, and
52
+ // template id -> its authored components
53
+ spawned: {},
54
+ spawnTemplates: {},
55
+ // this player's own controller tuning, { speed, jump } multipliers, or null
56
+ tune: null,
57
+ // player ids the place is holding still
58
+ held: new Set(),
59
+ // actor id -> { text, style }, and this player's own over them (an NPC's
60
+ // reply to the audience it was admitted for)
61
+ labels: {},
62
+ labelsMe: {},
63
+ // the facing the place gave this player on arrival, or null until it has,
64
+ // and whether the camera has been turned to it
65
+ arrivedFacing: null,
66
+ faced: false,
67
+ // a command for the hub system: { kind: 'go' | 'end' | 'result', ... }
68
+ commands: [],
69
+ // NPC conversation messages for the talk panel (`talk.js`)
70
+ npcEvents: [],
71
+ // the place's answers about a hold (`grab.js`, v1.3): hub-grab-deny, hub-drop
72
+ grabEvents: [],
73
+ // Idle play (v1.6, `idle.js`, `sound.js`): playerId -> { id, at, until } (an
74
+ // emote playing, simulation ms), playerId -> the seat actor they sit on,
75
+ // actorId -> { file, volume, radius, since } (a loop playing there), and the
76
+ // one-shot sounds not yet played
77
+ emotes: {},
78
+ seated: {},
79
+ sounds: {},
80
+ soundEvents: [],
81
+ // Arcade cabinets (v1.7, `cabinets.js`): door actor id -> { door, card, rev },
82
+ // the cards this player has been sent, and a count of card changes
83
+ cabinets: {},
84
+ cabinetsRev: 0,
85
+ };
86
+
87
+ export function clear() {
88
+ hub.place = null;
89
+ hub.avatars.clear();
90
+ hub.avatarsAll.clear();
91
+ hub.avatarsMe.clear();
92
+ hub.wearing.clear();
93
+ hub.wearAll.clear();
94
+ hub.wearMe.clear();
95
+ hub.hud = null;
96
+ hub.hudMe = null;
97
+ hub.hidden.clear();
98
+ hub.tints = {};
99
+ hub.doors = [];
100
+ hub.whereAt = {};
101
+ hub.whereAtAt = 0;
102
+ hub.jumpedTo = null;
103
+ hub.party = { members: null, away: 0, invite: null, proposal: null };
104
+ hub.marks = {};
105
+ hub.spawned = {};
106
+ hub.spawnTemplates = {};
107
+ hub.tune = null;
108
+ hub.held.clear();
109
+ hub.labels = {};
110
+ hub.labelsMe = {};
111
+ hub.npcEvents.length = 0;
112
+ hub.saidEvents.length = 0;
113
+ hub.grabEvents.length = 0;
114
+ hub.emotes = {};
115
+ hub.seated = {};
116
+ hub.sounds = {};
117
+ hub.soundEvents.length = 0;
118
+ hub.cabinets = {};
119
+ hub.arrivedFacing = null;
120
+ hub.faced = false;
121
+ hub.commands.length = 0;
122
+ }
123
+
124
+ export function avatarOf(playerId) {
125
+ return hub.avatars.has(playerId) ? hub.avatars.get(playerId) : null;
126
+ }
127
+
128
+ // The simulation time now, extrapolated from the newest snapshot.
129
+ export function simNow() {
130
+ return hub.lastT + (performance.now() - hub.lastTAt);
131
+ }
@@ -0,0 +1,259 @@
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`): the server admits it or refuses it, and the reply
9
+ // is read back with a status read, which is also when the server shows it
10
+ // over the NPC to the turn's audience. Without one, the words are a plain
11
+ // message and the place answers. The place's notes for the character (what it
12
+ // knows right now: a ledger, a pad) come from the server as a prefix sent
13
+ // ahead of the player's words; the server admits only notes it issued, and the
14
+ // player sees only their own words.
15
+
16
+ import { hub } from './state.js';
17
+
18
+ const TALK_RANGE_M = 3.5;
19
+ const POLL_MS = 1200;
20
+ const POLL_LIMIT_MS = 90_000;
21
+ const INPUT_MAX = 280;
22
+ const LISTENERS_MAX = 7;
23
+ // A turn refused for stale notes is sent again once, with the newest.
24
+ const NOTES_RETRY_MS = 400;
25
+ // Lines the place said because the mind was not answering, and how the panel
26
+ // says so.
27
+ const SCRIPTED = {
28
+ busy: 'is busy with someone else',
29
+ budget: 'has a crowd to answer',
30
+ unavailable: "can't answer right now",
31
+ };
32
+
33
+ // The server strips a reply's wrapping quotes the same way for its bubble.
34
+ const unquote = (text) => {
35
+ const t = text.trim();
36
+ return t.length > 1 && /^["“]/.test(t) && /["”]$/.test(t) ? t.slice(1, -1).trim() : t;
37
+ };
38
+
39
+ const newId = () => globalThis.crypto?.randomUUID?.() ?? `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 12)}`;
40
+
41
+ export function makeTalk(chrome) {
42
+ const t = {
43
+ chrome,
44
+ // { npc, name, mind, character, disclosure, hearRange } while a panel is open
45
+ open: null,
46
+ asking: null,
47
+ net: null,
48
+ // the nets whose participant this player has registered with an inspect read
49
+ inspected: new WeakSet(),
50
+ waiting: false,
51
+ };
52
+ chrome.onTalk({
53
+ open: () => ask(t),
54
+ send: (text) => send(t, text),
55
+ end: () => end(t),
56
+ });
57
+ return {
58
+ frame: (scene, mp, idle) => frame(t, scene, mp, idle),
59
+ typing: () => t.open !== null && chrome.talkFocused(),
60
+ talking: () => t.open !== null,
61
+ // The NPC this player is talking with: its words are in the panel.
62
+ talkingWith: () => t.open?.npc ?? null,
63
+ };
64
+ }
65
+
66
+ function frame(t, scene, mp, idle) {
67
+ const net = mp.net;
68
+ if (net !== t.net) {
69
+ // A session switch ends whatever was being said in the old one.
70
+ t.net = net;
71
+ t.open = null;
72
+ t.asking = null;
73
+ t.waiting = false;
74
+ t.chrome.closeTalk();
75
+ }
76
+ for (const msg of hub.npcEvents.splice(0)) {
77
+ if (msg.k === 'hub-npc') {
78
+ if (msg.open) {
79
+ t.open = { npc: msg.npc, name: msg.name, mind: Boolean(msg.mind), character: msg.character ?? null, disclosure: msg.disclosure, hearRange: msg.hearRange ?? 6, prefix: null };
80
+ t.chrome.openTalk(msg.name, msg.disclosure === 'world' ? 'Everyone nearby hears this' : 'Players nearby can hear');
81
+ if (t.open.mind) {
82
+ registerParticipant(t, net);
83
+ }
84
+ } else if (!t.open || t.open.npc === msg.npc) {
85
+ t.open = null;
86
+ t.waiting = false;
87
+ t.chrome.closeTalk();
88
+ }
89
+ if (t.asking === msg.npc) {
90
+ t.asking = null;
91
+ }
92
+ } else if (msg.k === 'hub-npc-line' && t.open?.npc === msg.npc) {
93
+ // With a mind, a line the place said instead is marked as scripted.
94
+ const scripted = t.open.mind ? SCRIPTED[msg.why] : null;
95
+ t.chrome.talkLine('them', msg.text, scripted ? `Scripted reply: ${t.open.name} ${scripted}` : null);
96
+ t.waiting = false;
97
+ } else if (msg.k === 'hub-npc-notes' && t.open?.npc === msg.npc) {
98
+ t.open.prefix = typeof msg.prefix === 'string' ? msg.prefix : null;
99
+ }
100
+ }
101
+ t.chrome.talkWaiting(t.waiting);
102
+
103
+ const near = idle && net && !t.open ? nearestNpc(scene) : null;
104
+ t.near = near;
105
+ t.chrome.talkPrompt(near ? near.name : null);
106
+ t.scene = scene;
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
+ audience: { disclosure: open.disclosure, playerIds: listeners(t, open), characterIds: [] },
187
+ });
188
+ };
189
+ try {
190
+ try {
191
+ await request();
192
+ } catch (err) {
193
+ // The notes changed under the words (a pad filled): once more, with the
194
+ // newest the server sent.
195
+ if (err?.code !== 'HUB_NPC_NOTES' || t.open !== open) {
196
+ throw err;
197
+ }
198
+ await new Promise((resolve) => setTimeout(resolve, NOTES_RETRY_MS));
199
+ await request();
200
+ }
201
+ } catch (err) {
202
+ // A refusal (busy, over budget, too far) comes with the place's own line,
203
+ // which ends the wait; this only covers a refusal that brought none.
204
+ console.log(`[hub] NPC turn refused: ${err?.code ?? err?.message ?? err}`);
205
+ setTimeout(() => {
206
+ if (t.open === open) {
207
+ t.waiting = false;
208
+ }
209
+ }, 1500);
210
+ return;
211
+ }
212
+ const started = performance.now();
213
+ while (t.open === open && t.net === net && performance.now() - started < POLL_LIMIT_MS) {
214
+ await new Promise((resolve) => setTimeout(resolve, POLL_MS));
215
+ let status = null;
216
+ try {
217
+ status = await characters.readCharacters({ requestId: newId(), query: { type: 'status', operationId } });
218
+ } catch (err) {
219
+ console.log(`[hub] NPC status read failed: ${err?.code ?? err?.message ?? err}`);
220
+ continue;
221
+ }
222
+ if (status?.status === 'completed' || status?.status === 'interrupted') {
223
+ if (t.open === open && typeof status.response === 'string' && status.response) {
224
+ t.chrome.talkLine('them', unquote(status.response));
225
+ }
226
+ break;
227
+ }
228
+ }
229
+ if (t.open === open) {
230
+ t.waiting = false;
231
+ }
232
+ }
233
+
234
+ // Who else hears this turn: the players drawn near the NPC now, at most seven.
235
+ // The server checks the same distance against its own poses.
236
+ function listeners(t, open) {
237
+ const scene = t.scene;
238
+ const mp = scene?.multiplayer;
239
+ const npc = scene?.getActor(open.npc)?.components.Transform;
240
+ if (!npc || !mp?.avatars?.actors) {
241
+ return [];
242
+ }
243
+ const near = [];
244
+ for (const [playerId, actorId] of mp.avatars.actors) {
245
+ const at = scene.getActor(actorId)?.components.Transform;
246
+ if (at) {
247
+ const d = Math.hypot(at.x - npc.x, at.z - npc.z);
248
+ // A little inside the server's range, so a player at its edge is not
249
+ // the reason a turn is refused.
250
+ if (d <= open.hearRange - 0.5) {
251
+ near.push({ playerId, d });
252
+ }
253
+ }
254
+ }
255
+ return near
256
+ .sort((a, b) => a.d - b.d)
257
+ .slice(0, LISTENERS_MAX)
258
+ .map((p) => p.playerId);
259
+ }