threedog 0.0.0-stage → 0.2.0

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 (506) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/LICENSE +21 -0
  3. package/README.md +39 -2
  4. package/core/Animator.js +268 -0
  5. package/core/Assets.js +256 -0
  6. package/core/Audio.js +267 -0
  7. package/core/Baked.js +72 -0
  8. package/core/Batching.js +247 -0
  9. package/core/Budget.js +221 -0
  10. package/core/Clock.js +94 -0
  11. package/core/Commits.js +77 -0
  12. package/core/DebugOverrides.js +143 -0
  13. package/core/Describe.js +145 -0
  14. package/core/Engine.js +184 -0
  15. package/core/Events.js +153 -0
  16. package/core/I18n.js +94 -0
  17. package/core/Input.js +245 -0
  18. package/core/Inspect.js +77 -0
  19. package/core/InspectScene.js +191 -0
  20. package/core/Jobs.js +146 -0
  21. package/core/Loading.js +149 -0
  22. package/core/MaterialInfo.js +32 -0
  23. package/core/Ops.js +256 -0
  24. package/core/Random.js +52 -0
  25. package/core/Rays.js +160 -0
  26. package/core/SaveStore.js +85 -0
  27. package/core/Saves.js +254 -0
  28. package/core/Stream.js +279 -0
  29. package/core/Threads.js +158 -0
  30. package/core/UrlState.js +94 -0
  31. package/core/Vars.js +35 -0
  32. package/core/Views.js +49 -0
  33. package/core/actor.js +23 -0
  34. package/core/assetEntry.js +89 -0
  35. package/core/assetLoad.js +190 -0
  36. package/core/assetManifest.js +35 -0
  37. package/core/assetReport.js +83 -0
  38. package/core/audioRules.js +121 -0
  39. package/core/audioVoice.js +225 -0
  40. package/core/batchGroups.js +116 -0
  41. package/core/canvasBox.js +63 -0
  42. package/core/catalog.js +169 -0
  43. package/core/checks.js +67 -0
  44. package/core/claims.js +51 -0
  45. package/core/dayTime.js +15 -0
  46. package/core/debugView.js +76 -0
  47. package/core/describeRender.js +45 -0
  48. package/core/describeText.js +143 -0
  49. package/core/easing.js +34 -0
  50. package/core/errorCodes.js +205 -0
  51. package/core/errorHints.js +208 -0
  52. package/core/errors.js +40 -0
  53. package/core/events.generated.js +84 -0
  54. package/core/ground.js +102 -0
  55. package/core/holds.js +106 -0
  56. package/core/inputContext.js +69 -0
  57. package/core/inputDefaults.js +22 -0
  58. package/core/inputPad.js +35 -0
  59. package/core/inputPointer.js +48 -0
  60. package/core/inputTouch.js +48 -0
  61. package/core/lifecycle.js +69 -0
  62. package/core/lightWatch.js +56 -0
  63. package/core/look/coreParts.js +112 -0
  64. package/core/look/layers.js +136 -0
  65. package/core/look/part.js +173 -0
  66. package/core/look/report.js +79 -0
  67. package/core/look/write.js +86 -0
  68. package/core/loop.js +200 -0
  69. package/core/math.js +33 -0
  70. package/core/modules.js +196 -0
  71. package/core/movement.js +19 -0
  72. package/core/mulberry32.js +14 -0
  73. package/core/opsAtomic.js +31 -0
  74. package/core/opsCheck.js +97 -0
  75. package/core/opsFail.js +20 -0
  76. package/core/opsGuard.js +26 -0
  77. package/core/opsHistory.js +27 -0
  78. package/core/opsRun.js +77 -0
  79. package/core/page.js +78 -0
  80. package/core/params.js +132 -0
  81. package/core/pureWorker.js +5 -0
  82. package/core/renamed.js +21 -0
  83. package/core/saveApply.js +128 -0
  84. package/core/saveFiles.js +139 -0
  85. package/core/saveIds.js +53 -0
  86. package/core/saveVersions.js +97 -0
  87. package/core/scatter.js +92 -0
  88. package/core/sceneMethods.js +100 -0
  89. package/core/screenToGround.js +39 -0
  90. package/core/services.js +73 -0
  91. package/core/session.js +171 -0
  92. package/core/shadowTypes.js +15 -0
  93. package/core/skip.js +104 -0
  94. package/core/startup.js +135 -0
  95. package/core/streamCells.js +24 -0
  96. package/core/streamSources.js +73 -0
  97. package/core/styleParts.js +96 -0
  98. package/core/targeting.js +64 -0
  99. package/core/thingExplain.js +68 -0
  100. package/core/thingOps.js +168 -0
  101. package/core/things.js +212 -0
  102. package/core/threadPure.js +120 -0
  103. package/core/threadServe.js +25 -0
  104. package/core/tier.js +11 -0
  105. package/core/util.js +11 -0
  106. package/core/validate.js +204 -0
  107. package/docs/AI.md +123 -0
  108. package/docs/API.md +83 -0
  109. package/docs/CODEMAP.md +347 -0
  110. package/docs/COMMANDS.md +108 -0
  111. package/docs/CONVENTIONS.md +145 -0
  112. package/docs/PERF.md +217 -0
  113. package/docs/THREE.md +86 -0
  114. package/docs/api/ambience.md +15 -0
  115. package/docs/api/assets.md +62 -0
  116. package/docs/api/audio.md +53 -0
  117. package/docs/api/budget.md +40 -0
  118. package/docs/api/cameraRig.md +120 -0
  119. package/docs/api/characters.md +144 -0
  120. package/docs/api/clock.md +7 -0
  121. package/docs/api/collision.md +39 -0
  122. package/docs/api/content.md +64 -0
  123. package/docs/api/dialogue.md +39 -0
  124. package/docs/api/engine.md +199 -0
  125. package/docs/api/entities.md +61 -0
  126. package/docs/api/events.md +95 -0
  127. package/docs/api/flow.md +27 -0
  128. package/docs/api/grids.md +11 -0
  129. package/docs/api/ground.md +16 -0
  130. package/docs/api/hlod.md +34 -0
  131. package/docs/api/hud.md +31 -0
  132. package/docs/api/i18n.md +10 -0
  133. package/docs/api/input.md +24 -0
  134. package/docs/api/interactions.md +31 -0
  135. package/docs/api/inventory.md +66 -0
  136. package/docs/api/jobs.md +10 -0
  137. package/docs/api/level.md +41 -0
  138. package/docs/api/lightPool.md +56 -0
  139. package/docs/api/music.md +34 -0
  140. package/docs/api/nav.md +26 -0
  141. package/docs/api/net.md +14 -0
  142. package/docs/api/openWorld.md +24 -0
  143. package/docs/api/particles.md +41 -0
  144. package/docs/api/photo.md +15 -0
  145. package/docs/api/post.md +10 -0
  146. package/docs/api/quests.md +36 -0
  147. package/docs/api/rays.md +26 -0
  148. package/docs/api/rigid.md +84 -0
  149. package/docs/api/saves.md +32 -0
  150. package/docs/api/settings.md +36 -0
  151. package/docs/api/sky.md +58 -0
  152. package/docs/api/stream.md +31 -0
  153. package/docs/api/style.md +134 -0
  154. package/docs/api/terrain.md +34 -0
  155. package/docs/api/timeline.md +39 -0
  156. package/docs/api/tools.md +57 -0
  157. package/docs/api/triggers.md +56 -0
  158. package/docs/api/vars.md +9 -0
  159. package/docs/api/vegetation.md +52 -0
  160. package/docs/api/water.md +48 -0
  161. package/docs/api/weather.md +29 -0
  162. package/docs/api/world.md +30 -0
  163. package/docs/recipes/01-new-game.md +148 -0
  164. package/docs/recipes/02-npc.md +167 -0
  165. package/docs/recipes/03-items-pickups.md +158 -0
  166. package/docs/recipes/04-quests.md +83 -0
  167. package/docs/recipes/05-props-bake.md +159 -0
  168. package/docs/recipes/06-clock-events.md +117 -0
  169. package/docs/recipes/07-flow-scenes-pause.md +132 -0
  170. package/docs/recipes/08-weather-sky.md +74 -0
  171. package/docs/recipes/09-style-pack.md +234 -0
  172. package/docs/recipes/10-blender-level.md +67 -0
  173. package/docs/recipes/11-vehicles.md +55 -0
  174. package/docs/recipes/12-navmesh.md +79 -0
  175. package/docs/recipes/13-animation.md +149 -0
  176. package/docs/recipes/14-custom-module.md +91 -0
  177. package/docs/recipes/15-perf.md +78 -0
  178. package/docs/recipes/16-dev-bridge.md +180 -0
  179. package/docs/recipes/18-world.md +185 -0
  180. package/docs/recipes/19-shooting-picking.md +76 -0
  181. package/docs/recipes/20-content-actions-save.md +176 -0
  182. package/docs/recipes/21-camera.md +153 -0
  183. package/docs/recipes/22-shop-gold.md +107 -0
  184. package/docs/recipes/23-ops.md +175 -0
  185. package/docs/recipes/26-ui-parts.md +138 -0
  186. package/docs/recipes/27-net.md +96 -0
  187. package/docs/recipes/28-terrain.md +80 -0
  188. package/docs/recipes/29-grid.md +87 -0
  189. package/docs/recipes/30-templates.md +131 -0
  190. package/docs/recipes/31-audio.md +126 -0
  191. package/docs/recipes/32-action-game.md +33 -0
  192. package/docs/recipes/33-stream-sources.md +37 -0
  193. package/docs/recipes/34-environment-lighting.md +36 -0
  194. package/docs/recipes/35-style-from-reference.md +89 -0
  195. package/docs/recipes/36-sky-ground-plant-looks.md +116 -0
  196. package/docs/recipes/37-terrain-look-hooks.md +97 -0
  197. package/docs/recipes/38-environment-look-hooks.md +83 -0
  198. package/docs/recipes/40-scene.md +108 -0
  199. package/docs/recipes/41-thing-registry.md +93 -0
  200. package/docs/recipes/42-terrain-shape.md +64 -0
  201. package/docs/recipes/43-shader-patches-surface-looks.md +120 -0
  202. package/docs/recipes/44-water-weather-look-hooks.md +135 -0
  203. package/docs/recipes/45-cascaded-shadows.md +51 -0
  204. package/docs/recipes/46-blender-art-route.md +114 -0
  205. package/docs/recipes/47-island-snow-backdrop.md +44 -0
  206. package/docs/recipes/48-make-material.md +70 -0
  207. package/docs/recipes/49-game-module-contract.md +71 -0
  208. package/docs/recipes/50-own-renderer.md +12 -0
  209. package/docs/recipes/51-capability-catalog.md +63 -0
  210. package/docs/recipes/52-look.md +108 -0
  211. package/examples/action/DESIGN.md +3 -0
  212. package/examples/action/GAMEMAP.md +127 -0
  213. package/examples/action/content/actions.js +8 -0
  214. package/examples/action/content/audio.js +9 -0
  215. package/examples/action/content/index.js +10 -0
  216. package/examples/action/content/items.js +4 -0
  217. package/examples/action/content/places.js +4 -0
  218. package/examples/action/content/props.js +11 -0
  219. package/examples/action/content/templates.js +10 -0
  220. package/examples/action/content/views.js +6 -0
  221. package/examples/action/look.js +5 -0
  222. package/examples/action/main.js +137 -0
  223. package/examples/action/models.js +25 -0
  224. package/examples/sheepisland/GAMEMAP.md +107 -0
  225. package/examples/sheepisland/content/index.js +6 -0
  226. package/examples/sheepisland/content/props.js +19 -0
  227. package/examples/sheepisland/content/templates.js +5 -0
  228. package/examples/sheepisland/content/vegetation.js +8 -0
  229. package/examples/sheepisland/main.js +71 -0
  230. package/examples/sheepisland/params.js +25 -0
  231. package/examples/sheepisland/style/index.js +79 -0
  232. package/examples/sheepisland/style/models.js +59 -0
  233. package/examples/sheepisland/views.js +9 -0
  234. package/examples/styletest/GAMEMAP.md +135 -0
  235. package/examples/styletest/content/index.js +9 -0
  236. package/examples/styletest/content/items.js +8 -0
  237. package/examples/styletest/content/npcs.js +8 -0
  238. package/examples/styletest/content/places.js +5 -0
  239. package/examples/styletest/content/props.js +16 -0
  240. package/examples/styletest/content/templates.js +9 -0
  241. package/examples/styletest/content/vegetation.js +31 -0
  242. package/examples/styletest/look.js +11 -0
  243. package/examples/styletest/main.js +95 -0
  244. package/examples/styletest/models.js +87 -0
  245. package/examples/styletest/overrides.js +33 -0
  246. package/examples/styletest/panel.js +135 -0
  247. package/examples/styletest/params.js +32 -0
  248. package/examples/styletest/views.js +31 -0
  249. package/examples/world/DESIGN.md +5 -0
  250. package/examples/world/GAMEMAP.md +171 -0
  251. package/examples/world/content/actions.js +8 -0
  252. package/examples/world/content/audio.js +25 -0
  253. package/examples/world/content/bodies.js +12 -0
  254. package/examples/world/content/containers.js +4 -0
  255. package/examples/world/content/dialogues.js +33 -0
  256. package/examples/world/content/index.js +17 -0
  257. package/examples/world/content/items.js +10 -0
  258. package/examples/world/content/npcs.js +14 -0
  259. package/examples/world/content/pickups.js +11 -0
  260. package/examples/world/content/places.js +6 -0
  261. package/examples/world/content/props.js +12 -0
  262. package/examples/world/content/quests.js +15 -0
  263. package/examples/world/content/rules.js +23 -0
  264. package/examples/world/content/templates.js +5 -0
  265. package/examples/world/content/vegetation.js +32 -0
  266. package/examples/world/main.js +168 -0
  267. package/examples/world/models.js +28 -0
  268. package/index.d.ts +306 -0
  269. package/index.js +122 -0
  270. package/llms.txt +61 -0
  271. package/modules/ambience/Ambience.js +186 -0
  272. package/modules/camera/FirstPersonCamera.js +131 -0
  273. package/modules/camera/FixedCamera.js +162 -0
  274. package/modules/camera/FollowCamera.js +91 -0
  275. package/modules/camera/RtsCamera.js +132 -0
  276. package/modules/camera/common.js +182 -0
  277. package/modules/camera/index.js +11 -0
  278. package/modules/camera/projection.js +34 -0
  279. package/modules/characters/Character.js +235 -0
  280. package/modules/characters/NPC.js +242 -0
  281. package/modules/characters/assetModel.js +42 -0
  282. package/modules/characters/astar.js +66 -0
  283. package/modules/characters/behaviors.js +72 -0
  284. package/modules/characters/footsteps.js +13 -0
  285. package/modules/characters/index.js +215 -0
  286. package/modules/characters/moveHelpers.js +29 -0
  287. package/modules/characters/movers/common.js +62 -0
  288. package/modules/characters/movers/rapier.js +74 -0
  289. package/modules/characters/movers/simple.js +43 -0
  290. package/modules/characters/npcAvoid.js +129 -0
  291. package/modules/characters/npcSim.js +150 -0
  292. package/modules/characters/npcView.js +42 -0
  293. package/modules/characters/pathAsync.js +113 -0
  294. package/modules/characters/pathJob.js +62 -0
  295. package/modules/characters/pathWorker.js +7 -0
  296. package/modules/characters/playerStrings.js +12 -0
  297. package/modules/characters/proceduralRig.js +58 -0
  298. package/modules/characters/useModel.js +22 -0
  299. package/modules/characters/walkTo.js +58 -0
  300. package/modules/collision/Collision.js +274 -0
  301. package/modules/collision/grid.js +63 -0
  302. package/modules/content/explain.js +13 -0
  303. package/modules/content/ids.js +95 -0
  304. package/modules/content/index.js +154 -0
  305. package/modules/content/loader.js +190 -0
  306. package/modules/content/models.js +69 -0
  307. package/modules/content/packs.js +69 -0
  308. package/modules/content/props.js +89 -0
  309. package/modules/content/rules.js +134 -0
  310. package/modules/content/templates.js +189 -0
  311. package/modules/debug/DebugPanel.js +174 -0
  312. package/modules/entities/Entities.js +243 -0
  313. package/modules/entities/build.js +25 -0
  314. package/modules/entities/index.js +4 -0
  315. package/modules/flow/index.js +101 -0
  316. package/modules/gameplay/Container.js +227 -0
  317. package/modules/gameplay/Containers.js +89 -0
  318. package/modules/gameplay/Dialogue.js +219 -0
  319. package/modules/gameplay/Interactions.js +253 -0
  320. package/modules/gameplay/Inventory.js +224 -0
  321. package/modules/gameplay/Quests.js +205 -0
  322. package/modules/gameplay/boxOps.js +60 -0
  323. package/modules/gameplay/dialogueBox.js +48 -0
  324. package/modules/gameplay/interactionPrompt.js +28 -0
  325. package/modules/gameplay/inventoryOps.js +137 -0
  326. package/modules/gameplay/pickups.js +115 -0
  327. package/modules/gameplay/questLog.js +54 -0
  328. package/modules/gameplay/questSave.js +71 -0
  329. package/modules/grid/Grid.js +184 -0
  330. package/modules/grid/Layer.js +142 -0
  331. package/modules/grid/index.js +236 -0
  332. package/modules/grid/ops.js +73 -0
  333. package/modules/grid/preview.js +85 -0
  334. package/modules/grid/tiles.js +79 -0
  335. package/modules/hlod/index.js +167 -0
  336. package/modules/hud/index.js +167 -0
  337. package/modules/hud/parts.js +80 -0
  338. package/modules/level/geometry.js +46 -0
  339. package/modules/level/heightfield.js +160 -0
  340. package/modules/level/index.js +233 -0
  341. package/modules/level/nodes.js +84 -0
  342. package/modules/lights/index.js +227 -0
  343. package/modules/music/Music.js +294 -0
  344. package/modules/music/index.js +2 -0
  345. package/modules/music/rules.js +28 -0
  346. package/modules/nav/index.js +156 -0
  347. package/modules/nav/navPath.js +17 -0
  348. package/modules/net/Client.js +137 -0
  349. package/modules/net/Host.js +117 -0
  350. package/modules/net/Net.js +49 -0
  351. package/modules/net/index.js +77 -0
  352. package/modules/net/sync.js +114 -0
  353. package/modules/net/transports.js +118 -0
  354. package/modules/particles/Particles.js +179 -0
  355. package/modules/particles/effect.js +112 -0
  356. package/modules/particles/shaders.js +28 -0
  357. package/modules/photo/Photo.js +146 -0
  358. package/modules/rigid/RigidBodies.js +265 -0
  359. package/modules/rigid/Vehicles.js +97 -0
  360. package/modules/rigid/sync.js +158 -0
  361. package/modules/settings/Settings.js +297 -0
  362. package/modules/settings/apply.js +21 -0
  363. package/modules/settings/index.js +3 -0
  364. package/modules/settings/sections.js +156 -0
  365. package/modules/settings/strings.js +53 -0
  366. package/modules/settings/view.js +65 -0
  367. package/modules/sky/Sky.js +259 -0
  368. package/modules/sky/calendar.js +80 -0
  369. package/modules/sky/cascades.js +125 -0
  370. package/modules/sky/clouds.js +79 -0
  371. package/modules/sky/domeLayers.js +33 -0
  372. package/modules/sky/environment.js +141 -0
  373. package/modules/sky/fog.js +139 -0
  374. package/modules/sky/frame.js +77 -0
  375. package/modules/sky/layers.js +69 -0
  376. package/modules/sky/look.js +51 -0
  377. package/modules/sky/lookFog.js +80 -0
  378. package/modules/sky/lookShadow.js +50 -0
  379. package/modules/sky/lookSky.js +108 -0
  380. package/modules/sky/schedule.js +80 -0
  381. package/modules/sky/shader.js +96 -0
  382. package/modules/sky/shadow.js +74 -0
  383. package/modules/sky/skyModule.js +60 -0
  384. package/modules/terrain/ChunkManager.js +260 -0
  385. package/modules/terrain/chunkJob.js +19 -0
  386. package/modules/terrain/chunkLevels.js +40 -0
  387. package/modules/terrain/chunkQueries.js +42 -0
  388. package/modules/terrain/chunkWorker.js +5 -0
  389. package/modules/terrain/index.js +118 -0
  390. package/modules/terrain/lookGround.js +61 -0
  391. package/modules/terrain/mountTerrain.js +32 -0
  392. package/modules/terrain/terrainColorMap.js +41 -0
  393. package/modules/terrain/terrainDefaults.js +39 -0
  394. package/modules/terrain/terrainEdit.js +139 -0
  395. package/modules/terrain/terrainGen.js +155 -0
  396. package/modules/terrain/terrainIsland.js +131 -0
  397. package/modules/terrain/terrainMesh.js +92 -0
  398. package/modules/terrain/terrainShape.js +108 -0
  399. package/modules/terrain/terrainShapeMain.js +41 -0
  400. package/modules/terrain/terrainTexture.js +44 -0
  401. package/modules/timeline/Timeline.js +253 -0
  402. package/modules/timeline/Tween.js +114 -0
  403. package/modules/timeline/letterbox.js +24 -0
  404. package/modules/timeline/shots.js +90 -0
  405. package/modules/triggers/index.js +268 -0
  406. package/modules/vegetation/Vegetation.js +256 -0
  407. package/modules/vegetation/groundChunks.js +177 -0
  408. package/modules/vegetation/index.js +43 -0
  409. package/modules/vegetation/lookWind.js +37 -0
  410. package/modules/vegetation/modelGeometry.js +21 -0
  411. package/modules/water/Water.js +160 -0
  412. package/modules/water/look.js +80 -0
  413. package/modules/water/surface.js +88 -0
  414. package/modules/weather/Weather.js +242 -0
  415. package/modules/weather/look.js +75 -0
  416. package/modules/weather/particles.js +92 -0
  417. package/modules/weather/shaders.js +49 -0
  418. package/modules/weather/strings.js +15 -0
  419. package/modules/world/World.js +231 -0
  420. package/modules/world/cells.js +42 -0
  421. package/modules/world/explain.js +11 -0
  422. package/modules/world/index.js +71 -0
  423. package/modules/world/persist.js +107 -0
  424. package/modules/world/pos.js +20 -0
  425. package/modules/world/records.js +89 -0
  426. package/modules/world/sim.js +175 -0
  427. package/modules/world/simFar.js +82 -0
  428. package/modules/world/state.js +115 -0
  429. package/modules/world/thingSource.js +44 -0
  430. package/modules/world/views.js +182 -0
  431. package/package.json +66 -4
  432. package/presets.js +96 -0
  433. package/render/PostFX.js +240 -0
  434. package/render/detail.js +105 -0
  435. package/render/effects.js +144 -0
  436. package/render/engineUniforms.js +23 -0
  437. package/render/fuse.js +84 -0
  438. package/render/lighting.js +100 -0
  439. package/render/lights.js +18 -0
  440. package/render/lookSurface.js +161 -0
  441. package/render/materials.js +220 -0
  442. package/render/patches.js +252 -0
  443. package/render/postLook.js +86 -0
  444. package/render/surface.js +69 -0
  445. package/render/uniformDecl.js +29 -0
  446. package/types/common.d.ts +34 -0
  447. package/types/core.d.ts +1965 -0
  448. package/types/gameplay.d.ts +1822 -0
  449. package/types/generated/core/Audio.d.ts +200 -0
  450. package/types/generated/core/Batching.d.ts +47 -0
  451. package/types/generated/core/Budget.d.ts +90 -0
  452. package/types/generated/core/Commits.d.ts +31 -0
  453. package/types/generated/core/DebugOverrides.d.ts +52 -0
  454. package/types/generated/core/Events.d.ts +89 -0
  455. package/types/generated/core/I18n.d.ts +30 -0
  456. package/types/generated/core/Input.d.ts +118 -0
  457. package/types/generated/core/Jobs.d.ts +41 -0
  458. package/types/generated/core/Ops.d.ts +203 -0
  459. package/types/generated/core/Random.d.ts +49 -0
  460. package/types/generated/core/Rays.d.ts +49 -0
  461. package/types/generated/core/Saves.d.ts +162 -0
  462. package/types/generated/core/Stream.d.ts +93 -0
  463. package/types/generated/core/Threads.d.ts +82 -0
  464. package/types/generated/core/Views.d.ts +33 -0
  465. package/types/generated/core/audioVoice.d.ts +47 -0
  466. package/types/generated/core/page.d.ts +18 -0
  467. package/types/generated/core/session.d.ts +48 -0
  468. package/types/generated/modules/ambience/Ambience.d.ts +48 -0
  469. package/types/generated/modules/debug/DebugPanel.d.ts +24 -0
  470. package/types/generated/modules/flow/index.d.ts +10 -0
  471. package/types/generated/modules/grid/Grid.d.ts +151 -0
  472. package/types/generated/modules/grid/Layer.d.ts +65 -0
  473. package/types/generated/modules/grid/index.d.ts +86 -0
  474. package/types/generated/modules/hlod/index.d.ts +11 -0
  475. package/types/generated/modules/hud/index.d.ts +10 -0
  476. package/types/generated/modules/lights/index.d.ts +13 -0
  477. package/types/generated/modules/music/Music.d.ts +111 -0
  478. package/types/generated/modules/net/Net.d.ts +34 -0
  479. package/types/generated/modules/net/index.d.ts +53 -0
  480. package/types/generated/modules/net/transports.d.ts +58 -0
  481. package/types/generated/modules/particles/Particles.d.ts +69 -0
  482. package/types/generated/modules/sky/Sky.d.ts +145 -0
  483. package/types/generated/modules/sky/skyModule.d.ts +10 -0
  484. package/types/generated/modules/timeline/Timeline.d.ts +51 -0
  485. package/types/generated/modules/water/Water.d.ts +29 -0
  486. package/types/generated/modules/weather/Weather.d.ts +62 -0
  487. package/types/generated/modules/world/World.d.ts +242 -0
  488. package/types/generated/modules/world/index.d.ts +15 -0
  489. package/types/generated/render/PostFX.d.ts +125 -0
  490. package/types/generated/render/materials.d.ts +100 -0
  491. package/types/generated/render/patches.d.ts +41 -0
  492. package/types/generated/ui/Focus.d.ts +11 -0
  493. package/types/generated/ui/LoadingScreen.d.ts +2 -0
  494. package/types/generated/ui/UI.d.ts +57 -0
  495. package/types/nav.d.ts +52 -0
  496. package/types/physics.d.ts +592 -0
  497. package/types/style.d.ts +638 -0
  498. package/types/worker.d.ts +11 -0
  499. package/types/world.d.ts +772 -0
  500. package/ui/Focus.js +51 -0
  501. package/ui/LoadingScreen.js +42 -0
  502. package/ui/Parts.js +199 -0
  503. package/ui/UI.js +167 -0
  504. package/ui/defaults.js +85 -0
  505. package/ui/theme.js +124 -0
  506. package/ui/touchPart.js +49 -0
@@ -0,0 +1,175 @@
1
+ // Simulation layer of world: near records every fixed step, far records in id buckets taking turns under a per-step cap. The public API is in World.js
2
+ // (simulate / anchor); this is the implementation.
3
+ // Time: the dt of both near and far is "accumulated fixed-step seconds" (same ruler as every engine hook's dt, not multiplied by clock.timeScale).
4
+ // For one record going near, far, near again, the dts add up to the elapsed time:
5
+ // far dt = seconds accumulated since the last simulation; on the far → near step, far first pays back the owed time, then near runs.
6
+ // A kind with only far (crops) does not split near / far and always advances at far's pace; a kind with only near is not simulated while far and its owed time
7
+ // is not made up.
8
+ // Owed time is saved: it is paid back after loading, so simulate code never tracks time itself.
9
+ // Determinism: near / far is split by the gameplay anchor (world.anchor), far work is capped by count with the rest queued first for the next step, and buckets
10
+ // follow the step count (saved) —
11
+ // so the result depends only on data and step count, not on machine speed, frame rate or whether models are built.
12
+ // Far with worker: true goes through simFar.js (batched to engine.threads, written back on a fixed later step through commits).
13
+ // Near order: at the start of each step collect records inside the radius, then simulate them sorted by id — order inside a cell depends on insertion order and
14
+ // changes after loading;
15
+ // when near records affect each other (avoiding each other, fighting over the same thing), sorting by id keeps "kept running" and "saved then loaded"
16
+ // bit-identical.
17
+
18
+ import { DEV, checkOptionsArg } from '../../core/validate.js';
19
+ import { FarFlights } from './simFar.js';
20
+
21
+ /** Near simulation order: by id (independent of insertion order and loading) */
22
+ const byId = (a, b) => a.rec.id < b.rec.id ? -1 : 1;
23
+ /** Simple hash of id: far simulation uses it to pick a bucket (computed once per record, stored on ent) */
24
+ const hashOf = s => { let h = 0; for (let i = 0; i < s.length; i++) h = (Math.imul(h, 31) + s.charCodeAt(i)) | 0; return h >>> 0; };
25
+
26
+ /** @internal */
27
+ export class Sim {
28
+ /** @param {import('./World.js').World} w */
29
+ constructor(w) {
30
+ this.w = w;
31
+ this.sims = new Map(); // kind → { near, far, farEvery, radius }
32
+ this.ents = new Map(); // id → { rec, h, last, near, queued, dead }: last = time of the last simulation, near = step number of the last near simulation
33
+ this.kinds = new Map(); // kind → { set: Set<ent>, P, buckets }: P is the far period (steps), buckets[step % P] is this step's turn
34
+ this.owed = new Map(); // from a save, content not added yet: id → time of the last simulation (used when added)
35
+ this.backlog = []; // far simulations left undone by the previous step's cap (FIFO, done first next step)
36
+ this.time = 0; // accumulated simulation seconds (saved)
37
+ this.n = 0; // simulation step count (saved: buckets follow it, so after loading the same batch is up as in a live run)
38
+ this.at = { x: 0, z: 0 }; // this step's simulation center (taken from world.anchor at the start of each step)
39
+ this.dt = 0;
40
+ this.list = []; // records to simulate near this step (reused, not allocated each step)
41
+ this.flights = new FarFlights(this); // far with worker: true (simFar.js)
42
+ // created once, no closure per step
43
+ this._pick = (rec, d2) => { const s = this.sims.get(rec.kind); if (s && d2 <= s.r2 && this.w._inZone(rec)) this.list.push(this.ents.get(rec.id)); };
44
+ }
45
+
46
+ simulate(kind, opts) {
47
+ if (DEV) checkOptionsArg(`world.simulate('${kind}', opts)`, opts, ['near', 'far', 'farEvery', 'radius', 'worker']);
48
+ const s = { ...opts };
49
+ if (s.worker && s.far) this.flights.register(kind, s);
50
+ this.sims.set(kind, s);
51
+ return { remove: () => { if (this.sims.get(kind) === s) this.sims.delete(kind); } };
52
+ }
53
+ /** A new record starts counting time from the moment it is added (owed time from a save carries on) */
54
+ added(rec) {
55
+ const id = rec.id, o = this.owed.get(id);
56
+ if (o !== undefined) this.owed.delete(id);
57
+ const e = { rec, h: hashOf(id), last: o ?? this.time, near: -1, queued: false, dead: false, flight: null };
58
+ this.ents.set(id, e);
59
+ let K = this.kinds.get(rec.kind); if (!K) this.kinds.set(rec.kind, K = { set: new Set(), P: 0, buckets: null });
60
+ K.set.add(e); if (K.P) K.buckets[e.h % K.P].add(e);
61
+ }
62
+ removed(id) {
63
+ const e = this.ents.get(id); if (!e) return;
64
+ this.ents.delete(id); e.dead = true; // queued entries are skipped when seen dead
65
+ const K = this.kinds.get(e.rec.kind); K.set.delete(e); if (K.P) K.buckets[e.h % K.P].delete(e);
66
+ }
67
+ /** This record was simulated near in the latest step (the presentation layer uses it to keep positionFrom 'view' models) */
68
+ isNear(id) { return this.ents.get(id)?.near === this.n; }
69
+
70
+ /** Near radius: the written one, else the presentation radius. Models that move themselves (positionFrom 'view') widen to the teardown distance (1.25×) —
71
+ * why: the model may still exist within that distance, and one with a model must run near, otherwise far changes to the data get copied back over by world from the model position */
72
+ radius(kind, s) {
73
+ const W = this.w, V = W._v, p = V.presenters.get(kind);
74
+ if (s.radius != null) return typeof s.radius === 'function' ? s.radius() : s.radius;
75
+ if (!p) return W.radius;
76
+ return V.radius(p) * (p.positionFrom === 'view' ? 1.25 : 1);
77
+ }
78
+
79
+ fixed(dt, anchor) {
80
+ const W = this.w, now = this.time += dt, n = ++this.n;
81
+ this.at.x = anchor.x; this.at.z = anchor.z; this.dt = dt;
82
+ if (!this.sims.size) return;
83
+ let maxR2 = -1;
84
+ // radius may be a function: asked once per kind per step
85
+ for (const [kind, s] of this.sims) { s.r2 = s.near ? this.radius(kind, s) ** 2 : -1; if (s.r2 > maxR2) maxR2 = s.r2; }
86
+ if (maxR2 >= 0) {
87
+ const L = this.list; L.length = 0;
88
+ W._cells.forEach(this.at, Math.sqrt(maxR2), this._pick);
89
+ L.sort(byId);
90
+ for (const e of L) e.near = n; // mark first: isNear in simulate and skipping far both follow what was picked at the start of this step
91
+ for (const e of L) if (!e.dead) this.nearOne(e);
92
+ }
93
+ // far: first the ones queued by the previous step, then this step's bucket; at most farMaxPerTick per step, the rest queue (dt accumulates, no time lost)
94
+ const max = W.farMaxPerTick, q = this.backlog;
95
+ let done = 0, i = 0;
96
+ for (; i < q.length && done < max; i++) {
97
+ const e = q[i]; e.queued = false;
98
+ const s = !e.dead && this.sims.get(e.rec.kind);
99
+ if (s?.far && !e.flight) done += this.farOne(e, s, now);
100
+ }
101
+ if (i) q.splice(0, i);
102
+ for (const [kind, s] of this.sims) {
103
+ const K = s.far && this.kinds.get(kind); if (!K?.set.size) continue;
104
+ const P = Math.max(1, Math.round((s.farEvery ?? W.farEvery) / dt));
105
+ if (K.P !== P) this.rebucket(K, P);
106
+ for (const e of K.buckets[n % P]) {
107
+ if (e.near === n || e.queued || e.flight) continue;
108
+ if (done >= max) { e.queued = true; q.push(e); continue; }
109
+ done += this.farOne(e, s, now);
110
+ }
111
+ }
112
+ this.flights.flush();
113
+ }
114
+ nearOne(e) {
115
+ const rec = e.rec, s = this.sims.get(rec.kind), now = this.time, dt = this.dt;
116
+ if (!s?.near) return; // earlier simulation in this step replaced the registration of this kind
117
+ // For records whose model drives the position (positionFrom 'view'): near simulation needs the model, so build it right now instead of next frame —
118
+ // otherwise the steps before the model is built and the first step after loading would differ from a live run (the result would depend on frame boundaries)
119
+ const V = this.w._v, p = V.presenters.get(rec.kind);
120
+ if (p?.positionFrom === 'view' && !V.map.has(rec.id) && (!p.show || p.show(rec))) V.build(rec, p);
121
+ if (e.flight) this.flights.land(e);
122
+ const owed = now - dt - e.last; // just came in from far: first make up the stretch far has not simulated
123
+ e.last = now;
124
+ if (owed > 1e-9 && s.far) s.far(rec, owed);
125
+ s.near(rec, dt, this.w.view(rec.id));
126
+ }
127
+ farOne(e, s, now) {
128
+ const dt = now - e.last;
129
+ e.last = now;
130
+ if (dt <= 0) return 0; // added during this step
131
+ if (s.pure) this.flights.add(e, s, dt); else s.far(e.rec, dt);
132
+ return 1;
133
+ }
134
+ /** engine.skip: move simulation time without stepping; on the last segment (done) every record with far catches up what it owes in one far(rec, dt), in id order.
135
+ * Same rule as a far turn: kinds with only near are not made up. Worker kinds run here synchronously (the skip result must exist when skip returns) */
136
+ skip(dt, done) {
137
+ const now = this.time += dt;
138
+ if (!done) return;
139
+ for (const e of [...this.ents.values()].sort(byId)) {
140
+ const s = !e.dead && this.sims.get(e.rec.kind);
141
+ if (!s?.far) continue;
142
+ if (e.flight) this.flights.land(e);
143
+ const owed = now - e.last;
144
+ e.last = now;
145
+ if (owed <= 0) continue;
146
+ if (s.pure) this.flights.now(e, s, owed); else s.far(e.rec, owed);
147
+ }
148
+ }
149
+ rebucket(K, P) {
150
+ K.P = P; K.buckets = Array.from({ length: P }, () => new Set());
151
+ for (const e of K.set) K.buckets[e.h % P].add(e);
152
+ }
153
+
154
+ /** Save: step count, accumulated seconds, and how many seconds each far-simulated record still owes (near ones simulate every step, owe nothing, are not written).
155
+ * Step count and seconds are saved too: after loading buckets take turns in the same batch and dt has the same float error, so one save advances the same way every time */
156
+ toJSON() {
157
+ const owed = {}, W = this.w, t = this.time;
158
+ // in flight (worker far not landed): the record does not have that stretch yet, so it is still owed
159
+ // only kinds simulated with far owe time: a prop, pickup or near-only kind never makes it up, so it writes nothing (saves stay small)
160
+ for (const [id, e] of this.ents) {
161
+ if (!this.sims.get(e.rec.kind)?.far) continue;
162
+ const last = e.flight ? e.flight.from : e.last;
163
+ if (t - last > 1e-6 && !W._unsaved.has(id)) owed[id] = t - last;
164
+ }
165
+ for (const [id, last] of this.owed) owed[id] = t - last; // those whose content is not added back yet are written too (used on the next add)
166
+ return { step: this.n, time: t, owed };
167
+ }
168
+ /** Load: work back the last simulation time from "seconds owed"; records are re-added later by loadWorld */
169
+ fromJSON(d) {
170
+ for (const e of this.ents.values()) e.dead = true; // results still in flight belong to the old records: dropped
171
+ this.ents.clear(); this.kinds.clear(); this.backlog.length = 0;
172
+ this.n = d?.step ?? 0; this.time = d?.time ?? 0;
173
+ this.owed = new Map(Object.entries(d?.owed || {}).map(([id, s]) => [id, this.time - s]));
174
+ }
175
+ }
@@ -0,0 +1,82 @@
1
+ // Far simulation in workers: world.simulate(kind, { far, worker: true }). Part of sim.js.
2
+ // Each step, the far records picked for this step (buckets and cap work exactly as without workers) are copied as one batch per kind,
3
+ // sent to engine.threads, and the results are written back FAR_LAG steps later through engine.commits:
4
+ // arrived in time -> worker result; not yet -> the same function runs on the main thread on the same copies.
5
+ // So threads on, off, slow or crashed all give bit-identical records at every step (same V8, same recompiled source, same input).
6
+ // Write-back is a field diff against the copy: only fields far changed are written, so gameplay changing other fields during the flight is kept;
7
+ // a field both changed is won by far (it lands later). A record that turns near while in flight lands its result first (same value), then near runs.
8
+ // A record in flight is skipped by far scheduling until it lands; its time keeps accumulating and is simulated on its next turn, so no time is lost.
9
+
10
+ /** Steps between sending a batch and writing it back (4 = 67 ms): enough for a worker round trip, short enough that far records barely lag */
11
+ const FAR_LAG = 4;
12
+
13
+ /** Deep equality of plain data (records hold only plain values) */
14
+ function same(a, b) {
15
+ if (a === b) return true;
16
+ if (!a || !b || typeof a !== 'object' || typeof b !== 'object') return Number.isNaN(a) && Number.isNaN(b);
17
+ if (Array.isArray(a) !== Array.isArray(b)) return false;
18
+ const ka = Object.keys(a);
19
+ if (ka.length !== Object.keys(b).length) return false;
20
+ for (const k of ka) if (!same(a[k], b[k])) return false;
21
+ return true;
22
+ }
23
+
24
+ /** @internal */
25
+ export class FarFlights {
26
+ /** @param {import('./sim.js').Sim} sim */
27
+ constructor(sim) {
28
+ this.sim = sim;
29
+ this.open = new Map(); // simulate registration -> [[ent, dt], ...] picked this step
30
+ this.defs = 0;
31
+ }
32
+ /** simulate(kind, { worker: true }): recompile far so it runs the same in a worker and here; the original closure is never called */
33
+ register(kind, s) {
34
+ const T = this.sim.w.engine.threads, label = `world.simulate('${kind}') far`;
35
+ s.pure = T.pure(s.far, label);
36
+ s.far = s.pure.call;
37
+ s.key = kind + '#' + ++this.defs;
38
+ if (T.size) T.pureLane().broadcast({ def: s.key, src: s.pure.src, label }, 'def:' + s.key);
39
+ }
40
+ add(e, s, dt) {
41
+ let b = this.open.get(s); if (!b) this.open.set(s, b = []);
42
+ b.push([e, dt]);
43
+ }
44
+ /** End of the far pass: send each kind's batch */
45
+ flush() {
46
+ for (const [s, items] of this.open) this.send(s, items);
47
+ this.open.clear();
48
+ }
49
+ send(s, items) {
50
+ const engine = this.sim.w.engine, T = engine.threads;
51
+ // copies taken now: what far sees is the record as of this step, whatever happens to it during the flight
52
+ const f = { s, items, snaps: items.map(([e, dt]) => [structuredClone(e.rec), dt]), results: null };
53
+ for (const [e, dt] of items) e.flight = { f, from: e.last - dt };
54
+ const t = engine.commits.request({ lag: FAR_LAG, apply: r => this.apply(f, r), fallback: () => f.snaps.map((_, i) => this.compute(f, i)) });
55
+ // a failed worker run is ignored: the fallback runs the same code here at the due step and throws the same (clear) error
56
+ if (T.size) T.pureLane().run({ call: s.key, items: f.snaps }).then(r => { f.results = r.map(x => x.args[0]); t.resolve(f.results); }, () => {});
57
+ }
58
+ /** Run one worker kind's far here and now (engine.skip), written back the same way as a landed result */
59
+ now(e, s, dt) {
60
+ const f = { s, snaps: [[structuredClone(e.rec), dt]] };
61
+ this.write(e, f, this.compute(f, 0), 0);
62
+ }
63
+ compute(f, i) { const [rec, dt] = f.snaps[i], c = structuredClone(rec); f.s.pure.call(c, dt); return c; }
64
+ apply(f, results) {
65
+ f.items.forEach(([e], i) => { if (e.flight?.f === f) this.write(e, f, results[i], i); });
66
+ }
67
+ /** Near needs this record now: land its result early (worker result if it arrived, else computed here; the value is the same) */
68
+ land(e) {
69
+ const f = e.flight.f, i = f.items.findIndex(x => x[0] === e);
70
+ this.write(e, f, f.results ? f.results[i] : this.compute(f, i), i);
71
+ }
72
+ write(e, f, res, i) {
73
+ e.flight = null;
74
+ if (e.dead) return;
75
+ const rec = e.rec, snap = f.snaps[i][0];
76
+ for (const k in res) {
77
+ if (k === 'id' || k === 'kind' || same(res[k], snap[k])) continue;
78
+ if (k === 'position') this.sim.w.move(rec.id, res.position); else rec[k] = res[k];
79
+ }
80
+ for (const k in snap) if (!(k in res)) delete rec[k];
81
+ }
82
+ }
@@ -0,0 +1,115 @@
1
+ // Internal half of World (World.js copies these onto World.prototype; a mixin, not a base class, so the generated World.d.ts needs no import of this file): state tables, the hooks the engine calls, and the helpers other world files use.
2
+ // Split by responsibility: World.js keeps the public API and its documentation; nothing here is public.
3
+ import { DEV, fmt } from '../../core/validate.js';
4
+ import { STRINGS as RECORD_STRINGS, restRecord, orphanIds } from './records.js';
5
+ import { saveWorld, loadWorld, renamePending } from './persist.js';
6
+ import { Cells } from './cells.js';
7
+ import { centerOf } from './pos.js';
8
+
9
+ export function initState(w, cellSize) {
10
+ /** @internal */ w._anchor = null;
11
+ /** @internal id of the record whose model build() is running right now (sync part only), else null */ w._building = null;
12
+ /** @internal */ w._zone = null;
13
+ /** @internal */ w._cells = new Cells(cellSize);
14
+ /** @internal */ w._seq = 0;
15
+ /** @internal */ w._base = new Map(); // initial record id → original content (saves diff against it; kept after removal so old saves can restore)
16
+ /** @internal */ w._removed = new Set(); // ids of removed initial records (tombstones, saved)
17
+ /** @internal */ w._pending = new Map(); // changes from a save whose content has not been added yet: id → { set: changed fields, del: removed fields }
18
+ /** @internal */ w._unsaved = new Set(); // ids added with save: false
19
+ /** @internal */ w._origin = new Map(); // dev only: id → where the record comes from (world.origin); empty in release builds
20
+ // content/renames.js: old authored id → new id (null = deleted on purpose); applied to every save this world loads
21
+ /** @internal */ w._renames = new Map();
22
+ // levels whose nodes were registered (setOrigin { level }): `<level>/<node>` diffs count as orphans only after that
23
+ /** @internal */ w._levels = new Set();
24
+ }
25
+
26
+ class Internals {
27
+ /** @internal The record whose build() is running (sync part): interactions.add inside it takes this id when none is given */
28
+ get buildingId() { return this._building; }
29
+ /**
30
+ * @internal An authored record's resting place, known only once its model is built (a physics body lifted to sit on the ground): when the record is
31
+ * still where its content put it (within tolerance meters), the content position moves there too, so the record stays out of the save. A record that was
32
+ * moved is left alone.
33
+ */
34
+ rest(id, position, tolerance = 0) { restRecord(this, id, position, tolerance); }
35
+ /** @internal Content / level loaders say where an authored record was defined (dev only; merged into what is there) */
36
+ setOrigin(id, info) {
37
+ if (info?.level != null) this._levels.add(info.level);
38
+ if (DEV) this._origin.set(id, { ...this._origin.get(id), ...info });
39
+ }
40
+ /**
41
+ * @internal Saved changes or removals whose id no authored entry produces any more (content renamed or deleted it). They are kept in the save
42
+ * (a reverted content change gets its data back); a `<level>/<node>` id counts only after that level has been shown.
43
+ * @returns {string[]}
44
+ */
45
+ orphans() { return orphanIds(this); }
46
+ /**
47
+ * @internal content/renames.js (content.load calls this before it adds placements): saved changes, removals and owed time under an old authored id
48
+ * move to the new id; null drops them. Kept, so a save loaded later is renamed the same way (loadWorld). Returns the old ids that could not move,
49
+ * each with the reason (still authored, or the new id has saved data of its own), for the dev warning.
50
+ * @param {Record<string, string | null>} map
51
+ * @returns {string[]}
52
+ */
53
+ renameIds(map) {
54
+ for (const [a, b] of Object.entries(map)) this._renames.set(a, b);
55
+ return renamePending(this);
56
+ }
57
+ /** @internal Whether id is an authored (content) record, removed or not: thing.remove's undo puts such a record back without a world.add */
58
+ authored(id) { return this._base.has(id); }
59
+ /**
60
+ * @internal Undo of a removal (thing.remove): an authored record loses its tombstone and comes back as it was; a runtime record is added again.
61
+ * @param {any} rec the record as it was (a copy) @param {boolean} authored
62
+ */
63
+ restore(rec, authored) {
64
+ if (!authored) { const { id, kind, ...rest } = rec; this.add({ id, kind, ...rest }); return; }
65
+ this._removed.delete(rec.id); this._insert(structuredClone(rec), true);
66
+ }
67
+ /** @internal whether some code already called present(kind) (content templates refuse to take over a kind with its own presenter) @param {string} kind */
68
+ presents(kind) { return this._v.presenters.has(kind); }
69
+ /** @internal The gameplay anchor in effect (anchor, else engine.actorPosition(), else engine.focus) */
70
+ get center() { return this.anchor ?? this.engine.actorPosition() ?? this.engine.focus; }
71
+
72
+ // ---------- called by the engine ----------
73
+ /** Fixed step: read this step's simulation center → simulate → copy model-driven positions back to the data @internal */
74
+ fixed(dt) {
75
+ const a = this.center;
76
+ this._s.fixed(dt, centerOf(a));
77
+ this._v.pull();
78
+ }
79
+ /** Every frame: build / tear down models by stream sources, sync positions, call update @internal */
80
+ frame(dt) { this._v.frame(dt); }
81
+ /** Save content (rules at the top of persist.js) @internal */
82
+ toJSON() { return saveWorld(this); }
83
+ /** Load a save, then emit world:load @internal */
84
+ fromJSON(d) { loadWorld(this, d); }
85
+ /** Tear down all models (called by engine.dispose) @internal */
86
+ dispose() { this._v.clear(); }
87
+
88
+ /** @internal */
89
+ _inZone(rec) { return this.zone == null || rec.zone == null || rec.zone === this.zone; }
90
+ /** @internal */
91
+ _need(id, fn) { const r = this.records.get(id); if (!r) throw new Error(fmt(RECORD_STRINGS.noId, { fn, id })); return r; }
92
+ /** Copy the original initial record and apply the changes from the save @internal */
93
+ _fromBase(id, base) {
94
+ const rec = structuredClone(base), d = this._pending.get(id);
95
+ if (d) { this._pending.delete(id); Object.assign(rec, d.set); for (const k of d.del ?? []) delete rec[k]; }
96
+ return rec;
97
+ }
98
+ /** Put into the data table, cells and simulation table @internal */
99
+ _insert(rec, emit = false) {
100
+ this.records.set(rec.id, rec);
101
+ this._index(rec);
102
+ this._s.added(rec);
103
+ if (emit) this.engine.events.emit('world:add', { id: rec.id, kind: rec.kind });
104
+ return rec;
105
+ }
106
+ /** Position changed: update the cell; if it crosses the build / teardown distance, make the presentation layer refresh next frame @internal */
107
+ _index(rec) { this._cells.put(rec); this._v.track(rec); }
108
+ }
109
+
110
+ /** Copy the internal methods onto World.prototype (getters stay getters) */
111
+ export function mixInternals(World) {
112
+ const d = Object.getOwnPropertyDescriptors(Internals.prototype);
113
+ delete d.constructor;
114
+ Object.defineProperties(World.prototype, d);
115
+ }
@@ -0,0 +1,44 @@
1
+ // What the world module tells core about its records: engine._things.addSource(...) at install. Core knows only this shape (has / get / set / remove /
2
+ // explain, plus idOf / view / ids / restore for the model link and undo), never the World class, so engine.thing, thing.set and the shared id space
3
+ // work the same whether or not this module is installed.
4
+ const SAY = {
5
+ defined: 'defined in {at}: edit it there to change the level for good (ops change only this session), or: npx threedog content set {id} <field> <json>',
6
+ node: 'level {level} node {node}: change it in the level model',
7
+ spawned: 'made during play{op}: not in content, saved whole',
8
+ };
9
+ const NOT_FIELDS = new Set(['id', 'kind', 'position', 'zone', 'floor', 'offsetY']);
10
+
11
+ /** The record as a thing to change: id, kind, template, where it was authored (definedAt / level node / spawned by which op), model and its fields */
12
+ function describeRecord(w, id) {
13
+ const rec = w.get(id); if (!rec) return null;
14
+ const o = w.origin(id) ?? {}, fields = {};
15
+ for (const k in rec) if (!NOT_FIELDS.has(k)) fields[k] = rec[k];
16
+ const how = o.definedAt ? SAY.defined.replace('{at}', o.definedAt).replace('{id}', id) : o.node ? SAY.node.replace('{level}', o.level).replace('{node}', o.node)
17
+ : o.spawned ? SAY.spawned.replace('{op}', o.spawned.op ? ` by op ${o.spawned.op} at tick ${o.spawned.tick}` : '') : null;
18
+ return { id, kind: rec.kind, template: o.template ?? null, definedAt: o.definedAt ?? null, ...(o.templateAt && { templateAt: o.templateAt }),
19
+ ...(o.node && { level: o.level, node: o.node }), ...(o.spawned && { spawned: o.spawned }), model: o.model ?? rec.model ?? null,
20
+ position: { ...rec.position }, fields, edit: how };
21
+ }
22
+
23
+ /** @param {any} w the World @returns the record source for engine._things.addSource @internal */
24
+ export function recordSource(w) {
25
+ return {
26
+ name: 'world',
27
+ has: id => !!w.get(id),
28
+ get: id => w.get(id),
29
+ ids: () => w.query({}).map(r => r.id),
30
+ idOf: obj => w.idOf(obj),
31
+ view: id => w.view(id),
32
+ explain: id => describeRecord(w, id),
33
+ /** Change a record (position / rot / scale); returns the values it replaced (set(id, thatValue) is the undo) */
34
+ set(id, patch) {
35
+ // rot / scale are the template instance fields (templates.js); a record without them reads as rot 0, scale 1
36
+ const rec = w.get(id), before = { position: { ...rec.position }, ...(patch.rot != null && { rot: rec.rot ?? 0 }), ...(patch.scale != null && { scale: rec.scale ?? 1 }) };
37
+ w.set(id, patch);
38
+ return before;
39
+ },
40
+ /** Remove a record; returns what restore needs */
41
+ remove(id) { const rec = structuredClone(w.get(id)), authored = w.authored(id); w.remove(id); return { rec, authored }; },
42
+ restore: ({ rec, authored }) => w.restore(rec, authored),
43
+ };
44
+ }
@@ -0,0 +1,182 @@
1
+ // Presentation layer of world: build / tear down models by distance from the full-detail stream sources (engine.stream sources: player, camera focus, teleport
2
+ // destination...),
3
+ // sync positions, decide ready. The public API is in World.js (present / view / ready / refresh); this is the implementation.
4
+ // It only decides "which records have a model" and never affects gameplay: simulation follows the gameplay anchor (sim.js), and how fast models are built, or
5
+ // whether they are built, does not change simulation results.
6
+ import { reportError } from '../../core/errors.js';
7
+ import { DEV, checkOptionsArg, warnOnce } from '../../core/validate.js';
8
+
9
+ const STRINGS = {
10
+ noBuild: 'world.present(\'{kind}\', { build }) needs build: rec => three.js object',
11
+ nullBuild: 'build of world.present(\'{kind}\') returned {got} (record {id}): build must return a three.js object; to have no model for this record for now, write show: rec => false',
12
+ failed: 'build of world.present(\'{kind}\')',
13
+ disposeFailed: 'dispose(rec, obj) callback of world.present(\'{kind}\')',
14
+ disposeArgs: 'dispose of world.present(\'{kind}\') declares only one parameter: the parameters are (rec, obj), record first and model second. Example: dispose: (rec, obj) => engine.remove(obj)',
15
+ positionFrom: 'positionFrom of world.present(\'{kind}\') must be \'record\' (the model follows the record, default) or \'view\' (the model moves itself, the record follows the model)',
16
+ };
17
+ /** Dev: code wrote the position of a model world places every frame (positionFrom 'record'): the write is lost next frame */
18
+ const moved = (e, id) => e.thingConflict(`world model of '${id}' was moved by code outside its present update; world puts it back on the record every frame (positionFrom 'record'). Move the record instead: ops.run('thing.set', { id: '${id}', position }) or world.move, or present it with positionFrom: 'view'`);
19
+ const say = (s, kind, id = '', got = '') => s.replace(/\{kind\}/g, kind).replace('{id}', id).replace('{got}', got);
20
+ const OPTS = ['build', 'update', 'dispose', 'show', 'radius', 'positionFrom'];
21
+ const FULL = { detail: 'full' };
22
+
23
+ /** @internal */
24
+ export class Views {
25
+ /** @param {import('./World.js').World} w */
26
+ constructor(w) {
27
+ this.w = w;
28
+ this.presenters = new Map(); // kind → { build, update, dispose, radius, positionFrom, show }
29
+ this.map = new Map(); // id → { obj, pending, p }; obj null and not pending = build failed (retried when you leave and come back)
30
+ this.byObj = new WeakMap(); // built model → record id (world.idOf: a ray hit on the model names its record)
31
+ this.radii = new Map(); // presentation → radius at the last refresh (used to decide whether a moving record needs another refresh)
32
+ this.moving = new Set(); // ids of built positionFrom 'view' models (each fixed step scans only these, not thousands of props)
33
+ // models need to be re-decided (a record crossed the build / teardown distance, zone changed, new registration, last frame unfinished)
34
+ this.dirty = true;
35
+ this.memo = {}; // stream sources at the last refresh (memo.at: [{ x, z, s }])
36
+ this.waiters = [];
37
+ }
38
+
39
+ present(kind, opts) {
40
+ if (typeof opts?.build !== 'function') throw new Error(say(STRINGS.noBuild, kind));
41
+ if (DEV) {
42
+ checkOptionsArg(`world.present('${kind}', opts)`, opts, OPTS);
43
+ if (opts.positionFrom != null && opts.positionFrom !== 'record' && opts.positionFrom !== 'view') throw new Error(say(STRINGS.positionFrom, kind));
44
+ if (opts.dispose?.length === 1) warnOnce(say(STRINGS.disposeArgs, kind));
45
+ }
46
+ const old = this.presenters.get(kind);
47
+ if (old) this.forget(old); // registering the same kind again: tear down old models with the old dispose, rebuild with the new one
48
+ const p = { positionFrom: 'record', ...opts };
49
+ this.presenters.set(kind, p);
50
+ this.dirty = true;
51
+ return { remove: () => { if (this.presenters.get(kind) !== p) return; this.presenters.delete(kind); this.forget(p); } };
52
+ }
53
+ forget(p) { for (const [id, v] of this.map) if (v.p === p) this.unview(id); }
54
+ /**
55
+ * Distance for building models: a number, or a function (asked at each refresh; write it as a function for distances that change at runtime, like terrain
56
+ * view distance)
57
+ */
58
+ radius(p) { const r = p.radius; return typeof r === 'function' ? r() : r ?? this.w.radius; }
59
+ /** A source was added / removed or moved more than min(cell / 4, 8 m) since the last refresh: refresh (that is the error of "build within radius") */
60
+ moved() { return this.w.engine.stream.changed(this.memo, Math.min(this.w._cells.size / 4, 8), FULL, false); }
61
+ /** Radius of presentation p for source s (a source radius caps it) */
62
+ reach(p, s) { return s.reach(this.radii.get(p) ?? this.radius(p)); }
63
+ /** Squared distance from rec to the nearest source of the last refresh, measured in units of each source's reach (≤ 1 = inside) */
64
+ near(rec, p) {
65
+ let best = Infinity;
66
+ for (const a of this.memo.at ?? []) {
67
+ const r = this.reach(p, a.s), dx = rec.position.x - a.x, dz = rec.position.z - a.z;
68
+ best = Math.min(best, (dx * dx + dz * dz) / (r * r));
69
+ }
70
+ return best;
71
+ }
72
+ /**
73
+ * A record moved: if it crossed the build / teardown distance (zone changed, or show changed via set) make the next frame refresh. Called on every move,
74
+ * costs a few multiplications
75
+ */
76
+ track(rec) {
77
+ if (this.dirty) return;
78
+ const p = this.presenters.get(rec.kind); if (!p) return;
79
+ const k = this.near(rec, p);
80
+ if (this.map.has(rec.id)) { if (k > 1.5625 && !(p.positionFrom === 'view' && this.w._s.isNear(rec.id))) this.dirty = true; }
81
+ else if (k <= 1 && (!p.show || p.show(rec))) this.dirty = true;
82
+ }
83
+
84
+ get ready() {
85
+ if (!this.presenters.size) return true;
86
+ if (this.dirty || this.moved()) return false; // not yet refreshed for the current stream sources
87
+ for (const v of this.map.values()) if (v.pending) return false;
88
+ return true;
89
+ }
90
+ get whenReady() { return this.ready ? Promise.resolve() : new Promise(r => this.waiters.push(r)); }
91
+
92
+ frame(dt) {
93
+ const W = this.w;
94
+ // build at most buildPerFrame per frame, the rest next frame (not limited while loading: the loading screen is waiting anyway)
95
+ if (this.presenters.size && (this.dirty || this.moved())) this.refresh(W.engine.ready ? W.buildPerFrame : Infinity);
96
+ for (const [id, v] of this.map) {
97
+ if (!v.obj) continue;
98
+ const rec = W.records.get(id), o = v.obj.position;
99
+ if (v.p.positionFrom !== 'view') {
100
+ if (DEV && v.lx !== undefined && (o.x !== v.lx || o.y !== v.ly || o.z !== v.lz)) moved(W.engine, id);
101
+ o.copy(rec.position); // 'view' position is copied back to the record at the end of the fixed step (pull)
102
+ }
103
+ v.p.update?.(rec, v.obj, dt);
104
+ if (DEV) { v.lx = o.x; v.ly = o.y; v.lz = o.z; } // after update: present's own update may move the model, other code may not
105
+ }
106
+ this.wake();
107
+ }
108
+ /** Resolve whenReady waiters once ready. Also called when an async build settles: a paused engine (tests, engine.step) runs no frame,
109
+ * so waiting only in frame() would never resolve; builds still owed (per-frame limit) are done right here, like world.refresh() */
110
+ wake() {
111
+ if (!this.waiters.length) return;
112
+ if (this.presenters.size && (this.dirty || this.moved()) && this.w.engine.paused) this.refresh();
113
+ if (this.ready) for (const r of this.waiters.splice(0)) r();
114
+ }
115
+ /** End of fixed step: models that move themselves (positionFrom 'view') copy their position back to the record, so later gameplay in this step and saves read this step's result.
116
+ * Ones not simulated near this step (focus and gameplay anchor apart: model still exists but counts as far) go the other way: the record rules, the model follows */
117
+ pull() {
118
+ const W = this.w, S = W._s;
119
+ for (const id of this.moving) {
120
+ const rec = W.records.get(id), o = this.map.get(id).obj.position;
121
+ if (S.sims.get(rec.kind)?.near && !S.isNear(id)) { o.copy(rec.position); continue; }
122
+ rec.position.x = o.x; rec.position.y = o.y; rec.position.z = o.z; W._index(rec);
123
+ }
124
+ }
125
+ /** Build models that should exist near any full-detail source (at most limit);
126
+ * tear down those far from all of them, in another zone, or that show says no to */
127
+ refresh(limit = Infinity) {
128
+ const W = this.w, L = W.engine.stream;
129
+ L.changed(this.memo, 0, FULL); this.dirty = false;
130
+ let maxR = W.radius, n = 0;
131
+ for (const p of this.presenters.values()) { const r = this.radius(p); this.radii.set(p, r); if (r > maxR) maxR = r; }
132
+ for (const a of this.memo.at) W._cells.forEach(a, a.s.reach(maxR), (rec, d2) => {
133
+ const p = this.presenters.get(rec.kind);
134
+ if (!p || this.map.has(rec.id) || !W._inZone(rec)) return;
135
+ const r = this.reach(p, a.s);
136
+ if (d2 > r * r || p.show && !p.show(rec)) return;
137
+ if (n >= limit) { this.dirty = true; return; } // enough built this frame: continue next frame, ready still waits
138
+ n++; this.build(rec, p);
139
+ });
140
+ for (const [id, v] of this.map) { // teardown distance is widened to 1.25×: walking back and forth on the boundary does not build and tear down repeatedly
141
+ const rec = W.records.get(id);
142
+ const keep = this.near(rec, v.p) <= 1.5625 || v.p.positionFrom === 'view' && W._s.isNear(id); // keep models that near simulation needs
143
+ if (!keep || !W._inZone(rec) || v.p.show && !v.p.show(rec)) this.unview(id);
144
+ }
145
+ }
146
+ /**
147
+ * Build one model. Sync throw, async failure, or a non-object result: reported once (once per kind) and marked "stop waiting", ready is not blocked; retried
148
+ * when you leave and come back
149
+ */
150
+ build(rec, p) {
151
+ const v = { obj: null, pending: true, p }, e = this.w.engine;
152
+ this.map.set(rec.id, v);
153
+ const fail = err => { if (this.map.get(rec.id) === v) v.pending = false; reportError(e, err, say(STRINGS.failed, rec.kind), { soft: true }); };
154
+ const done = obj => {
155
+ if (!obj?.isObject3D) throw new Error(say(STRINGS.nullBuild, rec.kind, rec.id, String(obj)));
156
+ if (this.map.get(rec.id) !== v) { this.drop(rec, p, obj); return; } // no longer wanted by the time it was built
157
+ v.obj = obj; v.pending = false; this.byObj.set(obj, rec.id);
158
+ if (p.positionFrom === 'view') this.moving.add(rec.id);
159
+ obj.position.copy(rec.position);
160
+ if (!obj.parent) e.add(obj, { owner: 'world', id: rec.id }); // the registry knows it as the record (engine.thing / hit.thing)
161
+ };
162
+ let out;
163
+ this.w._building = rec.id; // interactions.add inside build takes this as its stable id (the interaction.use op names it)
164
+ try { out = p.build(rec); if (typeof out?.then !== 'function') { done(out); return; } } catch (err) { fail(err); return; } finally { this.w._building = null; }
165
+ out.then(done).catch(fail).then(() => this.wake()); // an async build settled: waiters of a paused engine resolve here (see wake)
166
+ }
167
+ unview(id) {
168
+ const v = this.map.get(id); if (!v) return;
169
+ this.map.delete(id); this.moving.delete(id);
170
+ if (v.obj) this.byObj.delete(v.obj);
171
+ if (v.obj) this.drop(this.w.records.get(id), v.p, v.obj);
172
+ }
173
+ /**
174
+ * Tear down a model. A dispose error is reported on its own (naming the kind's dispose) and not thrown outward — otherwise loading a save would report it as
175
+ * "world load failed"
176
+ */
177
+ drop(rec, p, obj) {
178
+ if (!p.dispose) { this.w.engine.remove(obj); return; }
179
+ try { p.dispose(rec, obj); } catch (err) { reportError(this.w.engine, err, say(STRINGS.disposeFailed, rec.kind), { soft: true }); }
180
+ }
181
+ clear() { for (const id of [...this.map.keys()]) this.unview(id); this.dirty = true; }
182
+ }