Repository navigation
144 computer players 2 the brain
Question: document 143 showed how a computer player plugs in: its slot's controls ask an AIBrain for the input word once a tick. What does the brain decide, how, and where will the later layers of document 142 go?
| Document | Covers |
|---|---|
| 142 | The plan: the layers, skill levels, the order of work |
| 143 | What a computer player is, and how it plugs in where a person would be |
| 144 (this one) | The brain: what it decides each tick, and where the later layers will go |
A port feature. The behaviour below is a placeholder, carried over from the old EnemyVehicle (written 2026-09-06 before any tracing, not tuned). Phase 1 changed where it runs (in a player slot, through the input word), not what it does. The next phases replace it.
-
AIBrain.thinkreturns one input word per tick. It splits on the slot's state: on the choice screen it confirms the vehicle; while driving it seeks and shoots; at any other time it returns 0. - On the choice screen it presses A once the view has faded in, and lets go every other tick, because the choice confirms on a fresh press.
- While driving, it picks the nearest vehicle of another side. It turns toward it, drives closer until 180 px away, and holds A when within 300 px and lined up within 8 degrees. Beyond 500 px it does nothing.
- Two traps of the input word are handled. A press-only weapon (the Jeep's missile) needs A released between shots. Any fresh press while the vehicle stands still on its own pad docks it.
- What it does not do yet: find a way around walls or water, go for the targets or the flag, use the turret, raised fire, mines or swim mode, refuel on purpose, or choose a vehicle. Each of those is a later phase.
Mermaid source
flowchart TB
T["think(mc, ps, prev)"] --> S{"ps.selecting?<br/>(the vehicle choice is open)"}
S -- yes --> CW["_choice_word(ps, prev)"]
S -- no --> L{"vehicle exists, alive,<br/>not frozen, not dying?"}
L -- no --> Z["0<br/>(in the base, rising, docking,<br/>dying, out)"]
L -- yes --> DW["_drive_word(mc, v, prev)"]
## The word for player `ps` this tick; `prev` is last tick's (a fresh press needs a tick without the button before it).
func think(mc: MatchController, ps: PlayerSlot, prev: int) -> int:
if ps.selecting:
return _choice_word(ps, prev)
var v := ps.vehicle
if v == null or not is_instance_valid(v) or not v.alive or v.frozen or ps.death_phase != 0:
return 0
return _drive_word(mc, v, prev)
Translation: mc is the whole match (all vehicles, the level, the flags), ps the computer player's own slot, prev the word it gave last tick. The brain gets nothing a person couldn't in principle see, but this placeholder doesn't limit itself to what is on a screen. Perception with a sight radius is part of phase 2 onwards (document 142, "Perception and fairness").
frozen covers the times the match holds the vehicle: on the choice screen, while it rises out of the pad, and once the match is over. So the brain only drives a vehicle that can actually move.
The choice screen reads the word itself (MatchController._selection_input, the original's FUN_00417ad0, documents 76 and 78):
func _selection_input(ps: PlayerSlot) -> void:
if ps.controls == null:
return
var w := ps.controls.word # polled by the player's vehicle each tick (it runs while frozen in the choice)
var pressed := w & ~ps.controls.prev_word
if ps.map_open:
if pressed & (InputWord.A | InputWord.B | InputWord.C):
toggle_map(ps)
return
if w & InputWord.UP:
select_move(0, ps)
...
var faded := ps.select_anim == null or ps.select_anim.fade >= 1.0
if faded and pressed & (InputWord.B | InputWord.C):
toggle_map(ps)
elif pressed & InputWord.A:
confirm_selection(ps)
and confirm_selection ignores the press until the view has faded in. So the brain:
func _choice_word(ps: PlayerSlot, prev: int) -> int:
if ps.map_open:
return 0
var faded := ps.select_anim == null or ps.select_anim.fade >= 1.0
return InputWord.A if faded and not (prev & InputWord.A) else 0
Translation: nothing until the view has faded in, then A, nothing, A, nothing... Each A is a fresh press, and the first one after the fade confirms. It never moves the cursor, so it takes whatever the choice opens on: the first bay with stock left (_open_selection), the Tank on Return Fire's maps while Tanks remain. It never opens the map window (B or C), and if the map were open it would leave it alone.
Mermaid source
sequenceDiagram
participant B as AIBrain
participant M as MatchController (choice)
Note over M: the choice opens, the view fades in
B->>M: 0, 0, 0 ... (not faded yet)
Note over M: fade reaches 1.0
B->>M: A (prev had no A: a press)
M->>M: confirm_selection - stock spent, the undock script starts
Note over M: the slot is no longer selecting, and the vehicle is frozen while the pad rises
B->>M: 0 (think sees a frozen vehicle)
The whole driving rule:
func _drive_word(mc: MatchController, v: Vehicle, prev: int) -> int:
var target := _nearest_enemy(mc, v)
if target == null:
return 0
var to_target := target.position - v.position
var dist := to_target.length()
if dist > DETECT_RANGE_PX:
return 0
var delta_heading := wrapf(rad_to_deg(to_target.angle()) - v.heading_deg, -180.0, 180.0)
var w := 0
if delta_heading > TURN_DEAD_ZONE_DEG:
w |= InputWord.RIGHT
elif delta_heading < -TURN_DEAD_ZONE_DEG:
w |= InputWord.LEFT
# close in while still far away or badly misaimed; hold back once close and lined up, so it doesn't ram its target
if dist > FIRE_RANGE_PX * 0.6 or absf(delta_heading) > 45.0:
w |= InputWord.UP
var could_dock: bool = v.dock_check.is_valid() and v.dock_check.call(v)
if dist <= FIRE_RANGE_PX and absf(delta_heading) <= AIM_TOLERANCE_DEG and not could_dock:
var t: Variant = v.weapons[0].get_script().get("TRIGGER") if not v.weapons.is_empty() else null
var press_only := t != null and int(t) & InputWord.HOLD == 0
if not (press_only and prev & InputWord.A):
w |= InputWord.A
return w
Step by step:
-
The target.
_nearest_enemygoes throughmc.all_vehicles()and takes the closest live vehicle whose side (player_index()) differs. Every slot's vehicle counts, so with more computer players each picks its own nearest enemy. Buildings, turrets, soldiers, Drones and the flag are not targets yet. - Sight. Beyond 500 world pixels (about 16 tiles of 32) the word is 0: the vehicle stops and waits. This is the placeholder's only "perception".
-
The angle.
to_target.angle()is the direction to the target in the port's heading convention (0 = screen right, +X; clockwise positive, since screen y grows downward).delta_headingis how far the target is from straight ahead, wrapped to -180..180. -
Turning. RIGHT for a target clockwise of the nose, LEFT for counter-clockwise. Inside +/-2 degrees there is no turn bit, so it doesn't swing left and right across the target. The turn speed is the vehicle's own (
turn_rate_degfrom its definition): the brain only says which way. - Throttle. UP (forward) while farther than 180 px (0.6 x 300), or while the target is more than 45 degrees off the nose (the old code's choice, untuned: keep moving while badly misaimed). Inside 180 px and roughly lined up, no throttle: friction stops the vehicle so it doesn't ram its target. It never reverses.
- Firing. A, but only when all three hold: within 300 px, within 8 degrees, and not able to dock (below).
Mermaid source
flowchart TB
A{"within 300 px<br/>and |delta| <= 8 degrees?"} -- no --> N["no A"]
A -- yes --> D{"could dock?<br/>(still, on its own pad)"}
D -- yes --> N2["no A<br/>(a fresh press would dock it)"]
D -- no --> P{"primary weapon press-only?<br/>(TRIGGER without HOLD: the Jeep's missile)"}
P -- no --> F["A every tick<br/>(the weapon fires while held)"]
P -- yes --> Q{"A in last tick's word?"}
Q -- yes --> N3["no A this tick<br/>(release, so the next is a press)"]
Q -- no --> F2["A<br/>(a fresh press: one missile)"]
Docking. A vehicle standing still on its own pad docks on any fresh press of A, B or C, and the weapons are skipped that tick (FUN_0040b980, document 77). In the port (game/vehicle.gd):
func _dock_pressed() -> bool:
if not dock_check.is_valid():
return false
var buttons := false
if controls != null:
buttons = input_state & (InputWord.A_PRESS | InputWord.B_PRESS | InputWord.C_PRESS) != 0
...
if buttons and dock_check.call(self):
dock_requested.emit()
return true
return false
A brain that fired at an enemy parked next to its base would sink back into its own pad. So it asks the same dock_check (MatchController.can_dock) first, and never presses while it could dock. A later brain that wants to dock (to rearm, refuel or swap vehicles) presses on purpose.
Press-only weapons. Most primary weapons fire on a press or a hold (keys & 0x60: the Tank's cannon, the MSV's rockets, the Heli's guns). The Jeep's missile fires on a fresh press only (TRIGGER = 0x20, FUN_0040df00). Holding A would fire one missile and then nothing. So for a press-only weapon the brain sends A on every other tick. The weapon's own cooldown still decides when a shot comes out.
| It cannot | Because | Fixed in (document 142) |
|---|---|---|
| drive around a wall, a building or water | it steers straight at the target | 2. navigation |
| see only what a player would | it reads every vehicle's position, within 500 px | 2. navigation / perception |
| aim ahead of a moving target, or check the line of fire | it aims at where the target is now | 3. combat |
| use the Tank's turret, raised fire, the MSV's mines, the Jeep's swim mode | it only sends directions and A | 3. combat, 5. other vehicles |
| attack the targets, take the flag, bring it home, intercept a carrier | it only knows vehicles | 4. the objective |
| refuel, rearm, dock to swap, choose a vehicle that suits | it never docks on purpose; it takes the first bay | 5. logistics |
| play badly on purpose (easy) or well (hard) | one fixed behaviour | 6. skill profiles |
It also stops when no enemy is within 500 px. On a map where the bases are farther apart than that, it waits at home until the person comes close.
All of it stays behind think. The match, the slot and AIControls don't change; AIBrain grows into the four layers of document 142, each its own file in game/ai/:
Mermaid source
flowchart TB
T["AIBrain.think(mc, ps, prev)"] --> PE["perception.gd<br/>a snapshot of what the slot may know<br/>(own state, bases, zones, targets, flags,<br/>enemies within a sight radius)"]
PE --> ST["strategy.gd<br/>a goal, re-chosen about twice a second<br/>(restock, attack, flag, carry home, intercept, engage)<br/>+ the vehicle choice"]
ST --> NA["navigation.gd<br/>a tile path to the goal<br/>(A* on per-vehicle-class grids)"]
ST --> MO
NA --> MO["motor.gd<br/>the word: steer along the path, aim,<br/>turret, buttons with the press rules above"]
MO --> RQ["reaction queue<br/>(the word a few ticks late)"]
SK["skill profile (pack table)<br/>reaction delay, aim error, sight radius ..."] -.-> PE & ST & MO & RQ
This is a plan: the file names and the split are document 142's design, not yet written. Two things from this phase carry into it:
- the choice word and the two traps (docking, press-only weapons) move into the motor, because they belong to how a word is pressed, whatever the goal;
-
prevstays the motor's memory of the last word, which is all the press rules need.
Debug switches (environment variables, never set in a normal run):
| Switch | Effect |
|---|---|
RF_DEBUG_AI_LOG=1 |
each computer player prints its target distance, heading, delta and word every 30 frames |
RF_AI_PLAYERS=0 |
player 1 is a computer too: on a two-sided map, a computer-only match, watched through player 1's view |
For example, a computer-only match on a two-sided map, muted:
RF_AI_PLAYERS=0 RF_DEBUG_AI_LOG=1 godot --path . --audio-driver Dummy
then start a Multiplayer level.
tests/ai_player_check.gd (engine, 19 checks) checks the brain's word directly, with two computer players' vehicles placed by hand:
- lined up 200 px away: A, no turn bit;
- the target straight down the screen (+90 degrees): RIGHT and UP, no A;
- the target far away: 0.
It also checks the choice: a computer player on the choice screen confirms by itself and spends its stock. The other checks are listed in document 143.
Next: the navigation phase (document 142, order of work, step 2) will get its own document in this series.
Return Fire, rebuilt
Elsewhere