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