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
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
//
|
|
4
4
|
// const fisher = engine.npcs.add({
|
|
5
5
|
// id: 'fisher', name: '老渔夫', colors: { body: 0x4f6d8f },
|
|
6
|
-
// schedule: [ { at: '06:00', position:
|
|
7
|
-
// { at: '13:00', position:
|
|
6
|
+
// schedule: [ { at: '06:00', position: { x: 10, z: 20 } }, // go to the river at 6 am (at can also be a 0–1 time of day); a y in position picks the floor
|
|
7
|
+
// { at: '13:00', position: { x: 40, z: -5 } } ], // back home at noon
|
|
8
8
|
// interact: { op: 'npc.talk' }, // the interact key nearby runs this op (npc.talk = dialogue, script defaults to the NPC id)
|
|
9
9
|
// });
|
|
10
10
|
// interact: { label: 'Trade', op: 'shop.open', args: { shelf: 'fish' } } // any op: open a shop, hand in, recruit; the op gets { npc: id, ...args }
|
|
@@ -22,25 +22,27 @@
|
|
|
22
22
|
// Near / far is by distance to the focus; far -> near first catches up owed time (world handles it).
|
|
23
23
|
//
|
|
24
24
|
// One NPC record (all plain values, inspectable via engine.world.get(id)):
|
|
25
|
-
// name, behavior (behavior name), mind (behavior params and memory, plain values the behavior reads/writes), schedule [{ at: 0-1, x, z }],
|
|
25
|
+
// name, behavior (behavior name), mind (behavior params and memory, plain values the behavior reads/writes), schedule [{ at: 0-1, x, y?, z }] (y only when the stop gave one),
|
|
26
26
|
// interact ({ label (when text), op, args, radius } or null), pathStep, repath, arriveDistance, speed,
|
|
27
27
|
// look (look and feel params: colors, runSpeed, channel...), zone (which level it belongs to; null = everywhere),
|
|
28
28
|
// npcs.add({ channel: 'enemies' }): its time channel (default 'world'); near and far both follow it, so bullet time slows it anywhere.
|
|
29
|
-
// state (animation state), busy (in dialogue), nav { stop (current stop index), path (remaining waypoints), repath (re-path countdown) },
|
|
29
|
+
// state (animation state), busy (in dialogue), nav { stop (current stop index), path (remaining waypoints), repath (re-path countdown), link (the crossing under way, or none) },
|
|
30
30
|
// motion (motion state from Character.saveMotion)
|
|
31
31
|
// model given as an asset name ('villager', or ['villager', ...args]) is plain and goes into look (saved); a model object, animation, clips, a function-form interact label and
|
|
32
32
|
// any other option that is not a plain value (states: fn, water: fn, fall: { belowY: fn }, a Vector3 faceTarget) stay in memory only (by id);
|
|
33
33
|
// game code supplies them again via add after loading.
|
|
34
|
+
import { keepTable } from '../../core/base/sections.js';
|
|
34
35
|
import * as THREE from 'three';
|
|
35
36
|
import { CHARACTER_KEYS } from './Character.js';
|
|
36
37
|
import { buildNpcView, unbuildNpcView, defineTalk } from './npcView.js';
|
|
37
|
-
import { warnOnce, fmt
|
|
38
|
+
import { warnOnce, fmt } from '../../core/base/validate.js';
|
|
38
39
|
import { readParams, keysSchema } from '../../core/base/params.js';
|
|
39
40
|
import { NpcAvoid } from './npcAvoid.js';
|
|
40
41
|
import { parseClock } from '../../core/time/dayTime.js';
|
|
41
42
|
import { xzOf, yOf } from '../../core/base/math.js';
|
|
42
43
|
import { placeAtHeight } from './movers/support.js';
|
|
43
|
-
import { stopIndex, timeOfDay, npcChannel } from './npcSim.js';
|
|
44
|
+
import { stopIndex, timeOfDay, npcChannel, crossLink } from './npcSim.js';
|
|
45
|
+
import { leaveLink } from './moveHelpers.js';
|
|
44
46
|
import { Behaviors } from './behaviors.js';
|
|
45
47
|
import { codedError } from '../../core/base/errorHints.js';
|
|
46
48
|
import { isModelName } from '../../core/base/util.js';
|
|
@@ -111,9 +113,10 @@ export class NPC {
|
|
|
111
113
|
placeAt(position) {
|
|
112
114
|
const e = this._m.engine, W = this._m.world, rec = this.data; if (!rec) return this;
|
|
113
115
|
const [x, z] = xzOf(position), ch = this.character;
|
|
114
|
-
|
|
116
|
+
this._m._claim(rec); e.control.endTasks(this.id, 'placed');
|
|
117
|
+
if (ch) { if (ch.vehicle) ch.exit({ force: true }); ch._place(position); }
|
|
118
|
+
if (rec.vehicle != null) e.ops.allow(() => W.set(this.id, { vehicle: null, seat: null })); // its record waited for a vehicle not built yet: the game put it elsewhere
|
|
115
119
|
W.move(this.id, { x, y: ch ? ch.root.position.y : placeAtHeight(e, `npc.placeAt() (${this.id})`, x, z, yOf(position), rec.look?.stepHeight ?? e.movement.stepHeight, rec.position.y).y, z });
|
|
116
|
-
rec.nav.path = null; rec.nav.repath = 0;
|
|
117
120
|
return this;
|
|
118
121
|
}
|
|
119
122
|
/** Face a position (only turns with a model) */
|
|
@@ -125,18 +128,20 @@ export class NPC {
|
|
|
125
128
|
export class NPCManager {
|
|
126
129
|
/** @internal Every option add() reads: content npcs (modules/content/loader.js) are checked against the same table, without importing this module */
|
|
127
130
|
get optionKeys() { return NPC_KEYS; }
|
|
128
|
-
/** @param {
|
|
129
|
-
constructor(engine,
|
|
131
|
+
/** @param {any} engine @param {any} ctx the characters module's ctx @param {Map<string, { character: any }>} players the module's players, who NPCs give way to */
|
|
132
|
+
constructor(engine, ctx, players) {
|
|
133
|
+
const { track } = ctx;
|
|
130
134
|
this.engine = engine;
|
|
135
|
+
/** @internal the module's ctx: setup() registers npc.talk when the world is there */ this._ctx = ctx;
|
|
131
136
|
/** @internal */ this._handles = new Map(); // id -> NPC handle (just a cache, can be rebuilt any time)
|
|
132
137
|
/** @internal */ this._runtime = new Map(); // id -> non-saveable params (model, animation, clips, any non-plain option, function interact label)
|
|
133
138
|
/** @internal */ this._views = new WeakMap(); // model root -> { ch, use }
|
|
134
139
|
/** @internal */ this._ready = false;
|
|
135
140
|
/** Near NPCs avoid each other (on by default): avoid.set(false) turns it off, see npcAvoid.js */
|
|
136
|
-
this.avoid = new NpcAvoid(engine, track);
|
|
141
|
+
this.avoid = new NpcAvoid(engine, track, players);
|
|
137
142
|
/** @internal */ this._b = new Behaviors(); // behavior name -> { near, far } (code, not saved)
|
|
138
143
|
/** @internal */ this._tiers = null; // { fullRadius, nearEvery } from tiers(), null = every near NPC every step
|
|
139
|
-
/** @internal */ this._byName = new Set(); // NPCs added this run without id, using the name as id (
|
|
144
|
+
/** @internal */ this._byName = new Set(); // NPCs added this run without id, using the name as id (two with one name would be one record)
|
|
140
145
|
// on the event, not in remove(): world.remove(id) and thing.remove take NPCs too, and a later add of that id must not inherit the old model or trip the same-name check
|
|
141
146
|
track(engine.events.on('world:remove', ({ id }) => { this._runtime.delete(id); this._handles.delete(id); this._byName.delete(id); }));
|
|
142
147
|
// a load or new game is a new run, and it fires no world:remove: setup code adding the same names again is not a clash
|
|
@@ -149,14 +154,18 @@ export class NPCManager {
|
|
|
149
154
|
if (this._ready) return;
|
|
150
155
|
const e = this.engine, W = this.world;
|
|
151
156
|
this._ready = true;
|
|
152
|
-
W.present(KIND, {
|
|
157
|
+
this._ctx.track(W.present(KIND, {
|
|
153
158
|
positionFrom: 'view', // with a model the mover decides; world copies the model position back to data at the end of each fixed step
|
|
154
159
|
build: rec => buildNpcView(this, rec),
|
|
155
|
-
update: (rec, obj, dt) =>
|
|
160
|
+
update: (rec, obj, dt) => {
|
|
161
|
+
const v = this._views.get(obj);
|
|
162
|
+
if (v && rec.interact && v.useFrom !== e.interactions) W.rebuild(rec.id); // its use lives in the interactions module it was built with: another one starts empty
|
|
163
|
+
else v?.ch.animate(dt);
|
|
164
|
+
},
|
|
156
165
|
dispose: (rec, obj) => unbuildNpcView(this, rec, obj),
|
|
157
|
-
});
|
|
166
|
+
}));
|
|
158
167
|
this._b.addBuiltin(e, W);
|
|
159
|
-
defineTalk(e, this);
|
|
168
|
+
defineTalk(e, this._ctx, this);
|
|
160
169
|
/** @internal what world.simulate gets for NPCs; tiers() adds fullRadius / nearEvery on top and registers again */
|
|
161
170
|
this._sim = {
|
|
162
171
|
// near and far both run on the NPC's time channel (character.channel, else npcs.add({ channel })): world.simulate scales both dt by it
|
|
@@ -167,29 +176,47 @@ export class NPCManager {
|
|
|
167
176
|
// A bubble talk (npc.talk channel 'bubble') runs with no main dialogue, so bubbles still showing keep it busy too
|
|
168
177
|
if (rec.busy && !e.dialogue?.active && !e.dialogue?.bubbles?.().length) rec.busy = false;
|
|
169
178
|
this.avoid.stamp(rec);
|
|
170
|
-
|
|
179
|
+
const ch = rec.nav.link && this.character(rec.id);
|
|
180
|
+
const ran = this._drive(rec, () => ch ? crossLink(W, rec, ch, dt) : this._b.of(rec)?.near?.(rec, dt, this._handle(rec.id)));
|
|
181
|
+
if (ch && !ran) crossLink(W, rec, ch, dt, true);
|
|
171
182
|
},
|
|
172
183
|
far: (rec, dt) => {
|
|
173
184
|
this._drive(rec, () => this._b.of(rec)?.far?.(rec, dt, this._handle(rec.id)));
|
|
174
|
-
const ch = this.character(rec.id); // already far but the model is not torn down yet: let the model catch up with the data
|
|
175
|
-
if (ch) { ch.
|
|
185
|
+
const ch = this.character(rec.id); // already far but the model is not torn down yet: let the model catch up with the data. A rider stays with its seat, a ragdoll with its body: this is no write, and placing would end the ride or lift the body
|
|
186
|
+
if (ch && !ch.bodyHeld) { ch._place(rec.position); ch.loadMotion(rec.motion); }
|
|
176
187
|
},
|
|
177
188
|
};
|
|
189
|
+
this._ctx.track({ remove: () => this._simHandle.remove() }); // one tracked entry however often tiers() registers again
|
|
178
190
|
this._register();
|
|
179
191
|
}
|
|
180
192
|
/**
|
|
181
193
|
* @internal Run an NPC's behavior as the base holder of its control slot (core/ops/control.js, owner 'npcs'): only while nothing above holds it.
|
|
182
194
|
* A walkTo, a quest script or a cutscene on the NPC pauses its schedule / wander (near and far) and the behavior carries on when they end;
|
|
183
195
|
* the behavior's own character.move() calls pass the bypass check because they run inside this. A character another machine drives (net)
|
|
184
|
-
* is never taken: its behavior does not run here.
|
|
196
|
+
* is never taken: its behavior does not run here. Returns whether fn ran.
|
|
185
197
|
*/
|
|
186
198
|
_drive(rec, fn) {
|
|
199
|
+
return this._claim(rec) && this.engine.control.runAs(rec.id, 'npcs', fn);
|
|
200
|
+
}
|
|
201
|
+
/** @internal The NPC's own hold on its slot, taken when first needed. Its path is the task on it: a placement drops it, the other endings leave it to the schedule. false: another base or net holder drives it */
|
|
202
|
+
_claim(rec) {
|
|
187
203
|
const C = this.engine.control;
|
|
188
|
-
if (
|
|
189
|
-
C.
|
|
204
|
+
if (C.has(rec.id, 'npcs')) return true;
|
|
205
|
+
const top = C.of(rec.id)?.layer;
|
|
206
|
+
if (top === 'net' || top === 'base') return false;
|
|
207
|
+
C.take(rec.id, { owner: 'npcs', layer: 'base', task: why => { if (why === 'placed') this._dropPath(rec.id); } });
|
|
208
|
+
return true;
|
|
209
|
+
}
|
|
210
|
+
/** @internal A placement: the path, the answer still on its way (it starts from where it stood) and a crossing all belong to the old spot */
|
|
211
|
+
_dropPath(id) {
|
|
212
|
+
const rec = this.world.get(id), ch = this.character(id);
|
|
213
|
+
if (!rec) return;
|
|
214
|
+
if (ch) { ch._pathReq?.remove(); ch._pathReq = null; }
|
|
215
|
+
rec.nav.path = null; rec.nav.repath = 0;
|
|
216
|
+
if (rec.nav.link) { leaveLink(this.engine, id, ch, rec.nav.link); rec.nav.link = null; }
|
|
190
217
|
}
|
|
191
218
|
/** @internal (re)register NPC simulation with world: world.simulate replaces the earlier registration of the same kind */
|
|
192
|
-
_register() { this.world.simulate(KIND, this._tiers ? { ...this._sim, ...this._tiers } : this._sim); }
|
|
219
|
+
_register() { this._simHandle = this.world.simulate(KIND, this._tiers ? { ...this._sim, ...this._tiers } : this._sim); } // released: the entry setup() tracks
|
|
193
220
|
|
|
194
221
|
/**
|
|
195
222
|
* Near tiers for large crowds (off by default: every near NPC walks every fixed step). With tiers, NPCs within fullRadius m of the focus walk every step;
|
|
@@ -223,6 +250,8 @@ export class NPCManager {
|
|
|
223
250
|
this._b.set(name, impl);
|
|
224
251
|
return this;
|
|
225
252
|
}
|
|
253
|
+
/** @internal A failed content load puts what add kept beside the records back (core/base/sections.js undo): the function that does it @returns {() => void} */
|
|
254
|
+
undoPoint() { return keepTable(this._runtime); }
|
|
226
255
|
/**
|
|
227
256
|
* Add an NPC, returns a handle. Position: opts.position (Vector3 / object / [x, y, z] / { x, z }), defaults to the schedule's current stop.
|
|
228
257
|
* For behavior (default 'schedule') and mind see behavior().
|
|
@@ -240,7 +269,7 @@ export class NPCManager {
|
|
|
240
269
|
if (io !== null && !(io && typeof io === 'object' && typeof io.op === 'string')) throw codedError('character-option', fmt(STRINGS.badInteract, { got: JSON.stringify(io) }), TypeError);
|
|
241
270
|
if (io) readParams('npcs.add() interact', keysSchema(['label', 'op', 'args', 'radius']), io);
|
|
242
271
|
const id = given ?? name, look = {}, runtime = { ...this._runtime.get(id) }; // a re-add keeps what it does not pass, as the record does
|
|
243
|
-
if (
|
|
272
|
+
if (given == null) { if (this._byName.has(id)) throw codedError('world-id', fmt(STRINGS.sameName, { name })); this._byName.add(id); }
|
|
244
273
|
// an asset name is data (saved); a model object, animation, clips and whatever is not plain stay in memory
|
|
245
274
|
for (const [k, v] of Object.entries(rest)) {
|
|
246
275
|
if ((RUNTIME_KEYS.includes(k) && !(k === 'model' && isModelName(v))) || !W.isPlain(v)) runtime[k] = v;
|
|
@@ -251,7 +280,7 @@ export class NPCManager {
|
|
|
251
280
|
this._runtime.set(id, runtime);
|
|
252
281
|
const hpd = e.calendar?.hoursPerDay ?? 24;
|
|
253
282
|
// at may be 'HH:MM' or 0-1
|
|
254
|
-
const sched = schedule.map(s => { const [x, z] = xzOf(s.position); return { at: parseClock(s.at, hpd), x, z }; }).sort((a, b) => a.at - b.at);
|
|
283
|
+
const sched = schedule.map(s => { const [x, z] = xzOf(s.position), y = yOf(s.position); return { at: parseClock(s.at, hpd), x, ...(y !== undefined && { y }), z }; }).sort((a, b) => a.at - b.at);
|
|
255
284
|
if (io && !e.interactions) warnOnce(fmt(STRINGS.noInteract, { name }));
|
|
256
285
|
// the record keeps plain values only: a function label stays in runtime (above), the record gets null and the view reads runtime first
|
|
257
286
|
const interact = io && { label: typeof io.label === 'string' ? io.label : null, op: io.op, args: io.args ?? {}, radius: io.radius ?? 3.2 };
|
|
@@ -263,13 +292,16 @@ export class NPCManager {
|
|
|
263
292
|
const patch = {};
|
|
264
293
|
const keys = ['name', 'interact', 'pathStep', 'repath', 'arriveDistance', 'speed', 'zone', 'schedule'];
|
|
265
294
|
for (const k of keys) if (opts[k] !== undefined) patch[k] = k === 'schedule' ? sched : def[k];
|
|
266
|
-
|
|
295
|
+
// a new behavior starts a new memory, as setBehavior does; naming the one it has is setup code running again, and the memory is state
|
|
296
|
+
const changes = opts.behavior !== undefined && behavior !== old.behavior;
|
|
297
|
+
if (changes) { patch.behavior = behavior; patch.nav = { ...old.nav, path: null, repath: 0 }; }
|
|
298
|
+
if (changes || opts.mind !== undefined) patch.mind = mind;
|
|
267
299
|
if (Object.keys(look).length) patch.look = { ...old.look, ...look };
|
|
268
300
|
W.set(id, patch);
|
|
269
301
|
} else {
|
|
270
|
-
const i = stopIndex(sched, timeOfDay(e)),
|
|
302
|
+
const i = stopIndex(sched, timeOfDay(e)), at = position ?? (i != null ? sched[i] : { x: 0, z: 0 }), [x, z] = xzOf(at);
|
|
271
303
|
const nav = { stop: null, path: null, repath: 0 };
|
|
272
|
-
W.add({ id, kind: KIND, position: [x, placeAtHeight(e, `npcs.${id}`, x, z,
|
|
304
|
+
W.add({ id, kind: KIND, position: [x, placeAtHeight(e, `npcs.${id}`, x, z, yOf(at), 0, 0).y, z], ...def, state: 'idle', busy: false, nav, motion: null }, { initial });
|
|
273
305
|
}
|
|
274
306
|
return this._handle(id);
|
|
275
307
|
}
|
|
@@ -47,7 +47,7 @@ export function loadAssetModel(ch, model, use) {
|
|
|
47
47
|
if (args.length) throw codedError('character-option', `Character model ${JSON.stringify(model)}: arguments work on a generator that builds synchronously, and "${name}" does not`);
|
|
48
48
|
const fail = why => warnOnce(fmt(STRINGS.failed, { name, why, list }));
|
|
49
49
|
const swap = m => {
|
|
50
|
-
if (ch.
|
|
50
|
+
if (ch.disposed || e.disposed) { e.remove(m); return; } // removed before loading finished: never reaches the scene, the copy is released
|
|
51
51
|
// a registered file that failed resolves to the assets gray box: the character's own placeholder body is the better stand-in
|
|
52
52
|
if (m.name === 'placeholder' && e.loading.get(name, 'model')?.phase === 'failed') { e.remove(m); fail('load failed'); return; }
|
|
53
53
|
const rig = ch._rig; // drop the placeholder body and the joint properties it put on the character
|
|
@@ -62,5 +62,5 @@ export function loadAssetModel(ch, model, use) {
|
|
|
62
62
|
const now = A.entries.get(name)?.value ? A.modelNow(name) : null; // only when loaded: modelNow hints in dev when nothing loads it
|
|
63
63
|
if (now) { swap(now); return Promise.resolve(); }
|
|
64
64
|
ch.ready = false;
|
|
65
|
-
return A.model(name).then(swap).catch(err => { if (!ch.
|
|
65
|
+
return A.model(name).then(swap).catch(err => { if (!ch.disposed) fail(err.message); }).finally(() => { ch.ready = true; });
|
|
66
66
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Grid A* pathfinding (fallback when no navmesh is installed or it is not ready yet). The one A* in the engine:
|
|
2
2
|
// engine.characters.findPath calls it with the collision module's walkability, the pathfinding worker (pathJob.js) with a snapshot of it.
|
|
3
|
-
// In games use engine.characters.findPath / findPathAsync, do not import this directly. Worker-safe:
|
|
3
|
+
// In games use engine.characters.findPath / findPathAsync, do not import this directly. Worker-safe: its one import, floor.js, has none.
|
|
4
|
+
import { sameFloor } from '../../core/scene/floor.js';
|
|
4
5
|
|
|
5
6
|
/** Default cell size from the character radius: a small character gets finer cells (never below the 1 m ground raster, never above the old 2 m); no radius = 2 */
|
|
6
7
|
const defaultStep = r => r == null ? 2 : Math.min(2, Math.max(1, r * 4));
|
|
@@ -8,61 +9,79 @@ const defaultNodes = step => Math.round(6000 * Math.max(1, 2 / step) ** 2); //
|
|
|
8
9
|
/** The step / maxNodes findPath uses for a character radius when the caller gives none (pathAsync sizes its ground raster with the same numbers) */
|
|
9
10
|
export const astarDefaults = radius => { const step = defaultStep(radius); return { step, maxNodes: defaultNodes(step) }; };
|
|
10
11
|
|
|
12
|
+
const sameHeight = (x, z, y) => y;
|
|
13
|
+
|
|
11
14
|
class MinHeap {
|
|
12
15
|
constructor() { this.a = []; }
|
|
13
|
-
push(n, f, g) { const a = this.a;
|
|
14
|
-
pop() { const a = this.a, top = a[0], last = a.pop(); if (a.length) { a[0] = last; let i = 0; for (;;) { const l = i * 2 + 1, r = l + 1; let m = i; if (l < a.length && a[l][0] < a[m][0]) m = l; if (r < a.length && a[r][0] < a[m][0]) m = r; if (m === i) break; [a[m], a[i]] = [a[i], a[m]]; i = m; } } return top
|
|
16
|
+
push(n, f, g) { const a = this.a; a.push([f, n, g]); let i = a.length - 1; while (i > 0) { const p = (i - 1) >> 1; if (a[p][0] <= a[i][0]) break; [a[p], a[i]] = [a[i], a[p]]; i = p; } }
|
|
17
|
+
pop() { const a = this.a, top = a[0], last = a.pop(); if (a.length) { a[0] = last; let i = 0; for (;;) { const l = i * 2 + 1, r = l + 1; let m = i; if (l < a.length && a[l][0] < a[m][0]) m = l; if (r < a.length && a[r][0] < a[m][0]) m = r; if (m === i) break; [a[m], a[i]] = [a[i], a[m]]; i = m; } } return top; }
|
|
15
18
|
get size() { return this.a.length; }
|
|
16
19
|
}
|
|
17
20
|
|
|
18
21
|
/**
|
|
19
|
-
* Grid A*: cells of step meters, searches at most maxNodes cells; returns [{ x, z }...] or null if no path.
|
|
20
|
-
*
|
|
22
|
+
* Grid A*: cells of step meters, searches at most maxNodes cells; returns [{ x, y?, z }...] or null if no path.
|
|
23
|
+
* Height rides along: a node is a cell and a floor, the one a walker coming from the cell before ends up on, so a walker downstairs is measured against downstairs and a deck
|
|
24
|
+
* passed under is still reached by the ramp up to it. Floors are told apart by the exact height floor() answers: every way onto one layer gets the same number.
|
|
25
|
+
* shortcut: a floor change here is walking up a slope, a step at a time; a stair steeper than a step per cell, a ladder or a jump needs nav, or the characters.path rule.
|
|
26
|
+
* @param {(x: number, z: number, y?: number) => boolean} walkable can a character stand here (radius already applied)
|
|
27
|
+
* @param {{ radius?: number, step?: number, maxNodes?: number, y?: number, goalY?: number, floor?: (x: number, z: number, y?: number) => number | undefined, body?: number }} [o] y: the floor of the start; goalY: the height of the goal, none = any floor; floor: the height a walker at y comes to at (x, z), default stays at y; body: engine.movement.height, what sameFloor measures with
|
|
21
28
|
*/
|
|
22
|
-
export function findPath(walkable, sx, sz, gx, gz, { radius, step = defaultStep(radius), maxNodes = defaultNodes(step) } = {}) {
|
|
29
|
+
export function findPath(walkable, sx, sz, gx, gz, { radius, step = defaultStep(radius), maxNodes = defaultNodes(step), y, goalY, floor = sameHeight, body = Infinity } = {}) {
|
|
23
30
|
const sample = 0.12; // smoothing samples a segment every 0.12 m: one radius apart (the old rule) let a straight leg clip a wall corner by up to 0.3 m between two samples
|
|
24
|
-
if (!walkable(gx, gz)) return null; // a target inside a wall / water is unreachable: say so instead of a path that ends in the wall
|
|
25
|
-
const key = (i, j) => i * 100003 + j;
|
|
31
|
+
if (!walkable(gx, gz, goalY ?? floor(gx, gz, y))) return null; // a target inside a wall / water is unreachable: say so instead of a path that ends in the wall
|
|
32
|
+
const key = (i, j) => i * 100003 + j, level = (a, b) => a === b || sameFloor(a, b, body);
|
|
26
33
|
const si = Math.round(sx / step), sj = Math.round(sz / step), gi = Math.round(gx / step), gj = Math.round(gz / step);
|
|
27
|
-
const open = new MinHeap(),
|
|
28
|
-
const
|
|
34
|
+
const open = new MinHeap(), cells = new Map(); // cell -> its first node; other floors of the cell hang on .next (nearly every cell has one floor)
|
|
35
|
+
const add = (k, i, j, y) => { const n = { i, j, y, g: Infinity, from: null, ok: false, next: cells.get(k) ?? null }; cells.set(k, n); return n; };
|
|
36
|
+
/** The node a walker at height y comes to in cell (i, j): walkable() runs once per floor */
|
|
37
|
+
const reach = (i, j, y) => {
|
|
38
|
+
const k = key(i, j), fy = floor(i * step, j * step, y);
|
|
39
|
+
let n = cells.get(k);
|
|
40
|
+
while (n && n.y !== fy) n = n.next;
|
|
41
|
+
if (!n) { n = add(k, i, j, fy); n.ok = walkable(i * step, j * step, fy); }
|
|
42
|
+
return n;
|
|
43
|
+
};
|
|
29
44
|
const h = (i, j) => Math.hypot(i - gi, j - gj);
|
|
30
|
-
|
|
45
|
+
const first = add(key(si, sj), si, sj, y); first.g = 0; open.push(first, h(si, sj), 0);
|
|
31
46
|
let n = 0;
|
|
32
47
|
const dirs = [[1, 0, 1], [-1, 0, 1], [0, 1, 1], [0, -1, 1], [1, 1, 1.414], [1, -1, 1.414], [-1, 1, 1.414], [-1, -1, 1.414]];
|
|
33
48
|
while (open.size && n++ < maxNodes) {
|
|
34
|
-
const [
|
|
35
|
-
if (at > g
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
49
|
+
const [, s, at] = open.pop();
|
|
50
|
+
if (at > s.g) { n--; continue; } // a cheaper way here was found after this entry went in: expanding it would change nothing
|
|
51
|
+
// under the goal, on another floor: not there yet, the way up may be further on
|
|
52
|
+
if (s.i === gi && s.j === gj && (goalY === undefined || s.y === undefined || level(floor(gx, gz, s.y), floor(gx, gz, goalY)))) {
|
|
53
|
+
const path = [];
|
|
54
|
+
for (let c = s; c; c = c.from) path.push({ x: c.i * step, y: c.y, z: c.j * step });
|
|
55
|
+
path.reverse(); path[0] = { x: sx, y, z: sz }; path[path.length - 1] = { x: gx, y: s.y, z: gz }; // real start, not the snapped cell centre (which may even lie behind a wall)
|
|
56
|
+
const out = simplify(walkable, floor, level, path, sample);
|
|
57
|
+
out[out.length - 1] = { x: gx, z: gz }; // the goal's height is the caller's
|
|
58
|
+
return out;
|
|
41
59
|
}
|
|
42
|
-
const gc = g.get(key(i, j));
|
|
43
60
|
for (const [di, dj, c] of dirs) {
|
|
44
|
-
const ni = i + di, nj = j + dj;
|
|
45
|
-
if (!ok
|
|
46
|
-
const
|
|
47
|
-
if (ng <
|
|
61
|
+
const ni = s.i + di, nj = s.j + dj, t = reach(ni, nj, s.y);
|
|
62
|
+
if (!t.ok && !(ni === gi && nj === gj)) continue;
|
|
63
|
+
const ng = s.g + c;
|
|
64
|
+
if (ng < t.g) { t.g = ng; t.from = s; open.push(t, ng + h(ni, nj), ng); }
|
|
48
65
|
}
|
|
49
66
|
}
|
|
50
67
|
return null;
|
|
51
68
|
}
|
|
52
69
|
// drop redundant corners: go straight when two points are directly walkable
|
|
53
|
-
function simplify(walkable, path, sample) {
|
|
70
|
+
function simplify(walkable, floor, level, path, sample) {
|
|
54
71
|
if (path.length < 3) return path;
|
|
55
72
|
const out = [path[0]];
|
|
56
73
|
let a = 0;
|
|
57
74
|
while (a < path.length - 1) {
|
|
58
75
|
let b = path.length - 1;
|
|
59
|
-
while (b > a + 1 && !lineClear(walkable, path[a], path[b], sample)) b--;
|
|
76
|
+
while (b > a + 1 && !lineClear(walkable, floor, level, path[a], path[b], sample)) b--;
|
|
60
77
|
out.push(path[b]); a = b;
|
|
61
78
|
}
|
|
62
79
|
return out;
|
|
63
80
|
}
|
|
64
|
-
|
|
81
|
+
/** Can a walker go straight from p to q, and does it come out on q's floor: under a deck the line is clear, and ends below the point it was for */
|
|
82
|
+
function lineClear(walkable, floor, level, p, q, sample) {
|
|
65
83
|
const d = Math.hypot(q.x - p.x, q.z - p.z), n = Math.ceil(d / sample);
|
|
66
|
-
|
|
67
|
-
return
|
|
84
|
+
let y = p.y;
|
|
85
|
+
for (let i = 1; i < n; i++) { const t = i / n, x = p.x + (q.x - p.x) * t, z = p.z + (q.z - p.z) * t; y = floor(x, z, y); if (!walkable(x, z, y)) return false; }
|
|
86
|
+
return level(floor(q.x, q.z, y), q.y);
|
|
68
87
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// What one live character must be (an object contract: shared check @threedog/tools/contract, runContract sees kind: 'object'). Other modules hold it through
|
|
2
|
+
// engine.player and engine.control.characterOf(id), and a replacement of the characters slot answers both, so this list is what makes that slot replaceable.
|
|
3
|
+
// How a provider answers: engine.control.resolveWith(id => liveCharacter | null) at install, and resolveWith(() => null) at dispose. Core names no module, so this is the
|
|
4
|
+
// one way in, and only a provider calls it: not a game call, not on the public pages. The module contract (contract.js, `resolves`) checks it was called.
|
|
5
|
+
// The world protocol, what world and its thing source ask of a record's character. Games never call these; a replacement gives them all (design.md section 9):
|
|
6
|
+
// placeRecord(point) a write to its record's position that differs from where it stands: a placement
|
|
7
|
+
// takePose({ rot, scale }) a write to its record's rot and scale; throws what poseProblem says
|
|
8
|
+
// poseProblem({ rot, scale }) why it cannot keep that pose, or null: thing.set answers with it, because a throw inside an op freezes gameplay
|
|
9
|
+
// seatProblem({ vehicle, seat }) why it cannot go back to that seat, or null: an undo of a placement that ended a ride asks. Without seats, always a string
|
|
10
|
+
// backInSeat({ vehicle, seat }) sit there again; seatProblem said it can
|
|
11
|
+
// vehicle, seat, facing what it rides (null on foot), the seat it holds, its turn about Y: the undo of a placement keeps them
|
|
12
|
+
// bodyHeld: something other than a holder has the body (a ragdoll, a seat), so a cutscene path leaves it alone; control reads it.
|
|
13
|
+
// A ragdoll (rigid) lays the body down and hands it back: limp (the ragdoll that has the body, or null), velocity, vy, moveAmount, stateMark (the state last told to the animation), mover, syncRecord.
|
|
14
|
+
// The rest is what callers outside the characters folder use: move (through its holder, engine.control), placeAt, enter, exit, anim, walkTo, disposed, cameraFollow.
|
|
15
|
+
export default {
|
|
16
|
+
name: 'character',
|
|
17
|
+
kind: 'object',
|
|
18
|
+
api: ['move', 'placeAt', 'enter', 'exit', 'walkTo', 'placeRecord', 'takePose', 'poseProblem', 'seatProblem', 'backInSeat', 'syncRecord'],
|
|
19
|
+
fields: ['id', 'root', 'position', 'state', 'anim', 'channel', 'vehicle', 'seat', 'facing', 'disposed', 'cameraFollow', 'limp', 'bodyHeld', 'velocity', 'vy', 'moveAmount', 'stateMark', 'mover'],
|
|
20
|
+
calls: [['poseProblem', {}]],
|
|
21
|
+
};
|
|
@@ -1,11 +1,25 @@
|
|
|
1
1
|
// What any characters module must provide (fields: see gameplay/inventory.contract.js; shared check: @threedog/tools/contract).
|
|
2
2
|
// The player is the world record 'player' (saved with world, not under a key of its own); jumps, landings, launches and falls are events other
|
|
3
|
-
// modules listen to, fired by the character itself (not exercised by the shared check: they need a player on ground).
|
|
3
|
+
// modules listen to, fired by the character itself (not exercised by the shared check: they need a player on ground). kit is what a game's mover calls (mover.contract.js).
|
|
4
|
+
// What createPlayer and control.characterOf hand out is character.contract.js: every member other modules use, the world protocol among them.
|
|
5
|
+
// position: who may write a character's position (design.md section 9). The record follows the character, so a write to the record of a live character is a placeAt (world.move).
|
|
4
6
|
export default {
|
|
5
7
|
name: 'characters',
|
|
6
|
-
api: ['createPlayer', 'removePlayer', 'findSpawn', 'findPath', 'findPathAsync', 'support', 'moveInput', 'carryRiders'],
|
|
8
|
+
api: ['createPlayer', 'removePlayer', 'findSpawn', 'findPath', 'findPathAsync', 'support', 'moveInput', 'carryRiders', 'addVehicle'],
|
|
9
|
+
fields: ['kit'],
|
|
10
|
+
resolves: true, // calls engine.control.resolveWith; what it answers with is character.contract.js
|
|
11
|
+
position: {
|
|
12
|
+
authority: 'solver',
|
|
13
|
+
writers: {
|
|
14
|
+
holder: 'the holder of the character\'s engine.control slot, through its mover; a step that returns false placed it itself and calls syncRecord',
|
|
15
|
+
vehicle: 'a seat places its rider while it rides (vehicle.contract.js); a placeAt ends the ride, so a seat never takes a placement back',
|
|
16
|
+
physics: 'a hold on the physics layer lays it where the body lies (a ragdoll); a placeAt carries that body along',
|
|
17
|
+
placeAt: 'character.placeAt or npc.placeAt: a placement, by anyone; world.move, world.set and thing.set on its record arrive here',
|
|
18
|
+
load: 'a save puts it back',
|
|
19
|
+
},
|
|
20
|
+
},
|
|
7
21
|
ops: [],
|
|
8
|
-
events: ['character:jump', 'character:step', 'character:land', 'character:launch', 'character:fall'],
|
|
22
|
+
events: ['character:jump', 'character:step', 'character:land', 'character:launch', 'character:fall', 'link:enter', 'link:exit'],
|
|
9
23
|
saves: {},
|
|
10
24
|
describe: true,
|
|
11
25
|
options: { npcs: false },
|