@korial29/tiny-fps 0.18.0 → 0.20.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
@@ -25,8 +25,20 @@ var GRAB_REACH = .2;
25
25
  var GRAB_GAP = .03;
26
26
  var CLIMB_INSET = .2;
27
27
  var CLIMB_FWD_SPEED = 5.5;
28
- var RULES = { maxPlayers: 16 };
28
+ var STUMBLE_SPEED = 2.5;
29
+ var TEAM_COLOR = ["#ff3d4f", "#2f80ff"];
30
+ var RULES = {
31
+ maxPlayers: 16,
32
+ scoreLimit: 3,
33
+ timeLimit: 480
34
+ };
29
35
  // src/shared/types.ts
36
+ /** Where a team's flag is: at its base, on a carrier's back, or on the ground where it was dropped. */
37
+ var FLAG = {
38
+ HOME: 0,
39
+ CARRIED: 1,
40
+ DROPPED: 2
41
+ };
30
42
  /** Bits of Input.keys. Keys are read by KeyboardEvent.code, so WASD is ZQSD on AZERTY. */
31
43
  var KEY = {
32
44
  FORWARD: 1,
@@ -83,7 +95,9 @@ var EV = {
83
95
  WALLRUN_START: 64,
84
96
  WALLRUN_END: 128,
85
97
  SLIDE_START: 256,
86
- SLIDE_END: 512
98
+ SLIDE_END: 512,
99
+ /** Fell under the kill floor and was put back at a spawn: the host drops a carried flag. */
100
+ RESPAWN: 1024
87
101
  };
88
102
  /** Procedural textures, generated by the client. A brush names one for its top and one for its sides. */
89
103
  var TEX = {
@@ -100,6 +114,315 @@ var TEX = {
100
114
  /** Metal floor grating over darkness. */
101
115
  GRATE: 7
102
116
  };
117
+ // src/shared/world.ts
118
+ /** Metres per grid cell: about the size of the player's reach, so a query touches 1 to 4 cells. */
119
+ var CELL = 4;
120
+ /** Cell coordinates run from -OFF to OFF - 1: a 8 km square at 4 m, far more than any map. */
121
+ var OFF = 1024;
122
+ /** The key of the cell with integer coordinates (cx, cz). */
123
+ var key = (cx, cz) => (cx + OFF) * (2 * OFF) + cz + OFF;
124
+ /** Integer cell coordinate of a metre value, clamped to the keyed range. */
125
+ var at = (v, cell) => Math.min(Math.max(Math.floor(v / cell), -1024), 1023);
126
+ /** Builds the grid: each brush is listed in every cell its footprint covers. */
127
+ var build = (def, cell = CELL) => {
128
+ const grid = /* @__PURE__ */ new Map();
129
+ def.brushes.forEach(({ min, max }, i) => {
130
+ for (let cx = at(min[0], cell); cx <= at(max[0], cell); cx++) for (let cz = at(min[2], cell); cz <= at(max[2], cell); cz++) {
131
+ const k = key(cx, cz);
132
+ const list = grid.get(k);
133
+ if (list) list.push(i);
134
+ else grid.set(k, [i]);
135
+ }
136
+ });
137
+ return {
138
+ def,
139
+ cell,
140
+ grid
141
+ };
142
+ };
143
+ /**
144
+ * Indices of the brushes whose footprint may overlap the square of half side r around (x, z): every
145
+ * brush listed in a cell the square touches, once each. A candidate list, not an exact overlap.
146
+ */
147
+ var near = (w, x, z, r) => {
148
+ const out = [];
149
+ for (let cx = at(x - r, w.cell); cx <= at(x + r, w.cell); cx++) for (let cz = at(z - r, w.cell); cz <= at(z + r, w.cell); cz++) for (const i of w.grid.get(key(cx, cz)) ?? []) if (!out.includes(i)) out.push(i);
150
+ return out;
151
+ };
152
+ /**
153
+ * Distance along the ray from o in the unit direction d to the first brush face, at most max. A ray
154
+ * that starts inside a brush hits at 0; one that starts on a face and moves away from it does not hit it.
155
+ *
156
+ * Walks the grid cells the ray crosses (Amanatides and Woo), testing the brushes of each against
157
+ * the exact slab test, and stops once the next cell starts beyond the best hit or max. A brush
158
+ * spanning several cells is tested in each: the hit is the same, and lists are short.
159
+ */
160
+ var ray = (w, o, d, max) => {
161
+ const { cell, def } = w;
162
+ let best = max;
163
+ let cx = at(o[0], cell);
164
+ let cz = at(o[2], cell);
165
+ const sx = Math.sign(d[0]);
166
+ const sz = Math.sign(d[2]);
167
+ const nx = sx ? ((cx + (sx > 0 ? 1 : 0)) * cell - o[0]) / d[0] : Infinity;
168
+ const nz = sz ? ((cz + (sz > 0 ? 1 : 0)) * cell - o[2]) / d[2] : Infinity;
169
+ let tx = nx;
170
+ let tz = nz;
171
+ const dx = sx ? cell / Math.abs(d[0]) : Infinity;
172
+ const dz = sz ? cell / Math.abs(d[2]) : Infinity;
173
+ for (;;) {
174
+ for (const i of w.grid.get(key(cx, cz)) ?? []) {
175
+ const b = def.brushes[i];
176
+ let [lo, hi] = [0, best];
177
+ for (let a = 0; a < 3 && lo <= hi; a++) {
178
+ if (!d[a]) {
179
+ if (o[a] < b.min[a] || o[a] > b.max[a]) hi = -1;
180
+ continue;
181
+ }
182
+ const t0 = (b.min[a] - o[a]) / d[a];
183
+ const t1 = (b.max[a] - o[a]) / d[a];
184
+ lo = Math.max(lo, Math.min(t0, t1));
185
+ hi = Math.min(hi, Math.max(t0, t1));
186
+ }
187
+ if (lo <= hi && hi > 0 && lo < best) best = lo;
188
+ }
189
+ if (Math.min(tx, tz) >= best || Math.abs(cx) > OFF || Math.abs(cz) > OFF) return best;
190
+ if (tx < tz) {
191
+ cx += sx;
192
+ tx += dx;
193
+ } else {
194
+ cz += sz;
195
+ tz += dz;
196
+ }
197
+ }
198
+ };
199
+ // src/shared/ctf.ts
200
+ var home = () => ({
201
+ state: FLAG.HOME,
202
+ by: 0,
203
+ x: 0,
204
+ y: 0,
205
+ z: 0,
206
+ left: 0
207
+ });
208
+ /** How far under a carrier a flag that he drops in the air is looked for a floor, metres. */
209
+ var FALL = 60;
210
+ var Ctf = class {
211
+ /** Captures per team, red then blue. */
212
+ score = [0, 0];
213
+ /** The match is over and the end screen shows. */
214
+ over = false;
215
+ /** The flag of each team, red then blue. */
216
+ flags = [home(), home()];
217
+ world;
218
+ rules;
219
+ mates = /* @__PURE__ */ new Map();
220
+ /** Ticks to the end of the match, or to the next one while it is over. */
221
+ clock;
222
+ /** Ticks before each dropped flag goes home. */
223
+ back = [0, 0];
224
+ /** Who may not take each flag, and for how many more ticks: the carrier that was made to drop it. */
225
+ lock = [{
226
+ id: -1,
227
+ left: 0
228
+ }, {
229
+ id: -1,
230
+ left: 0
231
+ }];
232
+ constructor(world, rules) {
233
+ this.world = world;
234
+ this.rules = rules;
235
+ this.clock = rules.timeLimit * 60;
236
+ }
237
+ /** A player joined, standing at `at`. */
238
+ join(id, team, at) {
239
+ this.mates.set(id, {
240
+ team,
241
+ caps: 0,
242
+ returns: 0,
243
+ ground: [
244
+ at.x,
245
+ at.y,
246
+ at.z
247
+ ]
248
+ });
249
+ }
250
+ /** A player left: a flag he carried is dropped where he last stood. */
251
+ leave(id) {
252
+ const k = this.carrying(id);
253
+ const m = this.mates.get(id);
254
+ if (k >= 0 && m) this.dropAt(k, m.ground);
255
+ this.mates.delete(id);
256
+ }
257
+ /** A player was put at `at` (a new match): his last ground is there. */
258
+ place(id, at) {
259
+ const m = this.mates.get(id);
260
+ if (m) m.ground = [
261
+ at.x,
262
+ at.y,
263
+ at.z
264
+ ];
265
+ }
266
+ /**
267
+ * The player's state after one move. Under the kill floor and put back at a spawn (EV.RESPAWN),
268
+ * a carrier drops the flag where he last stood; then a floor under him is his last ground.
269
+ */
270
+ moved(p) {
271
+ const m = this.mates.get(p.id);
272
+ if (!m) return;
273
+ const k = this.carrying(p.id);
274
+ if (p.ev & EV.RESPAWN && k >= 0) this.dropAt(k, m.ground);
275
+ if (p.ground) m.ground = [
276
+ p.x,
277
+ p.y,
278
+ p.z
279
+ ];
280
+ }
281
+ /**
282
+ * Makes the player drop the flag he carries (the tackle of step 7). It lies under him: where he
283
+ * stands, or on the first floor below him, or at his last ground when there is none (over the
284
+ * void). False when he carried nothing.
285
+ */
286
+ drop(p) {
287
+ const k = this.carrying(p.id);
288
+ const m = this.mates.get(p.id);
289
+ if (k < 0 || !m || this.over) return false;
290
+ let at = m.ground;
291
+ if (p.ground) at = [
292
+ p.x,
293
+ p.y,
294
+ p.z
295
+ ];
296
+ else {
297
+ const d = ray(this.world, [
298
+ p.x,
299
+ p.y + .05,
300
+ p.z
301
+ ], [
302
+ 0,
303
+ -1,
304
+ 0
305
+ ], FALL);
306
+ if (d < FALL) at = [
307
+ p.x,
308
+ p.y + .05 - d,
309
+ p.z
310
+ ];
311
+ }
312
+ this.dropAt(k, at);
313
+ this.lock[k] = {
314
+ id: p.id,
315
+ left: 120
316
+ };
317
+ return true;
318
+ }
319
+ /** The team whose flag the player carries, or -1. */
320
+ carrying(id) {
321
+ return this.flags.findIndex((f) => f.state === FLAG.CARRIED && f.by === id);
322
+ }
323
+ /** The winning team of a match that is over, or -1 for a draw. */
324
+ winner() {
325
+ const [r, b] = this.score;
326
+ return r > b ? 0 : b > r ? 1 : -1;
327
+ }
328
+ /** What every player has done this match. */
329
+ tally() {
330
+ return [...this.mates].map(([id, m]) => ({
331
+ id,
332
+ caps: m.caps,
333
+ returns: m.returns
334
+ }));
335
+ }
336
+ /** The room is empty: the next match starts from scratch. */
337
+ reset() {
338
+ this.over = false;
339
+ this.score = [0, 0];
340
+ this.flags = [home(), home()];
341
+ this.back = [0, 0];
342
+ this.lock.forEach((l) => l.left = 0);
343
+ this.clock = this.rules.timeLimit * 60;
344
+ for (const m of this.mates.values()) m.caps = m.returns = 0;
345
+ }
346
+ /** The part of a snapshot that is the match's. */
347
+ wire() {
348
+ return {
349
+ score: [...this.score],
350
+ clock: Math.ceil(this.clock / 60),
351
+ over: this.over,
352
+ flags: this.flags.map((f) => ({ ...f }))
353
+ };
354
+ }
355
+ /**
356
+ * One host tick over the players: the dropped flags run down, flags are taken, returned and
357
+ * captured, the clock runs. 'end' when the match has just ended, 'start' when the end screen is
358
+ * done and a new match begins (the room puts everyone back at a spawn).
359
+ */
360
+ tick(players) {
361
+ if (this.over) {
362
+ if (--this.clock <= 0) {
363
+ this.reset();
364
+ return "start";
365
+ }
366
+ return;
367
+ }
368
+ for (const k of [0, 1]) {
369
+ const f = this.flags[k];
370
+ this.lock[k].left--;
371
+ if (f.state === FLAG.DROPPED) {
372
+ f.left = Math.ceil(--this.back[k] / 60);
373
+ if (this.back[k] <= 0) this.flags[k] = home();
374
+ }
375
+ }
376
+ for (const k of [0, 1]) {
377
+ const f = this.flags[k];
378
+ if (f.state === FLAG.CARRIED) continue;
379
+ const at = f.state === FLAG.HOME ? this.world.def.teams[k].base : [
380
+ f.x,
381
+ f.y,
382
+ f.z
383
+ ];
384
+ const near = players.filter((q) => touches(q.state, at));
385
+ const mate = f.state === FLAG.DROPPED ? near.find((q) => q.team === k) : void 0;
386
+ const foe = near.find((q) => q.team !== k && (q.id !== this.lock[k].id || this.lock[k].left <= 0));
387
+ if (mate) {
388
+ const m = this.mates.get(mate.id);
389
+ if (m) m.returns++;
390
+ this.flags[k] = home();
391
+ } else if (foe) this.flags[k] = {
392
+ ...home(),
393
+ state: FLAG.CARRIED,
394
+ by: foe.id
395
+ };
396
+ }
397
+ for (const p of players) {
398
+ const k = this.carrying(p.id);
399
+ if (k >= 0 && this.flags[p.team].state === FLAG.HOME && touches(p.state, this.world.def.teams[p.team].base)) {
400
+ this.score[p.team]++;
401
+ const m = this.mates.get(p.id);
402
+ if (m) m.caps++;
403
+ this.flags[k] = home();
404
+ }
405
+ }
406
+ if (this.score.some((s) => s >= this.rules.scoreLimit) || --this.clock <= 0) {
407
+ this.over = true;
408
+ this.clock = 600;
409
+ return "end";
410
+ }
411
+ }
412
+ dropAt(k, [x, y, z]) {
413
+ this.flags[k] = {
414
+ state: FLAG.DROPPED,
415
+ by: 0,
416
+ x,
417
+ y,
418
+ z,
419
+ left: 10
420
+ };
421
+ this.back[k] = 600;
422
+ }
423
+ };
424
+ /** Whether the player's body reaches the flag foot at `at`: close enough, and overlapping its height. */
425
+ var touches = (p, at) => Math.hypot(p.x - at[0], p.z - at[2]) < 1.5 && p.y < at[1] + 2.2 && p.y + 1.8 > at[1];
103
426
  // src/shared/map.ts
104
427
  /** The bottom of every building: far below the kill floor, so nothing shows beneath a roof. */
105
428
  var BASE = -30;
@@ -157,7 +480,7 @@ var stair = (z, y) => [
157
480
  * sprint jump 6.3 m, and reaches a box 1.7 m high; the hands catch a top 1.2 to 2.4 m above the
158
481
  * feet, and a fall that reaches a face within about 3.5 m is caught by them.
159
482
  *
160
- * - A: the start roof (10 m), with the spawns. B: a roof at 10 m, across a 4.2 m gap from A (a
483
+ * - A: the start roof (10 m), red's base, with its spawns. B: a roof at 10 m, across a 4.2 m gap from A (a
161
484
  * run and a jump: walking off the edge falls).
162
485
  * - On B, a platform floating at 12.2 to 12.6 m (an overhang to walk under, a bump for a jump) and
163
486
  * a stair of six 0.4 m steps up to it. 3 m north of it, C (12.4 m): the south face of C above B
@@ -167,6 +490,8 @@ var stair = (z, y) => [
167
490
  * - G (8 m) is across a 10 m drop from E, reached by a 0.5 m wide beam; it has a 1.6 m ledge.
168
491
  * - F (12.4 m) is 13 m north of C: a sprint and a jump fall short of it, and so does a walk with
169
492
  * the dash or the double jump; it takes the sprint and the jump and the dash or the double jump.
493
+ * F is blue's base, with its spawns: the flags are 90 m and the chasm apart, and the way back is
494
+ * the same line in reverse, then a drop to B and the A gap jumped south.
170
495
  */
171
496
  var ROOFTOPS = {
172
497
  name: "Rooftops",
@@ -277,56 +602,119 @@ var ROOFTOPS = {
277
602
  68
278
603
  ], TEX.STONE, TEX.BRICK)
279
604
  ],
280
- spawns: [
281
- [
282
- 6,
283
- 10,
284
- 72,
285
- 0
286
- ],
287
- [
288
- 10,
289
- 10,
290
- 72,
291
- 0
292
- ],
293
- [
605
+ teams: [{
606
+ base: [
294
607
  14,
295
608
  10,
296
- 72,
297
- 0
298
- ],
299
- [
300
- 18,
301
- 10,
302
- 72,
303
- 0
304
- ],
305
- [
306
- 22,
307
- 10,
308
- 72,
309
- 0
609
+ 62.5
310
610
  ],
311
- [
312
- 8,
313
- 10,
314
- 66,
315
- 0
316
- ],
317
- [
611
+ spawns: [
612
+ [
613
+ 6,
614
+ 10,
615
+ 72,
616
+ 0
617
+ ],
618
+ [
619
+ 10,
620
+ 10,
621
+ 72,
622
+ 0
623
+ ],
624
+ [
625
+ 14,
626
+ 10,
627
+ 72,
628
+ 0
629
+ ],
630
+ [
631
+ 18,
632
+ 10,
633
+ 72,
634
+ 0
635
+ ],
636
+ [
637
+ 22,
638
+ 10,
639
+ 72,
640
+ 0
641
+ ],
642
+ [
643
+ 8,
644
+ 10,
645
+ 66,
646
+ 0
647
+ ],
648
+ [
649
+ 14,
650
+ 10,
651
+ 66,
652
+ 0
653
+ ],
654
+ [
655
+ 20,
656
+ 10,
657
+ 66,
658
+ 0
659
+ ]
660
+ ]
661
+ }, {
662
+ base: [
318
663
  14,
319
- 10,
320
- 66,
321
- 0
664
+ 12.4,
665
+ 2.2
322
666
  ],
323
- [
324
- 20,
325
- 10,
326
- 66,
327
- 0
667
+ spawns: [
668
+ [
669
+ 6,
670
+ 12.4,
671
+ -3,
672
+ Math.PI
673
+ ],
674
+ [
675
+ 10,
676
+ 12.4,
677
+ -3,
678
+ Math.PI
679
+ ],
680
+ [
681
+ 14,
682
+ 12.4,
683
+ -3,
684
+ Math.PI
685
+ ],
686
+ [
687
+ 18,
688
+ 12.4,
689
+ -3,
690
+ Math.PI
691
+ ],
692
+ [
693
+ 22,
694
+ 12.4,
695
+ -3,
696
+ Math.PI
697
+ ],
698
+ [
699
+ 8,
700
+ 12.4,
701
+ .5,
702
+ Math.PI
703
+ ],
704
+ [
705
+ 14,
706
+ 12.4,
707
+ .5,
708
+ Math.PI
709
+ ],
710
+ [
711
+ 20,
712
+ 12.4,
713
+ .5,
714
+ Math.PI
715
+ ]
328
716
  ]
329
- ],
717
+ }],
330
718
  floor: -25,
331
719
  sun: {
332
720
  dir: [
@@ -356,41 +744,6 @@ var ROOFTOPS = {
356
744
  .008
357
745
  ]
358
746
  };
359
- // src/shared/world.ts
360
- /** Metres per grid cell: about the size of the player's reach, so a query touches 1 to 4 cells. */
361
- var CELL = 4;
362
- /** Cell coordinates run from -OFF to OFF - 1: a 8 km square at 4 m, far more than any map. */
363
- var OFF = 1024;
364
- /** The key of the cell with integer coordinates (cx, cz). */
365
- var key = (cx, cz) => (cx + OFF) * (2 * OFF) + cz + OFF;
366
- /** Integer cell coordinate of a metre value, clamped to the keyed range. */
367
- var at = (v, cell) => Math.min(Math.max(Math.floor(v / cell), -1024), 1023);
368
- /** Builds the grid: each brush is listed in every cell its footprint covers. */
369
- var build = (def, cell = CELL) => {
370
- const grid = /* @__PURE__ */ new Map();
371
- def.brushes.forEach(({ min, max }, i) => {
372
- for (let cx = at(min[0], cell); cx <= at(max[0], cell); cx++) for (let cz = at(min[2], cell); cz <= at(max[2], cell); cz++) {
373
- const k = key(cx, cz);
374
- const list = grid.get(k);
375
- if (list) list.push(i);
376
- else grid.set(k, [i]);
377
- }
378
- });
379
- return {
380
- def,
381
- cell,
382
- grid
383
- };
384
- };
385
- /**
386
- * Indices of the brushes whose footprint may overlap the square of half side r around (x, z): every
387
- * brush listed in a cell the square touches, once each. A candidate list, not an exact overlap.
388
- */
389
- var near = (w, x, z, r) => {
390
- const out = [];
391
- for (let cx = at(x - r, w.cell); cx <= at(x + r, w.cell); cx++) for (let cz = at(z - r, w.cell); cz <= at(z + r, w.cell); cz++) for (const i of w.grid.get(key(cx, cz)) ?? []) if (!out.includes(i)) out.push(i);
392
- return out;
393
- };
394
747
  // src/shared/physics.ts
395
748
  var DT = 1 / 60;
396
749
  var TAU = Math.PI * 2;
@@ -435,15 +788,16 @@ var STILL = {
435
788
  jumps: 1,
436
789
  dashes: 1,
437
790
  dash: 0,
791
+ stun: 0,
438
792
  coyote: 6,
439
793
  buffer: 0,
440
794
  held: 0,
441
795
  ev: 0,
442
796
  fall: 0
443
797
  };
444
- /** A player standing on spawn point n of the world (modulo the count). */
445
- var spawn = (id, w, n) => {
446
- const { spawns } = w.def;
798
+ /** A player standing on spawn point n of a team (modulo the count): the red team's by default. */
799
+ var spawn = (id, w, n, team = 0) => {
800
+ const { spawns } = w.def.teams[team];
447
801
  const [x, y, z, yaw] = spawns[(n % spawns.length + spawns.length) % spawns.length];
448
802
  return {
449
803
  id,
@@ -461,6 +815,8 @@ var spawn = (id, w, n) => {
461
815
  var onFloor = (m) => m === MODE.GROUND || m === MODE.SLIDE || m === MODE.CROUCH;
462
816
  /** The height of the cylinder in this mode. */
463
817
  var heightOf = (p) => p.mode === MODE.SLIDE || p.mode === MODE.CROUCH ? LOW_H : PLAYER_H;
818
+ /** Horizontal speed, m/s. */
819
+ var speedOf = (p) => len(p.vx, p.vz);
464
820
  /** Whether the brush stops a body of height h whose feet are at y when it walks sideways into it. */
465
821
  var blocks = (b, y, h) => b.max[1] > y + .5 + SKIN && b.min[1] < y + h - SKIN;
466
822
  /** The closest point of the brush footprint to (x, z), as the offset from it to (x, z). */
@@ -670,6 +1026,24 @@ var enter = (p, mode) => {
670
1026
  p.mode = mode;
671
1027
  p.t = 0;
672
1028
  };
1029
+ /**
1030
+ * A tackle lands on the player: he stumbles for STUMBLE_TICKS and is thrown STUMBLE_KNOCK m/s along
1031
+ * (dx, dz), a unit vector (the tackler's way). It ends a dash, a wall-run, a hang and a climb, and
1032
+ * clears a jump press and the coyote window. For the host to call between two steps.
1033
+ */
1034
+ var stumble = (p, dx, dz) => {
1035
+ p.stun = 48;
1036
+ p.vx = f32(dx * 6);
1037
+ p.vz = f32(dz * 6);
1038
+ p.dash = Math.min(p.dash, 50);
1039
+ p.coyote = 0;
1040
+ p.buffer = 0;
1041
+ if (p.mode === MODE.HANG || p.mode === MODE.CLIMB) p.vy = 0;
1042
+ if (p.mode === MODE.HANG || p.mode === MODE.CLIMB || p.mode === MODE.WALLRUN) {
1043
+ enter(p, MODE.AIR);
1044
+ p.edge = 0;
1045
+ }
1046
+ };
673
1047
  /** Scales the horizontal speed to at most max. */
674
1048
  var cap = (p, max) => {
675
1049
  const s = len(p.vx, p.vz);
@@ -956,7 +1330,7 @@ var onGround = (p, w, c, burst) => {
956
1330
  const slide = speed >= SLIDE_MIN_SPEED;
957
1331
  enter(p, slide ? MODE.SLIDE : MODE.CROUCH);
958
1332
  p.ev |= slide ? EV.SLIDE_START : 0;
959
- } else if (p.mode === MODE.SLIDE && (p.t >= 60 || speed < 4.5)) {
1333
+ } else if (p.mode === MODE.SLIDE && (p.t >= 60 || speed < 4.5 || c.stumble)) {
960
1334
  p.ev |= EV.SLIDE_END;
961
1335
  enter(p, !low && canStand(p, w) ? MODE.GROUND : MODE.CROUCH);
962
1336
  } else if (p.mode === MODE.SLIDE && !low && canStand(p, w)) {
@@ -964,7 +1338,13 @@ var onGround = (p, w, c, burst) => {
964
1338
  enter(p, MODE.GROUND);
965
1339
  } else if (p.mode === MODE.CROUCH && !low && canStand(p, w)) enter(p, MODE.GROUND);
966
1340
  if (burst) return;
967
- if (p.mode === MODE.SLIDE) {
1341
+ if (c.stumble) {
1342
+ if (speed > 2.5) {
1343
+ const k = Math.max(STUMBLE_SPEED, speed - 12 * DT) / speed;
1344
+ p.vx *= k;
1345
+ p.vz *= k;
1346
+ } else steer(p, unwalled(p, w, c, heightOf(p)), STUMBLE_SPEED);
1347
+ } else if (p.mode === MODE.SLIDE) {
968
1348
  const k = Math.max(0, speed - 4 * DT) / speed;
969
1349
  p.vx *= k;
970
1350
  p.vz *= k;
@@ -993,9 +1373,9 @@ var air = (p, w, c, burst) => {
993
1373
  /**
994
1374
  * Applies one input for 1 / TICK_HZ s, in place: yaw and pitch from the input (pitch clamped to
995
1375
  * MAX_PITCH), then the step of the current mode (see the top of the file). Feet under the world's
996
- * kill floor put the player back at a spawn.
1376
+ * kill floor put the player back at a spawn of his `team` (EV.RESPAWN).
997
1377
  */
998
- var move = (p, i, w) => {
1378
+ var move = (p, i, w, team = 0) => {
999
1379
  p.yaw = norm(i.yaw);
1000
1380
  p.pitch = Math.max(-MAX_PITCH, Math.min(MAX_PITCH, i.pitch));
1001
1381
  const f = (i.keys & KEY.FORWARD ? 1 : 0) - (i.keys & KEY.BACK ? 1 : 0);
@@ -1003,10 +1383,12 @@ var move = (p, i, w) => {
1003
1383
  const n = len(f, s) || 1;
1004
1384
  const [fx, fz] = forward(p.yaw);
1005
1385
  const pressed = (bit) => (i.keys & bit) !== 0 && (p.held & bit) === 0;
1386
+ const stumbling = p.stun > 0;
1006
1387
  const c = {
1007
1388
  keys: i.keys,
1008
- jump: pressed(KEY.JUMP),
1009
- dash: pressed(KEY.DASH),
1389
+ stumble: stumbling,
1390
+ jump: !stumbling && pressed(KEY.JUMP),
1391
+ dash: !stumbling && pressed(KEY.DASH),
1010
1392
  fwd: f > 0,
1011
1393
  wx: (fx * f - fz * s) / n,
1012
1394
  wz: (fz * f + fx * s) / n,
@@ -1017,6 +1399,7 @@ var move = (p, i, w) => {
1017
1399
  p.ev = 0;
1018
1400
  p.fall = 0;
1019
1401
  if (c.jump) p.buffer = 7;
1402
+ if (stumbling) p.stun--;
1020
1403
  if (p.dash > 0) {
1021
1404
  p.dash--;
1022
1405
  if (p.dash === 50) cap(p, 10);
@@ -1026,9 +1409,10 @@ var move = (p, i, w) => {
1026
1409
  else if (p.mode !== MODE.HANG) free(p, w, c);
1027
1410
  if (p.y < w.def.floor) {
1028
1411
  const { yaw, pitch, id } = p;
1029
- Object.assign(p, spawn(id, w, id), {
1412
+ Object.assign(p, spawn(id, w, id, team), {
1030
1413
  yaw,
1031
- pitch
1414
+ pitch,
1415
+ ev: p.ev | EV.RESPAWN
1032
1416
  });
1033
1417
  }
1034
1418
  p.t = p.mode === before ? Math.min(255, p.t + 1) : p.t;
@@ -1044,6 +1428,8 @@ var move = (p, i, w) => {
1044
1428
  p.vz = f32(p.vz);
1045
1429
  p.edge = f32(p.edge);
1046
1430
  };
1431
+ /** A flag's resting place on the wire: 1/16 m in an i16, so ±2 km. */
1432
+ var POS = 16;
1047
1433
  var TURN = 65536 / (2 * Math.PI);
1048
1434
  /** Radians to 65536ths of a turn; the mask also folds negative and ≥ 2π angles into one turn. */
1049
1435
  var toTurn = (yaw) => Math.round(yaw * TURN) & 65535;
@@ -1065,13 +1451,16 @@ var decodeInput = (buf) => {
1065
1451
  view: (v.getUint16(8) * 256 + v.getUint8(10)) / 256
1066
1452
  };
1067
1453
  };
1454
+ /** Bytes of a flag on the wire, by its state. */
1455
+ var flagBytes = (state) => state === FLAG.CARRIED ? 2 : state === FLAG.DROPPED ? 8 : 1;
1068
1456
  var encodeSnapshot = (s) => {
1069
- const v = /* @__PURE__ */ new DataView(/* @__PURE__ */ new ArrayBuffer(4 + 39 * s.players.length));
1457
+ const end = 4 + 40 * s.players.length;
1458
+ const v = new DataView(new ArrayBuffer(end + 4 + s.flags.reduce((n, f) => n + flagBytes(f.state), 0)));
1070
1459
  v.setUint8(0, 2);
1071
1460
  v.setUint16(1, s.tick & 65535);
1072
1461
  v.setUint8(3, s.players.length);
1073
1462
  s.players.forEach((p, i) => {
1074
- const o = 4 + i * 39;
1463
+ const o = 4 + i * 40;
1075
1464
  v.setUint8(o, p.id);
1076
1465
  v.setFloat32(o + 1, p.x);
1077
1466
  v.setFloat32(o + 5, p.y);
@@ -1085,9 +1474,25 @@ var encodeSnapshot = (s) => {
1085
1474
  v.setUint16(o + 33, p.ack & 65535);
1086
1475
  v.setUint8(o + 35, p.mode & 7 | (p.wall & 7) << 3 | (p.jumps & 1) << 6 | p.dashes << 7);
1087
1476
  v.setUint8(o + 36, p.t);
1088
- v.setUint8(o + 37, p.dash);
1477
+ v.setUint8(o + 37, p.dash | (p.by >> 2 & 3) << 6);
1089
1478
  v.setUint8(o + 38, p.coyote & 7 | (p.buffer & 7) << 3 | (p.held & KEY.JUMP ? 64 : 0) | (p.held & KEY.DASH ? 128 : 0));
1479
+ v.setUint8(o + 39, p.stun | (p.by & 3) << 6);
1090
1480
  });
1481
+ v.setUint8(end, s.score[0]);
1482
+ v.setUint8(end + 1, s.score[1]);
1483
+ v.setUint16(end + 2, s.clock & 32767 | (s.over ? 32768 : 0));
1484
+ let o = end + 4;
1485
+ for (const f of s.flags) {
1486
+ v.setUint8(o, f.state);
1487
+ if (f.state === FLAG.CARRIED) v.setUint8(o + 1, f.by);
1488
+ else if (f.state === FLAG.DROPPED) {
1489
+ v.setInt16(o + 1, clamp16(f.x * POS));
1490
+ v.setInt16(o + 3, clamp16(f.y * POS));
1491
+ v.setInt16(o + 5, clamp16(f.z * POS));
1492
+ v.setUint8(o + 7, Math.min(255, f.left));
1493
+ }
1494
+ o += flagBytes(f.state);
1495
+ }
1091
1496
  return v.buffer;
1092
1497
  };
1093
1498
  /** Returns null on anything that is not a known text message. */
@@ -1099,29 +1504,19 @@ var parseText = (raw) => {
1099
1504
  return null;
1100
1505
  }
1101
1506
  };
1507
+ // src/shared/tackle.ts
1508
+ /** Whether the player's body is a weapon: in a slide, or in the burst of a dash. */
1509
+ var tackling = (p) => p.mode === MODE.SLIDE || p.dash > 50;
1510
+ /**
1511
+ * Whether the cylinders of `a` and `b` overlap, with TACKLE_REACH of reach on top: closer than
1512
+ * two radii and the reach on the floor plane, and their heights (low in a slide) overlap.
1513
+ */
1514
+ var reaches = (a, b) => Math.hypot(a.x - b.x, a.z - b.z) < 1.1 && a.y < b.y + heightOf(b) && b.y < a.y + heightOf(a);
1102
1515
  // src/shared/room.ts
1103
1516
  /** Ticks run at most this many times per timer callback, so a stalled host does not spiral. */
1104
1517
  var MAX_CATCH_UP = 20;
1105
1518
  /** A new player does not spawn within this many metres of another one. */
1106
1519
  var SPAWN_CLEAR = 2;
1107
- var COLORS = [
1108
- "#e6194b",
1109
- "#3cb44b",
1110
- "#ffe119",
1111
- "#4363d8",
1112
- "#f58231",
1113
- "#911eb4",
1114
- "#42d4f4",
1115
- "#f032e6",
1116
- "#bfef45",
1117
- "#fabed4",
1118
- "#469990",
1119
- "#dcbeff",
1120
- "#9a6324",
1121
- "#800000",
1122
- "#aaffc3",
1123
- "#808000"
1124
- ];
1125
1520
  /** First n characters (code points) of s, trimmed. */
1126
1521
  var cut = (s, n) => Array.from(s).slice(0, n).join("").trim();
1127
1522
  var cleanName = (raw) => cut((typeof raw === "string" ? raw : "").replace(/\s+/g, " ").replace(/\p{Cc}/gu, "").trim(), 16);
@@ -1139,13 +1534,18 @@ var setEvery = (cb, ms) => {
1139
1534
  * One game, transport-agnostic. The Node server and the local host tab both drive a Room: they
1140
1535
  * feed it connections and frames, the room answers through Conn.send.
1141
1536
  *
1142
- * - A peer joins with { t: 'join', name }: unique name (`Bob 2`), unique colour, a spawn point not
1143
- * taken by another player; 'full' beyond rules.maxPlayers. Everyone gets a fresh 'roster' on
1144
- * every join and leave.
1537
+ * - A peer joins with { t: 'join', name }: unique name (`Bob 2`), the smaller team (red on a tie)
1538
+ * and its colour, a spawn point of the team not taken by another player; 'full' beyond
1539
+ * rules.maxPlayers. Everyone gets a fresh 'roster' on every join and leave. Teams are not
1540
+ * rebalanced when someone leaves: the next join fills the smaller one.
1145
1541
  * - Each input frame moves its player at once with move(); the INPUT_BURST bank drops the excess.
1146
1542
  * rewind() answers where the others were drawn at the view tick of an input (lag compensation,
1147
1543
  * at most MAX_REWIND ticks back).
1148
- * - Every SNAP_EVERY ticks of TICK_HZ, one snapshot with every player goes to every peer.
1544
+ * - Every SNAP_EVERY ticks of TICK_HZ, one snapshot with every player, the score, the clock and
1545
+ * the two flags goes to every peer. The flag rules and the match are ctf.ts's: a match ends at
1546
+ * rules.scoreLimit or rules.timeLimit ('end' text to everyone), the end screen runs END_SCREEN
1547
+ * seconds, then everyone is put back at a spawn of his team and the score starts again.
1548
+ * drop(id) makes a carrier drop the flag: the hook of the tackle (step 7).
1149
1549
  */
1150
1550
  var Room = class {
1151
1551
  /** Every connected peer; null until it joins. */
@@ -1154,10 +1554,11 @@ var Room = class {
1154
1554
  /** The players as each snapshot of the last MAX_REWIND ticks sent them. */
1155
1555
  history = [];
1156
1556
  world;
1557
+ ctf;
1157
1558
  halt;
1158
1559
  rules;
1159
- /** Round-robin cursor over the spawn points, used once they are all taken. */
1160
- rr = 0;
1560
+ /** Round-robin cursor over the spawn points of each team, used once they are all taken. */
1561
+ rr = [0, 0];
1161
1562
  /** Missing rules fall back to RULES. */
1162
1563
  constructor(rules = {}, map = ROOFTOPS) {
1163
1564
  this.rules = {
@@ -1165,6 +1566,7 @@ var Room = class {
1165
1566
  ...rules
1166
1567
  };
1167
1568
  this.world = build(map);
1569
+ this.ctf = new Ctf(this.world, this.rules);
1168
1570
  }
1169
1571
  /** Number of joined players. */
1170
1572
  get size() {
@@ -1185,7 +1587,9 @@ var Room = class {
1185
1587
  const input = p && decodeInput(data);
1186
1588
  if (input && p.bank > 0) {
1187
1589
  p.bank--;
1188
- move(p.state, input, this.world);
1590
+ move(p.state, input, this.world, p.info.team);
1591
+ this.ctf.moved(p.state);
1592
+ this.tackle(p, input.view);
1189
1593
  p.ack = input.seq;
1190
1594
  }
1191
1595
  }
@@ -1193,7 +1597,19 @@ var Room = class {
1193
1597
  disconnect(conn) {
1194
1598
  const p = this.peers.get(conn);
1195
1599
  this.peers.delete(conn);
1196
- if (p) this.roster();
1600
+ if (p) {
1601
+ this.ctf.leave(p.info.id);
1602
+ if (!this.size) this.ctf.reset();
1603
+ this.roster();
1604
+ }
1605
+ }
1606
+ /**
1607
+ * Makes the player `id` drop the flag he carries, where he stands (the tackle of step 7).
1608
+ * False when he carries none or is not there.
1609
+ */
1610
+ drop(id) {
1611
+ const p = this.players().find((q) => q.info.id === id);
1612
+ return !!p && this.ctf.drop(p.state);
1197
1613
  }
1198
1614
  /** One host tick. Public so tests can drive the room without a timer. */
1199
1615
  tick() {
@@ -1201,6 +1617,13 @@ var Room = class {
1201
1617
  if (!players.length) return;
1202
1618
  const now = ++this.ticks;
1203
1619
  for (const p of players) p.bank = Math.min(10, p.bank + 1);
1620
+ const turn = this.ctf.tick(players.map((p) => ({
1621
+ id: p.info.id,
1622
+ team: p.info.team,
1623
+ state: p.state
1624
+ })));
1625
+ if (turn === "end") this.end(players);
1626
+ else if (turn === "start") this.restart(players);
1204
1627
  if (now % 2 === 0) {
1205
1628
  this.history.push({
1206
1629
  tick: now,
@@ -1211,8 +1634,10 @@ var Room = class {
1211
1634
  tick: now,
1212
1635
  players: players.map((p) => ({
1213
1636
  ...p.state,
1214
- ack: p.ack
1215
- }))
1637
+ ack: p.ack,
1638
+ by: p.state.stun ? p.by : 0
1639
+ })),
1640
+ ...this.ctf.wire()
1216
1641
  });
1217
1642
  players.forEach((p) => p.conn.send(snap));
1218
1643
  }
@@ -1272,13 +1697,59 @@ var Room = class {
1272
1697
  } : pb ?? pa ?? p;
1273
1698
  });
1274
1699
  }
1275
- /** The first spawn point with none of `others` within SPAWN_CLEAR, else the next one round-robin. */
1276
- place(id, others) {
1277
- const n = this.world.def.spawns.findIndex((_, k) => {
1278
- const { x, z } = spawn(id, this.world, k);
1700
+ /**
1701
+ * After a move of `p` at the view tick `view`: a dash or a slide that overlaps an enemy carrier
1702
+ * tackles him. The carrier is where `p` drew him (lag compensation), so a tackle that hit on
1703
+ * the tackler's screen counts; the carrier drops the flag and stumbles, thrown the way the
1704
+ * tackler goes. Once per TACKLE_COOLDOWN ticks, for the first carrier hit.
1705
+ */
1706
+ tackle(p, view) {
1707
+ const me = p.state;
1708
+ if (!tackling(me) || this.ticks - p.tackled < 30) return;
1709
+ const foes = this.players().filter((q) => q.info.team !== p.info.team);
1710
+ const hit = this.rewind(p.info.id, view).find((seen) => reaches(me, seen) && foes.some((q) => q.info.id === seen.id && this.ctf.carrying(q.info.id) >= 0));
1711
+ const foe = foes.find((q) => q.info.id === hit?.id);
1712
+ if (!foe || !this.drop(foe.info.id)) return;
1713
+ const v = speedOf(me);
1714
+ const [dx, dz] = v > .001 ? [me.vx / v, me.vz / v] : forward(me.yaw);
1715
+ stumble(foe.state, dx, dz);
1716
+ foe.by = p.info.id;
1717
+ p.tackled = this.ticks;
1718
+ }
1719
+ /** The first spawn point of the team with none of `others` within SPAWN_CLEAR, else the next one round-robin. */
1720
+ place(id, team, others) {
1721
+ const n = this.world.def.teams[team].spawns.findIndex((_, k) => {
1722
+ const { x, z } = spawn(id, this.world, k, team);
1279
1723
  return !others.some((o) => Math.hypot(o.x - x, o.z - z) < SPAWN_CLEAR);
1280
1724
  });
1281
- return spawn(id, this.world, n < 0 ? this.rr++ : n);
1725
+ return spawn(id, this.world, n < 0 ? this.rr[team]++ : n, team);
1726
+ }
1727
+ /** The match is over: everyone gets the result and what each player did. */
1728
+ end(players) {
1729
+ const tally = this.ctf.tally().flatMap((t) => {
1730
+ const info = players.find((p) => p.info.id === t.id)?.info;
1731
+ return info ? [{
1732
+ ...t,
1733
+ name: info.name,
1734
+ team: info.team
1735
+ }] : [];
1736
+ });
1737
+ const msg = JSON.stringify({
1738
+ t: "end",
1739
+ score: this.ctf.score,
1740
+ players: tally
1741
+ });
1742
+ players.forEach((p) => p.conn.send(msg));
1743
+ }
1744
+ /** A new match: everyone back at a spawn of his team. */
1745
+ restart(players) {
1746
+ const put = [];
1747
+ this.rr = [0, 0];
1748
+ for (const p of players) {
1749
+ p.state = this.place(p.info.id, p.info.team, put);
1750
+ put.push(p.state);
1751
+ this.ctf.place(p.info.id, p.state);
1752
+ }
1282
1753
  }
1283
1754
  players() {
1284
1755
  return [...this.peers.values()].filter((p) => p !== null);
@@ -1291,19 +1762,24 @@ var Room = class {
1291
1762
  }
1292
1763
  let id = 0;
1293
1764
  while (players.some((p) => p.info.id === id)) id++;
1294
- const color = COLORS.find((c) => !players.some((p) => p.info.color === c)) ?? COLORS[id % COLORS.length];
1765
+ const reds = players.filter((p) => p.info.team === 0).length;
1766
+ const team = reds > players.length - reds ? 1 : 0;
1295
1767
  const player = {
1296
1768
  conn,
1297
1769
  info: {
1298
1770
  id,
1299
1771
  name: unique(cleanName(name) || `Player ${id}`, players.map((p) => p.info.name)),
1300
- color
1772
+ team,
1773
+ color: TEAM_COLOR[team]
1301
1774
  },
1302
- state: this.place(id, players.map((p) => p.state)),
1775
+ state: this.place(id, team, players.map((p) => p.state)),
1303
1776
  bank: 10,
1304
- ack: 65535
1777
+ ack: 65535,
1778
+ by: 0,
1779
+ tackled: -30
1305
1780
  };
1306
1781
  this.peers.set(conn, player);
1782
+ this.ctf.join(id, team, player.state);
1307
1783
  this.roster();
1308
1784
  }
1309
1785
  /** Each joined peer gets the full roster with its own id. */