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
|
@@ -7,9 +7,15 @@
|
|
|
7
7
|
// See the top of NPC.js for record fields.
|
|
8
8
|
|
|
9
9
|
import { standAt } from './movers/support.js';
|
|
10
|
+
import { nextPoint, sameLevel, climbTo, startLink, stepLink, landLink, leaveLink, linkAhead, LINK_REACH } from './moveHelpers.js';
|
|
10
11
|
|
|
11
12
|
/** Fixed steps an NPC waits for a findPathAsync answer before asking again (answers normally land 4 steps later; a long queue adds perStep batches) */
|
|
12
13
|
const PATH_WAIT = 300;
|
|
14
|
+
/** Recast's DT_CROWDAGENT_STATE_OFFMESH: the crowd has put the agent on a link */
|
|
15
|
+
const AGENT_ON_LINK = 2;
|
|
16
|
+
const STUCK_SECONDS = 0.5; // no ground gained for this long while walking a path: ask for a new one
|
|
17
|
+
const AGENT_LEASH = 1.5; // an agent farther than this from its NPC (m) is pulled back
|
|
18
|
+
const LINK_WALK = 3; // farther than this from the link the crowd took (m), the NPC was moved away: the crossing is off
|
|
13
19
|
|
|
14
20
|
/**
|
|
15
21
|
* Which stop to go to now (index): the latest stop at or before time t (0-1); before the first stop counts as the previous day's last stop; null if no schedule
|
|
@@ -45,7 +51,7 @@ export function advance(e, W, rec, seconds) {
|
|
|
45
51
|
if (!cal) walk(stopIndex(s, 0), seconds);
|
|
46
52
|
else {
|
|
47
53
|
// channel seconds for a full day (infinite when the calendar is stopped: far NPCs do not walk)
|
|
48
|
-
const k = e.time
|
|
54
|
+
const k = e.time.scale(npcChannel(rec), rec.id), perDay = cal.dayLength * k / e.clock.speed, now = cal.timeOfDay;
|
|
49
55
|
let left = Math.min(1, seconds / perDay); // more than one day counts as one day: every stop is visited once, more would only repeat
|
|
50
56
|
let t = ((now - left) % 1 + 1) % 1;
|
|
51
57
|
for (let guard = 0; left > 1e-9 && guard <= s.length + 1; guard++) {
|
|
@@ -59,7 +65,8 @@ export function advance(e, W, rec, seconds) {
|
|
|
59
65
|
}
|
|
60
66
|
const idx = stopIndex(s, timeOfDay(e)), g = s[idx];
|
|
61
67
|
rec.nav.stop = idx;
|
|
62
|
-
|
|
68
|
+
const moving = Math.hypot(g.x - x, g.z - z) > rec.arriveDistance;
|
|
69
|
+
settle(e, W, rec, x, z, moving, moving ? p.y : g.y ?? p.y); // at the stop, the stop's floor; on the way, the one it walks on
|
|
63
70
|
}
|
|
64
71
|
|
|
65
72
|
/** Set rec.state; with events.traceSize > 0 a change is noted as an `npc:state` trace entry (from, to), so "why did it stop" has a cause chain */
|
|
@@ -70,21 +77,30 @@ function noteState(e, rec, next) {
|
|
|
70
77
|
|
|
71
78
|
/**
|
|
72
79
|
* Settle without a model: write the computed x, z back to data (position, facing, animation state); shared by every behaviour's far. The path is for the model,
|
|
73
|
-
* so it is re-found at the new position after the model is built
|
|
80
|
+
* so it is re-found at the new position after the model is built. refY: the height the ground is looked up from, default where it stood
|
|
74
81
|
*/
|
|
75
|
-
export function settle(e, W, rec, x, z, moving) {
|
|
76
|
-
const
|
|
82
|
+
export function settle(e, W, rec, x, z, moving, refY = rec.position.y) {
|
|
83
|
+
const nav = rec.nav;
|
|
84
|
+
if (nav.link) { ({ x, z } = nav.link.to); W.move(rec.id, nav.link.to); leaveLink(e, rec.id, null, nav.link); nav.link = null; } // no model to cross with: it has crossed
|
|
85
|
+
const p = rec.position; nav.path = null; nav.repath = 0;
|
|
77
86
|
const m = rec.motion ??= {};
|
|
78
87
|
const facing = x !== p.x || z !== p.z ? Math.atan2(x - p.x, z - p.z) : m.facing ?? 0;
|
|
79
88
|
Object.assign(m, { vx: 0, vz: 0, vy: 0, amount: 0, facing, grounded: true, swimming: false, jump: { coyote: 0, used: 0 } });
|
|
80
89
|
noteState(e, rec, moving ? 'walk' : 'idle');
|
|
81
90
|
// NPC of another level: this level's ground height is meaningless for it, keep its y
|
|
82
|
-
const here =
|
|
91
|
+
const here = W.inZone(rec);
|
|
83
92
|
// the same rule the model uses (support.js standAt), from the height it is at: without it an NPC on a deck or an upper floor dropped to the ground in the data
|
|
84
|
-
const y = here ? standAt(e, x, z,
|
|
93
|
+
const y = here ? standAt(e, x, z, refY, rec.look?.stepHeight ?? e.movement.stepHeight).y : p.y;
|
|
85
94
|
W.move(rec.id, { x, y: y === -Infinity ? p.y : y, z });
|
|
86
95
|
}
|
|
87
96
|
|
|
97
|
+
/** One step of a link crossing with a model (rec.nav.link, saved): it runs before the behavior, so no goal, talk or behavior change leaves an NPC hanging over a gap. held: a control holder above has the body, so it lands now (landLink) */
|
|
98
|
+
export function crossLink(W, rec, ch, dt, held = false) {
|
|
99
|
+
if (held ? landLink(ch, rec.nav.link) : stepLink(ch, rec.nav.link, dt)) rec.nav.link = null;
|
|
100
|
+
rec.motion = ch.saveMotion(rec.motion ?? {});
|
|
101
|
+
W.move(rec.id, ch.root.position);
|
|
102
|
+
}
|
|
103
|
+
|
|
88
104
|
/**
|
|
89
105
|
* Near step of a schedule behaviour (every fixed step): pick the stop, goTo it, then copy position and motion state back to data.
|
|
90
106
|
*/
|
|
@@ -96,42 +112,51 @@ export function nearStep(e, W, rec, ch, dt) {
|
|
|
96
112
|
}
|
|
97
113
|
|
|
98
114
|
/**
|
|
99
|
-
* With a model, take one step toward goal ({ x, z }, null = stand): pathfind, move the model via the character's mover, then copy position and motion state
|
|
115
|
+
* With a model, take one step toward goal ({ x, y?, z }, null = stand): pathfind, move the model via the character's mover, then copy position and motion state
|
|
100
116
|
* back to data; shared by every behaviour's near.
|
|
101
117
|
* Standing when in conversation (rec.busy), arrived (distance < arriveDistance) or no goal.
|
|
102
|
-
* Pathfinding goes through
|
|
118
|
+
* Pathfinding goes through engine.characters.findPathAsync; a goal too far (>120 m) first walks straight 100 m, then pathfinds exactly, to avoid
|
|
103
119
|
* one huge search.
|
|
104
120
|
*/
|
|
105
121
|
export function goTo(e, W, rec, ch, goal, dt) {
|
|
106
122
|
const nav = rec.nav, p = ch.root.position;
|
|
107
123
|
const dGoal = goal ? Math.hypot(goal.x - p.x, goal.z - p.z) : 0;
|
|
108
|
-
if (rec.busy || !goal || dGoal < rec.arriveDistance) ch.move(0, 0, false, dt);
|
|
109
|
-
else if (e.nav?.crowd) crowdMove(e, ch, goal, dt);
|
|
124
|
+
if (rec.busy || !goal || (dGoal < rec.arriveDistance && sameLevel(e, p, goal) && !linkAhead(nav.path) && !ch._linkNext)) ch.move(0, 0, false, dt);
|
|
125
|
+
else if (e.nav?.crowd) crowdMove(e, rec, ch, goal, dt);
|
|
110
126
|
else {
|
|
111
127
|
nav.repath -= dt;
|
|
112
128
|
// goal changed while waiting, or the answer never came (engine.commits.invalidate dropped it: a partial saves.apply, a level change): ask again
|
|
113
|
-
if (ch._pathReq && (ch._pathReq.goal !== goal || e.tick - ch._pathReq.at > PATH_WAIT)) { ch._pathReq.remove(); ch._pathReq = null; }
|
|
129
|
+
if (ch._pathReq && (ch._pathReq.goal !== goal || e.time.tick - ch._pathReq.at > PATH_WAIT)) { ch._pathReq.remove(); ch._pathReq = null; }
|
|
114
130
|
if (!nav.path && nav.repath <= 0 && !ch._pathReq) {
|
|
115
|
-
const far = dGoal > 120, tx = far ? p.x + (goal.x - p.x) / dGoal * 100 : goal.x, tz = far ? p.z + (goal.z - p.z) / dGoal * 100 : goal.z;
|
|
131
|
+
const far = dGoal > 120, tx = far ? p.x + (goal.x - p.x) / dGoal * 100 : goal.x, tz = far ? p.z + (goal.z - p.z) / dGoal * 100 : goal.z, ty = far ? undefined : goal.y; // the stop's height says nothing about a point on the way
|
|
116
132
|
// async: many NPCs repathing in one step cost the main thread almost nothing; the answer lands a few steps later (the NPC waits in place)
|
|
117
|
-
const req = ch._pathReq = e.characters.findPathAsync(p, { x: tx, z: tz }, path => {
|
|
133
|
+
const req = ch._pathReq = e.characters.findPathAsync(p, { x: tx, y: ty, z: tz }, path => {
|
|
118
134
|
if (ch._pathReq !== req) return;
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
135
|
+
// no way there: it stands and asks again at the next repath, a failed search is no licence to press on along the line. A far leg is only a heading, not a place it must reach
|
|
136
|
+
ch._pathReq = null; nav.path = path ?? (far ? [{ x: tx, y: p.y, z: tz }] : null);
|
|
137
|
+
}, { radius: ch.radius, step: rec.pathStep, id: rec.id });
|
|
138
|
+
req.goal = goal; req.at = e.time.tick;
|
|
122
139
|
nav.repath = rec.repath;
|
|
123
140
|
}
|
|
141
|
+
// arriving is judged above, so a point counts as reached no farther out than arriveDistance: else an NPC with a small one stopped short for good, asking for a path every repath
|
|
142
|
+
const reach = Math.min(0.8, rec.arriveDistance);
|
|
143
|
+
let wp = null;
|
|
124
144
|
if (nav.path?.length) {
|
|
125
|
-
const
|
|
126
|
-
if (
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
145
|
+
const next = nextPoint(nav.path, 0, p.x, p.y, p.z, reach, e.movement.height);
|
|
146
|
+
if (next) nav.path.splice(0, next); // the path stays one saved list: what is behind the NPC leaves it
|
|
147
|
+
wp = nav.path[0];
|
|
148
|
+
if (Math.hypot(wp.x - p.x, wp.z - p.z) < (wp.link ? LINK_REACH : reach) && sameLevel(e, p, wp)) {
|
|
149
|
+
if (wp.link) { nav.link = startLink(ch, wp.link); nav.path.shift(); }
|
|
150
|
+
else nav.path = null;
|
|
151
|
+
wp = null;
|
|
131
152
|
}
|
|
132
153
|
}
|
|
154
|
+
if (wp) {
|
|
155
|
+
stepToward(nav, ch, wp, dt);
|
|
156
|
+
if (nav.stuck > STUCK_SECONDS) { nav.path = null; nav.stuck = 0; } // re-pathfind
|
|
157
|
+
} else if (!nav.link) ch.move(0, 0, false, dt); // waiting for a path: gravity, the idle pose and slowing down go on, it must not hang in the air walking on the spot
|
|
133
158
|
}
|
|
134
|
-
|
|
159
|
+
e.npcs.avoid.push(rec, ch, dt); // keep clear of other NPCs and the player (with crowd, the agents handle it: push adds nothing)
|
|
135
160
|
// position belongs to the mover: copy the model position back to data here (world also copies at the end of each fixed step; copying now means later
|
|
136
161
|
// simulation and saves this step see the latest)
|
|
137
162
|
rec.motion = ch.saveMotion(rec.motion ?? {});
|
|
@@ -139,20 +164,51 @@ export function goTo(e, W, rec, ch, goal, dt) {
|
|
|
139
164
|
W.move(rec.id, p);
|
|
140
165
|
}
|
|
141
166
|
|
|
167
|
+
/** One step straight at wp. nav.stuck counts time, not speed: a body pressed against something that came after the path (an obstacle added in play) still reports a walk */
|
|
168
|
+
function stepToward(nav, ch, wp, dt) {
|
|
169
|
+
const p = ch.root.position, dx = wp.x - p.x, dz = wp.z - p.z, d = Math.hypot(dx, dz), ox = p.x, oz = p.z;
|
|
170
|
+
ch.move(dx / d, dz / d, false, dt, climbTo(p, wp));
|
|
171
|
+
nav.stuck = Math.hypot(p.x - ox, p.z - oz) < 0.002 ? (nav.stuck ?? 0) + dt : 0;
|
|
172
|
+
}
|
|
173
|
+
|
|
142
174
|
// Crowd avoidance (nav({ crowd: true })): Recast agents steer around other NPCs (smarter than the simple push in npcAvoid.js); the NPC reads the agent velocity
|
|
143
175
|
// and walks itself.
|
|
144
|
-
// The agent belongs to the model (stored on the character) and is deleted with it (dispose in
|
|
176
|
+
// The agent belongs to the model (stored on the character) and is deleted with it (dispose in npcView.js); the agent itself does not guarantee identical results
|
|
145
177
|
// after load.
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
178
|
+
// Links: the crowd takes an agent over one by itself, but it runs its own timed line that a game cannot shape, so the NPC crosses the same way as any other (crossLink) and
|
|
179
|
+
// the agent is pulled back to it after. Native Recast does not say which link an agent took, so it is the nearest one starting here. The crowd takes a link 2.25 agent radii
|
|
180
|
+
// short of its start and a crossing runs from where the walker stands, so the NPC first walks the rest (reachLink), or its climb starts a meter off the ladder and cuts through the deck.
|
|
181
|
+
function crowdMove(e, rec, ch, goal, dt) {
|
|
182
|
+
const crowd = e.nav.crowd, p = ch.root.position;
|
|
183
|
+
// nav mesh rebuilt, or another nav module: the old agent went with its crowd. The crowd object says so; version numbers count per module and can match
|
|
184
|
+
if (ch._agent && ch._crowd !== crowd) { ch._agent = null; ch._agentGoal = null; ch._linkNext = null; }
|
|
185
|
+
ch._crowd = crowd;
|
|
151
186
|
if (!ch._agent) ch._agent = crowd.addAgent({ x: p.x, y: p.y, z: p.z }, { radius: ch.radius, height: e.movement.height, maxSpeed: ch.speed, maxAcceleration: ch.acceleration * ch.speed });
|
|
152
187
|
const a = ch._agent;
|
|
153
|
-
if (ch._agentGoal !== goal) { a.requestMoveTarget(
|
|
188
|
+
if (ch._agentGoal !== goal) { a.requestMoveTarget(e.nav.point(goal)); ch._agentGoal = goal; }
|
|
189
|
+
const onLink = a.state() === AGENT_ON_LINK;
|
|
190
|
+
if (onLink && !ch._agentOnLink) ch._linkNext = linkStartingAt(e.nav.links, p);
|
|
191
|
+
ch._agentOnLink = onLink; // once per crossing: the agent may still be on its line when a quick game crossing has landed
|
|
192
|
+
if (ch._linkNext) { reachLink(rec, ch, dt); return; }
|
|
154
193
|
const ap = a.position();
|
|
155
|
-
|
|
194
|
+
// blocked, drifted too far or left behind by a crossing (it walked on alone, and an agent already at the goal gives the NPC no way there): pull the agent back
|
|
195
|
+
if (ch._agentAhead || Math.hypot(ap.x - p.x, ap.z - p.z) > AGENT_LEASH) { a.teleport({ x: p.x, y: p.y, z: p.z }); ch._agentGoal = null; ch._agentAhead = false; }
|
|
156
196
|
const v = a.velocity(), sp = Math.hypot(v.x, v.z);
|
|
157
197
|
ch.move(sp > 0.05 ? v.x / ch.speed : 0, sp > 0.05 ? v.z / ch.speed : 0, false, dt);
|
|
158
198
|
}
|
|
199
|
+
|
|
200
|
+
/** Walk to the link the crowd took and start the crossing there, or from where the NPC is stuck */
|
|
201
|
+
function reachLink(rec, ch, dt) {
|
|
202
|
+
const nav = rec.nav, link = ch._linkNext, p = ch.root.position, d = Math.hypot(link.from.x - p.x, link.from.z - p.z);
|
|
203
|
+
if (d >= LINK_REACH && d < LINK_WALK && !(nav.stuck > STUCK_SECONDS)) { stepToward(nav, ch, link.from, dt); return; }
|
|
204
|
+
ch._linkNext = null; nav.stuck = 0; ch._agentAhead = true;
|
|
205
|
+
if (d < LINK_WALK) nav.link = startLink(ch, link);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** The way across the link nearest to p: { id, from, to }, either end of a two-way link */
|
|
209
|
+
function linkStartingAt(links, p) {
|
|
210
|
+
let best = null, bestD = Infinity;
|
|
211
|
+
const take = (id, from, to) => { const d = Math.hypot(from.x - p.x, from.y - p.y, from.z - p.z); if (d < bestD) { bestD = d; best = { id, from, to }; } };
|
|
212
|
+
for (const l of links) { take(l.id, l.from, l.to); if (!l.oneWay) take(l.id, l.to, l.from); }
|
|
213
|
+
return best;
|
|
214
|
+
}
|
|
@@ -1,24 +1,31 @@
|
|
|
1
1
|
// The model side of an NPC (split out of NPC.js): build the Character when the NPC comes near, tear it down when it goes far, run its interact op.
|
|
2
2
|
// Plain functions that take the NPCManager; the manager keeps the maps (_views, _runtime) and avoid, these read and write them.
|
|
3
3
|
import { Character } from './Character.js';
|
|
4
|
+
import { seatFromRecord, vehicleById } from './seat.js';
|
|
4
5
|
|
|
5
6
|
/** Build the model: reads position, motion and animation state from data; attaches an interaction when it has interact */
|
|
6
7
|
export function buildNpcView(m, rec) {
|
|
7
|
-
const e = m.engine, rt = m._runtime.get(rec.id) || {};
|
|
8
|
-
|
|
9
|
-
ch
|
|
10
|
-
ch.npcModel = true; // the model of a world NPC: disposing it (the NPC went far) keeps the NPC's control slot (core/control.js)
|
|
11
|
-
ch.
|
|
12
|
-
ch.
|
|
8
|
+
const e = m.engine, { interactLabel, ...rt } = m._runtime.get(rec.id) || {};
|
|
9
|
+
// id: engine.mod('move.speed', { id }) and time.local use the NPC's record id, the same id the game knows it by
|
|
10
|
+
const ch = new Character(e, { ...rec.look, ...rt, speed: rec.speed, id: rec.id });
|
|
11
|
+
ch.npcModel = true; // the model of a world NPC: disposing it (the NPC went far) keeps the NPC's control slot (core/ops/control.js)
|
|
12
|
+
ch.placeRecord = p => m.get(rec.id).placeAt(p);
|
|
13
|
+
ch._turnRecord = yaw => { ch.face(yaw); const motion = e.world.get(rec.id).motion; if (motion) motion.facing = yaw; }; // a far step or a rebuild reads the facing back from there
|
|
14
|
+
ch._place(rec.position);
|
|
15
|
+
ch.loadMotion(rec.motion); ch._fromRecord(rec); ch.state = ch.stateMark = rec.state;
|
|
16
|
+
const car = vehicleById(e, rec.vehicle);
|
|
17
|
+
if (car) seatFromRecord(ch, car, rec.seat); // no vehicle with that id yet: the record waits, addVehicle answers it (seat.js)
|
|
18
|
+
// after the seat above, as a load writes nothing. Its record may be gone already: a model torn down with it gets out too
|
|
19
|
+
ch._writeSeat = (vehicle, seat) => { if (e.world.get(rec.id)) e.ops.allow(() => e.world.set(rec.id, { vehicle, seat })); };
|
|
13
20
|
let use = null;
|
|
14
21
|
const io = rec.interact;
|
|
15
22
|
if (io && e.interactions) use = e.interactions.add(ch.root, {
|
|
16
23
|
id: rec.id, // stable for the interaction.use op, whether the model is built sync or async
|
|
17
|
-
label:
|
|
24
|
+
label: interactLabel || io.label || (() => io.op === 'npc.talk' ? e.t('npc.talk', { name: rec.name }) : rec.name), radius: io.radius,
|
|
18
25
|
// the op's result is the use's result: { ok: false, code } from its can fails interaction.use with that code
|
|
19
26
|
onUse: () => { const r = e.ops.run(io.op, { npc: rec.id, ...io.args }, { source: 'player' }); return r.ok ? r.value : r; },
|
|
20
27
|
});
|
|
21
|
-
m._views.set(ch.root, { ch, use });
|
|
28
|
+
m._views.set(ch.root, { ch, use, useFrom: e.interactions });
|
|
22
29
|
m.avoid.built.add(rec.id);
|
|
23
30
|
return ch.root;
|
|
24
31
|
}
|
|
@@ -31,7 +38,8 @@ export function unbuildNpcView(m, rec, obj) {
|
|
|
31
38
|
if (!v) { e.remove(obj); return; }
|
|
32
39
|
const { ch, use } = v;
|
|
33
40
|
use?.remove();
|
|
34
|
-
|
|
41
|
+
ch._pathReq?.remove(); // an answer for this model would write a path from where it stood into the record, after far moved it on
|
|
42
|
+
if (ch._agent && ch._crowd === e.nav?.crowd) ch._crowd.removeAgent(ch._agent);
|
|
35
43
|
const model = rec && m._runtime.get(rec.id)?.model;
|
|
36
44
|
if (model && model.parent === ch.root) ch.root.remove(model);
|
|
37
45
|
ch.dispose();
|
|
@@ -42,9 +50,9 @@ export function unbuildNpcView(m, rec, obj) {
|
|
|
42
50
|
* when the dialogue ends. script defaults to the NPC id. channel 'bubble' plays the script as lines over the NPC's head while the player keeps
|
|
43
51
|
* playing (a shopkeeper's greeting, a guard's warning); default 'main' is the conversation that locks input
|
|
44
52
|
*/
|
|
45
|
-
export function defineTalk(e, m) {
|
|
53
|
+
export function defineTalk(e, ctx, m) {
|
|
46
54
|
if (e.ops.defs.has('npc.talk')) return;
|
|
47
|
-
|
|
55
|
+
ctx.ops.define('npc.talk', {
|
|
48
56
|
desc: 'Talk to an NPC: it stops and faces the actor while its dialogue (script, default the NPC id; channel main | bubble) runs',
|
|
49
57
|
args: { npc: 'string', script: 'string?', channel: { type: 'string', values: ['main', 'bubble'], default: 'main', desc: 'main locks input; bubble = lines over the NPC' } },
|
|
50
58
|
can(e, { npc, channel = 'main' }) {
|
|
@@ -5,36 +5,66 @@
|
|
|
5
5
|
// if that changed before it lands, it is recomputed here on the current state, so threads on / off / slow / crashed give identical paths.
|
|
6
6
|
// A request belongs to the world it was asked in, like any background result: one still queued when a save load, new game or level change
|
|
7
7
|
// invalidates engine.commits is dropped (its done never runs), the same as a batch already sent.
|
|
8
|
-
|
|
8
|
+
// done stays a callback on purpose: it runs inside the fixed step. A Promise's then runs after the whole frame's steps, so a write there would depend on how many
|
|
9
|
+
// steps a frame ran, and a dropped request would leave the Promise pending forever.
|
|
10
|
+
import { xzOf, yOf } from '../../core/base/math.js';
|
|
11
|
+
import { surfaceOr } from '../../core/scene/ground.js';
|
|
9
12
|
import { solveBatch, pathHandler, astarDefaults, RES, TILE, tileOf } from './pathJob.js';
|
|
10
|
-
import { codedError } from '../../core/errorHints.js';
|
|
13
|
+
import { codedError } from '../../core/base/errorHints.js';
|
|
14
|
+
import { withHeights } from './moveHelpers.js';
|
|
11
15
|
|
|
12
16
|
/** Steps between sending a batch and its answers taking effect (4 = 67 ms): enough for a worker round trip, short enough that an NPC barely waits */
|
|
13
17
|
const PATH_LAG = 4;
|
|
14
18
|
/** A cached ground raster tile is reused for this many fixed steps (5 s), then sampled again: streamed terrain may have added trees meanwhile */
|
|
15
19
|
const TILE_TTL = 300;
|
|
20
|
+
/** Floors looked for under a cell's top one: only a ground that answers something new for every height ever reaches it */
|
|
21
|
+
const MAX_FLOORS = 16;
|
|
16
22
|
const NO_DONE = 'characters.findPathAsync(from, to, done): done must be a function (path, status) => { ... }; it runs on a fixed step when the path is ready';
|
|
17
23
|
|
|
24
|
+
/** A position in any form as the plain point a path source reads and writes */
|
|
25
|
+
const point = p => { const [x, z] = xzOf(p); return { x, y: yOf(p), z }; };
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* What the characters.path rule says for one request, as a search answer; null when the built-in search answers ('walk'). A game's source returns a list of points, null or an empty list for no way, 'walk' to fall back.
|
|
29
|
+
* The points are copied: walkers fill in heights and eat the path as they go, and the list may be one the game keeps and hands to everyone.
|
|
30
|
+
* Why it runs on the main thread at the due step, also for findPathAsync: a game's function cannot go to a worker, and done must still come on a fixed step
|
|
31
|
+
*/
|
|
32
|
+
export function sourcePath(e, rule, from, to, o) {
|
|
33
|
+
const d = rule.decide(o.id, { from: point(from), to: point(to), o });
|
|
34
|
+
if (d === 'walk') return null;
|
|
35
|
+
if (d && !Array.isArray(d)) throw codedError('character-option', `rule 'characters.path': a path source returns a list of { x, y?, z } points, null or 'walk', got ${JSON.stringify(d)}`);
|
|
36
|
+
return d?.length ? { status: 'ok', path: d.map(p => ({ ...p, ...point(p) })) } : { status: 'noPath', path: null };
|
|
37
|
+
}
|
|
38
|
+
|
|
18
39
|
/** @internal */
|
|
19
40
|
export class AsyncPaths {
|
|
20
|
-
constructor(e) {
|
|
41
|
+
constructor(e, rule) {
|
|
21
42
|
this.e = e;
|
|
43
|
+
this.rule = rule;
|
|
22
44
|
this.queue = [];
|
|
23
45
|
this.perStep = 32;
|
|
24
46
|
this.lane = null;
|
|
25
|
-
this.sent = {
|
|
26
|
-
this.
|
|
47
|
+
this.sent = { from: null, meshes: new Map(), ver: -1 }; // what the workers mirror, and of which provider: mesh size -> its revision, collision version
|
|
48
|
+
this.turn = 0; // bumps with every provider
|
|
49
|
+
this.tiles = new Map(); // 'tx:tz' -> { data, trees, at, bv }
|
|
27
50
|
}
|
|
28
51
|
request(from, to, done, o = {}) {
|
|
29
52
|
if (typeof done !== 'function') throw codedError('character-option', NO_DONE);
|
|
30
53
|
const [sx, sz] = xzOf(from), [gx, gz] = xzOf(to);
|
|
31
|
-
const h = { sx, sz, gx, gz, o, done, epoch: this.e.commits.epoch, canceled: false, remove() { h.canceled = true; } };
|
|
54
|
+
const h = { sx, sz, gx, gz, sy: yOf(from), gy: yOf(to), o, done, epoch: this.e.commits.epoch, canceled: false, remove() { h.canceled = true; } };
|
|
32
55
|
this.queue.push(h);
|
|
33
56
|
return h;
|
|
34
57
|
}
|
|
58
|
+
/** The module is unloading: nothing queued or on its way may answer, the world it was asked in has no characters now. A lane is the engine's: it ends with engine.threads */
|
|
59
|
+
stop() { this.queue.length = 0; this.stopped = true; }
|
|
35
60
|
/** Which walkable state answers now: navmesh revision, collision version, or nothing */
|
|
36
|
-
key() {
|
|
37
|
-
|
|
61
|
+
key() {
|
|
62
|
+
const e = this.e, from = e.nav?.ready ? e.nav : e.collision;
|
|
63
|
+
// another provider starts its numbers over, so equal ones say nothing: what the workers mirror goes, and a batch of the old one cannot match the new key
|
|
64
|
+
if (from !== this.sent.from) { this.sent = { from, meshes: new Map(), ver: -1 }; this.turn++; }
|
|
65
|
+
return from === e.nav ? `n${this.turn}.${e.nav.revision}` : from ? `g${this.turn}.${e.collision.version}` : '-';
|
|
66
|
+
}
|
|
67
|
+
/** Each fixed step (characters fixed): send up to perStep queued requests as one batch */
|
|
38
68
|
flush() {
|
|
39
69
|
const list = [];
|
|
40
70
|
const epoch = this.e.commits.epoch;
|
|
@@ -43,75 +73,97 @@ export class AsyncPaths {
|
|
|
43
73
|
const e = this.e, T = e.threads, batch = this.build(list);
|
|
44
74
|
const t = e.commits.request({ lag: PATH_LAG, fallback: () => null, apply: r => this.land(batch, list, r) });
|
|
45
75
|
if (!T.size || batch.key === '-') return;
|
|
46
|
-
this.mirror(batch
|
|
76
|
+
this.mirror(batch);
|
|
47
77
|
this.lane.run(batch).then(r => t.resolve(r), () => {}); // a failed run is ignored: land() computes the same answer here
|
|
48
78
|
}
|
|
49
79
|
/** Plain-data batch: everything the solver needs that the worker does not mirror */
|
|
50
80
|
build(list) {
|
|
51
|
-
const e = this.e, key = this.key(), mode = key[0] === 'n' ? 'nav' : key[0] === 'g' ? 'grid' : 'none', tiles = {}, trees = [], seen = new Set();
|
|
81
|
+
const e = this.e, key = this.key(), mode = key[0] === 'n' ? 'nav' : key[0] === 'g' ? 'grid' : 'none', tiles = {}, heights = {}, lower = {}, trees = [], seen = new Set();
|
|
52
82
|
const take = (tk, tx, tz) => {
|
|
53
83
|
if (tiles[tk]) return;
|
|
54
84
|
const c = this.tile(tk, tx, tz); tiles[tk] = c.data;
|
|
85
|
+
heights[tk] = c.top;
|
|
86
|
+
if (c.lower) lower[tk] = c.lower;
|
|
55
87
|
for (let i = 0; i < c.trees.length; i += 3) { const id = c.trees[i] + ',' + c.trees[i + 1]; if (!seen.has(id)) { seen.add(id); trees.push(c.trees[i], c.trees[i + 1], c.trees[i + 2]); } }
|
|
56
88
|
};
|
|
57
|
-
const reqs = list.map(({ sx, sz, gx, gz, o }) => {
|
|
58
|
-
if (mode === 'nav') return { mode,
|
|
89
|
+
const reqs = list.map(({ sx, sy, sz, gx, gy, gz, o }) => {
|
|
90
|
+
if (mode === 'nav') { const m = e.nav.mesh(o.radius); return { mode, size: m.size, s: e.nav.point([sx, sy, sz], m), g: e.nav.point([gx, gy, gz], m), reach: e.nav.reach(o.radius), body: e.movement.height }; }
|
|
59
91
|
if (mode === 'none') return { mode };
|
|
60
92
|
const radius = o.radius ?? e.movement.radius, dflt = astarDefaults(radius), step = o.step ?? dflt.step, maxNodes = o.maxNodes ?? dflt.maxNodes;
|
|
61
93
|
// A* may wander this far beyond the start-goal box; bounded by how far maxNodes cells can reach
|
|
62
94
|
const m = Math.min(step * Math.sqrt(maxNodes / Math.PI), 16 + Math.hypot(gx - sx, gz - sz) / 2);
|
|
63
95
|
for (let tx = tileOf(Math.min(sx, gx) - m); tx <= tileOf(Math.max(sx, gx) + m); tx++)
|
|
64
96
|
for (let tz = tileOf(Math.min(sz, gz) - m); tz <= tileOf(Math.max(sz, gz) + m); tz++) take(tx + ':' + tz, tx, tz);
|
|
65
|
-
|
|
97
|
+
const at = (x, z, y) => surfaceOr(e, x, z, y, y);
|
|
98
|
+
return { mode, s: { x: sx, y: at(sx, sz, sy), z: sz }, g: { x: gx, y: gy === undefined ? undefined : at(gx, gz, gy), z: gz }, body: e.movement.height, climb: e.movement.stepHeight, radius, step, maxNodes, navPending: !!e.nav };
|
|
66
99
|
});
|
|
67
|
-
return { type: 'batch', key, reqs, tiles, trees };
|
|
100
|
+
return { type: 'batch', key, reqs, tiles, heights, lower, trees };
|
|
68
101
|
}
|
|
69
102
|
/**
|
|
70
103
|
* One tile of the ground, sampled here (height / water / slope and terrain trees are main-thread functions) and cached for TILE_TTL steps:
|
|
71
|
-
* data =
|
|
104
|
+
* data = can the top floor be walked, top = its height, lower = the floors under it by cell index, { y, ok } each (null when no cell has one), trees = [x, z, r, ...] of the ground's colliders around the tile.
|
|
72
105
|
*/
|
|
73
106
|
tile(k, tx, tz) {
|
|
74
|
-
const e = this.e
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
107
|
+
const e = this.e;
|
|
108
|
+
// another collision module counts from zero, so an equal number says nothing; a new playthrough restarts the tick, so a negative age is an old tile, not a young one
|
|
109
|
+
const from = e.collision;
|
|
110
|
+
if (from !== this.tilesOf) { this.tiles.clear(); this.tilesOf = from; } // the issuer beside what it issued (architecture 8.2), compared, never called
|
|
111
|
+
const c = this.tiles.get(k), bv = e.collision.blockedVersion, age = c ? e.time.tick - c.at : -1;
|
|
112
|
+
if (c && c.bv === bv && age >= 0 && age < TILE_TTL) return c;
|
|
113
|
+
const data = new Uint8Array(TILE * TILE), top = new Float32Array(TILE * TILE), col = e.collision, trees = [], span = TILE * RES, body = e.movement.height;
|
|
114
|
+
let lower = null;
|
|
78
115
|
// addBlocked fields are functions and cannot go to a worker: they are sampled into this same raster (the worker sees blocked cells as unwalkable).
|
|
79
116
|
// A cell counts as blocked if fn is true at its centre or on a ring of half a cell (blockedAt), so a 1 m cell never hides a thin river; bounds skip the sampling outside.
|
|
80
117
|
for (let j = 0; j < TILE; j++) for (let i = 0; i < TILE; i++) {
|
|
81
118
|
const x = (tx * TILE + i) * RES, z = (tz * TILE + j) * RES;
|
|
82
|
-
|
|
119
|
+
const c = j * TILE + i, free = !col.blockedAt(x, z, RES / 2), t = e.surface(x, z);
|
|
120
|
+
data[c] = free && col.groundWalkable(x, z) ? 1 : 0;
|
|
121
|
+
top[c] = t;
|
|
122
|
+
// the ground gives floors by height only, so go down it: asked from a body under a floor (nearer is the same floor, and a ground's step is its own) it answers the next one, or the same one when that was the lowest
|
|
123
|
+
for (let n = 0, y = t, b; n < MAX_FLOORS && (b = e.surface(x, z, y - body)) < y; n++, y = b) ((lower ??= {})[c] ??= []).push({ y: b, ok: free && col.groundWalkable(x, z, b) ? 1 : 0 });
|
|
83
124
|
}
|
|
84
125
|
// every collider that can touch a point of the tile: half diagonal + the largest character radius we expect + 1 (the same reach walkableAt uses)
|
|
85
126
|
e.ground.forEachCollider?.((tx + 0.5) * span, (tz + 0.5) * span, span * 0.71 + 3, (x, z, r) => { trees.push(x, z, r); });
|
|
86
|
-
const v = { data, trees, at: e.tick, bv };
|
|
127
|
+
const v = { data, top, lower, trees, at: e.time.tick, bv };
|
|
87
128
|
this.tiles.set(k, v);
|
|
88
|
-
if (this.tiles.size > 4096) for (const [kk, o] of this.tiles) if (e.tick - o.at >= TILE_TTL) this.tiles.delete(kk);
|
|
129
|
+
if (this.tiles.size > 4096) for (const [kk, o] of this.tiles) if (e.time.tick - o.at >= TILE_TTL) this.tiles.delete(kk);
|
|
89
130
|
return v;
|
|
90
131
|
}
|
|
91
132
|
/** Forget cached tiles overlapping a world box (ground:change) */
|
|
92
133
|
dropTiles({ x0, z0, x1, z1 }) {
|
|
93
134
|
const span = TILE * RES;
|
|
94
|
-
for (const
|
|
135
|
+
for (const k of this.tiles.keys()) {
|
|
95
136
|
const [tx, tz] = k.split(':').map(Number);
|
|
96
137
|
if ((tx + 1) * span + 3 >= x0 && tx * span - 3 <= x1 && (tz + 1) * span + 3 >= z0 && tz * span - 3 <= z1) this.tiles.delete(k);
|
|
97
138
|
}
|
|
98
139
|
}
|
|
99
140
|
/** Bring the workers' mirror up to the state this batch is tagged with (broadcasts arrive before the batch: one message queue per worker) */
|
|
100
|
-
mirror(key) {
|
|
101
|
-
const e = this.e;
|
|
141
|
+
mirror({ key, reqs }) {
|
|
142
|
+
const e = this.e, sent = this.sent.meshes;
|
|
102
143
|
this.lane ??= e.threads.lane({ create: () => new Worker(new URL('./pathWorker.js', import.meta.url), { type: 'module' }), local: pathHandler });
|
|
103
|
-
if (key[0] === 'n'
|
|
144
|
+
if (key[0] === 'n') for (const size of new Set(reqs.map(q => q.size))) {
|
|
145
|
+
const m = e.nav.meshes.get(size);
|
|
146
|
+
if (sent.get(size) === e.nav.revision || !m.navMesh) continue;
|
|
147
|
+
sent.set(size, e.nav.revision);
|
|
148
|
+
this.lane.broadcast({ type: 'mesh', size, version: e.nav.version, data: e.nav.exportMesh(m), links: m.links }, 'mesh' + size);
|
|
149
|
+
}
|
|
104
150
|
if (key[0] === 'g' && this.sent.ver !== e.collision.version) {
|
|
105
151
|
this.sent.ver = e.collision.version;
|
|
106
152
|
const items = [];
|
|
107
|
-
e.collision.forEachStatic((type, o) => items.push(type === 'box' ? { x: o.x, z: o.z, r: o.r, c: o.c, s: o.s, hw: o.hw, hd: o.hd, box: true } : { x: o.x, z: o.z, r: o.r, box: false }));
|
|
153
|
+
e.collision.forEachStatic((type, o) => items.push(type === 'box' ? { x: o.x, y: o.y, z: o.z, h: o.h, r: o.r, c: o.c, s: o.s, hw: o.hw, hd: o.hd, box: true } : { x: o.x, z: o.z, r: o.r, box: false }));
|
|
108
154
|
this.lane.broadcast({ type: 'colliders', items }, 'colliders');
|
|
109
155
|
}
|
|
110
156
|
}
|
|
111
157
|
/** Due step: worker answer if it arrived and the walkable state is unchanged, else the same solver here on the current state */
|
|
112
158
|
land(batch, list, r) {
|
|
159
|
+
if (this.stopped) return;
|
|
113
160
|
const e = this.e, key = this.key();
|
|
114
|
-
if (!r || key !== batch.key) r = solveBatch(key === batch.key ? batch : this.build(list), {
|
|
115
|
-
list.forEach((h, i) => {
|
|
161
|
+
if (!r || key !== batch.key) r = solveBatch(key === batch.key ? batch : this.build(list), { meshes: e.nav?.meshes, grid: e.collision?.grid });
|
|
162
|
+
list.forEach((h, i) => {
|
|
163
|
+
if (h.canceled) return;
|
|
164
|
+
const from = { x: h.sx, y: h.sy, z: h.sz }, to = { x: h.gx, y: h.gy, z: h.gz }, a = sourcePath(e, this.rule, from, to, h.o) ?? r[i];
|
|
165
|
+
if (a.path) withHeights(e, a.path, from, to);
|
|
166
|
+
h.done(a.path, a.status);
|
|
167
|
+
});
|
|
116
168
|
}
|
|
117
169
|
}
|
|
@@ -1,60 +1,77 @@
|
|
|
1
1
|
// Async pathfinding batch solver: the same code answers in the path worker (pathWorker.js), in the lane's main-thread fallback,
|
|
2
2
|
// and in engine.commits' fallback when a worker reply is late (pathAsync.js). Worker-safe: no three.js, no engine objects.
|
|
3
|
-
// A batch carries everything that is not mirrored in the worker: start / goal (with ground heights), and for A* a raster of the ground half of walkability.
|
|
4
|
-
// The worker mirrors the rest: the navmesh (imported from the bytes the nav module exports) and the collision module's colliders (rebuilt into the same grid).
|
|
3
|
+
// A batch carries everything that is not mirrored in the worker: start / goal (with ground heights), and for A* a raster of the ground half of walkability, every floor of it.
|
|
4
|
+
// The worker mirrors the rest: the navmesh of every body size a batch asked for (imported from the bytes the nav module exports) and the collision module's colliders (rebuilt into the same grid).
|
|
5
5
|
import { findPath as astar, astarDefaults } from './astar.js';
|
|
6
6
|
export { astarDefaults };
|
|
7
7
|
import { CollisionGrid, gridBlocks } from '../collision/grid.js';
|
|
8
8
|
import { navSearch } from '../nav/navPath.js';
|
|
9
|
+
import { layerUnder } from '../../core/scene/floor.js';
|
|
9
10
|
|
|
10
|
-
/** Ground
|
|
11
|
+
/** Ground raster (height, water, slope): RES-meter cells, TILE x TILE cells per tile, keyed `${tx}:${tz}`; a cell's top floor is in flat arrays, the floors under it, few cells have any, in a sparse list */
|
|
11
12
|
export const RES = 1, TILE = 32;
|
|
12
13
|
export const tileOf = v => Math.floor(Math.round(v / RES) / TILE);
|
|
13
14
|
|
|
15
|
+
const TOP = { y: 0, ok: 0 }; // the answer for a cell's top floor: the search asks for every node, so it must not allocate
|
|
16
|
+
|
|
14
17
|
/**
|
|
15
|
-
*
|
|
16
|
-
*
|
|
18
|
+
* The floor of a batch's ground a walker at height y comes to at (x, z), { y, ok } (nearest cell), the snapshot of what engine.surface(x, z, y) and the ground half of walkableAt
|
|
19
|
+
* say on the main thread: no y is the top floor. Null outside the sent tiles, which counts as blocked: both sides see the same tiles.
|
|
17
20
|
*/
|
|
18
|
-
function
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
function floorAt({ tiles, heights, lower }, x, z, y, climb) {
|
|
22
|
+
const ix = Math.round(x / RES), iz = Math.round(z / RES), tx = Math.floor(ix / TILE), tz = Math.floor(iz / TILE), k = tx + ':' + tz, ok = tiles[k];
|
|
23
|
+
if (!ok) return null;
|
|
24
|
+
const i = (iz - tz * TILE) * TILE + (ix - tx * TILE), top = heights[k][i], under = y === undefined || top <= y + climb ? undefined : lower[k]?.[i];
|
|
25
|
+
if (under) return layerUnder(under, y + climb);
|
|
26
|
+
TOP.y = top; TOP.ok = ok[i];
|
|
27
|
+
return TOP;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Walkability from a batch: ground shape from the floor the walker is on, colliders tested exactly: the ground's own (terrain trees, sent with the batch) and the collision module's (mirrored grid) */
|
|
31
|
+
function snapshotWalkable(batch, q, grid, trees) {
|
|
32
|
+
return (x, z, y) => !!floorAt(batch, x, z, y, q.climb)?.ok && !gridBlocks(trees, x, z, q.radius) && !(grid && gridBlocks(grid, x, z, q.radius, y, q.body));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The worker's engine.surface(x, z, y); no ground there (the void of terrain({ shape }), or outside the sent tiles) leaves the walker at its height, as surfaceOr does */
|
|
36
|
+
function snapshotFloor(batch, q) {
|
|
37
|
+
return (x, z, y) => { const f = floorAt(batch, x, z, y, q.climb); return !f || f.y === -Infinity ? y : f.y; };
|
|
25
38
|
}
|
|
26
39
|
|
|
27
40
|
/**
|
|
28
41
|
* Answer every request of a batch: [{ status, path }...] in request order.
|
|
29
|
-
* @param {{ reqs: any[], tiles: Record<string, Uint8Array
|
|
30
|
-
* @param {{
|
|
42
|
+
* @param {{ reqs: any[], tiles: Record<string, Uint8Array>, heights: Record<string, Float32Array>, lower: Record<string, Record<number, { y: number, ok: number }[]>> }} batch
|
|
43
|
+
* @param {{ meshes: Map<any, any>, grid: any }} ctx navmeshes (query, navMesh, links) by size and collider grid (live ones on the main thread, mirrored ones in the worker)
|
|
31
44
|
*/
|
|
32
45
|
export function solveBatch(batch, ctx) {
|
|
33
46
|
let trees = null;
|
|
34
47
|
return batch.reqs.map(q => {
|
|
35
|
-
if (q.mode === 'nav') return navSearch(ctx.
|
|
48
|
+
if (q.mode === 'nav') return navSearch(ctx.meshes.get(q.size), q.s, q.g, q.reach, q.body);
|
|
36
49
|
if (q.mode !== 'grid') return { status: 'notReady', path: null };
|
|
37
50
|
if (!trees) { trees = new CollisionGrid(); for (let i = 0; i < batch.trees.length; i += 3) trees.add({ x: batch.trees[i], z: batch.trees[i + 1], r: batch.trees[i + 2] }, false); }
|
|
38
|
-
const
|
|
51
|
+
const floor = q.s.y === undefined ? undefined : snapshotFloor(batch, q); // undefined: no height to follow, A* keeps its default
|
|
52
|
+
const path = astar(snapshotWalkable(batch, q, ctx.grid, trees), q.s.x, q.s.z, q.g.x, q.g.z, { ...q, y: q.s.y, goalY: q.g.y, floor });
|
|
39
53
|
return { status: q.navPending ? 'notReady' : path ? 'ok' : 'noPath', path };
|
|
40
54
|
});
|
|
41
55
|
}
|
|
42
56
|
|
|
43
57
|
/**
|
|
44
|
-
* Lane handler: { type: 'mesh', data } (navmesh bytes
|
|
58
|
+
* Lane handler: { type: 'mesh', size, version, data, links } (the navmesh of a body size as bytes, the links its connections index; a new version drops the sizes of the old mesh), { type: 'colliders', items }, { type: 'batch', reqs, tiles } → results.
|
|
45
59
|
* @param {() => Promise<any> | any} load gives the Recast module: the worker passes its static import (a worker bundle cannot split chunks);
|
|
46
60
|
* the main-thread fallback imports it on demand so games without nav never download it
|
|
47
61
|
*/
|
|
48
62
|
export function pathHandler(load = () => import('@recast-navigation/core')) {
|
|
49
|
-
const ctx = {
|
|
50
|
-
let lib = null,
|
|
63
|
+
const ctx = { meshes: new Map(), grid: null };
|
|
64
|
+
let lib = null, chain = Promise.resolve(), version = -1;
|
|
51
65
|
// Recast loads asynchronously; every message waits for the ones before it, so a batch always sees the mesh / colliders sent before it
|
|
52
66
|
const then = fn => { const p = chain.then(fn); chain = p.catch(() => {}); return p; }; // one failed message must not block the rest
|
|
53
67
|
return msg => {
|
|
54
68
|
if (msg.type === 'mesh') return void then(async () => {
|
|
55
69
|
if (!lib) { const m = await load(); await m.init(); lib = m; }
|
|
56
|
-
ctx.query
|
|
57
|
-
if (msg.
|
|
70
|
+
const drop = size => { const m = ctx.meshes.get(size); m?.query.destroy(); m?.navMesh.destroy(); ctx.meshes.delete(size); };
|
|
71
|
+
if (msg.version !== version) { [...ctx.meshes.keys()].forEach(drop); version = msg.version; }
|
|
72
|
+
drop(msg.size);
|
|
73
|
+
const navMesh = lib.importNavMesh(msg.data).navMesh;
|
|
74
|
+
ctx.meshes.set(msg.size, { navMesh, query: new lib.NavMeshQuery(navMesh), links: msg.links });
|
|
58
75
|
});
|
|
59
76
|
if (msg.type === 'colliders') return void then(() => { const g = new CollisionGrid(); for (const it of msg.items) g.add(it, it.box); ctx.grid = g; });
|
|
60
77
|
return then(() => solveBatch(msg, ctx));
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Path worker: answers engine.characters.findPathAsync batches off the main thread (one per engine.threads slot).
|
|
2
2
|
// Recast is imported statically here (a worker bundle is one file); its wasm is inlined, so it loads in a worker without fetching anything.
|
|
3
3
|
import * as recast from '@recast-navigation/core';
|
|
4
|
-
import { serve } from '../../core/threadServe.js';
|
|
4
|
+
import { serve } from '../../core/threads/threadServe.js';
|
|
5
5
|
import { pathHandler } from './pathJob.js';
|
|
6
6
|
|
|
7
7
|
serve(pathHandler(() => recast));
|