threedog 0.7.0 → 1.0.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.
- package/CHANGELOG.md +411 -1
- package/README.md +8 -11
- package/core/{Animator.js → assets/Animator.js} +30 -18
- package/core/{Assets.js → assets/Assets.js} +131 -56
- package/core/{Baked.js → assets/Baked.js} +4 -4
- package/core/assets/assetEntry.js +302 -0
- package/core/{assetHot.js → assets/assetHot.js} +9 -7
- package/core/{assetLoad.js → assets/assetLoad.js} +55 -16
- package/core/assets/assetLod.js +113 -0
- package/core/{assetManifest.js → assets/assetManifest.js} +19 -11
- package/core/{assetReport.js → assets/assetReport.js} +7 -14
- package/core/{namedModel.js → assets/namedModel.js} +17 -13
- package/core/{Audio.js → audio/Audio.js} +47 -25
- package/core/{audioRules.js → audio/audioRules.js} +8 -5
- package/core/{audioVoice.js → audio/audioVoice.js} +17 -30
- package/core/{Events.js → base/Events.js} +3 -3
- package/core/{I18n.js → base/I18n.js} +15 -12
- package/core/{Random.js → base/Random.js} +7 -4
- package/core/{Vars.js → base/Vars.js} +14 -4
- package/core/{easing.js → base/easing.js} +1 -1
- package/core/{errorCodes.js → base/errorCodes.js} +151 -125
- package/core/{errorHints.js → base/errorHints.js} +17 -10
- package/core/{errors.js → base/errors.js} +12 -10
- package/core/{events.generated.js → base/events.generated.js} +11 -0
- package/core/base/follows.js +26 -0
- package/core/base/math.js +66 -0
- package/core/{params.js → base/params.js} +5 -6
- package/core/base/saveValues.js +81 -0
- package/core/base/sections.js +97 -0
- package/core/base/spelling.js +32 -0
- package/core/base/util.js +32 -0
- package/core/{validate.js → base/validate.js} +34 -34
- package/core/{Engine.js → engine/Engine.js} +137 -91
- package/core/{Loading.js → engine/Loading.js} +34 -5
- package/core/{copies.js → engine/copies.js} +4 -4
- package/core/{headlessLoad.js → engine/headlessLoad.js} +32 -15
- package/core/{lifecycle.js → engine/lifecycle.js} +41 -34
- package/core/{loop.js → engine/loop.js} +88 -49
- package/core/engine/moduleSlots.js +176 -0
- package/core/{moduleTypes.js → engine/moduleTypes.js} +26 -14
- package/core/engine/modules.js +503 -0
- package/core/{page.js → engine/page.js} +59 -29
- package/core/{plugins.js → engine/plugins.js} +21 -12
- package/core/{services.js → engine/services.js} +50 -40
- package/core/{startup.js → engine/startup.js} +66 -54
- package/core/{Input.js → input/Input.js} +67 -39
- package/core/{inputContext.js → input/inputContext.js} +14 -5
- package/core/input/inputKeyboard.js +39 -0
- package/core/{inputPad.js → input/inputPad.js} +24 -3
- package/core/{inputPointer.js → input/inputPointer.js} +11 -3
- package/core/input/inputSeat.js +66 -0
- package/core/{inputTouch.js → input/inputTouch.js} +24 -7
- package/core/{Budget.js → inspect/Budget.js} +21 -20
- package/core/{DebugOverrides.js → inspect/DebugOverrides.js} +43 -18
- package/core/{Describe.js → inspect/Describe.js} +34 -26
- package/core/{Inspect.js → inspect/Inspect.js} +39 -10
- package/core/{InspectMaterials.js → inspect/InspectMaterials.js} +7 -8
- package/core/{InspectPerf.js → inspect/InspectPerf.js} +19 -13
- package/core/{InspectScene.js → inspect/InspectScene.js} +8 -5
- package/core/{MaterialInfo.js → inspect/MaterialInfo.js} +3 -2
- package/core/{UrlState.js → inspect/UrlState.js} +60 -24
- package/core/{catalog.js → inspect/catalog.js} +15 -8
- package/core/{checks.js → inspect/checks.js} +7 -5
- package/core/{debugView.js → inspect/debugView.js} +4 -4
- package/core/{describeRender.js → inspect/describeRender.js} +24 -22
- package/core/{describeText.js → inspect/describeText.js} +17 -8
- package/core/inspect/geometryWatch.js +38 -0
- package/core/{lightWatch.js → inspect/lightWatch.js} +8 -8
- package/core/inspect/metadata.generated.js +4140 -0
- package/core/{pictureWriters.js → inspect/pictureWriters.js} +130 -47
- package/core/{shaderCost.js → inspect/shaderCost.js} +1 -1
- package/core/inspect/toolDoor.js +21 -0
- package/core/inspect/valueHistory.js +35 -0
- package/core/{Ops.js → ops/Ops.js} +44 -14
- package/core/{control.js → ops/control.js} +57 -31
- package/core/ops/holds.js +140 -0
- package/core/{opsAtomic.js → ops/opsAtomic.js} +18 -5
- package/core/{opsCheck.js → ops/opsCheck.js} +11 -10
- package/core/{opsFail.js → ops/opsFail.js} +5 -7
- package/core/{opsGuard.js → ops/opsGuard.js} +3 -3
- package/core/{opsRun.js → ops/opsRun.js} +9 -6
- package/core/{owned.js → ops/owned.js} +33 -19
- package/core/{sharedPoint.js → ops/sharedPoint.js} +13 -16
- package/core/{stackFrames.js → ops/stackFrames.js} +2 -2
- package/core/{stacks.js → ops/stacks.js} +34 -15
- package/core/{wrap.js → ops/wrap.js} +8 -6
- package/core/{SaveStore.js → saves/SaveStore.js} +53 -27
- package/core/{Saves.js → saves/Saves.js} +171 -54
- package/core/saves/saveApply.js +312 -0
- package/core/{saveFiles.js → saves/saveFiles.js} +45 -38
- package/core/{saveVersions.js → saves/saveVersions.js} +24 -23
- package/core/saves/session.js +231 -0
- package/core/saves/stateReplace.js +184 -0
- package/core/{Rays.js → scene/Rays.js} +44 -30
- package/core/{Views.js → scene/Views.js} +16 -8
- package/core/{adaptive.js → scene/adaptive.js} +19 -9
- package/core/{canvasBox.js → scene/canvasBox.js} +4 -3
- package/core/scene/compositeGround.js +72 -0
- package/core/scene/floor.js +17 -0
- package/core/{gameLights.js → scene/gameLights.js} +17 -7
- package/core/{grayMap.js → scene/grayMap.js} +8 -2
- package/core/{grid.js → scene/grid.js} +6 -4
- package/core/{gridLoad.js → scene/gridLoad.js} +1 -1
- package/core/scene/ground.contract.js +14 -0
- package/core/scene/ground.js +139 -0
- package/core/{movement.js → scene/movement.js} +3 -3
- package/core/{pictureSlots.js → scene/pictureSlots.js} +2 -2
- package/core/scene/pixel.js +154 -0
- package/core/scene/pixelRatio.js +167 -0
- package/core/{sceneDelta.js → scene/sceneDelta.js} +5 -5
- package/core/{sceneMethods.js → scene/sceneMethods.js} +43 -15
- package/core/scene/stepPoses.js +60 -0
- package/core/{targeting.js → scene/targeting.js} +4 -5
- package/core/{thingExplain.js → scene/thingExplain.js} +7 -6
- package/core/{thingOps.js → scene/thingOps.js} +25 -16
- package/core/{things.js → scene/things.js} +18 -5
- package/core/{threePatches.js → scene/threePatches.js} +1 -1
- package/core/{tier.js → scene/tier.js} +10 -0
- package/core/scene/view.js +161 -0
- package/core/{Batching.js → stream/Batching.js} +85 -28
- package/core/{Stream.js → stream/Stream.js} +80 -38
- package/core/{batchGroups.js → stream/batchGroups.js} +11 -9
- package/core/{chunkStream.js → stream/chunkStream.js} +40 -0
- package/core/{scatter.js → stream/scatter.js} +7 -4
- package/core/{streamSources.js → stream/streamSources.js} +14 -8
- package/core/{Commits.js → threads/Commits.js} +17 -11
- package/core/{Jobs.js → threads/Jobs.js} +25 -13
- package/core/{Threads.js → threads/Threads.js} +21 -18
- package/core/{threadPure.js → threads/threadPure.js} +7 -5
- package/core/{threadServe.js → threads/threadServe.js} +3 -3
- package/core/{Clock.js → time/Clock.js} +20 -11
- package/core/{dayTime.js → time/dayTime.js} +5 -1
- package/core/{skip.js → time/skip.js} +28 -21
- package/core/{time.js → time/time.js} +32 -20
- package/core/{uiClock.js → time/uiClock.js} +4 -4
- package/docs/AI.md +92 -72
- package/docs/API.md +33 -28
- package/docs/CODEMAP.md +121 -200
- package/docs/COMMANDS.md +77 -70
- package/docs/CONVENTIONS.md +167 -24
- package/docs/MIGRATING-0.8.md +171 -0
- package/docs/MIGRATING-1.0.md +101 -0
- package/docs/THREE.md +140 -38
- package/docs/api/assets.md +12 -13
- package/docs/api/audio.md +6 -5
- package/docs/api/budget.md +4 -4
- package/docs/api/calendar.md +10 -11
- package/docs/api/cameraRig.md +32 -29
- package/docs/api/characters.md +35 -24
- package/docs/api/clock.md +1 -1
- package/docs/api/collider3d.md +9 -0
- package/docs/api/collision.md +12 -9
- package/docs/api/content.md +18 -17
- package/docs/api/dialogue.md +5 -4
- package/docs/api/engine.md +58 -54
- package/docs/api/events.md +17 -6
- package/docs/api/flow.md +1 -1
- package/docs/api/grids.md +1 -1
- package/docs/api/ground.md +6 -7
- package/docs/api/hud.md +2 -2
- package/docs/api/i18n.md +6 -6
- package/docs/api/input.md +9 -8
- package/docs/api/interactions.md +5 -5
- package/docs/api/inventory.md +10 -10
- package/docs/api/jobs.md +5 -5
- package/docs/api/level.md +10 -9
- package/docs/api/lightPool.md +1 -6
- package/docs/api/music.md +3 -3
- package/docs/api/nav.md +14 -12
- package/docs/api/openWorld.md +1 -1
- package/docs/api/particles.md +10 -7
- package/docs/api/photo.md +4 -2
- package/docs/api/pickups.md +3 -3
- package/docs/api/post.md +21 -5
- package/docs/api/quests.md +7 -5
- package/docs/api/rays.md +7 -7
- package/docs/api/rigid.md +29 -19
- package/docs/api/saves.md +13 -11
- package/docs/api/settings.md +9 -8
- package/docs/api/sky.md +8 -7
- package/docs/api/stream.md +8 -9
- package/docs/api/swarm.md +34 -0
- package/docs/api/terrain.md +6 -8
- package/docs/api/terrainDraw.md +2 -2
- package/docs/api/timeline.md +4 -4
- package/docs/api/tools.md +27 -22
- package/docs/api/triggers.md +2 -0
- package/docs/api/vars.md +1 -1
- package/docs/api/vegetation.md +4 -3
- package/docs/api/vegetationDraw.md +2 -2
- package/docs/api/water.md +7 -3
- package/docs/api/weather.md +27 -12
- package/docs/api/weatherDraw.md +4 -4
- package/docs/api/world.md +6 -6
- package/docs/recipes/01-new-game.md +28 -43
- package/docs/recipes/02-npc.md +21 -23
- package/docs/recipes/03-items-pickups.md +37 -53
- package/docs/recipes/04-quests.md +5 -4
- package/docs/recipes/05-props-bake.md +34 -44
- package/docs/recipes/06-clock-events.md +31 -22
- package/docs/recipes/07-flow-scenes-pause.md +30 -32
- package/docs/recipes/08-weather-sky.md +91 -37
- package/docs/recipes/09-materials-lights-models.md +25 -39
- package/docs/recipes/10-blender-level.md +22 -11
- package/docs/recipes/11-vehicles.md +44 -24
- package/docs/recipes/12-navmesh.md +60 -15
- package/docs/recipes/13-animation.md +70 -22
- package/docs/recipes/14-custom-module.md +33 -20
- package/docs/recipes/15-perf.md +51 -28
- package/docs/recipes/16-dev-bridge.md +39 -58
- package/docs/recipes/18-world.md +49 -28
- package/docs/recipes/19-shooting-picking.md +7 -4
- package/docs/recipes/20-content-actions-save.md +55 -40
- package/docs/recipes/21-camera.md +47 -25
- package/docs/recipes/22-shop-gold.md +21 -16
- package/docs/recipes/23-ops.md +28 -24
- package/docs/recipes/26-ui-parts.md +25 -14
- package/docs/recipes/27-net.md +3 -5
- package/docs/recipes/28-terrain.md +119 -23
- package/docs/recipes/29-grid.md +3 -3
- package/docs/recipes/30-templates.md +63 -30
- package/docs/recipes/31-audio.md +7 -7
- package/docs/recipes/32-action-game.md +21 -11
- package/docs/recipes/33-stream-sources.md +6 -3
- package/docs/recipes/34-environment-lighting.md +72 -25
- package/docs/recipes/35-picture-from-reference.md +20 -23
- package/docs/recipes/36-ground-plants.md +106 -0
- package/docs/recipes/40-scene.md +18 -21
- package/docs/recipes/43-shader-patches-surfaces.md +27 -48
- package/docs/recipes/44-water-weather-hooks.md +56 -50
- package/docs/recipes/45-cascaded-shadows.md +41 -20
- package/docs/recipes/46-blender-art-route.md +9 -10
- package/docs/recipes/49-game-module-contract.md +7 -6
- package/docs/recipes/50-own-renderer.md +3 -2
- package/docs/recipes/51-capability-catalog.md +14 -8
- package/docs/recipes/52-around-the-picture.md +56 -100
- package/docs/recipes/53-ground-up-close.md +61 -24
- package/docs/recipes/55-mods-time-wrap.md +17 -20
- package/docs/recipes/56-webgpu-tsl.md +28 -15
- package/docs/recipes/57-control.md +39 -12
- package/docs/recipes/58-input.md +15 -2
- package/docs/recipes/59-depth-post-effects.md +1 -1
- package/docs/recipes/60-judge-the-picture.md +30 -25
- package/docs/recipes/62-picture-code.md +129 -39
- package/docs/recipes/63-third-person-start.md +19 -5
- package/docs/recipes/64-particles.md +54 -5
- package/docs/recipes/65-swarm.md +140 -0
- package/docs/recipes/66-common-tasks.md +313 -0
- package/examples/action/.threedog/content-index.json +1 -1
- package/examples/action/GAMEMAP.md +5 -5
- package/examples/perf-island/.threedog/content-index.json +1 -0
- package/examples/{sheepisland → perf-island}/GAMEMAP.md +15 -14
- package/examples/{sheepisland → perf-island}/assets.js +1 -1
- package/examples/{sheepisland → perf-island}/main.js +8 -8
- package/examples/{sheepisland → perf-island}/models.js +1 -1
- package/examples/{sheepisland → perf-island}/params.js +1 -1
- package/examples/{sheepisland → perf-island}/picture/index.js +1 -1
- package/examples/{sheepisland → perf-island}/picture/palette.js +2 -2
- package/examples/{sheepisland → perf-island}/views.js +1 -1
- package/examples/plain-picture/GAMEMAP.md +4 -1
- package/examples/rc2d/GAMEMAP.md +4 -3
- package/examples/rc2d/main.js +1 -3
- package/examples/rc2d/rc2d.js +1 -1
- package/examples/styletest/.threedog/content-index.json +1 -1
- package/examples/styletest/GAMEMAP.md +6 -5
- package/examples/techniques/contact-shadows/contactShadows.js +1 -1
- package/examples/techniques/contact-shadows/main.js +1 -1
- package/examples/techniques/interior-light/main.js +1 -1
- package/examples/techniques/modular-building/README.md +7 -6
- package/examples/techniques/outlines-and-rim/picture/post.js +1 -1
- package/examples/techniques/procedural-animation/README.md +1 -1
- package/examples/webgpu-tsl/GAMEMAP.md +2 -1
- package/examples/world/.threedog/content-index.json +1 -1
- package/examples/world/GAMEMAP.md +9 -7
- package/examples/world/main.js +3 -3
- package/examples/world/picture/sky.js +1 -1
- package/index.d.ts +16 -12
- package/index.js +53 -32
- package/llms.txt +11 -14
- package/modules/ambience/Ambience.js +16 -11
- package/modules/calendar/Calendar.js +19 -19
- package/modules/calendar/index.js +18 -16
- package/modules/calendar/schedule.js +11 -6
- package/modules/calendar/seasons.js +7 -7
- package/modules/camera/FirstPersonCamera.js +5 -5
- package/modules/camera/FixedCamera.js +43 -30
- package/modules/camera/FollowCamera.js +24 -15
- package/modules/camera/RtsCamera.js +12 -11
- package/modules/camera/common.js +72 -37
- package/modules/camera/contract.js +1 -0
- package/modules/camera/pixelSnap.js +62 -0
- package/modules/characters/Character.js +166 -70
- package/modules/characters/NPC.js +84 -44
- package/modules/characters/assetModel.js +16 -12
- package/modules/characters/astar.js +48 -27
- package/modules/characters/behaviors.js +4 -5
- package/modules/characters/character.contract.js +21 -0
- package/modules/characters/contract.js +17 -3
- package/modules/characters/index.js +240 -226
- package/modules/characters/moveHelpers.js +88 -12
- package/modules/characters/mover.contract.js +14 -0
- package/modules/characters/movers/common.js +22 -18
- package/modules/characters/movers/manual.js +1 -1
- package/modules/characters/movers/rapier.js +18 -10
- package/modules/characters/movers/simple.js +26 -13
- package/modules/characters/movers/support.js +24 -13
- package/modules/characters/npcAvoid.js +19 -16
- package/modules/characters/npcSim.js +88 -32
- package/modules/characters/npcView.js +19 -11
- package/modules/characters/pathAsync.js +81 -29
- package/modules/characters/pathJob.js +38 -21
- package/modules/characters/pathWorker.js +1 -1
- package/modules/characters/player.js +120 -0
- package/modules/characters/playerStrings.js +2 -2
- package/modules/characters/proceduralRig.js +2 -2
- package/modules/characters/seat.js +56 -0
- package/modules/characters/useModel.js +3 -3
- package/modules/characters/vehicle.contract.js +8 -5
- package/modules/characters/walkTo.js +40 -33
- package/modules/collision/Collision.js +69 -45
- package/modules/collision/contract.js +15 -0
- package/modules/collision/grid.js +6 -1
- package/modules/content/explain.js +2 -3
- package/modules/content/fields.js +24 -3
- package/modules/content/ids.js +5 -3
- package/modules/content/index.js +132 -114
- package/modules/content/loader.js +117 -140
- package/modules/content/markers.js +25 -0
- package/modules/content/models.js +54 -17
- package/modules/content/packs.js +5 -4
- package/modules/content/parts.js +2 -2
- package/modules/content/places.js +36 -0
- package/modules/content/props.js +22 -17
- package/modules/content/rules.js +47 -61
- package/modules/content/stages.js +7 -7
- package/modules/content/templates.js +80 -49
- package/modules/content/words.js +47 -0
- package/modules/debug/DebugPanel.js +14 -45
- package/modules/flow/contract.js +2 -1
- package/modules/flow/index.js +30 -16
- package/modules/gameplay/Container.js +17 -13
- package/modules/gameplay/Containers.js +23 -6
- package/modules/gameplay/Dialogue.js +93 -34
- package/modules/gameplay/Interactions.js +93 -51
- package/modules/gameplay/Inventory.js +64 -22
- package/modules/gameplay/Quests.js +146 -30
- package/modules/gameplay/boxOps.js +8 -1
- package/modules/gameplay/dialogueBubbles.js +16 -12
- package/modules/gameplay/inventoryOps.js +11 -8
- package/modules/gameplay/pickups.js +63 -37
- package/modules/gameplay/questLog.js +4 -2
- package/modules/gameplay/questSave.js +9 -4
- package/modules/gameplay/quests.contract.js +1 -1
- package/modules/grid/Grid.js +2 -2
- package/modules/grid/Layer.js +12 -8
- package/modules/grid/index.js +28 -23
- package/modules/grid/ops.js +43 -46
- package/modules/grid/preview.js +7 -7
- package/modules/grid/tiles.js +6 -6
- package/modules/hlod/index.js +42 -57
- package/modules/hud/index.js +22 -14
- package/modules/hud/parts.js +8 -8
- package/modules/level/contract.js +16 -0
- package/modules/level/heightfield.js +29 -31
- package/modules/level/index.js +113 -161
- package/modules/level/load.js +196 -0
- package/modules/level/meshes.js +65 -0
- package/modules/level/nodes.js +40 -23
- package/modules/lights/index.js +33 -183
- package/modules/lights/slots.js +93 -0
- package/modules/lights/sources.js +90 -0
- package/modules/music/Music.js +34 -28
- package/modules/music/rules.js +1 -1
- package/modules/nav/contract.js +16 -0
- package/modules/nav/index.js +180 -49
- package/modules/nav/navPath.js +45 -10
- package/modules/net/Client.js +7 -7
- package/modules/net/Host.js +7 -7
- package/modules/net/Net.js +1 -1
- package/modules/net/index.js +6 -5
- package/modules/net/sync.js +2 -2
- package/modules/net/transports.js +2 -2
- package/modules/particles/Particles.js +95 -29
- package/modules/particles/effect.js +86 -32
- package/modules/particles/shaders.js +19 -1
- package/modules/photo/Photo.js +23 -23
- package/modules/rigid/RigidBodies.js +255 -89
- package/modules/rigid/Vehicles.js +17 -8
- package/modules/rigid/collider3d.contract.js +26 -0
- package/modules/rigid/ragdoll.js +298 -0
- package/modules/rigid/ragdollBones.js +209 -0
- package/modules/rigid/refresh.js +10 -0
- package/modules/rigid/sync.js +51 -20
- package/modules/settings/Settings.js +177 -61
- package/modules/settings/apply.js +20 -13
- package/modules/settings/sections.js +19 -14
- package/modules/settings/strings.js +2 -2
- package/modules/settings/view.js +3 -3
- package/modules/sky/Sky.js +29 -22
- package/modules/sky/clouds.js +7 -7
- package/modules/sky/dome.js +33 -20
- package/modules/sky/domeLayers.js +9 -9
- package/modules/sky/environment.js +6 -6
- package/modules/sky/frame.js +19 -10
- package/modules/sky/season.js +7 -1
- package/modules/sky/shader.js +24 -25
- package/modules/sky/skyModule.js +17 -28
- package/modules/sky/tint.js +1 -1
- package/modules/swarm/Swarm.js +356 -0
- package/modules/swarm/draw.js +68 -0
- package/modules/swarm/grid.js +63 -0
- package/modules/terrain/ChunkManager.js +71 -59
- package/modules/terrain/chunkQueries.js +7 -24
- package/modules/terrain/chunkWorker.js +1 -1
- package/modules/terrain/draw.js +14 -11
- package/modules/terrain/index.js +42 -42
- package/modules/terrain/terrainColorMap.js +6 -3
- package/modules/terrain/terrainDefaults.js +2 -1
- package/modules/terrain/terrainEdit.js +45 -29
- package/modules/terrain/terrainGen.js +19 -12
- package/modules/terrain/terrainIsland.js +2 -2
- package/modules/terrain/terrainMesh.js +7 -6
- package/modules/terrain/terrainShape.js +8 -10
- package/modules/terrain/terrainShapeMain.js +3 -3
- package/modules/timeline/Timeline.js +83 -43
- package/modules/timeline/Tween.js +25 -19
- package/modules/timeline/shots.js +24 -17
- package/modules/triggers/index.js +95 -28
- package/modules/vegetation/Vegetation.js +34 -26
- package/modules/vegetation/densityMap.js +4 -7
- package/modules/vegetation/draw.js +39 -18
- package/modules/vegetation/groundChunks.js +21 -38
- package/modules/vegetation/index.js +37 -12
- package/modules/vegetation/modelGeometry.js +3 -6
- package/modules/vegetation/nearFade.js +1 -1
- package/modules/water/Water.js +20 -16
- package/modules/water/surface.js +3 -3
- package/modules/weather/Weather.js +52 -40
- package/modules/weather/particles.js +8 -8
- package/modules/weather/weatherDraw.js +28 -23
- package/modules/world/World.js +28 -15
- package/modules/world/explain.js +1 -1
- package/modules/world/index.js +30 -17
- package/modules/world/persist.js +90 -27
- package/modules/world/pos.js +5 -5
- package/modules/world/records.js +37 -11
- package/modules/world/sim.js +78 -27
- package/modules/world/simFar.js +2 -2
- package/modules/world/state.js +43 -11
- package/modules/world/thingSource.js +20 -5
- package/modules/world/views.js +27 -14
- package/package.json +48 -7
- package/presets.js +1 -3
- package/render/PostFX.js +83 -53
- package/render/backend.js +31 -33
- package/render/cascades.js +17 -12
- package/render/cascades.tsl.js +5 -8
- package/render/daylight.js +20 -3
- package/render/detail.js +8 -7
- package/render/effects.js +28 -25
- package/render/fuse.js +3 -3
- package/render/fx/cloudShadow.tsl.js +3 -3
- package/render/fx/detail.tsl.js +1 -1
- package/render/fx/index.js +5 -10
- package/render/fx/library.tsl.js +11 -0
- package/render/fx/lighting.tsl.js +2 -1
- package/render/fx/nodeErrors.tsl.js +2 -2
- package/render/fx/objectOrigin.tsl.js +1 -2
- package/render/fx/particles.tsl.js +8 -6
- package/render/fx/patchNodes.tsl.js +5 -15
- package/render/fx/postWebGPU.tsl.js +30 -26
- package/render/fx/sky.tsl.js +15 -17
- package/render/fx/uniforms.tsl.js +4 -4
- package/render/fx/wind.tsl.js +1 -1
- package/render/fxNumbers.js +3 -0
- package/render/layers.js +2 -2
- package/render/lighting.js +5 -3
- package/render/lightmap.js +41 -21
- package/render/lights.js +6 -6
- package/render/lit.js +24 -0
- package/render/materials.js +8 -7
- package/render/mergeCommon.js +22 -0
- package/render/modeling.js +2 -2
- package/render/occlusion.js +62 -15
- package/render/ownFrame.js +4 -4
- package/render/patches.js +15 -15
- package/render/postWebGL.js +15 -12
- package/render/probes.js +41 -19
- package/render/recordSweep.js +117 -0
- package/render/shaderErrors.js +10 -9
- package/render/shadowCache.tsl.js +107 -0
- package/render/shadowTypes.js +9 -0
- package/{core → render}/shadows.js +59 -13
- package/render/slots.js +7 -7
- package/render/terms.js +22 -24
- package/render/uniformDecl.js +1 -1
- package/types/core.d.ts +336 -202
- package/types/gameplay.d.ts +254 -130
- package/types/generated/core/{Audio.d.ts → audio/Audio.d.ts} +22 -7
- package/types/generated/core/{audioVoice.d.ts → audio/audioVoice.d.ts} +2 -2
- package/types/generated/core/{Events.d.ts → base/Events.d.ts} +4 -4
- package/types/generated/core/{I18n.d.ts → base/I18n.d.ts} +8 -16
- package/types/generated/core/{Random.d.ts → base/Random.d.ts} +4 -3
- package/types/generated/core/{page.d.ts → engine/page.d.ts} +8 -9
- package/types/generated/core/{Input.d.ts → input/Input.d.ts} +28 -10
- package/types/generated/core/input/inputSeat.d.ts +26 -0
- package/types/generated/core/{Budget.d.ts → inspect/Budget.d.ts} +36 -26
- package/types/generated/core/{DebugOverrides.d.ts → inspect/DebugOverrides.d.ts} +17 -3
- package/types/generated/core/{Ops.d.ts → ops/Ops.d.ts} +6 -4
- package/types/generated/core/{control.d.ts → ops/control.d.ts} +14 -10
- package/types/generated/core/{Saves.d.ts → saves/Saves.d.ts} +62 -18
- package/types/generated/core/{session.d.ts → saves/session.d.ts} +3 -2
- package/types/generated/core/{Rays.d.ts → scene/Rays.d.ts} +8 -27
- package/types/generated/core/{Views.d.ts → scene/Views.d.ts} +2 -2
- package/types/generated/core/{Batching.d.ts → stream/Batching.d.ts} +7 -7
- package/types/generated/core/{Stream.d.ts → stream/Stream.d.ts} +22 -18
- package/types/generated/core/{Commits.d.ts → threads/Commits.d.ts} +1 -1
- package/types/generated/core/{Jobs.d.ts → threads/Jobs.d.ts} +6 -6
- package/types/generated/core/{Threads.d.ts → threads/Threads.d.ts} +4 -4
- package/types/generated/modules/calendar/Calendar.d.ts +10 -11
- package/types/generated/modules/debug/DebugPanel.d.ts +1 -9
- package/types/generated/modules/grid/Grid.d.ts +1 -0
- package/types/generated/modules/grid/Layer.d.ts +1 -1
- package/types/generated/modules/grid/index.d.ts +3 -3
- package/types/generated/modules/music/Music.d.ts +7 -5
- package/types/generated/modules/particles/Particles.d.ts +5 -4
- package/types/generated/modules/sky/Sky.d.ts +21 -10
- package/types/generated/modules/swarm/Swarm.d.ts +80 -0
- package/types/generated/modules/timeline/Timeline.d.ts +7 -5
- package/types/generated/modules/water/Water.d.ts +1 -0
- package/types/generated/modules/weather/Weather.d.ts +14 -16
- package/types/generated/modules/weather/weatherDraw.d.ts +3 -3
- package/types/generated/modules/world/World.d.ts +11 -7
- package/types/generated/render/PostFX.d.ts +21 -23
- package/types/generated/render/materials.d.ts +1 -0
- package/types/generated/render/occlusion.d.ts +9 -0
- package/types/generated/render/patches.d.ts +1 -0
- package/types/generated/ui/Focus.d.ts +1 -0
- package/types/generated/ui/UI.d.ts +1 -0
- package/types/nav.d.ts +47 -15
- package/types/physics.d.ts +150 -73
- package/types/pixel.d.ts +75 -0
- package/types/tsl.d.ts +37 -4
- package/types/world.d.ts +48 -75
- package/ui/Focus.js +10 -9
- package/ui/Parts.js +13 -5
- package/ui/UI.js +7 -7
- package/ui/defaults.js +3 -2
- package/ui/theme.js +7 -5
- package/core/assetEntry.js +0 -109
- package/core/engineCamera.js +0 -32
- package/core/ground.js +0 -104
- package/core/holds.js +0 -134
- package/core/math.js +0 -48
- package/core/moduleSlots.js +0 -100
- package/core/modules.js +0 -314
- package/core/saveApply.js +0 -210
- package/core/saveValues.js +0 -46
- package/core/session.js +0 -174
- package/core/shadowTypes.js +0 -16
- package/core/util.js +0 -17
- package/core/view.js +0 -43
- package/docs/MIGRATING-0.3.md +0 -248
- package/docs/MIGRATING-0.4.md +0 -320
- package/docs/MIGRATING-0.5.md +0 -68
- package/docs/MIGRATING-0.6.md +0 -126
- package/docs/PERF.md +0 -245
- package/docs/recipes/36-sky-ground-plants.md +0 -158
- package/docs/recipes/37-terrain-hooks.md +0 -115
- package/docs/recipes/38-environment-hooks.md +0 -67
- package/docs/recipes/42-terrain-shape.md +0 -64
- package/docs/recipes/47-island-snow-backdrop.md +0 -43
- package/docs/recipes/54-three-idioms.md +0 -88
- package/examples/plain-picture/FINDINGS.md +0 -53
- package/examples/sheepisland/.threedog/content-index.json +0 -1
- package/modules/camera/projection.js +0 -21
- /package/core/{mulberry32.js → base/mulberry32.js} +0 -0
- /package/core/{inputDefaults.js → input/inputDefaults.js} +0 -0
- /package/core/{textureBytes.js → inspect/textureBytes.js} +0 -0
- /package/core/{actor.js → ops/actor.js} +0 -0
- /package/core/{opsHistory.js → ops/opsHistory.js} +0 -0
- /package/core/{saveIds.js → saves/saveIds.js} +0 -0
- /package/core/{screenToGround.js → scene/screenToGround.js} +0 -0
- /package/core/{cells.js → stream/cells.js} +0 -0
- /package/core/{streamCells.js → stream/streamCells.js} +0 -0
- /package/core/{pureWorker.js → threads/pureWorker.js} +0 -0
- /package/examples/{sheepisland → perf-island}/content/index.js +0 -0
- /package/examples/{sheepisland → perf-island}/content/props.js +0 -0
- /package/examples/{sheepisland → perf-island}/content/templates.js +0 -0
- /package/examples/{sheepisland → perf-island}/content/vegetation.js +0 -0
- /package/examples/{sheepisland → perf-island}/picture/lights.js +0 -0
- /package/examples/{sheepisland → perf-island}/picture/materials.js +0 -0
- /package/examples/{sheepisland → perf-island}/picture/sky.js +0 -0
package/modules/nav/index.js
CHANGED
|
@@ -1,35 +1,55 @@
|
|
|
1
1
|
// Navmesh (Recast): generated after a level loads; engine.characters.findPath switches to it automatically (falls back to A* when not installed).
|
|
2
2
|
// engine.use(level({ url }), collision(), characters(), nav({ agentRadius: 0.4 }));
|
|
3
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
|
|
4
|
+
// version: +1 on every rebuild / unload. Native objects (crowd agents etc.) are destroyed on rebuild; code that cached them keeps the object and compares it (NPCs compare crowd): version counts per module, so a replaced nav can show the same number.
|
|
5
5
|
//
|
|
6
|
-
// Where the mesh comes from: the ground API's
|
|
6
|
+
// Where the mesh comes from: the ground API's staticTrimeshes() (the level's walkable mesh).
|
|
7
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
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 (
|
|
9
|
+
// Levels only for now (ground with staticTrimeshes); procedural terrain keeps using A*. Recast (728 KB, 218 KB gzipped) starts loading when nav() is installed.
|
|
10
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
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
12
|
// characters.findPath then uses A*, which resamples the edited area (its rasters are dropped on 'ground:change').
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
// Body sizes: Recast bakes the body's radius into the mesh, so a mesh is good for one size (Recast's own advice, and what Unreal's supported agents do: one mesh per size).
|
|
14
|
+
// api itself is the mesh of agentRadius; a findPath for another radius gets its own, built the first time that size is asked for and kept in api.meshes until the next rebuild or unload.
|
|
15
|
+
// Radii that round to the same cells share one. Each has its own tile cache, obstacles and seated links; revision covers them all, and a game with one size pays for nothing else.
|
|
16
|
+
// shortcut: built on the main thread when first asked for, which stalls that step; move to a worker (or a list of sizes built at load) if a real level's build shows up in a frame.
|
|
17
|
+
// Links (Recast off-mesh connections): nav({ links: [{ id, from, to }] }) joins two places a walker cannot walk between, a gap to jump or a ladder to climb.
|
|
18
|
+
// generateTileCache has no offMeshConnections option (generateTiledNavMesh does), so they go in through the tile cache's mesh process, which Detour runs for
|
|
19
|
+
// every tile it builds: a tile rebuilt around an obstacle gets them back the same way. They live in the tile data, so exportMesh carries them to the path worker.
|
|
20
|
+
import { readParams, keysSchema } from '../../core/base/params.js';
|
|
21
|
+
import { xzOf, yOf } from '../../core/base/math.js';
|
|
22
|
+
import { follows } from '../../core/base/follows.js';
|
|
23
|
+
import { navSearch, OPTS } from './navPath.js';
|
|
24
|
+
import { sameFloor } from '../../core/scene/floor.js';
|
|
25
|
+
import { warnOnce } from '../../core/base/validate.js';
|
|
26
|
+
import { coded, codedError } from '../../core/base/errorHints.js';
|
|
18
27
|
|
|
19
|
-
const NAV_PARAMS = keysSchema(['cellSize', 'agentRadius', 'agentHeight', 'maxClimb', 'maxSlope', 'tileSize', 'crowd', 'debug', 'maxObstacles', 'maxAgents', 'arrive']);
|
|
28
|
+
const NAV_PARAMS = keysSchema(['cellSize', 'agentRadius', 'agentHeight', 'maxClimb', 'maxSlope', 'tileSize', 'crowd', 'debug', 'maxObstacles', 'maxAgents', 'arrive', 'links']);
|
|
29
|
+
const LINK_PARAMS = keysSchema(['id', 'from', 'to', 'oneWay', 'radius']);
|
|
30
|
+
const ANY_FLOOR = { halfExtents: { ...OPTS.halfExtents, y: 1e4 } }; // the search's reach sideways, every floor up and down: what stands at this spot, however far the height given is
|
|
31
|
+
const BASE = 'agent'; // the size of the mesh cut for agentRadius, which is api itself: cells alone can't name it, a wider body in the same cells needs more room round an obstacle
|
|
32
|
+
const NO_LINK = -1; // Detour's DT_NULL_LINK, the end of a polygon's link list: 0xffffffff in C++, but the wrapper reads it back signed
|
|
20
33
|
|
|
21
34
|
export function nav(opts = {}) {
|
|
22
35
|
readParams('nav', NAV_PARAMS, opts);
|
|
23
36
|
// maxObstacles / maxAgents: Recast's fixed capacities (a city of 3000 wall pieces needs more than a village; a crowd scene more than 128 agents).
|
|
24
37
|
// arrive: meters past the character's radius a path may end short of its goal and still count as arrived (a ledge-strict platformer wants less)
|
|
25
|
-
const { cellSize = 0.3, tileSize = 32, crowd = false, debug = false, maxObstacles = 1024, maxAgents = 128, arrive = 0.6 } = opts;
|
|
38
|
+
const { cellSize = 0.3, tileSize = 32, crowd = false, debug = false, maxObstacles = 1024, maxAgents = 128, arrive = 0.6, links = [] } = opts;
|
|
39
|
+
for (const l of links) readParams('nav links', LINK_PARAMS, l);
|
|
26
40
|
// agent size defaults come from engine.movement (filled at install); opts values override them
|
|
27
41
|
let agentRadius, agentHeight, maxClimb, maxSlope;
|
|
42
|
+
let revision = 0;
|
|
28
43
|
let e = null, lib = null, gen = null, pending = false, wasm = null; // wasm: the recast import + init begun at install
|
|
29
|
-
|
|
44
|
+
const processes = new Map(); // size -> what runs for every tile built, with links only: without them the generator's default is the same thing
|
|
45
|
+
const cellsOf = r => Math.ceil(+(r / cellSize).toFixed(6)); // rounded first: 0.9 / 0.3 is a hair over 3
|
|
30
46
|
|
|
31
47
|
const api = {
|
|
32
|
-
ready: false, agent: null, navMesh: null, query: null, tileCache: null, crowd: null, helper: null,
|
|
48
|
+
ready: false, agent: null, links: [], navMesh: null, query: null, tileCache: null, crowd: null, helper: null,
|
|
49
|
+
/** @internal this mesh's body: its size in api.meshes, walkableRadius in cells, the radius obstacles are grown by; obstacles: engine.collision.mirror, collider -> its Recast obstacles (big boxes are cut into several pieces), of the collision module obstaclesOf. A mesh of another size has these too */
|
|
50
|
+
size: BASE, cells: 0, radius: 0, obstacles: null, obstaclesOf: follows(() => e?.collision ?? null),
|
|
51
|
+
/** @internal size -> mesh ({ query, navMesh, links, ... }), this one included: async pathfinding looks a batch's mesh up here */
|
|
52
|
+
meshes: new Map(),
|
|
33
53
|
/** +1 on rebuild / unload: code that cached native objects such as crowd agents must re-create them when it changes */
|
|
34
54
|
version: 0,
|
|
35
55
|
/**
|
|
@@ -37,35 +57,60 @@ export function nav(opts = {}) {
|
|
|
37
57
|
* Pass { detailed: true } to get { status: 'ok' | 'noPath' | 'notReady', path } instead: tells "navmesh not generated yet" from "generated but unreachable"
|
|
38
58
|
*/
|
|
39
59
|
findPath(from, to, o = {}) {
|
|
60
|
+
syncObstacles();
|
|
40
61
|
const res = search(from, to, o);
|
|
41
62
|
return o.detailed ? res : res.path;
|
|
42
63
|
},
|
|
43
64
|
/** @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 */
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
65
|
+
/** Any walkable change. A read of it is a read of the mesh, so it syncs first: a frozen engine runs no step, and a cached path asks here */
|
|
66
|
+
get revision() { syncObstacles(); return revision; },
|
|
67
|
+
/** @internal a mesh as bytes, imported by the pathfinding worker */
|
|
68
|
+
exportMesh(m = api) { return lib.exportNavMesh(m.navMesh); },
|
|
69
|
+
/** @internal the mesh for a body of this radius, built the first time it is asked for; no radius: agentRadius. A mesh the build refused has no query, and every path on it is noPath */
|
|
70
|
+
mesh(radius = agentRadius) {
|
|
71
|
+
if (!(radius >= 0)) throw codedError('character-option', `nav: a path's radius is a number of meters, 0 or more, got ${radius}`);
|
|
72
|
+
const c = cellsOf(radius), size = c === api.cells && radius <= agentRadius ? BASE : c;
|
|
73
|
+
return api.meshes.get(size) ?? extraMesh(size);
|
|
74
|
+
},
|
|
75
|
+
/**
|
|
76
|
+
* @internal The one rule for a position given to the navmesh (search, links, async paths, crowd goals). No height: the ground. A height picks the nearest surface of the mesh at that
|
|
77
|
+
* spot, which is what tells the floors of a building apart, and the point lands on it: a crowd target looks only a little way up and down. A height farther than a body from
|
|
78
|
+
* every surface was not meant as a floor ([x, 0, z] on ground at 8), so it also warns. Before the mesh exists it stays as given; seatLinks asks again once it does.
|
|
79
|
+
* m: the mesh the point is for: a passage only a small body fits is on no other
|
|
80
|
+
*/
|
|
81
|
+
point(p, m = api) {
|
|
82
|
+
syncObstacles();
|
|
83
|
+
const [x, z] = xzOf(p), y = yOf(p);
|
|
84
|
+
if (y === undefined) return { x, y: e.surface(x, z), z };
|
|
85
|
+
const near = m.query?.findNearestPoly({ x, y, z }, ANY_FLOOR);
|
|
86
|
+
if (!near?.success || !near.nearestRef) return { x, y, z };
|
|
87
|
+
const floor = near.nearestPoint.y, r = v => +v.toFixed(2);
|
|
88
|
+
if (!sameFloor(y, floor, e.movement.height)) warnOnce(coded('nav-height', `nav: height ${r(y)} at x ${r(x)}, z ${r(z)} is on no floor, so ${r(floor)} is used, the nearest walkable surface there`), 'nav-height');
|
|
89
|
+
return { x, y: floor, z };
|
|
90
|
+
},
|
|
91
|
+
/** @internal how far (m) a search may end from its goal for a body of this radius; the path search and the worker read it */
|
|
48
92
|
reach(radius) { return (radius ?? agentRadius) + arrive; },
|
|
49
93
|
/** Unload the navmesh (frees native objects); afterwards characters.findPath falls back to A* */
|
|
50
94
|
dispose() {
|
|
51
|
-
api.ready = false; api.version++;
|
|
95
|
+
api.ready = false; api.version++; revision++;
|
|
52
96
|
if (api.helper) e.remove(api.helper); // the debug mesh is built by us: remove it completely
|
|
53
|
-
api.crowd?.destroy();
|
|
97
|
+
api.crowd?.destroy();
|
|
98
|
+
for (const m of api.meshes.values()) { m.tileCache?.destroy(); m.navMesh?.destroy(); m.query?.destroy(); }
|
|
54
99
|
Object.assign(api, { navMesh: null, query: null, tileCache: null, crowd: null, helper: null });
|
|
55
|
-
obstacles?.clear();
|
|
100
|
+
api.obstacles?.clear();
|
|
101
|
+
api.meshes.clear(); api.meshes.set(BASE, api);
|
|
56
102
|
},
|
|
57
103
|
};
|
|
58
104
|
|
|
59
105
|
function search(from, to, o) {
|
|
60
106
|
if (!api.ready) return { status: 'notReady', path: null };
|
|
61
|
-
const
|
|
62
|
-
return navSearch(
|
|
107
|
+
const m = api.mesh(o.radius);
|
|
108
|
+
return navSearch(m, api.point(from, m), api.point(to, m), api.reach(o.radius), e.movement.height);
|
|
63
109
|
}
|
|
64
110
|
|
|
65
111
|
/** Walkable triangle mesh of the ground (world coordinates), merged into one */
|
|
66
112
|
function geometry() {
|
|
67
113
|
const G = e.ground;
|
|
68
|
-
if (G.navGeometry) return G.navGeometry();
|
|
69
114
|
const list = G.staticTrimeshes?.() || [];
|
|
70
115
|
let n = 0, m = 0;
|
|
71
116
|
for (const t of list) { n += t.positions.length; m += t.indices.length; }
|
|
@@ -75,36 +120,106 @@ export function nav(opts = {}) {
|
|
|
75
120
|
return { positions, indices };
|
|
76
121
|
}
|
|
77
122
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
123
|
+
/** The links with their ends in 3D (a y left out is the ground there) and the catch radius: the mesh stops the agent radius, rounded up to cells, plus up to a cell short of an edge, and a ladder stands a little off that edge */
|
|
124
|
+
function placeLinks(m) {
|
|
125
|
+
return links.map((l, i) => ({ id: l.id ?? i, from: api.point(l.from, m), to: api.point(l.to, m), oneWay: !!l.oneWay, radius: l.radius ?? m.radius + cellSize * 3 }));
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* The mesh process with the links that start in the tile being built. Detour keeps a connection only in its start's tile, and the wrapper's copy of the list
|
|
129
|
+
* is never freed (nothing runs after this callback), so passing only those keeps that to a few bytes per link instead of the whole list for every tile.
|
|
130
|
+
*/
|
|
131
|
+
function linkProcess(size) {
|
|
132
|
+
if (!processes.has(size)) processes.set(size, new lib.TileCacheMeshProcess((params, areas, flags) => {
|
|
133
|
+
for (let i = 0; i < params.polyCount(); i++) { areas.set(i, 0); flags.set(i, 1); }
|
|
134
|
+
const raw = params.raw, inside = (v, a) => v >= raw.get_bmin(a) && v < raw.get_bmax(a), mine = [];
|
|
135
|
+
api.meshes.get(size).links.forEach((l, i) => { if (inside(l.from.x, 0) && inside(l.from.z, 2)) mine.push({ startPosition: l.from, endPosition: l.to, radius: l.radius, bidirectional: !l.oneWay, userId: i }); });
|
|
136
|
+
params.setOffMeshConnections(mine);
|
|
137
|
+
}));
|
|
138
|
+
return processes.get(size);
|
|
139
|
+
}
|
|
140
|
+
/** The indexes of the links attached at both ends */
|
|
141
|
+
function joinedLinks(m) {
|
|
142
|
+
const mesh = m.navMesh, joined = new Set();
|
|
143
|
+
for (let t = 0; t < mesh.getMaxTiles(); t++) {
|
|
144
|
+
const tile = mesh.getTile(t), h = tile.header();
|
|
145
|
+
for (let i = 0; h && i < h.offMeshConCount(); i++) {
|
|
146
|
+
let ends = 0;
|
|
147
|
+
for (let k = tile.polys(h.offMeshBase() + i).firstLink(); (k | 0) !== NO_LINK; k = tile.links(k).next()) ends++;
|
|
148
|
+
if (ends >= 2) joined.add(tile.offMeshCons(i).userId());
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return joined;
|
|
152
|
+
}
|
|
153
|
+
/** Where an end meets the ground it reaches: on that ground and inside its tile (a tile's far edge belongs to the next one), or the end as given when it reaches none */
|
|
154
|
+
function seat(m, p, radius) {
|
|
155
|
+
const near = m.query.findNearestPoly(p, { halfExtents: { x: radius, y: maxClimb, z: radius } });
|
|
156
|
+
if (!near.success || !near.nearestRef) return p;
|
|
157
|
+
const h = m.navMesh.getTileAndPolyByRef(near.nearestRef).tile.header(), { x, y, z } = near.nearestPoint, pad = cellSize / 4;
|
|
158
|
+
const within = (v, axis) => Math.min(Math.max(v, h.bmin(axis) + pad), h.bmax(axis) - pad);
|
|
159
|
+
return { x: within(x, 0), y, z: within(z, 2) };
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Links on the finished mesh. Detour files an end under the tile its x, z fall in and looks for ground in that tile alone, so an end beside a tile's edge with its
|
|
163
|
+
* ground across the edge stays loose (a ladder under a deck that starts in the next tile), and a start in a tile with no ground is dropped. Each end moves onto its
|
|
164
|
+
* ground, and the tiles its start left and joined are rebuilt with it. m.links is then where walkers really step on and land, which a crowd NPC reads. On the ground
|
|
165
|
+
* an end needs no reach beyond a cell or two, so an obstacle standing on it cuts the link and does not leave it hooked to ground past the obstacle.
|
|
166
|
+
* A link Detour still could not attach at both ends does nothing, silently: say so once, at the build. Only for agentRadius: a ledge too narrow for a wide body is that body's limit, not the game's mistake.
|
|
167
|
+
*/
|
|
168
|
+
function seatLinks(m) {
|
|
169
|
+
if (!m.links.length) return;
|
|
170
|
+
const tiles = new Map(), note = p => { const t = m.navMesh.calcTileLoc(p), x = t.tileX(), y = t.tileY(); tiles.set(`${x},${y}`, [x, y]); };
|
|
171
|
+
for (const l of m.links) { note(l.from); l.from = seat(m, api.point(l.from, m), l.radius); l.to = seat(m, api.point(l.to, m), l.radius); l.radius = cellSize * 2; note(l.from); }
|
|
172
|
+
for (const [x, y] of tiles.values()) m.tileCache.buildNavMeshTilesAt(x, y, m.navMesh);
|
|
173
|
+
if (m !== api) return;
|
|
174
|
+
const joined = joinedLinks(m);
|
|
175
|
+
m.links.forEach((l, i) => { if (!joined.has(i)) warnOnce(coded('nav-link', `nav: link "${l.id}" does not join the mesh at both ends, so no path uses it`)); });
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Cut the mesh of m's body out of the ground; false when Recast refuses */
|
|
179
|
+
function cut(m, { positions, indices }) {
|
|
83
180
|
const ch = cellSize / 2;
|
|
181
|
+
m.links = placeLinks(m);
|
|
84
182
|
const r = gen.generateTileCache(positions, indices, {
|
|
85
|
-
cs: cellSize, ch, tileSize, expectedLayersPerTile: 4, maxObstacles,
|
|
86
|
-
walkableSlopeAngle: maxSlope, walkableHeight: Math.ceil(agentHeight / ch), walkableClimb: Math.ceil(maxClimb / ch), walkableRadius:
|
|
183
|
+
cs: cellSize, ch, tileSize, expectedLayersPerTile: 4, maxObstacles, tileCacheMeshProcess: links.length ? linkProcess(m.size) : undefined,
|
|
184
|
+
walkableSlopeAngle: maxSlope, walkableHeight: Math.ceil(agentHeight / ch), walkableClimb: Math.ceil(maxClimb / ch), walkableRadius: m.cells,
|
|
87
185
|
});
|
|
88
|
-
if (!r.success) { warnOnce(coded('nav-build', 'nav: ' + r.error)); return; }
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
186
|
+
if (!r.success) { warnOnce(coded('nav-build', 'nav: ' + r.error)); return false; }
|
|
187
|
+
m.tileCache = r.tileCache; m.navMesh = r.navMesh;
|
|
188
|
+
m.query = new lib.NavMeshQuery(r.navMesh);
|
|
189
|
+
seatLinks(m);
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** The mesh of a size no one asked for before. The whole cell counts as its radius: it stands for every body that rounds to it. It takes no part in revision: nothing walkable changed, and async pathfinding sends each size when a batch first needs it */
|
|
194
|
+
function extraMesh(size) {
|
|
195
|
+
const m = { size, cells: size, radius: size * cellSize, links: [], tileCache: null, navMesh: null, query: null, obstacles: null, obstaclesOf: follows(() => e.collision ?? null) };
|
|
196
|
+
api.meshes.set(size, m);
|
|
197
|
+
if (cut(m, geometry())) syncMesh(m);
|
|
198
|
+
return m;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
async function build() {
|
|
202
|
+
pending = false;
|
|
203
|
+
if (api.ready) api.dispose();
|
|
204
|
+
const ground = geometry();
|
|
205
|
+
if (!ground.indices.length || !cut(api, ground)) return;
|
|
206
|
+
if (crowd) api.crowd = new lib.Crowd(api.navMesh, { maxAgents, maxAgentRadius: agentRadius * 2 });
|
|
207
|
+
api.ready = true; api.version++; revision++;
|
|
93
208
|
syncObstacles();
|
|
94
209
|
if (debug) {
|
|
95
210
|
const three = await import('@recast-navigation/three');
|
|
96
|
-
api.helper = new three.NavMeshHelper(
|
|
211
|
+
api.helper = new three.NavMeshHelper(api.navMesh);
|
|
97
212
|
api.helper.position.y = 0.05; e.add(api.helper, { owner: 'engine:nav' });
|
|
98
213
|
}
|
|
99
214
|
}
|
|
100
215
|
|
|
101
216
|
// collision-module colliders -> TileCache obstacles (engine.collision.mirror diffs add / remove, shared with rigid), then rebuild affected tiles
|
|
102
|
-
function obstacle(type, o) {
|
|
103
|
-
const tc =
|
|
217
|
+
function obstacle(m, type, o) {
|
|
218
|
+
const tc = m.tileCache, list = [];
|
|
104
219
|
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
|
|
105
220
|
if (type === 'box') {
|
|
106
221
|
// one obstacle affects at most 8 tiles: cut big boxes into pieces no larger than half a tile (along the box's own orientation)
|
|
107
|
-
const hw = o.hw +
|
|
222
|
+
const hw = o.hw + m.radius, hd = o.hd + m.radius, piece = tileSize * cellSize / 2; // add the agent radius so agents don't hug walls
|
|
108
223
|
const nx = Math.ceil(hw * 2 / piece), nz = Math.ceil(hd * 2 / piece), c = Math.cos(o.rot), sn = Math.sin(o.rot);
|
|
109
224
|
for (let i = 0; i < nx; i++) for (let j = 0; j < nz; j++) {
|
|
110
225
|
const lx = -hw + (i + 0.5) * hw * 2 / nx, lz = -hd + (j + 0.5) * hd * 2 / nz;
|
|
@@ -112,34 +227,48 @@ export function nav(opts = {}) {
|
|
|
112
227
|
if (res.success) list.push(res.obstacle);
|
|
113
228
|
}
|
|
114
229
|
} else {
|
|
115
|
-
const res = tc.addCylinderObstacle({ x: o.x, y, z: o.z }, o.r +
|
|
230
|
+
const res = tc.addCylinderObstacle({ x: o.x, y, z: o.z }, o.r + m.radius, h);
|
|
116
231
|
if (res.success) list.push(res.obstacle);
|
|
117
232
|
}
|
|
118
233
|
queued += list.length;
|
|
119
|
-
if (queued >=
|
|
234
|
+
if (queued >= BATCH) flush(m);
|
|
120
235
|
return list;
|
|
121
236
|
}
|
|
237
|
+
/** One mesh's obstacles up to date; true when any changed */
|
|
238
|
+
function syncMesh(m) {
|
|
239
|
+
if (!m.tileCache) return false;
|
|
240
|
+
const col = e.collision ?? null, swapped = m.obstaclesOf.sync(() => { m.obstacles?.clear(true); m.obstacles = null; });
|
|
241
|
+
if (!col && !swapped) return false;
|
|
242
|
+
m.obstacles ??= col?.mirror({ add: (type, o) => obstacle(m, type, o), remove: list => { for (const ob of list) { m.tileCache.removeObstacle(ob); if (++queued >= BATCH) flush(m); } } }) ?? null;
|
|
243
|
+
if (!m.obstacles?.sync() && !swapped) return false;
|
|
244
|
+
flush(m);
|
|
245
|
+
return true;
|
|
246
|
+
}
|
|
122
247
|
function syncObstacles() {
|
|
123
|
-
if (!api.ready
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
248
|
+
if (!api.ready) return;
|
|
249
|
+
let changed = false;
|
|
250
|
+
for (const m of api.meshes.values()) changed = syncMesh(m) || changed;
|
|
251
|
+
if (!changed) return;
|
|
252
|
+
revision++;
|
|
128
253
|
api.helper?.update();
|
|
129
254
|
}
|
|
130
|
-
//
|
|
255
|
+
// obstacle requests are processed once a batch has built up, until all tiles are rebuilt
|
|
256
|
+
const BATCH = 48;
|
|
131
257
|
let queued = 0;
|
|
132
|
-
function flush() { queued = 0; for (let i = 0; i < 256 && !
|
|
258
|
+
function flush(m) { queued = 0; for (let i = 0; i < 256 && !m.tileCache.update(m.navMesh).upToDate; i++); }
|
|
133
259
|
|
|
134
260
|
return {
|
|
135
|
-
name: 'nav', type: 'world', provides: ['nav'], optional: { collision: 'walls and obstacles are not cut out of the navmesh' }, params: NAV_PARAMS, options: opts, label: 'Navmesh',
|
|
261
|
+
name: 'nav', skipKind: 'none', type: 'world', provides: ['nav'], optional: { collision: 'walls and obstacles are not cut out of the navmesh', level: 'no walkable mesh to build from, so paths stay on A*' }, params: NAV_PARAMS, options: opts, label: 'Navmesh',
|
|
136
262
|
install(engine, ctx) {
|
|
137
263
|
e = engine;
|
|
138
264
|
e.i18n.add({ 'zh-CN': { 'module.nav': '导航网格' }, en: { 'module.nav': 'navmesh' } });
|
|
139
265
|
const mv = e.movement;
|
|
140
266
|
({ agentRadius = mv.radius, agentHeight = mv.height, maxClimb = mv.stepHeight, maxSlope = mv.maxSlope } = opts);
|
|
141
267
|
api.agent = { radius: agentRadius, height: agentHeight, maxClimb, maxSlope };
|
|
268
|
+
api.radius = agentRadius; api.cells = cellsOf(agentRadius); api.meshes.set(BASE, api);
|
|
142
269
|
ctx.track(e.events.on('level:loaded', () => { if (lib) build(); else pending = true; }));
|
|
270
|
+
ctx.track(e.events.on('level:unloaded', () => { pending = false; if (api.ready) api.dispose(); })); // every size was cut from the level that left
|
|
271
|
+
pending = !!e.level?.ready; // installed after the level loaded: that event is gone, init builds the mesh
|
|
143
272
|
// fetch and compile the WASM from install on, so it loads while the page builds the world instead of inside start()'s module inits
|
|
144
273
|
wasm = Promise.all([import('@recast-navigation/core'), import('@recast-navigation/generators')]).then(async ([l, g]) => { await l.init(); return [l, g]; });
|
|
145
274
|
wasm.catch(() => {}); // a failure is reported by init (which awaits the same promise), not as an unhandled rejection here
|
|
@@ -154,9 +283,11 @@ export function nav(opts = {}) {
|
|
|
154
283
|
syncObstacles();
|
|
155
284
|
api.crowd?.update(dt);
|
|
156
285
|
},
|
|
157
|
-
describe() { return { ready: api.ready, version: api.version, obstacles: obstacles?.size ?? 0 }; },
|
|
286
|
+
describe() { return { ready: api.ready, version: api.version, obstacles: api.obstacles?.size ?? 0, links: api.links.length, meshes: api.ready ? api.meshes.size : 0 }; },
|
|
158
287
|
// engine.dispose(): the Recast navMesh / query / tileCache / crowd live in the WASM heap, not in JS, so the garbage collector never frees
|
|
159
288
|
// them; an editor or test that makes engines over and over would grow that heap every time
|
|
160
289
|
dispose() { api.dispose(); },
|
|
161
290
|
};
|
|
162
291
|
}
|
|
292
|
+
|
|
293
|
+
export const handRolled = 'none: a pathfinder of its own is explicitly allowed';
|
package/modules/nav/navPath.js
CHANGED
|
@@ -1,17 +1,52 @@
|
|
|
1
|
-
// Navmesh path query: the one Recast search in the engine. engine.nav.findPath calls it with the live
|
|
2
|
-
// the pathfinding worker (characters/pathJob.js) with
|
|
1
|
+
// Navmesh path query: the one Recast search in the engine. engine.nav.findPath calls it with the live mesh,
|
|
2
|
+
// the pathfinding worker (characters/pathJob.js) with an imported copy of the same mesh. Worker-safe: imports only core/scene/floor.js.
|
|
3
|
+
// computePath would do, but it drops the flags that say a leg is a link; this is its search with the flags kept.
|
|
4
|
+
|
|
5
|
+
import { sameFloor } from '../../core/scene/floor.js';
|
|
6
|
+
|
|
7
|
+
export const OPTS = { halfExtents: { x: 2, y: 4, z: 2 } };
|
|
8
|
+
const OFF_MESH = 4; // DT_STRAIGHTPATH_OFFMESH_CONNECTION: this point starts a link, the next one ends it
|
|
9
|
+
const OFF_MESH_POLY = 1; // DT_POLYTYPE_OFFMESH_CONNECTION
|
|
10
|
+
const NO_PATH = { status: 'noPath', path: null };
|
|
3
11
|
|
|
4
12
|
/**
|
|
5
|
-
* @param {any} query a
|
|
13
|
+
* @param {{ query: any, navMesh: any, links: { id: string | number }[] } | undefined} nav Recast NavMeshQuery and NavMesh, and the links by the index their connections carry; none or no query: a body no mesh could be cut for
|
|
6
14
|
* @param {{ x: number, y: number, z: number }} s start (y = ground height, looked up by the caller: the worker has no ground)
|
|
7
15
|
* @param {{ x: number, y: number, z: number }} g goal
|
|
8
16
|
* @param {number} reach how far the path's end may stop short of the goal and still count as reaching it (m)
|
|
9
|
-
* @
|
|
17
|
+
* @param {number} body character height: the end must be on the goal's floor
|
|
18
|
+
* @returns {{ status: 'ok' | 'noPath', path: { x: number, y: number, z: number, link?: { id: string | number, from: object, to: object } }[] | null }}
|
|
10
19
|
*/
|
|
11
|
-
export function navSearch(
|
|
12
|
-
const
|
|
13
|
-
if (!
|
|
14
|
-
const
|
|
15
|
-
if (
|
|
16
|
-
|
|
20
|
+
export function navSearch(nav, s, g, reach, body) {
|
|
21
|
+
const q = nav?.query;
|
|
22
|
+
if (!q) return NO_PATH;
|
|
23
|
+
const a = q.findNearestPoly(s, OPTS), b = q.findNearestPoly(g, OPTS);
|
|
24
|
+
if (!a.success || !b.success) return NO_PATH;
|
|
25
|
+
const found = q.findPath(a.nearestRef, b.nearestRef, s, g), polys = found.polys;
|
|
26
|
+
let out = NO_PATH;
|
|
27
|
+
if (found.success && polys.size) {
|
|
28
|
+
const last = polys.get(polys.size - 1), close = last === b.nearestRef ? null : q.closestPointOnPoly(last, g);
|
|
29
|
+
if (!close || close.success) out = corners(nav, s, close ? close.closestPoint : g, polys, g, reach, body);
|
|
30
|
+
}
|
|
31
|
+
polys.destroy();
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The corners of a polygon corridor as path points; the point that starts a link says so, with both ends in 3D */
|
|
36
|
+
function corners(nav, s, end, polys, g, reach, body) {
|
|
37
|
+
const r = nav.query.findStraightPath(s, end, polys), { straightPath: v, straightPathFlags: flags, straightPathRefs: refs, straightPathCount: n } = r;
|
|
38
|
+
const at = i => ({ x: v.get(i * 3), y: v.get(i * 3 + 1), z: v.get(i * 3 + 2) });
|
|
39
|
+
// A walker standing on a link's start (it has just landed there, say) gets no corner for it: the funnel's apex is already on it, so only the corridor still shows the link
|
|
40
|
+
const standing = r.success && n > 1 && polys.size > 1 && !((flags.get(0) | flags.get(1)) & OFF_MESH) && nav.navMesh.getTileAndPolyByRef(polys.get(1)).poly.getType() === OFF_MESH_POLY ? polys.get(1) : 0;
|
|
41
|
+
const path = [];
|
|
42
|
+
for (let i = 0; r.success && i < n; i++) {
|
|
43
|
+
const p = at(i), ref = flags.get(i) & OFF_MESH ? refs.get(i) : i ? 0 : standing;
|
|
44
|
+
if (ref && i + 1 < n) { const u = nav.navMesh.getOffMeshConnectionByRef(ref).userId(); p.link = { id: nav.links[u]?.id ?? u, from: at(i), to: at(i + 1) }; }
|
|
45
|
+
path.push(p);
|
|
46
|
+
}
|
|
47
|
+
v.destroy(); flags.destroy(); refs.destroy();
|
|
48
|
+
const tail = path[path.length - 1];
|
|
49
|
+
// end point unreachable: too far off, or right under it, where Detour's best try at a goal cut off upstairs ends
|
|
50
|
+
if (!tail || Math.hypot(tail.x - g.x, tail.z - g.z) > reach || !sameFloor(tail.y, g.y, body)) return NO_PATH;
|
|
51
|
+
return { status: 'ok', path };
|
|
17
52
|
}
|
package/modules/net/Client.js
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
// prediction / rollback is a later step).
|
|
8
8
|
// - world.simulate does not run here: the host simulates and the result arrives as diffs (two simulations would fight).
|
|
9
9
|
import { applyPart, idMap } from './sync.js';
|
|
10
|
-
import { failure } from '../../core/opsFail.js';
|
|
10
|
+
import { failure } from '../../core/ops/opsFail.js';
|
|
11
11
|
|
|
12
12
|
const STEP = 1 / 60;
|
|
13
13
|
const ASK_EVERY = 30; // steps between snapshot requests while out of step
|
|
@@ -34,7 +34,7 @@ export class NetClient {
|
|
|
34
34
|
this.t?.close?.();
|
|
35
35
|
this.t = t; this.connected = false; this.synced = false; this.n = -1; this.inbox.length = 0;
|
|
36
36
|
this.subs = [
|
|
37
|
-
t.on('open', () => { this.connected = true; this.since = this.e.tick; this.e.events.emit('net:open', {}); }),
|
|
37
|
+
t.on('open', () => { this.connected = true; this.since = this.e.time.tick; this.e.events.emit('net:open', {}); }),
|
|
38
38
|
t.on('close', () => { this.connected = false; this.synced = false; this.e.events.emit('net:close', {}); }),
|
|
39
39
|
t.on('message', m => { if (m && typeof m === 'object') { this.stats.in++; this.inbox.push(m); } }),
|
|
40
40
|
];
|
|
@@ -60,7 +60,7 @@ export class NetClient {
|
|
|
60
60
|
const box = this.inbox.splice(0);
|
|
61
61
|
e.ops.allow(() => { for (const m of box) this.handle(m); });
|
|
62
62
|
}
|
|
63
|
-
if (this.synced && e.tick % this.o.posEvery === 0) this.sendPosition();
|
|
63
|
+
if (this.synced && e.time.tick % this.o.posEvery === 0) this.sendPosition();
|
|
64
64
|
}
|
|
65
65
|
handle(m) {
|
|
66
66
|
const e = this.e;
|
|
@@ -86,7 +86,7 @@ export class NetClient {
|
|
|
86
86
|
/** a diff was lost: stop applying diffs and ask for a snapshot (frame asks again if that is lost too) */
|
|
87
87
|
gap() {
|
|
88
88
|
this.synced = false; this.stats.gaps++;
|
|
89
|
-
this.since = this.e.tick;
|
|
89
|
+
this.since = this.e.time.tick;
|
|
90
90
|
this.send({ t: 'resync' });
|
|
91
91
|
}
|
|
92
92
|
apply(parts, full) {
|
|
@@ -104,7 +104,7 @@ export class NetClient {
|
|
|
104
104
|
}
|
|
105
105
|
}
|
|
106
106
|
/**
|
|
107
|
-
* A character the host drives (an NPC, another player's avatar): net holds its control slot for good (core/control.js, layer 'net'), so a
|
|
107
|
+
* A character the host drives (an NPC, another player's avatar): net holds its control slot for good (core/ops/control.js, layer 'net'), so a
|
|
108
108
|
* local walkTo / cutscene / script on it is a control-net error instead of a move the next diff silently undoes. Own avatar: ours, never held.
|
|
109
109
|
*/
|
|
110
110
|
hold(id) {
|
|
@@ -118,14 +118,14 @@ export class NetClient {
|
|
|
118
118
|
const p = rec.position, f = {};
|
|
119
119
|
for (const k of this.o.avatarFields) if (k in rec) f[k] = rec[k];
|
|
120
120
|
const key = `${p.x},${p.y},${p.z},${JSON.stringify(f)}`;
|
|
121
|
-
if (key === this.lastPos && this.e.tick % 60) return; // unchanged: resend once a second anyway, in case the last one was lost
|
|
121
|
+
if (key === this.lastPos && this.e.time.tick % 60) return; // unchanged: resend once a second anyway, in case the last one was lost
|
|
122
122
|
this.lastPos = key;
|
|
123
123
|
this.send({ t: 'p', p: [p.x, p.y, p.z], f });
|
|
124
124
|
}
|
|
125
125
|
|
|
126
126
|
frame(dt) {
|
|
127
127
|
// out of step (snapshot or the request for it lost, or the first one never came): ask again
|
|
128
|
-
if (!this.synced && this.connected && this.e.tick - this.since >= ASK_EVERY) { this.since = this.e.tick; this.send({ t: 'resync' }); }
|
|
128
|
+
if (!this.synced && this.connected && this.e.time.tick - this.since >= ASK_EVERY) { this.since = this.e.time.tick; this.send({ t: 'resync' }); }
|
|
129
129
|
if (!this.glides.size) return;
|
|
130
130
|
const W = this.e.world;
|
|
131
131
|
for (const [id, g] of this.glides) {
|
package/modules/net/Host.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// -> owner (op.owner(args) names a world record whose owner must be this peer's actor) -> ops.run with source 'net' and the actor
|
|
5
5
|
// taken from the connection, never from the message.
|
|
6
6
|
// Replies (op results) go out after the next state diff, so a client sees the new state before it hears "done".
|
|
7
|
-
import { checkArgs } from '../../core/opsCheck.js';
|
|
7
|
+
import { checkArgs } from '../../core/ops/opsCheck.js';
|
|
8
8
|
import { partKeys, diffParts, snapshotParts } from './sync.js';
|
|
9
9
|
|
|
10
10
|
const STEP = 1 / 60;
|
|
@@ -28,7 +28,7 @@ export class NetHost {
|
|
|
28
28
|
|
|
29
29
|
join(id) {
|
|
30
30
|
const e = this.e, o = this.o;
|
|
31
|
-
const peer = { id, actor: o.actorFor(id), tokens: o.burst, results: [], posTick: e.tick };
|
|
31
|
+
const peer = { id, actor: o.actorFor(id), tokens: o.burst, results: [], posTick: e.time.tick };
|
|
32
32
|
this.peers.set(id, peer);
|
|
33
33
|
e.ops.allow(() => o.join(e, peer)); // setting up the peer's avatar / bag is host bookkeeping, not a gameplay action
|
|
34
34
|
e.events.emit('net:join', { peer: id, actor: peer.actor });
|
|
@@ -44,7 +44,7 @@ export class NetHost {
|
|
|
44
44
|
snapshot(peer) {
|
|
45
45
|
this.keys ??= partKeys(this.e, this.o.sync);
|
|
46
46
|
this.stats.snapshots++;
|
|
47
|
-
this.send({ t: 's', n: this.n, tick: this.e.tick, you: peer.actor, every: this.o.syncEvery, parts: snapshotParts(this.e, this.keys) }, peer);
|
|
47
|
+
this.send({ t: 's', n: this.n, tick: this.e.time.tick, you: peer.actor, every: this.o.syncEvery, parts: snapshotParts(this.e, this.keys) }, peer);
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
receive(m, from) {
|
|
@@ -67,7 +67,7 @@ export class NetHost {
|
|
|
67
67
|
|
|
68
68
|
request(peer, m) {
|
|
69
69
|
const e = this.e, seq = isNum(m.seq) ? m.seq : 0, name = typeof m.name === 'string' ? m.name : '';
|
|
70
|
-
const reply = (ok, code, value) => peer.results.push({ t: 'r', seq, name, ok, code: code ?? null, value: plain(value), tick: e.tick });
|
|
70
|
+
const reply = (ok, code, value) => peer.results.push({ t: 'r', seq, name, ok, code: code ?? null, value: plain(value), tick: e.time.tick });
|
|
71
71
|
const d = e.ops.defs.get(name), args = m.args ?? {};
|
|
72
72
|
const reason = this.gate(peer, d, args);
|
|
73
73
|
if (reason) {
|
|
@@ -97,8 +97,8 @@ export class NetHost {
|
|
|
97
97
|
position(peer, m) {
|
|
98
98
|
const e = this.e, rec = e.world?.get(peer.actor), p = m.p;
|
|
99
99
|
if (!rec || !Array.isArray(p) || p.length !== 3 || !p.every(isNum)) return;
|
|
100
|
-
const dt = Math.min(0.25, Math.max(STEP, (e.tick - peer.posTick) * STEP)); // idle time earns no long jump
|
|
101
|
-
peer.posTick = e.tick;
|
|
100
|
+
const dt = Math.min(0.25, Math.max(STEP, (e.time.tick - peer.posTick) * STEP)); // idle time earns no long jump
|
|
101
|
+
peer.posTick = e.time.tick;
|
|
102
102
|
const c = rec.position, dx = p[0] - c.x, dy = p[1] - c.y, dz = p[2] - c.z, d = Math.hypot(dx, dy, dz), max = this.o.maxSpeed * dt + 0.5;
|
|
103
103
|
const k = d > max ? max / d : 1;
|
|
104
104
|
e.ops.allow(() => {
|
|
@@ -115,7 +115,7 @@ export class NetHost {
|
|
|
115
115
|
if (++this.steps % o.syncEvery) return;
|
|
116
116
|
this.keys ??= partKeys(this.e, o.sync);
|
|
117
117
|
const parts = diffParts(this.e, this.keys, this.base);
|
|
118
|
-
if (parts) { this.n++; this.stats.diffs++; this.lastSent = this.steps; for (const p of this.peers.values()) this.send({ t: 'd', n: this.n, tick: this.e.tick, parts }, p); }
|
|
118
|
+
if (parts) { this.n++; this.stats.diffs++; this.lastSent = this.steps; for (const p of this.peers.values()) this.send({ t: 'd', n: this.n, tick: this.e.time.tick, parts }, p); }
|
|
119
119
|
else if (this.steps - this.lastSent >= o.keepAlive) { this.lastSent = this.steps; for (const p of this.peers.values()) this.send({ t: 'k', n: this.n }, p); } // lets a client notice a lost last diff
|
|
120
120
|
for (const p of this.peers.values()) { for (const r of p.results) this.send(r, p); p.results.length = 0; }
|
|
121
121
|
}
|
package/modules/net/Net.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// The host runs the game as usual; clients mirror its state and send op requests. Host.js / Client.js hold each side; sync.js what is synced.
|
|
3
3
|
import { NetHost } from './Host.js';
|
|
4
4
|
import { NetClient } from './Client.js';
|
|
5
|
-
import { codedError } from '../../core/errorHints.js';
|
|
5
|
+
import { codedError } from '../../core/base/errorHints.js';
|
|
6
6
|
|
|
7
7
|
/** engine.net (installed by net()): role, who this machine is, connection state and counters. Changes go through engine.ops, never through net. @experimental */
|
|
8
8
|
export class Net {
|
package/modules/net/index.js
CHANGED
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
// engine.use(net({ role: 'client', transport })) // friends: mirror the host, send op requests
|
|
4
4
|
// Only ops defined with net: 'peer' can be requested by clients (default deny); the actor comes from the connection, not the message.
|
|
5
5
|
// Transports: fakeNetwork() (in-process, tests) and webSocket(url) (with packages/tools/net-relay.mjs); see transports.js for the shape.
|
|
6
|
-
import { readParams, keysSchema } from '../../core/params.js';
|
|
6
|
+
import { readParams, keysSchema } from '../../core/base/params.js';
|
|
7
7
|
import { Net } from './Net.js';
|
|
8
|
-
import { codedError } from '../../core/errorHints.js';
|
|
8
|
+
import { codedError } from '../../core/base/errorHints.js';
|
|
9
|
+
import { fmt } from '../../core/base/validate.js';
|
|
9
10
|
|
|
10
11
|
export { fakeNetwork, webSocket } from './transports.js';
|
|
11
12
|
|
|
@@ -45,7 +46,7 @@ const NET_PARAMS = keysSchema(OPTS);
|
|
|
45
46
|
*/
|
|
46
47
|
export function net(opts = {}) {
|
|
47
48
|
readParams('net', NET_PARAMS, opts);
|
|
48
|
-
if (opts.role !== 'host' && opts.role !== 'client') throw codedError('net-option', STRINGS.role
|
|
49
|
+
if (opts.role !== 'host' && opts.role !== 'client') throw codedError('net-option', fmt(STRINGS.role, { got: String(JSON.stringify(opts.role)) }));
|
|
49
50
|
if (!opts.transport) throw codedError('net-option', STRINGS.transport);
|
|
50
51
|
const left = new Map(); // actor -> last position, so a peer that reconnects comes back where it left
|
|
51
52
|
const o = {
|
|
@@ -58,7 +59,7 @@ export function net(opts = {}) {
|
|
|
58
59
|
const at = left.get(peer.actor) ?? e.world.get('player')?.position ?? { x: 0, y: 0, z: 0 };
|
|
59
60
|
e.world.add({ id: peer.actor, kind: 'player', position: { ...at } }, { save: false });
|
|
60
61
|
}
|
|
61
|
-
// the peer drives its avatar: net holds its control slot for good (core/control.js), so a host-side walkTo or cutscene on it is a
|
|
62
|
+
// the peer drives its avatar: net holds its control slot for good (core/ops/control.js), so a host-side walkTo or cutscene on it is a
|
|
62
63
|
// control-net error, not a fight with the peer's next position (the record leaving the world drops the hold)
|
|
63
64
|
if (o.avatar && e.world?.get(peer.actor)) o.control.take(peer.actor, { owner: 'net', layer: 'net' });
|
|
64
65
|
if (o.avatar && e.inventory) e.inventory.container(peer.actor);
|
|
@@ -71,7 +72,7 @@ export function net(opts = {}) {
|
|
|
71
72
|
};
|
|
72
73
|
let n = null;
|
|
73
74
|
return {
|
|
74
|
-
name: 'net', type: 'net', params: NET_PARAMS, options: opts, label: 'Multiplayer', stage: 'late', optional: ['world', 'inventory'],
|
|
75
|
+
name: 'net', skipKind: 'none', type: 'net', params: NET_PARAMS, options: opts, label: 'Multiplayer', stage: 'late', optional: ['world', 'inventory'],
|
|
75
76
|
// ctx.control: the holds on remote characters are the module's, so removing net hands them back
|
|
76
77
|
install(e, ctx) { e.i18n.add({ 'zh-CN': { 'module.net': '联机' }, en: { 'module.net': 'multiplayer' } }); o.control = ctx.control; return (n = new Net(e, o)); },
|
|
77
78
|
fixed(dt) { n._fixed(dt); },
|
package/modules/net/sync.js
CHANGED
|
@@ -49,7 +49,7 @@ export function diffParts(e, keys, base) {
|
|
|
49
49
|
return out;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
/** @internal host: full snapshot of `keys
|
|
52
|
+
/** @internal host: full snapshot of `keys` */
|
|
53
53
|
export function snapshotParts(e, keys) {
|
|
54
54
|
const out = {};
|
|
55
55
|
for (const key of keys) out[key] = readPart(e, key);
|
|
@@ -65,7 +65,7 @@ export function idMap(me) {
|
|
|
65
65
|
|
|
66
66
|
/**
|
|
67
67
|
* @internal client: write one part (full snapshot or diff) into this engine. Runs inside ops.allow (the client guard lets it through).
|
|
68
|
-
* Position changes of interpolated kinds go to `
|
|
68
|
+
* Position changes of interpolated kinds go to `glide(id, to)` instead of moving now; the own avatar keeps its local position (the client drives its own movement).
|
|
69
69
|
*/
|
|
70
70
|
export function applyPart(e, key, part, full, opts) {
|
|
71
71
|
const set = full ? part : part.set ?? {}, del = full ? null : part.del ?? [], { toLocal } = opts;
|
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
// heartbeat pings every heartbeat ms and treats 3 intervals without anything received as dead: it emits 'close' itself, so net:close fires and the
|
|
11
11
|
// game can reconnect. Pings and pongs never reach the net() code. Reconnecting is the game's call by default (engine.net.reconnect(transport) on
|
|
12
12
|
// net:close, the Client's documented path, so the game can show "reconnecting..." or give up); webSocket({ reconnect: true }) retries by itself.
|
|
13
|
-
import { Random } from '../../core/Random.js';
|
|
14
|
-
import { codedError } from '../../core/errorHints.js';
|
|
13
|
+
import { Random } from '../../core/base/Random.js';
|
|
14
|
+
import { codedError } from '../../core/base/errorHints.js';
|
|
15
15
|
|
|
16
16
|
const MISSED = 3; // heartbeat intervals without any message before the link counts as dead
|
|
17
17
|
|