castle-web-cli 0.4.231 → 0.4.233

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (368) hide show
  1. package/dist/agent-failures.d.ts +8 -0
  2. package/dist/agent-failures.js +19 -2
  3. package/dist/agent-prompts.d.ts +1 -1
  4. package/dist/agent-prompts.js +10 -3
  5. package/dist/agent.d.ts +64 -3
  6. package/dist/agent.js +170 -81
  7. package/dist/castle-host/host.d.ts +1 -0
  8. package/dist/castle-host/host.js +51 -3
  9. package/dist/castleJson.d.ts +4 -2
  10. package/dist/castleJson.js +2 -0
  11. package/dist/check.d.ts +3 -1
  12. package/dist/check.js +11 -1
  13. package/dist/devSessionStorage.js +47 -1
  14. package/dist/importPinMismatch.d.ts +22 -0
  15. package/dist/importPinMismatch.js +77 -0
  16. package/dist/importRegression.js +3 -15
  17. package/dist/imports.d.ts +10 -3
  18. package/dist/imports.js +88 -20
  19. package/dist/index.js +5 -4
  20. package/dist/init.js +1 -1
  21. package/dist/platformDoc.js +22 -0
  22. package/dist/routerFences.d.ts +5 -0
  23. package/dist/routerFences.js +12 -2
  24. package/dist/save-deck.d.ts +5 -0
  25. package/dist/save-deck.js +37 -4
  26. package/dist/serve.js +3 -0
  27. package/dist/serverBundle.js +5 -3
  28. package/dist/shell/assets/{index-D2tWM896.js → index-BjOxR-lW.js} +4 -4
  29. package/dist/shell/assets/index-MVLfNYd9.css +1 -0
  30. package/dist/shell/index.html +2 -2
  31. package/dist/taskSteer.d.ts +13 -0
  32. package/dist/taskSteer.js +22 -2
  33. package/kits/base/castle.json +1 -1
  34. package/kits/base/package-lock.json +1 -1
  35. package/kits/base/sdk/README.md +79 -2
  36. package/kits/base/sdk/avatar.d.ts +6 -2
  37. package/kits/base/sdk/avatar.js +12 -4
  38. package/kits/base/sdk/castle.d.ts +1 -1
  39. package/kits/base/sdk/commands.d.ts +14 -0
  40. package/kits/base/sdk/hub.d.ts +6 -2
  41. package/kits/base/sdk/hub.js +13 -0
  42. package/kits/base/sdk/multiplayer.js +17 -1
  43. package/kits/base/sdk/save.js +2 -1
  44. package/kits/base/sdk/server/hub.d.ts +87 -0
  45. package/kits/base/sdk/server/hub.js +70 -0
  46. package/kits/base/sdk/server/platformHandle.d.ts +31 -1
  47. package/kits/base/sdk/server/storage.d.ts +13 -0
  48. package/kits/base/sdk/server/storage.js +46 -0
  49. package/kits/base/sdk/server/wrapper.d.ts +36 -5
  50. package/kits/base/sdk/server/wrapper.js +83 -8
  51. package/kits/hub-game/CLAUDE.md +352 -12
  52. package/kits/hub-game/blueprints/avatar-10.scene +28 -0
  53. package/kits/hub-game/blueprints/avatar-11.scene +28 -0
  54. package/kits/hub-game/blueprints/avatar-12.scene +28 -0
  55. package/kits/hub-game/blueprints/avatar-13.scene +28 -0
  56. package/kits/hub-game/blueprints/avatar-14.scene +28 -0
  57. package/kits/hub-game/blueprints/avatar-15.scene +28 -0
  58. package/kits/hub-game/blueprints/avatar-4.scene +28 -0
  59. package/kits/hub-game/blueprints/avatar-5.scene +28 -0
  60. package/kits/hub-game/blueprints/avatar-6.scene +28 -0
  61. package/kits/hub-game/blueprints/avatar-7.scene +28 -0
  62. package/kits/hub-game/blueprints/avatar-8.scene +28 -0
  63. package/kits/hub-game/blueprints/avatar-9.scene +28 -0
  64. package/kits/hub-game/code/client/actorBudget.js +47 -0
  65. package/kits/hub-game/code/client/attention.js +11 -2
  66. package/kits/hub-game/code/client/avatar.js +345 -32
  67. package/kits/hub-game/code/client/avatarCap.js +228 -0
  68. package/kits/hub-game/code/client/avatarColors.js +70 -0
  69. package/kits/hub-game/code/client/avatarFaces.js +21 -0
  70. package/kits/hub-game/code/client/avatarShare.js +251 -0
  71. package/kits/hub-game/code/client/ballot.js +467 -0
  72. package/kits/hub-game/code/client/build.js +22 -0
  73. package/kits/hub-game/code/client/chat.js +12 -0
  74. package/kits/hub-game/code/client/chrome.js +1334 -50
  75. package/kits/hub-game/code/client/companion.js +716 -0
  76. package/kits/hub-game/code/client/companionDress.js +99 -0
  77. package/kits/hub-game/code/client/decor.js +509 -0
  78. package/kits/hub-game/code/client/earned.js +131 -0
  79. package/kits/hub-game/code/client/emoteBubble.js +75 -0
  80. package/kits/hub-game/code/client/grab.js +363 -20
  81. package/kits/hub-game/code/client/hats.js +26 -0
  82. package/kits/hub-game/code/client/hookLayer.js +328 -15
  83. package/kits/hub-game/code/client/hudClear.js +126 -0
  84. package/kits/hub-game/code/client/idle.js +163 -22
  85. package/kits/hub-game/code/client/interestCap.js +81 -0
  86. package/kits/hub-game/code/client/keys.js +36 -0
  87. package/kits/hub-game/code/client/labelsNear.js +205 -0
  88. package/kits/hub-game/code/client/menuIcons.js +34 -0
  89. package/kits/hub-game/code/client/pack.js +6 -2
  90. package/kits/hub-game/code/client/packFormat.js +3 -3
  91. package/kits/hub-game/code/client/rehello.js +167 -0
  92. package/kits/hub-game/code/client/shot.js +92 -0
  93. package/kits/hub-game/code/client/sound.js +63 -8
  94. package/kits/hub-game/code/client/soundscape.js +1151 -0
  95. package/kits/hub-game/code/client/soundscapeActions.js +99 -0
  96. package/kits/hub-game/code/client/soundscapeAmbience.js +453 -0
  97. package/kits/hub-game/code/client/soundscapeBaked.js +143 -0
  98. package/kits/hub-game/code/client/soundscapeBusker.js +217 -0
  99. package/kits/hub-game/code/client/soundscapeCourt.js +200 -0
  100. package/kits/hub-game/code/client/soundscapeCrowd.js +331 -0
  101. package/kits/hub-game/code/client/soundscapeCues.js +362 -0
  102. package/kits/hub-game/code/client/soundscapeMap.js +181 -0
  103. package/kits/hub-game/code/client/soundscapeMarket.js +176 -0
  104. package/kits/hub-game/code/client/soundscapeMusic.js +126 -0
  105. package/kits/hub-game/code/client/soundscapeMusicEngine.js +460 -0
  106. package/kits/hub-game/code/client/soundscapeSimlish.js +737 -0
  107. package/kits/hub-game/code/client/soundscapeSongs.js +127 -0
  108. package/kits/hub-game/code/client/soundscapeSongsRound3.js +63 -0
  109. package/kits/hub-game/code/client/soundscapeSynthesis.js +168 -0
  110. package/kits/hub-game/code/client/soundscapeUi.js +211 -0
  111. package/kits/hub-game/code/client/soundscapeVoice.js +1209 -0
  112. package/kits/hub-game/code/client/soundscapeWaves.js +206 -0
  113. package/kits/hub-game/code/client/spin.js +101 -0
  114. package/kits/hub-game/code/client/state.js +131 -1
  115. package/kits/hub-game/code/client/stuck.js +259 -0
  116. package/kits/hub-game/code/client/talk.js +538 -47
  117. package/kits/hub-game/code/client/tiers.js +541 -27
  118. package/kits/hub-game/code/client/toastBand.js +37 -0
  119. package/kits/hub-game/code/client/together.js +98 -1
  120. package/kits/hub-game/code/client/visibility.js +43 -0
  121. package/kits/hub-game/code/client/wear.js +470 -97
  122. package/kits/hub-game/code/client/yours.js +199 -0
  123. package/kits/hub-game/code/companionSpec.js +45 -0
  124. package/kits/hub-game/code/privateText.js +7 -0
  125. package/kits/hub-game/code/server/actions.js +133 -0
  126. package/kits/hub-game/code/server/actors.js +14 -1
  127. package/kits/hub-game/code/server/ask.js +155 -0
  128. package/kits/hub-game/code/server/ballot.js +539 -0
  129. package/kits/hub-game/code/server/cells.js +100 -0
  130. package/kits/hub-game/code/server/companion.js +82 -0
  131. package/kits/hub-game/code/server/cues.js +70 -0
  132. package/kits/hub-game/code/server/decor.js +269 -0
  133. package/kits/hub-game/code/server/earned.js +461 -0
  134. package/kits/hub-game/code/server/friendBests.js +166 -0
  135. package/kits/hub-game/code/server/friendsAmong.js +232 -0
  136. package/kits/hub-game/code/server/grab.js +296 -11
  137. package/kits/hub-game/code/server/history.js +5 -0
  138. package/kits/hub-game/code/server/hub.js +1317 -52
  139. package/kits/hub-game/code/server/idle.js +30 -11
  140. package/kits/hub-game/code/server/index.js +2 -2
  141. package/kits/hub-game/code/server/labels.js +28 -2
  142. package/kits/hub-game/code/server/manifest.js +424 -8
  143. package/kits/hub-game/code/server/menu.js +133 -0
  144. package/kits/hub-game/code/server/npcs.js +830 -80
  145. package/kits/hub-game/code/server/party.js +13 -2
  146. package/kits/hub-game/code/server/players.js +22 -0
  147. package/kits/hub-game/code/server/say.js +8 -2
  148. package/kits/hub-game/code/server/shot.js +110 -0
  149. package/kits/hub-game/code/server/standing.js +124 -0
  150. package/kits/hub-game/code/server/vis.js +113 -0
  151. package/kits/hub-game/code/server/wear.js +128 -11
  152. package/kits/hub-game/code/server/yours.js +59 -0
  153. package/kits/hub-game/code/shared/cues.js +9 -0
  154. package/kits/hub-game/code/shared/decor.js +41 -0
  155. package/kits/hub-game/code/shared/shot.js +19 -0
  156. package/kits/hub-game/code/shared/spin.js +57 -0
  157. package/kits/hub-game/code/systems/hub.js +1083 -85
  158. package/kits/hub-game/docs/content-pack.md +1 -1
  159. package/kits/hub-game/docs/contract.md +2440 -61
  160. package/kits/hub-game/docs/home-pieces.md +64 -0
  161. package/kits/hub-game/models/avatar-10.pxmodel +28 -0
  162. package/kits/hub-game/models/avatar-11.pxmodel +28 -0
  163. package/kits/hub-game/models/avatar-12.pxmodel +27 -0
  164. package/kits/hub-game/models/avatar-13.pxmodel +24 -0
  165. package/kits/hub-game/models/avatar-14.pxmodel +29 -0
  166. package/kits/hub-game/models/avatar-15.pxmodel +24 -0
  167. package/kits/hub-game/models/avatar-4.pxmodel +29 -0
  168. package/kits/hub-game/models/avatar-5.pxmodel +27 -0
  169. package/kits/hub-game/models/avatar-6.pxmodel +26 -0
  170. package/kits/hub-game/models/avatar-7.pxmodel +25 -0
  171. package/kits/hub-game/models/avatar-8.pxmodel +24 -0
  172. package/kits/hub-game/models/avatar-9.pxmodel +30 -0
  173. package/kits/hub-game/models/earned/badge-round.pxmodel +100 -0
  174. package/kits/hub-game/models/earned/badge-shield.pxmodel +117 -0
  175. package/kits/hub-game/models/earned/badge-star.pxmodel +87 -0
  176. package/kits/hub-game/models/earned/belt-plain.pxmodel +72 -0
  177. package/kits/hub-game/models/earned/held-ball.pxmodel +43 -0
  178. package/kits/hub-game/models/earned/held-lantern.pxmodel +85 -0
  179. package/kits/hub-game/models/earned/held-net.pxmodel +57 -0
  180. package/kits/hub-game/models/earned/held-trophy.pxmodel +109 -0
  181. package/kits/hub-game/models/earned/pin-round.pxmodel +62 -0
  182. package/kits/hub-game/models/earned/sash-plain.pxmodel +66 -0
  183. package/kits/hub-game/models/earned/square/belt-plain.pxmodel +104 -0
  184. package/kits/hub-game/models/earned/square/sash-plain.pxmodel +98 -0
  185. package/kits/hub-game/models/hats/bat-clip.pxmodel +206 -0
  186. package/kits/hub-game/models/hats/beekeeper.pxmodel +149 -0
  187. package/kits/hub-game/models/hats/cap.pxmodel +162 -0
  188. package/kits/hub-game/models/hats/captain.pxmodel +178 -0
  189. package/kits/hub-game/models/hats/crooked-top.pxmodel +172 -0
  190. package/kits/hub-game/models/hats/flower-crown.pxmodel +451 -0
  191. package/kits/hub-game/models/hats/foam-crown.pxmodel +179 -0
  192. package/kits/hub-game/models/hats/lantern.pxmodel +230 -0
  193. package/kits/hub-game/models/hats/laurel.pxmodel +533 -0
  194. package/kits/hub-game/models/hats/lighthouse.pxmodel +96 -0
  195. package/kits/hub-game/models/hats/sailor.pxmodel +141 -0
  196. package/kits/hub-game/models/hats/souwester.pxmodel +131 -0
  197. package/kits/hub-game/models/hats/straw.pxmodel +139 -0
  198. package/kits/hub-game/models/hats/sunflower.pxmodel +406 -0
  199. package/kits/hub-game/models/hats/sweatband.pxmodel +112 -0
  200. package/kits/hub-game/models/hats/witch.pxmodel +147 -0
  201. package/kits/hub-game/package-lock.json +2 -0
  202. package/kits/hub-game/package.json +2 -1
  203. package/kits/hub-game/scripts/check.mjs +78 -11
  204. package/kits/hub-game/scripts/companion-follower-wire.json +1 -0
  205. package/kits/hub-game/scripts/fixtures/decor-keyless-draw.json +121 -0
  206. package/kits/hub-game/scripts/fixtures/decor-keyless.json +293 -0
  207. package/kits/hub-game/scripts/fixtures/engine-models-stub.mjs +23 -0
  208. package/kits/hub-game/scripts/fixtures/fake-dom.mjs +107 -0
  209. package/kits/hub-game/scripts/fixtures/interest-legacy.json +385 -0
  210. package/kits/hub-game/scripts/fixtures/npc-notes-v1.77.jsonl +4 -0
  211. package/kits/hub-game/scripts/fixtures/vis-shared-wire.json +1 -0
  212. package/kits/hub-game/scripts/gen-avatars.mjs +933 -16
  213. package/kits/hub-game/scripts/gen-earned.mjs +285 -0
  214. package/kits/hub-game/scripts/gen-hats.mjs +827 -0
  215. package/kits/hub-game/scripts/grab.before-orbit.mjs +400 -0
  216. package/kits/hub-game/scripts/soundscape-wave-analysis.mjs +125 -0
  217. package/kits/hub-game/scripts/test-actions-client.mjs +488 -0
  218. package/kits/hub-game/scripts/test-actions.mjs +298 -0
  219. package/kits/hub-game/scripts/test-actor-budget.mjs +44 -0
  220. package/kits/hub-game/scripts/test-actors.mjs +19 -17
  221. package/kits/hub-game/scripts/test-ask-client.mjs +358 -0
  222. package/kits/hub-game/scripts/test-ask.mjs +269 -0
  223. package/kits/hub-game/scripts/test-attention.mjs +10 -1
  224. package/kits/hub-game/scripts/test-avatar-cap.mjs +379 -0
  225. package/kits/hub-game/scripts/test-avatar-colors.mjs +72 -0
  226. package/kits/hub-game/scripts/test-avatar-models.mjs +181 -0
  227. package/kits/hub-game/scripts/test-avatar-share.mjs +318 -0
  228. package/kits/hub-game/scripts/test-avatar-wire.mjs +753 -0
  229. package/kits/hub-game/scripts/test-avatar.mjs +540 -7
  230. package/kits/hub-game/scripts/test-ballot.mjs +1001 -0
  231. package/kits/hub-game/scripts/test-build.mjs +31 -0
  232. package/kits/hub-game/scripts/test-cells.mjs +71 -0
  233. package/kits/hub-game/scripts/test-chat.mjs +694 -0
  234. package/kits/hub-game/scripts/test-classic.mjs +2 -0
  235. package/kits/hub-game/scripts/test-companion-chat.mjs +198 -0
  236. package/kits/hub-game/scripts/test-companion-client.mjs +1167 -0
  237. package/kits/hub-game/scripts/test-companion.mjs +259 -0
  238. package/kits/hub-game/scripts/test-decor-client.mjs +303 -0
  239. package/kits/hub-game/scripts/test-decor.mjs +457 -0
  240. package/kits/hub-game/scripts/test-earned-client.mjs +459 -0
  241. package/kits/hub-game/scripts/test-earned.mjs +1194 -0
  242. package/kits/hub-game/scripts/test-emote-bubble.mjs +110 -0
  243. package/kits/hub-game/scripts/test-friend-bests.mjs +435 -0
  244. package/kits/hub-game/scripts/test-grab-client.mjs +186 -3
  245. package/kits/hub-game/scripts/test-grab-orbit.mjs +918 -0
  246. package/kits/hub-game/scripts/test-grab.mjs +864 -3
  247. package/kits/hub-game/scripts/test-hats.mjs +483 -0
  248. package/kits/hub-game/scripts/test-hub-leaving.mjs +203 -0
  249. package/kits/hub-game/scripts/test-hub-ops.mjs +439 -2
  250. package/kits/hub-game/scripts/test-hub-trip.mjs +1228 -1
  251. package/kits/hub-game/scripts/test-hud-clear.mjs +1000 -0
  252. package/kits/hub-game/scripts/test-idle.mjs +361 -1
  253. package/kits/hub-game/scripts/test-interest-cap.mjs +36 -0
  254. package/kits/hub-game/scripts/test-interest-cells.mjs +493 -0
  255. package/kits/hub-game/scripts/test-interest-client.mjs +118 -0
  256. package/kits/hub-game/scripts/test-keys.mjs +46 -1
  257. package/kits/hub-game/scripts/test-labels.mjs +596 -0
  258. package/kits/hub-game/scripts/test-manifest.mjs +282 -0
  259. package/kits/hub-game/scripts/test-menu-client.mjs +480 -0
  260. package/kits/hub-game/scripts/test-menu.mjs +296 -0
  261. package/kits/hub-game/scripts/test-npcs.mjs +1718 -12
  262. package/kits/hub-game/scripts/test-object-entry.mjs +76 -0
  263. package/kits/hub-game/scripts/test-pack.mjs +7 -0
  264. package/kits/hub-game/scripts/test-party.mjs +22 -1
  265. package/kits/hub-game/scripts/test-picker.mjs +314 -0
  266. package/kits/hub-game/scripts/test-rehello.mjs +1039 -0
  267. package/kits/hub-game/scripts/test-relations-among.mjs +222 -0
  268. package/kits/hub-game/scripts/test-relations-refresh.mjs +188 -0
  269. package/kits/hub-game/scripts/test-say.mjs +59 -2
  270. package/kits/hub-game/scripts/test-shot.mjs +756 -0
  271. package/kits/hub-game/scripts/test-soundscape.mjs +5432 -0
  272. package/kits/hub-game/scripts/test-spin.mjs +314 -0
  273. package/kits/hub-game/scripts/test-standing.mjs +416 -0
  274. package/kits/hub-game/scripts/test-stuck.mjs +1903 -0
  275. package/kits/hub-game/scripts/test-tier-cells.mjs +706 -0
  276. package/kits/hub-game/scripts/test-tiers-render.mjs +339 -0
  277. package/kits/hub-game/scripts/test-tiers.mjs +666 -3
  278. package/kits/hub-game/scripts/test-toast-band.mjs +61 -0
  279. package/kits/hub-game/scripts/test-try-on.mjs +940 -0
  280. package/kits/hub-game/scripts/test-vis.mjs +343 -0
  281. package/kits/hub-game/scripts/test-visibility.mjs +134 -0
  282. package/kits/hub-game/scripts/test-yours-chrome.mjs +166 -0
  283. package/kits/hub-game/scripts/test-yours.mjs +372 -0
  284. package/kits/multiplayer-3d/CLAUDE.md +50 -0
  285. package/kits/multiplayer-3d/castle.json +5 -5
  286. package/kits/multiplayer-3d/code/client/avatars.js +7 -0
  287. package/kits/multiplayer-3d/code/client/gait.js +65 -0
  288. package/kits/multiplayer-3d/code/client/nameTags.js +59 -4
  289. package/kits/multiplayer-3d/code/options.js +5 -0
  290. package/kits/multiplayer-3d/code/server/ownership.js +10 -2
  291. package/kits/multiplayer-3d/code/server/sceneBodies.js +12 -0
  292. package/kits/multiplayer-3d/code/server/world.js +7 -0
  293. package/kits/multiplayer-3d/code/systems/multiplayer.js +74 -4
  294. package/kits/multiplayer-3d/package.json +2 -1
  295. package/kits/multiplayer-3d/scripts/fixtures/README.md +11 -0
  296. package/kits/multiplayer-3d/scripts/fixtures/ownership-legacy.json +2 -0
  297. package/kits/multiplayer-3d/scripts/ownership-harness.mjs +102 -0
  298. package/kits/multiplayer-3d/scripts/ownership-loader.mjs +46 -0
  299. package/kits/multiplayer-3d/scripts/ownership-worker.mjs +15 -0
  300. package/kits/multiplayer-3d/scripts/record-ownership-session.mjs +98 -0
  301. package/kits/multiplayer-3d/scripts/test-eligible-claims.mjs +167 -0
  302. package/kits/multiplayer-3d/scripts/test-own-name-tag.mjs +247 -0
  303. package/kits/physics-2d/CLAUDE.md +9 -0
  304. package/kits/physics-2d/castle.json +2 -2
  305. package/kits/physics-2d/engine/scene.js +288 -1
  306. package/kits/physics-2d/engine/skyKey.js +129 -0
  307. package/kits/physics-2d/engine/stillClock.js +106 -0
  308. package/kits/physics-2d/engine/updateBudget.js +21 -0
  309. package/kits/physics-2d/scripts/test-skip-unchanged.mjs +370 -1
  310. package/kits/physics-3d/CLAUDE.md +1391 -0
  311. package/kits/physics-3d/behaviors/Player.jsx +921 -3
  312. package/kits/physics-3d/castle.json +3 -3
  313. package/kits/physics-3d/docs/pxmodel-format.md +12 -0
  314. package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +98 -10
  315. package/kits/physics-3d/engine3d/actornode.js +86 -0
  316. package/kits/physics-3d/engine3d/animationBudget.js +23 -0
  317. package/kits/physics-3d/engine3d/editorWorld.js +6 -0
  318. package/kits/physics-3d/engine3d/emptynodes.js +232 -0
  319. package/kits/physics-3d/engine3d/framecap.js +96 -0
  320. package/kits/physics-3d/engine3d/gait.js +118 -0
  321. package/kits/physics-3d/engine3d/kithooks.js +47 -0
  322. package/kits/physics-3d/engine3d/look.js +507 -48
  323. package/kits/physics-3d/engine3d/lookbake.js +208 -0
  324. package/kits/physics-3d/engine3d/materialpresets.js +2 -0
  325. package/kits/physics-3d/engine3d/motion.js +39 -10
  326. package/kits/physics-3d/engine3d/nearshadows.js +37 -19
  327. package/kits/physics-3d/engine3d/optimize.js +58 -5
  328. package/kits/physics-3d/engine3d/orbitinput.js +755 -0
  329. package/kits/physics-3d/engine3d/orbitlens.js +48 -0
  330. package/kits/physics-3d/engine3d/ownmodel.js +213 -0
  331. package/kits/physics-3d/engine3d/pxmodel.js +42 -11
  332. package/kits/physics-3d/engine3d/shadowfade.js +882 -0
  333. package/kits/physics-3d/engine3d/sky.js +547 -0
  334. package/kits/physics-3d/engine3d/skylife.js +306 -0
  335. package/kits/physics-3d/engine3d/unchanged.js +11 -0
  336. package/kits/physics-3d/engine3d/viewrange.js +52 -0
  337. package/kits/physics-3d/engine3d/world3d.js +870 -87
  338. package/kits/physics-3d/main.jsx +5 -0
  339. package/kits/physics-3d/package.json +1 -1
  340. package/kits/physics-3d/scripts/Player.before-orbit.mjs +310 -0
  341. package/kits/physics-3d/scripts/Scene3DPlayer.before-orbit.jsx +337 -0
  342. package/kits/physics-3d/scripts/look-baseline.mjs +155 -0
  343. package/kits/physics-3d/scripts/test-camera-follow.mjs +12 -2
  344. package/kits/physics-3d/scripts/test-camera-orbit.mjs +4588 -0
  345. package/kits/physics-3d/scripts/test-drop-empty-parts.mjs +787 -0
  346. package/kits/physics-3d/scripts/test-frame-cap-bob.mjs +435 -0
  347. package/kits/physics-3d/scripts/test-frame-cap.mjs +452 -0
  348. package/kits/physics-3d/scripts/test-gait.mjs +387 -0
  349. package/kits/physics-3d/scripts/test-look-bake.mjs +1056 -0
  350. package/kits/physics-3d/scripts/test-model-parse-cache.mjs +92 -0
  351. package/kits/physics-3d/scripts/test-near-shadows.mjs +3 -0
  352. package/kits/physics-3d/scripts/test-orbit-shadow-fade.mjs +1920 -0
  353. package/kits/physics-3d/scripts/test-orbit-shift-lock.mjs +545 -0
  354. package/kits/physics-3d/scripts/test-orbit-shot.mjs +583 -0
  355. package/kits/physics-3d/scripts/test-orbit-view.mjs +230 -0
  356. package/kits/physics-3d/scripts/test-skip-empty-nodes.mjs +845 -0
  357. package/kits/physics-3d/scripts/test-sky.mjs +2470 -0
  358. package/kits/physics-3d/scripts/test-static-actors.mjs +13 -1
  359. package/kits/physics-3d/scripts/test-static-passes.mjs +1 -1
  360. package/kits/physics-3d/scripts/test-static-scenery.mjs +2171 -0
  361. package/kits/physics-3d/scripts/test-steady-hover.mjs +635 -0
  362. package/kits/physics-3d/scripts/test-view-range.mjs +260 -0
  363. package/kits/physics-3d/systems/physics3d.js +474 -12
  364. package/kits/real-time/castle.json +2 -2
  365. package/kits/real-time/code/server/gameHooks.js +7 -0
  366. package/kits/real-time/code/server/session.js +97 -19
  367. package/package.json +2 -2
  368. package/dist/shell/assets/index-Cf61cNfT.css +0 -1
@@ -52,6 +52,45 @@ function applyHover(phys, actor, t, dims, dt) {
52
52
  return grounded;
53
53
  }
54
54
 
55
+ // The hover of a scene that set `"engine": { "camera": { "follow": "rigid" } }`
56
+ // (the kit's CLAUDE.md has the terms): the same spring, asked for as a force.
57
+ // `applyHover` pushes once per drawn frame while gravity pulls once per fixed
58
+ // physics step, and a frame takes none, one or two steps, so on a frame that
59
+ // is not exactly one step the player is kicked up or let drop: a bob of a
60
+ // centimetre or two walking, which the rigid camera would show as the view
61
+ // bobbing. A force acts once per step, as gravity does, so the spring and
62
+ // gravity balance on every step whatever the frames are.
63
+ //
64
+ // The spring rests STEADY_REST higher than applyHover's so the player stands
65
+ // where it does without the key. applyHover's push lands at the start of a
66
+ // step and gravity is spread through it, so the velocity it reads standing
67
+ // still is not 0 but a fraction of a step's fall (-0.123 m/s for the kit's
68
+ // player, whose gravity scale is 2), and the spring's damper answers that
69
+ // with lift: 18 * 0.123 / 160 = 1.38 cm. Under a force the velocity at rest
70
+ // is 0. (0.69 cm per unit of gravity scale: a player with another scale
71
+ // stands that much off its keyless height.) What counts as grounded is
72
+ // measured as it is there.
73
+ //
74
+ // The force is the physics system's to drop: `addForce` lasts this frame's
75
+ // physics pass and no longer, so a frame this does not run on has no lift.
76
+ const STEADY_REST = 0.0138;
77
+
78
+ function hoverSteadily(phys, actor, t, dims) {
79
+ const hit = phys.castRay({ x: t.x, y: t.y, z: t.z }, { x: 0, y: -1, z: 0 }, dims.height, actor);
80
+ const hoverHeight = dims.height / 2 + 0.15;
81
+ const vel = phys.getVelocity(actor);
82
+ if (hit && hit.distance < hoverHeight + 0.4 && vel.y <= 1) {
83
+ const accel = 160 * (hoverHeight + STEADY_REST - hit.distance) - 18 * vel.y;
84
+ phys.addForce(actor, { x: 0, y: accel * phys.mass(actor), z: 0 });
85
+ }
86
+ return Boolean(hit && hit.distance < hoverHeight + 0.25);
87
+ }
88
+
89
+ // The fall-faster pull under the same key: a force for the same reason.
90
+ function pullSteadily(phys, actor) {
91
+ phys.addForce(actor, { x: 0, y: -14 * phys.mass(actor), z: 0 });
92
+ }
93
+
55
94
  function applyMove(phys, actor, move, speed, grounded, dt) {
56
95
  const mass = phys.mass(actor);
57
96
  const vel = phys.getVelocity(actor);
@@ -110,6 +149,52 @@ function settle(value, from, to, dt) {
110
149
  return to - lag + (value - from + lag) * Math.exp(-FOLLOW_RATE * dt);
111
150
  }
112
151
 
152
+ // The follow camera of a scene that capped its frame rate (`"engine": {
153
+ // "maxFps": n }`, `scene.maxFps` above 0) and set no camera key: the camera
154
+ // `updateCamera` places, the same target and rate, placed differently.
155
+ // updateCamera eases once a frame by 1 - e^(-12 dt), from the update, toward
156
+ // where the player was drawn LAST frame: behind a target moving at v it trails
157
+ // by about v/12 + v dt/2, and the player is then drawn v dt further on. Both
158
+ // change with each frame's dt, and the cap hands frames of 25 to 50 ms on a
159
+ // slow phone, so the player would slide back and forth on screen as it walks.
160
+ // So under the cap the camera is placed as the rigid one is: at the end of
161
+ // the frame, by the physics system (`placeFollowCamera`), on the position the
162
+ // frame is drawn with, and each coordinate is eased as if the target moved
163
+ // steadily from last frame's place to this frame's (`settle`: the exact
164
+ // solution of x' = 12 (target - x)). A steady walk is then trailed by v/12
165
+ // whatever the frames; a target standing still is reached as the lerp
166
+ // reaches it. Last frame's target is on the actor's runtime (`camTarget`).
167
+ // Without the cap updateCamera runs as it always did.
168
+ function placeCapped(scene, id, dt) {
169
+ const actor = scene.getActor(id);
170
+ const t = actor?.components.Transform;
171
+ const props = actor?.components.Player;
172
+ const cam = scene.three?.camera;
173
+ if (!cam || !t || !props || props.$enabled === false) return;
174
+ const per = persistentOf(scene);
175
+ const rt = actor.runtime;
176
+ const target = {
177
+ x: t.x - props.camDistance * Math.sin(per.camAngle),
178
+ y: t.y + props.camHeight,
179
+ z: t.z + props.camDistance * Math.cos(per.camAngle),
180
+ };
181
+ const last = per.camLastAt;
182
+ const jumped = last && Math.hypot(t.x - last.x, t.z - last.z) > TELEPORT_PER_FRAME;
183
+ per.camLastAt = { x: t.x, z: t.z };
184
+ const from = rt.camTarget ?? target;
185
+ rt.camTarget = target;
186
+ if (jumped || Math.hypot(target.x - cam.position.x, target.z - cam.position.z) > 20) {
187
+ cam.position.set(target.x, target.y, target.z);
188
+ } else {
189
+ cam.position.set(
190
+ settle(cam.position.x, from.x, target.x, dt),
191
+ settle(cam.position.y, from.y, target.y, dt),
192
+ settle(cam.position.z, from.z, target.z, dt)
193
+ );
194
+ }
195
+ cam.lookAt(t.x, t.y + 0.5, t.z);
196
+ }
197
+
113
198
  // The follow camera of a scene that set `"engine": { "camera": { "follow":
114
199
  // "rigid" } }` (the kit's CLAUDE.md has the terms). It moves with the player
115
200
  // as one piece: the offset from the player (its angle and height) and the
@@ -166,6 +251,821 @@ function placeFollowing(scene, id, dt) {
166
251
  followRigidly(scene, t, per, actor.runtime, props, dt);
167
252
  }
168
253
 
254
+ // ---------------------------------------------------------------------------
255
+ // The camera the player controls: a scene that set `"engine": { "camera": {
256
+ // "follow": "rigid", "control": "orbit" } }` (the kit's CLAUDE.md has the
257
+ // terms). Everything from here to `findLookText` runs under that key only,
258
+ // from the one branch at the top of `Player.update`; nothing above this line
259
+ // calls into it. It restates the steps of `update` that it shares (the
260
+ // persistent defaults, the jump, the fall, the hud) rather than have `update`
261
+ // call out for them, so that without the key `update` is the code it was.
262
+ //
263
+ // Walking is camera-relative (up on the stick, or W, is away from the camera)
264
+ // and the character turns to face the way it walks. The camera's yaw is
265
+ // `scene.persistent.camAngle`, with the meaning it always had: the camera
266
+ // sits at player + (-d sin(yaw), h, d cos(yaw)). Its pitch and distance are
267
+ // the player's own and are not sent anywhere: they are kept on the runtime
268
+ // (`scene.cameraOrbit`), which a scene load does not replace, and never on
269
+ // `scene.persistent`.
270
+
271
+ // The numbers below are Roblox's default (Classic) third-person camera at
272
+ // our scale. A Roblox character is 5 studs tall; ours, the kit's
273
+ // player.pxmodel standing on its 1.4 m capsule (the hub's avatar), is 1.46 m
274
+ // from the soles of its feet (0.02 m above the model's origin) to the top of
275
+ // its cap (1.48 m), so one stud is 1.46 / 5 = 0.292 m here.
276
+ const STUD = 1.46 / 5;
277
+ // The camera looks at the player this far above its Transform. Roblox looks
278
+ // at the root part plus 1.5 studs; the root part is the torso's middle, and
279
+ // so is our Transform (the capsule's middle, 0.70 m above the model's origin,
280
+ // where the torso's middle is 0.69), so the same 1.5 studs: 0.438 m, the neck.
281
+ const LOOK_HEIGHT = 1.5 * STUD;
282
+ // How far from that point the camera starts: Roblox's 12.5 studs, 3.65 m,
283
+ // the same two and a half character heights.
284
+ const ORBIT_START = 12.5 * STUD;
285
+ // The nearest and farthest the player can zoom, metres from that point.
286
+ // Roblox lets the player in to 0.5 studs, which is first person; here the
287
+ // nearest keeps the camera clear of the character, which an avatar hides
288
+ // itself from within about 1 m. The farthest is Roblox's 128 studs, 37.4 m.
289
+ const ORBIT_NEAREST = 1.5;
290
+ const ORBIT_FARTHEST = 128 * STUD;
291
+ // The lowest and highest the camera tilts, above the horizontal: Roblox's 80
292
+ // degrees either way. Below the horizontal the camera is lower than the point
293
+ // it looks at, looking up, and the ground (a fixed solid, as walls are) holds
294
+ // it above itself as a wall holds it in front (`limitReach`, `sweep`).
295
+ const ORBIT_PITCH_LEAST = -80 * DEG;
296
+ const ORBIT_PITCH_MOST = 80 * DEG;
297
+ // The tilt the camera starts at, whatever the Player's camDistance and
298
+ // camHeight: 15 degrees down, behind the player, as Roblox starts. With the
299
+ // 70 degree lens (engine3d/orbitlens.js `fitOrbitLens`) the top of the frame
300
+ // is 20 degrees above the horizon: a world's sky shows.
301
+ const ORBIT_PITCH_START = 15 * DEG;
302
+ // How fast the camera's angle, tilt and distance catch up with what was
303
+ // asked: quick enough to feel direct, slow enough to take the steps out of a
304
+ // finger's drag.
305
+ const ORBIT_RATE = 20;
306
+ // How fast the character turns to face the way it walks.
307
+ const FACE_RATE = 12;
308
+ // The camera stays at least this far off every fixed surface: it is a ball
309
+ // of this radius swept out from the point it looks at (`sweep`), so it is
310
+ // this far off whatever stops it, square to the surface, however the line
311
+ // meets it (a wall met at a grazing angle, a box's edge or corner, the
312
+ // ground seen along at a low angle). A wider lens than the default's needs
313
+ // more: the lens's near plane must stay clear of the surface whichever way
314
+ // the camera looks, so the margin is the distance from the camera to the
315
+ // near plane's corners plus LENS_CLEARANCE when that is more
316
+ // (`lensMargin`). At the default 70 degrees on a 20:9 screen the corners
317
+ // are 0.198 m off and the margin is 0.25 m; at 120 degrees on 20:9, 0.484 m.
318
+ const CAMERA_MARGIN = 0.25;
319
+ const LENS_CLEARANCE = 0.05;
320
+ // A look point nearer fixed surfaces than the margin and START_SKIN (a
321
+ // player pressed against a wall, or in a corner, with a wide lens) has the
322
+ // ball start moved the least way that puts it that far off all of them
323
+ // together (`findStart`), and looked around again, START_TRIES times at
324
+ // most; a point within START_SETTLED of that distance is taken. A gap too
325
+ // narrow for that leaves the ball smaller, down to START_LEAST.
326
+ const START_SKIN = 0.05;
327
+ const START_TRIES = 8;
328
+ const START_SETTLED = 1e-4;
329
+ const START_LEAST = 0.02;
330
+ // Where no start is found at all (the look point inside a solid) the camera
331
+ // is held by a ray alone, as it was before the ball (`onRay`): a margin
332
+ // short of the hit, no nearer the look point than CAMERA_NEAREST, but never
333
+ // past a real surface: then SURFACE_GAP short of it.
334
+ const CAMERA_NEAREST = 0.6;
335
+ const SURFACE_GAP = 0.01;
336
+ // The lens the orbit camera is drawn with (world3d.js `orbitFov`, which sets
337
+ // it on the camera at the draw that follows this placement): the scene's
338
+ // `engine.camera.fov`, 10 to 120 degrees, else 70.
339
+ const ORBIT_FOV = 70;
340
+ function orbitFov(scene) {
341
+ const fov = scene.data?.engine?.camera?.fov;
342
+ return typeof fov === 'number' && fov >= 10 && fov <= 120 ? fov : ORBIT_FOV;
343
+ }
344
+ // The margin for this frame's lens: CAMERA_MARGIN, or the near plane's
345
+ // corners' distance from the camera, near * sqrt(1 + tan^2(v/2) +
346
+ // (aspect tan(v/2))^2), plus LENS_CLEARANCE, whichever is more. A point of
347
+ // the near plane is no farther from the camera than its corners, so a camera
348
+ // that far off a surface, square to it, has the whole near plane in front of
349
+ // it.
350
+ function lensMargin(scene, cam) {
351
+ const near = Number.isFinite(cam.near) && cam.near > 0 ? cam.near : 0.1;
352
+ const aspect = Number.isFinite(cam.aspect) && cam.aspect > 0 ? cam.aspect : 1;
353
+ const half = Math.tan((orbitFov(scene) * DEG) / 2);
354
+ const corner = near * Math.sqrt(1 + half * half + aspect * half * (aspect * half));
355
+ return Math.max(CAMERA_MARGIN, corner + LENS_CLEARANCE);
356
+ }
357
+ // How fast the camera moves back out once nothing is in the way.
358
+ const CAMERA_OUT_RATE = 6;
359
+ // Within this of the distance it wants, a camera let back out is let go.
360
+ const CAMERA_OUT_DONE = 1e-4;
361
+ // A camera something else placed is eased on from where it was left at this
362
+ // rate: a twentieth of the way is left after 0.3 s.
363
+ const TAKEOVER_RATE = 10;
364
+ // A settled shot uses its exact authored pose, avoiding round-trip drift.
365
+ const SHOT_SETTLED = 1e-10;
366
+ // Stay off the poles, where lookAt cannot retain the shot's azimuth.
367
+ const SHOT_PITCH_LIMIT = 89 * DEG;
368
+ const SHOT_YAW_EPS = 1e-8;
369
+ // The camera was placed on the frame before this one when the scene time it
370
+ // was last placed at is this frame's length ago, to within this (seconds).
371
+ const PLACED_WITHIN = 1e-6;
372
+ // A takeover blend farther than this from the camera's place has its drawn
373
+ // point tested for collision too (a second sweep, only while it lasts).
374
+ const BLEND_TESTED = 1e-4;
375
+
376
+ const clamp = (value, least, most) => Math.max(least, Math.min(most, value));
377
+ // The share of the way an easing at `rate` covers in `dt`.
378
+ const eased = (rate, dt) => 1 - Math.exp(-rate * dt);
379
+ // An angle as the shortest way round: -PI..PI.
380
+ function arc(angle) {
381
+ const turns = angle - 2 * Math.PI * Math.floor((angle + Math.PI) / (2 * Math.PI));
382
+ return Number.isFinite(turns) ? turns : 0;
383
+ }
384
+
385
+ function held(keys, plus, minus) {
386
+ return (plus.some((code) => keys.has(code)) ? 1 : 0) - (minus.some((code) => keys.has(code)) ? 1 : 0);
387
+ }
388
+
389
+ // The cross-scene store with the camera's angle in it.
390
+ function persistentOf(scene) {
391
+ const per = (scene.persistent ??= {});
392
+ if (per.camAngle === undefined) per.camAngle = Math.PI / 4;
393
+ return per;
394
+ }
395
+
396
+ // This frame's walk: a direction on the ground no longer than 1, or null
397
+ // standing still. W/S and up/down walk away from and toward the camera, A/D
398
+ // strafe, the stick does both by how far it is pushed; left/right swing the
399
+ // camera at `turnSpeed`. Under a held shot the camera cannot swing, so
400
+ // left/right strafe like A/D instead. The character's facing eases toward
401
+ // the walk and stays put standing still.
402
+ function driveOrbiting(scene, per, rt, props, dt) {
403
+ const keys = scene.keys;
404
+ const shot = validOrbitShot(scene.orbitShot);
405
+ if (!shot) per.camAngle += held(keys, ['ArrowRight'], ['ArrowLeft']) * props.turnSpeed * dt;
406
+ let ahead = held(keys, ['KeyW', 'ArrowUp'], ['KeyS', 'ArrowDown']);
407
+ let aside = shot ? held(keys, ['KeyD', 'ArrowRight'], ['KeyA', 'ArrowLeft']) : held(keys, ['KeyD'], ['KeyA']);
408
+ const stick = scene.moveInput;
409
+ if (ahead === 0 && aside === 0 && stick?.active && Math.hypot(stick.x, stick.y) >= 0.15) {
410
+ ahead = -stick.y;
411
+ aside = stick.x;
412
+ }
413
+ const length = Math.hypot(ahead, aside);
414
+ if (length === 0) return null;
415
+ const scale = length > 1 ? 1 / length : 1;
416
+ const sin = Math.sin(per.camAngle);
417
+ const cos = Math.cos(per.camAngle);
418
+ const move = { x: (sin * ahead + cos * aside) * scale, z: (sin * aside - cos * ahead) * scale };
419
+ rt.facing += arc(Math.atan2(move.x, move.z) - rt.facing) * eased(FACE_RATE, dt);
420
+ return move;
421
+ }
422
+
423
+ // The jump and the fall-faster pull, as `update` has them under the camera
424
+ // key (the hover is a force there, and so is the pull).
425
+ function jumpOrbiting(phys, actor, scene, rt, grounded, jumpSpeed, dt) {
426
+ const tapped = scene.moveInput?.consumeJump?.();
427
+ rt.jumpBuffer = tapped ? 0.18 : Math.max(0, (rt.jumpBuffer ?? 0) - dt);
428
+ if (grounded && (rt.jumpBuffer > 0 || keyDown(scene.keys, 'Space'))) {
429
+ rt.jumpBuffer = 0;
430
+ const { x, z } = phys.getVelocity(actor);
431
+ phys.setVelocity(actor, { x, y: jumpSpeed, z });
432
+ phys.resetForces(actor);
433
+ }
434
+ if (!grounded && phys.getVelocity(actor).y < 1) pullSteadily(phys, actor);
435
+ }
436
+
437
+ function hudOrbiting(scene, actor, t, per, facing) {
438
+ const left = scene.actorsWith('Pickup').length;
439
+ const all = per.score + left;
440
+ let score = '';
441
+ if (all !== 0) score = left === 0 ? '◆ all shards found' : `◆ ${per.score}/${all}`;
442
+ scene.hud = { score, look: findLookText(scene, actor, t, facing) };
443
+ }
444
+
445
+ // `Player.update` under the key.
446
+ function updateOrbiting(actor, scene, props, dt) {
447
+ scene.followCamera = { id: actor.id, data: scene.data, dt, place: placeOrbiting };
448
+ scene.orbitInput?.();
449
+ scene.lookInput?.listen?.();
450
+ const rt = actor.runtime;
451
+ // A facing asked for (`scene.faceRequest`, radians: the hub's arrival
452
+ // facing, a jump to a door) is taken once. Without the key the character
453
+ // always faces PI - camAngle, so code written for that sets camAngle;
454
+ // under it the camera and the character turn apart, and turning the
455
+ // character is asked for.
456
+ if (scene.faceRequest !== undefined) {
457
+ if (typeof scene.faceRequest === 'number' && Number.isFinite(scene.faceRequest)) rt.facing = scene.faceRequest;
458
+ scene.faceRequest = undefined;
459
+ }
460
+ const phys = scene.physics3d;
461
+ if (!phys?.ready()) return;
462
+ const t = actor.components.Transform;
463
+ const per = persistentOf(scene);
464
+ if (per.score === undefined) per.score = 0;
465
+ // The first frame of an actor (actor.runtime is rebuilt on a load) faces
466
+ // away from the camera, as the keyless Player does.
467
+ if (typeof rt.facing !== 'number') rt.facing = Math.PI - per.camAngle;
468
+ const shape = actor.components.Shape;
469
+ const grounded = hoverSteadily(phys, actor, t, { radius: shape?.radius ?? 0.35, height: shape?.height ?? 1.4 });
470
+ applyMove(phys, actor, driveOrbiting(scene, per, rt, props, dt), props.speed, grounded, dt);
471
+ t.rotationY = rt.facing / DEG;
472
+ jumpOrbiting(phys, actor, scene, rt, grounded, props.jumpSpeed, dt);
473
+ hudOrbiting(scene, actor, t, per, rt.facing);
474
+ }
475
+
476
+ const finiteOr = (value, otherwise) => (Number.isFinite(value) ? value : otherwise);
477
+
478
+ // The tilt and distance the player has chosen, on the runtime: they start
479
+ // at the scene's starting pitch and distance (or the defaults), and start
480
+ // again when those keys or camDistance/camHeight change. The latter two
481
+ // ask for a fresh camera but do not set its orbit distance or pitch.
482
+ function orbitChoice(scene, props) {
483
+ const distance = finiteOr(props.camDistance, 9);
484
+ const height = finiteOr(props.camHeight, 7);
485
+ // Only a finite number is a start; anything else is the default, so a
486
+ // copy of the same scene data (a fresh {} included) never restarts it.
487
+ const startDistance = finiteOr(scene.data?.engine?.camera?.distance, undefined);
488
+ const startPitch = finiteOr(scene.data?.engine?.camera?.pitch, undefined);
489
+ const chosen = scene.cameraOrbit;
490
+ if (chosen && chosen.distance === distance && chosen.height === height
491
+ && Object.is(chosen.startDistance, startDistance) && Object.is(chosen.startPitch, startPitch)) return chosen;
492
+ scene.cameraOrbit = {
493
+ distance,
494
+ height,
495
+ startDistance,
496
+ startPitch,
497
+ range: clamp(finiteOr(startDistance, ORBIT_START), ORBIT_NEAREST, ORBIT_FARTHEST),
498
+ pitch: Number.isFinite(startPitch) ? clamp(startPitch * DEG, ORBIT_PITCH_LEAST, ORBIT_PITCH_MOST) : ORBIT_PITCH_START,
499
+ nearest: ORBIT_NEAREST,
500
+ farthest: ORBIT_FARTHEST,
501
+ };
502
+ return scene.cameraOrbit;
503
+ }
504
+
505
+ // What the player asked of the camera this frame, onto what it is aimed for.
506
+ function steerOrbit(per, chosen, look) {
507
+ per.camAngle += look.yaw;
508
+ chosen.pitch = clamp(chosen.pitch + look.pitch, ORBIT_PITCH_LEAST, ORBIT_PITCH_MOST);
509
+ chosen.range = clamp(chosen.range * look.zoom, chosen.nearest, chosen.farthest);
510
+ }
511
+
512
+ // The ray from the point the camera looks at (the eased one the camera is
513
+ // placed from) toward where it wants to be: a unit direction.
514
+ const ray = { from: { x: 0, y: 0, z: 0 }, dir: { x: 0, y: 0, z: 0 } };
515
+
516
+ // Whether this frame's camera collides: a physics world, and an api with the
517
+ // lens's queries (systems/physics3d.js `castBallFixed`, `nearFixed`,
518
+ // `castRayFixedNormal`).
519
+ const collides = (phys) => Boolean(phys?.castBallFixed && phys.nearFixed && phys.castRayFixedNormal && phys.ready());
520
+
521
+ // Where the camera's ball is swept from this frame (`findStart`), and its
522
+ // radius: the look point (`ray.from`) and the margin, or, near surfaces, a
523
+ // point moved off them (and, in a gap narrower than twice the margin, a
524
+ // smaller ball). `clear` is false when no start was found; then it is the
525
+ // look point and the camera is held by a ray alone (`sweep`).
526
+ const start = { x: 0, y: 0, z: 0, radius: 0, clear: true };
527
+
528
+ // The ball must start clear of everything (rapier does not answer a ball
529
+ // that starts touching a solid reliably). Every fixed surface within the
530
+ // radius and START_SKIN of the start (`nearFixed`) gives a plane touching
531
+ // its solid at its nearest point, with all of a convex solid behind it; the
532
+ // start is the look point moved the least way that puts it that far in
533
+ // front of every plane found so far, all together (`leastMove`), so in a
534
+ // corner the moves do not undo each other; then it is looked around again,
535
+ // until nothing is that near. Each plane keeps the ball off its solid, so
536
+ // planes found from earlier places still hold. The move grows from nothing
537
+ // as the look point comes nearer, so the camera does not jump. Where no
538
+ // place is that far off every plane (a gap narrower than twice the radius)
539
+ // the place that falls least short is taken with the ball made that much
540
+ // smaller, no smaller than START_LEAST. Then the camera point stays out of
541
+ // the walls but the lens does not: its near plane cuts into them (accepted,
542
+ // as Roblox's camera clips there; the near plane is not shrunk). One query
543
+ // in the open; two by a wall or in a corner.
544
+ function findStart(phys, margin) {
545
+ start.x = ray.from.x;
546
+ start.y = ray.from.y;
547
+ start.z = ray.from.z;
548
+ start.radius = margin;
549
+ start.clear = true;
550
+ if (!collides(phys)) return;
551
+ planes.length = 0;
552
+ let radius = margin;
553
+ for (let i = 0; i < START_TRIES; i += 1) {
554
+ const near = phys.nearFixed(start, radius + START_SKIN);
555
+ let closest = Infinity;
556
+ for (const n of near) {
557
+ if (n.inside || !n.normal || !(n.distance >= 0)) return giveUpStart();
558
+ closest = Math.min(closest, n.distance);
559
+ }
560
+ if (closest >= radius + START_SKIN - START_SETTLED) {
561
+ start.radius = radius;
562
+ return;
563
+ }
564
+ for (const n of near) if (n.distance < radius + START_SKIN - START_SETTLED) addPlane(n);
565
+ const move = leastMove(radius + START_SKIN);
566
+ if (move.short > 0) radius -= move.short;
567
+ if (!(radius >= START_LEAST)) return giveUpStart();
568
+ start.x = ray.from.x + move.x;
569
+ start.y = ray.from.y + move.y;
570
+ start.z = ray.from.z + move.z;
571
+ }
572
+ return giveUpStart();
573
+ }
574
+ function giveUpStart() {
575
+ start.x = ray.from.x;
576
+ start.y = ray.from.y;
577
+ start.z = ray.from.z;
578
+ start.clear = false;
579
+ }
580
+
581
+ // The planes `findStart` has found this frame: a unit normal facing the
582
+ // look point's side and the offset along it (n . p = d on the plane).
583
+ const planes = [];
584
+ const MOST_PLANES = 12;
585
+ function addPlane(near) {
586
+ const n = near.normal;
587
+ const d = n.x * near.point.x + n.y * near.point.y + n.z * near.point.z;
588
+ if (!Number.isFinite(d) || planes.length >= MOST_PLANES) return;
589
+ for (const p of planes) if (p.x * n.x + p.y * n.y + p.z * n.z > 1 - 1e-9 && Math.abs(p.d - d) < 1e-6) return;
590
+ planes.push({ x: n.x, y: n.y, z: n.z, d });
591
+ }
592
+
593
+ // The least move from the look point that leaves it `gap` or more in front
594
+ // of every plane: the nearest point of the intersection of their
595
+ // half-spaces, which lies on at most three of the planes, so it is the
596
+ // nearest of the moves onto each set of up to three that clears them all.
597
+ // When none does, the one that falls least short (`short`, how much).
598
+ const best = { x: 0, y: 0, z: 0, short: 0 };
599
+ const trial = { x: 0, y: 0, z: 0 };
600
+ const want = [];
601
+ function leastMove(gap) {
602
+ const at = ray.from;
603
+ want.length = 0;
604
+ for (const p of planes) want.push(gap - (p.x * at.x + p.y * at.y + p.z * at.z - p.d));
605
+ let bestShort = Infinity;
606
+ let bestLength = Infinity;
607
+ const consider = () => {
608
+ let short = 0;
609
+ for (let i = 0; i < planes.length; i += 1) {
610
+ const p = planes[i];
611
+ short = Math.max(short, want[i] - (p.x * trial.x + p.y * trial.y + p.z * trial.z));
612
+ }
613
+ const length = Math.hypot(trial.x, trial.y, trial.z);
614
+ const fits = short <= 1e-9;
615
+ const bestFits = bestShort <= 1e-9;
616
+ if ((fits && (!bestFits || length < bestLength)) || (!fits && !bestFits && (short < bestShort - 1e-12 || (short <= bestShort + 1e-12 && length < bestLength)))) {
617
+ bestShort = short;
618
+ bestLength = length;
619
+ best.x = trial.x;
620
+ best.y = trial.y;
621
+ best.z = trial.z;
622
+ }
623
+ };
624
+ trial.x = 0;
625
+ trial.y = 0;
626
+ trial.z = 0;
627
+ consider();
628
+ const n = planes.length;
629
+ for (let i = 0; i < n; i += 1) {
630
+ const a = planes[i];
631
+ trial.x = a.x * want[i];
632
+ trial.y = a.y * want[i];
633
+ trial.z = a.z * want[i];
634
+ consider();
635
+ for (let j = i + 1; j < n; j += 1) {
636
+ const b = planes[j];
637
+ const c = a.x * b.x + a.y * b.y + a.z * b.z;
638
+ const det = 1 - c * c;
639
+ if (det > 1e-9) {
640
+ const s = (want[i] - c * want[j]) / det;
641
+ const t = (want[j] - c * want[i]) / det;
642
+ trial.x = a.x * s + b.x * t;
643
+ trial.y = a.y * s + b.y * t;
644
+ trial.z = a.z * s + b.z * t;
645
+ consider();
646
+ }
647
+ for (let k = j + 1; k < n; k += 1) {
648
+ const e = planes[k];
649
+ // Cramer's rule on the rows a, b, e.
650
+ const bxeX = b.y * e.z - b.z * e.y;
651
+ const bxeY = b.z * e.x - b.x * e.z;
652
+ const bxeZ = b.x * e.y - b.y * e.x;
653
+ const exaX = e.y * a.z - e.z * a.y;
654
+ const exaY = e.z * a.x - e.x * a.z;
655
+ const exaZ = e.x * a.y - e.y * a.x;
656
+ const axbX = a.y * b.z - a.z * b.y;
657
+ const axbY = a.z * b.x - a.x * b.z;
658
+ const axbZ = a.x * b.y - a.y * b.x;
659
+ const det3 = a.x * bxeX + a.y * bxeY + a.z * bxeZ;
660
+ if (Math.abs(det3) <= 1e-9) continue;
661
+ trial.x = (want[i] * bxeX + want[j] * exaX + want[k] * axbX) / det3;
662
+ trial.y = (want[i] * bxeY + want[j] * exaY + want[k] * axbY) / det3;
663
+ trial.z = (want[i] * bxeZ + want[j] * exaZ + want[k] * axbZ) / det3;
664
+ consider();
665
+ }
666
+ }
667
+ }
668
+ best.short = bestShort <= 1e-9 ? 0 : bestShort;
669
+ return best;
670
+ }
671
+
672
+ // The farthest the camera may be along a ray that met `hit`, when it is held
673
+ // by a ray alone: the margin short of it, never nearer than CAMERA_NEAREST,
674
+ // except that a hit with a normal (a real surface: systems/physics3d.js
675
+ // `castRayFixedNormal`) is never passed: then no farther than SURFACE_GAP
676
+ // short of it.
677
+ function onRay(hit, margin) {
678
+ const most = Math.max(CAMERA_NEAREST, hit.distance - margin);
679
+ return hit.normal ? Math.min(most, Math.max(0, hit.distance - SURFACE_GAP)) : most;
680
+ }
681
+
682
+ // How far from `start` along the unit `dir` the camera may be, looking no
683
+ // farther than `length`: where a ball of `start.radius` (the margin, but in
684
+ // a gap too narrow for it) swept that way first touches a fixed solid (its
685
+ // time of impact), or +Infinity when it touches nothing. The camera there
686
+ // is that far off what stopped it and off everything it passed, so the
687
+ // whole near plane is in front of every surface, at any angle. Without a
688
+ // start, `onRay` of a ray from the look point. One query.
689
+ function sweep(phys, dir, length, margin) {
690
+ if (start.clear) {
691
+ const hit = phys.castBallFixed(start, dir, length, start.radius);
692
+ return hit ? Math.max(0, hit.distance) : Infinity;
693
+ }
694
+ const hit = phys.castRayFixedNormal(start, dir, length + margin);
695
+ return hit ? onRay(hit, margin) : Infinity;
696
+ }
697
+
698
+ // The farthest the camera may be along `ray` from `start` (`state.limit`,
699
+ // +Infinity when nothing holds it), Roblox's Popper: the sweep toward where
700
+ // the camera wants to be (its distance), each frame. A nearer limit is taken
701
+ // at once; a farther one, or none, is eased out to at CAMERA_OUT_RATE, never
702
+ // past what this frame's sweep touched, and once the camera is back at its
703
+ // distance it is let go. The ease is measured from the look point, from how
704
+ // far from it the camera was drawn last frame (`state.out`), not along this
705
+ // frame's ray: the start moves as the player nears or leaves a wall, so a
706
+ // limit along the ray from last frame's start is not where the camera was
707
+ // (review 7: a wall 0.5 m off removed let the camera out 1.161 m in a frame
708
+ // where easing allows 1.139). And it eases whatever drew the camera short of
709
+ // its distance, a start moved off a wall too, not only a sweep that held it
710
+ // (review 8: a box beside the look point removed let it out 0.062 m where
711
+ // 0.006 m is allowed); only a zoom moves it at its own pace. The camera is drawn at the nearer of its
712
+ // distance and the limit, so an easing camera never passes what the sweep
713
+ // touches. Not asked while physics has no world.
714
+ // Queries a frame: one `nearFixed` in the open, two by a wall or in a corner
715
+ // (START_TRIES at most), and one sweep, one more while a takeover blend
716
+ // lasts (`holdBlend`). A sweep is three rapier queries as a rule and 25 at
717
+ // most (systems/physics3d.js `castBallFixed`). As a rule 4 in the open and 5
718
+ // by a wall; at most 8 + 2 * 25 = 58 (a blend, a slanted corner, and rapier
719
+ // missing solids in both sweeps). None without a world.
720
+ function limitReach(phys, state, margin, dt) {
721
+ findStart(phys, margin);
722
+ if (!collides(phys)) return;
723
+ const was = state.out;
724
+ // A camera just placed: where the sweep allows, at once.
725
+ if (!Number.isFinite(was) || !Number.isFinite(state.outRange)) {
726
+ state.limit = sweep(phys, ray.dir, state.range, margin);
727
+ return;
728
+ }
729
+ // How far short of its distance it was drawn last frame (held by a wall,
730
+ // or its start moved off one), eased away at CAMERA_OUT_RATE; and the
731
+ // distance itself as the player zooms, at the zoom's own pace. So it is
732
+ // let out at 6/s whatever drew it short, and a nearer wall takes it in at
733
+ // once. Along this frame's ray from this frame's start. The ball is swept
734
+ // only as far as the camera will be drawn, so the place drawn is the
735
+ // place the sweep checked (`castBallFixed` confirms its end is clear).
736
+ // (Zoomed in since, it is short of no more than the new distance.)
737
+ const short = Math.max(0, Math.min(state.outRange, state.range) - was);
738
+ const free = short <= CAMERA_OUT_DONE;
739
+ const letOut = free ? Infinity : alongRay(state.range - short * (1 - eased(CAMERA_OUT_RATE, dt)));
740
+ const wall = sweep(phys, ray.dir, Math.min(state.range, letOut), margin);
741
+ state.limit = free && wall === Infinity ? Infinity : Math.min(wall, letOut);
742
+ }
743
+
744
+ // How far along `ray` (or the unit `dir`) from `start` the camera is `out`
745
+ // from the look point
746
+ // (the farther place, where it moves away from the look point); the place
747
+ // nearest the look point when none is that far.
748
+ function alongRay(out, dir = ray.dir) {
749
+ const sx = start.x - ray.from.x;
750
+ const sy = start.y - ray.from.y;
751
+ const sz = start.z - ray.from.z;
752
+ const ahead = sx * dir.x + sy * dir.y + sz * dir.z;
753
+ const square = ahead * ahead - (sx * sx + sy * sy + sz * sz) + out * out;
754
+ return Math.max(0, -ahead + Math.sqrt(Math.max(0, square)));
755
+ }
756
+
757
+ // How far from its place (x, y, z) the camera is drawn this frame: nothing,
758
+ // unless something else placed it (`mine` is false), and then what is left
759
+ // of the way from where it was put (`at`).
760
+ function takenOver(off, at, x, y, z, mine, jumped, dt) {
761
+ if (!mine) {
762
+ // A teleport, or a camera more than 20 m off, snaps as the others do.
763
+ const snap = jumped || Math.hypot(x - at.x, z - at.z) > 20;
764
+ off.x = snap ? 0 : at.x - x;
765
+ off.y = snap ? 0 : at.y - y;
766
+ off.z = snap ? 0 : at.z - z;
767
+ }
768
+ const left = Math.exp(-TAKEOVER_RATE * dt);
769
+ off.x *= left;
770
+ off.y *= left;
771
+ off.z *= left;
772
+ return off;
773
+ }
774
+
775
+ // The point drawn while a takeover blend lasts (its place plus `off`) is
776
+ // collided as its place is: swept toward from `start`, and if the ball
777
+ // touches a fixed solid short of it, it is pulled in along that line to
778
+ // where the ball stopped, and the pull is kept in `off`, so the next frame
779
+ // eases on from where it was drawn. One more query, only while it lasts.
780
+ // Returns whether it was pulled in.
781
+ const drawn = { x: 0, y: 0, z: 0 };
782
+
783
+ function holdBlend(phys, off, x, y, z, margin) {
784
+ if (!collides(phys)) return;
785
+ drawn.x = x + off.x - start.x;
786
+ drawn.y = y + off.y - start.y;
787
+ drawn.z = z + off.z - start.z;
788
+ const length = Math.hypot(drawn.x, drawn.y, drawn.z);
789
+ if (!(length > 0)) return false;
790
+ drawn.x /= length;
791
+ drawn.y /= length;
792
+ drawn.z /= length;
793
+ const most = sweep(phys, drawn, length, margin);
794
+ if (most >= length) return false;
795
+ off.x = start.x + drawn.x * most - x;
796
+ off.y = start.y + drawn.y * most - y;
797
+ off.z = start.z + drawn.z * most - z;
798
+ return true;
799
+ }
800
+
801
+ // While a takeover blend lasts: a blend held in last frame is let out at
802
+ // the collision pace (`easeBlendOut`), and the point drawn is collided
803
+ // (`holdBlend`). Whether a wall has held it in, this frame or before in
804
+ // this blend: from then on, to the blend's end, it is let out at the
805
+ // collision pace.
806
+ function blendHeldIn(phys, state, off, x, y, z, margin, dt) {
807
+ const was = state.blendHeld === true && Number.isFinite(state.drawnOut);
808
+ if (was) easeBlendOut(state, off, x, y, z, dt);
809
+ return holdBlend(phys, off, x, y, z, margin) || was;
810
+ }
811
+
812
+ // A blend a wall held in (`holdBlend`, last frame) is let back out at the
813
+ // collision pace, not the blend's: no farther from the look point than
814
+ // easing at CAMERA_OUT_RATE from where it was drawn (`state.drawnOut`)
815
+ // toward where the camera rests this frame (review 10: not its distance,
816
+ // which a start moved off a wall can leave short of the place, holding the
817
+ // blend open for good), as `limitReach` lets its place out, so a
818
+ // wall that goes does not let the blend's TAKEOVER_RATE carry it out
819
+ // (review 9: 0.246 m in a frame where 0.179 m is allowed). Pulled in along
820
+ // its own line from `start`, and the pull kept in `off`. A blend whose own
821
+ // point is no farther than that goes on at its own pace. Returns whether it
822
+ // was held.
823
+ const blendDir = { x: 0, y: 0, z: 0 };
824
+ function easeBlendOut(state, off, x, y, z, dt) {
825
+ const px = x + off.x;
826
+ const py = y + off.y;
827
+ const pz = z + off.z;
828
+ const own = Math.hypot(px - ray.from.x, py - ray.from.y, pz - ray.from.z);
829
+ const was = state.drawnOut;
830
+ // Toward where the camera rests this frame (its place, which a start
831
+ // moved off a wall can put farther out than its distance), and let go
832
+ // within CAMERA_OUT_DONE of it, so a held blend always ends.
833
+ const rest = Math.hypot(x - ray.from.x, y - ray.from.y, z - ray.from.z);
834
+ const allowed = was + Math.max(0, rest - was) * eased(CAMERA_OUT_RATE, dt);
835
+ if (own <= allowed || rest - allowed <= CAMERA_OUT_DONE) return false;
836
+ blendDir.x = px - start.x;
837
+ blendDir.y = py - start.y;
838
+ blendDir.z = pz - start.z;
839
+ const length = Math.hypot(blendDir.x, blendDir.y, blendDir.z);
840
+ if (!(length > 0)) return false;
841
+ blendDir.x /= length;
842
+ blendDir.y /= length;
843
+ blendDir.z /= length;
844
+ const along = Math.min(length, alongRay(allowed, blendDir));
845
+ off.x = start.x + blendDir.x * along - x;
846
+ off.y = start.y + blendDir.y * along - y;
847
+ off.z = start.z + blendDir.z * along - z;
848
+ return true;
849
+ }
850
+
851
+ // Client-side, per-player shots only belong to the orbit camera. Neither
852
+ // its chosen angles nor its lens are changed; the deck places the shot clear
853
+ // of solids. Scratch is shared, while the drawn look travels with the actor.
854
+ const shotPose = { x: 0, y: 0, z: 0, yaw: 0, pitch: 0 };
855
+ function validOrbitShot(shot) {
856
+ const valid = shot && shot.look && Number.isFinite(shot.look.x) && Number.isFinite(shot.look.y)
857
+ && Number.isFinite(shot.look.z) && Number.isFinite(shot.distance) && shot.distance > 0
858
+ && Number.isFinite(shot.pitch) && Number.isFinite(shot.yaw);
859
+ if (!valid) return null;
860
+ const pitch = clamp(shot.pitch, -89, 89) * DEG;
861
+ const yaw = shot.yaw * DEG;
862
+ const flat = Math.cos(pitch);
863
+ shotPose.x = shot.look.x - flat * Math.sin(yaw) * shot.distance;
864
+ shotPose.y = shot.look.y + Math.sin(pitch) * shot.distance;
865
+ shotPose.z = shot.look.z + flat * Math.cos(yaw) * shot.distance;
866
+ shotPose.yaw = Math.atan2(flat * Math.sin(yaw), flat * Math.cos(yaw));
867
+ shotPose.pitch = pitch;
868
+ // Finite inputs can still overflow when added; treat that as absent too.
869
+ return Number.isFinite(shotPose.x) && Number.isFinite(shotPose.y) && Number.isFinite(shotPose.z) ? shot : null;
870
+ }
871
+ function ownsShotCamera(scene, drawnLook, cam, dt) {
872
+ return typeof drawnLook.placedAt === 'number' && Math.abs(scene.time - dt - drawnLook.placedAt) < PLACED_WITHIN
873
+ && drawnLook.atX === cam.position.x && drawnLook.atY === cam.position.y && drawnLook.atZ === cam.position.z
874
+ && drawnLook.qX === cam.quaternion.x && drawnLook.qY === cam.quaternion.y
875
+ && drawnLook.qZ === cam.quaternion.z && drawnLook.qW === cam.quaternion.w;
876
+ }
877
+ function recordShotCamera(scene, drawnLook, cam) {
878
+ drawnLook.placedAt = scene.time;
879
+ drawnLook.atX = cam.position.x;
880
+ drawnLook.atY = cam.position.y;
881
+ drawnLook.atZ = cam.position.z;
882
+ drawnLook.qX = cam.quaternion.x;
883
+ drawnLook.qY = cam.quaternion.y;
884
+ drawnLook.qZ = cam.quaternion.z;
885
+ drawnLook.qW = cam.quaternion.w;
886
+ }
887
+ function placeOrbitShot(scene, cam, rt, state, before, t, shot, dt) {
888
+ const drawnLook = (rt.orbitShot ??= { active: false, returning: false, x: 0, y: 0, z: 0 });
889
+ const continuing = drawnLook.active || drawnLook.returning;
890
+ if (!continuing || !ownsShotCamera(scene, drawnLook, cam, dt)) {
891
+ // The previous orbit look point gives the focus distance. Along the
892
+ // actual drawn direction, so even a camera another system placed has
893
+ // no rotation snap on entry. No temporary vectors on the frame path.
894
+ const at = cam.position;
895
+ const distance = (continuing
896
+ ? Math.hypot(at.x - shot.look.x, at.y - shot.look.y, at.z - shot.look.z)
897
+ : Math.hypot(at.x - (before?.x ?? t.x), at.y - ((state.heightTo ?? t.y) + LOOK_HEIGHT), at.z - (before?.z ?? t.z))) || shot.distance;
898
+ const q = cam.quaternion;
899
+ drawnLook.x = at.x - 2 * (q.x * q.z + q.w * q.y) * distance;
900
+ drawnLook.y = at.y + 2 * (q.w * q.x - q.y * q.z) * distance;
901
+ drawnLook.z = at.z + (2 * (q.x * q.x + q.y * q.y) - 1) * distance;
902
+ // The camera's right vector retains azimuth even when its forward
903
+ // vector is vertical. Seed it on entry or an ownership interruption.
904
+ drawnLook.yaw = Math.atan2(2 * (q.x * q.z - q.w * q.y), 1 - 2 * (q.y * q.y + q.z * q.z));
905
+ }
906
+ drawnLook.active = true;
907
+ drawnLook.returning = false;
908
+ const at = cam.position;
909
+ if (at.x === shotPose.x && at.y === shotPose.y && at.z === shotPose.z
910
+ && drawnLook.x === shot.look.x && drawnLook.y === shot.look.y && drawnLook.z === shot.look.z) {
911
+ drawnLook.yaw = shotPose.yaw;
912
+ cam.lookAt(drawnLook.x, drawnLook.y, drawnLook.z);
913
+ recordShotCamera(scene, drawnLook, cam);
914
+ return;
915
+ }
916
+ const k = eased(TAKEOVER_RATE, dt);
917
+ // Ease around the drawn focus, so opposite shots never cross through it.
918
+ const dx = at.x - drawnLook.x;
919
+ const dy = at.y - drawnLook.y;
920
+ const dz = at.z - drawnLook.z;
921
+ const flat = Math.hypot(dx, dz);
922
+ const distance = Math.hypot(flat, dy);
923
+ const yaw = flat < SHOT_YAW_EPS * distance ? drawnLook.yaw : Math.atan2(-dx, dz);
924
+ const pitch = Math.atan2(dy, flat);
925
+ const nextYaw = yaw + arc(shotPose.yaw - yaw) * k;
926
+ const nextPitch = clamp(pitch + (shotPose.pitch - pitch) * k, -SHOT_PITCH_LIMIT, SHOT_PITCH_LIMIT);
927
+ const nextDistance = distance + (shot.distance - distance) * k;
928
+ drawnLook.yaw = nextYaw;
929
+ drawnLook.x += (shot.look.x - drawnLook.x) * k;
930
+ drawnLook.y += (shot.look.y - drawnLook.y) * k;
931
+ drawnLook.z += (shot.look.z - drawnLook.z) * k;
932
+ if (Math.abs(arc(shotPose.yaw - nextYaw)) < SHOT_SETTLED && Math.abs(shotPose.pitch - nextPitch) < SHOT_SETTLED
933
+ && Math.abs(shot.distance - nextDistance) < SHOT_SETTLED
934
+ && Math.hypot(shot.look.x - drawnLook.x, shot.look.y - drawnLook.y, shot.look.z - drawnLook.z) < SHOT_SETTLED) {
935
+ drawnLook.x = shot.look.x;
936
+ drawnLook.y = shot.look.y;
937
+ drawnLook.z = shot.look.z;
938
+ drawnLook.yaw = shotPose.yaw;
939
+ at.set(shotPose.x, shotPose.y, shotPose.z);
940
+ } else {
941
+ const nextFlat = Math.cos(nextPitch);
942
+ at.set(drawnLook.x - nextFlat * Math.sin(nextYaw) * nextDistance,
943
+ drawnLook.y + Math.sin(nextPitch) * nextDistance,
944
+ drawnLook.z + nextFlat * Math.cos(nextYaw) * nextDistance);
945
+ }
946
+ cam.lookAt(drawnLook.x, drawnLook.y, drawnLook.z);
947
+ recordShotCamera(scene, drawnLook, cam);
948
+ }
949
+
950
+ // The orbit camera, placed at the end of the frame as the rigid one is
951
+ // (`placeFollowing`): the player's ground position is not eased, its height
952
+ // is, as there. What the camera is doing between frames (`actor.runtime.orbit`)
953
+ // goes with the actor on a scene load. The camera is the player's to steer on
954
+ // a frame only when this also placed it on the frame before, the camera
955
+ // is where this left it, the player did not teleport, and the look input is
956
+ // not locked (the hub locks it through a door trip). Otherwise what was asked
957
+ // of it is dropped; and, all but for the lock, it starts again from what was
958
+ // chosen and eases on from where the camera was put (a hub trip's camera, a
959
+ // cutscene, the first frame, a Player turned off and on).
960
+ function placeOrbiting(scene, id, dt) {
961
+ const actor = scene.getActor(id);
962
+ const props = actor?.components.Player;
963
+ const t = actor?.components.Transform;
964
+ const cam = scene.three?.camera;
965
+ if (!cam || !t || !props || props.$enabled === false) return;
966
+ const rt = actor.runtime;
967
+ const per = persistentOf(scene);
968
+ const chosen = orbitChoice(scene, props);
969
+ const look = scene.lookInput?.consume?.();
970
+ const before = per.camLastAt;
971
+ const jumped = Boolean(before) && Math.hypot(t.x - before.x, t.z - before.z) > TELEPORT_PER_FRAME;
972
+ per.camLastAt = { x: t.x, z: t.z };
973
+ const at = cam.position;
974
+ const state = (rt.orbit ??= { off: { x: 0, y: 0, z: 0 } });
975
+ const shot = validOrbitShot(scene.orbitShot);
976
+ if (shot) {
977
+ placeOrbitShot(scene, cam, rt, state, before, t, shot, dt);
978
+ // `look` was consumed above and is deliberately dropped while held.
979
+ scene.orbitOwnModel = id;
980
+ return;
981
+ }
982
+ const drawnLook = rt.orbitShot;
983
+ const handBack = drawnLook?.active === true;
984
+ const keptShot = drawnLook && ownsShotCamera(scene, drawnLook, cam, dt);
985
+ if (handBack) {
986
+ drawnLook.active = false;
987
+ drawnLook.returning = keptShot;
988
+ drawnLook.offX = drawnLook.x - t.x;
989
+ drawnLook.offY = drawnLook.y - t.y - LOOK_HEIGHT;
990
+ drawnLook.offZ = drawnLook.z - t.z;
991
+ }
992
+ if (drawnLook && !keptShot) {
993
+ drawnLook.returning = false;
994
+ drawnLook.offX = drawnLook.offY = drawnLook.offZ = 0;
995
+ }
996
+ // Placed on the frame before too: a frame this did not place on (the
997
+ // Player turned off, even by a collision callback after its update)
998
+ // breaks the chain.
999
+ const placed = typeof state.placedAt === 'number' && Math.abs(scene.time - dt - state.placedAt) < PLACED_WITHIN;
1000
+ const mine = !handBack && placed && !jumped && state.x === at.x && state.y === at.y && state.z === at.z;
1001
+ if (mine) {
1002
+ if (look && !look.locked) steerOrbit(per, chosen, look);
1003
+ const k = eased(ORBIT_RATE, dt);
1004
+ state.yaw += arc(per.camAngle - state.yaw) * k;
1005
+ state.pitch += (chosen.pitch - state.pitch) * k;
1006
+ state.range += (chosen.range - state.range) * k;
1007
+ state.height = settle(state.height, state.heightTo, t.y, dt);
1008
+ } else {
1009
+ Object.assign(state, { yaw: per.camAngle, pitch: chosen.pitch, range: chosen.range, height: t.y, limit: Infinity, out: undefined, outRange: undefined, blendHeld: false, drawnOut: undefined });
1010
+ }
1011
+ state.heightTo = t.y;
1012
+ const flat = Math.cos(state.pitch);
1013
+ ray.dir.x = -flat * Math.sin(state.yaw);
1014
+ ray.dir.y = Math.sin(state.pitch);
1015
+ ray.dir.z = flat * Math.cos(state.yaw);
1016
+ // From the point the camera is placed from, so what is tested is the
1017
+ // camera that is drawn (less the takeover blend below, which is not).
1018
+ ray.from.x = t.x;
1019
+ ray.from.y = state.height + LOOK_HEIGHT;
1020
+ ray.from.z = t.z;
1021
+ const margin = lensMargin(scene, cam);
1022
+ limitReach(scene.physics3d, state, margin, dt);
1023
+ // Drawn at the nearer of its distance and the limit, out from the ball's
1024
+ // start (the look point, or a point moved off a surface it is pressed to).
1025
+ const reach = Math.max(Math.min(state.range, state.limit), 0);
1026
+ const x = start.x + ray.dir.x * reach;
1027
+ const y = start.y + ray.dir.y * reach;
1028
+ const z = start.z + ray.dir.z * reach;
1029
+ // How far from the look point it is placed, and the distance it was
1030
+ // placed for, for the next frame's ease.
1031
+ state.out = Math.hypot(x - ray.from.x, y - ray.from.y, z - ray.from.z);
1032
+ state.outRange = state.range;
1033
+ const off = takenOver(state.off, at, x, y, z, mine, jumped, dt);
1034
+ state.blendHeld = Math.hypot(off.x, off.y, off.z) > BLEND_TESTED && blendHeldIn(scene.physics3d, state, off, x, y, z, margin, dt);
1035
+ cam.position.set(x + off.x, y + off.y, z + off.z);
1036
+ // How far from the look point it is drawn, for a held blend's ease.
1037
+ state.drawnOut = Math.hypot(x + off.x - ray.from.x, y + off.y - ray.from.y, z + off.z - ray.from.z);
1038
+ state.placedAt = scene.time;
1039
+ state.x = at.x;
1040
+ state.y = at.y;
1041
+ state.z = at.z;
1042
+ if (drawnLook?.returning) {
1043
+ // Decay the hand-back offset, rather than chasing the moving player:
1044
+ // walking carries the look point immediately and cannot hold it open.
1045
+ const left = jumped ? 0 : Math.exp(-TAKEOVER_RATE * dt);
1046
+ drawnLook.offX *= left;
1047
+ drawnLook.offY *= left;
1048
+ drawnLook.offZ *= left;
1049
+ drawnLook.x = t.x + drawnLook.offX;
1050
+ drawnLook.y = t.y + LOOK_HEIGHT + drawnLook.offY;
1051
+ drawnLook.z = t.z + drawnLook.offZ;
1052
+ if (Math.hypot(drawnLook.offX, drawnLook.offY, drawnLook.offZ) < BLEND_TESTED) {
1053
+ drawnLook.offX = drawnLook.offY = drawnLook.offZ = 0;
1054
+ drawnLook.returning = false;
1055
+ } else {
1056
+ cam.lookAt(drawnLook.x, drawnLook.y, drawnLook.z);
1057
+ }
1058
+ }
1059
+ if (!drawnLook?.returning) {
1060
+ cam.lookAt(t.x, t.y + LOOK_HEIGHT, t.z);
1061
+ }
1062
+ if (drawnLook) recordShotCamera(scene, drawnLook, cam);
1063
+ // The actor the camera was placed for, for this frame's draw: its model is
1064
+ // left out of the frame if the final camera is inside it
1065
+ // (engine3d/ownmodel.js; the draw takes the name, a load drops it).
1066
+ scene.orbitOwnModel = id;
1067
+ }
1068
+
169
1069
  // The nearest Lookable the player is facing, within its range.
170
1070
  function findLookText(scene, actor, t, facing) {
171
1071
  const fx = Math.sin(facing);
@@ -204,6 +1104,7 @@ export class Player {
204
1104
  }
205
1105
 
206
1106
  update(actor, scene, dt) {
1107
+ if (scene.cameraControl === 'orbit') return updateOrbiting(actor, scene, this.props, dt);
207
1108
  if (scene.cameraFollowsRigidly === true) {
208
1109
  scene.followCamera = { id: actor.id, data: scene.data, dt, place: placeFollowing };
209
1110
  }
@@ -220,7 +1121,15 @@ export class Player {
220
1121
 
221
1122
  const shape = actor.components.Shape;
222
1123
  const dims = { radius: shape?.radius ?? 0.35, height: shape?.height ?? 1.4 };
223
- const grounded = applyHover(phys, actor, t, dims, dt);
1124
+ // Under the camera key the player is held steady on screen: the hover
1125
+ // and the fall-faster pull are forces (hoverSteadily). So they are under
1126
+ // the frame cap (`engine.maxFps`, `scene.maxFps` above 0), whose frames
1127
+ // are handed what they took, 1 to 3 physics steps' worth: there an
1128
+ // impulse per frame against gravity per step bobs the player with the
1129
+ // accumulator's carry (the kit's CLAUDE.md, maxFps). Without either key
1130
+ // every call below is the one made before the keys had this meaning.
1131
+ const steady = scene.cameraFollowsRigidly === true || scene.maxFps > 0;
1132
+ const grounded = steady ? hoverSteadily(phys, actor, t, dims) : applyHover(phys, actor, t, dims, dt);
224
1133
  const drive = readDrive(scene.keys, scene.moveInput);
225
1134
  per.camAngle += drive.turn * this.props.turnSpeed * dt;
226
1135
  rt.facing = Math.PI - per.camAngle;
@@ -243,15 +1152,24 @@ export class Player {
243
1152
  rt.jumpBuffer = 0;
244
1153
  const vel = phys.getVelocity(actor);
245
1154
  phys.setVelocity(actor, { x: vel.x, y: this.props.jumpSpeed, z: vel.z });
1155
+ // The jump replaces the velocity the hover was holding; this frame's
1156
+ // lift goes too, or it would be added to the jump.
1157
+ if (steady) phys.resetForces(actor);
246
1158
  }
247
1159
  // Fall faster than the rise: extra downward pull past the jump apex kills
248
1160
  // the floaty arc.
249
1161
  if (!grounded) {
250
1162
  const vel = phys.getVelocity(actor);
251
- if (vel.y < 1) phys.applyImpulse(actor, { x: 0, y: -14 * phys.mass(actor) * dt, z: 0 });
1163
+ if (vel.y < 1) {
1164
+ if (steady) pullSteadily(phys, actor);
1165
+ else phys.applyImpulse(actor, { x: 0, y: -14 * phys.mass(actor) * dt, z: 0 });
1166
+ }
252
1167
  }
253
1168
 
254
- if (scene.cameraFollowsRigidly !== true) updateCamera(scene, t, per, this.props, dt);
1169
+ if (scene.cameraFollowsRigidly !== true) {
1170
+ if (scene.maxFps > 0) scene.followCamera = { id: actor.id, data: scene.data, dt, place: placeCapped };
1171
+ else updateCamera(scene, t, per, this.props, dt);
1172
+ }
255
1173
  const remaining = scene.actorsWith('Pickup').length;
256
1174
  const total = per.score + remaining;
257
1175
  const score =