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,139 @@
|
|
|
1
|
+
// A structure prepared for decisions: parts in key order, neighbour lists, bounding boxes, and the support solver.
|
|
2
|
+
//
|
|
3
|
+
// Support is connectivity from the ground anchors with a heuristic load path on top: a part is supported when it is
|
|
4
|
+
// an anchor, rests on a supported part (bearing bond), or hangs on a supported part through lateral bonds, at most
|
|
5
|
+
// `maxSpan` lateral bonds away from a part that rests on something (resting on a hanging part inherits its hang;
|
|
6
|
+
// hanging under a part costs two lateral hops).
|
|
7
|
+
// A plain connectivity test is `maxSpan = 255`.
|
|
8
|
+
// The solver is a bucket queue (small-integer shortest paths) seeded from the anchors in key order; the result (the smallest hang of
|
|
9
|
+
// every part) does not depend on the visiting order. A stress solver can replace it later without changing the data.
|
|
10
|
+
import { PART_FLAG } from '../../base/destructible.js';
|
|
11
|
+
import { pieceCentre } from './int-geometry.js';
|
|
12
|
+
/** A part with no supported path. */
|
|
13
|
+
export const UNSUPPORTED = 0xffff;
|
|
14
|
+
const cache = new WeakMap();
|
|
15
|
+
/** Prepares a structure description (cached per description object). Throws on unsorted or dangling data. */
|
|
16
|
+
export function compileStructure(desc) {
|
|
17
|
+
const hit = cache.get(desc);
|
|
18
|
+
if (hit)
|
|
19
|
+
return hit;
|
|
20
|
+
const n = desc.parts.length;
|
|
21
|
+
const index = new Map();
|
|
22
|
+
desc.parts.forEach((p, i) => {
|
|
23
|
+
if (i > 0 && desc.parts[i - 1].key >= p.key)
|
|
24
|
+
throw new Error(`structure ${desc.id}: parts are not sorted by key (${p.key})`);
|
|
25
|
+
index.set(p.key, i);
|
|
26
|
+
});
|
|
27
|
+
const above = Array.from({ length: n }, () => []);
|
|
28
|
+
const below = Array.from({ length: n }, () => []);
|
|
29
|
+
const lateral = Array.from({ length: n }, () => []);
|
|
30
|
+
for (const b of desc.bonds) {
|
|
31
|
+
const ia = index.get(b.a);
|
|
32
|
+
const ib = index.get(b.b);
|
|
33
|
+
if (ia === undefined || ib === undefined)
|
|
34
|
+
throw new Error(`structure ${desc.id}: bond ${b.a}–${b.b} names a missing part`);
|
|
35
|
+
if (b.bearing) {
|
|
36
|
+
above[ia].push(ib);
|
|
37
|
+
below[ib].push(ia);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
lateral[ia].push(ib);
|
|
41
|
+
lateral[ib].push(ia);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const byIndex = (p, q) => p - q;
|
|
45
|
+
for (const l of [above, below, lateral])
|
|
46
|
+
for (const x of l)
|
|
47
|
+
x.sort(byIndex);
|
|
48
|
+
const box = new Int32Array(n * 6);
|
|
49
|
+
const centre = new Int32Array(n * 3);
|
|
50
|
+
const bounds = [Infinity, -Infinity, Infinity, -Infinity, Infinity, -Infinity];
|
|
51
|
+
desc.parts.forEach((p, i) => {
|
|
52
|
+
const c = p.piece.xz;
|
|
53
|
+
let x0 = Infinity;
|
|
54
|
+
let x1 = -Infinity;
|
|
55
|
+
let z0 = Infinity;
|
|
56
|
+
let z1 = -Infinity;
|
|
57
|
+
for (let k = 0; k < c.length; k += 2) {
|
|
58
|
+
x0 = Math.min(x0, c[k]);
|
|
59
|
+
x1 = Math.max(x1, c[k]);
|
|
60
|
+
z0 = Math.min(z0, c[k + 1]);
|
|
61
|
+
z1 = Math.max(z1, c[k + 1]);
|
|
62
|
+
}
|
|
63
|
+
box.set([x0, x1, p.piece.y0, p.piece.y1, z0, z1], i * 6);
|
|
64
|
+
centre.set(pieceCentre(p.piece), i * 3);
|
|
65
|
+
bounds[0] = Math.min(bounds[0], x0);
|
|
66
|
+
bounds[1] = Math.max(bounds[1], x1);
|
|
67
|
+
bounds[2] = Math.min(bounds[2], p.piece.y0);
|
|
68
|
+
bounds[3] = Math.max(bounds[3], p.piece.y1);
|
|
69
|
+
bounds[4] = Math.min(bounds[4], z0);
|
|
70
|
+
bounds[5] = Math.max(bounds[5], z1);
|
|
71
|
+
});
|
|
72
|
+
const span = new Uint16Array(n).fill(UNSUPPORTED);
|
|
73
|
+
const alive = new Uint8Array(n).fill(1);
|
|
74
|
+
const partial = { desc, index, above, below, lateral, span, box, centre, bounds, floating: 0 };
|
|
75
|
+
// Intact hangs with no limit: what the structure already needs to stand.
|
|
76
|
+
const intact = solveSupport(partial, alive, null);
|
|
77
|
+
let floating = 0;
|
|
78
|
+
desc.parts.forEach((p, i) => {
|
|
79
|
+
if (intact[i] === UNSUPPORTED)
|
|
80
|
+
floating++;
|
|
81
|
+
span[i] = intact[i] === UNSUPPORTED ? UNSUPPORTED : Math.max(p.maxSpan, intact[i]);
|
|
82
|
+
});
|
|
83
|
+
const compiled = { ...partial, floating };
|
|
84
|
+
cache.set(desc, compiled);
|
|
85
|
+
return compiled;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Smallest hang of every part (0 = rests on something, UNSUPPORTED = no path to an anchor) for the parts marked in
|
|
89
|
+
* `alive`. `limits` caps the hang per part (the compiled spans); `null` means no cap. Parts that are floating even when
|
|
90
|
+
* intact count as anchors.
|
|
91
|
+
*/
|
|
92
|
+
export function solveSupport(s, alive, limits) {
|
|
93
|
+
const parts = s.desc.parts;
|
|
94
|
+
const n = parts.length;
|
|
95
|
+
const hang = new Uint16Array(n).fill(UNSUPPORTED);
|
|
96
|
+
// Bucket queue by hang: a part resting on another inherits its hang (weight 0), a lateral bond adds one.
|
|
97
|
+
const buckets = [[]];
|
|
98
|
+
for (let i = 0; i < n; i++) {
|
|
99
|
+
const anchor = (parts[i].flags & PART_FLAG.anchor) !== 0 || (limits !== null && limits[i] === UNSUPPORTED);
|
|
100
|
+
if (alive[i] && anchor) {
|
|
101
|
+
hang[i] = 0;
|
|
102
|
+
buckets[0].push(i);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
for (let h = 0; h < buckets.length; h++) {
|
|
106
|
+
const bucket = buckets[h];
|
|
107
|
+
if (!bucket)
|
|
108
|
+
continue;
|
|
109
|
+
for (let k = 0; k < bucket.length; k++) {
|
|
110
|
+
const i = bucket[k];
|
|
111
|
+
if (hang[i] !== h)
|
|
112
|
+
continue;
|
|
113
|
+
for (const j of s.above[i]) {
|
|
114
|
+
if (alive[j] && hang[j] > h) {
|
|
115
|
+
hang[j] = h;
|
|
116
|
+
bucket.push(j);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
const next = h + 1;
|
|
120
|
+
if (next >= UNSUPPORTED)
|
|
121
|
+
continue;
|
|
122
|
+
const relax = (j, to) => {
|
|
123
|
+
if (!alive[j] || hang[j] <= to)
|
|
124
|
+
return;
|
|
125
|
+
if (limits !== null && to > limits[j])
|
|
126
|
+
return;
|
|
127
|
+
hang[j] = to;
|
|
128
|
+
(buckets[to] ??= []).push(j);
|
|
129
|
+
};
|
|
130
|
+
for (const j of s.lateral[i])
|
|
131
|
+
relax(j, next);
|
|
132
|
+
// Hanging under a supported part is a weaker path: it counts as two lateral hops.
|
|
133
|
+
if (next + 1 < UNSUPPORTED)
|
|
134
|
+
for (const j of s.below[i])
|
|
135
|
+
relax(j, next + 1);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return hang;
|
|
139
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { ByteReader, ByteWriter } from './bytes.ts';
|
|
2
|
+
import type { SimState } from './simulation.ts';
|
|
3
|
+
/** A plain value an event or command may carry. */
|
|
4
|
+
export type SimValue = number | string | boolean;
|
|
5
|
+
export type SimData = Readonly<Record<string, SimValue>>;
|
|
6
|
+
export interface SimEvent {
|
|
7
|
+
/** Tick the event was posted in. */
|
|
8
|
+
readonly tick: number;
|
|
9
|
+
/** Dotted name, owned by whoever posts it (`move.arrive`, `damage.hit`, …). */
|
|
10
|
+
readonly kind: string;
|
|
11
|
+
/** Entity the event is about, 0 when none. */
|
|
12
|
+
readonly entity: number;
|
|
13
|
+
readonly data: SimData;
|
|
14
|
+
}
|
|
15
|
+
export declare class EventQueue implements SimState {
|
|
16
|
+
readonly name = "events";
|
|
17
|
+
private now;
|
|
18
|
+
private before;
|
|
19
|
+
private outbox;
|
|
20
|
+
private tickNow;
|
|
21
|
+
/** Events dropped because the app did not drain them in time. */
|
|
22
|
+
dropped: number;
|
|
23
|
+
/** Called by the simulation when a tick starts: this tick's events become last tick's. */
|
|
24
|
+
beginTick(tick: number): void;
|
|
25
|
+
post(kind: string, entity?: number, data?: SimData): void;
|
|
26
|
+
/** Events posted so far in this tick, optionally of one kind. */
|
|
27
|
+
thisTick(kind?: string): readonly SimEvent[];
|
|
28
|
+
/** Events of the previous tick, optionally of one kind. */
|
|
29
|
+
lastTick(kind?: string): readonly SimEvent[];
|
|
30
|
+
/** Everything posted since the last call (for the app, not for systems). */
|
|
31
|
+
drain(): SimEvent[];
|
|
32
|
+
save(w: ByteWriter): void;
|
|
33
|
+
load(r: ByteReader): void;
|
|
34
|
+
}
|
|
35
|
+
/** Writes plain data with its keys sorted, so equal data always gives equal bytes. */
|
|
36
|
+
export declare function writeData(w: ByteWriter, data: SimData): void;
|
|
37
|
+
export declare function readData(r: ByteReader): SimData;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// Event queue of the simulation. Any system posts events (arrivals, departures, anything another system defines,
|
|
2
|
+
// for example damage); systems later in the same tick and every system in the next tick read them, and the app
|
|
3
|
+
// drains everything that happened since its last frame.
|
|
4
|
+
//
|
|
5
|
+
// Events are part of the simulation state: the two ticks a system can see are saved in snapshots and hashed.
|
|
6
|
+
const NO_DATA = Object.freeze({});
|
|
7
|
+
/** Events the app has not drained yet are capped; older ones are dropped and counted. */
|
|
8
|
+
const APP_CAP = 65536;
|
|
9
|
+
export class EventQueue {
|
|
10
|
+
name = 'events';
|
|
11
|
+
now = [];
|
|
12
|
+
before = [];
|
|
13
|
+
outbox = [];
|
|
14
|
+
tickNow = 0;
|
|
15
|
+
/** Events dropped because the app did not drain them in time. */
|
|
16
|
+
dropped = 0;
|
|
17
|
+
/** Called by the simulation when a tick starts: this tick's events become last tick's. */
|
|
18
|
+
beginTick(tick) {
|
|
19
|
+
this.before = this.now;
|
|
20
|
+
this.now = [];
|
|
21
|
+
this.tickNow = tick;
|
|
22
|
+
}
|
|
23
|
+
post(kind, entity = 0, data = NO_DATA) {
|
|
24
|
+
const e = { tick: this.tickNow, kind, entity, data };
|
|
25
|
+
this.now.push(e);
|
|
26
|
+
this.outbox.push(e);
|
|
27
|
+
if (this.outbox.length > APP_CAP) {
|
|
28
|
+
const cut = this.outbox.length - APP_CAP;
|
|
29
|
+
this.outbox.splice(0, cut);
|
|
30
|
+
this.dropped += cut;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/** Events posted so far in this tick, optionally of one kind. */
|
|
34
|
+
thisTick(kind) {
|
|
35
|
+
return kind === undefined ? this.now : this.now.filter((e) => e.kind === kind);
|
|
36
|
+
}
|
|
37
|
+
/** Events of the previous tick, optionally of one kind. */
|
|
38
|
+
lastTick(kind) {
|
|
39
|
+
return kind === undefined ? this.before : this.before.filter((e) => e.kind === kind);
|
|
40
|
+
}
|
|
41
|
+
/** Everything posted since the last call (for the app, not for systems). */
|
|
42
|
+
drain() {
|
|
43
|
+
const out = this.outbox;
|
|
44
|
+
this.outbox = [];
|
|
45
|
+
return out;
|
|
46
|
+
}
|
|
47
|
+
save(w) {
|
|
48
|
+
w.i32(this.tickNow);
|
|
49
|
+
for (const list of [this.before, this.now]) {
|
|
50
|
+
w.u32(list.length);
|
|
51
|
+
for (const e of list) {
|
|
52
|
+
w.i32(e.tick);
|
|
53
|
+
w.str(e.kind);
|
|
54
|
+
w.f64(e.entity);
|
|
55
|
+
writeData(w, e.data);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
load(r) {
|
|
60
|
+
this.tickNow = r.i32();
|
|
61
|
+
const read = () => {
|
|
62
|
+
const n = r.u32();
|
|
63
|
+
const out = [];
|
|
64
|
+
for (let i = 0; i < n; i++)
|
|
65
|
+
out.push({ tick: r.i32(), kind: r.str(), entity: r.f64(), data: readData(r) });
|
|
66
|
+
return out;
|
|
67
|
+
};
|
|
68
|
+
this.before = read();
|
|
69
|
+
this.now = read();
|
|
70
|
+
// Events the app saw before a restore are history of another timeline.
|
|
71
|
+
this.outbox = [];
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/** Writes plain data with its keys sorted, so equal data always gives equal bytes. */
|
|
75
|
+
export function writeData(w, data) {
|
|
76
|
+
const keys = Object.keys(data).sort();
|
|
77
|
+
w.u32(keys.length);
|
|
78
|
+
for (const k of keys) {
|
|
79
|
+
const v = data[k];
|
|
80
|
+
w.str(k);
|
|
81
|
+
if (typeof v === 'number') {
|
|
82
|
+
w.u8(0);
|
|
83
|
+
w.f64(v);
|
|
84
|
+
}
|
|
85
|
+
else if (typeof v === 'string') {
|
|
86
|
+
w.u8(1);
|
|
87
|
+
w.str(v);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
w.u8(2);
|
|
91
|
+
w.u8(v ? 1 : 0);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
export function readData(r) {
|
|
96
|
+
const n = r.u32();
|
|
97
|
+
if (n === 0)
|
|
98
|
+
return NO_DATA;
|
|
99
|
+
const out = {};
|
|
100
|
+
for (let i = 0; i < n; i++) {
|
|
101
|
+
const k = r.str();
|
|
102
|
+
const t = r.u8();
|
|
103
|
+
out[k] = t === 0 ? r.f64() : t === 1 ? r.str() : r.u8() === 1;
|
|
104
|
+
}
|
|
105
|
+
return out;
|
|
106
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** Metres to whole millimetres. */
|
|
2
|
+
export declare function mm(metres: number): number;
|
|
3
|
+
/** Metres rounded to the nearest millimetre, still in metres: how coordinates enter the simulation. */
|
|
4
|
+
export declare function snapMm(metres: number): number;
|
|
5
|
+
/** Metres per second to millimetres per tick. */
|
|
6
|
+
export declare function mmPerTick(metresPerSecond: number, hz: number): number;
|
|
7
|
+
/** Metres per second squared to millimetres per tick squared (at least 1 when positive). */
|
|
8
|
+
export declare function mmPerTick2(metresPerSecond2: number, hz: number): number;
|
|
9
|
+
/** Floor of the square root of a non-negative integer below 2^53. */
|
|
10
|
+
export declare function isqrt(n: number): number;
|
|
11
|
+
/** Integer length of a vector given in millimetres. */
|
|
12
|
+
export declare function lengthMm(dx: number, dz: number): number;
|
|
13
|
+
/**
|
|
14
|
+
* Distance covered from speed v until standing, braking by `decel` each tick, counting this tick's move:
|
|
15
|
+
* v + (v − a) + (v − 2a) + … over the positive terms.
|
|
16
|
+
*/
|
|
17
|
+
export declare function stoppingDistance(v: number, decel: number): number;
|
|
18
|
+
/** The highest speed (mm/tick) from which the entity can still stop within `d` millimetres. */
|
|
19
|
+
export declare function brakeSpeed(d: number, decel: number): number;
|
|
20
|
+
/** The highest speed from which braking reaches `limit` within `d` millimetres: v² = limit² + 2·a·d. */
|
|
21
|
+
export declare function approachSpeed(limit: number, d: number, decel: number): number;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Integer units of the simulation. Positions along paths are whole millimetres, speeds millimetres per tick and
|
|
2
|
+
// accelerations millimetres per tick squared, so movement logic is exact integer arithmetic (tier A). Only `Math.sqrt`
|
|
3
|
+
// is used, which IEEE 754 defines exactly; transcendental functions are not.
|
|
4
|
+
//
|
|
5
|
+
// The app speaks metres and seconds; these helpers convert once, at the boundary, with explicit rounding.
|
|
6
|
+
/** Metres to whole millimetres. */
|
|
7
|
+
export function mm(metres) {
|
|
8
|
+
return Math.round(metres * 1000);
|
|
9
|
+
}
|
|
10
|
+
/** Metres rounded to the nearest millimetre, still in metres: how coordinates enter the simulation. */
|
|
11
|
+
export function snapMm(metres) {
|
|
12
|
+
return Math.round(metres * 1000) / 1000;
|
|
13
|
+
}
|
|
14
|
+
/** Metres per second to millimetres per tick. */
|
|
15
|
+
export function mmPerTick(metresPerSecond, hz) {
|
|
16
|
+
return Math.round((metresPerSecond * 1000) / hz);
|
|
17
|
+
}
|
|
18
|
+
/** Metres per second squared to millimetres per tick squared (at least 1 when positive). */
|
|
19
|
+
export function mmPerTick2(metresPerSecond2, hz) {
|
|
20
|
+
if (metresPerSecond2 <= 0)
|
|
21
|
+
return 0;
|
|
22
|
+
return Math.max(1, Math.round((metresPerSecond2 * 1000) / (hz * hz)));
|
|
23
|
+
}
|
|
24
|
+
/** Floor of the square root of a non-negative integer below 2^53. */
|
|
25
|
+
export function isqrt(n) {
|
|
26
|
+
if (n <= 0)
|
|
27
|
+
return 0;
|
|
28
|
+
let r = Math.floor(Math.sqrt(n));
|
|
29
|
+
while (r * r > n)
|
|
30
|
+
r--;
|
|
31
|
+
while ((r + 1) * (r + 1) <= n)
|
|
32
|
+
r++;
|
|
33
|
+
return r;
|
|
34
|
+
}
|
|
35
|
+
/** Integer length of a vector given in millimetres. */
|
|
36
|
+
export function lengthMm(dx, dz) {
|
|
37
|
+
return Math.round(Math.sqrt(dx * dx + dz * dz));
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Distance covered from speed v until standing, braking by `decel` each tick, counting this tick's move:
|
|
41
|
+
* v + (v − a) + (v − 2a) + … over the positive terms.
|
|
42
|
+
*/
|
|
43
|
+
export function stoppingDistance(v, decel) {
|
|
44
|
+
if (v <= 0)
|
|
45
|
+
return 0;
|
|
46
|
+
const k = Math.floor(v / decel);
|
|
47
|
+
return (k + 1) * v - (decel * k * (k + 1)) / 2;
|
|
48
|
+
}
|
|
49
|
+
/** The highest speed (mm/tick) from which the entity can still stop within `d` millimetres. */
|
|
50
|
+
export function brakeSpeed(d, decel) {
|
|
51
|
+
if (d <= 0)
|
|
52
|
+
return 0;
|
|
53
|
+
// Continuous estimate v(v + a) / 2a = d, then corrected to the exact discrete profile.
|
|
54
|
+
let v = Math.floor((isqrt(decel * decel + 8 * decel * d) - decel) / 2);
|
|
55
|
+
while (v > 0 && stoppingDistance(v, decel) > d)
|
|
56
|
+
v--;
|
|
57
|
+
while (stoppingDistance(v + 1, decel) <= d)
|
|
58
|
+
v++;
|
|
59
|
+
return v;
|
|
60
|
+
}
|
|
61
|
+
/** The highest speed from which braking reaches `limit` within `d` millimetres: v² = limit² + 2·a·d. */
|
|
62
|
+
export function approachSpeed(limit, d, decel) {
|
|
63
|
+
return isqrt(limit * limit + 2 * decel * Math.max(0, d));
|
|
64
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { FlowNetwork } from './flow.ts';
|
|
2
|
+
import type { FlowCensus } from './flow.ts';
|
|
3
|
+
import { Simulation } from './simulation.ts';
|
|
4
|
+
export interface FlowProbeOptions {
|
|
5
|
+
seed?: string;
|
|
6
|
+
/** Nodes per side of the lattice. Default 48. */
|
|
7
|
+
side?: number;
|
|
8
|
+
/** Distance between neighbouring nodes, metres. Default 90. */
|
|
9
|
+
step?: number;
|
|
10
|
+
/** Trips per network (there are two). Default 20000. */
|
|
11
|
+
trips?: number;
|
|
12
|
+
/** Clock seconds over which the trips depart, from 0. Default 3600. */
|
|
13
|
+
span?: number;
|
|
14
|
+
/** Clock second of tick 0; trips that left earlier are set up under way. Default 600. */
|
|
15
|
+
origin?: number;
|
|
16
|
+
/** Seconds simulated. Default 3000. */
|
|
17
|
+
seconds?: number;
|
|
18
|
+
/** Ticks per second. Default 10. */
|
|
19
|
+
hz?: number;
|
|
20
|
+
/** Edges a mover's path looks ahead (`FlowOptions.pathEdges`); a small number makes movers change paths often. Default: the network's. */
|
|
21
|
+
pathEdges?: number;
|
|
22
|
+
}
|
|
23
|
+
export interface FlowProbeResult {
|
|
24
|
+
/** State hash of the simulation at the end. */
|
|
25
|
+
hash: string;
|
|
26
|
+
tick: number;
|
|
27
|
+
seconds: number;
|
|
28
|
+
/** Per network: the census at the end, hand-overs both ways and the most individual movers at once. */
|
|
29
|
+
nets: {
|
|
30
|
+
name: string;
|
|
31
|
+
census: FlowCensus;
|
|
32
|
+
spawned: number;
|
|
33
|
+
merged: number;
|
|
34
|
+
peak: number;
|
|
35
|
+
}[];
|
|
36
|
+
/** Route search: regions, gates, searches and cache hits over both networks. */
|
|
37
|
+
routes: {
|
|
38
|
+
regions: number;
|
|
39
|
+
gates: number;
|
|
40
|
+
searches: number;
|
|
41
|
+
hits: number;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
export interface FlowProbe {
|
|
45
|
+
readonly sim: Simulation;
|
|
46
|
+
/** The two networks: vehicles, walkers. */
|
|
47
|
+
readonly nets: readonly FlowNetwork[];
|
|
48
|
+
/** Ticks of the whole run. */
|
|
49
|
+
readonly ticks: number;
|
|
50
|
+
/** Sets the networks up, at most `units` pieces of work per network; returns the pieces left (0: ready). */
|
|
51
|
+
prepare(units?: number): number;
|
|
52
|
+
/** Runs at most `maxTicks` ticks and returns true when the run is over. Throws when a count does not add up. */
|
|
53
|
+
advance(maxTicks?: number): boolean;
|
|
54
|
+
result(): FlowProbeResult;
|
|
55
|
+
}
|
|
56
|
+
export declare function createFlowProbe(options?: FlowProbeOptions): FlowProbe;
|
|
57
|
+
/** One line of a probe result for a log: the hash first, then the counts that explain it. */
|
|
58
|
+
export declare function flowProbeLine(r: FlowProbeResult): string;
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// A reference run of the flow model, the same on every machine: a lattice network made from a seed, two flow
|
|
2
|
+
// networks on it (vehicles that follow each other on lanes, walkers that avoid each other), tens of thousands of trips,
|
|
3
|
+
// and a scripted focus area that pans, jumps, disappears and comes back. It starts in the middle of the departures, so
|
|
4
|
+
// the set-up at a clock time is part of it. The run checks the conservation rule as it goes and ends in a state hash.
|
|
5
|
+
//
|
|
6
|
+
// Two uses: the golden test runs it headless, and an app can run it in the browser in slices (`prepare`, `advance`)
|
|
7
|
+
// and print the same line (`flowProbeLine`), so the two machines can be compared.
|
|
8
|
+
import { hashInts, streamSeed } from '../gen-core/hash.js';
|
|
9
|
+
import { lengthMm } from './fixed.js';
|
|
10
|
+
import { FlowNetwork } from './flow.js';
|
|
11
|
+
import { NavGraph } from './nav.js';
|
|
12
|
+
import { RegionNav } from './regions.js';
|
|
13
|
+
import { Simulation } from './simulation.js';
|
|
14
|
+
/** Ticks between two checks of the conservation rule. */
|
|
15
|
+
const CHECK_EVERY = 100;
|
|
16
|
+
export function createFlowProbe(options = {}) {
|
|
17
|
+
const seed = options.seed ?? 'flow-probe';
|
|
18
|
+
const side = Math.max(4, Math.floor(options.side ?? 48));
|
|
19
|
+
const step = Math.max(20, Math.floor(options.step ?? 90));
|
|
20
|
+
const trips = Math.max(1, Math.floor(options.trips ?? 20000));
|
|
21
|
+
const span = Math.max(1, Math.floor(options.span ?? 3600));
|
|
22
|
+
const origin = Math.floor(options.origin ?? 600);
|
|
23
|
+
const seconds = Math.max(1, Math.floor(options.seconds ?? 3000));
|
|
24
|
+
const hz = Math.max(1, Math.floor(options.hz ?? 10));
|
|
25
|
+
const ahead = options.pathEdges !== undefined ? { pathEdges: options.pathEdges } : {};
|
|
26
|
+
const sim = new Simulation({ seed, hz, checkpointEvery: 600 * hz, checkpoints: 4 });
|
|
27
|
+
// The lattice: nodes shifted by up to a fifth of the step, every sixth line a wide and fast one that is never cut.
|
|
28
|
+
const lattice = streamSeed(seed, 'lattice');
|
|
29
|
+
const nodes = [];
|
|
30
|
+
const xs = [];
|
|
31
|
+
const zs = [];
|
|
32
|
+
const shift = Math.floor((step * 1000) / 5);
|
|
33
|
+
for (let j = 0; j < side; j++) {
|
|
34
|
+
for (let i = 0; i < side; i++) {
|
|
35
|
+
const x = i * step * 1000 + (hashInts(lattice, i, j, 0) % shift);
|
|
36
|
+
const z = j * step * 1000 + (hashInts(lattice, i, j, 1) % shift);
|
|
37
|
+
xs.push(x);
|
|
38
|
+
zs.push(z);
|
|
39
|
+
nodes.push(x / 1000, z / 1000);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const driveEdges = [];
|
|
43
|
+
const driveLanes = [];
|
|
44
|
+
const walkEdges = [];
|
|
45
|
+
let serial = 0;
|
|
46
|
+
const link = (a, b, major) => {
|
|
47
|
+
const k = serial++;
|
|
48
|
+
const len = Math.max(1, lengthMm(xs[b] - xs[a], zs[b] - zs[a]));
|
|
49
|
+
if (major || hashInts(lattice, k, 2) % 100 >= 12) {
|
|
50
|
+
// Cost is travel time: the fast lines count 4/7 of their length.
|
|
51
|
+
driveEdges.push({ from: a, to: b, both: true, speed: major ? 14 : 8, cost: major ? Math.max(1, Math.round((len * 4) / 7)) : len });
|
|
52
|
+
driveLanes.push(major ? 2 : 1, major ? 2 : 1);
|
|
53
|
+
}
|
|
54
|
+
if (major || hashInts(lattice, k, 3) % 100 >= 8)
|
|
55
|
+
walkEdges.push({ from: a, to: b, both: true });
|
|
56
|
+
};
|
|
57
|
+
for (let j = 0; j < side; j++) {
|
|
58
|
+
for (let i = 0; i < side; i++) {
|
|
59
|
+
const a = j * side + i;
|
|
60
|
+
if (i + 1 < side)
|
|
61
|
+
link(a, a + 1, j % 6 === 0);
|
|
62
|
+
if (j + 1 < side)
|
|
63
|
+
link(a, a + side, i % 6 === 0);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const count = side * side;
|
|
67
|
+
const driveNav = new NavGraph('probe.drive', { nodes, edges: driveEdges });
|
|
68
|
+
const walkNav = new NavGraph('probe.walk', { nodes, edges: walkEdges });
|
|
69
|
+
const driveRoutes = new RegionNav(driveNav, { cell: step * 8 });
|
|
70
|
+
const walkRoutes = new RegionNav(walkNav, { cell: step * 8 });
|
|
71
|
+
const vs = streamSeed(seed, 'vehicles');
|
|
72
|
+
const ws = streamSeed(seed, 'walkers');
|
|
73
|
+
const vehicles = new FlowNetwork('probe.vehicles', {
|
|
74
|
+
nav: driveNav, routes: driveRoutes, hz, origin, speed: 14, spacing: 7.5, outflow: 0.5, lanes: driveLanes, patience: 90, horizon: 3600,
|
|
75
|
+
collision: 'follow', width: 1.8, rightOffset: 1.6, ...ahead,
|
|
76
|
+
spawn: (trip) => ({ cruise: 9 + (hashInts(vs, trip, 2) % 5), accel: 1.6, decel: 2.5, gap: 8 }),
|
|
77
|
+
});
|
|
78
|
+
const walkers = new FlowNetwork('probe.walkers', {
|
|
79
|
+
nav: walkNav, routes: walkRoutes, hz, origin, speed: 1.4, spacing: 0.8, outflow: 1.2, lanes: 3, patience: 60, horizon: 3600,
|
|
80
|
+
collision: 'agents', width: 0.5, rightOffset: 1, ...ahead,
|
|
81
|
+
spawn: (trip) => ({ cruise: 1.1 + (hashInts(ws, trip, 2) % 5) / 10, accel: 1.2, decel: 2, gap: 0.8, front: 0.25, back: 0.25, width: 0.5 }),
|
|
82
|
+
});
|
|
83
|
+
for (let i = 0; i < trips; i++) {
|
|
84
|
+
const at = Math.floor((i * span * hz) / trips) / hz;
|
|
85
|
+
vehicles.add({ from: hashInts(vs, i, 0) % count, to: hashInts(vs, i, 1) % count, at, tag: i });
|
|
86
|
+
// Walks are short: the goal lies within six lattice steps of the start.
|
|
87
|
+
const from = hashInts(ws, i, 0) % count;
|
|
88
|
+
const ti = Math.min(side - 1, Math.max(0, (from % side) + (hashInts(ws, i, 1) % 13) - 6));
|
|
89
|
+
const tj = Math.min(side - 1, Math.max(0, Math.floor(from / side) + (hashInts(ws, i, 3) % 13) - 6));
|
|
90
|
+
walkers.add({ from, to: tj * side + ti, at, tag: i });
|
|
91
|
+
}
|
|
92
|
+
const nets = [vehicles, walkers];
|
|
93
|
+
for (const net of nets)
|
|
94
|
+
net.attach(sim);
|
|
95
|
+
// The focus script: a pan, a jump and a second pan, a minute without a focus, then a wide focus that stays.
|
|
96
|
+
const total = seconds * hz;
|
|
97
|
+
const width = (side - 1) * step;
|
|
98
|
+
const third = Math.floor(seconds / 3);
|
|
99
|
+
const focus = (second, x, z, radius) => {
|
|
100
|
+
for (const net of nets)
|
|
101
|
+
sim.command(`${net.name}.focus`, { x, z, radius }, second * hz);
|
|
102
|
+
};
|
|
103
|
+
for (let s = 0; s < third; s += 15)
|
|
104
|
+
focus(s, Math.floor(width * 0.3) + s * 3, Math.floor(width * 0.3) + Math.floor((s * 3) / 2), 450);
|
|
105
|
+
for (let s = third; s < 2 * third; s += 15)
|
|
106
|
+
focus(s, Math.floor(width * 0.8) - (s - third) * 2, Math.floor(width * 0.2) + Math.floor(((s - third) * 5) / 2), 450);
|
|
107
|
+
focus(2 * third, 0, 0, 0);
|
|
108
|
+
if (2 * third + 60 < seconds)
|
|
109
|
+
focus(2 * third + 60, Math.floor(width / 2), Math.floor(width / 2), 600);
|
|
110
|
+
const peak = [0, 0];
|
|
111
|
+
const verify = () => {
|
|
112
|
+
nets.forEach((net, i) => {
|
|
113
|
+
const wrong = net.check();
|
|
114
|
+
if (wrong)
|
|
115
|
+
throw new Error(`flow probe: ${net.name} at tick ${sim.tick}: ${wrong}`);
|
|
116
|
+
peak[i] = Math.max(peak[i], net.movers.count);
|
|
117
|
+
});
|
|
118
|
+
};
|
|
119
|
+
return {
|
|
120
|
+
sim,
|
|
121
|
+
nets,
|
|
122
|
+
ticks: total,
|
|
123
|
+
prepare(units = Infinity) {
|
|
124
|
+
let left = 0;
|
|
125
|
+
for (const net of nets)
|
|
126
|
+
left += net.prepare(units);
|
|
127
|
+
return left;
|
|
128
|
+
},
|
|
129
|
+
advance(maxTicks = Infinity) {
|
|
130
|
+
let n = 0;
|
|
131
|
+
while (sim.tick < total && n < maxTicks) {
|
|
132
|
+
const chunk = Math.min(total - sim.tick, maxTicks - n, CHECK_EVERY - (sim.tick % CHECK_EVERY));
|
|
133
|
+
sim.step(chunk);
|
|
134
|
+
n += chunk;
|
|
135
|
+
if (sim.tick % CHECK_EVERY === 0 || sim.tick === total)
|
|
136
|
+
verify();
|
|
137
|
+
}
|
|
138
|
+
return sim.tick >= total;
|
|
139
|
+
},
|
|
140
|
+
result() {
|
|
141
|
+
return {
|
|
142
|
+
hash: sim.stateHash(),
|
|
143
|
+
tick: sim.tick,
|
|
144
|
+
seconds: sim.tick / hz,
|
|
145
|
+
nets: nets.map((net, i) => ({ name: net.name, census: net.census(), spawned: net.spawned, merged: net.merged, peak: peak[i] })),
|
|
146
|
+
routes: {
|
|
147
|
+
regions: driveRoutes.regionCount + walkRoutes.regionCount, gates: driveRoutes.gateCount + walkRoutes.gateCount,
|
|
148
|
+
searches: driveRoutes.searches + walkRoutes.searches, hits: driveRoutes.hits + walkRoutes.hits,
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
/** One line of a probe result for a log: the hash first, then the counts that explain it. */
|
|
155
|
+
export function flowProbeLine(r) {
|
|
156
|
+
const nets = r.nets.map((n) => `${n.name} arrived ${n.census.arrived}/${n.census.trips} under way ${n.census.load} individual ${n.census.individual} out ${n.spawned} back ${n.merged}`).join('; ');
|
|
157
|
+
return `flow ${r.hash} tick ${r.tick} (${r.seconds} s); ${nets}; routes ${r.routes.searches} searched ${r.routes.hits} cached`;
|
|
158
|
+
}
|