Stronghold Crusader - AI mechanics

The AI in Stronghold Crusader is generic for all 16 characters. Their behaviour is hard-coded into the game and cannot be altered. There are several utilities however, which give some freedom for modding.
==Base concepts==
*Hard-coded behaviour: the AI's decision making, unit management, etc. is programmed in the game and its code cannot be extracted nor modified, therefore it is not possible to be edited.
*Castle templates: the Crusader AI uses external files for castle building and creating a base. These files are created by an AIV Editor, which can be downloaded for free.
*Media files: Bink videos are used for interacting with the player. These media are exclusive for RAD Tools Inc. and they sell a video converter program, usually not for free.
==Decision making==
The Crusader AI has a number of actions which are predictable, yet effective to an extent. These are generally the same for all characters, even if it is not easily noticed for some of them.
The AI has five stances, which determine a set of actions. It alternates between them depending on the situation, but always uses one from the following:
*Natural: in this stance the AI behaves normally. It builds buildings and gathers units for harassment and attacks. It also reacts to enemy provocations, repairs walls, supplies siege equipment used for castle defence and digs/redigs their moat or an allied moat if it has to.
*Siege: when an army is built up, the AI sends it forth and besieges an enemy castle. This usually the weakest enemy or one directly facing them on the map. While there is little to many units standing, this stance is active and the AI uses this time to recruit patrols and make them wander around friendly territory. However they won't produce any troops for the keep during this period.
*Idle: the AI sometimes has a brief pause periodically, but it can also use pre-determined pauses injected in the castle template file. The AI only recruits units for defense and counterattacks and it does not place any buildings. Since it spends much less money on goods and recruitment, it is the most economic state of the computer player.
*Recovery: the AI uses this stance when it is very unpopular and needs to recover from this. It sells most goods and keeps a low stockpile in order to rebuy food (or in the Wazir's case, wheat and/or flour) and it even hands out bribes until it has stabilized its popularity. If they have run out of gold, they will demolish all buildings that are worth gold and stone, and if very desperate, wood, to try and regain some gold.
*Last stand: when there is a huge army approaching, the AI prepares for a final fight. It immediately (and gradually) shuts down production, sells most goods and recruits as many troops as possible. It also brings every unit home to defend the Lord, including those who may have just left the castle to take part in a siege. Soon enough, the AI will demolish all buildings that are worth gold and stone (apart from barracks), and if needed wood (apart from granary, marketplace and mercenary post) to gain even more gold. It will also call for help of its allies.
==Economy==
Each AI character has a pre-determined set of goods they can/will produce, use or trade. They will not always produce the goods they use, but rather buy them (the Wolf buys in bows and Phillip buys in stone in spite of producing no bows and stone respectively).
The AI has an upper limit and a lower limit for selling goods. By default, the upper limit is valid: the AI will wait for the resources to pile up then it immediately sells surplus resources as they come in. The AI can sell and buy varying quantities, including those that cannot be divided by 5 (unlike the human player, who has to trade by 5). On the other hand, it takes one transaction about 1 second to complete for the AI. The AI's goods are also visible during the entire game, regardless of being allied or not with human.
The lower limit is activated when the AI enters 'recovery' state. This remains active until the AI has stabilized its popularity.
Beside goods, most AIs that create units at the barracks (with the exception of the Marshal, Frederick, Abbot and Emir) also buy in weapons for the armoury to speed up recruitment. They always buy in small quantities, however they buy in greater packages when they are making their last stand. They also sell surplus weapons to free up armoury space and make gold.
The AI also has a preset limit for goods, above which they will hand out resources to an ally upon request. Some computer players will give all their goods they have, others may be stingy with it and only give food. They will however, they will transfer as many goods as possible if they are requested to send when they are under siege. This is understandable, as the ally can use up these resources to recruit more soldiers, thus increasing the chance of the ally being helped out.
As an ally, the AI itself will request goods from human players (AI players do not trade with each other), if a buying attempt of its fails. It will request twice the goods it attempted to buy, but it will not come up with another request until the previous one is not over.
Note: the AI players' requests can be ignored if one leaves all requests pending, since the AI does not care about these requests' urgency.
==Unit management==
The AI recruits units and tries to use them most effectively. It also has some tactics that it employs on the battlefield. It recruits units, which are preset for the following purposes:
*Strike force: these troops are always recruited first and immediately and the AI will not stop until the entire strike force is standing. The strike force is sent out every time the AI's properties or units are under attack. They are also the best micromanaged groups of the AI.
*Defensive troops: these units are randomly recruited and they are sent to preset locations. They are stationary troops and the AI replaces them if they fall. Siege and tower engines will autoattack, engaging every building and unit they catch within range. If foot soldiers do not have access to their defense points, they will wait on the keep until they can get there. Siege tents need a 3x3 tiles space to build on and they will not be sited if there is little space or the terrain prevents them from it (such as the terrain or buildings/vegetation occupy their space).
*Raiders: the AI forms small groups of these troops. These units patrol friendly buildings until their master decides to send them out to take down random units/buildings. Knights, macemen and slaves prioritize key buildings, quarries, farms and mines. Advanced characters also create fire ballistae or catapults for harasses, every time they have 500+ gold.
*Patrols: a rare sighting at most AI lords. Patrolling units are recruited while the sieging army is outbound. Their purpose is to wander friendly territory and guard it.
*Siege force: this is the AI's attempt to attack. These armies consist of more men than any other group and they are the best organized groups. The AI will methodically work its way through enemy defenses until it gets to the lord.
The AI will engage any hostile units if its troops get in range. It will try to focus down one target a time and even attacks units hidden behind buildings, using an area attack. A siege force marching will periodically stop to attack until the target has either moved out of range or is dead. Stationary units will not try to run away and counterattack. Some AI players might send out their lord to clear up small batches of forces.
Some moat digging units, or a higher proportion of these units if they are already used, are added to a siege if the target has a moat present. The exception to this is the Sultan, who does not send moat digging units such as slaves and whose army just stands still at its gathering point if an enemy has a complete moat. The force will then split up: some units will go destroy enemy buildings, while the rest will go and fill in the moat or start destroying walls until they carve a path in the enemy stronghold. Then almost every unit will be sent to dispose of the Lord. Ranged units will simply enter into range and try to attack enemy defenders from there. They systemically move closer to the keep until they have targets. When victory is achieved or there are very few sieging units left, the siege force is recalled to the keep.
==Notable differences from human players==
*The AI can trade in varying quantities, where players can only trade in packs of 5.
*Wall segments are sometimes free for the AI when lacking stone, notably in the beginning of the game. Stone is still depleted from the stockpile however.
*Assembly points for the keep can indefinitely hold multiple troops on the same tile until their units move.
Category:Mechanics