threedog 0.8.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 +219 -4
- package/README.md +6 -6
- package/core/assets/Animator.js +2 -0
- package/core/assets/Assets.js +44 -13
- package/core/assets/assetEntry.js +87 -22
- package/core/assets/assetHot.js +2 -1
- package/core/assets/assetReport.js +1 -2
- package/core/audio/Audio.js +24 -5
- package/core/base/I18n.js +6 -3
- package/core/base/Random.js +3 -1
- package/core/base/Vars.js +14 -4
- package/core/base/errorCodes.js +54 -16
- package/core/base/errorHints.js +12 -1
- package/core/base/errors.js +6 -4
- package/core/base/events.generated.js +10 -0
- package/core/base/follows.js +26 -0
- package/core/base/math.js +15 -8
- package/core/base/params.js +2 -2
- package/core/{saves → base}/saveValues.js +24 -6
- package/core/base/sections.js +97 -0
- package/core/base/spelling.js +32 -0
- package/core/base/validate.js +8 -29
- package/core/engine/Engine.js +61 -18
- package/core/engine/Loading.js +30 -3
- package/core/engine/copies.js +1 -1
- package/core/engine/headlessLoad.js +30 -13
- package/core/engine/lifecycle.js +30 -25
- package/core/engine/loop.js +22 -4
- package/core/engine/moduleSlots.js +91 -15
- package/core/engine/moduleTypes.js +13 -0
- package/core/engine/modules.js +248 -62
- package/core/engine/page.js +25 -8
- package/core/engine/plugins.js +15 -3
- package/core/engine/services.js +3 -0
- package/core/engine/startup.js +46 -36
- package/core/input/Input.js +39 -19
- package/core/input/inputKeyboard.js +39 -0
- package/core/input/inputPad.js +23 -2
- package/core/input/inputPointer.js +3 -1
- package/core/input/inputSeat.js +66 -0
- package/core/input/inputTouch.js +23 -6
- package/core/inspect/DebugOverrides.js +13 -2
- package/core/inspect/Describe.js +10 -6
- package/core/inspect/Inspect.js +31 -4
- package/core/inspect/UrlState.js +14 -10
- package/core/inspect/catalog.js +12 -5
- package/core/inspect/describeRender.js +3 -2
- package/core/inspect/describeText.js +6 -2
- package/core/inspect/metadata.generated.js +4140 -0
- package/core/inspect/pictureWriters.js +117 -33
- package/core/inspect/toolDoor.js +21 -0
- package/core/inspect/valueHistory.js +35 -0
- package/core/ops/Ops.js +32 -7
- package/core/ops/control.js +48 -23
- package/core/ops/holds.js +4 -3
- package/core/ops/opsAtomic.js +18 -6
- package/core/ops/opsCheck.js +7 -5
- package/core/ops/opsFail.js +5 -7
- package/core/ops/opsRun.js +2 -2
- package/core/ops/owned.js +20 -3
- package/core/ops/sharedPoint.js +7 -10
- package/core/ops/stacks.js +22 -1
- package/core/saves/SaveStore.js +52 -26
- package/core/saves/Saves.js +122 -27
- package/core/saves/saveApply.js +159 -61
- package/core/saves/saveFiles.js +30 -25
- package/core/saves/saveVersions.js +20 -6
- package/core/saves/session.js +76 -33
- package/core/saves/stateReplace.js +184 -0
- package/core/scene/Rays.js +21 -5
- package/core/scene/Views.js +6 -6
- package/core/scene/adaptive.js +13 -10
- package/core/scene/canvasBox.js +2 -0
- package/core/scene/compositeGround.js +72 -0
- package/core/scene/floor.js +17 -0
- package/core/scene/grid.js +2 -0
- package/core/scene/ground.contract.js +14 -0
- package/core/scene/ground.js +52 -17
- package/core/scene/pixel.js +154 -0
- package/core/scene/pixelRatio.js +167 -0
- package/core/scene/sceneDelta.js +3 -3
- package/core/scene/sceneMethods.js +11 -6
- package/core/scene/stepPoses.js +6 -1
- package/core/scene/thingExplain.js +1 -1
- package/core/scene/thingOps.js +22 -14
- package/core/scene/things.js +1 -1
- package/core/scene/tier.js +10 -0
- package/core/scene/view.js +126 -9
- package/core/stream/Batching.js +0 -1
- package/core/stream/Stream.js +24 -12
- package/core/stream/batchGroups.js +1 -0
- package/core/stream/scatter.js +5 -2
- package/core/stream/streamSources.js +2 -1
- package/core/threads/Commits.js +12 -7
- package/core/threads/Jobs.js +15 -5
- package/core/threads/Threads.js +5 -2
- package/core/time/Clock.js +3 -1
- package/core/time/skip.js +10 -4
- package/core/time/time.js +16 -10
- package/docs/AI.md +89 -72
- package/docs/API.md +18 -14
- package/docs/CODEMAP.md +68 -40
- package/docs/COMMANDS.md +22 -23
- package/docs/CONVENTIONS.md +120 -17
- package/docs/MIGRATING-0.8.md +2 -0
- package/docs/MIGRATING-1.0.md +101 -0
- package/docs/THREE.md +65 -12
- package/docs/api/cameraRig.md +3 -0
- package/docs/api/characters.md +13 -7
- package/docs/api/collider3d.md +9 -0
- package/docs/api/content.md +16 -15
- package/docs/api/dialogue.md +2 -1
- package/docs/api/engine.md +18 -16
- package/docs/api/events.md +14 -4
- package/docs/api/ground.md +2 -2
- package/docs/api/i18n.md +1 -1
- package/docs/api/input.md +2 -1
- package/docs/api/interactions.md +1 -1
- package/docs/api/inventory.md +1 -1
- package/docs/api/level.md +3 -2
- package/docs/api/nav.md +4 -2
- package/docs/api/particles.md +8 -5
- package/docs/api/pickups.md +1 -1
- package/docs/api/post.md +3 -4
- package/docs/api/quests.md +4 -2
- package/docs/api/rays.md +2 -0
- package/docs/api/rigid.md +3 -2
- package/docs/api/saves.md +8 -7
- package/docs/api/stream.md +4 -5
- package/docs/api/terrain.md +3 -4
- package/docs/api/terrainDraw.md +1 -1
- package/docs/api/timeline.md +2 -2
- package/docs/api/tools.md +3 -1
- package/docs/api/triggers.md +2 -0
- package/docs/api/vegetationDraw.md +1 -1
- package/docs/api/water.md +6 -2
- package/docs/api/weather.md +19 -4
- package/docs/api/weatherDraw.md +1 -1
- package/docs/api/world.md +1 -1
- package/docs/recipes/01-new-game.md +3 -3
- package/docs/recipes/02-npc.md +3 -4
- package/docs/recipes/03-items-pickups.md +11 -11
- package/docs/recipes/04-quests.md +2 -0
- package/docs/recipes/05-props-bake.md +9 -9
- package/docs/recipes/06-clock-events.md +6 -6
- package/docs/recipes/07-flow-scenes-pause.md +18 -15
- package/docs/recipes/08-weather-sky.md +5 -5
- package/docs/recipes/09-materials-lights-models.md +3 -3
- package/docs/recipes/10-blender-level.md +16 -4
- package/docs/recipes/11-vehicles.md +18 -6
- package/docs/recipes/12-navmesh.md +52 -8
- package/docs/recipes/13-animation.md +34 -8
- package/docs/recipes/14-custom-module.md +20 -8
- package/docs/recipes/15-perf.md +4 -4
- package/docs/recipes/16-dev-bridge.md +9 -8
- package/docs/recipes/18-world.md +13 -5
- package/docs/recipes/19-shooting-picking.md +3 -2
- package/docs/recipes/20-content-actions-save.md +26 -6
- package/docs/recipes/21-camera.md +17 -23
- package/docs/recipes/22-shop-gold.md +21 -12
- package/docs/recipes/23-ops.md +19 -15
- package/docs/recipes/26-ui-parts.md +3 -2
- package/docs/recipes/27-net.md +3 -3
- package/docs/recipes/28-terrain.md +3 -3
- package/docs/recipes/29-grid.md +1 -1
- package/docs/recipes/30-templates.md +10 -13
- package/docs/recipes/31-audio.md +2 -2
- package/docs/recipes/32-action-game.md +6 -5
- package/docs/recipes/33-stream-sources.md +4 -1
- package/docs/recipes/34-environment-lighting.md +7 -8
- package/docs/recipes/35-picture-from-reference.md +13 -9
- package/docs/recipes/36-ground-plants.md +3 -2
- package/docs/recipes/40-scene.md +11 -10
- package/docs/recipes/43-shader-patches-surfaces.md +7 -7
- package/docs/recipes/44-water-weather-hooks.md +9 -8
- package/docs/recipes/45-cascaded-shadows.md +3 -4
- package/docs/recipes/46-blender-art-route.md +3 -3
- package/docs/recipes/49-game-module-contract.md +2 -1
- package/docs/recipes/50-own-renderer.md +1 -0
- package/docs/recipes/51-capability-catalog.md +14 -8
- package/docs/recipes/52-around-the-picture.md +4 -4
- package/docs/recipes/55-mods-time-wrap.md +5 -4
- package/docs/recipes/56-webgpu-tsl.md +12 -1
- package/docs/recipes/57-control.md +30 -10
- package/docs/recipes/58-input.md +13 -0
- package/docs/recipes/60-judge-the-picture.md +1 -1
- package/docs/recipes/62-picture-code.md +106 -48
- package/docs/recipes/63-third-person-start.md +2 -1
- package/docs/recipes/64-particles.md +53 -4
- package/docs/recipes/65-swarm.md +2 -2
- package/docs/recipes/66-common-tasks.md +313 -0
- package/examples/action/.threedog/content-index.json +1 -1
- package/examples/action/GAMEMAP.md +3 -3
- package/examples/perf-island/.threedog/content-index.json +1 -1
- package/examples/perf-island/GAMEMAP.md +3 -3
- package/examples/perf-island/main.js +2 -2
- package/examples/plain-picture/GAMEMAP.md +3 -1
- package/examples/rc2d/GAMEMAP.md +1 -1
- package/examples/rc2d/rc2d.js +1 -1
- package/examples/styletest/.threedog/content-index.json +1 -1
- package/examples/styletest/GAMEMAP.md +3 -3
- package/examples/webgpu-tsl/GAMEMAP.md +1 -1
- package/examples/world/.threedog/content-index.json +1 -1
- package/examples/world/GAMEMAP.md +5 -4
- package/examples/world/main.js +3 -3
- package/index.d.ts +8 -5
- package/index.js +22 -1
- package/llms.txt +2 -1
- package/modules/ambience/Ambience.js +5 -3
- package/modules/calendar/index.js +7 -5
- package/modules/camera/FirstPersonCamera.js +3 -3
- package/modules/camera/FixedCamera.js +34 -20
- package/modules/camera/FollowCamera.js +20 -11
- package/modules/camera/RtsCamera.js +1 -0
- package/modules/camera/common.js +33 -22
- package/modules/camera/pixelSnap.js +62 -0
- package/modules/characters/Character.js +136 -43
- package/modules/characters/NPC.js +60 -28
- package/modules/characters/assetModel.js +2 -2
- package/modules/characters/astar.js +47 -28
- package/modules/characters/character.contract.js +21 -0
- package/modules/characters/contract.js +17 -3
- package/modules/characters/index.js +94 -34
- package/modules/characters/moveHelpers.js +80 -15
- package/modules/characters/mover.contract.js +14 -0
- package/modules/characters/movers/common.js +6 -4
- package/modules/characters/movers/rapier.js +12 -5
- package/modules/characters/movers/simple.js +25 -12
- package/modules/characters/movers/support.js +18 -7
- package/modules/characters/npcAvoid.js +15 -12
- package/modules/characters/npcSim.js +75 -25
- package/modules/characters/npcView.js +13 -6
- package/modules/characters/pathAsync.js +74 -24
- package/modules/characters/pathJob.js +38 -21
- package/modules/characters/player.js +59 -47
- package/modules/characters/seat.js +56 -0
- package/modules/characters/vehicle.contract.js +8 -5
- package/modules/characters/walkTo.js +31 -18
- package/modules/collision/Collision.js +44 -26
- package/modules/collision/contract.js +2 -2
- package/modules/collision/grid.js +6 -1
- package/modules/content/explain.js +1 -1
- package/modules/content/fields.js +3 -3
- package/modules/content/ids.js +3 -1
- package/modules/content/index.js +83 -27
- package/modules/content/loader.js +110 -130
- package/modules/content/markers.js +1 -1
- package/modules/content/models.js +48 -14
- package/modules/content/packs.js +2 -1
- package/modules/content/places.js +5 -4
- package/modules/content/props.js +8 -6
- package/modules/content/rules.js +29 -47
- package/modules/content/templates.js +66 -35
- package/modules/content/words.js +19 -6
- package/modules/debug/DebugPanel.js +4 -2
- package/modules/flow/contract.js +1 -0
- package/modules/flow/index.js +24 -14
- package/modules/gameplay/Container.js +5 -2
- package/modules/gameplay/Containers.js +9 -1
- package/modules/gameplay/Dialogue.js +82 -25
- package/modules/gameplay/Interactions.js +61 -32
- package/modules/gameplay/Inventory.js +56 -13
- package/modules/gameplay/Quests.js +133 -24
- package/modules/gameplay/boxOps.js +7 -0
- package/modules/gameplay/dialogueBubbles.js +14 -9
- package/modules/gameplay/inventoryOps.js +11 -7
- package/modules/gameplay/pickups.js +43 -16
- package/modules/gameplay/questLog.js +4 -2
- package/modules/gameplay/questSave.js +6 -1
- package/modules/gameplay/quests.contract.js +1 -1
- package/modules/grid/Grid.js +1 -1
- package/modules/grid/Layer.js +8 -4
- package/modules/grid/index.js +21 -15
- package/modules/grid/ops.js +43 -46
- package/modules/grid/tiles.js +1 -1
- package/modules/hlod/index.js +6 -3
- package/modules/hud/index.js +6 -4
- package/modules/hud/parts.js +4 -4
- package/modules/level/contract.js +2 -2
- package/modules/level/heightfield.js +17 -21
- package/modules/level/index.js +83 -26
- package/modules/level/load.js +165 -48
- package/modules/level/nodes.js +34 -16
- package/modules/lights/index.js +6 -3
- package/modules/lights/sources.js +3 -1
- package/modules/music/Music.js +4 -2
- package/modules/nav/contract.js +7 -5
- package/modules/nav/index.js +174 -44
- package/modules/nav/navPath.js +45 -10
- package/modules/net/index.js +1 -1
- package/modules/particles/Particles.js +85 -22
- package/modules/particles/effect.js +80 -28
- package/modules/particles/shaders.js +17 -0
- package/modules/photo/Photo.js +12 -10
- package/modules/rigid/RigidBodies.js +200 -57
- package/modules/rigid/Vehicles.js +15 -5
- package/modules/rigid/collider3d.contract.js +26 -0
- package/modules/rigid/ragdoll.js +55 -44
- package/modules/rigid/ragdollBones.js +83 -33
- package/modules/rigid/refresh.js +10 -0
- package/modules/rigid/sync.js +46 -15
- package/modules/settings/Settings.js +84 -37
- package/modules/settings/apply.js +15 -8
- package/modules/settings/sections.js +7 -4
- package/modules/settings/strings.js +2 -2
- package/modules/sky/skyModule.js +3 -1
- package/modules/swarm/Swarm.js +16 -6
- package/modules/terrain/ChunkManager.js +10 -16
- package/modules/terrain/index.js +17 -11
- package/modules/terrain/terrainEdit.js +13 -2
- package/modules/terrain/terrainGen.js +5 -3
- package/modules/terrain/terrainShape.js +1 -1
- package/modules/timeline/Timeline.js +58 -26
- package/modules/timeline/Tween.js +19 -13
- package/modules/timeline/shots.js +19 -10
- package/modules/triggers/index.js +87 -20
- package/modules/vegetation/Vegetation.js +4 -3
- package/modules/vegetation/draw.js +16 -0
- package/modules/vegetation/groundChunks.js +10 -7
- package/modules/vegetation/index.js +30 -7
- package/modules/water/Water.js +6 -2
- package/modules/weather/Weather.js +16 -15
- package/modules/weather/weatherDraw.js +7 -6
- package/modules/world/World.js +20 -7
- package/modules/world/explain.js +1 -1
- package/modules/world/index.js +13 -6
- package/modules/world/persist.js +23 -8
- package/modules/world/pos.js +4 -4
- package/modules/world/records.js +13 -7
- package/modules/world/sim.js +74 -23
- package/modules/world/simFar.js +2 -2
- package/modules/world/state.js +31 -6
- package/modules/world/thingSource.js +16 -3
- package/modules/world/views.js +19 -8
- package/package.json +7 -1
- package/render/PostFX.js +47 -29
- package/render/backend.js +5 -1
- package/render/fx/particles.tsl.js +8 -6
- package/render/fx/postWebGPU.tsl.js +3 -3
- package/render/layers.js +1 -1
- package/render/lightmap.js +23 -3
- package/render/occlusion.js +43 -12
- package/render/patches.js +2 -2
- package/render/postWebGL.js +4 -3
- package/render/probes.js +25 -0
- package/render/shadows.js +2 -2
- package/types/core.d.ts +142 -55
- package/types/gameplay.d.ts +92 -40
- package/types/generated/core/base/I18n.d.ts +2 -2
- package/types/generated/core/input/Input.d.ts +16 -2
- package/types/generated/core/input/inputSeat.d.ts +26 -0
- package/types/generated/core/inspect/DebugOverrides.d.ts +15 -1
- package/types/generated/core/ops/Ops.d.ts +2 -1
- package/types/generated/core/ops/control.d.ts +12 -2
- package/types/generated/core/saves/Saves.d.ts +26 -6
- package/types/generated/core/saves/session.d.ts +1 -1
- package/types/generated/core/scene/Views.d.ts +1 -1
- package/types/generated/core/stream/Stream.d.ts +6 -4
- package/types/generated/modules/grid/Grid.d.ts +1 -0
- package/types/generated/modules/particles/Particles.d.ts +4 -3
- package/types/generated/modules/timeline/Timeline.d.ts +5 -4
- package/types/generated/modules/weather/Weather.d.ts +1 -7
- package/types/generated/modules/world/World.d.ts +6 -4
- package/types/generated/render/PostFX.d.ts +6 -14
- package/types/nav.d.ts +36 -4
- package/types/physics.d.ts +55 -7
- package/types/pixel.d.ts +75 -0
- package/types/world.d.ts +14 -8
- package/ui/Parts.js +9 -2
- package/ui/theme.js +2 -2
- package/core/scene/engineCamera.js +0 -32
- package/modules/camera/projection.js +0 -21
|
@@ -10,20 +10,22 @@
|
|
|
10
10
|
// Vehicles are used through an interface (the { object, speed, drive(), seatPosition(), exitPosition() } returned by rigid.addWheeled); rigid is not imported
|
|
11
11
|
// here.
|
|
12
12
|
import { Character } from './Character.js';
|
|
13
|
-
import { createPlayer
|
|
13
|
+
import { createPlayer } from './player.js';
|
|
14
14
|
import { NPCManager, NPC } from './NPC.js';
|
|
15
15
|
import { findPath } from './astar.js';
|
|
16
|
-
import { findSpawn, moveInput } from './moveHelpers.js';
|
|
17
|
-
import { AsyncPaths } from './pathAsync.js';
|
|
16
|
+
import { findSpawn, moveInput, withHeights } from './moveHelpers.js';
|
|
17
|
+
import { AsyncPaths, sourcePath } from './pathAsync.js';
|
|
18
18
|
import { needMaterial } from '../../core/base/validate.js';
|
|
19
19
|
import { builtInSounds } from '../../core/assets/assetManifest.js';
|
|
20
20
|
import { readParams, keysSchema } from '../../core/base/params.js';
|
|
21
21
|
import { codedError } from '../../core/base/errorHints.js';
|
|
22
|
-
import { addSupport, carryRiders } from './movers/support.js';
|
|
23
|
-
import { moveStacks } from './movers/common.js';
|
|
22
|
+
import { addSupport, carryRiders, beginMove, endMove, groundAt } from './movers/support.js';
|
|
23
|
+
import { moveStacks, ownState } from './movers/common.js';
|
|
24
24
|
import { STRINGS } from './playerStrings.js';
|
|
25
|
+
import { addVehicle, stepSeats } from './seat.js';
|
|
25
26
|
import { footstepSynth, stepSound } from './footsteps.js';
|
|
26
|
-
import { xzOf } from '../../core/base/math.js';
|
|
27
|
+
import { xzOf, yOf } from '../../core/base/math.js';
|
|
28
|
+
import { surfaceOr } from '../../core/scene/ground.js';
|
|
27
29
|
import { isEngineMade } from '../../render/patches.js';
|
|
28
30
|
import { capsuleDefault, capsuleMaterial } from './proceduralRig.js';
|
|
29
31
|
export { Character, NPC };
|
|
@@ -37,22 +39,25 @@ export { Character, NPC };
|
|
|
37
39
|
* When an NPC is far it has no model: npc.root is null, npc.position is the data position
|
|
38
40
|
*/
|
|
39
41
|
|
|
40
|
-
/**
|
|
41
|
-
function solvePath(e, from, to, o) {
|
|
42
|
-
const { detailed, ...rest } = o;
|
|
43
|
-
let res;
|
|
44
|
-
if (
|
|
42
|
+
/** The characters.path rule, else navmesh when ready, else A* (characters.findPath; the one synchronous A* entry, see R20) */
|
|
43
|
+
function solvePath(e, st, from, to, o) {
|
|
44
|
+
const { detailed, id, ...rest } = o;
|
|
45
|
+
let res = sourcePath(e, st.path, from, to, o);
|
|
46
|
+
if (res) { /* the game's source answered */ }
|
|
47
|
+
else if (e.nav?.ready) res = e.nav.findPath(from, to, { ...rest, detailed: true });
|
|
45
48
|
else if (!e.collision) res = { status: 'notReady', path: null };
|
|
46
49
|
else {
|
|
47
50
|
const [sx, sz] = xzOf(from), [gx, gz] = xzOf(to), radius = rest.radius ?? e.movement.radius;
|
|
48
|
-
const
|
|
51
|
+
const floor = (x, z, y) => surfaceOr(e, x, z, y, y);
|
|
52
|
+
const path = findPath((x, z, y) => e.collision.walkableAt(x, z, radius, y), sx, sz, gx, gz, { ...rest, radius, y: floor(sx, sz, yOf(from)), goalY: yOf(to), floor, body: e.movement.height });
|
|
49
53
|
// nav installed but not ready: A* is only a stand-in, tell the caller honestly
|
|
50
54
|
res = { status: e.nav ? 'notReady' : path ? 'ok' : 'noPath', path };
|
|
51
55
|
}
|
|
56
|
+
if (res.path) withHeights(e, res.path, from, to);
|
|
52
57
|
return detailed ? res : res.path;
|
|
53
58
|
}
|
|
54
59
|
|
|
55
|
-
const CHARACTERS_PARAMS = keysSchema(['npcs', 'npcTiers', 'animation']);
|
|
60
|
+
const CHARACTERS_PARAMS = keysSchema(['npcs', 'npcTiers', 'animation', 'fall']);
|
|
56
61
|
|
|
57
62
|
/**
|
|
58
63
|
* The object behind engine.characters
|
|
@@ -102,18 +107,21 @@ function charactersApi(e, st, animation) {
|
|
|
102
107
|
*/
|
|
103
108
|
support(object, o) { return addSupport(e, object, o); },
|
|
104
109
|
/**
|
|
105
|
-
* Pathfinding (the only entry): navmesh if nav is ready, else A* (needs the collision module). Returns [{ x, z }…], null if unreachable.
|
|
110
|
+
* Pathfinding (the only entry): navmesh if nav is ready, else A* (needs the collision module). Returns [{ x, y, z }…], null if unreachable: every point has its height, and a y on from / to picks the floor.
|
|
106
111
|
* Pass { detailed: true } to get { status, path }: 'ok' found | 'noPath' unreachable | 'notReady' nav installed but mesh not generated yet (path is the
|
|
107
112
|
* A* fallback, may be inaccurate; null without collision)
|
|
113
|
+
* Where paths come from is the rule 'characters.path' (findPathAsync, walkTo and NPCs follow it too; o.id picks an entry for one character): default 'walk' is the above, false is no path,
|
|
114
|
+
* a function ({ from, to, o }, engine) => [{ x, y?, z }…] | null | 'walk' is your own source, for flight, swimming or anything off the ground. It must be deterministic; it is not saved
|
|
115
|
+
* @example engine.mod('characters.path', { owner: 'birds', id: 'hawk', set: ({ from, to }) => [from, { x: to.x, y: 40, z: to.z }, to] });
|
|
108
116
|
*/
|
|
109
|
-
findPath(from, to, o = {}) { return solvePath(e, from, to, o); },
|
|
117
|
+
findPath(from, to, o = {}) { return solvePath(e, st, from, to, o); },
|
|
110
118
|
/**
|
|
111
119
|
* findPath for many callers (NPC crowds, AI squads): solved in engine.threads workers, done(path, status) runs on a fixed step ~4 steps later
|
|
112
120
|
* via engine.commits, so threads on / off give the same paths. Keep findPath for the few that need a path now (click-to-move). Details: recipe 12
|
|
113
121
|
* section 4.
|
|
114
122
|
* @param {any} from
|
|
115
123
|
* @param {any} to
|
|
116
|
-
* @param {(path: { x: number, z: number }[] | null, status: 'ok' | 'noPath' | 'notReady') => void} done
|
|
124
|
+
* @param {(path: { x: number, y: number, z: number }[] | null, status: 'ok' | 'noPath' | 'notReady') => void} done
|
|
117
125
|
* @param {{ radius?: number, step?: number, maxNodes?: number }} [opts]
|
|
118
126
|
* @returns {{ remove: () => void }}
|
|
119
127
|
* @example const req = engine.characters.findPathAsync(guard.position, target, (path, status) => { guard.path = path; });
|
|
@@ -121,23 +129,46 @@ function charactersApi(e, st, animation) {
|
|
|
121
129
|
*/
|
|
122
130
|
findPathAsync(from, to, done, opts) { return st.paths.request(from, to, done, opts); },
|
|
123
131
|
/** How many findPathAsync requests leave per fixed step (default 32); the rest wait for the next steps */
|
|
132
|
+
/**
|
|
133
|
+
* Bends a link crossing away from its straight line: (t, position, crossing, character) with t 0-1, called each step after position is set on the line, write to position.
|
|
134
|
+
* Depends only on its arguments, so a crossing saved halfway resumes on the same curve. null = the straight line
|
|
135
|
+
* @example engine.characters.linkPose = (t, p) => { p.y += Math.sin(t * Math.PI) * 1.2; }; // every crossing is a hop
|
|
136
|
+
* @experimental
|
|
137
|
+
*/
|
|
138
|
+
linkPose: null,
|
|
124
139
|
get pathsPerStep() { return st.paths.perStep; },
|
|
125
140
|
set pathsPerStep(n) { st.paths.perStep = Math.max(1, n | 0); },
|
|
126
141
|
/**
|
|
127
142
|
* The 'camera' player control as a value: movement keys / stick relative to the camera (world axes, up = -Z, without a camera module), the 'run' action
|
|
128
143
|
* when the game bound one, input.autopilot.
|
|
129
|
-
* Returns a reused { x, z, run } (do not keep it). Call it from your own control function to wrap the default instead of rewriting it
|
|
144
|
+
* Returns a reused { x, z, run } (do not keep it). Call it from your own control function to wrap the default instead of rewriting it. seat: read an engine.input.seat() instead of engine.input
|
|
130
145
|
* @example createPlayer({ ...presets.player.thirdPerson, control: (p, input) => busy ? null : engine.characters.moveInput() }) // no movement while
|
|
131
146
|
* attacking
|
|
132
147
|
*/
|
|
133
|
-
moveInput() { return moveInput(e); },
|
|
148
|
+
moveInput(seat) { return moveInput(e, seat); },
|
|
149
|
+
/**
|
|
150
|
+
* What a game's mover calls to take part in contact, each optional: a mover that calls none owns contact itself.
|
|
151
|
+
* beginMove(c): first in move(), a platform under the feet carries it, true when something outside moved it. groundAt(c, x, z, y): ground height at a spot, supports included.
|
|
152
|
+
* endMove(c, dt, hit, ground): last in move(), keeps support, safeGround and the fall check (hit: the support under the feet, or null). ownState(c, base, speed): the game's states hook, then the state.
|
|
153
|
+
* The mover contract and an example: recipe 57
|
|
154
|
+
*/
|
|
155
|
+
kit: { beginMove, endMove, groundAt, ownState },
|
|
156
|
+
/**
|
|
157
|
+
* Join the vehicles characters can be seated in again: a saved rider finds its vehicle here by id, also when the vehicle is built after the load. rigid.addWheeled joins its cars;
|
|
158
|
+
* a game's own boat or mount joins here, before or after the load. Returns { remove() }.
|
|
159
|
+
* @param {any} vehicle anything with the vehicle contract and an id
|
|
160
|
+
* @returns {{ remove(): void }}
|
|
161
|
+
* @example engine.characters.addVehicle(boat);
|
|
162
|
+
*/
|
|
163
|
+
addVehicle(vehicle) { return e.ops.allow(() => addVehicle(e, vehicle)); },
|
|
134
164
|
/** @internal outside a fixed step: the rig calls it before it reads a rider's position (camera/common.js) */
|
|
165
|
+
/** @internal the camera calls this after its own step, so a rider and the view move together */
|
|
135
166
|
carryRiders() { carryRiders(e, true); },
|
|
136
167
|
/**
|
|
137
|
-
* Remove
|
|
168
|
+
* Remove a player, by default engine.player (exit the vehicle first, stop responding to keys, dispose the model: animation, geometry, materials; the 'player' record in
|
|
138
169
|
* world is removed too)
|
|
139
170
|
*/
|
|
140
|
-
removePlayer() { st.
|
|
171
|
+
removePlayer(id = e.player?.id) { st.players.get(id)?.dispose(); },
|
|
141
172
|
/**
|
|
142
173
|
* Create the player. The engine assumes no feel: control is required, keys and camera follow come only from options (presets.player.* bundle them).
|
|
143
174
|
* The player is also the world record with id 'player' (needs the world module).
|
|
@@ -156,7 +187,7 @@ function charactersApi(e, st, animation) {
|
|
|
156
187
|
* The world record, saving and describe().player work the same for every control. The engine drives the player on its channel (default
|
|
157
188
|
* 'player': dt passed to move and to the control function is engine.time.dt('player', 'player'), so engine.mod('time.player', ...) slows it)
|
|
158
189
|
* @example const player = engine.characters.createPlayer({ ...presets.player.thirdPerson, spawn: engine.characters.findSpawn() })
|
|
159
|
-
* @example const roll = engine.control.take(player, { owner: 'roll', layer: 'script', step: () => ({ x: 0, z: 1, run: true }) }); engine.clock.after(0.4, () => roll.remove())
|
|
190
|
+
* @example const roll = engine.control.take(player, { owner: 'roll', layer: 'script', step: () => ({ x: 0, z: 1, run: true }) }); engine.clock.after(0.4, () => roll.remove(), { channel: 'real' })
|
|
160
191
|
*/
|
|
161
192
|
// creating the player is setup (a game does it after loading its model, or on level:loaded), not a gameplay action: the ops guard lets its writes through
|
|
162
193
|
createPlayer(opts = {}) { return e.ops.allow(() => api._createPlayer(opts)); },
|
|
@@ -176,13 +207,35 @@ function declareActions(ctx) {
|
|
|
176
207
|
|
|
177
208
|
/** What the module listens to, all through ctx.track */
|
|
178
209
|
function listen(e, ctx, st) {
|
|
179
|
-
// the
|
|
180
|
-
|
|
181
|
-
ctx.track(e.events.on('character:
|
|
210
|
+
// the players' footsteps: plain listeners, so a game's own listener adds to them instead of taking the one slot (silent until audio.sounds names a sound)
|
|
211
|
+
const isPlayer = c => st.players.get(c.id)?.character === c;
|
|
212
|
+
ctx.track(e.events.on('character:step', ({ character }) => { if (isPlayer(character)) e.audio.sfx(stepSound(e, character.groundType())); }));
|
|
213
|
+
ctx.track(e.events.on('character:land', ({ character, impact }) => { if (isPlayer(character) && impact > 3) e.audio.sfx(stepSound(e, character.groundType()), { volume: Math.min(2, impact / 6) }); }));
|
|
182
214
|
ctx.track(e.events.on('ground:change', b => st.paths.dropTiles(b))); // dug or raised ground: resample the path rasters there
|
|
183
|
-
ctx.track(e.events.on(
|
|
215
|
+
for (const n of ['level:loaded', 'level:unloaded']) ctx.track(e.events.on(n, () => { st.paths.tiles.clear(); e.control.endTasks(null, 'level'); })); // another level, or none: no cached raster and no path planned on it applies
|
|
184
216
|
}
|
|
185
217
|
|
|
218
|
+
/**
|
|
219
|
+
* The npcs section of content data. The key is the NPC's stable id (saves and quest targets use it). Every documented npcs.add option can be written; the same
|
|
220
|
+
* option table checks the spelling. The content form owns start / schedule (clock strings, places) and the id, so those are translated, not passed as written
|
|
221
|
+
*/
|
|
222
|
+
const NPC_CONTENT = {
|
|
223
|
+
desc: 'NPCs (id -> name, schedule, interact)',
|
|
224
|
+
validate(d, { engine: e, ids }) {
|
|
225
|
+
if (!e.npcs) throw codedError('module-needs', 'content npcs: characters({ npcs: false }) has no NPCs: drop npcs: false');
|
|
226
|
+
const keys = keysSchema([...e.npcs.optionKeys.filter(k => !['id', 'position', 'zone', 'initial'].includes(k)), 'start']);
|
|
227
|
+
for (const [id, n] of Object.entries(d)) { ids.claim(id, `npcs.${id}`); readParams(`content npcs.${id}`, keys, n); }
|
|
228
|
+
},
|
|
229
|
+
undo: (d, { engine: e }) => e.npcs.undoPoint(), // the records go back with the world (modules/content/loader.js keepLoad)
|
|
230
|
+
commit(d, { engine: e, where, zone, origin }) {
|
|
231
|
+
for (const [id, n] of Object.entries(d)) {
|
|
232
|
+
const plan = n.schedule ?? [], { start: _s, schedule: _p, ...rest } = n;
|
|
233
|
+
e.npcs.add({ ...rest, id, schedule: plan.map(s => ({ at: s.at, position: where(s) })), position: where(n.start || plan[0]), zone: zone(n.start, ...plan), initial: true });
|
|
234
|
+
origin(id, d, id);
|
|
235
|
+
}
|
|
236
|
+
},
|
|
237
|
+
};
|
|
238
|
+
|
|
186
239
|
/** describe(): the NPCs near the focus and the player's character state */
|
|
187
240
|
function describeCharacters(e, ctx, mgr) {
|
|
188
241
|
// with a model report by the model (merged with interaction points and world's own report for the same object into one), without a model report by data
|
|
@@ -196,25 +249,28 @@ function describeCharacters(e, ctx, mgr) {
|
|
|
196
249
|
|
|
197
250
|
export function characters(opts = {}) {
|
|
198
251
|
readParams('characters', CHARACTERS_PARAMS, opts);
|
|
199
|
-
const { npcs = true, npcTiers, animation } = opts;
|
|
200
|
-
//
|
|
252
|
+
const { npcs = true, npcTiers, animation, fall } = opts;
|
|
253
|
+
// players: what createPlayer made, by id. Their per-frame animation and per-step drive run in this module's own fixed / frame hooks (not
|
|
201
254
|
// engine.onFixed), so a game onFixed sees the same player position whether it was registered before or after createPlayer (phase 'early' = before the move,
|
|
202
255
|
// 'late' = after)
|
|
203
|
-
const st = { mgr: null, paths: null,
|
|
256
|
+
const st = { mgr: null, paths: null, path: null, players: new Map(), material: null };
|
|
204
257
|
return {
|
|
205
258
|
// before rigid: the player's move this step is in the physics step that follows (as when control was an onFixed)
|
|
206
|
-
|
|
259
|
+
content: { npcs: NPC_CONTENT },
|
|
260
|
+
name: 'characters', skipKind: 'none', type: 'actor', provides: ['characters'], params: CHARACTERS_PARAMS, options: opts, label: 'Characters', before: ['rigid'], noSkip: 'fixed steps paths, control holders and riders, none of which a time jump moves; schedules read the calendar clock', optional: ['cameraRig', 'calendar', 'interactions', 'dialogue', 'nav', 'collision', 'rigid', 'world'],
|
|
207
261
|
install(e, ctx) {
|
|
208
262
|
e.i18n.add(STRINGS); e.i18n.add({ 'zh-CN': { 'module.characters': '角色' }, en: { 'module.characters': 'characters' } });
|
|
209
263
|
// move.* are defined at module install: doc examples call engine.mod('move.speed', …) in setup before creating a character, and relying on the first Character's definition would give unknown-name
|
|
210
264
|
moveStacks(e);
|
|
265
|
+
if (fall) ctx.mod('fall', { set: fall, layer: 'engine' }); // characters({ fall: 'rescue' }): the game's choice for everyone; engine.mod('fall', ...) from the game sits above it
|
|
211
266
|
declareActions(ctx);
|
|
212
267
|
builtInSounds(e.assets, { footstep: footstepSynth }); // sound asset 'footstep', silent until audio.sounds names it (step.grass: { src: 'footstep', params })
|
|
213
|
-
if (npcs) { st.mgr = new NPCManager(e, ctx.
|
|
268
|
+
if (npcs) { st.mgr = new NPCManager(e, ctx, st.players); e.npcs = st.mgr; if (npcTiers) st.mgr.tiers(npcTiers); }
|
|
214
269
|
else if (npcTiers) throw codedError('module-params', 'characters({ npcTiers }) needs NPCs: drop npcs: false, or drop npcTiers');
|
|
215
|
-
st.
|
|
270
|
+
st.path = ctx.rule('characters.path', 'walk');
|
|
271
|
+
st.paths = new AsyncPaths(e, st.path);
|
|
216
272
|
// control (core/ops/control.js) finds a held character by id: the player, else an NPC's model (null while it is far)
|
|
217
|
-
e.control.resolveWith(id => (id
|
|
273
|
+
e.control.resolveWith(id => st.players.get(id)?.character ?? st.mgr?.character(id) ?? null);
|
|
218
274
|
listen(e, ctx, st);
|
|
219
275
|
return charactersApi(e, st, animation);
|
|
220
276
|
},
|
|
@@ -224,9 +280,13 @@ export function characters(opts = {}) {
|
|
|
224
280
|
// player control runs here (stage late: after every early onFixed and the gameplay stages); then carry riders by what moved since their own step
|
|
225
281
|
// (support.js): every platform mover of this step has run by now
|
|
226
282
|
// control holders' steps (the player's input, game steps) run here too, in that order: one move per held character per step (core/ops/control.js)
|
|
227
|
-
fixed(_dt, e) { st.paths.flush(); e.control.stepAll(); carryRiders(e, false); },
|
|
283
|
+
fixed(_dt, e) { st.paths.flush(); e.control.stepAll(); stepSeats(e); carryRiders(e, false); },
|
|
228
284
|
// a timeline or frame code moved a platform: carry now, so the picture never shows a rider sinking into or floating above it
|
|
229
|
-
frame(dt, e) { st.
|
|
285
|
+
frame(dt, e) { for (const p of st.players.values()) p.character.animate(dt); carryRiders(e, true); },
|
|
230
286
|
describe(e, ctx) { describeCharacters(e, ctx, st.mgr); },
|
|
287
|
+
// a player, a queued path and the control lookup all outlive install unless they are ended here; the player's record stays in world, as NPC records do
|
|
288
|
+
dispose(e) { for (const p of [...st.players.values()]) p.dispose(true); st.paths?.stop(); e.control.resolveWith(() => null); if (st.mgr) e.npcs = null; },
|
|
231
289
|
};
|
|
232
290
|
}
|
|
291
|
+
|
|
292
|
+
export const handRolled = 'none: walking code is game movement, and any code shape would match ordinary maths';
|
|
@@ -1,37 +1,102 @@
|
|
|
1
1
|
// Spawn search and the camera-relative move input (split out of index.js; the characters API calls these with its engine)
|
|
2
2
|
import * as THREE from 'three';
|
|
3
|
-
import { xzOf } from '../../core/base/math.js';
|
|
3
|
+
import { xzOf, yOf } from '../../core/base/math.js';
|
|
4
4
|
import { readParams, keysSchema } from '../../core/base/params.js';
|
|
5
|
+
import { sameFloor } from '../../core/scene/floor.js';
|
|
5
6
|
|
|
6
|
-
const REQ = { x: 0, z: 0, run: false }; // moveInput() result, reused every step (no allocation)
|
|
7
|
+
const REQ = { x: 0, y: 0, z: 0, run: false }; // moveInput() result, reused every step (no allocation)
|
|
7
8
|
|
|
8
9
|
/** Spiral outward from position for walkable dry ground (characters.findSpawn) */
|
|
9
10
|
export function findSpawn(e, position, opts) {
|
|
10
|
-
const [x, z] = xzOf(position);
|
|
11
|
+
const [x, z] = xzOf(position), y = yOf(position); // a height picks the floor, as everywhere; none: the top ground
|
|
11
12
|
readParams('characters.findSpawn()', keysSchema(['minH', 'maxH', 'maxSlope', 'waterMargin']), opts);
|
|
12
13
|
const { minH = -Infinity, maxH = Infinity, maxSlope = 0.3, waterMargin = 0.5 } = opts; // 0.3: a spawn wants gentler ground than walkableAt, which still applies
|
|
13
14
|
for (let r = 0; r < 4000; r += 6) {
|
|
14
|
-
const a = r * 0.37, px = x + Math.cos(a) * r, pz = z + Math.sin(a) * r, h = e.surface(px, pz);
|
|
15
|
-
const dry = h > e.waterAt(px, pz) + waterMargin, walk = !e.collision || e.collision.walkableAt(px, pz);
|
|
16
|
-
if (dry && h > minH && h < maxH && e.slope(px, pz) < maxSlope && walk) return new THREE.Vector3(px, h, pz);
|
|
15
|
+
const a = r * 0.37, px = x + Math.cos(a) * r, pz = z + Math.sin(a) * r, h = e.surface(px, pz, y);
|
|
16
|
+
const dry = h > e.waterAt(px, pz, h) + waterMargin, walk = !e.collision || e.collision.walkableAt(px, pz, undefined, y);
|
|
17
|
+
if (dry && h > minH && h < maxH && e.slope(px, pz, y) < maxSlope && walk) return new THREE.Vector3(px, h, pz);
|
|
17
18
|
}
|
|
18
|
-
return new THREE.Vector3(x, e.surface(x, z), z);
|
|
19
|
+
return new THREE.Vector3(x, e.surface(x, z, y), z);
|
|
19
20
|
}
|
|
20
21
|
|
|
21
22
|
/**
|
|
22
|
-
* Index of the path point to head for from (x, z), starting at i: skips points within reach and points already passed (a later point is closer), so the walker
|
|
23
|
-
* never turns back. Never skips the last point
|
|
23
|
+
* Index of the path point to head for from (x, y, z), starting at i: skips points within reach and points already passed (a later point is closer), so the walker
|
|
24
|
+
* never turns back. A point more than `level` above or below is not passed, however close in x, z: on a spiral stair the next turn lies right overhead. Never skips the last point (arriving is the caller's) or a link's start (a link is crossed, not cut past). One rule for walkTo and NPCs; their stuck tests stay apart (walkTo's window has a public
|
|
24
25
|
* option, an NPC's would have to go in its save)
|
|
25
26
|
*/
|
|
26
|
-
export function nextPoint(path, i, x, z, reach) {
|
|
27
|
-
const dist = p => Math.hypot(p.x - x, p.z - z);
|
|
28
|
-
while (i < path.length - 1 && (dist(path[i]) < reach || dist(path[i + 1]) < dist(path[i]))) i++;
|
|
27
|
+
export function nextPoint(path, i, x, y, z, reach, level) {
|
|
28
|
+
const dist = p => !sameFloor(p.y ?? y, y, level) ? Infinity : Math.hypot(p.x - x, p.z - z); // p.y: a path saved before paths kept height has none
|
|
29
|
+
while (i < path.length - 1 && !path[i].link && dist(path[i]) < Infinity && (dist(path[i]) < reach || dist(path[i + 1]) < dist(path[i]))) i++;
|
|
29
30
|
return i;
|
|
30
31
|
}
|
|
31
32
|
|
|
32
|
-
/** The
|
|
33
|
-
export
|
|
34
|
-
|
|
33
|
+
/** The up part of the unit direction from p to q, what a walk hands a mover as y: built-in movers leave height to the ground, a flying one follows it */
|
|
34
|
+
export const climbTo = (p, q) => { const dy = (q.y ?? p.y) - p.y, d = Math.hypot(q.x - p.x, dy, q.z - p.z); return d > 1e-6 ? dy / d : 0; };
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Whether p stands at the height a goal asks for: within a body height of it, or of the floor that height picks. The second half is why a stop written [x, 0, z] on raised
|
|
38
|
+
* terrain still counts: the height field has one layer, so y only ever picks that. No y on the goal: any height. One rule for walkTo and NPC stops
|
|
39
|
+
*/
|
|
40
|
+
export function sameLevel(e, p, at) {
|
|
41
|
+
const y = yOf(at), h = e.movement.height;
|
|
42
|
+
if (y === undefined || sameFloor(p.y, y, h)) return true;
|
|
43
|
+
const [x, z] = xzOf(at);
|
|
44
|
+
return sameFloor(p.y, e.surface(x, z, y), h);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Give the points of a path that came without height (A*, a game's path source) theirs, in place: each from the ground under it, the height before it choosing the layer;
|
|
49
|
+
* the last takes the goal's given height. Here and not in the search: the worker that runs A* has no ground
|
|
50
|
+
*/
|
|
51
|
+
export function withHeights(e, path, from, to) {
|
|
52
|
+
let ref = yOf(from);
|
|
53
|
+
const gy = yOf(to);
|
|
54
|
+
path.forEach((q, i) => {
|
|
55
|
+
if (!Number.isFinite(q.y)) { const y = i === path.length - 1 && gy !== undefined ? gy : e.surface(q.x, q.z, ref); q.y = Number.isFinite(y) ? y : ref ?? 0; } // the void of terrain({ shape }) is -Infinity
|
|
56
|
+
ref = q.y;
|
|
57
|
+
});
|
|
58
|
+
return path;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Crossing a link (nav({ links })), one rule for NPCs and walkTo. The walker leaves its mover for the crossing: no gravity, no ground, its place comes from the
|
|
62
|
+
// crossing alone, so the plain data { link, from, to, seconds, time } is the whole state and a save taken halfway goes on from the same spot.
|
|
63
|
+
// The look is the game's: link:enter is the moment to play a clip and set crossing.seconds, characters.linkPose bends the line. A game doing neither gets a straight
|
|
64
|
+
// line at walking speed.
|
|
65
|
+
|
|
66
|
+
/** How near a link's start a walker must come before it crosses (m): a crossing runs from where the walker stands, so a far one starts a climb off the ladder's foot */
|
|
67
|
+
export const LINK_REACH = 0.3;
|
|
68
|
+
/** Begin crossing a path point's link from where the character stands; returns the crossing for the caller to keep */
|
|
69
|
+
export function startLink(ch, { id, to }) {
|
|
70
|
+
ch._place(ch.root.position); // off any platform: nothing may carry it during the crossing
|
|
71
|
+
const p = ch.root.position, crossing = { link: id, from: { x: p.x, y: p.y, z: p.z }, to, seconds: Math.hypot(to.x - p.x, to.y - p.y, to.z - p.z) / ch.stat('speed'), time: 0 };
|
|
72
|
+
ch.face(to); ch.velocity.set(0, 0); ch.vy = 0; ch.grounded = false;
|
|
73
|
+
ch.engine.events.emit('link:enter', { id: ch.id, character: ch, crossing });
|
|
74
|
+
if (!(crossing.seconds > 0 && crossing.seconds < Infinity)) crossing.seconds = 0; // speed 0 (a move.speed mod that roots it) or a game's bad value: a crossing always ends, and Infinity would not survive a save
|
|
75
|
+
return crossing;
|
|
76
|
+
}
|
|
77
|
+
/** One step of a crossing: true once it has landed */
|
|
78
|
+
export function stepLink(ch, c, dt) {
|
|
79
|
+
c.time += dt;
|
|
80
|
+
const t = c.seconds > 0 ? c.time / c.seconds : 1, p = ch.root.position, { from: a, to: b } = c;
|
|
81
|
+
if (t >= 1) return landLink(ch, c);
|
|
82
|
+
p.set(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t, a.z + (b.z - a.z) * t);
|
|
83
|
+
ch.engine.characters.linkPose?.(t, p, c, ch);
|
|
84
|
+
ch.syncRecord();
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Put the character at the far end now: the crossing ran out, its walk was cancelled, or a control holder above took the body. A holder pauses what is below it, but a
|
|
89
|
+
* crossing cannot wait paused: the body would hang over the gap, and picking the crossing up later would snap it back to the line. Returns true, as stepLink does on landing
|
|
90
|
+
*/
|
|
91
|
+
export function landLink(ch, c) { ch._place(c.to); ch.grounded = true; leaveLink(ch.engine, ch.id, ch, c); return true; }
|
|
92
|
+
/** Whether a link is still to cross on the path from point i on. Arriving is judged in x, z, which says nothing about a ladder's height: the walker is not there yet */
|
|
93
|
+
export const linkAhead = (path, i = 0) => !!path?.some((p, k) => k >= i && p.link);
|
|
94
|
+
/** A crossing is over, landed or cut short. character: null for an NPC that finished it without a model */
|
|
95
|
+
export function leaveLink(e, id, character, crossing) { e.events.emit('link:exit', { id, character, crossing }); }
|
|
96
|
+
|
|
97
|
+
/** The 'camera' player control as a value (characters.moveInput), read from a seat or engine.input; returns the reused REQ */
|
|
98
|
+
export function moveInput(e, inp = e.input) {
|
|
99
|
+
const a = inp.autopilot || inp.axis; // autopilot (stress tests, recordings): input.autopilot = { x, y, speed? } (speed: the player hook mods move.speed)
|
|
35
100
|
const f = e.cameraRig ? e.cameraRig.forward() : { x: 0, z: -1 }, rt = e.cameraRig ? e.cameraRig.right() : { x: 1, z: 0 };
|
|
36
101
|
// run is the game's key, not a core default
|
|
37
102
|
REQ.x = f.x * a.y + rt.x * a.x; REQ.z = f.z * a.y + rt.z * a.x; REQ.run = inp.actions.has('run') && inp.down('run');
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// What a game's mover must be (fields: see gameplay/inventory.contract.js; shared check: @threedog/tools/contract). Built by (character, engine) => mover, once per character.
|
|
2
|
+
// move(x, z, run, dt, y) every step the character moves: x, z the direction (length 0-1 scales the speed), y up or down, default 0. It writes character.root.position
|
|
3
|
+
// name what describe().player.mover shows
|
|
4
|
+
// dispose() optional: the character is gone, release what the mover built
|
|
5
|
+
// leave() optional: another mover took over; take out of the world what this one keeps there. Its move() runs again when it is back
|
|
6
|
+
// Contact is the mover's to take or leave: engine.characters.kit (beginMove, groundAt, endMove, ownState). Calling none of it means the mover owns grounded, platforms and the fall check.
|
|
7
|
+
export default {
|
|
8
|
+
name: 'mover',
|
|
9
|
+
kind: 'object',
|
|
10
|
+
api: ['move'],
|
|
11
|
+
fields: ['name'],
|
|
12
|
+
position: { authority: 'solver', writers: { mover: 'move() writes character.root.position while the holder of the control slot drives it; it writes no record' } },
|
|
13
|
+
optional: { dispose: v => typeof v === 'function', leave: v => typeof v === 'function' },
|
|
14
|
+
};
|
|
@@ -17,10 +17,12 @@ const MOVE_STATS = ['speed', 'runSpeed', 'gravity', 'jumpSpeed', 'canJump'];
|
|
|
17
17
|
/** 'speed' -> 'move.speed', built once: stat runs several times per character per step, so no string is joined there */
|
|
18
18
|
const STACK_OF = Object.fromEntries(MOVE_STATS.map(k => [k, 'move.' + k]));
|
|
19
19
|
|
|
20
|
-
/** @internal define the move.* stacks once per engine: the characters module install does it (so engine.mod works in setup); the Character constructor calls it too for a character made without the module */
|
|
20
|
+
/** @internal define the move.* stacks and the rule 'fall' once per engine: the characters module install does it (so engine.mod works in setup); the Character constructor calls it too for a character made without the module */
|
|
21
21
|
export function moveStacks(e) {
|
|
22
22
|
if (e.stacks.has('move.speed')) return;
|
|
23
|
+
e.stacks.rule('character.mover', null); // defined here for the same reason: engine.mod('character.mover', ...) in setup, before any character
|
|
23
24
|
for (const k of MOVE_STATS) e.stacks.define(STACK_OF[k], { per: 'optional', ...(k === 'gravity' ? {} : { min: 0 }) });
|
|
25
|
+
e.stacks.rule('fall', false, ['rescue']);
|
|
24
26
|
}
|
|
25
27
|
|
|
26
28
|
/** The steepest ground a walker climbs (rise over run): the collision module's, else the engine's own */
|
|
@@ -60,7 +62,7 @@ const WATER_KEY = new WeakMap(); // the water mode the character is in now (nu
|
|
|
60
62
|
export function water(c, ox, oz) {
|
|
61
63
|
const e = c.engine, p = c.root.position;
|
|
62
64
|
let ground = groundAt(c, p.x, p.z, p.y); // pass the current height: walk the ground under a bridge, the deck on top
|
|
63
|
-
const surface = e.waterAt(p.x, p.z), swimY = surface - c.swimDepth;
|
|
65
|
+
const surface = e.waterAt(p.x, p.z, p.y), swimY = surface - c.swimDepth;
|
|
64
66
|
const mode = ground < swimY ? waterMode(c, surface - ground, p) : null;
|
|
65
67
|
if (mode === 'block') { p.x = ox; p.z = oz; c.velocity.set(0, 0); ground = groundAt(c, ox, oz, p.y); }
|
|
66
68
|
c.swimming = mode === 'swim' && p.y <= swimY + 0.3;
|
|
@@ -135,7 +137,7 @@ export function finish(c, dt) {
|
|
|
135
137
|
}
|
|
136
138
|
c.moveAmount = sp;
|
|
137
139
|
// a game write since the last step would vanish here without a word (no-conflict rule): say so once, with the calls that do work
|
|
138
|
-
if (c.state !== c.
|
|
140
|
+
if (c.state !== c.stateMark) warnOnce(STATE_WRITTEN);
|
|
139
141
|
const base = c.swimming ? 'swim' : !c.grounded ? (c.vy > 0 ? 'jump' : 'fall') : sp > stat(c, 'speed') * 1.3 ? 'run' : sp > 0.4 ? 'walk' : 'idle';
|
|
140
142
|
// the game's states hook adds its own (crouch, climb, glide, dash...) on top of the built-in ones without taking the mover over; null keeps base
|
|
141
143
|
ownState(c, base, sp);
|
|
@@ -143,5 +145,5 @@ export function finish(c, dt) {
|
|
|
143
145
|
|
|
144
146
|
/** @internal the state after a mover's step: the game's states hook, or base when it returns null */
|
|
145
147
|
export function ownState(c, base, speed) {
|
|
146
|
-
c.state = c.
|
|
148
|
+
c.state = c.stateMark = (c.states && c.states(c, { base, speed, grounded: c.grounded, swimming: c.swimming, vy: c.vy, water: WATER_KEY.get(c) ?? null })) || base;
|
|
147
149
|
}
|
|
@@ -12,18 +12,20 @@ export function rapierMover(c, fallback) {
|
|
|
12
12
|
move(dirX, dirZ, run, dt) {
|
|
13
13
|
const e = c.engine, r = e.rigid, p = c.root.position;
|
|
14
14
|
if (!r?.ready || !r.readyAt(p)) return fallback.move(dirX, dirZ, run, dt);
|
|
15
|
-
const fresh = !m.controller || m.
|
|
15
|
+
const fresh = !m.controller || m.world !== r.world; // another world (newGame / load rebuilt it, a replaced rigid brought its own): the old controller went with the old one
|
|
16
16
|
if (fresh) setup(e, r);
|
|
17
17
|
const col = m.collider;
|
|
18
18
|
// moved outside the mover (placeAt, load, leaving a vehicle, a platform carrying it; support.js tells exactly): the collider goes there first
|
|
19
|
-
if (beginMove(c) || fresh) { m.body.setTranslation({ x: p.x, y: p.y, z: p.z }, true); r.world.propagateModifiedBodyPositionsToColliders(); }
|
|
19
|
+
if (beginMove(c) || fresh || m.away) { m.away = false; col.setEnabled(true); m.body.setTranslation({ x: p.x, y: p.y, z: p.z }, true); r.world.propagateModifiedBodyPositionsToColliders(); }
|
|
20
20
|
steer(c, dirX, dirZ, run, dt);
|
|
21
21
|
const ox = p.x, oz = p.z, inh = c.grounded ? null : inherited(c);
|
|
22
22
|
|
|
23
23
|
// vertical intent: float to the surface when swimming, otherwise gravity + jump
|
|
24
24
|
tickJump(c, dt);
|
|
25
|
+
// -Infinity: no water here any more (placed in a dry cellar under the lake, the water removed); water() below ends the swim, this step already falls
|
|
26
|
+
const floatY = c.swimming ? e.waterAt(p.x, p.z, p.y) - c.swimDepth : -Infinity;
|
|
25
27
|
let dy;
|
|
26
|
-
if (
|
|
28
|
+
if (floatY > -Infinity) dy = floatY - p.y;
|
|
27
29
|
else {
|
|
28
30
|
tryJump(c);
|
|
29
31
|
if (c.grounded && c.vy <= 0) { c.vy = 0; dy = 0; } // do not press down while standing (it would break auto step-up); downhill relies on ground snapping
|
|
@@ -48,12 +50,17 @@ export function rapierMover(c, fallback) {
|
|
|
48
50
|
finish(c, dt);
|
|
49
51
|
endMove(c, dt, hit && Math.abs(ground - p.y) < 0.1 ? hit : null, 'collider'); // a support counts when the capsule stands on its top
|
|
50
52
|
},
|
|
53
|
+
/** Another mover took over: an NPC's capsule stays out of the world until this mover runs again, or it is an invisible wall where the character left and blocks that mover's own sweeps. The player's belongs to rigid, which keeps it on the character */
|
|
54
|
+
leave() {
|
|
55
|
+
const r = c.engine.rigid;
|
|
56
|
+
if (m.controller && m.body !== r?.playerBody && m.world === r?.world) { m.collider.setEnabled(false); m.away = true; }
|
|
57
|
+
},
|
|
51
58
|
/** Called when the character is deleted: release the NPC's own capsule and controller */
|
|
52
59
|
dispose() {
|
|
53
60
|
const r = c.engine.rigid;
|
|
54
61
|
if (!m.controller) return;
|
|
55
62
|
// built for a world that was rebuilt or disposed since (engine.dispose runs rigid's before world's, which tears down the NPC models): freed with it
|
|
56
|
-
if (m.
|
|
63
|
+
if (m.world !== r?.world) { m.controller = m.body = m.collider = null; return; }
|
|
57
64
|
r.world.removeCharacterController(m.controller);
|
|
58
65
|
if (m.body !== r.playerBody) r.world.removeRigidBody(m.body);
|
|
59
66
|
m.controller = m.body = m.collider = null;
|
|
@@ -62,7 +69,7 @@ export function rapierMover(c, fallback) {
|
|
|
62
69
|
|
|
63
70
|
function setup(e, r) {
|
|
64
71
|
const R = r.rapier, w = r.world, p = c.root.position;
|
|
65
|
-
m.
|
|
72
|
+
m.world = w; // what the handles below belong to: the world object itself, since a count would match across two rigid modules
|
|
66
73
|
if (c === e.player) { m.body = r.playerBody; m.collider = r.playerCollider; }
|
|
67
74
|
else {
|
|
68
75
|
const hh = r.playerCapsule.halfHeight;
|
|
@@ -3,6 +3,26 @@
|
|
|
3
3
|
import { steer, water, float, tryJump, tickJump, leaveGround, land, finish, stat, inherited, climbLimit } from './common.js';
|
|
4
4
|
import { groundAt, beginMove, endMove } from './support.js';
|
|
5
5
|
|
|
6
|
+
const MIN_PIECE = 0.05; // a body of no radius still walks in pieces of this length (m), not in endless ones
|
|
7
|
+
|
|
8
|
+
/** Move by (dx, dz) where the ground and colliders let it. False once it stopped dead: the rest of the step goes nowhere */
|
|
9
|
+
function walkPiece(c, dx, dz) {
|
|
10
|
+
const e = c.engine, p = c.root.position, ox = p.x, oz = p.z, oh = groundAt(c, ox, oz, p.y);
|
|
11
|
+
p.x += dx; p.z += dz;
|
|
12
|
+
// back to the old position where it cannot climb: step up directly within step height; above that check the slope; hitting ground higher than itself in the air counts as a wall
|
|
13
|
+
const nh = groundAt(c, p.x, p.z, p.y), step = Math.hypot(p.x - ox, p.z - oz), maxClimb = climbLimit(e), rise = nh - Math.max(oh, c.grounded ? oh : p.y);
|
|
14
|
+
const blocked = c.grounded ? (rise > c.stepHeight && step > 1e-4 && (nh - oh) / step > maxClimb) : nh > p.y + c.stepHeight;
|
|
15
|
+
let moving = true;
|
|
16
|
+
if (blocked) { p.x = ox; p.z = oz; c.velocity.set(0, 0); moving = false; }
|
|
17
|
+
const col = e.collision;
|
|
18
|
+
if (col?.blockedAt(p.x, p.z, c.radius)) { // addBlocked ground (river): slide along it, else stay put
|
|
19
|
+
const nx = p.x, nz = p.z;
|
|
20
|
+
if (!col.blockedAt(nx, oz, c.radius)) p.z = oz; else if (!col.blockedAt(ox, nz, c.radius)) p.x = ox; else { p.x = ox; p.z = oz; c.velocity.set(0, 0); moving = false; }
|
|
21
|
+
}
|
|
22
|
+
col?.resolve(p, c.radius);
|
|
23
|
+
return moving;
|
|
24
|
+
}
|
|
25
|
+
|
|
6
26
|
/** Create the 'simple' mover (c is the character) */
|
|
7
27
|
export function simpleMover(c) {
|
|
8
28
|
return {
|
|
@@ -12,18 +32,11 @@ export function simpleMover(c) {
|
|
|
12
32
|
beginMove(c); // a platform under the feet moved since last step: move with it first
|
|
13
33
|
steer(c, dirX, dirZ, run, dt);
|
|
14
34
|
|
|
15
|
-
const ox = p.x, oz = p.z,
|
|
16
|
-
|
|
17
|
-
//
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
if (blocked) { p.x = ox; p.z = oz; c.velocity.set(0, 0); }
|
|
21
|
-
const col = e.collision;
|
|
22
|
-
if (col?.blockedAt(p.x, p.z, c.radius)) { // addBlocked ground (river): slide along it, else stay put
|
|
23
|
-
const nx = p.x, nz = p.z;
|
|
24
|
-
if (!col.blockedAt(nx, oz, c.radius)) p.z = oz; else if (!col.blockedAt(ox, nz, c.radius)) p.x = ox; else { p.x = ox; p.z = oz; c.velocity.set(0, 0); }
|
|
25
|
-
}
|
|
26
|
-
col?.resolve(p, c.radius);
|
|
35
|
+
const ox = p.x, oz = p.z, inh = c.grounded ? null : inherited(c);
|
|
36
|
+
const dx = (c.velocity.x + (inh?.x ?? 0)) * dt, dz = (c.velocity.y + (inh?.y ?? 0)) * dt;
|
|
37
|
+
// a step longer than half the body is walked in pieces: colliders only push out of what the body overlaps, so one long step would land past a thin wall, on its far side
|
|
38
|
+
const pieces = Math.max(1, Math.ceil(Math.hypot(dx, dz) / Math.max(c.radius / 2, MIN_PIECE)));
|
|
39
|
+
for (let i = 0; i < pieces && walkPiece(c, dx / pieces, dz / pieces); i++);
|
|
27
40
|
|
|
28
41
|
const { ground, swimY, hit } = water(c, ox, oz);
|
|
29
42
|
|
|
@@ -8,8 +8,9 @@
|
|
|
8
8
|
// Why one "moved outside the mover" check here: a teleport guess from distance ("collider more than 0.3 m from the position") is inexact; a
|
|
9
9
|
// platform carry, placeAt and a load all move the position outside the mover, so the mover compares with where it left the character (exact).
|
|
10
10
|
import * as THREE from 'three';
|
|
11
|
-
import { codedError } from '../../../core/base/errorHints.js';
|
|
12
|
-
import {
|
|
11
|
+
import { codedError, coded } from '../../../core/base/errorHints.js';
|
|
12
|
+
import { DEV, warnOnce } from '../../../core/base/validate.js';
|
|
13
|
+
import { placeY, FALL_Y } from '../../../core/scene/ground.js';
|
|
13
14
|
|
|
14
15
|
const REG = new WeakMap(); // engine -> { list: Map<id, entry>, riders: Set<character> }
|
|
15
16
|
const STATE = new WeakMap(); // character -> per-character support state (private: games read character.support / safeGround)
|
|
@@ -154,17 +155,27 @@ export function endMove(c, dt, hit, ground = 'ground') {
|
|
|
154
155
|
(s.last ??= new THREE.Vector3()).copy(p);
|
|
155
156
|
}
|
|
156
157
|
|
|
157
|
-
/**
|
|
158
|
+
/** Dev, once: a player gone past the void with nobody deciding stays gone, which reads as a bug in the game unless something names the choice */
|
|
159
|
+
function tellNoFallRule(c) {
|
|
160
|
+
const e = c.engine;
|
|
161
|
+
if (c !== e.player || c.root.position.y >= Math.max(e.ground.voidY?.() ?? -Infinity, FALL_Y)) return;
|
|
162
|
+
const point = e.stacks.get('fall');
|
|
163
|
+
if (point.holders().length + point.holders(c.id).length) return;
|
|
164
|
+
warnOnce(coded('character-option', `the player fell past the void and no rule 'fall' is set, so nothing brings it back. Decide what a fall is: engine.mod('fall', { owner: 'game', set: 'rescue' }) with createPlayer({ fall: { belowY } }) returns it to safeGround, a function is your own rule, and set: false says the game handles it`), 'fall-no-rule');
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** belowY: a height or (character) => boolean. character:fall once per fall, then the rule 'fall' decides: 'rescue' goes back to safeGround, nothing or false leaves it to the game */
|
|
158
168
|
function fallCheck(c, s) {
|
|
169
|
+
if (DEV) tellNoFallRule(c);
|
|
159
170
|
const f = c.fall;
|
|
160
171
|
if (!f) return;
|
|
161
172
|
const out = typeof f.belowY === 'function' ? !!f.belowY(c) : c.root.position.y < f.belowY;
|
|
162
173
|
if (!out) { s.fell = false; return; }
|
|
163
|
-
if (s.fell) return; //
|
|
174
|
+
if (s.fell) return; // the rule kept it out there: say it once, not every step
|
|
164
175
|
s.fell = true;
|
|
165
|
-
const
|
|
166
|
-
c.engine.events.emit('character:fall',
|
|
167
|
-
if (
|
|
176
|
+
const safe = c.safeGround ? c.safeGround.clone() : null;
|
|
177
|
+
c.engine.events.emit('character:fall', { character: c, safe });
|
|
178
|
+
if (c.engine.stacks.rule('fall', false, ['rescue']).decide(c.id, { kind: 'character', who: c, position: c.root.position.clone(), safe }) !== 'rescue') return;
|
|
168
179
|
// the exact spot, not placeAt: a platform that was under this spot may have moved away, and placeAt would land on the ground below it
|
|
169
180
|
if (c.safeGround) c.root.position.copy(c.safeGround);
|
|
170
181
|
detach(c); c.velocity.set(0, 0); c.vy = 0; c._push = 0; c.grounded = true;
|