@adatechnology/game-terrain 0.1.2 → 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;
@@ -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;
@@ -431,6 +515,7 @@ function createTerrain(options) {
431
515
  const mixed = new Uint8Array(chunksX * chunksZ);
432
516
  const dirty = /* @__PURE__ */ new Set();
433
517
  const marks = createMarkStore(budget);
518
+ const cracks = createCrackStore();
434
519
  const debrisStore = createDebrisStore(() => budget.maxDebris);
435
520
  const sliding = /* @__PURE__ */ new Set();
436
521
  const burning = /* @__PURE__ */ new Map();
@@ -607,6 +692,7 @@ function createTerrain(options) {
607
692
  let depth = start.depth;
608
693
  let radius = 0;
609
694
  let carved = 0;
695
+ let breached = false;
610
696
  const firstMaterial = materials.get(start.material);
611
697
  if (start.loose <= 0) {
612
698
  for (; ; ) {
@@ -618,6 +704,19 @@ function createTerrain(options) {
618
704
  const response = material.hits[blow];
619
705
  if (!response || energy < (response.thresholdJoules ?? 0)) break;
620
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
+ }
621
720
  const pastCover = bars !== void 0 && depth - layer.top >= bars.cover - 1e-6;
622
721
  const specificEnergy = response.specificEnergy * (pastCover ? bars.resistance : 1);
623
722
  const limit = bars && !pastCover ? Math.min(room, layer.top + bars.cover - depth) : room;
@@ -645,8 +744,10 @@ function createTerrain(options) {
645
744
  removed.push({ material: firstMaterial.id, volume });
646
745
  }
647
746
  }
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 };
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 };
650
751
  if (firstMaterial.reposeDegrees === void 0 && start.loose <= 0) radius *= 1 + RIGID_WIDENING * (start.depth / maxDepthOfProfile(profile));
651
752
  const volumeTotal = removed.reduce((sum, entry) => sum + entry.volume, 0);
652
753
  const fine = radius < cellSize * FINE_CELLS;
@@ -664,8 +765,16 @@ function createTerrain(options) {
664
765
  throwPieces({ x, y: point[1], z, volume: thrown, material, energy: spec.energy * (entry.volume / volumeTotal), direction: spec.direction, quota: share, bowlRadius: radius, blow });
665
766
  quota -= share;
666
767
  }
768
+ if (breached) collapsed = loadBars(blow, spec.energy, x, z, profile) || collapsed;
667
769
  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 };
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);
669
778
  }
670
779
  function loadBars(blow, energy, x, z, profile) {
671
780
  const bars = profile.strata.find((stratum) => stratum.reinforcement);
@@ -906,6 +1015,7 @@ function createTerrain(options) {
906
1015
  barBend.fill(0);
907
1016
  touched.fill(0);
908
1017
  marks.clear();
1018
+ cracks.clear();
909
1019
  debrisStore.clear();
910
1020
  sliding.clear();
911
1021
  burning.clear();
@@ -957,6 +1067,9 @@ function createTerrain(options) {
957
1067
  updateSlides();
958
1068
  updateFire(dt);
959
1069
  },
1070
+ get dirtyChunkCount() {
1071
+ return dirty.size;
1072
+ },
960
1073
  drainDirtyChunks(max = budget.maxDirtyChunksPerFrame) {
961
1074
  const taken = [];
962
1075
  for (const index of dirty) {
@@ -968,6 +1081,10 @@ function createTerrain(options) {
968
1081
  },
969
1082
  chunk,
970
1083
  marks: () => marks.list,
1084
+ cracks: () => cracks.list,
1085
+ get cracksVersion() {
1086
+ return cracks.version;
1087
+ },
971
1088
  debris: () => debrisStore.list,
972
1089
  burningPoints: () => [...burning.keys()].map((key) => {
973
1090
  const cellRow = Math.floor(key / 65536);
@@ -980,7 +1097,7 @@ function createTerrain(options) {
980
1097
  stats: () => ({ marks: marks.list.length, debris: debrisStore.list.length, debrisMoving: debrisStore.list.filter((piece) => !piece.atRest).length, dirtyChunks: dirty.size, burning: burning.size }),
981
1098
  reset,
982
1099
  serialize() {
983
- 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 })) };
984
1101
  },
985
1102
  load(snapshot) {
986
1103
  currentLayout = snapshot.layout;
@@ -990,6 +1107,7 @@ function createTerrain(options) {
990
1107
  fillSparse(scorch, snapshot.scorch);
991
1108
  fillSparse(barBend, snapshot.barBend ?? []);
992
1109
  marks.load(snapshot.marks);
1110
+ cracks.load(snapshot.cracks ?? []);
993
1111
  for (let chunkIndex = 0; chunkIndex < touched.length; chunkIndex++) markChunk(chunkIndex);
994
1112
  }
995
1113
  };
@@ -1060,12 +1178,14 @@ function createTerrainImpl(terrain, options) {
1060
1178
  // src/types.ts
1061
1179
  var TERRAIN_PATTERNS = ["plain", "grass", "soil", "sand", "asphalt", "concrete", "tile", "planks", "stone"];
1062
1180
  export {
1181
+ CRACK_REFERENCE_JOULES,
1063
1182
  DEFAULT_BAR_YIELD_JOULES,
1064
1183
  DEFAULT_PROFILE,
1065
1184
  DEFAULT_TERRAIN_BUDGET,
1066
1185
  TERRAIN_MATERIALS,
1067
1186
  TERRAIN_PATTERNS,
1068
1187
  TERRAIN_PROFILES,
1188
+ crackLines,
1069
1189
  createTerrain,
1070
1190
  createTerrainImpl
1071
1191
  };