Repository navigation
147 computer players 5 the objective
Question: phase 4 of document 142: what is a computer player trying to do? Until now it could drive and fight, but it only chased the other vehicle. Now it plays to win: it can break the other side's targets and bring the flag home, go after the other player to drain their vehicles, or defend its own base. It switches between these as a person would.
| 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 | The brain: what it decides each tick |
| 145 | Navigation: the map of where a vehicle can drive, the path, following it, breaching, getting unstuck, the harness |
| 146 | Combat: targets, leading, the turret, flying a copy of the shot, turrets, the Drone, no spawn camping |
| 147 (this one) | The objective: the three plans, the orders, the vehicle choice, docking for another vehicle |
| 148 | Logistics and the other vehicles |
| 149 | Skill levels and the computer's half of a split screen |
A port feature. The original has no computer-driven vehicle (document 113). Nothing here is traced, but the rules it plays by are:
- the target pools and the flag (documents 24, 26, 57 and 99);
- the vehicle choice and docking (documents 76, 77 and 78);
- who can still win (document 135).
- Three plans, as a person has in mind: attack (break their targets, bring the flag home), hunt (go after the other player to drain their vehicles, and stop a carrier that has our flag) and defend (stay by what protects our flag, and fight what comes).
- A plan per life, by weighted chance. In the hangar it scores the three by how the match stands, leans them by its own taste, and picks one by chance, so it isn't predictable.
- Events change the plan during a life: the enemy has our flag, the flag is there for a Jeep to take, the enemy reaches our flag, or a Defend has gone on too long with nothing happening.
- The plan gives an order each tick: go there, shoot that tile, hold here, or dock for another vehicle. The brain carries it out with what phases 2 and 3 built.
- The plan picks the vehicle: a gun to break targets, a Jeep once a flag is out. It walks the hangar cursor to it with its input word, like a person.
- Docking for another vehicle: it drives home, parks on the pad's centre at a crawl (a Tank has 4 px of tolerance), and presses a button.
- It gives up on what it can't reach, rather than sit there: some targets are on islands, which needs phase 5's Heli or swimming Jeep.
- The no-camping rule holds in every plan. Hunt goes for the objective when the enemy is down, Defend never stands by their pad, and nobody waits there for a vehicle to come up.
- What it achieves: it can win a whole match by itself, but it wins rarely in the 8-minute test runs, and it can't reach the targets on many maps yet. See step 10: the numbers aren't flattering, and the reasons are specific.
| Word | Means | In the code |
|---|---|---|
| plan | what it has in mind: attack, hunt or defend | AIStrategy.Plan |
| order | what the plan asks for this tick: go, shoot a tile, hold, dock |
AIStrategy.Order, AIStrategy.Do
|
| lean | this computer player's own taste for each plan, fixed for the match | AIStrategy._lean |
| active target | the one building of a pool that counts right now; when the pool runs out, the flag appears where the last one fell | TargetPool.get_active_position |
| carrier | a vehicle that can carry a flag: the Jeep |
flags.carries_flag, Vehicle.carries_flags
|
| swap | going home to dock, so the hangar opens and it can choose another vehicle | the order "swap", AIBrain._dock
|
| guard point | what Defend protects: our flag if it's out, else our active target | AIStrategy.guard_point |
| leash | how far from the guard point a defender will chase | AIStrategy.LEASH_PX |
Mermaid source
flowchart TB
A["shoot the other side's<br/>active target"] --> B{"budget<br/>left?"}
B -- yes --> C["another candidate<br/>becomes active"]
C --> A
B -- no --> D["the flag appears<br/>where it fell"]
D --> E["a Jeep touches it:<br/>it is carrying"]
E --> F["drive onto your<br/>own pad: won"]
E -. "Jeep destroyed" .-> D2["the flag lies there"]
D2 --> E
Only a Jeep carries a flag, and a Jeep's missile hardly scratches a building. So winning takes two kinds of vehicle: a gun to bring the flag out, then a Jeep to fetch it. That's the shape of the Attack plan.
AIStrategy is new, and the brain got smaller for it: the strategy says what, the brain says how.
Mermaid source
flowchart TB
S["AIStrategy<br/>the plan, and this tick's order"] --> B["AIBrain<br/>carries the order out, fights what it meets"]
B --> N["AINavigator<br/>where to drive"]
B --> G["AIGunner<br/>what to shoot"]
N --> M["AIMotor<br/>the input word"]
G --> M
# game/ai/ai_strategy.gd
func scores(mc: MatchController, ps: PlayerSlot) -> Array:
var attack := 1.0
var hunt := 0.6
var defend := 0.35
var foe := _foe(mc, ps)
if their_flag(mc, ps) != null:
attack += 1.5
if not _attack_possible(mc, ps):
attack = 0.05
if my_flag(mc, ps) != null:
defend += 0.8
hunt += 0.4
if foe != null:
if jeeps_left(mc, foe) <= 1:
hunt += 1.0 # one Jeep from unable to win (document 135)
if _stock(ps) + 3 < _stock(foe):
defend += 0.3 # behind: hold what we have
return [attack, hunt, defend]Translation: attacking comes first, because that's how a match is won. It's worth much more once their flag is out, and almost nothing if the target can't be reached. Our flag being out makes defending and hunting worth more. An enemy on its last Jeep is worth hunting: a side with no Jeep can't win. If we're well behind on vehicles, defending helps.
choose multiplies these by the computer player's own _lean (0.7 to 1.3 per plan, fixed for the match) and rolls a weighted chance, once per life, in the hangar. So one computer player may tend to attack and another to hunt, and neither is predictable. These numbers are starting points; the skill profiles (phase 6) will tune them.
Every 30 ticks, review looks for something urgent:
Mermaid source
flowchart TB
R["every 30 ticks"] --> C{"carrying<br/>their flag?"}
C -- yes --> A1["attack"]
C -- no --> M{"the enemy has<br/>our flag?"}
M -- yes --> H["hunt: stop it"]
M -- no --> F{"their flag free,<br/>and we are a Jeep?"}
F -- yes --> A2["attack"]
F -- no --> E{"enemy near our flag,<br/>or a Jeep near our target,<br/>and we are close?"}
E -- yes --> D["defend"]
E -- no --> Q{"a quiet Defend<br/>for 24 s?"}
Q -- yes --> N["choose afresh"]
Q -- no --> K["keep the plan"]
The defend alarm waits until the plan has stood for 5 s (MIN_PLAN_TICKS), so it doesn't flicker. The Defend time limit matters: without it, two defenders would wait for each other for ever.
| Plan | Situation | Order | Mode |
|---|---|---|---|
| any | carrying their flag | go home (driving onto the pad wins), stopping for nothing | carry |
| any | carrying our own flag | dock (docking returns it) | rescue |
| any | low on fuel, or out of shells | dock for another vehicle | swap |
| Attack | no flag out, in a gun | shoot the active target | attack |
| Attack | no flag out, in a Jeep, with a gun in stock | dock for a gun | swap |
| Attack | flag free, in a gun, a turret reaches the flag | shoot that turret | clear |
| Attack | flag free, in a gun, nothing guards it | dock for a Jeep | swap |
| Attack | flag free, in a Jeep | go to the flag, stopping for nothing | fetch |
| Attack | can't reach the objective | as Hunt | |
| Hunt | their vehicle is out | go to it, closing in | hunt |
| Hunt | their vehicle is protected | as Attack: nobody is defending | |
| Hunt | ... and no objective it can reach | go home | withdraw |
| Defend | our flag free, in a Jeep | go to it | rescue |
| Defend | away from the guard point | go there | guard |
| Defend | at the guard point | hold, chasing an enemy within the leash | guard |
What the brain does with a fight depends on the order. A hunter or a defender within its leash pursues: it closes in on what it sees. An attacker doesn't: its turret fires at a vehicle on the way while the hull carries on. A gun fixed to the hull has to stop and turn to fight. A gun turret or a Drone shooting at it is dealt with first, under any order that fights. A carrier stops for nothing.
A hunter with nobody to hunt goes for the objective. The enemy is down, so nobody is defending: it's the best moment to attack. That also keeps the no-camping rule better than going home did: it has something to do that isn't waiting.
Defend never holds a position inside the ring round the other side's pad (guard_point). When what it protects lies there, it stands at the ring's edge. Hunt and Defend have time limits (40 s and 24 s), so a life doesn't go by without the objective.
wanted_vehicle picks a Jeep when a flag is out for the taking, else the best gun in stock (a turret first), else whatever is left. It reads this from the pack's definitions (flags.carries_flag, the weapon handlers), so a mod's vehicles work. The Heli is left out until phase 5 teaches the brain to fly.
The computer player gets there by the same input word as a person, walking the cursor through the hangar's neighbour table:
# game/ai/ai_motor.gd
static func choice_word(mc: MatchController, ps: PlayerSlot, want: int, prev: int) -> int:
...
if not faded or prev != 0:
return 0
var goal := mc.bay_of_type(ps, want) if want >= 0 else -1
if goal >= 0 and goal != ps.selection:
var dir := _first_step(mc, ps.selection, goal)
if dir >= 0:
return [InputWord.UP, InputWord.DOWN, InputWord.LEFT, InputWord.RIGHT][dir]
return InputWord.ATranslation: wait for the view to fade in, and press nothing on the tick after a press (a held direction would move the cursor every tick). If the cursor isn't on the wanted bay, press the first step of the shortest way there. Once it's there, press A.
Docking needs a vehicle standing still on its own pad, within the type's tolerance of the centre: 4 px for a Tank (document 77). The navigator is too coarse for that, since it counts 20 px as arrived. So within 40 px of the pad the brain hands over to AIMotor.park_word:
static func park_word(v: Vehicle, spot: Vector2, tolerance: float) -> int:
var d := v.position.distance_to(spot)
if d <= tolerance * 0.6:
return 0
var w := turn_toward(v, spot)
if absf(heading_error(v, spot)) <= PARK_AIM_DEG and absf(v.speed) < d * 2.0:
w |= InputWord.UP
return wTranslation: inside the tolerance, press nothing, and friction stops it. Otherwise turn to the spot, and only throttle when nearly facing it and going slowly enough for the distance left. When can_dock says yes, it presses A once. The stock takes the vehicle back, the hangar opens, and the strategy chooses again.
Some targets are on islands. A Tank's route to the base may be fine, while its target is 400 px across water, out of reach of its shell. The navigator goes as near as it can. If it has nowhere left to drive, is still more than 48 px from the objective and has no line of fire for 2 s, the brain tells the strategy (give_up). That objective is then treated as out of reach: the Attack plan acts as Hunt, and attacking scores next to nothing until phase 5 brings something that can get there.
-
The gunner aimed at tile centres. A tile's shapes can sit anywhere in it, so a shot at the centre could miss the tile it was meant for. Worse, when a vehicle stood on the very tile it had to breach, the aim point was under it, the copy of the shot missed, and it never fired: stuck for the rest of the match on RFMAP121. It now aims at the middle of the tile's shape (
tile_aim_point), and a breach that loses no hit points for 2 s makes the navigator back off and try from elsewhere. -
A destroyed active target wasn't reported like other tiles. It reports through
target_hit, nottile_destroyed, so the navigation grid didn't re-check the tile. It does now. -
A hunter after a kill had nothing to do. It drove home, came back, and drove home again, never touching the objective. It now attacks (step 5).
-
My phase 4 change stopped attackers shooting back at turrets: the Tank kept its gun on the target building while a turret shot it. Any turret aimed at it now comes first. A Tank outranges a turret (216 px against 192).
-
A Jeep dies to one missile, so Jeep after Jeep died at the same spots. Turret reach now costs up to ten times more for a fragile vehicle (
FRAGILE_HP), and a gun clears the turrets that reach the flag before a Jeep comes (clear). -
A Jeep can't shoot a wall: its missile only goes for what it last ran into, so it now drives right up to a breach.
-
A vehicle wedged where the grid sees no way out stood still for good. A stranded vehicle now tries its unstick moves.
An engine check found another: MatchController._carriers counts carrier types in stock plus the one being driven, not Jeeps. "One Jeep left" was therefore true nearly always, and everyone leaned to Hunt. AIStrategy.jeeps_left counts the real number.
Two kinds of run:
RF_AI_LEVELS=all RF_AI_CHECK=1 godot --headless --audio-driver Dummy --path . --script res://tools/tests/ai_match_harness.gd
RF_AI_PASSIVE=1 RF_AI_TICKS=30000 ... # side 1 stays by its pad: can side 0 win against the map alone?
The 13 land maps of the every-5th sample, 30,000 ticks (8 minutes) each:
| Solo (the other side idle) | Head to head | |
|---|---|---|
| matches won | 2 (at 43 s and 5.8 min) | 1 (at 1.6 min) |
| targets broken | 10 | 15 |
| flags out | 4 | 6 |
| vehicles swapped by docking | 13 | 15 |
| vehicles destroyed | 90 | 150 |
All 104 two-sided maps, 6,000 ticks (96 s) each, as a regression check: 13,438 shots, 206 kills, 48 targets broken, 29 flags out, 1 match won; 78% of the time in Attack, 13% in Hunt, 9% in Defend. The whole match took 4.0 ms a tick on average, 10.4 ms at most (2.5 and 8.4 in document 146). The navigator got stuck 6 times a map on average (2.4 before), because vehicles now go to more places. These runs came before the last small change (guard_point); the flagged maps were checked again after it.
Why so few wins?
- Land doesn't always reach the target. A route between the bases says nothing about the targets. On RFMAP106, 111, 136 and 176 the target is on land a Tank can't reach, and it gives up. That needs phase 5.
- Maps are big. Targets can be 2,000 px away, about 30 s of driving each way, and it may take several to bring the flag out.
- Two equal opponents stall each other. A Jeep has 1 hit point, and whoever guards the flag kills it. Against a person the AI's fighting isn't the limit; between two copies of itself, it is.
- Solo, it hunts the idle side too when it picks that plan, which that test makes too easy.
What the runs did show: whole matches played start to finish: target, swap, clear, fetch, carry, win. RFMAP101 alone wins in 43 s. On RFMAP150 two Jeeps fight over a flag by a pad until one carries it off.
Two flags still open from the 104-map run:
- RFMAP104: a vehicle stayed stuck for 10 s. Not looked into.
- RFMAP150: "camped" for 7 s, 2 px from a pad. That's the Jeep fight just described, not waiting for a respawn: a false alarm of the harness's measure, left as it is.
The harness never found a computer player waiting by a pad for a vehicle to come up.
| It can't | Fixed in (document 142) |
|---|---|
| reach targets and flags across water | 5. the Heli, the swimming Jeep |
| win often: it fights so much it rarely finishes in 8 minutes | tuned with 6. skill profiles, once phase 5 reaches every target |
| refuel or rearm (it swaps the vehicle instead) | 5. logistics |
| lay mines | 5. the MSV's mines |
| search for the active target as a person must (it knows which candidate it is) | 6. skill profiles |
| be easier or harder | 6. skill profiles |
-
Engine,
tests/ai_strategy_check.gd(new, on the synthetic pack with a target pool for each side):- the scores, chance and lean;
- the vehicle for the plan; the orders in the table above, including clearing a gun that reaches the flag;
- a Hunt with nobody to hunt attacks, and goes home only when it can't; Defend stays out of the ring round their pad;
- the events that change a plan, and the Defend and Hunt time limits;
- out of fuel or shells;
- the hangar cursor; parking.
-
Engine,
ai_combat_check,ai_navigation_check,ai_player_check: the plan is pinned (forced_plan), so nothing in them depends on chance. -
Openfire: the harness above.
RF_AI_PLAN=attack|hunt|defendpins a plan for a run.
| File | What |
|---|---|
game/ai/ai_strategy.gd |
AIStrategy: the plans, scores, events, orders, the vehicle wanted |
game/ai/ai_brain.gd |
AIBrain: carries the order out, fights, docks |
game/ai/ai_motor.gd |
choice_word (the cursor), park_word
|
game/ai/ai_navigator.gd |
patience with a breach, a stranded vehicle shaking free, a Jeep driving up to a wall |
game/ai/nav_grid.gd |
turrets weigh more for a fragile vehicle |
game/ai/ai_target.gd |
a tile target aims at its shape |
game/match_controller.gd |
tile_aim_point, tile_hp, bay_neighbour, bay_of_type
|
tools/tests/ai_match_harness.gd (openfire) |
wins, targets, flags, plans, swaps; RF_AI_PASSIVE, RF_AI_PLAN, RF_AI_EVERY
|
Next: document 148, phase 5, logistics and the other vehicles (document 142): refuelling and rearming, the Heli, the MSV's mines and the swimming Jeep, which reach the water maps.
Return Fire, rebuilt
Elsewhere