@adatechnology/game-crowd 0.5.4 → 0.6.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 +8 -7
- package/dist/index.d.ts +172 -7
- package/dist/index.global.js +404 -122
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +305 -115
- package/dist/index.js.map +1 -1
- package/dist/physics-worker.js +2 -2
- package/dist/physics-worker.js.map +1 -1
- package/package.json +4 -3
package/dist/index.js
CHANGED
|
@@ -183,17 +183,22 @@ function createNavGrid(options = {}) {
|
|
|
183
183
|
const c = cellOf(x, z);
|
|
184
184
|
return c < 0 || occ[c] !== 0 && occ[c] !== 3 && occH[c] > y;
|
|
185
185
|
};
|
|
186
|
+
let version = 0;
|
|
186
187
|
const fillRect = (x0, z0, x1, z1, kind, h) => {
|
|
187
188
|
const a = Math.floor((x0 + half) / cell);
|
|
188
189
|
const b = Math.ceil((x1 + half) / cell);
|
|
189
190
|
const c = Math.floor((z0 + half) / cell);
|
|
190
191
|
const d = Math.ceil((z1 + half) / cell);
|
|
192
|
+
let changed = false;
|
|
191
193
|
for (let gz = Math.max(0, c); gz < Math.min(n, d); gz++)
|
|
192
194
|
for (let gx = Math.max(0, a); gx < Math.min(n, b); gx++) {
|
|
193
195
|
const i = gz * n + gx;
|
|
196
|
+
if (occ[i] === kind && occH[i] === Math.fround(h)) continue;
|
|
194
197
|
occ[i] = kind;
|
|
195
198
|
occH[i] = h;
|
|
199
|
+
changed = true;
|
|
196
200
|
}
|
|
201
|
+
if (changed) version++;
|
|
197
202
|
};
|
|
198
203
|
if (options.border ?? true) {
|
|
199
204
|
fillRect(-half, -half, half, -half + BORDER_WIDTH, 1, BORDER_HEIGHT);
|
|
@@ -207,6 +212,9 @@ function createNavGrid(options = {}) {
|
|
|
207
212
|
cell,
|
|
208
213
|
n,
|
|
209
214
|
occ,
|
|
215
|
+
get version() {
|
|
216
|
+
return version;
|
|
217
|
+
},
|
|
210
218
|
occH,
|
|
211
219
|
cellOf,
|
|
212
220
|
solidAt,
|
|
@@ -1032,24 +1040,8 @@ function createMound(ctx, extraBodies, height) {
|
|
|
1032
1040
|
return { height, step, weightOn };
|
|
1033
1041
|
}
|
|
1034
1042
|
|
|
1035
|
-
// src/physics/offload/
|
|
1036
|
-
|
|
1037
|
-
if (typeof Worker === "undefined") return null;
|
|
1038
|
-
let url;
|
|
1039
|
-
try {
|
|
1040
|
-
url = new URL("./physics-worker.js", import.meta.url);
|
|
1041
|
-
} catch {
|
|
1042
|
-
return null;
|
|
1043
|
-
}
|
|
1044
|
-
return () => {
|
|
1045
|
-
const worker = new Worker(url, { type: "module" });
|
|
1046
|
-
return {
|
|
1047
|
-
postMessage: (message, transfer) => worker.postMessage(message, transfer ?? []),
|
|
1048
|
-
onMessage: (listener) => worker.addEventListener("message", (event) => listener(event.data)),
|
|
1049
|
-
terminate: () => worker.terminate()
|
|
1050
|
-
};
|
|
1051
|
-
};
|
|
1052
|
-
}
|
|
1043
|
+
// src/physics/offload/kind.ts
|
|
1044
|
+
import { WORKER_KIND_DEFAULTS, WORKER_KIND_ID } from "@adatechnology/game-contracts";
|
|
1053
1045
|
|
|
1054
1046
|
// src/physics/offload/frame.ts
|
|
1055
1047
|
var F32_INPUTS = ["x", "z", "y", "vx", "vz", "pushX", "pushZ", "mass", "build", "aimX", "aimZ", "driveFactor", "fieldPrev"];
|
|
@@ -1058,7 +1050,7 @@ var F32_OUTPUTS = ["deltaX", "deltaZ", "deltaPushX", "deltaPushZ", "deltaVx", "d
|
|
|
1058
1050
|
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 };
|
|
1059
1051
|
var align8 = (bytes) => Math.ceil(bytes / 8) * 8;
|
|
1060
1052
|
var frameBytes = ({ capacity, cells }) => HEADER.size * 8 + (F32_INPUTS.length + F32_OUTPUTS.length + 2) * align8(capacity * 4) + U8_INPUTS.length * align8(capacity) + align8(cells);
|
|
1061
|
-
function createFrame(buffer, shape) {
|
|
1053
|
+
function createFrame(buffer, shape, occupancy) {
|
|
1062
1054
|
const { capacity, cells } = shape;
|
|
1063
1055
|
let offset = 0;
|
|
1064
1056
|
const take = (make, bytesPerElement, length) => {
|
|
@@ -1076,97 +1068,11 @@ function createFrame(buffer, shape) {
|
|
|
1076
1068
|
const doorCell = take((b, o, l) => new Int32Array(b, o, l), 4, capacity);
|
|
1077
1069
|
const bytes = {};
|
|
1078
1070
|
for (const key of U8_INPUTS) bytes[key] = take(u8, 1, capacity);
|
|
1079
|
-
const occ = take(u8, 1, cells);
|
|
1071
|
+
const occ = occupancy ?? take(u8, 1, cells);
|
|
1080
1072
|
return { header, f32: floats, u8: bytes, identity, doorCell, occ };
|
|
1081
1073
|
}
|
|
1082
1074
|
var INPUT_KEYS = { f32: F32_INPUTS, u8: U8_INPUTS };
|
|
1083
1075
|
|
|
1084
|
-
// src/physics/offload/offload.ts
|
|
1085
|
-
var isOpen2 = (grid, x, z) => {
|
|
1086
|
-
const cell = grid.cellOf(x, z);
|
|
1087
|
-
return cell >= 0 && (grid.occ[cell] === 0 || grid.occ[cell] === 3);
|
|
1088
|
-
};
|
|
1089
|
-
function writeSnapshot(frame, source, h, player) {
|
|
1090
|
-
const { store, grid } = source;
|
|
1091
|
-
const n = store.n;
|
|
1092
|
-
for (const key of INPUT_KEYS.f32) frame.f32[key].set(store[key].subarray(0, n));
|
|
1093
|
-
for (const key of INPUT_KEYS.u8) frame.u8[key].set(store[key].subarray(0, n));
|
|
1094
|
-
frame.identity.set(store.identity.subarray(0, n));
|
|
1095
|
-
frame.occ.set(grid.occ);
|
|
1096
|
-
frame.header[HEADER.n] = n;
|
|
1097
|
-
frame.header[HEADER.h] = h;
|
|
1098
|
-
frame.header[HEADER.playerOn] = player ? 1 : 0;
|
|
1099
|
-
frame.header[HEADER.playerX] = player?.x ?? 0;
|
|
1100
|
-
frame.header[HEADER.playerZ] = player?.z ?? 0;
|
|
1101
|
-
}
|
|
1102
|
-
function createOffload(runner, host, latency = 1) {
|
|
1103
|
-
const { store, grid, stats, contact } = host;
|
|
1104
|
-
const queue = [];
|
|
1105
|
-
const spare = Array.from({ length: latency }, () => ({ count: 0, origin: null, originBuffer: new Int32Array(store.capacity) }));
|
|
1106
|
-
const result = { playerLoad: 0 };
|
|
1107
|
-
const apply = (frame, pending) => {
|
|
1108
|
-
const { f32, header } = frame;
|
|
1109
|
-
const { origin, count } = pending;
|
|
1110
|
-
for (let agent = 0; agent < store.n; agent++) {
|
|
1111
|
-
const from = origin ? origin[agent] : agent < count ? agent : -1;
|
|
1112
|
-
if (from < 0) {
|
|
1113
|
-
contact.workspace.shove[agent] = 0;
|
|
1114
|
-
continue;
|
|
1115
|
-
}
|
|
1116
|
-
const dx = f32.deltaX[from];
|
|
1117
|
-
const dz = f32.deltaZ[from];
|
|
1118
|
-
if (dx !== 0 && isOpen2(grid, store.x[agent] + dx, store.z[agent])) store.x[agent] += dx;
|
|
1119
|
-
if (dz !== 0 && isOpen2(grid, store.x[agent], store.z[agent] + dz)) store.z[agent] += dz;
|
|
1120
|
-
store.pushX[agent] += f32.deltaPushX[from];
|
|
1121
|
-
store.pushZ[agent] += f32.deltaPushZ[from];
|
|
1122
|
-
store.vx[agent] += f32.deltaVx[from];
|
|
1123
|
-
store.vz[agent] += f32.deltaVz[from];
|
|
1124
|
-
store.field[agent] = f32.field[from];
|
|
1125
|
-
store.fieldPrev[agent] = f32.field[from];
|
|
1126
|
-
contact.workspace.shove[agent] = f32.shove[from];
|
|
1127
|
-
const cell = frame.doorCell[from];
|
|
1128
|
-
if (cell >= 0) host.addDoorLoad(cell, f32.doorLoad[from]);
|
|
1129
|
-
}
|
|
1130
|
-
stats.contacts = header[HEADER.contacts];
|
|
1131
|
-
stats.maxOverlap = header[HEADER.maxOverlap];
|
|
1132
|
-
stats.meanOverlap = header[HEADER.contacts] ? header[HEADER.overlapSum] / header[HEADER.contacts] : 0;
|
|
1133
|
-
stats.fieldMax = header[HEADER.fieldMax];
|
|
1134
|
-
result.playerLoad = header[HEADER.playerLoad];
|
|
1135
|
-
};
|
|
1136
|
-
const step = (h, player) => {
|
|
1137
|
-
result.playerLoad = 0;
|
|
1138
|
-
if (queue.length >= latency) {
|
|
1139
|
-
const frame2 = runner.settle();
|
|
1140
|
-
if (frame2) {
|
|
1141
|
-
const pending = queue.shift();
|
|
1142
|
-
apply(frame2, pending);
|
|
1143
|
-
pending.origin = null;
|
|
1144
|
-
spare.push(pending);
|
|
1145
|
-
}
|
|
1146
|
-
}
|
|
1147
|
-
const frame = runner.frame();
|
|
1148
|
-
if (frame && spare.length) {
|
|
1149
|
-
writeSnapshot(frame, { store, grid }, h, player);
|
|
1150
|
-
runner.submit();
|
|
1151
|
-
const pending = spare.shift();
|
|
1152
|
-
pending.count = store.n;
|
|
1153
|
-
queue.push(pending);
|
|
1154
|
-
}
|
|
1155
|
-
return result;
|
|
1156
|
-
};
|
|
1157
|
-
const remap = (removed, moved) => {
|
|
1158
|
-
queue.forEach((pending) => {
|
|
1159
|
-
if (!pending.origin) {
|
|
1160
|
-
pending.origin = pending.originBuffer.fill(-1);
|
|
1161
|
-
for (let agent = 0; agent < pending.count; agent++) pending.origin[agent] = agent;
|
|
1162
|
-
}
|
|
1163
|
-
pending.origin[removed] = removed === moved ? -1 : pending.origin[moved];
|
|
1164
|
-
if (removed !== moved) pending.origin[moved] = -1;
|
|
1165
|
-
});
|
|
1166
|
-
};
|
|
1167
|
-
return { step, remap, dispose: () => runner.dispose() };
|
|
1168
|
-
}
|
|
1169
|
-
|
|
1170
1076
|
// src/physics/pressure.ts
|
|
1171
1077
|
var TOUCH_SLACK = 0.08;
|
|
1172
1078
|
var WALL_PROBE = 0.12;
|
|
@@ -1390,6 +1296,176 @@ function createKernel(setup) {
|
|
|
1390
1296
|
return { run };
|
|
1391
1297
|
}
|
|
1392
1298
|
|
|
1299
|
+
// src/physics/offload/kind.ts
|
|
1300
|
+
var crowdPhysicsKind = Object.freeze({
|
|
1301
|
+
id: WORKER_KIND_ID.CROWD_PHYSICS,
|
|
1302
|
+
kind: "resident",
|
|
1303
|
+
...WORKER_KIND_DEFAULTS[WORKER_KIND_ID.CROWD_PHYSICS],
|
|
1304
|
+
create(init) {
|
|
1305
|
+
const kernel = createKernel(init.setup);
|
|
1306
|
+
return {
|
|
1307
|
+
step(buffer) {
|
|
1308
|
+
kernel.run(createFrame(buffer, init.shape, init.occupancy));
|
|
1309
|
+
return buffer;
|
|
1310
|
+
}
|
|
1311
|
+
};
|
|
1312
|
+
},
|
|
1313
|
+
transferOutput: (buffer) => [buffer]
|
|
1314
|
+
});
|
|
1315
|
+
function createCoordinatorRunner(setup, shape, coordinator, slots = 1, grid) {
|
|
1316
|
+
const frameShape = grid ? { capacity: shape.capacity, cells: 0 } : shape;
|
|
1317
|
+
const open = () => {
|
|
1318
|
+
const occupancy = grid?.occ.slice();
|
|
1319
|
+
return { resident: coordinator.resident(crowdPhysicsKind, { setup, shape: frameShape, ...occupancy ? { occupancy } : {} }), pending: 0, retired: false };
|
|
1320
|
+
};
|
|
1321
|
+
let current = open();
|
|
1322
|
+
let version = grid?.version ?? 0;
|
|
1323
|
+
const buffers = Array.from({ length: slots }, () => new ArrayBuffer(frameBytes(frameShape)));
|
|
1324
|
+
const frames = buffers.map((buffer) => createFrame(buffer, frameShape));
|
|
1325
|
+
const handles = [];
|
|
1326
|
+
const views = /* @__PURE__ */ new WeakMap();
|
|
1327
|
+
let submitted = 0;
|
|
1328
|
+
let settled = 0;
|
|
1329
|
+
const release = (owner) => {
|
|
1330
|
+
if (owner.retired && owner.pending === 0) owner.resident.dispose();
|
|
1331
|
+
};
|
|
1332
|
+
return {
|
|
1333
|
+
transport: "transfer",
|
|
1334
|
+
get inFlight() {
|
|
1335
|
+
return submitted - settled;
|
|
1336
|
+
},
|
|
1337
|
+
frame: () => submitted - settled < slots ? frames[submitted % slots] : null,
|
|
1338
|
+
submit: () => {
|
|
1339
|
+
if (grid && grid.version !== version) {
|
|
1340
|
+
version = grid.version;
|
|
1341
|
+
current.retired = true;
|
|
1342
|
+
release(current);
|
|
1343
|
+
current = open();
|
|
1344
|
+
}
|
|
1345
|
+
const owner = current;
|
|
1346
|
+
owner.pending++;
|
|
1347
|
+
handles.push({ handle: owner.resident.step(buffers[submitted % slots], { frame: submitted, latencyFrames: slots }), owner });
|
|
1348
|
+
submitted++;
|
|
1349
|
+
},
|
|
1350
|
+
settle: () => {
|
|
1351
|
+
const next = handles.shift();
|
|
1352
|
+
if (!next) return null;
|
|
1353
|
+
settled++;
|
|
1354
|
+
let out;
|
|
1355
|
+
try {
|
|
1356
|
+
out = next.handle.collect(next.handle.dueFrame);
|
|
1357
|
+
} finally {
|
|
1358
|
+
next.owner.pending--;
|
|
1359
|
+
release(next.owner);
|
|
1360
|
+
}
|
|
1361
|
+
let view = views.get(out);
|
|
1362
|
+
if (!view) views.set(out, view = createFrame(out, frameShape));
|
|
1363
|
+
return view;
|
|
1364
|
+
},
|
|
1365
|
+
dispose: () => {
|
|
1366
|
+
current.resident.dispose();
|
|
1367
|
+
for (const { owner } of handles) owner.resident.dispose();
|
|
1368
|
+
}
|
|
1369
|
+
};
|
|
1370
|
+
}
|
|
1371
|
+
|
|
1372
|
+
// src/physics/offload/offload.ts
|
|
1373
|
+
var isOpen2 = (grid, x, z) => {
|
|
1374
|
+
const cell = grid.cellOf(x, z);
|
|
1375
|
+
return cell >= 0 && (grid.occ[cell] === 0 || grid.occ[cell] === 3);
|
|
1376
|
+
};
|
|
1377
|
+
var clearStats = (stats) => {
|
|
1378
|
+
stats.contacts = 0;
|
|
1379
|
+
stats.maxOverlap = 0;
|
|
1380
|
+
stats.meanOverlap = 0;
|
|
1381
|
+
stats.fieldMax = 0;
|
|
1382
|
+
};
|
|
1383
|
+
function writeSnapshot(frame, source, h, player) {
|
|
1384
|
+
const { store, grid } = source;
|
|
1385
|
+
const n = store.n;
|
|
1386
|
+
for (const key of INPUT_KEYS.f32) frame.f32[key].set(store[key].subarray(0, n));
|
|
1387
|
+
for (const key of INPUT_KEYS.u8) frame.u8[key].set(store[key].subarray(0, n));
|
|
1388
|
+
frame.identity.set(store.identity.subarray(0, n));
|
|
1389
|
+
if (frame.occ.length > 0) frame.occ.set(grid.occ);
|
|
1390
|
+
frame.header[HEADER.n] = n;
|
|
1391
|
+
frame.header[HEADER.h] = h;
|
|
1392
|
+
frame.header[HEADER.playerOn] = player ? 1 : 0;
|
|
1393
|
+
frame.header[HEADER.playerX] = player?.x ?? 0;
|
|
1394
|
+
frame.header[HEADER.playerZ] = player?.z ?? 0;
|
|
1395
|
+
}
|
|
1396
|
+
function createOffload(runner, host, latency = 1) {
|
|
1397
|
+
const { store, grid, stats, contact } = host;
|
|
1398
|
+
const queue = [];
|
|
1399
|
+
const spare = Array.from({ length: latency }, () => ({ count: 0, origin: null, originBuffer: new Int32Array(store.capacity) }));
|
|
1400
|
+
const result = { playerLoad: 0 };
|
|
1401
|
+
const apply = (frame, pending) => {
|
|
1402
|
+
const { f32, header } = frame;
|
|
1403
|
+
const { origin, count } = pending;
|
|
1404
|
+
for (let agent = 0; agent < store.n; agent++) {
|
|
1405
|
+
const from = origin ? origin[agent] : agent < count ? agent : -1;
|
|
1406
|
+
if (from < 0) {
|
|
1407
|
+
contact.workspace.shove[agent] = 0;
|
|
1408
|
+
continue;
|
|
1409
|
+
}
|
|
1410
|
+
const dx = f32.deltaX[from];
|
|
1411
|
+
const dz = f32.deltaZ[from];
|
|
1412
|
+
if (dx !== 0 && isOpen2(grid, store.x[agent] + dx, store.z[agent])) store.x[agent] += dx;
|
|
1413
|
+
if (dz !== 0 && isOpen2(grid, store.x[agent], store.z[agent] + dz)) store.z[agent] += dz;
|
|
1414
|
+
store.pushX[agent] += f32.deltaPushX[from];
|
|
1415
|
+
store.pushZ[agent] += f32.deltaPushZ[from];
|
|
1416
|
+
store.vx[agent] += f32.deltaVx[from];
|
|
1417
|
+
store.vz[agent] += f32.deltaVz[from];
|
|
1418
|
+
store.field[agent] = f32.field[from];
|
|
1419
|
+
store.fieldPrev[agent] = f32.field[from];
|
|
1420
|
+
contact.workspace.shove[agent] = f32.shove[from];
|
|
1421
|
+
const cell = frame.doorCell[from];
|
|
1422
|
+
if (cell >= 0) host.addDoorLoad(cell, f32.doorLoad[from]);
|
|
1423
|
+
}
|
|
1424
|
+
stats.contacts = header[HEADER.contacts];
|
|
1425
|
+
stats.maxOverlap = header[HEADER.maxOverlap];
|
|
1426
|
+
stats.meanOverlap = header[HEADER.contacts] ? header[HEADER.overlapSum] / header[HEADER.contacts] : 0;
|
|
1427
|
+
stats.fieldMax = header[HEADER.fieldMax];
|
|
1428
|
+
result.playerLoad = header[HEADER.playerLoad];
|
|
1429
|
+
};
|
|
1430
|
+
const step = (h, player) => {
|
|
1431
|
+
result.playerLoad = 0;
|
|
1432
|
+
if (queue.length >= latency || store.n === 0 && queue.length > 0) {
|
|
1433
|
+
const frame2 = runner.settle();
|
|
1434
|
+
if (frame2) {
|
|
1435
|
+
const pending = queue.shift();
|
|
1436
|
+
apply(frame2, pending);
|
|
1437
|
+
pending.origin = null;
|
|
1438
|
+
spare.push(pending);
|
|
1439
|
+
}
|
|
1440
|
+
}
|
|
1441
|
+
if (store.n === 0) {
|
|
1442
|
+
contact.workspace.shove.fill(0);
|
|
1443
|
+
if (queue.length === 0) clearStats(stats);
|
|
1444
|
+
return result;
|
|
1445
|
+
}
|
|
1446
|
+
const frame = runner.frame();
|
|
1447
|
+
if (frame && spare.length) {
|
|
1448
|
+
writeSnapshot(frame, { store, grid }, h, player);
|
|
1449
|
+
runner.submit();
|
|
1450
|
+
const pending = spare.shift();
|
|
1451
|
+
pending.count = store.n;
|
|
1452
|
+
queue.push(pending);
|
|
1453
|
+
}
|
|
1454
|
+
return result;
|
|
1455
|
+
};
|
|
1456
|
+
const remap = (removed, moved) => {
|
|
1457
|
+
queue.forEach((pending) => {
|
|
1458
|
+
if (!pending.origin) {
|
|
1459
|
+
pending.origin = pending.originBuffer.fill(-1);
|
|
1460
|
+
for (let agent = 0; agent < pending.count; agent++) pending.origin[agent] = agent;
|
|
1461
|
+
}
|
|
1462
|
+
pending.origin[removed] = removed === moved ? -1 : pending.origin[moved];
|
|
1463
|
+
if (removed !== moved) pending.origin[moved] = -1;
|
|
1464
|
+
});
|
|
1465
|
+
};
|
|
1466
|
+
return { step, remap, dispose: () => runner.dispose() };
|
|
1467
|
+
}
|
|
1468
|
+
|
|
1393
1469
|
// src/physics/offload/worker-host.ts
|
|
1394
1470
|
var CONTROL = { IDLE: 0, SUBMITTED: 1, DONE: 2, STOP: 3 };
|
|
1395
1471
|
function attachPhysicsWorker(port) {
|
|
@@ -1809,12 +1885,13 @@ function createPhysics(base, options, stats = createPhysicsStats()) {
|
|
|
1809
1885
|
const gridCells = grid.occ.length;
|
|
1810
1886
|
const shape = { capacity: store.capacity, cells: gridCells };
|
|
1811
1887
|
const setup = { capacity: store.capacity, gridSize: grid.size, gridCell: grid.cell, contact: options.contact, pressure: options.pressure, driveForceOfType: Array.from(driveForceOfType) };
|
|
1812
|
-
const transport = options.workerTransport ?? (isSharedAvailable() ? "shared" : "transfer");
|
|
1813
1888
|
const slots = options.workerLatency;
|
|
1889
|
+
if (options.workers) return createCoordinatorRunner(setup, shape, options.workers, slots, grid);
|
|
1890
|
+
const transport = options.workerTransport ?? (isSharedAvailable() ? "shared" : "transfer");
|
|
1814
1891
|
if (transport === "inline") return createInlineRunner(setup, shape, slots);
|
|
1815
|
-
const factory = options.workerFactory
|
|
1892
|
+
const factory = options.workerFactory;
|
|
1816
1893
|
if (!factory) {
|
|
1817
|
-
if (options.worker === "on") throw new Error('crowd physics: worker "on" but no Worker
|
|
1894
|
+
if (options.worker === "on") throw new Error('crowd physics: worker "on" but no Worker was provided (give physics.workers or physics.workerFactory)');
|
|
1818
1895
|
return null;
|
|
1819
1896
|
}
|
|
1820
1897
|
return transport === "shared" ? createSharedRunner(setup, shape, factory, slots) : createTransferRunner(setup, shape, factory, slots);
|
|
@@ -2083,10 +2160,11 @@ function resolvePhysics(physics = {}) {
|
|
|
2083
2160
|
doorPressureDamage: physics.door?.pressureDamage ?? DOOR_PRESSURE_DAMAGE,
|
|
2084
2161
|
playerPressure,
|
|
2085
2162
|
deterministic: physics.deterministic ?? false,
|
|
2086
|
-
worker: physics.worker ?? "off",
|
|
2163
|
+
worker: physics.worker ?? (physics.workers ? "auto" : "off"),
|
|
2087
2164
|
workerFactory: physics.workerFactory,
|
|
2088
2165
|
workerTransport: physics.workerTransport,
|
|
2089
|
-
workerLatency: physics.workerLatency ?? 1
|
|
2166
|
+
workerLatency: physics.workerLatency ?? 1,
|
|
2167
|
+
workers: physics.workers
|
|
2090
2168
|
};
|
|
2091
2169
|
}
|
|
2092
2170
|
|
|
@@ -2397,7 +2475,7 @@ function createCrowd(options) {
|
|
|
2397
2475
|
const GC = grid.cell;
|
|
2398
2476
|
const FW = grid.n;
|
|
2399
2477
|
const physics = resolvePhysics(options.physics);
|
|
2400
|
-
const flowCells = options.flow?.cellsPerStep ?? (physics.deterministic ? DETERMINISTIC_FLOW_CELLS : void 0);
|
|
2478
|
+
const flowCells = options.flow?.cellsPerStep ?? (physics.deterministic || !options.flow?.clock ? DETERMINISTIC_FLOW_CELLS : void 0);
|
|
2401
2479
|
const seed32 = (options.seed ?? 1) >>> 0;
|
|
2402
2480
|
let serial = 0;
|
|
2403
2481
|
const moundHeight = new Uint16Array(physics.mound ? FW * FW : 0);
|
|
@@ -2612,7 +2690,7 @@ function createCrowd(options) {
|
|
|
2612
2690
|
};
|
|
2613
2691
|
const flowStep = (budget, cellsPerStep) => {
|
|
2614
2692
|
if (!flow.phase) return;
|
|
2615
|
-
const t0 = clock();
|
|
2693
|
+
const t0 = cellsPerStep === void 0 ? clock() : 0;
|
|
2616
2694
|
let cellsLeft = cellsPerStep ?? Infinity;
|
|
2617
2695
|
const D = flow.dist;
|
|
2618
2696
|
const Q = flow.q;
|
|
@@ -2661,7 +2739,7 @@ function createCrowd(options) {
|
|
|
2661
2739
|
flow.win = W;
|
|
2662
2740
|
flow.winB = tw;
|
|
2663
2741
|
flow.phase = 0;
|
|
2664
|
-
flow.ms = flow.t + clock() - t0;
|
|
2742
|
+
flow.ms = cellsPerStep === void 0 ? flow.t + clock() - t0 : 0;
|
|
2665
2743
|
return;
|
|
2666
2744
|
}
|
|
2667
2745
|
const cx = W[0] + j % ww;
|
|
@@ -4421,6 +4499,108 @@ function bindInfection(crowd, model, options) {
|
|
|
4421
4499
|
}
|
|
4422
4500
|
};
|
|
4423
4501
|
}
|
|
4502
|
+
|
|
4503
|
+
// src/sim/horde.ts
|
|
4504
|
+
var HORDE_COMMAND = {
|
|
4505
|
+
/** `[TARGET, x, z, yaw, alive]` */
|
|
4506
|
+
TARGET: 1,
|
|
4507
|
+
/** `[BLAST, x, y, z, radius, energy J]` */
|
|
4508
|
+
BLAST: 2
|
|
4509
|
+
};
|
|
4510
|
+
var HORDE_SIM_ID = "crowd.horde";
|
|
4511
|
+
var HEADER2 = 16;
|
|
4512
|
+
var align82 = (n) => Math.ceil(n / 8) * 8;
|
|
4513
|
+
function hordeSnapshotLayout(store) {
|
|
4514
|
+
let offset = HEADER2;
|
|
4515
|
+
const fields = [];
|
|
4516
|
+
for (const key of [...AGENT_FIELDS, "armHp", "legHp"]) {
|
|
4517
|
+
const array = store[key];
|
|
4518
|
+
const per = array.length / store.capacity;
|
|
4519
|
+
fields.push({ key, per, bytes: array.BYTES_PER_ELEMENT, offset });
|
|
4520
|
+
offset = align82(offset + array.byteLength);
|
|
4521
|
+
}
|
|
4522
|
+
return { bytes: offset, fields };
|
|
4523
|
+
}
|
|
4524
|
+
var bytesOf = (array, count) => new Uint8Array(array.buffer, array.byteOffset, count * array.BYTES_PER_ELEMENT);
|
|
4525
|
+
function writeHordeSnapshot(store, out, header, layout = hordeSnapshotLayout(store)) {
|
|
4526
|
+
const words = new Int32Array(out.buffer, out.byteOffset, HEADER2 / 4);
|
|
4527
|
+
words[0] = store.n;
|
|
4528
|
+
words[1] = header.frame;
|
|
4529
|
+
words[2] = header.kills;
|
|
4530
|
+
words[3] = Math.round(header.hurt * 1e3);
|
|
4531
|
+
for (const field of layout.fields) out.set(bytesOf(store[field.key], store.n * field.per), field.offset);
|
|
4532
|
+
}
|
|
4533
|
+
function readHordeSnapshot(store, bytes, layout = hordeSnapshotLayout(store)) {
|
|
4534
|
+
const words = new Int32Array(bytes.buffer, bytes.byteOffset, HEADER2 / 4);
|
|
4535
|
+
const n = words[0];
|
|
4536
|
+
store.n = n;
|
|
4537
|
+
for (const field of layout.fields) {
|
|
4538
|
+
const target = store[field.key];
|
|
4539
|
+
bytesOf(target, n * field.per).set(bytes.subarray(field.offset, field.offset + n * field.per * field.bytes));
|
|
4540
|
+
}
|
|
4541
|
+
return { n, frame: words[1], kills: words[2], hurt: words[3] / 1e3 };
|
|
4542
|
+
}
|
|
4543
|
+
var mirrorTarget = (x, z) => {
|
|
4544
|
+
const state = { x, z, y: 0, floor: 0, alive: true, turned: false, crouch: false, bleeding: 0, scentTimer: 0, screamBoost: 0, grabbedBy: -1, blind: 0, burnTimer: 0, yaw: 0 };
|
|
4545
|
+
const damage = { total: 0 };
|
|
4546
|
+
return {
|
|
4547
|
+
state,
|
|
4548
|
+
damage,
|
|
4549
|
+
grab: (by) => void (state.grabbedBy = by),
|
|
4550
|
+
hurt: (amount) => void (damage.total += amount),
|
|
4551
|
+
knock: () => {
|
|
4552
|
+
},
|
|
4553
|
+
release: () => void (state.grabbedBy = -1)
|
|
4554
|
+
};
|
|
4555
|
+
};
|
|
4556
|
+
function createHordeCrowd(init, target) {
|
|
4557
|
+
const crowd = createCrowd({
|
|
4558
|
+
pack: init.pack,
|
|
4559
|
+
seed: init.seed,
|
|
4560
|
+
capacity: init.capacity,
|
|
4561
|
+
target,
|
|
4562
|
+
flow: { cellsPerStep: init.flowCellsPerStep ?? 2e4 },
|
|
4563
|
+
...init.physics ? { physics: { ...init.physics, worker: "off" } } : {}
|
|
4564
|
+
});
|
|
4565
|
+
if (init.initial) {
|
|
4566
|
+
readHordeSnapshot(crowd.store, init.initial);
|
|
4567
|
+
return crowd;
|
|
4568
|
+
}
|
|
4569
|
+
for (let k = 0; k < init.count; k++) {
|
|
4570
|
+
const angle = k / Math.max(1, init.count) * Math.PI * 2 * 7.31;
|
|
4571
|
+
const ring = init.spawn.radius * (0.35 + 0.65 * (k * 0.618034 % 1));
|
|
4572
|
+
crowd.spawn(init.spawn.x + Math.cos(angle) * ring, init.spawn.z + Math.sin(angle) * ring);
|
|
4573
|
+
}
|
|
4574
|
+
return crowd;
|
|
4575
|
+
}
|
|
4576
|
+
var hordeSimModule = Object.freeze({
|
|
4577
|
+
id: HORDE_SIM_ID,
|
|
4578
|
+
create(init) {
|
|
4579
|
+
const target = mirrorTarget(init.target.x, init.target.z);
|
|
4580
|
+
const crowd = createHordeCrowd(init, target);
|
|
4581
|
+
const layout = hordeSnapshotLayout(crowd.store);
|
|
4582
|
+
return {
|
|
4583
|
+
step(_frame, dt, commands) {
|
|
4584
|
+
for (const c of commands) {
|
|
4585
|
+
if (c[0] === HORDE_COMMAND.TARGET) {
|
|
4586
|
+
target.state.x = c[1];
|
|
4587
|
+
target.state.z = c[2];
|
|
4588
|
+
target.state.yaw = c[3];
|
|
4589
|
+
target.state.alive = c[4] !== 0;
|
|
4590
|
+
} else if (c[0] === HORDE_COMMAND.BLAST) crowd.blast({ point: [c[1], c[2], c[3]], radius: c[4], energy: c[5] });
|
|
4591
|
+
}
|
|
4592
|
+
crowd.update(dt);
|
|
4593
|
+
},
|
|
4594
|
+
write(out, frame) {
|
|
4595
|
+
writeHordeSnapshot(crowd.store, out, { frame, kills: crowd.stats.kills, hurt: target.damage.total }, layout);
|
|
4596
|
+
},
|
|
4597
|
+
dispose: () => crowd.dispose()
|
|
4598
|
+
};
|
|
4599
|
+
}
|
|
4600
|
+
});
|
|
4601
|
+
function hordeSnapshotBytes(capacity) {
|
|
4602
|
+
return hordeSnapshotLayout(createStore(capacity)).bytes;
|
|
4603
|
+
}
|
|
4424
4604
|
export {
|
|
4425
4605
|
AGENT_FIELDS,
|
|
4426
4606
|
BALANCE,
|
|
@@ -4432,6 +4612,8 @@ export {
|
|
|
4432
4612
|
FALL_CAUSE,
|
|
4433
4613
|
HERD_DEFAULTS,
|
|
4434
4614
|
HERD_ROLES,
|
|
4615
|
+
HORDE_COMMAND,
|
|
4616
|
+
HORDE_SIM_ID,
|
|
4435
4617
|
MISSING,
|
|
4436
4618
|
STATE,
|
|
4437
4619
|
attachPhysicsWorker,
|
|
@@ -4440,18 +4622,26 @@ export {
|
|
|
4440
4622
|
contactStep,
|
|
4441
4623
|
createCivilians,
|
|
4442
4624
|
createContactView,
|
|
4625
|
+
createCoordinatorRunner,
|
|
4443
4626
|
createCrowd,
|
|
4444
4627
|
createDoors,
|
|
4445
4628
|
createFlock,
|
|
4446
4629
|
createHerd,
|
|
4630
|
+
createHordeCrowd,
|
|
4447
4631
|
createMovableContacts,
|
|
4448
4632
|
createNavGrid,
|
|
4449
4633
|
createRng,
|
|
4450
4634
|
createStore,
|
|
4635
|
+
crowdPhysicsKind,
|
|
4636
|
+
hordeSimModule,
|
|
4637
|
+
hordeSnapshotBytes,
|
|
4638
|
+
hordeSnapshotLayout,
|
|
4451
4639
|
identityOf,
|
|
4452
4640
|
mix32,
|
|
4641
|
+
readHordeSnapshot,
|
|
4453
4642
|
resolvePhysics,
|
|
4454
4643
|
unit,
|
|
4455
|
-
walksInto
|
|
4644
|
+
walksInto,
|
|
4645
|
+
writeHordeSnapshot
|
|
4456
4646
|
};
|
|
4457
4647
|
//# sourceMappingURL=index.js.map
|