@bendyline/molen-ambient 0.0.14 → 0.0.17

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/kernel.mjs CHANGED
@@ -1,7 +1,7 @@
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";
1
+ import { A as unit01, C as clipPolyline, D as fnv1a, E as fmix32, O as q4, S as classifyFeature, T as tilePois, _ as TRANSPORT_NETWORK_FORMAT, a as AMBIENT_ENTITY, b as wayDraft, c as AmbientObserver, d as AMBIENT_AGENT_COMPONENT, f as AMBIENT_OBSERVER_COMPONENT, g as TRANSPORT_NETWORK_EXAMPLE, h as registerAmbientSchema, i as pickWeighted, k as shortHash, l as AmbientRole, m as AMBIENT_STATE_COMPONENT, n as DEFAULT_TEMPLATES, o as AMBIENT_KINDS, p as AMBIENT_ROLE_COMPONENT, r as DEFAULT_TYPES, s as AmbientAgent, t as CAR_COLORS, u as AmbientState, v as documentPois, w as featureDrafts, x as KIND_SPEED, y as documentRunways } from "./templates-DoMmnJNE.mjs";
2
2
  import { createRng, dmath, lookRotation, quatRotateVec3, seedToInt } from "@bendyline/molen-kernel/determinism";
3
- import { Transform, componentHandle, isBoostMode } from "@bendyline/molen-kernel/world";
4
- import { boostSpeedMultiplier, deepMergeJson, getSchema, registerSchema, resolvePrefab, validateByKind } from "@bendyline/molen-schema";
3
+ import { Transform, componentHandle, isBoostMode, weatherOf } from "@bendyline/molen-kernel/world";
4
+ import { boostSpeedMultiplier, deepMergeJson, getSchema, registerSchema, resolvePrefab, snowCoverAt, snowWalkingPace, validateByKind } from "@bendyline/molen-schema";
5
5
  import { z } from "zod";
6
6
  import { PATH_SURFACE_LIFT, ROAD_SURFACE_LIFT, bridgeDeckHeightFn, gradeHighwayBridges, isArterialRoad, lineSegmentIndex, sampleTerrainLine } from "@bendyline/molen-terrain/kernel";
7
7
  //#region src/kernel/agents/idm.ts
@@ -25,37 +25,6 @@ function stoppingDistance(v, b) {
25
25
  return v * v / (2 * b);
26
26
  }
27
27
  //#endregion
28
- //#region src/kernel/ids.ts
29
- /** Small deterministic hashes and seeded draws shared by the graph and the agents. */
30
- /** FNV-1a 32-bit hash of a string. */
31
- function fnv1a(text) {
32
- let h = 2166136261;
33
- for (let i = 0; i < text.length; i++) {
34
- h ^= text.charCodeAt(i);
35
- h = Math.imul(h, 16777619);
36
- }
37
- return h >>> 0;
38
- }
39
- /** Short base-36 hash of a string (up to 7 characters). */
40
- function shortHash(text) {
41
- return fnv1a(text).toString(36);
42
- }
43
- /** murmur3 finalizer: a full-avalanche 32-bit mix. */
44
- function fmix32(x) {
45
- let h = x | 0;
46
- h = Math.imul(h ^ h >>> 16, 2246822507);
47
- h = Math.imul(h ^ h >>> 13, 3266489909);
48
- return (h ^ h >>> 16) >>> 0;
49
- }
50
- /** Independent stateless draw in [0, 1) for a (seed, stream) pair. */
51
- function unit01(seed, stream) {
52
- return fmix32((seed | 0) ^ Math.imul((stream | 0) + 1, 2654435761)) / 4294967296;
53
- }
54
- /** Quantize to 1e-4 so stored state equals the state the next tick reads. */
55
- function q4(value) {
56
- return Math.round(value * 1e4) / 1e4;
57
- }
58
- //#endregion
59
28
  //#region src/kernel/agents/signals.ts
60
29
  const AMBER_SECONDS = 3;
61
30
  const ALL_RED_SECONDS = 2;
@@ -122,6 +91,19 @@ function edgeHeight(edge, d) {
122
91
  const t = d1 > d0 ? dmath.clamp((d - d0) / (d1 - d0), 0, 1) : 0;
123
92
  return y0 + (y1 - y0) * t;
124
93
  }
94
+ /** Height of a lane's own surface at an edge-path distance (see `TransportLane.ys`). */
95
+ function laneHeight(lane, edge, d) {
96
+ const ys = lane.ys;
97
+ if (ys === void 0) return edgeHeight(edge, d);
98
+ const path = edge.path;
99
+ const i = lineSegmentIndex(path, d);
100
+ const d0 = path.distances[i] ?? 0;
101
+ const d1 = path.distances[i + 1] ?? d0;
102
+ const y0 = ys[i] ?? 0;
103
+ const y1 = ys[i + 1] ?? y0;
104
+ const t = d1 > d0 ? dmath.clamp((d - d0) / (d1 - d0), 0, 1) : 0;
105
+ return y0 + (y1 - y0) * t;
106
+ }
125
107
  /** Sample a lane at `s` within [0, length], writing into `out`. */
126
108
  function sampleLaneBody(lane, edge, s, out) {
127
109
  const d = laneEdgeDistance(lane, edge, s);
@@ -130,8 +112,8 @@ function sampleLaneBody(lane, edge, s, out) {
130
112
  out.z = p.z;
131
113
  out.dx = p.dx * lane.dir;
132
114
  out.dz = p.dz * lane.dir;
133
- out.y = edgeHeight(edge, d) + lane.lift;
134
- out.grade = (edgeHeight(edge, dmath.clamp(d + lane.dir * 2, 0, edge.path.length)) - edgeHeight(edge, dmath.clamp(d - lane.dir * 2, 0, edge.path.length))) / 4;
115
+ out.y = laneHeight(lane, edge, d) + lane.lift;
116
+ out.grade = (laneHeight(lane, edge, dmath.clamp(d + lane.dir * 2, 0, edge.path.length)) - laneHeight(lane, edge, dmath.clamp(d - lane.dir * 2, 0, edge.path.length))) / 4;
135
117
  return out;
136
118
  }
137
119
  /** Point and tangent on a movement's connector at arc-length fraction `t` in [0, 1]. */
@@ -202,6 +184,8 @@ function laneSample() {
202
184
  * Order: gather → occupancy → armed crossings → junction grants → cars → pedestrians → trains
203
185
  * (leads, then their cars) → vessels → aircraft → write changed agents and transforms.
204
186
  */
187
+ /** Share of lying snow left on a footway: trodden down, but not ploughed like a road. */
188
+ const FOOTWAY_SNOW = .5;
205
189
  const TURN_SPEED = {
206
190
  left: 7,
207
191
  right: 5.5,
@@ -684,7 +668,8 @@ function stepWalker(ref, occupancy, env) {
684
668
  const { a, w } = ref;
685
669
  const lane = ref.lane;
686
670
  const edge = ref.edge;
687
- const pace = (1.15 + .5 * unit01(a.seed, 2)) * (env.speedScale ?? 1);
671
+ const snow = env.snowCover === void 0 ? 1 : snowWalkingPace(snowCoverAt(env.snowCover, edge.ys[0] ?? 0).depth * FOOTWAY_SNOW);
672
+ const pace = (1.15 + .5 * unit01(a.seed, 2)) * (env.speedScale ?? 1) * snow;
688
673
  if (a.state === "idle") {
689
674
  const idleTicks = (3 + 15 * unit01(a.seed, 3e3 + a.hops)) * tickRate;
690
675
  if (tick - a.since < idleTicks) {
@@ -862,7 +847,7 @@ function walkBack(net, laneId, s, prev, trail, distance) {
862
847
  function placeTrainCar(ref, lead, env) {
863
848
  const { w } = ref;
864
849
  const spacing = (ref.a.length + lead.w.length) / 2 + 1;
865
- const back = walkBack(env.net, lead.w.lane, lead.w.s, lead.w.prev, lead.w.trail ?? [], spacing * (ref.a.carIndex ?? 1));
850
+ const back = walkBack(env.net, lead.w.lane, lead.w.s, lead.w.prev, lead.w.trail ?? [], ref.a.offset ?? spacing * (ref.a.carIndex ?? 1));
866
851
  w.lane = back.lane;
867
852
  w.s = back.s;
868
853
  if (back.prev !== void 0) w.prev = back.prev;
@@ -2272,25 +2257,24 @@ var TransportNetwork = class {
2272
2257
  if (path.length < .05) continue;
2273
2258
  const lift = draft.class === "walk" ? draft.kind === "crossing" ? ROAD_SURFACE_LIFT + .01 : PATH_SURFACE_LIFT : ROAD_SURFACE_LIFT;
2274
2259
  const ys = new Float64Array(path.points.length);
2260
+ let surface;
2275
2261
  if (dense.heights !== void 0) dense.heights.forEach((h, i) => {
2276
2262
  ys[i] = h;
2277
2263
  });
2278
2264
  else if (draft.class === "water") ys.fill(0);
2279
- else if (draft.bridge || draft.bridgeConnections?.length || draft.bridgeApproachProfile?.length) {
2280
- const deck = bridgeDeckHeightFn(path, heightAt, {
2281
- ...draft.deckElevation !== void 0 ? { deckElevation: draft.deckElevation } : {},
2282
- clearance: draft.class === "walk" ? 3 : 6,
2283
- approachLift: lift,
2284
- groundApproach: !draft.bridge,
2285
- approachProfile: draft.bridgeApproachProfile,
2286
- connects: [gradeRoad.has(piece.from), gradeRoad.has(piece.to)],
2287
- ...draft.bridgeConnections !== void 0 ? { connections: draft.bridgeConnections } : {}
2288
- });
2289
- path.points.forEach((p, i) => {
2290
- ys[i] = deck(p[0], p[1]);
2291
- });
2292
- } else path.points.forEach((p, i) => {
2293
- ys[i] = heightAt(p[0], p[1]) + lift;
2265
+ else if (draft.bridge || draft.bridgeConnections?.length || draft.bridgeApproachProfile?.length) surface = bridgeDeckHeightFn(path, heightAt, {
2266
+ ...draft.deckElevation !== void 0 ? { deckElevation: draft.deckElevation } : {},
2267
+ clearance: draft.class === "walk" ? 3 : 6,
2268
+ approachLift: lift,
2269
+ groundApproach: !draft.bridge,
2270
+ approachProfile: draft.bridgeApproachProfile,
2271
+ ...rect !== void 0 ? { bounds: rect } : {},
2272
+ connects: [gradeRoad.has(piece.from), gradeRoad.has(piece.to)],
2273
+ ...draft.bridgeConnections !== void 0 ? { connections: draft.bridgeConnections } : {}
2274
+ });
2275
+ else surface = (x, z) => heightAt(x, z) + lift;
2276
+ if (surface !== void 0) path.points.forEach((p, i) => {
2277
+ ys[i] = surface(p[0], p[1]);
2294
2278
  });
2295
2279
  const first = path.points[0];
2296
2280
  const last = path.points[path.points.length - 1];
@@ -2318,7 +2302,7 @@ var TransportNetwork = class {
2318
2302
  ...draft.name !== void 0 ? { name: draft.name } : {}
2319
2303
  };
2320
2304
  const inferSidewalks = walkSegments !== void 0 && draft.class === "road" && SIDEWALK_KINDS.has(draft.kind) && !draft.tunnel && !nearWalk(walkSegments, path, draft.width / 2 + 4);
2321
- this.createLanes(edge, draft, inferSidewalks);
2305
+ this.createLanes(edge, draft, inferSidewalks, surface);
2322
2306
  this.edges.set(id, edge);
2323
2307
  record.edges.push(id);
2324
2308
  this.nodes.get(from).edges.push(id);
@@ -2349,10 +2333,18 @@ var TransportNetwork = class {
2349
2333
  this.addCrossings(record);
2350
2334
  this.version++;
2351
2335
  }
2352
- createLanes(edge, draft, sidewalks) {
2336
+ createLanes(edge, draft, sidewalks, surface) {
2353
2337
  const side = edge.drivingSide === "right" ? 1 : -1;
2354
2338
  const make = (code, cls, dir, index, offset, lift = 0) => {
2355
2339
  const id = `${edge.id}>${code}`;
2340
+ let ys;
2341
+ if (surface !== void 0 && offset !== 0) {
2342
+ ys = new Float64Array(edge.path.points.length);
2343
+ for (let i = 0; i < ys.length; i++) {
2344
+ const p = sampleTerrainLine(edge.path, edge.path.distances[i] ?? 0, dir * offset);
2345
+ ys[i] = surface(p.x, p.z);
2346
+ }
2347
+ }
2356
2348
  const lane = {
2357
2349
  id,
2358
2350
  edge: edge.id,
@@ -2364,6 +2356,7 @@ var TransportNetwork = class {
2364
2356
  trimEnd: 0,
2365
2357
  length: edge.path.length,
2366
2358
  lift,
2359
+ ...ys !== void 0 ? { ys } : {},
2367
2360
  start: dir === 1 ? edge.from : edge.to,
2368
2361
  end: dir === 1 ? edge.to : edge.from,
2369
2362
  next: [],
@@ -2917,309 +2910,6 @@ function policyForBudget(policy, budget) {
2917
2910
  };
2918
2911
  }
2919
2912
  //#endregion
2920
- //#region src/kernel/templates.ts
2921
- /** Common real-world car body colours, weighted by repetition. */
2922
- const CAR_COLORS = [
2923
- "#f2f2f0",
2924
- "#f2f2f0",
2925
- "#f2f2f0",
2926
- "#1c1d21",
2927
- "#1c1d21",
2928
- "#1c1d21",
2929
- "#8d9196",
2930
- "#8d9196",
2931
- "#b9bcbf",
2932
- "#b9bcbf",
2933
- "#2f4f86",
2934
- "#8c1f1f",
2935
- "#3f5a3c",
2936
- "#6b4a2e",
2937
- "#c9b48a"
2938
- ];
2939
- const DEFAULT_TYPES = {
2940
- car: [
2941
- {
2942
- id: "proxy.sedan",
2943
- length: 4.6,
2944
- width: 1.8,
2945
- height: 1.45,
2946
- weight: 3
2947
- },
2948
- {
2949
- id: "proxy.suv",
2950
- length: 4.8,
2951
- width: 1.9,
2952
- height: 1.75,
2953
- weight: 2
2954
- },
2955
- {
2956
- id: "proxy.compact",
2957
- length: 4,
2958
- width: 1.75,
2959
- height: 1.45,
2960
- weight: 2
2961
- },
2962
- {
2963
- id: "proxy.van",
2964
- length: 5.2,
2965
- width: 2,
2966
- height: 2.1,
2967
- weight: 1
2968
- }
2969
- ],
2970
- train: [{
2971
- id: "proxy.rail",
2972
- length: 27,
2973
- width: 2.65,
2974
- height: 3.6,
2975
- cars: 2,
2976
- colors: ["#e8e6de"]
2977
- }],
2978
- aircraft: [{
2979
- id: "proxy.airliner",
2980
- length: 38,
2981
- width: 36,
2982
- height: 12,
2983
- cruise: 110
2984
- }],
2985
- boat: [{
2986
- id: "proxy.boat",
2987
- length: 12,
2988
- width: 4,
2989
- height: 4,
2990
- cruise: 5,
2991
- waterway: "channel"
2992
- }, {
2993
- id: "proxy.ferry",
2994
- length: 32,
2995
- width: 9,
2996
- height: 8,
2997
- cruise: 8,
2998
- waterway: "ferry"
2999
- }],
3000
- pedestrian: [
3001
- {
3002
- preset: "human.adult",
3003
- weight: 7
3004
- },
3005
- {
3006
- preset: "human.elder",
3007
- weight: 1.5
3008
- },
3009
- {
3010
- preset: "human.child",
3011
- weight: 1.5
3012
- }
3013
- ]
3014
- };
3015
- /** Weighted pick from a list using a stateless draw. */
3016
- function pickWeighted(list, u) {
3017
- let total = 0;
3018
- for (const item of list) total += item.weight ?? 1;
3019
- if (total <= 0) return list[0];
3020
- let target = u * total;
3021
- for (const item of list) {
3022
- target -= item.weight ?? 1;
3023
- if (target < 0) return item;
3024
- }
3025
- return list[list.length - 1];
3026
- }
3027
- const ADULTS = [
3028
- {
3029
- height: 1.62,
3030
- build: -.2,
3031
- skin: "#f1d3bd",
3032
- hair: "#3b2a20",
3033
- top: "#2f3b52",
3034
- bottom: "#1f1f24"
3035
- },
3036
- {
3037
- height: 1.78,
3038
- build: .2,
3039
- skin: "#c68b62",
3040
- hair: "#1f1a17",
3041
- top: "#e8e6de",
3042
- bottom: "#2f3b52"
3043
- },
3044
- {
3045
- height: 1.7,
3046
- build: 0,
3047
- skin: "#6c4430",
3048
- hair: "#1f1a17",
3049
- top: "#6b2f2f",
3050
- bottom: "#8a8f96"
3051
- },
3052
- {
3053
- height: 1.84,
3054
- build: .4,
3055
- skin: "#e0b597",
3056
- hair: "#a8793f",
3057
- top: "#2f5a3b",
3058
- bottom: "#c9b48a"
3059
- },
3060
- {
3061
- height: 1.58,
3062
- build: -.4,
3063
- skin: "#9b6645",
3064
- hair: "#1f1a17",
3065
- top: "#3b6ea5",
3066
- bottom: "#1f1f24"
3067
- },
3068
- {
3069
- height: 1.74,
3070
- build: .1,
3071
- skin: "#f1d3bd",
3072
- hair: "#d8c29a",
3073
- top: "#1f1f24",
3074
- bottom: "#2f3b52"
3075
- },
3076
- {
3077
- height: 1.66,
3078
- build: .3,
3079
- skin: "#4a2e21",
3080
- hair: "#1f1a17",
3081
- top: "#b0553a",
3082
- bottom: "#1f1f24"
3083
- },
3084
- {
3085
- height: 1.8,
3086
- build: -.1,
3087
- skin: "#e0b597",
3088
- hair: "#6b4a2e",
3089
- top: "#8a8f96",
3090
- bottom: "#1f1f24"
3091
- },
3092
- {
3093
- height: 1.68,
3094
- build: 0,
3095
- skin: "#c68b62",
3096
- hair: "#3b2a20",
3097
- top: "#5a3b6b",
3098
- bottom: "#8a8f96"
3099
- },
3100
- {
3101
- height: 1.76,
3102
- build: .5,
3103
- skin: "#f1d3bd",
3104
- hair: "#9a9a9a",
3105
- top: "#2f3b52",
3106
- bottom: "#c9b48a"
3107
- },
3108
- {
3109
- height: 1.6,
3110
- build: .2,
3111
- skin: "#9b6645",
3112
- hair: "#3b2a20",
3113
- top: "#e8e6de",
3114
- bottom: "#3b6ea5"
3115
- },
3116
- {
3117
- height: 1.72,
3118
- build: -.3,
3119
- skin: "#6c4430",
3120
- hair: "#1f1a17",
3121
- top: "#2f5a3b",
3122
- bottom: "#1f1f24"
3123
- }
3124
- ];
3125
- const ELDERS = [
3126
- {
3127
- height: 1.64,
3128
- build: .2,
3129
- skin: "#f1d3bd",
3130
- hair: "#c8c8c4",
3131
- top: "#8a8f96",
3132
- bottom: "#2f3b52"
3133
- },
3134
- {
3135
- height: 1.72,
3136
- build: .3,
3137
- skin: "#c68b62",
3138
- hair: "#e6e6e2",
3139
- top: "#6b4a2e",
3140
- bottom: "#1f1f24"
3141
- },
3142
- {
3143
- height: 1.58,
3144
- build: 0,
3145
- skin: "#6c4430",
3146
- hair: "#b8b8b4",
3147
- top: "#5a3b6b",
3148
- bottom: "#8a8f96"
3149
- }
3150
- ];
3151
- const CHILDREN = [
3152
- {
3153
- height: 1.2,
3154
- build: 0,
3155
- skin: "#f1d3bd",
3156
- hair: "#a8793f",
3157
- top: "#b0553a",
3158
- bottom: "#2f3b52"
3159
- },
3160
- {
3161
- height: 1.35,
3162
- build: -.2,
3163
- skin: "#9b6645",
3164
- hair: "#1f1a17",
3165
- top: "#3b6ea5",
3166
- bottom: "#1f1f24"
3167
- },
3168
- {
3169
- height: 1.28,
3170
- build: .1,
3171
- skin: "#e0b597",
3172
- hair: "#3b2a20",
3173
- top: "#2f5a3b",
3174
- bottom: "#c9b48a"
3175
- }
3176
- ];
3177
- function outfitFor(preset, seed) {
3178
- const list = preset === "human.child" ? CHILDREN : preset === "human.elder" ? ELDERS : ADULTS;
3179
- return list[Math.floor(unit01(seed, 40) * list.length)];
3180
- }
3181
- /** Built-in templates: vehicles render through `ambient-vehicle`, pedestrians as figures. */
3182
- const DEFAULT_TEMPLATES = {
3183
- boat: (agent) => ({ renderable: {
3184
- kind: "ambient-vehicle",
3185
- ref: agent.type
3186
- } }),
3187
- car: (agent) => ({ renderable: {
3188
- kind: "ambient-vehicle",
3189
- ref: agent.type
3190
- } }),
3191
- train: (agent) => ({ renderable: {
3192
- kind: "ambient-vehicle",
3193
- ref: agent.type
3194
- } }),
3195
- aircraft: (agent) => ({ renderable: {
3196
- kind: "ambient-vehicle",
3197
- ref: agent.type
3198
- } }),
3199
- pedestrian: (agent) => {
3200
- const outfit = outfitFor(agent.type, agent.seed);
3201
- return {
3202
- figure: {
3203
- preset: agent.type,
3204
- height: outfit.height,
3205
- build: outfit.build,
3206
- palette: {
3207
- skin: outfit.skin,
3208
- hair: outfit.hair,
3209
- top: outfit.top,
3210
- bottom: outfit.bottom
3211
- },
3212
- facing: "manual",
3213
- speedSource: "transform"
3214
- },
3215
- renderable: {
3216
- kind: "figure",
3217
- ref: "procedural"
3218
- }
3219
- };
3220
- }
3221
- };
3222
- //#endregion
3223
2913
  //#region src/kernel/spawn/spawner.ts
3224
2914
  /**
3225
2915
  * Observer-driven spawning and cleanup. Agents appear on lanes in a ring around the observer
@@ -3470,13 +3160,13 @@ function spawnOne(ctx, kind) {
3470
3160
  const seed = fmix32(seq ^ env.seed);
3471
3161
  const s = laneCoordinate(lane, edge, span.d0 + rng.next() * (span.d1 - span.d0));
3472
3162
  const sample = sampleLaneBody(lane, edge, s, laneSample());
3473
- 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;
3163
+ 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) : kind === "train" ? trainTypes(types, edge) : types;
3474
3164
  if ((kind === "boat" || kind === "car") && available.length === 0) continue;
3475
3165
  const vehicle = kind === "pedestrian" ? void 0 : pickWeighted(available, unit01(seed, 10));
3476
3166
  const length = kind === "pedestrian" ? .5 : vehicle?.length ?? 4.5;
3477
3167
  const width = kind === "pedestrian" ? .5 : vehicle?.width ?? 1.8;
3478
- const cars = kind === "train" ? dmath.max(1, vehicle?.cars ?? 2) : 1;
3479
- const behind = kind === "train" ? (cars - 1) * (length + 1) : 0;
3168
+ const makeUp = kind === "train" ? trainMakeUp(vehicle, length, width, seed) : void 0;
3169
+ const behind = makeUp?.at(-1)?.offset ?? 0;
3480
3170
  if (s < behind + length / 2 + 1 || s > lane.length - length / 2 - 1) continue;
3481
3171
  const dx = sample.x - observer.x;
3482
3172
  const dz = sample.z - observer.z;
@@ -3491,11 +3181,81 @@ function spawnOne(ctx, kind) {
3491
3181
  let clear = true;
3492
3182
  for (const id of clearLanes) for (const other of ctx.occupancy.get(id) ?? []) if (dmath.abs(other.s - s) < (kind === "boat" ? (length + other.length) / 2 + env.policy.marine.gap + 30 : minGap + behind)) clear = false;
3493
3183
  if (!clear) continue;
3494
- createLaneAgent(ctx, kind, lane, edge, s, seed, vehicle, length, width, cars);
3184
+ createLaneAgent(ctx, kind, lane, edge, s, seed, vehicle, length, width, makeUp);
3495
3185
  return true;
3496
3186
  }
3497
3187
  return false;
3498
3188
  }
3189
+ /** Gap between the bodies of coupled cars (couplers and diaphragms), metres. */
3190
+ const COUPLER_GAP = 1;
3191
+ /**
3192
+ * Rail types for an edge: light types on tram and light-rail lines, heavy ones elsewhere, any
3193
+ * type without a preference on either. All of them when none match, so a pack with only one kind
3194
+ * of train still runs it everywhere.
3195
+ */
3196
+ function trainTypes(types, edge) {
3197
+ const light = edge.kind === "tram";
3198
+ const matching = types.filter((t) => t.track === void 0 || t.track === "light" === light);
3199
+ return matching.length > 0 ? matching : types;
3200
+ }
3201
+ /** Draw an inclusive integer range with a seeded unit value. */
3202
+ function between(range, u, fallback) {
3203
+ if (range === void 0) return fallback;
3204
+ const lo = dmath.min(range[0], range[1]);
3205
+ const hi = dmath.max(range[0], range[1]);
3206
+ return lo + dmath.min(hi - lo, dmath.floor(u * (hi - lo + 1)));
3207
+ }
3208
+ /**
3209
+ * The cars of a train led by `vehicle`, front to back with their offsets behind the lead's centre:
3210
+ * its consist's units and drawn cars, or `cars` copies of it.
3211
+ */
3212
+ function trainMakeUp(vehicle, length, width, seed) {
3213
+ const lead = {
3214
+ type: vehicle?.id ?? "proxy",
3215
+ length,
3216
+ width
3217
+ };
3218
+ const consist = vehicle?.consist;
3219
+ const cars = [];
3220
+ if (consist === void 0 || consist.cars.length === 0) {
3221
+ const count = dmath.max(1, vehicle?.cars ?? 2);
3222
+ for (let k = 0; k < count; k++) cars.push(lead);
3223
+ } else {
3224
+ const units = dmath.max(1, between(consist.units, unit01(seed, 20), 1));
3225
+ for (let k = 0; k < units; k++) cars.push(lead);
3226
+ const wanted = between(consist.length, unit01(seed, 21), 0);
3227
+ const total = consist.cars.reduce((sum, car) => sum + (car.weight ?? 1), 0);
3228
+ let draw = 22;
3229
+ while (cars.length < units + wanted && total > 0) {
3230
+ let pick = unit01(seed, draw++) * total;
3231
+ let chosen = consist.cars[consist.cars.length - 1];
3232
+ for (const car of consist.cars) {
3233
+ pick -= car.weight ?? 1;
3234
+ if (pick < 0) {
3235
+ chosen = car;
3236
+ break;
3237
+ }
3238
+ }
3239
+ const palette = chosen.colors;
3240
+ const color = palette !== void 0 && palette.length > 0 ? palette[dmath.floor(unit01(seed, draw++) * palette.length)] : void 0;
3241
+ const block = between(consist.block, unit01(seed, draw++), 1);
3242
+ for (let k = 0; k < block && cars.length < units + wanted; k++) cars.push({
3243
+ type: chosen.id,
3244
+ length: chosen.length,
3245
+ width: chosen.width,
3246
+ ...color !== void 0 ? { color } : {}
3247
+ });
3248
+ }
3249
+ }
3250
+ let offset = 0;
3251
+ return cars.map((car, k) => {
3252
+ if (k > 0) offset += ((cars[k - 1]?.length ?? 0) + car.length) / 2 + COUPLER_GAP;
3253
+ return {
3254
+ ...car,
3255
+ offset
3256
+ };
3257
+ });
3258
+ }
3499
3259
  function nextSeqId(ctx, kind) {
3500
3260
  for (;;) {
3501
3261
  const seq = ctx.state.nextSeq++;
@@ -3506,7 +3266,7 @@ function nextSeqId(ctx, kind) {
3506
3266
  };
3507
3267
  }
3508
3268
  }
3509
- function createLaneAgent(ctx, kind, lane, edge, s, seed, vehicle, length, width, cars) {
3269
+ function createLaneAgent(ctx, kind, lane, edge, s, seed, vehicle, length, width, makeUp) {
3510
3270
  const { env } = ctx;
3511
3271
  const { id } = nextSeqId(ctx, kind);
3512
3272
  const type = kind === "pedestrian" ? pickWeighted(env.types.pedestrian, unit01(seed, 10))?.preset ?? "human.adult" : vehicle?.id ?? "proxy";
@@ -3534,15 +3294,23 @@ function createLaneAgent(ctx, kind, lane, edge, s, seed, vehicle, length, width,
3534
3294
  spawnedTick: env.tick
3535
3295
  };
3536
3296
  spawnWithTemplate(ctx, id, agent);
3537
- if (kind === "train") for (let k = 1; k < cars; k++) {
3538
- const back = walkBack(env.net, lane.id, s, void 0, [], k * (length + 1));
3297
+ if (makeUp !== void 0) for (let k = 1; k < makeUp.length; k++) {
3298
+ const car = makeUp[k];
3299
+ const back = walkBack(env.net, lane.id, s, void 0, [], car.offset);
3539
3300
  const { id: carId } = nextSeqId(ctx, kind);
3301
+ const { color: _lead, ...rest } = agent;
3302
+ const carColor = car.color ?? (car.type === agent.type ? color : void 0);
3540
3303
  spawnWithTemplate(ctx, carId, {
3541
- ...agent,
3304
+ ...rest,
3305
+ ...carColor !== void 0 ? { color: carColor } : {},
3306
+ type: car.type,
3307
+ length: car.length,
3308
+ width: car.width,
3542
3309
  lane: back.lane,
3543
3310
  s: q4(back.s),
3544
3311
  consist: id,
3545
3312
  carIndex: k,
3313
+ offset: q4(car.offset),
3546
3314
  seed: fmix32(seed + k)
3547
3315
  });
3548
3316
  }
@@ -3883,13 +3651,15 @@ function installAmbient(world, options = {}) {
3883
3651
  radius: 2.6
3884
3652
  });
3885
3653
  }
3654
+ const snowCover = weatherOf(w)?.snowCover;
3886
3655
  stepAgents(w, {
3887
3656
  net: network,
3888
3657
  policy: effectivePolicy(),
3889
3658
  tick: ctx.tick,
3890
3659
  tickRate: w.tickRate,
3891
3660
  dt: ctx.dt,
3892
- solids: driven
3661
+ solids: driven,
3662
+ ...snowCover !== void 0 ? { snowCover } : {}
3893
3663
  });
3894
3664
  }, {
3895
3665
  phase: "physics",
@@ -4520,4 +4290,4 @@ const wildlifeSpeciesSchema = z.strictObject({
4520
4290
  //#region src/kernel.ts
4521
4291
  registerAmbientSchema();
4522
4292
  //#endregion
4523
- 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 };
4293
+ 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, laneHeight, laneSample, mergePolicy, networkToDocument, pickWeighted, planarSplit, policyForBudget, pseudoAngle, registerAmbientSchema, registerFleetSchema, sampleConnector, sampleLaneBody, signalColor, signalTiming, stepAgents, stepWildlife, stoppingDistance, unit01, walkBack, wildlifeCandidates, wildlifeSpeciesSchema };