@maco-online/paint-tanks-rules 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/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 +20 -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 +43 -3
- package/README.md +0 -3
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
import { BOSSES } from "../bosses.js";
|
|
2
|
+
import { COL, ENEMY, N, PLAYER_COLORS, T, tankSpec, TEAM_STARTS } from "../constants.js";
|
|
3
|
+
import { createGame } from "../game-factory.js";
|
|
4
|
+
import { ENEMY_KINDS, PICKUP_TYPES, TANK_KINDS } from "./protocol.js";
|
|
5
|
+
/** Further than this between two states is a jump, such as a respawn, and is not blended. */
|
|
6
|
+
const MAX_BLEND_DISTANCE = 40;
|
|
7
|
+
const lerp = (a, b, t) => a + (b - a) * t;
|
|
8
|
+
/**
|
|
9
|
+
* The field of an online level as this browser draws it, rebuilt from the states the room sends. Every tank and
|
|
10
|
+
* bullet is blended between the two states around the moment drawn; the member's own tank is left to the
|
|
11
|
+
* prediction, which runs ahead of the room so it answers the controls at once.
|
|
12
|
+
*/
|
|
13
|
+
export class OnlineView {
|
|
14
|
+
game;
|
|
15
|
+
remaining = 0;
|
|
16
|
+
players = new Map();
|
|
17
|
+
enemies = new Map();
|
|
18
|
+
bullets = new Map();
|
|
19
|
+
boss = null;
|
|
20
|
+
/** Velocity of each other tank, in pixels per tick, from its last two states, for extrapolating over a late state. */
|
|
21
|
+
enemyVel = new Map();
|
|
22
|
+
playerVel = new Map();
|
|
23
|
+
bossVel = null;
|
|
24
|
+
constructor(lv) {
|
|
25
|
+
// The level's own map is where every match starts, so it is a correct field until the room sends tiles.
|
|
26
|
+
this.game = createGame({ lv, difficultyScale: 1, tanks: [], carry: null });
|
|
27
|
+
this.game.banner = null;
|
|
28
|
+
}
|
|
29
|
+
/** The tank of the player in `slot`, created on first sight; the same object for the whole level. */
|
|
30
|
+
playerFor(slot, wire, order) {
|
|
31
|
+
let p = this.players.get(slot);
|
|
32
|
+
const kind = TANK_KINDS[wire[1]] ?? 'ducky';
|
|
33
|
+
const spec = tankSpec(kind);
|
|
34
|
+
const [c, r] = TEAM_STARTS[order] ?? TEAM_STARTS[0];
|
|
35
|
+
if (!p) {
|
|
36
|
+
p = {
|
|
37
|
+
player: true,
|
|
38
|
+
up: 1,
|
|
39
|
+
idx: order,
|
|
40
|
+
kind,
|
|
41
|
+
spec,
|
|
42
|
+
s: 26,
|
|
43
|
+
x: wire[2],
|
|
44
|
+
y: wire[3],
|
|
45
|
+
sx: c * T + 3,
|
|
46
|
+
sy: r * T + 3,
|
|
47
|
+
dir: wire[4],
|
|
48
|
+
speed: spec.speed,
|
|
49
|
+
cd: 0,
|
|
50
|
+
lives: 0,
|
|
51
|
+
alive: true,
|
|
52
|
+
respawn: 0,
|
|
53
|
+
shield: 0,
|
|
54
|
+
cloak: 0,
|
|
55
|
+
anim: wire[5],
|
|
56
|
+
color: PLAYER_COLORS[order] ?? COL.blue
|
|
57
|
+
};
|
|
58
|
+
this.players.set(slot, p);
|
|
59
|
+
}
|
|
60
|
+
p.idx = order;
|
|
61
|
+
p.kind = kind;
|
|
62
|
+
p.spec = spec;
|
|
63
|
+
p.speed = spec.speed;
|
|
64
|
+
p.color = PLAYER_COLORS[order] ?? COL.blue;
|
|
65
|
+
p.alive = wire[6] === 1;
|
|
66
|
+
p.respawn = wire[7];
|
|
67
|
+
p.shield = wire[8];
|
|
68
|
+
p.cloak = wire[9];
|
|
69
|
+
p.up = wire[10];
|
|
70
|
+
p.lives = wire[11];
|
|
71
|
+
return p;
|
|
72
|
+
}
|
|
73
|
+
/** The team slot of a tank on the field, or null for one no longer in the level. */
|
|
74
|
+
slotOf(player) {
|
|
75
|
+
for (const [slot, p] of this.players) {
|
|
76
|
+
if (p === player) {
|
|
77
|
+
return this.game.players.includes(p) ? slot : null;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
/** Takes the tiles of `state`, if it carries them; called when the moment drawn reaches that state. */
|
|
83
|
+
applyTiles(state) {
|
|
84
|
+
if (!state.map || !state.hp || state.map.length !== N * N || state.hp.length !== N * N) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
for (let r = 0; r < N; r++) {
|
|
88
|
+
for (let c = 0; c < N; c++) {
|
|
89
|
+
this.game.map[r][c] = Number(state.map[r * N + c]);
|
|
90
|
+
this.game.hp[r][c] = Number(state.hp[r * N + c]);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Runs `drive` with every other tank where the room will find it when it plays this browser's input, rather than
|
|
96
|
+
* where it is drawn, a few ticks in the past: at its position in the `newest` state, carried on at the speed it
|
|
97
|
+
* had since the state before it for `lead` ticks. Teammates, computer tanks and the boss all count, so the own
|
|
98
|
+
* tank, predicted at the present, stops short of a spot another tank is about to take instead of driving into it
|
|
99
|
+
* and being pulled back out.
|
|
100
|
+
*/
|
|
101
|
+
withNewestTanks(newest, previous, lead, ownSlot, drive) {
|
|
102
|
+
const g = this.game;
|
|
103
|
+
const drawn = [];
|
|
104
|
+
const moved = [];
|
|
105
|
+
const drawnEnemies = g.enemies;
|
|
106
|
+
const drawnBoss = g.boss;
|
|
107
|
+
const ticks = newest && previous ? newest.tick - previous.tick : 0;
|
|
108
|
+
/** Where a tank laid out as `wire` will be after `lead` more ticks; `still` when it is not driving steadily. */
|
|
109
|
+
const ahead = (wire, before, still) => {
|
|
110
|
+
const steady = !still && before && ticks > 0 && near(before, wire);
|
|
111
|
+
return [
|
|
112
|
+
wire[2] + (steady ? ((wire[2] - before[2]) / ticks) * lead : 0),
|
|
113
|
+
wire[3] + (steady ? ((wire[3] - before[3]) / ticks) * lead : 0)
|
|
114
|
+
];
|
|
115
|
+
};
|
|
116
|
+
for (const wire of newest?.state.players ?? []) {
|
|
117
|
+
const p = this.players.get(wire[0]);
|
|
118
|
+
if (p && wire[0] !== ownSlot) {
|
|
119
|
+
drawn.push({ x: p.x, y: p.y, alive: p.alive, respawn: p.respawn });
|
|
120
|
+
moved.push(p);
|
|
121
|
+
const before = previous?.state.players.find(w => w[0] === wire[0]);
|
|
122
|
+
[p.x, p.y] = ahead(wire, before, !before || before[7] !== 0 || wire[7] !== 0);
|
|
123
|
+
p.alive = wire[6] === 1;
|
|
124
|
+
p.respawn = wire[7];
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (newest) {
|
|
128
|
+
// Stand-ins, so the enemies being drawn keep their drawn positions.
|
|
129
|
+
g.enemies = newest.state.enemies.map((wire) => {
|
|
130
|
+
const before = previous?.state.enemies.find(w => w[0] === wire[0]);
|
|
131
|
+
const [x, y] = ahead(wire, before, !before || before[6] !== 0 || wire[6] !== 0);
|
|
132
|
+
return { ...(this.enemies.get(wire[0]) ?? blankEnemy(ENEMY_KINDS[wire[1]] ?? 'dust')), x, y, spawning: wire[6] };
|
|
133
|
+
});
|
|
134
|
+
const wire = newest.state.boss;
|
|
135
|
+
if (wire) {
|
|
136
|
+
const earlier = previous?.state.boss;
|
|
137
|
+
const before = earlier && earlier[0] === wire[0] ? earlier : undefined;
|
|
138
|
+
const [x, y] = ahead(wire, before, !before);
|
|
139
|
+
g.boss = { ...(this.boss?.id === wire[0] ? this.boss.boss : blankBoss(wire)), x, y };
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
g.boss = null;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
try {
|
|
146
|
+
return drive();
|
|
147
|
+
}
|
|
148
|
+
finally {
|
|
149
|
+
moved.forEach((p, i) => Object.assign(p, drawn[i]));
|
|
150
|
+
g.enemies = drawnEnemies;
|
|
151
|
+
g.boss = drawnBoss;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/** Shows the moment `sample` points at; the tank in `ownSlot` keeps the position the prediction gave it. */
|
|
155
|
+
apply(sample, ownSlot) {
|
|
156
|
+
const { from, to, alpha, extrapolate } = sample;
|
|
157
|
+
const a = from.state;
|
|
158
|
+
const b = to.state;
|
|
159
|
+
const dt = Math.max(1, to.tick - from.tick);
|
|
160
|
+
const g = this.game;
|
|
161
|
+
g.t = Math.round(lerp(a.t, b.t, alpha));
|
|
162
|
+
g.state = b.over ? 'over' : 'play';
|
|
163
|
+
g.petHp = b.petHp;
|
|
164
|
+
g.petFlash = b.petFlash;
|
|
165
|
+
g.freeze = b.freeze;
|
|
166
|
+
g.shake = b.shake;
|
|
167
|
+
g.score = b.score;
|
|
168
|
+
g.bossT = b.bossT;
|
|
169
|
+
g.banner = b.banner;
|
|
170
|
+
this.remaining = b.remaining;
|
|
171
|
+
g.players = b.players.map((wire, order) => {
|
|
172
|
+
const p = this.playerFor(wire[0], wire, order);
|
|
173
|
+
if (wire[0] !== ownSlot) {
|
|
174
|
+
const prev = a.players.find(w => w[0] === wire[0]);
|
|
175
|
+
const walkable = prev && prev[7] === 0 && wire[7] === 0;
|
|
176
|
+
const [x, y] = blend(walkable ? prev : null, wire, alpha);
|
|
177
|
+
p.x = x;
|
|
178
|
+
p.y = y;
|
|
179
|
+
p.dir = wire[4];
|
|
180
|
+
p.anim = wire[5];
|
|
181
|
+
if (walkable && near(prev, wire)) {
|
|
182
|
+
const v = { x: (wire[2] - prev[2]) / dt, y: (wire[3] - prev[3]) / dt };
|
|
183
|
+
this.playerVel.set(wire[0], v);
|
|
184
|
+
p.x += v.x * extrapolate;
|
|
185
|
+
p.y += v.y * extrapolate;
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
this.playerVel.delete(wire[0]);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return p;
|
|
192
|
+
});
|
|
193
|
+
const enemies = new Map();
|
|
194
|
+
for (const wire of b.enemies) {
|
|
195
|
+
const e = this.enemies.get(wire[0]) ?? blankEnemy(ENEMY_KINDS[wire[1]] ?? 'dust');
|
|
196
|
+
const prev = a.enemies.find(w => w[0] === wire[0]);
|
|
197
|
+
const walkable = prev && prev[6] === 0 && wire[6] === 0;
|
|
198
|
+
const [x, y] = blend(walkable ? prev : null, wire, alpha);
|
|
199
|
+
e.x = x;
|
|
200
|
+
e.y = y;
|
|
201
|
+
e.dir = wire[4];
|
|
202
|
+
e.anim = wire[5];
|
|
203
|
+
e.spawning = wire[6];
|
|
204
|
+
e.flash = wire[7];
|
|
205
|
+
e.angry = (wire[8] & 1) !== 0;
|
|
206
|
+
e.carrier = (wire[8] & 2) !== 0;
|
|
207
|
+
e.fuse = (wire[8] & 4) !== 0;
|
|
208
|
+
e.slow = wire[8] & 8 ? 1 : 0;
|
|
209
|
+
if (walkable && near(prev, wire)) {
|
|
210
|
+
const v = { x: (wire[2] - prev[2]) / dt, y: (wire[3] - prev[3]) / dt };
|
|
211
|
+
this.enemyVel.set(wire[0], v);
|
|
212
|
+
e.x += v.x * extrapolate;
|
|
213
|
+
e.y += v.y * extrapolate;
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
this.enemyVel.delete(wire[0]);
|
|
217
|
+
}
|
|
218
|
+
enemies.set(wire[0], e);
|
|
219
|
+
}
|
|
220
|
+
this.enemies = enemies;
|
|
221
|
+
g.enemies = [...enemies.values()];
|
|
222
|
+
g.boss = b.boss ? this.bossFrom(a.boss, b.boss, alpha, dt, extrapolate) : null;
|
|
223
|
+
const bullets = new Map();
|
|
224
|
+
const lead = sample.tick - to.tick;
|
|
225
|
+
for (const wire of b.bullets) {
|
|
226
|
+
const shot = this.bullets.get(wire[0]) ?? blankBullet();
|
|
227
|
+
shot.x = wire[1] + wire[3] * lead;
|
|
228
|
+
shot.y = wire[2] + wire[4] * lead;
|
|
229
|
+
shot.vx = wire[3];
|
|
230
|
+
shot.vy = wire[4];
|
|
231
|
+
shot.r = wire[5];
|
|
232
|
+
shot.owner = wire[6];
|
|
233
|
+
shot.kind = TANK_KINDS[wire[7]];
|
|
234
|
+
shot.big = (wire[8] & 1) !== 0;
|
|
235
|
+
shot.power = (wire[8] & 2) !== 0;
|
|
236
|
+
shot.rainbow = (wire[8] & 4) !== 0;
|
|
237
|
+
bullets.set(wire[0], shot);
|
|
238
|
+
}
|
|
239
|
+
this.bullets = bullets;
|
|
240
|
+
g.bullets = [...bullets.values()];
|
|
241
|
+
g.picks = b.pickups.map(([x, y, type, life]) => ({ x, y, type: PICKUP_TYPES[type] ?? 'star', life }));
|
|
242
|
+
}
|
|
243
|
+
bossFrom(prev, wire, alpha, dt, extrapolate) {
|
|
244
|
+
if (this.boss?.id !== wire[0]) {
|
|
245
|
+
this.boss = { id: wire[0], boss: blankBoss(wire) };
|
|
246
|
+
}
|
|
247
|
+
const b = this.boss.boss;
|
|
248
|
+
const nearPrev = prev && prev[0] === wire[0] && near(prev, wire);
|
|
249
|
+
const [x, y] = blend(nearPrev ? prev : null, wire, alpha);
|
|
250
|
+
b.x = x;
|
|
251
|
+
b.y = y;
|
|
252
|
+
b.dir = wire[4];
|
|
253
|
+
b.anim = wire[5];
|
|
254
|
+
b.hp = wire[6];
|
|
255
|
+
b.maxHp = wire[7];
|
|
256
|
+
b.flash = wire[8];
|
|
257
|
+
b.inv = wire[9];
|
|
258
|
+
b.shielded = (wire[10] & 1) !== 0;
|
|
259
|
+
b.charge = wire[11];
|
|
260
|
+
b.plan = wire[12];
|
|
261
|
+
if (nearPrev) {
|
|
262
|
+
this.bossVel = { x: (wire[2] - prev[2]) / dt, y: (wire[3] - prev[3]) / dt };
|
|
263
|
+
b.x += this.bossVel.x * extrapolate;
|
|
264
|
+
b.y += this.bossVel.y * extrapolate;
|
|
265
|
+
}
|
|
266
|
+
else {
|
|
267
|
+
this.bossVel = null;
|
|
268
|
+
}
|
|
269
|
+
return b;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
/** Whether the two wire positions are close enough to treat as the same thing moving, not a respawn or teleport. */
|
|
273
|
+
function near(prev, next) {
|
|
274
|
+
return Math.abs(prev[2] - next[2]) + Math.abs(prev[3] - next[3]) <= MAX_BLEND_DISTANCE;
|
|
275
|
+
}
|
|
276
|
+
/** Position between `prev` and `next`, both laid out with x and y at indexes 2 and 3; `next` alone after a jump. */
|
|
277
|
+
function blend(prev, next, alpha) {
|
|
278
|
+
if (!prev || Math.abs(prev[2] - next[2]) + Math.abs(prev[3] - next[3]) > MAX_BLEND_DISTANCE) {
|
|
279
|
+
return [next[2], next[3]];
|
|
280
|
+
}
|
|
281
|
+
return [lerp(prev[2], next[2], alpha), lerp(prev[3], next[3], alpha)];
|
|
282
|
+
}
|
|
283
|
+
function blankEnemy(kind) {
|
|
284
|
+
return {
|
|
285
|
+
kind,
|
|
286
|
+
x: 0,
|
|
287
|
+
y: 0,
|
|
288
|
+
s: 26,
|
|
289
|
+
dir: 2,
|
|
290
|
+
anim: 0,
|
|
291
|
+
speed: 0,
|
|
292
|
+
hp: 1,
|
|
293
|
+
color: ENEMY[kind].color,
|
|
294
|
+
pts: 0,
|
|
295
|
+
cd: 0,
|
|
296
|
+
turnT: 0,
|
|
297
|
+
spawning: 0,
|
|
298
|
+
flash: 0,
|
|
299
|
+
angry: false,
|
|
300
|
+
carrier: false,
|
|
301
|
+
aimT: 0,
|
|
302
|
+
stuck: 0,
|
|
303
|
+
minion: false,
|
|
304
|
+
role: 'hunt',
|
|
305
|
+
bias: 1,
|
|
306
|
+
dodgeT: 0,
|
|
307
|
+
hold: 0,
|
|
308
|
+
cell: -1
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
function blankBoss(wire) {
|
|
312
|
+
const spec = Object.values(BOSSES).find(s => s.id === wire[1]) ?? BOSSES.bedroom;
|
|
313
|
+
return {
|
|
314
|
+
boss: true,
|
|
315
|
+
kind: 'boss',
|
|
316
|
+
id: spec.id,
|
|
317
|
+
x: wire[2],
|
|
318
|
+
y: wire[3],
|
|
319
|
+
s: 60,
|
|
320
|
+
dir: wire[4],
|
|
321
|
+
anim: wire[5],
|
|
322
|
+
hp: wire[6],
|
|
323
|
+
maxHp: wire[7],
|
|
324
|
+
color: spec.color,
|
|
325
|
+
cd: 0,
|
|
326
|
+
flash: 0,
|
|
327
|
+
spd: 0,
|
|
328
|
+
tx: 0,
|
|
329
|
+
ty: 0,
|
|
330
|
+
retarget: 0,
|
|
331
|
+
sc: 0,
|
|
332
|
+
shielded: false,
|
|
333
|
+
inv: 0,
|
|
334
|
+
last: false,
|
|
335
|
+
still: 0,
|
|
336
|
+
dodgeT: 0,
|
|
337
|
+
axis: true,
|
|
338
|
+
dodgeDir: 0,
|
|
339
|
+
charge: 0,
|
|
340
|
+
volley: 0,
|
|
341
|
+
plan: [],
|
|
342
|
+
queue: []
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
function blankBullet() {
|
|
346
|
+
return { x: 0, y: 0, vx: 0, vy: 0, r: 4, owner: 0 };
|
|
347
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Cell } from './constants.ts';
|
|
2
|
+
import type { DistanceField } from './game-state.ts';
|
|
3
|
+
import type { TileMap } from './levels.ts';
|
|
4
|
+
/**
|
|
5
|
+
* Step cost from each cell to the nearest of `src`. Tanks can shoot through jelly, candy and gifts, so those cost
|
|
6
|
+
* extra to cross; `hard` makes them impassable, as for a bomb tank that cannot fire.
|
|
7
|
+
*/
|
|
8
|
+
export declare function distanceField(map: TileMap, src: readonly Cell[], hard?: boolean): DistanceField;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { DX, DY, N } from "./constants.js";
|
|
2
|
+
import { blankGrid } from "./levels.js";
|
|
3
|
+
/**
|
|
4
|
+
* Step cost from each cell to the nearest of `src`. Tanks can shoot through jelly, candy and gifts, so those cost
|
|
5
|
+
* extra to cross; `hard` makes them impassable, as for a bomb tank that cannot fire.
|
|
6
|
+
*/
|
|
7
|
+
export function distanceField(map, src, hard = false) {
|
|
8
|
+
const INF = 1e6;
|
|
9
|
+
const f = blankGrid(INF);
|
|
10
|
+
const done = blankGrid(false);
|
|
11
|
+
for (const [c, r] of src) {
|
|
12
|
+
f[r][c] = 0;
|
|
13
|
+
}
|
|
14
|
+
for (let it = 0; it < N * N; it++) {
|
|
15
|
+
let bc = -1;
|
|
16
|
+
let br = -1;
|
|
17
|
+
let bv = INF;
|
|
18
|
+
for (let r = 0; r < N; r++) {
|
|
19
|
+
for (let c = 0; c < N; c++) {
|
|
20
|
+
if (!done[r][c] && f[r][c] < bv) {
|
|
21
|
+
bv = f[r][c];
|
|
22
|
+
bc = c;
|
|
23
|
+
br = r;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (bc < 0) {
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
done[br][bc] = true;
|
|
31
|
+
for (let d = 0; d < 4; d++) {
|
|
32
|
+
const nc = bc + DX[d];
|
|
33
|
+
const nr = br + DY[d];
|
|
34
|
+
if (nc < 0 || nr < 0 || nc >= N || nr >= N) {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
const v = map[nr][nc];
|
|
38
|
+
const cost = v === 0 || v === 6 ? 1 : hard ? INF : v === 1 ? 3 : v === 2 ? 5 : v === 7 ? 7 : INF;
|
|
39
|
+
if (cost < INF && bv + cost < f[nr][nc]) {
|
|
40
|
+
f[nr][nc] = bv + cost;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return f;
|
|
45
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Game, Player, PowerUpType } from './game-state.ts';
|
|
2
|
+
import type { PaintTanksLabelKey } from './labels.ts';
|
|
3
|
+
import type { SoundName } from './sound-names.ts';
|
|
4
|
+
export declare const POWER_UPS: readonly PowerUpType[];
|
|
5
|
+
/** Background of each support item's badge, from the Design System palette; its pick-up effect uses it too. */
|
|
6
|
+
export declare const POWER_COLORS: Readonly<Record<PowerUpType, string>>;
|
|
7
|
+
/** Chance that a destroyed enemy also drops a support item. */
|
|
8
|
+
export declare const POWER_DROP = 0.08;
|
|
9
|
+
export declare const POWER_WEIGHTS: Readonly<Record<PowerUpType, number>>;
|
|
10
|
+
/** How long each timed item lasts, in frames at 60 fps. */
|
|
11
|
+
export declare const POWER_FRAMES: {
|
|
12
|
+
readonly shield: 420;
|
|
13
|
+
readonly cloak: 300;
|
|
14
|
+
readonly freeze: 240;
|
|
15
|
+
};
|
|
16
|
+
/** How long a dropped item waits on the floor. */
|
|
17
|
+
export declare const POWER_LIFE = 600;
|
|
18
|
+
export declare const POWER_SOUNDS: Readonly<Record<PowerUpType, SoundName>>;
|
|
19
|
+
/** What floats up where an item is picked up. */
|
|
20
|
+
export declare const POWER_PICK_LABELS: Readonly<Record<PowerUpType, PaintTanksLabelKey>>;
|
|
21
|
+
export declare function isPowerUp(type: string): type is PowerUpType;
|
|
22
|
+
/**
|
|
23
|
+
* The support item a destroyed enemy drops, if any. At most one waits on the floor at a time, and the bone only
|
|
24
|
+
* drops while the puppy is hurt.
|
|
25
|
+
*/
|
|
26
|
+
export declare function rollPowerUp(g: Game, rand?: () => number): PowerUpType | null;
|
|
27
|
+
/** Gives player `p` the effect of `type`; a timed effect is refreshed, never shortened. */
|
|
28
|
+
export declare function applyPowerUp(g: Game, p: Player, type: PowerUpType): void;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { COL } from "./constants.js";
|
|
2
|
+
export const POWER_UPS = ['shield', 'cloak', 'freeze', 'bone'];
|
|
3
|
+
/** Background of each support item's badge, from the Design System palette; its pick-up effect uses it too. */
|
|
4
|
+
export const POWER_COLORS = {
|
|
5
|
+
shield: COL.blue,
|
|
6
|
+
cloak: COL.muted,
|
|
7
|
+
freeze: COL.green,
|
|
8
|
+
bone: COL.red
|
|
9
|
+
};
|
|
10
|
+
/** Chance that a destroyed enemy also drops a support item. */
|
|
11
|
+
export const POWER_DROP = 0.08;
|
|
12
|
+
export const POWER_WEIGHTS = { shield: 35, cloak: 25, freeze: 25, bone: 15 };
|
|
13
|
+
/** How long each timed item lasts, in frames at 60 fps. */
|
|
14
|
+
export const POWER_FRAMES = { shield: 420, cloak: 300, freeze: 240 };
|
|
15
|
+
/** How long a dropped item waits on the floor. */
|
|
16
|
+
export const POWER_LIFE = 600;
|
|
17
|
+
export const POWER_SOUNDS = {
|
|
18
|
+
shield: 'up',
|
|
19
|
+
cloak: 'cloak',
|
|
20
|
+
freeze: 'freeze',
|
|
21
|
+
bone: 'heal'
|
|
22
|
+
};
|
|
23
|
+
/** What floats up where an item is picked up. */
|
|
24
|
+
export const POWER_PICK_LABELS = {
|
|
25
|
+
shield: 'pickShield',
|
|
26
|
+
cloak: 'pickCloak',
|
|
27
|
+
freeze: 'pickFreeze',
|
|
28
|
+
bone: 'pickBone'
|
|
29
|
+
};
|
|
30
|
+
export function isPowerUp(type) {
|
|
31
|
+
return POWER_UPS.includes(type);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The support item a destroyed enemy drops, if any. At most one waits on the floor at a time, and the bone only
|
|
35
|
+
* drops while the puppy is hurt.
|
|
36
|
+
*/
|
|
37
|
+
export function rollPowerUp(g, rand = Math.random) {
|
|
38
|
+
if (g.picks.some(k => !k.dead && isPowerUp(k.type)) || rand() >= POWER_DROP) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
const pool = POWER_UPS.filter(type => type !== 'bone' || g.petHp < g.D.pet);
|
|
42
|
+
const total = pool.reduce((sum, type) => sum + POWER_WEIGHTS[type], 0);
|
|
43
|
+
let roll = rand() * total;
|
|
44
|
+
for (const type of pool) {
|
|
45
|
+
roll -= POWER_WEIGHTS[type];
|
|
46
|
+
if (roll < 0) {
|
|
47
|
+
return type;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return pool[pool.length - 1] ?? null;
|
|
51
|
+
}
|
|
52
|
+
/** Gives player `p` the effect of `type`; a timed effect is refreshed, never shortened. */
|
|
53
|
+
export function applyPowerUp(g, p, type) {
|
|
54
|
+
if (type === 'shield') {
|
|
55
|
+
p.shield = Math.max(p.shield, POWER_FRAMES.shield);
|
|
56
|
+
}
|
|
57
|
+
else if (type === 'cloak') {
|
|
58
|
+
p.cloak = Math.max(p.cloak, POWER_FRAMES.cloak);
|
|
59
|
+
}
|
|
60
|
+
else if (type === 'freeze') {
|
|
61
|
+
g.freeze = Math.max(g.freeze, POWER_FRAMES.freeze);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
g.petHp = Math.min(g.D.pet, g.petHp + 1);
|
|
65
|
+
}
|
|
66
|
+
}
|
package/dist/random.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const rnd: (a: number, b: number) => number;
|
package/dist/random.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const rnd = (a, b) => a + Math.random() * (b - a);
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { TankKind } from './constants.ts';
|
|
2
|
+
import type { ParticleType } from './game-state.ts';
|
|
3
|
+
import type { PaintTanksLabelKey } from './labels.ts';
|
|
4
|
+
import type { SoundName } from './sound-names.ts';
|
|
5
|
+
/** Text that floats up from the field, kept language-neutral until it is shown. */
|
|
6
|
+
export type FloatingLabel = {
|
|
7
|
+
kind: 'label';
|
|
8
|
+
key: PaintTanksLabelKey;
|
|
9
|
+
}
|
|
10
|
+
/** Points scored, shown with a plus sign in the number format of the page. */
|
|
11
|
+
| {
|
|
12
|
+
kind: 'points';
|
|
13
|
+
value: number;
|
|
14
|
+
}
|
|
15
|
+
/** Text that reads the same in every language, such as `-1♥`. */
|
|
16
|
+
| {
|
|
17
|
+
kind: 'raw';
|
|
18
|
+
text: string;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* What the simulation asks to be seen and heard. It never draws or plays anything itself: in the browser these
|
|
22
|
+
* become particles and sounds, and the online server records them to send to every player.
|
|
23
|
+
*/
|
|
24
|
+
export interface SimEffects {
|
|
25
|
+
sound: (name: SoundName | '') => void;
|
|
26
|
+
burst: (x: number, y: number, color: string, count: number, type?: ParticleType) => void;
|
|
27
|
+
/** The paint splash of a player tank's shot, which differs by tank. */
|
|
28
|
+
splash: (kind: TankKind, x: number, y: number, color: string, scale: number) => void;
|
|
29
|
+
float: (x: number, y: number, label: FloatingLabel, color: string) => void;
|
|
30
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { AiHost } from './ai-host.ts';
|
|
2
|
+
import type { Boss, Bullet, EndMessage, Enemy, Game, Particle, Player, PlayerInput } from './game-state.ts';
|
|
3
|
+
import type { SimEffects } from './sim-effects.ts';
|
|
4
|
+
import type { SoundName } from './sound-names.ts';
|
|
5
|
+
/** How a level ended, worked out once the end-of-level pause is over. */
|
|
6
|
+
export interface SimOutcome {
|
|
7
|
+
win: boolean;
|
|
8
|
+
lv: number;
|
|
9
|
+
/** Final score, with the clear bonus on a win. */
|
|
10
|
+
score: number;
|
|
11
|
+
/** Stars picked up, plus the clear bonus on a win. */
|
|
12
|
+
stars: number;
|
|
13
|
+
/** How far the level got, 0 to 1. */
|
|
14
|
+
progress: number;
|
|
15
|
+
/** Why the level was lost, for the end screen; null on a win. */
|
|
16
|
+
lossMessage: EndMessage | null;
|
|
17
|
+
/** Power level each player carries into the next level. */
|
|
18
|
+
carryUp: number[];
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The rules of one level: players, enemies, bullets, pick-ups and the end of the level. It runs the same in the
|
|
22
|
+
* browser for local play and on the online server, so it touches no DOM, canvas or audio; everything to show or
|
|
23
|
+
* play goes to `effects`.
|
|
24
|
+
*/
|
|
25
|
+
export declare class PaintTanksSimulation implements AiHost {
|
|
26
|
+
readonly game: Game;
|
|
27
|
+
private readonly effects;
|
|
28
|
+
outcome: SimOutcome | null;
|
|
29
|
+
/** For each online player, the input number of the last claim taken into account and the movement banked. */
|
|
30
|
+
private readonly claims;
|
|
31
|
+
/** Ticks each fallen player has waited for another tank to leave their starting position. */
|
|
32
|
+
private readonly respawnWait;
|
|
33
|
+
constructor(game: Game, effects: SimEffects);
|
|
34
|
+
sound(name: SoundName | ''): void;
|
|
35
|
+
burst(x: number, y: number, color: string, count: number, type?: Particle['type']): void;
|
|
36
|
+
/** Enemies the players still have to beat, the boss included. */
|
|
37
|
+
remaining(): number;
|
|
38
|
+
/** Advances one frame (1/60 s); `inputs[i]` drives player `i`, a missing one holds still. */
|
|
39
|
+
step(inputs: readonly PlayerInput[]): void;
|
|
40
|
+
/**
|
|
41
|
+
* Online movement: the browser drives its own tank and claims where it ended up. Each tick on the server earns the
|
|
42
|
+
* tank one tick of movement, banked up to `CLAIM_MAX_TICKS`, and a claim may spend as many ticks as the browser
|
|
43
|
+
* inputs it covers. Inputs that arrive bunched together after a late one are therefore adopted, while over time a
|
|
44
|
+
* tank never moves faster than its speed, whatever input numbers a browser sends. A claim within the budget that
|
|
45
|
+
* ends in a tank or wall is adopted as far as it is clear; any other claim that breaks the rules is replaced by one
|
|
46
|
+
* ordinary step in the held direction. Either way the browser is corrected.
|
|
47
|
+
*/
|
|
48
|
+
private applyClaim;
|
|
49
|
+
/** Takes player `p` out of the level, as when they leave an online room for good. */
|
|
50
|
+
retire(p: Player): void;
|
|
51
|
+
private stepPlayer;
|
|
52
|
+
/** Cooldowns and abilities a tank's spec gives it, run once a frame while it is in play. */
|
|
53
|
+
private stepPassives;
|
|
54
|
+
breakTile(r: number, c: number, v: number): void;
|
|
55
|
+
fire(p: Player): void;
|
|
56
|
+
efire(e: Enemy | Boss, vx: number, vy: number, shot?: Pick<Bullet, 'bounce' | 'rainbow'>): void;
|
|
57
|
+
private splat;
|
|
58
|
+
private bulletStep;
|
|
59
|
+
/**
|
|
60
|
+
* Turns shot `b` back from what it just ran into, when it has a bounce left: it returns to (`px`, `py`) and
|
|
61
|
+
* reverses along each axis it crossed.
|
|
62
|
+
*/
|
|
63
|
+
private bounce;
|
|
64
|
+
/** Whether a guarding tank's front stops enemy shot `b`: one flying at it from ahead while the guard is ready. */
|
|
65
|
+
private guards;
|
|
66
|
+
/** Paint splashing from a hit on `hit`: every other ordinary enemy within `radius` loses 1. */
|
|
67
|
+
private splashAround;
|
|
68
|
+
private hitEnemy;
|
|
69
|
+
killEnemy(e: Enemy | Boss, self: boolean, by?: Bullet): void;
|
|
70
|
+
private boom;
|
|
71
|
+
hitPlayer(p: Player): void;
|
|
72
|
+
/** Ends the level once its closing pause is over: adds the clear bonus and records the outcome. */
|
|
73
|
+
private conclude;
|
|
74
|
+
}
|