@adatechnology/game-terrain 0.1.2 → 0.2.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
@@ -65,6 +65,7 @@ var TERRAIN_MATERIALS = [
65
65
  debris: "chips",
66
66
  color: [0.05, 0.05, 0.055],
67
67
  pattern: "asphalt",
68
+ crack: { reach: 0.6, width: 8e-3, thresholdJoules: 800 },
68
69
  hits: {
69
70
  ballistic: { specificEnergy: 3e7, aspect: 0.2, minRadius: 0.03 },
70
71
  blunt: { specificEnergy: 1e8, aspect: 0.1, thresholdJoules: 400 },
@@ -78,6 +79,8 @@ var TERRAIN_MATERIALS = [
78
79
  debris: "chips",
79
80
  color: [0.33, 0.33, 0.31],
80
81
  pattern: "concrete",
82
+ crack: { reach: 1, width: 6e-3, thresholdJoules: 300 },
83
+ breach: { joulesPerCubedThickness: 24e8, radiusOverThickness: 1.5 },
81
84
  hits: {
82
85
  ballistic: { specificEnergy: 15e7, aspect: 0.14, minRadius: 0.018 },
83
86
  blunt: { specificEnergy: 3e8, aspect: 0.08, thresholdJoules: 800 },
@@ -104,6 +107,7 @@ var TERRAIN_MATERIALS = [
104
107
  debris: "shards",
105
108
  color: [0.72, 0.7, 0.66],
106
109
  pattern: "tile",
110
+ crack: { reach: 0.5, width: 2e-3, thresholdJoules: 40 },
107
111
  hits: {
108
112
  ballistic: { specificEnergy: 8e6, aspect: 0.1, minRadius: 0.05 },
109
113
  blunt: { specificEnergy: 1e7, aspect: 0.08, thresholdJoules: 80, minRadius: 0.06 },
@@ -130,6 +134,7 @@ var TERRAIN_MATERIALS = [
130
134
  debris: "chips",
131
135
  color: [0.23, 0.22, 0.2],
132
136
  pattern: "stone",
137
+ crack: { reach: 0.5, width: 4e-3, thresholdJoules: 1e3 },
133
138
  hits: {
134
139
  ballistic: { specificEnergy: 25e7, aspect: 0.12, minRadius: 0.015 },
135
140
  blunt: { specificEnergy: 5e8, aspect: 0.08, thresholdJoules: 1500 },
@@ -153,6 +158,95 @@ var TERRAIN_PROFILES = [
153
158
  var DEFAULT_BAR_YIELD_JOULES = 24e5;
154
159
  var DEFAULT_PROFILE = "concrete_slab";
155
160
 
161
+ // src/random.ts
162
+ function createRandom(seed) {
163
+ let state = seed | 0;
164
+ return () => {
165
+ state = state + 1831565813 | 0;
166
+ let mixed = Math.imul(state ^ state >>> 15, 1 | state);
167
+ mixed = mixed + Math.imul(mixed ^ mixed >>> 7, 61 | mixed) ^ mixed;
168
+ return ((mixed ^ mixed >>> 14) >>> 0) / 4294967296;
169
+ };
170
+ }
171
+
172
+ // src/cracks.ts
173
+ var CRACK_REFERENCE_JOULES = 1e6;
174
+ var MERGE_SHARE = 0.35;
175
+ var HAIRLINE = 15e-4;
176
+ var MIN_RAYS = 4;
177
+ var MAX_RAYS = 11;
178
+ var SEGMENTS = 4;
179
+ var BRANCH_CHANCE = 0.45;
180
+ function createCrackStore() {
181
+ const list = [];
182
+ let version = 0;
183
+ let nextId = 1;
184
+ let born = 0;
185
+ return {
186
+ list,
187
+ get version() {
188
+ return version;
189
+ },
190
+ add(spec, max) {
191
+ version++;
192
+ const near = list.find((crack2) => Math.hypot(crack2.x - spec.x, crack2.z - spec.z) < crack2.reach * MERGE_SHARE);
193
+ if (near) {
194
+ near.reach = Math.max(near.reach, spec.reach) + Math.min(near.reach, spec.reach) * 0.25;
195
+ near.width = Math.max(near.width, spec.width) * 1.1;
196
+ near.rays = Math.min(MAX_RAYS, near.rays + 1);
197
+ return near;
198
+ }
199
+ if (list.length >= max) list.shift();
200
+ const rays = MIN_RAYS + Math.floor(createRandom(spec.seed)() * (MAX_RAYS - MIN_RAYS - 2));
201
+ const crack = { id: nextId++, x: spec.x, z: spec.z, reach: spec.reach, width: Math.max(HAIRLINE, spec.width), rays, seed: spec.seed, material: spec.material, born: born++ };
202
+ list.push(crack);
203
+ return crack;
204
+ },
205
+ clear() {
206
+ list.length = 0;
207
+ version++;
208
+ },
209
+ load(cracks) {
210
+ list.length = 0;
211
+ for (const crack of cracks) {
212
+ list.push({ ...crack });
213
+ nextId = Math.max(nextId, crack.id + 1);
214
+ born = Math.max(born, crack.born + 1);
215
+ }
216
+ version++;
217
+ }
218
+ };
219
+ }
220
+ function crackLines(crack) {
221
+ const random = createRandom(crack.seed);
222
+ const segments = [];
223
+ const widthAt = (distance) => Math.max(HAIRLINE, crack.width * (1 - distance / crack.reach));
224
+ for (let ray = 0; ray < crack.rays; ray++) {
225
+ let angle = ray / crack.rays * Math.PI * 2 + (random() - 0.5) * 0.6;
226
+ const length = crack.reach * (0.45 + 0.55 * random());
227
+ let x = crack.x;
228
+ let z = crack.z;
229
+ let run = 0;
230
+ for (let segment = 0; segment < SEGMENTS; segment++) {
231
+ const step = length / SEGMENTS * (0.75 + 0.5 * random());
232
+ angle += (random() - 0.5) * 0.5;
233
+ const nx = x + Math.cos(angle) * step;
234
+ const nz = z + Math.sin(angle) * step;
235
+ segments.push({ x0: x, z0: z, x1: nx, z1: nz, w0: widthAt(run), w1: widthAt(run + step) });
236
+ if (segment > 0 && random() < BRANCH_CHANCE) {
237
+ const side = angle + (random() < 0.5 ? -1 : 1) * (0.45 + random() * 0.5);
238
+ const branch = step * (0.5 + 0.6 * random());
239
+ const w = widthAt(run + step) * 0.6;
240
+ segments.push({ x0: nx, z0: nz, x1: nx + Math.cos(side) * branch, z1: nz + Math.sin(side) * branch, w0: Math.max(HAIRLINE, w), w1: HAIRLINE });
241
+ }
242
+ run += step;
243
+ x = nx;
244
+ z = nz;
245
+ }
246
+ }
247
+ return segments;
248
+ }
249
+
156
250
  // src/debris.ts
157
251
  var REST_SPEED = 0.6;
158
252
  var RESTITUTION = 0.25;
@@ -334,17 +428,6 @@ function createMarkStore(budget) {
334
428
  };
335
429
  }
336
430
 
337
- // src/random.ts
338
- function createRandom(seed) {
339
- let state = seed | 0;
340
- return () => {
341
- state = state + 1831565813 | 0;
342
- let mixed = Math.imul(state ^ state >>> 15, 1 | state);
343
- mixed = mixed + Math.imul(mixed ^ mixed >>> 7, 61 | mixed) ^ mixed;
344
- return ((mixed ^ mixed >>> 14) >>> 0) / 4294967296;
345
- };
346
- }
347
-
348
431
  // src/terrain.ts
349
432
  var DEFAULT_TERRAIN_BUDGET = {
350
433
  markAreaSize: 2,
@@ -354,7 +437,8 @@ var DEFAULT_TERRAIN_BUDGET = {
354
437
  maxDebris: 400,
355
438
  maxDebrisPerBlow: 36,
356
439
  maxSlideCellsPerFrame: 2500,
357
- maxBurningCells: 300
440
+ maxBurningCells: 300,
441
+ maxCracks: 120
358
442
  };
359
443
  var GRAVITY = 9.81;
360
444
  var FINE_CELLS = 1.5;
@@ -397,6 +481,16 @@ function stratumAtDepth(profile, depth) {
397
481
  const stratum = profile.strata[index];
398
482
  return { index, stratum, top: top - stratum.thickness };
399
483
  }
484
+ function stratumMaterialAtDepth(profile, depth) {
485
+ const strata = profile.strata;
486
+ let top = 0;
487
+ for (let index = 0; index < strata.length; index++) {
488
+ const stratum = strata[index];
489
+ if (depth < top + stratum.thickness - 1e-6) return stratum.material;
490
+ top += stratum.thickness;
491
+ }
492
+ return strata[strata.length - 1].material;
493
+ }
400
494
  var totalThickness = (profile) => profile.strata.reduce((sum, stratum) => sum + stratum.thickness, 0);
401
495
  function createTerrain(options) {
402
496
  const [x0, z0, x1, z1] = options.bounds;
@@ -431,6 +525,7 @@ function createTerrain(options) {
431
525
  const mixed = new Uint8Array(chunksX * chunksZ);
432
526
  const dirty = /* @__PURE__ */ new Set();
433
527
  const marks = createMarkStore(budget);
528
+ const cracks = createCrackStore();
434
529
  const debrisStore = createDebrisStore(() => budget.maxDebris);
435
530
  const sliding = /* @__PURE__ */ new Set();
436
531
  const burning = /* @__PURE__ */ new Map();
@@ -511,7 +606,7 @@ function createTerrain(options) {
511
606
  };
512
607
  function vertexMaterial(column, row) {
513
608
  const index = row * stride + column;
514
- return materials.get(stratumAtDepth(profileList[profileIndex[index]], dug[index]).stratum.material);
609
+ return materials.get(stratumMaterialAtDepth(profileList[profileIndex[index]], dug[index]));
515
610
  }
516
611
  function columnAt(x, z) {
517
612
  const { index } = vertexOf(x, z);
@@ -607,6 +702,7 @@ function createTerrain(options) {
607
702
  let depth = start.depth;
608
703
  let radius = 0;
609
704
  let carved = 0;
705
+ let breached = false;
610
706
  const firstMaterial = materials.get(start.material);
611
707
  if (start.loose <= 0) {
612
708
  for (; ; ) {
@@ -618,6 +714,19 @@ function createTerrain(options) {
618
714
  const response = material.hits[blow];
619
715
  if (!response || energy < (response.thresholdJoules ?? 0)) break;
620
716
  const bars = layer.stratum.reinforcement;
717
+ const breach = blow === "explosive" ? material.breach : void 0;
718
+ const breachJoules = breach ? breach.joulesPerCubedThickness * layer.stratum.thickness ** 3 : Infinity;
719
+ if (breach && depth <= layer.top + 1e-6 && energy >= breachJoules) {
720
+ const takenRadius2 = layer.stratum.thickness * breach.radiusOverThickness * Math.cbrt(energy / breachJoules);
721
+ const takenVolume2 = Math.PI * takenRadius2 * takenRadius2 * room / 2;
722
+ energy -= breachJoules;
723
+ depth += room;
724
+ carved += room;
725
+ breached = true;
726
+ radius = Math.max(radius, takenRadius2);
727
+ removed.push({ material: material.id, volume: takenVolume2 });
728
+ continue;
729
+ }
621
730
  const pastCover = bars !== void 0 && depth - layer.top >= bars.cover - 1e-6;
622
731
  const specificEnergy = response.specificEnergy * (pastCover ? bars.resistance : 1);
623
732
  const limit = bars && !pastCover ? Math.min(room, layer.top + bars.cover - depth) : room;
@@ -645,8 +754,10 @@ function createTerrain(options) {
645
754
  removed.push({ material: firstMaterial.id, volume });
646
755
  }
647
756
  }
648
- const collapsed = loadBars(blow, spec.energy, x, z, profile);
649
- 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 };
757
+ let collapsed = loadBars(blow, spec.energy, x, z, profile);
758
+ const crack = crackAround(blow, spec.energy, x, z, start.loose > 0 ? void 0 : firstMaterial);
759
+ const extra = { ...crack ? { crack } : {}, ...breached ? { breached } : {} };
760
+ 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 };
650
761
  if (firstMaterial.reposeDegrees === void 0 && start.loose <= 0) radius *= 1 + RIGID_WIDENING * (start.depth / maxDepthOfProfile(profile));
651
762
  const volumeTotal = removed.reduce((sum, entry) => sum + entry.volume, 0);
652
763
  const fine = radius < cellSize * FINE_CELLS;
@@ -664,8 +775,16 @@ function createTerrain(options) {
664
775
  throwPieces({ x, y: point[1], z, volume: thrown, material, energy: spec.energy * (entry.volume / volumeTotal), direction: spec.direction, quota: share, bowlRadius: radius, blow });
665
776
  quota -= share;
666
777
  }
778
+ if (breached) collapsed = loadBars(blow, spec.energy, x, z, profile) || collapsed;
667
779
  const after = columnAt(x, z);
668
- return { point, radius, depth: carved, totalDepth: after.depth, removed, exposed: after.material, atLimit: after.remaining <= 1e-4, rebarExposed: rebarShowing(profile, after.depth), collapsed, fine };
780
+ return { point, radius, depth: carved, totalDepth: after.depth, removed, exposed: after.material, atLimit: after.remaining <= 1e-4, rebarExposed: rebarShowing(profile, after.depth), collapsed, fine, ...extra };
781
+ }
782
+ function crackAround(blow, energy, x, z, material) {
783
+ const response = material?.crack;
784
+ if (!material || !response || energy < response.thresholdJoules) return void 0;
785
+ if (blow === "slash") return void 0;
786
+ const scale = Math.cbrt(energy / CRACK_REFERENCE_JOULES);
787
+ return cracks.add({ x, z, reach: response.reach * scale, width: response.width * scale, material: material.id, seed: Math.floor(random() * 2 ** 31) }, budget.maxCracks);
669
788
  }
670
789
  function loadBars(blow, energy, x, z, profile) {
671
790
  const bars = profile.strata.find((stratum) => stratum.reinforcement);
@@ -906,6 +1025,7 @@ function createTerrain(options) {
906
1025
  barBend.fill(0);
907
1026
  touched.fill(0);
908
1027
  marks.clear();
1028
+ cracks.clear();
909
1029
  debrisStore.clear();
910
1030
  sliding.clear();
911
1031
  burning.clear();
@@ -957,6 +1077,9 @@ function createTerrain(options) {
957
1077
  updateSlides();
958
1078
  updateFire(dt);
959
1079
  },
1080
+ get dirtyChunkCount() {
1081
+ return dirty.size;
1082
+ },
960
1083
  drainDirtyChunks(max = budget.maxDirtyChunksPerFrame) {
961
1084
  const taken = [];
962
1085
  for (const index of dirty) {
@@ -968,6 +1091,10 @@ function createTerrain(options) {
968
1091
  },
969
1092
  chunk,
970
1093
  marks: () => marks.list,
1094
+ cracks: () => cracks.list,
1095
+ get cracksVersion() {
1096
+ return cracks.version;
1097
+ },
971
1098
  debris: () => debrisStore.list,
972
1099
  burningPoints: () => [...burning.keys()].map((key) => {
973
1100
  const cellRow = Math.floor(key / 65536);
@@ -980,7 +1107,7 @@ function createTerrain(options) {
980
1107
  stats: () => ({ marks: marks.list.length, debris: debrisStore.list.length, debrisMoving: debrisStore.list.filter((piece) => !piece.atRest).length, dirtyChunks: dirty.size, burning: burning.size }),
981
1108
  reset,
982
1109
  serialize() {
983
- return { layout: currentLayout, dug: sparse(dug), loose: sparse(loose), scorch: sparse(scorch), barBend: sparse(barBend), marks: marks.list.map((mark) => ({ ...mark })) };
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 })) };
984
1111
  },
985
1112
  load(snapshot) {
986
1113
  currentLayout = snapshot.layout;
@@ -990,6 +1117,7 @@ function createTerrain(options) {
990
1117
  fillSparse(scorch, snapshot.scorch);
991
1118
  fillSparse(barBend, snapshot.barBend ?? []);
992
1119
  marks.load(snapshot.marks);
1120
+ cracks.load(snapshot.cracks ?? []);
993
1121
  for (let chunkIndex = 0; chunkIndex < touched.length; chunkIndex++) markChunk(chunkIndex);
994
1122
  }
995
1123
  };
@@ -999,24 +1127,49 @@ function createTerrain(options) {
999
1127
  var PART_ID = "ground";
1000
1128
  var PART_DEPTH = 3;
1001
1129
  var RAY_BACKOFF = 1;
1130
+ var GROUND_HP = 1e12;
1002
1131
  function createTerrainImpl(terrain, options) {
1003
1132
  const { id } = options;
1004
1133
  const [x0, z0, x1, z1] = terrain.bounds;
1005
- const spec = () => ({
1134
+ const centerColumn = Math.max(0, Math.min(terrain.columns, Math.round((x1 - x0) / 2 / terrain.cellSize)));
1135
+ const centerRow = Math.max(0, Math.min(terrain.rows, Math.round((z1 - z0) / 2 / terrain.cellSize)));
1136
+ const centerMaterial = () => terrain.vertexMaterial(centerColumn, centerRow).id;
1137
+ const buildSpec = (material) => ({
1006
1138
  name: options.name ?? "Ground",
1007
1139
  parts: [
1008
1140
  {
1009
1141
  id: PART_ID,
1010
- material: terrain.columnAt((x0 + x1) / 2, (z0 + z1) / 2).material,
1142
+ material,
1011
1143
  shape: { kind: "box", size: [x1 - x0, PART_DEPTH, z1 - z0] },
1012
1144
  position: [(x0 + x1) / 2, -PART_DEPTH / 2, (z0 + z1) / 2],
1013
- hp: 1e12,
1145
+ hp: GROUND_HP,
1014
1146
  anchored: true,
1015
1147
  tags: ["terrain"]
1016
1148
  }
1017
1149
  ],
1018
1150
  joints: []
1019
1151
  });
1152
+ let cachedSpec;
1153
+ const spec = () => {
1154
+ const material = centerMaterial();
1155
+ if (cachedSpec?.parts[0]?.material !== material) cachedSpec = buildSpec(material);
1156
+ return cachedSpec;
1157
+ };
1158
+ const buildState = (material) => ({
1159
+ id,
1160
+ position: [0, 0, 0],
1161
+ rotation: [0, 0, 0, 1],
1162
+ parts: [{ id: PART_ID, material, status: "intact", hp: GROUND_HP, maxHp: GROUND_HP, mass: 0, temperature: 0, burning: false, fuel: 0, char: 0, melt: 0, dents: [], holes: [], cracks: [], group: 0 }],
1163
+ joints: [],
1164
+ groups: [[PART_ID]],
1165
+ destroyed: false
1166
+ });
1167
+ let cachedState;
1168
+ const state = () => {
1169
+ const material = centerMaterial();
1170
+ if (cachedState?.parts[0]?.material !== material) cachedState = buildState(material);
1171
+ return cachedState;
1172
+ };
1020
1173
  const resolve = (hit) => {
1021
1174
  if (hit.type !== "ballistic" && hit.type !== "blunt" && hit.type !== "slash") return hit;
1022
1175
  if (!hit.direction) return { ...hit, point: [hit.point[0], terrain.heightAt(hit.point[0], hit.point[2]), hit.point[2]] };
@@ -1034,38 +1187,34 @@ function createTerrainImpl(terrain, options) {
1034
1187
  const result = terrain.applyHit(landed);
1035
1188
  if (!result || result.radius <= 0) return [];
1036
1189
  options.onBlow?.(result, landed);
1037
- return [{ type: "damage", object: id, time: 0, part: PART_ID, amount: 0, hp: 1e12, damageType: hit.type, ...hit.source ? { source: hit.source } : {} }];
1190
+ return [{ type: "damage", object: id, time: 0, part: PART_ID, amount: 0, hp: GROUND_HP, damageType: hit.type, ...hit.source ? { source: hit.source } : {} }];
1038
1191
  },
1039
1192
  update(dt) {
1040
1193
  terrain.update(dt);
1041
1194
  return [];
1042
1195
  },
1043
- state() {
1044
- return {
1045
- id,
1046
- position: [0, 0, 0],
1047
- rotation: [0, 0, 0, 1],
1048
- parts: [],
1049
- joints: [],
1050
- groups: [[PART_ID]],
1051
- destroyed: false
1052
- };
1196
+ state,
1197
+ reset() {
1198
+ terrain.reset();
1053
1199
  },
1054
- reset: () => terrain.reset(),
1055
1200
  serialize: () => terrain.serialize(),
1056
- load: (data) => terrain.load(data)
1201
+ load(data) {
1202
+ terrain.load(data);
1203
+ }
1057
1204
  };
1058
1205
  }
1059
1206
 
1060
1207
  // src/types.ts
1061
1208
  var TERRAIN_PATTERNS = ["plain", "grass", "soil", "sand", "asphalt", "concrete", "tile", "planks", "stone"];
1062
1209
  export {
1210
+ CRACK_REFERENCE_JOULES,
1063
1211
  DEFAULT_BAR_YIELD_JOULES,
1064
1212
  DEFAULT_PROFILE,
1065
1213
  DEFAULT_TERRAIN_BUDGET,
1066
1214
  TERRAIN_MATERIALS,
1067
1215
  TERRAIN_PATTERNS,
1068
1216
  TERRAIN_PROFILES,
1217
+ crackLines,
1069
1218
  createTerrain,
1070
1219
  createTerrainImpl
1071
1220
  };