Skip to content

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 ​

ts
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 ​

ts
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 ​

ts
export const game: Rules = { ...mygame, ai: { defaults: { army: { firstAttack: 240 }, economy: { expansions: 1 } } } }

Make a race play its own way ​

ts
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 ​

ts
world.computers.defaults = { difficulty: 'easy' } // before the mission seats its computers; kept in a save

Turn a classic behaviour on or off ​

ts
world.computers.seat(2, { settings: { flags: { kite: false, unitsFlee: 0.4, repairStructures: true } } })

Check the difficulty in a rule ​

ts
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) ​

NameHow it plays
easyThinks every two seconds, 12 commands a think, slips a quarter of the time, attacks at four minutes
normalThe areas' own numbers (what a seat with no difficulty gets)
hardThinks twice a second, attacks sooner and with more
insaneHard, sees through the fog, half again of its income

Personalities (PERSONALITIES) ​

NameHow it plays
balancedThe defaults
rusherEarly, small, frequent attacks; no expansions; No Mercy
turtleLate attacks; two towers at a raided base; its workers flee
expanderThree more halls, early
harasserFrequent 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).

LayerWhere you set it
the brain'sBrainDef.settings (meleeBrain.with({ settings }))
the game'sRules.ai.defaults
the race'sRules.ai.races
the difficultyDIFFICULTIES, Rules.ai.difficulties
the personalityPERSONALITIES, Rules.ai.personalities
the map'sMapPlayer.data.ai.settings, then .races[race] (MapAi)
the seat'sAiOptions.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 ​

AreaSome of its keysPage
economyworkers, lumberjacks, workersPer, expansions, expandWhen, workersFleeBuild plans
defenceradius, odds, towers, towerAfter, recall, quietBuild plans
plansorder; build: wants, placement, rebuild, repair; production: composition, researchBuild plans
armyfirstAttack, attackEvery, waveSize, goOdds, surrenderAttack waves
waveswaves, groupsAttack waves
scoutinglookEvery, scout, flyers, fromAttack waves
teamhelpOdds, defendUsers, together, say, linesAttack waves
squadsretreatAt, reinforce, regroupWhen, tasksScript a brain
microtargets, unitsFlee, heroesFlee, kite, shopping, heroes, spellsScript a brain
heroesmost, secondFrom, priority (the melee player's)
lanesLanesPlayOptionsReference
weightsA goal's score multiplied, by goal id
flagsThe 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) ​

FieldWhat it does
everySeconds between thinks
commandsPerThinkMost commands a think; the rest are held in the report
mistakesHow often it slips (a think's fights left alone, a wave sent early)
visionfair, or all (sees through the fog; insane only)
handicapAiHandicap factors: income (via beforeDeposit), health, damage (beforeDamage), buildTime (beforeProduce)
budgetSpread 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.