Some Insights on AI in AoE: DE and a couple of suggested features

The AI in AoE: DE is mediocre (it wasn’t great in vanilla AoE either and it seems to have improved a lot in the latest patches in DE). It is slow to build and sometimes breaks. The only reason it can challenge a player in Random Maps is because it cheats resources on the Hardest difficulty (as with the original game) and also on the Hard difficulty (unlike the original game). I have spent a deal of time working out how to make a reliable AI to play in a random map or scenario setting. This by no means makes it competitive with good or even decent players but might hopefully help to make it more effective for RM and Campaigns/Scenarios.

While I haven’t been able to find a way to play my own AI against the standard Random Map AI I am reasonably confident that it plays a bit more efficiently and breaks less often. Of course it goes without saying that being able to change the game’s code could solve even more problems and be more effective in the long run but as it stands the AI could be improved just by changing things on a basic level. Mostly I hope that if somebody making AI does not know about these things (and I am sure they know of most of them if they are experienced) that it might help them a little.

I have this post written out with proper links in a Google Doc but am unsure if I’m allowed to post links on this forum.

AI is broken down into three files:
-AI
-PER
-CTY

All of these play crucial roles in making the Computer play at its best. I will break down this post into three sections to detail what needs to be done with each file type. Keep in mind that all of these files are basic .txt but have a different extension in order for the game to recognise them for what they are (.ai, .per, .cty).

I will first list some useful software for developing an Computer player for yourself, then list tips on ways to make strong Computer players within each file type. Finally I will set out an example Computer player and link to a place to download its corresponding files. This will not comprehensively cover the basics of how to make these files and instead tries to highlight things that work well for somebody who has already made a few files themselves.

To find the right directories to put AI, PER and CTY files open your game, click Options -> Open Game Folders then navigate to Game Content -> AI Data. Place any files in their corresponding folders. You can only play with custom AIs when they are selected for a Computer player in the Scenario Editor.

Software:
The following software is extremely useful to have when editing these files:

AI Editor:
AIEdit v2.0 by Stoyan Ratchev - Can be found at the Age of Empires Heaven website Granary - Utilities section

Per Edit:
Per Edit by Martin Sturm - Can be found at the Age of Empires Heaven website Granary - Utilities section

CTY Editor:
CTY Editor by BBalazs - Can be found at the Age of Empires Heaven website Granary - Utilities section

AI:
The AI should more accurately be called the build order for the computer. It is a series of commands that tell the computer what to build and, in a very roundabout way, when they should build it.

The format for each command looks like this:
Type - Name - Quantity - Parent - Retrains

-The Type indicates what kind of thing is to be produced (Unit, Building, Technology)
-The Name is the game’s name for that thing (These can differ from their in-game name)
-Quantity is how many to produce (in the case of Units or Building)
-Parent is a pointer to what building or unit produces the given thing
-Retrains is how many times to remake these if they are destroyed (in the case of Units or Buildings)
-If the retrains section is left blank then the Computer will always try to remake these throughout the game

In the actual AI file the commands look like this:

B109 Town_Center 1 -1
U83 Villager 1 109
U83 Villager 1 109
U83 Villager 1 109
U83 Villager 1 109
R101 R__Tool_Age 1 109

So in this instance the Computer would build one Town Center and four Villagers. Of course in a Random Map start the Town Center and three Villagers are already there so it will produce only one Villager to complete the list.

There is a specific Type number and Name for each thing in the game but I won’t list them in this post, instead I will share some of the important things I have learned in creating an effective AI.
The first thing is a rule of thumb: divide each build command into a single line, so if you want the computer to produce 14 Villagers you should make 14 lines telling it to produce 1 Villager. The reason this seems to be optimal is that the computer looks ahead in the build order to see where it needs to assign villagers to gathering. Having a massive chunk of villagers to produce can occasionally ‘confuse’ it into sub-optimal villager tasking.

The second thing to be aware of is that the computer does not actually treat the commands as a list of when to build/train/research but instead checks through the list (it seems that it checks through the whole list constantly though I may be wrong) and whatever it is validly able to build, it builds it. So if you tell it to make Villager, Farm, Villager then research Woodcutting it will make a Villager, build a Farm and research Woodcutting all at once.

Normally this is good as it allows the Computer to produce multiple units from multiple structures and multi-task its structures and building construction efficiently. It does however come with one big pitfall: since it makes units as soon as possible it will ignore whatever you want it to make in between. For instance if you tell it to make 10 Villagers in the Stone Age, 5 more in the Tool Age and then 5 more in the Bronze Age it will most often make 20 Villagers in the Stone Age before advancing. With this in mind you must either have a low Villager count and a weak economy throughout the game or have a strong economy later in the game but a very slow and even sometimes unattainable Tool Age/Bronze Age time.

The way to fix this is through what I call a Critical Research Gate. Technologies in the game can be denoted as a critical item to research by replacing the R in the Type with a C. So this:

R101 R__Tool_Age 1 109

Becomes this:

C101 R__Tool_Age 1 109

What this does is to stop the AI doing anything beyond that technology until it is researched. So the computer will continue to replace the things built in the order before the critical technology but nothing that comes after it until it is done. So you can then set the Tool Age research to critical and govern how many villagers are produced during the Stone Age while not sacrificing later economic power.

Research gates can be used to finely tune how a Computer progresses through its build order, though it is at the cost of the resources and time taken to research the technology you are using as a gate. Using critical research gates can make a solid difference in the computer’s strength throughout the game.

The third point is to understand the idiosyncrasies of the way the Computer reads the AI file:

-If a critical research is present that doesn’t exist in the technology tree of the Computer’s civilisation it will get stuck there in the build order forever. This doesn’t appear to be so egregious if it’s a non-critical technology.

-The Computer will sometimes double-build a structure you have told it to build only a single time if it has enough resources and the first building is being built but hasn’t yet finished construction and there is a roadblock in the build order.
-E.G The Computer must build 1 Archery Range. It has 500 Wood on hand so it places the foundation for an Archery Range. The next thing on the list is Bronze Age as a critical research so the computer has nothing else to do but it sees the Archery Range is not technically complete, so it places another down.

-Granaries, Storage Pits and Houses are automatically constructed on an as-needed basis by the Computer. There doesn’t appear to be a way to make them build these structures reliably using the AI file.

CTY:
The CTY file governs where the Computer can place its buildings. It is the most sorely neglected aspect of AI and also the most important to get right to prevent the Computer from failing to progress through its build order. The CTY file is best created using the CTY editor as the numbers can be quite counter-intuitive. The below are a few tricks I have learned to innoculate the Computer player from the threat of stalling in its build order:

-Farms should be placed directly around the Town Center to maximize efficiency. Farms counting over 8 will be automatically placed by the Computer

-Houses will be automatically placed by the Computer but MUST be present in the CTY file so that the Computer does not break

-It is important to place MANY houses in a wide variety of locations as the Computer seems to check for a valid/free place even when it ends up putting the houses nowhere near that place. This seems to be the #1 cause of the Computer player failing to progress

-Use of the Any Type building should be minimal as the computer will opt to sometimes build farms in those places instead of other buildings.
-The Computer MUST have AT LEAST one Any Type building in the CTY file or it will break
-I place the Any Type buildings adjacent to the 8 farms surrounding the Town Center so if the Computer opts to make them farms it is still reasonably efficient to build them there

-Use the specific types of buildings present in your .AI file liberally as the Computer MUST have a valid space to build it or it will stall
-Many things can block a space in the CTY file from being used such as:
-Resources (Forests, Gold/Stone Mines)
-Houses (From the Computer’s automatic placement)
-Farms (From the Computer’s automatic placement)
-Terrain (Too steep/rough/water)
-I will often places the specific buildings in the .AI file a dozen or so times EACH at varying distances and directions from the Town Center to maximise the chance the Computer will be able to build it

-For campaigns/scenarios it’s possible to use a CTY file to make the Computer tower rush an opponent at specific co-ordinates (including walling the tower), build proxy military structures or build structures in any specific place in the campaign (such as between flags, or to build a beautiful city that it repairs AND replaces rather than neglects)

Per:
The Per file governs the way the Computer’s unit behave in the game. It is a list of numbers that influences how villagers, military units and resource are treated and managed. In many ways the Per is the most mysterious (and buggy) aspect of the Computer player. Many of the numbers do nothing at all and some completely break the Computer player if they exist in the Per file. The below is a few tricks I have learned to make the Computer behave reasonably. Please take all of these with a grain of salt as the Per files are the trickiest to gain a true understanding of, therefore what is said may be wrong or in error to a degree:

The Drop Site distances help the computer to place drop sites fairly efficiently. One drawback of a lower number is that Granaries seem to be produced twice on some well-spaced Berry Bush patches. However raising the number too high means the AI will hunt gazelles and gather other food at a great distance to any dropsite:

163 5 //SNFoodDropsiteDistance
165 4 //SNStoneDropsiteDistance
164 4 //SNWoodDropsiteDistance
166 4 //SNGoldDropsiteDistance

The Civilian number caps are often by default set to an OK number (gatherers at -1/infinite, explorers at 1 or 2) but Builders is often a very low number. Setting it much higher means a computer can construct as many buildings as it can afford at the time and increases efficiency especially in producing the first round of farms:

4 20 //SNCapCivilianBuilders

Minimum and maximum town sizes are important as making them as low/high as possible lets the computer produce drop sites in the late game next to important distance resources more reliably:

73 1 //SNMinimumTownSize
74 144 //SNMaximumTownSize

This is also true for the Storage Pit and Granary max distances:

86 144 //SNStoragePitMaxDistance
87 144 //SNGranaryMaxDistance

The “Maximum House Before Dropsite” seems to be the only number with much real influence over the Computer’s very early game. It will construct two houses instead of one but this is better than letting it make a lot and chopping wood for too long. This number behaves very strangely:

174 1 //SNMaximumHousesBeforeDropsite

The retask gather amount set to 0 means the computer can freely jump between gather points without sticking to it for too long. I find this makes it slightly more efficient than mandating a certain amount:

148 0 //SNRetaskGatherAmount

For my own Computer players they do not use stone much if at all (towers and walls are inefficient since the Computer can’t dynamically pick the direction an enemy is faced towards, so must build these things in all four directions). Therefore I make the SNMaximumStone to 0. This combined with having nothing that costs stones in the .AI file means they pretty much never mine stone:

192 0 //SNMaximumStone

The Dynamic Gathering is a finnicky system you can’t change much as an end-user. However having the below variables at 1 and 100 respectivley seems to help a little with how it sets its gatherers (past the intial stupid Stone Age woodcutting):

147 1 //SNMostNeededResourceLookAhead
160 100 //SNMaxBuildPlanGathererPercentage

The Maximum Gaia Attack Response number governs how many villagers will be sent to deal with lions or crocodiles trying to eat the Computer’s villagers. Without having this number present in the .per file it can often send EVERY villager it has to kill one lion. I set it to 0 since by the time a villager arrives to help the first will likely be dead anyway, therefore wasting time:

100 0 //SNMaximumGAIAAttackResponse

The military numbers available for the .per file mostly seem to work. Setting the Sentry Distance to a high number can force a passive sort of “territorial” military scouting. Likewise the number governing the number of Transport escort ships works perfectly fine, so a Computer fiercely protecting its transports in a Scenario or Campaign or even Islands Random Map is possible.

Examples:
I currently only have one example Computer player but will be working on more in the future. Using critical research gates in the .ai it should be possible to make effective Tool Age rushers that transition steadily to the Bronze age as well as weaker economic starters/military attackers that boom from multiple Town Centers in the Bronze Age.

Legion:
The Legion Computer player is made for the Roman civilization and plays Random Maps. It plays exclusively land-based maps and will not produces docks or boats.

It produces fourteen villagers in the Stone Age (14 vils + Tool Age is the ideal number for a single 7 Berry Bush patch as the Computer can’t reliably find second berries or gazelle). As with any AI it will chop wood for the first couple of minutes instead of having a tight House -> Granary -> Villagers build order. This seems to be unfixable for the end-user.

It proceeds to a heavily economic Tool Age and then advances to the Bronze Age (19-22mins) then produces Broad Swordsmen from at least four Barracks. It has a problem in wasting wood and villager time on a second Archery Range due to the Bronze Age research being critical.

It will eventually progress to the Iron Age (~33-38mins unharrassed) and producing fully upgraded Legions from multiple Barracks at a time. From all of my testing it has not failed to reach the Bronze Age and produce swordsmen. It is very reliable.

Using research gates it would be easily possible to make this an effective Tool Age rushing AI to transition into Swordsmen later. It is also possible to force it to build towers to solidify its position if it reaches the last bronze age using a critical research gate without wasting villager time on stone mining or tower building early on.

Download:
Not sure if I am allowed to post links? I can provide a link to anybody who asks if they know where I am allowed to post links.

To find the right directories to put AI, PER and CTY files open your game, click Options -> Open Game Folders then navigate to Game Content -> AI Data. Place any files in their corresponding folders. You can only play with custom AIs when they are selected for a Computer player in the Scenario Editor.

I hope this will help anybody who is making a Campaign or Scenario or whoever is improving the AI for the default Random Maps.

I would also like to suggest adding a toggle in the Custom Game second and Scenario Editor to enable or disable the AI cheating for free resources (so you can pick Hard or Hardest without cheating, or have Easy or Moderate with cheating).

Also having a way to apply the default Random Map AI to a Computer plaer in the Scenario Editor would be nice.

Finally, being able to select your own custom AI for Random Maps would be wonderful.