@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.
Files changed (49) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +163 -2
  3. package/dist/definition.d.ts +16 -0
  4. package/dist/definition.js +42 -0
  5. package/dist/index.d.ts +5 -0
  6. package/dist/index.js +2 -0
  7. package/dist/net/predicted-sim.d.ts +21 -0
  8. package/dist/net/predicted-sim.js +66 -0
  9. package/dist/net/wire.d.ts +52 -0
  10. package/dist/net/wire.js +266 -0
  11. package/dist/room-match.d.ts +35 -0
  12. package/dist/room-match.js +71 -0
  13. package/dist/sim/anim.d.ts +7 -0
  14. package/dist/sim/anim.js +44 -0
  15. package/dist/sim/ball.d.ts +9 -0
  16. package/dist/sim/ball.js +464 -0
  17. package/dist/sim/bot.d.ts +5 -0
  18. package/dist/sim/bot.js +130 -0
  19. package/dist/sim/characters.d.ts +22 -0
  20. package/dist/sim/characters.js +42 -0
  21. package/dist/sim/constants.d.ts +156 -0
  22. package/dist/sim/constants.js +166 -0
  23. package/dist/sim/debug.d.ts +14 -0
  24. package/dist/sim/debug.js +34 -0
  25. package/dist/sim/events.d.ts +79 -0
  26. package/dist/sim/events.js +12 -0
  27. package/dist/sim/geometry.d.ts +33 -0
  28. package/dist/sim/geometry.js +58 -0
  29. package/dist/sim/index.d.ts +18 -0
  30. package/dist/sim/index.js +13 -0
  31. package/dist/sim/input.d.ts +12 -0
  32. package/dist/sim/input.js +25 -0
  33. package/dist/sim/match.d.ts +17 -0
  34. package/dist/sim/match.js +208 -0
  35. package/dist/sim/player.d.ts +16 -0
  36. package/dist/sim/player.js +207 -0
  37. package/dist/sim/powerups.d.ts +33 -0
  38. package/dist/sim/powerups.js +189 -0
  39. package/dist/sim/rng.d.ts +12 -0
  40. package/dist/sim/rng.js +21 -0
  41. package/dist/sim/specials.d.ts +21 -0
  42. package/dist/sim/specials.js +114 -0
  43. package/dist/sim/state.d.ts +125 -0
  44. package/dist/sim/state.js +1 -0
  45. package/dist/sim/types.d.ts +31 -0
  46. package/dist/sim/types.js +30 -0
  47. package/dist/sim/walls.d.ts +60 -0
  48. package/dist/sim/walls.js +215 -0
  49. package/package.json +56 -3
@@ -0,0 +1,130 @@
1
+ import { specialOf } from "./characters.js";
2
+ import { BALL_GRAVITY, DT, GROUND_Y, POWERUP_REST_Y, WORLD_W } from "./constants.js";
3
+ import { headCircle } from "./geometry.js";
4
+ import { INPUT_BITS } from "./input.js";
5
+ import { ballInReach, energyFull } from "./specials.js";
6
+ import { NO_INPUT, other } from "./types.js";
7
+ const PROFILES = {
8
+ 1: { hesitate: 0.5, aimError: 140, jumpChance: 0.02, kickChance: 0.05, readAhead: false, special: false, powerUps: false, hopsOver: false, guardsGoal: false, deadZone: 50, cover: 0 },
9
+ 2: { hesitate: 0.32, aimError: 100, jumpChance: 0.05, kickChance: 0.12, readAhead: false, special: false, powerUps: false, hopsOver: false, guardsGoal: false, deadZone: 35, cover: 80 },
10
+ 3: { hesitate: 0.16, aimError: 60, jumpChance: 0.12, kickChance: 0.3, readAhead: true, special: true, powerUps: true, hopsOver: false, guardsGoal: false, deadZone: 22, cover: 300 },
11
+ 4: { hesitate: 0.06, aimError: 30, jumpChance: 0.3, kickChance: 0.55, readAhead: true, special: true, powerUps: true, hopsOver: true, guardsGoal: true, deadZone: 14, cover: 500 },
12
+ 5: { hesitate: 0.02, aimError: 20, jumpChance: 0.35, kickChance: 0.6, readAhead: true, special: true, powerUps: true, hopsOver: true, guardsGoal: true, deadZone: 8, cover: 550 }
13
+ };
14
+ /** Where the bot stands relative to the ball: this far on its own goal's side, so it plays the ball forward. */
15
+ const BEHIND_BALL = 42;
16
+ /** How long ahead the bot reads a ball's flight, in ticks. */
17
+ const READ_AHEAD_TICKS = 50;
18
+ /** Height above the ground at which it meets a falling ball. */
19
+ const MEET_HEIGHT = 150;
20
+ /** The opponent has the ball when they are at least this much nearer to it than the bot... */
21
+ const FOE_LEAD = 30;
22
+ /** ...or have it this close in front of them. */
23
+ const FOE_FEET = 120;
24
+ /** Where it guards its goal: this far in front of the goal line. */
25
+ const GOAL_LINE_GAP = 40;
26
+ /**
27
+ * A slowly changing offset in [-1, 1], the same for a whole half second: the bot's aim wanders without it having to
28
+ * remember anything between ticks.
29
+ */
30
+ function slowNoise(tick, side, salt) {
31
+ let h = (Math.floor(tick / 30) * 374761393 + side * 668265263 + salt * 2147483647) | 0;
32
+ h = Math.imul(h ^ (h >>> 13), 1274126177);
33
+ h ^= h >>> 16;
34
+ return ((h >>> 0) / 4294967295) * 2 - 1;
35
+ }
36
+ /** Where the ball will be once it comes down to meeting height, or where it is if it is already low. */
37
+ function readBall(state) {
38
+ const ball = state.ball;
39
+ const meetY = GROUND_Y - MEET_HEIGHT;
40
+ let { x, y, vx, vy } = ball;
41
+ if (y >= meetY) {
42
+ return { x, y };
43
+ }
44
+ for (let i = 0; i < READ_AHEAD_TICKS; i++) {
45
+ vy += BALL_GRAVITY * DT;
46
+ x += vx * DT;
47
+ y += vy * DT;
48
+ if (x < ball.r || x > WORLD_W - ball.r) {
49
+ vx = -vx;
50
+ x = Math.min(Math.max(x, ball.r), WORLD_W - ball.r);
51
+ }
52
+ if (vy > 0 && y >= meetY) {
53
+ break;
54
+ }
55
+ }
56
+ return { x, y };
57
+ }
58
+ /** The bot presses a key only when it is not already held, so every press counts as a new one. */
59
+ function press(p, bit, want) {
60
+ return want && (p.input & bit) === 0;
61
+ }
62
+ /** The keys the bot playing `side` at `difficulty` presses this tick. */
63
+ export function botInput(state, side, difficulty, rng) {
64
+ const profile = PROFILES[difficulty];
65
+ if (!profile) {
66
+ throw new RangeError(`botInput: difficulty must be 1 to 5, got ${String(difficulty)}`);
67
+ }
68
+ // Rolls are drawn every tick in the same order, so the sequence never depends on which branch is taken.
69
+ const rollMove = rng.next();
70
+ const rollJump = rng.next();
71
+ const rollKick = rng.next();
72
+ if (state.phase !== 'play') {
73
+ return { ...NO_INPUT };
74
+ }
75
+ const p = state.players[side];
76
+ const ball = state.ball;
77
+ const f = p.facing;
78
+ const ownGoalX = side === 0 ? 0 : WORLD_W;
79
+ // A ghost ball from the opponent is all but invisible: weaker bots fall back to guarding their goal.
80
+ const lostBall = ball.mode === 'ghost' && ball.modeOwner !== side && profile.aimError > 20;
81
+ const read = profile.readAhead ? readBall(state) : { x: ball.x, y: ball.y };
82
+ // While the opponent has the ball (nearer to it, or at their feet in front of them), the bot hangs back on its own
83
+ // goal's side to cover their shot instead of standing right behind the ball.
84
+ const foe = state.players[other(side)];
85
+ const foeDistance = Math.abs(foe.x - ball.x);
86
+ const foeHasIt = foeDistance + FOE_LEAD < Math.abs(p.x - ball.x) || (foeDistance < FOE_FEET && (ball.x - foe.x) * f < 0);
87
+ const standOff = foeHasIt ? Math.max(BEHIND_BALL, profile.cover) : BEHIND_BALL;
88
+ let targetX = lostBall
89
+ ? ownGoalX + f * 200
90
+ : read.x - f * standOff + slowNoise(state.tick, side, 1) * profile.aimError;
91
+ // A fast ball already past the bot and heading for its goal: get back to the line instead of chasing it from behind.
92
+ const behindNow = (ball.x - p.x) * f < -10;
93
+ const goalLineX = side === 0 ? state.goals[0].depth + GOAL_LINE_GAP : WORLD_W - state.goals[1].depth - GOAL_LINE_GAP;
94
+ if (profile.guardsGoal && behindNow && ball.vx * f < -400 && Math.abs(ball.x - ownGoalX) < 700 && ball.y < GROUND_Y - 120) {
95
+ targetX = goalLineX;
96
+ }
97
+ // A power-up lying close by is worth a detour while the ball is in the opponent's half.
98
+ if (profile.powerUps && !lostBall) {
99
+ const ballSafe = (ball.x - WORLD_W / 2) * f > 0;
100
+ const item = state.items.find(i => i.y >= POWERUP_REST_Y - 200 && Math.abs(i.x - p.x) < 350);
101
+ if (ballSafe && item) {
102
+ targetX = item.x;
103
+ }
104
+ }
105
+ const dx = targetX - p.x;
106
+ const behind = behindNow;
107
+ let left = false;
108
+ let right = false;
109
+ if (Math.abs(dx) > profile.deadZone && rollMove >= profile.hesitate) {
110
+ left = dx < 0;
111
+ right = dx > 0;
112
+ }
113
+ const head = headCircle(p);
114
+ const ballDx = ball.x - p.x;
115
+ // A header: the ball is coming down over the head, within a jump.
116
+ const headerOn = !behind && Math.abs(ballDx) < 70 + ball.r && ball.y < head.y && ball.y > head.y - 260 && ball.vy > -300;
117
+ // A ball rolling behind the bot toward its own goal: hop over it to get back on the right side.
118
+ const hopOver = profile.hopsOver && behind && Math.abs(ballDx) < 110 && ball.y > GROUND_Y - 120;
119
+ const jump = p.onGround && ((headerOn && rollJump < profile.jumpChance) || hopOver);
120
+ const ahead = ballDx * f;
121
+ const atFoot = ahead > -10 && ahead < 110 && ball.y > p.y - 160;
122
+ const kick = press(p, INPUT_BITS.kick, atFoot && p.kickTicks === 0 && rollKick < profile.kickChance);
123
+ let special = false;
124
+ if (profile.special && energyFull(p) && !behind) {
125
+ special = specialOf(p.character) === 'titanCharge'
126
+ ? ahead > 0 && ahead < 380 && ball.y > GROUND_Y - 260
127
+ : ballInReach(state, p);
128
+ }
129
+ return { left, right, jump, kick, special: press(p, INPUT_BITS.special, special) };
130
+ }
@@ -0,0 +1,22 @@
1
+ import type { CharacterId, SpecialKind } from './types.ts';
2
+ export declare const CHARACTER_SPECIALS: Readonly<Record<CharacterId, SpecialKind>>;
3
+ /**
4
+ * Where each character's head is, in world units above the feet, so the head the simulation plays with is the head
5
+ * the client draws: `head` is the skull centre at head scale 1 and `neck` the point the head grows from. A scaled head
6
+ * (big head, shrink, titan charge) is centred at `neck + (head - neck) * scale` with radius HEAD_RADIUS * scale, the
7
+ * same formula the client draws it with.
8
+ *
9
+ * Taken from the client's sprite art (minigames games/head-clash, src/render/art/sprite-index.ts): the mean skull centre
10
+ * and neck point of the character's idle frames, at the display scale that makes its drawn skull HEAD_RADIUS on
11
+ * average. The client test tests/head-geometry.test.ts recomputes them from the art and fails when they drift.
12
+ */
13
+ export declare const CHARACTER_HEADS: Readonly<Record<CharacterId, {
14
+ head: number;
15
+ neck: number;
16
+ }>>;
17
+ /** Characters every new player has. */
18
+ export declare const STARTER_CHARACTERS: readonly CharacterId[];
19
+ /** Tournament matches a player must win before each other character unlocks. */
20
+ export declare const CHARACTER_UNLOCK_AFTER: Readonly<Partial<Record<CharacterId, number>>>;
21
+ export declare function isCharacterId(value: unknown): value is CharacterId;
22
+ export declare function specialOf(character: CharacterId): SpecialKind;
@@ -0,0 +1,42 @@
1
+ import { CHARACTER_IDS } from "./types.js";
2
+ export const CHARACTER_SPECIALS = {
3
+ tuan: 'fireShot',
4
+ linh: 'windCurve',
5
+ bao: 'iceShot',
6
+ kai: 'ghostBall',
7
+ mai: 'thunderDrop',
8
+ don: 'titanCharge'
9
+ };
10
+ /**
11
+ * Where each character's head is, in world units above the feet, so the head the simulation plays with is the head
12
+ * the client draws: `head` is the skull centre at head scale 1 and `neck` the point the head grows from. A scaled head
13
+ * (big head, shrink, titan charge) is centred at `neck + (head - neck) * scale` with radius HEAD_RADIUS * scale, the
14
+ * same formula the client draws it with.
15
+ *
16
+ * Taken from the client's sprite art (minigames games/head-clash, src/render/art/sprite-index.ts): the mean skull centre
17
+ * and neck point of the character's idle frames, at the display scale that makes its drawn skull HEAD_RADIUS on
18
+ * average. The client test tests/head-geometry.test.ts recomputes them from the art and fails when they drift.
19
+ */
20
+ export const CHARACTER_HEADS = {
21
+ tuan: { head: 157.7, neck: 92.3 },
22
+ linh: { head: 164.9, neck: 104.9 },
23
+ bao: { head: 174.6, neck: 110.5 },
24
+ kai: { head: 163.0, neck: 109.5 },
25
+ mai: { head: 168.8, neck: 114.6 },
26
+ don: { head: 192.2, neck: 134.9 }
27
+ };
28
+ /** Characters every new player has. */
29
+ export const STARTER_CHARACTERS = ['tuan', 'linh'];
30
+ /** Tournament matches a player must win before each other character unlocks. */
31
+ export const CHARACTER_UNLOCK_AFTER = {
32
+ bao: 3,
33
+ kai: 6,
34
+ mai: 9,
35
+ don: 12
36
+ };
37
+ export function isCharacterId(value) {
38
+ return typeof value === 'string' && CHARACTER_IDS.includes(value);
39
+ }
40
+ export function specialOf(character) {
41
+ return CHARACTER_SPECIALS[character];
42
+ }
@@ -0,0 +1,156 @@
1
+ /** Fixed simulation rate: one input frame advances the match exactly one tick. */
2
+ export declare const TICK_RATE = 60;
3
+ export declare const DT: number;
4
+ /** Whole seconds to ticks. */
5
+ export declare const secondsToTicks: (seconds: number) => number;
6
+ export declare const WORLD_W = 1600;
7
+ export declare const WORLD_H = 900;
8
+ export declare const GROUND_Y = 780;
9
+ export declare const CEILING_Y = 0;
10
+ export declare const CENTER_X: number;
11
+ export declare const GOAL_DEPTH = 110;
12
+ /** A big goal raises the crossbar and widens the frame. */
13
+ export declare const GOAL_DEPTH_BIG = 140;
14
+ export declare const CROSSBAR_Y = 520;
15
+ export declare const CROSSBAR_Y_BIG = 430;
16
+ export declare const CROSSBAR_Y_SMALL = 600;
17
+ export declare const CROSSBAR_THICKNESS = 14;
18
+ /** The shield stands this far in front of the goal line and is this thick. */
19
+ export declare const SHIELD_GAP = 6;
20
+ export declare const SHIELD_THICKNESS = 16;
21
+ export declare const HEAD_RADIUS = 46;
22
+ export declare const HEAD_SCALE_BIG = 1.6;
23
+ export declare const HEAD_SCALE_SMALL = 0.6;
24
+ /** Head scale during the Titan charge. */
25
+ export declare const HEAD_SCALE_TITAN = 2;
26
+ /** The chest circle sits on each character's neck point (CHARACTER_HEADS), between the body and the head. */
27
+ export declare const CHEST_RADIUS = 30;
28
+ export declare const BODY_RADIUS = 34;
29
+ /** The body circle's centre above the feet. */
30
+ export declare const BODY_CENTER_Y = 58;
31
+ export declare const FOOT_RADIUS = 22;
32
+ /** The resting foot, in front of the body. */
33
+ export declare const FOOT_FORWARD = 30;
34
+ export declare const FOOT_HEIGHT = 20;
35
+ /** The swinging foot during a kick reaches further and higher. */
36
+ export declare const KICK_FOOT_RADIUS = 34;
37
+ export declare const KICK_FOOT_FORWARD = 58;
38
+ export declare const KICK_FOOT_HEIGHT = 40;
39
+ export declare const PLAYER_START_X: readonly [number, number];
40
+ export declare const PLAYER_MIN_X = 130;
41
+ export declare const PLAYER_MAX_X: number;
42
+ /** Two players' bodies never come closer than this, centre to centre. */
43
+ export declare const PLAYER_SEPARATION: number;
44
+ export declare const RUN_SPEED = 430;
45
+ export declare const GROUND_ACCEL = 5200;
46
+ export declare const AIR_ACCEL = 3000;
47
+ export declare const GROUND_DECEL = 4200;
48
+ export declare const AIR_DECEL = 1500;
49
+ export declare const JUMP_SPEED = 920;
50
+ export declare const PLAYER_GRAVITY = 2400;
51
+ export declare const SPEED_BOOST_MOVE = 1.4;
52
+ export declare const SPEED_BOOST_JUMP = 1.25;
53
+ export declare const KICK_TICKS: number;
54
+ /** The foot strikes while the kick has between these many ticks left (inclusive). */
55
+ export declare const KICK_ACTIVE_FROM: number;
56
+ export declare const KICK_ACTIVE_TO: number;
57
+ export declare const KICK_VX = 980;
58
+ export declare const KICK_VY = 620;
59
+ /** A ball above the knee is struck flatter. */
60
+ export declare const KICK_VY_HIGH = 380;
61
+ /** Share of the kicker's run speed the ball takes with it. */
62
+ export declare const KICK_CARRY = 0.3;
63
+ export declare const HEADER_TICKS: number;
64
+ export declare const HEADER_PUSH = 240;
65
+ export declare const HEADER_FORWARD = 140;
66
+ /** A header leaves toward the opponent's goal at least this fast. */
67
+ export declare const HEADER_MIN_FORWARD = 380;
68
+ /** Approach speed a head contact needs to count as a header (event and energy). */
69
+ export declare const HEADER_MIN_SPEED = 150;
70
+ export declare const SPECIAL_TICKS: number;
71
+ export declare const BALL_RADIUS = 26;
72
+ export declare const BALL_RADIUS_BIG = 44;
73
+ export declare const BALL_RADIUS_SMALL = 16;
74
+ export declare const BALL_GRAVITY = 1500;
75
+ export declare const BALL_MAX_SPEED = 1900;
76
+ export declare const BALL_GROUND_RESTITUTION = 0.7;
77
+ export declare const BALL_WALL_RESTITUTION = 0.8;
78
+ export declare const BALL_BOUNCY_RESTITUTION = 0.95;
79
+ export declare const BALL_BAR_RESTITUTION = 0.6;
80
+ export declare const BALL_HEAD_RESTITUTION = 0.7;
81
+ export declare const BALL_BODY_RESTITUTION = 0.5;
82
+ /** Per-tick velocity kept in the air and while rolling. */
83
+ export declare const BALL_AIR_DRAG = 0.9995;
84
+ export declare const BALL_ROLL_FRICTION = 0.985;
85
+ /** A ball resting on a crossbar rolls off toward the field at least this fast. */
86
+ export declare const BAR_ROLL_OFF_SPEED = 120;
87
+ /** Impacts slower than this make no `bounce` event. */
88
+ export declare const BOUNCE_EVENT_SPEED = 220;
89
+ /** A ball squeezed between both players pops up at least this fast, so it never stays wedged between them. */
90
+ export declare const SQUEEZE_POP_SPEED = 650;
91
+ /** Ball physics runs this many sub-steps per tick, so a fast ball never passes through a head. */
92
+ export declare const BALL_SUBSTEPS = 2;
93
+ export declare const BALL_DROP_Y = 300;
94
+ export declare const MATCH_SECONDS = 90;
95
+ export declare const KICKOFF_TICKS: number;
96
+ export declare const GOAL_PAUSE_TICKS: number;
97
+ /** Golden goal ends as a draw after this long without a goal, so an online room always closes. */
98
+ export declare const GOLDEN_GOAL_MAX_TICKS: number;
99
+ export declare const ENERGY_MAX = 100;
100
+ export declare const ENERGY_PER_SECOND = 1;
101
+ export declare const ENERGY_TOUCH = 1;
102
+ export declare const ENERGY_KICK = 2;
103
+ export declare const ENERGY_HEADER = 3;
104
+ export declare const TOUCH_COOLDOWN_TICKS = 30;
105
+ /** A special pressed with the ball this close to the player's body launches at once; otherwise it waits armed. */
106
+ export declare const SPECIAL_REACH = 200;
107
+ export declare const FIRE_SPEED = 1700;
108
+ export declare const FIRE_VY = -60;
109
+ export declare const FIRE_TICKS: number;
110
+ export declare const KNOCKBACK_VX = 750;
111
+ export declare const KNOCKBACK_VY = 380;
112
+ export declare const KNOCKBACK_STUN_TICKS: number;
113
+ export declare const CURVE_SPEED = 1150;
114
+ export declare const CURVE_VY = -250;
115
+ export declare const CURVE_TICKS: number;
116
+ export declare const CURVE_PERIOD_TICKS = 36;
117
+ export declare const CURVE_ACCEL = 2600;
118
+ export declare const ICE_SPEED = 1300;
119
+ export declare const ICE_VY = -300;
120
+ export declare const ICE_TICKS: number;
121
+ export declare const GHOST_SPEED = 1250;
122
+ export declare const GHOST_VY = -450;
123
+ export declare const GHOST_TICKS: number;
124
+ export declare const THUNDER_UP_VY = -1350;
125
+ /** The thunder lob peaks this far in front of the target goal's line. */
126
+ export declare const THUNDER_TARGET_GAP = 40;
127
+ export declare const THUNDER_DOWN_VY = 2000;
128
+ export declare const THUNDER_DOWN_VX = 120;
129
+ export declare const THUNDER_TICKS: number;
130
+ export declare const THUNDER_IMPACT_RESTITUTION = 0.4;
131
+ export declare const THUNDER_STUN_RADIUS = 160;
132
+ export declare const THUNDER_STUN_TICKS: number;
133
+ export declare const DASH_TICKS: number;
134
+ export declare const DASH_SPEED = 1100;
135
+ export declare const DASH_BALL_VX = 1600;
136
+ export declare const DASH_BALL_VY = -380;
137
+ export declare const POWERUP_SPAWN_MIN_TICKS: number;
138
+ export declare const POWERUP_SPAWN_MAX_TICKS: number;
139
+ export declare const POWERUP_MAX_ON_FIELD = 2;
140
+ export declare const POWERUP_SPAWN_MIN_X = 300;
141
+ export declare const POWERUP_SPAWN_MAX_X = 1300;
142
+ export declare const POWERUP_SPAWN_Y = -30;
143
+ export declare const POWERUP_RADIUS = 30;
144
+ export declare const POWERUP_FALL_SPEED = 150;
145
+ /** Where an item comes to rest: on the grass. */
146
+ export declare const POWERUP_REST_Y: number;
147
+ /** An item nobody collects vanishes this long after it spawned. */
148
+ export declare const POWERUP_LIFETIME_TICKS: number;
149
+ export declare const EFFECT_TICKS: number;
150
+ export declare const FREEZE_TICKS: number;
151
+ export declare const SHIELD_TICKS: number;
152
+ export declare const REVERSE_TICKS: number;
153
+ export declare const DOUBLE_GOAL_TICKS: number;
154
+ export declare const MAGNET_ACCEL = 1100;
155
+ export declare const MAGNET_RANGE = 700;
156
+ export declare const WIND_ACCEL = 420;
@@ -0,0 +1,166 @@
1
+ // Every tuning number of the simulation, in world units (y grows downward), seconds and ticks.
2
+ /** Fixed simulation rate: one input frame advances the match exactly one tick. */
3
+ export const TICK_RATE = 60;
4
+ export const DT = 1 / TICK_RATE;
5
+ /** Whole seconds to ticks. */
6
+ export const secondsToTicks = (seconds) => Math.round(seconds * TICK_RATE);
7
+ // World.
8
+ export const WORLD_W = 1600;
9
+ export const WORLD_H = 900;
10
+ export const GROUND_Y = 780;
11
+ export const CEILING_Y = 0;
12
+ export const CENTER_X = WORLD_W / 2;
13
+ // Goals. The left goal's mouth runs from x = 0 to its depth, the right one from WORLD_W - depth to WORLD_W.
14
+ export const GOAL_DEPTH = 110;
15
+ /** A big goal raises the crossbar and widens the frame. */
16
+ export const GOAL_DEPTH_BIG = 140;
17
+ export const CROSSBAR_Y = 520;
18
+ export const CROSSBAR_Y_BIG = 430;
19
+ export const CROSSBAR_Y_SMALL = 600;
20
+ export const CROSSBAR_THICKNESS = 14;
21
+ /** The shield stands this far in front of the goal line and is this thick. */
22
+ export const SHIELD_GAP = 6;
23
+ export const SHIELD_THICKNESS = 16;
24
+ // Players. A player's (x, y) is the point between the feet; y = GROUND_Y when standing.
25
+ export const HEAD_RADIUS = 46;
26
+ export const HEAD_SCALE_BIG = 1.6;
27
+ export const HEAD_SCALE_SMALL = 0.6;
28
+ /** Head scale during the Titan charge. */
29
+ export const HEAD_SCALE_TITAN = 2;
30
+ /** The chest circle sits on each character's neck point (CHARACTER_HEADS), between the body and the head. */
31
+ export const CHEST_RADIUS = 30;
32
+ export const BODY_RADIUS = 34;
33
+ /** The body circle's centre above the feet. */
34
+ export const BODY_CENTER_Y = 58;
35
+ export const FOOT_RADIUS = 22;
36
+ /** The resting foot, in front of the body. */
37
+ export const FOOT_FORWARD = 30;
38
+ export const FOOT_HEIGHT = 20;
39
+ /** The swinging foot during a kick reaches further and higher. */
40
+ export const KICK_FOOT_RADIUS = 34;
41
+ export const KICK_FOOT_FORWARD = 58;
42
+ export const KICK_FOOT_HEIGHT = 40;
43
+ export const PLAYER_START_X = [420, 1180];
44
+ export const PLAYER_MIN_X = 130;
45
+ export const PLAYER_MAX_X = WORLD_W - PLAYER_MIN_X;
46
+ /** Two players' bodies never come closer than this, centre to centre. */
47
+ export const PLAYER_SEPARATION = 2 * BODY_RADIUS + 4;
48
+ export const RUN_SPEED = 430;
49
+ export const GROUND_ACCEL = 5200;
50
+ export const AIR_ACCEL = 3000;
51
+ export const GROUND_DECEL = 4200;
52
+ export const AIR_DECEL = 1500;
53
+ export const JUMP_SPEED = 920;
54
+ export const PLAYER_GRAVITY = 2400;
55
+ export const SPEED_BOOST_MOVE = 1.4;
56
+ export const SPEED_BOOST_JUMP = 1.25;
57
+ // Kicks and headers. Animation lengths follow the sprite sequences (frames / fps).
58
+ export const KICK_TICKS = secondsToTicks(5 / 18);
59
+ /** The foot strikes while the kick has between these many ticks left (inclusive). */
60
+ export const KICK_ACTIVE_FROM = KICK_TICKS - 2;
61
+ export const KICK_ACTIVE_TO = KICK_TICKS - 11;
62
+ export const KICK_VX = 980;
63
+ export const KICK_VY = 620;
64
+ /** A ball above the knee is struck flatter. */
65
+ export const KICK_VY_HIGH = 380;
66
+ /** Share of the kicker's run speed the ball takes with it. */
67
+ export const KICK_CARRY = 0.3;
68
+ export const HEADER_TICKS = secondsToTicks(4 / 14);
69
+ export const HEADER_PUSH = 240;
70
+ export const HEADER_FORWARD = 140;
71
+ /** A header leaves toward the opponent's goal at least this fast. */
72
+ export const HEADER_MIN_FORWARD = 380;
73
+ /** Approach speed a head contact needs to count as a header (event and energy). */
74
+ export const HEADER_MIN_SPEED = 150;
75
+ export const SPECIAL_TICKS = secondsToTicks(6 / 12);
76
+ // Ball.
77
+ export const BALL_RADIUS = 26;
78
+ export const BALL_RADIUS_BIG = 44;
79
+ export const BALL_RADIUS_SMALL = 16;
80
+ export const BALL_GRAVITY = 1500;
81
+ export const BALL_MAX_SPEED = 1900;
82
+ export const BALL_GROUND_RESTITUTION = 0.7;
83
+ export const BALL_WALL_RESTITUTION = 0.8;
84
+ export const BALL_BOUNCY_RESTITUTION = 0.95;
85
+ export const BALL_BAR_RESTITUTION = 0.6;
86
+ export const BALL_HEAD_RESTITUTION = 0.7;
87
+ export const BALL_BODY_RESTITUTION = 0.5;
88
+ /** Per-tick velocity kept in the air and while rolling. */
89
+ export const BALL_AIR_DRAG = 0.9995;
90
+ export const BALL_ROLL_FRICTION = 0.985;
91
+ /** A ball resting on a crossbar rolls off toward the field at least this fast. */
92
+ export const BAR_ROLL_OFF_SPEED = 120;
93
+ /** Impacts slower than this make no `bounce` event. */
94
+ export const BOUNCE_EVENT_SPEED = 220;
95
+ /** A ball squeezed between both players pops up at least this fast, so it never stays wedged between them. */
96
+ export const SQUEEZE_POP_SPEED = 650;
97
+ /** Ball physics runs this many sub-steps per tick, so a fast ball never passes through a head. */
98
+ export const BALL_SUBSTEPS = 2;
99
+ export const BALL_DROP_Y = 300;
100
+ // Match flow.
101
+ export const MATCH_SECONDS = 90;
102
+ export const KICKOFF_TICKS = secondsToTicks(1.2);
103
+ export const GOAL_PAUSE_TICKS = secondsToTicks(1.5);
104
+ /** Golden goal ends as a draw after this long without a goal, so an online room always closes. */
105
+ export const GOLDEN_GOAL_MAX_TICKS = secondsToTicks(60);
106
+ // Energy.
107
+ export const ENERGY_MAX = 100;
108
+ export const ENERGY_PER_SECOND = 1;
109
+ export const ENERGY_TOUCH = 1;
110
+ export const ENERGY_KICK = 2;
111
+ export const ENERGY_HEADER = 3;
112
+ export const TOUCH_COOLDOWN_TICKS = 30;
113
+ /** A special pressed with the ball this close to the player's body launches at once; otherwise it waits armed. */
114
+ export const SPECIAL_REACH = 200;
115
+ // Shots launched by specials and the fire-shot power-up.
116
+ export const FIRE_SPEED = 1700;
117
+ export const FIRE_VY = -60;
118
+ export const FIRE_TICKS = secondsToTicks(0.75);
119
+ export const KNOCKBACK_VX = 750;
120
+ export const KNOCKBACK_VY = 380;
121
+ export const KNOCKBACK_STUN_TICKS = secondsToTicks(0.5);
122
+ export const CURVE_SPEED = 1150;
123
+ export const CURVE_VY = -250;
124
+ export const CURVE_TICKS = secondsToTicks(1.2);
125
+ export const CURVE_PERIOD_TICKS = 36;
126
+ export const CURVE_ACCEL = 2600;
127
+ export const ICE_SPEED = 1300;
128
+ export const ICE_VY = -300;
129
+ export const ICE_TICKS = secondsToTicks(2);
130
+ export const GHOST_SPEED = 1250;
131
+ export const GHOST_VY = -450;
132
+ export const GHOST_TICKS = secondsToTicks(1.2);
133
+ export const THUNDER_UP_VY = -1350;
134
+ /** The thunder lob peaks this far in front of the target goal's line. */
135
+ export const THUNDER_TARGET_GAP = 40;
136
+ export const THUNDER_DOWN_VY = 2000;
137
+ export const THUNDER_DOWN_VX = 120;
138
+ export const THUNDER_TICKS = secondsToTicks(3);
139
+ export const THUNDER_IMPACT_RESTITUTION = 0.4;
140
+ export const THUNDER_STUN_RADIUS = 160;
141
+ export const THUNDER_STUN_TICKS = secondsToTicks(0.8);
142
+ export const DASH_TICKS = secondsToTicks(0.5);
143
+ export const DASH_SPEED = 1100;
144
+ export const DASH_BALL_VX = 1600;
145
+ export const DASH_BALL_VY = -380;
146
+ // Power-ups.
147
+ export const POWERUP_SPAWN_MIN_TICKS = secondsToTicks(6);
148
+ export const POWERUP_SPAWN_MAX_TICKS = secondsToTicks(9);
149
+ export const POWERUP_MAX_ON_FIELD = 2;
150
+ export const POWERUP_SPAWN_MIN_X = 300;
151
+ export const POWERUP_SPAWN_MAX_X = 1300;
152
+ export const POWERUP_SPAWN_Y = -30;
153
+ export const POWERUP_RADIUS = 30;
154
+ export const POWERUP_FALL_SPEED = 150;
155
+ /** Where an item comes to rest: on the grass. */
156
+ export const POWERUP_REST_Y = GROUND_Y - POWERUP_RADIUS - 10;
157
+ /** An item nobody collects vanishes this long after it spawned. */
158
+ export const POWERUP_LIFETIME_TICKS = secondsToTicks(12);
159
+ export const EFFECT_TICKS = secondsToTicks(7);
160
+ export const FREEZE_TICKS = secondsToTicks(2);
161
+ export const SHIELD_TICKS = secondsToTicks(6);
162
+ export const REVERSE_TICKS = secondsToTicks(5);
163
+ export const DOUBLE_GOAL_TICKS = secondsToTicks(15);
164
+ export const MAGNET_ACCEL = 1100;
165
+ export const MAGNET_RANGE = 700;
166
+ export const WIND_ACCEL = 420;
@@ -0,0 +1,14 @@
1
+ import type { MatchState, PowerUpItem } from './state.ts';
2
+ import type { PowerUpKind, Side } from './types.ts';
3
+ /**
4
+ * Drops a power-up of `kind` at `x`, from the sky by default or at height `y`. It ignores the limit of two items on
5
+ * the field and leaves the spawn timer alone.
6
+ */
7
+ export declare function debugSpawnPowerUp(state: MatchState, kind: PowerUpKind, x: number, y?: number): PowerUpItem;
8
+ /** Fills `player`'s energy meter, so their special is ready. */
9
+ export declare function debugFillEnergy(state: MatchState, player: Side): void;
10
+ /**
11
+ * Sets the regular time left to `seconds`; 0 ends regular time on the next tick of play. A match in golden goal goes
12
+ * back to regular time.
13
+ */
14
+ export declare function debugSetMatchTime(state: MatchState, seconds: number): void;
@@ -0,0 +1,34 @@
1
+ import { ENERGY_MAX, POWERUP_RADIUS, POWERUP_SPAWN_Y, secondsToTicks, WORLD_W } from "./constants.js";
2
+ import { spawnItem } from "./powerups.js";
3
+ import { POWERUP_KINDS } from "./types.js";
4
+ /**
5
+ * Drops a power-up of `kind` at `x`, from the sky by default or at height `y`. It ignores the limit of two items on
6
+ * the field and leaves the spawn timer alone.
7
+ */
8
+ export function debugSpawnPowerUp(state, kind, x, y = POWERUP_SPAWN_Y) {
9
+ if (!POWERUP_KINDS.includes(kind)) {
10
+ throw new RangeError(`debugSpawnPowerUp: unknown power-up ${String(kind)}`);
11
+ }
12
+ if (!Number.isFinite(x) || !Number.isFinite(y)) {
13
+ throw new RangeError('debugSpawnPowerUp: x and y must be finite');
14
+ }
15
+ const clampedX = Math.min(Math.max(x, POWERUP_RADIUS), WORLD_W - POWERUP_RADIUS);
16
+ return spawnItem(state, kind, clampedX, y);
17
+ }
18
+ /** Fills `player`'s energy meter, so their special is ready. */
19
+ export function debugFillEnergy(state, player) {
20
+ state.players[player].energy = ENERGY_MAX;
21
+ }
22
+ /**
23
+ * Sets the regular time left to `seconds`; 0 ends regular time on the next tick of play. A match in golden goal goes
24
+ * back to regular time.
25
+ */
26
+ export function debugSetMatchTime(state, seconds) {
27
+ if (!Number.isFinite(seconds) || seconds < 0) {
28
+ throw new RangeError(`debugSetMatchTime: seconds must be zero or more, got ${seconds}`);
29
+ }
30
+ // At least one tick, so the clock still runs out through the normal rules.
31
+ state.clockTicks = Math.max(1, secondsToTicks(seconds));
32
+ state.goldenGoal = false;
33
+ state.goldenTicks = 0;
34
+ }
@@ -0,0 +1,79 @@
1
+ import type { PowerUpKind, Side, SpecialKind } from './types.ts';
2
+ export type BounceSurface = 'ground' | 'wall' | 'ceiling' | 'player' | 'shield';
3
+ export type WhistleReason = 'kickoff' | 'goldenGoal' | 'end';
4
+ export type SimEvent =
5
+ /** A player's foot struck the ball. */
6
+ {
7
+ type: 'kick';
8
+ player: Side;
9
+ x: number;
10
+ y: number;
11
+ speed: number;
12
+ }
13
+ /** The ball came off a player's head with some pace. */
14
+ | {
15
+ type: 'header';
16
+ player: Side;
17
+ x: number;
18
+ y: number;
19
+ speed: number;
20
+ }
21
+ /** The ball bounced off something; `thunder` marks the slam of a thunder drop. */
22
+ | {
23
+ type: 'bounce';
24
+ surface: BounceSurface;
25
+ x: number;
26
+ y: number;
27
+ speed: number;
28
+ thunder?: true;
29
+ }
30
+ /** The ball hit a crossbar; `goal` is the side whose goal it is. */
31
+ | {
32
+ type: 'post';
33
+ goal: Side;
34
+ x: number;
35
+ y: number;
36
+ speed: number;
37
+ }
38
+ /** `scorer` scored `points` (2 with the double-goal power-up) into the other side's goal, or into their own. */
39
+ | {
40
+ type: 'goal';
41
+ scorer: Side;
42
+ points: number;
43
+ ownGoal: boolean;
44
+ score: [number, number];
45
+ golden: boolean;
46
+ } | {
47
+ type: 'powerupSpawn';
48
+ id: number;
49
+ kind: PowerUpKind;
50
+ x: number;
51
+ }
52
+ /** `player` collected an item, by touching it or with the ball they touched last (`byBall`). */
53
+ | {
54
+ type: 'powerupPick';
55
+ id: number;
56
+ kind: PowerUpKind;
57
+ player: Side;
58
+ byBall: boolean;
59
+ }
60
+ /** An effect on `target` that `owner` collected ran out or was used up. */
61
+ | {
62
+ type: 'effectEnd';
63
+ kind: PowerUpKind;
64
+ owner: Side;
65
+ target: Side;
66
+ }
67
+ /** A special fired: a shot launched, or the titan charge began. */
68
+ | {
69
+ type: 'special';
70
+ player: Side;
71
+ kind: SpecialKind;
72
+ }
73
+ /** The referee: the ball dropped for a kickoff, golden goal began, or the match ended. */
74
+ | {
75
+ type: 'whistle';
76
+ reason: WhistleReason;
77
+ };
78
+ export type SimEventType = SimEvent['type'];
79
+ export declare const SIM_EVENT_TYPES: readonly SimEventType[];
@@ -0,0 +1,12 @@
1
+ export const SIM_EVENT_TYPES = [
2
+ 'kick',
3
+ 'header',
4
+ 'bounce',
5
+ 'post',
6
+ 'goal',
7
+ 'powerupSpawn',
8
+ 'powerupPick',
9
+ 'effectEnd',
10
+ 'special',
11
+ 'whistle'
12
+ ];