# BotWars bot API Everything a bot can use: the `botwars` package (`engine/botwars/`). A bot is one Python file with one class that subclasses `Bot`. The game creates one instance per match and calls its methods; the bot answers by calling commands. ```python from botwars import Bot class MyBot(Bot): def on_tick(self, me, enemies): self.forward(8) # drive at full speed self.turn_radar(45) # sweep the radar if enemies: self.aim_at(enemies[0]) # point the turret at the first enemy seen self.fire(2) ``` Contents: [How a bot runs](#how-a-bot-runs) · [Units and angles](#units-and-angles) · [Commands](#commands) · [Your tank: `me`](#your-tank-me) · [Enemies: scans](#enemies-scans) · [Events](#events) · [Helpers](#helpers) · [Obstacles](#obstacles) · [Other attributes](#other-attributes) · [Debug drawing](#debug-drawing) · [Blocks bots](#blocks-bots) · [Output, errors and time](#output-errors-and-time) · [Rules cheat sheet](#rules-cheat-sheet) · [Bot file rules](#bot-file-rules) ## How a bot runs 1. **Load.** The file must define exactly one `Bot` subclass; anything else is a load error. 2. **Start.** The game creates the instance and calls `on_start(config)` once per match. Set up your state here (`self.target = None`, …). `self.id` is already set; `self.me` is not (it is falsy, and reading `self.me.x` raises `NoTankError`): your tank exists from `on_round_start` on. 3. **Every tick** (30 per second of playback), for each round: - the events since your last tick are delivered first, each to its hook (`on_hit_wall`, `on_bullet_hit`, …); - then `on_tick(me, enemies)` is called with your tank and the enemies your radar saw. 4. **Commands are for this tick only.** Whatever you ask for during the calls above happens in this tick, then the commands reset. If you don't call `forward()` on a tick, your target speed is 0 and the tank slows to a stop. Calling a command twice in one tick keeps the last value. 5. **State persists.** Attributes you set on `self` live for the whole match, across ticks and rounds. The example above calls commands every tick; that is the normal pattern. ## Units and angles | Quantity | Unit | |---|---| | Position, distance | arena units; the arena is 1000 × 1000 by default, origin at the **bottom-left**, y grows **up** | | Speed | units per tick | | Angle, heading | degrees, `0` = east (+x), **counterclockwise** positive, headings in `[0, 360)` | | Turn commands | degrees for this tick: positive turns counterclockwise (left), negative clockwise (right) | | Energy, damage, power, heat | plain numbers (a tank starts with 100 energy) | ## Commands Call these on `self`. Values outside the legal range are clamped, not rejected. | Command | Meaning | Range (default rules) | |---|---|---| | `forward(speed)` | Target speed. The tank accelerates toward it (+1 per tick), or brakes (−2 per tick). Negative drives in reverse. | −4 … 8 | | `turn(degrees)` | Turn the **body**. The real limit shrinks with speed: `10 − 0.75 × abs(speed)` degrees per tick (speed at the start of the tick). | −10 … 10 | | `turn_turret(degrees)` | Turn the **turret** (where bullets go). Independent of the body. | −20 … 20 | | `turn_radar(degrees)` | Turn the **radar**. Independent of body and turret. | −45 … 45 | | `fire(power)` | Fire a bullet along the turret. 0 or less: don't fire. It only fires if the gun is cool (`me.gun_heat == 0`), `me.energy > power`, and the tank isn't disabled; otherwise it silently does nothing. `self.can_fire(power)` tells you in advance. | 0.1 … 3 | | `use_powerup()` | Use the item you hold (see [Powerups](#powerups)). Nothing happens when your slot is empty (`me.held is None`). Works while disabled. | — | Turning the body does **not** carry the turret or radar along: each part keeps its own heading. ## Your tank: `me` `on_tick(self, me, enemies)` gets `me` (also available as `self.me`): | Attribute | Meaning | |---|---| | `me.x`, `me.y` | Position of your tank's centre | | `me.heading` | Body heading (degrees) | | `me.turret_heading` | Turret heading: the direction your next bullet goes | | `me.radar_heading` | Radar heading | | `me.speed` | Current speed (negative while reversing) | | `me.energy` | Energy left. At exactly 0 the tank is disabled; below 0 it is destroyed. | | `me.gun_heat` | Gun heat; the gun fires only at 0 | | `me.disabled` | `True` at 0 energy: no moving, turning or firing until a refund lifts energy above 0 | | `me.held` | The powerup you hold (`"energy"`, `"coolant"`, `"shield"`, `"ping"`, `"cloak"`) or `None` | | `me.shield` | Shield points left (0 without a shield) | | `me.effects` | Your active effects: kind → ticks left, e.g. `{"cloak": 40}` | ## Enemies: scans `enemies` lists the enemies your radar swept over **in the previous tick** (empty when it saw nothing). A turning radar sweeps the arc between its old and new heading; a still radar sees a 10° beam. Each entry has: | Attribute | Meaning | |---|---| | `id` | The enemy's slot number (stable for the match) | | `x`, `y` | Its position when scanned | | `heading`, `speed`, `energy` | Its body heading, speed and energy when scanned | | `distance` | Distance from you | | `bearing` | Absolute direction from you to it, in degrees (same frame as `me.heading`). `lock_radar` uses it. | | `shield` | Its shield points left (0 without one) | | `cloaked` | `True` while it is cloaked (you only see a cloaked enemy within `cloak_reveal_range`, or with a ping) | The radar crosses an enemy only now and then while it sweeps, so most ticks `enemies` is empty. Keep the last sighting on `self` if you want to act on it every tick: ```python if enemies: self.target = enemies[0] ``` ## Events Override the hooks you need; the defaults do nothing. Event hooks run before `on_tick` in the same tick. Each event object `e` also has `e.type` (the event name) and `e.bot` (your slot, the same number as `self.id`). Compare `e.shooter`, `e.target`, `e.other` and `result.winner` to `self.id`. | Hook | When | Payload | |---|---|---| | `on_start(self, config)` | Once, before the first tick | `config`: the match rules (see `self.config`) | | `on_round_start(self, round)` | A round begins | `round`: 0-based round number | | `on_hit_by_bullet(self, e)` | A bullet hit you | `e.shooter` (slot), `e.bullet` (id), `e.power`, `e.damage` (energy you lost) | | `on_bullet_hit(self, e)` | Your bullet hit a tank | `e.bullet` (id), `e.target` (slot), `e.damage` | | `on_bullet_missed(self, e)` | Your bullet is gone without hitting | `e.bullet` (id), `e.reason`: `"wall"` (left the arena), `"bullet"` (collided with another bullet) or `"obstacle"` (absorbed by an obstacle: `e.obstacle` is its id) | | `on_hit_wall(self, e)` | You drove into a wall (your speed drops to 0) | `e.damage` (0 when wall damage is off) | | `on_hit_obstacle(self, e)` | You drove into an obstacle (your speed drops to 0) | `e.obstacle` (its id: `self.obstacle(e.obstacle)`), `e.damage` (wall damage; 0 when wall damage is off, when the arena wall already stopped you this tick, or for a second obstacle met in the same move) | | `on_obstacle_scanned(self, obstacle)` | You learned of an obstacle for the first time: every obstacle at the first round start when the map is known; one at a time (by radar, by driving into it, or with a bullet) in radar discovery | `obstacle`: the object, already in `self.map` | | `on_hit_tank(self, e)` | You collided with a tank | `e.other` (slot), `e.damage` (each tank takes it) | | `on_powerup_picked(self, e)` | You drove onto a stocked pad with an empty slot | `e.pad` (its id), `e.kind` (what you now hold) | | `on_powerup_expired(self, e)` | An effect of yours ended | `e.kind`, `e.reason`: `"timeout"`, `"broken"` (shield used up) or `"fired"` (you fired while cloaked) | | `on_pinged(self, e)` | An enemy's radar ping found you | `e.by` (its slot), `e.from_x`, `e.from_y` (where it pinged from) | | `on_death(self)` | You were destroyed | — | | `on_round_end(self, result)` | A round ended | `result.round`, `result.ticks`, `result.winner` (slot or `None` for no winner), `result.reason` (`"elimination"`, `"deadlock"`, `"inactivity"` or `"time_limit"`), `result.scores` (points per slot this round) | When a shield takes some of the damage, damage events also carry `e.absorbed` (the part the shield took; `e.damage` is what you lost). `on_death` and `on_round_end` arrive with the next tick (the next round's first), or, for the last round, when the match ends. ## Helpers | Helper | Returns / does | |---|---| | `self.angle_to(x, y)` | Absolute direction from your tank to a point, degrees in `[0, 360)` | | `self.distance_to(x, y)` | Distance from your tank to a point | | `Bot.normalize_angle(angle)` | `angle` wrapped into `(-180, 180]` — the short way round | | `self.turn_toward(x, y)` | Calls `turn(...)` with the body turn toward a point (still limited per tick) | | `self.aim_at(target)` | Calls `turn_turret(...)` toward anything with `.x` and `.y` (a scan, or your own object): `turn_turret(aim_error(target))` | | `self.aim_error(target)` | Signed degrees from the turret to the target, in `(-180, 180]`: `0` is lined up, positive means turn the turret counterclockwise (left) | | `self.can_fire(power)` | `True` if `fire(power)` would fire this tick: not disabled, gun cool, energy above the power. Exactly the game's check | | `self.lock_radar(target)` | Keeps the radar on a target: `turn_radar` by twice the error so the next sweep crosses it again (see [Radar lock](#radar-lock)) | | `self.clear_path(x, y)` | `True` if the straight line from your centre to a point touches no obstacle you know (see [Obstacles](#obstacles)). Exactly the test the game uses for radar and bullets. | | `self.clear_shot(target)` | `clear_path` to anything with `.x` and `.y` | | `self.obstacle_ahead(distance)` | The first known obstacle your tank would run into within `distance` along your heading, or `None`. Negative: behind you. Exactly the game's movement test. | | `self.obstacle(id)` | The obstacle with that id if you know it, else `None` | | `self.nearest_pad(kind=None)` | The nearest stocked pad (straight line), optionally only of one kind, or `None`. Ties go to the lower id. | A typical "is the gun lined up?" check: ```python if abs(self.aim_error(t)) < 2 and self.can_fire(3): self.fire(3) ``` ### Radar lock Call `lock_radar` on the ticks when `enemies` is not empty, and sweep when it is: ```python if enemies: self.lock_radar(enemies[0]) else: self.turn_radar(45) ``` It uses the scan's own `bearing`. Locking on a stored sighting only re-finds a target that has not moved: the radar points at where the enemy was, `enemies` stays empty, and nothing re-acquires. The lock wobbles from side to side, and an enemy crossing fast at close range can slip out of the swept arc for a tick; then sweep again. What you do about that is up to you. ## Obstacles Some matches have boulders and walls in the arena (the rules' `map_layout`; `"open"` means none). They stop tanks, absorb bullets and hide what is behind them from radar. Every obstacle object has: | Field | Boulder | Wall | |---|---|---| | `id` | stable for the match | same | | `kind` | `"boulder"` | `"wall"` | | `x`, `y` | centre | bottom-left corner | | `r` | radius | `0` | | `w`, `h` | `2r`, `2r` | width, height | | `cx`, `cy` | centre | centre (so `self.angle_to(o.cx, o.cy)` works for both) | `self.map` is the list of obstacles you know, sorted by `id`. It is empty before the first `on_round_start`, and it is the same list object for the whole match: the game only ever adds to it, so keep a reference if you like. - **Known map** (`self.config.map_known` is `True`, the default): `self.map` is complete from the first `on_round_start`, and `on_obstacle_scanned` runs once per obstacle right after it, so a bot written for discovery runs unchanged. - **Radar discovery** (`map_known` is `False`): `self.map` starts empty. Your radar beam reveals an obstacle when it touches any part of it (whole obstacles, never fragments); so does driving into one or hitting one with a bullet. Each new obstacle is appended to `self.map` and `on_obstacle_scanned(obstacle)` runs once, before that tick's other events. What you found stays known in later rounds, since the map is the same for the whole match. Obstacles right next to your spawn point are known from the start. A bot written for one mode runs unchanged in the other. The helpers answer from `self.map`, so in discovery mode a `clear_path` can still run into a wall you have not found yet. Rules that apply on maps: - Driving into an obstacle works like driving into the arena wall: you stop where you were, your speed becomes 0, you take wall damage (`0.5 × abs(speed)`, if wall damage is on) and `on_hit_obstacle` runs. You can never start a tick inside an obstacle. For tanks, walls behave as if their corners were square. - A bullet that reaches an obstacle is gone: `on_bullet_missed` with `e.reason == "obstacle"`. - Your radar only sees an enemy when the straight line between your centres crosses no obstacle. A hidden enemy is simply not in `enemies`; `lock_radar` on a stored sighting keeps the list empty until it is clear again. - Obstacle damage never counts as tank damage: it does not reset the inactivity timer and scores nothing. What stays the same: scans keep their fields; events keep theirs; other bots never learn what you have discovered; the map cannot be derived from `self.random`; the practice bots are not obstacle-aware. A bot that uses cover: ```python from botwars import Bot class CoverBot(Bot): def on_start(self, config): self.target = None def on_tick(self, me, enemies): if enemies: self.target = enemies[0] self.lock_radar(self.target) else: self.turn_radar(45) if self.obstacle_ahead(60): # something within 60 units along my heading self.turn(10) self.forward(6) if self.target: self.aim_at(self.target) if abs(self.aim_error(self.target)) < 3 and self.clear_shot(self.target) and self.can_fire(2): self.fire(2) ``` In discovery mode, sweep first and shoot only at what you can see: ```python from botwars import Bot class ProbeBot(Bot): def on_obstacle_scanned(self, obstacle): print("found", obstacle.kind, obstacle.id, "at", obstacle.cx, obstacle.cy) def on_tick(self, me, enemies): self.turn_radar(45) # a full sweep every 8 ticks maps whatever is in range self.forward(4) if self.obstacle_ahead(80) or me.x < 100 or me.x > self.config.arena_width - 100: self.turn(10) for e in enemies: if self.clear_shot(e) and self.can_fire(1): self.aim_at(e) self.fire(1) ``` ## Powerups With powerups on (the `powerups` rule names the kinds), pads on the floor hold pickups. Drive onto a stocked pad with an empty slot to take its item, then call `use_powerup()` when you want it. You hold one item at a time; a pad restocks with a random kind `powerup_respawn_ticks` after it is taken, and every pad restocks at each round start. | Kind | On `use_powerup()` | |---|---| | `"energy"` | `+energy_cell_amount` energy, with no cap. Works while disabled: it revives you. | | `"coolant"` | Gun heat to 0, and the gun cools `coolant_multiplier` times faster for `coolant_ticks`. | | `"shield"` | Absorbs `shield_points` of any damage (bullets, rams, walls, obstacles) for `shield_ticks`. A shooter still gets its energy back but scores nothing for absorbed damage. | | `"ping"` | One 360° radar scan to `radar_range` that sees through obstacles and cloaks. Every tank it finds gets `on_pinged`. | | `"cloak"` | For `cloak_ticks`, enemy radar misses you unless it is within `cloak_reveal_range`. Bullets still hit. Firing ends it. | Using an item you already have active refreshes it. Different effects stack. `self.pads` lists every pad every tick (`[]` with powerups off): `pad.id`, `pad.x`, `pad.y`, `pad.kind` (`None` while restocking) and `pad.respawn_in` (ticks left). `nearest_pad(kind=None)` returns the nearest stocked pad, optionally of one kind, or `None`. ```python pad = self.nearest_pad() if me.held is None else None if pad: self.turn_toward(pad.x, pad.y) self.forward(8) if me.held == "shield" and enemies: self.use_powerup() ``` ## Other attributes | Attribute | Meaning | |---|---| | `self.config` | The match rules, as attributes: every rules field, including all of [the cheat sheet](#rules-cheat-sheet) (`self.config.arena_width`, `self.config.max_turret_turn`, …) | | `self.id` | Your slot, from `on_start` on. The game sets it; don't use the name for your own state. | | `self.tick` | Tick number within the current round, from 0 | | `self.round` | Current round, from 0 | | `self.me` | The same object as `on_tick`'s `me` | | `self.pads` | Every powerup pad, every tick (see [Powerups](#powerups)) | | `self.map` | The obstacles you know, sorted by id (see [Obstacles](#obstacles)); the same list for the whole match | | `self.random` | A `random.Random` seeded for this bot and match. The global `random` module is seeded the same way, so a bot's randomness repeats exactly in a replay. | | `self.debug` | Debug drawing (below) | ## Debug drawing Draw on the replay to see what your bot is thinking; shapes last one tick and show only when you turn on **View → Debug drawings** (off by default). | Call | Draws | |---|---| | `self.debug.line(x1, y1, x2, y2, color="#ffffff")` | A line between two arena points | | `self.debug.circle(x, y, r, color="#ffffff")` | A circle | | `self.debug.text(x, y, s, color="#ffffff")` | Text (first 200 characters) | Colours are `"#rrggbb"`; anything else becomes white. Up to 200 shapes per tick are kept. ```python if self.target: self.debug.line(me.x, me.y, self.target.x, self.target.y, "#ff6b6b") ``` ## Blocks bots Bots built in the block editor are ordinary Python bots that subclass `BlocksBot` (from `botwars.blocks`) instead of `Bot`. `BlocksBot` is a `Bot` with a few extras the block editor needs, and you can use it in your own code too. A bot converted from blocks keeps working unchanged. ```python from botwars.blocks import BlocksBot class MyBot(BlocksBot): def on_tick(self, me, enemies): self.track_enemies() target = self.last_enemy_seen() if target: self.turn_toward(target.x, target.y) self.aim_at(target) self.forward(6) if self.can_fire(1): self.fire(1) ``` | Helper | Returns / does | |---|---| | `self.track_enemies()` | Sweeps the radar until it sees an enemy, then keeps it locked on the nearest one | | `self.enemies_seen()` | The enemies the radar saw: the same list as `on_tick`'s `enemies`; in an event hook, the latest sweep, one tick older | | `self.nearest_enemy()` | The nearest enemy the radar saw, or `None`. In an event hook it uses the latest sweep, one tick older | | `self.last_enemy_seen()` | The most recent sighting this round (the nearest, if several were seen at once), even when the radar can't see it now, or `None` | | `self.last_sighting_of(n)` | The most recent sighting of player `n` this round, or `None` | | `self.ticks_since_seen()` | Ticks since the last sighting this round; `-1` before the first | | `self.point_of(t)` | Where to aim or drive for `t`: an obstacle's centre (`cx`, `cy`), anything else's `x`, `y` | | `self.guard(name)` | Use as `with self.guard(name):` to run a block of code; an error prints `Error in : …` to the Console and skips only the rest of that block | Sightings are forgotten at each round start. `BlocksBot` also changes a few `Bot` helpers so they accept `None` and obstacles: `aim_at(None)` and `lock_radar(None)` do nothing, `clear_shot(None)` is `False`, `aim_error(None)` is `0`, and an obstacle is aimed at its centre. `lock_radar` locks only on an enemy the radar sees now, and sweeps otherwise. **Events win.** In a `BlocksBot`, a command an event hook gives (for example `turn` in `on_hit_by_bullet`) can't be overwritten by `on_tick` in the same tick. When two events give the same command in one tick, the later one wins. Commands still last one tick. ## Output, errors and time - **`print()`** works: output is shown in the Console and the Inspector for that tick (up to 4096 bytes per tick). - **An exception** in a hook or `on_tick` shows its traceback in the Console; that tick counts as a **miss** (your tank does nothing), and the bot keeps running. - **`self.me` in `on_start`** is falsy and raises `NoTankError` (an `AttributeError`) when read: your tank exists from `on_round_start` on. - **Time:** each tick must answer within 200 ms, or it is a miss. More than 30 misses in a row forfeits the match. A bot stuck for over a second is stopped and forfeits. Starting (`on_start` plus the first tick) has 2 s. - A reply larger than 64 KB (mostly debug shapes) is a miss. ## Rules cheat sheet Default values; every one is a field of `self.config` and can differ per match (the workbench's ⚙ rules, or a lesson). | Rule | Default | Config field | |---|---|---| | Arena | 1000 × 1000 | `arena_width`, `arena_height` | | Rounds per match | 3 | `rounds` | | Ticks per round (time limit) | 3000 | `max_ticks_per_round` | | Round ends after this many ticks with no tank damage (0 = never) | 450 | `stalemate_ticks` | | Tank radius | 18 | `tank_radius` | | Top speed forward / reverse | 8 / 4 | `max_forward_speed`, `max_reverse_speed` | | Acceleration / braking per tick | 1 / 2 | `acceleration`, `deceleration` | | Body turn per tick | `10 − 0.75 × abs(speed)` | `body_turn_base`, `body_turn_per_speed` | | Turret / radar turn per tick | 20 / 45 | `max_turret_turn`, `max_radar_turn` | | Radar beam / range | 10° / 1200 | `radar_beam_width`, `radar_range` | | Start energy | 100 | `start_energy` | | Fire power | 0.1 … 3 | `fire_power_min`, `fire_power_max` | | Cost of a shot | its power | — | | Bullet speed | `20 − 3 × power` (power 3 is slowest: 11 per tick) | `bullet_speed_base`, `bullet_speed_per_power` | | Bullet damage | `4 × power + 2 × max(0, power − 1)` (power 1: 4, power 3: 16) | `bullet_damage_per_power`, `bullet_damage_bonus` | | Energy back on a hit | `3 × power` | `hit_refund_per_power` | | Gun heat per shot | `1 + power / 5` | `heat_base`, `heat_per_power` | | Gun cooling per tick / heat at the start | 0.1 / 3.0 (first shot after 30 ticks) | `gun_cooling_rate`, `start_gun_heat` | | Ram damage (to each tank) | 0.6 | `ram_damage` | | Wall damage | `0.5 × abs(speed)` | `wall_damage_enabled`, `wall_damage_per_speed` | | Obstacles | none ("open"); families cross, rooms, boulders and scatter, combined with +, drawn from the seed | `map_layout`, `map_density` | | Map known at round start (else discovered by radar) | yes | `map_known` | | Powerups on the pads | none | `powerups` | | Pads / restock delay | 4 / 300 ticks | `powerup_pads`, `powerup_respawn_ticks` | | Energy cell | +25 | `energy_cell_amount` | | Coolant | ×3 cooling for 150 ticks | `coolant_multiplier`, `coolant_ticks` | | Shield | 20 points for 300 ticks | `shield_points`, `shield_ticks` | | Cloak | 150 ticks, seen within 150 | `cloak_ticks`, `cloak_reveal_range` | **Scoring:** 1 point per point of bullet damage dealt; 50 survival points shared by the tanks still alive at each death; a kill bonus of 20% of the damage dealt to the destroyed tank; 10 points to the last survivor. The match goes to the highest total score (ties: more round wins). ## Bot file rules - **One bot class:** exactly one `Bot` subclass per file. - **Imports:** the Python standard library, and `numpy` (loaded only when your file imports it). No network or page access: bots run in a sandbox. - **Metadata (optional):** plain-string lines at the top level of the file name and describe the bot. The workbench keeps them in step with the bot's name and the `.bwbot` export. ```python __botname__ = "Rammer" # the bot's name: 1–32 letters, digits, spaces, _ or - __author__ = "your name" # up to 64 characters __version__ = "1.2" # up to 16 __date__ = "2026-10-09" # up to 32 __about__ = "Chases and rams." # up to 200 ```