@korial29/tiny-fps 0.3.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
@@ -23,14 +23,61 @@ var FLYER_HOVER = 1.2;
23
23
  var FLYER_SPEED = 3.5;
24
24
  var SIGHT_COS = Math.cos(Math.PI / 6);
25
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;
26
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
+ };
27
74
  // src/shared/level.ts
28
75
  /** Joins every edge a→b with the sector that has b→a, if any: that edge is a portal. */
29
76
  var compile = (map) => {
30
77
  const key = (ax, az, bx, bz) => `${ax},${az},${bx},${bz}`;
31
78
  const owner = /* @__PURE__ */ new Map();
32
79
  map.sectors.forEach(({ pts }, s) => pts.forEach(([ax, az], i) => owner.set(key(ax, az, ...pts[(i + 1) % pts.length]), s)));
33
- return {
80
+ const lvl = {
34
81
  map,
35
82
  edges: map.sectors.flatMap(({ pts }, front) => pts.map(([ax, az], i) => {
36
83
  const [bx, bz] = pts[(i + 1) % pts.length];
@@ -42,8 +89,17 @@ var compile = (map) => {
42
89
  front,
43
90
  back: owner.get(key(bx, bz, ax, az)) ?? -1
44
91
  };
45
- }))
92
+ })),
93
+ barred: /* @__PURE__ */ new Set()
46
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);
47
103
  };
48
104
  /** Whether (x, z) is inside the sector, its edges included. */
49
105
  var inside = ({ pts }, x, z) => pts.every(([ax, az], i) => {
@@ -72,228 +128,1393 @@ var blocked = ({ map, edges }, [ax, ay, az], [bx, by, bz]) => {
72
128
  return y < Math.max(f.floor, k.floor) || y > Math.min(f.ceil, k.ceil);
73
129
  });
74
130
  };
75
- // src/shared/types.ts
76
- /** Bits of Input.keys. Keys are read by KeyboardEvent.code, so WASD is ZQSD on AZERTY. */
77
- var KEY = {
78
- FORWARD: 1,
79
- BACK: 2,
80
- LEFT: 4,
81
- RIGHT: 8,
82
- JUMP: 16,
83
- /** Left mouse button: the chaingun fires every FIRE_EVERY steps while held. */
84
- FIRE: 32
85
- };
86
- /** Monster kinds. */
87
- var MONSTER = {
88
- /** Walks the floors like a player, shoots bullets from afar and hits up close. */
89
- WALKER: 0,
90
- /** Hovers above the floor, flies over steps and pits, hits up close. */
91
- FLYER: 1
92
- };
93
- /** What a GameEvent reports. */
94
- var EVENT = {
95
- /** Monster `id` died, (x, y, z) the middle of its body: gibs and an explosion. */
96
- DIE: 0,
97
- /** Player `id` was hit by a bullet or a blow at (x, y, z). Harmless until health (step 4). */
98
- HURT: 1,
99
- /** A bullet burst on the level at (x, y, z); `id` is the bullet. */
100
- BOOM: 2,
101
- /** Monster `id` swung at its target from (x, y, z), hit or miss. */
102
- SWING: 3
103
- };
104
- /** Procedural textures, generated by the client. A sector names one per surface. */
105
- var TEX = {
106
- BRICK: 0,
107
- TILE: 1,
108
- LAVA: 2,
109
- STONE: 3
110
- };
111
- // src/shared/map.ts
112
- var sector = (pts, floor, ceil, ft, light) => ({
113
- pts,
114
- floor,
115
- ceil,
116
- ft,
117
- wt: TEX.BRICK,
118
- ct: TEX.STONE,
119
- 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
+ ];
120
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;
121
167
  /**
122
- * The first sector map, a test bed for heights: a start room, a low corridor, a tall hall around a
123
- * 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.
124
171
  */
125
- var HALL = {
126
- sectors: [
127
- sector([
128
- [0, 0],
129
- [16, 0],
130
- [16, 6],
131
- [16, 10],
132
- [16, 16],
133
- [0, 16]
134
- ], 1, 5, TEX.TILE),
135
- sector([
136
- [16, 6],
137
- [20, 6],
138
- [20, 10],
139
- [16, 10]
140
- ], 1, 3.5, TEX.TILE, .6),
141
- sector([
142
- [20, 0],
143
- [36, 0],
144
- [36, 6],
145
- [30, 6],
146
- [26, 6],
147
- [20, 6]
148
- ], 1, 7, TEX.TILE),
149
- sector([
150
- [20, 6],
151
- [26, 6],
152
- [26, 10],
153
- [20, 10]
154
- ], 1, 7, TEX.TILE),
155
- sector([
156
- [30, 6],
157
- [36, 6],
158
- [36, 10],
159
- [30, 10]
160
- ], 1, 7, TEX.TILE),
161
- sector([
162
- [20, 10],
163
- [26, 10],
164
- [30, 10],
165
- [36, 10],
166
- [36, 16],
167
- [32, 16],
168
- [28, 16],
169
- [20, 16]
170
- ], 1, 7, TEX.TILE),
171
- sector([
172
- [26, 6],
173
- [30, 6],
174
- [30, 10],
175
- [26, 10]
176
- ], 0, 7, TEX.LAVA),
177
- sector([
178
- [28, 16],
179
- [32, 16],
180
- [32, 17],
181
- [28, 17]
182
- ], 1.4, 7, TEX.STONE),
183
- sector([
184
- [28, 17],
185
- [32, 17],
186
- [32, 18],
187
- [28, 18]
188
- ], 1.8, 7, TEX.STONE),
189
- sector([
190
- [28, 18],
191
- [32, 18],
192
- [32, 19],
193
- [28, 19]
194
- ], 2.2, 7, TEX.STONE),
195
- sector([
196
- [24, 19],
197
- [28, 19],
198
- [32, 19],
199
- [36, 19],
200
- [36, 26],
201
- [24, 26]
202
- ], 2.6, 7, TEX.TILE, .8)
203
- ],
204
- 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
+ ],
205
960
  [
206
- 4,
207
- 4,
208
- Math.PI * 1.5
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
209
1090
  ],
210
1091
  [
1092
+ 74,
1093
+ 32,
1094
+ Y
1095
+ ]
1096
+ ], [6, 13]),
1097
+ level([
1098
+ [
1099
+ 0,
1100
+ 60,
1101
+ 10,
1102
+ 72,
1103
+ 0,
211
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,
212
1123
  12,
213
- Math.PI * 1.5
1124
+ "313"
214
1125
  ],
215
1126
  [
216
- 8,
217
- 8,
218
- Math.PI * 1.5
1127
+ 27,
1128
+ 40,
1129
+ 31,
1130
+ 48,
1131
+ 0,
1132
+ 3.4,
1133
+ "000",
1134
+ .5
219
1135
  ],
220
1136
  [
1137
+ 24,
1138
+ 16,
1139
+ 34,
1140
+ 40,
1141
+ 0,
221
1142
  12,
222
- 4,
223
- Math.PI * 1.5
1143
+ "303",
1144
+ .9
224
1145
  ],
1146
+ ...stairs(0, [
1147
+ 34,
1148
+ 16,
1149
+ 40,
1150
+ 26
1151
+ ], -.4, -.4, 3, 12, "233"),
225
1152
  [
1153
+ 34,
1154
+ 26,
1155
+ 40,
1156
+ 30,
1157
+ 0,
226
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,
227
1173
  12,
228
- Math.PI * 1.5
1174
+ "303",
1175
+ .9
229
1176
  ],
230
1177
  [
231
- 2,
232
- 8,
233
- Math.PI * 1.5
1178
+ 60,
1179
+ 27,
1180
+ 61,
1181
+ 30,
1182
+ 0,
1183
+ 4,
1184
+ "000",
1185
+ 1,
1186
+ Y
234
1187
  ],
235
1188
  [
236
- 8,
237
- 2,
238
- Math.PI * 1.5
1189
+ 61,
1190
+ 10,
1191
+ 101,
1192
+ 16,
1193
+ 0,
1194
+ 14,
1195
+ "103"
239
1196
  ],
240
1197
  [
241
- 8,
1198
+ 61,
1199
+ 40,
1200
+ 101,
1201
+ 46,
1202
+ 0,
242
1203
  14,
243
- Math.PI * 1.5
244
- ]
245
- ],
246
- monsters: [
1204
+ "103"
1205
+ ],
247
1206
  [
248
- 24,
249
- 2,
250
- Math.PI * 1.2,
251
- MONSTER.WALKER
1207
+ 61,
1208
+ 16,
1209
+ 69,
1210
+ 40,
1211
+ 0,
1212
+ 14,
1213
+ "103"
252
1214
  ],
253
1215
  [
254
- 33,
255
- 3,
256
- Math.PI * .75,
257
- MONSTER.WALKER
1216
+ 93,
1217
+ 16,
1218
+ 101,
1219
+ 40,
1220
+ 0,
1221
+ 14,
1222
+ "103"
258
1223
  ],
259
1224
  [
260
- 33,
261
- 8,
262
- Math.PI * .5,
263
- MONSTER.WALKER
1225
+ 69,
1226
+ 16,
1227
+ 93,
1228
+ 20,
1229
+ .4,
1230
+ 14,
1231
+ "233",
1232
+ .9
264
1233
  ],
265
1234
  [
266
- 24,
1235
+ 69,
1236
+ 36,
1237
+ 93,
1238
+ 40,
1239
+ .4,
267
1240
  14,
268
- Math.PI * 1.8,
269
- MONSTER.WALKER
1241
+ "233",
1242
+ .9
270
1243
  ],
271
1244
  [
272
- 30,
273
- 23,
274
- 0,
275
- MONSTER.WALKER
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"
276
1272
  ],
277
1273
  [
1274
+ 101,
1275
+ 26,
1276
+ 111,
278
1277
  28,
1278
+ 0,
1279
+ 3.2,
1280
+ "333",
1281
+ .6
1282
+ ],
1283
+ [
1284
+ 111,
1285
+ 16,
1286
+ 131,
1287
+ 40,
1288
+ 0,
279
1289
  8,
1290
+ "113"
1291
+ ],
1292
+ [
1293
+ 56,
1294
+ 66,
1295
+ 57,
1296
+ 69,
280
1297
  0,
281
- MONSTER.FLYER
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
282
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
+ ], [
283
1364
  [
284
1365
  34,
285
- 13,
286
- Math.PI * .3,
287
- MONSTER.FLYER
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
288
1403
  ],
289
1404
  [
290
1405
  34,
1406
+ 100,
1407
+ 4
1408
+ ],
1409
+ [
1410
+ 50,
1411
+ 108,
1412
+ 2
1413
+ ],
1414
+ [
1415
+ 70,
1416
+ 13,
1417
+ 4
1418
+ ],
1419
+ [
1420
+ 92,
1421
+ 13,
1422
+ 2
1423
+ ],
1424
+ [
1425
+ 70,
1426
+ 43,
1427
+ 0
1428
+ ],
1429
+ [
1430
+ 92,
1431
+ 43,
1432
+ 6
1433
+ ]
1434
+ ], [
1435
+ [
1436
+ 30,
1437
+ 70,
1438
+ 0
1439
+ ],
1440
+ [
1441
+ 46,
1442
+ 68,
1443
+ 0
1444
+ ],
1445
+ [
1446
+ 50,
1447
+ 28,
1448
+ 0
1449
+ ],
1450
+ [
1451
+ 76,
1452
+ 26,
1453
+ 0
1454
+ ],
1455
+ [
1456
+ 86,
1457
+ 30,
1458
+ 0
1459
+ ],
1460
+ [
1461
+ 81,
291
1462
  22,
292
- Math.PI * .5,
293
- MONSTER.FLYER
1463
+ 0
294
1464
  ]
295
- ]
296
- };
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
+ ];
297
1518
  // src/shared/physics.ts
298
1519
  var DT$1 = 1 / 60;
299
1520
  var TAU = Math.PI * 2;
@@ -312,13 +1533,13 @@ var forward = (yaw) => [-Math.sin(yaw), -Math.cos(yaw)];
312
1533
  var f32 = Math.fround;
313
1534
  var clamp = (v, lo, hi) => Math.min(Math.max(v, lo), hi);
314
1535
  /**
315
- * Whether the edge stops a body of height h whose feet are at y: a solid wall, a floor on the far
316
- * side more than `step` above the feet, or an opening (the lower ceiling minus the higher floor)
317
- * too short for the body or under the head. SKIN keeps rounding from flipping the answer
318
- * 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.
319
1540
  */
320
- var shut = ({ map }, e, y, h, step) => {
321
- 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;
322
1543
  const f = map.sectors[e.front];
323
1544
  const k = map.sectors[e.back];
324
1545
  const top = Math.min(f.ceil, k.ceil);
@@ -646,38 +1867,39 @@ var mulberry32 = (seed) => {
646
1867
  return ((t ^ t >>> 14) >>> 0) / 4294967296;
647
1868
  };
648
1869
  };
649
- /** The monsters of the level's map, from the first spawn. */
650
- var createHorde = (lvl, rand) => {
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) => {
651
1875
  const def = (lvl.map.monsters ?? []).slice(0, 64);
652
- let mobs = [];
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));
653
1879
  let shells = [];
654
1880
  const events = [];
655
1881
  let tick = 0;
656
1882
  let nextBullet = 0;
657
- let wait = 0;
658
- const spawnAll = () => {
659
- mobs = def.map(([x, z, yaw, kind], i) => {
660
- const { floor, ceil } = lvl.map.sectors[Math.max(sectorAt(lvl, x, z), 0)];
661
- const flyer = kind === MONSTER.FLYER;
662
- return {
663
- id: 16 + i,
664
- kind,
665
- x: Math.fround(x),
666
- y: Math.fround(flyer ? Math.min(floor + FLYER_HOVER, ceil - FLYER_H) : floor),
667
- z: Math.fround(z),
668
- yaw: norm(yaw),
669
- chase: false,
670
- vy: 0,
671
- ground: true,
672
- target: -1,
673
- turn: Math.floor(rand() * 240),
674
- melee: 0,
675
- swing: 0,
676
- shoot: Math.floor(rand() * 90)
677
- };
678
- });
679
- };
680
- spawnAll();
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
+ });
681
1903
  const mid = (m) => m.y + monsterBody(m).h / 2;
682
1904
  const eye = (m) => [
683
1905
  m.x,
@@ -720,7 +1942,7 @@ var createHorde = (lvl, rand) => {
720
1942
  /** Whether the floor under the axis is more than a step below `y`. */
721
1943
  const ledge = (m, y) => lvl.map.sectors[Math.max(sectorAt(lvl, m.x, m.z), 0)].floor < y - STEP_H;
722
1944
  const patrol = (m) => {
723
- const s = (m.kind === MONSTER.WALKER ? WALKER_SPEED : FLYER_SPEED) * PATROL * DT;
1945
+ const s = (m.kind === MONSTER.WALKER ? walkerSpeed : flyerSpeed) * PATROL * DT;
724
1946
  const [fx, fz] = forward(m.yaw);
725
1947
  const before = {
726
1948
  x: m.x,
@@ -767,7 +1989,7 @@ var createHorde = (lvl, rand) => {
767
1989
  vz: v[2] * k,
768
1990
  life: 300
769
1991
  });
770
- m.shoot = 90 + Math.floor((rand() - .5) * 20);
1992
+ m.shoot = shootEvery + Math.floor((rand() - .5) * 20);
771
1993
  };
772
1994
  const chase = (m, p) => {
773
1995
  const [dx, dz] = [p.x - m.x, p.z - m.z];
@@ -775,11 +1997,11 @@ var createHorde = (lvl, rand) => {
775
1997
  if (d > 1e-6) turnTo(m, Math.atan2(-dx, -dz));
776
1998
  if (m.kind === MONSTER.WALKER) {
777
1999
  const [fx, fz] = forward(m.yaw);
778
- const s = d > STOP ? WALKER_SPEED * DT : 0;
2000
+ const s = d > STOP ? walkerSpeed * DT : 0;
779
2001
  walk(m, fx * s, fz * s, false, lvl);
780
2002
  } else {
781
2003
  const dy = p.y + CHEST - FLYER_H / 2 - m.y;
782
- const k = Math.min(1, FLYER_SPEED * DT / (Math.hypot(d, dy) || 1));
2004
+ const k = Math.min(1, flyerSpeed * DT / (Math.hypot(d, dy) || 1));
783
2005
  fly(m, d > STOP ? dx * k : 0, dy * k, d > STOP ? dz * k : 0, lvl);
784
2006
  }
785
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;
@@ -802,7 +2024,7 @@ var createHorde = (lvl, rand) => {
802
2024
  y: mid(m),
803
2025
  z: m.z
804
2026
  });
805
- [m.melee, m.swing] = [45, SWING_DELAY];
2027
+ [m.melee, m.swing] = [meleeEvery, SWING_DELAY];
806
2028
  }
807
2029
  if (m.kind === MONSTER.WALKER && m.shoot-- <= 0 && d <= 30) shootAt(m, p);
808
2030
  };
@@ -880,7 +2102,6 @@ var createHorde = (lvl, rand) => {
880
2102
  else patrol(m);
881
2103
  }
882
2104
  shells = shells.filter((b) => advance(b, players));
883
- if (!mobs.length && def.length && --wait <= 0) spawnAll();
884
2105
  },
885
2106
  kill: (id, by) => {
886
2107
  const i = mobs.findIndex((m) => m.id === id);
@@ -894,7 +2115,7 @@ var createHorde = (lvl, rand) => {
894
2115
  z: m.z
895
2116
  });
896
2117
  alert(m, by);
897
- wait = 300;
2118
+ return m;
898
2119
  }
899
2120
  };
900
2121
  };
@@ -920,16 +2141,20 @@ var decodeInput = (buf) => {
920
2141
  };
921
2142
  };
922
2143
  var encodeSnapshot = (s) => {
923
- const shots = 4 + 24 * s.players.length;
2144
+ const shots = 7 + 25 * s.players.length;
924
2145
  const monsters = shots + 1 + 14 * s.shots.length;
925
2146
  const bullets = monsters + 1 + 16 * s.monsters.length;
926
2147
  const events = bullets + 1 + 13 * s.bullets.length;
927
- const v = new DataView(new ArrayBuffer(events + 1 + 14 * s.events.length));
2148
+ const items = events + 1 + 14 * s.events.length;
2149
+ const v = new DataView(new ArrayBuffer(items + 1 + 14 * s.items.length));
928
2150
  v.setUint8(0, 2);
929
2151
  v.setUint16(1, s.tick & 65535);
930
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);
931
2156
  s.players.forEach((p, i) => {
932
- const o = 4 + i * 24;
2157
+ const o = 7 + i * 25;
933
2158
  v.setUint8(o, p.id);
934
2159
  v.setFloat32(o + 1, p.x);
935
2160
  v.setFloat32(o + 5, p.y);
@@ -939,6 +2164,7 @@ var encodeSnapshot = (s) => {
939
2164
  v.setInt16(o + 19, toPitch(p.pitch));
940
2165
  v.setUint16(o + 21, p.ack & 65535);
941
2166
  v.setUint8(o + 23, +p.ground | p.cool << 1);
2167
+ v.setUint8(o + 24, p.hp);
942
2168
  });
943
2169
  v.setUint8(shots, s.shots.length);
944
2170
  s.shots.forEach((h, i) => {
@@ -976,6 +2202,15 @@ var encodeSnapshot = (s) => {
976
2202
  v.setFloat32(o + 6, e.y);
977
2203
  v.setFloat32(o + 10, e.z);
978
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
+ });
979
2214
  return v.buffer;
980
2215
  };
981
2216
  /** Returns null on anything that is not a known text message. */
@@ -990,8 +2225,8 @@ var parseText = (raw) => {
990
2225
  // src/shared/room.ts
991
2226
  /** Ticks run at most this many times per timer callback, so a stalled host does not spiral. */
992
2227
  var MAX_CATCH_UP = 20;
993
- /** The map every room plays, compiled once. */
994
- 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;
995
2230
  /** A new player does not spawn within this many metres of another one. */
996
2231
  var SPAWN_CLEAR = 2;
997
2232
  var COLORS = [
@@ -1021,6 +2256,8 @@ var unique = (name, taken) => {
1021
2256
  for (let n = 2; taken.includes(out); n++) out = `${cut(name, 15 - `${n}`.length)} ${n}`;
1022
2257
  return out;
1023
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;
1024
2261
  var setEvery = (cb, ms) => {
1025
2262
  const id = setInterval(cb, ms);
1026
2263
  return () => clearInterval(id);
@@ -1037,8 +2274,17 @@ var setEvery = (cb, ms) => {
1037
2274
  * view tick of the input (lag compensation, at most MAX_REWIND ticks back).
1038
2275
  * - The room owns the horde (monsters.ts), stepped every tick while a player is present. A shot
1039
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.
1040
2285
  * - Every SNAP_EVERY ticks of TICK_HZ, one snapshot with every player, the shots resolved since
1041
- * the last one, the monsters, the bullets and the events since the last one goes to every peer.
2286
+ * the last one, the monsters, the bullets, the events since the last one and the items goes to
2287
+ * every peer.
1042
2288
  */
1043
2289
  var Room = class {
1044
2290
  /** Every connected peer; null until it joins. */
@@ -1046,20 +2292,36 @@ var Room = class {
1046
2292
  ticks = 0;
1047
2293
  /** The players and monsters as each snapshot of the last MAX_REWIND ticks sent them. */
1048
2294
  history = [];
2295
+ maps;
2296
+ lvl;
1049
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;
1050
2310
  /** Shots resolved since the last snapshot. */
1051
2311
  shots = [];
1052
2312
  halt;
1053
2313
  rules;
1054
2314
  /** Round-robin cursor over the spawn points, used once they are all taken. */
1055
2315
  rr = 0;
1056
- /** Missing rules fall back to RULES. The seed drives the monsters. */
1057
- constructor(rules = {}, seed = Date.now()) {
2316
+ /** Missing rules fall back to RULES. The seed drives the monsters. `levels` is the campaign. */
2317
+ constructor(rules = {}, seed = Date.now(), levels = LEVELS) {
1058
2318
  this.rules = {
1059
2319
  ...RULES,
1060
2320
  ...rules
1061
2321
  };
1062
- this.horde = createHorde(LEVEL, mulberry32(seed));
2322
+ this.rand = mulberry32(seed);
2323
+ this.maps = levels.map(compile);
2324
+ this.load(0);
1063
2325
  }
1064
2326
  /** Number of joined players. */
1065
2327
  get size() {
@@ -1080,10 +2342,13 @@ var Room = class {
1080
2342
  const input = p && decodeInput(data);
1081
2343
  if (input && p.bank > 0) {
1082
2344
  p.bank--;
1083
- if (move(p.state, input, LEVEL)) {
1084
- const shot = 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));
1085
2347
  this.shots.push(shot);
1086
- if (shot.hit >= 16) this.horde.kill(shot.hit, p.state.id);
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
+ }
1087
2352
  }
1088
2353
  p.ack = input.seq;
1089
2354
  }
@@ -1098,26 +2363,43 @@ var Room = class {
1098
2363
  tick() {
1099
2364
  const players = this.players();
1100
2365
  if (!players.length) return;
1101
- players.forEach((p) => p.bank = Math.min(10, p.bank + 1));
1102
- this.horde.step(players.map((p) => p.state));
1103
- 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) {
1104
2380
  const monsters = this.horde.monsters();
1105
2381
  this.history.push({
1106
- tick: this.ticks,
2382
+ tick: now,
1107
2383
  players: players.map((p) => ({ ...p.state })),
1108
2384
  monsters
1109
2385
  });
1110
- while (this.history[0].tick < this.ticks - 30) this.history.shift();
2386
+ while (this.history[0].tick < now - 30) this.history.shift();
1111
2387
  const snap = encodeSnapshot({
1112
- tick: this.ticks,
2388
+ tick: now,
1113
2389
  players: players.map((p) => ({
1114
2390
  ...p.state,
1115
- ack: p.ack
2391
+ ack: p.ack,
2392
+ hp: p.hp
1116
2393
  })),
1117
2394
  shots: this.shots,
1118
2395
  monsters,
1119
2396
  bullets: this.horde.bullets(),
1120
- events: this.horde.events.splice(0, 255)
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
1121
2403
  });
1122
2404
  this.shots = [];
1123
2405
  players.forEach((p) => p.conn.send(snap));
@@ -1157,7 +2439,7 @@ var Room = class {
1157
2439
  * does. One the history does not hold stands where it is.
1158
2440
  */
1159
2441
  rewind(shooter, view = 0) {
1160
- 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);
1161
2443
  const mobs = this.horde.monsters();
1162
2444
  const [first, last] = [this.history[0], this.history.at(-1)];
1163
2445
  if (!first || !last) return [...now.map(body), ...mobs.map(monsterBody)];
@@ -1180,6 +2462,93 @@ var Room = class {
1180
2462
  });
1181
2463
  return [...at(now, (h) => h.players).map(body), ...at(mobs, (h) => h.monsters).map(monsterBody)];
1182
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);
2551
+ }
1183
2552
  players() {
1184
2553
  return [...this.peers.values()].filter((p) => p !== null);
1185
2554
  }
@@ -1192,12 +2561,6 @@ var Room = class {
1192
2561
  let id = 0;
1193
2562
  while (players.some((p) => p.info.id === id)) id++;
1194
2563
  const color = COLORS.find((c) => !players.some((p) => p.info.color === c)) ?? COLORS[id % COLORS.length];
1195
- const near = (n) => {
1196
- const { x, z } = spawn(id, LEVEL, n);
1197
- return players.some((p) => Math.hypot(p.state.x - x, p.state.z - z) < SPAWN_CLEAR);
1198
- };
1199
- let n = 0;
1200
- while (n < HALL.spawns.length && near(n)) n++;
1201
2564
  const player = {
1202
2565
  conn,
1203
2566
  info: {
@@ -1205,9 +2568,11 @@ var Room = class {
1205
2568
  name: unique(cleanName(name) || `Player ${id}`, players.map((p) => p.info.name)),
1206
2569
  color
1207
2570
  },
1208
- 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)),
1209
2572
  bank: 10,
1210
- ack: 65535
2573
+ ack: 65535,
2574
+ hp: 9,
2575
+ hurt: -60
1211
2576
  };
1212
2577
  this.peers.set(conn, player);
1213
2578
  this.roster();