@adatechnology/game-crowd 0.4.7 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -58,6 +58,7 @@ var defaults_default = {
58
58
  limpSpeed: 0.5,
59
59
  separation: 0.31,
60
60
  separationPush: 2.2,
61
+ shoveForce: 450,
61
62
  doorDamage: 10,
62
63
  doorMessageDistance: 25,
63
64
  skullMinDamage: 20,
@@ -2189,6 +2190,9 @@ var createStore = (capacity) => {
2189
2190
  wounds: new Uint16Array(capacity),
2190
2191
  leanX: f32(),
2191
2192
  leanZ: f32(),
2193
+ /** The way the body leans its force on a movable piece this update (unit, or 0): the push pose. */
2194
+ shoveX: f32(),
2195
+ shoveZ: f32(),
2192
2196
  thrown: new Int8Array(capacity),
2193
2197
  prevX: f32(),
2194
2198
  prevZ: f32(),
@@ -2275,12 +2279,92 @@ var AGENT_FIELDS = [
2275
2279
  "wounds",
2276
2280
  "leanX",
2277
2281
  "leanZ",
2282
+ "shoveX",
2283
+ "shoveZ",
2278
2284
  "thrown",
2279
2285
  "prevX",
2280
2286
  "prevZ",
2281
2287
  "prevY"
2282
2288
  ];
2283
2289
 
2290
+ // src/movables.ts
2291
+ var INTO_MIN = 0.3;
2292
+ var TOUCH = 1e-6;
2293
+ function createMovableContacts() {
2294
+ let list = [];
2295
+ let sums = [];
2296
+ return {
2297
+ get list() {
2298
+ return list;
2299
+ },
2300
+ set(next) {
2301
+ list = next;
2302
+ sums = next.map(() => ({ x: 0, z: 0, agents: /* @__PURE__ */ new Map() }));
2303
+ },
2304
+ at(x, z, y, radius) {
2305
+ for (let k = 0; k < list.length; k++) {
2306
+ const m = list[k];
2307
+ if (y >= m.top || x <= m.minX - radius + TOUCH || x >= m.maxX + radius - TOUCH || z <= m.minZ - radius + TOUCH || z >= m.maxZ + radius - TOUCH) continue;
2308
+ const cx = Math.max(m.minX, Math.min(m.maxX, x)) - x;
2309
+ const cz = Math.max(m.minZ, Math.min(m.maxZ, z)) - z;
2310
+ const d = Math.hypot(cx, cz);
2311
+ return d < 1e-6 ? { index: k, intoX: 0, intoZ: 0 } : { index: k, intoX: cx / d, intoZ: cz / d };
2312
+ }
2313
+ return null;
2314
+ },
2315
+ lean(index, agent, dirX, dirZ, forceN) {
2316
+ const sum = sums[index];
2317
+ if (!sum) return;
2318
+ const length = Math.hypot(dirX, dirZ);
2319
+ if (length < 1e-6) return;
2320
+ const fx = dirX / length * forceN;
2321
+ const fz = dirZ / length * forceN;
2322
+ const before = sum.agents.get(agent);
2323
+ if (before) {
2324
+ if (Math.hypot(before[0], before[1]) >= forceN) return;
2325
+ sum.x -= before[0];
2326
+ sum.z -= before[1];
2327
+ }
2328
+ sum.agents.set(agent, [fx, fz]);
2329
+ sum.x += fx;
2330
+ sum.z += fz;
2331
+ },
2332
+ flush() {
2333
+ const out = [];
2334
+ for (let k = 0; k < sums.length; k++) {
2335
+ const sum = sums[k];
2336
+ if (sum.agents.size === 0) continue;
2337
+ const force = Math.hypot(sum.x, sum.z);
2338
+ if (force > 1e-6) out.push({ id: list[k].id, dirX: sum.x / force, dirZ: sum.z / force, forceN: force, pushers: sum.agents.size });
2339
+ sum.x = 0;
2340
+ sum.z = 0;
2341
+ sum.agents.clear();
2342
+ }
2343
+ return out;
2344
+ }
2345
+ };
2346
+ }
2347
+ var walksInto = (walkX, walkZ, hit) => {
2348
+ if (hit.intoX === 0 && hit.intoZ === 0) return true;
2349
+ const length = Math.hypot(walkX, walkZ);
2350
+ return length > 1e-6 && (walkX * hit.intoX + walkZ * hit.intoZ) / length > INTO_MIN;
2351
+ };
2352
+ function shoveStep(contacts, r) {
2353
+ const hit = contacts.at(r.nx, r.nz, r.y, r.radius);
2354
+ if (!hit) return null;
2355
+ let leanX = 0;
2356
+ let leanZ = 0;
2357
+ if (walksInto(r.walkX, r.walkZ, hit)) {
2358
+ const length = Math.hypot(r.walkX, r.walkZ) || 1;
2359
+ contacts.lean(hit.index, r.agent, r.walkX, r.walkZ, r.forceN);
2360
+ leanX = r.walkX / length;
2361
+ leanZ = r.walkZ / length;
2362
+ }
2363
+ if (!contacts.at(r.nx, r.z, r.y, r.radius) && !r.blocked(r.nx, r.z)) return { x: r.nx, z: r.z, leanX, leanZ };
2364
+ if (!contacts.at(r.x, r.nz, r.y, r.radius) && !r.blocked(r.x, r.nz)) return { x: r.x, z: r.nz, leanX, leanZ };
2365
+ return { x: r.x, z: r.z, leanX, leanZ };
2366
+ }
2367
+
2284
2368
  // src/crowd.ts
2285
2369
  var CROWD_DEFAULTS = {
2286
2370
  sources: defaults_default.sources,
@@ -2302,6 +2386,8 @@ function createCrowd(options) {
2302
2386
  const grid = options.grid ?? createNavGrid();
2303
2387
  const doors = options.doors ?? createDoors(grid, { random });
2304
2388
  const water = options.water ?? (() => false);
2389
+ const movables = createMovableContacts();
2390
+ let shoves = [];
2305
2391
  const plugins = options.plugins ?? [];
2306
2392
  const clock = options.flow?.clock ?? (() => typeof performance === "undefined" ? Date.now() : performance.now());
2307
2393
  const FLOW_R = options.flow?.radius ?? N.flowRadius;
@@ -3359,16 +3445,27 @@ function createCrowd(options) {
3359
3445
  }
3360
3446
  const nx = Z.x[i] + (Z.vx[i] + Z.pushX[i]) * dt;
3361
3447
  const nz = Z.z[i] + (Z.vz[i] + Z.pushZ[i]) * dt;
3448
+ const walkX = Z.dvx[i];
3449
+ const walkZ = Z.dvz[i];
3362
3450
  let hitCell = -1;
3363
- if (!blockedAt(i, nx, Z.z[i])) Z.x[i] = nx;
3364
- else {
3365
- if (Z.state[i] !== STATE.AIRBORNE) Z.vx[i] = Z.pushX[i] = 0;
3366
- hitCell = cellOf(nx, Z.z[i]);
3367
- }
3368
- if (!blockedAt(i, Z.x[i], nz)) Z.z[i] = nz;
3369
- else {
3370
- if (Z.state[i] !== STATE.AIRBORNE) Z.vz[i] = Z.pushZ[i] = 0;
3371
- hitCell = cellOf(Z.x[i], nz);
3451
+ const shove = movables.list.length > 0 && Z.state[i] !== STATE.AIRBORNE && movables.at(nx, nz, Z.y[i], N.separation * Z.build[i]) ? shoveStep(movables, { agent: i, x: Z.x[i], z: Z.z[i], nx, nz, y: Z.y[i], radius: N.separation * Z.build[i], walkX, walkZ, forceN: N.shoveForce * Z.build[i], blocked: (x, z) => blockedAt(i, x, z) }) : null;
3452
+ Z.shoveX[i] = shove?.leanX ?? 0;
3453
+ Z.shoveZ[i] = shove?.leanZ ?? 0;
3454
+ if (shove) {
3455
+ Z.x[i] = shove.x;
3456
+ Z.z[i] = shove.z;
3457
+ Z.vx[i] = Z.vz[i] = Z.pushX[i] = Z.pushZ[i] = 0;
3458
+ } else {
3459
+ if (!blockedAt(i, nx, Z.z[i])) Z.x[i] = nx;
3460
+ else {
3461
+ if (Z.state[i] !== STATE.AIRBORNE) Z.vx[i] = Z.pushX[i] = 0;
3462
+ hitCell = cellOf(nx, Z.z[i]);
3463
+ }
3464
+ if (!blockedAt(i, Z.x[i], nz)) Z.z[i] = nz;
3465
+ else {
3466
+ if (Z.state[i] !== STATE.AIRBORNE) Z.vz[i] = Z.pushZ[i] = 0;
3467
+ hitCell = cellOf(Z.x[i], nz);
3468
+ }
3372
3469
  }
3373
3470
  if (hitCell >= 0 && Z.state[i] === STATE.CHASE && b === 0) {
3374
3471
  const vl = tr.vaultsLow;
@@ -3398,7 +3495,7 @@ function createCrowd(options) {
3398
3495
  Z.renderSpeed[i] += (spd - Z.renderSpeed[i]) * Math.min(1, dt * 6);
3399
3496
  if (Z.band[i] <= 1) {
3400
3497
  const s = Z.state[i];
3401
- let goal = s === STATE.CHASE || s === STATE.GRAB || s === STATE.PILE_STEP ? 1 : 0;
3498
+ let goal = s === STATE.CHASE || s === STATE.GRAB || s === STATE.PILE_STEP || Z.shoveX[i] !== 0 || Z.shoveZ[i] !== 0 ? 1 : 0;
3402
3499
  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;
3403
3500
  Z.arms[i] += (goal - Z.arms[i]) * Math.min(1, dt * (goal > Z.arms[i] ? 3 : 5));
3404
3501
  }
@@ -3413,6 +3510,7 @@ function createCrowd(options) {
3413
3510
  Z.renderYaw[i] += dy * Math.min(1, dt * (Z.state[i] === STATE.AIRBORNE ? 20 : 9));
3414
3511
  }
3415
3512
  engine.endStep();
3513
+ shoves = movables.flush();
3416
3514
  if (pileColumns.length) collapsePileColumns(Z, pileColumns, FW * FW, stepRelease);
3417
3515
  killQFlush();
3418
3516
  noises.length = 0;
@@ -3543,6 +3641,11 @@ function createCrowd(options) {
3543
3641
  hunt = next;
3544
3642
  },
3545
3643
  chaseSpeed,
3644
+ setMovables: (list) => movables.set(list),
3645
+ movableAt: (x, z, radius) => movables.list[movables.at(x, z, 0, radius)?.index ?? -1] ?? null,
3646
+ get shoves() {
3647
+ return shoves;
3648
+ },
3546
3649
  asPlayerCrowd: () => ({
3547
3650
  count: () => Z.n,
3548
3651
  active: (i) => Z.band[i] === 0,
@@ -3582,6 +3685,7 @@ function createCrowd(options) {
3582
3685
  }
3583
3686
 
3584
3687
  // src/behaviours/civilians.ts
3688
+ var ROUND_TOWARD = 0.2;
3585
3689
  var CIVILIAN_DEFAULTS = {
3586
3690
  hpAdult: 100,
3587
3691
  hpChild: 40,
@@ -3610,6 +3714,7 @@ var CIVILIAN_DEFAULTS = {
3610
3714
  doorProbe: 0.6,
3611
3715
  steer: 8,
3612
3716
  bodyHeight: 0.4,
3717
+ bodyRadius: 0.3,
3613
3718
  patrolChance: 0.2,
3614
3719
  childHeight: 0.65,
3615
3720
  childBuild: 0.7,
@@ -3789,6 +3894,29 @@ function createCivilians(crowd, options) {
3789
3894
  Z.yaw[i] = Math.atan2(dx, dz);
3790
3895
  return true;
3791
3896
  });
3897
+ const blocked = (x, z) => grid.solidAt(x, z, N.bodyHeight) || crowd.movableAt(x, z, N.bodyRadius) !== null;
3898
+ const roundSides = /* @__PURE__ */ new WeakMap();
3899
+ const roundFurniture = (n, ax, az) => {
3900
+ const m = crowd.movableAt(n.x + ax * N.probe, n.z + az * N.probe, N.bodyRadius);
3901
+ if (!m) {
3902
+ roundSides.delete(n);
3903
+ return null;
3904
+ }
3905
+ let ox = n.x - Math.max(m.minX, Math.min(m.maxX, n.x));
3906
+ let oz = n.z - Math.max(m.minZ, Math.min(m.maxZ, n.z));
3907
+ const length = Math.hypot(ox, oz) || 1;
3908
+ ox /= length;
3909
+ oz /= length;
3910
+ let side = roundSides.get(n);
3911
+ if (side === void 0) {
3912
+ const toward = -oz * ax + ox * az;
3913
+ const plusEnd = Math.abs(oz) >= Math.abs(ox) ? -oz > 0 ? m.maxX - n.x : n.x - m.minX : ox > 0 ? m.maxZ - n.z : n.z - m.minZ;
3914
+ const minusEnd = Math.abs(oz) >= Math.abs(ox) ? -oz > 0 ? n.x - m.minX : m.maxX - n.x : ox > 0 ? n.z - m.minZ : m.maxZ - n.z;
3915
+ side = Math.abs(toward) > ROUND_TOWARD ? Math.sign(toward) : plusEnd <= minusEnd ? 1 : -1;
3916
+ roundSides.set(n, side);
3917
+ }
3918
+ return [-oz * side, ox * side];
3919
+ };
3792
3920
  const move = (n, dt, tx, tz, sp) => {
3793
3921
  const dx = tx - n.x;
3794
3922
  const dz = tz - n.z;
@@ -3799,12 +3927,14 @@ function createCivilians(crowd, options) {
3799
3927
  }
3800
3928
  let ax = dx / d;
3801
3929
  let az = dz / d;
3802
- for (const a of N.whiskers) {
3930
+ const round = roundFurniture(n, ax, az);
3931
+ if (round) [ax, az] = round;
3932
+ else for (const a of N.whiskers) {
3803
3933
  const c2 = Math.cos(a);
3804
3934
  const s = Math.sin(a);
3805
3935
  const bx = ax * c2 - az * s;
3806
3936
  const bz = ax * s + az * c2;
3807
- if (!grid.solidAt(n.x + bx * N.probe, n.z + bz * N.probe, N.bodyHeight)) {
3937
+ if (!blocked(n.x + bx * N.probe, n.z + bz * N.probe)) {
3808
3938
  ax = bx;
3809
3939
  az = bz;
3810
3940
  break;
@@ -3823,8 +3953,8 @@ function createCivilians(crowd, options) {
3823
3953
  n.vz = lerp(n.vz, az * sp, Math.min(1, dt * N.steer));
3824
3954
  const nx = n.x + n.vx * dt;
3825
3955
  const nz = n.z + n.vz * dt;
3826
- if (!grid.solidAt(nx, n.z, N.bodyHeight)) n.x = nx;
3827
- if (!grid.solidAt(n.x, nz, N.bodyHeight)) n.z = nz;
3956
+ if (!blocked(nx, n.z)) n.x = nx;
3957
+ if (!blocked(n.x, nz)) n.z = nz;
3828
3958
  n.yaw = Math.atan2(n.vx, n.vz);
3829
3959
  return false;
3830
3960
  };
@@ -3927,6 +4057,7 @@ function createCivilians(crowd, options) {
3927
4057
  n.state = "fleeing";
3928
4058
  n.path = path.map(([x, z]) => [x, z]);
3929
4059
  n.pathIndex = 0;
4060
+ n.stuckTimer = 0;
3930
4061
  },
3931
4062
  route(from, to) {
3932
4063
  return rota(casaDe(from), casaDe(to));
@@ -4313,12 +4444,14 @@ export {
4313
4444
  createDoors,
4314
4445
  createFlock,
4315
4446
  createHerd,
4447
+ createMovableContacts,
4316
4448
  createNavGrid,
4317
4449
  createRng,
4318
4450
  createStore,
4319
4451
  identityOf,
4320
4452
  mix32,
4321
4453
  resolvePhysics,
4322
- unit
4454
+ unit,
4455
+ walksInto
4323
4456
  };
4324
4457
  //# sourceMappingURL=index.js.map