@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.
Files changed (55) hide show
  1. package/LICENSE +21 -0
  2. package/dist/ai-host.d.ts +15 -0
  3. package/dist/ai-host.js +1 -0
  4. package/dist/balance-config.d.ts +50 -0
  5. package/dist/balance-config.js +41 -0
  6. package/dist/boss-ai.d.ts +9 -0
  7. package/dist/boss-ai.js +213 -0
  8. package/dist/bosses.d.ts +34 -0
  9. package/dist/bosses.js +47 -0
  10. package/dist/constants.d.ts +133 -0
  11. package/dist/constants.js +163 -0
  12. package/dist/enemy-ai.d.ts +11 -0
  13. package/dist/enemy-ai.js +361 -0
  14. package/dist/game-factory.d.ts +16 -0
  15. package/dist/game-factory.js +78 -0
  16. package/dist/game-state.d.ts +256 -0
  17. package/dist/game-state.js +1 -0
  18. package/dist/index.d.ts +25 -0
  19. package/dist/index.js +27 -0
  20. package/dist/labels.d.ts +12 -0
  21. package/dist/labels.js +129 -0
  22. package/dist/levels.d.ts +45 -0
  23. package/dist/levels.js +209 -0
  24. package/dist/movement.d.ts +25 -0
  25. package/dist/movement.js +158 -0
  26. package/dist/online/match.d.ts +28 -0
  27. package/dist/online/match.js +176 -0
  28. package/dist/online/prediction.d.ts +41 -0
  29. package/dist/online/prediction.js +97 -0
  30. package/dist/online/protocol.d.ts +124 -0
  31. package/dist/online/protocol.js +54 -0
  32. package/dist/online/room-game.d.ts +5 -0
  33. package/dist/online/room-game.js +24 -0
  34. package/dist/online/view.d.ts +43 -0
  35. package/dist/online/view.js +347 -0
  36. package/dist/pathfinding.d.ts +8 -0
  37. package/dist/pathfinding.js +45 -0
  38. package/dist/power-ups.d.ts +28 -0
  39. package/dist/power-ups.js +66 -0
  40. package/dist/random.d.ts +1 -0
  41. package/dist/random.js +1 -0
  42. package/dist/sim-effects.d.ts +30 -0
  43. package/dist/sim-effects.js +1 -0
  44. package/dist/simulation.d.ts +74 -0
  45. package/dist/simulation.js +768 -0
  46. package/dist/sound-names.d.ts +4 -0
  47. package/dist/sound-names.js +1 -0
  48. package/dist/spawning.d.ts +3 -0
  49. package/dist/spawning.js +56 -0
  50. package/dist/tank-rules.d.ts +20 -0
  51. package/dist/tank-rules.js +39 -0
  52. package/dist/visibility.d.ts +20 -0
  53. package/dist/visibility.js +39 -0
  54. package/package.json +40 -4
  55. package/README.md +0 -3
@@ -0,0 +1,97 @@
1
+ import { move, turn } from "../movement.js";
2
+ /** Predicted positions kept, one per input, to compare with the server at the same input. */
3
+ const MAX_HISTORY = 240;
4
+ /** Below this the server is only rounding (it sends tenths of a pixel). */
5
+ const DEADBAND = 0.5;
6
+ /** Further than this is a respawn or a teleport: jump straight there instead of shifting the prediction. */
7
+ const SNAP_DISTANCE = 40;
8
+ /**
9
+ * Drives the member's own tank the moment a key is pressed and reports where it ended up; the server adopts any
10
+ * position that obeys the rules. Each report is numbered, and every state from the server says which number it
11
+ * had applied, so the prediction is compared with the server at the same input, never with where the tank is now.
12
+ * A real difference (the server refused a move) shifts the tank and every later prediction once; states that were
13
+ * already on their way are then ignored until one reflects the corrected path, so a correction is never applied
14
+ * twice.
15
+ */
16
+ export class OwnTankPredictor {
17
+ /** Corrections made since the level started, and the size of the last one, for the network panel. */
18
+ corrections = 0;
19
+ lastCorrection = 0;
20
+ seq = 0;
21
+ history = [];
22
+ /** States acknowledging inputs up to this number predate the last correction. */
23
+ settledAfter = 0;
24
+ /** Starts numbering again, for a new connection: the room forgets the old numbers when it welcomes it. */
25
+ reset() {
26
+ this.seq = 0;
27
+ this.newLevel();
28
+ }
29
+ /** Forgets the previous level's predictions; the numbering goes on, as the room still remembers it. */
30
+ newLevel() {
31
+ this.history = [];
32
+ this.settledAfter = this.seq;
33
+ this.corrections = 0;
34
+ this.lastCorrection = 0;
35
+ }
36
+ /** Moves `me` by one tick of `input`, exactly as the server would, and returns the claim to send. */
37
+ advance(g, me, input) {
38
+ this.seq++;
39
+ if (me.alive && me.respawn === 0 && input.dir >= 0) {
40
+ turn(g, me, input.dir, false);
41
+ if (move(g, me, me.speed)) {
42
+ me.anim += me.speed;
43
+ }
44
+ }
45
+ const claim = { seq: this.seq, x: me.x, y: me.y };
46
+ this.history.push(claim);
47
+ if (this.history.length > MAX_HISTORY) {
48
+ this.history.shift();
49
+ }
50
+ return claim;
51
+ }
52
+ /**
53
+ * Takes in where the server had the tank after input `ack`; `active` is false while it is dead or respawning.
54
+ * Returns how the tank was moved: by a small `shift` the caller may fade in on screen, or a `jump` to show as is.
55
+ */
56
+ observe(me, x, y, ack, active) {
57
+ if (!active) {
58
+ // Nothing to predict: show the tank where the server has it.
59
+ me.x = x;
60
+ me.y = y;
61
+ this.history = [];
62
+ this.settledAfter = this.seq;
63
+ return { kind: 'jump' };
64
+ }
65
+ if (ack <= this.settledAfter) {
66
+ return null;
67
+ }
68
+ const at = this.history.find(h => h.seq === ack);
69
+ if (!at) {
70
+ return null;
71
+ }
72
+ const ex = x - at.x;
73
+ const ey = y - at.y;
74
+ const error = Math.hypot(ex, ey);
75
+ if (error <= DEADBAND) {
76
+ return null;
77
+ }
78
+ this.corrections++;
79
+ this.lastCorrection = error;
80
+ this.settledAfter = this.seq;
81
+ if (error > SNAP_DISTANCE) {
82
+ me.x = x;
83
+ me.y = y;
84
+ this.history = [];
85
+ return { kind: 'jump' };
86
+ }
87
+ me.x += ex;
88
+ me.y += ey;
89
+ for (const h of this.history) {
90
+ if (h.seq > ack) {
91
+ h.x += ex;
92
+ h.y += ey;
93
+ }
94
+ }
95
+ return { kind: 'shift', x: ex, y: ey };
96
+ }
97
+ }
@@ -0,0 +1,124 @@
1
+ import type { BossId, BossShot } from '../bosses.ts';
2
+ import type { Banner } from '../game-state.ts';
3
+ import type { FloatingLabel } from '../sim-effects.ts';
4
+ import type { SimOutcome } from '../simulation.ts';
5
+ import type { SoundName } from '../sound-names.ts';
6
+ import { z } from 'zod';
7
+ /**
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 20 times a second.
10
+ */
11
+ export declare const PAINT_TANKS_ONLINE_GAME = "paint-tanks";
12
+ export declare const PAINT_TANKS_TEAM_SIZE = 5;
13
+ export declare const PAINT_TANKS_TICK_RATE = 60;
14
+ /** A state every 2 ticks: 30 a second, so a late packet rarely leaves a visible gap. */
15
+ export declare const PAINT_TANKS_STATE_EVERY = 2;
16
+ /** Tanks by their number on the wire: append new tanks at the end and never reorder, so the numbers stay put. */
17
+ 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
+ /** Enemies by their number on the wire; append only, as for `TANK_KINDS`. */
19
+ export declare const ENEMY_KINDS: readonly ["dust", "racer", "box", "sniper", "bomb", "rainbow"];
20
+ export declare const PICKUP_TYPES: readonly ["star", "fruit", "up", "shield", "cloak", "freeze", "bone"];
21
+ export declare const PARTICLE_TYPES: readonly ["dot", "bubble", "star", "line", "sq", "ring"];
22
+ export declare const paintTanksInputSchema: z.ZodObject<{
23
+ /** Direction the tank faces after this input: 0 up, 1 right, 2 down, 3 left; -1 leaves it as it is. */
24
+ d: z.ZodInt;
25
+ /** Whether fire was pressed during this input. */
26
+ f: z.ZodBoolean;
27
+ /** The position the browser claims its tank is at after applying this input. */
28
+ x: z.ZodNumber;
29
+ y: z.ZodNumber;
30
+ /** The browser's input number, one per browser tick. */
31
+ n: z.ZodInt;
32
+ }, z.core.$strict>;
33
+ export type PaintTanksInput = z.infer<typeof paintTanksInputSchema>;
34
+ export declare const paintTanksSettingsSchema: z.ZodObject<{
35
+ level: z.ZodInt;
36
+ }, z.core.$strict>;
37
+ export type PaintTanksSettings = z.infer<typeof paintTanksSettingsSchema>;
38
+ export declare const paintTanksProfileSchema: z.ZodObject<{
39
+ tank: z.ZodEnum<{
40
+ ducky: "ducky";
41
+ bunny: "bunny";
42
+ robo: "robo";
43
+ dino: "dino";
44
+ bee: "bee";
45
+ crab: "crab";
46
+ firetruck: "firetruck";
47
+ penguin: "penguin";
48
+ turtle: "turtle";
49
+ frog: "frog";
50
+ unicorn: "unicorn";
51
+ swordfish: "swordfish";
52
+ octopus: "octopus";
53
+ giraffe: "giraffe";
54
+ elephant: "elephant";
55
+ mole: "mole";
56
+ cat: "cat";
57
+ bear: "bear";
58
+ piggy: "piggy";
59
+ }>;
60
+ }, z.core.$strict>;
61
+ export type PaintTanksProfile = z.infer<typeof paintTanksProfileSchema>;
62
+ export type WirePlayer = [
63
+ slot: number,
64
+ kind: number,
65
+ x: number,
66
+ y: number,
67
+ dir: number,
68
+ anim: number,
69
+ alive: 0 | 1,
70
+ respawn: number,
71
+ shield: number,
72
+ cloak: number,
73
+ up: number,
74
+ lives: number
75
+ ];
76
+ /** `flags`: 1 angry, 2 carrier, 4 about to blow, 8 slowed by a shot. */
77
+ export type WireEnemy = [id: number, kind: number, x: number, y: number, dir: number, anim: number, spawning: number, flash: number, flags: number];
78
+ /** `flags`: 1 shielded. `plan` is only sent while the boss shows its next volley. */
79
+ export type WireBoss = [
80
+ id: number,
81
+ boss: BossId,
82
+ x: number,
83
+ y: number,
84
+ dir: number,
85
+ anim: number,
86
+ hp: number,
87
+ maxHp: number,
88
+ flash: number,
89
+ inv: number,
90
+ flags: number,
91
+ charge: number,
92
+ plan: BossShot[]
93
+ ];
94
+ /** `kind` is -1 for an enemy shot; `flags`: 1 big, 2 power, 4 rainbow enemy shot. */
95
+ export type WireBullet = [id: number, x: number, y: number, vx: number, vy: number, r: number, owner: number, kind: number, flags: number];
96
+ export type WirePickup = [x: number, y: number, type: number, life: number];
97
+ export type WireEvent = ['s', SoundName] | ['b', number, number, string, number, number] | ['f', number, number, number, string, number] | ['t', number, number, string, FloatingLabel];
98
+ export interface PaintTanksState {
99
+ lv: number;
100
+ /** Frames since the level started; animations follow it. */
101
+ t: number;
102
+ over: boolean;
103
+ /** Tiles, one digit per cell by row; sent when they change and now and then for whoever just joined. */
104
+ map?: string;
105
+ /** Hits each tile can still take, one digit per cell, sent with `map`. */
106
+ hp?: string;
107
+ players: WirePlayer[];
108
+ enemies: WireEnemy[];
109
+ boss: WireBoss | null;
110
+ bullets: WireBullet[];
111
+ pickups: WirePickup[];
112
+ petHp: number;
113
+ petFlash: number;
114
+ freeze: number;
115
+ shake: number;
116
+ score: number;
117
+ remaining: number;
118
+ bossT: number | null;
119
+ banner: Banner | null;
120
+ /** Effects since the previous state, for particles and sound. */
121
+ events: WireEvent[];
122
+ /** How the level ended; set in the last state of a match. */
123
+ outcome: SimOutcome | null;
124
+ }
@@ -0,0 +1,54 @@
1
+ import { z } from 'zod';
2
+ import { MAX_TEAM, MAXL } from "../constants.js";
3
+ /**
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 20 times a second.
6
+ */
7
+ export const PAINT_TANKS_ONLINE_GAME = 'paint-tanks';
8
+ export const PAINT_TANKS_TEAM_SIZE = MAX_TEAM;
9
+ export const PAINT_TANKS_TICK_RATE = 60;
10
+ /** A state every 2 ticks: 30 a second, so a late packet rarely leaves a visible gap. */
11
+ export const PAINT_TANKS_STATE_EVERY = 2;
12
+ /** Tanks by their number on the wire: append new tanks at the end and never reorder, so the numbers stay put. */
13
+ export const TANK_KINDS = [
14
+ 'ducky',
15
+ 'bunny',
16
+ 'robo',
17
+ 'dino',
18
+ 'bee',
19
+ 'crab',
20
+ 'firetruck',
21
+ 'penguin',
22
+ 'turtle',
23
+ 'frog',
24
+ 'unicorn',
25
+ 'swordfish',
26
+ 'octopus',
27
+ 'giraffe',
28
+ 'elephant',
29
+ 'mole',
30
+ 'cat',
31
+ 'bear',
32
+ 'piggy'
33
+ ];
34
+ /** Enemies by their number on the wire; append only, as for `TANK_KINDS`. */
35
+ export const ENEMY_KINDS = ['dust', 'racer', 'box', 'sniper', 'bomb', 'rainbow'];
36
+ export const PICKUP_TYPES = ['star', 'fruit', 'up', 'shield', 'cloak', 'freeze', 'bone'];
37
+ export const PARTICLE_TYPES = ['dot', 'bubble', 'star', 'line', 'sq', 'ring'];
38
+ export const paintTanksInputSchema = z.strictObject({
39
+ /** Direction the tank faces after this input: 0 up, 1 right, 2 down, 3 left; -1 leaves it as it is. */
40
+ d: z.int().min(-1).max(3),
41
+ /** Whether fire was pressed during this input. */
42
+ f: z.boolean(),
43
+ /** The position the browser claims its tank is at after applying this input. */
44
+ x: z.number().min(-1000).max(1000),
45
+ y: z.number().min(-1000).max(1000),
46
+ /** The browser's input number, one per browser tick. */
47
+ n: z.int().nonnegative()
48
+ });
49
+ export const paintTanksSettingsSchema = z.strictObject({
50
+ level: z.int().min(1).max(MAXL)
51
+ });
52
+ export const paintTanksProfileSchema = z.strictObject({
53
+ tank: z.enum(TANK_KINDS)
54
+ });
@@ -0,0 +1,5 @@
1
+ import type { RoomGameDefinition } from '@maco-online/online-core';
2
+ import type { PaintTanksInput, PaintTanksProfile, PaintTanksSettings, PaintTanksState } from './protocol.ts';
3
+ import { PaintTanksMatch } from './match.ts';
4
+ /** Paint Tanks as the room server hosts it: up to five players on one team against the dust tanks. */
5
+ export declare const paintTanksRoomGame: RoomGameDefinition<PaintTanksInput, PaintTanksSettings, PaintTanksProfile, PaintTanksState, PaintTanksMatch>;
@@ -0,0 +1,24 @@
1
+ import { PaintTanksMatch } from "./match.js";
2
+ import { PAINT_TANKS_ONLINE_GAME, PAINT_TANKS_STATE_EVERY, PAINT_TANKS_TEAM_SIZE, PAINT_TANKS_TICK_RATE, paintTanksInputSchema, paintTanksProfileSchema, paintTanksSettingsSchema } from "./protocol.js";
3
+ /** Paint Tanks as the room server hosts it: up to five players on one team against the dust tanks. */
4
+ export const paintTanksRoomGame = {
5
+ id: PAINT_TANKS_ONLINE_GAME,
6
+ protocolVersion: 2,
7
+ maxMembers: PAINT_TANKS_TEAM_SIZE,
8
+ tickRate: PAINT_TANKS_TICK_RATE,
9
+ stateEvery: PAINT_TANKS_STATE_EVERY,
10
+ // No finite position is claimed, so a member who sends nothing holds still without resetting their input sequence.
11
+ idleInput: { d: -1, f: false, x: Number.NaN, y: Number.NaN, n: 0 },
12
+ // A tap of fire carried by an input that the newest one overtook within the same tick still fires.
13
+ mergeInput: (pending, next) => ({ ...next, f: pending.f || next.f }),
14
+ // Each browser drives its own tank and claims where it ended up, one claim per tick. Played as they arrive, claims
15
+ // bunched by a wobbly link moved the tank in starts and stops on teammates' screens; queued, it moves as it was
16
+ // driven, at the cost of up to six ticks (100 ms) of delay on a bad link and one on a steady one.
17
+ inputPlayout: { maxTicks: 6 },
18
+ inputSchema: paintTanksInputSchema,
19
+ settingsSchema: paintTanksSettingsSchema,
20
+ profileSchema: paintTanksProfileSchema,
21
+ defaultSettings: () => ({ level: 1 }),
22
+ defaultProfile: () => ({ tank: 'ducky' }),
23
+ createMatch: (settings, members, previous) => new PaintTanksMatch(settings, members, previous)
24
+ };
@@ -0,0 +1,43 @@
1
+ import type { TimelineSample } from '@maco-online/online-core';
2
+ import type { Game, Player } from '../game-state.ts';
3
+ import type { PaintTanksState, WirePlayer } from './protocol.ts';
4
+ /** A state the room sent, with the tick it describes. */
5
+ export interface TimedState {
6
+ tick: number;
7
+ state: PaintTanksState;
8
+ }
9
+ /**
10
+ * The field of an online level as this browser draws it, rebuilt from the states the room sends. Every tank and
11
+ * bullet is blended between the two states around the moment drawn; the member's own tank is left to the
12
+ * prediction, which runs ahead of the room so it answers the controls at once.
13
+ */
14
+ export declare class OnlineView {
15
+ readonly game: Game;
16
+ remaining: number;
17
+ private readonly players;
18
+ private enemies;
19
+ private bullets;
20
+ private boss;
21
+ /** Velocity of each other tank, in pixels per tick, from its last two states, for extrapolating over a late state. */
22
+ private readonly enemyVel;
23
+ private readonly playerVel;
24
+ private bossVel;
25
+ constructor(lv: number);
26
+ /** The tank of the player in `slot`, created on first sight; the same object for the whole level. */
27
+ playerFor(slot: number, wire: WirePlayer, order: number): Player;
28
+ /** The team slot of a tank on the field, or null for one no longer in the level. */
29
+ slotOf(player: Player): number | null;
30
+ /** Takes the tiles of `state`, if it carries them; called when the moment drawn reaches that state. */
31
+ applyTiles(state: PaintTanksState): void;
32
+ /**
33
+ * Runs `drive` with every other tank where the room will find it when it plays this browser's input, rather than
34
+ * where it is drawn, a few ticks in the past: at its position in the `newest` state, carried on at the speed it
35
+ * had since the state before it for `lead` ticks. Teammates, computer tanks and the boss all count, so the own
36
+ * tank, predicted at the present, stops short of a spot another tank is about to take instead of driving into it
37
+ * and being pulled back out.
38
+ */
39
+ withNewestTanks<T>(newest: TimedState | null, previous: TimedState | null, lead: number, ownSlot: number | null, drive: () => T): T;
40
+ /** Shows the moment `sample` points at; the tank in `ownSlot` keeps the position the prediction gave it. */
41
+ apply(sample: TimelineSample<PaintTanksState>, ownSlot: number | null): void;
42
+ private bossFrom;
43
+ }