castle-web-cli 0.4.232 → 0.4.233

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 (355) hide show
  1. package/dist/agent-failures.d.ts +8 -0
  2. package/dist/agent-failures.js +19 -2
  3. package/dist/agent-prompts.d.ts +1 -1
  4. package/dist/agent-prompts.js +10 -3
  5. package/dist/agent.d.ts +64 -3
  6. package/dist/agent.js +170 -81
  7. package/dist/castle-host/host.d.ts +1 -0
  8. package/dist/castle-host/host.js +51 -3
  9. package/dist/castleJson.d.ts +4 -2
  10. package/dist/castleJson.js +2 -0
  11. package/dist/check.d.ts +3 -1
  12. package/dist/check.js +11 -1
  13. package/dist/devSessionStorage.js +47 -1
  14. package/dist/importPinMismatch.d.ts +22 -0
  15. package/dist/importPinMismatch.js +77 -0
  16. package/dist/importRegression.js +3 -15
  17. package/dist/imports.d.ts +10 -3
  18. package/dist/imports.js +88 -20
  19. package/dist/index.js +5 -4
  20. package/dist/init.js +1 -1
  21. package/dist/platformDoc.js +22 -0
  22. package/dist/routerFences.d.ts +5 -0
  23. package/dist/routerFences.js +12 -2
  24. package/dist/save-deck.d.ts +5 -0
  25. package/dist/save-deck.js +37 -4
  26. package/dist/serve.js +3 -0
  27. package/dist/shell/assets/{index-D2tWM896.js → index-BjOxR-lW.js} +4 -4
  28. package/dist/shell/assets/index-MVLfNYd9.css +1 -0
  29. package/dist/shell/index.html +2 -2
  30. package/dist/taskSteer.d.ts +13 -0
  31. package/dist/taskSteer.js +22 -2
  32. package/kits/base/castle.json +1 -1
  33. package/kits/base/package-lock.json +1 -1
  34. package/kits/base/sdk/README.md +79 -2
  35. package/kits/base/sdk/avatar.d.ts +6 -2
  36. package/kits/base/sdk/avatar.js +12 -4
  37. package/kits/base/sdk/castle.d.ts +1 -1
  38. package/kits/base/sdk/commands.d.ts +14 -0
  39. package/kits/base/sdk/hub.d.ts +6 -2
  40. package/kits/base/sdk/hub.js +13 -0
  41. package/kits/base/sdk/multiplayer.js +17 -1
  42. package/kits/base/sdk/save.js +2 -1
  43. package/kits/base/sdk/server/hub.d.ts +87 -0
  44. package/kits/base/sdk/server/hub.js +70 -0
  45. package/kits/base/sdk/server/platformHandle.d.ts +31 -1
  46. package/kits/base/sdk/server/storage.d.ts +13 -0
  47. package/kits/base/sdk/server/storage.js +46 -0
  48. package/kits/base/sdk/server/wrapper.d.ts +36 -5
  49. package/kits/base/sdk/server/wrapper.js +83 -8
  50. package/kits/hub-game/CLAUDE.md +329 -11
  51. package/kits/hub-game/blueprints/avatar-10.scene +28 -0
  52. package/kits/hub-game/blueprints/avatar-11.scene +28 -0
  53. package/kits/hub-game/blueprints/avatar-12.scene +28 -0
  54. package/kits/hub-game/blueprints/avatar-13.scene +28 -0
  55. package/kits/hub-game/blueprints/avatar-14.scene +28 -0
  56. package/kits/hub-game/blueprints/avatar-15.scene +28 -0
  57. package/kits/hub-game/blueprints/avatar-4.scene +28 -0
  58. package/kits/hub-game/blueprints/avatar-5.scene +28 -0
  59. package/kits/hub-game/blueprints/avatar-6.scene +28 -0
  60. package/kits/hub-game/blueprints/avatar-7.scene +28 -0
  61. package/kits/hub-game/blueprints/avatar-8.scene +28 -0
  62. package/kits/hub-game/blueprints/avatar-9.scene +28 -0
  63. package/kits/hub-game/code/client/actorBudget.js +47 -0
  64. package/kits/hub-game/code/client/attention.js +11 -2
  65. package/kits/hub-game/code/client/avatar.js +309 -35
  66. package/kits/hub-game/code/client/avatarCap.js +228 -0
  67. package/kits/hub-game/code/client/avatarColors.js +70 -0
  68. package/kits/hub-game/code/client/avatarFaces.js +21 -0
  69. package/kits/hub-game/code/client/avatarShare.js +251 -0
  70. package/kits/hub-game/code/client/ballot.js +467 -0
  71. package/kits/hub-game/code/client/build.js +22 -0
  72. package/kits/hub-game/code/client/chrome.js +1144 -50
  73. package/kits/hub-game/code/client/companion.js +716 -0
  74. package/kits/hub-game/code/client/companionDress.js +99 -0
  75. package/kits/hub-game/code/client/decor.js +509 -0
  76. package/kits/hub-game/code/client/earned.js +131 -0
  77. package/kits/hub-game/code/client/emoteBubble.js +75 -0
  78. package/kits/hub-game/code/client/grab.js +339 -20
  79. package/kits/hub-game/code/client/hookLayer.js +299 -13
  80. package/kits/hub-game/code/client/idle.js +163 -22
  81. package/kits/hub-game/code/client/interestCap.js +81 -0
  82. package/kits/hub-game/code/client/keys.js +36 -0
  83. package/kits/hub-game/code/client/labelsNear.js +205 -0
  84. package/kits/hub-game/code/client/menuIcons.js +34 -0
  85. package/kits/hub-game/code/client/pack.js +6 -2
  86. package/kits/hub-game/code/client/packFormat.js +3 -3
  87. package/kits/hub-game/code/client/rehello.js +167 -0
  88. package/kits/hub-game/code/client/shot.js +92 -0
  89. package/kits/hub-game/code/client/sound.js +63 -8
  90. package/kits/hub-game/code/client/soundscape.js +1151 -0
  91. package/kits/hub-game/code/client/soundscapeActions.js +99 -0
  92. package/kits/hub-game/code/client/soundscapeAmbience.js +453 -0
  93. package/kits/hub-game/code/client/soundscapeBaked.js +143 -0
  94. package/kits/hub-game/code/client/soundscapeBusker.js +217 -0
  95. package/kits/hub-game/code/client/soundscapeCourt.js +200 -0
  96. package/kits/hub-game/code/client/soundscapeCrowd.js +331 -0
  97. package/kits/hub-game/code/client/soundscapeCues.js +362 -0
  98. package/kits/hub-game/code/client/soundscapeMap.js +181 -0
  99. package/kits/hub-game/code/client/soundscapeMarket.js +176 -0
  100. package/kits/hub-game/code/client/soundscapeMusic.js +126 -0
  101. package/kits/hub-game/code/client/soundscapeMusicEngine.js +460 -0
  102. package/kits/hub-game/code/client/soundscapeSimlish.js +737 -0
  103. package/kits/hub-game/code/client/soundscapeSongs.js +127 -0
  104. package/kits/hub-game/code/client/soundscapeSongsRound3.js +63 -0
  105. package/kits/hub-game/code/client/soundscapeSynthesis.js +168 -0
  106. package/kits/hub-game/code/client/soundscapeUi.js +211 -0
  107. package/kits/hub-game/code/client/soundscapeVoice.js +1209 -0
  108. package/kits/hub-game/code/client/soundscapeWaves.js +206 -0
  109. package/kits/hub-game/code/client/spin.js +101 -0
  110. package/kits/hub-game/code/client/state.js +109 -1
  111. package/kits/hub-game/code/client/stuck.js +259 -0
  112. package/kits/hub-game/code/client/talk.js +538 -47
  113. package/kits/hub-game/code/client/tiers.js +516 -27
  114. package/kits/hub-game/code/client/toastBand.js +37 -0
  115. package/kits/hub-game/code/client/together.js +32 -0
  116. package/kits/hub-game/code/client/visibility.js +43 -0
  117. package/kits/hub-game/code/client/wear.js +461 -170
  118. package/kits/hub-game/code/client/yours.js +199 -0
  119. package/kits/hub-game/code/companionSpec.js +45 -0
  120. package/kits/hub-game/code/privateText.js +7 -0
  121. package/kits/hub-game/code/server/actions.js +133 -0
  122. package/kits/hub-game/code/server/actors.js +9 -1
  123. package/kits/hub-game/code/server/ask.js +155 -0
  124. package/kits/hub-game/code/server/ballot.js +539 -0
  125. package/kits/hub-game/code/server/cells.js +100 -0
  126. package/kits/hub-game/code/server/companion.js +82 -0
  127. package/kits/hub-game/code/server/cues.js +70 -0
  128. package/kits/hub-game/code/server/decor.js +269 -0
  129. package/kits/hub-game/code/server/earned.js +461 -0
  130. package/kits/hub-game/code/server/friendBests.js +166 -0
  131. package/kits/hub-game/code/server/friendsAmong.js +232 -0
  132. package/kits/hub-game/code/server/history.js +5 -0
  133. package/kits/hub-game/code/server/hub.js +1254 -50
  134. package/kits/hub-game/code/server/idle.js +30 -11
  135. package/kits/hub-game/code/server/index.js +2 -2
  136. package/kits/hub-game/code/server/labels.js +28 -2
  137. package/kits/hub-game/code/server/manifest.js +378 -7
  138. package/kits/hub-game/code/server/menu.js +133 -0
  139. package/kits/hub-game/code/server/npcs.js +830 -80
  140. package/kits/hub-game/code/server/party.js +2 -2
  141. package/kits/hub-game/code/server/players.js +22 -0
  142. package/kits/hub-game/code/server/shot.js +110 -0
  143. package/kits/hub-game/code/server/standing.js +124 -0
  144. package/kits/hub-game/code/server/vis.js +113 -0
  145. package/kits/hub-game/code/server/wear.js +12 -0
  146. package/kits/hub-game/code/server/yours.js +59 -0
  147. package/kits/hub-game/code/shared/cues.js +9 -0
  148. package/kits/hub-game/code/shared/decor.js +41 -0
  149. package/kits/hub-game/code/shared/shot.js +19 -0
  150. package/kits/hub-game/code/shared/spin.js +57 -0
  151. package/kits/hub-game/code/systems/hub.js +845 -95
  152. package/kits/hub-game/docs/content-pack.md +1 -1
  153. package/kits/hub-game/docs/contract.md +2244 -61
  154. package/kits/hub-game/docs/home-pieces.md +64 -0
  155. package/kits/hub-game/models/avatar-10.pxmodel +28 -0
  156. package/kits/hub-game/models/avatar-11.pxmodel +28 -0
  157. package/kits/hub-game/models/avatar-12.pxmodel +27 -0
  158. package/kits/hub-game/models/avatar-13.pxmodel +24 -0
  159. package/kits/hub-game/models/avatar-14.pxmodel +29 -0
  160. package/kits/hub-game/models/avatar-15.pxmodel +24 -0
  161. package/kits/hub-game/models/avatar-4.pxmodel +29 -0
  162. package/kits/hub-game/models/avatar-5.pxmodel +27 -0
  163. package/kits/hub-game/models/avatar-6.pxmodel +26 -0
  164. package/kits/hub-game/models/avatar-7.pxmodel +25 -0
  165. package/kits/hub-game/models/avatar-8.pxmodel +24 -0
  166. package/kits/hub-game/models/avatar-9.pxmodel +30 -0
  167. package/kits/hub-game/models/earned/badge-round.pxmodel +100 -0
  168. package/kits/hub-game/models/earned/badge-shield.pxmodel +117 -0
  169. package/kits/hub-game/models/earned/badge-star.pxmodel +87 -0
  170. package/kits/hub-game/models/earned/belt-plain.pxmodel +72 -0
  171. package/kits/hub-game/models/earned/held-ball.pxmodel +43 -0
  172. package/kits/hub-game/models/earned/held-lantern.pxmodel +85 -0
  173. package/kits/hub-game/models/earned/held-net.pxmodel +57 -0
  174. package/kits/hub-game/models/earned/held-trophy.pxmodel +109 -0
  175. package/kits/hub-game/models/earned/pin-round.pxmodel +62 -0
  176. package/kits/hub-game/models/earned/sash-plain.pxmodel +66 -0
  177. package/kits/hub-game/models/earned/square/belt-plain.pxmodel +104 -0
  178. package/kits/hub-game/models/earned/square/sash-plain.pxmodel +98 -0
  179. package/kits/hub-game/models/hats/bat-clip.pxmodel +206 -0
  180. package/kits/hub-game/models/hats/beekeeper.pxmodel +149 -0
  181. package/kits/hub-game/models/hats/cap.pxmodel +162 -0
  182. package/kits/hub-game/models/hats/captain.pxmodel +178 -0
  183. package/kits/hub-game/models/hats/crooked-top.pxmodel +172 -0
  184. package/kits/hub-game/models/hats/flower-crown.pxmodel +451 -0
  185. package/kits/hub-game/models/hats/foam-crown.pxmodel +179 -0
  186. package/kits/hub-game/models/hats/lantern.pxmodel +230 -0
  187. package/kits/hub-game/models/hats/laurel.pxmodel +533 -0
  188. package/kits/hub-game/models/hats/lighthouse.pxmodel +96 -0
  189. package/kits/hub-game/models/hats/sailor.pxmodel +141 -0
  190. package/kits/hub-game/models/hats/souwester.pxmodel +131 -0
  191. package/kits/hub-game/models/hats/straw.pxmodel +139 -0
  192. package/kits/hub-game/models/hats/sunflower.pxmodel +406 -0
  193. package/kits/hub-game/models/hats/sweatband.pxmodel +112 -0
  194. package/kits/hub-game/models/hats/witch.pxmodel +147 -0
  195. package/kits/hub-game/package-lock.json +2 -0
  196. package/kits/hub-game/package.json +2 -1
  197. package/kits/hub-game/scripts/check.mjs +78 -11
  198. package/kits/hub-game/scripts/companion-follower-wire.json +1 -0
  199. package/kits/hub-game/scripts/fixtures/decor-keyless-draw.json +121 -0
  200. package/kits/hub-game/scripts/fixtures/decor-keyless.json +293 -0
  201. package/kits/hub-game/scripts/fixtures/engine-models-stub.mjs +23 -0
  202. package/kits/hub-game/scripts/fixtures/fake-dom.mjs +107 -0
  203. package/kits/hub-game/scripts/fixtures/interest-legacy.json +385 -0
  204. package/kits/hub-game/scripts/fixtures/npc-notes-v1.77.jsonl +4 -0
  205. package/kits/hub-game/scripts/fixtures/vis-shared-wire.json +1 -0
  206. package/kits/hub-game/scripts/gen-avatars.mjs +933 -16
  207. package/kits/hub-game/scripts/gen-earned.mjs +285 -0
  208. package/kits/hub-game/scripts/gen-hats.mjs +827 -0
  209. package/kits/hub-game/scripts/grab.before-orbit.mjs +400 -0
  210. package/kits/hub-game/scripts/soundscape-wave-analysis.mjs +125 -0
  211. package/kits/hub-game/scripts/test-actions-client.mjs +488 -0
  212. package/kits/hub-game/scripts/test-actions.mjs +298 -0
  213. package/kits/hub-game/scripts/test-actor-budget.mjs +44 -0
  214. package/kits/hub-game/scripts/test-actors.mjs +19 -17
  215. package/kits/hub-game/scripts/test-ask-client.mjs +358 -0
  216. package/kits/hub-game/scripts/test-ask.mjs +269 -0
  217. package/kits/hub-game/scripts/test-attention.mjs +10 -1
  218. package/kits/hub-game/scripts/test-avatar-cap.mjs +379 -0
  219. package/kits/hub-game/scripts/test-avatar-colors.mjs +72 -0
  220. package/kits/hub-game/scripts/test-avatar-models.mjs +181 -0
  221. package/kits/hub-game/scripts/test-avatar-share.mjs +318 -0
  222. package/kits/hub-game/scripts/test-avatar-wire.mjs +753 -0
  223. package/kits/hub-game/scripts/test-avatar.mjs +540 -7
  224. package/kits/hub-game/scripts/test-ballot.mjs +1001 -0
  225. package/kits/hub-game/scripts/test-build.mjs +31 -0
  226. package/kits/hub-game/scripts/test-cells.mjs +71 -0
  227. package/kits/hub-game/scripts/test-chat.mjs +662 -0
  228. package/kits/hub-game/scripts/test-classic.mjs +2 -0
  229. package/kits/hub-game/scripts/test-companion-chat.mjs +198 -0
  230. package/kits/hub-game/scripts/test-companion-client.mjs +1167 -0
  231. package/kits/hub-game/scripts/test-companion.mjs +259 -0
  232. package/kits/hub-game/scripts/test-decor-client.mjs +303 -0
  233. package/kits/hub-game/scripts/test-decor.mjs +457 -0
  234. package/kits/hub-game/scripts/test-earned-client.mjs +459 -0
  235. package/kits/hub-game/scripts/test-earned.mjs +1194 -0
  236. package/kits/hub-game/scripts/test-emote-bubble.mjs +110 -0
  237. package/kits/hub-game/scripts/test-friend-bests.mjs +435 -0
  238. package/kits/hub-game/scripts/test-grab-orbit.mjs +918 -0
  239. package/kits/hub-game/scripts/test-grab.mjs +7 -0
  240. package/kits/hub-game/scripts/test-hats.mjs +422 -168
  241. package/kits/hub-game/scripts/test-hub-leaving.mjs +203 -0
  242. package/kits/hub-game/scripts/test-hub-ops.mjs +439 -2
  243. package/kits/hub-game/scripts/test-hub-trip.mjs +716 -2
  244. package/kits/hub-game/scripts/test-hud-clear.mjs +328 -4
  245. package/kits/hub-game/scripts/test-idle.mjs +361 -1
  246. package/kits/hub-game/scripts/test-interest-cap.mjs +36 -0
  247. package/kits/hub-game/scripts/test-interest-cells.mjs +493 -0
  248. package/kits/hub-game/scripts/test-interest-client.mjs +118 -0
  249. package/kits/hub-game/scripts/test-keys.mjs +46 -1
  250. package/kits/hub-game/scripts/test-labels.mjs +596 -0
  251. package/kits/hub-game/scripts/test-manifest.mjs +207 -0
  252. package/kits/hub-game/scripts/test-menu-client.mjs +480 -0
  253. package/kits/hub-game/scripts/test-menu.mjs +296 -0
  254. package/kits/hub-game/scripts/test-npcs.mjs +1718 -12
  255. package/kits/hub-game/scripts/test-object-entry.mjs +76 -0
  256. package/kits/hub-game/scripts/test-pack.mjs +7 -0
  257. package/kits/hub-game/scripts/test-picker.mjs +314 -0
  258. package/kits/hub-game/scripts/test-rehello.mjs +1039 -0
  259. package/kits/hub-game/scripts/test-relations-among.mjs +222 -0
  260. package/kits/hub-game/scripts/test-relations-refresh.mjs +188 -0
  261. package/kits/hub-game/scripts/test-shot.mjs +756 -0
  262. package/kits/hub-game/scripts/test-soundscape.mjs +5432 -0
  263. package/kits/hub-game/scripts/test-spin.mjs +314 -0
  264. package/kits/hub-game/scripts/test-standing.mjs +416 -0
  265. package/kits/hub-game/scripts/test-stuck.mjs +1903 -0
  266. package/kits/hub-game/scripts/test-tier-cells.mjs +706 -0
  267. package/kits/hub-game/scripts/test-tiers-render.mjs +339 -0
  268. package/kits/hub-game/scripts/test-tiers.mjs +477 -2
  269. package/kits/hub-game/scripts/test-toast-band.mjs +61 -0
  270. package/kits/hub-game/scripts/test-try-on.mjs +22 -22
  271. package/kits/hub-game/scripts/test-vis.mjs +343 -0
  272. package/kits/hub-game/scripts/test-visibility.mjs +134 -0
  273. package/kits/hub-game/scripts/test-yours-chrome.mjs +166 -0
  274. package/kits/hub-game/scripts/test-yours.mjs +372 -0
  275. package/kits/multiplayer-3d/CLAUDE.md +50 -0
  276. package/kits/multiplayer-3d/castle.json +5 -5
  277. package/kits/multiplayer-3d/code/client/avatars.js +7 -0
  278. package/kits/multiplayer-3d/code/client/gait.js +65 -0
  279. package/kits/multiplayer-3d/code/client/nameTags.js +59 -4
  280. package/kits/multiplayer-3d/code/options.js +5 -0
  281. package/kits/multiplayer-3d/code/server/ownership.js +10 -2
  282. package/kits/multiplayer-3d/code/server/sceneBodies.js +12 -0
  283. package/kits/multiplayer-3d/code/server/world.js +7 -0
  284. package/kits/multiplayer-3d/code/systems/multiplayer.js +74 -4
  285. package/kits/multiplayer-3d/package.json +2 -1
  286. package/kits/multiplayer-3d/scripts/fixtures/README.md +11 -0
  287. package/kits/multiplayer-3d/scripts/fixtures/ownership-legacy.json +2 -0
  288. package/kits/multiplayer-3d/scripts/ownership-harness.mjs +102 -0
  289. package/kits/multiplayer-3d/scripts/ownership-loader.mjs +46 -0
  290. package/kits/multiplayer-3d/scripts/ownership-worker.mjs +15 -0
  291. package/kits/multiplayer-3d/scripts/record-ownership-session.mjs +98 -0
  292. package/kits/multiplayer-3d/scripts/test-eligible-claims.mjs +167 -0
  293. package/kits/multiplayer-3d/scripts/test-own-name-tag.mjs +247 -0
  294. package/kits/physics-2d/CLAUDE.md +9 -0
  295. package/kits/physics-2d/castle.json +2 -2
  296. package/kits/physics-2d/engine/scene.js +253 -1
  297. package/kits/physics-2d/engine/skyKey.js +129 -0
  298. package/kits/physics-2d/engine/stillClock.js +106 -0
  299. package/kits/physics-2d/engine/updateBudget.js +21 -0
  300. package/kits/physics-2d/scripts/test-skip-unchanged.mjs +326 -1
  301. package/kits/physics-3d/CLAUDE.md +1169 -30
  302. package/kits/physics-3d/behaviors/Player.jsx +629 -72
  303. package/kits/physics-3d/castle.json +3 -3
  304. package/kits/physics-3d/docs/pxmodel-format.md +8 -0
  305. package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +69 -13
  306. package/kits/physics-3d/engine3d/actornode.js +86 -0
  307. package/kits/physics-3d/engine3d/animationBudget.js +23 -0
  308. package/kits/physics-3d/engine3d/editorWorld.js +6 -0
  309. package/kits/physics-3d/engine3d/emptynodes.js +232 -0
  310. package/kits/physics-3d/engine3d/framecap.js +96 -0
  311. package/kits/physics-3d/engine3d/gait.js +118 -0
  312. package/kits/physics-3d/engine3d/kithooks.js +47 -0
  313. package/kits/physics-3d/engine3d/look.js +507 -48
  314. package/kits/physics-3d/engine3d/lookbake.js +208 -0
  315. package/kits/physics-3d/engine3d/materialpresets.js +2 -0
  316. package/kits/physics-3d/engine3d/motion.js +39 -10
  317. package/kits/physics-3d/engine3d/nearshadows.js +37 -19
  318. package/kits/physics-3d/engine3d/optimize.js +9 -5
  319. package/kits/physics-3d/engine3d/orbitinput.js +487 -9
  320. package/kits/physics-3d/engine3d/orbitlens.js +48 -0
  321. package/kits/physics-3d/engine3d/ownmodel.js +213 -0
  322. package/kits/physics-3d/engine3d/pxmodel.js +42 -11
  323. package/kits/physics-3d/engine3d/shadowfade.js +882 -0
  324. package/kits/physics-3d/engine3d/sky.js +547 -0
  325. package/kits/physics-3d/engine3d/skylife.js +306 -0
  326. package/kits/physics-3d/engine3d/unchanged.js +11 -0
  327. package/kits/physics-3d/engine3d/viewrange.js +52 -0
  328. package/kits/physics-3d/engine3d/world3d.js +796 -69
  329. package/kits/physics-3d/main.jsx +5 -0
  330. package/kits/physics-3d/package.json +1 -1
  331. package/kits/physics-3d/scripts/look-baseline.mjs +155 -0
  332. package/kits/physics-3d/scripts/test-camera-orbit.mjs +2563 -80
  333. package/kits/physics-3d/scripts/test-drop-empty-parts.mjs +1 -1
  334. package/kits/physics-3d/scripts/test-frame-cap-bob.mjs +435 -0
  335. package/kits/physics-3d/scripts/test-frame-cap.mjs +452 -0
  336. package/kits/physics-3d/scripts/test-gait.mjs +387 -0
  337. package/kits/physics-3d/scripts/test-look-bake.mjs +1056 -0
  338. package/kits/physics-3d/scripts/test-model-parse-cache.mjs +92 -0
  339. package/kits/physics-3d/scripts/test-near-shadows.mjs +3 -0
  340. package/kits/physics-3d/scripts/test-orbit-shadow-fade.mjs +1920 -0
  341. package/kits/physics-3d/scripts/test-orbit-shift-lock.mjs +545 -0
  342. package/kits/physics-3d/scripts/test-orbit-shot.mjs +583 -0
  343. package/kits/physics-3d/scripts/test-orbit-view.mjs +230 -0
  344. package/kits/physics-3d/scripts/test-skip-empty-nodes.mjs +845 -0
  345. package/kits/physics-3d/scripts/test-sky.mjs +2470 -0
  346. package/kits/physics-3d/scripts/test-static-actors.mjs +8 -1
  347. package/kits/physics-3d/scripts/test-static-scenery.mjs +2171 -0
  348. package/kits/physics-3d/scripts/test-steady-hover.mjs +2 -0
  349. package/kits/physics-3d/scripts/test-view-range.mjs +260 -0
  350. package/kits/physics-3d/systems/physics3d.js +383 -6
  351. package/kits/real-time/castle.json +2 -2
  352. package/kits/real-time/code/server/gameHooks.js +7 -0
  353. package/kits/real-time/code/server/session.js +97 -19
  354. package/package.json +2 -2
  355. package/dist/shell/assets/index-Cf61cNfT.css +0 -1
@@ -0,0 +1,1151 @@
1
+ // The soundscape (a deck with `hub.soundscape`): the sounds the kit makes
2
+ // itself, synthesised in WebAudio, never from a file and never speech:
3
+ //
4
+ // babble an NPC's line becomes made-up syllables with round-6 glottal pulses
5
+ // and four formants, in that NPC's own voice
6
+ // steps footsteps under the player and under the players and NPCs near
7
+ // them, a jump and a landing
8
+ // ambience optional zone beds, nearby spots and quiet generative music
9
+ // ui optional door transitions, private activity timers and chip taps
10
+ // cues chosen place cues and existing emote, seat and talk state edges
11
+ //
12
+ // with one control in the chrome (a speaker button: mute, volume) that also
13
+ // scales the place's own `hub.sound` files (`sound.js`).
14
+ //
15
+ // The hub system (`systems/hub.js`) makes one only in a deck that asks. A deck
16
+ // without the key never runs a line of this: no context, no listener, no
17
+ // storage read, no node.
18
+ //
19
+ // Everything plays into one master gain through a bus of its own (`voice`,
20
+ // `steps`, opt-in `ambience` and `ui`). `frame` sounds once per
21
+ // frame, `completeFrame` settles after the transport drain. Boolean steps
22
+ // use street everywhere; only an object reads the ground map.
23
+ //
24
+ // Rules that hold for every sound here:
25
+ // - nothing sounds before the player's first gesture, and nothing waits for
26
+ // it: a sound that cannot play now is dropped, never queued;
27
+ // - a line that arrived while nothing could sound is never said late;
28
+ // - who sounds is chosen before anything is built (the nearest two voices,
29
+ // the nearest four other steps), so a crowd costs no more than its cap;
30
+ // - no number that is not finite reaches the audio graph, and nothing here
31
+ // can throw into the hub's frame;
32
+ // - nothing sounds while the page is hidden, while the player is on a trip
33
+ // through a door, or while the control is muted, except the short tail of
34
+ // a departure whoosh started before the trip; hiding or muting cuts it;
35
+ // - no timers: everything is scheduled on the context's clock, and a node
36
+ // made for one sound is stopped and disconnected when it ends.
37
+
38
+ import * as Shared from '@imports/castle.physics-2d/engine/audioContext';
39
+ import { hub } from './state.js';
40
+ import { makeSoundscapeMap, sanitiseSoundscape } from './soundscapeMap.js';
41
+ import { makeAmbience, BEDS } from './soundscapeAmbience.js';
42
+ import { makeUi } from './soundscapeUi.js';
43
+ import { makeBusker } from './soundscapeBusker.js';
44
+ import { makeActions, distanceGain } from './soundscapeActions.js';
45
+ import { simlish, VOICE_TYPES } from './soundscapeVoice.js';
46
+ export { BABBLE_MAX_S, BABBLE_MAX_SYLLABLES, babblePlan, VOICE_TYPES } from './soundscapeVoice.js';
47
+
48
+ export const SETTINGS_KEY = 'castle.hub.soundscape.v1';
49
+ export const DEFAULT_VOLUME = 0.7;
50
+ // The control's 100% at the master gain, and each bus under it. Set by
51
+ // measuring what comes out (rendered offline in Chromium, at 100%): a step
52
+ // peaks at 0.09 to 0.17 of full scale by its surface, a landing near 0.25, a
53
+ // line near 0.3, and two voices, a landing and four other steps at the same
54
+ // instant near 0.6. A limiter behind the master holds what sums.
55
+ // The ambience bus was raised 8 dB from 0.5 on the owner's ear: footsteps
56
+ // were too loud against the beds, so a bed sits about 8 dB under a footstep.
57
+ export const MASTER = 0.9;
58
+ const BUS = { voice: 1, steps: 1.6, ambience: 0.5 * 10 ** (8 / 20), ui: 0.5 };
59
+
60
+ // --- babble ------------------------------------------------------------------
61
+
62
+ // NPC voices sounding at once (the nearest win), and how far a public line
63
+ // carries (`gainAt`'s straight line, as the place's own sounds fall off).
64
+ export const VOICES_MAX = 2;
65
+ export const BABBLE_RADIUS_M = 12;
66
+ // The panel owns its NPC's voice while open and for this long after closing
67
+ // or saying a line. Labels may be truncated, so no text is compared across sources.
68
+ const PANEL_VOICE_MS = 10000;
69
+ // Matches the old babble's loudness: rendered offline in Chromium through the
70
+ // whole chain (four NPCs x three lines, volume 0.7), the old line's median
71
+ // active RMS was -25.64 dBFS and this voice's -18.70 at 0.55, so 0.55 is
72
+ // scaled by 10^(-6.94/20).
73
+ export const BABBLE_LEVEL = 0.55 * 10 ** (-6.94 / 20);
74
+
75
+ // FNV-1a, 32 bits: the same string is the same number on every client.
76
+ export function hashOf(text) {
77
+ let h = 0x811c9dc5;
78
+ const s = String(text);
79
+ for (let i = 0; i < s.length; i += 1) {
80
+ h ^= s.charCodeAt(i);
81
+ h = Math.imul(h, 0x01000193);
82
+ }
83
+ return h >>> 0;
84
+ }
85
+
86
+ // A small seeded generator (mulberry32): numbers in [0, 1).
87
+ function seeded(seed) {
88
+ let a = seed >>> 0;
89
+ return () => {
90
+ a = (a + 0x6d2b79f5) >>> 0;
91
+ let t = a;
92
+ t = Math.imul(t ^ (t >>> 15), t | 1);
93
+ t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
94
+ return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
95
+ };
96
+ }
97
+
98
+ const clamp = (v, lo, hi) => Math.min(hi, Math.max(lo, v));
99
+
100
+ // Whole-word role defaults, in fixed precedence: gruff, gentle, lively.
101
+ // HubNpc role is the place's key; character is its characterBundle id.
102
+ export const VOICE_KEYWORDS = {
103
+ gruff: ['harbourmaster', 'harbour master', 'innkeeper', 'inn', 'fisher', 'captain', 'smith', 'guard', 'elder', 'grandpa', 'old'],
104
+ gentle: ['nursery', 'clinic', 'nurse', 'nana', 'granny', 'gardener', 'healer', 'vet', 'teacher', 'carer'],
105
+ lively: ['kid', 'child', 'barker', 'crier', 'hawker', 'postie', 'courier', 'runner', 'coach', 'announcer', 'busker'],
106
+ };
107
+ const rolePatterns = Object.entries(VOICE_KEYWORDS).map(([type, words]) =>
108
+ [type, new RegExp(`(?:^|[^\\p{L}\\p{N}_])(?:${words.join('|')})(?=$|[^\\p{L}\\p{N}_])`, 'u')]);
109
+
110
+ // Every NPC keeps its hash-derived words, pitch, mouth and rate variation.
111
+ // Legacy rate 4..10 maps linearly to 2.5..6; timbre remains ignored.
112
+ export function voiceOf(id, name = '', over = null, role = '', character = '') {
113
+ const seed = hashOf(`${id}|${name}`);
114
+ const next = seeded(seed);
115
+ const u = next();
116
+ next(); // Keep the old style draw so rate variation uses the same draw.
117
+ const words = `${role} ${character} ${name}`.toLowerCase();
118
+ const type = typeof over?.type === 'string' && Object.hasOwn(VOICE_TYPES, over.type)
119
+ ? over.type : rolePatterns.find(([, pattern]) => pattern.test(words))?.[0] ?? 'townsfolk';
120
+ const voice = {
121
+ ...VOICE_TYPES[type], seed, type,
122
+ pitch: 240 * 2 ** (u * 0.9 - 0.4),
123
+ mouth: 1 + u * 0.16,
124
+ rate: VOICE_TYPES[type].rate * (0.9 + next() * 0.2),
125
+ };
126
+ if (over && typeof over === 'object') {
127
+ if (typeof over.pitch === 'number' && Number.isFinite(over.pitch)) {
128
+ voice.pitch = clamp(over.pitch, 80, 500);
129
+ voice.mouth = 1 + clamp((Math.log2(voice.pitch / 240) + 0.4) / 0.9, 0, 1) * 0.16;
130
+ }
131
+ if (typeof over.rate === 'number' && Number.isFinite(over.rate)) {
132
+ voice.rate = 2.5 + (clamp(over.rate, 4, 10) - 4) * 3.5 / 6;
133
+ }
134
+ }
135
+ return voice;
136
+ }
137
+
138
+ // How loud a public line is `d` metres from the NPC who says it.
139
+ export function babbleGain(d) {
140
+ return Math.max(0, 1 - d / BABBLE_RADIUS_M);
141
+ }
142
+
143
+ // A line without the name the kit writes ahead of it over the NPC ("Rowan:
144
+ // Hello"), as the panel has it; its first words tell two copies of it apart.
145
+ function wordsOf(text, name) {
146
+ const line = String(text ?? '');
147
+ const head = name ? `${name}: ` : '';
148
+ return (head && line.startsWith(head) ? line.slice(head.length) : line).trim();
149
+ }
150
+
151
+ // --- steps -------------------------------------------------------------------
152
+
153
+ export const STRIDE_M = 0.65;
154
+ // A move this long in one frame is not a step: the player was put somewhere.
155
+ export const TELEPORT_M = 1.5;
156
+ export const OTHERS_GAIN = 0.5;
157
+ export const OTHERS_RADIUS_M = 10;
158
+ export const OTHER_STEPS_MAX = 4;
159
+ // A body slower than this is standing (a pose that drifts takes no steps).
160
+ const STILL_MPS = 0.4;
161
+ // A frame this long is a pause; what happened in it is not walked.
162
+ const PAUSE_S = 0.5;
163
+ // With no word on the ground (another player, an NPC): rising or falling this
164
+ // fast is in the air, and slower than SETTLED_MPS after a fall is landed.
165
+ const AIR_MPS = 3.5;
166
+ const SETTLED_MPS = 0.8;
167
+ // In the air this long with no fall seen: taken as back on the ground, quietly.
168
+ const AIR_LIMIT_S = 0.9;
169
+ // Shorter than this off the ground is a step's edge, not a landing.
170
+ const AIR_MIN_S = 0.12;
171
+
172
+ export const STEP = 1;
173
+ export const JUMP = 2;
174
+ export const LAND = 3;
175
+
176
+ // What a foot sounds like on each ground: a burst of noise through a filter
177
+ // (`type`, `f` Hz, `q`, `dur` seconds, `gain`) and a low thump under it.
178
+ // Grass is soft (low-passed at 2.5 kHz) and grass and wood are levelled so every
179
+ // surface's step sounds about as loud (K-weighted 50 ms step level within ~1 dB,
180
+ // measured offline in Chromium at volume 0.7).
181
+ export const SURFACES = {
182
+ grass: { lowCut: 180, type: 'lowpass', f: 2500, q: 0.6, dur: 0.11, gain: 0.17, thump: 68, thumpGain: 0.0785 },
183
+ street: { lowCut: 200, type: 'bandpass', f: 1100, q: 1.1, dur: 0.06, gain: 0.3, thump: 92, thumpGain: 0.12 },
184
+ wood: { lowCut: 220, type: 'bandpass', f: 520, q: 2.8, dur: 0.075, gain: 0.268, thump: 138, thumpGain: 0.142 },
185
+ stone: { lowCut: 200, type: 'bandpass', f: 1900, q: 2.4, dur: 0.045, gain: 0.44, thump: 108, thumpGain: 0.084 },
186
+ sand: { lowCut: 150, type: 'lowpass', f: 900, q: 0.5, dur: 0.14, gain: 0.4, thump: 60, thumpGain: 0.036 },
187
+ cobble: { lowCut: 210, type: 'bandpass', f: 1550, q: 3.2, dur: 0.055, gain: 0.36, thump: 122, thumpGain: 0.108 },
188
+ };
189
+ export const DEFAULT_SURFACE = 'street';
190
+
191
+ // The original ground, for boolean steps. Object steps use the scene map.
192
+ export function surfaceAt() {
193
+ return DEFAULT_SURFACE;
194
+ }
195
+
196
+ // How far a body goes between two footfalls at `speed` m/s: STRIDE_M at a walk
197
+ // (an NPC's 2.6 m/s is four steps a second), longer as it runs, so the
198
+ // Player's 6 m/s is still about four a second and not nine.
199
+ export function strideAt(speed) {
200
+ return STRIDE_M + clamp((speed - 2.6) * 0.25, 0, 0.85);
201
+ }
202
+
203
+ export function makeStepper() {
204
+ return { has: false, x: 0, y: 0, z: 0, walked: 0, foot: 0, air: false, airFor: 0, fell: false, fall: 0, seen: 0 };
205
+ }
206
+
207
+ // One frame of one body. `x`, `y`, `z` where it is, `dt` seconds since its
208
+ // last frame, `grounded` whether it stands on something (null where nobody
209
+ // says: then how fast it rises or falls decides), `vy` its vertical speed
210
+ // (null: taken from the two frames). Returns 0, STEP (`s.foot` is which
211
+ // foot), JUMP (it left the ground going up, where `grounded` is known) or
212
+ // LAND (`s.fall` is how fast it came down, m/s).
213
+ export function stepFrame(s, x, y, z, dt, grounded = null, vy = null) {
214
+ if (!s.has || !(dt > 0) || !Number.isFinite(x + y + z)) {
215
+ s.has = Number.isFinite(x + y + z);
216
+ s.x = x;
217
+ s.y = y;
218
+ s.z = z;
219
+ return 0;
220
+ }
221
+ const moved = Math.hypot(x - s.x, z - s.z);
222
+ const rise = y - s.y;
223
+ s.x = x;
224
+ s.y = y;
225
+ s.z = z;
226
+ if (moved > TELEPORT_M || Math.abs(rise) > 2 * TELEPORT_M || dt > PAUSE_S) {
227
+ s.walked = 0;
228
+ s.air = false;
229
+ s.airFor = 0;
230
+ return 0;
231
+ }
232
+ const v = vy ?? rise / dt;
233
+ let onGround = grounded;
234
+ if (onGround === null) {
235
+ if (!s.air) {
236
+ onGround = Math.abs(v) < AIR_MPS;
237
+ } else {
238
+ onGround = (s.fell && Math.abs(v) < SETTLED_MPS) || s.airFor > AIR_LIMIT_S;
239
+ }
240
+ }
241
+ if (!onGround) {
242
+ const left = !s.air;
243
+ if (left) {
244
+ s.air = true;
245
+ s.airFor = 0;
246
+ s.fell = false;
247
+ s.fall = 0;
248
+ }
249
+ s.airFor += dt;
250
+ if (v < -1) {
251
+ s.fell = true;
252
+ s.fall = Math.max(s.fall, -v);
253
+ }
254
+ return left && grounded === false && v > 2 ? JUMP : 0;
255
+ }
256
+ if (s.air) {
257
+ s.air = false;
258
+ s.walked = 0;
259
+ return s.fell && s.airFor >= AIR_MIN_S ? LAND : 0;
260
+ }
261
+ const speed = moved / dt;
262
+ if (speed < STILL_MPS) {
263
+ return 0;
264
+ }
265
+ s.walked += moved;
266
+ const stride = strideAt(speed);
267
+ if (s.walked < stride) {
268
+ return 0;
269
+ }
270
+ s.walked -= stride;
271
+ if (s.walked >= stride) {
272
+ s.walked = 0;
273
+ }
274
+ s.foot ^= 1;
275
+ return STEP;
276
+ }
277
+
278
+ // --- the control's memory ----------------------------------------------------
279
+
280
+ function pageStorage() {
281
+ try {
282
+ return globalThis.localStorage ?? null;
283
+ } catch {
284
+ return null;
285
+ }
286
+ }
287
+
288
+ export function readSettings(storage) {
289
+ const out = { volume: DEFAULT_VOLUME, muted: false };
290
+ try {
291
+ const kept = JSON.parse(storage?.getItem(SETTINGS_KEY) ?? 'null');
292
+ if (kept && typeof kept === 'object') {
293
+ if (typeof kept.volume === 'number' && Number.isFinite(kept.volume)) {
294
+ out.volume = clamp(kept.volume, 0, 1);
295
+ }
296
+ out.muted = kept.muted === true;
297
+ }
298
+ } catch {
299
+ // No storage, or not ours: the defaults.
300
+ }
301
+ return out;
302
+ }
303
+
304
+ function writeSettings(storage, settings) {
305
+ try {
306
+ storage?.setItem(SETTINGS_KEY, JSON.stringify({ volume: settings.volume, muted: settings.muted }));
307
+ } catch {
308
+ // A page without storage keeps the setting for this visit.
309
+ }
310
+ }
311
+
312
+ // --- the sounds --------------------------------------------------------------
313
+
314
+ // One second and a half of white noise per context, made once.
315
+ const noises = new WeakMap();
316
+ function noiseOf(ctx) {
317
+ let buffer = noises.get(ctx);
318
+ if (!buffer) {
319
+ const n = Math.floor(ctx.sampleRate * 1.5);
320
+ buffer = ctx.createBuffer(1, n, ctx.sampleRate);
321
+ const data = buffer.getChannelData(0);
322
+ const next = seeded(0x5eed);
323
+ for (let i = 0; i < n; i += 1) {
324
+ data[i] = next() * 2 - 1;
325
+ }
326
+ noises.set(ctx, buffer);
327
+ }
328
+ return buffer;
329
+ }
330
+
331
+ const RAY_FROM = { x: 0, y: 0, z: 0 };
332
+ const RAY_DOWN = { x: 0, y: -1, z: 0 };
333
+
334
+ // Whether the player's own body stands on something, as the Player behavior
335
+ // decides it (physics-3d `behaviors/Player.jsx` `applyHover`: a ray down from
336
+ // the middle of the capsule), or null where the physics cannot say.
337
+ function groundedOf(scene, local) {
338
+ const phys = scene.physics3d;
339
+ const t = local.components.Transform;
340
+ if (typeof phys?.castRay !== 'function') {
341
+ return null;
342
+ }
343
+ try {
344
+ const height = local.components.Shape?.height ?? 1.4;
345
+ RAY_FROM.x = t.x;
346
+ RAY_FROM.y = t.y;
347
+ RAY_FROM.z = t.z;
348
+ const hit = phys.castRay(RAY_FROM, RAY_DOWN, height, local);
349
+ return Boolean(hit && hit.distance < height / 2 + 0.4);
350
+ } catch {
351
+ return null;
352
+ }
353
+ }
354
+
355
+ const finite = (...values) => values.every((value) => typeof value === 'number' && Number.isFinite(value));
356
+
357
+ // Fallback activation belongs to the page, surviving disposal and scene rebuilds.
358
+ const gesturedPages = new WeakSet();
359
+
360
+ // `options` is the deck's `hub.soundscape` as the hook layer read it
361
+ // (`{ babble, steps, zones?, ambience?, ui?, cues? }`). `audio` is the shared context, `win`
362
+ // where the player's first gesture is heard, `onHide` is called as the page
363
+ // is hidden (the hub system quiets the place's own sound files there, which
364
+ // no frame would get to).
365
+ export function makeSoundscape(options, { audio = Shared, storage = pageStorage(), doc = globalThis.document ?? null, win = globalThis.window ?? null, nav = globalThis.navigator ?? null, random = Math.random, onHide = () => {}, isOn = null, loudestLoop = () => 0, cueVoices, createOfflineContext } = {}) {
366
+ options = sanitiseSoundscape(options, BEDS, SURFACES);
367
+ const want = { babble: options?.babble !== false, steps: options?.steps !== false };
368
+ const map = options?.ambience || typeof options?.steps === 'object' ? makeSoundscapeMap(options, SURFACES) : null;
369
+ const ambience = options?.ambience ? makeAmbience(options.ambience, { noise: noiseOf, seeded, hash: hashOf, loudestLoop, random, win, createOfflineContext, canBake: () => live() && ctx?.state === 'running' && Number.isFinite(ctx.currentTime) }) : null;
370
+ let mapScene = null;
371
+ const settings = readSettings(storage);
372
+ // The context once it has been asked for, and what hangs off it.
373
+ let ctx = null;
374
+ let out = null;
375
+ let resumeAt = -Infinity;
376
+ let disposed = false;
377
+ // Whether the player has touched the page: the browser's word where it
378
+ // gives one, else the first tap or key heard here.
379
+ const page = win ?? doc ?? globalThis;
380
+ // Lines being voiced: { npc, d, osc, gain, loud, nodes }.
381
+ const lines = [];
382
+ // npc actor id -> { text, frame }: the speech label last seen over it.
383
+ const seen = new Map();
384
+ // NPC -> when the panel's ownership of its voice expires after a line/close.
385
+ const panelUntil = new Map();
386
+ let panelNpc = null;
387
+ // This frame's new lines and others' steps, before any is built.
388
+ const linesWanted = [];
389
+ const stepsWanted = [];
390
+ // actor id -> stepper, the bodies near enough to hear; and the local one.
391
+ const steppers = new Map();
392
+ const mine = makeStepper();
393
+ // Other bodies' steps sounding: { until, d, shot }.
394
+ const otherSteps = [];
395
+ // Every one-shot not yet ended, to stop on the way out.
396
+ const shots = new Set();
397
+ // The scene's NPC actor ids, looked for again every 120 frames.
398
+ let npcIds = [];
399
+ let npcScene = null;
400
+ let npcAt = 0;
401
+ let frameNo = 0;
402
+ let lastAt = null;
403
+ /*
404
+ * RULE 1 (lateness): transport messages are buffered until a frame drains
405
+ * them. Set `settling` when hidden (visibilitychange), on a door trip,
406
+ * muted or at volume 0. Keep it set until TWO consecutive live frames have
407
+ * completed. While not live or settling, drop EVERYTHING processed for
408
+ * good and mark it seen: panel lines, speech labels, hub-sound one-shots
409
+ * (soundGate returns false), and footsteps. Count completed engine frames,
410
+ * after all systems, so drain order within a frame cannot change the rule.
411
+ * The first gesture does NOT set settling: before it, drop because the
412
+ * page is not activated; after it, play immediately if otherwise live.
413
+ * UI exceptions: a departure already started may finish into a trip;
414
+ * hiding or muting cuts it. Arrival alone waits for settling, for 3 s at
415
+ * most, and never waits for a gesture or through another interruption.
416
+ * RULE 2 (one voice per NPC line): the panel owns the voice of NPC X while
417
+ * open and for 10 s after closing or its last panel line. Voice NO private
418
+ * or public speech label on X in that window. Always voice panel lines
419
+ * (including repeats), subject to rule 1. Other NPCs' labels sound once by
420
+ * their existing per-label seen identity. Never compare panel/label text.
421
+ * INVARIANT C (activation is the page's, not the instance's): after a user
422
+ * gesture on this page, every later soundscape instance is activated.
423
+ */
424
+ let frameOn = true;
425
+ let settling = 0;
426
+ let warned = false;
427
+
428
+ const level = () => (settings.muted ? 0 : settings.volume * MASTER);
429
+ const hidden = () => doc?.hidden === true;
430
+ const activated = () => gesturedPages.has(page) || nav?.userActivation?.hasBeenActive === true;
431
+ // The hub supplies its current trip state; stand-alone frame callers keep
432
+ // their explicit `on` until their next frame.
433
+ const blocked = () => (isOn ? isOn() : frameOn) !== true || hidden() || settings.muted || settings.volume <= 0;
434
+ const live = () => {
435
+ const off = blocked();
436
+ if (off) {
437
+ settling = 2;
438
+ }
439
+ return !disposed && !off && activated() && settling === 0;
440
+ };
441
+ // How loud the place's own sound files play: null before the player's first
442
+ // gesture (`sound.js` then starts nothing), 0 hidden or muted.
443
+ const filesLevel = () => (!activated() ? null : hidden() || settings.muted ? 0 : settings.volume);
444
+
445
+ // Nothing here may stop the hub's frame: what throws is dropped, and said once.
446
+ function failed(err) {
447
+ if (!warned) {
448
+ warned = true;
449
+ console.warn(`[hub] soundscape: ${err?.message ?? err}`);
450
+ }
451
+ }
452
+
453
+ function build(context) {
454
+ const master = context.createGain();
455
+ master.gain.value = level();
456
+ const limiter = context.createDynamicsCompressor();
457
+ limiter.threshold.value = -10;
458
+ limiter.knee.value = 6;
459
+ limiter.ratio.value = 12;
460
+ limiter.attack.value = 0.003;
461
+ limiter.release.value = 0.15;
462
+ master.connect(limiter);
463
+ limiter.connect(context.destination);
464
+ const graph = { master, limiter };
465
+ for (const [name, gain] of Object.entries(BUS)) {
466
+ const bus = context.createGain();
467
+ bus.gain.value = gain;
468
+ bus.connect(master);
469
+ graph[name] = bus;
470
+ }
471
+ return graph;
472
+ }
473
+
474
+ // The context, running, or null: then whatever asked is dropped. Made the
475
+ // first time something would sound after the player has touched the page.
476
+ function ready(now) {
477
+ if (disposed || settings.muted || settings.volume <= 0 || hidden() || !activated()) {
478
+ return null;
479
+ }
480
+ if (!ctx) {
481
+ ctx = audio.getAudioContext?.() ?? null;
482
+ if (!ctx) {
483
+ return null;
484
+ }
485
+ audio.bindAudioResume?.();
486
+ out = build(ctx);
487
+ }
488
+ if (ctx.state !== 'running' || !Number.isFinite(ctx.currentTime)) {
489
+ if (now - resumeAt > 1000) {
490
+ resumeAt = now;
491
+ audio.resumeAudioContext?.();
492
+ }
493
+ return null;
494
+ }
495
+ return ctx;
496
+ }
497
+
498
+ function release(nodes) {
499
+ for (const node of nodes) {
500
+ try {
501
+ node.disconnect();
502
+ } catch {
503
+ // Already out.
504
+ }
505
+ }
506
+ }
507
+
508
+ const ui = options.ui === true || options.cues === true ? makeUi({ ready, context: () => ctx, bus: () => out.ui, noise: noiseOf, live, canArrive: () => !disposed && !blocked() && activated(), failed }) : null;
509
+ const busker = options.cues === true ? makeBusker({ ready, context: () => ctx, bus: () => out.ui, failed, distanceGain }) : null;
510
+ const actions = options.cues === true ? makeActions(ui, cueVoices, busker) : null;
511
+
512
+ // --- babble ---
513
+
514
+ // A line stopped: a 20 ms fade and all three sources stopped within 30 ms,
515
+ // with every node released by the ending callback.
516
+ function cut(line) {
517
+ const at = lines.indexOf(line);
518
+ if (at >= 0) {
519
+ lines.splice(at, 1);
520
+ }
521
+ line.voice.stop(ctx.currentTime);
522
+ }
523
+
524
+ function trackPanel(npc, now) {
525
+ if (panelNpc && panelNpc !== npc) {
526
+ panelUntil.set(panelNpc, now + PANEL_VOICE_MS);
527
+ }
528
+ panelNpc = npc;
529
+ for (const [id, until] of panelUntil) {
530
+ if (until <= now) {
531
+ panelUntil.delete(id);
532
+ }
533
+ }
534
+ }
535
+
536
+ // Builds and schedules one line: `words` in `npc`'s voice at `d` metres (0:
537
+ // said to this player in the panel). Whether it may sound, and whose place
538
+ // it takes, is decided before this is called. Returns whether it started.
539
+ function startLine(context, props, npc, name, words, d) {
540
+ const loud = babbleGain(d);
541
+ const t0 = context.currentTime + 0.03;
542
+ if (!(loud > 0) || !finite(loud, t0)) { return false; }
543
+ let gain;
544
+ try {
545
+ gain = context.createGain();
546
+ gain.gain.value = loud;
547
+ gain.connect(out.voice);
548
+ const line = { npc, d, gain, loud, voice: null };
549
+ line.voice = simlish(context, gain, words, t0, { ...voiceOf(npc, name, props.voice, props.role, props.character), level: BABBLE_LEVEL }, () => {
550
+ release([gain]);
551
+ const at = lines.indexOf(line);
552
+ if (at >= 0) { lines.splice(at, 1); }
553
+ });
554
+ if (!line.voice) { release([gain]); return false; }
555
+ lines.push(line);
556
+ return true;
557
+ } catch (err) {
558
+ if (gain) { release([gain]); }
559
+ failed(err);
560
+ return false;
561
+ }
562
+ }
563
+
564
+ // Makes room for a line at `d` metres from `npc`, or says there is none: a
565
+ // new line from the same NPC cuts its old one, and with VOICES_MAX sounding
566
+ // the farthest gives way to a nearer one only.
567
+ function roomFor(npc, d) {
568
+ for (let i = lines.length - 1; i >= 0; i -= 1) {
569
+ if (lines[i].npc === npc) {
570
+ cut(lines[i]);
571
+ }
572
+ }
573
+ if (lines.length < VOICES_MAX) {
574
+ return true;
575
+ }
576
+ let far = lines[0];
577
+ for (const line of lines) {
578
+ if (line.d > far.d) {
579
+ far = line;
580
+ }
581
+ }
582
+ if (!(far.d > d)) {
583
+ return false;
584
+ }
585
+ cut(far);
586
+ return true;
587
+ }
588
+
589
+ // This frame's new public lines: the nearest VOICES_MAX are looked at, and
590
+ // only one that has a place is built -- forty NPCs speaking in one frame
591
+ // cost two voices, not forty made and thirty-eight thrown away.
592
+ function voiceWanted(scene, now) {
593
+ const context = ready(now);
594
+ if (context) {
595
+ linesWanted.sort((a, b) => a.d - b.d);
596
+ for (let i = 0; i < linesWanted.length && i < VOICES_MAX; i += 1) {
597
+ const w = linesWanted[i];
598
+ if (roomFor(w.id, w.d)) {
599
+ startLine(context, w.props, w.id, w.name, w.words, w.d);
600
+ }
601
+ }
602
+ }
603
+ linesWanted.length = 0;
604
+ }
605
+
606
+ // How far `at` is from `me`, or Infinity where either is nowhere a number.
607
+ function apart(me, at) {
608
+ const d = me && at ? Math.hypot(at.x - me.x, at.y - me.y, at.z - me.z) : Infinity;
609
+ return Number.isFinite(d) ? d : Infinity;
610
+ }
611
+
612
+ // The speech labels over NPCs (`hub.labels`, with this player's own over
613
+ // them): one that is new is voiced, by how far its NPC stands. The NPC this
614
+ // player is talking with says its lines in the panel (`say`), not here.
615
+ // `fresh`: whether a new label may be voiced; one that appeared while
616
+ // nothing could sound (before the first gesture, on a trip, hidden, muted)
617
+ // is taken as seen and never said late.
618
+ function hear(scene, me, talkingWith, fresh, now) {
619
+ const own = hub.labelsMe;
620
+ for (let pass = 0; pass < 2; pass += 1) {
621
+ const labels = pass === 0 ? own : hub.labels;
622
+ for (const id in labels) {
623
+ if (pass === 1 && id in own) {
624
+ continue;
625
+ }
626
+ const label = labels[id];
627
+ if (label?.style !== 'speech' || typeof label.text !== 'string') {
628
+ continue;
629
+ }
630
+ const actor = scene.getActor(id);
631
+ const props = actor?.components?.HubNpc;
632
+ if (!props) {
633
+ continue;
634
+ }
635
+ const was = seen.get(id);
636
+ if (was && was.text === label.text) {
637
+ was.frame = frameNo;
638
+ continue;
639
+ }
640
+ seen.set(id, { text: label.text, frame: frameNo });
641
+ if (!fresh || id === talkingWith || now < (panelUntil.get(id) ?? -Infinity)) {
642
+ continue;
643
+ }
644
+ const name = typeof props.name === 'string' ? props.name : '';
645
+ const words = wordsOf(label.text, name);
646
+ const d = Math.max(apart(me, actor.components.Transform), 0.001);
647
+ if (!(babbleGain(d) > 0)) {
648
+ continue;
649
+ }
650
+ linesWanted.push({ id, props, name, words, d });
651
+ }
652
+ }
653
+ for (const [id, was] of seen) {
654
+ if (was.frame !== frameNo) {
655
+ seen.delete(id);
656
+ }
657
+ }
658
+ if (linesWanted.length) {
659
+ voiceWanted(scene, now);
660
+ }
661
+ // A public line follows the listener: louder as they walk up to it.
662
+ for (const line of lines) {
663
+ if (line.d === 0) {
664
+ continue;
665
+ }
666
+ const d = Math.max(apart(me, scene.getActor(line.npc)?.components.Transform), 0.001);
667
+ const loud = babbleGain(d);
668
+ line.d = d;
669
+ if (finite(loud) && Math.abs(loud - line.loud) > 0.03) {
670
+ line.loud = loud;
671
+ line.gain.gain.setTargetAtTime(loud, ctx.currentTime, 0.05);
672
+ }
673
+ }
674
+ }
675
+
676
+ // --- steps ---
677
+
678
+ function stopShot(shot) {
679
+ shots.delete(shot);
680
+ try {
681
+ shot.src.stop();
682
+ shot.low?.stop();
683
+ } catch {
684
+ // Not started, or over.
685
+ }
686
+ release(shot.nodes);
687
+ }
688
+
689
+ // A burst of the shared noise through `type` at `f`, and a thump under it,
690
+ // into the steps bus at `gain`; returns the one-shot, or null where a
691
+ // number it was given is not one (nothing is made then).
692
+ function burst(context, type, f, q, dur, gain, thump, thumpGain, sweepTo = 0, lowCut = 0) {
693
+ const t = context.currentTime;
694
+ if (!finite(t, f, q, dur, gain, thump, thumpGain, sweepTo, lowCut) || !(f > 0) || !(dur > 0) || !(gain > 0) || sweepTo < 0 || (thumpGain > 0 && !(thump > 0))) {
695
+ return null;
696
+ }
697
+ const nodes = [];
698
+ const made = (node) => {
699
+ nodes.push(node);
700
+ return node;
701
+ };
702
+ try {
703
+ const src = made(context.createBufferSource());
704
+ src.buffer = noiseOf(context);
705
+ const filter = made(context.createBiquadFilter());
706
+ filter.type = type;
707
+ filter.frequency.setValueAtTime(f, t);
708
+ if (sweepTo) {
709
+ filter.frequency.exponentialRampToValueAtTime(sweepTo, t + dur);
710
+ }
711
+ filter.Q.value = q;
712
+ const env = made(context.createGain());
713
+ env.gain.setValueAtTime(0, t);
714
+ env.gain.linearRampToValueAtTime(gain, t + (sweepTo ? dur * 0.4 : 0.004));
715
+ env.gain.exponentialRampToValueAtTime(0.0008, t + dur);
716
+ src.connect(filter);
717
+ if (lowCut >= 150) {
718
+ const cut = made(context.createBiquadFilter());
719
+ cut.type = 'highpass';
720
+ cut.frequency.value = lowCut;
721
+ cut.Q.value = 0.7;
722
+ filter.connect(cut);
723
+ cut.connect(env);
724
+ } else {
725
+ filter.connect(env);
726
+ }
727
+ env.connect(out.steps);
728
+ let low = null;
729
+ if (thumpGain > 0) {
730
+ low = made(context.createOscillator());
731
+ low.type = 'sine';
732
+ low.frequency.setValueAtTime(thump, t);
733
+ low.frequency.exponentialRampToValueAtTime(thump * 0.55, t + 0.045);
734
+ const lowEnv = made(context.createGain());
735
+ lowEnv.gain.setValueAtTime(0, t);
736
+ lowEnv.gain.linearRampToValueAtTime(thumpGain, t + 0.005);
737
+ lowEnv.gain.exponentialRampToValueAtTime(0.0008, t + 0.055);
738
+ low.connect(lowEnv);
739
+ lowEnv.connect(out.steps);
740
+ low.start(t);
741
+ low.stop(t + 0.065);
742
+ }
743
+ const shot = { nodes, src, low };
744
+ shots.add(shot);
745
+ src.onended = () => {
746
+ shots.delete(shot);
747
+ release(nodes);
748
+ };
749
+ // The noise from anywhere in the buffer, so no two steps are one sample.
750
+ src.start(t, clamp(random(), 0, 1) * 1.2 || 0, Math.max(dur, 0.1) + 0.02);
751
+ return shot;
752
+ } catch (err) {
753
+ release(nodes);
754
+ failed(err);
755
+ return null;
756
+ }
757
+ }
758
+
759
+ // A footfall on `surface` at `gain` (1 the player's own); `heavy` a landing
760
+ // (how hard, 0-1), or -1 for a plain step.
761
+ function footfall(context, surface, foot, gain, heavy = -1) {
762
+ const s = Object.hasOwn(SURFACES, surface) ? SURFACES[surface] : SURFACES[DEFAULT_SURFACE];
763
+ const tone = (foot ? 1 : 0.92) * (0.94 + random() * 0.12);
764
+ const loud = gain * (0.88 + random() * 0.24);
765
+ if (heavy < 0) {
766
+ // Another body's step is the burst alone.
767
+ return burst(context, s.type, s.f * tone, s.q, s.dur, s.gain * loud, s.thump * tone, gain >= 1 ? s.thumpGain * loud : 0, 0, s.lowCut);
768
+ }
769
+ const hard = 1 + heavy * 0.6;
770
+ return burst(context, s.type, s.f * 0.8, s.q, s.dur * 1.5, s.gain * gain * hard, s.thump * 0.85, Math.min(0.3, (s.thumpGain + 0.072) * gain * hard), 0, s.lowCut);
771
+ }
772
+
773
+ // The player's own step, jump or landing.
774
+ function playOwn(event, s, x, z, now) {
775
+ const context = ready(now);
776
+ if (!context) {
777
+ return;
778
+ }
779
+ if (event === JUMP) {
780
+ // A small whoosh upward, and no thump.
781
+ burst(context, 'bandpass', 500, 1.4, 0.13, 0.22, 0, 0, 1900);
782
+ } else if (event === LAND) {
783
+ footfall(context, map ? map.ownSurface(x, z) : surfaceAt(x, z), s.foot, 1, finite(s.fall) ? clamp((s.fall - 4) / 10, 0, 1) : 0);
784
+ } else {
785
+ footfall(context, map ? map.ownSurface(x, z) : surfaceAt(x, z), s.foot, 1);
786
+ }
787
+ }
788
+
789
+ // This frame's steps of other bodies, nearest first: OTHER_STEPS_MAX sound
790
+ // at once, a nearer one takes the place of the farthest still sounding
791
+ // (which is stopped, not faded under it), and nothing is built for a step
792
+ // that has no place -- forty walkers in step cost four bursts.
793
+ function playWanted(now) {
794
+ const context = ready(now);
795
+ if (context) {
796
+ const t = context.currentTime;
797
+ for (let i = otherSteps.length - 1; i >= 0; i -= 1) {
798
+ if (otherSteps[i].until <= t || !shots.has(otherSteps[i].shot)) {
799
+ otherSteps.splice(i, 1);
800
+ }
801
+ }
802
+ stepsWanted.sort((a, b) => a.d - b.d);
803
+ for (const w of stepsWanted) {
804
+ const gain = OTHERS_GAIN * (1 - w.d / OTHERS_RADIUS_M);
805
+ if (!(gain > 0)) {
806
+ break;
807
+ }
808
+ if (otherSteps.length >= OTHER_STEPS_MAX) {
809
+ let far = 0;
810
+ for (let i = 1; i < otherSteps.length; i += 1) {
811
+ if (otherSteps[i].d > otherSteps[far].d) {
812
+ far = i;
813
+ }
814
+ }
815
+ if (!(otherSteps[far].d > w.d)) {
816
+ break;
817
+ }
818
+ stopShot(otherSteps.splice(far, 1)[0].shot);
819
+ }
820
+ const surface = map ? map.surfaceAt(w.x, w.z, steppers.get(w.id)?.zone) : surfaceAt(w.x, w.z);
821
+ const shot = w.event === LAND ? footfall(context, surface, w.foot, gain, 0) : footfall(context, surface, w.foot, gain);
822
+ if (shot) {
823
+ otherSteps.push({ until: t + 0.12, d: w.d, shot });
824
+ }
825
+ }
826
+ }
827
+ stepsWanted.length = 0;
828
+ }
829
+
830
+ function walkOther(scene, id, me, dt, live) {
831
+ const t = scene.getActor(id)?.components.Transform;
832
+ // A hidden actor waits far under the ground.
833
+ if (!t || t.y < -100 || hub.hidden.has(id)) {
834
+ return;
835
+ }
836
+ const d = apart(me, t);
837
+ if (!(d <= OTHERS_RADIUS_M + 2)) {
838
+ return;
839
+ }
840
+ let s = steppers.get(id);
841
+ if (!s) {
842
+ s = makeStepper();
843
+ steppers.set(id, s);
844
+ }
845
+ s.seen = frameNo;
846
+ if (map) {
847
+ s.zone = map.zoneAt(t.x, t.z, s.zone);
848
+ }
849
+ const event = stepFrame(s, t.x, t.y, t.z, dt);
850
+ if (event && live && d < OTHERS_RADIUS_M) {
851
+ stepsWanted.push({ id, event, foot: s.foot, x: t.x, z: t.z, d: Math.max(d, 0.001) });
852
+ }
853
+ }
854
+
855
+ function walk(scene, mp, local, dt, live, now) {
856
+ const me = local?.components.Transform ?? null;
857
+ if (!me) {
858
+ mine.has = false;
859
+ return;
860
+ }
861
+ if (hub.hidden.has(local.id)) {
862
+ mine.has = false;
863
+ } else {
864
+ const grounded = groundedOf(scene, local);
865
+ let vy = null;
866
+ try {
867
+ vy = grounded === null ? null : (scene.physics3d.getVelocity?.(local)?.y ?? null);
868
+ } catch {
869
+ vy = null;
870
+ }
871
+ const event = stepFrame(mine, me.x, me.y, me.z, dt, grounded, finite(vy) ? vy : null);
872
+ if (event && live) {
873
+ playOwn(event, mine, me.x, me.z, now);
874
+ }
875
+ }
876
+ const actors = mp?.avatars?.actors;
877
+ if (actors) {
878
+ for (const actorId of actors.values()) {
879
+ walkOther(scene, actorId, me, dt, live);
880
+ }
881
+ }
882
+ if (scene !== npcScene || frameNo - npcAt > 120) {
883
+ npcScene = scene;
884
+ npcAt = frameNo;
885
+ npcIds = [];
886
+ for (const actor of scene.getActors?.() ?? []) {
887
+ if (actor.components?.HubNpc) {
888
+ npcIds.push(actor.id);
889
+ }
890
+ }
891
+ }
892
+ for (let i = 0; i < npcIds.length; i += 1) {
893
+ walkOther(scene, npcIds[i], me, dt, live);
894
+ }
895
+ if (stepsWanted.length) {
896
+ playWanted(now);
897
+ }
898
+ if (frameNo % 64 === 0) {
899
+ for (const [id, s] of steppers) {
900
+ if (s.seen < frameNo - 64) {
901
+ steppers.delete(id);
902
+ }
903
+ }
904
+ }
905
+ }
906
+
907
+ // --- all of it ---
908
+
909
+ function quiet(departing = false) {
910
+ ambience?.quiet();
911
+ if (busker) { busker.quiet(); }
912
+ ui?.quiet(departing && !hidden() && !settings.muted && settings.volume > 0 && activated());
913
+ if (actions && hub.cueEvents) {
914
+ hub.cueEvents.length = 0;
915
+ }
916
+ for (const line of [...lines]) {
917
+ cut(line);
918
+ }
919
+ for (const shot of [...shots]) {
920
+ stopShot(shot);
921
+ }
922
+ otherSteps.length = 0;
923
+ linesWanted.length = 0;
924
+ stepsWanted.length = 0;
925
+ }
926
+
927
+ function apply() {
928
+ try {
929
+ if (out) {
930
+ const t = ctx.currentTime;
931
+ out.master.gain.cancelScheduledValues(t);
932
+ out.master.gain.setTargetAtTime(hidden() ? 0 : level(), t, 0.02);
933
+ }
934
+ if (settings.muted || settings.volume <= 0) {
935
+ settling = 2;
936
+ quiet();
937
+ }
938
+ } catch (err) {
939
+ failed(err);
940
+ }
941
+ }
942
+
943
+ const onVisibility = () => {
944
+ try {
945
+ if (hidden()) {
946
+ settling = 2;
947
+ quiet();
948
+ onHide();
949
+ }
950
+ } catch (err) {
951
+ failed(err);
952
+ }
953
+ apply();
954
+ };
955
+ doc?.addEventListener?.('visibilitychange', onVisibility);
956
+ // The first tap or key, for a browser that does not say whether there was one.
957
+ const onGesture = () => {
958
+ gesturedPages.add(page);
959
+ win?.removeEventListener?.('pointerdown', onGesture, true);
960
+ win?.removeEventListener?.('keydown', onGesture, true);
961
+ };
962
+ if (!nav?.userActivation && !gesturedPages.has(page)) {
963
+ win?.addEventListener?.('pointerdown', onGesture, true);
964
+ win?.addEventListener?.('keydown', onGesture, true);
965
+ }
966
+
967
+ function run(scene, mp, local, on, talkingWith, now) {
968
+ frameNo += 1;
969
+ const dt = lastAt === null ? 0 : (now - lastAt) / 1000;
970
+ lastAt = now;
971
+ frameOn = on === true;
972
+ const liveNow = live();
973
+ trackPanel(talkingWith, now);
974
+ if (busker) { busker.frame(scene, local, now, liveNow); }
975
+ if (!liveNow) {
976
+ if (ui) {
977
+ quiet((isOn ? isOn() : frameOn) === false);
978
+ } else {
979
+ quiet();
980
+ }
981
+ }
982
+ if (ui) {
983
+ ui.frame(now, liveNow);
984
+ }
985
+ if (map) {
986
+ if (mapScene !== scene) {
987
+ ambience?.quiet();
988
+ mapScene = scene;
989
+ }
990
+ map.read(scene);
991
+ const me = local?.components.Transform;
992
+ if (me) {
993
+ map.update(me.x, me.z);
994
+ if (ambience && liveNow && !hub.hidden.has(local.id) && Number.isFinite(me.x) && Number.isFinite(me.y) && Number.isFinite(me.z)) {
995
+ const context = ready(now);
996
+ if (context) {
997
+ ambience.frame(context, out.ambience, map.bed(), map.spotAt(me.x, me.z));
998
+ }
999
+ } else {
1000
+ ambience?.quiet();
1001
+ }
1002
+ } else {
1003
+ ambience?.quiet();
1004
+ }
1005
+ }
1006
+ if (want.babble) {
1007
+ hear(scene, local?.components.Transform ?? null, talkingWith, liveNow, now);
1008
+ }
1009
+ if (want.steps) {
1010
+ walk(scene, mp, local, finite(dt) ? dt : 0, liveNow, now);
1011
+ }
1012
+ }
1013
+
1014
+ return {
1015
+ // Also gates file one-shots when the transport drains, including settling.
1016
+ live,
1017
+ // Only the new ui sub-key makes callbacks. Arrival alone waits for
1018
+ // settling; an ordinary chip, departure or activity cue never waits.
1019
+ ...(options.ui === true ? {
1020
+ cue(name, now) {
1021
+ try {
1022
+ return ui.cue(name, now);
1023
+ } catch (err) {
1024
+ failed(err);
1025
+ return false;
1026
+ }
1027
+ },
1028
+ uiArrival(now) {
1029
+ try {
1030
+ ui.arrive(now);
1031
+ } catch (err) {
1032
+ failed(err);
1033
+ }
1034
+ },
1035
+ } : {}),
1036
+ // Registered after every other engine system. The recovery frame stays
1037
+ // silent through a drain after `frame`, and those late labels/poses are
1038
+ // marked seen before counting this live frame as completed.
1039
+ completeFrame(scene, mp, local, on, talkingWith, now) {
1040
+ try {
1041
+ frameOn = on === true;
1042
+ trackPanel(talkingWith, now);
1043
+ const liveNow = live();
1044
+ if (ui) {
1045
+ ui.frame(now, liveNow, hub.place && !hub.place.result ? hub.hudMe : null, options.ui === true);
1046
+ }
1047
+ if (actions) {
1048
+ actions.frame(scene, mp, local, talkingWith, now, liveNow);
1049
+ busker.frame(scene, local, now, liveNow); // This frame's sends start now.
1050
+ }
1051
+ if (want.babble) {
1052
+ hear(scene, local?.components.Transform ?? null, talkingWith, liveNow, now);
1053
+ }
1054
+ if (!liveNow && want.steps) {
1055
+ walk(scene, mp, local, 0, false, now);
1056
+ }
1057
+ if (!disposed && !blocked() && activated()) {
1058
+ settling = Math.max(0, settling - 1);
1059
+ }
1060
+ } catch (err) {
1061
+ failed(err);
1062
+ }
1063
+ },
1064
+ // One frame. `on`: whether the place may sound now (not during a trip, as
1065
+ // `sound.js`); `talkingWith` the NPC whose words are in the open panel.
1066
+ // Returns how loud the place's own sound files play, 0 to 1, or null
1067
+ // before the player's first gesture: `sound.js` starts nothing then.
1068
+ // Never throws: a sound that cannot be made is dropped.
1069
+ frame(scene, mp, local, on, talkingWith, now) {
1070
+ try {
1071
+ run(scene, mp, local, on, talkingWith, now);
1072
+ } catch (err) {
1073
+ failed(err);
1074
+ linesWanted.length = 0;
1075
+ stepsWanted.length = 0;
1076
+ }
1077
+ return filesLevel();
1078
+ },
1079
+ // A line the NPC this player is talking with said in the panel: theirs
1080
+ // alone, at full voice. One that comes while nothing may sound (hidden,
1081
+ // a trip, before a gesture, or settling) is dropped, not kept.
1082
+ say(scene, npc, text, now) {
1083
+ try {
1084
+ if (!want.babble || !npc || typeof text !== 'string' || disposed) {
1085
+ return;
1086
+ }
1087
+ const props = scene?.getActor?.(npc)?.components?.HubNpc;
1088
+ if (!props) {
1089
+ return;
1090
+ }
1091
+ panelUntil.set(npc, now + PANEL_VOICE_MS);
1092
+ if (!live()) {
1093
+ return;
1094
+ }
1095
+ const name = typeof props.name === 'string' ? props.name : '';
1096
+ const words = wordsOf(text, name);
1097
+ const context = ready(now);
1098
+ if (context && roomFor(npc, 0)) {
1099
+ startLine(context, props, npc, name, words, 0);
1100
+ }
1101
+ } catch (err) {
1102
+ failed(err);
1103
+ }
1104
+ },
1105
+ settings: () => settings,
1106
+ setVolume(volume) {
1107
+ const v = Number(volume);
1108
+ settings.volume = Number.isNaN(v) ? 0 : clamp(v, 0, 1);
1109
+ writeSettings(storage, settings);
1110
+ apply();
1111
+ },
1112
+ setMuted(muted) {
1113
+ settings.muted = muted === true;
1114
+ writeSettings(storage, settings);
1115
+ apply();
1116
+ },
1117
+ // What is sounding, for the tests and a phone's readout.
1118
+ counts: () => ({ lines: lines.length, shots: shots.size, others: otherSteps.length, steppers: steppers.size, panels: panelUntil.size }),
1119
+ // Optional ground and bed readout; the original counts keep their shape.
1120
+ ambienceCounts: () => ambience?.counts() ?? { beds: 0, spot: 0, nodes: 0 },
1121
+ uiCounts: () => ui?.counts() ?? { voices: 0, nodes: 0, arrival: false },
1122
+ ...(busker ? { buskerCounts: () => busker.counts() } : {}),
1123
+ zone: () => map?.zone() ?? null,
1124
+ surfaceAt: (x, z) => map ? map.ownSurface(x, z) : surfaceAt(x, z),
1125
+ dispose() {
1126
+ if (disposed) {
1127
+ return;
1128
+ }
1129
+ doc?.removeEventListener?.('visibilitychange', onVisibility);
1130
+ win?.removeEventListener?.('pointerdown', onGesture, true);
1131
+ win?.removeEventListener?.('keydown', onGesture, true);
1132
+ try {
1133
+ if (ctx) {
1134
+ quiet();
1135
+ release(Object.values(out));
1136
+ }
1137
+ ui?.dispose();
1138
+ if (busker) { busker.dispose(); }
1139
+ } catch (err) {
1140
+ failed(err);
1141
+ }
1142
+ disposed = true;
1143
+ out = null;
1144
+ ctx = null;
1145
+ seen.clear();
1146
+ panelUntil.clear();
1147
+ panelNpc = null;
1148
+ steppers.clear();
1149
+ },
1150
+ };
1151
+ }