Pages

AI director scripts

A mod may ship a Lua script, the director, that steers the AEX AI in its matches: which tier to reach, which units to favour, when and how hard to attack. The director sets goals; the AEX AI still chooses sites, funds work, produces and fights. No mod needs one, and the AI plays every mod well without it. A director steers only AEX AI seats; seats playing the Original AI play TA's own logic.

Name the script in mod.toml (manifest):

[ai]
director = "ai/director.lua"

The script is read once when a match loads and runs for every AEX AI seat, each in its own sandbox. In multiplayer every player must have the same script: a different one is refused at join as an AI policy mismatch. Saves and replays carry the script, so they play with the director they were recorded with; a loaded save starts the director again (on_start runs again and the script's own variables start fresh, while goals it set before the save are gone until it sets them again). What the sandbox allows (no files, clock or network; deterministic math; memory and instruction limits) is in the engine's director page.

If the script fails (a syntax error, a runtime error, running over its instruction budget), the director stops for that seat for the rest of the match and the seat carries on as the plain AEX AI. The game log names the seat and the error.

When the script runs

Define any of these global functions; each is optional.

Function When
on_start(view) The seat's first AI pass.
on_unit_finished(type, count, view) Next tick after own units of type were completed.
on_unit_lost(type, count, view) Next tick after own units of type were lost.
on_enemy_seen(type, view) Next tick after an enemy type is seen for the first time.
on_tick(view) Every 5 seconds of game time, after the event handlers.

The top level of the file runs once at load; use it for constants and helper functions. Event handlers run in name order of the unit type, at most 32 calls per tick together; every event is also in view.events. One tick (its handlers and on_tick) shares one budget of 1,000,000 Lua instructions.

What a script sees: view

The same knowledge the AEX AI has for its seat, never more: enemies only as its fog allows (a Cheating seat sees through fog, as the AI itself does). Unit type names are upper case.

Field Meaning
tick, time Game tick and game time in seconds.
seat, side The seat's player number and its commander's side.
base.x, base.z Where the seat's base stands, in world units.
economy.metal, economy.energy income, use (per second), stored, storage.
units.count Own complete units.
units.by_type[TYPE], units.by_role[role], units.by_tier[n] Own complete units by type, by role (names below) and by tier; units.untiered have no tier.
units.planned[TYPE] Own frames and factory queue entries by type.
units.max_tier Highest tier among own units.
factories Array of {type, tier, count}, own complete factories by type.
can_build[TYPE] true for every type an own factory or builder can build now.
max_tier_buildable Highest tier in can_build.
enemy.seen Whether any enemy has been seen.
enemy.visible_power, enemy.defense_power, enemy.air_power Combat power seen now, of remembered defences, and in the air.
enemy.by_type[TYPE] Enemies in sight now, by type.
enemy.structures_known, enemy.radar_contacts Remembered enemy structures; radar and sonar blips.
enemy.radar_seen, enemy.long_range_seen Enemy radar or sonar, and long-range weapons, ever seen.
map.width, map.height Map size in world units.
map.class "land", "sea", "mixed" or "air-only".
map.water_permille, map.metal_world, map.metal_spots Water share, metal everywhere, metal spot count.
map.start_count, map.starts Start positions ({x, z}, the first 16).
map.chokepoint_count, map.chokepoints, map.basins Ground chokepoints ({x, z}, the first 32) and wide basins.
events.finished[TYPE], events.lost[TYPE], events.first_seen What changed since the last tick.

aex.type_info(name) returns what a unit type is, or nil for a type the match does not have: name, side, tier, tech_level, metal, energy, build_time, power, ground_power, range, hp, speed, can_move, can_fly, roles (a set: info.roles.factory) and build_options (an array). It describes types, never where a unit stands.

Role names (aex.roles lists them): extractor, metal_maker, energy, storage, builder, factory, commander, combat, defense, anti_air, artillery, scout, sensor, transport, superweapon, kamikaze, rift_gate, offensive_stockpile, interceptor.

What a script sets: aex.*

Goals last until the script changes them or clears them. Type names may be any case. A name the match does not know, a factory that is not a factory or a full list is ignored; a number out of range is clamped; both are counted and the first few logged. A wrong argument type (a string where a number belongs) is a script error.

Call Goal
set_tier_target(n) Reach tier n (0–16) sooner: factories and labs up to that tier need only the income a tier-1 factory needs. It does not choose which lab; use want_factory. nil clears.
want_factory(TYPE) Build this factory or lab ahead of the AI's own next choice, first named first (up to 8), when a builder can place it and it is funded.
set_role_weight(role, w) Favour units of a role that factories build: 1 neutral, 0 never, up to 10. A unit with several weighted roles takes the largest. Buildings placed by constructors are not weighted.
set_type_weight(TYPE, w) Favour one type, as above (up to 64 types); it multiplies the role weight.
keep(TYPE, n) Keep n (0–500) built or building; 0 drops it (up to 64 types). Only types a factory builds: a factory that offers the type builds it first while there are fewer. A building a constructor places is never kept.
set_attack_wave(n) Units in a wave before it may leave (1–500), in place of the profile's min_attack_squad; nil clears. Bomber runs and air drops keep their own sizes.
set_attack_earliest(s) No attack, bomber run or air drop before game time s (0–7200 s), on top of the profile's first_attack_sec; nil clears.
set_attack_cooldown(s) Pause between waves (0–1800 s), in place of the profile's; nil clears.
hold_attacks(on) No attacks at all while true: no new wave, bomber run or air drop, and waves and bomber runs under way come home.
set_target_priority(kind, ...) What waves go for first: commander, factories, economy, defenses, army, strategic. Of the targets a wave knows of and can reach, one of the first kind wins; naming commander or army lets it pick enemy units in sight, not only buildings. Bomber runs and air drops choose their own targets. No arguments clears.
want_strategic(kind, n) Strategic weapons to stand: offense (nukes), interceptor (anti-nukes), artillery; 0–16, in place of the AI's own target. Interceptors asked for are built before any enemy nuke is seen. A rich economy may build more offence and artillery.
set_play_style(style) defensive, balanced or aggressive: the seat plays that style at its own difficulty until cleared; nil returns it to the lobby's style.
add_region(kind, x, z, radius) An area (up to 16): expand claims its metal spots before any other, even past the play style's usual reach (the army's claim limit and danger checks still apply); avoid claims none of its metal; hold gets the AI's new defences first. Regions steer only extractors and defences: armies do not gather in hold areas or route around avoid ones. Where regions overlap, the first added counts.
clear(section) Clear tier, factories, roles, types, keep, attack, targets, strategic, style, regions, or everything with no argument.

Walkthrough: Escalation to tier 4

examples/public/creator/escalation-director is a complete director for Escalation. It is an example to copy and tune, not part of Escalation. To try it, put ai/director.lua in Escalation's folder and add the [ai] table to Escalation's mod.toml.

  • Tech. Until metal income reaches 20/s and energy 800/s it sets no tier target, so the AI keeps its own pace. Then it sets tier 4 and names the cheapest lab of the next tier it can build now, found with type_info by tier rather than by name, so it works for either side.
  • Unit mix. Once a tier-4 lab stands, it weights every tier-4 combat unit that lab makes at 4, raises combat to 1.5 and keeps four of the strongest on the ground.
  • Attacks. No attack before eight minutes; waves of 12 until four tier-4 combat units stand, then waves of 30 every four minutes aimed at factories, then economy, then the commander. Tier-4 losses double the pause for a while (on_unit_lost).
  • Strategic. The first enemy nuke launcher seen (on_enemy_seen) asks for two interceptors.

type_info is cached in a Lua table because type facts never change in a match; the script keeps everything else in locals so a tick stays well inside its budget.