@adatechnology/game-crowd 0.2.6 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -0
- package/dist/index.d.ts +433 -15
- package/dist/index.global.js +2153 -77
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +2088 -77
- package/dist/index.js.map +1 -1
- package/dist/physics-worker.js +580 -0
- package/dist/physics-worker.js.map +1 -0
- package/package.json +9 -9
package/dist/index.global.js
CHANGED
|
@@ -22,27 +22,54 @@ var GameCrowd = (() => {
|
|
|
22
22
|
var index_exports = {};
|
|
23
23
|
__export(index_exports, {
|
|
24
24
|
AGENT_FIELDS: () => AGENT_FIELDS,
|
|
25
|
+
BALANCE: () => BALANCE,
|
|
25
26
|
CELL: () => CELL,
|
|
26
27
|
CIVILIAN_DEFAULTS: () => CIVILIAN_DEFAULTS,
|
|
27
28
|
CROWD_DEFAULTS: () => CROWD_DEFAULTS,
|
|
29
|
+
CROWD_PHYSICS_DEFAULTS: () => CROWD_PHYSICS_DEFAULTS,
|
|
28
30
|
DOOR_DEFAULTS: () => DOOR_DEFAULTS,
|
|
31
|
+
FALL_CAUSE: () => FALL_CAUSE,
|
|
29
32
|
HERD_DEFAULTS: () => HERD_DEFAULTS,
|
|
30
33
|
HERD_ROLES: () => HERD_ROLES,
|
|
31
34
|
MISSING: () => MISSING,
|
|
32
35
|
STATE: () => STATE,
|
|
36
|
+
attachPhysicsWorker: () => attachPhysicsWorker,
|
|
33
37
|
bindInfection: () => bindInfection,
|
|
34
38
|
carrionCenter: () => carrionCenter,
|
|
39
|
+
contactStep: () => contactStep,
|
|
35
40
|
createCivilians: () => createCivilians,
|
|
41
|
+
createContactView: () => createContactView,
|
|
36
42
|
createCrowd: () => createCrowd,
|
|
37
43
|
createDoors: () => createDoors,
|
|
38
44
|
createFlock: () => createFlock,
|
|
39
45
|
createHerd: () => createHerd,
|
|
40
46
|
createNavGrid: () => createNavGrid,
|
|
41
47
|
createRng: () => createRng,
|
|
42
|
-
createStore: () => createStore
|
|
48
|
+
createStore: () => createStore,
|
|
49
|
+
identityOf: () => identityOf,
|
|
50
|
+
mix32: () => mix32,
|
|
51
|
+
resolvePhysics: () => resolvePhysics,
|
|
52
|
+
unit: () => unit
|
|
43
53
|
});
|
|
44
54
|
|
|
45
55
|
// ../game-contracts/dist/index.js
|
|
56
|
+
var DEFAULT_BLAST_SHOVE = { coupling: 0.02, maxSpeed: 20, lift: 0.3, minSpeed: 0.3, falloffPower: 2 };
|
|
57
|
+
function blastShove(blast, target, options = {}) {
|
|
58
|
+
const o = { ...DEFAULT_BLAST_SHOVE, ...options };
|
|
59
|
+
const dx = target.position[0] - blast.point[0];
|
|
60
|
+
const dy = target.position[1] - blast.point[1];
|
|
61
|
+
const dz = target.position[2] - blast.point[2];
|
|
62
|
+
const distance = Math.hypot(dx, dy, dz);
|
|
63
|
+
if (!(blast.radius > 0) || distance > blast.radius) return void 0;
|
|
64
|
+
const falloff = (1 - distance / blast.radius) ** o.falloffPower;
|
|
65
|
+
const mass = Math.max(target.mass, 0.05);
|
|
66
|
+
const speed = Math.min(o.maxSpeed, Math.sqrt(2 * blast.energy * (blast.reach ?? 1) * o.coupling / mass)) * falloff;
|
|
67
|
+
if (!(speed >= o.minSpeed)) return void 0;
|
|
68
|
+
const direction = distance > 1e-4 ? [dx / distance, dy / distance + o.lift, dz / distance] : [0, 1, 0];
|
|
69
|
+
const length = Math.hypot(direction[0], direction[1], direction[2]);
|
|
70
|
+
return { speed, direction: [direction[0] / length, direction[1] / length, direction[2] / length], distance, falloff };
|
|
71
|
+
}
|
|
72
|
+
var DEFAULT_COST_ALLOCATION_BYTES = 1024 * 1024;
|
|
46
73
|
var MISSING_BITS = {
|
|
47
74
|
head: 1,
|
|
48
75
|
armL: 2,
|
|
@@ -55,6 +82,54 @@ var GameCrowd = (() => {
|
|
|
55
82
|
legRBroken: 256,
|
|
56
83
|
jaw: 512
|
|
57
84
|
};
|
|
85
|
+
var DEFERRED_QUEUE_ID = {
|
|
86
|
+
DESTRUCTION_EVENTS: "destruction.events",
|
|
87
|
+
BREACH_PAINT: "floorplan.breach-paint",
|
|
88
|
+
AUDIO_VOICES: "audio.voices",
|
|
89
|
+
VEHICLE_CREASE: "vehicles.crease"
|
|
90
|
+
};
|
|
91
|
+
var DEFERRED_PRIORITY = {
|
|
92
|
+
[DEFERRED_QUEUE_ID.DESTRUCTION_EVENTS]: { priority: 300, phase: "present" },
|
|
93
|
+
[DEFERRED_QUEUE_ID.BREACH_PAINT]: { priority: 250, phase: "present" },
|
|
94
|
+
[DEFERRED_QUEUE_ID.AUDIO_VOICES]: { priority: 200, phase: "idle", reserveMs: 1 },
|
|
95
|
+
[DEFERRED_QUEUE_ID.VEHICLE_CREASE]: { priority: 100, phase: "idle" }
|
|
96
|
+
};
|
|
97
|
+
var SURFACE_IDS = ["concrete", "asphalt", "wood", "tile", "dirt", "grass", "gravel", "sand", "metal", "water", "glass", "carpet"];
|
|
98
|
+
var GAIT_IDS = ["walk", "run"];
|
|
99
|
+
var LANDING_IDS = ["light", "heavy"];
|
|
100
|
+
var BODY_SOUND_IDS = [
|
|
101
|
+
"jump",
|
|
102
|
+
"cloth_rustle",
|
|
103
|
+
"body_fall",
|
|
104
|
+
"fall_damage",
|
|
105
|
+
"player_hurt",
|
|
106
|
+
"player_breath_run",
|
|
107
|
+
"player_death",
|
|
108
|
+
"bite",
|
|
109
|
+
"grab",
|
|
110
|
+
"punch_swing",
|
|
111
|
+
"punch_hit_body",
|
|
112
|
+
"punch_hit_face",
|
|
113
|
+
"punch_hit_object",
|
|
114
|
+
"kick_swing",
|
|
115
|
+
"kick_hit",
|
|
116
|
+
"kick_hit_object",
|
|
117
|
+
"shove_two_hands",
|
|
118
|
+
"knife_swing",
|
|
119
|
+
"knife_stab",
|
|
120
|
+
"knife_slash",
|
|
121
|
+
"knife_hit_object",
|
|
122
|
+
"blunt_swing",
|
|
123
|
+
"blunt_hit",
|
|
124
|
+
"blunt_hit_object",
|
|
125
|
+
"axe_swing",
|
|
126
|
+
"axe_chop",
|
|
127
|
+
"axe_hit_object"
|
|
128
|
+
];
|
|
129
|
+
var footstepSoundId = (surface, gait) => `footstep_${surface}_${gait}`;
|
|
130
|
+
var FOOTSTEP_SOUND_IDS = SURFACE_IDS.flatMap((surface) => GAIT_IDS.map((gait) => footstepSoundId(surface, gait)));
|
|
131
|
+
var LAND_SOUND_IDS = SURFACE_IDS.flatMap((surface) => LANDING_IDS.map((landing) => `land_${surface}_${landing}`));
|
|
132
|
+
var BODY_FOLEY_IDS = [...FOOTSTEP_SOUND_IDS, ...LAND_SOUND_IDS, ...BODY_SOUND_IDS];
|
|
58
133
|
|
|
59
134
|
// ../game-characters/dist/index.js
|
|
60
135
|
var __defProp2 = Object.defineProperty;
|
|
@@ -1527,6 +1602,11 @@ var GameCrowd = (() => {
|
|
|
1527
1602
|
|
|
1528
1603
|
// src/grid.ts
|
|
1529
1604
|
var CELL = { FREE: 0, SOLID: 1, LOW_WALL: 2, OPEN_DOOR: 3, GLASS: 4, CLOSED_DOOR: 5 };
|
|
1605
|
+
var createCellOf = (half, cell, n) => (x, z) => {
|
|
1606
|
+
const gx = Math.floor((x + half) / cell);
|
|
1607
|
+
const gz = Math.floor((z + half) / cell);
|
|
1608
|
+
return gx < 0 || gz < 0 || gx >= n || gz >= n ? -1 : gz * n + gx;
|
|
1609
|
+
};
|
|
1530
1610
|
var BORDER_WIDTH = 1;
|
|
1531
1611
|
var BORDER_HEIGHT = 50;
|
|
1532
1612
|
var LOS_STEP = 0.5;
|
|
@@ -1538,11 +1618,7 @@ var GameCrowd = (() => {
|
|
|
1538
1618
|
const n = size / cell;
|
|
1539
1619
|
const occ = new Uint8Array(n * n);
|
|
1540
1620
|
const occH = new Float32Array(n * n);
|
|
1541
|
-
const cellOf = (
|
|
1542
|
-
const gx = Math.floor((x + half) / cell);
|
|
1543
|
-
const gz = Math.floor((z + half) / cell);
|
|
1544
|
-
return gx < 0 || gz < 0 || gx >= n || gz >= n ? -1 : gz * n + gx;
|
|
1545
|
-
};
|
|
1621
|
+
const cellOf = createCellOf(half, cell, n);
|
|
1546
1622
|
const solidAt = (x, z, y = 0.5) => {
|
|
1547
1623
|
const c = cellOf(x, z);
|
|
1548
1624
|
return c < 0 || occ[c] !== 0 && occ[c] !== 3 && occH[c] > y;
|
|
@@ -1596,6 +1672,1859 @@ var GameCrowd = (() => {
|
|
|
1596
1672
|
};
|
|
1597
1673
|
}
|
|
1598
1674
|
|
|
1675
|
+
// src/identity.ts
|
|
1676
|
+
var mix32 = (value) => {
|
|
1677
|
+
let hash = value >>> 0;
|
|
1678
|
+
hash ^= hash >>> 16;
|
|
1679
|
+
hash = Math.imul(hash, 2246822507);
|
|
1680
|
+
hash ^= hash >>> 13;
|
|
1681
|
+
hash = Math.imul(hash, 3266489909);
|
|
1682
|
+
hash ^= hash >>> 16;
|
|
1683
|
+
return hash >>> 0;
|
|
1684
|
+
};
|
|
1685
|
+
var unit = (identity, step, salt) => (mix32(mix32(identity ^ salt) + Math.imul(step + 1, 2654435769) >>> 0) >>> 0) / 4294967296;
|
|
1686
|
+
var identityOf = (seed, serial) => mix32(seed ^ mix32(serial));
|
|
1687
|
+
var SALT = {
|
|
1688
|
+
FALL: 62481,
|
|
1689
|
+
TRIP: 29209,
|
|
1690
|
+
LIMB: 110848,
|
|
1691
|
+
REGION: 189968,
|
|
1692
|
+
JAW: 239536,
|
|
1693
|
+
CORPSE: 311518,
|
|
1694
|
+
SPIN: 364922,
|
|
1695
|
+
SPEED: 413981,
|
|
1696
|
+
REACTION: 466972,
|
|
1697
|
+
STABILITY: 569886,
|
|
1698
|
+
TRIP_FACTOR: 605021,
|
|
1699
|
+
DRIVE: 655571,
|
|
1700
|
+
CADENCE: 728142,
|
|
1701
|
+
AGGRESSION: 797335,
|
|
1702
|
+
IDLE: 867848,
|
|
1703
|
+
GAIT: 937753
|
|
1704
|
+
};
|
|
1705
|
+
|
|
1706
|
+
// src/types.ts
|
|
1707
|
+
var STATE = {
|
|
1708
|
+
WANDER: 0,
|
|
1709
|
+
CHASE: 1,
|
|
1710
|
+
GRAB: 2,
|
|
1711
|
+
STUNNED: 3,
|
|
1712
|
+
CALLING: 4,
|
|
1713
|
+
AIRBORNE: 5,
|
|
1714
|
+
DORMANT: 6,
|
|
1715
|
+
SWELLING: 7,
|
|
1716
|
+
SWING: 8,
|
|
1717
|
+
EATING: 9,
|
|
1718
|
+
PILE_STEP: 10,
|
|
1719
|
+
SEEKING_SOUND: 12
|
|
1720
|
+
};
|
|
1721
|
+
var BALANCE = { STANDING: 0, STAGGERING: 1, FALLEN: 2, GETTING_UP: 3, THROWN: 4 };
|
|
1722
|
+
var FALL_CAUSE = { NONE: 0, PRESSURE: 1, SHOVE: 2, TRIP: 3, LANDING: 4, COLLAPSE: 5, BLAST: 6 };
|
|
1723
|
+
var MISSING = { SKULL: 1, ARM_L: 2, ARM_R: 4, LEG_L: 8, LEG_R: 16, ARM_L_BROKEN: 32, ARM_R_BROKEN: 64, LEG_L_BROKEN: 128, LEG_R_BROKEN: 256, JAW: 512 };
|
|
1724
|
+
|
|
1725
|
+
// src/physics/balance.ts
|
|
1726
|
+
var GRAVITY_FORCE = 9.8;
|
|
1727
|
+
var GROUND_MASS_FACTOR = 3;
|
|
1728
|
+
var TRIP_MIN_SPEED = 0.5;
|
|
1729
|
+
var TRIP_REFERENCE_SPEED = 1.4;
|
|
1730
|
+
var TRIP_MOUND = 150;
|
|
1731
|
+
var TRIP_CLEARANCE = 0.1;
|
|
1732
|
+
var COLLAPSE_STAGGER = 0.6;
|
|
1733
|
+
var COLLAPSE_FALL = 1;
|
|
1734
|
+
var CAUSE_ORDER = [FALL_CAUSE.PRESSURE, FALL_CAUSE.SHOVE, FALL_CAUSE.TRIP, FALL_CAUSE.LANDING, FALL_CAUSE.COLLAPSE, FALL_CAUSE.BLAST];
|
|
1735
|
+
var CHAIN_WINDOW_STEPS = 240;
|
|
1736
|
+
var CARRY_STEPS = 24;
|
|
1737
|
+
var isCarried = (cause) => cause !== FALL_CAUSE.PRESSURE && cause !== FALL_CAUSE.TRIP;
|
|
1738
|
+
function createBalance(ctx) {
|
|
1739
|
+
const { store } = ctx;
|
|
1740
|
+
const requests = [];
|
|
1741
|
+
const balanceOptions = () => ctx.options.balance;
|
|
1742
|
+
const stabilityOf = (i) => (ctx.typeOf(i).balance?.stability ?? 1) * store.stability[i];
|
|
1743
|
+
const canFall = (i) => ctx.typeOf(i).balance?.canFall !== false;
|
|
1744
|
+
const criticalOf = (i) => balanceOptions().criticalFactor * store.mass[i] * GRAVITY_FORCE * stabilityOf(i);
|
|
1745
|
+
const requestFall = (i, cause, score) => {
|
|
1746
|
+
if (!canFall(i)) return;
|
|
1747
|
+
requests.push({ index: i, identity: store.identity[i], cause, score, born: ctx.step });
|
|
1748
|
+
};
|
|
1749
|
+
const stagger = (i) => {
|
|
1750
|
+
if (store.balance[i] !== BALANCE.STANDING || !canFall(i)) return;
|
|
1751
|
+
store.balance[i] = BALANCE.STAGGERING;
|
|
1752
|
+
store.balanceTimer[i] = 0;
|
|
1753
|
+
};
|
|
1754
|
+
const shove = (i, speed, cause) => {
|
|
1755
|
+
const options = balanceOptions();
|
|
1756
|
+
const knockSpeed = options.shoveFall * stabilityOf(i);
|
|
1757
|
+
if (store.balance[i] !== BALANCE.STANDING && store.balance[i] !== BALANCE.STAGGERING) return;
|
|
1758
|
+
if (speed > knockSpeed) requestFall(i, cause, speed / knockSpeed);
|
|
1759
|
+
else if (speed > options.shoveStagger) stagger(i);
|
|
1760
|
+
};
|
|
1761
|
+
const countsAsChainFall = (i, cause) => {
|
|
1762
|
+
if (cause === FALL_CAUSE.PRESSURE || cause === FALL_CAUSE.BLAST) return false;
|
|
1763
|
+
if (ctx.step - ctx.lastBlastStep > CHAIN_WINDOW_STEPS) return false;
|
|
1764
|
+
const direct = store.blastStep[i] === ctx.lastBlastStep ? store.directShove[i] : 0;
|
|
1765
|
+
return direct < balanceOptions().shoveStagger;
|
|
1766
|
+
};
|
|
1767
|
+
const goDown = (i, cause) => {
|
|
1768
|
+
ctx.stats.falls[CAUSE_ORDER.indexOf(cause)]++;
|
|
1769
|
+
if (countsAsChainFall(i, cause)) ctx.stats.fallsBeyondBlast++;
|
|
1770
|
+
if (store.state[i] === STATE.PILE_STEP) ctx.releasePileStep(i);
|
|
1771
|
+
store.balance[i] = BALANCE.FALLEN;
|
|
1772
|
+
store.balanceTimer[i] = 0;
|
|
1773
|
+
store.fallCause[i] = cause;
|
|
1774
|
+
store.vx[i] = store.vz[i] = 0;
|
|
1775
|
+
store.pushX[i] /= GROUND_MASS_FACTOR;
|
|
1776
|
+
store.pushZ[i] /= GROUND_MASS_FACTOR;
|
|
1777
|
+
store.aimX[i] = store.aimZ[i] = 0;
|
|
1778
|
+
const target = ctx.target();
|
|
1779
|
+
if (target && target.state.grabbedBy === i) target.release();
|
|
1780
|
+
ctx.emit("fall", { agent: i, cause, x: store.x[i], z: store.z[i], identity: store.identity[i] });
|
|
1781
|
+
};
|
|
1782
|
+
const flush = () => {
|
|
1783
|
+
if (!requests.length) return;
|
|
1784
|
+
const options = balanceOptions();
|
|
1785
|
+
requests.sort((a, b) => b.score - a.score || a.identity - b.identity);
|
|
1786
|
+
let fallen = ctx.stats.fallen;
|
|
1787
|
+
let fallsLeft = options.maxFallsPerStep;
|
|
1788
|
+
const waiting = [];
|
|
1789
|
+
for (const request of requests) {
|
|
1790
|
+
const i = request.index;
|
|
1791
|
+
if (i >= store.n || store.identity[i] !== request.identity) continue;
|
|
1792
|
+
const balance = store.balance[i];
|
|
1793
|
+
if (balance !== BALANCE.STANDING && balance !== BALANCE.STAGGERING) continue;
|
|
1794
|
+
if (fallsLeft > 0 && fallen < options.maxFallen) {
|
|
1795
|
+
goDown(i, request.cause);
|
|
1796
|
+
fallsLeft--;
|
|
1797
|
+
fallen++;
|
|
1798
|
+
continue;
|
|
1799
|
+
}
|
|
1800
|
+
stagger(i);
|
|
1801
|
+
if (isCarried(request.cause) && ctx.step - request.born < CARRY_STEPS) waiting.push(request);
|
|
1802
|
+
}
|
|
1803
|
+
requests.length = 0;
|
|
1804
|
+
requests.push(...waiting);
|
|
1805
|
+
};
|
|
1806
|
+
const hasBodyOnTop = (i) => {
|
|
1807
|
+
const cell = ctx.grid.cellOf(store.x[i], store.z[i]);
|
|
1808
|
+
if (cell < 0) return false;
|
|
1809
|
+
const options = ctx.options.mound;
|
|
1810
|
+
if (!options) return false;
|
|
1811
|
+
const top = store.y[i] + options.bodyThickness * store.build[i];
|
|
1812
|
+
return ctx.moundAt(cell) / 1e3 > top + 0.05 || ctx.weightOn(i) > 0;
|
|
1813
|
+
};
|
|
1814
|
+
const stepStanding = (i, ratio, contactShove, options) => {
|
|
1815
|
+
if (contactShove > options.shoveFall * stabilityOf(i)) return requestFall(i, FALL_CAUSE.SHOVE, contactShove / (options.shoveFall * stabilityOf(i)));
|
|
1816
|
+
if (ratio > options.staggerEnter || contactShove > options.shoveStagger) stagger(i);
|
|
1817
|
+
else tryTrip(i);
|
|
1818
|
+
};
|
|
1819
|
+
const tryTrip = (i) => {
|
|
1820
|
+
const cell = ctx.grid.cellOf(store.x[i], store.z[i]);
|
|
1821
|
+
if (cell < 0 || cell === ctx.grid.cellOf(store.prevX[i], store.prevZ[i])) return;
|
|
1822
|
+
const mound = ctx.moundAt(cell);
|
|
1823
|
+
if (mound < TRIP_MOUND || store.y[i] >= mound / 1e3 - TRIP_CLEARANCE) return;
|
|
1824
|
+
const speed = Math.hypot(store.vx[i] + store.pushX[i], store.vz[i] + store.pushZ[i]);
|
|
1825
|
+
if (speed <= TRIP_MIN_SPEED) return;
|
|
1826
|
+
const chance = (ctx.typeOf(i).balance?.trip ?? 0.04) * store.tripFactor[i] * (speed / TRIP_REFERENCE_SPEED);
|
|
1827
|
+
if (unit(store.identity[i], ctx.step, SALT.TRIP) < chance) requestFall(i, FALL_CAUSE.TRIP, 1);
|
|
1828
|
+
};
|
|
1829
|
+
const stepStaggering = (i, ratio, contactShove, options) => {
|
|
1830
|
+
store.balanceTimer[i] += ctx.h;
|
|
1831
|
+
if (contactShove > options.shoveFall * stabilityOf(i)) return requestFall(i, FALL_CAUSE.SHOVE, contactShove / (options.shoveFall * stabilityOf(i)));
|
|
1832
|
+
if (ratio > 1 && unit(store.identity[i], ctx.step, SALT.FALL) < 1 - Math.exp(-options.fallRate * (ratio - 1) ** 2 * ctx.h)) return requestFall(i, FALL_CAUSE.PRESSURE, ratio);
|
|
1833
|
+
if (ratio < options.staggerLeave && store.balanceTimer[i] >= options.staggerMin) {
|
|
1834
|
+
store.balance[i] = BALANCE.STANDING;
|
|
1835
|
+
store.balanceTimer[i] = 0;
|
|
1836
|
+
}
|
|
1837
|
+
};
|
|
1838
|
+
const stepFallen = (i, ratio, options) => {
|
|
1839
|
+
store.balanceTimer[i] += ctx.h;
|
|
1840
|
+
if (store.balanceTimer[i] < options.fallenMin + store.reaction[i] || ratio >= options.releaseFactor || hasBodyOnTop(i)) return;
|
|
1841
|
+
store.balance[i] = BALANCE.GETTING_UP;
|
|
1842
|
+
store.balanceTimer[i] = 0;
|
|
1843
|
+
};
|
|
1844
|
+
const step = (options, contactShove) => {
|
|
1845
|
+
const smoothing = 1 - Math.exp(-ctx.h / options.pressureTau);
|
|
1846
|
+
let fallen = 0;
|
|
1847
|
+
let staggering = 0;
|
|
1848
|
+
for (let i = 0; i < store.n; i++) {
|
|
1849
|
+
const state = store.balance[i];
|
|
1850
|
+
if (state === BALANCE.THROWN) continue;
|
|
1851
|
+
store.pressure[i] += (store.field[i] - store.pressure[i]) * smoothing;
|
|
1852
|
+
const ratio = store.pressure[i] / criticalOf(i);
|
|
1853
|
+
if (state === BALANCE.STANDING) stepStanding(i, ratio, contactShove[i], options);
|
|
1854
|
+
else if (state === BALANCE.STAGGERING) stepStaggering(i, ratio, contactShove[i], options);
|
|
1855
|
+
else if (state === BALANCE.FALLEN) stepFallen(i, ratio, options);
|
|
1856
|
+
else if ((store.balanceTimer[i] += ctx.h) >= options.getUpSeconds) {
|
|
1857
|
+
store.balance[i] = BALANCE.STANDING;
|
|
1858
|
+
store.balanceTimer[i] = 0;
|
|
1859
|
+
}
|
|
1860
|
+
if (store.balance[i] === BALANCE.STAGGERING) staggering++;
|
|
1861
|
+
else if (store.balance[i] === BALANCE.FALLEN || store.balance[i] === BALANCE.GETTING_UP) fallen++;
|
|
1862
|
+
}
|
|
1863
|
+
ctx.stats.fallen = fallen;
|
|
1864
|
+
ctx.stats.staggering = staggering;
|
|
1865
|
+
};
|
|
1866
|
+
const landed = (i, height) => {
|
|
1867
|
+
if (store.balance[i] !== BALANCE.STANDING && store.balance[i] !== BALANCE.STAGGERING) return;
|
|
1868
|
+
if (height > COLLAPSE_FALL) requestFall(i, FALL_CAUSE.COLLAPSE, height / COLLAPSE_FALL);
|
|
1869
|
+
else if (height > COLLAPSE_STAGGER) stagger(i);
|
|
1870
|
+
};
|
|
1871
|
+
return { requestFall, stagger, shove, step, flush, landed, pending: () => requests.length };
|
|
1872
|
+
}
|
|
1873
|
+
|
|
1874
|
+
// src/physics/blast.ts
|
|
1875
|
+
var CENTRE_HEIGHT = 1;
|
|
1876
|
+
var DISMEMBER_SHARE = 0.5;
|
|
1877
|
+
var LIMB_CHANCE = 0.5;
|
|
1878
|
+
var STUN_BASE = 0.45;
|
|
1879
|
+
var BLOOD_PER_BODY = 60;
|
|
1880
|
+
var LIMBS = [
|
|
1881
|
+
{ bit: MISSING.ARM_L, region: 2 },
|
|
1882
|
+
{ bit: MISSING.ARM_R, region: 3 },
|
|
1883
|
+
{ bit: MISSING.LEG_L, region: 4 },
|
|
1884
|
+
{ bit: MISSING.LEG_R, region: 5 }
|
|
1885
|
+
];
|
|
1886
|
+
function createBlasts(ctx, hooks) {
|
|
1887
|
+
const { store } = ctx;
|
|
1888
|
+
const options = ctx.options.blast;
|
|
1889
|
+
const queue = [];
|
|
1890
|
+
const emitLimb = (tally, i, region) => {
|
|
1891
|
+
if (tally.limbEffects++ >= options.limbEffectsPerBlast) return;
|
|
1892
|
+
ctx.emit("effect", { effect: "limb", point: [store.x[i], store.height[i] * 0.9, store.z[i]], agent: i, region, severed: true });
|
|
1893
|
+
};
|
|
1894
|
+
const emitBlood = (tally, i) => {
|
|
1895
|
+
if (tally.bloodEffects++ >= options.bloodEffectsPerBlast) {
|
|
1896
|
+
tally.bloodSum += BLOOD_PER_BODY;
|
|
1897
|
+
return;
|
|
1898
|
+
}
|
|
1899
|
+
ctx.emit("effect", { effect: "blood", point: [store.x[i], store.height[i] * 1.2, store.z[i]], agent: i, amount: BLOOD_PER_BODY, died: true });
|
|
1900
|
+
};
|
|
1901
|
+
const kill = (i, blast, distance, tally) => {
|
|
1902
|
+
tally.killed++;
|
|
1903
|
+
store.hp[i] = 0;
|
|
1904
|
+
if (blast.lethalRadius !== void 0 && distance < DISMEMBER_SHARE * blast.lethalRadius) {
|
|
1905
|
+
for (let k = 0; k < LIMBS.length; k++) {
|
|
1906
|
+
if (unit(store.identity[i], ctx.step, SALT.LIMB + k) >= LIMB_CHANCE) continue;
|
|
1907
|
+
store.missing[i] |= LIMBS[k].bit;
|
|
1908
|
+
emitLimb(tally, i, LIMBS[k].region);
|
|
1909
|
+
}
|
|
1910
|
+
}
|
|
1911
|
+
emitBlood(tally, i);
|
|
1912
|
+
ctx.deferKill(i);
|
|
1913
|
+
};
|
|
1914
|
+
const hurt = (i, blast, distance, index) => {
|
|
1915
|
+
const falloff = (1 - distance / blast.radius) ** 2;
|
|
1916
|
+
const amount = blast.energy / 1e3 * options.pointsPerKilojoule * falloff;
|
|
1917
|
+
const draw = (salt) => () => unit(store.identity[i], ctx.step, salt);
|
|
1918
|
+
if (ctx.damage(i, { region: 1, amount, distance, cut: falloff }, draw(SALT.JAW + index)).died) return true;
|
|
1919
|
+
const region = Math.floor(unit(store.identity[i], ctx.step, SALT.REGION + index) * 6);
|
|
1920
|
+
return ctx.damage(i, { region, amount: amount / 2, distance, cut: falloff }, draw(SALT.JAW + 8 + index)).died;
|
|
1921
|
+
};
|
|
1922
|
+
const shoveOf = (i, blasts) => {
|
|
1923
|
+
const position = [store.x[i], store.y[i] + CENTRE_HEIGHT * store.height[i], store.z[i]];
|
|
1924
|
+
let vx = 0;
|
|
1925
|
+
let vy = 0;
|
|
1926
|
+
let vz = 0;
|
|
1927
|
+
let falloff = 0;
|
|
1928
|
+
for (const blast of blasts) {
|
|
1929
|
+
const shove = blastShove({ point: blast.point, energy: blast.energy, radius: blast.radius }, { position, mass: store.mass[i] }, options.shove);
|
|
1930
|
+
if (!shove) continue;
|
|
1931
|
+
vx += shove.speed * shove.direction[0];
|
|
1932
|
+
vy += shove.speed * shove.direction[1];
|
|
1933
|
+
vz += shove.speed * shove.direction[2];
|
|
1934
|
+
falloff = Math.max(falloff, shove.falloff);
|
|
1935
|
+
}
|
|
1936
|
+
return vx || vy || vz ? { vx, vy, vz, falloff } : void 0;
|
|
1937
|
+
};
|
|
1938
|
+
const distanceTo = (i, blast) => Math.hypot(store.x[i] - blast.point[0], store.y[i] + CENTRE_HEIGHT * store.height[i] - blast.point[1], store.z[i] - blast.point[2]);
|
|
1939
|
+
const applyShove = (i, shove, candidates, tallies, blasts) => {
|
|
1940
|
+
const horizontal = Math.hypot(shove.vx, shove.vz);
|
|
1941
|
+
const speed = Math.hypot(horizontal, shove.vy);
|
|
1942
|
+
store.directShove[i] = horizontal;
|
|
1943
|
+
store.blastStep[i] = ctx.step;
|
|
1944
|
+
const { balance, thrown } = hooks;
|
|
1945
|
+
if (thrown && speed >= options.throwSpeed && (store.x[i] - ctx.centre.x) ** 2 + (store.z[i] - ctx.centre.z) ** 2 <= ctx.options.thrown.radius ** 2) {
|
|
1946
|
+
candidates.push({ index: i, speed, vx: shove.vx, vy: shove.vy, vz: shove.vz, distance: Math.hypot(store.x[i] - ctx.centre.x, store.z[i] - ctx.centre.z) });
|
|
1947
|
+
return;
|
|
1948
|
+
}
|
|
1949
|
+
if (store.hp[i] <= 0) return;
|
|
1950
|
+
if (!balance) {
|
|
1951
|
+
store.state[i] = STATE.STUNNED;
|
|
1952
|
+
store.stateTimer[i] = STUN_BASE + shove.falloff;
|
|
1953
|
+
store.vx[i] = shove.vx;
|
|
1954
|
+
store.vz[i] = shove.vz;
|
|
1955
|
+
return;
|
|
1956
|
+
}
|
|
1957
|
+
store.pushX[i] += shove.vx;
|
|
1958
|
+
store.pushZ[i] += shove.vz;
|
|
1959
|
+
balance.shove(i, horizontal, FALL_CAUSE.BLAST);
|
|
1960
|
+
for (let b = 0; b < blasts.length; b++) if (distanceTo(i, blasts[b]) <= blasts[b].radius && horizontal > ctx.options.balance.shoveFall) tallies[b].fallen++;
|
|
1961
|
+
};
|
|
1962
|
+
const affect = (i, blasts, tallies, candidates) => {
|
|
1963
|
+
let dead = store.hp[i] <= 0;
|
|
1964
|
+
for (let b = 0; b < blasts.length && !dead; b++) {
|
|
1965
|
+
const blast = blasts[b];
|
|
1966
|
+
const distance = distanceTo(i, blast);
|
|
1967
|
+
if (distance > blast.radius) continue;
|
|
1968
|
+
tallies[b].reached++;
|
|
1969
|
+
if (blast.lethalRadius !== void 0 && distance < blast.lethalRadius) kill(i, blast, distance, tallies[b]);
|
|
1970
|
+
else if (hurt(i, blast, distance, b)) tallies[b].killed++;
|
|
1971
|
+
dead = store.hp[i] <= 0 || dead;
|
|
1972
|
+
}
|
|
1973
|
+
const shove = shoveOf(i, blasts);
|
|
1974
|
+
if (shove) applyShove(i, shove, candidates, tallies, blasts);
|
|
1975
|
+
};
|
|
1976
|
+
const resolve = () => {
|
|
1977
|
+
if (!queue.length) return;
|
|
1978
|
+
const blasts = queue.splice(0, options.maxBlastsPerStep);
|
|
1979
|
+
const tallies = blasts.map(() => ({ reached: 0, killed: 0, fallen: 0, limbEffects: 0, bloodEffects: 0, bloodSum: 0 }));
|
|
1980
|
+
const candidates = [];
|
|
1981
|
+
ctx.lastBlastStep = ctx.step;
|
|
1982
|
+
ctx.stats.blastChecks = store.n * blasts.length;
|
|
1983
|
+
for (let i = 0; i < store.n; i++) if (store.balance[i] !== BALANCE.THROWN) affect(i, blasts, tallies, candidates);
|
|
1984
|
+
const thrown = hooks.thrown?.assign(candidates) ?? 0;
|
|
1985
|
+
blasts.forEach((blast, b) => {
|
|
1986
|
+
const tally = tallies[b];
|
|
1987
|
+
if (tally.bloodSum > 0) ctx.emit("effect", { effect: "blood", point: [blast.point[0], blast.point[1], blast.point[2]], amount: tally.bloodSum, died: true });
|
|
1988
|
+
hooks.pileBlast(blast.point[0], blast.point[2], blast.radius);
|
|
1989
|
+
ctx.emit("blasted", {
|
|
1990
|
+
point: [blast.point[0], blast.point[1], blast.point[2]],
|
|
1991
|
+
radius: blast.radius,
|
|
1992
|
+
reached: tally.reached,
|
|
1993
|
+
killed: tally.killed,
|
|
1994
|
+
thrown: b === 0 ? thrown : 0,
|
|
1995
|
+
fallen: tally.fallen,
|
|
1996
|
+
...blast.chain === void 0 ? {} : { chain: blast.chain }
|
|
1997
|
+
});
|
|
1998
|
+
if (blast.chain !== void 0) ctx.stats.chainLinks++;
|
|
1999
|
+
});
|
|
2000
|
+
};
|
|
2001
|
+
return { enqueue: (blast) => void queue.push(blast), resolve, pending: () => queue.length };
|
|
2002
|
+
}
|
|
2003
|
+
|
|
2004
|
+
// src/physics/contact.ts
|
|
2005
|
+
var GROUND_MASS_FACTOR2 = 3;
|
|
2006
|
+
var PAIR_MARGIN = 0.15;
|
|
2007
|
+
var MAX_LEVEL_GAP = 0.15;
|
|
2008
|
+
var PAIRS_PER_AGENT = 12;
|
|
2009
|
+
var MIN_DISTANCE = 1e-6;
|
|
2010
|
+
var createContactWorkspace = (capacity, grid) => ({
|
|
2011
|
+
head: new Int32Array(Math.floor(grid.size) ** 2).fill(-1),
|
|
2012
|
+
next: new Int32Array(capacity),
|
|
2013
|
+
touched: new Int32Array(capacity),
|
|
2014
|
+
touchedCount: 0,
|
|
2015
|
+
pairI: new Int32Array(capacity * PAIRS_PER_AGENT),
|
|
2016
|
+
pairJ: new Int32Array(capacity * PAIRS_PER_AGENT),
|
|
2017
|
+
pairCount: 0,
|
|
2018
|
+
deltaX: new Float32Array(capacity),
|
|
2019
|
+
deltaZ: new Float32Array(capacity),
|
|
2020
|
+
count: new Uint8Array(capacity),
|
|
2021
|
+
pushBeforeX: new Float32Array(capacity),
|
|
2022
|
+
pushBeforeZ: new Float32Array(capacity),
|
|
2023
|
+
shove: new Float32Array(capacity),
|
|
2024
|
+
takesPart: new Uint8Array(capacity),
|
|
2025
|
+
radiusOf: new Float64Array(capacity),
|
|
2026
|
+
massOf: new Float64Array(capacity),
|
|
2027
|
+
pairReach: new Float64Array(capacity * PAIRS_PER_AGENT),
|
|
2028
|
+
pairDistance: new Float64Array(capacity * PAIRS_PER_AGENT),
|
|
2029
|
+
pairNormalX: new Float64Array(capacity * PAIRS_PER_AGENT),
|
|
2030
|
+
pairNormalZ: new Float64Array(capacity * PAIRS_PER_AGENT)
|
|
2031
|
+
});
|
|
2032
|
+
var createContactView = (store, grid) => ({ n: 0, store, grid, workspace: createContactWorkspace(store.x.length, grid) });
|
|
2033
|
+
var takesPartInContact = (store, i) => store.band[i] === 0 && store.balance[i] !== BALANCE.THROWN && store.state[i] !== STATE.PILE_STEP && store.state[i] !== STATE.DORMANT;
|
|
2034
|
+
var isOnGround = (store, i) => store.balance[i] === BALANCE.FALLEN || store.balance[i] === BALANCE.GETTING_UP;
|
|
2035
|
+
var effectiveMass = (store, i) => store.mass[i] * (isOnGround(store, i) ? GROUND_MASS_FACTOR2 : 1);
|
|
2036
|
+
var bodyRadius = (store, i, radius) => radius * store.build[i];
|
|
2037
|
+
function readBodies(view, radius) {
|
|
2038
|
+
const { store, workspace } = view;
|
|
2039
|
+
const { takesPart, radiusOf, massOf } = workspace;
|
|
2040
|
+
for (let i = 0; i < view.n; i++) {
|
|
2041
|
+
const takes = takesPartInContact(store, i);
|
|
2042
|
+
takesPart[i] = takes ? 1 : 0;
|
|
2043
|
+
radiusOf[i] = bodyRadius(store, i, radius);
|
|
2044
|
+
massOf[i] = effectiveMass(store, i);
|
|
2045
|
+
}
|
|
2046
|
+
}
|
|
2047
|
+
function rebuildHash(view) {
|
|
2048
|
+
const { store, grid, workspace } = view;
|
|
2049
|
+
const side = Math.floor(grid.size);
|
|
2050
|
+
for (let k = 0; k < workspace.touchedCount; k++) workspace.head[workspace.touched[k]] = -1;
|
|
2051
|
+
workspace.touchedCount = 0;
|
|
2052
|
+
for (let i = 0; i < view.n; i++) {
|
|
2053
|
+
if (!workspace.takesPart[i]) continue;
|
|
2054
|
+
const hx = Math.floor(store.x[i] + grid.half);
|
|
2055
|
+
const hz = Math.floor(store.z[i] + grid.half);
|
|
2056
|
+
if (hx < 0 || hz < 0 || hx >= side || hz >= side) continue;
|
|
2057
|
+
const cell = hz * side + hx;
|
|
2058
|
+
if (workspace.head[cell] === -1) workspace.touched[workspace.touchedCount++] = cell;
|
|
2059
|
+
workspace.next[i] = workspace.head[cell];
|
|
2060
|
+
workspace.head[cell] = i;
|
|
2061
|
+
}
|
|
2062
|
+
}
|
|
2063
|
+
var FORWARD_CELLS = [1, 0, -1, 1, 0, 1, 1, 1];
|
|
2064
|
+
function addPairsOfCell(view, i, cell, sameCell) {
|
|
2065
|
+
const { store, workspace } = view;
|
|
2066
|
+
const { head, next, radiusOf, pairI, pairJ } = workspace;
|
|
2067
|
+
const { x, z, y } = store;
|
|
2068
|
+
const xi = x[i];
|
|
2069
|
+
const zi = z[i];
|
|
2070
|
+
const yi = y[i];
|
|
2071
|
+
const reachBase = radiusOf[i] + PAIR_MARGIN;
|
|
2072
|
+
for (let j = head[cell]; j !== -1; j = next[j]) {
|
|
2073
|
+
if (sameCell && j <= i || Math.abs(yi - y[j]) > MAX_LEVEL_GAP) continue;
|
|
2074
|
+
const reach = reachBase + radiusOf[j];
|
|
2075
|
+
const ex = x[j] - xi;
|
|
2076
|
+
const ez = z[j] - zi;
|
|
2077
|
+
if (ex * ex + ez * ez >= reach * reach || workspace.pairCount >= pairI.length) continue;
|
|
2078
|
+
pairI[workspace.pairCount] = i;
|
|
2079
|
+
pairJ[workspace.pairCount++] = j;
|
|
2080
|
+
}
|
|
2081
|
+
}
|
|
2082
|
+
function collectPairs(view) {
|
|
2083
|
+
const { store, grid, workspace } = view;
|
|
2084
|
+
const side = Math.floor(grid.size);
|
|
2085
|
+
workspace.pairCount = 0;
|
|
2086
|
+
for (let i = 0; i < view.n; i++) {
|
|
2087
|
+
if (!workspace.takesPart[i]) continue;
|
|
2088
|
+
const hx = Math.floor(store.x[i] + grid.half);
|
|
2089
|
+
const hz = Math.floor(store.z[i] + grid.half);
|
|
2090
|
+
if (hx < 0 || hz < 0 || hx >= side || hz >= side) continue;
|
|
2091
|
+
addPairsOfCell(view, i, hz * side + hx, true);
|
|
2092
|
+
for (let k = 0; k < FORWARD_CELLS.length; k += 2) {
|
|
2093
|
+
const qx = hx + FORWARD_CELLS[k];
|
|
2094
|
+
const qz = hz + FORWARD_CELLS[k + 1];
|
|
2095
|
+
if (qx >= 0 && qz >= 0 && qx < side && qz < side) addPairsOfCell(view, i, qz * side + qx, false);
|
|
2096
|
+
}
|
|
2097
|
+
}
|
|
2098
|
+
}
|
|
2099
|
+
function measurePairs(view) {
|
|
2100
|
+
const { store, workspace } = view;
|
|
2101
|
+
const { pairI, pairJ, radiusOf, pairReach, pairDistance, pairNormalX, pairNormalZ } = workspace;
|
|
2102
|
+
const { x, z, identity } = store;
|
|
2103
|
+
for (let p = 0; p < workspace.pairCount; p++) {
|
|
2104
|
+
const i = pairI[p];
|
|
2105
|
+
const j = pairJ[p];
|
|
2106
|
+
const reach = radiusOf[i] + radiusOf[j];
|
|
2107
|
+
const ex = x[i] - x[j];
|
|
2108
|
+
const ez = z[i] - z[j];
|
|
2109
|
+
const distance = Math.sqrt(ex * ex + ez * ez);
|
|
2110
|
+
pairReach[p] = reach;
|
|
2111
|
+
pairDistance[p] = distance;
|
|
2112
|
+
if (distance >= reach) continue;
|
|
2113
|
+
if (distance > MIN_DISTANCE) {
|
|
2114
|
+
pairNormalX[p] = ex / distance;
|
|
2115
|
+
pairNormalZ[p] = ez / distance;
|
|
2116
|
+
continue;
|
|
2117
|
+
}
|
|
2118
|
+
const angle = 2 * Math.PI * unit(identity[i] ^ identity[j], 0, SALT.SPIN);
|
|
2119
|
+
pairNormalX[p] = Math.cos(angle);
|
|
2120
|
+
pairNormalZ[p] = Math.sin(angle);
|
|
2121
|
+
}
|
|
2122
|
+
}
|
|
2123
|
+
function solveImpulse(store, workspace, p) {
|
|
2124
|
+
if (workspace.pairDistance[p] >= workspace.pairReach[p]) return;
|
|
2125
|
+
const i = workspace.pairI[p];
|
|
2126
|
+
const j = workspace.pairJ[p];
|
|
2127
|
+
const nx = workspace.pairNormalX[p];
|
|
2128
|
+
const nz = workspace.pairNormalZ[p];
|
|
2129
|
+
const { vx, vz, pushX, pushZ } = store;
|
|
2130
|
+
const closing = (vx[i] + pushX[i] - vx[j] - pushX[j]) * nx + (vz[i] + pushZ[i] - vz[j] - pushZ[j]) * nz;
|
|
2131
|
+
if (closing >= 0) return;
|
|
2132
|
+
const massI = workspace.massOf[i];
|
|
2133
|
+
const massJ = workspace.massOf[j];
|
|
2134
|
+
const impulse = -closing * massI * massJ / (massI + massJ);
|
|
2135
|
+
pushX[i] += impulse * nx / massI;
|
|
2136
|
+
pushZ[i] += impulse * nz / massI;
|
|
2137
|
+
pushX[j] -= impulse * nx / massJ;
|
|
2138
|
+
pushZ[j] -= impulse * nz / massJ;
|
|
2139
|
+
}
|
|
2140
|
+
function accumulateCorrection(workspace, p, relaxation) {
|
|
2141
|
+
const distance = workspace.pairDistance[p];
|
|
2142
|
+
const reach = workspace.pairReach[p];
|
|
2143
|
+
if (distance >= reach) return;
|
|
2144
|
+
const i = workspace.pairI[p];
|
|
2145
|
+
const j = workspace.pairJ[p];
|
|
2146
|
+
const massI = workspace.massOf[i];
|
|
2147
|
+
const massJ = workspace.massOf[j];
|
|
2148
|
+
const move = relaxation * (reach - distance);
|
|
2149
|
+
const nx = workspace.pairNormalX[p];
|
|
2150
|
+
const nz = workspace.pairNormalZ[p];
|
|
2151
|
+
workspace.deltaX[i] += move * nx * (massJ / (massI + massJ));
|
|
2152
|
+
workspace.deltaZ[i] += move * nz * (massJ / (massI + massJ));
|
|
2153
|
+
workspace.deltaX[j] -= move * nx * (massI / (massI + massJ));
|
|
2154
|
+
workspace.deltaZ[j] -= move * nz * (massI / (massI + massJ));
|
|
2155
|
+
workspace.count[i]++;
|
|
2156
|
+
workspace.count[j]++;
|
|
2157
|
+
}
|
|
2158
|
+
var isOpen = (grid, x, z) => {
|
|
2159
|
+
const cell = grid.cellOf(x, z);
|
|
2160
|
+
return cell >= 0 && (grid.occ[cell] === 0 || grid.occ[cell] === 3);
|
|
2161
|
+
};
|
|
2162
|
+
function applyCorrection(view, agent, params) {
|
|
2163
|
+
const { store, grid, workspace } = view;
|
|
2164
|
+
const count = workspace.count[agent];
|
|
2165
|
+
if (!count) return;
|
|
2166
|
+
workspace.count[agent] = 0;
|
|
2167
|
+
const cap = params.maxCorrection;
|
|
2168
|
+
const dx = Math.max(-cap, Math.min(cap, workspace.deltaX[agent] / count));
|
|
2169
|
+
const dz = Math.max(-cap, Math.min(cap, workspace.deltaZ[agent] / count));
|
|
2170
|
+
if (isOpen(grid, store.x[agent] + dx, store.z[agent])) store.x[agent] += dx;
|
|
2171
|
+
if (isOpen(grid, store.x[agent], store.z[agent] + dz)) store.z[agent] += dz;
|
|
2172
|
+
}
|
|
2173
|
+
function resetCorrection(workspace, agent) {
|
|
2174
|
+
workspace.deltaX[agent] = 0;
|
|
2175
|
+
workspace.deltaZ[agent] = 0;
|
|
2176
|
+
workspace.count[agent] = 0;
|
|
2177
|
+
}
|
|
2178
|
+
function correctPositions(view, params) {
|
|
2179
|
+
const { workspace } = view;
|
|
2180
|
+
for (let p = 0; p < workspace.pairCount; p++) {
|
|
2181
|
+
resetCorrection(workspace, workspace.pairI[p]);
|
|
2182
|
+
resetCorrection(workspace, workspace.pairJ[p]);
|
|
2183
|
+
}
|
|
2184
|
+
for (let p = 0; p < workspace.pairCount; p++) accumulateCorrection(workspace, p, params.relaxation);
|
|
2185
|
+
for (let p = 0; p < workspace.pairCount; p++) {
|
|
2186
|
+
applyCorrection(view, workspace.pairI[p], params);
|
|
2187
|
+
applyCorrection(view, workspace.pairJ[p], params);
|
|
2188
|
+
}
|
|
2189
|
+
}
|
|
2190
|
+
function measureOverlaps(view, out) {
|
|
2191
|
+
const { store, workspace } = view;
|
|
2192
|
+
for (let p = 0; p < workspace.pairCount; p++) {
|
|
2193
|
+
const i = workspace.pairI[p];
|
|
2194
|
+
const j = workspace.pairJ[p];
|
|
2195
|
+
if (isOnGround(store, i) && isOnGround(store, j)) continue;
|
|
2196
|
+
const reach = workspace.radiusOf[i] + workspace.radiusOf[j];
|
|
2197
|
+
const overlap = reach - Math.hypot(store.x[i] - store.x[j], store.z[i] - store.z[j]);
|
|
2198
|
+
if (overlap <= 0) continue;
|
|
2199
|
+
out.contacts++;
|
|
2200
|
+
out.overlapSum += overlap;
|
|
2201
|
+
if (overlap > out.maxOverlap) out.maxOverlap = overlap;
|
|
2202
|
+
}
|
|
2203
|
+
}
|
|
2204
|
+
function dampPush(view, params) {
|
|
2205
|
+
const { store, workspace } = view;
|
|
2206
|
+
const lambda = Math.exp(-params.friction * params.h);
|
|
2207
|
+
for (let i = 0; i < view.n; i++) {
|
|
2208
|
+
workspace.shove[i] = Math.hypot(store.pushX[i] - workspace.pushBeforeX[i], store.pushZ[i] - workspace.pushBeforeZ[i]);
|
|
2209
|
+
store.pushX[i] *= lambda;
|
|
2210
|
+
store.pushZ[i] *= lambda;
|
|
2211
|
+
const speed = Math.hypot(store.pushX[i], store.pushZ[i]);
|
|
2212
|
+
if (speed <= params.maxPush) continue;
|
|
2213
|
+
store.pushX[i] *= params.maxPush / speed;
|
|
2214
|
+
store.pushZ[i] *= params.maxPush / speed;
|
|
2215
|
+
}
|
|
2216
|
+
}
|
|
2217
|
+
function prepareHash(view, radius) {
|
|
2218
|
+
readBodies(view, radius);
|
|
2219
|
+
rebuildHash(view);
|
|
2220
|
+
}
|
|
2221
|
+
function contactStep(view, params, out) {
|
|
2222
|
+
const { store, workspace } = view;
|
|
2223
|
+
out.contacts = 0;
|
|
2224
|
+
out.maxOverlap = 0;
|
|
2225
|
+
out.overlapSum = 0;
|
|
2226
|
+
for (let i = 0; i < view.n; i++) {
|
|
2227
|
+
workspace.pushBeforeX[i] = store.pushX[i];
|
|
2228
|
+
workspace.pushBeforeZ[i] = store.pushZ[i];
|
|
2229
|
+
}
|
|
2230
|
+
readBodies(view, params.radius);
|
|
2231
|
+
rebuildHash(view);
|
|
2232
|
+
collectPairs(view);
|
|
2233
|
+
for (let iteration = 0; iteration < params.iterations; iteration++) {
|
|
2234
|
+
measurePairs(view);
|
|
2235
|
+
for (let p = 0; p < workspace.pairCount; p++) solveImpulse(store, workspace, p);
|
|
2236
|
+
for (let p = workspace.pairCount - 1; p >= 0; p--) solveImpulse(store, workspace, p);
|
|
2237
|
+
correctPositions(view, params);
|
|
2238
|
+
}
|
|
2239
|
+
measureOverlaps(view, out);
|
|
2240
|
+
dampPush(view, params);
|
|
2241
|
+
}
|
|
2242
|
+
|
|
2243
|
+
// src/physics/context.ts
|
|
2244
|
+
var createPhysicsStats = () => ({
|
|
2245
|
+
contacts: 0,
|
|
2246
|
+
maxOverlap: 0,
|
|
2247
|
+
meanOverlap: 0,
|
|
2248
|
+
falls: [0, 0, 0, 0, 0, 0],
|
|
2249
|
+
fallsBeyondBlast: 0,
|
|
2250
|
+
fallen: 0,
|
|
2251
|
+
staggering: 0,
|
|
2252
|
+
climbers: 0,
|
|
2253
|
+
moundMax: 0,
|
|
2254
|
+
thrownActive: 0,
|
|
2255
|
+
thrownPeak: 0,
|
|
2256
|
+
blastChecks: 0,
|
|
2257
|
+
chainLinks: 0,
|
|
2258
|
+
fieldMax: 0,
|
|
2259
|
+
playerPressure: 0
|
|
2260
|
+
});
|
|
2261
|
+
|
|
2262
|
+
// src/physics/density.ts
|
|
2263
|
+
var DECISION_STEPS = 12;
|
|
2264
|
+
var FAR_BAND = 1;
|
|
2265
|
+
function createDensity(ctx, options) {
|
|
2266
|
+
const { store, grid } = ctx;
|
|
2267
|
+
const side = Math.ceil(grid.size / options.cell);
|
|
2268
|
+
const bodies = new Uint16Array(side * side);
|
|
2269
|
+
const touched = [];
|
|
2270
|
+
const half = grid.half;
|
|
2271
|
+
const cellOf = (i) => {
|
|
2272
|
+
const gx = Math.floor((store.x[i] + half) / options.cell);
|
|
2273
|
+
const gz = Math.floor((store.z[i] + half) / options.cell);
|
|
2274
|
+
return gx < 0 || gz < 0 || gx >= side || gz >= side ? -1 : gz * side + gx;
|
|
2275
|
+
};
|
|
2276
|
+
const isWalking = (i) => store.band[i] === FAR_BAND && (store.balance[i] === BALANCE.STANDING || store.balance[i] === BALANCE.STAGGERING) && store.state[i] !== STATE.PILE_STEP && store.state[i] !== STATE.DORMANT;
|
|
2277
|
+
const count = () => {
|
|
2278
|
+
for (let k = 0; k < touched.length; k++) bodies[touched[k]] = 0;
|
|
2279
|
+
touched.length = 0;
|
|
2280
|
+
for (let i = 0; i < store.n; i++) {
|
|
2281
|
+
if (!isWalking(i)) continue;
|
|
2282
|
+
const cell = cellOf(i);
|
|
2283
|
+
if (cell < 0) continue;
|
|
2284
|
+
if (bodies[cell] === 0) touched.push(cell);
|
|
2285
|
+
bodies[cell]++;
|
|
2286
|
+
}
|
|
2287
|
+
};
|
|
2288
|
+
const step = () => {
|
|
2289
|
+
if (ctx.step % DECISION_STEPS === 0) count();
|
|
2290
|
+
if (!touched.length) return;
|
|
2291
|
+
for (let i = 0; i < store.n; i++) {
|
|
2292
|
+
if (!isWalking(i)) continue;
|
|
2293
|
+
const cell = cellOf(i);
|
|
2294
|
+
const around = cell < 0 ? 0 : bodies[cell];
|
|
2295
|
+
store.field[i] = around > options.capacity ? (around - options.capacity) * options.pressurePerBody : 0;
|
|
2296
|
+
}
|
|
2297
|
+
};
|
|
2298
|
+
const bodiesAround = (i) => {
|
|
2299
|
+
const cell = cellOf(i);
|
|
2300
|
+
return cell < 0 ? 0 : bodies[cell];
|
|
2301
|
+
};
|
|
2302
|
+
const slowdown = (i, band) => {
|
|
2303
|
+
if (band !== FAR_BAND) return 1;
|
|
2304
|
+
const around = bodiesAround(i);
|
|
2305
|
+
return around > options.capacity ? options.capacity / around : 1;
|
|
2306
|
+
};
|
|
2307
|
+
return { step, slowdown, bodiesAround };
|
|
2308
|
+
}
|
|
2309
|
+
|
|
2310
|
+
// src/physics/mound.ts
|
|
2311
|
+
var CELLS_PER_BODY = 3;
|
|
2312
|
+
var BODY_PART_SPACING = 0.5;
|
|
2313
|
+
var CLIMB_HEIGHT = 0.25;
|
|
2314
|
+
var ELEVATED = 0.05;
|
|
2315
|
+
var NEUTRAL_MASS = 70;
|
|
2316
|
+
var NEIGHBOURS = [-1, 1, -1, 0, -1, -1, 0, -1, 0, 1, 1, -1, 1, 0, 1, 1];
|
|
2317
|
+
var BODY_IDENTITY_SCALE = 1e6;
|
|
2318
|
+
function createMound(ctx, extraBodies, height) {
|
|
2319
|
+
const { store, grid } = ctx;
|
|
2320
|
+
const options = ctx.options.mound;
|
|
2321
|
+
const balance = ctx.options.balance;
|
|
2322
|
+
const cellCount = grid.n * grid.n;
|
|
2323
|
+
const touched = new Int32Array((store.capacity + extraBodies) * CELLS_PER_BODY * 2);
|
|
2324
|
+
let touchedCount = 0;
|
|
2325
|
+
const entryCapacity = (store.capacity + extraBodies) * CELLS_PER_BODY;
|
|
2326
|
+
const entries = { count: 0, cell: new Int32Array(entryCapacity), thickness: new Uint16Array(entryCapacity), identity: new Uint32Array(entryCapacity), agent: new Int32Array(entryCapacity) };
|
|
2327
|
+
const standMass = new Float32Array(cellCount);
|
|
2328
|
+
const standTouched = new Int32Array(store.capacity);
|
|
2329
|
+
let standTouchedCount = 0;
|
|
2330
|
+
const maxMillimetres = Math.round(options.maxHeight * 1e3);
|
|
2331
|
+
const cells = [0, 0, 0];
|
|
2332
|
+
const cellsOfBody = (x, z, yaw) => {
|
|
2333
|
+
const sin = Math.sin(yaw);
|
|
2334
|
+
const cos = Math.cos(yaw);
|
|
2335
|
+
for (let part2 = 0; part2 < CELLS_PER_BODY; part2++) {
|
|
2336
|
+
const offset = (part2 - 1) * BODY_PART_SPACING;
|
|
2337
|
+
cells[part2] = grid.cellOf(x + sin * offset, z + cos * offset);
|
|
2338
|
+
}
|
|
2339
|
+
};
|
|
2340
|
+
const addBody = (x, z, yaw, millimetres, identity, agent) => {
|
|
2341
|
+
cellsOfBody(x, z, yaw);
|
|
2342
|
+
for (const cell of cells) {
|
|
2343
|
+
if (cell < 0) continue;
|
|
2344
|
+
if (height[cell] === 0) touched[touchedCount++] = cell;
|
|
2345
|
+
height[cell] += millimetres;
|
|
2346
|
+
const index = entries.count++;
|
|
2347
|
+
entries.cell[index] = cell;
|
|
2348
|
+
entries.thickness[index] = millimetres;
|
|
2349
|
+
entries.identity[index] = identity;
|
|
2350
|
+
entries.agent[index] = agent;
|
|
2351
|
+
}
|
|
2352
|
+
};
|
|
2353
|
+
const sumBodies = () => {
|
|
2354
|
+
for (let k = 0; k < touchedCount; k++) height[touched[k]] = 0;
|
|
2355
|
+
touchedCount = 0;
|
|
2356
|
+
entries.count = 0;
|
|
2357
|
+
for (let i = 0; i < store.n; i++) {
|
|
2358
|
+
const state = store.balance[i];
|
|
2359
|
+
if (store.band[i] !== 0 || state !== BALANCE.FALLEN && state !== BALANCE.GETTING_UP) continue;
|
|
2360
|
+
addBody(store.x[i], store.z[i], store.renderYaw[i], Math.round(options.bodyThickness * store.build[i] * 1e3), store.identity[i], i);
|
|
2361
|
+
}
|
|
2362
|
+
const reach = options.corpseRadius * options.corpseRadius;
|
|
2363
|
+
for (const { data } of ctx.corpseList()) {
|
|
2364
|
+
if ((data.x - ctx.centre.x) ** 2 + (data.z - ctx.centre.z) ** 2 > reach) continue;
|
|
2365
|
+
addBody(data.x, data.z, data.yaw, Math.round(options.corpseThickness * data.build * 1e3), mix32(Math.floor(data.seed * BODY_IDENTITY_SCALE)), -1);
|
|
2366
|
+
}
|
|
2367
|
+
};
|
|
2368
|
+
const isOpenCell = (cell) => grid.occ[cell] === 0 || grid.occ[cell] === 3;
|
|
2369
|
+
const lowestNeighbour = (cell, millimetres) => {
|
|
2370
|
+
const cx = cell % grid.n;
|
|
2371
|
+
const cz = Math.floor(cell / grid.n);
|
|
2372
|
+
let best = -1;
|
|
2373
|
+
for (let k = 0; k < NEIGHBOURS.length; k += 2) {
|
|
2374
|
+
const nx = cx + NEIGHBOURS[k];
|
|
2375
|
+
const nz = cz + NEIGHBOURS[k + 1];
|
|
2376
|
+
if (nx < 0 || nz < 0 || nx >= grid.n || nz >= grid.n) continue;
|
|
2377
|
+
const neighbour = nz * grid.n + nx;
|
|
2378
|
+
if (!isOpenCell(neighbour) || height[neighbour] + millimetres > maxMillimetres) continue;
|
|
2379
|
+
if (best < 0 || height[neighbour] < height[best] || height[neighbour] === height[best] && neighbour < best) best = neighbour;
|
|
2380
|
+
}
|
|
2381
|
+
return best;
|
|
2382
|
+
};
|
|
2383
|
+
const slideEntry = (index) => {
|
|
2384
|
+
const cell = entries.cell[index];
|
|
2385
|
+
const target = lowestNeighbour(cell, entries.thickness[index]);
|
|
2386
|
+
if (target < 0) return;
|
|
2387
|
+
if (height[target] === 0) touched[touchedCount++] = target;
|
|
2388
|
+
height[target] += entries.thickness[index];
|
|
2389
|
+
height[cell] -= entries.thickness[index];
|
|
2390
|
+
entries.cell[index] = target;
|
|
2391
|
+
const agent = entries.agent[index];
|
|
2392
|
+
if (agent < 0) return;
|
|
2393
|
+
store.x[agent] += (target % grid.n - cell % grid.n) * grid.cell;
|
|
2394
|
+
store.z[agent] += (Math.floor(target / grid.n) - Math.floor(cell / grid.n)) * grid.cell;
|
|
2395
|
+
};
|
|
2396
|
+
const capHeights = () => {
|
|
2397
|
+
let over = false;
|
|
2398
|
+
for (let k = 0; k < touchedCount && !over; k++) over = height[touched[k]] > maxMillimetres;
|
|
2399
|
+
if (!over) return;
|
|
2400
|
+
const order = Array.from({ length: entries.count }, (_, index) => index).sort((a, b) => entries.cell[a] - entries.cell[b] || entries.identity[a] - entries.identity[b] || a - b);
|
|
2401
|
+
let current = -1;
|
|
2402
|
+
let running = 0;
|
|
2403
|
+
for (const index of order) {
|
|
2404
|
+
const cell = entries.cell[index];
|
|
2405
|
+
if (cell !== current) {
|
|
2406
|
+
current = cell;
|
|
2407
|
+
running = 0;
|
|
2408
|
+
}
|
|
2409
|
+
if (height[cell] <= maxMillimetres) continue;
|
|
2410
|
+
if (running + entries.thickness[index] <= maxMillimetres) running += entries.thickness[index];
|
|
2411
|
+
else slideEntry(index);
|
|
2412
|
+
}
|
|
2413
|
+
for (let k = 0; k < touchedCount; k++) if (height[touched[k]] > maxMillimetres) height[touched[k]] = maxMillimetres;
|
|
2414
|
+
};
|
|
2415
|
+
const seatFallen = () => {
|
|
2416
|
+
let top = 0;
|
|
2417
|
+
for (let k = 0; k < touchedCount; k++) top = Math.max(top, height[touched[k]]);
|
|
2418
|
+
ctx.stats.moundMax = top / 1e3;
|
|
2419
|
+
for (let i = 0; i < store.n; i++) {
|
|
2420
|
+
const state = store.balance[i];
|
|
2421
|
+
if (store.band[i] !== 0 || state !== BALANCE.FALLEN && state !== BALANCE.GETTING_UP) continue;
|
|
2422
|
+
const cell = grid.cellOf(store.x[i], store.z[i]);
|
|
2423
|
+
if (cell >= 0) store.y[i] = Math.max(0, height[cell] / 1e3 - options.bodyThickness * store.build[i]);
|
|
2424
|
+
}
|
|
2425
|
+
};
|
|
2426
|
+
const weighStanding = () => {
|
|
2427
|
+
for (let k = 0; k < standTouchedCount; k++) standMass[standTouched[k]] = 0;
|
|
2428
|
+
standTouchedCount = 0;
|
|
2429
|
+
let climbers = 0;
|
|
2430
|
+
for (let i = 0; i < store.n; i++) {
|
|
2431
|
+
const state = store.balance[i];
|
|
2432
|
+
if (store.band[i] !== 0 || store.state[i] === STATE.PILE_STEP || state !== BALANCE.STANDING && state !== BALANCE.STAGGERING) continue;
|
|
2433
|
+
if (store.y[i] >= CLIMB_HEIGHT) climbers++;
|
|
2434
|
+
if (store.y[i] <= ELEVATED) continue;
|
|
2435
|
+
const cell = grid.cellOf(store.x[i], store.z[i]);
|
|
2436
|
+
if (cell < 0) continue;
|
|
2437
|
+
if (standMass[cell] === 0) standTouched[standTouchedCount++] = cell;
|
|
2438
|
+
standMass[cell] += store.mass[i];
|
|
2439
|
+
}
|
|
2440
|
+
ctx.stats.climbers = climbers;
|
|
2441
|
+
};
|
|
2442
|
+
const trample = () => {
|
|
2443
|
+
if (!standTouchedCount) return;
|
|
2444
|
+
for (let i = 0; i < store.n; i++) {
|
|
2445
|
+
const state = store.balance[i];
|
|
2446
|
+
if (store.band[i] !== 0 || state !== BALANCE.FALLEN && state !== BALANCE.GETTING_UP) continue;
|
|
2447
|
+
const mass = weightOn(i);
|
|
2448
|
+
if (!mass) continue;
|
|
2449
|
+
store.hp[i] -= balance.trampleDps * (mass / NEUTRAL_MASS) * ctx.h;
|
|
2450
|
+
if (store.hp[i] <= 0) ctx.deferKill(i);
|
|
2451
|
+
}
|
|
2452
|
+
};
|
|
2453
|
+
const step = () => {
|
|
2454
|
+
sumBodies();
|
|
2455
|
+
capHeights();
|
|
2456
|
+
seatFallen();
|
|
2457
|
+
weighStanding();
|
|
2458
|
+
trample();
|
|
2459
|
+
};
|
|
2460
|
+
const weightOn = (i) => {
|
|
2461
|
+
cellsOfBody(store.x[i], store.z[i], store.renderYaw[i]);
|
|
2462
|
+
let mass = 0;
|
|
2463
|
+
for (const cell of cells) if (cell >= 0) mass += standMass[cell];
|
|
2464
|
+
return mass;
|
|
2465
|
+
};
|
|
2466
|
+
return { height, step, weightOn };
|
|
2467
|
+
}
|
|
2468
|
+
|
|
2469
|
+
// src/physics/offload/default-worker.ts
|
|
2470
|
+
var import_meta = {};
|
|
2471
|
+
function createDefaultWorkerFactory() {
|
|
2472
|
+
if (typeof Worker === "undefined") return null;
|
|
2473
|
+
let url;
|
|
2474
|
+
try {
|
|
2475
|
+
url = new URL("./physics-worker.js", import_meta.url);
|
|
2476
|
+
} catch {
|
|
2477
|
+
return null;
|
|
2478
|
+
}
|
|
2479
|
+
return () => {
|
|
2480
|
+
const worker = new Worker(url, { type: "module" });
|
|
2481
|
+
return {
|
|
2482
|
+
postMessage: (message, transfer) => worker.postMessage(message, transfer ?? []),
|
|
2483
|
+
onMessage: (listener) => worker.addEventListener("message", (event) => listener(event.data)),
|
|
2484
|
+
terminate: () => worker.terminate()
|
|
2485
|
+
};
|
|
2486
|
+
};
|
|
2487
|
+
}
|
|
2488
|
+
|
|
2489
|
+
// src/physics/offload/frame.ts
|
|
2490
|
+
var F32_INPUTS = ["x", "z", "y", "vx", "vz", "pushX", "pushZ", "mass", "build", "aimX", "aimZ", "driveFactor", "fieldPrev"];
|
|
2491
|
+
var U8_INPUTS = ["band", "balance", "state", "type"];
|
|
2492
|
+
var F32_OUTPUTS = ["deltaX", "deltaZ", "deltaPushX", "deltaPushZ", "deltaVx", "deltaVz", "field", "shove", "doorLoad"];
|
|
2493
|
+
var HEADER = { n: 0, h: 1, playerOn: 2, playerX: 3, playerZ: 4, contacts: 5, maxOverlap: 6, overlapSum: 7, playerLoad: 8, drivers: 9, fieldMax: 10, size: 16 };
|
|
2494
|
+
var align8 = (bytes) => Math.ceil(bytes / 8) * 8;
|
|
2495
|
+
var frameBytes = ({ capacity, cells }) => HEADER.size * 8 + (F32_INPUTS.length + F32_OUTPUTS.length + 2) * align8(capacity * 4) + U8_INPUTS.length * align8(capacity) + align8(cells);
|
|
2496
|
+
function createFrame(buffer, shape) {
|
|
2497
|
+
const { capacity, cells } = shape;
|
|
2498
|
+
let offset = 0;
|
|
2499
|
+
const take = (make, bytesPerElement, length) => {
|
|
2500
|
+
const view = make(buffer, offset, length);
|
|
2501
|
+
offset += align8(length * bytesPerElement);
|
|
2502
|
+
return view;
|
|
2503
|
+
};
|
|
2504
|
+
const f64 = (b, o, l) => new Float64Array(b, o, l);
|
|
2505
|
+
const f32 = (b, o, l) => new Float32Array(b, o, l);
|
|
2506
|
+
const u8 = (b, o, l) => new Uint8Array(b, o, l);
|
|
2507
|
+
const header = take(f64, 8, HEADER.size);
|
|
2508
|
+
const floats = {};
|
|
2509
|
+
for (const key of [...F32_INPUTS, ...F32_OUTPUTS]) floats[key] = take(f32, 4, capacity);
|
|
2510
|
+
const identity = take((b, o, l) => new Uint32Array(b, o, l), 4, capacity);
|
|
2511
|
+
const doorCell = take((b, o, l) => new Int32Array(b, o, l), 4, capacity);
|
|
2512
|
+
const bytes = {};
|
|
2513
|
+
for (const key of U8_INPUTS) bytes[key] = take(u8, 1, capacity);
|
|
2514
|
+
const occ = take(u8, 1, cells);
|
|
2515
|
+
return { header, f32: floats, u8: bytes, identity, doorCell, occ };
|
|
2516
|
+
}
|
|
2517
|
+
var INPUT_KEYS = { f32: F32_INPUTS, u8: U8_INPUTS };
|
|
2518
|
+
|
|
2519
|
+
// src/physics/offload/offload.ts
|
|
2520
|
+
var isOpen2 = (grid, x, z) => {
|
|
2521
|
+
const cell = grid.cellOf(x, z);
|
|
2522
|
+
return cell >= 0 && (grid.occ[cell] === 0 || grid.occ[cell] === 3);
|
|
2523
|
+
};
|
|
2524
|
+
function writeSnapshot(frame, source, h, player) {
|
|
2525
|
+
const { store, grid } = source;
|
|
2526
|
+
const n = store.n;
|
|
2527
|
+
for (const key of INPUT_KEYS.f32) frame.f32[key].set(store[key].subarray(0, n));
|
|
2528
|
+
for (const key of INPUT_KEYS.u8) frame.u8[key].set(store[key].subarray(0, n));
|
|
2529
|
+
frame.identity.set(store.identity.subarray(0, n));
|
|
2530
|
+
frame.occ.set(grid.occ);
|
|
2531
|
+
frame.header[HEADER.n] = n;
|
|
2532
|
+
frame.header[HEADER.h] = h;
|
|
2533
|
+
frame.header[HEADER.playerOn] = player ? 1 : 0;
|
|
2534
|
+
frame.header[HEADER.playerX] = player?.x ?? 0;
|
|
2535
|
+
frame.header[HEADER.playerZ] = player?.z ?? 0;
|
|
2536
|
+
}
|
|
2537
|
+
function createOffload(runner, host, latency = 1) {
|
|
2538
|
+
const { store, grid, stats, contact } = host;
|
|
2539
|
+
const queue = [];
|
|
2540
|
+
const spare = Array.from({ length: latency }, () => ({ count: 0, origin: null, originBuffer: new Int32Array(store.capacity) }));
|
|
2541
|
+
const result = { playerLoad: 0 };
|
|
2542
|
+
const apply = (frame, pending) => {
|
|
2543
|
+
const { f32, header } = frame;
|
|
2544
|
+
const { origin, count } = pending;
|
|
2545
|
+
for (let agent = 0; agent < store.n; agent++) {
|
|
2546
|
+
const from = origin ? origin[agent] : agent < count ? agent : -1;
|
|
2547
|
+
if (from < 0) {
|
|
2548
|
+
contact.workspace.shove[agent] = 0;
|
|
2549
|
+
continue;
|
|
2550
|
+
}
|
|
2551
|
+
const dx = f32.deltaX[from];
|
|
2552
|
+
const dz = f32.deltaZ[from];
|
|
2553
|
+
if (dx !== 0 && isOpen2(grid, store.x[agent] + dx, store.z[agent])) store.x[agent] += dx;
|
|
2554
|
+
if (dz !== 0 && isOpen2(grid, store.x[agent], store.z[agent] + dz)) store.z[agent] += dz;
|
|
2555
|
+
store.pushX[agent] += f32.deltaPushX[from];
|
|
2556
|
+
store.pushZ[agent] += f32.deltaPushZ[from];
|
|
2557
|
+
store.vx[agent] += f32.deltaVx[from];
|
|
2558
|
+
store.vz[agent] += f32.deltaVz[from];
|
|
2559
|
+
store.field[agent] = f32.field[from];
|
|
2560
|
+
store.fieldPrev[agent] = f32.field[from];
|
|
2561
|
+
contact.workspace.shove[agent] = f32.shove[from];
|
|
2562
|
+
const cell = frame.doorCell[from];
|
|
2563
|
+
if (cell >= 0) host.addDoorLoad(cell, f32.doorLoad[from]);
|
|
2564
|
+
}
|
|
2565
|
+
stats.contacts = header[HEADER.contacts];
|
|
2566
|
+
stats.maxOverlap = header[HEADER.maxOverlap];
|
|
2567
|
+
stats.meanOverlap = header[HEADER.contacts] ? header[HEADER.overlapSum] / header[HEADER.contacts] : 0;
|
|
2568
|
+
stats.fieldMax = header[HEADER.fieldMax];
|
|
2569
|
+
result.playerLoad = header[HEADER.playerLoad];
|
|
2570
|
+
};
|
|
2571
|
+
const step = (h, player) => {
|
|
2572
|
+
result.playerLoad = 0;
|
|
2573
|
+
if (queue.length >= latency) {
|
|
2574
|
+
const frame2 = runner.settle();
|
|
2575
|
+
if (frame2) {
|
|
2576
|
+
const pending = queue.shift();
|
|
2577
|
+
apply(frame2, pending);
|
|
2578
|
+
pending.origin = null;
|
|
2579
|
+
spare.push(pending);
|
|
2580
|
+
}
|
|
2581
|
+
}
|
|
2582
|
+
const frame = runner.frame();
|
|
2583
|
+
if (frame && spare.length) {
|
|
2584
|
+
writeSnapshot(frame, { store, grid }, h, player);
|
|
2585
|
+
runner.submit();
|
|
2586
|
+
const pending = spare.shift();
|
|
2587
|
+
pending.count = store.n;
|
|
2588
|
+
queue.push(pending);
|
|
2589
|
+
}
|
|
2590
|
+
return result;
|
|
2591
|
+
};
|
|
2592
|
+
const remap = (removed, moved) => {
|
|
2593
|
+
queue.forEach((pending) => {
|
|
2594
|
+
if (!pending.origin) {
|
|
2595
|
+
pending.origin = pending.originBuffer.fill(-1);
|
|
2596
|
+
for (let agent = 0; agent < pending.count; agent++) pending.origin[agent] = agent;
|
|
2597
|
+
}
|
|
2598
|
+
pending.origin[removed] = removed === moved ? -1 : pending.origin[moved];
|
|
2599
|
+
if (removed !== moved) pending.origin[moved] = -1;
|
|
2600
|
+
});
|
|
2601
|
+
};
|
|
2602
|
+
return { step, remap, dispose: () => runner.dispose() };
|
|
2603
|
+
}
|
|
2604
|
+
|
|
2605
|
+
// src/physics/pressure.ts
|
|
2606
|
+
var TOUCH_SLACK = 0.08;
|
|
2607
|
+
var WALL_PROBE = 0.12;
|
|
2608
|
+
var PLAYER_REACH = 1.3;
|
|
2609
|
+
var PLAYER_RADIUS_SQUARED = PLAYER_REACH * PLAYER_REACH;
|
|
2610
|
+
var createPressureWorkspace = (capacity, pairCapacity) => ({
|
|
2611
|
+
weightSum: new Float32Array(capacity),
|
|
2612
|
+
pressing: new Uint8Array(capacity),
|
|
2613
|
+
wallKind: new Uint8Array(capacity),
|
|
2614
|
+
wallDoor: new Int32Array(capacity),
|
|
2615
|
+
playerWeight: new Float32Array(capacity),
|
|
2616
|
+
cutExcess: new Float32Array(capacity),
|
|
2617
|
+
cutX: new Float32Array(capacity),
|
|
2618
|
+
cutZ: new Float32Array(capacity),
|
|
2619
|
+
pairWeightForward: new Float32Array(pairCapacity),
|
|
2620
|
+
pairWeightBackward: new Float32Array(pairCapacity),
|
|
2621
|
+
canPress: new Uint8Array(capacity)
|
|
2622
|
+
});
|
|
2623
|
+
var isPressing = (store, i) => takesPartInContact(store, i) && (store.balance[i] === BALANCE.STANDING || store.balance[i] === BALANCE.STAGGERING);
|
|
2624
|
+
function readPressing(view, workspace) {
|
|
2625
|
+
for (let i = 0; i < view.n; i++) workspace.canPress[i] = isPressing(view.store, i) ? 1 : 0;
|
|
2626
|
+
}
|
|
2627
|
+
var frontWeight = (alignment, cone) => alignment > cone ? (alignment - cone) / (1 - cone) : 0;
|
|
2628
|
+
var hasAim = (store, i) => store.aimX[i] !== 0 || store.aimZ[i] !== 0;
|
|
2629
|
+
function noteCut(store, workspace, i, j, nx, nz) {
|
|
2630
|
+
const excess = (store.vx[i] - store.vx[j]) * nx + (store.vz[i] - store.vz[j]) * nz;
|
|
2631
|
+
if (excess <= workspace.cutExcess[i]) return;
|
|
2632
|
+
workspace.cutExcess[i] = excess;
|
|
2633
|
+
workspace.cutX[i] = nx;
|
|
2634
|
+
workspace.cutZ[i] = nz;
|
|
2635
|
+
}
|
|
2636
|
+
function weighPairs(view, workspace, params) {
|
|
2637
|
+
const { store } = view;
|
|
2638
|
+
for (let p = 0; p < view.workspace.pairCount; p++) {
|
|
2639
|
+
workspace.pairWeightForward[p] = 0;
|
|
2640
|
+
workspace.pairWeightBackward[p] = 0;
|
|
2641
|
+
const i = view.workspace.pairI[p];
|
|
2642
|
+
const j = view.workspace.pairJ[p];
|
|
2643
|
+
if (!workspace.canPress[i] || !workspace.canPress[j]) continue;
|
|
2644
|
+
const ex = store.x[j] - store.x[i];
|
|
2645
|
+
const ez = store.z[j] - store.z[i];
|
|
2646
|
+
const distance = Math.sqrt(ex * ex + ez * ez);
|
|
2647
|
+
if (distance < 1e-6 || distance > (store.build[i] + store.build[j]) * params.radius + TOUCH_SLACK) continue;
|
|
2648
|
+
const nx = ex / distance;
|
|
2649
|
+
const nz = ez / distance;
|
|
2650
|
+
const forward = frontWeight(store.aimX[i] * nx + store.aimZ[i] * nz, params.frontCone);
|
|
2651
|
+
const backward = frontWeight(-(store.aimX[j] * nx + store.aimZ[j] * nz), params.frontCone);
|
|
2652
|
+
if (forward > 0) {
|
|
2653
|
+
workspace.pressing[i] = 1;
|
|
2654
|
+
noteCut(store, workspace, i, j, nx, nz);
|
|
2655
|
+
}
|
|
2656
|
+
if (backward > 0) {
|
|
2657
|
+
workspace.pressing[j] = 1;
|
|
2658
|
+
noteCut(store, workspace, j, i, -nx, -nz);
|
|
2659
|
+
}
|
|
2660
|
+
if (forward > 0 && backward > 0) continue;
|
|
2661
|
+
workspace.pairWeightForward[p] = forward;
|
|
2662
|
+
workspace.pairWeightBackward[p] = backward;
|
|
2663
|
+
workspace.weightSum[i] += forward;
|
|
2664
|
+
workspace.weightSum[j] += backward;
|
|
2665
|
+
}
|
|
2666
|
+
}
|
|
2667
|
+
function weighWalls(view, workspace, params, doorAt) {
|
|
2668
|
+
const { store, grid } = view;
|
|
2669
|
+
for (let i = 0; i < view.n; i++) {
|
|
2670
|
+
if (!workspace.canPress[i] || !hasAim(store, i)) continue;
|
|
2671
|
+
const reach = store.build[i] * params.radius + WALL_PROBE;
|
|
2672
|
+
const cell = grid.cellOf(store.x[i] + store.aimX[i] * reach, store.z[i] + store.aimZ[i] * reach);
|
|
2673
|
+
if (cell >= 0 && (grid.occ[cell] === 0 || grid.occ[cell] === 3)) continue;
|
|
2674
|
+
workspace.wallKind[i] = 1;
|
|
2675
|
+
workspace.wallDoor[i] = cell >= 0 && grid.occ[cell] === 5 ? doorAt(cell) : -1;
|
|
2676
|
+
workspace.pressing[i] = 1;
|
|
2677
|
+
workspace.weightSum[i] += 1;
|
|
2678
|
+
}
|
|
2679
|
+
}
|
|
2680
|
+
function weighPlayer(view, workspace, params, player) {
|
|
2681
|
+
const { store } = view;
|
|
2682
|
+
for (let i = 0; i < view.n; i++) {
|
|
2683
|
+
if (!workspace.canPress[i]) continue;
|
|
2684
|
+
const ex = player.x - store.x[i];
|
|
2685
|
+
const ez = player.z - store.z[i];
|
|
2686
|
+
const squared = ex * ex + ez * ez;
|
|
2687
|
+
if (squared > PLAYER_RADIUS_SQUARED || squared < 1e-6) continue;
|
|
2688
|
+
const distance = Math.sqrt(squared);
|
|
2689
|
+
const weight = hasAim(store, i) ? frontWeight((store.aimX[i] * ex + store.aimZ[i] * ez) / distance, params.frontCone) : 1;
|
|
2690
|
+
workspace.playerWeight[i] = weight;
|
|
2691
|
+
if (weight > 0) workspace.pressing[i] = 1;
|
|
2692
|
+
workspace.weightSum[i] += weight;
|
|
2693
|
+
}
|
|
2694
|
+
}
|
|
2695
|
+
function clearWorkspace(view, workspace) {
|
|
2696
|
+
const { store } = view;
|
|
2697
|
+
for (let i = 0; i < view.n; i++) {
|
|
2698
|
+
workspace.weightSum[i] = 0;
|
|
2699
|
+
workspace.pressing[i] = 0;
|
|
2700
|
+
workspace.wallKind[i] = 0;
|
|
2701
|
+
workspace.playerWeight[i] = 0;
|
|
2702
|
+
workspace.cutExcess[i] = 0;
|
|
2703
|
+
store.field[i] = 0;
|
|
2704
|
+
}
|
|
2705
|
+
}
|
|
2706
|
+
function ownDrive(store, context, workspace, i) {
|
|
2707
|
+
if (!hasAim(store, i) || !workspace.pressing[i]) return 0;
|
|
2708
|
+
return context.driveForceOfType[store.type[i]] * store.driveFactor[i];
|
|
2709
|
+
}
|
|
2710
|
+
function spreadField(view, workspace, params, context, out) {
|
|
2711
|
+
const { store } = view;
|
|
2712
|
+
for (let i = 0; i < view.n; i++) {
|
|
2713
|
+
if (!workspace.canPress[i]) continue;
|
|
2714
|
+
store.field[i] = ownDrive(store, context, workspace, i);
|
|
2715
|
+
if (store.field[i] > 0) out.drivers++;
|
|
2716
|
+
}
|
|
2717
|
+
for (let p = 0; p < view.workspace.pairCount; p++) {
|
|
2718
|
+
const i = view.workspace.pairI[p];
|
|
2719
|
+
const j = view.workspace.pairJ[p];
|
|
2720
|
+
const forward = workspace.pairWeightForward[p];
|
|
2721
|
+
const backward = workspace.pairWeightBackward[p];
|
|
2722
|
+
if (forward > 0) store.field[j] += params.transmission * store.fieldPrev[i] * forward * forward / workspace.weightSum[i];
|
|
2723
|
+
if (backward > 0) store.field[i] += params.transmission * store.fieldPrev[j] * backward * backward / workspace.weightSum[j];
|
|
2724
|
+
}
|
|
2725
|
+
for (let i = 0; i < view.n; i++) {
|
|
2726
|
+
if (!workspace.canPress[i]) continue;
|
|
2727
|
+
const passed = params.transmission * store.fieldPrev[i] / (workspace.weightSum[i] || 1);
|
|
2728
|
+
if (workspace.wallKind[i] && workspace.wallDoor[i] >= 0) {
|
|
2729
|
+
if (context.doorOut) {
|
|
2730
|
+
context.doorOut.cell[i] = workspace.wallDoor[i];
|
|
2731
|
+
context.doorOut.load[i] = passed * params.h;
|
|
2732
|
+
} else context.doorLoad[workspace.wallDoor[i]] += passed * params.h;
|
|
2733
|
+
}
|
|
2734
|
+
if (workspace.playerWeight[i] > 0) out.playerLoad += passed * workspace.playerWeight[i] * workspace.playerWeight[i];
|
|
2735
|
+
if (store.field[i] > params.cap) store.field[i] = params.cap;
|
|
2736
|
+
}
|
|
2737
|
+
}
|
|
2738
|
+
function applyCuts(view, workspace) {
|
|
2739
|
+
const { store } = view;
|
|
2740
|
+
for (let i = 0; i < view.n; i++) {
|
|
2741
|
+
const excess = workspace.cutExcess[i];
|
|
2742
|
+
if (excess <= 0) continue;
|
|
2743
|
+
store.vx[i] -= excess * workspace.cutX[i];
|
|
2744
|
+
store.vz[i] -= excess * workspace.cutZ[i];
|
|
2745
|
+
}
|
|
2746
|
+
}
|
|
2747
|
+
function pressureStep(view, workspace, params, context, out) {
|
|
2748
|
+
out.playerLoad = 0;
|
|
2749
|
+
out.drivers = 0;
|
|
2750
|
+
clearWorkspace(view, workspace);
|
|
2751
|
+
readPressing(view, workspace);
|
|
2752
|
+
weighPairs(view, workspace, params);
|
|
2753
|
+
weighWalls(view, workspace, params, context.doorAt);
|
|
2754
|
+
if (context.player) weighPlayer(view, workspace, params, context.player);
|
|
2755
|
+
spreadField(view, workspace, params, context, out);
|
|
2756
|
+
applyCuts(view, workspace);
|
|
2757
|
+
for (let i = 0; i < view.n; i++) view.store.fieldPrev[i] = view.store.field[i];
|
|
2758
|
+
}
|
|
2759
|
+
|
|
2760
|
+
// src/physics/offload/kernel.ts
|
|
2761
|
+
var PAIRS_PER_AGENT2 = 12;
|
|
2762
|
+
var STORE_KEYS = ["x", "z", "y", "vx", "vz", "pushX", "pushZ", "mass", "build", "aimX", "aimZ", "driveFactor", "fieldPrev"];
|
|
2763
|
+
function createKernel(setup) {
|
|
2764
|
+
const { capacity } = setup;
|
|
2765
|
+
const side = Math.floor(setup.gridSize / setup.gridCell);
|
|
2766
|
+
const half = setup.gridSize / 2;
|
|
2767
|
+
const before = { x: new Float32Array(capacity), z: new Float32Array(capacity), pushX: new Float32Array(capacity), pushZ: new Float32Array(capacity), vx: new Float32Array(capacity), vz: new Float32Array(capacity) };
|
|
2768
|
+
const driveForceOfType = Float32Array.from(setup.driveForceOfType);
|
|
2769
|
+
const contactResult = { contacts: 0, maxOverlap: 0, overlapSum: 0 };
|
|
2770
|
+
const pressureResult = { playerLoad: 0, drivers: 0 };
|
|
2771
|
+
const workspace = setup.pressure ? createPressureWorkspace(capacity, capacity * PAIRS_PER_AGENT2) : null;
|
|
2772
|
+
const cellOf = createCellOf(half, setup.gridCell, side);
|
|
2773
|
+
const doorLoadUnused = new Float32Array(0);
|
|
2774
|
+
const store = {};
|
|
2775
|
+
const grid = { size: setup.gridSize, half, occ: new Uint8Array(0), cellOf };
|
|
2776
|
+
const view = { n: 0, store, grid, workspace: createContactWorkspace(capacity, grid) };
|
|
2777
|
+
const bind = (frame) => {
|
|
2778
|
+
Object.assign(store, frame.u8, { identity: frame.identity, field: frame.f32.field });
|
|
2779
|
+
for (const key of STORE_KEYS) store[key] = frame.f32[key];
|
|
2780
|
+
grid.occ = frame.occ;
|
|
2781
|
+
return view;
|
|
2782
|
+
};
|
|
2783
|
+
const run = (frame) => {
|
|
2784
|
+
const { header } = frame;
|
|
2785
|
+
const n = header[HEADER.n];
|
|
2786
|
+
const contact = bind(frame);
|
|
2787
|
+
contact.n = n;
|
|
2788
|
+
for (let i = 0; i < n; i++) {
|
|
2789
|
+
before.x[i] = frame.f32.x[i];
|
|
2790
|
+
before.z[i] = frame.f32.z[i];
|
|
2791
|
+
before.pushX[i] = frame.f32.pushX[i];
|
|
2792
|
+
before.pushZ[i] = frame.f32.pushZ[i];
|
|
2793
|
+
before.vx[i] = frame.f32.vx[i];
|
|
2794
|
+
before.vz[i] = frame.f32.vz[i];
|
|
2795
|
+
}
|
|
2796
|
+
const h = header[HEADER.h];
|
|
2797
|
+
const params = { ...setup.contact, h };
|
|
2798
|
+
contactStep(contact, params, contactResult);
|
|
2799
|
+
header[HEADER.contacts] = contactResult.contacts;
|
|
2800
|
+
header[HEADER.maxOverlap] = contactResult.maxOverlap;
|
|
2801
|
+
header[HEADER.overlapSum] = contactResult.overlapSum;
|
|
2802
|
+
frame.doorCell.fill(-1, 0, n);
|
|
2803
|
+
frame.f32.doorLoad.fill(0, 0, n);
|
|
2804
|
+
if (workspace && setup.pressure) {
|
|
2805
|
+
const player = header[HEADER.playerOn] ? { x: header[HEADER.playerX], z: header[HEADER.playerZ] } : void 0;
|
|
2806
|
+
const doorOut = { cell: frame.doorCell, load: frame.f32.doorLoad };
|
|
2807
|
+
pressureStep(contact, workspace, { ...setup.pressure, h, radius: setup.contact.radius }, { driveForceOfType, doorAt: (cell) => cell, doorLoad: doorLoadUnused, player, doorOut }, pressureResult);
|
|
2808
|
+
let top = 0;
|
|
2809
|
+
for (let i = 0; i < n; i++) top = Math.max(top, frame.f32.field[i]);
|
|
2810
|
+
header[HEADER.fieldMax] = top;
|
|
2811
|
+
} else header[HEADER.fieldMax] = 0;
|
|
2812
|
+
header[HEADER.playerLoad] = pressureResult.playerLoad;
|
|
2813
|
+
header[HEADER.drivers] = pressureResult.drivers;
|
|
2814
|
+
const { f32 } = frame;
|
|
2815
|
+
for (let i = 0; i < n; i++) {
|
|
2816
|
+
f32.deltaX[i] = f32.x[i] - before.x[i];
|
|
2817
|
+
f32.deltaZ[i] = f32.z[i] - before.z[i];
|
|
2818
|
+
f32.deltaPushX[i] = f32.pushX[i] - before.pushX[i];
|
|
2819
|
+
f32.deltaPushZ[i] = f32.pushZ[i] - before.pushZ[i];
|
|
2820
|
+
f32.deltaVx[i] = f32.vx[i] - before.vx[i];
|
|
2821
|
+
f32.deltaVz[i] = f32.vz[i] - before.vz[i];
|
|
2822
|
+
f32.shove[i] = contact.workspace.shove[i];
|
|
2823
|
+
}
|
|
2824
|
+
};
|
|
2825
|
+
return { run };
|
|
2826
|
+
}
|
|
2827
|
+
|
|
2828
|
+
// src/physics/offload/worker-host.ts
|
|
2829
|
+
var CONTROL = { IDLE: 0, SUBMITTED: 1, DONE: 2, STOP: 3 };
|
|
2830
|
+
function attachPhysicsWorker(port) {
|
|
2831
|
+
let kernel = null;
|
|
2832
|
+
let shape = null;
|
|
2833
|
+
port.onMessage((data) => {
|
|
2834
|
+
const message = data;
|
|
2835
|
+
if (message.type === "init") {
|
|
2836
|
+
kernel = createKernel(message.setup);
|
|
2837
|
+
shape = message.shape;
|
|
2838
|
+
if (message.frames && message.control) runShared(kernel, message.frames.map((buffer) => createFrame(buffer, message.shape)), new Int32Array(message.control));
|
|
2839
|
+
return;
|
|
2840
|
+
}
|
|
2841
|
+
if (!kernel || !shape) return;
|
|
2842
|
+
const frame = createFrame(message.buffer, shape);
|
|
2843
|
+
kernel.run(frame);
|
|
2844
|
+
port.postMessage({ type: "done", buffer: message.buffer }, [message.buffer]);
|
|
2845
|
+
});
|
|
2846
|
+
}
|
|
2847
|
+
function runShared(kernel, frames, control) {
|
|
2848
|
+
for (let turn = 0; ; turn++) {
|
|
2849
|
+
const slot = turn % frames.length;
|
|
2850
|
+
for (; ; ) {
|
|
2851
|
+
const state = Atomics.load(control, slot);
|
|
2852
|
+
if (state === CONTROL.STOP) return;
|
|
2853
|
+
if (state === CONTROL.SUBMITTED) break;
|
|
2854
|
+
Atomics.wait(control, slot, state);
|
|
2855
|
+
}
|
|
2856
|
+
kernel.run(frames[slot]);
|
|
2857
|
+
Atomics.store(control, slot, CONTROL.DONE);
|
|
2858
|
+
Atomics.notify(control, slot);
|
|
2859
|
+
}
|
|
2860
|
+
}
|
|
2861
|
+
|
|
2862
|
+
// src/physics/offload/runner.ts
|
|
2863
|
+
var WAIT_LIMIT_MS = 5e3;
|
|
2864
|
+
var canBlock = (() => {
|
|
2865
|
+
try {
|
|
2866
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 1, 0);
|
|
2867
|
+
return true;
|
|
2868
|
+
} catch {
|
|
2869
|
+
return false;
|
|
2870
|
+
}
|
|
2871
|
+
})();
|
|
2872
|
+
var isSharedAvailable = () => typeof SharedArrayBuffer !== "undefined" && globalThis.crossOriginIsolated !== false;
|
|
2873
|
+
function createInlineRunner(setup, shape, slots = 1) {
|
|
2874
|
+
const kernel = createKernel(setup);
|
|
2875
|
+
const frames = Array.from({ length: slots }, () => createFrame(new ArrayBuffer(frameBytes(shape)), shape));
|
|
2876
|
+
let submitted = 0;
|
|
2877
|
+
let settled = 0;
|
|
2878
|
+
const runner = {
|
|
2879
|
+
transport: "inline",
|
|
2880
|
+
get inFlight() {
|
|
2881
|
+
return submitted - settled;
|
|
2882
|
+
},
|
|
2883
|
+
frame: () => submitted - settled < slots ? frames[submitted % slots] : null,
|
|
2884
|
+
submit: () => kernel.run(frames[submitted++ % slots]),
|
|
2885
|
+
settle: () => submitted > settled ? frames[settled++ % slots] : null,
|
|
2886
|
+
dispose: () => void 0
|
|
2887
|
+
};
|
|
2888
|
+
return runner;
|
|
2889
|
+
}
|
|
2890
|
+
function createSharedRunner(setup, shape, factory, slots = 1) {
|
|
2891
|
+
const buffers = Array.from({ length: slots }, () => new SharedArrayBuffer(frameBytes(shape)));
|
|
2892
|
+
const control = new Int32Array(new SharedArrayBuffer(4 * slots));
|
|
2893
|
+
const frames = buffers.map((buffer) => createFrame(buffer, shape));
|
|
2894
|
+
const worker = factory();
|
|
2895
|
+
worker.postMessage({ type: "init", setup, shape, frames: buffers, control: control.buffer });
|
|
2896
|
+
let submitted = 0;
|
|
2897
|
+
let settled = 0;
|
|
2898
|
+
return {
|
|
2899
|
+
transport: "shared",
|
|
2900
|
+
get inFlight() {
|
|
2901
|
+
return submitted - settled;
|
|
2902
|
+
},
|
|
2903
|
+
frame: () => submitted - settled < slots ? frames[submitted % slots] : null,
|
|
2904
|
+
submit: () => {
|
|
2905
|
+
const slot = submitted++ % slots;
|
|
2906
|
+
Atomics.store(control, slot, CONTROL.SUBMITTED);
|
|
2907
|
+
Atomics.notify(control, slot);
|
|
2908
|
+
},
|
|
2909
|
+
settle: () => {
|
|
2910
|
+
if (submitted === settled) return null;
|
|
2911
|
+
const slot = settled++ % slots;
|
|
2912
|
+
const started = Date.now();
|
|
2913
|
+
while (Atomics.load(control, slot) !== CONTROL.DONE) {
|
|
2914
|
+
if (Date.now() - started > WAIT_LIMIT_MS) throw new Error("crowd physics worker did not answer");
|
|
2915
|
+
if (canBlock) Atomics.wait(control, slot, CONTROL.SUBMITTED, 50);
|
|
2916
|
+
}
|
|
2917
|
+
Atomics.store(control, slot, CONTROL.IDLE);
|
|
2918
|
+
return frames[slot];
|
|
2919
|
+
},
|
|
2920
|
+
dispose: () => {
|
|
2921
|
+
for (let slot = 0; slot < slots; slot++) {
|
|
2922
|
+
Atomics.store(control, slot, CONTROL.STOP);
|
|
2923
|
+
Atomics.notify(control, slot);
|
|
2924
|
+
}
|
|
2925
|
+
worker.terminate();
|
|
2926
|
+
}
|
|
2927
|
+
};
|
|
2928
|
+
}
|
|
2929
|
+
function createTransferRunner(setup, shape, factory, slots = 1) {
|
|
2930
|
+
const worker = factory();
|
|
2931
|
+
const free = Array.from({ length: slots }, () => new ArrayBuffer(frameBytes(shape)));
|
|
2932
|
+
const done = [];
|
|
2933
|
+
const views = /* @__PURE__ */ new WeakMap();
|
|
2934
|
+
const viewOf = (buffer) => {
|
|
2935
|
+
let view = views.get(buffer);
|
|
2936
|
+
if (!view) views.set(buffer, view = createFrame(buffer, shape));
|
|
2937
|
+
return view;
|
|
2938
|
+
};
|
|
2939
|
+
let inFlight = 0;
|
|
2940
|
+
worker.postMessage({ type: "init", setup, shape });
|
|
2941
|
+
worker.onMessage((data) => {
|
|
2942
|
+
const message = data;
|
|
2943
|
+
if (message.type === "done") done.push(message.buffer);
|
|
2944
|
+
});
|
|
2945
|
+
return {
|
|
2946
|
+
transport: "transfer",
|
|
2947
|
+
get inFlight() {
|
|
2948
|
+
return inFlight;
|
|
2949
|
+
},
|
|
2950
|
+
frame: () => free.length ? viewOf(free[0]) : null,
|
|
2951
|
+
submit: () => {
|
|
2952
|
+
const buffer = free.shift();
|
|
2953
|
+
if (!buffer) return;
|
|
2954
|
+
worker.postMessage({ type: "frame", buffer }, [buffer]);
|
|
2955
|
+
inFlight++;
|
|
2956
|
+
},
|
|
2957
|
+
settle: () => {
|
|
2958
|
+
const buffer = done.shift();
|
|
2959
|
+
if (!buffer) return null;
|
|
2960
|
+
inFlight--;
|
|
2961
|
+
free.push(buffer);
|
|
2962
|
+
return viewOf(buffer);
|
|
2963
|
+
},
|
|
2964
|
+
dispose: () => worker.terminate()
|
|
2965
|
+
};
|
|
2966
|
+
}
|
|
2967
|
+
|
|
2968
|
+
// src/physics/player-pressure.ts
|
|
2969
|
+
var KNOCK_SECONDS = 1.5;
|
|
2970
|
+
var KNOCK_COOLDOWN = 2;
|
|
2971
|
+
var CRUSH_BLOW_EVERY = 0.25;
|
|
2972
|
+
function createPlayerPressure(ctx) {
|
|
2973
|
+
const options = ctx.options.playerPressure;
|
|
2974
|
+
const tau = ctx.options.balance?.pressureTau ?? 0.3;
|
|
2975
|
+
let knockCooldown = 0;
|
|
2976
|
+
let crushed = 0;
|
|
2977
|
+
let crushClock = 0;
|
|
2978
|
+
const crush = (excess) => {
|
|
2979
|
+
crushed += options.crushDamagePerSecond * excess * ctx.h;
|
|
2980
|
+
crushClock += ctx.h;
|
|
2981
|
+
if (crushClock < CRUSH_BLOW_EVERY) return;
|
|
2982
|
+
crushClock = 0;
|
|
2983
|
+
if (crushed <= 0) return;
|
|
2984
|
+
ctx.target()?.hurt(crushed);
|
|
2985
|
+
crushed = 0;
|
|
2986
|
+
};
|
|
2987
|
+
const step = (load) => {
|
|
2988
|
+
ctx.stats.playerPressure += (load - ctx.stats.playerPressure) * (1 - Math.exp(-ctx.h / tau));
|
|
2989
|
+
if (!options.enabled) return;
|
|
2990
|
+
const target = ctx.target();
|
|
2991
|
+
knockCooldown = Math.max(0, knockCooldown - ctx.h);
|
|
2992
|
+
if (!target || !target.state.alive || ctx.stats.playerPressure <= options.knockThreshold) {
|
|
2993
|
+
crushed = 0;
|
|
2994
|
+
return;
|
|
2995
|
+
}
|
|
2996
|
+
if (knockCooldown === 0) {
|
|
2997
|
+
target.knock(KNOCK_SECONDS);
|
|
2998
|
+
knockCooldown = KNOCK_COOLDOWN;
|
|
2999
|
+
}
|
|
3000
|
+
crush(ctx.stats.playerPressure / options.knockThreshold - 1);
|
|
3001
|
+
};
|
|
3002
|
+
return { step };
|
|
3003
|
+
}
|
|
3004
|
+
|
|
3005
|
+
// src/physics/thrown.ts
|
|
3006
|
+
var FREE_SLOT = -2;
|
|
3007
|
+
var CARRIED_BODY = -1;
|
|
3008
|
+
var MAX_SPIN = 8;
|
|
3009
|
+
var SWEEP_DISTANCE = 0.25;
|
|
3010
|
+
var LANDING_DAMAGE_SPEED = 6;
|
|
3011
|
+
var CENTRE_HEIGHT2 = 1;
|
|
3012
|
+
var MAX_POOL = 127;
|
|
3013
|
+
var createThrownPool = (size) => {
|
|
3014
|
+
const f32 = () => new Float32Array(size);
|
|
3015
|
+
const pool = {
|
|
3016
|
+
size,
|
|
3017
|
+
agent: new Int32Array(size).fill(FREE_SLOT),
|
|
3018
|
+
identity: new Uint32Array(size),
|
|
3019
|
+
corpse: Array.from({ length: size }, () => null),
|
|
3020
|
+
x: f32(),
|
|
3021
|
+
y: f32(),
|
|
3022
|
+
z: f32(),
|
|
3023
|
+
prevX: f32(),
|
|
3024
|
+
prevY: f32(),
|
|
3025
|
+
prevZ: f32(),
|
|
3026
|
+
vx: f32(),
|
|
3027
|
+
vy: f32(),
|
|
3028
|
+
vz: f32(),
|
|
3029
|
+
pitch: f32(),
|
|
3030
|
+
roll: f32(),
|
|
3031
|
+
spinPitch: f32(),
|
|
3032
|
+
spinRoll: f32(),
|
|
3033
|
+
active: () => pool.agent.reduce((count, agent) => count + (agent === FREE_SLOT ? 0 : 1), 0)
|
|
3034
|
+
};
|
|
3035
|
+
return pool;
|
|
3036
|
+
};
|
|
3037
|
+
var byFastest = (a, b, identityOf2) => b.speed - a.speed || identityOf2(a.index) - identityOf2(b.index);
|
|
3038
|
+
var byNearest = (a, b, identityOf2) => a.distance - b.distance || identityOf2(a.index) - identityOf2(b.index);
|
|
3039
|
+
function chooseCandidates(candidates, free, options, identityOf2) {
|
|
3040
|
+
if (candidates.length <= free) return candidates;
|
|
3041
|
+
if (options.priority === "fastest") return candidates.sort((a, b) => byFastest(a, b, identityOf2)).slice(0, free);
|
|
3042
|
+
if (options.priority === "nearest") return candidates.sort((a, b) => byNearest(a, b, identityOf2)).slice(0, free);
|
|
3043
|
+
const fast = Math.round(free * options.mixedRatio);
|
|
3044
|
+
const sorted = candidates.sort((a, b) => byFastest(a, b, identityOf2));
|
|
3045
|
+
const rest = sorted.slice(fast).sort((a, b) => byNearest(a, b, identityOf2));
|
|
3046
|
+
return [...sorted.slice(0, fast), ...rest.slice(0, free - fast)];
|
|
3047
|
+
}
|
|
3048
|
+
function createThrown(ctx) {
|
|
3049
|
+
const { store, grid } = ctx;
|
|
3050
|
+
const options = ctx.options.thrown;
|
|
3051
|
+
const size = Math.min(MAX_POOL, options.pool);
|
|
3052
|
+
const pool = createThrownPool(size);
|
|
3053
|
+
const identityOf2 = (i) => store.identity[i];
|
|
3054
|
+
const assign = (candidates) => {
|
|
3055
|
+
let free = 0;
|
|
3056
|
+
for (let slot2 = 0; slot2 < size; slot2++) if (pool.agent[slot2] === FREE_SLOT) free++;
|
|
3057
|
+
const chosen = chooseCandidates(candidates, free, options, identityOf2);
|
|
3058
|
+
let slot = 0;
|
|
3059
|
+
for (const candidate of chosen) {
|
|
3060
|
+
while (pool.agent[slot] !== FREE_SLOT) slot++;
|
|
3061
|
+
const i = candidate.index;
|
|
3062
|
+
pool.agent[slot] = i;
|
|
3063
|
+
pool.identity[slot] = store.identity[i];
|
|
3064
|
+
pool.corpse[slot] = null;
|
|
3065
|
+
pool.x[slot] = pool.prevX[slot] = store.x[i];
|
|
3066
|
+
pool.y[slot] = pool.prevY[slot] = store.y[i] + CENTRE_HEIGHT2 * store.height[i];
|
|
3067
|
+
pool.z[slot] = pool.prevZ[slot] = store.z[i];
|
|
3068
|
+
pool.vx[slot] = candidate.vx;
|
|
3069
|
+
pool.vy[slot] = candidate.vy;
|
|
3070
|
+
pool.vz[slot] = candidate.vz;
|
|
3071
|
+
pool.pitch[slot] = pool.roll[slot] = 0;
|
|
3072
|
+
pool.spinPitch[slot] = (unit(store.identity[i], ctx.step, SALT.SPIN) * 2 - 1) * MAX_SPIN;
|
|
3073
|
+
pool.spinRoll[slot] = (unit(store.identity[i], ctx.step, SALT.SPIN + 1) * 2 - 1) * MAX_SPIN;
|
|
3074
|
+
if (store.state[i] === STATE.PILE_STEP) ctx.releasePileStep(i);
|
|
3075
|
+
store.balance[i] = BALANCE.THROWN;
|
|
3076
|
+
store.thrown[i] = slot;
|
|
3077
|
+
store.vx[i] = store.vz[i] = store.pushX[i] = store.pushZ[i] = 0;
|
|
3078
|
+
store.aimX[i] = store.aimZ[i] = 0;
|
|
3079
|
+
}
|
|
3080
|
+
return chosen.length;
|
|
3081
|
+
};
|
|
3082
|
+
const remap = (removed, moved) => {
|
|
3083
|
+
var _a;
|
|
3084
|
+
for (let slot = 0; slot < size; slot++) {
|
|
3085
|
+
const agent = pool.agent[slot];
|
|
3086
|
+
if (agent === removed) {
|
|
3087
|
+
(_a = pool.corpse)[slot] ?? (_a[slot] = ctx.corpseOf(removed, unit(pool.identity[slot], ctx.step, SALT.CORPSE) * 10));
|
|
3088
|
+
pool.agent[slot] = CARRIED_BODY;
|
|
3089
|
+
} else if (agent === moved && moved !== removed) pool.agent[slot] = removed;
|
|
3090
|
+
}
|
|
3091
|
+
};
|
|
3092
|
+
const sweepWall = (x0, z0, x1, z1, y) => {
|
|
3093
|
+
const cell = grid.cell;
|
|
3094
|
+
let gx = Math.floor((x0 + grid.half) / cell);
|
|
3095
|
+
let gz = Math.floor((z0 + grid.half) / cell);
|
|
3096
|
+
const dx = x1 - x0;
|
|
3097
|
+
const dz = z1 - z0;
|
|
3098
|
+
const stepX = dx > 0 ? 1 : -1;
|
|
3099
|
+
const stepZ = dz > 0 ? 1 : -1;
|
|
3100
|
+
const deltaX = dx === 0 ? Infinity : Math.abs(cell / dx);
|
|
3101
|
+
const deltaZ = dz === 0 ? Infinity : Math.abs(cell / dz);
|
|
3102
|
+
let nextX = dx === 0 ? Infinity : ((dx > 0 ? gx + 1 : gx) * cell - grid.half - x0) / dx;
|
|
3103
|
+
let nextZ = dz === 0 ? Infinity : ((dz > 0 ? gz + 1 : gz) * cell - grid.half - z0) / dz;
|
|
3104
|
+
let t = 0;
|
|
3105
|
+
while (Math.min(nextX, nextZ) <= 1) {
|
|
3106
|
+
if (nextX < nextZ) {
|
|
3107
|
+
t = nextX;
|
|
3108
|
+
nextX += deltaX;
|
|
3109
|
+
gx += stepX;
|
|
3110
|
+
} else {
|
|
3111
|
+
t = nextZ;
|
|
3112
|
+
nextZ += deltaZ;
|
|
3113
|
+
gz += stepZ;
|
|
3114
|
+
}
|
|
3115
|
+
const hitX = x0 + dx * t;
|
|
3116
|
+
const hitZ = z0 + dz * t;
|
|
3117
|
+
if (grid.solidAt(hitX + Math.sign(dx) * 1e-4, hitZ + Math.sign(dz) * 1e-4, y)) return { x: x0 + dx * Math.max(0, t - 1e-3), z: z0 + dz * Math.max(0, t - 1e-3) };
|
|
3118
|
+
}
|
|
3119
|
+
return void 0;
|
|
3120
|
+
};
|
|
3121
|
+
const fly = (slot) => {
|
|
3122
|
+
pool.prevX[slot] = pool.x[slot];
|
|
3123
|
+
pool.prevY[slot] = pool.y[slot];
|
|
3124
|
+
pool.prevZ[slot] = pool.z[slot];
|
|
3125
|
+
pool.vy[slot] -= ctx.gravity * ctx.h;
|
|
3126
|
+
pool.pitch[slot] += pool.spinPitch[slot] * ctx.h;
|
|
3127
|
+
pool.roll[slot] += pool.spinRoll[slot] * ctx.h;
|
|
3128
|
+
const nx = pool.x[slot] + pool.vx[slot] * ctx.h;
|
|
3129
|
+
const nz = pool.z[slot] + pool.vz[slot] * ctx.h;
|
|
3130
|
+
const ny = pool.y[slot] + pool.vy[slot] * ctx.h;
|
|
3131
|
+
const speed = Math.hypot(pool.vx[slot], pool.vy[slot], pool.vz[slot]);
|
|
3132
|
+
const wall = speed * ctx.h > SWEEP_DISTANCE ? sweepWall(pool.x[slot], pool.z[slot], nx, nz, ny) : grid.solidAt(nx, nz, ny) ? { x: pool.x[slot], z: pool.z[slot] } : void 0;
|
|
3133
|
+
pool.x[slot] = wall ? wall.x : nx;
|
|
3134
|
+
pool.z[slot] = wall ? wall.z : nz;
|
|
3135
|
+
pool.y[slot] = ny;
|
|
3136
|
+
if (wall) pool.vx[slot] = pool.vz[slot] = 0;
|
|
3137
|
+
};
|
|
3138
|
+
const massOf = (slot) => {
|
|
3139
|
+
const agent = pool.agent[slot];
|
|
3140
|
+
if (agent >= 0) return store.mass[agent];
|
|
3141
|
+
const body = pool.corpse[slot];
|
|
3142
|
+
return ctx.options.contact.baseMass * body.height * body.build * body.build;
|
|
3143
|
+
};
|
|
3144
|
+
const knockNeighbours = (contact, balance, slot) => {
|
|
3145
|
+
const side = Math.floor(grid.size);
|
|
3146
|
+
const x = pool.x[slot];
|
|
3147
|
+
const z = pool.z[slot];
|
|
3148
|
+
const hx = Math.floor(x + grid.half);
|
|
3149
|
+
const hz = Math.floor(z + grid.half);
|
|
3150
|
+
const mass = massOf(slot);
|
|
3151
|
+
const speed = Math.hypot(pool.vx[slot], pool.vz[slot]);
|
|
3152
|
+
const reach = options.landingRadius * options.landingRadius;
|
|
3153
|
+
for (let oz = -1; oz <= 1; oz++)
|
|
3154
|
+
for (let ox = -1; ox <= 1; ox++) {
|
|
3155
|
+
if (hx + ox < 0 || hz + oz < 0 || hx + ox >= side || hz + oz >= side) continue;
|
|
3156
|
+
for (let j = contact.workspace.head[(hz + oz) * side + hx + ox]; j !== -1; j = contact.workspace.next[j]) {
|
|
3157
|
+
const ex = store.x[j] - x;
|
|
3158
|
+
const ez = store.z[j] - z;
|
|
3159
|
+
if (j === pool.agent[slot] || ex * ex + ez * ez > reach || !takesPartInContact(store, j)) continue;
|
|
3160
|
+
const away = Math.hypot(ex, ez);
|
|
3161
|
+
const along = away > 1e-6 ? [ex / away, ez / away] : [pool.vx[slot] / (speed || 1), pool.vz[slot] / (speed || 1)];
|
|
3162
|
+
const delta = mass / (mass + store.mass[j]) * speed;
|
|
3163
|
+
store.pushX[j] += delta * along[0];
|
|
3164
|
+
store.pushZ[j] += delta * along[1];
|
|
3165
|
+
balance.shove(j, delta, FALL_CAUSE.LANDING);
|
|
3166
|
+
}
|
|
3167
|
+
}
|
|
3168
|
+
};
|
|
3169
|
+
const land = (contact, balance, slot, ground) => {
|
|
3170
|
+
knockNeighbours(contact, balance, slot);
|
|
3171
|
+
const x = pool.x[slot];
|
|
3172
|
+
const z = pool.z[slot];
|
|
3173
|
+
ctx.emit("sound", { sound: "impact", volume: 1.2, position: [x, ground, z], distance: Math.hypot(x - ctx.centre.x, z - ctx.centre.z) });
|
|
3174
|
+
ctx.emit("effect", { effect: "dust", point: [x, ground, z], color: 9075302 });
|
|
3175
|
+
const agent = pool.agent[slot];
|
|
3176
|
+
if (agent >= 0) {
|
|
3177
|
+
store.balance[agent] = BALANCE.FALLEN;
|
|
3178
|
+
store.balanceTimer[agent] = 0;
|
|
3179
|
+
store.fallCause[agent] = FALL_CAUSE.BLAST;
|
|
3180
|
+
store.x[agent] = x;
|
|
3181
|
+
store.z[agent] = z;
|
|
3182
|
+
store.y[agent] = ground;
|
|
3183
|
+
store.vy[agent] = 0;
|
|
3184
|
+
store.thrown[agent] = -1;
|
|
3185
|
+
const amount = Math.max(0, -pool.vy[slot] - LANDING_DAMAGE_SPEED) * options.fallDamage;
|
|
3186
|
+
if (amount > 0) ctx.damage(agent, { region: 1, amount, distance: 0 }, () => unit(store.identity[agent], ctx.step, SALT.JAW));
|
|
3187
|
+
} else {
|
|
3188
|
+
ctx.emit("effect", { effect: "blood", point: [x, ground + 0.1, z], amount: 30, died: true });
|
|
3189
|
+
ctx.addCorpse({ ...pool.corpse[slot], x, z });
|
|
3190
|
+
}
|
|
3191
|
+
pool.agent[slot] = FREE_SLOT;
|
|
3192
|
+
pool.corpse[slot] = null;
|
|
3193
|
+
};
|
|
3194
|
+
const step = (contact, balance) => {
|
|
3195
|
+
let active = 0;
|
|
3196
|
+
for (let slot = 0; slot < size; slot++) {
|
|
3197
|
+
if (pool.agent[slot] === FREE_SLOT) continue;
|
|
3198
|
+
fly(slot);
|
|
3199
|
+
const cell = grid.cellOf(pool.x[slot], pool.z[slot]);
|
|
3200
|
+
const ground = cell < 0 ? 0 : ctx.groundAt(cell, pool.y[slot]);
|
|
3201
|
+
const agent = pool.agent[slot];
|
|
3202
|
+
if (pool.y[slot] <= ground) {
|
|
3203
|
+
land(contact, balance, slot, ground);
|
|
3204
|
+
continue;
|
|
3205
|
+
}
|
|
3206
|
+
active++;
|
|
3207
|
+
if (agent < 0) continue;
|
|
3208
|
+
store.x[agent] = pool.x[slot];
|
|
3209
|
+
store.z[agent] = pool.z[slot];
|
|
3210
|
+
store.y[agent] = Math.max(ground, pool.y[slot] - CENTRE_HEIGHT2 * store.height[agent]);
|
|
3211
|
+
}
|
|
3212
|
+
ctx.stats.thrownActive = active;
|
|
3213
|
+
ctx.stats.thrownPeak = Math.max(ctx.stats.thrownPeak, active);
|
|
3214
|
+
};
|
|
3215
|
+
return { pool, assign, step, remap };
|
|
3216
|
+
}
|
|
3217
|
+
|
|
3218
|
+
// src/physics/engine.ts
|
|
3219
|
+
var DEFAULT_DRIVE_FORCE = 250;
|
|
3220
|
+
var GRAVITY_FORCE2 = 9.8;
|
|
3221
|
+
var EXTRA_BODIES = 500;
|
|
3222
|
+
var PAIRS_PER_AGENT3 = 12;
|
|
3223
|
+
function createPhysics(base, options, stats = createPhysicsStats()) {
|
|
3224
|
+
const moundOn = options.mound !== null;
|
|
3225
|
+
const ctx = { ...base, options, stats, step: -1, h: 0, centre: { x: 0, z: 0 }, lastBlastStep: -1e3, moundAt: (cell) => moundOn ? base.moundHeight[cell] : 0, weightOn: () => 0 };
|
|
3226
|
+
const { store, grid } = base;
|
|
3227
|
+
const balance = options.balance ? createBalance(ctx) : null;
|
|
3228
|
+
const thrown = options.thrown ? createThrown(ctx) : null;
|
|
3229
|
+
const contact = options.contact ? createContactView(store, grid) : null;
|
|
3230
|
+
const pressure = options.pressure && contact ? createPressureWorkspace(store.capacity, store.capacity * PAIRS_PER_AGENT3) : null;
|
|
3231
|
+
const mound = options.mound ? createMound(ctx, Math.max(EXTRA_BODIES, base.corpsesMax), base.moundHeight) : null;
|
|
3232
|
+
if (mound) ctx.weightOn = mound.weightOn;
|
|
3233
|
+
const density = options.density && balance ? createDensity(ctx, options.density) : null;
|
|
3234
|
+
const playerPressure = pressure ? createPlayerPressure(ctx) : null;
|
|
3235
|
+
const blasts = createBlasts(ctx, { balance, thrown, pileBlast: base.pileBlast });
|
|
3236
|
+
const contactResult = { contacts: 0, maxOverlap: 0, overlapSum: 0 };
|
|
3237
|
+
const pressureResult = { playerLoad: 0, drivers: 0 };
|
|
3238
|
+
const driveForceOfType = Float32Array.from(base.types, (type) => type.balance?.driveForce ?? DEFAULT_DRIVE_FORCE);
|
|
3239
|
+
const offload = options.worker !== "off" && contact ? createOffloadFor() : null;
|
|
3240
|
+
let doorLoad = new Float32Array(0);
|
|
3241
|
+
let doorSince = new Float32Array(0);
|
|
3242
|
+
const keepPositions = options.contact !== null || options.deterministic;
|
|
3243
|
+
function createRunner() {
|
|
3244
|
+
const gridCells = grid.occ.length;
|
|
3245
|
+
const shape = { capacity: store.capacity, cells: gridCells };
|
|
3246
|
+
const setup = { capacity: store.capacity, gridSize: grid.size, gridCell: grid.cell, contact: options.contact, pressure: options.pressure, driveForceOfType: Array.from(driveForceOfType) };
|
|
3247
|
+
const transport = options.workerTransport ?? (isSharedAvailable() ? "shared" : "transfer");
|
|
3248
|
+
const slots = options.workerLatency;
|
|
3249
|
+
if (transport === "inline") return createInlineRunner(setup, shape, slots);
|
|
3250
|
+
const factory = options.workerFactory ?? createDefaultWorkerFactory();
|
|
3251
|
+
if (!factory) {
|
|
3252
|
+
if (options.worker === "on") throw new Error('crowd physics: worker "on" but no Worker can be made here (give physics.workerFactory)');
|
|
3253
|
+
return null;
|
|
3254
|
+
}
|
|
3255
|
+
return transport === "shared" ? createSharedRunner(setup, shape, factory, slots) : createTransferRunner(setup, shape, factory, slots);
|
|
3256
|
+
}
|
|
3257
|
+
function createOffloadFor() {
|
|
3258
|
+
const runner = createRunner();
|
|
3259
|
+
if (!runner) return null;
|
|
3260
|
+
const host = {
|
|
3261
|
+
store,
|
|
3262
|
+
grid,
|
|
3263
|
+
stats,
|
|
3264
|
+
contact,
|
|
3265
|
+
addDoorLoad: (cell, load) => {
|
|
3266
|
+
const door = ctx.doors.at(cell);
|
|
3267
|
+
if (door < 0) return;
|
|
3268
|
+
growDoorArrays();
|
|
3269
|
+
doorLoad[door] += load;
|
|
3270
|
+
}
|
|
3271
|
+
};
|
|
3272
|
+
return createOffload(runner, host, options.workerLatency);
|
|
3273
|
+
}
|
|
3274
|
+
const beginStep = (h, centre) => {
|
|
3275
|
+
ctx.step++;
|
|
3276
|
+
ctx.h = h;
|
|
3277
|
+
ctx.centre = { x: centre.x, z: centre.z };
|
|
3278
|
+
if (keepPositions) {
|
|
3279
|
+
for (let i = 0; i < store.n; i++) {
|
|
3280
|
+
store.prevX[i] = store.x[i];
|
|
3281
|
+
store.prevZ[i] = store.z[i];
|
|
3282
|
+
store.prevY[i] = store.y[i];
|
|
3283
|
+
}
|
|
3284
|
+
}
|
|
3285
|
+
blasts.resolve();
|
|
3286
|
+
};
|
|
3287
|
+
const hitDoors = () => {
|
|
3288
|
+
for (let door = 0; door < ctx.doors.list.length; door++) {
|
|
3289
|
+
if (doorLoad[door] <= 0) continue;
|
|
3290
|
+
doorSince[door] += ctx.h;
|
|
3291
|
+
if (doorSince[door] < ctx.attackEvery) continue;
|
|
3292
|
+
ctx.doors.hit(door, doorLoad[door] * options.doorPressureDamage);
|
|
3293
|
+
doorLoad[door] = 0;
|
|
3294
|
+
doorSince[door] = 0;
|
|
3295
|
+
}
|
|
3296
|
+
};
|
|
3297
|
+
const growDoorArrays = () => {
|
|
3298
|
+
if (doorLoad.length >= ctx.doors.list.length) return;
|
|
3299
|
+
const length = Math.max(64, ctx.doors.list.length * 2);
|
|
3300
|
+
doorLoad = Float32Array.from({ length }, (_, door) => doorLoad[door] ?? 0);
|
|
3301
|
+
doorSince = Float32Array.from({ length }, (_, door) => doorSince[door] ?? 0);
|
|
3302
|
+
};
|
|
3303
|
+
const runPressure = (view, workspace) => {
|
|
3304
|
+
growDoorArrays();
|
|
3305
|
+
const target = ctx.target();
|
|
3306
|
+
const player = options.playerPressure.enabled && target && target.state.alive ? { x: target.state.x, z: target.state.z } : void 0;
|
|
3307
|
+
pressureStep(view, workspace, { ...options.pressure, h: ctx.h, radius: options.contact.radius }, { driveForceOfType, doorAt: ctx.doors.at, doorLoad, player }, pressureResult);
|
|
3308
|
+
let top = 0;
|
|
3309
|
+
for (let i = 0; i < store.n; i++) top = Math.max(top, store.field[i]);
|
|
3310
|
+
stats.fieldMax = top;
|
|
3311
|
+
hitDoors();
|
|
3312
|
+
};
|
|
3313
|
+
const stepOffloaded = (running, view) => {
|
|
3314
|
+
growDoorArrays();
|
|
3315
|
+
const target = ctx.target();
|
|
3316
|
+
const player = options.playerPressure.enabled && target && target.state.alive ? { x: target.state.x, z: target.state.z } : void 0;
|
|
3317
|
+
const result = running.step(ctx.h, player);
|
|
3318
|
+
pressureResult.playerLoad = result.playerLoad;
|
|
3319
|
+
prepareHash(view, options.contact.radius);
|
|
3320
|
+
if (pressure) hitDoors();
|
|
3321
|
+
};
|
|
3322
|
+
const measureLean = () => {
|
|
3323
|
+
const maxPush = options.contact.maxPush;
|
|
3324
|
+
for (let i = 0; i < store.n; i++) {
|
|
3325
|
+
store.leanX[i] = store.pushX[i] / maxPush;
|
|
3326
|
+
store.leanZ[i] = store.pushZ[i] / maxPush;
|
|
3327
|
+
}
|
|
3328
|
+
};
|
|
3329
|
+
const endStep = () => {
|
|
3330
|
+
if (!contact) return;
|
|
3331
|
+
contact.n = store.n;
|
|
3332
|
+
if (offload) stepOffloaded(offload, contact);
|
|
3333
|
+
else {
|
|
3334
|
+
contactStep(contact, { ...options.contact, h: ctx.h }, contactResult);
|
|
3335
|
+
stats.contacts = contactResult.contacts;
|
|
3336
|
+
stats.maxOverlap = contactResult.maxOverlap;
|
|
3337
|
+
stats.meanOverlap = contactResult.contacts ? contactResult.overlapSum / contactResult.contacts : 0;
|
|
3338
|
+
if (pressure) runPressure(contact, pressure);
|
|
3339
|
+
}
|
|
3340
|
+
density?.step();
|
|
3341
|
+
measureLean();
|
|
3342
|
+
balance?.step(options.balance, contact.workspace.shove);
|
|
3343
|
+
mound?.step();
|
|
3344
|
+
thrown?.step(contact, balance);
|
|
3345
|
+
balance?.flush();
|
|
3346
|
+
playerPressure?.step(pressureResult.playerLoad);
|
|
3347
|
+
};
|
|
3348
|
+
const criticalOf = (i) => options.balance.criticalFactor * store.mass[i] * GRAVITY_FORCE2 * (base.typeOf(i).balance?.stability ?? 1) * store.stability[i];
|
|
3349
|
+
return {
|
|
3350
|
+
ctx,
|
|
3351
|
+
active: contact !== null,
|
|
3352
|
+
stats,
|
|
3353
|
+
blasts,
|
|
3354
|
+
thrown: thrown?.pool ?? createThrownPool(0),
|
|
3355
|
+
contact,
|
|
3356
|
+
balance,
|
|
3357
|
+
beginStep,
|
|
3358
|
+
endStep,
|
|
3359
|
+
remap: (removed, moved) => {
|
|
3360
|
+
thrown?.remap(removed, moved);
|
|
3361
|
+
offload?.remap(removed, moved);
|
|
3362
|
+
},
|
|
3363
|
+
dispose: () => offload?.dispose(),
|
|
3364
|
+
densitySlowdown: (i, band) => density?.slowdown(i, band) ?? 1,
|
|
3365
|
+
pressureRatio: (i) => options.balance ? store.pressure[i] / criticalOf(i) : 0
|
|
3366
|
+
};
|
|
3367
|
+
}
|
|
3368
|
+
|
|
3369
|
+
// src/physics/pile-column.ts
|
|
3370
|
+
var COLLAPSE_MARK = -7;
|
|
3371
|
+
function collapsePileColumns(store, columns, cellCount, release) {
|
|
3372
|
+
const owners = /* @__PURE__ */ new Map();
|
|
3373
|
+
for (let i = 0; i < store.n; i++) if (store.state[i] === STATE.PILE_STEP && store.stepCell[i] >= 0) owners.set(store.stepCell[i], i);
|
|
3374
|
+
for (const column of columns.splice(0)) {
|
|
3375
|
+
if (column.dir === 0) continue;
|
|
3376
|
+
for (let k = 1, cell = column.cell - column.dir; cell >= 0 && cell < cellCount; k++, cell = column.cell - column.dir * k) {
|
|
3377
|
+
const j = owners.get(cell);
|
|
3378
|
+
if (j === void 0) break;
|
|
3379
|
+
owners.delete(cell);
|
|
3380
|
+
release(j);
|
|
3381
|
+
store.state[j] = STATE.AIRBORNE;
|
|
3382
|
+
store.vy[j] = 0;
|
|
3383
|
+
store.stateTimer[j] = COLLAPSE_MARK;
|
|
3384
|
+
}
|
|
3385
|
+
}
|
|
3386
|
+
}
|
|
3387
|
+
|
|
3388
|
+
// src/personality.ts
|
|
3389
|
+
var GAIT_WEIGHTS = [6, 1, 1, 1, 1, 2];
|
|
3390
|
+
var triangular = (identity, salt, range) => range[0] + (range[1] - range[0]) * ((unit(identity, 0, salt) + unit(identity, 1, salt)) / 2);
|
|
3391
|
+
var weighted = (identity, salt, weights) => {
|
|
3392
|
+
const total = weights.reduce((sum, weight) => sum + weight, 0);
|
|
3393
|
+
let pick = unit(identity, 0, salt) * total;
|
|
3394
|
+
for (let index = 0; index < weights.length; index++) {
|
|
3395
|
+
pick -= weights[index];
|
|
3396
|
+
if (pick < 0) return index;
|
|
3397
|
+
}
|
|
3398
|
+
return weights.length - 1;
|
|
3399
|
+
};
|
|
3400
|
+
function applyNeutralPersonality(store, i) {
|
|
3401
|
+
store.stability[i] = store.tripFactor[i] = store.driveFactor[i] = store.cadenceFactor[i] = store.aggression[i] = 1;
|
|
3402
|
+
store.reaction[i] = 0;
|
|
3403
|
+
store.idle[i] = 0;
|
|
3404
|
+
store.gaitStyle[i] = 0;
|
|
3405
|
+
}
|
|
3406
|
+
function applyPersonality(store, i, options) {
|
|
3407
|
+
const identity = store.identity[i];
|
|
3408
|
+
const speed = triangular(identity, SALT.SPEED, options.speed);
|
|
3409
|
+
store.chaseSpeed[i] *= speed;
|
|
3410
|
+
store.wanderSpeed[i] *= speed;
|
|
3411
|
+
store.reaction[i] = triangular(identity, SALT.REACTION, options.reaction);
|
|
3412
|
+
store.stability[i] = triangular(identity, SALT.STABILITY, options.stability);
|
|
3413
|
+
store.tripFactor[i] = triangular(identity, SALT.TRIP_FACTOR, options.trip);
|
|
3414
|
+
store.driveFactor[i] = triangular(identity, SALT.DRIVE, options.drive);
|
|
3415
|
+
store.cadenceFactor[i] = triangular(identity, SALT.CADENCE, options.cadence);
|
|
3416
|
+
store.aggression[i] = triangular(identity, SALT.AGGRESSION, options.aggression);
|
|
3417
|
+
store.idle[i] = weighted(identity, SALT.IDLE, options.idle);
|
|
3418
|
+
store.gaitStyle[i] = weighted(identity, SALT.GAIT, GAIT_WEIGHTS);
|
|
3419
|
+
}
|
|
3420
|
+
|
|
3421
|
+
// src/physics/seat.ts
|
|
3422
|
+
function seatPhysics(store, i, born) {
|
|
3423
|
+
store.identity[i] = identityOf(born.seed, born.serial);
|
|
3424
|
+
store.mass[i] = born.baseMass * store.height[i] * store.build[i] ** 2;
|
|
3425
|
+
store.balance[i] = BALANCE.STANDING;
|
|
3426
|
+
store.balanceTimer[i] = store.pushX[i] = store.pushZ[i] = store.pressure[i] = store.field[i] = store.fieldPrev[i] = store.aimX[i] = store.aimZ[i] = store.directShove[i] = store.leanX[i] = store.leanZ[i] = 0;
|
|
3427
|
+
store.fallCause[i] = 0;
|
|
3428
|
+
store.blastStep[i] = -1;
|
|
3429
|
+
store.thrown[i] = -1;
|
|
3430
|
+
store.wounds[i] = 0;
|
|
3431
|
+
store.stepDir[i] = 0;
|
|
3432
|
+
store.prevX[i] = store.x[i];
|
|
3433
|
+
store.prevZ[i] = store.z[i];
|
|
3434
|
+
store.prevY[i] = 0;
|
|
3435
|
+
if (born.personality) applyPersonality(store, i, born.personality);
|
|
3436
|
+
else applyNeutralPersonality(store, i);
|
|
3437
|
+
}
|
|
3438
|
+
|
|
3439
|
+
// src/physics/options.ts
|
|
3440
|
+
var CONTACT_DEFAULTS = { radius: 0.25, iterations: 2, friction: 6, maxPush: 3, maxCorrection: 0.1, relaxation: 1, baseMass: 70, separationMax: 1.5 };
|
|
3441
|
+
var PRESSURE_DEFAULTS = { transmission: 0.97, cap: 6e3, frontCone: 0.5, staggerSpeed: 0.35 };
|
|
3442
|
+
var BALANCE_DEFAULTS = {
|
|
3443
|
+
criticalFactor: 2,
|
|
3444
|
+
staggerEnter: 0.7,
|
|
3445
|
+
staggerLeave: 0.4,
|
|
3446
|
+
staggerMin: 0.5,
|
|
3447
|
+
fallRate: 1.5,
|
|
3448
|
+
shoveStagger: 1.2,
|
|
3449
|
+
shoveFall: 2.5,
|
|
3450
|
+
fallenMin: 1.5,
|
|
3451
|
+
releaseFactor: 0.3,
|
|
3452
|
+
getUpSeconds: 1.2,
|
|
3453
|
+
pressureTau: 0.3,
|
|
3454
|
+
maxFallsPerStep: 8,
|
|
3455
|
+
maxFallen: 300,
|
|
3456
|
+
trampleDps: 6
|
|
3457
|
+
};
|
|
3458
|
+
var MOUND_DEFAULTS = { bodyThickness: 0.28, corpseThickness: 0.25, maxHeight: 1.6, corpseRadius: 35 };
|
|
3459
|
+
var THROWN_DEFAULTS = { pool: 32, radius: 30, landingRadius: 1.2, fallDamage: 8, priority: "mixed", mixedRatio: 0.5 };
|
|
3460
|
+
var DENSITY_DEFAULTS = { cell: 2, capacity: 4, pressurePerBody: 150 };
|
|
3461
|
+
var BLAST_DEFAULTS = { pointsPerKilojoule: 0.6, throwSpeed: 4, maxBlastsPerStep: 8, limbEffectsPerBlast: 24, bloodEffectsPerBlast: 32, shove: {} };
|
|
3462
|
+
var PERSONALITY_DEFAULTS = {
|
|
3463
|
+
speed: [0.9, 1.1],
|
|
3464
|
+
reaction: [0.05, 0.35],
|
|
3465
|
+
stability: [0.75, 1.25],
|
|
3466
|
+
trip: [0.5, 1.5],
|
|
3467
|
+
drive: [0.7, 1.3],
|
|
3468
|
+
cadence: [0.85, 1.15],
|
|
3469
|
+
aggression: [0.8, 1.2],
|
|
3470
|
+
idle: [3, 2, 2, 1]
|
|
3471
|
+
};
|
|
3472
|
+
var PLAYER_PRESSURE_DEFAULTS = { enabled: false, knockThreshold: 1500, crushDamagePerSecond: 6 };
|
|
3473
|
+
var DOOR_PRESSURE_DAMAGE = 4e-3;
|
|
3474
|
+
var DETERMINISTIC_FLOW_CELLS = 2e4;
|
|
3475
|
+
var CROWD_PHYSICS_DEFAULTS = {
|
|
3476
|
+
contact: CONTACT_DEFAULTS,
|
|
3477
|
+
pressure: PRESSURE_DEFAULTS,
|
|
3478
|
+
balance: BALANCE_DEFAULTS,
|
|
3479
|
+
mound: MOUND_DEFAULTS,
|
|
3480
|
+
thrown: THROWN_DEFAULTS,
|
|
3481
|
+
density: DENSITY_DEFAULTS,
|
|
3482
|
+
blast: BLAST_DEFAULTS,
|
|
3483
|
+
personality: PERSONALITY_DEFAULTS,
|
|
3484
|
+
door: { pressureDamage: DOOR_PRESSURE_DAMAGE },
|
|
3485
|
+
playerPressure: PLAYER_PRESSURE_DEFAULTS,
|
|
3486
|
+
deterministic: true
|
|
3487
|
+
};
|
|
3488
|
+
var part = (option, defaults) => {
|
|
3489
|
+
if (option === void 0 || option === false) return null;
|
|
3490
|
+
return option === true ? defaults : { ...defaults, ...option };
|
|
3491
|
+
};
|
|
3492
|
+
function resolvePhysics(physics = {}) {
|
|
3493
|
+
const contact = part(physics.contact, CONTACT_DEFAULTS);
|
|
3494
|
+
const pressure = part(physics.pressure, PRESSURE_DEFAULTS);
|
|
3495
|
+
const balance = part(physics.balance, BALANCE_DEFAULTS);
|
|
3496
|
+
const mound = part(physics.mound, MOUND_DEFAULTS);
|
|
3497
|
+
const thrown = part(physics.thrown, THROWN_DEFAULTS);
|
|
3498
|
+
const density = part(physics.density, DENSITY_DEFAULTS);
|
|
3499
|
+
const playerPressure = { ...PLAYER_PRESSURE_DEFAULTS, ...physics.playerPressure };
|
|
3500
|
+
const missing = [];
|
|
3501
|
+
if (!contact && pressure) missing.push("pressure needs contact");
|
|
3502
|
+
if (!contact && balance) missing.push("balance needs contact");
|
|
3503
|
+
if (!balance && mound) missing.push("mound needs balance");
|
|
3504
|
+
if (!balance && thrown) missing.push("thrown needs balance");
|
|
3505
|
+
if (!balance && density) missing.push("density needs balance");
|
|
3506
|
+
if (!pressure && playerPressure.enabled) missing.push("playerPressure needs pressure");
|
|
3507
|
+
if (!contact && physics.worker === "on") missing.push("worker needs contact");
|
|
3508
|
+
if (missing.length) throw new Error(`crowd physics: ${missing.join("; ")}`);
|
|
3509
|
+
return {
|
|
3510
|
+
contact,
|
|
3511
|
+
pressure,
|
|
3512
|
+
balance,
|
|
3513
|
+
mound,
|
|
3514
|
+
thrown,
|
|
3515
|
+
density,
|
|
3516
|
+
blast: { ...BLAST_DEFAULTS, ...physics.blast },
|
|
3517
|
+
personality: part(physics.personality, PERSONALITY_DEFAULTS),
|
|
3518
|
+
doorPressureDamage: physics.door?.pressureDamage ?? DOOR_PRESSURE_DAMAGE,
|
|
3519
|
+
playerPressure,
|
|
3520
|
+
deterministic: physics.deterministic ?? false,
|
|
3521
|
+
worker: physics.worker ?? "off",
|
|
3522
|
+
workerFactory: physics.workerFactory,
|
|
3523
|
+
workerTransport: physics.workerTransport,
|
|
3524
|
+
workerLatency: physics.workerLatency ?? 1
|
|
3525
|
+
};
|
|
3526
|
+
}
|
|
3527
|
+
|
|
1599
3528
|
// src/rng.ts
|
|
1600
3529
|
function createRng(seed) {
|
|
1601
3530
|
let state = seed >>> 0;
|
|
@@ -1668,7 +3597,38 @@ var GameCrowd = (() => {
|
|
|
1668
3597
|
lod: u8(),
|
|
1669
3598
|
variant: u8(),
|
|
1670
3599
|
attackPose: u8(),
|
|
1671
|
-
arms: f32()
|
|
3600
|
+
arms: f32(),
|
|
3601
|
+
stepDir: new Int32Array(capacity),
|
|
3602
|
+
// physics (CrowdOptions.physics): they exist with it off, so the store keeps its shape; the game replays never read them
|
|
3603
|
+
pushX: f32(),
|
|
3604
|
+
pushZ: f32(),
|
|
3605
|
+
mass: f32(),
|
|
3606
|
+
pressure: f32(),
|
|
3607
|
+
field: f32(),
|
|
3608
|
+
fieldPrev: f32(),
|
|
3609
|
+
aimX: f32(),
|
|
3610
|
+
aimZ: f32(),
|
|
3611
|
+
directShove: f32(),
|
|
3612
|
+
blastStep: new Int32Array(capacity),
|
|
3613
|
+
balance: u8(),
|
|
3614
|
+
balanceTimer: f32(),
|
|
3615
|
+
fallCause: u8(),
|
|
3616
|
+
identity: new Uint32Array(capacity),
|
|
3617
|
+
stability: f32(),
|
|
3618
|
+
tripFactor: f32(),
|
|
3619
|
+
driveFactor: f32(),
|
|
3620
|
+
reaction: f32(),
|
|
3621
|
+
cadenceFactor: f32(),
|
|
3622
|
+
aggression: f32(),
|
|
3623
|
+
idle: u8(),
|
|
3624
|
+
gaitStyle: u8(),
|
|
3625
|
+
wounds: new Uint16Array(capacity),
|
|
3626
|
+
leanX: f32(),
|
|
3627
|
+
leanZ: f32(),
|
|
3628
|
+
thrown: new Int8Array(capacity),
|
|
3629
|
+
prevX: f32(),
|
|
3630
|
+
prevZ: f32(),
|
|
3631
|
+
prevY: f32()
|
|
1672
3632
|
};
|
|
1673
3633
|
};
|
|
1674
3634
|
var AGENT_FIELDS = [
|
|
@@ -1724,26 +3684,39 @@ var GameCrowd = (() => {
|
|
|
1724
3684
|
"lod",
|
|
1725
3685
|
"variant",
|
|
1726
3686
|
"attackPose",
|
|
1727
|
-
"arms"
|
|
3687
|
+
"arms",
|
|
3688
|
+
"stepDir",
|
|
3689
|
+
"pushX",
|
|
3690
|
+
"pushZ",
|
|
3691
|
+
"mass",
|
|
3692
|
+
"pressure",
|
|
3693
|
+
"field",
|
|
3694
|
+
"fieldPrev",
|
|
3695
|
+
"aimX",
|
|
3696
|
+
"aimZ",
|
|
3697
|
+
"directShove",
|
|
3698
|
+
"blastStep",
|
|
3699
|
+
"balance",
|
|
3700
|
+
"balanceTimer",
|
|
3701
|
+
"fallCause",
|
|
3702
|
+
"identity",
|
|
3703
|
+
"stability",
|
|
3704
|
+
"tripFactor",
|
|
3705
|
+
"driveFactor",
|
|
3706
|
+
"reaction",
|
|
3707
|
+
"cadenceFactor",
|
|
3708
|
+
"aggression",
|
|
3709
|
+
"idle",
|
|
3710
|
+
"gaitStyle",
|
|
3711
|
+
"wounds",
|
|
3712
|
+
"leanX",
|
|
3713
|
+
"leanZ",
|
|
3714
|
+
"thrown",
|
|
3715
|
+
"prevX",
|
|
3716
|
+
"prevZ",
|
|
3717
|
+
"prevY"
|
|
1728
3718
|
];
|
|
1729
3719
|
|
|
1730
|
-
// src/types.ts
|
|
1731
|
-
var STATE = {
|
|
1732
|
-
WANDER: 0,
|
|
1733
|
-
CHASE: 1,
|
|
1734
|
-
GRAB: 2,
|
|
1735
|
-
STUNNED: 3,
|
|
1736
|
-
CALLING: 4,
|
|
1737
|
-
AIRBORNE: 5,
|
|
1738
|
-
DORMANT: 6,
|
|
1739
|
-
SWELLING: 7,
|
|
1740
|
-
SWING: 8,
|
|
1741
|
-
EATING: 9,
|
|
1742
|
-
PILE_STEP: 10,
|
|
1743
|
-
SEEKING_SOUND: 12
|
|
1744
|
-
};
|
|
1745
|
-
var MISSING = { SKULL: 1, ARM_L: 2, ARM_R: 4, LEG_L: 8, LEG_R: 16, ARM_L_BROKEN: 32, ARM_R_BROKEN: 64, LEG_L_BROKEN: 128, LEG_R_BROKEN: 256, JAW: 512 };
|
|
1746
|
-
|
|
1747
3720
|
// src/crowd.ts
|
|
1748
3721
|
var CROWD_DEFAULTS = {
|
|
1749
3722
|
sources: defaults_default.sources,
|
|
@@ -1751,6 +3724,7 @@ var GameCrowd = (() => {
|
|
|
1751
3724
|
numbers: defaults_default.numbers
|
|
1752
3725
|
};
|
|
1753
3726
|
var clamp = (v, a, b) => v < a ? a : v > b ? b : v;
|
|
3727
|
+
var NO_TRAITS = {};
|
|
1754
3728
|
var TAU_APPROX = 6.2832;
|
|
1755
3729
|
var TURN = 6.28;
|
|
1756
3730
|
function createCrowd(options) {
|
|
@@ -1772,10 +3746,25 @@ var GameCrowd = (() => {
|
|
|
1772
3746
|
const HALF = grid.half;
|
|
1773
3747
|
const GC = grid.cell;
|
|
1774
3748
|
const FW = grid.n;
|
|
3749
|
+
const physics = resolvePhysics(options.physics);
|
|
3750
|
+
const flowCells = options.flow?.cellsPerStep ?? (physics.deterministic ? DETERMINISTIC_FLOW_CELLS : void 0);
|
|
3751
|
+
const seed32 = (options.seed ?? 1) >>> 0;
|
|
3752
|
+
let serial = 0;
|
|
3753
|
+
const moundHeight = new Uint16Array(physics.mound ? FW * FW : 0);
|
|
3754
|
+
const moundOn = physics.mound !== null;
|
|
3755
|
+
const balanceOn = physics.balance !== null;
|
|
3756
|
+
const staggerSpeed = physics.pressure?.staggerSpeed ?? 1;
|
|
3757
|
+
const separationMax = physics.contact?.separationMax ?? Infinity;
|
|
3758
|
+
const RESERVED_SLOTS = 2;
|
|
3759
|
+
const WALK_DOWN = 0.6;
|
|
3760
|
+
const ATTACK_ENTER = 2.56;
|
|
3761
|
+
const ATTACK_LEAVE = 4.84;
|
|
1775
3762
|
const HC = N.hashCell;
|
|
1776
3763
|
const HN = Math.floor(grid.size / HC);
|
|
1777
|
-
const hHead = new Int32Array(HN * HN);
|
|
3764
|
+
const hHead = new Int32Array(HN * HN).fill(-1);
|
|
1778
3765
|
const hNext = new Int32Array(Z.capacity);
|
|
3766
|
+
const hashTouched = new Int32Array(Z.capacity);
|
|
3767
|
+
let hashTouchedN = 0;
|
|
1779
3768
|
const pile = new Float32Array(FW * FW);
|
|
1780
3769
|
let pileN = 0;
|
|
1781
3770
|
const flow = {
|
|
@@ -1808,7 +3797,7 @@ var GameCrowd = (() => {
|
|
|
1808
3797
|
}
|
|
1809
3798
|
const typeIndexById = new Map(types.map((type, index) => [type.id, index]));
|
|
1810
3799
|
const noises = [];
|
|
1811
|
-
const stats = { bands: [0, 0, 0, 0], slots: 0, kills: 0, heads: 0 };
|
|
3800
|
+
const stats = { bands: [0, 0, 0, 0], slots: 0, kills: 0, heads: 0, physics: createPhysicsStats() };
|
|
1812
3801
|
const corpses = createCorpseLedger({ max: N.corpsesMax, seconds: N.corpseSeconds });
|
|
1813
3802
|
const trail = [];
|
|
1814
3803
|
let trailLast = { x: 1e9, z: 1e9 };
|
|
@@ -1843,7 +3832,7 @@ var GameCrowd = (() => {
|
|
|
1843
3832
|
return list[0];
|
|
1844
3833
|
};
|
|
1845
3834
|
const T = (i) => types[Z.type[i]];
|
|
1846
|
-
const traits = (i) => T(i).traits ??
|
|
3835
|
+
const traits = (i) => T(i).traits ?? NO_TRAITS;
|
|
1847
3836
|
const special = (i) => {
|
|
1848
3837
|
const tr = traits(i);
|
|
1849
3838
|
Z.y[i] = 0;
|
|
@@ -1926,13 +3915,18 @@ var GameCrowd = (() => {
|
|
|
1926
3915
|
Z.wz[i] = z;
|
|
1927
3916
|
Z.color[i] = RO.color || pack.palette[Math.floor(R() * pack.palette.length)];
|
|
1928
3917
|
Z.skin[i] = R();
|
|
3918
|
+
seatPhysics(Z, i, { seed: seed32, serial: serial++, baseMass: physics.contact?.baseMass ?? 70, personality: physics.personality });
|
|
1929
3919
|
special(i);
|
|
1930
3920
|
emit("spawn", { agent: i, type: C.id });
|
|
1931
3921
|
return i;
|
|
1932
3922
|
};
|
|
1933
3923
|
const remove = (i) => {
|
|
1934
|
-
if (Z.state[i] === STATE.PILE_STEP)
|
|
3924
|
+
if (Z.state[i] === STATE.PILE_STEP) {
|
|
3925
|
+
if (engine.active) releasePileStep(i);
|
|
3926
|
+
else stepRelease(i);
|
|
3927
|
+
}
|
|
1935
3928
|
const j = --Z.n;
|
|
3929
|
+
engine.remap(i, j);
|
|
1936
3930
|
if (i === j) {
|
|
1937
3931
|
emit("remove", { agent: i, moved: -1 });
|
|
1938
3932
|
return;
|
|
@@ -1966,9 +3960,10 @@ var GameCrowd = (() => {
|
|
|
1966
3960
|
flow.c = 0;
|
|
1967
3961
|
flow.t = 0;
|
|
1968
3962
|
};
|
|
1969
|
-
const flowStep = (budget) => {
|
|
3963
|
+
const flowStep = (budget, cellsPerStep) => {
|
|
1970
3964
|
if (!flow.phase) return;
|
|
1971
3965
|
const t0 = clock();
|
|
3966
|
+
let cellsLeft = cellsPerStep ?? Infinity;
|
|
1972
3967
|
const D = flow.dist;
|
|
1973
3968
|
const Q = flow.q;
|
|
1974
3969
|
const F = flow.back;
|
|
@@ -1979,6 +3974,7 @@ var GameCrowd = (() => {
|
|
|
1979
3974
|
flow.phase = 2;
|
|
1980
3975
|
break;
|
|
1981
3976
|
}
|
|
3977
|
+
if (--cellsLeft < 0) return;
|
|
1982
3978
|
const c = Q[flow.qh++];
|
|
1983
3979
|
const cx = c % FW;
|
|
1984
3980
|
const cz = c / FW | 0;
|
|
@@ -1998,13 +3994,14 @@ var GameCrowd = (() => {
|
|
|
1998
3994
|
if (flow.qt < Q.length) Q[flow.qt++] = k;
|
|
1999
3995
|
}
|
|
2000
3996
|
}
|
|
2001
|
-
if ((++n & 2047) === 0 && clock() - t0 > budget) {
|
|
3997
|
+
if (cellsPerStep === void 0 && (++n & 2047) === 0 && clock() - t0 > budget) {
|
|
2002
3998
|
flow.t += clock() - t0;
|
|
2003
3999
|
return;
|
|
2004
4000
|
}
|
|
2005
4001
|
}
|
|
2006
4002
|
const ww = W[2] - W[0] + 1;
|
|
2007
4003
|
while (flow.phase === 2) {
|
|
4004
|
+
if (--cellsLeft < 0) return;
|
|
2008
4005
|
const j = flow.c++;
|
|
2009
4006
|
if (j >= ww * (W[3] - W[1] + 1)) {
|
|
2010
4007
|
const tmp = flow.dir;
|
|
@@ -2041,7 +4038,7 @@ var GameCrowd = (() => {
|
|
|
2041
4038
|
}
|
|
2042
4039
|
F[c * 2] = bx;
|
|
2043
4040
|
F[c * 2 + 1] = bz;
|
|
2044
|
-
if ((j & 8191) === 0 && clock() - t0 > budget) {
|
|
4041
|
+
if (cellsPerStep === void 0 && (j & 8191) === 0 && clock() - t0 > budget) {
|
|
2045
4042
|
flow.t += clock() - t0;
|
|
2046
4043
|
return;
|
|
2047
4044
|
}
|
|
@@ -2052,14 +4049,14 @@ var GameCrowd = (() => {
|
|
|
2052
4049
|
if (c < 0) return 0;
|
|
2053
4050
|
const k = occ[c];
|
|
2054
4051
|
if (k === 2 || k === 1 && occH[c] <= _y + 0.05) return occH[c];
|
|
2055
|
-
return pile[c];
|
|
4052
|
+
return moundOn ? pile[c] + moundHeight[c] / 1e3 : pile[c];
|
|
2056
4053
|
};
|
|
2057
4054
|
const blockedAt = (i, x, z) => {
|
|
2058
4055
|
const c = cellOf(x, z);
|
|
2059
4056
|
if (c < 0) return true;
|
|
2060
4057
|
const k = occ[c];
|
|
2061
4058
|
const y = Z.y[i];
|
|
2062
|
-
if (k === 0 || k === 3) return pile[c] > y + 0.5 && Z.state[i] !== STATE.AIRBORNE;
|
|
4059
|
+
if (k === 0 || k === 3) return (moundOn ? pile[c] + moundHeight[c] / 1e3 : pile[c]) > y + 0.5 && Z.state[i] !== STATE.AIRBORNE;
|
|
2063
4060
|
if (k === 2 && y + 0.5 >= occH[c]) return false;
|
|
2064
4061
|
if (Z.state[i] === STATE.AIRBORNE && occH[c] <= y) return false;
|
|
2065
4062
|
return true;
|
|
@@ -2079,6 +4076,7 @@ var GameCrowd = (() => {
|
|
|
2079
4076
|
pileN++;
|
|
2080
4077
|
Z.state[i] = STATE.PILE_STEP;
|
|
2081
4078
|
Z.stepCell[i] = c;
|
|
4079
|
+
Z.stepDir[i] = dir;
|
|
2082
4080
|
Z.vx[i] = Z.vz[i] = 0;
|
|
2083
4081
|
Z.y[i] = h - N.pileStep;
|
|
2084
4082
|
Z.stateTimer[i] = 6 + R() * 6;
|
|
@@ -2102,6 +4100,11 @@ var GameCrowd = (() => {
|
|
|
2102
4100
|
Z.stateTimer[i] = 1.2;
|
|
2103
4101
|
}
|
|
2104
4102
|
};
|
|
4103
|
+
const pileColumns = [];
|
|
4104
|
+
const releasePileStep = (i) => {
|
|
4105
|
+
if (Z.stepCell[i] >= 0) pileColumns.push({ cell: Z.stepCell[i], dir: Z.stepDir[i] });
|
|
4106
|
+
stepRelease(i);
|
|
4107
|
+
};
|
|
2105
4108
|
const voice = (i) => traits(i).voice ?? "groan";
|
|
2106
4109
|
const groan = (i, d) => {
|
|
2107
4110
|
const r2 = N.alertRadius * N.alertRadius;
|
|
@@ -2135,9 +4138,9 @@ var GameCrowd = (() => {
|
|
|
2135
4138
|
const attack = (i, d, tdt) => {
|
|
2136
4139
|
Z.attackTimer[i] -= tdt;
|
|
2137
4140
|
if (Z.attackTimer[i] > 0) return;
|
|
2138
|
-
Z.attackTimer[i] = N.attackEvery;
|
|
4141
|
+
Z.attackTimer[i] = N.attackEvery / Z.aggression[i];
|
|
2139
4142
|
const C = T(i);
|
|
2140
|
-
const tr = C.traits ??
|
|
4143
|
+
const tr = C.traits ?? NO_TRAITS;
|
|
2141
4144
|
if (tr.heavySwing && R() < tr.heavySwing.chance) {
|
|
2142
4145
|
Z.state[i] = STATE.SWING;
|
|
2143
4146
|
Z.stateTimer[i] = tr.heavySwing.windUp;
|
|
@@ -2146,7 +4149,7 @@ var GameCrowd = (() => {
|
|
|
2146
4149
|
const noJaw = Z.missing[i] & 512;
|
|
2147
4150
|
const noArms = (Z.missing[i] & 6) === 6;
|
|
2148
4151
|
const P = target;
|
|
2149
|
-
if (P.state.grabbedBy < 0 && !noArms && !Z.crawl[i] && R() < C.bite * (P.state.crouch ? N.crouchBite : 1)) {
|
|
4152
|
+
if (P.state.grabbedBy < 0 && !noArms && !Z.crawl[i] && R() < C.bite * (P.state.crouch ? N.crouchBite : 1) * Z.aggression[i]) {
|
|
2150
4153
|
P.grab(i, !!noJaw, !!tr.drownGrab && water(P.state.x, P.state.z));
|
|
2151
4154
|
Z.state[i] = STATE.GRAB;
|
|
2152
4155
|
} else {
|
|
@@ -2157,7 +4160,7 @@ var GameCrowd = (() => {
|
|
|
2157
4160
|
};
|
|
2158
4161
|
const effect = (payload) => emit("effect", payload);
|
|
2159
4162
|
const blood = (x, y, z, age, amount, died, agent) => effect({ effect: "blood", point: [x, y, z], age, amount, died, ...agent === void 0 ? {} : { agent } });
|
|
2160
|
-
const
|
|
4163
|
+
const damageWith = (i, hit, draw, deferDeath = false) => {
|
|
2161
4164
|
const C = T(i);
|
|
2162
4165
|
const A = pack.ages[Z.age[i]];
|
|
2163
4166
|
const RO = pack.outfits[Z.outfit[i]];
|
|
@@ -2184,7 +4187,7 @@ var GameCrowd = (() => {
|
|
|
2184
4187
|
} else {
|
|
2185
4188
|
Z.hp[i] -= dano * 3;
|
|
2186
4189
|
Z.headHp[i] -= dano;
|
|
2187
|
-
if (dano > N.jawDamage &&
|
|
4190
|
+
if (dano > N.jawDamage && draw() < N.jawChance) Z.missing[i] |= 512;
|
|
2188
4191
|
}
|
|
2189
4192
|
} else if (regiao === 1) {
|
|
2190
4193
|
let m = C.multipliers.torso;
|
|
@@ -2214,7 +4217,8 @@ var GameCrowd = (() => {
|
|
|
2214
4217
|
if (Z.hp[i] <= 0) morreu = true;
|
|
2215
4218
|
blood(Z.x[i], regiao === 0 ? Z.height[i] * 1.65 : regiao === 1 ? Z.height[i] * 1.2 : Z.height[i] * 0.7, Z.z[i], Z.age[i], regiao === 0 ? 120 : 30, morreu, i);
|
|
2216
4219
|
if (morreu) {
|
|
2217
|
-
|
|
4220
|
+
if (deferDeath) deferKill(i);
|
|
4221
|
+
else kill(i, headshot, draw);
|
|
2218
4222
|
return { died: morreu, headshot };
|
|
2219
4223
|
}
|
|
2220
4224
|
const st = Z.state[i];
|
|
@@ -2231,14 +4235,15 @@ var GameCrowd = (() => {
|
|
|
2231
4235
|
}
|
|
2232
4236
|
return { died: morreu };
|
|
2233
4237
|
};
|
|
2234
|
-
|
|
4238
|
+
const damage = (i, hit) => damageWith(i, hit, R);
|
|
4239
|
+
function kill(i, headshot = false, draw = R) {
|
|
2235
4240
|
stats.kills++;
|
|
2236
4241
|
if (headshot) stats.heads++;
|
|
2237
4242
|
const tr = traits(i);
|
|
2238
4243
|
if (tr.burst) burstAt(i, Z.x[i], Z.z[i]);
|
|
2239
4244
|
const C = T(i);
|
|
2240
4245
|
emit("kill", { agent: i, type: C.id, headshot, x: Z.x[i], z: Z.z[i] });
|
|
2241
|
-
corpseAdd(i);
|
|
4246
|
+
if (Z.balance[i] !== BALANCE.THROWN) corpseAdd(i, draw);
|
|
2242
4247
|
if (target && target.state.grabbedBy === i) target.release();
|
|
2243
4248
|
remove(i);
|
|
2244
4249
|
}
|
|
@@ -2257,24 +4262,23 @@ var GameCrowd = (() => {
|
|
|
2257
4262
|
}
|
|
2258
4263
|
emitNoise(x, z, b.noise);
|
|
2259
4264
|
};
|
|
2260
|
-
const
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
};
|
|
4265
|
+
const corpseOf = (i, seed) => ({
|
|
4266
|
+
x: Z.x[i],
|
|
4267
|
+
z: Z.z[i],
|
|
4268
|
+
yaw: Z.renderYaw[i],
|
|
4269
|
+
height: Z.height[i],
|
|
4270
|
+
build: Z.build[i],
|
|
4271
|
+
named: Z.named[i],
|
|
4272
|
+
missing: Z.missing[i],
|
|
4273
|
+
color: Z.color[i],
|
|
4274
|
+
variant: Z.variant[i],
|
|
4275
|
+
skin: Z.skin[i],
|
|
4276
|
+
seed
|
|
4277
|
+
});
|
|
4278
|
+
const corpseAdd = (i, draw = R) => void corpses.add(corpseOf(i, draw() * 10));
|
|
2275
4279
|
const ability = (i, d, dx, dz, tdt, b, alive) => {
|
|
2276
4280
|
const C = T(i);
|
|
2277
|
-
const tr = C.traits ??
|
|
4281
|
+
const tr = C.traits ?? NO_TRAITS;
|
|
2278
4282
|
const st = Z.state[i];
|
|
2279
4283
|
const P = target;
|
|
2280
4284
|
if (st === STATE.DORMANT) {
|
|
@@ -2480,6 +4484,7 @@ var GameCrowd = (() => {
|
|
|
2480
4484
|
if (tr.submerged && water(Z.x[i], Z.z[i])) return tr.submerged.swim;
|
|
2481
4485
|
return Z.chaseSpeed[i];
|
|
2482
4486
|
};
|
|
4487
|
+
const physicsDraw = (i, salt) => engine.active ? () => unit(Z.identity[i], engine.ctx.step, salt) : R;
|
|
2483
4488
|
const hurtOther = (j, amount) => {
|
|
2484
4489
|
Z.hp[j] -= amount;
|
|
2485
4490
|
blood(Z.x[j], Z.height[j] * 1.2, Z.z[j], Z.age[j], 30, false, j);
|
|
@@ -2491,7 +4496,7 @@ var GameCrowd = (() => {
|
|
|
2491
4496
|
const killQFlush = () => {
|
|
2492
4497
|
if (!killQ.length) return;
|
|
2493
4498
|
killQ.sort((a, b) => b - a);
|
|
2494
|
-
for (const j of killQ) if (j < Z.n) kill(j, false);
|
|
4499
|
+
for (const j of killQ) if (j < Z.n) kill(j, false, physicsDraw(j, SALT.CORPSE));
|
|
2495
4500
|
killQ.length = 0;
|
|
2496
4501
|
};
|
|
2497
4502
|
const frame = (i, dt, d, alive) => {
|
|
@@ -2514,9 +4519,14 @@ var GameCrowd = (() => {
|
|
|
2514
4519
|
Z.hp[i] *= tr.waterWeak.keep;
|
|
2515
4520
|
effect({ effect: "cloud", point: [Z.x[i], 0, Z.z[i]], radius: 1.5, color: tr.waterWeak.cloud, amount: 3, agent: i });
|
|
2516
4521
|
}
|
|
4522
|
+
if (engine.active && (Z.balance[i] === BALANCE.FALLEN || Z.balance[i] === BALANCE.GETTING_UP)) return;
|
|
2517
4523
|
const c = cellOf(Z.x[i], Z.z[i]);
|
|
2518
4524
|
const gy = groundAt(c, Z.y[i]);
|
|
2519
4525
|
if (st === STATE.DORMANT || Z.floorLocked[i]) return;
|
|
4526
|
+
if (engine.active && st !== STATE.AIRBORNE && Z.vy[i] === 0 && Z.y[i] > gy + 0.02 && Z.y[i] - gy <= WALK_DOWN) {
|
|
4527
|
+
Z.y[i] = Math.max(gy, Z.y[i] - N.climbSpeed * dt);
|
|
4528
|
+
return;
|
|
4529
|
+
}
|
|
2520
4530
|
if (st === STATE.AIRBORNE || Z.y[i] > gy + 0.02 || Z.vy[i] > 0) {
|
|
2521
4531
|
Z.vy[i] -= N.gravity * dt;
|
|
2522
4532
|
Z.y[i] += Z.vy[i] * dt;
|
|
@@ -2524,6 +4534,9 @@ var GameCrowd = (() => {
|
|
|
2524
4534
|
const fall = -Z.vy[i];
|
|
2525
4535
|
Z.y[i] = gy;
|
|
2526
4536
|
Z.vy[i] = 0;
|
|
4537
|
+
const collapsed = st === STATE.AIRBORNE && Z.stateTimer[i] === COLLAPSE_MARK;
|
|
4538
|
+
if (collapsed) Z.stateTimer[i] = 0;
|
|
4539
|
+
if (engine.active && balanceOn && (st !== STATE.AIRBORNE || collapsed)) engine.balance.landed(i, fall * fall / (2 * N.gravity));
|
|
2527
4540
|
if (st === STATE.AIRBORNE) {
|
|
2528
4541
|
Z.state[i] = STATE.CHASE;
|
|
2529
4542
|
if (tr.pounce && alive && d < tr.pounce.landRange && target) {
|
|
@@ -2547,6 +4560,9 @@ var GameCrowd = (() => {
|
|
|
2547
4560
|
const pz = P ? P.state.z : context.flowTarget?.[1] ?? 0;
|
|
2548
4561
|
const alive = !!P && P.state.alive && !P.state.turned;
|
|
2549
4562
|
viewYaw = context.viewYaw ?? (P ? P.state.yaw - Math.PI : 0);
|
|
4563
|
+
const bandX = context.bandCentre?.[0] ?? px;
|
|
4564
|
+
const bandZ = context.bandCentre?.[1] ?? pz;
|
|
4565
|
+
engine.beginStep(dt, { x: bandX, z: bandZ });
|
|
2550
4566
|
const fx0 = context.flowTarget?.[0] ?? px;
|
|
2551
4567
|
const fz0 = context.flowTarget?.[1] ?? pz;
|
|
2552
4568
|
const pc = cellOf(fx0, fz0);
|
|
@@ -2558,14 +4574,18 @@ var GameCrowd = (() => {
|
|
|
2558
4574
|
flowStart(fx0, fz0);
|
|
2559
4575
|
flow.cell = pc;
|
|
2560
4576
|
}
|
|
2561
|
-
flowStep(N.flowBudgetMs);
|
|
2562
|
-
|
|
4577
|
+
flowStep(N.flowBudgetMs, flowCells);
|
|
4578
|
+
if (engine.active) {
|
|
4579
|
+
for (let k = 0; k < hashTouchedN; k++) hHead[hashTouched[k]] = -1;
|
|
4580
|
+
hashTouchedN = 0;
|
|
4581
|
+
} else hHead.fill(-1);
|
|
2563
4582
|
for (let i = 0; i < Z.n; i++) {
|
|
2564
|
-
if (Z.band[i] !== 0) continue;
|
|
4583
|
+
if (Z.band[i] !== 0 || engine.active && Z.balance[i] === BALANCE.THROWN) continue;
|
|
2565
4584
|
const hx = Math.floor(Z.x[i] + HALF);
|
|
2566
4585
|
const hz = Math.floor(Z.z[i] + HALF);
|
|
2567
4586
|
if (hx < 0 || hz < 0 || hx >= HN || hz >= HN) continue;
|
|
2568
4587
|
const h = hz * HN + hx;
|
|
4588
|
+
if (engine.active && hHead[h] === -1) hashTouched[hashTouchedN++] = h;
|
|
2569
4589
|
hNext[i] = hHead[h];
|
|
2570
4590
|
hHead[h] = i;
|
|
2571
4591
|
}
|
|
@@ -2577,13 +4597,16 @@ var GameCrowd = (() => {
|
|
|
2577
4597
|
const dx = Z.x[i] - px;
|
|
2578
4598
|
const dz = Z.z[i] - pz;
|
|
2579
4599
|
const d = Math.sqrt(dx * dx + dz * dz);
|
|
4600
|
+
const bandD = context.bandCentre ? Math.hypot(Z.x[i] - bandX, Z.z[i] - bandZ) : d;
|
|
2580
4601
|
let b = Z.band[i];
|
|
2581
|
-
while (b > 0 &&
|
|
2582
|
-
while (b < 3 &&
|
|
4602
|
+
while (b > 0 && bandD < BANDS[b - 1].max - HYST) b--;
|
|
4603
|
+
while (b < 3 && bandD > BANDS[b].max + HYST) b++;
|
|
2583
4604
|
Z.band[i] = b;
|
|
2584
4605
|
B[b]++;
|
|
4606
|
+
if (engine.active && Z.balance[i] === BALANCE.THROWN) continue;
|
|
4607
|
+
const grounded = engine.active && balanceOn && (Z.balance[i] === BALANCE.FALLEN || Z.balance[i] === BALANCE.GETTING_UP);
|
|
2585
4608
|
const C = T(i);
|
|
2586
|
-
const tr = C.traits ??
|
|
4609
|
+
const tr = C.traits ?? NO_TRAITS;
|
|
2587
4610
|
if (Z.state[i] === STATE.STUNNED) {
|
|
2588
4611
|
Z.stateTimer[i] -= dt;
|
|
2589
4612
|
if (Z.stateTimer[i] <= 0) Z.state[i] = STATE.CHASE;
|
|
@@ -2607,8 +4630,12 @@ var GameCrowd = (() => {
|
|
|
2607
4630
|
Z.lost[i] = 0;
|
|
2608
4631
|
}
|
|
2609
4632
|
}
|
|
2610
|
-
Z.next[i] -= dt;
|
|
2611
|
-
|
|
4633
|
+
if (!grounded) Z.next[i] -= dt;
|
|
4634
|
+
else {
|
|
4635
|
+
Z.vx[i] = Z.vz[i] = Z.dvx[i] = Z.dvz[i] = Z.aimX[i] = Z.aimZ[i] = 0;
|
|
4636
|
+
Z.steering[i] = 0;
|
|
4637
|
+
}
|
|
4638
|
+
if (Z.next[i] <= 0 && !grounded) {
|
|
2612
4639
|
const tdt = BANDS[b].dt;
|
|
2613
4640
|
Z.next[i] += tdt;
|
|
2614
4641
|
if (alive && b <= 1 && Z.state[i] <= 1) {
|
|
@@ -2655,6 +4682,7 @@ var GameCrowd = (() => {
|
|
|
2655
4682
|
Z.stateTimer[i] = tr.caller.windUp;
|
|
2656
4683
|
}
|
|
2657
4684
|
Z.steering[i] = 0;
|
|
4685
|
+
Z.aimX[i] = Z.aimZ[i] = 0;
|
|
2658
4686
|
if (!ability(i, d, dx, dz, tdt, b, alive)) {
|
|
2659
4687
|
let tx = 0;
|
|
2660
4688
|
let tz = 0;
|
|
@@ -2691,7 +4719,7 @@ var GameCrowd = (() => {
|
|
|
2691
4719
|
if (!alive) sp = Z.wanderSpeed[i];
|
|
2692
4720
|
if (d < N.attackRange && alive && Math.abs(P.state.y - Z.y[i]) < N.attackHeight) {
|
|
2693
4721
|
sp = 0;
|
|
2694
|
-
if (slots < N.attackSlots) {
|
|
4722
|
+
if (slots < N.attackSlots - (Z.aggression[i] < 1 ? RESERVED_SLOTS : 0)) {
|
|
2695
4723
|
slots++;
|
|
2696
4724
|
attack(i, d, tdt);
|
|
2697
4725
|
}
|
|
@@ -2713,6 +4741,7 @@ var GameCrowd = (() => {
|
|
|
2713
4741
|
}
|
|
2714
4742
|
if (Z.crawl[i]) sp = Math.min(sp, N.crawlSpeed);
|
|
2715
4743
|
else if (Z.limp[i]) sp *= N.limpSpeed;
|
|
4744
|
+
if (Z.balance[i] === BALANCE.STAGGERING) sp *= staggerSpeed;
|
|
2716
4745
|
let sx = 0;
|
|
2717
4746
|
let sz = 0;
|
|
2718
4747
|
if (b === 0) {
|
|
@@ -2724,7 +4753,7 @@ var GameCrowd = (() => {
|
|
|
2724
4753
|
const qz = hz + oz;
|
|
2725
4754
|
if (qx < 0 || qz < 0 || qx >= HN || qz >= HN) continue;
|
|
2726
4755
|
for (let j = hHead[qz * HN + qx]; j !== -1; j = hNext[j]) {
|
|
2727
|
-
if (j === i || Z.state[j] === STATE.PILE_STEP || Z.state[j] === STATE.DORMANT) continue;
|
|
4756
|
+
if (j === i || Z.state[j] === STATE.PILE_STEP || Z.state[j] === STATE.DORMANT || balanceOn && (Z.balance[j] === BALANCE.FALLEN || Z.balance[j] === BALANCE.GETTING_UP)) continue;
|
|
2728
4757
|
const ex = Z.x[i] - Z.x[j];
|
|
2729
4758
|
const ez = Z.z[i] - Z.z[j];
|
|
2730
4759
|
const e2 = ex * ex + ez * ez;
|
|
@@ -2739,8 +4768,20 @@ var GameCrowd = (() => {
|
|
|
2739
4768
|
}
|
|
2740
4769
|
}
|
|
2741
4770
|
if (Z.state[i] <= 2) {
|
|
2742
|
-
|
|
2743
|
-
|
|
4771
|
+
if (separationMax !== Infinity) {
|
|
4772
|
+
const push = Math.hypot(sx, sz) * N.separationPush;
|
|
4773
|
+
if (push > separationMax) {
|
|
4774
|
+
sx *= separationMax / push;
|
|
4775
|
+
sz *= separationMax / push;
|
|
4776
|
+
}
|
|
4777
|
+
}
|
|
4778
|
+
if (engine.active && sp > 0.05) {
|
|
4779
|
+
Z.aimX[i] = tx;
|
|
4780
|
+
Z.aimZ[i] = tz;
|
|
4781
|
+
}
|
|
4782
|
+
const slow = engine.densitySlowdown(i, b);
|
|
4783
|
+
Z.dvx[i] = (tx * sp + sx * N.separationPush) * slow;
|
|
4784
|
+
Z.dvz[i] = (tz * sp + sz * N.separationPush) * slow;
|
|
2744
4785
|
Z.steering[i] = 1;
|
|
2745
4786
|
if (sp > 0.05) Z.yaw[i] = Math.atan2(tx, tz);
|
|
2746
4787
|
else if (d < 3 && alive) Z.yaw[i] = Math.atan2(-dx, -dz);
|
|
@@ -2752,17 +4793,17 @@ var GameCrowd = (() => {
|
|
|
2752
4793
|
Z.vx[i] += (Z.dvx[i] - Z.vx[i]) * k;
|
|
2753
4794
|
Z.vz[i] += (Z.dvz[i] - Z.vz[i]) * k;
|
|
2754
4795
|
}
|
|
2755
|
-
const nx = Z.x[i] + Z.vx[i] * dt;
|
|
2756
|
-
const nz = Z.z[i] + Z.vz[i] * dt;
|
|
4796
|
+
const nx = Z.x[i] + (Z.vx[i] + Z.pushX[i]) * dt;
|
|
4797
|
+
const nz = Z.z[i] + (Z.vz[i] + Z.pushZ[i]) * dt;
|
|
2757
4798
|
let hitCell = -1;
|
|
2758
4799
|
if (!blockedAt(i, nx, Z.z[i])) Z.x[i] = nx;
|
|
2759
4800
|
else {
|
|
2760
|
-
if (Z.state[i] !== STATE.AIRBORNE) Z.vx[i] = 0;
|
|
4801
|
+
if (Z.state[i] !== STATE.AIRBORNE) Z.vx[i] = Z.pushX[i] = 0;
|
|
2761
4802
|
hitCell = cellOf(nx, Z.z[i]);
|
|
2762
4803
|
}
|
|
2763
4804
|
if (!blockedAt(i, Z.x[i], nz)) Z.z[i] = nz;
|
|
2764
4805
|
else {
|
|
2765
|
-
if (Z.state[i] !== STATE.AIRBORNE) Z.vz[i] = 0;
|
|
4806
|
+
if (Z.state[i] !== STATE.AIRBORNE) Z.vz[i] = Z.pushZ[i] = 0;
|
|
2766
4807
|
hitCell = cellOf(Z.x[i], nz);
|
|
2767
4808
|
}
|
|
2768
4809
|
if (hitCell >= 0 && Z.state[i] === STATE.CHASE && b === 0) {
|
|
@@ -2774,7 +4815,7 @@ var GameCrowd = (() => {
|
|
|
2774
4815
|
Z.vx[i] = Math.sin(Z.yaw[i]) * s2;
|
|
2775
4816
|
Z.vz[i] = Math.cos(Z.yaw[i]) * s2;
|
|
2776
4817
|
} else if (occ[hitCell] === 2 || occ[hitCell] === 0 && pile[hitCell] > 0) {
|
|
2777
|
-
Z.stuck[i] += dt;
|
|
4818
|
+
Z.stuck[i] += engine.active && balanceOn ? dt * (1 + engine.pressureRatio(i)) : dt;
|
|
2778
4819
|
if (Z.stuck[i] > 0.35 + 0.5 * Z.skin[i]) {
|
|
2779
4820
|
Z.stuck[i] = 0;
|
|
2780
4821
|
tryStep(i, hitCell);
|
|
@@ -2797,12 +4838,18 @@ var GameCrowd = (() => {
|
|
|
2797
4838
|
if (goal && solidAt(Z.x[i] + Math.sin(Z.renderYaw[i]) * 0.62, Z.z[i] + Math.cos(Z.renderYaw[i]) * 0.62, 1)) goal = 0.1;
|
|
2798
4839
|
Z.arms[i] += (goal - Z.arms[i]) * Math.min(1, dt * (goal > Z.arms[i] ? 3 : 5));
|
|
2799
4840
|
}
|
|
2800
|
-
Z.phase[i] += dt * (Z.renderSpeed[i] < 0.05 ? 0.15 : N.cadenceBase + N.cadencePerSpeed * Z.renderSpeed[i]);
|
|
4841
|
+
Z.phase[i] += dt * (Z.renderSpeed[i] < 0.05 ? 0.15 : N.cadenceBase + N.cadencePerSpeed * Z.renderSpeed[i]) * Z.cadenceFactor[i];
|
|
4842
|
+
if (physics.deterministic) {
|
|
4843
|
+
const attacking = Z.state[i] === STATE.CHASE && alive;
|
|
4844
|
+
Z.attackPose[i] = attacking && (Z.x[i] - P.state.x) ** 2 + (Z.z[i] - P.state.z) ** 2 < (Z.attackPose[i] ? ATTACK_LEAVE : ATTACK_ENTER) ? 1 : 0;
|
|
4845
|
+
}
|
|
2801
4846
|
if (spd > 0.35 && Z.state[i] <= 1 && !Z.attackPose[i]) Z.yaw[i] = Math.atan2(Z.vx[i], Z.vz[i]);
|
|
2802
4847
|
let dy = Z.yaw[i] - Z.renderYaw[i];
|
|
2803
4848
|
dy -= Math.round(dy / TAU_APPROX) * TAU_APPROX;
|
|
2804
4849
|
Z.renderYaw[i] += dy * Math.min(1, dt * (Z.state[i] === STATE.AIRBORNE ? 20 : 9));
|
|
2805
4850
|
}
|
|
4851
|
+
engine.endStep();
|
|
4852
|
+
if (pileColumns.length) collapsePileColumns(Z, pileColumns, FW * FW, stepRelease);
|
|
2806
4853
|
killQFlush();
|
|
2807
4854
|
noises.length = 0;
|
|
2808
4855
|
stats.slots = slots;
|
|
@@ -2866,6 +4913,31 @@ var GameCrowd = (() => {
|
|
|
2866
4913
|
if (m) emit("message", { id: "migration", seconds: 4 });
|
|
2867
4914
|
}
|
|
2868
4915
|
};
|
|
4916
|
+
const engine = createPhysics(
|
|
4917
|
+
{
|
|
4918
|
+
store: Z,
|
|
4919
|
+
grid,
|
|
4920
|
+
doors,
|
|
4921
|
+
gravity: N.gravity,
|
|
4922
|
+
attackEvery: N.attackEvery,
|
|
4923
|
+
types,
|
|
4924
|
+
moundHeight,
|
|
4925
|
+
corpsesMax: N.corpsesMax,
|
|
4926
|
+
typeOf: T,
|
|
4927
|
+
target: () => target,
|
|
4928
|
+
emit,
|
|
4929
|
+
damage: (i, hit, draw) => damageWith(i, hit, draw, true),
|
|
4930
|
+
deferKill,
|
|
4931
|
+
releasePileStep,
|
|
4932
|
+
corpseOf,
|
|
4933
|
+
corpseList: () => corpses.entries,
|
|
4934
|
+
addCorpse: (corpse) => void corpses.add(corpse),
|
|
4935
|
+
groundAt,
|
|
4936
|
+
pileBlast
|
|
4937
|
+
},
|
|
4938
|
+
physics,
|
|
4939
|
+
stats.physics
|
|
4940
|
+
);
|
|
2869
4941
|
const crowd = {
|
|
2870
4942
|
store: Z,
|
|
2871
4943
|
pack,
|
|
@@ -2875,6 +4947,9 @@ var GameCrowd = (() => {
|
|
|
2875
4947
|
numbers: N,
|
|
2876
4948
|
stats,
|
|
2877
4949
|
pile,
|
|
4950
|
+
mound: moundHeight,
|
|
4951
|
+
thrown: engine.thrown,
|
|
4952
|
+
blast: (blast) => engine.blasts.enqueue(blast),
|
|
2878
4953
|
pileCount: () => pileN,
|
|
2879
4954
|
trail,
|
|
2880
4955
|
variantsOf,
|
|
@@ -2891,6 +4966,7 @@ var GameCrowd = (() => {
|
|
|
2891
4966
|
spawn: (x, z, type = -1) => spawn(x, z, type),
|
|
2892
4967
|
remove,
|
|
2893
4968
|
update,
|
|
4969
|
+
dispose: () => engine.dispose(),
|
|
2894
4970
|
emitNoise,
|
|
2895
4971
|
damage,
|
|
2896
4972
|
kill: (i, headshot = false) => kill(i, headshot),
|