@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,215 @@
1
+ import { CEILING_Y, GROUND_Y, WORLD_W } from "./constants.js";
2
+ import { closestPoint, crossbarRect, shieldRect } from "./geometry.js";
3
+ /** A centre within this of a wall's grown outline counts as touching it, not inside it. */
4
+ const EPS = 1e-6;
5
+ /**
6
+ * The thin solids of the current goal frames: both crossbars, and each goal's shield while it stands. With a shield
7
+ * up, the bar reaches across the small gap to it, so bar and shield tops are one surface: a ball rolls from one onto
8
+ * the other instead of settling in the notch between them.
9
+ */
10
+ export function wallsOf(state) {
11
+ const walls = [];
12
+ for (const side of [0, 1]) {
13
+ const goal = state.goals[side];
14
+ const bar = crossbarRect(goal, side);
15
+ if (goal.shield) {
16
+ const shield = shieldRect(goal, side);
17
+ walls.push({ kind: 'bar', side, rect: side === 0 ? { ...bar, right: shield.left } : { ...bar, left: shield.right } });
18
+ walls.push({ kind: 'shield', side, rect: shield });
19
+ }
20
+ else {
21
+ walls.push({ kind: 'bar', side, rect: bar });
22
+ }
23
+ }
24
+ return walls;
25
+ }
26
+ /** Distance from a point to a rectangle (0 inside it). */
27
+ function distanceTo(rect, x, y) {
28
+ const near = closestPoint(rect, x, y);
29
+ const dx = x - near.x;
30
+ const dy = y - near.y;
31
+ // Not the library hypot: engines round it differently (V8 on the server, JavaScriptCore on an iPhone), and a browser's
32
+ // prediction must step exactly as the room does. A square root is exact everywhere.
33
+ return Math.sqrt(dx * dx + dy * dy);
34
+ }
35
+ /** Whether a ball of radius `r` centred at (x, y) overlaps `rect` by more than rounding. */
36
+ export function overlapsWall(rect, x, y, r) {
37
+ return distanceTo(rect, x, y) < r - EPS;
38
+ }
39
+ /**
40
+ * The first point where the centre, moving from (x0, y0) by (dx, dy), comes within `r` of `rect`, or null if it never
41
+ * does on this move. A start already within `r` is not a hit: `resolveInside` deals with it.
42
+ */
43
+ export function sweepRect(rect, r, x0, y0, dx, dy) {
44
+ if (distanceTo(rect, x0, y0) < r - EPS) {
45
+ return null;
46
+ }
47
+ // Slab test against the rectangle grown by r on every side.
48
+ let tEnter = -Infinity;
49
+ let tExit = Infinity;
50
+ let axis = 'x';
51
+ for (const [p, d, lo, hi, name] of [[x0, dx, rect.left - r, rect.right + r, 'x'], [y0, dy, rect.top - r, rect.bottom + r, 'y']]) {
52
+ if (Math.abs(d) < 1e-12) {
53
+ if (p < lo || p > hi) {
54
+ return null;
55
+ }
56
+ continue;
57
+ }
58
+ const a = (lo - p) / d;
59
+ const b = (hi - p) / d;
60
+ const near = Math.min(a, b);
61
+ const far = Math.max(a, b);
62
+ if (near > tEnter) {
63
+ tEnter = near;
64
+ axis = name;
65
+ }
66
+ tExit = Math.min(tExit, far);
67
+ }
68
+ if (tEnter > tExit || tEnter > 1 || tExit < 0) {
69
+ return null;
70
+ }
71
+ const t = Math.max(0, tEnter);
72
+ const qx = x0 + dx * t;
73
+ const qy = y0 + dy * t;
74
+ const besideX = qx < rect.left || qx > rect.right;
75
+ const besideY = qy < rect.top || qy > rect.bottom;
76
+ if (!(besideX && besideY) && tEnter >= 0) {
77
+ // A flat face: the left or right one when the x slab was entered last, else the top or bottom.
78
+ const nx = axis === 'x' ? (dx > 0 ? -1 : 1) : 0;
79
+ const ny = axis === 'y' ? (dy > 0 ? -1 : 1) : 0;
80
+ return { t, x: qx, y: qy, nx, ny };
81
+ }
82
+ // A rounded corner: the move against the circle of radius r around the corner point.
83
+ const cx = qx < rect.left ? rect.left : rect.right;
84
+ const cy = qy < rect.top ? rect.top : rect.bottom;
85
+ const fx = x0 - cx;
86
+ const fy = y0 - cy;
87
+ const a = dx * dx + dy * dy;
88
+ const b = 2 * (fx * dx + fy * dy);
89
+ const c = fx * fx + fy * fy - r * r;
90
+ const disc = b * b - 4 * a * c;
91
+ if (a < 1e-12 || disc < 0) {
92
+ return null;
93
+ }
94
+ const tc = (-b - Math.sqrt(disc)) / (2 * a);
95
+ if (tc < 0 || tc > 1) {
96
+ return null;
97
+ }
98
+ const x = x0 + dx * tc;
99
+ const y = y0 + dy * tc;
100
+ return { t: tc, x, y, nx: (x - cx) / r, ny: (y - cy) / r };
101
+ }
102
+ /** The earliest wall the move from (x0, y0) by (dx, dy) touches, or null. */
103
+ export function firstWallHit(walls, r, x0, y0, dx, dy) {
104
+ let best = null;
105
+ for (const wall of walls) {
106
+ const hit = sweepRect(wall.rect, r, x0, y0, dx, dy);
107
+ if (hit && (!best || hit.t < best.t)) {
108
+ best = { wall, ...hit };
109
+ }
110
+ }
111
+ return best;
112
+ }
113
+ /** Which way the field is from a goal's frame: right of the left goal, left of the right one. */
114
+ const fieldSideOf = (side) => (side === 0 ? 1 : -1);
115
+ /**
116
+ * Moves a ball whose centre is inside `wall` (grown by its radius) out of it: onto the top of a shield it is above,
117
+ * otherwise out of the shield's field face; onto the top of a crossbar it is above the middle of, otherwise under it.
118
+ * Returns the normal it left along.
119
+ */
120
+ export function resolveInside(ball, wall) {
121
+ const { rect } = wall;
122
+ if (wall.kind === 'shield' && ball.y >= rect.top) {
123
+ const nx = fieldSideOf(wall.side);
124
+ ball.x = nx === 1 ? rect.right + ball.r : rect.left - ball.r;
125
+ return { nx, ny: 0 };
126
+ }
127
+ const above = wall.kind === 'shield' || ball.y < (rect.top + rect.bottom) / 2;
128
+ ball.y = above ? rect.top - ball.r : rect.bottom + ball.r;
129
+ return { nx: 0, ny: above ? -1 : 1 };
130
+ }
131
+ /** The ball never leaves the field: inside the side walls, under the ceiling and on or above the grass. */
132
+ export function clampToField(ball) {
133
+ ball.x = Math.min(Math.max(ball.x, ball.r), WORLD_W - ball.r);
134
+ ball.y = Math.min(Math.max(ball.y, CEILING_Y + ball.r), GROUND_Y - ball.r);
135
+ if (ball.y + ball.r >= GROUND_Y && ball.vy > 0) {
136
+ ball.vy = 0;
137
+ }
138
+ }
139
+ /** Whether the ball is fully past goal `side`'s line under its crossbar: a goal (match `goalEntered`). */
140
+ export function ballInMouth(ball, goal, side) {
141
+ const pastLine = side === 0 ? ball.x <= goal.depth - ball.r : ball.x >= WORLD_W - goal.depth + ball.r;
142
+ return pastLine && ball.y > crossbarRect(goal, side).bottom;
143
+ }
144
+ /**
145
+ * A ball is never behind (or inside) a standing shield below its top: one there goes to just in front of its field face.
146
+ * Play never puts it there; this keeps it so whatever happened, a frame that changed or a state set from outside.
147
+ */
148
+ export function keepInFrontOfShield(state, side) {
149
+ const goal = state.goals[side];
150
+ if (!goal.shield) {
151
+ return;
152
+ }
153
+ const ball = state.ball;
154
+ const field = fieldSideOf(side);
155
+ const wall = shieldRect(goal, side);
156
+ const face = field === 1 ? wall.right : wall.left;
157
+ if ((ball.x - face) * field < ball.r - EPS && ball.y > wall.top) {
158
+ ball.x = face + field * ball.r;
159
+ ball.vx = field === 1 ? Math.max(ball.vx, 0) : Math.min(ball.vx, 0);
160
+ }
161
+ }
162
+ /**
163
+ * After a goal frame or the ball changed shape in play: a change never scores and never leaves the ball inside or
164
+ * behind a wall. A ball behind a shield's field face (below its top) goes to the field side; a ball a crossbar moved
165
+ * 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.
166
+ */
167
+ export function settleFrameChange(state, before) {
168
+ const ball = state.ball;
169
+ const changed = ball.r !== before.r || state.goals.some((goal, i) => {
170
+ const old = before.goals[i];
171
+ return goal.crossbarY !== old.crossbarY || goal.depth !== old.depth || goal.shield !== old.shield;
172
+ });
173
+ if (!changed || state.phase !== 'play') {
174
+ return;
175
+ }
176
+ for (const side of [0, 1]) {
177
+ const goal = state.goals[side];
178
+ const field = fieldSideOf(side);
179
+ const bar = crossbarRect(goal, side);
180
+ const oldGoal = before.goals[side];
181
+ if (oldGoal.crossbarY !== goal.crossbarY) {
182
+ // The bar (and the shield's top, which follows it) moved up or down through the ball: the ball stays on the side of
183
+ // it it was on, carried on top or kept under it.
184
+ const oldBar = crossbarRect(oldGoal, side);
185
+ const reach = goal.shield ? shieldRect(goal, side) : bar;
186
+ const left = Math.min(bar.left, reach.left);
187
+ const right = Math.max(bar.right, reach.right);
188
+ const swept = { left, right, top: Math.min(oldBar.top, bar.top), bottom: Math.max(oldBar.bottom, bar.bottom) };
189
+ if (overlapsWall(swept, ball.x, ball.y, ball.r) || overlapsWall(bar, ball.x, ball.y, ball.r)) {
190
+ if (ball.y < (oldBar.top + oldBar.bottom) / 2) {
191
+ ball.y = bar.top - ball.r;
192
+ ball.vy = Math.min(ball.vy, 0);
193
+ }
194
+ else if (ball.x >= bar.left - ball.r && ball.x <= bar.right + ball.r) {
195
+ ball.y = bar.bottom + ball.r;
196
+ ball.vy = Math.max(ball.vy, 0);
197
+ }
198
+ }
199
+ }
200
+ else if (overlapsWall(bar, ball.x, ball.y, ball.r)) {
201
+ resolveInside(ball, { kind: 'bar', side, rect: bar });
202
+ }
203
+ keepInFrontOfShield(state, side);
204
+ if (!goal.shield && ballInMouth(ball, goal, side)) {
205
+ ball.x = side === 0 ? goal.depth + ball.r : WORLD_W - goal.depth - ball.r;
206
+ ball.vx = field === 1 ? Math.max(ball.vx, 0) : Math.min(ball.vx, 0);
207
+ }
208
+ }
209
+ clampToField(ball);
210
+ for (const wall of wallsOf(state)) {
211
+ if (overlapsWall(wall.rect, ball.x, ball.y, ball.r)) {
212
+ resolveInside(ball, wall);
213
+ }
214
+ }
215
+ }
package/package.json CHANGED
@@ -1,6 +1,59 @@
1
1
  {
2
2
  "name": "@maco-online/head-clash-rules",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "Head Clash simulation (big-head 1v1 football with power-ups, specials and a bot), its online wire format and the room game definition that runs it on the Maco online server",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/myangel26/maco-online.git",
10
+ "directory": "games/head-clash"
11
+ },
12
+ "sideEffects": false,
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "default": "./dist/index.js"
17
+ },
18
+ "./sim": {
19
+ "types": "./dist/sim/index.d.ts",
20
+ "default": "./dist/sim/index.js"
21
+ },
22
+ "./sim/*": {
23
+ "types": "./dist/sim/*.d.ts",
24
+ "default": "./dist/sim/*.js"
25
+ },
26
+ "./net/wire": {
27
+ "types": "./dist/net/wire.d.ts",
28
+ "default": "./dist/net/wire.js"
29
+ },
30
+ "./net/predicted-sim": {
31
+ "types": "./dist/net/predicted-sim.d.ts",
32
+ "default": "./dist/net/predicted-sim.js"
33
+ }
34
+ },
35
+ "files": [
36
+ "dist"
37
+ ],
38
+ "maco": {
39
+ "protocolVersion": 1
40
+ },
41
+ "dependencies": {
42
+ "@maco-online/online-core": "0.9.0"
43
+ },
44
+ "peerDependencies": {
45
+ "zod": "^4.1.12"
46
+ },
47
+ "devDependencies": {
48
+ "@types/node": "24.19.2",
49
+ "typescript": "5.9.3",
50
+ "vite": "7.3.8",
51
+ "vitest": "3.2.4",
52
+ "zod": "4.1.12"
53
+ },
54
+ "scripts": {
55
+ "build": "node ../../scripts/clean-build.mjs && tsc -p tsconfig.build.json",
56
+ "typecheck": "tsc --noEmit -p .",
57
+ "test": "pnpm build && vitest run"
58
+ }
6
59
  }