@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,189 @@
|
|
|
1
|
+
import { BALL_RADIUS, BALL_RADIUS_BIG, BALL_RADIUS_SMALL, CROSSBAR_Y, CROSSBAR_Y_BIG, CROSSBAR_Y_SMALL, DOUBLE_GOAL_TICKS, EFFECT_TICKS, FREEZE_TICKS, GOAL_DEPTH, GOAL_DEPTH_BIG, HEAD_SCALE_BIG, HEAD_SCALE_SMALL, HEAD_SCALE_TITAN, POWERUP_FALL_SPEED, POWERUP_LIFETIME_TICKS, POWERUP_MAX_ON_FIELD, POWERUP_RADIUS, POWERUP_REST_Y, POWERUP_SPAWN_MAX_TICKS, POWERUP_SPAWN_MAX_X, POWERUP_SPAWN_MIN_TICKS, POWERUP_SPAWN_MIN_X, POWERUP_SPAWN_Y, REVERSE_TICKS, SHIELD_TICKS, DT } from "./constants.js";
|
|
2
|
+
import { bodyCircle, chestCircle, circlesOverlap, headCircle } from "./geometry.js";
|
|
3
|
+
import { other, POWERUP_KINDS } from "./types.js";
|
|
4
|
+
import { settleFrameChange } from "./walls.js";
|
|
5
|
+
/** Kinds that act on the opponent (or the opponent's goal); every other kind acts on its collector or on the ball. */
|
|
6
|
+
const ON_FOE = new Set(['shrinkFoe', 'bigGoalFoe', 'freezeFoe', 'reverseFoe']);
|
|
7
|
+
/** Kinds that replace each other: the newest head size, goal size or ball size wins. */
|
|
8
|
+
const GROUP = {
|
|
9
|
+
bigHead: 'head',
|
|
10
|
+
shrinkFoe: 'head',
|
|
11
|
+
bigGoalFoe: 'goal',
|
|
12
|
+
smallGoalSelf: 'goal',
|
|
13
|
+
bigBall: 'ball',
|
|
14
|
+
smallBall: 'ball'
|
|
15
|
+
};
|
|
16
|
+
/** The player an effect of `kind` collected by `owner` acts on. Ball effects are kept on their collector. */
|
|
17
|
+
export function effectTarget(kind, owner) {
|
|
18
|
+
return ON_FOE.has(kind) ? other(owner) : owner;
|
|
19
|
+
}
|
|
20
|
+
export function effectDuration(kind) {
|
|
21
|
+
switch (kind) {
|
|
22
|
+
case 'freezeFoe':
|
|
23
|
+
return FREEZE_TICKS;
|
|
24
|
+
case 'shield':
|
|
25
|
+
return SHIELD_TICKS;
|
|
26
|
+
case 'reverseFoe':
|
|
27
|
+
return REVERSE_TICKS;
|
|
28
|
+
case 'doubleGoal':
|
|
29
|
+
return DOUBLE_GOAL_TICKS;
|
|
30
|
+
default:
|
|
31
|
+
return EFFECT_TICKS;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export function effectOn(state, kind, target) {
|
|
35
|
+
return state.effects.find(effect => effect.kind === kind && effect.target === target);
|
|
36
|
+
}
|
|
37
|
+
export function hasEffect(state, kind, target) {
|
|
38
|
+
return effectOn(state, kind, target) !== undefined;
|
|
39
|
+
}
|
|
40
|
+
/** Whether any effect of `kind` is at work, whoever it acts on. */
|
|
41
|
+
export function anyEffect(state, kind) {
|
|
42
|
+
return state.effects.some(effect => effect.kind === kind);
|
|
43
|
+
}
|
|
44
|
+
/** Removes the effects `remove` picks, telling the renderer each one ended. */
|
|
45
|
+
function removeEffects(state, remove, events) {
|
|
46
|
+
const kept = [];
|
|
47
|
+
for (const effect of state.effects) {
|
|
48
|
+
if (remove(effect)) {
|
|
49
|
+
events.push({ type: 'effectEnd', kind: effect.kind, owner: effect.owner, target: effect.target });
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
kept.push(effect);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
state.effects = kept;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Puts an effect of `kind` collected by `owner` to work on `target` for `ticks`. The same kind on the same target
|
|
59
|
+
* starts its timer again; a rival of its group (big head against shrink, big goal against small goal, big ball against
|
|
60
|
+
* small ball) is replaced.
|
|
61
|
+
*/
|
|
62
|
+
export function addEffect(state, kind, owner, target, ticks, events) {
|
|
63
|
+
const group = GROUP[kind];
|
|
64
|
+
removeEffects(state, effect => {
|
|
65
|
+
if (effect.kind === kind && effect.target === target) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
return group !== undefined && GROUP[effect.kind] === group && (group === 'ball' || effect.target === target);
|
|
69
|
+
}, events);
|
|
70
|
+
const same = effectOn(state, kind, target);
|
|
71
|
+
if (same) {
|
|
72
|
+
same.owner = owner;
|
|
73
|
+
same.ticks = Math.max(same.ticks, ticks);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
state.effects.push({ kind, owner, target, ticks });
|
|
77
|
+
}
|
|
78
|
+
refreshDerived(state);
|
|
79
|
+
}
|
|
80
|
+
/** `owner` collected a power-up of `kind`. */
|
|
81
|
+
export function applyPowerUp(state, kind, owner, events) {
|
|
82
|
+
addEffect(state, kind, owner, effectTarget(kind, owner), effectDuration(kind), events);
|
|
83
|
+
}
|
|
84
|
+
/** Ends the effect of `kind` on `target` early, because it was used up (a fire shot kicked, a double goal scored). */
|
|
85
|
+
export function consumeEffect(state, kind, target, events) {
|
|
86
|
+
removeEffects(state, effect => effect.kind === kind && effect.target === target, events);
|
|
87
|
+
refreshDerived(state);
|
|
88
|
+
}
|
|
89
|
+
/** Ends every effect of `kind` on either player, as if each ran out now. */
|
|
90
|
+
export function endEffects(state, kind, events) {
|
|
91
|
+
removeEffects(state, effect => effect.kind === kind, events);
|
|
92
|
+
refreshDerived(state);
|
|
93
|
+
}
|
|
94
|
+
/** One tick of every effect's timer; effects that ran out end. */
|
|
95
|
+
export function tickEffects(state, events) {
|
|
96
|
+
for (const effect of state.effects) {
|
|
97
|
+
effect.ticks--;
|
|
98
|
+
}
|
|
99
|
+
if (state.effects.some(effect => effect.ticks <= 0)) {
|
|
100
|
+
removeEffects(state, effect => effect.ticks <= 0, events);
|
|
101
|
+
refreshDerived(state);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
function headScaleOf(state, p) {
|
|
105
|
+
if (p.dashTicks > 0) {
|
|
106
|
+
return HEAD_SCALE_TITAN;
|
|
107
|
+
}
|
|
108
|
+
if (hasEffect(state, 'bigHead', p.side)) {
|
|
109
|
+
return HEAD_SCALE_BIG;
|
|
110
|
+
}
|
|
111
|
+
return hasEffect(state, 'shrinkFoe', p.side) ? HEAD_SCALE_SMALL : 1;
|
|
112
|
+
}
|
|
113
|
+
/** Recomputes everything the effects decide: head sizes, frozen players, goal sizes, shields and the ball's radius. */
|
|
114
|
+
export function refreshDerived(state) {
|
|
115
|
+
const before = { goals: state.goals.map(goal => ({ ...goal })), r: state.ball.r };
|
|
116
|
+
for (const p of state.players) {
|
|
117
|
+
p.headScale = headScaleOf(state, p);
|
|
118
|
+
p.frozen = hasEffect(state, 'freezeFoe', p.side);
|
|
119
|
+
}
|
|
120
|
+
for (const side of [0, 1]) {
|
|
121
|
+
const goal = state.goals[side];
|
|
122
|
+
const big = hasEffect(state, 'bigGoalFoe', side);
|
|
123
|
+
const small = hasEffect(state, 'smallGoalSelf', side);
|
|
124
|
+
goal.crossbarY = big ? CROSSBAR_Y_BIG : small ? CROSSBAR_Y_SMALL : CROSSBAR_Y;
|
|
125
|
+
goal.depth = big ? GOAL_DEPTH_BIG : GOAL_DEPTH;
|
|
126
|
+
goal.shield = hasEffect(state, 'shield', side);
|
|
127
|
+
}
|
|
128
|
+
state.ball.r = anyEffect(state, 'bigBall') ? BALL_RADIUS_BIG : anyEffect(state, 'smallBall') ? BALL_RADIUS_SMALL : BALL_RADIUS;
|
|
129
|
+
// A frame that changed shape around the ball never scores and never leaves it inside or behind a wall.
|
|
130
|
+
settleFrameChange(state, before);
|
|
131
|
+
}
|
|
132
|
+
/** Adds an item of `kind` at (x, y); it falls until it rests on the grass. */
|
|
133
|
+
export function spawnItem(state, kind, x, y = POWERUP_SPAWN_Y) {
|
|
134
|
+
const item = { id: state.nextItemId++, kind, x, y: Math.min(y, POWERUP_REST_Y), landed: y >= POWERUP_REST_Y, age: 0 };
|
|
135
|
+
state.items.push(item);
|
|
136
|
+
return item;
|
|
137
|
+
}
|
|
138
|
+
/** One tick of the spawn timer: an item of a random kind drops at a random x every 6 to 9 s, at most two at once. */
|
|
139
|
+
export function tickSpawner(state, rng, events) {
|
|
140
|
+
if (!state.powerUps) {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
state.nextItemTicks--;
|
|
144
|
+
if (state.nextItemTicks > 0) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
state.nextItemTicks = rng.int(POWERUP_SPAWN_MIN_TICKS, POWERUP_SPAWN_MAX_TICKS);
|
|
148
|
+
if (state.items.length >= POWERUP_MAX_ON_FIELD) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
const kind = POWERUP_KINDS[rng.int(0, POWERUP_KINDS.length - 1)];
|
|
152
|
+
const x = Math.round(rng.range(POWERUP_SPAWN_MIN_X, POWERUP_SPAWN_MAX_X));
|
|
153
|
+
const item = spawnItem(state, kind, x);
|
|
154
|
+
events.push({ type: 'powerupSpawn', id: item.id, kind, x });
|
|
155
|
+
}
|
|
156
|
+
/** Who collects `item` this tick: a player touching it, else the last toucher of a ball touching it. */
|
|
157
|
+
function collectorOf(state, item) {
|
|
158
|
+
const circle = { x: item.x, y: item.y, r: POWERUP_RADIUS };
|
|
159
|
+
for (const p of state.players) {
|
|
160
|
+
if (circlesOverlap(circle, headCircle(p)) || circlesOverlap(circle, chestCircle(p)) || circlesOverlap(circle, bodyCircle(p))) {
|
|
161
|
+
return { side: p.side, byBall: false };
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
const ball = state.ball;
|
|
165
|
+
if (ball.lastToucher !== null && circlesOverlap(circle, ball)) {
|
|
166
|
+
return { side: ball.lastToucher, byBall: true };
|
|
167
|
+
}
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
/** Items fall, age and vanish, and are collected. */
|
|
171
|
+
export function updateItems(state, events) {
|
|
172
|
+
const kept = [];
|
|
173
|
+
for (const item of state.items) {
|
|
174
|
+
item.age++;
|
|
175
|
+
if (!item.landed) {
|
|
176
|
+
item.y = Math.min(POWERUP_REST_Y, item.y + POWERUP_FALL_SPEED * DT);
|
|
177
|
+
item.landed = item.y >= POWERUP_REST_Y;
|
|
178
|
+
}
|
|
179
|
+
const collector = collectorOf(state, item);
|
|
180
|
+
if (collector) {
|
|
181
|
+
events.push({ type: 'powerupPick', id: item.id, kind: item.kind, player: collector.side, byBall: collector.byBall });
|
|
182
|
+
applyPowerUp(state, item.kind, collector.side, events);
|
|
183
|
+
}
|
|
184
|
+
else if (item.age < POWERUP_LIFETIME_TICKS) {
|
|
185
|
+
kept.push(item);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
state.items = kept;
|
|
189
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface Rng {
|
|
2
|
+
/** Uniform float in [0, 1). */
|
|
3
|
+
next: () => number;
|
|
4
|
+
/** Uniform float in [a, b). */
|
|
5
|
+
range: (a: number, b: number) => number;
|
|
6
|
+
/** Uniform integer in [a, b]. */
|
|
7
|
+
int: (a: number, b: number) => number;
|
|
8
|
+
/** Where the sequence stands: `createRng(rng.state())` continues it exactly. */
|
|
9
|
+
state: () => number;
|
|
10
|
+
}
|
|
11
|
+
/** mulberry32: small, fast and good enough for gameplay rolls. */
|
|
12
|
+
export declare function createRng(seed: number): Rng;
|
package/dist/sim/rng.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Seeded random source. Every gameplay roll goes through an Rng whose position is kept in the match state.
|
|
2
|
+
/** mulberry32: small, fast and good enough for gameplay rolls. */
|
|
3
|
+
export function createRng(seed) {
|
|
4
|
+
if (!Number.isFinite(seed)) {
|
|
5
|
+
throw new RangeError(`createRng: seed must be finite, got ${seed}`);
|
|
6
|
+
}
|
|
7
|
+
let s = Math.trunc(seed) >>> 0;
|
|
8
|
+
const next = () => {
|
|
9
|
+
s = (s + 0x6d2b79f5) >>> 0;
|
|
10
|
+
let t = s;
|
|
11
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
12
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
13
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
14
|
+
};
|
|
15
|
+
return {
|
|
16
|
+
next,
|
|
17
|
+
range: (a, b) => a + next() * (b - a),
|
|
18
|
+
int: (a, b) => a + Math.floor(next() * (b - a + 1)),
|
|
19
|
+
state: () => s
|
|
20
|
+
};
|
|
21
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { SimEvent } from './events.ts';
|
|
2
|
+
import type { MatchState, PlayerState } from './state.ts';
|
|
3
|
+
import type { Side } from './types.ts';
|
|
4
|
+
export type ShotKind = 'fire' | 'curve' | 'ice' | 'ghost' | 'thunder';
|
|
5
|
+
export declare function addEnergy(p: PlayerState, amount: number): void;
|
|
6
|
+
export declare const energyFull: (p: Readonly<PlayerState>) => boolean;
|
|
7
|
+
/** `p` flies back from a hit, away from `fromX`, and has no control for a moment. */
|
|
8
|
+
export declare function knockBack(p: PlayerState, fromX: number): void;
|
|
9
|
+
/** `target` is iced for two seconds by something `owner` did. */
|
|
10
|
+
export declare function freezePlayer(state: MatchState, target: Side, owner: Side, events: SimEvent[]): void;
|
|
11
|
+
/** Launches the ball from `shooter` toward the opponent's goal as a shot of `kind`. */
|
|
12
|
+
export declare function launchShot(state: MatchState, shooter: Side, kind: ShotKind): void;
|
|
13
|
+
/** Fires `side`'s special: the energy is spent and the shot (or the charge) begins. */
|
|
14
|
+
export declare function fireSpecial(state: MatchState, side: Side, events: SimEvent[]): void;
|
|
15
|
+
/** Whether the ball is close enough to `p` for a special to strike it at once. */
|
|
16
|
+
export declare function ballInReach(state: Readonly<MatchState>, p: Readonly<PlayerState>): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* `side` pressed the special button. With a full meter the titan charges at once; a shot fires at once when the ball
|
|
19
|
+
* is in reach, and otherwise waits armed for the player's next touch of the ball.
|
|
20
|
+
*/
|
|
21
|
+
export declare function pressSpecial(state: MatchState, side: Side, events: SimEvent[]): void;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { specialOf } from "./characters.js";
|
|
2
|
+
import { CURVE_SPEED, CURVE_TICKS, CURVE_VY, DASH_SPEED, DASH_TICKS, ENERGY_MAX, FIRE_SPEED, FIRE_TICKS, FIRE_VY, FREEZE_TICKS, GHOST_SPEED, GHOST_TICKS, GHOST_VY, ICE_SPEED, ICE_TICKS, ICE_VY, KNOCKBACK_STUN_TICKS, KNOCKBACK_VX, KNOCKBACK_VY, SPECIAL_REACH, SPECIAL_TICKS, THUNDER_TARGET_GAP, THUNDER_TICKS, THUNDER_UP_VY, BALL_GRAVITY, WORLD_W } from "./constants.js";
|
|
3
|
+
import { bodyCircle, distance } from "./geometry.js";
|
|
4
|
+
import { addEffect, refreshDerived } from "./powerups.js";
|
|
5
|
+
export function addEnergy(p, amount) {
|
|
6
|
+
p.energy = Math.min(ENERGY_MAX, p.energy + amount);
|
|
7
|
+
}
|
|
8
|
+
export const energyFull = (p) => p.energy >= ENERGY_MAX;
|
|
9
|
+
/** `p` flies back from a hit, away from `fromX`, and has no control for a moment. */
|
|
10
|
+
export function knockBack(p, fromX) {
|
|
11
|
+
const dir = p.x >= fromX ? 1 : -1;
|
|
12
|
+
p.vx = dir * KNOCKBACK_VX;
|
|
13
|
+
p.vy = -KNOCKBACK_VY;
|
|
14
|
+
p.onGround = false;
|
|
15
|
+
p.stunTicks = Math.max(p.stunTicks, KNOCKBACK_STUN_TICKS);
|
|
16
|
+
p.kickTicks = 0;
|
|
17
|
+
}
|
|
18
|
+
/** `target` is iced for two seconds by something `owner` did. */
|
|
19
|
+
export function freezePlayer(state, target, owner, events) {
|
|
20
|
+
addEffect(state, 'freezeFoe', owner, target, FREEZE_TICKS, events);
|
|
21
|
+
}
|
|
22
|
+
/** Launches the ball from `shooter` toward the opponent's goal as a shot of `kind`. */
|
|
23
|
+
export function launchShot(state, shooter, kind) {
|
|
24
|
+
const p = state.players[shooter];
|
|
25
|
+
const ball = state.ball;
|
|
26
|
+
ball.lastToucher = shooter;
|
|
27
|
+
ball.modeOwner = shooter;
|
|
28
|
+
ball.modeAge = 0;
|
|
29
|
+
switch (kind) {
|
|
30
|
+
case 'fire':
|
|
31
|
+
ball.vx = p.facing * FIRE_SPEED;
|
|
32
|
+
ball.vy = FIRE_VY;
|
|
33
|
+
ball.mode = 'fire';
|
|
34
|
+
ball.modeTicks = FIRE_TICKS;
|
|
35
|
+
break;
|
|
36
|
+
case 'curve':
|
|
37
|
+
ball.vx = p.facing * CURVE_SPEED;
|
|
38
|
+
ball.vy = CURVE_VY;
|
|
39
|
+
ball.mode = 'curve';
|
|
40
|
+
ball.modeTicks = CURVE_TICKS;
|
|
41
|
+
break;
|
|
42
|
+
case 'ice':
|
|
43
|
+
ball.vx = p.facing * ICE_SPEED;
|
|
44
|
+
ball.vy = ICE_VY;
|
|
45
|
+
ball.mode = 'ice';
|
|
46
|
+
ball.modeTicks = ICE_TICKS;
|
|
47
|
+
break;
|
|
48
|
+
case 'ghost':
|
|
49
|
+
ball.vx = p.facing * GHOST_SPEED;
|
|
50
|
+
ball.vy = GHOST_VY;
|
|
51
|
+
ball.mode = 'ghost';
|
|
52
|
+
ball.modeTicks = GHOST_TICKS;
|
|
53
|
+
break;
|
|
54
|
+
case 'thunder': {
|
|
55
|
+
// A lob that peaks just in front of the opponent's goal line, where it turns into the slam.
|
|
56
|
+
const goal = state.goals[shooter === 0 ? 1 : 0];
|
|
57
|
+
const targetX = shooter === 0 ? WORLD_W - goal.depth - THUNDER_TARGET_GAP : goal.depth + THUNDER_TARGET_GAP;
|
|
58
|
+
const flight = -THUNDER_UP_VY / BALL_GRAVITY;
|
|
59
|
+
ball.vx = (targetX - ball.x) / flight;
|
|
60
|
+
ball.vy = THUNDER_UP_VY;
|
|
61
|
+
ball.mode = 'thunderUp';
|
|
62
|
+
ball.modeTicks = THUNDER_TICKS;
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const SHOT_OF = {
|
|
68
|
+
fireShot: 'fire',
|
|
69
|
+
windCurve: 'curve',
|
|
70
|
+
iceShot: 'ice',
|
|
71
|
+
ghostBall: 'ghost',
|
|
72
|
+
thunderDrop: 'thunder'
|
|
73
|
+
};
|
|
74
|
+
/** Fires `side`'s special: the energy is spent and the shot (or the charge) begins. */
|
|
75
|
+
export function fireSpecial(state, side, events) {
|
|
76
|
+
const p = state.players[side];
|
|
77
|
+
const kind = specialOf(p.character);
|
|
78
|
+
p.energy = 0;
|
|
79
|
+
p.specialArmed = false;
|
|
80
|
+
p.specialTicks = SPECIAL_TICKS;
|
|
81
|
+
p.kickTicks = 0;
|
|
82
|
+
if (kind === 'titanCharge') {
|
|
83
|
+
p.dashTicks = DASH_TICKS;
|
|
84
|
+
p.vx = p.facing * DASH_SPEED;
|
|
85
|
+
p.dashHitBall = false;
|
|
86
|
+
p.dashHitFoe = false;
|
|
87
|
+
refreshDerived(state);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
launchShot(state, side, SHOT_OF[kind]);
|
|
91
|
+
}
|
|
92
|
+
events.push({ type: 'special', player: side, kind });
|
|
93
|
+
}
|
|
94
|
+
/** Whether the ball is close enough to `p` for a special to strike it at once. */
|
|
95
|
+
export function ballInReach(state, p) {
|
|
96
|
+
const body = bodyCircle(p);
|
|
97
|
+
return distance(body.x, body.y, state.ball.x, state.ball.y) <= SPECIAL_REACH;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* `side` pressed the special button. With a full meter the titan charges at once; a shot fires at once when the ball
|
|
101
|
+
* is in reach, and otherwise waits armed for the player's next touch of the ball.
|
|
102
|
+
*/
|
|
103
|
+
export function pressSpecial(state, side, events) {
|
|
104
|
+
const p = state.players[side];
|
|
105
|
+
if (!energyFull(p) || p.specialTicks > 0 || p.dashTicks > 0) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
if (specialOf(p.character) === 'titanCharge' || ballInReach(state, p)) {
|
|
109
|
+
fireSpecial(state, side, events);
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
p.specialArmed = true;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import type { AnimState, BallMode, CharacterId, Phase, PowerUpKind, Side } from './types.ts';
|
|
2
|
+
export interface PlayerState {
|
|
3
|
+
side: Side;
|
|
4
|
+
character: CharacterId;
|
|
5
|
+
/** The point between the feet; y = GROUND_Y when standing. */
|
|
6
|
+
x: number;
|
|
7
|
+
y: number;
|
|
8
|
+
vx: number;
|
|
9
|
+
vy: number;
|
|
10
|
+
onGround: boolean;
|
|
11
|
+
/** Toward the opponent's goal: player 0 faces right (1), player 1 left (-1). */
|
|
12
|
+
facing: 1 | -1;
|
|
13
|
+
/** Size of the head, from power-ups and the titan charge (derived every tick). */
|
|
14
|
+
headScale: number;
|
|
15
|
+
/** Iced by the freeze power-up or an ice shot: no control, drawn with an ice overlay (derived every tick). */
|
|
16
|
+
frozen: boolean;
|
|
17
|
+
/** 0 to ENERGY_MAX; full lets the player fire their special. */
|
|
18
|
+
energy: number;
|
|
19
|
+
/** Ticks left of the current kick; the foot strikes in the middle of it. */
|
|
20
|
+
kickTicks: number;
|
|
21
|
+
/** The current kick already struck the ball. */
|
|
22
|
+
kickHit: boolean;
|
|
23
|
+
headerTicks: number;
|
|
24
|
+
/** Ticks left of the special animation. */
|
|
25
|
+
specialTicks: number;
|
|
26
|
+
/** The special was pressed with full energy and fires on the next touch of the ball. */
|
|
27
|
+
specialArmed: boolean;
|
|
28
|
+
/** Ticks left of the titan charge. */
|
|
29
|
+
dashTicks: number;
|
|
30
|
+
/** The charge already smashed the ball, and already knocked the opponent back. */
|
|
31
|
+
dashHitBall: boolean;
|
|
32
|
+
dashHitFoe: boolean;
|
|
33
|
+
/** Knocked back or caught in a thunder slam: no control, not iced. */
|
|
34
|
+
stunTicks: number;
|
|
35
|
+
/** Ticks before a plain touch of the ball gives energy again. */
|
|
36
|
+
touchCooldown: number;
|
|
37
|
+
/** The previous tick's input as INPUT_BITS, to find presses. */
|
|
38
|
+
input: number;
|
|
39
|
+
anim: AnimState;
|
|
40
|
+
/** Ticks spent in the current animation state; the renderer's animation time is animTicks / TICK_RATE. */
|
|
41
|
+
animTicks: number;
|
|
42
|
+
}
|
|
43
|
+
export interface BallState {
|
|
44
|
+
x: number;
|
|
45
|
+
y: number;
|
|
46
|
+
vx: number;
|
|
47
|
+
vy: number;
|
|
48
|
+
/** Radius, from the ball-size power-ups (derived every tick). */
|
|
49
|
+
r: number;
|
|
50
|
+
/** The last player who touched it, credited with the items it collects. */
|
|
51
|
+
lastToucher: Side | null;
|
|
52
|
+
mode: BallMode;
|
|
53
|
+
/** Ticks left of the mode. */
|
|
54
|
+
modeTicks: number;
|
|
55
|
+
/** Who launched the current shot. */
|
|
56
|
+
modeOwner: Side | null;
|
|
57
|
+
/** Ticks since the current shot was launched. */
|
|
58
|
+
modeAge: number;
|
|
59
|
+
}
|
|
60
|
+
/** A power-up on the field, falling from the sky or resting on the grass. */
|
|
61
|
+
export interface PowerUpItem {
|
|
62
|
+
id: number;
|
|
63
|
+
kind: PowerUpKind;
|
|
64
|
+
x: number;
|
|
65
|
+
y: number;
|
|
66
|
+
landed: boolean;
|
|
67
|
+
/** Ticks since it spawned. */
|
|
68
|
+
age: number;
|
|
69
|
+
}
|
|
70
|
+
/** A collected power-up at work. `owner` collected it; `target` is the player (or the goal of the player) it acts on. */
|
|
71
|
+
export interface ActiveEffect {
|
|
72
|
+
kind: PowerUpKind;
|
|
73
|
+
owner: Side;
|
|
74
|
+
target: Side;
|
|
75
|
+
ticks: number;
|
|
76
|
+
}
|
|
77
|
+
/** One goal as it currently stands (derived every tick from the effects). */
|
|
78
|
+
export interface GoalState {
|
|
79
|
+
/** Top of the crossbar. */
|
|
80
|
+
crossbarY: number;
|
|
81
|
+
/** Mouth depth from the side wall to the goal line. */
|
|
82
|
+
depth: number;
|
|
83
|
+
/** A shield wall stands in front of the goal line. */
|
|
84
|
+
shield: boolean;
|
|
85
|
+
}
|
|
86
|
+
export interface MatchState {
|
|
87
|
+
/** Ticks stepped since the match was created. */
|
|
88
|
+
tick: number;
|
|
89
|
+
phase: Phase;
|
|
90
|
+
/** Ticks left of a `kickoff` or `goal` phase. */
|
|
91
|
+
phaseTicks: number;
|
|
92
|
+
/** Ticks of regular time left; 0 once golden goal began. */
|
|
93
|
+
clockTicks: number;
|
|
94
|
+
/** Regular time ended level: the next goal wins. */
|
|
95
|
+
goldenGoal: boolean;
|
|
96
|
+
/** Ticks played in golden goal. */
|
|
97
|
+
goldenTicks: number;
|
|
98
|
+
score: [number, number];
|
|
99
|
+
players: [PlayerState, PlayerState];
|
|
100
|
+
ball: BallState;
|
|
101
|
+
/** Index 0 is the left goal (player 0 defends it), 1 the right goal. */
|
|
102
|
+
goals: [GoalState, GoalState];
|
|
103
|
+
items: PowerUpItem[];
|
|
104
|
+
effects: ActiveEffect[];
|
|
105
|
+
/** Ticks of play before the next item spawns. */
|
|
106
|
+
nextItemTicks: number;
|
|
107
|
+
nextItemId: number;
|
|
108
|
+
/** Power-ups spawn in this match. */
|
|
109
|
+
powerUps: boolean;
|
|
110
|
+
/** Who scored the last goal, for celebrations. */
|
|
111
|
+
lastScorer: Side | null;
|
|
112
|
+
/** Set once the match is over; null with phase `over` is a draw. */
|
|
113
|
+
winner: Side | null;
|
|
114
|
+
/** Position of the seeded random sequence. */
|
|
115
|
+
rng: number;
|
|
116
|
+
}
|
|
117
|
+
export interface MatchOptions {
|
|
118
|
+
seed: number;
|
|
119
|
+
/** Characters of players 0 and 1. */
|
|
120
|
+
characters: readonly [CharacterId, CharacterId];
|
|
121
|
+
/** Regular time; MATCH_SECONDS by default. */
|
|
122
|
+
matchSeconds?: number;
|
|
123
|
+
/** Whether power-ups spawn; true by default. */
|
|
124
|
+
powerUps?: boolean;
|
|
125
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** A player's index: 0 starts left and defends the left goal, 1 starts right and defends the right goal. */
|
|
2
|
+
export type Side = 0 | 1;
|
|
3
|
+
/** One player's controls on one tick. Presses are found by comparing with the previous tick. */
|
|
4
|
+
export interface InputFrame {
|
|
5
|
+
left: boolean;
|
|
6
|
+
right: boolean;
|
|
7
|
+
jump: boolean;
|
|
8
|
+
kick: boolean;
|
|
9
|
+
special: boolean;
|
|
10
|
+
}
|
|
11
|
+
export declare const NO_INPUT: Readonly<InputFrame>;
|
|
12
|
+
export declare const CHARACTER_IDS: readonly ["tuan", "linh", "bao", "kai", "mai", "don"];
|
|
13
|
+
export type CharacterId = (typeof CHARACTER_IDS)[number];
|
|
14
|
+
export declare const SPECIAL_KINDS: readonly ["fireShot", "windCurve", "iceShot", "ghostBall", "thunderDrop", "titanCharge"];
|
|
15
|
+
export type SpecialKind = (typeof SPECIAL_KINDS)[number];
|
|
16
|
+
export declare const POWERUP_KINDS: readonly ["bigHead", "shrinkFoe", "bigGoalFoe", "smallGoalSelf", "bigBall", "smallBall", "freezeFoe", "speedBoost", "fireShot", "shield", "magnet", "bouncyBall", "reverseFoe", "doubleGoal", "wind"];
|
|
17
|
+
export type PowerUpKind = (typeof POWERUP_KINDS)[number];
|
|
18
|
+
export declare const ANIM_STATES: readonly ["idle", "run", "jump", "kick", "header", "special", "celebrate", "sad"];
|
|
19
|
+
export type AnimState = (typeof ANIM_STATES)[number];
|
|
20
|
+
/** `kickoff`: everyone waits while the ball hangs at the centre; `goal`: the pause after a goal; `over`: final. */
|
|
21
|
+
export declare const PHASES: readonly ["kickoff", "play", "goal", "over"];
|
|
22
|
+
export type Phase = (typeof PHASES)[number];
|
|
23
|
+
/**
|
|
24
|
+
* What the ball is doing beyond plain physics: a shot launched by a special or the fire-shot power-up.
|
|
25
|
+
* `thunderUp` is the lob of a thunder drop, `thunderDown` its slam.
|
|
26
|
+
*/
|
|
27
|
+
export declare const BALL_MODES: readonly ["none", "fire", "curve", "ice", "ghost", "thunderUp", "thunderDown"];
|
|
28
|
+
export type BallMode = (typeof BALL_MODES)[number];
|
|
29
|
+
/** Bot strength, 1 (a beginner can beat it) to 5 (strong). */
|
|
30
|
+
export type Difficulty = 1 | 2 | 3 | 4 | 5;
|
|
31
|
+
export declare const other: (side: Side) => Side;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// Shared vocabulary of the simulation: inputs, characters, power-ups and animation states.
|
|
2
|
+
export const NO_INPUT = Object.freeze({ left: false, right: false, jump: false, kick: false, special: false });
|
|
3
|
+
export const CHARACTER_IDS = ['tuan', 'linh', 'bao', 'kai', 'mai', 'don'];
|
|
4
|
+
export const SPECIAL_KINDS = ['fireShot', 'windCurve', 'iceShot', 'ghostBall', 'thunderDrop', 'titanCharge'];
|
|
5
|
+
export const POWERUP_KINDS = [
|
|
6
|
+
'bigHead',
|
|
7
|
+
'shrinkFoe',
|
|
8
|
+
'bigGoalFoe',
|
|
9
|
+
'smallGoalSelf',
|
|
10
|
+
'bigBall',
|
|
11
|
+
'smallBall',
|
|
12
|
+
'freezeFoe',
|
|
13
|
+
'speedBoost',
|
|
14
|
+
'fireShot',
|
|
15
|
+
'shield',
|
|
16
|
+
'magnet',
|
|
17
|
+
'bouncyBall',
|
|
18
|
+
'reverseFoe',
|
|
19
|
+
'doubleGoal',
|
|
20
|
+
'wind'
|
|
21
|
+
];
|
|
22
|
+
export const ANIM_STATES = ['idle', 'run', 'jump', 'kick', 'header', 'special', 'celebrate', 'sad'];
|
|
23
|
+
/** `kickoff`: everyone waits while the ball hangs at the centre; `goal`: the pause after a goal; `over`: final. */
|
|
24
|
+
export const PHASES = ['kickoff', 'play', 'goal', 'over'];
|
|
25
|
+
/**
|
|
26
|
+
* What the ball is doing beyond plain physics: a shot launched by a special or the fire-shot power-up.
|
|
27
|
+
* `thunderUp` is the lob of a thunder drop, `thunderDown` its slam.
|
|
28
|
+
*/
|
|
29
|
+
export const BALL_MODES = ['none', 'fire', 'curve', 'ice', 'ghost', 'thunderUp', 'thunderDown'];
|
|
30
|
+
export const other = (side) => (side === 0 ? 1 : 0);
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { Rect } from './geometry.ts';
|
|
2
|
+
import type { BallState, GoalState, MatchState } from './state.ts';
|
|
3
|
+
import type { Side } from './types.ts';
|
|
4
|
+
export interface Wall {
|
|
5
|
+
kind: 'bar' | 'shield';
|
|
6
|
+
side: Side;
|
|
7
|
+
rect: Rect;
|
|
8
|
+
}
|
|
9
|
+
/** Where a move first touches a wall: the centre's position there and the surface normal (pointing at the ball). */
|
|
10
|
+
export interface WallHit {
|
|
11
|
+
wall: Wall;
|
|
12
|
+
t: number;
|
|
13
|
+
x: number;
|
|
14
|
+
y: number;
|
|
15
|
+
nx: number;
|
|
16
|
+
ny: number;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The thin solids of the current goal frames: both crossbars, and each goal's shield while it stands. With a shield
|
|
20
|
+
* up, the bar reaches across the small gap to it, so bar and shield tops are one surface: a ball rolls from one onto
|
|
21
|
+
* the other instead of settling in the notch between them.
|
|
22
|
+
*/
|
|
23
|
+
export declare function wallsOf(state: Readonly<MatchState>): Wall[];
|
|
24
|
+
/** Whether a ball of radius `r` centred at (x, y) overlaps `rect` by more than rounding. */
|
|
25
|
+
export declare function overlapsWall(rect: Readonly<Rect>, x: number, y: number, r: number): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* The first point where the centre, moving from (x0, y0) by (dx, dy), comes within `r` of `rect`, or null if it never
|
|
28
|
+
* does on this move. A start already within `r` is not a hit: `resolveInside` deals with it.
|
|
29
|
+
*/
|
|
30
|
+
export declare function sweepRect(rect: Readonly<Rect>, r: number, x0: number, y0: number, dx: number, dy: number): Omit<WallHit, 'wall'> | null;
|
|
31
|
+
/** The earliest wall the move from (x0, y0) by (dx, dy) touches, or null. */
|
|
32
|
+
export declare function firstWallHit(walls: readonly Wall[], r: number, x0: number, y0: number, dx: number, dy: number): WallHit | null;
|
|
33
|
+
/**
|
|
34
|
+
* Moves a ball whose centre is inside `wall` (grown by its radius) out of it: onto the top of a shield it is above,
|
|
35
|
+
* otherwise out of the shield's field face; onto the top of a crossbar it is above the middle of, otherwise under it.
|
|
36
|
+
* Returns the normal it left along.
|
|
37
|
+
*/
|
|
38
|
+
export declare function resolveInside(ball: BallState, wall: Wall): {
|
|
39
|
+
nx: number;
|
|
40
|
+
ny: number;
|
|
41
|
+
};
|
|
42
|
+
/** The ball never leaves the field: inside the side walls, under the ceiling and on or above the grass. */
|
|
43
|
+
export declare function clampToField(ball: BallState): void;
|
|
44
|
+
/** Whether the ball is fully past goal `side`'s line under its crossbar: a goal (match `goalEntered`). */
|
|
45
|
+
export declare function ballInMouth(ball: Readonly<BallState>, goal: Readonly<GoalState>, side: Side): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* A ball is never behind (or inside) a standing shield below its top: one there goes to just in front of its field face.
|
|
48
|
+
* Play never puts it there; this keeps it so whatever happened, a frame that changed or a state set from outside.
|
|
49
|
+
*/
|
|
50
|
+
export declare function keepInFrontOfShield(state: MatchState, side: Side): void;
|
|
51
|
+
export interface FrameBefore {
|
|
52
|
+
goals: ReadonlyArray<Readonly<GoalState>>;
|
|
53
|
+
r: number;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* After a goal frame or the ball changed shape in play: a change never scores and never leaves the ball inside or
|
|
57
|
+
* behind a wall. A ball behind a shield's field face (below its top) goes to the field side; a ball a crossbar moved
|
|
58
|
+
* into goes to the side of it its centre is on; a ball a grown mouth now holds goes to just in front of the goal line.
|
|
59
|
+
*/
|
|
60
|
+
export declare function settleFrameChange(state: MatchState, before: FrameBefore): void;
|