castle-web-cli 0.4.231 → 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 (368) 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/serverBundle.js +5 -3
  28. package/dist/shell/assets/{index-D2tWM896.js → index-BjOxR-lW.js} +4 -4
  29. package/dist/shell/assets/index-MVLfNYd9.css +1 -0
  30. package/dist/shell/index.html +2 -2
  31. package/dist/taskSteer.d.ts +13 -0
  32. package/dist/taskSteer.js +22 -2
  33. package/kits/base/castle.json +1 -1
  34. package/kits/base/package-lock.json +1 -1
  35. package/kits/base/sdk/README.md +79 -2
  36. package/kits/base/sdk/avatar.d.ts +6 -2
  37. package/kits/base/sdk/avatar.js +12 -4
  38. package/kits/base/sdk/castle.d.ts +1 -1
  39. package/kits/base/sdk/commands.d.ts +14 -0
  40. package/kits/base/sdk/hub.d.ts +6 -2
  41. package/kits/base/sdk/hub.js +13 -0
  42. package/kits/base/sdk/multiplayer.js +17 -1
  43. package/kits/base/sdk/save.js +2 -1
  44. package/kits/base/sdk/server/hub.d.ts +87 -0
  45. package/kits/base/sdk/server/hub.js +70 -0
  46. package/kits/base/sdk/server/platformHandle.d.ts +31 -1
  47. package/kits/base/sdk/server/storage.d.ts +13 -0
  48. package/kits/base/sdk/server/storage.js +46 -0
  49. package/kits/base/sdk/server/wrapper.d.ts +36 -5
  50. package/kits/base/sdk/server/wrapper.js +83 -8
  51. package/kits/hub-game/CLAUDE.md +352 -12
  52. package/kits/hub-game/blueprints/avatar-10.scene +28 -0
  53. package/kits/hub-game/blueprints/avatar-11.scene +28 -0
  54. package/kits/hub-game/blueprints/avatar-12.scene +28 -0
  55. package/kits/hub-game/blueprints/avatar-13.scene +28 -0
  56. package/kits/hub-game/blueprints/avatar-14.scene +28 -0
  57. package/kits/hub-game/blueprints/avatar-15.scene +28 -0
  58. package/kits/hub-game/blueprints/avatar-4.scene +28 -0
  59. package/kits/hub-game/blueprints/avatar-5.scene +28 -0
  60. package/kits/hub-game/blueprints/avatar-6.scene +28 -0
  61. package/kits/hub-game/blueprints/avatar-7.scene +28 -0
  62. package/kits/hub-game/blueprints/avatar-8.scene +28 -0
  63. package/kits/hub-game/blueprints/avatar-9.scene +28 -0
  64. package/kits/hub-game/code/client/actorBudget.js +47 -0
  65. package/kits/hub-game/code/client/attention.js +11 -2
  66. package/kits/hub-game/code/client/avatar.js +345 -32
  67. package/kits/hub-game/code/client/avatarCap.js +228 -0
  68. package/kits/hub-game/code/client/avatarColors.js +70 -0
  69. package/kits/hub-game/code/client/avatarFaces.js +21 -0
  70. package/kits/hub-game/code/client/avatarShare.js +251 -0
  71. package/kits/hub-game/code/client/ballot.js +467 -0
  72. package/kits/hub-game/code/client/build.js +22 -0
  73. package/kits/hub-game/code/client/chat.js +12 -0
  74. package/kits/hub-game/code/client/chrome.js +1334 -50
  75. package/kits/hub-game/code/client/companion.js +716 -0
  76. package/kits/hub-game/code/client/companionDress.js +99 -0
  77. package/kits/hub-game/code/client/decor.js +509 -0
  78. package/kits/hub-game/code/client/earned.js +131 -0
  79. package/kits/hub-game/code/client/emoteBubble.js +75 -0
  80. package/kits/hub-game/code/client/grab.js +363 -20
  81. package/kits/hub-game/code/client/hats.js +26 -0
  82. package/kits/hub-game/code/client/hookLayer.js +328 -15
  83. package/kits/hub-game/code/client/hudClear.js +126 -0
  84. package/kits/hub-game/code/client/idle.js +163 -22
  85. package/kits/hub-game/code/client/interestCap.js +81 -0
  86. package/kits/hub-game/code/client/keys.js +36 -0
  87. package/kits/hub-game/code/client/labelsNear.js +205 -0
  88. package/kits/hub-game/code/client/menuIcons.js +34 -0
  89. package/kits/hub-game/code/client/pack.js +6 -2
  90. package/kits/hub-game/code/client/packFormat.js +3 -3
  91. package/kits/hub-game/code/client/rehello.js +167 -0
  92. package/kits/hub-game/code/client/shot.js +92 -0
  93. package/kits/hub-game/code/client/sound.js +63 -8
  94. package/kits/hub-game/code/client/soundscape.js +1151 -0
  95. package/kits/hub-game/code/client/soundscapeActions.js +99 -0
  96. package/kits/hub-game/code/client/soundscapeAmbience.js +453 -0
  97. package/kits/hub-game/code/client/soundscapeBaked.js +143 -0
  98. package/kits/hub-game/code/client/soundscapeBusker.js +217 -0
  99. package/kits/hub-game/code/client/soundscapeCourt.js +200 -0
  100. package/kits/hub-game/code/client/soundscapeCrowd.js +331 -0
  101. package/kits/hub-game/code/client/soundscapeCues.js +362 -0
  102. package/kits/hub-game/code/client/soundscapeMap.js +181 -0
  103. package/kits/hub-game/code/client/soundscapeMarket.js +176 -0
  104. package/kits/hub-game/code/client/soundscapeMusic.js +126 -0
  105. package/kits/hub-game/code/client/soundscapeMusicEngine.js +460 -0
  106. package/kits/hub-game/code/client/soundscapeSimlish.js +737 -0
  107. package/kits/hub-game/code/client/soundscapeSongs.js +127 -0
  108. package/kits/hub-game/code/client/soundscapeSongsRound3.js +63 -0
  109. package/kits/hub-game/code/client/soundscapeSynthesis.js +168 -0
  110. package/kits/hub-game/code/client/soundscapeUi.js +211 -0
  111. package/kits/hub-game/code/client/soundscapeVoice.js +1209 -0
  112. package/kits/hub-game/code/client/soundscapeWaves.js +206 -0
  113. package/kits/hub-game/code/client/spin.js +101 -0
  114. package/kits/hub-game/code/client/state.js +131 -1
  115. package/kits/hub-game/code/client/stuck.js +259 -0
  116. package/kits/hub-game/code/client/talk.js +538 -47
  117. package/kits/hub-game/code/client/tiers.js +541 -27
  118. package/kits/hub-game/code/client/toastBand.js +37 -0
  119. package/kits/hub-game/code/client/together.js +98 -1
  120. package/kits/hub-game/code/client/visibility.js +43 -0
  121. package/kits/hub-game/code/client/wear.js +470 -97
  122. package/kits/hub-game/code/client/yours.js +199 -0
  123. package/kits/hub-game/code/companionSpec.js +45 -0
  124. package/kits/hub-game/code/privateText.js +7 -0
  125. package/kits/hub-game/code/server/actions.js +133 -0
  126. package/kits/hub-game/code/server/actors.js +14 -1
  127. package/kits/hub-game/code/server/ask.js +155 -0
  128. package/kits/hub-game/code/server/ballot.js +539 -0
  129. package/kits/hub-game/code/server/cells.js +100 -0
  130. package/kits/hub-game/code/server/companion.js +82 -0
  131. package/kits/hub-game/code/server/cues.js +70 -0
  132. package/kits/hub-game/code/server/decor.js +269 -0
  133. package/kits/hub-game/code/server/earned.js +461 -0
  134. package/kits/hub-game/code/server/friendBests.js +166 -0
  135. package/kits/hub-game/code/server/friendsAmong.js +232 -0
  136. package/kits/hub-game/code/server/grab.js +296 -11
  137. package/kits/hub-game/code/server/history.js +5 -0
  138. package/kits/hub-game/code/server/hub.js +1317 -52
  139. package/kits/hub-game/code/server/idle.js +30 -11
  140. package/kits/hub-game/code/server/index.js +2 -2
  141. package/kits/hub-game/code/server/labels.js +28 -2
  142. package/kits/hub-game/code/server/manifest.js +424 -8
  143. package/kits/hub-game/code/server/menu.js +133 -0
  144. package/kits/hub-game/code/server/npcs.js +830 -80
  145. package/kits/hub-game/code/server/party.js +13 -2
  146. package/kits/hub-game/code/server/players.js +22 -0
  147. package/kits/hub-game/code/server/say.js +8 -2
  148. package/kits/hub-game/code/server/shot.js +110 -0
  149. package/kits/hub-game/code/server/standing.js +124 -0
  150. package/kits/hub-game/code/server/vis.js +113 -0
  151. package/kits/hub-game/code/server/wear.js +128 -11
  152. package/kits/hub-game/code/server/yours.js +59 -0
  153. package/kits/hub-game/code/shared/cues.js +9 -0
  154. package/kits/hub-game/code/shared/decor.js +41 -0
  155. package/kits/hub-game/code/shared/shot.js +19 -0
  156. package/kits/hub-game/code/shared/spin.js +57 -0
  157. package/kits/hub-game/code/systems/hub.js +1083 -85
  158. package/kits/hub-game/docs/content-pack.md +1 -1
  159. package/kits/hub-game/docs/contract.md +2440 -61
  160. package/kits/hub-game/docs/home-pieces.md +64 -0
  161. package/kits/hub-game/models/avatar-10.pxmodel +28 -0
  162. package/kits/hub-game/models/avatar-11.pxmodel +28 -0
  163. package/kits/hub-game/models/avatar-12.pxmodel +27 -0
  164. package/kits/hub-game/models/avatar-13.pxmodel +24 -0
  165. package/kits/hub-game/models/avatar-14.pxmodel +29 -0
  166. package/kits/hub-game/models/avatar-15.pxmodel +24 -0
  167. package/kits/hub-game/models/avatar-4.pxmodel +29 -0
  168. package/kits/hub-game/models/avatar-5.pxmodel +27 -0
  169. package/kits/hub-game/models/avatar-6.pxmodel +26 -0
  170. package/kits/hub-game/models/avatar-7.pxmodel +25 -0
  171. package/kits/hub-game/models/avatar-8.pxmodel +24 -0
  172. package/kits/hub-game/models/avatar-9.pxmodel +30 -0
  173. package/kits/hub-game/models/earned/badge-round.pxmodel +100 -0
  174. package/kits/hub-game/models/earned/badge-shield.pxmodel +117 -0
  175. package/kits/hub-game/models/earned/badge-star.pxmodel +87 -0
  176. package/kits/hub-game/models/earned/belt-plain.pxmodel +72 -0
  177. package/kits/hub-game/models/earned/held-ball.pxmodel +43 -0
  178. package/kits/hub-game/models/earned/held-lantern.pxmodel +85 -0
  179. package/kits/hub-game/models/earned/held-net.pxmodel +57 -0
  180. package/kits/hub-game/models/earned/held-trophy.pxmodel +109 -0
  181. package/kits/hub-game/models/earned/pin-round.pxmodel +62 -0
  182. package/kits/hub-game/models/earned/sash-plain.pxmodel +66 -0
  183. package/kits/hub-game/models/earned/square/belt-plain.pxmodel +104 -0
  184. package/kits/hub-game/models/earned/square/sash-plain.pxmodel +98 -0
  185. package/kits/hub-game/models/hats/bat-clip.pxmodel +206 -0
  186. package/kits/hub-game/models/hats/beekeeper.pxmodel +149 -0
  187. package/kits/hub-game/models/hats/cap.pxmodel +162 -0
  188. package/kits/hub-game/models/hats/captain.pxmodel +178 -0
  189. package/kits/hub-game/models/hats/crooked-top.pxmodel +172 -0
  190. package/kits/hub-game/models/hats/flower-crown.pxmodel +451 -0
  191. package/kits/hub-game/models/hats/foam-crown.pxmodel +179 -0
  192. package/kits/hub-game/models/hats/lantern.pxmodel +230 -0
  193. package/kits/hub-game/models/hats/laurel.pxmodel +533 -0
  194. package/kits/hub-game/models/hats/lighthouse.pxmodel +96 -0
  195. package/kits/hub-game/models/hats/sailor.pxmodel +141 -0
  196. package/kits/hub-game/models/hats/souwester.pxmodel +131 -0
  197. package/kits/hub-game/models/hats/straw.pxmodel +139 -0
  198. package/kits/hub-game/models/hats/sunflower.pxmodel +406 -0
  199. package/kits/hub-game/models/hats/sweatband.pxmodel +112 -0
  200. package/kits/hub-game/models/hats/witch.pxmodel +147 -0
  201. package/kits/hub-game/package-lock.json +2 -0
  202. package/kits/hub-game/package.json +2 -1
  203. package/kits/hub-game/scripts/check.mjs +78 -11
  204. package/kits/hub-game/scripts/companion-follower-wire.json +1 -0
  205. package/kits/hub-game/scripts/fixtures/decor-keyless-draw.json +121 -0
  206. package/kits/hub-game/scripts/fixtures/decor-keyless.json +293 -0
  207. package/kits/hub-game/scripts/fixtures/engine-models-stub.mjs +23 -0
  208. package/kits/hub-game/scripts/fixtures/fake-dom.mjs +107 -0
  209. package/kits/hub-game/scripts/fixtures/interest-legacy.json +385 -0
  210. package/kits/hub-game/scripts/fixtures/npc-notes-v1.77.jsonl +4 -0
  211. package/kits/hub-game/scripts/fixtures/vis-shared-wire.json +1 -0
  212. package/kits/hub-game/scripts/gen-avatars.mjs +933 -16
  213. package/kits/hub-game/scripts/gen-earned.mjs +285 -0
  214. package/kits/hub-game/scripts/gen-hats.mjs +827 -0
  215. package/kits/hub-game/scripts/grab.before-orbit.mjs +400 -0
  216. package/kits/hub-game/scripts/soundscape-wave-analysis.mjs +125 -0
  217. package/kits/hub-game/scripts/test-actions-client.mjs +488 -0
  218. package/kits/hub-game/scripts/test-actions.mjs +298 -0
  219. package/kits/hub-game/scripts/test-actor-budget.mjs +44 -0
  220. package/kits/hub-game/scripts/test-actors.mjs +19 -17
  221. package/kits/hub-game/scripts/test-ask-client.mjs +358 -0
  222. package/kits/hub-game/scripts/test-ask.mjs +269 -0
  223. package/kits/hub-game/scripts/test-attention.mjs +10 -1
  224. package/kits/hub-game/scripts/test-avatar-cap.mjs +379 -0
  225. package/kits/hub-game/scripts/test-avatar-colors.mjs +72 -0
  226. package/kits/hub-game/scripts/test-avatar-models.mjs +181 -0
  227. package/kits/hub-game/scripts/test-avatar-share.mjs +318 -0
  228. package/kits/hub-game/scripts/test-avatar-wire.mjs +753 -0
  229. package/kits/hub-game/scripts/test-avatar.mjs +540 -7
  230. package/kits/hub-game/scripts/test-ballot.mjs +1001 -0
  231. package/kits/hub-game/scripts/test-build.mjs +31 -0
  232. package/kits/hub-game/scripts/test-cells.mjs +71 -0
  233. package/kits/hub-game/scripts/test-chat.mjs +694 -0
  234. package/kits/hub-game/scripts/test-classic.mjs +2 -0
  235. package/kits/hub-game/scripts/test-companion-chat.mjs +198 -0
  236. package/kits/hub-game/scripts/test-companion-client.mjs +1167 -0
  237. package/kits/hub-game/scripts/test-companion.mjs +259 -0
  238. package/kits/hub-game/scripts/test-decor-client.mjs +303 -0
  239. package/kits/hub-game/scripts/test-decor.mjs +457 -0
  240. package/kits/hub-game/scripts/test-earned-client.mjs +459 -0
  241. package/kits/hub-game/scripts/test-earned.mjs +1194 -0
  242. package/kits/hub-game/scripts/test-emote-bubble.mjs +110 -0
  243. package/kits/hub-game/scripts/test-friend-bests.mjs +435 -0
  244. package/kits/hub-game/scripts/test-grab-client.mjs +186 -3
  245. package/kits/hub-game/scripts/test-grab-orbit.mjs +918 -0
  246. package/kits/hub-game/scripts/test-grab.mjs +864 -3
  247. package/kits/hub-game/scripts/test-hats.mjs +483 -0
  248. package/kits/hub-game/scripts/test-hub-leaving.mjs +203 -0
  249. package/kits/hub-game/scripts/test-hub-ops.mjs +439 -2
  250. package/kits/hub-game/scripts/test-hub-trip.mjs +1228 -1
  251. package/kits/hub-game/scripts/test-hud-clear.mjs +1000 -0
  252. package/kits/hub-game/scripts/test-idle.mjs +361 -1
  253. package/kits/hub-game/scripts/test-interest-cap.mjs +36 -0
  254. package/kits/hub-game/scripts/test-interest-cells.mjs +493 -0
  255. package/kits/hub-game/scripts/test-interest-client.mjs +118 -0
  256. package/kits/hub-game/scripts/test-keys.mjs +46 -1
  257. package/kits/hub-game/scripts/test-labels.mjs +596 -0
  258. package/kits/hub-game/scripts/test-manifest.mjs +282 -0
  259. package/kits/hub-game/scripts/test-menu-client.mjs +480 -0
  260. package/kits/hub-game/scripts/test-menu.mjs +296 -0
  261. package/kits/hub-game/scripts/test-npcs.mjs +1718 -12
  262. package/kits/hub-game/scripts/test-object-entry.mjs +76 -0
  263. package/kits/hub-game/scripts/test-pack.mjs +7 -0
  264. package/kits/hub-game/scripts/test-party.mjs +22 -1
  265. package/kits/hub-game/scripts/test-picker.mjs +314 -0
  266. package/kits/hub-game/scripts/test-rehello.mjs +1039 -0
  267. package/kits/hub-game/scripts/test-relations-among.mjs +222 -0
  268. package/kits/hub-game/scripts/test-relations-refresh.mjs +188 -0
  269. package/kits/hub-game/scripts/test-say.mjs +59 -2
  270. package/kits/hub-game/scripts/test-shot.mjs +756 -0
  271. package/kits/hub-game/scripts/test-soundscape.mjs +5432 -0
  272. package/kits/hub-game/scripts/test-spin.mjs +314 -0
  273. package/kits/hub-game/scripts/test-standing.mjs +416 -0
  274. package/kits/hub-game/scripts/test-stuck.mjs +1903 -0
  275. package/kits/hub-game/scripts/test-tier-cells.mjs +706 -0
  276. package/kits/hub-game/scripts/test-tiers-render.mjs +339 -0
  277. package/kits/hub-game/scripts/test-tiers.mjs +666 -3
  278. package/kits/hub-game/scripts/test-toast-band.mjs +61 -0
  279. package/kits/hub-game/scripts/test-try-on.mjs +940 -0
  280. package/kits/hub-game/scripts/test-vis.mjs +343 -0
  281. package/kits/hub-game/scripts/test-visibility.mjs +134 -0
  282. package/kits/hub-game/scripts/test-yours-chrome.mjs +166 -0
  283. package/kits/hub-game/scripts/test-yours.mjs +372 -0
  284. package/kits/multiplayer-3d/CLAUDE.md +50 -0
  285. package/kits/multiplayer-3d/castle.json +5 -5
  286. package/kits/multiplayer-3d/code/client/avatars.js +7 -0
  287. package/kits/multiplayer-3d/code/client/gait.js +65 -0
  288. package/kits/multiplayer-3d/code/client/nameTags.js +59 -4
  289. package/kits/multiplayer-3d/code/options.js +5 -0
  290. package/kits/multiplayer-3d/code/server/ownership.js +10 -2
  291. package/kits/multiplayer-3d/code/server/sceneBodies.js +12 -0
  292. package/kits/multiplayer-3d/code/server/world.js +7 -0
  293. package/kits/multiplayer-3d/code/systems/multiplayer.js +74 -4
  294. package/kits/multiplayer-3d/package.json +2 -1
  295. package/kits/multiplayer-3d/scripts/fixtures/README.md +11 -0
  296. package/kits/multiplayer-3d/scripts/fixtures/ownership-legacy.json +2 -0
  297. package/kits/multiplayer-3d/scripts/ownership-harness.mjs +102 -0
  298. package/kits/multiplayer-3d/scripts/ownership-loader.mjs +46 -0
  299. package/kits/multiplayer-3d/scripts/ownership-worker.mjs +15 -0
  300. package/kits/multiplayer-3d/scripts/record-ownership-session.mjs +98 -0
  301. package/kits/multiplayer-3d/scripts/test-eligible-claims.mjs +167 -0
  302. package/kits/multiplayer-3d/scripts/test-own-name-tag.mjs +247 -0
  303. package/kits/physics-2d/CLAUDE.md +9 -0
  304. package/kits/physics-2d/castle.json +2 -2
  305. package/kits/physics-2d/engine/scene.js +288 -1
  306. package/kits/physics-2d/engine/skyKey.js +129 -0
  307. package/kits/physics-2d/engine/stillClock.js +106 -0
  308. package/kits/physics-2d/engine/updateBudget.js +21 -0
  309. package/kits/physics-2d/scripts/test-skip-unchanged.mjs +370 -1
  310. package/kits/physics-3d/CLAUDE.md +1391 -0
  311. package/kits/physics-3d/behaviors/Player.jsx +921 -3
  312. package/kits/physics-3d/castle.json +3 -3
  313. package/kits/physics-3d/docs/pxmodel-format.md +12 -0
  314. package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +98 -10
  315. package/kits/physics-3d/engine3d/actornode.js +86 -0
  316. package/kits/physics-3d/engine3d/animationBudget.js +23 -0
  317. package/kits/physics-3d/engine3d/editorWorld.js +6 -0
  318. package/kits/physics-3d/engine3d/emptynodes.js +232 -0
  319. package/kits/physics-3d/engine3d/framecap.js +96 -0
  320. package/kits/physics-3d/engine3d/gait.js +118 -0
  321. package/kits/physics-3d/engine3d/kithooks.js +47 -0
  322. package/kits/physics-3d/engine3d/look.js +507 -48
  323. package/kits/physics-3d/engine3d/lookbake.js +208 -0
  324. package/kits/physics-3d/engine3d/materialpresets.js +2 -0
  325. package/kits/physics-3d/engine3d/motion.js +39 -10
  326. package/kits/physics-3d/engine3d/nearshadows.js +37 -19
  327. package/kits/physics-3d/engine3d/optimize.js +58 -5
  328. package/kits/physics-3d/engine3d/orbitinput.js +755 -0
  329. package/kits/physics-3d/engine3d/orbitlens.js +48 -0
  330. package/kits/physics-3d/engine3d/ownmodel.js +213 -0
  331. package/kits/physics-3d/engine3d/pxmodel.js +42 -11
  332. package/kits/physics-3d/engine3d/shadowfade.js +882 -0
  333. package/kits/physics-3d/engine3d/sky.js +547 -0
  334. package/kits/physics-3d/engine3d/skylife.js +306 -0
  335. package/kits/physics-3d/engine3d/unchanged.js +11 -0
  336. package/kits/physics-3d/engine3d/viewrange.js +52 -0
  337. package/kits/physics-3d/engine3d/world3d.js +870 -87
  338. package/kits/physics-3d/main.jsx +5 -0
  339. package/kits/physics-3d/package.json +1 -1
  340. package/kits/physics-3d/scripts/Player.before-orbit.mjs +310 -0
  341. package/kits/physics-3d/scripts/Scene3DPlayer.before-orbit.jsx +337 -0
  342. package/kits/physics-3d/scripts/look-baseline.mjs +155 -0
  343. package/kits/physics-3d/scripts/test-camera-follow.mjs +12 -2
  344. package/kits/physics-3d/scripts/test-camera-orbit.mjs +4588 -0
  345. package/kits/physics-3d/scripts/test-drop-empty-parts.mjs +787 -0
  346. package/kits/physics-3d/scripts/test-frame-cap-bob.mjs +435 -0
  347. package/kits/physics-3d/scripts/test-frame-cap.mjs +452 -0
  348. package/kits/physics-3d/scripts/test-gait.mjs +387 -0
  349. package/kits/physics-3d/scripts/test-look-bake.mjs +1056 -0
  350. package/kits/physics-3d/scripts/test-model-parse-cache.mjs +92 -0
  351. package/kits/physics-3d/scripts/test-near-shadows.mjs +3 -0
  352. package/kits/physics-3d/scripts/test-orbit-shadow-fade.mjs +1920 -0
  353. package/kits/physics-3d/scripts/test-orbit-shift-lock.mjs +545 -0
  354. package/kits/physics-3d/scripts/test-orbit-shot.mjs +583 -0
  355. package/kits/physics-3d/scripts/test-orbit-view.mjs +230 -0
  356. package/kits/physics-3d/scripts/test-skip-empty-nodes.mjs +845 -0
  357. package/kits/physics-3d/scripts/test-sky.mjs +2470 -0
  358. package/kits/physics-3d/scripts/test-static-actors.mjs +13 -1
  359. package/kits/physics-3d/scripts/test-static-passes.mjs +1 -1
  360. package/kits/physics-3d/scripts/test-static-scenery.mjs +2171 -0
  361. package/kits/physics-3d/scripts/test-steady-hover.mjs +635 -0
  362. package/kits/physics-3d/scripts/test-view-range.mjs +260 -0
  363. package/kits/physics-3d/systems/physics3d.js +474 -12
  364. package/kits/real-time/castle.json +2 -2
  365. package/kits/real-time/code/server/gameHooks.js +7 -0
  366. package/kits/real-time/code/server/session.js +97 -19
  367. package/package.json +2 -2
  368. package/dist/shell/assets/index-Cf61cNfT.css +0 -1
@@ -0,0 +1,400 @@
1
+ // Grabbing and throwing (contract §6, v1.3), the client's half: a Tier 0 game
2
+ // has no client code, so the hub kit offers the input, holds the object and
3
+ // lets it go. Only in a place whose manifest says `grab`.
4
+ //
5
+ // What can be grabbed is what castle.multiplayer-3d already lets this client
6
+ // simulate: a loose scene object (`claimable`) it owns (`own.mine`), because
7
+ // the player is the nearest one in reach. A held object is put in front of the
8
+ // player every frame, so it stays in reach and this client stays its owner;
9
+ // the multiplayer kit reports its pose as it reports any owned object, and the
10
+ // server bounds those reports as before. A throw is the velocity it is let go
11
+ // with. The place's server hears each grab and throw (`hub-grab`) and may
12
+ // refuse a grab or end a hold (`hub-grab-deny`, `hub-drop`).
13
+ //
14
+ // A place may ask for a steadier offer (v1.22, `grab.reach` and
15
+ // `grab.steady`): a longer reach, and an offer that does not flicker at its
16
+ // edges. A place that asks for neither runs exactly the code it always did.
17
+ //
18
+ // A place may keep an object for one player (v1.27, `hub.keepFor`): it is
19
+ // never offered to anyone else, so the nearest object a player may take is
20
+ // the one offered. Where nothing is kept the offer is exactly what it was.
21
+
22
+ import { hub } from './state.js';
23
+
24
+ const DEG = Math.PI / 180;
25
+ // An object is offered when its near side is this close to the player
26
+ // (flat metres), within 60 degrees of where they face, and not far above or
27
+ // below them.
28
+ const REACH_M = 1.6;
29
+ const FRONT_COS = 0.5;
30
+ const RISE_M = 2;
31
+ // Objects wider than this (metres from centre to edge) are not offered.
32
+ const MAX_EXTENT = 2;
33
+ // How fast a held object follows its hold point: a pickup eases in rather
34
+ // than jumping, so each owner report stays far inside the server's limit on
35
+ // how far an owned object may move between reports.
36
+ const CARRY_SPEED = 20;
37
+ // A throw's upward part, as a share of its speed.
38
+ const THROW_UP = 0.25;
39
+ // After a throw, nothing is offered for this long: the object is still in
40
+ // front of the player as it leaves.
41
+ const REGRAB_MS = 400;
42
+ // v1.22, only where the place sets `grab.reach` or `grab.steady`: the centre
43
+ // of an offered object is never farther than this from the player's centre.
44
+ // The server refuses a grab past its GRAB_REACH (4 m, from the poses it has,
45
+ // which trail a moving player's: a grab can be heard before that frame's
46
+ // movement report). With `reach` or `steady`, the client keeps inside it by
47
+ // what the player may travel in that time at the speed they are moving now
48
+ // (their body's own velocity, so an authored speed, a tune, a jump and a tune
49
+ // not yet applied are all counted) or the speed they are tuned to, whichever
50
+ // is more. It is a margin, not a promise: a grab the server still refuses is
51
+ // let go, as any refused grab is (`hub-grab-deny`).
52
+ export const OFFER_CENTRE_M = 3;
53
+ const WALK_SPEED = 6; // the walk preset's speed, metres a second
54
+ const POSE_LAG_S = 0.2;
55
+ const LAG_SLACK_M = 0.25;
56
+ // `speed` is the controller's own (the kit's applyTune has already multiplied
57
+ // it by the tune); without one, the walk preset's times the tune.
58
+ export function offerCap(velocity = null, tune = hub.tune, speed = null) {
59
+ const moving = velocity ? Math.hypot(velocity.x ?? 0, velocity.y ?? 0, velocity.z ?? 0) : 0;
60
+ const tuned = Number(speed) > 0 ? Number(speed) : WALK_SPEED * Math.max(1, Number(tune?.speed) || 1);
61
+ return Math.max(0, Math.min(OFFER_CENTRE_M, 4 - LAG_SLACK_M - Math.max(moving, tuned) * POSE_LAG_S));
62
+ }
63
+ // The cap for this player now: their body's velocity and authored speed.
64
+ function capFor(scene, local) {
65
+ const body = scene.physics3d?.bodyOf?.(local.id);
66
+ return offerCap(body?.linvel?.() ?? null, hub.tune, local.components?.Player?.speed);
67
+ }
68
+ // v1.22 `grab.steady`: once an object is offered it stays the one offered
69
+ // while its near side is within the reach and this much more, and within 90
70
+ // degrees of facing. Through a moment where it is not (its owner briefly
71
+ // someone else, a turn past the cone, a step back), the button stays up for
72
+ // STEADY_GRACE_MS; a press then waits up to PRESS_WAIT_MS for the object to
73
+ // be this client's again, and is dropped if it never is.
74
+ export const KEEP_MARGIN_M = 0.4;
75
+ const KEEP_COS = 0;
76
+ export const STEADY_GRACE_MS = 600;
77
+ export const PRESS_WAIT_MS = 500;
78
+ // v1.25 `grab.keepHeld`: how often the server is told a hold goes on (its
79
+ // pin on the held object lasts 2.5 s past the last word).
80
+ export const KEEP_TELL_MS = 1000;
81
+ // Where the walk preset's capsule stands (physics-3d `Player`): its radius and
82
+ // height, and how far it hovers.
83
+ const PLAYER = { radius: 0.35, height: 1.4, hover: 0.15 };
84
+
85
+ export function makeGrab(chrome) {
86
+ const t = {
87
+ chrome,
88
+ held: null,
89
+ net: null,
90
+ pressed: false,
91
+ quietUntil: 0,
92
+ lastAt: 0,
93
+ // v1.22 `steady` only: the object kept on offer `{ id, okAt }`, and a press
94
+ // waiting for it `{ id, until }`.
95
+ offer: null,
96
+ waiting: null,
97
+ };
98
+ chrome.onGrab?.(() => {
99
+ t.pressed = true;
100
+ });
101
+ // E or F on a keyboard. Typing in the talk panel never reaches here (its
102
+ // input stops its keys), nor does any other text field.
103
+ const onKey = (event) => {
104
+ if (event.repeat || (event.code !== 'KeyE' && event.code !== 'KeyF')) {
105
+ return;
106
+ }
107
+ const target = event.target;
108
+ if (
109
+ target?.isContentEditable ||
110
+ target?.tagName === 'INPUT' ||
111
+ target?.tagName === 'TEXTAREA'
112
+ ) {
113
+ return;
114
+ }
115
+ t.pressed = true;
116
+ };
117
+ globalThis.addEventListener?.('keydown', onKey);
118
+ return {
119
+ frame: (scene, mp, local, idle) => frame(t, scene, mp, local, idle),
120
+ holding: () => t.held?.id ?? null,
121
+ dispose: () => globalThis.removeEventListener?.('keydown', onKey),
122
+ };
123
+ }
124
+
125
+ function frame(t, scene, mp, local, idle) {
126
+ const now = performance.now();
127
+ const dt = Math.min(0.1, Math.max(0, (now - (t.lastAt || now)) / 1000));
128
+ t.lastAt = now;
129
+ // A press counts in the frame it lands in, and only for what is offered then.
130
+ const pressed = t.pressed;
131
+ t.pressed = false;
132
+ const events = hub.grabEvents.splice(0);
133
+ const net = mp.net ?? null;
134
+ if (net !== t.net) {
135
+ // A new session: whatever was held belonged to the last one.
136
+ t.net = net;
137
+ t.held = null;
138
+ t.offer = null;
139
+ t.waiting = null;
140
+ }
141
+ const config = hub.place?.grab ?? null;
142
+ const phys = scene.physics3d;
143
+ const self = net?.selfId?.();
144
+ const can = Boolean(
145
+ config && idle && net && local?.components.Transform && phys?.ready?.() && !hub.held.has(self),
146
+ );
147
+
148
+ if (t.held) {
149
+ const id = t.held.id;
150
+ const body = phys?.bodyOf?.(id) ?? null;
151
+ const told = events.some((e) => e.k === 'hub-drop' || e.actor === id);
152
+ // Holding ends when the place says so, when the multiplayer kit hands the
153
+ // object to someone else or back to the server, when the place hides it,
154
+ // or when the player cannot act (a trip, held still, a game's end).
155
+ if (
156
+ !can ||
157
+ told ||
158
+ !mp.own?.mine?.has(id) ||
159
+ hub.hidden.has(id) ||
160
+ !body ||
161
+ !scene.getActor(id)
162
+ ) {
163
+ t.held = null;
164
+ } else if (pressed) {
165
+ throwHeld(t, scene, body, local, config, net, now);
166
+ } else {
167
+ carry(t, scene, body, local, config, dt);
168
+ if (config.keepHeld === true) {
169
+ // v1.25: the server keeps a held object this client's only while it
170
+ // hears the hold goes on.
171
+ t.held.toldAt ??= now;
172
+ if (now - t.held.toldAt >= KEEP_TELL_MS) {
173
+ t.held.toldAt = now;
174
+ net.send({ k: 'hub-grab', actor: id, op: 'hold' });
175
+ }
176
+ }
177
+ }
178
+ }
179
+ if (t.held) {
180
+ t.offer = null;
181
+ t.waiting = null;
182
+ t.chrome.grabPrompt?.('Throw');
183
+ return;
184
+ }
185
+ if (config?.steady === true) {
186
+ steadyFrame(t, scene, mp, local, config, net, can && now >= t.quietUntil, pressed, now, dt);
187
+ return;
188
+ }
189
+ const reach = config?.reach;
190
+ const target =
191
+ can && now >= t.quietUntil
192
+ ? reach === undefined
193
+ ? grabbable(scene, mp, local)
194
+ : grabbable(scene, mp, local, reach, FRONT_COS, capFor(scene, local))
195
+ : null;
196
+ if (target && pressed) {
197
+ const at = phys.bodyOf(target.id).translation();
198
+ t.held = {
199
+ id: target.id,
200
+ extent: target.extent,
201
+ half: target.half,
202
+ at: { x: at.x, y: at.y, z: at.z },
203
+ };
204
+ net.send({ k: 'hub-grab', actor: target.id, op: 'grab' });
205
+ carry(t, scene, phys.bodyOf(target.id), local, config, dt);
206
+ t.chrome.grabPrompt?.('Throw');
207
+ return;
208
+ }
209
+ t.chrome.grabPrompt?.(target ? 'Grab' : null);
210
+ }
211
+
212
+ // v1.22 `grab.steady`: the offer with hysteresis. Only an object that passes
213
+ // the full test this frame -- this client's own, in reach, in front -- is ever
214
+ // grabbed; the grace only keeps the button up and a press waiting.
215
+ function steadyFrame(t, scene, mp, local, config, net, active, pressed, now, dt) {
216
+ if (!active) {
217
+ t.offer = null;
218
+ t.waiting = null;
219
+ t.chrome.grabPrompt?.(null);
220
+ return;
221
+ }
222
+ const reach = config.reach ?? REACH_M;
223
+ const kept = t.offer;
224
+ // The kept object holds the offer on the wider test; when it fails this
225
+ // frame (or there is none), one passing the ordinary test takes over.
226
+ const ready =
227
+ (kept &&
228
+ grabbable(scene, mp, local, reach + KEEP_MARGIN_M, KEEP_COS, capFor(scene, local), kept.id)) ||
229
+ grabbable(scene, mp, local, reach, FRONT_COS, capFor(scene, local));
230
+ if (ready) {
231
+ t.offer = { id: ready.id, okAt: now };
232
+ } else if (kept && (now - kept.okAt > STEADY_GRACE_MS || !stillThere(scene, kept.id, mp))) {
233
+ t.offer = null;
234
+ }
235
+ if (t.waiting && (now > t.waiting.until || !stillThere(scene, t.waiting.id, mp))) {
236
+ t.waiting = null;
237
+ }
238
+ if (pressed && (ready || t.offer)) {
239
+ const id = ready?.id ?? t.offer.id;
240
+ t.waiting = { id, until: now + PRESS_WAIT_MS };
241
+ }
242
+ if (!ready || t.waiting?.id !== ready.id) {
243
+ t.chrome.grabPrompt?.(t.offer || t.waiting ? 'Grab' : null);
244
+ return;
245
+ }
246
+ t.waiting = null;
247
+ t.offer = null;
248
+ const body = scene.physics3d.bodyOf(ready.id);
249
+ const at = body.translation();
250
+ t.held = {
251
+ id: ready.id,
252
+ extent: ready.extent,
253
+ half: ready.half,
254
+ at: { x: at.x, y: at.y, z: at.z },
255
+ };
256
+ net.send({ k: 'hub-grab', actor: ready.id, op: 'grab' });
257
+ carry(t, scene, body, local, config, dt);
258
+ t.chrome.grabPrompt?.('Throw');
259
+ }
260
+
261
+ // Whether an object kept on offer can still come back: in the scene, not
262
+ // hidden by the place, not kept for someone else (v1.27). Anything else may
263
+ // change within a grace.
264
+ function stillThere(scene, id, mp) {
265
+ return Boolean(scene.getActor(id)) && !hub.hidden.has(id) && !keptFromMe(mp, id);
266
+ }
267
+
268
+ // v1.27 `hub.keepFor`: whether the place keeps this object for another player.
269
+ // False for every object where nothing is kept.
270
+ function keptFromMe(mp, id) {
271
+ return hub.keptFor.size > 0 && hub.keptFor.has(id) && hub.keptFor.get(id) !== mp.net?.selfId?.();
272
+ }
273
+
274
+ // The nearest object this client owns in front of the player, or null; one the
275
+ // place keeps for someone else (v1.27) is passed over for the next nearest. With
276
+ // no more arguments it is the test every place had before v1.22; `reach`,
277
+ // `cos` and `centre` (the farthest an object's centre may be from the
278
+ // player's) carry a place's `grab.reach` and `grab.steady`, and `only` limits
279
+ // the test to one object.
280
+ function grabbable(
281
+ scene,
282
+ mp,
283
+ local,
284
+ reach = REACH_M,
285
+ cos = FRONT_COS,
286
+ centre = Infinity,
287
+ only = null,
288
+ ) {
289
+ const mine = mp.own?.mine;
290
+ if (!mine?.size || typeof mp.claimable !== 'function') {
291
+ return null;
292
+ }
293
+ const me = local.components.Transform;
294
+ const f = (me.rotationY ?? 0) * DEG;
295
+ const fx = Math.sin(f);
296
+ const fz = Math.cos(f);
297
+ let best = null;
298
+ for (const [id, at] of mp.claimable(scene)) {
299
+ if ((only !== null && id !== only) || !mine.has(id) || hub.hidden.has(id) || keptFromMe(mp, id)) {
300
+ continue;
301
+ }
302
+ const actor = scene.getActor(id);
303
+ if (actor?.components.Body?.type !== 'dynamic' || !scene.physics3d.bodyOf(id)?.isDynamic?.()) {
304
+ continue;
305
+ }
306
+ const size = sizeOf(actor.components.Shape);
307
+ const dx = at.x - me.x;
308
+ const dz = at.z - me.z;
309
+ const d = Math.hypot(dx, dz);
310
+ if (size.extent > MAX_EXTENT || d - size.extent > reach || Math.abs(at.y - me.y) > RISE_M) {
311
+ continue;
312
+ }
313
+ if (d > 0.01 && (dx * fx + dz * fz) / d < cos) {
314
+ continue;
315
+ }
316
+ if (Math.hypot(d, at.y - me.y) > centre) {
317
+ continue;
318
+ }
319
+ if (!best || d < best.d) {
320
+ best = { id, d, ...size };
321
+ }
322
+ }
323
+ return best;
324
+ }
325
+
326
+ // How far an object reaches out from its centre across the ground, and up.
327
+ function sizeOf(shape) {
328
+ const kind = shape?.kind ?? 'box';
329
+ const radius = typeof shape?.radius === 'number' ? shape.radius : 0.5;
330
+ const w = typeof shape?.width === 'number' ? shape.width : 1;
331
+ const h = typeof shape?.height === 'number' ? shape.height : 1;
332
+ const d = typeof shape?.depth === 'number' ? shape.depth : 1;
333
+ if (kind === 'sphere') {
334
+ return { extent: radius, half: radius };
335
+ }
336
+ if (kind === 'cylinder' || kind === 'capsule') {
337
+ return { extent: radius, half: kind === 'capsule' ? h / 2 + radius : h / 2 };
338
+ }
339
+ return { extent: Math.hypot(w, d) / 2, half: h / 2 };
340
+ }
341
+
342
+ // Where a held object goes: `holdDistance` in front of the player (never so
343
+ // close that it touches them) and `holdHeight` above their feet (never so low
344
+ // that it is in the ground).
345
+ function holdPoint(local, config, held) {
346
+ const t = local.components.Transform;
347
+ const shape = local.components.Shape;
348
+ const radius = typeof shape?.radius === 'number' ? shape.radius : PLAYER.radius;
349
+ const height = typeof shape?.height === 'number' ? shape.height : PLAYER.height;
350
+ const feet = t.y - (height / 2 + PLAYER.hover);
351
+ const f = (t.rotationY ?? 0) * DEG;
352
+ const distance = Math.max(config.holdDistance, radius + held.extent + 0.15);
353
+ return {
354
+ x: t.x + Math.sin(f) * distance,
355
+ y: feet + Math.max(config.holdHeight, held.half + 0.05),
356
+ z: t.z + Math.cos(f) * distance,
357
+ };
358
+ }
359
+
360
+ // Move the held object toward its hold point, at most CARRY_SPEED. It keeps
361
+ // the velocity it is carried at, which the multiplayer kit reports with its
362
+ // pose, so other players draw it moving smoothly.
363
+ function carry(t, scene, body, local, config, dt) {
364
+ const held = t.held;
365
+ const to = holdPoint(local, config, held);
366
+ const from = held.at;
367
+ const dx = to.x - from.x;
368
+ const dy = to.y - from.y;
369
+ const dz = to.z - from.z;
370
+ const d = Math.hypot(dx, dy, dz);
371
+ const most = CARRY_SPEED * dt;
372
+ const k = d > most && d > 0 ? most / d : 1;
373
+ const next = { x: from.x + dx * k, y: from.y + dy * k, z: from.z + dz * k };
374
+ body.setTranslation(next, true);
375
+ body.setLinvel(
376
+ dt > 0
377
+ ? { x: (next.x - from.x) / dt, y: (next.y - from.y) / dt, z: (next.z - from.z) / dt }
378
+ : { x: 0, y: 0, z: 0 },
379
+ true,
380
+ );
381
+ body.setAngvel({ x: 0, y: 0, z: 0 }, true);
382
+ held.at = next;
383
+ // Drawn where it is this frame; physics writes the Transform after its step.
384
+ const transform = scene.getActor(held.id)?.components.Transform;
385
+ if (transform) {
386
+ transform.x = next.x;
387
+ transform.y = next.y;
388
+ transform.z = next.z;
389
+ }
390
+ }
391
+
392
+ // Let go along the player's facing at `throwSpeed`, a little upward.
393
+ function throwHeld(t, scene, body, local, config, net, now) {
394
+ const f = (local.components.Transform.rotationY ?? 0) * DEG;
395
+ const speed = config.throwSpeed;
396
+ body.setLinvel({ x: Math.sin(f) * speed, y: speed * THROW_UP, z: Math.cos(f) * speed }, true);
397
+ net.send({ k: 'hub-grab', actor: t.held.id, op: 'throw' });
398
+ t.held = null;
399
+ t.quietUntil = now + REGRAB_MS;
400
+ }
@@ -0,0 +1,125 @@
1
+ // Offline Harbour analysis without a browser. The recorded WebAudio graph is
2
+ // evaluated at 44.1 kHz: seeded PCM, AudioParam envelopes, band-limited tones
3
+ // and RBJ biquads (WebAudio's low/high-pass Q is in dB). Filter coefficients
4
+ // update every 32 samples during sweeps. Active RMS uses the audition's 2048
5
+ // sample windows within 20 dB of the loudest. This is a calibration model,
6
+ // not an implementation of the whole WebAudio API.
7
+ function envelope(param) {
8
+ const events = param.events.filter(([op]) => op !== 'cancelScheduledValues').sort((a, b) => a[2] - b[2]);
9
+ let i = 0;
10
+ let at = 0;
11
+ let value = param.value;
12
+ let target = null;
13
+ const held = (t) => target ? target[0] + (value - target[0]) * Math.exp(-(t - at) / target[1]) : value;
14
+ return (t) => {
15
+ while (i < events.length && events[i][2] <= t) {
16
+ const [op, v, time, tc] = events[i++];
17
+ value = op === 'setTargetAtTime' ? held(time) : v;
18
+ at = time;
19
+ target = op === 'setTargetAtTime' ? [v, tc] : null;
20
+ }
21
+ const e = events[i];
22
+ if (e?.[0] === 'linearRampToValueAtTime' || e?.[0] === 'exponentialRampToValueAtTime') {
23
+ const u = Math.max(0, Math.min(1, (t - at) / (e[2] - at)));
24
+ return e[0] === 'linearRampToValueAtTime' ? value + (e[1] - value) * u : value * (e[1] / value) ** u;
25
+ }
26
+ return held(t);
27
+ };
28
+ }
29
+
30
+ function biquad(type, hz, q, sr) {
31
+ const w = 2 * Math.PI * hz / sr;
32
+ const c = Math.cos(w);
33
+ const a = Math.sin(w) / (2 * (type === 'bandpass' ? q : 10 ** (q / 20)));
34
+ const b = type === 'bandpass' ? [a, 0, -a] : type === 'lowpass' ? [(1 - c) / 2, 1 - c, (1 - c) / 2] : [(1 + c) / 2, -(1 + c), (1 + c) / 2];
35
+ return [...b.map((v) => v / (1 + a)), -2 * c / (1 + a), (1 - a) / (1 + a)];
36
+ }
37
+
38
+ function tone(wave) {
39
+ const table = new Float64Array(4096);
40
+ if (!wave) {
41
+ for (let i = 0; i < table.length; i += 1) { table[i] = Math.sin(2 * Math.PI * i / table.length); }
42
+ } else {
43
+ for (let i = 0; i < table.length; i += 1) {
44
+ for (let h = 1; h < wave.real.length; h += 1) {
45
+ const a = 2 * Math.PI * i * h / table.length;
46
+ table[i] += wave.real[h] * Math.cos(a) + wave.imag[h] * Math.sin(a);
47
+ }
48
+ }
49
+ const peak = Math.max(...table.map(Math.abs));
50
+ for (let i = 0; i < table.length; i += 1) { table[i] /= peak; }
51
+ }
52
+ return table;
53
+ }
54
+
55
+ export function analyseHarbour(ctx, seconds = 300) {
56
+ const sr = ctx.sampleRate;
57
+ const count = Math.round(sr * seconds);
58
+ const ordered = [];
59
+ const seen = new Set();
60
+ function visit(node) {
61
+ if (seen.has(node)) { return; }
62
+ seen.add(node);
63
+ for (const input of ctx.nodes.filter((n) => n.to.includes(node))) { visit(input); }
64
+ ordered.push(node);
65
+ }
66
+ visit(ctx.destination);
67
+ const states = new Map(ordered.map((node) => [node, {
68
+ input: ctx.nodes.filter((n) => n.to.includes(node)), value: 0,
69
+ env: node.gain ? envelope(node.gain) : null,
70
+ frequency: node.frequency ? envelope(node.frequency) : null,
71
+ phase: 0, table: node.kind === 'osc' ? tone(node.wave) : null,
72
+ coeff: null, x1: 0, x2: 0, y1: 0, y2: 0,
73
+ }]));
74
+ const samples = new Float32Array(count);
75
+ let peak = 0;
76
+ for (let i = 0; i < count; i += 1) {
77
+ const t = i / sr;
78
+ for (const node of ordered) {
79
+ const s = states.get(node);
80
+ let x = s.input.reduce((sum, n) => sum + states.get(n).value, 0);
81
+ if (node.kind === 'source') {
82
+ const data = node.buffer.getChannelData(0);
83
+ const offset = Math.floor((node.started?.[1] ?? 0) * sr);
84
+ x = data[(i + offset) % data.length];
85
+ } else if (node.kind === 'osc') {
86
+ s.phase = (s.phase + s.frequency(t) / sr) % 1;
87
+ const pos = s.phase * s.table.length;
88
+ const k = Math.floor(pos);
89
+ x = s.table[k] + (s.table[(k + 1) % s.table.length] - s.table[k]) * (pos - k);
90
+ } else if (node.kind === 'gain') {
91
+ x *= s.env(t);
92
+ } else if (node.kind === 'filter') {
93
+ if (i % 32 === 0) { s.coeff = biquad(node.type, s.frequency(t), node.Q.value, sr); }
94
+ const [b0, b1, b2, a1, a2] = s.coeff;
95
+ const y = b0 * x + b1 * s.x1 + b2 * s.x2 - a1 * s.y1 - a2 * s.y2;
96
+ s.x2 = s.x1; s.x1 = x; s.y2 = s.y1; s.y1 = y; x = y;
97
+ }
98
+ s.value = x;
99
+ }
100
+ samples[i] = states.get(ctx.destination).value;
101
+ peak = Math.max(peak, Math.abs(samples[i]));
102
+ }
103
+ const windows = [];
104
+ for (let i = 0; i + 2048 <= count; i += 2048) {
105
+ let sum = 0;
106
+ for (let k = i; k < i + 2048; k += 1) { sum += samples[k] ** 2; }
107
+ windows.push(sum / 2048);
108
+ }
109
+ const top = Math.max(...windows);
110
+ const active = windows.filter((v) => v >= top * 0.01);
111
+ // Report each alternating channel's peak over the requested duration.
112
+ // Body/foam envelope sums are independent of noise phase; differing
113
+ // random sizes still give differing peaks despite identical channel gains.
114
+ const bodies = ctx.nodes.filter((n) => n.kind === 'filter' && n.type === 'lowpass' && n.frequency.value === 300);
115
+ const peaks = bodies.map((body) => {
116
+ const src = ctx.nodes.find((n) => n.to.includes(body));
117
+ const foam = src.to.find((n) => n.type === 'highpass');
118
+ const bodyAt = envelope(body.to[0].gain);
119
+ const foamAt = envelope(foam.to[0].gain);
120
+ let max = 0;
121
+ for (let i = 0; i < count; i += 1) { max = Math.max(max, bodyAt(i / sr) + foamAt(i / sr)); }
122
+ return max;
123
+ });
124
+ return { peak, rms: Math.sqrt(active.reduce((a, b) => a + b, 0) / active.length), envelopePeaks: peaks };
125
+ }