Skip to content

Computer players (AI) ​

A computer player sits in a seat like a person. It sees only what that seat sees (its own fog of war), and it can only command its own units. What plays the seat is a brain: the engine ships one for melee games, one for missions and one for lanes, and you can tune them or write your own.

Quick start ​

ts
import { meleeBrain } from '@v12-engine/engine/ai'

export const rules: Rules = { ...myRules, brains: [meleeBrain] } // the brains your game can seat
world.computers.seat(2, { enemyBases: [theirStart], difficulty: 'normal' }) // in your scene's start

Player 2 now gathers, builds, trains an army and attacks the base at that start. A seat that names no brain gets the first one in Rules.brains. The seat is kept in the world, so a save brings the computer back with its memory.

Common tasks ​

Seat a computer for every computer player of a map ​

ts
const starts = (map?.players ?? []).filter((player) => player.start)
for (const player of starts) {
	if (player.controller !== 'computer') continue
	const enemyBases = starts.filter((other) => other.team !== player.team).map((other) => other.start!)
	world.computers.seat(player.id, { enemyBases, difficulty: 'normal' })
}

Make it harder or easier ​

ts
world.computers.seat(2, { difficulty: 'hard', personality: 'rusher' })

Difficulties: easy, normal, hard, insane. Personalities: balanced, rusher, turtle, expander, harasser. See Difficulty and personality.

Change when it attacks ​

ts
world.computers.seat(2, { settings: { army: { firstAttack: 300, waveSize: 6, attackEvery: 120 } } })

See Attack waves to script the waves yourself.

Give it a build order ​

ts
world.computers.seat(2, { settings: { plans: { order: ['farm', 'barracks', { type: 'footman', count: 4 }] } } })

See Build plans.

Tell it what to do from a mission script ​

ts
world.computers.tell(ENEMY, 'attack', { at: 'the ford' }) // also 'defend', 'retreat', 'hold', 'pause', 'resume'

See Script a brain.

Pause, resume or remove a computer ​

ts
world.computers.pause(ENEMY) // sits still, kept in a save
world.computers.resume(ENEMY)
world.computers.unseat(ENEMY)

A camp that only holds its ground (missions) ​

ts
import { holdPosition, missionBrain, waveFrom } from '@v12-engine/engine/ai'

export const redHand = missionBrain({
	id: 'campaign.redHand',
	modules: [holdPosition('camp hall', { radius: 14 }), waveFrom('war pit', { group: 3, every: 90 })]
})
world.computers.seat(ENEMY, { brain: redHand }) // and list redHand in Rules.brains

See Script a brain: missions.

Computer heroes in a game of lanes ​

ts
import { lanesBrain } from '@v12-engine/engine/ai'
world.computers.seat(2, { brain: lanesBrain })

See Lanes (MOBA).

Computers on a server, or in a balance test ​

A server seats computers in the open seats with the brain it is started with (startServer({ computer: myBrain })), or one per seat (GameMode.brain, mapMode(game, map, { brain })), as hard as the lobby chose (GameMode.ai). simulate takes a brain per player (Simulation.brains). See Online and Testing your rules.

See what it is thinking ​

Open the debug panel (🐞 or Alt+Shift+D), pick the computer's player under Entities and open the AI tab: its goals as bars, its decisions, squads, plans and what it knows. See Script a brain: debugging.

The pages ​

PageWhat it covers
Difficulty and personalityDifficulties, personalities, settings by layer, the classic flags, races, the lobby
Build plansBuild orders, wants ("I want gryphons"), placement, the army mix, workers, defence
Attack wavesThe melee army's attacks, scripted waves, scouting, team play
Script a brainRules and words, mission brains, modules, goals, squads, micro, debugging, tests
AI for missions and campaignsEnemy bases on a schedule, camps, patrols, ambushes, bosses, allies, a campaign's difficulty
ReferenceHow a brain thinks: the view, knowledge, strength, the picker, the melee and lanes brains

Reference ​

world.computers ​

CallWhat it does
seat(player, request)Seats a computer (SeatRequest: brain or its id, plus AiOptions), kept in the world (core.computers)
unseat(player)Takes it out of the world and its saves
get(player)The seat: its computer, session and timing()
list()Who is seated
pause(player), resume(player)Sits still, or plays again (kept in a save)
tell(player, word, data)Says a word to its brain ('attack', 'raid'…), taken by a module with ai.takeTold
defaults{ difficulty, personality } every later seat takes where its request says nothing

Rules worth knowing ​

  • Fair play is built in. Nothing in the AI kit takes a World. A brain reads its seat's snapshot and sends commands; the world refuses commands for units it does not own.
  • List your brains. A save remembers a seat by its brain's id, so list every brain you seat in Rules.brains. The engine's brains live in @v12-engine/engine/ai and are only known where listed. Seating a computer when the game lists no brain throws.
  • Options in a save. Data options (enemyBases, difficulty, settings, a build order) are kept. Functions given as options (goals, roles, strength, a flag as a function) are dropped from the save (logged at debug); put them in the brain's definition instead.
  • Loading. Computers are seated again before the game's afterLoad. A brain id the game no longer lists warns and leaves the seat empty.
  • Moving old saves over. A game that kept its computers in a component of its own moves them with a Rules.saves step that writes each seat into core.computers as { brain?, options } (the example's melee: apps/example/src/games/melee/saves.ts).
  • Same seed, same game. Brains draw from ai.rng (seeded from the world's seed and the player), never Math.random or the clock.