wallhammer-web 0.0.0-stage → 0.4.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/LICENSE +21 -0
- package/README.md +88 -2
- package/dist/base/data.d.ts +67 -0
- package/dist/base/data.js +10 -0
- package/dist/base/destructible.d.ts +167 -0
- package/dist/base/destructible.js +47 -0
- package/dist/base/env-slot.d.ts +101 -0
- package/dist/base/env-slot.js +8 -0
- package/dist/base/frames.d.ts +138 -0
- package/dist/base/frames.js +459 -0
- package/dist/base/index.d.ts +21 -0
- package/dist/base/index.js +11 -0
- package/dist/base/mat34.d.ts +46 -0
- package/dist/base/mat34.js +173 -0
- package/dist/base/material.d.ts +87 -0
- package/dist/base/material.js +6 -0
- package/dist/base/math.d.ts +27 -0
- package/dist/base/math.js +60 -0
- package/dist/base/progress.d.ts +38 -0
- package/dist/base/progress.js +81 -0
- package/dist/base/rig.d.ts +177 -0
- package/dist/base/rig.js +97 -0
- package/dist/base/trig.d.ts +29 -0
- package/dist/base/trig.js +169 -0
- package/dist/base/up.d.ts +26 -0
- package/dist/base/up.js +40 -0
- package/dist/base/view.d.ts +128 -0
- package/dist/base/view.js +136 -0
- package/dist/camera/camera.d.ts +99 -0
- package/dist/camera/camera.js +228 -0
- package/dist/camera/follow.d.ts +120 -0
- package/dist/camera/follow.js +320 -0
- package/dist/camera/free.d.ts +153 -0
- package/dist/camera/free.js +359 -0
- package/dist/camera/index.d.ts +10 -0
- package/dist/camera/index.js +6 -0
- package/dist/camera/orbit.d.ts +93 -0
- package/dist/camera/orbit.js +261 -0
- package/dist/camera/path.d.ts +48 -0
- package/dist/camera/path.js +179 -0
- package/dist/core/ballistic.d.ts +66 -0
- package/dist/core/ballistic.js +104 -0
- package/dist/core/bodies.d.ts +77 -0
- package/dist/core/bodies.js +125 -0
- package/dist/core/clock.d.ts +37 -0
- package/dist/core/clock.js +87 -0
- package/dist/core/cover-fade.d.ts +14 -0
- package/dist/core/cover-fade.js +38 -0
- package/dist/core/index.d.ts +13 -0
- package/dist/core/index.js +8 -0
- package/dist/core/kepler.d.ts +74 -0
- package/dist/core/kepler.js +108 -0
- package/dist/core/level-fade.d.ts +66 -0
- package/dist/core/level-fade.js +123 -0
- package/dist/core/palette.d.ts +15 -0
- package/dist/core/palette.js +47 -0
- package/dist/env/atmosphere.d.ts +129 -0
- package/dist/env/atmosphere.js +707 -0
- package/dist/env/cloud-field.d.ts +44 -0
- package/dist/env/cloud-field.js +111 -0
- package/dist/env/environment.d.ts +217 -0
- package/dist/env/environment.js +441 -0
- package/dist/env/frame-block.d.ts +29 -0
- package/dist/env/frame-block.js +152 -0
- package/dist/env/glsl.d.ts +35 -0
- package/dist/env/glsl.js +246 -0
- package/dist/env/index.d.ts +13 -0
- package/dist/env/index.js +9 -0
- package/dist/env/sky.d.ts +48 -0
- package/dist/env/sky.js +227 -0
- package/dist/env/sun.d.ts +31 -0
- package/dist/env/sun.js +42 -0
- package/dist/gen-arch/build.d.ts +17 -0
- package/dist/gen-arch/build.js +414 -0
- package/dist/gen-arch/damage/damaged.d.ts +44 -0
- package/dist/gen-arch/damage/damaged.js +1119 -0
- package/dist/gen-arch/damage/fall-material.d.ts +10 -0
- package/dist/gen-arch/damage/fall-material.js +47 -0
- package/dist/gen-arch/damage/fall.d.ts +78 -0
- package/dist/gen-arch/damage/fall.js +797 -0
- package/dist/gen-arch/damage/rubble.d.ts +22 -0
- package/dist/gen-arch/damage/rubble.js +188 -0
- package/dist/gen-arch/index.d.ts +25 -0
- package/dist/gen-arch/index.js +16 -0
- package/dist/gen-arch/interior/interior-plan.d.ts +114 -0
- package/dist/gen-arch/interior/interior-plan.js +615 -0
- package/dist/gen-arch/interior/interior.d.ts +97 -0
- package/dist/gen-arch/interior/interior.js +966 -0
- package/dist/gen-arch/structure/building-shader.d.ts +25 -0
- package/dist/gen-arch/structure/building-shader.js +946 -0
- package/dist/gen-arch/structure/buildings.d.ts +248 -0
- package/dist/gen-arch/structure/buildings.js +294 -0
- package/dist/gen-arch/structure/dims.d.ts +77 -0
- package/dist/gen-arch/structure/dims.js +39 -0
- package/dist/gen-arch/structure/distant.d.ts +62 -0
- package/dist/gen-arch/structure/distant.js +316 -0
- package/dist/gen-arch/structure/env-slots.d.ts +18 -0
- package/dist/gen-arch/structure/env-slots.js +47 -0
- package/dist/gen-arch/structure/facade.d.ts +193 -0
- package/dist/gen-arch/structure/facade.js +159 -0
- package/dist/gen-arch/structure/parts.d.ts +31 -0
- package/dist/gen-arch/structure/parts.js +69 -0
- package/dist/gen-arch/structure/roof.d.ts +76 -0
- package/dist/gen-arch/structure/roof.js +568 -0
- package/dist/gen-arch/structure/structure-material.d.ts +23 -0
- package/dist/gen-arch/structure/structure-material.js +55 -0
- package/dist/gen-arch/structure/writer.d.ts +94 -0
- package/dist/gen-arch/structure/writer.js +195 -0
- package/dist/gen-core/cell.d.ts +138 -0
- package/dist/gen-core/cell.js +150 -0
- package/dist/gen-core/cube-sphere.d.ts +57 -0
- package/dist/gen-core/cube-sphere.js +159 -0
- package/dist/gen-core/field.d.ts +55 -0
- package/dist/gen-core/field.js +154 -0
- package/dist/gen-core/geometry.d.ts +31 -0
- package/dist/gen-core/geometry.js +231 -0
- package/dist/gen-core/hash.d.ts +18 -0
- package/dist/gen-core/hash.js +58 -0
- package/dist/gen-core/height-field.d.ts +33 -0
- package/dist/gen-core/height-field.js +137 -0
- package/dist/gen-core/index.d.ts +22 -0
- package/dist/gen-core/index.js +17 -0
- package/dist/gen-core/instance-cells.d.ts +37 -0
- package/dist/gen-core/instance-cells.js +72 -0
- package/dist/gen-core/near-scatter.d.ts +56 -0
- package/dist/gen-core/near-scatter.js +120 -0
- package/dist/gen-core/noise.d.ts +23 -0
- package/dist/gen-core/noise.js +72 -0
- package/dist/gen-core/occupancy.d.ts +21 -0
- package/dist/gen-core/occupancy.js +104 -0
- package/dist/gen-core/sphere-patch.d.ts +138 -0
- package/dist/gen-core/sphere-patch.js +544 -0
- package/dist/gen-core/strip-surface.d.ts +34 -0
- package/dist/gen-core/strip-surface.js +246 -0
- package/dist/gen-core/strip.d.ts +102 -0
- package/dist/gen-core/strip.js +817 -0
- package/dist/gen-ground/env-slots.d.ts +48 -0
- package/dist/gen-ground/env-slots.js +78 -0
- package/dist/gen-ground/ground.d.ts +78 -0
- package/dist/gen-ground/ground.js +208 -0
- package/dist/gen-ground/index.d.ts +9 -0
- package/dist/gen-ground/index.js +7 -0
- package/dist/gen-ground/markings.d.ts +46 -0
- package/dist/gen-ground/markings.js +124 -0
- package/dist/gen-ground/materials.d.ts +18 -0
- package/dist/gen-ground/materials.js +22 -0
- package/dist/gen-ground/terrain-shader.d.ts +5 -0
- package/dist/gen-ground/terrain-shader.js +151 -0
- package/dist/gen-mesh/bare-shapes.d.ts +194 -0
- package/dist/gen-mesh/bare-shapes.js +596 -0
- package/dist/gen-mesh/bisect-surface.d.ts +47 -0
- package/dist/gen-mesh/bisect-surface.js +261 -0
- package/dist/gen-mesh/box.d.ts +18 -0
- package/dist/gen-mesh/box.js +2 -0
- package/dist/gen-mesh/cell-morph-mesh.d.ts +52 -0
- package/dist/gen-mesh/cell-morph-mesh.js +258 -0
- package/dist/gen-mesh/cell-surface-mesh.d.ts +32 -0
- package/dist/gen-mesh/cell-surface-mesh.js +140 -0
- package/dist/gen-mesh/chunk-mesh.d.ts +25 -0
- package/dist/gen-mesh/chunk-mesh.js +112 -0
- package/dist/gen-mesh/drape-mesh.d.ts +36 -0
- package/dist/gen-mesh/drape-mesh.js +98 -0
- package/dist/gen-mesh/height-field-mesh.d.ts +14 -0
- package/dist/gen-mesh/height-field-mesh.js +98 -0
- package/dist/gen-mesh/index.d.ts +26 -0
- package/dist/gen-mesh/index.js +15 -0
- package/dist/gen-mesh/line-mesh.d.ts +25 -0
- package/dist/gen-mesh/line-mesh.js +62 -0
- package/dist/gen-mesh/mesh-builder.d.ts +115 -0
- package/dist/gen-mesh/mesh-builder.js +315 -0
- package/dist/gen-mesh/packed-mesh.d.ts +53 -0
- package/dist/gen-mesh/packed-mesh.js +205 -0
- package/dist/gen-mesh/prism-mesh.d.ts +33 -0
- package/dist/gen-mesh/prism-mesh.js +176 -0
- package/dist/gen-mesh/rig-mesh.d.ts +9 -0
- package/dist/gen-mesh/rig-mesh.js +135 -0
- package/dist/gen-mesh/strip-mesh.d.ts +45 -0
- package/dist/gen-mesh/strip-mesh.js +258 -0
- package/dist/gen-plan/drain.d.ts +58 -0
- package/dist/gen-plan/drain.js +270 -0
- package/dist/gen-plan/grid.d.ts +37 -0
- package/dist/gen-plan/grid.js +182 -0
- package/dist/gen-plan/horizon.d.ts +50 -0
- package/dist/gen-plan/horizon.js +185 -0
- package/dist/gen-plan/index.d.ts +15 -0
- package/dist/gen-plan/index.js +13 -0
- package/dist/gen-plan/line-tiles.d.ts +35 -0
- package/dist/gen-plan/line-tiles.js +190 -0
- package/dist/gen-plan/lines.d.ts +31 -0
- package/dist/gen-plan/lines.js +183 -0
- package/dist/gen-plan/plots.d.ts +40 -0
- package/dist/gen-plan/plots.js +282 -0
- package/dist/gen-plan/route.d.ts +55 -0
- package/dist/gen-plan/route.js +297 -0
- package/dist/gen-plan/sites.d.ts +32 -0
- package/dist/gen-plan/sites.js +209 -0
- package/dist/gen-tex/code-stamp.d.ts +31 -0
- package/dist/gen-tex/code-stamp.js +137 -0
- package/dist/gen-tex/index.d.ts +7 -0
- package/dist/gen-tex/index.js +5 -0
- package/dist/gen-tex/light-horizon.d.ts +47 -0
- package/dist/gen-tex/light-horizon.js +180 -0
- package/dist/gen-tex/textures.d.ts +8 -0
- package/dist/gen-tex/textures.js +49 -0
- package/dist/gen-tex/value-textures.d.ts +46 -0
- package/dist/gen-tex/value-textures.js +102 -0
- package/dist/gen-vegetation/env-slots.d.ts +20 -0
- package/dist/gen-vegetation/env-slots.js +29 -0
- package/dist/gen-vegetation/index.d.ts +8 -0
- package/dist/gen-vegetation/index.js +7 -0
- package/dist/gen-vegetation/materials.d.ts +7 -0
- package/dist/gen-vegetation/materials.js +17 -0
- package/dist/gen-vegetation/planting.d.ts +170 -0
- package/dist/gen-vegetation/planting.js +363 -0
- package/dist/gen-vegetation/tree-shader.d.ts +23 -0
- package/dist/gen-vegetation/tree-shader.js +229 -0
- package/dist/gen-vegetation/trees.d.ts +59 -0
- package/dist/gen-vegetation/trees.js +329 -0
- package/dist/gpu/device.d.ts +286 -0
- package/dist/gpu/device.js +1157 -0
- package/dist/gpu/gl.d.ts +134 -0
- package/dist/gpu/gl.js +276 -0
- package/dist/gpu/index.d.ts +4 -0
- package/dist/gpu/index.js +3 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +13 -0
- package/dist/motion/graph.d.ts +244 -0
- package/dist/motion/graph.js +739 -0
- package/dist/motion/index.d.ts +8 -0
- package/dist/motion/index.js +8 -0
- package/dist/motion/pose-ops.d.ts +30 -0
- package/dist/motion/pose-ops.js +232 -0
- package/dist/motion/reach.d.ts +45 -0
- package/dist/motion/reach.js +264 -0
- package/dist/motion/skeleton.d.ts +49 -0
- package/dist/motion/skeleton.js +99 -0
- package/dist/platform/byte-store.d.ts +21 -0
- package/dist/platform/byte-store.js +137 -0
- package/dist/platform/canvas.d.ts +29 -0
- package/dist/platform/canvas.js +76 -0
- package/dist/platform/context-loss.d.ts +8 -0
- package/dist/platform/context-loss.js +17 -0
- package/dist/platform/free-input.d.ts +51 -0
- package/dist/platform/free-input.js +100 -0
- package/dist/platform/index.d.ts +18 -0
- package/dist/platform/index.js +10 -0
- package/dist/platform/input.d.ts +48 -0
- package/dist/platform/input.js +117 -0
- package/dist/platform/keys.d.ts +12 -0
- package/dist/platform/keys.js +29 -0
- package/dist/platform/loop.d.ts +41 -0
- package/dist/platform/loop.js +84 -0
- package/dist/platform/walk-input.d.ts +74 -0
- package/dist/platform/walk-input.js +226 -0
- package/dist/platform/worker.d.ts +51 -0
- package/dist/platform/worker.js +170 -0
- package/dist/render/bone-instances.d.ts +24 -0
- package/dist/render/bone-instances.js +74 -0
- package/dist/render/cells.d.ts +70 -0
- package/dist/render/cells.js +144 -0
- package/dist/render/glsl.d.ts +66 -0
- package/dist/render/glsl.js +173 -0
- package/dist/render/index.d.ts +26 -0
- package/dist/render/index.js +16 -0
- package/dist/render/instance-sets.d.ts +97 -0
- package/dist/render/instance-sets.js +241 -0
- package/dist/render/near-instances.d.ts +75 -0
- package/dist/render/near-instances.js +219 -0
- package/dist/render/particles.d.ts +63 -0
- package/dist/render/particles.js +215 -0
- package/dist/render/pieces.d.ts +57 -0
- package/dist/render/pieces.js +151 -0
- package/dist/render/point-lights.d.ts +97 -0
- package/dist/render/point-lights.js +243 -0
- package/dist/render/renderer.d.ts +447 -0
- package/dist/render/renderer.js +1100 -0
- package/dist/render/rig.d.ts +74 -0
- package/dist/render/rig.js +576 -0
- package/dist/render/shadow-areas.d.ts +49 -0
- package/dist/render/shadow-areas.js +125 -0
- package/dist/render/shadow-glsl.d.ts +12 -0
- package/dist/render/shadow-glsl.js +146 -0
- package/dist/render/transient.d.ts +61 -0
- package/dist/render/transient.js +213 -0
- package/dist/render/value-glsl.d.ts +17 -0
- package/dist/render/value-glsl.js +208 -0
- package/dist/render/value-layers.d.ts +180 -0
- package/dist/render/value-layers.js +333 -0
- package/dist/runtime/atmosphere.d.ts +37 -0
- package/dist/runtime/atmosphere.js +304 -0
- package/dist/runtime/cloud-shell.d.ts +29 -0
- package/dist/runtime/cloud-shell.js +158 -0
- package/dist/runtime/engine.d.ts +155 -0
- package/dist/runtime/engine.js +305 -0
- package/dist/runtime/environment-link.d.ts +22 -0
- package/dist/runtime/environment-link.js +49 -0
- package/dist/runtime/index.d.ts +13 -0
- package/dist/runtime/index.js +12 -0
- package/dist/runtime/precipitation-glsl.d.ts +2 -0
- package/dist/runtime/precipitation-glsl.js +70 -0
- package/dist/runtime/precipitation.d.ts +11 -0
- package/dist/runtime/precipitation.js +78 -0
- package/dist/runtime/sky.d.ts +30 -0
- package/dist/runtime/sky.js +114 -0
- package/dist/runtime/streaming.d.ts +245 -0
- package/dist/runtime/streaming.js +602 -0
- package/dist/sim/agenda.d.ts +167 -0
- package/dist/sim/agenda.js +425 -0
- package/dist/sim/bytes.d.ts +34 -0
- package/dist/sim/bytes.js +125 -0
- package/dist/sim/destruction/apply.d.ts +59 -0
- package/dist/sim/destruction/apply.js +243 -0
- package/dist/sim/destruction/cell-states.d.ts +54 -0
- package/dist/sim/destruction/cell-states.js +198 -0
- package/dist/sim/destruction/index.d.ts +11 -0
- package/dist/sim/destruction/index.js +10 -0
- package/dist/sim/destruction/int-geometry.d.ts +17 -0
- package/dist/sim/destruction/int-geometry.js +131 -0
- package/dist/sim/destruction/pick.d.ts +13 -0
- package/dist/sim/destruction/pick.js +59 -0
- package/dist/sim/destruction/rubble.d.ts +8 -0
- package/dist/sim/destruction/rubble.js +159 -0
- package/dist/sim/destruction/state.d.ts +15 -0
- package/dist/sim/destruction/state.js +83 -0
- package/dist/sim/destruction/structure.d.ts +30 -0
- package/dist/sim/destruction/structure.js +139 -0
- package/dist/sim/events.d.ts +37 -0
- package/dist/sim/events.js +106 -0
- package/dist/sim/fixed.d.ts +21 -0
- package/dist/sim/fixed.js +64 -0
- package/dist/sim/flow-probe.d.ts +58 -0
- package/dist/sim/flow-probe.js +158 -0
- package/dist/sim/flow.d.ts +257 -0
- package/dist/sim/flow.js +967 -0
- package/dist/sim/index.d.ts +28 -0
- package/dist/sim/index.js +20 -0
- package/dist/sim/interlock.d.ts +47 -0
- package/dist/sim/interlock.js +252 -0
- package/dist/sim/movers.d.ts +228 -0
- package/dist/sim/movers.js +1083 -0
- package/dist/sim/nav.d.ts +72 -0
- package/dist/sim/nav.js +292 -0
- package/dist/sim/paths.d.ts +80 -0
- package/dist/sim/paths.js +197 -0
- package/dist/sim/regions.d.ts +87 -0
- package/dist/sim/regions.js +517 -0
- package/dist/sim/simulation.d.ts +114 -0
- package/dist/sim/simulation.js +291 -0
- package/dist/sim/states.d.ts +23 -0
- package/dist/sim/states.js +54 -0
- package/dist/sim/table.d.ts +49 -0
- package/dist/sim/table.js +126 -0
- package/dist/sim/zones.d.ts +11 -0
- package/dist/sim/zones.js +196 -0
- package/dist/tools/collision-lines.d.ts +12 -0
- package/dist/tools/collision-lines.js +50 -0
- package/dist/tools/data-view-glsl.d.ts +3 -0
- package/dist/tools/data-view-glsl.js +63 -0
- package/dist/tools/data-view-layers.d.ts +20 -0
- package/dist/tools/data-view-layers.js +60 -0
- package/dist/tools/debug-view.d.ts +30 -0
- package/dist/tools/debug-view.js +4 -0
- package/dist/tools/index.d.ts +10 -0
- package/dist/tools/index.js +7 -0
- package/dist/tools/mesh-checks.d.ts +67 -0
- package/dist/tools/mesh-checks.js +188 -0
- package/dist/tools/profile.d.ts +85 -0
- package/dist/tools/profile.js +173 -0
- package/dist/world/cell-cache.d.ts +32 -0
- package/dist/world/cell-cache.js +60 -0
- package/dist/world/cell-format.d.ts +25 -0
- package/dist/world/cell-format.js +232 -0
- package/dist/world/collide.d.ts +192 -0
- package/dist/world/collide.js +822 -0
- package/dist/world/index.d.ts +16 -0
- package/dist/world/index.js +12 -0
- package/dist/world/mesh-solids.d.ts +48 -0
- package/dist/world/mesh-solids.js +169 -0
- package/dist/world/pick.d.ts +34 -0
- package/dist/world/pick.js +177 -0
- package/dist/world/providers.d.ts +66 -0
- package/dist/world/providers.js +83 -0
- package/dist/world/sphere.d.ts +69 -0
- package/dist/world/sphere.js +271 -0
- package/dist/world/streaming.d.ts +288 -0
- package/dist/world/streaming.js +801 -0
- package/dist/world/walker.d.ts +145 -0
- package/dist/world/walker.js +502 -0
- package/docs/architecture.md +111 -0
- package/docs/cameras.md +302 -0
- package/docs/concepts.md +123 -0
- package/docs/destruction.md +127 -0
- package/docs/frames.md +199 -0
- package/docs/generators.md +575 -0
- package/docs/getting-started.md +123 -0
- package/docs/headless.md +50 -0
- package/docs/motion.md +160 -0
- package/docs/planet.md +152 -0
- package/docs/recipes.md +521 -0
- package/docs/reference.md +364 -0
- package/docs/rendering.md +823 -0
- package/docs/rigs.md +203 -0
- package/docs/simulation.md +235 -0
- package/docs/streaming.md +590 -0
- package/docs/system.md +166 -0
- package/docs/tools.md +175 -0
- package/docs/walking.md +262 -0
- package/package.json +131 -3
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { ByteReader, ByteWriter } from './bytes.ts';
|
|
2
|
+
import { EventQueue } from './events.ts';
|
|
3
|
+
import type { SimData } from './events.ts';
|
|
4
|
+
/** A part of the simulation state: saved into snapshots in a fixed order and hashed. */
|
|
5
|
+
export interface SimState {
|
|
6
|
+
readonly name: string;
|
|
7
|
+
save(w: ByteWriter): void;
|
|
8
|
+
load(r: ByteReader): void;
|
|
9
|
+
}
|
|
10
|
+
/** A system: a plain function run once per tick. */
|
|
11
|
+
export type System = (sim: Simulation) => void;
|
|
12
|
+
export interface Command {
|
|
13
|
+
/** Tick the command applies to (applied before the systems of that tick). */
|
|
14
|
+
readonly tick: number;
|
|
15
|
+
readonly kind: string;
|
|
16
|
+
readonly data: SimData;
|
|
17
|
+
}
|
|
18
|
+
export type CommandHandler = (cmd: Command, sim: Simulation) => void;
|
|
19
|
+
export interface SimulationOptions {
|
|
20
|
+
/** World seed; named streams derive from it. */
|
|
21
|
+
seed?: string | number;
|
|
22
|
+
/** Ticks per simulated second (integer). Default 20. */
|
|
23
|
+
hz?: number;
|
|
24
|
+
/** Most ticks one frame may run; beyond that simulated time slows down instead of stalling the frame. Default 8. */
|
|
25
|
+
maxTicksPerFrame?: number;
|
|
26
|
+
/** A checkpoint for seeking is kept every this many ticks. Default 200. */
|
|
27
|
+
checkpointEvery?: number;
|
|
28
|
+
/** How many checkpoints are kept besides the one of tick 0. Default 32. */
|
|
29
|
+
checkpoints?: number;
|
|
30
|
+
}
|
|
31
|
+
export declare class Simulation {
|
|
32
|
+
readonly seed: string;
|
|
33
|
+
readonly hz: number;
|
|
34
|
+
readonly events: EventQueue;
|
|
35
|
+
/** Most ticks one `advance` runs. */
|
|
36
|
+
maxTicksPerFrame: number;
|
|
37
|
+
private readonly systems;
|
|
38
|
+
private readonly states;
|
|
39
|
+
private readonly handlers;
|
|
40
|
+
private readonly commands;
|
|
41
|
+
private cursor;
|
|
42
|
+
private tickNow;
|
|
43
|
+
private acc;
|
|
44
|
+
private inTick;
|
|
45
|
+
private running;
|
|
46
|
+
private rate;
|
|
47
|
+
private readonly checkpointEvery;
|
|
48
|
+
private readonly checkpointLimit;
|
|
49
|
+
private readonly checkpoints;
|
|
50
|
+
private readonly tickListeners;
|
|
51
|
+
private readonly changeListeners;
|
|
52
|
+
constructor(options?: SimulationOptions);
|
|
53
|
+
/** Ticks run so far; also the tick that runs next. */
|
|
54
|
+
get tick(): number;
|
|
55
|
+
/** Simulated seconds (for display; logic counts ticks). */
|
|
56
|
+
get time(): number;
|
|
57
|
+
/** Fraction 0–1 of the way from the last tick to the next one, for interpolating the image. */
|
|
58
|
+
get alpha(): number;
|
|
59
|
+
get paused(): boolean;
|
|
60
|
+
/** Multiplier of simulated time per real second. */
|
|
61
|
+
get speed(): number;
|
|
62
|
+
/** Does the simulation move on its own (for the frame loop)? Without systems or queued commands nothing can change. */
|
|
63
|
+
get animating(): boolean;
|
|
64
|
+
/** Converts seconds to whole ticks. */
|
|
65
|
+
ticks(seconds: number): number;
|
|
66
|
+
/** Adds a system after the ones already registered. Names must be unique. */
|
|
67
|
+
addSystem(name: string, run: System): void;
|
|
68
|
+
/** Adds a state part; snapshots save the parts in registration order. Names must be unique. */
|
|
69
|
+
addState(state: SimState): void;
|
|
70
|
+
/** Handles commands of one kind. A command without a handler is an error when its tick runs. */
|
|
71
|
+
onCommand(kind: string, handler: CommandHandler): void;
|
|
72
|
+
/** Called after every tick (also during seeking), e.g. to record hashes. Returns an unsubscribe function. */
|
|
73
|
+
onTick(listener: (sim: Simulation) => void): () => void;
|
|
74
|
+
/** Called when pause, speed or the tick change from outside the frame (step, seek, restore). */
|
|
75
|
+
onChange(listener: () => void): () => void;
|
|
76
|
+
private changed;
|
|
77
|
+
/**
|
|
78
|
+
* Queues a command for `tick` (default: the next tick). Commands of one tick apply in the order they were queued.
|
|
79
|
+
* A command for a tick that already ran is an error. Queued from inside a tick, the default is the tick after it.
|
|
80
|
+
*/
|
|
81
|
+
command(kind: string, data?: SimData, tick?: number): Command;
|
|
82
|
+
/** The command log (applied and pending). */
|
|
83
|
+
get commandLog(): readonly Command[];
|
|
84
|
+
/** A uint32 from the named stream of the seed, keyed by the current tick and the given integers. */
|
|
85
|
+
random(stream: string, ...keys: number[]): number;
|
|
86
|
+
private runTick;
|
|
87
|
+
private keepCheckpoint;
|
|
88
|
+
/** Runs n ticks now, paused or not. */
|
|
89
|
+
step(n?: number): void;
|
|
90
|
+
/**
|
|
91
|
+
* Feeds real time (seconds) from the frame: runs as many whole ticks as fit, at most `maxTicksPerFrame`, and keeps
|
|
92
|
+
* the remainder as `alpha`. Returns the number of ticks run.
|
|
93
|
+
*/
|
|
94
|
+
advance(realSeconds: number): number;
|
|
95
|
+
pause(): void;
|
|
96
|
+
play(): void;
|
|
97
|
+
setSpeed(speed: number): void;
|
|
98
|
+
/**
|
|
99
|
+
* Starts the clock over: restores `snapshot` when given (for example one taken before the first tick), makes the
|
|
100
|
+
* state tick 0 and forgets the checkpoints and the command log, which belong to the old clock. For an app that puts
|
|
101
|
+
* tick 0 at a point of its own time and moves it on a long jump (see `Agendas.setOrigin`).
|
|
102
|
+
*/
|
|
103
|
+
rebase(snapshot?: Uint8Array): void;
|
|
104
|
+
/** Pauses and runs exactly one tick. */
|
|
105
|
+
stepOnce(): void;
|
|
106
|
+
/** Goes to an exact tick: forward by stepping, back by restoring the nearest checkpoint and replaying the log. */
|
|
107
|
+
seek(tick: number): void;
|
|
108
|
+
/** All state as bytes: the tick and every state part in registration order. Commands are input, not state. */
|
|
109
|
+
snapshot(): Uint8Array;
|
|
110
|
+
/** Restores a snapshot of this simulation (same state parts). Commands for later ticks are replayed from the log. */
|
|
111
|
+
restore(bytes: Uint8Array): void;
|
|
112
|
+
/** Hash of the whole state (16 hex digits). Equal hashes mean equal snapshots. */
|
|
113
|
+
stateHash(): string;
|
|
114
|
+
}
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
// Fixed-step simulation (Wallhammer Zero `eng_sim`, determinism tier A). The tick counter is an integer and the step
|
|
2
|
+
// is fixed (20 Hz by default), so the state after tick N depends only on the seed, the initial setup and the
|
|
3
|
+
// commands stamped for ticks below N: never on the frame rate, the wall clock, rendering or the camera.
|
|
4
|
+
//
|
|
5
|
+
// - Input is only commands, each stamped with the tick it applies to. They are kept as a log, so seeking back
|
|
6
|
+
// restores a checkpoint and replays the log.
|
|
7
|
+
// - Systems are plain functions run in registration order every tick. Commands of a tick are applied first.
|
|
8
|
+
// - State lives in registered `SimState` parts (entity tables, path sets, the event queue). A snapshot is their bytes
|
|
9
|
+
// in registration order, the state hash is a hash of the snapshot.
|
|
10
|
+
// - Pause, speed and single-step only decide how many ticks a frame runs; they are not state.
|
|
11
|
+
// - Randomness only from named streams of the seed (`random`), keyed by the tick and integers. No `Math.random`.
|
|
12
|
+
import { hashInts, streamSeed } from '../gen-core/hash.js';
|
|
13
|
+
import { ByteReader, ByteWriter, hashBytes } from './bytes.js';
|
|
14
|
+
import { EventQueue } from './events.js';
|
|
15
|
+
const MAGIC = 0x31534857; // "WHS1"
|
|
16
|
+
export class Simulation {
|
|
17
|
+
seed;
|
|
18
|
+
hz;
|
|
19
|
+
events = new EventQueue();
|
|
20
|
+
/** Most ticks one `advance` runs. */
|
|
21
|
+
maxTicksPerFrame;
|
|
22
|
+
systems = [];
|
|
23
|
+
states = [this.events];
|
|
24
|
+
handlers = new Map();
|
|
25
|
+
commands = [];
|
|
26
|
+
cursor = 0;
|
|
27
|
+
tickNow = 0;
|
|
28
|
+
acc = 0;
|
|
29
|
+
inTick = false;
|
|
30
|
+
running = true;
|
|
31
|
+
rate = 1;
|
|
32
|
+
checkpointEvery;
|
|
33
|
+
checkpointLimit;
|
|
34
|
+
checkpoints = new Map();
|
|
35
|
+
tickListeners = new Set();
|
|
36
|
+
changeListeners = new Set();
|
|
37
|
+
constructor(options = {}) {
|
|
38
|
+
this.seed = String(options.seed ?? 0);
|
|
39
|
+
this.hz = Math.round(options.hz ?? 20);
|
|
40
|
+
if (this.hz < 1)
|
|
41
|
+
throw new Error('Simulation: hz must be at least 1');
|
|
42
|
+
this.maxTicksPerFrame = options.maxTicksPerFrame ?? 8;
|
|
43
|
+
this.checkpointEvery = Math.max(1, options.checkpointEvery ?? 200);
|
|
44
|
+
this.checkpointLimit = options.checkpoints ?? 32;
|
|
45
|
+
}
|
|
46
|
+
/** Ticks run so far; also the tick that runs next. */
|
|
47
|
+
get tick() {
|
|
48
|
+
return this.tickNow;
|
|
49
|
+
}
|
|
50
|
+
/** Simulated seconds (for display; logic counts ticks). */
|
|
51
|
+
get time() {
|
|
52
|
+
return this.tickNow / this.hz;
|
|
53
|
+
}
|
|
54
|
+
/** Fraction 0–1 of the way from the last tick to the next one, for interpolating the image. */
|
|
55
|
+
get alpha() {
|
|
56
|
+
return this.acc;
|
|
57
|
+
}
|
|
58
|
+
get paused() {
|
|
59
|
+
return !this.running;
|
|
60
|
+
}
|
|
61
|
+
/** Multiplier of simulated time per real second. */
|
|
62
|
+
get speed() {
|
|
63
|
+
return this.rate;
|
|
64
|
+
}
|
|
65
|
+
/** Does the simulation move on its own (for the frame loop)? Without systems or queued commands nothing can change. */
|
|
66
|
+
get animating() {
|
|
67
|
+
return this.running && this.rate > 0 && (this.systems.length > 0 || this.cursor < this.commands.length);
|
|
68
|
+
}
|
|
69
|
+
/** Converts seconds to whole ticks. */
|
|
70
|
+
ticks(seconds) {
|
|
71
|
+
return Math.round(seconds * this.hz);
|
|
72
|
+
}
|
|
73
|
+
// --- Setup -------------------------------------------------------------------------------------------------------
|
|
74
|
+
/** Adds a system after the ones already registered. Names must be unique. */
|
|
75
|
+
addSystem(name, run) {
|
|
76
|
+
if (this.systems.some((s) => s.name === name))
|
|
77
|
+
throw new Error(`Simulation: system "${name}" is already registered`);
|
|
78
|
+
this.systems.push({ name, run });
|
|
79
|
+
}
|
|
80
|
+
/** Adds a state part; snapshots save the parts in registration order. Names must be unique. */
|
|
81
|
+
addState(state) {
|
|
82
|
+
if (this.states.some((s) => s.name === state.name))
|
|
83
|
+
throw new Error(`Simulation: state "${state.name}" is already registered`);
|
|
84
|
+
this.states.push(state);
|
|
85
|
+
}
|
|
86
|
+
/** Handles commands of one kind. A command without a handler is an error when its tick runs. */
|
|
87
|
+
onCommand(kind, handler) {
|
|
88
|
+
if (this.handlers.has(kind))
|
|
89
|
+
throw new Error(`Simulation: command "${kind}" already has a handler`);
|
|
90
|
+
this.handlers.set(kind, handler);
|
|
91
|
+
}
|
|
92
|
+
/** Called after every tick (also during seeking), e.g. to record hashes. Returns an unsubscribe function. */
|
|
93
|
+
onTick(listener) {
|
|
94
|
+
this.tickListeners.add(listener);
|
|
95
|
+
return () => this.tickListeners.delete(listener);
|
|
96
|
+
}
|
|
97
|
+
/** Called when pause, speed or the tick change from outside the frame (step, seek, restore). */
|
|
98
|
+
onChange(listener) {
|
|
99
|
+
this.changeListeners.add(listener);
|
|
100
|
+
return () => this.changeListeners.delete(listener);
|
|
101
|
+
}
|
|
102
|
+
changed() {
|
|
103
|
+
for (const l of this.changeListeners)
|
|
104
|
+
l();
|
|
105
|
+
}
|
|
106
|
+
// --- Input -------------------------------------------------------------------------------------------------------
|
|
107
|
+
/**
|
|
108
|
+
* Queues a command for `tick` (default: the next tick). Commands of one tick apply in the order they were queued.
|
|
109
|
+
* A command for a tick that already ran is an error. Queued from inside a tick, the default is the tick after it.
|
|
110
|
+
*/
|
|
111
|
+
command(kind, data = {}, tick = this.tickNow) {
|
|
112
|
+
if (this.inTick && tick === this.tickNow)
|
|
113
|
+
tick++;
|
|
114
|
+
if (!Number.isInteger(tick) || tick < this.tickNow)
|
|
115
|
+
throw new Error(`Simulation: command "${kind}" for tick ${tick}, but tick ${this.tickNow} is next`);
|
|
116
|
+
const cmd = { tick, kind, data };
|
|
117
|
+
let i = this.commands.length;
|
|
118
|
+
while (i > this.cursor && this.commands[i - 1].tick > tick)
|
|
119
|
+
i--;
|
|
120
|
+
this.commands.splice(i, 0, cmd);
|
|
121
|
+
// Checkpoints after this tick belong to a log without this command.
|
|
122
|
+
for (const t of [...this.checkpoints.keys()])
|
|
123
|
+
if (t > tick)
|
|
124
|
+
this.checkpoints.delete(t);
|
|
125
|
+
// A queued command can wake a simulation without systems.
|
|
126
|
+
if (!this.inTick)
|
|
127
|
+
this.changed();
|
|
128
|
+
return cmd;
|
|
129
|
+
}
|
|
130
|
+
/** The command log (applied and pending). */
|
|
131
|
+
get commandLog() {
|
|
132
|
+
return this.commands;
|
|
133
|
+
}
|
|
134
|
+
/** A uint32 from the named stream of the seed, keyed by the current tick and the given integers. */
|
|
135
|
+
random(stream, ...keys) {
|
|
136
|
+
return hashInts(streamSeed(this.seed, stream), this.tickNow, ...keys);
|
|
137
|
+
}
|
|
138
|
+
// --- Stepping ----------------------------------------------------------------------------------------------------
|
|
139
|
+
runTick() {
|
|
140
|
+
const t = this.tickNow;
|
|
141
|
+
if (t % this.checkpointEvery === 0 && !this.checkpoints.has(t))
|
|
142
|
+
this.keepCheckpoint(t);
|
|
143
|
+
this.events.beginTick(t);
|
|
144
|
+
this.inTick = true;
|
|
145
|
+
while (this.cursor < this.commands.length && this.commands[this.cursor].tick === t) {
|
|
146
|
+
const cmd = this.commands[this.cursor++];
|
|
147
|
+
const h = this.handlers.get(cmd.kind);
|
|
148
|
+
if (!h)
|
|
149
|
+
throw new Error(`Simulation: no handler for command "${cmd.kind}" (tick ${t})`);
|
|
150
|
+
h(cmd, this);
|
|
151
|
+
}
|
|
152
|
+
for (const s of this.systems)
|
|
153
|
+
s.run(this);
|
|
154
|
+
this.inTick = false;
|
|
155
|
+
this.tickNow = t + 1;
|
|
156
|
+
for (const l of this.tickListeners)
|
|
157
|
+
l(this);
|
|
158
|
+
}
|
|
159
|
+
keepCheckpoint(t) {
|
|
160
|
+
this.checkpoints.set(t, this.snapshot());
|
|
161
|
+
// Keep tick 0 and the newest ones.
|
|
162
|
+
const ticks = [...this.checkpoints.keys()].filter((k) => k !== 0).sort((a, b) => a - b);
|
|
163
|
+
while (ticks.length > this.checkpointLimit)
|
|
164
|
+
this.checkpoints.delete(ticks.shift());
|
|
165
|
+
}
|
|
166
|
+
/** Runs n ticks now, paused or not. */
|
|
167
|
+
step(n = 1) {
|
|
168
|
+
for (let i = 0; i < n; i++)
|
|
169
|
+
this.runTick();
|
|
170
|
+
this.acc = 0;
|
|
171
|
+
if (n > 0)
|
|
172
|
+
this.changed();
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Feeds real time (seconds) from the frame: runs as many whole ticks as fit, at most `maxTicksPerFrame`, and keeps
|
|
176
|
+
* the remainder as `alpha`. Returns the number of ticks run.
|
|
177
|
+
*/
|
|
178
|
+
advance(realSeconds) {
|
|
179
|
+
if (!this.running || realSeconds <= 0 || this.rate <= 0)
|
|
180
|
+
return 0;
|
|
181
|
+
this.acc += realSeconds * this.rate * this.hz;
|
|
182
|
+
let n = Math.floor(this.acc);
|
|
183
|
+
this.acc -= n;
|
|
184
|
+
if (n > this.maxTicksPerFrame)
|
|
185
|
+
n = this.maxTicksPerFrame;
|
|
186
|
+
for (let i = 0; i < n; i++)
|
|
187
|
+
this.runTick();
|
|
188
|
+
return n;
|
|
189
|
+
}
|
|
190
|
+
pause() {
|
|
191
|
+
if (!this.running)
|
|
192
|
+
return;
|
|
193
|
+
this.running = false;
|
|
194
|
+
this.changed();
|
|
195
|
+
}
|
|
196
|
+
play() {
|
|
197
|
+
if (this.running)
|
|
198
|
+
return;
|
|
199
|
+
this.running = true;
|
|
200
|
+
this.changed();
|
|
201
|
+
}
|
|
202
|
+
setSpeed(speed) {
|
|
203
|
+
if (speed === this.rate)
|
|
204
|
+
return;
|
|
205
|
+
this.rate = Math.max(0, speed);
|
|
206
|
+
this.changed();
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Starts the clock over: restores `snapshot` when given (for example one taken before the first tick), makes the
|
|
210
|
+
* state tick 0 and forgets the checkpoints and the command log, which belong to the old clock. For an app that puts
|
|
211
|
+
* tick 0 at a point of its own time and moves it on a long jump (see `Agendas.setOrigin`).
|
|
212
|
+
*/
|
|
213
|
+
rebase(snapshot) {
|
|
214
|
+
if (snapshot)
|
|
215
|
+
this.restore(snapshot);
|
|
216
|
+
this.tickNow = 0;
|
|
217
|
+
this.acc = 0;
|
|
218
|
+
this.checkpoints.clear();
|
|
219
|
+
this.commands.length = 0;
|
|
220
|
+
this.cursor = 0;
|
|
221
|
+
this.changed();
|
|
222
|
+
}
|
|
223
|
+
/** Pauses and runs exactly one tick. */
|
|
224
|
+
stepOnce() {
|
|
225
|
+
this.running = false;
|
|
226
|
+
this.step(1);
|
|
227
|
+
}
|
|
228
|
+
/** Goes to an exact tick: forward by stepping, back by restoring the nearest checkpoint and replaying the log. */
|
|
229
|
+
seek(tick) {
|
|
230
|
+
tick = Math.max(0, Math.floor(tick));
|
|
231
|
+
if (tick < this.tickNow) {
|
|
232
|
+
let best = -1;
|
|
233
|
+
for (const t of this.checkpoints.keys())
|
|
234
|
+
if (t <= tick && t > best)
|
|
235
|
+
best = t;
|
|
236
|
+
if (best < 0)
|
|
237
|
+
throw new Error(`Simulation: no checkpoint at or before tick ${tick}`);
|
|
238
|
+
this.restore(this.checkpoints.get(best));
|
|
239
|
+
}
|
|
240
|
+
for (let t = this.tickNow; t < tick; t++)
|
|
241
|
+
this.runTick();
|
|
242
|
+
this.acc = 0;
|
|
243
|
+
this.changed();
|
|
244
|
+
}
|
|
245
|
+
// --- State -------------------------------------------------------------------------------------------------------
|
|
246
|
+
/** All state as bytes: the tick and every state part in registration order. Commands are input, not state. */
|
|
247
|
+
snapshot() {
|
|
248
|
+
const w = new ByteWriter();
|
|
249
|
+
w.u32(MAGIC);
|
|
250
|
+
w.i32(this.tickNow);
|
|
251
|
+
w.u32(this.states.length);
|
|
252
|
+
for (const s of this.states) {
|
|
253
|
+
const part = new ByteWriter();
|
|
254
|
+
s.save(part);
|
|
255
|
+
w.str(s.name);
|
|
256
|
+
w.u32(part.length);
|
|
257
|
+
w.bytes(part.finish());
|
|
258
|
+
}
|
|
259
|
+
return w.finish();
|
|
260
|
+
}
|
|
261
|
+
/** Restores a snapshot of this simulation (same state parts). Commands for later ticks are replayed from the log. */
|
|
262
|
+
restore(bytes) {
|
|
263
|
+
const r = new ByteReader(bytes);
|
|
264
|
+
if (r.u32() !== MAGIC)
|
|
265
|
+
throw new Error('Simulation: not a snapshot');
|
|
266
|
+
const tick = r.i32();
|
|
267
|
+
const n = r.u32();
|
|
268
|
+
if (n !== this.states.length)
|
|
269
|
+
throw new Error(`Simulation: snapshot has ${n} state parts, the simulation ${this.states.length}`);
|
|
270
|
+
for (const s of this.states) {
|
|
271
|
+
const name = r.str();
|
|
272
|
+
if (name !== s.name)
|
|
273
|
+
throw new Error(`Simulation: snapshot part "${name}" where "${s.name}" was expected`);
|
|
274
|
+
const len = r.u32();
|
|
275
|
+
const part = new Uint8Array(len);
|
|
276
|
+
r.into(part);
|
|
277
|
+
s.load(new ByteReader(part));
|
|
278
|
+
}
|
|
279
|
+
this.tickNow = tick;
|
|
280
|
+
this.acc = 0;
|
|
281
|
+
let c = 0;
|
|
282
|
+
while (c < this.commands.length && this.commands[c].tick < tick)
|
|
283
|
+
c++;
|
|
284
|
+
this.cursor = c;
|
|
285
|
+
this.changed();
|
|
286
|
+
}
|
|
287
|
+
/** Hash of the whole state (16 hex digits). Equal hashes mean equal snapshots. */
|
|
288
|
+
stateHash() {
|
|
289
|
+
return hashBytes(this.snapshot());
|
|
290
|
+
}
|
|
291
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export interface StateDef<C> {
|
|
2
|
+
/** Called every tick while in the state; returns the next state's id, or nothing to stay. */
|
|
3
|
+
update?: (entity: number, ctx: C) => number | void;
|
|
4
|
+
/** Called once when the state is entered. */
|
|
5
|
+
enter?: (entity: number, ctx: C) => void;
|
|
6
|
+
}
|
|
7
|
+
export declare class StateMachine<C> {
|
|
8
|
+
private readonly names;
|
|
9
|
+
private readonly defs;
|
|
10
|
+
/** Adds a state and returns its id (ids count up from 0 in the order of definition). */
|
|
11
|
+
define(name: string, def?: StateDef<C>): number;
|
|
12
|
+
/** Id of a named state. */
|
|
13
|
+
id(name: string): number;
|
|
14
|
+
name(id: number): string;
|
|
15
|
+
get size(): number;
|
|
16
|
+
/** Enters a state: runs its `enter`. The owner stores the returned id. */
|
|
17
|
+
enter(entity: number, state: number, ctx: C): number;
|
|
18
|
+
/**
|
|
19
|
+
* One tick for one entity in `state`: runs `update`, follows the returned state (entering it) up to a few hand-overs
|
|
20
|
+
* in the same tick, and returns the state it ends in.
|
|
21
|
+
*/
|
|
22
|
+
step(entity: number, state: number, ctx: C, onChange?: (from: number, to: number) => void): number;
|
|
23
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// A small state machine for entities: named states with an `update` that returns the next state (or nothing to
|
|
2
|
+
// stay) and an optional `enter`. The state of each entity is a number the owner keeps in its own table, so it is
|
|
3
|
+
// saved with the snapshot; the machine itself is only the code. An app adds states (waiting, boarding, riding,
|
|
4
|
+
// inside) without writing a tick loop of its own: the owner runs `step` for every entity each tick.
|
|
5
|
+
/** Transitions a single `step` may take at most (a chain of states that hand over at once). */
|
|
6
|
+
const MAX_CHAIN = 8;
|
|
7
|
+
export class StateMachine {
|
|
8
|
+
names = [];
|
|
9
|
+
defs = [];
|
|
10
|
+
/** Adds a state and returns its id (ids count up from 0 in the order of definition). */
|
|
11
|
+
define(name, def = {}) {
|
|
12
|
+
if (this.names.includes(name))
|
|
13
|
+
throw new Error(`StateMachine: state "${name}" exists`);
|
|
14
|
+
this.names.push(name);
|
|
15
|
+
this.defs.push(def);
|
|
16
|
+
return this.names.length - 1;
|
|
17
|
+
}
|
|
18
|
+
/** Id of a named state. */
|
|
19
|
+
id(name) {
|
|
20
|
+
const i = this.names.indexOf(name);
|
|
21
|
+
if (i < 0)
|
|
22
|
+
throw new Error(`StateMachine: no state "${name}"`);
|
|
23
|
+
return i;
|
|
24
|
+
}
|
|
25
|
+
name(id) {
|
|
26
|
+
return this.names[id] ?? `?${id}`;
|
|
27
|
+
}
|
|
28
|
+
get size() {
|
|
29
|
+
return this.names.length;
|
|
30
|
+
}
|
|
31
|
+
/** Enters a state: runs its `enter`. The owner stores the returned id. */
|
|
32
|
+
enter(entity, state, ctx) {
|
|
33
|
+
this.defs[state]?.enter?.(entity, ctx);
|
|
34
|
+
return state;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* One tick for one entity in `state`: runs `update`, follows the returned state (entering it) up to a few hand-overs
|
|
38
|
+
* in the same tick, and returns the state it ends in.
|
|
39
|
+
*/
|
|
40
|
+
step(entity, state, ctx, onChange) {
|
|
41
|
+
let s = state;
|
|
42
|
+
for (let i = 0; i < MAX_CHAIN; i++) {
|
|
43
|
+
const def = this.defs[s];
|
|
44
|
+
if (!def?.update)
|
|
45
|
+
return s;
|
|
46
|
+
const next = def.update(entity, ctx);
|
|
47
|
+
if (next === undefined || next === s)
|
|
48
|
+
return s;
|
|
49
|
+
onChange?.(s, next);
|
|
50
|
+
s = this.enter(entity, next, ctx);
|
|
51
|
+
}
|
|
52
|
+
return s;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { ByteReader, ByteWriter } from './bytes.ts';
|
|
2
|
+
import type { SimState } from './simulation.ts';
|
|
3
|
+
export type ColumnType = 'i32' | 'u32' | 'i16' | 'u8' | 'f64';
|
|
4
|
+
export type ColumnSpec = Readonly<Record<string, ColumnType>>;
|
|
5
|
+
interface ArrayOf {
|
|
6
|
+
i32: Int32Array;
|
|
7
|
+
u32: Uint32Array;
|
|
8
|
+
i16: Int16Array;
|
|
9
|
+
u8: Uint8Array;
|
|
10
|
+
f64: Float64Array;
|
|
11
|
+
}
|
|
12
|
+
export type Columns<S extends ColumnSpec> = {
|
|
13
|
+
[K in keyof S]: ArrayOf[S[K]];
|
|
14
|
+
};
|
|
15
|
+
export declare class EntityTable<S extends ColumnSpec> implements SimState {
|
|
16
|
+
readonly name: string;
|
|
17
|
+
readonly spec: S;
|
|
18
|
+
/** The id space of this table; the high part of every id. */
|
|
19
|
+
readonly namespace: number;
|
|
20
|
+
private cols;
|
|
21
|
+
private idCol;
|
|
22
|
+
private rows;
|
|
23
|
+
private n;
|
|
24
|
+
private serial;
|
|
25
|
+
constructor(name: string, spec: S, options?: {
|
|
26
|
+
namespace?: number;
|
|
27
|
+
capacity?: number;
|
|
28
|
+
});
|
|
29
|
+
private alloc;
|
|
30
|
+
private grow;
|
|
31
|
+
/** The column arrays. They are replaced when the table grows, so read them again after `create`. */
|
|
32
|
+
get col(): Columns<S>;
|
|
33
|
+
/** Ids by row; only the first `count` are valid. */
|
|
34
|
+
get ids(): Float64Array;
|
|
35
|
+
get count(): number;
|
|
36
|
+
/** Adds an entity with every column 0 and returns its id. */
|
|
37
|
+
create(): number;
|
|
38
|
+
/** Row of an entity, or -1. */
|
|
39
|
+
rowOf(id: number): number;
|
|
40
|
+
has(id: number): boolean;
|
|
41
|
+
idAt(row: number): number;
|
|
42
|
+
/** The id's serial (the low 32 bits): handy as an integer key for hashing. */
|
|
43
|
+
static serialOf(id: number): number;
|
|
44
|
+
/** Removes an entity; the last row moves into its place. */
|
|
45
|
+
remove(id: number): boolean;
|
|
46
|
+
save(w: ByteWriter): void;
|
|
47
|
+
load(r: ByteReader): void;
|
|
48
|
+
}
|
|
49
|
+
export {};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// Dense entity table for mass entities (Wallhammer Zero, "Three classes of objects"): one typed array per column,
|
|
2
|
+
// rows packed without holes, and an id per row that the table assigns itself. The id is the identity; a row index
|
|
3
|
+
// is only where the entity sits right now and changes when another entity is removed (swap with the last row).
|
|
4
|
+
//
|
|
5
|
+
// Ids are integers below 2^53: `namespace * 2^32 + serial`. The serial counts up from 1 and is never reused, the
|
|
6
|
+
// namespace comes from the table name, so ids of different tables do not collide and 0 means "none".
|
|
7
|
+
import { hashString } from '../gen-core/hash.js';
|
|
8
|
+
const CTOR = { i32: Int32Array, u32: Uint32Array, i16: Int16Array, u8: Uint8Array, f64: Float64Array };
|
|
9
|
+
const SERIAL_SPAN = 2 ** 32;
|
|
10
|
+
/** Namespaces stay below 2^21, so every id is a safe integer. */
|
|
11
|
+
const NAMESPACE_MASK = 0x1fffff;
|
|
12
|
+
export class EntityTable {
|
|
13
|
+
name;
|
|
14
|
+
spec;
|
|
15
|
+
/** The id space of this table; the high part of every id. */
|
|
16
|
+
namespace;
|
|
17
|
+
cols;
|
|
18
|
+
idCol;
|
|
19
|
+
rows = new Map();
|
|
20
|
+
n = 0;
|
|
21
|
+
serial = 0;
|
|
22
|
+
constructor(name, spec, options = {}) {
|
|
23
|
+
this.name = name;
|
|
24
|
+
this.spec = spec;
|
|
25
|
+
this.namespace = (options.namespace ?? hashString(name)) & NAMESPACE_MASK;
|
|
26
|
+
const cap = Math.max(4, options.capacity ?? 64);
|
|
27
|
+
this.idCol = new Float64Array(cap);
|
|
28
|
+
this.cols = this.alloc(cap);
|
|
29
|
+
}
|
|
30
|
+
alloc(cap) {
|
|
31
|
+
const out = {};
|
|
32
|
+
for (const k in this.spec)
|
|
33
|
+
out[k] = new CTOR[this.spec[k]](cap);
|
|
34
|
+
return out;
|
|
35
|
+
}
|
|
36
|
+
grow() {
|
|
37
|
+
const cap = this.idCol.length * 2;
|
|
38
|
+
const next = this.alloc(cap);
|
|
39
|
+
for (const k in this.spec)
|
|
40
|
+
next[k].set(this.cols[k]);
|
|
41
|
+
const ids = new Float64Array(cap);
|
|
42
|
+
ids.set(this.idCol);
|
|
43
|
+
this.cols = next;
|
|
44
|
+
this.idCol = ids;
|
|
45
|
+
}
|
|
46
|
+
/** The column arrays. They are replaced when the table grows, so read them again after `create`. */
|
|
47
|
+
get col() {
|
|
48
|
+
return this.cols;
|
|
49
|
+
}
|
|
50
|
+
/** Ids by row; only the first `count` are valid. */
|
|
51
|
+
get ids() {
|
|
52
|
+
return this.idCol;
|
|
53
|
+
}
|
|
54
|
+
get count() {
|
|
55
|
+
return this.n;
|
|
56
|
+
}
|
|
57
|
+
/** Adds an entity with every column 0 and returns its id. */
|
|
58
|
+
create() {
|
|
59
|
+
if (this.n === this.idCol.length)
|
|
60
|
+
this.grow();
|
|
61
|
+
const id = this.namespace * SERIAL_SPAN + ++this.serial;
|
|
62
|
+
const row = this.n++;
|
|
63
|
+
this.idCol[row] = id;
|
|
64
|
+
for (const k in this.spec)
|
|
65
|
+
this.cols[k][row] = 0;
|
|
66
|
+
this.rows.set(id, row);
|
|
67
|
+
return id;
|
|
68
|
+
}
|
|
69
|
+
/** Row of an entity, or -1. */
|
|
70
|
+
rowOf(id) {
|
|
71
|
+
return this.rows.get(id) ?? -1;
|
|
72
|
+
}
|
|
73
|
+
has(id) {
|
|
74
|
+
return this.rows.has(id);
|
|
75
|
+
}
|
|
76
|
+
idAt(row) {
|
|
77
|
+
return this.idCol[row];
|
|
78
|
+
}
|
|
79
|
+
/** The id's serial (the low 32 bits): handy as an integer key for hashing. */
|
|
80
|
+
static serialOf(id) {
|
|
81
|
+
return id % SERIAL_SPAN;
|
|
82
|
+
}
|
|
83
|
+
/** Removes an entity; the last row moves into its place. */
|
|
84
|
+
remove(id) {
|
|
85
|
+
const row = this.rows.get(id);
|
|
86
|
+
if (row === undefined)
|
|
87
|
+
return false;
|
|
88
|
+
const last = --this.n;
|
|
89
|
+
this.rows.delete(id);
|
|
90
|
+
if (row !== last) {
|
|
91
|
+
const moved = this.idCol[last];
|
|
92
|
+
this.idCol[row] = moved;
|
|
93
|
+
for (const k in this.spec) {
|
|
94
|
+
const a = this.cols[k];
|
|
95
|
+
a[row] = a[last];
|
|
96
|
+
}
|
|
97
|
+
this.rows.set(moved, row);
|
|
98
|
+
}
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
save(w) {
|
|
102
|
+
w.u32(this.n);
|
|
103
|
+
w.f64(this.serial);
|
|
104
|
+
w.bytes(this.idCol.subarray(0, this.n));
|
|
105
|
+
for (const k in this.spec)
|
|
106
|
+
w.bytes(this.cols[k].subarray(0, this.n));
|
|
107
|
+
}
|
|
108
|
+
load(r) {
|
|
109
|
+
const n = r.u32();
|
|
110
|
+
this.serial = r.f64();
|
|
111
|
+
let cap = this.idCol.length;
|
|
112
|
+
while (cap < n)
|
|
113
|
+
cap *= 2;
|
|
114
|
+
if (cap !== this.idCol.length) {
|
|
115
|
+
this.idCol = new Float64Array(cap);
|
|
116
|
+
this.cols = this.alloc(cap);
|
|
117
|
+
}
|
|
118
|
+
this.n = n;
|
|
119
|
+
r.into(this.idCol.subarray(0, n));
|
|
120
|
+
for (const k in this.spec)
|
|
121
|
+
r.into(this.cols[k].subarray(0, n));
|
|
122
|
+
this.rows.clear();
|
|
123
|
+
for (let i = 0; i < n; i++)
|
|
124
|
+
this.rows.set(this.idCol[i], i);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Path } from './paths.ts';
|
|
2
|
+
export interface ZoneIntervals {
|
|
3
|
+
/** Per path id: zone stretches ascending along the path, mm, and the zone of each. */
|
|
4
|
+
byPath: Map<number, {
|
|
5
|
+
s0: Int32Array;
|
|
6
|
+
s1: Int32Array;
|
|
7
|
+
zone: Int32Array;
|
|
8
|
+
}>;
|
|
9
|
+
count: number;
|
|
10
|
+
}
|
|
11
|
+
export declare function buildZoneIntervals(paths: readonly Path[], clearance: number, parallelClearance?: number): ZoneIntervals;
|