@adatechnology/game-terrain 0.6.0 → 0.7.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
@@ -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) {
@@ -645,6 +711,17 @@ function createTerrain(options) {
645
711
  const emitDeform = (x, z, radius) => {
646
712
  for (const listener of deformListeners) listener({ x, z, radius });
647
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
+ };
648
725
  const chunkOfVertex = (column, row) => Math.min(chunksZ - 1, Math.floor(row / chunkCells)) * chunksX + Math.min(chunksX - 1, Math.floor(column / chunkCells));
649
726
  function touchVertex(column, row) {
650
727
  const chunk2 = chunkOfVertex(column, row);
@@ -804,7 +881,7 @@ function createTerrain(options) {
804
881
  loose[index] = loose[index] + amount - filled;
805
882
  }
806
883
  const isSoftExposed = (index) => materials.get(stratumAtDepth(profileList[profileIndex[index]], dug[index]).stratum.material).reposeDegrees !== void 0;
807
- function stampBowl(x, z, radius, depth, rimVolume) {
884
+ function stampBowl(x, z, radius, depth, rimVolume, lossSource = "crater") {
808
885
  const reach = radius * RIM_REACH;
809
886
  const column0 = clampIndex(Math.floor((x - reach - x0) / cellSize), columns);
810
887
  const column1 = clampIndex(Math.ceil((x + reach - x0) / cellSize), columns);
@@ -826,12 +903,15 @@ function createTerrain(options) {
826
903
  }
827
904
  }
828
905
  const cellArea = cellSize * cellSize;
906
+ const tally = /* @__PURE__ */ new Map();
829
907
  for (let row = row0; row <= row1; row++) {
830
908
  for (let column = column0; column <= column1; column++) {
831
909
  const q = shapeOf(x0 + column * cellSize, z0 + row * cellSize);
832
910
  const index = row * stride + column;
833
911
  if (q < 1) {
912
+ const before = dug[index];
834
913
  removeFromVertex(index, depth * (1 - q * q) * (0.92 + random() * 0.16));
914
+ tallyLowered(tally, index, dug[index] - before);
835
915
  sliding.add(index);
836
916
  touchVertex(column, row);
837
917
  } else if (q < RIM_REACH && rimWeight > 0) {
@@ -844,6 +924,7 @@ function createTerrain(options) {
844
924
  marks.removeInside(x, z, radius);
845
925
  clearDecalsInside(x, z, radius);
846
926
  emitDeform(x, z, reach);
927
+ if (lossSource) emitGroundLoss(tally, lossSource, 0);
847
928
  }
848
929
  function throwPieces(spec) {
849
930
  const radius = PIECE_RADIUS[spec.material.debris];
@@ -940,6 +1021,7 @@ function createTerrain(options) {
940
1021
  let collapsed = loadBars(blow, spec.energy, x, z, profile);
941
1022
  const crack = crackAround(blow, spec.energy, x, z, start.loose > 0 ? void 0 : firstMaterial);
942
1023
  const extra = { ...crack ? { crack } : {}, ...breached ? { breached } : {}, ...waterResult };
1024
+ const pressed = spec.contactRadius !== void 0 && spec.contactRadius >= radius;
943
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 };
944
1026
  if (firstMaterial.reposeDegrees === void 0 && start.loose <= 0) radius *= 1 + RIGID_WIDENING * (start.depth / maxDepthOfProfile(profile));
945
1027
  const volumeTotal = removed.reduce((sum, entry) => sum + entry.volume, 0);
@@ -947,7 +1029,7 @@ function createTerrain(options) {
947
1029
  if (fine) depositMark({ x, z, radius, depth: carved, blow, profile, cap: maxDepthOfProfile(profile) });
948
1030
  else {
949
1031
  const rimShare = materials.get(removed[0].material).rimShare ?? 0;
950
- stampBowl(x, z, radius, carved, volumeTotal * rimShare);
1032
+ stampBowl(x, z, radius, carved, volumeTotal * rimShare, pressed ? null : "crater");
951
1033
  }
952
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 });
953
1035
  let quota = waterHere && blow !== "explosive" ? 0 : budget.maxDebrisPerBlow;
@@ -1049,6 +1131,7 @@ function createTerrain(options) {
1049
1131
  const row1 = clampIndex(Math.ceil((z + reach - z0) / cellSize), rows);
1050
1132
  const limit = maxDepthOfProfile(profile);
1051
1133
  let collapsed = false;
1134
+ const tally = /* @__PURE__ */ new Map();
1052
1135
  for (let row = row0; row <= row1; row++) {
1053
1136
  for (let column = column0; column <= column1; column++) {
1054
1137
  const index = row * stride + column;
@@ -1058,16 +1141,17 @@ function createTerrain(options) {
1058
1141
  barBend[index] = barBend[index] + energy / yieldJoules * (1 - q * q);
1059
1142
  touchVertex(column, row);
1060
1143
  if (barBend[index] >= 1 && profile.drop !== void 0 && dug[index] >= limit * COLLAPSE_DEPTH_SHARE && dug[index] < limit + profile.drop) {
1061
- dug[index] = limit + profile.drop;
1144
+ dropVertex(index, limit + profile.drop, tally);
1062
1145
  collapsed = true;
1063
1146
  }
1064
1147
  }
1065
1148
  }
1066
- if (collapsed) giveWay({ x, z, reach, profile });
1149
+ if (collapsed) giveWay({ x, z, reach, profile, tally });
1150
+ emitGroundLoss(tally, "slab", 0);
1067
1151
  return collapsed;
1068
1152
  }
1069
1153
  function giveWay(spec) {
1070
- const { x, z, reach, profile } = spec;
1154
+ const { x, z, reach, profile, tally } = spec;
1071
1155
  emitDeform(x, z, reach);
1072
1156
  if (profile.drop === void 0) return;
1073
1157
  const limit = maxDepthOfProfile(profile);
@@ -1079,7 +1163,7 @@ function createTerrain(options) {
1079
1163
  for (let column = column0; column <= column1; column++) {
1080
1164
  const index = row * stride + column;
1081
1165
  if (barBend[index] >= COLLAPSE_SPREAD_BEND && dug[index] < limit + profile.drop) {
1082
- dug[index] = limit + profile.drop;
1166
+ dropVertex(index, limit + profile.drop, tally);
1083
1167
  touchVertex(column, row);
1084
1168
  }
1085
1169
  }
@@ -1103,6 +1187,7 @@ function createTerrain(options) {
1103
1187
  if (spread) touched2.push({ load, ...spread });
1104
1188
  }
1105
1189
  let opened = false;
1190
+ const tally = /* @__PURE__ */ new Map();
1106
1191
  for (const [index, bend] of bends) {
1107
1192
  if (barBend[index] >= bend) continue;
1108
1193
  barBend[index] = bend;
@@ -1112,11 +1197,12 @@ function createTerrain(options) {
1112
1197
  const profile = profileList[profileIndex[index]];
1113
1198
  const limit = maxDepthOfProfile(profile);
1114
1199
  if (bend >= 1 && profile.drop !== void 0 && dug[index] >= limit * COLLAPSE_DEPTH_SHARE && dug[index] < limit + profile.drop) {
1115
- dug[index] = limit + profile.drop;
1200
+ dropVertex(index, limit + profile.drop, tally);
1116
1201
  opened = true;
1117
1202
  }
1118
1203
  }
1119
- 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);
1120
1206
  return opened;
1121
1207
  }
1122
1208
  function spreadLoad(load, bends) {
@@ -1275,10 +1361,14 @@ function createTerrain(options) {
1275
1361
  if (nextColumn < 0 || nextRow < 0 || nextColumn > columns || nextRow > rows) continue;
1276
1362
  const excess = height - vertexHeight(nextColumn, nextRow) - slope;
1277
1363
  if (excess <= SLIDE_EPSILON) continue;
1364
+ const before = dug[index];
1278
1365
  const moved = removeFromVertex(index, excess * SLIDE_SHARE);
1279
1366
  if (moved <= 0) continue;
1367
+ tallyLowered(slideTally, index, dug[index] - before);
1280
1368
  const next = nextRow * stride + nextColumn;
1369
+ const nextBefore = dug[next];
1281
1370
  addToVertex(next, moved, isSoftExposed(next));
1371
+ tallyLowered(slideTally, next, dug[next] - nextBefore);
1282
1372
  sliding.add(index);
1283
1373
  sliding.add(next);
1284
1374
  touchVertex(column, row);
@@ -1292,6 +1382,10 @@ function createTerrain(options) {
1292
1382
  }
1293
1383
  }
1294
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
+ }
1295
1389
  }
1296
1390
  function settle(piece) {
1297
1391
  const material = materials.get(piece.material);
@@ -1378,6 +1472,7 @@ function createTerrain(options) {
1378
1472
  unitDamage.clear();
1379
1473
  debrisStore.clear();
1380
1474
  sliding.clear();
1475
+ slideTally.clear();
1381
1476
  burning.clear();
1382
1477
  random = createRandom(seed);
1383
1478
  applyLayout(currentLayout);
@@ -1428,6 +1523,7 @@ function createTerrain(options) {
1428
1523
  burn,
1429
1524
  applyHit,
1430
1525
  bear: bearAll,
1526
+ voidUnder: (x, z, radius) => voidUnder(grid, { x, z, radius }),
1431
1527
  onDeform(listener) {
1432
1528
  deformListeners.add(listener);
1433
1529
  return () => deformListeners.delete(listener);
@@ -1633,9 +1729,12 @@ export {
1633
1729
  DEFAULT_BAR_YIELD_PA,
1634
1730
  DEFAULT_PROFILE,
1635
1731
  DEFAULT_TERRAIN_BUDGET,
1732
+ GROUND_LOSS_MIN_DEPTH,
1733
+ SLIDE_LOSS_MIN_LOWERING,
1636
1734
  TERRAIN_MATERIALS,
1637
1735
  TERRAIN_PATTERNS,
1638
1736
  TERRAIN_PROFILES,
1737
+ VOID_MIN_DEPTH,
1639
1738
  crackLines,
1640
1739
  createFootprints,
1641
1740
  createTerrain,