Stronghold Crusader 2 - AI mechanics

The AI in Stronghold Crusader 2 inherits and reworks concepts from previous games.
Unlike previous games, it is well-documented and defined in internally packed .xml files that can be altered and repacked in order to modify the traits of various AI characters. While their behaviour is still hard-coded into the core files, modding is available at a much higher pace than those of the previous Stronghold games.
==An AI player==
AI players have the following components:
*Core behaviour: AI players have generic decision and building routines that are equal to each player. It is built in Lua scripts and are highly in line with other core components of the game (graphics, game flow, etc.).
*Castle templates: Crusader 2 AI characters have several pre-defined castles in .xml files. All characters have a build area, which buildings and unit markers can be placed upon. The AI chooses the most fitting castle template on its main estate.
*Media files: Bink videos are used for interacting with the player. These are included for each character and can be replaced as needed. The RAD Tools. Bink Video Converter can be used to produce a .bik video from the most popular formats.
Each AI player has a behaviour definition convering various gameplay aspects in a huge XML file. The following sub-sections cover all of them without precise technical info.
==Castles and building==
Each AI has an internal list of castles that can be used for a given character. Castle templates are kept in separate XML files each, rather than the AI player definition file.
===Castles===
Much like the previous games, AI players cycle over a checklist defining which building to build. Each build step ("turn") places a building and/or unit markers to defined places in the template, otherwise to the nearest available space on the map. Pre-defined buildings will always count as occupied, so that non-defined buildings will not be placed in place of these. Unit markers define points where specific units go.
===Build orders===
There is another checklist that defines the build order for the AI. This includes a series of build steps, in which order the AI will try to build buildings. Steps can be set to non-skippable, which means that the AI will not build the next building until it placed the building for the current step. Building and rebuilding is done in regular intervals.
AI characters have building limits, which define how many of a specific building can be built at most.
The AI is not limited to the faction specific building types (Crusader and Arab) and therefore can produce any building, if defined in their templates or build orders.
==Economy==
===Trading===
Much like previous games, the AI characters have buy and sell rules for specific goods. They can sell and buy above/below a certain quantity with variable amounts. They trade in bulks, all goods simultaneously as they are able to. If no rule is set for a particular goods type, it will not be altered.
===Popularity and taxing===
The AI quickly adjusts taxes, rations and ale/candle consumption to optimize income.
The AI usually tries to juggle population between the highest number of peasants and the lowest defined popularity level. There are three popularity levels, of which the lowest corresponds to the greediest strategy (just before peasants start to leave) and the highest determines how fast the AI desires to refill population.
It is possible to toggle whether an AI should factor religion and ale in their tax optimization.
===Production===
The AI normally operates buildings. When workers are consistently killed at a building, they will temporarily halt production there in order to not waste peasants continuously. They will not repair buildings with the repair tool, however.
==Military==
The AI is designed to be very flexible in terms of unit compositions and strategies. While the core behaviour is defined for each unit group, the AI can be given a unique personality and therefore it can be challenging to find optimal and fitting compositions for each of them.
===Group concept===
The AI uses pre-defined groups, which have their own purposes and scheduling. Some groups are sent towards the enemy, while others are kept at the castle or the main estate. Each group has a composition, which are built into clans (a group of units). Group definitions also define the order, based on which the AI tries to assemble clans.
When a clan reaches the maximum number of units (can also be capped by a percentage of the unit cap), it is sent to its destination. Sometimes the AI can deviate from this number, its decision frequency, as well as extend the limit over time, as clans are sent into battle for example.
Each group has its own activity time and an assign percentage. Higher % means that the AI is more likely to assign the next recruited unit to a particular group.
===Group types===
The following unit groups are used:
*Harassment groups: perhaps the most often encountered teams among any AI. These units form up gradually at home and then are sent at enemy targets to wreak havoc. If the group contains horse archers, the entire group is microed away to make use of the horse archer's firing ability. Harassing teams have a player targeting strategy and a general targeting strategy. Up to two different harassing groups can exist. The following targets are eligible:
**for players: closest, richest, weakest, random or human
**for general targets: farms, resources, workshops, town buildings, storages, troops, granary or any target
*Siege groups: these groups are aimed to work through a target's defenses and kill the Lord. Siege groups always form up at their recruitment buildings and then are sent to the closest estate, where a siege camp is erected and siege engines are sent with the army in an all-out attack. Siege groups can be directed the same way as harassing groups, targeting a specific player. The siege is halted when the number of troops fall under a certain % of the attacking force size.
*Defenders: defines the individual troops that are sent to markers. There are melee, missile and patrolling defenders. Patrols will visit patrol markers throughout the castle. If the cap percentage is reached, further points are not populated, resulting in unmanned wall segments and towers.
*Response force: these groups consist of mobile defenders, who patrol buildings and aggressively attack provoking units that cause damage or cross a friendly estate border. They tend to wait at their recruitment building before the group gets large enough to be sent out to patrol.
*Village force: this group is sent out to capture village estates on the map and permanently garrison.
*Emergency force: this group is a non-natural way of spawning units. It spawns at the keep entrance in the following situations:
**the AI has hostile units at its keep. This group stays at home and is meant to fight for the Lord as a last resort.
**the AI agrees to help or attack. Depending on the situation, the force is sent to either a player's keep entrance or the target enemy. In the latter case, the force is rather clumsily sent in a marching formation and does not react to enemy fire.
The AI also has simple starting troop definitions for different game modes (which are based on the starting stocks).
===Unit management===
The AI has a limited unit management logic that is done per unit, which can be pretty efficient in some cases.
*Melee units generally target any attackers that hit them. They can indefinitely chase units and will not switch to another target until they are disposed of.
*Ranged units follow the same strategy, as melee units, however they will try to fire when getting in range. They will not move away from other melee units that might be attacking them on the way.
*Harassment groups with horse archers will try to micro away and kite any unit within range, using the horse archer to fire while moving. Even after the horse archers killed, units will try to maneuver indefinitely without firing.
*Mantlets will periodically relocate near friendly troops in order to provide protection.
*Fire ballistae will slowly move toward enemy units and snipe them from a distance.
*Catapults, rams and trebuchets will slowly move toward enemy buildings and try to level them. Trebuchets will redeploy on suitable places if no building remains within range.
*Archers and Arabian bowmen will try to fire at pitch ditches as the enemy steps on them.
==Diplomacy==
The AI will have limited interaction with its human allies, just like in Stronghold Crusader. It will randomly send requests to the player when it experiences heavy enemy attacks or falls short in a particular goods supply.
However, when the human contacts the AI, it will have a mood variable that fluctuates as time goes on and the human player accepts/rejects its queries. Mood can range from 0% to 100%, where higher percentage means the AI is more likely to comply to a request. If the AI agrees on doing something, its mood will also be cut. Goods are sent with a variable delay. Sending goods to the AI, when not requested, does not improve its mood.
AI players do not conduct diplomacy with each other.
==Difficulty==
The AI has two difficulty levels: easy and normal.
On "easy" difficulty, the AI waits three times longer plus 150 seconds before it starts training troops, can train additional troops five times slower, as well as it gets only 75% of its (inflated) income.
On "normal" difficulty, the AI is not altered outside its granted benefits and modifiers.
==Cheats==
The AI can be assisted by a few extra options to gain an advantage in economy and troops:
*Periodic gold boosts: every 25 seconds, the AI can get a net amount of gold to its reserves, plus a percentage of the base amount.
*Goods discount: certain goods can be bought with a discount percentage on the market. This is prevalent for weapons and certain base materials with huge buying prices.
*Defining an emergency force.
*Defining a fast recruitment period. In the beginning of the game, for a fixated time period the AI can recruit troops at a higher rate, disregarding weapon stocks and peasants.
Category:Mechanics