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,539 @@
1
+ // The choice card (contract v1.68, `hub.ballot`): a card of players' faces,
2
+ // the deck's fixed lines and a few options, for a meeting, a vote, a reveal.
3
+ // Only a place whose manifest declares `ballot` has any of it: no state, no
4
+ // message and no work otherwise.
5
+ //
6
+ // No free text: a player's taps reach the server as ids only -- a face (a
7
+ // player id on the card), a line (an id of the deck's manifest lines), an
8
+ // option (an id of the card's options) -- and every word on the card is
9
+ // either the deck's own (its server's title, text, notes and options, and its
10
+ // manifest's lines) or the kit's (usernames). Nothing a player types is ever
11
+ // carried.
12
+ //
13
+ // Two rules every path here obeys (contract §6, "The choice card"):
14
+ //
15
+ // (A) Each player's card has a version `v`. A `hub.ballot` call that changes
16
+ // what that player can tap -- the mode, the faces offered and their
17
+ // `off`, the lines offered, the option ids, `picked` (`actionKey`) --
18
+ // gives their card a new version (an opaque string: this server's own
19
+ // epoch and a counter, so a version from an earlier server for the same
20
+ // layer never equals one of this server's); a call that
21
+ // changes only title, text, until, notes, votes, marks, option words or
22
+ // the feed keeps it. A tap carries the `v` of the card it was made on,
23
+ // and is taken only if that `v` is the version last delivered to the
24
+ // player AND the version of their current card, and its ids are on that
25
+ // card and allowed by it (`allows`). The same version means the same
26
+ // taps allowed, so a refresh of the words never refuses a tap, and a
27
+ // changed or undelivered card never takes an old one.
28
+ //
29
+ // (B) Keys absent means no ballot code path runs at all, client or server:
30
+ // no allocation, no per-frame call, no disposal work, no listener. The
31
+ // only thing a place without `ballot` does is read the key and find it
32
+ // missing: here `h.place.ballot` before a message, `h.ballotState`
33
+ // (made by the first `hub.ballot` call) before a tick's send and a
34
+ // hello's or a leave's cleanup, and `manifest.ballot` before giving the
35
+ // deck `hub.ballot` at all.
36
+ //
37
+ // Ids index only Maps, arrays and objects made with no prototype: an id
38
+ // ("constructor", "__proto__") is never looked up through Object.prototype.
39
+
40
+ import { warnOnce } from './actors.js';
41
+ import { BALLOT_LINE_ID, cardText } from './manifest.js';
42
+ import * as Blocks from './blocks.js';
43
+
44
+ export const MODES = ['talk', 'vote', 'reveal'];
45
+ export const MAX_FACES = 24;
46
+ export const MAX_OPTIONS = 6;
47
+ export const MAX_FEED = 6;
48
+ export const MAX_VOTES = 99;
49
+ const TITLE_MAX = 40;
50
+ const TEXT_MAX = 120;
51
+ const NOTE_MAX = 24;
52
+ const OPTION_MAX = 32;
53
+ const MAX_TARGETS = 64;
54
+ const COLOR = /^#[0-9a-fA-F]{6}$/;
55
+ const SPEC_KEYS = ['title', 'text', 'until', 'mode', 'faces', 'options', 'lines', 'feed', 'picked'];
56
+ const FACE_KEYS = ['playerId', 'note', 'off', 'votes', 'mark'];
57
+ const MESSAGE_KEYS = ['k', 'v', 'face', 'line', 'option'];
58
+ // One player's taps: a burst of three, then one every 1.5 s (as `say.js`).
59
+ const BURST = 3;
60
+ const EVERY_MS = 1500;
61
+
62
+ function refuse(h, key, text) {
63
+ warnOnce(h, `ballot:${key}`, `hub.ballot: ${text}`);
64
+ return false;
65
+ }
66
+
67
+ const isObject = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
68
+
69
+ // A server's version prefix: its start time and a random part, base 36.
70
+ function newEpoch() {
71
+ return `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 8)}`;
72
+ }
73
+
74
+ function state(h) {
75
+ // shared: the card for everyone here; me: playerId -> their own card, or
76
+ // a follower copy of the shared card carrying their own v (`of`, always
77
+ // the current `shared`: a player whose own card was cleared keeps or
78
+ // renews their version by the card they had, rule A);
79
+ // sent: playerId -> JSON of the view last delivered; views: playerId ->
80
+ // that view (rule A's first half); rate: playerId -> { tokens, at };
81
+ // player's feed rows and name stay hidden.
82
+ // version: the last version given to a card (rule A).
83
+ // epoch: this server's own, so no version of an earlier server for the
84
+ // same layer (a replacement restarts the counter) can equal one of ours.
85
+ h.ballotState ??= { epoch: newEpoch(), version: 0, shared: null, me: new Map(), sent: new Map(), views: new Map(), rate: new Map() };
86
+ return h.ballotState;
87
+ }
88
+
89
+ // A deck's spec, checked: `{ card }` or `{ error }`. `lines` are the place's
90
+ // manifest lines.
91
+ export function cleanSpec(spec, lines) {
92
+ if (!isObject(spec)) {
93
+ return { error: 'a card is an object (or null to clear it)' };
94
+ }
95
+ for (const key of Object.keys(spec)) {
96
+ if (!SPEC_KEYS.includes(key)) {
97
+ return { error: `"${key}" is not part of a card` };
98
+ }
99
+ }
100
+ const card = { mode: spec.mode ?? 'talk' };
101
+ if (!MODES.includes(card.mode)) {
102
+ return { error: `mode must be one of ${MODES.join(', ')}` };
103
+ }
104
+ for (const [key, max] of [['title', TITLE_MAX], ['text', TEXT_MAX]]) {
105
+ if (spec[key] !== undefined) {
106
+ const text = cardText(spec[key], max);
107
+ if (text === null) {
108
+ return { error: `${key} must be 1-${max} characters on one line` };
109
+ }
110
+ card[key] = text;
111
+ }
112
+ }
113
+ if (spec.until !== undefined) {
114
+ if (typeof spec.until !== 'number' || !Number.isFinite(spec.until)) {
115
+ return { error: 'until must be a finite simulation time in ms (hub.now)' };
116
+ }
117
+ card.until = spec.until;
118
+ }
119
+ const faces = spec.faces ?? [];
120
+ if (!Array.isArray(faces) || faces.length > MAX_FACES) {
121
+ return { error: `faces must be a list of at most ${MAX_FACES}` };
122
+ }
123
+ card.faces = [];
124
+ const faceIds = new Set();
125
+ for (const face of faces) {
126
+ if (!isObject(face) || Object.keys(face).some((key) => !FACE_KEYS.includes(key))) {
127
+ return { error: `a face is { ${FACE_KEYS.join(', ')} }` };
128
+ }
129
+ if (typeof face.playerId !== 'string' || !face.playerId || faceIds.has(face.playerId)) {
130
+ return { error: 'each face names a different playerId' };
131
+ }
132
+ faceIds.add(face.playerId);
133
+ const out = { playerId: face.playerId };
134
+ if (face.note !== undefined) {
135
+ const note = cardText(face.note, NOTE_MAX);
136
+ if (note === null) {
137
+ return { error: `a face's note must be 1-${NOTE_MAX} characters` };
138
+ }
139
+ out.note = note;
140
+ }
141
+ if (face.off !== undefined && typeof face.off !== 'boolean') {
142
+ return { error: "a face's off must be true or false" };
143
+ }
144
+ if (face.off === true) {
145
+ out.off = true;
146
+ }
147
+ if (face.votes !== undefined) {
148
+ if (!Number.isInteger(face.votes) || face.votes < 0 || face.votes > MAX_VOTES) {
149
+ return { error: `a face's votes must be a whole number from 0 to ${MAX_VOTES}` };
150
+ }
151
+ out.votes = face.votes;
152
+ }
153
+ if (face.mark !== undefined) {
154
+ if (typeof face.mark !== 'string' || !COLOR.test(face.mark)) {
155
+ return { error: "a face's mark must be '#rrggbb'" };
156
+ }
157
+ out.mark = face.mark.toLowerCase();
158
+ }
159
+ card.faces.push(out);
160
+ }
161
+ const options = spec.options ?? [];
162
+ if (!Array.isArray(options) || options.length > MAX_OPTIONS) {
163
+ return { error: `options must be a list of at most ${MAX_OPTIONS}` };
164
+ }
165
+ card.options = [];
166
+ const optionIds = new Set();
167
+ for (const option of options) {
168
+ if (!isObject(option) || Object.keys(option).some((key) => key !== 'id' && key !== 'text')) {
169
+ return { error: 'an option is { id, text }' };
170
+ }
171
+ const text = cardText(option.text, OPTION_MAX);
172
+ if (typeof option.id !== 'string' || !BALLOT_LINE_ID.test(option.id) || optionIds.has(option.id) || text === null) {
173
+ return { error: `an option has a different id ([a-z0-9-], 1-24) and text of 1-${OPTION_MAX} characters` };
174
+ }
175
+ optionIds.add(option.id);
176
+ card.options.push({ id: option.id, text });
177
+ }
178
+ const byId = new Map(lines.map((line) => [line.id, line]));
179
+ if (spec.lines === undefined) {
180
+ card.lines = lines.map((line) => line.id);
181
+ } else {
182
+ if (!Array.isArray(spec.lines) || spec.lines.some((id) => !byId.has(id)) || new Set(spec.lines).size !== spec.lines.length) {
183
+ return { error: "lines must be different ids of the place's manifest ballot.lines" };
184
+ }
185
+ card.lines = [...spec.lines];
186
+ }
187
+ const feed = spec.feed ?? [];
188
+ if (!Array.isArray(feed) || feed.length > MAX_FEED) {
189
+ return { error: `feed must be a list of at most ${MAX_FEED}` };
190
+ }
191
+ card.feed = [];
192
+ for (const row of feed) {
193
+ if (!isObject(row) || Object.keys(row).some((key) => !['from', 'to', 'line'].includes(key))) {
194
+ return { error: 'a feed row is { from, to, line }' };
195
+ }
196
+ const line = byId.get(row.line);
197
+ const to = row.to ?? null;
198
+ if (typeof row.from !== 'string' || !row.from || !line || (to !== null && (typeof to !== 'string' || !to))) {
199
+ return { error: "a feed row names a player (from), a manifest line, and a player (to) or null" };
200
+ }
201
+ if ((line.about === 'face') !== (to !== null)) {
202
+ return { error: `feed line "${line.id}" is about ${line.about === 'face' ? 'a face: it needs to' : 'the speaker: it takes no to'}` };
203
+ }
204
+ card.feed.push({ from: row.from, to, line: line.id });
205
+ }
206
+ if (spec.picked !== undefined && spec.picked !== null) {
207
+ const picked = spec.picked;
208
+ if (!isObject(picked) || Object.keys(picked).some((key) => key !== 'face' && key !== 'option')) {
209
+ return { error: 'picked is { face } or { option }' };
210
+ }
211
+ if ((picked.face === undefined) === (picked.option === undefined)) {
212
+ return { error: 'picked names one face or one option' };
213
+ }
214
+ if (picked.face !== undefined && !faceIds.has(picked.face)) {
215
+ return { error: 'picked.face must be one of the faces' };
216
+ }
217
+ if (picked.option !== undefined && !optionIds.has(picked.option)) {
218
+ return { error: 'picked.option must be one of the options' };
219
+ }
220
+ card.picked = picked.face !== undefined ? { face: picked.face } : { option: picked.option };
221
+ }
222
+ return { card };
223
+ }
224
+
225
+ // What can be tapped on a card, as one string: its mode, the faces offered
226
+ // and which are `off`, the lines offered, the option ids and the lock. Two
227
+ // cards with the same key allow exactly the same taps; title, text, until,
228
+ // notes, votes, marks, option words and the feed are not in it.
229
+ export function actionKey(card) {
230
+ return JSON.stringify([
231
+ card.mode,
232
+ card.faces.map((face) => [face.playerId, face.off === true]),
233
+ card.lines,
234
+ card.options.map((option) => option.id),
235
+ card.picked ?? null,
236
+ ]);
237
+ }
238
+
239
+ // `hub.ballot(playerIds | null, spec | null)`.
240
+ export function ballot(h, playerIds, spec) {
241
+ if (!h.place?.ballot) {
242
+ return refuse(h, 'off', 'this place declares no `ballot` in its hub manifest');
243
+ }
244
+ let targets = null;
245
+ if (playerIds !== null && playerIds !== undefined) {
246
+ if (!Array.isArray(playerIds) || playerIds.length > MAX_TARGETS || playerIds.some((id) => typeof id !== 'string')) {
247
+ return refuse(h, 'targets', 'playerIds must be a list of player ids, or null for everyone here');
248
+ }
249
+ targets = playerIds;
250
+ }
251
+ let card = null;
252
+ if (spec !== null && spec !== undefined) {
253
+ const checked = cleanSpec(spec, h.place.ballot.lines);
254
+ if (checked.error) {
255
+ return refuse(h, `spec:${checked.error}`, checked.error);
256
+ }
257
+ card = checked.card;
258
+ }
259
+ const s = state(h);
260
+ // Rule A: a card keeps the version of the one it replaces for a player
261
+ // while what can be tapped on it is the same (`actionKey`); anything else
262
+ // is a new version. A pure comparison with that current card.
263
+ const next = (current, to) => {
264
+ if (current && current.key === to.key) {
265
+ return current.v;
266
+ }
267
+ s.version += 1;
268
+ return `${s.epoch}-${s.version}`;
269
+ };
270
+ if (card) {
271
+ card.key = actionKey(card);
272
+ }
273
+ if (targets === null) {
274
+ if (card) {
275
+ card.v = next(s.shared, card);
276
+ // A follower of the old shared card keeps its own version while the
277
+ // new one allows the same taps, else it takes the new card's.
278
+ for (const [playerId, entry] of s.me) {
279
+ if (!entry.of) {
280
+ continue;
281
+ }
282
+ if (entry.key === card.key) {
283
+ s.me.set(playerId, { ...card, v: entry.v, of: card });
284
+ } else {
285
+ s.me.delete(playerId);
286
+ }
287
+ }
288
+ }
289
+ s.shared = card;
290
+ // Clearing for everyone takes down every player's own card too.
291
+ if (!card) {
292
+ s.me.clear();
293
+ }
294
+ return true;
295
+ }
296
+ for (const playerId of targets) {
297
+ if (!h.sim?.players.has(playerId)) {
298
+ continue;
299
+ }
300
+ const prior = s.me.get(playerId);
301
+ if (card) {
302
+ s.me.set(playerId, { ...card, v: next(prior ?? s.shared, card) });
303
+ } else if (!s.shared) {
304
+ s.me.delete(playerId);
305
+ } else if (prior) {
306
+ // Back to the shared card, by rule A against the card they had: a
307
+ // follower copy carrying their own version.
308
+ s.me.set(playerId, { ...s.shared, v: next(prior, s.shared), of: s.shared });
309
+ }
310
+ }
311
+ return true;
312
+ }
313
+
314
+ // What one viewer is shown of a card: names and avatars are the kit's, from
315
+ // the session, so a player not here now is not on it (no face, no feed row
316
+ // from or to them); a player a block hides from the viewer is not on it
317
+ // either, and their name is taken out of every word the card shows (only a
318
+ // name of a player here now: a departed player's name in the deck's own words
319
+ // is not, as with the HUD and labels):
320
+ // the deck's title, text, notes and options here, and the manifest's lines
321
+ // as `words` (line id -> the line as this viewer reads it, only for a line
322
+ // the redaction changes). A lock on a face they cannot see stays a lock,
323
+ // `picked: {}`, without naming whom.
324
+ // `d` is `{ usernameOf(playerId) | null, avatarOf(playerId) | null, hidden: Set | null, names: [usernames], lines: [manifest lines] }`.
325
+ export function viewFor(card, viewer, d) {
326
+ if (!card) {
327
+ return null;
328
+ }
329
+ const hidden = d.hidden;
330
+ const redact = (text) => (d.names.length ? Blocks.redactText(text, d.names) : text);
331
+ const who = Object.create(null);
332
+ const know = (playerId) => {
333
+ if (Object.hasOwn(who, playerId)) {
334
+ return true;
335
+ }
336
+ const name = d.usernameOf(playerId);
337
+ if (!name) {
338
+ return false;
339
+ }
340
+ const avatar = d.avatarOf(playerId);
341
+ who[playerId] = Number.isInteger(avatar) ? { name, avatar } : { name };
342
+ return true;
343
+ };
344
+ const view = { v: card.v, mode: card.mode };
345
+ if (card.title !== undefined) {
346
+ view.title = redact(card.title);
347
+ }
348
+ if (card.text !== undefined) {
349
+ view.text = redact(card.text);
350
+ }
351
+ if (card.until !== undefined) {
352
+ view.until = card.until;
353
+ }
354
+ view.faces = [];
355
+ for (const face of card.faces) {
356
+ if (hidden?.has(face.playerId) || !know(face.playerId)) {
357
+ continue;
358
+ }
359
+ const out = { id: face.playerId };
360
+ if (face.note !== undefined) {
361
+ out.note = redact(face.note);
362
+ }
363
+ for (const key of ['off', 'votes', 'mark']) {
364
+ if (face[key] !== undefined) {
365
+ out[key] = face[key];
366
+ }
367
+ }
368
+ view.faces.push(out);
369
+ }
370
+ view.options = card.options.map((option) => ({ id: option.id, text: redact(option.text) }));
371
+ view.lines = [...card.lines];
372
+ view.feed = [];
373
+ for (const row of card.feed) {
374
+ // Only rows between players here now, and none a block hides.
375
+ const absent = !d.usernameOf(row.from) || (row.to !== null && !d.usernameOf(row.to));
376
+ if (absent || hidden?.has(row.from) || (row.to !== null && hidden?.has(row.to))) {
377
+ continue;
378
+ }
379
+ know(row.from);
380
+ if (row.to !== null) {
381
+ know(row.to);
382
+ }
383
+ view.feed.push(row.to === null ? { from: row.from, line: row.line } : { from: row.from, to: row.to, line: row.line });
384
+ }
385
+ if (card.picked) {
386
+ view.picked = card.picked.option !== undefined || view.faces.some((face) => face.id === card.picked.face) ? { ...card.picked } : {};
387
+ }
388
+ if (d.names.length) {
389
+ const words = Object.create(null);
390
+ for (const line of d.lines ?? []) {
391
+ const text = redact(line.text);
392
+ if (text !== line.text) {
393
+ words[line.id] = text;
394
+ }
395
+ }
396
+ if (Object.keys(words).length) {
397
+ view.words = words;
398
+ }
399
+ }
400
+ view.who = who;
401
+ return view;
402
+ }
403
+
404
+ // The view of the card a player has now (theirs, else the shared one): a
405
+ // pure function of the card and of who is connected now. Blocks are the
406
+ // session's pairs among the players here (`hiddenPair` answers nothing about
407
+ // a player who has left), so nothing is remembered between sends.
408
+ function currentView(h, playerId, deps, hidden) {
409
+ const s = h.ballotState;
410
+ const card = s.me.get(playerId) ?? s.shared;
411
+ const hiddenIds = hidden?.get(playerId) ?? null;
412
+ return viewFor(card, playerId, {
413
+ usernameOf: deps.usernameOf,
414
+ avatarOf: deps.avatarOf,
415
+ hidden: hiddenIds,
416
+ names: hiddenIds ? deps.namesOf(hiddenIds) : [],
417
+ lines: h.place.ballot.lines,
418
+ });
419
+ }
420
+
421
+ // Each player's card, to them alone, when it changes (and again to a client
422
+ // that joined afresh). Nothing in a place that has never shown one. Whatever
423
+ // is kept for a connection no longer in the world goes, whether or not it
424
+ // ever said hello (`forget` runs only for players who did).
425
+ export function send(h, deps) {
426
+ const s = h.ballotState;
427
+ if (!s) {
428
+ return;
429
+ }
430
+ const players = h.sim.players;
431
+ for (const map of [s.me, s.sent, s.views, s.rate]) {
432
+ for (const playerId of map.keys()) {
433
+ if (!players.has(playerId)) {
434
+ map.delete(playerId);
435
+ }
436
+ }
437
+ }
438
+ const hidden = deps.blockedPairs();
439
+ for (const playerId of players.keys()) {
440
+ const view = currentView(h, playerId, deps, hidden);
441
+ const json = JSON.stringify(view);
442
+ if ((s.sent.get(playerId) ?? 'null') !== json) {
443
+ s.sent.set(playerId, json);
444
+ if (view) {
445
+ s.views.set(playerId, view);
446
+ } else {
447
+ s.views.delete(playerId);
448
+ }
449
+ h.session?.send(playerId, { k: 'hub-ballot-card', card: view });
450
+ }
451
+ }
452
+ }
453
+
454
+ // Whether the card `view` allows a tap of `{ face, line, option }` (ids or
455
+ // null): the ids are on it and its mode and lock take them.
456
+ function allows(view, face, line, option, lines) {
457
+ if (!view || view.mode === 'reveal') {
458
+ return false;
459
+ }
460
+ if (option !== null) {
461
+ // An option alone, and one this card offers.
462
+ if (face !== null || line !== null || !view.options.some((o) => o.id === option)) {
463
+ return false;
464
+ }
465
+ } else if (face !== null && !view.faces.some((f) => f.id === face && !f.off)) {
466
+ return false;
467
+ }
468
+ if (view.mode === 'vote') {
469
+ // A face or an option, no line, and no lock (`picked`, `{}` included).
470
+ return line === null && (face !== null || option !== null) && !view.picked;
471
+ }
472
+ if (option !== null) {
473
+ return true;
474
+ }
475
+ // Talk: a line this card offers; a line about a face takes one, a line
476
+ // about the speaker takes none.
477
+ const said = line !== null && view.lines.includes(line) ? lines.find((l) => l.id === line) : null;
478
+ return Boolean(said) && (said.about === 'face') === (face !== null);
479
+ }
480
+
481
+ // A tap on the card: `{ k: 'hub-ballot', v, face?, line?, option? }`, ids
482
+ // only. Rule A: `v` is the version last delivered to this player and the
483
+ // version of the card they have now, and the ids are allowed by that card as
484
+ // delivered; then the tap is limited and handed to the deck's `ballot` hook.
485
+ // Anything else is dropped unheard.
486
+ export function message(h, player, data, lines) {
487
+ const playerId = player?.playerId;
488
+ const s = h.ballotState;
489
+ const delivered = s?.views.get(playerId) ?? null;
490
+ if (!delivered || !h.sim?.players.has(playerId) || !h.players.get(playerId)?.arrived) {
491
+ return;
492
+ }
493
+ if (!isObject(data) || Object.keys(data).some((key) => !MESSAGE_KEYS.includes(key))) {
494
+ return;
495
+ }
496
+ const current = s.me.get(playerId) ?? s.shared;
497
+ if (data.v !== delivered.v || current?.v !== delivered.v) {
498
+ return;
499
+ }
500
+ const face = data.face ?? null;
501
+ const line = data.line ?? null;
502
+ const option = data.option ?? null;
503
+ for (const id of [face, line, option]) {
504
+ if (id !== null && typeof id !== 'string') {
505
+ return;
506
+ }
507
+ }
508
+ if (!allows(delivered, face, line, option, lines)) {
509
+ return;
510
+ }
511
+ const now = Date.now();
512
+ const r = s.rate.get(playerId) ?? { tokens: BURST, at: now };
513
+ r.tokens = Math.min(BURST, r.tokens + (now - r.at) / EVERY_MS);
514
+ r.at = now;
515
+ s.rate.set(playerId, r);
516
+ if (r.tokens < 1) {
517
+ return;
518
+ }
519
+ r.tokens -= 1;
520
+ try {
521
+ h.hooks.ballot?.(h.ctx, playerId, { face, line, option, mode: delivered.mode });
522
+ } catch (err) {
523
+ console.warn(`[hub] ballot hook failed: ${err?.message ?? err}`);
524
+ }
525
+ }
526
+
527
+ // A client that joined afresh holds no card: it is sent its card again.
528
+ export function joinedAfresh(h, playerId) {
529
+ h.ballotState.sent.delete(playerId);
530
+ h.ballotState.views.delete(playerId);
531
+ }
532
+
533
+ export function forget(h, playerId) {
534
+ const s = h.ballotState;
535
+ s.me.delete(playerId);
536
+ s.sent.delete(playerId);
537
+ s.views.delete(playerId);
538
+ s.rate.delete(playerId);
539
+ }
@@ -0,0 +1,100 @@
1
+ // Scene-authored ground cells. Geometry stays independent of physics and I/O.
2
+ export const VIEW_RADIUS_M = 10;
3
+
4
+ export function readCells(scene) {
5
+ const source = scene?.cells;
6
+ const bad = (reason) => ({ table: null, reason });
7
+ if (!source || source.version !== 1 || !Array.isArray(source.cells) || !source.cells.length) {
8
+ return bad('must have version 1 and a nonempty cells array');
9
+ }
10
+ const ids = new Set();
11
+ for (const cell of source.cells) {
12
+ if (!cell || typeof cell.id !== 'string' || !cell.id || ids.has(cell.id)) {
13
+ return bad('ids must be unique strings');
14
+ }
15
+ ids.add(cell.id);
16
+ if (
17
+ !Array.isArray(cell.outline) ||
18
+ cell.outline.length < 3 ||
19
+ !cell.outline.every((p) => Array.isArray(p) && p.length === 2 && p.every(Number.isFinite))
20
+ ) {
21
+ return bad('outlines must have at least three finite [x,z] pairs');
22
+ }
23
+ }
24
+ for (const cell of source.cells) {
25
+ if (
26
+ !Array.isArray(cell.sees) ||
27
+ !cell.sees.includes(cell.id) ||
28
+ !cell.sees.every((id) => ids.has(id))
29
+ ) {
30
+ return bad('sees must include its own id and contain only known ids');
31
+ }
32
+ }
33
+ if (
34
+ source.hysteresis !== undefined &&
35
+ (!Number.isFinite(source.hysteresis) || source.hysteresis < 0)
36
+ ) {
37
+ return bad('hysteresis must be finite and nonnegative');
38
+ }
39
+ return {
40
+ table: {
41
+ ...source,
42
+ hysteresis: source.hysteresis ?? 2.5,
43
+ byId: new Map(source.cells.map((c) => [c.id, c])),
44
+ },
45
+ reason: null,
46
+ };
47
+ }
48
+
49
+ function edgeDistance(x, z, a, b) {
50
+ const dx = b[0] - a[0];
51
+ const dz = b[1] - a[1];
52
+ const length = dx * dx + dz * dz;
53
+ const t = length ? Math.max(0, Math.min(1, ((x - a[0]) * dx + (z - a[1]) * dz) / length)) : 0;
54
+ return Math.hypot(x - a[0] - t * dx, z - a[1] - t * dz);
55
+ }
56
+
57
+ function distance(cell, x, z) {
58
+ let inside = false;
59
+ let near = Infinity;
60
+ const points = cell.outline;
61
+ for (let i = 0, j = points.length - 1; i < points.length; j = i++) {
62
+ const a = points[j];
63
+ const b = points[i];
64
+ near = Math.min(near, edgeDistance(x, z, a, b));
65
+ if (a[1] > z !== b[1] > z && x < ((b[0] - a[0]) * (z - a[1])) / (b[1] - a[1]) + a[0]) {
66
+ inside = !inside;
67
+ }
68
+ }
69
+ return inside ? 0 : near;
70
+ }
71
+
72
+ export function cellAt(table, x, z, previous = null) {
73
+ if (!Number.isFinite(x) || !Number.isFinite(z)) {
74
+ return null;
75
+ }
76
+ const old = table.byId.get(previous);
77
+ if (old && distance(old, x, z) <= table.hysteresis) {
78
+ return previous;
79
+ }
80
+ return table.cells.find((cell) => distance(cell, x, z) <= 1e-9)?.id ?? null;
81
+ }
82
+
83
+ export function cellsNear(table, x, z, radius) {
84
+ return new Set(
85
+ table.cells.filter((cell) => distance(cell, x, z) <= radius).map((cell) => cell.id),
86
+ );
87
+ }
88
+
89
+ export function visibleCells(table, x, z) {
90
+ if (cellAt(table, x, z) === null) {
91
+ return null;
92
+ }
93
+ const visible = new Set();
94
+ for (const id of cellsNear(table, x, z, VIEW_RADIUS_M)) {
95
+ for (const seen of table.byId.get(id).sees) {
96
+ visible.add(seen);
97
+ }
98
+ }
99
+ return visible;
100
+ }