Customizing the Default AI Using WML
This page describes how the behavior of the default (RCA) AI can be customized using simple WML commands. The descriptions below assume a basic understanding of how the RCA AI evaluates moves, what candidate actions, aspects and goals are, and how they interact. If you do not know these things yet, check out the RCA AI page.
This page contains:
- Descriptions of the aspects and goals of the default AI
- Instructions for setting them up at the beginning of the scenario in a [side] tag
This page does not contain:
- Instructions for setting up aspects dynamically using Lua.
- Instructions for changing aspects and goals mid scenario, although for many of them (for example, for all standard aspects) the methods are almost identical
- Instructions for some of the more complex AI customization tasks.
Defining Aspects and Goals of the RCA AI
Aspects and goals of the default (RCA) AI can be defined at the beginning of a scenario with [ai] tags inside the [side] tags. They are used to customize certain aspects of the AI. The [ai] tag, when used for this purpose, takes the aspects themselves as arguments, as well as the following general keys:
- time_of_day: (string) The time(s) of day when the AI should use the parameters given in this [ai] tag. Possible values are listed in data/core/macros/schedules.cfg (see also TimeWML). Note that it is the id (not the name) of the time of day that needs to be used here. Also note that this aspect did not work for a long time due to a bug (versions 1.7.4 - 1.11.6). It has been fixed for BfW 1.11.7.
- turns: (string) During which turns the AI should use the parameters given in this [ai] tag. This takes the same syntax of dashes (-) and commas (,) as is described under Filtering Locations in FilterWML, except of course they apply to turns not locations.
- ai_algorithm: (string) Allows an alternate AI algorithm (cannot be created with WML) to be used. Besides the default, the game only comes with idle_ai, which makes the AI do nothing and can be used to create a passive, unmoving side. Cannot be applied only to a set of turns or a given time of day using the keys turns and time_of_day, but must be given either in an [ai] tag without the aforementioned keys or in the [side] tag outside all [ai] tags.
An example of a simple use of the [ai] tag to set AI parameters (aspects) is
[ai] time_of_day=dusk aggression=0.99 caution=0.01 [avoid] [filter] side=2,3 [/filter] radius=2 [/avoid] [/ai]
Important Note: If you use a different AI engine (FormulaAI or LuaAI) for a side, setting aspects in the [side] tag might not work due to a bug in the AI setup mechanism (or it might; there's no general rule that catches all cases). In that case, you might have to use [modify_side][ai] in a prestart or start event instead. If unsure, you can use
:inspect in debug mode to verify whether an aspect was changed successfully.
List of AI Aspects
- advancements: (string) Defines a list of unit types to which AI units can advance. If a unit type is listed here, units that can advance to this type will only advance to this type, even if they have, in principle, several advancement options. As an example, if this aspect is set to 'Javelineer,Elvish Hero', then all Spearmen advance to Javelineers, and all Elvish Fighters advance to Elvish Heroes. All other units follow their usual advancement choices as determined by the AI. Important notes:
- This is a simplified version of the more complex (and arguably more useful) application of a dynamic Lua advancements aspect, as described here.
- The advancements aspect only takes effect during the AI turn, that is, on offense only, not on defense.
- Listing more than one advancement unit type for a given unit still results in only one of the types being used (generally the one listed last).
- aggression=0.4: (double: ranging from -infinity to 1.0) This key affects how the AI selects its attack targets. The higher the value, the more likely the AI is to attack even if odds are not in its favor. Applies to combat CA only.
- Important: do not set aggression to values greater than 1 as this makes the AI prefer attacks in which it receives more damage (see below) unless that is specifically the effect you want to achieve.
- In the attack evaluation, each attack (this includes attack combinations of several AI units against the same enemy unit) is assigned a score. The attack with the highest score is done first. Then the next attack is evaluated, until no attack with a score greater than 0 is found any more. (Note that this attack score is different from the combat CA score, which is always 100,000 as long as an individual attack score >0 is found. The combat CA score is zero if the highest attack score is <=0).
- The attack score is a complex combination of many different aspects of the attacks. Positive (additive) contributions to the score are things like the value (cost and XP) of the target, the chance to kill the target, whether it is already wounded, how much damage the attack is likely to inflict, etc. Negative (additive) factors include how much damage the AI's units are likely to take, how valuable they are, how exposed they will be after the attack, etc. There are also multiplicative factors that are used if the attack target is a threat to the AI's leader etc.
- All the negative contributions to the score are multiplied by '(1-aggression)'. This means that:
- If 'aggression=1', no negative contributions are added to the score. Thus, the AI disregards damage done to its own units and selects attacks based solely on the damage it can do to enemy units. If the AI can inflict 1 damage and take 0, or inflict 2 damage and take 20, it will take the latter option.
- The smaller the value of aggression, the more weight is put on value of and potential damage to the AI's units as compared to the attack target.
- Roughly speaking, 'aggression=0' results in the AI valuing its units approximately as much as the enemy units. This is not a one-to-one relation, but can be used as an approximate guideline.
- Very large negative values of aggression mean that the value of the AI's units is much more important than that of the enemy units. As a result, the AI never attacks unless it will receive no damage in exchange.
- The rating for damage received in an attack actually becomes a positive contribution to the score for values of aggression larger than 1. This usually does not result in sensible behavior and values greater than 1 should therefore not be used.
- Note: aggression is always set to 1.0 for attacks on units that pose a direct threat to the AI's leader. Currently this only means units adjacent to the leader.
- [attacks]: Filters the units considered for combat, both on the AI and the enemy sides. Applies to the combat and high_xp_attacks CAs only. It cannot be set in the same way as the other aspects and is therefore described in a separate section below.
- [avoid]: Makes the AI avoid specific locations. The AI never moves a unit to these locations except for trying to move its leader to a keep or toward [leader_goal]s, and thus applies to all CAs except move-leader-to-goals and move-leader-to-keep.
- StandardLocationFilter: The locations for the AI to avoid. Do not use a [filter_location] tag.
- caution=0.25: (double) Defines how cautious the AI is in several ways. It determines whether the leader should move toward [leader_goal], if attacks are worth moving onto less favorable terrain, whether units should retreat, and whether the AI should move units toward targets individually, as groups or not at all. Affects several CAs (listed in the order of their evaluation scores):
- Move-leader-to-goals CA: If max_risk is not set in [leader_goal], its default value is '1-caution'. This determines whether the leader takes the next step toward his goal. See description of [leader_goal].
- Combat CA:
- During the evaluation of attacks, the AI considers whether the attacking units could get onto more favorable terrain if they didn't attack but moved somewhere else instead. The difference between the two terrain ratings, together with a number of other factors, determines the "exposure" rating of the units. This exposure is then subtracted from the 'attack score' as described for aggression above.
- The exposure rating also contains a multiplication by caution, meaning that 'caution=0' results in exposure not being taken into account, and that it becomes more and more important for larger values. In other words, the higher the values of caution, the more reluctant is the AI to attack from unfavorable terrain. (Note that exposure is one of the negative contributions to the attack score as described for aggression, and therefore is also ignored if 'aggression=1' is set.)
- If the AI leader is used in an attack, the AI always uses 'caution=2' for the evaluation of that attack.
- Retreat CA:
- If caution is greater than 0, there is an evaluation of forces for the map location a unit stands on. This is basically the sum of damage that can be done to that location by either side, reduced by terrain defense and relative hitpoints if attackers don't have full health. A retreat takes place, if caution × their_power > our_power. There is also a terrain factor involved if the attacker is not on optimal terrain, similar to the exposure described above for the combat CA.
- So let's say the AI has its default caution of 0.25. Then the enemy forces have to be at least 4 times as strong before the unit retreats. For a caution of 1, as soon as the enemy is stronger, the unit retreats.
- The AI never retreats if caution is set to 0 or lower.
- Move-to-targets CA:
- If grouping for the AI is enabled and the path along which to move toward a target is considered to be dangerous, caution has an influence, too. "Dangerous" mainly means that there is a good chance for a unit to be killed if it's by itself. In that case, the AI compares its units to the enemy units and based on the result moves forward or not. All units that can reach the next location of the move are considered. The formula for deciding whether to move toward the target as a group is our_strength / their_strength > 0.5 + caution. If this condition holds true, units are moved toward the goal as a group, otherwise they try to group together in a location favorable for a attack on the enemy during the next turn.
- So if caution is 0.5, the AI side needs to be at least as strong as the enemy. If it is 0, the AI moves toward the target, even if the enemy is up to twice as strong as the AI. Setting caution to 1.5 means the AI needs to be at least twice as strong as the enemy.
- The AI also considers retreating units during the move-to-target phase based on criteria similar to those for the retreat CA.
- [goal]="": Defines units or locations as move targets (not attack targets!) for the AI. Applies to move-to-targets CA only. <-- That is extremely important to understand, in particular the fact that the [goal] tag has no influence whatsoever on attacks. See the dedicated section on the [goal] tag below for details.
- grouping="offensive": (string) How the AI should try to group units. Applies to move-to-targets CA only. Possible values:
- offensive: Makes the AI try to group units together before attacking.
- defensive: Makes the AI group units together very conservatively, only advancing them much beyond its castle if it has overwhelming force.
- no: Makes the AI use no grouping behavior.
- leader_aggression="-4.0": Exactly as aggression, but for units which can recruit. Applies to combat CA only. Note that the move-leader-to-keep CA has a higher score than the combat CA. A leader therefore usually only attacks if he is on his keep at the beginning of the turn, otherwise he moves toward the closest keep instead, even with leader_aggression=1.
- [leader_goal]="": Makes the AI try to move its leader to a specific location. Applies to move-leader-to-goals CA only.
- x, y: The location toward which the AI should move its leader.
- auto_remove=no: (bool) If 'no' (default), the AI moves the leader to the goal, after which he stays there until [leader_goal] is removed manually. If 'yes', the leader_goal is removed upon the leader getting there. Important: this only works if id is set correctly (see the next bullet).
- id="": (string) An internal id key of the [leader_goal] tag. An id is required for auto_remove to work. However, setting this id does not automatically set the id of the [leader_goal] facet. Thus, in principle for this to work, you need to set the id of the facet as described here and set the id key in [leader_value] to the same value. Since you are probably only going to use one [leader_goal] tag at a time, there is a much simpler way: setting 'id=0' (which refers to the first facet) or 'id=*' (which means all facets) in [leader_goal] allows auto_remove to work without the extra step of setting the facet id. (Version 1.13.5 and later only) Setting this now also sets the id of the [leader_goal] facet, allowing auto_remove to work with multiple leader goals without using the fully-expanded aspect syntax.
- max_risk=1-caution: (double: meaningful values are >=0) How much risk the leader may be exposed to by moving toward the goal. For evaluating this risk, the AI multiplies the leader's hitpoints by this number. The leader is only moved toward the goal if the resulting value is larger than the expected damage the leader is going to take during the next enemy turn. Thus, 'max_risk=0' means he will only move if no attack on him is possible at the target hex of the move. 'max_risk=1' (or larger) results in him moving even if he's almost certainly going to die.
- leader_ignores_keep=no: (bool) If 'yes', AI leaders do not move to the closest keep at the beginning of the turn. Instead, they participate in the move_to_targets candidate action (and all other CAs in which they already participated anyway, of course). For practical purposes this means that:
- Leaders participate in all the moves and attacks in the same way as any other unit of the AI side.
- AI leaders do not recruit except on the first turn if they start on a keep, or if they accidentally end up on a keep. Thus, if a leader is supposed to recruit first and then participate in the action, leader_ignores_keep should be set to 'no' at the beginning of the scenario, and be changed to 'yes' in an event later.
- leader_value=3: (double) A number 0 or higher which determines the value of enemy leaders as targets. Affects move-to-targets CA only (and therefore specifically does not apply to attacks).
- passive_leader=no: (bool) If 'yes', the AI leader never moves or attacks, not even to move back to the keep (unless 'passive_leader_shares_keep=yes' is set) or to attack adjacent units, except to obey [leader_goal]s. Affects all CAs except recruitment and move-leader-to-goals.
- passive_leader_shares_keep=no: (bool) If 'yes', lets the AI leader moves off the keep to share it with allied leaders (if they can reach it next turn) if 'passive_leader=yes' is set. He also returns to keep to recruit when possible and attacks adjacent enemy units.
- recruitment_diversity, recruitment_instructions, recruitment_more, recruitment_pattern, recruitment_randomness, recruitment_save_gold: These aspects can be used to customize recruiting with the new recruitment CA introduced in Wesnoth 1.11. They are described on a separate page.
- scout_village_targeting=3: (double) The AI multiplies the value of village targets for scouts by this value. Affects move-to-targets CA only.
- simple_targeting=no: (bool) If 'yes', the AI moves its units toward targets one by one (sequentially), without considering whether another unit might be better suited for the current move or target. If 'no' (the default), all units are considered for all targets. This is slower, but might result in better moves. Affects move-to-targets CA only.
- support_villages=no: (bool) Trigger a code path that tries to protect those villages that are threatened by the enemy. This seems to cause the AI to 'sit around' a lot, so it's only used if it's explicitly enabled. Affects move-to-targets CA only.
- village_value=1: (double) A number 0 or higher which determines how much the AI tries to go for villages as targets. Affects move-to-targets CA only.
- villages_per_scout=4: (int) A number 0 or higher which determines how many scouts the AI recruits. If 0, the AI doesn't recruit scouts to capture villages. Affects recruitment CA only.
Removed AI Aspects
The following AI parameters (aspects) can still be set, their values can be retrieved, and they can be viewed in the gamestate inspector dialog, but they do not have an effect in the RCA AI code any more. Some other parameters will also likely be removed in the future. We will update this list accordingly. (Version 1.13.5 and later only) Except for attack_depth, these have now all been removed.
- attack_depth=5: (int)
- number_of_possible_recruits_to_force_recruit=3.1: (double)
- recruitment: This aspect was used to customize the recruitment of the old default AI (the one used before the RCA AI). This recruitment code is not used in the RCA AI any more. Setting recruitment instructions with this aspect is therefore meaningless. Use the recruitment_instructions aspect instead.
- It is, in principle, still possible to use this aspect together with the recruitment stage, but this is discouraged for the reasons given at this link.
- recruitment_ignore_bad_combat=no: (bool)
- recruitment_ignore_bad_movement=no: (bool)
AI Targets and Goals
AI targets are used in the move-to-targets candidate action (CA) to move the AI's units toward selected units or locations. The AI engine automatically selects all enemy leaders, enemy units that pose a threat to the AI leader and unowned or enemy-owned villages as targets and assigns them certain base values. Additional targets can be defined using the [goal] tag.
It is very important to realize that these targets apply to the move-to-targets CA only and have no influence on other CAs. In particular, they have no effect whatsoever on which enemies the AI attacks. This is often a source of confusion for scenario creators. More background information on RCA AI targets and goals can be found here. The sections below only describe how to set up goals in order to add targets for the move-to-targets CA.
The [goal] Tag
The [goal] tag specifies target units or locations toward which the AI should move its units in the move-to-targets CA. The following keys/tags can be used:
- name="target": (string) The following values are possible and result in different types of targets, as shown in the examples below:
- target: The (default) target goal specifies target units (not necessarily enemy units) toward which the AI should move its units. (Version 1.13.5 and later only) target_unit is now a synonym for this.
- target_location: Specifies target locations toward which the AI should move its units.
- protect_location: Specifies locations that the AI should protect. Enemy units within the specified distance (protect_radius) of one of these locations are marked as targets with the provided value. Note that the AI will not station any units around the protected locations. It will only send units toward enemy units that come within protect_radius of them.
- protect_unit: Specifies units (of all sides) that the AI should protect. Enemy units within protect_radius of one of these units are marked as targets with the provided value. Note once again that the AI will not place units around the protected units if there are no enemies nearby.
- protect_my_unit: (deprecated)
Specifies units from the AI's own side that the AI should protect. (This is basically the protect_unit goal with an implied side= in the filter, restricting matching units to the AI's side.) Enemy units within protect_radius of one of these units are marked as targets with the provided value. Note once again that the AI will not place units around the protected units if there are no enemies nearby.
- lua_goal: A dynamically calculated goal written in Lua. See here for details on how this works.
- [criteria]="": Contains a StandardUnitFilter (for target, protect_unit or protect_my_unit) or StandardLocationFilter (for target_location or protect_location) describing the targets.
- value=0: (double) The value of the goal.
- protect_radius=20: (int) The protection radius. Applies to protect_location, protect_unit and protect_my_unit.
Examples of [goal] Tag Usage
[ai] [goal] [criteria] #NOTE: this is a SUF, because we're targeting a unit side=3 [/criteria] value=5 [/goal] [/ai]
[ai] [goal] name=target_location [criteria] #NOTE: this is a SLF, because we're targeting a location x,y=42,20 [/criteria] value=5 [/goal] [/ai]
[ai] [goal] name=protect_location [criteria] #NOTE: this is a SLF, because we're protecting a location x,y=42,20 [/criteria] protect_radius=16 value=5 [/goal] [/ai]
[ai] [goal] name=protect_unit [criteria] #NOTE: this is a SUF, because we're protecting a unit side=3 [/criteria] protect_radius=16 value=5 [/goal] [/ai]
How to use protect_unit to protect the own leader (We are side 2)
[ai] [goal] name=protect_unit [criteria] side=2 canrecruit=yes [/criteria] protect_radius=8 value=5 [/goal] [/ai]
Deprecated AI Targeting Aspects
The following AI targeting parameters currently still work, but have been superseded by the [goal] tag. They should not be used any more as they will likely be removed at some point. These tags only apply to the move-to-targets CA.
- [target]="": Deprecated. Any number of [target] tags can be used to set targets for the AI. For anything related to 'values', set them relative to other targets. An AI is willing to dedicate twice as many resources and travel twice as far to get to a target worth '2.0' as for a target worth '1.0'. Applies to move-to-targets CA only.
- StandardUnitFilter: Do not use a [filter] tag.
- value=1: (double) A number greater than 0 (default=1) which determines how much the AI tries to move toward units which pass the filter.
- [protect_location]="": Deprecated. Gives the AI a location to protect. Note that the AI does not station any units around the location, it only sends units to attack any enemy units that come within the guarding radius of the target. Applies to move-to-targets CA only.
- x, y: Standard coordinates. These indicate the location the AI is protecting.
- radius: The radius around it to protect (0 indicates a single hex). (Version 1.13.5 and later only) This key has been changed to protect_radius, though it's preferred to change old code to use the [goal] tag instead.
- value: The importance of protecting this location.
- [protect_unit]="": Deprecated. Gives the AI a set of units to protect. Note once again that the AI does not place units around the protected units if there are no enemies nearby. Applies to move-to-targets CA only.
- StandardUnitFilter: The unit(s) to protect. Do not use a [filter] tag.
- radius: The radius around it to protect (0 indicates a single hex). (Version 1.13.5 and later only) This key has been changed to protect_radius, though it's preferred to change old code to use the [goal] tag instead.
- value: The importance of protecting this unit.
- protect_leader=2.0 and protect_leader_radius=10: Deprecated. Target any enemy units that come within 'protect_leader_radius' of the AI leader with a value of 'protect_leader'. Applies to move-to-targets CA only.
Filtering Combat with the attacks Aspect
The attacks aspect lets us filter the units considered by the combat and high_xp_attacks candidate actions. Units on the AI side can be selected with the [filter_own] tag and enemy units are filtered via [filter_enemy], both of which take a StandardUnitFilter. Only units defined in these tags are considered as attacker/target pairs. To define, for example, an attacks aspect in which units from the elvish sorceress line are the only attackers, and undead units are the only targets, use
[ai] [aspect] id=attacks [facet] invalidate_on_gamestate_change=yes [filter_own] type=Elvish Sorceress,Elvish Enchantress,Elvish Sylph [/filter_own] [filter_enemy] race=undead [/filter_enemy] [/facet] [/aspect] [/ai]
Several important notes:
- The attacks aspect is a composite aspect and cannot be defined using the simple syntax of the other standard aspects. See here for a description of the syntax of the example.
- This only works if
invalidate_on_gamestate_change=yesis set. The reason is that the attacks aspect internally contains information about all available attacker/target pairs. These need to be recalculated after each move. This is explained here.
- If [filter_own] or [filter_enemy] are omitted, the selection defaults to all units of the respective sides.
- Most importantly: The above example results in sorceress-line vs undead attacks being the only attacks done by the AI. No other attacks are executed, no matter how advantageous their outcomes may be. There is no simple way around that, but it can be accomplished by one of the following means:
- Setting up the [filter_own] or [filter_enemy] StandardUnitFilters to adapt dynamically to the situation on the map.
- Adding a Lua candidate action which dynamically adjusts the aspect depending on the current situation on the map. An example of dealing with this is given in the Micro AI test scenario Protect Unit.
- Using dynamic Lua aspects is in principle also possible, but far from easy due to the complex tables this aspect returns.
Though the above code still works in 1.13.5 and later (and will continue to work indefinitely), it can also be simplified a little:
[ai] [attacks] invalidate_on_gamestate_change=yes [filter_own] type=Elvish Sorceress,Elvish Enchantress,Elvish Sylph [/filter_own] [filter_enemy] race=undead [/filter_enemy] [/attacks] [/ai]
See also: Wesnoth AI