AI Development Guide and M27AI v70 Devlog


v20 Update:
This version improves some of the AI's late game ability, both fixing an issue that led to severe under-utilisation of T3 mobile artillery in some cases, and having it build experimental units.

  • Experimental units will now sometimes be built late game
  • Improved use of T3 mobile artillery (they're less likely to gather in huge numbers doing nothing...)
  • Better use of heavily upgraded ACUs (including bugfix that caused Cybran ACUs to do nothing after getting laser)
  • Tweaks to Engineer management
  • Other minor bugfixes

v21 Update
This version focused mostly on smaller eco gains so when my AI is trying to eco it should do it a bit better. Main changes are:

  • Power stall manager to pause construction etc. to avoid or minimise power stalls
  • New dedicated ACU protection strategy to provide quicker help to our ACU when its in trouble
  • Increased focus on marginal mass gains when ecoing (e.g. not getting as much power so more mass can be spent on mexes; Reducing the combat units built so it can be spent on mexes, etc.)
  • Various bugfixes, including one that stopped a fully upgraded Sera com from doing anything, and issues with ACU logic for cancelling its upgrade and running away

Since my ACU self-destruct logic hardly ever triggers (I've only had it work in sandbox when testing it), I also decided to expand the behaviour - now my engineers and mexes should also exhibit self-destructive behaviour.

@jip FYI re your query a few versions ago when I introduced it, my ACU should only self-destruct in the niche scenario where it is about to die anyway and the enemy ACU is within range of the explosion and low enough health to die to it. I didn't want to risk a more general 'I give up' type logic both for fear of hit going horribly wrong, and since even in games where my AI's base has been destroyed it's sometimes managed a win with its ACU.


v22-v23 Update
There was no particular theme to these changes, other than that most of them arose from watching mistakes my AI made on a 1v1 against another AI on Burial Mounds

  • Tanks can now suicide (sometimes)
  • Fixed bug with power stall manager that made it far less effective; minor tweaks to this to prioritise HQ updates and initial engineer production (to allow more efficient power)
  • Minor logic improvements to experimentals (stopping them from trying to kite, making monkeylord move instead of attack-moving)
  • Ignoring PD that cant be pathed by land (so MMLs etc. arent overbuilt trying to counter it - relevant in particular for Burial Mounds)
  • Various tweaks to air logic (more likely to refuel, target transports with AA, make use of stealth)
  • More accurate logic for whether reclaim is within range (thanks to Marlo and Jip who helped me figure out how to determine the size of a reclaim object)
  • Another rework of my land scout 'intel path' logic - it should now form a path along the shoreline on island maps
  • Bugfix that caused my threat response logic to view each unit individually instead of grouping threats together
  • Various other bugfixes and tweaks

Although there was no big area of focus with this release, I did manage to achieve one of my long term goals with it, beating gunner1069 (the human sparring partner who usually plays a match against each version of the AI; they're a 1.1k global from playing astro exclusively, with their preferred tactic being to turtle - originally they played M27AI on Theta Passage, until around v10 when M27AI would fare well against their turtling on conventional maps, at which point they switched to Astro Craters).

Winning against a turtle on Astro Craters is harder than a normal map, since the normal advice against a turtle is to avoid attacking them, out-eco them with your map control (since T1 mexes are far cheaper per mass generated than T2 mexes), and then attack with an overwhelming force. Obviously this doesn't work so well if gaining map control just means you have 21 mexes to their 19. There are still plenty of flaws with my AI that a good astro player could exploit to win, but it can now at least offer a challenge on the map against weaker players.

V23 hotfix - Thanks to relent0r for flagging - there was a bug in v22 which caused my T1 platoon formers to not work, which should be fixed now.


v24 Update
The main focus of this update was having the AI rush bombers more often and various other changes to the air logic:

  • T3 bombers will sometimes be rushed depending on the map and game state (e.g. on 'island' maps)
  • Improved targeting for bombers, in particular when targeting enemy structures and considering whether to attack a shielded target (particularly relevant for strategic bombers who previously would ignore any shielded target)
  • Various tweaks to build orders and whether to upgrade an air factory in preference to a land factory
  • Various bugfixes

Latest changes (v25 summary)
v25 - Misc
There was no particular theme to this update or any one big change, just lots of small tweaks and bugfixes (many of which I'd been putting off for a while due to not being major issues). Some of the more noticeable changes were:

  • Building sonar on island maps
  • Guncom ACU considers targeting high value locations instead of just going for the enemy base
  • More factories and engineers are likely to be built earlier on
  • Minor adjustments to bomber targeting logic

In terms of the number of changes, ignoring hotfixes my main releases have typically averaged around 8 bugfixes and 16 misc/minor changes in addition to 1-2 significant changes/features. This version has 22 bugfixes and 64 misc changes, and was primarily aimed at clearing a backlog of small issues I'd noted to look at 'soon' from versions going back to v14.


V26 - Nukes, shields and novax
This update was aimed at increasing variety in the late-game (I'm basically working through the late game units I want my AI to try and build one by one).

  • Nuke launchers will be built late-game if the enemy doesnt have SMD, and aborted if the enemy gets SMD in time
  • Shield generators will be built to protect high value structures (although the Cybran's mostly for show since it builds the T2 rather than T3 shield most of the time)
  • UEF will build and use a novax centre in some cases
  • Various misc changes and bug fixes (12 bug fixes, 24 misc changes), including things like having factories self-destruct, multiple mass storage queued at a time, and better logic at avoiding trying to queue multiple buildings in the same place.

M27 turns 27!

Main change in v27: T3 arti introduced
Other changes: 23 bugfixes, 38 misc changes.

I was expecting the T3 arti to be relatively straightforward since I already had logic for choosing a nuke target that I could adapt, but getting the power adjacency to work became a real headache and also led me to a separate bug where structures could be built ontop of each other (thanks to Jip for confirming it's an engine bug).

The other bugfixes and misc changes included various updates relating to replays people helpfully provided:

  • Improved AI's ability to fight ghetto gunships (thanks to Babel for the replays highlighting this weakness)
  • Reworked overcharge logic (I'd originally written it when M27 could only build T1 units so there were a few flaws with it when fighting T2+ units)
  • Introduced a unit cap for ASFs (thanks to Azraeel for the replay highlighting 2 M27 AIs building c.900 asfs! and Fearghal for confirming a similar issue on the Air Wars map)
  • Fixed a hard crash that sometimes could happen at the start of the game. Also removed an amusing bug/feature where the Ythotha deathball would be controlled by the AI making it far deadlier (thanks to Relent0r for highlighting both of these)

Edit: Also managed to introduced a painful new bug - the ACU will now suicide into enemy PD (due to the new overcharge logic) - this will be fixed in the next version.


V28 – The compatibility update

This update was mostly focused on making M27 compatible with a wider range of game options. In particular, it should work far better on the following:

  • Non-assassination game modes (previously it would stop working altogether when its ACU was killed, or all enemy ACUs were killed)
  • No rush game option
  • Unit restrictions (only to a limited extent)
  • Maps where amphibious units can’t path to the enemy base
    Other notable changes:
  • Improved use of nukes (will consider if there are any valid targets that avoid SMDs rather than just assuming the SMD counters the nuke, while more targets will be considered initially and checked against SMD locations)
  • 19 Misc changes, 12 bugfixes (most notable being the one where the ACU would suicide into T2 PD)

Some of the changes to enable this compatibility involved a rewrite of numerous different sections of my code, given how up until now it’s been written on the assumption that if the ACU dies it wins the game, so it’s possible I’ve introduced a bunch of bugs and/or not all the changes introduced will work properly!


v29 - Optimisation and TMD
I spent the majority of this update trying to optimise some of the more intensive parts of the code, although the limitations with the profiling options meant it was hard to assess how much benefit this has:

  • Optimised logic relating to reclaim even more, in theory with an improvement in accuracy/responsiveness
  • Optimised platoon logic for raiding mexes, with a loss of functionality that I'm hoping might be more a benefit than a disdvantage (when originally written raiders would travel near friendly mexes on the way to the enemy mex - this was before my threat detection logic from ages ago so in theory this is less needed and in some cases is bad due to delaying the raiders)
  • TMD should now be built
  • Anti-mercy logic to help protect ACU from snipes
  • 4 Bug fixes (ACU should be slightly less aggressive, while there should be far fewer errors when running total mayhem mod) and 8 Misc fixes

v30 - Czar, Soul ripper and Defences
A bunch of new units unlocked/added this time, to varying degrees of depth. I'd originally hoped to have all the experimental air options added with this version, but I ended up getting sidetracked by some TMD building bugs which took far longer to resolve than I'd hoped (and included needing to rewrite parts of my engineer code), so Seraphim will have to wait.

New units that the AI will build and use:

  • Czar and Soul ripper
  • Static AA defences
  • Static PD and walls

Other changes:

  • Improved anti-cybran cloaked laser com logic
  • Improved fatboy logic
  • Reworked reclaim approach (again!)
  • ACU should run from experimentals much sooner
  • 23 Other misc changes and 9 bugfixes (including an unintended feature where the AI would get a 4-5s headstart in every game, and various bugs with building TMD)

The PD and AA buildings aren't anything fancy at the moment as this isn't meant to be a turtle AI (yet...).

Thanks to Azraeel for providing a number of replays which flagged certain issues (making me accelerate planned fatboy changes, and also highlighting M27's vulnerability to a cloaked laser com) along with providing some very entertaining moments, Babel for highlighting the 5s benefit M27 was getting at the start, and Fearghal for highlighting the vulnerability of the ACU to experimentals.


v31 - Bombers and TML
The main focus of this update was a rewrite of most of my bomber targeting logic and use of the Ahwassa. This took a bit longer than other updates in part because I had a week away.

  • Ahwassa can be built by Seraphim
  • Bomber targets are now determined based on the bomber tech level (previously the current Air factory HQ was used as a proxy with all bombers assumed to be the same tech level)
  • New engi hunter targeting mode for early bombers
  • Bombers should avoid targets protected by static AA ((previously they'd only avoid if the AA was near the target itself)
  • Introduced hover-bombing, mostly for strats and Ahwassa
  • Strats will launch co-ordinated attacks to try and take down more heavily defended targets
  • TMLs can now be built
  • 12 Bugfixes (including one causing massive over-production of engineers at the start of the game)
  • 37 General misc changes
  • 17 Further misc changes focused more on late-game (e.g. refining the logic for what experimental gets built, and how certain experimentals are used)

Some of the misc changes resulted from feedback/replays provided by people. In particular thanks to the following for this version:

  • Chp2001 – I almost gave up on getting hover-bombing to work, but Chp2001 shared code they’d used that achieved hover-bombing. Although I couldn’t get that code to work, it gave me better motivation due to knowing it should be possible for an AI, and a better steer on some of the timings - even after this I had so many failed attempts I was going to give up on my last attempt (which ended up working), so had it not been for Chp2001, there'd be no hover-bombing in M27AI as I'd have given up far sooner and needed more attempts to get there.
  • HintHunter – replays highlighting poor early-game performance including heavy mass stalling and engi-overproduction
  • Relent0r – highlighting poor performance on Selkie Mirror vs Sorian Air (on running the map I realised one reason was a bug with the pathfinding I used that led to 2 mexes being ignored)
  • Azraeel – Replays showing a number of late game flaws and issues when dealing with units on a plateau
  • Babel – Notes on ACU being too aggressive, and M27's vulnerability to enemy air if it doesnt scout the air fac in time (M27 now gets a couple of inties preemptively to support its bombers while its ACU is far more likely to fall back when ahead on eco)
    (apologies if I've missed anyone off - as it's been a longer update cycle I've lost track of the reasons for some of the changes)

v32 - Plateaus
The main focus of this update was introducing Plateau logic – on maps that have mexes that can’t be reached from the starting location, M27 should sometimes send engineers there with a transport. This effectively required the creation of a mini-M27AI to operate for these plateaus, with the following all being done:

  • Logic for identifying the plateaus, and deciding whether they’re safe to send a transport to
  • Building the transport
  • Loading engineers into the transport
  • Sending the transport to the plateau to land the engineers
  • Having the engineers decide on appropriate buildings to get (e.g. building mexes, land factories etc.)
  • Having factories on plateau decide on appropriate units to get
  • Disabling parts of the main M27AI logic that wouldn’t be very good on Plateaus (e.g. avoiding upgrading plateau factories to T2 unless there are no other options)
  • Having specialist logic for any units build by factories on a Plateau, to only consider targets on the Plateau. This involved different platoon AI logic for scouts, MAA, indirect fire units, and direct fire units, along with disabling parts of the main M27AI logic (e.g. for scouts and MAA)

Given the extent of the Plateau changes there will likely still be a few bugs relating to this.

Other changes:

  • 15 Bug fixes, including various issues with TML (they would rarely be built; get built in the opposite direction wanted; and could lead to a crash in the game), and a bug that could lead to 5+ experimentals being built simultaneously.
  • 44 Misc changes, mostly relating to use of air units, with some relating to building engineers.

Thanks to:

  • Relent0r – Replay highlighting bug that caused 5 fatboys to be built simultaneously and vulnerability of M27’s bomber defence to enemies building shielded flak near to M27’s base, along with general suggestion to have hover-bombing not apply if flak detected. Strategic flaw with having M27 ctrl-k all T1 factories at once when it thinks it doesn’t need them.
  • !MarLo - Providing several screenshots and replay highlighting issues with M27 having >300 strats idling

v33 Hotfix

  • Fixed a number of Plateau related bugs that resulted in issues where the Plateau had 3+ mexes
  • Fixed bug causing bombers (in particular T1 bombers) to not work properly when in defence mode
  • Various other bug fixes (23 in total including the above 2 points), including Czar targeting issues, MAA not being built for experimentals, and SMDs not building more than 2 missiles.
  • 36 misc changes, including making it less likely M27 will overflow mass, and improvements to special T1 bomber engi and mex hunting logic.
  • Misc changes included tweaks to the logic for building PD to make the AI build more of them (originally intended as part of a larger update to have the AI make firebases in some rare situations, but this part was postponed due to the number of bugs that needed fixing).

Thanks to:

  • Pyrolol – Highlighting issues relating to Plateaus on Open Palms
  • Relent0r – Highlighting a bug with the bomber defence logic and Plateau issues on Open Palms, along with replays showing a Czar targeting bug, and bug with sending MAA with experimental units
  • Fearghal – Replay highlighting SMD not unpausing after firing its missile (when 2+ missiles had been built)

v34 - Dynamic firebases

  • This update introduces firebases, although the scenarios where they get built will be relatively rare. More extensive turtling logic is planned for a future update (specifically pre-planned firebases), but for now M27 has what I'm calling 'dynamic' firebases, which in practice are likely to be built if it faces a constant stream of progressively stronger enemy units. As part of this, it will now build T2 artillery for the first time.

  • Another related change is the introduction of shield assist logic to help with T3 arti wars, with M27 being significantly better at surviving against a T3 artillery.

  • 15 Bug fixes, including an issue that stopped torpedo bombers working (they'd only attack enemies that got near them previously), and an error that would happen when building experimentals if the enemy had an experimental

  • 33 misc changes, including improving ecoing ability slightly by trying to always have something upgrading (sometimes this can hurt it though)

Thanks to:

  • Pyrolol for highlighting a bug with Plateaus on Twin Rivers
  • Relent0r for a bug with the decision on what experimental should be built.

v35 - pre-planned firebases
Part 2 of my turtling logic - this time M27 will preemptively setup a firebase at map chokepoints. It only works on very simple chokepoints (e.g. Astro and Fields of Isis; there will be a number of other chokepoint maps which it doesnt detect/work on), and I'm not even sure if it is beneficial even on these maps (it has a slight boost in performance against AI but that's to be expected as most AI will suicide units into the firebase - for example it was sometimes able to beat DilliDalli AiX 2.0 on Astro Craters. It's eco performance suffers massively from spending so much on the firebase though)

Other changes:
24 Bugfixes, including

  • Novax and T3 arti targeting (Novax would go to a unit's original position; T3 arti could sometimes error out when selecting a target)
  • Fatboy run logic (that'd cause them to flee in terror from the slightest enemy threat)
  • Issues with the power-stall manager, in particular a miscalculation of the energy usage of SMD and SMLs
    22 Misc changes, including:
  • Air factory builder tweaks so air scouts are more likely to be built, and so the air factory and intie should be rushed if enemy air units are detected early in the game
  • Making ACU more aggressive if the enemy doesn't have T3 units


  • Jip – Replay that resulted in an error relating to a land factory being built on a plateau where the pathfinding didn’t work.
  • JantDarvus – replay highlighting M27’s vulnerability to early jesters
  • Relent0r – highlighting issue with fatboys retreating, and issue with Ythotha’s attack-moving (I’ve decided to park the Ythotha point as a potential future change as I think I want experimentals to attack most of the time, except when there’s a Ythotha death-ball nearby)
  • Spyro – Replay where M27 may have caused a hard-crash (It's not clear from analysing the replay if M27 caused the crash or if it was a separate occasional error that happens with FAF where M27 isnt active, but the replay highlighted a few errors/issues)

v36 - Skirmishers
Dedicated skirmisher logic introduced (as opposed to previous logic which would sometimes have some units kite):

  • Units well suited to skirmishing (hoplite, mongoose, sniperbots) should now be built
  • Skirmisher units should be much harder to kill by retreating from enemies that get closer to them at a much earlier point, and maintaining good intel coverage
  • 8 bugfixes and 9 misc changes, including a new emergency T2 arti builder as a last resort for when enemy naval units get close to base, and making pre-emptive SMD get built sooner than before

v37 - Late game and game-enders

  • Added logic for building and using game-enders (Aeon will build rapid-fire arti instead of paragon since I expect building a poorly defended paragon in M27s base when it struggles to make use of all its mass without a paragon would do more harm than good)
  • M27 should now sometimes build quantum gateways and RAS SACUs
  • Tweaks and bugfixes relating to shields, so they're better at defending against enemy T3 arti (in particular Cybran)
  • Adjustments to bomber logic so T1 bombers are less likely to suicide into SAMs and more likely to prioritise units threatening mexes
  • Mass stall manager implemented
  • 20 other misc changes and 12 bugfixes

Note that the game-ender logic is very niche, so in most games I wouldnt expect any to get built.


  • Azraeel – Replay highlighting M27 suiciding bombers into SAMs, and M27 Cybrans failing to shield T3 artillery
  • Fearghal – Noting M27 doesn’t work when its start position is underwater (for now M27 will just give a chat message warning about this, and I'll park this for if I ever introduce navy)
  • Unknown – Note that M27’s experimentals can run from enemy experimentals (causing them to die without doing any damage)

Edit: V38 Hotfix:

  • Fixed bug that would break M27 if any unit restrictions were enabled (thanks to Punzler for the replay highlighting this)
warning: Error running lua script: ...ed alliance\mods\m27ai\lua\ai\m27factoryoverseer.lua(524): attempt to perform arithmetic on field `?' (a nil value)
         stack traceback:
         	...ed alliance\mods\m27ai\lua\ai\m27factoryoverseer.lua(524): in function `DetermineWhatToBuild'
         	...ed alliance\mods\m27ai\lua\ai\m27factoryoverseer.lua(1219): in function <...ed alliance\mods\m27ai\lua\ai\m27factoryoverseer.lua:1107>

Finished in like 10 minutes flat after norush. AI did not build much.


@splitice Thanks for flagging, have you got a replay, and were you using the latest version at the time (v38)? I checked the line referred to in the log and couldn't see anything obvious, and the current WIP version (v39) works ok still on a test game with norush so I'd need a replay to narrow it down.


v39 Update - Niche units: Quantum Optics

The first update focusing on niche units - Aeon can now build the Quantum Optics Facility. Main changes this update:

  • Quantum Optics will be used in some cases where intel is needed
  • Various tweaks to the bomber defence logic to tone it down slightly/reduce its tendency to suicide bombers
  • Various adjustments to the logic when losing air control
  • New T1 PD hunter logic to help when civilians have a T1 PD near our base (engineers should also now avoid T1 PD - previously they'd ignore it)
  • Unit cap logic to handle scenarios where the unit cap is reached
  • 13 Bug fixes, the most noticeable are likely to be issues with skirmisher logic that would lead to them either running too early or not running at all, and a bug that caused M27 to stop building more land factories if it suffered from a powerstall in the past
  • 37 other misc changes, including better support for multiple nukes, improved T3 arti targeting, and emergency T2 artillery builder enhancements


  • Azraeel – Lots of replays of M27 vs human matches, which highlighted yet more T1 bomber issues, M27’s vulnerabilities when losing air control and/or faced with mass restorers, and issues when multiple nukes fire at a similar time
  • Jip – Highlighting error messages on open palms relating to identifying reclaim segments of interest.