@bendyline/molen-ambient 0.0.11 → 0.0.12
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 +7 -1
- package/dist/client.mjs +165 -15
- package/dist/{components-D4HrizHN.mjs → components-vzvNQczG.mjs} +14 -3
- package/dist/kernel.d.mts +48 -6
- package/dist/kernel.mjs +163 -28
- package/package.json +10 -10
package/dist/client.d.mts
CHANGED
|
@@ -61,7 +61,7 @@ declare class ProxyBatches {
|
|
|
61
61
|
/** Where full vehicle models come from (absent: every car stays a proxy). */
|
|
62
62
|
interface AmbientVehicleModels {
|
|
63
63
|
/** A fresh model instance for a type, or undefined when the type has no model. */
|
|
64
|
-
load(type: string): Promise<THREE.Object3D | undefined>;
|
|
64
|
+
load(type: string, signal?: AbortSignal): Promise<THREE.Object3D | undefined>;
|
|
65
65
|
/** The vehicle data (wheel nodes, paint material, spec) of a type, if it is a vehicle type. */
|
|
66
66
|
vehicle(type: string): VehicleData | undefined;
|
|
67
67
|
}
|
|
@@ -146,6 +146,12 @@ interface AmbientRenderBudget {
|
|
|
146
146
|
detailedCars: number;
|
|
147
147
|
/** Full vessel models at most (default 4). */
|
|
148
148
|
detailedBoats?: number;
|
|
149
|
+
/** Idle detailed instances per pool (default 4); active instances have separate caps. */
|
|
150
|
+
idleVehicleModels?: number;
|
|
151
|
+
/** Estimated geometry/texture bytes per detail pool, including active and idle (default 32 MiB). */
|
|
152
|
+
vehicleModelBytes?: number;
|
|
153
|
+
/** Concurrent detailed model loads per pool (default 2). */
|
|
154
|
+
vehicleLoads?: number;
|
|
149
155
|
/** Animated (skinned) pedestrians at most. */
|
|
150
156
|
skinnedFigures: number;
|
|
151
157
|
/** Pedestrians beyond this distance (m) are not drawn. */
|
package/dist/client.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { d as registerAmbientSchema, r as AmbientAgent } from "./components-
|
|
1
|
+
import { d as registerAmbientSchema, r as AmbientAgent } from "./components-vzvNQczG.mjs";
|
|
2
2
|
import { Transform, componentHandle } from "@bendyline/molen-kernel/world";
|
|
3
3
|
import { createVehicleVisual, vehicleProxyGeometry, vehicleProxyMaterial } from "@bendyline/molen-client/vehicles";
|
|
4
4
|
import * as THREE from "three";
|
|
@@ -730,12 +730,74 @@ var AmbientAircraft = class {
|
|
|
730
730
|
* content model (wheels turning and steering), from a pool per type so a spawn never builds new
|
|
731
731
|
* GPU objects once the pool is warm. Farther cars stay instanced proxies.
|
|
732
732
|
*/
|
|
733
|
+
/**
|
|
734
|
+
* Authored glazing is often nearly clear (about 16% opaque), so from the street a passing car
|
|
735
|
+
* reads as an open frame of pillars and seats. Traffic glass is tinted and glossy instead:
|
|
736
|
+
* darker, at least 60% opaque and smooth enough to pick up the sky. Only traffic: a car the
|
|
737
|
+
* player sits in keeps its authored glass. The visual's materials are its own clones.
|
|
738
|
+
*/
|
|
739
|
+
function tintGlass(object) {
|
|
740
|
+
const mesh = object;
|
|
741
|
+
if (!mesh.isMesh) return;
|
|
742
|
+
for (const material of Array.isArray(mesh.material) ? mesh.material : [mesh.material]) {
|
|
743
|
+
if (!material.transparent || !/glass|glazing/i.test(material.name)) continue;
|
|
744
|
+
if (!(material instanceof THREE.MeshStandardMaterial)) continue;
|
|
745
|
+
material.color.multiplyScalar(.3);
|
|
746
|
+
material.opacity = Math.max(material.opacity, .62);
|
|
747
|
+
material.roughness = Math.min(material.roughness, .08);
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
/** Buffer residency estimate; textures add their decoded base level plus mip overhead. */
|
|
751
|
+
function modelBytes(object) {
|
|
752
|
+
const buffers = /* @__PURE__ */ new Set();
|
|
753
|
+
const textures = /* @__PURE__ */ new Set();
|
|
754
|
+
object.traverse((node) => {
|
|
755
|
+
const mesh = node;
|
|
756
|
+
if (!mesh.isMesh) return;
|
|
757
|
+
for (const attr of Object.values(mesh.geometry.attributes)) buffers.add(attr.array.buffer);
|
|
758
|
+
if (mesh.geometry.index) buffers.add(mesh.geometry.index.array.buffer);
|
|
759
|
+
for (const material of Array.isArray(mesh.material) ? mesh.material : [mesh.material]) for (const value of Object.values(material)) if (value instanceof THREE.Texture) textures.add(value);
|
|
760
|
+
});
|
|
761
|
+
let bytes = [...buffers].reduce((sum, buffer) => sum + buffer.byteLength, 0);
|
|
762
|
+
for (const texture of textures) {
|
|
763
|
+
const image = texture.image;
|
|
764
|
+
bytes += (image?.width ?? 1) * (image?.height ?? 1) * 4 * 4 / 3;
|
|
765
|
+
}
|
|
766
|
+
return bytes;
|
|
767
|
+
}
|
|
768
|
+
function disposeUnbound(model) {
|
|
769
|
+
const geometries = /* @__PURE__ */ new Set();
|
|
770
|
+
const materials = /* @__PURE__ */ new Set();
|
|
771
|
+
const textures = /* @__PURE__ */ new Set();
|
|
772
|
+
model.traverse((node) => {
|
|
773
|
+
const mesh = node;
|
|
774
|
+
if (!mesh.isMesh) return;
|
|
775
|
+
geometries.add(mesh.geometry);
|
|
776
|
+
for (const material of Array.isArray(mesh.material) ? mesh.material : [mesh.material]) {
|
|
777
|
+
materials.add(material);
|
|
778
|
+
for (const value of Object.values(material)) if (value instanceof THREE.Texture) textures.add(value);
|
|
779
|
+
}
|
|
780
|
+
});
|
|
781
|
+
for (const item of [
|
|
782
|
+
...geometries,
|
|
783
|
+
...materials,
|
|
784
|
+
...textures
|
|
785
|
+
]) item.dispose();
|
|
786
|
+
model.removeFromParent();
|
|
787
|
+
}
|
|
733
788
|
var VehicleDetail = class {
|
|
734
789
|
options;
|
|
735
790
|
group = new THREE.Group();
|
|
736
791
|
active = /* @__PURE__ */ new Map();
|
|
737
792
|
idle = /* @__PURE__ */ new Map();
|
|
738
|
-
loading = /* @__PURE__ */ new
|
|
793
|
+
loading = /* @__PURE__ */ new Map();
|
|
794
|
+
resident = /* @__PURE__ */ new Map();
|
|
795
|
+
wanted = /* @__PURE__ */ new Set();
|
|
796
|
+
candidates = [];
|
|
797
|
+
clock = 0;
|
|
798
|
+
idleLimit = 4;
|
|
799
|
+
byteLimit = 32 * 1024 * 1024;
|
|
800
|
+
concurrentLimit = 2;
|
|
739
801
|
missing = /* @__PURE__ */ new Set();
|
|
740
802
|
max = 8;
|
|
741
803
|
disposed = false;
|
|
@@ -747,6 +809,53 @@ var VehicleDetail = class {
|
|
|
747
809
|
setMax(max) {
|
|
748
810
|
this.max = Math.max(0, max);
|
|
749
811
|
}
|
|
812
|
+
setCacheBudget(options) {
|
|
813
|
+
for (const value of Object.values(options)) if (value !== void 0 && !Number.isFinite(value)) throw new RangeError("Vehicle cache budgets must be finite");
|
|
814
|
+
if (options.modelBytes !== void 0 && options.modelBytes > this.byteLimit) this.missing.clear();
|
|
815
|
+
if (options.idleModels !== void 0) this.idleLimit = Math.max(0, Math.floor(options.idleModels));
|
|
816
|
+
if (options.modelBytes !== void 0) this.byteLimit = Math.max(0, options.modelBytes);
|
|
817
|
+
if (options.concurrentLoads !== void 0) this.concurrentLimit = Math.max(1, Math.floor(options.concurrentLoads));
|
|
818
|
+
this.prune();
|
|
819
|
+
for (const [id, entry] of [...this.active].reverse()) {
|
|
820
|
+
if (this.cacheStats().bytes <= this.byteLimit) break;
|
|
821
|
+
this.active.delete(id);
|
|
822
|
+
this.release(entry.visual);
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
cacheStats() {
|
|
826
|
+
return {
|
|
827
|
+
active: this.active.size,
|
|
828
|
+
idle: this.resident.size - this.active.size,
|
|
829
|
+
bytes: [...this.resident.values()].reduce((sum, entry) => sum + entry.bytes, 0),
|
|
830
|
+
pending: this.loading.size
|
|
831
|
+
};
|
|
832
|
+
}
|
|
833
|
+
release(visual) {
|
|
834
|
+
if (!this.resident.delete(visual)) return;
|
|
835
|
+
const textures = /* @__PURE__ */ new Set();
|
|
836
|
+
visual.object.traverse((node) => {
|
|
837
|
+
const mesh = node;
|
|
838
|
+
if (mesh.isMesh) {
|
|
839
|
+
for (const material of Array.isArray(mesh.material) ? mesh.material : [mesh.material]) for (const value of Object.values(material)) if (value instanceof THREE.Texture) textures.add(value);
|
|
840
|
+
}
|
|
841
|
+
});
|
|
842
|
+
for (const texture of textures) texture.dispose();
|
|
843
|
+
visual.dispose();
|
|
844
|
+
}
|
|
845
|
+
prune(reserve = 0) {
|
|
846
|
+
const idle = [...this.idle.entries()].flatMap(([type, pool]) => pool.map((visual) => ({
|
|
847
|
+
type,
|
|
848
|
+
visual
|
|
849
|
+
}))).sort((a, b) => (this.resident.get(a.visual)?.used ?? 0) - (this.resident.get(b.visual)?.used ?? 0));
|
|
850
|
+
while (idle.length && (idle.length > this.idleLimit || this.cacheStats().bytes + reserve > this.byteLimit)) {
|
|
851
|
+
const oldest = idle.shift();
|
|
852
|
+
if (!oldest) break;
|
|
853
|
+
const pool = this.idle.get(oldest.type);
|
|
854
|
+
pool?.splice(pool.indexOf(oldest.visual), 1);
|
|
855
|
+
if (!pool?.length) this.idle.delete(oldest.type);
|
|
856
|
+
this.release(oldest.visual);
|
|
857
|
+
}
|
|
858
|
+
}
|
|
750
859
|
has(id) {
|
|
751
860
|
return this.active.has(id);
|
|
752
861
|
}
|
|
@@ -759,12 +868,17 @@ var VehicleDetail = class {
|
|
|
759
868
|
const far = this.options.far ?? 65;
|
|
760
869
|
const wanted = candidates.filter((c) => c.distance < (this.active.has(c.id) ? far : near) && !this.missing.has(c.type)).sort((a, b) => a.distance - b.distance || (a.id < b.id ? -1 : 1)).slice(0, this.max);
|
|
761
870
|
const keep = new Set(wanted.map((c) => c.id));
|
|
871
|
+
this.candidates = wanted;
|
|
872
|
+
this.wanted = new Set(wanted.map((c) => c.type));
|
|
873
|
+
for (const [type, controller] of this.loading) if (!this.wanted.has(type)) controller.abort();
|
|
762
874
|
for (const [id, entry] of this.active) if (!keep.has(id) || !exists(id)) {
|
|
763
875
|
entry.visual.object.visible = false;
|
|
764
876
|
this.active.delete(id);
|
|
765
877
|
const pool = this.idle.get(entry.type) ?? [];
|
|
766
878
|
pool.push(entry.visual);
|
|
767
879
|
this.idle.set(entry.type, pool);
|
|
880
|
+
const resident = this.resident.get(entry.visual);
|
|
881
|
+
if (resident) resident.used = ++this.clock;
|
|
768
882
|
}
|
|
769
883
|
let started = 0;
|
|
770
884
|
for (const candidate of wanted) {
|
|
@@ -774,10 +888,11 @@ var VehicleDetail = class {
|
|
|
774
888
|
this.activate(candidate, pooled);
|
|
775
889
|
continue;
|
|
776
890
|
}
|
|
777
|
-
if (this.loading.has(candidate.type) || started >= 1) continue;
|
|
891
|
+
if (this.loading.has(candidate.type) || this.loading.size >= this.concurrentLimit || started >= 1) continue;
|
|
778
892
|
started++;
|
|
779
893
|
this.load(candidate.type);
|
|
780
894
|
}
|
|
895
|
+
this.prune();
|
|
781
896
|
}
|
|
782
897
|
activate(candidate, visual) {
|
|
783
898
|
const data = this.options.models.vehicle(candidate.type);
|
|
@@ -805,30 +920,60 @@ var VehicleDetail = class {
|
|
|
805
920
|
this.missing.add(type);
|
|
806
921
|
return;
|
|
807
922
|
}
|
|
808
|
-
|
|
923
|
+
const controller = new AbortController();
|
|
924
|
+
this.loading.set(type, controller);
|
|
925
|
+
let bound;
|
|
809
926
|
try {
|
|
810
|
-
const model = await this.options.models.load(type);
|
|
927
|
+
const model = await this.options.models.load(type, controller.signal);
|
|
811
928
|
if (model === void 0) {
|
|
812
|
-
this.missing.add(type);
|
|
929
|
+
if (!controller.signal.aborted) this.missing.add(type);
|
|
813
930
|
return;
|
|
814
931
|
}
|
|
815
|
-
if (this.disposed)
|
|
932
|
+
if (this.disposed || controller.signal.aborted || !this.wanted.has(type)) {
|
|
933
|
+
disposeUnbound(model);
|
|
934
|
+
return;
|
|
935
|
+
}
|
|
936
|
+
const bytes = modelBytes(model);
|
|
937
|
+
this.prune(bytes);
|
|
938
|
+
if (this.cacheStats().bytes + bytes > this.byteLimit) {
|
|
939
|
+
disposeUnbound(model);
|
|
940
|
+
if (bytes > this.byteLimit) this.missing.add(type);
|
|
941
|
+
return;
|
|
942
|
+
}
|
|
943
|
+
const originalMaterials = /* @__PURE__ */ new Set();
|
|
944
|
+
model.traverse((node) => {
|
|
945
|
+
const mesh = node;
|
|
946
|
+
if (mesh.isMesh) for (const material of Array.isArray(mesh.material) ? mesh.material : [mesh.material]) originalMaterials.add(material);
|
|
947
|
+
});
|
|
816
948
|
const visual = createVehicleVisual(model, data);
|
|
949
|
+
bound = visual;
|
|
950
|
+
for (const material of originalMaterials) material.dispose();
|
|
951
|
+
this.resident.set(visual, {
|
|
952
|
+
bytes,
|
|
953
|
+
used: ++this.clock
|
|
954
|
+
});
|
|
817
955
|
visual.object.visible = false;
|
|
818
956
|
visual.object.traverse((object) => {
|
|
819
957
|
object.userData.walkIgnore = true;
|
|
958
|
+
tintGlass(object);
|
|
820
959
|
});
|
|
821
960
|
this.group.add(visual.object);
|
|
822
961
|
await this.options.prepare?.(visual.object);
|
|
823
|
-
if (this.disposed) {
|
|
824
|
-
|
|
962
|
+
if (this.disposed || controller.signal.aborted || !this.wanted.has(type) || this.cacheStats().bytes > this.byteLimit) {
|
|
963
|
+
this.release(visual);
|
|
825
964
|
return;
|
|
826
965
|
}
|
|
827
|
-
const
|
|
828
|
-
|
|
829
|
-
|
|
966
|
+
const candidate = this.candidates.find((c) => c.type === type && !this.active.has(c.id));
|
|
967
|
+
if (candidate && this.active.size < this.max) this.activate(candidate, visual);
|
|
968
|
+
else {
|
|
969
|
+
const pool = this.idle.get(type) ?? [];
|
|
970
|
+
pool.push(visual);
|
|
971
|
+
this.idle.set(type, pool);
|
|
972
|
+
}
|
|
973
|
+
this.prune();
|
|
830
974
|
} catch {
|
|
831
|
-
this.
|
|
975
|
+
if (bound) this.release(bound);
|
|
976
|
+
if (!controller.signal.aborted) this.missing.add(type);
|
|
832
977
|
} finally {
|
|
833
978
|
this.loading.delete(type);
|
|
834
979
|
}
|
|
@@ -854,8 +999,8 @@ var VehicleDetail = class {
|
|
|
854
999
|
}
|
|
855
1000
|
dispose() {
|
|
856
1001
|
this.disposed = true;
|
|
857
|
-
for (const
|
|
858
|
-
for (const
|
|
1002
|
+
for (const controller of this.loading.values()) controller.abort();
|
|
1003
|
+
for (const visual of this.resident.keys()) this.release(visual);
|
|
859
1004
|
this.active.clear();
|
|
860
1005
|
this.idle.clear();
|
|
861
1006
|
this.group.removeFromParent();
|
|
@@ -904,6 +1049,11 @@ function createAmbientRenderer(options) {
|
|
|
904
1049
|
const applyBudget = () => {
|
|
905
1050
|
detail?.setMax(budget.detailedCars);
|
|
906
1051
|
boatDetail?.setMax(budget.detailedBoats ?? 4);
|
|
1052
|
+
for (const pool of [detail, boatDetail]) pool?.setCacheBudget({
|
|
1053
|
+
...budget.idleVehicleModels !== void 0 ? { idleModels: budget.idleVehicleModels } : {},
|
|
1054
|
+
...budget.vehicleModelBytes !== void 0 ? { modelBytes: budget.vehicleModelBytes } : {},
|
|
1055
|
+
...budget.vehicleLoads !== void 0 ? { concurrentLoads: budget.vehicleLoads } : {}
|
|
1056
|
+
});
|
|
907
1057
|
figures.setBudget({
|
|
908
1058
|
skinned: budget.skinnedFigures,
|
|
909
1059
|
radius: budget.figureRadius,
|
|
@@ -203,7 +203,7 @@ function featureDrafts(input, options) {
|
|
|
203
203
|
const width = classified.class === "water" ? feature.width ?? 60 : classified.class === "walk" ? dmath.max(1.2, feature.width ?? 2.4) : classified.class === "rail" ? 3 : transportWidth(feature, kind);
|
|
204
204
|
const lanes = classified.class === "road" ? roadLanes(feature, width).total : 2;
|
|
205
205
|
const link = isTransportLink(feature);
|
|
206
|
-
for (const line of joinTerrainLines(feature.lines)) for (const piece of clipPolyline(toWorld(line), minX, minZ, maxX, maxZ)) {
|
|
206
|
+
for (const line of joinTerrainLines(feature.lines, feature.oneway === true)) for (const piece of clipPolyline(toWorld(line), minX, minZ, maxX, maxZ)) {
|
|
207
207
|
const draft = {
|
|
208
208
|
class: classified.class,
|
|
209
209
|
kind: classified.kind,
|
|
@@ -237,6 +237,8 @@ const POI_KINDS = new Set([
|
|
|
237
237
|
"tram_stop",
|
|
238
238
|
"bus_stop",
|
|
239
239
|
"traffic_signals",
|
|
240
|
+
"give_way",
|
|
241
|
+
"stop",
|
|
240
242
|
"crossing",
|
|
241
243
|
"aerodrome"
|
|
242
244
|
]);
|
|
@@ -247,13 +249,20 @@ function tilePois(pois, origin, tileSize) {
|
|
|
247
249
|
const kind = (poi.subclass ?? poi.class).toLowerCase();
|
|
248
250
|
const matched = POI_KINDS.has(kind) ? kind : POI_KINDS.has(poi.class) ? poi.class : void 0;
|
|
249
251
|
if (matched === void 0) continue;
|
|
252
|
+
if ([
|
|
253
|
+
"traffic_signals",
|
|
254
|
+
"stop",
|
|
255
|
+
"give_way"
|
|
256
|
+
].includes(matched) && (poi.bridge || poi.tunnel)) continue;
|
|
250
257
|
const [u, v] = poi.point;
|
|
251
258
|
if (u < 0 || u > 1 || v < 0 || v > 1) continue;
|
|
252
259
|
result.push({
|
|
253
|
-
kind: matched,
|
|
260
|
+
kind: matched === "stop" ? "give_way" : matched,
|
|
254
261
|
x: origin[0] + u * tileSize,
|
|
255
262
|
z: origin[1] + v * tileSize,
|
|
256
|
-
...poi.name !== void 0 ? { name: poi.name } : {}
|
|
263
|
+
...poi.name !== void 0 ? { name: poi.name } : {},
|
|
264
|
+
...poi.stop === "all" ? { allWay: true } : {},
|
|
265
|
+
layer: poi.layer ?? 0
|
|
257
266
|
});
|
|
258
267
|
}
|
|
259
268
|
return result;
|
|
@@ -288,6 +297,7 @@ function wayDraft(way) {
|
|
|
288
297
|
const points = way.points.map((p) => p.length === 3 ? [p[0], p[2]] : [p[0], p[1]]);
|
|
289
298
|
const heights = way.points.every((p) => p.length === 3) ? way.points.map((p) => p[1]) : void 0;
|
|
290
299
|
const common = {
|
|
300
|
+
...way.drivingSide !== void 0 ? { drivingSide: way.drivingSide } : {},
|
|
291
301
|
points,
|
|
292
302
|
...heights !== void 0 ? { heights } : {},
|
|
293
303
|
layer: way.layer ?? 0,
|
|
@@ -513,6 +523,7 @@ const ambientStateSchema = z.strictObject({
|
|
|
513
523
|
});
|
|
514
524
|
const point = z.union([vec2, vec3]);
|
|
515
525
|
const waySchema = z.strictObject({
|
|
526
|
+
drivingSide: z.enum(["right", "left"]).describe("Overrides the document road driving side.").optional(),
|
|
516
527
|
id: z.string().min(1).describe("Optional stable id.").optional(),
|
|
517
528
|
class: z.enum([
|
|
518
529
|
"road",
|
package/dist/kernel.d.mts
CHANGED
|
@@ -2,6 +2,7 @@ import { a as WildlifeWorld, c as WILDLIFE_FAMILIES, d as WildlifeHabitat, f as
|
|
|
2
2
|
import { Quat } from "@bendyline/molen-kernel/determinism";
|
|
3
3
|
import { ComponentType, JsonObject, World } from "@bendyline/molen-kernel/world";
|
|
4
4
|
import { ComponentMap, EntityId, SceneManifest } from "@bendyline/molen-schema";
|
|
5
|
+
import { z } from "zod";
|
|
5
6
|
import { TerrainLinePath, TerrainPoiFeature, TerrainTransportationFeature } from "@bendyline/molen-terrain/kernel";
|
|
6
7
|
|
|
7
8
|
//#region src/kernel/components.d.ts
|
|
@@ -205,6 +206,8 @@ interface TransportEdge {
|
|
|
205
206
|
readonly tunnel: boolean;
|
|
206
207
|
readonly link: boolean;
|
|
207
208
|
readonly oneway: boolean;
|
|
209
|
+
/** Regional road traffic rule, retained through junction rebuilds and export. */
|
|
210
|
+
readonly drivingSide: "right" | "left";
|
|
208
211
|
readonly lanes: string[];
|
|
209
212
|
/** Source feature name, when mapped. */
|
|
210
213
|
readonly name?: string;
|
|
@@ -216,7 +219,7 @@ interface TransportLane {
|
|
|
216
219
|
readonly class: TransportClass;
|
|
217
220
|
/** 1 travels the edge path forward (from → to), -1 backward. */
|
|
218
221
|
readonly dir: 1 | -1;
|
|
219
|
-
/** 0 = innermost (
|
|
222
|
+
/** 0 = innermost (leftmost for right-hand one-way roads, rightmost for left-hand). */
|
|
220
223
|
readonly index: number;
|
|
221
224
|
/** Signed lateral offset from the centreline, positive to the right of travel. */
|
|
222
225
|
readonly offset: number;
|
|
@@ -340,6 +343,7 @@ declare function allRed(junction: TransportJunction, tick: number, tickRate: num
|
|
|
340
343
|
type Point2 = [number, number];
|
|
341
344
|
/** A polyline awaiting the planar split, in world metres. */
|
|
342
345
|
interface EdgeDraft {
|
|
346
|
+
drivingSide?: "right" | "left";
|
|
343
347
|
class: TransportClass;
|
|
344
348
|
kind: RoadKind;
|
|
345
349
|
points: Point2[];
|
|
@@ -400,6 +404,8 @@ interface WorldPoi {
|
|
|
400
404
|
name?: string;
|
|
401
405
|
/** Stations: dwell time in seconds. */
|
|
402
406
|
dwell?: number;
|
|
407
|
+
allWay?: boolean;
|
|
408
|
+
layer?: number;
|
|
403
409
|
}
|
|
404
410
|
//#endregion
|
|
405
411
|
//#region src/kernel/graph/doc.d.ts
|
|
@@ -407,6 +413,8 @@ declare const TRANSPORT_NETWORK_FORMAT: "molen/transport-network@1";
|
|
|
407
413
|
/** A way's point: `[x, z]` or `[x, y, z]` in scene metres. */
|
|
408
414
|
type TransportNetworkPoint = [number, number] | [number, number, number];
|
|
409
415
|
interface TransportNetworkWay {
|
|
416
|
+
/** Overrides the document's road driving side. */
|
|
417
|
+
drivingSide?: "right" | "left";
|
|
410
418
|
id?: string;
|
|
411
419
|
class: "road" | "rail" | "path" | "air" | "water";
|
|
412
420
|
subclass?: string;
|
|
@@ -469,11 +477,12 @@ interface GraphStore {
|
|
|
469
477
|
setJunction(junction: TransportJunction): void;
|
|
470
478
|
deleteJunction(id: string): void;
|
|
471
479
|
/** Whether a mapped traffic-signal point lies within `radius` of a point. */
|
|
472
|
-
signalNear(x: number, z: number, radius: number): boolean;
|
|
480
|
+
signalNear(x: number, z: number, radius: number, layer?: number): boolean;
|
|
473
481
|
/** Stop and yield signs within `radius` of a point. */
|
|
474
|
-
giveWayNear(x: number, z: number, radius: number): readonly {
|
|
482
|
+
giveWayNear(x: number, z: number, radius: number, layer?: number): readonly {
|
|
475
483
|
x: number;
|
|
476
484
|
z: number;
|
|
485
|
+
allWay?: boolean;
|
|
477
486
|
}[];
|
|
478
487
|
}
|
|
479
488
|
/** Monotone stand-in for atan2 in [0, 4): orders directions without transcendental math. */
|
|
@@ -511,6 +520,8 @@ declare class LaneGrid {
|
|
|
511
520
|
//#region src/kernel/graph/network.d.ts
|
|
512
521
|
/** One streamed map tile's transport data. */
|
|
513
522
|
interface AmbientTileInput {
|
|
523
|
+
/** Regional road driving side; defaults to the network's traffic setting. */
|
|
524
|
+
drivingSide?: "right" | "left";
|
|
514
525
|
/** Unique tile key, e.g. `terrainPyramidTileKey(address)`. */
|
|
515
526
|
key: string;
|
|
516
527
|
/** World X/Z of the tile's north-west corner, in metres. */
|
|
@@ -545,6 +556,7 @@ interface TransportNetworkStats {
|
|
|
545
556
|
}
|
|
546
557
|
/** A script-added polyline. */
|
|
547
558
|
interface AmbientPolyline {
|
|
559
|
+
drivingSide?: "right" | "left";
|
|
548
560
|
class: "road" | "rail" | "path" | "water";
|
|
549
561
|
subclass?: string;
|
|
550
562
|
/** `[x, z]` or `[x, y, z]` points in scene metres. */
|
|
@@ -587,8 +599,8 @@ declare class TransportNetwork implements GraphStore {
|
|
|
587
599
|
junction(id: string): TransportJunction | undefined;
|
|
588
600
|
setJunction(junction: TransportJunction): void;
|
|
589
601
|
deleteJunction(id: string): void;
|
|
590
|
-
signalNear(x: number, z: number, radius: number): boolean;
|
|
591
|
-
giveWayNear(x: number, z: number, radius: number): readonly WorldPoi[];
|
|
602
|
+
signalNear(x: number, z: number, radius: number, layer?: number): boolean;
|
|
603
|
+
giveWayNear(x: number, z: number, radius: number, layer?: number): readonly WorldPoi[];
|
|
592
604
|
private controlsNear;
|
|
593
605
|
/** Whether a tile key is registered. */
|
|
594
606
|
hasTile(key: string): boolean;
|
|
@@ -734,6 +746,8 @@ interface AmbientOptions {
|
|
|
734
746
|
policy?: DeepPartial<AmbientPolicy>;
|
|
735
747
|
/** Vehicle and pedestrian types per class (default: proxy dimensions). */
|
|
736
748
|
types?: Partial<AmbientTypes>;
|
|
749
|
+
/** Deterministic local road fleet at a prospective spawn point. An empty list suppresses it. */
|
|
750
|
+
carsAt?: (x: number, z: number) => readonly AmbientVehicleType[];
|
|
737
751
|
/** Extra components per class; `null` spawns bare agents (ambientAgent + transform only). */
|
|
738
752
|
templates?: Partial<Record<AmbientKind, AmbientTemplate | null>>;
|
|
739
753
|
/** Seed for spawn draws (default: 'ambient'). */
|
|
@@ -809,6 +823,34 @@ interface AmbientCapabilityOptions extends Omit<AmbientOptions, "classes" | "see
|
|
|
809
823
|
/** Build-world capability hook: `buildWorld(scene, setup, { capabilities: [ambientCapability()] })`. */
|
|
810
824
|
declare function ambientCapability(opts?: AmbientCapabilityOptions): (world: World, manifest: SceneManifest) => Record<string, object> | undefined;
|
|
811
825
|
//#endregion
|
|
826
|
+
//#region src/kernel/fleet.d.ts
|
|
827
|
+
interface FleetCatalog {
|
|
828
|
+
format: "molen/fleet-catalog@1";
|
|
829
|
+
id: string;
|
|
830
|
+
version: number;
|
|
831
|
+
description: string;
|
|
832
|
+
fallback: string;
|
|
833
|
+
regions: Record<string, string>;
|
|
834
|
+
profiles: Array<{
|
|
835
|
+
id: string;
|
|
836
|
+
title: string;
|
|
837
|
+
extends?: string;
|
|
838
|
+
weights: Record<string, number>;
|
|
839
|
+
}>;
|
|
840
|
+
}
|
|
841
|
+
declare const fleetCatalogSchema: z.ZodType<FleetCatalog>;
|
|
842
|
+
interface FleetMix {
|
|
843
|
+
profile: string;
|
|
844
|
+
weights: ReadonlyMap<string, number>;
|
|
845
|
+
}
|
|
846
|
+
interface FleetLibrary {
|
|
847
|
+
regions: ReadonlyMap<string, string>;
|
|
848
|
+
/** Stable bounded shortlist; never depends on camera motion, downloads or wall time. */
|
|
849
|
+
select(region?: string): FleetMix;
|
|
850
|
+
}
|
|
851
|
+
declare function createFleetLibrary(documents: readonly FleetCatalog[], available: ReadonlySet<string>, maxModels?: number): FleetLibrary;
|
|
852
|
+
declare function registerFleetSchema(): void;
|
|
853
|
+
//#endregion
|
|
812
854
|
//#region src/kernel/graph/export.d.ts
|
|
813
855
|
/** Every road, rail and path edge as a way (with surface heights), plus stations and signals. */
|
|
814
856
|
declare function networkToDocument(network: TransportNetwork, meta?: Pick<TransportNetworkDocument, "name" | "origin">, options?: {
|
|
@@ -893,4 +935,4 @@ interface ObserverView {
|
|
|
893
935
|
keep?: number;
|
|
894
936
|
}
|
|
895
937
|
//#endregion
|
|
896
|
-
export { AMBIENT_AGENT_COMPONENT, AMBIENT_ENTITY, AMBIENT_KINDS, AMBIENT_OBSERVER_COMPONENT, AMBIENT_ROLE_COMPONENT, AMBIENT_STATE_COMPONENT, type AgentEnv, AmbientAgent, type AmbientAgentData, type AmbientAirCorridor, type AmbientBox, type AmbientBudget, type AmbientCapabilityOptions, type AmbientClassPolicy, type AmbientHandle, type AmbientKind, AmbientObserver, type AmbientObserverData, type AmbientObserverState, type AmbientOptions, type AmbientPedestrianType, type AmbientPolicy, type AmbientPolyline, AmbientRole, type AmbientRoleData, type AmbientRoleVisual, type AmbientSolid, type AmbientSpawnInfo, AmbientState, type AmbientStateData, type AmbientStats, type AmbientTemplate, type AmbientTileInput, type AmbientTypes, type AmbientVehicleType, CAR_COLORS, DEFAULT_POLICY, DEFAULT_TEMPLATES, DEFAULT_TYPES, type DeepPartial, type DriverParams, type EdgeDraft, type EdgeSpan, GRID_CELL, type JunctionArm, type JunctionControl, KIND_SPEED, LaneGrid, type LaneMovement, type LaneSample, type LaneStop, type ObserverView, type PlanarNode, type PlanarPiece, type PlanarResult, type RoadKind, type SignalColor, type SignalTiming, TRANSPORT_NETWORK_EXAMPLE, TRANSPORT_NETWORK_FORMAT, type TransportClass, type TransportCrossing, type TransportEdge, type TransportJunction, type TransportLane, TransportNetwork, type TransportNetworkAerodrome, type TransportNetworkDocument, type TransportNetworkOptions, type TransportNetworkPoint, type TransportNetworkSignal, type TransportNetworkStation, type TransportNetworkStats, type TransportNetworkWay, type TransportNode, type TransportRunway, type TurnKind, WILDLIFE_CELL_METERS, WILDLIFE_FAMILIES, WILDLIFE_TICK_RATE, type WildlifeAgent, type WildlifeBudget, type WildlifeChoice, type WildlifeFamily, type WildlifeHabitat, type WildlifeMotion, type WildlifeSpecies, type WildlifeWorld, type WorldPoi, 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 };
|
|
938
|
+
export { AMBIENT_AGENT_COMPONENT, AMBIENT_ENTITY, AMBIENT_KINDS, AMBIENT_OBSERVER_COMPONENT, AMBIENT_ROLE_COMPONENT, AMBIENT_STATE_COMPONENT, type AgentEnv, AmbientAgent, type AmbientAgentData, type AmbientAirCorridor, type AmbientBox, type AmbientBudget, type AmbientCapabilityOptions, type AmbientClassPolicy, type AmbientHandle, type AmbientKind, AmbientObserver, type AmbientObserverData, type AmbientObserverState, type AmbientOptions, type AmbientPedestrianType, type AmbientPolicy, type AmbientPolyline, AmbientRole, type AmbientRoleData, type AmbientRoleVisual, type AmbientSolid, type AmbientSpawnInfo, AmbientState, type AmbientStateData, type AmbientStats, type AmbientTemplate, type AmbientTileInput, type AmbientTypes, type AmbientVehicleType, CAR_COLORS, DEFAULT_POLICY, DEFAULT_TEMPLATES, DEFAULT_TYPES, type DeepPartial, type DriverParams, type EdgeDraft, type EdgeSpan, type FleetCatalog, type FleetLibrary, type FleetMix, GRID_CELL, type JunctionArm, type JunctionControl, KIND_SPEED, LaneGrid, type LaneMovement, type LaneSample, type LaneStop, type ObserverView, type PlanarNode, type PlanarPiece, type PlanarResult, type RoadKind, type SignalColor, type SignalTiming, TRANSPORT_NETWORK_EXAMPLE, TRANSPORT_NETWORK_FORMAT, type TransportClass, type TransportCrossing, type TransportEdge, type TransportJunction, type TransportLane, TransportNetwork, type TransportNetworkAerodrome, type TransportNetworkDocument, type TransportNetworkOptions, type TransportNetworkPoint, type TransportNetworkSignal, type TransportNetworkStation, type TransportNetworkStats, type TransportNetworkWay, type TransportNode, type TransportRunway, type TurnKind, WILDLIFE_CELL_METERS, WILDLIFE_FAMILIES, WILDLIFE_TICK_RATE, type WildlifeAgent, type WildlifeBudget, type WildlifeChoice, type WildlifeFamily, type WildlifeHabitat, type WildlifeMotion, type WildlifeSpecies, type WildlifeWorld, type WorldPoi, 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 };
|
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-vzvNQczG.mjs";
|
|
2
2
|
import { createRng, dmath, lookRotation, quatRotateVec3, seedToInt } from "@bendyline/molen-kernel/determinism";
|
|
3
3
|
import { Transform, componentHandle } from "@bendyline/molen-kernel/world";
|
|
4
|
-
import { deepMergeJson, resolvePrefab, validateByKind } from "@bendyline/molen-schema";
|
|
4
|
+
import { 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
|
/**
|
|
@@ -958,7 +958,7 @@ function stepAircraft(ref, env) {
|
|
|
958
958
|
//#endregion
|
|
959
959
|
//#region src/kernel/graph/junctions.ts
|
|
960
960
|
/**
|
|
961
|
-
* Node groups
|
|
961
|
+
* Node groups → junctions and movements. A node group is a node plus the node it is stitched to
|
|
962
962
|
* across a tile border. For every lane arriving at the group this computes its outgoing
|
|
963
963
|
* movements (turn class, target lane, connector curve), trims lanes back from junction cores, and
|
|
964
964
|
* builds the junction record: arms in angular order, a conflict matrix between arm-pair
|
|
@@ -1085,8 +1085,8 @@ var EndCache = class {
|
|
|
1085
1085
|
return p;
|
|
1086
1086
|
}
|
|
1087
1087
|
};
|
|
1088
|
-
function roadWeight(turn, target) {
|
|
1089
|
-
return (turn === "straight" ? 3 : turn ===
|
|
1088
|
+
function roadWeight(turn, target, drivingSide) {
|
|
1089
|
+
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
1090
|
}
|
|
1091
1091
|
const MAX_CONFLICT_ARMS = 8;
|
|
1092
1092
|
/**
|
|
@@ -1142,9 +1142,9 @@ function buildJunction(store, id, group, arms, radius) {
|
|
|
1142
1142
|
cx /= group.length;
|
|
1143
1143
|
cz /= group.length;
|
|
1144
1144
|
const n = arms.length;
|
|
1145
|
-
const side = store.traffic === "right" ? 1 : -1;
|
|
1146
1145
|
const rim = [];
|
|
1147
1146
|
arms.forEach((arm, k) => {
|
|
1147
|
+
const side = arm.edge.drivingSide === "right" ? 1 : -1;
|
|
1148
1148
|
const bx = arm.node.x + arm.dx * radius - cx;
|
|
1149
1149
|
const bz = arm.node.z + arm.dz * radius - cz;
|
|
1150
1150
|
const q = arm.edge.width / 4;
|
|
@@ -1213,13 +1213,20 @@ function buildJunction(store, id, group, arms, radius) {
|
|
|
1213
1213
|
if (best >= 0) junctionArms[best].group = groups;
|
|
1214
1214
|
groups++;
|
|
1215
1215
|
}
|
|
1216
|
-
const major = arms.
|
|
1216
|
+
const major = arms.filter((a) => isArterialRoad(a.edge.kind, a.edge.width)).length;
|
|
1217
1217
|
const minor = arms.filter((a) => a.edge.kind === "service" || a.edge.kind === "living" || a.edge.link).length;
|
|
1218
1218
|
let signed = 0;
|
|
1219
|
-
for (const sign of store.giveWayNear(cx, cz, radius + 20)) {
|
|
1219
|
+
for (const sign of store.giveWayNear(cx, cz, radius + 20, group[0]?.layer)) {
|
|
1220
1220
|
const sx = sign.x - cx;
|
|
1221
1221
|
const sz = sign.z - cz;
|
|
1222
1222
|
const length = dmath.hypot(sx, sz);
|
|
1223
|
+
if (sign.allWay && length <= 4) {
|
|
1224
|
+
for (const arm of junctionArms) {
|
|
1225
|
+
if (!arm.yields) signed++;
|
|
1226
|
+
arm.yields = true;
|
|
1227
|
+
}
|
|
1228
|
+
continue;
|
|
1229
|
+
}
|
|
1223
1230
|
let best;
|
|
1224
1231
|
let bestDot = Number.NEGATIVE_INFINITY;
|
|
1225
1232
|
for (const arm of junctionArms) {
|
|
@@ -1235,8 +1242,8 @@ function buildJunction(store, id, group, arms, radius) {
|
|
|
1235
1242
|
}
|
|
1236
1243
|
}
|
|
1237
1244
|
let control = "none";
|
|
1238
|
-
if (groups >= 2 && (
|
|
1239
|
-
else if (minor > 0 && minor < n || signed > 0
|
|
1245
|
+
if (groups >= 2 && (store.signalNear(cx, cz, radius + 20, group[0]?.layer) || n >= 4 && major >= 4 && signed === 0)) control = "signal";
|
|
1246
|
+
else if (minor > 0 && minor < n || signed > 0) control = "yield";
|
|
1240
1247
|
return {
|
|
1241
1248
|
id,
|
|
1242
1249
|
x: cx,
|
|
@@ -1296,8 +1303,8 @@ function movement(cache, from, to, turn, weight, junction, key, crosses) {
|
|
|
1296
1303
|
}
|
|
1297
1304
|
function buildRoadMovements(store, cache, arms, junction) {
|
|
1298
1305
|
const n = arms.length;
|
|
1299
|
-
const left = store.traffic === "left";
|
|
1300
1306
|
arms.forEach((inArm, i) => {
|
|
1307
|
+
const left = inArm.edge.drivingSide === "left";
|
|
1301
1308
|
const incoming = lanesAt(store, inArm, "road", true);
|
|
1302
1309
|
const nIn = incoming.length;
|
|
1303
1310
|
for (const lane of incoming) {
|
|
@@ -1328,11 +1335,11 @@ function buildRoadMovements(store, cache, arms, junction) {
|
|
|
1328
1335
|
else if (turn !== nearSide && inner) target = outgoing[0];
|
|
1329
1336
|
if (target === void 0) return;
|
|
1330
1337
|
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);
|
|
1338
|
+
const move = movement(cache, lane, target, moveTurn, roadWeight(moveTurn, outArm.edge, inArm.edge.drivingSide), n >= 3 ? junction : void 0, i * n + o);
|
|
1332
1339
|
if (move !== void 0) moves.push(move);
|
|
1333
1340
|
});
|
|
1334
1341
|
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);
|
|
1342
|
+
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
1343
|
if (move !== void 0) moves.push(move);
|
|
1337
1344
|
}
|
|
1338
1345
|
if (moves.length === 0) {
|
|
@@ -1926,17 +1933,17 @@ var TransportNetwork = class {
|
|
|
1926
1933
|
deleteJunction(id) {
|
|
1927
1934
|
this.junctions.delete(id);
|
|
1928
1935
|
}
|
|
1929
|
-
signalNear(x, z, radius) {
|
|
1930
|
-
return this.controlsNear(x, z, radius, "traffic_signals").length > 0;
|
|
1936
|
+
signalNear(x, z, radius, layer = 0) {
|
|
1937
|
+
return this.controlsNear(x, z, radius, "traffic_signals", layer).length > 0;
|
|
1931
1938
|
}
|
|
1932
|
-
giveWayNear(x, z, radius) {
|
|
1933
|
-
return this.controlsNear(x, z, radius, "give_way");
|
|
1939
|
+
giveWayNear(x, z, radius, layer = 0) {
|
|
1940
|
+
return this.controlsNear(x, z, radius, "give_way", layer);
|
|
1934
1941
|
}
|
|
1935
|
-
controlsNear(x, z, radius, kind) {
|
|
1942
|
+
controlsNear(x, z, radius, kind, layer) {
|
|
1936
1943
|
const cx = dmath.floor(x / 64);
|
|
1937
1944
|
const cz = dmath.floor(z / 64);
|
|
1938
1945
|
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);
|
|
1946
|
+
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
1947
|
return found;
|
|
1941
1948
|
}
|
|
1942
1949
|
/** Whether a tile key is registered. */
|
|
@@ -1978,6 +1985,7 @@ var TransportNetwork = class {
|
|
|
1978
1985
|
registerTile(input) {
|
|
1979
1986
|
if (this.tiles.has(input.key)) this.unregisterTile(input.key);
|
|
1980
1987
|
const { drafts, runways } = featureDrafts(input, { tunnels: this.tunnels });
|
|
1988
|
+
for (const draft of drafts) draft.drivingSide = input.drivingSide ?? this.traffic;
|
|
1981
1989
|
const pois = tilePois(input.pois, input.origin, input.tileSize);
|
|
1982
1990
|
const rect = [
|
|
1983
1991
|
input.origin[0],
|
|
@@ -2031,7 +2039,10 @@ var TransportNetwork = class {
|
|
|
2031
2039
|
/** Add a transport-network document's ways, stations, signals and aerodromes. */
|
|
2032
2040
|
addDocument(doc) {
|
|
2033
2041
|
for (const way of doc.ways) {
|
|
2034
|
-
const draft = wayDraft(
|
|
2042
|
+
const draft = wayDraft({
|
|
2043
|
+
...way,
|
|
2044
|
+
drivingSide: way.drivingSide ?? doc.drivingSide ?? this.traffic
|
|
2045
|
+
});
|
|
2035
2046
|
if (draft !== void 0) this.docDrafts.push(draft);
|
|
2036
2047
|
}
|
|
2037
2048
|
this.docPois.push(...documentPois(doc));
|
|
@@ -2158,6 +2169,7 @@ var TransportNetwork = class {
|
|
|
2158
2169
|
tunnel: draft.tunnel,
|
|
2159
2170
|
link: draft.link,
|
|
2160
2171
|
oneway: draft.oneway,
|
|
2172
|
+
drivingSide: draft.drivingSide ?? this.traffic,
|
|
2161
2173
|
lanes: [],
|
|
2162
2174
|
...draft.name !== void 0 ? { name: draft.name } : {}
|
|
2163
2175
|
};
|
|
@@ -2194,7 +2206,7 @@ var TransportNetwork = class {
|
|
|
2194
2206
|
this.version++;
|
|
2195
2207
|
}
|
|
2196
2208
|
createLanes(edge, draft, sidewalks) {
|
|
2197
|
-
const side =
|
|
2209
|
+
const side = edge.drivingSide === "right" ? 1 : -1;
|
|
2198
2210
|
const make = (code, cls, dir, index, offset, lift = 0) => {
|
|
2199
2211
|
const id = `${edge.id}>${code}`;
|
|
2200
2212
|
const lane = {
|
|
@@ -3272,16 +3284,16 @@ function spawnOne(ctx, kind) {
|
|
|
3272
3284
|
if (lane === void 0) continue;
|
|
3273
3285
|
const seq = ctx.state.nextSeq;
|
|
3274
3286
|
const seed = fmix32(seq ^ env.seed);
|
|
3275
|
-
const
|
|
3276
|
-
|
|
3287
|
+
const s = laneCoordinate(lane, edge, span.d0 + rng.next() * (span.d1 - span.d0));
|
|
3288
|
+
const sample = sampleLaneBody(lane, edge, s, laneSample());
|
|
3289
|
+
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;
|
|
3290
|
+
if ((kind === "boat" || kind === "car") && available.length === 0) continue;
|
|
3277
3291
|
const vehicle = kind === "pedestrian" ? void 0 : pickWeighted(available, unit01(seed, 10));
|
|
3278
3292
|
const length = kind === "pedestrian" ? .5 : vehicle?.length ?? 4.5;
|
|
3279
3293
|
const width = kind === "pedestrian" ? .5 : vehicle?.width ?? 1.8;
|
|
3280
3294
|
const cars = kind === "train" ? dmath.max(1, vehicle?.cars ?? 2) : 1;
|
|
3281
3295
|
const behind = kind === "train" ? (cars - 1) * (length + 1) : 0;
|
|
3282
|
-
const s = laneCoordinate(lane, edge, span.d0 + rng.next() * (span.d1 - span.d0));
|
|
3283
3296
|
if (s < behind + length / 2 + 1 || s > lane.length - length / 2 - 1) continue;
|
|
3284
|
-
const sample = sampleLaneBody(lane, edge, s, laneSample());
|
|
3285
3297
|
const dx = sample.x - observer.x;
|
|
3286
3298
|
const dz = sample.z - observer.z;
|
|
3287
3299
|
const distance = dmath.hypot(dx, dz);
|
|
@@ -3610,7 +3622,8 @@ function installAmbient(world, options = {}) {
|
|
|
3610
3622
|
types,
|
|
3611
3623
|
templates,
|
|
3612
3624
|
seed,
|
|
3613
|
-
ground
|
|
3625
|
+
ground,
|
|
3626
|
+
...options.carsAt ? { carsAt: options.carsAt } : {}
|
|
3614
3627
|
};
|
|
3615
3628
|
const removed = despawnAgents(w, env, observer);
|
|
3616
3629
|
const counters = {
|
|
@@ -3899,6 +3912,127 @@ function cloneMap(map) {
|
|
|
3899
3912
|
return JSON.parse(JSON.stringify(map));
|
|
3900
3913
|
}
|
|
3901
3914
|
//#endregion
|
|
3915
|
+
//#region src/kernel/fleet.ts
|
|
3916
|
+
/** Small, geometry-free market profiles. Weights are visual priors, not sales statistics. */
|
|
3917
|
+
const weights = z.record(z.string().min(1), z.number().finite().nonnegative());
|
|
3918
|
+
const fleetCatalogSchema = z.strictObject({
|
|
3919
|
+
format: z.literal("molen/fleet-catalog@1"),
|
|
3920
|
+
id: z.string().min(1),
|
|
3921
|
+
version: z.number().int().positive(),
|
|
3922
|
+
description: z.string(),
|
|
3923
|
+
fallback: z.string().min(1),
|
|
3924
|
+
regions: z.record(z.string(), z.string().min(1)),
|
|
3925
|
+
profiles: z.array(z.strictObject({
|
|
3926
|
+
id: z.string().min(1),
|
|
3927
|
+
title: z.string().min(1),
|
|
3928
|
+
extends: z.string().optional(),
|
|
3929
|
+
weights
|
|
3930
|
+
})).min(1)
|
|
3931
|
+
});
|
|
3932
|
+
function seedOf(value) {
|
|
3933
|
+
let seed = 2166136261;
|
|
3934
|
+
for (let i = 0; i < value.length; i++) seed = Math.imul(seed ^ value.charCodeAt(i), 16777619);
|
|
3935
|
+
return fmix32(seed);
|
|
3936
|
+
}
|
|
3937
|
+
function createFleetLibrary(documents, available, maxModels = 16) {
|
|
3938
|
+
if (!Number.isInteger(maxModels) || maxModels < 1 || maxModels > 128) throw new Error("Fleet maxModels must be an integer from 1 to 128");
|
|
3939
|
+
const profiles = /* @__PURE__ */ new Map();
|
|
3940
|
+
const regions = /* @__PURE__ */ new Map();
|
|
3941
|
+
let fallback;
|
|
3942
|
+
for (const raw of documents) {
|
|
3943
|
+
const doc = fleetCatalogSchema.parse(raw);
|
|
3944
|
+
fallback ??= doc.fallback;
|
|
3945
|
+
for (const profile of doc.profiles) {
|
|
3946
|
+
if (profiles.has(profile.id)) throw new Error(`Duplicate fleet profile: ${profile.id}`);
|
|
3947
|
+
profiles.set(profile.id, profile);
|
|
3948
|
+
}
|
|
3949
|
+
for (const [region, profile] of Object.entries(doc.regions)) {
|
|
3950
|
+
if (regions.has(region)) throw new Error(`Duplicate fleet region: ${region}`);
|
|
3951
|
+
regions.set(region, profile);
|
|
3952
|
+
}
|
|
3953
|
+
}
|
|
3954
|
+
const resolved = /* @__PURE__ */ new Map();
|
|
3955
|
+
const resolve = (id, visiting = /* @__PURE__ */ new Set()) => {
|
|
3956
|
+
const cached = resolved.get(id);
|
|
3957
|
+
if (cached) return cached;
|
|
3958
|
+
const profile = profiles.get(id);
|
|
3959
|
+
if (!profile) throw new Error(`Unknown fleet profile: ${id}`);
|
|
3960
|
+
if (visiting.has(id)) throw new Error(`Cyclic fleet inheritance: ${id}`);
|
|
3961
|
+
visiting.add(id);
|
|
3962
|
+
const result = {
|
|
3963
|
+
...profile.extends ? resolve(profile.extends, visiting) : {},
|
|
3964
|
+
...profile.weights
|
|
3965
|
+
};
|
|
3966
|
+
visiting.delete(id);
|
|
3967
|
+
resolved.set(id, result);
|
|
3968
|
+
return result;
|
|
3969
|
+
};
|
|
3970
|
+
for (const id of profiles.keys()) resolve(id);
|
|
3971
|
+
for (const id of regions.values()) resolve(id);
|
|
3972
|
+
if (!fallback) throw new Error("A fleet catalog needs a fallback");
|
|
3973
|
+
resolve(fallback);
|
|
3974
|
+
const mixes = /* @__PURE__ */ new Map();
|
|
3975
|
+
const select = (region) => {
|
|
3976
|
+
const profile = region && regions.get(region) || fallback;
|
|
3977
|
+
const cached = mixes.get(profile);
|
|
3978
|
+
if (cached) return cached;
|
|
3979
|
+
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));
|
|
3980
|
+
const chosen = pool.splice(0, Math.min(4, maxModels));
|
|
3981
|
+
const seed = seedOf(profile);
|
|
3982
|
+
while (pool.length && chosen.length < maxModels) {
|
|
3983
|
+
let draw = unit01(seed, chosen.length) * pool.reduce((sum, [, weight]) => sum + weight, 0);
|
|
3984
|
+
let at = 0;
|
|
3985
|
+
while (at < pool.length - 1 && draw >= (pool[at]?.[1] ?? 0)) draw -= pool[at++]?.[1] ?? 0;
|
|
3986
|
+
chosen.push(...pool.splice(at, 1));
|
|
3987
|
+
}
|
|
3988
|
+
const mix = {
|
|
3989
|
+
profile,
|
|
3990
|
+
weights: new Map(chosen)
|
|
3991
|
+
};
|
|
3992
|
+
mixes.set(profile, mix);
|
|
3993
|
+
return mix;
|
|
3994
|
+
};
|
|
3995
|
+
return {
|
|
3996
|
+
regions,
|
|
3997
|
+
select
|
|
3998
|
+
};
|
|
3999
|
+
}
|
|
4000
|
+
function registerFleetSchema() {
|
|
4001
|
+
if (getSchema("fleet-catalog")) return;
|
|
4002
|
+
registerSchema("fleet-catalog", fleetCatalogSchema, {
|
|
4003
|
+
id: "molen/fleet-catalog@1",
|
|
4004
|
+
title: "Regional vehicle fleet",
|
|
4005
|
+
description: "Shared vehicle IDs with inherited regional spawn weights and a global fallback.",
|
|
4006
|
+
docsRef: "guide/regional-fleets.md",
|
|
4007
|
+
examples: [{
|
|
4008
|
+
format: "molen/fleet-catalog@1",
|
|
4009
|
+
id: "example.fleet",
|
|
4010
|
+
version: 1,
|
|
4011
|
+
description: "Illustrative visual mixture",
|
|
4012
|
+
fallback: "global",
|
|
4013
|
+
regions: {},
|
|
4014
|
+
profiles: [{
|
|
4015
|
+
id: "global",
|
|
4016
|
+
title: "Global",
|
|
4017
|
+
weights: { "example.car": 1 }
|
|
4018
|
+
}]
|
|
4019
|
+
}],
|
|
4020
|
+
validate(data) {
|
|
4021
|
+
try {
|
|
4022
|
+
createFleetLibrary([data], /* @__PURE__ */ new Set());
|
|
4023
|
+
return [];
|
|
4024
|
+
} catch (error) {
|
|
4025
|
+
return [{
|
|
4026
|
+
path: "",
|
|
4027
|
+
code: "invalid_fleet",
|
|
4028
|
+
message: error.message
|
|
4029
|
+
}];
|
|
4030
|
+
}
|
|
4031
|
+
}
|
|
4032
|
+
});
|
|
4033
|
+
}
|
|
4034
|
+
registerFleetSchema();
|
|
4035
|
+
//#endregion
|
|
3902
4036
|
//#region src/kernel/graph/export.ts
|
|
3903
4037
|
/** Export a built network as a molen/transport-network@1 document (the bake op's output). */
|
|
3904
4038
|
const round = (v) => dmath.round(v * 100) / 100;
|
|
@@ -3931,6 +4065,7 @@ function networkToDocument(network, meta = {}, options = {}) {
|
|
|
3931
4065
|
lanes: roadLanes,
|
|
3932
4066
|
oneway: edge.oneway
|
|
3933
4067
|
} : {},
|
|
4068
|
+
...(edge.class === "road" || edge.class === "walk") && edge.drivingSide !== network.traffic ? { drivingSide: edge.drivingSide } : {},
|
|
3934
4069
|
...edge.class === "water" ? {
|
|
3935
4070
|
oneway: edge.oneway,
|
|
3936
4071
|
speed: edge.speed
|
|
@@ -4157,4 +4292,4 @@ const wildlifeSpeciesSchema = z.strictObject({
|
|
|
4157
4292
|
//#region src/kernel.ts
|
|
4158
4293
|
registerAmbientSchema();
|
|
4159
4294
|
//#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 };
|
|
4295
|
+
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 };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bendyline/molen-ambient",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.12",
|
|
4
4
|
"private": false,
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -41,15 +41,15 @@
|
|
|
41
41
|
"dist"
|
|
42
42
|
],
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"
|
|
45
|
-
"@bendyline/molen-
|
|
46
|
-
"@bendyline/molen-schema": "0.0.
|
|
47
|
-
"@bendyline/molen-
|
|
48
|
-
"
|
|
44
|
+
"@bendyline/molen-figures": "0.0.10",
|
|
45
|
+
"@bendyline/molen-kernel": "0.0.8",
|
|
46
|
+
"@bendyline/molen-schema": "0.0.8",
|
|
47
|
+
"@bendyline/molen-terrain": "0.0.12",
|
|
48
|
+
"zod": "^4.6.5"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
|
-
"
|
|
52
|
-
"
|
|
51
|
+
"@bendyline/molen-client": "0.0.10",
|
|
52
|
+
"three": ">=0.184.0 <0.187.0"
|
|
53
53
|
},
|
|
54
54
|
"peerDependenciesMeta": {
|
|
55
55
|
"@bendyline/molen-client": {
|
|
@@ -60,14 +60,14 @@
|
|
|
60
60
|
}
|
|
61
61
|
},
|
|
62
62
|
"devDependencies": {
|
|
63
|
+
"@bendyline/molen-client": "0.0.10",
|
|
63
64
|
"@biomejs/biome": "^2.5.0",
|
|
64
65
|
"@types/three": "0.184.0",
|
|
65
66
|
"fast-check": "^3.23.2",
|
|
66
67
|
"three": "0.184.0",
|
|
67
68
|
"tsdown": "^0.22.2",
|
|
68
69
|
"typescript": "^6.0.3",
|
|
69
|
-
"vitest": "^5.0.1"
|
|
70
|
-
"@bendyline/molen-client": "0.0.9"
|
|
70
|
+
"vitest": "^5.0.1"
|
|
71
71
|
},
|
|
72
72
|
"scripts": {
|
|
73
73
|
"build": "tsdown",
|