@adatechnology/game-test-room 0.33.0 → 0.33.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.
@@ -1429,21 +1429,19 @@ var GameTestRoom = (() => {
1429
1429
  for (const row of rows.values()) for (const i of row) if (!held.has(i)) loose.push(i);
1430
1430
  return loose;
1431
1431
  };
1432
- var clumpCells = (cells, thin, cap) => {
1432
+ var clumpUnits = (cells, thin, unit, cap) => {
1433
1433
  if (cells.length <= cap) return [...cells];
1434
1434
  const [u, v] = planeAxes(thin);
1435
- let lo0 = Infinity, lo1 = Infinity, hi0 = -Infinity, hi1 = -Infinity;
1435
+ let lo0 = Infinity, lo1 = Infinity;
1436
1436
  for (const cell of cells) {
1437
1437
  lo0 = Math.min(lo0, cell.center[u] - cell.size[u] / 2);
1438
- hi0 = Math.max(hi0, cell.center[u] + cell.size[u] / 2);
1439
1438
  lo1 = Math.min(lo1, cell.center[v] - cell.size[v] / 2);
1440
- hi1 = Math.max(hi1, cell.center[v] + cell.size[v] / 2);
1441
1439
  }
1442
- let side = Math.sqrt((hi0 - lo0) * (hi1 - lo1) / cap);
1443
- for (; ; ) {
1440
+ let along4 = 1, up = 1;
1441
+ for (let grow = 0; ; grow++) {
1444
1442
  const bins = /* @__PURE__ */ new Map();
1445
1443
  for (const cell of cells) {
1446
- const key2 = `${Math.floor((cell.center[u] - lo0) / side)}:${Math.floor((cell.center[v] - lo1) / side)}`;
1444
+ const key2 = `${Math.floor((cell.center[u] - lo0) / (along4 * unit[0]))}:${Math.floor((cell.center[v] - lo1) / (up * unit[1]))}`;
1447
1445
  const bin = bins.get(key2) ?? { lo: [Infinity, Infinity, Infinity], hi: [-Infinity, -Infinity, -Infinity] };
1448
1446
  for (let k = 0; k < 3; k++) {
1449
1447
  bin.lo[k] = Math.min(bin.lo[k], cell.center[k] - cell.size[k] / 2);
@@ -1454,7 +1452,8 @@ var GameTestRoom = (() => {
1454
1452
  if (bins.size <= cap) {
1455
1453
  return [...bins.values()].map(({ lo, hi }) => ({ center: [(lo[0] + hi[0]) / 2, (lo[1] + hi[1]) / 2, (lo[2] + hi[2]) / 2], size: [hi[0] - lo[0], hi[1] - lo[1], hi[2] - lo[2]] }));
1456
1454
  }
1457
- side *= 1.25;
1455
+ if (grow % 2 === 0) up++;
1456
+ else along4++;
1458
1457
  }
1459
1458
  };
1460
1459
  var MAX_SPALLS = 64;
@@ -1523,6 +1522,39 @@ var GameTestRoom = (() => {
1523
1522
  for (const x of spread(dims[u], COVER, spacing, MAX_BARS_PER_DIRECTION)) out.push(bar(v, dims[v] / 2 - 0.02, [[u, x], [thin, 0]]));
1524
1523
  return out;
1525
1524
  };
1525
+ var STIRRUP_SPACING = 0.15;
1526
+ var memberStirrups = (dims, spacing = STIRRUP_SPACING) => {
1527
+ if (!isMember(dims)) return [];
1528
+ const long = dims.indexOf(Math.max(...dims));
1529
+ const [u = 0, v = 0] = [0, 1, 2].filter((i) => i !== long);
1530
+ const hu = dims[u] / 2 - COVER, hv = dims[v] / 2 - COVER;
1531
+ const out = [];
1532
+ for (const s of spread(dims[long], 0.05, spacing, 200)) {
1533
+ const corner = (x, y) => {
1534
+ const c = [0, 0, 0];
1535
+ c[long] = s;
1536
+ c[u] = x;
1537
+ c[v] = y;
1538
+ return c;
1539
+ };
1540
+ out.push([corner(-hu, -hv), corner(hu, -hv)], [corner(hu, -hv), corner(hu, hv)], [corner(hu, hv), corner(-hu, hv)], [corner(-hu, hv), corner(-hu, -hv)]);
1541
+ }
1542
+ return out;
1543
+ };
1544
+ var clipBarsToVolume = (bars, center, size) => {
1545
+ const out = [];
1546
+ for (const [a, b] of bars) {
1547
+ const axis2 = [0, 1, 2].find((k) => a[k] !== b[k]) ?? 0;
1548
+ if ([0, 1, 2].some((k) => k !== axis2 && Math.abs(a[k] - center[k]) > size[k] / 2 + 1e-6)) continue;
1549
+ const lo = Math.max(Math.min(a[axis2], b[axis2]), center[axis2] - size[axis2] / 2), hi = Math.min(Math.max(a[axis2], b[axis2]), center[axis2] + size[axis2] / 2);
1550
+ if (hi - lo < 1e-6) continue;
1551
+ const p = [a[0], a[1], a[2]], q = [b[0], b[1], b[2]];
1552
+ p[axis2] = lo;
1553
+ q[axis2] = hi;
1554
+ out.push([p, q]);
1555
+ }
1556
+ return out;
1557
+ };
1526
1558
  var clipBarsToBox = (bars, u, v, center, size) => {
1527
1559
  const out = [];
1528
1560
  for (const [a, b] of bars) {
@@ -1926,6 +1958,120 @@ var GameTestRoom = (() => {
1926
1958
  const stable = converged && cholesky3(ev.Jt, [0, 0, 0]) !== null;
1927
1959
  return { s, a, b, stable, yielded: ev.yielded, bearing: capSum > 0 ? weight / capSum : Infinity, edge: ev.edge, residual: [ev.r[0], ev.r[1], ev.r[2]] };
1928
1960
  };
1961
+ function voidRuns(flags, step) {
1962
+ const runs = [];
1963
+ let start = -1;
1964
+ for (let i = 0; i <= flags.length; i++) {
1965
+ if (i < flags.length && flags[i]) {
1966
+ if (start < 0) start = i;
1967
+ continue;
1968
+ }
1969
+ if (start >= 0) runs.push({ start: start * step, end: i * step });
1970
+ start = -1;
1971
+ }
1972
+ return runs;
1973
+ }
1974
+ var PILLAR_REACH = 0.15;
1975
+ var pillarNear = (pillars, at3) => pillars.some((p) => Math.abs(p - at3) <= PILLAR_REACH);
1976
+ function splitAtPillars(runs, pillars) {
1977
+ const out = [];
1978
+ for (const run of runs) {
1979
+ const inside = pillars.filter((p) => p > run.start + PILLAR_REACH && p < run.end - PILLAR_REACH).sort((a, b) => a - b);
1980
+ let from = run.start;
1981
+ for (const p of inside) {
1982
+ out.push({ start: from, end: p });
1983
+ from = p;
1984
+ }
1985
+ out.push({ start: from, end: run.end });
1986
+ }
1987
+ return out;
1988
+ }
1989
+ function mergeRuns(runs, check2) {
1990
+ let list = [...runs];
1991
+ for (let changed = true; changed; ) {
1992
+ changed = false;
1993
+ for (let k = 0; k <= list.length && !changed; k++) {
1994
+ const before = list[k - 1];
1995
+ const after2 = list[k];
1996
+ const from = before ? before.end : 0;
1997
+ const to = after2 ? after2.start : check2.length;
1998
+ const soil = to - from;
1999
+ if (soil <= 0 || !before && !after2) continue;
2000
+ if (check2.pillars.some((p) => p >= from - PILLAR_REACH && p <= to + PILLAR_REACH)) continue;
2001
+ if (!before && check2.continuous?.[0] || !after2 && check2.continuous?.[1]) continue;
2002
+ const reaction = (before ? (before.end - before.start) / 2 : 0) + (after2 ? (after2.end - after2.start) / 2 : 0);
2003
+ const narrow = soil < check2.depth;
2004
+ const weak = reaction > (check2.soilSafety - 1) * soil;
2005
+ if (!narrow && !weak) continue;
2006
+ const merged = { start: before ? before.start : 0, end: after2 ? after2.end : check2.length };
2007
+ list = [...list.slice(0, Math.max(0, k - 1)), merged, ...list.slice(after2 ? k + 1 : k)];
2008
+ changed = true;
2009
+ }
2010
+ }
2011
+ return list;
2012
+ }
2013
+ function judgeBeam(flags, step, check2) {
2014
+ const merged = mergeRuns(voidRuns(flags, step), check2);
2015
+ const runs = splitAtPillars(merged, check2.pillars);
2016
+ const behind = (run, fromStart) => fromStart ? (merged.find((m) => m.start >= run.end - 1e-9)?.start ?? check2.length) - run.end : run.start - ([...merged].reverse().find((m) => m.end <= run.start + 1e-9)?.end ?? 0);
2017
+ const verdicts = [];
2018
+ let tips = false;
2019
+ for (const run of runs) {
2020
+ const span = run.end - run.start;
2021
+ const freeStart = run.start <= 1e-9 && !pillarNear(check2.pillars, 0) && !check2.continuous?.[0];
2022
+ const freeEnd = run.end >= check2.length - 1e-9 && !pillarNear(check2.pillars, check2.length) && !check2.continuous?.[1];
2023
+ if (freeStart && freeEnd) {
2024
+ verdicts.push({ ...run, kind: "cantilever", span, moment: Infinity, holds: false, cracks: true });
2025
+ tips = true;
2026
+ continue;
2027
+ }
2028
+ const cantilever = freeStart || freeEnd;
2029
+ const moment = (cantilever ? check2.load * span * span / 2 : check2.load * span * span / 8) * check2.loadFactor;
2030
+ const reach = cantilever ? check2.spanDepth * check2.depth / 2 : check2.spanDepth * check2.depth;
2031
+ const balanced = !cantilever || span <= behind(run, freeStart);
2032
+ const rooted = cantilever && pillarNear(check2.pillars, freeStart ? run.end : run.start);
2033
+ const held = cantilever && !!check2.continuous?.[freeStart ? 1 : 0];
2034
+ const holds2 = span <= reach && moment <= check2.resists && (balanced || rooted);
2035
+ verdicts.push({ ...run, kind: cantilever ? "cantilever" : "span", span, moment, holds: holds2, cracks: moment > check2.cracking });
2036
+ if (cantilever && !holds2 && !balanced && !rooted && !held) tips = true;
2037
+ }
2038
+ return { runs: verdicts, tips };
2039
+ }
2040
+ function judgePlate(check2) {
2041
+ const a = check2.across;
2042
+ const moment = (check2.load * a * a / 8 + check2.points.reduce((sum2, p) => sum2 + p.weight * a / 4, 0)) * check2.loadFactor;
2043
+ const d = Math.max(0.01, check2.effectiveDepth);
2044
+ const punched = check2.points.some((p) => p.weight * check2.loadFactor / (4 * (p.side + d) * d) > check2.punching);
2045
+ return { holds: moment <= check2.resists && !punched, cracks: moment > check2.cracking, moment, punched };
2046
+ }
2047
+ var cross = (o, a, b) => (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]);
2048
+ function convexHull(points) {
2049
+ const sorted = [...points].sort((a, b) => a[0] - b[0] || a[1] - b[1]);
2050
+ if (sorted.length < 3) return sorted;
2051
+ const lower = [];
2052
+ for (const p of sorted) {
2053
+ while (lower.length >= 2 && cross(lower[lower.length - 2], lower[lower.length - 1], p) <= 0) lower.pop();
2054
+ lower.push(p);
2055
+ }
2056
+ const upper = [];
2057
+ for (let i = sorted.length - 1; i >= 0; i--) {
2058
+ const p = sorted[i];
2059
+ while (upper.length >= 2 && cross(upper[upper.length - 2], upper[upper.length - 1], p) <= 0) upper.pop();
2060
+ upper.push(p);
2061
+ }
2062
+ return [...lower.slice(0, -1), ...upper.slice(0, -1)];
2063
+ }
2064
+ function restsOver(point, contacts, margin) {
2065
+ const hull = convexHull(contacts);
2066
+ if (hull.length < 3) return false;
2067
+ for (let i = 0; i < hull.length; i++) {
2068
+ const a = hull[i];
2069
+ const b = hull[(i + 1) % hull.length];
2070
+ const length3 = Math.hypot(b[0] - a[0], b[1] - a[1]) || 1;
2071
+ if (cross(a, b, point) / length3 < margin) return false;
2072
+ }
2073
+ return true;
2074
+ }
1929
2075
  var GROUND_LOSS_DEFAULTS = {
1930
2076
  footingDepth: 0.5,
1931
2077
  spread: 1,
@@ -1933,6 +2079,8 @@ var GameTestRoom = (() => {
1933
2079
  maxSettlement: 0.05,
1934
2080
  crackDistortion: 1 / 300,
1935
2081
  damageDistortion: 1 / 150,
2082
+ bearingCrackDistortion: 1 / 500,
2083
+ bearingDamageDistortion: 1 / 300,
1936
2084
  distortionDamage: 0.3,
1937
2085
  bridgeSpanDepth: 10,
1938
2086
  beamRebarRatio: 4e-3
@@ -1952,33 +2100,77 @@ var GameTestRoom = (() => {
1952
2100
  const steepest = loss.radius * loss.radius / (2 * loss.depth * spread2);
1953
2101
  return Math.max(0, cut2(reach), cut2(Math.min(reach, steepest)), cut2(0));
1954
2102
  };
1955
- var longestRun = (flags) => {
1956
- let best = null;
1957
- let start = -1;
1958
- for (let i = 0; i <= flags.length; i++) {
1959
- if (i < flags.length && flags[i]) {
1960
- if (start < 0) start = i;
1961
- continue;
1962
- }
1963
- if (start >= 0 && (!best || i - start > best.end - best.start)) best = { start, end: i };
1964
- start = -1;
1965
- }
1966
- return best;
2103
+ var relativeToChord = (drops) => {
2104
+ const n = drops.length;
2105
+ if (n < 2) return drops.map(() => 0);
2106
+ const first = drops[0];
2107
+ const last = drops[n - 1];
2108
+ return drops.map((v, k) => v - (first + (last - first) * k / (n - 1)));
1967
2109
  };
1968
2110
  var distortionOf = (drops, step) => {
1969
2111
  const span = Math.max(1, Math.round(DISTORTION_BASE / step));
1970
2112
  let value = 0;
1971
2113
  let at3 = 0;
2114
+ let down = 1;
1972
2115
  for (let i = 0; i < drops.length; i++) {
1973
2116
  const j = Math.min(drops.length - 1, i + span);
1974
2117
  if (j === i) continue;
1975
- const beta = Math.abs(drops[j] - drops[i]) / Math.max((j - i) * step, DISTORTION_BASE);
2118
+ const change = drops[j] - drops[i];
2119
+ const beta = Math.abs(change) / Math.max((j - i) * step, DISTORTION_BASE);
1976
2120
  if (beta > value) {
1977
2121
  value = beta;
1978
2122
  at3 = (i + j) / 2;
2123
+ down = change >= 0 ? 1 : -1;
1979
2124
  }
1980
2125
  }
1981
- return { value, at: at3 };
2126
+ return { value, at: at3, down };
2127
+ };
2128
+ var smoothProfile = (drops, step, window2) => {
2129
+ const half = Math.max(0, Math.round(window2 / step / 2));
2130
+ return drops.map((_, k) => {
2131
+ let sum2 = 0;
2132
+ let count = 0;
2133
+ for (let i = Math.max(0, k - half); i <= Math.min(drops.length - 1, k + half); i++) {
2134
+ sum2 += drops[i];
2135
+ count++;
2136
+ }
2137
+ return sum2 / count;
2138
+ });
2139
+ };
2140
+ var BEAM_SPREAD = 1;
2141
+ var SETTLEMENT_POINTS = 24;
2142
+ var settlementProfile = (drops, step) => {
2143
+ const n = drops.length;
2144
+ if (n === 0) return [0, 0];
2145
+ const length3 = n * step;
2146
+ const count = Math.max(2, Math.min(SETTLEMENT_POINTS, n + 1));
2147
+ const out = [];
2148
+ for (let k = 0; k < count; k++) {
2149
+ const at3 = clampNumber(k / (count - 1) * length3 / step - 0.5, 0, n - 1);
2150
+ const i = Math.floor(at3);
2151
+ const j = Math.min(n - 1, i + 1);
2152
+ out.push(drops[i] + (drops[j] - drops[i]) * (at3 - i));
2153
+ }
2154
+ return out;
2155
+ };
2156
+ var clampNumber = (v, a, b) => Math.max(a, Math.min(b, v));
2157
+ var settlementAt = (settlement, point) => {
2158
+ const d = [settlement.to[0] - settlement.from[0], settlement.to[1] - settlement.from[1], settlement.to[2] - settlement.from[2]];
2159
+ const length22 = d[0] * d[0] + d[1] * d[1] + d[2] * d[2];
2160
+ const t = length22 > 0 ? clampNumber(((point[0] - settlement.from[0]) * d[0] + (point[1] - settlement.from[1]) * d[1] + (point[2] - settlement.from[2]) * d[2]) / length22, 0, 1) : 0;
2161
+ const drops = settlement.drop;
2162
+ if (drops.length === 0) return 0;
2163
+ const at3 = t * (drops.length - 1);
2164
+ const i = Math.floor(at3);
2165
+ const j = Math.min(drops.length - 1, i + 1);
2166
+ return drops[i] + (drops[j] - drops[i]) * (at3 - i);
2167
+ };
2168
+ var mergeSettlement = (old, next) => {
2169
+ if (!old) return next;
2170
+ const same = old.from.every((v, k) => Math.abs(v - next.from[k]) < 1e-6) && old.to.every((v, k) => Math.abs(v - next.to[k]) < 1e-6);
2171
+ if (same && old.drop.length === next.drop.length) return { from: old.from, to: old.to, drop: old.drop.map((v, k) => Math.max(v, next.drop[k])) };
2172
+ const mean = (line) => line.drop.reduce((sum2, v) => sum2 + v, 0) / Math.max(1, line.drop.length);
2173
+ return mean(old) >= mean(next) ? old : next;
1982
2174
  };
1983
2175
  var wind_terrain_default = [
1984
2176
  { id: "I", zg: 250, b: { A: 1.1, B: 1.11, C: 1.12 }, p: { A: 0.06, B: 0.065, C: 0.07 }, z0: 5e-3 },
@@ -3780,6 +3972,13 @@ var GameTestRoom = (() => {
3780
3972
  var SUPPORT_TOLERANCE = 0.05;
3781
3973
  var UNIT_FRAGMENT_FACTOR = 4;
3782
3974
  var BREACH_GRID_CAP = 4096;
3975
+ var MASONRY_CRUMBLE_SPEED = 0.6;
3976
+ var TILE_CLUMPS = 160;
3977
+ var PANEL_EDGE_PILLAR = 2;
3978
+ var PANEL_EDGE_BASE = 4;
3979
+ var PANEL_EDGE_BEAM = 3;
3980
+ var PLASTER_FLAKE_AREA = 0.25;
3981
+ var MAX_PLASTER_FLAKES = 24;
3783
3982
  var CHIP_ENERGY = 200;
3784
3983
  var CHIP_SPLASH = 0.5;
3785
3984
  var CRATER_SPENT = 0.6;
@@ -3790,7 +3989,17 @@ var GameTestRoom = (() => {
3790
3989
  var DISTORTION_GROWTH = 1.2;
3791
3990
  var MAX_SETTLEMENT_CRACKS = 4;
3792
3991
  var SETTLEMENT_CRACK_LENGTH = 0.3;
3992
+ var SETTLEMENT_CRACK_SPACING = 0.4;
3993
+ var SETTLEMENT_CRACK_OPENING = 0.3;
3994
+ var MIN_CRACK_WIDTH = 5e-4;
3995
+ var MAX_CRACK_WIDTH = 0.02;
3996
+ var BESIDE_CRACK_OFFSET = 0.8;
3997
+ var BRICK_STEP = [0.2, 0.1];
3998
+ var SETTLEMENT_FOLLOW_DEPTH = 2;
3793
3999
  var CRACKED_STIFFNESS = 0.4;
4000
+ var PILLAR_SEAT = 0.05;
4001
+ var PUNCHING_SHARE = 0.5;
4002
+ var ARCH_ANGLE = Math.PI / 3;
3794
4003
  var CUT_SLICE = 1;
3795
4004
  var cutFactorOf = (shape) => clamp2(Math.max(...shapeDims(shape)) / CUT_SLICE, 1, 50);
3796
4005
  var clamp2 = (v, a, b) => Math.max(a, Math.min(b, v));
@@ -3866,6 +4075,8 @@ var GameTestRoom = (() => {
3866
4075
  for (const m of options.materials ?? []) materials.set(m.id, m);
3867
4076
  const ambient = options.ambientC ?? 20;
3868
4077
  const maxFragments = options.maxFragments ?? 24;
4078
+ const unitClumps = maxFragments * UNIT_FRAGMENT_FACTOR;
4079
+ const fallenClumps = Math.max(2, Math.round(unitClumps / 4));
3869
4080
  const maxDents = options.maxDents ?? 24;
3870
4081
  const maxHoles = options.maxHoles ?? 32;
3871
4082
  const fireSpread = options.fireSpread ?? 0.6;
@@ -4176,6 +4387,61 @@ var GameTestRoom = (() => {
4176
4387
  return (above2 + p.st.mass * share) / load;
4177
4388
  };
4178
4389
  };
4390
+ const contactsOf = (p, support) => {
4391
+ const box2 = contactBox(p, support);
4392
+ const broken = support.rt.broken;
4393
+ if (!broken?.length) return [box2];
4394
+ let pieces = [box2];
4395
+ for (const [axis2, from, to] of broken) {
4396
+ const a = [p.pos[0], p.pos[1], p.pos[2]];
4397
+ const b = [p.pos[0], p.pos[1], p.pos[2]];
4398
+ a[axis2] = from;
4399
+ b[axis2] = to;
4400
+ const la = toLocal(p.pos, p.q, a);
4401
+ const lb = toLocal(p.pos, p.q, b);
4402
+ const k = [0, 1, 2].reduce((best, c) => Math.abs(lb[c] - la[c]) > Math.abs(lb[best] - la[best]) ? c : best, 0);
4403
+ const lo = Math.min(la[k], lb[k]);
4404
+ const hi = Math.max(la[k], lb[k]);
4405
+ pieces = pieces.flatMap(([min2, max2]) => {
4406
+ if (hi <= min2[k] || lo >= max2[k]) return [[min2, max2]];
4407
+ const out = [];
4408
+ if (lo > min2[k]) {
4409
+ const end3 = [max2[0], max2[1], max2[2]];
4410
+ end3[k] = lo;
4411
+ out.push([min2, end3]);
4412
+ }
4413
+ if (hi < max2[k]) {
4414
+ const start = [min2[0], min2[1], min2[2]];
4415
+ start[k] = hi;
4416
+ out.push([start, max2]);
4417
+ }
4418
+ return out;
4419
+ });
4420
+ }
4421
+ return pieces;
4422
+ };
4423
+ const fixedJoint = (j) => j.spec.kind === "rebar" || j.spec.kind === "fused" || j.spec.kind === "weld";
4424
+ const standsOver = (o, p, lower) => {
4425
+ const role = p.spec.structure?.role;
4426
+ if (role !== "slab" && role !== "roof" || p.spec.shape.kind !== "box" || lower.length === 0) return true;
4427
+ if (Math.abs(rotateVec(p.q, [0, 0, 0].map((_, k) => k === p.thin ? 1 : 0))[1]) < 0.9) return true;
4428
+ const i = o.idx.get(p.spec.id);
4429
+ for (const j of o.byPart.get(p.spec.id) ?? []) {
4430
+ if (j.st.open) continue;
4431
+ if (fixedJoint(j) && lower.includes(restsOn(o, j, i)) || j.spec.kind === "tie") return true;
4432
+ }
4433
+ const corners = [];
4434
+ for (const k of lower) {
4435
+ for (const [lo, hi] of contactsOf(p, o.parts[k])) {
4436
+ for (let c = 0; c < 4; c++) {
4437
+ const local2 = [c & 1 ? hi[0] : lo[0], (lo[1] + hi[1]) / 2, c & 2 ? hi[2] : lo[2]];
4438
+ const at3 = toWorld(p.pos, p.q, local2);
4439
+ corners.push([at3[0], at3[2]]);
4440
+ }
4441
+ }
4442
+ }
4443
+ return restsOver([p.pos[0], p.pos[2]], corners, 0);
4444
+ };
4179
4445
  const borneOn = (p, supports) => {
4180
4446
  if (supports.length === 0) return void 0;
4181
4447
  const boxes = supports.map((support) => contactBox(p, support));
@@ -4231,7 +4497,7 @@ var GameTestRoom = (() => {
4231
4497
  const key2 = lower.join(",");
4232
4498
  if (key2 !== p.spanKey) {
4233
4499
  p.spanKey = key2;
4234
- p.span = spanOf(g, lower.map((k) => contactBox(p, o.parts[k])));
4500
+ p.span = spanOf(g, lower.flatMap((k) => contactsOf(p, o.parts[k])));
4235
4501
  }
4236
4502
  p.restMoment = 0;
4237
4503
  if (g.gravity < 0 || p.span <= 0) return 0;
@@ -4297,9 +4563,13 @@ var GameTestRoom = (() => {
4297
4563
  };
4298
4564
  const freeSpan = (p, supports) => {
4299
4565
  if (supports.length === 0) return Infinity;
4566
+ if (p.spec.shape.kind === "box") {
4567
+ const g = bendGeometry(p.spec);
4568
+ if (!g.member) return spanOf(g, supports.flatMap((k) => contactsOf(p, k)));
4569
+ }
4300
4570
  const d = p.dims;
4301
4571
  const L = [0, 1, 2].sort((a, b) => d[b] - d[a])[0];
4302
- const spans = supports.map((k) => contactBox(p, k)).map(([lo, hi]) => [lo[L], hi[L]]);
4572
+ const spans = supports.flatMap((k) => contactsOf(p, k)).map(([lo, hi]) => [lo[L], hi[L]]);
4303
4573
  let far = 0;
4304
4574
  for (let i = 0; i <= 16; i++) {
4305
4575
  const x = (i / 16 - 0.5) * d[L];
@@ -4349,7 +4619,10 @@ var GameTestRoom = (() => {
4349
4619
  }
4350
4620
  lowerOf.set(i, lower);
4351
4621
  const max2 = parts[i].spec.structure?.maxSpan;
4352
- if (max2 !== void 0 && !parts[i].anchored && freeSpan(parts[i], lower.map((k) => parts[k])) > max2) {
4622
+ const tooLong = max2 !== void 0 && freeSpan(parts[i], lower.map((k) => parts[k])) > max2;
4623
+ const standing = parts[i].heldAsDesigned || standsOver(o, parts[i], lower);
4624
+ if (o.seeding && !standing) parts[i].heldAsDesigned = true;
4625
+ if (!parts[i].anchored && (tooLong || !(standing || parts[i].heldAsDesigned))) {
4353
4626
  out.add(i);
4354
4627
  dropped = true;
4355
4628
  }
@@ -4803,20 +5076,46 @@ var GameTestRoom = (() => {
4803
5076
  if (!o.hasStructure || !o.parts.some((p) => p.anchored)) return;
4804
5077
  if (!pendingCollapse.has(o)) pendingCollapse.set(o, src);
4805
5078
  };
5079
+ const isLaidMasonry = (p) => !!p.spec.units && p.spec.units.mortar !== p.spec.material && !p.spec.reinforcement && !p.st.stripped;
5080
+ const isTiledCover = (p) => p.spec.structure?.role === "roof" && p.mat.fracture === "shatter" && !p.spec.reinforcement && !p.spec.units;
5081
+ const crumbleMasonry = (o, p, src) => {
5082
+ if (isLost(p)) return;
5083
+ const [u, v] = planeAxes(p.thin);
5084
+ const grid = gridOf(p);
5085
+ const worn = p.rt.chips ?? {};
5086
+ const op = p.st.opening ? toLocal(p.pos, p.q, p.st.opening.center) : null;
5087
+ const cells = grid.filter((cell, i) => worn[i] !== -1 && !(op && p.st.opening && Math.hypot(cell.center[u] - op[u], cell.center[v] - op[v]) <= p.st.opening.radius));
5088
+ const { c } = partXf(o, p);
5089
+ const fragments = cells.length > 0 ? [
5090
+ ...cellFragments(o, p, clumpUnits(cells, p.thin, unitSpan(p), isTiledCover(p) ? TILE_CLUMPS : unitClumps), vec.add(c, [0, 1, 0]), null, MASONRY_CRUMBLE_SPEED, p.st.mass, "m_", true),
5091
+ ...plasterFlakes(o, p, cells, vec.add(c, [0, 1, 0]), MASONRY_CRUMBLE_SPEED, "mp_")
5092
+ ] : [];
5093
+ p.st.status = "broken";
5094
+ p.st.hp = 0;
5095
+ p.st.mass = 0;
5096
+ emit2({ ...base(o, src), type: "fracture", part: p.spec.id, style: "crumble", fragments, point: c });
5097
+ debris3(o, p, p.mat.breakDebris, c, [0, 1, 0], clamp2(Math.round(Math.cbrt(partVolume(p.spec.shape)) * 30), 3, 20), 2, src);
5098
+ setStage(o, p, 3, {}, src);
5099
+ partLost(o, p, src);
5100
+ };
4806
5101
  const flushCollapse = () => {
4807
5102
  for (const [o, src] of [...pendingCollapse]) {
4808
5103
  pendingCollapse.delete(o);
4809
5104
  if (!objects.has(o.id) || objects.get(o.id) !== o) continue;
4810
- for (const g of o.st.groups) {
5105
+ const crumbling = [];
5106
+ for (const g of [...o.st.groups]) {
4811
5107
  const ps = g.map((id) => o.byId.get(id)).filter((p) => !isLost(p) && !o.collapsed.has(p.spec.id));
4812
5108
  if (ps.length === 0 || g.some((id) => o.byId.get(id).anchored) || !ps.some((p) => p.structural)) continue;
4813
- for (const p of ps) {
5109
+ const whole2 = ps.filter((p) => !isLaidMasonry(p) && !isTiledCover(p));
5110
+ crumbling.push(...ps.filter((p) => isLaidMasonry(p) || isTiledCover(p)));
5111
+ for (const p of whole2) {
4814
5112
  o.collapsed.add(p.spec.id);
4815
5113
  p.st.status = "collapsed";
4816
5114
  setStage(o, p, 3, {}, src);
4817
5115
  }
4818
- emit2({ ...base(o, src), type: "collapse", parts: ps.map((p) => p.spec.id), mass: ps.reduce((sum2, p) => sum2 + p.st.mass, 0) });
5116
+ if (whole2.length > 0) emit2({ ...base(o, src), type: "collapse", parts: whole2.map((p) => p.spec.id), mass: whole2.reduce((sum2, p) => sum2 + p.st.mass, 0) });
4819
5117
  }
5118
+ for (const p of crumbling) crumbleMasonry(o, p, src);
4820
5119
  }
4821
5120
  };
4822
5121
  const groundCfg = resolveGroundLoss(options.groundLoss);
@@ -4857,17 +5156,27 @@ var GameTestRoom = (() => {
4857
5156
  let sum2 = 0;
4858
5157
  let worst = -1;
4859
5158
  let deepest = toWorld(o.st.position, o.st.rotation, [(box2.min[0] + box2.max[0]) / 2, box2.min[1], (box2.min[2] + box2.max[2]) / 2]);
5159
+ let voidBox = null;
4860
5160
  for (let k = 0; k < n; k++) {
4861
5161
  let open = 0;
4862
5162
  let drop = 0;
5163
+ let cutSum = 0;
4863
5164
  for (let c = 0; c < m; c++) {
4864
5165
  const local2 = [0, box2.min[1], 0];
4865
5166
  local2[long] = box2.min[long] + (k + 0.5) * step;
4866
5167
  local2[across] = box2.min[across] + (c + 0.5) * (box2.max[across] - box2.min[across]) / m;
4867
5168
  const world2 = toWorld(o.st.position, o.st.rotation, local2);
4868
5169
  const cut2 = undercutAt(loss, Math.hypot(world2[0] - loss.x, world2[2] - loss.z), b.embed, groundCfg.spread);
4869
- if (cut2 >= groundCfg.voidDepth) open++;
5170
+ if (cut2 >= groundCfg.voidDepth) {
5171
+ open++;
5172
+ const along4 = local2[long] - box2.min[long];
5173
+ const side = local2[across] - box2.min[across];
5174
+ voidBox ?? (voidBox = { long: [along4, along4], across: [side, side] });
5175
+ voidBox.long = [Math.min(voidBox.long[0], along4 - step / 2), Math.max(voidBox.long[1], along4 + step / 2)];
5176
+ voidBox.across = [Math.min(voidBox.across[0], side), Math.max(voidBox.across[1], side)];
5177
+ }
4870
5178
  drop += Math.min(cut2, groundCfg.maxSettlement);
5179
+ cutSum += cut2;
4871
5180
  if (cut2 > worst) {
4872
5181
  worst = cut2;
4873
5182
  deepest = world2;
@@ -4875,56 +5184,326 @@ var GameTestRoom = (() => {
4875
5184
  }
4876
5185
  voids += open;
4877
5186
  sum2 += drop;
4878
- slices.push({ void: open * 2 > m, drop: drop / m });
5187
+ slices.push({ void: open * 2 > m, drop: drop / m, cut: cutSum / m });
4879
5188
  }
4880
- return { long, step, slices, share: voids / (n * m), drop: sum2 / (n * m), deepest };
5189
+ return { long, step, slices, voidBox, share: voids / (n * m), drop: sum2 / (n * m), deepest };
4881
5190
  };
4882
- const bridgeOf = (b, fp, voids) => {
4883
- const p = b.p;
5191
+ const sectionOfBearer = (p) => {
4884
5192
  if (p.spec.shape.kind !== "box" || !p.mat.strength?.tensileMPa) return null;
4885
5193
  const g = bendGeometry(p.spec);
4886
5194
  if (g.gravity < 0) return null;
4887
5195
  const footing = p.spec.structure?.role === "foundation" && g.member && !isReinforced(p.spec);
4888
5196
  const spec = footing ? { ...p.spec, structure: { ...p.spec.structure, rebar: { ratio: groundCfg.beamRebarRatio } } } : p.spec;
4889
5197
  const section = { b: sectionWidth(g, g.gravity), h: g.dims[g.gravity] };
4890
- const m = sectionMoments(p.mat, section, tensionSteel(spec, g.dims, section, flexure), flexure);
4891
- const span = (voids.end - voids.start) * fp.step;
4892
- const cantilever = voids.start === 0 || voids.end === fp.slices.length;
4893
- const w = lineLoad(g, p.st.load ?? p.st.mass);
4894
- const moment = (cantilever ? w * span * span / 2 : spanMoment(w, span)) * structureSafety;
4895
- const whole2 = voids.start === 0 && voids.end === fp.slices.length;
4896
- const holds2 = !whole2 && span <= groundCfg.bridgeSpanDepth * section.h && moment <= (p.st.cracked ? m.steel : m.ultimate);
4897
- const cracks = moment > m.cracking;
4898
- const EI = (p.mat.strength.elasticGPa ?? 0) * 1e9 * section.b * section.h ** 3 * (cracks || p.st.cracked ? CRACKED_STIFFNESS : 1) / 12;
4899
- const sag = EI > 0 ? w * span ** 4 / (cantilever ? 8 * EI : 384 / 5 * EI) : groundCfg.maxSettlement;
4900
- return { holds: holds2, cracks, cantilever, span, depth: section.h, sag };
4901
- };
4902
- const crackBridge = (o, b, fp, voids, bridge, src) => {
4903
- if (b.p.st.cracked) return;
5198
+ const steel = tensionSteel(spec, g.dims, section, flexure);
5199
+ const m = sectionMoments(p.mat, section, steel, flexure);
5200
+ return { g, section, steel, m, resists: p.st.cracked ? m.steel : m.ultimate };
5201
+ };
5202
+ const pillarsUnder = (o, b, fp, standing) => {
5203
+ const box2 = aabbOf(b.p.spec.shape, b.p.pos, b.p.q);
5204
+ const across = fp.long === 0 ? 2 : 0;
5205
+ const out = [];
5206
+ for (const i of standing) {
5207
+ const q = o.parts[i];
5208
+ if (q.spec.structure?.role !== "column") continue;
5209
+ const c = aabbOf(q.spec.shape, q.pos, q.q);
5210
+ const overlaps = c.max[across] > box2.min[across] && c.min[across] < box2.max[across] && c.max[fp.long] > box2.min[fp.long] && c.min[fp.long] < box2.max[fp.long];
5211
+ if (overlaps && Math.abs(c.min[1] - box2.max[1]) < PILLAR_SEAT) out.push((c.min[fp.long] + c.max[fp.long]) / 2 - box2.min[fp.long]);
5212
+ }
5213
+ return out;
5214
+ };
5215
+ const continuationOf = (o, b, fp, standing) => {
5216
+ const box2 = aabbOf(b.p.spec.shape, b.p.pos, b.p.q);
5217
+ const across = fp.long === 0 ? 2 : 0;
5218
+ const ends = [false, false];
5219
+ for (const i of standing) {
5220
+ const q = o.parts[i];
5221
+ if (i === b.i || q.spec.structure?.role !== "foundation" || isTerrain(q)) continue;
5222
+ const c = aabbOf(q.spec.shape, q.pos, q.q);
5223
+ const inLine = Math.abs((c.min[across] + c.max[across]) / 2 - (box2.min[across] + box2.max[across]) / 2) < PILLAR_SEAT && c.max[1] > box2.min[1] && c.min[1] < box2.max[1];
5224
+ if (!inLine) continue;
5225
+ if (Math.abs(c.max[fp.long] - box2.min[fp.long]) < PILLAR_SEAT) ends[0] = true;
5226
+ if (Math.abs(c.min[fp.long] - box2.max[fp.long]) < PILLAR_SEAT) ends[1] = true;
5227
+ }
5228
+ return ends;
5229
+ };
5230
+ const bearBeam = (o, b, fp, standing, sec) => {
5231
+ const length3 = fp.slices.length * fp.step;
5232
+ const load = lineLoad(sec.g, b.p.st.load ?? b.p.st.mass);
5233
+ const verdict = judgeBeam(fp.slices.map((slice) => slice.void), fp.step, {
5234
+ length: length3,
5235
+ depth: sec.section.h,
5236
+ load,
5237
+ resists: sec.resists,
5238
+ cracking: sec.m.cracking,
5239
+ spanDepth: groundCfg.bridgeSpanDepth,
5240
+ soilSafety: soilCfg.safety,
5241
+ pillars: pillarsUnder(o, b, fp, standing),
5242
+ loadFactor: structureSafety,
5243
+ continuous: continuationOf(o, b, fp, standing)
5244
+ });
5245
+ if (verdict.runs.length === 0) return { status: fp.share <= 1 - 1 / soilCfg.safety ? "settled" : "unsupported", failed: [], cracks: [] };
5246
+ if (verdict.tips) return { status: "unsupported", failed: [], cracks: [] };
5247
+ const cracks = [];
5248
+ const failed = [];
5249
+ const EI = (b.p.mat.strength.elasticGPa ?? 0) * 1e9 * sec.section.b * sec.section.h ** 3 / 12;
5250
+ for (const run2 of verdict.runs) {
5251
+ const k0 = Math.round(run2.start / fp.step);
5252
+ const k1 = Math.round(run2.end / fp.step);
5253
+ if (!run2.holds) {
5254
+ const free = run2.kind !== "cantilever" ? void 0 : run2.start <= 1e-9 ? "start" : "end";
5255
+ failed.push({ start: run2.start, end: run2.end, ...free ? { free } : {} });
5256
+ if (free !== "start") cracks.push({ at: run2.start, top: true });
5257
+ if (free !== "end") cracks.push({ at: run2.end, top: true });
5258
+ for (let k = k0; k < k1; k++) fp.slices[k].drop = Math.min(fp.slices[k].cut, run2.span / 2);
5259
+ continue;
5260
+ }
5261
+ if (run2.cracks) cracks.push(run2.kind === "cantilever" ? { at: run2.start <= 1e-9 ? run2.end : run2.start, top: true } : { at: (run2.start + run2.end) / 2, top: false });
5262
+ const stiffness = EI * (run2.cracks || b.p.st.cracked ? CRACKED_STIFFNESS : 1);
5263
+ const sag = stiffness > 0 ? load * run2.span ** 4 / (run2.kind === "cantilever" ? 8 * stiffness : 384 / 5 * stiffness) : groundCfg.maxSettlement;
5264
+ for (let k = k0; k < k1; k++) fp.slices[k].drop = Math.min(fp.slices[k].drop, sag);
5265
+ }
5266
+ return { status: failed.length ? "opened" : "bridged", failed, cracks };
5267
+ };
5268
+ const standingOver = (o, b, fp) => {
5269
+ const box2 = aabbOf(b.p.spec.shape, b.p.pos, b.p.q);
5270
+ const vb = fp.voidBox;
5271
+ if (!vb) return [];
5272
+ const across = fp.long === 0 ? 2 : 0;
5273
+ const out = [];
5274
+ for (const j of o.byPart.get(b.p.spec.id) ?? []) {
5275
+ const k = j.ia === b.i ? j.ib : j.ia;
5276
+ if (j.st.open || restsOn(o, j, k) !== b.i) continue;
5277
+ const q = o.parts[k];
5278
+ const c = aabbOf(q.spec.shape, q.pos, q.q);
5279
+ const along4 = (c.min[fp.long] + c.max[fp.long]) / 2 - box2.min[fp.long];
5280
+ const side = (c.min[across] + c.max[across]) / 2 - box2.min[across];
5281
+ if (along4 >= vb.long[0] && along4 <= vb.long[1] && side >= vb.across[0] && side <= vb.across[1]) out.push({ q, j });
5282
+ }
5283
+ return out;
5284
+ };
5285
+ const bearPlate = (o, b, fp, sec) => {
5286
+ const vb = fp.voidBox;
5287
+ if (!vb) return { status: "settled", failed: [], cracks: [] };
5288
+ const across = Math.max(vb.long[1] - vb.long[0], vb.across[1] - vb.across[0]);
5289
+ const over = standingOver(o, b, fp);
5290
+ const verdict = judgePlate({
5291
+ across,
5292
+ load: lineLoad(sec.g, b.p.st.mass),
5293
+ resists: sec.resists,
5294
+ cracking: sec.m.cracking,
5295
+ effectiveDepth: sec.steel.d,
5296
+ punching: PUNCHING_SHARE * (b.p.mat.strength.tensileMPa ?? 0) * 1e6,
5297
+ loadFactor: structureSafety,
5298
+ points: over.map(({ q }) => {
5299
+ const c = aabbOf(q.spec.shape, q.pos, q.q);
5300
+ return { weight: (q.st.load ?? q.st.mass) * GRAVITY2, side: Math.min(c.max[0] - c.min[0], c.max[2] - c.min[2]) };
5301
+ })
5302
+ });
5303
+ if (fp.share > 1 - 1 / soilCfg.safety) return { status: "unsupported", failed: [], cracks: [] };
5304
+ const cracks = verdict.cracks || !verdict.holds ? [{ at: vb.long[0], top: true }, { at: vb.long[1], top: true }] : [];
5305
+ if (verdict.holds) return { status: "bridged", failed: [], cracks };
5306
+ const box2 = aabbOf(b.p.spec.shape, b.p.pos, b.p.q);
5307
+ const crossing = fp.long === 0 ? 2 : 0;
5308
+ const center = [(box2.min[0] + box2.max[0]) / 2, (box2.min[1] + box2.max[1]) / 2, (box2.min[2] + box2.max[2]) / 2];
5309
+ center[fp.long] = box2.min[fp.long] + (vb.long[0] + vb.long[1]) / 2;
5310
+ center[crossing] = box2.min[crossing] + (vb.across[0] + vb.across[1]) / 2;
5311
+ const radius = Math.max(vb.long[1] - vb.long[0], vb.across[1] - vb.across[0]) / 2;
5312
+ if (2 * radius >= Math.min(box2.max[0] - box2.min[0], box2.max[2] - box2.min[2])) return { status: "unsupported", failed: [], cracks: [] };
5313
+ return { status: "opened", failed: [{ start: vb.long[0], end: vb.long[1] }], cracks, hole: { center, radius } };
5314
+ };
5315
+ const bearLoss = (o, b, fp, standing) => {
5316
+ const sec = sectionOfBearer(b.p);
5317
+ if (sec?.g.member) return bearBeam(o, b, fp, standing, sec);
5318
+ if (sec && fp.voidBox) return bearPlate(o, b, fp, sec);
5319
+ return { status: fp.share <= 1 - 1 / soilCfg.safety ? "settled" : "unsupported", failed: [], cracks: [] };
5320
+ };
5321
+ const crackBearer = (o, b, fp, bearing, src) => {
5322
+ if (bearing.cracks.length === 0) return;
4904
5323
  b.p.st.cracked = true;
4905
5324
  const box2 = aabbOf(b.p.spec.shape, b.p.pos, b.p.q);
4906
- const local2 = [(box2.min[0] + box2.max[0]) / 2, bridge.cantilever ? box2.max[1] : box2.min[1], (box2.min[2] + box2.max[2]) / 2];
4907
- const edge = voids.start === 0 ? voids.end : voids.start;
4908
- local2[fp.long] = box2.min[fp.long] + (bridge.cantilever ? edge : (voids.start + voids.end) / 2) * fp.step;
4909
- addCrack(o, b.p, toWorld(o.st.position, o.st.rotation, local2), rotateVec(o.st.rotation, [0, bridge.cantilever ? 1 : -1, 0]), bridge.depth, src);
5325
+ for (const crack2 of bearing.cracks) {
5326
+ const local2 = [(box2.min[0] + box2.max[0]) / 2, crack2.top ? box2.max[1] : box2.min[1], (box2.min[2] + box2.max[2]) / 2];
5327
+ local2[fp.long] = box2.min[fp.long] + crack2.at;
5328
+ addCrack(o, b.p, toWorld(o.st.position, o.st.rotation, local2), rotateVec(o.st.rotation, [0, crack2.top ? 1 : -1, 0]), box2.max[1] - box2.min[1], src);
5329
+ }
5330
+ };
5331
+ const openOver = (o, b, fp, failed, opened, src) => {
5332
+ const box2 = aabbOf(b.p.spec.shape, b.p.pos, b.p.q);
5333
+ let lost = false;
5334
+ for (const j of o.byPart.get(b.p.spec.id) ?? []) {
5335
+ const k = j.ia === b.i ? j.ib : j.ia;
5336
+ if (j.st.open || restsOn(o, j, k) !== b.i) continue;
5337
+ const q = o.parts[k];
5338
+ if (isLost(q)) continue;
5339
+ const c = aabbOf(q.spec.shape, q.pos, q.q);
5340
+ const from = c.min[fp.long] - box2.min[fp.long];
5341
+ const to = c.max[fp.long] - box2.min[fp.long];
5342
+ const run2 = failed.find((r) => to > r.start && from < r.end);
5343
+ if (!run2) continue;
5344
+ if (from >= run2.start - fp.step && to <= run2.end + fp.step) {
5345
+ j.st.open = true;
5346
+ j.st.hp = 0;
5347
+ j.st.openedBy = "break";
5348
+ opened.push(j);
5349
+ lost = true;
5350
+ continue;
5351
+ }
5352
+ if (isSlender(q.spec, q.mat) && archOver(o, q, fp.long, [box2.min[fp.long] + run2.start, box2.min[fp.long] + run2.end], run2.free, opened, src)) lost = true;
5353
+ }
5354
+ return lost;
5355
+ };
5356
+ const archOver = (o, q, axis2, span, free, opened, src) => {
5357
+ var _a;
5358
+ const grid = gridOf(q);
5359
+ const worn = (_a = q.rt).chips ?? (_a.chips = {});
5360
+ const bottom = aabbOf(q.spec.shape, q.pos, q.q).min[1];
5361
+ const half = (span[1] - span[0]) / 2;
5362
+ const middle = (span[0] + span[1]) / 2;
5363
+ const out = [];
5364
+ grid.forEach((cell, i) => {
5365
+ if (worn[i] === -1) return;
5366
+ const at3 = toWorld(q.pos, q.q, cell.center);
5367
+ const height2 = at3[1] - bottom;
5368
+ const reach = height2 / Math.tan(ARCH_ANGLE);
5369
+ const under = free === "start" ? at3[axis2] <= span[1] - reach : free === "end" ? at3[axis2] >= span[0] + reach : Math.abs(at3[axis2] - middle) <= half - reach;
5370
+ if (under) out.push(i);
5371
+ });
5372
+ if (out.length === 0) return false;
5373
+ const centre = [0, bottom, 0];
5374
+ centre[axis2] = middle;
5375
+ const other = axis2 === 0 ? 2 : 0;
5376
+ centre[other] = q.pos[other];
5377
+ knockOutUnits(o, q, toWorld(o.st.position, o.st.rotation, centre), 0, out, src);
5378
+ const height = aabbOf(q.spec.shape, q.pos, q.q).max[1] - bottom;
5379
+ const corbel = height / Math.tan(ARCH_ANGLE);
5380
+ const opening = free === "start" ? [span[0], span[1] - corbel] : free === "end" ? [span[0] + corbel, span[1]] : [span[0] + corbel, span[1] - corbel];
5381
+ return opening[1] > opening[0] && spanOpening(o, q, axis2, opening, !!free, opened, src);
5382
+ };
5383
+ const spanOpening = (o, q, axis2, opening, overhang, opened, src) => {
5384
+ const qi = o.idx.get(q.spec.id);
5385
+ const length3 = opening[1] - opening[0];
5386
+ let lost = false;
5387
+ for (const j of [...o.byPart.get(q.spec.id) ?? []]) {
5388
+ const ri = j.ia === qi ? j.ib : j.ia;
5389
+ const r = o.parts[ri];
5390
+ if (j.st.open || isLost(r) || restsOn(o, j, ri) !== qi || !r.structural) continue;
5391
+ const sec = sectionOfBearer(r);
5392
+ if (sec && !sec.g.member && !restsOnlyOn(o, ri, qi)) continue;
5393
+ const load = sec ? lineLoad(sec.g, r.st.load ?? r.st.mass) : 0;
5394
+ const moment = load * length3 * length3 / (overhang ? 2 : 8) * structureSafety;
5395
+ const reach = (overhang ? 0.5 : 1) * groundCfg.bridgeSpanDepth * (sec?.section.h ?? 0);
5396
+ if (sec && length3 <= reach && moment <= sec.resists) {
5397
+ if (moment > sec.m.cracking && !r.st.cracked) {
5398
+ r.st.cracked = true;
5399
+ addCrack(o, r, partXf(o, r).c, rotateVec(o.st.rotation, [0, -1, 0]), sec.section.h, src);
5400
+ }
5401
+ continue;
5402
+ }
5403
+ if (sec?.g.member) {
5404
+ if (breakOver(o, r, axis2, opening, opened, src)) lost = true;
5405
+ continue;
5406
+ }
5407
+ hingeDrop(o, r, src);
5408
+ lost = true;
5409
+ }
5410
+ return lost;
5411
+ };
5412
+ const breakOver = (o, r, axis2, stretch, opened, src) => {
5413
+ var _a;
5414
+ const ri = o.idx.get(r.spec.id);
5415
+ ((_a = r.rt).broken ?? (_a.broken = [])).push([axis2, stretch[0], stretch[1]]);
5416
+ r.st.cracked = true;
5417
+ const box2 = aabbOf(r.spec.shape, r.pos, r.q);
5418
+ for (const edge of stretch) {
5419
+ const at3 = [(box2.min[0] + box2.max[0]) / 2, box2.max[1], (box2.min[2] + box2.max[2]) / 2];
5420
+ at3[axis2] = edge;
5421
+ addCrack(o, r, toWorld(o.st.position, o.st.rotation, at3), rotateVec(o.st.rotation, [0, 1, 0]), box2.max[1] - box2.min[1], src);
5422
+ }
5423
+ const from = [(box2.min[0] + box2.max[0]) / 2, box2.min[1], (box2.min[2] + box2.max[2]) / 2];
5424
+ const to = [from[0], from[1], from[2]];
5425
+ from[axis2] = box2.min[axis2];
5426
+ to[axis2] = box2.max[axis2];
5427
+ const count = SETTLEMENT_POINTS;
5428
+ const drop = Array.from({ length: count }, (_, k) => {
5429
+ const x = box2.min[axis2] + (box2.max[axis2] - box2.min[axis2]) * k / (count - 1);
5430
+ return x <= stretch[0] || x >= stretch[1] ? 0 : Math.min(x - stretch[0], stretch[1] - x, Math.max(0, box2.min[1]));
5431
+ });
5432
+ r.st.settlement = mergeSettlement(r.st.settlement, { from, to, drop });
5433
+ r.st.subsidence = Math.max(r.st.subsidence ?? 0, drop.reduce((sum2, v) => sum2 + v, 0) / count);
5434
+ const middle = [from[0], from[1], from[2]];
5435
+ middle[axis2] = (stretch[0] + stretch[1]) / 2;
5436
+ emit2({ ...base(o, src), type: "subside", part: r.spec.id, drop: r.st.subsidence, share: (stretch[1] - stretch[0]) / Math.max(1e-6, box2.max[axis2] - box2.min[axis2]), point: toWorld(o.st.position, o.st.rotation, middle), status: "opened" });
5437
+ let lost = false;
5438
+ for (const j of [...o.byPart.get(r.spec.id) ?? []]) {
5439
+ const ki = j.ia === ri ? j.ib : j.ia;
5440
+ const k = o.parts[ki];
5441
+ if (j.st.open || isLost(k) || restsOn(o, j, ki) !== ri) continue;
5442
+ const c = aabbOf(k.spec.shape, k.pos, k.q);
5443
+ const within = c.min[axis2] >= stretch[0] - SUPPORT_TOLERANCE && c.max[axis2] <= stretch[1] + SUPPORT_TOLERANCE;
5444
+ const sec = within ? null : sectionOfBearer(k);
5445
+ const plate = sec && !sec.g.member;
5446
+ const length3 = stretch[1] - stretch[0];
5447
+ const spans = plate && lineLoad(sec.g, k.st.load ?? k.st.mass) * length3 * length3 / 8 * structureSafety <= sec.resists;
5448
+ if (!within && (!plate || spans)) continue;
5449
+ j.st.open = true;
5450
+ j.st.hp = 0;
5451
+ j.st.openedBy = "break";
5452
+ opened.push(j);
5453
+ lost = true;
5454
+ }
5455
+ return lost;
4910
5456
  };
4911
- const distort = (o, q, beta, at3, src) => {
5457
+ const breakOut = (o, p, hole, src) => {
5458
+ if (p.st.opening) return;
5459
+ const lc = toLocal(p.pos, p.q, hole.center);
5460
+ lc[p.thin] = 0;
5461
+ const worldCenter = toWorld(o.st.position, o.st.rotation, toWorld(p.pos, p.q, lc));
5462
+ const [u, v] = planeAxes(p.thin);
5463
+ const worn = p.rt.chips;
5464
+ const cells = cellsWithin(gridOf(p).filter((_, i) => worn?.[i] !== -1), p.thin, lc, hole.radius);
5465
+ const removed = Math.min(p.st.mass, p.st.mass * Math.PI * hole.radius * hole.radius / (p.dims[u] * p.dims[v]));
5466
+ const fragments = cellFragments(o, p, cells, worldCenter, [0, -1, 0], 0.5, removed, "g");
5467
+ p.st.mass = Math.max(0, p.st.mass - removed);
5468
+ p.rt.removed = (p.rt.removed ?? 0) + removed;
5469
+ p.st.opening = { center: objLocal(o, worldCenter), radius: hole.radius };
5470
+ markDamaged(p);
5471
+ if (p.staged) {
5472
+ setStage(o, p, 1, {}, src);
5473
+ setStage(o, p, 2, { opening: { center: worldCenter, radius: hole.radius }, fragments }, src);
5474
+ } else emit2({ ...base(o, src), type: "stage", part: p.spec.id, stage: 2, opening: { center: worldCenter, radius: hole.radius }, fragments });
5475
+ debris3(o, p, p.mat.breakDebris, worldCenter, [0, 1, 0], 14, 2, src);
5476
+ if (p.spec.reinforcement) exposeRebar(o, p, src);
5477
+ };
5478
+ const distort = (o, q, beta, at3, toward2, src) => {
4912
5479
  const prior = q.rt.distortion ?? 0;
4913
- if (beta < groundCfg.crackDistortion || beta <= prior * DISTORTION_GROWTH) return;
5480
+ const bearing = q.spec.structure?.role === "wall";
5481
+ const crackAt = bearing ? groundCfg.bearingCrackDistortion : groundCfg.crackDistortion;
5482
+ const damageAt = bearing ? groundCfg.bearingDamageDistortion : groundCfg.damageDistortion;
5483
+ if (beta < crackAt || beta <= prior * DISTORTION_GROWTH) return;
4914
5484
  q.rt.distortion = beta;
4915
5485
  const box2 = aabbOf(q.spec.shape, q.pos, q.q);
4916
5486
  const height = box2.max[1] - box2.min[1];
4917
- const cracks = (d) => d < groundCfg.crackDistortion ? 0 : Math.min(MAX_SETTLEMENT_CRACKS, 1 + Math.floor(Math.log2(d / groundCfg.crackDistortion)));
5487
+ const cracks = (d) => d < crackAt ? 0 : Math.min(MAX_SETTLEMENT_CRACKS, 1 + Math.floor(Math.log2(d / crackAt)));
4918
5488
  const count = Math.max(1, cracks(beta) - cracks(prior));
4919
5489
  const axis2 = [0, 0, 0];
4920
5490
  axis2[q.thin] = 1;
4921
5491
  const normal = rotateVec(quat.mul(o.st.rotation, q.q), axis2);
4922
- const length3 = Math.min(height * 0.6, SETTLEMENT_CRACK_LENGTH * Math.sqrt(beta / groundCfg.crackDistortion));
5492
+ const length3 = Math.min(height * 0.6, SETTLEMENT_CRACK_LENGTH * Math.sqrt(beta / crackAt));
5493
+ const units = q.spec.units?.size;
5494
+ const step = units ? [Math.max(units[0], units[2]), units[1]] : [BRICK_STEP[0], BRICK_STEP[1]];
5495
+ const reach = Math.hypot(toward2[0], toward2[2]) || 1;
5496
+ const along4 = vec.normalize([toward2[0] / reach, 1, toward2[2] / reach]);
5497
+ const width = clamp2(beta * SETTLEMENT_CRACK_OPENING, MIN_CRACK_WIDTH, MAX_CRACK_WIDTH);
5498
+ const through = q.dims[q.thin];
4923
5499
  for (let c = 0; c < count; c++) {
4924
- const local2 = [clamp2(at3[0], box2.min[0], box2.max[0]), box2.min[1] + height * (0.15 + 0.2 * c), clamp2(at3[2], box2.min[2], box2.max[2])];
4925
- addCrack(o, q, toWorld(o.st.position, o.st.rotation, local2), normal, length3, src);
5500
+ const back = c * SETTLEMENT_CRACK_SPACING;
5501
+ const local2 = [clamp2(at3[0] - toward2[0] / reach * back, box2.min[0], box2.max[0]), box2.min[1] + height * (0.2 + 0.12 * c), clamp2(at3[2] - toward2[2] / reach * back, box2.min[2], box2.max[2])];
5502
+ const face = toLocal(q.pos, q.q, local2);
5503
+ face[q.thin] = through / 2;
5504
+ addCrack(o, q, toWorld(o.st.position, o.st.rotation, toWorld(q.pos, q.q, face)), normal, length3, src, { along: along4, step, width, through });
4926
5505
  }
4927
- if (beta >= groundCfg.damageDistortion && prior < groundCfg.damageDistortion) {
5506
+ if (beta >= damageAt && prior < damageAt) {
4928
5507
  q.st.hp = Math.max(1, q.st.hp - q.st.maxHp * groundCfg.distortionDamage);
4929
5508
  markDamaged(q);
4930
5509
  }
@@ -4938,18 +5517,53 @@ var GameTestRoom = (() => {
4938
5517
  const q = o.parts[qi];
4939
5518
  if (isLost(q) || isTerrain(q) || o.collapsed.has(q.spec.id) || !isSlender(q.spec, q.mat)) continue;
4940
5519
  if (restsOn(o, j, qi) !== b.i) {
4941
- distort(o, q, (b.p.st.subsidence ?? 0) / DISTORTION_BASE, j.pos, src);
5520
+ const side = [j.pos[0] - q.pos[0], 0, j.pos[2] - q.pos[2]];
5521
+ const own = q.st.settlement ? settlementAt(q.st.settlement, j.pos) : 0;
5522
+ const reach = Math.hypot(side[0], side[2]) || 1;
5523
+ const from = [j.pos[0] - side[0] / reach * BESIDE_CRACK_OFFSET, j.pos[1], j.pos[2] - side[2] / reach * BESIDE_CRACK_OFFSET];
5524
+ distort(o, q, Math.abs((b.p.st.subsidence ?? 0) - own) / DISTORTION_BASE, from, side, src);
4942
5525
  continue;
4943
5526
  }
4944
5527
  const over = aabbOf(q.spec.shape, q.pos, q.q);
4945
5528
  const k0 = clamp2(Math.floor((over.min[fp.long] - box2.min[fp.long]) / fp.step), 0, drops.length - 1);
4946
5529
  const k1 = clamp2(Math.ceil((over.max[fp.long] - box2.min[fp.long]) / fp.step), k0 + 1, drops.length);
4947
- const { value, at: at3 } = distortionOf(drops.slice(k0, k1), fp.step);
5530
+ const { value, at: at3, down } = distortionOf(relativeToChord(drops.slice(k0, k1)), fp.step);
4948
5531
  const point = [(over.min[0] + over.max[0]) / 2, over.min[1], (over.min[2] + over.max[2]) / 2];
4949
5532
  point[fp.long] = box2.min[fp.long] + (k0 + at3 + 0.5) * fp.step;
4950
- distort(o, q, value, point, src);
5533
+ const toward2 = [0, 0, 0];
5534
+ toward2[fp.long] = down;
5535
+ distort(o, q, value, point, toward2, src);
4951
5536
  }
4952
5537
  };
5538
+ const settle = (o, b, fp) => {
5539
+ const box2 = aabbOf(b.p.spec.shape, b.p.pos, b.p.q);
5540
+ const from = [(box2.min[0] + box2.max[0]) / 2, box2.min[1], (box2.min[2] + box2.max[2]) / 2];
5541
+ const to = [from[0], from[1], from[2]];
5542
+ from[fp.long] = box2.min[fp.long];
5543
+ to[fp.long] = box2.max[fp.long];
5544
+ const line = { from, to, drop: settlementProfile(fp.slices.map((slice) => slice.drop), fp.step) };
5545
+ const follows = { from, to, drop: line.drop.map((v) => Math.min(v, groundCfg.maxSettlement)) };
5546
+ const moved = /* @__PURE__ */ new Set([b.i]);
5547
+ let ring2 = [b.i];
5548
+ for (let depth = 0; depth <= SETTLEMENT_FOLLOW_DEPTH && ring2.length; depth++) {
5549
+ const next = [];
5550
+ for (const i of ring2) {
5551
+ const p = o.parts[i];
5552
+ p.st.settlement = mergeSettlement(p.st.settlement, i === b.i ? line : follows);
5553
+ if (i !== b.i) p.st.subsidence = Math.max(p.st.subsidence ?? 0, settlementAt(p.st.settlement, p.pos));
5554
+ for (const j of o.byPart.get(p.spec.id) ?? []) {
5555
+ const k = j.ia === i ? j.ib : j.ia;
5556
+ const q = o.parts[k];
5557
+ if (j.st.open || moved.has(k) || isLost(q) || isTerrain(q) || q.anchored) continue;
5558
+ if (q.structural && (i !== b.i || restsOn(o, j, k) !== i || !restsOnlyOn(o, k, i))) continue;
5559
+ moved.add(k);
5560
+ next.push(k);
5561
+ }
5562
+ }
5563
+ ring2 = next;
5564
+ }
5565
+ };
5566
+ const restsOnlyOn = (o, k, i) => (o.byPart.get(o.parts[k].spec.id) ?? []).every((j) => j.st.open || restsOn(o, j, k) < 0 || restsOn(o, j, k) === i);
4953
5567
  const loseGround = (o, b, opened) => {
4954
5568
  if (b.p.anchored) {
4955
5569
  b.p.anchored = false;
@@ -4963,38 +5577,48 @@ var GameTestRoom = (() => {
4963
5577
  opened.push(j);
4964
5578
  }
4965
5579
  };
4966
- const bearLoss = (o, b, fp, src) => {
4967
- const voids = longestRun(fp.slices.map((slice) => slice.void));
4968
- const bridge = voids ? bridgeOf(b, fp, voids) : null;
4969
- if (voids && bridge && bridge.span >= bridge.depth) {
4970
- if (!bridge.holds) return "unsupported";
4971
- if (bridge.cracks) crackBridge(o, b, fp, voids, bridge, src);
4972
- for (let k = voids.start; k < voids.end; k++) fp.slices[k].drop = Math.min(fp.slices[k].drop, bridge.sag);
4973
- fp.drop = fp.slices.reduce((sum2, slice) => sum2 + slice.drop, 0) / fp.slices.length;
4974
- return "bridged";
4975
- }
4976
- return fp.share <= 1 - 1 / soilCfg.safety ? "settled" : "unsupported";
4977
- };
4978
5580
  const undermine = (o, loss) => {
4979
5581
  if (!o.hasStructure || !o.anyAnchor || o.st.soil?.status === "toppled") return;
4980
5582
  const src = loss.source ?? "ground";
4981
5583
  const opened = [];
4982
5584
  const settled = [];
4983
5585
  let lost = false;
4984
- for (const b of bearersOf(o)) {
4985
- if (!nearLoss(o, b.p, loss)) continue;
4986
- const fp = footprintOf(o, b, loss);
4987
- if (fp.share <= 0 && fp.drop < SUBSIDE_MIN) continue;
4988
- const status = bearLoss(o, b, fp, src);
5586
+ const touched = bearersOf(o).filter((b) => nearLoss(o, b.p, loss)).map((b) => ({ b, fp: footprintOf(o, b, loss) })).filter(({ fp }) => fp.share > 0 || fp.drop >= SUBSIDE_MIN);
5587
+ const bearers = bearersOf(o);
5588
+ const pillarFirst = (b) => b.p.spec.structure?.role === "column" ? 0 : 1;
5589
+ const order = [...touched].sort((x, y) => pillarFirst(x.b) - pillarFirst(y.b));
5590
+ const standing = new Set(bearers.map((b) => b.i));
5591
+ for (const { b, fp } of order) {
5592
+ const bearing = bearLoss(o, b, fp, standing);
5593
+ if (bearing.status === "unsupported") standing.delete(b.i);
5594
+ if (bearing.status !== "unsupported" && !bendGeometry(b.p.spec).member) {
5595
+ const kept = fp.slices.filter((slice) => !slice.void);
5596
+ const flat = kept.length ? kept.reduce((sum2, slice) => sum2 + slice.drop, 0) / kept.length : 0;
5597
+ fp.slices.forEach((slice) => slice.drop = flat);
5598
+ fp.drop = flat;
5599
+ }
5600
+ if (bearing.status !== "unsupported" && bendGeometry(b.p.spec).member) {
5601
+ const smooth2 = smoothProfile(fp.slices.map((slice) => slice.drop), fp.step, BEAM_SPREAD);
5602
+ fp.slices.forEach((slice, k) => slice.drop = bearing.failed.some((r) => (k + 0.5) * fp.step > r.start && (k + 0.5) * fp.step < r.end) ? slice.drop : smooth2[k]);
5603
+ fp.drop = fp.slices.reduce((sum2, slice) => sum2 + slice.drop, 0) / Math.max(1, fp.slices.length);
5604
+ }
4989
5605
  const before = b.p.st.subsidence ?? 0;
4990
5606
  const drop = Math.max(before, fp.drop);
4991
- if (status === "unsupported") {
5607
+ const peak = () => Math.max(0, ...b.p.st.settlement?.drop ?? []);
5608
+ const peakBefore = peak();
5609
+ if (bearing.status === "unsupported") {
4992
5610
  loseGround(o, b, opened);
4993
5611
  lost = true;
4994
- } else settled.push({ b, fp });
4995
- if (status === "settled" && drop - before < SUBSIDE_MIN) continue;
5612
+ } else {
5613
+ crackBearer(o, b, fp, bearing, src);
5614
+ if (bearing.failed.length && openOver(o, b, fp, bearing.failed, opened, src)) lost = true;
5615
+ if (bearing.hole) breakOut(o, b.p, bearing.hole, src);
5616
+ settled.push({ b, fp });
5617
+ settle(o, b, fp);
5618
+ }
5619
+ if (bearing.status === "settled" && drop - before < SUBSIDE_MIN && peak() - peakBefore < SUBSIDE_MIN) continue;
4996
5620
  b.p.st.subsidence = drop;
4997
- emit2({ ...base(o, src), type: "subside", part: b.p.spec.id, drop, share: fp.share, point: fp.deepest, status });
5621
+ emit2({ ...base(o, src), type: "subside", part: b.p.spec.id, drop, share: fp.share, point: fp.deepest, status: bearing.status });
4998
5622
  }
4999
5623
  for (const { b, fp } of settled) crackOver(o, b, fp, src);
5000
5624
  o.active = true;
@@ -5039,6 +5663,7 @@ var GameTestRoom = (() => {
5039
5663
  return r ? { t0: r.t0, t1: r.t1, n: rotateVec(q, r.n0) } : null;
5040
5664
  };
5041
5665
  const rayPart = (o, p, origin, d) => rayPartRaw(o.st.position, o.st.rotation, p.spec, p.q, origin, d, p.pos);
5666
+ const PART_BOUND_SLACK = 1e-6;
5042
5667
  const raySphereMiss = (origin, d, c, r, maxDist) => {
5043
5668
  const oc = vec.sub(origin, c);
5044
5669
  const b = vec.dot(oc, d);
@@ -5048,13 +5673,27 @@ var GameTestRoom = (() => {
5048
5673
  if (disc < 0) return true;
5049
5674
  return -b - Math.sqrt(disc) > maxDist;
5050
5675
  };
5676
+ const partSphereMiss = (o, d, p, maxDist) => {
5677
+ const x = o[0] - p.pos[0], y = o[1] - p.pos[1], z = o[2] - p.pos[2];
5678
+ const r = p.bound + PART_BOUND_SLACK;
5679
+ const b = x * d[0] + y * d[1] + z * d[2];
5680
+ const cc = x * x + y * y + z * z - r * r;
5681
+ if (cc > 0 && b > 0) return true;
5682
+ const disc = b * b - cc;
5683
+ if (disc < 0) return true;
5684
+ return -b - Math.sqrt(disc) > maxDist;
5685
+ };
5051
5686
  const raycastInternal = (origin, dir, maxDist, ignore, worldOnly = false, precise = false) => {
5052
5687
  const d = vec.normalize(dir);
5053
5688
  let best = null;
5054
5689
  for (const o of objects.values()) {
5055
5690
  if (raySphereMiss(origin, d, toWorld(o.st.position, o.st.rotation, o.bc), o.br, best ? Math.min(maxDist, best.distance) : maxDist)) continue;
5691
+ const qi = quat.conjugate(o.st.rotation);
5692
+ const lo = rotateVec(qi, vec.sub(origin, o.st.position));
5693
+ const ld = rotateVec(qi, d);
5056
5694
  for (const p of o.parts) {
5057
5695
  if (isLost(p) || ignore?.has(`${o.id}\0${p.spec.id}`)) continue;
5696
+ if (partSphereMiss(lo, ld, p, maxDist)) continue;
5058
5697
  const r = rayPart(o, p, origin, d);
5059
5698
  if (!r || r.t0 < 0 || r.t0 > maxDist) continue;
5060
5699
  if (p.st.opening && insideOpening(o, p, vec.madd(origin, d, r.t0))) continue;
@@ -5182,8 +5821,8 @@ var GameTestRoom = (() => {
5182
5821
  setStage(o, p, 1, {}, src);
5183
5822
  return hole;
5184
5823
  };
5185
- const addCrack = (o, p, wp, wn, length3, src) => {
5186
- const crack2 = { point: objLocal(o, wp), normal: objLocalDir(o, wn), length: Math.min(length3, Math.max(p.bound, 0.05)), rays: 5 + Math.floor(rnd2() * 5) };
5824
+ const addCrack = (o, p, wp, wn, length3, src, stair) => {
5825
+ const crack2 = { point: objLocal(o, wp), normal: objLocalDir(o, wn), length: Math.min(length3, Math.max(p.bound, 0.05)), rays: 5 + Math.floor(rnd2() * 5), ...stair ? { stair } : {} };
5187
5826
  p.st.cracks.push(crack2);
5188
5827
  if (p.st.cracks.length > 16) p.st.cracks.shift();
5189
5828
  markDamaged(p);
@@ -5310,6 +5949,12 @@ var GameTestRoom = (() => {
5310
5949
  const breachAreaOf = (p, damage, blast3) => damage / (p.mat.hpPerKg * p.mat.density) / Math.max(p.thickness, 5e-3) * (blast3 ? breachBlastFactor : 1);
5311
5950
  const localHolesOf = (p) => p.st.holes.map((h) => ({ point: toLocal(p.pos, p.q, h.point), normal: rotateVec(quat.conjugate(p.q), h.normal), diameter: h.diameter }));
5312
5951
  const unitsOf = (p) => p.spec.units ?? { size: [p.mat.fragmentSize?.[1] ?? 0.2, p.mat.fragmentSize?.[1] ?? 0.2, p.thickness], bond: "stack" };
5952
+ const unitSpan = (p) => {
5953
+ const [u, v] = planeAxes(p.thin);
5954
+ const first = gridOf(p)[0];
5955
+ const size = unitsOf(p).size;
5956
+ return first && first.size[u] > size[0] + 1e-6 ? [first.size[u], first.size[v]] : [Math.min(size[0], p.dims[u]), Math.min(size[1], p.dims[v])];
5957
+ };
5313
5958
  const FRAGMENT_SHAPE = { shatter: "shard", splinter: "splinter", tear: "scrap" };
5314
5959
  const cellFragments = (o, p, cells, hitW, dir, speed, mass, tag, inner = false) => {
5315
5960
  const { c, q } = partXf(o, p);
@@ -5340,6 +5985,44 @@ var GameTestRoom = (() => {
5340
5985
  };
5341
5986
  });
5342
5987
  };
5988
+ const plasterFlakes = (o, p, cells, hitW, speed, tag) => {
5989
+ const layer3 = p.layers.find((l) => l.mat.fracture === "crumble");
5990
+ if (!layer3 || cells.length === 0) return [];
5991
+ const { c, q } = partXf(o, p);
5992
+ const [u, v] = planeAxes(p.thin);
5993
+ const faces = layer3.def.faces === "both" ? [1, -1] : [layer3.def.faces === "+" ? 1 : -1];
5994
+ const area = cells.reduce((sum2, cell) => sum2 + cell.size[u] * cell.size[v], 0) * faces.length;
5995
+ const count = Math.min(MAX_PLASTER_FLAKES, Math.ceil(area / PLASTER_FLAKE_AREA));
5996
+ const out = [];
5997
+ for (let i = 0; i < count; i++) {
5998
+ const cell = cells[Math.floor(rnd2() * cells.length)];
5999
+ const face = faces[i % faces.length];
6000
+ const local2 = [cell.center[0], cell.center[1], cell.center[2]];
6001
+ local2[u] += (rnd2() - 0.5) * cell.size[u];
6002
+ local2[v] += (rnd2() - 0.5) * cell.size[v];
6003
+ local2[p.thin] = cell.center[p.thin] + face * (cell.size[p.thin] / 2 - layer3.def.thickness / 2);
6004
+ const size = [0, 0, 0];
6005
+ size[u] = 0.04 + 0.12 * rnd2();
6006
+ size[v] = 0.03 + 0.09 * rnd2();
6007
+ size[p.thin] = layer3.def.thickness;
6008
+ const cw = toWorld(c, q, local2);
6009
+ const out0 = vec.normalize(vec.sub(cw, hitW), [0, 1, 0]);
6010
+ const k = speed * (0.4 + 0.9 * rnd2());
6011
+ out.push({
6012
+ id: `${p.spec.id}#${tag}${i}`,
6013
+ part: p.spec.id,
6014
+ material: layer3.def.material,
6015
+ shape: "scrap",
6016
+ center: cw,
6017
+ size,
6018
+ rotation: q,
6019
+ velocity: [out0[0] * k, out0[1] * k + 0.5 * rnd2(), out0[2] * k],
6020
+ angularVelocity: [(rnd2() - 0.5) * 12, (rnd2() - 0.5) * 12, (rnd2() - 0.5) * 12],
6021
+ mass: layer3.mat.density * size[0] * size[1] * size[2]
6022
+ });
6023
+ }
6024
+ return out;
6025
+ };
5343
6026
  const worldBars = (o, p, bars) => {
5344
6027
  const { c, q } = partXf(o, p);
5345
6028
  return bars.map(([a, b]) => [toWorld(c, q, a), toWorld(c, q, b)]);
@@ -5577,6 +6260,11 @@ var GameTestRoom = (() => {
5577
6260
  }
5578
6261
  const bow = clamp2(((p.st.stress ?? 0) - 1) / 0.5, 0, 1);
5579
6262
  if (bow > 0) bars = bars.flatMap((bar2, i) => bendBar(bar2, p.thin, bow * BAR_BOW, i));
6263
+ const stirrups = memberStirrups(p.dims);
6264
+ if (stirrups.length > 0 && bars.length > 0) {
6265
+ if (p.st.stripped) bars = [...bars, ...stirrups];
6266
+ else for (const box2 of [...p.st.chipped ?? [], ...p.rt.bared ?? []]) bars.push(...clipBarsToVolume(stirrups, box2.center, box2.size));
6267
+ }
5580
6268
  emit2({ ...base(o, src), type: "expose", part: p.spec.id, bars: worldBars(o, p, bars) });
5581
6269
  };
5582
6270
  const openBreach = (o, p, center, total, energy, dir, src) => {
@@ -5647,6 +6335,33 @@ var GameTestRoom = (() => {
5647
6335
  if (out.length === 0) return false;
5648
6336
  return knockOutUnits(o, p, wp, energy, out, src);
5649
6337
  };
6338
+ const wedgedUnder = (o, p, loose) => {
6339
+ if (loose.length === 0) return loose;
6340
+ const beam = (o.byPart.get(p.spec.id) ?? []).some((j) => j.bears === "a_on_b" && j.spec.b === p.spec.id && !j.st.open && !!o.byId.get(j.spec.a)?.spec.reinforcement);
6341
+ if (!beam) return loose;
6342
+ const [, v] = planeAxes(p.thin);
6343
+ const grid = gridOf(p);
6344
+ const top = p.dims[v] / 2, course = unitSpan(p)[1];
6345
+ return loose.filter((i) => grid[i].center[v] + grid[i].size[v] / 2 < top - PANEL_EDGE_BEAM * course + 1e-4);
6346
+ };
6347
+ const panelEdge = (p, faces, seed) => {
6348
+ const [u, v] = planeAxes(p.thin);
6349
+ const [unitU, unitV] = unitSpan(p);
6350
+ const bottom = -p.dims[v] / 2, top = p.dims[v] / 2;
6351
+ const depth = (key2, most) => {
6352
+ const r = createRng(seed ^ Math.imul(key2 + 1, 2654435761)).next();
6353
+ return Math.floor(r * r * (most + 1));
6354
+ };
6355
+ return (cell) => {
6356
+ const course = Math.round((cell.center[v] - bottom) / unitV - 0.5);
6357
+ const column = Math.round((cell.center[u] - faces.fromFace) / unitU - 0.5);
6358
+ const keyed = course % 2 === 0 ? 0.5 : 0;
6359
+ const side = Math.min(cell.center[u] - faces.fromFace, faces.toFace - cell.center[u]) / unitU;
6360
+ if (side < depth(course * 2, PANEL_EDGE_PILLAR) + keyed + 0.5) return true;
6361
+ if ((cell.center[v] - bottom) / unitV < depth(1e5 + column, PANEL_EDGE_BASE) + 1) return true;
6362
+ return (top - cell.center[v]) / unitV < Math.floor(createRng(seed ^ Math.imul(200001 + column, 2654435761)).next() * (PANEL_EDGE_BEAM + 1));
6363
+ };
6364
+ };
5650
6365
  const blastPanel = (o, p, point, energy, src) => {
5651
6366
  var _a;
5652
6367
  if (!p.staged || p.st.stripped || isLost(p) || !p.spec.units || p.spec.units.mortar === p.spec.material || p.spec.reinforcement) return false;
@@ -5658,12 +6373,18 @@ var GameTestRoom = (() => {
5658
6373
  if (m === p || !m.spec.reinforcement || isLost(m) || m.spec.shape.kind !== "box") continue;
5659
6374
  const mp = toLocal(p.pos, p.q, m.pos);
5660
6375
  if (Math.abs(mp[p.thin]) > p.dims[p.thin] + 0.15 || Math.abs(mp[u]) > half + 0.15 || m.dims[v] < p.dims[v] * 0.5) continue;
5661
- posts.push(mp[u]);
6376
+ posts.push({ x: mp[u], half: m.dims[u] / 2 });
5662
6377
  }
5663
- let from = -half, to = half;
5664
- for (const x of posts) {
5665
- if (x <= lp[u] && x > from) from = x;
5666
- if (x >= lp[u] && x < to) to = x;
6378
+ let from = -half, to = half, fromFace = -half, toFace = half;
6379
+ for (const { x, half: w } of posts) {
6380
+ if (x <= lp[u] && x > from) {
6381
+ from = x;
6382
+ fromFace = x + w;
6383
+ }
6384
+ if (x >= lp[u] && x < to) {
6385
+ to = x;
6386
+ toFace = x - w;
6387
+ }
5667
6388
  }
5668
6389
  if (to - from < 0.2) return false;
5669
6390
  const tnt = energy * DEFAULT_BLAST_IMPULSE.tntPerJoule;
@@ -5685,10 +6406,11 @@ var GameTestRoom = (() => {
5685
6406
  const grid = gridOf(p);
5686
6407
  const worn = (_a = p.rt).chips ?? (_a.chips = {});
5687
6408
  const out = [];
6409
+ const edge = panelEdge(p, { fromFace, toFace }, seedOf(...Array.from(`${o.id}|${p.spec.id}`, (char) => char.charCodeAt(0) / 1e3), from));
5688
6410
  for (let i = 0; i < grid.length; i++) {
5689
6411
  const cell = grid[i];
5690
6412
  if (worn[i] === -1) continue;
5691
- if (cell.center[u] - cell.size[u] / 2 >= from - 1e-6 && cell.center[u] + cell.size[u] / 2 <= to + 1e-6) out.push(i);
6413
+ if (cell.center[u] - cell.size[u] / 2 >= from - 1e-6 && cell.center[u] + cell.size[u] / 2 <= to + 1e-6 && !edge(cell)) out.push(i);
5692
6414
  }
5693
6415
  if (out.length === 0) return false;
5694
6416
  for (const i of out) worn[i] = -1;
@@ -5701,7 +6423,8 @@ var GameTestRoom = (() => {
5701
6423
  const grid = gridOf(p);
5702
6424
  const worn = (_a = p.rt).chips ?? (_a.chips = {});
5703
6425
  for (const i of out) worn[i] = -1;
5704
- const loose = unsupportedCells(grid, p.thin, p.dims, (i) => worn[i] !== -1 && !(op && p.st.opening && Math.hypot(grid[i].center[u] - op[u], grid[i].center[v] - op[v]) <= p.st.opening.radius));
6426
+ const cast = p.spec.units?.mortar === p.spec.material;
6427
+ const loose = cast ? [] : wedgedUnder(o, p, unsupportedCells(grid, p.thin, p.dims, (i) => worn[i] !== -1 && !(op && p.st.opening && Math.hypot(grid[i].center[u] - op[u], grid[i].center[v] - op[v]) <= p.st.opening.radius)));
5705
6428
  for (const i of loose) worn[i] = -1;
5706
6429
  const blown = out.map((i) => grid[i]), fallen = loose.map((i) => grid[i]);
5707
6430
  const volume = (cells2) => cells2.reduce((sum2, cell) => sum2 + cell.size[0] * cell.size[1] * cell.size[2], 0);
@@ -5712,8 +6435,9 @@ var GameTestRoom = (() => {
5712
6435
  const chipped = (_b = p.st).chipped ?? (_b.chipped = []);
5713
6436
  const tag = `x${chipped.length}_`;
5714
6437
  const fragments = [
5715
- ...cellFragments(o, p, clumpCells(blown, p.thin, maxFragments), wp, null, speed, blownMass, tag, true),
5716
- ...fallen.length > 0 ? cellFragments(o, p, clumpCells(fallen, p.thin, Math.max(2, Math.round(maxFragments / 4))), wp, null, 0.3, fallenMass, `${tag}f`, true) : []
6438
+ ...cellFragments(o, p, clumpUnits(blown, p.thin, unitSpan(p), unitClumps), wp, null, speed, blownMass, tag, true),
6439
+ ...fallen.length > 0 ? cellFragments(o, p, clumpUnits(fallen, p.thin, unitSpan(p), fallenClumps), wp, null, 0.3, fallenMass, `${tag}f`, true) : [],
6440
+ ...plasterFlakes(o, p, blown, wp, speed, `${tag}p`)
5717
6441
  ];
5718
6442
  const cells = [...blown, ...fallen];
5719
6443
  const holes = localHolesOf(p);
@@ -5749,7 +6473,7 @@ var GameTestRoom = (() => {
5749
6473
  const worn = (_a = p.rt).chips ?? (_a.chips = {});
5750
6474
  const op = p.st.opening ? toLocal(p.pos, p.q, p.st.opening.center) : null;
5751
6475
  const opened = (cell) => !!op && !!p.st.opening && Math.hypot(cell.center[u] - op[u], cell.center[v] - op[v]) <= p.st.opening.radius;
5752
- const loose = unsupportedCells(grid, p.thin, p.dims, (i) => worn[i] !== -1 && !opened(grid[i]));
6476
+ const loose = wedgedUnder(o, p, unsupportedCells(grid, p.thin, p.dims, (i) => worn[i] !== -1 && !opened(grid[i])));
5753
6477
  if (loose.length === 0) return 0;
5754
6478
  for (const i of loose) worn[i] = -1;
5755
6479
  const fallen = loose.map((i) => grid[i]);
@@ -5764,7 +6488,7 @@ var GameTestRoom = (() => {
5764
6488
  }
5765
6489
  }
5766
6490
  const center = toWorld(c, q, [(lo[0] + hi[0]) / 2, (lo[1] + hi[1]) / 2, (lo[2] + hi[2]) / 2]);
5767
- const fragments = cellFragments(o, p, clumpCells(fallen, p.thin, Math.max(2, Math.round(maxFragments / 4))), vec.add(center, [0, 1, 0]), null, 0.3, mass, `u${chipped.length}_`, true);
6491
+ const fragments = cellFragments(o, p, clumpUnits(fallen, p.thin, unitSpan(p), fallenClumps), vec.add(center, [0, 1, 0]), null, 0.3, mass, `u${chipped.length}_`, true);
5768
6492
  const holes = localHolesOf(p);
5769
6493
  p.st.holes = p.st.holes.filter((_, k) => !fallen.some((cell) => marksInBox(cell.center, cell.size, void 0, [holes[k]]).length > 0));
5770
6494
  for (const cell of fallen) chipped.push({ center: [cell.center[0], cell.center[1], cell.center[2]], size: [cell.size[0], cell.size[1], cell.size[2]] });
@@ -6184,14 +6908,15 @@ var GameTestRoom = (() => {
6184
6908
  }
6185
6909
  if (Ein > 20 && p.layers.length > 0) spallOuter(o, p, wp, faceOf(o, p, wn), wn, clamp2(0.05 + Math.sqrt(Ein) * 2e-3, 0.05, 0.4), src);
6186
6910
  const breachReach = BREACH_BLAST_REACH * Math.cbrt(Math.max(0, energy) / 1e3);
6187
- const farMember = Boolean(p.spec.reinforcement && p.spec.units) && dist > MEMBER_CUT_REACH * breachReach;
6188
- const dealt = farMember ? applyDamage(o, p, Math.min(amount, Math.max(0, p.st.hp - p.st.maxHp * FAR_MEMBER_HP_FLOOR)), "explosive", null, null, Ein * 0.3, 40, src) : applyDamage(o, p, amount, "explosive", point, null, Ein * 0.3, 40, src);
6911
+ const caged = Boolean(p.spec.reinforcement && p.spec.units);
6912
+ const member = caged && (isMember(p.dims) || dist > MEMBER_CUT_REACH * breachReach);
6913
+ const dealt = member ? applyDamage(o, p, Math.min(amount, Math.max(0, p.st.hp - p.st.maxHp * FAR_MEMBER_HP_FLOOR)), "explosive", null, null, Ein * 0.3, 40, src) : applyDamage(o, p, amount, "explosive", point, null, Ein * 0.3, 40, src);
6189
6914
  res.damage += dealt;
6190
6915
  if (p.bend && flexure.enabled) bendByBlast(o, p, point, energy, radius, reach, src);
6191
6916
  if (p.spec.units && p.spec.units.mortar !== p.spec.material) {
6192
6917
  blastPanel(o, p, point, energy, src);
6193
6918
  if (!isLost(p)) blastUnits(o, p, wp, energy, dist, src);
6194
- } else if (farMember) blastUnits(o, p, wp, energy, dist, src);
6919
+ } else if (member) blastUnits(o, p, wp, energy, dist, src);
6195
6920
  else if (dist <= breachReach) addBreach(o, p, wp, breachAreaOf(p, dealt, true), Ein, null, src);
6196
6921
  return reach;
6197
6922
  };
@@ -18397,7 +19122,7 @@ var GameTestRoom = (() => {
18397
19122
  var tangentA = { x: 0, y: 0, z: 0 };
18398
19123
  var tangentB = { x: 0, y: 0, z: 0 };
18399
19124
  var axis = { x: 0, y: 0, z: 0 };
18400
- function cross(out, a, b) {
19125
+ function cross2(out, a, b) {
18401
19126
  const x = a.y * b.z - a.z * b.y;
18402
19127
  const y = a.z * b.x - a.x * b.z;
18403
19128
  const z = a.x * b.y - a.y * b.x;
@@ -18422,8 +19147,8 @@ var GameTestRoom = (() => {
18422
19147
  tangentA.x = 0;
18423
19148
  tangentA.y = 1;
18424
19149
  }
18425
- normalize(cross(tangentB, axis, tangentA));
18426
- normalize(cross(tangentA, tangentB, axis));
19150
+ normalize(cross2(tangentB, axis, tangentA));
19151
+ normalize(cross2(tangentA, tangentB, axis));
18427
19152
  out.x = dx * cosTilt;
18428
19153
  out.y = dy * cosTilt;
18429
19154
  out.z = dz * cosTilt;
@@ -22488,11 +23213,33 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
22488
23213
  }
22489
23214
  return faceted(THREE, positions);
22490
23215
  }
23216
+ function buildRubbleGeometry(THREE, seed) {
23217
+ const rnd2 = createRng3(9e3 + seed);
23218
+ const base = new THREE.BoxGeometry(1, 1, 1, 2, 2, 2).toNonIndexed();
23219
+ const source = base.getAttribute("position");
23220
+ const moved = /* @__PURE__ */ new Map();
23221
+ const positions = [];
23222
+ for (let i = 0; i < source.count; i++) {
23223
+ const p = [source.getX(i), source.getY(i), source.getZ(i)];
23224
+ const key2 = p.map((x) => x.toFixed(3)).join(",");
23225
+ let out = moved.get(key2);
23226
+ if (!out) {
23227
+ const outer = p.filter((x) => Math.abs(x) > 0.49).length;
23228
+ const bite = outer === 3 ? 0.08 + 0.3 * rnd2() : outer === 2 ? 0.1 * rnd2() : 0;
23229
+ out = p.map((x) => Math.abs(x) > 0.49 ? x - Math.sign(x) * bite * (0.4 + 0.6 * rnd2()) : x);
23230
+ moved.set(key2, out);
23231
+ }
23232
+ positions.push(out[0], out[1], out[2]);
23233
+ }
23234
+ base.dispose();
23235
+ return faceted(THREE, positions);
23236
+ }
22491
23237
  var BUILDERS2 = {
22492
23238
  sliver: buildSliverGeometry,
22493
23239
  lump: buildLumpGeometry,
22494
23240
  veneer: buildVeneerGeometry,
22495
- board: buildBrokenBoardGeometry
23241
+ board: buildBrokenBoardGeometry,
23242
+ rubble: buildRubbleGeometry
22496
23243
  };
22497
23244
  var SHEET_MAX = 4e-3;
22498
23245
  var SHEET_AIR = 2.5;
@@ -22599,6 +23346,7 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
22599
23346
  if (f.shape === "chunk" && form === "granules") return this.sharedGeometry("lump", seed);
22600
23347
  if (f.shape === "scrap" && form === "plies") return this.sharedGeometry("veneer", seed);
22601
23348
  if (f.shape === "slab" && (form === "slivers" || form === "plies")) return this.sharedGeometry("board", seed);
23349
+ if (f.shape === "chunk" && this.o.crumbles?.(f.material)) return this.sharedGeometry("rubble", seed);
22602
23350
  return this.box;
22603
23351
  }
22604
23352
  markParts() {
@@ -22607,18 +23355,18 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
22607
23355
  return { geometry: this.markGeometry, material: this.markMaterial };
22608
23356
  }
22609
23357
  /**
22610
- * 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
23358
+ * 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
22611
23359
  * glass materials. A host draws them at load so the first blast does not upload these buffers in the middle of a frame.
22612
23360
  */
22613
23361
  warmMeshes() {
22614
23362
  const opaque = this.materialFor("warm", 8421504, false);
22615
23363
  const glass2 = this.materialFor("warm", 8421504, true);
22616
23364
  const geometries = [this.box, ...Array.from({ length: SHARD_SHAPES }, (_, index) => this.shardGeometry(index))];
22617
- const wood = ["sliver", "lump", "veneer", "board"].flatMap((shape) => Array.from({ length: FRAGMENT_SHAPE_VARIANTS }, (_, index) => this.sharedGeometry(shape, index)));
23365
+ const shaped = ["sliver", "lump", "veneer", "board", "rubble"].flatMap((shape) => Array.from({ length: FRAGMENT_SHAPE_VARIANTS }, (_, index) => this.sharedGeometry(shape, index)));
22618
23366
  const { geometry, material } = this.markParts();
22619
23367
  return [
22620
23368
  ...geometries.flatMap((shared2) => [new this.THREE.Mesh(shared2, opaque), new this.THREE.Mesh(shared2, glass2)]),
22621
- ...wood.map((shared2) => new this.THREE.Mesh(shared2, opaque)),
23369
+ ...shaped.map((shared2) => new this.THREE.Mesh(shared2, opaque)),
22622
23370
  new this.THREE.Mesh(geometry, material)
22623
23371
  ];
22624
23372
  }
@@ -23368,16 +24116,29 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
23368
24116
  }
23369
24117
  }
23370
24118
  update(dt) {
23371
- const steps = this.collisionSteps(dt);
23372
- for (let i = 0; i < steps; i++) this.step(dt / steps);
24119
+ const { steps, near } = this.collisionSteps(dt);
24120
+ if (steps === 1) {
24121
+ this.time += dt;
24122
+ this.step(dt);
24123
+ return;
24124
+ }
24125
+ const start = this.time;
24126
+ this.time = start + dt;
24127
+ const far = (b) => !near.has(b);
24128
+ this.step(dt, { moves: far, meets: (a, b) => far(a) && far(b) });
24129
+ for (let i = 0; i < steps; i++) {
24130
+ this.time = start + dt * (i + 1) / steps;
24131
+ this.step(dt / steps, { moves: (b) => near.has(b), meets: (a, b) => near.has(a) || near.has(b) });
24132
+ }
23373
24133
  }
23374
24134
  /**
23375
24135
  * Steps of an update: one, or more while a round piece and a piece it could meet within the update close in fast enough to pass through each other between two (closing at
23376
24136
  * their two speeds they cover at most the round one's radius per step: two cans at 3.6 m/s close 12 cm in a frame, a can is 7 cm across; a plank thrown at a can still counts).
23377
- * A round piece with nothing in reach takes one step, so its sweep against the world (the costly part) runs once.
24137
+ * A round piece with nothing in reach takes one step, so its sweep against the world (the costly part) runs once; `near` holds the pieces of the pairs that need more.
23378
24138
  */
23379
24139
  collisionSteps(dt) {
23380
24140
  let need = 1;
24141
+ const near = /* @__PURE__ */ new Set();
23381
24142
  const p = this.p;
23382
24143
  const speedOf = (b) => b.held ? 0 : Math.hypot(b.velocity[0], b.velocity[1], b.velocity[2]);
23383
24144
  for (const b of this.list) {
@@ -23394,13 +24155,17 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
23394
24155
  const reach = closing * dt + rb + Math.hypot(o.halfExtents[0], o.halfExtents[1], o.halfExtents[2]);
23395
24156
  const oc = this.centerOf(o, p);
23396
24157
  if (Math.abs(oc.x - cx) > reach || Math.abs(oc.y - cy) > reach || Math.abs(oc.z - cz) > reach) continue;
23397
- need = Math.max(need, Math.ceil(closing * dt / radius));
24158
+ const pair = Math.ceil(closing * dt / radius);
24159
+ if (pair < 2) continue;
24160
+ need = Math.max(need, pair);
24161
+ near.add(b);
24162
+ near.add(o);
23398
24163
  }
23399
24164
  }
23400
- return Math.max(1, Math.min(MAX_COLLISION_STEPS, need));
24165
+ return { steps: Math.max(1, Math.min(MAX_COLLISION_STEPS, need)), near };
23401
24166
  }
23402
- step(dt) {
23403
- this.time += dt;
24167
+ /** A step of `dt`: the pieces `only.moves` takes move (all when absent) and the pairs `only.meets` takes meet, so each pair meets in one of the steps of an update. */
24168
+ step(dt, only) {
23404
24169
  this.solids = this.list.map((body) => {
23405
24170
  const c = this.centerOf(body, new this.THREE.Vector3());
23406
24171
  const h = body.halfExtents;
@@ -23461,14 +24226,14 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
23461
24226
  b.calmAt = null;
23462
24227
  continue;
23463
24228
  }
23464
- if (b.sleeping) continue;
24229
+ if (b.sleeping || only && !only.moves(b)) continue;
23465
24230
  this.integrate(b, dt);
23466
24231
  if (b.sleeping) continue;
23467
24232
  const v = b.velocity;
23468
24233
  const w = b.angularVelocity;
23469
24234
  if (Math.hypot(v[0], v[1], v[2]) > WAKE_ABOVE_SPEED || Math.hypot(w[0], w[1], w[2]) > WAKE_ABOVE_SPIN) this.wakeAbove(b);
23470
24235
  }
23471
- this.collide(dt);
24236
+ this.collide(dt, only?.meets);
23472
24237
  }
23473
24238
  /** A few sleeping pieces each update (`SUPPORT_CHECKS`, in turn) look under them again: one whose table broke, or whose shelf fell over, falls. */
23474
24239
  wakeUnsupported() {
@@ -23970,7 +24735,7 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
23970
24735
  * Pieces against each other. Candidates by sweep and prune along x on their bounding spheres (sorted once per step: fifty cans cost a sort and a handful of pairs, not
23971
24736
  * 1225); a pair with a round piece in it meets by its shapes (`shapeContact`) and exchanges an impulse, two boxes are only pushed apart (`separatePair`).
23972
24737
  */
23973
- collide(dt) {
24738
+ collide(dt, meets) {
23974
24739
  const n = this.list.length;
23975
24740
  if (n < 2) return;
23976
24741
  const centers = this.list.map((b) => {
@@ -23988,6 +24753,7 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
23988
24753
  const a = this.list[i];
23989
24754
  const b = this.list[j];
23990
24755
  if ((a.held || a.sleeping) && (b.held || b.sleeping)) continue;
24756
+ if (meets && !meets(a, b)) continue;
23991
24757
  const r = radii[i] + radii[j];
23992
24758
  const ca = centers[i];
23993
24759
  const cb = centers[j];
@@ -24514,6 +25280,45 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
24514
25280
  }
24515
25281
  return Math.sqrt(nearest) * nearestScale;
24516
25282
  }
25283
+ function stairCrackPath(spec) {
25284
+ const rnd2 = createRng3(spec.seed);
25285
+ const run = Math.max(0.02, spec.step[0] / 2);
25286
+ const rise = Math.max(0.02, spec.step[1]);
25287
+ const points = [[0, 0]];
25288
+ let h = 0;
25289
+ let u = 0;
25290
+ let walked = 0;
25291
+ while (walked < spec.length) {
25292
+ const along22 = run * (rnd2() < 0.2 ? 2 : 1);
25293
+ h += along22;
25294
+ points.push([h, u]);
25295
+ u += rise;
25296
+ points.push([h, u]);
25297
+ walked += along22 + rise;
25298
+ }
25299
+ return points;
25300
+ }
25301
+ function stairCrackStrip(points, width, taper) {
25302
+ const out = [];
25303
+ const n = points.length - 1;
25304
+ for (let k = 0; k < n; k++) {
25305
+ const [h0, u0] = points[k];
25306
+ const [h1, u1] = points[k + 1];
25307
+ const length3 = Math.hypot(h1 - h0, u1 - u0) || 1;
25308
+ const half0 = width * (1 - (1 - taper) * k / n) / 2;
25309
+ const half1 = width * (1 - (1 - taper) * (k + 1) / n) / 2;
25310
+ const sh = -(u1 - u0) / length3;
25311
+ const su = (h1 - h0) / length3;
25312
+ const eh = (h1 - h0) / length3 * half0;
25313
+ const eu = (u1 - u0) / length3 * half0;
25314
+ const a = [h0 - eh - sh * half0, u0 - eu - su * half0];
25315
+ const b = [h1 + eh - sh * half1, u1 + eu - su * half1];
25316
+ const c = [h1 + eh + sh * half1, u1 + eu + su * half1];
25317
+ const d = [h0 - eh + sh * half0, u0 - eu + su * half0];
25318
+ out.push(...a, ...b, ...c, ...a, ...c, ...d);
25319
+ }
25320
+ return out;
25321
+ }
24517
25322
  var POS_ATTR = "position";
24518
25323
  function positionAttr(geo) {
24519
25324
  const a = geo.getAttribute(POS_ATTR);
@@ -24617,6 +25422,8 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
24617
25422
  }
24618
25423
  };
24619
25424
  var DECAL_SNAP = 0.03;
25425
+ var STAIR_CRACK_LIFT = 2e-3;
25426
+ var STAIR_CRACK_TAPER = 0.35;
24620
25427
  var Visuals = class {
24621
25428
  constructor(THREE, lowQuality, decalCap) {
24622
25429
  __publicField3(this, "THREE", THREE);
@@ -25109,6 +25916,71 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
25109
25916
  lines.renderOrder = 2;
25110
25917
  this.addDecal(mv, { obj: lines, geometry: geo });
25111
25918
  }
25919
+ /**
25920
+ * A stair-step crack through the joints of a wall (settlement): from `worldPoint` on the face whose outward normal is `worldNormal`, climbing toward
25921
+ * `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
25922
+ * part's marks: it goes with them (cleared, restored, disposed).
25923
+ */
25924
+ addStairCrack(mv, worldPoint, worldNormal, worldToward, spec) {
25925
+ const mesh2 = mv.mesh;
25926
+ mesh2.updateWorldMatrix(true, false);
25927
+ const n = worldNormal.clone().normalize();
25928
+ const up = new this.THREE.Vector3(0, 1, 0).addScaledVector(n, -n.y).normalize();
25929
+ const side = worldToward.clone().addScaledVector(n, -worldToward.dot(n)).addScaledVector(up, -worldToward.dot(up));
25930
+ if (side.lengthSq() < 1e-8) side.crossVectors(up, n);
25931
+ side.normalize();
25932
+ const start = this.onFace(mesh2, worldPoint, n).clone().addScaledVector(n, STAIR_CRACK_LIFT);
25933
+ const flat = stairCrackStrip(stairCrackPath({ length: spec.length, step: spec.step, seed: spec.seed }), spec.width, STAIR_CRACK_TAPER);
25934
+ const flip = new this.THREE.Vector3().crossVectors(side, up).dot(n) < 0;
25935
+ const verts = [];
25936
+ const point = this.a;
25937
+ for (let i = 0; i < flat.length; i += 6) {
25938
+ const order = flip ? [0, 2, 1] : [0, 1, 2];
25939
+ for (const k of order) {
25940
+ point.copy(start).addScaledVector(side, flat[i + k * 2]).addScaledVector(up, flat[i + k * 2 + 1]);
25941
+ mesh2.worldToLocal(point);
25942
+ verts.push(point.x, point.y, point.z);
25943
+ }
25944
+ }
25945
+ const geo = new this.THREE.BufferGeometry();
25946
+ geo.setAttribute("position", new this.THREE.Float32BufferAttribute(verts, 3));
25947
+ const strip = new this.THREE.Mesh(geo, this.kit.patchMaterial(spec.color));
25948
+ strip.renderOrder = 2;
25949
+ this.addDecal(mv, { obj: strip, geometry: geo });
25950
+ }
25951
+ // ------------------------------------------------------------------------------------------------------------------------------------
25952
+ // Settlement
25953
+ // ------------------------------------------------------------------------------------------------------------------------------------
25954
+ /**
25955
+ * 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
25956
+ * one end tilts. Writes the geometry unless `write` is false (several parts of one merged mesh are then written once by `commit`).
25957
+ */
25958
+ sink(mv, dropAt, write = true) {
25959
+ if (!this.ownGeometry(mv)) return false;
25960
+ const mesh2 = mv.mesh;
25961
+ mesh2.updateWorldMatrix(true, false);
25962
+ const scale2 = mesh2.matrixWorld.getMaxScaleOnAxis() || 1;
25963
+ const down = this.b.set(0, -1, 0).transformDirection(this.m4.copy(mesh2.matrixWorld).invert());
25964
+ const work = mv.host ? mv.host.work : mv.work;
25965
+ const from = mv.range ? mv.range[0] * 3 : 0;
25966
+ const to = mv.range ? mv.range[1] * 3 : work.length;
25967
+ const point = this.a;
25968
+ for (let i = from; i < to; i += 3) {
25969
+ const d = dropAt(point.set(work[i], work[i + 1], work[i + 2]).applyMatrix4(mesh2.matrixWorld)) / scale2;
25970
+ work[i] += down.x * d;
25971
+ work[i + 1] += down.y * d;
25972
+ work[i + 2] += down.z * d;
25973
+ }
25974
+ for (const decal2 of mv.decals) decal2.obj.position.addScaledVector(down, dropAt(decal2.obj.getWorldPosition(point)) / scale2);
25975
+ mv.touched = true;
25976
+ if (write) this.commit(mv);
25977
+ return true;
25978
+ }
25979
+ /** Writes what `sink` moved into the geometry. */
25980
+ commit(mv) {
25981
+ const pos = positionAttr(mv.mesh.geometry);
25982
+ if (pos) this.writePositions(mv, pos);
25983
+ }
25112
25984
  // ------------------------------------------------------------------------------------------------------------------------------------
25113
25985
  // Char and heat glow
25114
25986
  // ------------------------------------------------------------------------------------------------------------------------------------
@@ -25504,6 +26376,7 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
25504
26376
  }
25505
26377
  restorePart(pv) {
25506
26378
  for (const mv of pv.meshes) this.restoreMesh(mv);
26379
+ delete pv.settled;
25507
26380
  pv.charApplied = 0;
25508
26381
  pv.glowApplied = 0;
25509
26382
  pv.meltApplied = 0;
@@ -25513,6 +26386,75 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
25513
26386
  this.kit.dispose();
25514
26387
  }
25515
26388
  };
26389
+ var SETTLE_SECONDS = 1.2;
26390
+ var SETTLE_REDRAW = 0.1;
26391
+ var dropOf = (line, point) => line ? settlementAt(line, point) : 0;
26392
+ 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]);
26393
+ var SettlementView = class {
26394
+ constructor(THREE, visuals) {
26395
+ __publicField3(this, "visuals", visuals);
26396
+ __publicField3(this, "moving", /* @__PURE__ */ new Map());
26397
+ __publicField3(this, "local");
26398
+ __publicField3(this, "inverse");
26399
+ this.local = new THREE.Vector3();
26400
+ this.inverse = new THREE.Matrix4();
26401
+ }
26402
+ /** The parts of an object (`root` draws it in object space) and their settlement now: those that changed start going to it. */
26403
+ follow(root, parts) {
26404
+ for (const [pv, target] of parts) {
26405
+ if (!target || pv.gone !== "none" || sameLine(this.moving.get(pv)?.target ?? pv.settled, target)) continue;
26406
+ this.finish(pv);
26407
+ this.moving.set(pv, { pv, root, start: pv.settled, target, shown: 0, elapsed: 0, lastDrawn: -SETTLE_REDRAW });
26408
+ }
26409
+ }
26410
+ get count() {
26411
+ return this.moving.size;
26412
+ }
26413
+ update(dt) {
26414
+ for (const s of this.moving.values()) {
26415
+ s.elapsed += dt;
26416
+ const done = s.elapsed >= SETTLE_SECONDS;
26417
+ if (!done && s.elapsed - s.lastDrawn < SETTLE_REDRAW) continue;
26418
+ s.lastDrawn = s.elapsed;
26419
+ const t = Math.min(1, s.elapsed / SETTLE_SECONDS);
26420
+ this.draw(s, t * t * (3 - 2 * t));
26421
+ if (done) {
26422
+ s.pv.settled = s.target;
26423
+ this.moving.delete(s.pv);
26424
+ }
26425
+ }
26426
+ }
26427
+ /** Draws the rest of a part's settlement at once (it is about to be split off or put back). */
26428
+ finish(pv) {
26429
+ const s = this.moving.get(pv);
26430
+ if (!s) return;
26431
+ this.draw(s, 1);
26432
+ pv.settled = s.target;
26433
+ this.moving.delete(pv);
26434
+ }
26435
+ /** Forget a part without drawing (it was put back as it was). */
26436
+ forget(pv) {
26437
+ this.moving.delete(pv);
26438
+ }
26439
+ clear() {
26440
+ this.moving.clear();
26441
+ }
26442
+ draw(s, shown) {
26443
+ const share = shown - s.shown;
26444
+ if (share <= 0) return;
26445
+ s.shown = shown;
26446
+ s.root.updateWorldMatrix(true, false);
26447
+ this.inverse.copy(s.root.matrixWorld).invert();
26448
+ const dropAt = (world) => {
26449
+ const p = this.local.copy(world).applyMatrix4(this.inverse);
26450
+ const at3 = [p.x, p.y, p.z];
26451
+ return (dropOf(s.target, at3) - dropOf(s.start, at3)) * share;
26452
+ };
26453
+ const writes = /* @__PURE__ */ new Map();
26454
+ for (const mv of s.pv.meshes) if (this.visuals.sink(mv, dropAt, false)) writes.set(mv.host ?? mv, mv);
26455
+ for (const mv of writes.values()) this.visuals.commit(mv);
26456
+ }
26457
+ };
25516
26458
  var rendererCount = 0;
25517
26459
  var COLLAPSE_CLOUD_KG = 1e3;
25518
26460
  var REBAR_COLOR = 7628380;
@@ -25521,6 +26463,8 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
25521
26463
  var WOOD_RIM = 1.5;
25522
26464
  var IMPACT_TAGS = ["metal", "glass", "wood"];
25523
26465
  var GLOW_FROM_C = 430;
26466
+ var MIN_DRAWN_CRACK_WIDTH = 4e-3;
26467
+ var SETTLEMENT_CRACK_COLOR = 2827296;
25524
26468
  var QUEUE_CAP = 4e3;
25525
26469
  var TRICKLE_SECONDS = [2, 5];
25526
26470
  var TRICKLE_EVERY = 0.25;
@@ -25729,6 +26673,7 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
25729
26673
  });
25730
26674
  __publicField3(this, "unsubscribe");
25731
26675
  __publicField3(this, "visuals");
26676
+ __publicField3(this, "settlement");
25732
26677
  __publicField3(this, "fragments");
25733
26678
  __publicField3(this, "ground");
25734
26679
  __publicField3(this, "pieceSys");
@@ -25736,6 +26681,7 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
25736
26681
  __publicField3(this, "active", /* @__PURE__ */ new Map());
25737
26682
  __publicField3(this, "fires", /* @__PURE__ */ new Map());
25738
26683
  __publicField3(this, "primedBurning", /* @__PURE__ */ new Set());
26684
+ __publicField3(this, "burnSounds", /* @__PURE__ */ new Map());
25739
26685
  __publicField3(this, "leaks", []);
25740
26686
  __publicField3(this, "homeTags", []);
25741
26687
  __publicField3(this, "throttles", /* @__PURE__ */ new Map());
@@ -25767,6 +26713,7 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
25767
26713
  this.v2 = new THREE.Vector3();
25768
26714
  const maxParticles = Math.max(8, Math.floor((o.maxParticles ?? 2500) * (this.low ? 0.5 : 1)));
25769
26715
  this.visuals = new Visuals(THREE, this.low, 600);
26716
+ this.settlement = new SettlementView(THREE, this.visuals);
25770
26717
  this.ground = new GroundSpots(THREE, o.scene, this.groundAt, 48);
25771
26718
  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");
25772
26719
  this.vfx = o.vfx ?? createVfx({ profile: this.quality, gravity: this.gravity, groundAt: this.groundAt, sprites: { capacity: maxParticles, density: this.low ? 0.6 : 1 } });
@@ -25781,7 +26728,8 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
25781
26728
  gravity: this.gravity,
25782
26729
  max: this.maxFragments,
25783
26730
  life: o.fragmentLife ?? 12,
25784
- formOf: (id) => this.world.materials.get(id)?.splitForm
26731
+ formOf: (id) => this.world.materials.get(id)?.splitForm,
26732
+ crumbles: (id) => this.world.materials.get(id)?.fracture === "crumble"
25785
26733
  });
25786
26734
  this.cages = new RebarCages(THREE, o.scene);
25787
26735
  this.pieceSys = new PieceSystem(THREE, {
@@ -26176,6 +27124,7 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
26176
27124
  this.checkBroken();
26177
27125
  for (const b of this.bounds.values()) b.onFrame?.(dt);
26178
27126
  for (const b of this.bounds.values()) if (b.soilNode && this.world.state(b.id)?.soil?.status === "yielding") this.onSettle(b.id);
27127
+ this.settlement.update(dt);
26179
27128
  this.updateActive(dt);
26180
27129
  this.updateLeaks(dt);
26181
27130
  this.updateTrickles(dt);
@@ -26251,6 +27200,18 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
26251
27200
  node.quaternion.set(q[0], q[1], q[2], q[3]);
26252
27201
  node.updateMatrixWorld(true);
26253
27202
  }
27203
+ /** The ground went down under some parts of an object: each part with a settlement (`PartState.settlement`) starts going down to it. */
27204
+ onSubside(objectId) {
27205
+ const b = this.bounds.get(objectId);
27206
+ const state = this.world.state(objectId);
27207
+ if (!b || !state) return;
27208
+ const parts = [];
27209
+ for (const ps of state.parts) {
27210
+ const pv = b.parts.get(ps.id);
27211
+ if (pv && ps.settlement) parts.push([pv, ps.settlement]);
27212
+ }
27213
+ this.settlement.follow(b.soilNode ?? b.root, parts);
27214
+ }
26254
27215
  syncTransforms() {
26255
27216
  for (const b of this.bounds.values()) this.syncOne(b);
26256
27217
  }
@@ -26401,9 +27362,15 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
26401
27362
  case "debris":
26402
27363
  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));
26403
27364
  break;
26404
- case "fracture":
27365
+ case "fracture": {
27366
+ const pv = this.partVis(ev.object, ev.part);
27367
+ if (pv) this.settlement.forget(pv);
26405
27368
  this.onFracture(ev);
26406
27369
  break;
27370
+ }
27371
+ case "subside":
27372
+ this.onSubside(ev.object);
27373
+ break;
26407
27374
  case "detach":
26408
27375
  this.onDetach(ev);
26409
27376
  break;
@@ -26413,6 +27380,10 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
26413
27380
  case "collapse": {
26414
27381
  const root = this.bounds.get(ev.object)?.root;
26415
27382
  const furniture = !!root && kitOriginOf(root)?.kind === "furniture";
27383
+ for (const part of ev.parts) {
27384
+ const pv = this.partVis(ev.object, part);
27385
+ if (pv) this.settlement.finish(pv);
27386
+ }
26416
27387
  this.splitLoose(ev.object);
26417
27388
  if (!furniture || ev.mass >= COLLAPSE_CLOUD_KG) this.playEvent({ kind: "collapse" }, this.rootPoint(ev.object));
26418
27389
  else this.vfx.burst("dust", this.rootPoint(ev.object), [0, 1, 0], clamp6(Math.round(ev.mass / 4), 4, 24), 0.8);
@@ -26430,7 +27401,7 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
26430
27401
  this.startFire(ev);
26431
27402
  this.activate(ev.object, ev.part);
26432
27403
  this.sound("ignite", ev.point, 0.7);
26433
- this.sound("burn_loop", ev.point, 0.5, this.burning(ev.object, ev.part));
27404
+ this.startBurnSound(ev.object, ev.part, ev.point, this.burning(ev.object, ev.part));
26434
27405
  break;
26435
27406
  case "primed":
26436
27407
  if (ev.trigger !== "explosion") {
@@ -26438,7 +27409,7 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
26438
27409
  this.primedBurning.add(`${ev.object}|${ev.part}`);
26439
27410
  this.activate(ev.object, ev.part);
26440
27411
  this.sound("primed", ev.point, 0.7, { seconds: ev.delay });
26441
- this.sound("burn_loop", ev.point, 0.5, { fuel: "gas" });
27412
+ if (!this.burnSounds.has(`${ev.object}|${ev.part}`)) this.startBurnSound(ev.object, ev.part, ev.point, { fuel: "gas" });
26442
27413
  }
26443
27414
  break;
26444
27415
  case "spall":
@@ -26460,20 +27431,20 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
26460
27431
  break;
26461
27432
  case "extinguish": {
26462
27433
  const f = this.fires.get(`${ev.object}|${ev.part}`);
26463
- const primed = this.primedBurning.delete(`${ev.object}|${ev.part}`);
27434
+ this.primedBurning.delete(`${ev.object}|${ev.part}`);
26464
27435
  if (f) {
26465
27436
  this.stopFire(`${ev.object}|${ev.part}`);
26466
27437
  this.vfx.burst("steam", f.point, [0, 1, 0], 14, 1.2);
26467
27438
  }
26468
- if (f || primed) this.sound("burn_end", f?.point ?? this.rootPoint(ev.object), 0.5, primed ? { fuel: "gas" } : this.burning(ev.object, ev.part));
27439
+ this.endBurnSound(`${ev.object}|${ev.part}`);
26469
27440
  this.activate(ev.object, ev.part);
26470
27441
  this.sound("extinguish", f?.point ?? this.rootPoint(ev.object), 0.6);
26471
27442
  break;
26472
27443
  }
26473
27444
  case "burnout": {
26474
27445
  this.stopFire(`${ev.object}|${ev.part}`);
26475
- const primed = this.primedBurning.delete(`${ev.object}|${ev.part}`);
26476
- this.sound("burn_end", ev.point, 0.5, primed ? { fuel: "gas" } : this.burning(ev.object, ev.part));
27446
+ this.primedBurning.delete(`${ev.object}|${ev.part}`);
27447
+ this.endBurnSound(`${ev.object}|${ev.part}`);
26477
27448
  this.vfx.burst("ash", ev.point, [0, 1, 0], 16, 1);
26478
27449
  this.vfx.burst("smoke", ev.point, [0, 1, 0], 6, 0.8, { color: SMOKE_TONES.grey });
26479
27450
  const pv = this.partVis(ev.object, ev.part);
@@ -26541,7 +27512,8 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
26541
27512
  break;
26542
27513
  }
26543
27514
  case "explode":
26544
- if (this.primedBurning.delete(`${ev.object}|${ev.part}`)) this.sound("burn_end", ev.point, 0.5, { fuel: "gas" });
27515
+ this.primedBurning.delete(`${ev.object}|${ev.part}`);
27516
+ this.endBurnSound(`${ev.object}|${ev.part}`);
26545
27517
  this.onExplode(ev);
26546
27518
  break;
26547
27519
  case "destroyed":
@@ -26660,7 +27632,20 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
26660
27632
  const pv = b?.parts.get(ev.part);
26661
27633
  if (!b || !pv) return;
26662
27634
  const def = this.world.materials.get(pv.material);
26663
- if (def?.visual?.transparent) {
27635
+ const stair = ev.crack.stair;
27636
+ if (stair && !def?.visual?.transparent) {
27637
+ const wp = new this.THREE.Vector3(ev.worldPoint[0], ev.worldPoint[1], ev.worldPoint[2]);
27638
+ const seed = hashNumbers(ev.time, ev.worldPoint[0], ev.worldPoint[1], ev.worldPoint[2]);
27639
+ const width = Math.max(stair.width, MIN_DRAWN_CRACK_WIDTH);
27640
+ const normal = this.worldDir(b, ev.crack.normal);
27641
+ const toward2 = this.worldDir(b, stair.along);
27642
+ const look = { length: ev.crack.length, step: stair.step, width, color: SETTLEMENT_CRACK_COLOR, seed };
27643
+ for (const face of [wp, wp.clone().addScaledVector(normal, -stair.through)]) {
27644
+ const out = face === wp ? normal : normal.clone().negate();
27645
+ const mv = this.nearestMesh(pv, face);
27646
+ if (mv) this.visuals.addStairCrack(mv, face, out, toward2, look);
27647
+ }
27648
+ } else if (def?.visual?.transparent) {
26664
27649
  const wp = new this.THREE.Vector3(ev.worldPoint[0], ev.worldPoint[1], ev.worldPoint[2]);
26665
27650
  const mv = this.nearestMesh(pv, wp);
26666
27651
  const seed = hashNumbers(ev.time, ev.worldPoint[0], ev.worldPoint[1], ev.worldPoint[2]);
@@ -27230,9 +28215,25 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
27230
28215
  }
27231
28216
  stopFire(key2) {
27232
28217
  const fire2 = this.fires.get(key2);
27233
- if (!fire2) return;
27234
- if (fire2.handle !== void 0) this.vfx.stop(fire2.handle);
27235
- this.fires.delete(key2);
28218
+ if (fire2) {
28219
+ if (fire2.handle !== void 0) this.vfx.stop(fire2.handle);
28220
+ this.fires.delete(key2);
28221
+ }
28222
+ this.endBurnSound(key2);
28223
+ }
28224
+ /** 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. */
28225
+ startBurnSound(object, part, point, detail) {
28226
+ const key2 = `${object}|${part}`;
28227
+ this.endBurnSound(key2);
28228
+ this.burnSounds.set(key2, { object, point, detail });
28229
+ this.sound("burn_loop", point, 0.5, detail);
28230
+ }
28231
+ /** The part's fire loop ends, with the point and the fuel it started with; nothing when it has none running. */
28232
+ endBurnSound(key2) {
28233
+ const started = this.burnSounds.get(key2);
28234
+ if (!started) return;
28235
+ this.burnSounds.delete(key2);
28236
+ this.sound("burn_end", started.point, 0.5, started.detail);
27236
28237
  }
27237
28238
  // ----------------------------------------------------------------------------------------------------------------------------------
27238
28239
  // Fire, heat, char, melt, leaks
@@ -27258,6 +28259,7 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
27258
28259
  const ps = pv ? this.world.part(a.object, a.part) : null;
27259
28260
  if (!pv || !ps) {
27260
28261
  this.active.delete(key2);
28262
+ this.stopFire(key2);
27261
28263
  continue;
27262
28264
  }
27263
28265
  const def = this.world.materials.get(pv.material);
@@ -27377,6 +28379,7 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
27377
28379
  for (const home of homes) this.restoreHome(home);
27378
28380
  for (const [key2, a] of [...this.active]) if (homes.has(a.object)) this.active.delete(key2);
27379
28381
  for (const [key2, f] of [...this.fires]) if (homes.has(f.object)) this.stopFire(key2);
28382
+ for (const [key2, started] of [...this.burnSounds]) if (homes.has(started.object)) this.endBurnSound(key2);
27380
28383
  for (let i = this.leaks.length - 1; i >= 0; i--) if (homes.has(this.leaks[i].object)) this.leaks.splice(i, 1);
27381
28384
  for (const home of homes) {
27382
28385
  this.fragments.removeHome(home);
@@ -27389,6 +28392,7 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
27389
28392
  for (const b of [...this.bounds.values()]) {
27390
28393
  for (const [pid, pv] of [...b.parts]) {
27391
28394
  if (pv.home !== home) continue;
28395
+ this.settlement.forget(pv);
27392
28396
  this.visuals.restorePart(pv);
27393
28397
  this.tagPart(pv, home);
27394
28398
  if (b !== homeBound && homeBound) {
@@ -27410,8 +28414,10 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
27410
28414
  this.slices.length = 0;
27411
28415
  this.active.clear();
27412
28416
  for (const key2 of [...this.fires.keys()]) this.stopFire(key2);
28417
+ for (const key2 of [...this.burnSounds.keys()]) this.endBurnSound(key2);
27413
28418
  this.leaks.length = 0;
27414
28419
  this.trickles.length = 0;
28420
+ this.settlement.clear();
27415
28421
  for (const id of [...this.bounds.keys()]) if (this.bounds.has(id)) this.restore(id);
27416
28422
  for (const b of [...this.bounds.values()]) {
27417
28423
  if (b.isSplit) {
@@ -28037,6 +29043,7 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
28037
29043
  }
28038
29044
  };
28039
29045
  var DEFAULT_FIRE = { perFire: 0.08, max: 0.45 };
29046
+ var FIRE_FADE_SECONDS = 0.4;
28040
29047
  var DEFAULT_CRACKLES = { base: 2, perSqrtFire: 3, max: 14, volume: 0.5 };
28041
29048
  var DEFAULT_INDOOR_CONFINEMENT = 1.3;
28042
29049
  function createDestructionSound(engine, options = {}) {
@@ -28058,6 +29065,11 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
28058
29065
  const loops = /* @__PURE__ */ new Map();
28059
29066
  const fires = [];
28060
29067
  let nextCrackle = 0;
29068
+ const fading = [];
29069
+ const fadeOut = (handle) => {
29070
+ handle.set({ volume: 0 });
29071
+ fading.push({ handle, left: FIRE_FADE_SECONDS });
29072
+ };
28061
29073
  const fuelFor = (detail) => detail?.fuel ?? (detail?.material !== void 0 ? fuelOf[detail.material] : void 0) ?? defaultFuel;
28062
29074
  const setFire = () => {
28063
29075
  if (options.burnLoop === false) return;
@@ -28068,7 +29080,7 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
28068
29080
  }
28069
29081
  for (const [id, handle] of loops) {
28070
29082
  if (count.has(id)) continue;
28071
- handle.stop();
29083
+ fadeOut(handle);
28072
29084
  loops.delete(id);
28073
29085
  }
28074
29086
  for (const [id, n] of count) {
@@ -28095,8 +29107,15 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
28095
29107
  else if (best >= 0) fires.splice(best, 1);
28096
29108
  };
28097
29109
  const reset = () => {
29110
+ for (const handle of loops.values()) fadeOut(handle);
29111
+ loops.clear();
29112
+ fires.length = 0;
29113
+ };
29114
+ const dispose = () => {
28098
29115
  for (const handle of loops.values()) handle.stop();
28099
29116
  loops.clear();
29117
+ for (const { handle } of fading) handle.stop();
29118
+ fading.length = 0;
28100
29119
  fires.length = 0;
28101
29120
  };
28102
29121
  const blastSound = (point, volume, detail) => {
@@ -28143,6 +29162,13 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
28143
29162
  engine.play(sound, { position: point, volume: volume * (rule.volumeScale ?? 1), intensity: Math.min(1, Math.max(0, volume)), ...params ? { params } : {} });
28144
29163
  },
28145
29164
  update(dt) {
29165
+ for (let i = fading.length - 1; i >= 0; i--) {
29166
+ const entry = fading[i];
29167
+ entry.left -= dt;
29168
+ if (entry.left > 0) continue;
29169
+ entry.handle.stop();
29170
+ fading.splice(i, 1);
29171
+ }
28146
29172
  if (!crackles || fires.length === 0 || options.burnLoop === false) return;
28147
29173
  const weight = fires.reduce((sum2, f) => sum2 + FIRE_CRACKLE_OF[f.fuel], 0);
28148
29174
  if (weight <= 0) return;
@@ -28168,7 +29194,7 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
28168
29194
  return by;
28169
29195
  },
28170
29196
  reset,
28171
- dispose: reset
29197
+ dispose
28172
29198
  };
28173
29199
  }
28174
29200
  var blast2 = (gain) => ({ bus: "world", gain, falloff: BLAST_FALLOFF, cutoffK: 6e-3, priority: 3 });
@@ -28554,7 +29580,7 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
28554
29580
  const l = Math.hypot(v[0], v[1], v[2]);
28555
29581
  return l > 1e-12 ? [v[0] / l, v[1] / l, v[2] / l] : [0, 0, -1];
28556
29582
  };
28557
- var cross2 = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
29583
+ var cross3 = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
28558
29584
  var sampleCone = (dir, deg, rng) => {
28559
29585
  if (deg <= 0) return dir;
28560
29586
  const cosMax = Math.cos(deg * Math.PI / 180);
@@ -28562,8 +29588,8 @@ void main(){vec2 d=gl_PointCoord-.5;float r=length(d)*2.;if(r>1.)discard;float a
28562
29588
  const sinT = Math.sqrt(Math.max(0, 1 - cosT * cosT));
28563
29589
  const phi = rng() * Math.PI * 2;
28564
29590
  const helper = Math.abs(dir[1]) < 0.9 ? [0, 1, 0] : [1, 0, 0];
28565
- const u = normalize2(cross2(helper, dir));
28566
- const v = cross2(dir, u);
29591
+ const u = normalize2(cross3(helper, dir));
29592
+ const v = cross3(dir, u);
28567
29593
  const cx = Math.cos(phi) * sinT;
28568
29594
  const cy = Math.sin(phi) * sinT;
28569
29595
  return normalize2([
@@ -45255,7 +46281,7 @@ ${problems.map((problem) => ` - ${problem}`).join("\n")}`);
45255
46281
  var sub3 = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
45256
46282
  var scale = (a, s) => [a[0] * s, a[1] * s, a[2] * s];
45257
46283
  var dot3 = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
45258
- var cross3 = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
46284
+ var cross4 = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
45259
46285
  var length2 = (a) => Math.hypot(a[0], a[1], a[2]);
45260
46286
  var distance3 = (a, b) => Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]);
45261
46287
  var normalize3 = (a, fallback2 = [0, 1, 0]) => {
@@ -45266,8 +46292,8 @@ ${problems.map((problem) => ` - ${problem}`).join("\n")}`);
45266
46292
  var lerp32 = (a, b, t) => [lerp6(a[0], b[0], t), lerp6(a[1], b[1], t), lerp6(a[2], b[2], t)];
45267
46293
  var quatRotate = (q, v) => {
45268
46294
  const u = [q[0], q[1], q[2]];
45269
- const c1 = cross3(u, v);
45270
- const c2 = cross3(u, c1);
46295
+ const c1 = cross4(u, v);
46296
+ const c2 = cross4(u, c1);
45271
46297
  return [v[0] + 2 * (q[3] * c1[0] + c2[0]), v[1] + 2 * (q[3] * c1[1] + c2[1]), v[2] + 2 * (q[3] * c1[2] + c2[2])];
45272
46298
  };
45273
46299
  var quatConjugate = (q) => [-q[0], -q[1], -q[2], q[3]];
@@ -45580,7 +46606,7 @@ ${problems.map((problem) => ` - ${problem}`).join("\n")}`);
45580
46606
  const normal = normalize3(normalIn);
45581
46607
  const ref = tangentIn && length2(tangentIn) > 1e-6 ? tangentIn : Math.abs(normal[1]) > 0.9 ? [1, 0, 0] : [0, 1, 0];
45582
46608
  const t1 = normalize3(sub3(ref, scale(normal, dot3(ref, normal))), [1, 0, 0]);
45583
- return { normal, t1, t2: cross3(normal, t1) };
46609
+ return { normal, t1, t2: cross4(normal, t1) };
45584
46610
  };
45585
46611
  var sampleDir = (b, i) => [
45586
46612
  b.t1[0] * COS2[i] + b.t2[0] * SIN2[i],
@@ -45937,7 +46963,7 @@ ${problems.map((problem) => ` - ${problem}`).join("\n")}`);
45937
46963
  if (!def || liters <= 0) return;
45938
46964
  let s = stainAt(hit.surface, liquid, hit.point);
45939
46965
  if (s) {
45940
- const basis2 = { normal: s.normal, t1: s.tangent, t2: cross3(s.normal, s.tangent) };
46966
+ const basis2 = { normal: s.normal, t1: s.tangent, t2: cross4(s.normal, s.tangent) };
45941
46967
  nudgeCenter(s, hit.point, clamp7(liters / (s.liters + liters) * 0.5, 0, 0.5), basis2);
45942
46968
  addInflow(s, liters, velocity);
45943
46969
  if (tint) s.tint = { ...tint };
@@ -47946,7 +48972,7 @@ ${problems.map((problem) => ` - ${problem}`).join("\n")}`);
47946
48972
  const projected = sub3(options.center, scale(floorNormal, dot3(sub3(options.center, options.floor.point), floorNormal)));
47947
48973
  const wallNormal = options.wall ? normalize3(reject(options.wall.normal, floorNormal), floorBasis.t2) : void 0;
47948
48974
  const awayAxis = wallNormal ?? floorBasis.t2;
47949
- const columnAxis = wallNormal ? normalize3(cross3(floorNormal, wallNormal), floorBasis.t1) : floorBasis.t1;
48975
+ const columnAxis = wallNormal ? normalize3(cross4(floorNormal, wallNormal), floorBasis.t1) : floorBasis.t1;
47950
48976
  const wallRows = options.wall ? Math.min(totalRows - 1, options.wallRows ?? DEFAULT_WALL_ROWS) : 0;
47951
48977
  let fold3 = projected;
47952
48978
  if (options.wall && wallNormal) {
@@ -48196,14 +49222,14 @@ ${problems.map((problem) => ` - ${problem}`).join("\n")}`);
48196
49222
  var __defNormalProp5 = (obj, key2, value) => key2 in obj ? __defProp6(obj, key2, { enumerable: true, configurable: true, writable: true, value }) : obj[key2] = value;
48197
49223
  var __publicField6 = (obj, key2, value) => __defNormalProp5(obj, typeof key2 !== "symbol" ? key2 + "" : key2, value);
48198
49224
  var sub4 = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
48199
- var cross4 = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
49225
+ var cross5 = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
48200
49226
  var dot4 = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
48201
49227
  var norm = (a) => {
48202
49228
  const l = Math.hypot(a[0], a[1], a[2]) || 1;
48203
49229
  return [a[0] / l, a[1] / l, a[2] / l];
48204
49230
  };
48205
49231
  var orient = (p, n, outward) => {
48206
- if (dot4(cross4(sub4(p[1], p[0]), sub4(p[2], p[0])), outward) < 0) return { p: [...p].reverse(), n: [...n].reverse() };
49232
+ if (dot4(cross5(sub4(p[1], p[0]), sub4(p[2], p[0])), outward) < 0) return { p: [...p].reverse(), n: [...n].reverse() };
48207
49233
  return { p, n };
48208
49234
  };
48209
49235
  var finish = (faces) => {
@@ -48320,8 +49346,8 @@ ${problems.map((problem) => ` - ${problem}`).join("\n")}`);
48320
49346
  c[1] += q[1] / pts.length;
48321
49347
  c[2] += q[2] / pts.length;
48322
49348
  }
48323
- const e1 = norm(cross4(u, Math.abs(u[0]) < 0.9 ? [1, 0, 0] : [0, 1, 0]));
48324
- const e2 = cross4(u, e1);
49349
+ const e1 = norm(cross5(u, Math.abs(u[0]) < 0.9 ? [1, 0, 0] : [0, 1, 0]));
49350
+ const e2 = cross5(u, e1);
48325
49351
  const keyed = pts.map((q) => ({ q, a: Math.atan2(dot4(sub4(q, c), e2), dot4(sub4(q, c), e1)) })).sort((p, r) => p.a - r.a);
48326
49352
  for (let i = 0; i < keyed.length; i++) {
48327
49353
  for (const q of [c, keyed[i].q, keyed[(i + 1) % keyed.length].q]) put(q[0], q[1], q[2], u[0], u[1], u[2]);
@@ -59218,8 +60244,8 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
59218
60244
  if (!pos || pos === "static") o.container.style.position = "relative";
59219
60245
  root.appendChild(style);
59220
60246
  o.container.appendChild(root);
59221
- const cross5 = mk4("div", "tr-cross");
59222
- cross5.appendChild(mk4("i"));
60247
+ const cross6 = mk4("div", "tr-cross");
60248
+ cross6.appendChild(mk4("i"));
59223
60249
  const ring2 = document.createElementNS("http://www.w3.org/2000/svg", "svg");
59224
60250
  ring2.setAttribute("class", "tr-ring");
59225
60251
  ring2.setAttribute("viewBox", "0 0 64 64");
@@ -59574,7 +60600,7 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
59574
60600
  o.onResize();
59575
60601
  };
59576
60602
  toggle.onclick = togglePanels;
59577
- root.append(cross5, ring2, top, bottom, help, left, right, toggle, ...toolMenu ? [toolMenu.element] : []);
60603
+ root.append(cross6, ring2, top, bottom, help, left, right, toggle, ...toolMenu ? [toolMenu.element] : []);
59578
60604
  const bar2 = (value, max2, cls) => {
59579
60605
  const b = mk4("div", `tr-bar ${cls}`);
59580
60606
  const i = mk4("i");