AI difficulty and personality
How well a computer plays (its difficulty), how it likes to play (its personality), and every number in between (its settings).
Quick start
import { meleeBrain } from '@v12-engine/engine/ai'
import type { World } from '@v12-engine/engine'
export function seatEnemy(world: World): void {
world.computers.seat(2, {
brain: meleeBrain,
difficulty: 'hard',
personality: 'turtle',
settings: { economy: { workers: 6 }, flags: { heroesFlee: false, noMercy: 0.8 } }
})
}Player 2 thinks twice a second, attacks sooner, builds two towers at a raided base, keeps six workers on each mine, keeps its heroes in the fight and presses on when it is ahead.
Common tasks
Add a difficulty of your own
export const game: Rules = { ...mygame, ai: { difficulties: { brutal: { every: 0.25, army: { firstAttack: 60 } } } } }
world.computers.seat(2, { difficulty: 'brutal' })Your game's difficulties sit beside the engine's (DIFFICULTIES) and are offered in the lobby. Personalities work the same way (Rules.ai.personalities, beside PERSONALITIES).
Change one number for every computer of your game
export const game: Rules = { ...mygame, ai: { defaults: { army: { firstAttack: 240 }, economy: { expansions: 1 } } } }Make a race play its own way
export const game = {
ai: {
races: {
undead: { personality: 'turtle', plans: { order: [{ type: 'boneSpire' }, { type: 'crypt' }] } },
human: { heroes: { most: 2 } }
}
}
}A race's profile may also name its own brain (ai.races.undead.brain: 'mygame.undead', listed in Rules.brains).
Set it on the map
A map's player with controller: 'computer' may carry its own profile (MapPlayer.data.ai: { difficulty, personality, settings, brain }, and .races[race] for a race the lobby picks). world.computers.seat reads it, under what the lobby or the scene says.
One difficulty for the whole campaign
world.computers.defaults = { difficulty: 'easy' } // before the mission seats its computers; kept in a saveTurn a classic behaviour on or off
world.computers.seat(2, { settings: { flags: { kite: false, unitsFlee: 0.4, repairStructures: true } } })Check the difficulty in a rule
when({ difficulty: 'hard' }).do({ say: 'attack' })In the lobby
A game that says how its computers play (Rules.ai, without lobby: false) offers each computer seat a difficulty and a personality (seatSettingsOf, @v12-engine/engine/server). The server's default computer player (againstTheRest) seats with both.
Reference
Difficulties (DIFFICULTIES)
| Name | How it plays |
|---|---|
easy | Thinks every two seconds, 12 commands a think, slips a quarter of the time, attacks at four minutes |
normal | The areas' own numbers (what a seat with no difficulty gets) |
hard | Thinks twice a second, attacks sooner and with more |
insane | Hard, sees through the fog, half again of its income |
Personalities (PERSONALITIES)
| Name | How it plays |
|---|---|
balanced | The defaults |
rusher | Early, small, frequent attacks; no expansions; No Mercy |
turtle | Late attacks; two towers at a raided base; its workers flee |
expander | Three more halls, early |
harasser | Frequent small attacks, kiting, heroes run at half health |
A name your game does not have is played as the defaults, and logged.
Settings layers
Every number a computer plays by is one schema, AiSettings. Layers apply lowest first; each area is merged key by key (a build plan's wants and the waves' groups add up instead).
| Layer | Where you set it |
|---|---|
| the brain's | BrainDef.settings (meleeBrain.with({ settings })) |
| the game's | Rules.ai.defaults |
| the race's | Rules.ai.races |
| the difficulty | DIFFICULTIES, Rules.ai.difficulties |
| the personality | PERSONALITIES, Rules.ai.personalities |
| the map's | MapPlayer.data.ai.settings, then .races[race] (MapAi) |
| the seat's | AiOptions.settings |
What a module was made with (economy({ expansions: 1 })) is under every layer. resolveSettings(layers) merges them; a brain reads the result from ai.settings.
Which brain a seat plays: the seat's brain, then the map's (MapPlayer.data.ai.brain), then the race's, then ai.defaults.brain, then the first of Rules.brains.
Settings areas
| Area | Some of its keys | Page |
|---|---|---|
economy | workers, lumberjacks, workersPer, expansions, expandWhen, workersFlee | Build plans |
defence | radius, odds, towers, towerAfter, recall, quiet | Build plans |
plans | order; build: wants, placement, rebuild, repair; production: composition, research | Build plans |
army | firstAttack, attackEvery, waveSize, goOdds, surrender | Attack waves |
waves | waves, groups | Attack waves |
scouting | lookEvery, scout, flyers, from | Attack waves |
team | helpOdds, defendUsers, together, say, lines | Attack waves |
squads | retreatAt, reinforce, regroupWhen, tasks | Script a brain |
micro | targets, unitsFlee, heroesFlee, kite, shopping, heroes, spells | Script a brain |
heroes | most, secondFrom, priority (the melee player's) | |
lanes | LanesPlayOptions | Reference |
weights | A goal's score multiplied, by goal id | |
flags | The classic flags, below |
Flags
defendUsers, targetHeroes, repairStructures, heroesFlee, unitsFlee, groupsFlee, noMercy, ignoreInjured, removeInjuries, takeItems, buyItems, allowHomeChanges, smartArtillery, randomPaths, and the kit's rebuild, kite, workersFlee, say, scout.
Each is true/false, a number where it says how much (heroesFlee: 0.5), or a function of the brain asked each think. A flag with an area setting is mapped onto it in the layer that says it (Kite: micro.kite, Units Flee: micro.unitsFlee, Groups Flee: squads.retreatAt, Rebuild: plans.build.rebuild). No Mercy, Random Paths and Allow Home Changes are the melee army's alone (flagOf(ai, name)).
Profile (AiProfile)
| Field | What it does |
|---|---|
every | Seconds between thinks |
commandsPerThink | Most commands a think; the rest are held in the report |
mistakes | How often it slips (a think's fights left alone, a wave sent early) |
vision | fair, or all (sees through the fog; insane only) |
handicap | AiHandicap factors: income (via beforeDeposit), health, damage (beforeDamage), buildTime (beforeProduce) |
budget | Spread a think over ticks (see Reference) |
resolveProfile(options, registry, brainSettings, race) gives the profile; a brain reads it as ai.profile. The AI tab shows it under Plays by, and the report as AiReport.playing.