Skip to content

Bots ​

Bots in Washiki Washoi are network clients: each connects to its game server over WebSocket and plays through the same protocol as a person. At the protocol level a bot is indistinguishable from a human player.

The bot system belongs to this game repository at bot/; in an assembled SHAG checkout that is src/games/fps/bot/. Run commands from the assembled root so data and navigation-cache paths resolve there.

Where bots run ​

By default the lobby runs them. lobby:server seats each invited bot itself, and when the match starts it hands that match's bots to a match runner, a child process that plays them (see A process per match). Nothing else needs starting:

bash
npm --prefix src/games/fps run lobby:server

A separate bot server is the alternative: bot:server holds the bots, and a lobby started with --bot-server asks it for them over its control port. It also boots an interactive console for tests, and can register with a lobby and queue for matches like a player.

bash
npm --prefix src/games/fps run bot:server
npm --prefix src/games/fps run lobby:server -- --bot-server

From src/games/fps, npm run bot:server does the same; the launcher keeps the SHAG root as the working directory. bot:server builds its bundle (dist-server/bot-server.mjs) before launching Node; bot:server:dev runs the source through vite-node.

The shared secret ​

Bots log in to the lobby with a secret shared by the lobby, the bot runners and any bot server, not with a player's account key:

  • Where it comes from: BOT_SECRET when it's set. Otherwise the first process to start writes one to <data-dir>/bot-secret (the data directory is --data-dir, else SERVER_DATA_DIR, else server-data/ at the repo root). Start them from the same folder, or give them the same --data-dir.
  • The control port: a lobby started with --bot-server sends the secret when it asks for a bot. The bot server's control port (9100, --port) listens on 127.0.0.1 only and closes any connection without it.
  • Bot accounts: standard difficulty presets have rating identities for private matches. Players cannot claim a bot's reserved name.

Architecture ​

Bot map loading uses the pk3 index in public/data/ and dist/data/ relative to the assembled root. Packages must contain the requested map. Missing maps or rejected published builds leave bot vision unavailable and log the loading error. Standalone BSP files are not a fallback for indexed loading.

Bot map knowledge uses the same spawn-point extractor as the game server, including generic and team-tagged spawn entities. Whitespace-separated origins are accepted; nonfinite or incomplete positions are skipped. Maps with no usable spawns use the server's three fallback positions.

Each BotClient owns one connection attempt. Retrying or rejoining creates a fresh client with the desired bot options. Failed attempts, disconnect and disposal consume the instance; a later connect() call is rejected before a socket is opened. The lobby retry loop already creates fresh clients. Map and navigation caches remain shared by BotServer.

Each playing bot sends paced input through its own timer. An explicit disconnect, socket close or socket error stops that timer.

ComponentResponsibility
BotServer.tsTop-level orchestrator; manages a fleet of bot clients
BotClient.tsA single headless network client; one per bot
BotBrain.tsAI base class; subclassed for different difficulty / strategy
BotGameState.tsParses incoming snapshots into a queryable world model
BotVision.tsBSP raytracing — line-of-sight, cover evaluation, spatial awareness
BotNavGraph.tsA* pathfinding on a BSP-derived Smart Waypoint Network
BotConfig.tsDifficulty presets; hands out bot names; default goal-arbiter mode
BotNames.tsBot name pool, reserved from players by the lobby
BotMovementProfile.tsKnowledge of movement mechanics (strafe jump, wall jump, etc.)
BotMovementAnalyzer.tsReads back what the movement actually did
BotWeaponProfile.tsKnowledge of weapon stats
BotFireSafety.tsRefuses shots that would hurt the bot or a teammate
BotNavCache.tsCaches the derived navigation graph per map on disk
BotSequence.ts, sequences/Multi-tick manoeuvres: strafe-jump routes, crouch-slides, jumppads, teleporters, validated jumps
goals/Goal sources, considerations and the arbiter that scores them
StateMachineBrain.tsThe rule-based brain
BotLobbyClient.tsLobby integration — bots join rooms and ready up like humans
runner/MatchRunnerPool.tsA child process per match that plays its bots (a match runner)

Difficulty Tiers ​

Four presets in BotConfig.ts, each a set of skill dials rather than a single number. The values below are the committed presets; aimAccuracy and the other skills run 0 to 1, and mistakeRate is how often a bot is allowed to be wrong.

PresetAimReactionTrackingPredictionItem timingStrafe jumpDodgeMistakesMatch sense
Easy0.25450 ms1.50.20.10.10.1580
Medium0.5280 ms3.00.50.40.40.430.45
Hard0.75150 ms5.00.750.70.70.710.8
Insane0.9260 ms8.00.90.90.90.90.21

Read the preset in source rather than a percentage here: these are the numbers the bot is actually built from.

How a bot turns ​

The brain decides where to look about 30 times a second; the client turns the view there over the commands it sends (BotAimController.ts), so a bot turns like a hand on a mouse rather than snapping to each decision. Its speeds come from the preset's tracking, reaction and aim values:

PresetTop turn speedSettle timeIgnores changes under
Easy150°/s0.41 s4°
Medium300°/s0.29 s3°
Hard500°/s0.22 s2°
Insane800°/s0.18 s1.5°

Pitch turns at 60% of these speeds. A bot that is firing tracks every change and follows its target's motion. A turn over 45° waits for the preset's reaction time first, drawn log-normal round it as people's are (one wait in ten is a third longer, one in a hundred nearly twice as long), and some of those turns overshoot by 4–12% and come back: about one in six at full aim accuracy, two in five on Easy. The aim error wanders slowly, drifting about once a second. Movement follows the wanted heading at once, so a bot never runs where its view happens to point mid-turn. While walking a route the bot looks about 250 units ahead along it; in a jump pad's flight, and stepping onto a pad or teleporter, it keeps its view rather than turning to a landing spot it is passing over. Scripted moves (learned routes, validated jumps) set the view exactly and bypass all of this.

A learned route (a trick recorded by a player and checked by bot:learn) is a script from standing on a mark: a sprint run-up, then the trick. Where the ground behind the mark is walkable, a bot runs in along the route's line instead of stopping on it, and takes over the script at the run-up state it matches in place, speed and heading (sequences/FlyingStart.ts). It joins only once the game's own movement, simulated from where the bot will be when the script arrives (two to five ticks later), lands the rest where the route lands; else, or when the run-in misses, it walks to the mark and stops as before.

Out of a fight with no enemy in sight (running for an item, hunting, retreating, or chasing someone it can no longer see) a bot is calm: there is nothing to react to, so its view eases toward where it wants to look, at most 120°/s, ignores changes under five of the deadzones above, and rests about 10° below level, at the floor ahead. A turn of more than 90° is a turnaround and goes the ordinary way: the reaction wait, then a fast turn. Between glances the view rests on the route ahead; every 2–4 s the bot glances for about 0.7 s toward a clearly more open direction within 45° of where it runs. Recorded people look like this: out of a fight their view turns about 35°/s while their movement weaves under it (bot:demo-motion measures it from demos).

What a bot knows ​

A bot knows what it sees and what it hears, not everything the server sends (BotPerception.ts). It sees enemies in line of sight within 55° of its view (70° for fast movers). It hears shots (1200 units; rockets, grenades, the electrobolt 2000), explosions (1500), footsteps (500), jumps (720), pain (800) and pickups (900; armour, mega and powerups 2500). Footsteps and jumps are heard when a player's client would play them (on the ground faster than 70 u/s and not sliding; leaving the ground faster than 60 u/s up), as far as they still play at an eighth of full volume under the game's sound falloff. A landing makes no sound of its own; one that hurts is heard as pain. A heard enemy is known where the sound came from. A heard major pickup is timed, with an error that shrinks with item-timing skill, and the bot is back at the spot when it should return, watching the ways in. An item taken out of sight and earshot is still believed there until the bot looks at its spot. No event names who takes armour or health, so the bot knows those as a player does: one that vanishes beside it is its own take, timed to the tick, and one it sees vanish or finds gone is the opponent's. Running out of a fight, a bot glances at an armour, mega or powerup spot it has not seen for 8 seconds when it is in sight within a quarter turn of where it runs, and dates one it finds gone from when it last saw the item there.

How a bot fights ​

  • Movement (BotCombatMovement.ts, by dodge skill): strafe runs of 0.25–0.9 s with double-backs, a jump or slide after a hit, a sidestep (and jump) off an incoming rocket or grenade after the reaction time, hops at range for strafe-jumpers, and a nearby ledge above the enemy with a splash weapon.
  • Slide-blade lunge (SlideBladeSequence.ts): from a run at slide speed, an enemy 200–450 units ahead is closed in one crouch-slide with the blade.
  • Combos (BotWeaponCombos.ts, by prediction skill): a launching rocket into the electrobolt or riotgun, a point-blank rocket into the blade, a rail between plasma; after a slow gun's shot, the other gun carried while the slow one reloads (an electrobolt into the machinegun or riotgun), when it fits the distance and fires at least 0.25 s sooner; and the gun for a heard enemy's distance readied before they appear.
  • Blocked (BotBlockedMove.ts): a bot on the ground that has asked to move for 0.6 s and stayed within 24 units, slower than 60 u/s and not pressed against an enemy, turns round, in any state. Out of a fight it backs off and plans its route again; in one it drops the route that led there, runs the most open way for half a second with its aim kept, and strafes the other way.
  • Firing on aim (BotFireSafety.ts): fire waits until the view is within 12° of the aim, so a bot turns round before it shoots.
  • Moods and mistakes (BotMood.ts): confident after two kills, tilted after three deaths (looser aim, slower reactions), cautious on low health or against an enemy with armour or a powerup. Each fight may carry one mistake at mistakeRate / 16 odds.

How a bot plays to win ​

Each decision the brain updates a read of the match (strategy/MatchRead.ts) from what the bot perceived: its own health and armor over time; an estimate of each enemy's, from spawn values, the hits it lands (armor takes 66% of a hit while it lasts), the pain tier each hit gives away and the major items the enemy was seen or heard taking; who took the major items; each fight and how it ended; and the score. Every second the strategist (strategy/Strategist.ts) names what is going on and picks a strategy for it, held at least 6 s:

DiagnosisWhenStrategy
UnderstackedFights started well under the enemy's stack, or a fresh estimate 40 or more above its ownRecover: retreat sooner, shop for items even armoured, no chasing, danger felt more
Losing the itemsOpponents took most of the major items in the last 90 sControl: lead the timed items, value them and their denial more
Enemy weakA fresh estimate under 70 while its own stack is 100 or morePressure: retreat later, shop less, more aggression
FocusedTwo or more enemies hurt it in the last 20 sRecover
Ahead late2 or more frags up with under 90 s leftProtect lead: take no risks, stay stacked
Behind late2 or more down with under 90 s leftPressure

A strategy is data: multipliers on the gametype's weight profile, which the goal arbiter scores items with, and a stance for the state machine (retreat line, how readily it shops, whether it chases). The rules still play every command. matchSense sets how much of this a bot does: Easy none, Medium only Recover and Protect lead, Hard and Insane all of it. At 0.6 and above the bot also adapts within the match: each diagnosis has two strategies that can answer it, and a bandit keeps how each paid off against this opponent (the score gap, the major item balance and its stack over the next 30 s), playing what has worked and now and then trying the other. It learns the opponent's habits too (strategy/OpponentModel.ts): a major item an opponent took twice in the last three minutes becomes contested, and the goal arbiter scores being there first by the profile's itemDenial (goals/considerations/ContestedItem.ts).

With a fresh estimate saying one hit would kill, a bot switches to the hitscan gun that does it at that distance (BotWeaponCombos), rolled at its prediction skill.

A strategy that fades (Recover, Protect lead) leaves a fight once it has taken 40 damage in it without a stack advantage by the estimate, and a bot retreating from an enemy in sight first heads for the nearest route node, 192 to 900 units away and not toward the enemy, that the enemy cannot see, then for health.

Bots learn across matches through a baseline, not through memory of anyone. At the end of each match a bot hands in a report (map, gametype, preset, score, its fights by 256-unit map cell and whether against a person or a bot, and each strategy episode with its payoff); the lobby (or the bot server that seated it) keeps them in <data-dir>/bot-intel.db for 90 days (a bot server started with --no-intel keeps none). npm --prefix src/games/fps run bot:intel distils them, on demand, into config/bot-intel/<map>.json: per gametype, how fights went in each cell and what each strategy paid for each diagnosis. Review and commit it like a route library. Bots read the payoffs as the bandit's starting estimates once an arm has 10 episodes in it, and the file is part of the behaviour fingerprint.

BOT_GOAL_ARBITER=verbose logs each change of strategy and its reason. The bot options strategy: false (no strategist whatever the preset) and habitItem (go for every item of one kind, the predictable opponent a strategist should exploit) exist for head-to-head tests with bot:ab.

Chat ​

Bots say the odd line to humans (bot/chat/): "gl hf" as the match goes live, something cheeky after killing a human (a blade show-off or a streak taunt among them), gracious or mad after being killed by one, a sulk after three deaths in a row, and "gg" in the last 30 seconds. The lines are English with a few Finnish ones. Each bot's name sets how chatty it is and how sore a loser. Lines are about humans only, so bot-only matches stay quiet. Each bot says at most one line per 20 s, all the bots in a process (a match runner, so a match) at most one per 5 s, and each line arrives 0.8–2.5 s after its moment, as if typed. Set BOT_CHAT=0 where the bots run (the lobby, or the bot server) to turn it off; training runs always have it off.

The brain ​

BotBrain is the interface; StateMachineBrain implements it, a rule-based brain. What it wants at any moment comes from the goal layer in goals/: goal sources propose candidates (acquire an item, hunt, hold an area, defend, escort, move to a place), each is scored by considerations, and GoalArbiter picks one. It combines them as gate × attraction × discount, taking the best reason rather than summing every reason, so a goal that is mediocre at everything cannot outscore a goal that is decisive about one thing. The arbiter's mode comes from BOT_GOAL_ARBITER: on by default, legacy to fall back to the older scoring, verbose to have it explain itself. Out of a fight a bot under 50 health or out of ammo goes for items; one with 25 armor or more hunts. Otherwise it weighs three things in the arbiter's terms and takes the best (goals/RoamChoice.ts): the arbiter's best item; hunting the latest sighting, worth 0.2 + 0.25 × aggression when just seen and nothing once it is stale (2.4 s for its own sighting, 6 s for a teammate's call); and, in Duel, holding its territory, worth half the profile's mapControl. aggression comes from the gametype's weight profile, so Duel (0.5) shops for most items and Deathmatch (0.8) for the good ones only. (scoredGoals: false on a bot puts back the older ladder: items over that bar, then the territory, then any item goal, then the hunt.)

A roaming bot decides on every second snapshot, about 31 a second (DEFAULT_DECIDE_EVERY; bots take turns); in a fight, a dodge or a movement sequence it decides on every one, and it sees and aims at full rate. Movement that takes more than a tick — a strafe-jump route, a crouch-slide into an item, a jumppad or teleporter traversal — runs as a sequence from sequences/, which the brain can interrupt.

Joining a room ​

The lobby flow is live: BotLobbyClient connects to the lobby with the shared secret, and either queues like a human or joins one exact room when the lobby hands it a room id and, for a private room, that room's invite token. It then readies up on its own. From the room's side this is the Add AI player key; from the lobby's side it is an invitation acknowledged only once the bot is actually seated.

The player-facing Add AI player action works in any waiting room, public or private, and leaves its visibility as it was. Without an explicit preset, the lobby chooses one near the player's private rating for that mode; new players fall back to medium. Standard presets have rating accounts used by the private ladder. A bot client still authenticates with its service secret, not a human account key; see Ranked matches.

A process per match ​

The lobby seats bots itself by default; a bot:server does the same with the seats it holds. Each bot's seat stays in its room but its match plays elsewhere. When the match starts, each bot's game connection goes to a match runner for that game server: a child process (cli/bot-runner.ts, dist-server/bot-runner.mjs in the bundle) that loads the map's vision and nav graph once for the match's bots and exits once its last bot has left. Two matches never share a runner, so one match's nav build or crash touches only its own bots, and the memory a match took goes when it ends. A runner reports each bot's rating identity and match report back to whoever seated it (the lobby, or a bot server), which passes them on to the lobby and bot-intel.db.

A bundled runner playing ten bots on washoi_snd1 holds about 175 MB: runners start with a 4 MB young generation (RUNNER_NODE_FLAGS), which cuts the heap V8 reserves from about 250 MB to 85 MB at the same snapshot cost, and trace the world in WebAssembly (src/engine/physics/CompiledWorldTrace.ts). SHAG_DEBUG=memory makes a runner log its memory every 10 s. A bot server started with --in-process plays every match's bots in itself, sharing each map's vision for as long as it runs.

Built on the SHAG Engine.