@arraypress/effects-astro 0.0.0-stage → 0.1.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/AGENTS.md +85 -0
- package/COMPONENTS.md +1264 -0
- package/LICENSE +139 -0
- package/README.md +197 -2
- package/package.json +60 -4
- package/src/components/AsciiRain.astro +36 -0
- package/src/components/BorderSweep.astro +39 -0
- package/src/components/Caustics.astro +39 -0
- package/src/components/ColorPanels.astro +62 -0
- package/src/components/Cursor.astro +97 -0
- package/src/components/DotGrid.astro +34 -0
- package/src/components/DotSurface.astro +35 -0
- package/src/components/EffectCanvas.astro +152 -0
- package/src/components/FitText.astro +35 -0
- package/src/components/FlutedGlass.astro +36 -0
- package/src/components/Glass.astro +69 -0
- package/src/components/GlassGlyph.astro +38 -0
- package/src/components/GlowField.astro +177 -0
- package/src/components/Grain.astro +98 -0
- package/src/components/HaloRing.astro +38 -0
- package/src/components/HighlightSweep.astro +74 -0
- package/src/components/ImageRipple.astro +56 -0
- package/src/components/Ink.astro +37 -0
- package/src/components/InkField.astro +45 -0
- package/src/components/IsoCity.astro +54 -0
- package/src/components/LightField.astro +40 -0
- package/src/components/LiquidMetal.astro +41 -0
- package/src/components/Magnetic.astro +52 -0
- package/src/components/Marquee.astro +216 -0
- package/src/components/MosaicReveal.astro +196 -0
- package/src/components/NebulaLogo.astro +38 -0
- package/src/components/NeonSwarm.astro +38 -0
- package/src/components/Ocean.astro +40 -0
- package/src/components/OutlineWordmark.astro +222 -0
- package/src/components/PaperBackground.astro +40 -0
- package/src/components/PaperShader.astro +55 -0
- package/src/components/Parallax.astro +49 -0
- package/src/components/ParticleLogo.astro +37 -0
- package/src/components/PixelGlitchText.astro +70 -0
- package/src/components/PointCloud.astro +37 -0
- package/src/components/ProductStack.astro +298 -0
- package/src/components/PulseMark.astro +189 -0
- package/src/components/RadiantGlass.astro +38 -0
- package/src/components/RainOnGlass.astro +71 -0
- package/src/components/Reveal.astro +115 -0
- package/src/components/ScrambleText.astro +52 -0
- package/src/components/ScrollReveal.astro +38 -0
- package/src/components/ShaderBackground.astro +55 -0
- package/src/components/SignalLines.astro +41 -0
- package/src/components/SilkRibbon.astro +37 -0
- package/src/components/SpotlightText.astro +131 -0
- package/src/components/Tilt.astro +79 -0
- package/src/components/Topography.astro +38 -0
- package/src/components/Typewriter.astro +66 -0
- package/src/components/VariableType.astro +59 -0
- package/src/components/VoxelLogo.astro +46 -0
- package/src/components/WaveBars.astro +41 -0
- package/src/components/WorldRoutes.astro +41 -0
- package/src/components/style.js +19 -0
- package/src/index.d.ts +143 -0
- package/src/index.js +105 -0
- package/src/runtime/ascii-rain.d.ts +53 -0
- package/src/runtime/ascii-rain.js +355 -0
- package/src/runtime/audio-meter.d.ts +65 -0
- package/src/runtime/audio-meter.js +282 -0
- package/src/runtime/audio.d.ts +26 -0
- package/src/runtime/audio.js +453 -0
- package/src/runtime/blur.d.ts +2 -0
- package/src/runtime/blur.js +56 -0
- package/src/runtime/canvas2d.d.ts +23 -0
- package/src/runtime/canvas2d.js +105 -0
- package/src/runtime/caustics.d.ts +40 -0
- package/src/runtime/caustics.js +286 -0
- package/src/runtime/color.d.ts +33 -0
- package/src/runtime/color.js +213 -0
- package/src/runtime/curl.d.ts +5 -0
- package/src/runtime/curl.js +81 -0
- package/src/runtime/cursor.d.ts +9 -0
- package/src/runtime/cursor.js +80 -0
- package/src/runtime/device.d.ts +14 -0
- package/src/runtime/device.js +73 -0
- package/src/runtime/dot-grid.d.ts +27 -0
- package/src/runtime/dot-grid.js +245 -0
- package/src/runtime/dot-surface.d.ts +46 -0
- package/src/runtime/dot-surface.js +429 -0
- package/src/runtime/feedback.d.ts +18 -0
- package/src/runtime/feedback.js +131 -0
- package/src/runtime/fit-text.d.ts +5 -0
- package/src/runtime/fit-text.js +60 -0
- package/src/runtime/fluid.d.ts +55 -0
- package/src/runtime/fluid.js +504 -0
- package/src/runtime/fluted-glass.d.ts +27 -0
- package/src/runtime/fluted-glass.js +157 -0
- package/src/runtime/gl.d.ts +19 -0
- package/src/runtime/gl.js +104 -0
- package/src/runtime/glass-glyph.d.ts +49 -0
- package/src/runtime/glass-glyph.js +355 -0
- package/src/runtime/glass-sheen.d.ts +2 -0
- package/src/runtime/glass-sheen.js +30 -0
- package/src/runtime/glsl.d.ts +17 -0
- package/src/runtime/glsl.js +78 -0
- package/src/runtime/glyphs.d.ts +22 -0
- package/src/runtime/glyphs.js +107 -0
- package/src/runtime/halo-ring.d.ts +47 -0
- package/src/runtime/halo-ring.js +334 -0
- package/src/runtime/height-field.d.ts +50 -0
- package/src/runtime/height-field.js +176 -0
- package/src/runtime/image-ripple.d.ts +25 -0
- package/src/runtime/image-ripple.js +353 -0
- package/src/runtime/image.d.ts +11 -0
- package/src/runtime/image.js +81 -0
- package/src/runtime/in-view.d.ts +6 -0
- package/src/runtime/in-view.js +36 -0
- package/src/runtime/ink-field/shaders.js +258 -0
- package/src/runtime/ink-field/sim.js +582 -0
- package/src/runtime/ink-field.d.ts +39 -0
- package/src/runtime/ink-field.js +311 -0
- package/src/runtime/ink.d.ts +31 -0
- package/src/runtime/ink.js +109 -0
- package/src/runtime/instancing.d.ts +12 -0
- package/src/runtime/instancing.js +37 -0
- package/src/runtime/iso-camera.d.ts +35 -0
- package/src/runtime/iso-camera.js +85 -0
- package/src/runtime/iso-city/figures.d.ts +30 -0
- package/src/runtime/iso-city/figures.js +136 -0
- package/src/runtime/iso-city/look.d.ts +34 -0
- package/src/runtime/iso-city/look.js +105 -0
- package/src/runtime/iso-city/plan.d.ts +59 -0
- package/src/runtime/iso-city/plan.js +463 -0
- package/src/runtime/iso-city/roads.d.ts +64 -0
- package/src/runtime/iso-city/roads.js +320 -0
- package/src/runtime/iso-city/scene.d.ts +27 -0
- package/src/runtime/iso-city/scene.js +432 -0
- package/src/runtime/iso-city/shaders.d.ts +6 -0
- package/src/runtime/iso-city/shaders.js +233 -0
- package/src/runtime/iso-city/shadows.d.ts +13 -0
- package/src/runtime/iso-city/shadows.js +148 -0
- package/src/runtime/iso-city/traffic.d.ts +80 -0
- package/src/runtime/iso-city/traffic.js +457 -0
- package/src/runtime/iso-city.d.ts +37 -0
- package/src/runtime/iso-city.js +458 -0
- package/src/runtime/lens-map.d.ts +11 -0
- package/src/runtime/lens-map.js +146 -0
- package/src/runtime/light-field.d.ts +29 -0
- package/src/runtime/light-field.js +292 -0
- package/src/runtime/liquid-glass.d.ts +48 -0
- package/src/runtime/liquid-glass.js +423 -0
- package/src/runtime/liquid-metal/beads.js +217 -0
- package/src/runtime/liquid-metal/field.js +88 -0
- package/src/runtime/liquid-metal/look.js +64 -0
- package/src/runtime/liquid-metal/shaders.js +255 -0
- package/src/runtime/liquid-metal/wave.js +53 -0
- package/src/runtime/liquid-metal.d.ts +46 -0
- package/src/runtime/liquid-metal.js +366 -0
- package/src/runtime/loop.d.ts +7 -0
- package/src/runtime/loop.js +112 -0
- package/src/runtime/magnetic.d.ts +9 -0
- package/src/runtime/magnetic.js +46 -0
- package/src/runtime/marquee.d.ts +2 -0
- package/src/runtime/marquee.js +27 -0
- package/src/runtime/mesh.d.ts +30 -0
- package/src/runtime/mesh.js +217 -0
- package/src/runtime/mosaic-reveal.d.ts +34 -0
- package/src/runtime/mosaic-reveal.js +504 -0
- package/src/runtime/motion.d.ts +2 -0
- package/src/runtime/motion.js +21 -0
- package/src/runtime/mount.d.ts +13 -0
- package/src/runtime/mount.js +120 -0
- package/src/runtime/nebula-logo.d.ts +31 -0
- package/src/runtime/nebula-logo.js +282 -0
- package/src/runtime/neon-swarm.d.ts +31 -0
- package/src/runtime/neon-swarm.js +258 -0
- package/src/runtime/ocean/presets.d.ts +54 -0
- package/src/runtime/ocean/presets.js +184 -0
- package/src/runtime/ocean/shader.d.ts +14 -0
- package/src/runtime/ocean/shader.js +389 -0
- package/src/runtime/ocean/view.d.ts +24 -0
- package/src/runtime/ocean/view.js +98 -0
- package/src/runtime/ocean/waves.d.ts +29 -0
- package/src/runtime/ocean/waves.js +119 -0
- package/src/runtime/ocean.d.ts +31 -0
- package/src/runtime/ocean.js +173 -0
- package/src/runtime/palettes.d.ts +37 -0
- package/src/runtime/palettes.js +75 -0
- package/src/runtime/paper-look.d.ts +42 -0
- package/src/runtime/paper-look.js +91 -0
- package/src/runtime/paper-looks.d.ts +17 -0
- package/src/runtime/paper-looks.js +21 -0
- package/src/runtime/paper.d.ts +37 -0
- package/src/runtime/paper.js +285 -0
- package/src/runtime/particle-logo.d.ts +31 -0
- package/src/runtime/particle-logo.js +196 -0
- package/src/runtime/ping-pong.d.ts +43 -0
- package/src/runtime/ping-pong.js +175 -0
- package/src/runtime/pixel-glitch.d.ts +17 -0
- package/src/runtime/pixel-glitch.js +287 -0
- package/src/runtime/point-cloud.d.ts +33 -0
- package/src/runtime/point-cloud.js +287 -0
- package/src/runtime/pointer.d.ts +27 -0
- package/src/runtime/pointer.js +128 -0
- package/src/runtime/points.d.ts +25 -0
- package/src/runtime/points.js +130 -0
- package/src/runtime/pools.d.ts +2 -0
- package/src/runtime/pools.js +25 -0
- package/src/runtime/product-stack.d.ts +24 -0
- package/src/runtime/product-stack.js +210 -0
- package/src/runtime/radiant-glass.d.ts +29 -0
- package/src/runtime/radiant-glass.js +282 -0
- package/src/runtime/rain-on-glass/backdrop.d.ts +22 -0
- package/src/runtime/rain-on-glass/backdrop.js +185 -0
- package/src/runtime/rain-on-glass/shader.d.ts +2 -0
- package/src/runtime/rain-on-glass/shader.js +168 -0
- package/src/runtime/rain-on-glass/wipe.d.ts +16 -0
- package/src/runtime/rain-on-glass/wipe.js +65 -0
- package/src/runtime/rain-on-glass.d.ts +27 -0
- package/src/runtime/rain-on-glass.js +210 -0
- package/src/runtime/ramp.d.ts +2 -0
- package/src/runtime/ramp.js +30 -0
- package/src/runtime/random.d.ts +2 -0
- package/src/runtime/random.js +22 -0
- package/src/runtime/scroll-reveal.d.ts +22 -0
- package/src/runtime/scroll-reveal.js +73 -0
- package/src/runtime/shader-background.d.ts +42 -0
- package/src/runtime/shader-background.js +261 -0
- package/src/runtime/shader-backgrounds/band-lens.js +72 -0
- package/src/runtime/shader-backgrounds/common.js +53 -0
- package/src/runtime/shader-backgrounds/grain-gradient.js +87 -0
- package/src/runtime/shader-backgrounds/grid.js +33 -0
- package/src/runtime/shader-backgrounds/mesh-gradient.js +74 -0
- package/src/runtime/shader-backgrounds/silk.js +57 -0
- package/src/runtime/shader-backgrounds/waves.js +31 -0
- package/src/runtime/shape.d.ts +42 -0
- package/src/runtime/shape.js +313 -0
- package/src/runtime/silk-ribbon.d.ts +23 -0
- package/src/runtime/silk-ribbon.js +206 -0
- package/src/runtime/slideshow.d.ts +27 -0
- package/src/runtime/slideshow.js +234 -0
- package/src/runtime/spotlight.d.ts +9 -0
- package/src/runtime/spotlight.js +97 -0
- package/src/runtime/stack-poses.d.ts +53 -0
- package/src/runtime/stack-poses.js +273 -0
- package/src/runtime/stage.d.ts +47 -0
- package/src/runtime/stage.js +187 -0
- package/src/runtime/swarm.d.ts +49 -0
- package/src/runtime/swarm.js +199 -0
- package/src/runtime/text-motion.d.ts +41 -0
- package/src/runtime/text-motion.js +165 -0
- package/src/runtime/text.d.ts +11 -0
- package/src/runtime/text.js +90 -0
- package/src/runtime/tilt.d.ts +19 -0
- package/src/runtime/tilt.js +116 -0
- package/src/runtime/topography.d.ts +71 -0
- package/src/runtime/topography.js +650 -0
- package/src/runtime/trackers.d.ts +21 -0
- package/src/runtime/trackers.js +76 -0
- package/src/runtime/variable-type.d.ts +43 -0
- package/src/runtime/variable-type.js +358 -0
- package/src/runtime/voxel-logo.d.ts +31 -0
- package/src/runtime/voxel-logo.js +237 -0
- package/src/runtime/waves.d.ts +70 -0
- package/src/runtime/waves.js +799 -0
- package/src/runtime/world-labels.d.ts +29 -0
- package/src/runtime/world-labels.js +59 -0
- package/src/runtime/world-land.d.ts +8 -0
- package/src/runtime/world-land.js +105 -0
- package/src/runtime/world-map.d.ts +69 -0
- package/src/runtime/world-map.js +308 -0
- package/src/runtime/world-plan.d.ts +101 -0
- package/src/runtime/world-plan.js +333 -0
- package/src/runtime/world-routes.d.ts +75 -0
- package/src/runtime/world-routes.js +673 -0
- package/src/styles/border-sweep.css +82 -0
- package/src/styles/glass.css +104 -0
- package/src/styles/motion.css +218 -0
- package/src/styles/reveal.css +51 -0
- package/src/vendor/paper/LICENSE +201 -0
- package/src/vendor/paper/NOTICE +5 -0
- package/src/vendor/paper/README.md +8 -0
- package/src/vendor/paper/get-shader-color-from-string.js +100 -0
- package/src/vendor/paper/get-shader-noise-texture.js +17 -0
- package/src/vendor/paper/presets.js +3678 -0
- package/src/vendor/paper/shader-sizing.js +37 -0
- package/src/vendor/paper/shader-utils.js +130 -0
- package/src/vendor/paper/shaders/color-panels.js +223 -0
- package/src/vendor/paper/shaders/dithering.js +270 -0
- package/src/vendor/paper/shaders/dot-grid.js +109 -0
- package/src/vendor/paper/shaders/dot-orbit.js +122 -0
- package/src/vendor/paper/shaders/fluted-glass.js +360 -0
- package/src/vendor/paper/shaders/gem-smoke.js +572 -0
- package/src/vendor/paper/shaders/god-rays.js +132 -0
- package/src/vendor/paper/shaders/grain-gradient.js +300 -0
- package/src/vendor/paper/shaders/halftone-cmyk.js +294 -0
- package/src/vendor/paper/shaders/halftone-dots.js +320 -0
- package/src/vendor/paper/shaders/heatmap.js +383 -0
- package/src/vendor/paper/shaders/image-dithering.js +198 -0
- package/src/vendor/paper/shaders/lens-distortion.js +275 -0
- package/src/vendor/paper/shaders/liquid-metal.js +632 -0
- package/src/vendor/paper/shaders/mesh-gradient.js +130 -0
- package/src/vendor/paper/shaders/metaballs.js +110 -0
- package/src/vendor/paper/shaders/neuro-noise.js +76 -0
- package/src/vendor/paper/shaders/paper-texture.js +526 -0
- package/src/vendor/paper/shaders/perlin-noise.js +166 -0
- package/src/vendor/paper/shaders/pulsing-border.js +238 -0
- package/src/vendor/paper/shaders/simplex-noise.js +91 -0
- package/src/vendor/paper/shaders/smoke-ring.js +132 -0
- package/src/vendor/paper/shaders/spiral.js +82 -0
- package/src/vendor/paper/shaders/static-mesh-gradient.js +130 -0
- package/src/vendor/paper/shaders/static-radial-gradient.js +187 -0
- package/src/vendor/paper/shaders/swirl.js +103 -0
- package/src/vendor/paper/shaders/voronoi.js +138 -0
- package/src/vendor/paper/shaders/warp.js +129 -0
- package/src/vendor/paper/shaders/water.js +121 -0
- package/src/vendor/paper/shaders/waves.js +65 -0
- package/src/vendor/paper/vertex-shader.js +159 -0
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/iso-city/shadows
|
|
3
|
+
*
|
|
4
|
+
* The city's shadows on the ground, baked once into a map seen from above:
|
|
5
|
+
* each building's shadow (the hull of its footprint and that footprint
|
|
6
|
+
* thrown along the sun to its roof's height), each tree's, and a gentle
|
|
7
|
+
* darkening round every building's foot. Drawn on a 2D canvas with a blur
|
|
8
|
+
* (the canvas's own shadow, cast from shapes drawn off its edge, which
|
|
9
|
+
* every browser supports), lightest over darkest so overlaps never double.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** World units of ground round the street grid the map covers. */
|
|
13
|
+
export const MARGIN = 3;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @typedef {[number, number]} Point
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Where the shadow of a point at a height falls: away from the sun.
|
|
21
|
+
*
|
|
22
|
+
* @param {[number, number, number]} sun The direction towards the sun (y up, above the horizon).
|
|
23
|
+
* @param {number} height The height.
|
|
24
|
+
* @returns {Point} The shift on the ground, x and z.
|
|
25
|
+
*/
|
|
26
|
+
export function shadowShift(sun, height) {
|
|
27
|
+
return [(-sun[0] / sun[1]) * height, (-sun[2] / sun[1]) * height];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The convex hull of some points (Andrew's monotone chain).
|
|
32
|
+
*
|
|
33
|
+
* @param {Point[]} points The points.
|
|
34
|
+
* @returns {Point[]} The hull, anticlockwise.
|
|
35
|
+
*/
|
|
36
|
+
export function hull(points) {
|
|
37
|
+
const p = [...points].sort((a, b) => a[0] - b[0] || a[1] - b[1]);
|
|
38
|
+
if (p.length < 3) return p;
|
|
39
|
+
/**
|
|
40
|
+
* Which way three points turn.
|
|
41
|
+
*
|
|
42
|
+
* @param {Point} o The first.
|
|
43
|
+
* @param {Point} a The second.
|
|
44
|
+
* @param {Point} b The third.
|
|
45
|
+
* @returns {number} Positive anticlockwise.
|
|
46
|
+
*/
|
|
47
|
+
const turn = (o, a, b) => (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]);
|
|
48
|
+
/** @type {Point[]} */
|
|
49
|
+
const lower = [];
|
|
50
|
+
for (const q of p) {
|
|
51
|
+
while (lower.length >= 2 && turn(lower[lower.length - 2], lower[lower.length - 1], q) <= 0) lower.pop();
|
|
52
|
+
lower.push(q);
|
|
53
|
+
}
|
|
54
|
+
/** @type {Point[]} */
|
|
55
|
+
const upper = [];
|
|
56
|
+
for (const q of [...p].reverse()) {
|
|
57
|
+
while (upper.length >= 2 && turn(upper[upper.length - 2], upper[upper.length - 1], q) <= 0) upper.pop();
|
|
58
|
+
upper.push(q);
|
|
59
|
+
}
|
|
60
|
+
return [...lower.slice(0, -1), ...upper.slice(0, -1)];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Bakes the shadow map.
|
|
65
|
+
*
|
|
66
|
+
* @param {import('./plan.js').CityPlan} plan The plan.
|
|
67
|
+
* @param {[number, number, number]} sun The direction towards the sun.
|
|
68
|
+
* @param {number} [resolution=1024] The map's size, pixels a side.
|
|
69
|
+
* @returns {{ canvas: HTMLCanvasElement, rect: [number, number, number] }} The map
|
|
70
|
+
* (red: 0 lit, 1 in shadow), and where it lies: its least x and z, and 1 / its span.
|
|
71
|
+
*/
|
|
72
|
+
export function bakeShadows(plan, sun, resolution = 1024) {
|
|
73
|
+
const span = plan.extent + MARGIN * 2;
|
|
74
|
+
const canvas = document.createElement('canvas');
|
|
75
|
+
canvas.width = canvas.height = resolution;
|
|
76
|
+
const ctx = /** @type {CanvasRenderingContext2D} */ (canvas.getContext('2d'));
|
|
77
|
+
ctx.fillStyle = '#000';
|
|
78
|
+
ctx.fillRect(0, 0, resolution, resolution);
|
|
79
|
+
ctx.globalCompositeOperation = 'lighten';
|
|
80
|
+
const k = resolution / span;
|
|
81
|
+
// Shapes are drawn a canvas-width off to the left; only their blurred shadow lands on the map.
|
|
82
|
+
const away = resolution * 2;
|
|
83
|
+
ctx.shadowOffsetX = away;
|
|
84
|
+
/**
|
|
85
|
+
* Draws a blurred shape.
|
|
86
|
+
*
|
|
87
|
+
* @param {(ctx: CanvasRenderingContext2D, at: (x: number, z: number) => Point) => void} shape Traces the path.
|
|
88
|
+
* @param {number} strength How dark, 0–1.
|
|
89
|
+
* @param {number} blur How soft, world units.
|
|
90
|
+
*/
|
|
91
|
+
const draw = (shape, strength, blur) => {
|
|
92
|
+
ctx.shadowColor = `rgba(255, 255, 255, ${strength})`;
|
|
93
|
+
ctx.shadowBlur = blur * k;
|
|
94
|
+
ctx.fillStyle = '#fff';
|
|
95
|
+
ctx.beginPath();
|
|
96
|
+
shape(ctx, (x, z) => [(x + MARGIN) * k - away, (z + MARGIN) * k]);
|
|
97
|
+
ctx.fill();
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* Traces a polygon.
|
|
101
|
+
*
|
|
102
|
+
* @param {Point[]} points The polygon, in world x and z.
|
|
103
|
+
* @returns {(ctx: CanvasRenderingContext2D, at: (x: number, z: number) => Point) => void} The tracer.
|
|
104
|
+
*/
|
|
105
|
+
const polygon = (points) => (c, at) => {
|
|
106
|
+
points.forEach(([x, z], i) => {
|
|
107
|
+
const [px, pz] = at(x, z);
|
|
108
|
+
if (i) c.lineTo(px, pz);
|
|
109
|
+
else c.moveTo(px, pz);
|
|
110
|
+
});
|
|
111
|
+
c.closePath();
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
for (const b of plan.buildings) {
|
|
115
|
+
const base = b.parts[0];
|
|
116
|
+
const grow = 0.05;
|
|
117
|
+
draw(polygon([[base.x0 - grow, base.z0 - grow], [base.x1 + grow, base.z0 - grow], [base.x1 + grow, base.z1 + grow], [base.x0 - grow, base.z1 + grow]]), 0.32, 0.12);
|
|
118
|
+
for (const part of b.parts) {
|
|
119
|
+
/** @type {Point[]} */
|
|
120
|
+
const corners = [[part.x0, part.z0], [part.x1, part.z0], [part.x1, part.z1], [part.x0, part.z1]];
|
|
121
|
+
/** @type {Point[]} */
|
|
122
|
+
const cast = [];
|
|
123
|
+
for (const h of [part.y0, part.y1]) {
|
|
124
|
+
const [dx, dz] = shadowShift(sun, h);
|
|
125
|
+
for (const [x, z] of corners) cast.push([x + dx, z + dz]);
|
|
126
|
+
}
|
|
127
|
+
const shape = polygon(hull(cast));
|
|
128
|
+
draw(shape, 0.78, 0.05);
|
|
129
|
+
draw(shape, 0.4, 0.3);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
for (const t of plan.trees) {
|
|
133
|
+
const [dx, dz] = shadowShift(sun, t.h + t.r * 1.6);
|
|
134
|
+
draw((c, at) => {
|
|
135
|
+
const [px, pz] = at(t.x + dx * 0.8, t.z + dz * 0.8);
|
|
136
|
+
c.arc(px, pz, t.r * 0.95 * k, 0, Math.PI * 2);
|
|
137
|
+
}, 0.5, 0.06);
|
|
138
|
+
draw((c, at) => {
|
|
139
|
+
const [px, pz] = at(t.x, t.z);
|
|
140
|
+
c.arc(px, pz, t.r * 0.6 * k, 0, Math.PI * 2);
|
|
141
|
+
}, 0.25, 0.1);
|
|
142
|
+
}
|
|
143
|
+
for (const l of plan.lamps) {
|
|
144
|
+
const [dx, dz] = shadowShift(sun, 0.36);
|
|
145
|
+
draw(polygon(hull([[l.x - 0.012, l.z - 0.012], [l.x + 0.012, l.z + 0.012], [l.x + dx - 0.012, l.z + dz - 0.012], [l.x + dx + 0.012, l.z + dz + 0.012]])), 0.35, 0.03);
|
|
146
|
+
}
|
|
147
|
+
return { canvas, rect: [-MARGIN, -MARGIN, 1 / span] };
|
|
148
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { Roads } from './roads.js';
|
|
2
|
+
|
|
3
|
+
/** How much larger than its model (0.44 long) a car is drawn. */
|
|
4
|
+
export const CAR_SCALE: number;
|
|
5
|
+
/** A car's length. */
|
|
6
|
+
export const CAR_LENGTH: number;
|
|
7
|
+
/** The least distance, centre to centre, between two cars in a lane. */
|
|
8
|
+
export const GAP: number;
|
|
9
|
+
/** How far before its lane's end a car waiting at the crossing stops (its centre). */
|
|
10
|
+
export const STOP: number;
|
|
11
|
+
/** A car pulled in at the kerb: its centre from the street's centre line. */
|
|
12
|
+
export const KERB: number;
|
|
13
|
+
/** Where a waiting rider stands, from the street's centre line. */
|
|
14
|
+
export const PAVEMENT: number;
|
|
15
|
+
|
|
16
|
+
export interface Car {
|
|
17
|
+
id: number;
|
|
18
|
+
taxi: boolean;
|
|
19
|
+
/** Which neutral colour (taxis wear the accent). */
|
|
20
|
+
paint: number;
|
|
21
|
+
phase: 'lane' | 'turn';
|
|
22
|
+
lane: number;
|
|
23
|
+
next: number;
|
|
24
|
+
s: number;
|
|
25
|
+
t: number;
|
|
26
|
+
v: number;
|
|
27
|
+
pref: number;
|
|
28
|
+
/** The junction it has been let across, or −1. */
|
|
29
|
+
holds: number;
|
|
30
|
+
waiting: number;
|
|
31
|
+
side: number;
|
|
32
|
+
plan: number[];
|
|
33
|
+
job: Job | null;
|
|
34
|
+
/** Seconds left of a pickup. */
|
|
35
|
+
pickup: number;
|
|
36
|
+
/** Seconds since its last pickup began, or −1. */
|
|
37
|
+
hop: number;
|
|
38
|
+
odo: number;
|
|
39
|
+
/** How hard it is braking, 0–1. */
|
|
40
|
+
brake: number;
|
|
41
|
+
x: number;
|
|
42
|
+
z: number;
|
|
43
|
+
heading: number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface Job {
|
|
47
|
+
id: number;
|
|
48
|
+
car: Car;
|
|
49
|
+
lane: number;
|
|
50
|
+
s: number;
|
|
51
|
+
/** The pin, on the pavement. */
|
|
52
|
+
x: number;
|
|
53
|
+
z: number;
|
|
54
|
+
/** The way there, for drawing. */
|
|
55
|
+
points: { x: number; z: number }[];
|
|
56
|
+
length: number;
|
|
57
|
+
/** The taxi's distance driven when it set off. */
|
|
58
|
+
odo: number;
|
|
59
|
+
state: 'going' | 'pickup' | 'done' | 'cancelled';
|
|
60
|
+
/** When it entered that state, seconds. */
|
|
61
|
+
since: number;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface Traffic {
|
|
65
|
+
cars: Car[];
|
|
66
|
+
jobs: Job[];
|
|
67
|
+
/** Junctions where people are crossing. */
|
|
68
|
+
crossings: Map<number, { start: number; until: number }>;
|
|
69
|
+
time: number;
|
|
70
|
+
/** Moves everything on by `dt` seconds. */
|
|
71
|
+
step(dt: number): void;
|
|
72
|
+
/** Calls the nearest free taxi by road to the street nearest a point; null when there is no street or taxi. */
|
|
73
|
+
call(x: number, z: number): Job | null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Puts cars on the roads (defaults: 14 cars, 4 taxis, speed 1, 5 paints, moving, Math.random). */
|
|
77
|
+
export function createTraffic(
|
|
78
|
+
roads: Roads,
|
|
79
|
+
options?: { cars?: number; taxis?: number; speed?: number; paints?: number; parked?: boolean; random?: () => number },
|
|
80
|
+
): Traffic;
|
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/iso-city/traffic
|
|
3
|
+
*
|
|
4
|
+
* The cars: each keeps to its lane, eases off for turns (more for a tight
|
|
5
|
+
* right than a wide left), keeps its distance from the car ahead, and
|
|
6
|
+
* stops at the crossing before a junction unless the junction is its to
|
|
7
|
+
* cross. A junction is crossed by one car at a time, and only when there
|
|
8
|
+
* is room on the far side, so traffic never locks up; now and then people
|
|
9
|
+
* cross, and every car waits. Taxis can be called: the nearest by road
|
|
10
|
+
* drives to the pin, pulls in to the kerb, picks up, and goes on its way.
|
|
11
|
+
*
|
|
12
|
+
* Pure: `step` moves it on, and positions are read off the cars.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { LANE, exits, findRoute, lanePoint, nearestStreet, routePoints } from './roads.js';
|
|
16
|
+
|
|
17
|
+
/** How much larger than its model (0.44 long) a car is drawn. */
|
|
18
|
+
export const CAR_SCALE = 1.3;
|
|
19
|
+
/** A car's length. */
|
|
20
|
+
export const CAR_LENGTH = 0.44 * CAR_SCALE;
|
|
21
|
+
/** The least distance, centre to centre, between two cars in a lane. */
|
|
22
|
+
export const GAP = CAR_LENGTH + 0.14;
|
|
23
|
+
/** How far before its lane's end a car waiting at the crossing stops (its centre). */
|
|
24
|
+
export const STOP = 0.34 + CAR_LENGTH / 2;
|
|
25
|
+
/** A car pulled in at the kerb: its centre from the street's centre line. */
|
|
26
|
+
export const KERB = 0.35;
|
|
27
|
+
/** Where a waiting rider stands: on the pavement, from the street's centre line. */
|
|
28
|
+
export const PAVEMENT = 0.62;
|
|
29
|
+
/** Pulling away, and braking (world units a second, a second). */
|
|
30
|
+
const ACCEL = 0.9;
|
|
31
|
+
const BRAKE = 2.2;
|
|
32
|
+
/** A share of a car's cruising speed for each kind of turn. */
|
|
33
|
+
const TURN_SPEED = { straight: 1, right: 0.48, left: 0.62, back: 0.4 };
|
|
34
|
+
/** Cruising speed at `speed` 1, world units a second. */
|
|
35
|
+
const CRUISE = 0.85;
|
|
36
|
+
/** A taxi's speed on its way to a pickup, a share of its usual. */
|
|
37
|
+
const HURRY = 1.35;
|
|
38
|
+
/** How long a pickup takes, seconds. */
|
|
39
|
+
const PICKUP = 1.4;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @typedef {import('./roads.js').Roads} Roads
|
|
43
|
+
* @typedef {import('./roads.js').Turn} Turn
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @typedef {object} Car
|
|
48
|
+
* @property {number} id Its index.
|
|
49
|
+
* @property {boolean} taxi Whether it is one of the brand's taxis.
|
|
50
|
+
* @property {number} paint Which neutral colour it is (taxis wear the accent).
|
|
51
|
+
* @property {'lane' | 'turn'} phase On a lane, or crossing a junction.
|
|
52
|
+
* @property {number} lane The lane it is on (or turning out of).
|
|
53
|
+
* @property {number} next The lane it turns into next.
|
|
54
|
+
* @property {number} s How far along its lane.
|
|
55
|
+
* @property {number} t How far through its turn.
|
|
56
|
+
* @property {number} v Its speed.
|
|
57
|
+
* @property {number} pref Its own share of the cruising speed.
|
|
58
|
+
* @property {number} holds The junction it has been let across, or −1.
|
|
59
|
+
* @property {number} waiting Seconds it has waited at the crossing.
|
|
60
|
+
* @property {number} side Its centre from the street's centre line (LANE, or KERB pulled in).
|
|
61
|
+
* @property {number[]} plan The lanes after `next`, on the way to a pickup.
|
|
62
|
+
* @property {Job | null} job The pickup it is on its way to.
|
|
63
|
+
* @property {number} pickup Seconds left of a pickup, 0 when not stopped for one.
|
|
64
|
+
* @property {number} hop Seconds since its last pickup began (for the little bounce), or −1.
|
|
65
|
+
* @property {number} odo Distance driven.
|
|
66
|
+
* @property {number} brake How hard it is braking, 0–1 (for the brake lights).
|
|
67
|
+
* @property {number} x Where it is, x.
|
|
68
|
+
* @property {number} z Where it is, z.
|
|
69
|
+
* @property {number} heading Which way it faces, radians.
|
|
70
|
+
*/
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @typedef {object} Job
|
|
74
|
+
* @property {number} id Its number.
|
|
75
|
+
* @property {Car} car The taxi sent.
|
|
76
|
+
* @property {number} lane The lane it picks up on.
|
|
77
|
+
* @property {number} s Where along it.
|
|
78
|
+
* @property {number} x The pin's x, on the pavement.
|
|
79
|
+
* @property {number} z The pin's z.
|
|
80
|
+
* @property {{ x: number, z: number }[]} points The way there, for drawing.
|
|
81
|
+
* @property {number} length The way's length.
|
|
82
|
+
* @property {number} odo The taxi's distance driven when it set off.
|
|
83
|
+
* @property {'going' | 'pickup' | 'done' | 'cancelled'} state How it stands.
|
|
84
|
+
* @property {number} since When it entered that state, seconds.
|
|
85
|
+
*/
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* @typedef {object} Traffic
|
|
89
|
+
* @property {Car[]} cars Every car.
|
|
90
|
+
* @property {Job[]} jobs Pickups under way, and ones just ended (for their exit animations).
|
|
91
|
+
* @property {Map<number, { start: number, until: number }>} crossings Junctions where people are crossing, by junction.
|
|
92
|
+
* @property {number} time Seconds simulated.
|
|
93
|
+
* @property {(dt: number) => void} step Moves everything on by `dt` seconds.
|
|
94
|
+
* @property {(x: number, z: number) => Job | null} call Calls the nearest free taxi to the street
|
|
95
|
+
* nearest a point (on the side of it the point is); null when there is no street there or no taxi.
|
|
96
|
+
*/
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Puts cars on the roads.
|
|
100
|
+
*
|
|
101
|
+
* @param {Roads} roads The roads.
|
|
102
|
+
* @param {object} [options]
|
|
103
|
+
* @param {number} [options.cars=14] How many cars.
|
|
104
|
+
* @param {number} [options.taxis=4] How many of them are taxis.
|
|
105
|
+
* @param {number} [options.speed=1] Cruising speed, a share of the usual.
|
|
106
|
+
* @param {number} [options.paints=5] How many neutral colours there are to pick from.
|
|
107
|
+
* @param {boolean} [options.parked=false] Park them at the kerb and never move (a still frame).
|
|
108
|
+
* @param {() => number} [options.random=Math.random] The generator.
|
|
109
|
+
* @returns {Traffic} The traffic.
|
|
110
|
+
*/
|
|
111
|
+
export function createTraffic(roads, { cars: count = 14, taxis = 4, speed = 1, paints = 5, parked = false, random = Math.random } = {}) {
|
|
112
|
+
const { lanes } = roads;
|
|
113
|
+
// Places to start: slots along every lane, far enough apart, dealt out at random.
|
|
114
|
+
/** @type {{ lane: number, s: number }[]} */
|
|
115
|
+
const slots = [];
|
|
116
|
+
for (const lane of lanes) {
|
|
117
|
+
for (let s = 0.4; s <= lane.length - STOP - 0.05; s += GAP + 0.3) slots.push({ lane: lane.id, s });
|
|
118
|
+
}
|
|
119
|
+
for (let i = slots.length - 1; i > 0; i--) {
|
|
120
|
+
const k = Math.floor(random() * (i + 1));
|
|
121
|
+
[slots[i], slots[k]] = [slots[k], slots[i]];
|
|
122
|
+
}
|
|
123
|
+
const total = Math.min(Math.max(0, Math.round(count)), slots.length);
|
|
124
|
+
const taxiCount = Math.min(total, Math.max(0, Math.round(taxis)));
|
|
125
|
+
/** @type {Car[]} */
|
|
126
|
+
const cars = slots.slice(0, total).map((slot, id) => ({
|
|
127
|
+
id,
|
|
128
|
+
taxi: id < taxiCount,
|
|
129
|
+
paint: Math.floor(random() * paints),
|
|
130
|
+
phase: 'lane',
|
|
131
|
+
lane: slot.lane,
|
|
132
|
+
next: -1,
|
|
133
|
+
s: slot.s,
|
|
134
|
+
t: 0,
|
|
135
|
+
v: 0,
|
|
136
|
+
pref: 0.88 + random() * 0.24,
|
|
137
|
+
holds: -1,
|
|
138
|
+
waiting: 0,
|
|
139
|
+
side: parked ? KERB : LANE,
|
|
140
|
+
plan: [],
|
|
141
|
+
job: null,
|
|
142
|
+
pickup: 0,
|
|
143
|
+
hop: -1,
|
|
144
|
+
odo: 0,
|
|
145
|
+
brake: parked ? 0 : 1,
|
|
146
|
+
x: 0,
|
|
147
|
+
z: 0,
|
|
148
|
+
heading: 0,
|
|
149
|
+
}));
|
|
150
|
+
for (const car of cars) {
|
|
151
|
+
car.next = wander(car.lane);
|
|
152
|
+
place(car);
|
|
153
|
+
}
|
|
154
|
+
/** Who has been let across each junction, by junction. */
|
|
155
|
+
/** @type {Map<number, Car>} */
|
|
156
|
+
const holders = new Map();
|
|
157
|
+
/** @type {Map<number, { start: number, until: number }>} */
|
|
158
|
+
const crossings = new Map();
|
|
159
|
+
/** @type {Job[]} */
|
|
160
|
+
const jobs = [];
|
|
161
|
+
let jobId = 0;
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Picks the lane to take after a lane: straight on more often than not.
|
|
165
|
+
*
|
|
166
|
+
* @param {number} id The lane.
|
|
167
|
+
* @returns {number} The lane after it.
|
|
168
|
+
*/
|
|
169
|
+
function wander(id) {
|
|
170
|
+
const options = exits(roads, id);
|
|
171
|
+
const weights = options.map((m) => (roads.turn(id, m).kind === 'straight' ? 2 : 1));
|
|
172
|
+
let pick = random() * weights.reduce((a, b) => a + b, 0);
|
|
173
|
+
for (let i = 0; i < options.length; i++) {
|
|
174
|
+
pick -= weights[i];
|
|
175
|
+
if (pick <= 0) return options[i];
|
|
176
|
+
}
|
|
177
|
+
return options[options.length - 1];
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Works out where a car is from where it is along its lane or turn.
|
|
182
|
+
*
|
|
183
|
+
* @param {Car} car The car.
|
|
184
|
+
*/
|
|
185
|
+
function place(car) {
|
|
186
|
+
const p = car.phase === 'lane' ? lanePoint(lanes[car.lane], car.s, car.side) : roads.turn(car.lane, car.next).at(car.t);
|
|
187
|
+
car.x = p.x;
|
|
188
|
+
car.z = p.z;
|
|
189
|
+
car.heading = p.heading;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* The speed a car may be doing now to stop within a distance (from `floor`, the speed it may arrive at).
|
|
194
|
+
*
|
|
195
|
+
* @param {number} distance The distance.
|
|
196
|
+
* @param {number} [floor=0] The speed it may arrive at.
|
|
197
|
+
* @param {number} [ease=1] A share of full braking.
|
|
198
|
+
* @returns {number} The speed.
|
|
199
|
+
*/
|
|
200
|
+
const within = (distance, floor = 0, ease = 1) => Math.sqrt(floor * floor + 2 * BRAKE * ease * Math.max(0, distance));
|
|
201
|
+
|
|
202
|
+
/** @type {Traffic} */
|
|
203
|
+
const traffic = {
|
|
204
|
+
cars,
|
|
205
|
+
jobs,
|
|
206
|
+
crossings,
|
|
207
|
+
time: 0,
|
|
208
|
+
step(dt) {
|
|
209
|
+
if (parked || dt <= 0) return;
|
|
210
|
+
traffic.time += dt;
|
|
211
|
+
const now = traffic.time;
|
|
212
|
+
|
|
213
|
+
// People cross now and then where three or four streets meet.
|
|
214
|
+
for (const [node, hold] of crossings) if (now > hold.until) crossings.delete(node);
|
|
215
|
+
for (let node = 0; node < roads.nodes.length; node++) {
|
|
216
|
+
if (roads.degree[node] < 3 || crossings.has(node) || holders.has(node)) continue;
|
|
217
|
+
if (random() < dt / 40) crossings.set(node, { start: now, until: now + 2.2 + random() * 1.2 });
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Each lane's cars, nearest its start first.
|
|
221
|
+
/** @type {Car[][]} */
|
|
222
|
+
const onLane = lanes.map(() => []);
|
|
223
|
+
for (const car of cars) if (car.phase === 'lane') onLane[car.lane].push(car);
|
|
224
|
+
for (const list of onLane) if (list.length > 1) list.sort((a, b) => a.s - b.s);
|
|
225
|
+
/**
|
|
226
|
+
* The car furthest back on a lane, or the one turning into it.
|
|
227
|
+
*
|
|
228
|
+
* @param {number} id The lane.
|
|
229
|
+
* @param {Car} me The car asking (skipped).
|
|
230
|
+
* @returns {number | null} How far along that car is (negative while it is still turning in), or null for an empty lane.
|
|
231
|
+
*/
|
|
232
|
+
const tailOf = (id, me) => {
|
|
233
|
+
const from = lanes[id].from;
|
|
234
|
+
const turning = holders.get(from);
|
|
235
|
+
if (turning && turning !== me && turning.phase === 'turn' && turning.next === id) {
|
|
236
|
+
return turning.t - roads.turn(turning.lane, turning.next).length;
|
|
237
|
+
}
|
|
238
|
+
const list = onLane[id];
|
|
239
|
+
for (const car of list) if (car !== me) return car.s;
|
|
240
|
+
return null;
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
/** @type {Map<number, Car[]>} */
|
|
244
|
+
const asks = new Map();
|
|
245
|
+
for (const car of cars) {
|
|
246
|
+
const before = car.v;
|
|
247
|
+
// A taxi on its way to a pickup hurries a little.
|
|
248
|
+
const cruise = CRUISE * speed * car.pref * (car.job?.state === 'going' ? HURRY : 1);
|
|
249
|
+
if (car.hop >= 0) car.hop += dt;
|
|
250
|
+
if (car.pickup > 0) {
|
|
251
|
+
car.v = 0;
|
|
252
|
+
car.pickup -= dt;
|
|
253
|
+
if (car.pickup <= 0) finish(car, now);
|
|
254
|
+
car.brake = 1;
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
let limit = cruise;
|
|
258
|
+
const lane = lanes[car.lane];
|
|
259
|
+
const turn = roads.turn(car.lane, car.next);
|
|
260
|
+
const turnSpeed = cruise * TURN_SPEED[turn.kind];
|
|
261
|
+
if (car.phase === 'lane') {
|
|
262
|
+
const left = lane.length - car.s;
|
|
263
|
+
limit = Math.min(limit, within(left, turnSpeed, 0.35));
|
|
264
|
+
const list = onLane[car.lane];
|
|
265
|
+
const ahead = list[list.indexOf(car) + 1];
|
|
266
|
+
if (ahead) {
|
|
267
|
+
limit = Math.min(limit, within(ahead.s - car.s - GAP, ahead.v * 0.9));
|
|
268
|
+
} else if (car.holds === lane.to) {
|
|
269
|
+
const tail = tailOf(car.next, car);
|
|
270
|
+
if (tail !== null) limit = Math.min(limit, within(left + turn.length + tail - GAP));
|
|
271
|
+
}
|
|
272
|
+
if (car.holds !== lane.to) {
|
|
273
|
+
const stopAt = lane.length - STOP;
|
|
274
|
+
const toStop = stopAt - car.s;
|
|
275
|
+
// Only the car at the front asks: one queued behind it never holds a junction it cannot reach.
|
|
276
|
+
if (!ahead && toStop < (car.v * car.v) / (2 * BRAKE) + 0.5) {
|
|
277
|
+
const list2 = asks.get(lane.to) ?? [];
|
|
278
|
+
list2.push(car);
|
|
279
|
+
asks.set(lane.to, list2);
|
|
280
|
+
}
|
|
281
|
+
limit = Math.min(limit, within(toStop));
|
|
282
|
+
if (toStop < 0.05 && car.v < 0.05) car.waiting += dt;
|
|
283
|
+
}
|
|
284
|
+
const job = car.job;
|
|
285
|
+
if (job && job.state === 'going' && job.lane === car.lane && car.next !== -1 && car.plan.length === 0 && pickupHere(car)) {
|
|
286
|
+
const toPin = job.s - car.s;
|
|
287
|
+
limit = Math.min(limit, within(toPin, 0, 0.55));
|
|
288
|
+
car.side = LANE + (KERB - LANE) * Math.min(1, Math.max(0, 1 - toPin / 1.4));
|
|
289
|
+
if (toPin < 0.03 && car.v < 0.1) {
|
|
290
|
+
car.v = 0;
|
|
291
|
+
car.pickup = PICKUP;
|
|
292
|
+
car.hop = 0;
|
|
293
|
+
job.state = 'pickup';
|
|
294
|
+
job.since = now;
|
|
295
|
+
car.brake = 1;
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
} else if (car.side !== LANE) {
|
|
299
|
+
car.side += (LANE - car.side) * Math.min(1, dt * 2.5);
|
|
300
|
+
if (Math.abs(car.side - LANE) < 0.002) car.side = LANE;
|
|
301
|
+
}
|
|
302
|
+
} else {
|
|
303
|
+
limit = Math.min(limit, Math.max(turnSpeed, cruise * (car.t / turn.length)));
|
|
304
|
+
const tail = tailOf(car.next, car);
|
|
305
|
+
if (tail !== null) limit = Math.min(limit, within(turn.length - car.t + tail - GAP));
|
|
306
|
+
}
|
|
307
|
+
car.v = limit < car.v ? Math.max(limit, car.v - BRAKE * 2 * dt) : Math.min(limit, car.v + ACCEL * dt);
|
|
308
|
+
advance(car, car.v * dt, onLane);
|
|
309
|
+
const slowing = (before - car.v) / dt;
|
|
310
|
+
const target = car.v < 0.03 ? 1 : Math.min(1, Math.max(0, slowing / 0.8));
|
|
311
|
+
car.brake += (target - car.brake) * Math.min(1, dt * 8);
|
|
312
|
+
place(car);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// Let one car across each free junction: the one that has waited
|
|
316
|
+
// longest, when there is room on the far side.
|
|
317
|
+
for (const [node, list] of asks) {
|
|
318
|
+
if (holders.has(node) || crossings.has(node)) continue;
|
|
319
|
+
list.sort((a, b) => b.waiting - a.waiting);
|
|
320
|
+
for (const car of list) {
|
|
321
|
+
const tail = tailOf(car.next, car);
|
|
322
|
+
if (tail !== null && tail < GAP + 0.05) continue;
|
|
323
|
+
holders.set(node, car);
|
|
324
|
+
car.holds = node;
|
|
325
|
+
car.waiting = 0;
|
|
326
|
+
break;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
},
|
|
330
|
+
call(x, z) {
|
|
331
|
+
const street = nearestStreet(roads, x, z);
|
|
332
|
+
if (!street) return null;
|
|
333
|
+
const free = cars.filter((c) => c.taxi && (!c.job || c.job.state === 'going') && c.pickup <= 0);
|
|
334
|
+
if (!free.length) return null;
|
|
335
|
+
/** @type {{ car: Car, route: { lanes: number[], length: number }, from: { lane: number, s: number }, lead: number } | null} */
|
|
336
|
+
let best = null;
|
|
337
|
+
for (const car of free) {
|
|
338
|
+
const from = startOf(car);
|
|
339
|
+
const route = findRoute(roads, from.at, street);
|
|
340
|
+
if (!route) continue;
|
|
341
|
+
const cost = route.length + from.lead + (car.job ? 6 : 0);
|
|
342
|
+
if (!best || cost < best.route.length + best.lead + (best.car.job ? 6 : 0)) best = { car, route, from: from.at, lead: from.lead };
|
|
343
|
+
}
|
|
344
|
+
if (!best) return null;
|
|
345
|
+
const { car, route, from } = best;
|
|
346
|
+
if (car.job) {
|
|
347
|
+
car.job.state = 'cancelled';
|
|
348
|
+
car.job.since = traffic.time;
|
|
349
|
+
}
|
|
350
|
+
// The way there: the rest of a turn under way, then the route.
|
|
351
|
+
/** @type {{ x: number, z: number }[]} */
|
|
352
|
+
const points = [{ x: car.x, z: car.z }];
|
|
353
|
+
if (car.phase === 'turn') {
|
|
354
|
+
const turn = roads.turn(car.lane, car.next);
|
|
355
|
+
for (let t = car.t + 0.12; t < turn.length; t += 0.12) points.push(turn.at(t));
|
|
356
|
+
}
|
|
357
|
+
points.push(...routePoints(roads, route.lanes, from.s, street.s));
|
|
358
|
+
let length = 0;
|
|
359
|
+
for (let i = 1; i < points.length; i++) length += Math.hypot(points[i].x - points[i - 1].x, points[i].z - points[i - 1].z);
|
|
360
|
+
const pin = lanePoint(lanes[street.lane], street.s, PAVEMENT);
|
|
361
|
+
/** @type {Job} */
|
|
362
|
+
const job = { id: ++jobId, car, lane: street.lane, s: street.s, x: pin.x, z: pin.z, points, length, odo: car.odo, state: 'going', since: traffic.time };
|
|
363
|
+
car.job = job;
|
|
364
|
+
if (car.phase === 'lane') {
|
|
365
|
+
if (route.lanes.length > 1) car.next = route.lanes[1];
|
|
366
|
+
car.plan = route.lanes.slice(2);
|
|
367
|
+
} else {
|
|
368
|
+
car.plan = route.lanes.slice(1);
|
|
369
|
+
}
|
|
370
|
+
jobs.push(job);
|
|
371
|
+
return job;
|
|
372
|
+
},
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Whether a car's pickup is ahead of it on the lane it is on.
|
|
377
|
+
*
|
|
378
|
+
* @param {Car} car The car.
|
|
379
|
+
* @returns {boolean} True when it should be pulling in.
|
|
380
|
+
*/
|
|
381
|
+
function pickupHere(car) {
|
|
382
|
+
return Boolean(car.job && car.job.s >= car.s - 0.05);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Where a car's way to a pickup starts: on its lane a stopping distance
|
|
387
|
+
* ahead, or (mid-turn) at the start of the lane it is turning into.
|
|
388
|
+
*
|
|
389
|
+
* @param {Car} car The car.
|
|
390
|
+
* @returns {{ at: { lane: number, s: number }, lead: number }} The start, and the distance to it.
|
|
391
|
+
*/
|
|
392
|
+
function startOf(car) {
|
|
393
|
+
if (car.phase === 'turn') return { at: { lane: car.next, s: 0 }, lead: roads.turn(car.lane, car.next).length - car.t };
|
|
394
|
+
return { at: { lane: car.lane, s: car.s }, lead: 0 };
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* Ends a pickup: the taxi pulls away and goes back to wandering.
|
|
399
|
+
*
|
|
400
|
+
* @param {Car} car The taxi.
|
|
401
|
+
* @param {number} now The time, seconds.
|
|
402
|
+
*/
|
|
403
|
+
function finish(car, now) {
|
|
404
|
+
car.pickup = 0;
|
|
405
|
+
if (car.job) {
|
|
406
|
+
car.job.state = 'done';
|
|
407
|
+
car.job.since = now;
|
|
408
|
+
}
|
|
409
|
+
car.job = null;
|
|
410
|
+
car.plan = [];
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Moves a car on along its lane and through its turns, never into the
|
|
415
|
+
* car ahead or past a crossing it has not been let over.
|
|
416
|
+
*
|
|
417
|
+
* @param {Car} car The car.
|
|
418
|
+
* @param {number} ds How far.
|
|
419
|
+
* @param {Car[][]} onLane Each lane's cars, nearest its start first.
|
|
420
|
+
*/
|
|
421
|
+
function advance(car, ds, onLane) {
|
|
422
|
+
car.odo += ds;
|
|
423
|
+
if (car.phase === 'lane') {
|
|
424
|
+
const lane = lanes[car.lane];
|
|
425
|
+
const list = onLane[car.lane];
|
|
426
|
+
const ahead = list[list.indexOf(car) + 1];
|
|
427
|
+
let s = car.s + ds;
|
|
428
|
+
if (ahead && s > ahead.s - GAP) {
|
|
429
|
+
s = Math.max(car.s, ahead.s - GAP);
|
|
430
|
+
car.v = Math.min(car.v, ahead.v);
|
|
431
|
+
}
|
|
432
|
+
if (car.holds !== lane.to && s > lane.length - STOP) {
|
|
433
|
+
s = Math.max(car.s, lane.length - STOP);
|
|
434
|
+
car.v = 0;
|
|
435
|
+
}
|
|
436
|
+
if (car.job && car.job.state === 'going' && car.job.lane === car.lane && car.plan.length === 0 && car.s <= car.job.s && s > car.job.s) s = car.job.s;
|
|
437
|
+
car.s = s;
|
|
438
|
+
if (car.s >= lane.length) {
|
|
439
|
+
car.phase = 'turn';
|
|
440
|
+
car.t = car.s - lane.length;
|
|
441
|
+
}
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
const turn = roads.turn(car.lane, car.next);
|
|
445
|
+
car.t += ds;
|
|
446
|
+
if (car.t < turn.length) return;
|
|
447
|
+
holders.delete(turn.node);
|
|
448
|
+
car.holds = -1;
|
|
449
|
+
car.phase = 'lane';
|
|
450
|
+
car.s = car.t - turn.length;
|
|
451
|
+
car.t = 0;
|
|
452
|
+
car.lane = car.next;
|
|
453
|
+
car.next = car.plan.length ? /** @type {number} */ (car.plan.shift()) : wander(car.lane);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
return traffic;
|
|
457
|
+
}
|