@adatechnology/game-destruction-three 0.27.1 → 0.28.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/dist/index.js CHANGED
@@ -3280,6 +3280,102 @@ var changedHeights = (before, after, size) => {
3280
3280
  return maxX < 0 ? null : { x: minX, z: minZ, sizeX: maxX - minX + 1, sizeZ: maxZ - minZ + 1 };
3281
3281
  };
3282
3282
 
3283
+ // src/engine-ground-follow.ts
3284
+ import { AWAKE_BOUNDS_STRIDE } from "@adatechnology/game-physics";
3285
+ var AHEAD_S = 0.15;
3286
+ var IDLE_UPDATES = 600;
3287
+ var SWEEP_EVERY = 30;
3288
+ var MOST_PER_SWEEP = 64;
3289
+ var HALF_HEIGHT = 2e3;
3290
+ var INITIAL_ROWS = 64;
3291
+ var OCCUPANTS = ["debris", "prop", "character", "agent", "vehicle", "corpse"];
3292
+ var GroundFollow = class {
3293
+ constructor(host) {
3294
+ __publicField(this, "host", host);
3295
+ __publicField(this, "rows", new Float64Array(INITIAL_ROWS * AWAKE_BOUNDS_STRIDE));
3296
+ /** The update each tile was last asked for by an outside body: the tiles that only the pieces use are not in it, and are never taken away. */
3297
+ __publicField(this, "askedAt", /* @__PURE__ */ new Map());
3298
+ __publicField(this, "occupants", []);
3299
+ __publicField(this, "sweepFilter", { layers: OCCUPANTS });
3300
+ __publicField(this, "stamp", 0);
3301
+ __publicField(this, "box", { kind: "box", half: [0, HALF_HEIGHT, 0] });
3302
+ this.box.half[0] = this.box.half[2] = host.tile / 2;
3303
+ }
3304
+ /** The tile count the follower is keeping an eye on. */
3305
+ get watched() {
3306
+ return this.askedAt.size;
3307
+ }
3308
+ clear() {
3309
+ this.askedAt.clear();
3310
+ }
3311
+ update(dt, stamp) {
3312
+ this.stamp = stamp;
3313
+ const { world, skip } = this.host;
3314
+ let count = world.readAwakeBounds(this.rows, skip);
3315
+ if (count * AWAKE_BOUNDS_STRIDE > this.rows.length) {
3316
+ this.rows = new Float64Array(count * 2 * AWAKE_BOUNDS_STRIDE);
3317
+ count = world.readAwakeBounds(this.rows, skip);
3318
+ }
3319
+ for (let row = 0; row < count; row++) this.cover(row * AWAKE_BOUNDS_STRIDE, dt);
3320
+ if (stamp % SWEEP_EVERY === 0) this.sweep();
3321
+ }
3322
+ /** The tiles under the row's box laid now, the ring around it and the path ahead of it as the host lays them. */
3323
+ cover(o, dt) {
3324
+ const r = this.rows;
3325
+ const { margin, tile, maxStops } = this.host;
3326
+ const minX = r[o + 2], minZ = r[o + 4], maxX = r[o + 5], maxZ = r[o + 7];
3327
+ const vx = r[o + 8], vz = r[o + 9];
3328
+ this.visit(minX, maxX, minZ, maxZ, true);
3329
+ const time = dt + AHEAD_S;
3330
+ const travel = Math.hypot(vx, vz) * time;
3331
+ const stops = travel > margin ? Math.min(maxStops, Math.ceil(travel / tile)) : 0;
3332
+ for (let stop = 0; stop <= stops; stop++) {
3333
+ const fraction = stops === 0 ? 0 : stop / stops;
3334
+ const dx = vx * time * fraction;
3335
+ const dz = vz * time * fraction;
3336
+ this.visit(minX + dx - margin, maxX + dx + margin, minZ + dz - margin, maxZ + dz + margin, false);
3337
+ }
3338
+ }
3339
+ visit(minX, maxX, minZ, maxZ, now) {
3340
+ const host = this.host;
3341
+ const { tile: size, keyOrigin, keyStride } = host;
3342
+ for (let ix = Math.floor(minX / size); ix <= Math.floor(maxX / size); ix++) {
3343
+ for (let iz = Math.floor(minZ / size); iz <= Math.floor(maxZ / size); iz++) {
3344
+ const key = (ix + keyOrigin) * keyStride + (iz + keyOrigin);
3345
+ this.askedAt.set(key, this.stamp);
3346
+ if (!host.needs(key)) continue;
3347
+ if (now) host.layNow(key, ix, iz);
3348
+ else host.layLater(key, ix, iz);
3349
+ }
3350
+ }
3351
+ }
3352
+ /** The tiles unasked for `IDLE_UPDATES` with nothing that moves on them are taken away; one with something on it is asked again later. */
3353
+ sweep() {
3354
+ const host = this.host;
3355
+ let looked = 0;
3356
+ for (const [key, last] of this.askedAt) {
3357
+ if (!host.has(key)) {
3358
+ this.askedAt.delete(key);
3359
+ continue;
3360
+ }
3361
+ if (this.stamp - last < IDLE_UPDATES) continue;
3362
+ if (looked++ >= MOST_PER_SWEEP) return;
3363
+ if (this.isOccupied(key)) {
3364
+ this.askedAt.set(key, this.stamp);
3365
+ continue;
3366
+ }
3367
+ this.askedAt.delete(key);
3368
+ host.drop(key);
3369
+ }
3370
+ }
3371
+ isOccupied(key) {
3372
+ const { tile, keyOrigin, keyStride, world } = this.host;
3373
+ const ix = Math.floor(key / keyStride) - keyOrigin;
3374
+ const iz = key % keyStride - keyOrigin;
3375
+ return world.overlapShape(this.box, [(ix + 0.5) * tile, 0, (iz + 0.5) * tile], [0, 0, 0, 1], this.occupants, this.sweepFilter) > 0;
3376
+ }
3377
+ };
3378
+
3283
3379
  // src/engine-ground-schedule.ts
3284
3380
  var MAX_FREE_PATCHES = 64;
3285
3381
  var EMPTY = new Float64Array(0);
@@ -3580,6 +3676,11 @@ var EnginePieces = class {
3580
3676
  /** The pieces by object id: what the sweep meets that the engine already has (`sweepOn`). */
3581
3677
  __publicField(this, "byObject", /* @__PURE__ */ new Map());
3582
3678
  __publicField(this, "byBody", /* @__PURE__ */ new Map());
3679
+ /** The same bodies as a set: what the engine leaves out when it lists the bodies that are not the pieces' (`GroundFollow`). */
3680
+ __publicField(this, "bodyIds", /* @__PURE__ */ new Set());
3681
+ /** Ground under the bodies and characters of the world that are not the pieces. */
3682
+ /** Ground under the bodies and characters of the world that are not the pieces. */
3683
+ __publicField(this, "follow");
3583
3684
  /** The engine's awake bodies as `finish` last read them (one crossing), and the pieces they are. */
3584
3685
  __publicField(this, "activeIds", []);
3585
3686
  __publicField(this, "active", /* @__PURE__ */ new Set());
@@ -3652,6 +3753,33 @@ var EnginePieces = class {
3652
3753
  this.statics = new EngineStatics(world, GROUND);
3653
3754
  this.schedule = o.groundWorker ? new GroundSchedule(o.groundWorker, Math.max(1, Math.round(o.groundWorkerFrames ?? GROUND_WORKER_FRAMES))) : null;
3654
3755
  if (this.schedule) this.warmGround(this.schedule);
3756
+ this.follow = new GroundFollow({
3757
+ world,
3758
+ skip: this.bodyIds,
3759
+ tile: TILE,
3760
+ margin: TILE_MARGIN,
3761
+ maxStops: MAX_PATH_STOPS,
3762
+ keyOrigin: TILE_KEY_ORIGIN,
3763
+ keyStride: TILE_KEY,
3764
+ needs: (key) => {
3765
+ const tile = this.tiles.get(key);
3766
+ return !tile || tile.dirty !== null;
3767
+ },
3768
+ has: (key) => this.tiles.has(key),
3769
+ layNow: this.layAt,
3770
+ layLater: this.schedule ? this.askAt : this.layAt,
3771
+ drop: (key) => this.dropTile(key)
3772
+ });
3773
+ }
3774
+ /** A tile nobody needs any more: its bodies leave the world, and a piece or body that comes back asks for it again. */
3775
+ dropTile(key) {
3776
+ const tile = this.tiles.get(key);
3777
+ if (!tile) return;
3778
+ this.world.removeBody(tile.ground);
3779
+ if (tile.steps !== null) this.world.removeBody(tile.steps);
3780
+ if (tile.solid !== null) this.world.removeBody(tile.solid);
3781
+ if (tile.patch) this.schedule?.givePatch(tile.patch.heights);
3782
+ this.tiles.delete(key);
3655
3783
  }
3656
3784
  /** The fixed bodies as a broad phase for the destruction world's rays (`engine-rays.ts`). */
3657
3785
  rayBroadPhase() {
@@ -3676,6 +3804,7 @@ var EnginePieces = class {
3676
3804
  if (wakeNeighbours && (b.sleeping || link.sleeping)) this.wakeAround(b, link.pos);
3677
3805
  this.byUser.delete(link.user);
3678
3806
  this.byBody.delete(link.id);
3807
+ this.bodyIds.delete(link.id);
3679
3808
  this.active.delete(b);
3680
3809
  if (this.byObject.get(b.objectId) === b) this.byObject.delete(b.objectId);
3681
3810
  this.links.delete(b);
@@ -3696,6 +3825,7 @@ var EnginePieces = class {
3696
3825
  if (t.solid !== null) this.world.removeBody(t.solid);
3697
3826
  }
3698
3827
  this.tiles.clear();
3828
+ this.follow.clear();
3699
3829
  this.schedule?.clear();
3700
3830
  this.blastAgain.clear();
3701
3831
  this.blasts.length = 0;
@@ -3811,6 +3941,7 @@ var EnginePieces = class {
3811
3941
  this.layStatics();
3812
3942
  this.applyDueGround();
3813
3943
  this.layBlastGround();
3944
+ this.follow.update(dt, n);
3814
3945
  this.makeBodies(each);
3815
3946
  each((b) => this.hang(b));
3816
3947
  each((b) => this.sync(b, dt, h));
@@ -4084,6 +4215,7 @@ var EnginePieces = class {
4084
4215
  this.links.set(b, link);
4085
4216
  this.byUser.set(user, b);
4086
4217
  this.byBody.set(id, b);
4218
+ this.bodyIds.add(id);
4087
4219
  this.byObject.set(b.objectId, b);
4088
4220
  this.outOfStatics(b, link);
4089
4221
  });