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
@@ -1,6 +1,6 @@
1
1
  // Talking to a place's NPCs (contract §13), drawn by the hub chrome because a
2
2
  // Tier 0 game has no client code of its own. Near a `HubNpc` actor the player
3
- // gets a Talk button; the place's server decides whether the conversation
3
+ // gets a Chat or Talk button; the place's server decides whether the conversation
4
4
  // opens (one player per NPC at a time) and whether it goes to the character's
5
5
  // mind or to the place's authored lines.
6
6
  //
@@ -23,6 +23,12 @@ const POLL_LIMIT_MS = 90_000;
23
23
  const INPUT_MAX = 280;
24
24
  // A turn refused for stale notes is sent again once, with the newest.
25
25
  const NOTES_RETRY_MS = 400;
26
+ // The server silently drops authored words inside its 900 ms cooldown.
27
+ const AUTHORED_GAP_MS = 900;
28
+ const AUTHORED_WAIT_MS = 1200;
29
+ const ATTACH_WAIT_MS = 8000;
30
+ const INTERRUPTED_MS = 1800;
31
+ const INTERRUPTED_LINE = 'Lost the thread there. Tap to talk again.';
26
32
  // Lines the place said because the mind was not answering, and how the panel
27
33
  // says so.
28
34
  const SCRIPTED = {
@@ -30,6 +36,12 @@ const SCRIPTED = {
30
36
  budget: 'has a crowd to answer',
31
37
  unavailable: "can't answer right now",
32
38
  };
39
+ const REFUSAL_WHY = {
40
+ HUB_NPC_BUSY: 'busy',
41
+ HUB_NPC_BUDGET: 'budget',
42
+ CAULDRON_SHARED_CHARACTER_BUSY: 'busy',
43
+ CAULDRON_SHARED_CHARACTER_BUDGET: 'budget',
44
+ };
33
45
 
34
46
  // The server strips a reply's wrapping quotes the same way for its bubble.
35
47
  const unquote = (text) => {
@@ -42,13 +54,23 @@ const newId = () => globalThis.crypto?.randomUUID?.() ?? `${Date.now().toString(
42
54
  export function makeTalk(chrome) {
43
55
  const t = {
44
56
  chrome,
45
- // { npc, name, mind, character, prefix } while a panel is open
57
+ // { npc, name, mind, character, prefix, canType, questions, sent } while open
46
58
  open: null,
47
59
  asking: null,
48
60
  net: null,
49
- // the nets whose participant this player has registered with an inspect read
61
+ // v1.46: only a successful inspect remembers permission to type.
50
62
  inspected: new WeakSet(),
63
+ // v1.47: approach starts one read; refusals still retry on the next open.
64
+ approached: new WeakSet(),
65
+ inspecting: new WeakSet(),
66
+ inspectRefusals: new Set(),
67
+ // v1.76: ids only, consumed on approach or on opening that character.
68
+ hooks: new Map(),
69
+ greeted: new Set(),
51
70
  waiting: false,
71
+ saidAt: -Infinity,
72
+ drafts: new Map(),
73
+ abandonedOpens: new Set(),
52
74
  };
53
75
  chrome.onTalk({
54
76
  open: () => ask(t),
@@ -57,32 +79,155 @@ export function makeTalk(chrome) {
57
79
  });
58
80
  return {
59
81
  frame: (scene, mp, idle) => frame(t, scene, mp, idle),
82
+ openCompanion(scene) {
83
+ const companion = companionNpc(scene);
84
+ if (companion && !t.open && t.net === scene.multiplayer?.net) {
85
+ ask(t, companion);
86
+ }
87
+ },
60
88
  typing: () => t.open !== null && chrome.talkFocused(),
61
89
  talking: () => t.open !== null,
90
+ talkingToCompanion: () => Boolean(hub.place?.companionChat && t.open?.npc === `companion:${t.net?.selfId?.()}`),
62
91
  // The NPC this player is talking with: its words are in the panel.
63
92
  talkingWith: () => t.open?.npc ?? null,
93
+ // A second WORLD under the same id restores the body, not its chat.
94
+ // Never replay a turn whose admission is unknown: only NOT_TALKING
95
+ // proves the server did not send it to the mind.
96
+ resume() {
97
+ if (t.open && !t.open.interrupted) {
98
+ t.open.needsAttach = true;
99
+ if (!t.waiting) {
100
+ void reattach(t, t.open);
101
+ }
102
+ }
103
+ },
104
+ // The same connection under another player id is another session, and the
105
+ // new one has no conversation with this player: words typed into the old
106
+ // panel would be refused there and never answered.
107
+ reset: () => reset(t),
64
108
  };
65
109
  }
66
110
 
111
+ // Whatever was being said belongs to the session it was said in.
112
+ function reset(t) {
113
+ clearAuthored(t.open);
114
+ if (t.open?.attachment) {
115
+ t.abandonedOpens.add(t.open.npc);
116
+ }
117
+ cancelAttach(t.open);
118
+ cancelNotesWait(t.open);
119
+ clearTimeout(t.open?.closeTimer);
120
+ clearTimeout(t.open?.interruptionTimer);
121
+ t.open = null;
122
+ t.asking = null;
123
+ t.waiting = false;
124
+ t.saidAt = -Infinity;
125
+ t.hooks.clear();
126
+ t.greeted.clear();
127
+ t.drafts.clear();
128
+ t.near = null;
129
+ t.preferred = null;
130
+ t.chrome.closeTalk();
131
+ }
132
+
67
133
  function frame(t, scene, mp, idle) {
68
134
  const net = mp.net;
69
135
  if (net !== t.net) {
70
136
  // A session switch ends whatever was being said in the old one.
71
137
  t.net = net;
72
- t.open = null;
73
- t.asking = null;
74
- t.waiting = false;
75
- t.chrome.closeTalk();
138
+ reset(t);
76
139
  }
140
+ // The server sends panel events only to their speaker. A body may have
141
+ // other conversations; its ambient labels never open or close this panel.
77
142
  for (const msg of hub.npcEvents.splice(0)) {
78
- if (msg.k === 'hub-npc') {
143
+ if (msg.k === 'hub-npc-preferred') {
144
+ t.preferred = typeof msg.npc === 'string' ? msg.npc : null;
145
+ } else if (msg.k === 'hub-npc-hooks') {
146
+ for (const hook of Array.isArray(msg.hooks) ? msg.hooks : []) {
147
+ if (typeof hook?.npc === 'string' && typeof hook.character === 'string' && hook.character && !t.greeted.has(hook.character)) {
148
+ t.hooks.set(hook.npc, hook.character);
149
+ }
150
+ }
151
+ if (t.open) {
152
+ dropHook(t, t.open.npc, t.open.character);
153
+ }
154
+ } else if (msg.k === 'hub-npc') {
155
+ if (msg.open && t.abandonedOpens.has(msg.npc) && t.asking !== msg.npc) {
156
+ t.net.send({ k: 'hub-npc-end', npc: msg.npc });
157
+ continue;
158
+ }
159
+ if (t.open?.npc === msg.npc && t.open.interrupted) {
160
+ continue;
161
+ }
79
162
  if (msg.open) {
80
- t.open = { npc: msg.npc, name: msg.name, mind: Boolean(msg.mind), character: msg.character ?? null, prefix: null };
81
- t.chrome.openTalk(msg.name);
163
+ if (t.open?.npc === msg.npc && t.open.attachment) {
164
+ const open = t.open;
165
+ // Recovery cannot send private mind input down an authored/public
166
+ // path, or give an already-typed question to a different character.
167
+ if (open.mind !== Boolean(msg.mind) || open.character !== (msg.character ?? null)) {
168
+ interrupted(t, open);
169
+ continue;
170
+ }
171
+ open.prefix = null;
172
+ open.questions = [...questionsOf(msg.opening?.asks), ...questionsOf(msg.opening?.more)];
173
+ delete open.persistentAsks;
174
+ delete open.withdrawnAsks;
175
+ open.needsAttach = false;
176
+ // A refused turn already counts this as its sole retry. An idle
177
+ // reconnect's open instead belongs to the next queued question.
178
+ open.reattached = !open.operationId;
179
+ t.asking = null;
180
+ open.attachment.opened = true;
181
+ // A body that required notes before the drop is not usable until
182
+ // its new notes arrive, even when the open is in an earlier frame.
183
+ // A prior no-note conversation uses bare words; the server sends
184
+ // no notes in that case.
185
+ if (!open.attachment.needsNotes) {
186
+ cancelAttach(open, true);
187
+ }
188
+ refill(t);
189
+ continue;
190
+ }
191
+ dropHook(t, msg.npc, msg.character);
192
+ clearAuthored(t.open);
193
+ cancelNotesWait(t.open);
194
+ t.waiting = false;
195
+ t.open = {
196
+ npc: msg.npc, name: msg.name, mind: Boolean(msg.mind), character: msg.character ?? null, prefix: null,
197
+ canType: !msg.mind || t.inspected.has(net),
198
+ questions: [...questionsOf(msg.opening?.asks), ...questionsOf(msg.opening?.more)],
199
+ sent: new Set(),
200
+ };
201
+ t.chrome.openTalk(msg.name, { mind: t.open.mind, canType: t.open.canType });
202
+ if (t.drafts.has(msg.npc)) {
203
+ t.chrome.talkDraft?.(t.drafts.get(msg.npc));
204
+ t.drafts.delete(msg.npc);
205
+ }
206
+ // v1.37: the NPC speaks first -- the place's own words, here at once,
207
+ // and what a player might ask it, each a tap away.
208
+ const first = typeof msg.opening?.text === 'string' ? msg.opening.text : '';
209
+ if (first) {
210
+ t.chrome.talkLine('them', first);
211
+ }
212
+ refill(t);
82
213
  if (t.open.mind) {
83
214
  registerParticipant(t, net);
84
215
  }
85
216
  } else if (!t.open || t.open.npc === msg.npc) {
217
+ if (t.open?.attachment) {
218
+ // These end the old conversation, never refuse an open. They may
219
+ // have been queued just before a NOT_TALKING receipt or reconnect.
220
+ if (['left', 'idle', 'end'].includes(msg.why)) {
221
+ continue;
222
+ }
223
+ t.open.interrupted = true;
224
+ t.open.interruptionWhy = msg.why;
225
+ cancelAttach(t.open);
226
+ t.asking = null;
227
+ continue;
228
+ }
229
+ clearAuthored(t.open);
230
+ cancelNotesWait(t.open);
86
231
  t.open = null;
87
232
  t.waiting = false;
88
233
  t.chrome.closeTalk();
@@ -90,28 +235,137 @@ function frame(t, scene, mp, idle) {
90
235
  if (t.asking === msg.npc) {
91
236
  t.asking = null;
92
237
  }
238
+ } else if (msg.k === 'hub-npc-opening' && t.open?.npc === msg.npc) {
239
+ // v1.47: a late greeting changes the first line only before we speak.
240
+ if (!t.open.sent.size && typeof msg.text === 'string' && msg.text) {
241
+ t.chrome.talkOpening?.(msg.text);
242
+ }
93
243
  } else if (msg.k === 'hub-npc-line' && t.open?.npc === msg.npc) {
244
+ if (t.open.interrupted) {
245
+ if (SCRIPTED[msg.why]) {
246
+ t.open.interruptionLine = msg.text;
247
+ }
248
+ continue;
249
+ }
94
250
  // With a mind, a line the place said instead is marked as scripted.
95
251
  const scripted = t.open.mind ? SCRIPTED[msg.why] : null;
96
252
  t.chrome.talkLine('them', msg.text, scripted ? `Scripted reply: ${t.open.name} ${scripted}` : null);
97
- t.waiting = false;
253
+ if (scripted && msg.op && msg.op === t.open.recoveryOperationId) {
254
+ // The line itself proves a recovery turn was refused. Cleanup must
255
+ // not depend on whether the request's rejection has arrived yet.
256
+ t.open.interruptionLine = msg.text;
257
+ t.open.interruptionShown = true;
258
+ interrupted(t, t.open);
259
+ continue;
260
+ }
261
+ if (!t.open.mind && t.open.authored && msg.why === 'authored' && (msg.rid === undefined || msg.rid === t.open.authored.rid)) {
262
+ finishAuthored(t, t.open, true);
263
+ } else if (t.open.mind && msg.op && msg.op === t.open.operationId) {
264
+ t.open.operationId = null;
265
+ t.waiting = false;
266
+ refill(t);
267
+ } else if (!t.open.operationId && !t.open.authored && !t.open.attachment) {
268
+ t.waiting = false;
269
+ refill(t);
270
+ }
98
271
  } else if (msg.k === 'hub-npc-notes' && t.open?.npc === msg.npc) {
99
272
  t.open.prefix = typeof msg.prefix === 'string' ? msg.prefix : null;
273
+ t.open.notesVersion = (t.open.notesVersion ?? 0) + 1;
274
+ if (msg.persistentAsks !== undefined || t.open.persistentAsks !== undefined) {
275
+ const previous = t.open.persistentAsks ?? [];
276
+ const persistent = questionsOf(msg.persistentAsks);
277
+ const withdrawn = t.open.withdrawnAsks ??= new Set();
278
+ for (const ask of previous) {
279
+ if (!persistent.includes(ask)) {
280
+ withdrawn.add(ask);
281
+ }
282
+ }
283
+ for (const ask of persistent) {
284
+ withdrawn.delete(ask);
285
+ }
286
+ t.open.persistentAsks = persistent;
287
+ if (!t.waiting && (previous.length !== persistent.length || previous.some((ask, i) => ask !== persistent[i]))) {
288
+ refill(t, t.suggestions);
289
+ }
290
+ }
291
+ if (t.open.attachment?.opened) {
292
+ cancelAttach(t.open, true);
293
+ }
294
+ cancelNotesWait(t.open, true);
295
+ }
296
+ }
297
+ if (t.open?.interrupted) {
298
+ const open = t.open;
299
+ // Close and scripted line are separate packets; allow the place's line
300
+ // to arrive on the next frame before choosing the fixed fallback.
301
+ if (open.interruptionLine || !SCRIPTED[open.interruptionWhy]) {
302
+ interrupted(t, open);
303
+ } else if (!open.interruptionTimer && !open.closeTimer) {
304
+ open.interruptionTimer = setTimeout(() => interrupted(t, open), 250);
100
305
  }
101
306
  }
102
307
  t.chrome.talkWaiting(t.waiting);
103
308
 
104
- const near = idle && net && !t.open ? nearestNpc(scene) : null;
309
+ const near = idle && net && !t.open ? nearestNpc(scene, t.preferred) : null;
105
310
  t.near = near;
106
- t.chrome.talkPrompt(near ? near.name : null);
311
+ t.chrome.talkPrompt(near ? near.name : null, near?.mind ?? false);
312
+ if (net && (near?.mind || companionNpc(scene)) && !t.approached.has(net)) {
313
+ t.approached.add(net);
314
+ registerParticipant(t, net);
315
+ }
316
+ const character = near && !t.asking ? t.hooks.get(near.id) : null;
317
+ if (character) {
318
+ dropHook(t, near.id, character);
319
+ void walkGreeting(net, character);
320
+ }
321
+ }
322
+
323
+ function dropHook(t, npc, character = t.hooks.get(npc)) {
324
+ t.hooks.delete(npc);
325
+ if (character) {
326
+ t.greeted.add(character);
327
+ for (const [id, mind] of t.hooks) {
328
+ if (mind === character) {
329
+ t.hooks.delete(id);
330
+ }
331
+ }
332
+ }
333
+ }
334
+
335
+ async function walkGreeting(net, character) {
336
+ try {
337
+ await net.characters.readCharacters({ requestId: newId(), query: { type: 'status', operationId: `greeting:${character}` } });
338
+ } catch {
339
+ // One attempt per hook; error details might contain private words.
340
+ console.log('[hub] NPC greeting read failed');
341
+ }
107
342
  }
108
343
 
109
- function nearestNpc(scene) {
344
+ // v1.46: the next questions, without anything this player already sent.
345
+ function questionsOf(value) {
346
+ return Array.isArray(value) ? value.filter((ask) => typeof ask === 'string').map((ask) => ask.trim().slice(0, INPUT_MAX)).filter(Boolean) : [];
347
+ }
348
+
349
+ function refill(t, suggestions) {
350
+ // Notes may change the offers between replies; keep that reply's source.
351
+ t.suggestions = suggestions;
352
+ const open = t.open;
353
+ const source = Array.isArray(suggestions) && suggestions.every((ask) => typeof ask === 'string') ? questionsOf(suggestions) : open?.questions ?? [];
354
+ let asks = open?.canType ? [...new Set(source)].filter((ask) => !open.sent.has(ask)).slice(0, 3) : [];
355
+ if (open?.canType && open.persistentAsks !== undefined) {
356
+ const persistent = open.persistentAsks;
357
+ asks = [...new Set([...persistent, ...source])].filter((ask) => persistent.includes(ask) || (!open.withdrawnAsks.has(ask) && !open.sent.has(ask))).slice(0, 3);
358
+ }
359
+ t.chrome.talkAsks?.(asks, (ask) => send(t, ask));
360
+ }
361
+
362
+ function nearestNpc(scene, preferred) {
110
363
  const local = scene.actorWith('Player')?.components.Transform;
111
364
  if (!local) {
112
365
  return null;
113
366
  }
114
367
  let best = null;
368
+ let choice = null;
115
369
  for (const actor of scene.getActors()) {
116
370
  const props = actor.components?.HubNpc;
117
371
  const at = actor.components?.Transform;
@@ -120,64 +374,244 @@ function nearestNpc(scene) {
120
374
  }
121
375
  const d = Math.hypot(at.x - local.x, at.z - local.z);
122
376
  const range = typeof props.talkRange === 'number' ? props.talkRange : TALK_RANGE_M;
123
- if (d <= range && (!best || d < best.d)) {
124
- best = { id: actor.id, name: typeof props.name === 'string' && props.name ? props.name : actor.id, d };
377
+ if (d <= range && (!best || d < best.d || actor.id === preferred)) {
378
+ best = { id: actor.id, name: typeof props.name === 'string' && props.name ? props.name : actor.id, mind: typeof props.character === 'string' && Boolean(props.character), character: props.character, d };
379
+ if (actor.id === preferred) {
380
+ choice = best;
381
+ }
125
382
  }
126
383
  }
127
- return best;
384
+ return choice ?? best;
128
385
  }
129
386
 
130
- function ask(t) {
131
- if (!t.near || !t.net || t.asking) {
387
+ function companionNpc(scene) {
388
+ if (!hub.place?.companionChat) {
389
+ return null;
390
+ }
391
+ const playerId = scene.multiplayer?.net?.selfId?.();
392
+ return playerId && hub.companion?.c && scene.actorWith('Player')
393
+ ? { id: `companion:${playerId}`, name: hub.place.companionChat.name, mind: true } : null;
394
+ }
395
+
396
+ function ask(t, near = t.near) {
397
+ if (!near || !t.net || t.asking) {
132
398
  return;
133
399
  }
134
- t.asking = t.near.id;
135
- t.net.send({ k: 'hub-npc-open', npc: t.near.id });
400
+ t.asking = near.id;
401
+ t.abandonedOpens.delete(near.id);
402
+ dropHook(t, near.id, near.character ?? t.hooks.get(near.id));
403
+ t.net.send({ k: 'hub-npc-open', npc: near.id });
136
404
  }
137
405
 
138
406
  function end(t) {
139
407
  if (t.open && t.net) {
140
408
  t.net.send({ k: 'hub-npc-end', npc: t.open.npc });
141
409
  }
410
+ clearAuthored(t.open);
411
+ if (t.open?.attachment) {
412
+ t.abandonedOpens.add(t.open.npc);
413
+ }
414
+ cancelAttach(t.open);
415
+ cancelNotesWait(t.open);
416
+ clearTimeout(t.open?.closeTimer);
417
+ clearTimeout(t.open?.interruptionTimer);
142
418
  t.open = null;
419
+ t.asking = null;
143
420
  t.waiting = false;
144
421
  t.chrome.closeTalk();
145
422
  }
146
423
 
424
+ function cancelAttach(open, attached = false) {
425
+ const attachment = open?.attachment;
426
+ if (attachment) {
427
+ clearTimeout(attachment.timer);
428
+ open.attachment = null;
429
+ attachment.resolve(attached);
430
+ }
431
+ }
432
+
433
+ function reattach(t, open) {
434
+ if (t.open !== open || open.interrupted || !t.net) {
435
+ return Promise.resolve(false);
436
+ }
437
+ if (open.attachment) {
438
+ return open.attachment.promise;
439
+ }
440
+ const attachment = { needsNotes: open.mind && open.prefix !== null, opened: false };
441
+ attachment.promise = new Promise((resolve) => { attachment.resolve = resolve; });
442
+ open.attachment = attachment;
443
+ t.asking = open.npc;
444
+ attachment.timer = setTimeout(() => {
445
+ if (t.open === open && open.attachment === attachment) {
446
+ interrupted(t, open);
447
+ }
448
+ }, ATTACH_WAIT_MS);
449
+ t.net.send({ k: 'hub-npc-open', npc: open.npc });
450
+ return attachment.promise;
451
+ }
452
+
453
+ function cancelNotesWait(open, ready = false) {
454
+ if (open?.notesWait) {
455
+ clearTimeout(open.notesWait.timer);
456
+ open.notesWait.resolve(ready);
457
+ open.notesWait = null;
458
+ }
459
+ }
460
+
461
+ function waitForNotes(t, open, version) {
462
+ if (t.open !== open || open.interrupted) {
463
+ return Promise.resolve(false);
464
+ }
465
+ if ((open.notesVersion ?? 0) > version) {
466
+ return Promise.resolve(true);
467
+ }
468
+ const wait = {};
469
+ wait.promise = new Promise((resolve) => { wait.resolve = resolve; });
470
+ open.notesWait = wait;
471
+ wait.timer = setTimeout(() => {
472
+ if (t.open === open && open.notesWait === wait) {
473
+ interrupted(t, open);
474
+ }
475
+ }, ATTACH_WAIT_MS);
476
+ return wait.promise;
477
+ }
478
+
479
+ function interrupted(t, open) {
480
+ if (t.open !== open || open.closeTimer) {
481
+ return;
482
+ }
483
+ open.interrupted = true;
484
+ clearTimeout(open.interruptionTimer);
485
+ if (open.attachment) {
486
+ t.abandonedOpens.add(open.npc);
487
+ }
488
+ cancelAttach(open);
489
+ cancelNotesWait(open);
490
+ clearAuthored(open);
491
+ open.operationId = null;
492
+ t.asking = null;
493
+ t.waiting = false;
494
+ t.chrome.talkWaiting(false);
495
+ t.chrome.talkAsks?.([], null);
496
+ if (!open.interruptionShown) {
497
+ t.chrome.talkLine('them', open.interruptionLine || INTERRUPTED_LINE);
498
+ open.interruptionShown = true;
499
+ }
500
+ const draft = open.pendingText || t.chrome.talkDraft?.();
501
+ if (draft) {
502
+ t.drafts.set(open.npc, draft);
503
+ t.chrome.talkDraft?.(draft);
504
+ open.sent.delete(draft);
505
+ }
506
+ t.chrome.talkTyping?.(false);
507
+ open.closeTimer = setTimeout(() => {
508
+ if (t.open === open) {
509
+ end(t);
510
+ }
511
+ }, INTERRUPTED_MS);
512
+ }
513
+
514
+ function clearAuthored(open) {
515
+ if (open?.authored) {
516
+ clearTimeout(open.authored.timer);
517
+ open.authored = null;
518
+ }
519
+ }
520
+
521
+ function finishAuthored(t, open, replied) {
522
+ if (t.open !== open || !open.authored) {
523
+ return;
524
+ }
525
+ if (replied) {
526
+ open.sent.add(open.authored.text);
527
+ open.pendingText = null;
528
+ }
529
+ clearAuthored(open);
530
+ t.waiting = false;
531
+ t.chrome.talkWaiting(false);
532
+ refill(t);
533
+ }
534
+
147
535
  // The world learns which player here is which participant from that player's
148
536
  // own read; the place needs it to tell who a character chose to follow.
149
- function registerParticipant(t, net) {
537
+ async function registerParticipant(t, net) {
150
538
  const characters = net?.characters;
151
- if (!characters || t.inspected.has(net)) {
539
+ if (!characters || t.inspected.has(net) || t.inspecting.has(net)) {
152
540
  return;
153
541
  }
154
- t.inspected.add(net);
155
- characters.readCharacters({ requestId: newId(), query: { type: 'inspect' } }).catch((err) => {
156
- console.log(`[hub] NPC inspect failed: ${err?.code ?? err?.message ?? err}`);
157
- });
542
+ t.inspecting.add(net);
543
+ try {
544
+ await characters.readCharacters({ requestId: newId(), query: { type: 'inspect' } });
545
+ t.inspected.add(net);
546
+ if (t.net === net && t.open?.mind) {
547
+ t.open.canType = true;
548
+ t.chrome.talkTyping(true);
549
+ refill(t);
550
+ }
551
+ } catch (err) {
552
+ const code = err?.code ?? err?.message ?? String(err);
553
+ if (!t.inspectRefusals.has(code)) {
554
+ t.inspectRefusals.add(code);
555
+ console.log(`[hub] NPC inspect failed: ${code}`);
556
+ }
557
+ } finally {
558
+ t.inspecting.delete(net);
559
+ }
158
560
  }
159
561
 
160
562
  function send(t, raw) {
161
563
  const text = String(raw ?? '').trim().slice(0, INPUT_MAX);
162
564
  const open = t.open;
163
- if (!text || !open || t.waiting || !t.net) {
565
+ if (!text || !open?.canType || open.interrupted || t.waiting || !t.net) {
164
566
  return;
165
567
  }
568
+ if (!open.mind && performance.now() - t.saidAt < AUTHORED_GAP_MS) {
569
+ return;
570
+ }
571
+ t.chrome.talkAsks?.([], null);
166
572
  t.chrome.talkLine('me', text);
573
+ t.waiting = true;
574
+ open.pendingText = text;
575
+ t.chrome.talkWaiting(true);
576
+ if (open.needsAttach || open.attachment) {
577
+ void reattach(t, open).then((attached) => {
578
+ if (attached && t.open === open) {
579
+ dispatch(t, open, text, true);
580
+ }
581
+ });
582
+ return;
583
+ }
584
+ dispatch(t, open, text);
585
+ }
586
+
587
+ function dispatch(t, open, text, reattached = false) {
588
+ reattached ||= open.reattached === true;
589
+ open.reattached = false;
167
590
  if (!open.mind) {
168
- t.net.send({ k: 'hub-npc-say', npc: open.npc, text });
591
+ t.saidAt = performance.now();
592
+ // A reply to an earlier send must not acknowledge this question.
593
+ const rid = newId();
594
+ open.authored = { text, rid, timer: setTimeout(() => finishAuthored(t, open, false), AUTHORED_WAIT_MS) };
595
+ t.net.send({ k: 'hub-npc-say', npc: open.npc, text, rid });
169
596
  return;
170
597
  }
171
- t.waiting = true;
172
- void turn(t, open, text);
598
+ open.sent.add(text);
599
+ void turn(t, open, text, reattached);
173
600
  }
174
601
 
175
- async function turn(t, open, text) {
602
+ async function turn(t, open, text, attachRetried = false) {
176
603
  const net = t.net;
177
604
  const characters = net?.characters;
178
605
  let operationId = null;
606
+ let requests = 0;
607
+ let notesVersion = 0;
608
+ // A scripted line may end this wait and let the player send again.
609
+ const active = () => t.open === open && t.net === net && open.operationId === operationId;
179
610
  const request = () => {
180
- operationId = newId();
611
+ requests++;
612
+ notesVersion = open.notesVersion ?? 0;
613
+ open.operationId = operationId = newId();
614
+ open.recoveryOperationId = attachRetried ? operationId : null;
181
615
  return characters.requestCharacterTurn({
182
616
  requestId: newId(),
183
617
  operationId,
@@ -187,32 +621,82 @@ async function turn(t, open, text) {
187
621
  audience: { disclosure: 'participants', playerIds: [], characterIds: [] },
188
622
  });
189
623
  };
624
+ let notesRetried = false;
190
625
  try {
191
- try {
192
- await request();
193
- } catch (err) {
194
- // The notes changed under the words (a pad filled): once more, with the
195
- // newest the server sent.
196
- if (err?.code !== 'HUB_NPC_NOTES' || t.open !== open) {
197
- throw err;
626
+ while (true) {
627
+ try {
628
+ await request();
629
+ if (open.recoveryOperationId === operationId) {
630
+ open.recoveryOperationId = null;
631
+ }
632
+ if (active()) {
633
+ open.pendingText = null;
634
+ }
635
+ break;
636
+ } catch (err) {
637
+ if (err?.code === 'HUB_NPC_NOT_TALKING' && active() && !attachRetried && requests < 2) {
638
+ attachRetried = true;
639
+ if (!(await reattach(t, open)) || !active()) {
640
+ return;
641
+ }
642
+ continue;
643
+ }
644
+ // The notes changed under the words (a pad filled): once more, with
645
+ // the newest the server sent.
646
+ if (err?.code !== 'HUB_NPC_NOTES' || !active() || notesRetried || requests >= 2) {
647
+ throw err;
648
+ }
649
+ notesRetried = true;
650
+ if (attachRetried) {
651
+ if (!(await waitForNotes(t, open, notesVersion))) {
652
+ return;
653
+ }
654
+ } else {
655
+ await new Promise((resolve) => setTimeout(resolve, NOTES_RETRY_MS));
656
+ }
657
+ if (!active()) {
658
+ return;
659
+ }
198
660
  }
199
- await new Promise((resolve) => setTimeout(resolve, NOTES_RETRY_MS));
200
- await request();
201
661
  }
202
662
  } catch (err) {
203
663
  // A refusal (busy, over budget, too far) comes with the place's own line,
204
664
  // which ends the wait; this only covers a refusal that brought none.
205
665
  console.log(`[hub] NPC turn refused: ${err?.code ?? err?.message ?? err}`);
666
+ // The server holds no conversation for this panel (it ended there, or the
667
+ // session never had one) and sends no line with that: a panel left open
668
+ // would take more words and answer none. Closed, the talk button is back.
669
+ if (active() && (err?.code === 'HUB_NPC_NOT_TALKING' || attachRetried)) {
670
+ const why = REFUSAL_WHY[err?.code];
671
+ if (why) {
672
+ // A rejection may precede its separate scripted-line packet. Keep
673
+ // the pending words and allow that line the same grace as an open
674
+ // refusal; neither packet order can leave the panel accepting words.
675
+ open.interrupted = true;
676
+ open.interruptionWhy = why;
677
+ open.interruptionTimer = setTimeout(() => interrupted(t, open), 250);
678
+ } else {
679
+ interrupted(t, open);
680
+ }
681
+ return;
682
+ }
683
+ if (open.interrupted) {
684
+ return;
685
+ }
206
686
  setTimeout(() => {
207
- if (t.open === open) {
687
+ if (active()) {
688
+ open.operationId = null;
208
689
  t.waiting = false;
209
690
  }
210
691
  }, 1500);
211
692
  return;
212
693
  }
213
694
  const started = performance.now();
214
- while (t.open === open && t.net === net && performance.now() - started < POLL_LIMIT_MS) {
695
+ while (active() && performance.now() - started < POLL_LIMIT_MS) {
215
696
  await new Promise((resolve) => setTimeout(resolve, POLL_MS));
697
+ if (!active()) {
698
+ return;
699
+ }
216
700
  let status = null;
217
701
  try {
218
702
  status = await characters.readCharacters({ requestId: newId(), query: { type: 'status', operationId } });
@@ -220,14 +704,21 @@ async function turn(t, open, text) {
220
704
  console.log(`[hub] NPC status read failed: ${err?.code ?? err?.message ?? err}`);
221
705
  continue;
222
706
  }
707
+ if (!active()) {
708
+ return;
709
+ }
223
710
  if (status?.status === 'completed' || status?.status === 'interrupted') {
224
- if (t.open === open && typeof status.response === 'string' && status.response) {
225
- t.chrome.talkLine('them', unquote(status.response));
711
+ const reply = typeof status.response === 'string' ? status.response : status.response?.text;
712
+ if (typeof reply === 'string' && reply) {
713
+ t.chrome.talkLine('them', unquote(reply));
226
714
  }
715
+ const suggestions = Array.isArray(status.suggestions) && status.suggestions.every((ask) => typeof ask === 'string') ? status.suggestions : status.response?.suggestions;
716
+ refill(t, suggestions);
227
717
  break;
228
718
  }
229
719
  }
230
- if (t.open === open) {
720
+ if (active()) {
721
+ open.operationId = null;
231
722
  t.waiting = false;
232
723
  }
233
724
  }