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
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
// engine.ui.set('inventory', { drop: true }) default window with a Drop button per row
|
|
6
6
|
// engine.ui.set('inventory', (engine) => ({ show: d => …, update: d => …, hide: () => …, dispose: () => … })) your own
|
|
7
7
|
import { esc, iconHTML as icon, injectUICSS } from '../../ui/UI.js';
|
|
8
|
-
|
|
8
|
+
import { isRunFailure } from '../../core/ops/opsFail.js';
|
|
9
9
|
|
|
10
10
|
/** Texts of the inventory window, the got / full toasts and the ops (zh-CN and en) @internal */
|
|
11
11
|
export const INVENTORY_STRINGS = {
|
|
@@ -66,11 +66,11 @@ export function inventoryWindow(e, o = {}) {
|
|
|
66
66
|
const posOf = (e, actor) => actor === 'player' && !e.player ? null : e.actorPosition(actor);
|
|
67
67
|
|
|
68
68
|
/** Define inventory.transfer / inventory.use / inventory.drop @internal */
|
|
69
|
-
export function defineInventoryOps(e, inv) {
|
|
69
|
+
export function defineInventoryOps(e, ctx, inv) {
|
|
70
70
|
const box = id => inv._containers.map.get(id);
|
|
71
71
|
const own = ctx => inv.of(ctx.actor); // the bag of whoever runs the op (a net peer, an NPC, the local user), never a fixed 'player'
|
|
72
72
|
// moving items between containers by hand (an inventory / chest window, drag and drop) is a player decision
|
|
73
|
-
|
|
73
|
+
ctx.ops.define('inventory.transfer', {
|
|
74
74
|
desc: 'Move items between containers (a bag of an actor has the actor id, the local one is player); item is an item id or an entry key (sword#3)', args: { from: 'string', to: 'string', item: 'string', n: 'int?' },
|
|
75
75
|
can(e, { from, to, item, n = 1 }) {
|
|
76
76
|
const a = box(from), b = box(to);
|
|
@@ -83,7 +83,7 @@ export function defineInventoryOps(e, inv) {
|
|
|
83
83
|
// Replace-when-full recipe: one op takes A and gives B, all or nothing. Undo with engine.ops.undo('inventory.trade', args, value).
|
|
84
84
|
// engine.ops.run('inventory.trade', { take: 'rusty_key', give: 'gold_key' }) // { ok: true, value } or { ok: false, code: 'full' | 'notEnough', facts, nextActions }
|
|
85
85
|
const at = (a, ctx) => a.container != null ? box(a.container) : own(ctx);
|
|
86
|
-
|
|
86
|
+
ctx.ops.define('inventory.trade', {
|
|
87
87
|
desc: "Trade in the actor's container: take an item (or entry key) and give another in one all-or-nothing step; works with a full bag when the taken item frees the room",
|
|
88
88
|
args: { take: 'string', give: 'string', takeN: 'int?', giveN: 'int?', container: 'string?' },
|
|
89
89
|
can(e, a, ctx) {
|
|
@@ -98,8 +98,10 @@ export function defineInventoryOps(e, inv) {
|
|
|
98
98
|
},
|
|
99
99
|
reasons: { noContainer: 'No container with that id (container arg; default the actor)', notEnough: 'The container does not hold the item to give up', full: 'The new item does not fit even after the old one is removed' },
|
|
100
100
|
});
|
|
101
|
-
|
|
102
|
-
desc: "Use an item from the actor's container: runs the item's use(e, info); consume: true removes one first", args: { item: 'string' },
|
|
101
|
+
ctx.ops.define('inventory.use', {
|
|
102
|
+
desc: "Use an item from the actor's container: runs the item's use(e, info); consume: true removes one first; a use that answers { ok: false, code } or throws was not used, so all of it goes back, the item too", args: { item: 'string' },
|
|
103
|
+
// consume stays before use, for the room it frees (a potion that leaves a bottle in a full bag), so taking it back is the atomic rollback's job
|
|
104
|
+
atomic: true,
|
|
103
105
|
can(e, { item }, ctx) {
|
|
104
106
|
const en = own(ctx)._of(item)[0];
|
|
105
107
|
if (!en) return 'notEnough';
|
|
@@ -110,13 +112,14 @@ export function defineInventoryOps(e, inv) {
|
|
|
110
112
|
run(e, { item }, ctx) {
|
|
111
113
|
const c = own(ctx), en = c._of(item)[0], def = inv.item(en.id), info = { actor: ctx.actor, item: en.id, key: en.key, data: en.data };
|
|
112
114
|
if (def.consume) c.remove(en.key, 1);
|
|
113
|
-
def.use(e, info);
|
|
115
|
+
const r = def.use(e, info);
|
|
116
|
+
if (isRunFailure(r)) return r;
|
|
114
117
|
e.events.emit('item:use', info);
|
|
115
118
|
return en.key;
|
|
116
119
|
},
|
|
117
120
|
reasons: { notEnough: 'The actor does not have it', notUsable: 'The item has no use function' },
|
|
118
121
|
});
|
|
119
|
-
|
|
122
|
+
ctx.ops.define('inventory.drop', {
|
|
120
123
|
desc: "Drop items from the actor's container on the ground as a pickup (keeps their data)", args: { item: 'string', n: 'int?' },
|
|
121
124
|
can(e, { item, n = 1 }, ctx) {
|
|
122
125
|
if (!own(ctx).has(item, n)) return 'notEnough';
|
|
@@ -14,17 +14,18 @@
|
|
|
14
14
|
// gray box; pickups({ color, size, lift }) for the plain values; engine.pickups.material is the three.js material the box and geometry-only models draw with;
|
|
15
15
|
// spawn(pos, { mesh }) puts the game's own object down instead.
|
|
16
16
|
import * as THREE from 'three';
|
|
17
|
+
import { clone as skeletonClone } from 'three/addons/utils/SkeletonUtils.js';
|
|
17
18
|
import { neutralMaterial } from '../../render/neutral.js';
|
|
18
19
|
import { copyPatches, isEngineMade, markEngineMade } from '../../render/patches.js';
|
|
19
|
-
import { xzOf } from '../../core/math.js';
|
|
20
|
-
import { placeY } from '../../core/ground.js';
|
|
21
|
-
import { warnOnce } from '../../core/validate.js';
|
|
22
|
-
import { readParams, keysSchema } from '../../core/params.js';
|
|
20
|
+
import { xzOf, yOf } from '../../core/base/math.js';
|
|
21
|
+
import { placeY } from '../../core/scene/ground.js';
|
|
22
|
+
import { warnOnce, needMaterial, fmt } from '../../core/base/validate.js';
|
|
23
|
+
import { readParams, keysSchema } from '../../core/base/params.js';
|
|
23
24
|
import { copyData } from './Container.js';
|
|
24
|
-
import { failure } from '../../core/opsFail.js';
|
|
25
|
-
import { protoOf } from '../../core/namedModel.js';
|
|
26
|
-
import { shadowDefaults } from '../../core/assetEntry.js';
|
|
27
|
-
import { codedError, coded } from '../../core/errorHints.js';
|
|
25
|
+
import { failure } from '../../core/ops/opsFail.js';
|
|
26
|
+
import { protoOf } from '../../core/assets/namedModel.js';
|
|
27
|
+
import { shadowDefaults } from '../../core/assets/assetEntry.js';
|
|
28
|
+
import { codedError, coded } from '../../core/base/errorHints.js';
|
|
28
29
|
|
|
29
30
|
const STRINGS = { 'zh-CN': { 'module.pickups': '拾取物', 'pickups.take': '捡起 {name}' }, en: { 'module.pickups': 'pickups', 'pickups.take': 'Pick up {name}' } };
|
|
30
31
|
const SPAWN_KEYS = ['give', 'count', 'data', 'name', 'model', 'color', 'mesh', 'respawnSeconds', 'bob', 'id', 'initial', 'zone', 'radius', 'auto'];
|
|
@@ -32,10 +33,11 @@ const NO_ACTORS = [];
|
|
|
32
33
|
const DEFAULT_COLOR = 0xcccccc;
|
|
33
34
|
// A net host measures peers at the position they last sent (a few steps old): pickup.take allows this much beyond the radius so a fair press is not refused
|
|
34
35
|
const NET_SLACK = 1;
|
|
36
|
+
const seatActors = e => e.input.seats.flatMap(s => s.actor && s.actor !== e.actor ? [s.actor] : NO_ACTORS);
|
|
35
37
|
|
|
36
38
|
export class Pickups {
|
|
37
|
-
/** @internal
|
|
38
|
-
constructor(engine, { radius
|
|
39
|
+
/** @internal ctx: the module's ctx */
|
|
40
|
+
constructor(engine, { radius, showDistance, takers, color, size, lift }, { track }) {
|
|
39
41
|
this.engine = engine;
|
|
40
42
|
/** Edge of the placeholder box (meters), read when one is built */ this.size = size;
|
|
41
43
|
/** Height above the ground new pickups float at (meters) */ this.lift = lift;
|
|
@@ -44,11 +46,11 @@ export class Pickups {
|
|
|
44
46
|
/** Reach of pickups that set no radius (meters); auto pickups are taken within it */ this.radius = radius;
|
|
45
47
|
/** @internal pickups float, spin and have a model only within this distance of the stream sources */ this.showDistance = showDistance;
|
|
46
48
|
/**
|
|
47
|
-
* Who can walk into auto pickups: () => actor ids. Default: engine.actor, plus every net peer on a host; none on a net client (the host takes them for it,
|
|
49
|
+
* Who can walk into auto pickups: () => actor ids. Default: engine.actor and every seat's actor, plus every net peer on a host; none on a net client (the host takes them for it,
|
|
48
50
|
* from the positions it is sent). A game with NPCs that collect coins returns their ids too.
|
|
49
51
|
* @type {(e: any) => string[]}
|
|
50
52
|
*/
|
|
51
|
-
this.takers = takers ?? (e => e.net?.role === 'client' ? NO_ACTORS :
|
|
53
|
+
this.takers = takers ?? (e => e.net?.role === 'client' ? NO_ACTORS : [e.actor, ...seatActors(e), ...(e.net?.peers.map(p => p.actor) ?? NO_ACTORS)]);
|
|
52
54
|
// world id -> what cannot go in saved data: the game's mesh, a give function. Spawning the same id again (content / setup code on load) sets them again
|
|
53
55
|
/** @internal */ this._live = new Map();
|
|
54
56
|
// the reach query around each taker uses the largest auto radius in the world, read from the records rather than from spawn calls:
|
|
@@ -61,7 +63,7 @@ export class Pickups {
|
|
|
61
63
|
// a record gone (taken without respawn, removed, not in a loaded save): its live parts are no longer needed for a rebuild
|
|
62
64
|
track(engine.events.on('world:remove', ({ id, kind }) => { this._live.delete(id); dirty({ kind }); }));
|
|
63
65
|
track(engine.events.on('world:load', () => { this._autoDirty = true; for (const id of this._live.keys()) if (!engine.world.get(id)) this._live.delete(id); }));
|
|
64
|
-
present(this);
|
|
66
|
+
present(this, track);
|
|
65
67
|
}
|
|
66
68
|
|
|
67
69
|
/**
|
|
@@ -73,7 +75,7 @@ export class Pickups {
|
|
|
73
75
|
*/
|
|
74
76
|
get material() { return this._material; }
|
|
75
77
|
set material(m) {
|
|
76
|
-
|
|
78
|
+
needMaterial('pickup-misuse', 'engine.pickups.material', m, '. Example: engine.pickups.material = new THREE.MeshStandardMaterial({ color: 0xffd24a })');
|
|
77
79
|
const old = this._material;
|
|
78
80
|
if (m === old) return;
|
|
79
81
|
this._material = m;
|
|
@@ -112,16 +114,15 @@ export class Pickups {
|
|
|
112
114
|
if (item && !e.inventory) throw codedError('pickup-misuse', `pickups.spawn({ give: '${item}' }): an item pickup goes into the picker's container, so it needs inventory() installed; for an instant effect pass give: (e, { actor }) => ...`);
|
|
113
115
|
const [x, z] = xzOf(position);
|
|
114
116
|
// a given y is the reference height on multi-layer ground (as snapToGround): a pickup on a bridge deck or a second floor stays there; without one it sits on the top layer
|
|
115
|
-
const p = /** @type {any} */ (position).isObject3D ? /** @type {any} */ (position).position : position, refY = Array.isArray(p) ? p[1] : p.y;
|
|
116
117
|
const { count = 1, mesh = null, respawnSeconds = 0, bob = true, id, initial = false, zone, radius, auto = false, name, model: m, color } = opts;
|
|
117
118
|
const what = item ?? name ?? 'pickup';
|
|
118
119
|
if (radius != null && !(radius > 0)) throw codedError('pickup-misuse', `pickups.spawn('${what}'): radius must be a positive number of meters, got ${radius}`);
|
|
119
120
|
const data = copyData('pickups.spawn', what, opts.data);
|
|
120
121
|
const def = { id, kind: 'pickup', ...(item && { item }), count, bob, ...(data && { data }), ...(name != null && { name }), ...(m != null && { model: m }), ...(color != null && { color }),
|
|
121
122
|
...(radius != null && { radius }), ...(auto && { auto: true }), ...(respawnSeconds > 0 && { respawn: respawnSeconds }), ...(zone != null && { zone }),
|
|
122
|
-
position: [x, placeY(e, `pickups.spawn('${what}')`, x, z,
|
|
123
|
+
position: [x, placeY(e, `pickups.spawn('${what}')`, x, z, yOf(position)) + this.lift, z] };
|
|
123
124
|
const rec = initial ? w.add(def, { initial }) : id != null && w.get(id) || w.add(def); // initial data: world decides whether it exists / was deleted from the save (null)
|
|
124
|
-
if (rec && (rec.kind !== 'pickup' || (rec.item ?? null) !== item)) throw codedError('
|
|
125
|
+
if (rec && (rec.kind !== 'pickup' || (rec.item ?? null) !== item)) throw codedError('world-id', `pickup id '${id}' is already used by other data (${rec.kind}${rec.item ? ' ' + rec.item : ''}): use a different id`);
|
|
125
126
|
const wid = rec?.id ?? id;
|
|
126
127
|
if (mesh || !item) this._live.set(wid, { mesh, give: item ? null : give });
|
|
127
128
|
return { id: wid, give, get object() { return w.view(wid); }, remove: () => { w.remove(wid); this._live.delete(wid); } };
|
|
@@ -148,12 +149,12 @@ export class Pickups {
|
|
|
148
149
|
*/
|
|
149
150
|
_take(rec, actor) {
|
|
150
151
|
const e = this.engine, W = e.world;
|
|
151
|
-
let value;
|
|
152
|
+
let value, left = 0;
|
|
152
153
|
if (rec.item) {
|
|
153
154
|
if (!e.inventory) return failure('noInventory');
|
|
154
|
-
const
|
|
155
|
-
if (!
|
|
156
|
-
value = key;
|
|
155
|
+
const put = e.inventory.of(actor).add(rec.item, rec.count, rec.data && { data: rec.data });
|
|
156
|
+
if (!put.ok) return put;
|
|
157
|
+
value = put.key; left = rec.count - (put.added ?? rec.count); // a bag with room for part of it: the rest stays on the ground
|
|
157
158
|
} else {
|
|
158
159
|
const fn = this._live.get(rec.id)?.give;
|
|
159
160
|
if (!fn) { warnOnce(`pickups: '${rec.id}' gives with a function, which is not saved data; spawn it again with the same id after a load (content and setup code do)`); return failure('noGive'); }
|
|
@@ -161,9 +162,10 @@ export class Pickups {
|
|
|
161
162
|
if (value && value.ok === false) return value;
|
|
162
163
|
}
|
|
163
164
|
// without respawn the record is deleted; with respawn time it gets until (show tears down the model), saved so the countdown continues after a load
|
|
164
|
-
if (
|
|
165
|
+
if (left > 0) W.set(rec.id, { count: left });
|
|
166
|
+
else if (rec.respawn > 0) W.set(rec.id, { until: e.clock.now + rec.respawn });
|
|
165
167
|
else W.remove(rec.id); // world:remove drops the live parts
|
|
166
|
-
e.events.emit('pickup:take', { pickup: rec.id, item: rec.item ?? null, count: rec.count, actor, x: rec.position.x, z: rec.position.z });
|
|
168
|
+
e.events.emit('pickup:take', { pickup: rec.id, item: rec.item ?? null, count: rec.count - left, actor, x: rec.position.x, z: rec.position.z });
|
|
167
169
|
return { ok: true, value };
|
|
168
170
|
}
|
|
169
171
|
|
|
@@ -188,24 +190,25 @@ export class Pickups {
|
|
|
188
190
|
const inReach = (rec, p, r) => { const dx = rec.position.x - p.x, dy = rec.position.y - (p.y ?? 0), dz = rec.position.z - p.z; return dx * dx + dy * dy + dz * dz <= r * r; };
|
|
189
191
|
|
|
190
192
|
/** Register how pickups picture and respawn (once, on install) @internal */
|
|
191
|
-
function present(pk) {
|
|
193
|
+
function present(pk, track) {
|
|
192
194
|
const e = pk.engine, w = e.world;
|
|
193
|
-
w.present('pickup', {
|
|
195
|
+
track(w.present('pickup', {
|
|
194
196
|
radius: pk.showDistance,
|
|
195
197
|
show: rec => rec.until == null, // waiting to respawn: no model
|
|
196
198
|
build: rec => model(pk, rec),
|
|
197
199
|
update: (rec, o) => { // bobbing spin: world places the model at the data position each frame first, this adds the bob on top
|
|
200
|
+
if (!rec.auto && o.userData.useFrom !== e.interactions) { w.rebuild(rec.id); return; } // its use lives in the interactions module it was built with: another one starts empty
|
|
198
201
|
if (rec.bob === false) return;
|
|
199
202
|
const t = e.time.now('real'), x = rec.position.x;
|
|
200
203
|
o.rotation.y = t * 1.5 + x; o.position.y = rec.position.y + Math.sin(t * 2 + x) * 0.08;
|
|
201
204
|
},
|
|
202
205
|
// only detached: a game's mesh is the game's, a model clone shares geometry / material with its prototype (protoOf), the material is shared by every pickup;
|
|
203
206
|
// the placeholder box is the one thing built here alone, so its geometry is freed
|
|
204
|
-
dispose: (rec, o) => { o.userData.useHandle?.remove();
|
|
205
|
-
});
|
|
207
|
+
dispose: (rec, o) => { o.userData.useHandle?.remove(); o.removeFromParent(); o.traverse(c => { if (c.userData.ownGeometry) /** @type {any} */ (c).geometry.dispose(); }); },
|
|
208
|
+
}));
|
|
206
209
|
// waiting to respawn: when due (game clock) clear until and return to the ground. Far ones are also checked once a second, so they grow back on time even when not nearby
|
|
207
210
|
const wake = rec => { if (rec.until != null && e.clock.now >= rec.until) w.set(rec.id, { until: undefined }); };
|
|
208
|
-
w.simulate('pickup', { near: wake, far: wake });
|
|
211
|
+
track(w.simulate('pickup', { near: wake, far: wake }));
|
|
209
212
|
}
|
|
210
213
|
|
|
211
214
|
// A pickup's shape goes one way only: a model name from assets (same protoOf as content props / templates: file or code-generated, cache, bake and shadow rules are identical).
|
|
@@ -213,14 +216,13 @@ function present(pk) {
|
|
|
213
216
|
const NO_PICKUP = "pickups: no model for '{item}', so it lies on the ground as a gray box (a placeholder). Register a model in an asset pack and name it: "
|
|
214
217
|
+ "pickups.spawn(p, { model: 'gem' }) / inventory.define('{item}', { model: 'gem' }), or call the model 'pickup:{item}' (this item) / 'pickup' (every pickup without its own)";
|
|
215
218
|
const NO_NAME = "pickups: '{item}' has model '{model}', but no asset pack registers a model of that name (engine.assets.manifest({ models: { {model}: fn | '/models/{model}.glb' } })); using the gray placeholder box";
|
|
216
|
-
const fill = (t, o) => t.replace(/\{(\w+)\}/g, (_, k) => o[k]);
|
|
217
219
|
/** Which model name a pickup shows (its own model, else the item's, else 'pickup:<item>' / 'pickup'), or null for the placeholder @internal */
|
|
218
220
|
function pickupModelName(e, item, model) {
|
|
219
221
|
const has = n => { const x = e.assets.entries.get(n); return !!x?.named && x.type === 'model'; };
|
|
220
222
|
const what = item ?? 'pickup';
|
|
221
|
-
if (typeof model === 'string') { if (has(model)) return model; warnOnce(
|
|
223
|
+
if (typeof model === 'string') { if (has(model)) return model; warnOnce(fmt(NO_NAME, { item: what, model })); return null; }
|
|
222
224
|
for (const n of item ? ['pickup:' + item, 'pickup'] : ['pickup']) if (has(n)) return n;
|
|
223
|
-
warnOnce(
|
|
225
|
+
warnOnce(fmt(NO_PICKUP, { item: what }));
|
|
224
226
|
return null;
|
|
225
227
|
}
|
|
226
228
|
/**
|
|
@@ -241,7 +243,7 @@ function model(pk, rec) {
|
|
|
241
243
|
};
|
|
242
244
|
if (!name) box();
|
|
243
245
|
else protoOf(e, pk, name).then(proto => {
|
|
244
|
-
const o = proto.obj
|
|
246
|
+
const o = skeletonClone(proto.obj); // own bones if it is skinned; geometry / materials stay the prototype's (shadow flags were settled once there, as for content props)
|
|
245
247
|
// generator gave only geometry: draw it with the pickups material in the item's own color (same color shares one); generators that return a full Object3D / file models stay as they are
|
|
246
248
|
if (proto.fromGeometry) drawnBy(pk, o, color);
|
|
247
249
|
holder.add(o);
|
|
@@ -249,6 +251,7 @@ function model(pk, rec) {
|
|
|
249
251
|
}
|
|
250
252
|
if (rec.auto) return obj; // taken by walking into it: no key prompt
|
|
251
253
|
if (!e.interactions) { warnOnce(coded('module-needs', 'Pickups without auto: true are taken with the interact key: install interactions() first.')); return obj; }
|
|
254
|
+
obj.userData.useFrom = e.interactions;
|
|
252
255
|
obj.userData.useHandle = e.interactions.add(obj, {
|
|
253
256
|
id: rec.id,
|
|
254
257
|
label: () => e.t('pickups.take', { name: pk._name(rec) }), radius: rec.radius ?? pk.radius,
|
|
@@ -279,8 +282,8 @@ function pickupMaterial(pk, mesh) {
|
|
|
279
282
|
}
|
|
280
283
|
|
|
281
284
|
/** The op pickup.take { id }: the interact key, auto pickups, a net peer's request, the dev bridge and tests all run it, as ctx.actor @internal */
|
|
282
|
-
function defineTake(e, pk) {
|
|
283
|
-
|
|
285
|
+
function defineTake(e, ctx, pk) {
|
|
286
|
+
ctx.ops.define('pickup.take', {
|
|
284
287
|
desc: "Take a pickup off the ground: its item goes into the actor's container (or its give function runs for the actor)",
|
|
285
288
|
args: { id: 'string' },
|
|
286
289
|
net: 'peer', // a client's press is sent to the host, which runs it as that peer: the item lands in the peer's bag there and syncs back
|
|
@@ -305,12 +308,32 @@ function defineTake(e, pk) {
|
|
|
305
308
|
const PICKUP_PARAMS = {
|
|
306
309
|
radius: { type: 'number', default: 2, min: 0, desc: 'reach of pickups that set no radius (meters); auto pickups are taken within it' },
|
|
307
310
|
showDistance: { type: 'number', default: 60, min: 0, desc: 'pickups have a model, float and spin only within this distance of the stream sources (meters)' },
|
|
308
|
-
takers: { type: 'any', optional: true, desc: 'e => actor ids that walk into auto pickups (default engine.actor, plus net peers on a host)' },
|
|
311
|
+
takers: { type: 'any', optional: true, desc: 'e => actor ids that walk into auto pickups (default engine.actor and the seats, plus net peers on a host)' },
|
|
309
312
|
color: { type: 'any', default: DEFAULT_COLOR, desc: 'color of the placeholder box and geometry-only models when the pickup and its item set none' },
|
|
310
313
|
size: { type: 'number', default: 0.25, min: 0, desc: 'edge of the placeholder box (meters)' },
|
|
311
314
|
lift: { type: 'number', default: 0.4, desc: 'height above the ground a pickup floats at (meters)' },
|
|
312
315
|
};
|
|
313
316
|
|
|
317
|
+
/**
|
|
318
|
+
* The pickups section of content data: data goes into world (kind 'pickup'). Without a written id: `pickup:<item>:<index in the pickups array>`, stable for the
|
|
319
|
+
* same content (when inserting in the middle, give existing entries an id). Taken / respawning is state
|
|
320
|
+
*/
|
|
321
|
+
const PICKUP_CONTENT = {
|
|
322
|
+
desc: 'pickups on the ground (list)',
|
|
323
|
+
live: true,
|
|
324
|
+
references: d => d.filter(p => typeof p.item === 'string').map((p, i) => ({ section: 'items', id: p.item, at: `pickups[${i}]` })),
|
|
325
|
+
validate(d, { ids, pickupId }) {
|
|
326
|
+
d.forEach((p, i) => { const label = `pickups[${i}] (${p.item})`; if (p.id != null) ids.claim(pickupId(p, i), label); else ids.unnamed(pickupId(p, i), label); });
|
|
327
|
+
},
|
|
328
|
+
commit(d, { engine: e, where, zone, origin, pickupId }) {
|
|
329
|
+
d.forEach((p, i) => {
|
|
330
|
+
const id = pickupId(p, i);
|
|
331
|
+
e.pickups.spawn(where(p), { give: p.item, id, count: p.count, data: p.data, radius: p.radius, auto: p.auto, respawnSeconds: p.respawn, initial: true, zone: zone(p) });
|
|
332
|
+
origin(id, d, i);
|
|
333
|
+
});
|
|
334
|
+
},
|
|
335
|
+
};
|
|
336
|
+
|
|
314
337
|
/**
|
|
315
338
|
* Pickups module: things on the ground that give whoever takes them an item (into that actor's container) or an instant effect. Installs engine.pickups and
|
|
316
339
|
* the op pickup.take. Needs world() first; interactions() for the key prompt (auto: true pickups need none); inventory() only for give: itemId.
|
|
@@ -322,10 +345,11 @@ export function pickups(opts = {}) {
|
|
|
322
345
|
const o = readParams('pickups', PICKUP_PARAMS, opts);
|
|
323
346
|
let pk = null;
|
|
324
347
|
return {
|
|
325
|
-
|
|
348
|
+
content: { pickups: PICKUP_CONTENT },
|
|
349
|
+
name: 'pickups', skipKind: 'exact', type: 'rules', label: 'Pickups', params: PICKUP_PARAMS, options: o, requires: ['world'], optional: ['interactions', 'inventory', 'net'], stage: 'gameplay',
|
|
326
350
|
noSkip: 'auto pickups are spatial; respawn follows the game clock',
|
|
327
351
|
install(e, ctx) {
|
|
328
|
-
pk = new Pickups(e,
|
|
352
|
+
pk = new Pickups(e, o, ctx); defineTake(e, ctx, pk); return pk; // o: the schema defaults are filled in, the class repeats none
|
|
329
353
|
},
|
|
330
354
|
fixed() { pk._auto(); },
|
|
331
355
|
describe(e, ctx) { // use the model if present (same object the interactions module reports, merged into one entry); far away only data
|
|
@@ -334,3 +358,5 @@ export function pickups(opts = {}) {
|
|
|
334
358
|
},
|
|
335
359
|
};
|
|
336
360
|
}
|
|
361
|
+
|
|
362
|
+
export const handRolled = 'none: a pickup by hand is a distance check with no word that sets it apart';
|
|
@@ -39,10 +39,12 @@ function questLogPart(engine) {
|
|
|
39
39
|
* @returns {{ readonly open: boolean, toggle(on?: boolean): void, refresh(): void }}
|
|
40
40
|
* @internal
|
|
41
41
|
*/
|
|
42
|
-
export function questLogWindow(e, q,
|
|
42
|
+
export function questLogWindow(e, q, ctx) {
|
|
43
|
+
const { track } = ctx;
|
|
43
44
|
/** @returns {QuestLogView} */
|
|
44
45
|
const view = () => ({ title: e.t('quests.title'), key: e.input.label(q.action), quests: q.list(), close: () => e.ui.close(NAME) });
|
|
45
|
-
|
|
46
|
+
ctx.ui.define(NAME, questLogPart, { modal: { cancel: eng => eng.ui.close(NAME), data: view } });
|
|
47
|
+
track(e.events.on('session:end', () => e.ui.close(NAME))); // it shows the playthrough that ended
|
|
46
48
|
const win = {
|
|
47
49
|
get open() { return e.ui.isOpen(NAME); },
|
|
48
50
|
toggle(on = !win.open) { if (on) e.ui.open(NAME); else e.ui.close(NAME); },
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
// Quest progress in saves (part 'quests', version
|
|
1
|
+
// Quest progress in saves (part 'quests', version 3: visited added; owed is optional, so older saves read as they are): saved by stable step and part ids, never by position, so content may insert or reorder steps.
|
|
2
2
|
// { state: { questId: { step: stepId | null (when done), done, failed?, parts?: [ids of parts met in the current parallel step],
|
|
3
3
|
// got?: { stepId: [ids of parts met in a finished parallel step] } } } }
|
|
4
4
|
// A step's (or parallel part's) id is its `id` field (quests.define / content: steps: [{ id: 'talk-mayor', text, done }]), else its position as text
|
|
5
5
|
// ('0', '1', ...): give steps ids before shipping, then edits to the step list never move a player's progress.
|
|
6
6
|
// Version 1 saved positions: migrateV1 maps them to ids through the quest definitions known when the save is loaded.
|
|
7
|
-
import { warnOnce } from '../../core/validate.js';
|
|
8
|
-
import { coded } from '../../core/errorHints.js';
|
|
7
|
+
import { warnOnce, fmt } from '../../core/base/validate.js';
|
|
8
|
+
import { coded } from '../../core/base/errorHints.js';
|
|
9
9
|
|
|
10
10
|
const STRINGS = {
|
|
11
11
|
missing: "quests: the save has quest '{q}' at step '{step}', which its definition no longer has; it restarts from the first step. Keep step ids stable, or add a migrate step to the 'quests' save part",
|
|
@@ -30,6 +30,8 @@ const indexOf = (def, id) => def.steps.findIndex((_, i) => stepId(def, i) === id
|
|
|
30
30
|
export function toSaved(def, s) {
|
|
31
31
|
const out = { step: s.done ? null : stepId(def, s.step), done: !!s.done };
|
|
32
32
|
if (s.failed) out.failed = true;
|
|
33
|
+
if (s.owed) out.owed = { ...s.owed };
|
|
34
|
+
if (s.visited?.length) out.visited = [...s.visited];
|
|
33
35
|
if (s.parts) out.parts = metIds(def.steps[s.step], s.parts);
|
|
34
36
|
if (s.got) out.got = Object.fromEntries(Object.entries(s.got).map(([i, flags]) => [stepId(def, +i), metIds(def.steps[+i], flags)]));
|
|
35
37
|
return out;
|
|
@@ -44,9 +46,12 @@ export function toSaved(def, s) {
|
|
|
44
46
|
*/
|
|
45
47
|
export function fromSaved(id, def, q) {
|
|
46
48
|
let step = q.done ? def.steps.length : indexOf(def, q.step);
|
|
47
|
-
if (step < 0) { warnOnce(coded('quest-step', STRINGS.missing
|
|
49
|
+
if (step < 0) { warnOnce(coded('quest-step', fmt(STRINGS.missing, { q: id, step: String(q.step) }))); step = 0; }
|
|
48
50
|
const s = { step, done: !!q.done };
|
|
49
51
|
if (q.failed) s.failed = true;
|
|
52
|
+
if (q.owed) s.owed = { ...q.owed };
|
|
53
|
+
// a save from before visited went through the list in order, so the steps behind this one are what it left
|
|
54
|
+
s.visited = Array.isArray(q.visited) ? q.visited.filter(v => typeof v === 'string' && indexOf(def, v) >= 0) : def.steps.slice(0, step).map((_, i) => stepId(def, i));
|
|
50
55
|
const st = def.steps[step];
|
|
51
56
|
if (q.parts && st?.all) s.parts = metFlags(st, q.parts);
|
|
52
57
|
for (const [sid, ids] of Object.entries(q.got || {})) {
|
package/modules/grid/Grid.js
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
// mapping is one replaceable pair (grids.add({ mapping })), so a game with another lattice swaps two functions instead of every caller's math.
|
|
8
8
|
// A footprint is anchored at its min corner cell; rotation is in quarter turns (0-3) and odd turns swap width and depth.
|
|
9
9
|
import { Layer } from './Layer.js';
|
|
10
|
-
import { xzOf } from '../../core/math.js';
|
|
10
|
+
import { xzOf } from '../../core/base/math.js';
|
|
11
11
|
|
|
12
12
|
/** The default occupancy layer of an item that names none */
|
|
13
13
|
export const MAIN = 'main';
|
|
@@ -16,7 +16,7 @@ export const occKey = (occupy, level, i, j) => `${occupy}|${level}|${i},${j}`;
|
|
|
16
16
|
const cap = s => s[0].toUpperCase() + s.slice(1);
|
|
17
17
|
const FLAT_SAMPLES = [[0, 0], [-1, -1], [1, -1], [-1, 1], [1, 1]];
|
|
18
18
|
// [x, z] here (a grid is flat), [x, y, z] / Vector3 / Object3D / { x, z } as everywhere else
|
|
19
|
-
const planar = v => (Array.isArray(v) && v.length === 2 ? v : xzOf(v));
|
|
19
|
+
export const planar = v => (Array.isArray(v) && v.length === 2 ? v : xzOf(v));
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* A placement grid. Several can exist at once (a yard outdoors, a room indoors); each has its own cell size, origin and bounds.
|
package/modules/grid/Layer.js
CHANGED
|
@@ -6,8 +6,10 @@
|
|
|
6
6
|
// Version 1 saved every cell whole; its step turns that into { set, whole: true } (cells not listed are empty, whatever content authors now).
|
|
7
7
|
// Writes are saved gameplay state, so they pass the ops guard like world.set (write inside an op or a fixed step).
|
|
8
8
|
// _ver counts changes so views (tiles) rebuild only when something changed, without events.
|
|
9
|
-
import { DEV } from '../../core/validate.js';
|
|
10
|
-
import { codedError } from '../../core/errorHints.js';
|
|
9
|
+
import { DEV, needMaterial } from '../../core/base/validate.js';
|
|
10
|
+
import { codedError } from '../../core/base/errorHints.js';
|
|
11
|
+
import { isPlainObject } from '../../core/base/util.js';
|
|
12
|
+
import { refuseSave, saveNumberOk } from '../../core/base/saveValues.js';
|
|
11
13
|
|
|
12
14
|
const key = (i, j) => i + ',' + j;
|
|
13
15
|
const plainErr = (where, v) => `${where}: layer data must be plain values (numbers, strings, booleans, arrays, plain objects); got ${v?.constructor?.name ?? typeof v}. Keep models in tiles / world.present`;
|
|
@@ -51,8 +53,8 @@ export class Layer {
|
|
|
51
53
|
get material() { return this.grid._owner._tiles.get(this)?.mesh?.material ?? null; }
|
|
52
54
|
set material(m) {
|
|
53
55
|
const mesh = this.grid._owner._tiles.get(this)?.mesh;
|
|
54
|
-
if (!mesh) throw codedError('grids-misuse', `layer '${this.name}'.material: this layer draws no default tiles
|
|
55
|
-
|
|
56
|
+
if (!mesh) throw codedError('grids-misuse', `layer '${this.name}'.material: this layer draws no default tiles, and a layer takes that when it is created: first call grid.layer('${this.name}', { tiles: true })`);
|
|
57
|
+
needMaterial('grids-misuse', `layer '${this.name}'.material`, m);
|
|
56
58
|
mesh.material = m;
|
|
57
59
|
}
|
|
58
60
|
/** @internal */
|
|
@@ -74,7 +76,7 @@ export class Layer {
|
|
|
74
76
|
*/
|
|
75
77
|
set([i, j], patch) {
|
|
76
78
|
this._guard('set');
|
|
77
|
-
|
|
79
|
+
checkPlain(`layer ${this.name}.set`, patch);
|
|
78
80
|
const k = key(i, j), d = { ...this._cells.get(k) };
|
|
79
81
|
for (const [f, v] of Object.entries(patch)) { if (v == null) delete d[f]; else d[f] = v; }
|
|
80
82
|
if (Object.keys(d).length) this._cells.set(k, seal(d)); else this._cells.delete(k);
|
|
@@ -115,7 +117,7 @@ export class Layer {
|
|
|
115
117
|
const copy = { ...d }, r = fn(copy, /** @type {[number, number]} */ (k.split(',').map(Number)));
|
|
116
118
|
const v = r === undefined ? copy : r;
|
|
117
119
|
if (v == null || !Object.keys(v).length) this._cells.delete(k);
|
|
118
|
-
else {
|
|
120
|
+
else { checkPlain(`layer ${this.name}.updateAll`, v); this._cells.set(k, seal(v === d ? { ...v } : v)); }
|
|
119
121
|
}
|
|
120
122
|
this._ver++;
|
|
121
123
|
return this._cells.size;
|
|
@@ -136,6 +138,8 @@ export class Layer {
|
|
|
136
138
|
get _same() { const d = this.toJSON(); return !d.set.length && !d.clear; }
|
|
137
139
|
/** @internal a save diff (or null = none): baseline first, then the diff */
|
|
138
140
|
fromJSON(d) {
|
|
141
|
+
const rows = l => l == null || (Array.isArray(l) && l.every(c => Array.isArray(c) && saveNumberOk(c[0]) && saveNumberOk(c[1])));
|
|
142
|
+
if (d != null && !(isPlainObject(d) && rows(d.set) && rows(d.clear) && (d.set ?? []).every(c => isPlainObject(c[2])))) refuseSave('grids', `layer '${this.name}'`, 'must be { set: [[i, j, data], ...], clear: [[i, j], ...] }', d);
|
|
139
143
|
this._diff = d ?? null;
|
|
140
144
|
this._cells = new Map(d?.whole ? [] : this._base);
|
|
141
145
|
for (const [i, j, v] of d?.set ?? []) this._cells.set(key(i, j), seal(v));
|
|
@@ -144,13 +148,13 @@ export class Layer {
|
|
|
144
148
|
}
|
|
145
149
|
/** @internal authored cells ([[i, j, data], ...] from content/grids.js): the baseline; the last loaded diff is applied on top again */
|
|
146
150
|
_setBase(rows) {
|
|
147
|
-
for (const [i, j, v] of rows ?? []) {
|
|
151
|
+
for (const [i, j, v] of rows ?? []) { checkPlain(`content grids layer ${this.name}`, v); this._base.set(key(i, j), seal(structuredClone(v))); }
|
|
148
152
|
this.fromJSON(this._diff);
|
|
149
153
|
}
|
|
150
154
|
}
|
|
151
155
|
|
|
152
156
|
/**
|
|
153
157
|
* Options of grid.layer(name, opts)
|
|
154
|
-
* @typedef {{ tiles?: boolean | {
|
|
158
|
+
* @typedef {{ tiles?: boolean | { color?: (data: Record<string, any>, cell: [number, number]) => any, colors?: Record<string, number | string>, lift?: number }
|
|
155
159
|
* | ((engine: any, layer: Layer) => { update?: (cells: { cell: [number, number], data: Record<string, any> }[]) => void, dispose?: () => void }) }} LayerOptions
|
|
156
160
|
*/
|
package/modules/grid/index.js
CHANGED
|
@@ -9,18 +9,19 @@
|
|
|
9
9
|
// Picture: grids({ ok, bad, previewOpacity, previewHeight, tiles, tileLift }) for the plain values, pv.material and layer.material for the three.js materials.
|
|
10
10
|
// The index is the "don't go around it" check: in dev a record's grid field is frozen (editing it in place throws), writes outside ops
|
|
11
11
|
// trigger the ops guard, and a record whose footprint overlaps another one is reported with the fix.
|
|
12
|
-
import {
|
|
12
|
+
import { keepTable } from '../../core/base/sections.js';
|
|
13
|
+
import { Grid, MAIN, occKey, planar } from './Grid.js';
|
|
13
14
|
import { layerTiles } from './tiles.js';
|
|
14
15
|
import { makePreview } from './preview.js';
|
|
15
16
|
import { defineGridOps, placed } from './ops.js';
|
|
16
17
|
|
|
17
18
|
/** Grids save part: version 1 stored every cell of a layer whole ([[i, j, data]]); version 2 stores a diff against the authored cells (Layer.js) */
|
|
18
19
|
const GRIDS_MIGRATE = { 1: d => Object.fromEntries(Object.entries(d || {}).map(([g, ls]) => [g, Object.fromEntries(Object.entries(ls || {}).map(([n, rows]) => [n, { set: rows, whole: true }]))])) };
|
|
19
|
-
import { closest, DEV, warnOnce } from '../../core/validate.js';
|
|
20
|
-
import { readParams, keysSchema } from '../../core/params.js';
|
|
21
|
-
import { failure } from '../../core/opsFail.js';
|
|
20
|
+
import { closest, DEV, warnOnce, fmt } from '../../core/base/validate.js';
|
|
21
|
+
import { readParams, keysSchema } from '../../core/base/params.js';
|
|
22
|
+
import { failure } from '../../core/ops/opsFail.js';
|
|
22
23
|
import { STRINGS as TEXT } from './strings.js';
|
|
23
|
-
import { codedError, coded } from '../../core/errorHints.js';
|
|
24
|
+
import { codedError, coded } from '../../core/base/errorHints.js';
|
|
24
25
|
|
|
25
26
|
const STRINGS = {
|
|
26
27
|
dup: "grids.add('{name}'): a grid with this name exists",
|
|
@@ -29,7 +30,6 @@ const STRINGS = {
|
|
|
29
30
|
orphan: "[engine] grid '{grid}': {id} is a '{item}', which this content no longer defines; it is kept in the save but takes no cells. Define '{item}' again (engine.grids.define) to bring it back, or remove it with engine.ops.run('grid.remove', { id })",
|
|
30
31
|
overlap: "[engine] grid '{grid}': {id} overlaps {other} at cell {cell}; the cell stays with {other}. Place things with engine.ops.run('grid.place', ...) / 'grid.move', which check can() first",
|
|
31
32
|
};
|
|
32
|
-
const fmt = (s, v) => s.replace(/\{(\w+)\}/g, (_, k) => v[k] ?? '');
|
|
33
33
|
const hint = (n, list) => { const c = closest(n, list); return c ? ` (did you mean '${c}'?)` : ''; };
|
|
34
34
|
const freeze = p => { Object.freeze(p.cell); Object.freeze(p.size); return Object.freeze(p); };
|
|
35
35
|
|
|
@@ -62,6 +62,8 @@ export class Grids {
|
|
|
62
62
|
/** @internal */ this._previews = new Set();
|
|
63
63
|
/** @internal layer data from a save, for layers created after the load */ this._loaded = null;
|
|
64
64
|
}
|
|
65
|
+
/** @internal A failed content load takes its grids back (core/base/sections.js undo): the function that does it @returns {() => void} */
|
|
66
|
+
undoPoint() { const back = keepTable(this._grids); return () => { back(); this._rebuild(); }; }
|
|
65
67
|
/**
|
|
66
68
|
* Create a grid. cell: edge length (m, default 1); origin: world [x, z] of the min corner of cell [0, 0]; size: [cells wide, cells deep] (omit = unbounded);
|
|
67
69
|
* zone: level name (tiles only show there); flat: max height difference under a footprint (m, default 0.5);
|
|
@@ -81,6 +83,10 @@ export class Grids {
|
|
|
81
83
|
const m = opts.mapping;
|
|
82
84
|
if (m && !(typeof m.toCell === 'function' && typeof m.center === 'function')) throw codedError('grids-misuse', `grids.add('${name}'): mapping needs both toCell(x, z) => [i, j] and center([i, j]) => [x, z]`, TypeError);
|
|
83
85
|
if (this._grids.has(name)) throw codedError('grids-misuse', fmt(STRINGS.dup, { name }));
|
|
86
|
+
// a cell of 0 or NaN turns every position into cell NaN: placed things collide with nothing and every later lookup misses
|
|
87
|
+
const { cell = 1, origin = [0, 0], size = null } = opts, whole = n => Number.isSafeInteger(n) && n > 0;
|
|
88
|
+
if (!(cell > 0 && Number.isFinite(cell)) || !planar(origin).every(Number.isFinite) || !(size === null || (Array.isArray(size) && size.length === 2 && size.every(whole))))
|
|
89
|
+
throw codedError('grids-misuse', `grids.add('${name}'): cell must be a number > 0, origin finite and size [width, depth] whole numbers > 0 or left out; got ${JSON.stringify({ cell, origin, size })}`);
|
|
84
90
|
const g = new Grid(this.engine, this, name, opts);
|
|
85
91
|
this._grids.set(name, g);
|
|
86
92
|
for (const [ln, rows] of Object.entries(opts.layers ?? {})) { const l = g.layer(ln); l._byContent = true; l._setBase(rows); }
|
|
@@ -88,11 +94,11 @@ export class Grids {
|
|
|
88
94
|
return g;
|
|
89
95
|
}
|
|
90
96
|
/**
|
|
91
|
-
* A grid by name, or
|
|
97
|
+
* A grid by name, or undefined when there is none
|
|
92
98
|
* @param {string} name
|
|
93
|
-
* @returns {Grid |
|
|
99
|
+
* @returns {Grid | undefined}
|
|
94
100
|
*/
|
|
95
|
-
get(name) { return this._grids.get(name)
|
|
101
|
+
get(name) { return this._grids.get(name); }
|
|
96
102
|
/** @internal A grid by name; throws with the names that exist */
|
|
97
103
|
_must(name) {
|
|
98
104
|
const g = this._grids.get(name);
|
|
@@ -259,31 +265,30 @@ const GRID_PARAMS = {
|
|
|
259
265
|
*/
|
|
260
266
|
export function grids(opts = {}) {
|
|
261
267
|
const o = readParams('grids', GRID_PARAMS, opts);
|
|
262
|
-
let G = null
|
|
263
|
-
const subs = [];
|
|
268
|
+
let G = null;
|
|
264
269
|
return {
|
|
265
|
-
|
|
266
|
-
|
|
270
|
+
// layout rules, not saved, before props: a props entry with grid places an item on one; a repeated load keeps the grid that exists
|
|
271
|
+
content: { grids: { desc: 'placement grids (name -> { cell, origin, size, zone?, flat? }); layout rules, not saved', undo: (d, { engine: e }) => e.grids.undoPoint(), commit(d, { engine: e }) { for (const [name, g] of Object.entries(d)) if (!e.grids.get(name)) e.grids.add(name, g); } } },
|
|
272
|
+
name: 'grids', skipKind: 'none', type: 'world', label: 'Grid placement', stage: 'gameplay', requires: ['world'], params: GRID_PARAMS, options: o,
|
|
273
|
+
install(e, ctx) {
|
|
267
274
|
G = new Grids(e, o);
|
|
268
275
|
e.i18n.add(TEXT); // grid.<code> player text for every built-in failure code, and the rotate action's label
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
e.events.on('world:load', () => G._rebuild()),
|
|
276
|
-
);
|
|
276
|
+
ctx.saves.register('grids', { atomic: true, version: 2, migrate: GRIDS_MIGRATE, save: () => G.toJSON(), load: d => G.fromJSON(d) });
|
|
277
|
+
defineGridOps(ctx, G);
|
|
278
|
+
ctx.track(e.events.on('world:add', ({ id }) => G._reindex(id)));
|
|
279
|
+
ctx.track(e.events.on('world:remove', ({ id }) => G._unindex(id)));
|
|
280
|
+
ctx.track(e.events.on('world:change', ({ id, keys }) => { if (keys.includes('grid')) G._reindex(id); }));
|
|
281
|
+
ctx.track(e.events.on('world:load', () => G._rebuild()));
|
|
277
282
|
return G;
|
|
278
283
|
},
|
|
279
284
|
frame() { for (const t of G._tiles.values()) t.frame(); for (const p of G._previews) p.frame(); },
|
|
280
285
|
explain: { grid: (e, q) => G._explain(q) },
|
|
281
286
|
describe() { return G._describe(); },
|
|
282
287
|
dispose() {
|
|
283
|
-
for (const s of subs) s.remove();
|
|
284
|
-
offOps?.();
|
|
285
288
|
for (const t of G._tiles.values()) t.dispose();
|
|
286
289
|
for (const p of [...G._previews]) p.remove();
|
|
287
290
|
},
|
|
288
291
|
};
|
|
289
292
|
}
|
|
293
|
+
|
|
294
|
+
export const handRolled = 'none: cell maths is one Math.floor that any other code writes too';
|