threedog 0.0.0-stage → 0.1.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 +5 -0
- package/LICENSE +21 -0
- package/README.md +39 -2
- package/core/Animator.js +267 -0
- package/core/Assets.js +240 -0
- package/core/Audio.js +267 -0
- package/core/Baked.js +72 -0
- package/core/Batching.js +247 -0
- package/core/Budget.js +221 -0
- package/core/Clock.js +94 -0
- package/core/Commits.js +77 -0
- package/core/Describe.js +134 -0
- package/core/Engine.js +221 -0
- package/core/Events.js +95 -0
- package/core/I18n.js +94 -0
- package/core/Input.js +245 -0
- package/core/Inspect.js +97 -0
- package/core/InspectScene.js +187 -0
- package/core/Jobs.js +146 -0
- package/core/Loading.js +149 -0
- package/core/Ops.js +272 -0
- package/core/Random.js +52 -0
- package/core/Rays.js +160 -0
- package/core/SaveStore.js +85 -0
- package/core/Saves.js +253 -0
- package/core/Stream.js +279 -0
- package/core/Threads.js +158 -0
- package/core/UrlState.js +93 -0
- package/core/Vars.js +34 -0
- package/core/actor.js +23 -0
- package/core/assetEntry.js +89 -0
- package/core/assetLoad.js +190 -0
- package/core/assetManifest.js +35 -0
- package/core/audioRules.js +121 -0
- package/core/audioVoice.js +225 -0
- package/core/batchGroups.js +116 -0
- package/core/canvasBox.js +63 -0
- package/core/dayTime.js +15 -0
- package/core/describeText.js +136 -0
- package/core/easing.js +34 -0
- package/core/errorCodes.js +172 -0
- package/core/errorHints.js +203 -0
- package/core/errors.js +40 -0
- package/core/events.generated.js +84 -0
- package/core/ground.js +102 -0
- package/core/inputContext.js +69 -0
- package/core/inputDefaults.js +22 -0
- package/core/inputPad.js +35 -0
- package/core/inputPointer.js +48 -0
- package/core/inputTouch.js +48 -0
- package/core/lifecycle.js +68 -0
- package/core/lightWatch.js +36 -0
- package/core/loop.js +199 -0
- package/core/math.js +33 -0
- package/core/modules.js +182 -0
- package/core/movement.js +19 -0
- package/core/mulberry32.js +14 -0
- package/core/opsCheck.js +86 -0
- package/core/opsGuard.js +26 -0
- package/core/opsHistory.js +27 -0
- package/core/page.js +78 -0
- package/core/pureWorker.js +5 -0
- package/core/saveApply.js +128 -0
- package/core/saveFiles.js +139 -0
- package/core/saveIds.js +53 -0
- package/core/saveVersions.js +97 -0
- package/core/scatter.js +92 -0
- package/core/sceneMethods.js +125 -0
- package/core/screenToGround.js +39 -0
- package/core/session.js +171 -0
- package/core/shadowTypes.js +15 -0
- package/core/skip.js +104 -0
- package/core/startup.js +133 -0
- package/core/streamCells.js +24 -0
- package/core/streamSources.js +73 -0
- package/core/styleParts.js +96 -0
- package/core/targeting.js +64 -0
- package/core/thingOps.js +157 -0
- package/core/things.js +198 -0
- package/core/threadPure.js +120 -0
- package/core/threadServe.js +25 -0
- package/core/tier.js +11 -0
- package/core/validate.js +188 -0
- package/docs/AI.md +149 -0
- package/docs/API.md +84 -0
- package/docs/CODEMAP.md +335 -0
- package/docs/COMMANDS.md +96 -0
- package/docs/CONVENTIONS.md +134 -0
- package/docs/PERF.md +217 -0
- package/docs/THREE.md +86 -0
- package/docs/api/ambience.md +15 -0
- package/docs/api/assets.md +60 -0
- package/docs/api/audio.md +53 -0
- package/docs/api/budget.md +40 -0
- package/docs/api/cameraRig.md +120 -0
- package/docs/api/characters.md +142 -0
- package/docs/api/clock.md +7 -0
- package/docs/api/collision.md +35 -0
- package/docs/api/content.md +63 -0
- package/docs/api/debug.md +10 -0
- package/docs/api/dialogue.md +39 -0
- package/docs/api/engine.md +190 -0
- package/docs/api/entities.md +61 -0
- package/docs/api/events.md +91 -0
- package/docs/api/flow.md +27 -0
- package/docs/api/grids.md +11 -0
- package/docs/api/ground.md +16 -0
- package/docs/api/hlod.md +34 -0
- package/docs/api/hud.md +31 -0
- package/docs/api/i18n.md +10 -0
- package/docs/api/input.md +24 -0
- package/docs/api/interactions.md +31 -0
- package/docs/api/inventory.md +66 -0
- package/docs/api/jobs.md +10 -0
- package/docs/api/level.md +41 -0
- package/docs/api/lightPool.md +56 -0
- package/docs/api/music.md +34 -0
- package/docs/api/nav.md +26 -0
- package/docs/api/net.md +14 -0
- package/docs/api/openWorld.md +24 -0
- package/docs/api/particles.md +41 -0
- package/docs/api/photo.md +15 -0
- package/docs/api/post.md +9 -0
- package/docs/api/quests.md +36 -0
- package/docs/api/rays.md +26 -0
- package/docs/api/rigid.md +84 -0
- package/docs/api/saves.md +31 -0
- package/docs/api/settings.md +36 -0
- package/docs/api/sky.md +58 -0
- package/docs/api/stream.md +31 -0
- package/docs/api/style.md +134 -0
- package/docs/api/terrain.md +34 -0
- package/docs/api/timeline.md +39 -0
- package/docs/api/tools.md +55 -0
- package/docs/api/triggers.md +56 -0
- package/docs/api/vars.md +9 -0
- package/docs/api/vegetation.md +52 -0
- package/docs/api/water.md +48 -0
- package/docs/api/weather.md +29 -0
- package/docs/api/world.md +30 -0
- package/docs/recipes/01-new-game.md +148 -0
- package/docs/recipes/02-npc.md +151 -0
- package/docs/recipes/03-items-pickups.md +158 -0
- package/docs/recipes/04-quests.md +83 -0
- package/docs/recipes/05-props-bake.md +159 -0
- package/docs/recipes/06-clock-events.md +104 -0
- package/docs/recipes/07-flow-scenes-pause.md +128 -0
- package/docs/recipes/08-weather-sky.md +74 -0
- package/docs/recipes/09-style-pack.md +234 -0
- package/docs/recipes/10-blender-level.md +67 -0
- package/docs/recipes/11-vehicles.md +55 -0
- package/docs/recipes/12-navmesh.md +57 -0
- package/docs/recipes/13-animation.md +146 -0
- package/docs/recipes/14-custom-module.md +91 -0
- package/docs/recipes/15-perf.md +78 -0
- package/docs/recipes/16-dev-bridge.md +146 -0
- package/docs/recipes/18-world.md +185 -0
- package/docs/recipes/19-shooting-picking.md +76 -0
- package/docs/recipes/20-content-actions-save.md +176 -0
- package/docs/recipes/21-camera.md +104 -0
- package/docs/recipes/22-shop-gold.md +107 -0
- package/docs/recipes/23-ops.md +139 -0
- package/docs/recipes/26-ui-parts.md +138 -0
- package/docs/recipes/27-net.md +96 -0
- package/docs/recipes/28-terrain.md +80 -0
- package/docs/recipes/29-grid.md +87 -0
- package/docs/recipes/30-templates.md +131 -0
- package/docs/recipes/31-audio.md +126 -0
- package/docs/recipes/32-action-game.md +33 -0
- package/docs/recipes/33-stream-sources.md +37 -0
- package/docs/recipes/34-environment-lighting.md +35 -0
- package/docs/recipes/35-style-from-reference.md +89 -0
- package/docs/recipes/36-sky-ground-plant-looks.md +116 -0
- package/docs/recipes/37-terrain-look-hooks.md +97 -0
- package/docs/recipes/38-environment-look-hooks.md +81 -0
- package/docs/recipes/40-scene.md +79 -0
- package/docs/recipes/41-thing-registry.md +64 -0
- package/docs/recipes/42-terrain-shape.md +64 -0
- package/docs/recipes/43-shader-patches-surface-looks.md +118 -0
- package/docs/recipes/44-water-weather-look-hooks.md +133 -0
- package/docs/recipes/45-cascaded-shadows.md +51 -0
- package/docs/recipes/46-blender-art-route.md +92 -0
- package/docs/recipes/47-island-snow-backdrop.md +44 -0
- package/examples/action/DESIGN.md +3 -0
- package/examples/action/GAMEMAP.md +127 -0
- package/examples/action/content/actions.js +8 -0
- package/examples/action/content/audio.js +9 -0
- package/examples/action/content/index.js +9 -0
- package/examples/action/content/items.js +4 -0
- package/examples/action/content/places.js +4 -0
- package/examples/action/content/props.js +11 -0
- package/examples/action/content/templates.js +10 -0
- package/examples/action/look.js +5 -0
- package/examples/action/main.js +137 -0
- package/examples/action/models.js +25 -0
- package/examples/sheepisland/GAMEMAP.md +108 -0
- package/examples/sheepisland/content/index.js +6 -0
- package/examples/sheepisland/content/props.js +19 -0
- package/examples/sheepisland/content/templates.js +5 -0
- package/examples/sheepisland/content/vegetation.js +8 -0
- package/examples/sheepisland/main.js +71 -0
- package/examples/sheepisland/params.js +25 -0
- package/examples/sheepisland/style/index.js +79 -0
- package/examples/sheepisland/style/models.js +59 -0
- package/examples/sheepisland/views.js +9 -0
- package/examples/styletest/GAMEMAP.md +135 -0
- package/examples/styletest/content/index.js +9 -0
- package/examples/styletest/content/items.js +8 -0
- package/examples/styletest/content/npcs.js +8 -0
- package/examples/styletest/content/places.js +5 -0
- package/examples/styletest/content/props.js +16 -0
- package/examples/styletest/content/templates.js +9 -0
- package/examples/styletest/content/vegetation.js +31 -0
- package/examples/styletest/look.js +11 -0
- package/examples/styletest/main.js +95 -0
- package/examples/styletest/models.js +87 -0
- package/examples/styletest/overrides.js +33 -0
- package/examples/styletest/panel.js +135 -0
- package/examples/styletest/params.js +32 -0
- package/examples/styletest/views.js +31 -0
- package/examples/world/DESIGN.md +5 -0
- package/examples/world/GAMEMAP.md +171 -0
- package/examples/world/content/actions.js +8 -0
- package/examples/world/content/audio.js +25 -0
- package/examples/world/content/bodies.js +12 -0
- package/examples/world/content/containers.js +4 -0
- package/examples/world/content/dialogues.js +33 -0
- package/examples/world/content/index.js +17 -0
- package/examples/world/content/items.js +10 -0
- package/examples/world/content/npcs.js +14 -0
- package/examples/world/content/pickups.js +11 -0
- package/examples/world/content/places.js +6 -0
- package/examples/world/content/props.js +12 -0
- package/examples/world/content/quests.js +15 -0
- package/examples/world/content/rules.js +23 -0
- package/examples/world/content/templates.js +5 -0
- package/examples/world/content/vegetation.js +32 -0
- package/examples/world/main.js +168 -0
- package/examples/world/models.js +28 -0
- package/index.d.ts +279 -0
- package/index.js +120 -0
- package/llms.txt +56 -0
- package/modules/ambience/Ambience.js +183 -0
- package/modules/camera/FirstPersonCamera.js +133 -0
- package/modules/camera/FixedCamera.js +155 -0
- package/modules/camera/FollowCamera.js +85 -0
- package/modules/camera/RtsCamera.js +134 -0
- package/modules/camera/common.js +152 -0
- package/modules/camera/index.js +11 -0
- package/modules/characters/Character.js +223 -0
- package/modules/characters/NPC.js +242 -0
- package/modules/characters/assetModel.js +42 -0
- package/modules/characters/astar.js +59 -0
- package/modules/characters/behaviors.js +72 -0
- package/modules/characters/footsteps.js +13 -0
- package/modules/characters/index.js +212 -0
- package/modules/characters/moveHelpers.js +29 -0
- package/modules/characters/movers/common.js +62 -0
- package/modules/characters/movers/rapier.js +74 -0
- package/modules/characters/movers/simple.js +38 -0
- package/modules/characters/npcAvoid.js +129 -0
- package/modules/characters/npcSim.js +144 -0
- package/modules/characters/npcView.js +42 -0
- package/modules/characters/pathAsync.js +107 -0
- package/modules/characters/pathJob.js +61 -0
- package/modules/characters/pathWorker.js +7 -0
- package/modules/characters/playerStrings.js +12 -0
- package/modules/characters/proceduralRig.js +54 -0
- package/modules/collision/Collision.js +176 -0
- package/modules/collision/grid.js +63 -0
- package/modules/content/ids.js +95 -0
- package/modules/content/index.js +152 -0
- package/modules/content/loader.js +187 -0
- package/modules/content/models.js +69 -0
- package/modules/content/packs.js +69 -0
- package/modules/content/props.js +89 -0
- package/modules/content/rules.js +133 -0
- package/modules/content/templates.js +189 -0
- package/modules/debug/DebugPanel.js +168 -0
- package/modules/entities/Entities.js +239 -0
- package/modules/entities/build.js +25 -0
- package/modules/entities/index.js +4 -0
- package/modules/flow/index.js +98 -0
- package/modules/gameplay/Container.js +227 -0
- package/modules/gameplay/Containers.js +89 -0
- package/modules/gameplay/Dialogue.js +216 -0
- package/modules/gameplay/Interactions.js +243 -0
- package/modules/gameplay/Inventory.js +214 -0
- package/modules/gameplay/Quests.js +195 -0
- package/modules/gameplay/boxOps.js +60 -0
- package/modules/gameplay/dialogueBox.js +48 -0
- package/modules/gameplay/interactionPrompt.js +28 -0
- package/modules/gameplay/inventoryOps.js +137 -0
- package/modules/gameplay/pickups.js +113 -0
- package/modules/gameplay/questLog.js +54 -0
- package/modules/gameplay/questSave.js +71 -0
- package/modules/grid/Grid.js +184 -0
- package/modules/grid/Layer.js +142 -0
- package/modules/grid/index.js +236 -0
- package/modules/grid/ops.js +73 -0
- package/modules/grid/preview.js +85 -0
- package/modules/grid/tiles.js +75 -0
- package/modules/hlod/index.js +164 -0
- package/modules/hud/index.js +164 -0
- package/modules/hud/parts.js +80 -0
- package/modules/level/geometry.js +46 -0
- package/modules/level/heightfield.js +160 -0
- package/modules/level/index.js +231 -0
- package/modules/level/nodes.js +84 -0
- package/modules/lights/index.js +224 -0
- package/modules/music/Music.js +290 -0
- package/modules/music/index.js +2 -0
- package/modules/music/rules.js +28 -0
- package/modules/nav/index.js +154 -0
- package/modules/nav/navPath.js +17 -0
- package/modules/net/Client.js +136 -0
- package/modules/net/Host.js +117 -0
- package/modules/net/Net.js +49 -0
- package/modules/net/index.js +75 -0
- package/modules/net/sync.js +114 -0
- package/modules/net/transports.js +118 -0
- package/modules/particles/Particles.js +179 -0
- package/modules/particles/effect.js +112 -0
- package/modules/particles/shaders.js +28 -0
- package/modules/photo/Photo.js +143 -0
- package/modules/rigid/RigidBodies.js +262 -0
- package/modules/rigid/Vehicles.js +97 -0
- package/modules/rigid/sync.js +158 -0
- package/modules/settings/Settings.js +294 -0
- package/modules/settings/apply.js +20 -0
- package/modules/settings/index.js +3 -0
- package/modules/settings/sections.js +156 -0
- package/modules/settings/strings.js +53 -0
- package/modules/settings/view.js +65 -0
- package/modules/sky/Sky.js +257 -0
- package/modules/sky/calendar.js +80 -0
- package/modules/sky/cascades.js +125 -0
- package/modules/sky/clouds.js +79 -0
- package/modules/sky/domeLayers.js +33 -0
- package/modules/sky/environment.js +115 -0
- package/modules/sky/fog.js +136 -0
- package/modules/sky/frame.js +76 -0
- package/modules/sky/layers.js +69 -0
- package/modules/sky/look.js +50 -0
- package/modules/sky/schedule.js +80 -0
- package/modules/sky/shader.js +96 -0
- package/modules/sky/shadow.js +80 -0
- package/modules/sky/skyModule.js +59 -0
- package/modules/terrain/ChunkManager.js +291 -0
- package/modules/terrain/chunkJob.js +19 -0
- package/modules/terrain/chunkQueries.js +42 -0
- package/modules/terrain/chunkWorker.js +5 -0
- package/modules/terrain/index.js +110 -0
- package/modules/terrain/mountTerrain.js +32 -0
- package/modules/terrain/terrainColorMap.js +41 -0
- package/modules/terrain/terrainDefaults.js +39 -0
- package/modules/terrain/terrainEdit.js +139 -0
- package/modules/terrain/terrainGen.js +155 -0
- package/modules/terrain/terrainIsland.js +131 -0
- package/modules/terrain/terrainMesh.js +92 -0
- package/modules/terrain/terrainShape.js +108 -0
- package/modules/terrain/terrainShapeMain.js +41 -0
- package/modules/terrain/terrainTexture.js +44 -0
- package/modules/timeline/Timeline.js +249 -0
- package/modules/timeline/Tween.js +118 -0
- package/modules/timeline/letterbox.js +24 -0
- package/modules/timeline/shots.js +88 -0
- package/modules/triggers/index.js +261 -0
- package/modules/vegetation/Vegetation.js +255 -0
- package/modules/vegetation/groundChunks.js +177 -0
- package/modules/vegetation/index.js +40 -0
- package/modules/vegetation/modelGeometry.js +21 -0
- package/modules/water/Water.js +144 -0
- package/modules/water/surface.js +88 -0
- package/modules/weather/Weather.js +251 -0
- package/modules/weather/particles.js +92 -0
- package/modules/weather/shaders.js +49 -0
- package/modules/world/World.js +231 -0
- package/modules/world/cells.js +42 -0
- package/modules/world/index.js +64 -0
- package/modules/world/persist.js +107 -0
- package/modules/world/pos.js +20 -0
- package/modules/world/records.js +89 -0
- package/modules/world/sim.js +175 -0
- package/modules/world/simFar.js +82 -0
- package/modules/world/state.js +115 -0
- package/modules/world/views.js +182 -0
- package/package.json +66 -4
- package/presets.js +96 -0
- package/render/PostFX.js +243 -0
- package/render/detail.js +105 -0
- package/render/effects.js +142 -0
- package/render/engineUniforms.js +23 -0
- package/render/fuse.js +72 -0
- package/render/lighting.js +100 -0
- package/render/lights.js +16 -0
- package/render/materials.js +203 -0
- package/render/patches.js +252 -0
- package/render/surface.js +67 -0
- package/render/uniformDecl.js +29 -0
- package/types/common.d.ts +34 -0
- package/types/core.d.ts +1773 -0
- package/types/gameplay.d.ts +1812 -0
- package/types/generated/core/Audio.d.ts +200 -0
- package/types/generated/core/Batching.d.ts +47 -0
- package/types/generated/core/Budget.d.ts +90 -0
- package/types/generated/core/Commits.d.ts +31 -0
- package/types/generated/core/Events.d.ts +46 -0
- package/types/generated/core/I18n.d.ts +30 -0
- package/types/generated/core/Input.d.ts +118 -0
- package/types/generated/core/Jobs.d.ts +41 -0
- package/types/generated/core/Ops.d.ts +162 -0
- package/types/generated/core/Random.d.ts +49 -0
- package/types/generated/core/Rays.d.ts +49 -0
- package/types/generated/core/Saves.d.ts +161 -0
- package/types/generated/core/Stream.d.ts +93 -0
- package/types/generated/core/Threads.d.ts +82 -0
- package/types/generated/core/audioVoice.d.ts +47 -0
- package/types/generated/core/page.d.ts +18 -0
- package/types/generated/core/session.d.ts +48 -0
- package/types/generated/modules/ambience/Ambience.d.ts +48 -0
- package/types/generated/modules/debug/DebugPanel.d.ts +24 -0
- package/types/generated/modules/flow/index.d.ts +10 -0
- package/types/generated/modules/grid/Grid.d.ts +151 -0
- package/types/generated/modules/grid/Layer.d.ts +64 -0
- package/types/generated/modules/grid/index.d.ts +86 -0
- package/types/generated/modules/hlod/index.d.ts +11 -0
- package/types/generated/modules/hud/index.d.ts +10 -0
- package/types/generated/modules/lights/index.d.ts +13 -0
- package/types/generated/modules/music/Music.d.ts +111 -0
- package/types/generated/modules/net/Net.d.ts +34 -0
- package/types/generated/modules/net/index.d.ts +53 -0
- package/types/generated/modules/net/transports.d.ts +58 -0
- package/types/generated/modules/particles/Particles.d.ts +69 -0
- package/types/generated/modules/sky/Sky.d.ts +152 -0
- package/types/generated/modules/sky/skyModule.d.ts +10 -0
- package/types/generated/modules/timeline/Timeline.d.ts +51 -0
- package/types/generated/modules/water/Water.d.ts +29 -0
- package/types/generated/modules/weather/Weather.d.ts +72 -0
- package/types/generated/modules/world/World.d.ts +242 -0
- package/types/generated/modules/world/index.d.ts +15 -0
- package/types/generated/render/PostFX.d.ts +128 -0
- package/types/generated/render/materials.d.ts +90 -0
- package/types/generated/render/patches.d.ts +41 -0
- package/types/generated/ui/Focus.d.ts +11 -0
- package/types/generated/ui/LoadingScreen.d.ts +2 -0
- package/types/generated/ui/UI.d.ts +57 -0
- package/types/nav.d.ts +52 -0
- package/types/physics.d.ts +584 -0
- package/types/style.d.ts +628 -0
- package/types/worker.d.ts +11 -0
- package/types/world.d.ts +772 -0
- package/ui/Focus.js +51 -0
- package/ui/LoadingScreen.js +42 -0
- package/ui/Parts.js +195 -0
- package/ui/UI.js +167 -0
- package/ui/defaults.js +85 -0
- package/ui/theme.js +124 -0
- package/ui/touchPart.js +49 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// Inventory ops and the 'inventory' UI part. Everything a player decides to do with items (move, use, drop) is an op, so the default window,
|
|
2
|
+
// a game's own window, dialogue, the dev bridge and tests all run the same can / run (recipes/23-ops.md).
|
|
3
|
+
// The window is a UI part (recipes/26-ui-parts.md): the engine owns opening / closing (key, Esc, input lock), the part only draws.
|
|
4
|
+
// engine.ui.set('inventory', false) no inventory window at all (no DOM is ever built)
|
|
5
|
+
// engine.ui.set('inventory', { drop: true }) default window with a Drop button per row
|
|
6
|
+
// engine.ui.set('inventory', (engine) => ({ show: d => …, update: d => …, hide: () => …, dispose: () => … })) your own
|
|
7
|
+
import { esc, iconHTML as icon, injectUICSS } from '../../ui/UI.js';
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
/** Texts of the inventory window, the pickup toast and the ops (zh-CN and en) @internal */
|
|
11
|
+
export const INVENTORY_STRINGS = {
|
|
12
|
+
'zh-CN': { 'inventory.title': '背包', 'inventory.action': '背包', 'inventory.empty': '背包是空的。', 'inventory.full': '背包满了',
|
|
13
|
+
'inventory.got': '获得 {icon} {name} ×{n}', 'inventory.pickup': '捡起 {name}', 'inventory.use': '使用', 'inventory.drop': '丢下' },
|
|
14
|
+
en: { 'inventory.title': 'Inventory', 'inventory.action': 'Inventory', 'inventory.empty': 'Your bag is empty.', 'inventory.full': 'Inventory full',
|
|
15
|
+
'inventory.got': 'Got {icon} {name} ×{n}', 'inventory.pickup': 'Pick up {name}', 'inventory.use': 'Use', 'inventory.drop': 'Drop' },
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/** Data the 'inventory' part receives in show / update @internal */
|
|
19
|
+
export function windowData(inv) {
|
|
20
|
+
const e = inv.engine, bag = inv.bag;
|
|
21
|
+
return { title: e.t('inventory.title'), hint: e.input.label(inv.action), items: bag.list(), capacity: bag.capacity, used: bag.slotsUsed, weight: bag.weight, maxWeight: bag.maxWeight };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Default 'inventory' part: the list window. Rows of items with a use function get a Use button; options.drop adds a Drop button. Buttons run ops.
|
|
26
|
+
* The style pack can replace the body with style.ui.templates.inventory(data, { esc, t, icon }).
|
|
27
|
+
* @internal
|
|
28
|
+
*/
|
|
29
|
+
export function inventoryWindow(e, o = {}) {
|
|
30
|
+
let el = null;
|
|
31
|
+
const tools = { esc, t: (k, p) => e.t(k, p), icon };
|
|
32
|
+
const row = it => {
|
|
33
|
+
const btn = (op, k) => `<button class="threedog-btn" type="button" data-op="${op}" data-key="${esc(it.key)}">${esc(e.t(k))}</button>`;
|
|
34
|
+
const acts = (typeof it.def.use === 'function' ? btn('inventory.use', 'inventory.use') : '') + (o.drop ? btn('inventory.drop', 'inventory.drop') : '');
|
|
35
|
+
return `<div class="threedog-row"><span class="threedog-icon">${icon(it.def)}</span>
|
|
36
|
+
<span class="threedog-row-main"><b>${esc(it.def.name)}</b><div class="threedog-row-sub">${esc(it.def.desc || '')}</div></span>
|
|
37
|
+
<span class="threedog-count">×${it.count}</span>${acts}</div>`;
|
|
38
|
+
};
|
|
39
|
+
const draw = d => {
|
|
40
|
+
const tpl = e.style.ui?.templates?.inventory;
|
|
41
|
+
const body = tpl ? tpl(d, tools) : d.items.length ? `<div class="threedog-list">${d.items.map(row).join('')}</div>` : `<p class="threedog-empty">${esc(e.t('inventory.empty'))}</p>`;
|
|
42
|
+
el.innerHTML = `<div class="threedog-win-head"><span>${esc(d.title)}</span><span><span class="threedog-win-hint">${esc(d.hint)}</span> `
|
|
43
|
+
+ `<button class="threedog-btn threedog-close" type="button">${esc(e.t('common.close'))}</button></span></div>${body}`;
|
|
44
|
+
};
|
|
45
|
+
return {
|
|
46
|
+
show(d) {
|
|
47
|
+
if (!el) { // built on first open: a game that never opens it (or turned it off) has no inventory DOM
|
|
48
|
+
injectUICSS();
|
|
49
|
+
el = document.createElement('div'); el.className = 'threedog-ui threedog-win threedog-win-inventory'; e.uiRoot.appendChild(el);
|
|
50
|
+
el.addEventListener('click', ev => {
|
|
51
|
+
const b = /** @type {HTMLElement} */ (ev.target).closest?.('button'); if (!b) return;
|
|
52
|
+
if (b.classList.contains('threedog-close')) e.ui.close('inventory');
|
|
53
|
+
else if (b.dataset.op) e.ops.run(b.dataset.op, { item: b.dataset.key });
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
el.hidden = false; draw(d);
|
|
57
|
+
},
|
|
58
|
+
update(d) { if (el && !el.hidden) draw(d); },
|
|
59
|
+
hide() { if (el) el.hidden = true; },
|
|
60
|
+
dispose() { el?.remove(); el = null; },
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Where an actor stands (engine.actorPosition: the player body, a vehicle, a world record) @internal */
|
|
65
|
+
const posOf = (e, actor) => actor === 'player' && !e.player ? null : e.actorPosition(actor);
|
|
66
|
+
|
|
67
|
+
/** Define inventory.transfer / inventory.use / inventory.drop @internal */
|
|
68
|
+
export function defineInventoryOps(e, inv) {
|
|
69
|
+
const box = id => inv._containers.map.get(id);
|
|
70
|
+
// moving items between containers by hand (an inventory / chest window, drag and drop) is a player decision
|
|
71
|
+
e.ops.define('inventory.transfer', {
|
|
72
|
+
desc: 'Move items between containers (player bag is player); item is an item id or an entry key (sword#3)', args: { from: 'string', to: 'string', item: 'string', n: 'int?' },
|
|
73
|
+
can(e, { from, to, item, n = 1 }) {
|
|
74
|
+
const a = box(from), b = box(to);
|
|
75
|
+
if (!a || !b) return 'noContainer';
|
|
76
|
+
if (!a.has(item, n)) return 'notEnough';
|
|
77
|
+
return from !== to && b._accepts(a, item, n) < n ? 'full' : true;
|
|
78
|
+
},
|
|
79
|
+
run: (e, { from, to, item, n = 1 }) => inv.transfer(from, to, item, n),
|
|
80
|
+
});
|
|
81
|
+
// Replace-when-full recipe: one op takes A and gives B, all or nothing. Undo with engine.ops.undo('inventory.trade', args, value).
|
|
82
|
+
// engine.ops.run('inventory.trade', { take: 'rusty_key', give: 'gold_key' }) // { ok: true, value } or { ok: false, reason: 'full' | 'notEnough' }
|
|
83
|
+
const at = (a, ctx) => box(a.container ?? ctx?.actor ?? 'player');
|
|
84
|
+
e.ops.define('inventory.trade', {
|
|
85
|
+
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",
|
|
86
|
+
args: { take: 'string', give: 'string', takeN: 'int?', giveN: 'int?', container: 'string?' },
|
|
87
|
+
can(e, a, ctx) {
|
|
88
|
+
const c = at(a, ctx);
|
|
89
|
+
return c ? c._canTrade(a.take, a.give, a.takeN ?? 1, a.giveN ?? 1) : 'noContainer';
|
|
90
|
+
},
|
|
91
|
+
run(e, a, ctx) { const { took, gave, giveId, giveN } = at(a, ctx).trade(a.take, a.give, { takeN: a.takeN, giveN: a.giveN }); return { took, gave, giveId, giveN }; },
|
|
92
|
+
undo(e, a, v, ctx) { // give B back, take A back (with its data); the value of the run is v
|
|
93
|
+
const c = at(a, ctx);
|
|
94
|
+
c.remove(v.gave, v.giveN);
|
|
95
|
+
for (const t of v.took) c.add(t.id, t.count, { ...(t.data && { data: t.data }), silent: true });
|
|
96
|
+
},
|
|
97
|
+
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' },
|
|
98
|
+
});
|
|
99
|
+
e.ops.define('inventory.use', {
|
|
100
|
+
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
|
+
can(e, { item }, ctx) {
|
|
102
|
+
const en = box(ctx.actor)?._of(item)[0];
|
|
103
|
+
if (!en) return 'notEnough';
|
|
104
|
+
const def = inv.item(en.id);
|
|
105
|
+
if (typeof def.use !== 'function') return 'notUsable';
|
|
106
|
+
return def.canUse ? def.canUse(e, { actor: ctx.actor, item: en.id, key: en.key, data: en.data }) : true;
|
|
107
|
+
},
|
|
108
|
+
run(e, { item }, ctx) {
|
|
109
|
+
const c = box(ctx.actor), en = c._of(item)[0], def = inv.item(en.id), info = { actor: ctx.actor, item: en.id, key: en.key, data: en.data };
|
|
110
|
+
if (def.consume) c.remove(en.key, 1);
|
|
111
|
+
def.use(e, info);
|
|
112
|
+
e.events.emit('item:use', info);
|
|
113
|
+
return en.key;
|
|
114
|
+
},
|
|
115
|
+
reasons: { notEnough: 'The actor does not have it', notUsable: 'The item has no use function' },
|
|
116
|
+
});
|
|
117
|
+
e.ops.define('inventory.drop', {
|
|
118
|
+
desc: "Drop items from the actor's container on the ground as a pickup (keeps their data)", args: { item: 'string', n: 'int?' },
|
|
119
|
+
can(e, { item, n = 1 }, ctx) {
|
|
120
|
+
if (!box(ctx.actor)?.has(item, n)) return 'notEnough';
|
|
121
|
+
if (!e.world) return 'noWorld';
|
|
122
|
+
return posOf(e, ctx.actor) ? true : 'noPosition';
|
|
123
|
+
},
|
|
124
|
+
run(e, { item, n = 1 }, ctx) {
|
|
125
|
+
const c = box(ctx.actor), p = posOf(e, ctx.actor);
|
|
126
|
+
let left = n;
|
|
127
|
+
for (const en of c._of(item)) {
|
|
128
|
+
const k = Math.min(left, en.count), { id, data } = en;
|
|
129
|
+
c.remove(en.key, k);
|
|
130
|
+
inv.spawnPickup(id, [p.x, p.y ?? e.surface(p.x, p.z), p.z], { count: k, ...(data && { data }) }); // y picks the floor the actor stands on (bridge deck, upper storey)
|
|
131
|
+
if (!(left -= k)) break;
|
|
132
|
+
}
|
|
133
|
+
return n;
|
|
134
|
+
},
|
|
135
|
+
reasons: { notEnough: 'The actor does not have that many', noWorld: 'Pickups live in world: install world()', noPosition: 'The actor has no position' },
|
|
136
|
+
});
|
|
137
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// Pickups on the ground: plain kind 'pickup' records in engine.world (saved with it); the model is built only near the engine.stream sources (unloading never deletes the record).
|
|
2
|
+
// Taken by the interact key (radius per pickup, default inventory pickupRadius) or, with auto: true, just by walking into it (bullets, coins, health packs).
|
|
3
|
+
import * as THREE from 'three';
|
|
4
|
+
import { makeMaterial } from '../../render/materials.js';
|
|
5
|
+
import { xzOf } from '../../core/math.js';
|
|
6
|
+
import { placeY } from '../../core/ground.js';
|
|
7
|
+
import { checkOptionsArg, warnOnce } from '../../core/validate.js';
|
|
8
|
+
import { copyData } from './Container.js';
|
|
9
|
+
|
|
10
|
+
/** inventory.spawnPickup implementation @internal */
|
|
11
|
+
export function spawnPickup(inv, item, position, opts = {}) {
|
|
12
|
+
const [x, z] = xzOf(position);
|
|
13
|
+
// 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
|
|
14
|
+
const p = /** @type {any} */ (position).isObject3D ? /** @type {any} */ (position).position : position, refY = Array.isArray(p) ? p[1] : p.y;
|
|
15
|
+
checkOptionsArg('inventory.spawnPickup()', opts, ['count', 'data', 'mesh', 'respawnSeconds', 'bob', 'id', 'initial', 'zone', 'radius', 'auto']);
|
|
16
|
+
if (typeof opts !== 'object' || !opts) opts = {};
|
|
17
|
+
inv._known(item);
|
|
18
|
+
const { count = 1, mesh = null, respawnSeconds = 0, bob = true, id, initial = false, zone, radius, auto = false } = opts;
|
|
19
|
+
if (radius != null && !(radius > 0)) throw new Error(`inventory.spawnPickup('${item}'): radius must be a positive number of meters, got ${radius}`);
|
|
20
|
+
const data = copyData('spawnPickup', item, opts.data);
|
|
21
|
+
const e = inv.engine, w = presentPickups(inv);
|
|
22
|
+
const def = { id, kind: 'pickup', item, count, bob, ...(data && { data }), ...(radius != null && { radius }), ...(auto && { auto: true }),
|
|
23
|
+
...(respawnSeconds > 0 && { respawn: respawnSeconds }), ...(zone != null && { zone }), position: [x, placeY(e, `inventory.spawnPickup('${item}')`, x, z, refY) + (e.style.pickup?.lift ?? 0.4), z] };
|
|
24
|
+
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)
|
|
25
|
+
if (rec && (rec.kind !== 'pickup' || rec.item !== item)) throw new Error(`pickup id '${id}' is already used by other data (${rec.kind}${rec.item ? ' ' + rec.item : ''}): use a different id`);
|
|
26
|
+
const wid = rec?.id ?? id;
|
|
27
|
+
if (mesh) inv._meshes.set(wid, mesh);
|
|
28
|
+
return { id: item, worldId: wid, get object() { return w.view(wid); }, remove: () => { w.remove(wid); inv._meshes.delete(wid); } };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Register how pickups look, respawn and get auto-taken (once, on first use; needs world installed first) @internal */
|
|
32
|
+
export function presentPickups(inv) {
|
|
33
|
+
const e = inv.engine, w = e.world;
|
|
34
|
+
if (!w) throw new Error('Pickup data lives in world: call engine.use(world()) first (the openWorld() preset already does) and order it before inventory().');
|
|
35
|
+
if (inv._presented) return w;
|
|
36
|
+
inv._presented = true;
|
|
37
|
+
w.present('pickup', {
|
|
38
|
+
radius: inv.bobDistance,
|
|
39
|
+
show: rec => rec.until == null, // waiting to respawn: no model
|
|
40
|
+
build: rec => model(inv, rec),
|
|
41
|
+
update: (rec, o) => { // bobbing spin: world places the model at the data position each frame first, this adds the bob on top
|
|
42
|
+
if (rec.bob === false) return;
|
|
43
|
+
const t = e.elapsed, x = rec.position.x;
|
|
44
|
+
o.rotation.y = t * 1.5 + x; o.position.y = rec.position.y + Math.sin(t * 2 + x) * 0.08;
|
|
45
|
+
},
|
|
46
|
+
dispose: (rec, o) => { o.userData.useHandle?.remove(); if (inv._meshes.has(rec.id)) e.detach(o); else e.remove(o); }, // a mesh passed in by the game is only detached
|
|
47
|
+
});
|
|
48
|
+
// 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
|
|
49
|
+
const wake = rec => { if (rec.until != null && e.clock.now >= rec.until) w.set(rec.id, { until: undefined }); };
|
|
50
|
+
// auto pickups in reach are queued here and taken in the inventory's fixed step (records are not removed while world is iterating them)
|
|
51
|
+
const reach = rec => {
|
|
52
|
+
const p = e.player || e.actor !== 'player' ? e.actorPosition() : null; // whoever the user controls (engine.actor); no player and no actor: nobody walks into it
|
|
53
|
+
if (!rec.auto || rec.until != null || !p) return;
|
|
54
|
+
const r = rec.radius ?? inv.pickupRadius, dx = rec.position.x - p.x, dy = rec.position.y - p.y, dz = rec.position.z - p.z;
|
|
55
|
+
if (dx * dx + dy * dy + dz * dz <= r * r) inv._autoQueue.push(rec);
|
|
56
|
+
};
|
|
57
|
+
w.simulate('pickup', { near: rec => { wake(rec); reach(rec); }, far: wake });
|
|
58
|
+
return w;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Take the queued auto pickups (fixed step) @internal */
|
|
62
|
+
export function takeAuto(inv) {
|
|
63
|
+
const q = inv._autoQueue; if (!q.length) return;
|
|
64
|
+
inv._autoQueue = [];
|
|
65
|
+
for (const rec of q) if (inv.engine.world.get(rec.id) === rec && rec.until == null) take(inv, rec);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Into the player inventory, then off the ground; { ok: false, reason: 'full' } (it stays on the ground) when it doesn't fit, { ok: true } otherwise.
|
|
69
|
+
* Pickup onUse returns this, and the interaction.use op passes it on as value, so callers can tell the player why @internal */
|
|
70
|
+
function take(inv, rec) {
|
|
71
|
+
const e = inv.engine, W = e.world;
|
|
72
|
+
if (!inv.add(rec.item, rec.count, rec.data && { data: rec.data })) return { ok: false, reason: 'full' };
|
|
73
|
+
// 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
|
|
74
|
+
if (rec.respawn > 0) W.set(rec.id, { until: e.clock.now + rec.respawn });
|
|
75
|
+
else { W.remove(rec.id); inv._meshes.delete(rec.id); }
|
|
76
|
+
e.events.emit('item:pickup', { id: rec.item, x: rec.position.x, z: rec.position.z });
|
|
77
|
+
return { ok: true };
|
|
78
|
+
}
|
|
79
|
+
const NO_PICKUP = 'pickups: no style pack / model given, so items on the ground are gray boxes (a placeholder). Pass a style pack with '
|
|
80
|
+
+ "style.pickup.model(item, { color, size }), e.g. createEngine({ style: lowpoly }) with lowpoly from '@threedog/style-lowpoly'";
|
|
81
|
+
/** The model (built when near) + interaction for this pickup record @internal */
|
|
82
|
+
function model(inv, rec) {
|
|
83
|
+
const e = inv.engine, def = inv.item(rec.item), st = e.style.pickup || {};
|
|
84
|
+
let obj = inv._meshes.get(rec.id);
|
|
85
|
+
if (!obj) {
|
|
86
|
+
const color = def.color ?? st.color ?? 0xcccccc, size = st.size ?? 0.25;
|
|
87
|
+
// shape is the style pack's; without one a neutral box
|
|
88
|
+
if (!st.model) warnOnce(NO_PICKUP);
|
|
89
|
+
const m = st.model ? st.model(def, { color, size }, e) : new THREE.BoxGeometry(size, size, size);
|
|
90
|
+
obj = m.isObject3D ? m : new THREE.Mesh(m, makeMaterial('pickup', { color }, e));
|
|
91
|
+
}
|
|
92
|
+
obj.castShadow = true;
|
|
93
|
+
if (rec.auto) return obj; // taken by walking into it: no key prompt
|
|
94
|
+
if (!e.interactions) { console.warn('Pickups require the interactions() module: install it first.'); return obj; }
|
|
95
|
+
obj.userData.useHandle = e.interactions.add(obj, {
|
|
96
|
+
id: rec.id,
|
|
97
|
+
label: () => e.t('inventory.pickup', { name: def.name }), radius: rec.radius ?? inv.pickupRadius,
|
|
98
|
+
enabled: () => e.world.get(rec.id) === rec && rec.until == null,
|
|
99
|
+
onUse: () => take(inv, rec), // { ok, reason? } goes back through interaction.use's value
|
|
100
|
+
});
|
|
101
|
+
return obj;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Pickup data of an item still on the ground nearest to from (default engine.actorPosition()); null if none @internal */
|
|
105
|
+
export function nearestPickup(inv, item, from) {
|
|
106
|
+
const w = inv.engine.world, f = from ?? inv.engine.actorPosition();
|
|
107
|
+
let best = null, bd = Infinity;
|
|
108
|
+
for (const r of w?.query({ kind: 'pickup', where: r => r.item === item && r.until == null }) || []) {
|
|
109
|
+
const dx = r.position.x - f.x, dy = r.position.y - (f.y ?? 0), dz = r.position.z - f.z, d = dx * dx + dy * dy + dz * dz;
|
|
110
|
+
if (d < bd) { bd = d; best = r; }
|
|
111
|
+
}
|
|
112
|
+
return best;
|
|
113
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// The quest log window as the UI part 'questLog'. Quests keeps the behavior through engine.ui (the J key toggles it; open = UI focus stack, input lock,
|
|
2
|
+
// Esc closes, ui:open / ui:close 'questLog'); the part only draws { title, key, quests, close() }. Replace it or turn it off with engine.ui.set('questLog', ...).
|
|
3
|
+
// Default look: a .threedog-win window; its body can be replaced by style.ui.templates.quests({ quests }, { esc, t }) or restyled via .threedog-quest*.
|
|
4
|
+
import { injectUICSS, esc } from '../../ui/UI.js';
|
|
5
|
+
/** @import { Engine, UIPart } from '../../types/core' */
|
|
6
|
+
/** @import { QuestLogView } from '../../types/gameplay' */
|
|
7
|
+
|
|
8
|
+
const NAME = 'questLog';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Default 'questLog' part: show(view) / update(view) draw the list, hide() hides it
|
|
12
|
+
* @param {Engine} engine
|
|
13
|
+
* @returns {UIPart}
|
|
14
|
+
*/
|
|
15
|
+
function questLogPart(engine) {
|
|
16
|
+
injectUICSS();
|
|
17
|
+
let w = null;
|
|
18
|
+
const tools = { esc, t: (k, p) => engine.t(k, p) };
|
|
19
|
+
const body = ({ quests }) => quests.length ? quests.map(q => `
|
|
20
|
+
<div class="threedog-quest${q.done ? ' is-done' : ''}"><div class="threedog-quest-title">${esc(q.title)}${q.done ? esc(engine.t('quests.doneTag')) : ''}</div>
|
|
21
|
+
${q.desc ? `<div class="threedog-row-sub">${esc(q.desc)}</div>` : ''}
|
|
22
|
+
<ol class="threedog-steps">${q.steps.map(s => `<li class="threedog-step${s.done ? ' is-done' : s.current ? ' is-current' : ''}">${esc(s.text)}</li>`).join('')}</ol></div>`).join('')
|
|
23
|
+
: `<p class="threedog-empty">${esc(engine.t('quests.empty'))}</p>`;
|
|
24
|
+
/** @param {QuestLogView} v */
|
|
25
|
+
const draw = v => {
|
|
26
|
+
if (!w) { w = document.createElement('div'); w.className = 'threedog-ui threedog-win threedog-win-quests'; engine.uiRoot.appendChild(w); }
|
|
27
|
+
w.hidden = false;
|
|
28
|
+
const tpl = engine.style.ui?.templates?.quests;
|
|
29
|
+
w.innerHTML = `<div class="threedog-win-head"><span>${esc(v.title)}</span><span><span class="threedog-win-hint">${esc(v.key)}</span> <button class="threedog-btn threedog-close" type="button">${esc(engine.t('common.close'))}</button></span></div>${tpl ? tpl(v, tools) : body(v)}`;
|
|
30
|
+
w.querySelector('.threedog-close').onclick = () => v.close();
|
|
31
|
+
};
|
|
32
|
+
return { show: draw, update: draw, hide() { if (w) w.hidden = true; }, dispose() { w?.remove(); w = null; } };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Declare the 'questLog' part and return the handle Quests uses: { open, toggle(on?), refresh() } (same shape toggleOnKey expects)
|
|
37
|
+
* @param {Engine} e
|
|
38
|
+
* @param {import('./Quests.js').Quests} q
|
|
39
|
+
* @returns {{ readonly open: boolean, toggle(on?: boolean): void, refresh(): void }}
|
|
40
|
+
* @internal
|
|
41
|
+
*/
|
|
42
|
+
export function questLogWindow(e, q) {
|
|
43
|
+
/** @returns {QuestLogView} */
|
|
44
|
+
const view = () => ({ title: e.t('quests.title'), key: e.input.label(q.action), quests: q.list(), close: () => e.ui.close(NAME) });
|
|
45
|
+
e.ui.define(NAME, questLogPart, { modal: { cancel: eng => eng.ui.close(NAME), data: view } });
|
|
46
|
+
const win = {
|
|
47
|
+
get open() { return e.ui.isOpen(NAME); },
|
|
48
|
+
toggle(on = !win.open) { if (on) e.ui.open(NAME); else e.ui.close(NAME); },
|
|
49
|
+
refresh() { if (win.open) e.ui.get(NAME).update(view()); },
|
|
50
|
+
};
|
|
51
|
+
e.events.on('i18n:change', win.refresh);
|
|
52
|
+
e.events.on('input:rebind', win.refresh);
|
|
53
|
+
return win;
|
|
54
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// Quest progress in saves (part 'quests', version 2): saved by stable step and part ids, never by position, so content may insert or reorder steps.
|
|
2
|
+
// { state: { questId: { step: stepId | null (when done), done, failed?, parts?: [ids of parts met in the current parallel step],
|
|
3
|
+
// got?: { stepId: [ids of parts met in a finished parallel step] } } } }
|
|
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
|
+
// ('0', '1', ...): give steps ids before shipping, then edits to the step list never move a player's progress.
|
|
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';
|
|
9
|
+
|
|
10
|
+
const STRINGS = {
|
|
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",
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/** Stable id of step i @param {any} def @param {number} i @returns {string} */
|
|
15
|
+
export const stepId = (def, i) => def.steps[i]?.id ?? String(i);
|
|
16
|
+
/** Stable id of part k of a parallel step @param {any} st @param {number} k @returns {string} */
|
|
17
|
+
const partId = (st, k) => st?.all?.[k]?.id ?? String(k);
|
|
18
|
+
/** [bool per part] -> ids of the parts that are true */
|
|
19
|
+
const metIds = (st, flags) => flags.flatMap((on, k) => (on ? [partId(st, k)] : []));
|
|
20
|
+
/** ids -> [bool per part] */
|
|
21
|
+
const metFlags = (st, ids) => st.all.map((_, k) => ids.includes(partId(st, k)));
|
|
22
|
+
const indexOf = (def, id) => def.steps.findIndex((_, i) => stepId(def, i) === id);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Runtime state of one quest ({ step: index, parts: [bool], got: { index: [bool] } }) -> its saved form.
|
|
26
|
+
* @param {any} def
|
|
27
|
+
* @param {any} s
|
|
28
|
+
* @returns {any}
|
|
29
|
+
*/
|
|
30
|
+
export function toSaved(def, s) {
|
|
31
|
+
const out = { step: s.done ? null : stepId(def, s.step), done: !!s.done };
|
|
32
|
+
if (s.failed) out.failed = true;
|
|
33
|
+
if (s.parts) out.parts = metIds(def.steps[s.step], s.parts);
|
|
34
|
+
if (s.got) out.got = Object.fromEntries(Object.entries(s.got).map(([i, flags]) => [stepId(def, +i), metIds(def.steps[+i], flags)]));
|
|
35
|
+
return out;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Saved form -> runtime state. A step id the definition no longer has restarts the quest from its first step (with a dev warning).
|
|
40
|
+
* @param {string} id quest id
|
|
41
|
+
* @param {any} def
|
|
42
|
+
* @param {any} q
|
|
43
|
+
* @returns {any}
|
|
44
|
+
*/
|
|
45
|
+
export function fromSaved(id, def, q) {
|
|
46
|
+
let step = q.done ? def.steps.length : indexOf(def, q.step);
|
|
47
|
+
if (step < 0) { warnOnce(coded('quest-step', STRINGS.missing.replace('{q}', id).replace('{step}', String(q.step)))); step = 0; }
|
|
48
|
+
const s = { step, done: !!q.done };
|
|
49
|
+
if (q.failed) s.failed = true;
|
|
50
|
+
const st = def.steps[step];
|
|
51
|
+
if (q.parts && st?.all) s.parts = metFlags(st, q.parts);
|
|
52
|
+
for (const [sid, ids] of Object.entries(q.got || {})) {
|
|
53
|
+
const i = indexOf(def, sid);
|
|
54
|
+
if (i >= 0 && def.steps[i].all) (s.got ??= {})[i] = metFlags(def.steps[i], ids);
|
|
55
|
+
}
|
|
56
|
+
return s;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Version 1 (positions) -> version 2 (ids), through the definitions known now (a quest not defined yet keeps positions as text ids).
|
|
61
|
+
* @param {Map<string, any>} defs
|
|
62
|
+
* @param {any} d
|
|
63
|
+
* @returns {{ state: Record<string, any> }}
|
|
64
|
+
*/
|
|
65
|
+
export function migrateV1(defs, d) {
|
|
66
|
+
const state = {};
|
|
67
|
+
for (const [id, s] of Object.entries(d?.state || {})) {
|
|
68
|
+
state[id] = toSaved(defs.get(id) ?? { steps: [] }, { ...s, step: s.step ?? 0 }); // version 1 saved the runtime form itself
|
|
69
|
+
}
|
|
70
|
+
return { state };
|
|
71
|
+
}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
// One placement grid (engine.grids.add returns it): world <-> cell math, footprints, occupancy, per-cell layers, can().
|
|
2
|
+
// Occupancy is not stored: it is an index derived from world records that carry a `grid` field
|
|
3
|
+
// ({ id, cell, size, rotation, item }), so a save holds each placed thing once (in world) and can never disagree with it.
|
|
4
|
+
// Cells are integer pairs [i, j]; cell [0, 0] spans origin.x..origin.x+cell on x and origin.z..origin.z+cell on z.
|
|
5
|
+
// A footprint is anchored at its min corner cell; rotation is in quarter turns (0-3) and odd turns swap width and depth.
|
|
6
|
+
import { Layer } from './Layer.js';
|
|
7
|
+
import { xzOf } from '../../core/math.js';
|
|
8
|
+
|
|
9
|
+
const key = (i, j) => i + ',' + j;
|
|
10
|
+
const cap = s => s[0].toUpperCase() + s.slice(1);
|
|
11
|
+
// [x, z] here (a grid is flat), [x, y, z] / Vector3 / Object3D / { x, z } as everywhere else
|
|
12
|
+
const planar = v => (Array.isArray(v) && v.length === 2 ? v : xzOf(v));
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A placement grid. Several can exist at once (a yard outdoors, a room indoors); each has its own cell size, origin and bounds.
|
|
16
|
+
* @example
|
|
17
|
+
* const room = engine.grids.add('room', { cell: 1, origin: [0, 0], size: [16, 12] });
|
|
18
|
+
* room.toCell(engine.player.position); // [3, 5]
|
|
19
|
+
* room.can('table', [3, 5]); // true or a reason: 'occupied' 'outOfBounds' 'notFlat' 'wrongGround' or a layer reason ...
|
|
20
|
+
* engine.ops.run('grid.place', { grid: 'room', item: 'table', cell: [3, 5] });
|
|
21
|
+
* @experimental
|
|
22
|
+
*/
|
|
23
|
+
export class Grid {
|
|
24
|
+
/** @internal */
|
|
25
|
+
constructor(engine, owner, name, opts) {
|
|
26
|
+
/** @internal */ this.engine = engine;
|
|
27
|
+
/** @internal */ this._owner = owner;
|
|
28
|
+
/** Grid name (the `grid` arg of the grid.* ops) @type {string} */
|
|
29
|
+
this.name = name;
|
|
30
|
+
/** Cell edge length (meters) @type {number} */
|
|
31
|
+
this.cell = opts.cell ?? 1;
|
|
32
|
+
const [ox, oz] = planar(opts.origin ?? [0, 0]);
|
|
33
|
+
/** World position of the min corner of cell [0, 0] @type {{ x: number, z: number }} */
|
|
34
|
+
this.origin = { x: ox, z: oz };
|
|
35
|
+
/** Cell count [width, depth]; null = unbounded @type {[number, number] | null} */
|
|
36
|
+
this.size = opts.size ?? null;
|
|
37
|
+
/** Level name this grid belongs to (null = everywhere); its tiles show only while engine.world.zone matches @type {string | null} */
|
|
38
|
+
this.zone = opts.zone ?? null;
|
|
39
|
+
/** Max height difference (m) allowed under a footprint before can() says 'notFlat' @type {number} */
|
|
40
|
+
this.flat = opts.flat ?? 0.5;
|
|
41
|
+
/** @internal 'i,j' -> world record id */ this._occ = new Map();
|
|
42
|
+
/** @internal name -> Layer */ this._layers = new Map();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* World position (Vector3 / object / [x, z] / [x, y, z] / (x, z)) -> cell [i, j]
|
|
47
|
+
* @example farm.toCell(engine.player.position)
|
|
48
|
+
* @param {any} x
|
|
49
|
+
* @param {number} [z]
|
|
50
|
+
* @returns {[number, number]}
|
|
51
|
+
*/
|
|
52
|
+
toCell(x, z) {
|
|
53
|
+
if (typeof x === 'object') [x, z] = planar(x);
|
|
54
|
+
return [Math.floor((x - this.origin.x) / this.cell), Math.floor((z - this.origin.z) / this.cell)];
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Center of a footprint in world space, y on the ground (engine.surface). Without size it is the center of one cell
|
|
58
|
+
* @example obj.position.copy(farm.toWorld([3, 5]))
|
|
59
|
+
* @param {[number, number]} cell
|
|
60
|
+
* @param {[number, number]} [size]
|
|
61
|
+
* @param {number} [rotation] quarter turns
|
|
62
|
+
* @returns {{ x: number, y: number, z: number }}
|
|
63
|
+
*/
|
|
64
|
+
toWorld(cell, size = [1, 1], rotation = 0) {
|
|
65
|
+
const [w, d] = rotation & 1 ? [size[1], size[0]] : size;
|
|
66
|
+
const x = this.origin.x + (cell[0] + w / 2) * this.cell, z = this.origin.z + (cell[1] + d / 2) * this.cell;
|
|
67
|
+
return { x, y: this.engine.surface(x, z), z };
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Snap a world position to the center of the cell it is in
|
|
71
|
+
* @example mesh.position.copy(farm.snap(hit.point))
|
|
72
|
+
* @param {any} pos
|
|
73
|
+
* @returns {{ x: number, y: number, z: number }}
|
|
74
|
+
*/
|
|
75
|
+
snap(pos) { return this.toWorld(this.toCell(pos)); }
|
|
76
|
+
/**
|
|
77
|
+
* Is the cell inside the grid bounds (always true for an unbounded grid)
|
|
78
|
+
* @param {[number, number]} cell
|
|
79
|
+
* @returns {boolean}
|
|
80
|
+
*/
|
|
81
|
+
inside([i, j]) { return !this.size || (i >= 0 && j >= 0 && i < this.size[0] && j < this.size[1]); }
|
|
82
|
+
/**
|
|
83
|
+
* Cells covered by a footprint anchored at cell (min corner)
|
|
84
|
+
* @example farm.footprint([2, 2], [2, 1], 1) // [[2,2],[2,3]]: a 2x1 bed turned a quarter covers two cells along z
|
|
85
|
+
* @param {[number, number]} cell
|
|
86
|
+
* @param {[number, number]} [size]
|
|
87
|
+
* @param {number} [rotation] quarter turns
|
|
88
|
+
* @returns {[number, number][]}
|
|
89
|
+
*/
|
|
90
|
+
footprint(cell, size = [1, 1], rotation = 0) {
|
|
91
|
+
const [w, d] = rotation & 1 ? [size[1], size[0]] : size, out = [];
|
|
92
|
+
for (let a = 0; a < w; a++) for (let b = 0; b < d; b++) out.push([cell[0] + a, cell[1] + b]);
|
|
93
|
+
return out;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Id of the world record occupying a cell, or null
|
|
97
|
+
* @example engine.world.get(farm.at([3, 5]))
|
|
98
|
+
* @param {[number, number]} cell
|
|
99
|
+
* @returns {string | null}
|
|
100
|
+
*/
|
|
101
|
+
at([i, j]) { return this._occ.get(key(i, j)) ?? null; }
|
|
102
|
+
/**
|
|
103
|
+
* Ids of the records occupying any cell of an area (each once)
|
|
104
|
+
* @example farm.area([0, 0], [4, 4])
|
|
105
|
+
* @param {[number, number]} cell min corner
|
|
106
|
+
* @param {[number, number]} [size]
|
|
107
|
+
* @returns {string[]}
|
|
108
|
+
*/
|
|
109
|
+
area(cell, size = [1, 1]) {
|
|
110
|
+
const ids = new Set();
|
|
111
|
+
for (const c of this.footprint(cell, size)) { const id = this.at(c); if (id) ids.add(id); }
|
|
112
|
+
return [...ids];
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Can this go here? Returns true or a reason: outOfBounds, occupied, wrongGround, not<Field> (a layer rule such as notTilled), notFlat, or the item's own can reason.
|
|
116
|
+
* what is an item name registered with engine.grids.define, a size [w, d], or an inline rule { size, ground, layer, flat, can }.
|
|
117
|
+
* @example farm.can('crop', [3, 5]) // 'notTilled' until the soil layer says tilled
|
|
118
|
+
* @example farm.can([2, 1], [0, 0], { rotation: 1 }) // only bounds, occupancy and flatness
|
|
119
|
+
* @param {string | [number, number] | GridRule} what
|
|
120
|
+
* @param {[number, number]} cell
|
|
121
|
+
* @param {{ rotation?: number, ignore?: string }} [opts] ignore: a record id to skip (moving a thing over its own cells)
|
|
122
|
+
* @returns {true | string}
|
|
123
|
+
*/
|
|
124
|
+
can(what, cell, opts) { return this.check(what, cell, opts).reason ?? true; }
|
|
125
|
+
/**
|
|
126
|
+
* can() with the details behind the answer (used by engine.explain): { reason, cell, by } where by is the occupying record id
|
|
127
|
+
* @param {string | [number, number] | GridRule} what
|
|
128
|
+
* @param {[number, number]} cell
|
|
129
|
+
* @param {{ rotation?: number, ignore?: string }} [opts]
|
|
130
|
+
* @returns {{ reason: string | null, cell?: [number, number], by?: string, text?: string }}
|
|
131
|
+
*/
|
|
132
|
+
check(what, cell, { rotation = 0, ignore } = {}) {
|
|
133
|
+
const rule = this._owner._rule(what), e = this.engine, cells = this.footprint(cell, rule.size, rotation);
|
|
134
|
+
for (const c of cells) if (!this.inside(c)) return { reason: 'outOfBounds', cell: c };
|
|
135
|
+
for (const c of cells) { const by = this.at(c); if (by && by !== ignore) return { reason: 'occupied', cell: c, by }; }
|
|
136
|
+
const ground = rule.ground == null ? null : [].concat(rule.ground);
|
|
137
|
+
for (const c of cells) {
|
|
138
|
+
const p = this.toWorld(c);
|
|
139
|
+
if (ground && !ground.includes(e.groundType(p.x, p.z))) return { reason: 'wrongGround', cell: c, text: `ground is ${e.groundType(p.x, p.z)}, needs ${ground.join(' / ')}` };
|
|
140
|
+
for (const [layer, field] of Object.entries(rule.layer ?? {})) {
|
|
141
|
+
if (!this.layer(layer).get(c)?.[field]) return { reason: 'not' + cap(field), cell: c, text: `layer ${layer} has no ${field} here` };
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
const flat = rule.flat ?? this.flat;
|
|
145
|
+
if (flat < Infinity) {
|
|
146
|
+
let lo = Infinity, hi = -Infinity;
|
|
147
|
+
const [w, d] = rotation & 1 ? [rule.size[1], rule.size[0]] : rule.size;
|
|
148
|
+
for (let a = 0; a <= w; a++) for (let b = 0; b <= d; b++) { // corners of every cell: a step inside the footprint counts too
|
|
149
|
+
const h = e.surface(this.origin.x + (cell[0] + a) * this.cell, this.origin.z + (cell[1] + b) * this.cell);
|
|
150
|
+
lo = Math.min(lo, h); hi = Math.max(hi, h);
|
|
151
|
+
}
|
|
152
|
+
if (hi - lo > flat) return { reason: 'notFlat', text: `height varies ${(hi - lo).toFixed(2)} m, allowed ${flat}` };
|
|
153
|
+
}
|
|
154
|
+
const own = rule.can?.(e, { grid: this, cell, rotation, cells });
|
|
155
|
+
if (own != null && own !== true) return { reason: String(own) };
|
|
156
|
+
return { reason: null };
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* A named per-cell data layer on this grid (created on first use): soil state, wetness, ownership, anything plain.
|
|
160
|
+
* Writes go through ops like other saved gameplay state; the data is saved with the game (only non-empty cells).
|
|
161
|
+
* @example const soil = farm.layer('soil'); // in an op: soil.set([3, 5], { tilled: true })
|
|
162
|
+
* @example farm.layer('soil', { tiles: false }) // no tile visuals for this layer
|
|
163
|
+
* @param {string} name
|
|
164
|
+
* @param {import('./Layer.js').LayerOptions} [opts] only read on creation
|
|
165
|
+
* @returns {Layer}
|
|
166
|
+
*/
|
|
167
|
+
layer(name, opts) {
|
|
168
|
+
let l = this._layers.get(name);
|
|
169
|
+
if (!l) { l = new Layer(this, name, opts); this._layers.set(name, l); this._owner._layerAdded(l); }
|
|
170
|
+
else if (opts && l._byContent) { l.opts = opts; this._owner._retile(l); } // made early for content/grids.js cells: the game's first opts still count
|
|
171
|
+
if (opts) l._byContent = false;
|
|
172
|
+
return l;
|
|
173
|
+
}
|
|
174
|
+
/** Layers created so far @returns {Layer[]} */
|
|
175
|
+
get layers() { return [...this._layers.values()]; }
|
|
176
|
+
/** Number of occupied cells @returns {number} */
|
|
177
|
+
get occupied() { return this._occ.size; }
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Placement rule: what engine.grids.define registers, or an inline rule for can()
|
|
182
|
+
* @typedef {{ size?: [number, number], kind?: string, ground?: string | string[], layer?: Record<string, string>, flat?: number,
|
|
183
|
+
* fields?: Record<string, any>, can?: (e: any, info: { grid: Grid, cell: [number, number], rotation: number, cells: [number, number][] }) => true | string }} GridRule
|
|
184
|
+
*/
|