@maco-online/head-clash-rules 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +163 -2
- package/dist/definition.d.ts +16 -0
- package/dist/definition.js +42 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +2 -0
- package/dist/net/predicted-sim.d.ts +21 -0
- package/dist/net/predicted-sim.js +66 -0
- package/dist/net/wire.d.ts +52 -0
- package/dist/net/wire.js +266 -0
- package/dist/room-match.d.ts +35 -0
- package/dist/room-match.js +71 -0
- package/dist/sim/anim.d.ts +7 -0
- package/dist/sim/anim.js +44 -0
- package/dist/sim/ball.d.ts +9 -0
- package/dist/sim/ball.js +464 -0
- package/dist/sim/bot.d.ts +5 -0
- package/dist/sim/bot.js +130 -0
- package/dist/sim/characters.d.ts +22 -0
- package/dist/sim/characters.js +42 -0
- package/dist/sim/constants.d.ts +156 -0
- package/dist/sim/constants.js +166 -0
- package/dist/sim/debug.d.ts +14 -0
- package/dist/sim/debug.js +34 -0
- package/dist/sim/events.d.ts +79 -0
- package/dist/sim/events.js +12 -0
- package/dist/sim/geometry.d.ts +33 -0
- package/dist/sim/geometry.js +58 -0
- package/dist/sim/index.d.ts +18 -0
- package/dist/sim/index.js +13 -0
- package/dist/sim/input.d.ts +12 -0
- package/dist/sim/input.js +25 -0
- package/dist/sim/match.d.ts +17 -0
- package/dist/sim/match.js +208 -0
- package/dist/sim/player.d.ts +16 -0
- package/dist/sim/player.js +207 -0
- package/dist/sim/powerups.d.ts +33 -0
- package/dist/sim/powerups.js +189 -0
- package/dist/sim/rng.d.ts +12 -0
- package/dist/sim/rng.js +21 -0
- package/dist/sim/specials.d.ts +21 -0
- package/dist/sim/specials.js +114 -0
- package/dist/sim/state.d.ts +125 -0
- package/dist/sim/state.js +1 -0
- package/dist/sim/types.d.ts +31 -0
- package/dist/sim/types.js +30 -0
- package/dist/sim/walls.d.ts +60 -0
- package/dist/sim/walls.js +215 -0
- package/package.json +56 -3
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { RoomMatch, RoomMatchMember } from '@maco-online/online-core';
|
|
2
|
+
import type { EndMessage, SnapshotMessage, WireInput } from './net/wire.ts';
|
|
3
|
+
import type { MatchState } from './sim/state.ts';
|
|
4
|
+
import type { CharacterId } from './sim/types.ts';
|
|
5
|
+
/** What a player tells the room about themselves: the character they play. */
|
|
6
|
+
export interface HeadClashProfile {
|
|
7
|
+
character: CharacterId;
|
|
8
|
+
}
|
|
9
|
+
export declare const DEFAULT_CHARACTER: CharacterId;
|
|
10
|
+
export interface HeadClashRoomMatchOptions {
|
|
11
|
+
/** Seed of the match's random sequence; a fresh cryptographic one by default. */
|
|
12
|
+
seed?: number;
|
|
13
|
+
/** Regular time; the standard 90 s by default. */
|
|
14
|
+
matchSeconds?: number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* One online Head Clash match, run by the room one tick at a time. Slot 0 plays player 0 (left side, defends the left
|
|
18
|
+
* goal) and slot 1 player 1. The room steps exactly the simulation a browser runs offline.
|
|
19
|
+
*/
|
|
20
|
+
export declare class HeadClashRoomMatch implements RoomMatch<WireInput, SnapshotMessage> {
|
|
21
|
+
private readonly match;
|
|
22
|
+
private tick;
|
|
23
|
+
/** Events since the previous state, stamped with the tick they happened on. */
|
|
24
|
+
private events;
|
|
25
|
+
private outcome;
|
|
26
|
+
constructor(members: readonly RoomMatchMember<Partial<HeadClashProfile>>[], options?: HeadClashRoomMatchOptions);
|
|
27
|
+
/** The match as the room runs it. */
|
|
28
|
+
get matchState(): Readonly<MatchState>;
|
|
29
|
+
get over(): boolean;
|
|
30
|
+
step(inputs: ReadonlyMap<number, WireInput>): void;
|
|
31
|
+
state(): SnapshotMessage;
|
|
32
|
+
/** The player in `slot` left for good, and the other side wins by forfeit. */
|
|
33
|
+
leave(slot: number): void;
|
|
34
|
+
result(): EndMessage | null;
|
|
35
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { IDLE_INPUT, snapshotOf } from "./net/wire.js";
|
|
2
|
+
import { decodeInput } from "./sim/input.js";
|
|
3
|
+
import { createMatch, stepMatch } from "./sim/match.js";
|
|
4
|
+
export const DEFAULT_CHARACTER = 'tuan';
|
|
5
|
+
/** A fresh seed for each match, from the platform's cryptographic source. */
|
|
6
|
+
function randomSeed() {
|
|
7
|
+
return crypto.getRandomValues(new Uint32Array(1))[0];
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* One online Head Clash match, run by the room one tick at a time. Slot 0 plays player 0 (left side, defends the left
|
|
11
|
+
* goal) and slot 1 player 1. The room steps exactly the simulation a browser runs offline.
|
|
12
|
+
*/
|
|
13
|
+
export class HeadClashRoomMatch {
|
|
14
|
+
match;
|
|
15
|
+
tick = 0;
|
|
16
|
+
/** Events since the previous state, stamped with the tick they happened on. */
|
|
17
|
+
events = [];
|
|
18
|
+
outcome = null;
|
|
19
|
+
constructor(members, options = {}) {
|
|
20
|
+
const characters = [DEFAULT_CHARACTER, DEFAULT_CHARACTER];
|
|
21
|
+
for (const member of members) {
|
|
22
|
+
if (member.slot !== 0 && member.slot !== 1) {
|
|
23
|
+
throw new RangeError(`HeadClashRoomMatch: a match has slots 0 and 1, got ${member.slot}`);
|
|
24
|
+
}
|
|
25
|
+
characters[member.slot] = member.profile.character ?? DEFAULT_CHARACTER;
|
|
26
|
+
}
|
|
27
|
+
this.match = createMatch({
|
|
28
|
+
seed: options.seed ?? randomSeed(),
|
|
29
|
+
characters,
|
|
30
|
+
...(options.matchSeconds === undefined ? {} : { matchSeconds: options.matchSeconds })
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
/** The match as the room runs it. */
|
|
34
|
+
get matchState() {
|
|
35
|
+
return this.match;
|
|
36
|
+
}
|
|
37
|
+
get over() {
|
|
38
|
+
return this.outcome !== null;
|
|
39
|
+
}
|
|
40
|
+
step(inputs) {
|
|
41
|
+
if (this.outcome) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const left = decodeInput(inputs.get(0) ?? IDLE_INPUT);
|
|
45
|
+
const right = decodeInput(inputs.get(1) ?? IDLE_INPUT);
|
|
46
|
+
const events = stepMatch(this.match, [left, right]);
|
|
47
|
+
this.tick++;
|
|
48
|
+
for (const event of events) {
|
|
49
|
+
this.events.push({ tick: this.tick, event });
|
|
50
|
+
}
|
|
51
|
+
if (this.match.phase === 'over') {
|
|
52
|
+
this.outcome = { winner: this.match.winner, reason: 'finished', score: [this.match.score[0], this.match.score[1]] };
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
state() {
|
|
56
|
+
const snapshot = snapshotOf(this.match, this.tick, this.events);
|
|
57
|
+
this.events = [];
|
|
58
|
+
return snapshot;
|
|
59
|
+
}
|
|
60
|
+
/** The player in `slot` left for good, and the other side wins by forfeit. */
|
|
61
|
+
leave(slot) {
|
|
62
|
+
if (this.outcome) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const winner = slot === 0 ? 1 : 0;
|
|
66
|
+
this.outcome = { winner, reason: 'forfeit', score: [this.match.score[0], this.match.score[1]] };
|
|
67
|
+
}
|
|
68
|
+
result() {
|
|
69
|
+
return this.outcome;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { MatchState, PlayerState } from './state.ts';
|
|
2
|
+
import type { AnimState } from './types.ts';
|
|
3
|
+
export declare function animOf(state: Readonly<MatchState>, p: Readonly<PlayerState>): AnimState;
|
|
4
|
+
/** Sets each player's animation; the clock of an animation restarts whenever the state changes. */
|
|
5
|
+
export declare function updateAnims(state: MatchState): void;
|
|
6
|
+
/** Seconds a player has spent in their current animation state. */
|
|
7
|
+
export declare function animTime(p: Readonly<PlayerState>): number;
|
package/dist/sim/anim.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { TICK_RATE } from "./constants.js";
|
|
2
|
+
/** Players moving slower than this stand idle. */
|
|
3
|
+
const RUN_MIN_SPEED = 40;
|
|
4
|
+
export function animOf(state, p) {
|
|
5
|
+
if (state.phase === 'over') {
|
|
6
|
+
return state.winner === null ? 'idle' : state.winner === p.side ? 'celebrate' : 'sad';
|
|
7
|
+
}
|
|
8
|
+
if (state.phase === 'goal') {
|
|
9
|
+
return state.lastScorer === p.side ? 'celebrate' : 'sad';
|
|
10
|
+
}
|
|
11
|
+
if (p.frozen) {
|
|
12
|
+
return 'idle';
|
|
13
|
+
}
|
|
14
|
+
if (p.specialTicks > 0 || p.dashTicks > 0) {
|
|
15
|
+
return 'special';
|
|
16
|
+
}
|
|
17
|
+
if (p.kickTicks > 0) {
|
|
18
|
+
return 'kick';
|
|
19
|
+
}
|
|
20
|
+
if (p.headerTicks > 0) {
|
|
21
|
+
return 'header';
|
|
22
|
+
}
|
|
23
|
+
if (!p.onGround) {
|
|
24
|
+
return 'jump';
|
|
25
|
+
}
|
|
26
|
+
return Math.abs(p.vx) > RUN_MIN_SPEED ? 'run' : 'idle';
|
|
27
|
+
}
|
|
28
|
+
/** Sets each player's animation; the clock of an animation restarts whenever the state changes. */
|
|
29
|
+
export function updateAnims(state) {
|
|
30
|
+
for (const p of state.players) {
|
|
31
|
+
const anim = animOf(state, p);
|
|
32
|
+
if (anim === p.anim) {
|
|
33
|
+
p.animTicks++;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
p.anim = anim;
|
|
37
|
+
p.animTicks = 0;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/** Seconds a player has spent in their current animation state. */
|
|
42
|
+
export function animTime(p) {
|
|
43
|
+
return p.animTicks / TICK_RATE;
|
|
44
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { SimEvent } from './events.ts';
|
|
2
|
+
import type { BallState, MatchState } from './state.ts';
|
|
3
|
+
export declare function newBall(): BallState;
|
|
4
|
+
/** The ball hangs at the centre for a kickoff, with no shot and nobody's touch. */
|
|
5
|
+
export declare function resetBall(ball: BallState): void;
|
|
6
|
+
/** Ends whatever shot the ball is (fire, curve, ice, ghost, thunder): it is a plain ball again. */
|
|
7
|
+
export declare function clearMode(ball: BallState): void;
|
|
8
|
+
/** One tick of the ball in play: forces, moves and collisions in sub-steps, then drag and the shot's clock. */
|
|
9
|
+
export declare function updateBall(state: MatchState, events: SimEvent[]): void;
|
package/dist/sim/ball.js
ADDED
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
import { BALL_AIR_DRAG, BALL_BAR_RESTITUTION, BALL_BODY_RESTITUTION, BALL_BOUNCY_RESTITUTION, BALL_DROP_Y, BALL_GRAVITY, BALL_GROUND_RESTITUTION, BALL_HEAD_RESTITUTION, BALL_MAX_SPEED, BALL_RADIUS, BALL_ROLL_FRICTION, BALL_SUBSTEPS, BALL_WALL_RESTITUTION, BAR_ROLL_OFF_SPEED, BOUNCE_EVENT_SPEED, CEILING_Y, CENTER_X, CURVE_ACCEL, CURVE_PERIOD_TICKS, DASH_BALL_VX, DASH_BALL_VY, DT, ENERGY_HEADER, ENERGY_KICK, ENERGY_TOUCH, GROUND_Y, HEADER_FORWARD, HEADER_MIN_FORWARD, HEADER_MIN_SPEED, HEADER_PUSH, HEADER_TICKS, KICK_CARRY, KICK_VX, KICK_VY, KICK_VY_HIGH, MAGNET_ACCEL, MAGNET_RANGE, THUNDER_DOWN_VX, THUNDER_DOWN_VY, THUNDER_IMPACT_RESTITUTION, THUNDER_STUN_RADIUS, SQUEEZE_POP_SPEED, THUNDER_STUN_TICKS, TOUCH_COOLDOWN_TICKS, WIND_ACCEL, WORLD_W } from "./constants.js";
|
|
2
|
+
import { bodyCircle, chestCircle, circlesOverlap, footCircle, headCircle, kickActive } from "./geometry.js";
|
|
3
|
+
import { clampToField, firstWallHit, keepInFrontOfShield, overlapsWall, resolveInside, wallsOf } from "./walls.js";
|
|
4
|
+
import { anyEffect, consumeEffect, hasEffect } from "./powerups.js";
|
|
5
|
+
import { addEnergy, fireSpecial, freezePlayer, knockBack, launchShot } from "./specials.js";
|
|
6
|
+
/** A shot passes through the player who launched it for this many ticks, so it leaves their foot cleanly. */
|
|
7
|
+
const SHOT_GRACE_TICKS = 12;
|
|
8
|
+
/** A ball whose centre is this far above the feet is struck flatter. */
|
|
9
|
+
const KNEE_HEIGHT = 70;
|
|
10
|
+
/** A head contact whose normal points up more steeply than this is a header. */
|
|
11
|
+
const HEADER_TOP_NY = -0.35;
|
|
12
|
+
/** A bounce slower than this leaves the ball resting on the ground. */
|
|
13
|
+
const REST_SPEED = 40;
|
|
14
|
+
export function newBall() {
|
|
15
|
+
return {
|
|
16
|
+
x: CENTER_X,
|
|
17
|
+
y: BALL_DROP_Y,
|
|
18
|
+
vx: 0,
|
|
19
|
+
vy: 0,
|
|
20
|
+
r: BALL_RADIUS,
|
|
21
|
+
lastToucher: null,
|
|
22
|
+
mode: 'none',
|
|
23
|
+
modeTicks: 0,
|
|
24
|
+
modeOwner: null,
|
|
25
|
+
modeAge: 0
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/** The ball hangs at the centre for a kickoff, with no shot and nobody's touch. */
|
|
29
|
+
export function resetBall(ball) {
|
|
30
|
+
ball.x = CENTER_X;
|
|
31
|
+
ball.y = BALL_DROP_Y;
|
|
32
|
+
ball.vx = 0;
|
|
33
|
+
ball.vy = 0;
|
|
34
|
+
ball.lastToucher = null;
|
|
35
|
+
clearMode(ball);
|
|
36
|
+
}
|
|
37
|
+
/** Ends whatever shot the ball is (fire, curve, ice, ghost, thunder): it is a plain ball again. */
|
|
38
|
+
export function clearMode(ball) {
|
|
39
|
+
ball.mode = 'none';
|
|
40
|
+
ball.modeTicks = 0;
|
|
41
|
+
ball.modeOwner = null;
|
|
42
|
+
ball.modeAge = 0;
|
|
43
|
+
}
|
|
44
|
+
/** A sine-like triangle wave over one period, [0, 1) to [-1, 1]; arithmetic only, so every engine agrees. */
|
|
45
|
+
function wave(phase) {
|
|
46
|
+
const p = phase - Math.floor(phase);
|
|
47
|
+
return p < 0.25 ? 4 * p : p < 0.75 ? 2 - 4 * p : 4 * p - 4;
|
|
48
|
+
}
|
|
49
|
+
function applyForces(state, h) {
|
|
50
|
+
const ball = state.ball;
|
|
51
|
+
let ax = 0;
|
|
52
|
+
let ay = BALL_GRAVITY;
|
|
53
|
+
if (ball.mode === 'fire') {
|
|
54
|
+
ay = 0;
|
|
55
|
+
}
|
|
56
|
+
else if (ball.mode === 'curve') {
|
|
57
|
+
ay = CURVE_ACCEL * wave(ball.modeAge / CURVE_PERIOD_TICKS);
|
|
58
|
+
}
|
|
59
|
+
for (const effect of state.effects) {
|
|
60
|
+
if (effect.kind === 'magnet') {
|
|
61
|
+
const head = headCircle(state.players[effect.target]);
|
|
62
|
+
const dx = head.x - ball.x;
|
|
63
|
+
const dy = head.y - ball.y;
|
|
64
|
+
const d = Math.sqrt(dx * dx + dy * dy);
|
|
65
|
+
if (d > 1 && d < MAGNET_RANGE) {
|
|
66
|
+
ax += (dx / d) * MAGNET_ACCEL;
|
|
67
|
+
ay += (dy / d) * MAGNET_ACCEL;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
else if (effect.kind === 'wind') {
|
|
71
|
+
// Toward the goal the collector attacks.
|
|
72
|
+
ax += (effect.owner === 0 ? 1 : -1) * WIND_ACCEL;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
ball.vx += ax * h;
|
|
76
|
+
ball.vy += ay * h;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Pushes the ball out of a surface whose outward normal at the contact is (nx, ny) and, if it moved into it relative
|
|
80
|
+
* to the surface's own velocity, bounces it with `restitution`.
|
|
81
|
+
*/
|
|
82
|
+
function bounce(ball, nx, ny, svx, svy, restitution) {
|
|
83
|
+
const rvx = ball.vx - svx;
|
|
84
|
+
const rvy = ball.vy - svy;
|
|
85
|
+
const vn = rvx * nx + rvy * ny;
|
|
86
|
+
if (vn >= 0) {
|
|
87
|
+
return { bounced: false, approach: 0, nx, ny };
|
|
88
|
+
}
|
|
89
|
+
ball.vx = svx + rvx - (1 + restitution) * vn * nx;
|
|
90
|
+
ball.vy = svy + rvy - (1 + restitution) * vn * ny;
|
|
91
|
+
return { bounced: true, approach: -vn, nx, ny };
|
|
92
|
+
}
|
|
93
|
+
/** Ball against a moving circle. Null when they do not touch. */
|
|
94
|
+
function hitCircle(ball, c, cvx, cvy, restitution) {
|
|
95
|
+
const dx = ball.x - c.x;
|
|
96
|
+
const dy = ball.y - c.y;
|
|
97
|
+
const reach = ball.r + c.r;
|
|
98
|
+
const d2 = dx * dx + dy * dy;
|
|
99
|
+
if (d2 >= reach * reach) {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
const d = Math.sqrt(d2);
|
|
103
|
+
const nx = d > 1e-6 ? dx / d : 0;
|
|
104
|
+
const ny = d > 1e-6 ? dy / d : -1;
|
|
105
|
+
ball.x = c.x + nx * reach;
|
|
106
|
+
ball.y = c.y + ny * reach;
|
|
107
|
+
return bounce(ball, nx, ny, cvx, cvy, restitution);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Moves the ball's centre from (x0, y0) to where it is now without crossing any thin wall (crossbars, active shields):
|
|
111
|
+
* at the first contact it stops on the wall, bounces off it, and slides along it for the rest of the move. A centre
|
|
112
|
+
* that starts inside a wall (the wall changed under it) leaves it first. Returns the last contact, or null.
|
|
113
|
+
*/
|
|
114
|
+
function sweepMove(state, events, x0, y0) {
|
|
115
|
+
const ball = state.ball;
|
|
116
|
+
const walls = wallsOf(state);
|
|
117
|
+
const wallRestitution = anyEffect(state, 'bouncyBall') ? BALL_BOUNCY_RESTITUTION : BALL_WALL_RESTITUTION;
|
|
118
|
+
let targetX = ball.x;
|
|
119
|
+
let targetY = ball.y;
|
|
120
|
+
let fromX = x0;
|
|
121
|
+
let fromY = y0;
|
|
122
|
+
let last = null;
|
|
123
|
+
for (const wall of walls) {
|
|
124
|
+
if (overlapsWall(wall.rect, fromX, fromY, ball.r)) {
|
|
125
|
+
ball.x = fromX;
|
|
126
|
+
ball.y = fromY;
|
|
127
|
+
const n = resolveInside(ball, wall);
|
|
128
|
+
// The rest of the move goes on from outside the wall, without its part into the wall.
|
|
129
|
+
const into = (targetX - fromX) * n.nx + (targetY - fromY) * n.ny;
|
|
130
|
+
targetX = ball.x + (targetX - fromX) - Math.min(0, into) * n.nx;
|
|
131
|
+
targetY = ball.y + (targetY - fromY) - Math.min(0, into) * n.ny;
|
|
132
|
+
fromX = ball.x;
|
|
133
|
+
fromY = ball.y;
|
|
134
|
+
wallContact(state, events, { wall, t: 0, x: ball.x, y: ball.y, nx: n.nx, ny: n.ny }, wallRestitution);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
for (let slide = 0; slide < 4; slide++) {
|
|
138
|
+
const dx = targetX - fromX;
|
|
139
|
+
const dy = targetY - fromY;
|
|
140
|
+
const hit = firstWallHit(walls, ball.r, fromX, fromY, dx, dy);
|
|
141
|
+
if (!hit) {
|
|
142
|
+
ball.x = targetX;
|
|
143
|
+
ball.y = targetY;
|
|
144
|
+
return last;
|
|
145
|
+
}
|
|
146
|
+
// Rest on the wall, a hair outside it, so the next move starts outside.
|
|
147
|
+
ball.x = hit.x + hit.nx * 1e-6;
|
|
148
|
+
ball.y = hit.y + hit.ny * 1e-6;
|
|
149
|
+
wallContact(state, events, hit, wallRestitution);
|
|
150
|
+
last = hit;
|
|
151
|
+
const restX = (1 - hit.t) * dx;
|
|
152
|
+
const restY = (1 - hit.t) * dy;
|
|
153
|
+
const along = restX * hit.nx + restY * hit.ny;
|
|
154
|
+
fromX = ball.x;
|
|
155
|
+
fromY = ball.y;
|
|
156
|
+
targetX = fromX + restX - along * hit.nx;
|
|
157
|
+
targetY = fromY + restY - along * hit.ny;
|
|
158
|
+
}
|
|
159
|
+
return last;
|
|
160
|
+
}
|
|
161
|
+
/** What touching a wall does: the bounce, the crossbar's sound and roll-off, the shield's sound, a shot ending. */
|
|
162
|
+
function wallContact(state, events, hit, wallRestitution) {
|
|
163
|
+
const ball = state.ball;
|
|
164
|
+
if (hit.wall.kind === 'bar') {
|
|
165
|
+
const contact = bounce(ball, hit.nx, hit.ny, 0, 0, BALL_BAR_RESTITUTION);
|
|
166
|
+
if (contact.approach >= BOUNCE_EVENT_SPEED) {
|
|
167
|
+
events.push({ type: 'post', goal: hit.wall.side, x: ball.x, y: ball.y, speed: contact.approach });
|
|
168
|
+
}
|
|
169
|
+
// A ball on top of the bar rolls off toward the field instead of resting there.
|
|
170
|
+
const toField = hit.wall.side === 0 ? 1 : -1;
|
|
171
|
+
if (hit.ny < -0.7 && ball.vx * toField < BAR_ROLL_OFF_SPEED) {
|
|
172
|
+
ball.vx = toField * BAR_ROLL_OFF_SPEED;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
const contact = bounce(ball, hit.nx, hit.ny, 0, 0, wallRestitution);
|
|
177
|
+
pushBounce(events, ball, 'shield', contact.approach);
|
|
178
|
+
// Like the crossbar, a shield's top never holds a ball: it rolls off toward the field.
|
|
179
|
+
const toField = hit.wall.side === 0 ? 1 : -1;
|
|
180
|
+
if (hit.ny < -0.7 && ball.vx * toField < BAR_ROLL_OFF_SPEED) {
|
|
181
|
+
ball.vx = toField * BAR_ROLL_OFF_SPEED;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
endFlightShot(ball);
|
|
185
|
+
}
|
|
186
|
+
function pushBounce(events, ball, surface, speed) {
|
|
187
|
+
if (speed >= BOUNCE_EVENT_SPEED) {
|
|
188
|
+
events.push({ type: 'bounce', surface, x: ball.x, y: ball.y, speed });
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
function stun(p) {
|
|
192
|
+
p.stunTicks = Math.max(p.stunTicks, THUNDER_STUN_TICKS);
|
|
193
|
+
p.kickTicks = 0;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* The slam of a thunder drop: the ball comes down hard, on the grass or on an opponent's head, and stuns every
|
|
197
|
+
* opponent close to where it lands.
|
|
198
|
+
*/
|
|
199
|
+
function thunderImpact(state, events, speed, surface) {
|
|
200
|
+
const ball = state.ball;
|
|
201
|
+
for (const p of state.players) {
|
|
202
|
+
if (p.side !== ball.modeOwner && Math.abs(p.x - ball.x) <= THUNDER_STUN_RADIUS) {
|
|
203
|
+
stun(p);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
events.push({ type: 'bounce', surface, x: ball.x, y: ball.y, speed, thunder: true });
|
|
207
|
+
}
|
|
208
|
+
/** The ball against the field's own bounds: the grass, the ceiling and the side walls. Goal frames are `sweepMove`'s. */
|
|
209
|
+
function collideWorld(state, events) {
|
|
210
|
+
const ball = state.ball;
|
|
211
|
+
const bouncy = anyEffect(state, 'bouncyBall');
|
|
212
|
+
const wallRestitution = bouncy ? BALL_BOUNCY_RESTITUTION : BALL_WALL_RESTITUTION;
|
|
213
|
+
if (ball.y + ball.r > GROUND_Y) {
|
|
214
|
+
ball.y = GROUND_Y - ball.r;
|
|
215
|
+
if (ball.vy > 0) {
|
|
216
|
+
const speed = ball.vy;
|
|
217
|
+
if (ball.mode === 'thunderDown') {
|
|
218
|
+
thunderImpact(state, events, speed, 'ground');
|
|
219
|
+
ball.vy = -speed * THUNDER_IMPACT_RESTITUTION;
|
|
220
|
+
clearMode(ball);
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
ball.vy = -speed * (bouncy ? BALL_BOUNCY_RESTITUTION : BALL_GROUND_RESTITUTION);
|
|
224
|
+
if (ball.vy > -REST_SPEED) {
|
|
225
|
+
ball.vy = 0;
|
|
226
|
+
}
|
|
227
|
+
pushBounce(events, ball, 'ground', speed);
|
|
228
|
+
if (ball.mode === 'curve') {
|
|
229
|
+
clearMode(ball);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
if (ball.y - ball.r < CEILING_Y) {
|
|
235
|
+
ball.y = CEILING_Y + ball.r;
|
|
236
|
+
if (ball.vy < 0) {
|
|
237
|
+
pushBounce(events, ball, 'ceiling', -ball.vy);
|
|
238
|
+
ball.vy = -ball.vy * wallRestitution;
|
|
239
|
+
endFlightShot(ball);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
if (ball.x - ball.r < 0 && ball.vx <= 0) {
|
|
243
|
+
ball.x = ball.r;
|
|
244
|
+
pushBounce(events, ball, 'wall', -ball.vx);
|
|
245
|
+
ball.vx = -ball.vx * wallRestitution;
|
|
246
|
+
endFlightShot(ball);
|
|
247
|
+
}
|
|
248
|
+
else if (ball.x + ball.r > WORLD_W && ball.vx >= 0) {
|
|
249
|
+
ball.x = WORLD_W - ball.r;
|
|
250
|
+
pushBounce(events, ball, 'wall', ball.vx);
|
|
251
|
+
ball.vx = -ball.vx * wallRestitution;
|
|
252
|
+
endFlightShot(ball);
|
|
253
|
+
}
|
|
254
|
+
// Squeezed by a player against a wall or the ground, the ball still never leaves the field.
|
|
255
|
+
clampToField(ball);
|
|
256
|
+
}
|
|
257
|
+
/** Shots that fly on a path of their own end when they hit the frame of the field. */
|
|
258
|
+
function endFlightShot(ball) {
|
|
259
|
+
if (ball.mode === 'fire' || ball.mode === 'curve' || ball.mode === 'thunderUp') {
|
|
260
|
+
clearMode(ball);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
/** The foot of `p` strikes the ball: an armed special, a fire shot, or a plain kick up and forward. */
|
|
264
|
+
function kickBall(state, p, events) {
|
|
265
|
+
const ball = state.ball;
|
|
266
|
+
p.kickHit = true;
|
|
267
|
+
ball.lastToucher = p.side;
|
|
268
|
+
if (p.specialArmed) {
|
|
269
|
+
fireSpecial(state, p.side, events);
|
|
270
|
+
}
|
|
271
|
+
else if (hasEffect(state, 'fireShot', p.side)) {
|
|
272
|
+
launchShot(state, p.side, 'fire');
|
|
273
|
+
consumeEffect(state, 'fireShot', p.side, events);
|
|
274
|
+
addEnergy(p, ENERGY_KICK);
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
const high = ball.y < p.y - KNEE_HEIGHT;
|
|
278
|
+
ball.vx = p.facing * KICK_VX + p.vx * KICK_CARRY;
|
|
279
|
+
ball.vy = -(high ? KICK_VY_HIGH : KICK_VY);
|
|
280
|
+
if (ball.mode !== 'ghost') {
|
|
281
|
+
clearMode(ball);
|
|
282
|
+
}
|
|
283
|
+
addEnergy(p, ENERGY_KICK);
|
|
284
|
+
}
|
|
285
|
+
events.push({ type: 'kick', player: p.side, x: ball.x, y: ball.y, speed: Math.sqrt(ball.vx * ball.vx + ball.vy * ball.vy) });
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* What a shot does to a player it reaches: a fireball knocks an opponent back, an ice shot freezes them, a thunder slam
|
|
289
|
+
* stuns them.
|
|
290
|
+
*/
|
|
291
|
+
function shotMeetsPlayer(state, p, events) {
|
|
292
|
+
const ball = state.ball;
|
|
293
|
+
const owner = ball.modeOwner;
|
|
294
|
+
if (ball.mode === 'fire' && owner !== p.side) {
|
|
295
|
+
knockBack(p, ball.x);
|
|
296
|
+
}
|
|
297
|
+
else if (ball.mode === 'ice' && owner !== null && owner !== p.side) {
|
|
298
|
+
freezePlayer(state, p.side, owner, events);
|
|
299
|
+
}
|
|
300
|
+
else if (ball.mode === 'thunderDown' && owner !== p.side) {
|
|
301
|
+
thunderImpact(state, events, Math.abs(ball.vy), 'player');
|
|
302
|
+
}
|
|
303
|
+
if (ball.mode !== 'none' && ball.mode !== 'ghost') {
|
|
304
|
+
clearMode(ball);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
/** Whether the ball touches `side`'s player, which then plays it. */
|
|
308
|
+
function collideWithPlayer(state, side, events) {
|
|
309
|
+
const ball = state.ball;
|
|
310
|
+
const p = state.players[side];
|
|
311
|
+
if (ball.mode !== 'none' && ball.modeOwner === side && ball.modeAge < SHOT_GRACE_TICKS) {
|
|
312
|
+
return false;
|
|
313
|
+
}
|
|
314
|
+
const head = headCircle(p);
|
|
315
|
+
const chest = chestCircle(p);
|
|
316
|
+
const body = bodyCircle(p);
|
|
317
|
+
const foot = footCircle(p);
|
|
318
|
+
if (!circlesOverlap(ball, head) && !circlesOverlap(ball, chest) && !circlesOverlap(ball, body) && !circlesOverlap(ball, foot)) {
|
|
319
|
+
return false;
|
|
320
|
+
}
|
|
321
|
+
if (p.dashTicks > 0 && !p.dashHitBall) {
|
|
322
|
+
// The titan's charge smashes the ball forward.
|
|
323
|
+
p.dashHitBall = true;
|
|
324
|
+
ball.lastToucher = side;
|
|
325
|
+
clearMode(ball);
|
|
326
|
+
ball.vx = p.facing * DASH_BALL_VX;
|
|
327
|
+
ball.vy = DASH_BALL_VY;
|
|
328
|
+
events.push({ type: 'header', player: side, x: ball.x, y: ball.y, speed: DASH_BALL_VX });
|
|
329
|
+
return true;
|
|
330
|
+
}
|
|
331
|
+
if (kickActive(p) && !p.kickHit && circlesOverlap(ball, foot) && !p.frozen && p.stunTicks === 0 && state.phase === 'play') {
|
|
332
|
+
kickBall(state, p, events);
|
|
333
|
+
return true;
|
|
334
|
+
}
|
|
335
|
+
// A shot only acts in play: an ice ball bouncing out of the net during the goal pause freezes nobody.
|
|
336
|
+
if (state.phase === 'play') {
|
|
337
|
+
shotMeetsPlayer(state, p, events);
|
|
338
|
+
}
|
|
339
|
+
if (p.specialArmed && state.phase === 'play' && !p.frozen && p.stunTicks === 0) {
|
|
340
|
+
ball.lastToucher = side;
|
|
341
|
+
fireSpecial(state, side, events);
|
|
342
|
+
return true;
|
|
343
|
+
}
|
|
344
|
+
let touched = false;
|
|
345
|
+
let bounced = 0;
|
|
346
|
+
const headHit = hitCircle(ball, head, p.vx, p.vy, BALL_HEAD_RESTITUTION);
|
|
347
|
+
if (headHit) {
|
|
348
|
+
touched = true;
|
|
349
|
+
const airborne = !p.onGround;
|
|
350
|
+
// Only the top of the head heads the ball; its side is just a bump.
|
|
351
|
+
const onTop = headHit.ny < HEADER_TOP_NY;
|
|
352
|
+
// Headers (an aimed ball, energy, the event) happen only in play; in a pause the head is just a bump.
|
|
353
|
+
if (state.phase === 'play' && headHit.bounced && onTop && (headHit.approach >= HEADER_MIN_SPEED || airborne)) {
|
|
354
|
+
// A header is aimed: it lifts the ball and always sends it on toward the opponent's goal.
|
|
355
|
+
const push = HEADER_PUSH * (airborne ? 1 : 0.5);
|
|
356
|
+
ball.vy += headHit.ny * push;
|
|
357
|
+
ball.vx = p.facing * Math.max(ball.vx * p.facing + HEADER_FORWARD, HEADER_MIN_FORWARD);
|
|
358
|
+
if (p.headerTicks === 0) {
|
|
359
|
+
p.headerTicks = HEADER_TICKS;
|
|
360
|
+
addEnergy(p, ENERGY_HEADER);
|
|
361
|
+
events.push({ type: 'header', player: side, x: ball.x, y: ball.y, speed: headHit.approach });
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
else if (headHit.bounced) {
|
|
365
|
+
bounced = Math.max(bounced, headHit.approach);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
for (const part of [chest, body, foot]) {
|
|
369
|
+
const hit = hitCircle(ball, part, p.vx, p.vy, BALL_BODY_RESTITUTION);
|
|
370
|
+
if (hit) {
|
|
371
|
+
touched = true;
|
|
372
|
+
if (hit.bounced) {
|
|
373
|
+
bounced = Math.max(bounced, hit.approach);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
if (touched && state.phase === 'play') {
|
|
378
|
+
ball.lastToucher = side;
|
|
379
|
+
// A dribble touches the ball every few ticks: it earns energy and makes a sound only now and then.
|
|
380
|
+
if (p.touchCooldown === 0) {
|
|
381
|
+
addEnergy(p, ENERGY_TOUCH);
|
|
382
|
+
p.touchCooldown = TOUCH_COOLDOWN_TICKS;
|
|
383
|
+
pushBounce(events, ball, 'player', bounced);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
return touched;
|
|
387
|
+
}
|
|
388
|
+
/** Shots age; a thunder lob turns into its slam at the top of its flight; a mode ends when its time is up. */
|
|
389
|
+
function advanceMode(ball) {
|
|
390
|
+
if (ball.mode === 'none') {
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
ball.modeAge++;
|
|
394
|
+
ball.modeTicks--;
|
|
395
|
+
if (ball.mode === 'thunderUp' && ball.vy >= 0) {
|
|
396
|
+
ball.mode = 'thunderDown';
|
|
397
|
+
ball.vx = (ball.modeOwner === 0 ? 1 : -1) * THUNDER_DOWN_VX;
|
|
398
|
+
ball.vy = THUNDER_DOWN_VY;
|
|
399
|
+
}
|
|
400
|
+
if (ball.modeTicks <= 0) {
|
|
401
|
+
clearMode(ball);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
/** Whether the ball overlaps either player's head, chest, body or foot. */
|
|
405
|
+
function touchesPlayer(state) {
|
|
406
|
+
const ball = state.ball;
|
|
407
|
+
return state.players.some(p => [headCircle(p), chestCircle(p), bodyCircle(p), footCircle(p)].some(c => circlesOverlap(ball, c)));
|
|
408
|
+
}
|
|
409
|
+
/** One tick of the ball in play: forces, moves and collisions in sub-steps, then drag and the shot's clock. */
|
|
410
|
+
export function updateBall(state, events) {
|
|
411
|
+
const ball = state.ball;
|
|
412
|
+
const h = DT / BALL_SUBSTEPS;
|
|
413
|
+
for (let i = 0; i < BALL_SUBSTEPS; i++) {
|
|
414
|
+
if (state.phase === 'play') {
|
|
415
|
+
keepInFrontOfShield(state, 0);
|
|
416
|
+
keepInFrontOfShield(state, 1);
|
|
417
|
+
}
|
|
418
|
+
applyForces(state, h);
|
|
419
|
+
const x0 = ball.x;
|
|
420
|
+
const y0 = ball.y;
|
|
421
|
+
ball.x += ball.vx * h;
|
|
422
|
+
ball.y += ball.vy * h;
|
|
423
|
+
sweepMove(state, events, x0, y0);
|
|
424
|
+
collideWorld(state, events);
|
|
425
|
+
const pushedFromX = ball.x;
|
|
426
|
+
const pushedFromY = ball.y;
|
|
427
|
+
const left = collideWithPlayer(state, 0, events);
|
|
428
|
+
const right = collideWithPlayer(state, 1, events);
|
|
429
|
+
if (left && right && ball.mode === 'none') {
|
|
430
|
+
// Wedged between both players: it squirts up and out to one side.
|
|
431
|
+
const mid = (state.players[0].x + state.players[1].x) / 2;
|
|
432
|
+
const away = ball.x === mid ? (state.tick % 2 === 0 ? 1 : -1) : Math.sign(ball.x - mid);
|
|
433
|
+
ball.vy = Math.min(ball.vy, -SQUEEZE_POP_SPEED);
|
|
434
|
+
ball.vx = away * SQUEEZE_POP_SPEED * 0.4;
|
|
435
|
+
}
|
|
436
|
+
if (left || right) {
|
|
437
|
+
// A push moves the ball too: swept like a flight, it can never carry the ball through a frame. The field bounds
|
|
438
|
+
// come first, so the swept path is the one the ball really takes (not one under the grass, lifted into a wall).
|
|
439
|
+
clampToField(ball);
|
|
440
|
+
const wall = sweepMove(state, events, pushedFromX, pushedFromY);
|
|
441
|
+
if (wall && Math.abs(wall.nx) > 0.7 && touchesPlayer(state)) {
|
|
442
|
+
// Wedged between a player and a shield or the end of a bar: it squirts up, as between two players.
|
|
443
|
+
ball.vy = Math.min(ball.vy, -SQUEEZE_POP_SPEED);
|
|
444
|
+
}
|
|
445
|
+
clampToField(ball);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
if (ball.mode !== 'fire' && ball.mode !== 'curve') {
|
|
449
|
+
ball.vx *= BALL_AIR_DRAG;
|
|
450
|
+
ball.vy *= BALL_AIR_DRAG;
|
|
451
|
+
}
|
|
452
|
+
if (ball.y + ball.r >= GROUND_Y - 0.5 && Math.abs(ball.vy) < 1) {
|
|
453
|
+
ball.vx *= BALL_ROLL_FRICTION;
|
|
454
|
+
if (Math.abs(ball.vx) < 2) {
|
|
455
|
+
ball.vx = 0;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
const speed = Math.sqrt(ball.vx * ball.vx + ball.vy * ball.vy);
|
|
459
|
+
if (speed > BALL_MAX_SPEED) {
|
|
460
|
+
ball.vx *= BALL_MAX_SPEED / speed;
|
|
461
|
+
ball.vy *= BALL_MAX_SPEED / speed;
|
|
462
|
+
}
|
|
463
|
+
advanceMode(ball);
|
|
464
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Rng } from './rng.ts';
|
|
2
|
+
import type { MatchState } from './state.ts';
|
|
3
|
+
import type { Difficulty, InputFrame, Side } from './types.ts';
|
|
4
|
+
/** The keys the bot playing `side` at `difficulty` presses this tick. */
|
|
5
|
+
export declare function botInput(state: Readonly<MatchState>, side: Side, difficulty: Difficulty, rng: Rng): InputFrame;
|