@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.
@@ -3,6 +3,37 @@ import { defineComponent } from "@bendyline/molen-kernel/world";
3
3
  import { getSchema, registerComponent, registerRenderableKind, registerSchema } from "@bendyline/molen-schema";
4
4
  import { z } from "zod";
5
5
  import { groupTransportationFeatures, inferLaneCount, isTransportLink, joinTerrainLines, roadWidth, transportKind, transportWidth } from "@bendyline/molen-terrain/kernel";
6
+ //#region src/kernel/ids.ts
7
+ /** Small deterministic hashes and seeded draws shared by the graph and the agents. */
8
+ /** FNV-1a 32-bit hash of a string. */
9
+ function fnv1a(text) {
10
+ let h = 2166136261;
11
+ for (let i = 0; i < text.length; i++) {
12
+ h ^= text.charCodeAt(i);
13
+ h = Math.imul(h, 16777619);
14
+ }
15
+ return h >>> 0;
16
+ }
17
+ /** Short base-36 hash of a string (up to 7 characters). */
18
+ function shortHash(text) {
19
+ return fnv1a(text).toString(36);
20
+ }
21
+ /** murmur3 finalizer: a full-avalanche 32-bit mix. */
22
+ function fmix32(x) {
23
+ let h = x | 0;
24
+ h = Math.imul(h ^ h >>> 16, 2246822507);
25
+ h = Math.imul(h ^ h >>> 13, 3266489909);
26
+ return (h ^ h >>> 16) >>> 0;
27
+ }
28
+ /** Independent stateless draw in [0, 1) for a (seed, stream) pair. */
29
+ function unit01(seed, stream) {
30
+ return fmix32((seed | 0) ^ Math.imul((stream | 0) + 1, 2654435761)) / 4294967296;
31
+ }
32
+ /** Quantize to 1e-4 so stored state equals the state the next tick reads. */
33
+ function q4(value) {
34
+ return Math.round(value * 1e4) / 1e4;
35
+ }
36
+ //#endregion
6
37
  //#region src/kernel/graph/drafts.ts
7
38
  /**
8
39
  * Map features → edge drafts: world-metre polylines with a transport class, kind, width, lane
@@ -42,7 +73,7 @@ function classifyFeature(feature) {
42
73
  if (feature.service !== void 0 && /yard|siding|spur|crossover/.test(feature.service)) return void 0;
43
74
  return {
44
75
  class: "rail",
45
- kind: /tram/.test(sub) ? "tram" : "rail"
76
+ kind: /tram|light_rail|subway|monorail/.test(sub) ? "tram" : "rail"
46
77
  };
47
78
  }
48
79
  if (kind === "path") {
@@ -457,7 +488,7 @@ function wayDraft(way) {
457
488
  if (way.class === "rail") return {
458
489
  ...common,
459
490
  class: "rail",
460
- kind: way.subclass === "tram" ? "tram" : "rail",
491
+ kind: /^(tram|light_rail|subway|monorail)$/.test(way.subclass ?? "") ? "tram" : "rail",
461
492
  width: way.width ?? 3,
462
493
  lanes: 2,
463
494
  oneway: false
@@ -603,6 +634,7 @@ const ambientAgentSchema = z.strictObject({
603
634
  hidden: z.boolean().describe("Inside a tunnel: simulated but not drawn.").optional(),
604
635
  consist: z.string().describe("Train cars: the lead agent of the consist.").optional(),
605
636
  carIndex: z.int().positive().describe("Train cars: position behind the lead (1 = first).").optional(),
637
+ offset: num.positive().describe("Train cars: metres behind the lead's centre (default from carIndex).").optional(),
606
638
  trail: z.array(z.string()).describe("Train leads: the lanes behind the lead, most recent first.").optional(),
607
639
  served: z.string().describe("The last stop served (not stopped at again).").optional(),
608
640
  air: z.strictObject({
@@ -637,6 +669,17 @@ const ambientRoleSchema = z.strictObject({
637
669
  max: num.positive().describe("Top speed in m/s.").optional(),
638
670
  weight: num.nonnegative().describe("Relative spawn weight within its pool (default 1).").optional(),
639
671
  cars: z.int().positive().describe("Rail: cars per consist (default 2).").optional(),
672
+ track: z.enum(["light", "heavy"]).describe("Rail: tram and light-rail lines (light) or the rest (heavy); default any.").optional(),
673
+ consist: z.strictObject({
674
+ units: z.tuple([z.int().positive(), z.int().positive()]).describe("Copies of this type at the head, inclusive range (default [1, 1]).").optional(),
675
+ length: z.tuple([z.int().nonnegative(), z.int().nonnegative()]).describe("Cars after the units, inclusive range."),
676
+ block: z.tuple([z.int().positive(), z.int().positive()]).describe("Cars of one type in a row, inclusive range (default [1, 1]).").optional(),
677
+ cars: z.array(z.strictObject({
678
+ type: z.string().min(1).describe("Car entity type id."),
679
+ weight: num.nonnegative().describe("Relative draw weight (default 1).").optional(),
680
+ colors: z.array(z.string()).min(1).describe("Paints this car picks from (#rrggbb).").optional()
681
+ })).min(1).describe("The car pool.")
682
+ }).describe("Rail: a mixed train this type leads; supersedes cars.").optional(),
640
683
  colors: z.array(z.string()).min(1).describe("Paint colours spawned agents pick from (#rrggbb).").optional(),
641
684
  visual: z.strictObject({
642
685
  wheelNodes: z.array(z.string()).describe("Wheel nodes (their first child spins).").optional(),
@@ -845,4 +888,320 @@ const AmbientState = defineComponent(AMBIENT_STATE_COMPONENT);
845
888
  /** The singleton entity holding ambient bookkeeping. */
846
889
  const AMBIENT_ENTITY = "$ambient";
847
890
  //#endregion
848
- export { KIND_SPEED as _, AmbientRole as a, featureDrafts as b, AMBIENT_OBSERVER_COMPONENT as c, registerAmbientSchema as d, TRANSPORT_NETWORK_EXAMPLE as f, wayDraft as g, documentRunways as h, AmbientObserver as i, AMBIENT_ROLE_COMPONENT as l, documentPois as m, AMBIENT_KINDS as n, AmbientState as o, TRANSPORT_NETWORK_FORMAT as p, AmbientAgent as r, AMBIENT_AGENT_COMPONENT as s, AMBIENT_ENTITY as t, AMBIENT_STATE_COMPONENT as u, classifyFeature as v, tilePois as x, clipPolyline as y };
891
+ //#region src/kernel/templates.ts
892
+ /** Common real-world car body colours, weighted by repetition. */
893
+ const CAR_COLORS = [
894
+ "#f2f2f0",
895
+ "#f2f2f0",
896
+ "#f2f2f0",
897
+ "#1c1d21",
898
+ "#1c1d21",
899
+ "#1c1d21",
900
+ "#8d9196",
901
+ "#8d9196",
902
+ "#b9bcbf",
903
+ "#b9bcbf",
904
+ "#2f4f86",
905
+ "#8c1f1f",
906
+ "#3f5a3c",
907
+ "#6b4a2e",
908
+ "#c9b48a"
909
+ ];
910
+ const DEFAULT_TYPES = {
911
+ car: [
912
+ {
913
+ id: "proxy.sedan",
914
+ length: 4.6,
915
+ width: 1.8,
916
+ height: 1.45,
917
+ weight: 3
918
+ },
919
+ {
920
+ id: "proxy.suv",
921
+ length: 4.8,
922
+ width: 1.9,
923
+ height: 1.75,
924
+ weight: 2
925
+ },
926
+ {
927
+ id: "proxy.compact",
928
+ length: 4,
929
+ width: 1.75,
930
+ height: 1.45,
931
+ weight: 2
932
+ },
933
+ {
934
+ id: "proxy.van",
935
+ length: 5.2,
936
+ width: 2,
937
+ height: 2.1,
938
+ weight: 1
939
+ }
940
+ ],
941
+ train: [{
942
+ id: "proxy.rail",
943
+ length: 27,
944
+ width: 2.65,
945
+ height: 3.6,
946
+ cars: 2,
947
+ colors: ["#e8e6de"]
948
+ }],
949
+ aircraft: [{
950
+ id: "proxy.airliner",
951
+ length: 38,
952
+ width: 36,
953
+ height: 12,
954
+ cruise: 110
955
+ }],
956
+ boat: [{
957
+ id: "proxy.boat",
958
+ length: 12,
959
+ width: 4,
960
+ height: 4,
961
+ cruise: 5,
962
+ waterway: "channel"
963
+ }, {
964
+ id: "proxy.ferry",
965
+ length: 32,
966
+ width: 9,
967
+ height: 8,
968
+ cruise: 8,
969
+ waterway: "ferry"
970
+ }],
971
+ pedestrian: [
972
+ {
973
+ preset: "human.adult",
974
+ weight: 7
975
+ },
976
+ {
977
+ preset: "human.elder",
978
+ weight: 1.5
979
+ },
980
+ {
981
+ preset: "human.child",
982
+ weight: 1.5
983
+ }
984
+ ]
985
+ };
986
+ /** Weighted pick from a list using a stateless draw. */
987
+ function pickWeighted(list, u) {
988
+ let total = 0;
989
+ for (const item of list) total += item.weight ?? 1;
990
+ if (total <= 0) return list[0];
991
+ let target = u * total;
992
+ for (const item of list) {
993
+ target -= item.weight ?? 1;
994
+ if (target < 0) return item;
995
+ }
996
+ return list[list.length - 1];
997
+ }
998
+ const outfit = (height, build, skin, hair, top, bottom, look, shoes) => ({
999
+ height,
1000
+ build,
1001
+ skin,
1002
+ hair,
1003
+ top,
1004
+ bottom,
1005
+ look,
1006
+ ...shoes ? { shoes } : {}
1007
+ });
1008
+ const [SKIN0, SKIN1, SKIN2, SKIN3, SKIN4, SKIN5, SKIN6] = [
1009
+ "#f1d3bd",
1010
+ "#e0b597",
1011
+ "#d3a080",
1012
+ "#c68b62",
1013
+ "#9b6645",
1014
+ "#6c4430",
1015
+ "#4a2e21"
1016
+ ];
1017
+ const BLACK = "#1f1a17", DARK = "#3b2a20", BROWN = "#6b4a2e", AUBURN = "#7a3b22", LIGHT = "#a8793f", BLOND = "#d8c29a";
1018
+ const NAVY = "#2f3b52", INK = "#1f1f24", WHITE = "#e8e6de", GREY = "#8a8f96", KHAKI = "#c9b48a", RED = "#6b2f2f", GREEN = "#2f5a3b", BLUE = "#3b6ea5", RUST = "#b0553a", PLUM = "#5a3b6b", MUSTARD = "#c9a227";
1019
+ const ADULTS = [
1020
+ outfit(1.78, .2, SKIN3, BLACK, WHITE, NAVY, {
1021
+ form: "masculine",
1022
+ hair: "cap"
1023
+ }),
1024
+ outfit(1.64, -.2, SKIN0, DARK, NAVY, INK, {
1025
+ form: "feminine",
1026
+ hair: "long"
1027
+ }),
1028
+ outfit(1.7, 0, SKIN5, BLACK, RED, GREY, {
1029
+ form: "masculine",
1030
+ hair: "buzz",
1031
+ facialHair: "stubble",
1032
+ sleeves: "short"
1033
+ }),
1034
+ outfit(1.84, .4, SKIN1, LIGHT, GREEN, KHAKI, {
1035
+ form: "masculine",
1036
+ hair: "crop",
1037
+ facialHair: "beard"
1038
+ }),
1039
+ outfit(1.6, -.3, SKIN4, BLACK, BLUE, INK, {
1040
+ form: "feminine",
1041
+ hair: "ponytail",
1042
+ legs: "skirt"
1043
+ }),
1044
+ outfit(1.68, 0, SKIN0, BLOND, INK, NAVY, {
1045
+ form: "feminine",
1046
+ hair: "bob"
1047
+ }, WHITE),
1048
+ outfit(1.75, .1, SKIN6, BLACK, RUST, INK, {
1049
+ form: "masculine",
1050
+ hair: "afro"
1051
+ }),
1052
+ outfit(1.8, -.1, SKIN1, BROWN, GREY, INK, {
1053
+ form: "masculine",
1054
+ hair: "cap",
1055
+ facialHair: "mustache"
1056
+ }),
1057
+ outfit(1.66, -.1, SKIN3, DARK, PLUM, GREY, {
1058
+ form: "feminine",
1059
+ hair: "bun",
1060
+ legs: "skirt"
1061
+ }),
1062
+ outfit(1.72, 0, SKIN0, AUBURN, WHITE, BLUE, {
1063
+ hair: "crop",
1064
+ sleeves: "short"
1065
+ }, INK),
1066
+ outfit(1.65, .2, SKIN5, BLACK, MUSTARD, NAVY, {
1067
+ form: "feminine",
1068
+ hair: "afro",
1069
+ legs: "skirt",
1070
+ sleeves: "short"
1071
+ }),
1072
+ outfit(1.76, .5, SKIN0, "#9a9a9a", NAVY, KHAKI, {
1073
+ form: "masculine",
1074
+ hair: "balding",
1075
+ facialHair: "beard"
1076
+ }),
1077
+ outfit(1.7, -.2, SKIN2, LIGHT, WHITE, GREEN, {
1078
+ form: "feminine",
1079
+ hair: "long"
1080
+ }),
1081
+ outfit(1.82, .3, SKIN4, BLACK, INK, NAVY, {
1082
+ form: "masculine",
1083
+ hair: "buzz",
1084
+ sleeves: "short"
1085
+ }),
1086
+ outfit(1.62, 0, SKIN0, BROWN, RUST, KHAKI, {
1087
+ form: "feminine",
1088
+ hair: "ponytail",
1089
+ legs: "shorts",
1090
+ sleeves: "short"
1091
+ }, WHITE),
1092
+ outfit(1.74, .1, SKIN3, DARK, BLUE, INK, {
1093
+ form: "masculine",
1094
+ hair: "cap",
1095
+ facialHair: "stubble"
1096
+ }),
1097
+ outfit(1.58, .1, SKIN4, BLACK, WHITE, RED, {
1098
+ form: "feminine",
1099
+ hair: "bob",
1100
+ legs: "skirt"
1101
+ }),
1102
+ outfit(1.69, -.3, SKIN2, DARK, PLUM, GREY, { hair: "crop" }),
1103
+ outfit(1.72, -.3, SKIN6, BLACK, GREEN, INK, {
1104
+ form: "feminine",
1105
+ hair: "bun"
1106
+ }),
1107
+ outfit(1.8, 0, SKIN1, BROWN, INK, NAVY, {
1108
+ form: "masculine",
1109
+ hair: "ponytail",
1110
+ facialHair: "beard"
1111
+ })
1112
+ ];
1113
+ const ELDERS = [
1114
+ outfit(1.72, .3, SKIN0, "#c8c8c4", GREY, NAVY, {
1115
+ form: "masculine",
1116
+ hair: "balding",
1117
+ facialHair: "mustache"
1118
+ }),
1119
+ outfit(1.6, .2, SKIN3, "#e6e6e2", BROWN, INK, {
1120
+ form: "feminine",
1121
+ hair: "bob",
1122
+ legs: "skirt"
1123
+ }),
1124
+ outfit(1.7, 0, SKIN5, "#b8b8b4", PLUM, GREY, {
1125
+ form: "masculine",
1126
+ hair: "cap"
1127
+ }),
1128
+ outfit(1.58, .1, SKIN0, "#d8d8d4", BLUE, NAVY, {
1129
+ form: "feminine",
1130
+ hair: "bun"
1131
+ }),
1132
+ outfit(1.68, .2, SKIN4, "#e6e6e2", GREEN, KHAKI, {
1133
+ form: "masculine",
1134
+ hair: "balding",
1135
+ facialHair: "beard"
1136
+ }),
1137
+ outfit(1.62, -.1, SKIN1, "#bdbdb8", RUST, INK, {
1138
+ form: "feminine",
1139
+ hair: "crop"
1140
+ })
1141
+ ];
1142
+ const CHILDREN = [
1143
+ outfit(1.2, 0, SKIN0, LIGHT, RUST, NAVY, { hair: "cap" }),
1144
+ outfit(1.35, -.2, SKIN4, BLACK, BLUE, INK, {
1145
+ hair: "ponytail",
1146
+ legs: "skirt"
1147
+ }),
1148
+ outfit(1.28, .1, SKIN1, DARK, GREEN, KHAKI, {
1149
+ hair: "crop",
1150
+ legs: "long",
1151
+ sleeves: "long"
1152
+ }),
1153
+ outfit(1.15, 0, SKIN5, BLACK, MUSTARD, NAVY, { hair: "afro" }),
1154
+ outfit(1.3, 0, SKIN0, BLOND, PLUM, GREY, {
1155
+ hair: "bob",
1156
+ legs: "skirt"
1157
+ }, WHITE)
1158
+ ];
1159
+ function outfitFor(preset, seed) {
1160
+ const list = preset === "human.child" ? CHILDREN : preset === "human.elder" ? ELDERS : ADULTS;
1161
+ return list[Math.floor(unit01(seed, 40) * list.length)];
1162
+ }
1163
+ /** Built-in templates: vehicles render through `ambient-vehicle`, pedestrians as figures. */
1164
+ const DEFAULT_TEMPLATES = {
1165
+ boat: (agent) => ({ renderable: {
1166
+ kind: "ambient-vehicle",
1167
+ ref: agent.type
1168
+ } }),
1169
+ car: (agent) => ({ renderable: {
1170
+ kind: "ambient-vehicle",
1171
+ ref: agent.type
1172
+ } }),
1173
+ train: (agent) => ({ renderable: {
1174
+ kind: "ambient-vehicle",
1175
+ ref: agent.type
1176
+ } }),
1177
+ aircraft: (agent) => ({ renderable: {
1178
+ kind: "ambient-vehicle",
1179
+ ref: agent.type
1180
+ } }),
1181
+ pedestrian: (agent) => {
1182
+ const dress = outfitFor(agent.type, agent.seed);
1183
+ return {
1184
+ figure: {
1185
+ preset: agent.type,
1186
+ height: dress.height,
1187
+ build: dress.build,
1188
+ palette: {
1189
+ skin: dress.skin,
1190
+ hair: dress.hair,
1191
+ top: dress.top,
1192
+ bottom: dress.bottom,
1193
+ ...dress.shoes ? { shoes: dress.shoes } : {}
1194
+ },
1195
+ features: { ...dress.look },
1196
+ facing: "manual",
1197
+ speedSource: "transform"
1198
+ },
1199
+ renderable: {
1200
+ kind: "figure",
1201
+ ref: "procedural"
1202
+ }
1203
+ };
1204
+ }
1205
+ };
1206
+ //#endregion
1207
+ export { unit01 as A, clipPolyline as C, fnv1a as D, fmix32 as E, q4 as O, classifyFeature as S, tilePois as T, TRANSPORT_NETWORK_FORMAT as _, AMBIENT_ENTITY as a, wayDraft as b, AmbientObserver as c, AMBIENT_AGENT_COMPONENT as d, AMBIENT_OBSERVER_COMPONENT as f, TRANSPORT_NETWORK_EXAMPLE as g, registerAmbientSchema as h, pickWeighted as i, shortHash as k, AmbientRole as l, AMBIENT_STATE_COMPONENT as m, DEFAULT_TEMPLATES as n, AMBIENT_KINDS as o, AMBIENT_ROLE_COMPONENT as p, DEFAULT_TYPES as r, AmbientAgent as s, CAR_COLORS as t, AmbientState as u, documentPois as v, featureDrafts as w, KIND_SPEED as x, documentRunways as y };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bendyline/molen-ambient",
3
- "version": "0.0.14",
3
+ "version": "0.0.17",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -41,14 +41,14 @@
41
41
  "dist"
42
42
  ],
43
43
  "dependencies": {
44
- "@bendyline/molen-figures": "0.0.12",
45
- "@bendyline/molen-kernel": "0.0.10",
46
- "@bendyline/molen-schema": "0.0.10",
47
- "@bendyline/molen-terrain": "0.0.14",
44
+ "@bendyline/molen-figures": "0.0.15",
45
+ "@bendyline/molen-kernel": "0.0.11",
46
+ "@bendyline/molen-schema": "0.0.11",
47
+ "@bendyline/molen-terrain": "0.0.16",
48
48
  "zod": "^4.6.5"
49
49
  },
50
50
  "peerDependencies": {
51
- "@bendyline/molen-client": "0.0.12",
51
+ "@bendyline/molen-client": "0.0.15",
52
52
  "three": ">=0.184.0 <0.187.0"
53
53
  },
54
54
  "peerDependenciesMeta": {
@@ -60,7 +60,7 @@
60
60
  }
61
61
  },
62
62
  "devDependencies": {
63
- "@bendyline/molen-client": "0.0.12",
63
+ "@bendyline/molen-client": "0.0.15",
64
64
  "@biomejs/biome": "^2.5.0",
65
65
  "@types/three": "0.184.0",
66
66
  "fast-check": "^3.23.2",