@bendyline/molen-ambient 0.0.11 → 0.0.13
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/dist/client.d.mts +15 -5
- package/dist/client.mjs +733 -153
- package/dist/{components-D4HrizHN.mjs → components-DJINUkXK.mjs} +196 -45
- package/dist/kernel.d.mts +114 -11
- package/dist/kernel.mjs +444 -97
- package/dist/{wildlife-B4EL0z-N.d.mts → wildlife-CvHzzI9m.d.mts} +1 -1
- package/package.json +10 -10
package/dist/kernel.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { _ as KIND_SPEED, a as AmbientRole, b as featureDrafts, c as AMBIENT_OBSERVER_COMPONENT, d as registerAmbientSchema, f as TRANSPORT_NETWORK_EXAMPLE, g as wayDraft, h as documentRunways, i as AmbientObserver, l as AMBIENT_ROLE_COMPONENT, m as documentPois, n as AMBIENT_KINDS, o as AmbientState, p as TRANSPORT_NETWORK_FORMAT, r as AmbientAgent, s as AMBIENT_AGENT_COMPONENT, t as AMBIENT_ENTITY, u as AMBIENT_STATE_COMPONENT, v as classifyFeature, x as tilePois, y as clipPolyline } from "./components-
|
|
1
|
+
import { _ as KIND_SPEED, a as AmbientRole, b as featureDrafts, c as AMBIENT_OBSERVER_COMPONENT, d as registerAmbientSchema, f as TRANSPORT_NETWORK_EXAMPLE, g as wayDraft, h as documentRunways, i as AmbientObserver, l as AMBIENT_ROLE_COMPONENT, m as documentPois, n as AMBIENT_KINDS, o as AmbientState, p as TRANSPORT_NETWORK_FORMAT, r as AmbientAgent, s as AMBIENT_AGENT_COMPONENT, t as AMBIENT_ENTITY, u as AMBIENT_STATE_COMPONENT, v as classifyFeature, x as tilePois, y as clipPolyline } from "./components-DJINUkXK.mjs";
|
|
2
2
|
import { createRng, dmath, lookRotation, quatRotateVec3, seedToInt } from "@bendyline/molen-kernel/determinism";
|
|
3
|
-
import { Transform, componentHandle } from "@bendyline/molen-kernel/world";
|
|
4
|
-
import { deepMergeJson, resolvePrefab, validateByKind } from "@bendyline/molen-schema";
|
|
3
|
+
import { Transform, componentHandle, isBoostMode } from "@bendyline/molen-kernel/world";
|
|
4
|
+
import { boostSpeedMultiplier, deepMergeJson, getSchema, registerSchema, resolvePrefab, validateByKind } from "@bendyline/molen-schema";
|
|
5
5
|
import { z } from "zod";
|
|
6
|
-
import { PATH_SURFACE_LIFT, ROAD_SURFACE_LIFT, bridgeDeckHeightFn, lineSegmentIndex, sampleTerrainLine } from "@bendyline/molen-terrain/kernel";
|
|
6
|
+
import { PATH_SURFACE_LIFT, ROAD_SURFACE_LIFT, bridgeDeckHeightFn, isArterialRoad, lineSegmentIndex, sampleTerrainLine } from "@bendyline/molen-terrain/kernel";
|
|
7
7
|
//#region src/kernel/agents/idm.ts
|
|
8
8
|
/** Intelligent Driver Model: acceleration from speed, desired speed and the gap to a leader. */
|
|
9
9
|
/**
|
|
@@ -277,13 +277,45 @@ function agentRotation(sample, pitch) {
|
|
|
277
277
|
]);
|
|
278
278
|
}
|
|
279
279
|
/** Advance every ambient agent one tick. */
|
|
280
|
-
function stepAgents(world,
|
|
280
|
+
function stepAgents(world, environment) {
|
|
281
|
+
const boosted = isBoostMode(world);
|
|
282
|
+
const scale = boostSpeedMultiplier(boosted);
|
|
283
|
+
const policy = environment.policy;
|
|
284
|
+
const env = {
|
|
285
|
+
...environment,
|
|
286
|
+
speedScale: scale,
|
|
287
|
+
policy: scale === 1 ? policy : {
|
|
288
|
+
...policy,
|
|
289
|
+
driver: {
|
|
290
|
+
...policy.driver,
|
|
291
|
+
accel: policy.driver.accel * scale,
|
|
292
|
+
brake: policy.driver.brake * scale
|
|
293
|
+
},
|
|
294
|
+
rail: {
|
|
295
|
+
...policy.rail,
|
|
296
|
+
accel: policy.rail.accel * scale,
|
|
297
|
+
brake: policy.rail.brake * scale
|
|
298
|
+
},
|
|
299
|
+
marine: {
|
|
300
|
+
...policy.marine,
|
|
301
|
+
accel: policy.marine.accel * scale,
|
|
302
|
+
brake: policy.marine.brake * scale
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
};
|
|
281
306
|
const { net } = env;
|
|
282
307
|
const refs = [];
|
|
283
|
-
for (const [id,
|
|
308
|
+
for (const [id, original] of world.query(AmbientAgent)) {
|
|
309
|
+
const a = {
|
|
310
|
+
...original,
|
|
311
|
+
speed: original.speed * scale / boostSpeedMultiplier(original.boosted)
|
|
312
|
+
};
|
|
313
|
+
if (boosted) a.boosted = true;
|
|
314
|
+
else delete a.boosted;
|
|
284
315
|
const lane = a.air === void 0 ? net.lane(a.lane) : void 0;
|
|
285
316
|
refs.push({
|
|
286
317
|
id,
|
|
318
|
+
original,
|
|
287
319
|
a,
|
|
288
320
|
w: { ...a },
|
|
289
321
|
lane,
|
|
@@ -348,10 +380,10 @@ function stepAgents(world, env) {
|
|
|
348
380
|
for (const ref of refs) if (ref.a.air !== void 0) stepAircraft(ref, env);
|
|
349
381
|
const sample = laneSample();
|
|
350
382
|
for (const ref of refs) {
|
|
351
|
-
const {
|
|
383
|
+
const { w } = ref;
|
|
352
384
|
w.s = q4(w.s);
|
|
353
385
|
w.speed = q4(w.speed);
|
|
354
|
-
if (!agentChanged(
|
|
386
|
+
if (!agentChanged(ref.original, w) && ref.original.boosted === w.boosted && !ref.teleport) continue;
|
|
355
387
|
world.set(ref.id, AmbientAgent, w);
|
|
356
388
|
if (agentSample(net, w, sample) === void 0) continue;
|
|
357
389
|
const current = world.get(ref.id, Transform);
|
|
@@ -426,7 +458,7 @@ function junctionGrants(refs, crossingArms, env) {
|
|
|
426
458
|
for (const ref of refs) {
|
|
427
459
|
const { a, lane } = ref;
|
|
428
460
|
if (a.kind !== "car" || lane === void 0) continue;
|
|
429
|
-
if (a.s < 0) {
|
|
461
|
+
if (a.s < 0 || (env.speedScale ?? 1) > 1 && a.s < a.length / 2) {
|
|
430
462
|
const inMove = net.movement(a.prev, a.lane);
|
|
431
463
|
if (inMove?.junction !== void 0) {
|
|
432
464
|
const list = occupied.get(inMove.junction);
|
|
@@ -479,7 +511,8 @@ function junctionGrants(refs, crossingArms, env) {
|
|
|
479
511
|
const key = entry.move.key;
|
|
480
512
|
const inArm = dmath.floor(key / n);
|
|
481
513
|
const outArm = key % n;
|
|
482
|
-
|
|
514
|
+
const boostConflict = (env.speedScale ?? 1) > 1 && taken.some((other) => other !== key);
|
|
515
|
+
if (entry.committed && !boostConflict) {
|
|
483
516
|
granted.add(entry.ref.id);
|
|
484
517
|
taken.push(key);
|
|
485
518
|
continue;
|
|
@@ -490,8 +523,8 @@ function junctionGrants(refs, crossingArms, env) {
|
|
|
490
523
|
signalOk = arm !== void 0 && signalColor(junction, arm.group, env.tick, env.tickRate, timing) === "green";
|
|
491
524
|
}
|
|
492
525
|
const blocked = walkers !== void 0 && (walkers.has(outArm) || walkers.has(inArm));
|
|
493
|
-
let ok = signalOk && !blocked && !conflictsWith(taken, key);
|
|
494
|
-
if (!ok && signalOk && !blocked && entry.ref.a.state === "wait" && env.tick - entry.ref.a.since > deadlockTicks && !conflictsWith(inside, key)) ok = true;
|
|
526
|
+
let ok = signalOk && !blocked && !boostConflict && !conflictsWith(taken, key);
|
|
527
|
+
if (!ok && signalOk && !blocked && entry.ref.a.state === "wait" && env.tick - entry.ref.a.since > deadlockTicks && !boostConflict && !conflictsWith(inside, key)) ok = true;
|
|
495
528
|
if (ok) {
|
|
496
529
|
granted.add(entry.ref.id);
|
|
497
530
|
taken.push(key);
|
|
@@ -572,7 +605,7 @@ function stepRoad(ref, occupancy, granted, armed, env) {
|
|
|
572
605
|
const rx = solid.x - here.x;
|
|
573
606
|
const rz = solid.z - here.z;
|
|
574
607
|
const forward = rx * here.dx + rz * here.dz;
|
|
575
|
-
if (forward <= 0 || forward > 40) continue;
|
|
608
|
+
if (forward <= 0 || forward > 40 * (env.speedScale ?? 1)) continue;
|
|
576
609
|
if (dmath.abs(rx * here.dz - rz * here.dx) > a.width / 2 + solid.radius + .3) continue;
|
|
577
610
|
const g = forward - a.length / 2 - solid.radius;
|
|
578
611
|
if (g < gap) {
|
|
@@ -581,7 +614,7 @@ function stepRoad(ref, occupancy, granted, armed, env) {
|
|
|
581
614
|
}
|
|
582
615
|
}
|
|
583
616
|
}
|
|
584
|
-
const accel = idmAcceleration(a.speed, v0, gap, a.speed - leaderSpeed, p);
|
|
617
|
+
const accel = idmAcceleration(a.speed, v0 * (env.speedScale ?? 1), gap, a.speed - leaderSpeed, p);
|
|
585
618
|
let v = dmath.max(0, a.speed + accel * dt);
|
|
586
619
|
let s = a.s + v * dt;
|
|
587
620
|
if (s > hardLimit) {
|
|
@@ -651,7 +684,7 @@ function stepWalker(ref, occupancy, env) {
|
|
|
651
684
|
const { a, w } = ref;
|
|
652
685
|
const lane = ref.lane;
|
|
653
686
|
const edge = ref.edge;
|
|
654
|
-
const pace = 1.15 + .5 * unit01(a.seed, 2);
|
|
687
|
+
const pace = (1.15 + .5 * unit01(a.seed, 2)) * (env.speedScale ?? 1);
|
|
655
688
|
if (a.state === "idle") {
|
|
656
689
|
const idleTicks = (3 + 15 * unit01(a.seed, 3e3 + a.hops)) * tickRate;
|
|
657
690
|
if (tick - a.since < idleTicks) {
|
|
@@ -722,7 +755,7 @@ function stepTrainLead(ref, occupancy, env, cars) {
|
|
|
722
755
|
return;
|
|
723
756
|
}
|
|
724
757
|
}
|
|
725
|
-
const v0 = edge.speed * (.92 + .08 * unit01(a.seed, 1));
|
|
758
|
+
const v0 = edge.speed * (.92 + .08 * unit01(a.seed, 1)) * (env.speedScale ?? 1);
|
|
726
759
|
const remaining = lane.length - a.s;
|
|
727
760
|
let gap = Number.POSITIVE_INFINITY;
|
|
728
761
|
let leaderSpeed = 0;
|
|
@@ -936,7 +969,7 @@ function stepVessel(ref, occupancy, env) {
|
|
|
936
969
|
leaderSpeed = 0;
|
|
937
970
|
limit = dmath.min(limit, dock.at);
|
|
938
971
|
}
|
|
939
|
-
const accel = idmAcceleration(a.speed, v0, gap, a.speed - leaderSpeed, p);
|
|
972
|
+
const accel = idmAcceleration(a.speed, v0 * (env.speedScale ?? 1), gap, a.speed - leaderSpeed, p);
|
|
940
973
|
w.speed = dmath.max(0, a.speed + accel * dt);
|
|
941
974
|
w.s = dmath.min(a.s + w.speed * dt, dmath.max(a.s, limit));
|
|
942
975
|
if (w.s >= limit) w.speed = dmath.min(w.speed, leaderSpeed);
|
|
@@ -958,7 +991,7 @@ function stepAircraft(ref, env) {
|
|
|
958
991
|
//#endregion
|
|
959
992
|
//#region src/kernel/graph/junctions.ts
|
|
960
993
|
/**
|
|
961
|
-
* Node groups
|
|
994
|
+
* Node groups → junctions and movements. A node group is a node plus the node it is stitched to
|
|
962
995
|
* across a tile border. For every lane arriving at the group this computes its outgoing
|
|
963
996
|
* movements (turn class, target lane, connector curve), trims lanes back from junction cores, and
|
|
964
997
|
* builds the junction record: arms in angular order, a conflict matrix between arm-pair
|
|
@@ -1085,8 +1118,8 @@ var EndCache = class {
|
|
|
1085
1118
|
return p;
|
|
1086
1119
|
}
|
|
1087
1120
|
};
|
|
1088
|
-
function roadWeight(turn, target) {
|
|
1089
|
-
return (turn === "straight" ? 3 : turn ===
|
|
1121
|
+
function roadWeight(turn, target, drivingSide) {
|
|
1122
|
+
return (turn === "straight" ? 3 : turn === drivingSide ? 2 : turn === "left" || turn === "right" ? 1.5 : turn === "merge" ? 2 : .05) * (target.kind === "service" || target.kind === "living" ? .25 : target.speed >= 15 ? 1.4 : 1);
|
|
1090
1123
|
}
|
|
1091
1124
|
const MAX_CONFLICT_ARMS = 8;
|
|
1092
1125
|
/**
|
|
@@ -1142,9 +1175,9 @@ function buildJunction(store, id, group, arms, radius) {
|
|
|
1142
1175
|
cx /= group.length;
|
|
1143
1176
|
cz /= group.length;
|
|
1144
1177
|
const n = arms.length;
|
|
1145
|
-
const side = store.traffic === "right" ? 1 : -1;
|
|
1146
1178
|
const rim = [];
|
|
1147
1179
|
arms.forEach((arm, k) => {
|
|
1180
|
+
const side = arm.edge.drivingSide === "right" ? 1 : -1;
|
|
1148
1181
|
const bx = arm.node.x + arm.dx * radius - cx;
|
|
1149
1182
|
const bz = arm.node.z + arm.dz * radius - cz;
|
|
1150
1183
|
const q = arm.edge.width / 4;
|
|
@@ -1213,13 +1246,20 @@ function buildJunction(store, id, group, arms, radius) {
|
|
|
1213
1246
|
if (best >= 0) junctionArms[best].group = groups;
|
|
1214
1247
|
groups++;
|
|
1215
1248
|
}
|
|
1216
|
-
const major = arms.
|
|
1249
|
+
const major = arms.filter((a) => isArterialRoad(a.edge.kind, a.edge.width)).length;
|
|
1217
1250
|
const minor = arms.filter((a) => a.edge.kind === "service" || a.edge.kind === "living" || a.edge.link).length;
|
|
1218
1251
|
let signed = 0;
|
|
1219
|
-
for (const sign of store.giveWayNear(cx, cz, radius + 20)) {
|
|
1252
|
+
for (const sign of store.giveWayNear(cx, cz, radius + 20, group[0]?.layer)) {
|
|
1220
1253
|
const sx = sign.x - cx;
|
|
1221
1254
|
const sz = sign.z - cz;
|
|
1222
1255
|
const length = dmath.hypot(sx, sz);
|
|
1256
|
+
if (sign.allWay && length <= 4) {
|
|
1257
|
+
for (const arm of junctionArms) {
|
|
1258
|
+
if (!arm.yields) signed++;
|
|
1259
|
+
arm.yields = true;
|
|
1260
|
+
}
|
|
1261
|
+
continue;
|
|
1262
|
+
}
|
|
1223
1263
|
let best;
|
|
1224
1264
|
let bestDot = Number.NEGATIVE_INFINITY;
|
|
1225
1265
|
for (const arm of junctionArms) {
|
|
@@ -1235,8 +1275,8 @@ function buildJunction(store, id, group, arms, radius) {
|
|
|
1235
1275
|
}
|
|
1236
1276
|
}
|
|
1237
1277
|
let control = "none";
|
|
1238
|
-
if (groups >= 2 && (
|
|
1239
|
-
else if (minor > 0 && minor < n || signed > 0
|
|
1278
|
+
if (groups >= 2 && (store.signalNear(cx, cz, radius + 20, group[0]?.layer) || n >= 4 && major >= 4 && signed === 0)) control = "signal";
|
|
1279
|
+
else if (minor > 0 && minor < n || signed > 0) control = "yield";
|
|
1240
1280
|
return {
|
|
1241
1281
|
id,
|
|
1242
1282
|
x: cx,
|
|
@@ -1296,8 +1336,8 @@ function movement(cache, from, to, turn, weight, junction, key, crosses) {
|
|
|
1296
1336
|
}
|
|
1297
1337
|
function buildRoadMovements(store, cache, arms, junction) {
|
|
1298
1338
|
const n = arms.length;
|
|
1299
|
-
const left = store.traffic === "left";
|
|
1300
1339
|
arms.forEach((inArm, i) => {
|
|
1340
|
+
const left = inArm.edge.drivingSide === "left";
|
|
1301
1341
|
const incoming = lanesAt(store, inArm, "road", true);
|
|
1302
1342
|
const nIn = incoming.length;
|
|
1303
1343
|
for (const lane of incoming) {
|
|
@@ -1328,11 +1368,11 @@ function buildRoadMovements(store, cache, arms, junction) {
|
|
|
1328
1368
|
else if (turn !== nearSide && inner) target = outgoing[0];
|
|
1329
1369
|
if (target === void 0) return;
|
|
1330
1370
|
const moveTurn = inArm.edge.link && turn === "straight" ? "merge" : turn;
|
|
1331
|
-
const move = movement(cache, lane, target, moveTurn, roadWeight(moveTurn, outArm.edge), n >= 3 ? junction : void 0, i * n + o);
|
|
1371
|
+
const move = movement(cache, lane, target, moveTurn, roadWeight(moveTurn, outArm.edge, inArm.edge.drivingSide), n >= 3 ? junction : void 0, i * n + o);
|
|
1332
1372
|
if (move !== void 0) moves.push(move);
|
|
1333
1373
|
});
|
|
1334
1374
|
if (moves.length === 0) for (const f of fallback) {
|
|
1335
|
-
const move = movement(cache, lane, f.lane, f.turn, roadWeight(f.turn, store.edge(f.lane.edge) ?? inArm.edge), n >= 3 ? junction : void 0, i * n + f.o);
|
|
1375
|
+
const move = movement(cache, lane, f.lane, f.turn, roadWeight(f.turn, store.edge(f.lane.edge) ?? inArm.edge, inArm.edge.drivingSide), n >= 3 ? junction : void 0, i * n + f.o);
|
|
1336
1376
|
if (move !== void 0) moves.push(move);
|
|
1337
1377
|
}
|
|
1338
1378
|
if (moves.length === 0) {
|
|
@@ -1722,7 +1762,9 @@ const CLASSES = [
|
|
|
1722
1762
|
"water"
|
|
1723
1763
|
];
|
|
1724
1764
|
var LaneGrid = class {
|
|
1725
|
-
cells
|
|
1765
|
+
/** Rows by cell z, then cells by cell x: integer keys, no string per probed cell. */
|
|
1766
|
+
rows = /* @__PURE__ */ new Map();
|
|
1767
|
+
cellCount = 0;
|
|
1726
1768
|
edgeCells = /* @__PURE__ */ new Map();
|
|
1727
1769
|
/** Bumped on every change; lets callers cache derived sums. */
|
|
1728
1770
|
version = 0;
|
|
@@ -1744,8 +1786,14 @@ var LaneGrid = class {
|
|
|
1744
1786
|
const t = s1 > s0 ? (mid - s0) / (s1 - s0) : 0;
|
|
1745
1787
|
const x = a[0] + (b[0] - a[0]) * t;
|
|
1746
1788
|
const z = a[1] + (b[1] - a[1]) * t;
|
|
1747
|
-
const
|
|
1748
|
-
|
|
1789
|
+
const cx = dmath.floor(x / 64);
|
|
1790
|
+
const cz = dmath.floor(z / 64);
|
|
1791
|
+
let row = this.rows.get(cz);
|
|
1792
|
+
if (row === void 0) {
|
|
1793
|
+
row = /* @__PURE__ */ new Map();
|
|
1794
|
+
this.rows.set(cz, row);
|
|
1795
|
+
}
|
|
1796
|
+
let cell = row.get(cx);
|
|
1749
1797
|
if (cell === void 0) {
|
|
1750
1798
|
cell = {
|
|
1751
1799
|
spans: [],
|
|
@@ -1757,10 +1805,13 @@ var LaneGrid = class {
|
|
|
1757
1805
|
water: 0
|
|
1758
1806
|
}
|
|
1759
1807
|
};
|
|
1760
|
-
|
|
1808
|
+
row.set(cx, cell);
|
|
1809
|
+
this.cellCount++;
|
|
1761
1810
|
}
|
|
1762
1811
|
cell.spans.push({
|
|
1763
1812
|
edge: edge.id,
|
|
1813
|
+
ref: edge,
|
|
1814
|
+
lanes: laneCounts,
|
|
1764
1815
|
d0,
|
|
1765
1816
|
d1,
|
|
1766
1817
|
x,
|
|
@@ -1779,7 +1830,8 @@ var LaneGrid = class {
|
|
|
1779
1830
|
meters[cls] = m;
|
|
1780
1831
|
}
|
|
1781
1832
|
touched.push({
|
|
1782
|
-
|
|
1833
|
+
cx,
|
|
1834
|
+
cz,
|
|
1783
1835
|
meters
|
|
1784
1836
|
});
|
|
1785
1837
|
}
|
|
@@ -1787,32 +1839,84 @@ var LaneGrid = class {
|
|
|
1787
1839
|
this.version++;
|
|
1788
1840
|
}
|
|
1789
1841
|
remove(edgeId) {
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1842
|
+
this.removeAll([edgeId]);
|
|
1843
|
+
}
|
|
1844
|
+
/**
|
|
1845
|
+
* Remove edges together, each touched cell's spans filtered once: a tile's edges share its
|
|
1846
|
+
* cells, and filtering a dense city cell again for each of its edges held a frame 5 ms as the
|
|
1847
|
+
* tile left.
|
|
1848
|
+
*/
|
|
1849
|
+
removeAll(edgeIds) {
|
|
1850
|
+
const gone = /* @__PURE__ */ new Set();
|
|
1851
|
+
const cells = /* @__PURE__ */ new Map();
|
|
1852
|
+
for (const edgeId of edgeIds) {
|
|
1853
|
+
const touched = this.edgeCells.get(edgeId);
|
|
1854
|
+
if (touched === void 0) continue;
|
|
1855
|
+
gone.add(edgeId);
|
|
1856
|
+
for (const { cx, cz, meters } of touched) {
|
|
1857
|
+
const row = this.rows.get(cz);
|
|
1858
|
+
const cell = row?.get(cx);
|
|
1859
|
+
if (row === void 0 || cell === void 0) continue;
|
|
1860
|
+
for (const cls of CLASSES) cell.meters[cls] = dmath.max(0, cell.meters[cls] - meters[cls]);
|
|
1861
|
+
cells.set(cell, {
|
|
1862
|
+
row,
|
|
1863
|
+
cx,
|
|
1864
|
+
cz
|
|
1865
|
+
});
|
|
1866
|
+
}
|
|
1867
|
+
this.edgeCells.delete(edgeId);
|
|
1868
|
+
}
|
|
1869
|
+
if (gone.size === 0) return;
|
|
1870
|
+
for (const [cell, { row, cx, cz }] of cells) {
|
|
1871
|
+
cell.spans = cell.spans.filter((span) => !gone.has(span.edge));
|
|
1872
|
+
if (cell.spans.length === 0) {
|
|
1873
|
+
row.delete(cx);
|
|
1874
|
+
this.cellCount--;
|
|
1875
|
+
if (row.size === 0) this.rows.delete(cz);
|
|
1876
|
+
}
|
|
1798
1877
|
}
|
|
1799
|
-
this.edgeCells.delete(edgeId);
|
|
1800
1878
|
this.version++;
|
|
1801
1879
|
}
|
|
1802
|
-
/**
|
|
1803
|
-
*
|
|
1880
|
+
/**
|
|
1881
|
+
* Cells overlapping the disc of radius `r`, in row-major order (z, then x, ascending). A train
|
|
1882
|
+
* or boat ring spans thousands of mostly empty cells: probing each one cost a fast flight
|
|
1883
|
+
* several milliseconds per spawner run, so a disc larger than the populated grid walks the
|
|
1884
|
+
* populated cells instead and sorts the matches into the same order.
|
|
1885
|
+
*/
|
|
1886
|
+
cellsNear(x, z, r) {
|
|
1804
1887
|
const x0 = dmath.floor((x - r) / 64);
|
|
1805
1888
|
const x1 = dmath.floor((x + r) / 64);
|
|
1806
1889
|
const z0 = dmath.floor((z - r) / 64);
|
|
1807
1890
|
const z1 = dmath.floor((z + r) / 64);
|
|
1808
|
-
|
|
1809
|
-
const cell = this.cells.get(`${cx}/${cz}`);
|
|
1810
|
-
if (cell === void 0) continue;
|
|
1891
|
+
const within = (cx, cz) => {
|
|
1811
1892
|
const nx = dmath.clamp(x, cx * 64, (cx + 1) * 64);
|
|
1812
1893
|
const nz = dmath.clamp(z, cz * 64, (cz + 1) * 64);
|
|
1813
|
-
|
|
1814
|
-
|
|
1894
|
+
return dmath.hypot(nx - x, nz - z) <= r;
|
|
1895
|
+
};
|
|
1896
|
+
const out = [];
|
|
1897
|
+
if ((x1 - x0 + 1) * (z1 - z0 + 1) <= this.cellCount) {
|
|
1898
|
+
for (let cz = z0; cz <= z1; cz++) {
|
|
1899
|
+
const row = this.rows.get(cz);
|
|
1900
|
+
if (row === void 0) continue;
|
|
1901
|
+
for (let cx = x0; cx <= x1; cx++) {
|
|
1902
|
+
const cell = row.get(cx);
|
|
1903
|
+
if (cell !== void 0 && within(cx, cz)) out.push(cell);
|
|
1904
|
+
}
|
|
1905
|
+
}
|
|
1906
|
+
return out;
|
|
1815
1907
|
}
|
|
1908
|
+
const found = [];
|
|
1909
|
+
for (const [cz, row] of this.rows) {
|
|
1910
|
+
if (cz < z0 || cz > z1) continue;
|
|
1911
|
+
for (const [cx, cell] of row) if (cx >= x0 && cx <= x1 && within(cx, cz)) found.push({
|
|
1912
|
+
cx,
|
|
1913
|
+
cz,
|
|
1914
|
+
cell
|
|
1915
|
+
});
|
|
1916
|
+
}
|
|
1917
|
+
found.sort((a, b) => a.cz - b.cz || a.cx - b.cx);
|
|
1918
|
+
for (const entry of found) out.push(entry.cell);
|
|
1919
|
+
return out;
|
|
1816
1920
|
}
|
|
1817
1921
|
/** Lane-metres of a class in cells overlapping the disc. */
|
|
1818
1922
|
laneMeters(x, z, r, cls) {
|
|
@@ -1842,7 +1946,8 @@ var LaneGrid = class {
|
|
|
1842
1946
|
return out;
|
|
1843
1947
|
}
|
|
1844
1948
|
clear() {
|
|
1845
|
-
this.
|
|
1949
|
+
this.rows.clear();
|
|
1950
|
+
this.cellCount = 0;
|
|
1846
1951
|
this.edgeCells.clear();
|
|
1847
1952
|
this.version++;
|
|
1848
1953
|
}
|
|
@@ -1876,8 +1981,14 @@ var TransportNetwork = class {
|
|
|
1876
1981
|
nodes = /* @__PURE__ */ new Map();
|
|
1877
1982
|
edges = /* @__PURE__ */ new Map();
|
|
1878
1983
|
lanes = /* @__PURE__ */ new Map();
|
|
1984
|
+
/** Each edge's lane count per class (see lanesOfClass), kept with its lanes. */
|
|
1985
|
+
classLanes = /* @__PURE__ */ new Map();
|
|
1879
1986
|
junctions = /* @__PURE__ */ new Map();
|
|
1880
1987
|
tiles = /* @__PURE__ */ new Map();
|
|
1988
|
+
/**
|
|
1989
|
+
* Border nodes by class and layer, then by rounded position (see borderCell): stitching probes
|
|
1990
|
+
* the 25 positions around each new border node, and a string key for each was most of it.
|
|
1991
|
+
*/
|
|
1881
1992
|
borders = /* @__PURE__ */ new Map();
|
|
1882
1993
|
crossings = /* @__PURE__ */ new Map();
|
|
1883
1994
|
crossingsByEdge = /* @__PURE__ */ new Map();
|
|
@@ -1917,6 +2028,13 @@ var TransportNetwork = class {
|
|
|
1917
2028
|
lane(id) {
|
|
1918
2029
|
return this.lanes.get(id);
|
|
1919
2030
|
}
|
|
2031
|
+
/**
|
|
2032
|
+
* How many of an edge's lanes are of `cls`. A city's spawn ring is tens of thousands of spans,
|
|
2033
|
+
* and looking up every lane of each one held a spawning tick near 10 ms.
|
|
2034
|
+
*/
|
|
2035
|
+
lanesOfClass(edgeId, cls) {
|
|
2036
|
+
return this.classLanes.get(edgeId)?.[cls] ?? 0;
|
|
2037
|
+
}
|
|
1920
2038
|
junction(id) {
|
|
1921
2039
|
return this.junctions.get(id);
|
|
1922
2040
|
}
|
|
@@ -1926,17 +2044,17 @@ var TransportNetwork = class {
|
|
|
1926
2044
|
deleteJunction(id) {
|
|
1927
2045
|
this.junctions.delete(id);
|
|
1928
2046
|
}
|
|
1929
|
-
signalNear(x, z, radius) {
|
|
1930
|
-
return this.controlsNear(x, z, radius, "traffic_signals").length > 0;
|
|
2047
|
+
signalNear(x, z, radius, layer = 0) {
|
|
2048
|
+
return this.controlsNear(x, z, radius, "traffic_signals", layer).length > 0;
|
|
1931
2049
|
}
|
|
1932
|
-
giveWayNear(x, z, radius) {
|
|
1933
|
-
return this.controlsNear(x, z, radius, "give_way");
|
|
2050
|
+
giveWayNear(x, z, radius, layer = 0) {
|
|
2051
|
+
return this.controlsNear(x, z, radius, "give_way", layer);
|
|
1934
2052
|
}
|
|
1935
|
-
controlsNear(x, z, radius, kind) {
|
|
2053
|
+
controlsNear(x, z, radius, kind, layer) {
|
|
1936
2054
|
const cx = dmath.floor(x / 64);
|
|
1937
2055
|
const cz = dmath.floor(z / 64);
|
|
1938
2056
|
const found = [];
|
|
1939
|
-
for (let ox = -1; ox <= 1; ox++) for (let oz = -1; oz <= 1; oz++) for (const poi of this.signalCells.get(`${cx + ox}/${cz + oz}`) ?? []) if (poi.kind === kind && dmath.hypot(poi.x - x, poi.z - z) <= radius) found.push(poi);
|
|
2057
|
+
for (let ox = -1; ox <= 1; ox++) for (let oz = -1; oz <= 1; oz++) for (const poi of this.signalCells.get(`${cx + ox}/${cz + oz}`) ?? []) if (poi.kind === kind && (poi.layer ?? 0) === layer && dmath.hypot(poi.x - x, poi.z - z) <= radius) found.push(poi);
|
|
1940
2058
|
return found;
|
|
1941
2059
|
}
|
|
1942
2060
|
/** Whether a tile key is registered. */
|
|
@@ -1954,9 +2072,12 @@ var TransportNetwork = class {
|
|
|
1954
2072
|
return this.junctions.values();
|
|
1955
2073
|
}
|
|
1956
2074
|
runways() {
|
|
1957
|
-
const
|
|
1958
|
-
for (const tile of this.tiles.values())
|
|
1959
|
-
|
|
2075
|
+
const unique = /* @__PURE__ */ new Map();
|
|
2076
|
+
for (const tile of this.tiles.values()) for (const runway of tile.runways) {
|
|
2077
|
+
const ends = [`${Math.round(runway.ax)},${Math.round(runway.az)}`, `${Math.round(runway.bx)},${Math.round(runway.bz)}`].sort();
|
|
2078
|
+
unique.set(ends.join("/"), runway);
|
|
2079
|
+
}
|
|
2080
|
+
return [...unique.values()];
|
|
1960
2081
|
}
|
|
1961
2082
|
/** Aerodrome POIs (and runway midpoints) across registered tiles. */
|
|
1962
2083
|
aerodromes() {
|
|
@@ -1978,8 +2099,14 @@ var TransportNetwork = class {
|
|
|
1978
2099
|
registerTile(input) {
|
|
1979
2100
|
if (this.tiles.has(input.key)) this.unregisterTile(input.key);
|
|
1980
2101
|
const { drafts, runways } = featureDrafts(input, { tunnels: this.tunnels });
|
|
1981
|
-
const
|
|
1982
|
-
const
|
|
2102
|
+
for (const draft of drafts) draft.drivingSide = input.drivingSide ?? this.traffic;
|
|
2103
|
+
const pois = tilePois(input.pois, input.origin, input.tileSize, input.bounds);
|
|
2104
|
+
const rect = input.bounds ? [
|
|
2105
|
+
input.bounds[0],
|
|
2106
|
+
input.bounds[1],
|
|
2107
|
+
input.bounds[2],
|
|
2108
|
+
input.bounds[3]
|
|
2109
|
+
] : [
|
|
1983
2110
|
input.origin[0],
|
|
1984
2111
|
input.origin[1],
|
|
1985
2112
|
input.origin[0] + input.tileSize,
|
|
@@ -2002,24 +2129,32 @@ var TransportNetwork = class {
|
|
|
2002
2129
|
}
|
|
2003
2130
|
}
|
|
2004
2131
|
}
|
|
2132
|
+
const gone = [];
|
|
2005
2133
|
for (const edgeId of tile.edges) {
|
|
2006
2134
|
const edge = this.edges.get(edgeId);
|
|
2007
2135
|
if (edge === void 0) continue;
|
|
2008
2136
|
for (const laneId of edge.lanes) this.lanes.delete(laneId);
|
|
2009
|
-
this.
|
|
2137
|
+
this.classLanes.delete(edgeId);
|
|
2138
|
+
gone.push(edgeId);
|
|
2010
2139
|
this.edges.delete(edgeId);
|
|
2011
2140
|
this.crossingsByEdge.delete(edgeId);
|
|
2012
2141
|
}
|
|
2142
|
+
this.grid.removeAll(gone);
|
|
2013
2143
|
for (const id of tile.crossings) this.crossings.delete(id);
|
|
2014
2144
|
for (const nodeId of tile.nodes) {
|
|
2015
2145
|
const node = this.nodes.get(nodeId);
|
|
2016
2146
|
if (node === void 0) continue;
|
|
2017
2147
|
this.junctions.delete(nodeId);
|
|
2018
2148
|
if (node.border) {
|
|
2019
|
-
const
|
|
2020
|
-
const
|
|
2021
|
-
|
|
2022
|
-
|
|
2149
|
+
const group = borderGroup(node);
|
|
2150
|
+
const cells = this.borders.get(group);
|
|
2151
|
+
const cell = borderCell(dmath.round(node.x), dmath.round(node.z));
|
|
2152
|
+
const list = cells?.get(cell)?.filter((id) => id !== nodeId) ?? [];
|
|
2153
|
+
if (list.length > 0) cells?.set(cell, list);
|
|
2154
|
+
else {
|
|
2155
|
+
cells?.delete(cell);
|
|
2156
|
+
if (cells?.size === 0) this.borders.delete(group);
|
|
2157
|
+
}
|
|
2023
2158
|
}
|
|
2024
2159
|
this.nodes.delete(nodeId);
|
|
2025
2160
|
}
|
|
@@ -2031,7 +2166,10 @@ var TransportNetwork = class {
|
|
|
2031
2166
|
/** Add a transport-network document's ways, stations, signals and aerodromes. */
|
|
2032
2167
|
addDocument(doc) {
|
|
2033
2168
|
for (const way of doc.ways) {
|
|
2034
|
-
const draft = wayDraft(
|
|
2169
|
+
const draft = wayDraft({
|
|
2170
|
+
...way,
|
|
2171
|
+
drivingSide: way.drivingSide ?? doc.drivingSide ?? this.traffic
|
|
2172
|
+
});
|
|
2035
2173
|
if (draft !== void 0) this.docDrafts.push(draft);
|
|
2036
2174
|
}
|
|
2037
2175
|
this.docPois.push(...documentPois(doc));
|
|
@@ -2122,12 +2260,13 @@ var TransportNetwork = class {
|
|
|
2122
2260
|
ys[i] = h;
|
|
2123
2261
|
});
|
|
2124
2262
|
else if (draft.class === "water") ys.fill(0);
|
|
2125
|
-
else if (draft.bridge || draft.bridgeConnections?.length) {
|
|
2263
|
+
else if (draft.bridge || draft.bridgeConnections?.length || draft.bridgeApproachProfile?.length) {
|
|
2126
2264
|
const deck = bridgeDeckHeightFn(path, heightAt, {
|
|
2127
2265
|
...draft.deckElevation !== void 0 ? { deckElevation: draft.deckElevation } : {},
|
|
2128
2266
|
clearance: draft.class === "walk" ? 3 : 6,
|
|
2129
2267
|
approachLift: lift,
|
|
2130
2268
|
groundApproach: !draft.bridge,
|
|
2269
|
+
approachProfile: draft.bridgeApproachProfile,
|
|
2131
2270
|
connects: [gradeRoad.has(piece.from), gradeRoad.has(piece.to)],
|
|
2132
2271
|
...draft.bridgeConnections !== void 0 ? { connections: draft.bridgeConnections } : {}
|
|
2133
2272
|
});
|
|
@@ -2158,6 +2297,7 @@ var TransportNetwork = class {
|
|
|
2158
2297
|
tunnel: draft.tunnel,
|
|
2159
2298
|
link: draft.link,
|
|
2160
2299
|
oneway: draft.oneway,
|
|
2300
|
+
drivingSide: draft.drivingSide ?? this.traffic,
|
|
2161
2301
|
lanes: [],
|
|
2162
2302
|
...draft.name !== void 0 ? { name: draft.name } : {}
|
|
2163
2303
|
};
|
|
@@ -2194,7 +2334,7 @@ var TransportNetwork = class {
|
|
|
2194
2334
|
this.version++;
|
|
2195
2335
|
}
|
|
2196
2336
|
createLanes(edge, draft, sidewalks) {
|
|
2197
|
-
const side =
|
|
2337
|
+
const side = edge.drivingSide === "right" ? 1 : -1;
|
|
2198
2338
|
const make = (code, cls, dir, index, offset, lift = 0) => {
|
|
2199
2339
|
const id = `${edge.id}>${code}`;
|
|
2200
2340
|
const lane = {
|
|
@@ -2215,6 +2355,12 @@ var TransportNetwork = class {
|
|
|
2215
2355
|
};
|
|
2216
2356
|
this.lanes.set(id, lane);
|
|
2217
2357
|
edge.lanes.push(id);
|
|
2358
|
+
let counts = this.classLanes.get(edge.id);
|
|
2359
|
+
if (counts === void 0) {
|
|
2360
|
+
counts = {};
|
|
2361
|
+
this.classLanes.set(edge.id, counts);
|
|
2362
|
+
}
|
|
2363
|
+
counts[cls] = (counts[cls] ?? 0) + 1;
|
|
2218
2364
|
};
|
|
2219
2365
|
if (draft.class === "road") {
|
|
2220
2366
|
const total = draft.lanes;
|
|
@@ -2247,8 +2393,15 @@ var TransportNetwork = class {
|
|
|
2247
2393
|
}
|
|
2248
2394
|
}
|
|
2249
2395
|
stitch(node) {
|
|
2250
|
-
const
|
|
2251
|
-
|
|
2396
|
+
const group = borderGroup(node);
|
|
2397
|
+
let cells = this.borders.get(group);
|
|
2398
|
+
if (cells === void 0) {
|
|
2399
|
+
cells = /* @__PURE__ */ new Map();
|
|
2400
|
+
this.borders.set(group, cells);
|
|
2401
|
+
}
|
|
2402
|
+
const bx = dmath.round(node.x);
|
|
2403
|
+
const bz = dmath.round(node.z);
|
|
2404
|
+
const own = borderCell(bx, bz);
|
|
2252
2405
|
let best;
|
|
2253
2406
|
let bestDistance = 1.01;
|
|
2254
2407
|
const consider = (ids, limit) => {
|
|
@@ -2262,16 +2415,13 @@ var TransportNetwork = class {
|
|
|
2262
2415
|
}
|
|
2263
2416
|
}
|
|
2264
2417
|
};
|
|
2265
|
-
consider(
|
|
2418
|
+
consider(cells.get(own) ?? [], 1.01);
|
|
2266
2419
|
if (best === void 0) {
|
|
2267
2420
|
bestDistance = 2.01;
|
|
2268
|
-
|
|
2269
|
-
const bx = dmath.round(node.x);
|
|
2270
|
-
const bz = dmath.round(node.z);
|
|
2271
|
-
for (let ox = -2; ox <= 2; ox++) for (let oz = -2; oz <= 2; oz++) consider(this.borders.get(`${cls}/${layer}/${bx + ox}/${bz + oz}`) ?? [], 2.01);
|
|
2421
|
+
for (let ox = -2; ox <= 2; ox++) for (let oz = -2; oz <= 2; oz++) consider(cells.get(borderCell(bx + ox, bz + oz)) ?? [], 2.01);
|
|
2272
2422
|
}
|
|
2273
|
-
const list =
|
|
2274
|
-
if (list === void 0)
|
|
2423
|
+
const list = cells.get(own);
|
|
2424
|
+
if (list === void 0) cells.set(own, [node.id]);
|
|
2275
2425
|
else list.push(node.id);
|
|
2276
2426
|
const partner = best;
|
|
2277
2427
|
if (partner !== void 0) {
|
|
@@ -2408,6 +2558,16 @@ var TransportNetwork = class {
|
|
|
2408
2558
|
}
|
|
2409
2559
|
return best;
|
|
2410
2560
|
}
|
|
2561
|
+
/** The network's sizes, without the walks `stats()` makes. */
|
|
2562
|
+
sizes() {
|
|
2563
|
+
return {
|
|
2564
|
+
tiles: this.tiles.size,
|
|
2565
|
+
nodes: this.nodes.size,
|
|
2566
|
+
edges: this.edges.size,
|
|
2567
|
+
lanes: this.lanes.size,
|
|
2568
|
+
junctions: this.junctions.size
|
|
2569
|
+
};
|
|
2570
|
+
}
|
|
2411
2571
|
stats() {
|
|
2412
2572
|
const laneKm = {
|
|
2413
2573
|
road: 0,
|
|
@@ -2450,8 +2610,12 @@ var TransportNetwork = class {
|
|
|
2450
2610
|
function q(value) {
|
|
2451
2611
|
return dmath.round(value * 10);
|
|
2452
2612
|
}
|
|
2453
|
-
function
|
|
2454
|
-
return `${node.class}/${node.layer}
|
|
2613
|
+
function borderGroup(node) {
|
|
2614
|
+
return `${node.class}/${node.layer}`;
|
|
2615
|
+
}
|
|
2616
|
+
/** A rounded border position as one number: exact for metres within 33,000 km of the origin. */
|
|
2617
|
+
function borderCell(x, z) {
|
|
2618
|
+
return (x + 2 ** 25) * 2 ** 26 + (z + 2 ** 25);
|
|
2455
2619
|
}
|
|
2456
2620
|
function linePath(points) {
|
|
2457
2621
|
const distances = cumulative(points);
|
|
@@ -3207,7 +3371,8 @@ function spawnAgents(world, env, observer, state) {
|
|
|
3207
3371
|
observer,
|
|
3208
3372
|
rng,
|
|
3209
3373
|
occupancy,
|
|
3210
|
-
state
|
|
3374
|
+
state,
|
|
3375
|
+
candidates: /* @__PURE__ */ new Map()
|
|
3211
3376
|
};
|
|
3212
3377
|
const targets = {};
|
|
3213
3378
|
for (const kind of AMBIENT_KINDS) targets[kind] = target(ctx, kind);
|
|
@@ -3235,9 +3400,10 @@ function target(ctx, kind) {
|
|
|
3235
3400
|
if (kind === "train" && meters > 400) return dmath.min(c.max, dmath.max(1, dmath.round(wanted)));
|
|
3236
3401
|
return dmath.min(c.max, dmath.round(wanted));
|
|
3237
3402
|
}
|
|
3238
|
-
function
|
|
3239
|
-
|
|
3240
|
-
|
|
3403
|
+
function spawnCandidates(ctx, kind) {
|
|
3404
|
+
const cached = ctx.candidates.get(kind);
|
|
3405
|
+
if (cached !== void 0) return cached;
|
|
3406
|
+
const { env, observer } = ctx;
|
|
3241
3407
|
const { net } = env;
|
|
3242
3408
|
const c = classPolicy(env.policy, kind);
|
|
3243
3409
|
const cls = LANE_CLASS[kind];
|
|
@@ -3246,15 +3412,29 @@ function spawnOne(ctx, kind) {
|
|
|
3246
3412
|
const edges = [];
|
|
3247
3413
|
let total = 0;
|
|
3248
3414
|
for (const span of spans) {
|
|
3249
|
-
const edge =
|
|
3250
|
-
let
|
|
3251
|
-
for (const id of edge.lanes) if (net.lane(id)?.class === cls) lanes++;
|
|
3252
|
-
let w = lanes * (span.d1 - span.d0);
|
|
3415
|
+
const edge = span.ref;
|
|
3416
|
+
let w = span.lanes[cls] * (span.d1 - span.d0);
|
|
3253
3417
|
if (kind === "car") w *= SPAWN_WEIGHT[edge.kind] ?? 1;
|
|
3254
3418
|
weights.push(w);
|
|
3255
3419
|
edges.push(edge);
|
|
3256
3420
|
total += w;
|
|
3257
3421
|
}
|
|
3422
|
+
const candidates = {
|
|
3423
|
+
spans,
|
|
3424
|
+
edges,
|
|
3425
|
+
weights,
|
|
3426
|
+
total
|
|
3427
|
+
};
|
|
3428
|
+
ctx.candidates.set(kind, candidates);
|
|
3429
|
+
return candidates;
|
|
3430
|
+
}
|
|
3431
|
+
function spawnOne(ctx, kind) {
|
|
3432
|
+
if (kind === "aircraft") return spawnAircraft(ctx);
|
|
3433
|
+
const { env, observer, rng } = ctx;
|
|
3434
|
+
const { net } = env;
|
|
3435
|
+
const c = classPolicy(env.policy, kind);
|
|
3436
|
+
const cls = LANE_CLASS[kind];
|
|
3437
|
+
const { spans, edges, weights, total } = spawnCandidates(ctx, kind);
|
|
3258
3438
|
if (total <= 0) return false;
|
|
3259
3439
|
const types = env.types[kind === "pedestrian" ? "pedestrian" : kind];
|
|
3260
3440
|
for (let attempt = 0; attempt < 6; attempt++) {
|
|
@@ -3272,16 +3452,16 @@ function spawnOne(ctx, kind) {
|
|
|
3272
3452
|
if (lane === void 0) continue;
|
|
3273
3453
|
const seq = ctx.state.nextSeq;
|
|
3274
3454
|
const seed = fmix32(seq ^ env.seed);
|
|
3275
|
-
const
|
|
3276
|
-
|
|
3455
|
+
const s = laneCoordinate(lane, edge, span.d0 + rng.next() * (span.d1 - span.d0));
|
|
3456
|
+
const sample = sampleLaneBody(lane, edge, s, laneSample());
|
|
3457
|
+
const available = kind === "car" && env.carsAt ? env.carsAt(sample.x, sample.z) : kind === "boat" ? types.filter((t) => (t.waterway ?? "channel") === edge.kind && t.width <= edge.width / (edge.oneway ? 1 : 2) && t.length + 2 < lane.length) : types;
|
|
3458
|
+
if ((kind === "boat" || kind === "car") && available.length === 0) continue;
|
|
3277
3459
|
const vehicle = kind === "pedestrian" ? void 0 : pickWeighted(available, unit01(seed, 10));
|
|
3278
3460
|
const length = kind === "pedestrian" ? .5 : vehicle?.length ?? 4.5;
|
|
3279
3461
|
const width = kind === "pedestrian" ? .5 : vehicle?.width ?? 1.8;
|
|
3280
3462
|
const cars = kind === "train" ? dmath.max(1, vehicle?.cars ?? 2) : 1;
|
|
3281
3463
|
const behind = kind === "train" ? (cars - 1) * (length + 1) : 0;
|
|
3282
|
-
const s = laneCoordinate(lane, edge, span.d0 + rng.next() * (span.d1 - span.d0));
|
|
3283
3464
|
if (s < behind + length / 2 + 1 || s > lane.length - length / 2 - 1) continue;
|
|
3284
|
-
const sample = sampleLaneBody(lane, edge, s, laneSample());
|
|
3285
3465
|
const dx = sample.x - observer.x;
|
|
3286
3466
|
const dz = sample.z - observer.z;
|
|
3287
3467
|
const distance = dmath.hypot(dx, dz);
|
|
@@ -3401,8 +3581,8 @@ function spawnAircraft(ctx) {
|
|
|
3401
3581
|
const width = vehicle?.width ?? 36;
|
|
3402
3582
|
let corridor;
|
|
3403
3583
|
let speed = vehicle?.cruise ?? 75 + 55 * rng.next();
|
|
3404
|
-
const runways = env.net.runways().filter((r) => dmath.hypot((r.ax + r.bx) / 2 - observer.x, (r.az + r.bz) / 2 - observer.z) < 6e3);
|
|
3405
|
-
if (runways.length > 0 && rng.next() < .
|
|
3584
|
+
const runways = env.net.runways().filter((r) => dmath.hypot((r.ax + r.bx) / 2 - observer.x, (r.az + r.bz) / 2 - observer.z) < 6e3 && dmath.hypot(r.bx - r.ax, r.bz - r.az) >= dmath.max(350, length * 35));
|
|
3585
|
+
if (runways.length > 0 && rng.next() < .85) {
|
|
3406
3586
|
const runway = runways[rng.int(runways.length)];
|
|
3407
3587
|
const flip = rng.next() < .5;
|
|
3408
3588
|
const ax = flip ? runway.bx : runway.ax;
|
|
@@ -3585,6 +3765,43 @@ function installAmbient(world, options = {}) {
|
|
|
3585
3765
|
for (const change of pending.splice(0)) if (change.op === "add") network.registerTile(change.tile);
|
|
3586
3766
|
else network.unregisterTile(change.key);
|
|
3587
3767
|
};
|
|
3768
|
+
const perTick = options.registrationsPerTick ?? Number.POSITIVE_INFINITY;
|
|
3769
|
+
const removalsPerTick = options.removalsPerTick ?? Number.POSITIVE_INFINITY;
|
|
3770
|
+
/**
|
|
3771
|
+
* The tick's share of the queue, in queue order: up to `removalsPerTick` removals and up to
|
|
3772
|
+
* `perTick` registrations. Removals do not wait behind registrations (a key's own queued
|
|
3773
|
+
* registration was cancelled when its removal was queued, so the two never meet), but a key's
|
|
3774
|
+
* registration does wait behind its own removal when that is left for a later tick.
|
|
3775
|
+
*/
|
|
3776
|
+
const flushTick = () => {
|
|
3777
|
+
if (perTick === Number.POSITIVE_INFINITY && removalsPerTick === Number.POSITIVE_INFINITY) {
|
|
3778
|
+
flush();
|
|
3779
|
+
return;
|
|
3780
|
+
}
|
|
3781
|
+
let added = 0;
|
|
3782
|
+
let removed = 0;
|
|
3783
|
+
let write = 0;
|
|
3784
|
+
let held;
|
|
3785
|
+
for (const change of pending) {
|
|
3786
|
+
const key = change.op === "add" ? change.tile.key : change.key;
|
|
3787
|
+
if (held?.has(key) !== true) {
|
|
3788
|
+
if (change.op === "remove" && removed < removalsPerTick) {
|
|
3789
|
+
removed++;
|
|
3790
|
+
network.unregisterTile(change.key);
|
|
3791
|
+
continue;
|
|
3792
|
+
}
|
|
3793
|
+
if (change.op === "add" && added < perTick) {
|
|
3794
|
+
added++;
|
|
3795
|
+
network.registerTile(change.tile);
|
|
3796
|
+
continue;
|
|
3797
|
+
}
|
|
3798
|
+
}
|
|
3799
|
+
held ??= /* @__PURE__ */ new Set();
|
|
3800
|
+
held.add(key);
|
|
3801
|
+
pending[write++] = change;
|
|
3802
|
+
}
|
|
3803
|
+
pending.length = write;
|
|
3804
|
+
};
|
|
3588
3805
|
const writeState = (patch) => {
|
|
3589
3806
|
const next = {
|
|
3590
3807
|
...world.get("$ambient", AmbientState) ?? {
|
|
@@ -3598,7 +3815,7 @@ function installAmbient(world, options = {}) {
|
|
|
3598
3815
|
world.set(AMBIENT_ENTITY, AmbientState, next);
|
|
3599
3816
|
};
|
|
3600
3817
|
world.addSystem((w, ctx) => {
|
|
3601
|
-
|
|
3818
|
+
flushTick();
|
|
3602
3819
|
const state = w.get(AMBIENT_ENTITY, AmbientState);
|
|
3603
3820
|
const observer = resolveObserver(w, state);
|
|
3604
3821
|
const policy = observerPolicy(effectivePolicy(), observer);
|
|
@@ -3610,7 +3827,8 @@ function installAmbient(world, options = {}) {
|
|
|
3610
3827
|
types,
|
|
3611
3828
|
templates,
|
|
3612
3829
|
seed,
|
|
3613
|
-
ground
|
|
3830
|
+
ground,
|
|
3831
|
+
...options.carsAt ? { carsAt: options.carsAt } : {}
|
|
3614
3832
|
};
|
|
3615
3833
|
const removed = despawnAgents(w, env, observer);
|
|
3616
3834
|
const counters = {
|
|
@@ -3690,6 +3908,10 @@ function installAmbient(world, options = {}) {
|
|
|
3690
3908
|
});
|
|
3691
3909
|
},
|
|
3692
3910
|
unregisterTile(key) {
|
|
3911
|
+
for (let i = pending.length - 1; i >= 0; i--) {
|
|
3912
|
+
const change = pending[i];
|
|
3913
|
+
if (change?.op === "add" && change.tile.key === key) pending.splice(i, 1);
|
|
3914
|
+
}
|
|
3693
3915
|
pending.push({
|
|
3694
3916
|
op: "remove",
|
|
3695
3917
|
key
|
|
@@ -3899,6 +4121,127 @@ function cloneMap(map) {
|
|
|
3899
4121
|
return JSON.parse(JSON.stringify(map));
|
|
3900
4122
|
}
|
|
3901
4123
|
//#endregion
|
|
4124
|
+
//#region src/kernel/fleet.ts
|
|
4125
|
+
/** Small, geometry-free market profiles. Weights are visual priors, not sales statistics. */
|
|
4126
|
+
const weights = z.record(z.string().min(1), z.number().finite().nonnegative());
|
|
4127
|
+
const fleetCatalogSchema = z.strictObject({
|
|
4128
|
+
format: z.literal("molen/fleet-catalog@1"),
|
|
4129
|
+
id: z.string().min(1),
|
|
4130
|
+
version: z.number().int().positive(),
|
|
4131
|
+
description: z.string(),
|
|
4132
|
+
fallback: z.string().min(1),
|
|
4133
|
+
regions: z.record(z.string(), z.string().min(1)),
|
|
4134
|
+
profiles: z.array(z.strictObject({
|
|
4135
|
+
id: z.string().min(1),
|
|
4136
|
+
title: z.string().min(1),
|
|
4137
|
+
extends: z.string().optional(),
|
|
4138
|
+
weights
|
|
4139
|
+
})).min(1)
|
|
4140
|
+
});
|
|
4141
|
+
function seedOf(value) {
|
|
4142
|
+
let seed = 2166136261;
|
|
4143
|
+
for (let i = 0; i < value.length; i++) seed = Math.imul(seed ^ value.charCodeAt(i), 16777619);
|
|
4144
|
+
return fmix32(seed);
|
|
4145
|
+
}
|
|
4146
|
+
function createFleetLibrary(documents, available, maxModels = 16) {
|
|
4147
|
+
if (!Number.isInteger(maxModels) || maxModels < 1 || maxModels > 128) throw new Error("Fleet maxModels must be an integer from 1 to 128");
|
|
4148
|
+
const profiles = /* @__PURE__ */ new Map();
|
|
4149
|
+
const regions = /* @__PURE__ */ new Map();
|
|
4150
|
+
let fallback;
|
|
4151
|
+
for (const raw of documents) {
|
|
4152
|
+
const doc = fleetCatalogSchema.parse(raw);
|
|
4153
|
+
fallback ??= doc.fallback;
|
|
4154
|
+
for (const profile of doc.profiles) {
|
|
4155
|
+
if (profiles.has(profile.id)) throw new Error(`Duplicate fleet profile: ${profile.id}`);
|
|
4156
|
+
profiles.set(profile.id, profile);
|
|
4157
|
+
}
|
|
4158
|
+
for (const [region, profile] of Object.entries(doc.regions)) {
|
|
4159
|
+
if (regions.has(region)) throw new Error(`Duplicate fleet region: ${region}`);
|
|
4160
|
+
regions.set(region, profile);
|
|
4161
|
+
}
|
|
4162
|
+
}
|
|
4163
|
+
const resolved = /* @__PURE__ */ new Map();
|
|
4164
|
+
const resolve = (id, visiting = /* @__PURE__ */ new Set()) => {
|
|
4165
|
+
const cached = resolved.get(id);
|
|
4166
|
+
if (cached) return cached;
|
|
4167
|
+
const profile = profiles.get(id);
|
|
4168
|
+
if (!profile) throw new Error(`Unknown fleet profile: ${id}`);
|
|
4169
|
+
if (visiting.has(id)) throw new Error(`Cyclic fleet inheritance: ${id}`);
|
|
4170
|
+
visiting.add(id);
|
|
4171
|
+
const result = {
|
|
4172
|
+
...profile.extends ? resolve(profile.extends, visiting) : {},
|
|
4173
|
+
...profile.weights
|
|
4174
|
+
};
|
|
4175
|
+
visiting.delete(id);
|
|
4176
|
+
resolved.set(id, result);
|
|
4177
|
+
return result;
|
|
4178
|
+
};
|
|
4179
|
+
for (const id of profiles.keys()) resolve(id);
|
|
4180
|
+
for (const id of regions.values()) resolve(id);
|
|
4181
|
+
if (!fallback) throw new Error("A fleet catalog needs a fallback");
|
|
4182
|
+
resolve(fallback);
|
|
4183
|
+
const mixes = /* @__PURE__ */ new Map();
|
|
4184
|
+
const select = (region) => {
|
|
4185
|
+
const profile = region && regions.get(region) || fallback;
|
|
4186
|
+
const cached = mixes.get(profile);
|
|
4187
|
+
if (cached) return cached;
|
|
4188
|
+
const pool = Object.entries(resolve(profile)).filter(([id, weight]) => weight > 0 && available.has(id)).sort((a, b) => b[1] - a[1] || (a[0] < b[0] ? -1 : 1));
|
|
4189
|
+
const chosen = pool.splice(0, Math.min(4, maxModels));
|
|
4190
|
+
const seed = seedOf(profile);
|
|
4191
|
+
while (pool.length && chosen.length < maxModels) {
|
|
4192
|
+
let draw = unit01(seed, chosen.length) * pool.reduce((sum, [, weight]) => sum + weight, 0);
|
|
4193
|
+
let at = 0;
|
|
4194
|
+
while (at < pool.length - 1 && draw >= (pool[at]?.[1] ?? 0)) draw -= pool[at++]?.[1] ?? 0;
|
|
4195
|
+
chosen.push(...pool.splice(at, 1));
|
|
4196
|
+
}
|
|
4197
|
+
const mix = {
|
|
4198
|
+
profile,
|
|
4199
|
+
weights: new Map(chosen)
|
|
4200
|
+
};
|
|
4201
|
+
mixes.set(profile, mix);
|
|
4202
|
+
return mix;
|
|
4203
|
+
};
|
|
4204
|
+
return {
|
|
4205
|
+
regions,
|
|
4206
|
+
select
|
|
4207
|
+
};
|
|
4208
|
+
}
|
|
4209
|
+
function registerFleetSchema() {
|
|
4210
|
+
if (getSchema("fleet-catalog")) return;
|
|
4211
|
+
registerSchema("fleet-catalog", fleetCatalogSchema, {
|
|
4212
|
+
id: "molen/fleet-catalog@1",
|
|
4213
|
+
title: "Regional vehicle fleet",
|
|
4214
|
+
description: "Shared vehicle IDs with inherited regional spawn weights and a global fallback.",
|
|
4215
|
+
docsRef: "guide/regional-fleets.md",
|
|
4216
|
+
examples: [{
|
|
4217
|
+
format: "molen/fleet-catalog@1",
|
|
4218
|
+
id: "example.fleet",
|
|
4219
|
+
version: 1,
|
|
4220
|
+
description: "Illustrative visual mixture",
|
|
4221
|
+
fallback: "global",
|
|
4222
|
+
regions: {},
|
|
4223
|
+
profiles: [{
|
|
4224
|
+
id: "global",
|
|
4225
|
+
title: "Global",
|
|
4226
|
+
weights: { "example.car": 1 }
|
|
4227
|
+
}]
|
|
4228
|
+
}],
|
|
4229
|
+
validate(data) {
|
|
4230
|
+
try {
|
|
4231
|
+
createFleetLibrary([data], /* @__PURE__ */ new Set());
|
|
4232
|
+
return [];
|
|
4233
|
+
} catch (error) {
|
|
4234
|
+
return [{
|
|
4235
|
+
path: "",
|
|
4236
|
+
code: "invalid_fleet",
|
|
4237
|
+
message: error.message
|
|
4238
|
+
}];
|
|
4239
|
+
}
|
|
4240
|
+
}
|
|
4241
|
+
});
|
|
4242
|
+
}
|
|
4243
|
+
registerFleetSchema();
|
|
4244
|
+
//#endregion
|
|
3902
4245
|
//#region src/kernel/graph/export.ts
|
|
3903
4246
|
/** Export a built network as a molen/transport-network@1 document (the bake op's output). */
|
|
3904
4247
|
const round = (v) => dmath.round(v * 100) / 100;
|
|
@@ -3931,6 +4274,7 @@ function networkToDocument(network, meta = {}, options = {}) {
|
|
|
3931
4274
|
lanes: roadLanes,
|
|
3932
4275
|
oneway: edge.oneway
|
|
3933
4276
|
} : {},
|
|
4277
|
+
...(edge.class === "road" || edge.class === "walk") && edge.drivingSide !== network.traffic ? { drivingSide: edge.drivingSide } : {},
|
|
3934
4278
|
...edge.class === "water" ? {
|
|
3935
4279
|
oneway: edge.oneway,
|
|
3936
4280
|
speed: edge.speed
|
|
@@ -4128,7 +4472,10 @@ const wildlifeSpeciesSchema = z.strictObject({
|
|
|
4128
4472
|
accent: color,
|
|
4129
4473
|
details: z.array(z.enum([
|
|
4130
4474
|
"antlers",
|
|
4475
|
+
"palmate",
|
|
4131
4476
|
"horns",
|
|
4477
|
+
"straight-horns",
|
|
4478
|
+
"mane",
|
|
4132
4479
|
"long-neck",
|
|
4133
4480
|
"bushy-tail",
|
|
4134
4481
|
"stripes",
|
|
@@ -4157,4 +4504,4 @@ const wildlifeSpeciesSchema = z.strictObject({
|
|
|
4157
4504
|
//#region src/kernel.ts
|
|
4158
4505
|
registerAmbientSchema();
|
|
4159
4506
|
//#endregion
|
|
4160
|
-
export { AMBIENT_AGENT_COMPONENT, AMBIENT_ENTITY, AMBIENT_KINDS, AMBIENT_OBSERVER_COMPONENT, AMBIENT_ROLE_COMPONENT, AMBIENT_STATE_COMPONENT, AmbientAgent, AmbientObserver, AmbientRole, AmbientState, CAR_COLORS, DEFAULT_POLICY, DEFAULT_TEMPLATES, DEFAULT_TYPES, GRID_CELL, KIND_SPEED, LaneGrid, TRANSPORT_NETWORK_EXAMPLE, TRANSPORT_NETWORK_FORMAT, TransportNetwork, WILDLIFE_CELL_METERS, WILDLIFE_FAMILIES, WILDLIFE_TICK_RATE, agentRotation, agentSample, allRed, ambientCapability, ambientOf, ambientScriptApi, chooseNext, classifyFeature, clipPolyline, edgeHeight, featureDrafts, fmix32, idmAcceleration, installAmbient, laneCoordinate, laneEdgeDistance, laneSample, mergePolicy, networkToDocument, pickWeighted, planarSplit, policyForBudget, pseudoAngle, registerAmbientSchema, sampleConnector, sampleLaneBody, signalColor, signalTiming, stepAgents, stepWildlife, stoppingDistance, unit01, walkBack, wildlifeCandidates, wildlifeSpeciesSchema };
|
|
4507
|
+
export { AMBIENT_AGENT_COMPONENT, AMBIENT_ENTITY, AMBIENT_KINDS, AMBIENT_OBSERVER_COMPONENT, AMBIENT_ROLE_COMPONENT, AMBIENT_STATE_COMPONENT, AmbientAgent, AmbientObserver, AmbientRole, AmbientState, CAR_COLORS, DEFAULT_POLICY, DEFAULT_TEMPLATES, DEFAULT_TYPES, GRID_CELL, KIND_SPEED, LaneGrid, TRANSPORT_NETWORK_EXAMPLE, TRANSPORT_NETWORK_FORMAT, TransportNetwork, WILDLIFE_CELL_METERS, WILDLIFE_FAMILIES, WILDLIFE_TICK_RATE, agentRotation, agentSample, allRed, ambientCapability, ambientOf, ambientScriptApi, chooseNext, classifyFeature, clipPolyline, createFleetLibrary, edgeHeight, featureDrafts, fleetCatalogSchema, fmix32, idmAcceleration, installAmbient, laneCoordinate, laneEdgeDistance, laneSample, mergePolicy, networkToDocument, pickWeighted, planarSplit, policyForBudget, pseudoAngle, registerAmbientSchema, registerFleetSchema, sampleConnector, sampleLaneBody, signalColor, signalTiming, stepAgents, stepWildlife, stoppingDistance, unit01, walkBack, wildlifeCandidates, wildlifeSpeciesSchema };
|