@adatechnology/game-terrain 0.5.2 → 0.7.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
@@ -405,6 +405,72 @@ function createDebrisStore(max) {
405
405
  };
406
406
  }
407
407
 
408
+ // src/ground-loss.ts
409
+ var GROUND_LOSS_MIN_DEPTH = 0.02;
410
+ var SLIDE_LOSS_MIN_LOWERING = 0.05;
411
+ var VOID_MIN_DEPTH = 0.02;
412
+ function tallyLowered(tally, index, amount) {
413
+ if (amount !== 0) tally.set(index, (tally.get(index) ?? 0) + amount);
414
+ }
415
+ function lossOf(grid, tally, spec) {
416
+ let weight = 0;
417
+ let centerX = 0;
418
+ let centerZ = 0;
419
+ let depth = 0;
420
+ let deepest = 0;
421
+ const kept = [];
422
+ for (const [index, amount] of tally) {
423
+ if (amount < spec.minLowering || amount <= 0) continue;
424
+ const column = index % grid.stride;
425
+ const row = (index - column) / grid.stride;
426
+ weight += amount;
427
+ centerX += (grid.x0 + column * grid.cellSize) * amount;
428
+ centerZ += (grid.z0 + row * grid.cellSize) * amount;
429
+ depth = Math.max(depth, grid.dug[index]);
430
+ deepest = Math.max(deepest, amount);
431
+ kept.push(index);
432
+ }
433
+ if (weight <= 0 || depth < GROUND_LOSS_MIN_DEPTH || deepest < GROUND_LOSS_MIN_DEPTH) return null;
434
+ centerX /= weight;
435
+ centerZ /= weight;
436
+ let radius = 0;
437
+ for (const index of kept) {
438
+ if (spec.shape === "bowl" && grid.dug[index] < depth / 2) continue;
439
+ const column = index % grid.stride;
440
+ const row = (index - column) / grid.stride;
441
+ radius = Math.max(radius, Math.hypot(grid.x0 + column * grid.cellSize - centerX, grid.z0 + row * grid.cellSize - centerZ));
442
+ }
443
+ const reach = spec.shape === "bowl" ? Math.SQRT2 * (radius + grid.cellSize / 2) : radius + grid.cellSize / 2;
444
+ return { x: centerX, z: centerZ, radius: reach, depth, removed: weight * grid.cellSize * grid.cellSize, source: spec.source };
445
+ }
446
+ function voidUnder(grid, spec) {
447
+ const { x, z, radius } = spec;
448
+ const clamp = (value, top) => Math.max(0, Math.min(top, value));
449
+ const column0 = clamp(Math.ceil((x - radius - grid.x0) / grid.cellSize), grid.columns);
450
+ const column1 = clamp(Math.floor((x + radius - grid.x0) / grid.cellSize), grid.columns);
451
+ const row0 = clamp(Math.ceil((z - radius - grid.z0) / grid.cellSize), grid.rows);
452
+ const row1 = clamp(Math.floor((z + radius - grid.z0) / grid.cellSize), grid.rows);
453
+ let count = 0;
454
+ let sum = 0;
455
+ let depth = 0;
456
+ let open = 0;
457
+ for (let row = row0; row <= row1; row++) {
458
+ for (let column = column0; column <= column1; column++) {
459
+ if (Math.hypot(grid.x0 + column * grid.cellSize - x, grid.z0 + row * grid.cellSize - z) > radius) continue;
460
+ const here = grid.dug[row * grid.stride + column];
461
+ count++;
462
+ sum += here;
463
+ depth = Math.max(depth, here);
464
+ if (here >= VOID_MIN_DEPTH) open++;
465
+ }
466
+ }
467
+ if (count === 0) {
468
+ const here = grid.dug[clamp(Math.round((z - grid.z0) / grid.cellSize), grid.rows) * grid.stride + clamp(Math.round((x - grid.x0) / grid.cellSize), grid.columns)];
469
+ return { depth: here, mean: here, fraction: here >= VOID_MIN_DEPTH ? 1 : 0 };
470
+ }
471
+ return { depth, mean: sum / count, fraction: open / count };
472
+ }
473
+
408
474
  // src/marks.ts
409
475
  var SAME_HOLE = 0.75;
410
476
  function createMarkStore(budget) {
@@ -534,6 +600,12 @@ var GRAVITY = 9.81;
534
600
  var FINE_CELLS = 1.5;
535
601
  var FINE_DEPTH_CELLS = 0.4;
536
602
  var RIGID_WIDENING = 2.5;
603
+ var REFERENCE_CALIBER = 7.62;
604
+ var CALIBER_ASPECT_POWER = 0.5;
605
+ var CALIBER_ASPECT_RANGE = [0.6, 1.6];
606
+ var CALIBERS_ACROSS = 2.5;
607
+ var caliberAspect = (caliber) => caliber && caliber > 0 ? Math.min(CALIBER_ASPECT_RANGE[1], Math.max(CALIBER_ASPECT_RANGE[0], (REFERENCE_CALIBER / caliber) ** CALIBER_ASPECT_POWER)) : 1;
608
+ var caliberRadius = (caliber) => caliber && caliber > 0 ? caliber / 1e3 * CALIBERS_ACROSS / 2 : 0;
537
609
  var COLLAPSE_DEPTH_SHARE = 0.75;
538
610
  var COLLAPSE_SPREAD_BEND = 0.5;
539
611
  var BARS_SHARE_REACH = 0.6;
@@ -639,6 +711,17 @@ function createTerrain(options) {
639
711
  const emitDeform = (x, z, radius) => {
640
712
  for (const listener of deformListeners) listener({ x, z, radius });
641
713
  };
714
+ const grid = { x0, z0, cellSize, stride, columns, rows, dug };
715
+ const slideTally = /* @__PURE__ */ new Map();
716
+ const emitGroundLoss = (tally, source, minLowering) => {
717
+ if (!options.onGroundLoss) return;
718
+ const loss = lossOf(grid, tally, { source, minLowering, shape: source === "slab" ? "flat" : "bowl" });
719
+ if (loss) options.onGroundLoss(loss);
720
+ };
721
+ const dropVertex = (index, depth, tally) => {
722
+ tallyLowered(tally, index, depth - dug[index]);
723
+ dug[index] = depth;
724
+ };
642
725
  const chunkOfVertex = (column, row) => Math.min(chunksZ - 1, Math.floor(row / chunkCells)) * chunksX + Math.min(chunksX - 1, Math.floor(column / chunkCells));
643
726
  function touchVertex(column, row) {
644
727
  const chunk2 = chunkOfVertex(column, row);
@@ -798,7 +881,7 @@ function createTerrain(options) {
798
881
  loose[index] = loose[index] + amount - filled;
799
882
  }
800
883
  const isSoftExposed = (index) => materials.get(stratumAtDepth(profileList[profileIndex[index]], dug[index]).stratum.material).reposeDegrees !== void 0;
801
- function stampBowl(x, z, radius, depth, rimVolume) {
884
+ function stampBowl(x, z, radius, depth, rimVolume, lossSource = "crater") {
802
885
  const reach = radius * RIM_REACH;
803
886
  const column0 = clampIndex(Math.floor((x - reach - x0) / cellSize), columns);
804
887
  const column1 = clampIndex(Math.ceil((x + reach - x0) / cellSize), columns);
@@ -820,12 +903,15 @@ function createTerrain(options) {
820
903
  }
821
904
  }
822
905
  const cellArea = cellSize * cellSize;
906
+ const tally = /* @__PURE__ */ new Map();
823
907
  for (let row = row0; row <= row1; row++) {
824
908
  for (let column = column0; column <= column1; column++) {
825
909
  const q = shapeOf(x0 + column * cellSize, z0 + row * cellSize);
826
910
  const index = row * stride + column;
827
911
  if (q < 1) {
912
+ const before = dug[index];
828
913
  removeFromVertex(index, depth * (1 - q * q) * (0.92 + random() * 0.16));
914
+ tallyLowered(tally, index, dug[index] - before);
829
915
  sliding.add(index);
830
916
  touchVertex(column, row);
831
917
  } else if (q < RIM_REACH && rimWeight > 0) {
@@ -838,6 +924,7 @@ function createTerrain(options) {
838
924
  marks.removeInside(x, z, radius);
839
925
  clearDecalsInside(x, z, radius);
840
926
  emitDeform(x, z, reach);
927
+ if (lossSource) emitGroundLoss(tally, lossSource, 0);
841
928
  }
842
929
  function throwPieces(spec) {
843
930
  const radius = PIECE_RADIUS[spec.material.debris];
@@ -906,16 +993,17 @@ function createTerrain(options) {
906
993
  const pastCover = bars !== void 0 && depth - layer.top >= bars.cover - 1e-6;
907
994
  const specificEnergy = response.specificEnergy * (pastCover ? bars.resistance : 1);
908
995
  const limit = bars && !pastCover ? Math.min(room, layer.top + bars.cover - depth) : room;
996
+ const aspect = response.aspect * caliberAspect(spec.caliber);
909
997
  const volume = energy / specificEnergy;
910
- const wanted = Math.cbrt(2 * volume / (Math.PI * response.aspect));
911
- const wantedDepth = wanted * response.aspect;
998
+ const wanted = Math.cbrt(2 * volume / (Math.PI * aspect));
999
+ const wantedDepth = wanted * aspect;
912
1000
  const takenDepth = Math.min(wantedDepth, limit);
913
- const takenRadius = takenDepth < wantedDepth ? takenDepth / response.aspect : wanted;
1001
+ const takenRadius = takenDepth < wantedDepth ? takenDepth / aspect : wanted;
914
1002
  const takenVolume = Math.PI * takenRadius * takenRadius * takenDepth / 2;
915
1003
  energy -= takenVolume * specificEnergy;
916
1004
  depth += takenDepth;
917
1005
  carved += takenDepth;
918
- radius = Math.max(radius, takenRadius, response.minRadius ?? 0, spec.contactRadius ?? 0);
1006
+ radius = Math.max(radius, takenRadius, response.minRadius ?? 0, spec.contactRadius ?? 0, caliberRadius(spec.caliber));
919
1007
  const last = removed[removed.length - 1];
920
1008
  if (last?.material === material.id) last.volume += takenVolume;
921
1009
  else removed.push({ material: material.id, volume: takenVolume });
@@ -933,6 +1021,7 @@ function createTerrain(options) {
933
1021
  let collapsed = loadBars(blow, spec.energy, x, z, profile);
934
1022
  const crack = crackAround(blow, spec.energy, x, z, start.loose > 0 ? void 0 : firstMaterial);
935
1023
  const extra = { ...crack ? { crack } : {}, ...breached ? { breached } : {}, ...waterResult };
1024
+ const pressed = spec.contactRadius !== void 0 && spec.contactRadius >= radius;
936
1025
  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 };
937
1026
  if (firstMaterial.reposeDegrees === void 0 && start.loose <= 0) radius *= 1 + RIGID_WIDENING * (start.depth / maxDepthOfProfile(profile));
938
1027
  const volumeTotal = removed.reduce((sum, entry) => sum + entry.volume, 0);
@@ -940,7 +1029,7 @@ function createTerrain(options) {
940
1029
  if (fine) depositMark({ x, z, radius, depth: carved, blow, profile, cap: maxDepthOfProfile(profile) });
941
1030
  else {
942
1031
  const rimShare = materials.get(removed[0].material).rimShare ?? 0;
943
- stampBowl(x, z, radius, carved, volumeTotal * rimShare);
1032
+ stampBowl(x, z, radius, carved, volumeTotal * rimShare, pressed ? null : "crater");
944
1033
  }
945
1034
  if (firstMaterial.cracks) addDecal({ kind: "crack", x, z, radius: Math.max(CRACK_MIN_RADIUS, radius * firstMaterial.cracks), angle: random() * 6.283, material: firstMaterial.id });
946
1035
  let quota = waterHere && blow !== "explosive" ? 0 : budget.maxDebrisPerBlow;
@@ -971,7 +1060,7 @@ function createTerrain(options) {
971
1060
  const bottom = layer.top + layer.stratum.thickness;
972
1061
  const miss = { point, radius: 0, depth: 0, totalDepth: layer.top, removed: [], exposed: material.id, atLimit: false, rebarExposed: false, collapsed: false, fine: true };
973
1062
  if (!response || energy < (response.thresholdJoules ?? 0)) return miss;
974
- const reach = Math.max(spec.contactRadius ?? 0, response.minRadius ?? 0, Math.cbrt(2 * energy / response.specificEnergy / (Math.PI * response.aspect)));
1063
+ const reach = Math.max(spec.contactRadius ?? 0, response.minRadius ?? 0, caliberRadius(spec.caliber), Math.cbrt(2 * energy / response.specificEnergy / (Math.PI * response.aspect * caliberAspect(spec.caliber))));
975
1064
  const rowFirst = Math.floor((z - reach) / units.width);
976
1065
  const rowLast = Math.floor((z + reach) / units.width);
977
1066
  let loosened = 0;
@@ -1042,6 +1131,7 @@ function createTerrain(options) {
1042
1131
  const row1 = clampIndex(Math.ceil((z + reach - z0) / cellSize), rows);
1043
1132
  const limit = maxDepthOfProfile(profile);
1044
1133
  let collapsed = false;
1134
+ const tally = /* @__PURE__ */ new Map();
1045
1135
  for (let row = row0; row <= row1; row++) {
1046
1136
  for (let column = column0; column <= column1; column++) {
1047
1137
  const index = row * stride + column;
@@ -1051,16 +1141,17 @@ function createTerrain(options) {
1051
1141
  barBend[index] = barBend[index] + energy / yieldJoules * (1 - q * q);
1052
1142
  touchVertex(column, row);
1053
1143
  if (barBend[index] >= 1 && profile.drop !== void 0 && dug[index] >= limit * COLLAPSE_DEPTH_SHARE && dug[index] < limit + profile.drop) {
1054
- dug[index] = limit + profile.drop;
1144
+ dropVertex(index, limit + profile.drop, tally);
1055
1145
  collapsed = true;
1056
1146
  }
1057
1147
  }
1058
1148
  }
1059
- if (collapsed) giveWay({ x, z, reach, profile });
1149
+ if (collapsed) giveWay({ x, z, reach, profile, tally });
1150
+ emitGroundLoss(tally, "slab", 0);
1060
1151
  return collapsed;
1061
1152
  }
1062
1153
  function giveWay(spec) {
1063
- const { x, z, reach, profile } = spec;
1154
+ const { x, z, reach, profile, tally } = spec;
1064
1155
  emitDeform(x, z, reach);
1065
1156
  if (profile.drop === void 0) return;
1066
1157
  const limit = maxDepthOfProfile(profile);
@@ -1072,7 +1163,7 @@ function createTerrain(options) {
1072
1163
  for (let column = column0; column <= column1; column++) {
1073
1164
  const index = row * stride + column;
1074
1165
  if (barBend[index] >= COLLAPSE_SPREAD_BEND && dug[index] < limit + profile.drop) {
1075
- dug[index] = limit + profile.drop;
1166
+ dropVertex(index, limit + profile.drop, tally);
1076
1167
  touchVertex(column, row);
1077
1168
  }
1078
1169
  }
@@ -1096,6 +1187,7 @@ function createTerrain(options) {
1096
1187
  if (spread) touched2.push({ load, ...spread });
1097
1188
  }
1098
1189
  let opened = false;
1190
+ const tally = /* @__PURE__ */ new Map();
1099
1191
  for (const [index, bend] of bends) {
1100
1192
  if (barBend[index] >= bend) continue;
1101
1193
  barBend[index] = bend;
@@ -1105,11 +1197,12 @@ function createTerrain(options) {
1105
1197
  const profile = profileList[profileIndex[index]];
1106
1198
  const limit = maxDepthOfProfile(profile);
1107
1199
  if (bend >= 1 && profile.drop !== void 0 && dug[index] >= limit * COLLAPSE_DEPTH_SHARE && dug[index] < limit + profile.drop) {
1108
- dug[index] = limit + profile.drop;
1200
+ dropVertex(index, limit + profile.drop, tally);
1109
1201
  opened = true;
1110
1202
  }
1111
1203
  }
1112
- if (opened) for (const { load, reach, profile } of touched2) giveWay({ x: load.x, z: load.z, reach, profile });
1204
+ if (opened) for (const { load, reach, profile } of touched2) giveWay({ x: load.x, z: load.z, reach, profile, tally });
1205
+ emitGroundLoss(tally, "slab", 0);
1113
1206
  return opened;
1114
1207
  }
1115
1208
  function spreadLoad(load, bends) {
@@ -1268,10 +1361,14 @@ function createTerrain(options) {
1268
1361
  if (nextColumn < 0 || nextRow < 0 || nextColumn > columns || nextRow > rows) continue;
1269
1362
  const excess = height - vertexHeight(nextColumn, nextRow) - slope;
1270
1363
  if (excess <= SLIDE_EPSILON) continue;
1364
+ const before = dug[index];
1271
1365
  const moved = removeFromVertex(index, excess * SLIDE_SHARE);
1272
1366
  if (moved <= 0) continue;
1367
+ tallyLowered(slideTally, index, dug[index] - before);
1273
1368
  const next = nextRow * stride + nextColumn;
1369
+ const nextBefore = dug[next];
1274
1370
  addToVertex(next, moved, isSoftExposed(next));
1371
+ tallyLowered(slideTally, next, dug[next] - nextBefore);
1275
1372
  sliding.add(index);
1276
1373
  sliding.add(next);
1277
1374
  touchVertex(column, row);
@@ -1285,6 +1382,10 @@ function createTerrain(options) {
1285
1382
  }
1286
1383
  }
1287
1384
  if (minX <= maxX) emitDeform((minX + maxX) / 2, (minZ + maxZ) / 2, Math.hypot(maxX - minX, maxZ - minZ) / 2 + cellSize * 2);
1385
+ if (sliding.size === 0 && slideTally.size > 0) {
1386
+ emitGroundLoss(slideTally, "slide", SLIDE_LOSS_MIN_LOWERING);
1387
+ slideTally.clear();
1388
+ }
1288
1389
  }
1289
1390
  function settle(piece) {
1290
1391
  const material = materials.get(piece.material);
@@ -1305,7 +1406,8 @@ function createTerrain(options) {
1305
1406
  case "slash": {
1306
1407
  if (!hit.energy) return null;
1307
1408
  const contactRadius = hit.type === "blunt" ? hit.radius : void 0;
1308
- return strike(hit.type, { x, z, energy: hit.energy, ...hit.direction ? { direction: hit.direction } : {}, ...contactRadius !== void 0 ? { contactRadius } : {} });
1409
+ const caliber = hit.type === "ballistic" ? hit.caliber : void 0;
1410
+ return strike(hit.type, { x, z, energy: hit.energy, ...hit.direction ? { direction: hit.direction } : {}, ...contactRadius !== void 0 ? { contactRadius } : {}, ...caliber !== void 0 ? { caliber } : {} });
1309
1411
  }
1310
1412
  case "explosive": {
1311
1413
  if (!hit.energy) return null;
@@ -1370,6 +1472,7 @@ function createTerrain(options) {
1370
1472
  unitDamage.clear();
1371
1473
  debrisStore.clear();
1372
1474
  sliding.clear();
1475
+ slideTally.clear();
1373
1476
  burning.clear();
1374
1477
  random = createRandom(seed);
1375
1478
  applyLayout(currentLayout);
@@ -1420,6 +1523,7 @@ function createTerrain(options) {
1420
1523
  burn,
1421
1524
  applyHit,
1422
1525
  bear: bearAll,
1526
+ voidUnder: (x, z, radius) => voidUnder(grid, { x, z, radius }),
1423
1527
  onDeform(listener) {
1424
1528
  deformListeners.add(listener);
1425
1529
  return () => deformListeners.delete(listener);
@@ -1625,9 +1729,12 @@ export {
1625
1729
  DEFAULT_BAR_YIELD_PA,
1626
1730
  DEFAULT_PROFILE,
1627
1731
  DEFAULT_TERRAIN_BUDGET,
1732
+ GROUND_LOSS_MIN_DEPTH,
1733
+ SLIDE_LOSS_MIN_LOWERING,
1628
1734
  TERRAIN_MATERIALS,
1629
1735
  TERRAIN_PATTERNS,
1630
1736
  TERRAIN_PROFILES,
1737
+ VOID_MIN_DEPTH,
1631
1738
  crackLines,
1632
1739
  createFootprints,
1633
1740
  createTerrain,