Town Size Attack

Article written by TOAO_DaVe
Originally published on 07-02-2005 ; updated on 08-17-2014
Tags: AI

Before We Begin

This article has been written specifically on town size attack (TSA), and for people who have already have some knowledge of AI scripting. As far as I know, there are not any basic standard-game scripting tutorials, however I encourage you to look at Berserker Jerker's The AI Tutorial for Scenario Designers, from the Tsuniversity and the AI Documentation, found on your Age of Empires II: The Conqueror's Expansion CD (CD Drive:\DOCS\CPSB.DOC). Also search the AoKH AI and RM Scripting Forum and AIScripters.com's Age of Kings Scripting Forum to find anything else you are looking for (or post a message if you can't find it). Anyway...

Understanding TSA

Introduction

"Town Size Attack" is a method of attacking, but unlike other methods, it does not seem to be intended by the creators of the AI-engine used in Age of Kings. TSA is not used through facts which were made for attacking properly - as it name suggests, you attack using TSA through setting the town size for your AI, which would normally just be for setting the area designated for the AI to build normal buildings (ie, houses, town centres, military buildings, monasteries, blacksmiths, and markets).

Explanation

While testing two AIs, or just playing against a computer sometime, you may have noticed when you built a tower close to them, either all their villagers ran at it to slash it down, or all their military ran towards the tower (ungrouped) to destroy it (this also may happen with other buildings, but only military units will react). The reason for this is that the building which was built in the enemy AI's town was inside the designated "town size" for that AI and the AI engine forces the computer to defend against the enemy buildings within its town, or rather attack them, and with this discovery came the creation of town size attack.

The biggest advantage TSA has over most other forms of attacking is, in my opinion, that TSA seems more coordinated - all units (well, usually all - depends on "lag" in-game) are attacking one certain area or single building, so generally the attack is stronger, and as TSA makes the AI attack the closest enemy building, it means there is no more of soldiers running through the middle of 3 town centres to just destroy a single watch tower on the other side of their town. The less the number of your units killed, the more resources can be spent on even more units, as well as other technologies.

One thing to point out is that TSA only really works on maps where the enemy can be attacked by land.

Basic TSA Script

Okay, so it seems town size attack might be worth a try... but how do you do that?

Increasing Town Size

(defrule
(not (enemy-buildings-in-town))
(strategic-number sn-maximum-town-size 124)
=>
(set-strategic-number sn-maximum-town-size 144)
)

(defrule
(not (enemy-buildings-in-town))
(strategic-number sn-maximum-town-size 104)
=>
(set-strategic-number sn-maximum-town-size 124)
)

(defrule
(not (enemy-buildings-in-town))
(strategic-number sn-maximum-town-size 84)
=>
(set-strategic-number sn-maximum-town-size 104)
)

(defrule
(not (enemy-buildings-in-town))
(strategic-number sn-maximum-town-size 64)
=>
(set-strategic-number sn-maximum-town-size 84)
)

(defrule
(not (enemy-buildings-in-town))
(strategic-number sn-maximum-town-size 44)
=>
(set-strategic-number sn-maximum-town-size 64)
)

(defrule
(not (enemy-buildings-in-town))
(strategic-number sn-maximum-town-size 24)
=>
(set-strategic-number sn-maximum-town-size 44)
)

(not (enemy-buildings-in-town))

We have to check whether there are already any enemy buildings in town or not to allow the AI to defend itself (TSA attacking) against the closest buildings. If this was not checked, the town size would just keep increasing and the TSA attack would be sluggish... and just a mess!

(strategic-number sn-maximum-town-size 24)

This is needed so TSA will act properly - the town size will go up in correct amount of increments, in this case - 20, (the number of tiles) every second (everytime the AI rules are checked) until enemy buildings are found.

(set-strategic-number sn-maximum-town-size 44)

Pretty self-explanatory. Increases the town size by 20 so the AI can increase its search for enemy buildings.

Decreasing Town Size

Below are the rules for decreasing the town size when using TSA after enemy buildings have been found within the town size. These rules are the opposite of the rules for increasing the TSA, and together are used to loop the AI's town size at the smallest size which contains enemy building so this constant attack is always attacking the closest buildings.

(defrule
(enemy-buildings-in-town)
(strategic-number sn-maximum-town-size 44)
=>

(set-strategic-number sn-maximum-town-size 24)
)

(defrule
(enemy-buildings-in-town)
(strategic-number sn-maximum-town-size 64)
=>
(set-strategic-number sn-maximum-town-size 44)
)

(defrule
(enemy-buildings-in-town)
(strategic-number sn-maximum-town-size 84)
=>
(set-strategic-number sn-maximum-town-size 64)
)

(defrule
(enemy-buildings-in-town)
(strategic-number sn-maximum-town-size 104)
=>
(set-strategic-number sn-maximum-town-size 84)
)

(defrule
(enemy-buildings-in-town)
(strategic-number sn-maximum-town-size 124)
=>
(set-strategic-number sn-maximum-town-size 104)
)

(defrule
(enemy-buildings-in-town)
(strategic-number sn-maximum-town-size 144)
=>
(set-strategic-number sn-maximum-town-size 124)
)

Improving/Modifying Your Town Size Attack

You have your basic TSA rules down, and the system is working, but like I named it above, the script is at the moment just a "basic" version. There are many ways on improving and changing the behaviour of AIs when using TSA, as well as completely changing the method of the town size attack.

Turning TSA On/Off

Unless you want your AI to constantly be attacking, the first thing you should think about is when you want TSA to working, and when not.

(defrule

=> (set-goal x y)
)

Obviously you will be puting in certain conditions which must be met for TSA to be turned on.

(set-goal x y)

A goal is the easiest way of being able to control the current state of TSA, among other things. Just change the (enemy-buildings-in-town) condition in the TSA rules to goals at a certain state (ie, on or off), depending on the whether you are increasing or decreasing - remember to make 2 rules for on and off!

Alternatively, you can use obsolete strategic numbers depending on how in-depth your TSA is.

Changing Increment Size

The script I provided above with the basic TSA rules had the town size go up (and down) in increments of 10 tiles, but this means the AI's forces will be split up between all the buildings which are withing this 10-tile range. If the town size went up in increments of 5, the forces would be closer together. For best effect, however, the town size should go up in increments of 2 tiles.

Although your AI's TSA will now have it's forces (hopefully) attacking the very closest enemy building, the time it takes for the AI to actually attack is 5 times slower at increments of 2, than if it were at 10, as it takes that much longer for the AI to get its town size up to minimum value needed.

Timer-based TSA

Now that you have learnt about how TSA works, you can try another popular method of TSA, which uses timers instead of constant increasing/decreasing which works a lot better in certain situations like lag. Timer-based TSA works by increasing the town size up until an enemy building is within the limits, and holds the town size at that amount for a small period of time before the TSA "restarts" from the base value (so, if using the rules I wrote above, the TSA restarts from 24).

(defrule
(not (enemy-buildings-in-town))
(strategic-number sn-maximum-town-size 124)
=>
(set-strategic-number sn-maximum-town-size 144)
)

(defrule
(not (enemy-buildings-in-town))
(strategic-number sn-maximum-town-size 104)
=>
(set-strategic-number sn-maximum-town-size 124)
)

(defrule
(not (enemy-buildings-in-town))
(strategic-number sn-maximum-town-size 84)
=>
(set-strategic-number sn-maximum-town-size 104)
)

(defrule
(not (enemy-buildings-in-town))
(strategic-number sn-maximum-town-size 64)
=>
(set-strategic-number sn-maximum-town-size 84)
)

(defrule
(not (enemy-buildings-in-town))
(strategic-number sn-maximum-town-size 44)
=>
(set-strategic-number sn-maximum-town-size 64)
)

(defrule
(not (enemy-buildings-in-town))
(strategic-number sn-maximum-town-size 24)
=>
(set-strategic-number sn-maximum-town-size 44)
)

(defrule
(goal 1 0)
(enemy-buildings-in-town)
=>
(enable-timer 1 14)
(set-goal 1 1)
)

(defrule
(goal 1 1)
(timer-triggered 1)
=>
(disable-timer 1)
(set-goal 1 0)
(set-strategic-number sn-maximum-town-size 24)
)

This method of TSA is usually better when it is laggy than the most basic way because as the town size as kept at a certain amount with enemy buildings within it for more than one second, it gives the AI more time to task its units to attack the building.

Changing Strategic Numbers

You can dramatically change the behaviour of your AI when it uses town size attack by changing the value of defense-based strategic numbers (like sn-sentry-distance and enemy-sighted-response-distance) because, like written above, TSA is really a defense.

You can view a list of all strategic numbers at scripter64's AI reference.

TSA in Scenario Design?

Town size attack can be used in scenarios as long as a town centre for the AI which is using TSA is on the map - you just can map-copy and town centre into the corner of the map or have it hidden somewhere else.

Wrapping It Up

TSA is a very powerful method of attacking for your Ai, using quite an annoying feature of the Ai engine to your advantage. However, that is not to say that town size attack in comparison to other methods of attacking is always superior. Town size attack can create issues when there are many enemies on a large map as forces are easily divided, and as mentioned earlier, town size attack really only works on land maps well. Other methods of attacking like using (attack-now) and attack-groups are able to get passed these problems. Likewise, TSA can get passed problems created from using the other 2 methods and so I encourage you to experiment with different methods of attacking, and combine 2 or more - see what happens.

I wanted to inform people of town size attack and I have done that, so all I have to say now is thank you and good night.