On this page
Bots: how they are spawned and driven
A bot is a player whose input comes from a library instead of a keyboard. The original keeps
the whole of it in one DLL beside the game (AIDLL.dll), asks it once per simulation tick, and
gives it a slice of the tick's time to think in. The port is the crate bf2-ai: 120 source files
that follow the DLL class by class. This page is the map of it: the layers, the clock, and what runs.
Where the bots live#
Bots exist when the setting UseAI is on: +ai 1 on the command line, and always in co-op (gpm_coop). The engine then loads the AI library and talks to it through a set of interfaces on both sides: the library asks the engine about objects, terrain and the game (AI\AIInterface in the executable), the engine tells the library about events (a shot heard, a vehicle destroyed, a player spawned).
| Piece | Original | What it holds |
|---|---|---|
| the manager of bots | AIDLL.dll!0x100e8d50 (BotManager's constructor) | the list of bots, the level of detail of each, the time shares |
| the strategy of a side | AIDLL.dll!0x100409d0 (StrategyManager) | the strategies a level's scripts added and the one in force |
| the path finder | AIDLL.dll!0x1004b1e0 (AIPathfinding) | the navigation maps, the search managers, the request queues |
| bot placement | AIDLL.dll!0x1003a040 (BotPlacementManager) | where a bot may be put |
| the factory of behaviours | AIDLL.dll!0x10028270 | a behaviour class by its name |
A bot is made by the script command ai.addBot <name> <side> <weights> <equipment> together with game.createAndSpawnAIPlayer, or by the automatic spawn of co-op, which fills the teams from a pool of names when the round changes to Playing. The server makes an ordinary player for it: the rest of the game (kits, spawn points, score, the network) does not know it is a bot.
The clock: once a tick, with a budget#
The engine updates the AI once per simulation tick, behind the game logic's update and only while the round is being played: Game::simulateTick (bf2_w32ded.exe!0x0044a840) calls the AI's update, which arrives at AIMain::update (AIDLL.dll!0x1005fba0). The AI's budget is 0.15 of the tick's 1/30 s, measured on the wall clock. Inside it:
- pending events are handed to the sides;
- one side's strategy runs (one side per tick, in turn);
- the squads update;
BotManager::action(AIDLL.dll!0x100e96d0) spends what is left.
BotManager::action gives 0.6 of its slice to carrying out plans, then shares the rest among a rotating list of update kinds with caps: 0.2 for decisions, 0.3 for path searches, 0.025 for hearing, 0.15 for seeing. A bot has a level of detail (full, medium, light) with its own count of ticks between full updates (6 / 6 / 6 by default, set by aiBotManager.setLodLevelTicks).
Every piece of AI work is sliced by the wall clock: how many path searches finish in a tick depends on how fast the machine is. Two runs of the original do not make the same decisions. The port's tests therefore freeze the AI clock, and the comparison with the original is made on pinned runs (the same clock readings handed to both).
The strategy of a side#
Each side has a strategic AI. A level's scripts give it strategies (ai.addSAIStrategy) and strategic areas (aiStrategicArea): rectangles on the map with a side, a temperature, neighbours and an order position. Once per cycle the strategy manager weighs each strategy's prerequisites (conditions on how many areas of each status the side holds) and picks one. A strategy sets the side's aggression, how many attacks and defences it runs and which kinds of object it favours. The strategy also chooses where a dead bot spawns next: one choice per death, made in the side's next cycle.
A bot's decision#
There are thirteen behaviours: Avoid, MoveTo, Idle, Fire, TakeCover, Change (enter or leave a vehicle), Special, Revive, c4, two more specials, Random and Triggerable. Each tick it decides, a bot computes the urgency of every behaviour, scales it by the weights of its kind (the standard weights favour Fire at 7.5 over Special and Revive at 3, TakeCover at 2 and Idle at 0.1) and by an urgency curve, and runs the most urgent. The script file says in capital letters that Idle's urgency must never be zero.
The piece is the last step only: weights times urgencies, largest first. In the original each urgency is itself computed (from what the bot sees, hears and was ordered), behaviours are asked in priority groups, and a running behaviour is kept for a while by a persistence curve. Avoid and MoveTo have the weight 1.
Which class carries a behaviour out depends on what the bot sits in. A line of aiSettings.setVehicleBehaviour binds, for one vehicle type, a behaviour to a behaviour class (BBFire, BBMoveTo, BBTakeCoverInfantry ..) and a behaviour pattern (BBPFireInfantery, BBPFire2d for a tank, BBPHeliFire3d ..). The pattern builds an action plan: a short program of statements (go there, turn, aim, fire, enter, wait) that the plan interpreter runs a few statements at a time. The plan's output is player input: throttle, yaw, pitch, fire, the stance keys.
Finding the way#
Bots walk and drive on navigation meshes: one map per kind of mover (infantry, vehicles), stored as quad trees of polygons with a graph of clusters above them. A path request finds the start and goal polygons, searches the cluster graph for the coarse route, then the polygons inside each cluster, then smooths the corner points against the mesh. Materials carry a cost per vehicle; a negative cost is impassable. The searches wait in two queues and are sliced by the clock like everything else.
What the port runs#
The crate bf2-ai and the server's bots.rs run co-op rounds: bot names and the pool's draws, squads, kits and spawn groups, the strategy choice and its cycle, decisions, the plan interpreter, path searches and path following, seeing and hearing, firing on foot and from vehicles, entering and leaving vehicles, tanks, cars, boats, helicopters and jets, radio messages, and the AI commander's artillery and requests. The pieces marked verified in the notes were compared bit for bit with both retail AI libraries through the native oracle; the rest is ported from the listing with its address beside it.
2142's AI library is the same design with the game's changes (walkers, gunships, a rule of its own for which team a new bot joins). Co-op bots play on the port's server with the shared crate: they spawn, move, shoot, kill, capture control points and crew the gunships and transports. The notes rate 2142's bots as partial: some vehicle behaviour still differs from the original's, and single player from the client is not compared. The 2142 pieces are cited from 2142's own server and AI library where they differ.
Heroes never shipped bots: the game has no AI library, though its executable still carries the engine's plumbing for one and its data still has the AI scripts of 2142 in spirit. The port adds bots to Heroes with the shared crate: this is not a port of original behaviour and the wiki marks it so. Team deathmatch, capture the flag and the hill have bots that fight, take objectives, use abilities and take seats; a round is filled to sixteen players in total, humans counted.
The notes keep a list of open questions (which phases of the strategy's cycle are reimplemented; urgency functions that are surveyed with their addresses and not yet compared). They mark every inference as unverified, and the wiki does not turn those into facts.
How it is tested#
- The oracle: the functions marked verified are called in the original library itself with the same inputs, in both floating point modes, and the port must answer the same bits.
- Pinned runs: recorded runs of the original server give the ground truth of the random draws, decisions and path goals; the port's run, with its AI clock held to the same readings, must make the same.
- Scenarios: played rounds with bots (a contact, a vehicle entry, a landing, a jet's attack) with asserts on what happened. See parity and how it is counted.
In the code#
| What | Where |
|---|---|
| the AI library | crates/bf2-ai/src/ (bot_manager.rs, decision.rs, behaviour.rs, sai.rs, strategy.rs, bot_navigation.rs, aim.rs) |
| the server's side | crates/bf2-server/src/bots.rs |
| the navigation data | crates/bf2-ai/src/search and the format note |
Notes: notes/subsystems/ai.md, notes/subsystems/ai-behaviour.md, notes/subsystems/ai-pathfinding.md, notes/subsystems/ai-navigation-bot.md, notes/bfheroes/bots.md.
The bots' radio calls use the same registry as the players': radio and voices.
docs/dev/server/bots.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.