@korial29/tiny-fps 0.12.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,37 +5,23 @@ 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
- /** A floor at most this much higher is walked onto; a higher one needs a jump. */
9
- var STEP_H = .5;
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;
10
24
  var RULES = { maxPlayers: 16 };
11
- // src/shared/level.ts
12
- /** Joins every edge a→b with the sector that has b→a, if any: that edge is a portal. */
13
- var compile = (map) => {
14
- const key = (ax, az, bx, bz) => `${ax},${az},${bx},${bz}`;
15
- const owner = /* @__PURE__ */ new Map();
16
- map.sectors.forEach(({ pts }, s) => pts.forEach(([ax, az], i) => owner.set(key(ax, az, ...pts[(i + 1) % pts.length]), s)));
17
- return {
18
- map,
19
- edges: map.sectors.flatMap(({ pts }, front) => pts.map(([ax, az], i) => {
20
- const [bx, bz] = pts[(i + 1) % pts.length];
21
- return {
22
- ax,
23
- az,
24
- bx,
25
- bz,
26
- front,
27
- back: owner.get(key(bx, bz, ax, az)) ?? -1
28
- };
29
- }))
30
- };
31
- };
32
- /** Whether (x, z) is inside the sector, its edges included. */
33
- var inside = ({ pts }, x, z) => pts.every(([ax, az], i) => {
34
- const [bx, bz] = pts[(i + 1) % pts.length];
35
- return (bx - ax) * (z - az) - (bz - az) * (x - ax) >= 0;
36
- });
37
- /** Index of the first sector holding (x, z), -1 outside the map. */
38
- var sectorAt = ({ map }, x, z) => map.sectors.findIndex((s) => inside(s, x, z));
39
25
  // src/shared/types.ts
40
26
  /** Bits of Input.keys. Keys are read by KeyboardEvent.code, so WASD is ZQSD on AZERTY. */
41
27
  var KEY = {
@@ -43,9 +29,59 @@ var KEY = {
43
29
  BACK: 2,
44
30
  LEFT: 4,
45
31
  RIGHT: 8,
46
- 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
47
56
  };
48
- /** Procedural textures, generated by the client. A sector names one per surface. */
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
83
+ };
84
+ /** Procedural textures, generated by the client. A brush names one for its top and one for its sides. */
49
85
  var TEX = {
50
86
  BRICK: 0,
51
87
  TILE: 1,
@@ -61,236 +97,296 @@ var TEX = {
61
97
  GRATE: 7
62
98
  };
63
99
  // src/shared/map.ts
64
- var sector = (pts, floor, ceil, ft, light) => ({
65
- pts,
66
- floor,
67
- ceil,
68
- ft,
69
- wt: TEX.BRICK,
70
- ct: TEX.STONE,
71
- light
72
- });
73
- sector([
74
- [0, 0],
75
- [16, 0],
76
- [16, 6],
77
- [16, 10],
78
- [16, 16],
79
- [0, 16]
80
- ], 1, 5, TEX.TILE), sector([
81
- [16, 6],
82
- [20, 6],
83
- [20, 10],
84
- [16, 10]
85
- ], 1, 3.5, TEX.TILE, .6), sector([
86
- [20, 0],
87
- [36, 0],
88
- [36, 6],
89
- [30, 6],
90
- [26, 6],
91
- [20, 6]
92
- ], 1, 7, TEX.TILE), sector([
93
- [20, 6],
94
- [26, 6],
95
- [26, 10],
96
- [20, 10]
97
- ], 1, 7, TEX.TILE), sector([
98
- [30, 6],
99
- [36, 6],
100
- [36, 10],
101
- [30, 10]
102
- ], 1, 7, TEX.TILE), sector([
103
- [20, 10],
104
- [26, 10],
105
- [30, 10],
106
- [36, 10],
107
- [36, 16],
108
- [32, 16],
109
- [28, 16],
110
- [20, 16]
111
- ], 1, 7, TEX.TILE), sector([
112
- [26, 6],
113
- [30, 6],
114
- [30, 10],
115
- [26, 10]
116
- ], 0, 7, TEX.LAVA), sector([
117
- [28, 16],
118
- [32, 16],
119
- [32, 17],
120
- [28, 17]
121
- ], 1.4, 7, TEX.STONE), sector([
122
- [28, 17],
123
- [32, 17],
124
- [32, 18],
125
- [28, 18]
126
- ], 1.8, 7, TEX.STONE), sector([
127
- [28, 18],
128
- [32, 18],
129
- [32, 19],
130
- [28, 19]
131
- ], 2.2, 7, TEX.STONE), sector([
132
- [24, 19],
133
- [28, 19],
134
- [32, 19],
135
- [36, 19],
136
- [36, 26],
137
- [24, 26]
138
- ], 2.6, 7, TEX.TILE, .8);
139
- /** Grid lines of ROOF, metres: every feature below starts and ends on one, so the cells join exactly. */
140
- var GRID_X = [
141
- 0,
142
- 1.2,
143
- 7,
144
- 9,
145
- 14,
146
- 17,
147
- 23,
148
- 26,
149
- 27,
150
- 31,
151
- 38.8,
152
- 40
153
- ];
154
- var GRID_Z = [
155
- 0,
156
- 1.2,
157
- 8,
158
- 9,
159
- 10,
160
- 11,
161
- 16,
162
- 18,
163
- 24,
164
- 26,
165
- 38.8,
166
- 40
167
- ];
168
- /** A very high ceiling: the sky, until the sector walls go (step 2). */
169
- var SKY = 120;
170
- /** What ROOF is made of, painted in order: x0, z0, x1, z1, floor height, floor texture. */
171
- var PAINT = [
172
- [
173
- 0,
174
- 0,
175
- 40,
176
- 40,
177
- 1,
178
- TEX.STONE
179
- ],
180
- [
181
- 1.2,
182
- 1.2,
183
- 38.8,
184
- 38.8,
185
- 0,
186
- TEX.TILE
100
+ /** The bottom of every building: far below the kill floor, so nothing shows beneath a roof. */
101
+ var BASE = -30;
102
+ /** A building: the footprint x0..x1 by z0..z1 from the street up to a flat roof at `top`. */
103
+ var roof = (x0, z0, x1, z1, top) => ({
104
+ min: [
105
+ x0,
106
+ BASE,
107
+ z0
187
108
  ],
188
- [
189
- 14,
190
- 1.2,
191
- 26,
192
- 8,
193
- 1.6,
194
- TEX.TILE
109
+ max: [
110
+ x1,
111
+ top,
112
+ z1
195
113
  ],
196
- [
197
- 17,
198
- 8,
199
- 23,
200
- 9,
201
- 1.2,
202
- TEX.STONE
203
- ],
204
- [
205
- 17,
206
- 9,
207
- 23,
208
- 10,
209
- .8,
210
- TEX.STONE
211
- ],
212
- [
213
- 17,
214
- 10,
215
- 23,
216
- 11,
217
- .4,
218
- TEX.STONE
114
+ top: TEX.TILE,
115
+ side: TEX.BRICK
116
+ });
117
+ /** Any box, corners (x0, y0, z0) and (x1, y1, z1). */
118
+ var box = ([x0, y0, z0], [x1, y1, z1], top, side = top, tint) => ({
119
+ min: [
120
+ x0,
121
+ y0,
122
+ z0
219
123
  ],
220
- [
221
- 7,
222
- 16,
223
- 9,
224
- 18,
225
- 1,
226
- TEX.WOOD
124
+ max: [
125
+ x1,
126
+ y1,
127
+ z1
227
128
  ],
228
- [
229
- 27,
230
- 24,
231
- 31,
232
- 26,
233
- 2,
234
- TEX.METAL
235
- ]
236
- ];
129
+ top,
130
+ side,
131
+ tint
132
+ });
133
+ /** Six steps of 0.4 m, 1 m deep and 2 m wide (x 12 to 14) from z down to z - 6, on a roof at y. */
134
+ var stair = (z, y) => [
135
+ 1,
136
+ 2,
137
+ 3,
138
+ 4,
139
+ 5,
140
+ 6
141
+ ].map((k) => box([
142
+ 12,
143
+ y,
144
+ z - k
145
+ ], [
146
+ 14,
147
+ y + .4 * k,
148
+ z - k + 1
149
+ ], TEX.STONE, TEX.STONE));
237
150
  /**
238
- * The first rooftop, for the capture-the-flag game: a flat 40 x 40 m deck in the open, a 1 m
239
- * parapet round it, a crate, a block and a platform with steps. Built as a grid of cells, one
240
- * sector each: neighbours at one height are open to each other, and the edge is solid wall.
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.
155
+ *
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).
158
+ * - On B, a platform floating at 12.2 to 12.6 m (an overhang to walk under, a bump for a jump) and
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
160
+ * is a 2.4 m ledge to grab. Two poles stand by the platform.
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
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.
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.
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.
241
166
  */
242
- var ROOF = {
167
+ var ROOFTOPS = {
243
168
  name: "Rooftops",
244
- fog: [
245
- .8,
246
- .88,
247
- 1,
248
- .02
169
+ brushes: [
170
+ roof(4, 57.7, 24, 76, 10),
171
+ roof(4, 36, 24, 53.5, 10),
172
+ roof(4, 20, 24, 36, 12.4),
173
+ roof(4, -5, 24, 7, 12.4),
174
+ roof(27, 20, 47, 50, 8),
175
+ roof(27, 60, 47, 76, 8),
176
+ box([
177
+ 9,
178
+ 12.2,
179
+ 39
180
+ ], [
181
+ 19,
182
+ 12.6,
183
+ 47
184
+ ], TEX.METAL, TEX.METAL),
185
+ ...stair(53, 10),
186
+ box([
187
+ 5,
188
+ 10,
189
+ 42
190
+ ], [
191
+ 5.5,
192
+ 14,
193
+ 42.5
194
+ ], TEX.METAL),
195
+ box([
196
+ 5,
197
+ 10,
198
+ 50
199
+ ], [
200
+ 5.5,
201
+ 14,
202
+ 50.5
203
+ ], TEX.METAL),
204
+ box([
205
+ 8,
206
+ 12.4,
207
+ 24
208
+ ], [
209
+ 20,
210
+ 12.85,
211
+ 24.45
212
+ ], TEX.METAL),
213
+ box([
214
+ 36,
215
+ 8,
216
+ 22
217
+ ], [
218
+ 37,
219
+ 14.5,
220
+ 48
221
+ ], TEX.STONE, TEX.STONE, [
222
+ 1,
223
+ .75,
224
+ .62
225
+ ]),
226
+ box([
227
+ 30,
228
+ 8,
229
+ 28
230
+ ], [
231
+ 34,
232
+ 10,
233
+ 32
234
+ ], TEX.STONE, TEX.BRICK),
235
+ box([
236
+ 40,
237
+ 8,
238
+ 30
239
+ ], [
240
+ 44,
241
+ 10.2,
242
+ 34
243
+ ], TEX.STONE, TEX.BRICK),
244
+ box([
245
+ 28,
246
+ 9.2,
247
+ 44
248
+ ], [
249
+ 35,
250
+ 12,
251
+ 46
252
+ ], TEX.METAL, TEX.GRATE, [
253
+ .8,
254
+ .9,
255
+ 1
256
+ ]),
257
+ box([
258
+ 41.5,
259
+ 7.6,
260
+ 50
261
+ ], [
262
+ 42,
263
+ 8,
264
+ 60
265
+ ], TEX.METAL),
266
+ box([
267
+ 30,
268
+ 8,
269
+ 64
270
+ ], [
271
+ 34,
272
+ 9.6,
273
+ 68
274
+ ], TEX.STONE, TEX.BRICK)
249
275
  ],
250
- sectors: GRID_X.slice(1).flatMap((x1, i) => GRID_Z.slice(1).map((z1, j) => {
251
- const [x0, z0] = [GRID_X[i], GRID_Z[j]];
252
- const [cx, cz] = [(x0 + x1) / 2, (z0 + z1) / 2];
253
- const [, , , , floor, tex] = PAINT.filter(([ax, az, bx, bz]) => ax < cx && cx < bx && az < cz && cz < bz).at(-1);
254
- return sector([
255
- [x0, z0],
256
- [x1, z0],
257
- [x1, z1],
258
- [x0, z1]
259
- ], floor, SKY, tex);
260
- })),
261
276
  spawns: [
262
277
  [
263
278
  6,
264
- 34,
279
+ 10,
280
+ 72,
265
281
  0
266
282
  ],
267
283
  [
268
- 13,
269
- 34,
284
+ 10,
285
+ 10,
286
+ 72,
270
287
  0
271
288
  ],
272
289
  [
273
- 20,
274
- 34,
290
+ 14,
291
+ 10,
292
+ 72,
275
293
  0
276
294
  ],
277
295
  [
278
- 27,
279
- 34,
296
+ 18,
297
+ 10,
298
+ 72,
280
299
  0
281
300
  ],
282
301
  [
283
- 34,
284
- 34,
302
+ 22,
303
+ 10,
304
+ 72,
305
+ 0
306
+ ],
307
+ [
308
+ 8,
309
+ 10,
310
+ 66,
311
+ 0
312
+ ],
313
+ [
314
+ 14,
315
+ 10,
316
+ 66,
285
317
  0
286
318
  ],
287
319
  [
288
320
  20,
289
- 22,
321
+ 10,
322
+ 66,
290
323
  0
291
324
  ]
325
+ ],
326
+ floor: -25,
327
+ sun: {
328
+ dir: [
329
+ .6,
330
+ .64,
331
+ .48
332
+ ],
333
+ color: [
334
+ 3,
335
+ 2.7,
336
+ 2.2
337
+ ]
338
+ },
339
+ sky: [[
340
+ .12,
341
+ .3,
342
+ .75
343
+ ], [
344
+ .7,
345
+ .82,
346
+ .95
347
+ ]],
348
+ fog: [
349
+ .7,
350
+ .82,
351
+ .95,
352
+ .008
292
353
  ]
293
354
  };
355
+ // src/shared/world.ts
356
+ /** Metres per grid cell: about the size of the player's reach, so a query touches 1 to 4 cells. */
357
+ var CELL = 4;
358
+ /** Cell coordinates run from -OFF to OFF - 1: a 8 km square at 4 m, far more than any map. */
359
+ var OFF = 1024;
360
+ /** The key of the cell with integer coordinates (cx, cz). */
361
+ var key = (cx, cz) => (cx + OFF) * (2 * OFF) + cz + OFF;
362
+ /** Integer cell coordinate of a metre value, clamped to the keyed range. */
363
+ var at = (v, cell) => Math.min(Math.max(Math.floor(v / cell), -1024), 1023);
364
+ /** Builds the grid: each brush is listed in every cell its footprint covers. */
365
+ var build = (def, cell = CELL) => {
366
+ const grid = /* @__PURE__ */ new Map();
367
+ def.brushes.forEach(({ min, max }, i) => {
368
+ 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++) {
369
+ const k = key(cx, cz);
370
+ const list = grid.get(k);
371
+ if (list) list.push(i);
372
+ else grid.set(k, [i]);
373
+ }
374
+ });
375
+ return {
376
+ def,
377
+ cell,
378
+ grid
379
+ };
380
+ };
381
+ /**
382
+ * Indices of the brushes whose footprint may overlap the square of half side r around (x, z): every
383
+ * brush listed in a cell the square touches, once each. A candidate list, not an exact overlap.
384
+ */
385
+ var near = (w, x, z, r) => {
386
+ const out = [];
387
+ 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);
388
+ return out;
389
+ };
294
390
  // src/shared/physics.ts
295
391
  var DT = 1 / 60;
296
392
  var TAU = Math.PI * 2;
@@ -302,152 +398,625 @@ var EPS = 1e-6;
302
398
  * lands on it.
303
399
  */
304
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;
305
406
  /** An angle in [0, 2π). */
306
407
  var norm = (a) => (a % TAU + TAU) % TAU;
307
408
  /** Unit vector the player faces at yaw, on the floor plane: yaw 0 looks toward -z. */
308
409
  var forward = (yaw) => [-Math.sin(yaw), -Math.cos(yaw)];
309
410
  var f32 = Math.fround;
411
+ var sqrt = Math.sqrt;
310
412
  var clamp = (v, lo, hi) => Math.min(Math.max(v, lo), hi);
311
- /**
312
- * Whether the edge stops a body of height h whose feet are at y: a solid wall, a floor on the far
313
- * side more than `step` above the feet, or an opening (the lower ceiling minus the higher floor)
314
- * too short for the body or under the head. SKIN keeps rounding from flipping the answer for a
315
- * player resting on a step or against a ceiling.
316
- */
317
- var shut = ({ map }, e, y, h, step) => {
318
- if (e.back < 0) return true;
319
- const f = map.sectors[e.front];
320
- const k = map.sectors[e.back];
321
- const top = Math.min(f.ceil, k.ceil);
322
- return k.floor > y + step + SKIN || top - Math.max(f.floor, k.floor) < h - SKIN || top < y + h - SKIN;
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
323
439
  };
440
+ /** A player standing on spawn point n of the world (modulo the count). */
441
+ var spawn = (id, w, n) => {
442
+ const { spawns } = w.def;
443
+ const [x, y, z, yaw] = spawns[(n % spawns.length + spawns.length) % spawns.length];
444
+ return {
445
+ id,
446
+ x: f32(x),
447
+ y: f32(y),
448
+ z: f32(z),
449
+ vy: 0,
450
+ yaw: norm(yaw),
451
+ pitch: 0,
452
+ ground: true,
453
+ ...STILL
454
+ };
455
+ };
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;
462
+ /** The closest point of the brush footprint to (x, z), as the offset from it to (x, z). */
463
+ var gap = (b, x, z) => [x - clamp(x, b.min[0], b.max[0]), z - clamp(z, b.min[2], b.max[2])];
324
464
  /**
325
- * Pushes the player out of every shut edge it overlaps, deepest first (up to 4 passes: two walls
326
- * in a corner settle in two). The push is along the vector from the closest point of the edge, so
327
- * against a wall it is the normal (the other axis keeps moving: the slide) and at an endpoint it
328
- * is radial: a player running at a corner slides round it. Deepest first matters on a straight
329
- * wall split by a collinear point: the endpoint of the next piece is always shallower than the
330
- * normal push of the piece the player is on, so it never pulls him back along the wall.
331
- * An edge only counts from its front side (the inside of its sector); across it, the twin edge of
332
- * the neighbour decides.
333
- * Tunnelling is impossible: a step is RUN_SPEED / TICK_HZ = 0.1 m and the axis ends each step at
334
- * least PLAYER_R = 0.4 m from every shut edge it started beside, so it cannot cross one in 0.1 m.
465
+ * Pushes the player out of every brush that blocks it and that the circle overlaps, deepest first
466
+ * (up to 4 passes: two walls in a corner settle in two). The push is along the vector from the
467
+ * closest point of the box, so against a side it is the normal (the other axis keeps moving: the
468
+ * slide) and at an edge it is radial: a player running at a corner slides round it. Deepest first
469
+ * matters on a flat wall made of several boxes: the corner of the next box is always shallower
470
+ * than the normal push of the box the player is on, so it never pulls him back along the wall.
471
+ * A centre inside a footprint (it never gets there by walking, see below) leaves by the nearest
472
+ * side. Returns the whole push, for the caller to take the speed into the wall away.
473
+ *
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.
335
478
  */
336
- var push = (p, lvl, r, h, step) => {
337
- const reach = (r - EPS) ** 2;
479
+ var push = (p, w, h) => {
480
+ const { brushes } = w.def;
481
+ const ids = near(w, p.x, p.z, 2 * PLAYER_R);
482
+ const reach = (PLAYER_R - EPS) ** 2;
483
+ let [tx, tz] = [0, 0];
338
484
  for (let pass = 0; pass < 4; pass++) {
339
485
  let best = 0;
340
486
  let px = 0;
341
487
  let pz = 0;
342
- for (const e of lvl.edges) {
343
- const ex = e.bx - e.ax;
344
- const ez = e.bz - e.az;
345
- const rx = p.x - e.ax;
346
- const rz = p.z - e.az;
347
- if (ex * rz - ez * rx < 0) continue;
348
- const t = clamp((rx * ex + rz * ez) / (ex * ex + ez * ez), 0, 1);
349
- const dx = rx - t * ex;
350
- const dz = rz - t * ez;
488
+ for (const i of ids) {
489
+ const b = brushes[i];
490
+ if (!blocks(b, p.y, h)) continue;
491
+ const [dx, dz] = gap(b, p.x, p.z);
351
492
  const d2 = dx * dx + dz * dz;
352
493
  if (d2 >= reach) continue;
353
- const d = Math.sqrt(d2);
354
- if (r - d <= best || !shut(lvl, e, p.y, h, step)) continue;
355
- best = r - d;
356
- const n = d || Math.hypot(ex, ez);
357
- px = (d ? dx : -ez) / n * best;
358
- pz = (d ? dz : ex) / n * best;
494
+ if (d2) {
495
+ const d = sqrt(d2);
496
+ if (.4 - d > best) {
497
+ best = PLAYER_R - d;
498
+ px = dx / d * best;
499
+ pz = dz / d * best;
500
+ }
501
+ continue;
502
+ }
503
+ const [m, nx, nz] = [
504
+ [
505
+ p.x - b.min[0],
506
+ -1,
507
+ 0
508
+ ],
509
+ [
510
+ b.max[0] - p.x,
511
+ 1,
512
+ 0
513
+ ],
514
+ [
515
+ p.z - b.min[2],
516
+ 0,
517
+ -1
518
+ ],
519
+ [
520
+ b.max[2] - p.z,
521
+ 0,
522
+ 1
523
+ ]
524
+ ].reduce((a, c) => c[0] < a[0] ? c : a);
525
+ if (m + .4 > best) {
526
+ best = m + PLAYER_R;
527
+ [px, pz] = [nx * best, nz * best];
528
+ }
359
529
  }
360
- if (!best) return;
530
+ if (!best) break;
361
531
  p.x += px;
362
532
  p.z += pz;
533
+ tx += px;
534
+ tz += pz;
363
535
  }
536
+ return [tx, tz];
364
537
  };
365
538
  /**
366
- * The floor and the ceiling under the player: the highest floor and the lowest ceiling of the
367
- * sector holding the axis and of every sector across a portal the circle overlaps and could walk
368
- * through (a shut edge is a wall: the circle never counts what lies behind it).
539
+ * The floor and the ceiling under the player: the highest top the player can stand on (at most
540
+ * STEP_H above the feet) and the lowest bottom above the head, among the boxes the circle overlaps.
541
+ * Every top between the feet and the fall is a candidate and so is every bottom above the head,
542
+ * however far: a fall of any speed stops on the first top under the circle, and a rise (at most
543
+ * DOUBLE_JUMP_SPEED / TICK_HZ = 0.12 m a step) cannot pass a bottom. Only a corner clipped by under a
544
+ * millimetre of overlap at one end of a step and none at the other could slip past.
369
545
  */
370
- var span = (lvl, x, z, y, r, h, step) => {
371
- const { map, edges } = lvl;
372
- const s = Math.max(sectorAt(lvl, x, z), 0);
373
- let { floor, ceil } = map.sectors[s];
374
- const reach = (r - EPS) ** 2;
375
- for (const e of edges) {
376
- if (e.front !== s || e.back < 0) continue;
377
- const ex = e.bx - e.ax;
378
- const ez = e.bz - e.az;
379
- const rx = x - e.ax;
380
- const rz = z - e.az;
381
- const t = clamp((rx * ex + rz * ez) / (ex * ex + ez * ez), 0, 1);
382
- if ((rx - t * ex) ** 2 + (rz - t * ez) ** 2 < reach && !shut(lvl, e, y, h, step)) {
383
- const k = map.sectors[e.back];
384
- floor = Math.max(floor, k.floor);
385
- ceil = Math.min(ceil, k.ceil);
386
- }
546
+ var span = (p, w, h) => {
547
+ const { brushes } = w.def;
548
+ const reach = (PLAYER_R - EPS) ** 2;
549
+ let floor = -Infinity;
550
+ let ceil = Infinity;
551
+ for (const i of near(w, p.x, p.z, PLAYER_R)) {
552
+ const b = brushes[i];
553
+ const [dx, dz] = gap(b, p.x, p.z);
554
+ if (dx * dx + dz * dz >= reach) continue;
555
+ if (b.max[1] <= p.y + .5 + SKIN) floor = Math.max(floor, b.max[1]);
556
+ else if (b.min[1] >= p.y + h - SKIN) ceil = Math.min(ceil, b.min[1]);
387
557
  }
388
558
  return [floor, ceil];
389
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
+ };
390
593
  /**
391
- * Moves the feet by (dx, dz) for 1 / TICK_HZ s, then a jump from the ground, GRAVITY and the
392
- * collisions of the cylinder (PLAYER_R, PLAYER_H) with the sectors of the level: it slides along
393
- * shut edges (see shut and push), stands on the highest floor under the circle (a step up to
394
- * STEP_H is climbed at once, a higher one needs a jump, a ledge is a fall), and bumps its head on
395
- * the lowest ceiling, in place.
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.
396
597
  */
397
- var walk = (p, dx, dz, jump, lvl) => {
398
- p.x += dx;
399
- p.z += dz;
400
- push(p, lvl, PLAYER_R, PLAYER_H, STEP_H);
401
- const [floor, ceil] = span(lvl, p.x, p.z, p.y, PLAYER_R, PLAYER_H, STEP_H);
402
- if (p.ground && jump) p.vy = 7;
403
- const vy = p.vy - 20 * DT;
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
+ };
717
+ /**
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).
722
+ */
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;
404
738
  p.y += (p.vy + vy) / 2 * DT;
405
739
  p.vy = vy;
406
- p.ground = false;
407
- if (p.y + 1.8 > ceil) {
408
- p.y = ceil - PLAYER_H;
740
+ if (p.y + h > ceil) {
741
+ p.y = ceil - h;
409
742
  p.vy = Math.min(p.vy, 0);
410
743
  }
411
744
  if (p.vy <= 0 ? p.y <= floor + SKIN : p.y < floor) {
412
745
  p.y = floor;
413
746
  if (p.vy <= 0) {
747
+ const hit = -p.vy;
414
748
  p.vy = 0;
415
- p.ground = true;
749
+ return hit;
416
750
  }
751
+ } else if (snap && p.vy <= 0 && floor >= y0 - .5) {
752
+ p.y = floor;
753
+ p.vy = 0;
754
+ return 0;
417
755
  }
418
- p.x = f32(p.x);
419
- p.y = f32(p.y);
420
- p.z = f32(p.z);
421
- p.vy = f32(p.vy);
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);
787
+ }
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));
422
966
  };
423
967
  /**
424
968
  * Applies one input for 1 / TICK_HZ s, in place: yaw and pitch from the input (pitch clamped to
425
- * 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.
426
971
  */
427
- var move = (p, i, lvl) => {
972
+ var move = (p, i, w) => {
428
973
  p.yaw = norm(i.yaw);
429
974
  p.pitch = Math.max(-MAX_PITCH, Math.min(MAX_PITCH, i.pitch));
430
975
  const f = (i.keys & KEY.FORWARD ? 1 : 0) - (i.keys & KEY.BACK ? 1 : 0);
431
976
  const s = (i.keys & KEY.RIGHT ? 1 : 0) - (i.keys & KEY.LEFT ? 1 : 0);
432
- const d = 6 * DT / (Math.hypot(f, s) || 1);
977
+ const n = len(f, s) || 1;
433
978
  const [fx, fz] = forward(p.yaw);
434
- walk(p, (fx * f - fz * s) * d, (fz * f + fx * s) * d, (i.keys & KEY.JUMP) !== 0, lvl);
435
- };
436
- /** A player standing on the floor at spawn point n of the map (modulo the count). */
437
- var spawn = (id, lvl, n) => {
438
- const { spawns, sectors } = lvl.map;
439
- const [x, z, yaw] = spawns[(n % spawns.length + spawns.length) % spawns.length];
440
- const y = sectors[Math.max(sectorAt(lvl, x, z), 0)].floor;
441
- return {
442
- id,
443
- x: f32(x),
444
- y: f32(y),
445
- z: f32(z),
446
- vy: 0,
447
- yaw: norm(yaw),
448
- pitch: 0,
449
- ground: true
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
450
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);
451
1020
  };
452
1021
  var TURN = 65536 / (2 * Math.PI);
453
1022
  /** Radians to 65536ths of a turn; the mask also folds negative and ≥ 2π angles into one turn. */
@@ -471,21 +1040,27 @@ var decodeInput = (buf) => {
471
1040
  };
472
1041
  };
473
1042
  var encodeSnapshot = (s) => {
474
- 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));
475
1044
  v.setUint8(0, 2);
476
1045
  v.setUint16(1, s.tick & 65535);
477
1046
  v.setUint8(3, s.players.length);
478
1047
  s.players.forEach((p, i) => {
479
- const o = 4 + i * 24;
1048
+ const o = 4 + i * 39;
480
1049
  v.setUint8(o, p.id);
481
1050
  v.setFloat32(o + 1, p.x);
482
1051
  v.setFloat32(o + 5, p.y);
483
1052
  v.setFloat32(o + 9, p.z);
484
- v.setFloat32(o + 13, p.vy);
485
- v.setUint16(o + 17, toTurn(p.yaw));
486
- v.setInt16(o + 19, toPitch(p.pitch));
487
- v.setUint16(o + 21, p.ack & 65535);
488
- 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));
489
1064
  });
490
1065
  return v.buffer;
491
1066
  };
@@ -552,18 +1127,18 @@ var Room = class {
552
1127
  ticks = 0;
553
1128
  /** The players as each snapshot of the last MAX_REWIND ticks sent them. */
554
1129
  history = [];
555
- lvl;
1130
+ world;
556
1131
  halt;
557
1132
  rules;
558
1133
  /** Round-robin cursor over the spawn points, used once they are all taken. */
559
1134
  rr = 0;
560
1135
  /** Missing rules fall back to RULES. */
561
- constructor(rules = {}, map = ROOF) {
1136
+ constructor(rules = {}, map = ROOFTOPS) {
562
1137
  this.rules = {
563
1138
  ...RULES,
564
1139
  ...rules
565
1140
  };
566
- this.lvl = compile(map);
1141
+ this.world = build(map);
567
1142
  }
568
1143
  /** Number of joined players. */
569
1144
  get size() {
@@ -584,7 +1159,7 @@ var Room = class {
584
1159
  const input = p && decodeInput(data);
585
1160
  if (input && p.bank > 0) {
586
1161
  p.bank--;
587
- move(p.state, input, this.lvl);
1162
+ move(p.state, input, this.world);
588
1163
  p.ack = input.seq;
589
1164
  }
590
1165
  }
@@ -673,11 +1248,11 @@ var Room = class {
673
1248
  }
674
1249
  /** The first spawn point with none of `others` within SPAWN_CLEAR, else the next one round-robin. */
675
1250
  place(id, others) {
676
- const n = this.lvl.map.spawns.findIndex((_, k) => {
677
- const { x, z } = spawn(id, this.lvl, k);
1251
+ const n = this.world.def.spawns.findIndex((_, k) => {
1252
+ const { x, z } = spawn(id, this.world, k);
678
1253
  return !others.some((o) => Math.hypot(o.x - x, o.z - z) < SPAWN_CLEAR);
679
1254
  });
680
- return spawn(id, this.lvl, n < 0 ? this.rr++ : n);
1255
+ return spawn(id, this.world, n < 0 ? this.rr++ : n);
681
1256
  }
682
1257
  players() {
683
1258
  return [...this.peers.values()].filter((p) => p !== null);