Bots: design and behaviour

How the bots of ngspades play. This is what a player meets in a match and what a server owner can count on. CONTRIBUTING.md holds the code-level notes: the files, the log lines and the bugs already fixed.

Bots are server-side players. They join, spawn, shoot, dig and build through the same paths as a client, and the server validates every action the same way: walls cost real blocks, digs respect the spade’s delays, a hit needs a real ray. They only play while a human is in the match.

Every bot is named Bot_ plus a generated name (Bot_SwiftFox). The words are the ones the client uses to name players (common/names/names.h).

Seeing and hearing

Fighting

Getting there

Shared bridge projects

A bot that meets a gap or water in front while clearing its way can start a short shared bridge (Server/BotProjects.c, from BattleSpades’ bounded project layer). It reserves up to six cells from the near bank toward the far one and looks for a partner within 16 blocks. The owner builds the near cells, the partner the far ones, alternating from the bank outward, so two bots never spend blocks on the same hole. Every placement still goes through the ordinary block action (reach, support, rate and the player’s wallet).

Cooperative fortifications

Where an objective is held by a group, the bots raise a structure on it together (Server/BotForts.c). The shapes come from the same place as the bridges: BattleSpades’ schematic library and its prefabs (bunker, watchtower, objective_ring, fort_wall), read as ideas and reimplemented with this server’s own cells and its ordinary block action. Every cell is validated by the server: support, reach, the block rate, protected cells and the water level are the same rules a player meets.

Builders put a wall from behind it: they stand on the side of a cell away from the threat (the structure covers that side only) and do not place from the enemy’s side while such a stand exists, so they no longer build a barricade and then stand in front of it.

Helicopters

Server/BotHeli.c, protocol 1.0, on the 1024 maps that have helicopters (docs/protocol-1.0.md 12.8). A bot uses one exactly as a player does: use (mount_use) to board and to get out, the movement keys, the look and the held jump / crouch to fly. Nothing in the seat or the flight knows it is a bot.

Forward posts on large maps

On a large map (bases 200 blocks apart or more: Raindance, Busan_Port) some squads hold posts in the middle instead of everyone running base to base. One squad in two holds them: two of its bots go to a high, flat spot on the squad’s side of the way, put up a wall toward the enemy and stay there, shooting from it and mending it. The squad’s other two hold a second post further on. The posts are spread over the team’s own half of the map, 20 to 50% of the way to the enemy. The holders stay in their squad, so the squad respawns next to them, but they never lead the squad’s lane; they leave the post only for an enemy carrying an intel within 30 blocks, or to carry the intel themselves. They do not walk home with our carrier: our carrier runs home from post to post (the next of ours on its way, if the detour is short), and each post watches round it as it passes. A squad that comes within 50 blocks of a manned enemy post goes to clear it instead of walking past. With posts the players are spread most evenly along the map (every tenth of the way holds 8-13% of them, 5-19% without), for some fighting and captures.

Zombies

In the zombie mode a zombie bot has no gun: it knows where every human is at any range and goes for the nearest, a human alone counting as nearer than one in a group (each mate within 8 blocks makes it count half as far again, up to three times), and runs at it along its planned way (digging through a fort when the way is shut). Going at a group, out of its sight and 15 to 40 blocks off, it waits for two more zombies to come up (8 s at most) and they go together. A zombie’s normal jump clears two blocks; it jumps at a step or a wall that high at once and leaps high at a human standing higher or at a higher wall, climbs houses with that leap (onto ledges 2 or 3 up, or into notches it cuts with the spade there, waiting for the leap to come back), spades a wall between it and a human close by, and scratches it within reach. The survivors spawn anywhere on the map. Each human bot walks up the highest place it can reach within 48 blocks of it (stairs, slopes: steps of one block), without digging and shooting on the move, and holds its own place there facing out; the bots that spawned in the same four chunks share that high ground, and two searches that end on the same roof are one group. Where nothing is higher on foot (a town’s houses with no stairs), it takes the best roof 3 to 10 blocks over a street it can walk to and climbs it with a pillar against the wall, each bot its own column. Up there they patrol: every 4 s each walks a few places on across the site (never onto its edge) and its look sweeps from side to side. A bot whose place is flat ground goes to the nearest high ground or roof another bot found within 64 blocks; with none, a bot alone joins the nearest group, and the group walls itself in: a ring three blocks high round it, built and mended from inside. A zombie close to one of them turns the whole group to it. A zombie comes back 35 to 50 blocks from a survivor drawn at random.

Arena

An arena round is fought until one team is left, on a map nothing may be dug or built on, so a bot’s job is only to find the other team. Each bot walks a lane of its own across the map: the team spreads over 90 blocks sideways between the two spawns (up to nine bots a row, closer together in a smaller team), each walking its lane toward the enemy’s spawn 30 blocks at a time, and only to ground at its own level (a lane with no ground near its next point, off the map’s land, is given up for the nearest enemy). While the gates are closed it waits at its spawn facing the enemy’s. An enemy within 80 blocks is the fight instead, and a bot that has walked its whole lane goes after the nearest enemy like it always did. Without the lanes both teams ran at whoever was nearest and every fight of a round happened in one cell of the middle of the map; now the damage of a round is spread over 4 to 12 cells of 24 blocks, and a round takes a little longer to settle because the bots meet in more places.

King of the hill

Every bot of a team has a job on the hill, the same one every life (by its place among the team’s bots):

Off the hill they all run onto it shooting as they go, never stopping for an enemy on the way: on a small map both teams see each other from their spawns, and bots that stopped to fight never reached the hill. When a round is won the hill moves and they all head for the new one.

A Rocketeer does not stand on the ground to hold the hill: any height over the ring counts, so it flies onto a roof or a tree top within twelve blocks of its place — a roof three blocks up is enough, and it prefers one over the ground — and stays up there for a minute, shooting down at the enemy’s way onto the hill from above. It is given the hill, never a post: a Rocketeer holding a bunker kept its jetpack on the ground, so the post rotation skips it and the rest of the team builds them.

Capture the flag roles

In CTF every team splits its bots into roles. Humans are not counted. The roles with a seat are drawn at random among the team’s bots when the match starts and kept all match, so a bot keeps its role from one life to the next. A seat changes hands only when its bot leaves the team; another bot draws it then. The gun comes with the role and is given while the bot is dead or about to spawn, like a player picking one on the respawn screen.

RoleWhenGunWhat it does
Defender2+ botsshotgunguards our intel from cover
Infiltrator3+ botsSMGgoes for the enemy intel unseen
Overwatch5+ botsriflethe defender’s rifle cover: watches our intel from high ground
Cover6+ botsshotgunthe infiltrator’s bodyguard
Tactical camper7+ botsriflesnipes from a spot on its side
Rusherevery second bot without a seatrifle or SMGtakes the intel whatever it costs
Attackerthe restrifle or SMGthe objective, fighting on the way

With 10 bots a team has one of each seat, 2 rushers and 3 attackers. Rushers and attackers draw a rifle or an SMG once and keep it. (The overwatch came from 4 bots in the first version: a team of 4 then kept two bots home, sent its only attacker the hidden way and fought half as much, 3 intel pickups in six 240 s matches instead of 12.)

Choosing in the chat

When the bots start playing (a player is in), each team talks its roles over in its team chat, the way players do in the first seconds of a round:

The defender and the infiltrator type at the same time, each line takes the time a quick typist needs for it (~16 characters a second, plus a moment to think), and the wording is drawn from a few ways of saying it. Meanwhile the whole team stands where it spawned, looking at whoever spoke last (the one speaking looks at the one it names): ~10 s. An enemy in sight still gets shot. A seat drawn later in the match is said alone (“i’ll guard the intel now”), without the wait. A new match draws the seats and talks again. Lines go through the server’s chat handler like a player’s, so the server logs them (Player Bot_.. (Team) said: ..).

Defender

Infiltrator

Cover

Camper

Rusher

Attacker

The dropped intel

Squads and lanes

Attackers and rushers play as squads (the squads of CTF, groups of 4). Bots with a role of their own (defenders, infiltrators, their cover, campers) are in no squad at all; they get one again if their role goes.

Before, every squad went down the middle in a line, and the sides of a big map stayed empty.

Everyone

Classes (next gen CTF)

In ngCTF a bot picks a class the way a player does (the same request the class screen sends), while it is dead or about to spawn, and the class gives it its gun. There are no roles there: no defender, infiltrator, cover, overwatch, campers, rushers or sneaky attackers, no briefing and no forward posts. Those are classic CTF’s; in ngCTF the class is how a bot plays, and every bot goes for the enemy intel its class’s way (the squads’ lanes, the escort of our carrier and the hunt of theirs stay). The classes are dealt round each team’s bots by id (team B two classes on), so every class plays from 4 bots a team; a class switched off goes to the next one on. They used to follow the seats (the guard always a Rocketeer, the campers Marksmen) and held half a team at posts: 16 bots, 600 s, six seeds, Raindance pickups 1.3 to 5.2 and captures 0.2 to 1.5, Busan_Port pickups 7 to 10.7, kills up on both; classic CTF event for event the same.

What stays of the old ways is the classes’ own:

What each class’s bot does with its own things:

Measured after the balance pass (16 bots and an idle human Commando, SP_TIME_SCALE=20, ~800 game s a match; rocket 0.5 of a grenade, reserve 4, the bots’ rocket spread widened; Miner 125 health and 0.95 speed; Rocketeer 85 health), kills/deaths per class:

Map, seedCommandoRocketeerMarksmanMinerRocket killsDrillsPerks earned
2Fort 1162/5951/3315/2921/325423revive 2, sentry 3
2Fort 1219/3358/2947/563/13102landmine 5, revive 1, sentry 3
Raindance 1114/2137/228/188/13130dynamite 1, landmine 1, sentry 1
Raindance 1212/1938/3635/431/1100landmine 2

With the Miner infiltrator and sneaky attackers and the Rocketeer guard (2Fort infiltrator plans mostly open=0, 60-73 cells dug of ~100 steps):

Map, seedCommandoRocketeerMarksmanMinerDrillsMiner pickupsInfiltrator under a roof
2Fort 1138/316/512/1110/261522 of 363%
2Fort 1242/344/740/3325/422782 of 483%
Raindance 1118/142/16/87/14170 of 015% (32% sneaking)
Raindance 1213/180/046/496/750 of 00 (no sample)

Before the pass (rocket 0.6 of a grenade, reserve 5, a tight bot aim) rockets made 74 of 156 kills on 2Fort seed 11 and 42 of 142 on seed 12. Rocket jumps: 6 to 24 a match. Miners closing in the hidden way (2Fort, 800 s, seeds 11 and 12): Miner 57/69 and 61/72 (0.84 against 0.54), 13 of 18 pickups and 6 of 9 captures by Miners.

Plant the Bomb

The terrorists’ carrier walks to the round’s site (the nearer to its spawn two rounds of three), and once inside it stops on its own spot and holds USE (the mode calls the same _ptb_set_use a client’s ptb-use reaches), crouched by the mode for the hold. Two terrorists escort it, the others take the site ahead of it; when the carrier dies the nearest terrorist fetches the dropped bomb. After the plant they hold a ring round the bomb. The counter-terrorists hold the two sites by rank, one in four rotating to the other at 35 s. After a plant they do not know where the bomb is (nothing announces the site; only a plant having happened is known): each goes to the nearest site the team has not cleared, until it is within hearing (90 blocks). A defender standing on a site (within its radius + 4) while hearing no beep clears that site for the team (they call it out in team chat), so the ones guarding the wrong site leave for the other at once and nobody turns back to a site just left (EVT ptb action=ct_rotate from= to= cleared=; if both end up cleared they listen again). The nearest defender that hears the bomb defuses when no terrorist is within 18 blocks of it or the fuse is short; the others retake the site in the open.

The defenders do not walk to a site in single file: from 55+ blocks away each takes a route by its rank among the living defenders, up to three: a staging point 25 to 40 blocks out, swung 66 degrees left or right of the line from the site to the bot (goal ptb-approach, reached or 30 s or within 30 blocks of the target), then the site; the third is the direct way, and a lone bot goes direct (action=ct_route route=0|1|2 of= site= dist=; searching and retaking both use it). SP_PTB_CT_SPLIT=0 plays the old search for a baseline.

Plant the Bomb plays Counter-Strike: Global Offensive’s competitive rules (MR15, 5 v 5): a team holds 5 bots and people at most (the host’s counts are brought down to it and a joining person takes a bot’s place), 15 s freeze, 1:55 rounds, 40 s fuse, 7 s between rounds, the sides swap after round 15, 16 rounds win and 15-15 after round 30 is a draw. A grenade already thrown still kills after its thrower died, in every mode. action=ct_arrive sector= secs= route= first= logs a defender’s first time within 25 blocks of the bomb (sector: bearing quarter, 0 east, 1 south, 2 west, 3 north). 16 bots, 1500 s, 4 seeds, five maps, old / new: CT round wins 88 / 124 of ~372, defuses 64 / 90 over the 20 matches, explosions fewer on dust2, cache, inferno; defenders reaching the bomb come in from 1.42 / 1.73 quarters a round (share of rounds with two or more 40% / 62%).

Asking bots for something

Everything above is the bots’ core, and it does not know which mode it plays. A mode, and later a plugin, drives bots through one struct, gm_bot_goal_t in server/Source/Server/BotGoal.h. The mode’s bot_goal hook fills it for a bot at every think (five a second), and the core plays it: plans the way, walks, fights, digs and builds. A zeroed goal, or no hook, means “nothing in particular”: the bot walks toward the mode’s patrol anchor and fights what it meets.

What for. The core reads only the intent, never the name.

fieldwhat the core does with it
namelogged (bot_goal goal=) when it changes; set with gm_goal_name(out, name, intent)
intentGM_INTENT_*, below
roleGM_BOT_ATTACK, DEFEND, INFILTRATE, COVER, RUSH: the gun (without classes), the fighting style, which enemies come first
seated, campthe mode picks the campers itself: 1 a camper (holds a spot behind a castle wall), 2 the overwatch (a high spot that sees home)
intentwhat the bot does
TAKEgoing to take the objective: close to it, it stops for no fight; puts a sentry or dynamite by it
PUSHan attacker on its way: an enemy farther than 30 blocks does not stop it
SQUADon its squad’s lane: picks enemies within 60 only, sneaks only off the lane
WITHkeeping up with someone (a leader, a carrier): shoots on the move
POSTholds a post: fights from it like a camper and never steps aside
HOMEthe defender’s work at home: its plans dig nothing there
GUARDthe defender’s post by home: crouched behind its wall
CHASEafter the enemy carrying our objective: the camper leaves its wall at once
BRIEFlistening to the team’s plan: holds still, looking at whoever talks
PLANTtaking an explosive (a landmine) to where it goes
COVERcovering a mate (the infiltrator)

Where.

field
x, y, zthe destination and its floor cell (z grows downwards; 0 unknown)
exactthe very cell, not just near it (a pickup)
holdstop this close and watch
watch_x, watch_ywhere to look while holding
perch> 0: a roof or a tree top within this of the destination counts as being there, so a bot with the jetpack flies onto one, prefers it and stays (the hill’s ring)

How to get there.

field
rushrun, ignoring fights (a carrier)
assaultfight in the open: no wall, no ducking
walk_onlyon foot: no digging, no sneaking
keep_terrainnot even the spade out of a jam

What to do there.

field
climbin the goal’s column: 1 builds a pillar up to floor z, 2 digs down to it
work, work_x/y/z, work_colorwithin reach: 1 puts a block there (of that colour), 2 spades it

What the team defends.

field
has_home, home_x/y/zhome: what the team protects. The guard and the overwatch post by it, and enemies near it come first
has_guard, guard_x/ysomething else to defend: enemies there come first

Outside the goal, the mode’s ops say the rest:

From a plugin

A Lua plugin (the plugins repository’s docs/api.md) gives bots their goals with the same fields, before the mode does:

function plugin.on_bot_think(bot)          -- every think of every bot
    if bot.team ~= 1 then return nil end    -- nil: the mode's own goal
    local v = game.player(vip)
    return {name = "escort", x = v.x, y = v.y, hold = 4,
            intent = {"with", "push"}, watch = {x = v.x + v.ox * 10, y = v.y + v.oy * 10}}
end
function plugin.on_bot_arrived(bot, name) end    -- once a goal is reached
function plugin.on_bot_stuck(bot, why) end       -- the core made no progress for a while
function plugin.on_bot_team_tick(team, bots) end -- once a second: hand out roles

server/tools/bottest is a test mode on this API (a VIP escort) with its measurements: 2.8 to 7.3 µs a call, 0.06 to 0.14 ms a tick with 120 bots.

Examples from the modes (server/Source/Server/Gamemodes/):

What bots do not do yet

Bots never ping: pings are for people.