@korial29/tiny-fps 0.11.0 → 0.13.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
@@ -3,29 +3,8 @@ import { WebSocketServer } from "ws";
3
3
  /** Player cylinder. */
4
4
  var PLAYER_R = .4;
5
5
  var PLAYER_H = 1.8;
6
- /** Camera height above the feet. */
7
- var EYE = 1.6;
8
6
  /** Pitch is clamped to ±MAX_PITCH radians, just short of straight up or down. */
9
7
  var MAX_PITCH = 1.55;
10
- /** A floor at most this much higher is walked onto; a higher one needs a jump. */
11
- var STEP_H = .5;
12
- /** Speed factor while the trigger is held. */
13
- var FIRE_SLOW = .5;
14
- /** Largest angle, in radians, between a shot and the view direction. */
15
- var SPREAD = .02;
16
- var WALKER_R = .4;
17
- var WALKER_H = 1.8;
18
- var WALKER_SPEED = 2.5;
19
- var FLYER_R = .45;
20
- var FLYER_H = .9;
21
- /** A flyer's underside floats this high above the floor below it, ceilings allowing. */
22
- var FLYER_HOVER = 1.2;
23
- var FLYER_SPEED = 3.5;
24
- var SIGHT_COS = Math.cos(Math.PI / 6);
25
- var BULLET_R = .15;
26
- /** Hard mode (second run on): monster speed factor, and the share of SHOOT_EVERY and MELEE_EVERY. */
27
- var HARD_SPEED = 1.3;
28
- var HARD_RATE = .6;
29
8
  var RULES = { maxPlayers: 16 };
30
9
  // src/shared/types.ts
31
10
  /** Bits of Input.keys. Keys are read by KeyboardEvent.code, so WASD is ZQSD on AZERTY. */
@@ -34,1565 +13,313 @@ var KEY = {
34
13
  BACK: 2,
35
14
  LEFT: 4,
36
15
  RIGHT: 8,
37
- JUMP: 16,
38
- /** Left mouse button: the chaingun fires every FIRE_EVERY steps while held. */
39
- FIRE: 32
40
- };
41
- /** Monster kinds. */
42
- var MONSTER = {
43
- /** Walks the floors like a player, shoots bullets from afar and hits up close. */
44
- WALKER: 0,
45
- /** Hovers above the floor, flies over steps and pits, hits up close. */
46
- FLYER: 1
47
- };
48
- /** What a GameEvent reports. */
49
- var EVENT = {
50
- /** Monster `id` died, (x, y, z) the middle of its body: gibs and an explosion. */
51
- DIE: 0,
52
- /** Player `id` was hit by a bullet or a blow at (x, y, z). Harmless until health (step 4). */
53
- HURT: 1,
54
- /** A bullet burst on the level at (x, y, z); `id` is the bullet. */
55
- BOOM: 2,
56
- /** Monster `id` swung at its target from (x, y, z), hit or miss. */
57
- SWING: 3
58
- };
59
- /** Pickup kinds. A key is shared by the team: once anyone takes it, its gates open for all. */
60
- var ITEM = {
61
- /** Gives HEAL health, up to HEALTH. Left in the level by every DROP_EVERY-th kill. */
62
- HEALTH: 0,
63
- KEY_RED: 1,
64
- KEY_BLUE: 2,
65
- KEY_YELLOW: 3
66
- };
67
- /** Where a level stands. */
68
- var PHASE = {
69
- /** Find the goal: the exit sector is barred. */
70
- HUNT: 0,
71
- /** The goal was touched: the exit sector is open; the level is won when every living player is in it. */
72
- RETURN: 1
73
- };
74
- // src/shared/level.ts
75
- /** Joins every edge a→b with the sector that has b→a, if any: that edge is a portal. */
76
- var compile = (map) => {
77
- const key = (ax, az, bx, bz) => `${ax},${az},${bx},${bz}`;
78
- const owner = /* @__PURE__ */ new Map();
79
- map.sectors.forEach(({ pts }, s) => pts.forEach(([ax, az], i) => owner.set(key(ax, az, ...pts[(i + 1) % pts.length]), s)));
80
- const lvl = {
81
- map,
82
- edges: map.sectors.flatMap(({ pts }, front) => pts.map(([ax, az], i) => {
83
- const [bx, bz] = pts[(i + 1) % pts.length];
84
- return {
85
- ax,
86
- az,
87
- bx,
88
- bz,
89
- front,
90
- back: owner.get(key(bx, bz, ax, az)) ?? -1
91
- };
92
- })),
93
- barred: /* @__PURE__ */ new Set()
94
- };
95
- bar(lvl, 0, PHASE.HUNT);
96
- return lvl;
97
- };
98
- /** Bars the gates whose key is not in `keys` (bits 1 << ITEM.KEY_*), and the exit before RETURN. */
99
- var bar = ({ map, barred }, keys, phase) => {
100
- barred.clear();
101
- map.gates?.forEach((g) => keys & 1 << g.key || barred.add(g.sector));
102
- if (map.exit !== void 0 && phase === PHASE.HUNT) barred.add(map.exit);
103
- };
104
- /** Whether (x, z) is inside the sector, its edges included. */
105
- var inside = ({ pts }, x, z) => pts.every(([ax, az], i) => {
106
- const [bx, bz] = pts[(i + 1) % pts.length];
107
- return (bx - ax) * (z - az) - (bz - az) * (x - ax) >= 0;
16
+ JUMP: 16
17
+ };
18
+ /** Procedural textures, generated by the client. A brush names one for its top and one for its sides. */
19
+ var TEX = {
20
+ BRICK: 0,
21
+ TILE: 1,
22
+ LAVA: 2,
23
+ STONE: 3,
24
+ /** Riveted metal plates. */
25
+ METAL: 4,
26
+ /** Planks, the grain running along u. */
27
+ WOOD: 5,
28
+ /** Stone blocks with moss in the joints. */
29
+ MOSS: 6,
30
+ /** Metal floor grating over darkness. */
31
+ GRATE: 7
32
+ };
33
+ // src/shared/map.ts
34
+ /** The bottom of every building: far below the kill floor, so nothing shows beneath a roof. */
35
+ var BASE = -30;
36
+ /** A building: the footprint x0..x1 by z0..z1 from the street up to a flat roof at `top`. */
37
+ var roof = (x0, z0, x1, z1, top) => ({
38
+ min: [
39
+ x0,
40
+ BASE,
41
+ z0
42
+ ],
43
+ max: [
44
+ x1,
45
+ top,
46
+ z1
47
+ ],
48
+ top: TEX.TILE,
49
+ side: TEX.BRICK
108
50
  });
109
- /** Index of the first sector holding (x, z), -1 outside the map. */
110
- var sectorAt = ({ map }, x, z) => map.sectors.findIndex((s) => inside(s, x, z));
111
- /**
112
- * Whether a wall, a step or a ceiling hides b from a (both inside the map). Between two edge
113
- * crossings the segment stays in one sector, and each crossing is checked against the opening of
114
- * its portal, so floors and ceilings need no test of their own.
115
- */
116
- var blocked = ({ map, edges }, [ax, ay, az], [bx, by, bz]) => {
117
- const [dx, dz] = [bx - ax, bz - az];
118
- return edges.some((e) => {
119
- const [ex, ez] = [e.bx - e.ax, e.bz - e.az];
120
- const den = dx * ez - dz * ex;
121
- if (!den) return false;
122
- const t = ((e.ax - ax) * ez - (e.az - az) * ex) / den;
123
- const u = ((e.ax - ax) * dz - (e.az - az) * dx) / den;
124
- if (t <= 0 || t >= 1 || u < 0 || u > 1) return false;
125
- if (e.back < 0) return true;
126
- const [f, k] = [map.sectors[e.front], map.sectors[e.back]];
127
- const y = ay + t * (by - ay);
128
- return y < Math.max(f.floor, k.floor) || y > Math.min(f.ceil, k.ceil);
129
- });
130
- };
131
- // src/shared/levels.ts
132
- /** The rooms with the colour of their ambient light (white when untinted). */
133
- var tinted = (t, ...rooms) => rooms.map(([x0, z0, x1, z1, f, c, tex, light, key]) => [
134
- x0,
135
- z0,
136
- x1,
137
- z1,
138
- f,
139
- c,
140
- tex,
141
- light,
142
- key,
143
- t
144
- ]);
145
- /** n slices of a box along x (0) or z (1), the first at floor f and each one rising by up. */
146
- var stairs = (axis, [x0, z0, x1, z1], f, up, n, ceil, tex, light) => Array.from({ length: n }, (_, i) => {
147
- const [lo, hi] = axis ? [z0, z1] : [x0, x1];
148
- const [a, b, h] = [
149
- lo + i * (hi - lo) / n,
150
- lo + (i + 1) * (hi - lo) / n,
151
- +(f + i * up).toFixed(1)
152
- ];
153
- return axis ? [
51
+ /** Any box, corners (x0, y0, z0) and (x1, y1, z1). */
52
+ var box = ([x0, y0, z0], [x1, y1, z1], top, side = top, tint) => ({
53
+ min: [
154
54
  x0,
155
- a,
55
+ y0,
56
+ z0
57
+ ],
58
+ max: [
156
59
  x1,
157
- b,
158
- h,
159
- ceil,
160
- tex,
161
- light
162
- ] : [
163
- a,
164
- z0,
165
- b,
166
- z1,
167
- h,
168
- ceil,
169
- tex,
170
- light
171
- ];
60
+ y1,
61
+ z1
62
+ ],
63
+ top,
64
+ side,
65
+ tint
172
66
  });
173
- var [H, R, B, Y] = [
174
- ITEM.HEALTH,
175
- ITEM.KEY_RED,
176
- ITEM.KEY_BLUE,
177
- ITEM.KEY_YELLOW
178
- ];
179
- var QUARTER = Math.PI / 4;
180
- var WOOD = [
67
+ /** 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. */
68
+ var stair = (z, y) => [
181
69
  1,
182
- .82,
183
- .6
184
- ];
185
- var STEEL = [
186
- .6,
187
- .75,
188
- 1
189
- ];
70
+ 2,
71
+ 3,
72
+ 4,
73
+ 5,
74
+ 6
75
+ ].map((k) => box([
76
+ 12,
77
+ y,
78
+ z - k
79
+ ], [
80
+ 14,
81
+ y + .4 * k,
82
+ z - k + 1
83
+ ], TEX.STONE, TEX.STONE));
190
84
  /**
191
- * One level from its name, its rooms (the first is the start and the exit), the spawn grid (x, z of its
192
- * corner, facing), walkers and flyers (x, z, facing in eighth turns: 0 north, 2 west, 4 south, 6
193
- * east), items (x, z, kind) and the goal.
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.
88
+ *
89
+ * - A: the start roof (10 m), with the spawns. B: a roof at 10 m, across a 2.5 m gap from A.
90
+ * - On B, a platform floating at 12.2 to 12.6 m (an overhang to walk under, a bump for a jump) and
91
+ * 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
+ * is a 2.4 m ledge to grab. Two poles stand by the platform.
93
+ * - 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
+ * 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
+ * - 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.
194
97
  */
195
- var level = (name, rooms, [sx, sz, sd], walkers, flyers, items, goal, fog) => {
196
- const boxes = rooms.map(([x0, z0, x1, z1]) => [
197
- [x0, z0],
198
- [x1, z0],
199
- [x1, z1],
200
- [x0, z1]
201
- ]);
202
- const corners = [...new Map(boxes.flat().map((p) => [`${p}`, p])).values()];
203
- return {
204
- name,
205
- sectors: rooms.map(([, , , , floor, ceil, t = "103", light, , tint], s) => ({
206
- pts: boxes[s].flatMap((a, i, p) => {
207
- const b = p[(i + 1) % 4];
208
- const along = (c) => (c[0] - a[0]) * (b[0] - a[0]) + (c[1] - a[1]) * (b[1] - a[1]);
209
- return [a, ...corners.filter((c) => (b[0] - a[0]) * (c[1] - a[1]) === (b[1] - a[1]) * (c[0] - a[0]) && along(c) > 0 && along(c) < (b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2).sort((c, d) => along(c) - along(d))];
210
- }),
211
- floor,
212
- ceil,
213
- ft: +t[0],
214
- wt: +t[1],
215
- ct: +t[2],
216
- light,
217
- tint
218
- })),
219
- spawns: Array.from({ length: 16 }, (_, i) => [
220
- sx + i % 4 * 2,
221
- sz + (i >> 2) * 2,
222
- sd * QUARTER
223
- ]),
224
- monsters: [...walkers.map(([x, z, d]) => [
225
- x,
226
- z,
227
- d * QUARTER,
228
- MONSTER.WALKER
229
- ]), ...flyers.map(([x, z, d]) => [
230
- x,
231
- z,
232
- d * QUARTER,
233
- MONSTER.FLYER
234
- ])],
235
- items,
236
- gates: rooms.flatMap(([, , , , , , , , key], sector) => key ? [{
237
- sector,
238
- key
239
- }] : []),
240
- goal,
241
- exit: 0,
242
- fog
243
- };
244
- };
245
- var LEVELS = [
246
- level("Warm-up", [
247
- [
248
- 0,
249
- 0,
250
- 10,
251
- 10,
252
- 0,
253
- 4,
254
- "103"
255
- ],
256
- [
257
- 10,
258
- 4,
259
- 22,
260
- 6,
261
- 0,
262
- 2.8,
263
- "333",
264
- .6
265
- ],
266
- ...tinted([
267
- 1,
268
- .85,
269
- .65
270
- ], [
271
- 22,
272
- 0,
273
- 38,
274
- 14,
275
- 0,
276
- 6,
277
- "553"
278
- ], ...stairs(0, [
279
- 38,
280
- 4,
281
- 42,
282
- 10
283
- ], .4, .4, 4, 6, "553")),
284
- [
285
- 42,
286
- 2,
287
- 52,
288
- 12,
289
- 2,
290
- 6,
291
- "103",
292
- .9
293
- ],
294
- ...stairs(1, [
295
- 46,
296
- 12,
297
- 48,
298
- 22
299
- ], 1.6, -.4, 4, 4.8, "744", .7),
300
- [
301
- 38,
302
- 22,
303
- 56,
304
- 36,
305
- 0,
306
- 6,
307
- "113"
308
- ]
309
- ], [
310
- 2,
311
- 2,
312
- 6
313
- ], [
314
- [
315
- 28,
316
- 4,
317
- 3
318
- ],
319
- [
320
- 34,
321
- 10,
322
- 4
323
- ],
324
- [
325
- 48,
326
- 6,
327
- 4
328
- ],
329
- [
330
- 44,
331
- 30,
332
- 0
333
- ]
334
- ], [[
335
- 30,
336
- 8,
337
- 0
338
- ], [
339
- 52,
340
- 28,
341
- 2
342
- ]], [
343
- [
344
- 26,
345
- 12,
346
- H
347
- ],
348
- [
349
- 50,
98
+ var ROOFTOPS = {
99
+ name: "Rooftops",
100
+ brushes: [
101
+ roof(4, 56, 24, 76, 10),
102
+ roof(4, 36, 24, 53.5, 10),
103
+ roof(4, 20, 24, 36, 12.4),
104
+ roof(4, 0, 24, 12, 12.4),
105
+ roof(27, 20, 47, 50, 8),
106
+ roof(27, 60, 47, 76, 8),
107
+ box([
108
+ 9,
109
+ 12.2,
110
+ 39
111
+ ], [
112
+ 19,
113
+ 12.6,
114
+ 47
115
+ ], TEX.METAL, TEX.METAL),
116
+ ...stair(53, 10),
117
+ box([
118
+ 5,
350
119
  10,
351
- H
352
- ],
353
- [
354
- 40,
355
- 34,
356
- H
357
- ]
358
- ], [54, 34]),
359
- level("Foundry", [
360
- ...tinted([
361
- 1,
362
- .9,
363
- .8
120
+ 42
364
121
  ], [
365
- 0,
122
+ 5.5,
366
123
  14,
124
+ 42.5
125
+ ], TEX.METAL),
126
+ box([
127
+ 5,
367
128
  10,
368
- 26,
369
- 0,
370
- 4,
371
- "443"
372
- ], [
373
- 10,
374
- 22,
375
- 20,
376
- 24,
377
- 0,
378
- 2.8,
379
- "744",
380
- .6
381
- ]),
382
- ...tinted([
383
- 1,
384
- .5,
385
- .25
386
- ], [
387
- 24,
388
- 8,
389
- 28,
390
- 16,
391
- 0,
392
- 9,
393
- "744"
129
+ 50
394
130
  ], [
395
- 20,
396
- 16,
397
- 28,
398
- 30,
399
- 0,
400
- 9,
401
- "443"
402
- ], ...stairs(1, [
403
- 20,
404
- 8,
405
- 24,
406
- 16
407
- ], 2, -.4, 5, 9, "744"), [
408
- 28,
409
- 8,
410
- 42,
411
- 18,
412
- -1.6,
413
- 9,
414
- "233",
415
- .9
416
- ], ...stairs(1, [
417
- 28,
418
- 18,
419
- 42,
420
- 21
421
- ], -1.2, .4, 3, 9, "333"), [
422
- 28,
423
- 21,
424
- 50,
425
- 30,
426
- 0,
427
- 9,
428
- "744"
429
- ], [
430
- 42,
431
- 8,
432
- 50,
433
- 21,
434
- 0,
435
- 9,
436
- "744"
437
- ], [
438
- 20,
439
- 0,
440
- 50,
441
- 8,
442
- 2.4,
443
- 9,
444
- "744",
445
- .8
446
- ]),
447
- [
448
- 50,
131
+ 5.5,
449
132
  14,
450
- 51,
451
- 17,
452
- 0,
453
- 3.5,
454
- "444",
455
- 1,
456
- R
457
- ],
458
- ...tinted([
459
- .85,
460
- .65,
461
- .5
462
- ], [
463
- 51,
464
- 8,
465
- 59,
466
- 26,
467
- 0,
468
- 7,
469
- "443",
470
- .6
471
- ], [
472
- 59,
133
+ 50.5
134
+ ], TEX.METAL),
135
+ box([
473
136
  8,
474
- 67,
475
- 26,
476
- .4,
477
- 7,
478
- "143",
479
- .8
480
- ], [
481
- 57,
482
- 26,
483
- 59,
484
- 36,
485
- 0,
486
- 4.8,
487
- "744",
488
- .7
489
- ]),
490
- ...tinted([
491
- 1,
492
- .6,
493
- .45
494
- ], [
495
- 50,
496
- 36,
497
- 68,
498
- 50,
499
- 0,
500
- 7,
501
- "443"
502
- ])
503
- ], [
504
- 2,
505
- 16,
506
- 6
507
- ], [
508
- [
509
- 46,
510
- 14,
511
- 2
512
- ],
513
- [
514
- 36,
515
- 26,
516
- 0
517
- ],
518
- [
519
- 38,
520
- 4,
521
- 2
522
- ],
523
- [
524
- 56,
525
- 12,
526
- 4
527
- ],
528
- [
529
- 62,
530
- 20,
531
- 2
532
- ],
533
- [
534
- 60,
535
- 44,
536
- 0
537
- ]
538
- ], [
539
- [
540
- 35,
541
- 13,
542
- 0
543
- ],
544
- [
545
- 60,
546
- 14,
547
- 4
548
- ],
549
- [
550
- 58,
551
- 42,
552
- 6
553
- ]
554
- ], [
555
- [
556
- 24,
557
- 26,
558
- H
559
- ],
560
- [
561
- 24,
562
- 4,
563
- H
564
- ],
565
- [
566
- 64,
567
- 10,
568
- H
569
- ],
570
- [
571
- 52,
572
- 46,
573
- H
574
- ],
575
- [
576
- 46,
577
- 4,
578
- R
579
- ]
580
- ], [66, 48], [
581
- .07,
582
- .05,
583
- .04,
584
- .026
585
- ]),
586
- level("Cistern", [
587
- ...tinted([
588
- .45,
589
- .9,
590
- 1
591
- ], [
592
- 0,
593
- 20,
594
- 10,
595
- 32,
596
- 0,
597
- 4,
598
- "363"
599
- ], [
600
- 10,
601
- 26,
602
- 20,
603
- 28,
604
- 0,
605
- 2.8,
606
- "363",
607
- .6
608
- ], [
609
- 20,
610
- 14,
611
- 56,
612
- 26,
613
- 0,
614
- 11,
615
- "363",
616
- .9
617
- ], [
618
- 20,
619
- 26,
620
- 34,
621
- 32,
622
- 0,
623
- 11,
624
- "363",
625
- .9
626
- ]),
627
- ...tinted([
628
- .9,
629
- .6,
630
- .45
631
- ], [
632
- 34,
633
- 26,
634
- 42,
635
- 32,
636
- -.4,
637
- 11,
638
- "263"
639
- ]),
640
- ...tinted([
641
- .45,
642
- .9,
643
- 1
644
- ], [
645
- 42,
646
- 26,
647
- 56,
648
- 32,
649
- 0,
650
- 11,
651
- "363",
652
- .9
137
+ 12.4,
138
+ 24
653
139
  ], [
654
140
  20,
655
- 32,
656
- 56,
657
- 44,
658
- 0,
659
- 11,
660
- "363",
661
- .9
662
- ], [
141
+ 12.85,
142
+ 24.45
143
+ ], TEX.METAL),
144
+ box([
663
145
  36,
664
- 44,
665
- 38,
666
- 54,
667
- 0,
668
- 1.9,
669
- "000",
670
- .35
671
- ], [
672
- 30,
673
- 54,
674
- 46,
675
- 68,
676
- 0,
677
- 5,
678
- "003",
679
- .7
680
- ]),
681
- [
682
- 56,
683
- 28,
684
- 57,
685
- 31,
686
- 0,
687
- 4,
688
- "444",
689
- 1,
690
- R
691
- ],
692
- ...tinted([
693
- .55,
694
- .85,
695
- 1
696
- ], ...stairs(0, [
697
- 57,
698
- 26,
699
- 65,
700
- 32
701
- ], .4, .4, 8, 10, "333", .8)),
702
- ...tinted([
703
- .7,
704
- 1,
705
- .85
146
+ 8,
147
+ 22
706
148
  ], [
707
- 65,
708
- 22,
709
- 81,
710
- 36,
711
- 3.6,
712
- 11,
713
- "563"
714
- ]),
715
- [
716
- 71,
717
- 36,
718
- 74,
719
149
  37,
720
- 3.6,
721
- 7,
722
- "444",
150
+ 14.5,
151
+ 48
152
+ ], TEX.STONE, TEX.STONE, [
723
153
  1,
724
- B
725
- ],
726
- ...tinted([
727
- .45,
728
- .9,
729
- 1
730
- ], [
731
- 71,
732
- 37,
733
- 74,
734
- 48,
735
- 3.6,
736
- 6.4,
737
- "363",
738
- .6
154
+ .75,
155
+ .62
739
156
  ]),
740
- ...tinted([
741
- .9,
742
- .6,
743
- .45
744
- ], [
745
- 62,
746
- 48,
747
- 82,
748
- 64,
749
- 3.6,
750
- 10,
751
- "263"
752
- ])
753
- ], [
754
- 2,
755
- 22,
756
- 6
757
- ], [
758
- [
759
- 30,
760
- 18,
761
- 4
762
- ],
763
- [
764
- 50,
765
- 20,
766
- 4
767
- ],
768
- [
157
+ box([
769
158
  30,
770
- 40,
771
- 0
772
- ],
773
- [
774
- 50,
775
- 40,
776
- 0
777
- ],
778
- [
779
- 36,
780
- 60,
781
- 2
782
- ],
783
- [
784
- 42,
785
- 64,
786
- 0
787
- ],
788
- [
789
- 76,
790
- 28,
791
- 2
792
- ],
793
- [
794
- 70,
795
- 58,
796
- 0
797
- ]
798
- ], [
799
- [
800
- 26,
801
- 24,
802
- 0
803
- ],
804
- [
805
- 48,
806
- 22,
807
- 0
808
- ],
809
- [
810
- 36,
811
- 40,
812
- 2
813
- ],
814
- [
815
- 72,
816
- 56,
817
- 4
818
- ]
819
- ], [
820
- [
821
- 24,
822
- 40,
823
- H
824
- ],
825
- [
826
- 52,
827
- 42,
828
- H
829
- ],
830
- [
831
- 44,
832
- 57,
833
- H
834
- ],
835
- [
836
- 68,
837
- 34,
838
- H
839
- ],
840
- [
841
- 64,
842
- 62,
843
- H
844
- ],
845
- [
846
- 38,
847
- 66,
848
- R
849
- ],
850
- [
851
- 79,
852
- 24,
853
- B
854
- ]
855
- ], [80, 62], [
856
- .04,
857
- .11,
858
- .1,
859
- .034
860
- ]),
861
- level("Loop", [
862
- ...tinted(WOOD, [
863
- 0,
864
- 44,
865
- 10,
866
- 56,
867
- 0,
868
- 4,
869
- "153"
870
- ], [
871
- 10,
872
- 49,
873
- 20,
874
- 51,
875
- 0,
876
- 2.8,
877
- "553",
878
- .6
879
- ]),
880
- [
881
- 20,
882
- 30,
883
- 52,
884
- 40,
885
- 0,
886
- 9,
887
- "113"
888
- ],
889
- [
890
- 20,
891
- 48,
892
- 52,
893
- 58,
894
- 0,
895
- 9,
896
- "113",
897
- .9
898
- ],
899
- [
900
- 20,
901
- 40,
902
- 32,
903
- 48,
904
- 0,
905
- 9,
906
- "113"
907
- ],
908
- [
909
- 40,
910
- 40,
911
- 52,
912
- 48,
913
- 0,
914
- 9,
915
- "113"
916
- ],
917
- [
918
- 20,
919
- 22,
920
- 49,
921
- 30,
922
- 3.2,
923
- 9,
924
- "743",
925
- .8
926
- ],
927
- ...tinted(WOOD, [
928
- 34,
929
- 58,
930
- 36,
931
- 66,
932
- 0,
933
- 2.8,
934
- "553",
935
- .4
936
- ], [
937
- 26,
938
- 66,
939
- 46,
940
- 84,
941
- 0,
942
- 5,
943
- "353",
944
- .5
945
- ]),
946
- [
947
- 52,
948
- 42,
949
- 53,
950
- 45,
951
- 0,
952
- 4,
953
- "444",
954
- 1,
955
- R
956
- ],
957
- ...tinted(WOOD, [
958
- 53,
959
- 36,
960
- 64,
961
- 54,
962
- 0,
963
- 6,
964
- "153",
965
- .8
966
- ], [
967
- 64,
968
- 36,
969
- 70,
970
- 54,
971
- .4,
972
- 6,
973
- "253",
974
- .8
975
- ]),
976
- [
977
- 60,
978
- 35,
979
- 63,
980
- 36,
981
- 0,
982
- 4,
983
- "444",
984
- 1,
985
- B
986
- ],
987
- ...tinted(WOOD, [
988
- 57,
989
- 12,
990
- 77,
991
- 35,
992
- 0,
993
159
  8,
994
- "053",
995
- .9
996
- ]),
997
- [
998
- 56,
999
- 24,
1000
- 57,
1001
- 27,
1002
- 0,
1003
- 4,
1004
- "444",
1005
- 1,
1006
- Y
1007
- ],
1008
- ...stairs(0, [
1009
- 49,
1010
- 22,
1011
- 56,
1012
- 30
1013
- ], 2.8, -.4, 7, 9, "744", .8),
1014
- ...tinted(WOOD, [
1015
- 12,
1016
- 24,
1017
- 20,
1018
- 28,
1019
- 3.2,
1020
- 6,
1021
- "744",
1022
- .6
160
+ 28
1023
161
  ], [
1024
- 0,
1025
- 10,
1026
- 12,
1027
- 34,
1028
- 3.2,
1029
- 9,
1030
- "553"
1031
- ])
1032
- ], [
1033
- 2,
1034
- 46,
1035
- 6
1036
- ], [
1037
- [
1038
- 30,
1039
- 36,
1040
- 4
1041
- ],
1042
- [
1043
- 44,
1044
- 36,
1045
- 2
1046
- ],
1047
- [
1048
- 40,
1049
- 52,
1050
- 0
1051
- ],
1052
- [
1053
- 40,
1054
- 26,
1055
- 4
1056
- ],
1057
- [
1058
- 30,
1059
- 74,
1060
- 6
1061
- ],
1062
- [
1063
- 42,
1064
- 78,
1065
- 2
1066
- ],
1067
- [
1068
- 56,
1069
- 40,
1070
- 6
1071
- ],
1072
- [
1073
- 64,
1074
- 50,
1075
- 2
1076
- ],
1077
- [
1078
- 62,
1079
- 18,
1080
- 4
1081
- ],
1082
- [
1083
- 72,
1084
- 28,
1085
- 2
1086
- ],
1087
- [
1088
- 6,
1089
- 22,
1090
- 0
1091
- ]
1092
- ], [
1093
- [
1094
- 28,
1095
- 48,
1096
- 0
1097
- ],
1098
- [
1099
- 46,
1100
- 44,
1101
- 4
1102
- ],
1103
- [
1104
- 36,
1105
- 72,
1106
- 0
1107
- ],
1108
- [
1109
- 60,
1110
- 45,
1111
- 6
1112
- ],
1113
- [
1114
- 68,
1115
- 22,
1116
- 4
1117
- ]
1118
- ], [
1119
- [
1120
- 24,
1121
- 54,
1122
- H
1123
- ],
1124
- [
1125
- 24,
1126
- 26,
1127
- H
1128
- ],
1129
- [
1130
- 44,
1131
- 80,
1132
- H
1133
- ],
1134
- [
1135
- 68,
1136
- 38,
1137
- H
1138
- ],
1139
- [
1140
- 74,
1141
- 14,
1142
- H
1143
- ],
1144
- [
1145
- 36,
1146
- 82,
1147
- R
1148
- ],
1149
- [
1150
- 68,
1151
- 52,
1152
- B
1153
- ],
1154
- [
1155
- 74,
1156
- 32,
1157
- Y
1158
- ]
1159
- ], [6, 13], [
1160
- .06,
1161
- .045,
1162
- .04,
1163
- .02
1164
- ]),
1165
- level("Citadel", [
1166
- ...tinted(STEEL, [
1167
- 0,
1168
- 60,
1169
- 10,
1170
- 72,
1171
- 0,
1172
- 4,
1173
- "303"
1174
- ], [
1175
- 10,
1176
- 65,
1177
- 24,
1178
- 67,
1179
- 0,
1180
- 2.8,
1181
- "363",
1182
- .6
1183
- ], [
1184
- 24,
1185
- 48,
1186
- 56,
1187
- 84,
1188
- 0,
1189
- 12,
1190
- "363"
1191
- ], [
1192
- 27,
1193
- 40,
1194
- 31,
1195
- 48,
1196
- 0,
1197
- 3.4,
1198
- "363",
1199
- .4
1200
- ]),
1201
- ...tinted([
1202
- .85,
1203
- .7,
1204
- .65
1205
- ], [
1206
- 24,
1207
- 16,
1208
- 34,
1209
- 40,
1210
- 0,
1211
- 12,
1212
- "443",
1213
- .9
1214
- ], ...stairs(0, [
1215
- 34,
1216
- 16,
1217
- 40,
1218
- 26
1219
- ], -.4, -.4, 3, 12, "243"), [
1220
- 34,
1221
- 26,
1222
- 40,
1223
- 30,
1224
- 0,
1225
- 12,
1226
- "744"
1227
- ], ...stairs(0, [
1228
- 34,
1229
- 30,
1230
- 40,
1231
- 40
1232
- ], -.4, -.4, 3, 12, "243"), [
1233
- 40,
1234
- 16,
1235
- 60,
1236
- 40,
1237
- 0,
1238
- 12,
1239
- "443",
1240
- .9
1241
- ]),
1242
- [
1243
- 60,
1244
- 27,
1245
- 61,
1246
- 30,
1247
- 0,
1248
- 4,
1249
- "444",
1250
- 1,
1251
- Y
1252
- ],
1253
- ...tinted([
1254
- .55,
1255
- .3,
1256
- 1
1257
- ], [
1258
- 61,
1259
- 10,
1260
- 101,
1261
- 16,
1262
- 0,
1263
- 14,
1264
- "443",
1265
- 1
1266
- ], [
1267
- 61,
1268
- 40,
1269
- 101,
1270
- 46,
1271
- 0,
1272
- 14,
1273
- "443",
1274
- 1
1275
- ], [
1276
- 61,
1277
- 16,
1278
- 69,
1279
- 40,
1280
- 0,
1281
- 14,
1282
- "443",
1283
- 1
1284
- ], [
1285
- 93,
1286
- 16,
1287
- 101,
1288
- 40,
1289
- 0,
1290
- 14,
1291
- "443",
1292
- 1
1293
- ], [
1294
- 69,
1295
- 16,
1296
- 93,
1297
- 20,
1298
- .4,
1299
- 14,
1300
- "243",
1301
- 1
1302
- ], [
1303
- 69,
1304
- 36,
1305
- 93,
1306
- 40,
1307
- .4,
1308
- 14,
1309
- "243",
1310
- 1
1311
- ], [
1312
- 69,
1313
- 20,
1314
- 73,
1315
- 36,
1316
- .4,
1317
- 14,
1318
- "243",
1319
- 1
1320
- ], [
1321
- 89,
1322
- 20,
1323
- 93,
1324
- 36,
1325
- .4,
1326
- 14,
1327
- "243",
1328
- 1
1329
- ], [
1330
- 73,
1331
- 20,
1332
- 89,
1333
- 36,
1334
- .8,
1335
- 14,
1336
- "744",
1337
- 1
1338
- ]),
1339
- [
1340
- 101,
1341
- 26,
1342
- 111,
1343
- 28,
1344
- 0,
1345
- 3.2,
1346
- "744",
1347
- .6
1348
- ],
1349
- [
1350
- 111,
1351
- 16,
1352
- 131,
1353
- 40,
1354
- 0,
1355
- 8,
1356
- "163"
1357
- ],
1358
- [
1359
- 56,
1360
- 66,
1361
- 57,
1362
- 69,
1363
- 0,
1364
- 4,
1365
- "444",
1366
- 1,
1367
- R
1368
- ],
1369
- ...tinted([
1370
- 1,
1371
- .5,
1372
- .45
162
+ 34,
163
+ 10,
164
+ 32
165
+ ], TEX.STONE, TEX.BRICK),
166
+ box([
167
+ 40,
168
+ 8,
169
+ 30
1373
170
  ], [
1374
- 57,
1375
- 56,
1376
- 75,
1377
- 80,
1378
- 0,
1379
- 12,
1380
- "443",
1381
- .7
1382
- ], ...stairs(0, [
1383
- 75,
1384
- 60,
1385
- 85,
1386
- 72
1387
- ], .4, .4, 5, 12, "744", .8), [
1388
- 85,
1389
- 52,
1390
- 101,
1391
- 80,
1392
- 2.4,
171
+ 44,
172
+ 10.2,
173
+ 34
174
+ ], TEX.STONE, TEX.BRICK),
175
+ box([
176
+ 28,
177
+ 9.2,
178
+ 44
179
+ ], [
180
+ 35,
1393
181
  12,
1394
- "543"
182
+ 46
183
+ ], TEX.METAL, TEX.GRATE, [
184
+ .8,
185
+ .9,
186
+ 1
1395
187
  ]),
1396
- [
1397
- 38,
1398
- 84,
1399
- 42,
1400
- 85,
1401
- 0,
1402
- 4,
1403
- "444",
1404
- 1,
1405
- B
1406
- ],
1407
- ...tinted([
1408
- .6,
1409
- .85,
1410
- .85
188
+ box([
189
+ 41.5,
190
+ 7.6,
191
+ 50
1411
192
  ], [
1412
- 38,
1413
- 85,
1414
193
  42,
1415
- 95,
1416
- 0,
1417
- 1.9,
1418
- "363",
1419
- .35
194
+ 8,
195
+ 60
196
+ ], TEX.METAL),
197
+ box([
198
+ 30,
199
+ 8,
200
+ 64
1420
201
  ], [
1421
- 28,
1422
- 95,
1423
- 56,
1424
- 115,
1425
- 0,
1426
- 6,
1427
- "663",
1428
- .6
1429
- ])
1430
- ], [
1431
- 2,
1432
- 62,
1433
- 6
1434
- ], [
1435
- [
1436
- 34,
1437
- 56,
1438
- 4
1439
- ],
1440
- [
1441
- 50,
1442
- 56,
1443
- 2
1444
- ],
1445
- [
1446
- 36,
1447
- 76,
1448
- 0
1449
- ],
1450
- [
1451
- 46,
1452
- 20,
1453
- 4
1454
- ],
1455
- [
1456
- 54,
1457
202
  34,
1458
- 2
1459
- ],
203
+ 9.6,
204
+ 68
205
+ ], TEX.STONE, TEX.BRICK)
206
+ ],
207
+ spawns: [
1460
208
  [
1461
- 66,
1462
- 60,
1463
- 2
1464
- ],
1465
- [
1466
- 66,
1467
- 76,
209
+ 6,
210
+ 10,
211
+ 72,
1468
212
  0
1469
213
  ],
1470
214
  [
1471
- 92,
1472
- 60,
1473
- 2
1474
- ],
1475
- [
1476
- 34,
1477
- 100,
1478
- 4
1479
- ],
1480
- [
1481
- 50,
1482
- 108,
1483
- 2
1484
- ],
1485
- [
1486
- 70,
1487
- 13,
1488
- 4
1489
- ],
1490
- [
1491
- 92,
1492
- 13,
1493
- 2
1494
- ],
1495
- [
1496
- 70,
1497
- 43,
215
+ 10,
216
+ 10,
217
+ 72,
1498
218
  0
1499
219
  ],
1500
220
  [
1501
- 92,
1502
- 43,
1503
- 6
1504
- ]
1505
- ], [
1506
- [
1507
- 30,
1508
- 70,
221
+ 14,
222
+ 10,
223
+ 72,
1509
224
  0
1510
225
  ],
1511
226
  [
1512
- 46,
1513
- 68,
227
+ 18,
228
+ 10,
229
+ 72,
1514
230
  0
1515
231
  ],
1516
232
  [
1517
- 50,
1518
- 28,
233
+ 22,
234
+ 10,
235
+ 72,
1519
236
  0
1520
237
  ],
1521
238
  [
1522
- 76,
1523
- 26,
239
+ 8,
240
+ 10,
241
+ 66,
1524
242
  0
1525
243
  ],
1526
244
  [
1527
- 86,
1528
- 30,
245
+ 14,
246
+ 10,
247
+ 66,
1529
248
  0
1530
249
  ],
1531
250
  [
1532
- 81,
1533
- 22,
251
+ 20,
252
+ 10,
253
+ 66,
1534
254
  0
1535
255
  ]
1536
- ], [
1537
- [
1538
- 30,
1539
- 80,
1540
- H
1541
- ],
1542
- [
1543
- 56,
1544
- 36,
1545
- H
1546
- ],
1547
- [
1548
- 60,
1549
- 78,
1550
- H
1551
- ],
1552
- [
1553
- 50,
1554
- 112,
1555
- H
1556
- ],
1557
- [
1558
- 64,
1559
- 13,
1560
- H
1561
- ],
1562
- [
1563
- 98,
1564
- 43,
1565
- H
1566
- ],
1567
- [
1568
- 114,
1569
- 38,
1570
- H
1571
- ],
1572
- [
1573
- 52,
1574
- 22,
1575
- R
1576
- ],
1577
- [
1578
- 95,
1579
- 66,
1580
- B
256
+ ],
257
+ floor: -25,
258
+ sun: {
259
+ dir: [
260
+ .6,
261
+ .64,
262
+ .48
1581
263
  ],
1582
- [
1583
- 42,
1584
- 110,
1585
- Y
264
+ color: [
265
+ 3,
266
+ 2.7,
267
+ 2.2
1586
268
  ]
1587
- ], [127, 36], [
1588
- .045,
1589
- .05,
1590
- .075,
1591
- .02
1592
- ])
1593
- ];
269
+ },
270
+ sky: [[
271
+ .12,
272
+ .3,
273
+ .75
274
+ ], [
275
+ .7,
276
+ .82,
277
+ .95
278
+ ]],
279
+ fog: [
280
+ .7,
281
+ .82,
282
+ .95,
283
+ .008
284
+ ]
285
+ };
286
+ // src/shared/world.ts
287
+ /** Metres per grid cell: about the size of the player's reach, so a query touches 1 to 4 cells. */
288
+ var CELL = 4;
289
+ /** Cell coordinates run from -OFF to OFF - 1: a 8 km square at 4 m, far more than any map. */
290
+ var OFF = 1024;
291
+ /** The key of the cell with integer coordinates (cx, cz). */
292
+ var key = (cx, cz) => (cx + OFF) * (2 * OFF) + cz + OFF;
293
+ /** Integer cell coordinate of a metre value, clamped to the keyed range. */
294
+ var at = (v, cell) => Math.min(Math.max(Math.floor(v / cell), -1024), 1023);
295
+ /** Builds the grid: each brush is listed in every cell its footprint covers. */
296
+ var build = (def, cell = CELL) => {
297
+ const grid = /* @__PURE__ */ new Map();
298
+ def.brushes.forEach(({ min, max }, i) => {
299
+ 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++) {
300
+ const k = key(cx, cz);
301
+ const list = grid.get(k);
302
+ if (list) list.push(i);
303
+ else grid.set(k, [i]);
304
+ }
305
+ });
306
+ return {
307
+ def,
308
+ cell,
309
+ grid
310
+ };
311
+ };
312
+ /**
313
+ * Indices of the brushes whose footprint may overlap the square of half side r around (x, z): every
314
+ * brush listed in a cell the square touches, once each. A candidate list, not an exact overlap.
315
+ */
316
+ var near = (w, x, z, r) => {
317
+ const out = [];
318
+ 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);
319
+ return out;
320
+ };
1594
321
  // src/shared/physics.ts
1595
- var DT$1 = 1 / 60;
322
+ var DT = 1 / 60;
1596
323
  var TAU = Math.PI * 2;
1597
324
  /** Overlaps smaller than this are rounding noise, not contact. */
1598
325
  var EPS = 1e-6;
@@ -1608,54 +335,89 @@ var norm = (a) => (a % TAU + TAU) % TAU;
1608
335
  var forward = (yaw) => [-Math.sin(yaw), -Math.cos(yaw)];
1609
336
  var f32 = Math.fround;
1610
337
  var clamp = (v, lo, hi) => Math.min(Math.max(v, lo), hi);
1611
- /**
1612
- * Whether the edge stops a body of height h whose feet are at y: a solid wall, a barred sector on
1613
- * the far side, a floor there more than `step` above the feet, or an opening (the lower ceiling
1614
- * minus the higher floor) too short for the body or under the head. SKIN keeps rounding from
1615
- * flipping the answer for a player resting on a step or against a ceiling.
1616
- */
1617
- var shut = ({ map, barred }, e, y, h, step) => {
1618
- if (e.back < 0 || barred.has(e.back)) return true;
1619
- const f = map.sectors[e.front];
1620
- const k = map.sectors[e.back];
1621
- const top = Math.min(f.ceil, k.ceil);
1622
- return k.floor > y + step + SKIN || top - Math.max(f.floor, k.floor) < h - SKIN || top < y + h - SKIN;
338
+ /** A player standing on spawn point n of the world (modulo the count). */
339
+ var spawn = (id, w, n) => {
340
+ const { spawns } = w.def;
341
+ const [x, y, z, yaw] = spawns[(n % spawns.length + spawns.length) % spawns.length];
342
+ return {
343
+ id,
344
+ x: f32(x),
345
+ y: f32(y),
346
+ z: f32(z),
347
+ vy: 0,
348
+ yaw: norm(yaw),
349
+ pitch: 0,
350
+ ground: true
351
+ };
1623
352
  };
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;
355
+ /** The closest point of the brush footprint to (x, z), as the offset from it to (x, z). */
356
+ var gap = (b, x, z) => [x - clamp(x, b.min[0], b.max[0]), z - clamp(z, b.min[2], b.max[2])];
1624
357
  /**
1625
- * Pushes the player out of every shut edge it overlaps, deepest first (up to 4 passes: two walls
1626
- * in a corner settle in two). The push is along the vector from the closest point of the edge, so
1627
- * against a wall it is the normal (the other axis keeps moving: the slide) and at an endpoint it
1628
- * is radial: a player running at a corner slides round it. Deepest first matters on a straight
1629
- * wall split by a collinear point: the endpoint of the next piece is always shallower than the
1630
- * normal push of the piece the player is on, so it never pulls him back along the wall.
1631
- * An edge only counts from its front side (the inside of its sector); across it, the twin edge of
1632
- * the neighbour decides.
1633
- * Tunnelling is impossible: a step is RUN_SPEED / TICK_HZ = 0.1 m and the axis ends each step at
1634
- * least PLAYER_R = 0.4 m from every shut edge it started beside, so it cannot cross one in 0.1 m.
358
+ * Pushes the player out of every brush that blocks it and that the circle overlaps, deepest first
359
+ * (up to 4 passes: two walls in a corner settle in two). The push is along the vector from the
360
+ * closest point of the box, so against a side it is the normal (the other axis keeps moving: the
361
+ * slide) and at an edge it is radial: a player running at a corner slides round it. Deepest first
362
+ * matters on a flat wall made of several boxes: the corner of the next box is always shallower
363
+ * than the normal push of the box the player is on, so it never pulls him back along the wall.
364
+ * A centre inside a footprint (it never gets there by walking, see below) leaves by the nearest
365
+ * side.
366
+ *
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.
1635
371
  */
1636
- var push = (p, lvl, r, h, step) => {
1637
- const reach = (r - EPS) ** 2;
372
+ var push = (p, w) => {
373
+ const { brushes } = w.def;
374
+ const ids = near(w, p.x, p.z, 2 * PLAYER_R);
375
+ const reach = (PLAYER_R - EPS) ** 2;
1638
376
  for (let pass = 0; pass < 4; pass++) {
1639
377
  let best = 0;
1640
378
  let px = 0;
1641
379
  let pz = 0;
1642
- for (const e of lvl.edges) {
1643
- const ex = e.bx - e.ax;
1644
- const ez = e.bz - e.az;
1645
- const rx = p.x - e.ax;
1646
- const rz = p.z - e.az;
1647
- if (ex * rz - ez * rx < 0) continue;
1648
- const t = clamp((rx * ex + rz * ez) / (ex * ex + ez * ez), 0, 1);
1649
- const dx = rx - t * ex;
1650
- const dz = rz - t * ez;
380
+ for (const i of ids) {
381
+ const b = brushes[i];
382
+ if (!blocks(b, p.y)) continue;
383
+ const [dx, dz] = gap(b, p.x, p.z);
1651
384
  const d2 = dx * dx + dz * dz;
1652
385
  if (d2 >= reach) continue;
1653
- const d = Math.sqrt(d2);
1654
- if (r - d <= best || !shut(lvl, e, p.y, h, step)) continue;
1655
- best = r - d;
1656
- const n = d || Math.hypot(ex, ez);
1657
- px = (d ? dx : -ez) / n * best;
1658
- pz = (d ? dz : ex) / n * best;
386
+ if (d2) {
387
+ const d = Math.sqrt(d2);
388
+ if (.4 - d > best) {
389
+ best = PLAYER_R - d;
390
+ px = dx / d * best;
391
+ pz = dz / d * best;
392
+ }
393
+ continue;
394
+ }
395
+ const [m, nx, nz] = [
396
+ [
397
+ p.x - b.min[0],
398
+ -1,
399
+ 0
400
+ ],
401
+ [
402
+ b.max[0] - p.x,
403
+ 1,
404
+ 0
405
+ ],
406
+ [
407
+ p.z - b.min[2],
408
+ 0,
409
+ -1
410
+ ],
411
+ [
412
+ b.max[2] - p.z,
413
+ 0,
414
+ 1
415
+ ]
416
+ ].reduce((a, c) => c[0] < a[0] ? c : a);
417
+ if (m + .4 > best) {
418
+ best = m + PLAYER_R;
419
+ [px, pz] = [nx * best, nz * best];
420
+ }
1659
421
  }
1660
422
  if (!best) return;
1661
423
  p.x += px;
@@ -1663,45 +425,43 @@ var push = (p, lvl, r, h, step) => {
1663
425
  }
1664
426
  };
1665
427
  /**
1666
- * The floor and the ceiling under the player: the highest floor and the lowest ceiling of the
1667
- * sector holding the axis and of every sector across a portal the circle overlaps and could walk
1668
- * through (a shut edge is a wall: the circle never counts what lies behind it).
428
+ * The floor and the ceiling under the player: the highest top the player can stand on (at most
429
+ * STEP_H above the feet) and the lowest bottom above the head, among the boxes the circle overlaps.
430
+ * Every top between the feet and the fall is a candidate and so is every bottom above the head,
431
+ * 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
433
+ * millimetre of overlap at one end of a step and none at the other could slip past.
1669
434
  */
1670
- var span = (lvl, x, z, y, r, h, step) => {
1671
- const { map, edges } = lvl;
1672
- const s = Math.max(sectorAt(lvl, x, z), 0);
1673
- let { floor, ceil } = map.sectors[s];
1674
- const reach = (r - EPS) ** 2;
1675
- for (const e of edges) {
1676
- if (e.front !== s || e.back < 0) continue;
1677
- const ex = e.bx - e.ax;
1678
- const ez = e.bz - e.az;
1679
- const rx = x - e.ax;
1680
- const rz = z - e.az;
1681
- const t = clamp((rx * ex + rz * ez) / (ex * ex + ez * ez), 0, 1);
1682
- if ((rx - t * ex) ** 2 + (rz - t * ez) ** 2 < reach && !shut(lvl, e, y, h, step)) {
1683
- const k = map.sectors[e.back];
1684
- floor = Math.max(floor, k.floor);
1685
- ceil = Math.min(ceil, k.ceil);
1686
- }
435
+ var span = (p, w) => {
436
+ const { brushes } = w.def;
437
+ const reach = (PLAYER_R - EPS) ** 2;
438
+ let floor = -Infinity;
439
+ let ceil = Infinity;
440
+ for (const i of near(w, p.x, p.z, PLAYER_R)) {
441
+ const b = brushes[i];
442
+ const [dx, dz] = gap(b, p.x, p.z);
443
+ if (dx * dx + dz * dz >= reach) continue;
444
+ 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]);
1687
446
  }
1688
447
  return [floor, ceil];
1689
448
  };
1690
449
  /**
1691
450
  * Moves the feet by (dx, dz) for 1 / TICK_HZ s, then a jump from the ground, GRAVITY and the
1692
- * collisions of the cylinder (PLAYER_R, PLAYER_H) with the sectors of the level: it slides along
1693
- * shut edges (see shut and push), stands on the highest floor under the circle (a step up to
1694
- * STEP_H is climbed at once, a higher one needs a jump, a ledge is a fall), and bumps its head on
1695
- * the lowest ceiling. Players and walkers share it, in place.
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.
1696
456
  */
1697
- var walk = (p, dx, dz, jump, lvl) => {
457
+ var walk = (p, dx, dz, jump, w) => {
1698
458
  p.x += dx;
1699
459
  p.z += dz;
1700
- push(p, lvl, PLAYER_R, PLAYER_H, STEP_H);
1701
- const [floor, ceil] = span(lvl, p.x, p.z, p.y, PLAYER_R, PLAYER_H, STEP_H);
460
+ push(p, w);
461
+ const [floor, ceil] = span(p, w);
1702
462
  if (p.ground && jump) p.vy = 7;
1703
- const vy = p.vy - 20 * DT$1;
1704
- p.y += (p.vy + vy) / 2 * DT$1;
463
+ const vy = p.vy - 20 * DT;
464
+ p.y += (p.vy + vy) / 2 * DT;
1705
465
  p.vy = vy;
1706
466
  p.ground = false;
1707
467
  if (p.y + 1.8 > ceil) {
@@ -1715,485 +475,33 @@ var walk = (p, dx, dz, jump, lvl) => {
1715
475
  p.ground = true;
1716
476
  }
1717
477
  }
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
+ });
487
+ }
1718
488
  p.x = f32(p.x);
1719
489
  p.y = f32(p.y);
1720
490
  p.z = f32(p.z);
1721
491
  p.vy = f32(p.vy);
1722
492
  };
1723
493
  /**
1724
- * Moves a flyer (underside at p.y, radius FLYER_R, height FLYER_H) by (dx, dy, dz): sideways it is
1725
- * stopped by solid walls and by openings too small for it, and it never steps (it flies over
1726
- * steps and pits); then y is held between FLYER_HOVER above the highest floor under it and the
1727
- * lowest ceiling minus FLYER_H, the ceiling winning in a low room. Below the hover height it
1728
- * rises at FLYER_SPEED, so crossing from a pit to a ledge is not a jump; the floor itself is
1729
- * never entered.
1730
- */
1731
- var fly = (p, dx, dy, dz, lvl) => {
1732
- p.x += dx;
1733
- p.z += dz;
1734
- push(p, lvl, FLYER_R, FLYER_H, 0);
1735
- const [floor, ceil] = span(lvl, p.x, p.z, p.y, FLYER_R, FLYER_H, 0);
1736
- const hover = Math.min(floor + FLYER_HOVER, ceil - FLYER_H);
1737
- const low = Math.max(floor, Math.min(hover, p.y + FLYER_SPEED * DT$1));
1738
- p.y = Math.min(Math.max(p.y + dy, low), ceil - FLYER_H);
1739
- p.x = f32(p.x);
1740
- p.y = f32(p.y);
1741
- p.z = f32(p.z);
1742
- };
1743
- /**
1744
494
  * Applies one input for 1 / TICK_HZ s, in place: yaw and pitch from the input (pitch clamped to
1745
495
  * MAX_PITCH), RUN_SPEED along the held keys (diagonals not faster), then walk().
1746
- * The chaingun: `cool` counts down one per step, RUN_SPEED is scaled by FIRE_SLOW while KEY.FIRE
1747
- * is held, and the step that finds it held with `cool` at 0 re-arms it to FIRE_EVERY and returns
1748
- * true: the caller resolves a shot (one every FIRE_EVERY steps, the first at once).
1749
496
  */
1750
- var move = (p, i, lvl) => {
1751
- const fire = (i.keys & KEY.FIRE) !== 0;
1752
- p.cool = Math.max(0, p.cool - 1);
497
+ var move = (p, i, w) => {
1753
498
  p.yaw = norm(i.yaw);
1754
499
  p.pitch = Math.max(-MAX_PITCH, Math.min(MAX_PITCH, i.pitch));
1755
500
  const f = (i.keys & KEY.FORWARD ? 1 : 0) - (i.keys & KEY.BACK ? 1 : 0);
1756
501
  const s = (i.keys & KEY.RIGHT ? 1 : 0) - (i.keys & KEY.LEFT ? 1 : 0);
1757
- const d = 6 * DT$1 * (fire ? FIRE_SLOW : 1) / (Math.hypot(f, s) || 1);
502
+ const d = 6 * DT / (Math.hypot(f, s) || 1);
1758
503
  const [fx, fz] = forward(p.yaw);
1759
- walk(p, (fx * f - fz * s) * d, (fz * f + fx * s) * d, (i.keys & KEY.JUMP) !== 0, lvl);
1760
- if (fire && !p.cool) {
1761
- p.cool = 5;
1762
- return true;
1763
- }
1764
- return false;
1765
- };
1766
- /** A player standing on the floor at spawn point n of the map (modulo the count). */
1767
- var spawn = (id, lvl, n) => {
1768
- const { spawns, sectors } = lvl.map;
1769
- const [x, z, yaw] = spawns[(n % spawns.length + spawns.length) % spawns.length];
1770
- const y = sectors[Math.max(sectorAt(lvl, x, z), 0)].floor;
1771
- return {
1772
- id,
1773
- x: f32(x),
1774
- y: f32(y),
1775
- z: f32(z),
1776
- vy: 0,
1777
- yaw: norm(yaw),
1778
- pitch: 0,
1779
- ground: true,
1780
- cool: 0
1781
- };
1782
- };
1783
- // src/shared/shoot.ts
1784
- /** 32-bit integer hash (a xorshift-multiply round, twice). */
1785
- var hash = (n) => {
1786
- for (let k = 0; k < 2; k++) n = Math.imul(n ^ n >>> 16, 73244475);
1787
- return (n ^ n >>> 16) >>> 0;
1788
- };
1789
- /**
1790
- * Unit direction of a shot: the view direction at yaw and pitch (yaw 0, pitch 0 is -z; pitch > 0
1791
- * looks up), turned by at most SPREAD in a direction hashed from seq, so the host and the
1792
- * shooter's client agree on it.
1793
- */
1794
- var aim = (yaw, pitch, seq) => {
1795
- const [sy, cy, sp, cp] = [
1796
- Math.sin(yaw),
1797
- Math.cos(yaw),
1798
- Math.sin(pitch),
1799
- Math.cos(pitch)
1800
- ];
1801
- const h = hash(seq + 1);
1802
- const a = (h & 65535) / 65536 * TAU;
1803
- const r = (h >>> 16) / 65535;
1804
- const [x, y] = [Math.cos(a) * r * SPREAD, Math.sin(a) * r * SPREAD];
1805
- const d = [
1806
- -sy * cp + cy * x + sy * sp * y,
1807
- sp + cp * y,
1808
- -cy * cp - sy * x + cy * sp * y
1809
- ];
1810
- const n = Math.hypot(...d);
1811
- return [
1812
- d[0] / n,
1813
- d[1] / n,
1814
- d[2] / n
1815
- ];
1816
- };
1817
- /**
1818
- * Distance along the ray from o (inside the map) in the unit direction d to the first wall, step
1819
- * face, floor or ceiling, at most max.
1820
- *
1821
- * Walks the sectors: in the current convex one the ray leaves through the edge with the least
1822
- * crossing distance among the edges it goes outward through, or ends earlier on the floor or the
1823
- * ceiling. A solid edge, or a portal whose opening does not hold the crossing height, ends it.
1824
- */
1825
- var trace = (lvl, o, d, max) => {
1826
- const { map, edges } = lvl;
1827
- let s = Math.max(sectorAt(lvl, o[0], o[2]), 0);
1828
- let t = 0;
1829
- for (let hop = 0; hop <= map.sectors.length; hop++) {
1830
- const { floor, ceil } = map.sectors[s];
1831
- let exit = Infinity;
1832
- let through;
1833
- for (const e of edges) {
1834
- if (e.front !== s) continue;
1835
- const [ex, ez] = [e.bx - e.ax, e.bz - e.az];
1836
- const out = d[2] * ex - d[0] * ez;
1837
- if (out >= 0) continue;
1838
- const x = Math.max(((o[2] - e.az) * ex - (o[0] - e.ax) * ez) / -out, t);
1839
- const u = ((o[0] + x * d[0] - e.ax) * ex + (o[2] + x * d[2] - e.az) * ez) / (ex * ex + ez * ez);
1840
- if (u > -1e-9 && u < 1 + 1e-9 && (x < exit - 1e-9 || x < exit + 1e-9 && e.back < 0)) [exit, through] = [x, e];
1841
- }
1842
- const flat = d[1] < 0 ? (floor - o[1]) / d[1] : d[1] > 0 ? (ceil - o[1]) / d[1] : Infinity;
1843
- if (flat <= exit || !through) return Math.min(Math.max(flat, t), max);
1844
- if (exit >= max) return max;
1845
- const y = o[1] + exit * d[1];
1846
- const k = through.back < 0 ? void 0 : map.sectors[through.back];
1847
- if (!k || y < Math.max(floor, k.floor) || y > Math.min(ceil, k.ceil)) return exit;
1848
- [s, t] = [through.back, exit];
1849
- }
1850
- return t;
1851
- };
1852
- /**
1853
- * Distance at which the ray enters the upright cylinder of t (sides and caps), Infinity on a miss:
1854
- * the span of the ray inside the infinite cylinder, cut to the span between the cap planes.
1855
- */
1856
- var hit = (o, d, t) => {
1857
- const [px, pz] = [o[0] - t.x, o[2] - t.z];
1858
- const a = d[0] * d[0] + d[2] * d[2];
1859
- const b = px * d[0] + pz * d[2];
1860
- const c = px * px + pz * pz - t.r * t.r;
1861
- let [lo, hi] = [0, Infinity];
1862
- if (a > 1e-12) {
1863
- const q = b * b - a * c;
1864
- if (q < 0) return Infinity;
1865
- [lo, hi] = [Math.max(0, (-b - Math.sqrt(q)) / a), (-b + Math.sqrt(q)) / a];
1866
- } else if (c > 0) return Infinity;
1867
- const [y0, y1] = [t.y - o[1], t.y + t.h - o[1]];
1868
- if (d[1]) {
1869
- const [u, v] = [y0 / d[1], y1 / d[1]];
1870
- lo = Math.max(lo, Math.min(u, v));
1871
- hi = Math.min(hi, Math.max(u, v));
1872
- } else if (y0 > 0 || y1 < 0) return Infinity;
1873
- return lo <= hi ? lo : Infinity;
1874
- };
1875
- /** The body a shot sees of a player. */
1876
- var body = ({ id, x, y, z }) => ({
1877
- id,
1878
- x,
1879
- y,
1880
- z,
1881
- r: PLAYER_R,
1882
- h: PLAYER_H
1883
- });
1884
- /** The body a shot sees of a monster. */
1885
- var monsterBody = ({ id, kind, x, y, z }) => {
1886
- const walker = kind === MONSTER.WALKER;
1887
- return {
1888
- id,
1889
- x,
1890
- y,
1891
- z,
1892
- r: walker ? WALKER_R : FLYER_R,
1893
- h: walker ? WALKER_H : FLYER_H
1894
- };
1895
- };
1896
- /**
1897
- * Resolves one shot of `shooter` (state after the step that fired) from its eye (feet + EYE)
1898
- * along aim(yaw, pitch, seq): the nearest of the level (trace) and the upright cylinders
1899
- * of `targets` (bodies: players, monsters) other than the shooter, within RANGE.
1900
- */
1901
- var fire = (lvl, shooter, seq, targets) => {
1902
- const o = [
1903
- shooter.x,
1904
- shooter.y + EYE,
1905
- shooter.z
1906
- ];
1907
- const d = aim(shooter.yaw, shooter.pitch, seq);
1908
- let t = trace(lvl, o, d, 100);
1909
- let id = -1;
1910
- for (const p of targets) {
1911
- const u = p.id === shooter.id ? Infinity : hit(o, d, p);
1912
- if (u < t) [t, id] = [u, p.id];
1913
- }
1914
- return {
1915
- id: shooter.id,
1916
- x: o[0] + d[0] * t,
1917
- y: o[1] + d[1] * t,
1918
- z: o[2] + d[2] * t,
1919
- hit: id
1920
- };
1921
- };
1922
- // src/shared/monsters.ts
1923
- var DT = 1 / 60;
1924
- /** Radians a monster turns per tick toward its target. */
1925
- var TURN$1 = .05;
1926
- /** A patrolling monster moves at this share of its speed. */
1927
- var PATROL = .4;
1928
- /** A chasing monster stops this close (metres): inside MELEE_R, short of standing in the player. */
1929
- var STOP = 1;
1930
- /** Ticks between the start of a swing and its blow. */
1931
- var SWING_DELAY = 15;
1932
- /** Height of a player's chest above the feet: where monsters aim. */
1933
- var CHEST = 1.2;
1934
- /** The snapshot counts bullets with a u8. */
1935
- var MAX_BULLETS = 255;
1936
- /** Mulberry32: a small seeded generator, uniform in [0, 1). */
1937
- var mulberry32 = (seed) => {
1938
- let a = seed | 0;
1939
- return () => {
1940
- a = a + 1831565813 | 0;
1941
- let t = Math.imul(a ^ a >>> 15, 1 | a);
1942
- t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
1943
- return ((t ^ t >>> 14) >>> 0) / 4294967296;
1944
- };
1945
- };
1946
- /**
1947
- * The monsters of the level's map, from the first spawn. In hard mode they move HARD_SPEED faster
1948
- * and swing and shoot HARD_RATE as often.
1949
- */
1950
- var createHorde = (lvl, rand, hard = false) => {
1951
- const def = (lvl.map.monsters ?? []).slice(0, 64);
1952
- const [fast, rate] = hard ? [HARD_SPEED, HARD_RATE] : [1, 1];
1953
- const [walkerSpeed, flyerSpeed] = [WALKER_SPEED * fast, FLYER_SPEED * fast];
1954
- const [shootEvery, meleeEvery] = [90, 45].map((n) => Math.round(n * rate));
1955
- let shells = [];
1956
- const events = [];
1957
- let tick = 0;
1958
- let nextBullet = 0;
1959
- const mobs = def.map(([x, z, yaw, kind], i) => {
1960
- const { floor, ceil } = lvl.map.sectors[Math.max(sectorAt(lvl, x, z), 0)];
1961
- const flyer = kind === MONSTER.FLYER;
1962
- return {
1963
- id: 16 + i,
1964
- kind,
1965
- x: Math.fround(x),
1966
- y: Math.fround(flyer ? Math.min(floor + FLYER_HOVER, ceil - FLYER_H) : floor),
1967
- z: Math.fround(z),
1968
- yaw: norm(yaw),
1969
- chase: false,
1970
- vy: 0,
1971
- ground: true,
1972
- target: -1,
1973
- turn: Math.floor(rand() * 240),
1974
- melee: 0,
1975
- swing: 0,
1976
- shoot: Math.floor(rand() * shootEvery)
1977
- };
1978
- });
1979
- const mid = (m) => m.y + monsterBody(m).h / 2;
1980
- const eye = (m) => [
1981
- m.x,
1982
- m.kind === MONSTER.WALKER ? m.y + EYE : mid(m),
1983
- m.z
1984
- ];
1985
- const eyeOf = (p) => [
1986
- p.x,
1987
- p.y + EYE,
1988
- p.z
1989
- ];
1990
- const hunt = (m, id) => {
1991
- m.target = id;
1992
- m.chase = id >= 0;
1993
- };
1994
- /** The monsters within ALERT_R of `src` that it can see, and that are not hunting, hunt `who`. */
1995
- const alert = (src, who) => {
1996
- for (const m of mobs) if (m !== src && !m.chase && Math.hypot(m.x - src.x, m.z - src.z) < 12 && !blocked(lvl, eye(src), eye(m))) hunt(m, who);
1997
- };
1998
- /** The nearest player in range, inside the sight cone and not hidden becomes the target. */
1999
- const look = (m, players) => {
2000
- const [fx, fz] = forward(m.yaw);
2001
- let seen;
2002
- let near = Infinity;
2003
- for (const p of players) {
2004
- const [dx, dz] = [p.x - m.x, p.z - m.z];
2005
- const d = Math.hypot(dx, dz);
2006
- if (d <= 30 && d < near && dx * fx + dz * fz >= SIGHT_COS * d && !blocked(lvl, eye(m), eyeOf(p))) [seen, near] = [p, d];
2007
- }
2008
- if (seen) {
2009
- const spotted = !m.chase;
2010
- hunt(m, seen.id);
2011
- if (spotted) alert(m, seen.id);
2012
- }
2013
- };
2014
- const turnTo = (m, yaw) => {
2015
- const arc = ((yaw - m.yaw + Math.PI) % TAU + TAU) % TAU - Math.PI;
2016
- m.yaw = norm(m.yaw + Math.max(-.05, Math.min(TURN$1, arc)));
2017
- };
2018
- /** Whether the floor under the axis is more than a step below `y`. */
2019
- const ledge = (m, y) => lvl.map.sectors[Math.max(sectorAt(lvl, m.x, m.z), 0)].floor < y - STEP_H;
2020
- const patrol = (m) => {
2021
- const s = (m.kind === MONSTER.WALKER ? walkerSpeed : flyerSpeed) * PATROL * DT;
2022
- const [fx, fz] = forward(m.yaw);
2023
- const before = {
2024
- x: m.x,
2025
- y: m.y,
2026
- z: m.z,
2027
- vy: m.vy,
2028
- ground: m.ground
2029
- };
2030
- if (m.kind === MONSTER.WALKER) walk(m, fx * s, fz * s, false, lvl);
2031
- else fly(m, fx * s, -s, fz * s, lvl);
2032
- const drop = m.kind === MONSTER.WALKER && ledge(m, before.y);
2033
- if (drop || Math.hypot(m.x - before.x, m.z - before.z) < s / 2) {
2034
- if (drop) Object.assign(m, before);
2035
- m.yaw = norm(m.yaw + rand() * TAU);
2036
- } else if (--m.turn <= 0) {
2037
- m.turn = 120 + Math.floor(rand() * 240);
2038
- m.yaw = norm(m.yaw + (rand() - .5) * Math.PI);
2039
- }
2040
- };
2041
- /** A walker that sees the player fires a bullet from its eye at the chest, and waits. */
2042
- const shootAt = (m, p) => {
2043
- const [ox, oy, oz] = eye(m);
2044
- if (shells.length >= MAX_BULLETS || blocked(lvl, [
2045
- ox,
2046
- oy,
2047
- oz
2048
- ], eyeOf(p))) {
2049
- m.shoot = 0;
2050
- return;
2051
- }
2052
- const v = [
2053
- p.x - ox,
2054
- p.y + CHEST - oy,
2055
- p.z - oz
2056
- ];
2057
- const k = 12 * DT / Math.hypot(...v);
2058
- shells.push({
2059
- id: nextBullet++ & 255,
2060
- x: ox,
2061
- y: oy,
2062
- z: oz,
2063
- vx: v[0] * k,
2064
- vy: v[1] * k,
2065
- vz: v[2] * k,
2066
- life: 300
2067
- });
2068
- m.shoot = shootEvery + Math.floor((rand() - .5) * 20);
2069
- };
2070
- const chase = (m, p) => {
2071
- const [dx, dz] = [p.x - m.x, p.z - m.z];
2072
- const d = Math.hypot(dx, dz);
2073
- if (d > 1e-6) turnTo(m, Math.atan2(-dx, -dz));
2074
- if (m.kind === MONSTER.WALKER) {
2075
- const [fx, fz] = forward(m.yaw);
2076
- const s = d > STOP ? walkerSpeed * DT : 0;
2077
- walk(m, fx * s, fz * s, false, lvl);
2078
- } else {
2079
- const dy = p.y + CHEST - FLYER_H / 2 - m.y;
2080
- const k = Math.min(1, flyerSpeed * DT / (Math.hypot(d, dy) || 1));
2081
- fly(m, d > STOP ? dx * k : 0, dy * k, d > STOP ? dz * k : 0, lvl);
2082
- }
2083
- const near = Math.hypot(p.x - m.x, p.z - m.z) <= 1.4 && m.y < p.y + 1.8 && p.y < m.y + monsterBody(m).h;
2084
- m.melee = Math.max(0, m.melee - 1);
2085
- if (m.swing && !--m.swing && near) {
2086
- const [fx, fz] = forward(m.yaw);
2087
- if ((p.x - m.x) * fx + (p.z - m.z) * fz > 0) events.push({
2088
- type: EVENT.HURT,
2089
- id: p.id,
2090
- x: p.x,
2091
- y: p.y + CHEST,
2092
- z: p.z
2093
- });
2094
- }
2095
- if (near && !m.melee) {
2096
- events.push({
2097
- type: EVENT.SWING,
2098
- id: m.id,
2099
- x: m.x,
2100
- y: mid(m),
2101
- z: m.z
2102
- });
2103
- [m.melee, m.swing] = [meleeEvery, SWING_DELAY];
2104
- }
2105
- if (m.kind === MONSTER.WALKER && m.shoot-- <= 0 && d <= 30) shootAt(m, p);
2106
- };
2107
- /** Moves a bullet one tick; false when it is spent: it hit a player or the level, or timed out. */
2108
- const advance = (b, players) => {
2109
- const len = Math.hypot(b.vx, b.vy, b.vz);
2110
- const o = [
2111
- b.x,
2112
- b.y,
2113
- b.z
2114
- ];
2115
- const dir = [
2116
- b.vx / len,
2117
- b.vy / len,
2118
- b.vz / len
2119
- ];
2120
- let t = trace(lvl, o, dir, len);
2121
- let who = -1;
2122
- for (const p of players) {
2123
- const u = hit(o, dir, {
2124
- id: p.id,
2125
- x: p.x,
2126
- y: p.y - BULLET_R,
2127
- z: p.z,
2128
- r: PLAYER_R + BULLET_R,
2129
- h: PLAYER_H + 2 * BULLET_R
2130
- });
2131
- if (u < t) [t, who] = [u, p.id];
2132
- }
2133
- [b.x, b.y, b.z] = [
2134
- o[0] + dir[0] * t,
2135
- o[1] + dir[1] * t,
2136
- o[2] + dir[2] * t
2137
- ];
2138
- if (who >= 0) events.push({
2139
- type: EVENT.HURT,
2140
- id: who,
2141
- x: b.x,
2142
- y: b.y,
2143
- z: b.z
2144
- });
2145
- else if (t < len) events.push({
2146
- type: EVENT.BOOM,
2147
- id: b.id,
2148
- x: b.x,
2149
- y: b.y,
2150
- z: b.z
2151
- });
2152
- return who < 0 && t >= len && --b.life > 0;
2153
- };
2154
- return {
2155
- monsters: () => mobs.map(({ id, kind, x, y, z, yaw, chase }) => ({
2156
- id,
2157
- kind,
2158
- x,
2159
- y,
2160
- z,
2161
- yaw,
2162
- chase
2163
- })),
2164
- bullets: () => shells.map(({ id, x, y, z }) => ({
2165
- id,
2166
- x,
2167
- y,
2168
- z
2169
- })),
2170
- events,
2171
- step: (players) => {
2172
- tick++;
2173
- for (const m of mobs) {
2174
- if ((tick + m.id) % 60 === 0) look(m, players);
2175
- const target = players.find((p) => p.id === m.target);
2176
- if (m.chase && !target) hunt(m, -1);
2177
- if (target) chase(m, target);
2178
- else patrol(m);
2179
- }
2180
- shells = shells.filter((b) => advance(b, players));
2181
- },
2182
- kill: (id, by) => {
2183
- const i = mobs.findIndex((m) => m.id === id);
2184
- if (i < 0) return;
2185
- const [m] = mobs.splice(i, 1);
2186
- events.push({
2187
- type: EVENT.DIE,
2188
- id,
2189
- x: m.x,
2190
- y: mid(m),
2191
- z: m.z
2192
- });
2193
- alert(m, by);
2194
- return m;
2195
- }
2196
- };
504
+ walk(p, (fx * f - fz * s) * d, (fz * f + fx * s) * d, (i.keys & KEY.JUMP) !== 0, w);
2197
505
  };
2198
506
  var TURN = 65536 / (2 * Math.PI);
2199
507
  /** Radians to 65536ths of a turn; the mask also folds negative and ≥ 2π angles into one turn. */
@@ -2217,20 +525,12 @@ var decodeInput = (buf) => {
2217
525
  };
2218
526
  };
2219
527
  var encodeSnapshot = (s) => {
2220
- const shots = 7 + 25 * s.players.length;
2221
- const monsters = shots + 1 + 14 * s.shots.length;
2222
- const bullets = monsters + 1 + 16 * s.monsters.length;
2223
- const events = bullets + 1 + 13 * s.bullets.length;
2224
- const items = events + 1 + 14 * s.events.length;
2225
- const v = new DataView(new ArrayBuffer(items + 1 + 14 * s.items.length));
528
+ const v = /* @__PURE__ */ new DataView(/* @__PURE__ */ new ArrayBuffer(4 + 24 * s.players.length));
2226
529
  v.setUint8(0, 2);
2227
530
  v.setUint16(1, s.tick & 65535);
2228
531
  v.setUint8(3, s.players.length);
2229
- v.setUint8(4, s.level);
2230
- v.setUint8(5, +s.hard | (s.phase & 1) << 1);
2231
- v.setUint8(6, s.keys);
2232
532
  s.players.forEach((p, i) => {
2233
- const o = 7 + i * 25;
533
+ const o = 4 + i * 24;
2234
534
  v.setUint8(o, p.id);
2235
535
  v.setFloat32(o + 1, p.x);
2236
536
  v.setFloat32(o + 5, p.y);
@@ -2239,53 +539,7 @@ var encodeSnapshot = (s) => {
2239
539
  v.setUint16(o + 17, toTurn(p.yaw));
2240
540
  v.setInt16(o + 19, toPitch(p.pitch));
2241
541
  v.setUint16(o + 21, p.ack & 65535);
2242
- v.setUint8(o + 23, +p.ground | p.cool << 1);
2243
- v.setUint8(o + 24, p.hp);
2244
- });
2245
- v.setUint8(shots, s.shots.length);
2246
- s.shots.forEach((h, i) => {
2247
- const o = shots + 1 + i * 14;
2248
- v.setUint8(o, h.id);
2249
- v.setFloat32(o + 1, h.x);
2250
- v.setFloat32(o + 5, h.y);
2251
- v.setFloat32(o + 9, h.z);
2252
- v.setUint8(o + 13, h.hit & 255);
2253
- });
2254
- v.setUint8(monsters, s.monsters.length);
2255
- s.monsters.forEach((m, i) => {
2256
- const o = monsters + 1 + i * 16;
2257
- v.setUint8(o, m.id);
2258
- v.setUint8(o + 1, m.kind & 1 | +m.chase << 1);
2259
- v.setFloat32(o + 2, m.x);
2260
- v.setFloat32(o + 6, m.y);
2261
- v.setFloat32(o + 10, m.z);
2262
- v.setUint16(o + 14, toTurn(m.yaw));
2263
- });
2264
- v.setUint8(bullets, s.bullets.length);
2265
- s.bullets.forEach((b, i) => {
2266
- const o = bullets + 1 + i * 13;
2267
- v.setUint8(o, b.id);
2268
- v.setFloat32(o + 1, b.x);
2269
- v.setFloat32(o + 5, b.y);
2270
- v.setFloat32(o + 9, b.z);
2271
- });
2272
- v.setUint8(events, s.events.length);
2273
- s.events.forEach((e, i) => {
2274
- const o = events + 1 + i * 14;
2275
- v.setUint8(o, e.type);
2276
- v.setUint8(o + 1, e.id);
2277
- v.setFloat32(o + 2, e.x);
2278
- v.setFloat32(o + 6, e.y);
2279
- v.setFloat32(o + 10, e.z);
2280
- });
2281
- v.setUint8(items, s.items.length);
2282
- s.items.forEach((t, i) => {
2283
- const o = items + 1 + i * 14;
2284
- v.setUint8(o, t.id);
2285
- v.setUint8(o + 1, t.kind);
2286
- v.setFloat32(o + 2, t.x);
2287
- v.setFloat32(o + 6, t.y);
2288
- v.setFloat32(o + 10, t.z);
542
+ v.setUint8(o + 23, +p.ground);
2289
543
  });
2290
544
  return v.buffer;
2291
545
  };
@@ -2301,8 +555,6 @@ var parseText = (raw) => {
2301
555
  // src/shared/room.ts
2302
556
  /** Ticks run at most this many times per timer callback, so a stalled host does not spiral. */
2303
557
  var MAX_CATCH_UP = 20;
2304
- /** A pickup is this tall, standing on the floor: a body touches it when their heights overlap. */
2305
- var ITEM_H = .5;
2306
558
  /** A new player does not spawn within this many metres of another one. */
2307
559
  var SPAWN_CLEAR = 2;
2308
560
  var COLORS = [
@@ -2332,8 +584,6 @@ var unique = (name, taken) => {
2332
584
  for (let n = 2; taken.includes(out); n++) out = `${cut(name, 15 - `${n}`.length)} ${n}`;
2333
585
  return out;
2334
586
  };
2335
- /** Whether the player's body is within r of (x, z) on the floor plane and meets a thing resting on y. */
2336
- var reach = (p, x, y, z, r) => Math.hypot(p.x - x, p.z - z) <= r && p.y < y + ITEM_H && y < p.y + 1.8;
2337
587
  var setEvery = (cb, ms) => {
2338
588
  const id = setInterval(cb, ms);
2339
589
  return () => clearInterval(id);
@@ -2346,59 +596,28 @@ var setEvery = (cb, ms) => {
2346
596
  * taken by another player; 'full' beyond rules.maxPlayers. Everyone gets a fresh 'roster' on
2347
597
  * every join and leave.
2348
598
  * - Each input frame moves its player at once with move(); the INPUT_BURST bank drops the excess.
2349
- * A step that fires resolves its shot at once with fire(), the other players rewound to the
2350
- * view tick of the input (lag compensation, at most MAX_REWIND ticks back).
2351
- * - The room owns the horde (monsters.ts), stepped every tick while a player is present. A shot
2352
- * rewinds the monsters with the players, and one that hits a monster kills it.
2353
- * - A player has HEALTH. A monster's blow or bullet takes 1, then HIT_COOLDOWN ticks of grace
2354
- * (only a hit that took health is told as a HURT event). A dead player moves, shoots and
2355
- * counts for nothing until PLAYER_RESPAWN ticks later, when it is back at a spawn.
2356
- * - The level's items lie in it, and every DROP_EVERY-th kill leaves a HEALTH pack. A key taken
2357
- * opens its gates for the team, a pack heals below full health.
2358
- * - Touching the goal turns the level from HUNT to RETURN (the exit opens); when every living
2359
- * player is in the exit the level is won: the next one starts, everyone respawned. After the
2360
- * last, every player gets { t: 'end' } with the run's kills and deaths per player, and a new
2361
- * run starts at level 0, hard from then on, the counts back at 0.
2362
- * - Every SNAP_EVERY ticks of TICK_HZ, one snapshot with every player, the shots resolved since
2363
- * the last one, the monsters, the bullets, the events since the last one and the items goes to
2364
- * every peer.
599
+ * rewind() answers where the others were drawn at the view tick of an input (lag compensation,
600
+ * at most MAX_REWIND ticks back).
601
+ * - Every SNAP_EVERY ticks of TICK_HZ, one snapshot with every player goes to every peer.
2365
602
  */
2366
603
  var Room = class {
2367
604
  /** Every connected peer; null until it joins. */
2368
605
  peers = /* @__PURE__ */ new Map();
2369
606
  ticks = 0;
2370
- /** The players and monsters as each snapshot of the last MAX_REWIND ticks sent them. */
607
+ /** The players as each snapshot of the last MAX_REWIND ticks sent them. */
2371
608
  history = [];
2372
- maps;
2373
- lvl;
2374
- horde;
2375
- rand;
2376
- level = 0;
2377
- hard = false;
2378
- phase = PHASE.HUNT;
2379
- /** The team's key bits (1 << ITEM.KEY_*). */
2380
- keys = 0;
2381
- items = [];
2382
- nextItem = 0;
2383
- /** Monsters the team killed in this level. */
2384
- kills = 0;
2385
- /** Host tick the run started at. */
2386
- runStart = 0;
2387
- /** Shots resolved since the last snapshot. */
2388
- shots = [];
609
+ world;
2389
610
  halt;
2390
611
  rules;
2391
612
  /** Round-robin cursor over the spawn points, used once they are all taken. */
2392
613
  rr = 0;
2393
- /** Missing rules fall back to RULES. The seed drives the monsters. `levels` is the campaign. */
2394
- constructor(rules = {}, seed = Date.now(), levels = LEVELS) {
614
+ /** Missing rules fall back to RULES. */
615
+ constructor(rules = {}, map = ROOFTOPS) {
2395
616
  this.rules = {
2396
617
  ...RULES,
2397
618
  ...rules
2398
619
  };
2399
- this.rand = mulberry32(seed);
2400
- this.maps = levels.map(compile);
2401
- this.load(0);
620
+ this.world = build(map);
2402
621
  }
2403
622
  /** Number of joined players. */
2404
623
  get size() {
@@ -2419,15 +638,7 @@ var Room = class {
2419
638
  const input = p && decodeInput(data);
2420
639
  if (input && p.bank > 0) {
2421
640
  p.bank--;
2422
- if (p.hp && move(p.state, input, this.lvl)) {
2423
- const shot = fire(this.lvl, p.state, input.seq, this.rewind(p, input.view));
2424
- this.shots.push(shot);
2425
- if (shot.hit >= 16) {
2426
- const dead = this.horde.kill(shot.hit, p.state.id);
2427
- if (dead) p.kills++;
2428
- if (dead && ++this.kills % 4 === 0) this.drop(ITEM.HEALTH, dead.x, dead.z);
2429
- }
2430
- }
641
+ move(p.state, input, this.world);
2431
642
  p.ack = input.seq;
2432
643
  }
2433
644
  }
@@ -2442,44 +653,20 @@ var Room = class {
2442
653
  const players = this.players();
2443
654
  if (!players.length) return;
2444
655
  const now = ++this.ticks;
2445
- for (const p of players) {
2446
- p.bank = Math.min(10, p.bank + 1);
2447
- if (!p.hp && now - p.hurt >= 180) {
2448
- p.hp = 9;
2449
- p.state = this.place(p.info.id, players.filter((q) => q.hp && q !== p).map((q) => q.state));
2450
- }
2451
- }
2452
- const { events } = this.horde;
2453
- const old = events.length;
2454
- this.horde.step(players.filter((p) => p.hp).map((p) => p.state));
2455
- events.push(...events.splice(old).filter((e) => e.type !== EVENT.HURT || this.wound(e.id, now)));
2456
- this.progress(players.filter((p) => p.hp));
656
+ for (const p of players) p.bank = Math.min(10, p.bank + 1);
2457
657
  if (now % 2 === 0) {
2458
- const monsters = this.horde.monsters();
2459
658
  this.history.push({
2460
659
  tick: now,
2461
- players: players.map((p) => ({ ...p.state })),
2462
- monsters
660
+ players: players.map((p) => ({ ...p.state }))
2463
661
  });
2464
662
  while (this.history[0].tick < now - 30) this.history.shift();
2465
663
  const snap = encodeSnapshot({
2466
664
  tick: now,
2467
665
  players: players.map((p) => ({
2468
666
  ...p.state,
2469
- ack: p.ack,
2470
- hp: p.hp
2471
- })),
2472
- shots: this.shots,
2473
- monsters,
2474
- bullets: this.horde.bullets(),
2475
- events: this.horde.events.splice(0, 255),
2476
- level: this.level,
2477
- hard: this.hard,
2478
- phase: this.phase,
2479
- keys: this.keys,
2480
- items: this.items
667
+ ack: p.ack
668
+ }))
2481
669
  });
2482
- this.shots = [];
2483
670
  players.forEach((p) => p.conn.send(snap));
2484
671
  }
2485
672
  }
@@ -2511,16 +698,15 @@ var Room = class {
2511
698
  this.halt = void 0;
2512
699
  }
2513
700
  /**
2514
- * The bodies a shot of `shooter` can hit as the shooter drew them at `view` (a u16 tick,
2515
- * fractional, read against the current tick and clamped to the kept history): the other players
2516
- * and the monsters, x, y, z interpolated between the two snapshots around it like the client
2517
- * does. One the history does not hold stands where it is.
701
+ * The players other than `id` as player `id` drew them at `view`, the view tick of
702
+ * one of its inputs (a u16 tick, fractional, read against the current tick and clamped to the
703
+ * kept history): x, y, z interpolated between the two snapshots around it like the client does.
704
+ * One the history does not hold stands where it is.
2518
705
  */
2519
- rewind(shooter, view = 0) {
2520
- const now = this.players().filter((p) => p !== shooter && p.hp).map((p) => p.state);
2521
- const mobs = this.horde.monsters();
706
+ rewind(id, view = 0) {
707
+ const now = this.players().filter((p) => p.info.id !== id).map((p) => p.state);
2522
708
  const [first, last] = [this.history[0], this.history.at(-1)];
2523
- if (!first || !last) return [...now.map(body), ...mobs.map(monsterBody)];
709
+ if (!first || !last) return now;
2524
710
  const back = ((this.ticks - view + 32768) % 65536 + 65536) % 65536 - 32768;
2525
711
  const t = Math.min(Math.max(this.ticks - back, first.tick), last.tick);
2526
712
  let k = 0;
@@ -2528,9 +714,9 @@ var Room = class {
2528
714
  const [a, b] = [this.history[k], this.history[k + 1] ?? this.history[k]];
2529
715
  const u = b.tick > a.tick ? (t - a.tick) / (b.tick - a.tick) : 0;
2530
716
  const l = (m, n) => m + (n - m) * u;
2531
- const at = (cur, list) => cur.map((p) => {
2532
- const pa = list(a).find((q) => q.id === p.id);
2533
- const pb = list(b).find((q) => q.id === p.id);
717
+ return now.map((p) => {
718
+ const pa = a.players.find((q) => q.id === p.id);
719
+ const pb = b.players.find((q) => q.id === p.id);
2534
720
  return pa && pb ? {
2535
721
  ...pb,
2536
722
  x: l(pa.x, pb.x),
@@ -2538,101 +724,14 @@ var Room = class {
2538
724
  z: l(pa.z, pb.z)
2539
725
  } : pb ?? pa ?? p;
2540
726
  });
2541
- return [...at(now, (h) => h.players).map(body), ...at(mobs, (h) => h.monsters).map(monsterBody)];
2542
- }
2543
- /** Resets everything a level owns, and puts every player on a spawn of it with full health. */
2544
- load(n) {
2545
- this.level = n;
2546
- this.lvl = this.maps[n];
2547
- this.keys = 0;
2548
- this.phase = PHASE.HUNT;
2549
- bar(this.lvl, 0, PHASE.HUNT);
2550
- this.horde = createHorde(this.lvl, this.rand, this.hard);
2551
- this.items = [];
2552
- this.nextItem = 0;
2553
- this.kills = 0;
2554
- this.lvl.map.items?.forEach(([x, z, kind]) => this.drop(kind, x, z));
2555
- this.shots = [];
2556
- this.history = [];
2557
- const placed = [];
2558
- for (const p of this.players()) {
2559
- p.hp = 9;
2560
- p.state = this.place(p.info.id, placed);
2561
- placed.push(p.state);
2562
- }
2563
727
  }
2564
728
  /** The first spawn point with none of `others` within SPAWN_CLEAR, else the next one round-robin. */
2565
729
  place(id, others) {
2566
- const n = this.lvl.map.spawns.findIndex((_, k) => {
2567
- const { x, z } = spawn(id, this.lvl, k);
730
+ const n = this.world.def.spawns.findIndex((_, k) => {
731
+ const { x, z } = spawn(id, this.world, k);
2568
732
  return !others.some((o) => Math.hypot(o.x - x, o.z - z) < SPAWN_CLEAR);
2569
733
  });
2570
- return spawn(id, this.lvl, n < 0 ? this.rr++ : n);
2571
- }
2572
- /** The floor of the sector holding (x, z). */
2573
- floor(x, z) {
2574
- return this.lvl.map.sectors[Math.max(sectorAt(this.lvl, x, z), 0)].floor;
2575
- }
2576
- /** Leaves an item of `kind` on the floor at (x, z). */
2577
- drop(kind, x, z) {
2578
- const f = Math.fround;
2579
- this.items.push({
2580
- id: this.nextItem++,
2581
- kind,
2582
- x: f(x),
2583
- y: f(this.floor(x, z)),
2584
- z: f(z)
2585
- });
2586
- }
2587
- /** Takes 1 health off player `id` unless it was hurt less than HIT_COOLDOWN ticks ago. */
2588
- wound(id, now) {
2589
- const p = this.players().find((q) => q.info.id === id);
2590
- if (!p?.hp || now - p.hurt < 60) return false;
2591
- [p.hp, p.hurt] = [p.hp - 1, now];
2592
- p.deaths += +!p.hp;
2593
- return true;
2594
- }
2595
- /** What the living players do to the level: pick things up, touch the goal, win. */
2596
- progress(alive) {
2597
- const { sectors, goal, exit } = this.lvl.map;
2598
- let bars = false;
2599
- this.items = this.items.filter((t) => {
2600
- const who = alive.find((p) => reach(p.state, t.x, t.y, t.z, .8) && (t.kind !== ITEM.HEALTH || p.hp < 9));
2601
- if (who && t.kind === ITEM.HEALTH) who.hp = Math.min(9, who.hp + 3);
2602
- else if (who) {
2603
- this.keys |= 1 << t.kind;
2604
- bars = true;
2605
- }
2606
- return !who;
2607
- });
2608
- if (this.phase === PHASE.HUNT) {
2609
- if (goal && alive.some((p) => reach(p.state, goal[0], this.floor(...goal), goal[1], 1))) {
2610
- this.phase = PHASE.RETURN;
2611
- bars = true;
2612
- }
2613
- } else if (exit !== void 0 && alive.length && alive.every((p) => inside(sectors[exit], p.state.x, p.state.z))) return this.win();
2614
- if (bars) bar(this.lvl, this.keys, this.phase);
2615
- }
2616
- /** The level is won: the next one, or the end of the run and a new, hard one. */
2617
- win() {
2618
- if (this.level + 1 < this.maps.length) {
2619
- this.load(this.level + 1);
2620
- return;
2621
- }
2622
- const end = JSON.stringify({
2623
- t: "end",
2624
- ticks: this.ticks - this.runStart,
2625
- hard: this.hard,
2626
- stats: this.players().map((p) => ({
2627
- id: p.info.id,
2628
- kills: p.kills,
2629
- deaths: p.deaths
2630
- }))
2631
- });
2632
- this.players().forEach((p) => p.conn.send(end));
2633
- this.players().forEach((p) => p.kills = p.deaths = 0);
2634
- [this.hard, this.runStart] = [true, this.ticks];
2635
- this.load(0);
734
+ return spawn(id, this.world, n < 0 ? this.rr++ : n);
2636
735
  }
2637
736
  players() {
2638
737
  return [...this.peers.values()].filter((p) => p !== null);
@@ -2653,13 +752,9 @@ var Room = class {
2653
752
  name: unique(cleanName(name) || `Player ${id}`, players.map((p) => p.info.name)),
2654
753
  color
2655
754
  },
2656
- state: this.place(id, players.filter((p) => p.hp).map((p) => p.state)),
755
+ state: this.place(id, players.map((p) => p.state)),
2657
756
  bank: 10,
2658
- ack: 65535,
2659
- hp: 9,
2660
- hurt: -60,
2661
- kills: 0,
2662
- deaths: 0
757
+ ack: 65535
2663
758
  };
2664
759
  this.peers.set(conn, player);
2665
760
  this.roster();