Use the AI
Put the AI in your room
Make the room's account play with the AI: observation, network, zigzag-free decisions, zero-delay "eyes" and a complete "challenge the AI" room.
Here the account that hosts the room takes the field and plays with the AI. Everything runs in Node, alongside the room, without Python. The code is in the example project, in the src/ia/ folder.
The quickest way
- Download the example project and the model. Put
football-1x1.policy.jsonin the project folder. - In
.env:MODO=ia(and, if you want,IA_MODELO=path/to/model.json). npm run dev, join the room and challenge the AI.
Whoever joins goes into a queue; the AI plays blue against the first one in the queue. Whoever beats the AI stays; whoever loses goes to the back of the queue.
How it works, piece by piece
"eyes" ScoreTracker (no delay) ──'state' every 100 ms──► JogadorIA
│ buildObs() → 39 numbers
│ policy.logits() → 18 scores
│ decide() → action, no zigzag
│ actionToBits() → keys (mirrored if red)
▼
room.sendInput(keys) ──► bonk.io server ──► players
tracker.recordOwnInput() (the local simulation must know too)1. The observation: features.ts
This is the most delicate part: it must be identical to training. Don't touch it without checking against the test cases.
// features.ts
/**
* Observation and actions of the BonkTools football AI — exactly as it was trained.
*
* Any difference (a scale, a sign, a missing field handled differently) makes the AI play badly
* WITHOUT any error. Check your version against the test cases published with the model.
*
* Canonical axis: the bot always "attacks to the right". Playing red (attacks to the left), x and the
* x velocities are mirrored in the observation and left/right are swapped in the action.
*
* Observation versions (the policy says which one it uses in `obsVersion`/`obsSize`): v1 = 25 numbers; v2 = 39 (v1 + posts,
* walls, ball-goal alignment and ball velocity relative to the bot).
*/
export const TEAM_RED = 2;
export const TEAM_BLUE = 3;
const CENTER_X = 33.18;
const CENTER_Y = 22.73;
const HALF_W = 33.0;
const HALF_H = 22.7;
const GOAL_X = 64.0;
const OWN_GOAL_X = 2 * CENTER_X - GOAL_X;
const PLAYER_MAX_SPEED = 11.7;
const BALL_MAX_SPEED = 20.0;
export const LEFT = 1;
export const RIGHT = 2;
export const UP = 4;
export const DOWN = 8;
export const KICK = 16;
const DIRS = [0, UP, UP | RIGHT, RIGHT, DOWN | RIGHT, DOWN, DOWN | LEFT, LEFT, UP | LEFT];
/** 9 directions (incl. standing still) x {no kick, kick} — same order as in training. */
export const ACTION_TABLE: readonly number[] = [...DIRS, ...DIRS.map((d) => d | KICK)];
/** Observation size per version. 101 = the RL-trained goalkeeper's (goleiro-rl.ts). */
export const OBS_SIZES: Readonly<Record<number, number>> = { 1: 25, 2: 39, 101: 27 };
export const OBS_SIZE = OBS_SIZES[1]!;
// v2 geometry (canonical axis).
const POST_X = 64.09;
const POST_Y_TOP = CENTER_Y - 6.5;
const POST_Y_BOTTOM = CENTER_Y + 6.5;
const PLAYER_X_MIN = 1.0;
const PLAYER_X_MAX = 65.36;
const PLAYER_Y_MIN = 1.0;
const PLAYER_Y_MAX = 44.45;
const BALL_Y_MIN = 7.79;
const BALL_Y_MAX = 37.67;
/** Observation version from the network's input size (25 -> 1, 39 -> 2); null if none. */
export function versionForSize(size: number): number | null {
for (const [v, n] of Object.entries(OBS_SIZES)) if (n === size) return Number(v);
return null;
}
export function mirrorBits(bits: number): number {
let rest = bits & ~(LEFT | RIGHT);
if (bits & LEFT) rest |= RIGHT;
if (bits & RIGHT) rest |= LEFT;
return rest;
}
export function actionToBits(action: number, mirror: boolean): number {
const bits = ACTION_TABLE[action] ?? 0;
return mirror ? mirrorBits(bits) : bits;
}
/** The team attacks to the left (red) — must be mirrored to the canonical axis. */
export function teamMirror(team: number): boolean {
return team === TEAM_RED;
}
interface Body {
x: number;
y: number;
xv: number;
yv: number;
kickReady?: boolean;
}
const ZERO: Body = { x: CENTER_X, y: CENTER_Y, xv: 0, yv: 0, kickReady: false };
/** null/undefined and an EMPTY object count as missing (same as the Python training). */
function isBody(v: unknown): v is Body {
return !!v && typeof v === 'object' && Object.keys(v).length > 0;
}
/** ScoreTracker/PhysicsSimulator state (ball, discs[], ftu). Missing = still at the center of the field. */
export interface GameStateLike {
ball?: unknown;
discs?: unknown;
ftu?: unknown;
}
function ball(state: GameStateLike): Body {
return isBody(state.ball) ? state.ball : ZERO;
}
function disc(state: GameStateLike, id: number): Body {
const discs = Array.isArray(state.discs) ? state.discs : [];
const d: unknown = id >= 0 && id < discs.length ? discs[id] : null;
return isBody(d) ? d : ZERO;
}
function canon(b: Body, mirror: boolean): [number, number, number, number] {
return mirror ? [2 * CENTER_X - b.x, b.y, -b.xv, b.yv] : [b.x, b.y, b.xv, b.yv];
}
const clip = (v: number): number => Math.min(2, Math.max(-2, v));
/**
* Projects the state `leadFrames` frames (30/s) AHEAD: positions += velocity × time (no collisions) and the
* kickoff freeze clock (`ftu`) advanced.
*
* The room's local simulation deliberately runs 6 frames (200 ms) behind the clock, so late network keys can
* arrive (and the auto-score doesn't announce a goal a late key would undo). The bot was deciding about that past:
* in the simulator, with 200 ms of delay the AI lost EVERY kickoff to a striker who shoots first-time;
* with projection, it stopped conceding kickoff goals again.
*/
export function projectState(state: GameStateLike, leadFrames: number): GameStateLike {
if (!(leadFrames > 0)) return state;
const dt = leadFrames / 30;
const move = (v: unknown): unknown => {
if (!isBody(v)) return v;
const b = v as Body & Record<string, unknown>;
return { ...b, x: b.x + b.xv * dt, y: b.y + b.yv * dt };
};
const out: GameStateLike = { ...state, ball: move(state.ball) };
if (Array.isArray(state.discs)) out.discs = state.discs.map(move);
if (typeof state.ftu === 'number' && state.ftu > 0) out.ftu = Math.max(state.ftu - leadFrames, 0);
return out;
}
function unit(dx: number, dy: number): [number, number] {
const n = Math.hypot(dx, dy);
return n > 1e-6 ? [dx / n, dy / n] : [0, 0];
}
/** Observation (float32 as in NumPy) from `selfId`'s point of view, in version `version` (1 = 25, 2 = 39 numbers). */
export function buildObs(
state: GameStateLike,
selfId: number,
oppId: number,
mirror: boolean,
version = 1,
): Float32Array {
const v1 = buildObsV1(state, selfId, oppId, mirror);
if (version === 1) return v1;
if (version !== 2) throw new Error(`versão de observação desconhecida: ${version}`);
const out = new Float32Array(OBS_SIZES[2]!);
out.set(v1);
out.set(extraV2(state, selfId, mirror), v1.length);
return out;
}
function extraV2(state: GameStateLike, selfId: number, mirror: boolean): Float32Array {
const [sx, sy, sxv, syv] = canon(disc(state, selfId), mirror);
const [bx, by, bxv, byv] = canon(ball(state), mirror);
const ownX = 2 * CENTER_X - POST_X;
const [ax, ay] = unit(bx - sx, by - sy);
const [gx, gy] = unit(POST_X - bx, CENTER_Y - by);
const [dx, dy] = unit(sx - bx, sy - by);
const [ox, oy] = unit(ownX - bx, CENTER_Y - by);
const values = [
(POST_X - sx) / HALF_W, (POST_Y_TOP - sy) / HALF_H, (POST_Y_BOTTOM - sy) / HALF_H, (sx - ownX) / HALF_W,
(sx - PLAYER_X_MIN) / HALF_W, (PLAYER_X_MAX - sx) / HALF_W,
(sy - PLAYER_Y_MIN) / HALF_H, (PLAYER_Y_MAX - sy) / HALF_H,
(by - BALL_Y_MIN) / HALF_H, (BALL_Y_MAX - by) / HALF_H,
ax * gx + ay * gy, dx * ox + dy * oy,
(bxv - sxv) / BALL_MAX_SPEED, (byv - syv) / BALL_MAX_SPEED,
];
const out = new Float32Array(values.length);
for (let i = 0; i < values.length; i++) out[i] = clip(Math.fround(values[i]!));
return out;
}
function buildObsV1(state: GameStateLike, selfId: number, oppId: number, mirror: boolean): Float32Array {
const [sx, sy, sxv, syv] = canon(disc(state, selfId), mirror);
const [ox, oy, oxv, oyv] = canon(disc(state, oppId), mirror);
const [bx, by, bxv, byv] = canon(ball(state), mirror);
// Python: `state.get("ftu", -1) or 0` — missing becomes -1; 0/None become 0.
const rawFtu = state.ftu === undefined ? -1 : typeof state.ftu === 'number' ? state.ftu : 0;
const px = (x: number): number => (x - CENTER_X) / HALF_W;
const py = (y: number): number => (y - CENTER_Y) / HALF_H;
const dSelf = Math.hypot(bx - sx, by - sy);
const dOpp = Math.hypot(bx - ox, by - oy);
const values = [
px(sx), py(sy), sxv / PLAYER_MAX_SPEED, syv / PLAYER_MAX_SPEED,
px(bx), py(by), bxv / BALL_MAX_SPEED, byv / BALL_MAX_SPEED,
px(ox), py(oy), oxv / PLAYER_MAX_SPEED, oyv / PLAYER_MAX_SPEED,
(bx - sx) / HALF_W, (by - sy) / HALF_H,
(bx - ox) / HALF_W, (by - oy) / HALF_H,
(GOAL_X - bx) / HALF_W, (CENTER_Y - by) / HALF_H,
(bx - OWN_GOAL_X) / HALF_W, (by - CENTER_Y) / HALF_H,
dSelf / HALF_W, dOpp / HALF_W,
disc(state, selfId).kickReady ? 1 : 0,
disc(state, oppId).kickReady ? 1 : 0,
Math.min(Math.max(rawFtu, 0), 120) / 120,
];
// np.array(..., dtype=float32) rounds each value to float32 BEFORE the clip (which compares in float32).
const out = new Float32Array(OBS_SIZE);
for (let i = 0; i < OBS_SIZE; i++) out[i] = clip(Math.fround(values[i]!));
return out;
}2. The network and the decision: policy.ts
Reads policy.json, runs the three layers and decides axis by axis (see why in Download the model).
// policy.ts
import { readFileSync } from 'node:fs';
import { ACTION_TABLE, DOWN, KICK, LEFT, OBS_SIZES, RIGHT, UP, versionForSize } from './features.js';
/**
* The AI network (the published policy.json), running in plain JS — no Python or PyTorch on the room's machine. Small MLP: obs(25 in v1, 39 in v2 — `obsVersion`) -> 256 -> tanh -> 256 -> tanh -> 18 logits;
* the action is the argmax (the most likely move). Weights in base64 float32.
*/
export const POLICY_FORMAT = 'bonktools-rl-policy/1';
interface LayerJson {
in: number;
out: number;
w: string;
b: string;
}
export interface PolicyJson {
format: string;
obsSize: number;
/** Observation version (features.ts). Missing in old policies: derived from obsSize. */
obsVersion?: number;
actionTable: number[];
activation: string;
layers: LayerJson[];
timesteps?: number;
exportedAt?: string;
}
interface Layer {
inSize: number;
outSize: number;
/** out x in, row by row (same layout as PyTorch's `weight`). */
w: Float32Array;
b: Float32Array;
}
function floats(b64: string, expected: number, what: string): Float32Array {
const buf = Buffer.from(b64, 'base64');
if (buf.length !== expected * 4) throw new Error(`política inválida: ${what} tem ${buf.length / 4} valores, esperado ${expected}`);
// Copies into an aligned buffer (the base64 one can start at any offset).
const out = new Float32Array(expected);
for (let i = 0; i < expected; i++) out[i] = buf.readFloatLE(i * 4);
return out;
}
export class Policy {
readonly timesteps: number;
readonly exportedAt: string;
/** Observation version this network expects (pass it to `buildObs`). */
readonly obsVersion: number;
private readonly layers: Layer[];
constructor(json: PolicyJson) {
if (json.format !== POLICY_FORMAT) throw new Error(`formato de política desconhecido: ${String(json.format)}`);
if (json.activation !== 'tanh') throw new Error(`ativação não suportada: ${json.activation}`);
const version = json.obsVersion ?? versionForSize(json.obsSize);
if (version === null || OBS_SIZES[version] !== json.obsSize) {
throw new Error(`política espera ${json.obsSize} entradas (versão ${String(json.obsVersion ?? '?')}); a sala não conhece essa observação`);
}
this.obsVersion = version;
if (json.actionTable.length !== ACTION_TABLE.length || json.actionTable.some((v, i) => v !== ACTION_TABLE[i])) {
throw new Error('tabela de ações da política não bate com a da sala (features.ts)');
}
this.layers = json.layers.map((l, i) => ({
inSize: l.in,
outSize: l.out,
w: floats(l.w, l.in * l.out, `camada ${i} (pesos)`),
b: floats(l.b, l.out, `camada ${i} (bias)`),
}));
for (let i = 1; i < this.layers.length; i++) {
if (this.layers[i]!.inSize !== this.layers[i - 1]!.outSize) throw new Error(`política inválida: camada ${i} não encaixa na anterior`);
}
if (this.layers[0]?.inSize !== json.obsSize || this.layers.at(-1)?.outSize !== ACTION_TABLE.length) {
throw new Error('política inválida: entrada/saída não batem com a observação/ações');
}
this.timesteps = json.timesteps ?? 0;
this.exportedAt = json.exportedAt ?? '';
}
static fromFile(file: string): Policy {
return new Policy(JSON.parse(readFileSync(file, 'utf8')) as PolicyJson);
}
/** Logits for each action (tanh in the hidden layers, linear in the last). */
logits(obs: ArrayLike<number>): Float64Array {
let x: ArrayLike<number> = obs;
for (let li = 0; li < this.layers.length; li++) {
const { inSize, outSize, w, b } = this.layers[li]!;
const y = new Float64Array(outSize);
for (let o = 0; o < outSize; o++) {
let s = b[o]!;
const row = o * inSize;
for (let i = 0; i < inSize; i++) s += w[row + i]! * x[i]!;
y[o] = li < this.layers.length - 1 ? Math.tanh(s) : s;
}
x = y;
}
return x as Float64Array;
}
/** Deterministic action (index in ACTION_TABLE): plain argmax. Live play uses `decide` (no zigzag). */
act(obs: ArrayLike<number>): number {
const l = this.logits(obs);
let best = 0;
for (let i = 1; i < l.length; i++) if (l[i]! > l[best]!) best = i;
return best;
}
}
/** Margins for the axis-by-axis decision (measured in the simulator). */
export const AXIS_MARGIN = 0.25;
export const FLIP_MARGIN = 0.7;
function axis(pPos: number, pNeg: number, prev: number): number {
const diff = pPos - pNeg;
const needPos = prev < 0 ? FLIP_MARGIN : AXIS_MARGIN;
const needNeg = prev > 0 ? FLIP_MARGIN : AXIS_MARGIN;
if (diff > needPos) return 1;
if (-diff > needNeg) return -1;
return 0;
}
/**
* Decides the move AXIS BY AXIS from the logits
* The argmax of the 18 actions kept alternating nearly tied "diagonal up"/"diagonal down" on every decision: the bot
* zigzagged and never went straight (42 flips/min in real matches; humans, ~1). Here each axis
* comes from the summed probability of each side; a tie = no key on that axis (goes straight), and flipping the previous
* direction needs a bigger edge (hysteresis). Kick if P(kick) > 0.5.
*/
export function decide(logits: ArrayLike<number>, prevAction: number | null): number {
let max = -Infinity;
for (let i = 0; i < logits.length; i++) max = Math.max(max, logits[i]!);
const p = new Float64Array(logits.length);
let sum = 0;
for (let i = 0; i < logits.length; i++) sum += p[i] = Math.exp(logits[i]! - max);
let up = 0, down = 0, left = 0, right = 0, kick = 0;
for (let i = 0; i < p.length; i++) {
const q = p[i]! / sum;
const b = ACTION_TABLE[i]!;
if (b & UP) up += q;
if (b & DOWN) down += q;
if (b & LEFT) left += q;
if (b & RIGHT) right += q;
if (b & KICK) kick += q;
}
const pb = prevAction === null ? 0 : ACTION_TABLE[prevAction] ?? 0;
const v = axis(down, up, pb & UP ? -1 : pb & DOWN ? 1 : 0); // +1 = down (y grows downward)
const h = axis(right, left, pb & RIGHT ? 1 : pb & LEFT ? -1 : 0);
let bits = (v > 0 ? DOWN : v < 0 ? UP : 0) | (h > 0 ? RIGHT : h < 0 ? LEFT : 0);
if (kick > 0.5) bits |= KICK;
return ACTION_TABLE.indexOf(bits);
}3. The player: jogador-ia.ts
// jogador-ia.ts
import type { BonkRoom, ScoreTracker, StateInfo } from 'bonktools';
import { actionToBits, buildObs, projectState, teamMirror } from './features.js';
import { decide, type Policy } from './policy.js';
/**
* Makes the bot (the account hosting the room) PLAY with the AI: on every new simulation state (100 ms),
* it builds the observation, runs the network and presses the keys.
*/
export class JogadorIA {
private partida: { eu: number; adversario: number; espelho: boolean } | null = null;
private ultimasTeclas = -1; // last combination sent (we only send when it changes, like a real keyboard)
private ultimaAcao: number | null = null; // the axis-by-axis decision compares with the previous one (avoids zigzag)
constructor(
private readonly room: BonkRoom,
/** The room's ScoreTracker (score): the bot's own key must be fed into it. */
private readonly placar: ScoreTracker,
private readonly policy: Policy,
/**
* Zero-delay "eyes" (optional): a 2nd ScoreTracker with lagFrames 0. Without it, the AI sees the state
* of the score tracker, which is 200 ms in the past, and we project that state to the present.
*/
private readonly olhos?: ScoreTracker,
/** Reports every key sent (the replay recorder needs it). */
private readonly aoTeclar?: (id: number, k: { i: number; f: number; c: number }) => void,
) {
(olhos ?? placar).on('state', this.aoEstado);
}
/** `time` (team): 3 = blue (attacks to the right), 2 = red (the AI mirrors the field). */
comecar(eu: number, adversario: number, time: number): void {
this.partida = { eu, adversario, espelho: teamMirror(time) };
this.ultimasTeclas = -1;
this.ultimaAcao = null;
}
parar(): void {
this.partida = null;
this.enviar(0); // releases every key
}
private readonly aoEstado = ({ state }: StateInfo): void => {
const p = this.partida;
if (!p || !state || typeof state !== 'object') return;
// without "eyes", the state is 6 frames (200 ms) behind: project it to now
const visto = this.olhos ? state : projectState(state, 6);
const obs = buildObs(visto, p.eu, p.adversario, p.espelho, this.policy.obsVersion);
const acao = decide(this.policy.logits(obs), this.ultimaAcao);
this.ultimaAcao = acao;
this.enviar(actionToBits(acao, p.espelho));
};
private enviar(teclas: number): void {
if (teclas === this.ultimasTeclas || this.partida === null && teclas !== 0) return;
this.ultimasTeclas = teclas;
const eu = this.partida?.eu ?? this.room.state.myId;
if (eu === null) return;
const k = this.room.sendInput(teclas);
// The server doesn't echo back to the host the key it sent itself: without this, the local simulation
// would think the bot is standing still and the AI would "see" a different game from the real one.
this.placar.recordOwnInput(eu, k.i, k.f, k.c);
this.olhos?.recordOwnInput(eu, k.i, k.f, k.c);
this.aoTeclar?.(eu, k);
}
destroy(): void {
(this.olhos ?? this.placar).off('state', this.aoEstado);
this.partida = null;
}
}Three details that make all the difference:
recordOwnInput. The server doesn't send back to the host the key it sent itself. Without recording it in the local simulation, the simulation would think the bot is standing still and the AI would "see" a game different from the real one.- Only sends a key when it changes, like a real keyboard. Sending every 100 ms bloats the history that people joining mid-match receive.
- Releases everything at the end (
parar(), "stop", sends 0); otherwise the bot's disc keeps moving on its own in the next match.
4. The "eyes": why two ScoreTrackers
The score ScoreTracker deliberately runs 200 ms behind the clock: it waits for late network keys so it never announces a goal that didn't happen. Great for the score, terrible for playing: the AI would be deciding about the past.
That's why the "challenge the AI" room creates a second ScoreTracker with lagFrames: 0: the "eyes". It simulates up to the present instant and, when a late key arrives, goes back and redoes that stretch.
| Room setup | Train with | Measured result (100 plays against the strongest script) |
|---|---|---|
With "eyes" (lagFrames: 0), no projection | --visao agora (default) | 95 goals, 2 conceded |
Score tracker only (200 ms behind) + projectState(state, 6) | --visao atrasada | 68 goals, 13 conceded |
5. The "challenge the AI" room: sala-ia.ts
// sala-ia.ts
import { ScoreTracker, TEAM_BLUE, TEAM_RED, TEAM_SPEC, type BonkRoom } from 'bonktools';
import { falar } from '../chat.js';
import { JogadorIA } from './jogador-ia.js';
import { Policy } from './policy.js';
/** "Challenge the AI" room: the AI plays blue and each player in the queue plays against it. The winner stays. */
export class SalaIA {
private readonly placar: ScoreTracker;
private readonly olhos: ScoreTracker;
private readonly ia: JogadorIA;
private fila: number[] = [];
private adversario: number | null = null;
private timer: NodeJS.Timeout | null = null;
constructor(private readonly room: BonkRoom, arquivoModelo = 'football-1x1.policy.json') {
this.placar = new ScoreTracker(room, { cacheDir: '.cache/bonk-client' });
this.olhos = new ScoreTracker(room, { cacheDir: '.cache/bonk-client', lagFrames: 0 });
this.ia = new JogadorIA(room, this.placar, Policy.fromFile(arquivoModelo), this.olhos);
room.on('player-join', (p) => {
this.fila.push(p.id);
falar(room, `${p.userName}, você está na fila para desafiar a IA (posição ${this.fila.length})`);
this.proxima();
});
room.on('player-leave', (p) => {
this.fila = this.fila.filter((id) => id !== p.id);
if (p.id === this.adversario) this.encerrar(null);
});
this.placar.on('score', ({ scores }) => falar(room, `IA ${scores[TEAM_BLUE]} x ${scores[TEAM_RED]} desafiante`));
this.placar.on('match-winner', ({ team }) => {
falar(room, team === TEAM_BLUE ? 'a IA venceu!' : 'você venceu a IA!');
this.timer = setTimeout(() => this.encerrar(team), 4_000); // after the victory screen
});
void this.placar.start();
void this.olhos.start();
room.lockTeams();
}
private proxima(): void {
const eu = this.room.state.myId;
if (this.adversario !== null || !this.fila.length || eu === null) return;
const adv = this.fila[0]!;
this.adversario = adv;
for (const id of this.room.state.players.keys()) {
this.room.setTeam(id, id === eu ? TEAM_BLUE : id === adv ? TEAM_RED : TEAM_SPEC);
}
setTimeout(async () => {
const escalacao: Array<{ id: number; team: number } | null> = [];
escalacao[eu] = { id: eu, team: TEAM_BLUE };
escalacao[adv] = { id: adv, team: TEAM_RED };
const is = await this.placar.buildInitialState(escalacao);
if (!is || this.adversario !== adv) return void this.encerrar(null);
this.room.startGame({ is });
this.ia.comecar(eu, adv, TEAM_BLUE);
}, 1_000); // gives the team changes time to reach the server
}
/** End of the match. `vencedor` (winner) null = cancelled. Whoever lost to the AI goes to the back of the queue. */
private encerrar(vencedor: number | null): void {
const adv = this.adversario;
this.adversario = null;
this.ia.parar();
this.room.stopGame();
this.room.emit('game-end', { type: 'GAME_END' });
if (adv !== null && vencedor === TEAM_BLUE && this.fila.includes(adv)) {
this.fila = [...this.fila.filter((id) => id !== adv), adv];
}
setTimeout(() => this.proxima(), 2_000);
}
destroy(): void {
if (this.timer) clearTimeout(this.timer);
this.ia.destroy();
this.placar.stop();
this.olhos.stop();
}
}To take the field, the bot puts itself on a team (room.setTeam(eu, TEAM_BLUE)) and the initial state is built with it in the lineup, like any player.
Recording the AI's matches
If you use the recorder too, pass the bot's keys to it (peer-input doesn't carry the host's own keys):
const gravador = new Gravador(room, placar);
const ia = new JogadorIA(room, placar, policy, olhos, (id, tecla) => gravador.teclaPropria(id, tecla));Swapping the model without restarting the room
In the BonkTools rooms the model was swapped with the room full: every minute the bot checked the file's date and, if it changed, loaded the new one (keeping the old one if the new file was broken). It's a good idea if you train and update often:
let mtime = 0;
setInterval(() => {
const m = statSync(arquivo).mtimeMs;
if (m === mtime) return;
try { policy = Policy.fromFile(arquivo); mtime = m; } catch { /* invalid file: keep the current one */ }
}, 60_000);Ideas
- Adjustable difficulty: with probability p, replace the decision with a random action or hold the previous key for one more tick. That's how the training's scripted opponent gets its "skill".
- 2v2 with AI: two
JogadorIAdon't work on the same account (one account = one player). For two bots, connect a second account withjoinRoom()and give it its ownJogadorIA. The other models (goalkeeper, mbappa) are in Download the models. - Train with your room's matches: record the matches against humans and use the real plays as training scenarios. That's what helped our AI defend the most (see What we learned).