@adatechnology/game-destruction-three 0.22.1 → 0.23.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
@@ -254,14 +254,37 @@ function buildBrokenBoardGeometry(THREE, seed) {
254
254
  }
255
255
  return faceted(THREE, positions);
256
256
  }
257
+ function buildRubbleGeometry(THREE, seed) {
258
+ const rnd = createRng(9e3 + seed);
259
+ const base = new THREE.BoxGeometry(1, 1, 1, 2, 2, 2).toNonIndexed();
260
+ const source = base.getAttribute("position");
261
+ const moved = /* @__PURE__ */ new Map();
262
+ const positions = [];
263
+ for (let i = 0; i < source.count; i++) {
264
+ const p = [source.getX(i), source.getY(i), source.getZ(i)];
265
+ const key = p.map((x) => x.toFixed(3)).join(",");
266
+ let out = moved.get(key);
267
+ if (!out) {
268
+ const outer = p.filter((x) => Math.abs(x) > 0.49).length;
269
+ const bite = outer === 3 ? 0.08 + 0.3 * rnd() : outer === 2 ? 0.1 * rnd() : 0;
270
+ out = p.map((x) => Math.abs(x) > 0.49 ? x - Math.sign(x) * bite * (0.4 + 0.6 * rnd()) : x);
271
+ moved.set(key, out);
272
+ }
273
+ positions.push(out[0], out[1], out[2]);
274
+ }
275
+ base.dispose();
276
+ return faceted(THREE, positions);
277
+ }
257
278
 
258
279
  // src/fragments.ts
259
280
  var BUILDERS = {
260
281
  sliver: buildSliverGeometry,
261
282
  lump: buildLumpGeometry,
262
283
  veneer: buildVeneerGeometry,
263
- board: buildBrokenBoardGeometry
284
+ board: buildBrokenBoardGeometry,
285
+ rubble: buildRubbleGeometry
264
286
  };
287
+ var FLOOR_REMEASURE = 0.5;
265
288
  var SHEET_MAX = 4e-3;
266
289
  var SHEET_AIR = 2.5;
267
290
  var SHEET_SWAY = 3;
@@ -369,6 +392,7 @@ var FragmentSystem = class {
369
392
  if (f.shape === "chunk" && form === "granules") return this.sharedGeometry("lump", seed);
370
393
  if (f.shape === "scrap" && form === "plies") return this.sharedGeometry("veneer", seed);
371
394
  if (f.shape === "slab" && (form === "slivers" || form === "plies")) return this.sharedGeometry("board", seed);
395
+ if (f.shape === "chunk" && this.o.crumbles?.(f.material)) return this.sharedGeometry("rubble", seed);
372
396
  return this.box;
373
397
  }
374
398
  markParts() {
@@ -377,18 +401,18 @@ var FragmentSystem = class {
377
401
  return { geometry: this.markGeometry, material: this.markMaterial };
378
402
  }
379
403
  /**
380
- * Meshes, never put in a scene by this class, that draw every shared piece of a fragment once: the box, the six shards, the wood splinters, MDF lumps and plywood veneer, the bullet-hole disc, and the opaque and
404
+ * Meshes, never put in a scene by this class, that draw every shared piece of a fragment once: the box, the six shards, the wood splinters, MDF lumps, plywood veneer, broken masonry, the bullet-hole disc, and the opaque and
381
405
  * glass materials. A host draws them at load so the first blast does not upload these buffers in the middle of a frame.
382
406
  */
383
407
  warmMeshes() {
384
408
  const opaque = this.materialFor("warm", 8421504, false);
385
409
  const glass2 = this.materialFor("warm", 8421504, true);
386
410
  const geometries = [this.box, ...Array.from({ length: SHARD_SHAPES }, (_, index) => this.shardGeometry(index))];
387
- const wood = ["sliver", "lump", "veneer", "board"].flatMap((shape) => Array.from({ length: FRAGMENT_SHAPE_VARIANTS }, (_, index) => this.sharedGeometry(shape, index)));
411
+ const shaped = ["sliver", "lump", "veneer", "board", "rubble"].flatMap((shape) => Array.from({ length: FRAGMENT_SHAPE_VARIANTS }, (_, index) => this.sharedGeometry(shape, index)));
388
412
  const { geometry, material } = this.markParts();
389
413
  return [
390
414
  ...geometries.flatMap((shared) => [new this.THREE.Mesh(shared, opaque), new this.THREE.Mesh(shared, glass2)]),
391
- ...wood.map((shared) => new this.THREE.Mesh(shared, opaque)),
415
+ ...shaped.map((shared) => new this.THREE.Mesh(shared, opaque)),
392
416
  new this.THREE.Mesh(geometry, material)
393
417
  ];
394
418
  }
@@ -450,6 +474,7 @@ var FragmentSystem = class {
450
474
  this.o.scene.add(mesh);
451
475
  const live = {
452
476
  mesh,
477
+ floor: null,
453
478
  home,
454
479
  vel: new this.THREE.Vector3(f.velocity[0], f.velocity[1], f.velocity[2]),
455
480
  ang: new this.THREE.Vector3(f.angularVelocity[0], f.angularVelocity[1], f.angularVelocity[2]),
@@ -466,7 +491,7 @@ var FragmentSystem = class {
466
491
  born: this.tick,
467
492
  life: lasting ? 0 : Math.max(0, typeof this.o.life === "function" ? this.o.life(f, home) : this.o.life)
468
493
  };
469
- if (lasting || f.shape !== "shard" && isBig(f.size)) {
494
+ if (lasting || isBig(f.size)) {
470
495
  live.body = {
471
496
  mass: f.mass,
472
497
  push: (x, z) => {
@@ -560,8 +585,18 @@ var FragmentSystem = class {
560
585
  if (!f.asleep || f.dying) continue;
561
586
  const e = this.m.makeRotationFromQuaternion(f.mesh.quaternion).elements;
562
587
  const ext = Math.abs(e[1]) * f.half.x + Math.abs(e[5]) * f.half.y + Math.abs(e[9]) * f.half.z;
563
- if (f.mesh.position.y - ext > this.o.groundAt(f.mesh.position.x, f.mesh.position.z) + SUPPORT_GAP) this.rouse(f);
588
+ if (f.mesh.position.y - ext > this.floorUnder(f) + SUPPORT_GAP) this.rouse(f);
589
+ }
590
+ }
591
+ /** What a fragment lands on: measured where it is, and again only once it moved `FLOOR_REMEASURE` m off that spot (a ray down is not free). */
592
+ floorUnder(f) {
593
+ const p = f.mesh.position;
594
+ if (!this.o.surfaceAt) return this.o.groundAt(p.x, p.z);
595
+ const at = f.floor;
596
+ if (!at || Math.abs(at.x - p.x) > FLOOR_REMEASURE || Math.abs(at.z - p.z) > FLOOR_REMEASURE || p.y < at.y - FLOOR_REMEASURE) {
597
+ f.floor = { x: p.x, z: p.z, y: this.o.surfaceAt(p.x, p.y, p.z) };
564
598
  }
599
+ return f.floor.y;
565
600
  }
566
601
  step(f, dt, g) {
567
602
  const mesh = f.mesh;
@@ -577,7 +612,7 @@ var FragmentSystem = class {
577
612
  const h = f.half;
578
613
  const sc = f.dying ? f.fade : 1;
579
614
  const ext = (Math.abs(e[1]) * h.x + Math.abs(e[5]) * h.y + Math.abs(e[9]) * h.z) * sc;
580
- const gy = this.o.groundAt(mesh.position.x, mesh.position.z);
615
+ const gy = this.floorUnder(f);
581
616
  let contact = false;
582
617
  if (mesh.position.y - ext < gy) {
583
618
  contact = true;
@@ -2329,6 +2364,47 @@ function shellDisplacementAt(THREE, worldPoint, shells, out) {
2329
2364
  return Math.sqrt(nearest) * nearestScale;
2330
2365
  }
2331
2366
 
2367
+ // src/stair-crack.ts
2368
+ function stairCrackPath(spec) {
2369
+ const rnd = createRng(spec.seed);
2370
+ const run = Math.max(0.02, spec.step[0] / 2);
2371
+ const rise = Math.max(0.02, spec.step[1]);
2372
+ const points = [[0, 0]];
2373
+ let h = 0;
2374
+ let u = 0;
2375
+ let walked = 0;
2376
+ while (walked < spec.length) {
2377
+ const along2 = run * (rnd() < 0.2 ? 2 : 1);
2378
+ h += along2;
2379
+ points.push([h, u]);
2380
+ u += rise;
2381
+ points.push([h, u]);
2382
+ walked += along2 + rise;
2383
+ }
2384
+ return points;
2385
+ }
2386
+ function stairCrackStrip(points, width, taper) {
2387
+ const out = [];
2388
+ const n = points.length - 1;
2389
+ for (let k = 0; k < n; k++) {
2390
+ const [h0, u0] = points[k];
2391
+ const [h1, u1] = points[k + 1];
2392
+ const length = Math.hypot(h1 - h0, u1 - u0) || 1;
2393
+ const half0 = width * (1 - (1 - taper) * k / n) / 2;
2394
+ const half1 = width * (1 - (1 - taper) * (k + 1) / n) / 2;
2395
+ const sh = -(u1 - u0) / length;
2396
+ const su = (h1 - h0) / length;
2397
+ const eh = (h1 - h0) / length * half0;
2398
+ const eu = (u1 - u0) / length * half0;
2399
+ const a = [h0 - eh - sh * half0, u0 - eu - su * half0];
2400
+ const b = [h1 + eh - sh * half1, u1 + eu - su * half1];
2401
+ const c = [h1 + eh + sh * half1, u1 + eu + su * half1];
2402
+ const d = [h0 - eh + sh * half0, u0 - eu + su * half0];
2403
+ out.push(...a, ...b, ...c, ...a, ...c, ...d);
2404
+ }
2405
+ return out;
2406
+ }
2407
+
2332
2408
  // src/visual.ts
2333
2409
  var POS_ATTR = "position";
2334
2410
  function positionAttr(geo) {
@@ -2433,6 +2509,8 @@ var collapseForeign = (mesh, arr) => {
2433
2509
  }
2434
2510
  };
2435
2511
  var DECAL_SNAP = 0.03;
2512
+ var STAIR_CRACK_LIFT = 2e-3;
2513
+ var STAIR_CRACK_TAPER = 0.35;
2436
2514
  var Visuals = class {
2437
2515
  constructor(THREE, lowQuality, decalCap) {
2438
2516
  __publicField(this, "THREE", THREE);
@@ -2926,6 +3004,71 @@ var Visuals = class {
2926
3004
  lines.renderOrder = 2;
2927
3005
  this.addDecal(mv, { obj: lines, geometry: geo });
2928
3006
  }
3007
+ /**
3008
+ * A stair-step crack through the joints of a wall (settlement): from `worldPoint` on the face whose outward normal is `worldNormal`, climbing toward
3009
+ * `worldToward` (the side that went down) one unit at a time, `width` metres wide at its start. A strip of triangles in the mesh's space, owned by the
3010
+ * part's marks: it goes with them (cleared, restored, disposed).
3011
+ */
3012
+ addStairCrack(mv, worldPoint, worldNormal, worldToward, spec) {
3013
+ const mesh = mv.mesh;
3014
+ mesh.updateWorldMatrix(true, false);
3015
+ const n = worldNormal.clone().normalize();
3016
+ const up = new this.THREE.Vector3(0, 1, 0).addScaledVector(n, -n.y).normalize();
3017
+ const side = worldToward.clone().addScaledVector(n, -worldToward.dot(n)).addScaledVector(up, -worldToward.dot(up));
3018
+ if (side.lengthSq() < 1e-8) side.crossVectors(up, n);
3019
+ side.normalize();
3020
+ const start = this.onFace(mesh, worldPoint, n).clone().addScaledVector(n, STAIR_CRACK_LIFT);
3021
+ const flat = stairCrackStrip(stairCrackPath({ length: spec.length, step: spec.step, seed: spec.seed }), spec.width, STAIR_CRACK_TAPER);
3022
+ const flip = new this.THREE.Vector3().crossVectors(side, up).dot(n) < 0;
3023
+ const verts = [];
3024
+ const point = this.a;
3025
+ for (let i = 0; i < flat.length; i += 6) {
3026
+ const order = flip ? [0, 2, 1] : [0, 1, 2];
3027
+ for (const k of order) {
3028
+ point.copy(start).addScaledVector(side, flat[i + k * 2]).addScaledVector(up, flat[i + k * 2 + 1]);
3029
+ mesh.worldToLocal(point);
3030
+ verts.push(point.x, point.y, point.z);
3031
+ }
3032
+ }
3033
+ const geo = new this.THREE.BufferGeometry();
3034
+ geo.setAttribute("position", new this.THREE.Float32BufferAttribute(verts, 3));
3035
+ const strip = new this.THREE.Mesh(geo, this.kit.patchMaterial(spec.color));
3036
+ strip.renderOrder = 2;
3037
+ this.addDecal(mv, { obj: strip, geometry: geo });
3038
+ }
3039
+ // ------------------------------------------------------------------------------------------------------------------------------------
3040
+ // Settlement
3041
+ // ------------------------------------------------------------------------------------------------------------------------------------
3042
+ /**
3043
+ * Moves the part's vertices and its marks down by `dropAt(world point)` metres (negative: up), where each of them is: a part that settled more at
3044
+ * one end tilts. Writes the geometry unless `write` is false (several parts of one merged mesh are then written once by `commit`).
3045
+ */
3046
+ sink(mv, dropAt, write = true) {
3047
+ if (!this.ownGeometry(mv)) return false;
3048
+ const mesh = mv.mesh;
3049
+ mesh.updateWorldMatrix(true, false);
3050
+ const scale = mesh.matrixWorld.getMaxScaleOnAxis() || 1;
3051
+ const down = this.b.set(0, -1, 0).transformDirection(this.m4.copy(mesh.matrixWorld).invert());
3052
+ const work = mv.host ? mv.host.work : mv.work;
3053
+ const from = mv.range ? mv.range[0] * 3 : 0;
3054
+ const to = mv.range ? mv.range[1] * 3 : work.length;
3055
+ const point = this.a;
3056
+ for (let i = from; i < to; i += 3) {
3057
+ const d = dropAt(point.set(work[i], work[i + 1], work[i + 2]).applyMatrix4(mesh.matrixWorld)) / scale;
3058
+ work[i] += down.x * d;
3059
+ work[i + 1] += down.y * d;
3060
+ work[i + 2] += down.z * d;
3061
+ }
3062
+ for (const decal of mv.decals) decal.obj.position.addScaledVector(down, dropAt(decal.obj.getWorldPosition(point)) / scale);
3063
+ mv.touched = true;
3064
+ if (write) this.commit(mv);
3065
+ return true;
3066
+ }
3067
+ /** Writes what `sink` moved into the geometry. */
3068
+ commit(mv) {
3069
+ const pos = positionAttr(mv.mesh.geometry);
3070
+ if (pos) this.writePositions(mv, pos);
3071
+ }
2929
3072
  // ------------------------------------------------------------------------------------------------------------------------------------
2930
3073
  // Char and heat glow
2931
3074
  // ------------------------------------------------------------------------------------------------------------------------------------
@@ -3321,6 +3464,7 @@ var Visuals = class {
3321
3464
  }
3322
3465
  restorePart(pv) {
3323
3466
  for (const mv of pv.meshes) this.restoreMesh(mv);
3467
+ delete pv.settled;
3324
3468
  pv.charApplied = 0;
3325
3469
  pv.glowApplied = 0;
3326
3470
  pv.meltApplied = 0;
@@ -3331,6 +3475,78 @@ var Visuals = class {
3331
3475
  }
3332
3476
  };
3333
3477
 
3478
+ // src/settlement.ts
3479
+ import { settlementAt } from "@adatechnology/game-destruction";
3480
+ var SETTLE_SECONDS = 1.2;
3481
+ var SETTLE_REDRAW = 0.1;
3482
+ var dropOf = (line, point) => line ? settlementAt(line, point) : 0;
3483
+ var sameLine = (a, b) => !!a && a.drop.length === b.drop.length && a.drop.every((v, k) => v === b.drop[k]) && a.from.every((v, k) => v === b.from[k]) && a.to.every((v, k) => v === b.to[k]);
3484
+ var SettlementView = class {
3485
+ constructor(THREE, visuals) {
3486
+ __publicField(this, "visuals", visuals);
3487
+ __publicField(this, "moving", /* @__PURE__ */ new Map());
3488
+ __publicField(this, "local");
3489
+ __publicField(this, "inverse");
3490
+ this.local = new THREE.Vector3();
3491
+ this.inverse = new THREE.Matrix4();
3492
+ }
3493
+ /** The parts of an object (`root` draws it in object space) and their settlement now: those that changed start going to it. */
3494
+ follow(root, parts) {
3495
+ for (const [pv, target] of parts) {
3496
+ if (!target || pv.gone !== "none" || sameLine(this.moving.get(pv)?.target ?? pv.settled, target)) continue;
3497
+ this.finish(pv);
3498
+ this.moving.set(pv, { pv, root, start: pv.settled, target, shown: 0, elapsed: 0, lastDrawn: -SETTLE_REDRAW });
3499
+ }
3500
+ }
3501
+ get count() {
3502
+ return this.moving.size;
3503
+ }
3504
+ update(dt) {
3505
+ for (const s of this.moving.values()) {
3506
+ s.elapsed += dt;
3507
+ const done = s.elapsed >= SETTLE_SECONDS;
3508
+ if (!done && s.elapsed - s.lastDrawn < SETTLE_REDRAW) continue;
3509
+ s.lastDrawn = s.elapsed;
3510
+ const t = Math.min(1, s.elapsed / SETTLE_SECONDS);
3511
+ this.draw(s, t * t * (3 - 2 * t));
3512
+ if (done) {
3513
+ s.pv.settled = s.target;
3514
+ this.moving.delete(s.pv);
3515
+ }
3516
+ }
3517
+ }
3518
+ /** Draws the rest of a part's settlement at once (it is about to be split off or put back). */
3519
+ finish(pv) {
3520
+ const s = this.moving.get(pv);
3521
+ if (!s) return;
3522
+ this.draw(s, 1);
3523
+ pv.settled = s.target;
3524
+ this.moving.delete(pv);
3525
+ }
3526
+ /** Forget a part without drawing (it was put back as it was). */
3527
+ forget(pv) {
3528
+ this.moving.delete(pv);
3529
+ }
3530
+ clear() {
3531
+ this.moving.clear();
3532
+ }
3533
+ draw(s, shown) {
3534
+ const share = shown - s.shown;
3535
+ if (share <= 0) return;
3536
+ s.shown = shown;
3537
+ s.root.updateWorldMatrix(true, false);
3538
+ this.inverse.copy(s.root.matrixWorld).invert();
3539
+ const dropAt = (world) => {
3540
+ const p = this.local.copy(world).applyMatrix4(this.inverse);
3541
+ const at = [p.x, p.y, p.z];
3542
+ return (dropOf(s.target, at) - dropOf(s.start, at)) * share;
3543
+ };
3544
+ const writes = /* @__PURE__ */ new Map();
3545
+ for (const mv of s.pv.meshes) if (this.visuals.sink(mv, dropAt, false)) writes.set(mv.host ?? mv, mv);
3546
+ for (const mv of writes.values()) this.visuals.commit(mv);
3547
+ }
3548
+ };
3549
+
3334
3550
  // src/renderer.ts
3335
3551
  var rendererCount = 0;
3336
3552
  var COLLAPSE_CLOUD_KG = 1e3;
@@ -3340,6 +3556,8 @@ var CHIP_DECAL_EDGE = 0.05;
3340
3556
  var WOOD_RIM = 1.5;
3341
3557
  var IMPACT_TAGS = ["metal", "glass", "wood"];
3342
3558
  var GLOW_FROM_C = 430;
3559
+ var MIN_DRAWN_CRACK_WIDTH = 4e-3;
3560
+ var SETTLEMENT_CRACK_COLOR = 2827296;
3343
3561
  var QUEUE_CAP = 4e3;
3344
3562
  var TRICKLE_SECONDS = [2, 5];
3345
3563
  var TRICKLE_EVERY = 0.25;
@@ -3363,6 +3581,9 @@ var STRUCTURAL_EVENTS = /* @__PURE__ */ new Set([
3363
3581
  var MIN_FRAGMENTS_PER_SLICE = 8;
3364
3582
  var STATIC_REFRESH = 0.5;
3365
3583
  var STATIC_REFRESH_SLICE_MS = 1;
3584
+ var SURFACE_RAYS = 4;
3585
+ var SURFACE_INSIDE = 5e-3;
3586
+ var SURFACE_STEP = 0.15;
3366
3587
  var STATIC_SPAN = 3;
3367
3588
  var FEET_BAND = 0.03;
3368
3589
  var TIP_MIN_HEIGHT = 0.3;
@@ -3559,6 +3780,8 @@ var Renderer = class {
3559
3780
  });
3560
3781
  __publicField(this, "unsubscribe");
3561
3782
  __publicField(this, "visuals");
3783
+ /** The parts the ground went down under, drawn going down (`subside`). */
3784
+ __publicField(this, "settlement");
3562
3785
  __publicField(this, "fragments");
3563
3786
  __publicField(this, "ground");
3564
3787
  __publicField(this, "pieceSys");
@@ -3567,6 +3790,8 @@ var Renderer = class {
3567
3790
  __publicField(this, "fires", /* @__PURE__ */ new Map());
3568
3791
  /** Primed parts burning in the effects world until they go off: their burn loop ends when they are put out. */
3569
3792
  __publicField(this, "primedBurning", /* @__PURE__ */ new Set());
3793
+ /** The burn loops asked of the host and not yet ended, by part: whatever stops a part's fire ends its loop, once, with what it started with. */
3794
+ __publicField(this, "burnSounds", /* @__PURE__ */ new Map());
3570
3795
  __publicField(this, "leaks", []);
3571
3796
  __publicField(this, "homeTags", []);
3572
3797
  __publicField(this, "throttles", /* @__PURE__ */ new Map());
@@ -3601,6 +3826,7 @@ var Renderer = class {
3601
3826
  this.v2 = new THREE.Vector3();
3602
3827
  const maxParticles = Math.max(8, Math.floor((o.maxParticles ?? 2500) * (this.low ? 0.5 : 1)));
3603
3828
  this.visuals = new Visuals(THREE, this.low, 600);
3829
+ this.settlement = new SettlementView(THREE, this.visuals);
3604
3830
  this.ground = new GroundSpots(THREE, o.scene, this.groundAt, 48);
3605
3831
  if (!o.vfx) warnOnce("game-destruction-three.own-effects-world", "game-destruction-three made an effects world of its own because the host lent none (options.vfx); pass the host effects so the effects share one budget");
3606
3832
  this.vfx = o.vfx ?? createVfx({ profile: this.quality, gravity: this.gravity, groundAt: this.groundAt, sprites: { capacity: maxParticles, density: this.low ? 0.6 : 1 } });
@@ -3615,7 +3841,10 @@ var Renderer = class {
3615
3841
  gravity: this.gravity,
3616
3842
  max: this.maxFragments,
3617
3843
  life: o.fragmentLife ?? 12,
3618
- formOf: (id) => this.world.materials.get(id)?.splitForm
3844
+ formOf: (id) => this.world.materials.get(id)?.splitForm,
3845
+ crumbles: (id) => this.world.materials.get(id)?.fracture === "crumble",
3846
+ // rubble lands on what is still built (a slab, a wall's top, a floor), not only on the terrain under it
3847
+ surfaceAt: (x, y, z) => this.surfaceAt(x, y, z)
3619
3848
  });
3620
3849
  this.cages = new RebarCages(THREE, o.scene);
3621
3850
  this.pieceSys = new PieceSystem(THREE, {
@@ -4010,6 +4239,7 @@ var Renderer = class {
4010
4239
  this.checkBroken();
4011
4240
  for (const b of this.bounds.values()) b.onFrame?.(dt);
4012
4241
  for (const b of this.bounds.values()) if (b.soilNode && this.world.state(b.id)?.soil?.status === "yielding") this.onSettle(b.id);
4242
+ this.settlement.update(dt);
4013
4243
  this.updateActive(dt);
4014
4244
  this.updateLeaks(dt);
4015
4245
  this.updateTrickles(dt);
@@ -4085,6 +4315,18 @@ var Renderer = class {
4085
4315
  node.quaternion.set(q[0], q[1], q[2], q[3]);
4086
4316
  node.updateMatrixWorld(true);
4087
4317
  }
4318
+ /** The ground went down under some parts of an object: each part with a settlement (`PartState.settlement`) starts going down to it. */
4319
+ onSubside(objectId) {
4320
+ const b = this.bounds.get(objectId);
4321
+ const state = this.world.state(objectId);
4322
+ if (!b || !state) return;
4323
+ const parts = [];
4324
+ for (const ps of state.parts) {
4325
+ const pv = b.parts.get(ps.id);
4326
+ if (pv && ps.settlement) parts.push([pv, ps.settlement]);
4327
+ }
4328
+ this.settlement.follow(b.soilNode ?? b.root, parts);
4329
+ }
4088
4330
  syncTransforms() {
4089
4331
  for (const b of this.bounds.values()) this.syncOne(b);
4090
4332
  }
@@ -4235,9 +4477,15 @@ var Renderer = class {
4235
4477
  case "debris":
4236
4478
  this.vfx.burst(ev.kind, ev.point, ev.normal, ev.count, ev.speed > 0 ? ev.speed : DEFAULT_SPEED[ev.kind] ?? 1, this.debrisOptions(ev.kind, ev.object, ev.part, ev.material));
4237
4479
  break;
4238
- case "fracture":
4480
+ case "fracture": {
4481
+ const pv = this.partVis(ev.object, ev.part);
4482
+ if (pv) this.settlement.forget(pv);
4239
4483
  this.onFracture(ev);
4240
4484
  break;
4485
+ }
4486
+ case "subside":
4487
+ this.onSubside(ev.object);
4488
+ break;
4241
4489
  case "detach":
4242
4490
  this.onDetach(ev);
4243
4491
  break;
@@ -4247,6 +4495,10 @@ var Renderer = class {
4247
4495
  case "collapse": {
4248
4496
  const root = this.bounds.get(ev.object)?.root;
4249
4497
  const furniture = !!root && kitOriginOf(root)?.kind === "furniture";
4498
+ for (const part of ev.parts) {
4499
+ const pv = this.partVis(ev.object, part);
4500
+ if (pv) this.settlement.finish(pv);
4501
+ }
4250
4502
  this.splitLoose(ev.object);
4251
4503
  if (!furniture || ev.mass >= COLLAPSE_CLOUD_KG) this.playEvent({ kind: "collapse" }, this.rootPoint(ev.object));
4252
4504
  else this.vfx.burst("dust", this.rootPoint(ev.object), [0, 1, 0], clamp(Math.round(ev.mass / 4), 4, 24), 0.8);
@@ -4264,7 +4516,7 @@ var Renderer = class {
4264
4516
  this.startFire(ev);
4265
4517
  this.activate(ev.object, ev.part);
4266
4518
  this.sound("ignite", ev.point, 0.7);
4267
- this.sound("burn_loop", ev.point, 0.5, this.burning(ev.object, ev.part));
4519
+ this.startBurnSound(ev.object, ev.part, ev.point, this.burning(ev.object, ev.part));
4268
4520
  break;
4269
4521
  case "primed":
4270
4522
  if (ev.trigger !== "explosion") {
@@ -4272,7 +4524,7 @@ var Renderer = class {
4272
4524
  this.primedBurning.add(`${ev.object}|${ev.part}`);
4273
4525
  this.activate(ev.object, ev.part);
4274
4526
  this.sound("primed", ev.point, 0.7, { seconds: ev.delay });
4275
- this.sound("burn_loop", ev.point, 0.5, { fuel: "gas" });
4527
+ if (!this.burnSounds.has(`${ev.object}|${ev.part}`)) this.startBurnSound(ev.object, ev.part, ev.point, { fuel: "gas" });
4276
4528
  }
4277
4529
  break;
4278
4530
  case "spall":
@@ -4294,20 +4546,20 @@ var Renderer = class {
4294
4546
  break;
4295
4547
  case "extinguish": {
4296
4548
  const f = this.fires.get(`${ev.object}|${ev.part}`);
4297
- const primed = this.primedBurning.delete(`${ev.object}|${ev.part}`);
4549
+ this.primedBurning.delete(`${ev.object}|${ev.part}`);
4298
4550
  if (f) {
4299
4551
  this.stopFire(`${ev.object}|${ev.part}`);
4300
4552
  this.vfx.burst("steam", f.point, [0, 1, 0], 14, 1.2);
4301
4553
  }
4302
- if (f || primed) this.sound("burn_end", f?.point ?? this.rootPoint(ev.object), 0.5, primed ? { fuel: "gas" } : this.burning(ev.object, ev.part));
4554
+ this.endBurnSound(`${ev.object}|${ev.part}`);
4303
4555
  this.activate(ev.object, ev.part);
4304
4556
  this.sound("extinguish", f?.point ?? this.rootPoint(ev.object), 0.6);
4305
4557
  break;
4306
4558
  }
4307
4559
  case "burnout": {
4308
4560
  this.stopFire(`${ev.object}|${ev.part}`);
4309
- const primed = this.primedBurning.delete(`${ev.object}|${ev.part}`);
4310
- this.sound("burn_end", ev.point, 0.5, primed ? { fuel: "gas" } : this.burning(ev.object, ev.part));
4561
+ this.primedBurning.delete(`${ev.object}|${ev.part}`);
4562
+ this.endBurnSound(`${ev.object}|${ev.part}`);
4311
4563
  this.vfx.burst("ash", ev.point, [0, 1, 0], 16, 1);
4312
4564
  this.vfx.burst("smoke", ev.point, [0, 1, 0], 6, 0.8, { color: SMOKE_TONES.grey });
4313
4565
  const pv = this.partVis(ev.object, ev.part);
@@ -4375,7 +4627,8 @@ var Renderer = class {
4375
4627
  break;
4376
4628
  }
4377
4629
  case "explode":
4378
- if (this.primedBurning.delete(`${ev.object}|${ev.part}`)) this.sound("burn_end", ev.point, 0.5, { fuel: "gas" });
4630
+ this.primedBurning.delete(`${ev.object}|${ev.part}`);
4631
+ this.endBurnSound(`${ev.object}|${ev.part}`);
4379
4632
  this.onExplode(ev);
4380
4633
  break;
4381
4634
  case "destroyed":
@@ -4403,7 +4656,8 @@ var Renderer = class {
4403
4656
  }
4404
4657
  /** Current colour of the part's first mesh (char included), or null. */
4405
4658
  partColor(pv, def) {
4406
- const mv = pv.meshes[0];
4659
+ const size = (m) => m.range ? m.range[1] - m.range[0] : m.mesh.geometry.getAttribute("position")?.count ?? 0;
4660
+ const mv = pv.meshes.reduce((best, m) => !best || size(m) > size(best) ? m : best, void 0);
4407
4661
  if (!mv) return null;
4408
4662
  const mat = materialsOf2(mv.mesh.material)[0];
4409
4663
  const c = mat?.color;
@@ -4494,7 +4748,20 @@ var Renderer = class {
4494
4748
  const pv = b?.parts.get(ev.part);
4495
4749
  if (!b || !pv) return;
4496
4750
  const def = this.world.materials.get(pv.material);
4497
- if (def?.visual?.transparent) {
4751
+ const stair = ev.crack.stair;
4752
+ if (stair && !def?.visual?.transparent) {
4753
+ const wp = new this.THREE.Vector3(ev.worldPoint[0], ev.worldPoint[1], ev.worldPoint[2]);
4754
+ const seed = hashNumbers(ev.time, ev.worldPoint[0], ev.worldPoint[1], ev.worldPoint[2]);
4755
+ const width = Math.max(stair.width, MIN_DRAWN_CRACK_WIDTH);
4756
+ const normal = this.worldDir(b, ev.crack.normal);
4757
+ const toward = this.worldDir(b, stair.along);
4758
+ const look = { length: ev.crack.length, step: stair.step, width, color: SETTLEMENT_CRACK_COLOR, seed };
4759
+ for (const face of [wp, wp.clone().addScaledVector(normal, -stair.through)]) {
4760
+ const out = face === wp ? normal : normal.clone().negate();
4761
+ const mv = this.nearestMesh(pv, face);
4762
+ if (mv) this.visuals.addStairCrack(mv, face, out, toward, look);
4763
+ }
4764
+ } else if (def?.visual?.transparent) {
4498
4765
  const wp = new this.THREE.Vector3(ev.worldPoint[0], ev.worldPoint[1], ev.worldPoint[2]);
4499
4766
  const mv = this.nearestMesh(pv, wp);
4500
4767
  const seed = hashNumbers(ev.time, ev.worldPoint[0], ev.worldPoint[1], ev.worldPoint[2]);
@@ -4910,6 +5177,19 @@ var Renderer = class {
4910
5177
  }
4911
5178
  return "";
4912
5179
  }
5180
+ /** The top of the ground or of a standing part of the world under (x, z), below height y: a ray straight down through the destruction world. */
5181
+ surfaceAt(x, y, z) {
5182
+ const ground = this.groundAt(x, z);
5183
+ if (y <= ground || typeof this.world.raycast !== "function") return ground;
5184
+ let from = y;
5185
+ for (let k = 0; k < SURFACE_RAYS && from > ground; k++) {
5186
+ const hit = this.world.raycast([x, from, z], [0, -1, 0], from - ground);
5187
+ if (!hit) return ground;
5188
+ if (hit.distance > SURFACE_INSIDE) return Math.max(ground, hit.point[1]);
5189
+ from -= SURFACE_STEP;
5190
+ }
5191
+ return ground;
5192
+ }
4913
5193
  /** `staticTopAt` without the boxes of `objectId` itself. */
4914
5194
  staticTopAtBut(objectId, x, z, y, step) {
4915
5195
  let top = -Infinity;
@@ -5064,9 +5344,25 @@ var Renderer = class {
5064
5344
  }
5065
5345
  stopFire(key) {
5066
5346
  const fire2 = this.fires.get(key);
5067
- if (!fire2) return;
5068
- if (fire2.handle !== void 0) this.vfx.stop(fire2.handle);
5069
- this.fires.delete(key);
5347
+ if (fire2) {
5348
+ if (fire2.handle !== void 0) this.vfx.stop(fire2.handle);
5349
+ this.fires.delete(key);
5350
+ }
5351
+ this.endBurnSound(key);
5352
+ }
5353
+ /** A part's fire loop starts; one already running for the part (it caught again) is ended first, so the host never counts a fire twice. */
5354
+ startBurnSound(object, part, point, detail) {
5355
+ const key = `${object}|${part}`;
5356
+ this.endBurnSound(key);
5357
+ this.burnSounds.set(key, { object, point, detail });
5358
+ this.sound("burn_loop", point, 0.5, detail);
5359
+ }
5360
+ /** The part's fire loop ends, with the point and the fuel it started with; nothing when it has none running. */
5361
+ endBurnSound(key) {
5362
+ const started = this.burnSounds.get(key);
5363
+ if (!started) return;
5364
+ this.burnSounds.delete(key);
5365
+ this.sound("burn_end", started.point, 0.5, started.detail);
5070
5366
  }
5071
5367
  // ----------------------------------------------------------------------------------------------------------------------------------
5072
5368
  // Fire, heat, char, melt, leaks
@@ -5092,6 +5388,7 @@ var Renderer = class {
5092
5388
  const ps = pv ? this.world.part(a.object, a.part) : null;
5093
5389
  if (!pv || !ps) {
5094
5390
  this.active.delete(key);
5391
+ this.stopFire(key);
5095
5392
  continue;
5096
5393
  }
5097
5394
  const def = this.world.materials.get(pv.material);
@@ -5211,6 +5508,7 @@ var Renderer = class {
5211
5508
  for (const home of homes) this.restoreHome(home);
5212
5509
  for (const [key, a] of [...this.active]) if (homes.has(a.object)) this.active.delete(key);
5213
5510
  for (const [key, f] of [...this.fires]) if (homes.has(f.object)) this.stopFire(key);
5511
+ for (const [key, started] of [...this.burnSounds]) if (homes.has(started.object)) this.endBurnSound(key);
5214
5512
  for (let i = this.leaks.length - 1; i >= 0; i--) if (homes.has(this.leaks[i].object)) this.leaks.splice(i, 1);
5215
5513
  for (const home of homes) {
5216
5514
  this.fragments.removeHome(home);
@@ -5223,6 +5521,7 @@ var Renderer = class {
5223
5521
  for (const b of [...this.bounds.values()]) {
5224
5522
  for (const [pid, pv] of [...b.parts]) {
5225
5523
  if (pv.home !== home) continue;
5524
+ this.settlement.forget(pv);
5226
5525
  this.visuals.restorePart(pv);
5227
5526
  this.tagPart(pv, home);
5228
5527
  if (b !== homeBound && homeBound) {
@@ -5244,8 +5543,10 @@ var Renderer = class {
5244
5543
  this.slices.length = 0;
5245
5544
  this.active.clear();
5246
5545
  for (const key of [...this.fires.keys()]) this.stopFire(key);
5546
+ for (const key of [...this.burnSounds.keys()]) this.endBurnSound(key);
5247
5547
  this.leaks.length = 0;
5248
5548
  this.trickles.length = 0;
5549
+ this.settlement.clear();
5249
5550
  for (const id of [...this.bounds.keys()]) if (this.bounds.has(id)) this.restore(id);
5250
5551
  for (const b of [...this.bounds.values()]) {
5251
5552
  if (b.isSplit) {
@@ -5883,6 +6184,7 @@ var DESTRUCTION_SOUND_KEYS = {
5883
6184
  }
5884
6185
  };
5885
6186
  var DEFAULT_FIRE = { perFire: 0.08, max: 0.45 };
6187
+ var FIRE_FADE_SECONDS = 0.4;
5886
6188
  var DEFAULT_CRACKLES = { base: 2, perSqrtFire: 3, max: 14, volume: 0.5 };
5887
6189
  var DEFAULT_INDOOR_CONFINEMENT = 1.3;
5888
6190
  function createDestructionSound(engine, options = {}) {
@@ -5904,6 +6206,11 @@ function createDestructionSound(engine, options = {}) {
5904
6206
  const loops = /* @__PURE__ */ new Map();
5905
6207
  const fires = [];
5906
6208
  let nextCrackle = 0;
6209
+ const fading = [];
6210
+ const fadeOut = (handle) => {
6211
+ handle.set({ volume: 0 });
6212
+ fading.push({ handle, left: FIRE_FADE_SECONDS });
6213
+ };
5907
6214
  const fuelFor = (detail) => detail?.fuel ?? (detail?.material !== void 0 ? fuelOf[detail.material] : void 0) ?? defaultFuel;
5908
6215
  const setFire = () => {
5909
6216
  if (options.burnLoop === false) return;
@@ -5914,7 +6221,7 @@ function createDestructionSound(engine, options = {}) {
5914
6221
  }
5915
6222
  for (const [id, handle] of loops) {
5916
6223
  if (count.has(id)) continue;
5917
- handle.stop();
6224
+ fadeOut(handle);
5918
6225
  loops.delete(id);
5919
6226
  }
5920
6227
  for (const [id, n] of count) {
@@ -5941,8 +6248,15 @@ function createDestructionSound(engine, options = {}) {
5941
6248
  else if (best >= 0) fires.splice(best, 1);
5942
6249
  };
5943
6250
  const reset = () => {
6251
+ for (const handle of loops.values()) fadeOut(handle);
6252
+ loops.clear();
6253
+ fires.length = 0;
6254
+ };
6255
+ const dispose = () => {
5944
6256
  for (const handle of loops.values()) handle.stop();
5945
6257
  loops.clear();
6258
+ for (const { handle } of fading) handle.stop();
6259
+ fading.length = 0;
5946
6260
  fires.length = 0;
5947
6261
  };
5948
6262
  const blastSound = (point, volume, detail) => {
@@ -5989,6 +6303,13 @@ function createDestructionSound(engine, options = {}) {
5989
6303
  engine.play(sound, { position: point, volume: volume * (rule.volumeScale ?? 1), intensity: Math.min(1, Math.max(0, volume)), ...params ? { params } : {} });
5990
6304
  },
5991
6305
  update(dt) {
6306
+ for (let i = fading.length - 1; i >= 0; i--) {
6307
+ const entry = fading[i];
6308
+ entry.left -= dt;
6309
+ if (entry.left > 0) continue;
6310
+ entry.handle.stop();
6311
+ fading.splice(i, 1);
6312
+ }
5992
6313
  if (!crackles || fires.length === 0 || options.burnLoop === false) return;
5993
6314
  const weight = fires.reduce((sum, f) => sum + FIRE_CRACKLE_OF[f.fuel], 0);
5994
6315
  if (weight <= 0) return;
@@ -6014,7 +6335,7 @@ function createDestructionSound(engine, options = {}) {
6014
6335
  return by;
6015
6336
  },
6016
6337
  reset,
6017
- dispose: reset
6338
+ dispose
6018
6339
  };
6019
6340
  }
6020
6341
 
@@ -6076,6 +6397,7 @@ export {
6076
6397
  DESTRUCTION_SOUND_KEYS,
6077
6398
  DRAG_SOUNDS,
6078
6399
  EXPLOSION_BOOM,
6400
+ FIRE_FADE_SECONDS,
6079
6401
  FIRE_FALLOFF,
6080
6402
  FIRE_LOOP,
6081
6403
  FIRE_SOUNDS,