@adatechnology/game-terrain 0.2.3 → 0.3.1

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
@@ -48,17 +48,18 @@ var TERRAIN_MATERIALS = [
48
48
  {
49
49
  id: "gravel",
50
50
  debris: "chips",
51
- rimShare: 0.3,
51
+ rimShare: 0.15,
52
52
  reposeDegrees: 38,
53
53
  color: [0.27, 0.25, 0.22],
54
- pattern: "stone",
54
+ pattern: "gravel",
55
55
  hits: {
56
56
  ballistic: { specificEnergy: 8e6, aspect: 0.4 },
57
57
  blunt: { specificEnergy: 8e6, aspect: 0.2, thresholdJoules: 200 },
58
58
  slash: { specificEnergy: 3e7, aspect: 0.3, thresholdJoules: 400 },
59
59
  explosive: { specificEnergy: 1e7, aspect: 0.35 }
60
60
  },
61
- fire: { maxScorch: 0.3, scorchPerSecond: 0.06 }
61
+ fire: { maxScorch: 0.3, scorchPerSecond: 0.06 },
62
+ scatters: true
62
63
  },
63
64
  {
64
65
  id: "asphalt",
@@ -142,8 +143,85 @@ var TERRAIN_MATERIALS = [
142
143
  explosive: { specificEnergy: 4e9, aspect: 0.14 }
143
144
  },
144
145
  fire: { maxScorch: 0.4, scorchPerSecond: 0.06 }
146
+ },
147
+ {
148
+ id: "mud",
149
+ debris: "clods",
150
+ rimShare: 0.15,
151
+ reposeDegrees: 18,
152
+ color: [0.11, 0.075, 0.04],
153
+ pattern: "mud",
154
+ hits: {
155
+ ballistic: { specificEnergy: 12e5, aspect: 0.7 },
156
+ blunt: { specificEnergy: 3e5, aspect: 0.3, thresholdJoules: 15 },
157
+ slash: { specificEnergy: 5e4, aspect: 0.5 },
158
+ explosive: { specificEnergy: 15e5, aspect: 0.4 }
159
+ },
160
+ fire: { maxScorch: 0.15, scorchPerSecond: 0.03 },
161
+ footing: { sink: 0.07, drag: 0.6, prints: true },
162
+ splash: "mud",
163
+ splats: true
164
+ },
165
+ {
166
+ id: "clay",
167
+ debris: "chips",
168
+ color: [0.47, 0.27, 0.14],
169
+ pattern: "clay",
170
+ hits: {
171
+ ballistic: { specificEnergy: 1e7, aspect: 0.2, minRadius: 0.03 },
172
+ blunt: { specificEnergy: 15e6, aspect: 0.15, thresholdJoules: 100 },
173
+ slash: { specificEnergy: 6e7, aspect: 0.3, thresholdJoules: 30 },
174
+ explosive: { specificEnergy: 8e6, aspect: 0.25 }
175
+ },
176
+ fire: { maxScorch: 0.35, scorchPerSecond: 0.05 },
177
+ cracks: 4
178
+ },
179
+ {
180
+ id: "snow",
181
+ debris: "dust",
182
+ rimShare: 0.4,
183
+ reposeDegrees: 38,
184
+ color: [0.82, 0.85, 0.88],
185
+ pattern: "snow",
186
+ hits: {
187
+ ballistic: { specificEnergy: 15e5, aspect: 1 },
188
+ blunt: { specificEnergy: 3e5, aspect: 0.3, thresholdJoules: 10 },
189
+ slash: { specificEnergy: 5e4, aspect: 0.6 },
190
+ explosive: { specificEnergy: 15e5, aspect: 0.5 }
191
+ },
192
+ fire: { maxScorch: 0, scorchPerSecond: 0, burnsAway: 0.015 },
193
+ footing: { sink: 0.08, drag: 0.7, prints: true }
194
+ },
195
+ {
196
+ id: "cobble",
197
+ debris: "blocks",
198
+ color: [0.2, 0.19, 0.18],
199
+ pattern: "cobble",
200
+ hits: {
201
+ ballistic: { specificEnergy: 25e7, aspect: 0.12, minRadius: 0.015 },
202
+ blunt: { specificEnergy: 5e7, aspect: 0.12, thresholdJoules: 250 },
203
+ slash: { specificEnergy: 2e9, aspect: 0.2, thresholdJoules: 1500 },
204
+ explosive: { specificEnergy: 15e6, aspect: 0.2 }
205
+ },
206
+ fire: { maxScorch: 0.4, scorchPerSecond: 0.06 },
207
+ units: { length: 0.3, width: 0.2, looseJoules: 4e3 }
208
+ },
209
+ {
210
+ id: "paver",
211
+ debris: "blocks",
212
+ color: [0.36, 0.33, 0.3],
213
+ pattern: "paver",
214
+ hits: {
215
+ ballistic: { specificEnergy: 15e7, aspect: 0.14, minRadius: 0.018 },
216
+ blunt: { specificEnergy: 4e7, aspect: 0.12, thresholdJoules: 150 },
217
+ slash: { specificEnergy: 1e9, aspect: 0.2, thresholdJoules: 800 },
218
+ explosive: { specificEnergy: 12e6, aspect: 0.2 }
219
+ },
220
+ fire: { maxScorch: 0.45, scorchPerSecond: 0.08 },
221
+ units: { length: 0.2, width: 0.2, looseJoules: 2500 }
145
222
  }
146
223
  ];
224
+ var WATER_BLOW_SHARE = { ballistic: 0.5, slash: 0.3, blunt: 0.5, explosive: 1 };
147
225
  var TERRAIN_PROFILES = [
148
226
  { id: "lawn", strata: [{ material: "turf", thickness: 0.05 }, { material: "soil", thickness: 1.5 }, { material: "stone", thickness: 0.5 }] },
149
227
  { id: "dirt", strata: [{ material: "soil", thickness: 1.8 }, { material: "stone", thickness: 0.5 }] },
@@ -153,7 +231,14 @@ var TERRAIN_PROFILES = [
153
231
  { id: "suspended_slab", drop: 3, strata: [{ material: "concrete", thickness: 0.12, reinforcement: { material: "steel", cover: 0.03, spacing: 0.15, resistance: 4 } }] },
154
232
  { id: "tile_floor", strata: [{ material: "ceramic", thickness: 8e-3 }, { material: "mortar", thickness: 0.03 }, { material: "concrete", thickness: 0.1, reinforcement: { material: "steel", cover: 0.03, spacing: 0.15, resistance: 4 } }] },
155
233
  { id: "wood_floor", strata: [{ material: "wood_hard", thickness: 0.022 }, { material: "mortar", thickness: 0.03 }, { material: "concrete", thickness: 0.1, reinforcement: { material: "steel", cover: 0.03, spacing: 0.15, resistance: 4 } }] },
156
- { id: "stone_paving", strata: [{ material: "stone", thickness: 0.08 }, { material: "gravel", thickness: 0.2 }, { material: "soil", thickness: 1.2 }] }
234
+ { id: "stone_paving", strata: [{ material: "stone", thickness: 0.08 }, { material: "gravel", thickness: 0.2 }, { material: "soil", thickness: 1.2 }] },
235
+ { id: "mud", strata: [{ material: "mud", thickness: 0.5 }, { material: "soil", thickness: 1.5 }, { material: "stone", thickness: 0.5 }] },
236
+ { id: "gravel_bed", strata: [{ material: "gravel", thickness: 0.3 }, { material: "soil", thickness: 1.5 }, { material: "stone", thickness: 0.5 }] },
237
+ { id: "dry_clay", strata: [{ material: "clay", thickness: 0.35 }, { material: "soil", thickness: 1.5 }, { material: "stone", thickness: 0.5 }] },
238
+ { id: "snow", strata: [{ material: "snow", thickness: 0.3 }, { material: "soil", thickness: 1.5 }, { material: "stone", thickness: 0.5 }] },
239
+ { id: "cobblestone", strata: [{ material: "cobble", thickness: 0.12 }, { material: "sand", thickness: 0.06 }, { material: "gravel", thickness: 0.2 }, { material: "soil", thickness: 1.2 }] },
240
+ { id: "paver_floor", strata: [{ material: "paver", thickness: 0.08 }, { material: "sand", thickness: 0.05 }, { material: "gravel", thickness: 0.15 }, { material: "soil", thickness: 1.2 }] },
241
+ { id: "shallow_water", water: { liquid: "water", depth: 0.3, drag: 0.65, blowShare: WATER_BLOW_SHARE }, strata: [{ material: "mud", thickness: 0.15 }, { material: "soil", thickness: 1.5 }, { material: "stone", thickness: 0.5 }] }
157
242
  ];
158
243
  var DEFAULT_BAR_YIELD_JOULES = 24e5;
159
244
  var DEFAULT_PROFILE = "concrete_slab";
@@ -438,7 +523,8 @@ var DEFAULT_TERRAIN_BUDGET = {
438
523
  maxDebrisPerBlow: 36,
439
524
  maxSlideCellsPerFrame: 2500,
440
525
  maxBurningCells: 300,
441
- maxCracks: 120
526
+ maxCracks: 120,
527
+ maxDecals: 300
442
528
  };
443
529
  var GRAVITY = 9.81;
444
530
  var FINE_CELLS = 1.5;
@@ -450,14 +536,20 @@ var RIM_REACH = 1.45;
450
536
  var FLAME_MINIMUM = 200;
451
537
  var FLAME_REACH = 0.6;
452
538
  var BLAST_HEIGHT_REACH = 0.35;
539
+ var BLAST_SOOT_REACH = 1.2;
540
+ var BLAST_SOOT = 0.9;
453
541
  var THROW_SHARE = 0.02;
454
542
  var FIRE_CELL = 0.5;
455
543
  var FIRE_SECONDS = 7;
456
544
  var FIRE_SPREAD_PER_SECOND = 0.35;
457
545
  var SLIDE_SHARE = 0.4;
458
546
  var SLIDE_EPSILON = 2e-3;
459
- var PIECE_RADIUS = { clods: 0.045, chips: 0.022, shards: 0.028, splinters: 0.02, dust: 0.012 };
547
+ var PIECE_RADIUS = { clods: 0.045, chips: 0.022, shards: 0.028, splinters: 0.02, dust: 0.012, blocks: 0.08 };
460
548
  var MAX_PIECE_SPEED = { explosive: 14, ballistic: 5, blunt: 2.5, slash: 1.5 };
549
+ var BANK_CELLS = 6;
550
+ var CRACK_MIN_RADIUS = 0.18;
551
+ var RIPPLE_SECONDS = 1.6;
552
+ var WATER_MINIMUM = 0.02;
461
553
  var clampIndex = (value, max) => Math.max(0, Math.min(max, value));
462
554
  var sparse = (array) => {
463
555
  const entries = [];
@@ -512,10 +604,12 @@ function createTerrain(options) {
512
604
  }
513
605
  const indexOfProfile = new Map(profileList.map((profile, index) => [profile.id, index]));
514
606
  const maxDepthOf = profileList.map(totalThickness);
515
- const base = new Float32Array(vertexCount);
607
+ const surface = new Float32Array(vertexCount);
516
608
  if (options.baseHeight) {
517
- for (let row = 0; row <= rows; row++) for (let column = 0; column <= columns; column++) base[row * stride + column] = options.baseHeight(x0 + column * cellSize, z0 + row * cellSize);
609
+ for (let row = 0; row <= rows; row++) for (let column = 0; column <= columns; column++) surface[row * stride + column] = options.baseHeight(x0 + column * cellSize, z0 + row * cellSize);
518
610
  }
611
+ const base = Float32Array.from(surface);
612
+ const hasWater = profileList.some((profile) => profile.water);
519
613
  const dug = new Float32Array(vertexCount);
520
614
  const loose = new Float32Array(vertexCount);
521
615
  const scorch = new Float32Array(vertexCount);
@@ -526,6 +620,10 @@ function createTerrain(options) {
526
620
  const dirty = /* @__PURE__ */ new Set();
527
621
  const marks = createMarkStore(budget);
528
622
  const cracks = createCrackStore();
623
+ const decalList = [];
624
+ let decalSerial = 0;
625
+ let decalsVersion = 0;
626
+ const unitDamage = /* @__PURE__ */ new Map();
529
627
  const debrisStore = createDebrisStore(() => budget.maxDebris);
530
628
  const sliding = /* @__PURE__ */ new Set();
531
629
  const burning = /* @__PURE__ */ new Map();
@@ -562,16 +660,18 @@ function createTerrain(options) {
562
660
  const row1 = clampIndex(Math.round((zz1 - z0) / cellSize), rows);
563
661
  for (let row = row0; row <= row1; row++) profileIndex.fill(index, row * stride + column0, row * stride + column1 + 1);
564
662
  }
663
+ if (hasWater) layBeds();
565
664
  for (let chunkZ = 0; chunkZ < chunksZ; chunkZ++) {
566
665
  for (let chunkX = 0; chunkX < chunksX; chunkX++) {
567
666
  const chunk2 = chunkZ * chunksX + chunkX;
568
667
  const first = profileIndex[chunkZ * chunkCells * stride + chunkX * chunkCells];
668
+ const firstDrop = surface[chunkZ * chunkCells * stride + chunkX * chunkCells] - base[chunkZ * chunkCells * stride + chunkX * chunkCells];
569
669
  let isMixed = 0;
570
670
  const lastRow = Math.min(rows, (chunkZ + 1) * chunkCells);
571
671
  const lastColumn = Math.min(columns, (chunkX + 1) * chunkCells);
572
672
  for (let row = chunkZ * chunkCells; row <= lastRow && !isMixed; row++) {
573
673
  for (let column = chunkX * chunkCells; column <= lastColumn; column++) {
574
- if (profileIndex[row * stride + column] !== first) {
674
+ if (profileIndex[row * stride + column] !== first || surface[row * stride + column] - base[row * stride + column] !== firstDrop) {
575
675
  isMixed = 1;
576
676
  break;
577
677
  }
@@ -582,11 +682,39 @@ function createTerrain(options) {
582
682
  }
583
683
  }
584
684
  }
685
+ function layBeds() {
686
+ const steps = new Uint8Array(vertexCount);
687
+ for (let index = 0; index < vertexCount; index++) steps[index] = profileList[profileIndex[index]].water ? BANK_CELLS : 0;
688
+ for (let pass = 0; pass < BANK_CELLS; pass++) {
689
+ for (let row = 0; row <= rows; row++) {
690
+ for (let column = 0; column <= columns; column++) {
691
+ const index = row * stride + column;
692
+ if (steps[index] === 0) continue;
693
+ const lowest = Math.min(column > 0 ? steps[index - 1] : BANK_CELLS, column < columns ? steps[index + 1] : BANK_CELLS, row > 0 ? steps[index - stride] : BANK_CELLS, row < rows ? steps[index + stride] : BANK_CELLS);
694
+ steps[index] = Math.min(steps[index], lowest + 1);
695
+ }
696
+ }
697
+ }
698
+ for (let index = 0; index < vertexCount; index++) base[index] = surface[index] - (profileList[profileIndex[index]].water?.depth ?? 0) * (steps[index] / BANK_CELLS);
699
+ }
585
700
  const vertexHeight = (column, row) => {
586
701
  const index = row * stride + column;
587
702
  return base[index] - dug[index] + loose[index];
588
703
  };
589
704
  const contains = (x, z) => x >= x0 && x <= x1 && z >= z0 && z <= z1;
705
+ function addDecal(spec) {
706
+ if (decalList.length >= budget.maxDecals) decalList.shift();
707
+ decalList.push({ ...spec, id: ++decalSerial, age: 0 });
708
+ decalsVersion++;
709
+ }
710
+ function clearDecalsInside(x, z, radius) {
711
+ const before = decalList.length;
712
+ for (let i = before - 1; i >= 0; i--) {
713
+ const decal = decalList[i];
714
+ if (decal.kind !== "ripple" && decal.kind !== "splat" && decal.kind !== "crack" && Math.hypot(decal.x - x, decal.z - z) < radius) decalList.splice(i, 1);
715
+ }
716
+ if (decalList.length !== before) decalsVersion++;
717
+ }
590
718
  function heightAt(x, z) {
591
719
  if (!contains(x, z)) return options.baseHeight?.(x, z) ?? 0;
592
720
  const fx = Math.max(0, Math.min(columns - 1e-6, (x - x0) / cellSize));
@@ -604,6 +732,42 @@ function createTerrain(options) {
604
732
  const row = clampIndex(Math.round((z - z0) / cellSize), rows);
605
733
  return { column, row, index: row * stride + column };
606
734
  };
735
+ function waterAt(x, z) {
736
+ if (!hasWater || !contains(x, z)) return null;
737
+ const { index } = vertexOf(x, z);
738
+ const water = profileList[profileIndex[index]].water;
739
+ if (!water) return null;
740
+ const level = surface[index];
741
+ const depth = level - heightAt(x, z);
742
+ return depth > WATER_MINIMUM ? { liquid: water.liquid, level, depth } : null;
743
+ }
744
+ const exposedAt = (x, z) => {
745
+ const { column, row } = vertexOf(x, z);
746
+ return vertexMaterial(column, row);
747
+ };
748
+ const footAt = (x, z) => {
749
+ if (!contains(x, z)) return heightAt(x, z);
750
+ return heightAt(x, z) - (exposedAt(x, z).footing?.sink ?? 0);
751
+ };
752
+ function walkFactorAt(x, z) {
753
+ if (!contains(x, z)) return 1;
754
+ const water = waterAt(x, z) ? profileList[profileIndex[vertexOf(x, z).index]].water : null;
755
+ return Math.min(water?.drag ?? 1, exposedAt(x, z).footing?.drag ?? 1);
756
+ }
757
+ function ripple(x, z, radius) {
758
+ addDecal({ kind: "ripple", x, z, radius, angle: 0, material: exposedAt(x, z).id });
759
+ }
760
+ function press(spec) {
761
+ if (!contains(spec.x, spec.z)) return false;
762
+ if (waterAt(spec.x, spec.z)) {
763
+ ripple(spec.x, spec.z, spec.radius * 2);
764
+ return true;
765
+ }
766
+ const material = exposedAt(spec.x, spec.z);
767
+ if (!material.footing?.prints) return false;
768
+ addDecal({ kind: spec.kind ?? "print", x: spec.x, z: spec.z, radius: spec.radius, angle: spec.angle ?? 0, material: material.id });
769
+ return true;
770
+ }
607
771
  function vertexMaterial(column, row) {
608
772
  const index = row * stride + column;
609
773
  return materials.get(stratumMaterialAtDepth(profileList[profileIndex[index]], dug[index]));
@@ -667,6 +831,7 @@ function createTerrain(options) {
667
831
  }
668
832
  }
669
833
  marks.removeInside(x, z, radius);
834
+ clearDecalsInside(x, z, radius);
670
835
  emitDeform(x, z, reach);
671
836
  }
672
837
  function throwPieces(spec) {
@@ -696,9 +861,15 @@ function createTerrain(options) {
696
861
  const { x, z } = spec;
697
862
  const start = columnAt(x, z);
698
863
  const profile = profileMap.get(start.profile);
864
+ const waterHere = waterAt(x, z);
865
+ const waterResult = waterHere ? { water: { liquid: waterHere.liquid, level: waterHere.level } } : {};
866
+ if (waterHere) ripple(x, z, Math.min(1.5, 0.15 + Math.cbrt(spec.energy) / 40));
699
867
  const removed = [];
700
868
  const point = [x, heightAt(x, z), z];
701
- let energy = spec.energy;
869
+ const startLayer = stratumAtDepth(profile, start.depth);
870
+ if (start.loose <= 0 && materials.get(startLayer.stratum.material).units) return strikeUnits(blow, spec, startLayer, profile, point);
871
+ const blowEnergy = spec.energy * (waterHere ? profile.water?.blowShare[blow] ?? 1 : 1);
872
+ let energy = blowEnergy;
702
873
  let depth = start.depth;
703
874
  let radius = 0;
704
875
  let carved = 0;
@@ -756,7 +927,7 @@ function createTerrain(options) {
756
927
  }
757
928
  let collapsed = loadBars(blow, spec.energy, x, z, profile);
758
929
  const crack = crackAround(blow, spec.energy, x, z, start.loose > 0 ? void 0 : firstMaterial);
759
- const extra = { ...crack ? { crack } : {}, ...breached ? { breached } : {} };
930
+ const extra = { ...crack ? { crack } : {}, ...breached ? { breached } : {}, ...waterResult };
760
931
  if (radius <= 0) return { point, radius: 0, depth: 0, totalDepth: columnAt(x, z).depth, removed: [], exposed: start.material, atLimit: start.remaining <= 1e-6, rebarExposed: rebarShowing(profile, start.depth), collapsed, fine: true, ...extra };
761
932
  if (firstMaterial.reposeDegrees === void 0 && start.loose <= 0) radius *= 1 + RIGID_WIDENING * (start.depth / maxDepthOfProfile(profile));
762
933
  const volumeTotal = removed.reduce((sum, entry) => sum + entry.volume, 0);
@@ -766,7 +937,8 @@ function createTerrain(options) {
766
937
  const rimShare = materials.get(removed[0].material).rimShare ?? 0;
767
938
  stampBowl(x, z, radius, carved, volumeTotal * rimShare);
768
939
  }
769
- let quota = budget.maxDebrisPerBlow;
940
+ if (firstMaterial.cracks) addDecal({ kind: "crack", x, z, radius: Math.max(CRACK_MIN_RADIUS, radius * firstMaterial.cracks), angle: random() * 6.283, material: firstMaterial.id });
941
+ let quota = waterHere && blow !== "explosive" ? 0 : budget.maxDebrisPerBlow;
770
942
  for (const entry of removed) {
771
943
  const material = materials.get(entry.material);
772
944
  const thrown = entry.volume * (1 - (fine ? 0 : material.rimShare ?? 0));
@@ -786,6 +958,67 @@ function createTerrain(options) {
786
958
  const scale = Math.cbrt(energy / CRACK_REFERENCE_JOULES);
787
959
  return cracks.add({ x, z, reach: response.reach * scale, width: response.width * scale, material: material.id, seed: Math.floor(random() * 2 ** 31) }, budget.maxCracks);
788
960
  }
961
+ function strikeUnits(blow, spec, layer, profile, point) {
962
+ const { x, z, energy } = spec;
963
+ const material = materials.get(layer.stratum.material);
964
+ const units = material.units;
965
+ const response = material.hits[blow];
966
+ const bottom = layer.top + layer.stratum.thickness;
967
+ const miss = { point, radius: 0, depth: 0, totalDepth: layer.top, removed: [], exposed: material.id, atLimit: false, rebarExposed: false, collapsed: false, fine: true };
968
+ if (!response || energy < (response.thresholdJoules ?? 0)) return miss;
969
+ const reach = Math.max(spec.contactRadius ?? 0, response.minRadius ?? 0, Math.cbrt(2 * energy / response.specificEnergy / (Math.PI * response.aspect)));
970
+ const rowFirst = Math.floor((z - reach) / units.width);
971
+ const rowLast = Math.floor((z + reach) / units.width);
972
+ let loosened = 0;
973
+ for (let row = rowFirst; row <= rowLast; row++) {
974
+ const shift = Math.abs(row % 2) * cellSize;
975
+ const unitFirst = Math.floor((x - reach - shift) / units.length);
976
+ const unitLast = Math.floor((x + reach - shift) / units.length);
977
+ for (let unit = unitFirst; unit <= unitLast; unit++) {
978
+ const minX = unit * units.length + shift;
979
+ const minZ = row * units.width;
980
+ const near = Math.hypot(Math.max(minX - x, 0, x - minX - units.length), Math.max(minZ - z, 0, z - minZ - units.width));
981
+ if (near > reach) continue;
982
+ const key = `${unit}:${row}`;
983
+ const taken = (unitDamage.get(key) ?? 0) + energy * (1 - (near / (reach + 1e-6)) ** 2);
984
+ unitDamage.set(key, taken);
985
+ if (taken >= units.looseJoules && liftUnit(minX, minZ, units, bottom, material, profile)) {
986
+ loosened++;
987
+ const speed = Math.min(MAX_PIECE_SPEED[blow], Math.sqrt(2 * energy * THROW_SHARE / Math.max(1, loosened * 15)));
988
+ const angle = random() * 6.283;
989
+ debrisStore.throwPiece({ material: material.id, kind: "blocks", radius: units.length / 2, position: [minX + units.length / 2, point[1] + 0.05, minZ + units.width / 2], velocity: [Math.cos(angle) * speed * 0.6, speed * (0.5 + random() * 0.5), Math.sin(angle) * speed * 0.6], spin: [(random() - 0.5) * 8, (random() - 0.5) * 8, (random() - 0.5) * 8] });
990
+ }
991
+ }
992
+ }
993
+ if (loosened === 0) {
994
+ addDecal({ kind: "crack", x, z, radius: Math.max(0.05, reach * 2), angle: random() * 6.283, material: material.id });
995
+ throwPieces({ x, y: point[1], z, volume: 1e-5, material: { ...material, debris: "chips" }, energy, ...spec.direction ? { direction: spec.direction } : {}, quota: 3, bowlRadius: 0.02, blow });
996
+ return { ...miss, radius: Math.max(0.02, Math.min(reach, 0.06)) };
997
+ }
998
+ emitDeform(x, z, reach + units.length);
999
+ clearDecalsInside(x, z, reach);
1000
+ const volume = loosened * units.length * units.width * layer.stratum.thickness;
1001
+ const after = columnAt(x, z);
1002
+ return { point, radius: reach, depth: layer.stratum.thickness, totalDepth: after.depth, removed: [{ material: material.id, volume }], exposed: after.material, atLimit: false, rebarExposed: false, collapsed: false, fine: false };
1003
+ }
1004
+ function liftUnit(minX, minZ, units, bottom, material, profile) {
1005
+ const column0 = clampIndex(Math.round((minX - x0) / cellSize), columns);
1006
+ const column1 = clampIndex(Math.round((minX + units.length - x0) / cellSize), columns);
1007
+ const row0 = clampIndex(Math.round((minZ - z0) / cellSize), rows);
1008
+ const row1 = clampIndex(Math.round((minZ + units.width - z0) / cellSize), rows);
1009
+ let lifted = false;
1010
+ for (let row = row0; row <= row1; row++) {
1011
+ for (let column = column0; column <= column1; column++) {
1012
+ const index = row * stride + column;
1013
+ if (profileList[profileIndex[index]].id !== profile.id || dug[index] >= bottom) continue;
1014
+ if (materials.get(stratumAtDepth(profile, dug[index]).stratum.material) !== material) continue;
1015
+ dug[index] = bottom;
1016
+ lifted = true;
1017
+ touchVertex(column, row);
1018
+ }
1019
+ }
1020
+ return lifted;
1021
+ }
789
1022
  function loadBars(blow, energy, x, z, profile) {
790
1023
  const bars = profile.strata.find((stratum) => stratum.reinforcement);
791
1024
  if (!bars?.reinforcement) return false;
@@ -865,10 +1098,31 @@ function createTerrain(options) {
865
1098
  const column1 = clampIndex(Math.ceil((spec.x + spec.radius - x0) / cellSize), columns);
866
1099
  const row0 = clampIndex(Math.floor((spec.z - spec.radius - z0) / cellSize), rows);
867
1100
  const row1 = clampIndex(Math.ceil((spec.z + spec.radius - z0) / cellSize), rows);
1101
+ let melted = false;
868
1102
  for (let row = row0; row <= row1; row++) {
869
1103
  for (let column = column0; column <= column1; column++) {
870
1104
  if (Math.hypot(x0 + column * cellSize - spec.x, z0 + row * cellSize - spec.z) > spec.radius) continue;
871
- burnVertex(column, row, spec.seconds);
1105
+ if (hasWater && base[row * stride + column] < surface[row * stride + column]) continue;
1106
+ melted = burnVertex(column, row, spec.seconds) || melted;
1107
+ }
1108
+ }
1109
+ if (melted) emitDeform(spec.x, spec.z, spec.radius);
1110
+ }
1111
+ function soot(x, z, radius) {
1112
+ if (radius <= 0) return;
1113
+ const column0 = clampIndex(Math.floor((x - radius - x0) / cellSize), columns);
1114
+ const column1 = clampIndex(Math.ceil((x + radius - x0) / cellSize), columns);
1115
+ const row0 = clampIndex(Math.floor((z - radius - z0) / cellSize), rows);
1116
+ const row1 = clampIndex(Math.ceil((z + radius - z0) / cellSize), rows);
1117
+ for (let row = row0; row <= row1; row++) {
1118
+ for (let column = column0; column <= column1; column++) {
1119
+ const distance = Math.hypot(x0 + column * cellSize - x, z0 + row * cellSize - z);
1120
+ const response = distance < radius ? vertexMaterial(column, row).fire : void 0;
1121
+ if (!response) continue;
1122
+ const index = row * stride + column;
1123
+ const before = scorch[index];
1124
+ scorch[index] = Math.max(before, response.maxScorch * BLAST_SOOT * Math.sqrt(1 - distance / radius));
1125
+ if (scorch[index] !== before) touchVertex(column, row);
872
1126
  }
873
1127
  }
874
1128
  }
@@ -876,12 +1130,13 @@ function createTerrain(options) {
876
1130
  const index = row * stride + column;
877
1131
  const material = vertexMaterial(column, row);
878
1132
  const response = material.fire;
879
- if (!response) return;
1133
+ if (!response) return false;
880
1134
  const before = scorch[index];
881
1135
  scorch[index] = Math.max(before, Math.min(response.maxScorch, before + response.scorchPerSecond * seconds));
882
1136
  if (response.burnsAway) removeFromVertex(index, response.burnsAway * seconds);
883
1137
  if (scorch[index] !== before || response.burnsAway) touchVertex(column, row);
884
1138
  if (response.flammable) ignite(x0 + column * cellSize, z0 + row * cellSize);
1139
+ return (response.burnsAway ?? 0) > 0;
885
1140
  }
886
1141
  const fireKey = (x, z) => Math.floor((z - z0) / FIRE_CELL) * 65536 + Math.floor((x - x0) / FIRE_CELL);
887
1142
  function ignite(x, z) {
@@ -952,7 +1207,8 @@ function createTerrain(options) {
952
1207
  }
953
1208
  function settle(piece) {
954
1209
  const material = materials.get(piece.material);
955
- if (!material || material.reposeDegrees === void 0) return false;
1210
+ if (!material || material.reposeDegrees === void 0 || material.scatters) return false;
1211
+ if (material.splats && contains(piece.position[0], piece.position[2]) && !waterAt(piece.position[0], piece.position[2])) addDecal({ kind: "splat", x: piece.position[0], z: piece.position[2], radius: piece.radius * 2.6, angle: piece.id * 2.399, material: material.id });
956
1212
  if (!contains(piece.position[0], piece.position[2])) return true;
957
1213
  const { index, column, row } = vertexOf(piece.position[0], piece.position[2]);
958
1214
  addToVertex(index, 4 / 3 * Math.PI * piece.radius ** 3 / (cellSize * cellSize), false);
@@ -975,7 +1231,9 @@ function createTerrain(options) {
975
1231
  const above = Math.max(0, y - heightAt(x, z));
976
1232
  const reach = (hit.radius ?? 1) * BLAST_HEIGHT_REACH;
977
1233
  if (above >= reach) return null;
978
- return strike("explosive", { x, z, energy: hit.energy * (1 - above / reach) ** 2 });
1234
+ const share = (1 - above / reach) ** 2;
1235
+ soot(x, z, BLAST_SOOT_REACH * Math.cbrt(hit.energy * share / 1e6));
1236
+ return strike("explosive", { x, z, energy: hit.energy * share });
979
1237
  }
980
1238
  case "fire":
981
1239
  case "heat":
@@ -1026,6 +1284,9 @@ function createTerrain(options) {
1026
1284
  touched.fill(0);
1027
1285
  marks.clear();
1028
1286
  cracks.clear();
1287
+ decalList.length = 0;
1288
+ decalsVersion++;
1289
+ unitDamage.clear();
1029
1290
  debrisStore.clear();
1030
1291
  sliding.clear();
1031
1292
  burning.clear();
@@ -1060,6 +1321,15 @@ function createTerrain(options) {
1060
1321
  reset();
1061
1322
  },
1062
1323
  heightAt,
1324
+ footAt,
1325
+ walkFactorAt,
1326
+ waterAt,
1327
+ press,
1328
+ ripple,
1329
+ decals: () => decalList,
1330
+ get decalsVersion() {
1331
+ return decalsVersion;
1332
+ },
1063
1333
  vertexHeight,
1064
1334
  vertexMaterial,
1065
1335
  columnAt,
@@ -1076,6 +1346,14 @@ function createTerrain(options) {
1076
1346
  debrisStore.update(dt, { heightAt, gravity, settle });
1077
1347
  updateSlides();
1078
1348
  updateFire(dt);
1349
+ for (let i = decalList.length - 1; i >= 0; i--) {
1350
+ const decal = decalList[i];
1351
+ decal.age += dt;
1352
+ if (decal.kind === "ripple" && decal.age > RIPPLE_SECONDS) {
1353
+ decalList.splice(i, 1);
1354
+ decalsVersion++;
1355
+ }
1356
+ }
1079
1357
  },
1080
1358
  get dirtyChunkCount() {
1081
1359
  return dirty.size;
@@ -1107,7 +1385,7 @@ function createTerrain(options) {
1107
1385
  stats: () => ({ marks: marks.list.length, debris: debrisStore.list.length, debrisMoving: debrisStore.list.filter((piece) => !piece.atRest).length, dirtyChunks: dirty.size, burning: burning.size }),
1108
1386
  reset,
1109
1387
  serialize() {
1110
- return { layout: currentLayout, dug: sparse(dug), loose: sparse(loose), scorch: sparse(scorch), barBend: sparse(barBend), marks: marks.list.map((mark) => ({ ...mark })), cracks: cracks.list.map((crack) => ({ ...crack })) };
1388
+ return { layout: currentLayout, dug: sparse(dug), loose: sparse(loose), scorch: sparse(scorch), barBend: sparse(barBend), marks: marks.list.map((mark) => ({ ...mark })), cracks: cracks.list.map((crack) => ({ ...crack })), decals: decalList.filter((decal) => decal.kind !== "ripple").map((decal) => ({ ...decal })) };
1111
1389
  },
1112
1390
  load(snapshot) {
1113
1391
  currentLayout = snapshot.layout;
@@ -1118,6 +1396,8 @@ function createTerrain(options) {
1118
1396
  fillSparse(barBend, snapshot.barBend ?? []);
1119
1397
  marks.load(snapshot.marks);
1120
1398
  cracks.load(snapshot.cracks ?? []);
1399
+ decalList.push(...(snapshot.decals ?? []).map((decal) => ({ ...decal })));
1400
+ decalSerial = decalList.reduce((highest, decal) => Math.max(highest, decal.id), decalSerial);
1121
1401
  for (let chunkIndex = 0; chunkIndex < touched.length; chunkIndex++) markChunk(chunkIndex);
1122
1402
  }
1123
1403
  };
@@ -1205,7 +1485,57 @@ function createTerrainImpl(terrain, options) {
1205
1485
  }
1206
1486
 
1207
1487
  // src/types.ts
1208
- var TERRAIN_PATTERNS = ["plain", "grass", "soil", "sand", "asphalt", "concrete", "tile", "planks", "stone"];
1488
+ var TERRAIN_PATTERNS = ["plain", "grass", "soil", "sand", "asphalt", "concrete", "tile", "planks", "stone", "mud", "gravel", "clay", "snow", "cobble", "paver"];
1489
+
1490
+ // src/footprints.ts
1491
+ var STRIDE = 0.72;
1492
+ var FOOT_LENGTH = 0.15;
1493
+ var FOOT_APART = 0.11;
1494
+ function createFootprints(terrain, options = {}) {
1495
+ const stride = options.stride ?? STRIDE;
1496
+ const footLength = options.footLength ?? FOOT_LENGTH;
1497
+ let lastX = null;
1498
+ let lastZ = 0;
1499
+ let side = 1;
1500
+ return {
1501
+ update(x, z) {
1502
+ if (lastX === null) {
1503
+ lastX = x;
1504
+ lastZ = z;
1505
+ return;
1506
+ }
1507
+ const dx = x - lastX;
1508
+ const dz = z - lastZ;
1509
+ const walked = Math.hypot(dx, dz);
1510
+ if (walked < stride) return;
1511
+ lastX = x;
1512
+ lastZ = z;
1513
+ if (walked > stride * 3) return;
1514
+ const angle = Math.atan2(dx, dz);
1515
+ side = -side;
1516
+ terrain.press({ x: x + Math.cos(angle) * FOOT_APART * side, z: z - Math.sin(angle) * FOOT_APART * side, radius: footLength, angle });
1517
+ },
1518
+ reset() {
1519
+ lastX = null;
1520
+ }
1521
+ };
1522
+ }
1523
+
1524
+ // src/buoyancy.ts
1525
+ var GRAVITY2 = 9.81;
1526
+ var WATER_DRAG = 9;
1527
+ var MAX_LIFT_WEIGHTS = 1.6;
1528
+ function waterLift(params) {
1529
+ const density = params.liquidDensity ?? 1e3;
1530
+ const submerged = Math.max(0, Math.min(1, params.submerged));
1531
+ const mass = Math.max(1e-6, params.mass);
1532
+ const gravity = params.gravity ?? GRAVITY2;
1533
+ return {
1534
+ acceleration: Math.min(MAX_LIFT_WEIGHTS * gravity, density * params.volume * submerged * gravity / mass),
1535
+ drag: WATER_DRAG * submerged,
1536
+ floats: mass / Math.max(1e-9, params.volume) < density
1537
+ };
1538
+ }
1209
1539
  export {
1210
1540
  CRACK_REFERENCE_JOULES,
1211
1541
  DEFAULT_BAR_YIELD_JOULES,
@@ -1215,7 +1545,9 @@ export {
1215
1545
  TERRAIN_PATTERNS,
1216
1546
  TERRAIN_PROFILES,
1217
1547
  crackLines,
1548
+ createFootprints,
1218
1549
  createTerrain,
1219
- createTerrainImpl
1550
+ createTerrainImpl,
1551
+ waterLift
1220
1552
  };
1221
1553
  //# sourceMappingURL=index.js.map