@korial29/tiny-fps 0.13.0 → 0.14.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
@@ -5,6 +5,22 @@ var PLAYER_R = .4;
5
5
  var PLAYER_H = 1.8;
6
6
  /** Pitch is clamped to ±MAX_PITCH radians, just short of straight up or down. */
7
7
  var MAX_PITCH = 1.55;
8
+ /** Height of the cylinder when sliding or crouching, and the eye in it. */
9
+ var LOW_H = .9;
10
+ /** Speed from which a crouch is a slide (above RUN_SPEED: only a sprint slides). */
11
+ var SLIDE_MIN_SPEED = 7.5;
12
+ /** Speed lost along the wall, m/s². */
13
+ var WALLRUN_DRAG = 2.5;
14
+ /** Gravity during the run, as a share of GRAVITY, and the fastest rise it starts with. */
15
+ var WALLRUN_GRAVITY = .15;
16
+ /** How far from the cylinder a wall still counts as next to it (m). */
17
+ var WALL_REACH = .15;
18
+ var WALL_KICK_UP = 6.5;
19
+ /** A hand's reach beyond the cylinder (m), and the gap left between the hanging body and the wall. */
20
+ var GRAB_REACH = .2;
21
+ var GRAB_GAP = .03;
22
+ var CLIMB_INSET = .2;
23
+ var CLIMB_FWD_SPEED = 5.5;
8
24
  var RULES = { maxPlayers: 16 };
9
25
  // src/shared/types.ts
10
26
  /** Bits of Input.keys. Keys are read by KeyboardEvent.code, so WASD is ZQSD on AZERTY. */
@@ -13,7 +29,57 @@ var KEY = {
13
29
  BACK: 2,
14
30
  LEFT: 4,
15
31
  RIGHT: 8,
16
- JUMP: 16
32
+ JUMP: 16,
33
+ /** Hold to sprint (with forward). */
34
+ SPRINT: 32,
35
+ /** Press to dash. */
36
+ DASH: 64,
37
+ /** Hold to crouch, or to slide when fast. */
38
+ CROUCH: 128
39
+ };
40
+ /** The movement state of a player: what the body is doing, one of these. */
41
+ var MODE = {
42
+ /** Standing or running on a floor. */
43
+ GROUND: 0,
44
+ /** Off the floor: a jump, a fall, a dash. */
45
+ AIR: 1,
46
+ /** Running along a wall. */
47
+ WALLRUN: 2,
48
+ /** Hanging from a ledge. */
49
+ HANG: 3,
50
+ /** Pulling up onto a ledge. */
51
+ CLIMB: 4,
52
+ /** Sliding on the ground, low. */
53
+ SLIDE: 5,
54
+ /** Walking low. */
55
+ CROUCH: 6
56
+ };
57
+ /**
58
+ * Which side of the player a wall or a ledge is on (PlayerState.wall). In the air it is the side
59
+ * of the last wall-run or hang, which does not catch the player again until he lands.
60
+ */
61
+ var SIDE = {
62
+ NONE: 0,
63
+ EAST: 1,
64
+ WEST: 2,
65
+ SOUTH: 3,
66
+ NORTH: 4
67
+ };
68
+ /** Bits of PlayerState.ev: what happened in the last step. */
69
+ var EV = {
70
+ JUMP: 1,
71
+ /** The extra jump in the air. */
72
+ DOUBLE: 2,
73
+ DASH: 4,
74
+ /** Back on a floor: PlayerState.fall is the speed it came down at. */
75
+ LAND: 8,
76
+ GRAB: 16,
77
+ /** The climb ended standing on the top. */
78
+ CLIMB: 32,
79
+ WALLRUN_START: 64,
80
+ WALLRUN_END: 128,
81
+ SLIDE_START: 256,
82
+ SLIDE_END: 512
17
83
  };
18
84
  /** Procedural textures, generated by the client. A brush names one for its top and one for its sides. */
19
85
  var TEX = {
@@ -82,26 +148,29 @@ var stair = (z, y) => [
82
148
  z - k + 1
83
149
  ], TEX.STONE, TEX.STONE));
84
150
  /**
85
- * A parkour test course, laid out for the moves of step 3 and walkable now with run, jump and the
86
- * 0.5 m step-up. Metres, y up, the course runs north (toward -z). Jumps reach about 4 m on the
87
- * flat, and a box 1.7 m high.
151
+ * A parkour test course, laid out for the moves of step 3 and flown with them by test/lines.ts.
152
+ * Metres, y up, the course runs north (toward -z). A walking jump flies about 4.2 m on the flat, a
153
+ * 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
154
+ * feet, and a fall that reaches a face within about 3.5 m is caught by them.
88
155
  *
89
- * - A: the start roof (10 m), with the spawns. B: a roof at 10 m, across a 2.5 m gap from A.
156
+ * - A: the start roof (10 m), with the spawns. B: a roof at 10 m, across a 4.2 m gap from A (a
157
+ * run and a jump: walking off the edge falls).
90
158
  * - On B, a platform floating at 12.2 to 12.6 m (an overhang to walk under, a bump for a jump) and
91
159
  * 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
92
160
  * is a 2.4 m ledge to grab. Two poles stand by the platform.
93
161
  * - E (8 m), 3 m east of C and 4.4 m lower: a long wall (6.5 m high, 26 m long) to run along, ledges
94
162
  * 2.0 and 2.2 m high, and a 1.2 m low gap under a lintel to slide under, with a way round it.
95
163
  * - 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.
96
- * - F (12.4 m) is 8 m north of C: out of reach until the dash and the double jump.
164
+ * - 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
165
+ * the dash or the double jump; it takes the sprint and the jump and the dash or the double jump.
97
166
  */
98
167
  var ROOFTOPS = {
99
168
  name: "Rooftops",
100
169
  brushes: [
101
- roof(4, 56, 24, 76, 10),
170
+ roof(4, 57.7, 24, 76, 10),
102
171
  roof(4, 36, 24, 53.5, 10),
103
172
  roof(4, 20, 24, 36, 12.4),
104
- roof(4, 0, 24, 12, 12.4),
173
+ roof(4, -5, 24, 7, 12.4),
105
174
  roof(27, 20, 47, 50, 8),
106
175
  roof(27, 60, 47, 76, 8),
107
176
  box([
@@ -329,12 +398,45 @@ var EPS = 1e-6;
329
398
  * lands on it.
330
399
  */
331
400
  var SKIN = .01;
401
+ /**
402
+ * A circle this close to a box (1 mm) is touching it: a push leaves it exactly PLAYER_R away, and
403
+ * the float32 position it is rounded to is a few micrometres off.
404
+ */
405
+ var TOUCH = .001;
332
406
  /** An angle in [0, 2π). */
333
407
  var norm = (a) => (a % TAU + TAU) % TAU;
334
408
  /** Unit vector the player faces at yaw, on the floor plane: yaw 0 looks toward -z. */
335
409
  var forward = (yaw) => [-Math.sin(yaw), -Math.cos(yaw)];
336
410
  var f32 = Math.fround;
411
+ var sqrt = Math.sqrt;
337
412
  var clamp = (v, lo, hi) => Math.min(Math.max(v, lo), hi);
413
+ /** The length of (x, z). */
414
+ var len = (x, z) => sqrt(x * x + z * z);
415
+ /** The unit vector from a player toward a wall or a ledge on each SIDE. */
416
+ var TOWARD = [
417
+ [0, 0],
418
+ [1, 0],
419
+ [-1, 0],
420
+ [0, 1],
421
+ [0, -1]
422
+ ];
423
+ /** The movement fields of a player at rest on a floor, for a state built by hand. */
424
+ var STILL = {
425
+ vx: 0,
426
+ vz: 0,
427
+ mode: MODE.GROUND,
428
+ t: 0,
429
+ wall: SIDE.NONE,
430
+ edge: 0,
431
+ jumps: 1,
432
+ dashes: 1,
433
+ dash: 0,
434
+ coyote: 6,
435
+ buffer: 0,
436
+ held: 0,
437
+ ev: 0,
438
+ fall: 0
439
+ };
338
440
  /** A player standing on spawn point n of the world (modulo the count). */
339
441
  var spawn = (id, w, n) => {
340
442
  const { spawns } = w.def;
@@ -347,11 +449,16 @@ var spawn = (id, w, n) => {
347
449
  vy: 0,
348
450
  yaw: norm(yaw),
349
451
  pitch: 0,
350
- ground: true
452
+ ground: true,
453
+ ...STILL
351
454
  };
352
455
  };
353
- /** Whether the brush stops a body whose feet are at y when it walks sideways into it. */
354
- var blocks = (b, y) => b.max[1] > y + .5 + SKIN && b.min[1] < y + 1.8 - SKIN;
456
+ /** The modes with the feet on a floor: ground, slide and crouch. */
457
+ var onFloor = (m) => m === MODE.GROUND || m === MODE.SLIDE || m === MODE.CROUCH;
458
+ /** The height of the cylinder in this mode. */
459
+ var heightOf = (p) => p.mode === MODE.SLIDE || p.mode === MODE.CROUCH ? LOW_H : PLAYER_H;
460
+ /** Whether the brush stops a body of height h whose feet are at y when it walks sideways into it. */
461
+ var blocks = (b, y, h) => b.max[1] > y + .5 + SKIN && b.min[1] < y + h - SKIN;
355
462
  /** The closest point of the brush footprint to (x, z), as the offset from it to (x, z). */
356
463
  var gap = (b, x, z) => [x - clamp(x, b.min[0], b.max[0]), z - clamp(z, b.min[2], b.max[2])];
357
464
  /**
@@ -362,29 +469,30 @@ var gap = (b, x, z) => [x - clamp(x, b.min[0], b.max[0]), z - clamp(z, b.min[2],
362
469
  * matters on a flat wall made of several boxes: the corner of the next box is always shallower
363
470
  * than the normal push of the box the player is on, so it never pulls him back along the wall.
364
471
  * A centre inside a footprint (it never gets there by walking, see below) leaves by the nearest
365
- * side.
472
+ * side. Returns the whole push, for the caller to take the speed into the wall away.
366
473
  *
367
- * Tunnelling is impossible: a step is RUN_SPEED / TICK_HZ = 0.1 m, and each push leaves the centre
368
- * at least PLAYER_R = 0.4 m from the footprint of every box it started beside, so a 0.1 m step
369
- * cannot bring the centre across one, however thin. The candidates come from near() with twice
370
- * PLAYER_R, enough for the one push of at most PLAYER_R that matters.
474
+ * Tunnelling is impossible: a step is at most DASH_SPEED / TICK_HZ = 0.27 m, and each push leaves
475
+ * the centre at least PLAYER_R = 0.4 m from the footprint of every box it started beside, so a
476
+ * step of that size cannot bring the centre across one, however thin. The candidates come from
477
+ * near() with twice PLAYER_R, enough for the one push of at most PLAYER_R that matters.
371
478
  */
372
- var push = (p, w) => {
479
+ var push = (p, w, h) => {
373
480
  const { brushes } = w.def;
374
481
  const ids = near(w, p.x, p.z, 2 * PLAYER_R);
375
482
  const reach = (PLAYER_R - EPS) ** 2;
483
+ let [tx, tz] = [0, 0];
376
484
  for (let pass = 0; pass < 4; pass++) {
377
485
  let best = 0;
378
486
  let px = 0;
379
487
  let pz = 0;
380
488
  for (const i of ids) {
381
489
  const b = brushes[i];
382
- if (!blocks(b, p.y)) continue;
490
+ if (!blocks(b, p.y, h)) continue;
383
491
  const [dx, dz] = gap(b, p.x, p.z);
384
492
  const d2 = dx * dx + dz * dz;
385
493
  if (d2 >= reach) continue;
386
494
  if (d2) {
387
- const d = Math.sqrt(d2);
495
+ const d = sqrt(d2);
388
496
  if (.4 - d > best) {
389
497
  best = PLAYER_R - d;
390
498
  px = dx / d * best;
@@ -419,20 +527,23 @@ var push = (p, w) => {
419
527
  [px, pz] = [nx * best, nz * best];
420
528
  }
421
529
  }
422
- if (!best) return;
530
+ if (!best) break;
423
531
  p.x += px;
424
532
  p.z += pz;
533
+ tx += px;
534
+ tz += pz;
425
535
  }
536
+ return [tx, tz];
426
537
  };
427
538
  /**
428
539
  * The floor and the ceiling under the player: the highest top the player can stand on (at most
429
540
  * STEP_H above the feet) and the lowest bottom above the head, among the boxes the circle overlaps.
430
541
  * Every top between the feet and the fall is a candidate and so is every bottom above the head,
431
542
  * however far: a fall of any speed stops on the first top under the circle, and a rise (at most
432
- * JUMP_SPEED / TICK_HZ = 0.12 m a step) cannot pass a bottom. Only a corner clipped by under a
543
+ * DOUBLE_JUMP_SPEED / TICK_HZ = 0.12 m a step) cannot pass a bottom. Only a corner clipped by under a
433
544
  * millimetre of overlap at one end of a step and none at the other could slip past.
434
545
  */
435
- var span = (p, w) => {
546
+ var span = (p, w, h) => {
436
547
  const { brushes } = w.def;
437
548
  const reach = (PLAYER_R - EPS) ** 2;
438
549
  let floor = -Infinity;
@@ -442,66 +553,470 @@ var span = (p, w) => {
442
553
  const [dx, dz] = gap(b, p.x, p.z);
443
554
  if (dx * dx + dz * dz >= reach) continue;
444
555
  if (b.max[1] <= p.y + .5 + SKIN) floor = Math.max(floor, b.max[1]);
445
- else if (b.min[1] >= p.y + 1.8 - SKIN) ceil = Math.min(ceil, b.min[1]);
556
+ else if (b.min[1] >= p.y + h - SKIN) ceil = Math.min(ceil, b.min[1]);
446
557
  }
447
558
  return [floor, ceil];
448
559
  };
560
+ /** Whether a standing body fits where the player is: no ceiling or box within PLAYER_H. */
561
+ var canStand = (p, w) => {
562
+ const reach = (PLAYER_R - EPS) ** 2;
563
+ return near(w, p.x, p.z, PLAYER_R).every((i) => {
564
+ const b = w.def.brushes[i];
565
+ const [dx, dz] = gap(b, p.x, p.z);
566
+ return dx * dx + dz * dz >= reach || !blocks(b, p.y, 1.8);
567
+ });
568
+ };
569
+ /** Whether no box meets the circle at (x, z) between the heights y0 and y1. */
570
+ var clear = (w, x, z, y0, y1) => {
571
+ const reach = (PLAYER_R - EPS) ** 2;
572
+ return near(w, x, z, PLAYER_R).every((i) => {
573
+ const b = w.def.brushes[i];
574
+ const [dx, dz] = gap(b, x, z);
575
+ return dx * dx + dz * dz >= reach || b.max[1] <= y0 || b.min[1] >= y1;
576
+ });
577
+ };
578
+ /**
579
+ * The axis of a side (0 for x, 2 for z, the index into a brush corner) and the sign toward it,
580
+ * with the player's coordinate on that axis and on the other one.
581
+ */
582
+ var along = (p, side) => {
583
+ const [nx, nz] = TOWARD[side];
584
+ const a = nx ? 0 : 2;
585
+ return {
586
+ a,
587
+ o: 2 - a,
588
+ sign: nx || nz,
589
+ c: nx ? p.x : p.z,
590
+ co: nx ? p.z : p.x
591
+ };
592
+ };
593
+ /**
594
+ * The distance of the circle's centre to the face of the brush that looks at the player on
595
+ * `side`, when the centre is beside that face (not past its end) and the face is within reach of
596
+ * the cylinder; else undefined.
597
+ */
598
+ var faceDist = (b, p, side, reach) => {
599
+ const { a, o, sign, c, co } = along(p, side);
600
+ const dist = ((sign > 0 ? b.min[a] : b.max[a]) - c) * sign;
601
+ return dist >= .4 - TOUCH && dist <= .4 + reach && co >= b.min[o] && co <= b.max[o] ? dist : void 0;
602
+ };
603
+ /** Whether a wall on `side` covers the body from the knees to the head and is within reach. */
604
+ var wallOn = (p, w, side) => near(w, p.x, p.z, PLAYER_R + WALL_REACH).some((i) => {
605
+ const b = w.def.brushes[i];
606
+ return faceDist(b, p, side, .15) !== void 0 && b.min[1] < p.y + .5 && b.max[1] > p.y + 1.8;
607
+ });
608
+ /**
609
+ * The highest ledge the player can catch: on a side he faces and is not locked out of, a top
610
+ * GRAB_MIN to GRAB_MAX above the feet on a box that goes down at least 0.25 m under it, with the
611
+ * hands' reach (GRAB_REACH) from the cylinder, the body clear while it hangs and rises, and the
612
+ * top clear above where the climb ends.
613
+ */
614
+ var ledgeAt = (p, w) => {
615
+ const [fx, fz] = forward(p.yaw);
616
+ let best;
617
+ for (let side = 1; side <= 4; side++) {
618
+ const [nx, nz] = TOWARD[side];
619
+ if (side === p.wall || fx * nx + fz * nz < .5) continue;
620
+ const { a, sign } = along(p, side);
621
+ for (const i of near(w, p.x, p.z, PLAYER_R + GRAB_REACH)) {
622
+ const b = w.def.brushes[i];
623
+ const top = b.max[1];
624
+ const dy = top - p.y;
625
+ if (faceDist(b, p, side, .2) === void 0 || dy < 1.2 || dy > 2.4 || b.min[1] > top - .25 || best && best.top >= top) continue;
626
+ const face = sign > 0 ? b.min[a] : b.max[a];
627
+ const hang = face - sign * (PLAYER_R + GRAB_GAP);
628
+ const land = face + sign * CLIMB_INSET;
629
+ const [hx, hz] = a === 0 ? [hang, p.z] : [p.x, hang];
630
+ const [lx, lz] = a === 0 ? [land, p.z] : [p.x, land];
631
+ if (clear(w, hx, hz, Math.min(p.y, top - 2) + SKIN, top + 1.8) && clear(w, lx, lz, top + SKIN, top + 1.8)) best = {
632
+ side,
633
+ top,
634
+ hang
635
+ };
636
+ }
637
+ }
638
+ return best;
639
+ };
640
+ /** Whether the centre is over the top of the ledge, CLIMB_INSET in from its edge. */
641
+ var over = (p, w) => {
642
+ const { a, o, sign, c, co } = along(p, p.wall);
643
+ return near(w, p.x, p.z, 0).some((i) => {
644
+ const b = w.def.brushes[i];
645
+ const depth = sign > 0 ? c - b.min[a] : b.max[a] - c;
646
+ return f32(b.max[1]) === p.edge && co >= b.min[o] && co <= b.max[o] && depth >= .2;
647
+ });
648
+ };
649
+ /** Whether the player will be back on a floor within the jump buffer: a press now is kept for it. */
650
+ var landingSoon = (p, w) => {
651
+ if (p.vy >= 0) return false;
652
+ const [floor] = span(p, w, PLAYER_H);
653
+ const t = 6 * DT;
654
+ return p.y - floor <= -p.vy * t + 10 * t * t;
655
+ };
656
+ /** Changes the mode, and starts counting the time in it. */
657
+ var enter = (p, mode) => {
658
+ p.mode = mode;
659
+ p.t = 0;
660
+ };
661
+ /** Scales the horizontal speed to at most max. */
662
+ var cap = (p, max) => {
663
+ const s = len(p.vx, p.vz);
664
+ if (s > max) {
665
+ p.vx *= max / s;
666
+ p.vz *= max / s;
667
+ }
668
+ };
669
+ /**
670
+ * The wish without the part that pushes into a box the circle is touching: a player running at
671
+ * a wall at an angle slides along it at the speed the wall leaves him, and at a corner into two
672
+ * of them stops. Without it the speed built toward the wall would be cut by the wall every step.
673
+ * Returns the wish, its length (0 to 1) not kept: the top speed is scaled by it.
674
+ */
675
+ var unwalled = (p, w, c, h) => {
676
+ let [x, z] = [c.wx, c.wz];
677
+ for (const i of near(w, p.x, p.z, PLAYER_R + TOUCH)) {
678
+ const b = w.def.brushes[i];
679
+ const [dx, dz] = gap(b, p.x, p.z);
680
+ const d = len(dx, dz);
681
+ if (d > 0 && d <= .4 + TOUCH && blocks(b, p.y, h)) {
682
+ const into = (-x * dx - z * dz) / d;
683
+ if (into > 0) {
684
+ x += into * dx / d;
685
+ z += into * dz / d;
686
+ }
687
+ }
688
+ }
689
+ return [x, z];
690
+ };
691
+ /**
692
+ * Ground control: speed toward the wished direction (a vector of length 0 to 1), up to `top` times
693
+ * its length; friction with none.
694
+ */
695
+ var steer = (p, wish, top) => {
696
+ const m = len(wish[0], wish[1]);
697
+ if (m < EPS) {
698
+ const s = len(p.vx, p.vz);
699
+ const k = s ? Math.max(0, s - 45 * DT) / s : 0;
700
+ p.vx *= k;
701
+ p.vz *= k;
702
+ return;
703
+ }
704
+ const [wx, wz] = [wish[0] / m, wish[1] / m];
705
+ let par = p.vx * wx + p.vz * wz;
706
+ let [px, pz] = [p.vx - par * wx, p.vz - par * wz];
707
+ const across = len(px, pz);
708
+ if (across) {
709
+ const k = Math.max(0, across - 45 * DT) / across;
710
+ [px, pz] = [px * k, pz * k];
711
+ }
712
+ top *= m;
713
+ par = par < top ? Math.min(top, par + (par >= 6 ? 3 : 50) * DT) : Math.max(top, par - 12 * DT);
714
+ p.vx = px + par * wx;
715
+ p.vz = pz + par * wz;
716
+ };
449
717
  /**
450
- * Moves the feet by (dx, dz) for 1 / TICK_HZ s, then a jump from the ground, GRAVITY and the
451
- * collisions of the cylinder (PLAYER_R, PLAYER_H) with the brushes of the world: it slides along
452
- * their sides (see push), stands on the highest top under the circle (a top up to STEP_H above the
453
- * feet is climbed at once, a higher one needs a jump, an edge is a fall), and bumps its head on the
454
- * lowest bottom, in place. Feet under the world's kill floor put the player back at a spawn.
455
- * Sides and tops are judged from the feet at the start of the step.
718
+ * Moves the cylinder by its speed for one step: sideways (sliding along the boxes, which takes the
719
+ * speed into them away), then up or down by g times GRAVITY (a ceiling stops a rise, a floor a
720
+ * fall). Returns the speed it came down on a floor at, or -1 when it is not on one. With `snap`
721
+ * a drop of at most STEP_H stays on the ground (a stair going down is not a fall).
456
722
  */
457
- var walk = (p, dx, dz, jump, w) => {
458
- p.x += dx;
459
- p.z += dz;
460
- push(p, w);
461
- const [floor, ceil] = span(p, w);
462
- if (p.ground && jump) p.vy = 7;
463
- const vy = p.vy - 20 * DT;
723
+ var fly = (p, w, h, g, snap) => {
724
+ const y0 = p.y;
725
+ p.x += p.vx * DT;
726
+ p.z += p.vz * DT;
727
+ const [cx, cz] = push(p, w, h);
728
+ const d = len(cx, cz);
729
+ if (d) {
730
+ const into = (p.vx * cx + p.vz * cz) / d;
731
+ if (into < 0) {
732
+ p.vx -= into * cx / d;
733
+ p.vz -= into * cz / d;
734
+ }
735
+ }
736
+ const [floor, ceil] = span(p, w, h);
737
+ const vy = p.vy - g * 20 * DT;
464
738
  p.y += (p.vy + vy) / 2 * DT;
465
739
  p.vy = vy;
466
- p.ground = false;
467
- if (p.y + 1.8 > ceil) {
468
- p.y = ceil - PLAYER_H;
740
+ if (p.y + h > ceil) {
741
+ p.y = ceil - h;
469
742
  p.vy = Math.min(p.vy, 0);
470
743
  }
471
744
  if (p.vy <= 0 ? p.y <= floor + SKIN : p.y < floor) {
472
745
  p.y = floor;
473
746
  if (p.vy <= 0) {
747
+ const hit = -p.vy;
474
748
  p.vy = 0;
475
- p.ground = true;
749
+ return hit;
476
750
  }
751
+ } else if (snap && p.vy <= 0 && floor >= y0 - .5) {
752
+ p.y = floor;
753
+ p.vy = 0;
754
+ return 0;
477
755
  }
478
- if (p.y < w.def.floor) {
479
- const { x, y, z } = spawn(p.id, w, p.id);
480
- Object.assign(p, {
481
- x,
482
- y,
483
- z,
484
- vy: 0,
485
- ground: true
486
- });
756
+ return -1;
757
+ };
758
+ /** A jump or a kick: the vertical speed, and the windows closed. */
759
+ var leap = (p, up) => {
760
+ p.vy = up;
761
+ p.coyote = 0;
762
+ p.buffer = 0;
763
+ p.ev |= EV.JUMP;
764
+ };
765
+ /** Hanging: let go, climb, vault, or settle at hanging height. */
766
+ var hang = (p, w, c) => {
767
+ const [nx, nz] = TOWARD[p.wall];
768
+ const go = (mode) => {
769
+ enter(p, mode);
770
+ p.edge = 0;
771
+ };
772
+ if (c.keys & (KEY.BACK | KEY.CROUCH)) {
773
+ go(MODE.AIR);
774
+ p.coyote = 0;
775
+ } else if (c.jump && c.fx * nx + c.fz * nz < 0) {
776
+ go(MODE.AIR);
777
+ p.vx = -nx * 4;
778
+ p.vz = -nz * 4;
779
+ leap(p, 6);
780
+ } else if (c.jump || c.fwd && p.t >= 6) enter(p, MODE.CLIMB);
781
+ else if (p.t >= 30) {
782
+ go(MODE.AIR);
783
+ p.coyote = 0;
784
+ } else {
785
+ const target = Math.max(p.edge - 2, span(p, w, PLAYER_H)[0]);
786
+ p.y += clamp(target - p.y, -8 * DT, 8 * DT);
487
787
  }
488
- p.x = f32(p.x);
489
- p.y = f32(p.y);
490
- p.z = f32(p.z);
491
- p.vy = f32(p.vy);
788
+ };
789
+ /** Climbing: up to the top, then in from the edge, then standing on it. */
790
+ var climb = (p, w) => {
791
+ const [nx, nz] = TOWARD[p.wall];
792
+ if (p.y < p.edge) p.y = Math.min(p.edge, p.y + 8 * DT);
793
+ else {
794
+ p.x += nx * CLIMB_FWD_SPEED * DT;
795
+ p.z += nz * CLIMB_FWD_SPEED * DT;
796
+ }
797
+ if (p.y >= p.edge && over(p, w)) {
798
+ enter(p, MODE.GROUND);
799
+ p.vx = nx * 3;
800
+ p.vz = nz * 3;
801
+ p.vy = 0;
802
+ p.edge = 0;
803
+ p.wall = SIDE.NONE;
804
+ p.coyote = 6;
805
+ p.ev |= EV.CLIMB;
806
+ } else if (p.t >= 40) {
807
+ enter(p, p.y >= p.edge ? MODE.GROUND : MODE.AIR);
808
+ p.edge = 0;
809
+ p.wall = SIDE.NONE;
810
+ }
811
+ };
812
+ /** Running along a wall: kick off it, or keep going, or end the run. */
813
+ var run = (p, w, c) => {
814
+ const [nx, nz] = TOWARD[p.wall];
815
+ const into = p.vx * nx + p.vz * nz;
816
+ const [tx, tz] = [p.vx - into * nx, p.vz - into * nz];
817
+ const s = len(tx, tz);
818
+ if (c.jump) {
819
+ enter(p, MODE.AIR);
820
+ p.vx = tx - nx * 5;
821
+ p.vz = tz - nz * 5;
822
+ leap(p, WALL_KICK_UP);
823
+ p.jumps = 1;
824
+ p.dashes = 1;
825
+ p.ev |= EV.WALLRUN_END;
826
+ } else if (!c.fwd || s < 5.5 || p.t >= 60 || !wallOn(p, w, p.wall)) {
827
+ enter(p, MODE.AIR);
828
+ p.ev |= EV.WALLRUN_END;
829
+ } else {
830
+ const k = Math.max(0, s - WALLRUN_DRAG * DT) / s;
831
+ p.vx = tx * k;
832
+ p.vz = tz * k;
833
+ }
834
+ };
835
+ /** Starts a wall-run if the player is moving fast along a wall he is next to. */
836
+ var startRun = (p, w, c) => {
837
+ if (!c.fwd) return;
838
+ const s = len(p.vx, p.vz);
839
+ for (let side = 1; side <= 4; side++) {
840
+ const [nx, nz] = TOWARD[side];
841
+ const into = p.vx * nx + p.vz * nz;
842
+ const [tx, tz] = [p.vx - into * nx, p.vz - into * nz];
843
+ if (side !== p.wall && len(tx, tz) >= 7 && Math.abs(into) <= .4 * s && wallOn(p, w, side)) {
844
+ enter(p, MODE.WALLRUN);
845
+ p.wall = side;
846
+ [p.vx, p.vz] = [tx, tz];
847
+ p.vy = Math.min(p.vy, 3);
848
+ p.jumps = 1;
849
+ p.dashes = 1;
850
+ p.coyote = 0;
851
+ p.ev |= EV.WALLRUN_START;
852
+ return;
853
+ }
854
+ }
855
+ };
856
+ /** Takes hold of a ledge: hang there. */
857
+ var grab = (p, l) => {
858
+ enter(p, MODE.HANG);
859
+ p.wall = l.side;
860
+ p.edge = f32(l.top);
861
+ if (l.side <= SIDE.WEST) p.x = l.hang;
862
+ else p.z = l.hang;
863
+ p.vx = p.vy = p.vz = 0;
864
+ p.jumps = 1;
865
+ p.dashes = 1;
866
+ p.coyote = 0;
867
+ p.buffer = 0;
868
+ p.ev |= EV.GRAB;
869
+ };
870
+ /** The modes that move freely: everything but the hang and the climb. */
871
+ var free = (p, w, c) => {
872
+ const ground = p.mode === MODE.GROUND;
873
+ if (c.dash && p.dash === 0 && (ground || (p.mode === MODE.AIR || p.mode === MODE.WALLRUN) && p.dashes > 0)) {
874
+ const [dx, dz] = c.wx || c.wz ? [c.wx, c.wz] : [c.fx, c.fz];
875
+ if (!ground) {
876
+ p.dashes--;
877
+ p.vy = 0;
878
+ if (p.mode === MODE.WALLRUN) {
879
+ enter(p, MODE.AIR);
880
+ p.ev |= EV.WALLRUN_END;
881
+ }
882
+ }
883
+ p.vx = dx * 16;
884
+ p.vz = dz * 16;
885
+ p.dash = 60;
886
+ p.ev |= EV.DASH;
887
+ }
888
+ const burst = p.dash > 50;
889
+ if (p.mode === MODE.WALLRUN) run(p, w, c);
890
+ if (onFloor(p.mode)) onGround(p, w, c, burst);
891
+ if (p.mode === MODE.AIR) air(p, w, c, burst);
892
+ const g = burst && p.vy <= 0 ? 0 : p.mode === MODE.WALLRUN ? WALLRUN_GRAVITY : 1;
893
+ const was = p.mode;
894
+ const hit = fly(p, w, heightOf(p), g, onFloor(was));
895
+ if (hit >= 0) {
896
+ if (!onFloor(p.mode)) {
897
+ p.ev |= EV.LAND | (p.mode === MODE.WALLRUN ? EV.WALLRUN_END : 0);
898
+ p.fall = hit;
899
+ p.jumps = 1;
900
+ p.dashes = 1;
901
+ p.wall = SIDE.NONE;
902
+ if (c.keys & KEY.CROUCH) {
903
+ const slide = len(p.vx, p.vz) >= SLIDE_MIN_SPEED;
904
+ enter(p, slide ? MODE.SLIDE : MODE.CROUCH);
905
+ p.ev |= slide ? EV.SLIDE_START : 0;
906
+ } else enter(p, MODE.GROUND);
907
+ }
908
+ p.coyote = 6;
909
+ } else if (onFloor(p.mode)) {
910
+ p.ev |= p.mode === MODE.SLIDE ? EV.SLIDE_END : 0;
911
+ enter(p, MODE.AIR);
912
+ }
913
+ if (p.mode === MODE.AIR) {
914
+ const ledge = ledgeAt(p, w);
915
+ if (ledge) grab(p, ledge);
916
+ else startRun(p, w, c);
917
+ }
918
+ };
919
+ /** On the floor: jump, crouch, slide, stand up, and the speed. */
920
+ var onGround = (p, w, c, burst) => {
921
+ const low = (c.keys & KEY.CROUCH) !== 0;
922
+ if (p.buffer > 0 && (p.mode === MODE.GROUND || canStand(p, w))) {
923
+ p.ev |= p.mode === MODE.SLIDE ? EV.SLIDE_END : 0;
924
+ enter(p, MODE.AIR);
925
+ leap(p, 7);
926
+ return;
927
+ }
928
+ const speed = len(p.vx, p.vz);
929
+ if (p.mode === MODE.GROUND && low) {
930
+ const slide = speed >= SLIDE_MIN_SPEED;
931
+ enter(p, slide ? MODE.SLIDE : MODE.CROUCH);
932
+ p.ev |= slide ? EV.SLIDE_START : 0;
933
+ } else if (p.mode === MODE.SLIDE && (p.t >= 60 || speed < 4.5)) {
934
+ p.ev |= EV.SLIDE_END;
935
+ enter(p, !low && canStand(p, w) ? MODE.GROUND : MODE.CROUCH);
936
+ } else if (p.mode === MODE.SLIDE && !low && canStand(p, w)) {
937
+ p.ev |= EV.SLIDE_END;
938
+ enter(p, MODE.GROUND);
939
+ } else if (p.mode === MODE.CROUCH && !low && canStand(p, w)) enter(p, MODE.GROUND);
940
+ if (burst) return;
941
+ if (p.mode === MODE.SLIDE) {
942
+ const k = Math.max(0, speed - 4 * DT) / speed;
943
+ p.vx *= k;
944
+ p.vz *= k;
945
+ } else if (p.mode === MODE.CROUCH) steer(p, unwalled(p, w, c, LOW_H), 3);
946
+ else {
947
+ const sprint = (c.keys & KEY.SPRINT) !== 0 && c.fwd;
948
+ steer(p, unwalled(p, w, c, PLAYER_H), sprint ? 9 : 6);
949
+ }
950
+ };
951
+ /** In the air: the extra jump, a jump just after an edge (coyote), and air control. */
952
+ var air = (p, w, c, burst) => {
953
+ if (p.buffer > 0) {
954
+ if (p.coyote > 0) leap(p, 7);
955
+ else if (p.jumps > 0 && !landingSoon(p, w)) {
956
+ p.jumps--;
957
+ leap(p, 6);
958
+ p.ev = p.ev & ~EV.JUMP | EV.DOUBLE;
959
+ }
960
+ }
961
+ if (burst || !c.wx && !c.wz) return;
962
+ const before = len(p.vx, p.vz);
963
+ p.vx += c.wx * 14 * DT;
964
+ p.vz += c.wz * 14 * DT;
965
+ cap(p, Math.max(before, 6));
492
966
  };
493
967
  /**
494
968
  * Applies one input for 1 / TICK_HZ s, in place: yaw and pitch from the input (pitch clamped to
495
- * MAX_PITCH), RUN_SPEED along the held keys (diagonals not faster), then walk().
969
+ * MAX_PITCH), then the step of the current mode (see the top of the file). Feet under the world's
970
+ * kill floor put the player back at a spawn.
496
971
  */
497
972
  var move = (p, i, w) => {
498
973
  p.yaw = norm(i.yaw);
499
974
  p.pitch = Math.max(-MAX_PITCH, Math.min(MAX_PITCH, i.pitch));
500
975
  const f = (i.keys & KEY.FORWARD ? 1 : 0) - (i.keys & KEY.BACK ? 1 : 0);
501
976
  const s = (i.keys & KEY.RIGHT ? 1 : 0) - (i.keys & KEY.LEFT ? 1 : 0);
502
- const d = 6 * DT / (Math.hypot(f, s) || 1);
977
+ const n = len(f, s) || 1;
503
978
  const [fx, fz] = forward(p.yaw);
504
- walk(p, (fx * f - fz * s) * d, (fz * f + fx * s) * d, (i.keys & KEY.JUMP) !== 0, w);
979
+ const pressed = (bit) => (i.keys & bit) !== 0 && (p.held & bit) === 0;
980
+ const c = {
981
+ keys: i.keys,
982
+ jump: pressed(KEY.JUMP),
983
+ dash: pressed(KEY.DASH),
984
+ fwd: f > 0,
985
+ wx: (fx * f - fz * s) / n,
986
+ wz: (fz * f + fx * s) / n,
987
+ fx,
988
+ fz
989
+ };
990
+ const before = p.mode;
991
+ p.ev = 0;
992
+ p.fall = 0;
993
+ if (c.jump) p.buffer = 7;
994
+ if (p.dash > 0) {
995
+ p.dash--;
996
+ if (p.dash === 50) cap(p, 10);
997
+ }
998
+ if (p.mode === MODE.HANG) hang(p, w, c);
999
+ if (p.mode === MODE.CLIMB) climb(p, w);
1000
+ else if (p.mode !== MODE.HANG) free(p, w, c);
1001
+ if (p.y < w.def.floor) {
1002
+ const { yaw, pitch, id } = p;
1003
+ Object.assign(p, spawn(id, w, id), {
1004
+ yaw,
1005
+ pitch
1006
+ });
1007
+ }
1008
+ p.t = p.mode === before ? Math.min(255, p.t + 1) : p.t;
1009
+ p.buffer = Math.max(0, p.buffer - 1);
1010
+ if (p.mode === MODE.AIR && before === MODE.AIR) p.coyote = Math.max(0, p.coyote - 1);
1011
+ p.held = i.keys & (KEY.JUMP | KEY.DASH);
1012
+ p.ground = onFloor(p.mode);
1013
+ p.x = f32(p.x);
1014
+ p.y = f32(p.y);
1015
+ p.z = f32(p.z);
1016
+ p.vx = f32(p.vx);
1017
+ p.vy = f32(p.vy);
1018
+ p.vz = f32(p.vz);
1019
+ p.edge = f32(p.edge);
505
1020
  };
506
1021
  var TURN = 65536 / (2 * Math.PI);
507
1022
  /** Radians to 65536ths of a turn; the mask also folds negative and ≥ 2π angles into one turn. */
@@ -525,21 +1040,27 @@ var decodeInput = (buf) => {
525
1040
  };
526
1041
  };
527
1042
  var encodeSnapshot = (s) => {
528
- const v = /* @__PURE__ */ new DataView(/* @__PURE__ */ new ArrayBuffer(4 + 24 * s.players.length));
1043
+ const v = /* @__PURE__ */ new DataView(/* @__PURE__ */ new ArrayBuffer(4 + 39 * s.players.length));
529
1044
  v.setUint8(0, 2);
530
1045
  v.setUint16(1, s.tick & 65535);
531
1046
  v.setUint8(3, s.players.length);
532
1047
  s.players.forEach((p, i) => {
533
- const o = 4 + i * 24;
1048
+ const o = 4 + i * 39;
534
1049
  v.setUint8(o, p.id);
535
1050
  v.setFloat32(o + 1, p.x);
536
1051
  v.setFloat32(o + 5, p.y);
537
1052
  v.setFloat32(o + 9, p.z);
538
- v.setFloat32(o + 13, p.vy);
539
- v.setUint16(o + 17, toTurn(p.yaw));
540
- v.setInt16(o + 19, toPitch(p.pitch));
541
- v.setUint16(o + 21, p.ack & 65535);
542
- v.setUint8(o + 23, +p.ground);
1053
+ v.setFloat32(o + 13, p.vx);
1054
+ v.setFloat32(o + 17, p.vy);
1055
+ v.setFloat32(o + 21, p.vz);
1056
+ v.setFloat32(o + 25, p.edge);
1057
+ v.setUint16(o + 29, toTurn(p.yaw));
1058
+ v.setInt16(o + 31, toPitch(p.pitch));
1059
+ v.setUint16(o + 33, p.ack & 65535);
1060
+ v.setUint8(o + 35, p.mode & 7 | (p.wall & 7) << 3 | (p.jumps & 1) << 6 | p.dashes << 7);
1061
+ v.setUint8(o + 36, p.t);
1062
+ v.setUint8(o + 37, p.dash);
1063
+ v.setUint8(o + 38, p.coyote & 7 | (p.buffer & 7) << 3 | (p.held & KEY.JUMP ? 64 : 0) | (p.held & KEY.DASH ? 128 : 0));
543
1064
  });
544
1065
  return v.buffer;
545
1066
  };