@adatechnology/game-urban 0.3.13 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -10,6 +10,34 @@ function createLightAllowance(count) {
10
10
  }
11
11
  var createLightBudget = createLightAllowance;
12
12
 
13
+ // src/lamps.ts
14
+ var UINT32 = 2 ** 32;
15
+ var PHASE_SALT = 1597463007;
16
+ var SEED_BITS = new DataView(new ArrayBuffer(8));
17
+ var hashLamp = (seed, x, z) => {
18
+ SEED_BITS.setFloat64(0, seed);
19
+ let h = 2166136261;
20
+ for (const value of [SEED_BITS.getUint32(0), SEED_BITS.getUint32(4), Math.round(x * 100), Math.round(z * 100)]) {
21
+ h ^= value & 65535;
22
+ h = Math.imul(h, 16777619);
23
+ h ^= value >>> 16 & 65535;
24
+ h = Math.imul(h, 16777619);
25
+ }
26
+ return (h >>> 0) / UINT32;
27
+ };
28
+ var flickerPhaseOf = (x, z) => hashLamp(PHASE_SALT, x, z) * 2 * Math.PI;
29
+ function assertLampShares(shares) {
30
+ const { lit, flicker } = shares;
31
+ if (!(lit >= 0 && lit <= 1)) throw new RangeError(`lamp shares: lit must be in [0, 1], got ${lit}`);
32
+ if (!(flicker >= 0 && flicker <= lit)) throw new RangeError(`lamp shares: flicker must be in [0, lit], got ${flicker}`);
33
+ }
34
+ function lampStateOf({ seed, x, z, shares }) {
35
+ assertLampShares(shares);
36
+ const roll = hashLamp(seed, x, z);
37
+ if (roll >= shares.lit) return { on: false, flicker: false };
38
+ return { on: true, flicker: roll >= shares.lit - shares.flicker };
39
+ }
40
+
13
41
  // src/scene.ts
14
42
  import { boxRadius } from "@adatechnology/game-floorplan";
15
43
  var ROOT = -1;
@@ -28,6 +56,7 @@ function createUrbanBuilder(random, options = {}) {
28
56
  const box = (x, y, z, width, height, depth, color, o = {}) => {
29
57
  const part = { shape: "box", parent: o.parent ?? ROOT, x, y: y + height / 2, z, width, height, depth, color, emissive: o.emissive ?? false, ry: o.ry ?? 0, rz: o.rz ?? 0, radius: boxRadius(width, height, depth) };
30
58
  if (o.style) part.style = o.style;
59
+ if (o.flickerPhase !== void 0) part.flickerPhase = o.flickerPhase;
31
60
  parts.push(part);
32
61
  };
33
62
  const cylinder = (x, y, z, radiusTop, radiusBottom, height, color, parent = ROOT, segments = DEFAULT_CYLINDER_SEGMENTS, style) => {
@@ -92,52 +121,93 @@ function createUrbanBuilder(random, options = {}) {
92
121
  return { random, pick, lights, parts, items, windows, group, box, cylinder, cone, prism, plane, vault, terrain, crown, line, label, light, spot, place, item, fence, add, scene };
93
122
  }
94
123
 
124
+ // src/networks/slots.ts
125
+ var DEFAULT_POLE_SLOTS = {
126
+ height: 11,
127
+ embedment: 1.7,
128
+ bands: { mv: [9, 9.25], transformer: [7.7, 8.7], lv: [7.1, 7.4], lighting: [6.5, 6.8], telecom: [5.7, 6.1] }
129
+ };
130
+
131
+ // src/pole-distribution.ts
132
+ var DISTRIBUTION = {
133
+ mast: { radiusTop: 0.12, radiusBottom: 0.22, height: 9.3, color: 9079428, segments: 4 },
134
+ arm: { reach: 1, y: 6.7, width: 2, height: 0.1, depth: 0.1, color: 6974054 },
135
+ lamp: { reach: 2, y: 6.55, width: 0.5, height: 0.16, depth: 0.3 },
136
+ lens: { lit: 16752704, unlit: 3815994 },
137
+ light: { color: 16751168, intensity: 1.3, distance: 20, y: 6.4 },
138
+ transformer: { offset: 0.45, y: 7.7, radius: 0.32, height: 1, color: 5922402, segments: 8 },
139
+ slots: DEFAULT_POLE_SLOTS,
140
+ crossarm: { reach: 0.3, width: 2, height: 0.1, depth: 0.09, color: 9211014 },
141
+ insulators: { at: [-0.55, 0.35, 1.15], radius: 0.04, color: 14210248 },
142
+ bushings: { spread: 0.16, radius: 0.025, height: 0.14, color: 11055288 },
143
+ lowVoltage: { out: 0.2, color: 6185060 },
144
+ telecom: { out: 0.18, color: 6185060 },
145
+ guy: { reach: 3, color: 8027262 }
146
+ };
147
+
95
148
  // src/poles.ts
96
149
  var on = (o) => o.on ?? true;
150
+ var lensOf = (o, parent) => ({
151
+ emissive: on(o),
152
+ ...parent === void 0 ? {} : { parent },
153
+ ...on(o) && o.flicker ? { flickerPhase: flickerPhaseOf(o.x, o.z) } : {}
154
+ });
155
+ var along = (o, reach) => ({ x: o.x + reach * Math.cos(o.ry ?? 0), z: o.z - reach * Math.sin(o.ry ?? 0) });
97
156
  function drawMast(b, mast, x, z, parent = -1) {
98
157
  b.cylinder(x, 0, z, mast.radiusTop, mast.radiusBottom, mast.height, mast.color, parent, mast.segments, mast.style);
99
158
  }
100
159
  function spendLight(b, light, x, z, o) {
101
- if (on(o) && b.lights.take()) b.light({ x: x + (light.reach ?? 0), y: light.y, z: z + (light.z ?? 0), color: light.color, intensity: light.intensity, distance: light.distance, flicker: o.flicker ?? false });
160
+ const ry = o.ry ?? 0;
161
+ const [reach, side] = [light.reach ?? 0, light.z ?? 0];
162
+ if (on(o) && b.lights.take()) b.light({ x: x + reach * Math.cos(ry) + side * Math.sin(ry), y: light.y, z: z - reach * Math.sin(ry) + side * Math.cos(ry), color: light.color, intensity: light.intensity, distance: light.distance, flicker: o.flicker ?? false, ...o.flicker ? { flickerPhase: flickerPhaseOf(o.x, o.z) } : {} });
102
163
  }
103
164
  var doubleArm = (b, spec, o) => {
104
165
  const group = b.group(o.x, 0, o.z, o.ry ?? 0);
105
166
  drawMast(b, spec.mast, 0, 0, group);
106
167
  for (const side of [-1, 1]) {
107
168
  b.box(side * spec.arm.reach, spec.arm.y, 0, spec.arm.width, spec.arm.height, spec.arm.depth, spec.arm.color, { parent: group });
108
- b.box(side * spec.lamp.reach, spec.lamp.y, 0, spec.lamp.width, spec.lamp.height, spec.lamp.depth, on(o) ? spec.lens.lit : spec.lens.unlit, { parent: group, emissive: on(o) });
169
+ b.box(side * spec.lamp.reach, spec.lamp.y, 0, spec.lamp.width, spec.lamp.height, spec.lamp.depth, on(o) ? spec.lens.lit : spec.lens.unlit, lensOf(o, group));
109
170
  }
110
- spendLight(b, spec.light, o.x, o.z, o);
171
+ spendLight(b, spec.light, o.x, o.z, { ...o, ry: 0 });
111
172
  };
112
173
  var singleArm = (b, spec, o) => {
113
174
  const dir = o.dir ?? 1;
175
+ const ry = o.ry ?? 0;
176
+ const arm = along(o, dir * spec.arm.reach);
177
+ const lamp = along(o, dir * spec.lamp.reach);
178
+ const drum = along(o, -dir * spec.transformer.offset);
114
179
  drawMast(b, spec.mast, o.x, o.z);
115
- b.box(o.x + dir * spec.arm.reach, spec.arm.y, o.z, spec.arm.width, spec.arm.height, spec.arm.depth, spec.arm.color);
116
- b.box(o.x + dir * spec.lamp.reach, spec.lamp.y, o.z, spec.lamp.width, spec.lamp.height, spec.lamp.depth, on(o) ? spec.lens.lit : spec.lens.unlit, { emissive: on(o) });
117
- if (o.transformer) b.cylinder(o.x - dir * spec.transformer.offset, spec.transformer.y, o.z, spec.transformer.radius, spec.transformer.radius, spec.transformer.height, spec.transformer.color, -1, spec.transformer.segments);
180
+ b.box(arm.x, spec.arm.y, arm.z, spec.arm.width, spec.arm.height, spec.arm.depth, spec.arm.color, { ry });
181
+ b.box(lamp.x, spec.lamp.y, lamp.z, spec.lamp.width, spec.lamp.height, spec.lamp.depth, on(o) ? spec.lens.lit : spec.lens.unlit, { ...lensOf(o), ry });
182
+ if (o.transformer) b.cylinder(drum.x, spec.transformer.y, drum.z, spec.transformer.radius, spec.transformer.radius, spec.transformer.height, spec.transformer.color, -1, spec.transformer.segments);
118
183
  spendLight(b, { ...spec.light, reach: dir * (spec.light.reach ?? spec.lamp.reach) }, o.x, o.z, o);
119
184
  };
120
185
  var lantern = (b, spec, o) => {
186
+ const lamp = along(o, spec.lamp.reach);
121
187
  drawMast(b, spec.mast, o.x, o.z);
122
- b.box(o.x + spec.lamp.reach, spec.lamp.y, o.z, spec.lamp.width, spec.lamp.height, spec.lamp.depth, on(o) ? spec.lens.lit : spec.lens.unlit, { emissive: on(o) });
188
+ b.box(lamp.x, spec.lamp.y, lamp.z, spec.lamp.width, spec.lamp.height, spec.lamp.depth, on(o) ? spec.lens.lit : spec.lens.unlit, { ...lensOf(o), ry: o.ry ?? 0 });
123
189
  b.cone(o.x, spec.cap.y, o.z, spec.cap.radius, spec.cap.height, spec.cap.color, -1, spec.cap.segments);
124
190
  spendLight(b, spec.light, o.x, o.z, o);
125
191
  };
126
192
  var crossarm = (b, spec, o) => {
193
+ const ry = o.ry ?? 0;
194
+ const arm = along(o, spec.arm.reach);
195
+ const lamp = along(o, spec.lamp.reach);
127
196
  drawMast(b, spec.mast, o.x, o.z);
128
- b.box(o.x + spec.arm.reach, spec.arm.y, o.z, spec.arm.width, spec.arm.height, spec.arm.depth, spec.arm.color);
129
- b.box(o.x + spec.lamp.reach, spec.lamp.y, o.z, spec.lamp.width, spec.lamp.height, spec.lamp.depth, on(o) ? spec.lens.lit : spec.lens.unlit, { emissive: on(o) });
197
+ b.box(arm.x, spec.arm.y, arm.z, spec.arm.width, spec.arm.height, spec.arm.depth, spec.arm.color, { ry });
198
+ b.box(lamp.x, spec.lamp.y, lamp.z, spec.lamp.width, spec.lamp.height, spec.lamp.depth, on(o) ? spec.lens.lit : spec.lens.unlit, { ...lensOf(o), ry });
130
199
  spendLight(b, { ...spec.light, reach: spec.light.reach ?? spec.lamp.reach }, o.x, o.z, o);
131
200
  };
132
201
  var floodTower = (b, spec, o) => {
133
202
  for (const a of [-1, 1]) for (const c of [-1, 1]) b.box(o.x + a * spec.legs.offset, 0, o.z + c * spec.legs.offset, spec.legs.size, spec.legs.height, spec.legs.size, spec.legs.color);
134
203
  b.box(o.x + spec.deck.reach, spec.deck.y, o.z, spec.deck.width, spec.deck.height, spec.deck.depth, spec.deck.color);
135
204
  for (let index = 0; index < spec.heads.count; index++) {
136
- b.box(o.x + spec.heads.start + index * spec.heads.spacing, spec.heads.y, o.z + spec.heads.z, spec.heads.width, spec.heads.height, spec.heads.depth, on(o) ? spec.lens.lit : spec.lens.unlit, { emissive: on(o) });
205
+ b.box(o.x + spec.heads.start + index * spec.heads.spacing, spec.heads.y, o.z + spec.heads.z, spec.heads.width, spec.heads.height, spec.heads.depth, on(o) ? spec.lens.lit : spec.lens.unlit, lensOf(o));
137
206
  }
138
207
  spendLight(b, spec.light, o.x, o.z, o);
139
208
  };
140
- var POLE_SHAPES = { double_arm: doubleArm, single_arm: singleArm, lantern, crossarm, flood_tower: floodTower };
209
+ var distribution = (b, spec, o) => singleArm(b, spec, o);
210
+ var POLE_SHAPES = { double_arm: doubleArm, single_arm: singleArm, lantern, crossarm, flood_tower: floodTower, distribution };
141
211
  var LED = {
142
212
  mast: { radiusTop: 0.1, radiusBottom: 0.14, height: 9, color: 5593438, style: { metalness: 0.6, roughness: 0.5 } },
143
213
  arm: { reach: 0.8, y: 8.8, width: 1.6, height: 0.1, depth: 0.12, color: 5593438 },
@@ -179,7 +249,8 @@ var DEFAULT_POLES = [
179
249
  { id: "concrete_arm", shape: "single_arm", spec: CONCRETE },
180
250
  { id: "cast_iron", shape: "lantern", spec: IRON },
181
251
  { id: "wood_crossarm", shape: "crossarm", spec: WOOD },
182
- { id: "flood_tower", shape: "flood_tower", spec: FLOOD }
252
+ { id: "flood_tower", shape: "flood_tower", spec: FLOOD },
253
+ { id: "concrete_distribution", shape: "distribution", spec: DISTRIBUTION }
183
254
  ];
184
255
 
185
256
  // src/streets.ts
@@ -190,9 +261,9 @@ var DEFAULT_GROUND_COLOR = 1382426;
190
261
  var DEFAULT_GROUND_SIZE = 600;
191
262
  var roadStyle = (o) => ({ ...DEFAULT_ROAD_STYLE, ...o?.road, dash: { ...DEFAULT_ROAD_STYLE.dash, ...o?.road?.dash } });
192
263
  var walkStyle = (o) => ({ ...DEFAULT_WALK_STYLE, ...o?.walk });
193
- function drawRoad(b, x, z, w, l, along, options) {
264
+ function drawRoad(b, x, z, w, l, along2, options) {
194
265
  const style = roadStyle(options);
195
- const alongX = along === "x";
266
+ const alongX = along2 === "x";
196
267
  b.plane(x, ROAD_LEVEL, z, alongX ? l : w, alongX ? w : l, style.color);
197
268
  const { dash } = style;
198
269
  for (let u = -l / 2 + dash.first; u < l / 2; u += dash.every) {
@@ -406,12 +477,12 @@ function drawWatchtower(b, x, z, searchlight = false, c) {
406
477
  for (const side of [-1.5, 1.5]) b.box(x + side, 8.2, z, 0.15, 1.6, 0.15, palette.deck);
407
478
  if (searchlight) b.spot({ x, y: 9, z, color: 15134463, intensity: 2.4, distance: 70, decay: 1.2, angle: 0.35, penumbra: 0.5, target: { x: x + 20, y: 0, z: z + 20 }, sweep: { speed: 0.5, radius: 25 } });
408
479
  }
409
- function drawSandbags(b, x, z, length, along, rows = 3, c) {
480
+ function drawSandbags(b, x, z, length, along2, rows = 3, c) {
410
481
  const color = colors(c).sandbags;
411
482
  for (let r = 0; r < rows; r++) {
412
483
  for (let i = 0; i < length / SANDBAG_LENGTH; i++) {
413
484
  const u = -length / 2 + 0.28 + i * SANDBAG_LENGTH + r % 2 * 0.28;
414
- b.item(along === "x" ? x + u : x, r * 0.21, along === "x" ? z : z + u, along === "x" ? 0.55 : 0.32, 0.22, along === "x" ? 0.32 : 0.55, color);
485
+ b.item(along2 === "x" ? x + u : x, r * 0.21, along2 === "x" ? z : z + u, along2 === "x" ? 0.55 : 0.32, 0.22, along2 === "x" ? 0.32 : 0.55, color);
415
486
  }
416
487
  }
417
488
  }
@@ -1424,7 +1495,7 @@ function createUrban(options = {}) {
1424
1495
  const core = {
1425
1496
  poles,
1426
1497
  streets: {
1427
- road: (b, x, z, w, l, along) => drawRoad(b, x, z, w, l, along, options.streets),
1498
+ road: (b, x, z, w, l, along2) => drawRoad(b, x, z, w, l, along2, options.streets),
1428
1499
  walk: (b, x, z, w, d) => drawWalk(b, x, z, w, d, options.streets),
1429
1500
  ground: drawGround,
1430
1501
  grid: (b, g) => drawStreetGrid(b, g.block, g.count, g.width, options.streets)
@@ -1441,7 +1512,7 @@ function createUrban(options = {}) {
1441
1512
  tree: (b, x, z, scale) => drawTree(b, x, z, scale, propColors),
1442
1513
  container: drawContainer,
1443
1514
  watchtower: (b, x, z, searchlight) => drawWatchtower(b, x, z, searchlight, propColors),
1444
- sandbags: (b, x, z, length, along, rows) => drawSandbags(b, x, z, length, along, rows, propColors),
1515
+ sandbags: (b, x, z, length, along2, rows) => drawSandbags(b, x, z, length, along2, rows, propColors),
1445
1516
  label: (b, x, z, title, subtitle) => drawStripLabel(b, x, z, title, subtitle, propColors),
1446
1517
  wires: (b, points, y, color) => drawWires(b, points, y, color, propColors)
1447
1518
  }
@@ -1575,6 +1646,7 @@ export {
1575
1646
  TOWER_DEFAULTS,
1576
1647
  TOWER_PATTERNS,
1577
1648
  TOWER_SAMPLE_CELLS,
1649
+ assertLampShares,
1578
1650
  clamp,
1579
1651
  composePattern,
1580
1652
  createCities,
@@ -1605,9 +1677,11 @@ export {
1605
1677
  drawWatchtower,
1606
1678
  drawWires,
1607
1679
  fillBlock,
1680
+ flickerPhaseOf,
1608
1681
  hash2,
1609
1682
  insideEllipse,
1610
1683
  insideRect,
1684
+ lampStateOf,
1611
1685
  lerp2 as lerp,
1612
1686
  patternMarker,
1613
1687
  pickTowerPreset,