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.
Files changed (372) hide show
  1. package/CHANGELOG.md +219 -4
  2. package/README.md +6 -6
  3. package/core/assets/Animator.js +2 -0
  4. package/core/assets/Assets.js +44 -13
  5. package/core/assets/assetEntry.js +87 -22
  6. package/core/assets/assetHot.js +2 -1
  7. package/core/assets/assetReport.js +1 -2
  8. package/core/audio/Audio.js +24 -5
  9. package/core/base/I18n.js +6 -3
  10. package/core/base/Random.js +3 -1
  11. package/core/base/Vars.js +14 -4
  12. package/core/base/errorCodes.js +54 -16
  13. package/core/base/errorHints.js +12 -1
  14. package/core/base/errors.js +6 -4
  15. package/core/base/events.generated.js +10 -0
  16. package/core/base/follows.js +26 -0
  17. package/core/base/math.js +15 -8
  18. package/core/base/params.js +2 -2
  19. package/core/{saves → base}/saveValues.js +24 -6
  20. package/core/base/sections.js +97 -0
  21. package/core/base/spelling.js +32 -0
  22. package/core/base/validate.js +8 -29
  23. package/core/engine/Engine.js +61 -18
  24. package/core/engine/Loading.js +30 -3
  25. package/core/engine/copies.js +1 -1
  26. package/core/engine/headlessLoad.js +30 -13
  27. package/core/engine/lifecycle.js +30 -25
  28. package/core/engine/loop.js +22 -4
  29. package/core/engine/moduleSlots.js +91 -15
  30. package/core/engine/moduleTypes.js +13 -0
  31. package/core/engine/modules.js +248 -62
  32. package/core/engine/page.js +25 -8
  33. package/core/engine/plugins.js +15 -3
  34. package/core/engine/services.js +3 -0
  35. package/core/engine/startup.js +46 -36
  36. package/core/input/Input.js +39 -19
  37. package/core/input/inputKeyboard.js +39 -0
  38. package/core/input/inputPad.js +23 -2
  39. package/core/input/inputPointer.js +3 -1
  40. package/core/input/inputSeat.js +66 -0
  41. package/core/input/inputTouch.js +23 -6
  42. package/core/inspect/DebugOverrides.js +13 -2
  43. package/core/inspect/Describe.js +10 -6
  44. package/core/inspect/Inspect.js +31 -4
  45. package/core/inspect/UrlState.js +14 -10
  46. package/core/inspect/catalog.js +12 -5
  47. package/core/inspect/describeRender.js +3 -2
  48. package/core/inspect/describeText.js +6 -2
  49. package/core/inspect/metadata.generated.js +4140 -0
  50. package/core/inspect/pictureWriters.js +117 -33
  51. package/core/inspect/toolDoor.js +21 -0
  52. package/core/inspect/valueHistory.js +35 -0
  53. package/core/ops/Ops.js +32 -7
  54. package/core/ops/control.js +48 -23
  55. package/core/ops/holds.js +4 -3
  56. package/core/ops/opsAtomic.js +18 -6
  57. package/core/ops/opsCheck.js +7 -5
  58. package/core/ops/opsFail.js +5 -7
  59. package/core/ops/opsRun.js +2 -2
  60. package/core/ops/owned.js +20 -3
  61. package/core/ops/sharedPoint.js +7 -10
  62. package/core/ops/stacks.js +22 -1
  63. package/core/saves/SaveStore.js +52 -26
  64. package/core/saves/Saves.js +122 -27
  65. package/core/saves/saveApply.js +159 -61
  66. package/core/saves/saveFiles.js +30 -25
  67. package/core/saves/saveVersions.js +20 -6
  68. package/core/saves/session.js +76 -33
  69. package/core/saves/stateReplace.js +184 -0
  70. package/core/scene/Rays.js +21 -5
  71. package/core/scene/Views.js +6 -6
  72. package/core/scene/adaptive.js +13 -10
  73. package/core/scene/canvasBox.js +2 -0
  74. package/core/scene/compositeGround.js +72 -0
  75. package/core/scene/floor.js +17 -0
  76. package/core/scene/grid.js +2 -0
  77. package/core/scene/ground.contract.js +14 -0
  78. package/core/scene/ground.js +52 -17
  79. package/core/scene/pixel.js +154 -0
  80. package/core/scene/pixelRatio.js +167 -0
  81. package/core/scene/sceneDelta.js +3 -3
  82. package/core/scene/sceneMethods.js +11 -6
  83. package/core/scene/stepPoses.js +6 -1
  84. package/core/scene/thingExplain.js +1 -1
  85. package/core/scene/thingOps.js +22 -14
  86. package/core/scene/things.js +1 -1
  87. package/core/scene/tier.js +10 -0
  88. package/core/scene/view.js +126 -9
  89. package/core/stream/Batching.js +0 -1
  90. package/core/stream/Stream.js +24 -12
  91. package/core/stream/batchGroups.js +1 -0
  92. package/core/stream/scatter.js +5 -2
  93. package/core/stream/streamSources.js +2 -1
  94. package/core/threads/Commits.js +12 -7
  95. package/core/threads/Jobs.js +15 -5
  96. package/core/threads/Threads.js +5 -2
  97. package/core/time/Clock.js +3 -1
  98. package/core/time/skip.js +10 -4
  99. package/core/time/time.js +16 -10
  100. package/docs/AI.md +89 -72
  101. package/docs/API.md +18 -14
  102. package/docs/CODEMAP.md +68 -40
  103. package/docs/COMMANDS.md +22 -23
  104. package/docs/CONVENTIONS.md +120 -17
  105. package/docs/MIGRATING-0.8.md +2 -0
  106. package/docs/MIGRATING-1.0.md +101 -0
  107. package/docs/THREE.md +65 -12
  108. package/docs/api/cameraRig.md +3 -0
  109. package/docs/api/characters.md +13 -7
  110. package/docs/api/collider3d.md +9 -0
  111. package/docs/api/content.md +16 -15
  112. package/docs/api/dialogue.md +2 -1
  113. package/docs/api/engine.md +18 -16
  114. package/docs/api/events.md +14 -4
  115. package/docs/api/ground.md +2 -2
  116. package/docs/api/i18n.md +1 -1
  117. package/docs/api/input.md +2 -1
  118. package/docs/api/interactions.md +1 -1
  119. package/docs/api/inventory.md +1 -1
  120. package/docs/api/level.md +3 -2
  121. package/docs/api/nav.md +4 -2
  122. package/docs/api/particles.md +8 -5
  123. package/docs/api/pickups.md +1 -1
  124. package/docs/api/post.md +3 -4
  125. package/docs/api/quests.md +4 -2
  126. package/docs/api/rays.md +2 -0
  127. package/docs/api/rigid.md +3 -2
  128. package/docs/api/saves.md +8 -7
  129. package/docs/api/stream.md +4 -5
  130. package/docs/api/terrain.md +3 -4
  131. package/docs/api/terrainDraw.md +1 -1
  132. package/docs/api/timeline.md +2 -2
  133. package/docs/api/tools.md +3 -1
  134. package/docs/api/triggers.md +2 -0
  135. package/docs/api/vegetationDraw.md +1 -1
  136. package/docs/api/water.md +6 -2
  137. package/docs/api/weather.md +19 -4
  138. package/docs/api/weatherDraw.md +1 -1
  139. package/docs/api/world.md +1 -1
  140. package/docs/recipes/01-new-game.md +3 -3
  141. package/docs/recipes/02-npc.md +3 -4
  142. package/docs/recipes/03-items-pickups.md +11 -11
  143. package/docs/recipes/04-quests.md +2 -0
  144. package/docs/recipes/05-props-bake.md +9 -9
  145. package/docs/recipes/06-clock-events.md +6 -6
  146. package/docs/recipes/07-flow-scenes-pause.md +18 -15
  147. package/docs/recipes/08-weather-sky.md +5 -5
  148. package/docs/recipes/09-materials-lights-models.md +3 -3
  149. package/docs/recipes/10-blender-level.md +16 -4
  150. package/docs/recipes/11-vehicles.md +18 -6
  151. package/docs/recipes/12-navmesh.md +52 -8
  152. package/docs/recipes/13-animation.md +34 -8
  153. package/docs/recipes/14-custom-module.md +20 -8
  154. package/docs/recipes/15-perf.md +4 -4
  155. package/docs/recipes/16-dev-bridge.md +9 -8
  156. package/docs/recipes/18-world.md +13 -5
  157. package/docs/recipes/19-shooting-picking.md +3 -2
  158. package/docs/recipes/20-content-actions-save.md +26 -6
  159. package/docs/recipes/21-camera.md +17 -23
  160. package/docs/recipes/22-shop-gold.md +21 -12
  161. package/docs/recipes/23-ops.md +19 -15
  162. package/docs/recipes/26-ui-parts.md +3 -2
  163. package/docs/recipes/27-net.md +3 -3
  164. package/docs/recipes/28-terrain.md +3 -3
  165. package/docs/recipes/29-grid.md +1 -1
  166. package/docs/recipes/30-templates.md +10 -13
  167. package/docs/recipes/31-audio.md +2 -2
  168. package/docs/recipes/32-action-game.md +6 -5
  169. package/docs/recipes/33-stream-sources.md +4 -1
  170. package/docs/recipes/34-environment-lighting.md +7 -8
  171. package/docs/recipes/35-picture-from-reference.md +13 -9
  172. package/docs/recipes/36-ground-plants.md +3 -2
  173. package/docs/recipes/40-scene.md +11 -10
  174. package/docs/recipes/43-shader-patches-surfaces.md +7 -7
  175. package/docs/recipes/44-water-weather-hooks.md +9 -8
  176. package/docs/recipes/45-cascaded-shadows.md +3 -4
  177. package/docs/recipes/46-blender-art-route.md +3 -3
  178. package/docs/recipes/49-game-module-contract.md +2 -1
  179. package/docs/recipes/50-own-renderer.md +1 -0
  180. package/docs/recipes/51-capability-catalog.md +14 -8
  181. package/docs/recipes/52-around-the-picture.md +4 -4
  182. package/docs/recipes/55-mods-time-wrap.md +5 -4
  183. package/docs/recipes/56-webgpu-tsl.md +12 -1
  184. package/docs/recipes/57-control.md +30 -10
  185. package/docs/recipes/58-input.md +13 -0
  186. package/docs/recipes/60-judge-the-picture.md +1 -1
  187. package/docs/recipes/62-picture-code.md +106 -48
  188. package/docs/recipes/63-third-person-start.md +2 -1
  189. package/docs/recipes/64-particles.md +53 -4
  190. package/docs/recipes/65-swarm.md +2 -2
  191. package/docs/recipes/66-common-tasks.md +313 -0
  192. package/examples/action/.threedog/content-index.json +1 -1
  193. package/examples/action/GAMEMAP.md +3 -3
  194. package/examples/perf-island/.threedog/content-index.json +1 -1
  195. package/examples/perf-island/GAMEMAP.md +3 -3
  196. package/examples/perf-island/main.js +2 -2
  197. package/examples/plain-picture/GAMEMAP.md +3 -1
  198. package/examples/rc2d/GAMEMAP.md +1 -1
  199. package/examples/rc2d/rc2d.js +1 -1
  200. package/examples/styletest/.threedog/content-index.json +1 -1
  201. package/examples/styletest/GAMEMAP.md +3 -3
  202. package/examples/webgpu-tsl/GAMEMAP.md +1 -1
  203. package/examples/world/.threedog/content-index.json +1 -1
  204. package/examples/world/GAMEMAP.md +5 -4
  205. package/examples/world/main.js +3 -3
  206. package/index.d.ts +8 -5
  207. package/index.js +22 -1
  208. package/llms.txt +2 -1
  209. package/modules/ambience/Ambience.js +5 -3
  210. package/modules/calendar/index.js +7 -5
  211. package/modules/camera/FirstPersonCamera.js +3 -3
  212. package/modules/camera/FixedCamera.js +34 -20
  213. package/modules/camera/FollowCamera.js +20 -11
  214. package/modules/camera/RtsCamera.js +1 -0
  215. package/modules/camera/common.js +33 -22
  216. package/modules/camera/pixelSnap.js +62 -0
  217. package/modules/characters/Character.js +136 -43
  218. package/modules/characters/NPC.js +60 -28
  219. package/modules/characters/assetModel.js +2 -2
  220. package/modules/characters/astar.js +47 -28
  221. package/modules/characters/character.contract.js +21 -0
  222. package/modules/characters/contract.js +17 -3
  223. package/modules/characters/index.js +94 -34
  224. package/modules/characters/moveHelpers.js +80 -15
  225. package/modules/characters/mover.contract.js +14 -0
  226. package/modules/characters/movers/common.js +6 -4
  227. package/modules/characters/movers/rapier.js +12 -5
  228. package/modules/characters/movers/simple.js +25 -12
  229. package/modules/characters/movers/support.js +18 -7
  230. package/modules/characters/npcAvoid.js +15 -12
  231. package/modules/characters/npcSim.js +75 -25
  232. package/modules/characters/npcView.js +13 -6
  233. package/modules/characters/pathAsync.js +74 -24
  234. package/modules/characters/pathJob.js +38 -21
  235. package/modules/characters/player.js +59 -47
  236. package/modules/characters/seat.js +56 -0
  237. package/modules/characters/vehicle.contract.js +8 -5
  238. package/modules/characters/walkTo.js +31 -18
  239. package/modules/collision/Collision.js +44 -26
  240. package/modules/collision/contract.js +2 -2
  241. package/modules/collision/grid.js +6 -1
  242. package/modules/content/explain.js +1 -1
  243. package/modules/content/fields.js +3 -3
  244. package/modules/content/ids.js +3 -1
  245. package/modules/content/index.js +83 -27
  246. package/modules/content/loader.js +110 -130
  247. package/modules/content/markers.js +1 -1
  248. package/modules/content/models.js +48 -14
  249. package/modules/content/packs.js +2 -1
  250. package/modules/content/places.js +5 -4
  251. package/modules/content/props.js +8 -6
  252. package/modules/content/rules.js +29 -47
  253. package/modules/content/templates.js +66 -35
  254. package/modules/content/words.js +19 -6
  255. package/modules/debug/DebugPanel.js +4 -2
  256. package/modules/flow/contract.js +1 -0
  257. package/modules/flow/index.js +24 -14
  258. package/modules/gameplay/Container.js +5 -2
  259. package/modules/gameplay/Containers.js +9 -1
  260. package/modules/gameplay/Dialogue.js +82 -25
  261. package/modules/gameplay/Interactions.js +61 -32
  262. package/modules/gameplay/Inventory.js +56 -13
  263. package/modules/gameplay/Quests.js +133 -24
  264. package/modules/gameplay/boxOps.js +7 -0
  265. package/modules/gameplay/dialogueBubbles.js +14 -9
  266. package/modules/gameplay/inventoryOps.js +11 -7
  267. package/modules/gameplay/pickups.js +43 -16
  268. package/modules/gameplay/questLog.js +4 -2
  269. package/modules/gameplay/questSave.js +6 -1
  270. package/modules/gameplay/quests.contract.js +1 -1
  271. package/modules/grid/Grid.js +1 -1
  272. package/modules/grid/Layer.js +8 -4
  273. package/modules/grid/index.js +21 -15
  274. package/modules/grid/ops.js +43 -46
  275. package/modules/grid/tiles.js +1 -1
  276. package/modules/hlod/index.js +6 -3
  277. package/modules/hud/index.js +6 -4
  278. package/modules/hud/parts.js +4 -4
  279. package/modules/level/contract.js +2 -2
  280. package/modules/level/heightfield.js +17 -21
  281. package/modules/level/index.js +83 -26
  282. package/modules/level/load.js +165 -48
  283. package/modules/level/nodes.js +34 -16
  284. package/modules/lights/index.js +6 -3
  285. package/modules/lights/sources.js +3 -1
  286. package/modules/music/Music.js +4 -2
  287. package/modules/nav/contract.js +7 -5
  288. package/modules/nav/index.js +174 -44
  289. package/modules/nav/navPath.js +45 -10
  290. package/modules/net/index.js +1 -1
  291. package/modules/particles/Particles.js +85 -22
  292. package/modules/particles/effect.js +80 -28
  293. package/modules/particles/shaders.js +17 -0
  294. package/modules/photo/Photo.js +12 -10
  295. package/modules/rigid/RigidBodies.js +200 -57
  296. package/modules/rigid/Vehicles.js +15 -5
  297. package/modules/rigid/collider3d.contract.js +26 -0
  298. package/modules/rigid/ragdoll.js +55 -44
  299. package/modules/rigid/ragdollBones.js +83 -33
  300. package/modules/rigid/refresh.js +10 -0
  301. package/modules/rigid/sync.js +46 -15
  302. package/modules/settings/Settings.js +84 -37
  303. package/modules/settings/apply.js +15 -8
  304. package/modules/settings/sections.js +7 -4
  305. package/modules/settings/strings.js +2 -2
  306. package/modules/sky/skyModule.js +3 -1
  307. package/modules/swarm/Swarm.js +16 -6
  308. package/modules/terrain/ChunkManager.js +10 -16
  309. package/modules/terrain/index.js +17 -11
  310. package/modules/terrain/terrainEdit.js +13 -2
  311. package/modules/terrain/terrainGen.js +5 -3
  312. package/modules/terrain/terrainShape.js +1 -1
  313. package/modules/timeline/Timeline.js +58 -26
  314. package/modules/timeline/Tween.js +19 -13
  315. package/modules/timeline/shots.js +19 -10
  316. package/modules/triggers/index.js +87 -20
  317. package/modules/vegetation/Vegetation.js +4 -3
  318. package/modules/vegetation/draw.js +16 -0
  319. package/modules/vegetation/groundChunks.js +10 -7
  320. package/modules/vegetation/index.js +30 -7
  321. package/modules/water/Water.js +6 -2
  322. package/modules/weather/Weather.js +16 -15
  323. package/modules/weather/weatherDraw.js +7 -6
  324. package/modules/world/World.js +20 -7
  325. package/modules/world/explain.js +1 -1
  326. package/modules/world/index.js +13 -6
  327. package/modules/world/persist.js +23 -8
  328. package/modules/world/pos.js +4 -4
  329. package/modules/world/records.js +13 -7
  330. package/modules/world/sim.js +74 -23
  331. package/modules/world/simFar.js +2 -2
  332. package/modules/world/state.js +31 -6
  333. package/modules/world/thingSource.js +16 -3
  334. package/modules/world/views.js +19 -8
  335. package/package.json +7 -1
  336. package/render/PostFX.js +47 -29
  337. package/render/backend.js +5 -1
  338. package/render/fx/particles.tsl.js +8 -6
  339. package/render/fx/postWebGPU.tsl.js +3 -3
  340. package/render/layers.js +1 -1
  341. package/render/lightmap.js +23 -3
  342. package/render/occlusion.js +43 -12
  343. package/render/patches.js +2 -2
  344. package/render/postWebGL.js +4 -3
  345. package/render/probes.js +25 -0
  346. package/render/shadows.js +2 -2
  347. package/types/core.d.ts +142 -55
  348. package/types/gameplay.d.ts +92 -40
  349. package/types/generated/core/base/I18n.d.ts +2 -2
  350. package/types/generated/core/input/Input.d.ts +16 -2
  351. package/types/generated/core/input/inputSeat.d.ts +26 -0
  352. package/types/generated/core/inspect/DebugOverrides.d.ts +15 -1
  353. package/types/generated/core/ops/Ops.d.ts +2 -1
  354. package/types/generated/core/ops/control.d.ts +12 -2
  355. package/types/generated/core/saves/Saves.d.ts +26 -6
  356. package/types/generated/core/saves/session.d.ts +1 -1
  357. package/types/generated/core/scene/Views.d.ts +1 -1
  358. package/types/generated/core/stream/Stream.d.ts +6 -4
  359. package/types/generated/modules/grid/Grid.d.ts +1 -0
  360. package/types/generated/modules/particles/Particles.d.ts +4 -3
  361. package/types/generated/modules/timeline/Timeline.d.ts +5 -4
  362. package/types/generated/modules/weather/Weather.d.ts +1 -7
  363. package/types/generated/modules/world/World.d.ts +6 -4
  364. package/types/generated/render/PostFX.d.ts +6 -14
  365. package/types/nav.d.ts +36 -4
  366. package/types/physics.d.ts +55 -7
  367. package/types/pixel.d.ts +75 -0
  368. package/types/world.d.ts +14 -8
  369. package/ui/Parts.js +9 -2
  370. package/ui/theme.js +2 -2
  371. package/core/scene/engineCamera.js +0 -32
  372. package/modules/camera/projection.js +0 -21
@@ -7,12 +7,17 @@ import { DEV, fmt } from '../../core/base/validate.js';
7
7
  import { readParams, keysSchema } from '../../core/base/params.js';
8
8
  import { xzOf } from '../../core/base/math.js';
9
9
  import { codedError } from '../../core/base/errorHints.js';
10
+ import { isPlainObject } from '../../core/base/util.js';
11
+ import { refuseSave, saveNumberOk } from '../../core/base/saveValues.js';
10
12
  import { unkey } from './terrainGen.js';
11
13
 
12
14
  // grid points past its edge that a chunk reads: a shared point, then the neighbor smooth normals and the color map's border read
13
15
  const REACH = 2;
14
16
  const EDIT_KEYS = ['radius', 'shape', 'raise', 'lower', 'flatten', 'paint'];
17
+ /** Grid points one edit may cover: far past any brush, still a moment's work */
18
+ const MAX_EDIT_POINTS = 1 << 22;
15
19
  const STRINGS = {
20
+ big: 'terrain.edit(): radius {r} covers more ground than one edit can hold; at most {max} here. Split a large change into several edits',
16
21
  none: "terrain.edit(): pass at least one of raise, lower, flatten or paint, e.g. engine.terrain.edit(pos, { radius: 2, lower: 1 })",
17
22
  many: 'terrain.edit(): raise, lower and flatten all set the height; pass only one of them',
18
23
  shape: "terrain.edit(): shape must be 'circle' or 'square', got {got}",
@@ -22,13 +27,13 @@ const STRINGS = {
22
27
 
23
28
  /** @internal owned by ChunkManager (engine.terrain.edits) */
24
29
  export class TerrainEdits {
25
- constructor(engine, mgr) {
30
+ constructor(engine, mgr, ctx) {
26
31
  this.e = engine; this.mgr = mgr;
27
32
  this.h = new Map(); // grid key -> height of grid point (i * cell, j * cell)
28
33
  this.t = new Map(); // cell key -> ground type of cell [i, i + 1) x [j, j + 1)
29
34
  this.ver = new Map(); // chunk key 'cx,cz' -> edit version (jobs carry it; an older result is stale)
30
35
  this.attach();
31
- engine.saves.register('terrain', { save: () => this.save(), load: d => this.load(d) });
36
+ ctx.saves.register('terrain', { notAtomic: 'height data is too large to copy for each atomic run, and its load rebuilds meshes', save: () => this.save(), load: d => this.load(d) });
32
37
  }
33
38
  /** Point the main-thread generator at the edit maps (again after the generator is rebuilt, e.g. a height map finished loading) */
34
39
  attach() { this.mgr.generator.useEdits(this.h, this.t); }
@@ -43,6 +48,8 @@ export class TerrainEdits {
43
48
  if (o.shape != null && o.shape !== 'circle' && o.shape !== 'square') throw codedError('terrain-option', fmt(STRINGS.shape, { got: JSON.stringify(o.shape) }));
44
49
  if ('paint' in o && o.paint !== null && typeof o.paint !== 'string') throw codedError('terrain-option', fmt(STRINGS.paint, { got: JSON.stringify(o.paint) }));
45
50
  const T = this.mgr.generator, C = T.CELL, [x, z] = xzOf(position), r = o.radius ?? C / 2, sq = o.shape === 'square';
51
+ // every point covered is a Map entry kept in the save: a radius from bad data would fill memory and hang the tab before anything could refuse it
52
+ if ((2 * r / C) ** 2 > MAX_EDIT_POINTS) throw codedError('terrain-option', fmt(STRINGS.big, { r, max: Math.floor(Math.sqrt(MAX_EDIT_POINTS) * C / 2) }));
46
53
  // off the outline of terrain({ shape }) there is no ground to dig or paint; near the rim the grid points just outside still move,
47
54
  // because the rim's height is interpolated from them. The outline itself is not editable
48
55
  if (!T.has(x, z)) return { heights: 0, cells: 0 };
@@ -127,6 +134,10 @@ export class TerrainEdits {
127
134
  return { h, t };
128
135
  }
129
136
  load(d) {
137
+ const cell = (i, j) => Number.isInteger(i) && Number.isInteger(j) && saveNumberOk(i) && saveNumberOk(j);
138
+ const heights = h => h == null || (Array.isArray(h) && h.length % 3 === 0 && h.every(saveNumberOk) && h.every((v, n) => n % 3 || cell(v, h[n + 1])));
139
+ const types = t => t == null || (Array.isArray(t) && t.every(c => Array.isArray(c) && c.length === 3 && cell(c[0], c[1]) && typeof c[2] === 'string'));
140
+ if (d != null && !(isPlainObject(d) && heights(d.h) && types(d.t))) refuseSave('terrain', 'the part', 'must be { h: [i, j, height, ...], t: [[i, j, type], ...] }', d);
130
141
  const T = this.mgr.generator, before = [...this.h.keys(), ...this.t.keys()];
131
142
  this.h.clear(); this.t.clear();
132
143
  for (let n = 0; n < (d?.h?.length ?? 0); n += 3) this.h.set(T.key(d.h[n], d.h[n + 1]), d.h[n + 2]);
@@ -16,9 +16,11 @@ export { DEFAULT_TERRAIN } from './terrainDefaults.js';
16
16
  const srgbToLinear = c => (c <= 0.04045 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4));
17
17
  const hexToLinear = h => [((h >> 16) & 255) / 255, ((h >> 8) & 255) / 255, (h & 255) / 255].map(srgbToLinear);
18
18
 
19
- // grid and cell keys of the edit overlay (terrainEdit.js reads them back)
20
- const key = (i, j) => (i + 50000) * 100000 + (j + 50000);
21
- export const unkey = k => [Math.floor(k / 100000) - 50000, (k % 100000) - 50000];
19
+ // grid and cell keys of the edit overlay, the height cache and the shape cache (terrainEdit.js reads them back). Inside +-2^25 cells a key is
20
+ // one exact integer (< 2^52, so no two cells share one); farther out it is the string 'i,j'. A string never equals a number, so the two ranges cannot clash either.
21
+ const KEY_HALF = 2 ** 25, KEY_SPAN = 2 ** 26;
22
+ const key = (i, j) => (i >= -KEY_HALF && i < KEY_HALF && j >= -KEY_HALF && j < KEY_HALF) ? (i + KEY_HALF) * KEY_SPAN + (j + KEY_HALF) : i + ',' + j;
23
+ export const unkey = k => typeof k === 'string' ? k.split(',').map(Number) : [Math.floor(k / KEY_SPAN) - KEY_HALF, (k % KEY_SPAN) - KEY_HALF];
22
24
 
23
25
  export function createTerrain(userCfg = {}) {
24
26
  const cfg = { ...DEFAULT_TERRAIN, ...userCfg, colors: { ...DEFAULT_TERRAIN.colors, ...(userCfg.colors || {}) }, boundsPad: { ...DEFAULT_TERRAIN.boundsPad, ...(userCfg.boundsPad || {}) } };
@@ -24,7 +24,7 @@ export function forEdges(P, fn) {
24
24
 
25
25
  /**
26
26
  * @param {any} S the shape config (functions already source text or functions; an image mask carries data)
27
- * @param {{ CELL: number, NOISE: any, key: (i: number, j: number) => number, hash: Function, fbm: Function }} ctx
27
+ * @param {{ CELL: number, NOISE: any, key: (i: number, j: number) => number | string, hash: Function, fbm: Function }} ctx
28
28
  */
29
29
  export function makeShape(S, { CELL, NOISE, key, hash, fbm }) {
30
30
  const pure = (v, label) => (typeof v === 'function' || typeof v === 'string' ? compilePure(v, label).call : null);
@@ -25,10 +25,13 @@
25
25
  // While frozen (engine.freeze / flow 'paused') wait, tweens, camera and paths do not advance; they resume after unfreezing.
26
26
  // On skip all running tweens jump to their end and the remaining steps finish instantly (do steps still run, so state matches a normal playthrough).
27
27
  // Camera takeover: a camera step takes the camera (engine.takeCamera, see camera/common.js) and releases its claim and restores the view after the whole run; the camera
28
- // module then resumes.
28
+ // module then resumes. While a newer holder has the camera, or something above the cutscene has a path's character, the step keeps its time and writes nothing (shots.js).
29
29
  // To hand the camera back midway: { camera: { release: true } }
30
30
  // General tween (usable anywhere in a game): await engine.timeline.tween(mesh, { 'position.y': 2, 'material.opacity': 0 }, { duration: 1, ease: 'outCubic' })
31
- // Events: timeline:start, timeline:end { skipped }
31
+ // Ending: a run has one, set once: 'finished', 'skipped', 'dropped' (its state is gone: a load, a new game, the engine) or 'failed' (a step threw). play() resolves with
32
+ // { ending, error? }, timeline:end carries the same, describe counts them. Dropped and failed run nothing more and give the screen back; a run that ends in a step of its own
33
+ // choosing keeps the fade it asked for. What a run holds goes back in the one finally of play().
34
+ // Events: timeline:start, timeline:end { id, ending, error? }
32
35
  // Scenario param cutscene (engine.applyState / URL ?cutscene=skip): 'skip' skips the playing cutscene and all later ones, 'keep' restores normal behavior.
33
36
  import { Tweener } from './Tween.js';
34
37
  import { fadeScreen, uiFocus } from '../../ui/UI.js';
@@ -41,6 +44,7 @@ import { warnOnce } from '../../core/base/validate.js';
41
44
  const TWEEN_KEYS = ['duration', 'ease'];
42
45
  import { coded, codedError } from '../../core/base/errorHints.js';
43
46
  import { reportError } from '../../core/base/errors.js';
47
+ import { stateTurn } from '../../core/saves/stateReplace.js';
44
48
  /** @import { ModuleDef } from '../../types/core' */
45
49
  /** @import { Ease } from '../../types/common' */
46
50
  /** @import { TimelineStep, TimelinePlayOptions, TimelineOptions, TweenTo, TweenPromise } from '../../types/gameplay' */
@@ -72,6 +76,7 @@ export class Timeline {
72
76
  /** @internal */ this._tweener = new Tweener();
73
77
  /** @internal */ this.runs = new Set();
74
78
  /** @internal numbers the runs of this engine (timeline:start / timeline:end id) */ this._runSeq = 0;
79
+ /** @internal how runs ended, and the last failure: describe */ this._endings = { finished: 0, skipped: 0, dropped: 0, failed: 0 }; this._failure = null;
75
80
  /** Skip every cutscene immediately (set by scenario param cutscene=skip) @type {boolean} @experimental */
76
81
  this.skipAll = false;
77
82
  ctx.track(engine.input.action('skip', { context: 'cutscene', label: 'timeline.skipAction', optional: true, owner: 'timeline' })); // cancel skips anyway
@@ -87,7 +92,7 @@ export class Timeline {
87
92
 
88
93
  /**
89
94
  * General tween (the only entry point for tweens in the engine): await engine.timeline.tween(mesh, { 'position.y': 2 }, { duration: 1, ease: 'outCubic' }); duration in seconds (default 1), ease default 'inOutCubic'; tween(mesh, to, 1) throws in dev.
90
- * Returns a Promise (with finish() / remove())
95
+ * Returns a Promise (with finish() / remove()); a write that throws ends the tween and rejects it
91
96
  * @param {any} target
92
97
  * @param {TweenTo} to
93
98
  * @param {{ duration?: number, ease?: Ease }} [opts]
@@ -99,17 +104,17 @@ export class Timeline {
99
104
  }
100
105
 
101
106
  /**
102
- * Play a timeline; resolves with { skipped } when it ends
107
+ * Play a timeline; resolves with { ending, error? } when it ends: 'finished', 'skipped', 'dropped' (a load, a new game or dispose took its state) or 'failed' (a step threw; error says what)
103
108
  * @param {TimelineStep | TimelineStep[]} steps
104
109
  * @param {TimelinePlayOptions} [opts]
105
- * @returns {Promise<{ skipped: boolean }>}
110
+ * @returns {Promise<{ ending: 'finished' | 'skipped' | 'dropped' | 'failed', error?: string }>}
106
111
  */
107
112
  async play(steps, opts = {}) {
108
113
  readParams('timeline.play()', keysSchema(['skippable', 'letterbox', 'actors']), opts);
109
114
  const { skippable = true, letterbox = false } = opts, e = this.engine;
110
115
  const actors = opts.actors ?? (e.player ? [e.player] : []);
111
116
  if (!Array.isArray(actors)) throw codedError('module-params', `timeline.play(steps, { actors }): actors is a list of characters (or ids) the cutscene drives, e.g. [engine.player, guard]; [] drives nobody. Got ${typeof actors}`);
112
- const run = { id: ++this._runSeq, skippable, letterbox, skipping: false, cancels: new Set(), cam: null, actors: new Set(), ctx: null, line: false };
117
+ const run = { id: ++this._runSeq, skippable, letterbox, ending: null, error: null, cancels: new Set(), cam: null, actors: new Set(), ctx: null, line: false, faded: false, duringLoad: e.saves.replacing };
113
118
  // a bad or remote actor throws: the ones already taken go back, or the player stays in the cutscene with no run left to end it
114
119
  try { for (const a of actors) this._take(run, a); } catch (err) { this._give(run); throw err; }
115
120
  // enter the UI focus stack: skippable cutscenes skip on Esc, unskippable ones swallow Esc (so it does not open the settings menu)
@@ -118,19 +123,27 @@ export class Timeline {
118
123
  this._refreshUI();
119
124
  e.events.emit('timeline:start', { id: run.id });
120
125
  if (this.skipAll && skippable) this.skip(run);
126
+ const turn = stateTurn(e); // no load could continue a run from half way: an autosave waits for its end
121
127
  try {
122
128
  await this._step(steps, run);
129
+ run.ending ??= 'finished';
123
130
  } catch (err) {
124
131
  reportError(this.engine, err, 'timeline step', { soft: true }); // a step that threw ends this run; the game carries on
132
+ run.error = err?.message ?? String(err);
133
+ this._stop(run, 'failed'); // a parallel branch beside the throw is still waiting
125
134
  } finally {
135
+ if (run.faded && gone(run) && ![...this.runs].some(r => r !== run && r.faded)) fadeScreen(e, 0, { seconds: 0 }); // the steps that would have faded back in will not run
136
+ turn.remove();
126
137
  releaseCamera(this, run);
127
138
  run.focus.remove();
128
139
  this._give(run);
129
140
  this.runs.delete(run);
130
141
  this._refreshUI();
131
- e.events.emit('timeline:end', { id: run.id, skipped: run.skipping });
142
+ this._endings[run.ending]++;
143
+ if (run.error) this._failure = run.error;
144
+ e.events.emit('timeline:end', { id: run.id, ...answer(run) });
132
145
  }
133
- return { skipped: run.skipping };
146
+ return answer(run);
134
147
  }
135
148
 
136
149
  /**
@@ -139,12 +152,20 @@ export class Timeline {
139
152
  * @returns {void}
140
153
  */
141
154
  skip(run) {
142
- for (const r of run ? [run] : [...this.runs]) {
143
- if (r.skipping) continue;
144
- r.skipping = true;
145
- this._tweener.finishAll(r);
146
- for (const c of [...r.cancels]) c();
147
- }
155
+ for (const r of run ? [run] : [...this.runs]) if (!r.ending) this._stop(r, 'skipped');
156
+ }
157
+
158
+ /** @internal The one way a run stops early. The ending is set once; only a skip can still turn into a harder one, because it goes on running the steps left */
159
+ _stop(r, ending) {
160
+ if (r.ending && r.ending !== 'skipped') return;
161
+ r.ending = ending;
162
+ this._tweener.finishAll(r);
163
+ for (const c of [...r.cancels]) c();
164
+ }
165
+
166
+ /** @internal The state a run belongs to is gone: it ends without its remaining steps, whose effects would land in what replaced it. keep: a run a load started stays */
167
+ _drop(keep = () => false) {
168
+ for (const r of [...this.runs]) if (!keep(r)) this._stop(r, 'dropped');
148
169
  }
149
170
 
150
171
  /**
@@ -190,7 +211,7 @@ export class Timeline {
190
211
  // ---------- Steps ----------
191
212
  /** @internal */
192
213
  async _step(s, run) {
193
- if (s == null) return;
214
+ if (s == null || gone(run)) return;
194
215
  if (Array.isArray(s)) { for (const x of s) await this._step(x, run); return; }
195
216
  if (typeof s === 'function') { await s(this.engine, run); return; }
196
217
  checkStep(s);
@@ -206,10 +227,11 @@ export class Timeline {
206
227
  if ('fade' in s) { await this._fade(s.fade, s.to ?? 1, run); return; }
207
228
  }
208
229
 
209
- /** Full-screen fade: jumps to the target when skipped @internal */
230
+ /** Full-screen fade: jumps to the target when skipped. faded: the run holds the screen, so an ending that drops the later steps gives it back @internal */
210
231
  async _fade(seconds, to, run) {
211
232
  const now = () => fadeScreen(this.engine, to, { seconds: 0 });
212
- if (run.skipping) return now();
233
+ run.faded = to > 0;
234
+ if (run.ending) return now();
213
235
  run.cancels.add(now);
214
236
  await fadeScreen(this.engine, to, { seconds });
215
237
  run.cancels.delete(now);
@@ -221,7 +243,7 @@ export class Timeline {
221
243
  * @internal
222
244
  */
223
245
  _wait(run, seconds) {
224
- if (run.skipping || !(seconds > 0)) return Promise.resolve();
246
+ if (run.ending || !(seconds > 0)) return Promise.resolve();
225
247
  return new Promise(res => {
226
248
  const done = () => { h.remove(); run.cancels.delete(done); res(); };
227
249
  const h = this.engine.clock.after(seconds, done, { channel: 'real' });
@@ -229,13 +251,13 @@ export class Timeline {
229
251
  });
230
252
  }
231
253
  /** @internal */
232
- _timed(run, o) { return this._tweener.run({ ...o, duration: run.skipping ? 0 : o.duration, group: run }); }
254
+ _timed(run, o) { return this._tweener.run({ ...o, duration: run.ending ? 0 : o.duration, group: run }); }
233
255
  /** @internal */
234
- _tween(run, target, to, d, ease) { return this._tweener.tween(target, to, run.skipping ? 0 : d, ease, run); }
256
+ _tween(run, target, to, d, ease) { return this._tweener.tween(target, to, run.ending ? 0 : d, ease, run); }
235
257
 
236
258
  /** @internal */
237
259
  _waitFor(name, timeout, run) {
238
- if (run.skipping) return Promise.resolve();
260
+ if (run.ending) return Promise.resolve();
239
261
  return new Promise(res => {
240
262
  let timer = null, over = false;
241
263
  const done = () => { if (over) return; over = true; sub.remove(); timer?.remove(); run.cancels.delete(done); res(); };
@@ -249,7 +271,7 @@ export class Timeline {
249
271
  _say(s, run) {
250
272
  const e = this.engine, d = e.dialogue;
251
273
  if (!d) { warnOnce(coded('module-needs', '[timeline] the say step was skipped: it needs dialogue(); add it to engine.use(...)')); return Promise.resolve(); }
252
- if (run.skipping) return Promise.resolve();
274
+ if (run.ending) return Promise.resolve();
253
275
  // the line is on top of the focus stack: Esc still skips the whole cutscene
254
276
  const said = d.say(s.nodes || s.text, { speaker: s.speaker, node: s.node || 'start', channel: s.channel, at: s.at, seconds: s.seconds, onCancel: run.skippable ? () => this.skip(run) : null });
255
277
  run.line = s.channel !== 'bubble'; // a main line sits above the run in the focus stack; a bubble takes no focus
@@ -264,7 +286,7 @@ export class Timeline {
264
286
  // ---------- Letterbox ----------
265
287
  /** @internal */
266
288
  _refreshUI() {
267
- const runs = [...this.runs], on = runs.some(r => r.letterbox), canSkip = runs.some(r => r.letterbox && r.skippable && !r.skipping);
289
+ const runs = [...this.runs], on = runs.some(r => r.letterbox), canSkip = runs.some(r => r.letterbox && r.skippable && !r.ending);
268
290
  this.top.classList.toggle('is-on', on); this.bottom.classList.toggle('is-on', on);
269
291
  this.skipBtn.textContent = this.engine.t('timeline.skip');
270
292
  this.skipBtn.classList.toggle('is-on', canSkip);
@@ -272,6 +294,11 @@ export class Timeline {
272
294
  }
273
295
  }
274
296
 
297
+ /** The run's state is gone, or it broke: the steps left must not run. A skip does run them, so what it leaves matches a playthrough */
298
+ const gone = run => run.ending === 'dropped' || run.ending === 'failed';
299
+ /** What a run says when it ends, to play() and timeline:end alike */
300
+ const answer = run => ({ ending: run.ending, ...(run.error && { error: run.error }) });
301
+
275
302
  const STRINGS = {
276
303
  'zh-CN': { 'module.timeline': '时间轴', 'timeline.skip': '{key:cancel} 跳过', 'timeline.skipAction': '跳过过场' },
277
304
  en: { 'module.timeline': 'timeline', 'timeline.skip': '{key:cancel} Skip', 'timeline.skipAction': 'Skip cutscene' },
@@ -290,14 +317,17 @@ export function timeline(opts = {}) {
290
317
  readParams('timeline', TIMELINE_PARAMS, opts);
291
318
  let t = null;
292
319
  return {
293
- name: 'timeline', type: 'rules', params: TIMELINE_PARAMS, options: opts, label: 'Timeline', stage: 'control', before: ['cameraRig'], optional: ['dialogue'], // before the camera, so the takeover applies on the same frame
320
+ name: 'timeline', skipKind: 'none', type: 'rules', params: TIMELINE_PARAMS, options: opts, label: 'Timeline', stage: 'control', before: ['cameraRig'], optional: ['dialogue'], // before the camera, so the takeover applies on the same frame
294
321
  install(e, ctx) {
295
322
  e.i18n.add(STRINGS); t = new Timeline(e, ctx);
296
323
  // a running cutscene ends with its playthrough (session waits one macrotask for its steps to settle)
297
- ctx.track(e.events.on('session:end', () => { if (t.runs.size) t.skip(); }));
324
+ ctx.track(e.events.on('session:end', () => t._drop()));
298
325
  return t;
299
326
  },
300
327
  frame(dt) { t.frame(dt); },
328
+ afterLoad() { t._drop(r => r.duringLoad); },
329
+ // unloaded mid-cutscene: every run ends through its finally (camera, focus, actors go back); nothing else would, the frame hook is gone
330
+ dispose() { t._drop(); },
301
331
  // scenario param cutscene: 'skip' | 'keep'
302
332
  stateKeys: ['cutscene'],
303
333
  applyState(e, p) {
@@ -307,7 +337,9 @@ export function timeline(opts = {}) {
307
337
  },
308
338
  describe(e, ctx) {
309
339
  const runs = [...t.runs];
310
- if (runs.length) ctx.out.cutscene = { count: runs.length, skippable: runs.some(r => r.skippable && !r.skipping), letterbox: runs.some(r => r.letterbox), actors: [...t._held.keys()] };
340
+ if (runs.length || t._failure) ctx.out.cutscene = { count: runs.length, skippable: runs.some(r => r.skippable && !r.ending), letterbox: runs.some(r => r.letterbox), actors: [...t._held.keys()], endings: { ...t._endings }, ...(t._failure && { failure: t._failure }) };
311
341
  },
312
342
  };
313
343
  }
344
+
345
+ export const handRolled = [{ kind: 'tween', seen: 'a tween of its own', use: 'await engine.timeline.tween(target, to, { duration, ease }): on game time, stops with pause (recipe 07)', quiet: /\b(engine|e|eng|ctx)\.timeline\b/, code: /\b(?:elapsed|time|timer|age|t)\s*\/\s*(?:\w+\.)?(?:dur|duration)\b/i, orRaw: /\bfrom\s+['"](?:gsap|@tweenjs\/tween\.js|tween\.js|animejs)['"]/ }];
@@ -6,6 +6,8 @@
6
6
  // const tw = new Tweener(); call tw.update(dt) every frame
7
7
  // await tw.tween(mesh, { 'position.y': 3, 'material.color': 0xffffff }, 1.5, 'outBack');
8
8
  // const p = tw.tween(...); p.finish() jumps to the end; p.remove() stops where it is (the cancel convention)
9
+ // A task that throws (its start, a step, its end) ends there: what it holds goes back, it leaves the list, its promise rejects. The one who awaits it decides what that means (a
10
+ // Timeline run ends 'failed'); a tween nobody awaits is the page's unhandled rejection, which the engine reports (core/base/errors.js watchRejections). The other tasks go on.
9
11
  import * as THREE from 'three';
10
12
  import { ease as easing } from '../../core/base/easing.js';
11
13
  import { allowOwned } from '../../core/ops/owned.js';
@@ -60,13 +62,13 @@ export class Tweener {
60
62
  constructor() { this.list = []; }
61
63
 
62
64
  /**
63
- * General task: calls step(k) every frame, k is the eased progress. duration in seconds; returns a Promise with finish() / remove().
65
+ * General task: calls step(k) every frame, k is the eased progress. duration in seconds; returns a Promise with finish() / remove(), rejected if start, step or end throws.
64
66
  * The start value is read on the first update (so a later queued tween starts from where the previous one ended).
65
67
  */
66
68
  run({ duration = 0, ease = 'linear', start, step, end, group = null }) {
67
- let res;
68
- const p = new Promise(r => { res = r; });
69
- const task = { t: 0, dur: Math.max(0, duration), ease: easing(ease), start, step, end, group, res, started: false, done: false };
69
+ let res, rej;
70
+ const p = new Promise((yes, no) => { res = yes; rej = no; });
71
+ const task = { t: 0, dur: Math.max(0, duration), ease: easing(ease), start, step, end, group, res, rej, started: false, done: false };
70
72
  task.finish = () => this._complete(task, 1);
71
73
  task.cancel = () => this._complete(task, null);
72
74
  p.finish = task.finish; p.remove = task.cancel; p.task = task;
@@ -98,25 +100,29 @@ export class Tweener {
98
100
  if (!this.list.length) return;
99
101
  for (const task of [...this.list]) {
100
102
  if (task.done) continue;
101
- if (!task.started) { task.started = true; task.start?.(); }
102
- task.t += dt;
103
- if (task.t >= task.dur) this._complete(task, 1);
104
- else task.step?.(task.ease(task.t / task.dur));
103
+ try {
104
+ if (!task.started) { task.started = true; task.start?.(); }
105
+ task.t += dt;
106
+ if (task.t >= task.dur) this._complete(task, 1);
107
+ else task.step?.(task.ease(task.t / task.dur));
108
+ } catch (error) { this._complete(task, null, { error }); }
105
109
  }
106
110
  }
107
111
 
108
112
  /** Jump a group of tasks (or all) to their end */
109
113
  finishAll(group) { for (const t of [...this.list]) if (group === undefined || t.group === group) t.finish(); }
110
114
 
111
- _complete(task, k) {
115
+ /** The one way a task ends: k 1 jumps to the end, null stops where it is. failed { error }: a throw on the way, and the first one is the promise's answer. So finish() never throws and finishAll reaches every task */
116
+ _complete(task, k, failed = null) {
112
117
  if (task.done) return;
113
118
  task.done = true;
114
- if (k !== null) {
119
+ const guard = fn => { try { fn(); } catch (error) { failed ??= { error }; } };
120
+ if (k !== null) guard(() => {
115
121
  if (!task.started) { task.started = true; task.start?.(); }
116
122
  task.step?.(task.ease(1));
117
- }
118
- task.end?.(k !== null);
123
+ });
124
+ guard(() => task.end?.(k !== null && !failed)); // whatever happened above: end gives back what the task holds
119
125
  const i = this.list.indexOf(task); if (i >= 0) this.list.splice(i, 1);
120
- task.res(k !== null);
126
+ if (failed) task.rej(failed.error); else task.res(k !== null);
121
127
  }
122
128
  }
@@ -1,19 +1,28 @@
1
1
  // Timeline camera and path steps (helper of Timeline.js): a shot takes the camera over with engine.takeCamera (core/ops/holds.js) and gives it back when the run ends;
2
2
  // a path moves any object (a character, the camera) along a spline. Each function takes the Timeline as `t`.
3
+ // One writer: a step writes what it drives only while it is the holder in charge (written). Out of charge its clock runs on and it writes nothing; back in charge it writes where its
4
+ // clock is. So a shot under a newer holder of the camera, or a path on a character something above the cutscene has, ends on time and leaves the real owner's values alone.
3
5
  import * as THREE from 'three';
4
6
  import { vec3 as v3 } from '../../core/base/math.js';
5
7
  import { surfaceOr } from '../../core/scene/ground.js';
8
+ import { holdFov } from '../../core/scene/view.js';
9
+
10
+ /** The one check: a timed step whose step(k) runs only while mine() says it is in charge */
11
+ const written = (t, run, mine, o) => t._timed(run, { ...o, step: k => { if (mine()) o.step(k); } });
12
+ /** Holds the camera now: compared by owner, because engine.cameraOverride is a copy of what takeCamera got */
13
+ const onCamera = (t, st) => t.engine.cameraOverride?.owner === st.override.owner;
6
14
 
7
15
  /** Camera: { from?, to: { position, lookAt }, duration, ease, fov } or { release: true } */
8
16
  export function cameraStep(t, c, run) {
9
- const e = t.engine, cam = e.camera;
17
+ const e = t.engine;
10
18
  if (c.release) { releaseCamera(t, run); return Promise.resolve(); }
11
19
  const st = takeCamera(t, run);
12
20
  let fromP, fromL, fromFov, toP, toL;
13
21
  const lookOf = l => l?.isObject3D ? l.position : l; // may look at a moving object
14
- return t._timed(run, {
22
+ return written(t, run, () => onCamera(t, st), {
15
23
  duration: c.duration ?? 1, ease: c.ease ?? 'inOutCubic',
16
24
  start: () => {
25
+ const cam = e.camera;
17
26
  fromP = c.from?.position ? v3(c.from.position) : cam.position.clone();
18
27
  toP = c.to?.position ? v3(c.to.position) : fromP.clone();
19
28
  // start look-at: use it if given, otherwise keep the current view direction at the same distance as the target
@@ -23,11 +32,12 @@ export function cameraStep(t, c, run) {
23
32
  fromFov = c.from?.fov ?? cam.fov;
24
33
  },
25
34
  step: k => {
35
+ const cam = e.camera; // read here: a game may assign engine.camera during the shot
26
36
  if (c.to?.lookAt?.isObject3D) toL.copy(c.to.lookAt.position);
27
37
  cam.position.lerpVectors(fromP, toP, k);
28
38
  st.look.lerpVectors(fromL, toL, k);
29
39
  cam.lookAt(st.look);
30
- if (c.fov != null) { cam.fov = fromFov + (c.fov - fromFov) * k; cam.updateProjectionMatrix(); }
40
+ if (c.fov != null && cam.fov !== undefined) { fromFov ??= cam.fov; holdFov(e, fromFov + (c.fov - fromFov) * k); } // a real orthographic camera has none
31
41
  st.override.focus.copy(st.look);
32
42
  },
33
43
  });
@@ -38,16 +48,14 @@ function takeCamera(t, run) {
38
48
  // a claim of its own (engine.takeCamera): a named view or a game holding the camera keeps its claim and gets the camera back afterwards.
39
49
  // The owner carries the run id so two overlapping runs are two holders, not one overwriting the other.
40
50
  const override = { owner: 'timeline:' + run.id, id: run.id, focus: e.focus.clone() };
41
- run.cam = { fov: e.camera.fov, look: new THREE.Vector3(), override, hold: e.takeCamera(override.owner, override) };
51
+ run.cam = { look: new THREE.Vector3(), override, hold: e.takeCamera(override.owner, override) };
42
52
  }
43
53
  return run.cam;
44
54
  }
45
55
  export function releaseCamera(t, run) {
46
56
  const st = run.cam; if (!st) return;
47
57
  run.cam = null;
48
- const e = t.engine;
49
- st.hold.remove();
50
- if (e.camera.fov !== st.fov) { e.camera.fov = st.fov; e.camera.updateProjectionMatrix(); }
58
+ st.hold.remove(); // the view comes back with the last holder (core/scene/view.js viewHold)
51
59
  }
52
60
 
53
61
  /** Path: { object, points, duration, ease, lookAhead, ground, closed, curve: 'catmullrom'|'centripetal'|'chordal', tension } */
@@ -64,7 +72,8 @@ export function pathStep(t, p, run) {
64
72
  const isCam = obj === e.camera;
65
73
  const st = isCam ? takeCamera(t, run) : null;
66
74
  const pos = new THREE.Vector3(), ahead = new THREE.Vector3();
67
- return t._timed(run, {
75
+ const mine = isCam ? () => onCamera(t, st) : chr ? () => !!t._held.get(chr.id)?.h.placing : () => true;
76
+ return written(t, run, mine, {
68
77
  duration: p.duration ?? 2, ease: p.ease ?? 'inOutSine',
69
78
  step: k => {
70
79
  curve.getPointAt(Math.min(1, Math.max(0, k)), pos);
@@ -84,9 +93,9 @@ export function pathStep(t, p, run) {
84
93
  },
85
94
  }).then(r => {
86
95
  // the path moved the model, not the mover: the character's world record (the player's) catches up once, so a save right after the cutscene has the new
87
- // place
96
+ // place. Not while another owner has it: the path wrote nothing on its last step, and the record is that owner's
88
97
  unpath();
89
- if (chr?.recordId && chr.syncRecord) e.ops.allow(() => chr.syncRecord());
98
+ if (chr?.recordId && chr.syncRecord && mine()) e.ops.allow(() => chr.syncRecord());
90
99
  return r;
91
100
  }, err => { unpath(); throw err; });
92
101
  }