@korial29/tiny-fps 0.30.0 → 0.31.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/README.md +1 -1
- package/dist/index.js +14 -14
- package/dist/local.js +1 -1
- package/dist/server.js +578 -381
- package/dist/types/client/beams.d.ts +4 -0
- package/dist/types/client/beats.d.ts +12 -3
- package/dist/types/client/combat.d.ts +12 -2
- package/dist/types/client/effects.d.ts +2 -0
- package/dist/types/client/flag.d.ts +2 -0
- package/dist/types/client/hud.d.ts +6 -2
- package/dist/types/client/input.d.ts +20 -4
- package/dist/types/client/katana.d.ts +52 -0
- package/dist/types/client/props.d.ts +4 -2
- package/dist/types/client/sfx.d.ts +4 -0
- package/dist/types/client/soldier.d.ts +5 -0
- package/dist/types/client/trail.d.ts +14 -0
- package/dist/types/shared/blade.d.ts +20 -0
- package/dist/types/shared/constants.d.ts +19 -0
- package/dist/types/shared/gun.d.ts +19 -6
- package/dist/types/shared/physics.d.ts +10 -4
- package/dist/types/shared/protocol.d.ts +1 -1
- package/dist/types/shared/room.d.ts +18 -2
- package/dist/types/shared/types.d.ts +28 -2
- package/dist/types/shared/world.d.ts +2 -0
- package/package.json +1 -1
package/dist/server.js
CHANGED
|
@@ -30,6 +30,48 @@ var CLIMB_INSET = .2;
|
|
|
30
30
|
var CLIMB_FWD_SPEED = 5.5;
|
|
31
31
|
var STUMBLE_SPEED = 2.5;
|
|
32
32
|
var TEAM_COLOR = ["#ff3d4f", "#2f80ff"];
|
|
33
|
+
/**
|
|
34
|
+
* The cuts, by CUT. A swing lasts `ticks`, during which no other starts, and strikes once, `strike`
|
|
35
|
+
* ticks after its start. It hits every enemy whose body comes within `reach` metres of the
|
|
36
|
+
* attacker's (centre to centre less PLAYER_R, on the floor plane), within `arc` radians either side
|
|
37
|
+
* of his look, at a height his body overlaps, with no brush between. A hit takes `hp`.
|
|
38
|
+
*/
|
|
39
|
+
var CUTS = [
|
|
40
|
+
(
|
|
41
|
+
/** THRUST: quick and the longest, a narrow line. */
|
|
42
|
+
{
|
|
43
|
+
ticks: 24,
|
|
44
|
+
strike: 8,
|
|
45
|
+
reach: 2.4,
|
|
46
|
+
arc: .3,
|
|
47
|
+
hp: 2
|
|
48
|
+
}),
|
|
49
|
+
(
|
|
50
|
+
/** LEFT, then RIGHT: across, a wide arc. */
|
|
51
|
+
{
|
|
52
|
+
ticks: 30,
|
|
53
|
+
strike: 11,
|
|
54
|
+
reach: 1.9,
|
|
55
|
+
arc: 1.2,
|
|
56
|
+
hp: 2
|
|
57
|
+
}),
|
|
58
|
+
{
|
|
59
|
+
ticks: 30,
|
|
60
|
+
strike: 11,
|
|
61
|
+
reach: 1.9,
|
|
62
|
+
arc: 1.2,
|
|
63
|
+
hp: 2
|
|
64
|
+
},
|
|
65
|
+
(
|
|
66
|
+
/** SLASH: down from above, the slowest and the hardest. */
|
|
67
|
+
{
|
|
68
|
+
ticks: 36,
|
|
69
|
+
strike: 14,
|
|
70
|
+
reach: 1.9,
|
|
71
|
+
arc: .45,
|
|
72
|
+
hp: 3
|
|
73
|
+
})
|
|
74
|
+
];
|
|
33
75
|
var RULES = {
|
|
34
76
|
maxPlayers: 16,
|
|
35
77
|
scoreLimit: 3,
|
|
@@ -55,8 +97,24 @@ var KEY = {
|
|
|
55
97
|
DASH: 64,
|
|
56
98
|
/** Hold to crouch, or to slide when fast. */
|
|
57
99
|
CROUCH: 128,
|
|
58
|
-
/** Hold to fire the gun: one shot every GUN_COOLDOWN ticks while there is ammo. */
|
|
59
|
-
FIRE: 256
|
|
100
|
+
/** Hold to fire the gun: one shot every GUN_COOLDOWN ticks while there is ammo. With the katana in hand: a cut (see CUT). */
|
|
101
|
+
FIRE: 256,
|
|
102
|
+
/**
|
|
103
|
+
* Set while the player wants the katana in hand (the client's choice: 1, 2 or the wheel), clear
|
|
104
|
+
* for the gun. Ignored without the katana; without the gun the katana is in hand whatever it says.
|
|
105
|
+
*/
|
|
106
|
+
BLADE: 512
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* The katana's cuts (PlayerState.cut, constants CUTS), chosen by the keys held when FIRE starts
|
|
110
|
+
* one: LEFT or RIGHT (one of them) cuts across from that side, else FORWARD thrusts, else (back or
|
|
111
|
+
* no key) it slashes down from above.
|
|
112
|
+
*/
|
|
113
|
+
var CUT = {
|
|
114
|
+
THRUST: 0,
|
|
115
|
+
LEFT: 1,
|
|
116
|
+
RIGHT: 2,
|
|
117
|
+
SLASH: 3
|
|
60
118
|
};
|
|
61
119
|
/** The movement state of a player: what the body is doing, one of these. */
|
|
62
120
|
var MODE = {
|
|
@@ -209,238 +267,20 @@ var ray = (w, o, d, max) => {
|
|
|
209
267
|
}
|
|
210
268
|
}
|
|
211
269
|
};
|
|
212
|
-
|
|
213
|
-
var
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
score = [0, 0];
|
|
226
|
-
/** The match is over and the end screen shows. */
|
|
227
|
-
over = false;
|
|
228
|
-
/** The flag of each team, red then blue. */
|
|
229
|
-
flags = [home(), home()];
|
|
230
|
-
world;
|
|
231
|
-
rules;
|
|
232
|
-
mates = /* @__PURE__ */ new Map();
|
|
233
|
-
/** Ticks to the end of the match, or to the next one while it is over. */
|
|
234
|
-
clock;
|
|
235
|
-
/** Ticks before each dropped flag goes home. */
|
|
236
|
-
back = [0, 0];
|
|
237
|
-
/** Who may not take each flag, and for how many more ticks: the carrier that was made to drop it. */
|
|
238
|
-
lock = [{
|
|
239
|
-
id: -1,
|
|
240
|
-
left: 0
|
|
241
|
-
}, {
|
|
242
|
-
id: -1,
|
|
243
|
-
left: 0
|
|
244
|
-
}];
|
|
245
|
-
/** Ticks before a tackled flag can be sent home: it lies there, and the carrier's mates may take it. */
|
|
246
|
-
hold = [0, 0];
|
|
247
|
-
constructor(world, rules) {
|
|
248
|
-
this.world = world;
|
|
249
|
-
this.rules = rules;
|
|
250
|
-
this.clock = rules.timeLimit * 60;
|
|
251
|
-
}
|
|
252
|
-
/** A player joined, standing at `at`. */
|
|
253
|
-
join(id, team, at) {
|
|
254
|
-
this.mates.set(id, {
|
|
255
|
-
team,
|
|
256
|
-
caps: 0,
|
|
257
|
-
returns: 0,
|
|
258
|
-
ground: [
|
|
259
|
-
at.x,
|
|
260
|
-
at.y,
|
|
261
|
-
at.z
|
|
262
|
-
]
|
|
263
|
-
});
|
|
264
|
-
}
|
|
265
|
-
/** A player left: a flag he carried is dropped where he last stood. */
|
|
266
|
-
leave(id) {
|
|
267
|
-
const k = this.carrying(id);
|
|
268
|
-
const m = this.mates.get(id);
|
|
269
|
-
if (k >= 0 && m) this.dropAt(k, m.ground);
|
|
270
|
-
this.mates.delete(id);
|
|
271
|
-
}
|
|
272
|
-
/** A player was put at `at` (a new match): his last ground is there. */
|
|
273
|
-
place(id, at) {
|
|
274
|
-
const m = this.mates.get(id);
|
|
275
|
-
if (m) m.ground = [
|
|
276
|
-
at.x,
|
|
277
|
-
at.y,
|
|
278
|
-
at.z
|
|
279
|
-
];
|
|
280
|
-
}
|
|
281
|
-
/**
|
|
282
|
-
* The player's state after one move. Under the kill floor and put back at a spawn (EV.RESPAWN),
|
|
283
|
-
* a carrier drops the flag where he last stood; then a floor under him is his last ground.
|
|
284
|
-
*/
|
|
285
|
-
moved(p) {
|
|
286
|
-
const m = this.mates.get(p.id);
|
|
287
|
-
if (!m) return;
|
|
288
|
-
const k = this.carrying(p.id);
|
|
289
|
-
if (p.ev & EV.RESPAWN && k >= 0) this.dropAt(k, m.ground);
|
|
290
|
-
if (p.ground) m.ground = [
|
|
291
|
-
p.x,
|
|
292
|
-
p.y,
|
|
293
|
-
p.z
|
|
294
|
-
];
|
|
295
|
-
}
|
|
296
|
-
/**
|
|
297
|
-
* Makes the player drop the flag he carries (the tackle of step 7). It lies under him: where he
|
|
298
|
-
* stands, or on the first floor below him, or at his last ground when there is none (over the
|
|
299
|
-
* void). False when he carried nothing.
|
|
300
|
-
*/
|
|
301
|
-
drop(p) {
|
|
302
|
-
const k = this.carrying(p.id);
|
|
303
|
-
const m = this.mates.get(p.id);
|
|
304
|
-
if (k < 0 || !m || this.over) return false;
|
|
305
|
-
let at = m.ground;
|
|
306
|
-
if (p.ground) at = [
|
|
307
|
-
p.x,
|
|
308
|
-
p.y,
|
|
309
|
-
p.z
|
|
310
|
-
];
|
|
311
|
-
else {
|
|
312
|
-
const d = ray(this.world, [
|
|
313
|
-
p.x,
|
|
314
|
-
p.y + .05,
|
|
315
|
-
p.z
|
|
316
|
-
], [
|
|
317
|
-
0,
|
|
318
|
-
-1,
|
|
319
|
-
0
|
|
320
|
-
], FALL);
|
|
321
|
-
if (d < FALL) at = [
|
|
322
|
-
p.x,
|
|
323
|
-
p.y + .05 - d,
|
|
324
|
-
p.z
|
|
325
|
-
];
|
|
326
|
-
}
|
|
327
|
-
this.dropAt(k, at);
|
|
328
|
-
this.lock[k] = {
|
|
329
|
-
id: p.id,
|
|
330
|
-
left: 120
|
|
331
|
-
};
|
|
332
|
-
this.hold[k] = 120;
|
|
333
|
-
return true;
|
|
334
|
-
}
|
|
335
|
-
/** The team whose flag the player carries, or -1. */
|
|
336
|
-
carrying(id) {
|
|
337
|
-
return this.flags.findIndex((f) => f.state === FLAG.CARRIED && f.by === id);
|
|
338
|
-
}
|
|
339
|
-
/** The winning team of a match that is over, or -1 for a draw. */
|
|
340
|
-
winner() {
|
|
341
|
-
const [r, b] = this.score;
|
|
342
|
-
return r > b ? 0 : b > r ? 1 : -1;
|
|
343
|
-
}
|
|
344
|
-
/** What every player has done this match. */
|
|
345
|
-
tally() {
|
|
346
|
-
return [...this.mates].map(([id, m]) => ({
|
|
347
|
-
id,
|
|
348
|
-
caps: m.caps,
|
|
349
|
-
returns: m.returns
|
|
350
|
-
}));
|
|
351
|
-
}
|
|
352
|
-
/** The room is empty: the next match starts from scratch. */
|
|
353
|
-
reset() {
|
|
354
|
-
this.over = false;
|
|
355
|
-
this.score = [0, 0];
|
|
356
|
-
this.flags = [home(), home()];
|
|
357
|
-
this.back = [0, 0];
|
|
358
|
-
this.lock.forEach((l) => l.left = 0);
|
|
359
|
-
this.hold = [0, 0];
|
|
360
|
-
this.clock = this.rules.timeLimit * 60;
|
|
361
|
-
for (const m of this.mates.values()) m.caps = m.returns = 0;
|
|
362
|
-
}
|
|
363
|
-
/** The part of a snapshot that is the match's. */
|
|
364
|
-
wire() {
|
|
365
|
-
return {
|
|
366
|
-
score: [...this.score],
|
|
367
|
-
clock: Math.ceil(this.clock / 60),
|
|
368
|
-
over: this.over,
|
|
369
|
-
flags: this.flags.map((f) => ({ ...f }))
|
|
370
|
-
};
|
|
371
|
-
}
|
|
372
|
-
/**
|
|
373
|
-
* One host tick over the players: the dropped flags run down, flags are taken, returned and
|
|
374
|
-
* captured, the clock runs. 'end' when the match has just ended, 'start' when the end screen is
|
|
375
|
-
* done and a new match begins (the room puts everyone back at a spawn).
|
|
376
|
-
*/
|
|
377
|
-
tick(players) {
|
|
378
|
-
if (this.over) {
|
|
379
|
-
if (--this.clock <= 0) {
|
|
380
|
-
this.reset();
|
|
381
|
-
return "start";
|
|
382
|
-
}
|
|
383
|
-
return;
|
|
384
|
-
}
|
|
385
|
-
for (const k of [0, 1]) {
|
|
386
|
-
const f = this.flags[k];
|
|
387
|
-
this.lock[k].left--;
|
|
388
|
-
this.hold[k]--;
|
|
389
|
-
if (f.state === FLAG.DROPPED) {
|
|
390
|
-
f.left = Math.ceil(--this.back[k] / 60);
|
|
391
|
-
if (this.back[k] <= 0) this.flags[k] = home();
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
for (const k of [0, 1]) {
|
|
395
|
-
const f = this.flags[k];
|
|
396
|
-
if (f.state === FLAG.CARRIED) continue;
|
|
397
|
-
const at = f.state === FLAG.HOME ? this.world.def.teams[k].base : [
|
|
398
|
-
f.x,
|
|
399
|
-
f.y,
|
|
400
|
-
f.z
|
|
401
|
-
];
|
|
402
|
-
const near = players.filter((q) => touches(q.state, at));
|
|
403
|
-
const mate = f.state === FLAG.DROPPED && this.hold[k] <= 0 ? near.find((q) => q.team === k) : void 0;
|
|
404
|
-
const foe = near.find((q) => q.team !== k && (q.id !== this.lock[k].id || this.lock[k].left <= 0));
|
|
405
|
-
if (mate) {
|
|
406
|
-
const m = this.mates.get(mate.id);
|
|
407
|
-
if (m) m.returns++;
|
|
408
|
-
this.flags[k] = home();
|
|
409
|
-
} else if (foe) this.flags[k] = {
|
|
410
|
-
...home(),
|
|
411
|
-
state: FLAG.CARRIED,
|
|
412
|
-
by: foe.id
|
|
413
|
-
};
|
|
414
|
-
}
|
|
415
|
-
for (const p of players) {
|
|
416
|
-
const k = this.carrying(p.id);
|
|
417
|
-
if (k >= 0 && this.flags[p.team].state === FLAG.HOME && touches(p.state, this.world.def.teams[p.team].base)) {
|
|
418
|
-
this.score[p.team]++;
|
|
419
|
-
const m = this.mates.get(p.id);
|
|
420
|
-
if (m) m.caps++;
|
|
421
|
-
this.flags[k] = home();
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
if (this.score.some((s) => s >= this.rules.scoreLimit) || --this.clock <= 0) {
|
|
425
|
-
this.over = true;
|
|
426
|
-
this.clock = 600;
|
|
427
|
-
return "end";
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
dropAt(k, [x, y, z]) {
|
|
431
|
-
this.flags[k] = {
|
|
432
|
-
state: FLAG.DROPPED,
|
|
433
|
-
by: 0,
|
|
434
|
-
x,
|
|
435
|
-
y,
|
|
436
|
-
z,
|
|
437
|
-
left: 10
|
|
438
|
-
};
|
|
439
|
-
this.back[k] = 600;
|
|
440
|
-
}
|
|
270
|
+
/** Whether a brush hides b from a. */
|
|
271
|
+
var hidden = (w, a, b) => {
|
|
272
|
+
const [dx, dy, dz] = [
|
|
273
|
+
b[0] - a[0],
|
|
274
|
+
b[1] - a[1],
|
|
275
|
+
b[2] - a[2]
|
|
276
|
+
];
|
|
277
|
+
const len = Math.hypot(dx, dy, dz);
|
|
278
|
+
return len > 0 && ray(w, a, [
|
|
279
|
+
dx / len,
|
|
280
|
+
dy / len,
|
|
281
|
+
dz / len
|
|
282
|
+
], len) < len - 1e-4;
|
|
441
283
|
};
|
|
442
|
-
/** Whether the player's body reaches the flag foot at `at`: close enough, and overlapping its height. */
|
|
443
|
-
var touches = (p, at) => Math.hypot(p.x - at[0], p.z - at[2]) < 1.5 && p.y < at[1] + 2.2 && p.y + 1.8 > at[1];
|
|
444
284
|
// src/shared/physics.ts
|
|
445
285
|
var DT = 1 / 60;
|
|
446
286
|
var TAU = Math.PI * 2;
|
|
@@ -496,6 +336,11 @@ var STILL = {
|
|
|
496
336
|
down: 0,
|
|
497
337
|
shots: 0,
|
|
498
338
|
hits: 0,
|
|
339
|
+
blade: false,
|
|
340
|
+
wield: false,
|
|
341
|
+
swing: 0,
|
|
342
|
+
cut: 0,
|
|
343
|
+
zap: false,
|
|
499
344
|
ev: 0,
|
|
500
345
|
fall: 0
|
|
501
346
|
};
|
|
@@ -747,14 +592,15 @@ var enter = (p, mode) => {
|
|
|
747
592
|
p.t = 0;
|
|
748
593
|
};
|
|
749
594
|
/**
|
|
750
|
-
* A tackle lands on the player: he stumbles for STUMBLE_TICKS and is thrown
|
|
751
|
-
* (dx, dz), a unit vector (the tackler's way). It ends a dash, a wall-run,
|
|
752
|
-
* clears a jump press and the coyote window. For the host to call between
|
|
595
|
+
* A tackle lands on the player: he stumbles for `ticks` (STUMBLE_TICKS) and is thrown `knock`
|
|
596
|
+
* (STUMBLE_KNOCK) m/s along (dx, dz), a unit vector (the tackler's way). It ends a dash, a wall-run,
|
|
597
|
+
* a hang and a climb, and clears a jump press and the coyote window. For the host to call between
|
|
598
|
+
* two steps. The katana's shock calls it with its own ticks and knock.
|
|
753
599
|
*/
|
|
754
|
-
var stumble = (p, dx, dz) => {
|
|
755
|
-
p.stun =
|
|
756
|
-
p.vx = f32(dx *
|
|
757
|
-
p.vz = f32(dz *
|
|
600
|
+
var stumble = (p, dx, dz, ticks = 48, knock = 6) => {
|
|
601
|
+
p.stun = ticks;
|
|
602
|
+
p.vx = f32(dx * knock);
|
|
603
|
+
p.vz = f32(dz * knock);
|
|
758
604
|
p.dash = Math.min(p.dash, 50);
|
|
759
605
|
p.coyote = 0;
|
|
760
606
|
p.buffer = 0;
|
|
@@ -1025,136 +871,428 @@ var free = (p, w, c) => {
|
|
|
1025
871
|
p.ev |= slide ? EV.SLIDE_START : 0;
|
|
1026
872
|
} else enter(p, MODE.GROUND);
|
|
1027
873
|
}
|
|
1028
|
-
p.coyote = 6;
|
|
1029
|
-
} else if (onFloor(p.mode)) {
|
|
1030
|
-
p.ev |= p.mode === MODE.SLIDE ? EV.SLIDE_END : 0;
|
|
1031
|
-
enter(p, MODE.AIR);
|
|
874
|
+
p.coyote = 6;
|
|
875
|
+
} else if (onFloor(p.mode)) {
|
|
876
|
+
p.ev |= p.mode === MODE.SLIDE ? EV.SLIDE_END : 0;
|
|
877
|
+
enter(p, MODE.AIR);
|
|
878
|
+
}
|
|
879
|
+
if (p.mode === MODE.AIR) {
|
|
880
|
+
const ledge = ledgeAt(p, w);
|
|
881
|
+
if (ledge) grab(p, ledge);
|
|
882
|
+
else startRun(p, w, c);
|
|
883
|
+
}
|
|
884
|
+
};
|
|
885
|
+
/** On the floor: jump, crouch, slide, stand up, and the speed. */
|
|
886
|
+
var onGround = (p, w, c, burst) => {
|
|
887
|
+
const low = (c.keys & KEY.CROUCH) !== 0;
|
|
888
|
+
if (p.buffer > 0 && (p.mode === MODE.GROUND || canStand(p, w))) {
|
|
889
|
+
p.ev |= p.mode === MODE.SLIDE ? EV.SLIDE_END : 0;
|
|
890
|
+
enter(p, MODE.AIR);
|
|
891
|
+
leap(p, 7);
|
|
892
|
+
return;
|
|
893
|
+
}
|
|
894
|
+
const speed = len(p.vx, p.vz);
|
|
895
|
+
if (p.mode === MODE.GROUND && low) {
|
|
896
|
+
const slide = speed >= SLIDE_MIN_SPEED;
|
|
897
|
+
enter(p, slide ? MODE.SLIDE : MODE.CROUCH);
|
|
898
|
+
p.ev |= slide ? EV.SLIDE_START : 0;
|
|
899
|
+
} else if (p.mode === MODE.SLIDE && (p.t >= 60 || speed < 4.5 || c.stumble)) {
|
|
900
|
+
p.ev |= EV.SLIDE_END;
|
|
901
|
+
enter(p, !low && canStand(p, w) ? MODE.GROUND : MODE.CROUCH);
|
|
902
|
+
} else if (p.mode === MODE.SLIDE && !low && canStand(p, w)) {
|
|
903
|
+
p.ev |= EV.SLIDE_END;
|
|
904
|
+
enter(p, MODE.GROUND);
|
|
905
|
+
} else if (p.mode === MODE.CROUCH && !low && canStand(p, w)) enter(p, MODE.GROUND);
|
|
906
|
+
if (burst) return;
|
|
907
|
+
if (c.stumble) {
|
|
908
|
+
if (speed > 2.5) {
|
|
909
|
+
const k = Math.max(STUMBLE_SPEED, speed - 12 * DT) / speed;
|
|
910
|
+
p.vx *= k;
|
|
911
|
+
p.vz *= k;
|
|
912
|
+
} else steer(p, unwalled(p, w, c, heightOf(p)), STUMBLE_SPEED);
|
|
913
|
+
} else if (p.mode === MODE.SLIDE) {
|
|
914
|
+
const k = Math.max(0, speed - 4 * DT) / speed;
|
|
915
|
+
p.vx *= k;
|
|
916
|
+
p.vz *= k;
|
|
917
|
+
} else if (p.mode === MODE.CROUCH) steer(p, unwalled(p, w, c, LOW_H), 3);
|
|
918
|
+
else {
|
|
919
|
+
const sprint = (c.keys & KEY.SPRINT) !== 0 && c.fwd;
|
|
920
|
+
steer(p, unwalled(p, w, c, PLAYER_H), sprint ? 9 : 6);
|
|
921
|
+
}
|
|
922
|
+
};
|
|
923
|
+
/** In the air: the extra jump, a jump just after an edge (coyote), and air control. */
|
|
924
|
+
var air = (p, w, c, burst) => {
|
|
925
|
+
if (p.buffer > 0) {
|
|
926
|
+
if (p.coyote > 0) leap(p, 7);
|
|
927
|
+
else if (p.jumps > 0 && !landingSoon(p, w)) {
|
|
928
|
+
p.jumps--;
|
|
929
|
+
leap(p, 6);
|
|
930
|
+
p.ev = p.ev & ~EV.JUMP | EV.DOUBLE;
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
if (burst || !c.wx && !c.wz) return;
|
|
934
|
+
const before = len(p.vx, p.vz);
|
|
935
|
+
p.vx += c.wx * 14 * DT;
|
|
936
|
+
p.vz += c.wz * 14 * DT;
|
|
937
|
+
cap(p, Math.max(before, 6));
|
|
938
|
+
};
|
|
939
|
+
/**
|
|
940
|
+
* Applies one input for 1 / TICK_HZ s, in place: yaw and pitch from the input (pitch clamped to
|
|
941
|
+
* MAX_PITCH), then the step of the current mode (see the top of the file). Feet under the world's
|
|
942
|
+
* kill floor put the player back at a spawn of his `team` (EV.RESPAWN).
|
|
943
|
+
*/
|
|
944
|
+
var move = (p, i, w, team = 0) => {
|
|
945
|
+
p.yaw = norm(i.yaw);
|
|
946
|
+
p.pitch = Math.max(-MAX_PITCH, Math.min(MAX_PITCH, i.pitch));
|
|
947
|
+
const f = (i.keys & KEY.FORWARD ? 1 : 0) - (i.keys & KEY.BACK ? 1 : 0);
|
|
948
|
+
const s = (i.keys & KEY.RIGHT ? 1 : 0) - (i.keys & KEY.LEFT ? 1 : 0);
|
|
949
|
+
const n = len(f, s) || 1;
|
|
950
|
+
const [fx, fz] = forward(p.yaw);
|
|
951
|
+
const pressed = (bit) => (i.keys & bit) !== 0 && (p.held & bit) === 0;
|
|
952
|
+
const stumbling = p.stun > 0;
|
|
953
|
+
const c = {
|
|
954
|
+
keys: i.keys,
|
|
955
|
+
stumble: stumbling,
|
|
956
|
+
jump: !stumbling && pressed(KEY.JUMP),
|
|
957
|
+
dash: !stumbling && pressed(KEY.DASH),
|
|
958
|
+
fwd: f > 0,
|
|
959
|
+
wx: (fx * f - fz * s) / n,
|
|
960
|
+
wz: (fz * f + fx * s) / n,
|
|
961
|
+
fx,
|
|
962
|
+
fz
|
|
963
|
+
};
|
|
964
|
+
const before = p.mode;
|
|
965
|
+
p.ev = 0;
|
|
966
|
+
p.fall = 0;
|
|
967
|
+
if (c.jump) p.buffer = 7;
|
|
968
|
+
if (stumbling) p.stun--;
|
|
969
|
+
if (p.dash > 0) {
|
|
970
|
+
p.dash--;
|
|
971
|
+
if (p.dash === 50) cap(p, 10);
|
|
972
|
+
}
|
|
973
|
+
if (p.mode === MODE.HANG) hang(p, w, c);
|
|
974
|
+
if (p.mode === MODE.CLIMB) climb(p, w);
|
|
975
|
+
else if (p.mode !== MODE.HANG) free(p, w, c);
|
|
976
|
+
if (p.y < w.def.floor) respawn(p, w, team);
|
|
977
|
+
p.t = p.mode === before ? Math.min(255, p.t + 1) : p.t;
|
|
978
|
+
p.buffer = Math.max(0, p.buffer - 1);
|
|
979
|
+
if (p.mode === MODE.AIR && before === MODE.AIR) p.coyote = Math.max(0, p.coyote - 1);
|
|
980
|
+
p.held = i.keys & (KEY.JUMP | KEY.DASH);
|
|
981
|
+
p.ground = onFloor(p.mode);
|
|
982
|
+
p.x = f32(p.x);
|
|
983
|
+
p.y = f32(p.y);
|
|
984
|
+
p.z = f32(p.z);
|
|
985
|
+
p.vx = f32(p.vx);
|
|
986
|
+
p.vy = f32(p.vy);
|
|
987
|
+
p.vz = f32(p.vz);
|
|
988
|
+
p.edge = f32(p.edge);
|
|
989
|
+
};
|
|
990
|
+
// src/shared/blade.ts
|
|
991
|
+
/** Where on the body the blade is, as a share of its height: the chest, which a brush must not cover. */
|
|
992
|
+
var CHEST = .7;
|
|
993
|
+
/**
|
|
994
|
+
* The cut the keys held when FIRE starts a swing give: LEFT or RIGHT, one of them, cuts across from
|
|
995
|
+
* that side; else FORWARD thrusts; else (back or no key) it slashes. Both sides held are neither.
|
|
996
|
+
*/
|
|
997
|
+
var cutOf = (keys) => {
|
|
998
|
+
const [l, r] = [keys & KEY.LEFT, keys & KEY.RIGHT];
|
|
999
|
+
return l && !r ? CUT.LEFT : r && !l ? CUT.RIGHT : keys & KEY.FORWARD ? CUT.THRUST : CUT.SLASH;
|
|
1000
|
+
};
|
|
1001
|
+
/**
|
|
1002
|
+
* The enemies the cut of `p` (`p.cut`, `p` the state after the step that struck) hits, of `foes`
|
|
1003
|
+
* (as he saw them): each within CUTS reach of his centre less PLAYER_R on the floor plane, inside
|
|
1004
|
+
* the arc round his look (the foe's own angular radius counts: his edge in the arc is a hit), with
|
|
1005
|
+
* the heights of the two bodies overlapping and no brush between the chests.
|
|
1006
|
+
*/
|
|
1007
|
+
var strike = (w, p, foes) => {
|
|
1008
|
+
const { reach, arc } = CUTS[p.cut ?? 0];
|
|
1009
|
+
const [fx, fz] = forward(p.yaw);
|
|
1010
|
+
const o = [
|
|
1011
|
+
p.x,
|
|
1012
|
+
p.y + CHEST * heightOf(p),
|
|
1013
|
+
p.z
|
|
1014
|
+
];
|
|
1015
|
+
return foes.filter((f) => {
|
|
1016
|
+
const [dx, dz] = [f.x - p.x, f.z - p.z];
|
|
1017
|
+
const d = Math.hypot(dx, dz);
|
|
1018
|
+
if (d - .4 > reach || p.y >= f.y + heightOf(f) || f.y >= p.y + heightOf(p)) return false;
|
|
1019
|
+
const off = d > .4 ? Math.abs(Math.atan2(fx * dz - fz * dx, fx * dx + fz * dz)) : 0;
|
|
1020
|
+
const wide = d > .4 ? Math.asin(PLAYER_R / d) : Math.PI;
|
|
1021
|
+
return off <= arc + wide && !hidden(w, o, [
|
|
1022
|
+
f.x,
|
|
1023
|
+
f.y + CHEST * heightOf(f),
|
|
1024
|
+
f.z
|
|
1025
|
+
]);
|
|
1026
|
+
});
|
|
1027
|
+
};
|
|
1028
|
+
/**
|
|
1029
|
+
* The way the shock of the cut of `p` throws a foe, a unit vector on the floor plane: along his
|
|
1030
|
+
* look for a thrust and a slash; a cut across ends on the opposite side to the one it starts from,
|
|
1031
|
+
* so LEFT throws to his right and RIGHT to his left.
|
|
1032
|
+
*/
|
|
1033
|
+
var knock = (p) => {
|
|
1034
|
+
const [fx, fz] = forward(p.yaw);
|
|
1035
|
+
const c = p.cut ?? 0;
|
|
1036
|
+
return c === CUT.LEFT ? [-fz, fx] : c === CUT.RIGHT ? [fz, -fx] : [fx, fz];
|
|
1037
|
+
};
|
|
1038
|
+
// src/shared/ctf.ts
|
|
1039
|
+
var home = () => ({
|
|
1040
|
+
state: FLAG.HOME,
|
|
1041
|
+
by: 0,
|
|
1042
|
+
x: 0,
|
|
1043
|
+
y: 0,
|
|
1044
|
+
z: 0,
|
|
1045
|
+
left: 0
|
|
1046
|
+
});
|
|
1047
|
+
/** How far under a carrier a flag that he drops in the air is looked for a floor, metres. */
|
|
1048
|
+
var FALL = 60;
|
|
1049
|
+
var Ctf = class {
|
|
1050
|
+
/** Captures per team, red then blue. */
|
|
1051
|
+
score = [0, 0];
|
|
1052
|
+
/** The match is over and the end screen shows. */
|
|
1053
|
+
over = false;
|
|
1054
|
+
/** The flag of each team, red then blue. */
|
|
1055
|
+
flags = [home(), home()];
|
|
1056
|
+
world;
|
|
1057
|
+
rules;
|
|
1058
|
+
mates = /* @__PURE__ */ new Map();
|
|
1059
|
+
/** Ticks to the end of the match, or to the next one while it is over. */
|
|
1060
|
+
clock;
|
|
1061
|
+
/** Ticks before each dropped flag goes home. */
|
|
1062
|
+
back = [0, 0];
|
|
1063
|
+
/** Who may not take each flag, and for how many more ticks: the carrier that was made to drop it. */
|
|
1064
|
+
lock = [{
|
|
1065
|
+
id: -1,
|
|
1066
|
+
left: 0
|
|
1067
|
+
}, {
|
|
1068
|
+
id: -1,
|
|
1069
|
+
left: 0
|
|
1070
|
+
}];
|
|
1071
|
+
/** Ticks before a tackled flag can be sent home: it lies there, and the carrier's mates may take it. */
|
|
1072
|
+
hold = [0, 0];
|
|
1073
|
+
constructor(world, rules) {
|
|
1074
|
+
this.world = world;
|
|
1075
|
+
this.rules = rules;
|
|
1076
|
+
this.clock = rules.timeLimit * 60;
|
|
1077
|
+
}
|
|
1078
|
+
/** A player joined, standing at `at`. */
|
|
1079
|
+
join(id, team, at) {
|
|
1080
|
+
this.mates.set(id, {
|
|
1081
|
+
team,
|
|
1082
|
+
caps: 0,
|
|
1083
|
+
returns: 0,
|
|
1084
|
+
ground: [
|
|
1085
|
+
at.x,
|
|
1086
|
+
at.y,
|
|
1087
|
+
at.z
|
|
1088
|
+
]
|
|
1089
|
+
});
|
|
1090
|
+
}
|
|
1091
|
+
/** A player left: a flag he carried is dropped where he last stood. */
|
|
1092
|
+
leave(id) {
|
|
1093
|
+
const k = this.carrying(id);
|
|
1094
|
+
const m = this.mates.get(id);
|
|
1095
|
+
if (k >= 0 && m) this.dropAt(k, m.ground);
|
|
1096
|
+
this.mates.delete(id);
|
|
1097
|
+
}
|
|
1098
|
+
/** A player was put at `at` (a new match): his last ground is there. */
|
|
1099
|
+
place(id, at) {
|
|
1100
|
+
const m = this.mates.get(id);
|
|
1101
|
+
if (m) m.ground = [
|
|
1102
|
+
at.x,
|
|
1103
|
+
at.y,
|
|
1104
|
+
at.z
|
|
1105
|
+
];
|
|
1106
|
+
}
|
|
1107
|
+
/**
|
|
1108
|
+
* The player's state after one move. Under the kill floor and put back at a spawn (EV.RESPAWN),
|
|
1109
|
+
* a carrier drops the flag where he last stood; then a floor under him is his last ground.
|
|
1110
|
+
*/
|
|
1111
|
+
moved(p) {
|
|
1112
|
+
const m = this.mates.get(p.id);
|
|
1113
|
+
if (!m) return;
|
|
1114
|
+
const k = this.carrying(p.id);
|
|
1115
|
+
if (p.ev & EV.RESPAWN && k >= 0) this.dropAt(k, m.ground);
|
|
1116
|
+
if (p.ground) m.ground = [
|
|
1117
|
+
p.x,
|
|
1118
|
+
p.y,
|
|
1119
|
+
p.z
|
|
1120
|
+
];
|
|
1121
|
+
}
|
|
1122
|
+
/**
|
|
1123
|
+
* Makes the player drop the flag he carries (the tackle of step 7). It lies under him: where he
|
|
1124
|
+
* stands, or on the first floor below him, or at his last ground when there is none (over the
|
|
1125
|
+
* void). False when he carried nothing.
|
|
1126
|
+
*/
|
|
1127
|
+
drop(p) {
|
|
1128
|
+
const k = this.carrying(p.id);
|
|
1129
|
+
const m = this.mates.get(p.id);
|
|
1130
|
+
if (k < 0 || !m || this.over) return false;
|
|
1131
|
+
let at = m.ground;
|
|
1132
|
+
if (p.ground) at = [
|
|
1133
|
+
p.x,
|
|
1134
|
+
p.y,
|
|
1135
|
+
p.z
|
|
1136
|
+
];
|
|
1137
|
+
else {
|
|
1138
|
+
const d = ray(this.world, [
|
|
1139
|
+
p.x,
|
|
1140
|
+
p.y + .05,
|
|
1141
|
+
p.z
|
|
1142
|
+
], [
|
|
1143
|
+
0,
|
|
1144
|
+
-1,
|
|
1145
|
+
0
|
|
1146
|
+
], FALL);
|
|
1147
|
+
if (d < FALL) at = [
|
|
1148
|
+
p.x,
|
|
1149
|
+
p.y + .05 - d,
|
|
1150
|
+
p.z
|
|
1151
|
+
];
|
|
1152
|
+
}
|
|
1153
|
+
this.dropAt(k, at);
|
|
1154
|
+
this.lock[k] = {
|
|
1155
|
+
id: p.id,
|
|
1156
|
+
left: 120
|
|
1157
|
+
};
|
|
1158
|
+
this.hold[k] = 120;
|
|
1159
|
+
return true;
|
|
1032
1160
|
}
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
else startRun(p, w, c);
|
|
1161
|
+
/** The team whose flag the player carries, or -1. */
|
|
1162
|
+
carrying(id) {
|
|
1163
|
+
return this.flags.findIndex((f) => f.state === FLAG.CARRIED && f.by === id);
|
|
1037
1164
|
}
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
if (p.buffer > 0 && (p.mode === MODE.GROUND || canStand(p, w))) {
|
|
1043
|
-
p.ev |= p.mode === MODE.SLIDE ? EV.SLIDE_END : 0;
|
|
1044
|
-
enter(p, MODE.AIR);
|
|
1045
|
-
leap(p, 7);
|
|
1046
|
-
return;
|
|
1165
|
+
/** The winning team of a match that is over, or -1 for a draw. */
|
|
1166
|
+
winner() {
|
|
1167
|
+
const [r, b] = this.score;
|
|
1168
|
+
return r > b ? 0 : b > r ? 1 : -1;
|
|
1047
1169
|
}
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
enter(p, !low && canStand(p, w) ? MODE.GROUND : MODE.CROUCH);
|
|
1056
|
-
} else if (p.mode === MODE.SLIDE && !low && canStand(p, w)) {
|
|
1057
|
-
p.ev |= EV.SLIDE_END;
|
|
1058
|
-
enter(p, MODE.GROUND);
|
|
1059
|
-
} else if (p.mode === MODE.CROUCH && !low && canStand(p, w)) enter(p, MODE.GROUND);
|
|
1060
|
-
if (burst) return;
|
|
1061
|
-
if (c.stumble) {
|
|
1062
|
-
if (speed > 2.5) {
|
|
1063
|
-
const k = Math.max(STUMBLE_SPEED, speed - 12 * DT) / speed;
|
|
1064
|
-
p.vx *= k;
|
|
1065
|
-
p.vz *= k;
|
|
1066
|
-
} else steer(p, unwalled(p, w, c, heightOf(p)), STUMBLE_SPEED);
|
|
1067
|
-
} else if (p.mode === MODE.SLIDE) {
|
|
1068
|
-
const k = Math.max(0, speed - 4 * DT) / speed;
|
|
1069
|
-
p.vx *= k;
|
|
1070
|
-
p.vz *= k;
|
|
1071
|
-
} else if (p.mode === MODE.CROUCH) steer(p, unwalled(p, w, c, LOW_H), 3);
|
|
1072
|
-
else {
|
|
1073
|
-
const sprint = (c.keys & KEY.SPRINT) !== 0 && c.fwd;
|
|
1074
|
-
steer(p, unwalled(p, w, c, PLAYER_H), sprint ? 9 : 6);
|
|
1170
|
+
/** What every player has done this match. */
|
|
1171
|
+
tally() {
|
|
1172
|
+
return [...this.mates].map(([id, m]) => ({
|
|
1173
|
+
id,
|
|
1174
|
+
caps: m.caps,
|
|
1175
|
+
returns: m.returns
|
|
1176
|
+
}));
|
|
1075
1177
|
}
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1178
|
+
/** The room is empty: the next match starts from scratch. */
|
|
1179
|
+
reset() {
|
|
1180
|
+
this.over = false;
|
|
1181
|
+
this.score = [0, 0];
|
|
1182
|
+
this.flags = [home(), home()];
|
|
1183
|
+
this.back = [0, 0];
|
|
1184
|
+
this.lock.forEach((l) => l.left = 0);
|
|
1185
|
+
this.hold = [0, 0];
|
|
1186
|
+
this.clock = this.rules.timeLimit * 60;
|
|
1187
|
+
for (const m of this.mates.values()) m.caps = m.returns = 0;
|
|
1188
|
+
}
|
|
1189
|
+
/** The part of a snapshot that is the match's. */
|
|
1190
|
+
wire() {
|
|
1191
|
+
return {
|
|
1192
|
+
score: [...this.score],
|
|
1193
|
+
clock: Math.ceil(this.clock / 60),
|
|
1194
|
+
over: this.over,
|
|
1195
|
+
flags: this.flags.map((f) => ({ ...f }))
|
|
1196
|
+
};
|
|
1197
|
+
}
|
|
1198
|
+
/**
|
|
1199
|
+
* One host tick over the players: the dropped flags run down, flags are taken, returned and
|
|
1200
|
+
* captured, the clock runs. 'end' when the match has just ended, 'start' when the end screen is
|
|
1201
|
+
* done and a new match begins (the room puts everyone back at a spawn).
|
|
1202
|
+
*/
|
|
1203
|
+
tick(players) {
|
|
1204
|
+
if (this.over) {
|
|
1205
|
+
if (--this.clock <= 0) {
|
|
1206
|
+
this.reset();
|
|
1207
|
+
return "start";
|
|
1208
|
+
}
|
|
1209
|
+
return;
|
|
1210
|
+
}
|
|
1211
|
+
for (const k of [0, 1]) {
|
|
1212
|
+
const f = this.flags[k];
|
|
1213
|
+
this.lock[k].left--;
|
|
1214
|
+
this.hold[k]--;
|
|
1215
|
+
if (f.state === FLAG.DROPPED) {
|
|
1216
|
+
f.left = Math.ceil(--this.back[k] / 60);
|
|
1217
|
+
if (this.back[k] <= 0) this.flags[k] = home();
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
for (const k of [0, 1]) {
|
|
1221
|
+
const f = this.flags[k];
|
|
1222
|
+
if (f.state === FLAG.CARRIED) continue;
|
|
1223
|
+
const at = f.state === FLAG.HOME ? this.world.def.teams[k].base : [
|
|
1224
|
+
f.x,
|
|
1225
|
+
f.y,
|
|
1226
|
+
f.z
|
|
1227
|
+
];
|
|
1228
|
+
const near = players.filter((q) => touches(q.state, at));
|
|
1229
|
+
const mate = f.state === FLAG.DROPPED && this.hold[k] <= 0 ? near.find((q) => q.team === k) : void 0;
|
|
1230
|
+
const foe = near.find((q) => q.team !== k && (q.id !== this.lock[k].id || this.lock[k].left <= 0));
|
|
1231
|
+
if (mate) {
|
|
1232
|
+
const m = this.mates.get(mate.id);
|
|
1233
|
+
if (m) m.returns++;
|
|
1234
|
+
this.flags[k] = home();
|
|
1235
|
+
} else if (foe) this.flags[k] = {
|
|
1236
|
+
...home(),
|
|
1237
|
+
state: FLAG.CARRIED,
|
|
1238
|
+
by: foe.id
|
|
1239
|
+
};
|
|
1240
|
+
}
|
|
1241
|
+
for (const p of players) {
|
|
1242
|
+
const k = this.carrying(p.id);
|
|
1243
|
+
if (k >= 0 && this.flags[p.team].state === FLAG.HOME && touches(p.state, this.world.def.teams[p.team].base)) {
|
|
1244
|
+
this.score[p.team]++;
|
|
1245
|
+
const m = this.mates.get(p.id);
|
|
1246
|
+
if (m) m.caps++;
|
|
1247
|
+
this.flags[k] = home();
|
|
1248
|
+
}
|
|
1249
|
+
}
|
|
1250
|
+
if (this.score.some((s) => s >= this.rules.scoreLimit) || --this.clock <= 0) {
|
|
1251
|
+
this.over = true;
|
|
1252
|
+
this.clock = 600;
|
|
1253
|
+
return "end";
|
|
1085
1254
|
}
|
|
1086
1255
|
}
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
*/
|
|
1098
|
-
var move = (p, i, w, team = 0) => {
|
|
1099
|
-
p.yaw = norm(i.yaw);
|
|
1100
|
-
p.pitch = Math.max(-MAX_PITCH, Math.min(MAX_PITCH, i.pitch));
|
|
1101
|
-
const f = (i.keys & KEY.FORWARD ? 1 : 0) - (i.keys & KEY.BACK ? 1 : 0);
|
|
1102
|
-
const s = (i.keys & KEY.RIGHT ? 1 : 0) - (i.keys & KEY.LEFT ? 1 : 0);
|
|
1103
|
-
const n = len(f, s) || 1;
|
|
1104
|
-
const [fx, fz] = forward(p.yaw);
|
|
1105
|
-
const pressed = (bit) => (i.keys & bit) !== 0 && (p.held & bit) === 0;
|
|
1106
|
-
const stumbling = p.stun > 0;
|
|
1107
|
-
const c = {
|
|
1108
|
-
keys: i.keys,
|
|
1109
|
-
stumble: stumbling,
|
|
1110
|
-
jump: !stumbling && pressed(KEY.JUMP),
|
|
1111
|
-
dash: !stumbling && pressed(KEY.DASH),
|
|
1112
|
-
fwd: f > 0,
|
|
1113
|
-
wx: (fx * f - fz * s) / n,
|
|
1114
|
-
wz: (fz * f + fx * s) / n,
|
|
1115
|
-
fx,
|
|
1116
|
-
fz
|
|
1117
|
-
};
|
|
1118
|
-
const before = p.mode;
|
|
1119
|
-
p.ev = 0;
|
|
1120
|
-
p.fall = 0;
|
|
1121
|
-
if (c.jump) p.buffer = 7;
|
|
1122
|
-
if (stumbling) p.stun--;
|
|
1123
|
-
if (p.dash > 0) {
|
|
1124
|
-
p.dash--;
|
|
1125
|
-
if (p.dash === 50) cap(p, 10);
|
|
1256
|
+
dropAt(k, [x, y, z]) {
|
|
1257
|
+
this.flags[k] = {
|
|
1258
|
+
state: FLAG.DROPPED,
|
|
1259
|
+
by: 0,
|
|
1260
|
+
x,
|
|
1261
|
+
y,
|
|
1262
|
+
z,
|
|
1263
|
+
left: 10
|
|
1264
|
+
};
|
|
1265
|
+
this.back[k] = 600;
|
|
1126
1266
|
}
|
|
1127
|
-
if (p.mode === MODE.HANG) hang(p, w, c);
|
|
1128
|
-
if (p.mode === MODE.CLIMB) climb(p, w);
|
|
1129
|
-
else if (p.mode !== MODE.HANG) free(p, w, c);
|
|
1130
|
-
if (p.y < w.def.floor) respawn(p, w, team);
|
|
1131
|
-
p.t = p.mode === before ? Math.min(255, p.t + 1) : p.t;
|
|
1132
|
-
p.buffer = Math.max(0, p.buffer - 1);
|
|
1133
|
-
if (p.mode === MODE.AIR && before === MODE.AIR) p.coyote = Math.max(0, p.coyote - 1);
|
|
1134
|
-
p.held = i.keys & (KEY.JUMP | KEY.DASH);
|
|
1135
|
-
p.ground = onFloor(p.mode);
|
|
1136
|
-
p.x = f32(p.x);
|
|
1137
|
-
p.y = f32(p.y);
|
|
1138
|
-
p.z = f32(p.z);
|
|
1139
|
-
p.vx = f32(p.vx);
|
|
1140
|
-
p.vy = f32(p.vy);
|
|
1141
|
-
p.vz = f32(p.vz);
|
|
1142
|
-
p.edge = f32(p.edge);
|
|
1143
1267
|
};
|
|
1268
|
+
/** Whether the player's body reaches the flag foot at `at`: close enough, and overlapping its height. */
|
|
1269
|
+
var touches = (p, at) => Math.hypot(p.x - at[0], p.z - at[2]) < 1.5 && p.y < at[1] + 2.2 && p.y + 1.8 > at[1];
|
|
1144
1270
|
// src/shared/gun.ts
|
|
1145
1271
|
/** Whether the feet of `p` are within `r` of the point `at` on the floor plane and within PICK_H of its height. */
|
|
1146
1272
|
var within = (p, at, r) => {
|
|
1147
1273
|
const [dx, dz] = [p.x - at[0], p.z - at[2]];
|
|
1148
1274
|
return dx * dx + dz * dz < r * r && Math.abs(p.y - at[1]) < 1;
|
|
1149
1275
|
};
|
|
1276
|
+
/** What a step struck, for the host to resolve: nothing, a laser shot, or the katana's cut. */
|
|
1277
|
+
var STRIKE = {
|
|
1278
|
+
NONE: 0,
|
|
1279
|
+
SHOT: 1,
|
|
1280
|
+
CUT: 2
|
|
1281
|
+
};
|
|
1150
1282
|
/**
|
|
1151
|
-
* One input: the move, then the
|
|
1283
|
+
* One input: the move, then the weapons. Returns what struck on this step (STRIKE), for the host to
|
|
1284
|
+
* resolve it: a shot, or a cut, which is not a shot.
|
|
1152
1285
|
* - Down: the keys are dropped, and when `down` runs out the player is back at a spawn.
|
|
1153
1286
|
* - The team's rack fills the gun when the player steps into its circle (`rack` tells he is in it),
|
|
1154
|
-
* and again each time he comes back; staying gives nothing.
|
|
1155
|
-
*
|
|
1156
|
-
*
|
|
1157
|
-
*
|
|
1287
|
+
* and again each time he comes back; staying gives nothing. The katana rack gives the katana,
|
|
1288
|
+
* once and for good: it stays until he goes down.
|
|
1289
|
+
* - In hand (`wield`) is the katana when he has it and wants it (KEY.BLADE) or has no gun, and
|
|
1290
|
+
* it stays in hand for the length of a swing.
|
|
1291
|
+
* - The trigger: `cool` and `swing` run down, and at 0 a held FIRE fires the gun, with ammo, or
|
|
1292
|
+
* starts the katana's swing (`cut` from the keys), unless the hands are busy (a hang, a climb).
|
|
1293
|
+
* The cut strikes `CUTS[cut].strike` ticks into its swing. The look is the input's, so both go
|
|
1294
|
+
* where the player sees.
|
|
1295
|
+
* `armed` is false while the match is over: no weapon, no rack.
|
|
1158
1296
|
*/
|
|
1159
1297
|
var step = (p, i, w, team, armed = true) => {
|
|
1160
1298
|
const down = p.down > 0;
|
|
@@ -1162,22 +1300,35 @@ var step = (p, i, w, team, armed = true) => {
|
|
|
1162
1300
|
...i,
|
|
1163
1301
|
keys: 0
|
|
1164
1302
|
} : i, w, team);
|
|
1303
|
+
p.zap = !!p.stun && !!p.zap;
|
|
1165
1304
|
if (down) {
|
|
1166
1305
|
if (p.down > 0 && !--p.down) respawn(p, w, team);
|
|
1167
|
-
return
|
|
1306
|
+
return STRIKE.NONE;
|
|
1168
1307
|
}
|
|
1169
|
-
const rack = w.def.teams[team]
|
|
1308
|
+
const { rack, blade } = w.def.teams[team];
|
|
1170
1309
|
const inside = !!rack && within(p, rack, 1.2);
|
|
1171
1310
|
if (armed && inside && !p.rack) p.ammo = 60;
|
|
1172
1311
|
p.rack = inside;
|
|
1312
|
+
p.blade = !!p.blade || armed && !!blade && within(p, blade, 1.2);
|
|
1173
1313
|
p.cool = Math.max(0, p.cool - 1);
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1314
|
+
p.swing = Math.max(0, (p.swing ?? 0) - 1);
|
|
1315
|
+
p.wield = p.blade && (p.swing > 0 || !!(i.keys & KEY.BLADE) || p.ammo < 0);
|
|
1316
|
+
const busy = p.mode === MODE.HANG || p.mode === MODE.CLIMB;
|
|
1317
|
+
if (!armed) return STRIKE.NONE;
|
|
1318
|
+
const c = CUTS[p.cut ?? 0];
|
|
1319
|
+
if (p.swing && c.ticks - p.swing === c.strike) return STRIKE.CUT;
|
|
1320
|
+
if (i.keys & KEY.FIRE && !busy) {
|
|
1321
|
+
if (p.wield && !p.swing) {
|
|
1322
|
+
p.cut = cutOf(i.keys);
|
|
1323
|
+
p.swing = CUTS[p.cut].ticks;
|
|
1324
|
+
} else if (!p.wield && !p.cool && p.ammo > 0) {
|
|
1325
|
+
p.ammo--;
|
|
1326
|
+
p.shots = p.shots + 1 & 255;
|
|
1327
|
+
p.cool = 7;
|
|
1328
|
+
return STRIKE.SHOT;
|
|
1329
|
+
}
|
|
1179
1330
|
}
|
|
1180
|
-
return
|
|
1331
|
+
return STRIKE.NONE;
|
|
1181
1332
|
};
|
|
1182
1333
|
/** Distance at which the ray from `o` along the unit `d` enters the upright cylinder on `c`'s feet, `h` tall; Infinity on a miss. */
|
|
1183
1334
|
var cylinder = (o, d, c, h) => {
|
|
@@ -2022,6 +2173,11 @@ var HIGHLINE = {
|
|
|
2022
2173
|
47,
|
|
2023
2174
|
26,
|
|
2024
2175
|
234
|
|
2176
|
+
],
|
|
2177
|
+
blade: [
|
|
2178
|
+
47,
|
|
2179
|
+
26,
|
|
2180
|
+
231.5
|
|
2025
2181
|
]
|
|
2026
2182
|
}, {
|
|
2027
2183
|
base: [
|
|
@@ -2034,6 +2190,11 @@ var HIGHLINE = {
|
|
|
2034
2190
|
47,
|
|
2035
2191
|
26,
|
|
2036
2192
|
6
|
|
2193
|
+
],
|
|
2194
|
+
blade: [
|
|
2195
|
+
47,
|
|
2196
|
+
26,
|
|
2197
|
+
8.5
|
|
2037
2198
|
]
|
|
2038
2199
|
}],
|
|
2039
2200
|
packs: [
|
|
@@ -2107,7 +2268,7 @@ var decodeInput = (buf) => {
|
|
|
2107
2268
|
if (v.getUint8(0) !== 1) return null;
|
|
2108
2269
|
return {
|
|
2109
2270
|
seq: v.getUint16(1),
|
|
2110
|
-
keys: v.getUint16(3) &
|
|
2271
|
+
keys: v.getUint16(3) & 1023,
|
|
2111
2272
|
yaw: fromTurn(v.getUint16(5)),
|
|
2112
2273
|
pitch: fromPitch(v.getInt16(7)),
|
|
2113
2274
|
view: (v.getUint16(9) * 256 + v.getUint8(11)) / 256
|
|
@@ -2116,13 +2277,13 @@ var decodeInput = (buf) => {
|
|
|
2116
2277
|
/** Bytes of a flag on the wire, by its state. */
|
|
2117
2278
|
var flagBytes = (state) => state === FLAG.CARRIED ? 2 : state === FLAG.DROPPED ? 8 : 1;
|
|
2118
2279
|
var encodeSnapshot = (s) => {
|
|
2119
|
-
const end = 4 +
|
|
2280
|
+
const end = 4 + 46 * s.players.length;
|
|
2120
2281
|
const v = new DataView(new ArrayBuffer(end + 8 + s.flags.reduce((n, f) => n + flagBytes(f.state), 0)));
|
|
2121
2282
|
v.setUint8(0, 2);
|
|
2122
2283
|
v.setUint16(1, s.tick & 65535);
|
|
2123
2284
|
v.setUint8(3, s.players.length);
|
|
2124
2285
|
s.players.forEach((p, i) => {
|
|
2125
|
-
const o = 4 + i *
|
|
2286
|
+
const o = 4 + i * 46;
|
|
2126
2287
|
v.setUint8(o, p.id);
|
|
2127
2288
|
v.setFloat32(o + 1, p.x);
|
|
2128
2289
|
v.setFloat32(o + 5, p.y);
|
|
@@ -2139,11 +2300,12 @@ var encodeSnapshot = (s) => {
|
|
|
2139
2300
|
v.setUint8(o + 37, p.dash | (p.by >> 2 & 3) << 6);
|
|
2140
2301
|
v.setUint8(o + 38, p.coyote & 7 | (p.buffer & 7) << 3 | (p.held & KEY.JUMP ? 64 : 0) | (p.held & KEY.DASH ? 128 : 0));
|
|
2141
2302
|
v.setUint8(o + 39, p.stun | (p.by & 3) << 6);
|
|
2142
|
-
const gun = p.hp | p.cool << 3 | (p.ammo < 0 ? NO_GUN : p.ammo) << 6 | p.down << 13 | +!!p.rack << 20;
|
|
2303
|
+
const gun = p.hp | p.cool << 3 | (p.ammo < 0 ? NO_GUN : p.ammo) << 6 | p.down << 13 | +!!p.rack << 20 | +!!p.blade << 21 | +!!p.wield << 22 | +!!p.zap << 23;
|
|
2143
2304
|
v.setUint8(o + 40, gun >> 16);
|
|
2144
2305
|
v.setUint16(o + 41, gun & 65535);
|
|
2145
2306
|
v.setUint8(o + 43, p.shots);
|
|
2146
2307
|
v.setUint8(o + 44, p.hits);
|
|
2308
|
+
v.setUint8(o + 45, (p.swing ?? 0) & 63 | ((p.cut ?? 0) & 3) << 6);
|
|
2147
2309
|
});
|
|
2148
2310
|
v.setUint8(end, s.score[0]);
|
|
2149
2311
|
v.setUint8(end + 1, s.score[1]);
|
|
@@ -2258,10 +2420,11 @@ var Room = class {
|
|
|
2258
2420
|
const input = p && decodeInput(data);
|
|
2259
2421
|
if (input && p.bank > 0) {
|
|
2260
2422
|
p.bank--;
|
|
2261
|
-
const
|
|
2423
|
+
const struck = step(p.state, input, this.world, p.info.team, !this.ctf.over);
|
|
2262
2424
|
this.ctf.moved(p.state);
|
|
2263
2425
|
this.tackle(p, input.view);
|
|
2264
|
-
if (
|
|
2426
|
+
if (struck === STRIKE.SHOT) this.shoot(p, input.view);
|
|
2427
|
+
else if (struck === STRIKE.CUT) this.cut(p, input.view);
|
|
2265
2428
|
p.ack = input.seq;
|
|
2266
2429
|
}
|
|
2267
2430
|
}
|
|
@@ -2390,31 +2553,65 @@ var Room = class {
|
|
|
2390
2553
|
const v = speedOf(me);
|
|
2391
2554
|
const [dx, dz] = v > .001 ? [me.vx / v, me.vz / v] : forward(me.yaw);
|
|
2392
2555
|
stumble(foe.state, dx, dz);
|
|
2556
|
+
foe.state.zap = false;
|
|
2393
2557
|
foe.by = p.info.id;
|
|
2394
2558
|
p.tackled = this.ticks;
|
|
2395
2559
|
}
|
|
2396
2560
|
/**
|
|
2397
|
-
*
|
|
2398
|
-
*
|
|
2399
|
-
* him down: he lies where he fell, without the gun, and a flag he carries drops with the lock.
|
|
2561
|
+
* The enemies of `p` as `p` drew them at the view tick `view` (lag compensation), without one who
|
|
2562
|
+
* lies down now or did then.
|
|
2400
2563
|
*/
|
|
2401
|
-
|
|
2564
|
+
enemies(p, view) {
|
|
2402
2565
|
const peers = this.players();
|
|
2403
|
-
|
|
2566
|
+
return this.rewind(p.info.id, view).filter((seen) => {
|
|
2404
2567
|
const q = peers.find((r) => r.info.id === seen.id);
|
|
2405
2568
|
return q && q.info.team !== p.info.team && !q.state.down && !seen.down;
|
|
2406
2569
|
});
|
|
2407
|
-
|
|
2570
|
+
}
|
|
2571
|
+
/**
|
|
2572
|
+
* `hp` of damage to `foe`; the last one puts him down: he lies where he fell, without the gun or
|
|
2573
|
+
* the katana, and a flag he carries drops with the lock. True when he went down.
|
|
2574
|
+
*/
|
|
2575
|
+
harm(foe, hp) {
|
|
2576
|
+
if ((foe.state.hp -= hp) > 0) return false;
|
|
2577
|
+
Object.assign(foe.state, {
|
|
2578
|
+
hp: 0,
|
|
2579
|
+
down: 120,
|
|
2580
|
+
ammo: -1,
|
|
2581
|
+
cool: 0,
|
|
2582
|
+
blade: false,
|
|
2583
|
+
wield: false,
|
|
2584
|
+
swing: 0
|
|
2585
|
+
});
|
|
2586
|
+
this.ctf.drop(foe.state);
|
|
2587
|
+
return true;
|
|
2588
|
+
}
|
|
2589
|
+
/**
|
|
2590
|
+
* A shot of `p` went off at the view tick `view`: it hits the first enemy on its way as `p` drew
|
|
2591
|
+
* him, and takes 1 hp.
|
|
2592
|
+
*/
|
|
2593
|
+
shoot(p, view) {
|
|
2594
|
+
const foe = this.players().find((q) => q.info.id === shoot(this.world, p.state, this.enemies(p, view))?.id);
|
|
2408
2595
|
if (!foe) return;
|
|
2409
2596
|
p.state.hits = p.state.hits + 1 & 255;
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2597
|
+
this.harm(foe, 1);
|
|
2598
|
+
}
|
|
2599
|
+
/**
|
|
2600
|
+
* A cut of `p` struck at the view tick `view`: every enemy it reaches as `p` drew him is hit
|
|
2601
|
+
* (`strike`), and the `hits` count goes up once. Each takes the cut's hp; one it does not put
|
|
2602
|
+
* down is shocked: a stumble of SHOCK_TICKS thrown SHOCK_KNOCK m/s the way the blade goes, with
|
|
2603
|
+
* `zap`, and `by` the attacker. A shock drops no flag.
|
|
2604
|
+
*/
|
|
2605
|
+
cut(p, view) {
|
|
2606
|
+
const seen = strike(this.world, p.state, this.enemies(p, view)).map((s) => s.id);
|
|
2607
|
+
const foes = this.players().filter((q) => seen.includes(q.info.id));
|
|
2608
|
+
if (!foes.length) return;
|
|
2609
|
+
p.state.hits = p.state.hits + 1 & 255;
|
|
2610
|
+
const [dx, dz] = knock(p.state);
|
|
2611
|
+
for (const foe of foes) if (!this.harm(foe, CUTS[p.state.cut ?? 0].hp)) {
|
|
2612
|
+
stumble(foe.state, dx, dz, 24, 4);
|
|
2613
|
+
foe.state.zap = true;
|
|
2614
|
+
foe.by = p.info.id;
|
|
2418
2615
|
}
|
|
2419
2616
|
}
|
|
2420
2617
|
/**
|