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,228 @@
1
+ // A cap on how many different character/colour models this client draws at once.
2
+ // Colour variants cost a model too: lose colour before using a stand-in.
3
+ //
4
+ // Every character on screen is a model of its own to the renderer, and a
5
+ // low-end phone pays for each one more (a 12-player scene of eleven
6
+ // different characters ran ~3.5 fps under eleven copies of one). Beyond
7
+ // `limit()` different characters, the farthest players are drawn as their
8
+ // stand-in, one of the first four (`avatar % 4`, as `hubAv` already names
9
+ // them for a client from before v1.51). Only how this viewer draws them: the
10
+ // wire, what is saved and the picker are unchanged, the local player always
11
+ // draws their own, and nearer players win. A player within `NEAR_M` metres is
12
+ // never a stand-in, even past the limit: one who walks up to you is drawn as
13
+ // themselves at once, hold or not, and stays so until `NEAR_M + NEARER_M`.
14
+ //
15
+ // The stand-ins count toward the limit too (`choose`). Decided a few times a
16
+ // second (`EVERY_MS`), with two kinds of hysteresis so nobody flickers between
17
+ // models: a player drawn as their own counts as `NEARER_M` metres nearer than
18
+ // they are, and a player who changed is held for `HOLD_MS` -- a stand-in stays
19
+ // one, and one drawn as their own is asked about before anyone else, but is
20
+ // still made a stand-in rather than pass the limit. A player who changes is respawned through the
21
+ // same path as an avatar change (hookLayer.js `redraw`), fade guard included.
22
+ //
23
+ // By default the limit is every character there is (`DISTINCT_DEFAULT`, all
24
+ // sixteen), so nobody is a stand-in: on the TECNO a close crowd of twelve
25
+ // different characters with shared materials held ~34 fps (2026-10-05), and
26
+ // the way back to the first four's ~36 is the engine's geometry per model,
27
+ // not this. `?hubAvDistinct=N` sets a lower limit: absent, 0 or not a number
28
+ // is `DISTINCT_DEFAULT`.
29
+
30
+ import * as Avatar from './avatar.js';
31
+ import * as Colors from './avatarColors.js';
32
+ import { hub } from './state.js';
33
+
34
+ export const DISTINCT_DEFAULT = Avatar.AVATARS;
35
+ export const EVERY_MS = 250;
36
+ export const NEARER_M = 3;
37
+ export const HOLD_MS = 2000;
38
+ export const NEAR_M = 8;
39
+ const PARAM = 'hubAvDistinct';
40
+ // Where nobody knows a player's body: as far as anyone gets.
41
+ const FAR = 1e9;
42
+
43
+ // Tests only: a location in place of the page's.
44
+ let capEnv = null;
45
+ let cached = null;
46
+ export function setCapEnv(env) {
47
+ capEnv = env ?? null;
48
+ cached = null;
49
+ }
50
+
51
+ // How many different characters are drawn at most, the local player's
52
+ // included. Read once: the page's query does not change under it.
53
+ export function limit() {
54
+ if (cached === null) {
55
+ let value = null;
56
+ try {
57
+ const location = capEnv ? capEnv.location : globalThis.location;
58
+ value = new URLSearchParams(location?.search ?? '').get(PARAM);
59
+ } catch {
60
+ value = null;
61
+ }
62
+ const n = Number.parseInt(value ?? '', 10);
63
+ cached = Number.isInteger(n) && n > 0 ? n : DISTINCT_DEFAULT;
64
+ }
65
+ return cached;
66
+ }
67
+
68
+ // The avatar a player is drawn as here: the one they wear (`Avatar.known`),
69
+ // or its stand-in while the cap has them as one. Null for nobody.
70
+ export function shown(playerId) {
71
+ if (!hub.avatars.has(playerId)) {
72
+ return null;
73
+ }
74
+ const index = Avatar.known(hub.avatars.get(playerId));
75
+ if (index !== null && index >= Avatar.DEFAULT_AVATARS && hub.avatarStandIns.has(playerId)) {
76
+ return index % Avatar.DEFAULT_AVATARS;
77
+ }
78
+ return index;
79
+ }
80
+
81
+ export function shownColor(playerId) {
82
+ if (hub.avatarStandIns.has(playerId) || hub.avatarColorStandIns.has(playerId) || Avatar.style() !== 'toon') {
83
+ return 0;
84
+ }
85
+ return Colors.known(hub.avatars.get(playerId), hub.avatarColors.get(playerId));
86
+ }
87
+
88
+ const pair = (index, color = 0) => color ? `${index}:${color}` : index;
89
+
90
+ // Who is drawn as a stand-in: the set of player ids. `own` is the local
91
+ // player's avatar, always drawn; `players` is `[{ playerId, index, distance }]`
92
+ // for everyone else, `index` what they wear (`Avatar.known`), `distance` from
93
+ // the local player in metres (null where their body is not known). `standIns`
94
+ // is who is a stand-in now and `changedAt` when each last changed.
95
+ //
96
+ // The stand-ins are models too, so they are counted before anyone is given
97
+ // their own: a player is drawn as their own character only if, with it, the
98
+ // local player's, those already given theirs, and the stand-ins (or first
99
+ // four) that every player still to be decided would need at the least, it
100
+ // is still at most `most` different models. So the total never passes the
101
+ // limit unless the local player's and the first four worn here alone do,
102
+ // or the players within `NEAR_M` (`near`), always drawn as their own, do.
103
+ export function choose({ own, ownColor = 0, players, limit: most, standIns = new Set(), colorStandIns = new Set(), changedAt = new Map(), now = 0 }) {
104
+ const drawn = new Set(Number.isInteger(own) ? [pair(own, Colors.known(own, ownColor))] : []);
105
+ const out = new Set();
106
+ out.colorStandIns = new Set();
107
+ const held = (player) => now - (changedAt.get(player.playerId) ?? -Infinity) < HOLD_MS;
108
+ const ranked = players
109
+ .filter((player) => Number.isInteger(player.index))
110
+ .map((player) => {
111
+ const distance = Number.isFinite(player.distance) ? player.distance : FAR;
112
+ return { ...player, color: Colors.known(player.index, player.color), rank: standIns.has(player.playerId) || colorStandIns.has(player.playerId) ? distance : distance - NEARER_M };
113
+ })
114
+ .sort((a, b) => a.rank - b.rank || (a.playerId < b.playerId ? -1 : a.playerId > b.playerId ? 1 : 0));
115
+ // Within NEAR_M, or within NEAR_M + NEARER_M of one drawn as their own.
116
+ const near = (player) => Number.isFinite(player.distance) && player.distance < NEAR_M + (standIns.has(player.playerId) || colorStandIns.has(player.playerId) ? 0 : NEARER_M);
117
+ const fallback = (index) => index % Avatar.DEFAULT_AVATARS;
118
+ const standIn = (player) => {
119
+ out.add(player.playerId);
120
+ drawn.add(fallback(player.index));
121
+ };
122
+ // Those near are drawn as their own, before anyone. A stand-in who changed
123
+ // less than HOLD_MS ago stays one; one drawn as their own who did is asked
124
+ // about first. Then nearest first.
125
+ const open = [];
126
+ for (const player of ranked) {
127
+ if (near(player)) {
128
+ drawn.add(pair(player.index, player.color));
129
+ } else if (player.index >= Avatar.DEFAULT_AVATARS && standIns.has(player.playerId) && held(player)) {
130
+ standIn(player);
131
+ } else {
132
+ open.push(player);
133
+ }
134
+ }
135
+ open.sort((a, b) => Number(held(b)) - Number(held(a)));
136
+ // The least each of `rest` needs drawn, given `models`.
137
+ const fits = (models, rest) => {
138
+ const need = new Set(models);
139
+ for (const player of rest) {
140
+ if (!need.has(pair(player.index, player.color)) && !need.has(player.index)) {
141
+ need.add(fallback(player.index));
142
+ }
143
+ }
144
+ return need.size <= most;
145
+ };
146
+ open.forEach((player, at) => {
147
+ const model = pair(player.index, player.color);
148
+ const keepPlain = player.color > 0 && colorStandIns.has(player.playerId) && held(player);
149
+ if (!keepPlain && ((!player.color && player.index < Avatar.DEFAULT_AVATARS) || drawn.has(model))) {
150
+ drawn.add(model);
151
+ return;
152
+ }
153
+ if (!keepPlain && fits(new Set(drawn).add(model), open.slice(at + 1))) {
154
+ drawn.add(model);
155
+ } else if (player.color > 0 && (player.index < Avatar.DEFAULT_AVATARS || drawn.has(player.index) || fits(new Set(drawn).add(player.index), open.slice(at + 1)))) {
156
+ // Colour costs a model too: keep the character before using its stand-in.
157
+ out.colorStandIns.add(player.playerId);
158
+ drawn.add(player.index);
159
+ } else {
160
+ standIn(player);
161
+ }
162
+ });
163
+ return out;
164
+ }
165
+
166
+ // The cap's own clock and memory, one per page.
167
+ const cap = { at: -Infinity, changedAt: new Map() };
168
+
169
+ // Decides again, at most every `EVERY_MS`, who is a stand-in, and returns the
170
+ // players whose model changed: the hook layer respawns them. Needs the scene's
171
+ // local player and the multiplayer system's bodies; does nothing without.
172
+ export function frame(scene, system, now = performance.now()) {
173
+ if (now - cap.at < EVERY_MS || typeof scene?.actorWith !== 'function' || !system?.avatars?.actors) {
174
+ return [];
175
+ }
176
+ cap.at = now;
177
+ const self = system.net?.selfId?.() ?? null;
178
+ const here = scene.actorWith('Player')?.components?.Transform ?? null;
179
+ const players = [];
180
+ const unplaced = new Set();
181
+ for (const playerId of hub.avatars.keys()) {
182
+ if (playerId === self) {
183
+ continue;
184
+ }
185
+ const actorId = system.avatars.actors.get(playerId);
186
+ const there = actorId ? (scene.getActor?.(actorId)?.components?.Transform ?? null) : null;
187
+ const distance = here && there ? Math.hypot(there.x - here.x, there.z - here.z) : null;
188
+ if (distance === null) {
189
+ unplaced.add(playerId);
190
+ }
191
+ players.push({ playerId, index: Avatar.known(hub.avatars.get(playerId)), color: Avatar.style() === 'toon' ? Colors.known(hub.avatars.get(playerId), hub.avatarColors.get(playerId)) : 0, distance });
192
+ }
193
+ const next = choose({ own: Avatar.current(scene), ownColor: Avatar.style() === 'toon' ? Avatar.currentColor(scene) : 0, players, limit: limit(), standIns: hub.avatarStandIns, colorStandIns: hub.avatarColorStandIns, changedAt: cap.changedAt, now });
194
+ const changed = [];
195
+ for (const playerId of new Set([...next, ...hub.avatarStandIns, ...next.colorStandIns, ...hub.avatarColorStandIns])) {
196
+ const colorChanged = next.colorStandIns.has(playerId) !== hub.avatarColorStandIns.has(playerId);
197
+ if (next.has(playerId) !== hub.avatarStandIns.has(playerId) || colorChanged) {
198
+ // One whose body is not here yet (joining) is not held: decided again
199
+ // as soon as it is, while it is still growing in.
200
+ if (unplaced.has(playerId)) {
201
+ cap.changedAt.delete(playerId);
202
+ } else {
203
+ cap.changedAt.set(playerId, now);
204
+ }
205
+ // A stand-in for a player who now wears one of the first four is no change.
206
+ const index = Avatar.known(hub.avatars.get(playerId));
207
+ if (index !== null && (index >= Avatar.DEFAULT_AVATARS || colorChanged || Colors.known(index, hub.avatarColors.get(playerId)) > 0)) {
208
+ changed.push(playerId);
209
+ }
210
+ }
211
+ }
212
+ for (const playerId of [...cap.changedAt.keys()]) {
213
+ if (!hub.avatars.has(playerId)) {
214
+ cap.changedAt.delete(playerId);
215
+ }
216
+ }
217
+ hub.avatarStandIns = next;
218
+ hub.avatarColorStandIns = next.colorStandIns;
219
+ return changed;
220
+ }
221
+
222
+ // Tests only.
223
+ export function resetCap() {
224
+ cap.at = -Infinity;
225
+ cap.changedAt.clear();
226
+ hub.avatarStandIns = new Set();
227
+ hub.avatarColorStandIns = new Set();
228
+ }
@@ -0,0 +1,70 @@
1
+ // Colour choices for the toon avatars (v1.8x): `color` 0 is the model as drawn;
2
+ // 1-5 recolour the parts each character names. `main` parts take the first
3
+ // colour, `shade` parts the second (a darker or lighter partner of the same
4
+ // hue, where the model has one). Face parts (eyes, cheeks, tongue, nose), the
5
+ // outline and anything not named keep the model's colours, so every character
6
+ // stays the one it is. Other sets (blocky, bean, pixel) draw colour 0.
7
+
8
+ export const COLORS = 6;
9
+
10
+ export const AVATAR_COLORS = [
11
+ // 0 Castle: the body stays dark so its white eye reads.
12
+ { main: ['torso', 'shoeL', 'shoeR'], colors: [['#5b2a6e'], ['#1f3d6b'], ['#23523a'], ['#6b2330'], ['#5a4a3a']] },
13
+ // 1 Petal: the petals and shoes.
14
+ { main: ['petals', 'shoeL', 'shoeR'], colors: [['#ffc93c'], ['#a77bff'], ['#4fb3ff'], ['#ff6b4a'], ['#f4f1ea']] },
15
+ // 2 Beat: body and crest, and the lighter head.
16
+ { main: ['torso', 'crest0', 'crest1', 'crest2'], shade: ['head'], colors: [['#d8343f', '#ff7a7a'], ['#2a9a4a', '#6fd889'], ['#6a35c8', '#b48cff'], ['#3a3644', '#7c788a'], ['#e0a400', '#ffd95a']] },
17
+ // 3 Star: the star, and the body under it.
18
+ { main: ['head'], shade: ['torso', 'badge'], colors: [['#dfe6ee', '#8f9db0'], ['#ff9ccf', '#e8508f'], ['#8ff0c8', '#2fbf8a'], ['#8fd4ff', '#3a8ef0'], ['#ff7a5a', '#c8402a']] },
19
+ // 4 Biscuit: the coat, and the darker ears, patch and shoes.
20
+ { main: ['torso', 'head', 'tailA'], shade: ['ear', 'eyePatch', 'shoeL', 'shoeR'], colors: [['#f2e6d0', '#8a6a4a'], ['#e8b04a', '#a8612a'], ['#9aa0aa', '#4a4f5a'], ['#d97a3a', '#7a3a1a'], ['#f4c9a0', '#c8743a']] },
21
+ // 5 Mochi: the coat.
22
+ { main: ['torso', 'head', 'ear', 'tailA'], colors: [['#f08a2a'], ['#3a3644'], ['#f4efe6'], ['#c9a27a'], ['#9a8ac8']] },
23
+ // 6 Hop: the coat.
24
+ { main: ['torso', 'head', 'ear', 'tailA'], colors: [['#a8794f'], ['#9aa0aa'], ['#3a3644'], ['#ffd0e0'], ['#f2d48a']] },
25
+ // 7 Pip: the back and wings.
26
+ { main: ['handL', 'handR', 'torso', 'head', 'tailA'], colors: [['#3a3644'], ['#2f7a8a'], ['#6a2a4a'], ['#4a6a3a'], ['#7a8090']] },
27
+ // 8 Ribbit: the skin.
28
+ { main: ['shoeL', 'shoeR', 'torso', 'head', 'bump'], colors: [['#3aa8e0'], ['#ff8a2a'], ['#e23b4a'], ['#a77bff'], ['#f2c230']] },
29
+ // 9 Ember: the scales, and the darker wings.
30
+ { main: ['shoeL', 'shoeR', 'torso', 'head', 'tailA'], shade: ['wing'], colors: [['#d8343f', '#a81e2a'], ['#2fa84f', '#1e7a38'], ['#2b62e3', '#1c3fa8'], ['#3a3644', '#211d29'], ['#e0a400', '#b07a00']] },
31
+ // 10 Bolt: the casing.
32
+ { main: ['torso', 'head'], colors: [['#e8746a'], ['#7ab8e8'], ['#8ad08a'], ['#c9a8ff'], ['#f4f1ea']] },
33
+ // 11 Gloop: the head, and the darker body and spots.
34
+ { main: ['head'], shade: ['torso', 'stalk', 'spotA', 'spotB', 'spotC'], colors: [['#a77bff', '#6a35c8'], ['#ff8a2a', '#c25a10'], ['#8fe04a', '#4f9a1f'], ['#4fb3ff', '#1f6ad0'], ['#ffd21f', '#c89a00']] },
35
+ // 12 Cap: the cap (never red: a game has a red mushroom).
36
+ { main: ['cap'], colors: [['#2b9ae0'], ['#f2a83a'], ['#4fbf3f'], ['#f2d23a'], ['#8a5a3a']] },
37
+ // 13 Toast: the shirt; the bread stays bread.
38
+ { main: ['torso'], colors: [['#2b62e3'], ['#2fa84f'], ['#f2c230'], ['#7a3fd0'], ['#3a3644']] },
39
+ // 14 Scoop: the scoop and its foot (vanilla, mint, ube, mango, blueberry).
40
+ { main: ['head', 'foot'], colors: [['#f4ead0'], ['#9ae0b0'], ['#c9a8ff'], ['#ffd27a'], ['#7a9aff']] },
41
+ // 15 Zest: the peel and shoes (lemon, lime, grapefruit, plum, blood orange).
42
+ { main: ['head', 'shoeL', 'shoeR'], colors: [['#ffd21f'], ['#9ad84a'], ['#ff7aa0'], ['#a85ad0'], ['#e04a2a']] },
43
+ ];
44
+
45
+ // The colour a stored or sent value means for this avatar: 0 for anything
46
+ // this kit does not offer (a later kit's colour, another set, garbage).
47
+ export function known(index, color) {
48
+ return Number.isInteger(color) && color > 0 && color < COLORS && AVATAR_COLORS[index]?.colors[color - 1] ? color : 0;
49
+ }
50
+
51
+ // The model text of `index` in `color`: the model's own text for 0, else its
52
+ // named parts recoloured. Parsed and written back, never string-replaced, so
53
+ // a colour shared with a face part stays on the face.
54
+ export function recolored(text, index, color) {
55
+ const c = known(index, color);
56
+ if (c === 0) {
57
+ return text;
58
+ }
59
+ const { main, shade = [], colors } = AVATAR_COLORS[index];
60
+ const [first, second = first] = colors[c - 1];
61
+ const model = JSON.parse(text);
62
+ for (const part of model.parts ?? []) {
63
+ if (main.includes(part.id)) {
64
+ part.color = first;
65
+ } else if (shade.includes(part.id)) {
66
+ part.color = second;
67
+ }
68
+ }
69
+ return JSON.stringify(model);
70
+ }
@@ -0,0 +1,21 @@
1
+ // Generated by scripts/gen-avatars.mjs; do not edit. Each avatar index: the
2
+ // character's name, and its face for the picker (`chrome.js`), the inside of a
3
+ // 24 x 24 SVG.
4
+ export const AVATAR_FACES = [
5
+ ['Castle', '<path d="M5 22V10a7 7 0 0 1 14 0v12z" fill="#4a4556"/><path d="M7.2 19.5V10a4.8 4.8 0 0 1 1.6-3.6" fill="none" stroke="#ffffff"/><path d="M7.6 13.4q4.4-4.6 8.8 0q-4.4 4.6-8.8 0z" fill="#ffffff"/><circle cx="12" cy="13.4" r="1.8" fill="#241a2e" stroke="none"/>'],
6
+ ['Petal', '<circle cx="12" cy="5.4" r="4" fill="#ff5fa2"/><circle cx="17.716" cy="8.7" r="4" fill="#ff5fa2"/><circle cx="17.716" cy="15.3" r="4" fill="#ff5fa2"/><circle cx="12" cy="18.6" r="4" fill="#ff5fa2"/><circle cx="6.284" cy="15.3" r="4" fill="#ff5fa2"/><circle cx="6.284" cy="8.7" r="4" fill="#ff5fa2"/><circle cx="12" cy="12" r="6.4" fill="#fff1cf"/><g fill="#241a2e" stroke="none"><circle cx="9.4" cy="12" r="1.5"/><circle cx="14.6" cy="12" r="1.5"/></g>'],
7
+ ['Beat', '<path d="M4 13a8 8 0 0 1 16 0" fill="none" stroke-width="2.2"/><circle cx="12" cy="14" r="7.5" fill="#38b6ff"/><ellipse cx="3.6" cy="14.5" rx="2" ry="3.4" fill="#ff6a2b"/><ellipse cx="20.4" cy="14.5" rx="2" ry="3.4" fill="#ff6a2b"/><g fill="#241a2e" stroke="none"><circle cx="9.4" cy="13.5" r="1.5"/><circle cx="14.6" cy="13.5" r="1.5"/></g>'],
8
+ ['Star', '<path d="M12 1.5l3 6.6 7.2.8-5.4 4.9 1.5 7.1L12 17.3l-6.3 3.6 1.5-7.1L1.8 8.9l7.2-.8z" fill="#ffd21f"/><g fill="#241a2e" stroke="none"><circle cx="9.8" cy="11.5" r="1.3"/><circle cx="14.2" cy="11.5" r="1.3"/></g>'],
9
+ ['Biscuit', '<circle cx="12" cy="13" r="8" fill="#c79a68"/><ellipse cx="4.3" cy="13.5" rx="2.4" ry="5.4" fill="#6b3d20"/><ellipse cx="19.7" cy="13.5" rx="2.4" ry="5.4" fill="#6b3d20"/><circle cx="14.8" cy="11.6" r="3.1" fill="#6b3d20" stroke="none"/><g fill="#241a2e" stroke="none"><circle cx="9.4" cy="11.6" r="1.5"/><circle cx="14.6" cy="11.6" r="1.5"/></g><ellipse cx="12" cy="15.6" rx="1.9" ry="1.3" fill="#241a2e"/>'],
10
+ ['Mochi', '<path d="M4.6 11 5.4 3l5.4 3.6zM19.4 11 18.6 3l-5.4 3.6z" fill="#686c7d"/><circle cx="12" cy="14" r="8" fill="#686c7d"/><path d="M1.5 14.5h4M18.5 14.5h4" fill="none"/><g fill="#241a2e" stroke="none"><circle cx="9.4" cy="13" r="1.5"/><circle cx="14.6" cy="13" r="1.5"/></g><ellipse cx="12" cy="16.2" rx="1.3" ry="0.9" fill="#ff7fae"/>'],
11
+ ['Hop', '<ellipse cx="8.6" cy="6.4" rx="2.2" ry="5.4" fill="#fbf6ee"/><ellipse cx="15.4" cy="6.4" rx="2.2" ry="5.4" fill="#fbf6ee"/><path d="M8.6 3.5v5M15.4 3.5v5" fill="none" stroke="#ff8fb3" stroke-width="1.5"/><circle cx="12" cy="15.5" r="7" fill="#fbf6ee"/><g fill="#241a2e" stroke="none"><circle cx="9.4" cy="14.5" r="1.5"/><circle cx="14.6" cy="14.5" r="1.5"/></g><ellipse cx="12" cy="17.4" rx="1.2" ry="0.9" fill="#ff8fb3"/>'],
12
+ ['Pip', '<circle cx="12" cy="12.5" r="9" fill="#27356e"/><ellipse cx="12" cy="14" rx="6.4" ry="5.8" fill="#ffffff"/><g fill="#241a2e" stroke="none"><circle cx="9.4" cy="12.6" r="1.5"/><circle cx="14.6" cy="12.6" r="1.5"/></g><path d="M10.3 15h3.4L12 17.8z" fill="#ff9326"/>'],
13
+ ['Ribbit', '<circle cx="6.8" cy="8" r="3.6" fill="#4fbf3f"/><circle cx="17.2" cy="8" r="3.6" fill="#4fbf3f"/><ellipse cx="12" cy="14.5" rx="10" ry="7" fill="#4fbf3f"/><g fill="#241a2e" stroke="none"><circle cx="6.8" cy="8" r="1.6"/><circle cx="17.2" cy="8" r="1.6"/></g><path d="M7 15.5q5 4 10 0" fill="none"/>'],
14
+ ['Ember', '<path d="M5.2 9.5 3.6 2.6l5.6 3.6zM18.8 9.5l1.6-6.9-5.6 3.6z" fill="#ffe9a8"/><path d="M12 1.8l2.2 4h-4.4z" fill="#ff9a2e"/><circle cx="12" cy="13.5" r="8" fill="#7a3fd0"/><g fill="#241a2e" stroke="none"><circle cx="9.4" cy="11.4" r="1.5"/><circle cx="14.6" cy="11.4" r="1.5"/></g><ellipse cx="12" cy="17" rx="4" ry="2.9" fill="#c9adf8"/><g fill="#241a2e" stroke="none"><circle cx="10.7" cy="16" r="0.6"/><circle cx="13.3" cy="16" r="0.6"/></g><path d="M10.6 18.2q1.4 1 2.8 0" fill="none" stroke-width="1"/>'],
15
+ ['Bolt', '<path d="M20.5 10V4" fill="none"/><circle cx="20.5" cy="3.4" r="1.7" fill="#ff4d4d"/><rect x="3.5" y="6.5" width="16" height="14" rx="2.6" fill="#b5c1cf"/><rect x="5.6" y="9.4" width="11.8" height="5" rx="1.2" fill="#241a2e"/><g fill="#35e0ff" stroke="none"><circle cx="9" cy="11.9" r="1.4"/><circle cx="14" cy="11.9" r="1.4"/></g><path d="M9 17.6h5" fill="none"/>'],
16
+ ['Gloop', '<path d="M13 6l3-3.4" fill="none"/><circle cx="16.6" cy="2.4" r="1.6" fill="#ff4fa3"/><path d="M12 4.5c5 1.6 8 6.5 8 10.5a8 7 0 0 1-16 0c0-4 3-8.9 8-10.5z" fill="#14bfae"/><circle cx="12" cy="11" r="1.8" fill="#241a2e" stroke="none"/><g fill="#241a2e" stroke="none"><circle cx="8.4" cy="15.6" r="1.5"/><circle cx="15.6" cy="15.6" r="1.5"/></g>'],
17
+ ['Cap', '<ellipse cx="12" cy="16.5" rx="5.4" ry="6" fill="#ffdfa6"/><path d="M2.6 10.6a9.4 8.4 0 0 1 18.8 0q-9.4 2.6-18.8 0z" fill="#a8479a"/><g fill="#fff3d6" stroke="none"><circle cx="9.4" cy="6.2" r="2.3"/><circle cx="15.6" cy="7.6" r="1.2"/><circle cx="5.4" cy="9.4" r=".9"/></g><g fill="#241a2e" stroke="none"><circle cx="9.8" cy="16" r="1.3"/><circle cx="14.2" cy="16" r="1.3"/></g><path d="M10.4 19.2q1.6 1.3 3.2 0" fill="none" stroke-width="1"/>'],
18
+ ['Toast', '<path d="M5 22V11.5Q2 9.5 4 6t8-3t8 3t-1 5.5V22z" fill="#a8612a"/><path d="M7.2 19.8V10.6Q5 9 6.6 7t5.4-1.8T17.4 7t-.6 3.6v9.2z" fill="#fff2c4" stroke="none"/><rect x="12.6" y="7.3" width="4" height="3.3" rx=".6" fill="#ffe24a" stroke-width=".8"/><g fill="#241a2e" stroke="none"><circle cx="9.7" cy="14.4" r="1.3"/><circle cx="14.3" cy="14.4" r="1.3"/></g>'],
19
+ ['Scoop', '<path d="M7 15.5h10l-5 7.5z" fill="#e3a550"/><circle cx="12" cy="10.5" r="7.5" fill="#ff93a6"/><path d="M4.6 9.4a7.5 7.5 0 0 1 14.8 0q-.2 1.5-1.3 1.5t-1.2-1.4q-.1 2.6-1.3 2.6t-1.2-2.6q-.1 1.4-1.2 1.4t-1.2-1.4q-.1 2.6-1.3 2.6T8.3 9.5q-.1 1.4-1.2 1.4T4.6 9.4z" fill="#6a3a1f"/><path d="M9 6.3l2.2-1.2" fill="none" stroke="#fff27a" stroke-width="1.2"/><path d="M13.2 5.2l1.6 1.8" fill="none" stroke="#7ae0ff" stroke-width="1.2"/><g fill="#241a2e" stroke="none"><circle cx="9.6" cy="13.6" r="1.2"/><circle cx="14.4" cy="13.6" r="1.2"/></g>'],
20
+ ['Zest', '<circle cx="12" cy="14" r="8.5" fill="#ff8a14"/><path d="M12 5.8q3.5-6 8.5-3.2q-3 5.4-8.5 3.2z" fill="#4fc04a"/><g fill="#241a2e" stroke="none"><circle cx="9.4" cy="13.2" r="1.5"/><circle cx="14.6" cy="13.2" r="1.5"/></g><path d="M9.2 16.4q2.8 2.4 5.6 0" fill="none"/>'],
21
+ ];
@@ -0,0 +1,251 @@
1
+ // Players drawn from different avatar models share their materials.
2
+ //
3
+ // The engine builds each model file once and every actor placing it shares
4
+ // that build's materials (physics-3d `optimize.js` takeModel/copyModel). Two
5
+ // different characters never share one: each build has its own eight (six
6
+ // batches and two outline hulls), although their values are the same -- the
7
+ // colours are in the vertices, every batch is white with roughness and
8
+ // metalness per vertex, and the outline is the same ink. three.js sorts the
9
+ // opaque list by material id and refreshes a material's uniforms, and on a
10
+ // change of shader program also the camera's and the lights', at every change
11
+ // of material id; so 11 copies of one character draw as 8 runs, and 11
12
+ // different characters as about 88, with a program switch at each change of
13
+ // batch kind inside every character. A low-end phone pays for each.
14
+ //
15
+ // Here each avatar's meshes are pointed at one material per look: a copy the
16
+ // kit owns of the first material met with that look, made once and never
17
+ // disposed (no model's release can take it from the others). A look is
18
+ // everything that decides how a material draws: its serialised fields, its
19
+ // defines, its shader's program key, and the values of the uniforms the
20
+ // engine's look adds (rim, occlusion, outline width). Two materials with the
21
+ // same look draw the same pixels, so nothing changes on screen; only the
22
+ // number of distinct materials does. A material with a texture, one not yet
23
+ // visible (its outline still baking, physics-3d `look.js` "Baking later"),
24
+ // or one that cannot be read, is left alone, and its avatar tried again.
25
+ //
26
+ // Sharing alone puts the copies of a character apart: three.js orders the
27
+ // draws of one material by depth, so 11 copies of one character, which bound
28
+ // each batch's geometry once as 8 runs, would bind a geometry at nearly every
29
+ // draw. So the renderer's opaque order is three's own (`painterSortStable`),
30
+ // but within the draws of one of the kit's shared materials the geometry goes
31
+ // before the depth: those draws stay together as before, and within them
32
+ // those of one geometry (`opaqueOrder`). Every other draw in the scene keeps
33
+ // three's exact order.
34
+ //
35
+ // Counted over the models built as a game builds them (local player Castle
36
+ // and 11 others; programs / material changes / geometry binds per frame):
37
+ // eleven different new characters 45 / 96 / 89 as before, 6 / 9 / 95 with
38
+ // this; the first four 16 / 32 / 31 before, 6 / 7 / 31 with this; 11 copies
39
+ // of one new character 7 / 16 / 15 before, 5 / 5 / 14 with this. A geometry
40
+ // per character and batch is still bound once each: that is the engine's.
41
+ //
42
+ // The order is off by default: on a low-end phone (TECNO, 12 players of eleven
43
+ // different characters) the shared materials alone won back ~1.6 fps of the
44
+ // gap to copies of one, and the order nothing more. `?hubAvShare=sort` turns
45
+ // it on, `?hubAvShare=0` turns both off, for measuring each.
46
+
47
+ const SHARE_PARAM = 'hubAvShare';
48
+
49
+ // Written each frame by the outline's own `onBeforeRender`: not part of a look.
50
+ const PER_FRAME_UNIFORMS = new Set(['lookViewportHeight']);
51
+
52
+ const shared = new Map();
53
+ // The materials the kit made: only their draws are grouped by geometry.
54
+ const ours = new WeakSet();
55
+ const done = new WeakSet();
56
+
57
+ // Tests only: a location in place of the page's.
58
+ let shareEnv = null;
59
+ let mode = null;
60
+ export function setShareEnv(env) {
61
+ shareEnv = env ?? null;
62
+ mode = null;
63
+ }
64
+
65
+ // `"off"`, `"nosort"` (the default) or `"on"` (`sort`), read once: the page's query does not change
66
+ // under it.
67
+ export function shareMode() {
68
+ if (mode === null) {
69
+ let value = null;
70
+ try {
71
+ const location = shareEnv ? shareEnv.location : globalThis.location;
72
+ value = new URLSearchParams(location?.search ?? '').get(SHARE_PARAM);
73
+ } catch {
74
+ value = null;
75
+ }
76
+ mode = value === '0' ? 'off' : value === 'sort' ? 'on' : 'nosort';
77
+ }
78
+ return mode;
79
+ }
80
+
81
+ export function enabled() {
82
+ return shareMode() !== 'off';
83
+ }
84
+
85
+ // What the engine's `onBeforeCompile` does, run over a shader that is thrown
86
+ // away: the uniforms it adds, with their values, and what it makes of
87
+ // placeholder code with the chunks it rewrites. It only assigns its own
88
+ // uniform objects and rewrites the strings it is given.
89
+ const VERTEX = '#include <begin_vertex>\n#include <project_vertex>';
90
+ const FRAGMENT = '#include <color_fragment>\n#include <roughnessmap_fragment>\n#include <metalnessmap_fragment>\n#include <opaque_fragment>';
91
+ function compiled(material) {
92
+ const shader = { uniforms: {}, vertexShader: VERTEX, fragmentShader: FRAGMENT, defines: {} };
93
+ material.onBeforeCompile(shader, null);
94
+ const uniforms = Object.entries(shader.uniforms)
95
+ .filter(([name]) => !PER_FRAME_UNIFORMS.has(name))
96
+ .map(([name, uniform]) => [name, uniform?.value])
97
+ .sort(([a], [b]) => (a < b ? -1 : 1));
98
+ return [uniforms, shader.vertexShader, shader.fragmentShader];
99
+ }
100
+
101
+ // A material's look as a string, or null for one that is not shared.
102
+ export function lookOf(material) {
103
+ if (!material || Array.isArray(material) || !material.isMaterial || material.visible !== true) {
104
+ return null;
105
+ }
106
+ for (const value of Object.values(material)) {
107
+ if (value && value.isTexture) {
108
+ return null;
109
+ }
110
+ }
111
+ try {
112
+ const json = material.toJSON();
113
+ delete json.uuid;
114
+ delete json.name;
115
+ delete json.userData;
116
+ delete json.metadata;
117
+ return JSON.stringify([
118
+ json,
119
+ material.defines ?? null,
120
+ material.customProgramCacheKey(),
121
+ String(material.onBeforeRender),
122
+ material.constructor.name,
123
+ compiled(material),
124
+ ]);
125
+ } catch {
126
+ return null;
127
+ }
128
+ }
129
+
130
+ // The kit's own copy: the fields as `copy` takes them, and what the engine
131
+ // put on the instance, which `copy` does not carry. Not `material.clone()`,
132
+ // which a material still waiting for its bake has replaced.
133
+ function ownCopy(material) {
134
+ const made = new material.constructor().copy(material);
135
+ made.defines = material.defines ? { ...material.defines } : material.defines;
136
+ made.onBeforeCompile = material.onBeforeCompile;
137
+ made.customProgramCacheKey = material.customProgramCacheKey;
138
+ made.onBeforeRender = material.onBeforeRender;
139
+ made.name = 'hub-avatar-shared';
140
+ ours.add(made);
141
+ return made;
142
+ }
143
+
144
+ // The shared material for `material`'s look, or null.
145
+ export function sharedFor(material) {
146
+ if (ours.has(material)) {
147
+ return material;
148
+ }
149
+ const look = lookOf(material);
150
+ if (look === null) {
151
+ return null;
152
+ }
153
+ let one = shared.get(look);
154
+ if (!one) {
155
+ one = ownCopy(material);
156
+ shared.set(look, one);
157
+ }
158
+ return one;
159
+ }
160
+
161
+ // Points every mesh of an avatar's model at the shared material for its
162
+ // look. True when the rig is done; false to try it again later (a material
163
+ // still baking). A mesh that cannot share keeps its own.
164
+ export function shareRig(rig) {
165
+ if (done.has(rig)) {
166
+ return true;
167
+ }
168
+ const meshes = [];
169
+ let waiting = false;
170
+ rig.traverse((object) => {
171
+ if (object.isMesh) {
172
+ meshes.push(object);
173
+ if (!Array.isArray(object.material) && object.material?.visible === false) {
174
+ waiting = true;
175
+ }
176
+ }
177
+ });
178
+ if (waiting) {
179
+ return false;
180
+ }
181
+ for (const mesh of meshes) {
182
+ const one = sharedFor(mesh.material);
183
+ if (one && one !== mesh.material) {
184
+ mesh.material = one;
185
+ }
186
+ }
187
+ done.add(rig);
188
+ return true;
189
+ }
190
+
191
+ // The model under an actor's node (physics-3d world3d `addModelRig`): what
192
+ // is worn and the name tag hang beside it, not in it.
193
+ export function rigOf(node) {
194
+ return node?.children?.find((child) => child.userData?.modelParts instanceof Map) ?? null;
195
+ }
196
+
197
+ // three.js's own opaque order (`painterSortStable`: group order, render
198
+ // order, material id, depth, id), except that inside the draws of one of the
199
+ // kit's shared avatar materials the geometry comes before the depth (above).
200
+ // Every other material's draws keep three's exact order: grouping by geometry
201
+ // there could swap two coplanar meshes of one material, and the later one
202
+ // wins an equal depth. One material is one rule for its whole run, so the
203
+ // order stays consistent.
204
+ export function opaqueOrder(a, b) {
205
+ if (a.groupOrder !== b.groupOrder) {
206
+ return a.groupOrder - b.groupOrder;
207
+ }
208
+ if (a.renderOrder !== b.renderOrder) {
209
+ return a.renderOrder - b.renderOrder;
210
+ }
211
+ if (a.material.id !== b.material.id) {
212
+ return a.material.id - b.material.id;
213
+ }
214
+ if (ours.has(a.material) && a.geometry.id !== b.geometry.id) {
215
+ return a.geometry.id - b.geometry.id;
216
+ }
217
+ if (a.z !== b.z) {
218
+ return a.z - b.z;
219
+ }
220
+ return a.id - b.id;
221
+ }
222
+
223
+ const sorted = new WeakSet();
224
+
225
+ // Every avatar drawn now, `actorIds` the actors of the local player and the
226
+ // others. Cheap once each is done.
227
+ export function share(scene, actorIds) {
228
+ if (!enabled()) {
229
+ return;
230
+ }
231
+ const renderer = scene.three?.renderer;
232
+ if (shareMode() === 'on' && renderer && !sorted.has(renderer) && typeof renderer.setOpaqueSort === 'function') {
233
+ sorted.add(renderer);
234
+ renderer.setOpaqueSort(opaqueOrder);
235
+ }
236
+ const nodes = scene.three?.nodes;
237
+ if (!nodes) {
238
+ return;
239
+ }
240
+ for (const actorId of actorIds) {
241
+ const rig = rigOf(nodes.get(actorId)?.node);
242
+ if (rig) {
243
+ shareRig(rig);
244
+ }
245
+ }
246
+ }
247
+
248
+ // Tests only.
249
+ export function sharedCount() {
250
+ return shared.size;
251
+ }