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,294 @@
1
+ // Background music module: tracks on the "music" bus, cross-faded; chosen by a small rule list; optional stems that follow an intensity value.
2
+ //
3
+ // engine.use(music({ tracks: {
4
+ // explore: '/music/explore.ogg',
5
+ // combat: { url: '/music/combat-base.ogg', stems: [{ url: '/music/combat-drums.ogg', at: [0.3, 0.7] }, { url: '/music/combat-brass.ogg', at: [0.7, 1] }] },
6
+ // win: { url: '/music/win.ogg', loop: false },
7
+ // } }));
8
+ // engine.music.play('explore') // 2 s cross-fade by default; play('explore', { fade: 0 }) cuts at once
9
+ // engine.music.intensity = 0.8 // stems fade in as it rises through their `at` range (0-1)
10
+ // engine.music.stinger('win') // a short piece over the music; the music dips under it
11
+ // engine.music.stop(3) // fade out over 3 s
12
+ //
13
+ // Rules (checked about once a second; the highest-priority match wins; a new result must hold `hold` seconds before switching):
14
+ // engine.music.rules([
15
+ // { track: 'combat', when: { state: 'combat' }, priority: 10 }, // engine.audio.state = 'combat' (the game sets it)
16
+ // { track: 'cave', when: { zone: 'cave' }, priority: 5 }, // condition keys: state, zone (triggers), time (sky), weather
17
+ // { track: 'night', when: { time: 'night' }, priority: 2 },
18
+ // { track: 'explore', priority: 0 }, // no when = always: the fallback
19
+ // { track: null, when: e => e.dialogue?.active, priority: 20 }, // a function works too; track: null = silence
20
+ // ], { hold: 2, fade: 3 });
21
+ // engine.music.override('boss') // pick by hand, pausing the rules; override(null) hands control back
22
+ // Tracks and rules may also come from createEngine({ audio: { music: { tracks, rules, fade } } }). Ducking is engine.audio.duck({ music: 0.3 });
23
+ // music({ duckDialogue: 0.35 }) does that during dialogue (off by default: whether dialogue lowers the music is the game's call). Before the first click play()
24
+ // is remembered and starts once audio is usable.
25
+ //
26
+ // Events: music:change { from, to }, music:stinger { name }
27
+
28
+ import { checkOptions, warnMissing, warnOnce } from '../../core/validate.js';
29
+ import { readParams, keysSchema } from '../../core/params.js';
30
+ import { pickTrack, evaluateRules } from './rules.js';
31
+ /** @import { ModuleDef } from '../../types/core' */
32
+ /** @import { MusicTrack, MusicRule, MusicOptions } from '../../types/gameplay' */
33
+
34
+ const DEFAULTS = { fade: 2, duckDialogue: false, duckFade: 0.4, hold: 2, interval: 1 };
35
+ const RULE_KEYS = ['track', 'when', 'priority'];
36
+ const stemLevel = (st, x) => { const [lo, hi] = st.at ?? [0, 1]; return (st.volume ?? 1) * Math.min(1, Math.max(0, hi > lo ? (x - lo) / (hi - lo) : +(x >= lo))); };
37
+
38
+ /** engine.music */
39
+ export class Music {
40
+ /** @internal */
41
+ constructor(engine, opts = {}) {
42
+ /** @internal */ this.engine = engine;
43
+ const st = engine.audio.config.music;
44
+ /** @internal */ this.opts = { ...DEFAULTS, ...st, ...opts };
45
+ /** Registered tracks @type {Map<string, { url?: string, volume?: number, loop?: boolean, stems?: { url: string, at?: number[], volume?: number }[] }>} */
46
+ this.tracks = new Map();
47
+ this.addTracks(st.tracks || {});
48
+ this.addTracks(opts.tracks || {});
49
+ /** Name of the track playing (or wanted) @type {string | null} */
50
+ this.current = null;
51
+ /** 0-1: how loud each stem of the playing track is (see the stems `at` range); fades in about half a second @type {number} */
52
+ this.intensity = 0;
53
+ /** @internal */ this.playing = null; // { name, gain, srcs, stems: [{ def, gain }], token }
54
+ /** @internal */ this.pending = null; // { name, fade } remembered while audio is still locked
55
+ /** @internal */ this._token = 0;
56
+ /** @internal */ this._rules = null;
57
+ /** @internal */ this._ruleOpts = {};
58
+ /** @internal */ this._candidate = undefined;
59
+ /** @internal */ this._candidateT = 0;
60
+ /** @internal */ this._ruleT = 0;
61
+ /** @internal */ this._override = undefined;
62
+ /** @internal */ this._duck = null;
63
+ /** @internal */ this._lastIntensity = -1;
64
+ /** @internal the game's own rules (rules() / options) and each content pack's: the rule list is both, the game's first */
65
+ this._base = null;
66
+ /** @internal */ this._packRules = new Map();
67
+ const rules = opts.rules ?? st.rules;
68
+ if (rules) this.rules(rules);
69
+ // content packs (audio: { music: { tracks, rules } }): tracks by name, rules appended; a pack loaded again replaces its own rules
70
+ // (createEngine({ audio: { music: { tracks } } }) names are checked by engine.audio.contentPack; music({ tracks }) ones here)
71
+ const code = new Set(Object.keys(opts.tracks || {}));
72
+ engine.audio.onPack((key, pack) => {
73
+ for (const name of Object.keys(pack.music.tracks)) if (code.has(name)) throw new Error(`music track '${name}' is defined both in music({ tracks }) `
74
+ + `and in content ${pack.from}. One name, one place: keep it in one of them [audio-name-clash]`);
75
+ this.addTracks(pack.music.tracks);
76
+ this._packRules.set(key, { from: pack.from, rules: pack.music.rules });
77
+ this._setRules();
78
+ });
79
+ // Duck the music bus during dialogue only when the game asks: music({ duckDialogue: 0.35 }) (0-1 multiplier; false / 1 = no duck)
80
+ if (this.opts.duckDialogue !== false && this.opts.duckDialogue != null && this.opts.duckDialogue < 1) {
81
+ engine.events.on('dialogue:start', () => { this._duck?.remove(); this._duck = engine.audio.duck({ music: this.opts.duckDialogue }, { fade: this.opts.duckFade }); });
82
+ engine.events.on('dialogue:end', () => { this._duck?.remove(this.opts.duckFade * 2); this._duck = null; });
83
+ }
84
+ }
85
+
86
+ /**
87
+ * Register tracks: { name: url | { url, volume, loop, stems } }
88
+ * @param {Record<string, MusicTrack>} map
89
+ * @returns {this}
90
+ */
91
+ addTracks(map) {
92
+ for (const [name, v] of Object.entries(map)) {
93
+ const tr = typeof v === 'string' ? { url: v } : { ...v };
94
+ if (tr.url == null && !tr.stems?.length) throw new TypeError(`music track '${name}' needs url or stems, e.g. { url: '/music/${name}.ogg' }`);
95
+ this.tracks.set(name, tr);
96
+ }
97
+ return this;
98
+ }
99
+ /**
100
+ * Whether audio is usable
101
+ * @returns {boolean}
102
+ */
103
+ get ready() { return this.engine.audio.running; }
104
+ /** Auto track rules are in use */
105
+ get ruled() { return !!this._rules; }
106
+ /**
107
+ * State (for debugging and tests): gain is the playing track's fade level, stems each stem's level
108
+ * @returns {{ current: string | null, playing: string | null, pending: string | null, override: string | null, intensity: number, gain: number, stems:
109
+ * number[] }}
110
+ */
111
+ get state() {
112
+ const p = this.playing;
113
+ return { current: this.current, playing: p?.name ?? null, pending: this.pending?.name ?? null, override: this._override ?? null, intensity: this.intensity,
114
+ gain: p ? +p.gain.gain.value.toFixed(3) : 0, stems: p ? p.stems.map(s => +s.gain.gain.value.toFixed(3)) : [] };
115
+ }
116
+
117
+ // ---------- playback ----------
118
+ /**
119
+ * Play a track (cross-fade over fade seconds). Does nothing if it is already playing; force replays it
120
+ * @param {string | null} name
121
+ * @param {{ fade?: number, force?: boolean }} [opts]
122
+ * @returns {void}
123
+ */
124
+ play(name, opts = {}) {
125
+ const { fade = this.opts.fade, force = false } = opts;
126
+ if (name == null) return this.stop(fade);
127
+ const tr = this.tracks.get(name);
128
+ if (!tr) { warnMissing('track', name, [...this.tracks.keys()]); return; }
129
+ if (!force && this.current === name && (this.playing?.name === name || this.pending?.name === name)) return;
130
+ const from = this.current;
131
+ this.current = name;
132
+ if (from !== name) this.engine.events.emit('music:change', { from, to: name });
133
+ const audio = this.engine.audio;
134
+ if (!audio.running || !this._ensureOutput()) { this.pending = { name, fade }; return; } // cannot sound yet: remember it
135
+ this.pending = null;
136
+ this._fadeOutCurrent(fade);
137
+ const token = ++this._token;
138
+ const ctx = audio.ctx, g = ctx.createGain(), vol = tr.volume ?? 1;
139
+ g.gain.setValueAtTime(fade > 0 ? 0 : vol, ctx.currentTime);
140
+ g.connect(this.output);
141
+ const stems = (tr.stems || []).map(def => { const sg = ctx.createGain(); sg.gain.value = stemLevel(def, this.intensity); sg.connect(g); return { def, gain: sg }; });
142
+ const slot = { name, gain: g, srcs: [], stems, token };
143
+ this.playing = slot;
144
+ const parts = [...(tr.url != null ? [{ url: tr.url, out: g }] : []), ...stems.map(s => ({ url: s.def.url, out: s.gain }))];
145
+ // all parts decode first, then start on the same audio-clock instant: stems only line up if they begin together
146
+ Promise.all(parts.map(p => audio.load(p.url))).then(bufs => {
147
+ if (this._token !== token || this.playing !== slot) { g.disconnect(); return; } // another track was chosen while loading
148
+ const t = ctx.currentTime + 0.02, loop = tr.loop !== false;
149
+ bufs.forEach((buf, i) => { const src = ctx.createBufferSource(); src.buffer = buf; src.loop = loop; src.connect(parts[i].out); src.start(t); slot.srcs.push(src); });
150
+ if (fade > 0) { g.gain.setValueAtTime(0, t); g.gain.linearRampToValueAtTime(vol, t + fade); }
151
+ if (!loop && slot.srcs[0]) slot.srcs[0].onended = () => { if (this.playing === slot) { this.playing = null; this.current = null; this.engine.events.emit('music:change', { from: name, to: null }); } };
152
+ }).catch(err => { warnOnce(err.message); if (this.playing === slot) this.playing = null; }); // a missing file is said once; the game goes on silent
153
+ }
154
+ /**
155
+ * Fade out and stop (fade over fade seconds)
156
+ * @param {number} [fade]
157
+ * @returns {void}
158
+ */
159
+ stop(fade = this.opts.fade) {
160
+ const from = this.current;
161
+ this.pending = null; this.current = null; this._token++;
162
+ this._fadeOutCurrent(fade);
163
+ if (from) this.engine.events.emit('music:change', { from, to: null });
164
+ }
165
+ /** @internal */
166
+ _fadeOutCurrent(fade) {
167
+ const p = this.playing; this.playing = null;
168
+ if (!p) return;
169
+ const ctx = this.engine.audio.ctx, t = ctx.currentTime, f = Math.max(0.02, fade);
170
+ p.gain.gain.cancelScheduledValues(t);
171
+ p.gain.gain.setValueAtTime(p.gain.gain.value, t);
172
+ p.gain.gain.linearRampToValueAtTime(0, t + f);
173
+ for (const s of p.srcs) { try { s.stop(t + f + 0.05); } catch { /* already stopped */ } }
174
+ if (p.srcs[0]) p.srcs[0].onended = () => p.gain.disconnect(); // free the nodes once the fade has really ended
175
+ }
176
+ /** @internal the background bed: tracks connect here, then to the music bus; a stinger dips this node, never the bus (it plays on the bus itself) */
177
+ _ensureOutput() {
178
+ const a = this.engine.audio;
179
+ if (this.output) return true;
180
+ if (!a.ctx) return false;
181
+ /** @internal */ this.output = a.ctx.createGain();
182
+ this.output.connect(a.buses.music);
183
+ return true;
184
+ }
185
+
186
+ /**
187
+ * Layer a one-shot piece (victory, discovered place...); the background dips to `duck` while it plays
188
+ * @param {string} name
189
+ * @param {{ duck?: number, volume?: number }} [opts]
190
+ * @returns {void}
191
+ */
192
+ stinger(name, opts = {}) {
193
+ const { duck = 0.5, volume } = opts;
194
+ const tr = this.tracks.get(name);
195
+ if (!tr) { warnMissing('track', name, [...this.tracks.keys()]); return; }
196
+ const a = this.engine.audio;
197
+ if (!a.running || !this._ensureOutput()) return; // a one-shot piece is not worth queueing
198
+ this.engine.events.emit('music:stinger', { name });
199
+ a.play(tr.url, { bus: 'music', volume: volume ?? tr.volume ?? 1 }).then(h => {
200
+ const d = h.source.buffer?.duration || 1, g = this.output.gain, t = a.ctx.currentTime;
201
+ if (duck >= 1) return;
202
+ // scheduled on the audio clock (no timers): dip, hold for the stinger's length, come back
203
+ g.cancelScheduledValues(t); g.setValueAtTime(g.value, t); g.linearRampToValueAtTime(duck, t + 0.15);
204
+ g.setValueAtTime(duck, t + d); g.linearRampToValueAtTime(1, t + d + 0.3);
205
+ }).catch(err => warnOnce(err.message));
206
+ }
207
+
208
+ // ---------- rules ----------
209
+ /**
210
+ * Set the auto track rules (null turns them off). opts: hold how many seconds a new result must be stable before switching, fade switch fade seconds,
211
+ * interval check interval
212
+ * @param {MusicRule[] | null} list
213
+ * @param {{ hold?: number, fade?: number, interval?: number }} [opts]
214
+ * @returns {this}
215
+ */
216
+ rules(list, opts = {}) {
217
+ checkOptions('music.rules()', opts, ['hold', 'fade', 'interval']);
218
+ this._base = list ? [...list] : null;
219
+ this._ruleOpts = opts;
220
+ this._setRules();
221
+ return this;
222
+ }
223
+ /** @internal the live rule list: the game's rules, then every content pack's (null = no rules at all) */
224
+ _setRules() {
225
+ for (const r of this._base || []) checkOptions('music.rules() rule', r, RULE_KEYS);
226
+ // a pack's rule names its pack in the message, so a typo points at the right data file
227
+ for (const p of this._packRules.values()) for (const r of p.rules) checkOptions(`content ${p.from} audio.music.rules rule`, r, RULE_KEYS);
228
+ const all = [...(this._base || []), ...[...this._packRules.values()].flatMap(p => p.rules)];
229
+ this._rules = all.length || this._base ? all : null;
230
+ this._candidate = undefined; this._candidateT = 0; this._ruleT = 0;
231
+ if (this._rules) this._evaluate(true);
232
+ }
233
+ /**
234
+ * Pick a track by hand, pausing the rules; override(null) hands control back
235
+ * @param {string | null} name
236
+ * @param {{ fade?: number, force?: boolean }} [opts]
237
+ * @returns {void}
238
+ */
239
+ override(name, opts) {
240
+ if (name == null) { this._override = undefined; if (this._rules) this._evaluate(true); return; }
241
+ this._override = name; this.play(name, opts);
242
+ }
243
+ /**
244
+ * Which track the current rules would pick (without switching)
245
+ * @returns {string | null | undefined}
246
+ */
247
+ pick() { return pickTrack(this); }
248
+ /** @internal */
249
+ _evaluate(immediate = false) { return evaluateRules(this, immediate); }
250
+ /**
251
+ * Per-frame update (plays queued tracks, follows intensity, picks tracks by rules; the module calls it every frame)
252
+ * @param {number} dt
253
+ * @returns {void}
254
+ * @internal
255
+ */
256
+ frame(dt) {
257
+ // Once audio is usable, play the queued track
258
+ if (this.pending && this.engine.audio.running) { const p = this.pending; this.pending = null; this.play(p.name, { fade: p.fade, force: true }); }
259
+ if (this.playing?.stems.length && this.intensity !== this._lastIntensity) {
260
+ this._lastIntensity = this.intensity;
261
+ const t = this.engine.audio.ctx.currentTime;
262
+ for (const s of this.playing.stems) s.gain.gain.setTargetAtTime(stemLevel(s.def, this.intensity), t, 0.15);
263
+ }
264
+ if (!this._rules) return;
265
+ const iv = this._ruleOpts.interval ?? this.opts.interval;
266
+ if (this._candidate !== undefined) this._candidateT += dt;
267
+ if ((this._ruleT -= dt) > 0) return;
268
+ this._ruleT = iv;
269
+ this._evaluate(false);
270
+ }
271
+ }
272
+
273
+ const MUSIC_PARAMS = keysSchema(['tracks', 'rules', ...Object.keys(DEFAULTS)], DEFAULTS);
274
+
275
+ /**
276
+ * Background music -> engine.music. tracks; rules; fade: default fade seconds (2); duckDialogue: duck the music bus to this level during dialogue (default
277
+ * false = off; e.g. 0.35); duckFade; hold: rule debounce seconds (2)
278
+ * @param {MusicOptions} [opts]
279
+ * @returns {ModuleDef<Music>}
280
+ * @example engine.use(music({ tracks: { explore: '/music/explore.ogg' }, rules: [{ track: 'explore' }] }));
281
+ */
282
+ export function music(opts = {}) {
283
+ readParams('music', MUSIC_PARAMS, opts);
284
+ let m = null;
285
+ return {
286
+ name: 'music', params: MUSIC_PARAMS, options: opts, label: 'Music', optional: ['dialogue'], stage: 'audio', presentation: true,
287
+ install(e) {
288
+ m = new Music(e, opts);
289
+ e.events.on('session:end', () => m.stop(0)); // a playthrough ends: its music stops at once
290
+ return m;
291
+ },
292
+ frame(dt) { m.frame(dt); },
293
+ };
294
+ }
@@ -0,0 +1,2 @@
1
+ // Background music module entry. See Music.js
2
+ export { music, Music } from './Music.js';
@@ -0,0 +1,28 @@
1
+ // Music rule evaluation (helper of Music.js): which rule wins, and when to switch (debounced by `hold`, immediate on request). `m` is the Music instance.
2
+ import { reportError } from '../../core/errors.js';
3
+
4
+ export function pickTrack(m) {
5
+ if (!m._rules) return undefined;
6
+ let best = null;
7
+ for (const r of m._rules) {
8
+ let ok = false;
9
+ try { ok = m.engine.audio.matches(r.when, m); } catch (err) { reportError(m.engine, err, 'music rule when', { soft: true }); }
10
+ if (ok && (!best || (r.priority ?? 0) > (best.priority ?? 0))) best = r;
11
+ }
12
+ return best ? best.track ?? null : undefined;
13
+ }
14
+
15
+ export function evaluateRules(m, immediate = false) {
16
+ if (m._override !== undefined) return;
17
+ const want = pickTrack(m);
18
+ if (want === undefined) return; // no rule matches: keep the current state
19
+ const cur = m.current ?? null;
20
+ if (want === cur) { m._candidate = undefined; m._candidateT = 0; return; }
21
+ const hold = m._ruleOpts.hold ?? m.opts.hold;
22
+ if (want !== m._candidate) { m._candidate = want; m._candidateT = 0; }
23
+ if (immediate || m._candidateT >= hold) {
24
+ m._candidate = undefined; m._candidateT = 0;
25
+ const fade = m._ruleOpts.fade ?? m.opts.fade;
26
+ want == null ? m.stop(fade) : m.play(want, { fade });
27
+ }
28
+ }
@@ -0,0 +1,156 @@
1
+ // Navmesh (Recast): generated after a level loads; engine.characters.findPath switches to it automatically (falls back to A* when not installed).
2
+ // engine.use(level({ url }), collision(), characters(), nav({ agentRadius: 0.4 }));
3
+ // Once installed, engine.nav offers findPath(from, to) and the native objects navMesh / query / tileCache / crowd / helper.
4
+ // version: +1 on every rebuild / unload. Native objects (crowd agents etc.) are destroyed on rebuild; code that cached them must compare version and re-fetch when it changed (NPCs do this).
5
+ //
6
+ // Where the mesh comes from: the ground API's navGeometry() (optional, { positions, indices }); otherwise staticTrimeshes() (the level's walkable mesh).
7
+ // Walls and obstacles: boxes/circles in the collision module (level col_ boxes, ones added by game collision.addBox) are all synced into TileCache dynamic obstacles,
8
+ // and affected tiles are rebuilt on add/remove, so level walls and later-added obstacles work the same way.
9
+ // Levels only for now (ground with staticTrimeshes); procedural terrain keeps using A*. Recast (about 1MB) is downloaded only at init.
10
+ // The mesh is built once per level: ground edits (engine.terrain.edit, 'ground:change') do not update it, so paths keep following the ground as it was
11
+ // at level load (rebuilding all tiles per edit is too slow to do automatically). A game that reshapes walkable ground at runtime leaves nav out:
12
+ // characters.findPath then uses A*, which resamples the edited area (its rasters are dropped on 'ground:change').
13
+ import { readParams, keysSchema } from '../../core/params.js';
14
+ import { xzOf } from '../../core/math.js';
15
+ import { navSearch } from './navPath.js';
16
+
17
+ const NAV_PARAMS = keysSchema(['cellSize', 'agentRadius', 'agentHeight', 'maxClimb', 'maxSlope', 'tileSize', 'crowd', 'debug']);
18
+
19
+ export function nav(opts = {}) {
20
+ readParams('nav', NAV_PARAMS, opts);
21
+ const { cellSize = 0.3, tileSize = 32, crowd = false, debug = false } = opts;
22
+ // agent size defaults come from engine.movement (filled at install); opts values override them
23
+ let agentRadius, agentHeight, maxClimb, maxSlope;
24
+ let e = null, lib = null, gen = null, pending = false, version = -1;
25
+ const obstacles = new Map(); // collision-module collider → Recast obstacle (big boxes are cut into several pieces)
26
+
27
+ const api = {
28
+ ready: false, agent: null, navMesh: null, query: null, tileCache: null, crowd: null, helper: null,
29
+ /** +1 on rebuild / unload: code that cached native objects such as crowd agents must re-create them when it changes */
30
+ version: 0,
31
+ /**
32
+ * Path [{ x, z }...] (meters) from `from` to `to`; null if not ready or unreachable.
33
+ * Pass { detailed: true } to get { status: 'ok' | 'noPath' | 'notReady', path } instead: tells "navmesh not generated yet" from "generated but unreachable"
34
+ */
35
+ findPath(from, to, o = {}) {
36
+ const res = search(from, to, o);
37
+ return o.detailed ? res : res.path;
38
+ },
39
+ /** @internal +1 whenever the walkable mesh changes (rebuild, unload, obstacle sync): async pathfinding re-sends the mesh to its workers and redoes answers computed on an older one */
40
+ revision: 0,
41
+ /** @internal the current mesh as bytes, imported by the pathfinding worker */
42
+ exportMesh() { return lib.exportNavMesh(api.navMesh); },
43
+ /** @internal how far (m) a path may end short of its goal and still count as arriving, for a character of this radius */
44
+ reach(radius) { return (radius ?? agentRadius) + 0.6; },
45
+ /** Unload the navmesh (frees native objects); afterwards characters.findPath falls back to A* */
46
+ dispose() {
47
+ api.ready = false; api.version++; api.revision++;
48
+ if (api.helper) e.remove(api.helper); // the debug mesh is built by us: remove it completely
49
+ api.crowd?.destroy(); api.tileCache?.destroy(); api.navMesh?.destroy(); api.query?.destroy();
50
+ Object.assign(api, { navMesh: null, query: null, tileCache: null, crowd: null, helper: null });
51
+ obstacles.clear(); version = -1;
52
+ },
53
+ };
54
+
55
+ function search(from, to, o) {
56
+ if (!api.ready) return { status: 'notReady', path: null };
57
+ const [sx, sz] = xzOf(from), [gx, gz] = xzOf(to);
58
+ return navSearch(api.query, { x: sx, y: e.surface(sx, sz), z: sz }, { x: gx, y: e.surface(gx, gz), z: gz }, api.reach(o.radius));
59
+ }
60
+
61
+ /** Walkable triangle mesh of the ground (world coordinates), merged into one */
62
+ function geometry() {
63
+ const G = e.ground;
64
+ if (G.navGeometry) return G.navGeometry();
65
+ const list = G.staticTrimeshes?.() || [];
66
+ let n = 0, m = 0;
67
+ for (const t of list) { n += t.positions.length; m += t.indices.length; }
68
+ const positions = new Float32Array(n), indices = new Uint32Array(m);
69
+ let pn = 0, im = 0;
70
+ for (const t of list) { positions.set(t.positions, pn); for (let i = 0; i < t.indices.length; i++) indices[im + i] = t.indices[i] + pn / 3; pn += t.positions.length; im += t.indices.length; }
71
+ return { positions, indices };
72
+ }
73
+
74
+ async function build() {
75
+ pending = false;
76
+ if (api.ready) api.dispose();
77
+ const { positions, indices } = geometry();
78
+ if (!indices.length) return;
79
+ const ch = cellSize / 2;
80
+ const r = gen.generateTileCache(positions, indices, {
81
+ cs: cellSize, ch, tileSize, expectedLayersPerTile: 4, maxObstacles: 1024,
82
+ walkableSlopeAngle: maxSlope, walkableHeight: Math.ceil(agentHeight / ch), walkableClimb: Math.ceil(maxClimb / ch), walkableRadius: Math.ceil(agentRadius / cellSize),
83
+ });
84
+ if (!r.success) { console.warn('nav: ' + r.error); return; }
85
+ api.tileCache = r.tileCache; api.navMesh = r.navMesh;
86
+ api.query = new lib.NavMeshQuery(r.navMesh);
87
+ if (crowd) api.crowd = new lib.Crowd(r.navMesh, { maxAgents: 128, maxAgentRadius: agentRadius * 2 });
88
+ api.ready = true; api.version++; api.revision++;
89
+ syncObstacles();
90
+ if (debug) {
91
+ const three = await import('@recast-navigation/three');
92
+ api.helper = new three.NavMeshHelper(r.navMesh);
93
+ api.helper.position.y = 0.05; e.add(api.helper, { owner: 'engine:nav' });
94
+ }
95
+ }
96
+
97
+ // collision-module colliders → TileCache obstacles (only on add/remove), then rebuild affected tiles
98
+ function syncObstacles() {
99
+ const ph = e.collision;
100
+ if (!api.ready || !ph || ph.version === version) return;
101
+ version = ph.version;
102
+ const seen = new Set(), tc = api.tileCache;
103
+ ph.forEachStatic((type, o) => {
104
+ seen.add(o);
105
+ if (obstacles.has(o)) return;
106
+ const y = (o.y ?? e.surface(o.x, o.z)) - maxClimb, h = (o.h ?? 6) + maxClimb; // extend a bit downward: Recast ignores a bottom face lying exactly on the ground
107
+ const list = [];
108
+ if (type === 'box') {
109
+ // one obstacle affects at most 8 tiles: cut big boxes into pieces no larger than half a tile (along the box's own orientation)
110
+ const hw = o.hw + agentRadius, hd = o.hd + agentRadius, piece = tileSize * cellSize / 2; // add the agent radius so agents don't hug walls
111
+ const nx = Math.ceil(hw * 2 / piece), nz = Math.ceil(hd * 2 / piece), c = Math.cos(o.rot), sn = Math.sin(o.rot);
112
+ for (let i = 0; i < nx; i++) for (let j = 0; j < nz; j++) {
113
+ const lx = -hw + (i + 0.5) * hw * 2 / nx, lz = -hd + (j + 0.5) * hd * 2 / nz;
114
+ const res = tc.addBoxObstacle({ x: o.x + lx * c + lz * sn, y: y + h / 2, z: o.z - lx * sn + lz * c }, { x: hw / nx, y: h / 2, z: hd / nz }, -o.rot); // Detour's rotation direction is opposite to three's
115
+ if (res.success) list.push(res.obstacle);
116
+ }
117
+ } else {
118
+ const res = tc.addCylinderObstacle({ x: o.x, y, z: o.z }, o.r + agentRadius, h);
119
+ if (res.success) list.push(res.obstacle);
120
+ }
121
+ obstacles.set(o, list);
122
+ queued += list.length;
123
+ if (queued >= 48) flush();
124
+ });
125
+ for (const [o, list] of obstacles) if (!seen.has(o)) { for (const ob of list) { tc.removeObstacle(ob); if (++queued >= 48) flush(); } obstacles.delete(o); }
126
+ flush();
127
+ api.revision++;
128
+ api.helper?.update();
129
+ }
130
+ // at most 64 obstacle requests per batch: process once a batch has built up, until all tiles are rebuilt
131
+ let queued = 0;
132
+ function flush() { queued = 0; for (let i = 0; i < 256 && !api.tileCache.update(api.navMesh).upToDate; i++); }
133
+
134
+ return {
135
+ name: 'nav', params: NAV_PARAMS, options: opts, label: 'Navmesh',
136
+ install(engine) {
137
+ e = engine;
138
+ const mv = e.movement;
139
+ ({ agentRadius = mv.radius, agentHeight = mv.height, maxClimb = mv.stepHeight, maxSlope = mv.maxSlope } = opts);
140
+ api.agent = { radius: agentRadius, height: agentHeight, maxClimb, maxSlope };
141
+ e.events.on('level:loaded', () => { if (lib) build(); else pending = true; });
142
+ return api;
143
+ },
144
+ async init() {
145
+ [lib, gen] = await Promise.all([import('@recast-navigation/core'), import('@recast-navigation/generators')]);
146
+ await lib.init();
147
+ if (pending) await build();
148
+ },
149
+ isReady() { return !!lib && !pending; },
150
+ fixed(dt) {
151
+ syncObstacles();
152
+ api.crowd?.update(dt);
153
+ },
154
+ describe() { return { ready: api.ready, version: api.version, obstacles: obstacles.size }; },
155
+ };
156
+ }
@@ -0,0 +1,17 @@
1
+ // Navmesh path query: the one Recast search in the engine. engine.nav.findPath calls it with the live query,
2
+ // the pathfinding worker (characters/pathJob.js) with a query on an imported copy of the same mesh. Worker-safe: no imports.
3
+
4
+ /**
5
+ * @param {any} query a Recast NavMeshQuery
6
+ * @param {{ x: number, y: number, z: number }} s start (y = ground height, looked up by the caller: the worker has no ground)
7
+ * @param {{ x: number, y: number, z: number }} g goal
8
+ * @param {number} reach how far the path's end may stop short of the goal and still count as reaching it (m)
9
+ * @returns {{ status: 'ok' | 'noPath', path: { x: number, z: number }[] | null }}
10
+ */
11
+ export function navSearch(query, s, g, reach) {
12
+ const r = query.computePath(s, g, { halfExtents: { x: 2, y: 4, z: 2 } });
13
+ if (!r.success || !r.path.length) return { status: 'noPath', path: null };
14
+ const end = r.path[r.path.length - 1];
15
+ if (Math.hypot(end.x - g.x, end.z - g.z) > reach) return { status: 'noPath', path: null }; // end point unreachable
16
+ return { status: 'ok', path: r.path.map(p => ({ x: p.x, z: p.z })) };
17
+ }
@@ -0,0 +1,137 @@
1
+ // Client side of net(): a read-only mirror of the host. It never changes shared state itself:
2
+ // - ops.run of a net: 'peer' op checks can locally (instant feedback, e.g. grey out "buy" without gold), sends { name, args, seq } and returns
3
+ // { ok: true, pending: true, seq }; other ops are refused here ('notAllowed') except net: 'local' ones, which run on this machine
4
+ // (ops they call are routed the same way, so a local "quick buy" button still asks the host to buy).
5
+ // - snapshots / diffs from the host are queued and applied in the next fixed step (inside ops.allow, the only window the client guard opens).
6
+ // - the own avatar is driven locally and its position sent to the host every few steps; other avatars glide between updates (plain interpolation;
7
+ // prediction / rollback is a later step).
8
+ // - world.simulate does not run here: the host simulates and the result arrives as diffs (two simulations would fight).
9
+ import { applyPart, idMap } from './sync.js';
10
+ import { failure } from '../../core/opsFail.js';
11
+
12
+ const STEP = 1 / 60;
13
+ const ASK_EVERY = 30; // steps between snapshot requests while out of step
14
+
15
+ /** @internal */
16
+ export class NetClient {
17
+ constructor(e, t, o) {
18
+ this.e = e; this.o = o;
19
+ this.actor = null; // this client's actor id on the host (e.g. 'guest-1'); here it is called 'player'
20
+ this.n = -1; this.synced = false; this.since = 0;
21
+ this.inbox = []; this.pending = new Map(); this.seq = 0;
22
+ this.glides = new Map(); this.every = o.syncEvery; this.lastPos = '';
23
+ this.map = idMap(null);
24
+ this.stats = { in: 0, out: 0, diffs: 0, snapshots: 0, gaps: 0 };
25
+ this.connected = false;
26
+ e.ops._client = true;
27
+ e.ops._route = (d, args, opts) => this.route(d, args, opts);
28
+ if (e.world) e.world._s.fixed = () => {}; // the host simulates; see the top of the file
29
+ this.connect(t);
30
+ }
31
+
32
+ /** (Re)attach a transport, e.g. after a dropped connection: the host answers with a full snapshot */
33
+ connect(t) {
34
+ for (const s of this.subs || []) s.remove();
35
+ this.t?.close?.();
36
+ this.t = t; this.connected = false; this.synced = false; this.n = -1; this.inbox.length = 0;
37
+ this.subs = [
38
+ t.on('open', () => { this.connected = true; this.since = this.e.tick; this.e.events.emit('net:open', {}); }),
39
+ t.on('close', () => { this.connected = false; this.synced = false; this.e.events.emit('net:close', {}); }),
40
+ t.on('message', m => { if (m && typeof m === 'object') { this.stats.in++; this.inbox.push(m); } }),
41
+ ];
42
+ }
43
+ send(msg) { this.stats.out++; this.t.send(msg); }
44
+
45
+ route(d, args, opts) {
46
+ if (d.net === 'local') return null;
47
+ if (d.net !== 'peer') return failure('notAllowed');
48
+ if (!this.synced) return failure('offline');
49
+ const c = this.e.ops.can(d.name, args, opts);
50
+ if (!c.ok) return c;
51
+ const seq = ++this.seq, out = {};
52
+ for (const k in args) out[k] = typeof args[k] === 'string' ? this.map.toHost(args[k]) : args[k]; // 'player' here means this client's actor there
53
+ this.pending.set(seq, d.name);
54
+ this.send({ t: 'op', seq, name: d.name, args: out });
55
+ return { ok: true, pending: true, seq };
56
+ }
57
+
58
+ fixed() {
59
+ const e = this.e;
60
+ if (this.inbox.length) {
61
+ const box = this.inbox.splice(0);
62
+ e.ops.allow(() => { for (const m of box) this.handle(m); });
63
+ }
64
+ if (this.synced && e.tick % this.o.posEvery === 0) this.sendPosition();
65
+ }
66
+ handle(m) {
67
+ const e = this.e;
68
+ if (m.t === 's') {
69
+ this.actor = m.you; this.map = idMap(m.you); this.every = m.every ?? this.every;
70
+ this.glides.clear();
71
+ this.apply(m.parts, true);
72
+ this.n = m.n; this.synced = true; this.stats.snapshots++;
73
+ e.events.emit('net:sync', { tick: m.tick, full: true });
74
+ } else if (m.t === 'd') {
75
+ if (!this.synced || m.n <= this.n) return;
76
+ if (m.n !== this.n + 1) return this.gap();
77
+ this.apply(m.parts, false);
78
+ this.n = m.n; this.stats.diffs++;
79
+ e.events.emit('net:sync', { tick: m.tick, full: false });
80
+ } else if (m.t === 'k') {
81
+ if (this.synced && m.n > this.n) this.gap();
82
+ } else if (m.t === 'r') {
83
+ this.pending.delete(m.seq);
84
+ e.events.emit('net:result', { seq: m.seq, name: m.name, ok: m.ok, code: m.code, value: m.value, tick: m.tick });
85
+ }
86
+ }
87
+ /** a diff was lost: stop applying diffs and ask for a snapshot (frame asks again if that is lost too) */
88
+ gap() {
89
+ this.synced = false; this.stats.gaps++;
90
+ this.since = this.e.tick;
91
+ this.send({ t: 'resync' });
92
+ }
93
+ apply(parts, full) {
94
+ const o = this.o, opts = {
95
+ toLocal: this.map.toLocal, actorFields: o.actorFields, avatarFields: o.avatarFields,
96
+ smooth: rec => o.interpolate.includes(rec.kind),
97
+ glide: (id, to) => this.glides.set(id, { from: { ...this.e.world.get(id).position }, to: { ...to }, t: 0, dur: this.every * STEP }),
98
+ };
99
+ for (const key in parts) {
100
+ if (key !== 'world' && key !== 'vars' && key !== 'containers' && !this.e.saves.providers.has(key)) continue;
101
+ if (key === 'world' && !this.e.world) continue;
102
+ if (key === 'containers' && !this.e.inventory) continue;
103
+ applyPart(this.e, key, parts[key], full, opts);
104
+ }
105
+ }
106
+ sendPosition() {
107
+ const rec = this.e.world?.get('player');
108
+ if (!rec) return;
109
+ const p = rec.position, f = {};
110
+ for (const k of this.o.avatarFields) if (k in rec) f[k] = rec[k];
111
+ const key = `${p.x},${p.y},${p.z},${JSON.stringify(f)}`;
112
+ if (key === this.lastPos && this.e.tick % 60) return; // unchanged: resend once a second anyway, in case the last one was lost
113
+ this.lastPos = key;
114
+ this.send({ t: 'p', p: [p.x, p.y, p.z], f });
115
+ }
116
+
117
+ frame(dt) {
118
+ // out of step (snapshot or the request for it lost, or the first one never came): ask again
119
+ if (!this.synced && this.connected && this.e.tick - this.since >= ASK_EVERY) { this.since = this.e.tick; this.send({ t: 'resync' }); }
120
+ if (!this.glides.size) return;
121
+ const W = this.e.world;
122
+ for (const [id, g] of this.glides) {
123
+ if (!W.get(id)) { this.glides.delete(id); continue; }
124
+ g.t += dt;
125
+ const k = Math.min(1, g.t / g.dur);
126
+ W.move(id, { x: g.from.x + (g.to.x - g.from.x) * k, y: g.from.y + (g.to.y - g.from.y) * k, z: g.from.z + (g.to.z - g.from.z) * k });
127
+ if (k >= 1) this.glides.delete(id);
128
+ }
129
+ }
130
+ dispose() {
131
+ for (const s of this.subs) s.remove();
132
+ this.t.close?.();
133
+ const e = this.e;
134
+ e.ops._client = false; e.ops._route = null;
135
+ if (e.world) delete e.world._s.fixed;
136
+ }
137
+ }