Add a computer player
Mark a map's player as a computer and seat a brain at it as the game starts. The engine's melee brain plays from your rules alone; write your own with defineBrain.
Let the computer play my map
// in src/rules.ts
import { defineIds, type GameMap, type Rules, type World } from '@v12-engine/engine'
export const UNIT = defineIds('scout')
export const rules: Rules = {
id: 'my-game',
name: 'My game',
units: [{ id: UNIT.scout, name: 'Scout', kind: 'unit', radius: 0.4, sight: 10, stats: { health: 100, moveSpeed: 5 } }],
// how its computers play, by difficulty, offered in a lobby
ai: { difficulties: { brutal: { every: 0.25, army: { attackEvery: 60 }, economy: { expansions: 2 } } } },
victory: null
}
export function onStart(world: World, { map }: { map?: GameMap }): void {
const starts = (map?.players ?? []).filter((player) => player.start)
for (const player of starts) world.spawn(UNIT.scout, player.id, player.start!)
// every player the map marks as a computer: a seat, knowing where the others 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' })
}
}Done: every player the map marks controller: 'computer' gets the first brain your game lists (Rules.brains; meleeBrain in a melee). On a server, the ai option says which players are computers and computer which brain.
Write a brain of my own
// in src/rules.ts
import { defineIds, type Rules, type World } from '@v12-engine/engine'
import { type AiModule, defineBrain } from '@v12-engine/engine/ai'
export const UNIT = defineIds('scout', 'raider')
const ENEMY = 2
/** A module: one named part of a think, made for each brain so that two seats never share state */
const holdTheCamp = (radius: number) => (): AiModule => ({
id: 'camp.hold',
think(ai) {
// `ai` is the brain: its reading of the game (`view`), its seat, its options, its numbers, its log
const camp = ai.view.named('camp')
const foes = camp ? ai.view.enemies.near(camp, radius) : ai.view.enemies
const first = foes.first()
if (first)
ai.send({ type: 'order', entities: [...ai.view.mine.fighters().ids()], order: { type: 'attackMove', point: { x: first.x, z: first.z } } })
}
})
export const redHand = defineBrain({ id: 'mygame.redHand', modules: [holdTheCamp(20)], every: 1 })
export const rules: Rules = {
id: 'my-game',
name: 'My game',
units: [
{ id: UNIT.scout, name: 'Scout', kind: 'unit', radius: 0.4, sight: 10, stats: { health: 100, moveSpeed: 5 } },
{ id: UNIT.raider, name: 'Raider', kind: 'unit', radius: 0.4, sight: 8, stats: { health: 60, moveSpeed: 4 } }
],
// listed, so that a save finds the brain again by its id
brains: [redHand],
victory: null
}
/** Once the world is made: the enemy's camp, and a brain at its seat */
export function onStart(world: World): void {
world.spawn(UNIT.raider, ENEMY, { x: 40, z: 40 }, { name: 'camp' })
world.computers.seat(ENEMY, { every: 1, brain: redHand })
}Done: the enemy attacks anything that comes within 20 cells of its camp.
Add a goal to the melee brain
meleeBrain.with({ goals: [raid] }) // a goal made with defineGoalThe melee brain is built from the kit's modules: economy(), build(), defence(), squads(), micro().
Test it
import { testWorld, thinkOnce } from '@v12-engine/engine/testing'
const world = await testWorld(rules)
start(world)
world.spawn(UNIT.scout, 1, { x: 45, z: 40 })
const report = thinkOnce(world, ENEMY) // one think: what it saw, what it decided, how many commands it sent
expect(report.brain).toBe('mygame.redHand')
expect(report.sent).toBeGreaterThan(0)In the editor
- Open Map settings › Players and set a player's controller to computer, and its team.
- Click Play here: the computers are seated as your start code says.
- Turn on View › AI overlay to see what each brain is doing.
Reference
| Call | What it does |
|---|---|
world.computers.seat(player, { brain, every, difficulty, enemyBases }) | Seats a brain (or its id). Kept in the world and in saves, memory included. |
world.computers.pause(player), resume, unseat | Stop, restart or remove a computer. |
world.computers.tell(player, word) | Send a brain a word your modules read. |
Rules.brains | Brains a save can find by id; the first is the default. |
Rules.ai.difficulties | Difficulty presets the lobby offers. |
thinkOnce(world, player) | One think, in a test: what it saw, decided and sent. |
A brain's choices come from ai.rng, never the clock, so replays play the same. Computer players covers the view, goals, difficulties and the melee brain's modules; Test my rules shows simulate, which plays brains against each other.