@maco-online/shuttle-blitz-rules 0.0.0 → 0.1.1
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/dist/definition.d.ts +18 -0
- package/dist/definition.js +36 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +2 -0
- package/dist/net/wire.d.ts +65 -0
- package/dist/net/wire.js +108 -0
- package/dist/room-match.d.ts +26 -0
- package/dist/room-match.js +86 -0
- package/dist/sim/actor.d.ts +39 -0
- package/dist/sim/actor.js +146 -0
- package/dist/sim/ai.d.ts +41 -0
- package/dist/sim/ai.js +181 -0
- package/dist/sim/events.d.ts +170 -0
- package/dist/sim/events.js +54 -0
- package/dist/sim/index.d.ts +18 -0
- package/dist/sim/index.js +10 -0
- package/dist/sim/match.d.ts +66 -0
- package/dist/sim/match.js +434 -0
- package/dist/sim/pose-state.d.ts +6 -0
- package/dist/sim/pose-state.js +39 -0
- package/dist/sim/rng.d.ts +8 -0
- package/dist/sim/rng.js +15 -0
- package/dist/sim/rules.d.ts +34 -0
- package/dist/sim/rules.js +234 -0
- package/dist/sim/shots.d.ts +47 -0
- package/dist/sim/shots.js +220 -0
- package/dist/sim/slowmo.d.ts +21 -0
- package/dist/sim/slowmo.js +57 -0
- package/dist/sim/state.d.ts +167 -0
- package/dist/sim/state.js +1 -0
- package/dist/sim/training.d.ts +12 -0
- package/dist/sim/training.js +112 -0
- package/dist/sim/types.d.ts +15 -0
- package/dist/sim/types.js +2 -0
- package/dist/sim/world.d.ts +86 -0
- package/dist/sim/world.js +79 -0
- package/package.json +45 -4
- package/README.md +0 -3
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { BANNER_KEYS, POP_KEYS } from "./events.js";
|
|
2
|
+
import { pop } from "./shots.js";
|
|
3
|
+
import { other } from "./state.js";
|
|
4
|
+
import { trainLanded } from "./training.js";
|
|
5
|
+
import { CAP, COURT_L, COURT_R, FLOOR, HELD_CORK_DROP, LINE_IN, NET_X, POINTS, SERVE_STAMINA, SERVICE_COURT_GAP, SHORT_SERVICE } from "./world.js";
|
|
6
|
+
const SIDES = ['p', 'ai'];
|
|
7
|
+
/** Serve stances, both inside the service court (behind the short service line): the server a step nearer the net. */
|
|
8
|
+
export const SERVE_X = {
|
|
9
|
+
p: { serving: NET_X - SHORT_SERVICE - SERVICE_COURT_GAP - 10, receiving: 600 },
|
|
10
|
+
ai: { serving: NET_X + SHORT_SERVICE + SERVICE_COURT_GAP + 10, receiving: 1320 },
|
|
11
|
+
};
|
|
12
|
+
/** Until the serve is struck, server and receiver stay in their own service court: behind the short line, inside the back line. */
|
|
13
|
+
export function keepInServiceCourt(a) {
|
|
14
|
+
const min = a.side === 'p' ? COURT_L + SERVICE_COURT_GAP : NET_X + SHORT_SERVICE + SERVICE_COURT_GAP;
|
|
15
|
+
const max = a.side === 'p' ? NET_X - SHORT_SERVICE - SERVICE_COURT_GAP : COURT_R - SERVICE_COURT_GAP;
|
|
16
|
+
a.x = Math.min(max, Math.max(min, a.x));
|
|
17
|
+
}
|
|
18
|
+
export const POINT_PAUSE = 1.4;
|
|
19
|
+
export const GAME_PAUSE = 2.0;
|
|
20
|
+
export const AI_SERVE_DELAY = 1.1;
|
|
21
|
+
/** True when either side wins the game with its next point. */
|
|
22
|
+
export function isGamePoint(score) {
|
|
23
|
+
return SIDES.some((k) => {
|
|
24
|
+
const n = score[k] + 1;
|
|
25
|
+
const o = score[other(k)];
|
|
26
|
+
return (n >= POINTS && n - o >= 2) || n >= CAP;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
export function gameWinner(score) {
|
|
30
|
+
for (const k of SIDES) {
|
|
31
|
+
const o = score[other(k)];
|
|
32
|
+
if (score[k] >= CAP || (score[k] >= POINTS && score[k] - o >= 2))
|
|
33
|
+
return k;
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
export const gamesNeeded = (bestOf) => Math.ceil(bestOf / 2);
|
|
38
|
+
export function matchWinner(games, bestOf) {
|
|
39
|
+
const need = gamesNeeded(bestOf);
|
|
40
|
+
for (const k of SIDES)
|
|
41
|
+
if (games[k] >= need)
|
|
42
|
+
return k;
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
/** Puts a held shuttle in its server's free hand, hanging by the feathers. Does nothing while it is not held. */
|
|
46
|
+
export function placeHeldShuttle(s) {
|
|
47
|
+
const sh = s.shuttle;
|
|
48
|
+
const a = sh.heldBy === 'p' ? s.p : sh.heldBy === 'ai' ? s.ai : null;
|
|
49
|
+
if (!a)
|
|
50
|
+
return;
|
|
51
|
+
sh.x = a.x + a.facing * a.serveHand.x;
|
|
52
|
+
sh.y = a.y + a.serveHand.y + HELD_CORK_DROP;
|
|
53
|
+
}
|
|
54
|
+
export function setupServe(ctx) {
|
|
55
|
+
const { s, out } = ctx;
|
|
56
|
+
const p = s.p;
|
|
57
|
+
const ai = s.ai;
|
|
58
|
+
if (!ai)
|
|
59
|
+
throw new Error('setupServe: a match needs an opponent');
|
|
60
|
+
s.phase = 'serve';
|
|
61
|
+
s.rally = 0;
|
|
62
|
+
s.slowmoUsed = false;
|
|
63
|
+
const sh = s.shuttle;
|
|
64
|
+
Object.assign(sh, {
|
|
65
|
+
live: false,
|
|
66
|
+
netted: false,
|
|
67
|
+
kind: 'clear',
|
|
68
|
+
heldBy: s.server,
|
|
69
|
+
lastHitter: null,
|
|
70
|
+
vx: 0,
|
|
71
|
+
vy: 0,
|
|
72
|
+
cross: false,
|
|
73
|
+
});
|
|
74
|
+
p.x = s.server === 'p' ? SERVE_X.p.serving : SERVE_X.p.receiving;
|
|
75
|
+
ai.x = s.server === 'ai' ? SERVE_X.ai.serving : SERVE_X.ai.receiving;
|
|
76
|
+
for (const a of [p, ai]) {
|
|
77
|
+
a.y = FLOOR;
|
|
78
|
+
a.vy = 0;
|
|
79
|
+
a.onGround = true;
|
|
80
|
+
a.swing = null;
|
|
81
|
+
a.pending = null;
|
|
82
|
+
a.mood = null;
|
|
83
|
+
a.stamina = Math.min(100, a.stamina + SERVE_STAMINA);
|
|
84
|
+
a.holding = s.server === a.side;
|
|
85
|
+
}
|
|
86
|
+
p.charge = null;
|
|
87
|
+
ai.charge = null;
|
|
88
|
+
if (ai.brain)
|
|
89
|
+
ai.brain.plan = null;
|
|
90
|
+
placeHeldShuttle(s);
|
|
91
|
+
if (s.server === 'p') {
|
|
92
|
+
out.push({ type: 'banner', key: BANNER_KEYS.yourServe, sub: BANNER_KEYS.yourServeHint, vars: {}, side: 'p', duration: 0 });
|
|
93
|
+
}
|
|
94
|
+
else if (ai.brain) {
|
|
95
|
+
ai.brain.serveTimer = AI_SERVE_DELAY;
|
|
96
|
+
out.push({ type: 'banner', key: BANNER_KEYS.oppServe, sub: BANNER_KEYS.oppServeHint, vars: {}, side: 'ai', duration: 1.0 });
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
// In PvP the other browser drives this side, so it must not be auto-served by the AI timer.
|
|
100
|
+
out.push({ type: 'banner', key: BANNER_KEYS.oppServe, sub: BANNER_KEYS.oppServeHint, vars: {}, side: 'ai', duration: 0 });
|
|
101
|
+
}
|
|
102
|
+
out.push({ type: 'serveReady', side: s.server });
|
|
103
|
+
}
|
|
104
|
+
/** The shuttle reached the floor: settle the point (or the training ball). */
|
|
105
|
+
export function land(ctx) {
|
|
106
|
+
const { s, out } = ctx;
|
|
107
|
+
const sh = s.shuttle;
|
|
108
|
+
sh.live = false;
|
|
109
|
+
sh.y = FLOOR - 6;
|
|
110
|
+
sh.vx = 0;
|
|
111
|
+
sh.vy = 0;
|
|
112
|
+
out.push({ type: 'floor', x: sh.x, kind: sh.kind });
|
|
113
|
+
if (s.mode === 'training') {
|
|
114
|
+
trainLanded(ctx);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
// A live shuttle always has a hitter: serves and strikes set it before launch.
|
|
118
|
+
const hitter = sh.lastHitter ?? 'p';
|
|
119
|
+
const inB = sh.x >= COURT_L - LINE_IN && sh.x <= COURT_R + LINE_IN;
|
|
120
|
+
let winner;
|
|
121
|
+
let reason;
|
|
122
|
+
if (sh.netted) {
|
|
123
|
+
winner = other(hitter);
|
|
124
|
+
reason = 'net';
|
|
125
|
+
}
|
|
126
|
+
else if (!inB) {
|
|
127
|
+
winner = other(hitter);
|
|
128
|
+
reason = 'out';
|
|
129
|
+
// Called where it landed, so a shuttle on the green beyond the line reads as out at a glance.
|
|
130
|
+
pop(ctx, POP_KEYS.out, sh.x, FLOOR - 90, 'small');
|
|
131
|
+
}
|
|
132
|
+
else if (s.rally === 1 && Math.abs(sh.x - NET_X) < SHORT_SERVICE) {
|
|
133
|
+
// Still the serve (nobody has hit it back) and it fell between the net and the short service line.
|
|
134
|
+
winner = other(hitter);
|
|
135
|
+
reason = 'fault';
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
winner = sh.x < NET_X ? 'ai' : 'p';
|
|
139
|
+
reason = 'in';
|
|
140
|
+
}
|
|
141
|
+
if (winner === 'ai' && hitter === 'p' && reason !== 'in')
|
|
142
|
+
s.stats.errors++;
|
|
143
|
+
awardPoint(ctx, winner, reason);
|
|
144
|
+
}
|
|
145
|
+
export function awardPoint(ctx, winner, reason) {
|
|
146
|
+
const { s, out } = ctx;
|
|
147
|
+
s.phase = 'point';
|
|
148
|
+
s.timer = POINT_PAUSE;
|
|
149
|
+
s.stats.longest = Math.max(s.stats.longest, s.rally);
|
|
150
|
+
s.score[winner]++;
|
|
151
|
+
s.server = winner;
|
|
152
|
+
out.push({ type: 'point', winner, reason });
|
|
153
|
+
if (winner === 'p')
|
|
154
|
+
out.push({ type: 'excite', amount: 1 });
|
|
155
|
+
out.push({
|
|
156
|
+
type: 'banner',
|
|
157
|
+
key: winner === 'p' ? BANNER_KEYS.pointWon : BANNER_KEYS.pointLost,
|
|
158
|
+
sub: BANNER_KEYS.pointReason[reason],
|
|
159
|
+
vars: { p: s.score.p, ai: s.score.ai },
|
|
160
|
+
side: winner,
|
|
161
|
+
duration: 1.3,
|
|
162
|
+
});
|
|
163
|
+
const w = winner === 'p' ? s.p : s.ai;
|
|
164
|
+
const l = winner === 'p' ? s.ai : s.p;
|
|
165
|
+
if (w)
|
|
166
|
+
w.mood = { type: 'celebrate', t: 1.2, hold: false };
|
|
167
|
+
if (l)
|
|
168
|
+
l.mood = { type: 'sad', t: 1.2, hold: false };
|
|
169
|
+
}
|
|
170
|
+
export function afterPoint(ctx) {
|
|
171
|
+
const { s, out } = ctx;
|
|
172
|
+
const gw = gameWinner(s.score);
|
|
173
|
+
if (!gw) {
|
|
174
|
+
setupServe(ctx);
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
s.games[gw]++;
|
|
178
|
+
s.gameScores.push([s.score.p, s.score.ai]);
|
|
179
|
+
out.push({ type: 'gameWon', side: gw, game: s.gameNo });
|
|
180
|
+
const vars = { game: s.gameNo, p: s.games.p, ai: s.games.ai };
|
|
181
|
+
if (s.games[gw] >= s.need) {
|
|
182
|
+
s.phase = 'ending';
|
|
183
|
+
s.timer = GAME_PAUSE;
|
|
184
|
+
out.push({
|
|
185
|
+
type: 'banner',
|
|
186
|
+
key: gw === 'p' ? BANNER_KEYS.matchWon : BANNER_KEYS.matchLost,
|
|
187
|
+
sub: BANNER_KEYS.gamesScore,
|
|
188
|
+
vars,
|
|
189
|
+
side: gw,
|
|
190
|
+
duration: 0,
|
|
191
|
+
});
|
|
192
|
+
const w = gw === 'p' ? s.p : s.ai;
|
|
193
|
+
const l = gw === 'p' ? s.ai : s.p;
|
|
194
|
+
if (w)
|
|
195
|
+
w.mood = { type: 'celebrate', t: 9, hold: true };
|
|
196
|
+
if (l)
|
|
197
|
+
l.mood = { type: 'sad', t: 9, hold: true };
|
|
198
|
+
if (gw === 'p')
|
|
199
|
+
out.push({ type: 'excite', amount: 1 });
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
s.phase = 'between';
|
|
203
|
+
s.timer = GAME_PAUSE;
|
|
204
|
+
out.push({
|
|
205
|
+
type: 'banner',
|
|
206
|
+
key: gw === 'p' ? BANNER_KEYS.gameWon : BANNER_KEYS.gameLost,
|
|
207
|
+
sub: BANNER_KEYS.nextGame,
|
|
208
|
+
vars,
|
|
209
|
+
side: gw,
|
|
210
|
+
duration: 0,
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
/** Starts the next game after the between-games pause. */
|
|
214
|
+
export function nextGame(ctx) {
|
|
215
|
+
ctx.s.gameNo++;
|
|
216
|
+
ctx.s.score = { p: 0, ai: 0 };
|
|
217
|
+
setupServe(ctx);
|
|
218
|
+
}
|
|
219
|
+
export function buildResult(s, won, forfeit) {
|
|
220
|
+
const st = s.stats;
|
|
221
|
+
return {
|
|
222
|
+
won,
|
|
223
|
+
forfeit,
|
|
224
|
+
games: { p: s.games.p, ai: s.games.ai },
|
|
225
|
+
gameScores: s.gameScores.map(([a, b]) => [a, b]),
|
|
226
|
+
stats: {
|
|
227
|
+
smashes: st.smashes,
|
|
228
|
+
errors: st.errors,
|
|
229
|
+
shots: st.shots,
|
|
230
|
+
errorRate: st.shots ? st.errors / st.shots : 0,
|
|
231
|
+
longest: Math.max(st.longest, s.rally),
|
|
232
|
+
},
|
|
233
|
+
};
|
|
234
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { PopTone, SwingType, TextVars } from './events.ts';
|
|
2
|
+
import type { Actor, SimCtx } from './state.ts';
|
|
3
|
+
import type { ActionId } from './types.ts';
|
|
4
|
+
export interface Velocity {
|
|
5
|
+
vx: number;
|
|
6
|
+
vy: number;
|
|
7
|
+
}
|
|
8
|
+
/** A shuttle hit below this height (y greater) is a low rescue. */
|
|
9
|
+
export declare const RESCUE_Y = 790;
|
|
10
|
+
/** A smash needs the shuttle above NET_TOP - SMASH_MARGIN; lower contacts become drives. */
|
|
11
|
+
export declare const SMASH_MARGIN = 40;
|
|
12
|
+
export declare const KMH_PER_PX = 0.12;
|
|
13
|
+
export declare const smashSpeed: (power: number, pow: number) => number;
|
|
14
|
+
export declare const toKmh: (speed: number) => number;
|
|
15
|
+
/** Velocity that rises from (x0, y0) to apex height ay and lands at tx on the floor. */
|
|
16
|
+
export declare function ballistic(x0: number, y0: number, tx: number, ay: number): Velocity;
|
|
17
|
+
/** True when the path passes the net plane with 14px to spare, or never reaches it. */
|
|
18
|
+
export declare function clearsNet(x0: number, y0: number, v: Velocity): boolean;
|
|
19
|
+
export declare function lob(x0: number, y0: number, tx: number, apexY: number, careful: boolean): Velocity;
|
|
20
|
+
/** Straight, fast path toward NET_X + dir * d; careful shots push the target deeper until they clear the net. */
|
|
21
|
+
export declare function line(x0: number, y0: number, dir: number, d: number, speed: number, careful: boolean): Velocity;
|
|
22
|
+
/** Pushes a floating text, clamped to the court area like the mockup popText. */
|
|
23
|
+
export declare function pop(ctx: SimCtx, key: string, x: number, y: number, tone: PopTone, vars?: TextVars): void;
|
|
24
|
+
export declare function tryHit(ctx: SimCtx, a: Actor): void;
|
|
25
|
+
export interface StrikeOptions {
|
|
26
|
+
perfect?: boolean;
|
|
27
|
+
cross?: boolean;
|
|
28
|
+
q?: number;
|
|
29
|
+
nd?: number;
|
|
30
|
+
}
|
|
31
|
+
export declare function strike(ctx: SimCtx, a: Actor, type: SwingType, power: number, o: StrikeOptions): void;
|
|
32
|
+
/** Power of a shot struck in the air: the tap needs no charge, but a tired player still hits softer. */
|
|
33
|
+
export declare const airShotPower: (a: Actor) => number;
|
|
34
|
+
/**
|
|
35
|
+
* Releasing a held shot key: power from charge time, perfect window, cross latched during the charge or held now. The
|
|
36
|
+
* charged power stands in the air too: a short charge gives a steep smash that lands near the net, a real shot.
|
|
37
|
+
*/
|
|
38
|
+
export declare function releaseCharge(ctx: SimCtx, a: Actor, held: ReadonlySet<ActionId>): void;
|
|
39
|
+
/** Puts a serve swing on the actor: already past contact, so it never strikes again. */
|
|
40
|
+
export declare function serveSwing(a: Actor, type: 'clear' | 'drop', t: number, power: number): void;
|
|
41
|
+
/** Launches a served shuttle and switches the rally on. */
|
|
42
|
+
export declare function launchServe(ctx: SimCtx, a: Actor, type: 'clear' | 'drop', v: Velocity, cross?: boolean): void;
|
|
43
|
+
/**
|
|
44
|
+
* A served shuttle. The short serve's length follows the charge: a quick tap (power under about 0.3) falls before the
|
|
45
|
+
* short service line, a fault unless the receiver plays it.
|
|
46
|
+
*/
|
|
47
|
+
export declare function doServe(ctx: SimCtx, a: Actor, type: 'clear' | 'drop', power: number, cross?: boolean): void;
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
// Shot trajectories, contact and serves (mockup match.js:201-213, 278-391).
|
|
2
|
+
import { normDist, startSwing, swingWithAssist } from "./actor.js";
|
|
3
|
+
import { POP_KEYS } from "./events.js";
|
|
4
|
+
import { shouldTrigger, startSlowmo } from "./slowmo.js";
|
|
5
|
+
import { AIR_CONTACT_ASSIST, AIR_SHOT_POWER, AIR_SMASH_SPEED, CHARGE_FULL, CONTACT_ASSIST, clamp, COURT_L, COURT_R, CROSS_SHIFT, FLOOR, G, NET_TOP, NET_X, SHORT_SERVICE, SWEET, SWING_DUR, TIRED_AT, TIRED_POWER, } from "./world.js";
|
|
6
|
+
/** A shuttle hit below this height (y greater) is a low rescue. */
|
|
7
|
+
export const RESCUE_Y = 790;
|
|
8
|
+
/** A smash needs the shuttle above NET_TOP - SMASH_MARGIN; lower contacts become drives. */
|
|
9
|
+
export const SMASH_MARGIN = 40;
|
|
10
|
+
export const KMH_PER_PX = 0.12;
|
|
11
|
+
export const smashSpeed = (power, pow) => 1650 + power * 850 + pow * 55;
|
|
12
|
+
export const toKmh = (speed) => Math.round(speed * KMH_PER_PX);
|
|
13
|
+
/** Velocity that rises from (x0, y0) to apex height ay and lands at tx on the floor. */
|
|
14
|
+
export function ballistic(x0, y0, tx, ay) {
|
|
15
|
+
const vy = -Math.sqrt(2 * G * Math.max(4, y0 - ay));
|
|
16
|
+
const T = -vy / G + Math.sqrt((2 * Math.max(4, FLOOR - ay)) / G);
|
|
17
|
+
return { vx: (tx - x0) / T, vy };
|
|
18
|
+
}
|
|
19
|
+
/** True when the path passes the net plane with 14px to spare, or never reaches it. */
|
|
20
|
+
export function clearsNet(x0, y0, v) {
|
|
21
|
+
if ((NET_X - x0) * v.vx <= 0)
|
|
22
|
+
return true;
|
|
23
|
+
const t = (NET_X - x0) / v.vx;
|
|
24
|
+
return y0 + v.vy * t + 0.5 * G * t * t < NET_TOP - 14;
|
|
25
|
+
}
|
|
26
|
+
export function lob(x0, y0, tx, apexY, careful) {
|
|
27
|
+
let ay = Math.min(apexY, y0 - 12);
|
|
28
|
+
let v = ballistic(x0, y0, tx, ay);
|
|
29
|
+
if (careful) {
|
|
30
|
+
for (let i = 0; i < 8 && !clearsNet(x0, y0, v); i++) {
|
|
31
|
+
ay -= 28;
|
|
32
|
+
v = ballistic(x0, y0, tx, ay);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return v;
|
|
36
|
+
}
|
|
37
|
+
/** Straight, fast path toward NET_X + dir * d; careful shots push the target deeper until they clear the net. */
|
|
38
|
+
export function line(x0, y0, dir, d, speed, careful) {
|
|
39
|
+
let v = { vx: 0, vy: 0 };
|
|
40
|
+
for (let k = 0; k < 6; k++) {
|
|
41
|
+
const tx = NET_X + dir * (d + k * 70);
|
|
42
|
+
const dx = tx - x0;
|
|
43
|
+
const dy = FLOOR - y0;
|
|
44
|
+
const T = Math.hypot(dx, dy) / speed;
|
|
45
|
+
v = { vx: dx / T, vy: (dy - 0.5 * G * T * T) / T };
|
|
46
|
+
if (!careful || clearsNet(x0, y0, v))
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
return v;
|
|
50
|
+
}
|
|
51
|
+
/** Pushes a floating text, clamped to the court area like the mockup popText. */
|
|
52
|
+
export function pop(ctx, key, x, y, tone, vars = {}) {
|
|
53
|
+
ctx.out.push({ type: 'pop', key, vars, x: clamp(x, COURT_L, COURT_R), y: clamp(y, 180, 940), tone });
|
|
54
|
+
}
|
|
55
|
+
export function tryHit(ctx, a) {
|
|
56
|
+
const sh = ctx.s.shuttle;
|
|
57
|
+
const sw = a.swing;
|
|
58
|
+
if (!sw)
|
|
59
|
+
return;
|
|
60
|
+
if (!sh.live || sh.heldBy || sh.netted || sh.lastHitter === a.side)
|
|
61
|
+
return;
|
|
62
|
+
const onSide = a.side === 'p' ? sh.x < NET_X + 10 : sh.x > NET_X - 10;
|
|
63
|
+
if (!onSide)
|
|
64
|
+
return;
|
|
65
|
+
const nd = normDist(a, sh);
|
|
66
|
+
if (nd > 1)
|
|
67
|
+
return;
|
|
68
|
+
sw.done = true;
|
|
69
|
+
strike(ctx, a, sw.type, sw.power, { perfect: sw.perfect, cross: sw.cross, q: sw.q, nd });
|
|
70
|
+
}
|
|
71
|
+
export function strike(ctx, a, type, power, o) {
|
|
72
|
+
const { s, rng } = ctx;
|
|
73
|
+
const sh = s.shuttle;
|
|
74
|
+
const dir = a.facing;
|
|
75
|
+
const st = a.stats;
|
|
76
|
+
const q = o.q != null ? o.q : clamp(0.5 + st.tec * 0.04 + (o.perfect ? 0.18 : 0) - (o.nd ?? 0) * 0.3, 0.05, 1);
|
|
77
|
+
const err = 1 - q;
|
|
78
|
+
const careful = q > 0.38;
|
|
79
|
+
const scatter = (rng.next() * 2 - 1) * err * 200;
|
|
80
|
+
const lowSave = sh.y > RESCUE_Y;
|
|
81
|
+
// The jump-smash reward and the wider cross are a person's tools, on either side of a two-player match; the solo AI
|
|
82
|
+
// keeps the mockup's shots, so its strength across the career only moves through its brain (src/sim/ai.ts).
|
|
83
|
+
const player = ctx.humans.has(a.side);
|
|
84
|
+
const air = player && !a.onGround;
|
|
85
|
+
const returnsSmash = player && sh.kind === 'smash' && sh.lastHitter !== a.side;
|
|
86
|
+
const shift = (legacy) => (o.cross ? (player ? CROSS_SHIFT : legacy) : 0);
|
|
87
|
+
let kind = type;
|
|
88
|
+
if (type === 'smash' && sh.y >= NET_TOP - SMASH_MARGIN)
|
|
89
|
+
kind = 'drive';
|
|
90
|
+
let v;
|
|
91
|
+
let speed = 0;
|
|
92
|
+
if (kind === 'clear') {
|
|
93
|
+
const d = 560 + power * 170 + scatter - shift(60);
|
|
94
|
+
v = lob(sh.x, sh.y, NET_X + dir * d, 170 + (1 - power) * 110 + err * 60, careful);
|
|
95
|
+
}
|
|
96
|
+
else if (kind === 'drop') {
|
|
97
|
+
const d = 90 + power * 150 + scatter * 0.6 + shift(70);
|
|
98
|
+
v = lob(sh.x, sh.y, NET_X + dir * d, Math.min(sh.y - 30, NET_TOP - 60 - power * 30) + err * 50, careful);
|
|
99
|
+
}
|
|
100
|
+
else if (kind === 'drive') {
|
|
101
|
+
const d = 420 + power * 240 + scatter;
|
|
102
|
+
v = lob(sh.x, sh.y, NET_X + dir * d, Math.min(sh.y - 20, NET_TOP - 70) + err * 40, careful);
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
speed = smashSpeed(power, st.pow) * (air ? AIR_SMASH_SPEED : 1);
|
|
106
|
+
v = line(sh.x, sh.y, dir, 280 + power * 360 + scatter - shift(60) - (air ? 40 : 0), speed, careful);
|
|
107
|
+
}
|
|
108
|
+
sh.vx = v.vx;
|
|
109
|
+
sh.vy = v.vy;
|
|
110
|
+
sh.lastHitter = a.side;
|
|
111
|
+
sh.kind = kind;
|
|
112
|
+
sh.cross = !!o.cross;
|
|
113
|
+
sh.kmh = kind === 'smash' ? toKmh(speed) : 0;
|
|
114
|
+
s.rally++;
|
|
115
|
+
if (a.side === 'p') {
|
|
116
|
+
s.stats.shots++;
|
|
117
|
+
if (kind === 'smash')
|
|
118
|
+
s.stats.smashes++;
|
|
119
|
+
}
|
|
120
|
+
if (s.ai?.brain)
|
|
121
|
+
s.ai.brain.plan = null;
|
|
122
|
+
const rescue = lowSave && s.mode === 'match';
|
|
123
|
+
ctx.out.push({
|
|
124
|
+
type: 'hit',
|
|
125
|
+
side: a.side,
|
|
126
|
+
kind,
|
|
127
|
+
power: kind === 'smash' ? 1 : kind === 'drop' ? 0.2 : 0.3 + power * 0.5,
|
|
128
|
+
x: sh.x,
|
|
129
|
+
y: sh.y,
|
|
130
|
+
...(kind === 'smash' ? { kmh: sh.kmh } : {}),
|
|
131
|
+
...(o.perfect ? { perfect: true } : {}),
|
|
132
|
+
...(rescue ? { rescue: true } : {}),
|
|
133
|
+
});
|
|
134
|
+
if (kind === 'smash') {
|
|
135
|
+
pop(ctx, air ? POP_KEYS.jumpSmash : POP_KEYS.smash, sh.x, sh.y - 40, 'smash');
|
|
136
|
+
pop(ctx, POP_KEYS.kmh, sh.x, sh.y + 50, 'speed', { kmh: sh.kmh });
|
|
137
|
+
ctx.out.push({ type: 'excite', amount: 0.6 });
|
|
138
|
+
}
|
|
139
|
+
if (o.perfect && player)
|
|
140
|
+
pop(ctx, POP_KEYS.perfect, sh.x, sh.y - 90, 'perfect');
|
|
141
|
+
if (returnsSmash)
|
|
142
|
+
pop(ctx, POP_KEYS.smashSave, sh.x, sh.y - 140, 'save');
|
|
143
|
+
if (o.cross && player)
|
|
144
|
+
pop(ctx, POP_KEYS.cross, sh.x + 60, sh.y + 40, 'small');
|
|
145
|
+
if (rescue) {
|
|
146
|
+
pop(ctx, POP_KEYS.rescue, sh.x, sh.y - 120, 'save');
|
|
147
|
+
ctx.out.push({ type: 'excite', amount: 0.5 });
|
|
148
|
+
}
|
|
149
|
+
if (kind === 'smash' && shouldTrigger(s, true))
|
|
150
|
+
startSlowmo(ctx);
|
|
151
|
+
}
|
|
152
|
+
/** Power of a shot struck in the air: the tap needs no charge, but a tired player still hits softer. */
|
|
153
|
+
export const airShotPower = (a) => AIR_SHOT_POWER * (a.stamina < TIRED_AT ? TIRED_POWER : 1);
|
|
154
|
+
/**
|
|
155
|
+
* Releasing a held shot key: power from charge time, perfect window, cross latched during the charge or held now. The
|
|
156
|
+
* charged power stands in the air too: a short charge gives a steep smash that lands near the net, a real shot.
|
|
157
|
+
*/
|
|
158
|
+
export function releaseCharge(ctx, a, held) {
|
|
159
|
+
const c = a.charge;
|
|
160
|
+
if (!c)
|
|
161
|
+
return;
|
|
162
|
+
a.charge = null;
|
|
163
|
+
const { s } = ctx;
|
|
164
|
+
let power = clamp(c.t / CHARGE_FULL, 0.15, 1);
|
|
165
|
+
const perfect = power >= SWEET[0] && power <= SWEET[1];
|
|
166
|
+
if (a.stamina < TIRED_AT)
|
|
167
|
+
power *= TIRED_POWER;
|
|
168
|
+
const cross = c.cross || held.has('cross');
|
|
169
|
+
if (s.mode === 'match' && s.phase === 'serve' && s.server === a.side && s.shuttle.heldBy === a.side) {
|
|
170
|
+
doServe(ctx, a, c.type === 'drop' ? 'drop' : 'clear', power, cross);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
if (a.onGround)
|
|
174
|
+
swingWithAssist(ctx, a, c.type, power, { perfect, cross }, CONTACT_ASSIST);
|
|
175
|
+
else
|
|
176
|
+
swingWithAssist(ctx, a, c.type, power, { perfect, cross }, AIR_CONTACT_ASSIST);
|
|
177
|
+
}
|
|
178
|
+
/** Puts a serve swing on the actor: already past contact, so it never strikes again. */
|
|
179
|
+
export function serveSwing(a, type, t, power) {
|
|
180
|
+
a.swing = { type, t, power, perfect: false, cross: false, done: true };
|
|
181
|
+
a.swingP = t / SWING_DUR;
|
|
182
|
+
a.pose = type;
|
|
183
|
+
a.stateT = 0;
|
|
184
|
+
a.holding = false;
|
|
185
|
+
}
|
|
186
|
+
/** Launches a served shuttle and switches the rally on. */
|
|
187
|
+
export function launchServe(ctx, a, type, v, cross = false) {
|
|
188
|
+
const { s } = ctx;
|
|
189
|
+
const sh = s.shuttle;
|
|
190
|
+
sh.heldBy = null;
|
|
191
|
+
sh.live = true;
|
|
192
|
+
sh.netted = false;
|
|
193
|
+
sh.vx = v.vx;
|
|
194
|
+
sh.vy = v.vy;
|
|
195
|
+
sh.lastHitter = a.side;
|
|
196
|
+
sh.kind = 'clear';
|
|
197
|
+
sh.cross = cross;
|
|
198
|
+
s.rally = 1;
|
|
199
|
+
s.phase = 'rally';
|
|
200
|
+
if (a.side === 'p')
|
|
201
|
+
s.stats.shots++;
|
|
202
|
+
ctx.out.push({ type: 'hit', side: a.side, kind: type, power: type === 'drop' ? 0.2 : 0.5, x: sh.x, y: sh.y });
|
|
203
|
+
ctx.out.push({ type: 'bannerHide' });
|
|
204
|
+
if (cross && ctx.humans.has(a.side))
|
|
205
|
+
pop(ctx, POP_KEYS.cross, sh.x + 60, sh.y + 40, 'small');
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* A served shuttle. The short serve's length follows the charge: a quick tap (power under about 0.3) falls before the
|
|
209
|
+
* short service line, a fault unless the receiver plays it.
|
|
210
|
+
*/
|
|
211
|
+
export function doServe(ctx, a, type, power, cross = false) {
|
|
212
|
+
const sh = ctx.s.shuttle;
|
|
213
|
+
serveSwing(a, type, 0.12, power);
|
|
214
|
+
const dir = a.facing;
|
|
215
|
+
const shift = cross ? CROSS_SHIFT : 0;
|
|
216
|
+
const v = type === 'drop'
|
|
217
|
+
? lob(sh.x, sh.y, NET_X + dir * (SHORT_SERVICE - 40 + power * 130 + shift), NET_TOP - 90, true)
|
|
218
|
+
: lob(sh.x, sh.y, NET_X + dir * (560 + power * 140 - shift), 220, true);
|
|
219
|
+
launchServe(ctx, a, type, v, cross);
|
|
220
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { MatchState, SimCtx } from './state.ts';
|
|
2
|
+
export interface SlowMo {
|
|
3
|
+
/** Time factor for the next interval: SLOWMO_FACTOR while active, else 1. */
|
|
4
|
+
readonly factor: number;
|
|
5
|
+
readonly active: boolean;
|
|
6
|
+
/** Starts (or restarts) a slow-motion window of `duration` real seconds. */
|
|
7
|
+
trigger(): void;
|
|
8
|
+
/** Consumes realDt real seconds of the window and returns the factor that applies afterwards. */
|
|
9
|
+
tick(realDt: number): number;
|
|
10
|
+
}
|
|
11
|
+
export declare function createSlowMo(factor?: number, duration?: number): SlowMo;
|
|
12
|
+
/** Distance (in reach units) beyond which a falling shuttle counts as a desperate chase. */
|
|
13
|
+
export declare const FAR_CHASE = 1.8;
|
|
14
|
+
/** The falling shuttle must be lower than this (y greater) to trigger. */
|
|
15
|
+
export declare const FALL_Y = 720;
|
|
16
|
+
/**
|
|
17
|
+
* Slow motion only happens in a match, at a game point, at most once per rally:
|
|
18
|
+
* on a smash, or when the falling shuttle is past FALL_Y with the receiver beyond FAR_CHASE reach.
|
|
19
|
+
*/
|
|
20
|
+
export declare function shouldTrigger(s: MatchState, smashed?: boolean): boolean;
|
|
21
|
+
export declare function startSlowmo(ctx: SimCtx): void;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// Decisive-moment slow motion (mockup match.js:721-728, 825-829, 982-991).
|
|
2
|
+
import { normDist } from "./actor.js";
|
|
3
|
+
import { isGamePoint } from "./rules.js";
|
|
4
|
+
import { NET_X, SLOWMO_DURATION, SLOWMO_FACTOR } from "./world.js";
|
|
5
|
+
/** Absorbs float drift so that ten 0.1 s ticks end a 1.0 s span. */
|
|
6
|
+
const EPS = 1e-9;
|
|
7
|
+
export function createSlowMo(factor = SLOWMO_FACTOR, duration = SLOWMO_DURATION) {
|
|
8
|
+
let remaining = 0;
|
|
9
|
+
return {
|
|
10
|
+
get factor() {
|
|
11
|
+
return remaining > 0 ? factor : 1;
|
|
12
|
+
},
|
|
13
|
+
get active() {
|
|
14
|
+
return remaining > 0;
|
|
15
|
+
},
|
|
16
|
+
trigger() {
|
|
17
|
+
remaining = duration;
|
|
18
|
+
},
|
|
19
|
+
tick(realDt) {
|
|
20
|
+
if (!(realDt >= 0))
|
|
21
|
+
throw new RangeError(`SlowMo.tick: realDt must be >= 0, got ${realDt}`);
|
|
22
|
+
if (remaining > 0) {
|
|
23
|
+
remaining -= realDt;
|
|
24
|
+
if (remaining <= EPS)
|
|
25
|
+
remaining = 0;
|
|
26
|
+
}
|
|
27
|
+
return remaining > 0 ? factor : 1;
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/** Distance (in reach units) beyond which a falling shuttle counts as a desperate chase. */
|
|
32
|
+
export const FAR_CHASE = 1.8;
|
|
33
|
+
/** The falling shuttle must be lower than this (y greater) to trigger. */
|
|
34
|
+
export const FALL_Y = 720;
|
|
35
|
+
/**
|
|
36
|
+
* Slow motion only happens in a match, at a game point, at most once per rally:
|
|
37
|
+
* on a smash, or when the falling shuttle is past FALL_Y with the receiver beyond FAR_CHASE reach.
|
|
38
|
+
*/
|
|
39
|
+
export function shouldTrigger(s, smashed = false) {
|
|
40
|
+
if (s.mode !== 'match' || s.slowmoUsed || !isGamePoint(s.score))
|
|
41
|
+
return false;
|
|
42
|
+
if (smashed)
|
|
43
|
+
return true;
|
|
44
|
+
if (s.phase !== 'rally' || !s.ai)
|
|
45
|
+
return false;
|
|
46
|
+
const sh = s.shuttle;
|
|
47
|
+
const recv = sh.x < NET_X ? s.p : s.ai;
|
|
48
|
+
return sh.live && !sh.netted && sh.vy > 0 && sh.y > FALL_Y && normDist(recv, sh) > FAR_CHASE;
|
|
49
|
+
}
|
|
50
|
+
export function startSlowmo(ctx) {
|
|
51
|
+
const { s, slow } = ctx;
|
|
52
|
+
if (s.mode !== 'match' || s.slowmoUsed)
|
|
53
|
+
return;
|
|
54
|
+
s.slowmoUsed = true;
|
|
55
|
+
slow.trigger();
|
|
56
|
+
ctx.out.push({ type: 'slowmo', factor: SLOWMO_FACTOR, duration: SLOWMO_DURATION });
|
|
57
|
+
}
|