castle-web-cli 0.4.232 → 0.4.233

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (355) hide show
  1. package/dist/agent-failures.d.ts +8 -0
  2. package/dist/agent-failures.js +19 -2
  3. package/dist/agent-prompts.d.ts +1 -1
  4. package/dist/agent-prompts.js +10 -3
  5. package/dist/agent.d.ts +64 -3
  6. package/dist/agent.js +170 -81
  7. package/dist/castle-host/host.d.ts +1 -0
  8. package/dist/castle-host/host.js +51 -3
  9. package/dist/castleJson.d.ts +4 -2
  10. package/dist/castleJson.js +2 -0
  11. package/dist/check.d.ts +3 -1
  12. package/dist/check.js +11 -1
  13. package/dist/devSessionStorage.js +47 -1
  14. package/dist/importPinMismatch.d.ts +22 -0
  15. package/dist/importPinMismatch.js +77 -0
  16. package/dist/importRegression.js +3 -15
  17. package/dist/imports.d.ts +10 -3
  18. package/dist/imports.js +88 -20
  19. package/dist/index.js +5 -4
  20. package/dist/init.js +1 -1
  21. package/dist/platformDoc.js +22 -0
  22. package/dist/routerFences.d.ts +5 -0
  23. package/dist/routerFences.js +12 -2
  24. package/dist/save-deck.d.ts +5 -0
  25. package/dist/save-deck.js +37 -4
  26. package/dist/serve.js +3 -0
  27. package/dist/shell/assets/{index-D2tWM896.js → index-BjOxR-lW.js} +4 -4
  28. package/dist/shell/assets/index-MVLfNYd9.css +1 -0
  29. package/dist/shell/index.html +2 -2
  30. package/dist/taskSteer.d.ts +13 -0
  31. package/dist/taskSteer.js +22 -2
  32. package/kits/base/castle.json +1 -1
  33. package/kits/base/package-lock.json +1 -1
  34. package/kits/base/sdk/README.md +79 -2
  35. package/kits/base/sdk/avatar.d.ts +6 -2
  36. package/kits/base/sdk/avatar.js +12 -4
  37. package/kits/base/sdk/castle.d.ts +1 -1
  38. package/kits/base/sdk/commands.d.ts +14 -0
  39. package/kits/base/sdk/hub.d.ts +6 -2
  40. package/kits/base/sdk/hub.js +13 -0
  41. package/kits/base/sdk/multiplayer.js +17 -1
  42. package/kits/base/sdk/save.js +2 -1
  43. package/kits/base/sdk/server/hub.d.ts +87 -0
  44. package/kits/base/sdk/server/hub.js +70 -0
  45. package/kits/base/sdk/server/platformHandle.d.ts +31 -1
  46. package/kits/base/sdk/server/storage.d.ts +13 -0
  47. package/kits/base/sdk/server/storage.js +46 -0
  48. package/kits/base/sdk/server/wrapper.d.ts +36 -5
  49. package/kits/base/sdk/server/wrapper.js +83 -8
  50. package/kits/hub-game/CLAUDE.md +329 -11
  51. package/kits/hub-game/blueprints/avatar-10.scene +28 -0
  52. package/kits/hub-game/blueprints/avatar-11.scene +28 -0
  53. package/kits/hub-game/blueprints/avatar-12.scene +28 -0
  54. package/kits/hub-game/blueprints/avatar-13.scene +28 -0
  55. package/kits/hub-game/blueprints/avatar-14.scene +28 -0
  56. package/kits/hub-game/blueprints/avatar-15.scene +28 -0
  57. package/kits/hub-game/blueprints/avatar-4.scene +28 -0
  58. package/kits/hub-game/blueprints/avatar-5.scene +28 -0
  59. package/kits/hub-game/blueprints/avatar-6.scene +28 -0
  60. package/kits/hub-game/blueprints/avatar-7.scene +28 -0
  61. package/kits/hub-game/blueprints/avatar-8.scene +28 -0
  62. package/kits/hub-game/blueprints/avatar-9.scene +28 -0
  63. package/kits/hub-game/code/client/actorBudget.js +47 -0
  64. package/kits/hub-game/code/client/attention.js +11 -2
  65. package/kits/hub-game/code/client/avatar.js +309 -35
  66. package/kits/hub-game/code/client/avatarCap.js +228 -0
  67. package/kits/hub-game/code/client/avatarColors.js +70 -0
  68. package/kits/hub-game/code/client/avatarFaces.js +21 -0
  69. package/kits/hub-game/code/client/avatarShare.js +251 -0
  70. package/kits/hub-game/code/client/ballot.js +467 -0
  71. package/kits/hub-game/code/client/build.js +22 -0
  72. package/kits/hub-game/code/client/chrome.js +1144 -50
  73. package/kits/hub-game/code/client/companion.js +716 -0
  74. package/kits/hub-game/code/client/companionDress.js +99 -0
  75. package/kits/hub-game/code/client/decor.js +509 -0
  76. package/kits/hub-game/code/client/earned.js +131 -0
  77. package/kits/hub-game/code/client/emoteBubble.js +75 -0
  78. package/kits/hub-game/code/client/grab.js +339 -20
  79. package/kits/hub-game/code/client/hookLayer.js +299 -13
  80. package/kits/hub-game/code/client/idle.js +163 -22
  81. package/kits/hub-game/code/client/interestCap.js +81 -0
  82. package/kits/hub-game/code/client/keys.js +36 -0
  83. package/kits/hub-game/code/client/labelsNear.js +205 -0
  84. package/kits/hub-game/code/client/menuIcons.js +34 -0
  85. package/kits/hub-game/code/client/pack.js +6 -2
  86. package/kits/hub-game/code/client/packFormat.js +3 -3
  87. package/kits/hub-game/code/client/rehello.js +167 -0
  88. package/kits/hub-game/code/client/shot.js +92 -0
  89. package/kits/hub-game/code/client/sound.js +63 -8
  90. package/kits/hub-game/code/client/soundscape.js +1151 -0
  91. package/kits/hub-game/code/client/soundscapeActions.js +99 -0
  92. package/kits/hub-game/code/client/soundscapeAmbience.js +453 -0
  93. package/kits/hub-game/code/client/soundscapeBaked.js +143 -0
  94. package/kits/hub-game/code/client/soundscapeBusker.js +217 -0
  95. package/kits/hub-game/code/client/soundscapeCourt.js +200 -0
  96. package/kits/hub-game/code/client/soundscapeCrowd.js +331 -0
  97. package/kits/hub-game/code/client/soundscapeCues.js +362 -0
  98. package/kits/hub-game/code/client/soundscapeMap.js +181 -0
  99. package/kits/hub-game/code/client/soundscapeMarket.js +176 -0
  100. package/kits/hub-game/code/client/soundscapeMusic.js +126 -0
  101. package/kits/hub-game/code/client/soundscapeMusicEngine.js +460 -0
  102. package/kits/hub-game/code/client/soundscapeSimlish.js +737 -0
  103. package/kits/hub-game/code/client/soundscapeSongs.js +127 -0
  104. package/kits/hub-game/code/client/soundscapeSongsRound3.js +63 -0
  105. package/kits/hub-game/code/client/soundscapeSynthesis.js +168 -0
  106. package/kits/hub-game/code/client/soundscapeUi.js +211 -0
  107. package/kits/hub-game/code/client/soundscapeVoice.js +1209 -0
  108. package/kits/hub-game/code/client/soundscapeWaves.js +206 -0
  109. package/kits/hub-game/code/client/spin.js +101 -0
  110. package/kits/hub-game/code/client/state.js +109 -1
  111. package/kits/hub-game/code/client/stuck.js +259 -0
  112. package/kits/hub-game/code/client/talk.js +538 -47
  113. package/kits/hub-game/code/client/tiers.js +516 -27
  114. package/kits/hub-game/code/client/toastBand.js +37 -0
  115. package/kits/hub-game/code/client/together.js +32 -0
  116. package/kits/hub-game/code/client/visibility.js +43 -0
  117. package/kits/hub-game/code/client/wear.js +461 -170
  118. package/kits/hub-game/code/client/yours.js +199 -0
  119. package/kits/hub-game/code/companionSpec.js +45 -0
  120. package/kits/hub-game/code/privateText.js +7 -0
  121. package/kits/hub-game/code/server/actions.js +133 -0
  122. package/kits/hub-game/code/server/actors.js +9 -1
  123. package/kits/hub-game/code/server/ask.js +155 -0
  124. package/kits/hub-game/code/server/ballot.js +539 -0
  125. package/kits/hub-game/code/server/cells.js +100 -0
  126. package/kits/hub-game/code/server/companion.js +82 -0
  127. package/kits/hub-game/code/server/cues.js +70 -0
  128. package/kits/hub-game/code/server/decor.js +269 -0
  129. package/kits/hub-game/code/server/earned.js +461 -0
  130. package/kits/hub-game/code/server/friendBests.js +166 -0
  131. package/kits/hub-game/code/server/friendsAmong.js +232 -0
  132. package/kits/hub-game/code/server/history.js +5 -0
  133. package/kits/hub-game/code/server/hub.js +1254 -50
  134. package/kits/hub-game/code/server/idle.js +30 -11
  135. package/kits/hub-game/code/server/index.js +2 -2
  136. package/kits/hub-game/code/server/labels.js +28 -2
  137. package/kits/hub-game/code/server/manifest.js +378 -7
  138. package/kits/hub-game/code/server/menu.js +133 -0
  139. package/kits/hub-game/code/server/npcs.js +830 -80
  140. package/kits/hub-game/code/server/party.js +2 -2
  141. package/kits/hub-game/code/server/players.js +22 -0
  142. package/kits/hub-game/code/server/shot.js +110 -0
  143. package/kits/hub-game/code/server/standing.js +124 -0
  144. package/kits/hub-game/code/server/vis.js +113 -0
  145. package/kits/hub-game/code/server/wear.js +12 -0
  146. package/kits/hub-game/code/server/yours.js +59 -0
  147. package/kits/hub-game/code/shared/cues.js +9 -0
  148. package/kits/hub-game/code/shared/decor.js +41 -0
  149. package/kits/hub-game/code/shared/shot.js +19 -0
  150. package/kits/hub-game/code/shared/spin.js +57 -0
  151. package/kits/hub-game/code/systems/hub.js +845 -95
  152. package/kits/hub-game/docs/content-pack.md +1 -1
  153. package/kits/hub-game/docs/contract.md +2244 -61
  154. package/kits/hub-game/docs/home-pieces.md +64 -0
  155. package/kits/hub-game/models/avatar-10.pxmodel +28 -0
  156. package/kits/hub-game/models/avatar-11.pxmodel +28 -0
  157. package/kits/hub-game/models/avatar-12.pxmodel +27 -0
  158. package/kits/hub-game/models/avatar-13.pxmodel +24 -0
  159. package/kits/hub-game/models/avatar-14.pxmodel +29 -0
  160. package/kits/hub-game/models/avatar-15.pxmodel +24 -0
  161. package/kits/hub-game/models/avatar-4.pxmodel +29 -0
  162. package/kits/hub-game/models/avatar-5.pxmodel +27 -0
  163. package/kits/hub-game/models/avatar-6.pxmodel +26 -0
  164. package/kits/hub-game/models/avatar-7.pxmodel +25 -0
  165. package/kits/hub-game/models/avatar-8.pxmodel +24 -0
  166. package/kits/hub-game/models/avatar-9.pxmodel +30 -0
  167. package/kits/hub-game/models/earned/badge-round.pxmodel +100 -0
  168. package/kits/hub-game/models/earned/badge-shield.pxmodel +117 -0
  169. package/kits/hub-game/models/earned/badge-star.pxmodel +87 -0
  170. package/kits/hub-game/models/earned/belt-plain.pxmodel +72 -0
  171. package/kits/hub-game/models/earned/held-ball.pxmodel +43 -0
  172. package/kits/hub-game/models/earned/held-lantern.pxmodel +85 -0
  173. package/kits/hub-game/models/earned/held-net.pxmodel +57 -0
  174. package/kits/hub-game/models/earned/held-trophy.pxmodel +109 -0
  175. package/kits/hub-game/models/earned/pin-round.pxmodel +62 -0
  176. package/kits/hub-game/models/earned/sash-plain.pxmodel +66 -0
  177. package/kits/hub-game/models/earned/square/belt-plain.pxmodel +104 -0
  178. package/kits/hub-game/models/earned/square/sash-plain.pxmodel +98 -0
  179. package/kits/hub-game/models/hats/bat-clip.pxmodel +206 -0
  180. package/kits/hub-game/models/hats/beekeeper.pxmodel +149 -0
  181. package/kits/hub-game/models/hats/cap.pxmodel +162 -0
  182. package/kits/hub-game/models/hats/captain.pxmodel +178 -0
  183. package/kits/hub-game/models/hats/crooked-top.pxmodel +172 -0
  184. package/kits/hub-game/models/hats/flower-crown.pxmodel +451 -0
  185. package/kits/hub-game/models/hats/foam-crown.pxmodel +179 -0
  186. package/kits/hub-game/models/hats/lantern.pxmodel +230 -0
  187. package/kits/hub-game/models/hats/laurel.pxmodel +533 -0
  188. package/kits/hub-game/models/hats/lighthouse.pxmodel +96 -0
  189. package/kits/hub-game/models/hats/sailor.pxmodel +141 -0
  190. package/kits/hub-game/models/hats/souwester.pxmodel +131 -0
  191. package/kits/hub-game/models/hats/straw.pxmodel +139 -0
  192. package/kits/hub-game/models/hats/sunflower.pxmodel +406 -0
  193. package/kits/hub-game/models/hats/sweatband.pxmodel +112 -0
  194. package/kits/hub-game/models/hats/witch.pxmodel +147 -0
  195. package/kits/hub-game/package-lock.json +2 -0
  196. package/kits/hub-game/package.json +2 -1
  197. package/kits/hub-game/scripts/check.mjs +78 -11
  198. package/kits/hub-game/scripts/companion-follower-wire.json +1 -0
  199. package/kits/hub-game/scripts/fixtures/decor-keyless-draw.json +121 -0
  200. package/kits/hub-game/scripts/fixtures/decor-keyless.json +293 -0
  201. package/kits/hub-game/scripts/fixtures/engine-models-stub.mjs +23 -0
  202. package/kits/hub-game/scripts/fixtures/fake-dom.mjs +107 -0
  203. package/kits/hub-game/scripts/fixtures/interest-legacy.json +385 -0
  204. package/kits/hub-game/scripts/fixtures/npc-notes-v1.77.jsonl +4 -0
  205. package/kits/hub-game/scripts/fixtures/vis-shared-wire.json +1 -0
  206. package/kits/hub-game/scripts/gen-avatars.mjs +933 -16
  207. package/kits/hub-game/scripts/gen-earned.mjs +285 -0
  208. package/kits/hub-game/scripts/gen-hats.mjs +827 -0
  209. package/kits/hub-game/scripts/grab.before-orbit.mjs +400 -0
  210. package/kits/hub-game/scripts/soundscape-wave-analysis.mjs +125 -0
  211. package/kits/hub-game/scripts/test-actions-client.mjs +488 -0
  212. package/kits/hub-game/scripts/test-actions.mjs +298 -0
  213. package/kits/hub-game/scripts/test-actor-budget.mjs +44 -0
  214. package/kits/hub-game/scripts/test-actors.mjs +19 -17
  215. package/kits/hub-game/scripts/test-ask-client.mjs +358 -0
  216. package/kits/hub-game/scripts/test-ask.mjs +269 -0
  217. package/kits/hub-game/scripts/test-attention.mjs +10 -1
  218. package/kits/hub-game/scripts/test-avatar-cap.mjs +379 -0
  219. package/kits/hub-game/scripts/test-avatar-colors.mjs +72 -0
  220. package/kits/hub-game/scripts/test-avatar-models.mjs +181 -0
  221. package/kits/hub-game/scripts/test-avatar-share.mjs +318 -0
  222. package/kits/hub-game/scripts/test-avatar-wire.mjs +753 -0
  223. package/kits/hub-game/scripts/test-avatar.mjs +540 -7
  224. package/kits/hub-game/scripts/test-ballot.mjs +1001 -0
  225. package/kits/hub-game/scripts/test-build.mjs +31 -0
  226. package/kits/hub-game/scripts/test-cells.mjs +71 -0
  227. package/kits/hub-game/scripts/test-chat.mjs +662 -0
  228. package/kits/hub-game/scripts/test-classic.mjs +2 -0
  229. package/kits/hub-game/scripts/test-companion-chat.mjs +198 -0
  230. package/kits/hub-game/scripts/test-companion-client.mjs +1167 -0
  231. package/kits/hub-game/scripts/test-companion.mjs +259 -0
  232. package/kits/hub-game/scripts/test-decor-client.mjs +303 -0
  233. package/kits/hub-game/scripts/test-decor.mjs +457 -0
  234. package/kits/hub-game/scripts/test-earned-client.mjs +459 -0
  235. package/kits/hub-game/scripts/test-earned.mjs +1194 -0
  236. package/kits/hub-game/scripts/test-emote-bubble.mjs +110 -0
  237. package/kits/hub-game/scripts/test-friend-bests.mjs +435 -0
  238. package/kits/hub-game/scripts/test-grab-orbit.mjs +918 -0
  239. package/kits/hub-game/scripts/test-grab.mjs +7 -0
  240. package/kits/hub-game/scripts/test-hats.mjs +422 -168
  241. package/kits/hub-game/scripts/test-hub-leaving.mjs +203 -0
  242. package/kits/hub-game/scripts/test-hub-ops.mjs +439 -2
  243. package/kits/hub-game/scripts/test-hub-trip.mjs +716 -2
  244. package/kits/hub-game/scripts/test-hud-clear.mjs +328 -4
  245. package/kits/hub-game/scripts/test-idle.mjs +361 -1
  246. package/kits/hub-game/scripts/test-interest-cap.mjs +36 -0
  247. package/kits/hub-game/scripts/test-interest-cells.mjs +493 -0
  248. package/kits/hub-game/scripts/test-interest-client.mjs +118 -0
  249. package/kits/hub-game/scripts/test-keys.mjs +46 -1
  250. package/kits/hub-game/scripts/test-labels.mjs +596 -0
  251. package/kits/hub-game/scripts/test-manifest.mjs +207 -0
  252. package/kits/hub-game/scripts/test-menu-client.mjs +480 -0
  253. package/kits/hub-game/scripts/test-menu.mjs +296 -0
  254. package/kits/hub-game/scripts/test-npcs.mjs +1718 -12
  255. package/kits/hub-game/scripts/test-object-entry.mjs +76 -0
  256. package/kits/hub-game/scripts/test-pack.mjs +7 -0
  257. package/kits/hub-game/scripts/test-picker.mjs +314 -0
  258. package/kits/hub-game/scripts/test-rehello.mjs +1039 -0
  259. package/kits/hub-game/scripts/test-relations-among.mjs +222 -0
  260. package/kits/hub-game/scripts/test-relations-refresh.mjs +188 -0
  261. package/kits/hub-game/scripts/test-shot.mjs +756 -0
  262. package/kits/hub-game/scripts/test-soundscape.mjs +5432 -0
  263. package/kits/hub-game/scripts/test-spin.mjs +314 -0
  264. package/kits/hub-game/scripts/test-standing.mjs +416 -0
  265. package/kits/hub-game/scripts/test-stuck.mjs +1903 -0
  266. package/kits/hub-game/scripts/test-tier-cells.mjs +706 -0
  267. package/kits/hub-game/scripts/test-tiers-render.mjs +339 -0
  268. package/kits/hub-game/scripts/test-tiers.mjs +477 -2
  269. package/kits/hub-game/scripts/test-toast-band.mjs +61 -0
  270. package/kits/hub-game/scripts/test-try-on.mjs +22 -22
  271. package/kits/hub-game/scripts/test-vis.mjs +343 -0
  272. package/kits/hub-game/scripts/test-visibility.mjs +134 -0
  273. package/kits/hub-game/scripts/test-yours-chrome.mjs +166 -0
  274. package/kits/hub-game/scripts/test-yours.mjs +372 -0
  275. package/kits/multiplayer-3d/CLAUDE.md +50 -0
  276. package/kits/multiplayer-3d/castle.json +5 -5
  277. package/kits/multiplayer-3d/code/client/avatars.js +7 -0
  278. package/kits/multiplayer-3d/code/client/gait.js +65 -0
  279. package/kits/multiplayer-3d/code/client/nameTags.js +59 -4
  280. package/kits/multiplayer-3d/code/options.js +5 -0
  281. package/kits/multiplayer-3d/code/server/ownership.js +10 -2
  282. package/kits/multiplayer-3d/code/server/sceneBodies.js +12 -0
  283. package/kits/multiplayer-3d/code/server/world.js +7 -0
  284. package/kits/multiplayer-3d/code/systems/multiplayer.js +74 -4
  285. package/kits/multiplayer-3d/package.json +2 -1
  286. package/kits/multiplayer-3d/scripts/fixtures/README.md +11 -0
  287. package/kits/multiplayer-3d/scripts/fixtures/ownership-legacy.json +2 -0
  288. package/kits/multiplayer-3d/scripts/ownership-harness.mjs +102 -0
  289. package/kits/multiplayer-3d/scripts/ownership-loader.mjs +46 -0
  290. package/kits/multiplayer-3d/scripts/ownership-worker.mjs +15 -0
  291. package/kits/multiplayer-3d/scripts/record-ownership-session.mjs +98 -0
  292. package/kits/multiplayer-3d/scripts/test-eligible-claims.mjs +167 -0
  293. package/kits/multiplayer-3d/scripts/test-own-name-tag.mjs +247 -0
  294. package/kits/physics-2d/CLAUDE.md +9 -0
  295. package/kits/physics-2d/castle.json +2 -2
  296. package/kits/physics-2d/engine/scene.js +253 -1
  297. package/kits/physics-2d/engine/skyKey.js +129 -0
  298. package/kits/physics-2d/engine/stillClock.js +106 -0
  299. package/kits/physics-2d/engine/updateBudget.js +21 -0
  300. package/kits/physics-2d/scripts/test-skip-unchanged.mjs +326 -1
  301. package/kits/physics-3d/CLAUDE.md +1169 -30
  302. package/kits/physics-3d/behaviors/Player.jsx +629 -72
  303. package/kits/physics-3d/castle.json +3 -3
  304. package/kits/physics-3d/docs/pxmodel-format.md +8 -0
  305. package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +69 -13
  306. package/kits/physics-3d/engine3d/actornode.js +86 -0
  307. package/kits/physics-3d/engine3d/animationBudget.js +23 -0
  308. package/kits/physics-3d/engine3d/editorWorld.js +6 -0
  309. package/kits/physics-3d/engine3d/emptynodes.js +232 -0
  310. package/kits/physics-3d/engine3d/framecap.js +96 -0
  311. package/kits/physics-3d/engine3d/gait.js +118 -0
  312. package/kits/physics-3d/engine3d/kithooks.js +47 -0
  313. package/kits/physics-3d/engine3d/look.js +507 -48
  314. package/kits/physics-3d/engine3d/lookbake.js +208 -0
  315. package/kits/physics-3d/engine3d/materialpresets.js +2 -0
  316. package/kits/physics-3d/engine3d/motion.js +39 -10
  317. package/kits/physics-3d/engine3d/nearshadows.js +37 -19
  318. package/kits/physics-3d/engine3d/optimize.js +9 -5
  319. package/kits/physics-3d/engine3d/orbitinput.js +487 -9
  320. package/kits/physics-3d/engine3d/orbitlens.js +48 -0
  321. package/kits/physics-3d/engine3d/ownmodel.js +213 -0
  322. package/kits/physics-3d/engine3d/pxmodel.js +42 -11
  323. package/kits/physics-3d/engine3d/shadowfade.js +882 -0
  324. package/kits/physics-3d/engine3d/sky.js +547 -0
  325. package/kits/physics-3d/engine3d/skylife.js +306 -0
  326. package/kits/physics-3d/engine3d/unchanged.js +11 -0
  327. package/kits/physics-3d/engine3d/viewrange.js +52 -0
  328. package/kits/physics-3d/engine3d/world3d.js +796 -69
  329. package/kits/physics-3d/main.jsx +5 -0
  330. package/kits/physics-3d/package.json +1 -1
  331. package/kits/physics-3d/scripts/look-baseline.mjs +155 -0
  332. package/kits/physics-3d/scripts/test-camera-orbit.mjs +2563 -80
  333. package/kits/physics-3d/scripts/test-drop-empty-parts.mjs +1 -1
  334. package/kits/physics-3d/scripts/test-frame-cap-bob.mjs +435 -0
  335. package/kits/physics-3d/scripts/test-frame-cap.mjs +452 -0
  336. package/kits/physics-3d/scripts/test-gait.mjs +387 -0
  337. package/kits/physics-3d/scripts/test-look-bake.mjs +1056 -0
  338. package/kits/physics-3d/scripts/test-model-parse-cache.mjs +92 -0
  339. package/kits/physics-3d/scripts/test-near-shadows.mjs +3 -0
  340. package/kits/physics-3d/scripts/test-orbit-shadow-fade.mjs +1920 -0
  341. package/kits/physics-3d/scripts/test-orbit-shift-lock.mjs +545 -0
  342. package/kits/physics-3d/scripts/test-orbit-shot.mjs +583 -0
  343. package/kits/physics-3d/scripts/test-orbit-view.mjs +230 -0
  344. package/kits/physics-3d/scripts/test-skip-empty-nodes.mjs +845 -0
  345. package/kits/physics-3d/scripts/test-sky.mjs +2470 -0
  346. package/kits/physics-3d/scripts/test-static-actors.mjs +8 -1
  347. package/kits/physics-3d/scripts/test-static-scenery.mjs +2171 -0
  348. package/kits/physics-3d/scripts/test-steady-hover.mjs +2 -0
  349. package/kits/physics-3d/scripts/test-view-range.mjs +260 -0
  350. package/kits/physics-3d/systems/physics3d.js +383 -6
  351. package/kits/real-time/castle.json +2 -2
  352. package/kits/real-time/code/server/gameHooks.js +7 -0
  353. package/kits/real-time/code/server/session.js +97 -19
  354. package/package.json +2 -2
  355. package/dist/shell/assets/index-Cf61cNfT.css +0 -1
@@ -0,0 +1,918 @@
1
+ // Throwing under the engine's orbit camera key (code/client/grab.js,
2
+ // `scene.cameraControl === 'orbit'`), on a fake scene, Player, multiplayer kit
3
+ // and chrome:
4
+ // - Without the key every frame is what it was: a seeded run of frames
5
+ // (presses, turns, uneven frames, objects moving, handed on, hidden, the
6
+ // player unable to act, keepHeld and steady places) gives the same body
7
+ // calls, sends, prompts and transforms as `grab.before-orbit.mjs`, which is
8
+ // code/client/grab.js as it was on the commit before the key, copied whole,
9
+ // and never sets `scene.faceRequest`.
10
+ // - With it a throw goes along PI - camAngle: the press asks the Player to
11
+ // face that way, the held object swings round the player to the hold point
12
+ // along it, and the throw is let go only once a movement report carrying
13
+ // the new facing has gone out (or AIM_WAIT_MS after the press without
14
+ // one) and the object is there, or AIM_GIVE_UP_MS after the press whatever
15
+ // happened; anything that ends a hold meanwhile ends it with no throw.
16
+ // - Against multiplayer-3d's own setObjectPoses (the owned-object move
17
+ // bound) with reports bunched between server steps, a stall, ticks that
18
+ // run no physics step, carry movement not yet acknowledged at the press and
19
+ // uneven frames, the swing never puts the object more than AIM_SLACK_M
20
+ // from anywhere the server's body may be, so the object is never taken
21
+ // away mid-throw and the throw reaches the server while the object is
22
+ // still the player's. The
23
+ // fake multiplayer kit of the tests before that has no acknowledgements,
24
+ // so its swings are the unbounded ones at the carry speed.
25
+ // `npm run check` runs it; it needs nothing installed.
26
+
27
+ import assert from 'node:assert/strict';
28
+ import { readFileSync } from 'node:fs';
29
+ import { register } from 'node:module';
30
+ import { AIM_GIVE_UP_MS, AIM_SLACK_M, AIM_WAIT_MS, KEEP_TELL_MS, makeGrab } from '../code/client/grab.js';
31
+ import { hub } from '../code/client/state.js';
32
+ import { GRAB_DEFAULTS } from '../code/server/manifest.js';
33
+ import { THROW_REACH } from '../code/server/grab.js';
34
+
35
+ let clock = 1000;
36
+ Object.defineProperty(globalThis, 'performance', {
37
+ value: { now: () => clock },
38
+ configurable: true,
39
+ });
40
+
41
+ // The copy imports './state.js' as the live module does; it is pointed at the
42
+ // same file, so both read one `hub`.
43
+ const beforeSource = readFileSync(new URL('./grab.before-orbit.mjs', import.meta.url), 'utf8');
44
+ assert.ok(!beforeSource.includes('orbit') && beforeSource.includes("from './state.js'"));
45
+ const { makeGrab: makeGrabBefore } = await import(
46
+ `data:text/javascript,${encodeURIComponent(
47
+ beforeSource.replace("from './state.js'", `from '${new URL('../code/client/state.js', import.meta.url)}'`),
48
+ )}`
49
+ );
50
+
51
+ const DEG = Math.PI / 180;
52
+ const PILLOW = { kind: 'sphere', radius: 0.3 };
53
+ const SEND_MS = 50; // the multiplayer kit's MOVE_HZ, 20
54
+
55
+ // One client in a place, in the engine's order each frame: the Player
56
+ // behavior, which takes a facing request once (physics-3d updateOrbiting);
57
+ // the client's physics (`physics(rig, ms)`); the hub, whose grab runs after
58
+ // that physics step; and the multiplayer kit, which runs after the hub and
59
+ // sends a movement report at most every 50 ms, packing the bodies as the hub
60
+ // left them (`onReport(rig)` sees each report as it goes out, before
61
+ // `moveSeq` counts it). A rig without `physics` or `onReport` has neither.
62
+ function rig(
63
+ grab,
64
+ objects,
65
+ { make = makeGrab, cameraControl, persistent, moveSeq = true, player = 'orbit', physics, onReport } = {},
66
+ ) {
67
+ hub.place = grab === null ? {} : { grab };
68
+ hub.hidden.clear();
69
+ hub.held.clear();
70
+ hub.grabEvents.length = 0;
71
+ const log = [];
72
+ const prompts = [];
73
+ const wire = [];
74
+ let press = null;
75
+ const chrome = {
76
+ onGrab: (fn) => (press = fn),
77
+ grabPrompt: (text) => {
78
+ prompts.push(text);
79
+ log.push(['prompt', text]);
80
+ },
81
+ };
82
+ const g = make(chrome);
83
+ const actors = new Map();
84
+ const bodies = new Map();
85
+ for (const [id, o] of Object.entries(objects)) {
86
+ const transform = { x: o.x, y: o.y ?? 0.85, z: o.z };
87
+ actors.set(id, {
88
+ components: { Transform: transform, Shape: o.shape ?? PILLOW, Body: { type: 'dynamic' } },
89
+ });
90
+ const body = {
91
+ v: { x: 0, y: 0, z: 0 },
92
+ translation: () => ({ x: transform.x, y: transform.y, z: transform.z }),
93
+ isDynamic: () => true,
94
+ linvel: () => ({ ...body.v }),
95
+ setTranslation(p, wake) {
96
+ log.push(['setTranslation', id, { ...p }, wake]);
97
+ Object.assign(transform, p);
98
+ },
99
+ setLinvel(v, wake) {
100
+ log.push(['setLinvel', id, { ...v }, wake]);
101
+ body.v = { ...v };
102
+ },
103
+ setAngvel(v, wake) {
104
+ log.push(['setAngvel', id, { ...v }, wake]);
105
+ },
106
+ };
107
+ bodies.set(id, body);
108
+ }
109
+ const scene = {
110
+ getActor: (id) => actors.get(id) ?? null,
111
+ physics3d: { ready: () => true, bodyOf: (id) => bodies.get(id) ?? null },
112
+ persistent,
113
+ };
114
+ if (cameraControl !== undefined) {
115
+ scene.cameraControl = cameraControl;
116
+ }
117
+ const mine = new Set(actors.keys());
118
+ const mp = {
119
+ net: {
120
+ selfId: () => 'me',
121
+ send: (d) => {
122
+ log.push(['send', d]);
123
+ wire.push({ send: d });
124
+ },
125
+ },
126
+ own: { mine },
127
+ claimable: () => [...actors].map(([id, a]) => [id, a.components.Transform]),
128
+ };
129
+ if (moveSeq) {
130
+ mp.moveSeq = 0;
131
+ }
132
+ let sentAt = -Infinity;
133
+ const me = { x: 0, y: 0.85, z: 0, rotationY: 0 };
134
+ const rig = { local: { components: { Transform: me } } };
135
+ const step = (ms = 16, pressed = false, idle = true) => {
136
+ clock += ms;
137
+ if (player === 'orbit' && scene.faceRequest !== undefined) {
138
+ if (Number.isFinite(scene.faceRequest)) {
139
+ rig.local.components.Transform.rotationY = scene.faceRequest / DEG;
140
+ }
141
+ scene.faceRequest = undefined;
142
+ }
143
+ physics?.(rig, ms);
144
+ if (pressed) {
145
+ press();
146
+ }
147
+ g.frame(scene, mp, rig.local, idle);
148
+ if (clock - sentAt >= SEND_MS) {
149
+ sentAt = clock;
150
+ onReport?.(rig);
151
+ if (moveSeq) {
152
+ mp.moveSeq += 1;
153
+ }
154
+ wire.push({ facing: rig.local.components.Transform.rotationY });
155
+ }
156
+ log.push(['transform', { ...me }, scene.faceRequest]);
157
+ return prompts.at(-1);
158
+ };
159
+ const throws = () => wire.filter((w) => w.send?.op === 'throw');
160
+ Object.assign(rig, { g, step, me, mine, scene, mp, actors, bodies, log, wire, throws, rig });
161
+ return rig;
162
+ }
163
+
164
+ function at(distance, deg) {
165
+ return { x: Math.sin(deg * DEG) * distance, z: Math.cos(deg * DEG) * distance };
166
+ }
167
+
168
+ // A small seeded generator, so the run is the same every time.
169
+ function seeded(seed) {
170
+ let s = seed >>> 0;
171
+ return () => {
172
+ s = (s * 1664525 + 1013904223) >>> 0;
173
+ return s / 2 ** 32;
174
+ };
175
+ }
176
+
177
+ let n = 0;
178
+ function test(name, fn) {
179
+ try {
180
+ fn();
181
+ } catch (err) {
182
+ console.error(`test-grab-orbit: ${name}`);
183
+ throw err;
184
+ }
185
+ n += 1;
186
+ }
187
+
188
+ // A scripted run: the same random choices each time for the same seed.
189
+ function script(r, seed, frames = 900) {
190
+ const rand = seeded(seed);
191
+ for (let i = 0; i < frames; i += 1) {
192
+ const roll = rand();
193
+ if (roll < 0.04) {
194
+ r.me.rotationY = (rand() - 0.5) * 400;
195
+ } else if (roll < 0.06) {
196
+ r.me.x = (rand() - 0.5) * 2;
197
+ r.me.z = (rand() - 0.5) * 2;
198
+ } else if (roll < 0.07) {
199
+ r.mine.has('a') ? r.mine.delete('a') : r.mine.add('a');
200
+ } else if (roll < 0.075) {
201
+ hub.hidden.has('b') ? hub.hidden.delete('b') : hub.hidden.add('b');
202
+ } else if (roll < 0.08) {
203
+ hub.grabEvents.push({ k: 'hub-drop', actor: 'a' });
204
+ } else if (roll < 0.09) {
205
+ const t = r.actors.get('b').components.Transform;
206
+ const p = at(0.8 + rand() * 1.5, (rand() - 0.5) * 120);
207
+ t.x = r.me.x + p.x;
208
+ t.z = r.me.z + p.z;
209
+ }
210
+ const ms = rand() < 0.1 ? rand() * 120 : 8 + rand() * 25;
211
+ r.step(ms, rand() < 0.12, rand() > 0.03);
212
+ }
213
+ }
214
+
215
+ test('without the key: a seeded run is the code before the key, call for call, and never asks for a facing', () => {
216
+ const places = [
217
+ { ...GRAB_DEFAULTS },
218
+ { ...GRAB_DEFAULTS, steady: true, reach: 2.2 },
219
+ { ...GRAB_DEFAULTS, keepHeld: true, steady: true, holdDistance: 0.9, throwSpeed: 14 },
220
+ { ...GRAB_DEFAULTS, reach: 2, holdDistance: 3, holdHeight: 2.5 },
221
+ ];
222
+ // No key, a null one (what the engine latches without it), and anything
223
+ // that is not exactly 'orbit'; with and without a camera angle.
224
+ const keys = [undefined, null, 'Orbit', 'free'];
225
+ let throws = 0;
226
+ for (const [pi, grab] of places.entries()) {
227
+ for (const [ki, cameraControl] of keys.entries()) {
228
+ const seed = 1 + pi * 10 + ki;
229
+ const objects = { a: at(1.2, 0), b: at(1.5, 20) };
230
+ const persistent = ki % 2 === 0 ? { camAngle: 2.1 } : undefined;
231
+ const start = clock;
232
+ const now = rig(grab, objects, { cameraControl, persistent });
233
+ script(now, seed);
234
+ clock = start;
235
+ const before = rig(grab, objects, { cameraControl, persistent, make: makeGrabBefore });
236
+ script(before, seed);
237
+ assert.ok(now.log.length > 1500, String(now.log.length));
238
+ assert.deepEqual(now.log, before.log, `place ${pi}, key ${String(cameraControl)}`);
239
+ assert.deepEqual(now.wire, before.wire);
240
+ assert.ok(now.log.every((e) => e[0] !== 'transform' || e[2] === undefined));
241
+ assert.equal(now.scene.faceRequest, undefined);
242
+ throws += now.throws().length;
243
+ }
244
+ }
245
+ // The run threw, many times over.
246
+ assert.ok(throws > 50, String(throws));
247
+ });
248
+
249
+ // Grab the object in front with a press; returns the rig.
250
+ function holding(grab = { ...GRAB_DEFAULTS }, options = {}, objects = { a: at(1.2, 0) }) {
251
+ const r = rig(grab, objects, { cameraControl: 'orbit', persistent: { camAngle: 0 }, ...options });
252
+ r.step();
253
+ assert.equal(r.step(16, true), 'Throw');
254
+ assert.equal(r.g.holding(), 'a');
255
+ // Carried to its hold point in front.
256
+ for (let i = 0; i < 20; i += 1) {
257
+ r.step();
258
+ }
259
+ return r;
260
+ }
261
+
262
+ const flat = (p, q) => Math.hypot(p.x - q.x, p.z - q.z);
263
+
264
+ for (const grab of [{ ...GRAB_DEFAULTS }, { ...GRAB_DEFAULTS, holdDistance: 0.9, throwSpeed: 14, keepHeld: true, steady: true }]) test(`with the key (hold ${grab.holdDistance} m): the throw goes along PI - camAngle, the character is asked to face it, and the throw waits for the swing and a report of that facing`, () => {
265
+ const r = holding(grab);
266
+ const body = r.bodies.get('a');
267
+ const obj = r.actors.get('a').components.Transform;
268
+ assert.ok(obj.z > 0.8, 'held in front of the facing (+z)');
269
+ // The camera behind at camAngle 0 looks along -z: f = PI, behind the
270
+ // character, which faces +z.
271
+ const cam = 0;
272
+ const f = Math.PI - cam;
273
+ const pressAt = clock;
274
+ r.step(16, true);
275
+ assert.equal(r.scene.faceRequest, f);
276
+ assert.equal(r.throws().length, 0);
277
+ assert.equal(r.g.holding(), 'a');
278
+ // Frame by frame until it goes: still held, prompt Throw, never inside the
279
+ // player's capsule (radius 0.35 + the pillow's 0.3).
280
+ let frames = 0;
281
+ while (r.throws().length === 0) {
282
+ assert.equal(r.step(16), r.throws().length ? null : 'Throw');
283
+ assert.ok(flat(obj, r.me) >= 0.35 + 0.3, `${flat(obj, r.me)}`);
284
+ frames += 1;
285
+ assert.ok(frames < 60);
286
+ }
287
+ // Let go as soon as the swing (half a turn at 20 m/s) reached the hold
288
+ // point along f: a report of the facing went out long before.
289
+ const holdD = Math.max(grab.holdDistance, 0.35 + 0.3 + 0.15);
290
+ const swingMs = ((Math.PI * holdD) / 20) * 1000;
291
+ assert.ok(clock - pressAt >= swingMs && clock - pressAt <= swingMs + 40, `${clock - pressAt} ${swingMs}`);
292
+ assert.ok(Math.abs(obj.x - Math.sin(f) * holdD) < 1e-9 && Math.abs(obj.z - Math.cos(f) * holdD) < 1e-9);
293
+ assert.ok(flat(obj, r.me) < THROW_REACH);
294
+ // Along f at throwSpeed, a quarter up.
295
+ const v = body.v;
296
+ assert.ok(Math.abs(v.x - Math.sin(f) * grab.throwSpeed) < 1e-9);
297
+ assert.ok(Math.abs(v.z - Math.cos(f) * grab.throwSpeed) < 1e-9);
298
+ assert.equal(v.y, grab.throwSpeed * 0.25);
299
+ // On the wire: a movement report with the new facing went out before the throw.
300
+ const ti = r.wire.findIndex((w) => w.send?.op === 'throw');
301
+ const faced = r.wire.findIndex((w) => w.facing !== undefined && Math.abs(w.facing - 180) < 1e-9);
302
+ assert.ok(faced >= 0 && faced < ti, JSON.stringify(r.wire.slice(-8)));
303
+ // Exactly one throw, nothing more asked of the Player, the hold over.
304
+ assert.equal(r.g.holding(), null);
305
+ assert.equal(r.scene.faceRequest, undefined);
306
+ for (let i = 0; i < 30; i += 1) {
307
+ r.step();
308
+ }
309
+ assert.equal(r.throws().length, 1);
310
+ });
311
+
312
+ test('with the key: a camera already looking the way the character faces lets go at the next report, no swing', () => {
313
+ const r = holding({ ...GRAB_DEFAULTS }, { persistent: { camAngle: Math.PI } });
314
+ const obj = r.actors.get('a').components.Transform;
315
+ const held = { ...obj };
316
+ r.step(16, true);
317
+ assert.equal(r.throws().length, 0);
318
+ let frames = 0;
319
+ while (r.throws().length === 0) {
320
+ r.step(16);
321
+ frames += 1;
322
+ }
323
+ // One frame for the Player to take the request, up to a report's 50 ms,
324
+ // and the frame after it.
325
+ assert.ok(frames <= 6, String(frames));
326
+ assert.ok(Math.abs(obj.x - held.x) < 1e-9 && Math.abs(obj.z - held.z) < 1e-9);
327
+ assert.ok(Math.abs(r.bodies.get('a').v.z - 10) < 1e-9);
328
+ });
329
+
330
+ test('with the key: the release follows the report, not just the facing: a report that has not gone out yet holds the throw', () => {
331
+ const r = holding({ ...GRAB_DEFAULTS }, { persistent: { camAngle: Math.PI } });
332
+ // Frames of 5 ms: reports are 10 frames apart.
333
+ r.step(5, true);
334
+ const seq = r.mp.moveSeq;
335
+ let frames = 0;
336
+ while (r.throws().length === 0) {
337
+ r.step(5);
338
+ frames += 1;
339
+ }
340
+ assert.ok(r.mp.moveSeq > seq);
341
+ // The facing was taken the frame after the press; the throw waited for a report after it.
342
+ assert.ok(frames >= 2);
343
+ const ti = r.wire.findIndex((w) => w.send?.op === 'throw');
344
+ const lastReport = r.wire.slice(0, ti).findLastIndex((w) => w.facing !== undefined);
345
+ assert.ok(lastReport >= 0 && r.wire[lastReport].facing === 0);
346
+ });
347
+
348
+ test('with the key: no camera angle falls back to the facing', () => {
349
+ const r = holding({ ...GRAB_DEFAULTS }, { persistent: {} });
350
+ r.me.rotationY = 30;
351
+ for (let i = 0; i < 10; i += 1) {
352
+ r.step();
353
+ }
354
+ r.step(16, true);
355
+ assert.ok(Math.abs(r.scene.faceRequest - 30 * DEG) < 1e-12);
356
+ while (r.throws().length === 0) {
357
+ r.step();
358
+ }
359
+ const v = r.bodies.get('a').v;
360
+ assert.ok(Math.abs(v.x - Math.sin(30 * DEG) * 10) < 1e-9 && Math.abs(v.z - Math.cos(30 * DEG) * 10) < 1e-9);
361
+ });
362
+
363
+ test('with the key: the wait runs out -- no report counter, or a Player that never takes the facing -- and the throw still goes along f', () => {
364
+ for (const options of [{ moveSeq: false }, { player: 'none' }]) {
365
+ let pressAt;
366
+ // Half a turn: when the swing gets there, past the wait.
367
+ const r = holding({ ...GRAB_DEFAULTS }, options);
368
+ pressAt = clock;
369
+ r.step(16, true);
370
+ while (r.throws().length === 0) {
371
+ r.step(16);
372
+ }
373
+ const swingMs = ((Math.PI * 1.4) / 20) * 1000;
374
+ assert.ok(clock - pressAt >= swingMs && clock - pressAt <= swingMs + 40, String(clock - pressAt));
375
+ const v = r.bodies.get('a').v;
376
+ assert.ok(Math.abs(v.z + 10) < 1e-9 && Math.abs(v.x) < 1e-9);
377
+ assert.ok(Math.abs(r.actors.get('a').components.Transform.z + 1.4) < 1e-9);
378
+ // Camera aligned with the facing (no swing), no report counter: the
379
+ // throw goes at AIM_WAIT_MS. (A Player that never takes a request faces
380
+ // f already here, and lets go at the next report.)
381
+ if (options.moveSeq !== false) {
382
+ continue;
383
+ }
384
+ const aligned = holding({ ...GRAB_DEFAULTS }, { ...options, persistent: { camAngle: Math.PI } });
385
+ pressAt = clock;
386
+ aligned.step(16, true);
387
+ while (aligned.throws().length === 0) {
388
+ aligned.step(16);
389
+ }
390
+ // (Measured from before the press frame, 16 ms before the press.)
391
+ assert.ok(clock - pressAt >= AIM_WAIT_MS + 16 && clock - pressAt <= AIM_WAIT_MS + 32, String(clock - pressAt));
392
+ assert.ok(Math.abs(aligned.bodies.get('a').v.z - 10) < 1e-9);
393
+ }
394
+ // The longest hold (3 m), half a turn: under the give-up.
395
+ const far = holding({ ...GRAB_DEFAULTS, holdDistance: 3 });
396
+ const pressAt = clock;
397
+ far.step(16, true);
398
+ while (far.throws().length === 0) {
399
+ far.step(16);
400
+ }
401
+ assert.ok(clock - pressAt < AIM_GIVE_UP_MS, String(clock - pressAt));
402
+ });
403
+
404
+ test('with the key: a player who outruns the carry still throws, at AIM_GIVE_UP_MS', () => {
405
+ const r = holding();
406
+ const pressAt = clock;
407
+ r.step(16, true);
408
+ while (r.throws().length === 0) {
409
+ r.me.x += 0.8; // 50 m/s: the carry (20 m/s) never catches up
410
+ r.step(16);
411
+ assert.ok(clock - pressAt <= AIM_GIVE_UP_MS + 32);
412
+ }
413
+ assert.ok(clock - pressAt >= AIM_GIVE_UP_MS + 16);
414
+ const v = r.bodies.get('a').v;
415
+ assert.ok(Math.abs(v.z + 10) < 1e-9 && Math.abs(v.x) < 1e-9);
416
+ });
417
+
418
+ test('with the key: a second press while it waits throws nothing more', () => {
419
+ const r = holding();
420
+ r.step(16, true);
421
+ r.step(16, true);
422
+ r.step(16, true);
423
+ while (r.throws().length === 0) {
424
+ r.step(16, true);
425
+ }
426
+ // One throw, from the first press, along it; the presses that came while
427
+ // it waited grabbed nothing and threw nothing, and nothing is offered for
428
+ // REGRAB_MS after it (the object, which this fake does not fly, is still
429
+ // in reach).
430
+ assert.equal(r.throws().length, 1);
431
+ assert.ok(Math.abs(r.bodies.get('a').v.z + 10) < 1e-9);
432
+ for (let i = 0; i < 20; i += 1) {
433
+ assert.equal(r.step(16, i % 2 === 0), null);
434
+ }
435
+ assert.equal(r.throws().length, 1);
436
+ assert.equal(r.wire.filter((w) => w.send?.op === 'grab').length, 1);
437
+ });
438
+
439
+ test('with the key: a hold ended while the throw waits sends no throw', () => {
440
+ const cases = {
441
+ drop: (r) => hub.grabEvents.push({ k: 'hub-drop', actor: 'a' }),
442
+ deny: (r) => hub.grabEvents.push({ k: 'hub-grab-deny', actor: 'a' }),
443
+ handedOn: (r) => r.mine.delete('a'),
444
+ hidden: () => hub.hidden.add('a'),
445
+ removed: (r) => r.actors.delete('a'),
446
+ newSession: (r) => (r.mp.net = { ...r.mp.net }),
447
+ load: (r) => (r.rig.local = { components: { Transform: { ...r.me } } }),
448
+ };
449
+ for (const [name, end] of Object.entries(cases)) {
450
+ const r = holding();
451
+ r.step(16, true);
452
+ r.step(16);
453
+ end(r);
454
+ r.step(16);
455
+ assert.equal(r.g.holding(), null, name);
456
+ for (let i = 0; i < 40; i += 1) {
457
+ r.step(16);
458
+ }
459
+ assert.equal(r.throws().length, 0, name);
460
+ }
461
+ // A player who cannot act (a trip, the talk panel) for a frame.
462
+ const r = holding();
463
+ r.step(16, true);
464
+ r.step(16, false, false);
465
+ assert.equal(r.g.holding(), null);
466
+ for (let i = 0; i < 40; i += 1) {
467
+ r.step(16);
468
+ }
469
+ assert.equal(r.throws().length, 0);
470
+ });
471
+
472
+ test('with the key, keepHeld (v1.25): the hold is still told while the throw waits', () => {
473
+ const r = holding({ ...GRAB_DEFAULTS, keepHeld: true }, { moveSeq: false });
474
+ const holds = () => r.wire.filter((w) => w.send?.op === 'hold').length;
475
+ // Hold until a word is just due, then press.
476
+ while (holds() === 0) {
477
+ r.step(16);
478
+ }
479
+ for (let t = 0; t < KEEP_TELL_MS - 40; t += 16) {
480
+ r.step(16);
481
+ }
482
+ const before = holds();
483
+ r.step(16, true);
484
+ while (r.throws().length === 0) {
485
+ r.step(16);
486
+ }
487
+ assert.equal(holds(), before + 1);
488
+ const ti = r.wire.findIndex((w) => w.send?.op === 'throw');
489
+ assert.ok(r.wire.findLastIndex((w) => w.send?.op === 'hold') < ti);
490
+ for (let i = 0; i < 100; i += 1) {
491
+ r.step(33);
492
+ }
493
+ assert.equal(holds(), before + 1);
494
+ });
495
+
496
+ // ---- The swing against multiplayer-3d's real move bound ---------------------
497
+ //
498
+ // multiplayer-3d's own `setObjectPoses` and `acks` (code/server/ownership.js),
499
+ // on a stand-in for its kinematic body (a target set now is where the body is
500
+ // after the next physics step) and its session (30 Hz ticks of two 60 Hz
501
+ // steps; the reports a tick has received are applied before its steps, and
502
+ // its broadcast after them carries the acknowledgements and handovers). The
503
+ // network between holds reports back and hands them over in bunches -- two,
504
+ // three, a stall's worth, to one tick -- and the client's frames are uneven.
505
+ // Every report, the throw included, goes on the one ordered uplink.
506
+
507
+ const kits = new URL('../../', import.meta.url);
508
+ register(
509
+ `data:text/javascript,${encodeURIComponent(`
510
+ const kits = ${JSON.stringify(kits.href)};
511
+ const rapier = 'export default { RigidBodyType: { Dynamic: 0, Fixed: 1, KinematicPositionBased: 2 } };';
512
+ export async function resolve(specifier, context, next) {
513
+ if (specifier === '@dimforge/rapier3d-compat') {
514
+ return { url: 'data:text/javascript,' + encodeURIComponent(rapier), shortCircuit: true };
515
+ }
516
+ if (specifier.startsWith('@imports/castle.')) {
517
+ return { url: kits + specifier.slice('@imports/castle.'.length), shortCircuit: true };
518
+ }
519
+ return next(specifier, context);
520
+ }
521
+ `)}`,
522
+ );
523
+ const Ownership = await import('../../multiplayer-3d/code/server/ownership.js');
524
+ const Limits = await import('../../multiplayer-3d/code/server/limits.js');
525
+
526
+ function kinematic(at) {
527
+ const body = {
528
+ at: { ...at },
529
+ next: null,
530
+ type: 2,
531
+ translation: () => ({ ...body.at }),
532
+ setNextKinematicTranslation: (p) => (body.next = { ...p }),
533
+ setNextKinematicRotation() {},
534
+ setBodyType: (type) => (body.type = type),
535
+ setTranslation: (p) => (body.at = { ...p }),
536
+ setRotation() {},
537
+ setLinvel() {},
538
+ setAngvel() {},
539
+ linvel: () => ({ x: 0, y: 0, z: 0 }),
540
+ angvel: () => ({ x: 0, y: 0, z: 0 }),
541
+ step() {
542
+ if (body.next) {
543
+ body.at = body.next;
544
+ body.next = null;
545
+ }
546
+ },
547
+ };
548
+ return body;
549
+ }
550
+
551
+ const TICK_MS = 1000 / 30;
552
+
553
+ // A held object, a server that granted it, and the network between them.
554
+ // `deliver(tick)` says whether a tick hands the server what has reached it
555
+ // (otherwise it waits for a later one), from when `seen.bunching` is set
556
+ // (every tick until then); `upMs` and `downMs` are the one-way delays.
557
+ // `acks: false` leaves the acknowledgements off the multiplayer kit, as one
558
+ // from before them: the swing is the unbounded one it was.
559
+ function networked(
560
+ grab,
561
+ { deliver = () => true, upMs = 30, downMs = 30, camAngle = 0, acks = true, steps = () => 2 } = {},
562
+ ) {
563
+ const uplink = [];
564
+ const downlink = [];
565
+ const seen = { reports: 0, worst: 0, handovers: [], throws: [], bunches: [], bunching: false };
566
+ let sim = null;
567
+ // The client's physics (physics-3d's physics3d system), before the hub:
568
+ // fixed 60 Hz steps from an accumulator, at most four a frame (a frame that
569
+ // takes four drops the rest of its time). The held object moves on at the
570
+ // velocity the hub left it with, and falls.
571
+ let acc = 0;
572
+ const physics = (r, ms) => {
573
+ acc += ms / 1000;
574
+ let steps = 0;
575
+ while (acc >= 1 / 60 && steps < 4) {
576
+ const h = 1 / 60;
577
+ for (const [id, body] of r.bodies) {
578
+ const t = r.actors.get(id)?.components.Transform;
579
+ if (!t || !r.mine.has(id)) {
580
+ continue;
581
+ }
582
+ t.x += body.v.x * h;
583
+ t.y += body.v.y * h - 0.5 * 9.81 * h * h;
584
+ t.z += body.v.z * h;
585
+ body.v = { ...body.v, y: body.v.y - 9.81 * h };
586
+ }
587
+ acc -= h;
588
+ steps += 1;
589
+ }
590
+ if (steps === 4) {
591
+ acc = 0;
592
+ }
593
+ };
594
+ const onReport = (r) => {
595
+ const t = r.actors.get('a').components.Transform;
596
+ const v = r.bodies.get('a').v;
597
+ if (!r.mine.has('a')) {
598
+ return;
599
+ }
600
+ const seq = r.mp.moveSeq + 1;
601
+ const ack = r.mp.acks?.get('a');
602
+ uplink.push({
603
+ at: clock + upMs,
604
+ pose: ['a', t.x, t.y, t.z, 0, 0, 0, 1, v.x, v.y, v.z, 0, 0, 0, 1, seq, ack ? ack[1] : 0],
605
+ });
606
+ };
607
+ const r = rig(grab, { a: at(1.2, 0) }, { cameraControl: 'orbit', persistent: { camAngle }, physics, onReport });
608
+ if (acks) {
609
+ r.mp.acks = new Map();
610
+ }
611
+ hub.lastT = 0;
612
+ r.mp.handed = new Map([['a', { epoch: 1 }]]);
613
+ // Every message the hub sends goes on the uplink after the reports before it.
614
+ const send = r.mp.net.send;
615
+ r.mp.net.send = (d) => {
616
+ send(d);
617
+ if (d.op === 'throw') {
618
+ seen.thrownFrom = { ...r.actors.get('a').components.Transform };
619
+ seen.thrownAt = clock;
620
+ }
621
+ uplink.push({ at: clock + upMs, message: d });
622
+ };
623
+ const start = r.actors.get('a').components.Transform;
624
+ sim = {
625
+ timeMs: 0,
626
+ players: new Map([['me', {}]]),
627
+ objects: new Map([
628
+ ['a', { owner: 'me', epoch: 1, claimable: true, body: kinematic(start), reportedAt: 0, claimedAt: 0 }],
629
+ ]),
630
+ };
631
+ let tick = 0;
632
+ let nextTick = clock + TICK_MS;
633
+ const server = () => {
634
+ while (nextTick <= clock) {
635
+ tick += 1;
636
+ const handovers = [];
637
+ if (!seen.bunching || deliver(tick)) {
638
+ let bunch = 0;
639
+ while (uplink.length && uplink[0].at <= nextTick) {
640
+ const m = uplink.shift();
641
+ const entry = sim.objects.get('a');
642
+ if (m.pose && seen.throws.length > 0) {
643
+ // After the throw: the object's flight, which the server may take
644
+ // over (with its velocity) as it likes. Not followed here.
645
+ } else if (m.pose && entry.owner !== 'me') {
646
+ // Handed back to the server: the hold is over, and the reports
647
+ // still on the way are from a grant that has ended.
648
+ } else if (m.pose) {
649
+ const at = entry.body.translation();
650
+ const elapsed = Math.max(0, sim.timeMs - entry.reportedAt) / 1000;
651
+ const d = Math.hypot(m.pose[1] - at.x, m.pose[2] - at.y, m.pose[3] - at.z);
652
+ seen.worst = Math.max(seen.worst, d - Limits.MAX_OBJECT_SPEED * elapsed);
653
+ seen.reports += 1;
654
+ bunch += 1;
655
+ Ownership.setObjectPoses(sim, 'me', [m.pose], handovers);
656
+ } else if (m.message.op === 'throw') {
657
+ // What the hub server checks of a throw before it lets it fly
658
+ // (code/server/grab.js): the object is still this player's.
659
+ seen.throws.push({ owned: entry.owner === 'me', at: entry.body.translation() });
660
+ }
661
+ }
662
+ if (seen.bunching) {
663
+ seen.bunches.push(bunch);
664
+ }
665
+ }
666
+ // Physics steps: two a tick, unless `steps(tick)` says otherwise (a
667
+ // tick that ran no step still acknowledges what it applied).
668
+ for (let i = 0, k = seen.bunching ? steps(tick) : 2; i < k; i += 1) {
669
+ sim.objects.get('a').body.step();
670
+ sim.timeMs += 1000 / 60;
671
+ }
672
+ seen.handovers.push(...handovers);
673
+ const acks = Ownership.acks(sim);
674
+ if (acks.length || handovers.length) {
675
+ downlink.push({ at: nextTick + downMs, acks, handovers, t: sim.timeMs });
676
+ }
677
+ nextTick += TICK_MS;
678
+ }
679
+ };
680
+ const client = () => {
681
+ while (downlink.length && downlink[0].at <= clock) {
682
+ const m = downlink.shift();
683
+ // The broadcast's simulation time, as the hub's state hook keeps it.
684
+ hub.lastT = m.t;
685
+ for (const [id, ...ack] of m.acks) {
686
+ r.mp.acks?.set(id, ack);
687
+ }
688
+ for (const [id, owner] of m.handovers) {
689
+ if (owner !== 'me') {
690
+ r.mine.delete(id);
691
+ }
692
+ }
693
+ }
694
+ };
695
+ const step = r.step;
696
+ r.step = (ms = 16, pressed = false, idle = true) => {
697
+ // Up to the moment this frame starts: the server's ticks, then what has
698
+ // reached the client.
699
+ const was = clock;
700
+ clock += ms;
701
+ server();
702
+ client();
703
+ clock = was;
704
+ return step(ms, pressed, idle);
705
+ };
706
+ return { r, seen, sim, tick: () => tick };
707
+ }
708
+
709
+ // Grab, carry to the hold point, and press Throw with the camera looking
710
+ // behind the character; frames from `frames(i)` ms. Returns when the throw
711
+ // has reached the server, or the hold is gone.
712
+ function throwBehind(grab, network, frames) {
713
+ const { r, seen, sim } = networked(grab, network);
714
+ r.step();
715
+ assert.equal(r.step(16, true), 'Throw');
716
+ for (let i = 0; i < 40; i += 1) {
717
+ r.step();
718
+ }
719
+ assert.equal(seen.handovers.length, 0, 'the carry itself is inside the bound');
720
+ // The bunches start with the press. (A pickup's ease-in at the carry speed,
721
+ // the same with or without the key, is no more bunch-proof than it was.)
722
+ seen.bunching = true;
723
+ const pressAt = clock;
724
+ r.step(16, true);
725
+ let i = 0;
726
+ while (seen.throws.length === 0 && r.mine.has('a') && i < 1000) {
727
+ r.step(frames(i));
728
+ i += 1;
729
+ }
730
+ // How long after the press the client let go.
731
+ return { r, seen, sim, ms: seen.thrownAt - pressAt };
732
+ }
733
+
734
+ const lumpy = (i) => (i % 17 === 5 ? 120 : i % 5 === 2 ? 33 : 16);
735
+
736
+ for (const [name, network] of Object.entries({
737
+ 'two reports to a tick': { deliver: (tick) => tick % 3 === 0 },
738
+ 'three reports to a tick': { deliver: (tick) => tick % 5 === 0 },
739
+ 'a stall, then a bunch': { deliver: (tick) => tick % 12 === 0 || tick % 12 > 8, upMs: 60, downMs: 60 },
740
+ 'a long stall': { deliver: (tick) => tick % 40 === 0 },
741
+ })) {
742
+ for (const grab of [
743
+ { ...GRAB_DEFAULTS },
744
+ { ...GRAB_DEFAULTS, holdDistance: 3 },
745
+ { ...GRAB_DEFAULTS, holdDistance: 0.9, throwSpeed: 14, keepHeld: true, steady: true },
746
+ ]) {
747
+ test(`with the key, ${name} (hold ${grab.holdDistance} m): the swing keeps inside multiplayer-3d's move bound and the throw reaches the server`, () => {
748
+ const { r, seen, ms } = throwBehind(grab, network, lumpy);
749
+ // Bunches came: some tick was handed two reports or more at once.
750
+ assert.ok(Math.max(...seen.bunches) >= 2, JSON.stringify(seen.bunches));
751
+ // Never past the 0.6 m a zero-time report gets, from the server's body.
752
+ assert.ok(seen.worst <= Limits.MOVE_SLACK, String(seen.worst));
753
+ // No handover before the throw (after it, the server may take the
754
+ // flight over as it likes).
755
+ assert.deepEqual(seen.handovers, []);
756
+ // Thrown once, while the server still had it the player's.
757
+ assert.equal(r.throws().length, 1);
758
+ assert.equal(seen.throws.length, 1);
759
+ assert.equal(seen.throws[0].owned, true);
760
+ assert.ok(ms <= AIM_GIVE_UP_MS + 200, String(ms));
761
+ // Along f = PI (behind), at the place's throw speed.
762
+ const v = r.bodies.get('a').v;
763
+ assert.ok(Math.abs(v.x) < 1e-9 && Math.abs(v.z + grab.throwSpeed) < 1e-9, JSON.stringify(v));
764
+ const holdD = Math.max(grab.holdDistance, 0.35 + 0.3 + 0.15);
765
+ if (ms < AIM_GIVE_UP_MS) {
766
+ // From the hold point behind: the swing got there before the give-up.
767
+ assert.ok(flat(seen.thrownFrom, { x: 0, z: -holdD }) < 1e-6, JSON.stringify(seen.thrownFrom));
768
+ // The server's body, a report or two behind, within its throw reach.
769
+ assert.ok(flat(seen.throws[0].at, r.me) < THROW_REACH);
770
+ } else {
771
+ // The server heard too little of the swing in time: let go at the
772
+ // give-up, from where it had got to, still the player's.
773
+ assert.ok(name !== 'two reports to a tick' || grab.holdDistance > 1.4, String(ms));
774
+ }
775
+ });
776
+ }
777
+ }
778
+
779
+ test('with the key, three reports to a tick, a multiplayer kit without acknowledgements: the unbounded swing loses the object and the throw (what the bound is for)', () => {
780
+ const { r, seen } = throwBehind({ ...GRAB_DEFAULTS }, { deliver: (tick) => tick % 5 === 0, acks: false }, lumpy);
781
+ assert.ok(seen.handovers.length > 0);
782
+ assert.ok(seen.worst > Limits.MOVE_SLACK, String(seen.worst));
783
+ assert.equal(r.g.holding(), null);
784
+ assert.equal(seen.throws.length, 0);
785
+ assert.equal(r.throws().length, 0);
786
+ });
787
+
788
+ test('with the key, an ordinary connection: half a turn at the default hold takes about a second, at most AIM_SLACK_M from where the server\'s body may be', () => {
789
+ const { r, seen, ms } = throwBehind({ ...GRAB_DEFAULTS }, {}, () => 16);
790
+ assert.deepEqual(seen.handovers, []);
791
+ assert.equal(seen.throws.length, 1);
792
+ assert.equal(seen.throws[0].owned, true);
793
+ assert.ok(seen.worst <= AIM_SLACK_M + 0.05, String(seen.worst));
794
+ // Slower than the carry speed's 220 ms (the server's acknowledgements set
795
+ // the pace), well inside the give-up.
796
+ assert.ok(ms > 400 && ms < 1500, String(ms));
797
+ assert.ok(flat(seen.thrownFrom, { x: 0, z: -1.4 }) < 1e-6, JSON.stringify(seen.thrownFrom));
798
+ });
799
+
800
+ // Codex review 2, finding 1: carry movement the server has not acknowledged
801
+ // yet, from before the press, counts against the swing. The player steps
802
+ // 0.3 m aside just before pressing; the carry follows in reports the server
803
+ // has not acknowledged, and the swing may not then add a fresh 0.5 m to them.
804
+ test('with the key, three reports to a tick: carry movement not yet acknowledged when Throw is pressed counts against the swing', () => {
805
+ const { r, seen } = networked({ ...GRAB_DEFAULTS }, { deliver: (tick) => tick % 5 === 0 });
806
+ r.step();
807
+ assert.equal(r.step(16, true), 'Throw');
808
+ for (let i = 0; i < 40; i += 1) {
809
+ r.step();
810
+ }
811
+ assert.equal(seen.handovers.length, 0);
812
+ seen.bunching = true;
813
+ for (let i = 0; i < 6; i += 1) {
814
+ r.me.x -= 0.05;
815
+ r.step();
816
+ }
817
+ r.step(16, true);
818
+ let i = 0;
819
+ while (seen.throws.length === 0 && r.mine.has('a') && i < 1000) {
820
+ r.step();
821
+ i += 1;
822
+ }
823
+ assert.ok(seen.worst <= Limits.MOVE_SLACK, String(seen.worst));
824
+ assert.deepEqual(seen.handovers, []);
825
+ assert.equal(seen.throws.length, 1);
826
+ assert.equal(seen.throws[0].owned, true);
827
+ });
828
+
829
+ // Codex review 2, finding 2: an acknowledgement is not a body moved. A tick
830
+ // that applies and acknowledges reports but runs no physics step leaves the
831
+ // server's body where it was; the next report is measured from there with no
832
+ // time allowed. One such tick anywhere in the swing -- or two or three back to
833
+ // back -- on a link with no delay and 5 ms frames (reports ten frames apart),
834
+ // loses nothing.
835
+ for (const run of [1, 2, 3]) {
836
+ test(`with the key, ${run} tick(s) in a row that run no physics step: the swing does not take an acknowledgement as the body moved`, () => {
837
+ let swung = 0;
838
+ for (let skip = 1; skip <= 20; skip += 1) {
839
+ let first = null;
840
+ const { r, seen, tick } = networked(
841
+ { ...GRAB_DEFAULTS },
842
+ {
843
+ upMs: 0,
844
+ downMs: 0,
845
+ steps: (k) => {
846
+ first ??= k;
847
+ return k >= first + skip && k < first + skip + run ? 0 : 2;
848
+ },
849
+ },
850
+ );
851
+ r.step(5);
852
+ assert.equal(r.step(5, true), 'Throw');
853
+ for (let i = 0; i < 150; i += 1) {
854
+ r.step(5);
855
+ }
856
+ assert.equal(seen.handovers.length, 0);
857
+ seen.bunching = true;
858
+ r.step(5, true);
859
+ let i = 0;
860
+ while (seen.throws.length === 0 && r.mine.has('a') && i < 2000) {
861
+ r.step(5);
862
+ i += 1;
863
+ }
864
+ assert.ok(seen.worst <= Limits.MOVE_SLACK, `stepless from tick ${skip}: ${seen.worst}`);
865
+ assert.deepEqual(seen.handovers, [], `stepless from tick ${skip}`);
866
+ assert.equal(seen.throws.length, 1, `stepless from tick ${skip}`);
867
+ assert.equal(seen.throws[0].owned, true);
868
+ assert.ok(flat(seen.thrownFrom, { x: 0, z: -1.4 }) < 1e-6, JSON.stringify(seen.thrownFrom));
869
+ if (tick() > first + skip) {
870
+ swung += 1;
871
+ }
872
+ }
873
+ // The swing on this link takes about a dozen ticks: most runs put the
874
+ // stepless ticks inside it.
875
+ assert.ok(swung >= 10, String(swung));
876
+ });
877
+ }
878
+
879
+ // Codex review 3: the trail holds each report's position as it went out --
880
+ // the body where the hub left it, read by the multiplayer kit straight after
881
+ // -- not the body a frame later, which the client's physics (before the hub)
882
+ // has moved on at the carry's velocity. The player steps 0.15 m aside over
883
+ // one 80 ms frame (four physics steps, the cap) and presses Throw on the next
884
+ // 80 ms frame: the carry report says one place, the body read back a frame
885
+ // later says another 0.125 m on, and a swing bounded by the second (turning
886
+ // the way the player stepped, -x) puts a report 0.620646 m from the server's
887
+ // body with no time allowed -- the object taken away, no throw.
888
+ test('with the key, three reports to a tick: the swing is bounded by where each report put the object, not where the client\'s physics moved it after', () => {
889
+ for (const [grab, dx] of [
890
+ [{ ...GRAB_DEFAULTS }, -0.15],
891
+ [{ ...GRAB_DEFAULTS }, -0.2],
892
+ [{ ...GRAB_DEFAULTS }, 0.15],
893
+ [{ ...GRAB_DEFAULTS, holdDistance: 0.9 }, -0.15],
894
+ ]) {
895
+ const { r, seen } = networked(grab, { deliver: (tick) => tick % 5 === 0 });
896
+ r.step();
897
+ assert.equal(r.step(16, true), 'Throw');
898
+ for (let i = 0; i < 40; i += 1) {
899
+ r.step();
900
+ }
901
+ assert.equal(seen.handovers.length, 0);
902
+ seen.bunching = true;
903
+ r.me.x += dx;
904
+ r.step(80);
905
+ r.step(80, true);
906
+ let i = 0;
907
+ while (seen.throws.length === 0 && r.mine.has('a') && i < 1000) {
908
+ r.step();
909
+ i += 1;
910
+ }
911
+ assert.ok(seen.worst <= Limits.MOVE_SLACK, `hold ${grab.holdDistance}, step ${dx}: ${seen.worst}`);
912
+ assert.deepEqual(seen.handovers, []);
913
+ assert.equal(seen.throws.length, 1);
914
+ assert.equal(seen.throws[0].owned, true);
915
+ }
916
+ });
917
+
918
+ console.log(`test-grab-orbit: ${n} passed`);