@korial29/tiny-fps 0.194.0 → 0.196.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/server.js CHANGED
@@ -829,7 +829,7 @@ var roleOf = (role, def) => {
829
829
  var kitOf = (role, def) => KITS[roleOf(role, def)];
830
830
  // src/shared/world.ts
831
831
  /** Metres per grid cell: about the size of the player's reach, so a query touches 1 to 4 cells. */
832
- var CELL = 4;
832
+ var CELL$1 = 4;
833
833
  /** Cell coordinates run from -OFF to OFF - 1: a 8 km square at 4 m, far more than any map. */
834
834
  var OFF = 1024;
835
835
  /** The key of the cell with integer coordinates (cx, cz). */
@@ -837,7 +837,7 @@ var key = (cx, cz) => (cx + OFF) * (2 * OFF) + cz + OFF;
837
837
  /** Integer cell coordinate of a metre value, clamped to the keyed range. */
838
838
  var at$3 = (v, cell) => Math.min(Math.max(Math.floor(v / cell), -1024), 1023);
839
839
  /** Builds the grid: each brush is listed in every cell its footprint covers. */
840
- var build$1 = (def, cell = CELL) => {
840
+ var build$1 = (def, cell = CELL$1) => {
841
841
  const grid = /* @__PURE__ */ new Map();
842
842
  def.brushes.forEach(({ min, max }, i) => {
843
843
  for (let cx = at$3(min[0], cell); cx <= at$3(max[0], cell); cx++) for (let cz = at$3(min[2], cell); cz <= at$3(max[2], cell); cz++) {
@@ -858,13 +858,21 @@ var build$1 = (def, cell = CELL) => {
858
858
  })) } : {}
859
859
  };
860
860
  };
861
+ /** The call of near() that last listed each brush, so a brush in several cells is listed once (the bots' mesh asks for hundreds). */
862
+ var marks = /* @__PURE__ */ new Uint32Array(0);
863
+ var mark = 0;
861
864
  /**
862
865
  * Indices of the brushes whose footprint may overlap the square of half side r around (x, z): every
863
866
  * brush listed in a cell the square touches, once each. A candidate list, not an exact overlap.
864
867
  */
865
868
  var near = (w, x, z, r) => {
866
869
  const out = [];
867
- for (let cx = at$3(x - r, w.cell); cx <= at$3(x + r, w.cell); cx++) for (let cz = at$3(z - r, w.cell); cz <= at$3(z + r, w.cell); cz++) for (const i of w.grid.get(key(cx, cz)) ?? []) if (!out.includes(i)) out.push(i);
870
+ if (marks.length < w.def.brushes.length) marks = new Uint32Array(w.def.brushes.length);
871
+ mark = mark + 1 >>> 0 || (marks.fill(0), 1);
872
+ for (let cx = at$3(x - r, w.cell); cx <= at$3(x + r, w.cell); cx++) for (let cz = at$3(z - r, w.cell); cz <= at$3(z + r, w.cell); cz++) for (const i of w.grid.get(key(cx, cz)) ?? []) if (marks[i] !== mark) {
873
+ marks[i] = mark;
874
+ out.push(i);
875
+ }
868
876
  return out;
869
877
  };
870
878
  /**
@@ -1088,7 +1096,7 @@ var gap = (b, x, z) => [x - clamp$3(x, b.min[0], b.max[0]), z - clamp$3(z, b.min
1088
1096
  * step of that size cannot bring the centre across one, however thin. The candidates come from
1089
1097
  * near() with twice PLAYER_R, enough for the one push of at most PLAYER_R that matters.
1090
1098
  */
1091
- var push = (p, w, h) => shove(p, w, h, PLAYER_R);
1099
+ var push$1 = (p, w, h) => shove(p, w, h, PLAYER_R);
1092
1100
  /** push() for a circle of radius r: a rover's body is a few of them. */
1093
1101
  var shove = (p, w, h, r) => {
1094
1102
  const { brushes } = w.def;
@@ -1438,10 +1446,10 @@ var fly$1 = (p, w, h, g, snap, bodies) => {
1438
1446
  const y0 = p.y;
1439
1447
  p.x += p.vx * DT$2;
1440
1448
  p.z += p.vz * DT$2;
1441
- let [cx, cz] = push(p, w, h);
1449
+ let [cx, cz] = push$1(p, w, h);
1442
1450
  const [jx, jz] = jostle(p, bodies, h);
1443
1451
  if (jx || jz) {
1444
- const [ex, ez] = push(p, w, h);
1452
+ const [ex, ez] = push$1(p, w, h);
1445
1453
  cx += jx + ex;
1446
1454
  cz += jz + ez;
1447
1455
  }
@@ -5069,7 +5077,7 @@ var MID = {
5069
5077
  ]
5070
5078
  };
5071
5079
  /** The Lowline: the stairs down the tower's east face, short gaps, the east terrace; back up. */
5072
- var LOW = {
5080
+ var LOW$1 = {
5073
5081
  mid: [
5074
5082
  86,
5075
5083
  14.5,
@@ -5167,142 +5175,76 @@ var UP = {
5167
5175
  }
5168
5176
  };
5169
5177
  // src/shared/nav.ts
5178
+ /** How a body goes through a portal: walks either way, drops down it, or jumps up onto a box. */
5179
+ var PASS = {
5180
+ WALK: 0,
5181
+ DROP: 1,
5182
+ HOP: 2
5183
+ };
5170
5184
  /** Metres a second a bot is taken to run, for the costs. */
5171
5185
  var PACE = 7;
5172
5186
  /** Seconds added to a jump, and to a jump that wants a dash, a double jump or a wall-run. */
5173
5187
  var LEAP = .5;
5174
5188
  var TRICK = 1.5;
5175
- /** The longest straight walk between two points, metres. */
5176
- var WALK = 30;
5177
- /** The most a straight walk steps down, metres: off a plinth, down a short drop. */
5189
+ /** Seconds added to a drop and to a jump onto a box. */
5190
+ var DOWN = .3;
5191
+ var HOP_COST = 1;
5192
+ /** The highest box a body jumps onto standing (a standing jump lands on 1.7 m), and the floor it wants behind the edge. */
5193
+ var HOP$1 = 1.6;
5194
+ var HOP_DEPTH$1 = 2.5;
5195
+ /** The most a body drops onto any floor; past it, it wants SPAN metres of floor ahead to come down on at a run. */
5178
5196
  var DROP$1 = 2;
5179
- /**
5180
- * Metres to the nearest node a bot heads for when no straight walk reaches one (the moon's relief
5181
- * fails most, a stair all), how far above it may be (up a stair, or a ledge to climb) and how far
5182
- * below: a drop that does not hurt at full gravity.
5183
- */
5184
- var NEAR = 20;
5185
- var ABOVE = 2.2;
5186
- /** Two points of the routes this close (metres, and in height) are one. */
5187
- var SAME = .6;
5197
+ var SPAN = 4;
5198
+ /** Seconds added to a crossing of a route the bot does not prefer: the bridge's rectangles, a jump's first link. */
5199
+ var AREA = 20;
5200
+ /** The widest crack two rectangles are joined across: a drop's, where the higher's edge is a body's radius off the lower. */
5201
+ var PORTAL = PLAYER_R + .1;
5202
+ /** Metres a wall is kept off: the body's radius and a little. */
5203
+ var KEEP = PLAYER_R + .05;
5204
+ /** Metres back along a jump's way a bot comes at its take-off from. */
5205
+ var RUN_IN = 3;
5206
+ /** Metres the ends of a portal are drawn in, at most. */
5207
+ var EDGE = .7;
5208
+ /** The relief's squares merged into a rectangle at most, a side. */
5209
+ var MERGE = 8;
5210
+ /** Metres of grid cell. */
5211
+ var CELL = 4;
5212
+ /** A link of a route the bot does not prefer costs this many times more, one of its own as many times less. */
5213
+ var OTHER = 3;
5188
5214
  /** The routes, fastest first: the Spine, Midtown, the Lowline. */
5189
5215
  var ROUTES = [
5190
5216
  HIGH,
5191
5217
  MID,
5192
- LOW
5218
+ LOW$1
5193
5219
  ];
5194
- /** A leg of a route the bot does not prefer costs this many times more. */
5195
- var OTHER = 3;
5196
5220
  /**
5197
- * MOON's lanes in red's half, walked straight from point to point both ways: round the dome on the
5198
- * west (the modules close the east), in at its door to the flag, and across the field to the three
5199
- * crossings of the rille (the bridge, the west jump's run-up, the east stones' lip). Blue's are
5200
- * their mirror across z = 140. Relief and boulders fail walkable(), so these are not checked: a
5201
- * bot jumps what it bumps into.
5221
+ * MOON's walks across the rille, x0 and x1 and the route each is a crossing of: the bridge (the
5222
+ * first), and round the rille's two closed ends (the west with the long jump, the east with the
5223
+ * stones). The jumps are links of their own.
5202
5224
  */
5203
- var LANES = [
5225
+ var ACROSS = [
5204
5226
  [
5205
- [
5206
- 72,
5207
- 0,
5208
- 258
5209
- ],
5210
- [
5211
- 80,
5212
- 0,
5213
- 269
5214
- ],
5215
- [
5216
- 52,
5217
- 0,
5218
- 270
5219
- ],
5220
- [
5221
- 50,
5222
- 0,
5223
- 212
5224
- ],
5225
- [
5226
- 80,
5227
- 0,
5228
- 208
5229
- ],
5230
- [
5231
- 80,
5232
- 0,
5233
- 214.5
5234
- ],
5235
- [
5236
- 80,
5237
- 0,
5238
- 224
5239
- ],
5240
- [
5241
- 80,
5242
- 1,
5243
- 232
5244
- ]
5227
+ 74,
5228
+ 86,
5229
+ 0
5245
5230
  ],
5246
5231
  [
5247
- [
5248
- 80,
5249
- 0,
5250
- 208
5251
- ],
5252
- [
5253
- 80,
5254
- 0,
5255
- 152
5256
- ],
5257
- [
5258
- 80,
5259
- 0,
5260
- 140
5261
- ]
5262
- ],
5263
- [[
5264
- 50,
5265
- 0,
5266
- 212
5267
- ], [
5268
- 18,
5269
- 0,
5270
- 190
5271
- ]],
5272
- [[
5273
- 18,
5274
- 0,
5275
- 125
5276
- ], [
5277
- 18,
5232
+ -60,
5278
5233
  0,
5279
- 90
5280
- ]],
5234
+ 1
5235
+ ],
5281
5236
  [
5282
- [
5283
- 80,
5284
- 0,
5285
- 208
5286
- ],
5287
- [
5288
- 128.5,
5289
- 0,
5290
- 160
5291
- ],
5292
- [
5293
- 128.5,
5294
- 0,
5295
- 150
5296
- ]
5237
+ 160,
5238
+ 220,
5239
+ 2
5297
5240
  ]
5298
5241
  ];
5299
5242
  /**
5300
5243
  * The jumps over MOON's rille from red's side, as moon.test.ts flies them: the west long jump (a
5301
5244
  * sprint jumped at the lip, a dash late in the air), and the east stones at 1.2, 2.4 and 1.2 m,
5302
- * each a jump that comes down on the stone. Blue's are their mirror. With the bridge (LANES[1]),
5303
- * they are the moon's three routes, an index of ROUTES each, so that bots spread over them.
5245
+ * each a jump that comes down on the stone. Blue's are their mirror. With the bridge, they are the
5246
+ * moon's three routes, an index of ROUTES each, so that bots spread over them.
5304
5247
  */
5305
- var BRIDGE = 1;
5306
5248
  var CROSSINGS = [[
5307
5249
  1,
5308
5250
  [
@@ -5358,81 +5300,332 @@ var end = (l) => [
5358
5300
  l.y,
5359
5301
  l.to[1]
5360
5302
  ];
5361
- /** Where a leg starts its own move: a jump's take-off, a slide's run-in, a run's goal. */
5362
- var from = (l) => l.k === "leap" ? l.at : l.k === "slide" ? l.from : l.to;
5363
- var flat$2 = (a, b) => {
5364
- const [ax, az] = a.length === 3 ? [a[0], a[2]] : a;
5365
- const [bx, bz] = b.length === 3 ? [b[0], b[2]] : b;
5366
- return Math.sqrt((bx - ax) ** 2 + (bz - az) ** 2);
5367
- };
5368
- /**
5369
- * Whether a body walks in a straight line from a to b, a step up or a drop of DROP at most: a floor
5370
- * under the middle and both sides of the body every half metre, and nothing in the way at the knees
5371
- * or the head from the height of a.
5372
- */
5373
- var walkable = (w, a, b) => {
5374
- if (b[1] - a[1] > .5 || a[1] - b[1] > DROP$1) return false;
5375
- const d = flat$2(a, b);
5376
- if (d < .001) return true;
5377
- const [ux, uz] = [(b[0] - a[0]) / d, (b[2] - a[2]) / d];
5378
- const y = Math.min(a[1], b[1]);
5379
- for (const s of [
5380
- -PLAYER_R,
5381
- 0,
5382
- PLAYER_R
5383
- ]) {
5384
- const [ox, oz] = [a[0] - uz * s, a[2] + ux * s];
5385
- for (const h of [STEP_H + .1, 1.5]) if (ray(w, [
5386
- ox,
5387
- a[1] + h,
5388
- oz
5389
- ], [
5390
- ux,
5391
- 0,
5392
- uz
5393
- ], d) < d) return false;
5394
- for (let t = 0; t <= d; t += .5) if (floorAt(w, ox + ux * t, oz + uz * t, Math.max(a[1], b[1]) + .5) < y - .3) return false;
5395
- }
5396
- return true;
5303
+ var flat$2 = (ax, az, bx, bz) => Math.sqrt((bx - ax) ** 2 + (bz - az) ** 2);
5304
+ /** The rectangle r less h: up to four rectangles. */
5305
+ var cut$1 = (r, h) => {
5306
+ if (h[0] >= r[2] || h[2] <= r[0] || h[1] >= r[3] || h[3] <= r[1]) return [r];
5307
+ const out = [];
5308
+ const [x0, x1] = [Math.max(r[0], h[0]), Math.min(r[2], h[2])];
5309
+ if (h[0] > r[0]) out.push([
5310
+ r[0],
5311
+ r[1],
5312
+ h[0],
5313
+ r[3]
5314
+ ]);
5315
+ if (h[2] < r[2]) out.push([
5316
+ h[2],
5317
+ r[1],
5318
+ r[2],
5319
+ r[3]
5320
+ ]);
5321
+ if (h[1] > r[1]) out.push([
5322
+ x0,
5323
+ r[1],
5324
+ x1,
5325
+ h[1]
5326
+ ]);
5327
+ if (h[3] < r[3]) out.push([
5328
+ x0,
5329
+ h[3],
5330
+ x1,
5331
+ r[3]
5332
+ ]);
5333
+ return out;
5397
5334
  };
5398
- var built = /* @__PURE__ */ new WeakMap();
5399
- /** The bots' map of a world, built once per world. */
5400
- var navOf = (w) => {
5401
- let nav = built.get(w);
5402
- if (!nav) {
5403
- nav = build(w);
5404
- built.set(w, nav);
5335
+ /** Rectangles that share a whole side, merged, until none do. */
5336
+ var merge = (rs) => {
5337
+ for (let i = 0; i < rs.length; i++) for (let j = i + 1; j < rs.length; j++) {
5338
+ const [a, b] = [rs[i], rs[j]];
5339
+ const m = a[0] === b[0] && a[2] === b[2] && (a[3] === b[1] || b[3] === a[1]) ? [
5340
+ a[0],
5341
+ Math.min(a[1], b[1]),
5342
+ a[2],
5343
+ Math.max(a[3], b[3])
5344
+ ] : a[1] === b[1] && a[3] === b[3] && (a[2] === b[0] || b[2] === a[0]) ? [
5345
+ Math.min(a[0], b[0]),
5346
+ a[1],
5347
+ Math.max(a[2], b[2]),
5348
+ a[3]
5349
+ ] : void 0;
5350
+ if (m) {
5351
+ rs[i] = m;
5352
+ rs.splice(j, 1);
5353
+ j = i;
5354
+ }
5405
5355
  }
5406
- return nav;
5356
+ return rs;
5407
5357
  };
5408
- var build = (w) => {
5409
- const nodes = [];
5358
+ /** The height of a rectangle's floor at (x, z). */
5359
+ var heightAt = (w, p, x, z) => p.r ? ground(w, x, z) : p.y;
5360
+ /** The relief's ground as rectangles of whole squares, none over the hole, a pit or past the bound. */
5361
+ var lowlands = (w) => {
5362
+ const r = w.relief;
5363
+ if (!r) return [];
5364
+ const { x0, z0, cell, nx, nz, hole, pits, pitted, bound } = r;
5365
+ const [cx, cz, ea, eb] = bound;
5366
+ const [mx, mz] = [nx - 1, nz - 1];
5367
+ const over = (q, h) => q[0] < h[2] && q[2] > h[0] && q[1] < h[3] && q[3] > h[1];
5368
+ const free = new Uint8Array(mx * mz);
5369
+ for (let j = 0; j < mz; j++) for (let i = 0; i < mx; i++) {
5370
+ const q = [
5371
+ x0 + i * cell,
5372
+ z0 + j * cell,
5373
+ x0 + (i + 1) * cell,
5374
+ z0 + (j + 1) * cell
5375
+ ];
5376
+ let inside = true;
5377
+ for (const [x, z] of [
5378
+ [q[0], q[1]],
5379
+ [q[2], q[1]],
5380
+ [q[0], q[3]],
5381
+ [q[2], q[3]]
5382
+ ]) inside &&= ((x - cx) / ea) ** 2 + ((z - cz) / eb) ** 2 <= 1;
5383
+ free[i + j * mx] = +(inside && !over(q, hole) && !(pitted[i + j * nx] && pits.some((p) => over(q, p))));
5384
+ }
5410
5385
  const out = [];
5411
- const runUp = /* @__PURE__ */ new Set();
5412
- const node = (p) => {
5413
- let i = nodes.findIndex((n) => flat$2(n, p) < SAME && Math.abs(n[1] - p[1]) < SAME);
5414
- if (i < 0) {
5415
- i = nodes.push(p) - 1;
5416
- out.push([]);
5386
+ for (let j = 0; j < mz; j++) for (let i = 0; i < mx; i++) {
5387
+ if (!free[i + j * mx]) continue;
5388
+ let a = 1;
5389
+ while (i + a < mx && a < MERGE && free[i + a + j * mx]) a++;
5390
+ let b = 1;
5391
+ while (j + b < mz && b < MERGE) {
5392
+ let row = true;
5393
+ for (let k = 0; k < a; k++) row &&= !!free[i + k + (j + b) * mx];
5394
+ if (!row) break;
5395
+ b++;
5417
5396
  }
5418
- return i;
5419
- };
5420
- const chain = (start, legs, route = -1) => {
5421
- let at = node(start);
5422
- for (const l of legs) {
5423
- const to = node(end(l));
5424
- const tricky = l.k === "leap" && (l.dash !== void 0 || l.double !== void 0 || l.run);
5425
- const cost = (flat$2(nodes[at], from(l)) + flat$2(from(l), end(l))) / PACE + (l.k === "go" ? 0 : tricky ? TRICK : LEAP);
5426
- out[at].push({
5427
- to,
5428
- leg: l,
5429
- route,
5430
- cost
5397
+ for (let l = 0; l < b; l++) free.fill(0, i + (j + l) * mx, i + a + (j + l) * mx);
5398
+ out.push([
5399
+ x0 + i * cell,
5400
+ z0 + j * cell,
5401
+ x0 + (i + a) * cell,
5402
+ z0 + (j + b) * cell
5403
+ ]);
5404
+ }
5405
+ return out;
5406
+ };
5407
+ /**
5408
+ * The floor of a world as rectangles a body's middle may stand on: each brush's top, and the
5409
+ * relief's ground, less the footprint of what stands on it within a body's height: kept KEEP off
5410
+ * a wall, cut to the edge of a box low enough to step onto (which is a floor of its own).
5411
+ */
5412
+ var floors = (w) => {
5413
+ const { brushes, floor } = w.def;
5414
+ const polys = [];
5415
+ const add = (q, y, r, self) => {
5416
+ const s = Math.max(q[2] - q[0], q[3] - q[1]) / 2 + KEEP;
5417
+ let rs = [q];
5418
+ for (const i of near(w, (q[0] + q[2]) / 2, (q[1] + q[3]) / 2, s)) {
5419
+ const b = brushes[i];
5420
+ if (i === self || b.max[0] <= q[0] - KEEP || b.min[0] >= q[2] + KEEP) continue;
5421
+ if (b.max[2] <= q[1] - KEEP || b.min[2] >= q[3] + KEEP) continue;
5422
+ let [lo, hi] = [y, y];
5423
+ if (r) {
5424
+ const [ax, az] = [Math.max(q[0], b.min[0]), Math.max(q[1], b.min[2])];
5425
+ const [bx, bz] = [Math.min(q[2], b.max[0]), Math.min(q[3], b.max[2])];
5426
+ const gs = [
5427
+ [ax, az],
5428
+ [bx, az],
5429
+ [ax, bz],
5430
+ [bx, bz],
5431
+ [(ax + bx) / 2, (az + bz) / 2]
5432
+ ].map(([x, z]) => ground(w, x, z));
5433
+ [lo, hi] = [Math.min(...gs), Math.max(...gs)];
5434
+ }
5435
+ const top = b.max[1];
5436
+ if (top < lo - .05 || b.min[1] >= hi + 1.8 - .05) continue;
5437
+ if (top <= hi + .001 && (r || !(top >= y - .001 && i < self))) continue;
5438
+ const k = top - hi <= .5 ? 0 : KEEP;
5439
+ rs = rs.flatMap((c) => cut$1(c, [
5440
+ b.min[0] - k,
5441
+ b.min[2] - k,
5442
+ b.max[0] + k,
5443
+ b.max[2] + k
5444
+ ]));
5445
+ }
5446
+ for (const c of merge(rs)) {
5447
+ const [x, z] = [(c[0] + c[2]) / 2, (c[1] + c[3]) / 2];
5448
+ if (c[2] - c[0] > .1 && c[3] - c[1] > .1 && (r || ground(w, x, z) <= y + .05)) polys.push({
5449
+ q: c,
5450
+ y,
5451
+ ...r ? { r } : {}
5431
5452
  });
5432
- if (l.k === "leap" && l.sprint && legs[0] !== l) runUp.add(at);
5433
- at = to;
5434
5453
  }
5435
5454
  };
5455
+ brushes.forEach((b, i) => {
5456
+ if (b.max[1] > floor) add([
5457
+ b.min[0],
5458
+ b.min[2],
5459
+ b.max[0],
5460
+ b.max[2]
5461
+ ], b.max[1], false, i);
5462
+ });
5463
+ for (const q of lowlands(w)) add(q, 0, true, -1);
5464
+ return polys;
5465
+ };
5466
+ /** The cells a rectangle touches, `m` metres round it. */
5467
+ var cells = (q, m, f) => {
5468
+ for (let cx = Math.floor((q[0] - m) / CELL); cx <= Math.floor((q[2] + m) / CELL); cx++) for (let cz = Math.floor((q[1] - m) / CELL); cz <= Math.floor((q[3] + m) / CELL); cz++) f(key(cx, cz));
5469
+ };
5470
+ /**
5471
+ * Joins the rectangles that touch, or face each other across a crack of PORTAL at most, along a
5472
+ * side: walked either way when their heights are a step apart, dropped down when the lower is a
5473
+ * fall that does not hurt, jumped up onto a box of HOP at most.
5474
+ */
5475
+ var portals = (w, polys, grid) => {
5476
+ const out = polys.map(() => []);
5477
+ const deep = 256 / (2 * gravity(w));
5478
+ const met = new Int32Array(polys.length).fill(-1);
5479
+ polys.forEach((pa, i) => {
5480
+ cells(pa.q, PORTAL, (k) => {
5481
+ for (const j of grid.get(k) ?? []) {
5482
+ if (j <= i || met[j] === i) continue;
5483
+ met[j] = i;
5484
+ const [a, b] = [pa.q, polys[j].q];
5485
+ const gx = Math.max(b[0] - a[2], a[0] - b[2]);
5486
+ const gz = Math.max(b[1] - a[3], a[1] - b[3]);
5487
+ const alongZ = gx > -.001 && gx <= PORTAL && gz < -.1;
5488
+ if (!alongZ && !(gz > -.001 && gz <= PORTAL && gx < -.1)) continue;
5489
+ const c = alongZ ? a[2] <= b[0] + .001 ? (a[2] + b[0]) / 2 : (a[0] + b[2]) / 2 : a[3] <= b[1] + .001 ? (a[3] + b[1]) / 2 : (a[1] + b[3]) / 2;
5490
+ const [s0, s1] = alongZ ? [Math.max(a[1], b[1]), Math.min(a[3], b[3])] : [Math.max(a[0], b[0]), Math.min(a[2], b[2])];
5491
+ const m = (s0 + s1) / 2;
5492
+ const at = (s) => alongZ ? [c, s] : [s, c];
5493
+ const [mx, mz] = at(m);
5494
+ const dy = heightAt(w, polys[j], mx, mz) - heightAt(w, pa, mx, mz);
5495
+ const [pa0, pa1] = [at(s0), at(s1)];
5496
+ const join = (from, to, pass) => out[from].push({
5497
+ to,
5498
+ a: pa0,
5499
+ b: pa1,
5500
+ pass,
5501
+ cost: 0
5502
+ });
5503
+ if (Math.abs(dy) <= .55) {
5504
+ join(i, j, PASS.WALK);
5505
+ join(j, i, PASS.WALK);
5506
+ } else {
5507
+ const [up, down] = dy > 0 ? [j, i] : [i, j];
5508
+ const depth = (k) => {
5509
+ const q = polys[k].q;
5510
+ return alongZ ? q[2] - q[0] : q[3] - q[1];
5511
+ };
5512
+ if (Math.abs(dy) <= DROP$1 || Math.abs(dy) <= deep && depth(down) >= SPAN) join(up, down, PASS.DROP);
5513
+ if (Math.abs(dy) <= HOP$1 && depth(up) >= HOP_DEPTH$1) join(down, up, PASS.HOP);
5514
+ }
5515
+ }
5516
+ });
5517
+ });
5518
+ return out;
5519
+ };
5520
+ /** A rectangle with (x, z) on or within `m` of it and its floor the highest not over y + 0.6, else -1. */
5521
+ var under = (nav, w, x, y, z, m) => {
5522
+ let best = -1;
5523
+ let top = -Infinity;
5524
+ for (const i of nav.grid.get(key(Math.floor(x / CELL), Math.floor(z / CELL))) ?? []) {
5525
+ const p = nav.polys[i];
5526
+ const q = p.q;
5527
+ if (x < q[0] - m || x > q[2] + m || z < q[1] - m || z > q[3] + m) continue;
5528
+ const h = heightAt(w, p, Math.min(Math.max(x, q[0]), q[2]), Math.min(Math.max(z, q[1]), q[3]));
5529
+ if (h <= y + .6 && h > top) [best, top] = [i, h];
5530
+ }
5531
+ return best;
5532
+ };
5533
+ /** The point of a rectangle nearest (x, z), on its floor. */
5534
+ var onto = (w, p, x, z) => {
5535
+ const [cx, cz] = [Math.min(Math.max(x, p.q[0]), p.q[2]), Math.min(Math.max(z, p.q[1]), p.q[3])];
5536
+ return [
5537
+ cx,
5538
+ heightAt(w, p, cx, cz),
5539
+ cz
5540
+ ];
5541
+ };
5542
+ /** Metres to the nearest floor of a body on none, and how far above and below him it may be. */
5543
+ var NEAR$1 = 6;
5544
+ var ABOVE = 2.2;
5545
+ var BELOW = 8;
5546
+ /**
5547
+ * The rectangle a body at `v` stands on, or the nearest one within NEAR metres, at most ABOVE over
5548
+ * it and BELOW under it, as (index, the point of it to walk to); -1 when none.
5549
+ */
5550
+ var locate = (nav, w, v) => {
5551
+ const i = under(nav, w, v[0], v[1], v[2], PLAYER_R);
5552
+ if (i >= 0 && heightAt(w, nav.polys[i], v[0], v[2]) > v[1] - 1.5) return [i, onto(w, nav.polys[i], v[0], v[2])];
5553
+ let best = -1;
5554
+ let least = Infinity;
5555
+ let pt = v;
5556
+ const seen = /* @__PURE__ */ new Set();
5557
+ cells([
5558
+ v[0],
5559
+ v[2],
5560
+ v[0],
5561
+ v[2]
5562
+ ], NEAR$1, (k) => {
5563
+ for (const j of nav.grid.get(k) ?? []) {
5564
+ if (seen.has(j)) continue;
5565
+ seen.add(j);
5566
+ const o = onto(w, nav.polys[j], v[0], v[2]);
5567
+ const d = flat$2(v[0], v[2], o[0], o[2]) + Math.abs(o[1] - v[1]);
5568
+ if (d < least && o[1] - v[1] <= ABOVE && v[1] - o[1] <= BELOW) [best, least, pt] = [
5569
+ j,
5570
+ d,
5571
+ o
5572
+ ];
5573
+ }
5574
+ });
5575
+ return [least <= NEAR$1 ? best : -1, pt];
5576
+ };
5577
+ /**
5578
+ * Adds a route's legs from `start` as links: a jump with the run up to it, a slide; the runs
5579
+ * between are the mesh's. With `toll`, the first is a crossing's (see Edge.toll).
5580
+ */
5581
+ var links = (nav, w, start, legs, route = -1, toll = false) => {
5582
+ let at = start;
5583
+ legs.forEach((l, k) => {
5584
+ const next = legs[k + 1];
5585
+ if (l.k === "go" && next?.k === "leap" && next.sprint) return;
5586
+ const run = legs[k - 1]?.k === "go" && l.k === "leap" && l.sprint;
5587
+ const from = at;
5588
+ if (l.k !== "go") {
5589
+ const [i] = locate(nav, w, from);
5590
+ const [j] = locate(nav, w, end(l));
5591
+ if (i >= 0 && j >= 0) {
5592
+ const tricky = l.k === "leap" && (l.dash !== void 0 || l.double !== void 0 || l.run);
5593
+ const pts = run ? [
5594
+ from,
5595
+ end(legs[k - 1]),
5596
+ end(l)
5597
+ ] : [from, end(l)];
5598
+ let len = 0;
5599
+ for (let n = 1; n < pts.length; n++) len += flat$2(pts[n - 1][0], pts[n - 1][2], pts[n][0], pts[n][2]);
5600
+ nav.out[i].push({
5601
+ to: j,
5602
+ from,
5603
+ end: end(l),
5604
+ legs: run ? [legs[k - 1], l] : [l],
5605
+ route,
5606
+ cost: len / PACE + (tricky ? TRICK : LEAP),
5607
+ ...toll && k === 0 ? { toll } : {}
5608
+ });
5609
+ }
5610
+ }
5611
+ at = end(l);
5612
+ });
5613
+ };
5614
+ var build = (w) => {
5615
+ const polys = floors(w);
5616
+ const grid = /* @__PURE__ */ new Map();
5617
+ polys.forEach((p, i) => cells(p.q, PORTAL, (k) => {
5618
+ const list = grid.get(k);
5619
+ if (list) list.push(i);
5620
+ else grid.set(k, [i]);
5621
+ }));
5622
+ const route = new Int8Array(polys.length).fill(-1);
5623
+ const nav = {
5624
+ polys,
5625
+ out: portals(w, polys, grid),
5626
+ grid,
5627
+ route
5628
+ };
5436
5629
  if (w.def === HIGHLINE) {
5437
5630
  const [red] = w.def.teams;
5438
5631
  for (const f of [(l) => l, flip]) {
@@ -5443,140 +5636,263 @@ var build = (w) => {
5443
5636
  ] : p;
5444
5637
  ROUTES.forEach((r, k) => {
5445
5638
  const [sx, sy, sz] = red.spawns[r.start];
5446
- chain(z([
5639
+ links(nav, w, z([
5447
5640
  sx,
5448
5641
  sy,
5449
5642
  sz
5450
5643
  ]), r.out.map(f), k);
5451
- if (r.flag.length) chain(z(red.base), [...r.flag, ...r.out].map(f), k);
5452
- chain(z(r.mid), r.in.map(f), k);
5644
+ if (r.flag.length) links(nav, w, z(red.base), [...r.flag, ...r.out].map(f), k);
5645
+ links(nav, w, z(r.mid), r.in.map(f), k);
5453
5646
  });
5454
- for (const u of Object.values(UP)) chain(z(u.from), u.legs.map(f));
5647
+ for (const u of Object.values(UP)) links(nav, w, z(u.from), u.legs.map(f));
5455
5648
  }
5456
5649
  }
5457
5650
  if (w.def === MOON) {
5458
- for (const [route, [x, y, z], legs] of CROSSINGS) {
5459
- chain([
5651
+ polys.forEach(({ q }, i) => {
5652
+ const a = ACROSS.find(([x0, x1]) => q[0] < x1 && q[2] > x0 && q[1] < 140 && q[3] > 140);
5653
+ if (a) route[i] = a[2];
5654
+ });
5655
+ for (const [k, [x, y, z], legs] of CROSSINGS) {
5656
+ links(nav, w, [
5460
5657
  x,
5461
5658
  y,
5462
5659
  z
5463
- ], legs, route);
5464
- chain([
5660
+ ], legs, k, true);
5661
+ links(nav, w, [
5465
5662
  x,
5466
5663
  y,
5467
5664
  280 - z
5468
- ], legs.map((l) => mirror(l, 280)), route);
5665
+ ], legs.map((l) => mirror(l, 280)), k, true);
5469
5666
  }
5470
- [...LANES, ...LANES.map((l) => l.map(([x, y, z]) => [
5471
- x,
5472
- y,
5473
- 280 - z
5474
- ]))].forEach((lane, i) => {
5475
- const route = i % LANES.length === BRIDGE ? 0 : -1;
5476
- for (let k = 1; k < lane.length; k++) {
5477
- const [a, b] = [node(lane[k - 1]), node(lane[k])];
5478
- const cost = flat$2(nodes[a], nodes[b]) / PACE;
5479
- out[a].push({
5480
- to: b,
5481
- route,
5482
- cost
5483
- });
5484
- out[b].push({
5485
- to: a,
5486
- route,
5487
- cost
5488
- });
5489
- }
5490
- });
5491
5667
  }
5492
- for (let i = 0; i < nodes.length; i++) for (let j = 0; j < nodes.length; j++) {
5493
- const d = flat$2(nodes[i], nodes[j]);
5494
- if (i !== j && !runUp.has(j) && d <= WALK && walkable(w, nodes[i], nodes[j])) out[i].push({
5495
- to: j,
5496
- route: -1,
5497
- cost: d / PACE
5498
- });
5668
+ return nav;
5669
+ };
5670
+ var built = /* @__PURE__ */ new WeakMap();
5671
+ /** The bots' map of a world, built once per world. */
5672
+ var navOf = (w) => {
5673
+ let nav = built.get(w);
5674
+ if (!nav) {
5675
+ nav = build(w);
5676
+ built.set(w, nav);
5499
5677
  }
5500
- return {
5501
- nodes,
5502
- out,
5503
- runUp
5504
- };
5678
+ return nav;
5505
5679
  };
5680
+ /** Twice the signed area of the triangle a, b, c on the floor plane. */
5681
+ var area = (a, b, c) => (c[0] - a[0]) * (b[1] - a[1]) - (b[0] - a[0]) * (c[1] - a[1]);
5506
5682
  /**
5507
- * The node a bot goes to, or from, when no straight walk joins it to the point `p`: among the nodes
5508
- * `ok` takes, within NEAR, the best by `weigh` whose height off `p` a body manages (a stair, a
5509
- * ledge to climb, a drop that does not hurt), `up` 1 when the bot goes from `p` to the node and -1
5510
- * when it goes from the node to `p`; with nothing in the way of the head first, then even hidden.
5511
- */
5512
- var close = (w, nodes, p, ok, weigh, up) => {
5513
- for (const clear of [true, false]) {
5514
- let best = -1;
5515
- let least = Infinity;
5516
- for (let i = 0; i < nodes.length; i++) {
5517
- const [d, dy] = [flat$2(p, nodes[i]), (nodes[i][1] - p[1]) * up];
5518
- const [ux, uz] = [(nodes[i][0] - p[0]) / (d || 1), (nodes[i][2] - p[2]) / (d || 1)];
5519
- const head = Math.max(p[1], nodes[i][1]) + 1.5;
5520
- if (ok(i) && d < NEAR && dy <= ABOVE && dy >= -8 && weigh(d, i) < least && (!clear || ray(w, [
5521
- p[0],
5522
- head,
5523
- p[2]
5524
- ], [
5525
- ux,
5526
- 0,
5527
- uz
5528
- ], d) >= d)) [best, least] = [i, weigh(d, i)];
5683
+ * The corners of the shortest line from `s` to `t` through the portals, each [left, right] as one
5684
+ * goes (the simple stupid funnel algorithm), `t` last.
5685
+ */
5686
+ var funnel = (s, ports, t) => {
5687
+ const ps = [...ports, [t, t]];
5688
+ const out = [];
5689
+ let [apex, left, right] = [
5690
+ s,
5691
+ s,
5692
+ s
5693
+ ];
5694
+ let [ai, li, ri] = [
5695
+ -1,
5696
+ -1,
5697
+ -1
5698
+ ];
5699
+ for (let i = 0; i < ps.length; i++) {
5700
+ const [l, r] = ps[i];
5701
+ if (area(apex, right, r) <= 0) {
5702
+ if (apex === right || area(apex, left, r) > 0) [right, ri] = [r, i];
5703
+ else {
5704
+ out.push(left);
5705
+ [apex, ai] = [left, li];
5706
+ [left, right, ri] = [
5707
+ apex,
5708
+ apex,
5709
+ ai
5710
+ ];
5711
+ i = ai;
5712
+ continue;
5713
+ }
5714
+ }
5715
+ if (area(apex, left, l) >= 0) {
5716
+ if (apex === left || area(apex, right, l) < 0) [left, li] = [l, i];
5717
+ else {
5718
+ out.push(right);
5719
+ [apex, ai] = [right, ri];
5720
+ [left, right, li] = [
5721
+ apex,
5722
+ apex,
5723
+ ai
5724
+ ];
5725
+ i = ai;
5726
+ continue;
5727
+ }
5529
5728
  }
5530
- if (best >= 0) return best;
5531
5729
  }
5532
- return -1;
5730
+ if (out.at(-1) !== t) out.push(t);
5731
+ return out;
5732
+ };
5733
+ /** A binary heap of indices by cost, smallest first. */
5734
+ var pop = (h, f) => {
5735
+ const top = h[0];
5736
+ const last = h.pop();
5737
+ if (h.length) {
5738
+ let i = 0;
5739
+ for (;;) {
5740
+ const [l, r] = [2 * i + 1, 2 * i + 2];
5741
+ let m = i;
5742
+ if (l < h.length && f[h[l]] < f[last] && (r >= h.length || f[h[l]] <= f[h[r]])) m = l;
5743
+ else if (r < h.length && f[h[r]] < f[last]) m = r;
5744
+ if (m === i) break;
5745
+ h[i] = h[m];
5746
+ i = m;
5747
+ }
5748
+ h[i] = last;
5749
+ }
5750
+ return top;
5751
+ };
5752
+ var push = (h, f, v) => {
5753
+ let i = h.push(v) - 1;
5754
+ while (i > 0) {
5755
+ const p = i - 1 >> 1;
5756
+ if (f[h[p]] <= f[v]) break;
5757
+ h[i] = h[p];
5758
+ i = p;
5759
+ }
5760
+ h[i] = v;
5761
+ };
5762
+ /** The point of the segment a b nearest (x, z). */
5763
+ var nearest = (a, b, x, z) => {
5764
+ const [dx, dz] = [b[0] - a[0], b[1] - a[1]];
5765
+ const t = Math.min(Math.max(((x - a[0]) * dx + (z - a[1]) * dz) / (dx * dx + dz * dz || 1), 0), 1);
5766
+ return [a[0] + dx * t, a[1] + dz * t];
5533
5767
  };
5534
5768
  /**
5535
- * The cheapest way from the point `a` to the point `b`. No entry when `a` walks straight to `b`, or
5536
- * when the map has no way. The legs of the routes other than `prefer` (an index of ROUTES) cost
5537
- * more, so bots that prefer different ones spread over the map.
5769
+ * The points a bot goes through from `a` to `b`, the last `b` or, when no way gets there, the
5770
+ * nearest point it can get to. The links of the routes other than `prefer` (an index of ROUTES)
5771
+ * cost more, so bots that prefer different ones spread over the map. Off the mesh, `b` alone.
5538
5772
  */
5539
5773
  var path = (nav, w, a, b, prefer = -1) => {
5540
- const { nodes, out, runUp } = nav;
5541
- if (!nodes.length || walkable(w, a, b)) return { edges: [] };
5542
- const n = nodes.length;
5543
- const cost = new Array(n).fill(Infinity);
5774
+ const [s, sp] = locate(nav, w, a);
5775
+ const [t, tp] = locate(nav, w, b);
5776
+ if (s < 0) return [{ to: b }];
5777
+ const n = nav.polys.length;
5778
+ const g = new Float64Array(n).fill(Infinity);
5779
+ const f = new Float64Array(n).fill(Infinity);
5544
5780
  const prev = new Array(n);
5545
- const back = new Array(n).fill(-1);
5546
- const done = new Array(n).fill(false);
5547
- for (let i = 0; i < n; i++) if (!runUp.has(i) && flat$2(a, nodes[i]) <= WALK && walkable(w, a, nodes[i])) cost[i] = flat$2(a, nodes[i]) / PACE;
5548
- if (!cost.some((c) => c < Infinity)) {
5549
- const first = close(w, nodes, a, (i) => !runUp.has(i), (d) => d, 1);
5550
- if (first >= 0) cost[first] = flat$2(a, nodes[first]) / PACE;
5551
- }
5552
- for (;;) {
5553
- let i = -1;
5554
- for (let k = 0; k < n; k++) if (!done[k] && cost[k] < Infinity && (i < 0 || cost[k] < cost[i])) i = k;
5555
- if (i < 0) break;
5556
- done[i] = true;
5557
- for (const e of out[i]) {
5558
- const c = cost[i] + e.cost * (e.route >= 0 && prefer >= 0 && e.route !== prefer ? OTHER : 1);
5559
- if (c < cost[e.to]) {
5560
- cost[e.to] = c;
5781
+ const back = new Int32Array(n).fill(-1);
5782
+ const at = new Float64Array(2 * n);
5783
+ const done = new Uint8Array(n);
5784
+ const h = (i) => flat$2(at[2 * i], at[2 * i + 1], b[0], b[2]) / PACE;
5785
+ g[s] = flat$2(a[0], a[2], sp[0], sp[2]) / PACE;
5786
+ [at[2 * s], at[2 * s + 1]] = [sp[0], sp[2]];
5787
+ f[s] = g[s] + h(s);
5788
+ const heap = [s];
5789
+ let close = s;
5790
+ while (heap.length) {
5791
+ const i = pop(heap, f);
5792
+ if (done[i]) continue;
5793
+ done[i] = 1;
5794
+ if (h(i) < h(close)) close = i;
5795
+ if (i === t) break;
5796
+ const [x, z] = [at[2 * i], at[2 * i + 1]];
5797
+ for (const e of nav.out[i]) {
5798
+ if (done[e.to]) continue;
5799
+ let c;
5800
+ let p;
5801
+ if (e.legs) {
5802
+ const [fx, , fz] = e.from;
5803
+ const other = e.route < 0 || prefer < 0 ? 1 : e.route === prefer ? 1 / OTHER : OTHER;
5804
+ c = flat$2(x, z, fx, fz) / PACE + e.cost * other * (1 + (e.fails ?? 0)) + (e.toll && other > 1 ? AREA : 0);
5805
+ p = [e.end[0], e.end[2]];
5806
+ } else {
5807
+ p = nearest(e.a, e.b, x, z);
5808
+ const r = nav.route[e.to];
5809
+ c = flat$2(x, z, p[0], p[1]) / PACE + (e.pass === PASS.HOP ? HOP_COST : e.pass ? DOWN : 0) + (r >= 0 && prefer >= 0 && r !== prefer && nav.route[i] !== r ? AREA : 0);
5810
+ }
5811
+ if (g[i] + c < g[e.to]) {
5812
+ g[e.to] = g[i] + c;
5561
5813
  prev[e.to] = e;
5562
5814
  back[e.to] = i;
5815
+ [at[2 * e.to], at[2 * e.to + 1]] = p;
5816
+ f[e.to] = g[e.to] + h(e.to);
5817
+ push(heap, f, e.to);
5563
5818
  }
5564
5819
  }
5565
5820
  }
5566
- let best = -1;
5567
- let total = Infinity;
5568
- for (let i = 0; i < n; i++) {
5569
- const c = cost[i] + flat$2(nodes[i], b) / PACE;
5570
- if (cost[i] < Infinity && c < total && flat$2(nodes[i], b) <= WALK && walkable(w, nodes[i], b)) [best, total] = [i, c];
5571
- }
5572
- if (best < 0) best = close(w, nodes, b, (i) => cost[i] < Infinity, (d, i) => cost[i] + d / PACE, -1);
5821
+ const goal = t >= 0 && done[t] ? t : close;
5822
+ const target = goal !== t ? onto(w, nav.polys[goal], b[0], b[2]) : flat$2(b[0], b[2], tp[0], tp[2]) < .5 ? b : tp;
5573
5823
  const edges = [];
5574
- let i = best;
5575
- for (; i >= 0 && prev[i]; i = back[i]) edges.unshift(prev[i]);
5576
- return i < 0 ? { edges: [] } : {
5577
- entry: nodes[i],
5578
- edges
5824
+ for (let i = goal; i !== s && prev[i]; i = back[i]) edges.unshift(prev[i]);
5825
+ const steps = [];
5826
+ let from = a;
5827
+ let polyAt = s;
5828
+ let ports = [];
5829
+ let heights = [];
5830
+ const walk = (to) => {
5831
+ const pts = funnel([from[0], from[2]], ports, [to[0], to[2]]);
5832
+ for (const [k, q] of pts.entries()) if (k < pts.length - 1) {
5833
+ const y = heights[ports.findIndex((pt) => pt[0] === q || pt[1] === q)] ?? to[1];
5834
+ steps.push({ to: [
5835
+ q[0],
5836
+ y,
5837
+ q[1]
5838
+ ] });
5839
+ }
5840
+ steps.push({ to });
5841
+ ports = [];
5842
+ heights = [];
5579
5843
  };
5844
+ for (const e of edges) {
5845
+ if (e.legs) {
5846
+ const [fx, , fz] = e.from;
5847
+ if (ports.length || !steps.at(-1)?.leg || flat$2(from[0], from[2], fx, fz) > 1) {
5848
+ const l = e.legs[0];
5849
+ if (l.k === "leap") {
5850
+ const d = flat$2(l.at[0], l.at[1], l.to[0], l.to[1]) || 1;
5851
+ const [rx, rz] = [fx - (l.to[0] - l.at[0]) / d * RUN_IN, fz - (l.to[1] - l.at[1]) / d * RUN_IN];
5852
+ let level = true;
5853
+ for (let u = 0; u <= 1 && level; u += .25) {
5854
+ const [ux, uz] = [rx + (fx - rx) * u, rz + (fz - rz) * u];
5855
+ const k = under(nav, w, ux, e.from[1], uz, 0);
5856
+ level = k >= 0 && Math.abs(heightAt(w, nav.polys[k], ux, uz) - e.from[1]) < .3;
5857
+ }
5858
+ if (level) {
5859
+ walk([
5860
+ rx,
5861
+ e.from[1],
5862
+ rz
5863
+ ]);
5864
+ from = [
5865
+ rx,
5866
+ e.from[1],
5867
+ rz
5868
+ ];
5869
+ }
5870
+ }
5871
+ walk(e.from);
5872
+ }
5873
+ for (const l of e.legs) steps.push({
5874
+ to: end(l),
5875
+ leg: l,
5876
+ edge: e
5877
+ });
5878
+ from = e.end;
5879
+ } else {
5880
+ const [p, q] = [nav.polys[polyAt].q, nav.polys[e.to].q];
5881
+ const [dx, dz] = [(q[0] + q[2] - p[0] - p[2]) / 2, (q[1] + q[3] - p[1] - p[3]) / 2];
5882
+ const [ax, az] = e.a;
5883
+ const [bx, bz] = e.b;
5884
+ const len = flat$2(ax, az, bx, bz);
5885
+ const k = Math.min(EDGE, len * .4) / (len || 1);
5886
+ const ea = [ax + (bx - ax) * k, az + (bz - az) * k];
5887
+ const eb = [bx - (bx - ax) * k, bz - (bz - az) * k];
5888
+ const right = dx * (eb[1] - ea[1]) - dz * (eb[0] - ea[0]) < 0;
5889
+ ports.push(right ? [ea, eb] : [eb, ea]);
5890
+ heights.push(heightAt(w, nav.polys[e.to], (ax + bx) / 2, (az + bz) / 2));
5891
+ }
5892
+ polyAt = e.to;
5893
+ }
5894
+ walk(target);
5895
+ return steps;
5580
5896
  };
5581
5897
  ARC_SPEED / 60;
5582
5898
  /** How far shot `n` of the laser rifle drifts off the look, yaw and pitch: a pattern within RIFLE_DRIFT, the same for the host and every client. */
@@ -7312,6 +7628,8 @@ var SORTIE = {
7312
7628
  EAST: 6,
7313
7629
  LAND: 10
7314
7630
  };
7631
+ /** Metres before a ship's centre a bot walks to, to board it. */
7632
+ var NOSE = 4;
7315
7633
  var flat$1 = (ax, az, bx, bz) => Math.sqrt((bx - ax) ** 2 + (bz - az) ** 2);
7316
7634
  var toward$1 = (x0, z0, x1, z1) => norm(Math.atan2(-(x1 - x0), -(z1 - z0)));
7317
7635
  var clamp = (v, a, b) => Math.min(b, Math.max(a, v));
@@ -7457,10 +7775,11 @@ var Sortie = class {
7457
7775
  yaw: toward$1(p.x, p.z, ship.x, ship.z),
7458
7776
  pitch: 0
7459
7777
  };
7778
+ const [fx, fz] = forward(ship.yaw);
7460
7779
  this.board = [
7461
- ship.x,
7780
+ ship.x + fx * NOSE,
7462
7781
  ship.y - SHIP.DROP,
7463
- ship.z
7782
+ ship.z + fz * NOSE
7464
7783
  ];
7465
7784
  }
7466
7785
  }
@@ -7495,6 +7814,17 @@ var { FORWARD, BACK, LEFT, RIGHT, JUMP, SPRINT, DASH, CROUCH, FIRE, RELOAD } = K
7495
7814
  var RANGE = 35;
7496
7815
  /** Radians off the target a bot still shoots. */
7497
7816
  var CONE = .06;
7817
+ /** Half the angle a bot sees ahead of its look, radians, and metres all round it hears an enemy. */
7818
+ var VIEW = 1.1;
7819
+ var HEAR = 5;
7820
+ /** Ticks a bot remembers an enemy it fought: met again within them, he gets no warning shot. */
7821
+ var MEMORY = 180;
7822
+ /** Bots that shoot to hit the same enemy at once; the others shoot past him. */
7823
+ var TOKENS = 2;
7824
+ /** Metres a second across a bot's view (its own speed with the target's) that double its aim's wander. */
7825
+ var CROSS = 6;
7826
+ /** Metres a shot that is meant to miss passes beside the body. */
7827
+ var PAST = 1.6;
7498
7828
  /** A number in [0, 1) that looks random, the same for the same n. */
7499
7829
  var frac = (n) => {
7500
7830
  const s = Math.sin(n * 12.9898) * 43758.5453;
@@ -7506,11 +7836,15 @@ var personaOf = (id, skill = 4) => {
7506
7836
  const s = Math.max(0, Math.min(7, skill)) / 7;
7507
7837
  return {
7508
7838
  miss: (.3 - .22 * s) * (.75 + .5 * r(1)),
7509
- react: Math.round((36 - 28 * s) * (.75 + .5 * r(2))),
7510
- turn: .05 + .09 * s,
7839
+ react: Math.round((40 - 24 * s) * (.75 + .5 * r(2))),
7840
+ turn: .04 + .06 * s,
7511
7841
  jumpy: .003 + .012 * r(3),
7512
7842
  strafe: Math.round(25 + 45 * r(4)),
7513
- duck: .15 + .75 * s
7843
+ duck: .1 + .6 * s,
7844
+ warn: Math.round(1 + 2 * (1 - s) + r(5)),
7845
+ shake: (.15 - .1 * s) * (.75 + .5 * r(6)),
7846
+ lead: .3 + .6 * s,
7847
+ track: .03 + .09 * s
7514
7848
  };
7515
7849
  };
7516
7850
  /** One jump in DASHY that a fighting bot makes is a dash aside instead; metres it wants open for it. */
@@ -7573,8 +7907,44 @@ var DASH_FAR = 8;
7573
7907
  var DASH_GAP = 120;
7574
7908
  /** Metres of floor a bot wants on the side it strafes to. */
7575
7909
  var SIDE = 1.5;
7576
- /** The bots whose id is GUARD - 1 modulo GUARD stay home. */
7577
- var GUARD = 8;
7910
+ /** Health a player is in trouble at or under, and metres off him an enemy makes it trouble. */
7911
+ var LOW = 3;
7912
+ var NEAR = 12;
7913
+ /** Metres a bot goes out of its way to a health pack when it is in trouble, or to help a teammate. */
7914
+ var MEND = 30;
7915
+ var HELP = 40;
7916
+ /**
7917
+ * The roles of a team's bots this tick, the same in every bot's head (they see the same team):
7918
+ * the nearest to each job takes it, the most pressing jobs first. Half the team or more chase the
7919
+ * enemy who has the flag (all but an escort when ours has theirs too: no capture till ours is
7920
+ * home), one or two return a lying flag, two escort the carrier, one goes to a teammate an enemy
7921
+ * is on while he is low, one guards home in a squad of three or more, and the rest attack.
7922
+ */
7923
+ var squad = (team, players, flags, world) => {
7924
+ const [mine, theirs] = [flags[team], flags[1 - team]];
7925
+ const roles = /* @__PURE__ */ new Map();
7926
+ const up = players.filter((o) => !o.state.down);
7927
+ const by = (id) => up.find((o) => o.id === id);
7928
+ const ours = theirs.state === FLAG$1.CARRIED ? by(theirs.by) : void 0;
7929
+ const enemy = mine.state === FLAG$1.CARRIED ? by(mine.by) : void 0;
7930
+ const trouble = up.filter((o) => o.team === team && o.state.hp <= LOW).filter((o) => up.some((f) => f.team !== team && flat(f.state.x, f.state.z, o.state.x, o.state.z) < NEAR)).sort((a, b) => a.state.hp - b.state.hp)[0];
7931
+ const free = up.filter((o) => o.team === team && o.bot && o !== ours && o !== trouble).sort((a, b) => a.id - b.id);
7932
+ const n = free.length;
7933
+ const take = (role, count, x, z, far = Infinity) => free.filter((o) => !roles.has(o.id) && flat(o.state.x, o.state.z, x, z) < far).sort((a, b) => flat(a.state.x, a.state.z, x, z) - flat(b.state.x, b.state.z, x, z)).slice(0, count).forEach((o) => roles.set(o.id, role));
7934
+ if (enemy) take("chase", ours ? Math.max(1, n - 1) : Math.ceil(n / 2), enemy.state.x, enemy.state.z);
7935
+ if (mine.state === FLAG$1.DROPPED) take("return", n >= 4 ? 2 : 1, mine.x, mine.z);
7936
+ if (ours) take("escort", 2, ours.state.x, ours.state.z);
7937
+ if (trouble) take("help", 1, trouble.state.x, trouble.state.z, HELP);
7938
+ if (n >= 3 && !enemy) {
7939
+ const [x, , z] = world.def.teams[team].base;
7940
+ take("guard", 1, x, z);
7941
+ }
7942
+ free.forEach((o) => roles.has(o.id) || roles.set(o.id, "attack"));
7943
+ return {
7944
+ roles,
7945
+ trouble
7946
+ };
7947
+ };
7578
7948
  var flat = (ax, az, bx, bz) => Math.sqrt((bx - ax) ** 2 + (bz - az) ** 2);
7579
7949
  /** Yaw that faces from (x0, z0) toward (x1, z1): yaw 0 looks toward -z. */
7580
7950
  var toward = (x0, z0, x1, z1) => norm(Math.atan2(-(x1 - x0), -(z1 - z0)));
@@ -7607,13 +7977,13 @@ var Bot = class {
7607
7977
  id;
7608
7978
  /** The route it prefers, an index of nav.ts's ROUTES. */
7609
7979
  prefer;
7610
- nav;
7611
- way = { edges: [] };
7980
+ /** Its way: the points it goes through, walking or on a leg; the last is its goal or as near as it gets. */
7981
+ steps = [];
7612
7982
  /** The goal of the way, and the tick it was planned. */
7613
7983
  goal;
7614
7984
  planned = 0;
7615
- /** -1 walking to the way's entry, then the edge it is on; edges.length walking to the goal. */
7616
- leg = 0;
7985
+ /** The step it is on; steps.length once there. */
7986
+ step = 0;
7617
7987
  flight;
7618
7988
  /** The best distance to the point it walks to, and the tick it was last bettered. */
7619
7989
  best = Infinity;
@@ -7624,6 +7994,22 @@ var Bot = class {
7624
7994
  pitch = 0;
7625
7995
  foe = -1;
7626
7996
  since = 0;
7997
+ /** Its aim's wander off the target, yaw and pitch, and the target's speed as it has made it out. */
7998
+ err = 0;
7999
+ errUp = 0;
8000
+ speed = [
8001
+ 0,
8002
+ 0,
8003
+ 0
8004
+ ];
8005
+ /** Its shot count when it met the target, the shots it fires past him first, and to which side. */
8006
+ shot = 0;
8007
+ warned = 0;
8008
+ past = 1;
8009
+ /** The tick it last fought each enemy, by id. */
8010
+ met = /* @__PURE__ */ new Map();
8011
+ /** The enemy it shot to hit this tick, -1 for none: the room hands it to the others as `hunters`. */
8012
+ hunting = -1;
7627
8013
  last = [
7628
8014
  0,
7629
8015
  0,
@@ -7675,21 +8061,18 @@ var Bot = class {
7675
8061
  * goes to now, what it is doing on the way, the enemy it fights and the keys it last pressed.
7676
8062
  */
7677
8063
  view() {
7678
- const { entry, edges } = this.way;
7679
8064
  const r = (v) => [
7680
8065
  v[0],
7681
8066
  v[1],
7682
8067
  v[2]
7683
8068
  ].map((n) => Math.round(n * 10) / 10);
7684
- const path = [...entry ? [entry] : [], ...edges.map((e) => this.nav.nodes[e.to])];
7685
- if (this.goal) path.push(this.goal);
7686
- const e = edges[this.leg];
8069
+ const s = this.steps[this.step];
7687
8070
  return {
7688
8071
  id: this.id,
7689
8072
  why: this.why,
7690
- path: path.map(r),
7691
- next: this.leg < 0 ? 0 : this.leg + (entry ? 1 : 0),
7692
- doing: !this.goal ? "no way" : this.leg < 0 ? "to entry" : e ? e.leg?.k ?? "walk" : "to goal",
8073
+ path: this.steps.map((st) => r(st.to)),
8074
+ next: Math.max(0, Math.min(this.step, this.steps.length - 1)),
8075
+ doing: !this.goal ? "no way" : !s ? "there" : s.leg ? s.leg.k : this.step === this.steps.length - 1 ? "to goal" : "walk",
7693
8076
  foe: this.foe,
7694
8077
  keys: this.keys
7695
8078
  };
@@ -7698,6 +8081,7 @@ var Bot = class {
7698
8081
  decide(s) {
7699
8082
  const t = ++this.ticks;
7700
8083
  const p = s.me.state;
8084
+ this.hunting = -1;
7701
8085
  this.world = s.world;
7702
8086
  this.steer = !!s.kit && s.kit.jump !== 7;
7703
8087
  if (p.down) {
@@ -7730,7 +8114,7 @@ var Bot = class {
7730
8114
  yaw: p.yaw,
7731
8115
  pitch: 0
7732
8116
  };
7733
- if (p.ground && (!this.goal || flat(goal[0], goal[2], this.goal[0], this.goal[2]) > Math.max(DRIFT, SHARE * flat(p.x, p.z, goal[0], goal[2])) || !this.way.edges.length && t - this.planned > REPLAN)) this.plan(s.world, p, goal);
8117
+ if (p.ground && (!this.goal || flat(goal[0], goal[2], this.goal[0], this.goal[2]) > Math.max(DRIFT, SHARE * flat(p.x, p.z, goal[0], goal[2])) || !this.steps.some((st) => st.leg) && t - this.planned > REPLAN)) this.plan(s.world, p, goal);
7734
8118
  const move = this.pilot(p);
7735
8119
  const fight = this.fight(s, move.free);
7736
8120
  const reload = p.ammo > 0 && !p.mag && !p.reload && t & 1 ? RELOAD : 0;
@@ -7879,14 +8263,24 @@ var Bot = class {
7879
8263
  this.why = "hunt";
7880
8264
  return at(prey.state);
7881
8265
  }
7882
- if (mine.state === FLAG$1.CARRIED) {
7883
- const h = holder(mine);
7884
- if (h) {
7885
- this.why = "chase carrier";
7886
- return h;
8266
+ const { roles, trouble } = squad(team, [{
8267
+ ...me,
8268
+ bot: true
8269
+ }, ...others], flags, world);
8270
+ const role = roles.get(me.id);
8271
+ if (p.hp <= LOW && role !== "chase") {
8272
+ const pack = s.packs?.map((q) => [q, flat(p.x, p.z, q[0], q[2])]).filter(([, d]) => d < MEND).sort((a, b) => a[1] - b[1])[0];
8273
+ if (pack) {
8274
+ this.why = "health";
8275
+ return pack[0];
7887
8276
  }
7888
8277
  }
7889
- if (mine.state === FLAG$1.DROPPED) {
8278
+ const h = role === "chase" ? holder(mine) : role === "escort" ? holder(theirs) : void 0;
8279
+ if (h) {
8280
+ this.why = role === "chase" ? "chase carrier" : "escort";
8281
+ return h;
8282
+ }
8283
+ if (role === "return") {
7890
8284
  this.why = "return flag";
7891
8285
  return [
7892
8286
  mine.x,
@@ -7894,57 +8288,56 @@ var Bot = class {
7894
8288
  mine.z
7895
8289
  ];
7896
8290
  }
7897
- if (this.id % GUARD === 7) {
8291
+ if (role === "help" && trouble) {
8292
+ this.why = "help";
8293
+ return at(trouble.state);
8294
+ }
8295
+ if (role === "guard") {
7898
8296
  this.why = "guard";
7899
8297
  return home.base;
7900
8298
  }
7901
- if (theirs.state === FLAG$1.CARRIED) {
7902
- const h = holder(theirs);
7903
- if (h) {
7904
- this.why = "escort";
7905
- return h;
7906
- }
7907
- }
7908
- if (theirs.state === FLAG$1.DROPPED) {
7909
- this.why = "take flag";
7910
- return [
7911
- theirs.x,
7912
- theirs.y,
7913
- theirs.z
7914
- ];
7915
- }
7916
8299
  this.why = "take flag";
7917
- return world.def.teams[1 - team].base;
8300
+ return theirs.state === FLAG$1.DROPPED ? [
8301
+ theirs.x,
8302
+ theirs.y,
8303
+ theirs.z
8304
+ ] : world.def.teams[1 - team].base;
7918
8305
  }
7919
8306
  plan(w, p, goal) {
7920
8307
  this.goal = goal;
7921
8308
  this.planned = this.ticks;
7922
- this.nav = navOf(w);
7923
- this.way = path(this.nav, w, at(p), goal, this.prefer);
7924
- this.leg = this.way.entry ? -1 : 0;
8309
+ const prefer = this.why === "flag home" && !this.steer ? -1 : this.prefer;
8310
+ this.steps = path(navOf(w), w, at(p), goal, prefer);
8311
+ this.step = 0;
7925
8312
  this.flight = void 0;
7926
8313
  this.best = Infinity;
7927
8314
  this.better = this.ticks;
7928
8315
  }
7929
- /** The point it walks straight to now: the entry, a walk's end, or the goal past the last edge. */
7930
- target() {
7931
- const { edges, entry } = this.way;
7932
- if (this.leg < 0) return entry;
7933
- const e = edges[this.leg];
7934
- return e ? this.nav.nodes[e.to] : this.goal;
7935
- }
7936
8316
  /**
7937
8317
  * The keys and yaw of the move, whether the bot may look away from it to shoot, and whether the
7938
8318
  * yaw must be kept to the letter (a leg of a route, as the tests fly it).
7939
8319
  */
7940
8320
  pilot(p) {
7941
- const e = this.leg >= 0 ? this.way.edges[this.leg] : void 0;
7942
- if (e?.leg) return this.fly(p, e.leg);
7943
- const [x, , z] = this.target();
8321
+ const s = this.steps[this.step];
8322
+ if (s?.leg) return this.fly(p, s.leg);
8323
+ if (!s) return {
8324
+ keys: 0,
8325
+ yaw: this.aim,
8326
+ free: true,
8327
+ exact: false
8328
+ };
8329
+ const [x, y, z] = s.to;
7944
8330
  const d = flat(p.x, p.z, x, z);
8331
+ if (!p.ground) return {
8332
+ keys: this.alight(p, x, y, z),
8333
+ yaw: toward(p.x, p.z, x, z),
8334
+ free: false,
8335
+ exact: false
8336
+ };
8337
+ const last = this.step === this.steps.length - 1;
7945
8338
  const near = d < 2 && this.ticks - this.better > STUCK_JUMP;
7946
- if (d < (e || this.leg < 0 ? .5 : 1) || near && this.leg < this.way.edges.length) {
7947
- if (this.leg < this.way.edges.length) {
8339
+ if (d < (last ? 1 : .5) || near && !last) {
8340
+ if (!last) {
7948
8341
  this.next();
7949
8342
  return this.pilot(p);
7950
8343
  }
@@ -7986,7 +8379,7 @@ var Bot = class {
7986
8379
  };
7987
8380
  let keys = FORWARD | SPRINT;
7988
8381
  if (p.ground && hop && !(p.held & JUMP)) keys |= JUMP;
7989
- if (Math.abs(turn(this.look, yaw)) < .15 && !this.way.edges[this.leg + 1]?.leg) keys |= this.dash(w, p, yaw, d);
8382
+ if (Math.abs(turn(this.look, yaw)) < .15 && !this.steps[this.step + 1]?.leg) keys |= this.dash(w, p, yaw, d);
7990
8383
  return {
7991
8384
  keys,
7992
8385
  yaw,
@@ -8016,7 +8409,11 @@ var Bot = class {
8016
8409
  const [fx, fz] = forward(yaw);
8017
8410
  const hurts = SAFE * 400 / (2 * gravity(w));
8018
8411
  const far = Math.max(ahead, AHEAD + BRAKE * Math.sqrt(p.vx ** 2 + p.vz ** 2));
8019
- for (let t = .5; t <= far; t += .5) if (floorAt(w, p.x + fx * t, p.z + fz * t, p.y + HOP) < p.y - hurts) return false;
8412
+ for (let t = .5; t <= far; t += .5) {
8413
+ const [x, z] = [p.x + fx * t, p.z + fz * t];
8414
+ const y = p.y + HOP;
8415
+ if (Math.max(floorAt(w, x, z, y), floorAt(w, x - fz * .4, z + fx * .4, y), floorAt(w, x + fz * .4, z - fx * .4, y)) < p.y - hurts) return false;
8416
+ }
8020
8417
  const reach = Math.min(ahead, AHEAD);
8021
8418
  if (ray(w, [
8022
8419
  p.x,
@@ -8041,7 +8438,7 @@ var Bot = class {
8041
8438
  return "hop";
8042
8439
  }
8043
8440
  next() {
8044
- this.leg++;
8441
+ this.step++;
8045
8442
  this.flight = void 0;
8046
8443
  this.best = Infinity;
8047
8444
  this.better = this.ticks;
@@ -8076,7 +8473,7 @@ var Bot = class {
8076
8473
  if (leg.k === "go") {
8077
8474
  const run = go(leg.to[0], leg.to[1], leg.sprint ? .4 : .15, FORWARD | (leg.sprint ? SPRINT : 0));
8078
8475
  if (run) {
8079
- const next = this.way.edges[this.leg + 1]?.leg;
8476
+ const next = this.steps[this.step + 1]?.leg;
8080
8477
  const dash = leg.sprint && next?.k !== "leap" ? this.dash(this.world, p, run.yaw, flat(p.x, p.z, ...leg.to) - LEG_ROOM) : 0;
8081
8478
  return {
8082
8479
  ...run,
@@ -8127,19 +8524,13 @@ var Bot = class {
8127
8524
  else if (f.ran && f.ended < 0) f.ended = f.k;
8128
8525
  const n = leg.run ? f.ended < 0 ? -1 : f.k - f.ended : f.k;
8129
8526
  const extra = (n === leg.dash ? DASH : 0) | (n === leg.double ? JUMP : 0);
8130
- if (leg.land || this.steer) {
8131
- const [tx, tz] = leg.to;
8132
- const d = flat(p.x, p.z, tx, tz);
8133
- const v = (p.vx * (tx - p.x) + p.vz * (tz - p.z)) / (d || 1);
8134
- const g = gravity(this.world);
8135
- const t = Math.max((p.vy + Math.sqrt(Math.max(p.vy ** 2 + 2 * g * (p.y - leg.y), 0))) / g, .05);
8136
- return {
8137
- keys: (v * t < d - .3 ? FORWARD : v * t > d + .3 ? BACK : 0) | (leg.land ? 0 : extra),
8138
- yaw,
8139
- free: false,
8140
- exact: true
8141
- };
8142
- }
8527
+ const spent = n > Math.max(leg.dash ?? -1, leg.double ?? -1);
8528
+ if (leg.land || this.steer && spent) return {
8529
+ keys: this.alight(p, leg.to[0], leg.y, leg.to[1]) | (leg.land ? 0 : extra),
8530
+ yaw,
8531
+ free: false,
8532
+ exact: true
8533
+ };
8143
8534
  return {
8144
8535
  keys: keys | extra,
8145
8536
  yaw,
@@ -8147,14 +8538,27 @@ var Bot = class {
8147
8538
  exact: true
8148
8539
  };
8149
8540
  }
8541
+ /**
8542
+ * FORWARD or BACK in the air to come down on (x, y, z) facing it: will the flight carry it past
8543
+ * the point, or short of it, by the time it is down at its height? 0 when it will land there.
8544
+ */
8545
+ alight(p, x, y, z) {
8546
+ const d = flat(p.x, p.z, x, z);
8547
+ const v = (p.vx * (x - p.x) + p.vz * (z - p.z)) / (d || 1);
8548
+ const g = gravity(this.world);
8549
+ const t = Math.max((p.vy + Math.sqrt(Math.max(p.vy ** 2 + 2 * g * (p.y - y), 0))) / g, .05);
8550
+ return v * t < d - .3 ? FORWARD : v * t > d + .3 ? BACK : 0;
8551
+ }
8150
8552
  /** Whether the edge after this one is a jump that wants the run-up of this one. */
8151
8553
  leapNext() {
8152
- const l = this.way.edges[this.leg + 1]?.leg;
8554
+ const l = this.steps[this.step + 1]?.leg;
8153
8555
  return l?.k === "leap" && l.sprint;
8154
8556
  }
8155
8557
  /** A leg is over: on to the next on its roof, else a new way from where the bot came down. */
8156
8558
  landed(p, leg) {
8157
8559
  if (Math.abs(p.y - leg.y) > .3) {
8560
+ const e = this.steps[this.step]?.edge;
8561
+ if (e) e.fails = (e.fails ?? 0) + 1;
8158
8562
  this.goal = void 0;
8159
8563
  return {
8160
8564
  keys: 0,
@@ -8180,14 +8584,18 @@ var Bot = class {
8180
8584
  p.z
8181
8585
  ];
8182
8586
  const carrier = flags[me.team].state === FLAG$1.CARRIED ? flags[me.team].by : -1;
8587
+ const theirs = flags[1 - me.team];
8588
+ const mates = others.filter((o) => o.team === me.team && !o.state.down).filter((o) => o.state.hp <= LOW || theirs.state === FLAG$1.CARRIED && theirs.by === o.id).map((o) => o.state);
8183
8589
  let foe;
8184
8590
  let best = Infinity;
8185
8591
  for (const o of others) {
8186
8592
  const q = o.state;
8187
8593
  if (o.team === me.team || q.down) continue;
8188
8594
  const d = flat(p.x, p.z, q.x, q.z);
8189
- const score = o.id === carrier ? d - RANGE : d;
8190
- if (d < RANGE && score < best && !hidden(world, eye, [
8595
+ const onMate = mates.some((m) => flat(m.x, m.z, q.x, q.z) < NEAR);
8596
+ const score = o.id === carrier ? d - RANGE : onMate ? d - RANGE / 2 : d;
8597
+ const ahead = Math.abs(turn(p.yaw, toward(p.x, p.z, q.x, q.z))) < VIEW;
8598
+ if ((o.id === this.foe || d < HEAR || ahead) && d < RANGE && score < best && !hidden(world, eye, [
8191
8599
  q.x,
8192
8600
  q.y + 1.2,
8193
8601
  q.z
@@ -8204,22 +8612,52 @@ var Bot = class {
8204
8612
  this.since = this.ticks;
8205
8613
  const miss = this.persona.miss;
8206
8614
  this.aim = norm(p.yaw + (this.rnd() < .5 ? miss : -miss));
8615
+ this.speed = [
8616
+ 0,
8617
+ 0,
8618
+ 0
8619
+ ];
8620
+ const known = this.ticks - (this.met.get(foe.id) ?? -180) < MEMORY;
8621
+ [this.shot, this.warned, this.past] = [
8622
+ p.shots,
8623
+ known ? 0 : this.persona.warn,
8624
+ 1
8625
+ ];
8626
+ if (this.rnd() < .5) this.past = -1;
8207
8627
  }
8628
+ this.met.set(foe.id, this.ticks);
8208
8629
  const d = flat(p.x, p.z, q.x, q.z);
8209
- const lead = d / (p.rifled ? 400 : 80);
8630
+ const v = this.speed;
8631
+ const k = this.persona.track;
8632
+ [v[0], v[1], v[2]] = [
8633
+ v[0] + (q.vx - v[0]) * k,
8634
+ v[1] + (q.vy - v[1]) * k,
8635
+ v[2] + (q.vz - v[2]) * k
8636
+ ];
8637
+ const lead = this.persona.lead * d / (p.rifled ? 400 : 80);
8210
8638
  const [tx, ty, tz] = [
8211
- q.x + q.vx * lead,
8212
- q.y + 1.1 + q.vy * lead,
8213
- q.z + q.vz * lead
8639
+ q.x + v[0] * lead,
8640
+ q.y + 1.1 + v[1] * lead,
8641
+ q.z + v[2] * lead
8214
8642
  ];
8215
- const want = toward(p.x, p.z, tx, tz);
8643
+ const [ux, uz] = [(q.x - p.x) / Math.max(d, .1), (q.z - p.z) / Math.max(d, .1)];
8644
+ const across = Math.abs((q.vx - p.vx) * uz - (q.vz - p.vz) * ux);
8645
+ const shake = this.persona.shake * (1 + across / CROSS);
8646
+ this.err = this.err * .94 + (this.rnd() * 2 - 1) * shake * .35;
8647
+ this.errUp = this.errUp * .94 + (this.rnd() * 2 - 1) * shake * .2;
8648
+ const before = (s.hunters?.get(foe.id) ?? []).filter((id) => id !== this.id && id < this.id).length;
8649
+ const warning = p.shots - this.shot < this.warned;
8650
+ const off = !warning && before < TOKENS ? this.err : this.past * Math.atan2(PAST, Math.max(d, 1));
8651
+ const want = norm(toward(p.x, p.z, tx, tz) + off);
8216
8652
  const rate = this.persona.turn;
8217
8653
  this.aim = norm(this.aim + Math.max(-rate, Math.min(rate, turn(this.aim, want))));
8218
- this.pitch = Math.atan2(ty - eye[1], Math.max(d, .1));
8654
+ this.pitch = Math.atan2(ty - eye[1], Math.max(d, .1)) + this.errUp;
8219
8655
  const ready = this.ticks - this.since > this.persona.react && Math.abs(turn(this.aim, want)) < CONE;
8220
8656
  if (foe.id === carrier && d < TACKLE && p.ground && !(p.held & DASH)) return { keys: FORWARD | DASH };
8221
8657
  const armed = p.ammo > 0 && (p.mag ?? 0) > 0 && !p.wield;
8222
- return { keys: ready && (armed && d < RANGE || !armed && d < 1.4) && this.ticks & 1 ? FIRE : 0 };
8658
+ const pull = armed && d < RANGE || !armed && d < 1.4;
8659
+ if (!warning) this.hunting = foe.id;
8660
+ return { keys: ready && pull && this.ticks & 1 ? FIRE : 0 };
8223
8661
  }
8224
8662
  };
8225
8663
  // src/shared/ctf.ts
@@ -8886,6 +9324,8 @@ var Room = class {
8886
9324
  mapName;
8887
9325
  /** The map the host picked for the next match, when it is another one. */
8888
9326
  next;
9327
+ /** The next map's world, built with the bots' mesh while the end screen runs rather than in the tick that starts the match. */
9328
+ ready;
8889
9329
  /** Joins so far: each player's `since`. */
8890
9330
  joins = 0;
8891
9331
  /** Ids of bots let go this tick, not given again before the next: the person who takes a bot's place would be drawn where it stood. */
@@ -8936,7 +9376,9 @@ var Room = class {
8936
9376
  if (this.next === void 0) return;
8937
9377
  this.mapName = this.next;
8938
9378
  this.next = void 0;
8939
- this.world = build$1(mapOf(this.mapName));
9379
+ const [map, world] = this.ready ?? [];
9380
+ this.ready = void 0;
9381
+ this.world = map === this.mapName && world ? world : build$1(mapOf(this.mapName));
8940
9382
  this.ctf = new Ctf(this.world, this.rules);
8941
9383
  this.gone = this.spots();
8942
9384
  this.fresh();
@@ -8947,6 +9389,14 @@ var Room = class {
8947
9389
  for (const p of this.players()) this.ctf.join(p.info.id, p.info.team, p.state);
8948
9390
  this.roster();
8949
9391
  }
9392
+ /** The next map's world and the bots' mesh of it, built ahead (Tranquility's mesh takes some 30 ms). */
9393
+ prepare() {
9394
+ if (this.next !== void 0) {
9395
+ const world = build$1(mapOf(this.next));
9396
+ if (this.rules.bots) navOf(world);
9397
+ this.ready = [this.next, world];
9398
+ }
9399
+ }
8950
9400
  /** Number of joined players, the bots left out: a room of bots alone is empty. */
8951
9401
  get size() {
8952
9402
  return this.humans().length;
@@ -9083,8 +9533,10 @@ var Room = class {
9083
9533
  team: p.info.team,
9084
9534
  state: p.state
9085
9535
  })));
9086
- if (turn === "end") this.end(players);
9087
- else if (turn === "start") {
9536
+ if (turn === "end") {
9537
+ this.end(players);
9538
+ this.prepare();
9539
+ } else if (turn === "start") {
9088
9540
  this.load();
9089
9541
  this.restart(players);
9090
9542
  }
@@ -10106,9 +10558,14 @@ var Room = class {
10106
10558
  const others = players.map((q) => ({
10107
10559
  id: q.info.id,
10108
10560
  team: q.info.team,
10109
- state: q.state
10561
+ state: q.state,
10562
+ ...q.bot ? { bot: true } : {}
10110
10563
  }));
10564
+ const { packs = [] } = this.world.def;
10565
+ const health = packs.filter((_, k) => !this.gone[k]);
10111
10566
  const errands = this.errands(players);
10567
+ const hunters = /* @__PURE__ */ new Map();
10568
+ for (const p of players) if (p.bot && p.bot.hunting >= 0) hunters.set(p.bot.hunting, [...hunters.get(p.bot.hunting) ?? [], p.info.id]);
10112
10569
  for (const p of players) if (p.bot) {
10113
10570
  const me = others.find((o) => o.id === p.info.id);
10114
10571
  const i = p.bot.think({
@@ -10118,7 +10575,9 @@ var Room = class {
10118
10575
  world: this.world,
10119
10576
  balls: this.balls,
10120
10577
  errand: errands.get(p.info.id),
10121
- kit: this.kit(p)
10578
+ kit: this.kit(p),
10579
+ hunters,
10580
+ packs: health
10122
10581
  });
10123
10582
  p.seq = (p.seq ?? -1) + 1 & 65535;
10124
10583
  const watchers = players.filter((q) => q.watch === p.info.id);