@maco-online/paint-tanks-rules 0.0.0 → 0.2.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/dist/ai-host.d.ts +15 -0
- package/dist/ai-host.js +1 -0
- package/dist/balance-config.d.ts +50 -0
- package/dist/balance-config.js +41 -0
- package/dist/boss-ai.d.ts +9 -0
- package/dist/boss-ai.js +213 -0
- package/dist/bosses.d.ts +34 -0
- package/dist/bosses.js +47 -0
- package/dist/constants.d.ts +133 -0
- package/dist/constants.js +163 -0
- package/dist/enemy-ai.d.ts +11 -0
- package/dist/enemy-ai.js +361 -0
- package/dist/game-factory.d.ts +16 -0
- package/dist/game-factory.js +78 -0
- package/dist/game-state.d.ts +256 -0
- package/dist/game-state.js +1 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +27 -0
- package/dist/labels.d.ts +12 -0
- package/dist/labels.js +129 -0
- package/dist/levels.d.ts +45 -0
- package/dist/levels.js +209 -0
- package/dist/movement.d.ts +25 -0
- package/dist/movement.js +158 -0
- package/dist/online/match.d.ts +28 -0
- package/dist/online/match.js +176 -0
- package/dist/online/prediction.d.ts +41 -0
- package/dist/online/prediction.js +97 -0
- package/dist/online/protocol.d.ts +124 -0
- package/dist/online/protocol.js +54 -0
- package/dist/online/room-game.d.ts +5 -0
- package/dist/online/room-game.js +24 -0
- package/dist/online/view.d.ts +43 -0
- package/dist/online/view.js +347 -0
- package/dist/pathfinding.d.ts +8 -0
- package/dist/pathfinding.js +45 -0
- package/dist/power-ups.d.ts +28 -0
- package/dist/power-ups.js +66 -0
- package/dist/random.d.ts +1 -0
- package/dist/random.js +1 -0
- package/dist/sim-effects.d.ts +30 -0
- package/dist/sim-effects.js +1 -0
- package/dist/simulation.d.ts +74 -0
- package/dist/simulation.js +768 -0
- package/dist/sound-names.d.ts +4 -0
- package/dist/sound-names.js +1 -0
- package/dist/spawning.d.ts +3 -0
- package/dist/spawning.js +56 -0
- package/dist/tank-rules.d.ts +20 -0
- package/dist/tank-rules.js +39 -0
- package/dist/visibility.d.ts +20 -0
- package/dist/visibility.js +39 -0
- package/package.json +40 -4
- package/README.md +0 -3
package/dist/levels.js
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { BALANCE, rampFactor } from "./balance-config.js";
|
|
2
|
+
import { DX, DY, MAXL, N, RAINBOW_FROM, SPAWNS, STARTS, TIERS } from "./constants.js";
|
|
3
|
+
/** A `N`×`N` grid filled with `value`. */
|
|
4
|
+
export function blankGrid(value) {
|
|
5
|
+
return Array.from({ length: N }, () => Array.from({ length: N }, () => value));
|
|
6
|
+
}
|
|
7
|
+
export const OL = (L) => Math.round(1 + ((L - 1) * 199) / (MAXL - 1));
|
|
8
|
+
export const tierOf = (L) => TIERS.findIndex(t => L <= t.to);
|
|
9
|
+
export function mulberry(a) {
|
|
10
|
+
return function () {
|
|
11
|
+
a |= 0;
|
|
12
|
+
a = (a + 0x6D2B79F5) | 0;
|
|
13
|
+
let t = Math.imul(a ^ (a >>> 15), 1 | a);
|
|
14
|
+
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
|
|
15
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export function pickW(R, arr) {
|
|
19
|
+
let s = 0;
|
|
20
|
+
for (const [, w] of arr) {
|
|
21
|
+
s += w;
|
|
22
|
+
}
|
|
23
|
+
let x = R() * s;
|
|
24
|
+
for (const [v, w] of arr) {
|
|
25
|
+
x -= w;
|
|
26
|
+
if (x < 0) {
|
|
27
|
+
return v;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return arr[0][0];
|
|
31
|
+
}
|
|
32
|
+
export function reach(m, from, to) {
|
|
33
|
+
const seen = new Set([`${from}`]);
|
|
34
|
+
const q = [from];
|
|
35
|
+
while (q.length) {
|
|
36
|
+
const [c, r] = q.shift();
|
|
37
|
+
if (c === to[0] && r === to[1]) {
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
for (let d = 0; d < 4; d++) {
|
|
41
|
+
const nc = c + DX[d];
|
|
42
|
+
const nr = r + DY[d];
|
|
43
|
+
const k = `${nc},${nr}`;
|
|
44
|
+
if (nc < 0 || nr < 0 || nc >= N || nr >= N || seen.has(k)) {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
const v = m[nr][nc];
|
|
48
|
+
if (v === 3 || v === 4) {
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
seen.add(k);
|
|
52
|
+
q.push([nc, nr]);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
export function genMap(L0) {
|
|
58
|
+
const L = OL(L0);
|
|
59
|
+
const tier = TIERS[tierOf(L0)];
|
|
60
|
+
const SH = [
|
|
61
|
+
[1, 3],
|
|
62
|
+
[3, 1],
|
|
63
|
+
[2, 2],
|
|
64
|
+
[1, 2],
|
|
65
|
+
[2, 1],
|
|
66
|
+
[1, 1],
|
|
67
|
+
[4, 1],
|
|
68
|
+
[1, 4]
|
|
69
|
+
];
|
|
70
|
+
const finish = (m) => {
|
|
71
|
+
for (const [c, r] of SPAWNS) {
|
|
72
|
+
m[r][c] = 0;
|
|
73
|
+
m[r + 1][c] = 0;
|
|
74
|
+
}
|
|
75
|
+
for (let c = 0; c < N; c++) {
|
|
76
|
+
m[11][c] = 0;
|
|
77
|
+
m[12][c] = 0;
|
|
78
|
+
}
|
|
79
|
+
const fm = L <= 100 ? 2 : 1;
|
|
80
|
+
for (const [c, r] of [
|
|
81
|
+
[5, 11],
|
|
82
|
+
[6, 11],
|
|
83
|
+
[7, 11],
|
|
84
|
+
[5, 12],
|
|
85
|
+
[7, 12]
|
|
86
|
+
]) {
|
|
87
|
+
m[r][c] = fm;
|
|
88
|
+
}
|
|
89
|
+
for (let c = 4; c <= 8; c++) {
|
|
90
|
+
m[10][c] = L <= 40 ? 2 : 1;
|
|
91
|
+
}
|
|
92
|
+
m[11][4] = m[11][8] = 1;
|
|
93
|
+
const lane = L > 180
|
|
94
|
+
? [
|
|
95
|
+
[4, 2],
|
|
96
|
+
[6, 7],
|
|
97
|
+
[8, 2]
|
|
98
|
+
]
|
|
99
|
+
: [
|
|
100
|
+
[2, 1],
|
|
101
|
+
[3, 1],
|
|
102
|
+
[4, 2],
|
|
103
|
+
[5, 2],
|
|
104
|
+
[6, 7],
|
|
105
|
+
[7, 2],
|
|
106
|
+
[8, 2],
|
|
107
|
+
[9, 1]
|
|
108
|
+
];
|
|
109
|
+
for (const [r, v] of lane) {
|
|
110
|
+
m[r][6] = v;
|
|
111
|
+
}
|
|
112
|
+
m[9][6] = 3;
|
|
113
|
+
if (L > 180) {
|
|
114
|
+
m[11][5] = 0;
|
|
115
|
+
m[11][7] = 0;
|
|
116
|
+
}
|
|
117
|
+
m[12][6] = 5;
|
|
118
|
+
return m;
|
|
119
|
+
};
|
|
120
|
+
for (let a = 0; a < 40; a++) {
|
|
121
|
+
const R = mulberry(L * 7919 + a * 104729 + 13);
|
|
122
|
+
const m = blankGrid(0);
|
|
123
|
+
const n = Math.max(4, Math.round(tier.sh[0] + R() * (tier.sh[1] - tier.sh[0])) - Math.floor(a / 4));
|
|
124
|
+
for (let i = 0; i < n; i++) {
|
|
125
|
+
const v = pickW(R, tier.mats);
|
|
126
|
+
const [w, h] = SH[Math.floor(R() * SH.length)];
|
|
127
|
+
const c = Math.floor(R() * (8 - w));
|
|
128
|
+
const r = 1 + Math.floor(R() * (11 - h));
|
|
129
|
+
for (let y = r; y < r + h; y++) {
|
|
130
|
+
for (let x = c; x < c + w; x++) {
|
|
131
|
+
m[y][x] = v;
|
|
132
|
+
m[y][12 - x] = v;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
finish(m);
|
|
137
|
+
if (SPAWNS.every(s => reach(m, s, [6, 11])) &&
|
|
138
|
+
STARTS.every(s => reach(m, s, [6, 11])) &&
|
|
139
|
+
reach(m, [0, 0], [12, 0])) {
|
|
140
|
+
return m;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return finish(blankGrid(0));
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* The level as a team of `size` plays it. Levels are balanced for one or two players, so a bigger team faces more
|
|
147
|
+
* tanks at once and a tougher boss; one or two players get the level unchanged.
|
|
148
|
+
*/
|
|
149
|
+
export function teamDifficulty(D, size) {
|
|
150
|
+
const extra = Math.max(0, size - 2);
|
|
151
|
+
if (!extra) {
|
|
152
|
+
return D;
|
|
153
|
+
}
|
|
154
|
+
return {
|
|
155
|
+
...D,
|
|
156
|
+
count: Math.round(D.count * (1 + 0.3 * extra)),
|
|
157
|
+
max: Math.min(11, D.max + extra),
|
|
158
|
+
bossHp: Math.round(D.bossHp * (1 + 0.35 * extra))
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
export function diffOf(L0, k = 1) {
|
|
162
|
+
const L = OL(L0);
|
|
163
|
+
const p = (L0 - 1) / (MAXL - 1);
|
|
164
|
+
let e = 0.72 * p ** 1.3 + 0.28 * p ** 7;
|
|
165
|
+
e += (((L0 - 1) % 10) / 9) * 0.05 - 0.025;
|
|
166
|
+
if (L0 % 5 === 0) {
|
|
167
|
+
e += 0.03;
|
|
168
|
+
}
|
|
169
|
+
e = Math.max(0, Math.min(1.2, e * k * 0.8));
|
|
170
|
+
const E = Math.min(1, e);
|
|
171
|
+
const boss = L0 % 5 === 0;
|
|
172
|
+
const mini = boss && L0 % 10 !== 0;
|
|
173
|
+
const ramp = (r) => rampFactor(r, L0);
|
|
174
|
+
// Rainbow tanks take their share from dust and racers, so a level does not get more enemies.
|
|
175
|
+
const rainbow = L0 >= RAINBOW_FROM ? 0.1 + 0.15 * E : 0;
|
|
176
|
+
return {
|
|
177
|
+
e,
|
|
178
|
+
boss,
|
|
179
|
+
count: Math.max(6, Math.round((10 + 30 * e) * (boss ? 0.7 : 1))),
|
|
180
|
+
max: Math.min(9, 3 + Math.round(5 * e)),
|
|
181
|
+
gap: Math.round(120 - 80 * E),
|
|
182
|
+
spd: (1 + 0.8 * e) * ramp(BALANCE.enemy.move),
|
|
183
|
+
fire: [Math.max(22, 110 - 80 * e), Math.max(40, 190 - 130 * e)],
|
|
184
|
+
eb: (3 + 2.2 * e) * ramp(BALANCE.enemy.bullet),
|
|
185
|
+
bossEb: (3 + 2.2 * e) * ramp(BALANCE.boss.bullet),
|
|
186
|
+
mix: [
|
|
187
|
+
['dust', Math.max(0.05, Math.max(0.1, 1 - 0.85 * e) - rainbow / 2)],
|
|
188
|
+
['racer', 0.2 + 0.45 * e - rainbow / 2],
|
|
189
|
+
['box', L >= 8 ? 0.1 + 0.4 * e : 0],
|
|
190
|
+
['sniper', L >= 30 ? 0.55 * e : 0],
|
|
191
|
+
['bomb', L >= 6 ? 0.08 + 0.22 * e : 0],
|
|
192
|
+
['rainbow', rainbow]
|
|
193
|
+
],
|
|
194
|
+
smart: Math.min(0.95, 0.45 + 0.5 * e),
|
|
195
|
+
aim: Math.min(1, 0.15 + 0.85 * e),
|
|
196
|
+
lives: L <= 40 ? 3 : L <= 160 ? 2 : 1,
|
|
197
|
+
pet: L <= 30 ? 3 : L <= 120 ? 2 : 1,
|
|
198
|
+
shield: Math.round(150 - 90 * E),
|
|
199
|
+
drop: Math.max(0.15, 0.45 - 0.3 * e),
|
|
200
|
+
rush: L >= 15,
|
|
201
|
+
bossHp: Math.max(1, Math.round((L0 === MAXL ? 140 : (12 + 70 * e) * (mini ? 0.6 : 1)) * ramp(BALANCE.boss.health))),
|
|
202
|
+
bossShield: L >= 50,
|
|
203
|
+
bossLast: L >= 30,
|
|
204
|
+
bossEarly: L >= 100 ? 2 : 0,
|
|
205
|
+
bossSpd: (0.6 + 0.5 * e) * ramp(BALANCE.boss.move),
|
|
206
|
+
dodge: L >= 20 ? 0.06 * e : 0,
|
|
207
|
+
edodge: L >= 10 ? 0.05 + 0.6 * E : 0
|
|
208
|
+
};
|
|
209
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Box, Game, Player, Tank } from './game-state.ts';
|
|
2
|
+
/** Leeway on a claimed step, in pixels, for rounding; `move()` never shifts a tank more than its speed per tick. */
|
|
3
|
+
export declare const CLAIM_SLACK = 2;
|
|
4
|
+
/** Most ticks of movement one claim may cover, after a network stall delivers several inputs at once. */
|
|
5
|
+
export declare const CLAIM_MAX_TICKS = 30;
|
|
6
|
+
export declare function overlaps(a: Box, x: number, y: number, b: Box): boolean;
|
|
7
|
+
export declare function tanksOf(g: Game): Tank[];
|
|
8
|
+
/** Whether a tank of size `e.s` at (`x`, `y`) would leave the field or overlap a solid tile; `swim` crosses water. */
|
|
9
|
+
export declare function isBlockedByTiles(g: Game, e: Box, x: number, y: number, swim?: boolean): boolean;
|
|
10
|
+
export declare function isBlocked(g: Game, e: Tank, x: number, y: number): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Whether player `p` may move to (`x`, `y`) when the browser decides its own position. `budget` is how far the tank
|
|
13
|
+
* could have driven since the server last moved it. The tank must get there through open floor, by a route that only
|
|
14
|
+
* heads toward the claimed spot, and must not end inside a wall or another tank.
|
|
15
|
+
*/
|
|
16
|
+
export declare function claimStepAllowed(g: Game, p: Player, x: number, y: number, budget: number): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* The largest part, 0 to 1, of the way from `p` toward a refused claim at (`x`, `y`) that is itself allowed, found
|
|
19
|
+
* by halving. Only a claim within the movement budget has a part: one that runs a little into a tank or a wall the
|
|
20
|
+
* browser had not seen yet. A claim beyond the budget gets nothing, so a teleport moves the tank no closer.
|
|
21
|
+
*/
|
|
22
|
+
export declare function furthestAllowedPart(g: Game, p: Player, x: number, y: number, budget: number): number;
|
|
23
|
+
export declare function isAligned(e: Box, sp: number): boolean;
|
|
24
|
+
export declare function turn(g: Game, e: Tank, d: number, snap?: boolean): void;
|
|
25
|
+
export declare function move(g: Game, e: Tank, sp: number): boolean;
|
package/dist/movement.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { DX, DY, T, W } from "./constants.js";
|
|
2
|
+
/** Leeway on a claimed step, in pixels, for rounding; `move()` never shifts a tank more than its speed per tick. */
|
|
3
|
+
export const CLAIM_SLACK = 2;
|
|
4
|
+
/** Most ticks of movement one claim may cover, after a network stall delivers several inputs at once. */
|
|
5
|
+
export const CLAIM_MAX_TICKS = 30;
|
|
6
|
+
export function overlaps(a, x, y, b) {
|
|
7
|
+
return x < b.x + b.s && x + a.s > b.x && y < b.y + b.s && y + a.s > b.y;
|
|
8
|
+
}
|
|
9
|
+
export function tanksOf(g) {
|
|
10
|
+
return [...g.players.filter(p => p.alive && !p.respawn), ...g.enemies, ...(g.boss ? [g.boss] : [])];
|
|
11
|
+
}
|
|
12
|
+
/** Whether a tank of size `e.s` at (`x`, `y`) would leave the field or overlap a solid tile; `swim` crosses water. */
|
|
13
|
+
export function isBlockedByTiles(g, e, x, y, swim = false) {
|
|
14
|
+
if (x < 0 || y < 0 || x + e.s > W || y + e.s > W) {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
for (let r = Math.floor(y / T); r <= Math.floor((y + e.s - 0.01) / T); r++) {
|
|
18
|
+
for (let c = Math.floor(x / T); c <= Math.floor((x + e.s - 0.01) / T); c++) {
|
|
19
|
+
const v = g.map[r][c];
|
|
20
|
+
if (v && v !== 6 && !(swim && v === 4)) {
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
export function isBlocked(g, e, x, y) {
|
|
28
|
+
if (isBlockedByTiles(g, e, x, y, !!e.player && !!e.spec.swim)) {
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
for (const o of tanksOf(g)) {
|
|
32
|
+
if (o === e || !overlaps(e, x, y, o)) {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
// A tank already stuck on another may move off it; two players may only move apart, never through each other.
|
|
36
|
+
if (!overlaps(e, e.x, e.y, o) || (e.player && o.player && centreGap(x, y, o) < centreGap(e.x, e.y, o))) {
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
function centreGap(x, y, o) {
|
|
43
|
+
return Math.hypot(x - o.x, y - o.y);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Whether player `p` may move to (`x`, `y`) when the browser decides its own position. `budget` is how far the tank
|
|
47
|
+
* could have driven since the server last moved it. The tank must get there through open floor, by a route that only
|
|
48
|
+
* heads toward the claimed spot, and must not end inside a wall or another tank.
|
|
49
|
+
*/
|
|
50
|
+
export function claimStepAllowed(g, p, x, y, budget) {
|
|
51
|
+
if (!Number.isFinite(x) || !Number.isFinite(y)) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
if (Math.abs(x - p.x) + Math.abs(y - p.y) > budget) {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
return monotoneRouteClear(g, p, x, y);
|
|
58
|
+
}
|
|
59
|
+
/** Halvings when looking for the allowed part of a refused claim: about a pixel on the longest claim. */
|
|
60
|
+
const PARTIAL_CLAIM_STEPS = 6;
|
|
61
|
+
/**
|
|
62
|
+
* The largest part, 0 to 1, of the way from `p` toward a refused claim at (`x`, `y`) that is itself allowed, found
|
|
63
|
+
* by halving. Only a claim within the movement budget has a part: one that runs a little into a tank or a wall the
|
|
64
|
+
* browser had not seen yet. A claim beyond the budget gets nothing, so a teleport moves the tank no closer.
|
|
65
|
+
*/
|
|
66
|
+
export function furthestAllowedPart(g, p, x, y, budget) {
|
|
67
|
+
if (!Number.isFinite(x) || !Number.isFinite(y) || Math.abs(x - p.x) + Math.abs(y - p.y) > budget) {
|
|
68
|
+
return 0;
|
|
69
|
+
}
|
|
70
|
+
let low = 0;
|
|
71
|
+
let high = 1;
|
|
72
|
+
for (let i = 0; i < PARTIAL_CLAIM_STEPS; i++) {
|
|
73
|
+
const mid = (low + high) / 2;
|
|
74
|
+
if (claimStepAllowed(g, p, p.x + (x - p.x) * mid, p.y + (y - p.y) * mid, budget)) {
|
|
75
|
+
low = mid;
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
high = mid;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return low;
|
|
82
|
+
}
|
|
83
|
+
/** Spacing of points checked along a claimed path, well under a wall's thickness. */
|
|
84
|
+
const PATH_STEP = 2;
|
|
85
|
+
/**
|
|
86
|
+
* Whether `p` can reach (`x`, `y`) through open floor on a lattice of points `PATH_STEP` apart, stepping only toward
|
|
87
|
+
* it along one axis at a time. Any route with that shape is exactly as long as the movement budget measures.
|
|
88
|
+
*/
|
|
89
|
+
function monotoneRouteClear(g, p, x, y) {
|
|
90
|
+
const nx = Math.ceil(Math.abs(x - p.x) / PATH_STEP);
|
|
91
|
+
const ny = Math.ceil(Math.abs(y - p.y) / PATH_STEP);
|
|
92
|
+
const at = (i, j) => [nx ? p.x + ((x - p.x) * i) / nx : p.x, ny ? p.y + ((y - p.y) * j) / ny : p.y];
|
|
93
|
+
const reach = [];
|
|
94
|
+
for (let j = 0; j <= ny; j++) {
|
|
95
|
+
for (let i = 0; i <= nx; i++) {
|
|
96
|
+
const from = (i === 0 && j === 0) || (i > 0 && reach[i - 1]) || (j > 0 && reach[i]);
|
|
97
|
+
reach[i] = !!from && (i + j === 0 || !isBlocked(g, p, ...at(i, j)));
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return !!reach[nx];
|
|
101
|
+
}
|
|
102
|
+
export function isAligned(e, sp) {
|
|
103
|
+
const ox = (e.x - 3) / T;
|
|
104
|
+
const oy = (e.y - 3) / T;
|
|
105
|
+
return Math.abs(ox - Math.round(ox)) * T <= sp + 0.01 && Math.abs(oy - Math.round(oy)) * T <= sp + 0.01;
|
|
106
|
+
}
|
|
107
|
+
export function turn(g, e, d, snap = true) {
|
|
108
|
+
if (d === e.dir) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (snap && d % 2 !== e.dir % 2) {
|
|
112
|
+
const off = (T - e.s) / 2;
|
|
113
|
+
const k = d % 2 === 0 ? 'x' : 'y';
|
|
114
|
+
const old = e[k];
|
|
115
|
+
const gr = e.player || e.boss ? 16 : T;
|
|
116
|
+
e[k] = Math.round((old - off) / gr) * gr + off;
|
|
117
|
+
if (isBlocked(g, e, e.x, e.y)) {
|
|
118
|
+
e[k] = Math.round((old - off) / 16) * 16 + off;
|
|
119
|
+
}
|
|
120
|
+
if (isBlocked(g, e, e.x, e.y)) {
|
|
121
|
+
e[k] = old;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
e.dir = d;
|
|
125
|
+
}
|
|
126
|
+
export function move(g, e, sp) {
|
|
127
|
+
const nx = e.x + DX[e.dir] * sp;
|
|
128
|
+
const ny = e.y + DY[e.dir] * sp;
|
|
129
|
+
if (!isBlocked(g, e, nx, ny)) {
|
|
130
|
+
e.x = nx;
|
|
131
|
+
e.y = ny;
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
const k = e.dir % 2 === 0 ? 'x' : 'y';
|
|
135
|
+
const off = (T - e.s) / 2;
|
|
136
|
+
const cur = e[k];
|
|
137
|
+
const lim = e.player ? 14 : 9;
|
|
138
|
+
for (const gr of [32, 16]) {
|
|
139
|
+
const tgt = Math.round((cur - off) / gr) * gr + off;
|
|
140
|
+
const d = tgt - cur;
|
|
141
|
+
if (Math.abs(d) < 0.01 || Math.abs(d) > lim) {
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
const tx = k === 'x' ? tgt : e.x;
|
|
145
|
+
const ty = k === 'y' ? tgt : e.y;
|
|
146
|
+
if (isBlocked(g, e, tx + DX[e.dir] * sp, ty + DY[e.dir] * sp)) {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
const st = Math.sign(d) * Math.min(Math.abs(d), sp);
|
|
150
|
+
const px = k === 'x' ? cur + st : e.x;
|
|
151
|
+
const py = k === 'y' ? cur + st : e.y;
|
|
152
|
+
if (!isBlocked(g, e, px, py)) {
|
|
153
|
+
e[k] = k === 'x' ? px : py;
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { RoomMatch, RoomMatchMember } from '@maco-online/online-core';
|
|
2
|
+
import type { Game } from '../game-state.ts';
|
|
3
|
+
import type { PaintTanksInput, PaintTanksProfile, PaintTanksSettings, PaintTanksState } from './protocol.ts';
|
|
4
|
+
import { PaintTanksSimulation } from '../simulation.ts';
|
|
5
|
+
/**
|
|
6
|
+
* One level played online by the members who started it, run by the room on the server. Each member drives the
|
|
7
|
+
* tank of their slot; the rules are the same simulation as local play, scaled for the size of the team.
|
|
8
|
+
*/
|
|
9
|
+
export declare class PaintTanksMatch implements RoomMatch<PaintTanksInput, PaintTanksState> {
|
|
10
|
+
readonly sim: PaintTanksSimulation;
|
|
11
|
+
private readonly events;
|
|
12
|
+
private readonly ids;
|
|
13
|
+
private nextId;
|
|
14
|
+
/** Room slot of each player, in player order. */
|
|
15
|
+
private readonly slots;
|
|
16
|
+
private sentTiles;
|
|
17
|
+
private statesSent;
|
|
18
|
+
constructor(settings: PaintTanksSettings, members: readonly RoomMatchMember<PaintTanksProfile>[], previous: PaintTanksMatch | null);
|
|
19
|
+
get game(): Game;
|
|
20
|
+
get over(): boolean;
|
|
21
|
+
/** Power level the player in `slot` carries into the next level; 1 for anyone who did not play this one. */
|
|
22
|
+
carriedUp(slot: number): number;
|
|
23
|
+
step(inputs: ReadonlyMap<number, PaintTanksInput>): void;
|
|
24
|
+
leave(slot: number): void;
|
|
25
|
+
state(): PaintTanksState;
|
|
26
|
+
/** A number that names the same enemy or bullet in every state, so browsers can follow it between them. */
|
|
27
|
+
private idOf;
|
|
28
|
+
}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { createGame } from "../game-factory.js";
|
|
2
|
+
import { PaintTanksSimulation } from "../simulation.js";
|
|
3
|
+
import { ENEMY_KINDS, PARTICLE_TYPES, PICKUP_TYPES, TANK_KINDS } from "./protocol.js";
|
|
4
|
+
/** Effects between two states are capped, so a burst of explosions cannot make one message huge. */
|
|
5
|
+
const MAX_EVENTS_PER_STATE = 300;
|
|
6
|
+
/** The map is sent at least this often, in states, for members who joined since it last changed. */
|
|
7
|
+
const MAP_KEYFRAME_EVERY = 20;
|
|
8
|
+
/** The command a member has not yet sent anything replaces: hold still at whatever place it has. */
|
|
9
|
+
const IDLE = { d: -1, f: false, x: 0, y: 0, n: 0 };
|
|
10
|
+
/** Rounds a coordinate to a tenth of a pixel, which is all a browser needs to draw it. */
|
|
11
|
+
const r1 = (v) => Math.round(v * 10) / 10;
|
|
12
|
+
const r2 = (v) => Math.round(v * 100) / 100;
|
|
13
|
+
/** Writes down what the simulation wants seen and heard, to send it with the next state. */
|
|
14
|
+
class RecordedEffects {
|
|
15
|
+
events;
|
|
16
|
+
constructor(events) {
|
|
17
|
+
this.events = events;
|
|
18
|
+
}
|
|
19
|
+
push(event) {
|
|
20
|
+
if (this.events.length < MAX_EVENTS_PER_STATE) {
|
|
21
|
+
this.events.push(event);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
sound(name) {
|
|
25
|
+
if (name) {
|
|
26
|
+
this.push(['s', name]);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
burst(x, y, color, count, type = 'dot') {
|
|
30
|
+
this.push(['b', r1(x), r1(y), color, count, PARTICLE_TYPES.indexOf(type)]);
|
|
31
|
+
}
|
|
32
|
+
splash(kind, x, y, color, scale) {
|
|
33
|
+
this.push(['f', TANK_KINDS.indexOf(kind), r1(x), r1(y), color, scale]);
|
|
34
|
+
}
|
|
35
|
+
float(x, y, label, color) {
|
|
36
|
+
this.push(['t', r1(x), r1(y), color, label]);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* One level played online by the members who started it, run by the room on the server. Each member drives the
|
|
41
|
+
* tank of their slot; the rules are the same simulation as local play, scaled for the size of the team.
|
|
42
|
+
*/
|
|
43
|
+
export class PaintTanksMatch {
|
|
44
|
+
sim;
|
|
45
|
+
events = [];
|
|
46
|
+
ids = new WeakMap();
|
|
47
|
+
nextId = 1;
|
|
48
|
+
/** Room slot of each player, in player order. */
|
|
49
|
+
slots;
|
|
50
|
+
sentTiles = '';
|
|
51
|
+
statesSent = 0;
|
|
52
|
+
constructor(settings, members, previous) {
|
|
53
|
+
const ordered = [...members].sort((a, b) => a.slot - b.slot);
|
|
54
|
+
this.slots = ordered.map(m => m.slot);
|
|
55
|
+
const won = previous?.sim.outcome?.win ? previous : null;
|
|
56
|
+
const game = createGame({
|
|
57
|
+
lv: settings.level,
|
|
58
|
+
difficultyScale: 1,
|
|
59
|
+
tanks: ordered.map(m => m.profile.tank),
|
|
60
|
+
carry: won ? { lv: won.sim.game.lv, up: ordered.map(m => won.carriedUp(m.slot)) } : null
|
|
61
|
+
});
|
|
62
|
+
this.sim = new PaintTanksSimulation(game, new RecordedEffects(this.events));
|
|
63
|
+
}
|
|
64
|
+
get game() {
|
|
65
|
+
return this.sim.game;
|
|
66
|
+
}
|
|
67
|
+
get over() {
|
|
68
|
+
return this.sim.outcome !== null;
|
|
69
|
+
}
|
|
70
|
+
/** Power level the player in `slot` carries into the next level; 1 for anyone who did not play this one. */
|
|
71
|
+
carriedUp(slot) {
|
|
72
|
+
const i = this.slots.indexOf(slot);
|
|
73
|
+
return (i >= 0 && this.sim.outcome?.carryUp[i]) || 1;
|
|
74
|
+
}
|
|
75
|
+
step(inputs) {
|
|
76
|
+
this.sim.step(this.slots.map((slot) => {
|
|
77
|
+
const input = inputs.get(slot) ?? IDLE;
|
|
78
|
+
return { dir: input.d, fire: input.f, x: input.x, y: input.y, n: input.n };
|
|
79
|
+
}));
|
|
80
|
+
}
|
|
81
|
+
leave(slot) {
|
|
82
|
+
const player = this.game.players[this.slots.indexOf(slot)];
|
|
83
|
+
if (player) {
|
|
84
|
+
this.sim.retire(player);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
state() {
|
|
88
|
+
const g = this.game;
|
|
89
|
+
const tiles = g.map.map(row => row.join('')).join('');
|
|
90
|
+
const hp = g.hp.map(row => row.map(v => Math.max(0, Math.min(9, v))).join('')).join('');
|
|
91
|
+
const sendTiles = tiles + hp !== this.sentTiles || this.statesSent % MAP_KEYFRAME_EVERY === 0;
|
|
92
|
+
this.sentTiles = tiles + hp;
|
|
93
|
+
this.statesSent++;
|
|
94
|
+
const boss = g.boss;
|
|
95
|
+
const state = {
|
|
96
|
+
lv: g.lv,
|
|
97
|
+
t: g.t,
|
|
98
|
+
over: g.state === 'over',
|
|
99
|
+
...(sendTiles ? { map: tiles, hp } : {}),
|
|
100
|
+
players: g.players.map(p => [
|
|
101
|
+
this.slots[p.idx],
|
|
102
|
+
TANK_KINDS.indexOf(p.kind),
|
|
103
|
+
r1(p.x),
|
|
104
|
+
r1(p.y),
|
|
105
|
+
p.dir,
|
|
106
|
+
r1(p.anim),
|
|
107
|
+
p.alive ? 1 : 0,
|
|
108
|
+
p.respawn,
|
|
109
|
+
p.shield,
|
|
110
|
+
p.cloak,
|
|
111
|
+
p.up,
|
|
112
|
+
p.lives
|
|
113
|
+
]),
|
|
114
|
+
enemies: g.enemies.map(e => [
|
|
115
|
+
this.idOf(e),
|
|
116
|
+
ENEMY_KINDS.indexOf(e.kind),
|
|
117
|
+
r1(e.x),
|
|
118
|
+
r1(e.y),
|
|
119
|
+
e.dir,
|
|
120
|
+
r1(e.anim),
|
|
121
|
+
e.spawning,
|
|
122
|
+
e.flash,
|
|
123
|
+
(e.angry ? 1 : 0) | (e.carrier ? 2 : 0) | (e.fuse ? 4 : 0) | (e.slow ? 8 : 0)
|
|
124
|
+
]),
|
|
125
|
+
boss: boss
|
|
126
|
+
? [
|
|
127
|
+
this.idOf(boss),
|
|
128
|
+
boss.id,
|
|
129
|
+
r1(boss.x),
|
|
130
|
+
r1(boss.y),
|
|
131
|
+
boss.dir,
|
|
132
|
+
r1(boss.anim),
|
|
133
|
+
boss.hp,
|
|
134
|
+
boss.maxHp,
|
|
135
|
+
boss.flash,
|
|
136
|
+
boss.inv,
|
|
137
|
+
boss.shielded ? 1 : 0,
|
|
138
|
+
boss.charge,
|
|
139
|
+
boss.charge > 0 ? boss.plan : []
|
|
140
|
+
]
|
|
141
|
+
: null,
|
|
142
|
+
bullets: g.bullets.map(b => [
|
|
143
|
+
this.idOf(b),
|
|
144
|
+
r1(b.x),
|
|
145
|
+
r1(b.y),
|
|
146
|
+
r2(b.vx),
|
|
147
|
+
r2(b.vy),
|
|
148
|
+
b.r,
|
|
149
|
+
b.owner,
|
|
150
|
+
b.kind ? TANK_KINDS.indexOf(b.kind) : -1,
|
|
151
|
+
(b.big ? 1 : 0) | (b.power ? 2 : 0) | (b.rainbow ? 4 : 0)
|
|
152
|
+
]),
|
|
153
|
+
pickups: g.picks.map(k => [r1(k.x), r1(k.y), PICKUP_TYPES.indexOf(k.type), k.life]),
|
|
154
|
+
petHp: g.petHp,
|
|
155
|
+
petFlash: g.petFlash,
|
|
156
|
+
freeze: g.freeze,
|
|
157
|
+
shake: g.shake,
|
|
158
|
+
score: Math.round(g.score),
|
|
159
|
+
remaining: this.sim.remaining(),
|
|
160
|
+
bossT: g.bossT,
|
|
161
|
+
banner: g.banner,
|
|
162
|
+
events: this.events.splice(0),
|
|
163
|
+
outcome: this.sim.outcome
|
|
164
|
+
};
|
|
165
|
+
return state;
|
|
166
|
+
}
|
|
167
|
+
/** A number that names the same enemy or bullet in every state, so browsers can follow it between them. */
|
|
168
|
+
idOf(thing) {
|
|
169
|
+
let id = this.ids.get(thing);
|
|
170
|
+
if (id === undefined) {
|
|
171
|
+
id = this.nextId++;
|
|
172
|
+
this.ids.set(thing, id);
|
|
173
|
+
}
|
|
174
|
+
return id;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Game, Player, PlayerInput } from '../game-state.ts';
|
|
2
|
+
export interface OwnTankClaim {
|
|
3
|
+
seq: number;
|
|
4
|
+
x: number;
|
|
5
|
+
y: number;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Drives the member's own tank the moment a key is pressed and reports where it ended up; the server adopts any
|
|
9
|
+
* position that obeys the rules. Each report is numbered, and every state from the server says which number it
|
|
10
|
+
* had applied, so the prediction is compared with the server at the same input, never with where the tank is now.
|
|
11
|
+
* A real difference (the server refused a move) shifts the tank and every later prediction once; states that were
|
|
12
|
+
* already on their way are then ignored until one reflects the corrected path, so a correction is never applied
|
|
13
|
+
* twice.
|
|
14
|
+
*/
|
|
15
|
+
export declare class OwnTankPredictor {
|
|
16
|
+
/** Corrections made since the level started, and the size of the last one, for the network panel. */
|
|
17
|
+
corrections: number;
|
|
18
|
+
lastCorrection: number;
|
|
19
|
+
private seq;
|
|
20
|
+
private history;
|
|
21
|
+
/** States acknowledging inputs up to this number predate the last correction. */
|
|
22
|
+
private settledAfter;
|
|
23
|
+
/** Starts numbering again, for a new connection: the room forgets the old numbers when it welcomes it. */
|
|
24
|
+
reset(): void;
|
|
25
|
+
/** Forgets the previous level's predictions; the numbering goes on, as the room still remembers it. */
|
|
26
|
+
newLevel(): void;
|
|
27
|
+
/** Moves `me` by one tick of `input`, exactly as the server would, and returns the claim to send. */
|
|
28
|
+
advance(g: Game, me: Player, input: PlayerInput): OwnTankClaim;
|
|
29
|
+
/**
|
|
30
|
+
* Takes in where the server had the tank after input `ack`; `active` is false while it is dead or respawning.
|
|
31
|
+
* Returns how the tank was moved: by a small `shift` the caller may fade in on screen, or a `jump` to show as is.
|
|
32
|
+
*/
|
|
33
|
+
observe(me: Player, x: number, y: number, ack: number, active: boolean): OwnTankCorrection | null;
|
|
34
|
+
}
|
|
35
|
+
export type OwnTankCorrection = {
|
|
36
|
+
kind: 'shift';
|
|
37
|
+
x: number;
|
|
38
|
+
y: number;
|
|
39
|
+
} | {
|
|
40
|
+
kind: 'jump';
|
|
41
|
+
};
|