@adatechnology/game-terrain 0.1.1 → 0.2.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
@@ -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;
@@ -217,6 +311,7 @@ function createDebrisStore(max) {
217
311
  },
218
312
  clear() {
219
313
  list.length = 0;
314
+ serial = 0;
220
315
  }
221
316
  };
222
317
  }
@@ -317,6 +412,7 @@ function createMarkStore(budget) {
317
412
  clear() {
318
413
  list.length = 0;
319
414
  areas.clear();
415
+ serial = 0;
320
416
  version++;
321
417
  },
322
418
  load(marks) {
@@ -332,17 +428,6 @@ function createMarkStore(budget) {
332
428
  };
333
429
  }
334
430
 
335
- // src/random.ts
336
- function createRandom(seed) {
337
- let state = seed | 0;
338
- return () => {
339
- state = state + 1831565813 | 0;
340
- let mixed = Math.imul(state ^ state >>> 15, 1 | state);
341
- mixed = mixed + Math.imul(mixed ^ mixed >>> 7, 61 | mixed) ^ mixed;
342
- return ((mixed ^ mixed >>> 14) >>> 0) / 4294967296;
343
- };
344
- }
345
-
346
431
  // src/terrain.ts
347
432
  var DEFAULT_TERRAIN_BUDGET = {
348
433
  markAreaSize: 2,
@@ -352,7 +437,8 @@ var DEFAULT_TERRAIN_BUDGET = {
352
437
  maxDebris: 400,
353
438
  maxDebrisPerBlow: 36,
354
439
  maxSlideCellsPerFrame: 2500,
355
- maxBurningCells: 300
440
+ maxBurningCells: 300,
441
+ maxCracks: 120
356
442
  };
357
443
  var GRAVITY = 9.81;
358
444
  var FINE_CELLS = 1.5;
@@ -429,10 +515,12 @@ function createTerrain(options) {
429
515
  const mixed = new Uint8Array(chunksX * chunksZ);
430
516
  const dirty = /* @__PURE__ */ new Set();
431
517
  const marks = createMarkStore(budget);
518
+ const cracks = createCrackStore();
432
519
  const debrisStore = createDebrisStore(() => budget.maxDebris);
433
520
  const sliding = /* @__PURE__ */ new Set();
434
521
  const burning = /* @__PURE__ */ new Map();
435
- const random = createRandom(options.seed ?? 1);
522
+ const seed = options.seed ?? 1;
523
+ let random = createRandom(seed);
436
524
  let currentLayout = options.layout ?? { surface: DEFAULT_PROFILE };
437
525
  const deformListeners = /* @__PURE__ */ new Set();
438
526
  const emitDeform = (x, z, radius) => {
@@ -604,6 +692,7 @@ function createTerrain(options) {
604
692
  let depth = start.depth;
605
693
  let radius = 0;
606
694
  let carved = 0;
695
+ let breached = false;
607
696
  const firstMaterial = materials.get(start.material);
608
697
  if (start.loose <= 0) {
609
698
  for (; ; ) {
@@ -615,6 +704,19 @@ function createTerrain(options) {
615
704
  const response = material.hits[blow];
616
705
  if (!response || energy < (response.thresholdJoules ?? 0)) break;
617
706
  const bars = layer.stratum.reinforcement;
707
+ const breach = blow === "explosive" ? material.breach : void 0;
708
+ const breachJoules = breach ? breach.joulesPerCubedThickness * layer.stratum.thickness ** 3 : Infinity;
709
+ if (breach && depth <= layer.top + 1e-6 && energy >= breachJoules) {
710
+ const takenRadius2 = layer.stratum.thickness * breach.radiusOverThickness * Math.cbrt(energy / breachJoules);
711
+ const takenVolume2 = Math.PI * takenRadius2 * takenRadius2 * room / 2;
712
+ energy -= breachJoules;
713
+ depth += room;
714
+ carved += room;
715
+ breached = true;
716
+ radius = Math.max(radius, takenRadius2);
717
+ removed.push({ material: material.id, volume: takenVolume2 });
718
+ continue;
719
+ }
618
720
  const pastCover = bars !== void 0 && depth - layer.top >= bars.cover - 1e-6;
619
721
  const specificEnergy = response.specificEnergy * (pastCover ? bars.resistance : 1);
620
722
  const limit = bars && !pastCover ? Math.min(room, layer.top + bars.cover - depth) : room;
@@ -642,8 +744,10 @@ function createTerrain(options) {
642
744
  removed.push({ material: firstMaterial.id, volume });
643
745
  }
644
746
  }
645
- const collapsed = loadBars(blow, spec.energy, x, z, profile);
646
- 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 };
747
+ let collapsed = loadBars(blow, spec.energy, x, z, profile);
748
+ const crack = crackAround(blow, spec.energy, x, z, start.loose > 0 ? void 0 : firstMaterial);
749
+ const extra = { ...crack ? { crack } : {}, ...breached ? { breached } : {} };
750
+ 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 };
647
751
  if (firstMaterial.reposeDegrees === void 0 && start.loose <= 0) radius *= 1 + RIGID_WIDENING * (start.depth / maxDepthOfProfile(profile));
648
752
  const volumeTotal = removed.reduce((sum, entry) => sum + entry.volume, 0);
649
753
  const fine = radius < cellSize * FINE_CELLS;
@@ -661,8 +765,16 @@ function createTerrain(options) {
661
765
  throwPieces({ x, y: point[1], z, volume: thrown, material, energy: spec.energy * (entry.volume / volumeTotal), direction: spec.direction, quota: share, bowlRadius: radius, blow });
662
766
  quota -= share;
663
767
  }
768
+ if (breached) collapsed = loadBars(blow, spec.energy, x, z, profile) || collapsed;
664
769
  const after = columnAt(x, z);
665
- return { point, radius, depth: carved, totalDepth: after.depth, removed, exposed: after.material, atLimit: after.remaining <= 1e-4, rebarExposed: rebarShowing(profile, after.depth), collapsed, fine };
770
+ 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 };
771
+ }
772
+ function crackAround(blow, energy, x, z, material) {
773
+ const response = material?.crack;
774
+ if (!material || !response || energy < response.thresholdJoules) return void 0;
775
+ if (blow === "slash") return void 0;
776
+ const scale = Math.cbrt(energy / CRACK_REFERENCE_JOULES);
777
+ return cracks.add({ x, z, reach: response.reach * scale, width: response.width * scale, material: material.id, seed: Math.floor(random() * 2 ** 31) }, budget.maxCracks);
666
778
  }
667
779
  function loadBars(blow, energy, x, z, profile) {
668
780
  const bars = profile.strata.find((stratum) => stratum.reinforcement);
@@ -903,9 +1015,11 @@ function createTerrain(options) {
903
1015
  barBend.fill(0);
904
1016
  touched.fill(0);
905
1017
  marks.clear();
1018
+ cracks.clear();
906
1019
  debrisStore.clear();
907
1020
  sliding.clear();
908
1021
  burning.clear();
1022
+ random = createRandom(seed);
909
1023
  applyLayout(currentLayout);
910
1024
  }
911
1025
  applyLayout(currentLayout);
@@ -953,6 +1067,9 @@ function createTerrain(options) {
953
1067
  updateSlides();
954
1068
  updateFire(dt);
955
1069
  },
1070
+ get dirtyChunkCount() {
1071
+ return dirty.size;
1072
+ },
956
1073
  drainDirtyChunks(max = budget.maxDirtyChunksPerFrame) {
957
1074
  const taken = [];
958
1075
  for (const index of dirty) {
@@ -964,6 +1081,10 @@ function createTerrain(options) {
964
1081
  },
965
1082
  chunk,
966
1083
  marks: () => marks.list,
1084
+ cracks: () => cracks.list,
1085
+ get cracksVersion() {
1086
+ return cracks.version;
1087
+ },
967
1088
  debris: () => debrisStore.list,
968
1089
  burningPoints: () => [...burning.keys()].map((key) => {
969
1090
  const cellRow = Math.floor(key / 65536);
@@ -976,7 +1097,7 @@ function createTerrain(options) {
976
1097
  stats: () => ({ marks: marks.list.length, debris: debrisStore.list.length, debrisMoving: debrisStore.list.filter((piece) => !piece.atRest).length, dirtyChunks: dirty.size, burning: burning.size }),
977
1098
  reset,
978
1099
  serialize() {
979
- return { layout: currentLayout, dug: sparse(dug), loose: sparse(loose), scorch: sparse(scorch), barBend: sparse(barBend), marks: marks.list.map((mark) => ({ ...mark })) };
1100
+ 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 })) };
980
1101
  },
981
1102
  load(snapshot) {
982
1103
  currentLayout = snapshot.layout;
@@ -986,6 +1107,7 @@ function createTerrain(options) {
986
1107
  fillSparse(scorch, snapshot.scorch);
987
1108
  fillSparse(barBend, snapshot.barBend ?? []);
988
1109
  marks.load(snapshot.marks);
1110
+ cracks.load(snapshot.cracks ?? []);
989
1111
  for (let chunkIndex = 0; chunkIndex < touched.length; chunkIndex++) markChunk(chunkIndex);
990
1112
  }
991
1113
  };
@@ -1056,12 +1178,14 @@ function createTerrainImpl(terrain, options) {
1056
1178
  // src/types.ts
1057
1179
  var TERRAIN_PATTERNS = ["plain", "grass", "soil", "sand", "asphalt", "concrete", "tile", "planks", "stone"];
1058
1180
  export {
1181
+ CRACK_REFERENCE_JOULES,
1059
1182
  DEFAULT_BAR_YIELD_JOULES,
1060
1183
  DEFAULT_PROFILE,
1061
1184
  DEFAULT_TERRAIN_BUDGET,
1062
1185
  TERRAIN_MATERIALS,
1063
1186
  TERRAIN_PATTERNS,
1064
1187
  TERRAIN_PROFILES,
1188
+ crackLines,
1065
1189
  createTerrain,
1066
1190
  createTerrainImpl
1067
1191
  };