@maco-online/paint-tanks-rules 0.2.0 → 0.4.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/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/online/match.js +2 -2
- package/dist/online/protocol.d.ts +6 -3
- package/dist/online/protocol.js +6 -3
- package/dist/online/room-game.js +1 -1
- package/dist/online/shot-preview.d.ts +16 -0
- package/dist/online/shot-preview.js +38 -0
- package/dist/online/view.d.ts +19 -0
- package/dist/online/view.js +155 -18
- package/dist/simulation.d.ts +2 -0
- package/dist/simulation.js +11 -6
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/online/match.js
CHANGED
|
@@ -3,8 +3,8 @@ import { PaintTanksSimulation } from "../simulation.js";
|
|
|
3
3
|
import { ENEMY_KINDS, PARTICLE_TYPES, PICKUP_TYPES, TANK_KINDS } from "./protocol.js";
|
|
4
4
|
/** Effects between two states are capped, so a burst of explosions cannot make one message huge. */
|
|
5
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 =
|
|
6
|
+
/** The map is sent at least this often, in states (about two-thirds of a second), for members who joined since it last changed. */
|
|
7
|
+
const MAP_KEYFRAME_EVERY = 40;
|
|
8
8
|
/** The command a member has not yet sent anything replaces: hold still at whatever place it has. */
|
|
9
9
|
const IDLE = { d: -1, f: false, x: 0, y: 0, n: 0 };
|
|
10
10
|
/** Rounds a coordinate to a tenth of a pixel, which is all a browser needs to draw it. */
|
|
@@ -6,13 +6,16 @@ import type { SoundName } from '../sound-names.ts';
|
|
|
6
6
|
import { z } from 'zod';
|
|
7
7
|
/**
|
|
8
8
|
* What Paint Tanks puts in the generic room protocol: a player's controls, the host's lobby settings, each
|
|
9
|
-
* member's tank, and the state of the field, packed into short arrays because it is sent
|
|
9
|
+
* member's tank, and the state of the field, packed into short arrays because it is sent 60 times a second.
|
|
10
10
|
*/
|
|
11
11
|
export declare const PAINT_TANKS_ONLINE_GAME = "paint-tanks";
|
|
12
12
|
export declare const PAINT_TANKS_TEAM_SIZE = 5;
|
|
13
13
|
export declare const PAINT_TANKS_TICK_RATE = 60;
|
|
14
|
-
/**
|
|
15
|
-
|
|
14
|
+
/**
|
|
15
|
+
* A state every tick: 60 a second. Each state is about a kilobyte, and with one per tick a browser needs only a
|
|
16
|
+
* tick or two of states in hand to blend between, so every tank and shot is drawn that much closer to the present.
|
|
17
|
+
*/
|
|
18
|
+
export declare const PAINT_TANKS_STATE_EVERY = 1;
|
|
16
19
|
/** Tanks by their number on the wire: append new tanks at the end and never reorder, so the numbers stay put. */
|
|
17
20
|
export declare const TANK_KINDS: readonly ["ducky", "bunny", "robo", "dino", "bee", "crab", "firetruck", "penguin", "turtle", "frog", "unicorn", "swordfish", "octopus", "giraffe", "elephant", "mole", "cat", "bear", "piggy"];
|
|
18
21
|
/** Enemies by their number on the wire; append only, as for `TANK_KINDS`. */
|
package/dist/online/protocol.js
CHANGED
|
@@ -2,13 +2,16 @@ import { z } from 'zod';
|
|
|
2
2
|
import { MAX_TEAM, MAXL } from "../constants.js";
|
|
3
3
|
/**
|
|
4
4
|
* What Paint Tanks puts in the generic room protocol: a player's controls, the host's lobby settings, each
|
|
5
|
-
* member's tank, and the state of the field, packed into short arrays because it is sent
|
|
5
|
+
* member's tank, and the state of the field, packed into short arrays because it is sent 60 times a second.
|
|
6
6
|
*/
|
|
7
7
|
export const PAINT_TANKS_ONLINE_GAME = 'paint-tanks';
|
|
8
8
|
export const PAINT_TANKS_TEAM_SIZE = MAX_TEAM;
|
|
9
9
|
export const PAINT_TANKS_TICK_RATE = 60;
|
|
10
|
-
/**
|
|
11
|
-
|
|
10
|
+
/**
|
|
11
|
+
* A state every tick: 60 a second. Each state is about a kilobyte, and with one per tick a browser needs only a
|
|
12
|
+
* tick or two of states in hand to blend between, so every tank and shot is drawn that much closer to the present.
|
|
13
|
+
*/
|
|
14
|
+
export const PAINT_TANKS_STATE_EVERY = 1;
|
|
12
15
|
/** Tanks by their number on the wire: append new tanks at the end and never reorder, so the numbers stay put. */
|
|
13
16
|
export const TANK_KINDS = [
|
|
14
17
|
'ducky',
|
package/dist/online/room-game.js
CHANGED
|
@@ -3,7 +3,7 @@ import { PAINT_TANKS_ONLINE_GAME, PAINT_TANKS_STATE_EVERY, PAINT_TANKS_TEAM_SIZE
|
|
|
3
3
|
/** Paint Tanks as the room server hosts it: up to five players on one team against the dust tanks. */
|
|
4
4
|
export const paintTanksRoomGame = {
|
|
5
5
|
id: PAINT_TANKS_ONLINE_GAME,
|
|
6
|
-
protocolVersion:
|
|
6
|
+
protocolVersion: 4,
|
|
7
7
|
maxMembers: PAINT_TANKS_TEAM_SIZE,
|
|
8
8
|
tickRate: PAINT_TANKS_TICK_RATE,
|
|
9
9
|
stateEvery: PAINT_TANKS_STATE_EVERY,
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Game, Player } from '../game-state.ts';
|
|
2
|
+
import type { SimEffects } from '../sim-effects.ts';
|
|
3
|
+
/** What firing the own shot did on a scratch copy of the field, in the tick it was fired. */
|
|
4
|
+
export interface OwnShotPreview {
|
|
5
|
+
/** Every bullet of the shot already hit something, so the room will never send it. */
|
|
6
|
+
ended: boolean;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Fires `me`'s shot on a scratch copy of the field `g` and flies it through the tick it is fired in, exactly as the
|
|
10
|
+
* room does. A shot fired at a tile right at the barrel ends within that tick, so no state ever carries it: on its
|
|
11
|
+
* own the browser would play the shot at once but the hit only when the room's state is drawn, a moment later, and
|
|
12
|
+
* the bullet would seem lost. `effects` hears only what the bullet did, not the shot itself, which the browser plays
|
|
13
|
+
* already. The field drawn is left alone: its tiles change when the room's state says so. Other tanks are left out of
|
|
14
|
+
* the copy, so the preview only ends a shot on the field, and with no pet to heal it never guesses a medic's heal.
|
|
15
|
+
*/
|
|
16
|
+
export declare function previewOwnShot(g: Game, me: Player, effects: SimEffects): OwnShotPreview;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { PaintTanksSimulation } from "../simulation.js";
|
|
2
|
+
/**
|
|
3
|
+
* Fires `me`'s shot on a scratch copy of the field `g` and flies it through the tick it is fired in, exactly as the
|
|
4
|
+
* room does. A shot fired at a tile right at the barrel ends within that tick, so no state ever carries it: on its
|
|
5
|
+
* own the browser would play the shot at once but the hit only when the room's state is drawn, a moment later, and
|
|
6
|
+
* the bullet would seem lost. `effects` hears only what the bullet did, not the shot itself, which the browser plays
|
|
7
|
+
* already. The field drawn is left alone: its tiles change when the room's state says so. Other tanks are left out of
|
|
8
|
+
* the copy, so the preview only ends a shot on the field, and with no pet to heal it never guesses a medic's heal.
|
|
9
|
+
*/
|
|
10
|
+
export function previewOwnShot(g, me, effects) {
|
|
11
|
+
const shooter = { ...me };
|
|
12
|
+
const copy = {
|
|
13
|
+
...g,
|
|
14
|
+
map: g.map.map(row => [...row]),
|
|
15
|
+
hp: g.hp.map(row => [...row]),
|
|
16
|
+
players: [shooter],
|
|
17
|
+
enemies: [],
|
|
18
|
+
boss: null,
|
|
19
|
+
bullets: [],
|
|
20
|
+
picks: [],
|
|
21
|
+
parts: [],
|
|
22
|
+
texts: [],
|
|
23
|
+
petHp: 0
|
|
24
|
+
};
|
|
25
|
+
let heard = false;
|
|
26
|
+
const gated = {
|
|
27
|
+
sound: name => heard && effects.sound(name),
|
|
28
|
+
burst: (...args) => heard && effects.burst(...args),
|
|
29
|
+
splash: (...args) => heard && effects.splash(...args),
|
|
30
|
+
float: (...args) => heard && effects.float(...args)
|
|
31
|
+
};
|
|
32
|
+
const sim = new PaintTanksSimulation(copy, gated);
|
|
33
|
+
sim.fire(shooter);
|
|
34
|
+
const fired = copy.bullets.length;
|
|
35
|
+
heard = true;
|
|
36
|
+
sim.flyBullets();
|
|
37
|
+
return { ended: fired > 0 && copy.bullets.length === 0 };
|
|
38
|
+
}
|
package/dist/online/view.d.ts
CHANGED
|
@@ -22,6 +22,8 @@ export declare class OnlineView {
|
|
|
22
22
|
private readonly enemyVel;
|
|
23
23
|
private readonly playerVel;
|
|
24
24
|
private bossVel;
|
|
25
|
+
/** How far each tank next to the own tank is drawn from its drawn place, by `pullNearTanks`. */
|
|
26
|
+
private readonly shifts;
|
|
25
27
|
constructor(lv: number);
|
|
26
28
|
/** The tank of the player in `slot`, created on first sight; the same object for the whole level. */
|
|
27
29
|
playerFor(slot: number, wire: WirePlayer, order: number): Player;
|
|
@@ -37,6 +39,23 @@ export declare class OnlineView {
|
|
|
37
39
|
* and being pulled back out.
|
|
38
40
|
*/
|
|
39
41
|
withNewestTanks<T>(newest: TimedState | null, previous: TimedState | null, lead: number, ownSlot: number | null, drive: () => T): T;
|
|
42
|
+
/**
|
|
43
|
+
* Draws the tanks right next to the own tank where its collisions are tested, as `withNewestTanks` places them,
|
|
44
|
+
* instead of where they were a few ticks ago. Without it the own tank, which is drawn at the present, slides into
|
|
45
|
+
* a teammate drawn in the past, or stops short of one that has not visibly arrived yet. A tank a tank's width away
|
|
46
|
+
* or further keeps its drawn place, so tanks that stop or turn far from the own tank never jump back on screen;
|
|
47
|
+
* in between the two places blend, so a tank glides from one to the other as it closes in.
|
|
48
|
+
*
|
|
49
|
+
* A tank gone from the newest state, such as one just shot, keeps its shift until it leaves the field drawn, and
|
|
50
|
+
* the shifts fade out over `PULL_FOLLOW_MS` while the own tank is down, so neither jumps a tank back to its drawn
|
|
51
|
+
* place.
|
|
52
|
+
*/
|
|
53
|
+
pullNearTanks(newest: TimedState | null, previous: TimedState | null, lead: number, ownSlot: number | null, me: Player, elapsedMs: number): void;
|
|
54
|
+
/**
|
|
55
|
+
* Where the room will find every tank but the own one when it plays this browser's next input: at its position in
|
|
56
|
+
* the `newest` state, carried on at the speed it had since the state before it for `lead` ticks.
|
|
57
|
+
*/
|
|
58
|
+
private collisionPlaces;
|
|
40
59
|
/** Shows the moment `sample` points at; the tank in `ownSlot` keeps the position the prediction gave it. */
|
|
41
60
|
apply(sample: TimelineSample<PaintTanksState>, ownSlot: number | null): void;
|
|
42
61
|
private bossFrom;
|
package/dist/online/view.js
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
import { BOSSES } from "../bosses.js";
|
|
2
2
|
import { COL, ENEMY, N, PLAYER_COLORS, T, tankSpec, TEAM_STARTS } from "../constants.js";
|
|
3
3
|
import { createGame } from "../game-factory.js";
|
|
4
|
+
import { isBlockedByTiles } from "../movement.js";
|
|
4
5
|
import { ENEMY_KINDS, PICKUP_TYPES, TANK_KINDS } from "./protocol.js";
|
|
5
6
|
/** Further than this between two states is a jump, such as a respawn, and is not blended. */
|
|
6
7
|
const MAX_BLEND_DISTANCE = 40;
|
|
8
|
+
/** A tank this many pixels or more from the own tank is drawn where it was, not where its collisions are tested. */
|
|
9
|
+
const CONTACT_RANGE = 26;
|
|
10
|
+
/** How quickly a tank's shift fades once the own tank is down: about 63% of the way after this many ms. */
|
|
11
|
+
const PULL_FOLLOW_MS = 60;
|
|
12
|
+
/** Halvings when stopping a tank carried on against a wall or the own tank: about a pixel on the longest lead. */
|
|
13
|
+
const CLAMP_STEPS = 6;
|
|
7
14
|
const lerp = (a, b, t) => a + (b - a) * t;
|
|
8
15
|
/**
|
|
9
16
|
* The field of an online level as this browser draws it, rebuilt from the states the room sends. Every tank and
|
|
@@ -21,6 +28,8 @@ export class OnlineView {
|
|
|
21
28
|
enemyVel = new Map();
|
|
22
29
|
playerVel = new Map();
|
|
23
30
|
bossVel = null;
|
|
31
|
+
/** How far each tank next to the own tank is drawn from its drawn place, by `pullNearTanks`. */
|
|
32
|
+
shifts = new Map();
|
|
24
33
|
constructor(lv) {
|
|
25
34
|
// The level's own map is where every match starts, so it is a correct field until the room sends tiles.
|
|
26
35
|
this.game = createGame({ lv, difficultyScale: 1, tanks: [], carry: null });
|
|
@@ -104,22 +113,14 @@ export class OnlineView {
|
|
|
104
113
|
const moved = [];
|
|
105
114
|
const drawnEnemies = g.enemies;
|
|
106
115
|
const drawnBoss = g.boss;
|
|
107
|
-
const
|
|
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
|
+
const places = this.collisionPlaces(newest, previous, lead, ownSlot);
|
|
116
117
|
for (const wire of newest?.state.players ?? []) {
|
|
117
118
|
const p = this.players.get(wire[0]);
|
|
118
|
-
|
|
119
|
+
const at = places.players.get(wire[0]);
|
|
120
|
+
if (p && at) {
|
|
119
121
|
drawn.push({ x: p.x, y: p.y, alive: p.alive, respawn: p.respawn });
|
|
120
122
|
moved.push(p);
|
|
121
|
-
|
|
122
|
-
[p.x, p.y] = ahead(wire, before, !before || before[7] !== 0 || wire[7] !== 0);
|
|
123
|
+
[p.x, p.y] = at;
|
|
123
124
|
p.alive = wire[6] === 1;
|
|
124
125
|
p.respawn = wire[7];
|
|
125
126
|
}
|
|
@@ -127,15 +128,12 @@ export class OnlineView {
|
|
|
127
128
|
if (newest) {
|
|
128
129
|
// Stand-ins, so the enemies being drawn keep their drawn positions.
|
|
129
130
|
g.enemies = newest.state.enemies.map((wire) => {
|
|
130
|
-
const
|
|
131
|
-
const [x, y] = ahead(wire, before, !before || before[6] !== 0 || wire[6] !== 0);
|
|
131
|
+
const [x, y] = places.enemies.get(wire[0]);
|
|
132
132
|
return { ...(this.enemies.get(wire[0]) ?? blankEnemy(ENEMY_KINDS[wire[1]] ?? 'dust')), x, y, spawning: wire[6] };
|
|
133
133
|
});
|
|
134
134
|
const wire = newest.state.boss;
|
|
135
|
-
if (wire) {
|
|
136
|
-
const
|
|
137
|
-
const before = earlier && earlier[0] === wire[0] ? earlier : undefined;
|
|
138
|
-
const [x, y] = ahead(wire, before, !before);
|
|
135
|
+
if (wire && places.boss) {
|
|
136
|
+
const [x, y] = places.boss;
|
|
139
137
|
g.boss = { ...(this.boss?.id === wire[0] ? this.boss.boss : blankBoss(wire)), x, y };
|
|
140
138
|
}
|
|
141
139
|
else {
|
|
@@ -151,6 +149,145 @@ export class OnlineView {
|
|
|
151
149
|
g.boss = drawnBoss;
|
|
152
150
|
}
|
|
153
151
|
}
|
|
152
|
+
/**
|
|
153
|
+
* Draws the tanks right next to the own tank where its collisions are tested, as `withNewestTanks` places them,
|
|
154
|
+
* instead of where they were a few ticks ago. Without it the own tank, which is drawn at the present, slides into
|
|
155
|
+
* a teammate drawn in the past, or stops short of one that has not visibly arrived yet. A tank a tank's width away
|
|
156
|
+
* or further keeps its drawn place, so tanks that stop or turn far from the own tank never jump back on screen;
|
|
157
|
+
* in between the two places blend, so a tank glides from one to the other as it closes in.
|
|
158
|
+
*
|
|
159
|
+
* A tank gone from the newest state, such as one just shot, keeps its shift until it leaves the field drawn, and
|
|
160
|
+
* the shifts fade out over `PULL_FOLLOW_MS` while the own tank is down, so neither jumps a tank back to its drawn
|
|
161
|
+
* place.
|
|
162
|
+
*/
|
|
163
|
+
pullNearTanks(newest, previous, lead, ownSlot, me, elapsedMs) {
|
|
164
|
+
const places = this.collisionPlaces(newest, previous, lead, ownSlot);
|
|
165
|
+
const active = !!newest && me.alive && me.respawn === 0;
|
|
166
|
+
const follow = 1 - Math.exp(-Math.max(0, elapsedMs) / PULL_FOLLOW_MS);
|
|
167
|
+
const seen = new Set();
|
|
168
|
+
const pull = (key, tank, at) => {
|
|
169
|
+
seen.add(key);
|
|
170
|
+
const shift = this.shifts.get(key) ?? { x: 0, y: 0 };
|
|
171
|
+
// Held while the tank is gone from the newest state, fading while the own tank is down.
|
|
172
|
+
let target = active ? { ...shift } : { x: 0, y: 0 };
|
|
173
|
+
let exact = false;
|
|
174
|
+
if (active && at) {
|
|
175
|
+
exact = true;
|
|
176
|
+
const reach = (me.s + tank.s) / 2;
|
|
177
|
+
const dx = Math.abs(at[0] + tank.s / 2 - (me.x + me.s / 2)) - reach;
|
|
178
|
+
const dy = Math.abs(at[1] + tank.s / 2 - (me.y + me.s / 2)) - reach;
|
|
179
|
+
const weight = Math.min(1, Math.max(0, 1 - Math.max(dx, dy, 0) / CONTACT_RANGE));
|
|
180
|
+
target = { x: (at[0] - tank.x) * weight, y: (at[1] - tank.y) * weight };
|
|
181
|
+
}
|
|
182
|
+
if (Math.abs(target.x) + Math.abs(target.y) > MAX_BLEND_DISTANCE) {
|
|
183
|
+
// A respawn or a teleport, not a tank closing in: show it where it is.
|
|
184
|
+
this.shifts.delete(key);
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
if (exact) {
|
|
188
|
+
// Where the own tank collides with it, so the two touch on screen exactly where they touch in the room.
|
|
189
|
+
shift.x = target.x;
|
|
190
|
+
shift.y = target.y;
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
shift.x += (target.x - shift.x) * follow;
|
|
194
|
+
shift.y += (target.y - shift.y) * follow;
|
|
195
|
+
}
|
|
196
|
+
if (Math.abs(shift.x) + Math.abs(shift.y) < 0.05 && Math.abs(target.x) + Math.abs(target.y) < 0.05) {
|
|
197
|
+
this.shifts.delete(key);
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
this.shifts.set(key, shift);
|
|
201
|
+
tank.x += shift.x;
|
|
202
|
+
tank.y += shift.y;
|
|
203
|
+
};
|
|
204
|
+
for (const [slot, p] of this.players) {
|
|
205
|
+
if (this.game.players.includes(p) && slot !== ownSlot) {
|
|
206
|
+
pull(`p${slot}`, p, places.players.get(slot));
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
for (const [id, e] of this.enemies) {
|
|
210
|
+
pull(`e${id}`, e, places.enemies.get(id));
|
|
211
|
+
}
|
|
212
|
+
if (this.boss && this.game.boss === this.boss.boss) {
|
|
213
|
+
pull(`b${this.boss.id}`, this.boss.boss, newest?.state.boss?.[0] === this.boss.id ? places.boss : null);
|
|
214
|
+
}
|
|
215
|
+
for (const key of this.shifts.keys()) {
|
|
216
|
+
if (!seen.has(key)) {
|
|
217
|
+
this.shifts.delete(key);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Where the room will find every tank but the own one when it plays this browser's next input: at its position in
|
|
223
|
+
* the `newest` state, carried on at the speed it had since the state before it for `lead` ticks.
|
|
224
|
+
*/
|
|
225
|
+
collisionPlaces(newest, previous, lead, ownSlot) {
|
|
226
|
+
const places = {
|
|
227
|
+
players: new Map(),
|
|
228
|
+
enemies: new Map(),
|
|
229
|
+
boss: null
|
|
230
|
+
};
|
|
231
|
+
if (!newest) {
|
|
232
|
+
return places;
|
|
233
|
+
}
|
|
234
|
+
const ticks = previous ? newest.tick - previous.tick : 0;
|
|
235
|
+
const me = ownSlot === null ? undefined : this.players.get(ownSlot);
|
|
236
|
+
/** Whether a tank of size `s` at (`x`, `y`) stays clear of the own tank, touching it at most. */
|
|
237
|
+
const clearOfMe = (x, y, s) => !me || !me.alive || me.respawn > 0
|
|
238
|
+
|| Math.abs(x + s / 2 - (me.x + me.s / 2)) >= (s + me.s) / 2
|
|
239
|
+
|| Math.abs(y + s / 2 - (me.y + me.s / 2)) >= (s + me.s) / 2;
|
|
240
|
+
/** Whether a tank of size `s` may stand at (`x`, `y`): on the field, off solid tiles and clear of the own tank. */
|
|
241
|
+
const open = (x, y, s, swim) => clearOfMe(x, y, s) && !isBlockedByTiles(this.game, { x, y, s }, x, y, swim);
|
|
242
|
+
/**
|
|
243
|
+
* Where a tank laid out as `wire`, of size `s`, will be after `lead` more ticks; `still` when it is not driving
|
|
244
|
+
* steadily. A tank that walls and tanks stop (`stops`) is carried on no further than the next wall or the own
|
|
245
|
+
* tank, as in the room: carried through, a tank that already stopped would block the own tank from where it never
|
|
246
|
+
* is, and be drawn on top of it.
|
|
247
|
+
*/
|
|
248
|
+
const ahead = (wire, before, still, s, stops = true, swim = false) => {
|
|
249
|
+
const steady = !still && before && ticks > 0 && near(before, wire);
|
|
250
|
+
const dx = steady ? ((wire[2] - before[2]) / ticks) * lead : 0;
|
|
251
|
+
const dy = steady ? ((wire[3] - before[3]) / ticks) * lead : 0;
|
|
252
|
+
if (!stops || open(wire[2] + dx, wire[3] + dy, s, swim)) {
|
|
253
|
+
return [wire[2] + dx, wire[3] + dy];
|
|
254
|
+
}
|
|
255
|
+
if (!open(wire[2], wire[3], s, swim)) {
|
|
256
|
+
return [wire[2], wire[3]];
|
|
257
|
+
}
|
|
258
|
+
let low = 0;
|
|
259
|
+
let high = 1;
|
|
260
|
+
for (let i = 0; i < CLAMP_STEPS; i++) {
|
|
261
|
+
const mid = (low + high) / 2;
|
|
262
|
+
if (open(wire[2] + dx * mid, wire[3] + dy * mid, s, swim)) {
|
|
263
|
+
low = mid;
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
high = mid;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return [wire[2] + dx * low, wire[3] + dy * low];
|
|
270
|
+
};
|
|
271
|
+
for (const wire of newest.state.players) {
|
|
272
|
+
if (wire[0] !== ownSlot) {
|
|
273
|
+
const before = previous?.state.players.find(w => w[0] === wire[0]);
|
|
274
|
+
const p = this.players.get(wire[0]);
|
|
275
|
+
places.players.set(wire[0], ahead(wire, before, !before || before[7] !== 0 || wire[7] !== 0, p?.s ?? 26, true, !!p?.spec.swim));
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
for (const wire of newest.state.enemies) {
|
|
279
|
+
const before = previous?.state.enemies.find(w => w[0] === wire[0]);
|
|
280
|
+
places.enemies.set(wire[0], ahead(wire, before, !before || before[6] !== 0 || wire[6] !== 0, this.enemies.get(wire[0])?.s ?? 26));
|
|
281
|
+
}
|
|
282
|
+
const wire = newest.state.boss;
|
|
283
|
+
if (wire) {
|
|
284
|
+
const earlier = previous?.state.boss;
|
|
285
|
+
const before = earlier && earlier[0] === wire[0] ? earlier : undefined;
|
|
286
|
+
// The boss crosses tiles and tanks alike, kept only on the field.
|
|
287
|
+
places.boss = ahead(wire, before, !before, 60, false);
|
|
288
|
+
}
|
|
289
|
+
return places;
|
|
290
|
+
}
|
|
154
291
|
/** Shows the moment `sample` points at; the tank in `ownSlot` keeps the position the prediction gave it. */
|
|
155
292
|
apply(sample, ownSlot) {
|
|
156
293
|
const { from, to, alpha, extrapolate } = sample;
|
package/dist/simulation.d.ts
CHANGED
|
@@ -54,6 +54,8 @@ export declare class PaintTanksSimulation implements AiHost {
|
|
|
54
54
|
breakTile(r: number, c: number, v: number): void;
|
|
55
55
|
fire(p: Player): void;
|
|
56
56
|
efire(e: Enemy | Boss, vx: number, vy: number, shot?: Pick<Bullet, 'bounce' | 'rainbow'>): void;
|
|
57
|
+
/** Moves every bullet on the field by one tick; those that hit something end and leave the field. */
|
|
58
|
+
flyBullets(): void;
|
|
57
59
|
private splat;
|
|
58
60
|
private bulletStep;
|
|
59
61
|
/**
|
package/dist/simulation.js
CHANGED
|
@@ -109,12 +109,7 @@ export class PaintTanksSimulation {
|
|
|
109
109
|
if (g.boss) {
|
|
110
110
|
bossAI(this, g.boss);
|
|
111
111
|
}
|
|
112
|
-
|
|
113
|
-
if (!b.dead) {
|
|
114
|
-
this.bulletStep(b);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
g.bullets = g.bullets.filter(b => !b.dead);
|
|
112
|
+
this.flyBullets();
|
|
118
113
|
g.enemies = g.enemies.filter(e => !e.dead);
|
|
119
114
|
for (const k of g.picks) {
|
|
120
115
|
if (--k.life <= 0) {
|
|
@@ -373,6 +368,16 @@ export class PaintTanksSimulation {
|
|
|
373
368
|
this.game.bullets.push({ x: cx + (vx / m) * off, y: cy + (vy / m) * off, vx, vy, owner: 0, r: e.boss ? 5 : 4, ...shot });
|
|
374
369
|
this.sound('eshot');
|
|
375
370
|
}
|
|
371
|
+
/** Moves every bullet on the field by one tick; those that hit something end and leave the field. */
|
|
372
|
+
flyBullets() {
|
|
373
|
+
const g = this.game;
|
|
374
|
+
for (const b of g.bullets) {
|
|
375
|
+
if (!b.dead) {
|
|
376
|
+
this.bulletStep(b);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
g.bullets = g.bullets.filter(b => !b.dead);
|
|
380
|
+
}
|
|
376
381
|
splat(b, x, y) {
|
|
377
382
|
if (b.owner && b.kind) {
|
|
378
383
|
this.effects.splash(b.kind, x, y, PAINT[b.owner], 1);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@maco-online/paint-tanks-rules",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "The pure Paint Tanks rules (simulation, levels, AI, wire protocol, prediction) and its Maco room definition",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
},
|
|
12
12
|
"sideEffects": false,
|
|
13
13
|
"maco": {
|
|
14
|
-
"protocolVersion":
|
|
14
|
+
"protocolVersion": 4
|
|
15
15
|
},
|
|
16
16
|
"exports": {
|
|
17
17
|
".": {
|