@adatechnology/game-terrain 0.6.0 → 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.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { GroundLayout, Hit, DestructibleImpl } from '@adatechnology/game-contracts';
1
+ import { GroundLayout, Hit, GroundLoss, DestructibleImpl } from '@adatechnology/game-contracts';
2
2
  export { GroundLayout, GroundZone } from '@adatechnology/game-contracts';
3
3
 
4
4
  /** The blows that remove ground. Fire and heat char it instead (`TerrainMaterial.fire`). */
@@ -184,6 +184,20 @@ interface TerrainOptions {
184
184
  /** Metres per second squared. */
185
185
  readonly gravity?: number;
186
186
  readonly seed?: number;
187
+ /**
188
+ * Ground taken away from under the surface: a crater (`crater`), a slab that gave way into the void under it (`slab`), sand that slid off a slope
189
+ * (`slide`). The host hands it to what stands there (`DestructionWorld.groundLoss`), so foundations over it settle or lose their support.
190
+ */
191
+ readonly onGroundLoss?: (loss: GroundLoss) => void;
192
+ }
193
+ /** What is missing under a disc of the ground (`Terrain.voidUnder`). */
194
+ interface TerrainVoid {
195
+ /** Metres under the untouched surface at the deepest vertex. */
196
+ readonly depth: number;
197
+ /** Metres under the untouched surface, averaged over the disc. */
198
+ readonly mean: number;
199
+ /** 0..1, the share of the disc's vertices that are void (dug at least `VOID_MIN_DEPTH`). */
200
+ readonly fraction: number;
187
201
  }
188
202
  /** A fine mark: a bullet pock or a chip, smaller than a cell. It grows with the blows on it until it is dug into the height field. */
189
203
  interface TerrainMark {
@@ -368,6 +382,8 @@ interface Terrain {
368
382
  applyHit(hit: Hit): TerrainBlowResult | null;
369
383
  /** What stands still on the ground now. Where it stands over bare bars they sag under it, and past what they hold they give way; a slab over a void then opens. True when it opened. */
370
384
  bear(loads: readonly TerrainLoad[]): boolean;
385
+ /** How much ground is missing under the disc (x, z, radius), measured from the untouched surface. */
386
+ voidUnder(x: number, z: number, radius: number): TerrainVoid;
371
387
  /** Height a foot stands at: the ground less what it sinks in. */
372
388
  footAt(x: number, z: number): number;
373
389
  /** What is left of the walking speed here, 0..1. */
@@ -437,6 +453,13 @@ interface TerrainImplOptions {
437
453
  */
438
454
  declare function createTerrainImpl(terrain: Terrain, options: TerrainImplOptions): DestructibleImpl;
439
455
 
456
+ /** A loss shallower than this (m below the untouched surface) is a scuff, not a void anything bears over. */
457
+ declare const GROUND_LOSS_MIN_DEPTH = 0.02;
458
+ /** Sand that slid tells the host only where it lowered the ground by at least this much (m). */
459
+ declare const SLIDE_LOSS_MIN_LOWERING = 0.05;
460
+ /** Under this depth (m) a vertex still counts as ground in `voidUnder`. */
461
+ declare const VOID_MIN_DEPTH = 0.02;
462
+
440
463
  declare const TERRAIN_MATERIALS: readonly TerrainMaterial[];
441
464
  declare const TERRAIN_PROFILES: readonly TerrainProfile[];
442
465
  /** About twelve grenades (200 kJ) on the same spot of bare bars. */
@@ -497,4 +520,4 @@ interface WaterLift {
497
520
  /** Archimedes on a body in a liquid: what it displaces lifts it, so a light log floats and a concrete block goes to the bed. */
498
521
  declare function waterLift(params: WaterLiftParams): WaterLift;
499
522
 
500
- export { CRACK_REFERENCE_JOULES, type CrackSegment, DEFAULT_BAR_DIAMETER, DEFAULT_BAR_YIELD_JOULES, DEFAULT_BAR_YIELD_PA, DEFAULT_PROFILE, DEFAULT_TERRAIN_BUDGET, type Footprints, TERRAIN_MATERIALS, TERRAIN_PATTERNS, TERRAIN_PROFILES, type Terrain, type TerrainBlow, type TerrainBlowResponse, type TerrainBlowResult, type TerrainBreachResponse, type TerrainBudget, type TerrainChunk, type TerrainColumn, type TerrainCrack, type TerrainCrackResponse, type TerrainDebris, type TerrainDebrisKind, type TerrainDecal, type TerrainDecalKind, type TerrainFireResponse, type TerrainFooting, type TerrainImplOptions, type TerrainLoad, type TerrainMark, type TerrainMaterial, type TerrainOptions, type TerrainPattern, type TerrainProfile, type TerrainRegion, type TerrainSnapshot, type TerrainStats, type TerrainStratum, type TerrainStrike, type TerrainUnits, type TerrainWater, type TerrainWaterHere, type WaterLift, type WaterLiftParams, crackLines, createFootprints, createTerrain, createTerrainImpl, waterLift };
523
+ export { CRACK_REFERENCE_JOULES, type CrackSegment, DEFAULT_BAR_DIAMETER, DEFAULT_BAR_YIELD_JOULES, DEFAULT_BAR_YIELD_PA, DEFAULT_PROFILE, DEFAULT_TERRAIN_BUDGET, type Footprints, GROUND_LOSS_MIN_DEPTH, SLIDE_LOSS_MIN_LOWERING, TERRAIN_MATERIALS, TERRAIN_PATTERNS, TERRAIN_PROFILES, type Terrain, type TerrainBlow, type TerrainBlowResponse, type TerrainBlowResult, type TerrainBreachResponse, type TerrainBudget, type TerrainChunk, type TerrainColumn, type TerrainCrack, type TerrainCrackResponse, type TerrainDebris, type TerrainDebrisKind, type TerrainDecal, type TerrainDecalKind, type TerrainFireResponse, type TerrainFooting, type TerrainImplOptions, type TerrainLoad, type TerrainMark, type TerrainMaterial, type TerrainOptions, type TerrainPattern, type TerrainProfile, type TerrainRegion, type TerrainSnapshot, type TerrainStats, type TerrainStratum, type TerrainStrike, type TerrainUnits, type TerrainVoid, type TerrainWater, type TerrainWaterHere, VOID_MIN_DEPTH, type WaterLift, type WaterLiftParams, crackLines, createFootprints, createTerrain, createTerrainImpl, waterLift };
@@ -27,9 +27,12 @@ var GameTerrain = (() => {
27
27
  DEFAULT_BAR_YIELD_PA: () => DEFAULT_BAR_YIELD_PA,
28
28
  DEFAULT_PROFILE: () => DEFAULT_PROFILE,
29
29
  DEFAULT_TERRAIN_BUDGET: () => DEFAULT_TERRAIN_BUDGET,
30
+ GROUND_LOSS_MIN_DEPTH: () => GROUND_LOSS_MIN_DEPTH,
31
+ SLIDE_LOSS_MIN_LOWERING: () => SLIDE_LOSS_MIN_LOWERING,
30
32
  TERRAIN_MATERIALS: () => TERRAIN_MATERIALS,
31
33
  TERRAIN_PATTERNS: () => TERRAIN_PATTERNS,
32
34
  TERRAIN_PROFILES: () => TERRAIN_PROFILES,
35
+ VOID_MIN_DEPTH: () => VOID_MIN_DEPTH,
33
36
  crackLines: () => crackLines,
34
37
  createFootprints: () => createFootprints,
35
38
  createTerrain: () => createTerrain,
@@ -444,6 +447,72 @@ var GameTerrain = (() => {
444
447
  };
445
448
  }
446
449
 
450
+ // src/ground-loss.ts
451
+ var GROUND_LOSS_MIN_DEPTH = 0.02;
452
+ var SLIDE_LOSS_MIN_LOWERING = 0.05;
453
+ var VOID_MIN_DEPTH = 0.02;
454
+ function tallyLowered(tally, index, amount) {
455
+ if (amount !== 0) tally.set(index, (tally.get(index) ?? 0) + amount);
456
+ }
457
+ function lossOf(grid, tally, spec) {
458
+ let weight = 0;
459
+ let centerX = 0;
460
+ let centerZ = 0;
461
+ let depth = 0;
462
+ let deepest = 0;
463
+ const kept = [];
464
+ for (const [index, amount] of tally) {
465
+ if (amount < spec.minLowering || amount <= 0) continue;
466
+ const column = index % grid.stride;
467
+ const row = (index - column) / grid.stride;
468
+ weight += amount;
469
+ centerX += (grid.x0 + column * grid.cellSize) * amount;
470
+ centerZ += (grid.z0 + row * grid.cellSize) * amount;
471
+ depth = Math.max(depth, grid.dug[index]);
472
+ deepest = Math.max(deepest, amount);
473
+ kept.push(index);
474
+ }
475
+ if (weight <= 0 || depth < GROUND_LOSS_MIN_DEPTH || deepest < GROUND_LOSS_MIN_DEPTH) return null;
476
+ centerX /= weight;
477
+ centerZ /= weight;
478
+ let radius = 0;
479
+ for (const index of kept) {
480
+ if (spec.shape === "bowl" && grid.dug[index] < depth / 2) continue;
481
+ const column = index % grid.stride;
482
+ const row = (index - column) / grid.stride;
483
+ radius = Math.max(radius, Math.hypot(grid.x0 + column * grid.cellSize - centerX, grid.z0 + row * grid.cellSize - centerZ));
484
+ }
485
+ const reach = spec.shape === "bowl" ? Math.SQRT2 * (radius + grid.cellSize / 2) : radius + grid.cellSize / 2;
486
+ return { x: centerX, z: centerZ, radius: reach, depth, removed: weight * grid.cellSize * grid.cellSize, source: spec.source };
487
+ }
488
+ function voidUnder(grid, spec) {
489
+ const { x, z, radius } = spec;
490
+ const clamp = (value, top) => Math.max(0, Math.min(top, value));
491
+ const column0 = clamp(Math.ceil((x - radius - grid.x0) / grid.cellSize), grid.columns);
492
+ const column1 = clamp(Math.floor((x + radius - grid.x0) / grid.cellSize), grid.columns);
493
+ const row0 = clamp(Math.ceil((z - radius - grid.z0) / grid.cellSize), grid.rows);
494
+ const row1 = clamp(Math.floor((z + radius - grid.z0) / grid.cellSize), grid.rows);
495
+ let count = 0;
496
+ let sum = 0;
497
+ let depth = 0;
498
+ let open = 0;
499
+ for (let row = row0; row <= row1; row++) {
500
+ for (let column = column0; column <= column1; column++) {
501
+ if (Math.hypot(grid.x0 + column * grid.cellSize - x, grid.z0 + row * grid.cellSize - z) > radius) continue;
502
+ const here = grid.dug[row * grid.stride + column];
503
+ count++;
504
+ sum += here;
505
+ depth = Math.max(depth, here);
506
+ if (here >= VOID_MIN_DEPTH) open++;
507
+ }
508
+ }
509
+ if (count === 0) {
510
+ 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)];
511
+ return { depth: here, mean: here, fraction: here >= VOID_MIN_DEPTH ? 1 : 0 };
512
+ }
513
+ return { depth, mean: sum / count, fraction: open / count };
514
+ }
515
+
447
516
  // src/marks.ts
448
517
  var SAME_HOLE = 0.75;
449
518
  function createMarkStore(budget) {
@@ -684,6 +753,17 @@ var GameTerrain = (() => {
684
753
  const emitDeform = (x, z, radius) => {
685
754
  for (const listener of deformListeners) listener({ x, z, radius });
686
755
  };
756
+ const grid = { x0, z0, cellSize, stride, columns, rows, dug };
757
+ const slideTally = /* @__PURE__ */ new Map();
758
+ const emitGroundLoss = (tally, source, minLowering) => {
759
+ if (!options.onGroundLoss) return;
760
+ const loss = lossOf(grid, tally, { source, minLowering, shape: source === "slab" ? "flat" : "bowl" });
761
+ if (loss) options.onGroundLoss(loss);
762
+ };
763
+ const dropVertex = (index, depth, tally) => {
764
+ tallyLowered(tally, index, depth - dug[index]);
765
+ dug[index] = depth;
766
+ };
687
767
  const chunkOfVertex = (column, row) => Math.min(chunksZ - 1, Math.floor(row / chunkCells)) * chunksX + Math.min(chunksX - 1, Math.floor(column / chunkCells));
688
768
  function touchVertex(column, row) {
689
769
  const chunk2 = chunkOfVertex(column, row);
@@ -843,7 +923,7 @@ var GameTerrain = (() => {
843
923
  loose[index] = loose[index] + amount - filled;
844
924
  }
845
925
  const isSoftExposed = (index) => materials.get(stratumAtDepth(profileList[profileIndex[index]], dug[index]).stratum.material).reposeDegrees !== void 0;
846
- function stampBowl(x, z, radius, depth, rimVolume) {
926
+ function stampBowl(x, z, radius, depth, rimVolume, lossSource = "crater") {
847
927
  const reach = radius * RIM_REACH;
848
928
  const column0 = clampIndex(Math.floor((x - reach - x0) / cellSize), columns);
849
929
  const column1 = clampIndex(Math.ceil((x + reach - x0) / cellSize), columns);
@@ -865,12 +945,15 @@ var GameTerrain = (() => {
865
945
  }
866
946
  }
867
947
  const cellArea = cellSize * cellSize;
948
+ const tally = /* @__PURE__ */ new Map();
868
949
  for (let row = row0; row <= row1; row++) {
869
950
  for (let column = column0; column <= column1; column++) {
870
951
  const q = shapeOf(x0 + column * cellSize, z0 + row * cellSize);
871
952
  const index = row * stride + column;
872
953
  if (q < 1) {
954
+ const before = dug[index];
873
955
  removeFromVertex(index, depth * (1 - q * q) * (0.92 + random() * 0.16));
956
+ tallyLowered(tally, index, dug[index] - before);
874
957
  sliding.add(index);
875
958
  touchVertex(column, row);
876
959
  } else if (q < RIM_REACH && rimWeight > 0) {
@@ -883,6 +966,7 @@ var GameTerrain = (() => {
883
966
  marks.removeInside(x, z, radius);
884
967
  clearDecalsInside(x, z, radius);
885
968
  emitDeform(x, z, reach);
969
+ if (lossSource) emitGroundLoss(tally, lossSource, 0);
886
970
  }
887
971
  function throwPieces(spec) {
888
972
  const radius = PIECE_RADIUS[spec.material.debris];
@@ -979,6 +1063,7 @@ var GameTerrain = (() => {
979
1063
  let collapsed = loadBars(blow, spec.energy, x, z, profile);
980
1064
  const crack = crackAround(blow, spec.energy, x, z, start.loose > 0 ? void 0 : firstMaterial);
981
1065
  const extra = { ...crack ? { crack } : {}, ...breached ? { breached } : {}, ...waterResult };
1066
+ const pressed = spec.contactRadius !== void 0 && spec.contactRadius >= radius;
982
1067
  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 };
983
1068
  if (firstMaterial.reposeDegrees === void 0 && start.loose <= 0) radius *= 1 + RIGID_WIDENING * (start.depth / maxDepthOfProfile(profile));
984
1069
  const volumeTotal = removed.reduce((sum, entry) => sum + entry.volume, 0);
@@ -986,7 +1071,7 @@ var GameTerrain = (() => {
986
1071
  if (fine) depositMark({ x, z, radius, depth: carved, blow, profile, cap: maxDepthOfProfile(profile) });
987
1072
  else {
988
1073
  const rimShare = materials.get(removed[0].material).rimShare ?? 0;
989
- stampBowl(x, z, radius, carved, volumeTotal * rimShare);
1074
+ stampBowl(x, z, radius, carved, volumeTotal * rimShare, pressed ? null : "crater");
990
1075
  }
991
1076
  if (firstMaterial.cracks) addDecal({ kind: "crack", x, z, radius: Math.max(CRACK_MIN_RADIUS, radius * firstMaterial.cracks), angle: random() * 6.283, material: firstMaterial.id });
992
1077
  let quota = waterHere && blow !== "explosive" ? 0 : budget.maxDebrisPerBlow;
@@ -1088,6 +1173,7 @@ var GameTerrain = (() => {
1088
1173
  const row1 = clampIndex(Math.ceil((z + reach - z0) / cellSize), rows);
1089
1174
  const limit = maxDepthOfProfile(profile);
1090
1175
  let collapsed = false;
1176
+ const tally = /* @__PURE__ */ new Map();
1091
1177
  for (let row = row0; row <= row1; row++) {
1092
1178
  for (let column = column0; column <= column1; column++) {
1093
1179
  const index = row * stride + column;
@@ -1097,16 +1183,17 @@ var GameTerrain = (() => {
1097
1183
  barBend[index] = barBend[index] + energy / yieldJoules * (1 - q * q);
1098
1184
  touchVertex(column, row);
1099
1185
  if (barBend[index] >= 1 && profile.drop !== void 0 && dug[index] >= limit * COLLAPSE_DEPTH_SHARE && dug[index] < limit + profile.drop) {
1100
- dug[index] = limit + profile.drop;
1186
+ dropVertex(index, limit + profile.drop, tally);
1101
1187
  collapsed = true;
1102
1188
  }
1103
1189
  }
1104
1190
  }
1105
- if (collapsed) giveWay({ x, z, reach, profile });
1191
+ if (collapsed) giveWay({ x, z, reach, profile, tally });
1192
+ emitGroundLoss(tally, "slab", 0);
1106
1193
  return collapsed;
1107
1194
  }
1108
1195
  function giveWay(spec) {
1109
- const { x, z, reach, profile } = spec;
1196
+ const { x, z, reach, profile, tally } = spec;
1110
1197
  emitDeform(x, z, reach);
1111
1198
  if (profile.drop === void 0) return;
1112
1199
  const limit = maxDepthOfProfile(profile);
@@ -1118,7 +1205,7 @@ var GameTerrain = (() => {
1118
1205
  for (let column = column0; column <= column1; column++) {
1119
1206
  const index = row * stride + column;
1120
1207
  if (barBend[index] >= COLLAPSE_SPREAD_BEND && dug[index] < limit + profile.drop) {
1121
- dug[index] = limit + profile.drop;
1208
+ dropVertex(index, limit + profile.drop, tally);
1122
1209
  touchVertex(column, row);
1123
1210
  }
1124
1211
  }
@@ -1142,6 +1229,7 @@ var GameTerrain = (() => {
1142
1229
  if (spread) touched2.push({ load, ...spread });
1143
1230
  }
1144
1231
  let opened = false;
1232
+ const tally = /* @__PURE__ */ new Map();
1145
1233
  for (const [index, bend] of bends) {
1146
1234
  if (barBend[index] >= bend) continue;
1147
1235
  barBend[index] = bend;
@@ -1151,11 +1239,12 @@ var GameTerrain = (() => {
1151
1239
  const profile = profileList[profileIndex[index]];
1152
1240
  const limit = maxDepthOfProfile(profile);
1153
1241
  if (bend >= 1 && profile.drop !== void 0 && dug[index] >= limit * COLLAPSE_DEPTH_SHARE && dug[index] < limit + profile.drop) {
1154
- dug[index] = limit + profile.drop;
1242
+ dropVertex(index, limit + profile.drop, tally);
1155
1243
  opened = true;
1156
1244
  }
1157
1245
  }
1158
- if (opened) for (const { load, reach, profile } of touched2) giveWay({ x: load.x, z: load.z, reach, profile });
1246
+ if (opened) for (const { load, reach, profile } of touched2) giveWay({ x: load.x, z: load.z, reach, profile, tally });
1247
+ emitGroundLoss(tally, "slab", 0);
1159
1248
  return opened;
1160
1249
  }
1161
1250
  function spreadLoad(load, bends) {
@@ -1314,10 +1403,14 @@ var GameTerrain = (() => {
1314
1403
  if (nextColumn < 0 || nextRow < 0 || nextColumn > columns || nextRow > rows) continue;
1315
1404
  const excess = height - vertexHeight(nextColumn, nextRow) - slope;
1316
1405
  if (excess <= SLIDE_EPSILON) continue;
1406
+ const before = dug[index];
1317
1407
  const moved = removeFromVertex(index, excess * SLIDE_SHARE);
1318
1408
  if (moved <= 0) continue;
1409
+ tallyLowered(slideTally, index, dug[index] - before);
1319
1410
  const next = nextRow * stride + nextColumn;
1411
+ const nextBefore = dug[next];
1320
1412
  addToVertex(next, moved, isSoftExposed(next));
1413
+ tallyLowered(slideTally, next, dug[next] - nextBefore);
1321
1414
  sliding.add(index);
1322
1415
  sliding.add(next);
1323
1416
  touchVertex(column, row);
@@ -1331,6 +1424,10 @@ var GameTerrain = (() => {
1331
1424
  }
1332
1425
  }
1333
1426
  if (minX <= maxX) emitDeform((minX + maxX) / 2, (minZ + maxZ) / 2, Math.hypot(maxX - minX, maxZ - minZ) / 2 + cellSize * 2);
1427
+ if (sliding.size === 0 && slideTally.size > 0) {
1428
+ emitGroundLoss(slideTally, "slide", SLIDE_LOSS_MIN_LOWERING);
1429
+ slideTally.clear();
1430
+ }
1334
1431
  }
1335
1432
  function settle(piece) {
1336
1433
  const material = materials.get(piece.material);
@@ -1417,6 +1514,7 @@ var GameTerrain = (() => {
1417
1514
  unitDamage.clear();
1418
1515
  debrisStore.clear();
1419
1516
  sliding.clear();
1517
+ slideTally.clear();
1420
1518
  burning.clear();
1421
1519
  random = createRandom(seed);
1422
1520
  applyLayout(currentLayout);
@@ -1467,6 +1565,7 @@ var GameTerrain = (() => {
1467
1565
  burn,
1468
1566
  applyHit,
1469
1567
  bear: bearAll,
1568
+ voidUnder: (x, z, radius) => voidUnder(grid, { x, z, radius }),
1470
1569
  onDeform(listener) {
1471
1570
  deformListeners.add(listener);
1472
1571
  return () => deformListeners.delete(listener);