<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.wesnoth.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mattsc</id>
	<title>The Battle for Wesnoth Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.wesnoth.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mattsc"/>
	<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/Special:Contributions/Mattsc"/>
	<updated>2026-04-14T00:02:42Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.31.16</generator>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=LuaAPI/wesnoth/interface&amp;diff=69716</id>
		<title>LuaAPI/wesnoth/interface</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=LuaAPI/wesnoth/interface&amp;diff=69716"/>
		<updated>2022-07-09T13:48:16Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* wesnoth.interface.add_overlay_text */ add wesnoth.interface.handle_user_interact&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;div class=&amp;quot;tright&amp;quot;&amp;gt; __TOC__ &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|0}}&lt;br /&gt;
&lt;br /&gt;
The entire interface module is only available in the game. It is not available to plugins or map generators.&lt;br /&gt;
&lt;br /&gt;
== Interface functions ==&lt;br /&gt;
&lt;br /&gt;
=== wesnoth.interface.delay ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.interface.delay'''(''milliseconds'')&lt;br /&gt;
&lt;br /&gt;
Delays the engine for a period of time, specified in milliseconds.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=lua&amp;gt;&lt;br /&gt;
wesnoth.delay(500)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== wesnoth.interface.deselect_hex ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.interface.deselect_hex'''()&lt;br /&gt;
&lt;br /&gt;
Reverses any highlight_hex call, leaving all locations unhighlighted. Takes no arguments.&lt;br /&gt;
&lt;br /&gt;
=== wesnoth.interface.highlight_hex ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.interface.highlight_hex'''(''x'', ''y'')&lt;br /&gt;
&lt;br /&gt;
Draws an outline around the specified hex.&lt;br /&gt;
&lt;br /&gt;
=== wesnoth.interface.select_unit ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.interface.select_unit'''(''x'', ''y'', [''show_movement'', [''fire_events'']])&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.units.select'''(''unit'', [''show_movement'', [''fire_events'']])&lt;br /&gt;
* {{LuaGameOnly}} ''unit'':'''select'''([''show_movement'', [''fire_events'']])&lt;br /&gt;
&lt;br /&gt;
Selects the given unit in the game map as if the player had clicked on it.&lt;br /&gt;
Argument 3: boolean, whether to show the movement range of any unit on that location (def: true)&lt;br /&gt;
Argument 4: boolean, whether to fire any select events (def: false).&lt;br /&gt;
&lt;br /&gt;
This function is available under two different names, but the possible arguments are the same in both cases. In other words, '''wesnoth.units.select''' can be called with a location instead of a unit, and '''wesnoth.interface.select_unit''' can be called with a unit instead of a location.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=lua&amp;gt;&lt;br /&gt;
wesnoth.interface.select_unit(14, 6, true, true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If called without arguments, this function deselects the current unit from the map as long as the mouse cursor is not on its hex. It will continue to be displayed on the UI sidebar in any case.&lt;br /&gt;
&lt;br /&gt;
=== wesnoth.interface.float_label ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.interface.float_label'''(''x'', ''y'', ''text'')&lt;br /&gt;
&lt;br /&gt;
Pops some text above a map tile.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=lua&amp;gt;&lt;br /&gt;
wesnoth.float_label(unit.x, unit.y, &amp;quot;&amp;lt;span color='#ff0000'&amp;gt;Ouch&amp;lt;/span&amp;gt;&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== wesnoth.interface.get_displayed_unit ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.interface.get_displayed_unit'''() → ''unit''&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.units.get_hovered'''() → ''unit''&lt;br /&gt;
&lt;br /&gt;
Returns a proxy to the unit currently displayed in the side pane of the user interface, if any.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=lua&amp;gt;&lt;br /&gt;
local name = tostring(wesnoth.interface.get_displayed_unit().name)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== wesnoth.interface.get_hovered_hex ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.interface.get_hovered_hex'''() → ''x'', ''y''&lt;br /&gt;
&lt;br /&gt;
Returns the two coordinates of the currently hovered tile, that is, where the mouse cursor is located. This is mostly useful for defining command-mode helpers.&lt;br /&gt;
&lt;br /&gt;
=== wesnoth.interface.get_selected_hex ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.interface.get_selected_hex'''() → ''x'', ''y''&lt;br /&gt;
&lt;br /&gt;
Returns the two coordinates of the currently selected (highlighted) tile. This is mostly useful for defining command-mode helpers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=lua&amp;gt;&lt;br /&gt;
function chg_unit(attr, val)&lt;br /&gt;
   local x, y = wesnoth.interface.get_selected_hex()&lt;br /&gt;
   if not x then wesnoth.message(&amp;quot;Error&amp;quot;, &amp;quot;No unit selected.&amp;quot;); return end&lt;br /&gt;
   wesnoth.units.modify({ x = x, y = y }, { [attr] = val })&lt;br /&gt;
end&lt;br /&gt;
-- Function chg_unit can be used in command mode to modify unit attributes on the fly:&lt;br /&gt;
--   :lua chg_unit(&amp;quot;status.poisoned&amp;quot;, true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== wesnoth.interface.get_viewing_side ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.interface.get_viewing_side'''() &amp;amp;rarr; ''side'', ''full_vision''&lt;br /&gt;
&lt;br /&gt;
Returns the viewing side of the current client in a multiplayer game.&lt;br /&gt;
&lt;br /&gt;
For a player participating in the game, this will always be the side that they control, or, if they control multiple sides, the one they most recently had control over. However, for an observer client, this will return the currently-active side instead.&lt;br /&gt;
&lt;br /&gt;
The second return value indicates whether the current client has full vision. This can only happen in replays or for observers.&lt;br /&gt;
&lt;br /&gt;
For obvious reasons, use of this function can easily cause out-of-sync errors. Use with care.&lt;br /&gt;
&lt;br /&gt;
=== wesnoth.interface.lock===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.interface.lock'''(''lock'')&lt;br /&gt;
&lt;br /&gt;
Locks or unlocks gamemap view scrolling for human players. If true is passed as the first parameter, the view is locked; pass false to unlock.&lt;br /&gt;
&lt;br /&gt;
Human players cannot scroll the gamemap view as long as it is locked, but Lua or WML actions such as wesnoth.scroll_to_hex still can; the locked/unlocked state is preserved when saving the current game. This feature is generally intended to be used in cutscenes to prevent the player scrolling away from scripted actions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=lua&amp;gt;&lt;br /&gt;
wesnoth.interface.lock_view(true)&lt;br /&gt;
wesnoth.interface.scroll_to_hex(12, 14, false, true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== wesnoth.interface.is_locked ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.interface.is_locked'''() → ''locked?''&lt;br /&gt;
&lt;br /&gt;
Returns a boolean indicating whether gamemap view scrolling is currently locked.&lt;br /&gt;
&lt;br /&gt;
=== wesnoth.interface.scroll_to_hex ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.interface.scroll_to_hex'''(''x'', ''y'', [''only_if_visible'', [''instant'', [''only_if_needed'']]])&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.units.scroll_to'''(''unit'', [''only_if_visible'', [''instant'', [''only_if_needed'']]])&lt;br /&gt;
* {{LuaGameOnly}} ''unit'':'''scroll_to'''([''only_if_visible'', [''instant'', [''only_if_needed'']]])&lt;br /&gt;
&lt;br /&gt;
Scrolls the map to the given location. If true is passed as the third parameter, scrolling is disabled if the tile is hidden under the fog. If true is passed as the fourth parameter, the view instantly warps to the location regardless of the scroll speed setting in Preferences. If true is passed as the fifth parameter, no scrolling occurs if the target location is already visible onscreen. It is also possible to pass a unit instead of a pair of tile coordinates (regardless of which module the function is called from).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=lua&amp;gt;&lt;br /&gt;
local u = wesnoth.units.get &amp;quot;hero&amp;quot;&lt;br /&gt;
u:scroll_to()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== wesnoth.interface.scroll ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.interface.scroll'''(''dx'', ''dy'')&lt;br /&gt;
&lt;br /&gt;
Scrolls the map by the given amount, which may be either positive or negative.&lt;br /&gt;
&lt;br /&gt;
=== wesnoth.interface.zoom ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.interface.zoom'''(''factor'', [''relative'']) → ''new_zoom''&lt;br /&gt;
&lt;br /&gt;
Changes the zoom level of the map. If relative is false, which is the default, it simply sets the zoom level. If relative is true, it zooms relative to the current zoom level. So, for example, if the zoom level is currently 0.5, then&lt;br /&gt;
&amp;lt;syntaxhighlight lang=lua inline&amp;gt;wesnoth.zoom(2)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
sets it to 2, while&lt;br /&gt;
&amp;lt;syntaxhighlight lang=lua inline&amp;gt;wesnoth.zoom(2, true)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
sets it to 1 (2 * 0.5).&lt;br /&gt;
&lt;br /&gt;
This function also returns the resulting zoom level. Because of this, you can call&lt;br /&gt;
&amp;lt;syntaxhighlight lang=lua inline&amp;gt;wesnoth.zoom(1, true)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
to simply get the current zoom level.&lt;br /&gt;
&lt;br /&gt;
Note that this function cannot zoom to a level that the user would not be able to reach from the UI. Attempting to do so will simply select the nearest allowed zoom level.&lt;br /&gt;
&lt;br /&gt;
=== wesnoth.interface.skip_messages ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.interface.skip_messages'''([''skip?''])&lt;br /&gt;
&lt;br /&gt;
Sets the skip messages flag. By default it sets it to true, but you can also pass false to unset the flag.&lt;br /&gt;
&lt;br /&gt;
=== wesnoth.interface.is_skipping_messages ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.interface.is_skipping_messages'''()&lt;br /&gt;
&lt;br /&gt;
Returns true if messages are currently being skipped, for example because the player has chosen to skip replay, or has pressed escape to dismiss a message.&lt;br /&gt;
&lt;br /&gt;
=== wesnoth.interface.add_chat_message ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.interface.add_chat_message'''([''speaker'',] ''message'')&lt;br /&gt;
&lt;br /&gt;
Displays a string in the onscreen chat. The chat line speaker is &amp;quot;Lua&amp;quot; by default, but it can be changed by passing a string before the message.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=lua&amp;gt;&lt;br /&gt;
wesnoth.interface.add_chat_message &amp;quot;Hello World!&amp;quot; -- will result in &amp;quot;&amp;lt;Lua&amp;gt; Hello World!&amp;quot;&lt;br /&gt;
wesnoth.interface.add_chat_message(&amp;quot;Big Brother&amp;quot;, &amp;quot;I'm watching you.&amp;quot;) -- will result in &amp;quot;&amp;lt;Big Brother&amp;gt; I'm watching you.&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
See also [[LuaAPI/wml#wml.error|wml.error]] for displaying error messages.&lt;br /&gt;
&lt;br /&gt;
=== wesnoth.interface.clear_chat_messages ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.interface.clear_chat_messages'''()&lt;br /&gt;
&lt;br /&gt;
Removes all messages from the onscreen chat, including error messages.&lt;br /&gt;
&lt;br /&gt;
=== wesnoth.interface.add_item_image ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.interface.place_item_image'''(''location'', ''filename'')&lt;br /&gt;
&lt;br /&gt;
Places an image at a given location and registers it as a WML ''[item]'' would do, so that it can be restored after save/load. Note that the location must be passed as separate x and y coordinates, as in the example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=lua&amp;gt;&lt;br /&gt;
wesnoth.interface.place_item_image(17, 42, &amp;quot;items/orcish-flag.png&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== wesnoth.interface.add_item_halo ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.interface.place_item_halo'''(''location'', ''filename'')&lt;br /&gt;
&lt;br /&gt;
Behaves the same as [[#wesnoth.interface.add_item_image]] but for halos.&lt;br /&gt;
&lt;br /&gt;
=== wesnoth.interface.remove_item ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.interface.remove_item'''(''location'', [''filename''])&lt;br /&gt;
&lt;br /&gt;
Removes an overlay set by [[#wesnoth.interface.add_item_image]] or [[#wesnoth.interface.add_item_halo]]. If no filename is provided, all the overlays on a given tile are removed. Note that the location must be passed as separate x and y coordinates, as in the example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=lua&amp;gt;&lt;br /&gt;
wesnoth.interface.remove_item(17, 42, &amp;quot;items/orcish-flag.png&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== wesnoth.interface.get_items ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.interface.get_items'''(''location'') &amp;amp;rarr; ''array of item tables''&lt;br /&gt;
&lt;br /&gt;
Get all items placed on the specified location. Note that the location must be passed as separate x and y coordinates.&lt;br /&gt;
&lt;br /&gt;
=== wesnoth.interface.add_hex_overlay ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.add_hex_overlay'''(''location'', ''item_wml'')&lt;br /&gt;
&lt;br /&gt;
Places a hex overlay (either an image or a halo) on the given location. The overlay is described by a table supporting the same fields as [[InterfaceActionsWML|[item]]]. This is a lower-level version of the item functions above. Note that the overlay is not kept over save/load cycles.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=lua&amp;gt;&lt;br /&gt;
wesnoth.interface.add_hex_overlay(17, 42, { image = &amp;quot;items/orcish-flag.png&amp;quot; })&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== wesnoth.interface.remove_hex_overlay ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.remove_hex_overlay'''(''location'', [''filename''])&lt;br /&gt;
&lt;br /&gt;
Removes all the overlays at the given location, including any added by '''wesnoth.interface.add_item_halo''' or '''wesnoth.interface.add_item_image''' (however, such a removal will not be kept over save/load cycles). If a filename is passed as a third argument, only this overlay (either image or halo) is removed.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=lua&amp;gt;&lt;br /&gt;
wesnoth.interface.remove_hex_overlay(17, 42, &amp;quot;items/orcish-flag.png&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== wesnoth.interface.end_turn ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.interface.end_turn'''([''next_side''])&lt;br /&gt;
&lt;br /&gt;
Immediately ends the current side's turn. By default, the next sequential side will gain control, but if you pass an argument that side gains control instead.&lt;br /&gt;
&lt;br /&gt;
The ''next_side'' can either be a valid side number, or a valid side number plus the total number of sides in the scenario. In the latter case, this function also increases the turn counter by 1.&lt;br /&gt;
&lt;br /&gt;
Using this, it's entirely possible to pass control around without ever increasing the turn counter.&lt;br /&gt;
&lt;br /&gt;
=== wesnoth.interface.allow_end_turn ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.interface.allow_end_turn'''(''allow'')&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.interface.allow_end_turn'''(''reason'')&lt;br /&gt;
&lt;br /&gt;
Enables or disables ending the turn in the UI. You can optionally pass a translatable string to show to the player if they attempt to do so anyway. It will be shown to the player without any additional framing, so it should normally be phrased similar to &amp;quot;You cannot end your turn because...&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
To allow the player to end their turn again, just pass '''true'''. If you pass '''false''', a default message will be shown.&lt;br /&gt;
&lt;br /&gt;
=== wesnoth.interface.color_adjust ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.interface.color_adjust'''(''red'', ''green'', ''blue'')&lt;br /&gt;
&lt;br /&gt;
Adjust the screen tint. (0,0,0) is no tint, and possible values range from -255 to 255.&lt;br /&gt;
&lt;br /&gt;
=== wesnoth.interface.add_overlay_text ===&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.17|3}}&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.interface.add_overlay_text'''(''text'' [, ''options'']) &amp;amp;rarr; ''text handle''&lt;br /&gt;
&lt;br /&gt;
Adds a text overlay to the screen that does not move with the map. [[Pango formatting]] is supported. The ''options'' table supports the following keys:&lt;br /&gt;
&lt;br /&gt;
* ''size'' - The font size.&lt;br /&gt;
* ''max_width'' - Sets the maximum width the text can occupy on the screen, either in screen units (pixels) or as a percentage (a string ending in &amp;lt;tt&amp;gt;%&amp;lt;/tt&amp;gt;). If the text is too long to fit in that width, it will automatically be word-wrapped. Defaults to 100%.&lt;br /&gt;
* ''color'' - The text color, either as a six-digit hex string (no leading &amp;lt;tt&amp;gt;#&amp;lt;/tt&amp;gt;) or an array of three integers (red, green, and blue).&lt;br /&gt;
* ''bgcolor'' - If present, a background in this color is placed behind the text to enhance readability. This works like the background behind chat messages, but can be any colour. It is formatted the same as ''color''. If omitted, the background is fully transparent (ie, there is no background).&lt;br /&gt;
* ''bgalpha'' - If a background colour is specified, this sets its transparency. The default is fully opaque.&lt;br /&gt;
* ''duration'' - How long the text should be displayed, in milliseconds, or the string &amp;lt;syntaxhighlight lang=lua inline&amp;gt;'unlimited'&amp;lt;/syntaxhighlight&amp;gt; for an infinite duration. Defaults to two seconds.&lt;br /&gt;
* ''fade_time'' - When the label is removed, either explicitly or because its duration expired, this is the time it takes to fade out, in milliseconds. A value of 0 means the label disappears instantly. Defaults to 100.&lt;br /&gt;
* ''halign'' - Where to anchor the text horizontally on the screen. Must be one of &amp;lt;syntaxhighlight lang=lua inline&amp;gt;'left', 'center', 'right'&amp;lt;/syntaxhighlight&amp;gt;.&lt;br /&gt;
* ''valign'' - Where to anchor the text vertically on the screen. Must be one of &amp;lt;syntaxhighlight lang=lua inline&amp;gt;'top', 'center', 'bottom'&amp;lt;/syntaxhighlight&amp;gt;.&lt;br /&gt;
* ''location'' - The screen offset where the text should be placed, relative to the specified anchor. Defaults to (0,0).&lt;br /&gt;
&lt;br /&gt;
The text handle returned from this function supports the following method calls:&lt;br /&gt;
&lt;br /&gt;
* ''handle'':'''move'''(''x'', ''y'')&lt;br /&gt;
&lt;br /&gt;
Moves the text to a new location on the screen. The provided ''x'' and ''y'' are offsets from its current location.&lt;br /&gt;
&lt;br /&gt;
* ''handle'':'''remove'''([''fade_time''])&lt;br /&gt;
&lt;br /&gt;
Remove the text from the screen, optionally overriding the fade-out time.&lt;br /&gt;
&lt;br /&gt;
* ''handle'':'''replace'''(''text'', ''options'') &amp;amp;rarr; ''handle''&lt;br /&gt;
&lt;br /&gt;
Replaces the text with new text. Options are the same as in ''add_overlay_text'', and the returned handle is the same one.&lt;br /&gt;
&lt;br /&gt;
=== wesnoth.interface.handle_user_interact ===&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.17|6}}&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.interface.handle_user_interact'''()&lt;br /&gt;
&lt;br /&gt;
Handle user interaction with the game interface. This can be inserted into Lua code that takes a long time to execute in order to prevent the user interface from freezing.&lt;br /&gt;
&lt;br /&gt;
== wesnoth.interface.game_display ==&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''wesnoth.interface.game_display'''.''theme_item'' &amp;amp;harr; '''function'''() &amp;amp;rarr; ''wml table of theme elements''&lt;br /&gt;
&lt;br /&gt;
This is an associative table linking item names to functions that describe the content of the in-game user interface. These functions are called with no arguments whenever the user interface is refreshed, and must return a WML table containing '''[element]''' children. Each subtag shall contain either a '''text''' or an '''image''' field that is displayed to the user. It can also contain a '''tooltip''' field that is displayed to the user when moused over, and a '''help''' field that points to the help section that is displayed when the user clicks on the theme item.&lt;br /&gt;
&lt;br /&gt;
Note that the '''wesnoth.interface.game_display''' cannot be iterated using '''pairs''' or '''next''' to retrieve the items from the current theme. However, built-in items ''can'' be recovered as long as their name is known. The example below shows how to modify the ''unit_status'' item to display a custom status:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=lua&amp;gt;&lt;br /&gt;
local old_unit_status = wesnoth.interface.game_display.unit_status&lt;br /&gt;
function wesnoth.interface.game_display.unit_status()&lt;br /&gt;
    local _ = wesnoth.textdomain &amp;quot;mydomain&amp;quot;&lt;br /&gt;
    local u = wesnoth.interface.get_displayed_unit()&lt;br /&gt;
    if not u then return {} end&lt;br /&gt;
    local s = old_unit_status()&lt;br /&gt;
    if u.status.entangled then&lt;br /&gt;
        table.insert(s, wml.tag.element {&lt;br /&gt;
            image = &amp;quot;entangled.png&amp;quot;,&lt;br /&gt;
            tooltip = _&amp;quot;entangled: This unit is entangled. It cannot move but it can still attack.&amp;quot;&lt;br /&gt;
        })&lt;br /&gt;
    end&lt;br /&gt;
    return s&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The things that would need to be to modified in the above code are:&lt;br /&gt;
&lt;br /&gt;
* the domain of your addon (&amp;quot;mydomain&amp;quot;), assuming that you are using translations. Otherwise just remove the underscore in the tooltip line.&lt;br /&gt;
* the name of the status (u.status.entangled). Note that if the attribute happens to be inside [variables], so be it: u.variables.whatever.&lt;br /&gt;
* the path to the image (&amp;quot;entangled.png&amp;quot;).&lt;br /&gt;
* the tooltip of the status (&amp;quot;entangled: This unit ...&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
The following is a list of valid entries in '''wesnoth.interface.game_display''' which will have an effect in the game, together with sample output and a brief description of their use.&lt;br /&gt;
&lt;br /&gt;
=== unit_name ===&lt;br /&gt;
&lt;br /&gt;
Shows the name of the unit the mouse is hovering over.&lt;br /&gt;
&lt;br /&gt;
=== unit_type ===&lt;br /&gt;
&lt;br /&gt;
Shows the type of the unit the mouse is hovering over.&lt;br /&gt;
&lt;br /&gt;
=== unit_race ===&lt;br /&gt;
&lt;br /&gt;
Shows the race of the unit the mouse is hovering over.&lt;br /&gt;
&lt;br /&gt;
=== unit_side ===&lt;br /&gt;
&lt;br /&gt;
Shows the side number and team color of the unit the mouse is hovering over.&lt;br /&gt;
&lt;br /&gt;
=== unit_level ===&lt;br /&gt;
&lt;br /&gt;
Shows the level of the unit the mouse is hovering over.&lt;br /&gt;
&lt;br /&gt;
=== unit_amla ===&lt;br /&gt;
&lt;br /&gt;
Similar to unit_advancement_options but shows AMLAs only.&lt;br /&gt;
&lt;br /&gt;
=== unit_traits ===&lt;br /&gt;
&lt;br /&gt;
Shows the list of trait names for the unit the mouse is hovering over.&lt;br /&gt;
&lt;br /&gt;
=== unit_status ===&lt;br /&gt;
&lt;br /&gt;
Shows the status icons for statuses afflicting the unit the mouse is hovering over.&lt;br /&gt;
&lt;br /&gt;
=== unit_alignment ===&lt;br /&gt;
&lt;br /&gt;
Shows the alignment of the unit the mouse is hovering over.&lt;br /&gt;
&lt;br /&gt;
=== unit_abilities ===&lt;br /&gt;
&lt;br /&gt;
Shows the ability names of the unit the mouse is hovering over.&lt;br /&gt;
&lt;br /&gt;
=== unit_hp ===&lt;br /&gt;
&lt;br /&gt;
Shows the current and maximum hit points of the unit the mouse is hovering over.&lt;br /&gt;
&lt;br /&gt;
=== unit_xp ===&lt;br /&gt;
&lt;br /&gt;
Shows the current and target experience points of the unit the mouse is hovering over.&lt;br /&gt;
&lt;br /&gt;
=== unit_advancement_options ===&lt;br /&gt;
&lt;br /&gt;
Shows the options the unit the mouse is hovering over has for advancement, including both level ups and AMLAs. This item is not used in the default theme.&lt;br /&gt;
&lt;br /&gt;
=== unit_defense ===&lt;br /&gt;
&lt;br /&gt;
Shows the defense of the unit the mouse is hovering over.&lt;br /&gt;
&lt;br /&gt;
=== unit_vision ===&lt;br /&gt;
&lt;br /&gt;
Shows the current and maximum vision points of the unit the mouse is hovering over.&lt;br /&gt;
&lt;br /&gt;
=== unit_moves ===&lt;br /&gt;
&lt;br /&gt;
Shows the current and maximum movement points of the unit the mouse is hovering over.&lt;br /&gt;
&lt;br /&gt;
=== unit_weapons ===&lt;br /&gt;
&lt;br /&gt;
Shows the available weapons of the unit the mouse is hovering over. The default generator expresses each weapon line (basic details, specials, etc) as a separate &amp;lt;code&amp;gt;element&amp;lt;/code&amp;gt;. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=wml&amp;gt;&lt;br /&gt;
[element]&lt;br /&gt;
  image=&amp;quot;melee.png&amp;quot;&lt;br /&gt;
  tooltip=&amp;quot;...stuff...&amp;quot;&lt;br /&gt;
[/element]&lt;br /&gt;
[element]&lt;br /&gt;
  image=&amp;quot;arcane.png&amp;quot;&lt;br /&gt;
  tooltip=&amp;quot;...stuff...&amp;quot;&lt;br /&gt;
[/element]&lt;br /&gt;
[element]&lt;br /&gt;
  text=&amp;quot;6×3 holy sword&amp;quot;&lt;br /&gt;
  tooltip=&amp;quot;...stuff...&amp;quot;&lt;br /&gt;
[/element]&lt;br /&gt;
[element]&lt;br /&gt;
  # This is empty if the range and type icons both exist.&lt;br /&gt;
  text=&amp;quot;melee-arcane&amp;quot;&lt;br /&gt;
  tooltip=&amp;quot;...stuff...&amp;quot;&lt;br /&gt;
[/element]&lt;br /&gt;
# If the weapon has accuracy or parry...&lt;br /&gt;
[element]&lt;br /&gt;
  text=&amp;quot;+20%/-10%&amp;quot;&lt;br /&gt;
  tooltip=&amp;quot;Accuracy: +20%&lt;br /&gt;
Parry: -10%&lt;br /&gt;
&amp;quot;&lt;br /&gt;
[/element]&lt;br /&gt;
# If the weapon has special abilities...&lt;br /&gt;
[element]&lt;br /&gt;
  text=&amp;quot;magical&amp;quot;&lt;br /&gt;
  tooltip=&amp;quot;...stuff...&amp;quot;&lt;br /&gt;
[/element]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== unit_image ===&lt;br /&gt;
&lt;br /&gt;
Shows the sprite of the unit the mouse is hovering over.&lt;br /&gt;
&lt;br /&gt;
=== unit_profile ===&lt;br /&gt;
&lt;br /&gt;
Shows the portrait of the unit the mouse is hovering over.&lt;br /&gt;
&lt;br /&gt;
=== selected_unit ===&lt;br /&gt;
&lt;br /&gt;
Nearly every item beginning with '''unit_''' has a corresponding item beginning with '''selected_unit_''', which describes the currently-selected unit (if any) rather than the unit the mouse is hovering over. These items are not used in the default theme.&lt;br /&gt;
&lt;br /&gt;
There is no '''selected_unit_amla''' however.&lt;br /&gt;
&lt;br /&gt;
=== highlighted_unit_weapons ===&lt;br /&gt;
&lt;br /&gt;
This is almost the same as '''selected_unit_weapons''' but with slightly different logic of choosing which unit to show.&lt;br /&gt;
&lt;br /&gt;
=== tod_stats and selected_tod_stats ===&lt;br /&gt;
&lt;br /&gt;
Shows the time of day stats for the hex the mouse is hovering over or the hex the selected unit is on. The time of day stats shows the counter of your position in the schedule as well as the entire cycle.&lt;br /&gt;
&lt;br /&gt;
=== time_of_day and selected_time_of_day ===&lt;br /&gt;
&lt;br /&gt;
Shows the time of day image for the hex the mouse is hovering over or the hex the selected unit is on.&lt;br /&gt;
&lt;br /&gt;
=== unit_box ===&lt;br /&gt;
&lt;br /&gt;
This is an experimental item that combines a unit display and the time of day.&lt;br /&gt;
&lt;br /&gt;
=== turn ===&lt;br /&gt;
&lt;br /&gt;
Shows the current turn count.&lt;br /&gt;
&lt;br /&gt;
=== gold ===&lt;br /&gt;
&lt;br /&gt;
Shows the amount of gold for the active side.&lt;br /&gt;
&lt;br /&gt;
=== villages ===&lt;br /&gt;
&lt;br /&gt;
Shows the number of villages owned by the active side.&lt;br /&gt;
&lt;br /&gt;
=== num_units ===&lt;br /&gt;
&lt;br /&gt;
Shows the number of units owned by the active side.&lt;br /&gt;
&lt;br /&gt;
=== upkeep ===&lt;br /&gt;
&lt;br /&gt;
Shows the upkeep and expenses for the active side.&lt;br /&gt;
&lt;br /&gt;
=== income ===&lt;br /&gt;
&lt;br /&gt;
Shows the income for the active side&lt;br /&gt;
&lt;br /&gt;
=== terrain_info ===&lt;br /&gt;
&lt;br /&gt;
Shows the icons of the terrain on hex the mouse is hovering over.&lt;br /&gt;
&lt;br /&gt;
=== terrain &amp;lt;!--and selected_terrain--&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- selected_terrain commented out here because at time of last edit it was commented out in the source code --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Shows the name of the terrain on the hex the mouse is hovering over&amp;lt;!-- or the hex the selected unit is on--&amp;gt;. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=wml&amp;gt;&lt;br /&gt;
[element]&lt;br /&gt;
  text=&amp;quot;Grassland (Flat)&amp;quot;&lt;br /&gt;
[/element]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== position ===&lt;br /&gt;
&lt;br /&gt;
Shows the map coordinates of the hex the mouse is hovering over.&lt;br /&gt;
&lt;br /&gt;
=== zoom_level ===&lt;br /&gt;
&lt;br /&gt;
Shows the zoom level as a percentage. This item is not used in the default theme.&lt;br /&gt;
&lt;br /&gt;
=== side_playing ===&lt;br /&gt;
&lt;br /&gt;
Shows the active side's flag.&lt;br /&gt;
&lt;br /&gt;
=== observers ===&lt;br /&gt;
&lt;br /&gt;
When there is no observer, it returns an empty table. When there are observers, it returns:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=wml&amp;gt;&lt;br /&gt;
[element]&lt;br /&gt;
  tooltip=&amp;quot;Observers&lt;br /&gt;
&amp;lt;observer1&amp;gt;&lt;br /&gt;
&amp;lt;observer2&amp;gt;&lt;br /&gt;
&amp;quot;&lt;br /&gt;
  image=&amp;quot;misc/eye.png&amp;quot;&lt;br /&gt;
[/element]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== report_clock ===&lt;br /&gt;
&lt;br /&gt;
This returns the current time in HH:MM format according to the user's preferences, for example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=wml&amp;gt;&lt;br /&gt;
[element]&lt;br /&gt;
  text=&amp;quot;22:32&amp;quot;&lt;br /&gt;
[/element]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== report_countdown ===&lt;br /&gt;
&lt;br /&gt;
This returns the current chess-timer countdown in MM:SS format for the player's turn, for example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=wml&amp;gt;&lt;br /&gt;
[element]&lt;br /&gt;
  text=&amp;quot;12:43&amp;quot;&lt;br /&gt;
[/element]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Depending on the turn limit, it may also be colored using Pango markup.&lt;br /&gt;
&lt;br /&gt;
[[Category:Lua Reference]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=69678</id>
		<title>Micro AIs</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=69678"/>
		<updated>2022-06-27T14:47:06Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Custom Micro AIs */ add new syntax for keys tables&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== A Few General Words about Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
* Micro AIs allow Wesnoth campaign and scenario authors to add new AI functionality without the need for any AI programming. They bring new specialized behavior to Wesnoth that cannot be achieved, at least not easily, by adjusting default AI parameters (such as those described at [[AiWML]]) or with WML code.&lt;br /&gt;
&lt;br /&gt;
* A Micro AI is activated and configured (or deleted) via the [micro_ai] tag placed in an event, requiring only a few lines of WML code. {{DevFeature1.15|?}} Starting in 1.16 the [micro_ai] tag may also be used in [side][ai] or [unit][ai]. These are both shorthands for placing it in the prestart event with &amp;quot;action=add&amp;quot;, side set to the side, and in the case of [unit][ai], a [filter] tag set to limit the Micro AI to that one unit.&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.6''', the Micro AI behavior was inconsistent when it came to '''hidden enemies and petrified units'''.  Some Micro AIs ignored those, while others did not.  In some case there was even a potential for AI errors when an AI unit ran into hidden or petrified enemies.  As of 1.13.6, this has all been fixed.  The Micro AIs now correctly and consistently ignore hidden units and treat petrified units as blocking the hex they are on but having no effect otherwise, in the same way as the default AI does.  '''Important notes:'''&lt;br /&gt;
** It is not possible to re-include hidden/petrified units via the Standard Unit Filters (SUFs) used by some of the Micro AIs. In fact, if this is attempted, the filters will generally not match any unit.&lt;br /&gt;
*** This only applies to SUFs which go directly into the [micro_ai] tag. Any SUF that is a sub-tag of another filter (for example, of a Standard Location Filter) is taken literally and returns all units matching the filter. If hidden or petrified units are to be excluded from such a sub-filter, that needs to be specified directly in that filter.&lt;br /&gt;
** We also note that, because all the Micro AIs are written in Lua, it is possible to [[Micro_AIs#Using_Development_.281.13.29_Version_Micro_AIs_in_your_Stable_.281.12.29_Version_Add-on|move a Micro AI's code into one's add-on]] and modify it to whatever scenario-specific behavior is desired (this does not only apply to hidden/petrified units, but to all behavior). Setting up general Micro AI behavior that works in every possible setting is not feasible, but adapting it to work (only) in the specific setting of a given scenario is often quite simple. If you do not know how to do this, please post a message on the Wesnoth forums and we will be happy to help you with it.&lt;br /&gt;
&lt;br /&gt;
* The '''[[AiWML#List_of_AI_Aspects|default AI aspects]] are, for the most part, not taken into account by the Micro AIs'''.  For example, unless noted otherwise, specifying a value for ''aggression'' does not change the MAI behavior.  The exception is the ''[avoid]'' tag which is taken into account by many Micro AIs (except those for which it would interfere with the Micro AI behavior or those that already use a a [[StandardLocationFilter|Standard Location Filter]] that can be used for the same purpose).  This is a feature of the Micro AIs, not a bug.  If you feel that a certain default AI aspect should also apply to a Micro AI, please let us know in the [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 Micro AIs Feedback thread].&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.7''', the Micro AIs did not all work reliably when the '''AI side is under shroud''' and it may have been necessary to turn shroud off for the side when using a Micro AI. {{DevFeature1.13|7}} The Micro AIs now all work for sides under shroud. They do this by disregarding shroud for terrain purposes when path finding, consistent with default Wesnoth AI behavior, while still ignoring hidden units (including those hidden under shroud) correctly.&lt;br /&gt;
&lt;br /&gt;
* Most Micro AIs in their default setting apply to all units of a side, although many of them provide parameters for filtering the units that should be affected by the new behavior.  If the Micro AI is applied only to part of the units of a side, the other units follow the default Wesnoth AI behavior.&lt;br /&gt;
&lt;br /&gt;
* Micro AIs are available in mainline since Wesnoth 1.11/1.12.&lt;br /&gt;
&lt;br /&gt;
== Micro AI Test and Demo Scenarios ==&lt;br /&gt;
&lt;br /&gt;
All Micro AIs described here can be checked out in a number of test and demo scenarios.  The easiest way to access the test scenarios is by assigning a hotkey to &amp;quot;Start Test Scenario&amp;quot; in the preferences (as of Wesnoth 1.13.8). Then, simply press the hotkey in the titlescreen and select the desired scenario from the list (e.g. 'micro_ai_test' for the overall &amp;quot;switchboard scenario&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Test scenarios can also be accessed from the command line by typing&lt;br /&gt;
 path/wesnoth-executable -t micro_ai_test&lt;br /&gt;
or (depending on your system and Wesnoth version)&lt;br /&gt;
 path/wesnoth-executable -tmicro_ai_test&lt;br /&gt;
This starts up a &amp;quot;switchboard scenario&amp;quot; in which you can select the Micro AI demonstration scenario you want to check out.  Here, ''path/wesnoth-executable'' needs to be replaced by whatever the path and filename of the Wesnoth executable is on your system.&lt;br /&gt;
&lt;br /&gt;
== Setting up a Micro AI ==&lt;br /&gt;
&lt;br /&gt;
Setting up a Micro AI requires only one simple step:&lt;br /&gt;
==== Activating and configuring (or deleting) a Micro AI ====&lt;br /&gt;
&lt;br /&gt;
Micro AIs are activated, deleted and configured using the [micro_ai] tag.  This tag needs to be placed in [[ActionWML]], that is, in an event, a menu option or the like.  As an example, the following code activates the healer_support Micro AI in its default configuration for Side 2 from the beginning of the scenario:&lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
 &lt;br /&gt;
         # Configure the healer support micro AI&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
'''Keys required for all Micro AIs:'''&lt;br /&gt;
All [micro_ai] tags must contain the following three required keys:&lt;br /&gt;
* '''side''': The side for which the Micro AI is to be added, changed or deleted.  This has to be an individual side number, lists of sides are not supported.&lt;br /&gt;
* '''ai_type''': The type of Micro AI to be added, changed or deleted.  See the following sections for allowed values.&lt;br /&gt;
* '''action''' (string): The action to take concerning the Micro AI.  The following values are allowed: &lt;br /&gt;
** ''add'': Add a Micro AI to a side.&lt;br /&gt;
** ''change'': Change the configuration of an existing Micro AI.  Note that this does not only change the specific parameters provided in the tag, but it replaces the entire existing Micro AI by a new version with the new (and only the new) configuration.  It is therefore equivalent to using first the ''delete'' and then the ''add'' action.&lt;br /&gt;
** ''delete'': Delete an existing Micro AI from a side.&lt;br /&gt;
&lt;br /&gt;
'''Keys required under certain circumstances:'''&lt;br /&gt;
* '''ca_id''': (string) If several MAIs of the same type are used for the same side and one or several of these are to be changed or deleted during a scenario, this key needs to be included so that the [micro_ai] tag knows which of them needs to be changed/deleted.  The same is also true if a Micro AI is changed more than once during a scenario.  We therefore '''strongly recommend always setting ''ca_id''''' for MAIs which are, or might be, changed or deleted during a scenario.&lt;br /&gt;
** '''Note:''' The id(s)/name(s) actually assigned to the CA(s) of the MAI by the engine might be different from the parameter passed here.  It is, however, sufficient to use this ca_id with the [micro_ai] tag add/delete/change actions to identify the MAI uniquely.  (In other words: you don't have to worry about this if you always use the [micro_ai] tag, but might have to figure out the actual name(s) of the CA(s) if you want to delete or change them manually.)&lt;br /&gt;
* Other keys may also be required depending on the Micro AI.  In additional, a number of optional keys may be available to configure the AI behavior.  See the sections on the individual Micro AIs for details.&lt;br /&gt;
&lt;br /&gt;
'''Required keys for deleting a Micro AI:'''&lt;br /&gt;
&lt;br /&gt;
Only a subset of the keys required for setting up a Micro AI are needed when deleting it.  '''side''', '''ai_type''' and '''action'''=delete always need to be provided.  '''ca_id''' needs to be given if it was provided when adding the Micro AI (see above for when this is needed).&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* The ''add'' and ''change'' actions ignore all keys that do not apply to the respective Micro AI type.  The ''delete'' action ignores all keys other than the required keys listed above.&lt;br /&gt;
&lt;br /&gt;
* Some of the Micro AIs use and/or modify default AI components (candidate actions or aspects) when they are added, or reset these components to their default values when they are deleted.  Thus, if you have modified the AI yourself using the default CAs or aspects, some of these modifications might be affected, or might be interfering with the Micro AI.  The sections below indicate whether a Micro AI modifies any default AI components.&lt;br /&gt;
&lt;br /&gt;
===Combining Different Micro AI Types for the Same Side===&lt;br /&gt;
&lt;br /&gt;
No extra steps need to be taken when combining different Micro AIs on the same side.  Simply set up the [micro_ai] tags for all AIs you want to use.&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* While it is ''technically'' possible to combine any number of Micro AIs, this does not mean that all Micro AIs work together in practice.  Some are not compatible because they control units in contradictory ways.  &lt;br /&gt;
* Not all combinations that ''should'' work have been tested yet.  If you find a combination that you believe should work but doesn't, [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 please let us know] and we will see if that can be fixed.&lt;br /&gt;
* The candidate action score almost certainly has to be set (using the ca_score= key) for one or several of the MAIs if several of them are used on the same side.&lt;br /&gt;
&lt;br /&gt;
== Animals Micro AIs ==&lt;br /&gt;
=== General Comments on the Animal Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Animals Micro AIs are meant to simulate the behaviors of certain types of animals.  They are, however, set up so that they can be used with arbitrary unit types and might be applicable in quite diverse situations.  The AIs nevertheless remain named after the animals for which they were written originally, so that they are (to some extent) descriptive of their behavior.&lt;br /&gt;
&lt;br /&gt;
Brief descriptions of the AI behaviors are given in the following subsections.  To get a better feeling for their behavior, check out the &amp;quot;Animals&amp;quot;, &amp;quot;Swarm&amp;quot;, &amp;quot;Wolves&amp;quot; and &amp;quot;Dragon&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  Note that some of the animal AIs are designed for animal types that do not exist in mainline.  In the test scenario, other animal types are therefore substituted for them.&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Wolves (ai_type=wolves) ===&lt;br /&gt;
&lt;br /&gt;
The Wolves Micro AI organizes all &amp;quot;wolf&amp;quot; units (predators) of the side to hunt in a single pack.  They actively chase after the closest prey and try to corner it (not always super successfully), but are easily distracted by other enemy units coming into attack range, to the point where the pack might split up.  The wolves can, however, be told to attack only prey units as well as to try to avoid other types of units (such as larger predators).  When no prey is left, the wolves wander randomly.&lt;br /&gt;
&lt;br /&gt;
This AI works best for a small number of predators (2 - 5) and sparse prey.  Also, the predators should be put on the map in a pack, that is, reasonably close to each other.  For a demonstration of the Wolves AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Note that another wolves AI exists, called [[Micro_AIs#Animals_AI:_Multi-pack_Wolves_.28ai_type.3Dwolves_multipacks.29|Multipack Wolves]], which distributes the wolves of a side into several packs, rather than all of them into one single pack as it is done here.&lt;br /&gt;
&lt;br /&gt;
Enable the Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the predator units.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the prey units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_only_prey'''=no: In the default configuration, the wolves attack all enemy units in attack range.  If this key is set to 'yes', they only attack prey units.&lt;br /&gt;
* '''avoid_type''': Comma-separated list of unit types (on enemy sides) which the wolves try to avoid.  They try to stay out of these units' way as much as possible when moving around (except when they are moving in to attack another enemy unit) and never attack them.&lt;br /&gt;
* '''ca_score'''=90000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 90,000, which is lower than the default combat CA, but higher than most other default CAs which move units.  It can be changed by setting this parameter but in most cases this will not make sense.  Note that the Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Notes on default (RCA) AI aspects:''' &lt;br /&gt;
* If ''attack_only_prey'' or ''avoid_type'' are set, the Wolves Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* Attacks by the wolves are left to the default (RCA) AI, meaning that the value of ''aggression'' has an influence on the behavior.&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI is taken into account. Note: as attacks by the wolves are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=6&lt;br /&gt;
             ai_type=wolves&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Wolf&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Vampire Bat&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             avoid_type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Multi-pack Wolves (ai_type=wolves_multipacks) ===&lt;br /&gt;
&lt;br /&gt;
The Multi-pack Wolves Micro AI is different from the [[Micro_AIs#Animals_AI:_Wolves_.28ai_type.3Dwolves.29|Wolves Micro AI]] in that there can be an arbitrary number of wolf packs, with the pack size being a free parameter.  At the beginning of the scenario, close wolves are grouped into packs in a semi-methodical way.  If the wolves are put on the map in distinct groups of close wolves of the correct number, they will be joined into packs matching these groups.  If, on the other hand, they are spread out all over the map, the method of assigning them to packs is semi-random.&lt;br /&gt;
&lt;br /&gt;
Wolves of the same pack begin by joining each other on the map.  After that, they stay together until only one wolf is left, which then tries to join up with an incomplete pack or with other single wolves.  Individual wolves entering the map during the scenario behave in that way as well.&lt;br /&gt;
&lt;br /&gt;
A second difference to the other Wolves AI is that wolves do not actively hunt here.  For the most part they just wander (often long distance).  However, the pack ferociously (and without regard for its own health) attacks any enemy units that come into range, as long as that does not mean separating the pack by more than a few hexes.  Staying together, or joining with a new wolf assigned to the pack, is the only thing that takes priority over satisfying the wolves' thirst for blood.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Multi-pack Wolves AI behavior, check out the &amp;quot;Wolves&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Multi-pack Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves_multipacks&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Multi-pack Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''pack_size'''=3: (integer) The size of the packs.&lt;br /&gt;
* '''show_pack_number'''=no: If set to &amp;quot;yes&amp;quot;, the wolves' pack numbers will be shown below them.  Note that this means that the pack number of a wolf killed in an attack will remain as a label on the map.  This behavior is intentional to help UMC authors note where units might have made suicidal mistakes.&lt;br /&gt;
* '''type'''=Wolf: Comma-separated list of the unit types which the AI controls. The default value is &amp;quot;Wolf&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves_multipacks [micro_ai] tag usage from the &amp;quot;Wolves&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=wolves_multipacks&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             show_pack_number=yes&lt;br /&gt;
             pack_size=4&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Big Animals (ai_type=big_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Big Animals Micro AI is a simulation of large predators that wander and hunt alone, such as Bears, Giant Spiders or Yetis.  For the most part, these just wander on the terrain that's been defined for them, but they attack enemy units if those happen to come into range.  The AI can be set up so that the Big Animals stay out of the way of other units (such as other large predators), in which case they only attack those if cornered or if they run into them accidentally (such as ambushers).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Big Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Big Animals Micro AI by using&lt;br /&gt;
 ai_type=big_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the animals to which this AI is applied.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': The big animals often go for long distance (multi-turn) journeys.  This tag is a [[StandardLocationFilter|Standard Location Filter]] describing the terrain from which the next goal is chosen.  The default is all terrain on the map. &lt;br /&gt;
* '''[filter_location_wander]''': The terrain on which the big animals will end their moves. This is a [[StandardLocationFilter|Standard Location Filter]].  The default is all terrain on the map.&lt;br /&gt;
* '''[avoid_unit]''': A [[StandardUnitFilter|Standard Unit Filter]] describing all enemy units that the big animals will avoid.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
Here's an example of big_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=big_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Ghast&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [avoid_unit]&lt;br /&gt;
                 type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
             [/avoid_unit]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=1-40,1-18&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Forest Animals (ai_type=forest_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Forest Animals Micro AI simulates a number of different animal behaviors that one might find in a forest, namely animals behaving like deer, rabbits and families of tuskers.  In the following, we will refer to those animals by these types, even though the actual unit types can be selected freely.  They behave as follows:&lt;br /&gt;
* Each deer wanders randomly on (selectable) terrain, except when enemies get in its (the deer's) maximum movement range, in which case it flees to the farthest point it can reach.&lt;br /&gt;
* Tuskers exhibit the same behavior as deer, except when an enemy is next to one of the tusklets.  This enemy is attacked unconditionally by the tuskers trying to protect their young.&lt;br /&gt;
* Tusklets blindly follow the closest adult tusker, except when there is no tusker left, in which case they behave the same as deer.&lt;br /&gt;
* Rabbits also wander randomly, but in addition disappear into their holes (if any are within reach) when enemies are close.  They reappear out of their holes at the beginning of the turn, if it is safe.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Forest Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Forest Animals Micro AI by using&lt;br /&gt;
 ai_type=forest_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Forest Animals AI consists of 4 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-3&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''rabbit_type, tusker_type, tusklet_type, deer_type''': Comma-separated lists of the unit types behaving as the respective animals.  If any of these parameters are not set, those types of animal behavior are skipped by the AI.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain on which the forest animals wander when they are not threatened by enemies.&lt;br /&gt;
* '''rabbit_number'''=6: (integer)  The number of rabbits for the AI to have on the map simultaneously.  The AI puts the missing number of rabbits onto the map at the beginning of the AI's turn (when the rabbits come out of their holes).&lt;br /&gt;
* '''rabbit_enemy_distance'''=3: (number)  Rabbits won't spawn in holes that aren't more than this distance away from all enemies.&lt;br /&gt;
* '''rabbit_hole_img''': Rabbit holes are marked by items on the map.  If this parameter is set, it must be the file name of the item image.  If it is not set, all items on the map are treated as rabbit holes.&lt;br /&gt;
** {{DevFeature1.14|10}} Items used for rabbit holes on the map border used to produce an error. Placing them on the map border therefore needed to be avoided on the scenario setup side. They are now automatically excluded from consideration by the AI code.&lt;br /&gt;
&lt;br /&gt;
Here's an example of forest_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=forest_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             deer_type=Vampire Bat&lt;br /&gt;
             rabbit_type=Giant Rat&lt;br /&gt;
             tusker_type=Ogre&lt;br /&gt;
             tusklet_type=Young Ogre&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=*^F*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Herd and Herders (ai_type=herding) ===&lt;br /&gt;
&lt;br /&gt;
The Herding Micro AI sets up an area on the map where a number of herder units (such as sheep dog) control and protect a herd (such as sheep).  For convenience, we simply refer to them as sheep dogs and sheep in the following, even though arbitrary unit types can be selected for either.&lt;br /&gt;
&lt;br /&gt;
Sheep dogs try to keep their sheep safe.  This involves keeping them inside the herding area, positioning themselves in between the sheep and approaching enemies, and attacking the enemies if those get too close.  If no active herding or protecting move is needed, the dogs go to a random location on the perimeter of the herding area.&lt;br /&gt;
&lt;br /&gt;
Sheep wander aimlessly except when a sheep dog is next to them, in which case they run away from the dog.  The dogs exploit this by positioning themselves on the outside of the sheep, if possible.  Sheep also run away from approaching enemies.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Herding AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Herding Micro AI by using&lt;br /&gt;
 ai_type=herding&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herder units (e.g. dogs).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herd units (e.g. sheep).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain forming the perimeter of the herding area.  In other words, this is the path the herders (dogs) use to patrol around the outside of the herding area.  This must be a set of coordinates ''completely enclosing'' the herding area.  Here's an example of setting up a rectangular perimeter:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         x=10-20,10-20,10,  ,20&lt;br /&gt;
         y=11,   21,   11-21,11-21&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
Another possibility is by defining the path by certain terrain on the map (as it is done in the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]), in which case the filter can simply look like this:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         terrain=Rb&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
* '''herd_x,herd_y''': The coordinate of one hex (''any'' one hex) inside the herding area.  This is needed to define which part of the map is on the ''inside'' of the herding area perimeter defined by [herding_perimeter], and which is the ''outside''. {{DevFeature1.15|0}} '''herd_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''attention_distance'''=8: (integer)  If an enemy is within this distance of a herd unit, but not as close as ''attack_distance'', the herders try to position themselves in between the enemy and the herd unit.&lt;br /&gt;
* '''attack_distance'''=4: (integer)  If an enemy is within this distance of a herd animal, the herders attack this enemy.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Herding AI consists of 6 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-5&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Herding [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=herding&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Footpad&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Troll Whelp&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             herd_x,herd_y=32,28&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=Rb&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Hunter (ai_type=hunter) ===&lt;br /&gt;
&lt;br /&gt;
The Hunter Micro AI leads a unit (or units) through hunt-and-rest cycles.  The units wander in a prescribed area of the map, their hunting ground, until an enemy unit comes within range.  Note that the units do not actively chase down enemies, but wander randomly through the hunting ground until an enemy is encountered.&lt;br /&gt;
&lt;br /&gt;
If enemies are within range of a hunter unit, the hunter attacks the weakest of them.  If a kill is made, it then retreats to its rest location, where it stays for a certain number of turns and until fully healed (whatever happens later).&lt;br /&gt;
&lt;br /&gt;
A few more details:&lt;br /&gt;
* If the hunter's way home is entirely blocked on the return (so that there is no possible path), the normal RCA AI takes over its behavior.&lt;br /&gt;
* However, if the way is blocked by an enemy unit occupying the rest hex, that enemy will be attacked unconditionally.&lt;br /&gt;
* A kill only makes the hunter go home when it is the attacker, not as defender.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Hunter AI behavior, check out the &amp;quot;Dragon&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]. &lt;br /&gt;
&lt;br /&gt;
Enable the Hunter Micro AI by using&lt;br /&gt;
 ai_type=hunter&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''home_x,home_y''': The coordinates of the hex to which the hunter returns and rests at after a kill. {{DevFeature1.15|0}} '''home_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the unit's hunting ground.  If this tag is not provided, the hunter uses all of the map.&lt;br /&gt;
* '''rest_turns'''=3: (integer)  The number of turns the AI stays at home_x,home_y after a kill.  It will not leave its home until this number of turns have passed ''and'' it is fully healed.&lt;br /&gt;
* '''show_messages'''=no: (boolean) If set to yes, the hunter announces whenever its behavior changes to the next phase.  Note that this is meant as debug output for the scenario author.  The messages are not translated and are not included in replays (and are only visible to the host in an MP game).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a hunter [micro_ai] tag usage from the &amp;quot;Dragon&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hunter&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Rowck&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=5-30,1-15&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             home_x,home_y=3,15&lt;br /&gt;
             rest_turns=2&lt;br /&gt;
 &lt;br /&gt;
             show_messages=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Swarm (ai_type=swarm) ===&lt;br /&gt;
&lt;br /&gt;
The Swarm Micro AI uses some very simply algorithms to simulate animal swarm behavior.  Without adjacent enemies, they simply do a random move, trying to stay together and at a certain distance from enemies.  However, if an enemy unit is close to any bat, the swarm scatters.  This is particular fun to watch when one places an enemy unit in the middle of the swarm.  After being scattered, the swarm members slowly rejoin, but not in a very organized way.  Sub-swarms or individual bats might roam around for quite some time before they find their way back.  It is also possible that individual bats (or small groups) split off from the larger swarm at times.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Swarm AI behavior, check out the &amp;quot;Swarm&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Swarm Micro AI by using&lt;br /&gt;
 ai_type=swarm&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Swarm AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_distance'''=5: (int) The minimum distance kept between units of the swarm and enemies when the swarm moves as a whole.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.15|12}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the units.  Only AI units matching this filter participate in the swarm.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''scatter_distance'''=3: (int) Enemies within &amp;quot;scatter_distance&amp;quot; hexes of any swarm unit cause the swarm to scatter, by each unit trying to maximize its individual distance from the enemy.&lt;br /&gt;
* '''vision_distance'''=12: (int) Only units within this distance follow the overall swarm motion (either away from an enemy or of the swarm as a whole).  The smaller this value is set, the less likely the swarm is to stay together or rejoin.  This parameter is meant to simulate how far an individual member of the swarm can see, meaning that the swarm is &amp;quot;out of sight&amp;quot; (and the unit will not be able to follow it) if it is more than this distance away.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Swarm [micro_ai] tag usage from the &amp;quot;Swarm&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=swarm&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Assassin Squad Micro AI (ai_type=assassin) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|6}}&lt;br /&gt;
&lt;br /&gt;
This Micro AI controls one or several assassin units which try to circle around enemy units with the goal of hitting a target unit without being distracted or impeded by other enemies.  The units will always attack the target if such attacks are possible, otherwise they will follow the path of least resistance toward it. Notes:&lt;br /&gt;
&lt;br /&gt;
* The main priority for the AI is to find paths that avoid enemy units. However, if no such path can be found, the assassins will still advance toward the target, taking the path exposing the units to the least potential damage from enemies.&lt;br /&gt;
&lt;br /&gt;
* The units will not necessarily follow the same path. The best path is chosen individually for each unit.&lt;br /&gt;
&lt;br /&gt;
* While it is not a requirement for the AI to work, using ambusher or skirmisher units, or units with the quick trait, makes this AI more lethal in many cases.&lt;br /&gt;
&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded, it does not work otherwise. That means that '''ca_score''' is not a valid key for the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
Enable the Assassin Squad Micro AI by using&lt;br /&gt;
 ai_type=assassin&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Assassin Squad specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the assassin units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the target unit.&lt;br /&gt;
** It is important to realize that the AI currently only works correctly with a single target unit. It is thus the responsibility of the scenario designer to make sure that this filter matches an individual enemy unit. If several enemy units are to be targeted in the same scenario, several instances of the Assassin Micro AI can be used.&lt;br /&gt;
** The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[prefer]''': A [[StandardLocationFilter|Standard Location Filter]] selecting locations on the map for the assassins to prefer when making their way toward the target unit. This could be used, for example, to make units with the ambush ability move (mostly) through forest or to have the assassins circle around the, say, northern part of the map rather than the south if that is desirable for scenario design purposes.&lt;br /&gt;
** As with avoiding enemies, the units will still move across non-preferred terrain if that is either the only option or ''much'' shorter than preferred terrain&lt;br /&gt;
** Attacking, once the target unit is in range, is not influenced by the [prefer] SLF.&lt;br /&gt;
&lt;br /&gt;
Here is an example of an Assassin Squad [micro_ai] tag usage:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=assassin&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Assassin,Master at Arms,Duelist&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             [prefer] # Circle around northern part of map&lt;br /&gt;
                 x=1-9, 10-99&lt;br /&gt;
                 y=1-37, 1-17&lt;br /&gt;
             [/prefer]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Bottleneck Defense Micro AI (ai_type=bottleneck_defense) ==&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense Micro AI lets you define a location on the map where the AI can take a defensive stance behind a narrow passage (bottleneck).  Units on the front line are backed up by healers and/or units with the leadership ability.  Injured units are moved to the back and replaced by uninjured (or less injured) units.  The units on the front line only attack when it is safe (no retaliation) or when there is a high chance that they will make a kill or level up.  Using this Micro AI only makes sense if there is no way for the enemy sides to move around the bottleneck and attack the AI's units from behind.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Bottleneck&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Bottleneck Defense Micro AI by using&lt;br /&gt;
 ai_type=bottleneck_defense&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Bottleneck Defense specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense AI requires two sets of coordinates that define where it should be taking up its defensive stance, and from which side the enemy attacks at that location.&lt;br /&gt;
&lt;br /&gt;
* '''x,y''': Comma separated lists of the hexes on the front line, where strong units are placed.  All hexes on which the AI makes contact with (can be attacked by) the enemy need to be included here.  Units are placed on them in the order in which they are listed here. {{DevFeature1.15|0}} '''location_id''' can be used instead with named/special locations.&lt;br /&gt;
* '''enemy_x,enemy_y''': Comma separated list of the hexes from which the enemy can attack the AI front line units.  This is needed for the AI to know on which side of the front line support units (healers etc.) need to be placed.  In many cases, it is sufficient to provide only one of the enemy front line hexes, but there are geometries for which that does not work.  It is therefore safer to list all enemy front line hexes here. {{DevFeature1.15|0}} '''enemy_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
By default, the AI places healers and units with the leadership ability both on the front line itself (if they are the strongest units available) and on hexes adjacent to and behind the line.  If different placement is desired, these locations can be overridden with the following keys.&lt;br /&gt;
&lt;br /&gt;
* '''active_side_leader'''=no: (boolean)  If set to 'yes', the side leader participates in the bottleneck defense action after the side's gold has been spent.  If set to 'no' (default), the leader follows default AI behavior (sitting on the keep and recruiting, for the most part).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Bottleneck Defense AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.15|3}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the units.  Only AI units matching this filter participate in the bottleneck defense.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''healer_x,healer_y''': Comma separated list of hexes where healers are placed.  This can be used to keep healers from the front line (or to take up only certain positions on the front line), and/or to override the default healing positions behind the line.  Healers are placed on them in the order in which the hexes are listed, with the exception that hexes on the line always take priority over hexes behind the line. {{DevFeature1.15|0}} '''healer_loc''' can be used instead with named/special locations.&lt;br /&gt;
* '''leadership_x,leadership_y''': Same as ''healer_x,healer_y'', but for units with the leadership ability. {{DevFeature1.15|0}} '''leadership_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
As an example, in the &amp;quot;Bottleneck&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]], there are three front line hexes, two of which are on hill terrain, while the third is a road with a lower defense rating.  The healer and side leader are supposed to participate in combat because they are strong units, but only from the hill hexes to keep them safer.  This is accomplished by using the following keys settings (check out the scenario to see which hex is which):&lt;br /&gt;
 [micro_ai]&lt;br /&gt;
     side=1&lt;br /&gt;
     ai_type=bottleneck_defense&lt;br /&gt;
     action=add&lt;br /&gt;
 &lt;br /&gt;
     x=14,14,14&lt;br /&gt;
     y= 7, 9, 8&lt;br /&gt;
     enemy_x=13,13&lt;br /&gt;
     enemy_y= 8, 9&lt;br /&gt;
 &lt;br /&gt;
     healer_x=14,14,15,15&lt;br /&gt;
     healer_y= 7, 9, 8, 9&lt;br /&gt;
     leadership_x=14,14,15,15&lt;br /&gt;
     leadership_y= 7, 9, 9 ,8&lt;br /&gt;
     active_side_leader=yes&lt;br /&gt;
 [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Fast Micro AI (ai_type=fast_ai) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|0}}&lt;br /&gt;
&lt;br /&gt;
This AI is meant as a fall-back AI for scenarios where there are so many units that attacks and moves done by the default AI are slow with long delays before each move.  It replaces the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions (CAs).  It should be obvious that in order to achieve the faster speed, compromises concerning the quality of the moves have to be made.&lt;br /&gt;
&lt;br /&gt;
Enable the Fast Micro AI by using&lt;br /&gt;
 ai_type=fast_ai&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Combat candidate action:'''&lt;br /&gt;
* Attackers are considered individually one at a time.  The best attack found for a unit is executed without checking whether a better attack by another unit might be possible.&lt;br /&gt;
* The default order in which attackers are considered is by decreasing hitpoints, starting with the strongest unit (in terms of HP).  This can be modified with the optional keys below.&lt;br /&gt;
* By default, if weaker units block attack hexes for a stronger unit, they do not move out of the way (this is the same as for the default AI).  If a hex becomes available after an attack (e.g. due to a weaker unit dying in the attack), all attacks that were possible ''before'' this happened are finished first.  Only after that are attacks by the stronger unit reconsidered.&lt;br /&gt;
* If include_occupied_attack_hexes=yes is set, weaker units move out of the way for stronger units if possible.  This can, however slow down the AI depending on the situation on the map.  Use with caution.&lt;br /&gt;
* {{DevFeature1.13|2}} Previous to this version, attacks by the leader were bunched in with the attacks by the other units.  Now leader attacks are treated separately and contain an extra layer of analysis to determine whether the attack position is &amp;quot;safe&amp;quot; (in an approximate way).  Currently, this is done with two simple considerations:&lt;br /&gt;
** Count all enemy units that can reach the leader's attack position.  If there are more than given by optional parameter 'leader_attack_max_units' (default value 3), do not attack.&lt;br /&gt;
** Add up the hitpoints of all enemies that can reach the leader's attack position.  If this value, multiplied by optional parameter 'leader_weight'(default value 2), is larger than the leaders hitpoints, do not attack.&lt;br /&gt;
** Two optional parameters, 'threatened_leader_fights' and 'leader_additional_threat', can also be used to let the leader attack when the threat at the attack hex is comparable to that at the leader's current position.&lt;br /&gt;
** In order to keep this AI as fast as possible, leader attacks are done by a separate candidate action that is only evaluated when all other CAs are done.&lt;br /&gt;
&lt;br /&gt;
'''Move-to-targets, villages and retreat candidate actions:'''&lt;br /&gt;
* The move-to-targets, villages and retreat candidate actions of the default AI are removed.  Move-to-targets and villages are taken over by the Fast Micro AI, whereas retreat is simply not done.&lt;br /&gt;
* This CA moves units toward goals after attacks are done.  Goals are unowned (if the AI side has a leader only) and enemy-owned villages and enemy leaders.&lt;br /&gt;
* For each goal, units are pre-sorted in order of the fewest moves needed to cover the ''distance'' toward the goal (that is, as if move cost on each hex toward the goal were 1).  Units are then considered sequentially and if a unit can get there &amp;quot;efficiently&amp;quot;, its move is executed without considering other units.&lt;br /&gt;
** A move is considered efficient if the move cost to get to the hex is not more than 'move_cost_factor' larger than the distance to the hex.  'move_cost_factor' is an optional key that defaults to 2.  It can be set to values of 1.1 or larger (see below).&lt;br /&gt;
** If the move for the current unit is not found to be efficient, the AI considers the next unit instead.  If no unit can do an efficient move toward the goal, the best (fastest) of the inefficient moves is selected instead.&lt;br /&gt;
&lt;br /&gt;
* Village goals:&lt;br /&gt;
** Village goals are considered before enemy leader goals so that the AI preferentially chooses scouts (or otherwise fast units) for them.&lt;br /&gt;
** The number of units sent toward villages depends on the value of the default AI aspect village_value.  It is set to village_value/4 times the number of units the AI has left to move.  Thus, by default (village_value=1.0) the AI sends a quarter of its units toward villages, and the rest toward enemy leaders.  This means that the meaningful range of values for village_value is 0..4.&lt;br /&gt;
** Only one unit is moved toward each village.  This consideration also overrides the number of units to move toward villages defined by village_value, topping it at the number of unowned/enemy-owned villages on the map.&lt;br /&gt;
** Setting village_value=0 disables moving units toward villages altogether.  That is, not only are no units chosen, but the village targeting phase of the AI is entirely disabled with no evaluation time used for it.  Note that, unlike with the default AI, this also means that no villages within range of an AI unit are grabbed.&lt;br /&gt;
** The order in which villages are chosen as goals is some combination of them being close to the AI side's leader and being spread out over the map.&lt;br /&gt;
&lt;br /&gt;
* Enemy leader goals:&lt;br /&gt;
** The order of enemy leaders goals is set according to their distance from the AI leader with the closest enemy leader being targeted first (if the AI has a leader, otherwise their order is random).  One unit is sent toward each enemy leader in this order, after which the AI starts with the first enemy leader again.  This is repeated until all units have moved.&lt;br /&gt;
&lt;br /&gt;
* Guardians (units with ai_special=guardian set) are ignored by the fast move-to-targets CA.&lt;br /&gt;
&lt;br /&gt;
'''Tips for speeding up/modifying the Fast Micro AI in specific scenarios:'''&lt;br /&gt;
&lt;br /&gt;
A number of optional keys and default AI aspects can be used to modify the behavior of the Fast Micro AI.  The MAI is set up so that it is a reasonable compromise between speed and quality of the moves in most situations.  However, this might not be ideal for all scenarios here are some things that can be done to speed up the AI (or to get better behavior in some situations by doing the opposite):&lt;br /&gt;
* Use large values for the default AI aspect aggression.  The largest delays before attacks happen (in some circumstances) when the Fast MAI has to consider lots of units with attacks left but decides not to attack with them.  See below for a description of how aggression is used by the fast AI.&lt;br /&gt;
* Use large values of move_cost_factor (see above for description of what this does).  If you want to emphasize units that can move across terrain easily (e.g. bats) to be chosen for village grabbing, you might want to use low values of move_cost_factor, but this might slow down the AI.  The default value should work reasonably well in most standard situations, but modifying the value might work better on specific maps.&lt;br /&gt;
* Don't use dungeon_mode=yes unless absolutely necessary, even in most dungeon crawls.  Test the AI without this setting first and only use it if it is the only way to solve the problem.  Often, a minor tweak to the map will have the same effect, without the speed penalty.&lt;br /&gt;
* Use include_occupied_attack_hexes=yes with caution.  It might (or might not) slow down the attack CA significantly, depending on the scenario.&lt;br /&gt;
* Set village_value=0, which makes the AI not consider villages as goals.  This has not been found to make a large difference on the maps tested so far, but it might have an effect on maps with a very large number of villages.&lt;br /&gt;
* Use attack_hidden_enemies=yes on maps where there are no hidden enemies. Excluding hidden enemies from the attack calculation requires an additional filter to be evaluated that takes extra time. In most cases this will not result in a large computation time saving.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_hidden_enemies'''=no: (boolean)  By default, hidden enemies are excluded from the attack calculation.  Setting this to 'yes' makes the AI also consider those enemies as attack targets.&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.13|2}} A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  As with the default AI, units will not step on hexes defined by the [avoid] tag.  In addition, no goals will be chosen that lie on those hexes.  The one minor exception is that a unit stepping out of the way for another unit might temporarily enter the avoided zone.&lt;br /&gt;
* '''dungeon_mode'''=no: (boolean)  As much as possible, the Fast Micro AI works with distances between hexes rather than path finding.  This works pretty well on open maps and even most dungeon crawls (and similar scenarios), but it might result in some units getting temporarily (and in rare cases permanently) stuck on maps with convoluted labyrinths.  If that is the case, setting dungeon_mode=yes triggers more path finding that avoids such situations (but as a result is somewhat slower).&lt;br /&gt;
* '''include_occupied_attack_hexes'''=no: (boolean) If set to 'yes', the AI moves weaker units out of the way for a stronger attacker.  This might lead to better attacks but is slower, so don't use if speed is essential.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the attackers.  Only AI units matching this filter will attack, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes the side= key set to the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the enemies.  Only enemy units matching this filter will be attacked, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''leader_weight'''=2: (number &amp;gt;= 0) {{DevFeature1.13|2}} A factor specifying how much more valuable the AI considers side leaders than non-leader units in the attack evaluation. This is also used to determine whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''leader_additional_threat'''=1: (number &amp;gt; 0) {{DevFeature1.13|2}} If threatened_leader_fights=yes, the threat at the attack position may be larger by this factor for the leader to leave the keep for an attack.&lt;br /&gt;
* '''leader_attack_max_units'''=3: (number &amp;gt;= 0) {{DevFeature1.13|2}} A number specifying how many enemy units within reach are acceptable when determining whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''move_cost_factor'''=2: (number &amp;gt;= 1.1)  The factor by which the move cost to a goal hex may be larger than the distance to the goal hex for a move to be considered &amp;quot;efficient&amp;quot;.  See description above.  1.1 is used if this is set to values smaller than 1.1.  Smaller values might lead to better behavior, while larger values might speed up the AI.&lt;br /&gt;
* '''weak_units_first'''=no: (boolean) If set to 'yes', inverts the order in which units are considered by the combat CA.  Attacks are then done in order of increasing hitpoints of the attackers.&lt;br /&gt;
* '''skip_combat_ca'''=no: (boolean) If set to 'yes', the fast combat CA is not used.  The default AI's high_xp_attack and combat CAs remains in place in this case.&lt;br /&gt;
* '''skip_move_ca'''=no: (boolean) If set to 'yes', the fast move-to-targets CA is not used.  The default AI's retreat, villages and move-to-targets CAs remains in place in this case.&lt;br /&gt;
* '''threatened_leader_fights'''=no: (boolean) {{DevFeature1.13|2}} By default, the AI leader will not attack if the threat at the attack position is too high.  If this key is set to 'yes', the leader will attack if the threat there is not larger than the threat at his current position multiplied by the value of 'leader_additional_threat'. This is meant as an option for the endgame: when the AI leader is under attack, he might as well go down fighting.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''aggression''': The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to set how aggressive the AI should attack.  It works in roughly the same way as the default AI aspect:&lt;br /&gt;
** The value of (1 - aggression) determines how much the AI values its own units compared to the enemy's units.  Thus, aggression=0 means that attacks are only done if the AI expects to deal (roughly) the same amount of damage as it will receive.  aggression=1 means that expected damage to the AI's units is not considered, only the expected damage to enemy units matters.&lt;br /&gt;
** Damage is calculated as a combination of hitpoints and chance to die, in &amp;quot;gold equivalent units&amp;quot;.&lt;br /&gt;
** Unlike the default AI, aggression is set to 1 for values larger than 1.  Allowed values are thus anything from -infinity to +infinity, although only -infinity to +1.0 make sense.&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.13|2}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
*'''attacks aspect''':  {{DevFeature1.13|2}} The standard RCA AI [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] used to filter own and enemy units for attacks is now also taken into account as long as neither [filter] nor [filter_second] (see above under 'optional keys') are defined in the [micro_ai] tag.&lt;br /&gt;
*'''village_value=1''': (number ranging from 0 to 4) The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to define how much villages should be targeted by the move-to-targets CA.  In the Fast MAI, this defines which fraction of the AI units are moved toward village goals (see above for details).  If set to 0, villages are ignored as goals.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded to the same values as those of the respective [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|default candidate actions]].  ca_score is therefore not a valid optional key.&lt;br /&gt;
* The fast Micro AI modifies (removes) the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions.&lt;br /&gt;
&lt;br /&gt;
Enabling the Fast Micro AI in its default configuration is as simple as this usage example from the &amp;quot;Fast AI&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=fast_ai&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Generalized Goto Micro AI (ai_type=goto) ==&lt;br /&gt;
&lt;br /&gt;
The Goto Micro AI provides a highly configurable method of sending a unit (or units) to a location or set of locations.  The units to be moved are defined using a [[StandardUnitFilter|Standard Unit Filter]] (SUF) [alternatively, all side units are used if the SUF is not provided), while the goto locations are given in a [[StandardLocationFilter|Standard Location Filter]] (SLF).&lt;br /&gt;
&lt;br /&gt;
By default, each unit matching the SUF will move toward the closest goal (in terms of turns needed to get there) matching the SLF via the fastest route given the current situation on the map (locations of enemy units etc.), but this behavior can be influenced by the optional keys listed below.  Note that this means that a unit's goal might change from one turn to the next depending on the situation on the map.&lt;br /&gt;
&lt;br /&gt;
The behavior of the unit(s) once they reach their goal location(s) is configurable as well.  By default, they will stay there until the Goto Micro AI is deleted or changed.&lt;br /&gt;
&lt;br /&gt;
For demonstrations of several Goto Micro AI usages, check out the &amp;quot;Goto&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Goto Micro AI by using&lt;br /&gt;
 ai_type=goto&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Goto specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the possible goal locations for the move.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists). Note that the area defined by [avoid] is not entered at all, even if the end point of the move would lie outside the area again.&lt;br /&gt;
* '''avoid_enemies''': (number&amp;gt;0) By default, units controlled by the Goto Micro AI will not take enemy units into account for route finding, other than the enemies' ZoCs.  By contrast, if this key is set, they will try to avoid traversing hexes on which they can be attacked by enemies.  The larger the value of ''avoid_enemies'', the more the units will try to stay out of the enemies' way.  Notes:&lt;br /&gt;
** Units are willing to take a path that is 'avoid_enemies' movement points longer for each enemy unit that can be avoided by doing so for each hex along the path.  As an example, if avoid_enemies=1 and the shortest path crosses 2 hexes that can be reached by 3 enemies each, the unit is willing to take a path that is up to 6 hexes longer instead.  If avoid_enemies=10 in the same situation, the unit will take a path up to 60 hexes longer.&lt;br /&gt;
** Hexes adjacent to enemy units are always strongly avoided, independent of the value of avoid_enemies.&lt;br /&gt;
** avoid_enemies is ignored if 'use_straight_line=true' is set.&lt;br /&gt;
** Due to an issue with the path finding code, '''tunnels and teleports are ignored in Wesnoth 1.14 and before''' if 'avoid_enemies' is used. This has been fixed in Wesnoth 1.15.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units to be moved.  Defaults to all units of the side if not defined.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''ignore_enemy_at_goal'''=no and '''ignore_units'''=no: (boolean)  In the default configuration, the Goto MAI does not move units if the path to all goal hexes is blocked by enemies.  It is therefore possible to disable the Goto MAI by putting enemy units on all the goal hexes.  That can be changed if either of these two keys is set to 'yes'.&lt;br /&gt;
** 'ignore_enemy_at_goal=yes' makes the AI choose its goals while disregarding any enemy units at the goal hexes.  Even so, a stiff rating penalty is applied to goal hexes occupied by enemies, thus that unoccupied goal hexes are chosen unless all possible goals are occupied by enemies.  Note that this setting ''only'' ignores enemy units at the goal hexes themselves.  It is still possible to disable the AI by putting enemies all around the goals.&lt;br /&gt;
** 'ignore_units=yes' let's the AI ignore ''all'' enemy units when deciding which goal hex(es) to move toward.&lt;br /&gt;
** In both cases, enemies are only ignored for deciding which unit to move toward which goal hex.  For the actual move itself, all enemies and their ZoCs are considered as usual.&lt;br /&gt;
** Both options only take effect if neither avoid_enemies is used, nor use_straight_line is set to 'yes', as neither of these consider enemies as blockers. Or in other words, if avoid_enemies is used, or use_straight_line=yes, it is unnecessary to set ignore_enemy_at_goal or ignore_units to yes.&lt;br /&gt;
* '''release_unit_at_goal'''=no: (boolean) By default, a unit that reaches a goal location will stay there until the Goto Micro AI is deleted or changed (or until the conditions specified in the [goto_goals] SLF change).  If this parameter is set, a unit that has reached its goal will not be controlled by this Micro AI any more starting from the next turn.&lt;br /&gt;
* '''release_all_units_at_goal'''=no: (boolean) Same as ''release_unit_at_goal'', but this will deactivate the Goto Micro AI for all units once the first unit reaches a goal.&lt;br /&gt;
* '''remove_movement'''=yes: (boolean):  {{DevFeature1.15|11}}  By default, when the Goto MAI cannot move a unit (for example, because the path is blocked by enemies), its movement points are still removed. If this option is set to 'no', this step does not happen, so that other AI candidate actions (or even another Goto MAI with different parameters) can take over instead.&lt;br /&gt;
* '''unique_goals'''=no: (boolean)  If set to 'yes', each unit will be sent to a different goal location.  Note that the best goals are recalculated each turn, so a unit might choose a different goal this turn from what it was heading for during the previous turn if the situation on the map has changed.  Also note that if there are more units than goal locations, the left over units will not be handled by the Goto MAI but follow default behavior instead.&lt;br /&gt;
* '''use_straight_line'''=no: (boolean)  By default, units choose the route by which they will get to their goal locations in the smallest number of turns.  If this parameter is set to 'yes', a straight line route (or something pretty close to it at least) will be chosen instead, irrespective of whether this leads into a dead end, whether it is blocked by enemies, etc.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* Depending on the parameters used, the Goto Micro AI might require a lot of path finding to be done.  Thus, while it is possible to set up this AI to move many units to many different locations, a usage like that might require quite a long calculation time.&lt;br /&gt;
&lt;br /&gt;
===Goto Micro AI example usages:===&lt;br /&gt;
&lt;br /&gt;
Here are several examples of goto [micro_ai] tag usages from the &amp;quot;Goto&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
* Send a unit toward a castle (similar to the RCA AI's Goto Candidate Action, but avoiding dead ends and areas ZoCed by enemies)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
             ca_id=messenger1&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=messenger1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=6-7,2-4&lt;br /&gt;
                 terrain=C*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             ca_score=210001&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send all units of the side toward the southern border of the map&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=8&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                  y=33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             avoid_enemies=1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send units to the four corners of the map (with each unit going to a different corner).&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x=1,1,44,44&lt;br /&gt;
                 y=1,33,1,33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             unique_goals=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Set up a guardian unit that doesn't move while no enemy unit is within 8 hexes (ironically, the Goto Micro AI can be used to keep units in place, although using one of the Guardian Micro AIs might often be a better idea)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=guard1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 [filter]&lt;br /&gt;
                     id=guard1&lt;br /&gt;
                 [/filter]&lt;br /&gt;
                 [not]&lt;br /&gt;
                     [filter]&lt;br /&gt;
                         side=1&lt;br /&gt;
                     [/filter]&lt;br /&gt;
                     radius=8&lt;br /&gt;
                 [/not]&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Guardian Micro AIs ==&lt;br /&gt;
=== General Comments on the Guardian Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Guardian Micro AIs can be used to set up different kind of guardian behaviors for a unit or a set of units.  Four different types of guardians are available and are described below.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different guardian behaviors, check out the &amp;quot;Guardians&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
=== Return Guardian  (ai_type=return_guardian)===&lt;br /&gt;
&lt;br /&gt;
A 'return guardian' is a variation of the standard Wesnoth guardian. It has an assigned guard position (GP) to which it returns after attacks on approaching enemies:&lt;br /&gt;
* If at GP with no enemy in reach, do nothing.&lt;br /&gt;
* If at GP with an enemy in reach, attack. Note that the attack is done by the default AI, which might result in the guardian not attacking if the enemy is deemed too strong (this can be influenced by setting the default ''aggression'' value to a different value).&lt;br /&gt;
* If not at GP, return there, no matter whether an enemy is in reach or not.&lt;br /&gt;
* If enemies are blocking the way back to GP, do your best to move around them.&lt;br /&gt;
* If the guardian ends up next to an enemy on the way back, attack that enemy after the move.&lt;br /&gt;
&lt;br /&gt;
Enable the Return Guardian Micro AI by using&lt;br /&gt;
 ai_type=return_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''return_x,return_y''': The coordinate of the hex the unit returns to after each attack.  {{DevFeature1.15|0}} '''return_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=100010: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  This AI consist of 2 CAs, one with score &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt; the other with &amp;lt;tt&amp;gt;ca_score-20&amp;lt;/tt&amp;gt;.  The default value is set so that the first CA gets executed right before the default combat CA, and the other right after.  It usually will not make sense to change this value, but it is possible to do so for custom applications.  {{DevFeature1.13|6}}: This is now changed to 100100 and 99900 in order to avoid conflicts with the new high_XP_attacks candidate action.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a return guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=return_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=return1&lt;br /&gt;
             return_x,return_y=20,2&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
===Stationed Guardian (ai_type=stationed_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A 'stationed guardian' is another variation of the standard Wesnoth guardian with a somewhat more complex behavior than that of the 'return guardian'. Two positions are defined for it, a 'station' and a 'guarded location', as well as a distance. The behavior is as follows:&lt;br /&gt;
* If no enemy is within 'distance' of the guard's current position, do nothing.  This is independent of the guardian being at its station or not.&lt;br /&gt;
* Otherwise: If an enemy is within 'distance' of the guard, but not also within the same distance of the guarded location and the station (all of this simultaneously), move the guard in the direction of the station (or keep the guard in place if the station is unreachable).&lt;br /&gt;
* Otherwise:&lt;br /&gt;
** Pick the enemy unit that is closest to the guarded location.&lt;br /&gt;
** If we can reach it, pick the adjacent hex with the highest defense rating and attack from there.&lt;br /&gt;
** If not in reach, move toward this unit.&lt;br /&gt;
&lt;br /&gt;
Enable the Stationed Guardian Micro AI by using&lt;br /&gt;
 ai_type=stationed_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance parameter as explained above&lt;br /&gt;
* '''station_x,station_y''': The x and y position of the hex that serves as the guardians station. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''guard_x, guard_y''': The x and y position of the guarded location.  {{DevFeature1.13|0}}  These are now optional keys, defaulting to station_x,station_y if not provided. {{DevFeature1.15|0}} '''guard_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a stationed guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=stationed_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=stationed1&lt;br /&gt;
             distance=4&lt;br /&gt;
             station_x,station_y=2,14&lt;br /&gt;
             guard_x,guard_y=3,13&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Coward (ai_type=coward) ===&lt;br /&gt;
&lt;br /&gt;
Cowards are units that, like guardians, sit around doing nothing until an enemy comes into range.  Unlike guardians, however, they run away once enemies approach. Applications might be wild animals, unarmed civilians getting in the way of a battle, etc.  The coward macro can be called with two optional locations, 'seek' and 'avoid':&lt;br /&gt;
* If neither is given, the coward retreats to the position farthest away from the approaching enemies.&lt;br /&gt;
* If 'seek' is given, it preferentially goes toward that location (but getting away from enemies takes priority).&lt;br /&gt;
* If 'avoid' is given, it in addition tries to avoid that location (with both maximizing distance from enemies and going toward 'seek' taking priority).&lt;br /&gt;
* Both 'seek' and 'avoid' may consist of only one coordinate ('x' or 'y'), in which case not a single hex, but a line of hexes is sought or avoided.&lt;br /&gt;
&lt;br /&gt;
Enable the Coward Micro AI by using&lt;br /&gt;
 ai_type=coward&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance within which an enemy must be to &amp;quot;scare&amp;quot; the unit&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_if_trapped'''='no':  {{DevFeature1.13|0}}  With the default settings, a coward never attacks.  If this key is set to 'yes', the coward attacks the weakest (lowest hitpoints) enemy next to which it ends up after moving (such as a cornered animal would do, blindly lashing out without regards for its own safety).&lt;br /&gt;
* '''avoid_x, avoid_y''': The x and y coordinate of the hex to avoid. {{DevFeature1.15|0}} '''avoid_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units from which the cowards flee.  If not set, this defaults to all enemy units.  Note, however, that no &amp;quot;enemy of AI side&amp;quot; condition is added if [filter_second] is specified, meaning that it can include allied and/or enemy units as desired (and even units on the same side as the coward).&lt;br /&gt;
* '''seek_x, seek_y''': The x and y coordinate of the hex to seek. {{DevFeature1.15|0}} '''seek_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a coward [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=coward&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=coward3&lt;br /&gt;
             distance=5&lt;br /&gt;
             seek_x,seek_y=24,5&lt;br /&gt;
             avoid_x,avoid_y=24,15&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Zone guardian (ai_type=zone_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A zone guardian is a unit that, as the name says, guards a zone. It moves randomly inside this zone until an enemy enters it (or a separately defined enemy zone, see below). Applications might be the defense of a castle, a nesting area or for protecting an unit.  The zone macro can be called with an optional SLF, [filter_location_enemy]:&lt;br /&gt;
* If not specified, the zone guard attacks any enemy coming inside its guard zone.&lt;br /&gt;
* If [filter_location_enemy] is given, it attacks any enemy entering this zone and once there are no more enemies, it goes back to patrol in its basic zone.&lt;br /&gt;
&lt;br /&gt;
Enable the Zone Guardian Micro AI by using&lt;br /&gt;
 ai_type=zone_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone to guard.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_enemy]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone where enemies have to be attacked.  If not given, it defaults to [filter_location].&lt;br /&gt;
* '''station_x,station_y''': By default, the zone guardian moves randomly within the zone if there is no enemy to attack.  However, if station_x/y is given, it moves to this position instead during turns when it does not have an enemy to attack.  This position does not need to be inside the zone defined above.  Note: obviously it does not make sense to define [filter_location], [filter_location_enemy] and station_x/y all at the same time.  Only [filter_location] and either one of the latter two (or none of them) should be provided for any zone guardian. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a zone guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=zone_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=zone3&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=22-31,4-11  # This is intentionally chosen to extend past the lake&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             station_x,station_y=32,8&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Hang Out Micro AI (ai_type=hang_out) ==&lt;br /&gt;
&lt;br /&gt;
The Hang Out Micro AI instructs units of the AI side to &amp;quot;hang out&amp;quot; around a certain location, or locations, until a condition for mobilizing the side is met.  If none of the optional keys are given, the AI controls all units of the side to hang around the leader while moving off castle tiles to make room for recruiting.&lt;br /&gt;
&lt;br /&gt;
The AI works by moving each unit to its best hex first using the minimum amount of movement points required for this move.  Once all AI units are in position, movement points are set to zero for all of them simultaneously such that they cannot be used by other AI candidate actions.  This is done because the mobilizing condition might be met as result of one of the moves of the Hang Out AI, after which we want units to be able to continue with other AI actions.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Hang Out and Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Hang Out Micro AI by using&lt;br /&gt;
 ai_type=hang_out&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Hang Out specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  In its default configuration, the Hang Out MAI controls all units of a side, keeping them as close to the side leader(s) as possible while moving off castle tiles to make room for recruiting.  For this purpose, the candidate action score defaults to 170,000, which is just below the score of the default recruiting CA.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''': A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  If neither of these two exists, this parameter is set to default to all castle terrain (&amp;lt;tt&amp;gt;terrain=C*,C*^*,*^C*&amp;lt;/tt&amp;gt;) so that units move off the castle to make room for recruiting.  Note that this does not include keep terrain, otherwise the leader would be moved off the keep as well.&lt;br /&gt;
* '''ca_score'''=170000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 170,000, which means that it is executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Hang Out action (&amp;lt;tt&amp;gt;side=&amp;lt;/tt&amp;gt; is always set and does not need to be provided).  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the locations ''near'' which the AI units should hang out.  This does not mean that the units only move onto terrain specified here, but that they minimize the distance to such terrain.  It defaults to the position of the side leader(s).&lt;br /&gt;
* '''[mobilize_condition]''':  Contains ([[ConditionalActionsWML#Condition_Tags|WML Condition Tags]])  Once the condition set up here has been met, the Hang Out MAI is disabled and the side goes over to other AI activity.  The Hang Out MAI remains disabled after this, even if the condition is not met any more later in the scenario.&lt;br /&gt;
* '''mobilize_on_gold_less_than''': (integer) Since side gold cannot be checked in a WML condition tag without storing it in a variable first, this key is provided separately.  It disables the Hang Out Micro AI once the side gold goes below the value specified here for the first time.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
* The Hang Out AI still takes effect if no locations to hang out at are found or if all map locations are to be avoided.  In that case, it simply keeps all units at their current locations.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Hang Out Micro AI configuration from the &amp;quot;Hang Out and Messenger&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hang_out&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [avoid]&lt;br /&gt;
                 terrain=C*,H*,M*,A*,S*,*^F*&lt;br /&gt;
             [/avoid]&lt;br /&gt;
             [mobilize_condition]&lt;br /&gt;
                 [have_unit]&lt;br /&gt;
                     side=1&lt;br /&gt;
                     count=7-99&lt;br /&gt;
                 [/have_unit]&lt;br /&gt;
             [/mobilize_condition]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Healer Support Micro AI (ai_type=healer_support) ==&lt;br /&gt;
&lt;br /&gt;
The Healer Support Micro AI configures the healers of a side to stay behind the battle lines and heal injured and/or threatened units rather than participate in combat under all circumstances.  You can set different levels of aggressiveness for the healers.  &lt;br /&gt;
&lt;br /&gt;
'''Notes'''&lt;br /&gt;
* By default, ''all'' healers of the AI side are controlled by this AI.  This includes in particular also the side leader, if the leader is a healer.  If you do not want the leader to be included (and possibly be moved away from the keep etc.) use the [filter] tag with canrecruit=no (see below).&lt;br /&gt;
* It is not always better to use this Micro AI than the default AI, as it takes healers (which can be strong units) away from the pool of units to choose for attacks.&lt;br /&gt;
* {{DevFeature1.15|3}} This Micro AI now takes unit guardian status and the passive_leader aspect into account.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Healers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Healer Support Micro AI by using&lt;br /&gt;
 ai_type=healer_support&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Healer Support specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the healer units.  If set, only units on the AI side passing this filter ''and'' having the healing ability are considered by this Micro AI.&lt;br /&gt;
*'''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting units to receive healing. If set, only units on the AI side passing this filter ''and'' the other healee selection criteria will be selected for healing by this Micro AI.&lt;br /&gt;
* '''aggression'''=1.0: (float) Sets the aggressiveness of the AI.  This parameter is set up as a float to accommodate future functionality, but it currently acts as a boolean: if set to zero, the AI will never let its healers participate in combat, for all other values it allows them to attack after all other units have attacked and if the healers cannot find any more units to support.  The former behavior might be appropriate in scenarios with only one or few valuable healers, while the latter might work better in scenarios with many healers.&lt;br /&gt;
* '''injured_units_only'''=no: (boolean) If set to 'yes', the AI will only move healers next to units that are already injured and skip units that currently have full hitpoints, but might get injured during the next enemy turn.&lt;br /&gt;
* '''max_threats'''=9999: (integer) The maximum allowable number of enemies that can attack a hex in order for it to be considered for a healer.  As an example, setting 'max_threats=0' means that the AI only moves healers to locations that are entirely safe from the enemy (assuming that none of the units currently on the map dies).  Note that the value of this key is checked against the number of enemies that can make it to the hex, not the number of adjacent hexes from which the healer could be attacked.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI. Note: as attacks by the healers are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The Healer Support Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded.  It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Healer Support Micro AI configuration from the &amp;quot;Healers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             aggression=0&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Lurkers Micro AI (ai_type=lurkers) ==&lt;br /&gt;
&lt;br /&gt;
The Lurker Micro AI controls some or all units of a side in &amp;quot;lurker mode&amp;quot;.  A lurker is a unit that is capable of moving across most terrains, but that only stops on and attacks from specific terrain.  It might also have the ability to hide on this terrain (which is the reason why it is called a lurker).&lt;br /&gt;
&lt;br /&gt;
Lurkers move individually without strategy and always attack the weakest enemy within their reach.  If no enemy is in reach, the lurker does a random move instead - or it just sits and waits (lurks).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of different types of lurker behavior, check out the &amp;quot;Lurkers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Lurkers Micro AI by using&lt;br /&gt;
 ai_type=lurkers&lt;br /&gt;
in the [micro_ai] tag&lt;br /&gt;
&lt;br /&gt;
===Lurkers specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
* '''[filter]''': Specifies which units of the side are controlled by the Lurker Micro AI.  This is a [[StandardUnitFilter|Standard Unit Filter]].  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': Specifies the terrain from which the lurkers attack. This is a [[StandardLocationFilter|Standard Location Filter]].&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_wander]''': Specifies the terrain on which lurkers end their moves if there is no enemy in reach. This is a [[StandardLocationFilter|Standard Location Filter]].  If [filter_location_wander] is not given, it defaults to [filter_location].&lt;br /&gt;
* '''stationary'''=no: (boolean)  If set to yes, a lurker never moves if there is no enemy within attack range.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Lurkers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=4&lt;br /&gt;
             ai_type=lurkers&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Naga Fighter&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=W*,S*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             [filter_location_wander]&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location_wander]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Messenger Escort Micro AI (ai_type=messenger_escort) ==&lt;br /&gt;
&lt;br /&gt;
The Messenger Escort Micro AI lets you define a set of waypoints through which it tries to move one or several of its units, the messenger(s).  The other units escort the messenger, trying to protect it and attacking enemies that are in its way.  Note that a messenger might not strictly hit intermediate waypoints, but that getting close is good enough, especially if a waypoint is occupied by an enemy unit.  It does, however, always try to get to the last waypoint.&lt;br /&gt;
&lt;br /&gt;
'''Notes on the use of several messengers:''' {{DevFeature1.13|0}}  &lt;br /&gt;
* This Micro AI can control several messengers at the same time, which can but do not have to move in a group.  They can be put on the map altogether in one location, or be spread out on the map or across different turns.&lt;br /&gt;
* The farther a messenger has moved through the chain of waypoints, the higher a priority it is for the AI, meaning that farther advanced (on the map) messengers are moved first, and that the escort units try to clear the way and stay with those messengers more so than with those further back on the map.  This order can be inverted using the invert_order= key.&lt;br /&gt;
* If several messengers are on the map, escort units might move from one to the other, the AI making a judgement call where they are needed most.  Closeness to a messenger is generally the most important criterion, but not the only one.&lt;br /&gt;
* If a messenger dies, the escort units previously staying with it now join one of the other messengers if there are any left (otherwise they follow default AI behavior).&lt;br /&gt;
* If you want certain escort units to stay always with the same messenger, use several messenger [micro_ai] tags with different [filter]/[filter_second] combinations.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Messenger Escort Micro AI by using&lt;br /&gt;
 ai_type=messenger_escort&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Messenger Escort specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': {{DevFeature1.13|0}} (Previous to 1.13.0, only id= was allowed.) The id of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated list of waypoint coordinates through which the messenger will go in the given order to reach its goal, given by the last coordinate.  If you want the messenger to go directly to the goal, simply enter a single x,y coordinate here.  Note that the messenger does not have to hit each waypoint exactly (except for the final one), getting close to them is good enough. {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|11}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Messenger Escort AI consists of 3 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-2&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_death_chance'''=0.67 and '''messenger_death_chance'''=0.0: (both floats; ranging from 0 to 1)    If the messenger is adjacent to an enemy at the end of the move, it will only attack the enemy if the chance of death of the enemy is &amp;gt;= enemy_death_chance and if the messenger's chance of death is &amp;lt;= messenger_death_chance (or if it is an attack at a range for which the enemy does not have a weapon).&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|0}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the escort units.  If not set, this defaults to all enemy units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''invert_order'''=no: {{DevFeature1.13|0}} If set to 'yes', the order of priority with which messenger units are considered is inverted, starting with the messenger unit that is the least advanced on the map (see description above).  In most cases, selecting this option results in worse performance of the AI, so use it with caution.  (And obviously it has no effect at all if there is only one messenger controlled by the AI.)&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|11}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Messenger Escort&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=messenger_escort&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=messenger&lt;br /&gt;
             waypoint_x=31,24,27,28&lt;br /&gt;
             waypoint_y=20,14,7,1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Patrol Micro AI (ai_type=patrol) ==&lt;br /&gt;
&lt;br /&gt;
The defines a number of waypoints for a unit (or units) to follow.  Options can be set whether the route should be followed in a loop, as an out-and back, only once, whether units encountered along the way should be attacked, etc.&lt;br /&gt;
&lt;br /&gt;
With the default settings, getting to the next waypoint takes priority over attacking for a patroller, but this can be overridden (as of '''Wesnoth 1.15.12''', using the '''attack_range''' option).  Unless instructed otherwise, the AI thus prefers to move the patrol unit around enemies rather than straight for them.  Also, if a waypoint is occupied by a unit the AI is not instructed to attack, it will (eventually) abandon that waypoint once it gets close enough and move on to the next one.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different patrol behaviors, check out the &amp;quot;Patrols&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Patrol Micro AI it by using&lt;br /&gt;
 ai_type=patrol&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Patrol specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated lists of the waypoint coordinates through which the patrol travels (in order). {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack''': By default (if this key is not set), the patrol unit attacks any unit it ends up next to after a move, or that is within '''attack_range''' (see below).  Alternatively, this parameter can be set to a comma-separated list of unit ids. If that is done, the patrol only attacks these units when it ends up next to them or if it gets within attack_range.  It ignores any other unit.  This can in turn be used to have the patroller not attack at all, by setting ''attack='' to a non-existing id.  The exception to this is an enemy on the last waypoint with ''one_time_only=yes'', which will always be attacked.&lt;br /&gt;
* '''attack_range'''=1: {{DevFeature1.15|12}} If the patrol comes within this many hexes of an enemy unit, it interrupts its patrol route and attacks that enemy (assuming it still has enough movement left to get to the enemy).  The '''attack=''' instructions are taken into account for this.  Note that the patrol generally tries to move around enemies, which means that for the default value of 1, enemies are usually not within attack range, except at the end of the move.&lt;br /&gt;
* '''attack_invisible_enemies'''=no: {{DevFeature1.15|12}} If set to 'yes', the unit also attacks invisible enemies (according to the criteria set by '''attack''' and '''attack_range''').&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''one_time_only'''=no: (boolean)  If set to 'yes', the unit goes through the patrol route only once and then hangs out at the last waypoint.  It also always attacks any enemy unit on the last waypoint, independent of what ''attack='' is set to.&lt;br /&gt;
* '''out_and_back'''=no: (boolean)  By default, a patrol unit returns to the first waypoint after reaching the last, doing the patrol route as a loop.  If this parameter is set to 'yes', the unit instead reverses its course and perpetually does out and backs of the route.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Patrols&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=patrol&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Konrad&lt;br /&gt;
             waypoint_x=9,24,25&lt;br /&gt;
             waypoint_y=21,23,15&lt;br /&gt;
             one_time_only=yes&lt;br /&gt;
             attack=Gertburt&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Protect Unit Micro AI (ai_type=protect_unit) ==&lt;br /&gt;
&lt;br /&gt;
The Protect Unit Micro AI tries to move one or several units to a given location while keeping them safe from enemy attacks. It does so by keeping the protected units away from enemy units and close to friendly units. Other units on the AI side are controlled by the default Wesnoth AI.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Protect Unit&amp;quot; and &amp;quot;The Elves Besieged&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Protect Unit Micro AI by using&lt;br /&gt;
 ai_type=protect_unit&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Protect Unit specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[unit]''': A separate [unit] tag is required for each protected unit(s).  Each tag has required keys '''id''', '''goal_x''', '''goal_y''' for each unit, as shown in the example below. {{DevFeature1.15|0}} '''goal_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''disable_move_leader_to_keep'''=no: (boolean) If set to 'yes', side leaders protected by this AI do not try to return to their keep.  This is necessary for this Micro AI to work with units that are side leaders and that are supposed to move to an off-keep location.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
*The Protect Unit Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].  Depending on the parameters, it might also modify the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|move leader to keep candidate action]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded. It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;HttT: The Elves Besieged&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=protect_unit&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Delfador&lt;br /&gt;
                 goal_x,goal_y=1,2&lt;br /&gt;
             [/unit]&lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
                 goal_x,goal_y=1,1&lt;br /&gt;
             [/unit]&lt;br /&gt;
 &lt;br /&gt;
             disable_move_leader_to_keep=true&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Recruiting Micro AI==&lt;br /&gt;
=== General Comments on the Recruiting Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Recruiting Micro AI replaces the default recruitment AI by alternative recruiting patterns.  Currently there are two recruiting algorithms you can choose from, random recruiting and the rush recruiting used in the new (as of Version 1.11.1) Experimental AI (see below).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of a different recruiting pattern, check out the &amp;quot;Recruiting&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  You can also explore the unit choices by watching the Experimental AI in a multiplayer game.&lt;br /&gt;
&lt;br /&gt;
'''Note:''' Obviously, the Recruitment Micro AI modifies the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|recruitment candidate action]].&lt;br /&gt;
&lt;br /&gt;
=== Random Recruitment AI (ai_type=recruit_random) ===&lt;br /&gt;
&lt;br /&gt;
This AI randomly selects a unit type from the recruitment list of the side.  The probability of choosing each type and the behavior in low-gold situations can be influenced with optional parameters.&lt;br /&gt;
&lt;br /&gt;
Enable the Random Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_random&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* '''[probability]''': This tag sets the probability of selecting the given unit type.  It has two required sub-keys:&lt;br /&gt;
** '''type''': comma-separated list of unit types to which this probability is to be applied&lt;br /&gt;
** '''probability''': (non-negative float)  The probability with which these unit types is to be selected.  All unit types for which no [probability] tag is defined receive a default probability of 1.0, thus values should be chosen relative to that.  As an example, in the code below swordsmen and peasants are given a probability of 8, while no other units have assigned probabilities.  Thus, each of these two unit types is 8 times more likely to be selected than any other individual unit type.&lt;br /&gt;
* '''skip_low_gold_recruiting'''=no: (boolean)  By default, the random recruitment AI chooses a unit to recruit only from those types for which it has sufficient gold.  If this parameter is set to 'yes', it will instead choose from all available unit types and end recruiting for the turn if there is not enough gold to recruit it this turn (even if other, affordable unit types exist).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a random recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_random&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [probability]&lt;br /&gt;
                 type=Swordsman,Peasant&lt;br /&gt;
                 probability=8&lt;br /&gt;
             [/probability]&lt;br /&gt;
 &lt;br /&gt;
             skip_low_gold_recruiting=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Rush Recruitment AI (ai_type=recruit_rushers) ===&lt;br /&gt;
&lt;br /&gt;
This AI replaces the default recruiting with the recruiting used in the Experimental AI [[Experimental_AI#Recruitment|as described here]].&lt;br /&gt;
&lt;br /&gt;
Enable the Rush Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_rushers&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* {{DevFeature1.15|2}} '''high_level_fraction'''=0: (non-negative number) The approximate fraction of units of level 2 or higher to be recruited. This is defined as fraction of units on the map, not of new units being recruited during a turn or over several turns (which makes a difference if there are already units on the map). The effect is also cumulative per level (starting from level 2), meaning that 1/3 of L2 units will be recruited in the example above, 1/3*1/3=1/9 L3s etc. The default value is zero, which leaves the high-level unit recruiting to the default Experimental AI algorithm resulting usually in very few such units being recruited.&lt;br /&gt;
* '''randomness'''=0.1: (number)  A random number is applied to the rusher recruit engine's score to prevent the recruitment pattern from being too predictable. 0 causes no randomness to be applied, while larger numbers increase the random effect. A value of 1-2 generates results in which the random effect is approximately equal to the scored effect. Extremely high values are essentially entirely random.&lt;br /&gt;
* {{DevFeature1.17|6}} '''reset_cache_each_turn'''=no: (bool) Many of the operations of the rush recruitment evaluation are expensive and are therefore cached. For the most part, this cache can be kept from turn to turn, but in case there are events that change, for example, the map or the recruit list, this parameter can be set to 'yes' to force a reset of the cache each turn.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a rush recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_rushers&lt;br /&gt;
             action=add&lt;br /&gt;
          [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Simple Attack Micro AI (ai_type=simple_attack) ==&lt;br /&gt;
&lt;br /&gt;
The Simple Attack Micro AI is meant as an addition to the default combat candidate action for when one wants to force the AI to do attacks that it would otherwise not do (for example on units that are 1 XP from leveling) or to execute certain attacks before all others (for example, attack with all &amp;quot;expendable&amp;quot; units first).  It is not set up to be a replacement for the default attacks.  Some notes:&lt;br /&gt;
&lt;br /&gt;
* The algorithm is different from and simpler than that of the default combat CA.  For example, no attack combinations are considered, only individual attacks.  As a result, the [[AiWML#List_of_AI_Aspects|default aspects]] (aggression, caution, [avoid], ...) are not taken into account.  Also, if lots of units are involved, evaluation of this Micro AI is potentially quite a bit slower than of the default attacks.&lt;br /&gt;
&lt;br /&gt;
* If an attack matching the Standard Unit Filters described below is found, this attack will always be executed, no matter how bad the odds are.  Thus, this Micro AI can be used to force the AI to do attacks it would otherwise avoid.&lt;br /&gt;
&lt;br /&gt;
* Other than selecting the attacker/defender units, this AI is currently not configurable.  Let us know if you would like to see certain options to be added.  (No guarantees that we will add them, but we will definitely consider them.)&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Simple Attack&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=simple_attack&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Simple Attack specific keys for the [micro_ai] tag===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  It is, however, in general not desirable to use this AI without at least one of the optional Standard Unit Filters below, as this AI is not meant as a replacement of the default combat candidate action.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=110000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 110,000, which means that it is executed right before the default combat candidate action.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Simple Attack action.  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units the AI should attack.  It defaults to all enemy units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
* '''weapon'''=-1: (integer) The number of the weapon that should be used for this attack.  Weapon numbers start at 1 (not 0).  -1 stands for &amp;quot;best weapon&amp;quot; (by whatever criterion the AI chooses to use).   '''Available from Wesnoth 1.11.8'''&lt;br /&gt;
&lt;br /&gt;
Below are 2 examples of Simple Attack Micro AI configurations from the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  The first one shows how to force the AI to attack with all Soulless and Walking Corpse units first:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110000&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless,Walking Corpse&lt;br /&gt;
             [/filter]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
The second example executes attacks by Soulless on all enemy units that are 1 XP from leveling.  Note that the Lua code in the preload event is there simply in order to code the &amp;quot;1 XP from advancing&amp;quot; condition.  It is not needed for most other SUFs, as can be seen in the previous example.&lt;br /&gt;
     [event]&lt;br /&gt;
         name=preload&lt;br /&gt;
         first_time_only=no&lt;br /&gt;
         [lua]&lt;br /&gt;
             code=&amp;lt;&amp;lt;&lt;br /&gt;
                 function close_to_advancing(unit)&lt;br /&gt;
                     if (unit.experience &amp;gt;= unit.max_experience-1) then&lt;br /&gt;
                         return true&lt;br /&gt;
                     else&lt;br /&gt;
                         return false&lt;br /&gt;
                     end&lt;br /&gt;
                 end&lt;br /&gt;
             &amp;gt;&amp;gt;&lt;br /&gt;
         [/lua]&lt;br /&gt;
     [/event]&lt;br /&gt;
 &lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110001&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless  # No Walking Corpses; L0 units don't advance enemy&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 lua_function = &amp;quot;close_to_advancing&amp;quot;&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
Several Simple Attack Micro AIs can be combined for the same AI side.  The two examples above are both used (note the different values for ca_score) in the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
== Custom Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|5}}&lt;br /&gt;
&lt;br /&gt;
From 1.13.5 onwards, adding a new Micro AI can be easily done without altering any core files or overriding the micro_ai tag. The [micro_ai] tag now looks up its ''ai_type'' parameter in the &amp;lt;code&amp;gt;wesnoth.micro_ais&amp;lt;/code&amp;gt; table and uses the micro AI setup function found there to determine which keys are required and allowed and what candidate actions to add.&lt;br /&gt;
&lt;br /&gt;
A micro AI setup function takes as its single parameter the contents of the [micro_ai] tag and returns three separate values:&lt;br /&gt;
*'''required''': A list of required keys and tags, as strings. Tags are indicated by enclosing them within square brackets in the string. If a key or tag in this list is missing from the [micro_ai] tag, an error will be printed. {{DevFeature1.17|6}} In order to enable better error handling, the syntax of this table has been changed. It is now of format &amp;lt;syntaxhighlight lang='lua' inline&amp;gt;{ param1 = 'param1_type', ... }&amp;lt;/syntaxhighlight&amp;gt;. The supported parameter types can be found at the link to examples below.&lt;br /&gt;
*'''optional''': Similar to above, a list of optional keys and tags, as strings. No error will be printed if a key or tag in this list is missing, nor will an error be printed if it is present (as would be the case for unrecognized keys or tags). {{DevFeature1.17|6}} The format of this table has been changed in the same way as for 'required'. Also, a warning message is now issued if the '[micro_ai]' tag contains a key or tag that is not listed in either 'required' or 'optional'.&lt;br /&gt;
*'''ca_params''': A table specifying the candidate actions that constitute the micro AI. It should contain an ''ai_id'' key (which specifies a base ID to be used for candidate actions of this micro AI) and a series of candidate action definitions. Each candidate action definition is a table containing the following keys:&lt;br /&gt;
**'''ca_id''': The ID used for the candidate action. This will be combined with the ''ai_id'' to produce the final ID.&lt;br /&gt;
**'''location''': Path to the Lua file that defines the candidate action.&lt;br /&gt;
**'''score''': The maximum score for the candidate action. If the [micro_ai] tag specified a ''ca_score'' parameter, the score here should normally be based on that.&lt;br /&gt;
&lt;br /&gt;
(The return statement would thus look like &amp;lt;syntaxhighlight lang='lua' inline&amp;gt;return required, optional, ca_params&amp;lt;/syntaxhighlight&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
'''Examples''' of micro AI setup functions can be found in the [https://github.com/wesnoth/wesnoth/tree/1.14/data/ai/micro_ais/mai-defs default MAI definitions directory].  See the [https://github.com/wesnoth/wesnoth/blob/1.14/data/ai/micro_ais/mai-defs/animals.lua#L4-L12 Big Animals MAI definition] for a simple example.  For custom micro AIs, the setup code should be put into a ''[lua]'' tag inside a preload event.&lt;br /&gt;
&lt;br /&gt;
Since the function has full access to the contents of the [micro_ai] tag, it can conditionally add candidate actions to '''ca_params''' depending on the values of other keys within the [micro_ai] tag. It can also perform other actions such as adding facets to an aspect; if it does such things, it should pay attention to the ''action'' key to determine whether it's being added or removed.&lt;br /&gt;
&lt;br /&gt;
There are two useful helper functions that can be used by Micro AI setup functions requiring aspect changes. They can be brought into scope by a line similar to the following:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='lua&amp;gt;local MAIH = wesnoth.require(&amp;quot;ai/micro_ais/micro_ai_helper.lua&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* MAIH.add_aspects(side, aspect_params)&lt;br /&gt;
* MAIH.delete_aspects(side, aspect_params)&lt;br /&gt;
&lt;br /&gt;
The ''aspect_params'' parameter is an array of tables, each of which contains the following keys:&lt;br /&gt;
*'''aspect''': The aspect to change.&lt;br /&gt;
*'''facet''': The WML defining the facet. This must include an ''id'' key in order for the aspect to be correctly deleted. For maximum compatibility, incorporate the ''ca_id'' key from the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=69677</id>
		<title>Micro AIs</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=69677"/>
		<updated>2022-06-26T13:47:48Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* A Few General Words about Micro AIs */ [avoid] is taken into account by many MAIs&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== A Few General Words about Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
* Micro AIs allow Wesnoth campaign and scenario authors to add new AI functionality without the need for any AI programming. They bring new specialized behavior to Wesnoth that cannot be achieved, at least not easily, by adjusting default AI parameters (such as those described at [[AiWML]]) or with WML code.&lt;br /&gt;
&lt;br /&gt;
* A Micro AI is activated and configured (or deleted) via the [micro_ai] tag placed in an event, requiring only a few lines of WML code. {{DevFeature1.15|?}} Starting in 1.16 the [micro_ai] tag may also be used in [side][ai] or [unit][ai]. These are both shorthands for placing it in the prestart event with &amp;quot;action=add&amp;quot;, side set to the side, and in the case of [unit][ai], a [filter] tag set to limit the Micro AI to that one unit.&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.6''', the Micro AI behavior was inconsistent when it came to '''hidden enemies and petrified units'''.  Some Micro AIs ignored those, while others did not.  In some case there was even a potential for AI errors when an AI unit ran into hidden or petrified enemies.  As of 1.13.6, this has all been fixed.  The Micro AIs now correctly and consistently ignore hidden units and treat petrified units as blocking the hex they are on but having no effect otherwise, in the same way as the default AI does.  '''Important notes:'''&lt;br /&gt;
** It is not possible to re-include hidden/petrified units via the Standard Unit Filters (SUFs) used by some of the Micro AIs. In fact, if this is attempted, the filters will generally not match any unit.&lt;br /&gt;
*** This only applies to SUFs which go directly into the [micro_ai] tag. Any SUF that is a sub-tag of another filter (for example, of a Standard Location Filter) is taken literally and returns all units matching the filter. If hidden or petrified units are to be excluded from such a sub-filter, that needs to be specified directly in that filter.&lt;br /&gt;
** We also note that, because all the Micro AIs are written in Lua, it is possible to [[Micro_AIs#Using_Development_.281.13.29_Version_Micro_AIs_in_your_Stable_.281.12.29_Version_Add-on|move a Micro AI's code into one's add-on]] and modify it to whatever scenario-specific behavior is desired (this does not only apply to hidden/petrified units, but to all behavior). Setting up general Micro AI behavior that works in every possible setting is not feasible, but adapting it to work (only) in the specific setting of a given scenario is often quite simple. If you do not know how to do this, please post a message on the Wesnoth forums and we will be happy to help you with it.&lt;br /&gt;
&lt;br /&gt;
* The '''[[AiWML#List_of_AI_Aspects|default AI aspects]] are, for the most part, not taken into account by the Micro AIs'''.  For example, unless noted otherwise, specifying a value for ''aggression'' does not change the MAI behavior.  The exception is the ''[avoid]'' tag which is taken into account by many Micro AIs (except those for which it would interfere with the Micro AI behavior or those that already use a a [[StandardLocationFilter|Standard Location Filter]] that can be used for the same purpose).  This is a feature of the Micro AIs, not a bug.  If you feel that a certain default AI aspect should also apply to a Micro AI, please let us know in the [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 Micro AIs Feedback thread].&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.7''', the Micro AIs did not all work reliably when the '''AI side is under shroud''' and it may have been necessary to turn shroud off for the side when using a Micro AI. {{DevFeature1.13|7}} The Micro AIs now all work for sides under shroud. They do this by disregarding shroud for terrain purposes when path finding, consistent with default Wesnoth AI behavior, while still ignoring hidden units (including those hidden under shroud) correctly.&lt;br /&gt;
&lt;br /&gt;
* Most Micro AIs in their default setting apply to all units of a side, although many of them provide parameters for filtering the units that should be affected by the new behavior.  If the Micro AI is applied only to part of the units of a side, the other units follow the default Wesnoth AI behavior.&lt;br /&gt;
&lt;br /&gt;
* Micro AIs are available in mainline since Wesnoth 1.11/1.12.&lt;br /&gt;
&lt;br /&gt;
== Micro AI Test and Demo Scenarios ==&lt;br /&gt;
&lt;br /&gt;
All Micro AIs described here can be checked out in a number of test and demo scenarios.  The easiest way to access the test scenarios is by assigning a hotkey to &amp;quot;Start Test Scenario&amp;quot; in the preferences (as of Wesnoth 1.13.8). Then, simply press the hotkey in the titlescreen and select the desired scenario from the list (e.g. 'micro_ai_test' for the overall &amp;quot;switchboard scenario&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Test scenarios can also be accessed from the command line by typing&lt;br /&gt;
 path/wesnoth-executable -t micro_ai_test&lt;br /&gt;
or (depending on your system and Wesnoth version)&lt;br /&gt;
 path/wesnoth-executable -tmicro_ai_test&lt;br /&gt;
This starts up a &amp;quot;switchboard scenario&amp;quot; in which you can select the Micro AI demonstration scenario you want to check out.  Here, ''path/wesnoth-executable'' needs to be replaced by whatever the path and filename of the Wesnoth executable is on your system.&lt;br /&gt;
&lt;br /&gt;
== Setting up a Micro AI ==&lt;br /&gt;
&lt;br /&gt;
Setting up a Micro AI requires only one simple step:&lt;br /&gt;
==== Activating and configuring (or deleting) a Micro AI ====&lt;br /&gt;
&lt;br /&gt;
Micro AIs are activated, deleted and configured using the [micro_ai] tag.  This tag needs to be placed in [[ActionWML]], that is, in an event, a menu option or the like.  As an example, the following code activates the healer_support Micro AI in its default configuration for Side 2 from the beginning of the scenario:&lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
 &lt;br /&gt;
         # Configure the healer support micro AI&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
'''Keys required for all Micro AIs:'''&lt;br /&gt;
All [micro_ai] tags must contain the following three required keys:&lt;br /&gt;
* '''side''': The side for which the Micro AI is to be added, changed or deleted.  This has to be an individual side number, lists of sides are not supported.&lt;br /&gt;
* '''ai_type''': The type of Micro AI to be added, changed or deleted.  See the following sections for allowed values.&lt;br /&gt;
* '''action''' (string): The action to take concerning the Micro AI.  The following values are allowed: &lt;br /&gt;
** ''add'': Add a Micro AI to a side.&lt;br /&gt;
** ''change'': Change the configuration of an existing Micro AI.  Note that this does not only change the specific parameters provided in the tag, but it replaces the entire existing Micro AI by a new version with the new (and only the new) configuration.  It is therefore equivalent to using first the ''delete'' and then the ''add'' action.&lt;br /&gt;
** ''delete'': Delete an existing Micro AI from a side.&lt;br /&gt;
&lt;br /&gt;
'''Keys required under certain circumstances:'''&lt;br /&gt;
* '''ca_id''': (string) If several MAIs of the same type are used for the same side and one or several of these are to be changed or deleted during a scenario, this key needs to be included so that the [micro_ai] tag knows which of them needs to be changed/deleted.  The same is also true if a Micro AI is changed more than once during a scenario.  We therefore '''strongly recommend always setting ''ca_id''''' for MAIs which are, or might be, changed or deleted during a scenario.&lt;br /&gt;
** '''Note:''' The id(s)/name(s) actually assigned to the CA(s) of the MAI by the engine might be different from the parameter passed here.  It is, however, sufficient to use this ca_id with the [micro_ai] tag add/delete/change actions to identify the MAI uniquely.  (In other words: you don't have to worry about this if you always use the [micro_ai] tag, but might have to figure out the actual name(s) of the CA(s) if you want to delete or change them manually.)&lt;br /&gt;
* Other keys may also be required depending on the Micro AI.  In additional, a number of optional keys may be available to configure the AI behavior.  See the sections on the individual Micro AIs for details.&lt;br /&gt;
&lt;br /&gt;
'''Required keys for deleting a Micro AI:'''&lt;br /&gt;
&lt;br /&gt;
Only a subset of the keys required for setting up a Micro AI are needed when deleting it.  '''side''', '''ai_type''' and '''action'''=delete always need to be provided.  '''ca_id''' needs to be given if it was provided when adding the Micro AI (see above for when this is needed).&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* The ''add'' and ''change'' actions ignore all keys that do not apply to the respective Micro AI type.  The ''delete'' action ignores all keys other than the required keys listed above.&lt;br /&gt;
&lt;br /&gt;
* Some of the Micro AIs use and/or modify default AI components (candidate actions or aspects) when they are added, or reset these components to their default values when they are deleted.  Thus, if you have modified the AI yourself using the default CAs or aspects, some of these modifications might be affected, or might be interfering with the Micro AI.  The sections below indicate whether a Micro AI modifies any default AI components.&lt;br /&gt;
&lt;br /&gt;
===Combining Different Micro AI Types for the Same Side===&lt;br /&gt;
&lt;br /&gt;
No extra steps need to be taken when combining different Micro AIs on the same side.  Simply set up the [micro_ai] tags for all AIs you want to use.&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* While it is ''technically'' possible to combine any number of Micro AIs, this does not mean that all Micro AIs work together in practice.  Some are not compatible because they control units in contradictory ways.  &lt;br /&gt;
* Not all combinations that ''should'' work have been tested yet.  If you find a combination that you believe should work but doesn't, [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 please let us know] and we will see if that can be fixed.&lt;br /&gt;
* The candidate action score almost certainly has to be set (using the ca_score= key) for one or several of the MAIs if several of them are used on the same side.&lt;br /&gt;
&lt;br /&gt;
== Animals Micro AIs ==&lt;br /&gt;
=== General Comments on the Animal Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Animals Micro AIs are meant to simulate the behaviors of certain types of animals.  They are, however, set up so that they can be used with arbitrary unit types and might be applicable in quite diverse situations.  The AIs nevertheless remain named after the animals for which they were written originally, so that they are (to some extent) descriptive of their behavior.&lt;br /&gt;
&lt;br /&gt;
Brief descriptions of the AI behaviors are given in the following subsections.  To get a better feeling for their behavior, check out the &amp;quot;Animals&amp;quot;, &amp;quot;Swarm&amp;quot;, &amp;quot;Wolves&amp;quot; and &amp;quot;Dragon&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  Note that some of the animal AIs are designed for animal types that do not exist in mainline.  In the test scenario, other animal types are therefore substituted for them.&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Wolves (ai_type=wolves) ===&lt;br /&gt;
&lt;br /&gt;
The Wolves Micro AI organizes all &amp;quot;wolf&amp;quot; units (predators) of the side to hunt in a single pack.  They actively chase after the closest prey and try to corner it (not always super successfully), but are easily distracted by other enemy units coming into attack range, to the point where the pack might split up.  The wolves can, however, be told to attack only prey units as well as to try to avoid other types of units (such as larger predators).  When no prey is left, the wolves wander randomly.&lt;br /&gt;
&lt;br /&gt;
This AI works best for a small number of predators (2 - 5) and sparse prey.  Also, the predators should be put on the map in a pack, that is, reasonably close to each other.  For a demonstration of the Wolves AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Note that another wolves AI exists, called [[Micro_AIs#Animals_AI:_Multi-pack_Wolves_.28ai_type.3Dwolves_multipacks.29|Multipack Wolves]], which distributes the wolves of a side into several packs, rather than all of them into one single pack as it is done here.&lt;br /&gt;
&lt;br /&gt;
Enable the Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the predator units.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the prey units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_only_prey'''=no: In the default configuration, the wolves attack all enemy units in attack range.  If this key is set to 'yes', they only attack prey units.&lt;br /&gt;
* '''avoid_type''': Comma-separated list of unit types (on enemy sides) which the wolves try to avoid.  They try to stay out of these units' way as much as possible when moving around (except when they are moving in to attack another enemy unit) and never attack them.&lt;br /&gt;
* '''ca_score'''=90000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 90,000, which is lower than the default combat CA, but higher than most other default CAs which move units.  It can be changed by setting this parameter but in most cases this will not make sense.  Note that the Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Notes on default (RCA) AI aspects:''' &lt;br /&gt;
* If ''attack_only_prey'' or ''avoid_type'' are set, the Wolves Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* Attacks by the wolves are left to the default (RCA) AI, meaning that the value of ''aggression'' has an influence on the behavior.&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI is taken into account. Note: as attacks by the wolves are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=6&lt;br /&gt;
             ai_type=wolves&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Wolf&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Vampire Bat&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             avoid_type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Multi-pack Wolves (ai_type=wolves_multipacks) ===&lt;br /&gt;
&lt;br /&gt;
The Multi-pack Wolves Micro AI is different from the [[Micro_AIs#Animals_AI:_Wolves_.28ai_type.3Dwolves.29|Wolves Micro AI]] in that there can be an arbitrary number of wolf packs, with the pack size being a free parameter.  At the beginning of the scenario, close wolves are grouped into packs in a semi-methodical way.  If the wolves are put on the map in distinct groups of close wolves of the correct number, they will be joined into packs matching these groups.  If, on the other hand, they are spread out all over the map, the method of assigning them to packs is semi-random.&lt;br /&gt;
&lt;br /&gt;
Wolves of the same pack begin by joining each other on the map.  After that, they stay together until only one wolf is left, which then tries to join up with an incomplete pack or with other single wolves.  Individual wolves entering the map during the scenario behave in that way as well.&lt;br /&gt;
&lt;br /&gt;
A second difference to the other Wolves AI is that wolves do not actively hunt here.  For the most part they just wander (often long distance).  However, the pack ferociously (and without regard for its own health) attacks any enemy units that come into range, as long as that does not mean separating the pack by more than a few hexes.  Staying together, or joining with a new wolf assigned to the pack, is the only thing that takes priority over satisfying the wolves' thirst for blood.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Multi-pack Wolves AI behavior, check out the &amp;quot;Wolves&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Multi-pack Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves_multipacks&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Multi-pack Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''pack_size'''=3: (integer) The size of the packs.&lt;br /&gt;
* '''show_pack_number'''=no: If set to &amp;quot;yes&amp;quot;, the wolves' pack numbers will be shown below them.  Note that this means that the pack number of a wolf killed in an attack will remain as a label on the map.  This behavior is intentional to help UMC authors note where units might have made suicidal mistakes.&lt;br /&gt;
* '''type'''=Wolf: Comma-separated list of the unit types which the AI controls. The default value is &amp;quot;Wolf&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves_multipacks [micro_ai] tag usage from the &amp;quot;Wolves&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=wolves_multipacks&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             show_pack_number=yes&lt;br /&gt;
             pack_size=4&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Big Animals (ai_type=big_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Big Animals Micro AI is a simulation of large predators that wander and hunt alone, such as Bears, Giant Spiders or Yetis.  For the most part, these just wander on the terrain that's been defined for them, but they attack enemy units if those happen to come into range.  The AI can be set up so that the Big Animals stay out of the way of other units (such as other large predators), in which case they only attack those if cornered or if they run into them accidentally (such as ambushers).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Big Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Big Animals Micro AI by using&lt;br /&gt;
 ai_type=big_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the animals to which this AI is applied.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': The big animals often go for long distance (multi-turn) journeys.  This tag is a [[StandardLocationFilter|Standard Location Filter]] describing the terrain from which the next goal is chosen.  The default is all terrain on the map. &lt;br /&gt;
* '''[filter_location_wander]''': The terrain on which the big animals will end their moves. This is a [[StandardLocationFilter|Standard Location Filter]].  The default is all terrain on the map.&lt;br /&gt;
* '''[avoid_unit]''': A [[StandardUnitFilter|Standard Unit Filter]] describing all enemy units that the big animals will avoid.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
Here's an example of big_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=big_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Ghast&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [avoid_unit]&lt;br /&gt;
                 type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
             [/avoid_unit]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=1-40,1-18&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Forest Animals (ai_type=forest_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Forest Animals Micro AI simulates a number of different animal behaviors that one might find in a forest, namely animals behaving like deer, rabbits and families of tuskers.  In the following, we will refer to those animals by these types, even though the actual unit types can be selected freely.  They behave as follows:&lt;br /&gt;
* Each deer wanders randomly on (selectable) terrain, except when enemies get in its (the deer's) maximum movement range, in which case it flees to the farthest point it can reach.&lt;br /&gt;
* Tuskers exhibit the same behavior as deer, except when an enemy is next to one of the tusklets.  This enemy is attacked unconditionally by the tuskers trying to protect their young.&lt;br /&gt;
* Tusklets blindly follow the closest adult tusker, except when there is no tusker left, in which case they behave the same as deer.&lt;br /&gt;
* Rabbits also wander randomly, but in addition disappear into their holes (if any are within reach) when enemies are close.  They reappear out of their holes at the beginning of the turn, if it is safe.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Forest Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Forest Animals Micro AI by using&lt;br /&gt;
 ai_type=forest_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Forest Animals AI consists of 4 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-3&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''rabbit_type, tusker_type, tusklet_type, deer_type''': Comma-separated lists of the unit types behaving as the respective animals.  If any of these parameters are not set, those types of animal behavior are skipped by the AI.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain on which the forest animals wander when they are not threatened by enemies.&lt;br /&gt;
* '''rabbit_number'''=6: (integer)  The number of rabbits for the AI to have on the map simultaneously.  The AI puts the missing number of rabbits onto the map at the beginning of the AI's turn (when the rabbits come out of their holes).&lt;br /&gt;
* '''rabbit_enemy_distance'''=3: (number)  Rabbits won't spawn in holes that aren't more than this distance away from all enemies.&lt;br /&gt;
* '''rabbit_hole_img''': Rabbit holes are marked by items on the map.  If this parameter is set, it must be the file name of the item image.  If it is not set, all items on the map are treated as rabbit holes.&lt;br /&gt;
** {{DevFeature1.14|10}} Items used for rabbit holes on the map border used to produce an error. Placing them on the map border therefore needed to be avoided on the scenario setup side. They are now automatically excluded from consideration by the AI code.&lt;br /&gt;
&lt;br /&gt;
Here's an example of forest_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=forest_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             deer_type=Vampire Bat&lt;br /&gt;
             rabbit_type=Giant Rat&lt;br /&gt;
             tusker_type=Ogre&lt;br /&gt;
             tusklet_type=Young Ogre&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=*^F*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Herd and Herders (ai_type=herding) ===&lt;br /&gt;
&lt;br /&gt;
The Herding Micro AI sets up an area on the map where a number of herder units (such as sheep dog) control and protect a herd (such as sheep).  For convenience, we simply refer to them as sheep dogs and sheep in the following, even though arbitrary unit types can be selected for either.&lt;br /&gt;
&lt;br /&gt;
Sheep dogs try to keep their sheep safe.  This involves keeping them inside the herding area, positioning themselves in between the sheep and approaching enemies, and attacking the enemies if those get too close.  If no active herding or protecting move is needed, the dogs go to a random location on the perimeter of the herding area.&lt;br /&gt;
&lt;br /&gt;
Sheep wander aimlessly except when a sheep dog is next to them, in which case they run away from the dog.  The dogs exploit this by positioning themselves on the outside of the sheep, if possible.  Sheep also run away from approaching enemies.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Herding AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Herding Micro AI by using&lt;br /&gt;
 ai_type=herding&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herder units (e.g. dogs).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herd units (e.g. sheep).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain forming the perimeter of the herding area.  In other words, this is the path the herders (dogs) use to patrol around the outside of the herding area.  This must be a set of coordinates ''completely enclosing'' the herding area.  Here's an example of setting up a rectangular perimeter:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         x=10-20,10-20,10,  ,20&lt;br /&gt;
         y=11,   21,   11-21,11-21&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
Another possibility is by defining the path by certain terrain on the map (as it is done in the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]), in which case the filter can simply look like this:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         terrain=Rb&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
* '''herd_x,herd_y''': The coordinate of one hex (''any'' one hex) inside the herding area.  This is needed to define which part of the map is on the ''inside'' of the herding area perimeter defined by [herding_perimeter], and which is the ''outside''. {{DevFeature1.15|0}} '''herd_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''attention_distance'''=8: (integer)  If an enemy is within this distance of a herd unit, but not as close as ''attack_distance'', the herders try to position themselves in between the enemy and the herd unit.&lt;br /&gt;
* '''attack_distance'''=4: (integer)  If an enemy is within this distance of a herd animal, the herders attack this enemy.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Herding AI consists of 6 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-5&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Herding [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=herding&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Footpad&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Troll Whelp&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             herd_x,herd_y=32,28&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=Rb&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Hunter (ai_type=hunter) ===&lt;br /&gt;
&lt;br /&gt;
The Hunter Micro AI leads a unit (or units) through hunt-and-rest cycles.  The units wander in a prescribed area of the map, their hunting ground, until an enemy unit comes within range.  Note that the units do not actively chase down enemies, but wander randomly through the hunting ground until an enemy is encountered.&lt;br /&gt;
&lt;br /&gt;
If enemies are within range of a hunter unit, the hunter attacks the weakest of them.  If a kill is made, it then retreats to its rest location, where it stays for a certain number of turns and until fully healed (whatever happens later).&lt;br /&gt;
&lt;br /&gt;
A few more details:&lt;br /&gt;
* If the hunter's way home is entirely blocked on the return (so that there is no possible path), the normal RCA AI takes over its behavior.&lt;br /&gt;
* However, if the way is blocked by an enemy unit occupying the rest hex, that enemy will be attacked unconditionally.&lt;br /&gt;
* A kill only makes the hunter go home when it is the attacker, not as defender.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Hunter AI behavior, check out the &amp;quot;Dragon&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]. &lt;br /&gt;
&lt;br /&gt;
Enable the Hunter Micro AI by using&lt;br /&gt;
 ai_type=hunter&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''home_x,home_y''': The coordinates of the hex to which the hunter returns and rests at after a kill. {{DevFeature1.15|0}} '''home_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the unit's hunting ground.  If this tag is not provided, the hunter uses all of the map.&lt;br /&gt;
* '''rest_turns'''=3: (integer)  The number of turns the AI stays at home_x,home_y after a kill.  It will not leave its home until this number of turns have passed ''and'' it is fully healed.&lt;br /&gt;
* '''show_messages'''=no: (boolean) If set to yes, the hunter announces whenever its behavior changes to the next phase.  Note that this is meant as debug output for the scenario author.  The messages are not translated and are not included in replays (and are only visible to the host in an MP game).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a hunter [micro_ai] tag usage from the &amp;quot;Dragon&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hunter&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Rowck&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=5-30,1-15&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             home_x,home_y=3,15&lt;br /&gt;
             rest_turns=2&lt;br /&gt;
 &lt;br /&gt;
             show_messages=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Swarm (ai_type=swarm) ===&lt;br /&gt;
&lt;br /&gt;
The Swarm Micro AI uses some very simply algorithms to simulate animal swarm behavior.  Without adjacent enemies, they simply do a random move, trying to stay together and at a certain distance from enemies.  However, if an enemy unit is close to any bat, the swarm scatters.  This is particular fun to watch when one places an enemy unit in the middle of the swarm.  After being scattered, the swarm members slowly rejoin, but not in a very organized way.  Sub-swarms or individual bats might roam around for quite some time before they find their way back.  It is also possible that individual bats (or small groups) split off from the larger swarm at times.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Swarm AI behavior, check out the &amp;quot;Swarm&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Swarm Micro AI by using&lt;br /&gt;
 ai_type=swarm&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Swarm AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_distance'''=5: (int) The minimum distance kept between units of the swarm and enemies when the swarm moves as a whole.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.15|12}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the units.  Only AI units matching this filter participate in the swarm.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''scatter_distance'''=3: (int) Enemies within &amp;quot;scatter_distance&amp;quot; hexes of any swarm unit cause the swarm to scatter, by each unit trying to maximize its individual distance from the enemy.&lt;br /&gt;
* '''vision_distance'''=12: (int) Only units within this distance follow the overall swarm motion (either away from an enemy or of the swarm as a whole).  The smaller this value is set, the less likely the swarm is to stay together or rejoin.  This parameter is meant to simulate how far an individual member of the swarm can see, meaning that the swarm is &amp;quot;out of sight&amp;quot; (and the unit will not be able to follow it) if it is more than this distance away.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Swarm [micro_ai] tag usage from the &amp;quot;Swarm&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=swarm&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Assassin Squad Micro AI (ai_type=assassin) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|6}}&lt;br /&gt;
&lt;br /&gt;
This Micro AI controls one or several assassin units which try to circle around enemy units with the goal of hitting a target unit without being distracted or impeded by other enemies.  The units will always attack the target if such attacks are possible, otherwise they will follow the path of least resistance toward it. Notes:&lt;br /&gt;
&lt;br /&gt;
* The main priority for the AI is to find paths that avoid enemy units. However, if no such path can be found, the assassins will still advance toward the target, taking the path exposing the units to the least potential damage from enemies.&lt;br /&gt;
&lt;br /&gt;
* The units will not necessarily follow the same path. The best path is chosen individually for each unit.&lt;br /&gt;
&lt;br /&gt;
* While it is not a requirement for the AI to work, using ambusher or skirmisher units, or units with the quick trait, makes this AI more lethal in many cases.&lt;br /&gt;
&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded, it does not work otherwise. That means that '''ca_score''' is not a valid key for the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
Enable the Assassin Squad Micro AI by using&lt;br /&gt;
 ai_type=assassin&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Assassin Squad specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the assassin units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the target unit.&lt;br /&gt;
** It is important to realize that the AI currently only works correctly with a single target unit. It is thus the responsibility of the scenario designer to make sure that this filter matches an individual enemy unit. If several enemy units are to be targeted in the same scenario, several instances of the Assassin Micro AI can be used.&lt;br /&gt;
** The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[prefer]''': A [[StandardLocationFilter|Standard Location Filter]] selecting locations on the map for the assassins to prefer when making their way toward the target unit. This could be used, for example, to make units with the ambush ability move (mostly) through forest or to have the assassins circle around the, say, northern part of the map rather than the south if that is desirable for scenario design purposes.&lt;br /&gt;
** As with avoiding enemies, the units will still move across non-preferred terrain if that is either the only option or ''much'' shorter than preferred terrain&lt;br /&gt;
** Attacking, once the target unit is in range, is not influenced by the [prefer] SLF.&lt;br /&gt;
&lt;br /&gt;
Here is an example of an Assassin Squad [micro_ai] tag usage:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=assassin&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Assassin,Master at Arms,Duelist&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             [prefer] # Circle around northern part of map&lt;br /&gt;
                 x=1-9, 10-99&lt;br /&gt;
                 y=1-37, 1-17&lt;br /&gt;
             [/prefer]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Bottleneck Defense Micro AI (ai_type=bottleneck_defense) ==&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense Micro AI lets you define a location on the map where the AI can take a defensive stance behind a narrow passage (bottleneck).  Units on the front line are backed up by healers and/or units with the leadership ability.  Injured units are moved to the back and replaced by uninjured (or less injured) units.  The units on the front line only attack when it is safe (no retaliation) or when there is a high chance that they will make a kill or level up.  Using this Micro AI only makes sense if there is no way for the enemy sides to move around the bottleneck and attack the AI's units from behind.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Bottleneck&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Bottleneck Defense Micro AI by using&lt;br /&gt;
 ai_type=bottleneck_defense&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Bottleneck Defense specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense AI requires two sets of coordinates that define where it should be taking up its defensive stance, and from which side the enemy attacks at that location.&lt;br /&gt;
&lt;br /&gt;
* '''x,y''': Comma separated lists of the hexes on the front line, where strong units are placed.  All hexes on which the AI makes contact with (can be attacked by) the enemy need to be included here.  Units are placed on them in the order in which they are listed here. {{DevFeature1.15|0}} '''location_id''' can be used instead with named/special locations.&lt;br /&gt;
* '''enemy_x,enemy_y''': Comma separated list of the hexes from which the enemy can attack the AI front line units.  This is needed for the AI to know on which side of the front line support units (healers etc.) need to be placed.  In many cases, it is sufficient to provide only one of the enemy front line hexes, but there are geometries for which that does not work.  It is therefore safer to list all enemy front line hexes here. {{DevFeature1.15|0}} '''enemy_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
By default, the AI places healers and units with the leadership ability both on the front line itself (if they are the strongest units available) and on hexes adjacent to and behind the line.  If different placement is desired, these locations can be overridden with the following keys.&lt;br /&gt;
&lt;br /&gt;
* '''active_side_leader'''=no: (boolean)  If set to 'yes', the side leader participates in the bottleneck defense action after the side's gold has been spent.  If set to 'no' (default), the leader follows default AI behavior (sitting on the keep and recruiting, for the most part).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Bottleneck Defense AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.15|3}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the units.  Only AI units matching this filter participate in the bottleneck defense.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''healer_x,healer_y''': Comma separated list of hexes where healers are placed.  This can be used to keep healers from the front line (or to take up only certain positions on the front line), and/or to override the default healing positions behind the line.  Healers are placed on them in the order in which the hexes are listed, with the exception that hexes on the line always take priority over hexes behind the line. {{DevFeature1.15|0}} '''healer_loc''' can be used instead with named/special locations.&lt;br /&gt;
* '''leadership_x,leadership_y''': Same as ''healer_x,healer_y'', but for units with the leadership ability. {{DevFeature1.15|0}} '''leadership_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
As an example, in the &amp;quot;Bottleneck&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]], there are three front line hexes, two of which are on hill terrain, while the third is a road with a lower defense rating.  The healer and side leader are supposed to participate in combat because they are strong units, but only from the hill hexes to keep them safer.  This is accomplished by using the following keys settings (check out the scenario to see which hex is which):&lt;br /&gt;
 [micro_ai]&lt;br /&gt;
     side=1&lt;br /&gt;
     ai_type=bottleneck_defense&lt;br /&gt;
     action=add&lt;br /&gt;
 &lt;br /&gt;
     x=14,14,14&lt;br /&gt;
     y= 7, 9, 8&lt;br /&gt;
     enemy_x=13,13&lt;br /&gt;
     enemy_y= 8, 9&lt;br /&gt;
 &lt;br /&gt;
     healer_x=14,14,15,15&lt;br /&gt;
     healer_y= 7, 9, 8, 9&lt;br /&gt;
     leadership_x=14,14,15,15&lt;br /&gt;
     leadership_y= 7, 9, 9 ,8&lt;br /&gt;
     active_side_leader=yes&lt;br /&gt;
 [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Fast Micro AI (ai_type=fast_ai) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|0}}&lt;br /&gt;
&lt;br /&gt;
This AI is meant as a fall-back AI for scenarios where there are so many units that attacks and moves done by the default AI are slow with long delays before each move.  It replaces the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions (CAs).  It should be obvious that in order to achieve the faster speed, compromises concerning the quality of the moves have to be made.&lt;br /&gt;
&lt;br /&gt;
Enable the Fast Micro AI by using&lt;br /&gt;
 ai_type=fast_ai&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Combat candidate action:'''&lt;br /&gt;
* Attackers are considered individually one at a time.  The best attack found for a unit is executed without checking whether a better attack by another unit might be possible.&lt;br /&gt;
* The default order in which attackers are considered is by decreasing hitpoints, starting with the strongest unit (in terms of HP).  This can be modified with the optional keys below.&lt;br /&gt;
* By default, if weaker units block attack hexes for a stronger unit, they do not move out of the way (this is the same as for the default AI).  If a hex becomes available after an attack (e.g. due to a weaker unit dying in the attack), all attacks that were possible ''before'' this happened are finished first.  Only after that are attacks by the stronger unit reconsidered.&lt;br /&gt;
* If include_occupied_attack_hexes=yes is set, weaker units move out of the way for stronger units if possible.  This can, however slow down the AI depending on the situation on the map.  Use with caution.&lt;br /&gt;
* {{DevFeature1.13|2}} Previous to this version, attacks by the leader were bunched in with the attacks by the other units.  Now leader attacks are treated separately and contain an extra layer of analysis to determine whether the attack position is &amp;quot;safe&amp;quot; (in an approximate way).  Currently, this is done with two simple considerations:&lt;br /&gt;
** Count all enemy units that can reach the leader's attack position.  If there are more than given by optional parameter 'leader_attack_max_units' (default value 3), do not attack.&lt;br /&gt;
** Add up the hitpoints of all enemies that can reach the leader's attack position.  If this value, multiplied by optional parameter 'leader_weight'(default value 2), is larger than the leaders hitpoints, do not attack.&lt;br /&gt;
** Two optional parameters, 'threatened_leader_fights' and 'leader_additional_threat', can also be used to let the leader attack when the threat at the attack hex is comparable to that at the leader's current position.&lt;br /&gt;
** In order to keep this AI as fast as possible, leader attacks are done by a separate candidate action that is only evaluated when all other CAs are done.&lt;br /&gt;
&lt;br /&gt;
'''Move-to-targets, villages and retreat candidate actions:'''&lt;br /&gt;
* The move-to-targets, villages and retreat candidate actions of the default AI are removed.  Move-to-targets and villages are taken over by the Fast Micro AI, whereas retreat is simply not done.&lt;br /&gt;
* This CA moves units toward goals after attacks are done.  Goals are unowned (if the AI side has a leader only) and enemy-owned villages and enemy leaders.&lt;br /&gt;
* For each goal, units are pre-sorted in order of the fewest moves needed to cover the ''distance'' toward the goal (that is, as if move cost on each hex toward the goal were 1).  Units are then considered sequentially and if a unit can get there &amp;quot;efficiently&amp;quot;, its move is executed without considering other units.&lt;br /&gt;
** A move is considered efficient if the move cost to get to the hex is not more than 'move_cost_factor' larger than the distance to the hex.  'move_cost_factor' is an optional key that defaults to 2.  It can be set to values of 1.1 or larger (see below).&lt;br /&gt;
** If the move for the current unit is not found to be efficient, the AI considers the next unit instead.  If no unit can do an efficient move toward the goal, the best (fastest) of the inefficient moves is selected instead.&lt;br /&gt;
&lt;br /&gt;
* Village goals:&lt;br /&gt;
** Village goals are considered before enemy leader goals so that the AI preferentially chooses scouts (or otherwise fast units) for them.&lt;br /&gt;
** The number of units sent toward villages depends on the value of the default AI aspect village_value.  It is set to village_value/4 times the number of units the AI has left to move.  Thus, by default (village_value=1.0) the AI sends a quarter of its units toward villages, and the rest toward enemy leaders.  This means that the meaningful range of values for village_value is 0..4.&lt;br /&gt;
** Only one unit is moved toward each village.  This consideration also overrides the number of units to move toward villages defined by village_value, topping it at the number of unowned/enemy-owned villages on the map.&lt;br /&gt;
** Setting village_value=0 disables moving units toward villages altogether.  That is, not only are no units chosen, but the village targeting phase of the AI is entirely disabled with no evaluation time used for it.  Note that, unlike with the default AI, this also means that no villages within range of an AI unit are grabbed.&lt;br /&gt;
** The order in which villages are chosen as goals is some combination of them being close to the AI side's leader and being spread out over the map.&lt;br /&gt;
&lt;br /&gt;
* Enemy leader goals:&lt;br /&gt;
** The order of enemy leaders goals is set according to their distance from the AI leader with the closest enemy leader being targeted first (if the AI has a leader, otherwise their order is random).  One unit is sent toward each enemy leader in this order, after which the AI starts with the first enemy leader again.  This is repeated until all units have moved.&lt;br /&gt;
&lt;br /&gt;
* Guardians (units with ai_special=guardian set) are ignored by the fast move-to-targets CA.&lt;br /&gt;
&lt;br /&gt;
'''Tips for speeding up/modifying the Fast Micro AI in specific scenarios:'''&lt;br /&gt;
&lt;br /&gt;
A number of optional keys and default AI aspects can be used to modify the behavior of the Fast Micro AI.  The MAI is set up so that it is a reasonable compromise between speed and quality of the moves in most situations.  However, this might not be ideal for all scenarios here are some things that can be done to speed up the AI (or to get better behavior in some situations by doing the opposite):&lt;br /&gt;
* Use large values for the default AI aspect aggression.  The largest delays before attacks happen (in some circumstances) when the Fast MAI has to consider lots of units with attacks left but decides not to attack with them.  See below for a description of how aggression is used by the fast AI.&lt;br /&gt;
* Use large values of move_cost_factor (see above for description of what this does).  If you want to emphasize units that can move across terrain easily (e.g. bats) to be chosen for village grabbing, you might want to use low values of move_cost_factor, but this might slow down the AI.  The default value should work reasonably well in most standard situations, but modifying the value might work better on specific maps.&lt;br /&gt;
* Don't use dungeon_mode=yes unless absolutely necessary, even in most dungeon crawls.  Test the AI without this setting first and only use it if it is the only way to solve the problem.  Often, a minor tweak to the map will have the same effect, without the speed penalty.&lt;br /&gt;
* Use include_occupied_attack_hexes=yes with caution.  It might (or might not) slow down the attack CA significantly, depending on the scenario.&lt;br /&gt;
* Set village_value=0, which makes the AI not consider villages as goals.  This has not been found to make a large difference on the maps tested so far, but it might have an effect on maps with a very large number of villages.&lt;br /&gt;
* Use attack_hidden_enemies=yes on maps where there are no hidden enemies. Excluding hidden enemies from the attack calculation requires an additional filter to be evaluated that takes extra time. In most cases this will not result in a large computation time saving.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_hidden_enemies'''=no: (boolean)  By default, hidden enemies are excluded from the attack calculation.  Setting this to 'yes' makes the AI also consider those enemies as attack targets.&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.13|2}} A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  As with the default AI, units will not step on hexes defined by the [avoid] tag.  In addition, no goals will be chosen that lie on those hexes.  The one minor exception is that a unit stepping out of the way for another unit might temporarily enter the avoided zone.&lt;br /&gt;
* '''dungeon_mode'''=no: (boolean)  As much as possible, the Fast Micro AI works with distances between hexes rather than path finding.  This works pretty well on open maps and even most dungeon crawls (and similar scenarios), but it might result in some units getting temporarily (and in rare cases permanently) stuck on maps with convoluted labyrinths.  If that is the case, setting dungeon_mode=yes triggers more path finding that avoids such situations (but as a result is somewhat slower).&lt;br /&gt;
* '''include_occupied_attack_hexes'''=no: (boolean) If set to 'yes', the AI moves weaker units out of the way for a stronger attacker.  This might lead to better attacks but is slower, so don't use if speed is essential.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the attackers.  Only AI units matching this filter will attack, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes the side= key set to the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the enemies.  Only enemy units matching this filter will be attacked, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''leader_weight'''=2: (number &amp;gt;= 0) {{DevFeature1.13|2}} A factor specifying how much more valuable the AI considers side leaders than non-leader units in the attack evaluation. This is also used to determine whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''leader_additional_threat'''=1: (number &amp;gt; 0) {{DevFeature1.13|2}} If threatened_leader_fights=yes, the threat at the attack position may be larger by this factor for the leader to leave the keep for an attack.&lt;br /&gt;
* '''leader_attack_max_units'''=3: (number &amp;gt;= 0) {{DevFeature1.13|2}} A number specifying how many enemy units within reach are acceptable when determining whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''move_cost_factor'''=2: (number &amp;gt;= 1.1)  The factor by which the move cost to a goal hex may be larger than the distance to the goal hex for a move to be considered &amp;quot;efficient&amp;quot;.  See description above.  1.1 is used if this is set to values smaller than 1.1.  Smaller values might lead to better behavior, while larger values might speed up the AI.&lt;br /&gt;
* '''weak_units_first'''=no: (boolean) If set to 'yes', inverts the order in which units are considered by the combat CA.  Attacks are then done in order of increasing hitpoints of the attackers.&lt;br /&gt;
* '''skip_combat_ca'''=no: (boolean) If set to 'yes', the fast combat CA is not used.  The default AI's high_xp_attack and combat CAs remains in place in this case.&lt;br /&gt;
* '''skip_move_ca'''=no: (boolean) If set to 'yes', the fast move-to-targets CA is not used.  The default AI's retreat, villages and move-to-targets CAs remains in place in this case.&lt;br /&gt;
* '''threatened_leader_fights'''=no: (boolean) {{DevFeature1.13|2}} By default, the AI leader will not attack if the threat at the attack position is too high.  If this key is set to 'yes', the leader will attack if the threat there is not larger than the threat at his current position multiplied by the value of 'leader_additional_threat'. This is meant as an option for the endgame: when the AI leader is under attack, he might as well go down fighting.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''aggression''': The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to set how aggressive the AI should attack.  It works in roughly the same way as the default AI aspect:&lt;br /&gt;
** The value of (1 - aggression) determines how much the AI values its own units compared to the enemy's units.  Thus, aggression=0 means that attacks are only done if the AI expects to deal (roughly) the same amount of damage as it will receive.  aggression=1 means that expected damage to the AI's units is not considered, only the expected damage to enemy units matters.&lt;br /&gt;
** Damage is calculated as a combination of hitpoints and chance to die, in &amp;quot;gold equivalent units&amp;quot;.&lt;br /&gt;
** Unlike the default AI, aggression is set to 1 for values larger than 1.  Allowed values are thus anything from -infinity to +infinity, although only -infinity to +1.0 make sense.&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.13|2}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
*'''attacks aspect''':  {{DevFeature1.13|2}} The standard RCA AI [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] used to filter own and enemy units for attacks is now also taken into account as long as neither [filter] nor [filter_second] (see above under 'optional keys') are defined in the [micro_ai] tag.&lt;br /&gt;
*'''village_value=1''': (number ranging from 0 to 4) The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to define how much villages should be targeted by the move-to-targets CA.  In the Fast MAI, this defines which fraction of the AI units are moved toward village goals (see above for details).  If set to 0, villages are ignored as goals.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded to the same values as those of the respective [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|default candidate actions]].  ca_score is therefore not a valid optional key.&lt;br /&gt;
* The fast Micro AI modifies (removes) the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions.&lt;br /&gt;
&lt;br /&gt;
Enabling the Fast Micro AI in its default configuration is as simple as this usage example from the &amp;quot;Fast AI&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=fast_ai&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Generalized Goto Micro AI (ai_type=goto) ==&lt;br /&gt;
&lt;br /&gt;
The Goto Micro AI provides a highly configurable method of sending a unit (or units) to a location or set of locations.  The units to be moved are defined using a [[StandardUnitFilter|Standard Unit Filter]] (SUF) [alternatively, all side units are used if the SUF is not provided), while the goto locations are given in a [[StandardLocationFilter|Standard Location Filter]] (SLF).&lt;br /&gt;
&lt;br /&gt;
By default, each unit matching the SUF will move toward the closest goal (in terms of turns needed to get there) matching the SLF via the fastest route given the current situation on the map (locations of enemy units etc.), but this behavior can be influenced by the optional keys listed below.  Note that this means that a unit's goal might change from one turn to the next depending on the situation on the map.&lt;br /&gt;
&lt;br /&gt;
The behavior of the unit(s) once they reach their goal location(s) is configurable as well.  By default, they will stay there until the Goto Micro AI is deleted or changed.&lt;br /&gt;
&lt;br /&gt;
For demonstrations of several Goto Micro AI usages, check out the &amp;quot;Goto&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Goto Micro AI by using&lt;br /&gt;
 ai_type=goto&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Goto specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the possible goal locations for the move.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists). Note that the area defined by [avoid] is not entered at all, even if the end point of the move would lie outside the area again.&lt;br /&gt;
* '''avoid_enemies''': (number&amp;gt;0) By default, units controlled by the Goto Micro AI will not take enemy units into account for route finding, other than the enemies' ZoCs.  By contrast, if this key is set, they will try to avoid traversing hexes on which they can be attacked by enemies.  The larger the value of ''avoid_enemies'', the more the units will try to stay out of the enemies' way.  Notes:&lt;br /&gt;
** Units are willing to take a path that is 'avoid_enemies' movement points longer for each enemy unit that can be avoided by doing so for each hex along the path.  As an example, if avoid_enemies=1 and the shortest path crosses 2 hexes that can be reached by 3 enemies each, the unit is willing to take a path that is up to 6 hexes longer instead.  If avoid_enemies=10 in the same situation, the unit will take a path up to 60 hexes longer.&lt;br /&gt;
** Hexes adjacent to enemy units are always strongly avoided, independent of the value of avoid_enemies.&lt;br /&gt;
** avoid_enemies is ignored if 'use_straight_line=true' is set.&lt;br /&gt;
** Due to an issue with the path finding code, '''tunnels and teleports are ignored in Wesnoth 1.14 and before''' if 'avoid_enemies' is used. This has been fixed in Wesnoth 1.15.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units to be moved.  Defaults to all units of the side if not defined.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''ignore_enemy_at_goal'''=no and '''ignore_units'''=no: (boolean)  In the default configuration, the Goto MAI does not move units if the path to all goal hexes is blocked by enemies.  It is therefore possible to disable the Goto MAI by putting enemy units on all the goal hexes.  That can be changed if either of these two keys is set to 'yes'.&lt;br /&gt;
** 'ignore_enemy_at_goal=yes' makes the AI choose its goals while disregarding any enemy units at the goal hexes.  Even so, a stiff rating penalty is applied to goal hexes occupied by enemies, thus that unoccupied goal hexes are chosen unless all possible goals are occupied by enemies.  Note that this setting ''only'' ignores enemy units at the goal hexes themselves.  It is still possible to disable the AI by putting enemies all around the goals.&lt;br /&gt;
** 'ignore_units=yes' let's the AI ignore ''all'' enemy units when deciding which goal hex(es) to move toward.&lt;br /&gt;
** In both cases, enemies are only ignored for deciding which unit to move toward which goal hex.  For the actual move itself, all enemies and their ZoCs are considered as usual.&lt;br /&gt;
** Both options only take effect if neither avoid_enemies is used, nor use_straight_line is set to 'yes', as neither of these consider enemies as blockers. Or in other words, if avoid_enemies is used, or use_straight_line=yes, it is unnecessary to set ignore_enemy_at_goal or ignore_units to yes.&lt;br /&gt;
* '''release_unit_at_goal'''=no: (boolean) By default, a unit that reaches a goal location will stay there until the Goto Micro AI is deleted or changed (or until the conditions specified in the [goto_goals] SLF change).  If this parameter is set, a unit that has reached its goal will not be controlled by this Micro AI any more starting from the next turn.&lt;br /&gt;
* '''release_all_units_at_goal'''=no: (boolean) Same as ''release_unit_at_goal'', but this will deactivate the Goto Micro AI for all units once the first unit reaches a goal.&lt;br /&gt;
* '''remove_movement'''=yes: (boolean):  {{DevFeature1.15|11}}  By default, when the Goto MAI cannot move a unit (for example, because the path is blocked by enemies), its movement points are still removed. If this option is set to 'no', this step does not happen, so that other AI candidate actions (or even another Goto MAI with different parameters) can take over instead.&lt;br /&gt;
* '''unique_goals'''=no: (boolean)  If set to 'yes', each unit will be sent to a different goal location.  Note that the best goals are recalculated each turn, so a unit might choose a different goal this turn from what it was heading for during the previous turn if the situation on the map has changed.  Also note that if there are more units than goal locations, the left over units will not be handled by the Goto MAI but follow default behavior instead.&lt;br /&gt;
* '''use_straight_line'''=no: (boolean)  By default, units choose the route by which they will get to their goal locations in the smallest number of turns.  If this parameter is set to 'yes', a straight line route (or something pretty close to it at least) will be chosen instead, irrespective of whether this leads into a dead end, whether it is blocked by enemies, etc.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* Depending on the parameters used, the Goto Micro AI might require a lot of path finding to be done.  Thus, while it is possible to set up this AI to move many units to many different locations, a usage like that might require quite a long calculation time.&lt;br /&gt;
&lt;br /&gt;
===Goto Micro AI example usages:===&lt;br /&gt;
&lt;br /&gt;
Here are several examples of goto [micro_ai] tag usages from the &amp;quot;Goto&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
* Send a unit toward a castle (similar to the RCA AI's Goto Candidate Action, but avoiding dead ends and areas ZoCed by enemies)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
             ca_id=messenger1&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=messenger1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=6-7,2-4&lt;br /&gt;
                 terrain=C*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             ca_score=210001&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send all units of the side toward the southern border of the map&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=8&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                  y=33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             avoid_enemies=1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send units to the four corners of the map (with each unit going to a different corner).&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x=1,1,44,44&lt;br /&gt;
                 y=1,33,1,33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             unique_goals=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Set up a guardian unit that doesn't move while no enemy unit is within 8 hexes (ironically, the Goto Micro AI can be used to keep units in place, although using one of the Guardian Micro AIs might often be a better idea)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=guard1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 [filter]&lt;br /&gt;
                     id=guard1&lt;br /&gt;
                 [/filter]&lt;br /&gt;
                 [not]&lt;br /&gt;
                     [filter]&lt;br /&gt;
                         side=1&lt;br /&gt;
                     [/filter]&lt;br /&gt;
                     radius=8&lt;br /&gt;
                 [/not]&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Guardian Micro AIs ==&lt;br /&gt;
=== General Comments on the Guardian Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Guardian Micro AIs can be used to set up different kind of guardian behaviors for a unit or a set of units.  Four different types of guardians are available and are described below.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different guardian behaviors, check out the &amp;quot;Guardians&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
=== Return Guardian  (ai_type=return_guardian)===&lt;br /&gt;
&lt;br /&gt;
A 'return guardian' is a variation of the standard Wesnoth guardian. It has an assigned guard position (GP) to which it returns after attacks on approaching enemies:&lt;br /&gt;
* If at GP with no enemy in reach, do nothing.&lt;br /&gt;
* If at GP with an enemy in reach, attack. Note that the attack is done by the default AI, which might result in the guardian not attacking if the enemy is deemed too strong (this can be influenced by setting the default ''aggression'' value to a different value).&lt;br /&gt;
* If not at GP, return there, no matter whether an enemy is in reach or not.&lt;br /&gt;
* If enemies are blocking the way back to GP, do your best to move around them.&lt;br /&gt;
* If the guardian ends up next to an enemy on the way back, attack that enemy after the move.&lt;br /&gt;
&lt;br /&gt;
Enable the Return Guardian Micro AI by using&lt;br /&gt;
 ai_type=return_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''return_x,return_y''': The coordinate of the hex the unit returns to after each attack.  {{DevFeature1.15|0}} '''return_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=100010: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  This AI consist of 2 CAs, one with score &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt; the other with &amp;lt;tt&amp;gt;ca_score-20&amp;lt;/tt&amp;gt;.  The default value is set so that the first CA gets executed right before the default combat CA, and the other right after.  It usually will not make sense to change this value, but it is possible to do so for custom applications.  {{DevFeature1.13|6}}: This is now changed to 100100 and 99900 in order to avoid conflicts with the new high_XP_attacks candidate action.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a return guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=return_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=return1&lt;br /&gt;
             return_x,return_y=20,2&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
===Stationed Guardian (ai_type=stationed_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A 'stationed guardian' is another variation of the standard Wesnoth guardian with a somewhat more complex behavior than that of the 'return guardian'. Two positions are defined for it, a 'station' and a 'guarded location', as well as a distance. The behavior is as follows:&lt;br /&gt;
* If no enemy is within 'distance' of the guard's current position, do nothing.  This is independent of the guardian being at its station or not.&lt;br /&gt;
* Otherwise: If an enemy is within 'distance' of the guard, but not also within the same distance of the guarded location and the station (all of this simultaneously), move the guard in the direction of the station (or keep the guard in place if the station is unreachable).&lt;br /&gt;
* Otherwise:&lt;br /&gt;
** Pick the enemy unit that is closest to the guarded location.&lt;br /&gt;
** If we can reach it, pick the adjacent hex with the highest defense rating and attack from there.&lt;br /&gt;
** If not in reach, move toward this unit.&lt;br /&gt;
&lt;br /&gt;
Enable the Stationed Guardian Micro AI by using&lt;br /&gt;
 ai_type=stationed_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance parameter as explained above&lt;br /&gt;
* '''station_x,station_y''': The x and y position of the hex that serves as the guardians station. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''guard_x, guard_y''': The x and y position of the guarded location.  {{DevFeature1.13|0}}  These are now optional keys, defaulting to station_x,station_y if not provided. {{DevFeature1.15|0}} '''guard_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a stationed guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=stationed_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=stationed1&lt;br /&gt;
             distance=4&lt;br /&gt;
             station_x,station_y=2,14&lt;br /&gt;
             guard_x,guard_y=3,13&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Coward (ai_type=coward) ===&lt;br /&gt;
&lt;br /&gt;
Cowards are units that, like guardians, sit around doing nothing until an enemy comes into range.  Unlike guardians, however, they run away once enemies approach. Applications might be wild animals, unarmed civilians getting in the way of a battle, etc.  The coward macro can be called with two optional locations, 'seek' and 'avoid':&lt;br /&gt;
* If neither is given, the coward retreats to the position farthest away from the approaching enemies.&lt;br /&gt;
* If 'seek' is given, it preferentially goes toward that location (but getting away from enemies takes priority).&lt;br /&gt;
* If 'avoid' is given, it in addition tries to avoid that location (with both maximizing distance from enemies and going toward 'seek' taking priority).&lt;br /&gt;
* Both 'seek' and 'avoid' may consist of only one coordinate ('x' or 'y'), in which case not a single hex, but a line of hexes is sought or avoided.&lt;br /&gt;
&lt;br /&gt;
Enable the Coward Micro AI by using&lt;br /&gt;
 ai_type=coward&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance within which an enemy must be to &amp;quot;scare&amp;quot; the unit&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_if_trapped'''='no':  {{DevFeature1.13|0}}  With the default settings, a coward never attacks.  If this key is set to 'yes', the coward attacks the weakest (lowest hitpoints) enemy next to which it ends up after moving (such as a cornered animal would do, blindly lashing out without regards for its own safety).&lt;br /&gt;
* '''avoid_x, avoid_y''': The x and y coordinate of the hex to avoid. {{DevFeature1.15|0}} '''avoid_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units from which the cowards flee.  If not set, this defaults to all enemy units.  Note, however, that no &amp;quot;enemy of AI side&amp;quot; condition is added if [filter_second] is specified, meaning that it can include allied and/or enemy units as desired (and even units on the same side as the coward).&lt;br /&gt;
* '''seek_x, seek_y''': The x and y coordinate of the hex to seek. {{DevFeature1.15|0}} '''seek_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a coward [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=coward&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=coward3&lt;br /&gt;
             distance=5&lt;br /&gt;
             seek_x,seek_y=24,5&lt;br /&gt;
             avoid_x,avoid_y=24,15&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Zone guardian (ai_type=zone_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A zone guardian is a unit that, as the name says, guards a zone. It moves randomly inside this zone until an enemy enters it (or a separately defined enemy zone, see below). Applications might be the defense of a castle, a nesting area or for protecting an unit.  The zone macro can be called with an optional SLF, [filter_location_enemy]:&lt;br /&gt;
* If not specified, the zone guard attacks any enemy coming inside its guard zone.&lt;br /&gt;
* If [filter_location_enemy] is given, it attacks any enemy entering this zone and once there are no more enemies, it goes back to patrol in its basic zone.&lt;br /&gt;
&lt;br /&gt;
Enable the Zone Guardian Micro AI by using&lt;br /&gt;
 ai_type=zone_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone to guard.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_enemy]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone where enemies have to be attacked.  If not given, it defaults to [filter_location].&lt;br /&gt;
* '''station_x,station_y''': By default, the zone guardian moves randomly within the zone if there is no enemy to attack.  However, if station_x/y is given, it moves to this position instead during turns when it does not have an enemy to attack.  This position does not need to be inside the zone defined above.  Note: obviously it does not make sense to define [filter_location], [filter_location_enemy] and station_x/y all at the same time.  Only [filter_location] and either one of the latter two (or none of them) should be provided for any zone guardian. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a zone guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=zone_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=zone3&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=22-31,4-11  # This is intentionally chosen to extend past the lake&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             station_x,station_y=32,8&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Hang Out Micro AI (ai_type=hang_out) ==&lt;br /&gt;
&lt;br /&gt;
The Hang Out Micro AI instructs units of the AI side to &amp;quot;hang out&amp;quot; around a certain location, or locations, until a condition for mobilizing the side is met.  If none of the optional keys are given, the AI controls all units of the side to hang around the leader while moving off castle tiles to make room for recruiting.&lt;br /&gt;
&lt;br /&gt;
The AI works by moving each unit to its best hex first using the minimum amount of movement points required for this move.  Once all AI units are in position, movement points are set to zero for all of them simultaneously such that they cannot be used by other AI candidate actions.  This is done because the mobilizing condition might be met as result of one of the moves of the Hang Out AI, after which we want units to be able to continue with other AI actions.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Hang Out and Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Hang Out Micro AI by using&lt;br /&gt;
 ai_type=hang_out&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Hang Out specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  In its default configuration, the Hang Out MAI controls all units of a side, keeping them as close to the side leader(s) as possible while moving off castle tiles to make room for recruiting.  For this purpose, the candidate action score defaults to 170,000, which is just below the score of the default recruiting CA.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''': A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  If neither of these two exists, this parameter is set to default to all castle terrain (&amp;lt;tt&amp;gt;terrain=C*,C*^*,*^C*&amp;lt;/tt&amp;gt;) so that units move off the castle to make room for recruiting.  Note that this does not include keep terrain, otherwise the leader would be moved off the keep as well.&lt;br /&gt;
* '''ca_score'''=170000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 170,000, which means that it is executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Hang Out action (&amp;lt;tt&amp;gt;side=&amp;lt;/tt&amp;gt; is always set and does not need to be provided).  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the locations ''near'' which the AI units should hang out.  This does not mean that the units only move onto terrain specified here, but that they minimize the distance to such terrain.  It defaults to the position of the side leader(s).&lt;br /&gt;
* '''[mobilize_condition]''':  Contains ([[ConditionalActionsWML#Condition_Tags|WML Condition Tags]])  Once the condition set up here has been met, the Hang Out MAI is disabled and the side goes over to other AI activity.  The Hang Out MAI remains disabled after this, even if the condition is not met any more later in the scenario.&lt;br /&gt;
* '''mobilize_on_gold_less_than''': (integer) Since side gold cannot be checked in a WML condition tag without storing it in a variable first, this key is provided separately.  It disables the Hang Out Micro AI once the side gold goes below the value specified here for the first time.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
* The Hang Out AI still takes effect if no locations to hang out at are found or if all map locations are to be avoided.  In that case, it simply keeps all units at their current locations.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Hang Out Micro AI configuration from the &amp;quot;Hang Out and Messenger&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hang_out&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [avoid]&lt;br /&gt;
                 terrain=C*,H*,M*,A*,S*,*^F*&lt;br /&gt;
             [/avoid]&lt;br /&gt;
             [mobilize_condition]&lt;br /&gt;
                 [have_unit]&lt;br /&gt;
                     side=1&lt;br /&gt;
                     count=7-99&lt;br /&gt;
                 [/have_unit]&lt;br /&gt;
             [/mobilize_condition]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Healer Support Micro AI (ai_type=healer_support) ==&lt;br /&gt;
&lt;br /&gt;
The Healer Support Micro AI configures the healers of a side to stay behind the battle lines and heal injured and/or threatened units rather than participate in combat under all circumstances.  You can set different levels of aggressiveness for the healers.  &lt;br /&gt;
&lt;br /&gt;
'''Notes'''&lt;br /&gt;
* By default, ''all'' healers of the AI side are controlled by this AI.  This includes in particular also the side leader, if the leader is a healer.  If you do not want the leader to be included (and possibly be moved away from the keep etc.) use the [filter] tag with canrecruit=no (see below).&lt;br /&gt;
* It is not always better to use this Micro AI than the default AI, as it takes healers (which can be strong units) away from the pool of units to choose for attacks.&lt;br /&gt;
* {{DevFeature1.15|3}} This Micro AI now takes unit guardian status and the passive_leader aspect into account.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Healers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Healer Support Micro AI by using&lt;br /&gt;
 ai_type=healer_support&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Healer Support specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the healer units.  If set, only units on the AI side passing this filter ''and'' having the healing ability are considered by this Micro AI.&lt;br /&gt;
*'''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting units to receive healing. If set, only units on the AI side passing this filter ''and'' the other healee selection criteria will be selected for healing by this Micro AI.&lt;br /&gt;
* '''aggression'''=1.0: (float) Sets the aggressiveness of the AI.  This parameter is set up as a float to accommodate future functionality, but it currently acts as a boolean: if set to zero, the AI will never let its healers participate in combat, for all other values it allows them to attack after all other units have attacked and if the healers cannot find any more units to support.  The former behavior might be appropriate in scenarios with only one or few valuable healers, while the latter might work better in scenarios with many healers.&lt;br /&gt;
* '''injured_units_only'''=no: (boolean) If set to 'yes', the AI will only move healers next to units that are already injured and skip units that currently have full hitpoints, but might get injured during the next enemy turn.&lt;br /&gt;
* '''max_threats'''=9999: (integer) The maximum allowable number of enemies that can attack a hex in order for it to be considered for a healer.  As an example, setting 'max_threats=0' means that the AI only moves healers to locations that are entirely safe from the enemy (assuming that none of the units currently on the map dies).  Note that the value of this key is checked against the number of enemies that can make it to the hex, not the number of adjacent hexes from which the healer could be attacked.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI. Note: as attacks by the healers are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The Healer Support Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded.  It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Healer Support Micro AI configuration from the &amp;quot;Healers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             aggression=0&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Lurkers Micro AI (ai_type=lurkers) ==&lt;br /&gt;
&lt;br /&gt;
The Lurker Micro AI controls some or all units of a side in &amp;quot;lurker mode&amp;quot;.  A lurker is a unit that is capable of moving across most terrains, but that only stops on and attacks from specific terrain.  It might also have the ability to hide on this terrain (which is the reason why it is called a lurker).&lt;br /&gt;
&lt;br /&gt;
Lurkers move individually without strategy and always attack the weakest enemy within their reach.  If no enemy is in reach, the lurker does a random move instead - or it just sits and waits (lurks).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of different types of lurker behavior, check out the &amp;quot;Lurkers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Lurkers Micro AI by using&lt;br /&gt;
 ai_type=lurkers&lt;br /&gt;
in the [micro_ai] tag&lt;br /&gt;
&lt;br /&gt;
===Lurkers specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
* '''[filter]''': Specifies which units of the side are controlled by the Lurker Micro AI.  This is a [[StandardUnitFilter|Standard Unit Filter]].  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': Specifies the terrain from which the lurkers attack. This is a [[StandardLocationFilter|Standard Location Filter]].&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_wander]''': Specifies the terrain on which lurkers end their moves if there is no enemy in reach. This is a [[StandardLocationFilter|Standard Location Filter]].  If [filter_location_wander] is not given, it defaults to [filter_location].&lt;br /&gt;
* '''stationary'''=no: (boolean)  If set to yes, a lurker never moves if there is no enemy within attack range.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Lurkers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=4&lt;br /&gt;
             ai_type=lurkers&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Naga Fighter&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=W*,S*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             [filter_location_wander]&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location_wander]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Messenger Escort Micro AI (ai_type=messenger_escort) ==&lt;br /&gt;
&lt;br /&gt;
The Messenger Escort Micro AI lets you define a set of waypoints through which it tries to move one or several of its units, the messenger(s).  The other units escort the messenger, trying to protect it and attacking enemies that are in its way.  Note that a messenger might not strictly hit intermediate waypoints, but that getting close is good enough, especially if a waypoint is occupied by an enemy unit.  It does, however, always try to get to the last waypoint.&lt;br /&gt;
&lt;br /&gt;
'''Notes on the use of several messengers:''' {{DevFeature1.13|0}}  &lt;br /&gt;
* This Micro AI can control several messengers at the same time, which can but do not have to move in a group.  They can be put on the map altogether in one location, or be spread out on the map or across different turns.&lt;br /&gt;
* The farther a messenger has moved through the chain of waypoints, the higher a priority it is for the AI, meaning that farther advanced (on the map) messengers are moved first, and that the escort units try to clear the way and stay with those messengers more so than with those further back on the map.  This order can be inverted using the invert_order= key.&lt;br /&gt;
* If several messengers are on the map, escort units might move from one to the other, the AI making a judgement call where they are needed most.  Closeness to a messenger is generally the most important criterion, but not the only one.&lt;br /&gt;
* If a messenger dies, the escort units previously staying with it now join one of the other messengers if there are any left (otherwise they follow default AI behavior).&lt;br /&gt;
* If you want certain escort units to stay always with the same messenger, use several messenger [micro_ai] tags with different [filter]/[filter_second] combinations.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Messenger Escort Micro AI by using&lt;br /&gt;
 ai_type=messenger_escort&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Messenger Escort specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': {{DevFeature1.13|0}} (Previous to 1.13.0, only id= was allowed.) The id of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated list of waypoint coordinates through which the messenger will go in the given order to reach its goal, given by the last coordinate.  If you want the messenger to go directly to the goal, simply enter a single x,y coordinate here.  Note that the messenger does not have to hit each waypoint exactly (except for the final one), getting close to them is good enough. {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|11}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Messenger Escort AI consists of 3 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-2&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_death_chance'''=0.67 and '''messenger_death_chance'''=0.0: (both floats; ranging from 0 to 1)    If the messenger is adjacent to an enemy at the end of the move, it will only attack the enemy if the chance of death of the enemy is &amp;gt;= enemy_death_chance and if the messenger's chance of death is &amp;lt;= messenger_death_chance (or if it is an attack at a range for which the enemy does not have a weapon).&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|0}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the escort units.  If not set, this defaults to all enemy units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''invert_order'''=no: {{DevFeature1.13|0}} If set to 'yes', the order of priority with which messenger units are considered is inverted, starting with the messenger unit that is the least advanced on the map (see description above).  In most cases, selecting this option results in worse performance of the AI, so use it with caution.  (And obviously it has no effect at all if there is only one messenger controlled by the AI.)&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|11}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Messenger Escort&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=messenger_escort&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=messenger&lt;br /&gt;
             waypoint_x=31,24,27,28&lt;br /&gt;
             waypoint_y=20,14,7,1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Patrol Micro AI (ai_type=patrol) ==&lt;br /&gt;
&lt;br /&gt;
The defines a number of waypoints for a unit (or units) to follow.  Options can be set whether the route should be followed in a loop, as an out-and back, only once, whether units encountered along the way should be attacked, etc.&lt;br /&gt;
&lt;br /&gt;
With the default settings, getting to the next waypoint takes priority over attacking for a patroller, but this can be overridden (as of '''Wesnoth 1.15.12''', using the '''attack_range''' option).  Unless instructed otherwise, the AI thus prefers to move the patrol unit around enemies rather than straight for them.  Also, if a waypoint is occupied by a unit the AI is not instructed to attack, it will (eventually) abandon that waypoint once it gets close enough and move on to the next one.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different patrol behaviors, check out the &amp;quot;Patrols&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Patrol Micro AI it by using&lt;br /&gt;
 ai_type=patrol&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Patrol specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated lists of the waypoint coordinates through which the patrol travels (in order). {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack''': By default (if this key is not set), the patrol unit attacks any unit it ends up next to after a move, or that is within '''attack_range''' (see below).  Alternatively, this parameter can be set to a comma-separated list of unit ids. If that is done, the patrol only attacks these units when it ends up next to them or if it gets within attack_range.  It ignores any other unit.  This can in turn be used to have the patroller not attack at all, by setting ''attack='' to a non-existing id.  The exception to this is an enemy on the last waypoint with ''one_time_only=yes'', which will always be attacked.&lt;br /&gt;
* '''attack_range'''=1: {{DevFeature1.15|12}} If the patrol comes within this many hexes of an enemy unit, it interrupts its patrol route and attacks that enemy (assuming it still has enough movement left to get to the enemy).  The '''attack=''' instructions are taken into account for this.  Note that the patrol generally tries to move around enemies, which means that for the default value of 1, enemies are usually not within attack range, except at the end of the move.&lt;br /&gt;
* '''attack_invisible_enemies'''=no: {{DevFeature1.15|12}} If set to 'yes', the unit also attacks invisible enemies (according to the criteria set by '''attack''' and '''attack_range''').&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''one_time_only'''=no: (boolean)  If set to 'yes', the unit goes through the patrol route only once and then hangs out at the last waypoint.  It also always attacks any enemy unit on the last waypoint, independent of what ''attack='' is set to.&lt;br /&gt;
* '''out_and_back'''=no: (boolean)  By default, a patrol unit returns to the first waypoint after reaching the last, doing the patrol route as a loop.  If this parameter is set to 'yes', the unit instead reverses its course and perpetually does out and backs of the route.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Patrols&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=patrol&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Konrad&lt;br /&gt;
             waypoint_x=9,24,25&lt;br /&gt;
             waypoint_y=21,23,15&lt;br /&gt;
             one_time_only=yes&lt;br /&gt;
             attack=Gertburt&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Protect Unit Micro AI (ai_type=protect_unit) ==&lt;br /&gt;
&lt;br /&gt;
The Protect Unit Micro AI tries to move one or several units to a given location while keeping them safe from enemy attacks. It does so by keeping the protected units away from enemy units and close to friendly units. Other units on the AI side are controlled by the default Wesnoth AI.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Protect Unit&amp;quot; and &amp;quot;The Elves Besieged&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Protect Unit Micro AI by using&lt;br /&gt;
 ai_type=protect_unit&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Protect Unit specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[unit]''': A separate [unit] tag is required for each protected unit(s).  Each tag has required keys '''id''', '''goal_x''', '''goal_y''' for each unit, as shown in the example below. {{DevFeature1.15|0}} '''goal_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''disable_move_leader_to_keep'''=no: (boolean) If set to 'yes', side leaders protected by this AI do not try to return to their keep.  This is necessary for this Micro AI to work with units that are side leaders and that are supposed to move to an off-keep location.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
*The Protect Unit Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].  Depending on the parameters, it might also modify the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|move leader to keep candidate action]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded. It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;HttT: The Elves Besieged&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=protect_unit&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Delfador&lt;br /&gt;
                 goal_x,goal_y=1,2&lt;br /&gt;
             [/unit]&lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
                 goal_x,goal_y=1,1&lt;br /&gt;
             [/unit]&lt;br /&gt;
 &lt;br /&gt;
             disable_move_leader_to_keep=true&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Recruiting Micro AI==&lt;br /&gt;
=== General Comments on the Recruiting Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Recruiting Micro AI replaces the default recruitment AI by alternative recruiting patterns.  Currently there are two recruiting algorithms you can choose from, random recruiting and the rush recruiting used in the new (as of Version 1.11.1) Experimental AI (see below).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of a different recruiting pattern, check out the &amp;quot;Recruiting&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  You can also explore the unit choices by watching the Experimental AI in a multiplayer game.&lt;br /&gt;
&lt;br /&gt;
'''Note:''' Obviously, the Recruitment Micro AI modifies the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|recruitment candidate action]].&lt;br /&gt;
&lt;br /&gt;
=== Random Recruitment AI (ai_type=recruit_random) ===&lt;br /&gt;
&lt;br /&gt;
This AI randomly selects a unit type from the recruitment list of the side.  The probability of choosing each type and the behavior in low-gold situations can be influenced with optional parameters.&lt;br /&gt;
&lt;br /&gt;
Enable the Random Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_random&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* '''[probability]''': This tag sets the probability of selecting the given unit type.  It has two required sub-keys:&lt;br /&gt;
** '''type''': comma-separated list of unit types to which this probability is to be applied&lt;br /&gt;
** '''probability''': (non-negative float)  The probability with which these unit types is to be selected.  All unit types for which no [probability] tag is defined receive a default probability of 1.0, thus values should be chosen relative to that.  As an example, in the code below swordsmen and peasants are given a probability of 8, while no other units have assigned probabilities.  Thus, each of these two unit types is 8 times more likely to be selected than any other individual unit type.&lt;br /&gt;
* '''skip_low_gold_recruiting'''=no: (boolean)  By default, the random recruitment AI chooses a unit to recruit only from those types for which it has sufficient gold.  If this parameter is set to 'yes', it will instead choose from all available unit types and end recruiting for the turn if there is not enough gold to recruit it this turn (even if other, affordable unit types exist).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a random recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_random&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [probability]&lt;br /&gt;
                 type=Swordsman,Peasant&lt;br /&gt;
                 probability=8&lt;br /&gt;
             [/probability]&lt;br /&gt;
 &lt;br /&gt;
             skip_low_gold_recruiting=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Rush Recruitment AI (ai_type=recruit_rushers) ===&lt;br /&gt;
&lt;br /&gt;
This AI replaces the default recruiting with the recruiting used in the Experimental AI [[Experimental_AI#Recruitment|as described here]].&lt;br /&gt;
&lt;br /&gt;
Enable the Rush Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_rushers&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* {{DevFeature1.15|2}} '''high_level_fraction'''=0: (non-negative number) The approximate fraction of units of level 2 or higher to be recruited. This is defined as fraction of units on the map, not of new units being recruited during a turn or over several turns (which makes a difference if there are already units on the map). The effect is also cumulative per level (starting from level 2), meaning that 1/3 of L2 units will be recruited in the example above, 1/3*1/3=1/9 L3s etc. The default value is zero, which leaves the high-level unit recruiting to the default Experimental AI algorithm resulting usually in very few such units being recruited.&lt;br /&gt;
* '''randomness'''=0.1: (number)  A random number is applied to the rusher recruit engine's score to prevent the recruitment pattern from being too predictable. 0 causes no randomness to be applied, while larger numbers increase the random effect. A value of 1-2 generates results in which the random effect is approximately equal to the scored effect. Extremely high values are essentially entirely random.&lt;br /&gt;
* {{DevFeature1.17|6}} '''reset_cache_each_turn'''=no: (bool) Many of the operations of the rush recruitment evaluation are expensive and are therefore cached. For the most part, this cache can be kept from turn to turn, but in case there are events that change, for example, the map or the recruit list, this parameter can be set to 'yes' to force a reset of the cache each turn.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a rush recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_rushers&lt;br /&gt;
             action=add&lt;br /&gt;
          [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Simple Attack Micro AI (ai_type=simple_attack) ==&lt;br /&gt;
&lt;br /&gt;
The Simple Attack Micro AI is meant as an addition to the default combat candidate action for when one wants to force the AI to do attacks that it would otherwise not do (for example on units that are 1 XP from leveling) or to execute certain attacks before all others (for example, attack with all &amp;quot;expendable&amp;quot; units first).  It is not set up to be a replacement for the default attacks.  Some notes:&lt;br /&gt;
&lt;br /&gt;
* The algorithm is different from and simpler than that of the default combat CA.  For example, no attack combinations are considered, only individual attacks.  As a result, the [[AiWML#List_of_AI_Aspects|default aspects]] (aggression, caution, [avoid], ...) are not taken into account.  Also, if lots of units are involved, evaluation of this Micro AI is potentially quite a bit slower than of the default attacks.&lt;br /&gt;
&lt;br /&gt;
* If an attack matching the Standard Unit Filters described below is found, this attack will always be executed, no matter how bad the odds are.  Thus, this Micro AI can be used to force the AI to do attacks it would otherwise avoid.&lt;br /&gt;
&lt;br /&gt;
* Other than selecting the attacker/defender units, this AI is currently not configurable.  Let us know if you would like to see certain options to be added.  (No guarantees that we will add them, but we will definitely consider them.)&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Simple Attack&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=simple_attack&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Simple Attack specific keys for the [micro_ai] tag===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  It is, however, in general not desirable to use this AI without at least one of the optional Standard Unit Filters below, as this AI is not meant as a replacement of the default combat candidate action.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=110000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 110,000, which means that it is executed right before the default combat candidate action.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Simple Attack action.  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units the AI should attack.  It defaults to all enemy units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
* '''weapon'''=-1: (integer) The number of the weapon that should be used for this attack.  Weapon numbers start at 1 (not 0).  -1 stands for &amp;quot;best weapon&amp;quot; (by whatever criterion the AI chooses to use).   '''Available from Wesnoth 1.11.8'''&lt;br /&gt;
&lt;br /&gt;
Below are 2 examples of Simple Attack Micro AI configurations from the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  The first one shows how to force the AI to attack with all Soulless and Walking Corpse units first:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110000&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless,Walking Corpse&lt;br /&gt;
             [/filter]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
The second example executes attacks by Soulless on all enemy units that are 1 XP from leveling.  Note that the Lua code in the preload event is there simply in order to code the &amp;quot;1 XP from advancing&amp;quot; condition.  It is not needed for most other SUFs, as can be seen in the previous example.&lt;br /&gt;
     [event]&lt;br /&gt;
         name=preload&lt;br /&gt;
         first_time_only=no&lt;br /&gt;
         [lua]&lt;br /&gt;
             code=&amp;lt;&amp;lt;&lt;br /&gt;
                 function close_to_advancing(unit)&lt;br /&gt;
                     if (unit.experience &amp;gt;= unit.max_experience-1) then&lt;br /&gt;
                         return true&lt;br /&gt;
                     else&lt;br /&gt;
                         return false&lt;br /&gt;
                     end&lt;br /&gt;
                 end&lt;br /&gt;
             &amp;gt;&amp;gt;&lt;br /&gt;
         [/lua]&lt;br /&gt;
     [/event]&lt;br /&gt;
 &lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110001&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless  # No Walking Corpses; L0 units don't advance enemy&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 lua_function = &amp;quot;close_to_advancing&amp;quot;&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
Several Simple Attack Micro AIs can be combined for the same AI side.  The two examples above are both used (note the different values for ca_score) in the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
== Custom Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|5}}&lt;br /&gt;
&lt;br /&gt;
From 1.13.5 onwards, adding a new Micro AI can be easily done without altering any core files or overriding the micro_ai tag. The [micro_ai] tag now looks up its ''ai_type'' parameter in the &amp;lt;code&amp;gt;wesnoth.micro_ais&amp;lt;/code&amp;gt; table and uses the micro AI setup function found there to determine which keys are required and allowed and what candidate actions to add.&lt;br /&gt;
&lt;br /&gt;
A micro AI setup function takes as its single parameter the contents of the [micro_ai] tag and returns three separate values:&lt;br /&gt;
*'''required''': A list of required keys and tags, as strings. Tags are indicated by enclosing them within square brackets in the string. If a key or tag in this list is missing from the [micro_ai] tag, an error will be printed.&lt;br /&gt;
*'''optional''': Similar to above, a list of optional keys and tags, as strings. No error will be printed if a key or tag in this list is missing, nor will an error be printed if it is present (as would be the case for unrecognized keys or tags).&lt;br /&gt;
*'''ca_params''': A table specifying the candidate actions that constitute the micro AI. It should contain an ''ai_id'' key (which specifies a base ID to be used for candidate actions of this micro AI) and a series of candidate action definitions. Each candidate action definition is a table containing the following keys:&lt;br /&gt;
**'''ca_id''': The ID used for the candidate action. This will be combined with the ''ai_id'' to produce the final ID.&lt;br /&gt;
**'''location''': Path to the Lua file that defines the candidate action.&lt;br /&gt;
**'''score''': The maximum score for the candidate action. If the [micro_ai] tag specified a ''ca_score'' parameter, the score here should normally be based on that.&lt;br /&gt;
&lt;br /&gt;
(The return statement would thus look like &amp;lt;syntaxhighlight lang='lua' inline&amp;gt;return required, optional, ca_params&amp;lt;/syntaxhighlight&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
'''Examples''' of micro AI setup functions can be found in the [https://github.com/wesnoth/wesnoth/tree/1.14/data/ai/micro_ais/mai-defs default MAI definitions directory].  See the [https://github.com/wesnoth/wesnoth/blob/1.14/data/ai/micro_ais/mai-defs/animals.lua#L4-L12 Big Animals MAI definition] for a simple example.  For custom micro AIs, the setup code should be put into a ''[lua]'' tag inside a preload event.&lt;br /&gt;
&lt;br /&gt;
Since the function has full access to the contents of the [micro_ai] tag, it can conditionally add candidate actions to '''ca_params''' depending on the values of other keys within the [micro_ai] tag. It can also perform other actions such as adding facets to an aspect; if it does such things, it should pay attention to the ''action'' key to determine whether it's being added or removed.&lt;br /&gt;
&lt;br /&gt;
There are two useful helper functions that can be used by Micro AI setup functions requiring aspect changes. They can be brought into scope by a line similar to the following:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='lua&amp;gt;local MAIH = wesnoth.require(&amp;quot;ai/micro_ais/micro_ai_helper.lua&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* MAIH.add_aspects(side, aspect_params)&lt;br /&gt;
* MAIH.delete_aspects(side, aspect_params)&lt;br /&gt;
&lt;br /&gt;
The ''aspect_params'' parameter is an array of tables, each of which contains the following keys:&lt;br /&gt;
*'''aspect''': The aspect to change.&lt;br /&gt;
*'''facet''': The WML defining the facet. This must include an ''id'' key in order for the aspect to be correctly deleted. For maximum compatibility, incorporate the ''ca_id'' key from the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=69670</id>
		<title>Micro AIs</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=69670"/>
		<updated>2022-06-24T21:53:10Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Hang Out specific keys for the [micro_ai] tag: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== A Few General Words about Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
* Micro AIs allow Wesnoth campaign and scenario authors to add new AI functionality without the need for any AI programming. They bring new specialized behavior to Wesnoth that cannot be achieved, at least not easily, by adjusting default AI parameters (such as those described at [[AiWML]]) or with WML code.&lt;br /&gt;
&lt;br /&gt;
* A Micro AI is activated and configured (or deleted) via the [micro_ai] tag, requiring only a few lines of WML code.&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.6''', the Micro AI behavior was inconsistent when it came to '''hidden enemies and petrified units'''.  Some Micro AIs ignored those, while others did not.  In some case there was even a potential for AI errors when an AI unit ran into hidden or petrified enemies.  As of 1.13.6, this has all been fixed.  The Micro AIs now correctly and consistently ignore hidden units and treat petrified units as blocking the hex they are on but having no effect otherwise, in the same way as the default AI does.  '''Important notes:'''&lt;br /&gt;
** It is not possible to re-include hidden/petrified units via the Standard Unit Filters (SUFs) used by some of the Micro AIs. In fact, if this is attempted, the filters will generally not match any unit.&lt;br /&gt;
*** This only applies to SUFs which go directly into the [micro_ai] tag. Any SUF that is a sub-tag of another filter (for example, of a Standard Location Filter) is taken literally and returns all units matching the filter. If hidden or petrified units are to be excluded from such a sub-filter, that needs to be specified directly in that filter.&lt;br /&gt;
** We also note that, because all the Micro AIs are written in Lua, it is possible to [[Micro_AIs#Using_Development_.281.13.29_Version_Micro_AIs_in_your_Stable_.281.12.29_Version_Add-on|move a Micro AI's code into one's add-on]] and modify it to whatever scenario-specific behavior is desired (this does not only apply to hidden/petrified units, but to all behavior). Setting up general Micro AI behavior that works in every possible setting is not feasible, but adapting it to work (only) in the specific setting of a given scenario is often quite simple. If you do not know how to do this, please post a message on the Wesnoth forums and we will be happy to help you with it.&lt;br /&gt;
&lt;br /&gt;
* The '''[[AiWML#List_of_AI_Aspects|default AI aspects]] are, for the most part, not taken into account by the Micro AIs'''.  For example, unless noted otherwise, specifying a value for ''aggression'' does not change the MAI behavior, and locations specified in an ''[avoid]'' tag generally can be entered by units controlled by an MAI.  This is a feature of the Micro AIs, not a bug.  If you feel that a certain default AI aspect should also apply to a Micro AI, please let us know in the [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 Micro AIs Feedback thread].&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.7''', the Micro AIs did not all work reliably when the '''AI side is under shroud''' and it may have been necessary to turn shroud off for the side when using a Micro AI. {{DevFeature1.13|7}} The Micro AIs now all work for sides under shroud. They do this by disregarding shroud for terrain purposes when path finding, consistent with default Wesnoth AI behavior, while still ignoring hidden units (including those hidden under shroud) correctly.&lt;br /&gt;
&lt;br /&gt;
* Most Micro AIs in their default setting apply to all units of a side, although many of them provide parameters for filtering the units that should be affected by the new behavior.  If the Micro AI is applied only to part of the units of a side, the other units follow the default Wesnoth AI behavior.&lt;br /&gt;
&lt;br /&gt;
* Micro AIs are available in mainline since Wesnoth 1.11/1.12.&lt;br /&gt;
&lt;br /&gt;
== Micro AI Test and Demo Scenarios ==&lt;br /&gt;
&lt;br /&gt;
All Micro AIs described here can be checked out in a number of test and demo scenarios.  The easiest way to access the test scenarios is by assigning a hotkey to &amp;quot;Start Test Scenario&amp;quot; in the preferences (as of Wesnoth 1.13.8). Then, simply press the hotkey in the titlescreen and select the desired scenario from the list (e.g. 'micro_ai_test' for the overall &amp;quot;switchboard scenario&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Test scenarios can also be accessed from the command line by typing&lt;br /&gt;
 path/wesnoth-executable -t micro_ai_test&lt;br /&gt;
or (depending on your system and Wesnoth version)&lt;br /&gt;
 path/wesnoth-executable -tmicro_ai_test&lt;br /&gt;
This starts up a &amp;quot;switchboard scenario&amp;quot; in which you can select the Micro AI demonstration scenario you want to check out.  Here, ''path/wesnoth-executable'' needs to be replaced by whatever the path and filename of the Wesnoth executable is on your system.&lt;br /&gt;
&lt;br /&gt;
== Setting up a Micro AI ==&lt;br /&gt;
&lt;br /&gt;
Setting up a Micro AI requires only one simple step:&lt;br /&gt;
==== Activating and configuring (or deleting) a Micro AI ====&lt;br /&gt;
&lt;br /&gt;
Micro AIs are activated, deleted and configured using the [micro_ai] tag.  This tag needs to be placed in [[ActionWML]], that is, in an event, a menu option or the like.  As an example, the following code activates the healer_support Micro AI in its default configuration for Side 2 from the beginning of the scenario:&lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
 &lt;br /&gt;
         # Configure the healer support micro AI&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
'''Keys required for all Micro AIs:'''&lt;br /&gt;
All [micro_ai] tags must contain the following three required keys:&lt;br /&gt;
* '''side''': The side for which the Micro AI is to be added, changed or deleted.  This has to be an individual side number, lists of sides are not supported.&lt;br /&gt;
* '''ai_type''': The type of Micro AI to be added, changed or deleted.  See the following sections for allowed values.&lt;br /&gt;
* '''action''' (string): The action to take concerning the Micro AI.  The following values are allowed: &lt;br /&gt;
** ''add'': Add a Micro AI to a side.&lt;br /&gt;
** ''change'': Change the configuration of an existing Micro AI.  Note that this does not only change the specific parameters provided in the tag, but it replaces the entire existing Micro AI by a new version with the new (and only the new) configuration.  It is therefore equivalent to using first the ''delete'' and then the ''add'' action.&lt;br /&gt;
** ''delete'': Delete an existing Micro AI from a side.&lt;br /&gt;
&lt;br /&gt;
'''Keys required under certain circumstances:'''&lt;br /&gt;
* '''ca_id''': (string) If several MAIs of the same type are used for the same side and one or several of these are to be changed or deleted during a scenario, this key needs to be included so that the [micro_ai] tag knows which of them needs to be changed/deleted.  The same is also true if a Micro AI is changed more than once during a scenario.  We therefore '''strongly recommend always setting ''ca_id''''' for MAIs which are, or might be, changed or deleted during a scenario.&lt;br /&gt;
** '''Note:''' The id(s)/name(s) actually assigned to the CA(s) of the MAI by the engine might be different from the parameter passed here.  It is, however, sufficient to use this ca_id with the [micro_ai] tag add/delete/change actions to identify the MAI uniquely.  (In other words: you don't have to worry about this if you always use the [micro_ai] tag, but might have to figure out the actual name(s) of the CA(s) if you want to delete or change them manually.)&lt;br /&gt;
* Other keys may also be required depending on the Micro AI.  In additional, a number of optional keys may be available to configure the AI behavior.  See the sections on the individual Micro AIs for details.&lt;br /&gt;
&lt;br /&gt;
'''Required keys for deleting a Micro AI:'''&lt;br /&gt;
&lt;br /&gt;
Only a subset of the keys required for setting up a Micro AI are needed when deleting it.  '''side''', '''ai_type''' and '''action'''=delete always need to be provided.  '''ca_id''' needs to be given if it was provided when adding the Micro AI (see above for when this is needed).&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* The ''add'' and ''change'' actions ignore all keys that do not apply to the respective Micro AI type.  The ''delete'' action ignores all keys other than the required keys listed above.&lt;br /&gt;
&lt;br /&gt;
* Some of the Micro AIs use and/or modify default AI components (candidate actions or aspects) when they are added, or reset these components to their default values when they are deleted.  Thus, if you have modified the AI yourself using the default CAs or aspects, some of these modifications might be affected, or might be interfering with the Micro AI.  The sections below indicate whether a Micro AI modifies any default AI components.&lt;br /&gt;
&lt;br /&gt;
===Combining Different Micro AI Types for the Same Side===&lt;br /&gt;
&lt;br /&gt;
No extra steps need to be taken when combining different Micro AIs on the same side.  Simply set up the [micro_ai] tags for all AIs you want to use.&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* While it is ''technically'' possible to combine any number of Micro AIs, this does not mean that all Micro AIs work together in practice.  Some are not compatible because they control units in contradictory ways.  &lt;br /&gt;
* Not all combinations that ''should'' work have been tested yet.  If you find a combination that you believe should work but doesn't, [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 please let us know] and we will see if that can be fixed.&lt;br /&gt;
* The candidate action score almost certainly has to be set (using the ca_score= key) for one or several of the MAIs if several of them are used on the same side.&lt;br /&gt;
&lt;br /&gt;
== Animals Micro AIs ==&lt;br /&gt;
=== General Comments on the Animal Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Animals Micro AIs are meant to simulate the behaviors of certain types of animals.  They are, however, set up so that they can be used with arbitrary unit types and might be applicable in quite diverse situations.  The AIs nevertheless remain named after the animals for which they were written originally, so that they are (to some extent) descriptive of their behavior.&lt;br /&gt;
&lt;br /&gt;
Brief descriptions of the AI behaviors are given in the following subsections.  To get a better feeling for their behavior, check out the &amp;quot;Animals&amp;quot;, &amp;quot;Swarm&amp;quot;, &amp;quot;Wolves&amp;quot; and &amp;quot;Dragon&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  Note that some of the animal AIs are designed for animal types that do not exist in mainline.  In the test scenario, other animal types are therefore substituted for them.&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Wolves (ai_type=wolves) ===&lt;br /&gt;
&lt;br /&gt;
The Wolves Micro AI organizes all &amp;quot;wolf&amp;quot; units (predators) of the side to hunt in a single pack.  They actively chase after the closest prey and try to corner it (not always super successfully), but are easily distracted by other enemy units coming into attack range, to the point where the pack might split up.  The wolves can, however, be told to attack only prey units as well as to try to avoid other types of units (such as larger predators).  When no prey is left, the wolves wander randomly.&lt;br /&gt;
&lt;br /&gt;
This AI works best for a small number of predators (2 - 5) and sparse prey.  Also, the predators should be put on the map in a pack, that is, reasonably close to each other.  For a demonstration of the Wolves AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Note that another wolves AI exists, called [[Micro_AIs#Animals_AI:_Multi-pack_Wolves_.28ai_type.3Dwolves_multipacks.29|Multipack Wolves]], which distributes the wolves of a side into several packs, rather than all of them into one single pack as it is done here.&lt;br /&gt;
&lt;br /&gt;
Enable the Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the predator units.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the prey units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_only_prey'''=no: In the default configuration, the wolves attack all enemy units in attack range.  If this key is set to 'yes', they only attack prey units.&lt;br /&gt;
* '''avoid_type''': Comma-separated list of unit types (on enemy sides) which the wolves try to avoid.  They try to stay out of these units' way as much as possible when moving around (except when they are moving in to attack another enemy unit) and never attack them.&lt;br /&gt;
* '''ca_score'''=90000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 90,000, which is lower than the default combat CA, but higher than most other default CAs which move units.  It can be changed by setting this parameter but in most cases this will not make sense.  Note that the Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Notes on default (RCA) AI aspects:''' &lt;br /&gt;
* If ''attack_only_prey'' or ''avoid_type'' are set, the Wolves Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* Attacks by the wolves are left to the default (RCA) AI, meaning that the value of ''aggression'' has an influence on the behavior.&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI is taken into account. Note: as attacks by the wolves are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=6&lt;br /&gt;
             ai_type=wolves&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Wolf&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Vampire Bat&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             avoid_type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Multi-pack Wolves (ai_type=wolves_multipacks) ===&lt;br /&gt;
&lt;br /&gt;
The Multi-pack Wolves Micro AI is different from the [[Micro_AIs#Animals_AI:_Wolves_.28ai_type.3Dwolves.29|Wolves Micro AI]] in that there can be an arbitrary number of wolf packs, with the pack size being a free parameter.  At the beginning of the scenario, close wolves are grouped into packs in a semi-methodical way.  If the wolves are put on the map in distinct groups of close wolves of the correct number, they will be joined into packs matching these groups.  If, on the other hand, they are spread out all over the map, the method of assigning them to packs is semi-random.&lt;br /&gt;
&lt;br /&gt;
Wolves of the same pack begin by joining each other on the map.  After that, they stay together until only one wolf is left, which then tries to join up with an incomplete pack or with other single wolves.  Individual wolves entering the map during the scenario behave in that way as well.&lt;br /&gt;
&lt;br /&gt;
A second difference to the other Wolves AI is that wolves do not actively hunt here.  For the most part they just wander (often long distance).  However, the pack ferociously (and without regard for its own health) attacks any enemy units that come into range, as long as that does not mean separating the pack by more than a few hexes.  Staying together, or joining with a new wolf assigned to the pack, is the only thing that takes priority over satisfying the wolves' thirst for blood.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Multi-pack Wolves AI behavior, check out the &amp;quot;Wolves&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Multi-pack Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves_multipacks&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Multi-pack Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''pack_size'''=3: (integer) The size of the packs.&lt;br /&gt;
* '''show_pack_number'''=no: If set to &amp;quot;yes&amp;quot;, the wolves' pack numbers will be shown below them.  Note that this means that the pack number of a wolf killed in an attack will remain as a label on the map.  This behavior is intentional to help UMC authors note where units might have made suicidal mistakes.&lt;br /&gt;
* '''type'''=Wolf: Comma-separated list of the unit types which the AI controls. The default value is &amp;quot;Wolf&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves_multipacks [micro_ai] tag usage from the &amp;quot;Wolves&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=wolves_multipacks&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             show_pack_number=yes&lt;br /&gt;
             pack_size=4&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Big Animals (ai_type=big_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Big Animals Micro AI is a simulation of large predators that wander and hunt alone, such as Bears, Giant Spiders or Yetis.  For the most part, these just wander on the terrain that's been defined for them, but they attack enemy units if those happen to come into range.  The AI can be set up so that the Big Animals stay out of the way of other units (such as other large predators), in which case they only attack those if cornered or if they run into them accidentally (such as ambushers).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Big Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Big Animals Micro AI by using&lt;br /&gt;
 ai_type=big_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the animals to which this AI is applied.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': The big animals often go for long distance (multi-turn) journeys.  This tag is a [[StandardLocationFilter|Standard Location Filter]] describing the terrain from which the next goal is chosen.  The default is all terrain on the map. &lt;br /&gt;
* '''[filter_location_wander]''': The terrain on which the big animals will end their moves. This is a [[StandardLocationFilter|Standard Location Filter]].  The default is all terrain on the map.&lt;br /&gt;
* '''[avoid_unit]''': A [[StandardUnitFilter|Standard Unit Filter]] describing all enemy units that the big animals will avoid.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
Here's an example of big_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=big_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Ghast&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [avoid_unit]&lt;br /&gt;
                 type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
             [/avoid_unit]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=1-40,1-18&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Forest Animals (ai_type=forest_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Forest Animals Micro AI simulates a number of different animal behaviors that one might find in a forest, namely animals behaving like deer, rabbits and families of tuskers.  In the following, we will refer to those animals by these types, even though the actual unit types can be selected freely.  They behave as follows:&lt;br /&gt;
* Each deer wanders randomly on (selectable) terrain, except when enemies get in its (the deer's) maximum movement range, in which case it flees to the farthest point it can reach.&lt;br /&gt;
* Tuskers exhibit the same behavior as deer, except when an enemy is next to one of the tusklets.  This enemy is attacked unconditionally by the tuskers trying to protect their young.&lt;br /&gt;
* Tusklets blindly follow the closest adult tusker, except when there is no tusker left, in which case they behave the same as deer.&lt;br /&gt;
* Rabbits also wander randomly, but in addition disappear into their holes (if any are within reach) when enemies are close.  They reappear out of their holes at the beginning of the turn, if it is safe.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Forest Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Forest Animals Micro AI by using&lt;br /&gt;
 ai_type=forest_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Forest Animals AI consists of 4 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-3&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''rabbit_type, tusker_type, tusklet_type, deer_type''': Comma-separated lists of the unit types behaving as the respective animals.  If any of these parameters are not set, those types of animal behavior are skipped by the AI.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain on which the forest animals wander when they are not threatened by enemies.&lt;br /&gt;
* '''rabbit_number'''=6: (integer)  The number of rabbits for the AI to have on the map simultaneously.  The AI puts the missing number of rabbits onto the map at the beginning of the AI's turn (when the rabbits come out of their holes).&lt;br /&gt;
* '''rabbit_enemy_distance'''=3: (number)  Rabbits won't spawn in holes that aren't more than this distance away from all enemies.&lt;br /&gt;
* '''rabbit_hole_img''': Rabbit holes are marked by items on the map.  If this parameter is set, it must be the file name of the item image.  If it is not set, all items on the map are treated as rabbit holes.&lt;br /&gt;
** {{DevFeature1.14|10}} Items used for rabbit holes on the map border used to produce an error. Placing them on the map border therefore needed to be avoided on the scenario setup side. They are now automatically excluded from consideration by the AI code.&lt;br /&gt;
&lt;br /&gt;
Here's an example of forest_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=forest_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             deer_type=Vampire Bat&lt;br /&gt;
             rabbit_type=Giant Rat&lt;br /&gt;
             tusker_type=Ogre&lt;br /&gt;
             tusklet_type=Young Ogre&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=*^F*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Herd and Herders (ai_type=herding) ===&lt;br /&gt;
&lt;br /&gt;
The Herding Micro AI sets up an area on the map where a number of herder units (such as sheep dog) control and protect a herd (such as sheep).  For convenience, we simply refer to them as sheep dogs and sheep in the following, even though arbitrary unit types can be selected for either.&lt;br /&gt;
&lt;br /&gt;
Sheep dogs try to keep their sheep safe.  This involves keeping them inside the herding area, positioning themselves in between the sheep and approaching enemies, and attacking the enemies if those get too close.  If no active herding or protecting move is needed, the dogs go to a random location on the perimeter of the herding area.&lt;br /&gt;
&lt;br /&gt;
Sheep wander aimlessly except when a sheep dog is next to them, in which case they run away from the dog.  The dogs exploit this by positioning themselves on the outside of the sheep, if possible.  Sheep also run away from approaching enemies.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Herding AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Herding Micro AI by using&lt;br /&gt;
 ai_type=herding&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herder units (e.g. dogs).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herd units (e.g. sheep).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain forming the perimeter of the herding area.  In other words, this is the path the herders (dogs) use to patrol around the outside of the herding area.  This must be a set of coordinates ''completely enclosing'' the herding area.  Here's an example of setting up a rectangular perimeter:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         x=10-20,10-20,10,  ,20&lt;br /&gt;
         y=11,   21,   11-21,11-21&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
Another possibility is by defining the path by certain terrain on the map (as it is done in the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]), in which case the filter can simply look like this:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         terrain=Rb&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
* '''herd_x,herd_y''': The coordinate of one hex (''any'' one hex) inside the herding area.  This is needed to define which part of the map is on the ''inside'' of the herding area perimeter defined by [herding_perimeter], and which is the ''outside''. {{DevFeature1.15|0}} '''herd_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''attention_distance'''=8: (integer)  If an enemy is within this distance of a herd unit, but not as close as ''attack_distance'', the herders try to position themselves in between the enemy and the herd unit.&lt;br /&gt;
* '''attack_distance'''=4: (integer)  If an enemy is within this distance of a herd animal, the herders attack this enemy.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Herding AI consists of 6 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-5&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Herding [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=herding&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Footpad&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Troll Whelp&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             herd_x,herd_y=32,28&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=Rb&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Hunter (ai_type=hunter) ===&lt;br /&gt;
&lt;br /&gt;
The Hunter Micro AI leads a unit (or units) through hunt-and-rest cycles.  The units wander in a prescribed area of the map, their hunting ground, until an enemy unit comes within range.  Note that the units do not actively chase down enemies, but wander randomly through the hunting ground until an enemy is encountered.&lt;br /&gt;
&lt;br /&gt;
If enemies are within range of a hunter unit, the hunter attacks the weakest of them.  If a kill is made, it then retreats to its rest location, where it stays for a certain number of turns and until fully healed (whatever happens later).&lt;br /&gt;
&lt;br /&gt;
A few more details:&lt;br /&gt;
* If the hunter's way home is entirely blocked on the return (so that there is no possible path), the normal RCA AI takes over its behavior.&lt;br /&gt;
* However, if the way is blocked by an enemy unit occupying the rest hex, that enemy will be attacked unconditionally.&lt;br /&gt;
* A kill only makes the hunter go home when it is the attacker, not as defender.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Hunter AI behavior, check out the &amp;quot;Dragon&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]. &lt;br /&gt;
&lt;br /&gt;
Enable the Hunter Micro AI by using&lt;br /&gt;
 ai_type=hunter&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''home_x,home_y''': The coordinates of the hex to which the hunter returns and rests at after a kill. {{DevFeature1.15|0}} '''home_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the unit's hunting ground.  If this tag is not provided, the hunter uses all of the map.&lt;br /&gt;
* '''rest_turns'''=3: (integer)  The number of turns the AI stays at home_x,home_y after a kill.  It will not leave its home until this number of turns have passed ''and'' it is fully healed.&lt;br /&gt;
* '''show_messages'''=no: (boolean) If set to yes, the hunter announces whenever its behavior changes to the next phase.  Note that this is meant as debug output for the scenario author.  The messages are not translated and are not included in replays (and are only visible to the host in an MP game).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a hunter [micro_ai] tag usage from the &amp;quot;Dragon&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hunter&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Rowck&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=5-30,1-15&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             home_x,home_y=3,15&lt;br /&gt;
             rest_turns=2&lt;br /&gt;
 &lt;br /&gt;
             show_messages=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Swarm (ai_type=swarm) ===&lt;br /&gt;
&lt;br /&gt;
The Swarm Micro AI uses some very simply algorithms to simulate animal swarm behavior.  Without adjacent enemies, they simply do a random move, trying to stay together and at a certain distance from enemies.  However, if an enemy unit is close to any bat, the swarm scatters.  This is particular fun to watch when one places an enemy unit in the middle of the swarm.  After being scattered, the swarm members slowly rejoin, but not in a very organized way.  Sub-swarms or individual bats might roam around for quite some time before they find their way back.  It is also possible that individual bats (or small groups) split off from the larger swarm at times.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Swarm AI behavior, check out the &amp;quot;Swarm&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Swarm Micro AI by using&lt;br /&gt;
 ai_type=swarm&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Swarm AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_distance'''=5: (int) The minimum distance kept between units of the swarm and enemies when the swarm moves as a whole.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.15|12}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the units.  Only AI units matching this filter participate in the swarm.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''scatter_distance'''=3: (int) Enemies within &amp;quot;scatter_distance&amp;quot; hexes of any swarm unit cause the swarm to scatter, by each unit trying to maximize its individual distance from the enemy.&lt;br /&gt;
* '''vision_distance'''=12: (int) Only units within this distance follow the overall swarm motion (either away from an enemy or of the swarm as a whole).  The smaller this value is set, the less likely the swarm is to stay together or rejoin.  This parameter is meant to simulate how far an individual member of the swarm can see, meaning that the swarm is &amp;quot;out of sight&amp;quot; (and the unit will not be able to follow it) if it is more than this distance away.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Swarm [micro_ai] tag usage from the &amp;quot;Swarm&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=swarm&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Assassin Squad Micro AI (ai_type=assassin) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|6}}&lt;br /&gt;
&lt;br /&gt;
This Micro AI controls one or several assassin units which try to circle around enemy units with the goal of hitting a target unit without being distracted or impeded by other enemies.  The units will always attack the target if such attacks are possible, otherwise they will follow the path of least resistance toward it. Notes:&lt;br /&gt;
&lt;br /&gt;
* The main priority for the AI is to find paths that avoid enemy units. However, if no such path can be found, the assassins will still advance toward the target, taking the path exposing the units to the least potential damage from enemies.&lt;br /&gt;
&lt;br /&gt;
* The units will not necessarily follow the same path. The best path is chosen individually for each unit.&lt;br /&gt;
&lt;br /&gt;
* While it is not a requirement for the AI to work, using ambusher or skirmisher units, or units with the quick trait, makes this AI more lethal in many cases.&lt;br /&gt;
&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded, it does not work otherwise. That means that '''ca_score''' is not a valid key for the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
Enable the Assassin Squad Micro AI by using&lt;br /&gt;
 ai_type=assassin&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Assassin Squad specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the assassin units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the target unit.&lt;br /&gt;
** It is important to realize that the AI currently only works correctly with a single target unit. It is thus the responsibility of the scenario designer to make sure that this filter matches an individual enemy unit. If several enemy units are to be targeted in the same scenario, several instances of the Assassin Micro AI can be used.&lt;br /&gt;
** The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[prefer]''': A [[StandardLocationFilter|Standard Location Filter]] selecting locations on the map for the assassins to prefer when making their way toward the target unit. This could be used, for example, to make units with the ambush ability move (mostly) through forest or to have the assassins circle around the, say, northern part of the map rather than the south if that is desirable for scenario design purposes.&lt;br /&gt;
** As with avoiding enemies, the units will still move across non-preferred terrain if that is either the only option or ''much'' shorter than preferred terrain&lt;br /&gt;
** Attacking, once the target unit is in range, is not influenced by the [prefer] SLF.&lt;br /&gt;
&lt;br /&gt;
Here is an example of an Assassin Squad [micro_ai] tag usage:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=assassin&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Assassin,Master at Arms,Duelist&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             [prefer] # Circle around northern part of map&lt;br /&gt;
                 x=1-9, 10-99&lt;br /&gt;
                 y=1-37, 1-17&lt;br /&gt;
             [/prefer]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Bottleneck Defense Micro AI (ai_type=bottleneck_defense) ==&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense Micro AI lets you define a location on the map where the AI can take a defensive stance behind a narrow passage (bottleneck).  Units on the front line are backed up by healers and/or units with the leadership ability.  Injured units are moved to the back and replaced by uninjured (or less injured) units.  The units on the front line only attack when it is safe (no retaliation) or when there is a high chance that they will make a kill or level up.  Using this Micro AI only makes sense if there is no way for the enemy sides to move around the bottleneck and attack the AI's units from behind.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Bottleneck&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Bottleneck Defense Micro AI by using&lt;br /&gt;
 ai_type=bottleneck_defense&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Bottleneck Defense specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense AI requires two sets of coordinates that define where it should be taking up its defensive stance, and from which side the enemy attacks at that location.&lt;br /&gt;
&lt;br /&gt;
* '''x,y''': Comma separated lists of the hexes on the front line, where strong units are placed.  All hexes on which the AI makes contact with (can be attacked by) the enemy need to be included here.  Units are placed on them in the order in which they are listed here. {{DevFeature1.15|0}} '''location_id''' can be used instead with named/special locations.&lt;br /&gt;
* '''enemy_x,enemy_y''': Comma separated list of the hexes from which the enemy can attack the AI front line units.  This is needed for the AI to know on which side of the front line support units (healers etc.) need to be placed.  In many cases, it is sufficient to provide only one of the enemy front line hexes, but there are geometries for which that does not work.  It is therefore safer to list all enemy front line hexes here. {{DevFeature1.15|0}} '''enemy_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
By default, the AI places healers and units with the leadership ability both on the front line itself (if they are the strongest units available) and on hexes adjacent to and behind the line.  If different placement is desired, these locations can be overridden with the following keys.&lt;br /&gt;
&lt;br /&gt;
* '''active_side_leader'''=no: (boolean)  If set to 'yes', the side leader participates in the bottleneck defense action after the side's gold has been spent.  If set to 'no' (default), the leader follows default AI behavior (sitting on the keep and recruiting, for the most part).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Bottleneck Defense AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.15|3}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the units.  Only AI units matching this filter participate in the bottleneck defense.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''healer_x,healer_y''': Comma separated list of hexes where healers are placed.  This can be used to keep healers from the front line (or to take up only certain positions on the front line), and/or to override the default healing positions behind the line.  Healers are placed on them in the order in which the hexes are listed, with the exception that hexes on the line always take priority over hexes behind the line. {{DevFeature1.15|0}} '''healer_loc''' can be used instead with named/special locations.&lt;br /&gt;
* '''leadership_x,leadership_y''': Same as ''healer_x,healer_y'', but for units with the leadership ability. {{DevFeature1.15|0}} '''leadership_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
As an example, in the &amp;quot;Bottleneck&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]], there are three front line hexes, two of which are on hill terrain, while the third is a road with a lower defense rating.  The healer and side leader are supposed to participate in combat because they are strong units, but only from the hill hexes to keep them safer.  This is accomplished by using the following keys settings (check out the scenario to see which hex is which):&lt;br /&gt;
 [micro_ai]&lt;br /&gt;
     side=1&lt;br /&gt;
     ai_type=bottleneck_defense&lt;br /&gt;
     action=add&lt;br /&gt;
 &lt;br /&gt;
     x=14,14,14&lt;br /&gt;
     y= 7, 9, 8&lt;br /&gt;
     enemy_x=13,13&lt;br /&gt;
     enemy_y= 8, 9&lt;br /&gt;
 &lt;br /&gt;
     healer_x=14,14,15,15&lt;br /&gt;
     healer_y= 7, 9, 8, 9&lt;br /&gt;
     leadership_x=14,14,15,15&lt;br /&gt;
     leadership_y= 7, 9, 9 ,8&lt;br /&gt;
     active_side_leader=yes&lt;br /&gt;
 [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Fast Micro AI (ai_type=fast_ai) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|0}}&lt;br /&gt;
&lt;br /&gt;
This AI is meant as a fall-back AI for scenarios where there are so many units that attacks and moves done by the default AI are slow with long delays before each move.  It replaces the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions (CAs).  It should be obvious that in order to achieve the faster speed, compromises concerning the quality of the moves have to be made.&lt;br /&gt;
&lt;br /&gt;
Enable the Fast Micro AI by using&lt;br /&gt;
 ai_type=fast_ai&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Combat candidate action:'''&lt;br /&gt;
* Attackers are considered individually one at a time.  The best attack found for a unit is executed without checking whether a better attack by another unit might be possible.&lt;br /&gt;
* The default order in which attackers are considered is by decreasing hitpoints, starting with the strongest unit (in terms of HP).  This can be modified with the optional keys below.&lt;br /&gt;
* By default, if weaker units block attack hexes for a stronger unit, they do not move out of the way (this is the same as for the default AI).  If a hex becomes available after an attack (e.g. due to a weaker unit dying in the attack), all attacks that were possible ''before'' this happened are finished first.  Only after that are attacks by the stronger unit reconsidered.&lt;br /&gt;
* If include_occupied_attack_hexes=yes is set, weaker units move out of the way for stronger units if possible.  This can, however slow down the AI depending on the situation on the map.  Use with caution.&lt;br /&gt;
* {{DevFeature1.13|2}} Previous to this version, attacks by the leader were bunched in with the attacks by the other units.  Now leader attacks are treated separately and contain an extra layer of analysis to determine whether the attack position is &amp;quot;safe&amp;quot; (in an approximate way).  Currently, this is done with two simple considerations:&lt;br /&gt;
** Count all enemy units that can reach the leader's attack position.  If there are more than given by optional parameter 'leader_attack_max_units' (default value 3), do not attack.&lt;br /&gt;
** Add up the hitpoints of all enemies that can reach the leader's attack position.  If this value, multiplied by optional parameter 'leader_weight'(default value 2), is larger than the leaders hitpoints, do not attack.&lt;br /&gt;
** Two optional parameters, 'threatened_leader_fights' and 'leader_additional_threat', can also be used to let the leader attack when the threat at the attack hex is comparable to that at the leader's current position.&lt;br /&gt;
** In order to keep this AI as fast as possible, leader attacks are done by a separate candidate action that is only evaluated when all other CAs are done.&lt;br /&gt;
&lt;br /&gt;
'''Move-to-targets, villages and retreat candidate actions:'''&lt;br /&gt;
* The move-to-targets, villages and retreat candidate actions of the default AI are removed.  Move-to-targets and villages are taken over by the Fast Micro AI, whereas retreat is simply not done.&lt;br /&gt;
* This CA moves units toward goals after attacks are done.  Goals are unowned (if the AI side has a leader only) and enemy-owned villages and enemy leaders.&lt;br /&gt;
* For each goal, units are pre-sorted in order of the fewest moves needed to cover the ''distance'' toward the goal (that is, as if move cost on each hex toward the goal were 1).  Units are then considered sequentially and if a unit can get there &amp;quot;efficiently&amp;quot;, its move is executed without considering other units.&lt;br /&gt;
** A move is considered efficient if the move cost to get to the hex is not more than 'move_cost_factor' larger than the distance to the hex.  'move_cost_factor' is an optional key that defaults to 2.  It can be set to values of 1.1 or larger (see below).&lt;br /&gt;
** If the move for the current unit is not found to be efficient, the AI considers the next unit instead.  If no unit can do an efficient move toward the goal, the best (fastest) of the inefficient moves is selected instead.&lt;br /&gt;
&lt;br /&gt;
* Village goals:&lt;br /&gt;
** Village goals are considered before enemy leader goals so that the AI preferentially chooses scouts (or otherwise fast units) for them.&lt;br /&gt;
** The number of units sent toward villages depends on the value of the default AI aspect village_value.  It is set to village_value/4 times the number of units the AI has left to move.  Thus, by default (village_value=1.0) the AI sends a quarter of its units toward villages, and the rest toward enemy leaders.  This means that the meaningful range of values for village_value is 0..4.&lt;br /&gt;
** Only one unit is moved toward each village.  This consideration also overrides the number of units to move toward villages defined by village_value, topping it at the number of unowned/enemy-owned villages on the map.&lt;br /&gt;
** Setting village_value=0 disables moving units toward villages altogether.  That is, not only are no units chosen, but the village targeting phase of the AI is entirely disabled with no evaluation time used for it.  Note that, unlike with the default AI, this also means that no villages within range of an AI unit are grabbed.&lt;br /&gt;
** The order in which villages are chosen as goals is some combination of them being close to the AI side's leader and being spread out over the map.&lt;br /&gt;
&lt;br /&gt;
* Enemy leader goals:&lt;br /&gt;
** The order of enemy leaders goals is set according to their distance from the AI leader with the closest enemy leader being targeted first (if the AI has a leader, otherwise their order is random).  One unit is sent toward each enemy leader in this order, after which the AI starts with the first enemy leader again.  This is repeated until all units have moved.&lt;br /&gt;
&lt;br /&gt;
* Guardians (units with ai_special=guardian set) are ignored by the fast move-to-targets CA.&lt;br /&gt;
&lt;br /&gt;
'''Tips for speeding up/modifying the Fast Micro AI in specific scenarios:'''&lt;br /&gt;
&lt;br /&gt;
A number of optional keys and default AI aspects can be used to modify the behavior of the Fast Micro AI.  The MAI is set up so that it is a reasonable compromise between speed and quality of the moves in most situations.  However, this might not be ideal for all scenarios here are some things that can be done to speed up the AI (or to get better behavior in some situations by doing the opposite):&lt;br /&gt;
* Use large values for the default AI aspect aggression.  The largest delays before attacks happen (in some circumstances) when the Fast MAI has to consider lots of units with attacks left but decides not to attack with them.  See below for a description of how aggression is used by the fast AI.&lt;br /&gt;
* Use large values of move_cost_factor (see above for description of what this does).  If you want to emphasize units that can move across terrain easily (e.g. bats) to be chosen for village grabbing, you might want to use low values of move_cost_factor, but this might slow down the AI.  The default value should work reasonably well in most standard situations, but modifying the value might work better on specific maps.&lt;br /&gt;
* Don't use dungeon_mode=yes unless absolutely necessary, even in most dungeon crawls.  Test the AI without this setting first and only use it if it is the only way to solve the problem.  Often, a minor tweak to the map will have the same effect, without the speed penalty.&lt;br /&gt;
* Use include_occupied_attack_hexes=yes with caution.  It might (or might not) slow down the attack CA significantly, depending on the scenario.&lt;br /&gt;
* Set village_value=0, which makes the AI not consider villages as goals.  This has not been found to make a large difference on the maps tested so far, but it might have an effect on maps with a very large number of villages.&lt;br /&gt;
* Use attack_hidden_enemies=yes on maps where there are no hidden enemies. Excluding hidden enemies from the attack calculation requires an additional filter to be evaluated that takes extra time. In most cases this will not result in a large computation time saving.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_hidden_enemies'''=no: (boolean)  By default, hidden enemies are excluded from the attack calculation.  Setting this to 'yes' makes the AI also consider those enemies as attack targets.&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.13|2}} A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  As with the default AI, units will not step on hexes defined by the [avoid] tag.  In addition, no goals will be chosen that lie on those hexes.  The one minor exception is that a unit stepping out of the way for another unit might temporarily enter the avoided zone.&lt;br /&gt;
* '''dungeon_mode'''=no: (boolean)  As much as possible, the Fast Micro AI works with distances between hexes rather than path finding.  This works pretty well on open maps and even most dungeon crawls (and similar scenarios), but it might result in some units getting temporarily (and in rare cases permanently) stuck on maps with convoluted labyrinths.  If that is the case, setting dungeon_mode=yes triggers more path finding that avoids such situations (but as a result is somewhat slower).&lt;br /&gt;
* '''include_occupied_attack_hexes'''=no: (boolean) If set to 'yes', the AI moves weaker units out of the way for a stronger attacker.  This might lead to better attacks but is slower, so don't use if speed is essential.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the attackers.  Only AI units matching this filter will attack, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes the side= key set to the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the enemies.  Only enemy units matching this filter will be attacked, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''leader_weight'''=2: (number &amp;gt;= 0) {{DevFeature1.13|2}} A factor specifying how much more valuable the AI considers side leaders than non-leader units in the attack evaluation. This is also used to determine whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''leader_additional_threat'''=1: (number &amp;gt; 0) {{DevFeature1.13|2}} If threatened_leader_fights=yes, the threat at the attack position may be larger by this factor for the leader to leave the keep for an attack.&lt;br /&gt;
* '''leader_attack_max_units'''=3: (number &amp;gt;= 0) {{DevFeature1.13|2}} A number specifying how many enemy units within reach are acceptable when determining whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''move_cost_factor'''=2: (number &amp;gt;= 1.1)  The factor by which the move cost to a goal hex may be larger than the distance to the goal hex for a move to be considered &amp;quot;efficient&amp;quot;.  See description above.  1.1 is used if this is set to values smaller than 1.1.  Smaller values might lead to better behavior, while larger values might speed up the AI.&lt;br /&gt;
* '''weak_units_first'''=no: (boolean) If set to 'yes', inverts the order in which units are considered by the combat CA.  Attacks are then done in order of increasing hitpoints of the attackers.&lt;br /&gt;
* '''skip_combat_ca'''=no: (boolean) If set to 'yes', the fast combat CA is not used.  The default AI's high_xp_attack and combat CAs remains in place in this case.&lt;br /&gt;
* '''skip_move_ca'''=no: (boolean) If set to 'yes', the fast move-to-targets CA is not used.  The default AI's retreat, villages and move-to-targets CAs remains in place in this case.&lt;br /&gt;
* '''threatened_leader_fights'''=no: (boolean) {{DevFeature1.13|2}} By default, the AI leader will not attack if the threat at the attack position is too high.  If this key is set to 'yes', the leader will attack if the threat there is not larger than the threat at his current position multiplied by the value of 'leader_additional_threat'. This is meant as an option for the endgame: when the AI leader is under attack, he might as well go down fighting.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''aggression''': The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to set how aggressive the AI should attack.  It works in roughly the same way as the default AI aspect:&lt;br /&gt;
** The value of (1 - aggression) determines how much the AI values its own units compared to the enemy's units.  Thus, aggression=0 means that attacks are only done if the AI expects to deal (roughly) the same amount of damage as it will receive.  aggression=1 means that expected damage to the AI's units is not considered, only the expected damage to enemy units matters.&lt;br /&gt;
** Damage is calculated as a combination of hitpoints and chance to die, in &amp;quot;gold equivalent units&amp;quot;.&lt;br /&gt;
** Unlike the default AI, aggression is set to 1 for values larger than 1.  Allowed values are thus anything from -infinity to +infinity, although only -infinity to +1.0 make sense.&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.13|2}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
*'''attacks aspect''':  {{DevFeature1.13|2}} The standard RCA AI [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] used to filter own and enemy units for attacks is now also taken into account as long as neither [filter] nor [filter_second] (see above under 'optional keys') are defined in the [micro_ai] tag.&lt;br /&gt;
*'''village_value=1''': (number ranging from 0 to 4) The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to define how much villages should be targeted by the move-to-targets CA.  In the Fast MAI, this defines which fraction of the AI units are moved toward village goals (see above for details).  If set to 0, villages are ignored as goals.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded to the same values as those of the respective [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|default candidate actions]].  ca_score is therefore not a valid optional key.&lt;br /&gt;
* The fast Micro AI modifies (removes) the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions.&lt;br /&gt;
&lt;br /&gt;
Enabling the Fast Micro AI in its default configuration is as simple as this usage example from the &amp;quot;Fast AI&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=fast_ai&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Generalized Goto Micro AI (ai_type=goto) ==&lt;br /&gt;
&lt;br /&gt;
The Goto Micro AI provides a highly configurable method of sending a unit (or units) to a location or set of locations.  The units to be moved are defined using a [[StandardUnitFilter|Standard Unit Filter]] (SUF) [alternatively, all side units are used if the SUF is not provided), while the goto locations are given in a [[StandardLocationFilter|Standard Location Filter]] (SLF).&lt;br /&gt;
&lt;br /&gt;
By default, each unit matching the SUF will move toward the closest goal (in terms of turns needed to get there) matching the SLF via the fastest route given the current situation on the map (locations of enemy units etc.), but this behavior can be influenced by the optional keys listed below.  Note that this means that a unit's goal might change from one turn to the next depending on the situation on the map.&lt;br /&gt;
&lt;br /&gt;
The behavior of the unit(s) once they reach their goal location(s) is configurable as well.  By default, they will stay there until the Goto Micro AI is deleted or changed.&lt;br /&gt;
&lt;br /&gt;
For demonstrations of several Goto Micro AI usages, check out the &amp;quot;Goto&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Goto Micro AI by using&lt;br /&gt;
 ai_type=goto&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Goto specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the possible goal locations for the move.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists). Note that the area defined by [avoid] is not entered at all, even if the end point of the move would lie outside the area again.&lt;br /&gt;
* '''avoid_enemies''': (number&amp;gt;0) By default, units controlled by the Goto Micro AI will not take enemy units into account for route finding, other than the enemies' ZoCs.  By contrast, if this key is set, they will try to avoid traversing hexes on which they can be attacked by enemies.  The larger the value of ''avoid_enemies'', the more the units will try to stay out of the enemies' way.  Notes:&lt;br /&gt;
** Units are willing to take a path that is 'avoid_enemies' movement points longer for each enemy unit that can be avoided by doing so for each hex along the path.  As an example, if avoid_enemies=1 and the shortest path crosses 2 hexes that can be reached by 3 enemies each, the unit is willing to take a path that is up to 6 hexes longer instead.  If avoid_enemies=10 in the same situation, the unit will take a path up to 60 hexes longer.&lt;br /&gt;
** Hexes adjacent to enemy units are always strongly avoided, independent of the value of avoid_enemies.&lt;br /&gt;
** avoid_enemies is ignored if 'use_straight_line=true' is set.&lt;br /&gt;
** Due to an issue with the path finding code, '''tunnels and teleports are ignored in Wesnoth 1.14 and before''' if 'avoid_enemies' is used. This has been fixed in Wesnoth 1.15.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units to be moved.  Defaults to all units of the side if not defined.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''ignore_enemy_at_goal'''=no and '''ignore_units'''=no: (boolean)  In the default configuration, the Goto MAI does not move units if the path to all goal hexes is blocked by enemies.  It is therefore possible to disable the Goto MAI by putting enemy units on all the goal hexes.  That can be changed if either of these two keys is set to 'yes'.&lt;br /&gt;
** 'ignore_enemy_at_goal=yes' makes the AI choose its goals while disregarding any enemy units at the goal hexes.  Even so, a stiff rating penalty is applied to goal hexes occupied by enemies, thus that unoccupied goal hexes are chosen unless all possible goals are occupied by enemies.  Note that this setting ''only'' ignores enemy units at the goal hexes themselves.  It is still possible to disable the AI by putting enemies all around the goals.&lt;br /&gt;
** 'ignore_units=yes' let's the AI ignore ''all'' enemy units when deciding which goal hex(es) to move toward.&lt;br /&gt;
** In both cases, enemies are only ignored for deciding which unit to move toward which goal hex.  For the actual move itself, all enemies and their ZoCs are considered as usual.&lt;br /&gt;
** Both options only take effect if neither avoid_enemies is used, nor use_straight_line is set to 'yes', as neither of these consider enemies as blockers. Or in other words, if avoid_enemies is used, or use_straight_line=yes, it is unnecessary to set ignore_enemy_at_goal or ignore_units to yes.&lt;br /&gt;
* '''release_unit_at_goal'''=no: (boolean) By default, a unit that reaches a goal location will stay there until the Goto Micro AI is deleted or changed (or until the conditions specified in the [goto_goals] SLF change).  If this parameter is set, a unit that has reached its goal will not be controlled by this Micro AI any more starting from the next turn.&lt;br /&gt;
* '''release_all_units_at_goal'''=no: (boolean) Same as ''release_unit_at_goal'', but this will deactivate the Goto Micro AI for all units once the first unit reaches a goal.&lt;br /&gt;
* '''remove_movement'''=yes: (boolean):  {{DevFeature1.15|11}}  By default, when the Goto MAI cannot move a unit (for example, because the path is blocked by enemies), its movement points are still removed. If this option is set to 'no', this step does not happen, so that other AI candidate actions (or even another Goto MAI with different parameters) can take over instead.&lt;br /&gt;
* '''unique_goals'''=no: (boolean)  If set to 'yes', each unit will be sent to a different goal location.  Note that the best goals are recalculated each turn, so a unit might choose a different goal this turn from what it was heading for during the previous turn if the situation on the map has changed.  Also note that if there are more units than goal locations, the left over units will not be handled by the Goto MAI but follow default behavior instead.&lt;br /&gt;
* '''use_straight_line'''=no: (boolean)  By default, units choose the route by which they will get to their goal locations in the smallest number of turns.  If this parameter is set to 'yes', a straight line route (or something pretty close to it at least) will be chosen instead, irrespective of whether this leads into a dead end, whether it is blocked by enemies, etc.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* Depending on the parameters used, the Goto Micro AI might require a lot of path finding to be done.  Thus, while it is possible to set up this AI to move many units to many different locations, a usage like that might require quite a long calculation time.&lt;br /&gt;
&lt;br /&gt;
===Goto Micro AI example usages:===&lt;br /&gt;
&lt;br /&gt;
Here are several examples of goto [micro_ai] tag usages from the &amp;quot;Goto&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
* Send a unit toward a castle (similar to the RCA AI's Goto Candidate Action, but avoiding dead ends and areas ZoCed by enemies)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
             ca_id=messenger1&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=messenger1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=6-7,2-4&lt;br /&gt;
                 terrain=C*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             ca_score=210001&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send all units of the side toward the southern border of the map&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=8&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                  y=33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             avoid_enemies=1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send units to the four corners of the map (with each unit going to a different corner).&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x=1,1,44,44&lt;br /&gt;
                 y=1,33,1,33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             unique_goals=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Set up a guardian unit that doesn't move while no enemy unit is within 8 hexes (ironically, the Goto Micro AI can be used to keep units in place, although using one of the Guardian Micro AIs might often be a better idea)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=guard1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 [filter]&lt;br /&gt;
                     id=guard1&lt;br /&gt;
                 [/filter]&lt;br /&gt;
                 [not]&lt;br /&gt;
                     [filter]&lt;br /&gt;
                         side=1&lt;br /&gt;
                     [/filter]&lt;br /&gt;
                     radius=8&lt;br /&gt;
                 [/not]&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Guardian Micro AIs ==&lt;br /&gt;
=== General Comments on the Guardian Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Guardian Micro AIs can be used to set up different kind of guardian behaviors for a unit or a set of units.  Four different types of guardians are available and are described below.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different guardian behaviors, check out the &amp;quot;Guardians&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
=== Return Guardian  (ai_type=return_guardian)===&lt;br /&gt;
&lt;br /&gt;
A 'return guardian' is a variation of the standard Wesnoth guardian. It has an assigned guard position (GP) to which it returns after attacks on approaching enemies:&lt;br /&gt;
* If at GP with no enemy in reach, do nothing.&lt;br /&gt;
* If at GP with an enemy in reach, attack. Note that the attack is done by the default AI, which might result in the guardian not attacking if the enemy is deemed too strong (this can be influenced by setting the default ''aggression'' value to a different value).&lt;br /&gt;
* If not at GP, return there, no matter whether an enemy is in reach or not.&lt;br /&gt;
* If enemies are blocking the way back to GP, do your best to move around them.&lt;br /&gt;
* If the guardian ends up next to an enemy on the way back, attack that enemy after the move.&lt;br /&gt;
&lt;br /&gt;
Enable the Return Guardian Micro AI by using&lt;br /&gt;
 ai_type=return_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''return_x,return_y''': The coordinate of the hex the unit returns to after each attack.  {{DevFeature1.15|0}} '''return_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=100010: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  This AI consist of 2 CAs, one with score &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt; the other with &amp;lt;tt&amp;gt;ca_score-20&amp;lt;/tt&amp;gt;.  The default value is set so that the first CA gets executed right before the default combat CA, and the other right after.  It usually will not make sense to change this value, but it is possible to do so for custom applications.  {{DevFeature1.13|6}}: This is now changed to 100100 and 99900 in order to avoid conflicts with the new high_XP_attacks candidate action.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a return guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=return_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=return1&lt;br /&gt;
             return_x,return_y=20,2&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
===Stationed Guardian (ai_type=stationed_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A 'stationed guardian' is another variation of the standard Wesnoth guardian with a somewhat more complex behavior than that of the 'return guardian'. Two positions are defined for it, a 'station' and a 'guarded location', as well as a distance. The behavior is as follows:&lt;br /&gt;
* If no enemy is within 'distance' of the guard's current position, do nothing.  This is independent of the guardian being at its station or not.&lt;br /&gt;
* Otherwise: If an enemy is within 'distance' of the guard, but not also within the same distance of the guarded location and the station (all of this simultaneously), move the guard in the direction of the station (or keep the guard in place if the station is unreachable).&lt;br /&gt;
* Otherwise:&lt;br /&gt;
** Pick the enemy unit that is closest to the guarded location.&lt;br /&gt;
** If we can reach it, pick the adjacent hex with the highest defense rating and attack from there.&lt;br /&gt;
** If not in reach, move toward this unit.&lt;br /&gt;
&lt;br /&gt;
Enable the Stationed Guardian Micro AI by using&lt;br /&gt;
 ai_type=stationed_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance parameter as explained above&lt;br /&gt;
* '''station_x,station_y''': The x and y position of the hex that serves as the guardians station. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''guard_x, guard_y''': The x and y position of the guarded location.  {{DevFeature1.13|0}}  These are now optional keys, defaulting to station_x,station_y if not provided. {{DevFeature1.15|0}} '''guard_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a stationed guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=stationed_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=stationed1&lt;br /&gt;
             distance=4&lt;br /&gt;
             station_x,station_y=2,14&lt;br /&gt;
             guard_x,guard_y=3,13&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Coward (ai_type=coward) ===&lt;br /&gt;
&lt;br /&gt;
Cowards are units that, like guardians, sit around doing nothing until an enemy comes into range.  Unlike guardians, however, they run away once enemies approach. Applications might be wild animals, unarmed civilians getting in the way of a battle, etc.  The coward macro can be called with two optional locations, 'seek' and 'avoid':&lt;br /&gt;
* If neither is given, the coward retreats to the position farthest away from the approaching enemies.&lt;br /&gt;
* If 'seek' is given, it preferentially goes toward that location (but getting away from enemies takes priority).&lt;br /&gt;
* If 'avoid' is given, it in addition tries to avoid that location (with both maximizing distance from enemies and going toward 'seek' taking priority).&lt;br /&gt;
* Both 'seek' and 'avoid' may consist of only one coordinate ('x' or 'y'), in which case not a single hex, but a line of hexes is sought or avoided.&lt;br /&gt;
&lt;br /&gt;
Enable the Coward Micro AI by using&lt;br /&gt;
 ai_type=coward&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance within which an enemy must be to &amp;quot;scare&amp;quot; the unit&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_if_trapped'''='no':  {{DevFeature1.13|0}}  With the default settings, a coward never attacks.  If this key is set to 'yes', the coward attacks the weakest (lowest hitpoints) enemy next to which it ends up after moving (such as a cornered animal would do, blindly lashing out without regards for its own safety).&lt;br /&gt;
* '''avoid_x, avoid_y''': The x and y coordinate of the hex to avoid. {{DevFeature1.15|0}} '''avoid_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units from which the cowards flee.  If not set, this defaults to all enemy units.  Note, however, that no &amp;quot;enemy of AI side&amp;quot; condition is added if [filter_second] is specified, meaning that it can include allied and/or enemy units as desired (and even units on the same side as the coward).&lt;br /&gt;
* '''seek_x, seek_y''': The x and y coordinate of the hex to seek. {{DevFeature1.15|0}} '''seek_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a coward [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=coward&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=coward3&lt;br /&gt;
             distance=5&lt;br /&gt;
             seek_x,seek_y=24,5&lt;br /&gt;
             avoid_x,avoid_y=24,15&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Zone guardian (ai_type=zone_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A zone guardian is a unit that, as the name says, guards a zone. It moves randomly inside this zone until an enemy enters it (or a separately defined enemy zone, see below). Applications might be the defense of a castle, a nesting area or for protecting an unit.  The zone macro can be called with an optional SLF, [filter_location_enemy]:&lt;br /&gt;
* If not specified, the zone guard attacks any enemy coming inside its guard zone.&lt;br /&gt;
* If [filter_location_enemy] is given, it attacks any enemy entering this zone and once there are no more enemies, it goes back to patrol in its basic zone.&lt;br /&gt;
&lt;br /&gt;
Enable the Zone Guardian Micro AI by using&lt;br /&gt;
 ai_type=zone_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone to guard.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_enemy]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone where enemies have to be attacked.  If not given, it defaults to [filter_location].&lt;br /&gt;
* '''station_x,station_y''': By default, the zone guardian moves randomly within the zone if there is no enemy to attack.  However, if station_x/y is given, it moves to this position instead during turns when it does not have an enemy to attack.  This position does not need to be inside the zone defined above.  Note: obviously it does not make sense to define [filter_location], [filter_location_enemy] and station_x/y all at the same time.  Only [filter_location] and either one of the latter two (or none of them) should be provided for any zone guardian. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a zone guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=zone_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=zone3&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=22-31,4-11  # This is intentionally chosen to extend past the lake&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             station_x,station_y=32,8&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Hang Out Micro AI (ai_type=hang_out) ==&lt;br /&gt;
&lt;br /&gt;
The Hang Out Micro AI instructs units of the AI side to &amp;quot;hang out&amp;quot; around a certain location, or locations, until a condition for mobilizing the side is met.  If none of the optional keys are given, the AI controls all units of the side to hang around the leader while moving off castle tiles to make room for recruiting.&lt;br /&gt;
&lt;br /&gt;
The AI works by moving each unit to its best hex first using the minimum amount of movement points required for this move.  Once all AI units are in position, movement points are set to zero for all of them simultaneously such that they cannot be used by other AI candidate actions.  This is done because the mobilizing condition might be met as result of one of the moves of the Hang Out AI, after which we want units to be able to continue with other AI actions.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Hang Out and Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Hang Out Micro AI by using&lt;br /&gt;
 ai_type=hang_out&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Hang Out specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  In its default configuration, the Hang Out MAI controls all units of a side, keeping them as close to the side leader(s) as possible while moving off castle tiles to make room for recruiting.  For this purpose, the candidate action score defaults to 170,000, which is just below the score of the default recruiting CA.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''': A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  If neither of these two exists, this parameter is set to default to all castle terrain (&amp;lt;tt&amp;gt;terrain=C*,C*^*,*^C*&amp;lt;/tt&amp;gt;) so that units move off the castle to make room for recruiting.  Note that this does not include keep terrain, otherwise the leader would be moved off the keep as well.&lt;br /&gt;
* '''ca_score'''=170000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 170,000, which means that it is executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Hang Out action (&amp;lt;tt&amp;gt;side=&amp;lt;/tt&amp;gt; is always set and does not need to be provided).  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the locations ''near'' which the AI units should hang out.  This does not mean that the units only move onto terrain specified here, but that they minimize the distance to such terrain.  It defaults to the position of the side leader(s).&lt;br /&gt;
* '''[mobilize_condition]''':  Contains ([[ConditionalActionsWML#Condition_Tags|WML Condition Tags]])  Once the condition set up here has been met, the Hang Out MAI is disabled and the side goes over to other AI activity.  The Hang Out MAI remains disabled after this, even if the condition is not met any more later in the scenario.&lt;br /&gt;
* '''mobilize_on_gold_less_than''': (integer) Since side gold cannot be checked in a WML condition tag without storing it in a variable first, this key is provided separately.  It disables the Hang Out Micro AI once the side gold goes below the value specified here for the first time.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
* The Hang Out AI still takes effect if no locations to hang out at are found or if all map locations are to be avoided.  In that case, it simply keeps all units at their current locations.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Hang Out Micro AI configuration from the &amp;quot;Hang Out and Messenger&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hang_out&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [avoid]&lt;br /&gt;
                 terrain=C*,H*,M*,A*,S*,*^F*&lt;br /&gt;
             [/avoid]&lt;br /&gt;
             [mobilize_condition]&lt;br /&gt;
                 [have_unit]&lt;br /&gt;
                     side=1&lt;br /&gt;
                     count=7-99&lt;br /&gt;
                 [/have_unit]&lt;br /&gt;
             [/mobilize_condition]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Healer Support Micro AI (ai_type=healer_support) ==&lt;br /&gt;
&lt;br /&gt;
The Healer Support Micro AI configures the healers of a side to stay behind the battle lines and heal injured and/or threatened units rather than participate in combat under all circumstances.  You can set different levels of aggressiveness for the healers.  &lt;br /&gt;
&lt;br /&gt;
'''Notes'''&lt;br /&gt;
* By default, ''all'' healers of the AI side are controlled by this AI.  This includes in particular also the side leader, if the leader is a healer.  If you do not want the leader to be included (and possibly be moved away from the keep etc.) use the [filter] tag with canrecruit=no (see below).&lt;br /&gt;
* It is not always better to use this Micro AI than the default AI, as it takes healers (which can be strong units) away from the pool of units to choose for attacks.&lt;br /&gt;
* {{DevFeature1.15|3}} This Micro AI now takes unit guardian status and the passive_leader aspect into account.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Healers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Healer Support Micro AI by using&lt;br /&gt;
 ai_type=healer_support&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Healer Support specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the healer units.  If set, only units on the AI side passing this filter ''and'' having the healing ability are considered by this Micro AI.&lt;br /&gt;
*'''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting units to receive healing. If set, only units on the AI side passing this filter ''and'' the other healee selection criteria will be selected for healing by this Micro AI.&lt;br /&gt;
* '''aggression'''=1.0: (float) Sets the aggressiveness of the AI.  This parameter is set up as a float to accommodate future functionality, but it currently acts as a boolean: if set to zero, the AI will never let its healers participate in combat, for all other values it allows them to attack after all other units have attacked and if the healers cannot find any more units to support.  The former behavior might be appropriate in scenarios with only one or few valuable healers, while the latter might work better in scenarios with many healers.&lt;br /&gt;
* '''injured_units_only'''=no: (boolean) If set to 'yes', the AI will only move healers next to units that are already injured and skip units that currently have full hitpoints, but might get injured during the next enemy turn.&lt;br /&gt;
* '''max_threats'''=9999: (integer) The maximum allowable number of enemies that can attack a hex in order for it to be considered for a healer.  As an example, setting 'max_threats=0' means that the AI only moves healers to locations that are entirely safe from the enemy (assuming that none of the units currently on the map dies).  Note that the value of this key is checked against the number of enemies that can make it to the hex, not the number of adjacent hexes from which the healer could be attacked.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI. Note: as attacks by the healers are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The Healer Support Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded.  It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Healer Support Micro AI configuration from the &amp;quot;Healers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             aggression=0&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Lurkers Micro AI (ai_type=lurkers) ==&lt;br /&gt;
&lt;br /&gt;
The Lurker Micro AI controls some or all units of a side in &amp;quot;lurker mode&amp;quot;.  A lurker is a unit that is capable of moving across most terrains, but that only stops on and attacks from specific terrain.  It might also have the ability to hide on this terrain (which is the reason why it is called a lurker).&lt;br /&gt;
&lt;br /&gt;
Lurkers move individually without strategy and always attack the weakest enemy within their reach.  If no enemy is in reach, the lurker does a random move instead - or it just sits and waits (lurks).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of different types of lurker behavior, check out the &amp;quot;Lurkers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Lurkers Micro AI by using&lt;br /&gt;
 ai_type=lurkers&lt;br /&gt;
in the [micro_ai] tag&lt;br /&gt;
&lt;br /&gt;
===Lurkers specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
* '''[filter]''': Specifies which units of the side are controlled by the Lurker Micro AI.  This is a [[StandardUnitFilter|Standard Unit Filter]].  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': Specifies the terrain from which the lurkers attack. This is a [[StandardLocationFilter|Standard Location Filter]].&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_wander]''': Specifies the terrain on which lurkers end their moves if there is no enemy in reach. This is a [[StandardLocationFilter|Standard Location Filter]].  If [filter_location_wander] is not given, it defaults to [filter_location].&lt;br /&gt;
* '''stationary'''=no: (boolean)  If set to yes, a lurker never moves if there is no enemy within attack range.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Lurkers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=4&lt;br /&gt;
             ai_type=lurkers&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Naga Fighter&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=W*,S*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             [filter_location_wander]&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location_wander]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Messenger Escort Micro AI (ai_type=messenger_escort) ==&lt;br /&gt;
&lt;br /&gt;
The Messenger Escort Micro AI lets you define a set of waypoints through which it tries to move one or several of its units, the messenger(s).  The other units escort the messenger, trying to protect it and attacking enemies that are in its way.  Note that a messenger might not strictly hit intermediate waypoints, but that getting close is good enough, especially if a waypoint is occupied by an enemy unit.  It does, however, always try to get to the last waypoint.&lt;br /&gt;
&lt;br /&gt;
'''Notes on the use of several messengers:''' {{DevFeature1.13|0}}  &lt;br /&gt;
* This Micro AI can control several messengers at the same time, which can but do not have to move in a group.  They can be put on the map altogether in one location, or be spread out on the map or across different turns.&lt;br /&gt;
* The farther a messenger has moved through the chain of waypoints, the higher a priority it is for the AI, meaning that farther advanced (on the map) messengers are moved first, and that the escort units try to clear the way and stay with those messengers more so than with those further back on the map.  This order can be inverted using the invert_order= key.&lt;br /&gt;
* If several messengers are on the map, escort units might move from one to the other, the AI making a judgement call where they are needed most.  Closeness to a messenger is generally the most important criterion, but not the only one.&lt;br /&gt;
* If a messenger dies, the escort units previously staying with it now join one of the other messengers if there are any left (otherwise they follow default AI behavior).&lt;br /&gt;
* If you want certain escort units to stay always with the same messenger, use several messenger [micro_ai] tags with different [filter]/[filter_second] combinations.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Messenger Escort Micro AI by using&lt;br /&gt;
 ai_type=messenger_escort&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Messenger Escort specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': {{DevFeature1.13|0}} (Previous to 1.13.0, only id= was allowed.) The id of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated list of waypoint coordinates through which the messenger will go in the given order to reach its goal, given by the last coordinate.  If you want the messenger to go directly to the goal, simply enter a single x,y coordinate here.  Note that the messenger does not have to hit each waypoint exactly (except for the final one), getting close to them is good enough. {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|11}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Messenger Escort AI consists of 3 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-2&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_death_chance'''=0.67 and '''messenger_death_chance'''=0.0: (both floats; ranging from 0 to 1)    If the messenger is adjacent to an enemy at the end of the move, it will only attack the enemy if the chance of death of the enemy is &amp;gt;= enemy_death_chance and if the messenger's chance of death is &amp;lt;= messenger_death_chance (or if it is an attack at a range for which the enemy does not have a weapon).&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|0}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the escort units.  If not set, this defaults to all enemy units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''invert_order'''=no: {{DevFeature1.13|0}} If set to 'yes', the order of priority with which messenger units are considered is inverted, starting with the messenger unit that is the least advanced on the map (see description above).  In most cases, selecting this option results in worse performance of the AI, so use it with caution.  (And obviously it has no effect at all if there is only one messenger controlled by the AI.)&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|11}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Messenger Escort&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=messenger_escort&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=messenger&lt;br /&gt;
             waypoint_x=31,24,27,28&lt;br /&gt;
             waypoint_y=20,14,7,1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Patrol Micro AI (ai_type=patrol) ==&lt;br /&gt;
&lt;br /&gt;
The defines a number of waypoints for a unit (or units) to follow.  Options can be set whether the route should be followed in a loop, as an out-and back, only once, whether units encountered along the way should be attacked, etc.&lt;br /&gt;
&lt;br /&gt;
With the default settings, getting to the next waypoint takes priority over attacking for a patroller, but this can be overridden (as of '''Wesnoth 1.15.12''', using the '''attack_range''' option).  Unless instructed otherwise, the AI thus prefers to move the patrol unit around enemies rather than straight for them.  Also, if a waypoint is occupied by a unit the AI is not instructed to attack, it will (eventually) abandon that waypoint once it gets close enough and move on to the next one.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different patrol behaviors, check out the &amp;quot;Patrols&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Patrol Micro AI it by using&lt;br /&gt;
 ai_type=patrol&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Patrol specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated lists of the waypoint coordinates through which the patrol travels (in order). {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack''': By default (if this key is not set), the patrol unit attacks any unit it ends up next to after a move, or that is within '''attack_range''' (see below).  Alternatively, this parameter can be set to a comma-separated list of unit ids. If that is done, the patrol only attacks these units when it ends up next to them or if it gets within attack_range.  It ignores any other unit.  This can in turn be used to have the patroller not attack at all, by setting ''attack='' to a non-existing id.  The exception to this is an enemy on the last waypoint with ''one_time_only=yes'', which will always be attacked.&lt;br /&gt;
* '''attack_range'''=1: {{DevFeature1.15|12}} If the patrol comes within this many hexes of an enemy unit, it interrupts its patrol route and attacks that enemy (assuming it still has enough movement left to get to the enemy).  The '''attack=''' instructions are taken into account for this.  Note that the patrol generally tries to move around enemies, which means that for the default value of 1, enemies are usually not within attack range, except at the end of the move.&lt;br /&gt;
* '''attack_invisible_enemies'''=no: {{DevFeature1.15|12}} If set to 'yes', the unit also attacks invisible enemies (according to the criteria set by '''attack''' and '''attack_range''').&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''one_time_only'''=no: (boolean)  If set to 'yes', the unit goes through the patrol route only once and then hangs out at the last waypoint.  It also always attacks any enemy unit on the last waypoint, independent of what ''attack='' is set to.&lt;br /&gt;
* '''out_and_back'''=no: (boolean)  By default, a patrol unit returns to the first waypoint after reaching the last, doing the patrol route as a loop.  If this parameter is set to 'yes', the unit instead reverses its course and perpetually does out and backs of the route.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Patrols&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=patrol&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Konrad&lt;br /&gt;
             waypoint_x=9,24,25&lt;br /&gt;
             waypoint_y=21,23,15&lt;br /&gt;
             one_time_only=yes&lt;br /&gt;
             attack=Gertburt&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Protect Unit Micro AI (ai_type=protect_unit) ==&lt;br /&gt;
&lt;br /&gt;
The Protect Unit Micro AI tries to move one or several units to a given location while keeping them safe from enemy attacks. It does so by keeping the protected units away from enemy units and close to friendly units. Other units on the AI side are controlled by the default Wesnoth AI.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Protect Unit&amp;quot; and &amp;quot;The Elves Besieged&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Protect Unit Micro AI by using&lt;br /&gt;
 ai_type=protect_unit&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Protect Unit specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[unit]''': A separate [unit] tag is required for each protected unit(s).  Each tag has required keys '''id''', '''goal_x''', '''goal_y''' for each unit, as shown in the example below. {{DevFeature1.15|0}} '''goal_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''disable_move_leader_to_keep'''=no: (boolean) If set to 'yes', side leaders protected by this AI do not try to return to their keep.  This is necessary for this Micro AI to work with units that are side leaders and that are supposed to move to an off-keep location.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
*The Protect Unit Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].  Depending on the parameters, it might also modify the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|move leader to keep candidate action]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded. It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;HttT: The Elves Besieged&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=protect_unit&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Delfador&lt;br /&gt;
                 goal_x,goal_y=1,2&lt;br /&gt;
             [/unit]&lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
                 goal_x,goal_y=1,1&lt;br /&gt;
             [/unit]&lt;br /&gt;
 &lt;br /&gt;
             disable_move_leader_to_keep=true&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Recruiting Micro AI==&lt;br /&gt;
=== General Comments on the Recruiting Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Recruiting Micro AI replaces the default recruitment AI by alternative recruiting patterns.  Currently there are two recruiting algorithms you can choose from, random recruiting and the rush recruiting used in the new (as of Version 1.11.1) Experimental AI (see below).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of a different recruiting pattern, check out the &amp;quot;Recruiting&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  You can also explore the unit choices by watching the Experimental AI in a multiplayer game.&lt;br /&gt;
&lt;br /&gt;
'''Note:''' Obviously, the Recruitment Micro AI modifies the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|recruitment candidate action]].&lt;br /&gt;
&lt;br /&gt;
=== Random Recruitment AI (ai_type=recruit_random) ===&lt;br /&gt;
&lt;br /&gt;
This AI randomly selects a unit type from the recruitment list of the side.  The probability of choosing each type and the behavior in low-gold situations can be influenced with optional parameters.&lt;br /&gt;
&lt;br /&gt;
Enable the Random Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_random&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* '''[probability]''': This tag sets the probability of selecting the given unit type.  It has two required sub-keys:&lt;br /&gt;
** '''type''': comma-separated list of unit types to which this probability is to be applied&lt;br /&gt;
** '''probability''': (non-negative float)  The probability with which these unit types is to be selected.  All unit types for which no [probability] tag is defined receive a default probability of 1.0, thus values should be chosen relative to that.  As an example, in the code below swordsmen and peasants are given a probability of 8, while no other units have assigned probabilities.  Thus, each of these two unit types is 8 times more likely to be selected than any other individual unit type.&lt;br /&gt;
* '''skip_low_gold_recruiting'''=no: (boolean)  By default, the random recruitment AI chooses a unit to recruit only from those types for which it has sufficient gold.  If this parameter is set to 'yes', it will instead choose from all available unit types and end recruiting for the turn if there is not enough gold to recruit it this turn (even if other, affordable unit types exist).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a random recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_random&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [probability]&lt;br /&gt;
                 type=Swordsman,Peasant&lt;br /&gt;
                 probability=8&lt;br /&gt;
             [/probability]&lt;br /&gt;
 &lt;br /&gt;
             skip_low_gold_recruiting=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Rush Recruitment AI (ai_type=recruit_rushers) ===&lt;br /&gt;
&lt;br /&gt;
This AI replaces the default recruiting with the recruiting used in the Experimental AI [[Experimental_AI#Recruitment|as described here]].&lt;br /&gt;
&lt;br /&gt;
Enable the Rush Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_rushers&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* {{DevFeature1.15|2}} '''high_level_fraction'''=0: (non-negative number) The approximate fraction of units of level 2 or higher to be recruited. This is defined as fraction of units on the map, not of new units being recruited during a turn or over several turns (which makes a difference if there are already units on the map). The effect is also cumulative per level (starting from level 2), meaning that 1/3 of L2 units will be recruited in the example above, 1/3*1/3=1/9 L3s etc. The default value is zero, which leaves the high-level unit recruiting to the default Experimental AI algorithm resulting usually in very few such units being recruited.&lt;br /&gt;
* '''randomness'''=0.1: (number)  A random number is applied to the rusher recruit engine's score to prevent the recruitment pattern from being too predictable. 0 causes no randomness to be applied, while larger numbers increase the random effect. A value of 1-2 generates results in which the random effect is approximately equal to the scored effect. Extremely high values are essentially entirely random.&lt;br /&gt;
* {{DevFeature1.17|6}} '''reset_cache_each_turn'''=no: (bool) Many of the operations of the rush recruitment evaluation are expensive and are therefore cached. For the most part, this cache can be kept from turn to turn, but in case there are events that change, for example, the map or the recruit list, this parameter can be set to 'yes' to force a reset of the cache each turn.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a rush recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_rushers&lt;br /&gt;
             action=add&lt;br /&gt;
          [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Simple Attack Micro AI (ai_type=simple_attack) ==&lt;br /&gt;
&lt;br /&gt;
The Simple Attack Micro AI is meant as an addition to the default combat candidate action for when one wants to force the AI to do attacks that it would otherwise not do (for example on units that are 1 XP from leveling) or to execute certain attacks before all others (for example, attack with all &amp;quot;expendable&amp;quot; units first).  It is not set up to be a replacement for the default attacks.  Some notes:&lt;br /&gt;
&lt;br /&gt;
* The algorithm is different from and simpler than that of the default combat CA.  For example, no attack combinations are considered, only individual attacks.  As a result, the [[AiWML#List_of_AI_Aspects|default aspects]] (aggression, caution, [avoid], ...) are not taken into account.  Also, if lots of units are involved, evaluation of this Micro AI is potentially quite a bit slower than of the default attacks.&lt;br /&gt;
&lt;br /&gt;
* If an attack matching the Standard Unit Filters described below is found, this attack will always be executed, no matter how bad the odds are.  Thus, this Micro AI can be used to force the AI to do attacks it would otherwise avoid.&lt;br /&gt;
&lt;br /&gt;
* Other than selecting the attacker/defender units, this AI is currently not configurable.  Let us know if you would like to see certain options to be added.  (No guarantees that we will add them, but we will definitely consider them.)&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Simple Attack&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=simple_attack&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Simple Attack specific keys for the [micro_ai] tag===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  It is, however, in general not desirable to use this AI without at least one of the optional Standard Unit Filters below, as this AI is not meant as a replacement of the default combat candidate action.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=110000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 110,000, which means that it is executed right before the default combat candidate action.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Simple Attack action.  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units the AI should attack.  It defaults to all enemy units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
* '''weapon'''=-1: (integer) The number of the weapon that should be used for this attack.  Weapon numbers start at 1 (not 0).  -1 stands for &amp;quot;best weapon&amp;quot; (by whatever criterion the AI chooses to use).   '''Available from Wesnoth 1.11.8'''&lt;br /&gt;
&lt;br /&gt;
Below are 2 examples of Simple Attack Micro AI configurations from the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  The first one shows how to force the AI to attack with all Soulless and Walking Corpse units first:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110000&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless,Walking Corpse&lt;br /&gt;
             [/filter]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
The second example executes attacks by Soulless on all enemy units that are 1 XP from leveling.  Note that the Lua code in the preload event is there simply in order to code the &amp;quot;1 XP from advancing&amp;quot; condition.  It is not needed for most other SUFs, as can be seen in the previous example.&lt;br /&gt;
     [event]&lt;br /&gt;
         name=preload&lt;br /&gt;
         first_time_only=no&lt;br /&gt;
         [lua]&lt;br /&gt;
             code=&amp;lt;&amp;lt;&lt;br /&gt;
                 function close_to_advancing(unit)&lt;br /&gt;
                     if (unit.experience &amp;gt;= unit.max_experience-1) then&lt;br /&gt;
                         return true&lt;br /&gt;
                     else&lt;br /&gt;
                         return false&lt;br /&gt;
                     end&lt;br /&gt;
                 end&lt;br /&gt;
             &amp;gt;&amp;gt;&lt;br /&gt;
         [/lua]&lt;br /&gt;
     [/event]&lt;br /&gt;
 &lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110001&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless  # No Walking Corpses; L0 units don't advance enemy&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 lua_function = &amp;quot;close_to_advancing&amp;quot;&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
Several Simple Attack Micro AIs can be combined for the same AI side.  The two examples above are both used (note the different values for ca_score) in the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
== Custom Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|5}}&lt;br /&gt;
&lt;br /&gt;
From 1.13.5 onwards, adding a new Micro AI can be easily done without altering any core files or overriding the micro_ai tag. The [micro_ai] tag now looks up its ''ai_type'' parameter in the &amp;lt;code&amp;gt;wesnoth.micro_ais&amp;lt;/code&amp;gt; table and uses the micro AI setup function found there to determine which keys are required and allowed and what candidate actions to add.&lt;br /&gt;
&lt;br /&gt;
A micro AI setup function takes as its single parameter the contents of the [micro_ai] tag and returns three separate values:&lt;br /&gt;
*'''required''': A list of required keys and tags, as strings. Tags are indicated by enclosing them within square brackets in the string. If a key or tag in this list is missing from the [micro_ai] tag, an error will be printed.&lt;br /&gt;
*'''optional''': Similar to above, a list of optional keys and tags, as strings. No error will be printed if a key or tag in this list is missing, nor will an error be printed if it is present (as would be the case for unrecognized keys or tags).&lt;br /&gt;
*'''ca_params''': A table specifying the candidate actions that constitute the micro AI. It should contain an ''ai_id'' key (which specifies a base ID to be used for candidate actions of this micro AI) and a series of candidate action definitions. Each candidate action definition is a table containing the following keys:&lt;br /&gt;
**'''ca_id''': The ID used for the candidate action. This will be combined with the ''ai_id'' to produce the final ID.&lt;br /&gt;
**'''location''': Path to the Lua file that defines the candidate action.&lt;br /&gt;
**'''score''': The maximum score for the candidate action. If the [micro_ai] tag specified a ''ca_score'' parameter, the score here should normally be based on that.&lt;br /&gt;
&lt;br /&gt;
(The return statement would thus look like &amp;lt;syntaxhighlight lang='lua' inline&amp;gt;return required, optional, ca_params&amp;lt;/syntaxhighlight&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
'''Examples''' of micro AI setup functions can be found in the [https://github.com/wesnoth/wesnoth/tree/1.14/data/ai/micro_ais/mai-defs default MAI definitions directory].  See the [https://github.com/wesnoth/wesnoth/blob/1.14/data/ai/micro_ais/mai-defs/animals.lua#L4-L12 Big Animals MAI definition] for a simple example.  For custom micro AIs, the setup code should be put into a ''[lua]'' tag inside a preload event.&lt;br /&gt;
&lt;br /&gt;
Since the function has full access to the contents of the [micro_ai] tag, it can conditionally add candidate actions to '''ca_params''' depending on the values of other keys within the [micro_ai] tag. It can also perform other actions such as adding facets to an aspect; if it does such things, it should pay attention to the ''action'' key to determine whether it's being added or removed.&lt;br /&gt;
&lt;br /&gt;
There are two useful helper functions that can be used by Micro AI setup functions requiring aspect changes. They can be brought into scope by a line similar to the following:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='lua&amp;gt;local MAIH = wesnoth.require(&amp;quot;ai/micro_ais/micro_ai_helper.lua&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* MAIH.add_aspects(side, aspect_params)&lt;br /&gt;
* MAIH.delete_aspects(side, aspect_params)&lt;br /&gt;
&lt;br /&gt;
The ''aspect_params'' parameter is an array of tables, each of which contains the following keys:&lt;br /&gt;
*'''aspect''': The aspect to change.&lt;br /&gt;
*'''facet''': The WML defining the facet. This must include an ''id'' key in order for the aspect to be correctly deleted. For maximum compatibility, incorporate the ''ca_id'' key from the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=LuaAPI/ai&amp;diff=69667</id>
		<title>LuaAPI/ai</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=LuaAPI/ai&amp;diff=69667"/>
		<updated>2022-06-23T11:28:01Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* ai.aspects */ add allow_ally_villages&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;div class=&amp;quot;tright&amp;quot;&amp;gt; __TOC__ &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Code executed by [[Wesnoth AI]] components has access to the &amp;lt;tt&amp;gt;ai&amp;lt;/tt&amp;gt; module, which contains functions for controlling the AI and querying AI information. Thus, all functions on this page are only available to AI code.&lt;br /&gt;
&lt;br /&gt;
== Game State Queries ==&lt;br /&gt;
&lt;br /&gt;
=== ai.read_only ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''ai.read_only''' &amp;amp;rarr; ''boolean''&lt;br /&gt;
&lt;br /&gt;
The AI module operates in two modes: read-only, and read-write. Read-write mode is used in the execution function for stages and candidate actions, while read-only mode is used in goals and aspects and in the evaluation function for candidate actions. The '''read_only''' key allows you to distinguish these cases at runtime, if necessary. Functions that are only available in read-write mode will be tagged (mutable) in the descriptions on this page.&lt;br /&gt;
&lt;br /&gt;
=== ai.side ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''ai.side''' &amp;amp;rarr; ''number''&lt;br /&gt;
&lt;br /&gt;
The side number that the AI module is bound to.&lt;br /&gt;
&lt;br /&gt;
=== ai.aspects ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''ai.aspects'''.''aspect'' &amp;amp;rarr; ''value''&lt;br /&gt;
&lt;br /&gt;
Provides access to the aspects of the AI engine. This fetches the result of the aspect evaluation given the game state when it was last refreshed (see [[Wesnoth_AI_Framework#Some_more_on_the_invalidation_keys|aspect invalidation rules]]), not its definition. So, for example, '''ai.aspects.avoid''' returns the list of avoided hexes, not the &lt;br /&gt;
[[StandardLocationFilter|Standard Location Filter]] that produced them, and a [[#Dynamic_Lua_Aspects|dynamic Lua aspect]] will return the result of calling the function, not the function itself. For more details, see the definition of each aspect on [[AiWML]].&lt;br /&gt;
&lt;br /&gt;
The following aspects return scalar values (numbers, strings, or booleans):&lt;br /&gt;
* '''aggression'''&lt;br /&gt;
* '''allow_ally_villages''' {{DevFeature1.17|6}}&lt;br /&gt;
* '''caution'''&lt;br /&gt;
* '''grouping'''&lt;br /&gt;
* '''leader_aggression'''&lt;br /&gt;
* '''leader_ignores_keep'''&lt;br /&gt;
* '''leader_value'''&lt;br /&gt;
* '''passive_leader'''&lt;br /&gt;
* '''passive_leader_shares_keep'''&lt;br /&gt;
* '''recruitment_diversity'''&lt;br /&gt;
* '''recruitment_randomness'''&lt;br /&gt;
* '''retreat_enemy_weight'''&lt;br /&gt;
* '''retreat_factor'''&lt;br /&gt;
* '''scout_village_targeting'''&lt;br /&gt;
* '''simple_targeting'''&lt;br /&gt;
* '''support_villages'''&lt;br /&gt;
* '''village_value'''&lt;br /&gt;
* '''villages_per_scout'''&lt;br /&gt;
&lt;br /&gt;
The following aspects return tables:&lt;br /&gt;
&lt;br /&gt;
* '''advancements''' – the values of a [[LuaAPI/location_set|location set]] mapping locations to a list of possible advancements for the unit on that hex. To convert this to a fully functional standard  location set:&lt;br /&gt;
*:&amp;lt;syntaxhighlight lang=lua&amp;gt;&lt;br /&gt;
local location_set = wesnoth.require &amp;quot;location_set&amp;quot;&lt;br /&gt;
local ls = location_set.create()&lt;br /&gt;
ls.values = ai.aspects.advancements&lt;br /&gt;
-- Now you can use it as a regular location set&lt;br /&gt;
print(ls[{5,6}]) -- might print out {&amp;quot;Elvish Hero&amp;quot;, &amp;quot;Elvish Captain&amp;quot;} for example&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
*:{{DevFeature1.17|?}} It's now even simpler to convert:&amp;lt;syntaxhighlight lang=lua&amp;gt;&lt;br /&gt;
local location_set = wesnoth.require &amp;quot;location_set&amp;quot;&lt;br /&gt;
local ls = location_set.of_raw(ai.aspects.advancements)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* '''attacks''' – a table with keys '''own''' containing all units that may attack and '''enemy''' containing all units that may be attacked.&lt;br /&gt;
* '''avoid''' – an array of locations.&lt;br /&gt;
* '''leader_goal''' – the WML content of the leader_goal aspect&lt;br /&gt;
* '''recruitment_instructions''' – the WML content of the recruitment_instructions aspect&lt;br /&gt;
* '''recruitment_more''' – an array of strings where each string is a usage, a unit type ID, or an integer&lt;br /&gt;
* '''recruitment_pattern''' – an array of usage strings (see [[UnitTypeWML]] for an explanation of usage)&lt;br /&gt;
* '''recruitment_save_gold''' – the WML content of the recruitment_save_gold aspect&lt;br /&gt;
&lt;br /&gt;
=== ai.get_attacks ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''ai.get_attacks'''() &amp;amp;rarr; ''array of attack analyses''&lt;br /&gt;
&lt;br /&gt;
Get all possible attacks the AI is able to perform, and an analysis of each. If multiple AI units can attack the same enemy, all different combinations of AI units are listed. However, for a given combination of AI units, only one attack (the one the AI considers best) is listed. In other words, permutations of those units placed on different hexes are not included, as that would result in too long a list.&lt;br /&gt;
&lt;br /&gt;
Each attack analysis table has the following keys:&lt;br /&gt;
&lt;br /&gt;
* ''analysis''.'''rating'''() &amp;amp;rarr; ''numeric rating''&lt;br /&gt;
* ''analysis''.'''movements''' &amp;amp;rarr; ''array of location pairs''&lt;br /&gt;
** ''move''.'''src''' &amp;amp;rarr; ''location''&lt;br /&gt;
** ''move''.'''dst''' &amp;amp;rarr; ''location''&lt;br /&gt;
* ''analysis''.'''target''' &amp;amp;rarr; ''location''&lt;br /&gt;
* ''analysis''.'''target_value''' &amp;amp;rarr; ''number''&lt;br /&gt;
* ''analysis''.'''avg_losses''' &amp;amp;rarr; ''number''&lt;br /&gt;
* ''analysis''.'''chance_to_kill''' &amp;amp;rarr; ''probability''&lt;br /&gt;
* ''analysis''.'''avg_damage_inflicted''' &amp;amp;rarr; ''number''&lt;br /&gt;
* ''analysis''.'''target_starting_damage''' &amp;amp;rarr; ''integer''&lt;br /&gt;
* ''analysis''.'''avg_damage_taken''' &amp;amp;rarr; ''number''&lt;br /&gt;
* ''analysis''.'''resources_used''' &amp;amp;rarr; ''number''&lt;br /&gt;
* ''analysis''.'''terrain_quality''' &amp;amp;rarr; ''number''&lt;br /&gt;
* ''analysis''.'''alternative_terrain_quality''' &amp;amp;rarr; ''number''&lt;br /&gt;
* ''analysis''.'''vulnerability''' &amp;amp;rarr; ''number''&lt;br /&gt;
* ''analysis''.'''support''' &amp;amp;rarr; ''number''&lt;br /&gt;
* ''analysis''.'''leader_threat''' &amp;amp;rarr; ''boolean''&lt;br /&gt;
* ''analysis''.'''uses_leader''' &amp;amp;rarr; ''boolean''&lt;br /&gt;
* ''analysis''.'''is_surrounded''' &amp;amp;rarr; ''boolean''&lt;br /&gt;
&lt;br /&gt;
=== ai.get_targets ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''ai.get_targets'''() &amp;amp;rarr; ''array of targets''&lt;br /&gt;
&lt;br /&gt;
Get a list of all the current AI targets. Each target has the following keys:&lt;br /&gt;
&lt;br /&gt;
* '''loc''': The coordinates of the target, as a 2-element array.&lt;br /&gt;
* '''type''': The type of target, as a string.&lt;br /&gt;
* '''value''': The value of the target, as a real number.&lt;br /&gt;
&lt;br /&gt;
The possible target types are &amp;lt;syntaxhighlight lang='lua' inline&amp;gt;'village', 'leader', 'explicit', 'threat', 'battle aid', 'mass', 'support'&amp;lt;/syntaxhighlight&amp;gt;. More information on the meaning of each type can be found in the [[LuaAI#Lua_Goals_and_Targets|LuaAI documentation]].&lt;br /&gt;
&lt;br /&gt;
=== ai.suitable_keep ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''ai.suitable_keep'''(''unit'') &amp;amp;rarr; ''x'', ''y''&lt;br /&gt;
&lt;br /&gt;
This returns the location of the closest keep to the unit passed as argument.&lt;br /&gt;
&lt;br /&gt;
== Move Maps ==&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''ai.get_dst_src'''() &amp;amp;rarr; ''move map''&lt;br /&gt;
* {{LuaGameOnly}} '''ai.get_src_dst'''() &amp;amp;rarr; ''move map''&lt;br /&gt;
* {{LuaGameOnly}} '''ai.get_enemy_dst_src'''() &amp;amp;rarr; ''move map''&lt;br /&gt;
* {{LuaGameOnly}} '''ai.get_enemy_src_dst'''() &amp;amp;rarr; ''move map''&lt;br /&gt;
&lt;br /&gt;
These functions return move maps – tables that relate the current positions of units with the hexes they can move to. A move map is a [[LuaAPI/location_set|location set]], similar to the advancements map, which can be converted to a standard location set with the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=lua&amp;gt;&lt;br /&gt;
local location_set = wesnoth.require &amp;quot;location_set&amp;quot;&lt;br /&gt;
local ls = location_set.create()&lt;br /&gt;
ls.values = ai.get_dst_src()&lt;br /&gt;
-- Now you can use it as a regular location set&lt;br /&gt;
print(ls[{1,4}]) -- prints out something like {{5,4},{5,3}} or the like&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
{{DevFeature1.17|?}} It's now even simpler to convert:&amp;lt;syntaxhighlight lang=lua&amp;gt;&lt;br /&gt;
local location_set = wesnoth.require &amp;quot;location_set&amp;quot;&lt;br /&gt;
local ls = location_set.of_raw(ai.get_dst_src())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These functions are defined in ''ai/lua/stdlib.lua'', which is loaded by default if you allow the game to implicitly define a Lua [engine] tag. However, if you require a custom Lua [engine] tag, it must load this file manually with the following code in order to use these functions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang='lua'&amp;gt;&lt;br /&gt;
local ai_stdlib = wesnoth.require('ai/lua/stdlib.lua')&lt;br /&gt;
ai_stdlib.init(ai)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''ai.recalculate_move_maps'''()&lt;br /&gt;
* {{LuaGameOnly}} '''ai.recalculate_enemy_move_maps'''()&lt;br /&gt;
&lt;br /&gt;
Force the move maps to be recalculated. This might be necessary if you've moved units around using '''wesnoth.units.to_map'''.&lt;br /&gt;
&lt;br /&gt;
== AI Actions ==&lt;br /&gt;
&lt;br /&gt;
All these functions instruct the AI to check or perform a specific action, and return a result table indicating the success of the action. The table has the following keys:&lt;br /&gt;
&lt;br /&gt;
* '''gamestate_changed''': a boolean indicating whether anything actually changed as a result of the action&lt;br /&gt;
* '''ok''': a boolean indicating the success of the action&lt;br /&gt;
* '''status''': a numeric status code indicating why the action failed&lt;br /&gt;
* '''result''': a string error code indicating why the action failed&lt;br /&gt;
&lt;br /&gt;
=== ai.attack ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} (mutable) '''ai.attack'''(''attacker'', ''defender'', ''weapon'', [''aggression'']) &amp;amp;rarr; ''result''&lt;br /&gt;
&lt;br /&gt;
Execute attack by ''attacker'' against ''defender''. If ''weapon'' is provided, it is the number of the weapon to be used (count starts at 1, not 0, as always in Lua), otherwise the choice of the weapon is left to the AI engine.  If ''aggression'' is provided, it is used to influence the choice of the best weapon.  Obviously, this only makes sense if this choice is left to the engine, that is, if ''weapon'' is set to either 'nil' or '-1'.&lt;br /&gt;
&lt;br /&gt;
=== ai.check_attack ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''ai.check_attack'''(''attacker'', ''defender'', ''weapon'', [''aggression'']) &amp;amp;rarr; ''result''&lt;br /&gt;
&lt;br /&gt;
Similar to '''ai.attack''', but does not execute the attack – it merely checks if it would succeed.&lt;br /&gt;
&lt;br /&gt;
=== ai.move ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} (mutable) '''ai.move'''(''unit'', ''to'') &amp;amp;rarr; ''result''&lt;br /&gt;
&lt;br /&gt;
Execute a &amp;quot;partial&amp;quot; move of ''unit'' to hex (''to.x'', ''to.y''). A &amp;quot;partial&amp;quot; move means that the unit keeps whatever movement points it has left after the move.&lt;br /&gt;
&lt;br /&gt;
=== ai.move_full ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} (mutable) '''ai.move_full'''(''unit'', ''to'') &amp;amp;rarr; ''result''&lt;br /&gt;
&lt;br /&gt;
Execute a &amp;quot;full&amp;quot; move of ''unit'' to hex (''to.x'', ''to.y''). A &amp;quot;full&amp;quot; move means that the unit's movement points are set to zero at the end of the move.&lt;br /&gt;
&lt;br /&gt;
=== ai.check_move ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''ai.check_move'''(''unit'', ''to'') &amp;amp;rarr; ''result''&lt;br /&gt;
&lt;br /&gt;
Similar to '''ai.move''' or '''ai.move_full''', but does not execute the move – it merely checks if it would succeed.&lt;br /&gt;
&lt;br /&gt;
=== ai.recall ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} (mutable) '''ai.recall'''(''unit_id'', [''location'']) &amp;amp;rarr; ''result''&lt;br /&gt;
&lt;br /&gt;
Recall the unit with id ''unit_id''.  An optional recruit location can be given.  If the location is omitted, a suitable hex is automatically chosen by the AI engine.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.17|0}} The location may be specified as an object (for example, &amp;lt;tt&amp;gt;{x,y}&amp;lt;/tt&amp;gt; or a unit) rather than separate ''x'' and ''y'' parameters.&lt;br /&gt;
&lt;br /&gt;
=== ai.check_recall ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''ai.check_recall'''(''unit_id'', [''location'']) &amp;amp;rarr; ''result''&lt;br /&gt;
&lt;br /&gt;
Similar to '''ai.recall''', but does not execute the recall – it merely checks if it would succeed.&lt;br /&gt;
&lt;br /&gt;
=== ai.recruit ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} (mutable) '''ai.recruit'''(''unit_type'', [''location'']) &amp;amp;rarr; ''result''&lt;br /&gt;
&lt;br /&gt;
Recruit a unit of type ''unit_type''. An optional recruit location can be given.  If the location is omitted, a suitable hex is automatically chosen by the AI engine.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.17|0}} The location may be specified as an object (for example, &amp;lt;tt&amp;gt;{x,y}&amp;lt;/tt&amp;gt; or a unit) rather than separate ''x'' and ''y'' parameters.&lt;br /&gt;
&lt;br /&gt;
=== ai.check_recruit ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''ai.check_recruit'''(''unit_type'', [''location'']) &amp;amp;rarr; ''result''&lt;br /&gt;
&lt;br /&gt;
Similar to '''ai.recruit''', but does not execute the recruit – it merely checks if it would succeed.&lt;br /&gt;
&lt;br /&gt;
=== ai.stopunit_attacks ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} (mutable) '''ai.stopunit_attacks'''(''unit'') &amp;amp;rarr; ''result''&lt;br /&gt;
&lt;br /&gt;
Remove remaining attacks from ''unit''.  This is equivalent to setting &amp;lt;code&amp;gt;attacks_left=0&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== ai.stopunit_moves ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} (mutable) '''ai.stopunit_moves'''(''unit'') &amp;amp;rarr; ''result''&lt;br /&gt;
&lt;br /&gt;
Remove remaining movement points from ''unit''.  This is equivalent to setting &amp;lt;code&amp;gt;moves=0&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== ai.stopunit_all ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} (mutable) '''ai.stopunit_all'''(''unit'') &amp;amp;rarr;&lt;br /&gt;
&lt;br /&gt;
Remove both remaining attacks and remaining movement points from ''unit''.&lt;br /&gt;
&lt;br /&gt;
=== ai.check_stopunit ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''ai.check_stopunit'''(''unit'') &amp;amp;rarr; ''result''&lt;br /&gt;
&lt;br /&gt;
Similar to '''ai.stopunit_attacks''', '''ai.stopunit_moves''', or '''ai.stopunit_all''', but does not execute the stop – it merely checks if it would succeed.&lt;br /&gt;
&lt;br /&gt;
=== ai.fallback_human ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} (mutable) '''ai.fallback_human'''()&lt;br /&gt;
&lt;br /&gt;
Hands over control of this side to the local human player until the end of the current turn. This action does not return a result; it always succeeds.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
&lt;br /&gt;
There are two other functions that are not in the AI module but are important for AI coding:&lt;br /&gt;
&lt;br /&gt;
* [[LuaAPI/wesnoth/sides#wesnoth.sides.debug_ai|wesnoth.sides.debug_ai]]&lt;br /&gt;
* [[LuaAPI/wesnoth/sync#wesnoth.sync.invoke_command|wesnoth.sync.invoke_command]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Lua Reference]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AiWML&amp;diff=69662</id>
		<title>AiWML</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AiWML&amp;diff=69662"/>
		<updated>2022-06-22T23:20:03Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* List of AI Aspects */ add allow_ally_villages aspect&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{WML Tags}}&lt;br /&gt;
&lt;br /&gt;
== Customizing the Default AI Using WML ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
This page contains:&lt;br /&gt;
* Descriptions of the aspects and goals of the default AI&lt;br /&gt;
* Instructions for setting them up at the beginning of the scenario in a [side] tag&lt;br /&gt;
&lt;br /&gt;
This page does '''not''' contain:&lt;br /&gt;
* Instructions for [[LuaAI#Dynamic_Lua_Aspects|setting up aspects dynamically using Lua]].&lt;br /&gt;
* Instructions for [[Modifying_AI_Components#Modifying_Standard_Aspects|changing aspects and goals mid scenario]], although for many of them (for example, for all [[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Aspects|standard aspects]]) the methods are almost identical&lt;br /&gt;
* Instructions for some of the [[Modifying_AI_Components|more complex AI customization tasks]].&lt;br /&gt;
* Anything about [[Micro_AIs|MicroAIs]]&lt;br /&gt;
* [[AI_Recruitment|Advanced recruitment details]]&lt;br /&gt;
&lt;br /&gt;
== Defining Aspects and Goals of the RCA AI ==&lt;br /&gt;
&lt;br /&gt;
[[RCA_AI#Use_of_Aspects_in_the_RCA_AI|Aspects]] and [[RCA_AI#Use_of_Goals_and_Targets_in_the_RCA_AI|goals]] of the [[RCA_AI|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 [[#List_of_AI_Aspects|aspects themselves]] as arguments, as well as the following general keys:&lt;br /&gt;
* '''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 [https://github.com/wesnoth/wesnoth/blob/master/data/core/macros/schedules.cfg 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.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
* '''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. {{DevFeature1.13|5}} In 1.13, the meaning of '''ai_algorithm''' has changed and no longer selects a native AI that's built into the engine, as the RCA AI has now become the core system and no alternatives exist anymore. Instead, it selects a predefined ''[ai]'' definition, such as those defined in ''data/ai/ais''. Since these are defined in WML, you can easily build your own.&lt;br /&gt;
&lt;br /&gt;
An example of a simple use of the [ai] tag to set AI parameters (aspects) is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=wml&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    time_of_day=dusk&lt;br /&gt;
    aggression=0.99&lt;br /&gt;
    caution=0.01&lt;br /&gt;
    [avoid]&lt;br /&gt;
        [filter]&lt;br /&gt;
            side=2,3&lt;br /&gt;
        [/filter]&lt;br /&gt;
        radius=2&lt;br /&gt;
    [/avoid]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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 &amp;lt;code&amp;gt;:inspect&amp;lt;/code&amp;gt; in debug mode to verify whether an aspect was changed successfully.&lt;br /&gt;
&lt;br /&gt;
==List of AI Aspects==&lt;br /&gt;
&lt;br /&gt;
'''Note:''' Only one instance of each aspect will be active for a side at any time. Where multiple instances of the same aspect are included in an [ai] tag, the last whose conditions (time of day, turns, etc.) match will be the only one that applies.&lt;br /&gt;
&lt;br /&gt;
* '''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 &amp;lt;u&amp;gt;only&amp;lt;/u&amp;gt; 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:'''&lt;br /&gt;
** This is a simplified version of the more complex (and arguably more useful) application of a dynamic Lua ''advancements'' aspect, as described [[LuaAI#The_advancements_Aspect|here]].&lt;br /&gt;
** The ''advancements'' aspect only takes effect during the AI turn, that is, on offense only, not on defense.&lt;br /&gt;
** 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).&lt;br /&gt;
&lt;br /&gt;
* '''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.  The description below applies as written to the ''combat'' CA only, but aggression is taken into account by the ''high_xp_attack'' and ''spread_poison'' CAs as well (as of '''Wesnoth 1.13.5''' and '''1.15.3''' respectively).&lt;br /&gt;
** '''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.&lt;br /&gt;
** 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 &amp;gt;0 is found.  The combat CA score is zero if the highest attack score is &amp;lt;=0).&lt;br /&gt;
** 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.&lt;br /&gt;
** All the negative contributions to the score are multiplied by '(1-aggression)'.  This means that:&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
** 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.&lt;br /&gt;
&lt;br /&gt;
*'''allow_ally_villages'''=no (bool) {{DevFeature1.17|6}} Affects ''retreat-injured'' CA only. Normally, the AI is not allowed to take villages from an ally. If this is set to 'yes', the AI may retreat injured units to allied villages (and thereby capture them).&lt;br /&gt;
&lt;br /&gt;
*'''[attacks]''': Filters the units considered for combat, both on the AI and the enemy sides.  Applies to the ''combat'', ''high_xp_attacks'' and ''spread_poison'' CAs only.  It cannot be set in the same way as the other aspects and is therefore described in a [[#Filtering_Combat_with_the_attacks_Aspect|separate section]] below.&lt;br /&gt;
&lt;br /&gt;
* '''[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''.&lt;br /&gt;
** '''[[StandardLocationFilter]]''': The locations for the AI to avoid.  Do not use a [filter_location] tag.&lt;br /&gt;
**'''Note:''' Only one instance of an [avoid] aspect will be active for a side at any time. Where multiple instances are included in an [ai] tag, the last whose conditions (time of day, turns, etc.) match will be the only one that applies.&lt;br /&gt;
&lt;br /&gt;
* '''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 [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|evaluation scores]]):&lt;br /&gt;
** ''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].&lt;br /&gt;
** ''Combat'' CA: &lt;br /&gt;
*** 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 &amp;quot;exposure&amp;quot; rating of the units.  This exposure is then subtracted from the 'attack score' as described for ''aggression'' above.&lt;br /&gt;
*** 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.)&lt;br /&gt;
*** If the AI leader is used in an attack, the AI ''always'' uses 'caution=2' for the evaluation of that attack.&lt;br /&gt;
** ''Retreat'' CA: {{DevFeature1.15|3}} This CA has been removed.&lt;br /&gt;
*** 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 &amp;lt;tt&amp;gt;caution &amp;amp;times; their_power &amp;gt; our_power&amp;lt;/tt&amp;gt;. 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.&lt;br /&gt;
*** 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. &lt;br /&gt;
*** The AI never retreats if caution is set to 0 or lower.&lt;br /&gt;
** ''Move-to-targets'' CA:&lt;br /&gt;
*** 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. &amp;quot;Dangerous&amp;quot; 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 &amp;lt;tt&amp;gt;our_strength / their_strength &amp;gt; 0.5 + caution&amp;lt;/tt&amp;gt;. 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.&lt;br /&gt;
***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.&lt;br /&gt;
*** The AI also considers retreating units during the move-to-target phase based on criteria similar to those for the retreat CA.&lt;br /&gt;
&lt;br /&gt;
* '''grouping'''=&amp;quot;offensive&amp;quot;: (string) How the AI should try to group units.  Applies to ''move-to-targets'' CA only.  Possible values:&lt;br /&gt;
** ''offensive'': Makes the AI try to group units together before attacking.&lt;br /&gt;
** ''defensive'': Makes the AI group units together very conservatively, only advancing them much beyond its castle if it has overwhelming force.&lt;br /&gt;
** ''no'': Makes the AI use no grouping behavior.&lt;br /&gt;
&lt;br /&gt;
* '''leader_aggression'''=&amp;quot;-4.0&amp;quot;: 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''.&lt;br /&gt;
&lt;br /&gt;
* '''[leader_goal]'''=&amp;quot;&amp;quot;: Makes the AI try to move its leader to a specific location.  Applies to ''move-leader-to-goals'' CA only.&lt;br /&gt;
** '''x''', '''y''': The location toward which the AI should move its leader.&lt;br /&gt;
** '''auto_remove'''=no: (bool)  If 'no' (default), the AI moves the leader to the goal, after which he stays there until [leader_goal] is [[Modifying_AI_Components#Modifying_Standard_Aspects|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).&lt;br /&gt;
** '''id'''=&amp;quot;&amp;quot;: (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] [[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Aspects|facet]].  Thus, in principle for this to work, you need to set the id of the facet as described [[Modifying_AI_Components#Modifying_Standard_Aspects|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. {{DevFeature1.13|5}} 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.&lt;br /&gt;
**'''max_risk'''=1-caution: (double: meaningful values are &amp;gt;=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.&lt;br /&gt;
&lt;br /&gt;
* '''leader_ignores_keep'''=no: (bool) Set to 'yes' for this aspect to apply to all leaders of a side. {{DevFeature1.15|3}} In addition to 'yes/no', the aspect can now also be set to a comma-separated list of unit ids. The aspect then only applies to leaders with ids matching an id on that list. Setting leader_ignores_keep for some or all leaders means that:&lt;br /&gt;
** The affected 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). Thus, these leaders behave in the same way as any other unit of the AI side.&lt;br /&gt;
** This also means that these 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.&lt;br /&gt;
&lt;br /&gt;
* '''leader_value'''=3: (double) A number 0 or higher which determines the value of enemy leaders as [[#AI_Targets_and_Goals|targets]].  Affects ''move-to-targets CA'' only (and therefore specifically does not apply to attacks).&lt;br /&gt;
&lt;br /&gt;
* '''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''. {{DevFeature1.15|3}} In addition to 'yes/no', the aspect can now also be set to a comma-separated list of unit ids. The aspect then only applies to leaders with ids matching an id on that list.&lt;br /&gt;
&lt;br /&gt;
* '''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. {{DevFeature1.15|3}} In addition to 'yes/no', the aspect can now also be set to a comma-separated list of unit ids. The aspect then only applies to leaders with ids matching an id on that list.&lt;br /&gt;
&lt;br /&gt;
*'''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 [[AI_Recruitment|separate page]].&lt;br /&gt;
&lt;br /&gt;
*'''retreat_factor'''=0.25 (double) {{DevFeature1.15|11}} Affects ''retreat-injured'' CA only. Determines how injured AI units have to be before they attempt retreating. This is approximately the fraction of the units' hitpoints, but other factors play a role also, such as the terrain the units are on, and leaders and poisoned units retreat somewhat earlier. Setting this to 0 (or negative values) disables retreating. Note that even setting this to very large values still requires units to be somewhat injured before they start retreating.&lt;br /&gt;
&lt;br /&gt;
*'''retreat_enemy_weight'''=1.0 (double) {{DevFeature1.15|11}} Affects ''retreat-injured'' CA only. When deciding where to retreat to, the AI considers how much healing a location provides and weighs it against the threat of all the enemies that can get there (balanced by the own units that can get there). In this evaluation, it multiplies the enemy threat rating by ''retreat_enemy_weight'', meaning that the larger its value is, the more a unit tries to move away from enemies.  There are three regimes for this aspect:&lt;br /&gt;
** Positive values: Only locations that provide healing (both from terrain and from adjacent healers which have no MP left) are considered. For regenerating units, all hexes are treated as if they provide healing.&lt;br /&gt;
** Zero: Enemy threats are ignored&lt;br /&gt;
** Negative values: All hexes are considered for retreating. The enemy threat rating is multiplied by the absolute value of the aspect value. Thus, if very negative values are used (e.g. -100), this can be used to make units run away from enemies with little consideration for anything else.&lt;br /&gt;
&lt;br /&gt;
* '''scout_village_targeting'''=3: (double) The AI multiplies the value of village [[#AI_Targets_and_Goals|targets]] for scouts by this value.  Affects ''move-to-targets'' CA only.&lt;br /&gt;
&lt;br /&gt;
* '''simple_targeting'''=no: (bool) If 'yes', the AI moves its units toward [[#AI_Targets_and_Goals|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.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
* '''village_value'''=1: (double) A number 0 or higher which determines how much the AI tries to go for villages as [[#AI_Targets_and_Goals|targets]].  Affects ''move-to-targets'' CA only.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
=== Removed AI Aspects ===&lt;br /&gt;
&lt;br /&gt;
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. {{DevFeature1.13|5}} Except for attack_depth, these have now all been removed.  {{DevFeature1.15|0}} attack_depth is now removed as well.&lt;br /&gt;
&lt;br /&gt;
* '''attack_depth'''=5: (int)&lt;br /&gt;
* '''number_of_possible_recruits_to_force_recruit'''=3.1: (double)&lt;br /&gt;
* '''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.&lt;br /&gt;
** It is, in principle, still possible to use this aspect together with the [[Wesnoth_AI_Framework#Recruitment_Stage|recruitment stage]], but this is discouraged for the reasons given at this link.&lt;br /&gt;
* '''recruitment_ignore_bad_combat'''=no: (bool)&lt;br /&gt;
* '''recruitment_ignore_bad_movement'''=no: (bool)&lt;br /&gt;
&lt;br /&gt;
==AI Targets and Goals==&lt;br /&gt;
&lt;br /&gt;
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 [[RCA_AI#RCA_AI_Aspect_and_Goal_Configuration|base values]].  Additional targets can be defined using the [goal] tag.&lt;br /&gt;
&lt;br /&gt;
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 [[RCA_AI#Use_of_Goals_and_Targets_in_the_RCA_AI|here]].  The sections below only describe how to set up goals in order to add targets for the ''move-to-targets'' CA.&lt;br /&gt;
&lt;br /&gt;
===The [goal] Tag===&lt;br /&gt;
&lt;br /&gt;
The [goal] tag defines units or locations as [[#AI_Targets_and_Goals|move targets]] (not attack targets!) for the AI.  '''Applies to ''move-to-targets'' CA only'''. &amp;lt;-- That is '''extremely important''' to understand, in particular the fact that the [goal] tag has '''no influence whatsoever on attacks'''.&lt;br /&gt;
&lt;br /&gt;
 The following keys/tags can be used:&lt;br /&gt;
* '''name'''=&amp;quot;target&amp;quot;: (string)  The following values are possible and result in different types of targets, as shown in the examples below:&lt;br /&gt;
** ''target'':  The (default) target goal specifies target units (not necessarily enemy units) toward which the AI should move its units. {{DevFeature1.13|5}} target_unit is now a synonym for this.&lt;br /&gt;
** ''target_location'': Specifies target locations toward which the AI should move its units.&lt;br /&gt;
** ''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.&lt;br /&gt;
** ''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.&lt;br /&gt;
** ''protect_my_unit'': ('''deprecated''') &amp;lt;s&amp;gt;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.&amp;lt;/s&amp;gt;&lt;br /&gt;
** ''lua_goal'': A dynamically calculated goal written in Lua. See [[LuaAI#Lua_Goals_and_Targets|here]] for details on how this works.&lt;br /&gt;
&lt;br /&gt;
* '''[criteria]'''=&amp;quot;&amp;quot;: Contains a [[StandardUnitFilter]] (for ''target'', ''protect_unit'' or ''protect_my_unit'') or [[StandardLocationFilter]] (for ''target_location'' or ''protect_location'') describing the targets.&lt;br /&gt;
&lt;br /&gt;
* '''value'''=0: (double) The value of the goal.&lt;br /&gt;
&lt;br /&gt;
* '''protect_radius'''=20: (int) The protection radius.  Applies to ''protect_location'', ''protect_unit'' and ''protect_my_unit''.&lt;br /&gt;
&lt;br /&gt;
* '''id'''=&amp;quot;&amp;quot;: (string)  Optional ID used for [[Modifying_AI_Components#Modifying_Goals|modifying the goal]].&lt;br /&gt;
&lt;br /&gt;
===Examples of [goal] Tag Usage===&lt;br /&gt;
&lt;br /&gt;
'''target:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        [criteria] #NOTE: this is a SUF, because we're targeting a unit&lt;br /&gt;
            side=3&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''target_location:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=target_location&lt;br /&gt;
        [criteria] #NOTE: this is a SLF, because we're targeting a location&lt;br /&gt;
            x,y=42,20&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''protect_location:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=protect_location&lt;br /&gt;
        [criteria] #NOTE: this is a SLF, because we're protecting a location&lt;br /&gt;
            x,y=42,20&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        protect_radius=16&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''protect_unit:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=protect_unit&lt;br /&gt;
        [criteria] #NOTE: this is a SUF, because we're protecting a unit&lt;br /&gt;
            side=3&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        protect_radius=16&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''How to use protect_unit to protect the own leader''' (We are side 2)&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=protect_unit&lt;br /&gt;
        [criteria]&lt;br /&gt;
            side=2&lt;br /&gt;
            canrecruit=yes&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        protect_radius=8&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Deprecated AI Targeting Aspects ===&lt;br /&gt;
&lt;br /&gt;
The following AI targeting parameters currently still work, but have been superseded by the [[#The_.5Bgoal.5D_Tag|[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.&lt;br /&gt;
&lt;br /&gt;
* '''[target]'''=&amp;quot;&amp;quot;: '''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.&lt;br /&gt;
** '''[[StandardUnitFilter]]''': Do not use a [filter] tag.&lt;br /&gt;
** '''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.&lt;br /&gt;
&lt;br /&gt;
* '''[protect_location]'''=&amp;quot;&amp;quot;: '''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.&lt;br /&gt;
** '''x''', '''y''': Standard coordinates. These indicate the location the AI is protecting.&lt;br /&gt;
** '''radius''': The radius around it to protect (0 indicates a single hex). {{DevFeature1.13|5}} This key has been changed to protect_radius, though it's preferred to change old code to use the [goal] tag instead.&lt;br /&gt;
** '''value''': The importance of protecting this location.&lt;br /&gt;
&lt;br /&gt;
* '''[protect_unit]'''=&amp;quot;&amp;quot;: '''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.&lt;br /&gt;
** '''[[StandardUnitFilter]]''': The unit(s) to protect. Do not use a [filter] tag.&lt;br /&gt;
** '''radius''': The radius around it to protect (0 indicates a single hex). {{DevFeature1.13|5}} This key has been changed to protect_radius, though it's preferred to change old code to use the [goal] tag instead.&lt;br /&gt;
** '''value''': The importance of protecting this unit.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
== Filtering which Units Participate in which Actions==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}}&lt;br /&gt;
&lt;br /&gt;
Each [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|candidate action of the default AI]] can be restricted to apply to only a subset of the AI units. This is done by adding a '''[filter_own]''' tag to the '''[[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Candidate_Actions|[candidate_action]]]''' tag definition.&lt;br /&gt;
&lt;br /&gt;
Typical use cases:&lt;br /&gt;
* Restricting an action to certain units so that other units are not used to this. For example, village grabbing could be restricted to scout units only, so that slower units do not take detours on their way toward the enemies. See the example below.&lt;br /&gt;
* Excluding units from a certain action in order to have them available for other actions afterward. For example, one might want to add a custom candidate action which moves healers behind injured units (or use the existing [[Micro_AIs|Micro AI]] for this). Ideally, this should happen after ''all'' other units have moved, specifically also after the move-to-targets candidate action (MtT CA) is done. In the default setting, however, the MtT CA would also move the healers. Thus, we would want to exclude them using a '''[filter_own]''' tag.&lt;br /&gt;
* Adding two instances of the same candidate action with different scores to force the order in which units participate in this action. For example, one could add another instance of the move-leader-to-keep CA with a '''[filter_own]''' tag restricting it to one leader, in order to make this leader move to a keep (and therefore recruit) first.&lt;br /&gt;
&lt;br /&gt;
Note that for the combat AI, using the attacks aspect to filter units as described in the next section '''is slightly more efficient'''. Thus, that should be the preferred method if there are many units on the map.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[candidate_action]&lt;br /&gt;
	id=villages&lt;br /&gt;
	engine=cpp&lt;br /&gt;
	name=ai_default_rca::get_villages_phase&lt;br /&gt;
	max_score=60000&lt;br /&gt;
	score=60000&lt;br /&gt;
	[filter_own]&lt;br /&gt;
		type=Wolf Rider&lt;br /&gt;
	[/filter_own]&lt;br /&gt;
[/candidate_action]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Filtering Combat with the ''attacks'' Aspect==&lt;br /&gt;
&lt;br /&gt;
The ''attacks'' aspect lets us filter the units considered by the ''combat'', ''high_xp_attacks'' and ''spread_poison'' 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&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [aspect]&lt;br /&gt;
        id=attacks&lt;br /&gt;
        [facet]&lt;br /&gt;
            invalidate_on_gamestate_change=yes&lt;br /&gt;
            [filter_own]&lt;br /&gt;
                type=Elvish Sorceress,Elvish Enchantress,Elvish Sylph&lt;br /&gt;
            [/filter_own]&lt;br /&gt;
            [filter_enemy]&lt;br /&gt;
                race=undead&lt;br /&gt;
            [/filter_enemy]&lt;br /&gt;
        [/facet]&lt;br /&gt;
    [/aspect]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Several important notes:&lt;br /&gt;
* The ''attacks'' aspect is a ''composite'' aspect and cannot be defined using the simple syntax of the other ''standard'' aspects.  See [[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Aspects|here]] for a description of the syntax of the example.&lt;br /&gt;
* This only works if &amp;lt;code&amp;gt;invalidate_on_gamestate_change=yes&amp;lt;/code&amp;gt; is 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 [[Wesnoth_AI_Framework#Some_more_on_the_invalidation_keys|here]].&lt;br /&gt;
* If [filter_own] or [filter_enemy] are omitted, the selection defaults to all units of the respective sides.&lt;br /&gt;
* '''Most importantly''': The above example results in sorceress-line vs undead attacks being the &amp;lt;u&amp;gt;only&amp;lt;/u&amp;gt; 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:&lt;br /&gt;
** Setting up the [filter_own] or [filter_enemy] [[StandardUnitFilter]]s to adapt dynamically to the situation on the map.&lt;br /&gt;
** 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_AIs#Micro_AI_Test_and_Demo_Scenarios|Micro AI test scenario]] [https://github.com/wesnoth/wesnoth/blob/master/data/ai/micro_ais/scenarios/protect_unit.cfg ''Protect Unit''].&lt;br /&gt;
** Using [[LuaAI#Dynamic_Lua_Aspects|dynamic Lua aspects]] is in principle also possible, but far from easy due to the complex tables this aspect returns.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|5}}&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [attacks]&lt;br /&gt;
        invalidate_on_gamestate_change=yes&lt;br /&gt;
        [filter_own]&lt;br /&gt;
            type=Elvish Sorceress,Elvish Enchantress,Elvish Sylph&lt;br /&gt;
        [/filter_own]&lt;br /&gt;
        [filter_enemy]&lt;br /&gt;
            race=undead&lt;br /&gt;
        [/filter_enemy]&lt;br /&gt;
    [/attacks]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category: WML Reference]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Experimental_AI&amp;diff=69661</id>
		<title>Experimental AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Experimental_AI&amp;diff=69661"/>
		<updated>2022-06-22T23:14:55Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Recruitment */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Experimental AI Summary ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} Several of the Experimental AI candidate actions (CAs) '''have been merged into the [[RCA_AI|default (RCA) AI]]''', see descriptions below for the individual CAs.&lt;br /&gt;
&lt;br /&gt;
An ''Experimental AI'' is available for use in both MP maps and SP scenarios.  At the moment, this AI contains the following [[Wesnoth_AI_Framework#The_Candidate_Actions_of_the_main_loop_Stage|candidate actions]]:&lt;br /&gt;
* New and improved recruiting&lt;br /&gt;
* More aggressive village grabbing&lt;br /&gt;
* Healer placement behind injured units and more assertive retreating of injured units to healing locations.&lt;br /&gt;
* Leader castle switching: useful on certain MP maps&lt;br /&gt;
* Poison spreading&lt;br /&gt;
* Moving toward any enemy, if there are no enemy leaders or villages&lt;br /&gt;
* All the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|default RCA AI candidate actions]] except for recruiting&lt;br /&gt;
&lt;br /&gt;
== Experimental AI Setup ==&lt;br /&gt;
&lt;br /&gt;
In multiplayer maps, this AI is available from the game setup menu as 'Experimental AI'.  In single-player scenarios, it can be included by using the following code in a [side] tag&lt;br /&gt;
 [ai]&lt;br /&gt;
     # Deprecated version (for Wesnoth 1.14 and earlier)&lt;br /&gt;
     {EXPERIMENTAL_AI}&lt;br /&gt;
 [/ai]&lt;br /&gt;
{{DevFeature1.15|0}} The EXPERIMENTAL_AI Macro is deprecated now. The Experimental AI should be included as follows instead (this works in Wesnoth 1.14 already):&lt;br /&gt;
 [ai]&lt;br /&gt;
     # Current version (works from Wesnoth 1.14 on)&lt;br /&gt;
     ai_algorithm=experimental_ai&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Note that the EXPERIMENTAL_AI macro must be inside &amp;lt;code&amp;gt;[side][ai]&amp;lt;/code&amp;gt;, it does not work with &amp;lt;code&amp;gt;[modify_side]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[modify_ai]&amp;lt;/code&amp;gt;. If the &amp;lt;code&amp;gt;ai_algorithm&amp;lt;/code&amp;gt; version is used, the Experimental AI can also be added as&lt;br /&gt;
     [modify_side]&lt;br /&gt;
         side=2&lt;br /&gt;
         [ai]&lt;br /&gt;
             ai_algorithm=experimental_ai&lt;br /&gt;
         [/ai]&lt;br /&gt;
     [/modify_side]&lt;br /&gt;
or&lt;br /&gt;
     [modify_side]&lt;br /&gt;
         side=2&lt;br /&gt;
         switch_ai=ai/ais/ai_generic_rush.cfg  # Wesnoth 1.14 and earlier&lt;br /&gt;
         #switch_ai=ai/ais/ai_experimental.cfg # Wesnoth 1.15 and later&lt;br /&gt;
     [/modify_side]&lt;br /&gt;
&lt;br /&gt;
=== Custom Parameter Setup ===&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|2}}&lt;br /&gt;
A small number of custom parameters can be passed to the Experimental AI. These cannot be set up using the &amp;lt;code&amp;gt;ai_algorithm&amp;lt;/code&amp;gt; syntax, but require the use of a macro in the &amp;lt;code&amp;gt;[side][ai]&amp;lt;/code&amp;gt; tag:&lt;br /&gt;
 [side]&lt;br /&gt;
     [ai]&lt;br /&gt;
         {CUSTOMIZABLE_EXPERIMENTAL_AI (&lt;br /&gt;
             high_level_fraction=0.33&lt;br /&gt;
             randomness=0.2&lt;br /&gt;
         )}&lt;br /&gt;
     [/ai]&lt;br /&gt;
 [/side]&lt;br /&gt;
Note that only the parameters one wants changed need to be defined in the macro, those omitted will take their default values.&lt;br /&gt;
&lt;br /&gt;
'''Custom parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''high_level_fraction'''=0: (non-negative number) The approximate fraction of units of level 2 or higher to be recruited. This is defined as fraction of units on the map, not of new units being recruited during a turn or over several turns (which makes a difference if there are already units on the map). The effect is also cumulative per level (starting from level 2), meaning that 1/3 of L2 units will be recruited in the example above, 1/3*1/3=1/9 L3s etc. The default value is zero, which leaves the high-level unit recruiting to the default Experimental AI algorithm resulting usually in very few such units being recruited.&lt;br /&gt;
* '''randomness'''=0.1: (number)  A random number is applied to the Experimental AI recruiting score to prevent the recruitment pattern from being too predictable. 0 causes no randomness to be applied, while larger numbers increase the random effect. A value of 1-2 generates results in which the random effect is approximately equal to the scored effect. Extremely high values are essentially entirely random.&lt;br /&gt;
&lt;br /&gt;
== Experimental AI Details ==&lt;br /&gt;
&lt;br /&gt;
The Experimental AI uses most of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|candidate actions]] (CAs) of the [[RCA AI]], the one exception being the recruitment CA which is replaced by another recruiting CA.  In addition, it also adds a number of new CAs.  The following are the differences between the Experimental AI and the default (RCA) AI:&lt;br /&gt;
&lt;br /&gt;
====Recruitment====&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} At the moment, the recruiting CA remains the main difference between the default and the Experimental AI.&lt;br /&gt;
&lt;br /&gt;
The Experimental AI has a completely different recruiting algorithm that was designed to emulate the choices of a human player, especially in the first few turns.  As such, it tries to pick units that counter both what is on the battlefield and that the opponent could recruit.  As it gains more units relative to the enemy, it picks harder hitting units, even at the cost of fragility, to break through enemy lines.&lt;br /&gt;
&lt;br /&gt;
Unlike the default recruitment algorithm, it also chooses where the units are recruited in order to maximize the number of villages that can be captured and if it knows the leader will be moving to a &amp;quot;better&amp;quot; keep (see below), it will under-recruit at the first keep to project more forward power, recruiting just enough to capture all villages.&lt;br /&gt;
&lt;br /&gt;
It also adjusts its recruitment based on map size, favouring faster units on larger maps, trusting that the economy advantage from capturing more villages will more than offset the price.&lt;br /&gt;
&lt;br /&gt;
It knows about poison and avoids recruiting units that depend on it for damage if the enemy is immune.&lt;br /&gt;
&lt;br /&gt;
It also looks at the expected cost of the unit over the next few turns, which prevents the recruitment of too many fast weak units unless the additional gold from an expected capture of a village is enough to offset the increased cost.&lt;br /&gt;
&lt;br /&gt;
Time of day is taken into account and it prefers units that will reach the current closest enemy at the favoured time of day. This allows, for example, for a slight bias towards saurians during the day and drakes at night on small maps (because they will reach the fight at the right time).&lt;br /&gt;
&lt;br /&gt;
Because the AI cannot use them well, it does not accurately measure the damage done by a unit with berserk, causing them to almost never be recruited.&lt;br /&gt;
&lt;br /&gt;
There is also a small amount of randomness added to allow two units that are almost equally good according to the evaluation to be chosen evenly, instead of only picking the first one every time.&lt;br /&gt;
&lt;br /&gt;
'''Configuration parameters:''' The [args] tag of the recruitment CA can take configuration parameters '''high_level_fraction''', '''randomness''' and '''reset_cache_each_turn''' to modify its behavior.  They are described under the [[Micro_AIs#Rush_Recruitment_AI_.28ai_type.3Drecruit_rushers.29|Rush Recruitment AI]] entry on the Micro AI wiki page.&lt;br /&gt;
&lt;br /&gt;
====Villages====&lt;br /&gt;
&lt;br /&gt;
The Experimental AI prefers to capture villages over fighting. This weakens it somewhat in raw fighting but it tends to get enough extra gold that this is actually to its advantage vs. the default AI, although its current implementation is probably too focused on villages.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} This CA has '''not''' been merged into the default AI. It is too simple to work in a general setting (and therefore not suited for the default AI) and there is a separate village grabbing CA in the default AI, so it does not make much of a difference. This CA needs to be either improved or removed.&lt;br /&gt;
&lt;br /&gt;
====Healing and Retreating====&lt;br /&gt;
&lt;br /&gt;
If a healer is not used to attack, the Experimental AI will try to heal units, instead of positioning to attack later.&lt;br /&gt;
&lt;br /&gt;
In a separate CA, units retreat to villages/healers when injured.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} These two CAs have been merged into the default AI.&lt;br /&gt;
&lt;br /&gt;
====Castle Switching====&lt;br /&gt;
&lt;br /&gt;
The leader will move to different keeps, providing a forward force. Usually this works very well, especially at the beginning of the game, but it keeps moving later, which sometimes might get it killed in some scenarios. In that case, it might be worth stopping this behaviour after several turn by removing this CA.&lt;br /&gt;
&lt;br /&gt;
====Enemy Poisoning====&lt;br /&gt;
&lt;br /&gt;
There is also an explicit poison attack routine that tries to poison unpoisoned units instead of repeatedly poisoning the same one.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} This CA has been merged into the default AI.&lt;br /&gt;
&lt;br /&gt;
====Move to Any Enemy====&lt;br /&gt;
&lt;br /&gt;
This is a fall-back CA that kicks in only when no other actions are taken. Its purpose is to not let the AI units sit idle on maps without enemy leaders and villages.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} This CA has been merged into the default AI.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=69660</id>
		<title>Micro AIs</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=69660"/>
		<updated>2022-06-22T23:12:23Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Rush Recruitment AI (ai_type=recruit_rushers) */ add reset_cache_each_turn parameter&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== A Few General Words about Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
* Micro AIs allow Wesnoth campaign and scenario authors to add new AI functionality without the need for any AI programming. They bring new specialized behavior to Wesnoth that cannot be achieved, at least not easily, by adjusting default AI parameters (such as those described at [[AiWML]]) or with WML code.&lt;br /&gt;
&lt;br /&gt;
* A Micro AI is activated and configured (or deleted) via the [micro_ai] tag, requiring only a few lines of WML code.&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.6''', the Micro AI behavior was inconsistent when it came to '''hidden enemies and petrified units'''.  Some Micro AIs ignored those, while others did not.  In some case there was even a potential for AI errors when an AI unit ran into hidden or petrified enemies.  As of 1.13.6, this has all been fixed.  The Micro AIs now correctly and consistently ignore hidden units and treat petrified units as blocking the hex they are on but having no effect otherwise, in the same way as the default AI does.  '''Important notes:'''&lt;br /&gt;
** It is not possible to re-include hidden/petrified units via the Standard Unit Filters (SUFs) used by some of the Micro AIs. In fact, if this is attempted, the filters will generally not match any unit.&lt;br /&gt;
*** This only applies to SUFs which go directly into the [micro_ai] tag. Any SUF that is a sub-tag of another filter (for example, of a Standard Location Filter) is taken literally and returns all units matching the filter. If hidden or petrified units are to be excluded from such a sub-filter, that needs to be specified directly in that filter.&lt;br /&gt;
** We also note that, because all the Micro AIs are written in Lua, it is possible to [[Micro_AIs#Using_Development_.281.13.29_Version_Micro_AIs_in_your_Stable_.281.12.29_Version_Add-on|move a Micro AI's code into one's add-on]] and modify it to whatever scenario-specific behavior is desired (this does not only apply to hidden/petrified units, but to all behavior). Setting up general Micro AI behavior that works in every possible setting is not feasible, but adapting it to work (only) in the specific setting of a given scenario is often quite simple. If you do not know how to do this, please post a message on the Wesnoth forums and we will be happy to help you with it.&lt;br /&gt;
&lt;br /&gt;
* The '''[[AiWML#List_of_AI_Aspects|default AI aspects]] are, for the most part, not taken into account by the Micro AIs'''.  For example, unless noted otherwise, specifying a value for ''aggression'' does not change the MAI behavior, and locations specified in an ''[avoid]'' tag generally can be entered by units controlled by an MAI.  This is a feature of the Micro AIs, not a bug.  If you feel that a certain default AI aspect should also apply to a Micro AI, please let us know in the [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 Micro AIs Feedback thread].&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.7''', the Micro AIs did not all work reliably when the '''AI side is under shroud''' and it may have been necessary to turn shroud off for the side when using a Micro AI. {{DevFeature1.13|7}} The Micro AIs now all work for sides under shroud. They do this by disregarding shroud for terrain purposes when path finding, consistent with default Wesnoth AI behavior, while still ignoring hidden units (including those hidden under shroud) correctly.&lt;br /&gt;
&lt;br /&gt;
* Most Micro AIs in their default setting apply to all units of a side, although many of them provide parameters for filtering the units that should be affected by the new behavior.  If the Micro AI is applied only to part of the units of a side, the other units follow the default Wesnoth AI behavior.&lt;br /&gt;
&lt;br /&gt;
* Micro AIs are available in mainline since Wesnoth 1.11/1.12.&lt;br /&gt;
&lt;br /&gt;
== Micro AI Test and Demo Scenarios ==&lt;br /&gt;
&lt;br /&gt;
All Micro AIs described here can be checked out in a number of test and demo scenarios.  The easiest way to access the test scenarios is by assigning a hotkey to &amp;quot;Start Test Scenario&amp;quot; in the preferences (as of Wesnoth 1.13.8). Then, simply press the hotkey in the titlescreen and select the desired scenario from the list (e.g. 'micro_ai_test' for the overall &amp;quot;switchboard scenario&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Test scenarios can also be accessed from the command line by typing&lt;br /&gt;
 path/wesnoth-executable -t micro_ai_test&lt;br /&gt;
or (depending on your system and Wesnoth version)&lt;br /&gt;
 path/wesnoth-executable -tmicro_ai_test&lt;br /&gt;
This starts up a &amp;quot;switchboard scenario&amp;quot; in which you can select the Micro AI demonstration scenario you want to check out.  Here, ''path/wesnoth-executable'' needs to be replaced by whatever the path and filename of the Wesnoth executable is on your system.&lt;br /&gt;
&lt;br /&gt;
== Setting up a Micro AI ==&lt;br /&gt;
&lt;br /&gt;
Setting up a Micro AI requires only one simple step:&lt;br /&gt;
==== Activating and configuring (or deleting) a Micro AI ====&lt;br /&gt;
&lt;br /&gt;
Micro AIs are activated, deleted and configured using the [micro_ai] tag.  This tag needs to be placed in [[ActionWML]], that is, in an event, a menu option or the like.  As an example, the following code activates the healer_support Micro AI in its default configuration for Side 2 from the beginning of the scenario:&lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
 &lt;br /&gt;
         # Configure the healer support micro AI&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
'''Keys required for all Micro AIs:'''&lt;br /&gt;
All [micro_ai] tags must contain the following three required keys:&lt;br /&gt;
* '''side''': The side for which the Micro AI is to be added, changed or deleted.  This has to be an individual side number, lists of sides are not supported.&lt;br /&gt;
* '''ai_type''': The type of Micro AI to be added, changed or deleted.  See the following sections for allowed values.&lt;br /&gt;
* '''action''' (string): The action to take concerning the Micro AI.  The following values are allowed: &lt;br /&gt;
** ''add'': Add a Micro AI to a side.&lt;br /&gt;
** ''change'': Change the configuration of an existing Micro AI.  Note that this does not only change the specific parameters provided in the tag, but it replaces the entire existing Micro AI by a new version with the new (and only the new) configuration.  It is therefore equivalent to using first the ''delete'' and then the ''add'' action.&lt;br /&gt;
** ''delete'': Delete an existing Micro AI from a side.&lt;br /&gt;
&lt;br /&gt;
'''Keys required under certain circumstances:'''&lt;br /&gt;
* '''ca_id''': (string) If several MAIs of the same type are used for the same side and one or several of these are to be changed or deleted during a scenario, this key needs to be included so that the [micro_ai] tag knows which of them needs to be changed/deleted.  The same is also true if a Micro AI is changed more than once during a scenario.  We therefore '''strongly recommend always setting ''ca_id''''' for MAIs which are, or might be, changed or deleted during a scenario.&lt;br /&gt;
** '''Note:''' The id(s)/name(s) actually assigned to the CA(s) of the MAI by the engine might be different from the parameter passed here.  It is, however, sufficient to use this ca_id with the [micro_ai] tag add/delete/change actions to identify the MAI uniquely.  (In other words: you don't have to worry about this if you always use the [micro_ai] tag, but might have to figure out the actual name(s) of the CA(s) if you want to delete or change them manually.)&lt;br /&gt;
* Other keys may also be required depending on the Micro AI.  In additional, a number of optional keys may be available to configure the AI behavior.  See the sections on the individual Micro AIs for details.&lt;br /&gt;
&lt;br /&gt;
'''Required keys for deleting a Micro AI:'''&lt;br /&gt;
&lt;br /&gt;
Only a subset of the keys required for setting up a Micro AI are needed when deleting it.  '''side''', '''ai_type''' and '''action'''=delete always need to be provided.  '''ca_id''' needs to be given if it was provided when adding the Micro AI (see above for when this is needed).&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* The ''add'' and ''change'' actions ignore all keys that do not apply to the respective Micro AI type.  The ''delete'' action ignores all keys other than the required keys listed above.&lt;br /&gt;
&lt;br /&gt;
* Some of the Micro AIs use and/or modify default AI components (candidate actions or aspects) when they are added, or reset these components to their default values when they are deleted.  Thus, if you have modified the AI yourself using the default CAs or aspects, some of these modifications might be affected, or might be interfering with the Micro AI.  The sections below indicate whether a Micro AI modifies any default AI components.&lt;br /&gt;
&lt;br /&gt;
===Combining Different Micro AI Types for the Same Side===&lt;br /&gt;
&lt;br /&gt;
No extra steps need to be taken when combining different Micro AIs on the same side.  Simply set up the [micro_ai] tags for all AIs you want to use.&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* While it is ''technically'' possible to combine any number of Micro AIs, this does not mean that all Micro AIs work together in practice.  Some are not compatible because they control units in contradictory ways.  &lt;br /&gt;
* Not all combinations that ''should'' work have been tested yet.  If you find a combination that you believe should work but doesn't, [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 please let us know] and we will see if that can be fixed.&lt;br /&gt;
* The candidate action score almost certainly has to be set (using the ca_score= key) for one or several of the MAIs if several of them are used on the same side.&lt;br /&gt;
&lt;br /&gt;
== Animals Micro AIs ==&lt;br /&gt;
=== General Comments on the Animal Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Animals Micro AIs are meant to simulate the behaviors of certain types of animals.  They are, however, set up so that they can be used with arbitrary unit types and might be applicable in quite diverse situations.  The AIs nevertheless remain named after the animals for which they were written originally, so that they are (to some extent) descriptive of their behavior.&lt;br /&gt;
&lt;br /&gt;
Brief descriptions of the AI behaviors are given in the following subsections.  To get a better feeling for their behavior, check out the &amp;quot;Animals&amp;quot;, &amp;quot;Swarm&amp;quot;, &amp;quot;Wolves&amp;quot; and &amp;quot;Dragon&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  Note that some of the animal AIs are designed for animal types that do not exist in mainline.  In the test scenario, other animal types are therefore substituted for them.&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Wolves (ai_type=wolves) ===&lt;br /&gt;
&lt;br /&gt;
The Wolves Micro AI organizes all &amp;quot;wolf&amp;quot; units (predators) of the side to hunt in a single pack.  They actively chase after the closest prey and try to corner it (not always super successfully), but are easily distracted by other enemy units coming into attack range, to the point where the pack might split up.  The wolves can, however, be told to attack only prey units as well as to try to avoid other types of units (such as larger predators).  When no prey is left, the wolves wander randomly.&lt;br /&gt;
&lt;br /&gt;
This AI works best for a small number of predators (2 - 5) and sparse prey.  Also, the predators should be put on the map in a pack, that is, reasonably close to each other.  For a demonstration of the Wolves AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Note that another wolves AI exists, called [[Micro_AIs#Animals_AI:_Multi-pack_Wolves_.28ai_type.3Dwolves_multipacks.29|Multipack Wolves]], which distributes the wolves of a side into several packs, rather than all of them into one single pack as it is done here.&lt;br /&gt;
&lt;br /&gt;
Enable the Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the predator units.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the prey units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_only_prey'''=no: In the default configuration, the wolves attack all enemy units in attack range.  If this key is set to 'yes', they only attack prey units.&lt;br /&gt;
* '''avoid_type''': Comma-separated list of unit types (on enemy sides) which the wolves try to avoid.  They try to stay out of these units' way as much as possible when moving around (except when they are moving in to attack another enemy unit) and never attack them.&lt;br /&gt;
* '''ca_score'''=90000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 90,000, which is lower than the default combat CA, but higher than most other default CAs which move units.  It can be changed by setting this parameter but in most cases this will not make sense.  Note that the Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Notes on default (RCA) AI aspects:''' &lt;br /&gt;
* If ''attack_only_prey'' or ''avoid_type'' are set, the Wolves Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* Attacks by the wolves are left to the default (RCA) AI, meaning that the value of ''aggression'' has an influence on the behavior.&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI is taken into account. Note: as attacks by the wolves are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=6&lt;br /&gt;
             ai_type=wolves&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Wolf&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Vampire Bat&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             avoid_type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Multi-pack Wolves (ai_type=wolves_multipacks) ===&lt;br /&gt;
&lt;br /&gt;
The Multi-pack Wolves Micro AI is different from the [[Micro_AIs#Animals_AI:_Wolves_.28ai_type.3Dwolves.29|Wolves Micro AI]] in that there can be an arbitrary number of wolf packs, with the pack size being a free parameter.  At the beginning of the scenario, close wolves are grouped into packs in a semi-methodical way.  If the wolves are put on the map in distinct groups of close wolves of the correct number, they will be joined into packs matching these groups.  If, on the other hand, they are spread out all over the map, the method of assigning them to packs is semi-random.&lt;br /&gt;
&lt;br /&gt;
Wolves of the same pack begin by joining each other on the map.  After that, they stay together until only one wolf is left, which then tries to join up with an incomplete pack or with other single wolves.  Individual wolves entering the map during the scenario behave in that way as well.&lt;br /&gt;
&lt;br /&gt;
A second difference to the other Wolves AI is that wolves do not actively hunt here.  For the most part they just wander (often long distance).  However, the pack ferociously (and without regard for its own health) attacks any enemy units that come into range, as long as that does not mean separating the pack by more than a few hexes.  Staying together, or joining with a new wolf assigned to the pack, is the only thing that takes priority over satisfying the wolves' thirst for blood.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Multi-pack Wolves AI behavior, check out the &amp;quot;Wolves&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Multi-pack Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves_multipacks&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Multi-pack Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''pack_size'''=3: (integer) The size of the packs.&lt;br /&gt;
* '''show_pack_number'''=no: If set to &amp;quot;yes&amp;quot;, the wolves' pack numbers will be shown below them.  Note that this means that the pack number of a wolf killed in an attack will remain as a label on the map.  This behavior is intentional to help UMC authors note where units might have made suicidal mistakes.&lt;br /&gt;
* '''type'''=Wolf: Comma-separated list of the unit types which the AI controls. The default value is &amp;quot;Wolf&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves_multipacks [micro_ai] tag usage from the &amp;quot;Wolves&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=wolves_multipacks&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             show_pack_number=yes&lt;br /&gt;
             pack_size=4&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Big Animals (ai_type=big_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Big Animals Micro AI is a simulation of large predators that wander and hunt alone, such as Bears, Giant Spiders or Yetis.  For the most part, these just wander on the terrain that's been defined for them, but they attack enemy units if those happen to come into range.  The AI can be set up so that the Big Animals stay out of the way of other units (such as other large predators), in which case they only attack those if cornered or if they run into them accidentally (such as ambushers).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Big Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Big Animals Micro AI by using&lt;br /&gt;
 ai_type=big_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the animals to which this AI is applied.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': The big animals often go for long distance (multi-turn) journeys.  This tag is a [[StandardLocationFilter|Standard Location Filter]] describing the terrain from which the next goal is chosen.  The default is all terrain on the map. &lt;br /&gt;
* '''[filter_location_wander]''': The terrain on which the big animals will end their moves. This is a [[StandardLocationFilter|Standard Location Filter]].  The default is all terrain on the map.&lt;br /&gt;
* '''[avoid_unit]''': A [[StandardUnitFilter|Standard Unit Filter]] describing all enemy units that the big animals will avoid.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
Here's an example of big_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=big_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Ghast&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [avoid_unit]&lt;br /&gt;
                 type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
             [/avoid_unit]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=1-40,1-18&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Forest Animals (ai_type=forest_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Forest Animals Micro AI simulates a number of different animal behaviors that one might find in a forest, namely animals behaving like deer, rabbits and families of tuskers.  In the following, we will refer to those animals by these types, even though the actual unit types can be selected freely.  They behave as follows:&lt;br /&gt;
* Each deer wanders randomly on (selectable) terrain, except when enemies get in its (the deer's) maximum movement range, in which case it flees to the farthest point it can reach.&lt;br /&gt;
* Tuskers exhibit the same behavior as deer, except when an enemy is next to one of the tusklets.  This enemy is attacked unconditionally by the tuskers trying to protect their young.&lt;br /&gt;
* Tusklets blindly follow the closest adult tusker, except when there is no tusker left, in which case they behave the same as deer.&lt;br /&gt;
* Rabbits also wander randomly, but in addition disappear into their holes (if any are within reach) when enemies are close.  They reappear out of their holes at the beginning of the turn, if it is safe.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Forest Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Forest Animals Micro AI by using&lt;br /&gt;
 ai_type=forest_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Forest Animals AI consists of 4 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-3&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''rabbit_type, tusker_type, tusklet_type, deer_type''': Comma-separated lists of the unit types behaving as the respective animals.  If any of these parameters are not set, those types of animal behavior are skipped by the AI.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain on which the forest animals wander when they are not threatened by enemies.&lt;br /&gt;
* '''rabbit_number'''=6: (integer)  The number of rabbits for the AI to have on the map simultaneously.  The AI puts the missing number of rabbits onto the map at the beginning of the AI's turn (when the rabbits come out of their holes).&lt;br /&gt;
* '''rabbit_enemy_distance'''=3: (number)  Rabbits won't spawn in holes that aren't more than this distance away from all enemies.&lt;br /&gt;
* '''rabbit_hole_img''': Rabbit holes are marked by items on the map.  If this parameter is set, it must be the file name of the item image.  If it is not set, all items on the map are treated as rabbit holes.&lt;br /&gt;
** {{DevFeature1.14|10}} Items used for rabbit holes on the map border used to produce an error. Placing them on the map border therefore needed to be avoided on the scenario setup side. They are now automatically excluded from consideration by the AI code.&lt;br /&gt;
&lt;br /&gt;
Here's an example of forest_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=forest_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             deer_type=Vampire Bat&lt;br /&gt;
             rabbit_type=Giant Rat&lt;br /&gt;
             tusker_type=Ogre&lt;br /&gt;
             tusklet_type=Young Ogre&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=*^F*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Herd and Herders (ai_type=herding) ===&lt;br /&gt;
&lt;br /&gt;
The Herding Micro AI sets up an area on the map where a number of herder units (such as sheep dog) control and protect a herd (such as sheep).  For convenience, we simply refer to them as sheep dogs and sheep in the following, even though arbitrary unit types can be selected for either.&lt;br /&gt;
&lt;br /&gt;
Sheep dogs try to keep their sheep safe.  This involves keeping them inside the herding area, positioning themselves in between the sheep and approaching enemies, and attacking the enemies if those get too close.  If no active herding or protecting move is needed, the dogs go to a random location on the perimeter of the herding area.&lt;br /&gt;
&lt;br /&gt;
Sheep wander aimlessly except when a sheep dog is next to them, in which case they run away from the dog.  The dogs exploit this by positioning themselves on the outside of the sheep, if possible.  Sheep also run away from approaching enemies.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Herding AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Herding Micro AI by using&lt;br /&gt;
 ai_type=herding&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herder units (e.g. dogs).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herd units (e.g. sheep).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain forming the perimeter of the herding area.  In other words, this is the path the herders (dogs) use to patrol around the outside of the herding area.  This must be a set of coordinates ''completely enclosing'' the herding area.  Here's an example of setting up a rectangular perimeter:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         x=10-20,10-20,10,  ,20&lt;br /&gt;
         y=11,   21,   11-21,11-21&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
Another possibility is by defining the path by certain terrain on the map (as it is done in the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]), in which case the filter can simply look like this:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         terrain=Rb&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
* '''herd_x,herd_y''': The coordinate of one hex (''any'' one hex) inside the herding area.  This is needed to define which part of the map is on the ''inside'' of the herding area perimeter defined by [herding_perimeter], and which is the ''outside''. {{DevFeature1.15|0}} '''herd_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''attention_distance'''=8: (integer)  If an enemy is within this distance of a herd unit, but not as close as ''attack_distance'', the herders try to position themselves in between the enemy and the herd unit.&lt;br /&gt;
* '''attack_distance'''=4: (integer)  If an enemy is within this distance of a herd animal, the herders attack this enemy.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Herding AI consists of 6 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-5&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Herding [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=herding&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Footpad&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Troll Whelp&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             herd_x,herd_y=32,28&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=Rb&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Hunter (ai_type=hunter) ===&lt;br /&gt;
&lt;br /&gt;
The Hunter Micro AI leads a unit (or units) through hunt-and-rest cycles.  The units wander in a prescribed area of the map, their hunting ground, until an enemy unit comes within range.  Note that the units do not actively chase down enemies, but wander randomly through the hunting ground until an enemy is encountered.&lt;br /&gt;
&lt;br /&gt;
If enemies are within range of a hunter unit, the hunter attacks the weakest of them.  If a kill is made, it then retreats to its rest location, where it stays for a certain number of turns and until fully healed (whatever happens later).&lt;br /&gt;
&lt;br /&gt;
A few more details:&lt;br /&gt;
* If the hunter's way home is entirely blocked on the return (so that there is no possible path), the normal RCA AI takes over its behavior.&lt;br /&gt;
* However, if the way is blocked by an enemy unit occupying the rest hex, that enemy will be attacked unconditionally.&lt;br /&gt;
* A kill only makes the hunter go home when it is the attacker, not as defender.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Hunter AI behavior, check out the &amp;quot;Dragon&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]. &lt;br /&gt;
&lt;br /&gt;
Enable the Hunter Micro AI by using&lt;br /&gt;
 ai_type=hunter&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''home_x,home_y''': The coordinates of the hex to which the hunter returns and rests at after a kill. {{DevFeature1.15|0}} '''home_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the unit's hunting ground.  If this tag is not provided, the hunter uses all of the map.&lt;br /&gt;
* '''rest_turns'''=3: (integer)  The number of turns the AI stays at home_x,home_y after a kill.  It will not leave its home until this number of turns have passed ''and'' it is fully healed.&lt;br /&gt;
* '''show_messages'''=no: (boolean) If set to yes, the hunter announces whenever its behavior changes to the next phase.  Note that this is meant as debug output for the scenario author.  The messages are not translated and are not included in replays (and are only visible to the host in an MP game).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a hunter [micro_ai] tag usage from the &amp;quot;Dragon&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hunter&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Rowck&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=5-30,1-15&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             home_x,home_y=3,15&lt;br /&gt;
             rest_turns=2&lt;br /&gt;
 &lt;br /&gt;
             show_messages=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Swarm (ai_type=swarm) ===&lt;br /&gt;
&lt;br /&gt;
The Swarm Micro AI uses some very simply algorithms to simulate animal swarm behavior.  Without adjacent enemies, they simply do a random move, trying to stay together and at a certain distance from enemies.  However, if an enemy unit is close to any bat, the swarm scatters.  This is particular fun to watch when one places an enemy unit in the middle of the swarm.  After being scattered, the swarm members slowly rejoin, but not in a very organized way.  Sub-swarms or individual bats might roam around for quite some time before they find their way back.  It is also possible that individual bats (or small groups) split off from the larger swarm at times.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Swarm AI behavior, check out the &amp;quot;Swarm&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Swarm Micro AI by using&lt;br /&gt;
 ai_type=swarm&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Swarm AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_distance'''=5: (int) The minimum distance kept between units of the swarm and enemies when the swarm moves as a whole.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.15|12}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the units.  Only AI units matching this filter participate in the swarm.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''scatter_distance'''=3: (int) Enemies within &amp;quot;scatter_distance&amp;quot; hexes of any swarm unit cause the swarm to scatter, by each unit trying to maximize its individual distance from the enemy.&lt;br /&gt;
* '''vision_distance'''=12: (int) Only units within this distance follow the overall swarm motion (either away from an enemy or of the swarm as a whole).  The smaller this value is set, the less likely the swarm is to stay together or rejoin.  This parameter is meant to simulate how far an individual member of the swarm can see, meaning that the swarm is &amp;quot;out of sight&amp;quot; (and the unit will not be able to follow it) if it is more than this distance away.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Swarm [micro_ai] tag usage from the &amp;quot;Swarm&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=swarm&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Assassin Squad Micro AI (ai_type=assassin) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|6}}&lt;br /&gt;
&lt;br /&gt;
This Micro AI controls one or several assassin units which try to circle around enemy units with the goal of hitting a target unit without being distracted or impeded by other enemies.  The units will always attack the target if such attacks are possible, otherwise they will follow the path of least resistance toward it. Notes:&lt;br /&gt;
&lt;br /&gt;
* The main priority for the AI is to find paths that avoid enemy units. However, if no such path can be found, the assassins will still advance toward the target, taking the path exposing the units to the least potential damage from enemies.&lt;br /&gt;
&lt;br /&gt;
* The units will not necessarily follow the same path. The best path is chosen individually for each unit.&lt;br /&gt;
&lt;br /&gt;
* While it is not a requirement for the AI to work, using ambusher or skirmisher units, or units with the quick trait, makes this AI more lethal in many cases.&lt;br /&gt;
&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded, it does not work otherwise. That means that '''ca_score''' is not a valid key for the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
Enable the Assassin Squad Micro AI by using&lt;br /&gt;
 ai_type=assassin&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Assassin Squad specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the assassin units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the target unit.&lt;br /&gt;
** It is important to realize that the AI currently only works correctly with a single target unit. It is thus the responsibility of the scenario designer to make sure that this filter matches an individual enemy unit. If several enemy units are to be targeted in the same scenario, several instances of the Assassin Micro AI can be used.&lt;br /&gt;
** The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[prefer]''': A [[StandardLocationFilter|Standard Location Filter]] selecting locations on the map for the assassins to prefer when making their way toward the target unit. This could be used, for example, to make units with the ambush ability move (mostly) through forest or to have the assassins circle around the, say, northern part of the map rather than the south if that is desirable for scenario design purposes.&lt;br /&gt;
** As with avoiding enemies, the units will still move across non-preferred terrain if that is either the only option or ''much'' shorter than preferred terrain&lt;br /&gt;
** Attacking, once the target unit is in range, is not influenced by the [prefer] SLF.&lt;br /&gt;
&lt;br /&gt;
Here is an example of an Assassin Squad [micro_ai] tag usage:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=assassin&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Assassin,Master at Arms,Duelist&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             [prefer] # Circle around northern part of map&lt;br /&gt;
                 x=1-9, 10-99&lt;br /&gt;
                 y=1-37, 1-17&lt;br /&gt;
             [/prefer]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Bottleneck Defense Micro AI (ai_type=bottleneck_defense) ==&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense Micro AI lets you define a location on the map where the AI can take a defensive stance behind a narrow passage (bottleneck).  Units on the front line are backed up by healers and/or units with the leadership ability.  Injured units are moved to the back and replaced by uninjured (or less injured) units.  The units on the front line only attack when it is safe (no retaliation) or when there is a high chance that they will make a kill or level up.  Using this Micro AI only makes sense if there is no way for the enemy sides to move around the bottleneck and attack the AI's units from behind.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Bottleneck&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Bottleneck Defense Micro AI by using&lt;br /&gt;
 ai_type=bottleneck_defense&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Bottleneck Defense specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense AI requires two sets of coordinates that define where it should be taking up its defensive stance, and from which side the enemy attacks at that location.&lt;br /&gt;
&lt;br /&gt;
* '''x,y''': Comma separated lists of the hexes on the front line, where strong units are placed.  All hexes on which the AI makes contact with (can be attacked by) the enemy need to be included here.  Units are placed on them in the order in which they are listed here. {{DevFeature1.15|0}} '''location_id''' can be used instead with named/special locations.&lt;br /&gt;
* '''enemy_x,enemy_y''': Comma separated list of the hexes from which the enemy can attack the AI front line units.  This is needed for the AI to know on which side of the front line support units (healers etc.) need to be placed.  In many cases, it is sufficient to provide only one of the enemy front line hexes, but there are geometries for which that does not work.  It is therefore safer to list all enemy front line hexes here. {{DevFeature1.15|0}} '''enemy_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
By default, the AI places healers and units with the leadership ability both on the front line itself (if they are the strongest units available) and on hexes adjacent to and behind the line.  If different placement is desired, these locations can be overridden with the following keys.&lt;br /&gt;
&lt;br /&gt;
* '''active_side_leader'''=no: (boolean)  If set to 'yes', the side leader participates in the bottleneck defense action after the side's gold has been spent.  If set to 'no' (default), the leader follows default AI behavior (sitting on the keep and recruiting, for the most part).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Bottleneck Defense AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.15|3}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the units.  Only AI units matching this filter participate in the bottleneck defense.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''healer_x,healer_y''': Comma separated list of hexes where healers are placed.  This can be used to keep healers from the front line (or to take up only certain positions on the front line), and/or to override the default healing positions behind the line.  Healers are placed on them in the order in which the hexes are listed, with the exception that hexes on the line always take priority over hexes behind the line. {{DevFeature1.15|0}} '''healer_loc''' can be used instead with named/special locations.&lt;br /&gt;
* '''leadership_x,leadership_y''': Same as ''healer_x,healer_y'', but for units with the leadership ability. {{DevFeature1.15|0}} '''leadership_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
As an example, in the &amp;quot;Bottleneck&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]], there are three front line hexes, two of which are on hill terrain, while the third is a road with a lower defense rating.  The healer and side leader are supposed to participate in combat because they are strong units, but only from the hill hexes to keep them safer.  This is accomplished by using the following keys settings (check out the scenario to see which hex is which):&lt;br /&gt;
 [micro_ai]&lt;br /&gt;
     side=1&lt;br /&gt;
     ai_type=bottleneck_defense&lt;br /&gt;
     action=add&lt;br /&gt;
 &lt;br /&gt;
     x=14,14,14&lt;br /&gt;
     y= 7, 9, 8&lt;br /&gt;
     enemy_x=13,13&lt;br /&gt;
     enemy_y= 8, 9&lt;br /&gt;
 &lt;br /&gt;
     healer_x=14,14,15,15&lt;br /&gt;
     healer_y= 7, 9, 8, 9&lt;br /&gt;
     leadership_x=14,14,15,15&lt;br /&gt;
     leadership_y= 7, 9, 9 ,8&lt;br /&gt;
     active_side_leader=yes&lt;br /&gt;
 [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Fast Micro AI (ai_type=fast_ai) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|0}}&lt;br /&gt;
&lt;br /&gt;
This AI is meant as a fall-back AI for scenarios where there are so many units that attacks and moves done by the default AI are slow with long delays before each move.  It replaces the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions (CAs).  It should be obvious that in order to achieve the faster speed, compromises concerning the quality of the moves have to be made.&lt;br /&gt;
&lt;br /&gt;
Enable the Fast Micro AI by using&lt;br /&gt;
 ai_type=fast_ai&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Combat candidate action:'''&lt;br /&gt;
* Attackers are considered individually one at a time.  The best attack found for a unit is executed without checking whether a better attack by another unit might be possible.&lt;br /&gt;
* The default order in which attackers are considered is by decreasing hitpoints, starting with the strongest unit (in terms of HP).  This can be modified with the optional keys below.&lt;br /&gt;
* By default, if weaker units block attack hexes for a stronger unit, they do not move out of the way (this is the same as for the default AI).  If a hex becomes available after an attack (e.g. due to a weaker unit dying in the attack), all attacks that were possible ''before'' this happened are finished first.  Only after that are attacks by the stronger unit reconsidered.&lt;br /&gt;
* If include_occupied_attack_hexes=yes is set, weaker units move out of the way for stronger units if possible.  This can, however slow down the AI depending on the situation on the map.  Use with caution.&lt;br /&gt;
* {{DevFeature1.13|2}} Previous to this version, attacks by the leader were bunched in with the attacks by the other units.  Now leader attacks are treated separately and contain an extra layer of analysis to determine whether the attack position is &amp;quot;safe&amp;quot; (in an approximate way).  Currently, this is done with two simple considerations:&lt;br /&gt;
** Count all enemy units that can reach the leader's attack position.  If there are more than given by optional parameter 'leader_attack_max_units' (default value 3), do not attack.&lt;br /&gt;
** Add up the hitpoints of all enemies that can reach the leader's attack position.  If this value, multiplied by optional parameter 'leader_weight'(default value 2), is larger than the leaders hitpoints, do not attack.&lt;br /&gt;
** Two optional parameters, 'threatened_leader_fights' and 'leader_additional_threat', can also be used to let the leader attack when the threat at the attack hex is comparable to that at the leader's current position.&lt;br /&gt;
** In order to keep this AI as fast as possible, leader attacks are done by a separate candidate action that is only evaluated when all other CAs are done.&lt;br /&gt;
&lt;br /&gt;
'''Move-to-targets, villages and retreat candidate actions:'''&lt;br /&gt;
* The move-to-targets, villages and retreat candidate actions of the default AI are removed.  Move-to-targets and villages are taken over by the Fast Micro AI, whereas retreat is simply not done.&lt;br /&gt;
* This CA moves units toward goals after attacks are done.  Goals are unowned (if the AI side has a leader only) and enemy-owned villages and enemy leaders.&lt;br /&gt;
* For each goal, units are pre-sorted in order of the fewest moves needed to cover the ''distance'' toward the goal (that is, as if move cost on each hex toward the goal were 1).  Units are then considered sequentially and if a unit can get there &amp;quot;efficiently&amp;quot;, its move is executed without considering other units.&lt;br /&gt;
** A move is considered efficient if the move cost to get to the hex is not more than 'move_cost_factor' larger than the distance to the hex.  'move_cost_factor' is an optional key that defaults to 2.  It can be set to values of 1.1 or larger (see below).&lt;br /&gt;
** If the move for the current unit is not found to be efficient, the AI considers the next unit instead.  If no unit can do an efficient move toward the goal, the best (fastest) of the inefficient moves is selected instead.&lt;br /&gt;
&lt;br /&gt;
* Village goals:&lt;br /&gt;
** Village goals are considered before enemy leader goals so that the AI preferentially chooses scouts (or otherwise fast units) for them.&lt;br /&gt;
** The number of units sent toward villages depends on the value of the default AI aspect village_value.  It is set to village_value/4 times the number of units the AI has left to move.  Thus, by default (village_value=1.0) the AI sends a quarter of its units toward villages, and the rest toward enemy leaders.  This means that the meaningful range of values for village_value is 0..4.&lt;br /&gt;
** Only one unit is moved toward each village.  This consideration also overrides the number of units to move toward villages defined by village_value, topping it at the number of unowned/enemy-owned villages on the map.&lt;br /&gt;
** Setting village_value=0 disables moving units toward villages altogether.  That is, not only are no units chosen, but the village targeting phase of the AI is entirely disabled with no evaluation time used for it.  Note that, unlike with the default AI, this also means that no villages within range of an AI unit are grabbed.&lt;br /&gt;
** The order in which villages are chosen as goals is some combination of them being close to the AI side's leader and being spread out over the map.&lt;br /&gt;
&lt;br /&gt;
* Enemy leader goals:&lt;br /&gt;
** The order of enemy leaders goals is set according to their distance from the AI leader with the closest enemy leader being targeted first (if the AI has a leader, otherwise their order is random).  One unit is sent toward each enemy leader in this order, after which the AI starts with the first enemy leader again.  This is repeated until all units have moved.&lt;br /&gt;
&lt;br /&gt;
* Guardians (units with ai_special=guardian set) are ignored by the fast move-to-targets CA.&lt;br /&gt;
&lt;br /&gt;
'''Tips for speeding up/modifying the Fast Micro AI in specific scenarios:'''&lt;br /&gt;
&lt;br /&gt;
A number of optional keys and default AI aspects can be used to modify the behavior of the Fast Micro AI.  The MAI is set up so that it is a reasonable compromise between speed and quality of the moves in most situations.  However, this might not be ideal for all scenarios here are some things that can be done to speed up the AI (or to get better behavior in some situations by doing the opposite):&lt;br /&gt;
* Use large values for the default AI aspect aggression.  The largest delays before attacks happen (in some circumstances) when the Fast MAI has to consider lots of units with attacks left but decides not to attack with them.  See below for a description of how aggression is used by the fast AI.&lt;br /&gt;
* Use large values of move_cost_factor (see above for description of what this does).  If you want to emphasize units that can move across terrain easily (e.g. bats) to be chosen for village grabbing, you might want to use low values of move_cost_factor, but this might slow down the AI.  The default value should work reasonably well in most standard situations, but modifying the value might work better on specific maps.&lt;br /&gt;
* Don't use dungeon_mode=yes unless absolutely necessary, even in most dungeon crawls.  Test the AI without this setting first and only use it if it is the only way to solve the problem.  Often, a minor tweak to the map will have the same effect, without the speed penalty.&lt;br /&gt;
* Use include_occupied_attack_hexes=yes with caution.  It might (or might not) slow down the attack CA significantly, depending on the scenario.&lt;br /&gt;
* Set village_value=0, which makes the AI not consider villages as goals.  This has not been found to make a large difference on the maps tested so far, but it might have an effect on maps with a very large number of villages.&lt;br /&gt;
* Use attack_hidden_enemies=yes on maps where there are no hidden enemies. Excluding hidden enemies from the attack calculation requires an additional filter to be evaluated that takes extra time. In most cases this will not result in a large computation time saving.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_hidden_enemies'''=no: (boolean)  By default, hidden enemies are excluded from the attack calculation.  Setting this to 'yes' makes the AI also consider those enemies as attack targets.&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.13|2}} A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  As with the default AI, units will not step on hexes defined by the [avoid] tag.  In addition, no goals will be chosen that lie on those hexes.  The one minor exception is that a unit stepping out of the way for another unit might temporarily enter the avoided zone.&lt;br /&gt;
* '''dungeon_mode'''=no: (boolean)  As much as possible, the Fast Micro AI works with distances between hexes rather than path finding.  This works pretty well on open maps and even most dungeon crawls (and similar scenarios), but it might result in some units getting temporarily (and in rare cases permanently) stuck on maps with convoluted labyrinths.  If that is the case, setting dungeon_mode=yes triggers more path finding that avoids such situations (but as a result is somewhat slower).&lt;br /&gt;
* '''include_occupied_attack_hexes'''=no: (boolean) If set to 'yes', the AI moves weaker units out of the way for a stronger attacker.  This might lead to better attacks but is slower, so don't use if speed is essential.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the attackers.  Only AI units matching this filter will attack, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes the side= key set to the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the enemies.  Only enemy units matching this filter will be attacked, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''leader_weight'''=2: (number &amp;gt;= 0) {{DevFeature1.13|2}} A factor specifying how much more valuable the AI considers side leaders than non-leader units in the attack evaluation. This is also used to determine whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''leader_additional_threat'''=1: (number &amp;gt; 0) {{DevFeature1.13|2}} If threatened_leader_fights=yes, the threat at the attack position may be larger by this factor for the leader to leave the keep for an attack.&lt;br /&gt;
* '''leader_attack_max_units'''=3: (number &amp;gt;= 0) {{DevFeature1.13|2}} A number specifying how many enemy units within reach are acceptable when determining whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''move_cost_factor'''=2: (number &amp;gt;= 1.1)  The factor by which the move cost to a goal hex may be larger than the distance to the goal hex for a move to be considered &amp;quot;efficient&amp;quot;.  See description above.  1.1 is used if this is set to values smaller than 1.1.  Smaller values might lead to better behavior, while larger values might speed up the AI.&lt;br /&gt;
* '''weak_units_first'''=no: (boolean) If set to 'yes', inverts the order in which units are considered by the combat CA.  Attacks are then done in order of increasing hitpoints of the attackers.&lt;br /&gt;
* '''skip_combat_ca'''=no: (boolean) If set to 'yes', the fast combat CA is not used.  The default AI's high_xp_attack and combat CAs remains in place in this case.&lt;br /&gt;
* '''skip_move_ca'''=no: (boolean) If set to 'yes', the fast move-to-targets CA is not used.  The default AI's retreat, villages and move-to-targets CAs remains in place in this case.&lt;br /&gt;
* '''threatened_leader_fights'''=no: (boolean) {{DevFeature1.13|2}} By default, the AI leader will not attack if the threat at the attack position is too high.  If this key is set to 'yes', the leader will attack if the threat there is not larger than the threat at his current position multiplied by the value of 'leader_additional_threat'. This is meant as an option for the endgame: when the AI leader is under attack, he might as well go down fighting.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''aggression''': The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to set how aggressive the AI should attack.  It works in roughly the same way as the default AI aspect:&lt;br /&gt;
** The value of (1 - aggression) determines how much the AI values its own units compared to the enemy's units.  Thus, aggression=0 means that attacks are only done if the AI expects to deal (roughly) the same amount of damage as it will receive.  aggression=1 means that expected damage to the AI's units is not considered, only the expected damage to enemy units matters.&lt;br /&gt;
** Damage is calculated as a combination of hitpoints and chance to die, in &amp;quot;gold equivalent units&amp;quot;.&lt;br /&gt;
** Unlike the default AI, aggression is set to 1 for values larger than 1.  Allowed values are thus anything from -infinity to +infinity, although only -infinity to +1.0 make sense.&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.13|2}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
*'''attacks aspect''':  {{DevFeature1.13|2}} The standard RCA AI [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] used to filter own and enemy units for attacks is now also taken into account as long as neither [filter] nor [filter_second] (see above under 'optional keys') are defined in the [micro_ai] tag.&lt;br /&gt;
*'''village_value=1''': (number ranging from 0 to 4) The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to define how much villages should be targeted by the move-to-targets CA.  In the Fast MAI, this defines which fraction of the AI units are moved toward village goals (see above for details).  If set to 0, villages are ignored as goals.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded to the same values as those of the respective [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|default candidate actions]].  ca_score is therefore not a valid optional key.&lt;br /&gt;
* The fast Micro AI modifies (removes) the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions.&lt;br /&gt;
&lt;br /&gt;
Enabling the Fast Micro AI in its default configuration is as simple as this usage example from the &amp;quot;Fast AI&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=fast_ai&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Generalized Goto Micro AI (ai_type=goto) ==&lt;br /&gt;
&lt;br /&gt;
The Goto Micro AI provides a highly configurable method of sending a unit (or units) to a location or set of locations.  The units to be moved are defined using a [[StandardUnitFilter|Standard Unit Filter]] (SUF) [alternatively, all side units are used if the SUF is not provided), while the goto locations are given in a [[StandardLocationFilter|Standard Location Filter]] (SLF).&lt;br /&gt;
&lt;br /&gt;
By default, each unit matching the SUF will move toward the closest goal (in terms of turns needed to get there) matching the SLF via the fastest route given the current situation on the map (locations of enemy units etc.), but this behavior can be influenced by the optional keys listed below.  Note that this means that a unit's goal might change from one turn to the next depending on the situation on the map.&lt;br /&gt;
&lt;br /&gt;
The behavior of the unit(s) once they reach their goal location(s) is configurable as well.  By default, they will stay there until the Goto Micro AI is deleted or changed.&lt;br /&gt;
&lt;br /&gt;
For demonstrations of several Goto Micro AI usages, check out the &amp;quot;Goto&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Goto Micro AI by using&lt;br /&gt;
 ai_type=goto&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Goto specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the possible goal locations for the move.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists). Note that the area defined by [avoid] is not entered at all, even if the end point of the move would lie outside the area again.&lt;br /&gt;
* '''avoid_enemies''': (number&amp;gt;0) By default, units controlled by the Goto Micro AI will not take enemy units into account for route finding, other than the enemies' ZoCs.  By contrast, if this key is set, they will try to avoid traversing hexes on which they can be attacked by enemies.  The larger the value of ''avoid_enemies'', the more the units will try to stay out of the enemies' way.  Notes:&lt;br /&gt;
** Units are willing to take a path that is 'avoid_enemies' movement points longer for each enemy unit that can be avoided by doing so for each hex along the path.  As an example, if avoid_enemies=1 and the shortest path crosses 2 hexes that can be reached by 3 enemies each, the unit is willing to take a path that is up to 6 hexes longer instead.  If avoid_enemies=10 in the same situation, the unit will take a path up to 60 hexes longer.&lt;br /&gt;
** Hexes adjacent to enemy units are always strongly avoided, independent of the value of avoid_enemies.&lt;br /&gt;
** avoid_enemies is ignored if 'use_straight_line=true' is set.&lt;br /&gt;
** Due to an issue with the path finding code, '''tunnels and teleports are ignored in Wesnoth 1.14 and before''' if 'avoid_enemies' is used. This has been fixed in Wesnoth 1.15.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units to be moved.  Defaults to all units of the side if not defined.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''ignore_enemy_at_goal'''=no and '''ignore_units'''=no: (boolean)  In the default configuration, the Goto MAI does not move units if the path to all goal hexes is blocked by enemies.  It is therefore possible to disable the Goto MAI by putting enemy units on all the goal hexes.  That can be changed if either of these two keys is set to 'yes'.&lt;br /&gt;
** 'ignore_enemy_at_goal=yes' makes the AI choose its goals while disregarding any enemy units at the goal hexes.  Even so, a stiff rating penalty is applied to goal hexes occupied by enemies, thus that unoccupied goal hexes are chosen unless all possible goals are occupied by enemies.  Note that this setting ''only'' ignores enemy units at the goal hexes themselves.  It is still possible to disable the AI by putting enemies all around the goals.&lt;br /&gt;
** 'ignore_units=yes' let's the AI ignore ''all'' enemy units when deciding which goal hex(es) to move toward.&lt;br /&gt;
** In both cases, enemies are only ignored for deciding which unit to move toward which goal hex.  For the actual move itself, all enemies and their ZoCs are considered as usual.&lt;br /&gt;
** Both options only take effect if neither avoid_enemies is used, nor use_straight_line is set to 'yes', as neither of these consider enemies as blockers. Or in other words, if avoid_enemies is used, or use_straight_line=yes, it is unnecessary to set ignore_enemy_at_goal or ignore_units to yes.&lt;br /&gt;
* '''release_unit_at_goal'''=no: (boolean) By default, a unit that reaches a goal location will stay there until the Goto Micro AI is deleted or changed (or until the conditions specified in the [goto_goals] SLF change).  If this parameter is set, a unit that has reached its goal will not be controlled by this Micro AI any more starting from the next turn.&lt;br /&gt;
* '''release_all_units_at_goal'''=no: (boolean) Same as ''release_unit_at_goal'', but this will deactivate the Goto Micro AI for all units once the first unit reaches a goal.&lt;br /&gt;
* '''remove_movement'''=yes: (boolean):  {{DevFeature1.15|11}}  By default, when the Goto MAI cannot move a unit (for example, because the path is blocked by enemies), its movement points are still removed. If this option is set to 'no', this step does not happen, so that other AI candidate actions (or even another Goto MAI with different parameters) can take over instead.&lt;br /&gt;
* '''unique_goals'''=no: (boolean)  If set to 'yes', each unit will be sent to a different goal location.  Note that the best goals are recalculated each turn, so a unit might choose a different goal this turn from what it was heading for during the previous turn if the situation on the map has changed.  Also note that if there are more units than goal locations, the left over units will not be handled by the Goto MAI but follow default behavior instead.&lt;br /&gt;
* '''use_straight_line'''=no: (boolean)  By default, units choose the route by which they will get to their goal locations in the smallest number of turns.  If this parameter is set to 'yes', a straight line route (or something pretty close to it at least) will be chosen instead, irrespective of whether this leads into a dead end, whether it is blocked by enemies, etc.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* Depending on the parameters used, the Goto Micro AI might require a lot of path finding to be done.  Thus, while it is possible to set up this AI to move many units to many different locations, a usage like that might require quite a long calculation time.&lt;br /&gt;
&lt;br /&gt;
===Goto Micro AI example usages:===&lt;br /&gt;
&lt;br /&gt;
Here are several examples of goto [micro_ai] tag usages from the &amp;quot;Goto&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
* Send a unit toward a castle (similar to the RCA AI's Goto Candidate Action, but avoiding dead ends and areas ZoCed by enemies)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
             ca_id=messenger1&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=messenger1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=6-7,2-4&lt;br /&gt;
                 terrain=C*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             ca_score=210001&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send all units of the side toward the southern border of the map&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=8&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                  y=33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             avoid_enemies=1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send units to the four corners of the map (with each unit going to a different corner).&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x=1,1,44,44&lt;br /&gt;
                 y=1,33,1,33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             unique_goals=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Set up a guardian unit that doesn't move while no enemy unit is within 8 hexes (ironically, the Goto Micro AI can be used to keep units in place, although using one of the Guardian Micro AIs might often be a better idea)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=guard1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 [filter]&lt;br /&gt;
                     id=guard1&lt;br /&gt;
                 [/filter]&lt;br /&gt;
                 [not]&lt;br /&gt;
                     [filter]&lt;br /&gt;
                         side=1&lt;br /&gt;
                     [/filter]&lt;br /&gt;
                     radius=8&lt;br /&gt;
                 [/not]&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Guardian Micro AIs ==&lt;br /&gt;
=== General Comments on the Guardian Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Guardian Micro AIs can be used to set up different kind of guardian behaviors for a unit or a set of units.  Four different types of guardians are available and are described below.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different guardian behaviors, check out the &amp;quot;Guardians&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
=== Return Guardian  (ai_type=return_guardian)===&lt;br /&gt;
&lt;br /&gt;
A 'return guardian' is a variation of the standard Wesnoth guardian. It has an assigned guard position (GP) to which it returns after attacks on approaching enemies:&lt;br /&gt;
* If at GP with no enemy in reach, do nothing.&lt;br /&gt;
* If at GP with an enemy in reach, attack. Note that the attack is done by the default AI, which might result in the guardian not attacking if the enemy is deemed too strong (this can be influenced by setting the default ''aggression'' value to a different value).&lt;br /&gt;
* If not at GP, return there, no matter whether an enemy is in reach or not.&lt;br /&gt;
* If enemies are blocking the way back to GP, do your best to move around them.&lt;br /&gt;
* If the guardian ends up next to an enemy on the way back, attack that enemy after the move.&lt;br /&gt;
&lt;br /&gt;
Enable the Return Guardian Micro AI by using&lt;br /&gt;
 ai_type=return_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''return_x,return_y''': The coordinate of the hex the unit returns to after each attack.  {{DevFeature1.15|0}} '''return_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=100010: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  This AI consist of 2 CAs, one with score &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt; the other with &amp;lt;tt&amp;gt;ca_score-20&amp;lt;/tt&amp;gt;.  The default value is set so that the first CA gets executed right before the default combat CA, and the other right after.  It usually will not make sense to change this value, but it is possible to do so for custom applications.  {{DevFeature1.13|6}}: This is now changed to 100100 and 99900 in order to avoid conflicts with the new high_XP_attacks candidate action.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a return guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=return_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=return1&lt;br /&gt;
             return_x,return_y=20,2&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
===Stationed Guardian (ai_type=stationed_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A 'stationed guardian' is another variation of the standard Wesnoth guardian with a somewhat more complex behavior than that of the 'return guardian'. Two positions are defined for it, a 'station' and a 'guarded location', as well as a distance. The behavior is as follows:&lt;br /&gt;
* If no enemy is within 'distance' of the guard's current position, do nothing.  This is independent of the guardian being at its station or not.&lt;br /&gt;
* Otherwise: If an enemy is within 'distance' of the guard, but not also within the same distance of the guarded location and the station (all of this simultaneously), move the guard in the direction of the station (or keep the guard in place if the station is unreachable).&lt;br /&gt;
* Otherwise:&lt;br /&gt;
** Pick the enemy unit that is closest to the guarded location.&lt;br /&gt;
** If we can reach it, pick the adjacent hex with the highest defense rating and attack from there.&lt;br /&gt;
** If not in reach, move toward this unit.&lt;br /&gt;
&lt;br /&gt;
Enable the Stationed Guardian Micro AI by using&lt;br /&gt;
 ai_type=stationed_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance parameter as explained above&lt;br /&gt;
* '''station_x,station_y''': The x and y position of the hex that serves as the guardians station. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''guard_x, guard_y''': The x and y position of the guarded location.  {{DevFeature1.13|0}}  These are now optional keys, defaulting to station_x,station_y if not provided. {{DevFeature1.15|0}} '''guard_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a stationed guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=stationed_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=stationed1&lt;br /&gt;
             distance=4&lt;br /&gt;
             station_x,station_y=2,14&lt;br /&gt;
             guard_x,guard_y=3,13&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Coward (ai_type=coward) ===&lt;br /&gt;
&lt;br /&gt;
Cowards are units that, like guardians, sit around doing nothing until an enemy comes into range.  Unlike guardians, however, they run away once enemies approach. Applications might be wild animals, unarmed civilians getting in the way of a battle, etc.  The coward macro can be called with two optional locations, 'seek' and 'avoid':&lt;br /&gt;
* If neither is given, the coward retreats to the position farthest away from the approaching enemies.&lt;br /&gt;
* If 'seek' is given, it preferentially goes toward that location (but getting away from enemies takes priority).&lt;br /&gt;
* If 'avoid' is given, it in addition tries to avoid that location (with both maximizing distance from enemies and going toward 'seek' taking priority).&lt;br /&gt;
* Both 'seek' and 'avoid' may consist of only one coordinate ('x' or 'y'), in which case not a single hex, but a line of hexes is sought or avoided.&lt;br /&gt;
&lt;br /&gt;
Enable the Coward Micro AI by using&lt;br /&gt;
 ai_type=coward&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance within which an enemy must be to &amp;quot;scare&amp;quot; the unit&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_if_trapped'''='no':  {{DevFeature1.13|0}}  With the default settings, a coward never attacks.  If this key is set to 'yes', the coward attacks the weakest (lowest hitpoints) enemy next to which it ends up after moving (such as a cornered animal would do, blindly lashing out without regards for its own safety).&lt;br /&gt;
* '''avoid_x, avoid_y''': The x and y coordinate of the hex to avoid. {{DevFeature1.15|0}} '''avoid_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units from which the cowards flee.  If not set, this defaults to all enemy units.  Note, however, that no &amp;quot;enemy of AI side&amp;quot; condition is added if [filter_second] is specified, meaning that it can include allied and/or enemy units as desired (and even units on the same side as the coward).&lt;br /&gt;
* '''seek_x, seek_y''': The x and y coordinate of the hex to seek. {{DevFeature1.15|0}} '''seek_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a coward [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=coward&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=coward3&lt;br /&gt;
             distance=5&lt;br /&gt;
             seek_x,seek_y=24,5&lt;br /&gt;
             avoid_x,avoid_y=24,15&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Zone guardian (ai_type=zone_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A zone guardian is a unit that, as the name says, guards a zone. It moves randomly inside this zone until an enemy enters it (or a separately defined enemy zone, see below). Applications might be the defense of a castle, a nesting area or for protecting an unit.  The zone macro can be called with an optional SLF, [filter_location_enemy]:&lt;br /&gt;
* If not specified, the zone guard attacks any enemy coming inside its guard zone.&lt;br /&gt;
* If [filter_location_enemy] is given, it attacks any enemy entering this zone and once there are no more enemies, it goes back to patrol in its basic zone.&lt;br /&gt;
&lt;br /&gt;
Enable the Zone Guardian Micro AI by using&lt;br /&gt;
 ai_type=zone_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone to guard.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_enemy]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone where enemies have to be attacked.  If not given, it defaults to [filter_location].&lt;br /&gt;
* '''station_x,station_y''': By default, the zone guardian moves randomly within the zone if there is no enemy to attack.  However, if station_x/y is given, it moves to this position instead during turns when it does not have an enemy to attack.  This position does not need to be inside the zone defined above.  Note: obviously it does not make sense to define [filter_location], [filter_location_enemy] and station_x/y all at the same time.  Only [filter_location] and either one of the latter two (or none of them) should be provided for any zone guardian. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a zone guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=zone_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=zone3&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=22-31,4-11  # This is intentionally chosen to extend past the lake&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             station_x,station_y=32,8&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Hang Out Micro AI (ai_type=hang_out) ==&lt;br /&gt;
&lt;br /&gt;
The Hang Out Micro AI instructs units of the AI side to &amp;quot;hang out&amp;quot; around a certain location, or locations, until a condition for mobilizing the side is met.  If none of the optional keys are given, the AI controls all units of the side to hang around the leader while moving off castle tiles to make room for recruiting.&lt;br /&gt;
&lt;br /&gt;
The AI works by moving each unit to its best hex first using the minimum amount of movement points required for this move.  Once all AI units are in position, movement points are set to zero for all of them simultaneously such that they cannot be used by other AI candidate actions.  This is done because the mobilizing condition might be met as result of one of the moves of the Hang Out AI, after which we want units to be able to continue with other AI actions.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Hang Out and Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Hang Out Micro AI by using&lt;br /&gt;
 ai_type=hang_out&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Hang Out specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  In its default configuration, the Hang Out MAI controls all units of a side, keeping them as close to the side leader(s) as possible while moving off castle tiles to make room for recruiting.  For this purpose, the candidate action score defaults to 170,000, which is just below the score of the default recruiting CA.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''': A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  If neither of these two exists, this parameter is set to default to all castle terrain (&amp;lt;tt&amp;gt;terrain=C*,C*^*,*^C*&amp;lt;/tt&amp;gt;) so that units move off the castle to make room for recruiting.  Note that this does not include keep terrain, otherwise the leader would be moved off the keep as well.&lt;br /&gt;
* '''ca_score'''=170000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 170,000, which means that it is executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Hang Out action (&amp;lt;tt&amp;gt;side=&amp;lt;/tt&amp;gt; is always set and does not need to be provided).  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the locations ''near'' which the AI units should hang out.  This does not mean that the units only move onto terrain specified here, but that they minimize the distance to such terrain.  It defaults to the position of the side leader(s).&lt;br /&gt;
* '''[mobilize_condition]''':  Contains ([[ConditionalActionsWML#Condition_Tags|WML Condition Tags]])  Once the condition set up here has been met, the Hang Out MAI is disabled and the side goes over to other AI activity.  The Hang Out MAI remains disabled after this, even if the condition is not met any more later in the scenario.&lt;br /&gt;
* '''mobilize_on_gold_less_than''': (number) Since side gold cannot be checked in a WML condition tag without storing it in a variable first, this key is provided separately.  It disables the Hang Out Micro AI once the side gold goes below the value specified here for the first time.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
* The Hang Out AI still takes effect if no locations to hang out at are found or if all map locations are to be avoided.  In that case, it simply keeps all units at their current locations.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Hang Out Micro AI configuration from the &amp;quot;Hang Out and Messenger&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hang_out&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [avoid]&lt;br /&gt;
                 terrain=C*,H*,M*,A*,S*,*^F*&lt;br /&gt;
             [/avoid]&lt;br /&gt;
             [mobilize_condition]&lt;br /&gt;
                 [have_unit]&lt;br /&gt;
                     side=1&lt;br /&gt;
                     count=7-99&lt;br /&gt;
                 [/have_unit]&lt;br /&gt;
             [/mobilize_condition]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Healer Support Micro AI (ai_type=healer_support) ==&lt;br /&gt;
&lt;br /&gt;
The Healer Support Micro AI configures the healers of a side to stay behind the battle lines and heal injured and/or threatened units rather than participate in combat under all circumstances.  You can set different levels of aggressiveness for the healers.  &lt;br /&gt;
&lt;br /&gt;
'''Notes'''&lt;br /&gt;
* By default, ''all'' healers of the AI side are controlled by this AI.  This includes in particular also the side leader, if the leader is a healer.  If you do not want the leader to be included (and possibly be moved away from the keep etc.) use the [filter] tag with canrecruit=no (see below).&lt;br /&gt;
* It is not always better to use this Micro AI than the default AI, as it takes healers (which can be strong units) away from the pool of units to choose for attacks.&lt;br /&gt;
* {{DevFeature1.15|3}} This Micro AI now takes unit guardian status and the passive_leader aspect into account.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Healers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Healer Support Micro AI by using&lt;br /&gt;
 ai_type=healer_support&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Healer Support specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the healer units.  If set, only units on the AI side passing this filter ''and'' having the healing ability are considered by this Micro AI.&lt;br /&gt;
*'''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting units to receive healing. If set, only units on the AI side passing this filter ''and'' the other healee selection criteria will be selected for healing by this Micro AI.&lt;br /&gt;
* '''aggression'''=1.0: (float) Sets the aggressiveness of the AI.  This parameter is set up as a float to accommodate future functionality, but it currently acts as a boolean: if set to zero, the AI will never let its healers participate in combat, for all other values it allows them to attack after all other units have attacked and if the healers cannot find any more units to support.  The former behavior might be appropriate in scenarios with only one or few valuable healers, while the latter might work better in scenarios with many healers.&lt;br /&gt;
* '''injured_units_only'''=no: (boolean) If set to 'yes', the AI will only move healers next to units that are already injured and skip units that currently have full hitpoints, but might get injured during the next enemy turn.&lt;br /&gt;
* '''max_threats'''=9999: (integer) The maximum allowable number of enemies that can attack a hex in order for it to be considered for a healer.  As an example, setting 'max_threats=0' means that the AI only moves healers to locations that are entirely safe from the enemy (assuming that none of the units currently on the map dies).  Note that the value of this key is checked against the number of enemies that can make it to the hex, not the number of adjacent hexes from which the healer could be attacked.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI. Note: as attacks by the healers are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The Healer Support Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded.  It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Healer Support Micro AI configuration from the &amp;quot;Healers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             aggression=0&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Lurkers Micro AI (ai_type=lurkers) ==&lt;br /&gt;
&lt;br /&gt;
The Lurker Micro AI controls some or all units of a side in &amp;quot;lurker mode&amp;quot;.  A lurker is a unit that is capable of moving across most terrains, but that only stops on and attacks from specific terrain.  It might also have the ability to hide on this terrain (which is the reason why it is called a lurker).&lt;br /&gt;
&lt;br /&gt;
Lurkers move individually without strategy and always attack the weakest enemy within their reach.  If no enemy is in reach, the lurker does a random move instead - or it just sits and waits (lurks).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of different types of lurker behavior, check out the &amp;quot;Lurkers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Lurkers Micro AI by using&lt;br /&gt;
 ai_type=lurkers&lt;br /&gt;
in the [micro_ai] tag&lt;br /&gt;
&lt;br /&gt;
===Lurkers specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
* '''[filter]''': Specifies which units of the side are controlled by the Lurker Micro AI.  This is a [[StandardUnitFilter|Standard Unit Filter]].  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': Specifies the terrain from which the lurkers attack. This is a [[StandardLocationFilter|Standard Location Filter]].&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_wander]''': Specifies the terrain on which lurkers end their moves if there is no enemy in reach. This is a [[StandardLocationFilter|Standard Location Filter]].  If [filter_location_wander] is not given, it defaults to [filter_location].&lt;br /&gt;
* '''stationary'''=no: (boolean)  If set to yes, a lurker never moves if there is no enemy within attack range.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Lurkers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=4&lt;br /&gt;
             ai_type=lurkers&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Naga Fighter&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=W*,S*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             [filter_location_wander]&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location_wander]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Messenger Escort Micro AI (ai_type=messenger_escort) ==&lt;br /&gt;
&lt;br /&gt;
The Messenger Escort Micro AI lets you define a set of waypoints through which it tries to move one or several of its units, the messenger(s).  The other units escort the messenger, trying to protect it and attacking enemies that are in its way.  Note that a messenger might not strictly hit intermediate waypoints, but that getting close is good enough, especially if a waypoint is occupied by an enemy unit.  It does, however, always try to get to the last waypoint.&lt;br /&gt;
&lt;br /&gt;
'''Notes on the use of several messengers:''' {{DevFeature1.13|0}}  &lt;br /&gt;
* This Micro AI can control several messengers at the same time, which can but do not have to move in a group.  They can be put on the map altogether in one location, or be spread out on the map or across different turns.&lt;br /&gt;
* The farther a messenger has moved through the chain of waypoints, the higher a priority it is for the AI, meaning that farther advanced (on the map) messengers are moved first, and that the escort units try to clear the way and stay with those messengers more so than with those further back on the map.  This order can be inverted using the invert_order= key.&lt;br /&gt;
* If several messengers are on the map, escort units might move from one to the other, the AI making a judgement call where they are needed most.  Closeness to a messenger is generally the most important criterion, but not the only one.&lt;br /&gt;
* If a messenger dies, the escort units previously staying with it now join one of the other messengers if there are any left (otherwise they follow default AI behavior).&lt;br /&gt;
* If you want certain escort units to stay always with the same messenger, use several messenger [micro_ai] tags with different [filter]/[filter_second] combinations.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Messenger Escort Micro AI by using&lt;br /&gt;
 ai_type=messenger_escort&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Messenger Escort specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': {{DevFeature1.13|0}} (Previous to 1.13.0, only id= was allowed.) The id of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated list of waypoint coordinates through which the messenger will go in the given order to reach its goal, given by the last coordinate.  If you want the messenger to go directly to the goal, simply enter a single x,y coordinate here.  Note that the messenger does not have to hit each waypoint exactly (except for the final one), getting close to them is good enough. {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|11}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Messenger Escort AI consists of 3 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-2&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_death_chance'''=0.67 and '''messenger_death_chance'''=0.0: (both floats; ranging from 0 to 1)    If the messenger is adjacent to an enemy at the end of the move, it will only attack the enemy if the chance of death of the enemy is &amp;gt;= enemy_death_chance and if the messenger's chance of death is &amp;lt;= messenger_death_chance (or if it is an attack at a range for which the enemy does not have a weapon).&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|0}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the escort units.  If not set, this defaults to all enemy units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''invert_order'''=no: {{DevFeature1.13|0}} If set to 'yes', the order of priority with which messenger units are considered is inverted, starting with the messenger unit that is the least advanced on the map (see description above).  In most cases, selecting this option results in worse performance of the AI, so use it with caution.  (And obviously it has no effect at all if there is only one messenger controlled by the AI.)&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|11}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Messenger Escort&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=messenger_escort&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=messenger&lt;br /&gt;
             waypoint_x=31,24,27,28&lt;br /&gt;
             waypoint_y=20,14,7,1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Patrol Micro AI (ai_type=patrol) ==&lt;br /&gt;
&lt;br /&gt;
The defines a number of waypoints for a unit (or units) to follow.  Options can be set whether the route should be followed in a loop, as an out-and back, only once, whether units encountered along the way should be attacked, etc.&lt;br /&gt;
&lt;br /&gt;
With the default settings, getting to the next waypoint takes priority over attacking for a patroller, but this can be overridden (as of '''Wesnoth 1.15.12''', using the '''attack_range''' option).  Unless instructed otherwise, the AI thus prefers to move the patrol unit around enemies rather than straight for them.  Also, if a waypoint is occupied by a unit the AI is not instructed to attack, it will (eventually) abandon that waypoint once it gets close enough and move on to the next one.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different patrol behaviors, check out the &amp;quot;Patrols&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Patrol Micro AI it by using&lt;br /&gt;
 ai_type=patrol&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Patrol specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated lists of the waypoint coordinates through which the patrol travels (in order). {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack''': By default (if this key is not set), the patrol unit attacks any unit it ends up next to after a move, or that is within '''attack_range''' (see below).  Alternatively, this parameter can be set to a comma-separated list of unit ids. If that is done, the patrol only attacks these units when it ends up next to them or if it gets within attack_range.  It ignores any other unit.  This can in turn be used to have the patroller not attack at all, by setting ''attack='' to a non-existing id.  The exception to this is an enemy on the last waypoint with ''one_time_only=yes'', which will always be attacked.&lt;br /&gt;
* '''attack_range'''=1: {{DevFeature1.15|12}} If the patrol comes within this many hexes of an enemy unit, it interrupts its patrol route and attacks that enemy (assuming it still has enough movement left to get to the enemy).  The '''attack=''' instructions are taken into account for this.  Note that the patrol generally tries to move around enemies, which means that for the default value of 1, enemies are usually not within attack range, except at the end of the move.&lt;br /&gt;
* '''attack_invisible_enemies'''=no: {{DevFeature1.15|12}} If set to 'yes', the unit also attacks invisible enemies (according to the criteria set by '''attack''' and '''attack_range''').&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''one_time_only'''=no: (boolean)  If set to 'yes', the unit goes through the patrol route only once and then hangs out at the last waypoint.  It also always attacks any enemy unit on the last waypoint, independent of what ''attack='' is set to.&lt;br /&gt;
* '''out_and_back'''=no: (boolean)  By default, a patrol unit returns to the first waypoint after reaching the last, doing the patrol route as a loop.  If this parameter is set to 'yes', the unit instead reverses its course and perpetually does out and backs of the route.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Patrols&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=patrol&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Konrad&lt;br /&gt;
             waypoint_x=9,24,25&lt;br /&gt;
             waypoint_y=21,23,15&lt;br /&gt;
             one_time_only=yes&lt;br /&gt;
             attack=Gertburt&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Protect Unit Micro AI (ai_type=protect_unit) ==&lt;br /&gt;
&lt;br /&gt;
The Protect Unit Micro AI tries to move one or several units to a given location while keeping them safe from enemy attacks. It does so by keeping the protected units away from enemy units and close to friendly units. Other units on the AI side are controlled by the default Wesnoth AI.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Protect Unit&amp;quot; and &amp;quot;The Elves Besieged&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Protect Unit Micro AI by using&lt;br /&gt;
 ai_type=protect_unit&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Protect Unit specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[unit]''': A separate [unit] tag is required for each protected unit(s).  Each tag has required keys '''id''', '''goal_x''', '''goal_y''' for each unit, as shown in the example below. {{DevFeature1.15|0}} '''goal_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''disable_move_leader_to_keep'''=no: (boolean) If set to 'yes', side leaders protected by this AI do not try to return to their keep.  This is necessary for this Micro AI to work with units that are side leaders and that are supposed to move to an off-keep location.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
*The Protect Unit Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].  Depending on the parameters, it might also modify the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|move leader to keep candidate action]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded. It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;HttT: The Elves Besieged&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=protect_unit&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Delfador&lt;br /&gt;
                 goal_x,goal_y=1,2&lt;br /&gt;
             [/unit]&lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
                 goal_x,goal_y=1,1&lt;br /&gt;
             [/unit]&lt;br /&gt;
 &lt;br /&gt;
             disable_move_leader_to_keep=true&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Recruiting Micro AI==&lt;br /&gt;
=== General Comments on the Recruiting Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Recruiting Micro AI replaces the default recruitment AI by alternative recruiting patterns.  Currently there are two recruiting algorithms you can choose from, random recruiting and the rush recruiting used in the new (as of Version 1.11.1) Experimental AI (see below).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of a different recruiting pattern, check out the &amp;quot;Recruiting&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  You can also explore the unit choices by watching the Experimental AI in a multiplayer game.&lt;br /&gt;
&lt;br /&gt;
'''Note:''' Obviously, the Recruitment Micro AI modifies the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|recruitment candidate action]].&lt;br /&gt;
&lt;br /&gt;
=== Random Recruitment AI (ai_type=recruit_random) ===&lt;br /&gt;
&lt;br /&gt;
This AI randomly selects a unit type from the recruitment list of the side.  The probability of choosing each type and the behavior in low-gold situations can be influenced with optional parameters.&lt;br /&gt;
&lt;br /&gt;
Enable the Random Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_random&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* '''[probability]''': This tag sets the probability of selecting the given unit type.  It has two required sub-keys:&lt;br /&gt;
** '''type''': comma-separated list of unit types to which this probability is to be applied&lt;br /&gt;
** '''probability''': (non-negative float)  The probability with which these unit types is to be selected.  All unit types for which no [probability] tag is defined receive a default probability of 1.0, thus values should be chosen relative to that.  As an example, in the code below swordsmen and peasants are given a probability of 8, while no other units have assigned probabilities.  Thus, each of these two unit types is 8 times more likely to be selected than any other individual unit type.&lt;br /&gt;
* '''skip_low_gold_recruiting'''=no: (boolean)  By default, the random recruitment AI chooses a unit to recruit only from those types for which it has sufficient gold.  If this parameter is set to 'yes', it will instead choose from all available unit types and end recruiting for the turn if there is not enough gold to recruit it this turn (even if other, affordable unit types exist).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a random recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_random&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [probability]&lt;br /&gt;
                 type=Swordsman,Peasant&lt;br /&gt;
                 probability=8&lt;br /&gt;
             [/probability]&lt;br /&gt;
 &lt;br /&gt;
             skip_low_gold_recruiting=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Rush Recruitment AI (ai_type=recruit_rushers) ===&lt;br /&gt;
&lt;br /&gt;
This AI replaces the default recruiting with the recruiting used in the Experimental AI [[Experimental_AI#Recruitment|as described here]].&lt;br /&gt;
&lt;br /&gt;
Enable the Rush Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_rushers&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* {{DevFeature1.15|2}} '''high_level_fraction'''=0: (non-negative number) The approximate fraction of units of level 2 or higher to be recruited. This is defined as fraction of units on the map, not of new units being recruited during a turn or over several turns (which makes a difference if there are already units on the map). The effect is also cumulative per level (starting from level 2), meaning that 1/3 of L2 units will be recruited in the example above, 1/3*1/3=1/9 L3s etc. The default value is zero, which leaves the high-level unit recruiting to the default Experimental AI algorithm resulting usually in very few such units being recruited.&lt;br /&gt;
* '''randomness'''=0.1: (number)  A random number is applied to the rusher recruit engine's score to prevent the recruitment pattern from being too predictable. 0 causes no randomness to be applied, while larger numbers increase the random effect. A value of 1-2 generates results in which the random effect is approximately equal to the scored effect. Extremely high values are essentially entirely random.&lt;br /&gt;
* {{DevFeature1.17|6}} '''reset_cache_each_turn'''=no: (bool) Many of the operations of the rush recruitment evaluation are expensive and are therefore cached. For the most part, this cache can be kept from turn to turn, but in case there are events that change, for example, the map or the recruit list, this parameter can be set to 'yes' to force a reset of the cache each turn.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a rush recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_rushers&lt;br /&gt;
             action=add&lt;br /&gt;
          [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Simple Attack Micro AI (ai_type=simple_attack) ==&lt;br /&gt;
&lt;br /&gt;
The Simple Attack Micro AI is meant as an addition to the default combat candidate action for when one wants to force the AI to do attacks that it would otherwise not do (for example on units that are 1 XP from leveling) or to execute certain attacks before all others (for example, attack with all &amp;quot;expendable&amp;quot; units first).  It is not set up to be a replacement for the default attacks.  Some notes:&lt;br /&gt;
&lt;br /&gt;
* The algorithm is different from and simpler than that of the default combat CA.  For example, no attack combinations are considered, only individual attacks.  As a result, the [[AiWML#List_of_AI_Aspects|default aspects]] (aggression, caution, [avoid], ...) are not taken into account.  Also, if lots of units are involved, evaluation of this Micro AI is potentially quite a bit slower than of the default attacks.&lt;br /&gt;
&lt;br /&gt;
* If an attack matching the Standard Unit Filters described below is found, this attack will always be executed, no matter how bad the odds are.  Thus, this Micro AI can be used to force the AI to do attacks it would otherwise avoid.&lt;br /&gt;
&lt;br /&gt;
* Other than selecting the attacker/defender units, this AI is currently not configurable.  Let us know if you would like to see certain options to be added.  (No guarantees that we will add them, but we will definitely consider them.)&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Simple Attack&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=simple_attack&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Simple Attack specific keys for the [micro_ai] tag===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  It is, however, in general not desirable to use this AI without at least one of the optional Standard Unit Filters below, as this AI is not meant as a replacement of the default combat candidate action.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=110000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 110,000, which means that it is executed right before the default combat candidate action.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Simple Attack action.  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units the AI should attack.  It defaults to all enemy units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
* '''weapon'''=-1: (integer) The number of the weapon that should be used for this attack.  Weapon numbers start at 1 (not 0).  -1 stands for &amp;quot;best weapon&amp;quot; (by whatever criterion the AI chooses to use).   '''Available from Wesnoth 1.11.8'''&lt;br /&gt;
&lt;br /&gt;
Below are 2 examples of Simple Attack Micro AI configurations from the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  The first one shows how to force the AI to attack with all Soulless and Walking Corpse units first:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110000&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless,Walking Corpse&lt;br /&gt;
             [/filter]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
The second example executes attacks by Soulless on all enemy units that are 1 XP from leveling.  Note that the Lua code in the preload event is there simply in order to code the &amp;quot;1 XP from advancing&amp;quot; condition.  It is not needed for most other SUFs, as can be seen in the previous example.&lt;br /&gt;
     [event]&lt;br /&gt;
         name=preload&lt;br /&gt;
         first_time_only=no&lt;br /&gt;
         [lua]&lt;br /&gt;
             code=&amp;lt;&amp;lt;&lt;br /&gt;
                 function close_to_advancing(unit)&lt;br /&gt;
                     if (unit.experience &amp;gt;= unit.max_experience-1) then&lt;br /&gt;
                         return true&lt;br /&gt;
                     else&lt;br /&gt;
                         return false&lt;br /&gt;
                     end&lt;br /&gt;
                 end&lt;br /&gt;
             &amp;gt;&amp;gt;&lt;br /&gt;
         [/lua]&lt;br /&gt;
     [/event]&lt;br /&gt;
 &lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110001&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless  # No Walking Corpses; L0 units don't advance enemy&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 lua_function = &amp;quot;close_to_advancing&amp;quot;&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
Several Simple Attack Micro AIs can be combined for the same AI side.  The two examples above are both used (note the different values for ca_score) in the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
== Custom Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|5}}&lt;br /&gt;
&lt;br /&gt;
From 1.13.5 onwards, adding a new Micro AI can be easily done without altering any core files or overriding the micro_ai tag. The [micro_ai] tag now looks up its ''ai_type'' parameter in the &amp;lt;code&amp;gt;wesnoth.micro_ais&amp;lt;/code&amp;gt; table and uses the micro AI setup function found there to determine which keys are required and allowed and what candidate actions to add.&lt;br /&gt;
&lt;br /&gt;
A micro AI setup function takes as its single parameter the contents of the [micro_ai] tag and returns three separate values:&lt;br /&gt;
*'''required''': A list of required keys and tags, as strings. Tags are indicated by enclosing them within square brackets in the string. If a key or tag in this list is missing from the [micro_ai] tag, an error will be printed.&lt;br /&gt;
*'''optional''': Similar to above, a list of optional keys and tags, as strings. No error will be printed if a key or tag in this list is missing, nor will an error be printed if it is present (as would be the case for unrecognized keys or tags).&lt;br /&gt;
*'''ca_params''': A table specifying the candidate actions that constitute the micro AI. It should contain an ''ai_id'' key (which specifies a base ID to be used for candidate actions of this micro AI) and a series of candidate action definitions. Each candidate action definition is a table containing the following keys:&lt;br /&gt;
**'''ca_id''': The ID used for the candidate action. This will be combined with the ''ai_id'' to produce the final ID.&lt;br /&gt;
**'''location''': Path to the Lua file that defines the candidate action.&lt;br /&gt;
**'''score''': The maximum score for the candidate action. If the [micro_ai] tag specified a ''ca_score'' parameter, the score here should normally be based on that.&lt;br /&gt;
&lt;br /&gt;
(The return statement would thus look like &amp;lt;syntaxhighlight lang='lua' inline&amp;gt;return required, optional, ca_params&amp;lt;/syntaxhighlight&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
'''Examples''' of micro AI setup functions can be found in the [https://github.com/wesnoth/wesnoth/tree/1.14/data/ai/micro_ais/mai-defs default MAI definitions directory].  See the [https://github.com/wesnoth/wesnoth/blob/1.14/data/ai/micro_ais/mai-defs/animals.lua#L4-L12 Big Animals MAI definition] for a simple example.  For custom micro AIs, the setup code should be put into a ''[lua]'' tag inside a preload event.&lt;br /&gt;
&lt;br /&gt;
Since the function has full access to the contents of the [micro_ai] tag, it can conditionally add candidate actions to '''ca_params''' depending on the values of other keys within the [micro_ai] tag. It can also perform other actions such as adding facets to an aspect; if it does such things, it should pay attention to the ''action'' key to determine whether it's being added or removed.&lt;br /&gt;
&lt;br /&gt;
There are two useful helper functions that can be used by Micro AI setup functions requiring aspect changes. They can be brought into scope by a line similar to the following:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='lua&amp;gt;local MAIH = wesnoth.require(&amp;quot;ai/micro_ais/micro_ai_helper.lua&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* MAIH.add_aspects(side, aspect_params)&lt;br /&gt;
* MAIH.delete_aspects(side, aspect_params)&lt;br /&gt;
&lt;br /&gt;
The ''aspect_params'' parameter is an array of tables, each of which contains the following keys:&lt;br /&gt;
*'''aspect''': The aspect to change.&lt;br /&gt;
*'''facet''': The WML defining the facet. This must include an ''id'' key in order for the aspect to be correctly deleted. For maximum compatibility, incorporate the ''ca_id'' key from the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Experimental_AI&amp;diff=69647</id>
		<title>Experimental AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Experimental_AI&amp;diff=69647"/>
		<updated>2022-06-20T15:36:35Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Recruitment */ add [args] parameters&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Experimental AI Summary ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} Several of the Experimental AI candidate actions (CAs) '''have been merged into the [[RCA_AI|default (RCA) AI]]''', see descriptions below for the individual CAs.&lt;br /&gt;
&lt;br /&gt;
An ''Experimental AI'' is available for use in both MP maps and SP scenarios.  At the moment, this AI contains the following [[Wesnoth_AI_Framework#The_Candidate_Actions_of_the_main_loop_Stage|candidate actions]]:&lt;br /&gt;
* New and improved recruiting&lt;br /&gt;
* More aggressive village grabbing&lt;br /&gt;
* Healer placement behind injured units and more assertive retreating of injured units to healing locations.&lt;br /&gt;
* Leader castle switching: useful on certain MP maps&lt;br /&gt;
* Poison spreading&lt;br /&gt;
* Moving toward any enemy, if there are no enemy leaders or villages&lt;br /&gt;
* All the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|default RCA AI candidate actions]] except for recruiting&lt;br /&gt;
&lt;br /&gt;
== Experimental AI Setup ==&lt;br /&gt;
&lt;br /&gt;
In multiplayer maps, this AI is available from the game setup menu as 'Experimental AI'.  In single-player scenarios, it can be included by using the following code in a [side] tag&lt;br /&gt;
 [ai]&lt;br /&gt;
     # Deprecated version (for Wesnoth 1.14 and earlier)&lt;br /&gt;
     {EXPERIMENTAL_AI}&lt;br /&gt;
 [/ai]&lt;br /&gt;
{{DevFeature1.15|0}} The EXPERIMENTAL_AI Macro is deprecated now. The Experimental AI should be included as follows instead (this works in Wesnoth 1.14 already):&lt;br /&gt;
 [ai]&lt;br /&gt;
     # Current version (works from Wesnoth 1.14 on)&lt;br /&gt;
     ai_algorithm=experimental_ai&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Note that the EXPERIMENTAL_AI macro must be inside &amp;lt;code&amp;gt;[side][ai]&amp;lt;/code&amp;gt;, it does not work with &amp;lt;code&amp;gt;[modify_side]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[modify_ai]&amp;lt;/code&amp;gt;. If the &amp;lt;code&amp;gt;ai_algorithm&amp;lt;/code&amp;gt; version is used, the Experimental AI can also be added as&lt;br /&gt;
     [modify_side]&lt;br /&gt;
         side=2&lt;br /&gt;
         [ai]&lt;br /&gt;
             ai_algorithm=experimental_ai&lt;br /&gt;
         [/ai]&lt;br /&gt;
     [/modify_side]&lt;br /&gt;
or&lt;br /&gt;
     [modify_side]&lt;br /&gt;
         side=2&lt;br /&gt;
         switch_ai=ai/ais/ai_generic_rush.cfg  # Wesnoth 1.14 and earlier&lt;br /&gt;
         #switch_ai=ai/ais/ai_experimental.cfg # Wesnoth 1.15 and later&lt;br /&gt;
     [/modify_side]&lt;br /&gt;
&lt;br /&gt;
=== Custom Parameter Setup ===&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|2}}&lt;br /&gt;
A small number of custom parameters can be passed to the Experimental AI. These cannot be set up using the &amp;lt;code&amp;gt;ai_algorithm&amp;lt;/code&amp;gt; syntax, but require the use of a macro in the &amp;lt;code&amp;gt;[side][ai]&amp;lt;/code&amp;gt; tag:&lt;br /&gt;
 [side]&lt;br /&gt;
     [ai]&lt;br /&gt;
         {CUSTOMIZABLE_EXPERIMENTAL_AI (&lt;br /&gt;
             high_level_fraction=0.33&lt;br /&gt;
             randomness=0.2&lt;br /&gt;
         )}&lt;br /&gt;
     [/ai]&lt;br /&gt;
 [/side]&lt;br /&gt;
Note that only the parameters one wants changed need to be defined in the macro, those omitted will take their default values.&lt;br /&gt;
&lt;br /&gt;
'''Custom parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''high_level_fraction'''=0: (non-negative number) The approximate fraction of units of level 2 or higher to be recruited. This is defined as fraction of units on the map, not of new units being recruited during a turn or over several turns (which makes a difference if there are already units on the map). The effect is also cumulative per level (starting from level 2), meaning that 1/3 of L2 units will be recruited in the example above, 1/3*1/3=1/9 L3s etc. The default value is zero, which leaves the high-level unit recruiting to the default Experimental AI algorithm resulting usually in very few such units being recruited.&lt;br /&gt;
* '''randomness'''=0.1: (number)  A random number is applied to the Experimental AI recruiting score to prevent the recruitment pattern from being too predictable. 0 causes no randomness to be applied, while larger numbers increase the random effect. A value of 1-2 generates results in which the random effect is approximately equal to the scored effect. Extremely high values are essentially entirely random.&lt;br /&gt;
&lt;br /&gt;
== Experimental AI Details ==&lt;br /&gt;
&lt;br /&gt;
The Experimental AI uses most of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|candidate actions]] (CAs) of the [[RCA AI]], the one exception being the recruitment CA which is replaced by another recruiting CA.  In addition, it also adds a number of new CAs.  The following are the differences between the Experimental AI and the default (RCA) AI:&lt;br /&gt;
&lt;br /&gt;
====Recruitment====&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} At the moment, the recruiting CA remains the main difference between the default and the Experimental AI.&lt;br /&gt;
&lt;br /&gt;
The Experimental AI has a completely different recruiting algorithm that was designed to emulate the choices of a human player, especially in the first few turns.  As such, it tries to pick units that counter both what is on the battlefield and that the opponent could recruit.  As it gains more units relative to the enemy, it picks harder hitting units, even at the cost of fragility, to break through enemy lines.&lt;br /&gt;
&lt;br /&gt;
Unlike the default recruitment algorithm, it also chooses where the units are recruited in order to maximize the number of villages that can be captured and if it knows the leader will be moving to a &amp;quot;better&amp;quot; keep (see below), it will under-recruit at the first keep to project more forward power, recruiting just enough to capture all villages.&lt;br /&gt;
&lt;br /&gt;
It also adjusts its recruitment based on map size, favouring faster units on larger maps, trusting that the economy advantage from capturing more villages will more than offset the price.&lt;br /&gt;
&lt;br /&gt;
It knows about poison and avoids recruiting units that depend on it for damage if the enemy is immune.&lt;br /&gt;
&lt;br /&gt;
It also looks at the expected cost of the unit over the next few turns, which prevents the recruitment of too many fast weak units unless the additional gold from an expected capture of a village is enough to offset the increased cost.&lt;br /&gt;
&lt;br /&gt;
Time of day is taken into account and it prefers units that will reach the current closest enemy at the favoured time of day. This allows, for example, for a slight bias towards saurians during the day and drakes at night on small maps (because they will reach the fight at the right time).&lt;br /&gt;
&lt;br /&gt;
Because the AI cannot use them well, it does not accurately measure the damage done by a unit with berserk, causing them to almost never be recruited.&lt;br /&gt;
&lt;br /&gt;
There is also a small amount of randomness added to allow two units that are almost equally good according to the evaluation to be chosen evenly, instead of only picking the first one every time.&lt;br /&gt;
&lt;br /&gt;
'''Configurable parameters:''' The [args] tag of the recruitment CA can take two parameters, '''high_level_fraction''' and '''randomness''' to modify its behavior.  They are described under the [[Micro_AIs#Rush_Recruitment_AI_.28ai_type.3Drecruit_rushers.29|Rush Recruitment AI]] entry on the Micro AI wiki page.&lt;br /&gt;
&lt;br /&gt;
====Villages====&lt;br /&gt;
&lt;br /&gt;
The Experimental AI prefers to capture villages over fighting. This weakens it somewhat in raw fighting but it tends to get enough extra gold that this is actually to its advantage vs. the default AI, although its current implementation is probably too focused on villages.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} This CA has '''not''' been merged into the default AI. It is too simple to work in a general setting (and therefore not suited for the default AI) and there is a separate village grabbing CA in the default AI, so it does not make much of a difference. This CA needs to be either improved or removed.&lt;br /&gt;
&lt;br /&gt;
====Healing and Retreating====&lt;br /&gt;
&lt;br /&gt;
If a healer is not used to attack, the Experimental AI will try to heal units, instead of positioning to attack later.&lt;br /&gt;
&lt;br /&gt;
In a separate CA, units retreat to villages/healers when injured.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} These two CAs have been merged into the default AI.&lt;br /&gt;
&lt;br /&gt;
====Castle Switching====&lt;br /&gt;
&lt;br /&gt;
The leader will move to different keeps, providing a forward force. Usually this works very well, especially at the beginning of the game, but it keeps moving later, which sometimes might get it killed in some scenarios. In that case, it might be worth stopping this behaviour after several turn by removing this CA.&lt;br /&gt;
&lt;br /&gt;
====Enemy Poisoning====&lt;br /&gt;
&lt;br /&gt;
There is also an explicit poison attack routine that tries to poison unpoisoned units instead of repeatedly poisoning the same one.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} This CA has been merged into the default AI.&lt;br /&gt;
&lt;br /&gt;
====Move to Any Enemy====&lt;br /&gt;
&lt;br /&gt;
This is a fall-back CA that kicks in only when no other actions are taken. Its purpose is to not let the AI units sit idle on maps without enemy leaders and villages.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} This CA has been merged into the default AI.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=69625</id>
		<title>Micro AIs</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=69625"/>
		<updated>2022-06-13T02:41:16Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Rush Recruitment AI (ai_type=recruit_rushers) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== A Few General Words about Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
* Micro AIs allow Wesnoth campaign and scenario authors to add new AI functionality without the need for any AI programming. They bring new specialized behavior to Wesnoth that cannot be achieved, at least not easily, by adjusting default AI parameters (such as those described at [[AiWML]]) or with WML code.&lt;br /&gt;
&lt;br /&gt;
* A Micro AI is activated and configured (or deleted) via the [micro_ai] tag, requiring only a few lines of WML code.&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.6''', the Micro AI behavior was inconsistent when it came to '''hidden enemies and petrified units'''.  Some Micro AIs ignored those, while others did not.  In some case there was even a potential for AI errors when an AI unit ran into hidden or petrified enemies.  As of 1.13.6, this has all been fixed.  The Micro AIs now correctly and consistently ignore hidden units and treat petrified units as blocking the hex they are on but having no effect otherwise, in the same way as the default AI does.  '''Important notes:'''&lt;br /&gt;
** It is not possible to re-include hidden/petrified units via the Standard Unit Filters (SUFs) used by some of the Micro AIs. In fact, if this is attempted, the filters will generally not match any unit.&lt;br /&gt;
*** This only applies to SUFs which go directly into the [micro_ai] tag. Any SUF that is a sub-tag of another filter (for example, of a Standard Location Filter) is taken literally and returns all units matching the filter. If hidden or petrified units are to be excluded from such a sub-filter, that needs to be specified directly in that filter.&lt;br /&gt;
** We also note that, because all the Micro AIs are written in Lua, it is possible to [[Micro_AIs#Using_Development_.281.13.29_Version_Micro_AIs_in_your_Stable_.281.12.29_Version_Add-on|move a Micro AI's code into one's add-on]] and modify it to whatever scenario-specific behavior is desired (this does not only apply to hidden/petrified units, but to all behavior). Setting up general Micro AI behavior that works in every possible setting is not feasible, but adapting it to work (only) in the specific setting of a given scenario is often quite simple. If you do not know how to do this, please post a message on the Wesnoth forums and we will be happy to help you with it.&lt;br /&gt;
&lt;br /&gt;
* The '''[[AiWML#List_of_AI_Aspects|default AI aspects]] are, for the most part, not taken into account by the Micro AIs'''.  For example, unless noted otherwise, specifying a value for ''aggression'' does not change the MAI behavior, and locations specified in an ''[avoid]'' tag generally can be entered by units controlled by an MAI.  This is a feature of the Micro AIs, not a bug.  If you feel that a certain default AI aspect should also apply to a Micro AI, please let us know in the [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 Micro AIs Feedback thread].&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.7''', the Micro AIs did not all work reliably when the '''AI side is under shroud''' and it may have been necessary to turn shroud off for the side when using a Micro AI. {{DevFeature1.13|7}} The Micro AIs now all work for sides under shroud. They do this by disregarding shroud for terrain purposes when path finding, consistent with default Wesnoth AI behavior, while still ignoring hidden units (including those hidden under shroud) correctly.&lt;br /&gt;
&lt;br /&gt;
* Most Micro AIs in their default setting apply to all units of a side, although many of them provide parameters for filtering the units that should be affected by the new behavior.  If the Micro AI is applied only to part of the units of a side, the other units follow the default Wesnoth AI behavior.&lt;br /&gt;
&lt;br /&gt;
* Micro AIs are available in mainline since Wesnoth 1.11/1.12.&lt;br /&gt;
&lt;br /&gt;
== Micro AI Test and Demo Scenarios ==&lt;br /&gt;
&lt;br /&gt;
All Micro AIs described here can be checked out in a number of test and demo scenarios.  The easiest way to access the test scenarios is by assigning a hotkey to &amp;quot;Start Test Scenario&amp;quot; in the preferences (as of Wesnoth 1.13.8). Then, simply press the hotkey in the titlescreen and select the desired scenario from the list (e.g. 'micro_ai_test' for the overall &amp;quot;switchboard scenario&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Test scenarios can also be accessed from the command line by typing&lt;br /&gt;
 path/wesnoth-executable -t micro_ai_test&lt;br /&gt;
or (depending on your system and Wesnoth version)&lt;br /&gt;
 path/wesnoth-executable -tmicro_ai_test&lt;br /&gt;
This starts up a &amp;quot;switchboard scenario&amp;quot; in which you can select the Micro AI demonstration scenario you want to check out.  Here, ''path/wesnoth-executable'' needs to be replaced by whatever the path and filename of the Wesnoth executable is on your system.&lt;br /&gt;
&lt;br /&gt;
== Setting up a Micro AI ==&lt;br /&gt;
&lt;br /&gt;
Setting up a Micro AI requires only one simple step:&lt;br /&gt;
==== Activating and configuring (or deleting) a Micro AI ====&lt;br /&gt;
&lt;br /&gt;
Micro AIs are activated, deleted and configured using the [micro_ai] tag.  This tag needs to be placed in [[ActionWML]], that is, in an event, a menu option or the like.  As an example, the following code activates the healer_support Micro AI in its default configuration for Side 2 from the beginning of the scenario:&lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
 &lt;br /&gt;
         # Configure the healer support micro AI&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
'''Keys required for all Micro AIs:'''&lt;br /&gt;
All [micro_ai] tags must contain the following three required keys:&lt;br /&gt;
* '''side''': The side for which the Micro AI is to be added, changed or deleted.  This has to be an individual side number, lists of sides are not supported.&lt;br /&gt;
* '''ai_type''': The type of Micro AI to be added, changed or deleted.  See the following sections for allowed values.&lt;br /&gt;
* '''action''' (string): The action to take concerning the Micro AI.  The following values are allowed: &lt;br /&gt;
** ''add'': Add a Micro AI to a side.&lt;br /&gt;
** ''change'': Change the configuration of an existing Micro AI.  Note that this does not only change the specific parameters provided in the tag, but it replaces the entire existing Micro AI by a new version with the new (and only the new) configuration.  It is therefore equivalent to using first the ''delete'' and then the ''add'' action.&lt;br /&gt;
** ''delete'': Delete an existing Micro AI from a side.&lt;br /&gt;
&lt;br /&gt;
'''Keys required under certain circumstances:'''&lt;br /&gt;
* '''ca_id''': (string) If several MAIs of the same type are used for the same side and one or several of these are to be changed or deleted during a scenario, this key needs to be included so that the [micro_ai] tag knows which of them needs to be changed/deleted.  The same is also true if a Micro AI is changed more than once during a scenario.  We therefore '''strongly recommend always setting ''ca_id''''' for MAIs which are, or might be, changed or deleted during a scenario.&lt;br /&gt;
** '''Note:''' The id(s)/name(s) actually assigned to the CA(s) of the MAI by the engine might be different from the parameter passed here.  It is, however, sufficient to use this ca_id with the [micro_ai] tag add/delete/change actions to identify the MAI uniquely.  (In other words: you don't have to worry about this if you always use the [micro_ai] tag, but might have to figure out the actual name(s) of the CA(s) if you want to delete or change them manually.)&lt;br /&gt;
* Other keys may also be required depending on the Micro AI.  In additional, a number of optional keys may be available to configure the AI behavior.  See the sections on the individual Micro AIs for details.&lt;br /&gt;
&lt;br /&gt;
'''Required keys for deleting a Micro AI:'''&lt;br /&gt;
&lt;br /&gt;
Only a subset of the keys required for setting up a Micro AI are needed when deleting it.  '''side''', '''ai_type''' and '''action'''=delete always need to be provided.  '''ca_id''' needs to be given if it was provided when adding the Micro AI (see above for when this is needed).&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* The ''add'' and ''change'' actions ignore all keys that do not apply to the respective Micro AI type.  The ''delete'' action ignores all keys other than the required keys listed above.&lt;br /&gt;
&lt;br /&gt;
* Some of the Micro AIs use and/or modify default AI components (candidate actions or aspects) when they are added, or reset these components to their default values when they are deleted.  Thus, if you have modified the AI yourself using the default CAs or aspects, some of these modifications might be affected, or might be interfering with the Micro AI.  The sections below indicate whether a Micro AI modifies any default AI components.&lt;br /&gt;
&lt;br /&gt;
===Combining Different Micro AI Types for the Same Side===&lt;br /&gt;
&lt;br /&gt;
No extra steps need to be taken when combining different Micro AIs on the same side.  Simply set up the [micro_ai] tags for all AIs you want to use.&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* While it is ''technically'' possible to combine any number of Micro AIs, this does not mean that all Micro AIs work together in practice.  Some are not compatible because they control units in contradictory ways.  &lt;br /&gt;
* Not all combinations that ''should'' work have been tested yet.  If you find a combination that you believe should work but doesn't, [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 please let us know] and we will see if that can be fixed.&lt;br /&gt;
* The candidate action score almost certainly has to be set (using the ca_score= key) for one or several of the MAIs if several of them are used on the same side.&lt;br /&gt;
&lt;br /&gt;
== Animals Micro AIs ==&lt;br /&gt;
=== General Comments on the Animal Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Animals Micro AIs are meant to simulate the behaviors of certain types of animals.  They are, however, set up so that they can be used with arbitrary unit types and might be applicable in quite diverse situations.  The AIs nevertheless remain named after the animals for which they were written originally, so that they are (to some extent) descriptive of their behavior.&lt;br /&gt;
&lt;br /&gt;
Brief descriptions of the AI behaviors are given in the following subsections.  To get a better feeling for their behavior, check out the &amp;quot;Animals&amp;quot;, &amp;quot;Swarm&amp;quot;, &amp;quot;Wolves&amp;quot; and &amp;quot;Dragon&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  Note that some of the animal AIs are designed for animal types that do not exist in mainline.  In the test scenario, other animal types are therefore substituted for them.&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Wolves (ai_type=wolves) ===&lt;br /&gt;
&lt;br /&gt;
The Wolves Micro AI organizes all &amp;quot;wolf&amp;quot; units (predators) of the side to hunt in a single pack.  They actively chase after the closest prey and try to corner it (not always super successfully), but are easily distracted by other enemy units coming into attack range, to the point where the pack might split up.  The wolves can, however, be told to attack only prey units as well as to try to avoid other types of units (such as larger predators).  When no prey is left, the wolves wander randomly.&lt;br /&gt;
&lt;br /&gt;
This AI works best for a small number of predators (2 - 5) and sparse prey.  Also, the predators should be put on the map in a pack, that is, reasonably close to each other.  For a demonstration of the Wolves AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Note that another wolves AI exists, called [[Micro_AIs#Animals_AI:_Multi-pack_Wolves_.28ai_type.3Dwolves_multipacks.29|Multipack Wolves]], which distributes the wolves of a side into several packs, rather than all of them into one single pack as it is done here.&lt;br /&gt;
&lt;br /&gt;
Enable the Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the predator units.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the prey units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_only_prey'''=no: In the default configuration, the wolves attack all enemy units in attack range.  If this key is set to 'yes', they only attack prey units.&lt;br /&gt;
* '''avoid_type''': Comma-separated list of unit types (on enemy sides) which the wolves try to avoid.  They try to stay out of these units' way as much as possible when moving around (except when they are moving in to attack another enemy unit) and never attack them.&lt;br /&gt;
* '''ca_score'''=90000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 90,000, which is lower than the default combat CA, but higher than most other default CAs which move units.  It can be changed by setting this parameter but in most cases this will not make sense.  Note that the Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Notes on default (RCA) AI aspects:''' &lt;br /&gt;
* If ''attack_only_prey'' or ''avoid_type'' are set, the Wolves Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* Attacks by the wolves are left to the default (RCA) AI, meaning that the value of ''aggression'' has an influence on the behavior.&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI is taken into account. Note: as attacks by the wolves are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=6&lt;br /&gt;
             ai_type=wolves&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Wolf&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Vampire Bat&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             avoid_type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Multi-pack Wolves (ai_type=wolves_multipacks) ===&lt;br /&gt;
&lt;br /&gt;
The Multi-pack Wolves Micro AI is different from the [[Micro_AIs#Animals_AI:_Wolves_.28ai_type.3Dwolves.29|Wolves Micro AI]] in that there can be an arbitrary number of wolf packs, with the pack size being a free parameter.  At the beginning of the scenario, close wolves are grouped into packs in a semi-methodical way.  If the wolves are put on the map in distinct groups of close wolves of the correct number, they will be joined into packs matching these groups.  If, on the other hand, they are spread out all over the map, the method of assigning them to packs is semi-random.&lt;br /&gt;
&lt;br /&gt;
Wolves of the same pack begin by joining each other on the map.  After that, they stay together until only one wolf is left, which then tries to join up with an incomplete pack or with other single wolves.  Individual wolves entering the map during the scenario behave in that way as well.&lt;br /&gt;
&lt;br /&gt;
A second difference to the other Wolves AI is that wolves do not actively hunt here.  For the most part they just wander (often long distance).  However, the pack ferociously (and without regard for its own health) attacks any enemy units that come into range, as long as that does not mean separating the pack by more than a few hexes.  Staying together, or joining with a new wolf assigned to the pack, is the only thing that takes priority over satisfying the wolves' thirst for blood.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Multi-pack Wolves AI behavior, check out the &amp;quot;Wolves&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Multi-pack Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves_multipacks&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Multi-pack Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''pack_size'''=3: (integer) The size of the packs.&lt;br /&gt;
* '''show_pack_number'''=no: If set to &amp;quot;yes&amp;quot;, the wolves' pack numbers will be shown below them.  Note that this means that the pack number of a wolf killed in an attack will remain as a label on the map.  This behavior is intentional to help UMC authors note where units might have made suicidal mistakes.&lt;br /&gt;
* '''type'''=Wolf: Comma-separated list of the unit types which the AI controls. The default value is &amp;quot;Wolf&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves_multipacks [micro_ai] tag usage from the &amp;quot;Wolves&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=wolves_multipacks&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             show_pack_number=yes&lt;br /&gt;
             pack_size=4&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Big Animals (ai_type=big_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Big Animals Micro AI is a simulation of large predators that wander and hunt alone, such as Bears, Giant Spiders or Yetis.  For the most part, these just wander on the terrain that's been defined for them, but they attack enemy units if those happen to come into range.  The AI can be set up so that the Big Animals stay out of the way of other units (such as other large predators), in which case they only attack those if cornered or if they run into them accidentally (such as ambushers).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Big Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Big Animals Micro AI by using&lt;br /&gt;
 ai_type=big_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the animals to which this AI is applied.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': The big animals often go for long distance (multi-turn) journeys.  This tag is a [[StandardLocationFilter|Standard Location Filter]] describing the terrain from which the next goal is chosen.  The default is all terrain on the map. &lt;br /&gt;
* '''[filter_location_wander]''': The terrain on which the big animals will end their moves. This is a [[StandardLocationFilter|Standard Location Filter]].  The default is all terrain on the map.&lt;br /&gt;
* '''[avoid_unit]''': A [[StandardUnitFilter|Standard Unit Filter]] describing all enemy units that the big animals will avoid.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
Here's an example of big_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=big_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Ghast&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [avoid_unit]&lt;br /&gt;
                 type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
             [/avoid_unit]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=1-40,1-18&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Forest Animals (ai_type=forest_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Forest Animals Micro AI simulates a number of different animal behaviors that one might find in a forest, namely animals behaving like deer, rabbits and families of tuskers.  In the following, we will refer to those animals by these types, even though the actual unit types can be selected freely.  They behave as follows:&lt;br /&gt;
* Each deer wanders randomly on (selectable) terrain, except when enemies get in its (the deer's) maximum movement range, in which case it flees to the farthest point it can reach.&lt;br /&gt;
* Tuskers exhibit the same behavior as deer, except when an enemy is next to one of the tusklets.  This enemy is attacked unconditionally by the tuskers trying to protect their young.&lt;br /&gt;
* Tusklets blindly follow the closest adult tusker, except when there is no tusker left, in which case they behave the same as deer.&lt;br /&gt;
* Rabbits also wander randomly, but in addition disappear into their holes (if any are within reach) when enemies are close.  They reappear out of their holes at the beginning of the turn, if it is safe.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Forest Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Forest Animals Micro AI by using&lt;br /&gt;
 ai_type=forest_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Forest Animals AI consists of 4 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-3&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''rabbit_type, tusker_type, tusklet_type, deer_type''': Comma-separated lists of the unit types behaving as the respective animals.  If any of these parameters are not set, those types of animal behavior are skipped by the AI.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain on which the forest animals wander when they are not threatened by enemies.&lt;br /&gt;
* '''rabbit_number'''=6: (integer)  The number of rabbits for the AI to have on the map simultaneously.  The AI puts the missing number of rabbits onto the map at the beginning of the AI's turn (when the rabbits come out of their holes).&lt;br /&gt;
* '''rabbit_enemy_distance'''=3: (number)  Rabbits won't spawn in holes that aren't more than this distance away from all enemies.&lt;br /&gt;
* '''rabbit_hole_img''': Rabbit holes are marked by items on the map.  If this parameter is set, it must be the file name of the item image.  If it is not set, all items on the map are treated as rabbit holes.&lt;br /&gt;
** {{DevFeature1.14|10}} Items used for rabbit holes on the map border used to produce an error. Placing them on the map border therefore needed to be avoided on the scenario setup side. They are now automatically excluded from consideration by the AI code.&lt;br /&gt;
&lt;br /&gt;
Here's an example of forest_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=forest_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             deer_type=Vampire Bat&lt;br /&gt;
             rabbit_type=Giant Rat&lt;br /&gt;
             tusker_type=Ogre&lt;br /&gt;
             tusklet_type=Young Ogre&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=*^F*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Herd and Herders (ai_type=herding) ===&lt;br /&gt;
&lt;br /&gt;
The Herding Micro AI sets up an area on the map where a number of herder units (such as sheep dog) control and protect a herd (such as sheep).  For convenience, we simply refer to them as sheep dogs and sheep in the following, even though arbitrary unit types can be selected for either.&lt;br /&gt;
&lt;br /&gt;
Sheep dogs try to keep their sheep safe.  This involves keeping them inside the herding area, positioning themselves in between the sheep and approaching enemies, and attacking the enemies if those get too close.  If no active herding or protecting move is needed, the dogs go to a random location on the perimeter of the herding area.&lt;br /&gt;
&lt;br /&gt;
Sheep wander aimlessly except when a sheep dog is next to them, in which case they run away from the dog.  The dogs exploit this by positioning themselves on the outside of the sheep, if possible.  Sheep also run away from approaching enemies.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Herding AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Herding Micro AI by using&lt;br /&gt;
 ai_type=herding&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herder units (e.g. dogs).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herd units (e.g. sheep).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain forming the perimeter of the herding area.  In other words, this is the path the herders (dogs) use to patrol around the outside of the herding area.  This must be a set of coordinates ''completely enclosing'' the herding area.  Here's an example of setting up a rectangular perimeter:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         x=10-20,10-20,10,  ,20&lt;br /&gt;
         y=11,   21,   11-21,11-21&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
Another possibility is by defining the path by certain terrain on the map (as it is done in the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]), in which case the filter can simply look like this:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         terrain=Rb&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
* '''herd_x,herd_y''': The coordinate of one hex (''any'' one hex) inside the herding area.  This is needed to define which part of the map is on the ''inside'' of the herding area perimeter defined by [herding_perimeter], and which is the ''outside''. {{DevFeature1.15|0}} '''herd_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''attention_distance'''=8: (integer)  If an enemy is within this distance of a herd unit, but not as close as ''attack_distance'', the herders try to position themselves in between the enemy and the herd unit.&lt;br /&gt;
* '''attack_distance'''=4: (integer)  If an enemy is within this distance of a herd animal, the herders attack this enemy.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Herding AI consists of 6 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-5&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Herding [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=herding&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Footpad&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Troll Whelp&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             herd_x,herd_y=32,28&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=Rb&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Hunter (ai_type=hunter) ===&lt;br /&gt;
&lt;br /&gt;
The Hunter Micro AI leads a unit (or units) through hunt-and-rest cycles.  The units wander in a prescribed area of the map, their hunting ground, until an enemy unit comes within range.  Note that the units do not actively chase down enemies, but wander randomly through the hunting ground until an enemy is encountered.&lt;br /&gt;
&lt;br /&gt;
If enemies are within range of a hunter unit, the hunter attacks the weakest of them.  If a kill is made, it then retreats to its rest location, where it stays for a certain number of turns and until fully healed (whatever happens later).&lt;br /&gt;
&lt;br /&gt;
A few more details:&lt;br /&gt;
* If the hunter's way home is entirely blocked on the return (so that there is no possible path), the normal RCA AI takes over its behavior.&lt;br /&gt;
* However, if the way is blocked by an enemy unit occupying the rest hex, that enemy will be attacked unconditionally.&lt;br /&gt;
* A kill only makes the hunter go home when it is the attacker, not as defender.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Hunter AI behavior, check out the &amp;quot;Dragon&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]. &lt;br /&gt;
&lt;br /&gt;
Enable the Hunter Micro AI by using&lt;br /&gt;
 ai_type=hunter&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''home_x,home_y''': The coordinates of the hex to which the hunter returns and rests at after a kill. {{DevFeature1.15|0}} '''home_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the unit's hunting ground.  If this tag is not provided, the hunter uses all of the map.&lt;br /&gt;
* '''rest_turns'''=3: (integer)  The number of turns the AI stays at home_x,home_y after a kill.  It will not leave its home until this number of turns have passed ''and'' it is fully healed.&lt;br /&gt;
* '''show_messages'''=no: (boolean) If set to yes, the hunter announces whenever its behavior changes to the next phase.  Note that this is meant as debug output for the scenario author.  The messages are not translated and are not included in replays (and are only visible to the host in an MP game).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a hunter [micro_ai] tag usage from the &amp;quot;Dragon&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hunter&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Rowck&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=5-30,1-15&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             home_x,home_y=3,15&lt;br /&gt;
             rest_turns=2&lt;br /&gt;
 &lt;br /&gt;
             show_messages=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Swarm (ai_type=swarm) ===&lt;br /&gt;
&lt;br /&gt;
The Swarm Micro AI uses some very simply algorithms to simulate animal swarm behavior.  Without adjacent enemies, they simply do a random move, trying to stay together and at a certain distance from enemies.  However, if an enemy unit is close to any bat, the swarm scatters.  This is particular fun to watch when one places an enemy unit in the middle of the swarm.  After being scattered, the swarm members slowly rejoin, but not in a very organized way.  Sub-swarms or individual bats might roam around for quite some time before they find their way back.  It is also possible that individual bats (or small groups) split off from the larger swarm at times.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Swarm AI behavior, check out the &amp;quot;Swarm&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Swarm Micro AI by using&lt;br /&gt;
 ai_type=swarm&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Swarm AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_distance'''=5: (int) The minimum distance kept between units of the swarm and enemies when the swarm moves as a whole.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.15|12}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the units.  Only AI units matching this filter participate in the swarm.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''scatter_distance'''=3: (int) Enemies within &amp;quot;scatter_distance&amp;quot; hexes of any swarm unit cause the swarm to scatter, by each unit trying to maximize its individual distance from the enemy.&lt;br /&gt;
* '''vision_distance'''=12: (int) Only units within this distance follow the overall swarm motion (either away from an enemy or of the swarm as a whole).  The smaller this value is set, the less likely the swarm is to stay together or rejoin.  This parameter is meant to simulate how far an individual member of the swarm can see, meaning that the swarm is &amp;quot;out of sight&amp;quot; (and the unit will not be able to follow it) if it is more than this distance away.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Swarm [micro_ai] tag usage from the &amp;quot;Swarm&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=swarm&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Assassin Squad Micro AI (ai_type=assassin) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|6}}&lt;br /&gt;
&lt;br /&gt;
This Micro AI controls one or several assassin units which try to circle around enemy units with the goal of hitting a target unit without being distracted or impeded by other enemies.  The units will always attack the target if such attacks are possible, otherwise they will follow the path of least resistance toward it. Notes:&lt;br /&gt;
&lt;br /&gt;
* The main priority for the AI is to find paths that avoid enemy units. However, if no such path can be found, the assassins will still advance toward the target, taking the path exposing the units to the least potential damage from enemies.&lt;br /&gt;
&lt;br /&gt;
* The units will not necessarily follow the same path. The best path is chosen individually for each unit.&lt;br /&gt;
&lt;br /&gt;
* While it is not a requirement for the AI to work, using ambusher or skirmisher units, or units with the quick trait, makes this AI more lethal in many cases.&lt;br /&gt;
&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded, it does not work otherwise. That means that '''ca_score''' is not a valid key for the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
Enable the Assassin Squad Micro AI by using&lt;br /&gt;
 ai_type=assassin&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Assassin Squad specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the assassin units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the target unit.&lt;br /&gt;
** It is important to realize that the AI currently only works correctly with a single target unit. It is thus the responsibility of the scenario designer to make sure that this filter matches an individual enemy unit. If several enemy units are to be targeted in the same scenario, several instances of the Assassin Micro AI can be used.&lt;br /&gt;
** The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[prefer]''': A [[StandardLocationFilter|Standard Location Filter]] selecting locations on the map for the assassins to prefer when making their way toward the target unit. This could be used, for example, to make units with the ambush ability move (mostly) through forest or to have the assassins circle around the, say, northern part of the map rather than the south if that is desirable for scenario design purposes.&lt;br /&gt;
** As with avoiding enemies, the units will still move across non-preferred terrain if that is either the only option or ''much'' shorter than preferred terrain&lt;br /&gt;
** Attacking, once the target unit is in range, is not influenced by the [prefer] SLF.&lt;br /&gt;
&lt;br /&gt;
Here is an example of an Assassin Squad [micro_ai] tag usage:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=assassin&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Assassin,Master at Arms,Duelist&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             [prefer] # Circle around northern part of map&lt;br /&gt;
                 x=1-9, 10-99&lt;br /&gt;
                 y=1-37, 1-17&lt;br /&gt;
             [/prefer]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Bottleneck Defense Micro AI (ai_type=bottleneck_defense) ==&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense Micro AI lets you define a location on the map where the AI can take a defensive stance behind a narrow passage (bottleneck).  Units on the front line are backed up by healers and/or units with the leadership ability.  Injured units are moved to the back and replaced by uninjured (or less injured) units.  The units on the front line only attack when it is safe (no retaliation) or when there is a high chance that they will make a kill or level up.  Using this Micro AI only makes sense if there is no way for the enemy sides to move around the bottleneck and attack the AI's units from behind.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Bottleneck&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Bottleneck Defense Micro AI by using&lt;br /&gt;
 ai_type=bottleneck_defense&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Bottleneck Defense specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense AI requires two sets of coordinates that define where it should be taking up its defensive stance, and from which side the enemy attacks at that location.&lt;br /&gt;
&lt;br /&gt;
* '''x,y''': Comma separated lists of the hexes on the front line, where strong units are placed.  All hexes on which the AI makes contact with (can be attacked by) the enemy need to be included here.  Units are placed on them in the order in which they are listed here. {{DevFeature1.15|0}} '''location_id''' can be used instead with named/special locations.&lt;br /&gt;
* '''enemy_x,enemy_y''': Comma separated list of the hexes from which the enemy can attack the AI front line units.  This is needed for the AI to know on which side of the front line support units (healers etc.) need to be placed.  In many cases, it is sufficient to provide only one of the enemy front line hexes, but there are geometries for which that does not work.  It is therefore safer to list all enemy front line hexes here. {{DevFeature1.15|0}} '''enemy_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
By default, the AI places healers and units with the leadership ability both on the front line itself (if they are the strongest units available) and on hexes adjacent to and behind the line.  If different placement is desired, these locations can be overridden with the following keys.&lt;br /&gt;
&lt;br /&gt;
* '''active_side_leader'''=no: (boolean)  If set to 'yes', the side leader participates in the bottleneck defense action after the side's gold has been spent.  If set to 'no' (default), the leader follows default AI behavior (sitting on the keep and recruiting, for the most part).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Bottleneck Defense AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.15|3}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the units.  Only AI units matching this filter participate in the bottleneck defense.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''healer_x,healer_y''': Comma separated list of hexes where healers are placed.  This can be used to keep healers from the front line (or to take up only certain positions on the front line), and/or to override the default healing positions behind the line.  Healers are placed on them in the order in which the hexes are listed, with the exception that hexes on the line always take priority over hexes behind the line. {{DevFeature1.15|0}} '''healer_loc''' can be used instead with named/special locations.&lt;br /&gt;
* '''leadership_x,leadership_y''': Same as ''healer_x,healer_y'', but for units with the leadership ability. {{DevFeature1.15|0}} '''leadership_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
As an example, in the &amp;quot;Bottleneck&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]], there are three front line hexes, two of which are on hill terrain, while the third is a road with a lower defense rating.  The healer and side leader are supposed to participate in combat because they are strong units, but only from the hill hexes to keep them safer.  This is accomplished by using the following keys settings (check out the scenario to see which hex is which):&lt;br /&gt;
 [micro_ai]&lt;br /&gt;
     side=1&lt;br /&gt;
     ai_type=bottleneck_defense&lt;br /&gt;
     action=add&lt;br /&gt;
 &lt;br /&gt;
     x=14,14,14&lt;br /&gt;
     y= 7, 9, 8&lt;br /&gt;
     enemy_x=13,13&lt;br /&gt;
     enemy_y= 8, 9&lt;br /&gt;
 &lt;br /&gt;
     healer_x=14,14,15,15&lt;br /&gt;
     healer_y= 7, 9, 8, 9&lt;br /&gt;
     leadership_x=14,14,15,15&lt;br /&gt;
     leadership_y= 7, 9, 9 ,8&lt;br /&gt;
     active_side_leader=yes&lt;br /&gt;
 [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Fast Micro AI (ai_type=fast_ai) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|0}}&lt;br /&gt;
&lt;br /&gt;
This AI is meant as a fall-back AI for scenarios where there are so many units that attacks and moves done by the default AI are slow with long delays before each move.  It replaces the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions (CAs).  It should be obvious that in order to achieve the faster speed, compromises concerning the quality of the moves have to be made.&lt;br /&gt;
&lt;br /&gt;
Enable the Fast Micro AI by using&lt;br /&gt;
 ai_type=fast_ai&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Combat candidate action:'''&lt;br /&gt;
* Attackers are considered individually one at a time.  The best attack found for a unit is executed without checking whether a better attack by another unit might be possible.&lt;br /&gt;
* The default order in which attackers are considered is by decreasing hitpoints, starting with the strongest unit (in terms of HP).  This can be modified with the optional keys below.&lt;br /&gt;
* By default, if weaker units block attack hexes for a stronger unit, they do not move out of the way (this is the same as for the default AI).  If a hex becomes available after an attack (e.g. due to a weaker unit dying in the attack), all attacks that were possible ''before'' this happened are finished first.  Only after that are attacks by the stronger unit reconsidered.&lt;br /&gt;
* If include_occupied_attack_hexes=yes is set, weaker units move out of the way for stronger units if possible.  This can, however slow down the AI depending on the situation on the map.  Use with caution.&lt;br /&gt;
* {{DevFeature1.13|2}} Previous to this version, attacks by the leader were bunched in with the attacks by the other units.  Now leader attacks are treated separately and contain an extra layer of analysis to determine whether the attack position is &amp;quot;safe&amp;quot; (in an approximate way).  Currently, this is done with two simple considerations:&lt;br /&gt;
** Count all enemy units that can reach the leader's attack position.  If there are more than given by optional parameter 'leader_attack_max_units' (default value 3), do not attack.&lt;br /&gt;
** Add up the hitpoints of all enemies that can reach the leader's attack position.  If this value, multiplied by optional parameter 'leader_weight'(default value 2), is larger than the leaders hitpoints, do not attack.&lt;br /&gt;
** Two optional parameters, 'threatened_leader_fights' and 'leader_additional_threat', can also be used to let the leader attack when the threat at the attack hex is comparable to that at the leader's current position.&lt;br /&gt;
** In order to keep this AI as fast as possible, leader attacks are done by a separate candidate action that is only evaluated when all other CAs are done.&lt;br /&gt;
&lt;br /&gt;
'''Move-to-targets, villages and retreat candidate actions:'''&lt;br /&gt;
* The move-to-targets, villages and retreat candidate actions of the default AI are removed.  Move-to-targets and villages are taken over by the Fast Micro AI, whereas retreat is simply not done.&lt;br /&gt;
* This CA moves units toward goals after attacks are done.  Goals are unowned (if the AI side has a leader only) and enemy-owned villages and enemy leaders.&lt;br /&gt;
* For each goal, units are pre-sorted in order of the fewest moves needed to cover the ''distance'' toward the goal (that is, as if move cost on each hex toward the goal were 1).  Units are then considered sequentially and if a unit can get there &amp;quot;efficiently&amp;quot;, its move is executed without considering other units.&lt;br /&gt;
** A move is considered efficient if the move cost to get to the hex is not more than 'move_cost_factor' larger than the distance to the hex.  'move_cost_factor' is an optional key that defaults to 2.  It can be set to values of 1.1 or larger (see below).&lt;br /&gt;
** If the move for the current unit is not found to be efficient, the AI considers the next unit instead.  If no unit can do an efficient move toward the goal, the best (fastest) of the inefficient moves is selected instead.&lt;br /&gt;
&lt;br /&gt;
* Village goals:&lt;br /&gt;
** Village goals are considered before enemy leader goals so that the AI preferentially chooses scouts (or otherwise fast units) for them.&lt;br /&gt;
** The number of units sent toward villages depends on the value of the default AI aspect village_value.  It is set to village_value/4 times the number of units the AI has left to move.  Thus, by default (village_value=1.0) the AI sends a quarter of its units toward villages, and the rest toward enemy leaders.  This means that the meaningful range of values for village_value is 0..4.&lt;br /&gt;
** Only one unit is moved toward each village.  This consideration also overrides the number of units to move toward villages defined by village_value, topping it at the number of unowned/enemy-owned villages on the map.&lt;br /&gt;
** Setting village_value=0 disables moving units toward villages altogether.  That is, not only are no units chosen, but the village targeting phase of the AI is entirely disabled with no evaluation time used for it.  Note that, unlike with the default AI, this also means that no villages within range of an AI unit are grabbed.&lt;br /&gt;
** The order in which villages are chosen as goals is some combination of them being close to the AI side's leader and being spread out over the map.&lt;br /&gt;
&lt;br /&gt;
* Enemy leader goals:&lt;br /&gt;
** The order of enemy leaders goals is set according to their distance from the AI leader with the closest enemy leader being targeted first (if the AI has a leader, otherwise their order is random).  One unit is sent toward each enemy leader in this order, after which the AI starts with the first enemy leader again.  This is repeated until all units have moved.&lt;br /&gt;
&lt;br /&gt;
* Guardians (units with ai_special=guardian set) are ignored by the fast move-to-targets CA.&lt;br /&gt;
&lt;br /&gt;
'''Tips for speeding up/modifying the Fast Micro AI in specific scenarios:'''&lt;br /&gt;
&lt;br /&gt;
A number of optional keys and default AI aspects can be used to modify the behavior of the Fast Micro AI.  The MAI is set up so that it is a reasonable compromise between speed and quality of the moves in most situations.  However, this might not be ideal for all scenarios here are some things that can be done to speed up the AI (or to get better behavior in some situations by doing the opposite):&lt;br /&gt;
* Use large values for the default AI aspect aggression.  The largest delays before attacks happen (in some circumstances) when the Fast MAI has to consider lots of units with attacks left but decides not to attack with them.  See below for a description of how aggression is used by the fast AI.&lt;br /&gt;
* Use large values of move_cost_factor (see above for description of what this does).  If you want to emphasize units that can move across terrain easily (e.g. bats) to be chosen for village grabbing, you might want to use low values of move_cost_factor, but this might slow down the AI.  The default value should work reasonably well in most standard situations, but modifying the value might work better on specific maps.&lt;br /&gt;
* Don't use dungeon_mode=yes unless absolutely necessary, even in most dungeon crawls.  Test the AI without this setting first and only use it if it is the only way to solve the problem.  Often, a minor tweak to the map will have the same effect, without the speed penalty.&lt;br /&gt;
* Use include_occupied_attack_hexes=yes with caution.  It might (or might not) slow down the attack CA significantly, depending on the scenario.&lt;br /&gt;
* Set village_value=0, which makes the AI not consider villages as goals.  This has not been found to make a large difference on the maps tested so far, but it might have an effect on maps with a very large number of villages.&lt;br /&gt;
* Use attack_hidden_enemies=yes on maps where there are no hidden enemies. Excluding hidden enemies from the attack calculation requires an additional filter to be evaluated that takes extra time. In most cases this will not result in a large computation time saving.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_hidden_enemies'''=no: (boolean)  By default, hidden enemies are excluded from the attack calculation.  Setting this to 'yes' makes the AI also consider those enemies as attack targets.&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.13|2}} A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  As with the default AI, units will not step on hexes defined by the [avoid] tag.  In addition, no goals will be chosen that lie on those hexes.  The one minor exception is that a unit stepping out of the way for another unit might temporarily enter the avoided zone.&lt;br /&gt;
* '''dungeon_mode'''=no: (boolean)  As much as possible, the Fast Micro AI works with distances between hexes rather than path finding.  This works pretty well on open maps and even most dungeon crawls (and similar scenarios), but it might result in some units getting temporarily (and in rare cases permanently) stuck on maps with convoluted labyrinths.  If that is the case, setting dungeon_mode=yes triggers more path finding that avoids such situations (but as a result is somewhat slower).&lt;br /&gt;
* '''include_occupied_attack_hexes'''=no: (boolean) If set to 'yes', the AI moves weaker units out of the way for a stronger attacker.  This might lead to better attacks but is slower, so don't use if speed is essential.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the attackers.  Only AI units matching this filter will attack, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes the side= key set to the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the enemies.  Only enemy units matching this filter will be attacked, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''leader_weight'''=2: (number &amp;gt;= 0) {{DevFeature1.13|2}} A factor specifying how much more valuable the AI considers side leaders than non-leader units in the attack evaluation. This is also used to determine whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''leader_additional_threat'''=1: (number &amp;gt; 0) {{DevFeature1.13|2}} If threatened_leader_fights=yes, the threat at the attack position may be larger by this factor for the leader to leave the keep for an attack.&lt;br /&gt;
* '''leader_attack_max_units'''=3: (number &amp;gt;= 0) {{DevFeature1.13|2}} A number specifying how many enemy units within reach are acceptable when determining whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''move_cost_factor'''=2: (number &amp;gt;= 1.1)  The factor by which the move cost to a goal hex may be larger than the distance to the goal hex for a move to be considered &amp;quot;efficient&amp;quot;.  See description above.  1.1 is used if this is set to values smaller than 1.1.  Smaller values might lead to better behavior, while larger values might speed up the AI.&lt;br /&gt;
* '''weak_units_first'''=no: (boolean) If set to 'yes', inverts the order in which units are considered by the combat CA.  Attacks are then done in order of increasing hitpoints of the attackers.&lt;br /&gt;
* '''skip_combat_ca'''=no: (boolean) If set to 'yes', the fast combat CA is not used.  The default AI's high_xp_attack and combat CAs remains in place in this case.&lt;br /&gt;
* '''skip_move_ca'''=no: (boolean) If set to 'yes', the fast move-to-targets CA is not used.  The default AI's retreat, villages and move-to-targets CAs remains in place in this case.&lt;br /&gt;
* '''threatened_leader_fights'''=no: (boolean) {{DevFeature1.13|2}} By default, the AI leader will not attack if the threat at the attack position is too high.  If this key is set to 'yes', the leader will attack if the threat there is not larger than the threat at his current position multiplied by the value of 'leader_additional_threat'. This is meant as an option for the endgame: when the AI leader is under attack, he might as well go down fighting.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''aggression''': The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to set how aggressive the AI should attack.  It works in roughly the same way as the default AI aspect:&lt;br /&gt;
** The value of (1 - aggression) determines how much the AI values its own units compared to the enemy's units.  Thus, aggression=0 means that attacks are only done if the AI expects to deal (roughly) the same amount of damage as it will receive.  aggression=1 means that expected damage to the AI's units is not considered, only the expected damage to enemy units matters.&lt;br /&gt;
** Damage is calculated as a combination of hitpoints and chance to die, in &amp;quot;gold equivalent units&amp;quot;.&lt;br /&gt;
** Unlike the default AI, aggression is set to 1 for values larger than 1.  Allowed values are thus anything from -infinity to +infinity, although only -infinity to +1.0 make sense.&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.13|2}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
*'''attacks aspect''':  {{DevFeature1.13|2}} The standard RCA AI [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] used to filter own and enemy units for attacks is now also taken into account as long as neither [filter] nor [filter_second] (see above under 'optional keys') are defined in the [micro_ai] tag.&lt;br /&gt;
*'''village_value=1''': (number ranging from 0 to 4) The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to define how much villages should be targeted by the move-to-targets CA.  In the Fast MAI, this defines which fraction of the AI units are moved toward village goals (see above for details).  If set to 0, villages are ignored as goals.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded to the same values as those of the respective [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|default candidate actions]].  ca_score is therefore not a valid optional key.&lt;br /&gt;
* The fast Micro AI modifies (removes) the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions.&lt;br /&gt;
&lt;br /&gt;
Enabling the Fast Micro AI in its default configuration is as simple as this usage example from the &amp;quot;Fast AI&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=fast_ai&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Generalized Goto Micro AI (ai_type=goto) ==&lt;br /&gt;
&lt;br /&gt;
The Goto Micro AI provides a highly configurable method of sending a unit (or units) to a location or set of locations.  The units to be moved are defined using a [[StandardUnitFilter|Standard Unit Filter]] (SUF) [alternatively, all side units are used if the SUF is not provided), while the goto locations are given in a [[StandardLocationFilter|Standard Location Filter]] (SLF).&lt;br /&gt;
&lt;br /&gt;
By default, each unit matching the SUF will move toward the closest goal (in terms of turns needed to get there) matching the SLF via the fastest route given the current situation on the map (locations of enemy units etc.), but this behavior can be influenced by the optional keys listed below.  Note that this means that a unit's goal might change from one turn to the next depending on the situation on the map.&lt;br /&gt;
&lt;br /&gt;
The behavior of the unit(s) once they reach their goal location(s) is configurable as well.  By default, they will stay there until the Goto Micro AI is deleted or changed.&lt;br /&gt;
&lt;br /&gt;
For demonstrations of several Goto Micro AI usages, check out the &amp;quot;Goto&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Goto Micro AI by using&lt;br /&gt;
 ai_type=goto&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Goto specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the possible goal locations for the move.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists). Note that the area defined by [avoid] is not entered at all, even if the end point of the move would lie outside the area again.&lt;br /&gt;
* '''avoid_enemies''': (number&amp;gt;0) By default, units controlled by the Goto Micro AI will not take enemy units into account for route finding, other than the enemies' ZoCs.  By contrast, if this key is set, they will try to avoid traversing hexes on which they can be attacked by enemies.  The larger the value of ''avoid_enemies'', the more the units will try to stay out of the enemies' way.  Notes:&lt;br /&gt;
** Units are willing to take a path that is 'avoid_enemies' movement points longer for each enemy unit that can be avoided by doing so for each hex along the path.  As an example, if avoid_enemies=1 and the shortest path crosses 2 hexes that can be reached by 3 enemies each, the unit is willing to take a path that is up to 6 hexes longer instead.  If avoid_enemies=10 in the same situation, the unit will take a path up to 60 hexes longer.&lt;br /&gt;
** Hexes adjacent to enemy units are always strongly avoided, independent of the value of avoid_enemies.&lt;br /&gt;
** avoid_enemies is ignored if 'use_straight_line=true' is set.&lt;br /&gt;
** Due to an issue with the path finding code, '''tunnels and teleports are ignored in Wesnoth 1.14 and before''' if 'avoid_enemies' is used. This has been fixed in Wesnoth 1.15.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units to be moved.  Defaults to all units of the side if not defined.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''ignore_enemy_at_goal'''=no and '''ignore_units'''=no: (boolean)  In the default configuration, the Goto MAI does not move units if the path to all goal hexes is blocked by enemies.  It is therefore possible to disable the Goto MAI by putting enemy units on all the goal hexes.  That can be changed if either of these two keys is set to 'yes'.&lt;br /&gt;
** 'ignore_enemy_at_goal=yes' makes the AI choose its goals while disregarding any enemy units at the goal hexes.  Even so, a stiff rating penalty is applied to goal hexes occupied by enemies, thus that unoccupied goal hexes are chosen unless all possible goals are occupied by enemies.  Note that this setting ''only'' ignores enemy units at the goal hexes themselves.  It is still possible to disable the AI by putting enemies all around the goals.&lt;br /&gt;
** 'ignore_units=yes' let's the AI ignore ''all'' enemy units when deciding which goal hex(es) to move toward.&lt;br /&gt;
** In both cases, enemies are only ignored for deciding which unit to move toward which goal hex.  For the actual move itself, all enemies and their ZoCs are considered as usual.&lt;br /&gt;
** Both options only take effect if neither avoid_enemies is used, nor use_straight_line is set to 'yes', as neither of these consider enemies as blockers. Or in other words, if avoid_enemies is used, or use_straight_line=yes, it is unnecessary to set ignore_enemy_at_goal or ignore_units to yes.&lt;br /&gt;
* '''release_unit_at_goal'''=no: (boolean) By default, a unit that reaches a goal location will stay there until the Goto Micro AI is deleted or changed (or until the conditions specified in the [goto_goals] SLF change).  If this parameter is set, a unit that has reached its goal will not be controlled by this Micro AI any more starting from the next turn.&lt;br /&gt;
* '''release_all_units_at_goal'''=no: (boolean) Same as ''release_unit_at_goal'', but this will deactivate the Goto Micro AI for all units once the first unit reaches a goal.&lt;br /&gt;
* '''remove_movement'''=yes: (boolean):  {{DevFeature1.15|11}}  By default, when the Goto MAI cannot move a unit (for example, because the path is blocked by enemies), its movement points are still removed. If this option is set to 'no', this step does not happen, so that other AI candidate actions (or even another Goto MAI with different parameters) can take over instead.&lt;br /&gt;
* '''unique_goals'''=no: (boolean)  If set to 'yes', each unit will be sent to a different goal location.  Note that the best goals are recalculated each turn, so a unit might choose a different goal this turn from what it was heading for during the previous turn if the situation on the map has changed.  Also note that if there are more units than goal locations, the left over units will not be handled by the Goto MAI but follow default behavior instead.&lt;br /&gt;
* '''use_straight_line'''=no: (boolean)  By default, units choose the route by which they will get to their goal locations in the smallest number of turns.  If this parameter is set to 'yes', a straight line route (or something pretty close to it at least) will be chosen instead, irrespective of whether this leads into a dead end, whether it is blocked by enemies, etc.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* Depending on the parameters used, the Goto Micro AI might require a lot of path finding to be done.  Thus, while it is possible to set up this AI to move many units to many different locations, a usage like that might require quite a long calculation time.&lt;br /&gt;
&lt;br /&gt;
===Goto Micro AI example usages:===&lt;br /&gt;
&lt;br /&gt;
Here are several examples of goto [micro_ai] tag usages from the &amp;quot;Goto&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
* Send a unit toward a castle (similar to the RCA AI's Goto Candidate Action, but avoiding dead ends and areas ZoCed by enemies)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
             ca_id=messenger1&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=messenger1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=6-7,2-4&lt;br /&gt;
                 terrain=C*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             ca_score=210001&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send all units of the side toward the southern border of the map&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=8&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                  y=33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             avoid_enemies=1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send units to the four corners of the map (with each unit going to a different corner).&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x=1,1,44,44&lt;br /&gt;
                 y=1,33,1,33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             unique_goals=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Set up a guardian unit that doesn't move while no enemy unit is within 8 hexes (ironically, the Goto Micro AI can be used to keep units in place, although using one of the Guardian Micro AIs might often be a better idea)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=guard1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 [filter]&lt;br /&gt;
                     id=guard1&lt;br /&gt;
                 [/filter]&lt;br /&gt;
                 [not]&lt;br /&gt;
                     [filter]&lt;br /&gt;
                         side=1&lt;br /&gt;
                     [/filter]&lt;br /&gt;
                     radius=8&lt;br /&gt;
                 [/not]&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Guardian Micro AIs ==&lt;br /&gt;
=== General Comments on the Guardian Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Guardian Micro AIs can be used to set up different kind of guardian behaviors for a unit or a set of units.  Four different types of guardians are available and are described below.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different guardian behaviors, check out the &amp;quot;Guardians&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
=== Return Guardian  (ai_type=return_guardian)===&lt;br /&gt;
&lt;br /&gt;
A 'return guardian' is a variation of the standard Wesnoth guardian. It has an assigned guard position (GP) to which it returns after attacks on approaching enemies:&lt;br /&gt;
* If at GP with no enemy in reach, do nothing.&lt;br /&gt;
* If at GP with an enemy in reach, attack. Note that the attack is done by the default AI, which might result in the guardian not attacking if the enemy is deemed too strong (this can be influenced by setting the default ''aggression'' value to a different value).&lt;br /&gt;
* If not at GP, return there, no matter whether an enemy is in reach or not.&lt;br /&gt;
* If enemies are blocking the way back to GP, do your best to move around them.&lt;br /&gt;
* If the guardian ends up next to an enemy on the way back, attack that enemy after the move.&lt;br /&gt;
&lt;br /&gt;
Enable the Return Guardian Micro AI by using&lt;br /&gt;
 ai_type=return_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''return_x,return_y''': The coordinate of the hex the unit returns to after each attack.  {{DevFeature1.15|0}} '''return_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=100010: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  This AI consist of 2 CAs, one with score &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt; the other with &amp;lt;tt&amp;gt;ca_score-20&amp;lt;/tt&amp;gt;.  The default value is set so that the first CA gets executed right before the default combat CA, and the other right after.  It usually will not make sense to change this value, but it is possible to do so for custom applications.  {{DevFeature1.13|6}}: This is now changed to 100100 and 99900 in order to avoid conflicts with the new high_XP_attacks candidate action.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a return guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=return_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=return1&lt;br /&gt;
             return_x,return_y=20,2&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
===Stationed Guardian (ai_type=stationed_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A 'stationed guardian' is another variation of the standard Wesnoth guardian with a somewhat more complex behavior than that of the 'return guardian'. Two positions are defined for it, a 'station' and a 'guarded location', as well as a distance. The behavior is as follows:&lt;br /&gt;
* If no enemy is within 'distance' of the guard's current position, do nothing.  This is independent of the guardian being at its station or not.&lt;br /&gt;
* Otherwise: If an enemy is within 'distance' of the guard, but not also within the same distance of the guarded location and the station (all of this simultaneously), move the guard in the direction of the station (or keep the guard in place if the station is unreachable).&lt;br /&gt;
* Otherwise:&lt;br /&gt;
** Pick the enemy unit that is closest to the guarded location.&lt;br /&gt;
** If we can reach it, pick the adjacent hex with the highest defense rating and attack from there.&lt;br /&gt;
** If not in reach, move toward this unit.&lt;br /&gt;
&lt;br /&gt;
Enable the Stationed Guardian Micro AI by using&lt;br /&gt;
 ai_type=stationed_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance parameter as explained above&lt;br /&gt;
* '''station_x,station_y''': The x and y position of the hex that serves as the guardians station. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''guard_x, guard_y''': The x and y position of the guarded location.  {{DevFeature1.13|0}}  These are now optional keys, defaulting to station_x,station_y if not provided. {{DevFeature1.15|0}} '''guard_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a stationed guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=stationed_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=stationed1&lt;br /&gt;
             distance=4&lt;br /&gt;
             station_x,station_y=2,14&lt;br /&gt;
             guard_x,guard_y=3,13&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Coward (ai_type=coward) ===&lt;br /&gt;
&lt;br /&gt;
Cowards are units that, like guardians, sit around doing nothing until an enemy comes into range.  Unlike guardians, however, they run away once enemies approach. Applications might be wild animals, unarmed civilians getting in the way of a battle, etc.  The coward macro can be called with two optional locations, 'seek' and 'avoid':&lt;br /&gt;
* If neither is given, the coward retreats to the position farthest away from the approaching enemies.&lt;br /&gt;
* If 'seek' is given, it preferentially goes toward that location (but getting away from enemies takes priority).&lt;br /&gt;
* If 'avoid' is given, it in addition tries to avoid that location (with both maximizing distance from enemies and going toward 'seek' taking priority).&lt;br /&gt;
* Both 'seek' and 'avoid' may consist of only one coordinate ('x' or 'y'), in which case not a single hex, but a line of hexes is sought or avoided.&lt;br /&gt;
&lt;br /&gt;
Enable the Coward Micro AI by using&lt;br /&gt;
 ai_type=coward&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance within which an enemy must be to &amp;quot;scare&amp;quot; the unit&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_if_trapped'''='no':  {{DevFeature1.13|0}}  With the default settings, a coward never attacks.  If this key is set to 'yes', the coward attacks the weakest (lowest hitpoints) enemy next to which it ends up after moving (such as a cornered animal would do, blindly lashing out without regards for its own safety).&lt;br /&gt;
* '''avoid_x, avoid_y''': The x and y coordinate of the hex to avoid. {{DevFeature1.15|0}} '''avoid_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units from which the cowards flee.  If not set, this defaults to all enemy units.  Note, however, that no &amp;quot;enemy of AI side&amp;quot; condition is added if [filter_second] is specified, meaning that it can include allied and/or enemy units as desired (and even units on the same side as the coward).&lt;br /&gt;
* '''seek_x, seek_y''': The x and y coordinate of the hex to seek. {{DevFeature1.15|0}} '''seek_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a coward [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=coward&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=coward3&lt;br /&gt;
             distance=5&lt;br /&gt;
             seek_x,seek_y=24,5&lt;br /&gt;
             avoid_x,avoid_y=24,15&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Zone guardian (ai_type=zone_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A zone guardian is a unit that, as the name says, guards a zone. It moves randomly inside this zone until an enemy enters it (or a separately defined enemy zone, see below). Applications might be the defense of a castle, a nesting area or for protecting an unit.  The zone macro can be called with an optional SLF, [filter_location_enemy]:&lt;br /&gt;
* If not specified, the zone guard attacks any enemy coming inside its guard zone.&lt;br /&gt;
* If [filter_location_enemy] is given, it attacks any enemy entering this zone and once there are no more enemies, it goes back to patrol in its basic zone.&lt;br /&gt;
&lt;br /&gt;
Enable the Zone Guardian Micro AI by using&lt;br /&gt;
 ai_type=zone_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone to guard.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_enemy]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone where enemies have to be attacked.  If not given, it defaults to [filter_location].&lt;br /&gt;
* '''station_x,station_y''': By default, the zone guardian moves randomly within the zone if there is no enemy to attack.  However, if station_x/y is given, it moves to this position instead during turns when it does not have an enemy to attack.  This position does not need to be inside the zone defined above.  Note: obviously it does not make sense to define [filter_location], [filter_location_enemy] and station_x/y all at the same time.  Only [filter_location] and either one of the latter two (or none of them) should be provided for any zone guardian. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a zone guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=zone_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=zone3&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=22-31,4-11  # This is intentionally chosen to extend past the lake&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             station_x,station_y=32,8&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Hang Out Micro AI (ai_type=hang_out) ==&lt;br /&gt;
&lt;br /&gt;
The Hang Out Micro AI instructs units of the AI side to &amp;quot;hang out&amp;quot; around a certain location, or locations, until a condition for mobilizing the side is met.  If none of the optional keys are given, the AI controls all units of the side to hang around the leader while moving off castle tiles to make room for recruiting.&lt;br /&gt;
&lt;br /&gt;
The AI works by moving each unit to its best hex first using the minimum amount of movement points required for this move.  Once all AI units are in position, movement points are set to zero for all of them simultaneously such that they cannot be used by other AI candidate actions.  This is done because the mobilizing condition might be met as result of one of the moves of the Hang Out AI, after which we want units to be able to continue with other AI actions.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Hang Out and Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Hang Out Micro AI by using&lt;br /&gt;
 ai_type=hang_out&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Hang Out specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  In its default configuration, the Hang Out MAI controls all units of a side, keeping them as close to the side leader(s) as possible while moving off castle tiles to make room for recruiting.  For this purpose, the candidate action score defaults to 170,000, which is just below the score of the default recruiting CA.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''': A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  If neither of these two exists, this parameter is set to default to all castle terrain (&amp;lt;tt&amp;gt;terrain=C*,C*^*,*^C*&amp;lt;/tt&amp;gt;) so that units move off the castle to make room for recruiting.  Note that this does not include keep terrain, otherwise the leader would be moved off the keep as well.&lt;br /&gt;
* '''ca_score'''=170000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 170,000, which means that it is executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Hang Out action (&amp;lt;tt&amp;gt;side=&amp;lt;/tt&amp;gt; is always set and does not need to be provided).  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the locations ''near'' which the AI units should hang out.  This does not mean that the units only move onto terrain specified here, but that they minimize the distance to such terrain.  It defaults to the position of the side leader(s).&lt;br /&gt;
* '''[mobilize_condition]''':  Contains ([[ConditionalActionsWML#Condition_Tags|WML Condition Tags]])  Once the condition set up here has been met, the Hang Out MAI is disabled and the side goes over to other AI activity.  The Hang Out MAI remains disabled after this, even if the condition is not met any more later in the scenario.&lt;br /&gt;
* '''mobilize_on_gold_less_than''': (number) Since side gold cannot be checked in a WML condition tag without storing it in a variable first, this key is provided separately.  It disables the Hang Out Micro AI once the side gold goes below the value specified here for the first time.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
* The Hang Out AI still takes effect if no locations to hang out at are found or if all map locations are to be avoided.  In that case, it simply keeps all units at their current locations.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Hang Out Micro AI configuration from the &amp;quot;Hang Out and Messenger&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hang_out&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [avoid]&lt;br /&gt;
                 terrain=C*,H*,M*,A*,S*,*^F*&lt;br /&gt;
             [/avoid]&lt;br /&gt;
             [mobilize_condition]&lt;br /&gt;
                 [have_unit]&lt;br /&gt;
                     side=1&lt;br /&gt;
                     count=7-99&lt;br /&gt;
                 [/have_unit]&lt;br /&gt;
             [/mobilize_condition]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Healer Support Micro AI (ai_type=healer_support) ==&lt;br /&gt;
&lt;br /&gt;
The Healer Support Micro AI configures the healers of a side to stay behind the battle lines and heal injured and/or threatened units rather than participate in combat under all circumstances.  You can set different levels of aggressiveness for the healers.  &lt;br /&gt;
&lt;br /&gt;
'''Notes'''&lt;br /&gt;
* By default, ''all'' healers of the AI side are controlled by this AI.  This includes in particular also the side leader, if the leader is a healer.  If you do not want the leader to be included (and possibly be moved away from the keep etc.) use the [filter] tag with canrecruit=no (see below).&lt;br /&gt;
* It is not always better to use this Micro AI than the default AI, as it takes healers (which can be strong units) away from the pool of units to choose for attacks.&lt;br /&gt;
* {{DevFeature1.15|3}} This Micro AI now takes unit guardian status and the passive_leader aspect into account.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Healers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Healer Support Micro AI by using&lt;br /&gt;
 ai_type=healer_support&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Healer Support specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the healer units.  If set, only units on the AI side passing this filter ''and'' having the healing ability are considered by this Micro AI.&lt;br /&gt;
*'''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting units to receive healing. If set, only units on the AI side passing this filter ''and'' the other healee selection criteria will be selected for healing by this Micro AI.&lt;br /&gt;
* '''aggression'''=1.0: (float) Sets the aggressiveness of the AI.  This parameter is set up as a float to accommodate future functionality, but it currently acts as a boolean: if set to zero, the AI will never let its healers participate in combat, for all other values it allows them to attack after all other units have attacked and if the healers cannot find any more units to support.  The former behavior might be appropriate in scenarios with only one or few valuable healers, while the latter might work better in scenarios with many healers.&lt;br /&gt;
* '''injured_units_only'''=no: (boolean) If set to 'yes', the AI will only move healers next to units that are already injured and skip units that currently have full hitpoints, but might get injured during the next enemy turn.&lt;br /&gt;
* '''max_threats'''=9999: (integer) The maximum allowable number of enemies that can attack a hex in order for it to be considered for a healer.  As an example, setting 'max_threats=0' means that the AI only moves healers to locations that are entirely safe from the enemy (assuming that none of the units currently on the map dies).  Note that the value of this key is checked against the number of enemies that can make it to the hex, not the number of adjacent hexes from which the healer could be attacked.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI. Note: as attacks by the healers are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The Healer Support Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded.  It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Healer Support Micro AI configuration from the &amp;quot;Healers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             aggression=0&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Lurkers Micro AI (ai_type=lurkers) ==&lt;br /&gt;
&lt;br /&gt;
The Lurker Micro AI controls some or all units of a side in &amp;quot;lurker mode&amp;quot;.  A lurker is a unit that is capable of moving across most terrains, but that only stops on and attacks from specific terrain.  It might also have the ability to hide on this terrain (which is the reason why it is called a lurker).&lt;br /&gt;
&lt;br /&gt;
Lurkers move individually without strategy and always attack the weakest enemy within their reach.  If no enemy is in reach, the lurker does a random move instead - or it just sits and waits (lurks).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of different types of lurker behavior, check out the &amp;quot;Lurkers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Lurkers Micro AI by using&lt;br /&gt;
 ai_type=lurkers&lt;br /&gt;
in the [micro_ai] tag&lt;br /&gt;
&lt;br /&gt;
===Lurkers specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
* '''[filter]''': Specifies which units of the side are controlled by the Lurker Micro AI.  This is a [[StandardUnitFilter|Standard Unit Filter]].  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': Specifies the terrain from which the lurkers attack. This is a [[StandardLocationFilter|Standard Location Filter]].&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_wander]''': Specifies the terrain on which lurkers end their moves if there is no enemy in reach. This is a [[StandardLocationFilter|Standard Location Filter]].  If [filter_location_wander] is not given, it defaults to [filter_location].&lt;br /&gt;
* '''stationary'''=no: (boolean)  If set to yes, a lurker never moves if there is no enemy within attack range.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Lurkers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=4&lt;br /&gt;
             ai_type=lurkers&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Naga Fighter&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=W*,S*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             [filter_location_wander]&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location_wander]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Messenger Escort Micro AI (ai_type=messenger_escort) ==&lt;br /&gt;
&lt;br /&gt;
The Messenger Escort Micro AI lets you define a set of waypoints through which it tries to move one or several of its units, the messenger(s).  The other units escort the messenger, trying to protect it and attacking enemies that are in its way.  Note that a messenger might not strictly hit intermediate waypoints, but that getting close is good enough, especially if a waypoint is occupied by an enemy unit.  It does, however, always try to get to the last waypoint.&lt;br /&gt;
&lt;br /&gt;
'''Notes on the use of several messengers:''' {{DevFeature1.13|0}}  &lt;br /&gt;
* This Micro AI can control several messengers at the same time, which can but do not have to move in a group.  They can be put on the map altogether in one location, or be spread out on the map or across different turns.&lt;br /&gt;
* The farther a messenger has moved through the chain of waypoints, the higher a priority it is for the AI, meaning that farther advanced (on the map) messengers are moved first, and that the escort units try to clear the way and stay with those messengers more so than with those further back on the map.  This order can be inverted using the invert_order= key.&lt;br /&gt;
* If several messengers are on the map, escort units might move from one to the other, the AI making a judgement call where they are needed most.  Closeness to a messenger is generally the most important criterion, but not the only one.&lt;br /&gt;
* If a messenger dies, the escort units previously staying with it now join one of the other messengers if there are any left (otherwise they follow default AI behavior).&lt;br /&gt;
* If you want certain escort units to stay always with the same messenger, use several messenger [micro_ai] tags with different [filter]/[filter_second] combinations.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Messenger Escort Micro AI by using&lt;br /&gt;
 ai_type=messenger_escort&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Messenger Escort specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': {{DevFeature1.13|0}} (Previous to 1.13.0, only id= was allowed.) The id of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated list of waypoint coordinates through which the messenger will go in the given order to reach its goal, given by the last coordinate.  If you want the messenger to go directly to the goal, simply enter a single x,y coordinate here.  Note that the messenger does not have to hit each waypoint exactly (except for the final one), getting close to them is good enough. {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|11}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Messenger Escort AI consists of 3 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-2&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_death_chance'''=0.67 and '''messenger_death_chance'''=0.0: (both floats; ranging from 0 to 1)    If the messenger is adjacent to an enemy at the end of the move, it will only attack the enemy if the chance of death of the enemy is &amp;gt;= enemy_death_chance and if the messenger's chance of death is &amp;lt;= messenger_death_chance (or if it is an attack at a range for which the enemy does not have a weapon).&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|0}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the escort units.  If not set, this defaults to all enemy units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''invert_order'''=no: {{DevFeature1.13|0}} If set to 'yes', the order of priority with which messenger units are considered is inverted, starting with the messenger unit that is the least advanced on the map (see description above).  In most cases, selecting this option results in worse performance of the AI, so use it with caution.  (And obviously it has no effect at all if there is only one messenger controlled by the AI.)&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|11}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Messenger Escort&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=messenger_escort&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=messenger&lt;br /&gt;
             waypoint_x=31,24,27,28&lt;br /&gt;
             waypoint_y=20,14,7,1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Patrol Micro AI (ai_type=patrol) ==&lt;br /&gt;
&lt;br /&gt;
The defines a number of waypoints for a unit (or units) to follow.  Options can be set whether the route should be followed in a loop, as an out-and back, only once, whether units encountered along the way should be attacked, etc.&lt;br /&gt;
&lt;br /&gt;
With the default settings, getting to the next waypoint takes priority over attacking for a patroller, but this can be overridden (as of '''Wesnoth 1.15.12''', using the '''attack_range''' option).  Unless instructed otherwise, the AI thus prefers to move the patrol unit around enemies rather than straight for them.  Also, if a waypoint is occupied by a unit the AI is not instructed to attack, it will (eventually) abandon that waypoint once it gets close enough and move on to the next one.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different patrol behaviors, check out the &amp;quot;Patrols&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Patrol Micro AI it by using&lt;br /&gt;
 ai_type=patrol&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Patrol specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated lists of the waypoint coordinates through which the patrol travels (in order). {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack''': By default (if this key is not set), the patrol unit attacks any unit it ends up next to after a move, or that is within '''attack_range''' (see below).  Alternatively, this parameter can be set to a comma-separated list of unit ids. If that is done, the patrol only attacks these units when it ends up next to them or if it gets within attack_range.  It ignores any other unit.  This can in turn be used to have the patroller not attack at all, by setting ''attack='' to a non-existing id.  The exception to this is an enemy on the last waypoint with ''one_time_only=yes'', which will always be attacked.&lt;br /&gt;
* '''attack_range'''=1: {{DevFeature1.15|12}} If the patrol comes within this many hexes of an enemy unit, it interrupts its patrol route and attacks that enemy (assuming it still has enough movement left to get to the enemy).  The '''attack=''' instructions are taken into account for this.  Note that the patrol generally tries to move around enemies, which means that for the default value of 1, enemies are usually not within attack range, except at the end of the move.&lt;br /&gt;
* '''attack_invisible_enemies'''=no: {{DevFeature1.15|12}} If set to 'yes', the unit also attacks invisible enemies (according to the criteria set by '''attack''' and '''attack_range''').&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''one_time_only'''=no: (boolean)  If set to 'yes', the unit goes through the patrol route only once and then hangs out at the last waypoint.  It also always attacks any enemy unit on the last waypoint, independent of what ''attack='' is set to.&lt;br /&gt;
* '''out_and_back'''=no: (boolean)  By default, a patrol unit returns to the first waypoint after reaching the last, doing the patrol route as a loop.  If this parameter is set to 'yes', the unit instead reverses its course and perpetually does out and backs of the route.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Patrols&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=patrol&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Konrad&lt;br /&gt;
             waypoint_x=9,24,25&lt;br /&gt;
             waypoint_y=21,23,15&lt;br /&gt;
             one_time_only=yes&lt;br /&gt;
             attack=Gertburt&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Protect Unit Micro AI (ai_type=protect_unit) ==&lt;br /&gt;
&lt;br /&gt;
The Protect Unit Micro AI tries to move one or several units to a given location while keeping them safe from enemy attacks. It does so by keeping the protected units away from enemy units and close to friendly units. Other units on the AI side are controlled by the default Wesnoth AI.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Protect Unit&amp;quot; and &amp;quot;The Elves Besieged&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Protect Unit Micro AI by using&lt;br /&gt;
 ai_type=protect_unit&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Protect Unit specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[unit]''': A separate [unit] tag is required for each protected unit(s).  Each tag has required keys '''id''', '''goal_x''', '''goal_y''' for each unit, as shown in the example below. {{DevFeature1.15|0}} '''goal_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''disable_move_leader_to_keep'''=no: (boolean) If set to 'yes', side leaders protected by this AI do not try to return to their keep.  This is necessary for this Micro AI to work with units that are side leaders and that are supposed to move to an off-keep location.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
*The Protect Unit Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].  Depending on the parameters, it might also modify the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|move leader to keep candidate action]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded. It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;HttT: The Elves Besieged&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=protect_unit&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Delfador&lt;br /&gt;
                 goal_x,goal_y=1,2&lt;br /&gt;
             [/unit]&lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
                 goal_x,goal_y=1,1&lt;br /&gt;
             [/unit]&lt;br /&gt;
 &lt;br /&gt;
             disable_move_leader_to_keep=true&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Recruiting Micro AI==&lt;br /&gt;
=== General Comments on the Recruiting Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Recruiting Micro AI replaces the default recruitment AI by alternative recruiting patterns.  Currently there are two recruiting algorithms you can choose from, random recruiting and the rush recruiting used in the new (as of Version 1.11.1) Experimental AI (see below).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of a different recruiting pattern, check out the &amp;quot;Recruiting&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  You can also explore the unit choices by watching the Experimental AI in a multiplayer game.&lt;br /&gt;
&lt;br /&gt;
'''Note:''' Obviously, the Recruitment Micro AI modifies the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|recruitment candidate action]].&lt;br /&gt;
&lt;br /&gt;
=== Random Recruitment AI (ai_type=recruit_random) ===&lt;br /&gt;
&lt;br /&gt;
This AI randomly selects a unit type from the recruitment list of the side.  The probability of choosing each type and the behavior in low-gold situations can be influenced with optional parameters.&lt;br /&gt;
&lt;br /&gt;
Enable the Random Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_random&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* '''[probability]''': This tag sets the probability of selecting the given unit type.  It has two required sub-keys:&lt;br /&gt;
** '''type''': comma-separated list of unit types to which this probability is to be applied&lt;br /&gt;
** '''probability''': (non-negative float)  The probability with which these unit types is to be selected.  All unit types for which no [probability] tag is defined receive a default probability of 1.0, thus values should be chosen relative to that.  As an example, in the code below swordsmen and peasants are given a probability of 8, while no other units have assigned probabilities.  Thus, each of these two unit types is 8 times more likely to be selected than any other individual unit type.&lt;br /&gt;
* '''skip_low_gold_recruiting'''=no: (boolean)  By default, the random recruitment AI chooses a unit to recruit only from those types for which it has sufficient gold.  If this parameter is set to 'yes', it will instead choose from all available unit types and end recruiting for the turn if there is not enough gold to recruit it this turn (even if other, affordable unit types exist).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a random recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_random&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [probability]&lt;br /&gt;
                 type=Swordsman,Peasant&lt;br /&gt;
                 probability=8&lt;br /&gt;
             [/probability]&lt;br /&gt;
 &lt;br /&gt;
             skip_low_gold_recruiting=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Rush Recruitment AI (ai_type=recruit_rushers) ===&lt;br /&gt;
&lt;br /&gt;
This AI replaces the default recruiting with the recruiting used in the Experimental AI [[Experimental_AI#Recruitment|as described here]].&lt;br /&gt;
&lt;br /&gt;
Enable the Rush Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_rushers&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* {{DevFeature1.15|2}} '''high_level_fraction'''=0: (non-negative number) The approximate fraction of units of level 2 or higher to be recruited. This is defined as fraction of units on the map, not of new units being recruited during a turn or over several turns (which makes a difference if there are already units on the map). The effect is also cumulative per level (starting from level 2), meaning that 1/3 of L2 units will be recruited in the example above, 1/3*1/3=1/9 L3s etc. The default value is zero, which leaves the high-level unit recruiting to the default Experimental AI algorithm resulting usually in very few such units being recruited.&lt;br /&gt;
* '''randomness'''=0.1: (number)  A random number is applied to the rusher recruit engine's score to prevent the recruitment pattern from being too predictable. 0 causes no randomness to be applied, while larger numbers increase the random effect. A value of 1-2 generates results in which the random effect is approximately equal to the scored effect. Extremely high values are essentially entirely random.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a rush recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_rushers&lt;br /&gt;
             action=add&lt;br /&gt;
          [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Simple Attack Micro AI (ai_type=simple_attack) ==&lt;br /&gt;
&lt;br /&gt;
The Simple Attack Micro AI is meant as an addition to the default combat candidate action for when one wants to force the AI to do attacks that it would otherwise not do (for example on units that are 1 XP from leveling) or to execute certain attacks before all others (for example, attack with all &amp;quot;expendable&amp;quot; units first).  It is not set up to be a replacement for the default attacks.  Some notes:&lt;br /&gt;
&lt;br /&gt;
* The algorithm is different from and simpler than that of the default combat CA.  For example, no attack combinations are considered, only individual attacks.  As a result, the [[AiWML#List_of_AI_Aspects|default aspects]] (aggression, caution, [avoid], ...) are not taken into account.  Also, if lots of units are involved, evaluation of this Micro AI is potentially quite a bit slower than of the default attacks.&lt;br /&gt;
&lt;br /&gt;
* If an attack matching the Standard Unit Filters described below is found, this attack will always be executed, no matter how bad the odds are.  Thus, this Micro AI can be used to force the AI to do attacks it would otherwise avoid.&lt;br /&gt;
&lt;br /&gt;
* Other than selecting the attacker/defender units, this AI is currently not configurable.  Let us know if you would like to see certain options to be added.  (No guarantees that we will add them, but we will definitely consider them.)&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Simple Attack&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=simple_attack&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Simple Attack specific keys for the [micro_ai] tag===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  It is, however, in general not desirable to use this AI without at least one of the optional Standard Unit Filters below, as this AI is not meant as a replacement of the default combat candidate action.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=110000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 110,000, which means that it is executed right before the default combat candidate action.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Simple Attack action.  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units the AI should attack.  It defaults to all enemy units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
* '''weapon'''=-1: (integer) The number of the weapon that should be used for this attack.  Weapon numbers start at 1 (not 0).  -1 stands for &amp;quot;best weapon&amp;quot; (by whatever criterion the AI chooses to use).   '''Available from Wesnoth 1.11.8'''&lt;br /&gt;
&lt;br /&gt;
Below are 2 examples of Simple Attack Micro AI configurations from the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  The first one shows how to force the AI to attack with all Soulless and Walking Corpse units first:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110000&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless,Walking Corpse&lt;br /&gt;
             [/filter]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
The second example executes attacks by Soulless on all enemy units that are 1 XP from leveling.  Note that the Lua code in the preload event is there simply in order to code the &amp;quot;1 XP from advancing&amp;quot; condition.  It is not needed for most other SUFs, as can be seen in the previous example.&lt;br /&gt;
     [event]&lt;br /&gt;
         name=preload&lt;br /&gt;
         first_time_only=no&lt;br /&gt;
         [lua]&lt;br /&gt;
             code=&amp;lt;&amp;lt;&lt;br /&gt;
                 function close_to_advancing(unit)&lt;br /&gt;
                     if (unit.experience &amp;gt;= unit.max_experience-1) then&lt;br /&gt;
                         return true&lt;br /&gt;
                     else&lt;br /&gt;
                         return false&lt;br /&gt;
                     end&lt;br /&gt;
                 end&lt;br /&gt;
             &amp;gt;&amp;gt;&lt;br /&gt;
         [/lua]&lt;br /&gt;
     [/event]&lt;br /&gt;
 &lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110001&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless  # No Walking Corpses; L0 units don't advance enemy&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 lua_function = &amp;quot;close_to_advancing&amp;quot;&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
Several Simple Attack Micro AIs can be combined for the same AI side.  The two examples above are both used (note the different values for ca_score) in the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
== Custom Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|5}}&lt;br /&gt;
&lt;br /&gt;
From 1.13.5 onwards, adding a new Micro AI can be easily done without altering any core files or overriding the micro_ai tag. The [micro_ai] tag now looks up its ''ai_type'' parameter in the &amp;lt;code&amp;gt;wesnoth.micro_ais&amp;lt;/code&amp;gt; table and uses the micro AI setup function found there to determine which keys are required and allowed and what candidate actions to add.&lt;br /&gt;
&lt;br /&gt;
A micro AI setup function takes as its single parameter the contents of the [micro_ai] tag and returns three separate values:&lt;br /&gt;
*'''required''': A list of required keys and tags, as strings. Tags are indicated by enclosing them within square brackets in the string. If a key or tag in this list is missing from the [micro_ai] tag, an error will be printed.&lt;br /&gt;
*'''optional''': Similar to above, a list of optional keys and tags, as strings. No error will be printed if a key or tag in this list is missing, nor will an error be printed if it is present (as would be the case for unrecognized keys or tags).&lt;br /&gt;
*'''ca_params''': A table specifying the candidate actions that constitute the micro AI. It should contain an ''ai_id'' key (which specifies a base ID to be used for candidate actions of this micro AI) and a series of candidate action definitions. Each candidate action definition is a table containing the following keys:&lt;br /&gt;
**'''ca_id''': The ID used for the candidate action. This will be combined with the ''ai_id'' to produce the final ID.&lt;br /&gt;
**'''location''': Path to the Lua file that defines the candidate action.&lt;br /&gt;
**'''score''': The maximum score for the candidate action. If the [micro_ai] tag specified a ''ca_score'' parameter, the score here should normally be based on that.&lt;br /&gt;
&lt;br /&gt;
(The return statement would thus look like &amp;lt;syntaxhighlight lang='lua' inline&amp;gt;return required, optional, ca_params&amp;lt;/syntaxhighlight&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
'''Examples''' of micro AI setup functions can be found in the [https://github.com/wesnoth/wesnoth/tree/1.14/data/ai/micro_ais/mai-defs default MAI definitions directory].  See the [https://github.com/wesnoth/wesnoth/blob/1.14/data/ai/micro_ais/mai-defs/animals.lua#L4-L12 Big Animals MAI definition] for a simple example.  For custom micro AIs, the setup code should be put into a ''[lua]'' tag inside a preload event.&lt;br /&gt;
&lt;br /&gt;
Since the function has full access to the contents of the [micro_ai] tag, it can conditionally add candidate actions to '''ca_params''' depending on the values of other keys within the [micro_ai] tag. It can also perform other actions such as adding facets to an aspect; if it does such things, it should pay attention to the ''action'' key to determine whether it's being added or removed.&lt;br /&gt;
&lt;br /&gt;
There are two useful helper functions that can be used by Micro AI setup functions requiring aspect changes. They can be brought into scope by a line similar to the following:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='lua&amp;gt;local MAIH = wesnoth.require(&amp;quot;ai/micro_ais/micro_ai_helper.lua&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* MAIH.add_aspects(side, aspect_params)&lt;br /&gt;
* MAIH.delete_aspects(side, aspect_params)&lt;br /&gt;
&lt;br /&gt;
The ''aspect_params'' parameter is an array of tables, each of which contains the following keys:&lt;br /&gt;
*'''aspect''': The aspect to change.&lt;br /&gt;
*'''facet''': The WML defining the facet. This must include an ''id'' key in order for the aspect to be correctly deleted. For maximum compatibility, incorporate the ''ca_id'' key from the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AiWML&amp;diff=69624</id>
		<title>AiWML</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AiWML&amp;diff=69624"/>
		<updated>2022-06-13T02:39:01Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* The [goal] Tag */ add id=&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{WML Tags}}&lt;br /&gt;
&lt;br /&gt;
== Customizing the Default AI Using WML ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
This page contains:&lt;br /&gt;
* Descriptions of the aspects and goals of the default AI&lt;br /&gt;
* Instructions for setting them up at the beginning of the scenario in a [side] tag&lt;br /&gt;
&lt;br /&gt;
This page does '''not''' contain:&lt;br /&gt;
* Instructions for [[LuaAI#Dynamic_Lua_Aspects|setting up aspects dynamically using Lua]].&lt;br /&gt;
* Instructions for [[Modifying_AI_Components#Modifying_Standard_Aspects|changing aspects and goals mid scenario]], although for many of them (for example, for all [[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Aspects|standard aspects]]) the methods are almost identical&lt;br /&gt;
* Instructions for some of the [[Modifying_AI_Components|more complex AI customization tasks]].&lt;br /&gt;
* Anything about [[Micro_AIs|MicroAIs]]&lt;br /&gt;
* [[AI_Recruitment|Advanced recruitment details]]&lt;br /&gt;
&lt;br /&gt;
== Defining Aspects and Goals of the RCA AI ==&lt;br /&gt;
&lt;br /&gt;
[[RCA_AI#Use_of_Aspects_in_the_RCA_AI|Aspects]] and [[RCA_AI#Use_of_Goals_and_Targets_in_the_RCA_AI|goals]] of the [[RCA_AI|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 [[#List_of_AI_Aspects|aspects themselves]] as arguments, as well as the following general keys:&lt;br /&gt;
* '''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 [https://github.com/wesnoth/wesnoth/blob/master/data/core/macros/schedules.cfg 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.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
* '''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. {{DevFeature1.13|5}} In 1.13, the meaning of '''ai_algorithm''' has changed and no longer selects a native AI that's built into the engine, as the RCA AI has now become the core system and no alternatives exist anymore. Instead, it selects a predefined ''[ai]'' definition, such as those defined in ''data/ai/ais''. Since these are defined in WML, you can easily build your own.&lt;br /&gt;
&lt;br /&gt;
An example of a simple use of the [ai] tag to set AI parameters (aspects) is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=wml&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    time_of_day=dusk&lt;br /&gt;
    aggression=0.99&lt;br /&gt;
    caution=0.01&lt;br /&gt;
    [avoid]&lt;br /&gt;
        [filter]&lt;br /&gt;
            side=2,3&lt;br /&gt;
        [/filter]&lt;br /&gt;
        radius=2&lt;br /&gt;
    [/avoid]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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 &amp;lt;code&amp;gt;:inspect&amp;lt;/code&amp;gt; in debug mode to verify whether an aspect was changed successfully.&lt;br /&gt;
&lt;br /&gt;
==List of AI Aspects==&lt;br /&gt;
&lt;br /&gt;
'''Note:''' Only one instance of each aspect will be active for a side at any time. Where multiple instances of the same aspect are included in an [ai] tag, the last whose conditions (time of day, turns, etc.) match will be the only one that applies.&lt;br /&gt;
&lt;br /&gt;
* '''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 &amp;lt;u&amp;gt;only&amp;lt;/u&amp;gt; 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:'''&lt;br /&gt;
** This is a simplified version of the more complex (and arguably more useful) application of a dynamic Lua ''advancements'' aspect, as described [[LuaAI#The_advancements_Aspect|here]].&lt;br /&gt;
** The ''advancements'' aspect only takes effect during the AI turn, that is, on offense only, not on defense.&lt;br /&gt;
** 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).&lt;br /&gt;
&lt;br /&gt;
* '''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.  The description below applies as written to the ''combat'' CA only, but aggression is taken into account by the ''high_xp_attack'' and ''spread_poison'' CAs as well (as of '''Wesnoth 1.13.5''' and '''1.15.3''' respectively).&lt;br /&gt;
** '''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.&lt;br /&gt;
** 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 &amp;gt;0 is found.  The combat CA score is zero if the highest attack score is &amp;lt;=0).&lt;br /&gt;
** 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.&lt;br /&gt;
** All the negative contributions to the score are multiplied by '(1-aggression)'.  This means that:&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
** 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.&lt;br /&gt;
&lt;br /&gt;
*'''[attacks]''': Filters the units considered for combat, both on the AI and the enemy sides.  Applies to the ''combat'', ''high_xp_attacks'' and ''spread_poison'' CAs only.  It cannot be set in the same way as the other aspects and is therefore described in a [[#Filtering_Combat_with_the_attacks_Aspect|separate section]] below.&lt;br /&gt;
&lt;br /&gt;
* '''[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''.&lt;br /&gt;
** '''[[StandardLocationFilter]]''': The locations for the AI to avoid.  Do not use a [filter_location] tag.&lt;br /&gt;
**'''Note:''' Only one instance of an [avoid] aspect will be active for a side at any time. Where multiple instances are included in an [ai] tag, the last whose conditions (time of day, turns, etc.) match will be the only one that applies.&lt;br /&gt;
&lt;br /&gt;
* '''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 [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|evaluation scores]]):&lt;br /&gt;
** ''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].&lt;br /&gt;
** ''Combat'' CA: &lt;br /&gt;
*** 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 &amp;quot;exposure&amp;quot; rating of the units.  This exposure is then subtracted from the 'attack score' as described for ''aggression'' above.&lt;br /&gt;
*** 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.)&lt;br /&gt;
*** If the AI leader is used in an attack, the AI ''always'' uses 'caution=2' for the evaluation of that attack.&lt;br /&gt;
** ''Retreat'' CA: {{DevFeature1.15|3}} This CA has been removed.&lt;br /&gt;
*** 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 &amp;lt;tt&amp;gt;caution &amp;amp;times; their_power &amp;gt; our_power&amp;lt;/tt&amp;gt;. 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.&lt;br /&gt;
*** 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. &lt;br /&gt;
*** The AI never retreats if caution is set to 0 or lower.&lt;br /&gt;
** ''Move-to-targets'' CA:&lt;br /&gt;
*** 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. &amp;quot;Dangerous&amp;quot; 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 &amp;lt;tt&amp;gt;our_strength / their_strength &amp;gt; 0.5 + caution&amp;lt;/tt&amp;gt;. 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.&lt;br /&gt;
***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.&lt;br /&gt;
*** The AI also considers retreating units during the move-to-target phase based on criteria similar to those for the retreat CA.&lt;br /&gt;
&lt;br /&gt;
* '''grouping'''=&amp;quot;offensive&amp;quot;: (string) How the AI should try to group units.  Applies to ''move-to-targets'' CA only.  Possible values:&lt;br /&gt;
** ''offensive'': Makes the AI try to group units together before attacking.&lt;br /&gt;
** ''defensive'': Makes the AI group units together very conservatively, only advancing them much beyond its castle if it has overwhelming force.&lt;br /&gt;
** ''no'': Makes the AI use no grouping behavior.&lt;br /&gt;
&lt;br /&gt;
* '''leader_aggression'''=&amp;quot;-4.0&amp;quot;: 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''.&lt;br /&gt;
&lt;br /&gt;
* '''[leader_goal]'''=&amp;quot;&amp;quot;: Makes the AI try to move its leader to a specific location.  Applies to ''move-leader-to-goals'' CA only.&lt;br /&gt;
** '''x''', '''y''': The location toward which the AI should move its leader.&lt;br /&gt;
** '''auto_remove'''=no: (bool)  If 'no' (default), the AI moves the leader to the goal, after which he stays there until [leader_goal] is [[Modifying_AI_Components#Modifying_Standard_Aspects|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).&lt;br /&gt;
** '''id'''=&amp;quot;&amp;quot;: (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] [[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Aspects|facet]].  Thus, in principle for this to work, you need to set the id of the facet as described [[Modifying_AI_Components#Modifying_Standard_Aspects|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. {{DevFeature1.13|5}} 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.&lt;br /&gt;
**'''max_risk'''=1-caution: (double: meaningful values are &amp;gt;=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.&lt;br /&gt;
&lt;br /&gt;
* '''leader_ignores_keep'''=no: (bool) Set to 'yes' for this aspect to apply to all leaders of a side. {{DevFeature1.15|3}} In addition to 'yes/no', the aspect can now also be set to a comma-separated list of unit ids. The aspect then only applies to leaders with ids matching an id on that list. Setting leader_ignores_keep for some or all leaders means that:&lt;br /&gt;
** The affected 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). Thus, these leaders behave in the same way as any other unit of the AI side.&lt;br /&gt;
** This also means that these 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.&lt;br /&gt;
&lt;br /&gt;
* '''leader_value'''=3: (double) A number 0 or higher which determines the value of enemy leaders as [[#AI_Targets_and_Goals|targets]].  Affects ''move-to-targets CA'' only (and therefore specifically does not apply to attacks).&lt;br /&gt;
&lt;br /&gt;
* '''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''. {{DevFeature1.15|3}} In addition to 'yes/no', the aspect can now also be set to a comma-separated list of unit ids. The aspect then only applies to leaders with ids matching an id on that list.&lt;br /&gt;
&lt;br /&gt;
* '''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. {{DevFeature1.15|3}} In addition to 'yes/no', the aspect can now also be set to a comma-separated list of unit ids. The aspect then only applies to leaders with ids matching an id on that list.&lt;br /&gt;
&lt;br /&gt;
*'''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 [[AI_Recruitment|separate page]].&lt;br /&gt;
&lt;br /&gt;
*'''retreat_factor'''=0.25 (double) {{DevFeature1.15|11}} Affects ''retreat-injured'' CA only. Determines how injured AI units have to be before they attempt retreating. This is approximately the fraction of the units' hitpoints, but other factors play a role also, such as the terrain the units are on, and leaders and poisoned units retreat somewhat earlier. Setting this to 0 (or negative values) disables retreating. Note that even setting this to very large values still requires units to be somewhat injured before they start retreating.&lt;br /&gt;
&lt;br /&gt;
*'''retreat_enemy_weight'''=1.0 (double) {{DevFeature1.15|11}} Affects ''retreat-injured'' CA only. When deciding where to retreat to, the AI considers how much healing a location provides and weighs it against the threat of all the enemies that can get there (balanced by the own units that can get there). In this evaluation, it multiplies the enemy threat rating by ''retreat_enemy_weight'', meaning that the larger its value is, the more a unit tries to move away from enemies.  There are three regimes for this aspect:&lt;br /&gt;
** Positive values: Only locations that provide healing (both from terrain and from adjacent healers which have no MP left) are considered. For regenerating units, all hexes are treated as if they provide healing.&lt;br /&gt;
** Zero: Enemy threats are ignored&lt;br /&gt;
** Negative values: All hexes are considered for retreating. The enemy threat rating is multiplied by the absolute value of the aspect value. Thus, if very negative values are used (e.g. -100), this can be used to make units run away from enemies with little consideration for anything else.&lt;br /&gt;
&lt;br /&gt;
* '''scout_village_targeting'''=3: (double) The AI multiplies the value of village [[#AI_Targets_and_Goals|targets]] for scouts by this value.  Affects ''move-to-targets'' CA only.&lt;br /&gt;
&lt;br /&gt;
* '''simple_targeting'''=no: (bool) If 'yes', the AI moves its units toward [[#AI_Targets_and_Goals|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.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
* '''village_value'''=1: (double) A number 0 or higher which determines how much the AI tries to go for villages as [[#AI_Targets_and_Goals|targets]].  Affects ''move-to-targets'' CA only.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
=== Removed AI Aspects ===&lt;br /&gt;
&lt;br /&gt;
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. {{DevFeature1.13|5}} Except for attack_depth, these have now all been removed.  {{DevFeature1.15|0}} attack_depth is now removed as well.&lt;br /&gt;
&lt;br /&gt;
* '''attack_depth'''=5: (int)&lt;br /&gt;
* '''number_of_possible_recruits_to_force_recruit'''=3.1: (double)&lt;br /&gt;
* '''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.&lt;br /&gt;
** It is, in principle, still possible to use this aspect together with the [[Wesnoth_AI_Framework#Recruitment_Stage|recruitment stage]], but this is discouraged for the reasons given at this link.&lt;br /&gt;
* '''recruitment_ignore_bad_combat'''=no: (bool)&lt;br /&gt;
* '''recruitment_ignore_bad_movement'''=no: (bool)&lt;br /&gt;
&lt;br /&gt;
==AI Targets and Goals==&lt;br /&gt;
&lt;br /&gt;
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 [[RCA_AI#RCA_AI_Aspect_and_Goal_Configuration|base values]].  Additional targets can be defined using the [goal] tag.&lt;br /&gt;
&lt;br /&gt;
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 [[RCA_AI#Use_of_Goals_and_Targets_in_the_RCA_AI|here]].  The sections below only describe how to set up goals in order to add targets for the ''move-to-targets'' CA.&lt;br /&gt;
&lt;br /&gt;
===The [goal] Tag===&lt;br /&gt;
&lt;br /&gt;
The [goal] tag defines units or locations as [[#AI_Targets_and_Goals|move targets]] (not attack targets!) for the AI.  '''Applies to ''move-to-targets'' CA only'''. &amp;lt;-- That is '''extremely important''' to understand, in particular the fact that the [goal] tag has '''no influence whatsoever on attacks'''.&lt;br /&gt;
&lt;br /&gt;
 The following keys/tags can be used:&lt;br /&gt;
* '''name'''=&amp;quot;target&amp;quot;: (string)  The following values are possible and result in different types of targets, as shown in the examples below:&lt;br /&gt;
** ''target'':  The (default) target goal specifies target units (not necessarily enemy units) toward which the AI should move its units. {{DevFeature1.13|5}} target_unit is now a synonym for this.&lt;br /&gt;
** ''target_location'': Specifies target locations toward which the AI should move its units.&lt;br /&gt;
** ''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.&lt;br /&gt;
** ''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.&lt;br /&gt;
** ''protect_my_unit'': ('''deprecated''') &amp;lt;s&amp;gt;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.&amp;lt;/s&amp;gt;&lt;br /&gt;
** ''lua_goal'': A dynamically calculated goal written in Lua. See [[LuaAI#Lua_Goals_and_Targets|here]] for details on how this works.&lt;br /&gt;
&lt;br /&gt;
* '''[criteria]'''=&amp;quot;&amp;quot;: Contains a [[StandardUnitFilter]] (for ''target'', ''protect_unit'' or ''protect_my_unit'') or [[StandardLocationFilter]] (for ''target_location'' or ''protect_location'') describing the targets.&lt;br /&gt;
&lt;br /&gt;
* '''value'''=0: (double) The value of the goal.&lt;br /&gt;
&lt;br /&gt;
* '''protect_radius'''=20: (int) The protection radius.  Applies to ''protect_location'', ''protect_unit'' and ''protect_my_unit''.&lt;br /&gt;
&lt;br /&gt;
* '''id'''=&amp;quot;&amp;quot;: (string)  Optional ID used for [[Modifying_AI_Components#Modifying_Goals|modifying the goal]].&lt;br /&gt;
&lt;br /&gt;
===Examples of [goal] Tag Usage===&lt;br /&gt;
&lt;br /&gt;
'''target:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        [criteria] #NOTE: this is a SUF, because we're targeting a unit&lt;br /&gt;
            side=3&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''target_location:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=target_location&lt;br /&gt;
        [criteria] #NOTE: this is a SLF, because we're targeting a location&lt;br /&gt;
            x,y=42,20&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''protect_location:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=protect_location&lt;br /&gt;
        [criteria] #NOTE: this is a SLF, because we're protecting a location&lt;br /&gt;
            x,y=42,20&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        protect_radius=16&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''protect_unit:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=protect_unit&lt;br /&gt;
        [criteria] #NOTE: this is a SUF, because we're protecting a unit&lt;br /&gt;
            side=3&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        protect_radius=16&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''How to use protect_unit to protect the own leader''' (We are side 2)&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=protect_unit&lt;br /&gt;
        [criteria]&lt;br /&gt;
            side=2&lt;br /&gt;
            canrecruit=yes&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        protect_radius=8&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Deprecated AI Targeting Aspects ===&lt;br /&gt;
&lt;br /&gt;
The following AI targeting parameters currently still work, but have been superseded by the [[#The_.5Bgoal.5D_Tag|[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.&lt;br /&gt;
&lt;br /&gt;
* '''[target]'''=&amp;quot;&amp;quot;: '''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.&lt;br /&gt;
** '''[[StandardUnitFilter]]''': Do not use a [filter] tag.&lt;br /&gt;
** '''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.&lt;br /&gt;
&lt;br /&gt;
* '''[protect_location]'''=&amp;quot;&amp;quot;: '''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.&lt;br /&gt;
** '''x''', '''y''': Standard coordinates. These indicate the location the AI is protecting.&lt;br /&gt;
** '''radius''': The radius around it to protect (0 indicates a single hex). {{DevFeature1.13|5}} This key has been changed to protect_radius, though it's preferred to change old code to use the [goal] tag instead.&lt;br /&gt;
** '''value''': The importance of protecting this location.&lt;br /&gt;
&lt;br /&gt;
* '''[protect_unit]'''=&amp;quot;&amp;quot;: '''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.&lt;br /&gt;
** '''[[StandardUnitFilter]]''': The unit(s) to protect. Do not use a [filter] tag.&lt;br /&gt;
** '''radius''': The radius around it to protect (0 indicates a single hex). {{DevFeature1.13|5}} This key has been changed to protect_radius, though it's preferred to change old code to use the [goal] tag instead.&lt;br /&gt;
** '''value''': The importance of protecting this unit.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
== Filtering which Units Participate in which Actions==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}}&lt;br /&gt;
&lt;br /&gt;
Each [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|candidate action of the default AI]] can be restricted to apply to only a subset of the AI units. This is done by adding a '''[filter_own]''' tag to the '''[[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Candidate_Actions|[candidate_action]]]''' tag definition.&lt;br /&gt;
&lt;br /&gt;
Typical use cases:&lt;br /&gt;
* Restricting an action to certain units so that other units are not used to this. For example, village grabbing could be restricted to scout units only, so that slower units do not take detours on their way toward the enemies. See the example below.&lt;br /&gt;
* Excluding units from a certain action in order to have them available for other actions afterward. For example, one might want to add a custom candidate action which moves healers behind injured units (or use the existing [[Micro_AIs|Micro AI]] for this). Ideally, this should happen after ''all'' other units have moved, specifically also after the move-to-targets candidate action (MtT CA) is done. In the default setting, however, the MtT CA would also move the healers. Thus, we would want to exclude them using a '''[filter_own]''' tag.&lt;br /&gt;
* Adding two instances of the same candidate action with different scores to force the order in which units participate in this action. For example, one could add another instance of the move-leader-to-keep CA with a '''[filter_own]''' tag restricting it to one leader, in order to make this leader move to a keep (and therefore recruit) first.&lt;br /&gt;
&lt;br /&gt;
Note that for the combat AI, using the attacks aspect to filter units as described in the next section '''is slightly more efficient'''. Thus, that should be the preferred method if there are many units on the map.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[candidate_action]&lt;br /&gt;
	id=villages&lt;br /&gt;
	engine=cpp&lt;br /&gt;
	name=ai_default_rca::get_villages_phase&lt;br /&gt;
	max_score=60000&lt;br /&gt;
	score=60000&lt;br /&gt;
	[filter_own]&lt;br /&gt;
		type=Wolf Rider&lt;br /&gt;
	[/filter_own]&lt;br /&gt;
[/candidate_action]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Filtering Combat with the ''attacks'' Aspect==&lt;br /&gt;
&lt;br /&gt;
The ''attacks'' aspect lets us filter the units considered by the ''combat'', ''high_xp_attacks'' and ''spread_poison'' 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&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [aspect]&lt;br /&gt;
        id=attacks&lt;br /&gt;
        [facet]&lt;br /&gt;
            invalidate_on_gamestate_change=yes&lt;br /&gt;
            [filter_own]&lt;br /&gt;
                type=Elvish Sorceress,Elvish Enchantress,Elvish Sylph&lt;br /&gt;
            [/filter_own]&lt;br /&gt;
            [filter_enemy]&lt;br /&gt;
                race=undead&lt;br /&gt;
            [/filter_enemy]&lt;br /&gt;
        [/facet]&lt;br /&gt;
    [/aspect]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Several important notes:&lt;br /&gt;
* The ''attacks'' aspect is a ''composite'' aspect and cannot be defined using the simple syntax of the other ''standard'' aspects.  See [[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Aspects|here]] for a description of the syntax of the example.&lt;br /&gt;
* This only works if &amp;lt;code&amp;gt;invalidate_on_gamestate_change=yes&amp;lt;/code&amp;gt; is 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 [[Wesnoth_AI_Framework#Some_more_on_the_invalidation_keys|here]].&lt;br /&gt;
* If [filter_own] or [filter_enemy] are omitted, the selection defaults to all units of the respective sides.&lt;br /&gt;
* '''Most importantly''': The above example results in sorceress-line vs undead attacks being the &amp;lt;u&amp;gt;only&amp;lt;/u&amp;gt; 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:&lt;br /&gt;
** Setting up the [filter_own] or [filter_enemy] [[StandardUnitFilter]]s to adapt dynamically to the situation on the map.&lt;br /&gt;
** 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_AIs#Micro_AI_Test_and_Demo_Scenarios|Micro AI test scenario]] [https://github.com/wesnoth/wesnoth/blob/master/data/ai/micro_ais/scenarios/protect_unit.cfg ''Protect Unit''].&lt;br /&gt;
** Using [[LuaAI#Dynamic_Lua_Aspects|dynamic Lua aspects]] is in principle also possible, but far from easy due to the complex tables this aspect returns.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|5}}&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [attacks]&lt;br /&gt;
        invalidate_on_gamestate_change=yes&lt;br /&gt;
        [filter_own]&lt;br /&gt;
            type=Elvish Sorceress,Elvish Enchantress,Elvish Sylph&lt;br /&gt;
        [/filter_own]&lt;br /&gt;
        [filter_enemy]&lt;br /&gt;
            race=undead&lt;br /&gt;
        [/filter_enemy]&lt;br /&gt;
    [/attacks]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category: WML Reference]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=EasyCoding&amp;diff=68482</id>
		<title>EasyCoding</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=EasyCoding&amp;diff=68482"/>
		<updated>2021-09-15T03:28:34Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Improvements to AI */ remove tasks that have been taken care of&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''We are permanently updating this page. Parts marked by &amp;lt;s&amp;gt;strikeout&amp;lt;/s&amp;gt; will likely be deleted shortly.&lt;br /&gt;
&lt;br /&gt;
This page is here to document easy to do coding tasks. It is not here to double the feature request database, and should only be filled by people that know the code well enough to judge the difficulty of a given task. &lt;br /&gt;
&lt;br /&gt;
If you are such a person, you should feel free to edit this page.&lt;br /&gt;
&lt;br /&gt;
If you're not, you should post a feature request and discuss your idea on the forum or IRC. A coder with better knowledge of the code might give you the green light to add your feature here.&lt;br /&gt;
&lt;br /&gt;
Anybody should feel free to add &amp;quot;clues&amp;quot; to any tasks, that is entry points, traps to avoid, person to contact to discuss and so on.&lt;br /&gt;
&lt;br /&gt;
If you plan to work on a feature, write your name at the bottom of the feature, with the date. Note that if you are too long at working on a feature we'll &amp;quot;free&amp;quot; it back (that is if you're not working on it. If you have problems implementing it, just tell us....)&lt;br /&gt;
&lt;br /&gt;
If none of these are to your liking, feel free to check our [http://bugs.wesnoth.org/?q=is%3Aissue+is%3Aopen+label%3A%22Good+first+issue%22 bug tracker] with a broad spectrum of tasks.&lt;br /&gt;
&lt;br /&gt;
== Engine related features ==&lt;br /&gt;
&lt;br /&gt;
=== Make a &amp;quot;map diff&amp;quot; tool to visualizes changes between two version of a map ===&lt;br /&gt;
&lt;br /&gt;
Maintainers of maps often make small tweaks for balance purposes. For someone who doesn't know the map like the back of their hand it may be very difficult to spot exactly what changed. There isn't any really good way to do this right now -- using the 'diff' tool at command line doesn't give good results for .map files.&lt;br /&gt;
&lt;br /&gt;
Such a tool would be helpful for someone who maintains a map pool containing maps made by others, also for someone who wants to browse a repository and examine balance changes on several maps, which could be instructive for someone who wants to make and balance their own maps.&lt;br /&gt;
&lt;br /&gt;
There's any number of ways to actually do this, one might be to have a feature in the map editor that takes the name of another map and adds map labels to indicate what hexes changed. It could also be a command accessible in debug mode when playing the game, or it could be an additional command-line argument (--diff [map1] [map2] ?) for the game. (It could be a standalone application but that's probably going to end up with much more work for you.)&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/issues/2323&lt;br /&gt;
&lt;br /&gt;
=== Add an option to not store replays ===&lt;br /&gt;
&lt;br /&gt;
As noted in https://wiki.wesnoth.org/SavefileWML all normal savefiles usually contain [replay] and [replay_start] tags that usually take over 50% of the total savefile size and are only used for providing the possibilty to replay scenarios. It's be nice to have an option to skip these information to reduce the savefile size. Alternativeley one could create a tool inrtegated in a load/savegame dialog to remove the replay information from a given savegame file.&lt;br /&gt;
&lt;br /&gt;
See the feature request https://github.com/wesnoth/wesnoth/issues/1457&lt;br /&gt;
&lt;br /&gt;
== MP related features ==&lt;br /&gt;
&lt;br /&gt;
=== Add possibility to switch off mod dependency checking ===&lt;br /&gt;
MP modifications can define dependencies. Currently there's no way to force a &amp;quot;broken&amp;quot; configuration, although overzealous use of the system by UMC authors could make this feature useful. Add an option to the MP game creation screen to switch on/off dependency checking. Ask lipk (forum)/lipkab (IRC).&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/issues/2324&lt;br /&gt;
&lt;br /&gt;
=== Make a header for the add-on when we organize scenarios by add-on in the MP create dialog ===&lt;br /&gt;
&lt;br /&gt;
Using an established GUI technique to signal when one block of scenarios for an add-on ends and the next begins would make it much easier to use this dialog, if the user has several add-ons.&lt;br /&gt;
&lt;br /&gt;
See one proposal here:&lt;br /&gt;
https://github.com/wesnoth/wesnoth/issues/1365&lt;br /&gt;
&lt;br /&gt;
=== Timer pause button ===&lt;br /&gt;
&lt;br /&gt;
Longstanding feature request here:&lt;br /&gt;
https://github.com/wesnoth/wesnoth/issues/1237&lt;br /&gt;
&lt;br /&gt;
Request is for either a button, or a simple chat command like :pause, :unpause, to allow players to put the timer on hold if they are interrupted.&lt;br /&gt;
&lt;br /&gt;
Ideally would be accompanied with a multiplayer-only scenario attribute &amp;quot;number_of_pauses&amp;quot; which decreases each time a client pauses the game. Pause should use a blindfold object to black out the screen, and send a chat message automatically when it occurs / resume occurs. For an example of how it might look, start a local hotseat mp game and enable the Preferences-&amp;gt;General-&amp;gt;Turn Dialog option. (This behavior was introduced in this commit: https://github.com/wesnoth/wesnoth/commit/bab03d554bea92f1ce6f585ef1123202173a3491)&lt;br /&gt;
&lt;br /&gt;
Wintermute/happygrue also has other ideas about how the timer should/could work. For example, instead of their client ending their turn, the networked opponents could be required to &amp;quot;confirm&amp;quot; the time out. (This is probably a networked MP-only idea.)&lt;br /&gt;
&lt;br /&gt;
A very fancy implementation might instead of a counter, allow the other players to vote on whether a pause request is granted.&lt;br /&gt;
&lt;br /&gt;
Make sure that the paused client still responds to network commands, so that it is possible to kick a person who pauses and then doesn't ever unpause.&lt;br /&gt;
&lt;br /&gt;
=== Add a &amp;quot;concede&amp;quot; button ===&lt;br /&gt;
&lt;br /&gt;
Often times in mp, players concede the game before their leader is actually killed. When the game ends 'officially' the map is revealed, which can sometimes be instructive for the losing player. Additionally, it makes it easier to analyze the results of many games, for purposes of balancing, or for maintaining a ladder. So while it may seem small, this would actually be a pretty useful feature.&lt;br /&gt;
&lt;br /&gt;
FIXME: figure out gh issue id&lt;br /&gt;
&lt;br /&gt;
This could be implemented in lua.&lt;br /&gt;
&lt;br /&gt;
=== Show [options] values in the mp lobby gameslist ===&lt;br /&gt;
&lt;br /&gt;
It' be useful for newly joining payer to see the chosen [options] of a mp game, since most likeley there isn't enough space we could for example wigher show the options via an extra dropdown button in th elobby or show them in the mp waiting dialog instead.&lt;br /&gt;
&lt;br /&gt;
See the featurerequest https://github.com/wesnoth/wesnoth/issues/1514&lt;br /&gt;
&lt;br /&gt;
== WML-related features ==&lt;br /&gt;
&lt;br /&gt;
=== Side filter support in [item] ===&lt;br /&gt;
&lt;br /&gt;
currently [items] allows a team_name= attribute to specify for which side the item is displayed, it would be better if [item] allowed a side filter to specify for which side the item is displayed.&lt;br /&gt;
&lt;br /&gt;
See the bug report https://github.com/wesnoth/wesnoth/issues/1477&lt;br /&gt;
&lt;br /&gt;
=== Support variable poison properties ===&lt;br /&gt;
&lt;br /&gt;
See a feature request here:&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/issues/1339&lt;br /&gt;
&lt;br /&gt;
=== Events Manipulation ===&lt;br /&gt;
&lt;br /&gt;
* [event] persistent=&amp;quot;yes&amp;quot; - creates an event that lasts for the rest of the campaign&lt;br /&gt;
* [disable_event][/disable_event] disables the event within which it appears (useful with conditionals). A more complex take on first_time_only=yes/no.&lt;br /&gt;
''This is largely possible with event IDs, but could be useful when multiple copies of an event should be floating around''&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/issues/2326&lt;br /&gt;
&lt;br /&gt;
=== Game Information ===&lt;br /&gt;
&lt;br /&gt;
* [query_location] - queries a location from the user&lt;br /&gt;
** variable&lt;br /&gt;
** [filter_location]&lt;br /&gt;
''Regarding [query_location]: probably a better name would be choose_location. This is used when a wml event is running and needs to get a target location hex from the user. For example, you right click on a catapult unit and choose the menu item &amp;quot;fire Catapult.&amp;quot; Now a [message] says to click on any target within 5 hexes. Now [choose_location] is encountered and the mouse cursor changes to a target sign (possibly customizable). If the user's mouse drifts outside of the allowed locations it changes to a cancel icon. If the operation is cancelled then the variable is cleared and no location stored. --Sapient''&lt;br /&gt;
&lt;br /&gt;
''This can be implemented in lua by using wesnoth.game_events.on_mouse_action and wesnoth.syncronize_choice''&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/issues/2325&lt;br /&gt;
&lt;br /&gt;
== Improvements to AI ==&lt;br /&gt;
&lt;br /&gt;
=== Existing bugs or undesirable features ===&lt;br /&gt;
&lt;br /&gt;
Most of these are probably relatively easy to fix, although we can, of course, not guarantee that.&lt;br /&gt;
&lt;br /&gt;
* Bug: Adding a candidate action mid-turn does not include it where it belongs in terms of its evaluation score. Instead, it gets added at the end of the candidate action list and might not be executed on the current turn, because other CAs with lower scores get evaluated first.&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/issues/2345&lt;br /&gt;
&lt;br /&gt;
* Bug: ai.get_new_enemy_dst_src and related functions have several problems:&lt;br /&gt;
** They use the current moves of the enemy units, which generally are all zero.&lt;br /&gt;
** Hexes occupied by other enemy units count as not reachable.&lt;br /&gt;
** Working around this with a Lua wrapper makes this actually slower than ai_helper.get_enemy_dst_src, which makes the functions pretty much useless, as speed is the only thing speaking for them.&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/issues/2346&lt;br /&gt;
&lt;br /&gt;
== GUI2 features ==&lt;br /&gt;
&lt;br /&gt;
Although mordante developed a new GUI system, GUI2, some dialogs still use the older GUI1 and could be improved or transferred. mordante has since disappeared, leaving GUI2's core code unmaintained; shadowm and vultraz should be able to answer most GUI2 (and even GUI1) related questions in his absence.&lt;br /&gt;
&lt;br /&gt;
* Information on the wiki can be found under [[GUIToolkit]] on the wiki.&lt;br /&gt;
* The source code is under [https://github.com/wesnoth/wesnoth/tree/master/src/gui src/gui/]&lt;br /&gt;
* All mainline GUI2 WML resides in [https://github.com/wesnoth/wesnoth/tree/master/data/gui data/gui/]&lt;br /&gt;
&lt;br /&gt;
* Make games sortable in the lobby (open slots, total number of players, era, XP modifier, gold per village, fog/shroud)&lt;br /&gt;
* The biggest feature yet to be converted to GUI2 is the in-game Help documentation (first available from the main screen).&lt;br /&gt;
&lt;br /&gt;
== Miscellaneous ==&lt;br /&gt;
&lt;br /&gt;
=== Debug mode ===&lt;br /&gt;
&lt;br /&gt;
* New debug command functionality (setting additional status.variables, possibly terrain)&lt;br /&gt;
&lt;br /&gt;
=== woptipng / wesnoth-optipng ===&lt;br /&gt;
&lt;br /&gt;
For lossless png compression we use woptipng, a script using an external toolchain (ImageMagick, OptiPNG, Advdef). Perhaps the performance could be improved by using PNGOut instead of advdef. This task includes&lt;br /&gt;
* a quick profiling of compression ratio and decompression speed for both tools&lt;br /&gt;
* if it provides a higher compression while not significantly slowing down decompression, adapt woptipng to use PNGOut&lt;br /&gt;
&lt;br /&gt;
== Bugs ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
&lt;br /&gt;
[[NotSoEasyCoding]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:Future]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=EasyCoding&amp;diff=68481</id>
		<title>EasyCoding</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=EasyCoding&amp;diff=68481"/>
		<updated>2021-09-15T03:24:50Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Existing bugs or undesirable features */ remove issues that have been taken care of&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''We are permanently updating this page. Parts marked by &amp;lt;s&amp;gt;strikeout&amp;lt;/s&amp;gt; will likely be deleted shortly.&lt;br /&gt;
&lt;br /&gt;
This page is here to document easy to do coding tasks. It is not here to double the feature request database, and should only be filled by people that know the code well enough to judge the difficulty of a given task. &lt;br /&gt;
&lt;br /&gt;
If you are such a person, you should feel free to edit this page.&lt;br /&gt;
&lt;br /&gt;
If you're not, you should post a feature request and discuss your idea on the forum or IRC. A coder with better knowledge of the code might give you the green light to add your feature here.&lt;br /&gt;
&lt;br /&gt;
Anybody should feel free to add &amp;quot;clues&amp;quot; to any tasks, that is entry points, traps to avoid, person to contact to discuss and so on.&lt;br /&gt;
&lt;br /&gt;
If you plan to work on a feature, write your name at the bottom of the feature, with the date. Note that if you are too long at working on a feature we'll &amp;quot;free&amp;quot; it back (that is if you're not working on it. If you have problems implementing it, just tell us....)&lt;br /&gt;
&lt;br /&gt;
If none of these are to your liking, feel free to check our [http://bugs.wesnoth.org/?q=is%3Aissue+is%3Aopen+label%3A%22Good+first+issue%22 bug tracker] with a broad spectrum of tasks.&lt;br /&gt;
&lt;br /&gt;
== Engine related features ==&lt;br /&gt;
&lt;br /&gt;
=== Make a &amp;quot;map diff&amp;quot; tool to visualizes changes between two version of a map ===&lt;br /&gt;
&lt;br /&gt;
Maintainers of maps often make small tweaks for balance purposes. For someone who doesn't know the map like the back of their hand it may be very difficult to spot exactly what changed. There isn't any really good way to do this right now -- using the 'diff' tool at command line doesn't give good results for .map files.&lt;br /&gt;
&lt;br /&gt;
Such a tool would be helpful for someone who maintains a map pool containing maps made by others, also for someone who wants to browse a repository and examine balance changes on several maps, which could be instructive for someone who wants to make and balance their own maps.&lt;br /&gt;
&lt;br /&gt;
There's any number of ways to actually do this, one might be to have a feature in the map editor that takes the name of another map and adds map labels to indicate what hexes changed. It could also be a command accessible in debug mode when playing the game, or it could be an additional command-line argument (--diff [map1] [map2] ?) for the game. (It could be a standalone application but that's probably going to end up with much more work for you.)&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/issues/2323&lt;br /&gt;
&lt;br /&gt;
=== Add an option to not store replays ===&lt;br /&gt;
&lt;br /&gt;
As noted in https://wiki.wesnoth.org/SavefileWML all normal savefiles usually contain [replay] and [replay_start] tags that usually take over 50% of the total savefile size and are only used for providing the possibilty to replay scenarios. It's be nice to have an option to skip these information to reduce the savefile size. Alternativeley one could create a tool inrtegated in a load/savegame dialog to remove the replay information from a given savegame file.&lt;br /&gt;
&lt;br /&gt;
See the feature request https://github.com/wesnoth/wesnoth/issues/1457&lt;br /&gt;
&lt;br /&gt;
== MP related features ==&lt;br /&gt;
&lt;br /&gt;
=== Add possibility to switch off mod dependency checking ===&lt;br /&gt;
MP modifications can define dependencies. Currently there's no way to force a &amp;quot;broken&amp;quot; configuration, although overzealous use of the system by UMC authors could make this feature useful. Add an option to the MP game creation screen to switch on/off dependency checking. Ask lipk (forum)/lipkab (IRC).&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/issues/2324&lt;br /&gt;
&lt;br /&gt;
=== Make a header for the add-on when we organize scenarios by add-on in the MP create dialog ===&lt;br /&gt;
&lt;br /&gt;
Using an established GUI technique to signal when one block of scenarios for an add-on ends and the next begins would make it much easier to use this dialog, if the user has several add-ons.&lt;br /&gt;
&lt;br /&gt;
See one proposal here:&lt;br /&gt;
https://github.com/wesnoth/wesnoth/issues/1365&lt;br /&gt;
&lt;br /&gt;
=== Timer pause button ===&lt;br /&gt;
&lt;br /&gt;
Longstanding feature request here:&lt;br /&gt;
https://github.com/wesnoth/wesnoth/issues/1237&lt;br /&gt;
&lt;br /&gt;
Request is for either a button, or a simple chat command like :pause, :unpause, to allow players to put the timer on hold if they are interrupted.&lt;br /&gt;
&lt;br /&gt;
Ideally would be accompanied with a multiplayer-only scenario attribute &amp;quot;number_of_pauses&amp;quot; which decreases each time a client pauses the game. Pause should use a blindfold object to black out the screen, and send a chat message automatically when it occurs / resume occurs. For an example of how it might look, start a local hotseat mp game and enable the Preferences-&amp;gt;General-&amp;gt;Turn Dialog option. (This behavior was introduced in this commit: https://github.com/wesnoth/wesnoth/commit/bab03d554bea92f1ce6f585ef1123202173a3491)&lt;br /&gt;
&lt;br /&gt;
Wintermute/happygrue also has other ideas about how the timer should/could work. For example, instead of their client ending their turn, the networked opponents could be required to &amp;quot;confirm&amp;quot; the time out. (This is probably a networked MP-only idea.)&lt;br /&gt;
&lt;br /&gt;
A very fancy implementation might instead of a counter, allow the other players to vote on whether a pause request is granted.&lt;br /&gt;
&lt;br /&gt;
Make sure that the paused client still responds to network commands, so that it is possible to kick a person who pauses and then doesn't ever unpause.&lt;br /&gt;
&lt;br /&gt;
=== Add a &amp;quot;concede&amp;quot; button ===&lt;br /&gt;
&lt;br /&gt;
Often times in mp, players concede the game before their leader is actually killed. When the game ends 'officially' the map is revealed, which can sometimes be instructive for the losing player. Additionally, it makes it easier to analyze the results of many games, for purposes of balancing, or for maintaining a ladder. So while it may seem small, this would actually be a pretty useful feature.&lt;br /&gt;
&lt;br /&gt;
FIXME: figure out gh issue id&lt;br /&gt;
&lt;br /&gt;
This could be implemented in lua.&lt;br /&gt;
&lt;br /&gt;
=== Show [options] values in the mp lobby gameslist ===&lt;br /&gt;
&lt;br /&gt;
It' be useful for newly joining payer to see the chosen [options] of a mp game, since most likeley there isn't enough space we could for example wigher show the options via an extra dropdown button in th elobby or show them in the mp waiting dialog instead.&lt;br /&gt;
&lt;br /&gt;
See the featurerequest https://github.com/wesnoth/wesnoth/issues/1514&lt;br /&gt;
&lt;br /&gt;
== WML-related features ==&lt;br /&gt;
&lt;br /&gt;
=== Side filter support in [item] ===&lt;br /&gt;
&lt;br /&gt;
currently [items] allows a team_name= attribute to specify for which side the item is displayed, it would be better if [item] allowed a side filter to specify for which side the item is displayed.&lt;br /&gt;
&lt;br /&gt;
See the bug report https://github.com/wesnoth/wesnoth/issues/1477&lt;br /&gt;
&lt;br /&gt;
=== Support variable poison properties ===&lt;br /&gt;
&lt;br /&gt;
See a feature request here:&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/issues/1339&lt;br /&gt;
&lt;br /&gt;
=== Events Manipulation ===&lt;br /&gt;
&lt;br /&gt;
* [event] persistent=&amp;quot;yes&amp;quot; - creates an event that lasts for the rest of the campaign&lt;br /&gt;
* [disable_event][/disable_event] disables the event within which it appears (useful with conditionals). A more complex take on first_time_only=yes/no.&lt;br /&gt;
''This is largely possible with event IDs, but could be useful when multiple copies of an event should be floating around''&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/issues/2326&lt;br /&gt;
&lt;br /&gt;
=== Game Information ===&lt;br /&gt;
&lt;br /&gt;
* [query_location] - queries a location from the user&lt;br /&gt;
** variable&lt;br /&gt;
** [filter_location]&lt;br /&gt;
''Regarding [query_location]: probably a better name would be choose_location. This is used when a wml event is running and needs to get a target location hex from the user. For example, you right click on a catapult unit and choose the menu item &amp;quot;fire Catapult.&amp;quot; Now a [message] says to click on any target within 5 hexes. Now [choose_location] is encountered and the mouse cursor changes to a target sign (possibly customizable). If the user's mouse drifts outside of the allowed locations it changes to a cancel icon. If the operation is cancelled then the variable is cleared and no location stored. --Sapient''&lt;br /&gt;
&lt;br /&gt;
''This can be implemented in lua by using wesnoth.game_events.on_mouse_action and wesnoth.syncronize_choice''&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/issues/2325&lt;br /&gt;
&lt;br /&gt;
== Improvements to AI ==&lt;br /&gt;
&lt;br /&gt;
=== Existing bugs or undesirable features ===&lt;br /&gt;
&lt;br /&gt;
Most of these are probably relatively easy to fix, although we can, of course, not guarantee that.&lt;br /&gt;
&lt;br /&gt;
* Bug: Adding a candidate action mid-turn does not include it where it belongs in terms of its evaluation score. Instead, it gets added at the end of the candidate action list and might not be executed on the current turn, because other CAs with lower scores get evaluated first.&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/issues/2345&lt;br /&gt;
&lt;br /&gt;
* Bug: ai.get_new_enemy_dst_src and related functions have several problems:&lt;br /&gt;
** They use the current moves of the enemy units, which generally are all zero.&lt;br /&gt;
** Hexes occupied by other enemy units count as not reachable.&lt;br /&gt;
** Working around this with a Lua wrapper makes this actually slower than ai_helper.get_enemy_dst_src, which makes the functions pretty much useless, as speed is the only thing speaking for them.&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/issues/2346&lt;br /&gt;
&lt;br /&gt;
=== Add new features to the AI ===&lt;br /&gt;
&lt;br /&gt;
* Add a cost function to the C++ path finding code that takes avoided hexes into account, that is, hexes defined in an [ai][avoid] tag onto which the AI never moves.  Also add an option to the Lua wesnoth.find_path() function with which using this cost functions can be turned on and off.&lt;br /&gt;
** The background behind this task is the way how the default AI's move-to-targets candidate action works.  It does path finding for units without taking avoided hexes into account, and ''afterwards'' eliminates moves for which the end point falls onto an avoided hex.  As a result, some units might not move at all, even if it is, in principle, easily possible to move around an avoided area.  The inclusion of the new path-finding in the AI's move-to-targets phase could therefore be a follow-up task.&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/issues/2347&lt;br /&gt;
&lt;br /&gt;
* Add a Lua function ai.get_individual_attacks() that returns all the single attacks the side can do (as opposed to attack combinations).  This needs to include an option to recalculate and take the current situation on the map into account (making the function believe that the game state has changed).&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/issues/2348&lt;br /&gt;
&lt;br /&gt;
* Set up an option the allows us to exclude units from the move-to-targets phase similar to [filter_own] for the combat phase (other than setting ai_special=guardian, which has a number of other side effects). Actually, having this option for all CA's would be nice.&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/issues/2349&lt;br /&gt;
&lt;br /&gt;
* Change the aspects &amp;quot;passive_leader&amp;quot;, &amp;quot;passive_leader_shares_keep&amp;quot;, &amp;quot;leader_ignores_keep&amp;quot; to work with multiple leaders. The idea is to change the type of those aspects from yes/no to string so both &amp;quot;passive_leader=yes&amp;quot; and &amp;quot;passive_leader=leader_id_1,leader_id_2&amp;quot; is possible.&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/issues/2350&lt;br /&gt;
&lt;br /&gt;
== GUI2 features ==&lt;br /&gt;
&lt;br /&gt;
Although mordante developed a new GUI system, GUI2, some dialogs still use the older GUI1 and could be improved or transferred. mordante has since disappeared, leaving GUI2's core code unmaintained; shadowm and vultraz should be able to answer most GUI2 (and even GUI1) related questions in his absence.&lt;br /&gt;
&lt;br /&gt;
* Information on the wiki can be found under [[GUIToolkit]] on the wiki.&lt;br /&gt;
* The source code is under [https://github.com/wesnoth/wesnoth/tree/master/src/gui src/gui/]&lt;br /&gt;
* All mainline GUI2 WML resides in [https://github.com/wesnoth/wesnoth/tree/master/data/gui data/gui/]&lt;br /&gt;
&lt;br /&gt;
* Make games sortable in the lobby (open slots, total number of players, era, XP modifier, gold per village, fog/shroud)&lt;br /&gt;
* The biggest feature yet to be converted to GUI2 is the in-game Help documentation (first available from the main screen).&lt;br /&gt;
&lt;br /&gt;
== Miscellaneous ==&lt;br /&gt;
&lt;br /&gt;
=== Debug mode ===&lt;br /&gt;
&lt;br /&gt;
* New debug command functionality (setting additional status.variables, possibly terrain)&lt;br /&gt;
&lt;br /&gt;
=== woptipng / wesnoth-optipng ===&lt;br /&gt;
&lt;br /&gt;
For lossless png compression we use woptipng, a script using an external toolchain (ImageMagick, OptiPNG, Advdef). Perhaps the performance could be improved by using PNGOut instead of advdef. This task includes&lt;br /&gt;
* a quick profiling of compression ratio and decompression speed for both tools&lt;br /&gt;
* if it provides a higher compression while not significantly slowing down decompression, adapt woptipng to use PNGOut&lt;br /&gt;
&lt;br /&gt;
== Bugs ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
&lt;br /&gt;
[[NotSoEasyCoding]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:Future]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=LuaAPI/ai&amp;diff=68330</id>
		<title>LuaAPI/ai</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=LuaAPI/ai&amp;diff=68330"/>
		<updated>2021-07-10T14:52:21Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: Various minor changes&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;div class=&amp;quot;tright&amp;quot;&amp;gt; __TOC__ &amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Code executed by [[Wesnoth AI]] components has access to the &amp;lt;tt&amp;gt;ai&amp;lt;/tt&amp;gt; module, which contains functions for controlling the AI and querying AI information. Thus, all functions on this page are only available to AI code.&lt;br /&gt;
&lt;br /&gt;
== Game State Queries ==&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''ai.read_only''' &amp;amp;rarr; ''boolean''&lt;br /&gt;
&lt;br /&gt;
The AI module operates in two modes: read-only, and read-write. Read-write mode is used in the execution function for stages and candidate actions, while read-only mode is used in goals and aspects and in the evaluation function for candidate actions. The '''read_only''' key allows you to distinguish these cases at runtime, if necessary. Functions that are only available in read-write mode will be tagged (mutable) in the descriptions on this page.&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''ai.side''' &amp;amp;rarr; ''number''&lt;br /&gt;
&lt;br /&gt;
The side number that the AI module is bound to.&lt;br /&gt;
&lt;br /&gt;
=== ai.aspects ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''ai.aspects'''.''aspect'' &amp;amp;rarr; ''value''&lt;br /&gt;
&lt;br /&gt;
Provides access to the aspects of the AI engine. This fetches the result of the aspect evaluation given the game state when it was last refreshed (see [[Wesnoth_AI_Framework#Some_more_on_the_invalidation_keys|aspect invalidation rules]]), not its definition. So, for example, '''ai.aspects.avoid''' returns the list of avoided hexes, not the &lt;br /&gt;
[[StandardLocationFilter|Standard Location Filter]] that produced them, and a [[#Dynamic_Lua_Aspects|dynamic Lua aspect]] will return the result of calling the function, not the function itself. For more details, see the definition of each aspect on [[AiWML]].&lt;br /&gt;
&lt;br /&gt;
The following aspects return scalar values (numbers, strings, or booleans):&lt;br /&gt;
* '''aggression'''&lt;br /&gt;
* '''caution'''&lt;br /&gt;
* '''grouping'''&lt;br /&gt;
* '''leader_aggression'''&lt;br /&gt;
* '''leader_ignores_keep'''&lt;br /&gt;
* '''leader_value'''&lt;br /&gt;
* '''passive_leader'''&lt;br /&gt;
* '''passive_leader_shares_keep'''&lt;br /&gt;
* '''recruitment_diversity'''&lt;br /&gt;
* '''recruitment_randomness'''&lt;br /&gt;
* '''retreat_enemy_weight'''&lt;br /&gt;
* '''retreat_factor'''&lt;br /&gt;
* '''scout_village_targeting'''&lt;br /&gt;
* '''simple_targeting'''&lt;br /&gt;
* '''support_villages'''&lt;br /&gt;
* '''village_value'''&lt;br /&gt;
* '''villages_per_scout'''&lt;br /&gt;
&lt;br /&gt;
The following aspects return tables:&lt;br /&gt;
&lt;br /&gt;
* '''advancements''' – the values of a [[LuaAPI/location_set|location set]] mapping locations to a list of possible advancements for the unit on that hex. To convert this to a fully functional standard  location set:&lt;br /&gt;
*:&amp;lt;syntaxhighlight lang=lua&amp;gt;&lt;br /&gt;
local location_set = wesnoth.require &amp;quot;location_set&amp;quot;&lt;br /&gt;
local ls = location_set.create()&lt;br /&gt;
ls.values = ai.aspects.advancements&lt;br /&gt;
-- Now you can use it as a regular location set&lt;br /&gt;
print(ls[{5,6}]) -- might print out {&amp;quot;Elvish Hero&amp;quot;, &amp;quot;Elvish Captain&amp;quot;} for example&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* '''attacks''' – a table with keys '''own''' containing all units that may attack and '''enemy''' containing all units that may be attacked.&lt;br /&gt;
* '''avoid''' – an array of locations.&lt;br /&gt;
* '''leader_goal''' – the WML content of the leader_goal aspect&lt;br /&gt;
* '''recruitment_instructions''' – the WML content of the recruitment_instructions aspect&lt;br /&gt;
* '''recruitment_more''' – an array of strings where each string is a usage, a unit type ID, or an integer&lt;br /&gt;
* '''recruitment_pattern''' – an array of usage strings (see [[UnitTypeWML]] for an explanation of usage)&lt;br /&gt;
* '''recruitment_save_gold''' – the WML content of the recruitment_save_gold aspect&lt;br /&gt;
&lt;br /&gt;
=== ai.get_attacks ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''ai.get_attacks'''() &amp;amp;rarr; ''array of attack analyses''&lt;br /&gt;
&lt;br /&gt;
Get all possible attacks the AI is able to perform, and an analysis of each. If multiple AI units can attack the same enemy, all different combinations of AI units are listed. However, for a given combination of AI units, only one attack (the one the AI considers best) is listed. In other words, permutations of those units placed on different hexes are not included, as that would result in too long a list.&lt;br /&gt;
&lt;br /&gt;
Each attack analysis table has the following keys:&lt;br /&gt;
&lt;br /&gt;
* ''analysis''.'''rating'''() &amp;amp;rarr; ''numeric rating''&lt;br /&gt;
* ''analysis''.'''movements''' &amp;amp;rarr; ''array of location pairs''&lt;br /&gt;
** ''move''.'''src''' &amp;amp;rarr; ''location''&lt;br /&gt;
** ''move''.'''dst''' &amp;amp;rarr; ''location''&lt;br /&gt;
* ''analysis''.'''target''' &amp;amp;rarr; ''location''&lt;br /&gt;
* ''analysis''.'''target_value''' &amp;amp;rarr; ''number''&lt;br /&gt;
* ''analysis''.'''avg_losses''' &amp;amp;rarr; ''number''&lt;br /&gt;
* ''analysis''.'''chance_to_kill''' &amp;amp;rarr; ''probability''&lt;br /&gt;
* ''analysis''.'''avg_damage_inflicted''' &amp;amp;rarr; ''number''&lt;br /&gt;
* ''analysis''.'''target_starting_damage''' &amp;amp;rarr; ''integer''&lt;br /&gt;
* ''analysis''.'''avg_damage_taken''' &amp;amp;rarr; ''number''&lt;br /&gt;
* ''analysis''.'''resources_used''' &amp;amp;rarr; ''number''&lt;br /&gt;
* ''analysis''.'''terrain_quality''' &amp;amp;rarr; ''number''&lt;br /&gt;
* ''analysis''.'''alternative_terrain_quality''' &amp;amp;rarr; ''number''&lt;br /&gt;
* ''analysis''.'''vulnerability''' &amp;amp;rarr; ''number''&lt;br /&gt;
* ''analysis''.'''support''' &amp;amp;rarr; ''number''&lt;br /&gt;
* ''analysis''.'''leader_threat''' &amp;amp;rarr; ''boolean''&lt;br /&gt;
* ''analysis''.'''uses_leader''' &amp;amp;rarr; ''boolean''&lt;br /&gt;
* ''analysis''.'''is_surrounded''' &amp;amp;rarr; ''boolean''&lt;br /&gt;
&lt;br /&gt;
=== ai.get_targets ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''ai.get_targets'''() &amp;amp;rarr; ''array of targets''&lt;br /&gt;
&lt;br /&gt;
Get a list of all the current AI targets. Each target has the following keys:&lt;br /&gt;
&lt;br /&gt;
* '''loc''': The coordinates of the target, as a 2-element array.&lt;br /&gt;
* '''type''': The type of target, as a string.&lt;br /&gt;
* '''value''': The value of the target, as a real number.&lt;br /&gt;
&lt;br /&gt;
The possible target types are &amp;lt;syntaxhighlight lang='lua' inline&amp;gt;'village', 'leader', 'explicit', 'threat', 'battle aid', 'mass', 'support'&amp;lt;/syntaxhighlight&amp;gt;. More information on the meaning of each type can be found in the [[LuaAI#Lua_Goals_and_Targets|LuaAI documentation]].&lt;br /&gt;
&lt;br /&gt;
=== ai.suitable_keep ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''ai.suitable_keep'''(''unit'') &amp;amp;rarr; ''x'', ''y''&lt;br /&gt;
&lt;br /&gt;
This returns the location of the closest keep to the unit passed as argument.&lt;br /&gt;
&lt;br /&gt;
== Move Maps ==&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''ai.get_dst_src'''() &amp;amp;rarr; ''move map''&lt;br /&gt;
* {{LuaGameOnly}} '''ai.get_src_dst'''() &amp;amp;rarr; ''move map''&lt;br /&gt;
* {{LuaGameOnly}} '''ai.get_enemy_dst_src'''() &amp;amp;rarr; ''move map''&lt;br /&gt;
* {{LuaGameOnly}} '''ai.get_enemy_src_dst'''() &amp;amp;rarr; ''move map''&lt;br /&gt;
&lt;br /&gt;
These functions return move maps – tables that relate the current positions of units with the hexes they can move to. A move map is a [[LuaAPI/location_set|location set]], similar to the advancements map, which can be converted to a standard location set with the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=lua&amp;gt;&lt;br /&gt;
local location_set = wesnoth.require &amp;quot;location_set&amp;quot;&lt;br /&gt;
local ls = location_set.create()&lt;br /&gt;
ls.values = ai.get_dst_src()&lt;br /&gt;
-- Now you can use it as a regular location set&lt;br /&gt;
print(ls[{1,4}]) -- prints out something like {{5,4},{5,3}} or the like&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These functions are defined in ''ai/lua/stdlib.lua'', which is loaded by default if you allow the game to implicitly define a Lua [engine] tag. However, if you require a custom Lua [engine] tag, it must load this file manually with the following code in order to use these functions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang='lua'&amp;gt;&lt;br /&gt;
local ai_stdlib = wesnoth.require('ai/lua/stdlib.lua')&lt;br /&gt;
ai_stdlib.init(ai)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''ai.recalculate_move_maps'''()&lt;br /&gt;
* {{LuaGameOnly}} '''ai.recalculate_enemy_move_maps'''()&lt;br /&gt;
&lt;br /&gt;
Force the move maps to be recalculated. This might be necessary if you've moved units around using '''wesnoth.units.to_map'''.&lt;br /&gt;
&lt;br /&gt;
== AI Actions ==&lt;br /&gt;
&lt;br /&gt;
All these functions instruct the AI to check or perform a specific action, and return a result table indicating the success of the action. The table has the following keys:&lt;br /&gt;
&lt;br /&gt;
* '''gamestate_changed''': a boolean indicating whether anything actually changed as a result of the action&lt;br /&gt;
* '''ok''': a boolean indicating the success of the action&lt;br /&gt;
* '''status''': a numeric status code indicating why the action failed&lt;br /&gt;
* '''result''': a string error code indicating why the action failed&lt;br /&gt;
&lt;br /&gt;
=== ai.attack ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} (mutable) '''ai.attack'''(''attacker'', ''defender'', ''weapon'', ''aggression'') &amp;amp;rarr; ''result''&lt;br /&gt;
&lt;br /&gt;
Execute attack by ''attacker'' against ''defender''. If ''weapon'' is provided, it is the number of the weapon to be used (count starts at 1, not 0, as always in Lua), otherwise the choice of the weapon is left to the AI engine.  If ''aggression'' is provided, it is used to influence the choice of the best weapon.  Obviously, this only makes sense if this choice is left to the engine, that is, if ''weapon'' is set to either 'nil' or '-1'.&lt;br /&gt;
&lt;br /&gt;
=== ai.check_attack ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''ai.check_attack'''(''attacker'', ''defender'', ''weapon'', ''aggression'') &amp;amp;rarr; ''result''&lt;br /&gt;
&lt;br /&gt;
Similar to '''ai.attack''', but does not execute the attack – it merely checks if it would succeed.&lt;br /&gt;
&lt;br /&gt;
=== ai.move ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} (mutable) '''ai.move'''(''unit'', ''to'') &amp;amp;rarr; ''result''&lt;br /&gt;
&lt;br /&gt;
Execute a &amp;quot;partial&amp;quot; move of ''unit'' to hex (''to.x'', ''to.y''). A &amp;quot;partial&amp;quot; move means that the unit keeps whatever movement points it has left after the move.&lt;br /&gt;
&lt;br /&gt;
=== ai.move_full ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} (mutable) '''ai.move_full'''(''unit'', ''to'') &amp;amp;rarr; ''result''&lt;br /&gt;
&lt;br /&gt;
Execute a &amp;quot;full&amp;quot; move of ''unit'' to hex (''to.x'', ''to.y''). A &amp;quot;full&amp;quot; move means that the unit's movement points are set to zero at the end of the move.&lt;br /&gt;
&lt;br /&gt;
=== ai.check_move ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''ai.check_move'''(''unit'', ''to'') &amp;amp;rarr; ''result''&lt;br /&gt;
&lt;br /&gt;
Similar to '''ai.move''' or '''ai.move_full''', but does not execute the move – it merely checks if it would succeed.&lt;br /&gt;
&lt;br /&gt;
=== ai.recall ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} (mutable) '''ai.recall'''(''unit_id'', ''location'') &amp;amp;rarr; ''result''&lt;br /&gt;
&lt;br /&gt;
Recall the unit with id ''unit_id''.  An optional recruit location can be given.  If the location is omitted, a suitable hex is automatically chosen by the AI engine.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|14}} The location must be specified as separate ''x'' and ''y'' parameters.&lt;br /&gt;
&lt;br /&gt;
=== ai.check_recall ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''ai.check_recall'''(''unit_id'', ''location'') &amp;amp;rarr; ''result''&lt;br /&gt;
&lt;br /&gt;
Similar to '''ai.recall''', but does not execute the recall – it merely checks if it would succeed.&lt;br /&gt;
&lt;br /&gt;
=== ai.recruit ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} (mutable) '''ai.recruit'''(''unit_type'', ''location'') &amp;amp;rarr; ''result''&lt;br /&gt;
&lt;br /&gt;
Recruit a unit of type ''unit_type''. An optional recruit location can be given.  If the location is omitted, a suitable hex is automatically chosen by the AI engine.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|14}} The location must be specified as separate ''x'' and ''y'' parameters.&lt;br /&gt;
&lt;br /&gt;
=== ai.check_recruit ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''ai.check_recruit'''(''unit_type'', ''location'') &amp;amp;rarr; ''result''&lt;br /&gt;
&lt;br /&gt;
Similar to '''ai.recruit''', but does not execute the recruit – it merely checks if it would succeed.&lt;br /&gt;
&lt;br /&gt;
=== ai.stopunit_attacks ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} (mutable) '''ai.stopunit_attacks'''(''unit'') &amp;amp;rarr; ''result''&lt;br /&gt;
&lt;br /&gt;
Remove remaining attacks from ''unit''.  This is equivalent to setting &amp;lt;code&amp;gt;attacks_left=0&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== ai.stopunit_moves ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} (mutable) '''ai.stopunit_moves'''(''unit'') &amp;amp;rarr; ''result''&lt;br /&gt;
&lt;br /&gt;
Remove remaining movement points from ''unit''.  This is equivalent to setting &amp;lt;code&amp;gt;moves=0&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== ai.stopunit_all ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} (mutable) '''ai.stopunit_all'''(''unit'') &amp;amp;rarr;&lt;br /&gt;
&lt;br /&gt;
Remove both remaining attacks and remaining movement points from ''unit''.&lt;br /&gt;
&lt;br /&gt;
=== ai.check_stopunit ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} '''ai.check_stopunit'''(''unit'') &amp;amp;rarr; ''result''&lt;br /&gt;
&lt;br /&gt;
Similar to '''ai.stopunit_attacks''', '''ai.stopunit_moves''', or '''ai.stopunit_all''', but does not execute the stop – it merely checks if it would succeed.&lt;br /&gt;
&lt;br /&gt;
=== ai.fallback_human ===&lt;br /&gt;
&lt;br /&gt;
* {{LuaGameOnly}} (mutable) '''ai.fallback_human'''()&lt;br /&gt;
&lt;br /&gt;
Hands over control of this side to the local human player until the end of the current turn. This action does not return a result; it always succeeds.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
&lt;br /&gt;
There are two other functions that are not in the AI module but are important for AI coding:&lt;br /&gt;
&lt;br /&gt;
* [[LuaAPI/wesnoth/sides#wesnoth.sides.debug_ai|wesnoth.sides.debug_ai]]&lt;br /&gt;
* [[LuaAPI/wesnoth/sync#wesnoth.sync.invoke_command|wesnoth.sync.invoke_command]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Lua Reference]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AiWML&amp;diff=68207</id>
		<title>AiWML</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AiWML&amp;diff=68207"/>
		<updated>2021-06-30T14:00:13Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Removed AI Aspects */ attack_depth is removed also&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{WML Tags}}&lt;br /&gt;
&lt;br /&gt;
== Customizing the Default AI Using WML ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
This page contains:&lt;br /&gt;
* Descriptions of the aspects and goals of the default AI&lt;br /&gt;
* Instructions for setting them up at the beginning of the scenario in a [side] tag&lt;br /&gt;
&lt;br /&gt;
This page does '''not''' contain:&lt;br /&gt;
* Instructions for [[LuaAI#Dynamic_Lua_Aspects|setting up aspects dynamically using Lua]].&lt;br /&gt;
* Instructions for [[Modifying_AI_Components#Modifying_Standard_Aspects|changing aspects and goals mid scenario]], although for many of them (for example, for all [[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Aspects|standard aspects]]) the methods are almost identical&lt;br /&gt;
* Instructions for some of the [[Modifying_AI_Components|more complex AI customization tasks]].&lt;br /&gt;
* Anything about [[Micro_AIs|MicroAIs]]&lt;br /&gt;
* [[AI_Recruitment|Advanced recruitment details]]&lt;br /&gt;
&lt;br /&gt;
== Defining Aspects and Goals of the RCA AI ==&lt;br /&gt;
&lt;br /&gt;
[[RCA_AI#Use_of_Aspects_in_the_RCA_AI|Aspects]] and [[RCA_AI#Use_of_Goals_and_Targets_in_the_RCA_AI|goals]] of the [[RCA_AI|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 [[#List_of_AI_Aspects|aspects themselves]] as arguments, as well as the following general keys:&lt;br /&gt;
* '''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 [https://github.com/wesnoth/wesnoth/blob/master/data/core/macros/schedules.cfg 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.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
* '''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. {{DevFeature1.13|5}} In 1.13, the meaning of '''ai_algorithm''' has changed and no longer selects a native AI that's built into the engine, as the RCA AI has now become the core system and no alternatives exist anymore. Instead, it selects a predefined ''[ai]'' definition, such as those defined in ''data/ai/ais''. Since these are defined in WML, you can easily build your own.&lt;br /&gt;
&lt;br /&gt;
An example of a simple use of the [ai] tag to set AI parameters (aspects) is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=wml&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    time_of_day=dusk&lt;br /&gt;
    aggression=0.99&lt;br /&gt;
    caution=0.01&lt;br /&gt;
    [avoid]&lt;br /&gt;
        [filter]&lt;br /&gt;
            side=2,3&lt;br /&gt;
        [/filter]&lt;br /&gt;
        radius=2&lt;br /&gt;
    [/avoid]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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 &amp;lt;code&amp;gt;:inspect&amp;lt;/code&amp;gt; in debug mode to verify whether an aspect was changed successfully.&lt;br /&gt;
&lt;br /&gt;
==List of AI Aspects==&lt;br /&gt;
&lt;br /&gt;
'''Note:''' Only one instance of each aspect will be active for a side at any time. Where multiple instances of the same aspect are included in an [ai] tag, the last whose conditions (time of day, turns, etc.) match will be the only one that applies.&lt;br /&gt;
&lt;br /&gt;
* '''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 &amp;lt;u&amp;gt;only&amp;lt;/u&amp;gt; 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:'''&lt;br /&gt;
** This is a simplified version of the more complex (and arguably more useful) application of a dynamic Lua ''advancements'' aspect, as described [[LuaAI#The_advancements_Aspect|here]].&lt;br /&gt;
** The ''advancements'' aspect only takes effect during the AI turn, that is, on offense only, not on defense.&lt;br /&gt;
** 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).&lt;br /&gt;
&lt;br /&gt;
* '''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.  The description below applies as written to the ''combat'' CA only, but aggression is taken into account by the ''high_xp_attack'' and ''spread_poison'' CAs as well (as of '''Wesnoth 1.13.5''' and '''1.15.3''' respectively).&lt;br /&gt;
** '''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.&lt;br /&gt;
** 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 &amp;gt;0 is found.  The combat CA score is zero if the highest attack score is &amp;lt;=0).&lt;br /&gt;
** 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.&lt;br /&gt;
** All the negative contributions to the score are multiplied by '(1-aggression)'.  This means that:&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
** 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.&lt;br /&gt;
&lt;br /&gt;
*'''[attacks]''': Filters the units considered for combat, both on the AI and the enemy sides.  Applies to the ''combat'', ''high_xp_attacks'' and ''spread_poison'' CAs only.  It cannot be set in the same way as the other aspects and is therefore described in a [[#Filtering_Combat_with_the_attacks_Aspect|separate section]] below.&lt;br /&gt;
&lt;br /&gt;
* '''[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''.&lt;br /&gt;
** '''[[StandardLocationFilter]]''': The locations for the AI to avoid.  Do not use a [filter_location] tag.&lt;br /&gt;
**'''Note:''' Only one instance of an [avoid] aspect will be active for a side at any time. Where multiple instances are included in an [ai] tag, the last whose conditions (time of day, turns, etc.) match will be the only one that applies.&lt;br /&gt;
&lt;br /&gt;
* '''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 [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|evaluation scores]]):&lt;br /&gt;
** ''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].&lt;br /&gt;
** ''Combat'' CA: &lt;br /&gt;
*** 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 &amp;quot;exposure&amp;quot; rating of the units.  This exposure is then subtracted from the 'attack score' as described for ''aggression'' above.&lt;br /&gt;
*** 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.)&lt;br /&gt;
*** If the AI leader is used in an attack, the AI ''always'' uses 'caution=2' for the evaluation of that attack.&lt;br /&gt;
** ''Retreat'' CA: {{DevFeature1.15|3}} This CA has been removed.&lt;br /&gt;
*** 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 &amp;lt;tt&amp;gt;caution &amp;amp;times; their_power &amp;gt; our_power&amp;lt;/tt&amp;gt;. 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.&lt;br /&gt;
*** 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. &lt;br /&gt;
*** The AI never retreats if caution is set to 0 or lower.&lt;br /&gt;
** ''Move-to-targets'' CA:&lt;br /&gt;
*** 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. &amp;quot;Dangerous&amp;quot; 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 &amp;lt;tt&amp;gt;our_strength / their_strength &amp;gt; 0.5 + caution&amp;lt;/tt&amp;gt;. 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.&lt;br /&gt;
***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.&lt;br /&gt;
*** The AI also considers retreating units during the move-to-target phase based on criteria similar to those for the retreat CA.&lt;br /&gt;
&lt;br /&gt;
* '''grouping'''=&amp;quot;offensive&amp;quot;: (string) How the AI should try to group units.  Applies to ''move-to-targets'' CA only.  Possible values:&lt;br /&gt;
** ''offensive'': Makes the AI try to group units together before attacking.&lt;br /&gt;
** ''defensive'': Makes the AI group units together very conservatively, only advancing them much beyond its castle if it has overwhelming force.&lt;br /&gt;
** ''no'': Makes the AI use no grouping behavior.&lt;br /&gt;
&lt;br /&gt;
* '''leader_aggression'''=&amp;quot;-4.0&amp;quot;: 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''.&lt;br /&gt;
&lt;br /&gt;
* '''[leader_goal]'''=&amp;quot;&amp;quot;: Makes the AI try to move its leader to a specific location.  Applies to ''move-leader-to-goals'' CA only.&lt;br /&gt;
** '''x''', '''y''': The location toward which the AI should move its leader.&lt;br /&gt;
** '''auto_remove'''=no: (bool)  If 'no' (default), the AI moves the leader to the goal, after which he stays there until [leader_goal] is [[Modifying_AI_Components#Modifying_Standard_Aspects|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).&lt;br /&gt;
** '''id'''=&amp;quot;&amp;quot;: (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] [[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Aspects|facet]].  Thus, in principle for this to work, you need to set the id of the facet as described [[Modifying_AI_Components#Modifying_Standard_Aspects|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. {{DevFeature1.13|5}} 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.&lt;br /&gt;
**'''max_risk'''=1-caution: (double: meaningful values are &amp;gt;=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.&lt;br /&gt;
&lt;br /&gt;
* '''leader_ignores_keep'''=no: (bool) Set to 'yes' for this aspect to apply to all leaders of a side. {{DevFeature1.15|3}} In addition to 'yes/no', the aspect can now also be set to a comma-separated list of unit ids. The aspect then only applies to leaders with ids matching an id on that list. Setting leader_ignores_keep for some or all leaders means that:&lt;br /&gt;
** The affected 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). Thus, these leaders behave in the same way as any other unit of the AI side.&lt;br /&gt;
** This also means that these 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.&lt;br /&gt;
&lt;br /&gt;
* '''leader_value'''=3: (double) A number 0 or higher which determines the value of enemy leaders as [[#AI_Targets_and_Goals|targets]].  Affects ''move-to-targets CA'' only (and therefore specifically does not apply to attacks).&lt;br /&gt;
&lt;br /&gt;
* '''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''. {{DevFeature1.15|3}} In addition to 'yes/no', the aspect can now also be set to a comma-separated list of unit ids. The aspect then only applies to leaders with ids matching an id on that list.&lt;br /&gt;
&lt;br /&gt;
* '''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. {{DevFeature1.15|3}} In addition to 'yes/no', the aspect can now also be set to a comma-separated list of unit ids. The aspect then only applies to leaders with ids matching an id on that list.&lt;br /&gt;
&lt;br /&gt;
*'''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 [[AI_Recruitment|separate page]].&lt;br /&gt;
&lt;br /&gt;
*'''retreat_factor'''=0.25 (double) {{DevFeature1.15|11}} Affects ''retreat-injured'' CA only. Determines how injured AI units have to be before they attempt retreating. This is approximately the fraction of the units' hitpoints, but other factors play a role also, such as the terrain the units are on, and leaders and poisoned units retreat somewhat earlier. Setting this to 0 (or negative values) disables retreating. Note that even setting this to very large values still requires units to be somewhat injured before they start retreating.&lt;br /&gt;
&lt;br /&gt;
*'''retreat_enemy_weight'''=1.0 (double) {{DevFeature1.15|11}} Affects ''retreat-injured'' CA only. When deciding where to retreat to, the AI considers how much healing a location provides and weighs it against the threat of all the enemies that can get there (balanced by the own units that can get there). In this evaluation, it multiplies the enemy threat rating by ''retreat_enemy_weight'', meaning that the larger its value is, the more a unit tries to move away from enemies.  There are three regimes for this aspect:&lt;br /&gt;
** Positive values: Only locations that provide healing (both from terrain and from adjacent healers which have no MP left) are considered. For regenerating units, all hexes are treated as if they provide healing.&lt;br /&gt;
** Zero: Enemy threats are ignored&lt;br /&gt;
** Negative values: All hexes are considered for retreating. The enemy threat rating is multiplied by the absolute value of the aspect value. Thus, if very negative values are used (e.g. -100), this can be used to make units run away from enemies with little consideration for anything else.&lt;br /&gt;
&lt;br /&gt;
* '''scout_village_targeting'''=3: (double) The AI multiplies the value of village [[#AI_Targets_and_Goals|targets]] for scouts by this value.  Affects ''move-to-targets'' CA only.&lt;br /&gt;
&lt;br /&gt;
* '''simple_targeting'''=no: (bool) If 'yes', the AI moves its units toward [[#AI_Targets_and_Goals|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.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
* '''village_value'''=1: (double) A number 0 or higher which determines how much the AI tries to go for villages as [[#AI_Targets_and_Goals|targets]].  Affects ''move-to-targets'' CA only.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
=== Removed AI Aspects ===&lt;br /&gt;
&lt;br /&gt;
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. {{DevFeature1.13|5}} Except for attack_depth, these have now all been removed.  {{DevFeature1.15|0}} attack_depth is now removed as well.&lt;br /&gt;
&lt;br /&gt;
* '''attack_depth'''=5: (int)&lt;br /&gt;
* '''number_of_possible_recruits_to_force_recruit'''=3.1: (double)&lt;br /&gt;
* '''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.&lt;br /&gt;
** It is, in principle, still possible to use this aspect together with the [[Wesnoth_AI_Framework#Recruitment_Stage|recruitment stage]], but this is discouraged for the reasons given at this link.&lt;br /&gt;
* '''recruitment_ignore_bad_combat'''=no: (bool)&lt;br /&gt;
* '''recruitment_ignore_bad_movement'''=no: (bool)&lt;br /&gt;
&lt;br /&gt;
==AI Targets and Goals==&lt;br /&gt;
&lt;br /&gt;
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 [[RCA_AI#RCA_AI_Aspect_and_Goal_Configuration|base values]].  Additional targets can be defined using the [goal] tag.&lt;br /&gt;
&lt;br /&gt;
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 [[RCA_AI#Use_of_Goals_and_Targets_in_the_RCA_AI|here]].  The sections below only describe how to set up goals in order to add targets for the ''move-to-targets'' CA.&lt;br /&gt;
&lt;br /&gt;
===The [goal] Tag===&lt;br /&gt;
&lt;br /&gt;
The [goal] tag defines units or locations as [[#AI_Targets_and_Goals|move targets]] (not attack targets!) for the AI.  '''Applies to ''move-to-targets'' CA only'''. &amp;lt;-- That is '''extremely important''' to understand, in particular the fact that the [goal] tag has '''no influence whatsoever on attacks'''.&lt;br /&gt;
&lt;br /&gt;
 The following keys/tags can be used:&lt;br /&gt;
* '''name'''=&amp;quot;target&amp;quot;: (string)  The following values are possible and result in different types of targets, as shown in the examples below:&lt;br /&gt;
** ''target'':  The (default) target goal specifies target units (not necessarily enemy units) toward which the AI should move its units. {{DevFeature1.13|5}} target_unit is now a synonym for this.&lt;br /&gt;
** ''target_location'': Specifies target locations toward which the AI should move its units.&lt;br /&gt;
** ''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.&lt;br /&gt;
** ''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.&lt;br /&gt;
** ''protect_my_unit'': ('''deprecated''') &amp;lt;s&amp;gt;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.&amp;lt;/s&amp;gt;&lt;br /&gt;
** ''lua_goal'': A dynamically calculated goal written in Lua. See [[LuaAI#Lua_Goals_and_Targets|here]] for details on how this works.&lt;br /&gt;
&lt;br /&gt;
* '''[criteria]'''=&amp;quot;&amp;quot;: Contains a [[StandardUnitFilter]] (for ''target'', ''protect_unit'' or ''protect_my_unit'') or [[StandardLocationFilter]] (for ''target_location'' or ''protect_location'') describing the targets.&lt;br /&gt;
&lt;br /&gt;
* '''value'''=0: (double) The value of the goal.&lt;br /&gt;
&lt;br /&gt;
* '''protect_radius'''=20: (int) The protection radius.  Applies to ''protect_location'', ''protect_unit'' and ''protect_my_unit''.&lt;br /&gt;
&lt;br /&gt;
===Examples of [goal] Tag Usage===&lt;br /&gt;
&lt;br /&gt;
'''target:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        [criteria] #NOTE: this is a SUF, because we're targeting a unit&lt;br /&gt;
            side=3&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''target_location:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=target_location&lt;br /&gt;
        [criteria] #NOTE: this is a SLF, because we're targeting a location&lt;br /&gt;
            x,y=42,20&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''protect_location:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=protect_location&lt;br /&gt;
        [criteria] #NOTE: this is a SLF, because we're protecting a location&lt;br /&gt;
            x,y=42,20&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        protect_radius=16&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''protect_unit:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=protect_unit&lt;br /&gt;
        [criteria] #NOTE: this is a SUF, because we're protecting a unit&lt;br /&gt;
            side=3&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        protect_radius=16&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''How to use protect_unit to protect the own leader''' (We are side 2)&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=protect_unit&lt;br /&gt;
        [criteria]&lt;br /&gt;
            side=2&lt;br /&gt;
            canrecruit=yes&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        protect_radius=8&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Deprecated AI Targeting Aspects ===&lt;br /&gt;
&lt;br /&gt;
The following AI targeting parameters currently still work, but have been superseded by the [[#The_.5Bgoal.5D_Tag|[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.&lt;br /&gt;
&lt;br /&gt;
* '''[target]'''=&amp;quot;&amp;quot;: '''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.&lt;br /&gt;
** '''[[StandardUnitFilter]]''': Do not use a [filter] tag.&lt;br /&gt;
** '''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.&lt;br /&gt;
&lt;br /&gt;
* '''[protect_location]'''=&amp;quot;&amp;quot;: '''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.&lt;br /&gt;
** '''x''', '''y''': Standard coordinates. These indicate the location the AI is protecting.&lt;br /&gt;
** '''radius''': The radius around it to protect (0 indicates a single hex). {{DevFeature1.13|5}} This key has been changed to protect_radius, though it's preferred to change old code to use the [goal] tag instead.&lt;br /&gt;
** '''value''': The importance of protecting this location.&lt;br /&gt;
&lt;br /&gt;
* '''[protect_unit]'''=&amp;quot;&amp;quot;: '''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.&lt;br /&gt;
** '''[[StandardUnitFilter]]''': The unit(s) to protect. Do not use a [filter] tag.&lt;br /&gt;
** '''radius''': The radius around it to protect (0 indicates a single hex). {{DevFeature1.13|5}} This key has been changed to protect_radius, though it's preferred to change old code to use the [goal] tag instead.&lt;br /&gt;
** '''value''': The importance of protecting this unit.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
== Filtering which Units Participate in which Actions==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}}&lt;br /&gt;
&lt;br /&gt;
Each [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|candidate action of the default AI]] can be restricted to apply to only a subset of the AI units. This is done by adding a '''[filter_own]''' tag to the '''[[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Candidate_Actions|[candidate_action]]]''' tag definition.&lt;br /&gt;
&lt;br /&gt;
Typical use cases:&lt;br /&gt;
* Restricting an action to certain units so that other units are not used to this. For example, village grabbing could be restricted to scout units only, so that slower units do not take detours on their way toward the enemies. See the example below.&lt;br /&gt;
* Excluding units from a certain action in order to have them available for other actions afterward. For example, one might want to add a custom candidate action which moves healers behind injured units (or use the existing [[Micro_AIs|Micro AI]] for this). Ideally, this should happen after ''all'' other units have moved, specifically also after the move-to-targets candidate action (MtT CA) is done. In the default setting, however, the MtT CA would also move the healers. Thus, we would want to exclude them using a '''[filter_own]''' tag.&lt;br /&gt;
* Adding two instances of the same candidate action with different scores to force the order in which units participate in this action. For example, one could add another instance of the move-leader-to-keep CA with a '''[filter_own]''' tag restricting it to one leader, in order to make this leader move to a keep (and therefore recruit) first.&lt;br /&gt;
&lt;br /&gt;
Note that for the combat AI, using the attacks aspect to filter units as described in the next section '''is slightly more efficient'''. Thus, that should be the preferred method if there are many units on the map.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[candidate_action]&lt;br /&gt;
	id=villages&lt;br /&gt;
	engine=cpp&lt;br /&gt;
	name=ai_default_rca::get_villages_phase&lt;br /&gt;
	max_score=60000&lt;br /&gt;
	score=60000&lt;br /&gt;
	[filter_own]&lt;br /&gt;
		type=Wolf Rider&lt;br /&gt;
	[/filter_own]&lt;br /&gt;
[/candidate_action]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Filtering Combat with the ''attacks'' Aspect==&lt;br /&gt;
&lt;br /&gt;
The ''attacks'' aspect lets us filter the units considered by the ''combat'', ''high_xp_attacks'' and ''spread_poison'' 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&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [aspect]&lt;br /&gt;
        id=attacks&lt;br /&gt;
        [facet]&lt;br /&gt;
            invalidate_on_gamestate_change=yes&lt;br /&gt;
            [filter_own]&lt;br /&gt;
                type=Elvish Sorceress,Elvish Enchantress,Elvish Sylph&lt;br /&gt;
            [/filter_own]&lt;br /&gt;
            [filter_enemy]&lt;br /&gt;
                race=undead&lt;br /&gt;
            [/filter_enemy]&lt;br /&gt;
        [/facet]&lt;br /&gt;
    [/aspect]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Several important notes:&lt;br /&gt;
* The ''attacks'' aspect is a ''composite'' aspect and cannot be defined using the simple syntax of the other ''standard'' aspects.  See [[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Aspects|here]] for a description of the syntax of the example.&lt;br /&gt;
* This only works if &amp;lt;code&amp;gt;invalidate_on_gamestate_change=yes&amp;lt;/code&amp;gt; is 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 [[Wesnoth_AI_Framework#Some_more_on_the_invalidation_keys|here]].&lt;br /&gt;
* If [filter_own] or [filter_enemy] are omitted, the selection defaults to all units of the respective sides.&lt;br /&gt;
* '''Most importantly''': The above example results in sorceress-line vs undead attacks being the &amp;lt;u&amp;gt;only&amp;lt;/u&amp;gt; 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:&lt;br /&gt;
** Setting up the [filter_own] or [filter_enemy] [[StandardUnitFilter]]s to adapt dynamically to the situation on the map.&lt;br /&gt;
** 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_AIs#Micro_AI_Test_and_Demo_Scenarios|Micro AI test scenario]] [https://github.com/wesnoth/wesnoth/blob/master/data/ai/micro_ais/scenarios/protect_unit.cfg ''Protect Unit''].&lt;br /&gt;
** Using [[LuaAI#Dynamic_Lua_Aspects|dynamic Lua aspects]] is in principle also possible, but far from easy due to the complex tables this aspect returns.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|5}}&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [attacks]&lt;br /&gt;
        invalidate_on_gamestate_change=yes&lt;br /&gt;
        [filter_own]&lt;br /&gt;
            type=Elvish Sorceress,Elvish Enchantress,Elvish Sylph&lt;br /&gt;
        [/filter_own]&lt;br /&gt;
        [filter_enemy]&lt;br /&gt;
            race=undead&lt;br /&gt;
        [/filter_enemy]&lt;br /&gt;
    [/attacks]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category: WML Reference]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=67284</id>
		<title>Micro AIs</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=67284"/>
		<updated>2021-03-22T16:03:06Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Animals AI: Swarm (ai_type=swarm) */ add optional parameter [filter]&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== A Few General Words about Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
* Micro AIs allow Wesnoth campaign and scenario authors to add new AI functionality without the need for any AI programming. They bring new specialized behavior to Wesnoth that cannot be achieved, at least not easily, by adjusting default AI parameters (such as those described at [[AiWML]]) or with WML code.&lt;br /&gt;
&lt;br /&gt;
* A Micro AI is activated and configured (or deleted) via the [micro_ai] tag, requiring only a few lines of WML code.&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.6''', the Micro AI behavior was inconsistent when it came to '''hidden enemies and petrified units'''.  Some Micro AIs ignored those, while others did not.  In some case there was even a potential for AI errors when an AI unit ran into hidden or petrified enemies.  As of 1.13.6, this has all been fixed.  The Micro AIs now correctly and consistently ignore hidden units and treat petrified units as blocking the hex they are on but having no effect otherwise, in the same way as the default AI does.  '''Important notes:'''&lt;br /&gt;
** It is not possible to re-include hidden/petrified units via the Standard Unit Filters (SUFs) used by some of the Micro AIs. In fact, if this is attempted, the filters will generally not match any unit.&lt;br /&gt;
*** This only applies to SUFs which go directly into the [micro_ai] tag. Any SUF that is a sub-tag of another filter (for example, of a Standard Location Filter) is taken literally and returns all units matching the filter. If hidden or petrified units are to be excluded from such a sub-filter, that needs to be specified directly in that filter.&lt;br /&gt;
** We also note that, because all the Micro AIs are written in Lua, it is possible to [[Micro_AIs#Using_Development_.281.13.29_Version_Micro_AIs_in_your_Stable_.281.12.29_Version_Add-on|move a Micro AI's code into one's add-on]] and modify it to whatever scenario-specific behavior is desired (this does not only apply to hidden/petrified units, but to all behavior). Setting up general Micro AI behavior that works in every possible setting is not feasible, but adapting it to work (only) in the specific setting of a given scenario is often quite simple. If you do not know how to do this, please post a message on the Wesnoth forums and we will be happy to help you with it.&lt;br /&gt;
&lt;br /&gt;
* The '''[[AiWML#List_of_AI_Aspects|default AI aspects]] are, for the most part, not taken into account by the Micro AIs'''.  For example, unless noted otherwise, specifying a value for ''aggression'' does not change the MAI behavior, and locations specified in an ''[avoid]'' tag generally can be entered by units controlled by an MAI.  This is a feature of the Micro AIs, not a bug.  If you feel that a certain default AI aspect should also apply to a Micro AI, please let us know in the [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 Micro AIs Feedback thread].&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.7''', the Micro AIs did not all work reliably when the '''AI side is under shroud''' and it may have been necessary to turn shroud off for the side when using a Micro AI. {{DevFeature1.13|7}} The Micro AIs now all work for sides under shroud. They do this by disregarding shroud for terrain purposes when path finding, consistent with default Wesnoth AI behavior, while still ignoring hidden units (including those hidden under shroud) correctly.&lt;br /&gt;
&lt;br /&gt;
* Most Micro AIs in their default setting apply to all units of a side, although many of them provide parameters for filtering the units that should be affected by the new behavior.  If the Micro AI is applied only to part of the units of a side, the other units follow the default Wesnoth AI behavior.&lt;br /&gt;
&lt;br /&gt;
* Micro AIs are available in mainline since Wesnoth 1.11/1.12.&lt;br /&gt;
&lt;br /&gt;
== Micro AI Test and Demo Scenarios ==&lt;br /&gt;
&lt;br /&gt;
All Micro AIs described here can be checked out in a number of test and demo scenarios.  The easiest way to access the test scenarios is by assigning a hotkey to &amp;quot;Start Test Scenario&amp;quot; in the preferences (as of Wesnoth 1.13.8). Then, simply press the hotkey in the titlescreen and select the desired scenario from the list (e.g. 'micro_ai_test' for the overall &amp;quot;switchboard scenario&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Test scenarios can also be accessed from the command line by typing&lt;br /&gt;
 path/wesnoth-executable -t micro_ai_test&lt;br /&gt;
or (depending on your system and Wesnoth version)&lt;br /&gt;
 path/wesnoth-executable -tmicro_ai_test&lt;br /&gt;
This starts up a &amp;quot;switchboard scenario&amp;quot; in which you can select the Micro AI demonstration scenario you want to check out.  Here, ''path/wesnoth-executable'' needs to be replaced by whatever the path and filename of the Wesnoth executable is on your system.&lt;br /&gt;
&lt;br /&gt;
== Setting up a Micro AI ==&lt;br /&gt;
&lt;br /&gt;
Setting up a Micro AI requires only one simple step:&lt;br /&gt;
==== Activating and configuring (or deleting) a Micro AI ====&lt;br /&gt;
&lt;br /&gt;
Micro AIs are activated, deleted and configured using the [micro_ai] tag.  This tag needs to be placed in [[ActionWML]], that is, in an event, a menu option or the like.  As an example, the following code activates the healer_support Micro AI in its default configuration for Side 2 from the beginning of the scenario:&lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
 &lt;br /&gt;
         # Configure the healer support micro AI&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
'''Keys required for all Micro AIs:'''&lt;br /&gt;
All [micro_ai] tags must contain the following three required keys:&lt;br /&gt;
* '''side''': The side for which the Micro AI is to be added, changed or deleted.  This has to be an individual side number, lists of sides are not supported.&lt;br /&gt;
* '''ai_type''': The type of Micro AI to be added, changed or deleted.  See the following sections for allowed values.&lt;br /&gt;
* '''action''' (string): The action to take concerning the Micro AI.  The following values are allowed: &lt;br /&gt;
** ''add'': Add a Micro AI to a side.&lt;br /&gt;
** ''change'': Change the configuration of an existing Micro AI.  Note that this does not only change the specific parameters provided in the tag, but it replaces the entire existing Micro AI by a new version with the new (and only the new) configuration.  It is therefore equivalent to using first the ''delete'' and then the ''add'' action.&lt;br /&gt;
** ''delete'': Delete an existing Micro AI from a side.&lt;br /&gt;
&lt;br /&gt;
'''Keys required under certain circumstances:'''&lt;br /&gt;
* '''ca_id''': (string) If several MAIs of the same type are used for the same side and one or several of these are to be changed or deleted during a scenario, this key needs to be included so that the [micro_ai] tag knows which of them needs to be changed/deleted.  The same is also true if a Micro AI is changed more than once during a scenario.  We therefore '''strongly recommend always setting ''ca_id''''' for MAIs which are, or might be, changed or deleted during a scenario.&lt;br /&gt;
** '''Note:''' The id(s)/name(s) actually assigned to the CA(s) of the MAI by the engine might be different from the parameter passed here.  It is, however, sufficient to use this ca_id with the [micro_ai] tag add/delete/change actions to identify the MAI uniquely.  (In other words: you don't have to worry about this if you always use the [micro_ai] tag, but might have to figure out the actual name(s) of the CA(s) if you want to delete or change them manually.)&lt;br /&gt;
* Other keys may also be required depending on the Micro AI.  In additional, a number of optional keys may be available to configure the AI behavior.  See the sections on the individual Micro AIs for details.&lt;br /&gt;
&lt;br /&gt;
'''Required keys for deleting a Micro AI:'''&lt;br /&gt;
&lt;br /&gt;
Only a subset of the keys required for setting up a Micro AI are needed when deleting it.  '''side''', '''ai_type''' and '''action'''=delete always need to be provided.  '''ca_id''' needs to be given if it was provided when adding the Micro AI (see above for when this is needed).&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* The ''add'' and ''change'' actions ignore all keys that do not apply to the respective Micro AI type.  The ''delete'' action ignores all keys other than the required keys listed above.&lt;br /&gt;
&lt;br /&gt;
* Some of the Micro AIs use and/or modify default AI components (candidate actions or aspects) when they are added, or reset these components to their default values when they are deleted.  Thus, if you have modified the AI yourself using the default CAs or aspects, some of these modifications might be affected, or might be interfering with the Micro AI.  The sections below indicate whether a Micro AI modifies any default AI components.&lt;br /&gt;
&lt;br /&gt;
===Combining Different Micro AI Types for the Same Side===&lt;br /&gt;
&lt;br /&gt;
No extra steps need to be taken when combining different Micro AIs on the same side.  Simply set up the [micro_ai] tags for all AIs you want to use.&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* While it is ''technically'' possible to combine any number of Micro AIs, this does not mean that all Micro AIs work together in practice.  Some are not compatible because they control units in contradictory ways.  &lt;br /&gt;
* Not all combinations that ''should'' work have been tested yet.  If you find a combination that you believe should work but doesn't, [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 please let us know] and we will see if that can be fixed.&lt;br /&gt;
* The candidate action score almost certainly has to be set (using the ca_score= key) for one or several of the MAIs if several of them are used on the same side.&lt;br /&gt;
&lt;br /&gt;
== Animals Micro AIs ==&lt;br /&gt;
=== General Comments on the Animal Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Animals Micro AIs are meant to simulate the behaviors of certain types of animals.  They are, however, set up so that they can be used with arbitrary unit types and might be applicable in quite diverse situations.  The AIs nevertheless remain named after the animals for which they were written originally, so that they are (to some extent) descriptive of their behavior.&lt;br /&gt;
&lt;br /&gt;
Brief descriptions of the AI behaviors are given in the following subsections.  To get a better feeling for their behavior, check out the &amp;quot;Animals&amp;quot;, &amp;quot;Swarm&amp;quot;, &amp;quot;Wolves&amp;quot; and &amp;quot;Dragon&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  Note that some of the animal AIs are designed for animal types that do not exist in mainline.  In the test scenario, other animal types are therefore substituted for them.&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Wolves (ai_type=wolves) ===&lt;br /&gt;
&lt;br /&gt;
The Wolves Micro AI organizes all &amp;quot;wolf&amp;quot; units (predators) of the side to hunt in a single pack.  They actively chase after the closest prey and try to corner it (not always super successfully), but are easily distracted by other enemy units coming into attack range, to the point where the pack might split up.  The wolves can, however, be told to attack only prey units as well as to try to avoid other types of units (such as larger predators).  When no prey is left, the wolves wander randomly.&lt;br /&gt;
&lt;br /&gt;
This AI works best for a small number of predators (2 - 5) and sparse prey.  Also, the predators should be put on the map in a pack, that is, reasonably close to each other.  For a demonstration of the Wolves AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Note that another wolves AI exists, called [[Micro_AIs#Animals_AI:_Multi-pack_Wolves_.28ai_type.3Dwolves_multipacks.29|Multipack Wolves]], which distributes the wolves of a side into several packs, rather than all of them into one single pack as it is done here.&lt;br /&gt;
&lt;br /&gt;
Enable the Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the predator units.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the prey units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_only_prey'''=no: In the default configuration, the wolves attack all enemy units in attack range.  If this key is set to 'yes', they only attack prey units.&lt;br /&gt;
* '''avoid_type''': Comma-separated list of unit types (on enemy sides) which the wolves try to avoid.  They try to stay out of these units' way as much as possible when moving around (except when they are moving in to attack another enemy unit) and never attack them.&lt;br /&gt;
* '''ca_score'''=90000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 90,000, which is lower than the default combat CA, but higher than most other default CAs which move units.  It can be changed by setting this parameter but in most cases this will not make sense.  Note that the Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Notes on default (RCA) AI aspects:''' &lt;br /&gt;
* If ''attack_only_prey'' or ''avoid_type'' are set, the Wolves Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* Attacks by the wolves are left to the default (RCA) AI, meaning that the value of ''aggression'' has an influence on the behavior.&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI is taken into account. Note: as attacks by the wolves are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=6&lt;br /&gt;
             ai_type=wolves&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Wolf&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Vampire Bat&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             avoid_type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Multi-pack Wolves (ai_type=wolves_multipacks) ===&lt;br /&gt;
&lt;br /&gt;
The Multi-pack Wolves Micro AI is different from the [[Micro_AIs#Animals_AI:_Wolves_.28ai_type.3Dwolves.29|Wolves Micro AI]] in that there can be an arbitrary number of wolf packs, with the pack size being a free parameter.  At the beginning of the scenario, close wolves are grouped into packs in a semi-methodical way.  If the wolves are put on the map in distinct groups of close wolves of the correct number, they will be joined into packs matching these groups.  If, on the other hand, they are spread out all over the map, the method of assigning them to packs is semi-random.&lt;br /&gt;
&lt;br /&gt;
Wolves of the same pack begin by joining each other on the map.  After that, they stay together until only one wolf is left, which then tries to join up with an incomplete pack or with other single wolves.  Individual wolves entering the map during the scenario behave in that way as well.&lt;br /&gt;
&lt;br /&gt;
A second difference to the other Wolves AI is that wolves do not actively hunt here.  For the most part they just wander (often long distance).  However, the pack ferociously (and without regard for its own health) attacks any enemy units that come into range, as long as that does not mean separating the pack by more than a few hexes.  Staying together, or joining with a new wolf assigned to the pack, is the only thing that takes priority over satisfying the wolves' thirst for blood.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Multi-pack Wolves AI behavior, check out the &amp;quot;Wolves&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Multi-pack Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves_multipacks&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Multi-pack Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''pack_size'''=3: (integer) The size of the packs.&lt;br /&gt;
* '''show_pack_number'''=no: If set to &amp;quot;yes&amp;quot;, the wolves' pack numbers will be shown below them.  Note that this means that the pack number of a wolf killed in an attack will remain as a label on the map.  &lt;br /&gt;
* '''type'''=Wolf: Comma-separated list of the unit types which the AI controls. The default value is &amp;quot;Wolf&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves_multipacks [micro_ai] tag usage from the &amp;quot;Wolves&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=wolves_multipacks&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             show_pack_number=yes&lt;br /&gt;
             pack_size=4&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Big Animals (ai_type=big_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Big Animals Micro AI is a simulation of large predators that wander and hunt alone, such as Bears, Giant Spiders or Yetis.  For the most part, these just wander on the terrain that's been defined for them, but they attack enemy units if those happen to come into range.  The AI can be set up so that the Big Animals stay out of the way of other units (such as other large predators), in which case they only attack those if cornered or if they run into them accidentally (such as ambushers).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Big Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Big Animals Micro AI by using&lt;br /&gt;
 ai_type=big_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the animals to which this AI is applied.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': The big animals often go for long distance (multi-turn) journeys.  This tag is a [[StandardLocationFilter|Standard Location Filter]] describing the terrain from which the next goal is chosen.  The default is all terrain on the map. &lt;br /&gt;
* '''[filter_location_wander]''': The terrain on which the big animals will end their moves. This is a [[StandardLocationFilter|Standard Location Filter]].  The default is all terrain on the map.&lt;br /&gt;
* '''[avoid_unit]''': A [[StandardUnitFilter|Standard Unit Filter]] describing all enemy units that the big animals will avoid.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
Here's an example of big_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=big_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Ghast&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [avoid_unit]&lt;br /&gt;
                 type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
             [/avoid_unit]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=1-40,1-18&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Forest Animals (ai_type=forest_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Forest Animals Micro AI simulates a number of different animal behaviors that one might find in a forest, namely animals behaving like deer, rabbits and families of tuskers.  In the following, we will refer to those animals by these types, even though the actual unit types can be selected freely.  They behave as follows:&lt;br /&gt;
* Each deer wanders randomly on (selectable) terrain, except when enemies get in its (the deer's) maximum movement range, in which case it flees to the farthest point it can reach.&lt;br /&gt;
* Tuskers exhibit the same behavior as deer, except when an enemy is next to one of the tusklets.  This enemy is attacked unconditionally by the tuskers trying to protect their young.&lt;br /&gt;
* Tusklets blindly follow the closest adult tusker, except when there is no tusker left, in which case they behave the same as deer.&lt;br /&gt;
* Rabbits also wander randomly, but in addition disappear into their holes (if any are within reach) when enemies are close.  They reappear out of their holes at the beginning of the turn, if it is safe.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Forest Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Forest Animals Micro AI by using&lt;br /&gt;
 ai_type=forest_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Forest Animals AI consists of 4 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-3&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''rabbit_type, tusker_type, tusklet_type, deer_type''': Comma-separated lists of the unit types behaving as the respective animals.  If any of these parameters are not set, those types of animal behavior are skipped by the AI.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain on which the forest animals wander when they are not threatened by enemies.&lt;br /&gt;
* '''rabbit_number'''=6: (integer)  The number of rabbits for the AI to have on the map simultaneously.  The AI puts the missing number of rabbits onto the map at the beginning of the AI's turn (when the rabbits come out of their holes).&lt;br /&gt;
* '''rabbit_enemy_distance'''=3: (number)  Rabbits won't spawn in holes that aren't more than this distance away from all enemies.&lt;br /&gt;
* '''rabbit_hole_img''': Rabbit holes are marked by items on the map.  If this parameter is set, it must be the file name of the item image.  If it is not set, all items on the map are treated as rabbit holes.&lt;br /&gt;
** {{DevFeature1.14|10}} Items used for rabbit holes on the map border used to produce an error. Placing them on the map border therefore needed to be avoided on the scenario setup side. They are now automatically excluded from consideration by the AI code.&lt;br /&gt;
&lt;br /&gt;
Here's an example of forest_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=forest_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             deer_type=Vampire Bat&lt;br /&gt;
             rabbit_type=Giant Rat&lt;br /&gt;
             tusker_type=Ogre&lt;br /&gt;
             tusklet_type=Young Ogre&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=*^F*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Herd and Herders (ai_type=herding) ===&lt;br /&gt;
&lt;br /&gt;
The Herding Micro AI sets up an area on the map where a number of herder units (such as sheep dog) control and protect a herd (such as sheep).  For convenience, we simply refer to them as sheep dogs and sheep in the following, even though arbitrary unit types can be selected for either.&lt;br /&gt;
&lt;br /&gt;
Sheep dogs try to keep their sheep safe.  This involves keeping them inside the herding area, positioning themselves in between the sheep and approaching enemies, and attacking the enemies if those get too close.  If no active herding or protecting move is needed, the dogs go to a random location on the perimeter of the herding area.&lt;br /&gt;
&lt;br /&gt;
Sheep wander aimlessly except when a sheep dog is next to them, in which case they run away from the dog.  The dogs exploit this by positioning themselves on the outside of the sheep, if possible.  Sheep also run away from approaching enemies.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Herding AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Herding Micro AI by using&lt;br /&gt;
 ai_type=herding&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herder units (e.g. dogs).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herd units (e.g. sheep).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain forming the perimeter of the herding area.  In other words, this is the path the herders (dogs) use to patrol around the outside of the herding area.  This must be a set of coordinates ''completely enclosing'' the herding area.  Here's an example of setting up a rectangular perimeter:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         x=10-20,10-20,10,  ,20&lt;br /&gt;
         y=11,   21,   11-21,11-21&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
Another possibility is by defining the path by certain terrain on the map (as it is done in the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]), in which case the filter can simply look like this:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         terrain=Rb&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
* '''herd_x,herd_y''': The coordinate of one hex (''any'' one hex) inside the herding area.  This is needed to define which part of the map is on the ''inside'' of the herding area perimeter defined by [herding_perimeter], and which is the ''outside''. {{DevFeature1.15|0}} '''herd_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''attention_distance'''=8: (integer)  If an enemy is within this distance of a herd unit, but not as close as ''attack_distance'', the herders try to position themselves in between the enemy and the herd unit.&lt;br /&gt;
* '''attack_distance'''=4: (integer)  If an enemy is within this distance of a herd animal, the herders attack this enemy.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Herding AI consists of 6 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-5&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Herding [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=herding&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Footpad&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Troll Whelp&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             herd_x,herd_y=32,28&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=Rb&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Hunter (ai_type=hunter) ===&lt;br /&gt;
&lt;br /&gt;
The Hunter Micro AI leads a unit (or units) through hunt-and-rest cycles.  The units wander in a prescribed area of the map, their hunting ground, until an enemy unit comes within range.  Note that the units do not actively chase down enemies, but wander randomly through the hunting ground until an enemy is encountered.&lt;br /&gt;
&lt;br /&gt;
If enemies are within range of a hunter unit, the hunter attacks the weakest of them.  If a kill is made, it then retreats to its rest location, where it stays for a certain number of turns and until fully healed (whatever happens later).&lt;br /&gt;
&lt;br /&gt;
A few more details:&lt;br /&gt;
* If the hunter's way home is entirely blocked on the return (so that there is no possible path), the normal RCA AI takes over its behavior.&lt;br /&gt;
* However, if the way is blocked by an enemy unit occupying the rest hex, that enemy will be attacked unconditionally.&lt;br /&gt;
* A kill only makes the hunter go home when it is the attacker, not as defender.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Hunter AI behavior, check out the &amp;quot;Dragon&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]. &lt;br /&gt;
&lt;br /&gt;
Enable the Hunter Micro AI by using&lt;br /&gt;
 ai_type=hunter&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''home_x,home_y''': The coordinates of the hex to which the hunter returns and rests at after a kill. {{DevFeature1.15|0}} '''home_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the unit's hunting ground.  If this tag is not provided, the hunter uses all of the map.&lt;br /&gt;
* '''rest_turns'''=3: (integer)  The number of turns the AI stays at home_x,home_y after a kill.  It will not leave its home until this number of turns have passed ''and'' it is fully healed.&lt;br /&gt;
* '''show_messages'''=no: (boolean) If set to yes, the hunter announces whenever its behavior changes to the next phase.  Note that this is meant as debug output for the scenario author.  The messages are not translated and are not included in replays (and are only visible to the host in an MP game).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a hunter [micro_ai] tag usage from the &amp;quot;Dragon&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hunter&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Rowck&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=5-30,1-15&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             home_x,home_y=3,15&lt;br /&gt;
             rest_turns=2&lt;br /&gt;
 &lt;br /&gt;
             show_messages=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Swarm (ai_type=swarm) ===&lt;br /&gt;
&lt;br /&gt;
The Swarm Micro AI uses some very simply algorithms to simulate animal swarm behavior.  Without adjacent enemies, they simply do a random move, trying to stay together and at a certain distance from enemies.  However, if an enemy unit is close to any bat, the swarm scatters.  This is particular fun to watch when one places an enemy unit in the middle of the swarm.  After being scattered, the swarm members slowly rejoin, but not in a very organized way.  Sub-swarms or individual bats might roam around for quite some time before they find their way back.  It is also possible that individual bats (or small groups) split off from the larger swarm at times.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Swarm AI behavior, check out the &amp;quot;Swarm&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Swarm Micro AI by using&lt;br /&gt;
 ai_type=swarm&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Swarm AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_distance'''=5: (int) The minimum distance kept between units of the swarm and enemies when the swarm moves as a whole.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.15|12}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the units.  Only AI units matching this filter participate in the swarm.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''scatter_distance'''=3: (int) Enemies within &amp;quot;scatter_distance&amp;quot; hexes of any swarm unit cause the swarm to scatter, by each unit trying to maximize its individual distance from the enemy.&lt;br /&gt;
* '''vision_distance'''=12: (int) Only units within this distance follow the overall swarm motion (either away from an enemy or of the swarm as a whole).  The smaller this value is set, the less likely the swarm is to stay together or rejoin.  This parameter is meant to simulate how far an individual member of the swarm can see, meaning that the swarm is &amp;quot;out of sight&amp;quot; (and the unit will not be able to follow it) if it is more than this distance away.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Swarm [micro_ai] tag usage from the &amp;quot;Swarm&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=swarm&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Assassin Squad Micro AI (ai_type=assassin) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|6}}&lt;br /&gt;
&lt;br /&gt;
This Micro AI controls one or several assassin units which try to circle around enemy units with the goal of hitting a target unit without being distracted or impeded by other enemies.  The units will always attack the target if such attacks are possible, otherwise they will follow the path of least resistance toward it. Notes:&lt;br /&gt;
&lt;br /&gt;
* The main priority for the AI is to find paths that avoid enemy units. However, if no such path can be found, the assassins will still advance toward the target, taking the path exposing the units to the least potential damage from enemies.&lt;br /&gt;
&lt;br /&gt;
* The units will not necessarily follow the same path. The best path is chosen individually for each unit.&lt;br /&gt;
&lt;br /&gt;
* While it is not a requirement for the AI to work, using ambusher or skirmisher units, or units with the quick trait, makes this AI more lethal in many cases.&lt;br /&gt;
&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded, it does not work otherwise. That means that '''ca_score''' is not a valid key for the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=assassin&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Assassin Squad specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the assassin units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the target unit.&lt;br /&gt;
** It is important to realize that the AI currently only works correctly with a single target unit. It is thus the responsibility of the scenario designer to make sure that this filter matches an individual enemy unit. If several enemy units are to be targeted in the same scenario, several instances of the Assassin Micro AI can be used.&lt;br /&gt;
** The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[prefer]''': A [[StandardLocationFilter|Standard Location Filter]] selecting locations on the map for the assassins to prefer when making their way toward the target unit. This could be used, for example, to make units with the ambush ability move (mostly) through forest or to have the assassins circle around the, say, northern part of the map rather than the south if that is desirable for scenario design purposes.&lt;br /&gt;
** As with avoiding enemies, the units will still move across non-preferred terrain if that is either the only option or ''much'' shorter than preferred terrain&lt;br /&gt;
** Attacking, once the target unit is in range, is not influenced by the [prefer] SLF.&lt;br /&gt;
&lt;br /&gt;
Here is an example of an Assassin Squad [micro_ai] tag usage:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=assassin&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Assassin,Master at Arms,Duelist&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             [prefer] # Circle around northern part of map&lt;br /&gt;
                 x=1-9, 10-99&lt;br /&gt;
                 y=1-37, 1-17&lt;br /&gt;
             [/prefer]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Bottleneck Defense Micro AI (ai_type=bottleneck_defense) ==&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense Micro AI lets you define a location on the map where the AI can take a defensive stance behind a narrow passage (bottleneck).  Units on the front line are backed up by healers and/or units with the leadership ability.  Injured units are moved to the back and replaced by uninjured (or less injured) units.  The units on the front line only attack when it is safe (no retaliation) or when there is a high chance that they will make a kill or level up.  Using this Micro AI only makes sense if there is no way for the enemy sides to move around the bottleneck and attack the AI's units from behind.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Bottleneck&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Bottleneck Defense Micro AI by using&lt;br /&gt;
 ai_type=bottleneck_defense&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Bottleneck Defense specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense AI requires two sets of coordinates that define where it should be taking up its defensive stance, and from which side the enemy attacks at that location.&lt;br /&gt;
&lt;br /&gt;
* '''x,y''': Comma separated lists of the hexes on the front line, where strong units are placed.  All hexes on which the AI makes contact with (can be attacked by) the enemy need to be included here.  Units are placed on them in the order in which they are listed here. {{DevFeature1.15|0}} '''location_id''' can be used instead with named/special locations.&lt;br /&gt;
* '''enemy_x,enemy_y''': Comma separated list of the hexes from which the enemy can attack the AI front line units.  This is needed for the AI to know on which side of the front line support units (healers etc.) need to be placed.  In many cases, it is sufficient to provide only one of the enemy front line hexes, but there are geometries for which that does not work.  It is therefore safer to list all enemy front line hexes here. {{DevFeature1.15|0}} '''enemy_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
By default, the AI places healers and units with the leadership ability both on the front line itself (if they are the strongest units available) and on hexes adjacent to and behind the line.  If different placement is desired, these locations can be overridden with the following keys.&lt;br /&gt;
&lt;br /&gt;
* '''active_side_leader'''=no: (boolean)  If set to 'yes', the side leader participates in the bottleneck defense action after the side's gold has been spent.  If set to 'no' (default), the leader follows default AI behavior (sitting on the keep and recruiting, for the most part).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Bottleneck Defense AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.15|3}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the units.  Only AI units matching this filter participate in the bottleneck defense.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''healer_x,healer_y''': Comma separated list of hexes where healers are placed.  This can be used to keep healers from the front line (or to take up only certain positions on the front line), and/or to override the default healing positions behind the line.  Healers are placed on them in the order in which the hexes are listed, with the exception that hexes on the line always take priority over hexes behind the line. {{DevFeature1.15|0}} '''healer_loc''' can be used instead with named/special locations.&lt;br /&gt;
* '''leadership_x,leadership_y''': Same as ''healer_x,healer_y'', but for units with the leadership ability. {{DevFeature1.15|0}} '''leadership_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
As an example, in the &amp;quot;Bottleneck&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]], there are three front line hexes, two of which are on hill terrain, while the third is a road with a lower defense rating.  The healer and side leader are supposed to participate in combat because they are strong units, but only from the hill hexes to keep them safer.  This is accomplished by using the following keys settings (check out the scenario to see which hex is which):&lt;br /&gt;
 [micro_ai]&lt;br /&gt;
     side=1&lt;br /&gt;
     ai_type=bottleneck_defense&lt;br /&gt;
     action=add&lt;br /&gt;
 &lt;br /&gt;
     x=14,14,14&lt;br /&gt;
     y= 7, 9, 8&lt;br /&gt;
     enemy_x=13,13&lt;br /&gt;
     enemy_y= 8, 9&lt;br /&gt;
 &lt;br /&gt;
     healer_x=14,14,15,15&lt;br /&gt;
     healer_y= 7, 9, 8, 9&lt;br /&gt;
     leadership_x=14,14,15,15&lt;br /&gt;
     leadership_y= 7, 9, 9 ,8&lt;br /&gt;
     active_side_leader=yes&lt;br /&gt;
 [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Fast Micro AI (ai_type=fast_ai) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|0}}&lt;br /&gt;
&lt;br /&gt;
This AI is meant as a fall-back AI for scenarios where there are so many units that attacks and moves done by the default AI are slow with long delays before each move.  It replaces the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions (CAs).  It should be obvious that in order to achieve the faster speed, compromises concerning the quality of the moves have to be made.&lt;br /&gt;
&lt;br /&gt;
Enable the Fast Micro AI by using&lt;br /&gt;
 ai_type=fast_ai&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Combat candidate action:'''&lt;br /&gt;
* Attackers are considered individually one at a time.  The best attack found for a unit is executed without checking whether a better attack by another unit might be possible.&lt;br /&gt;
* The default order in which attackers are considered is by decreasing hitpoints, starting with the strongest unit (in terms of HP).  This can be modified with the optional keys below.&lt;br /&gt;
* By default, if weaker units block attack hexes for a stronger unit, they do not move out of the way (this is the same as for the default AI).  If a hex becomes available after an attack (e.g. due to a weaker unit dying in the attack), all attacks that were possible ''before'' this happened are finished first.  Only after that are attacks by the stronger unit reconsidered.&lt;br /&gt;
* If include_occupied_attack_hexes=yes is set, weaker units move out of the way for stronger units if possible.  This can, however slow down the AI depending on the situation on the map.  Use with caution.&lt;br /&gt;
* {{DevFeature1.13|2}} Previous to this version, attacks by the leader were bunched in with the attacks by the other units.  Now leader attacks are treated separately and contain an extra layer of analysis to determine whether the attack position is &amp;quot;safe&amp;quot; (in an approximate way).  Currently, this is done with two simple considerations:&lt;br /&gt;
** Count all enemy units that can reach the leader's attack position.  If there are more than given by optional parameter 'leader_attack_max_units' (default value 3), do not attack.&lt;br /&gt;
** Add up the hitpoints of all enemies that can reach the leader's attack position.  If this value, multiplied by optional parameter 'leader_weight'(default value 2), is larger than the leaders hitpoints, do not attack.&lt;br /&gt;
** Two optional parameters, 'threatened_leader_fights' and 'leader_additional_threat', can also be used to let the leader attack when the threat at the attack hex is comparable to that at the leader's current position.&lt;br /&gt;
** In order to keep this AI as fast as possible, leader attacks are done by a separate candidate action that is only evaluated when all other CAs are done.&lt;br /&gt;
&lt;br /&gt;
'''Move-to-targets, villages and retreat candidate actions:'''&lt;br /&gt;
* The move-to-targets, villages and retreat candidate actions of the default AI are removed.  Move-to-targets and villages are taken over by the Fast Micro AI, whereas retreat is simply not done.&lt;br /&gt;
* This CA moves units toward goals after attacks are done.  Goals are unowned (if the AI side has a leader only) and enemy-owned villages and enemy leaders.&lt;br /&gt;
* For each goal, units are pre-sorted in order of the fewest moves needed to cover the ''distance'' toward the goal (that is, as if move cost on each hex toward the goal were 1).  Units are then considered sequentially and if a unit can get there &amp;quot;efficiently&amp;quot;, its move is executed without considering other units.&lt;br /&gt;
** A move is considered efficient if the move cost to get to the hex is not more than 'move_cost_factor' larger than the distance to the hex.  'move_cost_factor' is an optional key that defaults to 2.  It can be set to values of 1.1 or larger (see below).&lt;br /&gt;
** If the move for the current unit is not found to be efficient, the AI considers the next unit instead.  If no unit can do an efficient move toward the goal, the best (fastest) of the inefficient moves is selected instead.&lt;br /&gt;
&lt;br /&gt;
* Village goals:&lt;br /&gt;
** Village goals are considered before enemy leader goals so that the AI preferentially chooses scouts (or otherwise fast units) for them.&lt;br /&gt;
** The number of units sent toward villages depends on the value of the default AI aspect village_value.  It is set to village_value/4 times the number of units the AI has left to move.  Thus, by default (village_value=1.0) the AI sends a quarter of its units toward villages, and the rest toward enemy leaders.  This means that the meaningful range of values for village_value is 0..4.&lt;br /&gt;
** Only one unit is moved toward each village.  This consideration also overrides the number of units to move toward villages defined by village_value, topping it at the number of unowned/enemy-owned villages on the map.&lt;br /&gt;
** Setting village_value=0 disables moving units toward villages altogether.  That is, not only are no units chosen, but the village targeting phase of the AI is entirely disabled with no evaluation time used for it.  Note that, unlike with the default AI, this also means that no villages within range of an AI unit are grabbed.&lt;br /&gt;
** The order in which villages are chosen as goals is some combination of them being close to the AI side's leader and being spread out over the map.&lt;br /&gt;
&lt;br /&gt;
* Enemy leader goals:&lt;br /&gt;
** The order of enemy leaders goals is set according to their distance from the AI leader with the closest enemy leader being targeted first (if the AI has a leader, otherwise their order is random).  One unit is sent toward each enemy leader in this order, after which the AI starts with the first enemy leader again.  This is repeated until all units have moved.&lt;br /&gt;
&lt;br /&gt;
* Guardians (units with ai_special=guardian set) are ignored by the fast move-to-targets CA.&lt;br /&gt;
&lt;br /&gt;
'''Tips for speeding up/modifying the Fast Micro AI in specific scenarios:'''&lt;br /&gt;
&lt;br /&gt;
A number of optional keys and default AI aspects can be used to modify the behavior of the Fast Micro AI.  The MAI is set up so that it is a reasonable compromise between speed and quality of the moves in most situations.  However, this might not be ideal for all scenarios here are some things that can be done to speed up the AI (or to get better behavior in some situations by doing the opposite):&lt;br /&gt;
* Use large values for the default AI aspect aggression.  The largest delays before attacks happen (in some circumstances) when the Fast MAI has to consider lots of units with attacks left but decides not to attack with them.  See below for a description of how aggression is used by the fast AI.&lt;br /&gt;
* Use large values of move_cost_factor (see above for description of what this does).  If you want to emphasize units that can move across terrain easily (e.g. bats) to be chosen for village grabbing, you might want to use low values of move_cost_factor, but this might slow down the AI.  The default value should work reasonably well in most standard situations, but modifying the value might work better on specific maps.&lt;br /&gt;
* Don't use dungeon_mode=yes unless absolutely necessary, even in most dungeon crawls.  Test the AI without this setting first and only use it if it is the only way to solve the problem.  Often, a minor tweak to the map will have the same effect, without the speed penalty.&lt;br /&gt;
* Use include_occupied_attack_hexes=yes with caution.  It might (or might not) slow down the attack CA significantly, depending on the scenario.&lt;br /&gt;
* Set village_value=0, which makes the AI not consider villages as goals.  This has not been found to make a large difference on the maps tested so far, but it might have an effect on maps with a very large number of villages.&lt;br /&gt;
* Use attack_hidden_enemies=yes on maps where there are no hidden enemies. Excluding hidden enemies from the attack calculation requires an additional filter to be evaluated that takes extra time. In most cases this will not result in a large computation time saving.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_hidden_enemies'''=no: (boolean)  By default, hidden enemies are excluded from the attack calculation.  Setting this to 'yes' makes the AI also consider those enemies as attack targets.&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.13|2}} A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  As with the default AI, units will not step on hexes defined by the [avoid] tag.  In addition, no goals will be chosen that lie on those hexes.  The one minor exception is that a unit stepping out of the way for another unit might temporarily enter the avoided zone.&lt;br /&gt;
* '''dungeon_mode'''=no: (boolean)  As much as possible, the Fast Micro AI works with distances between hexes rather than path finding.  This works pretty well on open maps and even most dungeon crawls (and similar scenarios), but it might result in some units getting temporarily (and in rare cases permanently) stuck on maps with convoluted labyrinths.  If that is the case, setting dungeon_mode=yes triggers more path finding that avoids such situations (but as a result is somewhat slower).&lt;br /&gt;
* '''include_occupied_attack_hexes'''=no: (boolean) If set to 'yes', the AI moves weaker units out of the way for a stronger attacker.  This might lead to better attacks but is slower, so don't use if speed is essential.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the attackers.  Only AI units matching this filter will attack, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes the side= key set to the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the enemies.  Only enemy units matching this filter will be attacked, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''leader_weight'''=2: (number &amp;gt;= 0) {{DevFeature1.13|2}} A factor specifying how much more valuable the AI considers side leaders than non-leader units in the attack evaluation. This is also used to determine whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''leader_additional_threat'''=1: (number &amp;gt; 0) {{DevFeature1.13|2}} If threatened_leader_fights=yes, the threat at the attack position may be larger by this factor for the leader to leave the keep for an attack.&lt;br /&gt;
* '''leader_attack_max_units'''=3: (number &amp;gt;= 0) {{DevFeature1.13|2}} A number specifying how many enemy units within reach are acceptable when determining whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''move_cost_factor'''=2: (number &amp;gt;= 1.1)  The factor by which the move cost to a goal hex may be larger than the distance to the goal hex for a move to be considered &amp;quot;efficient&amp;quot;.  See description above.  1.1 is used if this is set to values smaller than 1.1.  Smaller values might lead to better behavior, while larger values might speed up the AI.&lt;br /&gt;
* '''weak_units_first'''=no: (boolean) If set to 'yes', inverts the order in which units are considered by the combat CA.  Attacks are then done in order of increasing hitpoints of the attackers.&lt;br /&gt;
* '''skip_combat_ca'''=no: (boolean) If set to 'yes', the fast combat CA is not used.  The default AI's high_xp_attack and combat CAs remains in place in this case.&lt;br /&gt;
* '''skip_move_ca'''=no: (boolean) If set to 'yes', the fast move-to-targets CA is not used.  The default AI's retreat, villages and move-to-targets CAs remains in place in this case.&lt;br /&gt;
* '''threatened_leader_fights'''=no: (boolean) {{DevFeature1.13|2}} By default, the AI leader will not attack if the threat at the attack position is too high.  If this key is set to 'yes', the leader will attack if the threat there is not larger than the threat at his current position multiplied by the value of 'leader_additional_threat'. This is meant as an option for the endgame: when the AI leader is under attack, he might as well go down fighting.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''aggression''': The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to set how aggressive the AI should attack.  It works in roughly the same way as the default AI aspect:&lt;br /&gt;
** The value of (1 - aggression) determines how much the AI values its own units compared to the enemy's units.  Thus, aggression=0 means that attacks are only done if the AI expects to deal (roughly) the same amount of damage as it will receive.  aggression=1 means that expected damage to the AI's units is not considered, only the expected damage to enemy units matters.&lt;br /&gt;
** Damage is calculated as a combination of hitpoints and chance to die, in &amp;quot;gold equivalent units&amp;quot;.&lt;br /&gt;
** Unlike the default AI, aggression is set to 1 for values larger than 1.  Allowed values are thus anything from -infinity to +infinity, although only -infinity to +1.0 make sense.&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.13|2}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
*'''attacks aspect''':  {{DevFeature1.13|2}} The standard RCA AI [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] used to filter own and enemy units for attacks is now also taken into account as long as neither [filter] nor [filter_second] (see above under 'optional keys') are defined in the [micro_ai] tag.&lt;br /&gt;
*'''village_value=1''': (number ranging from 0 to 4) The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to define how much villages should be targeted by the move-to-targets CA.  In the Fast MAI, this defines which fraction of the AI units are moved toward village goals (see above for details).  If set to 0, villages are ignored as goals.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded to the same values as those of the respective [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|default candidate actions]].  ca_score is therefore not a valid optional key.&lt;br /&gt;
* The fast Micro AI modifies (removes) the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions.&lt;br /&gt;
&lt;br /&gt;
Enabling the Fast Micro AI in its default configuration is as simple as this usage example from the &amp;quot;Fast AI&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=fast_ai&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Generalized Goto Micro AI (ai_type=goto) ==&lt;br /&gt;
&lt;br /&gt;
The Goto Micro AI provides a highly configurable method of sending a unit (or units) to a location or set of locations.  The units to be moved are defined using a [[StandardUnitFilter|Standard Unit Filter]] (SUF) [alternatively, all side units are used if the SUF is not provided), while the goto locations are given in a [[StandardLocationFilter|Standard Location Filter]] (SLF).&lt;br /&gt;
&lt;br /&gt;
By default, each unit matching the SUF will move toward the closest goal (in terms of turns needed to get there) matching the SLF via the fastest route given the current situation on the map (locations of enemy units etc.), but this behavior can be influenced by the optional keys listed below.  Note that this means that a unit's goal might change from one turn to the next depending on the situation on the map.&lt;br /&gt;
&lt;br /&gt;
The behavior of the unit(s) once they reach their goal location(s) is configurable as well.  By default, they will stay there until the Goto Micro AI is deleted or changed.&lt;br /&gt;
&lt;br /&gt;
For demonstrations of several Goto Micro AI usages, check out the &amp;quot;Goto&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Goto Micro AI by using&lt;br /&gt;
 ai_type=goto&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Goto specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the possible goal locations for the move.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists). Note that the area defined by [avoid] is not entered at all, even if the end point of the move would lie outside the area again.&lt;br /&gt;
* '''avoid_enemies''': (number&amp;gt;0) By default, units controlled by the Goto Micro AI will not take enemy units into account for route finding, other than the enemies' ZoCs.  By contrast, if this key is set, they will try to avoid traversing hexes on which they can be attacked by enemies.  The larger the value of ''avoid_enemies'', the more the units will try to stay out of the enemies' way.  Notes:&lt;br /&gt;
** Units are willing to take a path that is 'avoid_enemies' movement points longer for each enemy unit that can be avoided by doing so for each hex along the path.  As an example, if avoid_enemies=1 and the shortest path crosses 2 hexes that can be reached by 3 enemies each, the unit is willing to take a path that is up to 6 hexes longer instead.  If avoid_enemies=10 in the same situation, the unit will take a path up to 60 hexes longer.&lt;br /&gt;
** Hexes adjacent to enemy units are always strongly avoided, independent of the value of avoid_enemies.&lt;br /&gt;
** avoid_enemies is ignored if 'use_straight_line=true' is set.&lt;br /&gt;
** Due to an issue with the path finding code, '''tunnels and teleports are ignored in Wesnoth 1.14 and before''' if 'avoid_enemies' is used. This has been fixed in Wesnoth 1.15.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units to be moved.  Defaults to all units of the side if not defined.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''ignore_enemy_at_goal'''=no and '''ignore_units'''=no: (boolean)  In the default configuration, the Goto MAI does not move units if the path to all goal hexes is blocked by enemies.  It is therefore possible to disable the Goto MAI by putting enemy units on all the goal hexes.  That can be changed if either of these two keys is set to 'yes'.&lt;br /&gt;
** 'ignore_enemy_at_goal=yes' makes the AI choose its goals while disregarding any enemy units at the goal hexes.  Even so, a stiff rating penalty is applied to goal hexes occupied by enemies, thus that unoccupied goal hexes are chosen unless all possible goals are occupied by enemies.  Note that this setting ''only'' ignores enemy units at the goal hexes themselves.  It is still possible to disable the AI by putting enemies all around the goals.&lt;br /&gt;
** 'ignore_units=yes' let's the AI ignore ''all'' enemy units when deciding which goal hex(es) to move toward.&lt;br /&gt;
** In both cases, enemies are only ignored for deciding which unit to move toward which goal hex.  For the actual move itself, all enemies and their ZoCs are considered as usual.&lt;br /&gt;
** Both options only take effect if neither avoid_enemies is used, nor use_straight_line is set to 'yes', as neither of these consider enemies as blockers. Or in other words, if avoid_enemies is used, or use_straight_line=yes, it is unnecessary to set ignore_enemy_at_goal or ignore_units to yes.&lt;br /&gt;
* '''release_unit_at_goal'''=no: (boolean) By default, a unit that reaches a goal location will stay there until the Goto Micro AI is deleted or changed (or until the conditions specified in the [goto_goals] SLF change).  If this parameter is set, a unit that has reached its goal will not be controlled by this Micro AI any more starting from the next turn.&lt;br /&gt;
* '''release_all_units_at_goal'''=no: (boolean) Same as ''release_unit_at_goal'', but this will deactivate the Goto Micro AI for all units once the first unit reaches a goal.&lt;br /&gt;
* '''remove_movement'''=yes: (boolean):  {{DevFeature1.15|11}}  By default, when the Goto MAI cannot move a unit (for example, because the path is blocked by enemies), its movement points are still removed. If this option is set to 'no', this step does not happen, so that other AI candidate actions (or even another Goto MAI with different parameters) can take over instead.&lt;br /&gt;
* '''unique_goals'''=no: (boolean)  If set to 'yes', each unit will be sent to a different goal location.  Note that the best goals are recalculated each turn, so a unit might choose a different goal this turn from what it was heading for during the previous turn if the situation on the map has changed.  Also note that if there are more units than goal locations, the left over units will not be handled by the Goto MAI but follow default behavior instead.&lt;br /&gt;
* '''use_straight_line'''=no: (boolean)  By default, units choose the route by which they will get to their goal locations in the smallest number of turns.  If this parameter is set to 'yes', a straight line route (or something pretty close to it at least) will be chosen instead, irrespective of whether this leads into a dead end, whether it is blocked by enemies, etc.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* Depending on the parameters used, the Goto Micro AI might require a lot of path finding to be done.  Thus, while it is possible to set up this AI to move many units to many different locations, a usage like that might require quite a long calculation time.&lt;br /&gt;
&lt;br /&gt;
===Goto Micro AI example usages:===&lt;br /&gt;
&lt;br /&gt;
Here are several examples of goto [micro_ai] tag usages from the &amp;quot;Goto&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
* Send a unit toward a castle (similar to the RCA AI's Goto Candidate Action, but avoiding dead ends and areas ZoCed by enemies)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
             ca_id=messenger1&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=messenger1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=6-7,2-4&lt;br /&gt;
                 terrain=C*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             ca_score=210001&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send all units of the side toward the southern border of the map&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=8&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                  y=33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             avoid_enemies=1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send units to the four corners of the map (with each unit going to a different corner).&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x=1,1,44,44&lt;br /&gt;
                 y=1,33,1,33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             unique_goals=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Set up a guardian unit that doesn't move while no enemy unit is within 8 hexes (ironically, the Goto Micro AI can be used to keep units in place, although using one of the Guardian Micro AIs might often be a better idea)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=guard1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 [filter]&lt;br /&gt;
                     id=guard1&lt;br /&gt;
                 [/filter]&lt;br /&gt;
                 [not]&lt;br /&gt;
                     [filter]&lt;br /&gt;
                         side=1&lt;br /&gt;
                     [/filter]&lt;br /&gt;
                     radius=8&lt;br /&gt;
                 [/not]&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Guardian Micro AIs ==&lt;br /&gt;
=== General Comments on the Guardian Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Guardian Micro AIs can be used to set up different kind of guardian behaviors for a unit or a set of units.  Four different types of guardians are available and are described below.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different guardian behaviors, check out the &amp;quot;Guardians&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
=== Return Guardian  (ai_type=return_guardian)===&lt;br /&gt;
&lt;br /&gt;
A 'return guardian' is a variation of the standard Wesnoth guardian. It has an assigned guard position (GP) to which it returns after attacks on approaching enemies:&lt;br /&gt;
* If at GP with no enemy in reach, do nothing.&lt;br /&gt;
* If at GP with an enemy in reach, attack. Note that the attack is done by the default AI, which might result in the guardian not attacking if the enemy is deemed too strong (this can be influenced by setting the default ''aggression'' value to a different value).&lt;br /&gt;
* If not at GP, return there, no matter whether an enemy is in reach or not.&lt;br /&gt;
* If enemies are blocking the way back to GP, do your best to move around them.&lt;br /&gt;
* If the guardian ends up next to an enemy on the way back, attack that enemy after the move.&lt;br /&gt;
&lt;br /&gt;
Enable the Return Guardian Micro AI by using&lt;br /&gt;
 ai_type=return_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''return_x,return_y''': The coordinate of the hex the unit returns to after each attack.  {{DevFeature1.15|0}} '''return_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=100010: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  This AI consist of 2 CAs, one with score &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt; the other with &amp;lt;tt&amp;gt;ca_score-20&amp;lt;/tt&amp;gt;.  The default value is set so that the first CA gets executed right before the default combat CA, and the other right after.  It usually will not make sense to change this value, but it is possible to do so for custom applications.  {{DevFeature1.13|6}}: This is now changed to 100100 and 99900 in order to avoid conflicts with the new high_XP_attacks candidate action.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a return guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=return_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=return1&lt;br /&gt;
             return_x,return_y=20,2&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
===Stationed Guardian (ai_type=stationed_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A 'stationed guardian' is another variation of the standard Wesnoth guardian with a somewhat more complex behavior than that of the 'return guardian'. Two positions are defined for it, a 'station' and a 'guarded location', as well as a distance. The behavior is as follows:&lt;br /&gt;
* If no enemy is within 'distance' of the guard's current position, do nothing.  This is independent of the guardian being at its station or not.&lt;br /&gt;
* Otherwise: If an enemy is within 'distance' of the guard, but not also within the same distance of the guarded location and the station (all of this simultaneously), move the guard in the direction of the station (or keep the guard in place if the station is unreachable).&lt;br /&gt;
* Otherwise:&lt;br /&gt;
** Pick the enemy unit that is closest to the guarded location.&lt;br /&gt;
** If we can reach it, pick the adjacent hex with the highest defense rating and attack from there.&lt;br /&gt;
** If not in reach, move toward this unit.&lt;br /&gt;
&lt;br /&gt;
Enable the Stationed Guardian Micro AI by using&lt;br /&gt;
 ai_type=stationed_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance parameter as explained above&lt;br /&gt;
* '''station_x,station_y''': The x and y position of the hex that serves as the guardians station. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''guard_x, guard_y''': The x and y position of the guarded location.  {{DevFeature1.13|0}}  These are now optional keys, defaulting to station_x,station_y if not provided. {{DevFeature1.15|0}} '''guard_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a stationed guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=stationed_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=stationed1&lt;br /&gt;
             distance=4&lt;br /&gt;
             station_x,station_y=2,14&lt;br /&gt;
             guard_x,guard_y=3,13&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Coward (ai_type=coward) ===&lt;br /&gt;
&lt;br /&gt;
Cowards are units that, like guardians, sit around doing nothing until an enemy comes into range.  Unlike guardians, however, they run away once enemies approach. Applications might be wild animals, unarmed civilians getting in the way of a battle, etc.  The coward macro can be called with two optional locations, 'seek' and 'avoid':&lt;br /&gt;
* If neither is given, the coward retreats to the position farthest away from the approaching enemies.&lt;br /&gt;
* If 'seek' is given, it preferentially goes toward that location (but getting away from enemies takes priority).&lt;br /&gt;
* If 'avoid' is given, it in addition tries to avoid that location (with both maximizing distance from enemies and going toward 'seek' taking priority).&lt;br /&gt;
* Both 'seek' and 'avoid' may consist of only one coordinate ('x' or 'y'), in which case not a single hex, but a line of hexes is sought or avoided.&lt;br /&gt;
&lt;br /&gt;
Enable the Coward Micro AI by using&lt;br /&gt;
 ai_type=coward&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance within which an enemy must be to &amp;quot;scare&amp;quot; the unit&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_if_trapped'''='no':  {{DevFeature1.13|0}}  With the default settings, a coward never attacks.  If this key is set to 'yes', the coward attacks the weakest (lowest hitpoints) enemy next to which it ends up after moving (such as a cornered animal would do, blindly lashing out without regards for its own safety).&lt;br /&gt;
* '''avoid_x, avoid_y''': The x and y coordinate of the hex to avoid. {{DevFeature1.15|0}} '''avoid_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units from which the cowards flee.  If not set, this defaults to all enemy units.  Note, however, that no &amp;quot;enemy of AI side&amp;quot; condition is added if [filter_second] is specified, meaning that it can include allied and/or enemy units as desired (and even units on the same side as the coward).&lt;br /&gt;
* '''seek_x, seek_y''': The x and y coordinate of the hex to seek. {{DevFeature1.15|0}} '''seek_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a coward [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=coward&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=coward3&lt;br /&gt;
             distance=5&lt;br /&gt;
             seek_x,seek_y=24,5&lt;br /&gt;
             avoid_x,avoid_y=24,15&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Zone guardian (ai_type=zone_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A zone guardian is a unit that, as the name says, guards a zone. It moves randomly inside this zone until an enemy enters it (or a separately defined enemy zone, see below). Applications might be the defense of a castle, a nesting area or for protecting an unit.  The zone macro can be called with an optional SLF, [filter_location_enemy]:&lt;br /&gt;
* If not specified, the zone guard attacks any enemy coming inside its guard zone.&lt;br /&gt;
* If [filter_location_enemy] is given, it attacks any enemy entering this zone and once there are no more enemies, it goes back to patrol in its basic zone.&lt;br /&gt;
&lt;br /&gt;
Enable the Zone Guardian Micro AI by using&lt;br /&gt;
 ai_type=zone_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone to guard.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_enemy]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone where enemies have to be attacked.  If not given, it defaults to [filter_location].&lt;br /&gt;
* '''station_x,station_y''': By default, the zone guardian moves randomly within the zone if there is no enemy to attack.  However, if station_x/y is given, it moves to this position instead during turns when it does not have an enemy to attack.  This position does not need to be inside the zone defined above.  Note: obviously it does not make sense to define [filter_location], [filter_location_enemy] and station_x/y all at the same time.  Only [filter_location] and either one of the latter two (or none of them) should be provided for any zone guardian. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a zone guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=zone_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=zone3&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=22-31,4-11  # This is intentionally chosen to extend past the lake&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             station_x,station_y=32,8&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Hang Out Micro AI (ai_type=hang_out) ==&lt;br /&gt;
&lt;br /&gt;
The Hang Out Micro AI instructs units of the AI side to &amp;quot;hang out&amp;quot; around a certain location, or locations, until a condition for mobilizing the side is met.  If none of the optional keys are given, the AI controls all units of the side to hang around the leader while moving off castle tiles to make room for recruiting.&lt;br /&gt;
&lt;br /&gt;
The AI works by moving each unit to its best hex first using the minimum amount of movement points required for this move.  Once all AI units are in position, movement points are set to zero for all of them simultaneously such that they cannot be used by other AI candidate actions.  This is done because the mobilizing condition might be met as result of one of the moves of the Hang Out AI, after which we want units to be able to continue with other AI actions.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Hang Out and Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Hang Out Micro AI by using&lt;br /&gt;
 ai_type=hang_out&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Hang Out specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  In its default configuration, the Hang Out MAI controls all units of a side, keeping them as close to the side leader(s) as possible while moving off castle tiles to make room for recruiting.  For this purpose, the candidate action score defaults to 170,000, which is just below the score of the default recruiting CA.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''': A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  If neither of these two exists, this parameter is set to default to all castle terrain (&amp;lt;tt&amp;gt;terrain=C*,C*^*,*^C*&amp;lt;/tt&amp;gt;) so that units move off the castle to make room for recruiting.  Note that this does not include keep terrain, otherwise the leader would be moved off the keep as well.&lt;br /&gt;
* '''ca_score'''=170000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 170,000, which means that it is executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Hang Out action (&amp;lt;tt&amp;gt;side=&amp;lt;/tt&amp;gt; is always set and does not need to be provided).  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the locations ''near'' which the AI units should hang out.  This does not mean that the units only move onto terrain specified here, but that they minimize the distance to such terrain.  It defaults to the position of the side leader(s).&lt;br /&gt;
* '''[mobilize_condition]''':  Contains ([[ConditionalActionsWML#Condition_Tags|WML Condition Tags]])  Once the condition set up here has been met, the Hang Out MAI is disabled and the side goes over to other AI activity.  The Hang Out MAI remains disabled after this, even if the condition is not met any more later in the scenario.&lt;br /&gt;
* '''mobilize_on_gold_less_than''': (number) Since side gold cannot be checked in a WML condition tag without storing it in a variable first, this key is provided separately.  It disables the Hang Out Micro AI once the side gold goes below the value specified here for the first time.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
* The Hang Out AI still takes effect if no locations to hang out at are found or if all map locations are to be avoided.  In that case, it simply keeps all units at their current locations.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Hang Out Micro AI configuration from the &amp;quot;Hang Out and Messenger&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hang_out&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [avoid]&lt;br /&gt;
                 terrain=C*,H*,M*,A*,S*,*^F*&lt;br /&gt;
             [/avoid]&lt;br /&gt;
             [mobilize_condition]&lt;br /&gt;
                 [have_unit]&lt;br /&gt;
                     side=1&lt;br /&gt;
                     count=7-99&lt;br /&gt;
                 [/have_unit]&lt;br /&gt;
             [/mobilize_condition]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Healer Support Micro AI (ai_type=healer_support) ==&lt;br /&gt;
&lt;br /&gt;
The Healer Support Micro AI configures the healers of a side to stay behind the battle lines and heal injured and/or threatened units rather than participate in combat under all circumstances.  You can set different levels of aggressiveness for the healers.  &lt;br /&gt;
&lt;br /&gt;
'''Notes'''&lt;br /&gt;
* By default, ''all'' healers of the AI side are controlled by this AI.  This includes in particular also the side leader, if the leader is a healer.  If you do not want the leader to be included (and possibly be moved away from the keep etc.) use the [filter] tag with canrecruit=no (see below).&lt;br /&gt;
* It is not always better to use this Micro AI than the default AI, as it takes healers (which can be strong units) away from the pool of units to choose for attacks.&lt;br /&gt;
* {{DevFeature1.15|3}} This Micro AI now takes unit guardian status and the passive_leader aspect into account.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Healers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Healer Support Micro AI by using&lt;br /&gt;
 ai_type=healer_support&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Healer Support specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the healer units.  If set, only units on the AI side passing this filter ''and'' having the healing ability are considered by this Micro AI.&lt;br /&gt;
*'''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting units to receive healing. If set, only units on the AI side passing this filter ''and'' the other healee selection criteria will be selected for healing by this Micro AI.&lt;br /&gt;
* '''aggression'''=1.0: (float) Sets the aggressiveness of the AI.  This parameter is set up as a float to accommodate future functionality, but it currently acts as a boolean: if set to zero, the AI will never let its healers participate in combat, for all other values it allows them to attack after all other units have attacked and if the healers cannot find any more units to support.  The former behavior might be appropriate in scenarios with only one or few valuable healers, while the latter might work better in scenarios with many healers.&lt;br /&gt;
* '''injured_units_only'''=no: (boolean) If set to 'yes', the AI will only move healers next to units that are already injured and skip units that currently have full hitpoints, but might get injured during the next enemy turn.&lt;br /&gt;
* '''max_threats'''=9999: (integer) The maximum allowable number of enemies that can attack a hex in order for it to be considered for a healer.  As an example, setting 'max_threats=0' means that the AI only moves healers to locations that are entirely safe from the enemy (assuming that none of the units currently on the map dies).  Note that the value of this key is checked against the number of enemies that can make it to the hex, not the number of adjacent hexes from which the healer could be attacked.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI. Note: as attacks by the healers are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The Healer Support Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded.  It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Healer Support Micro AI configuration from the &amp;quot;Healers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             aggression=0&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Lurkers Micro AI (ai_type=lurkers) ==&lt;br /&gt;
&lt;br /&gt;
The Lurker Micro AI controls some or all units of a side in &amp;quot;lurker mode&amp;quot;.  A lurker is a unit that is capable of moving across most terrains, but that only stops on and attacks from specific terrain.  It might also have the ability to hide on this terrain (which is the reason why it is called a lurker).&lt;br /&gt;
&lt;br /&gt;
Lurkers move individually without strategy and always attack the weakest enemy within their reach.  If no enemy is in reach, the lurker does a random move instead - or it just sits and waits (lurks).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of different types of lurker behavior, check out the &amp;quot;Lurkers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Lurkers Micro AI by using&lt;br /&gt;
 ai_type=lurkers&lt;br /&gt;
in the [micro_ai] tag&lt;br /&gt;
&lt;br /&gt;
===Lurkers specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
* '''[filter]''': Specifies which units of the side are controlled by the Lurker Micro AI.  This is a [[StandardUnitFilter|Standard Unit Filter]].  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': Specifies the terrain from which the lurkers attack. This is a [[StandardLocationFilter|Standard Location Filter]].&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_wander]''': Specifies the terrain on which lurkers end their moves if there is no enemy in reach. This is a [[StandardLocationFilter|Standard Location Filter]].  If [filter_location_wander] is not given, it defaults to [filter_location].&lt;br /&gt;
* '''stationary'''=no: (boolean)  If set to yes, a lurker never moves if there is no enemy within attack range.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Lurkers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=4&lt;br /&gt;
             ai_type=lurkers&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Naga Fighter&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=W*,S*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             [filter_location_wander]&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location_wander]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Messenger Escort Micro AI (ai_type=messenger_escort) ==&lt;br /&gt;
&lt;br /&gt;
The Messenger Escort Micro AI lets you define a set of waypoints through which it tries to move one or several of its units, the messenger(s).  The other units escort the messenger, trying to protect it and attacking enemies that are in its way.  Note that a messenger might not strictly hit intermediate waypoints, but that getting close is good enough, especially if a waypoint is occupied by an enemy unit.  It does, however, always try to get to the last waypoint.&lt;br /&gt;
&lt;br /&gt;
'''Notes on the use of several messengers:''' {{DevFeature1.13|0}}  &lt;br /&gt;
* This Micro AI can control several messengers at the same time, which can but do not have to move in a group.  They can be put on the map altogether in one location, or be spread out on the map or across different turns.&lt;br /&gt;
* The farther a messenger has moved through the chain of waypoints, the higher a priority it is for the AI, meaning that farther advanced (on the map) messengers are moved first, and that the escort units try to clear the way and stay with those messengers more so than with those further back on the map.  This order can be inverted using the invert_order= key.&lt;br /&gt;
* If several messengers are on the map, escort units might move from one to the other, the AI making a judgement call where they are needed most.  Closeness to a messenger is generally the most important criterion, but not the only one.&lt;br /&gt;
* If a messenger dies, the escort units previously staying with it now join one of the other messengers if there are any left (otherwise they follow default AI behavior).&lt;br /&gt;
* If you want certain escort units to stay always with the same messenger, use several messenger [micro_ai] tags with different [filter]/[filter_second] combinations.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Messenger Escort Micro AI by using&lt;br /&gt;
 ai_type=messenger_escort&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Messenger Escort specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': {{DevFeature1.13|0}} (Previous to 1.13.0, only id= was allowed.) The id of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated list of waypoint coordinates through which the messenger will go in the given order to reach its goal, given by the last coordinate.  If you want the messenger to go directly to the goal, simply enter a single x,y coordinate here.  Note that the messenger does not have to hit each waypoint exactly (except for the final one), getting close to them is good enough. {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|11}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Messenger Escort AI consists of 3 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-2&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_death_chance'''=0.67 and '''messenger_death_chance'''=0.0: (both floats; ranging from 0 to 1)    If the messenger is adjacent to an enemy at the end of the move, it will only attack the enemy if the chance of death of the enemy is &amp;gt;= enemy_death_chance and if the messenger's chance of death is &amp;lt;= messenger_death_chance (or if it is an attack at a range for which the enemy does not have a weapon).&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|0}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the escort units.  If not set, this defaults to all enemy units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''invert_order'''=no: {{DevFeature1.13|0}} If set to 'yes', the order of priority with which messenger units are considered is inverted, starting with the messenger unit that is the least advanced on the map (see description above).  In most cases, selecting this option results in worse performance of the AI, so use it with caution.  (And obviously it has no effect at all if there is only one messenger controlled by the AI.)&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|11}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Messenger Escort&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=messenger_escort&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=messenger&lt;br /&gt;
             waypoint_x=31,24,27,28&lt;br /&gt;
             waypoint_y=20,14,7,1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Patrol Micro AI (ai_type=patrol) ==&lt;br /&gt;
&lt;br /&gt;
The defines a number of waypoints for a unit (or units) to follow.  Options can be set whether the route should be followed in a loop, as an out-and back, only once, whether units encountered along the way should be attacked, etc.&lt;br /&gt;
&lt;br /&gt;
With the default settings, getting to the next waypoint takes priority over attacking for a patroller, but this can be overridden (as of '''Wesnoth 1.15.12''', using the '''attack_range''' option).  Unless instructed otherwise, the AI thus prefers to move the patrol unit around enemies rather than straight for them.  Also, if a waypoint is occupied by a unit the AI is not instructed to attack, it will (eventually) abandon that waypoint once it gets close enough and move on to the next one.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different patrol behaviors, check out the &amp;quot;Patrols&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Patrol Micro AI it by using&lt;br /&gt;
 ai_type=patrol&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Patrol specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated lists of the waypoint coordinates through which the patrol travels (in order). {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack''': By default (if this key is not set), the patrol unit attacks any unit it ends up next to after a move, or that is within '''attack_range''' (see below).  Alternatively, this parameter can be set to a comma-separated list of unit ids. If that is done, the patrol only attacks these units when it ends up next to them or if it gets within attack_range.  It ignores any other unit.  This can in turn be used to have the patroller not attack at all, by setting ''attack='' to a non-existing id.  The exception to this is an enemy on the last waypoint with ''one_time_only=yes'', which will always be attacked.&lt;br /&gt;
* '''attack_range'''=1: {{DevFeature1.15|12}} If the patrol comes within this many hexes of an enemy unit, it interrupts its patrol route and attacks that enemy (assuming it still has enough movement left to get to the enemy).  The '''attack=''' instructions are taken into account for this.  Note that the patrol generally tries to move around enemies, which means that for the default value of 1, enemies are usually not within attack range, except at the end of the move.&lt;br /&gt;
* '''attack_invisible_enemies'''=no: {{DevFeature1.15|12}} If set to 'yes', the unit also attacks invisible enemies (according to the criteria set by '''attack''' and '''attack_range''').&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''one_time_only'''=no: (boolean)  If set to 'yes', the unit goes through the patrol route only once and then hangs out at the last waypoint.  It also always attacks any enemy unit on the last waypoint, independent of what ''attack='' is set to.&lt;br /&gt;
* '''out_and_back'''=no: (boolean)  By default, a patrol unit returns to the first waypoint after reaching the last, doing the patrol route as a loop.  If this parameter is set to 'yes', the unit instead reverses its course and perpetually does out and backs of the route.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Patrols&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=patrol&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Konrad&lt;br /&gt;
             waypoint_x=9,24,25&lt;br /&gt;
             waypoint_y=21,23,15&lt;br /&gt;
             one_time_only=yes&lt;br /&gt;
             attack=Gertburt&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Protect Unit Micro AI (ai_type=protect_unit) ==&lt;br /&gt;
&lt;br /&gt;
The Protect Unit Micro AI tries to move one or several units to a given location while keeping them safe from enemy attacks. It does so by keeping the protected units away from enemy units and close to friendly units. Other units on the AI side are controlled by the default Wesnoth AI.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Protect Unit&amp;quot; and &amp;quot;The Elves Besieged&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Protect Unit Micro AI by using&lt;br /&gt;
 ai_type=protect_unit&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Protect Unit specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[unit]''': A separate [unit] tag is required for each protected unit(s).  Each tag has required keys '''id''', '''goal_x''', '''goal_y''' for each unit, as shown in the example below. {{DevFeature1.15|0}} '''goal_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''disable_move_leader_to_keep'''=no: (boolean) If set to 'yes', side leaders protected by this AI do not try to return to their keep.  This is necessary for this Micro AI to work with units that are side leaders and that are supposed to move to an off-keep location.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
*The Protect Unit Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].  Depending on the parameters, it might also modify the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|move leader to keep candidate action]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded. It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;HttT: The Elves Besieged&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=protect_unit&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Delfador&lt;br /&gt;
                 goal_x,goal_y=1,2&lt;br /&gt;
             [/unit]&lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
                 goal_x,goal_y=1,1&lt;br /&gt;
             [/unit]&lt;br /&gt;
 &lt;br /&gt;
             disable_move_leader_to_keep=true&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Recruiting Micro AI==&lt;br /&gt;
=== General Comments on the Recruiting Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Recruiting Micro AI replaces the default recruitment AI by alternative recruiting patterns.  Currently there are two recruiting algorithms you can choose from, random recruiting and the rush recruiting used in the new (as of Version 1.11.1) Experimental AI (see below).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of a different recruiting pattern, check out the &amp;quot;Recruiting&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  You can also explore the unit choices by watching the Experimental AI in a multiplayer game.&lt;br /&gt;
&lt;br /&gt;
'''Note:''' Obviously, the Recruitment Micro AI modifies the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|recruitment candidate action]].&lt;br /&gt;
&lt;br /&gt;
=== Random Recruitment AI (ai_type=recruit_random) ===&lt;br /&gt;
&lt;br /&gt;
This AI randomly selects a unit type from the recruitment list of the side.  The probability of choosing each type and the behavior in low-gold situations can be influenced with optional parameters.&lt;br /&gt;
&lt;br /&gt;
Enable the Random Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_random&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* '''[probability]''': This tag sets the probability of selecting the given unit type.  It has two required sub-keys:&lt;br /&gt;
** '''type''': comma-separated list of unit types to which this probability is to be applied&lt;br /&gt;
** '''probability''': (non-negative float)  The probability with which these unit types is to be selected.  All unit types for which no [probability] tag is defined receive a default probability of 1.0, thus values should be chosen relative to that.  As an example, in the code below swordsmen and peasants are given a probability of 8, while no other units have assigned probabilities.  Thus, each of these two unit types is 8 times more likely to be selected than any other individual unit type.&lt;br /&gt;
* '''skip_low_gold_recruiting'''=no: (boolean)  By default, the random recruitment AI chooses a unit to recruit only from those types for which it has sufficient gold.  If this parameter is set to 'yes', it will instead choose from all available unit types and end recruiting for the turn if there is not enough gold to recruit it this turn (even if other, affordable unit types exist).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a random recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_random&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [probability]&lt;br /&gt;
                 type=Swordsman,Peasant&lt;br /&gt;
                 probability=8&lt;br /&gt;
             [/probability]&lt;br /&gt;
 &lt;br /&gt;
             skip_low_gold_recruiting=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Rush Recruitment AI (ai_type=recruit_rushers) ===&lt;br /&gt;
&lt;br /&gt;
This AI replaces the default recruiting with the rush recruiting used in the new (as of Version 1.11.1) Experimental AI.&lt;br /&gt;
&lt;br /&gt;
Enable the Rush Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_rushers&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* {{DevFeature1.15|2}} '''high_level_fraction'''=0: (non-negative number) The approximate fraction of units of level 2 or higher to be recruited. This is defined as fraction of units on the map, not of new units being recruited during a turn or over several turns (which makes a difference if there are already units on the map). The effect is also cumulative per level (starting from level 2), meaning that 1/3 of L2 units will be recruited in the example above, 1/3*1/3=1/9 L3s etc. The default value is zero, which leaves the high-level unit recruiting to the default Experimental AI algorithm resulting usually in very few such units being recruited.&lt;br /&gt;
* '''randomness'''=0.1: (number)  A random number is applied to the rusher recruit engine's score to prevent the recruitment pattern from being too predictable. 0 causes no randomness to be applied, while larger numbers increase the random effect. A value of 1-2 generates results in which the random effect is approximately equal to the scored effect. Extremely high values are essentially entirely random.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a rush recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_rushers&lt;br /&gt;
             action=add&lt;br /&gt;
          [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Simple Attack Micro AI (ai_type=simple_attack) ==&lt;br /&gt;
&lt;br /&gt;
The Simple Attack Micro AI is meant as an addition to the default combat candidate action for when one wants to force the AI to do attacks that it would otherwise not do (for example on units that are 1 XP from leveling) or to execute certain attacks before all others (for example, attack with all &amp;quot;expendable&amp;quot; units first).  It is not set up to be a replacement for the default attacks.  Some notes:&lt;br /&gt;
&lt;br /&gt;
* The algorithm is different from and simpler than that of the default combat CA.  For example, no attack combinations are considered, only individual attacks.  As a result, the [[AiWML#List_of_AI_Aspects|default aspects]] (aggression, caution, [avoid], ...) are not taken into account.  Also, if lots of units are involved, evaluation of this Micro AI is potentially quite a bit slower than of the default attacks.&lt;br /&gt;
&lt;br /&gt;
* If an attack matching the Standard Unit Filters described below is found, this attack will always be executed, no matter how bad the odds are.  Thus, this Micro AI can be used to force the AI to do attacks it would otherwise avoid.&lt;br /&gt;
&lt;br /&gt;
* Other than selecting the attacker/defender units, this AI is currently not configurable.  Let us know if you would like to see certain options to be added.  (No guarantees that we will add them, but we will definitely consider them.)&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Simple Attack&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=simple_attack&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Simple Attack specific keys for the [micro_ai] tag===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  It is, however, in general not desirable to use this AI without at least one of the optional Standard Unit Filters below, as this AI is not meant as a replacement of the default combat candidate action.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=110000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 110,000, which means that it is executed right before the default combat candidate action.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Simple Attack action.  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units the AI should attack.  It defaults to all enemy units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
* '''weapon'''=-1: (integer) The number of the weapon that should be used for this attack.  Weapon numbers start at 1 (not 0).  -1 stands for &amp;quot;best weapon&amp;quot; (by whatever criterion the AI chooses to use).   '''Available from Wesnoth 1.11.8'''&lt;br /&gt;
&lt;br /&gt;
Below are 2 examples of Simple Attack Micro AI configurations from the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  The first one shows how to force the AI to attack with all Soulless and Walking Corpse units first:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110000&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless,Walking Corpse&lt;br /&gt;
             [/filter]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
The second example executes attacks by Soulless on all enemy units that are 1 XP from leveling.  Note that the Lua code in the preload event is there simply in order to code the &amp;quot;1 XP from advancing&amp;quot; condition.  It is not needed for most other SUFs, as can be seen in the previous example.&lt;br /&gt;
     [event]&lt;br /&gt;
         name=preload&lt;br /&gt;
         first_time_only=no&lt;br /&gt;
         [lua]&lt;br /&gt;
             code=&amp;lt;&amp;lt;&lt;br /&gt;
                 function close_to_advancing(unit)&lt;br /&gt;
                     if (unit.experience &amp;gt;= unit.max_experience-1) then&lt;br /&gt;
                         return true&lt;br /&gt;
                     else&lt;br /&gt;
                         return false&lt;br /&gt;
                     end&lt;br /&gt;
                 end&lt;br /&gt;
             &amp;gt;&amp;gt;&lt;br /&gt;
         [/lua]&lt;br /&gt;
     [/event]&lt;br /&gt;
 &lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110001&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless  # No Walking Corpses; L0 units don't advance enemy&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 lua_function = &amp;quot;close_to_advancing&amp;quot;&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
Several Simple Attack Micro AIs can be combined for the same AI side.  The two examples above are both used (note the different values for ca_score) in the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
== Custom Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|5}}&lt;br /&gt;
&lt;br /&gt;
From 1.13.5 onwards, adding a new Micro AI can be easily done without altering any core files or overriding the micro_ai tag. The [micro_ai] tag now looks up its ''ai_type'' parameter in the &amp;lt;code&amp;gt;wesnoth.micro_ais&amp;lt;/code&amp;gt; table and uses the micro AI setup function found there to determine which keys are required and allowed and what candidate actions to add.&lt;br /&gt;
&lt;br /&gt;
A micro AI setup function takes as its single parameter the contents of the [micro_ai] tag and returns three separate values:&lt;br /&gt;
*'''required''': A list of required keys and tags, as strings. Tags are indicated by enclosing them within square brackets in the string. If a key or tag in this list is missing from the [micro_ai] tag, an error will be printed.&lt;br /&gt;
*'''optional''': Similar to above, a list of optional keys and tags, as strings. No error will be printed if a key or tag in this list is missing, nor will an error be printed if it is present (as would be the case for unrecognized keys or tags).&lt;br /&gt;
*'''ca_params''': A table specifying the candidate actions that constitute the micro AI. It should contain an ''ai_id'' key (which specifies a base ID to be used for candidate actions of this micro AI) and a series of candidate action definitions. Each candidate action definition is a table containing the following keys:&lt;br /&gt;
**'''ca_id''': The ID used for the candidate action. This will be combined with the ''ai_id'' to produce the final ID.&lt;br /&gt;
**'''location''': Path to the Lua file that defines the candidate action.&lt;br /&gt;
**'''score''': The maximum score for the candidate action. If the [micro_ai] tag specified a ''ca_score'' parameter, the score here should normally be based on that.&lt;br /&gt;
&lt;br /&gt;
(The return statement would thus look like &amp;lt;syntaxhighlight lang='lua' inline&amp;gt;return required, optional, ca_params&amp;lt;/syntaxhighlight&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
'''Examples''' of micro AI setup functions can be found in the [https://github.com/wesnoth/wesnoth/tree/1.14/data/ai/micro_ais/mai-defs default MAI definitions directory].  See the [https://github.com/wesnoth/wesnoth/blob/1.14/data/ai/micro_ais/mai-defs/animals.lua#L4-L12 Big Animals MAI definition] for a simple example.  For custom micro AIs, the setup code should be put into a ''[lua]'' tag inside a preload event.&lt;br /&gt;
&lt;br /&gt;
Since the function has full access to the contents of the [micro_ai] tag, it can conditionally add candidate actions to '''ca_params''' depending on the values of other keys within the [micro_ai] tag. It can also perform other actions such as adding facets to an aspect; if it does such things, it should pay attention to the ''action'' key to determine whether it's being added or removed.&lt;br /&gt;
&lt;br /&gt;
There are two useful helper functions that can be used by Micro AI setup functions requiring aspect changes. They can be brought into scope by a line similar to the following:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='lua&amp;gt;local MAIH = wesnoth.require(&amp;quot;ai/micro_ais/micro_ai_helper.lua&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* MAIH.add_aspects(side, aspect_params)&lt;br /&gt;
* MAIH.delete_aspects(side, aspect_params)&lt;br /&gt;
&lt;br /&gt;
The ''aspect_params'' parameter is an array of tables, each of which contains the following keys:&lt;br /&gt;
*'''aspect''': The aspect to change.&lt;br /&gt;
*'''facet''': The WML defining the facet. This must include an ''id'' key in order for the aspect to be correctly deleted. For maximum compatibility, incorporate the ''ca_id'' key from the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=67279</id>
		<title>Micro AIs</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=67279"/>
		<updated>2021-03-21T15:56:48Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Patrol specific keys for the [micro_ai] tag: */ add attack_invisible_enemies parameter&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== A Few General Words about Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
* Micro AIs allow Wesnoth campaign and scenario authors to add new AI functionality without the need for any AI programming. They bring new specialized behavior to Wesnoth that cannot be achieved, at least not easily, by adjusting default AI parameters (such as those described at [[AiWML]]) or with WML code.&lt;br /&gt;
&lt;br /&gt;
* A Micro AI is activated and configured (or deleted) via the [micro_ai] tag, requiring only a few lines of WML code.&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.6''', the Micro AI behavior was inconsistent when it came to '''hidden enemies and petrified units'''.  Some Micro AIs ignored those, while others did not.  In some case there was even a potential for AI errors when an AI unit ran into hidden or petrified enemies.  As of 1.13.6, this has all been fixed.  The Micro AIs now correctly and consistently ignore hidden units and treat petrified units as blocking the hex they are on but having no effect otherwise, in the same way as the default AI does.  '''Important notes:'''&lt;br /&gt;
** It is not possible to re-include hidden/petrified units via the Standard Unit Filters (SUFs) used by some of the Micro AIs. In fact, if this is attempted, the filters will generally not match any unit.&lt;br /&gt;
*** This only applies to SUFs which go directly into the [micro_ai] tag. Any SUF that is a sub-tag of another filter (for example, of a Standard Location Filter) is taken literally and returns all units matching the filter. If hidden or petrified units are to be excluded from such a sub-filter, that needs to be specified directly in that filter.&lt;br /&gt;
** We also note that, because all the Micro AIs are written in Lua, it is possible to [[Micro_AIs#Using_Development_.281.13.29_Version_Micro_AIs_in_your_Stable_.281.12.29_Version_Add-on|move a Micro AI's code into one's add-on]] and modify it to whatever scenario-specific behavior is desired (this does not only apply to hidden/petrified units, but to all behavior). Setting up general Micro AI behavior that works in every possible setting is not feasible, but adapting it to work (only) in the specific setting of a given scenario is often quite simple. If you do not know how to do this, please post a message on the Wesnoth forums and we will be happy to help you with it.&lt;br /&gt;
&lt;br /&gt;
* The '''[[AiWML#List_of_AI_Aspects|default AI aspects]] are, for the most part, not taken into account by the Micro AIs'''.  For example, unless noted otherwise, specifying a value for ''aggression'' does not change the MAI behavior, and locations specified in an ''[avoid]'' tag generally can be entered by units controlled by an MAI.  This is a feature of the Micro AIs, not a bug.  If you feel that a certain default AI aspect should also apply to a Micro AI, please let us know in the [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 Micro AIs Feedback thread].&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.7''', the Micro AIs did not all work reliably when the '''AI side is under shroud''' and it may have been necessary to turn shroud off for the side when using a Micro AI. {{DevFeature1.13|7}} The Micro AIs now all work for sides under shroud. They do this by disregarding shroud for terrain purposes when path finding, consistent with default Wesnoth AI behavior, while still ignoring hidden units (including those hidden under shroud) correctly.&lt;br /&gt;
&lt;br /&gt;
* Most Micro AIs in their default setting apply to all units of a side, although many of them provide parameters for filtering the units that should be affected by the new behavior.  If the Micro AI is applied only to part of the units of a side, the other units follow the default Wesnoth AI behavior.&lt;br /&gt;
&lt;br /&gt;
* Micro AIs are available in mainline since Wesnoth 1.11/1.12.&lt;br /&gt;
&lt;br /&gt;
== Micro AI Test and Demo Scenarios ==&lt;br /&gt;
&lt;br /&gt;
All Micro AIs described here can be checked out in a number of test and demo scenarios.  The easiest way to access the test scenarios is by assigning a hotkey to &amp;quot;Start Test Scenario&amp;quot; in the preferences (as of Wesnoth 1.13.8). Then, simply press the hotkey in the titlescreen and select the desired scenario from the list (e.g. 'micro_ai_test' for the overall &amp;quot;switchboard scenario&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Test scenarios can also be accessed from the command line by typing&lt;br /&gt;
 path/wesnoth-executable -t micro_ai_test&lt;br /&gt;
or (depending on your system and Wesnoth version)&lt;br /&gt;
 path/wesnoth-executable -tmicro_ai_test&lt;br /&gt;
This starts up a &amp;quot;switchboard scenario&amp;quot; in which you can select the Micro AI demonstration scenario you want to check out.  Here, ''path/wesnoth-executable'' needs to be replaced by whatever the path and filename of the Wesnoth executable is on your system.&lt;br /&gt;
&lt;br /&gt;
== Setting up a Micro AI ==&lt;br /&gt;
&lt;br /&gt;
Setting up a Micro AI requires only one simple step:&lt;br /&gt;
==== Activating and configuring (or deleting) a Micro AI ====&lt;br /&gt;
&lt;br /&gt;
Micro AIs are activated, deleted and configured using the [micro_ai] tag.  This tag needs to be placed in [[ActionWML]], that is, in an event, a menu option or the like.  As an example, the following code activates the healer_support Micro AI in its default configuration for Side 2 from the beginning of the scenario:&lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
 &lt;br /&gt;
         # Configure the healer support micro AI&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
'''Keys required for all Micro AIs:'''&lt;br /&gt;
All [micro_ai] tags must contain the following three required keys:&lt;br /&gt;
* '''side''': The side for which the Micro AI is to be added, changed or deleted.  This has to be an individual side number, lists of sides are not supported.&lt;br /&gt;
* '''ai_type''': The type of Micro AI to be added, changed or deleted.  See the following sections for allowed values.&lt;br /&gt;
* '''action''' (string): The action to take concerning the Micro AI.  The following values are allowed: &lt;br /&gt;
** ''add'': Add a Micro AI to a side.&lt;br /&gt;
** ''change'': Change the configuration of an existing Micro AI.  Note that this does not only change the specific parameters provided in the tag, but it replaces the entire existing Micro AI by a new version with the new (and only the new) configuration.  It is therefore equivalent to using first the ''delete'' and then the ''add'' action.&lt;br /&gt;
** ''delete'': Delete an existing Micro AI from a side.&lt;br /&gt;
&lt;br /&gt;
'''Keys required under certain circumstances:'''&lt;br /&gt;
* '''ca_id''': (string) If several MAIs of the same type are used for the same side and one or several of these are to be changed or deleted during a scenario, this key needs to be included so that the [micro_ai] tag knows which of them needs to be changed/deleted.  The same is also true if a Micro AI is changed more than once during a scenario.  We therefore '''strongly recommend always setting ''ca_id''''' for MAIs which are, or might be, changed or deleted during a scenario.&lt;br /&gt;
** '''Note:''' The id(s)/name(s) actually assigned to the CA(s) of the MAI by the engine might be different from the parameter passed here.  It is, however, sufficient to use this ca_id with the [micro_ai] tag add/delete/change actions to identify the MAI uniquely.  (In other words: you don't have to worry about this if you always use the [micro_ai] tag, but might have to figure out the actual name(s) of the CA(s) if you want to delete or change them manually.)&lt;br /&gt;
* Other keys may also be required depending on the Micro AI.  In additional, a number of optional keys may be available to configure the AI behavior.  See the sections on the individual Micro AIs for details.&lt;br /&gt;
&lt;br /&gt;
'''Required keys for deleting a Micro AI:'''&lt;br /&gt;
&lt;br /&gt;
Only a subset of the keys required for setting up a Micro AI are needed when deleting it.  '''side''', '''ai_type''' and '''action'''=delete always need to be provided.  '''ca_id''' needs to be given if it was provided when adding the Micro AI (see above for when this is needed).&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* The ''add'' and ''change'' actions ignore all keys that do not apply to the respective Micro AI type.  The ''delete'' action ignores all keys other than the required keys listed above.&lt;br /&gt;
&lt;br /&gt;
* Some of the Micro AIs use and/or modify default AI components (candidate actions or aspects) when they are added, or reset these components to their default values when they are deleted.  Thus, if you have modified the AI yourself using the default CAs or aspects, some of these modifications might be affected, or might be interfering with the Micro AI.  The sections below indicate whether a Micro AI modifies any default AI components.&lt;br /&gt;
&lt;br /&gt;
===Combining Different Micro AI Types for the Same Side===&lt;br /&gt;
&lt;br /&gt;
No extra steps need to be taken when combining different Micro AIs on the same side.  Simply set up the [micro_ai] tags for all AIs you want to use.&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* While it is ''technically'' possible to combine any number of Micro AIs, this does not mean that all Micro AIs work together in practice.  Some are not compatible because they control units in contradictory ways.  &lt;br /&gt;
* Not all combinations that ''should'' work have been tested yet.  If you find a combination that you believe should work but doesn't, [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 please let us know] and we will see if that can be fixed.&lt;br /&gt;
* The candidate action score almost certainly has to be set (using the ca_score= key) for one or several of the MAIs if several of them are used on the same side.&lt;br /&gt;
&lt;br /&gt;
== Animals Micro AIs ==&lt;br /&gt;
=== General Comments on the Animal Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Animals Micro AIs are meant to simulate the behaviors of certain types of animals.  They are, however, set up so that they can be used with arbitrary unit types and might be applicable in quite diverse situations.  The AIs nevertheless remain named after the animals for which they were written originally, so that they are (to some extent) descriptive of their behavior.&lt;br /&gt;
&lt;br /&gt;
Brief descriptions of the AI behaviors are given in the following subsections.  To get a better feeling for their behavior, check out the &amp;quot;Animals&amp;quot;, &amp;quot;Swarm&amp;quot;, &amp;quot;Wolves&amp;quot; and &amp;quot;Dragon&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  Note that some of the animal AIs are designed for animal types that do not exist in mainline.  In the test scenario, other animal types are therefore substituted for them.&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Wolves (ai_type=wolves) ===&lt;br /&gt;
&lt;br /&gt;
The Wolves Micro AI organizes all &amp;quot;wolf&amp;quot; units (predators) of the side to hunt in a single pack.  They actively chase after the closest prey and try to corner it (not always super successfully), but are easily distracted by other enemy units coming into attack range, to the point where the pack might split up.  The wolves can, however, be told to attack only prey units as well as to try to avoid other types of units (such as larger predators).  When no prey is left, the wolves wander randomly.&lt;br /&gt;
&lt;br /&gt;
This AI works best for a small number of predators (2 - 5) and sparse prey.  Also, the predators should be put on the map in a pack, that is, reasonably close to each other.  For a demonstration of the Wolves AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Note that another wolves AI exists, called [[Micro_AIs#Animals_AI:_Multi-pack_Wolves_.28ai_type.3Dwolves_multipacks.29|Multipack Wolves]], which distributes the wolves of a side into several packs, rather than all of them into one single pack as it is done here.&lt;br /&gt;
&lt;br /&gt;
Enable the Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the predator units.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the prey units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_only_prey'''=no: In the default configuration, the wolves attack all enemy units in attack range.  If this key is set to 'yes', they only attack prey units.&lt;br /&gt;
* '''avoid_type''': Comma-separated list of unit types (on enemy sides) which the wolves try to avoid.  They try to stay out of these units' way as much as possible when moving around (except when they are moving in to attack another enemy unit) and never attack them.&lt;br /&gt;
* '''ca_score'''=90000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 90,000, which is lower than the default combat CA, but higher than most other default CAs which move units.  It can be changed by setting this parameter but in most cases this will not make sense.  Note that the Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Notes on default (RCA) AI aspects:''' &lt;br /&gt;
* If ''attack_only_prey'' or ''avoid_type'' are set, the Wolves Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* Attacks by the wolves are left to the default (RCA) AI, meaning that the value of ''aggression'' has an influence on the behavior.&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI is taken into account. Note: as attacks by the wolves are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=6&lt;br /&gt;
             ai_type=wolves&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Wolf&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Vampire Bat&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             avoid_type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Multi-pack Wolves (ai_type=wolves_multipacks) ===&lt;br /&gt;
&lt;br /&gt;
The Multi-pack Wolves Micro AI is different from the [[Micro_AIs#Animals_AI:_Wolves_.28ai_type.3Dwolves.29|Wolves Micro AI]] in that there can be an arbitrary number of wolf packs, with the pack size being a free parameter.  At the beginning of the scenario, close wolves are grouped into packs in a semi-methodical way.  If the wolves are put on the map in distinct groups of close wolves of the correct number, they will be joined into packs matching these groups.  If, on the other hand, they are spread out all over the map, the method of assigning them to packs is semi-random.&lt;br /&gt;
&lt;br /&gt;
Wolves of the same pack begin by joining each other on the map.  After that, they stay together until only one wolf is left, which then tries to join up with an incomplete pack or with other single wolves.  Individual wolves entering the map during the scenario behave in that way as well.&lt;br /&gt;
&lt;br /&gt;
A second difference to the other Wolves AI is that wolves do not actively hunt here.  For the most part they just wander (often long distance).  However, the pack ferociously (and without regard for its own health) attacks any enemy units that come into range, as long as that does not mean separating the pack by more than a few hexes.  Staying together, or joining with a new wolf assigned to the pack, is the only thing that takes priority over satisfying the wolves' thirst for blood.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Multi-pack Wolves AI behavior, check out the &amp;quot;Wolves&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Multi-pack Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves_multipacks&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Multi-pack Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''pack_size'''=3: (integer) The size of the packs.&lt;br /&gt;
* '''show_pack_number'''=no: If set to &amp;quot;yes&amp;quot;, the wolves' pack numbers will be shown below them.  Note that this means that the pack number of a wolf killed in an attack will remain as a label on the map.  &lt;br /&gt;
* '''type'''=Wolf: Comma-separated list of the unit types which the AI controls. The default value is &amp;quot;Wolf&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves_multipacks [micro_ai] tag usage from the &amp;quot;Wolves&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=wolves_multipacks&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             show_pack_number=yes&lt;br /&gt;
             pack_size=4&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Big Animals (ai_type=big_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Big Animals Micro AI is a simulation of large predators that wander and hunt alone, such as Bears, Giant Spiders or Yetis.  For the most part, these just wander on the terrain that's been defined for them, but they attack enemy units if those happen to come into range.  The AI can be set up so that the Big Animals stay out of the way of other units (such as other large predators), in which case they only attack those if cornered or if they run into them accidentally (such as ambushers).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Big Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Big Animals Micro AI by using&lt;br /&gt;
 ai_type=big_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the animals to which this AI is applied.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': The big animals often go for long distance (multi-turn) journeys.  This tag is a [[StandardLocationFilter|Standard Location Filter]] describing the terrain from which the next goal is chosen.  The default is all terrain on the map. &lt;br /&gt;
* '''[filter_location_wander]''': The terrain on which the big animals will end their moves. This is a [[StandardLocationFilter|Standard Location Filter]].  The default is all terrain on the map.&lt;br /&gt;
* '''[avoid_unit]''': A [[StandardUnitFilter|Standard Unit Filter]] describing all enemy units that the big animals will avoid.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
Here's an example of big_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=big_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Ghast&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [avoid_unit]&lt;br /&gt;
                 type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
             [/avoid_unit]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=1-40,1-18&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Forest Animals (ai_type=forest_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Forest Animals Micro AI simulates a number of different animal behaviors that one might find in a forest, namely animals behaving like deer, rabbits and families of tuskers.  In the following, we will refer to those animals by these types, even though the actual unit types can be selected freely.  They behave as follows:&lt;br /&gt;
* Each deer wanders randomly on (selectable) terrain, except when enemies get in its (the deer's) maximum movement range, in which case it flees to the farthest point it can reach.&lt;br /&gt;
* Tuskers exhibit the same behavior as deer, except when an enemy is next to one of the tusklets.  This enemy is attacked unconditionally by the tuskers trying to protect their young.&lt;br /&gt;
* Tusklets blindly follow the closest adult tusker, except when there is no tusker left, in which case they behave the same as deer.&lt;br /&gt;
* Rabbits also wander randomly, but in addition disappear into their holes (if any are within reach) when enemies are close.  They reappear out of their holes at the beginning of the turn, if it is safe.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Forest Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Forest Animals Micro AI by using&lt;br /&gt;
 ai_type=forest_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Forest Animals AI consists of 4 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-3&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''rabbit_type, tusker_type, tusklet_type, deer_type''': Comma-separated lists of the unit types behaving as the respective animals.  If any of these parameters are not set, those types of animal behavior are skipped by the AI.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain on which the forest animals wander when they are not threatened by enemies.&lt;br /&gt;
* '''rabbit_number'''=6: (integer)  The number of rabbits for the AI to have on the map simultaneously.  The AI puts the missing number of rabbits onto the map at the beginning of the AI's turn (when the rabbits come out of their holes).&lt;br /&gt;
* '''rabbit_enemy_distance'''=3: (number)  Rabbits won't spawn in holes that aren't more than this distance away from all enemies.&lt;br /&gt;
* '''rabbit_hole_img''': Rabbit holes are marked by items on the map.  If this parameter is set, it must be the file name of the item image.  If it is not set, all items on the map are treated as rabbit holes.&lt;br /&gt;
** {{DevFeature1.14|10}} Items used for rabbit holes on the map border used to produce an error. Placing them on the map border therefore needed to be avoided on the scenario setup side. They are now automatically excluded from consideration by the AI code.&lt;br /&gt;
&lt;br /&gt;
Here's an example of forest_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=forest_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             deer_type=Vampire Bat&lt;br /&gt;
             rabbit_type=Giant Rat&lt;br /&gt;
             tusker_type=Ogre&lt;br /&gt;
             tusklet_type=Young Ogre&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=*^F*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Herd and Herders (ai_type=herding) ===&lt;br /&gt;
&lt;br /&gt;
The Herding Micro AI sets up an area on the map where a number of herder units (such as sheep dog) control and protect a herd (such as sheep).  For convenience, we simply refer to them as sheep dogs and sheep in the following, even though arbitrary unit types can be selected for either.&lt;br /&gt;
&lt;br /&gt;
Sheep dogs try to keep their sheep safe.  This involves keeping them inside the herding area, positioning themselves in between the sheep and approaching enemies, and attacking the enemies if those get too close.  If no active herding or protecting move is needed, the dogs go to a random location on the perimeter of the herding area.&lt;br /&gt;
&lt;br /&gt;
Sheep wander aimlessly except when a sheep dog is next to them, in which case they run away from the dog.  The dogs exploit this by positioning themselves on the outside of the sheep, if possible.  Sheep also run away from approaching enemies.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Herding AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Herding Micro AI by using&lt;br /&gt;
 ai_type=herding&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herder units (e.g. dogs).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herd units (e.g. sheep).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain forming the perimeter of the herding area.  In other words, this is the path the herders (dogs) use to patrol around the outside of the herding area.  This must be a set of coordinates ''completely enclosing'' the herding area.  Here's an example of setting up a rectangular perimeter:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         x=10-20,10-20,10,  ,20&lt;br /&gt;
         y=11,   21,   11-21,11-21&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
Another possibility is by defining the path by certain terrain on the map (as it is done in the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]), in which case the filter can simply look like this:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         terrain=Rb&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
* '''herd_x,herd_y''': The coordinate of one hex (''any'' one hex) inside the herding area.  This is needed to define which part of the map is on the ''inside'' of the herding area perimeter defined by [herding_perimeter], and which is the ''outside''. {{DevFeature1.15|0}} '''herd_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''attention_distance'''=8: (integer)  If an enemy is within this distance of a herd unit, but not as close as ''attack_distance'', the herders try to position themselves in between the enemy and the herd unit.&lt;br /&gt;
* '''attack_distance'''=4: (integer)  If an enemy is within this distance of a herd animal, the herders attack this enemy.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Herding AI consists of 6 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-5&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Herding [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=herding&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Footpad&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Troll Whelp&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             herd_x,herd_y=32,28&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=Rb&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Hunter (ai_type=hunter) ===&lt;br /&gt;
&lt;br /&gt;
The Hunter Micro AI leads a unit (or units) through hunt-and-rest cycles.  The units wander in a prescribed area of the map, their hunting ground, until an enemy unit comes within range.  Note that the units do not actively chase down enemies, but wander randomly through the hunting ground until an enemy is encountered.&lt;br /&gt;
&lt;br /&gt;
If enemies are within range of a hunter unit, the hunter attacks the weakest of them.  If a kill is made, it then retreats to its rest location, where it stays for a certain number of turns and until fully healed (whatever happens later).&lt;br /&gt;
&lt;br /&gt;
A few more details:&lt;br /&gt;
* If the hunter's way home is entirely blocked on the return (so that there is no possible path), the normal RCA AI takes over its behavior.&lt;br /&gt;
* However, if the way is blocked by an enemy unit occupying the rest hex, that enemy will be attacked unconditionally.&lt;br /&gt;
* A kill only makes the hunter go home when it is the attacker, not as defender.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Hunter AI behavior, check out the &amp;quot;Dragon&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]. &lt;br /&gt;
&lt;br /&gt;
Enable the Hunter Micro AI by using&lt;br /&gt;
 ai_type=hunter&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''home_x,home_y''': The coordinates of the hex to which the hunter returns and rests at after a kill. {{DevFeature1.15|0}} '''home_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the unit's hunting ground.  If this tag is not provided, the hunter uses all of the map.&lt;br /&gt;
* '''rest_turns'''=3: (integer)  The number of turns the AI stays at home_x,home_y after a kill.  It will not leave its home until this number of turns have passed ''and'' it is fully healed.&lt;br /&gt;
* '''show_messages'''=no: (boolean) If set to yes, the hunter announces whenever its behavior changes to the next phase.  Note that this is meant as debug output for the scenario author.  The messages are not translated and are not included in replays (and are only visible to the host in an MP game).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a hunter [micro_ai] tag usage from the &amp;quot;Dragon&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hunter&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Rowck&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=5-30,1-15&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             home_x,home_y=3,15&lt;br /&gt;
             rest_turns=2&lt;br /&gt;
 &lt;br /&gt;
             show_messages=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Swarm (ai_type=swarm) ===&lt;br /&gt;
&lt;br /&gt;
The Swarm Micro AI uses some very simply algorithms to simulate animal swarm behavior.  Without adjacent enemies, they simply do a random move, trying to stay together and at a certain distance from enemies.  However, if an enemy unit is close to any bat, the swarm scatters.  This is particular fun to watch when one places an enemy unit in the middle of the swarm.  After being scattered, the swarm members slowly rejoin, but not in a very organized way.  Sub-swarms or individual bats might roam around for quite some time before they find their way back.  It is also possible that individual bats (or small groups) split off from the larger swarm at times.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Swarm AI behavior, check out the &amp;quot;Swarm&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Swarm Micro AI by using&lt;br /&gt;
 ai_type=swarm&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Swarm AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_distance'''=5: (int) The minimum distance kept between units of the swarm and enemies when the swarm moves as a whole.&lt;br /&gt;
* '''scatter_distance'''=3: (int) Enemies within &amp;quot;scatter_distance&amp;quot; hexes of any swarm unit cause the swarm to scatter, by each unit trying to maximize its individual distance from the enemy.&lt;br /&gt;
* '''vision_distance'''=12: (int) Only units within this distance follow the overall swarm motion (either away from an enemy or of the swarm as a whole).  The smaller this value is set, the less likely the swarm is to stay together or rejoin.  This parameter is meant to simulate how far an individual member of the swarm can see, meaning that the swarm is &amp;quot;out of sight&amp;quot; (and the unit will not be able to follow it) if it is more than this distance away.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Swarm [micro_ai] tag usage from the &amp;quot;Swarm&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=swarm&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Assassin Squad Micro AI (ai_type=assassin) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|6}}&lt;br /&gt;
&lt;br /&gt;
This Micro AI controls one or several assassin units which try to circle around enemy units with the goal of hitting a target unit without being distracted or impeded by other enemies.  The units will always attack the target if such attacks are possible, otherwise they will follow the path of least resistance toward it. Notes:&lt;br /&gt;
&lt;br /&gt;
* The main priority for the AI is to find paths that avoid enemy units. However, if no such path can be found, the assassins will still advance toward the target, taking the path exposing the units to the least potential damage from enemies.&lt;br /&gt;
&lt;br /&gt;
* The units will not necessarily follow the same path. The best path is chosen individually for each unit.&lt;br /&gt;
&lt;br /&gt;
* While it is not a requirement for the AI to work, using ambusher or skirmisher units, or units with the quick trait, makes this AI more lethal in many cases.&lt;br /&gt;
&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded, it does not work otherwise. That means that '''ca_score''' is not a valid key for the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=assassin&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Assassin Squad specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the assassin units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the target unit.&lt;br /&gt;
** It is important to realize that the AI currently only works correctly with a single target unit. It is thus the responsibility of the scenario designer to make sure that this filter matches an individual enemy unit. If several enemy units are to be targeted in the same scenario, several instances of the Assassin Micro AI can be used.&lt;br /&gt;
** The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[prefer]''': A [[StandardLocationFilter|Standard Location Filter]] selecting locations on the map for the assassins to prefer when making their way toward the target unit. This could be used, for example, to make units with the ambush ability move (mostly) through forest or to have the assassins circle around the, say, northern part of the map rather than the south if that is desirable for scenario design purposes.&lt;br /&gt;
** As with avoiding enemies, the units will still move across non-preferred terrain if that is either the only option or ''much'' shorter than preferred terrain&lt;br /&gt;
** Attacking, once the target unit is in range, is not influenced by the [prefer] SLF.&lt;br /&gt;
&lt;br /&gt;
Here is an example of an Assassin Squad [micro_ai] tag usage:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=assassin&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Assassin,Master at Arms,Duelist&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             [prefer] # Circle around northern part of map&lt;br /&gt;
                 x=1-9, 10-99&lt;br /&gt;
                 y=1-37, 1-17&lt;br /&gt;
             [/prefer]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Bottleneck Defense Micro AI (ai_type=bottleneck_defense) ==&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense Micro AI lets you define a location on the map where the AI can take a defensive stance behind a narrow passage (bottleneck).  Units on the front line are backed up by healers and/or units with the leadership ability.  Injured units are moved to the back and replaced by uninjured (or less injured) units.  The units on the front line only attack when it is safe (no retaliation) or when there is a high chance that they will make a kill or level up.  Using this Micro AI only makes sense if there is no way for the enemy sides to move around the bottleneck and attack the AI's units from behind.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Bottleneck&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Bottleneck Defense Micro AI by using&lt;br /&gt;
 ai_type=bottleneck_defense&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Bottleneck Defense specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense AI requires two sets of coordinates that define where it should be taking up its defensive stance, and from which side the enemy attacks at that location.&lt;br /&gt;
&lt;br /&gt;
* '''x,y''': Comma separated lists of the hexes on the front line, where strong units are placed.  All hexes on which the AI makes contact with (can be attacked by) the enemy need to be included here.  Units are placed on them in the order in which they are listed here. {{DevFeature1.15|0}} '''location_id''' can be used instead with named/special locations.&lt;br /&gt;
* '''enemy_x,enemy_y''': Comma separated list of the hexes from which the enemy can attack the AI front line units.  This is needed for the AI to know on which side of the front line support units (healers etc.) need to be placed.  In many cases, it is sufficient to provide only one of the enemy front line hexes, but there are geometries for which that does not work.  It is therefore safer to list all enemy front line hexes here. {{DevFeature1.15|0}} '''enemy_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
By default, the AI places healers and units with the leadership ability both on the front line itself (if they are the strongest units available) and on hexes adjacent to and behind the line.  If different placement is desired, these locations can be overridden with the following keys.&lt;br /&gt;
&lt;br /&gt;
* '''active_side_leader'''=no: (boolean)  If set to 'yes', the side leader participates in the bottleneck defense action after the side's gold has been spent.  If set to 'no' (default), the leader follows default AI behavior (sitting on the keep and recruiting, for the most part).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Bottleneck Defense AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.15|3}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the units.  Only AI units matching this filter participate in the bottleneck defense.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''healer_x,healer_y''': Comma separated list of hexes where healers are placed.  This can be used to keep healers from the front line (or to take up only certain positions on the front line), and/or to override the default healing positions behind the line.  Healers are placed on them in the order in which the hexes are listed, with the exception that hexes on the line always take priority over hexes behind the line. {{DevFeature1.15|0}} '''healer_loc''' can be used instead with named/special locations.&lt;br /&gt;
* '''leadership_x,leadership_y''': Same as ''healer_x,healer_y'', but for units with the leadership ability. {{DevFeature1.15|0}} '''leadership_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
As an example, in the &amp;quot;Bottleneck&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]], there are three front line hexes, two of which are on hill terrain, while the third is a road with a lower defense rating.  The healer and side leader are supposed to participate in combat because they are strong units, but only from the hill hexes to keep them safer.  This is accomplished by using the following keys settings (check out the scenario to see which hex is which):&lt;br /&gt;
 [micro_ai]&lt;br /&gt;
     side=1&lt;br /&gt;
     ai_type=bottleneck_defense&lt;br /&gt;
     action=add&lt;br /&gt;
 &lt;br /&gt;
     x=14,14,14&lt;br /&gt;
     y= 7, 9, 8&lt;br /&gt;
     enemy_x=13,13&lt;br /&gt;
     enemy_y= 8, 9&lt;br /&gt;
 &lt;br /&gt;
     healer_x=14,14,15,15&lt;br /&gt;
     healer_y= 7, 9, 8, 9&lt;br /&gt;
     leadership_x=14,14,15,15&lt;br /&gt;
     leadership_y= 7, 9, 9 ,8&lt;br /&gt;
     active_side_leader=yes&lt;br /&gt;
 [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Fast Micro AI (ai_type=fast_ai) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|0}}&lt;br /&gt;
&lt;br /&gt;
This AI is meant as a fall-back AI for scenarios where there are so many units that attacks and moves done by the default AI are slow with long delays before each move.  It replaces the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions (CAs).  It should be obvious that in order to achieve the faster speed, compromises concerning the quality of the moves have to be made.&lt;br /&gt;
&lt;br /&gt;
Enable the Fast Micro AI by using&lt;br /&gt;
 ai_type=fast_ai&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Combat candidate action:'''&lt;br /&gt;
* Attackers are considered individually one at a time.  The best attack found for a unit is executed without checking whether a better attack by another unit might be possible.&lt;br /&gt;
* The default order in which attackers are considered is by decreasing hitpoints, starting with the strongest unit (in terms of HP).  This can be modified with the optional keys below.&lt;br /&gt;
* By default, if weaker units block attack hexes for a stronger unit, they do not move out of the way (this is the same as for the default AI).  If a hex becomes available after an attack (e.g. due to a weaker unit dying in the attack), all attacks that were possible ''before'' this happened are finished first.  Only after that are attacks by the stronger unit reconsidered.&lt;br /&gt;
* If include_occupied_attack_hexes=yes is set, weaker units move out of the way for stronger units if possible.  This can, however slow down the AI depending on the situation on the map.  Use with caution.&lt;br /&gt;
* {{DevFeature1.13|2}} Previous to this version, attacks by the leader were bunched in with the attacks by the other units.  Now leader attacks are treated separately and contain an extra layer of analysis to determine whether the attack position is &amp;quot;safe&amp;quot; (in an approximate way).  Currently, this is done with two simple considerations:&lt;br /&gt;
** Count all enemy units that can reach the leader's attack position.  If there are more than given by optional parameter 'leader_attack_max_units' (default value 3), do not attack.&lt;br /&gt;
** Add up the hitpoints of all enemies that can reach the leader's attack position.  If this value, multiplied by optional parameter 'leader_weight'(default value 2), is larger than the leaders hitpoints, do not attack.&lt;br /&gt;
** Two optional parameters, 'threatened_leader_fights' and 'leader_additional_threat', can also be used to let the leader attack when the threat at the attack hex is comparable to that at the leader's current position.&lt;br /&gt;
** In order to keep this AI as fast as possible, leader attacks are done by a separate candidate action that is only evaluated when all other CAs are done.&lt;br /&gt;
&lt;br /&gt;
'''Move-to-targets, villages and retreat candidate actions:'''&lt;br /&gt;
* The move-to-targets, villages and retreat candidate actions of the default AI are removed.  Move-to-targets and villages are taken over by the Fast Micro AI, whereas retreat is simply not done.&lt;br /&gt;
* This CA moves units toward goals after attacks are done.  Goals are unowned (if the AI side has a leader only) and enemy-owned villages and enemy leaders.&lt;br /&gt;
* For each goal, units are pre-sorted in order of the fewest moves needed to cover the ''distance'' toward the goal (that is, as if move cost on each hex toward the goal were 1).  Units are then considered sequentially and if a unit can get there &amp;quot;efficiently&amp;quot;, its move is executed without considering other units.&lt;br /&gt;
** A move is considered efficient if the move cost to get to the hex is not more than 'move_cost_factor' larger than the distance to the hex.  'move_cost_factor' is an optional key that defaults to 2.  It can be set to values of 1.1 or larger (see below).&lt;br /&gt;
** If the move for the current unit is not found to be efficient, the AI considers the next unit instead.  If no unit can do an efficient move toward the goal, the best (fastest) of the inefficient moves is selected instead.&lt;br /&gt;
&lt;br /&gt;
* Village goals:&lt;br /&gt;
** Village goals are considered before enemy leader goals so that the AI preferentially chooses scouts (or otherwise fast units) for them.&lt;br /&gt;
** The number of units sent toward villages depends on the value of the default AI aspect village_value.  It is set to village_value/4 times the number of units the AI has left to move.  Thus, by default (village_value=1.0) the AI sends a quarter of its units toward villages, and the rest toward enemy leaders.  This means that the meaningful range of values for village_value is 0..4.&lt;br /&gt;
** Only one unit is moved toward each village.  This consideration also overrides the number of units to move toward villages defined by village_value, topping it at the number of unowned/enemy-owned villages on the map.&lt;br /&gt;
** Setting village_value=0 disables moving units toward villages altogether.  That is, not only are no units chosen, but the village targeting phase of the AI is entirely disabled with no evaluation time used for it.  Note that, unlike with the default AI, this also means that no villages within range of an AI unit are grabbed.&lt;br /&gt;
** The order in which villages are chosen as goals is some combination of them being close to the AI side's leader and being spread out over the map.&lt;br /&gt;
&lt;br /&gt;
* Enemy leader goals:&lt;br /&gt;
** The order of enemy leaders goals is set according to their distance from the AI leader with the closest enemy leader being targeted first (if the AI has a leader, otherwise their order is random).  One unit is sent toward each enemy leader in this order, after which the AI starts with the first enemy leader again.  This is repeated until all units have moved.&lt;br /&gt;
&lt;br /&gt;
* Guardians (units with ai_special=guardian set) are ignored by the fast move-to-targets CA.&lt;br /&gt;
&lt;br /&gt;
'''Tips for speeding up/modifying the Fast Micro AI in specific scenarios:'''&lt;br /&gt;
&lt;br /&gt;
A number of optional keys and default AI aspects can be used to modify the behavior of the Fast Micro AI.  The MAI is set up so that it is a reasonable compromise between speed and quality of the moves in most situations.  However, this might not be ideal for all scenarios here are some things that can be done to speed up the AI (or to get better behavior in some situations by doing the opposite):&lt;br /&gt;
* Use large values for the default AI aspect aggression.  The largest delays before attacks happen (in some circumstances) when the Fast MAI has to consider lots of units with attacks left but decides not to attack with them.  See below for a description of how aggression is used by the fast AI.&lt;br /&gt;
* Use large values of move_cost_factor (see above for description of what this does).  If you want to emphasize units that can move across terrain easily (e.g. bats) to be chosen for village grabbing, you might want to use low values of move_cost_factor, but this might slow down the AI.  The default value should work reasonably well in most standard situations, but modifying the value might work better on specific maps.&lt;br /&gt;
* Don't use dungeon_mode=yes unless absolutely necessary, even in most dungeon crawls.  Test the AI without this setting first and only use it if it is the only way to solve the problem.  Often, a minor tweak to the map will have the same effect, without the speed penalty.&lt;br /&gt;
* Use include_occupied_attack_hexes=yes with caution.  It might (or might not) slow down the attack CA significantly, depending on the scenario.&lt;br /&gt;
* Set village_value=0, which makes the AI not consider villages as goals.  This has not been found to make a large difference on the maps tested so far, but it might have an effect on maps with a very large number of villages.&lt;br /&gt;
* Use attack_hidden_enemies=yes on maps where there are no hidden enemies. Excluding hidden enemies from the attack calculation requires an additional filter to be evaluated that takes extra time. In most cases this will not result in a large computation time saving.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_hidden_enemies'''=no: (boolean)  By default, hidden enemies are excluded from the attack calculation.  Setting this to 'yes' makes the AI also consider those enemies as attack targets.&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.13|2}} A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  As with the default AI, units will not step on hexes defined by the [avoid] tag.  In addition, no goals will be chosen that lie on those hexes.  The one minor exception is that a unit stepping out of the way for another unit might temporarily enter the avoided zone.&lt;br /&gt;
* '''dungeon_mode'''=no: (boolean)  As much as possible, the Fast Micro AI works with distances between hexes rather than path finding.  This works pretty well on open maps and even most dungeon crawls (and similar scenarios), but it might result in some units getting temporarily (and in rare cases permanently) stuck on maps with convoluted labyrinths.  If that is the case, setting dungeon_mode=yes triggers more path finding that avoids such situations (but as a result is somewhat slower).&lt;br /&gt;
* '''include_occupied_attack_hexes'''=no: (boolean) If set to 'yes', the AI moves weaker units out of the way for a stronger attacker.  This might lead to better attacks but is slower, so don't use if speed is essential.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the attackers.  Only AI units matching this filter will attack, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes the side= key set to the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the enemies.  Only enemy units matching this filter will be attacked, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''leader_weight'''=2: (number &amp;gt;= 0) {{DevFeature1.13|2}} A factor specifying how much more valuable the AI considers side leaders than non-leader units in the attack evaluation. This is also used to determine whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''leader_additional_threat'''=1: (number &amp;gt; 0) {{DevFeature1.13|2}} If threatened_leader_fights=yes, the threat at the attack position may be larger by this factor for the leader to leave the keep for an attack.&lt;br /&gt;
* '''leader_attack_max_units'''=3: (number &amp;gt;= 0) {{DevFeature1.13|2}} A number specifying how many enemy units within reach are acceptable when determining whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''move_cost_factor'''=2: (number &amp;gt;= 1.1)  The factor by which the move cost to a goal hex may be larger than the distance to the goal hex for a move to be considered &amp;quot;efficient&amp;quot;.  See description above.  1.1 is used if this is set to values smaller than 1.1.  Smaller values might lead to better behavior, while larger values might speed up the AI.&lt;br /&gt;
* '''weak_units_first'''=no: (boolean) If set to 'yes', inverts the order in which units are considered by the combat CA.  Attacks are then done in order of increasing hitpoints of the attackers.&lt;br /&gt;
* '''skip_combat_ca'''=no: (boolean) If set to 'yes', the fast combat CA is not used.  The default AI's high_xp_attack and combat CAs remains in place in this case.&lt;br /&gt;
* '''skip_move_ca'''=no: (boolean) If set to 'yes', the fast move-to-targets CA is not used.  The default AI's retreat, villages and move-to-targets CAs remains in place in this case.&lt;br /&gt;
* '''threatened_leader_fights'''=no: (boolean) {{DevFeature1.13|2}} By default, the AI leader will not attack if the threat at the attack position is too high.  If this key is set to 'yes', the leader will attack if the threat there is not larger than the threat at his current position multiplied by the value of 'leader_additional_threat'. This is meant as an option for the endgame: when the AI leader is under attack, he might as well go down fighting.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''aggression''': The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to set how aggressive the AI should attack.  It works in roughly the same way as the default AI aspect:&lt;br /&gt;
** The value of (1 - aggression) determines how much the AI values its own units compared to the enemy's units.  Thus, aggression=0 means that attacks are only done if the AI expects to deal (roughly) the same amount of damage as it will receive.  aggression=1 means that expected damage to the AI's units is not considered, only the expected damage to enemy units matters.&lt;br /&gt;
** Damage is calculated as a combination of hitpoints and chance to die, in &amp;quot;gold equivalent units&amp;quot;.&lt;br /&gt;
** Unlike the default AI, aggression is set to 1 for values larger than 1.  Allowed values are thus anything from -infinity to +infinity, although only -infinity to +1.0 make sense.&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.13|2}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
*'''attacks aspect''':  {{DevFeature1.13|2}} The standard RCA AI [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] used to filter own and enemy units for attacks is now also taken into account as long as neither [filter] nor [filter_second] (see above under 'optional keys') are defined in the [micro_ai] tag.&lt;br /&gt;
*'''village_value=1''': (number ranging from 0 to 4) The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to define how much villages should be targeted by the move-to-targets CA.  In the Fast MAI, this defines which fraction of the AI units are moved toward village goals (see above for details).  If set to 0, villages are ignored as goals.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded to the same values as those of the respective [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|default candidate actions]].  ca_score is therefore not a valid optional key.&lt;br /&gt;
* The fast Micro AI modifies (removes) the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions.&lt;br /&gt;
&lt;br /&gt;
Enabling the Fast Micro AI in its default configuration is as simple as this usage example from the &amp;quot;Fast AI&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=fast_ai&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Generalized Goto Micro AI (ai_type=goto) ==&lt;br /&gt;
&lt;br /&gt;
The Goto Micro AI provides a highly configurable method of sending a unit (or units) to a location or set of locations.  The units to be moved are defined using a [[StandardUnitFilter|Standard Unit Filter]] (SUF) [alternatively, all side units are used if the SUF is not provided), while the goto locations are given in a [[StandardLocationFilter|Standard Location Filter]] (SLF).&lt;br /&gt;
&lt;br /&gt;
By default, each unit matching the SUF will move toward the closest goal (in terms of turns needed to get there) matching the SLF via the fastest route given the current situation on the map (locations of enemy units etc.), but this behavior can be influenced by the optional keys listed below.  Note that this means that a unit's goal might change from one turn to the next depending on the situation on the map.&lt;br /&gt;
&lt;br /&gt;
The behavior of the unit(s) once they reach their goal location(s) is configurable as well.  By default, they will stay there until the Goto Micro AI is deleted or changed.&lt;br /&gt;
&lt;br /&gt;
For demonstrations of several Goto Micro AI usages, check out the &amp;quot;Goto&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Goto Micro AI by using&lt;br /&gt;
 ai_type=goto&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Goto specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the possible goal locations for the move.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists). Note that the area defined by [avoid] is not entered at all, even if the end point of the move would lie outside the area again.&lt;br /&gt;
* '''avoid_enemies''': (number&amp;gt;0) By default, units controlled by the Goto Micro AI will not take enemy units into account for route finding, other than the enemies' ZoCs.  By contrast, if this key is set, they will try to avoid traversing hexes on which they can be attacked by enemies.  The larger the value of ''avoid_enemies'', the more the units will try to stay out of the enemies' way.  Notes:&lt;br /&gt;
** Units are willing to take a path that is 'avoid_enemies' movement points longer for each enemy unit that can be avoided by doing so for each hex along the path.  As an example, if avoid_enemies=1 and the shortest path crosses 2 hexes that can be reached by 3 enemies each, the unit is willing to take a path that is up to 6 hexes longer instead.  If avoid_enemies=10 in the same situation, the unit will take a path up to 60 hexes longer.&lt;br /&gt;
** Hexes adjacent to enemy units are always strongly avoided, independent of the value of avoid_enemies.&lt;br /&gt;
** avoid_enemies is ignored if 'use_straight_line=true' is set.&lt;br /&gt;
** Due to an issue with the path finding code, '''tunnels and teleports are ignored in Wesnoth 1.14 and before''' if 'avoid_enemies' is used. This has been fixed in Wesnoth 1.15.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units to be moved.  Defaults to all units of the side if not defined.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''ignore_enemy_at_goal'''=no and '''ignore_units'''=no: (boolean)  In the default configuration, the Goto MAI does not move units if the path to all goal hexes is blocked by enemies.  It is therefore possible to disable the Goto MAI by putting enemy units on all the goal hexes.  That can be changed if either of these two keys is set to 'yes'.&lt;br /&gt;
** 'ignore_enemy_at_goal=yes' makes the AI choose its goals while disregarding any enemy units at the goal hexes.  Even so, a stiff rating penalty is applied to goal hexes occupied by enemies, thus that unoccupied goal hexes are chosen unless all possible goals are occupied by enemies.  Note that this setting ''only'' ignores enemy units at the goal hexes themselves.  It is still possible to disable the AI by putting enemies all around the goals.&lt;br /&gt;
** 'ignore_units=yes' let's the AI ignore ''all'' enemy units when deciding which goal hex(es) to move toward.&lt;br /&gt;
** In both cases, enemies are only ignored for deciding which unit to move toward which goal hex.  For the actual move itself, all enemies and their ZoCs are considered as usual.&lt;br /&gt;
** Both options only take effect if neither avoid_enemies is used, nor use_straight_line is set to 'yes', as neither of these consider enemies as blockers. Or in other words, if avoid_enemies is used, or use_straight_line=yes, it is unnecessary to set ignore_enemy_at_goal or ignore_units to yes.&lt;br /&gt;
* '''release_unit_at_goal'''=no: (boolean) By default, a unit that reaches a goal location will stay there until the Goto Micro AI is deleted or changed (or until the conditions specified in the [goto_goals] SLF change).  If this parameter is set, a unit that has reached its goal will not be controlled by this Micro AI any more starting from the next turn.&lt;br /&gt;
* '''release_all_units_at_goal'''=no: (boolean) Same as ''release_unit_at_goal'', but this will deactivate the Goto Micro AI for all units once the first unit reaches a goal.&lt;br /&gt;
* '''remove_movement'''=yes: (boolean):  {{DevFeature1.15|11}}  By default, when the Goto MAI cannot move a unit (for example, because the path is blocked by enemies), its movement points are still removed. If this option is set to 'no', this step does not happen, so that other AI candidate actions (or even another Goto MAI with different parameters) can take over instead.&lt;br /&gt;
* '''unique_goals'''=no: (boolean)  If set to 'yes', each unit will be sent to a different goal location.  Note that the best goals are recalculated each turn, so a unit might choose a different goal this turn from what it was heading for during the previous turn if the situation on the map has changed.  Also note that if there are more units than goal locations, the left over units will not be handled by the Goto MAI but follow default behavior instead.&lt;br /&gt;
* '''use_straight_line'''=no: (boolean)  By default, units choose the route by which they will get to their goal locations in the smallest number of turns.  If this parameter is set to 'yes', a straight line route (or something pretty close to it at least) will be chosen instead, irrespective of whether this leads into a dead end, whether it is blocked by enemies, etc.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* Depending on the parameters used, the Goto Micro AI might require a lot of path finding to be done.  Thus, while it is possible to set up this AI to move many units to many different locations, a usage like that might require quite a long calculation time.&lt;br /&gt;
&lt;br /&gt;
===Goto Micro AI example usages:===&lt;br /&gt;
&lt;br /&gt;
Here are several examples of goto [micro_ai] tag usages from the &amp;quot;Goto&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
* Send a unit toward a castle (similar to the RCA AI's Goto Candidate Action, but avoiding dead ends and areas ZoCed by enemies)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
             ca_id=messenger1&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=messenger1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=6-7,2-4&lt;br /&gt;
                 terrain=C*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             ca_score=210001&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send all units of the side toward the southern border of the map&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=8&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                  y=33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             avoid_enemies=1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send units to the four corners of the map (with each unit going to a different corner).&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x=1,1,44,44&lt;br /&gt;
                 y=1,33,1,33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             unique_goals=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Set up a guardian unit that doesn't move while no enemy unit is within 8 hexes (ironically, the Goto Micro AI can be used to keep units in place, although using one of the Guardian Micro AIs might often be a better idea)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=guard1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 [filter]&lt;br /&gt;
                     id=guard1&lt;br /&gt;
                 [/filter]&lt;br /&gt;
                 [not]&lt;br /&gt;
                     [filter]&lt;br /&gt;
                         side=1&lt;br /&gt;
                     [/filter]&lt;br /&gt;
                     radius=8&lt;br /&gt;
                 [/not]&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Guardian Micro AIs ==&lt;br /&gt;
=== General Comments on the Guardian Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Guardian Micro AIs can be used to set up different kind of guardian behaviors for a unit or a set of units.  Four different types of guardians are available and are described below.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different guardian behaviors, check out the &amp;quot;Guardians&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
=== Return Guardian  (ai_type=return_guardian)===&lt;br /&gt;
&lt;br /&gt;
A 'return guardian' is a variation of the standard Wesnoth guardian. It has an assigned guard position (GP) to which it returns after attacks on approaching enemies:&lt;br /&gt;
* If at GP with no enemy in reach, do nothing.&lt;br /&gt;
* If at GP with an enemy in reach, attack. Note that the attack is done by the default AI, which might result in the guardian not attacking if the enemy is deemed too strong (this can be influenced by setting the default ''aggression'' value to a different value).&lt;br /&gt;
* If not at GP, return there, no matter whether an enemy is in reach or not.&lt;br /&gt;
* If enemies are blocking the way back to GP, do your best to move around them.&lt;br /&gt;
* If the guardian ends up next to an enemy on the way back, attack that enemy after the move.&lt;br /&gt;
&lt;br /&gt;
Enable the Return Guardian Micro AI by using&lt;br /&gt;
 ai_type=return_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''return_x,return_y''': The coordinate of the hex the unit returns to after each attack.  {{DevFeature1.15|0}} '''return_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=100010: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  This AI consist of 2 CAs, one with score &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt; the other with &amp;lt;tt&amp;gt;ca_score-20&amp;lt;/tt&amp;gt;.  The default value is set so that the first CA gets executed right before the default combat CA, and the other right after.  It usually will not make sense to change this value, but it is possible to do so for custom applications.  {{DevFeature1.13|6}}: This is now changed to 100100 and 99900 in order to avoid conflicts with the new high_XP_attacks candidate action.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a return guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=return_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=return1&lt;br /&gt;
             return_x,return_y=20,2&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
===Stationed Guardian (ai_type=stationed_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A 'stationed guardian' is another variation of the standard Wesnoth guardian with a somewhat more complex behavior than that of the 'return guardian'. Two positions are defined for it, a 'station' and a 'guarded location', as well as a distance. The behavior is as follows:&lt;br /&gt;
* If no enemy is within 'distance' of the guard's current position, do nothing.  This is independent of the guardian being at its station or not.&lt;br /&gt;
* Otherwise: If an enemy is within 'distance' of the guard, but not also within the same distance of the guarded location and the station (all of this simultaneously), move the guard in the direction of the station (or keep the guard in place if the station is unreachable).&lt;br /&gt;
* Otherwise:&lt;br /&gt;
** Pick the enemy unit that is closest to the guarded location.&lt;br /&gt;
** If we can reach it, pick the adjacent hex with the highest defense rating and attack from there.&lt;br /&gt;
** If not in reach, move toward this unit.&lt;br /&gt;
&lt;br /&gt;
Enable the Stationed Guardian Micro AI by using&lt;br /&gt;
 ai_type=stationed_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance parameter as explained above&lt;br /&gt;
* '''station_x,station_y''': The x and y position of the hex that serves as the guardians station. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''guard_x, guard_y''': The x and y position of the guarded location.  {{DevFeature1.13|0}}  These are now optional keys, defaulting to station_x,station_y if not provided. {{DevFeature1.15|0}} '''guard_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a stationed guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=stationed_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=stationed1&lt;br /&gt;
             distance=4&lt;br /&gt;
             station_x,station_y=2,14&lt;br /&gt;
             guard_x,guard_y=3,13&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Coward (ai_type=coward) ===&lt;br /&gt;
&lt;br /&gt;
Cowards are units that, like guardians, sit around doing nothing until an enemy comes into range.  Unlike guardians, however, they run away once enemies approach. Applications might be wild animals, unarmed civilians getting in the way of a battle, etc.  The coward macro can be called with two optional locations, 'seek' and 'avoid':&lt;br /&gt;
* If neither is given, the coward retreats to the position farthest away from the approaching enemies.&lt;br /&gt;
* If 'seek' is given, it preferentially goes toward that location (but getting away from enemies takes priority).&lt;br /&gt;
* If 'avoid' is given, it in addition tries to avoid that location (with both maximizing distance from enemies and going toward 'seek' taking priority).&lt;br /&gt;
* Both 'seek' and 'avoid' may consist of only one coordinate ('x' or 'y'), in which case not a single hex, but a line of hexes is sought or avoided.&lt;br /&gt;
&lt;br /&gt;
Enable the Coward Micro AI by using&lt;br /&gt;
 ai_type=coward&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance within which an enemy must be to &amp;quot;scare&amp;quot; the unit&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_if_trapped'''='no':  {{DevFeature1.13|0}}  With the default settings, a coward never attacks.  If this key is set to 'yes', the coward attacks the weakest (lowest hitpoints) enemy next to which it ends up after moving (such as a cornered animal would do, blindly lashing out without regards for its own safety).&lt;br /&gt;
* '''avoid_x, avoid_y''': The x and y coordinate of the hex to avoid. {{DevFeature1.15|0}} '''avoid_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units from which the cowards flee.  If not set, this defaults to all enemy units.  Note, however, that no &amp;quot;enemy of AI side&amp;quot; condition is added if [filter_second] is specified, meaning that it can include allied and/or enemy units as desired (and even units on the same side as the coward).&lt;br /&gt;
* '''seek_x, seek_y''': The x and y coordinate of the hex to seek. {{DevFeature1.15|0}} '''seek_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a coward [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=coward&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=coward3&lt;br /&gt;
             distance=5&lt;br /&gt;
             seek_x,seek_y=24,5&lt;br /&gt;
             avoid_x,avoid_y=24,15&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Zone guardian (ai_type=zone_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A zone guardian is a unit that, as the name says, guards a zone. It moves randomly inside this zone until an enemy enters it (or a separately defined enemy zone, see below). Applications might be the defense of a castle, a nesting area or for protecting an unit.  The zone macro can be called with an optional SLF, [filter_location_enemy]:&lt;br /&gt;
* If not specified, the zone guard attacks any enemy coming inside its guard zone.&lt;br /&gt;
* If [filter_location_enemy] is given, it attacks any enemy entering this zone and once there are no more enemies, it goes back to patrol in its basic zone.&lt;br /&gt;
&lt;br /&gt;
Enable the Zone Guardian Micro AI by using&lt;br /&gt;
 ai_type=zone_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone to guard.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_enemy]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone where enemies have to be attacked.  If not given, it defaults to [filter_location].&lt;br /&gt;
* '''station_x,station_y''': By default, the zone guardian moves randomly within the zone if there is no enemy to attack.  However, if station_x/y is given, it moves to this position instead during turns when it does not have an enemy to attack.  This position does not need to be inside the zone defined above.  Note: obviously it does not make sense to define [filter_location], [filter_location_enemy] and station_x/y all at the same time.  Only [filter_location] and either one of the latter two (or none of them) should be provided for any zone guardian. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a zone guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=zone_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=zone3&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=22-31,4-11  # This is intentionally chosen to extend past the lake&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             station_x,station_y=32,8&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Hang Out Micro AI (ai_type=hang_out) ==&lt;br /&gt;
&lt;br /&gt;
The Hang Out Micro AI instructs units of the AI side to &amp;quot;hang out&amp;quot; around a certain location, or locations, until a condition for mobilizing the side is met.  If none of the optional keys are given, the AI controls all units of the side to hang around the leader while moving off castle tiles to make room for recruiting.&lt;br /&gt;
&lt;br /&gt;
The AI works by moving each unit to its best hex first using the minimum amount of movement points required for this move.  Once all AI units are in position, movement points are set to zero for all of them simultaneously such that they cannot be used by other AI candidate actions.  This is done because the mobilizing condition might be met as result of one of the moves of the Hang Out AI, after which we want units to be able to continue with other AI actions.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Hang Out and Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Hang Out Micro AI by using&lt;br /&gt;
 ai_type=hang_out&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Hang Out specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  In its default configuration, the Hang Out MAI controls all units of a side, keeping them as close to the side leader(s) as possible while moving off castle tiles to make room for recruiting.  For this purpose, the candidate action score defaults to 170,000, which is just below the score of the default recruiting CA.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''': A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  If neither of these two exists, this parameter is set to default to all castle terrain (&amp;lt;tt&amp;gt;terrain=C*,C*^*,*^C*&amp;lt;/tt&amp;gt;) so that units move off the castle to make room for recruiting.  Note that this does not include keep terrain, otherwise the leader would be moved off the keep as well.&lt;br /&gt;
* '''ca_score'''=170000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 170,000, which means that it is executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Hang Out action (&amp;lt;tt&amp;gt;side=&amp;lt;/tt&amp;gt; is always set and does not need to be provided).  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the locations ''near'' which the AI units should hang out.  This does not mean that the units only move onto terrain specified here, but that they minimize the distance to such terrain.  It defaults to the position of the side leader(s).&lt;br /&gt;
* '''[mobilize_condition]''':  Contains ([[ConditionalActionsWML#Condition_Tags|WML Condition Tags]])  Once the condition set up here has been met, the Hang Out MAI is disabled and the side goes over to other AI activity.  The Hang Out MAI remains disabled after this, even if the condition is not met any more later in the scenario.&lt;br /&gt;
* '''mobilize_on_gold_less_than''': (number) Since side gold cannot be checked in a WML condition tag without storing it in a variable first, this key is provided separately.  It disables the Hang Out Micro AI once the side gold goes below the value specified here for the first time.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
* The Hang Out AI still takes effect if no locations to hang out at are found or if all map locations are to be avoided.  In that case, it simply keeps all units at their current locations.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Hang Out Micro AI configuration from the &amp;quot;Hang Out and Messenger&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hang_out&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [avoid]&lt;br /&gt;
                 terrain=C*,H*,M*,A*,S*,*^F*&lt;br /&gt;
             [/avoid]&lt;br /&gt;
             [mobilize_condition]&lt;br /&gt;
                 [have_unit]&lt;br /&gt;
                     side=1&lt;br /&gt;
                     count=7-99&lt;br /&gt;
                 [/have_unit]&lt;br /&gt;
             [/mobilize_condition]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Healer Support Micro AI (ai_type=healer_support) ==&lt;br /&gt;
&lt;br /&gt;
The Healer Support Micro AI configures the healers of a side to stay behind the battle lines and heal injured and/or threatened units rather than participate in combat under all circumstances.  You can set different levels of aggressiveness for the healers.  &lt;br /&gt;
&lt;br /&gt;
'''Notes'''&lt;br /&gt;
* By default, ''all'' healers of the AI side are controlled by this AI.  This includes in particular also the side leader, if the leader is a healer.  If you do not want the leader to be included (and possibly be moved away from the keep etc.) use the [filter] tag with canrecruit=no (see below).&lt;br /&gt;
* It is not always better to use this Micro AI than the default AI, as it takes healers (which can be strong units) away from the pool of units to choose for attacks.&lt;br /&gt;
* {{DevFeature1.15|3}} This Micro AI now takes unit guardian status and the passive_leader aspect into account.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Healers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Healer Support Micro AI by using&lt;br /&gt;
 ai_type=healer_support&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Healer Support specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the healer units.  If set, only units on the AI side passing this filter ''and'' having the healing ability are considered by this Micro AI.&lt;br /&gt;
*'''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting units to receive healing. If set, only units on the AI side passing this filter ''and'' the other healee selection criteria will be selected for healing by this Micro AI.&lt;br /&gt;
* '''aggression'''=1.0: (float) Sets the aggressiveness of the AI.  This parameter is set up as a float to accommodate future functionality, but it currently acts as a boolean: if set to zero, the AI will never let its healers participate in combat, for all other values it allows them to attack after all other units have attacked and if the healers cannot find any more units to support.  The former behavior might be appropriate in scenarios with only one or few valuable healers, while the latter might work better in scenarios with many healers.&lt;br /&gt;
* '''injured_units_only'''=no: (boolean) If set to 'yes', the AI will only move healers next to units that are already injured and skip units that currently have full hitpoints, but might get injured during the next enemy turn.&lt;br /&gt;
* '''max_threats'''=9999: (integer) The maximum allowable number of enemies that can attack a hex in order for it to be considered for a healer.  As an example, setting 'max_threats=0' means that the AI only moves healers to locations that are entirely safe from the enemy (assuming that none of the units currently on the map dies).  Note that the value of this key is checked against the number of enemies that can make it to the hex, not the number of adjacent hexes from which the healer could be attacked.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI. Note: as attacks by the healers are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The Healer Support Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded.  It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Healer Support Micro AI configuration from the &amp;quot;Healers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             aggression=0&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Lurkers Micro AI (ai_type=lurkers) ==&lt;br /&gt;
&lt;br /&gt;
The Lurker Micro AI controls some or all units of a side in &amp;quot;lurker mode&amp;quot;.  A lurker is a unit that is capable of moving across most terrains, but that only stops on and attacks from specific terrain.  It might also have the ability to hide on this terrain (which is the reason why it is called a lurker).&lt;br /&gt;
&lt;br /&gt;
Lurkers move individually without strategy and always attack the weakest enemy within their reach.  If no enemy is in reach, the lurker does a random move instead - or it just sits and waits (lurks).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of different types of lurker behavior, check out the &amp;quot;Lurkers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Lurkers Micro AI by using&lt;br /&gt;
 ai_type=lurkers&lt;br /&gt;
in the [micro_ai] tag&lt;br /&gt;
&lt;br /&gt;
===Lurkers specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
* '''[filter]''': Specifies which units of the side are controlled by the Lurker Micro AI.  This is a [[StandardUnitFilter|Standard Unit Filter]].  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': Specifies the terrain from which the lurkers attack. This is a [[StandardLocationFilter|Standard Location Filter]].&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_wander]''': Specifies the terrain on which lurkers end their moves if there is no enemy in reach. This is a [[StandardLocationFilter|Standard Location Filter]].  If [filter_location_wander] is not given, it defaults to [filter_location].&lt;br /&gt;
* '''stationary'''=no: (boolean)  If set to yes, a lurker never moves if there is no enemy within attack range.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Lurkers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=4&lt;br /&gt;
             ai_type=lurkers&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Naga Fighter&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=W*,S*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             [filter_location_wander]&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location_wander]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Messenger Escort Micro AI (ai_type=messenger_escort) ==&lt;br /&gt;
&lt;br /&gt;
The Messenger Escort Micro AI lets you define a set of waypoints through which it tries to move one or several of its units, the messenger(s).  The other units escort the messenger, trying to protect it and attacking enemies that are in its way.  Note that a messenger might not strictly hit intermediate waypoints, but that getting close is good enough, especially if a waypoint is occupied by an enemy unit.  It does, however, always try to get to the last waypoint.&lt;br /&gt;
&lt;br /&gt;
'''Notes on the use of several messengers:''' {{DevFeature1.13|0}}  &lt;br /&gt;
* This Micro AI can control several messengers at the same time, which can but do not have to move in a group.  They can be put on the map altogether in one location, or be spread out on the map or across different turns.&lt;br /&gt;
* The farther a messenger has moved through the chain of waypoints, the higher a priority it is for the AI, meaning that farther advanced (on the map) messengers are moved first, and that the escort units try to clear the way and stay with those messengers more so than with those further back on the map.  This order can be inverted using the invert_order= key.&lt;br /&gt;
* If several messengers are on the map, escort units might move from one to the other, the AI making a judgement call where they are needed most.  Closeness to a messenger is generally the most important criterion, but not the only one.&lt;br /&gt;
* If a messenger dies, the escort units previously staying with it now join one of the other messengers if there are any left (otherwise they follow default AI behavior).&lt;br /&gt;
* If you want certain escort units to stay always with the same messenger, use several messenger [micro_ai] tags with different [filter]/[filter_second] combinations.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Messenger Escort Micro AI by using&lt;br /&gt;
 ai_type=messenger_escort&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Messenger Escort specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': {{DevFeature1.13|0}} (Previous to 1.13.0, only id= was allowed.) The id of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated list of waypoint coordinates through which the messenger will go in the given order to reach its goal, given by the last coordinate.  If you want the messenger to go directly to the goal, simply enter a single x,y coordinate here.  Note that the messenger does not have to hit each waypoint exactly (except for the final one), getting close to them is good enough. {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|11}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Messenger Escort AI consists of 3 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-2&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_death_chance'''=0.67 and '''messenger_death_chance'''=0.0: (both floats; ranging from 0 to 1)    If the messenger is adjacent to an enemy at the end of the move, it will only attack the enemy if the chance of death of the enemy is &amp;gt;= enemy_death_chance and if the messenger's chance of death is &amp;lt;= messenger_death_chance (or if it is an attack at a range for which the enemy does not have a weapon).&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|0}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the escort units.  If not set, this defaults to all enemy units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''invert_order'''=no: {{DevFeature1.13|0}} If set to 'yes', the order of priority with which messenger units are considered is inverted, starting with the messenger unit that is the least advanced on the map (see description above).  In most cases, selecting this option results in worse performance of the AI, so use it with caution.  (And obviously it has no effect at all if there is only one messenger controlled by the AI.)&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|11}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Messenger Escort&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=messenger_escort&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=messenger&lt;br /&gt;
             waypoint_x=31,24,27,28&lt;br /&gt;
             waypoint_y=20,14,7,1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Patrol Micro AI (ai_type=patrol) ==&lt;br /&gt;
&lt;br /&gt;
The defines a number of waypoints for a unit (or units) to follow.  Options can be set whether the route should be followed in a loop, as an out-and back, only once, whether units encountered along the way should be attacked, etc.&lt;br /&gt;
&lt;br /&gt;
With the default settings, getting to the next waypoint takes priority over attacking for a patroller, but this can be overridden (as of '''Wesnoth 1.15.12''', using the '''attack_range''' option).  Unless instructed otherwise, the AI thus prefers to move the patrol unit around enemies rather than straight for them.  Also, if a waypoint is occupied by a unit the AI is not instructed to attack, it will (eventually) abandon that waypoint once it gets close enough and move on to the next one.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different patrol behaviors, check out the &amp;quot;Patrols&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Patrol Micro AI it by using&lt;br /&gt;
 ai_type=patrol&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Patrol specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated lists of the waypoint coordinates through which the patrol travels (in order). {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack''': By default (if this key is not set), the patrol unit attacks any unit it ends up next to after a move, or that is within '''attack_range''' (see below).  Alternatively, this parameter can be set to a comma-separated list of unit ids. If that is done, the patrol only attacks these units when it ends up next to them or if it gets within attack_range.  It ignores any other unit.  This can in turn be used to have the patroller not attack at all, by setting ''attack='' to a non-existing id.  The exception to this is an enemy on the last waypoint with ''one_time_only=yes'', which will always be attacked.&lt;br /&gt;
* '''attack_range'''=1: {{DevFeature1.15|12}} If the patrol comes within this many hexes of an enemy unit, it interrupts its patrol route and attacks that enemy (assuming it still has enough movement left to get to the enemy).  The '''attack=''' instructions are taken into account for this.  Note that the patrol generally tries to move around enemies, which means that for the default value of 1, enemies are usually not within attack range, except at the end of the move.&lt;br /&gt;
* '''attack_invisible_enemies'''=no: {{DevFeature1.15|12}} If set to 'yes', the unit also attacks invisible enemies (according to the criteria set by '''attack''' and '''attack_range''').&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''one_time_only'''=no: (boolean)  If set to 'yes', the unit goes through the patrol route only once and then hangs out at the last waypoint.  It also always attacks any enemy unit on the last waypoint, independent of what ''attack='' is set to.&lt;br /&gt;
* '''out_and_back'''=no: (boolean)  By default, a patrol unit returns to the first waypoint after reaching the last, doing the patrol route as a loop.  If this parameter is set to 'yes', the unit instead reverses its course and perpetually does out and backs of the route.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Patrols&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=patrol&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Konrad&lt;br /&gt;
             waypoint_x=9,24,25&lt;br /&gt;
             waypoint_y=21,23,15&lt;br /&gt;
             one_time_only=yes&lt;br /&gt;
             attack=Gertburt&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Protect Unit Micro AI (ai_type=protect_unit) ==&lt;br /&gt;
&lt;br /&gt;
The Protect Unit Micro AI tries to move one or several units to a given location while keeping them safe from enemy attacks. It does so by keeping the protected units away from enemy units and close to friendly units. Other units on the AI side are controlled by the default Wesnoth AI.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Protect Unit&amp;quot; and &amp;quot;The Elves Besieged&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Protect Unit Micro AI by using&lt;br /&gt;
 ai_type=protect_unit&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Protect Unit specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[unit]''': A separate [unit] tag is required for each protected unit(s).  Each tag has required keys '''id''', '''goal_x''', '''goal_y''' for each unit, as shown in the example below. {{DevFeature1.15|0}} '''goal_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''disable_move_leader_to_keep'''=no: (boolean) If set to 'yes', side leaders protected by this AI do not try to return to their keep.  This is necessary for this Micro AI to work with units that are side leaders and that are supposed to move to an off-keep location.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
*The Protect Unit Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].  Depending on the parameters, it might also modify the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|move leader to keep candidate action]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded. It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;HttT: The Elves Besieged&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=protect_unit&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Delfador&lt;br /&gt;
                 goal_x,goal_y=1,2&lt;br /&gt;
             [/unit]&lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
                 goal_x,goal_y=1,1&lt;br /&gt;
             [/unit]&lt;br /&gt;
 &lt;br /&gt;
             disable_move_leader_to_keep=true&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Recruiting Micro AI==&lt;br /&gt;
=== General Comments on the Recruiting Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Recruiting Micro AI replaces the default recruitment AI by alternative recruiting patterns.  Currently there are two recruiting algorithms you can choose from, random recruiting and the rush recruiting used in the new (as of Version 1.11.1) Experimental AI (see below).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of a different recruiting pattern, check out the &amp;quot;Recruiting&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  You can also explore the unit choices by watching the Experimental AI in a multiplayer game.&lt;br /&gt;
&lt;br /&gt;
'''Note:''' Obviously, the Recruitment Micro AI modifies the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|recruitment candidate action]].&lt;br /&gt;
&lt;br /&gt;
=== Random Recruitment AI (ai_type=recruit_random) ===&lt;br /&gt;
&lt;br /&gt;
This AI randomly selects a unit type from the recruitment list of the side.  The probability of choosing each type and the behavior in low-gold situations can be influenced with optional parameters.&lt;br /&gt;
&lt;br /&gt;
Enable the Random Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_random&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* '''[probability]''': This tag sets the probability of selecting the given unit type.  It has two required sub-keys:&lt;br /&gt;
** '''type''': comma-separated list of unit types to which this probability is to be applied&lt;br /&gt;
** '''probability''': (non-negative float)  The probability with which these unit types is to be selected.  All unit types for which no [probability] tag is defined receive a default probability of 1.0, thus values should be chosen relative to that.  As an example, in the code below swordsmen and peasants are given a probability of 8, while no other units have assigned probabilities.  Thus, each of these two unit types is 8 times more likely to be selected than any other individual unit type.&lt;br /&gt;
* '''skip_low_gold_recruiting'''=no: (boolean)  By default, the random recruitment AI chooses a unit to recruit only from those types for which it has sufficient gold.  If this parameter is set to 'yes', it will instead choose from all available unit types and end recruiting for the turn if there is not enough gold to recruit it this turn (even if other, affordable unit types exist).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a random recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_random&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [probability]&lt;br /&gt;
                 type=Swordsman,Peasant&lt;br /&gt;
                 probability=8&lt;br /&gt;
             [/probability]&lt;br /&gt;
 &lt;br /&gt;
             skip_low_gold_recruiting=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Rush Recruitment AI (ai_type=recruit_rushers) ===&lt;br /&gt;
&lt;br /&gt;
This AI replaces the default recruiting with the rush recruiting used in the new (as of Version 1.11.1) Experimental AI.&lt;br /&gt;
&lt;br /&gt;
Enable the Rush Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_rushers&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* {{DevFeature1.15|2}} '''high_level_fraction'''=0: (non-negative number) The approximate fraction of units of level 2 or higher to be recruited. This is defined as fraction of units on the map, not of new units being recruited during a turn or over several turns (which makes a difference if there are already units on the map). The effect is also cumulative per level (starting from level 2), meaning that 1/3 of L2 units will be recruited in the example above, 1/3*1/3=1/9 L3s etc. The default value is zero, which leaves the high-level unit recruiting to the default Experimental AI algorithm resulting usually in very few such units being recruited.&lt;br /&gt;
* '''randomness'''=0.1: (number)  A random number is applied to the rusher recruit engine's score to prevent the recruitment pattern from being too predictable. 0 causes no randomness to be applied, while larger numbers increase the random effect. A value of 1-2 generates results in which the random effect is approximately equal to the scored effect. Extremely high values are essentially entirely random.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a rush recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_rushers&lt;br /&gt;
             action=add&lt;br /&gt;
          [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Simple Attack Micro AI (ai_type=simple_attack) ==&lt;br /&gt;
&lt;br /&gt;
The Simple Attack Micro AI is meant as an addition to the default combat candidate action for when one wants to force the AI to do attacks that it would otherwise not do (for example on units that are 1 XP from leveling) or to execute certain attacks before all others (for example, attack with all &amp;quot;expendable&amp;quot; units first).  It is not set up to be a replacement for the default attacks.  Some notes:&lt;br /&gt;
&lt;br /&gt;
* The algorithm is different from and simpler than that of the default combat CA.  For example, no attack combinations are considered, only individual attacks.  As a result, the [[AiWML#List_of_AI_Aspects|default aspects]] (aggression, caution, [avoid], ...) are not taken into account.  Also, if lots of units are involved, evaluation of this Micro AI is potentially quite a bit slower than of the default attacks.&lt;br /&gt;
&lt;br /&gt;
* If an attack matching the Standard Unit Filters described below is found, this attack will always be executed, no matter how bad the odds are.  Thus, this Micro AI can be used to force the AI to do attacks it would otherwise avoid.&lt;br /&gt;
&lt;br /&gt;
* Other than selecting the attacker/defender units, this AI is currently not configurable.  Let us know if you would like to see certain options to be added.  (No guarantees that we will add them, but we will definitely consider them.)&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Simple Attack&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=simple_attack&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Simple Attack specific keys for the [micro_ai] tag===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  It is, however, in general not desirable to use this AI without at least one of the optional Standard Unit Filters below, as this AI is not meant as a replacement of the default combat candidate action.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=110000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 110,000, which means that it is executed right before the default combat candidate action.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Simple Attack action.  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units the AI should attack.  It defaults to all enemy units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
* '''weapon'''=-1: (integer) The number of the weapon that should be used for this attack.  Weapon numbers start at 1 (not 0).  -1 stands for &amp;quot;best weapon&amp;quot; (by whatever criterion the AI chooses to use).   '''Available from Wesnoth 1.11.8'''&lt;br /&gt;
&lt;br /&gt;
Below are 2 examples of Simple Attack Micro AI configurations from the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  The first one shows how to force the AI to attack with all Soulless and Walking Corpse units first:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110000&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless,Walking Corpse&lt;br /&gt;
             [/filter]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
The second example executes attacks by Soulless on all enemy units that are 1 XP from leveling.  Note that the Lua code in the preload event is there simply in order to code the &amp;quot;1 XP from advancing&amp;quot; condition.  It is not needed for most other SUFs, as can be seen in the previous example.&lt;br /&gt;
     [event]&lt;br /&gt;
         name=preload&lt;br /&gt;
         first_time_only=no&lt;br /&gt;
         [lua]&lt;br /&gt;
             code=&amp;lt;&amp;lt;&lt;br /&gt;
                 function close_to_advancing(unit)&lt;br /&gt;
                     if (unit.experience &amp;gt;= unit.max_experience-1) then&lt;br /&gt;
                         return true&lt;br /&gt;
                     else&lt;br /&gt;
                         return false&lt;br /&gt;
                     end&lt;br /&gt;
                 end&lt;br /&gt;
             &amp;gt;&amp;gt;&lt;br /&gt;
         [/lua]&lt;br /&gt;
     [/event]&lt;br /&gt;
 &lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110001&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless  # No Walking Corpses; L0 units don't advance enemy&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 lua_function = &amp;quot;close_to_advancing&amp;quot;&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
Several Simple Attack Micro AIs can be combined for the same AI side.  The two examples above are both used (note the different values for ca_score) in the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
== Custom Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|5}}&lt;br /&gt;
&lt;br /&gt;
From 1.13.5 onwards, adding a new Micro AI can be easily done without altering any core files or overriding the micro_ai tag. The [micro_ai] tag now looks up its ''ai_type'' parameter in the &amp;lt;code&amp;gt;wesnoth.micro_ais&amp;lt;/code&amp;gt; table and uses the micro AI setup function found there to determine which keys are required and allowed and what candidate actions to add.&lt;br /&gt;
&lt;br /&gt;
A micro AI setup function takes as its single parameter the contents of the [micro_ai] tag and returns three separate values:&lt;br /&gt;
*'''required''': A list of required keys and tags, as strings. Tags are indicated by enclosing them within square brackets in the string. If a key or tag in this list is missing from the [micro_ai] tag, an error will be printed.&lt;br /&gt;
*'''optional''': Similar to above, a list of optional keys and tags, as strings. No error will be printed if a key or tag in this list is missing, nor will an error be printed if it is present (as would be the case for unrecognized keys or tags).&lt;br /&gt;
*'''ca_params''': A table specifying the candidate actions that constitute the micro AI. It should contain an ''ai_id'' key (which specifies a base ID to be used for candidate actions of this micro AI) and a series of candidate action definitions. Each candidate action definition is a table containing the following keys:&lt;br /&gt;
**'''ca_id''': The ID used for the candidate action. This will be combined with the ''ai_id'' to produce the final ID.&lt;br /&gt;
**'''location''': Path to the Lua file that defines the candidate action.&lt;br /&gt;
**'''score''': The maximum score for the candidate action. If the [micro_ai] tag specified a ''ca_score'' parameter, the score here should normally be based on that.&lt;br /&gt;
&lt;br /&gt;
(The return statement would thus look like &amp;lt;syntaxhighlight lang='lua' inline&amp;gt;return required, optional, ca_params&amp;lt;/syntaxhighlight&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
'''Examples''' of micro AI setup functions can be found in the [https://github.com/wesnoth/wesnoth/tree/1.14/data/ai/micro_ais/mai-defs default MAI definitions directory].  See the [https://github.com/wesnoth/wesnoth/blob/1.14/data/ai/micro_ais/mai-defs/animals.lua#L4-L12 Big Animals MAI definition] for a simple example.  For custom micro AIs, the setup code should be put into a ''[lua]'' tag inside a preload event.&lt;br /&gt;
&lt;br /&gt;
Since the function has full access to the contents of the [micro_ai] tag, it can conditionally add candidate actions to '''ca_params''' depending on the values of other keys within the [micro_ai] tag. It can also perform other actions such as adding facets to an aspect; if it does such things, it should pay attention to the ''action'' key to determine whether it's being added or removed.&lt;br /&gt;
&lt;br /&gt;
There are two useful helper functions that can be used by Micro AI setup functions requiring aspect changes. They can be brought into scope by a line similar to the following:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='lua&amp;gt;local MAIH = wesnoth.require(&amp;quot;ai/micro_ais/micro_ai_helper.lua&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* MAIH.add_aspects(side, aspect_params)&lt;br /&gt;
* MAIH.delete_aspects(side, aspect_params)&lt;br /&gt;
&lt;br /&gt;
The ''aspect_params'' parameter is an array of tables, each of which contains the following keys:&lt;br /&gt;
*'''aspect''': The aspect to change.&lt;br /&gt;
*'''facet''': The WML defining the facet. This must include an ''id'' key in order for the aspect to be correctly deleted. For maximum compatibility, incorporate the ''ca_id'' key from the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=67260</id>
		<title>Micro AIs</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=67260"/>
		<updated>2021-03-20T23:08:56Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Patrol specific keys for the [micro_ai] tag: */ add dev version number&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== A Few General Words about Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
* Micro AIs allow Wesnoth campaign and scenario authors to add new AI functionality without the need for any AI programming. They bring new specialized behavior to Wesnoth that cannot be achieved, at least not easily, by adjusting default AI parameters (such as those described at [[AiWML]]) or with WML code.&lt;br /&gt;
&lt;br /&gt;
* A Micro AI is activated and configured (or deleted) via the [micro_ai] tag, requiring only a few lines of WML code.&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.6''', the Micro AI behavior was inconsistent when it came to '''hidden enemies and petrified units'''.  Some Micro AIs ignored those, while others did not.  In some case there was even a potential for AI errors when an AI unit ran into hidden or petrified enemies.  As of 1.13.6, this has all been fixed.  The Micro AIs now correctly and consistently ignore hidden units and treat petrified units as blocking the hex they are on but having no effect otherwise, in the same way as the default AI does.  '''Important notes:'''&lt;br /&gt;
** It is not possible to re-include hidden/petrified units via the Standard Unit Filters (SUFs) used by some of the Micro AIs. In fact, if this is attempted, the filters will generally not match any unit.&lt;br /&gt;
*** This only applies to SUFs which go directly into the [micro_ai] tag. Any SUF that is a sub-tag of another filter (for example, of a Standard Location Filter) is taken literally and returns all units matching the filter. If hidden or petrified units are to be excluded from such a sub-filter, that needs to be specified directly in that filter.&lt;br /&gt;
** We also note that, because all the Micro AIs are written in Lua, it is possible to [[Micro_AIs#Using_Development_.281.13.29_Version_Micro_AIs_in_your_Stable_.281.12.29_Version_Add-on|move a Micro AI's code into one's add-on]] and modify it to whatever scenario-specific behavior is desired (this does not only apply to hidden/petrified units, but to all behavior). Setting up general Micro AI behavior that works in every possible setting is not feasible, but adapting it to work (only) in the specific setting of a given scenario is often quite simple. If you do not know how to do this, please post a message on the Wesnoth forums and we will be happy to help you with it.&lt;br /&gt;
&lt;br /&gt;
* The '''[[AiWML#List_of_AI_Aspects|default AI aspects]] are, for the most part, not taken into account by the Micro AIs'''.  For example, unless noted otherwise, specifying a value for ''aggression'' does not change the MAI behavior, and locations specified in an ''[avoid]'' tag generally can be entered by units controlled by an MAI.  This is a feature of the Micro AIs, not a bug.  If you feel that a certain default AI aspect should also apply to a Micro AI, please let us know in the [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 Micro AIs Feedback thread].&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.7''', the Micro AIs did not all work reliably when the '''AI side is under shroud''' and it may have been necessary to turn shroud off for the side when using a Micro AI. {{DevFeature1.13|7}} The Micro AIs now all work for sides under shroud. They do this by disregarding shroud for terrain purposes when path finding, consistent with default Wesnoth AI behavior, while still ignoring hidden units (including those hidden under shroud) correctly.&lt;br /&gt;
&lt;br /&gt;
* Most Micro AIs in their default setting apply to all units of a side, although many of them provide parameters for filtering the units that should be affected by the new behavior.  If the Micro AI is applied only to part of the units of a side, the other units follow the default Wesnoth AI behavior.&lt;br /&gt;
&lt;br /&gt;
* Micro AIs are available in mainline since Wesnoth 1.11/1.12.&lt;br /&gt;
&lt;br /&gt;
== Micro AI Test and Demo Scenarios ==&lt;br /&gt;
&lt;br /&gt;
All Micro AIs described here can be checked out in a number of test and demo scenarios.  The easiest way to access the test scenarios is by assigning a hotkey to &amp;quot;Start Test Scenario&amp;quot; in the preferences (as of Wesnoth 1.13.8). Then, simply press the hotkey in the titlescreen and select the desired scenario from the list (e.g. 'micro_ai_test' for the overall &amp;quot;switchboard scenario&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Test scenarios can also be accessed from the command line by typing&lt;br /&gt;
 path/wesnoth-executable -t micro_ai_test&lt;br /&gt;
or (depending on your system and Wesnoth version)&lt;br /&gt;
 path/wesnoth-executable -tmicro_ai_test&lt;br /&gt;
This starts up a &amp;quot;switchboard scenario&amp;quot; in which you can select the Micro AI demonstration scenario you want to check out.  Here, ''path/wesnoth-executable'' needs to be replaced by whatever the path and filename of the Wesnoth executable is on your system.&lt;br /&gt;
&lt;br /&gt;
== Setting up a Micro AI ==&lt;br /&gt;
&lt;br /&gt;
Setting up a Micro AI requires only one simple step:&lt;br /&gt;
==== Activating and configuring (or deleting) a Micro AI ====&lt;br /&gt;
&lt;br /&gt;
Micro AIs are activated, deleted and configured using the [micro_ai] tag.  This tag needs to be placed in [[ActionWML]], that is, in an event, a menu option or the like.  As an example, the following code activates the healer_support Micro AI in its default configuration for Side 2 from the beginning of the scenario:&lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
 &lt;br /&gt;
         # Configure the healer support micro AI&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
'''Keys required for all Micro AIs:'''&lt;br /&gt;
All [micro_ai] tags must contain the following three required keys:&lt;br /&gt;
* '''side''': The side for which the Micro AI is to be added, changed or deleted.  This has to be an individual side number, lists of sides are not supported.&lt;br /&gt;
* '''ai_type''': The type of Micro AI to be added, changed or deleted.  See the following sections for allowed values.&lt;br /&gt;
* '''action''' (string): The action to take concerning the Micro AI.  The following values are allowed: &lt;br /&gt;
** ''add'': Add a Micro AI to a side.&lt;br /&gt;
** ''change'': Change the configuration of an existing Micro AI.  Note that this does not only change the specific parameters provided in the tag, but it replaces the entire existing Micro AI by a new version with the new (and only the new) configuration.  It is therefore equivalent to using first the ''delete'' and then the ''add'' action.&lt;br /&gt;
** ''delete'': Delete an existing Micro AI from a side.&lt;br /&gt;
&lt;br /&gt;
'''Keys required under certain circumstances:'''&lt;br /&gt;
* '''ca_id''': (string) If several MAIs of the same type are used for the same side and one or several of these are to be changed or deleted during a scenario, this key needs to be included so that the [micro_ai] tag knows which of them needs to be changed/deleted.  The same is also true if a Micro AI is changed more than once during a scenario.  We therefore '''strongly recommend always setting ''ca_id''''' for MAIs which are, or might be, changed or deleted during a scenario.&lt;br /&gt;
** '''Note:''' The id(s)/name(s) actually assigned to the CA(s) of the MAI by the engine might be different from the parameter passed here.  It is, however, sufficient to use this ca_id with the [micro_ai] tag add/delete/change actions to identify the MAI uniquely.  (In other words: you don't have to worry about this if you always use the [micro_ai] tag, but might have to figure out the actual name(s) of the CA(s) if you want to delete or change them manually.)&lt;br /&gt;
* Other keys may also be required depending on the Micro AI.  In additional, a number of optional keys may be available to configure the AI behavior.  See the sections on the individual Micro AIs for details.&lt;br /&gt;
&lt;br /&gt;
'''Required keys for deleting a Micro AI:'''&lt;br /&gt;
&lt;br /&gt;
Only a subset of the keys required for setting up a Micro AI are needed when deleting it.  '''side''', '''ai_type''' and '''action'''=delete always need to be provided.  '''ca_id''' needs to be given if it was provided when adding the Micro AI (see above for when this is needed).&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* The ''add'' and ''change'' actions ignore all keys that do not apply to the respective Micro AI type.  The ''delete'' action ignores all keys other than the required keys listed above.&lt;br /&gt;
&lt;br /&gt;
* Some of the Micro AIs use and/or modify default AI components (candidate actions or aspects) when they are added, or reset these components to their default values when they are deleted.  Thus, if you have modified the AI yourself using the default CAs or aspects, some of these modifications might be affected, or might be interfering with the Micro AI.  The sections below indicate whether a Micro AI modifies any default AI components.&lt;br /&gt;
&lt;br /&gt;
===Combining Different Micro AI Types for the Same Side===&lt;br /&gt;
&lt;br /&gt;
No extra steps need to be taken when combining different Micro AIs on the same side.  Simply set up the [micro_ai] tags for all AIs you want to use.&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* While it is ''technically'' possible to combine any number of Micro AIs, this does not mean that all Micro AIs work together in practice.  Some are not compatible because they control units in contradictory ways.  &lt;br /&gt;
* Not all combinations that ''should'' work have been tested yet.  If you find a combination that you believe should work but doesn't, [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 please let us know] and we will see if that can be fixed.&lt;br /&gt;
* The candidate action score almost certainly has to be set (using the ca_score= key) for one or several of the MAIs if several of them are used on the same side.&lt;br /&gt;
&lt;br /&gt;
== Animals Micro AIs ==&lt;br /&gt;
=== General Comments on the Animal Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Animals Micro AIs are meant to simulate the behaviors of certain types of animals.  They are, however, set up so that they can be used with arbitrary unit types and might be applicable in quite diverse situations.  The AIs nevertheless remain named after the animals for which they were written originally, so that they are (to some extent) descriptive of their behavior.&lt;br /&gt;
&lt;br /&gt;
Brief descriptions of the AI behaviors are given in the following subsections.  To get a better feeling for their behavior, check out the &amp;quot;Animals&amp;quot;, &amp;quot;Swarm&amp;quot;, &amp;quot;Wolves&amp;quot; and &amp;quot;Dragon&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  Note that some of the animal AIs are designed for animal types that do not exist in mainline.  In the test scenario, other animal types are therefore substituted for them.&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Wolves (ai_type=wolves) ===&lt;br /&gt;
&lt;br /&gt;
The Wolves Micro AI organizes all &amp;quot;wolf&amp;quot; units (predators) of the side to hunt in a single pack.  They actively chase after the closest prey and try to corner it (not always super successfully), but are easily distracted by other enemy units coming into attack range, to the point where the pack might split up.  The wolves can, however, be told to attack only prey units as well as to try to avoid other types of units (such as larger predators).  When no prey is left, the wolves wander randomly.&lt;br /&gt;
&lt;br /&gt;
This AI works best for a small number of predators (2 - 5) and sparse prey.  Also, the predators should be put on the map in a pack, that is, reasonably close to each other.  For a demonstration of the Wolves AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Note that another wolves AI exists, called [[Micro_AIs#Animals_AI:_Multi-pack_Wolves_.28ai_type.3Dwolves_multipacks.29|Multipack Wolves]], which distributes the wolves of a side into several packs, rather than all of them into one single pack as it is done here.&lt;br /&gt;
&lt;br /&gt;
Enable the Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the predator units.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the prey units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_only_prey'''=no: In the default configuration, the wolves attack all enemy units in attack range.  If this key is set to 'yes', they only attack prey units.&lt;br /&gt;
* '''avoid_type''': Comma-separated list of unit types (on enemy sides) which the wolves try to avoid.  They try to stay out of these units' way as much as possible when moving around (except when they are moving in to attack another enemy unit) and never attack them.&lt;br /&gt;
* '''ca_score'''=90000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 90,000, which is lower than the default combat CA, but higher than most other default CAs which move units.  It can be changed by setting this parameter but in most cases this will not make sense.  Note that the Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Notes on default (RCA) AI aspects:''' &lt;br /&gt;
* If ''attack_only_prey'' or ''avoid_type'' are set, the Wolves Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* Attacks by the wolves are left to the default (RCA) AI, meaning that the value of ''aggression'' has an influence on the behavior.&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI is taken into account. Note: as attacks by the wolves are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=6&lt;br /&gt;
             ai_type=wolves&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Wolf&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Vampire Bat&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             avoid_type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Multi-pack Wolves (ai_type=wolves_multipacks) ===&lt;br /&gt;
&lt;br /&gt;
The Multi-pack Wolves Micro AI is different from the [[Micro_AIs#Animals_AI:_Wolves_.28ai_type.3Dwolves.29|Wolves Micro AI]] in that there can be an arbitrary number of wolf packs, with the pack size being a free parameter.  At the beginning of the scenario, close wolves are grouped into packs in a semi-methodical way.  If the wolves are put on the map in distinct groups of close wolves of the correct number, they will be joined into packs matching these groups.  If, on the other hand, they are spread out all over the map, the method of assigning them to packs is semi-random.&lt;br /&gt;
&lt;br /&gt;
Wolves of the same pack begin by joining each other on the map.  After that, they stay together until only one wolf is left, which then tries to join up with an incomplete pack or with other single wolves.  Individual wolves entering the map during the scenario behave in that way as well.&lt;br /&gt;
&lt;br /&gt;
A second difference to the other Wolves AI is that wolves do not actively hunt here.  For the most part they just wander (often long distance).  However, the pack ferociously (and without regard for its own health) attacks any enemy units that come into range, as long as that does not mean separating the pack by more than a few hexes.  Staying together, or joining with a new wolf assigned to the pack, is the only thing that takes priority over satisfying the wolves' thirst for blood.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Multi-pack Wolves AI behavior, check out the &amp;quot;Wolves&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Multi-pack Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves_multipacks&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Multi-pack Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''pack_size'''=3: (integer) The size of the packs.&lt;br /&gt;
* '''show_pack_number'''=no: If set to &amp;quot;yes&amp;quot;, the wolves' pack numbers will be shown below them.  Note that this means that the pack number of a wolf killed in an attack will remain as a label on the map.  &lt;br /&gt;
* '''type'''=Wolf: Comma-separated list of the unit types which the AI controls. The default value is &amp;quot;Wolf&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves_multipacks [micro_ai] tag usage from the &amp;quot;Wolves&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=wolves_multipacks&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             show_pack_number=yes&lt;br /&gt;
             pack_size=4&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Big Animals (ai_type=big_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Big Animals Micro AI is a simulation of large predators that wander and hunt alone, such as Bears, Giant Spiders or Yetis.  For the most part, these just wander on the terrain that's been defined for them, but they attack enemy units if those happen to come into range.  The AI can be set up so that the Big Animals stay out of the way of other units (such as other large predators), in which case they only attack those if cornered or if they run into them accidentally (such as ambushers).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Big Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Big Animals Micro AI by using&lt;br /&gt;
 ai_type=big_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the animals to which this AI is applied.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': The big animals often go for long distance (multi-turn) journeys.  This tag is a [[StandardLocationFilter|Standard Location Filter]] describing the terrain from which the next goal is chosen.  The default is all terrain on the map. &lt;br /&gt;
* '''[filter_location_wander]''': The terrain on which the big animals will end their moves. This is a [[StandardLocationFilter|Standard Location Filter]].  The default is all terrain on the map.&lt;br /&gt;
* '''[avoid_unit]''': A [[StandardUnitFilter|Standard Unit Filter]] describing all enemy units that the big animals will avoid.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
Here's an example of big_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=big_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Ghast&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [avoid_unit]&lt;br /&gt;
                 type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
             [/avoid_unit]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=1-40,1-18&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Forest Animals (ai_type=forest_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Forest Animals Micro AI simulates a number of different animal behaviors that one might find in a forest, namely animals behaving like deer, rabbits and families of tuskers.  In the following, we will refer to those animals by these types, even though the actual unit types can be selected freely.  They behave as follows:&lt;br /&gt;
* Each deer wanders randomly on (selectable) terrain, except when enemies get in its (the deer's) maximum movement range, in which case it flees to the farthest point it can reach.&lt;br /&gt;
* Tuskers exhibit the same behavior as deer, except when an enemy is next to one of the tusklets.  This enemy is attacked unconditionally by the tuskers trying to protect their young.&lt;br /&gt;
* Tusklets blindly follow the closest adult tusker, except when there is no tusker left, in which case they behave the same as deer.&lt;br /&gt;
* Rabbits also wander randomly, but in addition disappear into their holes (if any are within reach) when enemies are close.  They reappear out of their holes at the beginning of the turn, if it is safe.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Forest Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Forest Animals Micro AI by using&lt;br /&gt;
 ai_type=forest_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Forest Animals AI consists of 4 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-3&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''rabbit_type, tusker_type, tusklet_type, deer_type''': Comma-separated lists of the unit types behaving as the respective animals.  If any of these parameters are not set, those types of animal behavior are skipped by the AI.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain on which the forest animals wander when they are not threatened by enemies.&lt;br /&gt;
* '''rabbit_number'''=6: (integer)  The number of rabbits for the AI to have on the map simultaneously.  The AI puts the missing number of rabbits onto the map at the beginning of the AI's turn (when the rabbits come out of their holes).&lt;br /&gt;
* '''rabbit_enemy_distance'''=3: (number)  Rabbits won't spawn in holes that aren't more than this distance away from all enemies.&lt;br /&gt;
* '''rabbit_hole_img''': Rabbit holes are marked by items on the map.  If this parameter is set, it must be the file name of the item image.  If it is not set, all items on the map are treated as rabbit holes.&lt;br /&gt;
** {{DevFeature1.14|10}} Items used for rabbit holes on the map border used to produce an error. Placing them on the map border therefore needed to be avoided on the scenario setup side. They are now automatically excluded from consideration by the AI code.&lt;br /&gt;
&lt;br /&gt;
Here's an example of forest_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=forest_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             deer_type=Vampire Bat&lt;br /&gt;
             rabbit_type=Giant Rat&lt;br /&gt;
             tusker_type=Ogre&lt;br /&gt;
             tusklet_type=Young Ogre&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=*^F*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Herd and Herders (ai_type=herding) ===&lt;br /&gt;
&lt;br /&gt;
The Herding Micro AI sets up an area on the map where a number of herder units (such as sheep dog) control and protect a herd (such as sheep).  For convenience, we simply refer to them as sheep dogs and sheep in the following, even though arbitrary unit types can be selected for either.&lt;br /&gt;
&lt;br /&gt;
Sheep dogs try to keep their sheep safe.  This involves keeping them inside the herding area, positioning themselves in between the sheep and approaching enemies, and attacking the enemies if those get too close.  If no active herding or protecting move is needed, the dogs go to a random location on the perimeter of the herding area.&lt;br /&gt;
&lt;br /&gt;
Sheep wander aimlessly except when a sheep dog is next to them, in which case they run away from the dog.  The dogs exploit this by positioning themselves on the outside of the sheep, if possible.  Sheep also run away from approaching enemies.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Herding AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Herding Micro AI by using&lt;br /&gt;
 ai_type=herding&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herder units (e.g. dogs).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herd units (e.g. sheep).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain forming the perimeter of the herding area.  In other words, this is the path the herders (dogs) use to patrol around the outside of the herding area.  This must be a set of coordinates ''completely enclosing'' the herding area.  Here's an example of setting up a rectangular perimeter:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         x=10-20,10-20,10,  ,20&lt;br /&gt;
         y=11,   21,   11-21,11-21&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
Another possibility is by defining the path by certain terrain on the map (as it is done in the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]), in which case the filter can simply look like this:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         terrain=Rb&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
* '''herd_x,herd_y''': The coordinate of one hex (''any'' one hex) inside the herding area.  This is needed to define which part of the map is on the ''inside'' of the herding area perimeter defined by [herding_perimeter], and which is the ''outside''. {{DevFeature1.15|0}} '''herd_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''attention_distance'''=8: (integer)  If an enemy is within this distance of a herd unit, but not as close as ''attack_distance'', the herders try to position themselves in between the enemy and the herd unit.&lt;br /&gt;
* '''attack_distance'''=4: (integer)  If an enemy is within this distance of a herd animal, the herders attack this enemy.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Herding AI consists of 6 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-5&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Herding [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=herding&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Footpad&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Troll Whelp&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             herd_x,herd_y=32,28&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=Rb&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Hunter (ai_type=hunter) ===&lt;br /&gt;
&lt;br /&gt;
The Hunter Micro AI leads a unit (or units) through hunt-and-rest cycles.  The units wander in a prescribed area of the map, their hunting ground, until an enemy unit comes within range.  Note that the units do not actively chase down enemies, but wander randomly through the hunting ground until an enemy is encountered.&lt;br /&gt;
&lt;br /&gt;
If enemies are within range of a hunter unit, the hunter attacks the weakest of them.  If a kill is made, it then retreats to its rest location, where it stays for a certain number of turns and until fully healed (whatever happens later).&lt;br /&gt;
&lt;br /&gt;
A few more details:&lt;br /&gt;
* If the hunter's way home is entirely blocked on the return (so that there is no possible path), the normal RCA AI takes over its behavior.&lt;br /&gt;
* However, if the way is blocked by an enemy unit occupying the rest hex, that enemy will be attacked unconditionally.&lt;br /&gt;
* A kill only makes the hunter go home when it is the attacker, not as defender.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Hunter AI behavior, check out the &amp;quot;Dragon&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]. &lt;br /&gt;
&lt;br /&gt;
Enable the Hunter Micro AI by using&lt;br /&gt;
 ai_type=hunter&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''home_x,home_y''': The coordinates of the hex to which the hunter returns and rests at after a kill. {{DevFeature1.15|0}} '''home_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the unit's hunting ground.  If this tag is not provided, the hunter uses all of the map.&lt;br /&gt;
* '''rest_turns'''=3: (integer)  The number of turns the AI stays at home_x,home_y after a kill.  It will not leave its home until this number of turns have passed ''and'' it is fully healed.&lt;br /&gt;
* '''show_messages'''=no: (boolean) If set to yes, the hunter announces whenever its behavior changes to the next phase.  Note that this is meant as debug output for the scenario author.  The messages are not translated and are not included in replays (and are only visible to the host in an MP game).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a hunter [micro_ai] tag usage from the &amp;quot;Dragon&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hunter&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Rowck&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=5-30,1-15&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             home_x,home_y=3,15&lt;br /&gt;
             rest_turns=2&lt;br /&gt;
 &lt;br /&gt;
             show_messages=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Swarm (ai_type=swarm) ===&lt;br /&gt;
&lt;br /&gt;
The Swarm Micro AI uses some very simply algorithms to simulate animal swarm behavior.  Without adjacent enemies, they simply do a random move, trying to stay together and at a certain distance from enemies.  However, if an enemy unit is close to any bat, the swarm scatters.  This is particular fun to watch when one places an enemy unit in the middle of the swarm.  After being scattered, the swarm members slowly rejoin, but not in a very organized way.  Sub-swarms or individual bats might roam around for quite some time before they find their way back.  It is also possible that individual bats (or small groups) split off from the larger swarm at times.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Swarm AI behavior, check out the &amp;quot;Swarm&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Swarm Micro AI by using&lt;br /&gt;
 ai_type=swarm&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Swarm AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_distance'''=5: (int) The minimum distance kept between units of the swarm and enemies when the swarm moves as a whole.&lt;br /&gt;
* '''scatter_distance'''=3: (int) Enemies within &amp;quot;scatter_distance&amp;quot; hexes of any swarm unit cause the swarm to scatter, by each unit trying to maximize its individual distance from the enemy.&lt;br /&gt;
* '''vision_distance'''=12: (int) Only units within this distance follow the overall swarm motion (either away from an enemy or of the swarm as a whole).  The smaller this value is set, the less likely the swarm is to stay together or rejoin.  This parameter is meant to simulate how far an individual member of the swarm can see, meaning that the swarm is &amp;quot;out of sight&amp;quot; (and the unit will not be able to follow it) if it is more than this distance away.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Swarm [micro_ai] tag usage from the &amp;quot;Swarm&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=swarm&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Assassin Squad Micro AI (ai_type=assassin) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|6}}&lt;br /&gt;
&lt;br /&gt;
This Micro AI controls one or several assassin units which try to circle around enemy units with the goal of hitting a target unit without being distracted or impeded by other enemies.  The units will always attack the target if such attacks are possible, otherwise they will follow the path of least resistance toward it. Notes:&lt;br /&gt;
&lt;br /&gt;
* The main priority for the AI is to find paths that avoid enemy units. However, if no such path can be found, the assassins will still advance toward the target, taking the path exposing the units to the least potential damage from enemies.&lt;br /&gt;
&lt;br /&gt;
* The units will not necessarily follow the same path. The best path is chosen individually for each unit.&lt;br /&gt;
&lt;br /&gt;
* While it is not a requirement for the AI to work, using ambusher or skirmisher units, or units with the quick trait, makes this AI more lethal in many cases.&lt;br /&gt;
&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded, it does not work otherwise. That means that '''ca_score''' is not a valid key for the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=assassin&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Assassin Squad specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the assassin units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the target unit.&lt;br /&gt;
** It is important to realize that the AI currently only works correctly with a single target unit. It is thus the responsibility of the scenario designer to make sure that this filter matches an individual enemy unit. If several enemy units are to be targeted in the same scenario, several instances of the Assassin Micro AI can be used.&lt;br /&gt;
** The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[prefer]''': A [[StandardLocationFilter|Standard Location Filter]] selecting locations on the map for the assassins to prefer when making their way toward the target unit. This could be used, for example, to make units with the ambush ability move (mostly) through forest or to have the assassins circle around the, say, northern part of the map rather than the south if that is desirable for scenario design purposes.&lt;br /&gt;
** As with avoiding enemies, the units will still move across non-preferred terrain if that is either the only option or ''much'' shorter than preferred terrain&lt;br /&gt;
** Attacking, once the target unit is in range, is not influenced by the [prefer] SLF.&lt;br /&gt;
&lt;br /&gt;
Here is an example of an Assassin Squad [micro_ai] tag usage:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=assassin&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Assassin,Master at Arms,Duelist&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             [prefer] # Circle around northern part of map&lt;br /&gt;
                 x=1-9, 10-99&lt;br /&gt;
                 y=1-37, 1-17&lt;br /&gt;
             [/prefer]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Bottleneck Defense Micro AI (ai_type=bottleneck_defense) ==&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense Micro AI lets you define a location on the map where the AI can take a defensive stance behind a narrow passage (bottleneck).  Units on the front line are backed up by healers and/or units with the leadership ability.  Injured units are moved to the back and replaced by uninjured (or less injured) units.  The units on the front line only attack when it is safe (no retaliation) or when there is a high chance that they will make a kill or level up.  Using this Micro AI only makes sense if there is no way for the enemy sides to move around the bottleneck and attack the AI's units from behind.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Bottleneck&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Bottleneck Defense Micro AI by using&lt;br /&gt;
 ai_type=bottleneck_defense&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Bottleneck Defense specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense AI requires two sets of coordinates that define where it should be taking up its defensive stance, and from which side the enemy attacks at that location.&lt;br /&gt;
&lt;br /&gt;
* '''x,y''': Comma separated lists of the hexes on the front line, where strong units are placed.  All hexes on which the AI makes contact with (can be attacked by) the enemy need to be included here.  Units are placed on them in the order in which they are listed here. {{DevFeature1.15|0}} '''location_id''' can be used instead with named/special locations.&lt;br /&gt;
* '''enemy_x,enemy_y''': Comma separated list of the hexes from which the enemy can attack the AI front line units.  This is needed for the AI to know on which side of the front line support units (healers etc.) need to be placed.  In many cases, it is sufficient to provide only one of the enemy front line hexes, but there are geometries for which that does not work.  It is therefore safer to list all enemy front line hexes here. {{DevFeature1.15|0}} '''enemy_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
By default, the AI places healers and units with the leadership ability both on the front line itself (if they are the strongest units available) and on hexes adjacent to and behind the line.  If different placement is desired, these locations can be overridden with the following keys.&lt;br /&gt;
&lt;br /&gt;
* '''active_side_leader'''=no: (boolean)  If set to 'yes', the side leader participates in the bottleneck defense action after the side's gold has been spent.  If set to 'no' (default), the leader follows default AI behavior (sitting on the keep and recruiting, for the most part).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Bottleneck Defense AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.15|3}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the units.  Only AI units matching this filter participate in the bottleneck defense.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''healer_x,healer_y''': Comma separated list of hexes where healers are placed.  This can be used to keep healers from the front line (or to take up only certain positions on the front line), and/or to override the default healing positions behind the line.  Healers are placed on them in the order in which the hexes are listed, with the exception that hexes on the line always take priority over hexes behind the line. {{DevFeature1.15|0}} '''healer_loc''' can be used instead with named/special locations.&lt;br /&gt;
* '''leadership_x,leadership_y''': Same as ''healer_x,healer_y'', but for units with the leadership ability. {{DevFeature1.15|0}} '''leadership_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
As an example, in the &amp;quot;Bottleneck&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]], there are three front line hexes, two of which are on hill terrain, while the third is a road with a lower defense rating.  The healer and side leader are supposed to participate in combat because they are strong units, but only from the hill hexes to keep them safer.  This is accomplished by using the following keys settings (check out the scenario to see which hex is which):&lt;br /&gt;
 [micro_ai]&lt;br /&gt;
     side=1&lt;br /&gt;
     ai_type=bottleneck_defense&lt;br /&gt;
     action=add&lt;br /&gt;
 &lt;br /&gt;
     x=14,14,14&lt;br /&gt;
     y= 7, 9, 8&lt;br /&gt;
     enemy_x=13,13&lt;br /&gt;
     enemy_y= 8, 9&lt;br /&gt;
 &lt;br /&gt;
     healer_x=14,14,15,15&lt;br /&gt;
     healer_y= 7, 9, 8, 9&lt;br /&gt;
     leadership_x=14,14,15,15&lt;br /&gt;
     leadership_y= 7, 9, 9 ,8&lt;br /&gt;
     active_side_leader=yes&lt;br /&gt;
 [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Fast Micro AI (ai_type=fast_ai) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|0}}&lt;br /&gt;
&lt;br /&gt;
This AI is meant as a fall-back AI for scenarios where there are so many units that attacks and moves done by the default AI are slow with long delays before each move.  It replaces the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions (CAs).  It should be obvious that in order to achieve the faster speed, compromises concerning the quality of the moves have to be made.&lt;br /&gt;
&lt;br /&gt;
Enable the Fast Micro AI by using&lt;br /&gt;
 ai_type=fast_ai&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Combat candidate action:'''&lt;br /&gt;
* Attackers are considered individually one at a time.  The best attack found for a unit is executed without checking whether a better attack by another unit might be possible.&lt;br /&gt;
* The default order in which attackers are considered is by decreasing hitpoints, starting with the strongest unit (in terms of HP).  This can be modified with the optional keys below.&lt;br /&gt;
* By default, if weaker units block attack hexes for a stronger unit, they do not move out of the way (this is the same as for the default AI).  If a hex becomes available after an attack (e.g. due to a weaker unit dying in the attack), all attacks that were possible ''before'' this happened are finished first.  Only after that are attacks by the stronger unit reconsidered.&lt;br /&gt;
* If include_occupied_attack_hexes=yes is set, weaker units move out of the way for stronger units if possible.  This can, however slow down the AI depending on the situation on the map.  Use with caution.&lt;br /&gt;
* {{DevFeature1.13|2}} Previous to this version, attacks by the leader were bunched in with the attacks by the other units.  Now leader attacks are treated separately and contain an extra layer of analysis to determine whether the attack position is &amp;quot;safe&amp;quot; (in an approximate way).  Currently, this is done with two simple considerations:&lt;br /&gt;
** Count all enemy units that can reach the leader's attack position.  If there are more than given by optional parameter 'leader_attack_max_units' (default value 3), do not attack.&lt;br /&gt;
** Add up the hitpoints of all enemies that can reach the leader's attack position.  If this value, multiplied by optional parameter 'leader_weight'(default value 2), is larger than the leaders hitpoints, do not attack.&lt;br /&gt;
** Two optional parameters, 'threatened_leader_fights' and 'leader_additional_threat', can also be used to let the leader attack when the threat at the attack hex is comparable to that at the leader's current position.&lt;br /&gt;
** In order to keep this AI as fast as possible, leader attacks are done by a separate candidate action that is only evaluated when all other CAs are done.&lt;br /&gt;
&lt;br /&gt;
'''Move-to-targets, villages and retreat candidate actions:'''&lt;br /&gt;
* The move-to-targets, villages and retreat candidate actions of the default AI are removed.  Move-to-targets and villages are taken over by the Fast Micro AI, whereas retreat is simply not done.&lt;br /&gt;
* This CA moves units toward goals after attacks are done.  Goals are unowned (if the AI side has a leader only) and enemy-owned villages and enemy leaders.&lt;br /&gt;
* For each goal, units are pre-sorted in order of the fewest moves needed to cover the ''distance'' toward the goal (that is, as if move cost on each hex toward the goal were 1).  Units are then considered sequentially and if a unit can get there &amp;quot;efficiently&amp;quot;, its move is executed without considering other units.&lt;br /&gt;
** A move is considered efficient if the move cost to get to the hex is not more than 'move_cost_factor' larger than the distance to the hex.  'move_cost_factor' is an optional key that defaults to 2.  It can be set to values of 1.1 or larger (see below).&lt;br /&gt;
** If the move for the current unit is not found to be efficient, the AI considers the next unit instead.  If no unit can do an efficient move toward the goal, the best (fastest) of the inefficient moves is selected instead.&lt;br /&gt;
&lt;br /&gt;
* Village goals:&lt;br /&gt;
** Village goals are considered before enemy leader goals so that the AI preferentially chooses scouts (or otherwise fast units) for them.&lt;br /&gt;
** The number of units sent toward villages depends on the value of the default AI aspect village_value.  It is set to village_value/4 times the number of units the AI has left to move.  Thus, by default (village_value=1.0) the AI sends a quarter of its units toward villages, and the rest toward enemy leaders.  This means that the meaningful range of values for village_value is 0..4.&lt;br /&gt;
** Only one unit is moved toward each village.  This consideration also overrides the number of units to move toward villages defined by village_value, topping it at the number of unowned/enemy-owned villages on the map.&lt;br /&gt;
** Setting village_value=0 disables moving units toward villages altogether.  That is, not only are no units chosen, but the village targeting phase of the AI is entirely disabled with no evaluation time used for it.  Note that, unlike with the default AI, this also means that no villages within range of an AI unit are grabbed.&lt;br /&gt;
** The order in which villages are chosen as goals is some combination of them being close to the AI side's leader and being spread out over the map.&lt;br /&gt;
&lt;br /&gt;
* Enemy leader goals:&lt;br /&gt;
** The order of enemy leaders goals is set according to their distance from the AI leader with the closest enemy leader being targeted first (if the AI has a leader, otherwise their order is random).  One unit is sent toward each enemy leader in this order, after which the AI starts with the first enemy leader again.  This is repeated until all units have moved.&lt;br /&gt;
&lt;br /&gt;
* Guardians (units with ai_special=guardian set) are ignored by the fast move-to-targets CA.&lt;br /&gt;
&lt;br /&gt;
'''Tips for speeding up/modifying the Fast Micro AI in specific scenarios:'''&lt;br /&gt;
&lt;br /&gt;
A number of optional keys and default AI aspects can be used to modify the behavior of the Fast Micro AI.  The MAI is set up so that it is a reasonable compromise between speed and quality of the moves in most situations.  However, this might not be ideal for all scenarios here are some things that can be done to speed up the AI (or to get better behavior in some situations by doing the opposite):&lt;br /&gt;
* Use large values for the default AI aspect aggression.  The largest delays before attacks happen (in some circumstances) when the Fast MAI has to consider lots of units with attacks left but decides not to attack with them.  See below for a description of how aggression is used by the fast AI.&lt;br /&gt;
* Use large values of move_cost_factor (see above for description of what this does).  If you want to emphasize units that can move across terrain easily (e.g. bats) to be chosen for village grabbing, you might want to use low values of move_cost_factor, but this might slow down the AI.  The default value should work reasonably well in most standard situations, but modifying the value might work better on specific maps.&lt;br /&gt;
* Don't use dungeon_mode=yes unless absolutely necessary, even in most dungeon crawls.  Test the AI without this setting first and only use it if it is the only way to solve the problem.  Often, a minor tweak to the map will have the same effect, without the speed penalty.&lt;br /&gt;
* Use include_occupied_attack_hexes=yes with caution.  It might (or might not) slow down the attack CA significantly, depending on the scenario.&lt;br /&gt;
* Set village_value=0, which makes the AI not consider villages as goals.  This has not been found to make a large difference on the maps tested so far, but it might have an effect on maps with a very large number of villages.&lt;br /&gt;
* Use attack_hidden_enemies=yes on maps where there are no hidden enemies. Excluding hidden enemies from the attack calculation requires an additional filter to be evaluated that takes extra time. In most cases this will not result in a large computation time saving.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_hidden_enemies'''=no: (boolean)  By default, hidden enemies are excluded from the attack calculation.  Setting this to 'yes' makes the AI also consider those enemies as attack targets.&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.13|2}} A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  As with the default AI, units will not step on hexes defined by the [avoid] tag.  In addition, no goals will be chosen that lie on those hexes.  The one minor exception is that a unit stepping out of the way for another unit might temporarily enter the avoided zone.&lt;br /&gt;
* '''dungeon_mode'''=no: (boolean)  As much as possible, the Fast Micro AI works with distances between hexes rather than path finding.  This works pretty well on open maps and even most dungeon crawls (and similar scenarios), but it might result in some units getting temporarily (and in rare cases permanently) stuck on maps with convoluted labyrinths.  If that is the case, setting dungeon_mode=yes triggers more path finding that avoids such situations (but as a result is somewhat slower).&lt;br /&gt;
* '''include_occupied_attack_hexes'''=no: (boolean) If set to 'yes', the AI moves weaker units out of the way for a stronger attacker.  This might lead to better attacks but is slower, so don't use if speed is essential.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the attackers.  Only AI units matching this filter will attack, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes the side= key set to the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the enemies.  Only enemy units matching this filter will be attacked, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''leader_weight'''=2: (number &amp;gt;= 0) {{DevFeature1.13|2}} A factor specifying how much more valuable the AI considers side leaders than non-leader units in the attack evaluation. This is also used to determine whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''leader_additional_threat'''=1: (number &amp;gt; 0) {{DevFeature1.13|2}} If threatened_leader_fights=yes, the threat at the attack position may be larger by this factor for the leader to leave the keep for an attack.&lt;br /&gt;
* '''leader_attack_max_units'''=3: (number &amp;gt;= 0) {{DevFeature1.13|2}} A number specifying how many enemy units within reach are acceptable when determining whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''move_cost_factor'''=2: (number &amp;gt;= 1.1)  The factor by which the move cost to a goal hex may be larger than the distance to the goal hex for a move to be considered &amp;quot;efficient&amp;quot;.  See description above.  1.1 is used if this is set to values smaller than 1.1.  Smaller values might lead to better behavior, while larger values might speed up the AI.&lt;br /&gt;
* '''weak_units_first'''=no: (boolean) If set to 'yes', inverts the order in which units are considered by the combat CA.  Attacks are then done in order of increasing hitpoints of the attackers.&lt;br /&gt;
* '''skip_combat_ca'''=no: (boolean) If set to 'yes', the fast combat CA is not used.  The default AI's high_xp_attack and combat CAs remains in place in this case.&lt;br /&gt;
* '''skip_move_ca'''=no: (boolean) If set to 'yes', the fast move-to-targets CA is not used.  The default AI's retreat, villages and move-to-targets CAs remains in place in this case.&lt;br /&gt;
* '''threatened_leader_fights'''=no: (boolean) {{DevFeature1.13|2}} By default, the AI leader will not attack if the threat at the attack position is too high.  If this key is set to 'yes', the leader will attack if the threat there is not larger than the threat at his current position multiplied by the value of 'leader_additional_threat'. This is meant as an option for the endgame: when the AI leader is under attack, he might as well go down fighting.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''aggression''': The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to set how aggressive the AI should attack.  It works in roughly the same way as the default AI aspect:&lt;br /&gt;
** The value of (1 - aggression) determines how much the AI values its own units compared to the enemy's units.  Thus, aggression=0 means that attacks are only done if the AI expects to deal (roughly) the same amount of damage as it will receive.  aggression=1 means that expected damage to the AI's units is not considered, only the expected damage to enemy units matters.&lt;br /&gt;
** Damage is calculated as a combination of hitpoints and chance to die, in &amp;quot;gold equivalent units&amp;quot;.&lt;br /&gt;
** Unlike the default AI, aggression is set to 1 for values larger than 1.  Allowed values are thus anything from -infinity to +infinity, although only -infinity to +1.0 make sense.&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.13|2}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
*'''attacks aspect''':  {{DevFeature1.13|2}} The standard RCA AI [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] used to filter own and enemy units for attacks is now also taken into account as long as neither [filter] nor [filter_second] (see above under 'optional keys') are defined in the [micro_ai] tag.&lt;br /&gt;
*'''village_value=1''': (number ranging from 0 to 4) The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to define how much villages should be targeted by the move-to-targets CA.  In the Fast MAI, this defines which fraction of the AI units are moved toward village goals (see above for details).  If set to 0, villages are ignored as goals.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded to the same values as those of the respective [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|default candidate actions]].  ca_score is therefore not a valid optional key.&lt;br /&gt;
* The fast Micro AI modifies (removes) the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions.&lt;br /&gt;
&lt;br /&gt;
Enabling the Fast Micro AI in its default configuration is as simple as this usage example from the &amp;quot;Fast AI&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=fast_ai&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Generalized Goto Micro AI (ai_type=goto) ==&lt;br /&gt;
&lt;br /&gt;
The Goto Micro AI provides a highly configurable method of sending a unit (or units) to a location or set of locations.  The units to be moved are defined using a [[StandardUnitFilter|Standard Unit Filter]] (SUF) [alternatively, all side units are used if the SUF is not provided), while the goto locations are given in a [[StandardLocationFilter|Standard Location Filter]] (SLF).&lt;br /&gt;
&lt;br /&gt;
By default, each unit matching the SUF will move toward the closest goal (in terms of turns needed to get there) matching the SLF via the fastest route given the current situation on the map (locations of enemy units etc.), but this behavior can be influenced by the optional keys listed below.  Note that this means that a unit's goal might change from one turn to the next depending on the situation on the map.&lt;br /&gt;
&lt;br /&gt;
The behavior of the unit(s) once they reach their goal location(s) is configurable as well.  By default, they will stay there until the Goto Micro AI is deleted or changed.&lt;br /&gt;
&lt;br /&gt;
For demonstrations of several Goto Micro AI usages, check out the &amp;quot;Goto&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Goto Micro AI by using&lt;br /&gt;
 ai_type=goto&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Goto specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the possible goal locations for the move.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists). Note that the area defined by [avoid] is not entered at all, even if the end point of the move would lie outside the area again.&lt;br /&gt;
* '''avoid_enemies''': (number&amp;gt;0) By default, units controlled by the Goto Micro AI will not take enemy units into account for route finding, other than the enemies' ZoCs.  By contrast, if this key is set, they will try to avoid traversing hexes on which they can be attacked by enemies.  The larger the value of ''avoid_enemies'', the more the units will try to stay out of the enemies' way.  Notes:&lt;br /&gt;
** Units are willing to take a path that is 'avoid_enemies' movement points longer for each enemy unit that can be avoided by doing so for each hex along the path.  As an example, if avoid_enemies=1 and the shortest path crosses 2 hexes that can be reached by 3 enemies each, the unit is willing to take a path that is up to 6 hexes longer instead.  If avoid_enemies=10 in the same situation, the unit will take a path up to 60 hexes longer.&lt;br /&gt;
** Hexes adjacent to enemy units are always strongly avoided, independent of the value of avoid_enemies.&lt;br /&gt;
** avoid_enemies is ignored if 'use_straight_line=true' is set.&lt;br /&gt;
** Due to an issue with the path finding code, '''tunnels and teleports are ignored in Wesnoth 1.14 and before''' if 'avoid_enemies' is used. This has been fixed in Wesnoth 1.15.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units to be moved.  Defaults to all units of the side if not defined.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''ignore_enemy_at_goal'''=no and '''ignore_units'''=no: (boolean)  In the default configuration, the Goto MAI does not move units if the path to all goal hexes is blocked by enemies.  It is therefore possible to disable the Goto MAI by putting enemy units on all the goal hexes.  That can be changed if either of these two keys is set to 'yes'.&lt;br /&gt;
** 'ignore_enemy_at_goal=yes' makes the AI choose its goals while disregarding any enemy units at the goal hexes.  Even so, a stiff rating penalty is applied to goal hexes occupied by enemies, thus that unoccupied goal hexes are chosen unless all possible goals are occupied by enemies.  Note that this setting ''only'' ignores enemy units at the goal hexes themselves.  It is still possible to disable the AI by putting enemies all around the goals.&lt;br /&gt;
** 'ignore_units=yes' let's the AI ignore ''all'' enemy units when deciding which goal hex(es) to move toward.&lt;br /&gt;
** In both cases, enemies are only ignored for deciding which unit to move toward which goal hex.  For the actual move itself, all enemies and their ZoCs are considered as usual.&lt;br /&gt;
** Both options only take effect if neither avoid_enemies is used, nor use_straight_line is set to 'yes', as neither of these consider enemies as blockers. Or in other words, if avoid_enemies is used, or use_straight_line=yes, it is unnecessary to set ignore_enemy_at_goal or ignore_units to yes.&lt;br /&gt;
* '''release_unit_at_goal'''=no: (boolean) By default, a unit that reaches a goal location will stay there until the Goto Micro AI is deleted or changed (or until the conditions specified in the [goto_goals] SLF change).  If this parameter is set, a unit that has reached its goal will not be controlled by this Micro AI any more starting from the next turn.&lt;br /&gt;
* '''release_all_units_at_goal'''=no: (boolean) Same as ''release_unit_at_goal'', but this will deactivate the Goto Micro AI for all units once the first unit reaches a goal.&lt;br /&gt;
* '''remove_movement'''=yes: (boolean):  {{DevFeature1.15|11}}  By default, when the Goto MAI cannot move a unit (for example, because the path is blocked by enemies), its movement points are still removed. If this option is set to 'no', this step does not happen, so that other AI candidate actions (or even another Goto MAI with different parameters) can take over instead.&lt;br /&gt;
* '''unique_goals'''=no: (boolean)  If set to 'yes', each unit will be sent to a different goal location.  Note that the best goals are recalculated each turn, so a unit might choose a different goal this turn from what it was heading for during the previous turn if the situation on the map has changed.  Also note that if there are more units than goal locations, the left over units will not be handled by the Goto MAI but follow default behavior instead.&lt;br /&gt;
* '''use_straight_line'''=no: (boolean)  By default, units choose the route by which they will get to their goal locations in the smallest number of turns.  If this parameter is set to 'yes', a straight line route (or something pretty close to it at least) will be chosen instead, irrespective of whether this leads into a dead end, whether it is blocked by enemies, etc.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* Depending on the parameters used, the Goto Micro AI might require a lot of path finding to be done.  Thus, while it is possible to set up this AI to move many units to many different locations, a usage like that might require quite a long calculation time.&lt;br /&gt;
&lt;br /&gt;
===Goto Micro AI example usages:===&lt;br /&gt;
&lt;br /&gt;
Here are several examples of goto [micro_ai] tag usages from the &amp;quot;Goto&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
* Send a unit toward a castle (similar to the RCA AI's Goto Candidate Action, but avoiding dead ends and areas ZoCed by enemies)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
             ca_id=messenger1&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=messenger1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=6-7,2-4&lt;br /&gt;
                 terrain=C*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             ca_score=210001&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send all units of the side toward the southern border of the map&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=8&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                  y=33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             avoid_enemies=1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send units to the four corners of the map (with each unit going to a different corner).&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x=1,1,44,44&lt;br /&gt;
                 y=1,33,1,33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             unique_goals=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Set up a guardian unit that doesn't move while no enemy unit is within 8 hexes (ironically, the Goto Micro AI can be used to keep units in place, although using one of the Guardian Micro AIs might often be a better idea)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=guard1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 [filter]&lt;br /&gt;
                     id=guard1&lt;br /&gt;
                 [/filter]&lt;br /&gt;
                 [not]&lt;br /&gt;
                     [filter]&lt;br /&gt;
                         side=1&lt;br /&gt;
                     [/filter]&lt;br /&gt;
                     radius=8&lt;br /&gt;
                 [/not]&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Guardian Micro AIs ==&lt;br /&gt;
=== General Comments on the Guardian Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Guardian Micro AIs can be used to set up different kind of guardian behaviors for a unit or a set of units.  Four different types of guardians are available and are described below.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different guardian behaviors, check out the &amp;quot;Guardians&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
=== Return Guardian  (ai_type=return_guardian)===&lt;br /&gt;
&lt;br /&gt;
A 'return guardian' is a variation of the standard Wesnoth guardian. It has an assigned guard position (GP) to which it returns after attacks on approaching enemies:&lt;br /&gt;
* If at GP with no enemy in reach, do nothing.&lt;br /&gt;
* If at GP with an enemy in reach, attack. Note that the attack is done by the default AI, which might result in the guardian not attacking if the enemy is deemed too strong (this can be influenced by setting the default ''aggression'' value to a different value).&lt;br /&gt;
* If not at GP, return there, no matter whether an enemy is in reach or not.&lt;br /&gt;
* If enemies are blocking the way back to GP, do your best to move around them.&lt;br /&gt;
* If the guardian ends up next to an enemy on the way back, attack that enemy after the move.&lt;br /&gt;
&lt;br /&gt;
Enable the Return Guardian Micro AI by using&lt;br /&gt;
 ai_type=return_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''return_x,return_y''': The coordinate of the hex the unit returns to after each attack.  {{DevFeature1.15|0}} '''return_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=100010: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  This AI consist of 2 CAs, one with score &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt; the other with &amp;lt;tt&amp;gt;ca_score-20&amp;lt;/tt&amp;gt;.  The default value is set so that the first CA gets executed right before the default combat CA, and the other right after.  It usually will not make sense to change this value, but it is possible to do so for custom applications.  {{DevFeature1.13|6}}: This is now changed to 100100 and 99900 in order to avoid conflicts with the new high_XP_attacks candidate action.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a return guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=return_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=return1&lt;br /&gt;
             return_x,return_y=20,2&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
===Stationed Guardian (ai_type=stationed_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A 'stationed guardian' is another variation of the standard Wesnoth guardian with a somewhat more complex behavior than that of the 'return guardian'. Two positions are defined for it, a 'station' and a 'guarded location', as well as a distance. The behavior is as follows:&lt;br /&gt;
* If no enemy is within 'distance' of the guard's current position, do nothing.  This is independent of the guardian being at its station or not.&lt;br /&gt;
* Otherwise: If an enemy is within 'distance' of the guard, but not also within the same distance of the guarded location and the station (all of this simultaneously), move the guard in the direction of the station (or keep the guard in place if the station is unreachable).&lt;br /&gt;
* Otherwise:&lt;br /&gt;
** Pick the enemy unit that is closest to the guarded location.&lt;br /&gt;
** If we can reach it, pick the adjacent hex with the highest defense rating and attack from there.&lt;br /&gt;
** If not in reach, move toward this unit.&lt;br /&gt;
&lt;br /&gt;
Enable the Stationed Guardian Micro AI by using&lt;br /&gt;
 ai_type=stationed_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance parameter as explained above&lt;br /&gt;
* '''station_x,station_y''': The x and y position of the hex that serves as the guardians station. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''guard_x, guard_y''': The x and y position of the guarded location.  {{DevFeature1.13|0}}  These are now optional keys, defaulting to station_x,station_y if not provided. {{DevFeature1.15|0}} '''guard_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a stationed guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=stationed_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=stationed1&lt;br /&gt;
             distance=4&lt;br /&gt;
             station_x,station_y=2,14&lt;br /&gt;
             guard_x,guard_y=3,13&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Coward (ai_type=coward) ===&lt;br /&gt;
&lt;br /&gt;
Cowards are units that, like guardians, sit around doing nothing until an enemy comes into range.  Unlike guardians, however, they run away once enemies approach. Applications might be wild animals, unarmed civilians getting in the way of a battle, etc.  The coward macro can be called with two optional locations, 'seek' and 'avoid':&lt;br /&gt;
* If neither is given, the coward retreats to the position farthest away from the approaching enemies.&lt;br /&gt;
* If 'seek' is given, it preferentially goes toward that location (but getting away from enemies takes priority).&lt;br /&gt;
* If 'avoid' is given, it in addition tries to avoid that location (with both maximizing distance from enemies and going toward 'seek' taking priority).&lt;br /&gt;
* Both 'seek' and 'avoid' may consist of only one coordinate ('x' or 'y'), in which case not a single hex, but a line of hexes is sought or avoided.&lt;br /&gt;
&lt;br /&gt;
Enable the Coward Micro AI by using&lt;br /&gt;
 ai_type=coward&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance within which an enemy must be to &amp;quot;scare&amp;quot; the unit&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_if_trapped'''='no':  {{DevFeature1.13|0}}  With the default settings, a coward never attacks.  If this key is set to 'yes', the coward attacks the weakest (lowest hitpoints) enemy next to which it ends up after moving (such as a cornered animal would do, blindly lashing out without regards for its own safety).&lt;br /&gt;
* '''avoid_x, avoid_y''': The x and y coordinate of the hex to avoid. {{DevFeature1.15|0}} '''avoid_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units from which the cowards flee.  If not set, this defaults to all enemy units.  Note, however, that no &amp;quot;enemy of AI side&amp;quot; condition is added if [filter_second] is specified, meaning that it can include allied and/or enemy units as desired (and even units on the same side as the coward).&lt;br /&gt;
* '''seek_x, seek_y''': The x and y coordinate of the hex to seek. {{DevFeature1.15|0}} '''seek_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a coward [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=coward&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=coward3&lt;br /&gt;
             distance=5&lt;br /&gt;
             seek_x,seek_y=24,5&lt;br /&gt;
             avoid_x,avoid_y=24,15&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Zone guardian (ai_type=zone_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A zone guardian is a unit that, as the name says, guards a zone. It moves randomly inside this zone until an enemy enters it (or a separately defined enemy zone, see below). Applications might be the defense of a castle, a nesting area or for protecting an unit.  The zone macro can be called with an optional SLF, [filter_location_enemy]:&lt;br /&gt;
* If not specified, the zone guard attacks any enemy coming inside its guard zone.&lt;br /&gt;
* If [filter_location_enemy] is given, it attacks any enemy entering this zone and once there are no more enemies, it goes back to patrol in its basic zone.&lt;br /&gt;
&lt;br /&gt;
Enable the Zone Guardian Micro AI by using&lt;br /&gt;
 ai_type=zone_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone to guard.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_enemy]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone where enemies have to be attacked.  If not given, it defaults to [filter_location].&lt;br /&gt;
* '''station_x,station_y''': By default, the zone guardian moves randomly within the zone if there is no enemy to attack.  However, if station_x/y is given, it moves to this position instead during turns when it does not have an enemy to attack.  This position does not need to be inside the zone defined above.  Note: obviously it does not make sense to define [filter_location], [filter_location_enemy] and station_x/y all at the same time.  Only [filter_location] and either one of the latter two (or none of them) should be provided for any zone guardian. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a zone guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=zone_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=zone3&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=22-31,4-11  # This is intentionally chosen to extend past the lake&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             station_x,station_y=32,8&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Hang Out Micro AI (ai_type=hang_out) ==&lt;br /&gt;
&lt;br /&gt;
The Hang Out Micro AI instructs units of the AI side to &amp;quot;hang out&amp;quot; around a certain location, or locations, until a condition for mobilizing the side is met.  If none of the optional keys are given, the AI controls all units of the side to hang around the leader while moving off castle tiles to make room for recruiting.&lt;br /&gt;
&lt;br /&gt;
The AI works by moving each unit to its best hex first using the minimum amount of movement points required for this move.  Once all AI units are in position, movement points are set to zero for all of them simultaneously such that they cannot be used by other AI candidate actions.  This is done because the mobilizing condition might be met as result of one of the moves of the Hang Out AI, after which we want units to be able to continue with other AI actions.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Hang Out and Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Hang Out Micro AI by using&lt;br /&gt;
 ai_type=hang_out&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Hang Out specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  In its default configuration, the Hang Out MAI controls all units of a side, keeping them as close to the side leader(s) as possible while moving off castle tiles to make room for recruiting.  For this purpose, the candidate action score defaults to 170,000, which is just below the score of the default recruiting CA.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''': A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  If neither of these two exists, this parameter is set to default to all castle terrain (&amp;lt;tt&amp;gt;terrain=C*,C*^*,*^C*&amp;lt;/tt&amp;gt;) so that units move off the castle to make room for recruiting.  Note that this does not include keep terrain, otherwise the leader would be moved off the keep as well.&lt;br /&gt;
* '''ca_score'''=170000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 170,000, which means that it is executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Hang Out action (&amp;lt;tt&amp;gt;side=&amp;lt;/tt&amp;gt; is always set and does not need to be provided).  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the locations ''near'' which the AI units should hang out.  This does not mean that the units only move onto terrain specified here, but that they minimize the distance to such terrain.  It defaults to the position of the side leader(s).&lt;br /&gt;
* '''[mobilize_condition]''':  Contains ([[ConditionalActionsWML#Condition_Tags|WML Condition Tags]])  Once the condition set up here has been met, the Hang Out MAI is disabled and the side goes over to other AI activity.  The Hang Out MAI remains disabled after this, even if the condition is not met any more later in the scenario.&lt;br /&gt;
* '''mobilize_on_gold_less_than''': (number) Since side gold cannot be checked in a WML condition tag without storing it in a variable first, this key is provided separately.  It disables the Hang Out Micro AI once the side gold goes below the value specified here for the first time.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
* The Hang Out AI still takes effect if no locations to hang out at are found or if all map locations are to be avoided.  In that case, it simply keeps all units at their current locations.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Hang Out Micro AI configuration from the &amp;quot;Hang Out and Messenger&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hang_out&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [avoid]&lt;br /&gt;
                 terrain=C*,H*,M*,A*,S*,*^F*&lt;br /&gt;
             [/avoid]&lt;br /&gt;
             [mobilize_condition]&lt;br /&gt;
                 [have_unit]&lt;br /&gt;
                     side=1&lt;br /&gt;
                     count=7-99&lt;br /&gt;
                 [/have_unit]&lt;br /&gt;
             [/mobilize_condition]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Healer Support Micro AI (ai_type=healer_support) ==&lt;br /&gt;
&lt;br /&gt;
The Healer Support Micro AI configures the healers of a side to stay behind the battle lines and heal injured and/or threatened units rather than participate in combat under all circumstances.  You can set different levels of aggressiveness for the healers.  &lt;br /&gt;
&lt;br /&gt;
'''Notes'''&lt;br /&gt;
* By default, ''all'' healers of the AI side are controlled by this AI.  This includes in particular also the side leader, if the leader is a healer.  If you do not want the leader to be included (and possibly be moved away from the keep etc.) use the [filter] tag with canrecruit=no (see below).&lt;br /&gt;
* It is not always better to use this Micro AI than the default AI, as it takes healers (which can be strong units) away from the pool of units to choose for attacks.&lt;br /&gt;
* {{DevFeature1.15|3}} This Micro AI now takes unit guardian status and the passive_leader aspect into account.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Healers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Healer Support Micro AI by using&lt;br /&gt;
 ai_type=healer_support&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Healer Support specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the healer units.  If set, only units on the AI side passing this filter ''and'' having the healing ability are considered by this Micro AI.&lt;br /&gt;
*'''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting units to receive healing. If set, only units on the AI side passing this filter ''and'' the other healee selection criteria will be selected for healing by this Micro AI.&lt;br /&gt;
* '''aggression'''=1.0: (float) Sets the aggressiveness of the AI.  This parameter is set up as a float to accommodate future functionality, but it currently acts as a boolean: if set to zero, the AI will never let its healers participate in combat, for all other values it allows them to attack after all other units have attacked and if the healers cannot find any more units to support.  The former behavior might be appropriate in scenarios with only one or few valuable healers, while the latter might work better in scenarios with many healers.&lt;br /&gt;
* '''injured_units_only'''=no: (boolean) If set to 'yes', the AI will only move healers next to units that are already injured and skip units that currently have full hitpoints, but might get injured during the next enemy turn.&lt;br /&gt;
* '''max_threats'''=9999: (integer) The maximum allowable number of enemies that can attack a hex in order for it to be considered for a healer.  As an example, setting 'max_threats=0' means that the AI only moves healers to locations that are entirely safe from the enemy (assuming that none of the units currently on the map dies).  Note that the value of this key is checked against the number of enemies that can make it to the hex, not the number of adjacent hexes from which the healer could be attacked.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI. Note: as attacks by the healers are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The Healer Support Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded.  It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Healer Support Micro AI configuration from the &amp;quot;Healers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             aggression=0&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Lurkers Micro AI (ai_type=lurkers) ==&lt;br /&gt;
&lt;br /&gt;
The Lurker Micro AI controls some or all units of a side in &amp;quot;lurker mode&amp;quot;.  A lurker is a unit that is capable of moving across most terrains, but that only stops on and attacks from specific terrain.  It might also have the ability to hide on this terrain (which is the reason why it is called a lurker).&lt;br /&gt;
&lt;br /&gt;
Lurkers move individually without strategy and always attack the weakest enemy within their reach.  If no enemy is in reach, the lurker does a random move instead - or it just sits and waits (lurks).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of different types of lurker behavior, check out the &amp;quot;Lurkers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Lurkers Micro AI by using&lt;br /&gt;
 ai_type=lurkers&lt;br /&gt;
in the [micro_ai] tag&lt;br /&gt;
&lt;br /&gt;
===Lurkers specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
* '''[filter]''': Specifies which units of the side are controlled by the Lurker Micro AI.  This is a [[StandardUnitFilter|Standard Unit Filter]].  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': Specifies the terrain from which the lurkers attack. This is a [[StandardLocationFilter|Standard Location Filter]].&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_wander]''': Specifies the terrain on which lurkers end their moves if there is no enemy in reach. This is a [[StandardLocationFilter|Standard Location Filter]].  If [filter_location_wander] is not given, it defaults to [filter_location].&lt;br /&gt;
* '''stationary'''=no: (boolean)  If set to yes, a lurker never moves if there is no enemy within attack range.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Lurkers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=4&lt;br /&gt;
             ai_type=lurkers&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Naga Fighter&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=W*,S*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             [filter_location_wander]&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location_wander]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Messenger Escort Micro AI (ai_type=messenger_escort) ==&lt;br /&gt;
&lt;br /&gt;
The Messenger Escort Micro AI lets you define a set of waypoints through which it tries to move one or several of its units, the messenger(s).  The other units escort the messenger, trying to protect it and attacking enemies that are in its way.  Note that a messenger might not strictly hit intermediate waypoints, but that getting close is good enough, especially if a waypoint is occupied by an enemy unit.  It does, however, always try to get to the last waypoint.&lt;br /&gt;
&lt;br /&gt;
'''Notes on the use of several messengers:''' {{DevFeature1.13|0}}  &lt;br /&gt;
* This Micro AI can control several messengers at the same time, which can but do not have to move in a group.  They can be put on the map altogether in one location, or be spread out on the map or across different turns.&lt;br /&gt;
* The farther a messenger has moved through the chain of waypoints, the higher a priority it is for the AI, meaning that farther advanced (on the map) messengers are moved first, and that the escort units try to clear the way and stay with those messengers more so than with those further back on the map.  This order can be inverted using the invert_order= key.&lt;br /&gt;
* If several messengers are on the map, escort units might move from one to the other, the AI making a judgement call where they are needed most.  Closeness to a messenger is generally the most important criterion, but not the only one.&lt;br /&gt;
* If a messenger dies, the escort units previously staying with it now join one of the other messengers if there are any left (otherwise they follow default AI behavior).&lt;br /&gt;
* If you want certain escort units to stay always with the same messenger, use several messenger [micro_ai] tags with different [filter]/[filter_second] combinations.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Messenger Escort Micro AI by using&lt;br /&gt;
 ai_type=messenger_escort&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Messenger Escort specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': {{DevFeature1.13|0}} (Previous to 1.13.0, only id= was allowed.) The id of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated list of waypoint coordinates through which the messenger will go in the given order to reach its goal, given by the last coordinate.  If you want the messenger to go directly to the goal, simply enter a single x,y coordinate here.  Note that the messenger does not have to hit each waypoint exactly (except for the final one), getting close to them is good enough. {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|11}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Messenger Escort AI consists of 3 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-2&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_death_chance'''=0.67 and '''messenger_death_chance'''=0.0: (both floats; ranging from 0 to 1)    If the messenger is adjacent to an enemy at the end of the move, it will only attack the enemy if the chance of death of the enemy is &amp;gt;= enemy_death_chance and if the messenger's chance of death is &amp;lt;= messenger_death_chance (or if it is an attack at a range for which the enemy does not have a weapon).&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|0}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the escort units.  If not set, this defaults to all enemy units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''invert_order'''=no: {{DevFeature1.13|0}} If set to 'yes', the order of priority with which messenger units are considered is inverted, starting with the messenger unit that is the least advanced on the map (see description above).  In most cases, selecting this option results in worse performance of the AI, so use it with caution.  (And obviously it has no effect at all if there is only one messenger controlled by the AI.)&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|11}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Messenger Escort&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=messenger_escort&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=messenger&lt;br /&gt;
             waypoint_x=31,24,27,28&lt;br /&gt;
             waypoint_y=20,14,7,1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Patrol Micro AI (ai_type=patrol) ==&lt;br /&gt;
&lt;br /&gt;
The defines a number of waypoints for a unit (or units) to follow.  Options can be set whether the route should be followed in a loop, as an out-and back, only once, whether units encountered along the way should be attacked, etc.&lt;br /&gt;
&lt;br /&gt;
With the default settings, getting to the next waypoint takes priority over attacking for a patroller, but this can be overridden (as of '''Wesnoth 1.15.12''', using the '''attack_range''' option).  Unless instructed otherwise, the AI thus prefers to move the patrol unit around enemies rather than straight for them.  Also, if a waypoint is occupied by a unit the AI is not instructed to attack, it will (eventually) abandon that waypoint once it gets close enough and move on to the next one.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different patrol behaviors, check out the &amp;quot;Patrols&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Patrol Micro AI it by using&lt;br /&gt;
 ai_type=patrol&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Patrol specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated lists of the waypoint coordinates through which the patrol travels (in order). {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack''': By default (if this key is not set), the patrol unit attacks any unit it ends up next to after a move, or that is within '''attack_range''' (see below).  Alternatively, this parameter can be set to a comma-separated list of unit ids. If that is done, the patrol only attacks these units when it ends up next to them or if it gets within attack_range.  It ignores any other unit.  This can in turn be used to have the patroller not attack at all, by setting ''attack='' to a non-existing id.  The exception to this is an enemy on the last waypoint with ''one_time_only=yes'', which will always be attacked.&lt;br /&gt;
* '''attack_range'''=1: {{DevFeature1.15|12}} If the patrol comes within this many hexes of an enemy unit, it interrupts its patrol route and attacks that enemy (assuming it still has enough movement left to get to the enemy).  The '''attack=''' instructions are taken into account for this.  Note that the patrol generally tries to move around enemies, which means that for the default value of 1, enemies are usually not within attack range, except at the end of the move.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''one_time_only'''=no: (boolean)  If set to 'yes', the unit goes through the patrol route only once and then hangs out at the last waypoint.  It also always attacks any enemy unit on the last waypoint, independent of what ''attack='' is set to.&lt;br /&gt;
* '''out_and_back'''=no: (boolean)  By default, a patrol unit returns to the first waypoint after reaching the last, doing the patrol route as a loop.  If this parameter is set to 'yes', the unit instead reverses its course and perpetually does out and backs of the route.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Patrols&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=patrol&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Konrad&lt;br /&gt;
             waypoint_x=9,24,25&lt;br /&gt;
             waypoint_y=21,23,15&lt;br /&gt;
             one_time_only=yes&lt;br /&gt;
             attack=Gertburt&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Protect Unit Micro AI (ai_type=protect_unit) ==&lt;br /&gt;
&lt;br /&gt;
The Protect Unit Micro AI tries to move one or several units to a given location while keeping them safe from enemy attacks. It does so by keeping the protected units away from enemy units and close to friendly units. Other units on the AI side are controlled by the default Wesnoth AI.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Protect Unit&amp;quot; and &amp;quot;The Elves Besieged&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Protect Unit Micro AI by using&lt;br /&gt;
 ai_type=protect_unit&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Protect Unit specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[unit]''': A separate [unit] tag is required for each protected unit(s).  Each tag has required keys '''id''', '''goal_x''', '''goal_y''' for each unit, as shown in the example below. {{DevFeature1.15|0}} '''goal_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''disable_move_leader_to_keep'''=no: (boolean) If set to 'yes', side leaders protected by this AI do not try to return to their keep.  This is necessary for this Micro AI to work with units that are side leaders and that are supposed to move to an off-keep location.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
*The Protect Unit Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].  Depending on the parameters, it might also modify the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|move leader to keep candidate action]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded. It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;HttT: The Elves Besieged&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=protect_unit&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Delfador&lt;br /&gt;
                 goal_x,goal_y=1,2&lt;br /&gt;
             [/unit]&lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
                 goal_x,goal_y=1,1&lt;br /&gt;
             [/unit]&lt;br /&gt;
 &lt;br /&gt;
             disable_move_leader_to_keep=true&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Recruiting Micro AI==&lt;br /&gt;
=== General Comments on the Recruiting Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Recruiting Micro AI replaces the default recruitment AI by alternative recruiting patterns.  Currently there are two recruiting algorithms you can choose from, random recruiting and the rush recruiting used in the new (as of Version 1.11.1) Experimental AI (see below).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of a different recruiting pattern, check out the &amp;quot;Recruiting&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  You can also explore the unit choices by watching the Experimental AI in a multiplayer game.&lt;br /&gt;
&lt;br /&gt;
'''Note:''' Obviously, the Recruitment Micro AI modifies the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|recruitment candidate action]].&lt;br /&gt;
&lt;br /&gt;
=== Random Recruitment AI (ai_type=recruit_random) ===&lt;br /&gt;
&lt;br /&gt;
This AI randomly selects a unit type from the recruitment list of the side.  The probability of choosing each type and the behavior in low-gold situations can be influenced with optional parameters.&lt;br /&gt;
&lt;br /&gt;
Enable the Random Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_random&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* '''[probability]''': This tag sets the probability of selecting the given unit type.  It has two required sub-keys:&lt;br /&gt;
** '''type''': comma-separated list of unit types to which this probability is to be applied&lt;br /&gt;
** '''probability''': (non-negative float)  The probability with which these unit types is to be selected.  All unit types for which no [probability] tag is defined receive a default probability of 1.0, thus values should be chosen relative to that.  As an example, in the code below swordsmen and peasants are given a probability of 8, while no other units have assigned probabilities.  Thus, each of these two unit types is 8 times more likely to be selected than any other individual unit type.&lt;br /&gt;
* '''skip_low_gold_recruiting'''=no: (boolean)  By default, the random recruitment AI chooses a unit to recruit only from those types for which it has sufficient gold.  If this parameter is set to 'yes', it will instead choose from all available unit types and end recruiting for the turn if there is not enough gold to recruit it this turn (even if other, affordable unit types exist).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a random recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_random&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [probability]&lt;br /&gt;
                 type=Swordsman,Peasant&lt;br /&gt;
                 probability=8&lt;br /&gt;
             [/probability]&lt;br /&gt;
 &lt;br /&gt;
             skip_low_gold_recruiting=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Rush Recruitment AI (ai_type=recruit_rushers) ===&lt;br /&gt;
&lt;br /&gt;
This AI replaces the default recruiting with the rush recruiting used in the new (as of Version 1.11.1) Experimental AI.&lt;br /&gt;
&lt;br /&gt;
Enable the Rush Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_rushers&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* {{DevFeature1.15|2}} '''high_level_fraction'''=0: (non-negative number) The approximate fraction of units of level 2 or higher to be recruited. This is defined as fraction of units on the map, not of new units being recruited during a turn or over several turns (which makes a difference if there are already units on the map). The effect is also cumulative per level (starting from level 2), meaning that 1/3 of L2 units will be recruited in the example above, 1/3*1/3=1/9 L3s etc. The default value is zero, which leaves the high-level unit recruiting to the default Experimental AI algorithm resulting usually in very few such units being recruited.&lt;br /&gt;
* '''randomness'''=0.1: (number)  A random number is applied to the rusher recruit engine's score to prevent the recruitment pattern from being too predictable. 0 causes no randomness to be applied, while larger numbers increase the random effect. A value of 1-2 generates results in which the random effect is approximately equal to the scored effect. Extremely high values are essentially entirely random.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a rush recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_rushers&lt;br /&gt;
             action=add&lt;br /&gt;
          [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Simple Attack Micro AI (ai_type=simple_attack) ==&lt;br /&gt;
&lt;br /&gt;
The Simple Attack Micro AI is meant as an addition to the default combat candidate action for when one wants to force the AI to do attacks that it would otherwise not do (for example on units that are 1 XP from leveling) or to execute certain attacks before all others (for example, attack with all &amp;quot;expendable&amp;quot; units first).  It is not set up to be a replacement for the default attacks.  Some notes:&lt;br /&gt;
&lt;br /&gt;
* The algorithm is different from and simpler than that of the default combat CA.  For example, no attack combinations are considered, only individual attacks.  As a result, the [[AiWML#List_of_AI_Aspects|default aspects]] (aggression, caution, [avoid], ...) are not taken into account.  Also, if lots of units are involved, evaluation of this Micro AI is potentially quite a bit slower than of the default attacks.&lt;br /&gt;
&lt;br /&gt;
* If an attack matching the Standard Unit Filters described below is found, this attack will always be executed, no matter how bad the odds are.  Thus, this Micro AI can be used to force the AI to do attacks it would otherwise avoid.&lt;br /&gt;
&lt;br /&gt;
* Other than selecting the attacker/defender units, this AI is currently not configurable.  Let us know if you would like to see certain options to be added.  (No guarantees that we will add them, but we will definitely consider them.)&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Simple Attack&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=simple_attack&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Simple Attack specific keys for the [micro_ai] tag===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  It is, however, in general not desirable to use this AI without at least one of the optional Standard Unit Filters below, as this AI is not meant as a replacement of the default combat candidate action.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=110000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 110,000, which means that it is executed right before the default combat candidate action.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Simple Attack action.  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units the AI should attack.  It defaults to all enemy units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
* '''weapon'''=-1: (integer) The number of the weapon that should be used for this attack.  Weapon numbers start at 1 (not 0).  -1 stands for &amp;quot;best weapon&amp;quot; (by whatever criterion the AI chooses to use).   '''Available from Wesnoth 1.11.8'''&lt;br /&gt;
&lt;br /&gt;
Below are 2 examples of Simple Attack Micro AI configurations from the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  The first one shows how to force the AI to attack with all Soulless and Walking Corpse units first:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110000&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless,Walking Corpse&lt;br /&gt;
             [/filter]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
The second example executes attacks by Soulless on all enemy units that are 1 XP from leveling.  Note that the Lua code in the preload event is there simply in order to code the &amp;quot;1 XP from advancing&amp;quot; condition.  It is not needed for most other SUFs, as can be seen in the previous example.&lt;br /&gt;
     [event]&lt;br /&gt;
         name=preload&lt;br /&gt;
         first_time_only=no&lt;br /&gt;
         [lua]&lt;br /&gt;
             code=&amp;lt;&amp;lt;&lt;br /&gt;
                 function close_to_advancing(unit)&lt;br /&gt;
                     if (unit.experience &amp;gt;= unit.max_experience-1) then&lt;br /&gt;
                         return true&lt;br /&gt;
                     else&lt;br /&gt;
                         return false&lt;br /&gt;
                     end&lt;br /&gt;
                 end&lt;br /&gt;
             &amp;gt;&amp;gt;&lt;br /&gt;
         [/lua]&lt;br /&gt;
     [/event]&lt;br /&gt;
 &lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110001&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless  # No Walking Corpses; L0 units don't advance enemy&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 lua_function = &amp;quot;close_to_advancing&amp;quot;&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
Several Simple Attack Micro AIs can be combined for the same AI side.  The two examples above are both used (note the different values for ca_score) in the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
== Custom Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|5}}&lt;br /&gt;
&lt;br /&gt;
From 1.13.5 onwards, adding a new Micro AI can be easily done without altering any core files or overriding the micro_ai tag. The [micro_ai] tag now looks up its ''ai_type'' parameter in the &amp;lt;code&amp;gt;wesnoth.micro_ais&amp;lt;/code&amp;gt; table and uses the micro AI setup function found there to determine which keys are required and allowed and what candidate actions to add.&lt;br /&gt;
&lt;br /&gt;
A micro AI setup function takes as its single parameter the contents of the [micro_ai] tag and returns three separate values:&lt;br /&gt;
*'''required''': A list of required keys and tags, as strings. Tags are indicated by enclosing them within square brackets in the string. If a key or tag in this list is missing from the [micro_ai] tag, an error will be printed.&lt;br /&gt;
*'''optional''': Similar to above, a list of optional keys and tags, as strings. No error will be printed if a key or tag in this list is missing, nor will an error be printed if it is present (as would be the case for unrecognized keys or tags).&lt;br /&gt;
*'''ca_params''': A table specifying the candidate actions that constitute the micro AI. It should contain an ''ai_id'' key (which specifies a base ID to be used for candidate actions of this micro AI) and a series of candidate action definitions. Each candidate action definition is a table containing the following keys:&lt;br /&gt;
**'''ca_id''': The ID used for the candidate action. This will be combined with the ''ai_id'' to produce the final ID.&lt;br /&gt;
**'''location''': Path to the Lua file that defines the candidate action.&lt;br /&gt;
**'''score''': The maximum score for the candidate action. If the [micro_ai] tag specified a ''ca_score'' parameter, the score here should normally be based on that.&lt;br /&gt;
&lt;br /&gt;
(The return statement would thus look like &amp;lt;syntaxhighlight lang='lua' inline&amp;gt;return required, optional, ca_params&amp;lt;/syntaxhighlight&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
'''Examples''' of micro AI setup functions can be found in the [https://github.com/wesnoth/wesnoth/tree/1.14/data/ai/micro_ais/mai-defs default MAI definitions directory].  See the [https://github.com/wesnoth/wesnoth/blob/1.14/data/ai/micro_ais/mai-defs/animals.lua#L4-L12 Big Animals MAI definition] for a simple example.  For custom micro AIs, the setup code should be put into a ''[lua]'' tag inside a preload event.&lt;br /&gt;
&lt;br /&gt;
Since the function has full access to the contents of the [micro_ai] tag, it can conditionally add candidate actions to '''ca_params''' depending on the values of other keys within the [micro_ai] tag. It can also perform other actions such as adding facets to an aspect; if it does such things, it should pay attention to the ''action'' key to determine whether it's being added or removed.&lt;br /&gt;
&lt;br /&gt;
There are two useful helper functions that can be used by Micro AI setup functions requiring aspect changes. They can be brought into scope by a line similar to the following:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='lua&amp;gt;local MAIH = wesnoth.require(&amp;quot;ai/micro_ais/micro_ai_helper.lua&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* MAIH.add_aspects(side, aspect_params)&lt;br /&gt;
* MAIH.delete_aspects(side, aspect_params)&lt;br /&gt;
&lt;br /&gt;
The ''aspect_params'' parameter is an array of tables, each of which contains the following keys:&lt;br /&gt;
*'''aspect''': The aspect to change.&lt;br /&gt;
*'''facet''': The WML defining the facet. This must include an ''id'' key in order for the aspect to be correctly deleted. For maximum compatibility, incorporate the ''ca_id'' key from the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=67259</id>
		<title>Micro AIs</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=67259"/>
		<updated>2021-03-20T23:08:11Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Patrol Micro AI (ai_type=patrol) */ add attack_range parameter&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== A Few General Words about Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
* Micro AIs allow Wesnoth campaign and scenario authors to add new AI functionality without the need for any AI programming. They bring new specialized behavior to Wesnoth that cannot be achieved, at least not easily, by adjusting default AI parameters (such as those described at [[AiWML]]) or with WML code.&lt;br /&gt;
&lt;br /&gt;
* A Micro AI is activated and configured (or deleted) via the [micro_ai] tag, requiring only a few lines of WML code.&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.6''', the Micro AI behavior was inconsistent when it came to '''hidden enemies and petrified units'''.  Some Micro AIs ignored those, while others did not.  In some case there was even a potential for AI errors when an AI unit ran into hidden or petrified enemies.  As of 1.13.6, this has all been fixed.  The Micro AIs now correctly and consistently ignore hidden units and treat petrified units as blocking the hex they are on but having no effect otherwise, in the same way as the default AI does.  '''Important notes:'''&lt;br /&gt;
** It is not possible to re-include hidden/petrified units via the Standard Unit Filters (SUFs) used by some of the Micro AIs. In fact, if this is attempted, the filters will generally not match any unit.&lt;br /&gt;
*** This only applies to SUFs which go directly into the [micro_ai] tag. Any SUF that is a sub-tag of another filter (for example, of a Standard Location Filter) is taken literally and returns all units matching the filter. If hidden or petrified units are to be excluded from such a sub-filter, that needs to be specified directly in that filter.&lt;br /&gt;
** We also note that, because all the Micro AIs are written in Lua, it is possible to [[Micro_AIs#Using_Development_.281.13.29_Version_Micro_AIs_in_your_Stable_.281.12.29_Version_Add-on|move a Micro AI's code into one's add-on]] and modify it to whatever scenario-specific behavior is desired (this does not only apply to hidden/petrified units, but to all behavior). Setting up general Micro AI behavior that works in every possible setting is not feasible, but adapting it to work (only) in the specific setting of a given scenario is often quite simple. If you do not know how to do this, please post a message on the Wesnoth forums and we will be happy to help you with it.&lt;br /&gt;
&lt;br /&gt;
* The '''[[AiWML#List_of_AI_Aspects|default AI aspects]] are, for the most part, not taken into account by the Micro AIs'''.  For example, unless noted otherwise, specifying a value for ''aggression'' does not change the MAI behavior, and locations specified in an ''[avoid]'' tag generally can be entered by units controlled by an MAI.  This is a feature of the Micro AIs, not a bug.  If you feel that a certain default AI aspect should also apply to a Micro AI, please let us know in the [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 Micro AIs Feedback thread].&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.7''', the Micro AIs did not all work reliably when the '''AI side is under shroud''' and it may have been necessary to turn shroud off for the side when using a Micro AI. {{DevFeature1.13|7}} The Micro AIs now all work for sides under shroud. They do this by disregarding shroud for terrain purposes when path finding, consistent with default Wesnoth AI behavior, while still ignoring hidden units (including those hidden under shroud) correctly.&lt;br /&gt;
&lt;br /&gt;
* Most Micro AIs in their default setting apply to all units of a side, although many of them provide parameters for filtering the units that should be affected by the new behavior.  If the Micro AI is applied only to part of the units of a side, the other units follow the default Wesnoth AI behavior.&lt;br /&gt;
&lt;br /&gt;
* Micro AIs are available in mainline since Wesnoth 1.11/1.12.&lt;br /&gt;
&lt;br /&gt;
== Micro AI Test and Demo Scenarios ==&lt;br /&gt;
&lt;br /&gt;
All Micro AIs described here can be checked out in a number of test and demo scenarios.  The easiest way to access the test scenarios is by assigning a hotkey to &amp;quot;Start Test Scenario&amp;quot; in the preferences (as of Wesnoth 1.13.8). Then, simply press the hotkey in the titlescreen and select the desired scenario from the list (e.g. 'micro_ai_test' for the overall &amp;quot;switchboard scenario&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Test scenarios can also be accessed from the command line by typing&lt;br /&gt;
 path/wesnoth-executable -t micro_ai_test&lt;br /&gt;
or (depending on your system and Wesnoth version)&lt;br /&gt;
 path/wesnoth-executable -tmicro_ai_test&lt;br /&gt;
This starts up a &amp;quot;switchboard scenario&amp;quot; in which you can select the Micro AI demonstration scenario you want to check out.  Here, ''path/wesnoth-executable'' needs to be replaced by whatever the path and filename of the Wesnoth executable is on your system.&lt;br /&gt;
&lt;br /&gt;
== Setting up a Micro AI ==&lt;br /&gt;
&lt;br /&gt;
Setting up a Micro AI requires only one simple step:&lt;br /&gt;
==== Activating and configuring (or deleting) a Micro AI ====&lt;br /&gt;
&lt;br /&gt;
Micro AIs are activated, deleted and configured using the [micro_ai] tag.  This tag needs to be placed in [[ActionWML]], that is, in an event, a menu option or the like.  As an example, the following code activates the healer_support Micro AI in its default configuration for Side 2 from the beginning of the scenario:&lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
 &lt;br /&gt;
         # Configure the healer support micro AI&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
'''Keys required for all Micro AIs:'''&lt;br /&gt;
All [micro_ai] tags must contain the following three required keys:&lt;br /&gt;
* '''side''': The side for which the Micro AI is to be added, changed or deleted.  This has to be an individual side number, lists of sides are not supported.&lt;br /&gt;
* '''ai_type''': The type of Micro AI to be added, changed or deleted.  See the following sections for allowed values.&lt;br /&gt;
* '''action''' (string): The action to take concerning the Micro AI.  The following values are allowed: &lt;br /&gt;
** ''add'': Add a Micro AI to a side.&lt;br /&gt;
** ''change'': Change the configuration of an existing Micro AI.  Note that this does not only change the specific parameters provided in the tag, but it replaces the entire existing Micro AI by a new version with the new (and only the new) configuration.  It is therefore equivalent to using first the ''delete'' and then the ''add'' action.&lt;br /&gt;
** ''delete'': Delete an existing Micro AI from a side.&lt;br /&gt;
&lt;br /&gt;
'''Keys required under certain circumstances:'''&lt;br /&gt;
* '''ca_id''': (string) If several MAIs of the same type are used for the same side and one or several of these are to be changed or deleted during a scenario, this key needs to be included so that the [micro_ai] tag knows which of them needs to be changed/deleted.  The same is also true if a Micro AI is changed more than once during a scenario.  We therefore '''strongly recommend always setting ''ca_id''''' for MAIs which are, or might be, changed or deleted during a scenario.&lt;br /&gt;
** '''Note:''' The id(s)/name(s) actually assigned to the CA(s) of the MAI by the engine might be different from the parameter passed here.  It is, however, sufficient to use this ca_id with the [micro_ai] tag add/delete/change actions to identify the MAI uniquely.  (In other words: you don't have to worry about this if you always use the [micro_ai] tag, but might have to figure out the actual name(s) of the CA(s) if you want to delete or change them manually.)&lt;br /&gt;
* Other keys may also be required depending on the Micro AI.  In additional, a number of optional keys may be available to configure the AI behavior.  See the sections on the individual Micro AIs for details.&lt;br /&gt;
&lt;br /&gt;
'''Required keys for deleting a Micro AI:'''&lt;br /&gt;
&lt;br /&gt;
Only a subset of the keys required for setting up a Micro AI are needed when deleting it.  '''side''', '''ai_type''' and '''action'''=delete always need to be provided.  '''ca_id''' needs to be given if it was provided when adding the Micro AI (see above for when this is needed).&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* The ''add'' and ''change'' actions ignore all keys that do not apply to the respective Micro AI type.  The ''delete'' action ignores all keys other than the required keys listed above.&lt;br /&gt;
&lt;br /&gt;
* Some of the Micro AIs use and/or modify default AI components (candidate actions or aspects) when they are added, or reset these components to their default values when they are deleted.  Thus, if you have modified the AI yourself using the default CAs or aspects, some of these modifications might be affected, or might be interfering with the Micro AI.  The sections below indicate whether a Micro AI modifies any default AI components.&lt;br /&gt;
&lt;br /&gt;
===Combining Different Micro AI Types for the Same Side===&lt;br /&gt;
&lt;br /&gt;
No extra steps need to be taken when combining different Micro AIs on the same side.  Simply set up the [micro_ai] tags for all AIs you want to use.&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* While it is ''technically'' possible to combine any number of Micro AIs, this does not mean that all Micro AIs work together in practice.  Some are not compatible because they control units in contradictory ways.  &lt;br /&gt;
* Not all combinations that ''should'' work have been tested yet.  If you find a combination that you believe should work but doesn't, [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 please let us know] and we will see if that can be fixed.&lt;br /&gt;
* The candidate action score almost certainly has to be set (using the ca_score= key) for one or several of the MAIs if several of them are used on the same side.&lt;br /&gt;
&lt;br /&gt;
== Animals Micro AIs ==&lt;br /&gt;
=== General Comments on the Animal Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Animals Micro AIs are meant to simulate the behaviors of certain types of animals.  They are, however, set up so that they can be used with arbitrary unit types and might be applicable in quite diverse situations.  The AIs nevertheless remain named after the animals for which they were written originally, so that they are (to some extent) descriptive of their behavior.&lt;br /&gt;
&lt;br /&gt;
Brief descriptions of the AI behaviors are given in the following subsections.  To get a better feeling for their behavior, check out the &amp;quot;Animals&amp;quot;, &amp;quot;Swarm&amp;quot;, &amp;quot;Wolves&amp;quot; and &amp;quot;Dragon&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  Note that some of the animal AIs are designed for animal types that do not exist in mainline.  In the test scenario, other animal types are therefore substituted for them.&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Wolves (ai_type=wolves) ===&lt;br /&gt;
&lt;br /&gt;
The Wolves Micro AI organizes all &amp;quot;wolf&amp;quot; units (predators) of the side to hunt in a single pack.  They actively chase after the closest prey and try to corner it (not always super successfully), but are easily distracted by other enemy units coming into attack range, to the point where the pack might split up.  The wolves can, however, be told to attack only prey units as well as to try to avoid other types of units (such as larger predators).  When no prey is left, the wolves wander randomly.&lt;br /&gt;
&lt;br /&gt;
This AI works best for a small number of predators (2 - 5) and sparse prey.  Also, the predators should be put on the map in a pack, that is, reasonably close to each other.  For a demonstration of the Wolves AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Note that another wolves AI exists, called [[Micro_AIs#Animals_AI:_Multi-pack_Wolves_.28ai_type.3Dwolves_multipacks.29|Multipack Wolves]], which distributes the wolves of a side into several packs, rather than all of them into one single pack as it is done here.&lt;br /&gt;
&lt;br /&gt;
Enable the Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the predator units.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the prey units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_only_prey'''=no: In the default configuration, the wolves attack all enemy units in attack range.  If this key is set to 'yes', they only attack prey units.&lt;br /&gt;
* '''avoid_type''': Comma-separated list of unit types (on enemy sides) which the wolves try to avoid.  They try to stay out of these units' way as much as possible when moving around (except when they are moving in to attack another enemy unit) and never attack them.&lt;br /&gt;
* '''ca_score'''=90000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 90,000, which is lower than the default combat CA, but higher than most other default CAs which move units.  It can be changed by setting this parameter but in most cases this will not make sense.  Note that the Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Notes on default (RCA) AI aspects:''' &lt;br /&gt;
* If ''attack_only_prey'' or ''avoid_type'' are set, the Wolves Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* Attacks by the wolves are left to the default (RCA) AI, meaning that the value of ''aggression'' has an influence on the behavior.&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI is taken into account. Note: as attacks by the wolves are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=6&lt;br /&gt;
             ai_type=wolves&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Wolf&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Vampire Bat&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             avoid_type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Multi-pack Wolves (ai_type=wolves_multipacks) ===&lt;br /&gt;
&lt;br /&gt;
The Multi-pack Wolves Micro AI is different from the [[Micro_AIs#Animals_AI:_Wolves_.28ai_type.3Dwolves.29|Wolves Micro AI]] in that there can be an arbitrary number of wolf packs, with the pack size being a free parameter.  At the beginning of the scenario, close wolves are grouped into packs in a semi-methodical way.  If the wolves are put on the map in distinct groups of close wolves of the correct number, they will be joined into packs matching these groups.  If, on the other hand, they are spread out all over the map, the method of assigning them to packs is semi-random.&lt;br /&gt;
&lt;br /&gt;
Wolves of the same pack begin by joining each other on the map.  After that, they stay together until only one wolf is left, which then tries to join up with an incomplete pack or with other single wolves.  Individual wolves entering the map during the scenario behave in that way as well.&lt;br /&gt;
&lt;br /&gt;
A second difference to the other Wolves AI is that wolves do not actively hunt here.  For the most part they just wander (often long distance).  However, the pack ferociously (and without regard for its own health) attacks any enemy units that come into range, as long as that does not mean separating the pack by more than a few hexes.  Staying together, or joining with a new wolf assigned to the pack, is the only thing that takes priority over satisfying the wolves' thirst for blood.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Multi-pack Wolves AI behavior, check out the &amp;quot;Wolves&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Multi-pack Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves_multipacks&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Multi-pack Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''pack_size'''=3: (integer) The size of the packs.&lt;br /&gt;
* '''show_pack_number'''=no: If set to &amp;quot;yes&amp;quot;, the wolves' pack numbers will be shown below them.  Note that this means that the pack number of a wolf killed in an attack will remain as a label on the map.  &lt;br /&gt;
* '''type'''=Wolf: Comma-separated list of the unit types which the AI controls. The default value is &amp;quot;Wolf&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves_multipacks [micro_ai] tag usage from the &amp;quot;Wolves&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=wolves_multipacks&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             show_pack_number=yes&lt;br /&gt;
             pack_size=4&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Big Animals (ai_type=big_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Big Animals Micro AI is a simulation of large predators that wander and hunt alone, such as Bears, Giant Spiders or Yetis.  For the most part, these just wander on the terrain that's been defined for them, but they attack enemy units if those happen to come into range.  The AI can be set up so that the Big Animals stay out of the way of other units (such as other large predators), in which case they only attack those if cornered or if they run into them accidentally (such as ambushers).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Big Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Big Animals Micro AI by using&lt;br /&gt;
 ai_type=big_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the animals to which this AI is applied.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': The big animals often go for long distance (multi-turn) journeys.  This tag is a [[StandardLocationFilter|Standard Location Filter]] describing the terrain from which the next goal is chosen.  The default is all terrain on the map. &lt;br /&gt;
* '''[filter_location_wander]''': The terrain on which the big animals will end their moves. This is a [[StandardLocationFilter|Standard Location Filter]].  The default is all terrain on the map.&lt;br /&gt;
* '''[avoid_unit]''': A [[StandardUnitFilter|Standard Unit Filter]] describing all enemy units that the big animals will avoid.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
Here's an example of big_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=big_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Ghast&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [avoid_unit]&lt;br /&gt;
                 type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
             [/avoid_unit]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=1-40,1-18&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Forest Animals (ai_type=forest_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Forest Animals Micro AI simulates a number of different animal behaviors that one might find in a forest, namely animals behaving like deer, rabbits and families of tuskers.  In the following, we will refer to those animals by these types, even though the actual unit types can be selected freely.  They behave as follows:&lt;br /&gt;
* Each deer wanders randomly on (selectable) terrain, except when enemies get in its (the deer's) maximum movement range, in which case it flees to the farthest point it can reach.&lt;br /&gt;
* Tuskers exhibit the same behavior as deer, except when an enemy is next to one of the tusklets.  This enemy is attacked unconditionally by the tuskers trying to protect their young.&lt;br /&gt;
* Tusklets blindly follow the closest adult tusker, except when there is no tusker left, in which case they behave the same as deer.&lt;br /&gt;
* Rabbits also wander randomly, but in addition disappear into their holes (if any are within reach) when enemies are close.  They reappear out of their holes at the beginning of the turn, if it is safe.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Forest Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Forest Animals Micro AI by using&lt;br /&gt;
 ai_type=forest_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Forest Animals AI consists of 4 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-3&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''rabbit_type, tusker_type, tusklet_type, deer_type''': Comma-separated lists of the unit types behaving as the respective animals.  If any of these parameters are not set, those types of animal behavior are skipped by the AI.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain on which the forest animals wander when they are not threatened by enemies.&lt;br /&gt;
* '''rabbit_number'''=6: (integer)  The number of rabbits for the AI to have on the map simultaneously.  The AI puts the missing number of rabbits onto the map at the beginning of the AI's turn (when the rabbits come out of their holes).&lt;br /&gt;
* '''rabbit_enemy_distance'''=3: (number)  Rabbits won't spawn in holes that aren't more than this distance away from all enemies.&lt;br /&gt;
* '''rabbit_hole_img''': Rabbit holes are marked by items on the map.  If this parameter is set, it must be the file name of the item image.  If it is not set, all items on the map are treated as rabbit holes.&lt;br /&gt;
** {{DevFeature1.14|10}} Items used for rabbit holes on the map border used to produce an error. Placing them on the map border therefore needed to be avoided on the scenario setup side. They are now automatically excluded from consideration by the AI code.&lt;br /&gt;
&lt;br /&gt;
Here's an example of forest_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=forest_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             deer_type=Vampire Bat&lt;br /&gt;
             rabbit_type=Giant Rat&lt;br /&gt;
             tusker_type=Ogre&lt;br /&gt;
             tusklet_type=Young Ogre&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=*^F*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Herd and Herders (ai_type=herding) ===&lt;br /&gt;
&lt;br /&gt;
The Herding Micro AI sets up an area on the map where a number of herder units (such as sheep dog) control and protect a herd (such as sheep).  For convenience, we simply refer to them as sheep dogs and sheep in the following, even though arbitrary unit types can be selected for either.&lt;br /&gt;
&lt;br /&gt;
Sheep dogs try to keep their sheep safe.  This involves keeping them inside the herding area, positioning themselves in between the sheep and approaching enemies, and attacking the enemies if those get too close.  If no active herding or protecting move is needed, the dogs go to a random location on the perimeter of the herding area.&lt;br /&gt;
&lt;br /&gt;
Sheep wander aimlessly except when a sheep dog is next to them, in which case they run away from the dog.  The dogs exploit this by positioning themselves on the outside of the sheep, if possible.  Sheep also run away from approaching enemies.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Herding AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Herding Micro AI by using&lt;br /&gt;
 ai_type=herding&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herder units (e.g. dogs).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herd units (e.g. sheep).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain forming the perimeter of the herding area.  In other words, this is the path the herders (dogs) use to patrol around the outside of the herding area.  This must be a set of coordinates ''completely enclosing'' the herding area.  Here's an example of setting up a rectangular perimeter:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         x=10-20,10-20,10,  ,20&lt;br /&gt;
         y=11,   21,   11-21,11-21&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
Another possibility is by defining the path by certain terrain on the map (as it is done in the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]), in which case the filter can simply look like this:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         terrain=Rb&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
* '''herd_x,herd_y''': The coordinate of one hex (''any'' one hex) inside the herding area.  This is needed to define which part of the map is on the ''inside'' of the herding area perimeter defined by [herding_perimeter], and which is the ''outside''. {{DevFeature1.15|0}} '''herd_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''attention_distance'''=8: (integer)  If an enemy is within this distance of a herd unit, but not as close as ''attack_distance'', the herders try to position themselves in between the enemy and the herd unit.&lt;br /&gt;
* '''attack_distance'''=4: (integer)  If an enemy is within this distance of a herd animal, the herders attack this enemy.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Herding AI consists of 6 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-5&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Herding [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=herding&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Footpad&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Troll Whelp&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             herd_x,herd_y=32,28&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=Rb&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Hunter (ai_type=hunter) ===&lt;br /&gt;
&lt;br /&gt;
The Hunter Micro AI leads a unit (or units) through hunt-and-rest cycles.  The units wander in a prescribed area of the map, their hunting ground, until an enemy unit comes within range.  Note that the units do not actively chase down enemies, but wander randomly through the hunting ground until an enemy is encountered.&lt;br /&gt;
&lt;br /&gt;
If enemies are within range of a hunter unit, the hunter attacks the weakest of them.  If a kill is made, it then retreats to its rest location, where it stays for a certain number of turns and until fully healed (whatever happens later).&lt;br /&gt;
&lt;br /&gt;
A few more details:&lt;br /&gt;
* If the hunter's way home is entirely blocked on the return (so that there is no possible path), the normal RCA AI takes over its behavior.&lt;br /&gt;
* However, if the way is blocked by an enemy unit occupying the rest hex, that enemy will be attacked unconditionally.&lt;br /&gt;
* A kill only makes the hunter go home when it is the attacker, not as defender.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Hunter AI behavior, check out the &amp;quot;Dragon&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]. &lt;br /&gt;
&lt;br /&gt;
Enable the Hunter Micro AI by using&lt;br /&gt;
 ai_type=hunter&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''home_x,home_y''': The coordinates of the hex to which the hunter returns and rests at after a kill. {{DevFeature1.15|0}} '''home_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the unit's hunting ground.  If this tag is not provided, the hunter uses all of the map.&lt;br /&gt;
* '''rest_turns'''=3: (integer)  The number of turns the AI stays at home_x,home_y after a kill.  It will not leave its home until this number of turns have passed ''and'' it is fully healed.&lt;br /&gt;
* '''show_messages'''=no: (boolean) If set to yes, the hunter announces whenever its behavior changes to the next phase.  Note that this is meant as debug output for the scenario author.  The messages are not translated and are not included in replays (and are only visible to the host in an MP game).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a hunter [micro_ai] tag usage from the &amp;quot;Dragon&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hunter&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Rowck&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=5-30,1-15&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             home_x,home_y=3,15&lt;br /&gt;
             rest_turns=2&lt;br /&gt;
 &lt;br /&gt;
             show_messages=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Swarm (ai_type=swarm) ===&lt;br /&gt;
&lt;br /&gt;
The Swarm Micro AI uses some very simply algorithms to simulate animal swarm behavior.  Without adjacent enemies, they simply do a random move, trying to stay together and at a certain distance from enemies.  However, if an enemy unit is close to any bat, the swarm scatters.  This is particular fun to watch when one places an enemy unit in the middle of the swarm.  After being scattered, the swarm members slowly rejoin, but not in a very organized way.  Sub-swarms or individual bats might roam around for quite some time before they find their way back.  It is also possible that individual bats (or small groups) split off from the larger swarm at times.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Swarm AI behavior, check out the &amp;quot;Swarm&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Swarm Micro AI by using&lt;br /&gt;
 ai_type=swarm&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Swarm AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_distance'''=5: (int) The minimum distance kept between units of the swarm and enemies when the swarm moves as a whole.&lt;br /&gt;
* '''scatter_distance'''=3: (int) Enemies within &amp;quot;scatter_distance&amp;quot; hexes of any swarm unit cause the swarm to scatter, by each unit trying to maximize its individual distance from the enemy.&lt;br /&gt;
* '''vision_distance'''=12: (int) Only units within this distance follow the overall swarm motion (either away from an enemy or of the swarm as a whole).  The smaller this value is set, the less likely the swarm is to stay together or rejoin.  This parameter is meant to simulate how far an individual member of the swarm can see, meaning that the swarm is &amp;quot;out of sight&amp;quot; (and the unit will not be able to follow it) if it is more than this distance away.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Swarm [micro_ai] tag usage from the &amp;quot;Swarm&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=swarm&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Assassin Squad Micro AI (ai_type=assassin) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|6}}&lt;br /&gt;
&lt;br /&gt;
This Micro AI controls one or several assassin units which try to circle around enemy units with the goal of hitting a target unit without being distracted or impeded by other enemies.  The units will always attack the target if such attacks are possible, otherwise they will follow the path of least resistance toward it. Notes:&lt;br /&gt;
&lt;br /&gt;
* The main priority for the AI is to find paths that avoid enemy units. However, if no such path can be found, the assassins will still advance toward the target, taking the path exposing the units to the least potential damage from enemies.&lt;br /&gt;
&lt;br /&gt;
* The units will not necessarily follow the same path. The best path is chosen individually for each unit.&lt;br /&gt;
&lt;br /&gt;
* While it is not a requirement for the AI to work, using ambusher or skirmisher units, or units with the quick trait, makes this AI more lethal in many cases.&lt;br /&gt;
&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded, it does not work otherwise. That means that '''ca_score''' is not a valid key for the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=assassin&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Assassin Squad specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the assassin units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the target unit.&lt;br /&gt;
** It is important to realize that the AI currently only works correctly with a single target unit. It is thus the responsibility of the scenario designer to make sure that this filter matches an individual enemy unit. If several enemy units are to be targeted in the same scenario, several instances of the Assassin Micro AI can be used.&lt;br /&gt;
** The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[prefer]''': A [[StandardLocationFilter|Standard Location Filter]] selecting locations on the map for the assassins to prefer when making their way toward the target unit. This could be used, for example, to make units with the ambush ability move (mostly) through forest or to have the assassins circle around the, say, northern part of the map rather than the south if that is desirable for scenario design purposes.&lt;br /&gt;
** As with avoiding enemies, the units will still move across non-preferred terrain if that is either the only option or ''much'' shorter than preferred terrain&lt;br /&gt;
** Attacking, once the target unit is in range, is not influenced by the [prefer] SLF.&lt;br /&gt;
&lt;br /&gt;
Here is an example of an Assassin Squad [micro_ai] tag usage:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=assassin&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Assassin,Master at Arms,Duelist&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             [prefer] # Circle around northern part of map&lt;br /&gt;
                 x=1-9, 10-99&lt;br /&gt;
                 y=1-37, 1-17&lt;br /&gt;
             [/prefer]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Bottleneck Defense Micro AI (ai_type=bottleneck_defense) ==&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense Micro AI lets you define a location on the map where the AI can take a defensive stance behind a narrow passage (bottleneck).  Units on the front line are backed up by healers and/or units with the leadership ability.  Injured units are moved to the back and replaced by uninjured (or less injured) units.  The units on the front line only attack when it is safe (no retaliation) or when there is a high chance that they will make a kill or level up.  Using this Micro AI only makes sense if there is no way for the enemy sides to move around the bottleneck and attack the AI's units from behind.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Bottleneck&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Bottleneck Defense Micro AI by using&lt;br /&gt;
 ai_type=bottleneck_defense&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Bottleneck Defense specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense AI requires two sets of coordinates that define where it should be taking up its defensive stance, and from which side the enemy attacks at that location.&lt;br /&gt;
&lt;br /&gt;
* '''x,y''': Comma separated lists of the hexes on the front line, where strong units are placed.  All hexes on which the AI makes contact with (can be attacked by) the enemy need to be included here.  Units are placed on them in the order in which they are listed here. {{DevFeature1.15|0}} '''location_id''' can be used instead with named/special locations.&lt;br /&gt;
* '''enemy_x,enemy_y''': Comma separated list of the hexes from which the enemy can attack the AI front line units.  This is needed for the AI to know on which side of the front line support units (healers etc.) need to be placed.  In many cases, it is sufficient to provide only one of the enemy front line hexes, but there are geometries for which that does not work.  It is therefore safer to list all enemy front line hexes here. {{DevFeature1.15|0}} '''enemy_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
By default, the AI places healers and units with the leadership ability both on the front line itself (if they are the strongest units available) and on hexes adjacent to and behind the line.  If different placement is desired, these locations can be overridden with the following keys.&lt;br /&gt;
&lt;br /&gt;
* '''active_side_leader'''=no: (boolean)  If set to 'yes', the side leader participates in the bottleneck defense action after the side's gold has been spent.  If set to 'no' (default), the leader follows default AI behavior (sitting on the keep and recruiting, for the most part).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Bottleneck Defense AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.15|3}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the units.  Only AI units matching this filter participate in the bottleneck defense.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''healer_x,healer_y''': Comma separated list of hexes where healers are placed.  This can be used to keep healers from the front line (or to take up only certain positions on the front line), and/or to override the default healing positions behind the line.  Healers are placed on them in the order in which the hexes are listed, with the exception that hexes on the line always take priority over hexes behind the line. {{DevFeature1.15|0}} '''healer_loc''' can be used instead with named/special locations.&lt;br /&gt;
* '''leadership_x,leadership_y''': Same as ''healer_x,healer_y'', but for units with the leadership ability. {{DevFeature1.15|0}} '''leadership_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
As an example, in the &amp;quot;Bottleneck&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]], there are three front line hexes, two of which are on hill terrain, while the third is a road with a lower defense rating.  The healer and side leader are supposed to participate in combat because they are strong units, but only from the hill hexes to keep them safer.  This is accomplished by using the following keys settings (check out the scenario to see which hex is which):&lt;br /&gt;
 [micro_ai]&lt;br /&gt;
     side=1&lt;br /&gt;
     ai_type=bottleneck_defense&lt;br /&gt;
     action=add&lt;br /&gt;
 &lt;br /&gt;
     x=14,14,14&lt;br /&gt;
     y= 7, 9, 8&lt;br /&gt;
     enemy_x=13,13&lt;br /&gt;
     enemy_y= 8, 9&lt;br /&gt;
 &lt;br /&gt;
     healer_x=14,14,15,15&lt;br /&gt;
     healer_y= 7, 9, 8, 9&lt;br /&gt;
     leadership_x=14,14,15,15&lt;br /&gt;
     leadership_y= 7, 9, 9 ,8&lt;br /&gt;
     active_side_leader=yes&lt;br /&gt;
 [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Fast Micro AI (ai_type=fast_ai) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|0}}&lt;br /&gt;
&lt;br /&gt;
This AI is meant as a fall-back AI for scenarios where there are so many units that attacks and moves done by the default AI are slow with long delays before each move.  It replaces the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions (CAs).  It should be obvious that in order to achieve the faster speed, compromises concerning the quality of the moves have to be made.&lt;br /&gt;
&lt;br /&gt;
Enable the Fast Micro AI by using&lt;br /&gt;
 ai_type=fast_ai&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Combat candidate action:'''&lt;br /&gt;
* Attackers are considered individually one at a time.  The best attack found for a unit is executed without checking whether a better attack by another unit might be possible.&lt;br /&gt;
* The default order in which attackers are considered is by decreasing hitpoints, starting with the strongest unit (in terms of HP).  This can be modified with the optional keys below.&lt;br /&gt;
* By default, if weaker units block attack hexes for a stronger unit, they do not move out of the way (this is the same as for the default AI).  If a hex becomes available after an attack (e.g. due to a weaker unit dying in the attack), all attacks that were possible ''before'' this happened are finished first.  Only after that are attacks by the stronger unit reconsidered.&lt;br /&gt;
* If include_occupied_attack_hexes=yes is set, weaker units move out of the way for stronger units if possible.  This can, however slow down the AI depending on the situation on the map.  Use with caution.&lt;br /&gt;
* {{DevFeature1.13|2}} Previous to this version, attacks by the leader were bunched in with the attacks by the other units.  Now leader attacks are treated separately and contain an extra layer of analysis to determine whether the attack position is &amp;quot;safe&amp;quot; (in an approximate way).  Currently, this is done with two simple considerations:&lt;br /&gt;
** Count all enemy units that can reach the leader's attack position.  If there are more than given by optional parameter 'leader_attack_max_units' (default value 3), do not attack.&lt;br /&gt;
** Add up the hitpoints of all enemies that can reach the leader's attack position.  If this value, multiplied by optional parameter 'leader_weight'(default value 2), is larger than the leaders hitpoints, do not attack.&lt;br /&gt;
** Two optional parameters, 'threatened_leader_fights' and 'leader_additional_threat', can also be used to let the leader attack when the threat at the attack hex is comparable to that at the leader's current position.&lt;br /&gt;
** In order to keep this AI as fast as possible, leader attacks are done by a separate candidate action that is only evaluated when all other CAs are done.&lt;br /&gt;
&lt;br /&gt;
'''Move-to-targets, villages and retreat candidate actions:'''&lt;br /&gt;
* The move-to-targets, villages and retreat candidate actions of the default AI are removed.  Move-to-targets and villages are taken over by the Fast Micro AI, whereas retreat is simply not done.&lt;br /&gt;
* This CA moves units toward goals after attacks are done.  Goals are unowned (if the AI side has a leader only) and enemy-owned villages and enemy leaders.&lt;br /&gt;
* For each goal, units are pre-sorted in order of the fewest moves needed to cover the ''distance'' toward the goal (that is, as if move cost on each hex toward the goal were 1).  Units are then considered sequentially and if a unit can get there &amp;quot;efficiently&amp;quot;, its move is executed without considering other units.&lt;br /&gt;
** A move is considered efficient if the move cost to get to the hex is not more than 'move_cost_factor' larger than the distance to the hex.  'move_cost_factor' is an optional key that defaults to 2.  It can be set to values of 1.1 or larger (see below).&lt;br /&gt;
** If the move for the current unit is not found to be efficient, the AI considers the next unit instead.  If no unit can do an efficient move toward the goal, the best (fastest) of the inefficient moves is selected instead.&lt;br /&gt;
&lt;br /&gt;
* Village goals:&lt;br /&gt;
** Village goals are considered before enemy leader goals so that the AI preferentially chooses scouts (or otherwise fast units) for them.&lt;br /&gt;
** The number of units sent toward villages depends on the value of the default AI aspect village_value.  It is set to village_value/4 times the number of units the AI has left to move.  Thus, by default (village_value=1.0) the AI sends a quarter of its units toward villages, and the rest toward enemy leaders.  This means that the meaningful range of values for village_value is 0..4.&lt;br /&gt;
** Only one unit is moved toward each village.  This consideration also overrides the number of units to move toward villages defined by village_value, topping it at the number of unowned/enemy-owned villages on the map.&lt;br /&gt;
** Setting village_value=0 disables moving units toward villages altogether.  That is, not only are no units chosen, but the village targeting phase of the AI is entirely disabled with no evaluation time used for it.  Note that, unlike with the default AI, this also means that no villages within range of an AI unit are grabbed.&lt;br /&gt;
** The order in which villages are chosen as goals is some combination of them being close to the AI side's leader and being spread out over the map.&lt;br /&gt;
&lt;br /&gt;
* Enemy leader goals:&lt;br /&gt;
** The order of enemy leaders goals is set according to their distance from the AI leader with the closest enemy leader being targeted first (if the AI has a leader, otherwise their order is random).  One unit is sent toward each enemy leader in this order, after which the AI starts with the first enemy leader again.  This is repeated until all units have moved.&lt;br /&gt;
&lt;br /&gt;
* Guardians (units with ai_special=guardian set) are ignored by the fast move-to-targets CA.&lt;br /&gt;
&lt;br /&gt;
'''Tips for speeding up/modifying the Fast Micro AI in specific scenarios:'''&lt;br /&gt;
&lt;br /&gt;
A number of optional keys and default AI aspects can be used to modify the behavior of the Fast Micro AI.  The MAI is set up so that it is a reasonable compromise between speed and quality of the moves in most situations.  However, this might not be ideal for all scenarios here are some things that can be done to speed up the AI (or to get better behavior in some situations by doing the opposite):&lt;br /&gt;
* Use large values for the default AI aspect aggression.  The largest delays before attacks happen (in some circumstances) when the Fast MAI has to consider lots of units with attacks left but decides not to attack with them.  See below for a description of how aggression is used by the fast AI.&lt;br /&gt;
* Use large values of move_cost_factor (see above for description of what this does).  If you want to emphasize units that can move across terrain easily (e.g. bats) to be chosen for village grabbing, you might want to use low values of move_cost_factor, but this might slow down the AI.  The default value should work reasonably well in most standard situations, but modifying the value might work better on specific maps.&lt;br /&gt;
* Don't use dungeon_mode=yes unless absolutely necessary, even in most dungeon crawls.  Test the AI without this setting first and only use it if it is the only way to solve the problem.  Often, a minor tweak to the map will have the same effect, without the speed penalty.&lt;br /&gt;
* Use include_occupied_attack_hexes=yes with caution.  It might (or might not) slow down the attack CA significantly, depending on the scenario.&lt;br /&gt;
* Set village_value=0, which makes the AI not consider villages as goals.  This has not been found to make a large difference on the maps tested so far, but it might have an effect on maps with a very large number of villages.&lt;br /&gt;
* Use attack_hidden_enemies=yes on maps where there are no hidden enemies. Excluding hidden enemies from the attack calculation requires an additional filter to be evaluated that takes extra time. In most cases this will not result in a large computation time saving.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_hidden_enemies'''=no: (boolean)  By default, hidden enemies are excluded from the attack calculation.  Setting this to 'yes' makes the AI also consider those enemies as attack targets.&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.13|2}} A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  As with the default AI, units will not step on hexes defined by the [avoid] tag.  In addition, no goals will be chosen that lie on those hexes.  The one minor exception is that a unit stepping out of the way for another unit might temporarily enter the avoided zone.&lt;br /&gt;
* '''dungeon_mode'''=no: (boolean)  As much as possible, the Fast Micro AI works with distances between hexes rather than path finding.  This works pretty well on open maps and even most dungeon crawls (and similar scenarios), but it might result in some units getting temporarily (and in rare cases permanently) stuck on maps with convoluted labyrinths.  If that is the case, setting dungeon_mode=yes triggers more path finding that avoids such situations (but as a result is somewhat slower).&lt;br /&gt;
* '''include_occupied_attack_hexes'''=no: (boolean) If set to 'yes', the AI moves weaker units out of the way for a stronger attacker.  This might lead to better attacks but is slower, so don't use if speed is essential.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the attackers.  Only AI units matching this filter will attack, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes the side= key set to the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the enemies.  Only enemy units matching this filter will be attacked, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''leader_weight'''=2: (number &amp;gt;= 0) {{DevFeature1.13|2}} A factor specifying how much more valuable the AI considers side leaders than non-leader units in the attack evaluation. This is also used to determine whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''leader_additional_threat'''=1: (number &amp;gt; 0) {{DevFeature1.13|2}} If threatened_leader_fights=yes, the threat at the attack position may be larger by this factor for the leader to leave the keep for an attack.&lt;br /&gt;
* '''leader_attack_max_units'''=3: (number &amp;gt;= 0) {{DevFeature1.13|2}} A number specifying how many enemy units within reach are acceptable when determining whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''move_cost_factor'''=2: (number &amp;gt;= 1.1)  The factor by which the move cost to a goal hex may be larger than the distance to the goal hex for a move to be considered &amp;quot;efficient&amp;quot;.  See description above.  1.1 is used if this is set to values smaller than 1.1.  Smaller values might lead to better behavior, while larger values might speed up the AI.&lt;br /&gt;
* '''weak_units_first'''=no: (boolean) If set to 'yes', inverts the order in which units are considered by the combat CA.  Attacks are then done in order of increasing hitpoints of the attackers.&lt;br /&gt;
* '''skip_combat_ca'''=no: (boolean) If set to 'yes', the fast combat CA is not used.  The default AI's high_xp_attack and combat CAs remains in place in this case.&lt;br /&gt;
* '''skip_move_ca'''=no: (boolean) If set to 'yes', the fast move-to-targets CA is not used.  The default AI's retreat, villages and move-to-targets CAs remains in place in this case.&lt;br /&gt;
* '''threatened_leader_fights'''=no: (boolean) {{DevFeature1.13|2}} By default, the AI leader will not attack if the threat at the attack position is too high.  If this key is set to 'yes', the leader will attack if the threat there is not larger than the threat at his current position multiplied by the value of 'leader_additional_threat'. This is meant as an option for the endgame: when the AI leader is under attack, he might as well go down fighting.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''aggression''': The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to set how aggressive the AI should attack.  It works in roughly the same way as the default AI aspect:&lt;br /&gt;
** The value of (1 - aggression) determines how much the AI values its own units compared to the enemy's units.  Thus, aggression=0 means that attacks are only done if the AI expects to deal (roughly) the same amount of damage as it will receive.  aggression=1 means that expected damage to the AI's units is not considered, only the expected damage to enemy units matters.&lt;br /&gt;
** Damage is calculated as a combination of hitpoints and chance to die, in &amp;quot;gold equivalent units&amp;quot;.&lt;br /&gt;
** Unlike the default AI, aggression is set to 1 for values larger than 1.  Allowed values are thus anything from -infinity to +infinity, although only -infinity to +1.0 make sense.&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.13|2}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
*'''attacks aspect''':  {{DevFeature1.13|2}} The standard RCA AI [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] used to filter own and enemy units for attacks is now also taken into account as long as neither [filter] nor [filter_second] (see above under 'optional keys') are defined in the [micro_ai] tag.&lt;br /&gt;
*'''village_value=1''': (number ranging from 0 to 4) The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to define how much villages should be targeted by the move-to-targets CA.  In the Fast MAI, this defines which fraction of the AI units are moved toward village goals (see above for details).  If set to 0, villages are ignored as goals.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded to the same values as those of the respective [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|default candidate actions]].  ca_score is therefore not a valid optional key.&lt;br /&gt;
* The fast Micro AI modifies (removes) the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions.&lt;br /&gt;
&lt;br /&gt;
Enabling the Fast Micro AI in its default configuration is as simple as this usage example from the &amp;quot;Fast AI&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=fast_ai&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Generalized Goto Micro AI (ai_type=goto) ==&lt;br /&gt;
&lt;br /&gt;
The Goto Micro AI provides a highly configurable method of sending a unit (or units) to a location or set of locations.  The units to be moved are defined using a [[StandardUnitFilter|Standard Unit Filter]] (SUF) [alternatively, all side units are used if the SUF is not provided), while the goto locations are given in a [[StandardLocationFilter|Standard Location Filter]] (SLF).&lt;br /&gt;
&lt;br /&gt;
By default, each unit matching the SUF will move toward the closest goal (in terms of turns needed to get there) matching the SLF via the fastest route given the current situation on the map (locations of enemy units etc.), but this behavior can be influenced by the optional keys listed below.  Note that this means that a unit's goal might change from one turn to the next depending on the situation on the map.&lt;br /&gt;
&lt;br /&gt;
The behavior of the unit(s) once they reach their goal location(s) is configurable as well.  By default, they will stay there until the Goto Micro AI is deleted or changed.&lt;br /&gt;
&lt;br /&gt;
For demonstrations of several Goto Micro AI usages, check out the &amp;quot;Goto&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Goto Micro AI by using&lt;br /&gt;
 ai_type=goto&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Goto specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the possible goal locations for the move.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists). Note that the area defined by [avoid] is not entered at all, even if the end point of the move would lie outside the area again.&lt;br /&gt;
* '''avoid_enemies''': (number&amp;gt;0) By default, units controlled by the Goto Micro AI will not take enemy units into account for route finding, other than the enemies' ZoCs.  By contrast, if this key is set, they will try to avoid traversing hexes on which they can be attacked by enemies.  The larger the value of ''avoid_enemies'', the more the units will try to stay out of the enemies' way.  Notes:&lt;br /&gt;
** Units are willing to take a path that is 'avoid_enemies' movement points longer for each enemy unit that can be avoided by doing so for each hex along the path.  As an example, if avoid_enemies=1 and the shortest path crosses 2 hexes that can be reached by 3 enemies each, the unit is willing to take a path that is up to 6 hexes longer instead.  If avoid_enemies=10 in the same situation, the unit will take a path up to 60 hexes longer.&lt;br /&gt;
** Hexes adjacent to enemy units are always strongly avoided, independent of the value of avoid_enemies.&lt;br /&gt;
** avoid_enemies is ignored if 'use_straight_line=true' is set.&lt;br /&gt;
** Due to an issue with the path finding code, '''tunnels and teleports are ignored in Wesnoth 1.14 and before''' if 'avoid_enemies' is used. This has been fixed in Wesnoth 1.15.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units to be moved.  Defaults to all units of the side if not defined.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''ignore_enemy_at_goal'''=no and '''ignore_units'''=no: (boolean)  In the default configuration, the Goto MAI does not move units if the path to all goal hexes is blocked by enemies.  It is therefore possible to disable the Goto MAI by putting enemy units on all the goal hexes.  That can be changed if either of these two keys is set to 'yes'.&lt;br /&gt;
** 'ignore_enemy_at_goal=yes' makes the AI choose its goals while disregarding any enemy units at the goal hexes.  Even so, a stiff rating penalty is applied to goal hexes occupied by enemies, thus that unoccupied goal hexes are chosen unless all possible goals are occupied by enemies.  Note that this setting ''only'' ignores enemy units at the goal hexes themselves.  It is still possible to disable the AI by putting enemies all around the goals.&lt;br /&gt;
** 'ignore_units=yes' let's the AI ignore ''all'' enemy units when deciding which goal hex(es) to move toward.&lt;br /&gt;
** In both cases, enemies are only ignored for deciding which unit to move toward which goal hex.  For the actual move itself, all enemies and their ZoCs are considered as usual.&lt;br /&gt;
** Both options only take effect if neither avoid_enemies is used, nor use_straight_line is set to 'yes', as neither of these consider enemies as blockers. Or in other words, if avoid_enemies is used, or use_straight_line=yes, it is unnecessary to set ignore_enemy_at_goal or ignore_units to yes.&lt;br /&gt;
* '''release_unit_at_goal'''=no: (boolean) By default, a unit that reaches a goal location will stay there until the Goto Micro AI is deleted or changed (or until the conditions specified in the [goto_goals] SLF change).  If this parameter is set, a unit that has reached its goal will not be controlled by this Micro AI any more starting from the next turn.&lt;br /&gt;
* '''release_all_units_at_goal'''=no: (boolean) Same as ''release_unit_at_goal'', but this will deactivate the Goto Micro AI for all units once the first unit reaches a goal.&lt;br /&gt;
* '''remove_movement'''=yes: (boolean):  {{DevFeature1.15|11}}  By default, when the Goto MAI cannot move a unit (for example, because the path is blocked by enemies), its movement points are still removed. If this option is set to 'no', this step does not happen, so that other AI candidate actions (or even another Goto MAI with different parameters) can take over instead.&lt;br /&gt;
* '''unique_goals'''=no: (boolean)  If set to 'yes', each unit will be sent to a different goal location.  Note that the best goals are recalculated each turn, so a unit might choose a different goal this turn from what it was heading for during the previous turn if the situation on the map has changed.  Also note that if there are more units than goal locations, the left over units will not be handled by the Goto MAI but follow default behavior instead.&lt;br /&gt;
* '''use_straight_line'''=no: (boolean)  By default, units choose the route by which they will get to their goal locations in the smallest number of turns.  If this parameter is set to 'yes', a straight line route (or something pretty close to it at least) will be chosen instead, irrespective of whether this leads into a dead end, whether it is blocked by enemies, etc.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* Depending on the parameters used, the Goto Micro AI might require a lot of path finding to be done.  Thus, while it is possible to set up this AI to move many units to many different locations, a usage like that might require quite a long calculation time.&lt;br /&gt;
&lt;br /&gt;
===Goto Micro AI example usages:===&lt;br /&gt;
&lt;br /&gt;
Here are several examples of goto [micro_ai] tag usages from the &amp;quot;Goto&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
* Send a unit toward a castle (similar to the RCA AI's Goto Candidate Action, but avoiding dead ends and areas ZoCed by enemies)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
             ca_id=messenger1&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=messenger1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=6-7,2-4&lt;br /&gt;
                 terrain=C*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             ca_score=210001&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send all units of the side toward the southern border of the map&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=8&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                  y=33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             avoid_enemies=1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send units to the four corners of the map (with each unit going to a different corner).&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x=1,1,44,44&lt;br /&gt;
                 y=1,33,1,33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             unique_goals=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Set up a guardian unit that doesn't move while no enemy unit is within 8 hexes (ironically, the Goto Micro AI can be used to keep units in place, although using one of the Guardian Micro AIs might often be a better idea)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=guard1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 [filter]&lt;br /&gt;
                     id=guard1&lt;br /&gt;
                 [/filter]&lt;br /&gt;
                 [not]&lt;br /&gt;
                     [filter]&lt;br /&gt;
                         side=1&lt;br /&gt;
                     [/filter]&lt;br /&gt;
                     radius=8&lt;br /&gt;
                 [/not]&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Guardian Micro AIs ==&lt;br /&gt;
=== General Comments on the Guardian Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Guardian Micro AIs can be used to set up different kind of guardian behaviors for a unit or a set of units.  Four different types of guardians are available and are described below.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different guardian behaviors, check out the &amp;quot;Guardians&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
=== Return Guardian  (ai_type=return_guardian)===&lt;br /&gt;
&lt;br /&gt;
A 'return guardian' is a variation of the standard Wesnoth guardian. It has an assigned guard position (GP) to which it returns after attacks on approaching enemies:&lt;br /&gt;
* If at GP with no enemy in reach, do nothing.&lt;br /&gt;
* If at GP with an enemy in reach, attack. Note that the attack is done by the default AI, which might result in the guardian not attacking if the enemy is deemed too strong (this can be influenced by setting the default ''aggression'' value to a different value).&lt;br /&gt;
* If not at GP, return there, no matter whether an enemy is in reach or not.&lt;br /&gt;
* If enemies are blocking the way back to GP, do your best to move around them.&lt;br /&gt;
* If the guardian ends up next to an enemy on the way back, attack that enemy after the move.&lt;br /&gt;
&lt;br /&gt;
Enable the Return Guardian Micro AI by using&lt;br /&gt;
 ai_type=return_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''return_x,return_y''': The coordinate of the hex the unit returns to after each attack.  {{DevFeature1.15|0}} '''return_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=100010: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  This AI consist of 2 CAs, one with score &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt; the other with &amp;lt;tt&amp;gt;ca_score-20&amp;lt;/tt&amp;gt;.  The default value is set so that the first CA gets executed right before the default combat CA, and the other right after.  It usually will not make sense to change this value, but it is possible to do so for custom applications.  {{DevFeature1.13|6}}: This is now changed to 100100 and 99900 in order to avoid conflicts with the new high_XP_attacks candidate action.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a return guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=return_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=return1&lt;br /&gt;
             return_x,return_y=20,2&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
===Stationed Guardian (ai_type=stationed_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A 'stationed guardian' is another variation of the standard Wesnoth guardian with a somewhat more complex behavior than that of the 'return guardian'. Two positions are defined for it, a 'station' and a 'guarded location', as well as a distance. The behavior is as follows:&lt;br /&gt;
* If no enemy is within 'distance' of the guard's current position, do nothing.  This is independent of the guardian being at its station or not.&lt;br /&gt;
* Otherwise: If an enemy is within 'distance' of the guard, but not also within the same distance of the guarded location and the station (all of this simultaneously), move the guard in the direction of the station (or keep the guard in place if the station is unreachable).&lt;br /&gt;
* Otherwise:&lt;br /&gt;
** Pick the enemy unit that is closest to the guarded location.&lt;br /&gt;
** If we can reach it, pick the adjacent hex with the highest defense rating and attack from there.&lt;br /&gt;
** If not in reach, move toward this unit.&lt;br /&gt;
&lt;br /&gt;
Enable the Stationed Guardian Micro AI by using&lt;br /&gt;
 ai_type=stationed_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance parameter as explained above&lt;br /&gt;
* '''station_x,station_y''': The x and y position of the hex that serves as the guardians station. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''guard_x, guard_y''': The x and y position of the guarded location.  {{DevFeature1.13|0}}  These are now optional keys, defaulting to station_x,station_y if not provided. {{DevFeature1.15|0}} '''guard_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a stationed guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=stationed_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=stationed1&lt;br /&gt;
             distance=4&lt;br /&gt;
             station_x,station_y=2,14&lt;br /&gt;
             guard_x,guard_y=3,13&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Coward (ai_type=coward) ===&lt;br /&gt;
&lt;br /&gt;
Cowards are units that, like guardians, sit around doing nothing until an enemy comes into range.  Unlike guardians, however, they run away once enemies approach. Applications might be wild animals, unarmed civilians getting in the way of a battle, etc.  The coward macro can be called with two optional locations, 'seek' and 'avoid':&lt;br /&gt;
* If neither is given, the coward retreats to the position farthest away from the approaching enemies.&lt;br /&gt;
* If 'seek' is given, it preferentially goes toward that location (but getting away from enemies takes priority).&lt;br /&gt;
* If 'avoid' is given, it in addition tries to avoid that location (with both maximizing distance from enemies and going toward 'seek' taking priority).&lt;br /&gt;
* Both 'seek' and 'avoid' may consist of only one coordinate ('x' or 'y'), in which case not a single hex, but a line of hexes is sought or avoided.&lt;br /&gt;
&lt;br /&gt;
Enable the Coward Micro AI by using&lt;br /&gt;
 ai_type=coward&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance within which an enemy must be to &amp;quot;scare&amp;quot; the unit&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_if_trapped'''='no':  {{DevFeature1.13|0}}  With the default settings, a coward never attacks.  If this key is set to 'yes', the coward attacks the weakest (lowest hitpoints) enemy next to which it ends up after moving (such as a cornered animal would do, blindly lashing out without regards for its own safety).&lt;br /&gt;
* '''avoid_x, avoid_y''': The x and y coordinate of the hex to avoid. {{DevFeature1.15|0}} '''avoid_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units from which the cowards flee.  If not set, this defaults to all enemy units.  Note, however, that no &amp;quot;enemy of AI side&amp;quot; condition is added if [filter_second] is specified, meaning that it can include allied and/or enemy units as desired (and even units on the same side as the coward).&lt;br /&gt;
* '''seek_x, seek_y''': The x and y coordinate of the hex to seek. {{DevFeature1.15|0}} '''seek_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a coward [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=coward&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=coward3&lt;br /&gt;
             distance=5&lt;br /&gt;
             seek_x,seek_y=24,5&lt;br /&gt;
             avoid_x,avoid_y=24,15&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Zone guardian (ai_type=zone_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A zone guardian is a unit that, as the name says, guards a zone. It moves randomly inside this zone until an enemy enters it (or a separately defined enemy zone, see below). Applications might be the defense of a castle, a nesting area or for protecting an unit.  The zone macro can be called with an optional SLF, [filter_location_enemy]:&lt;br /&gt;
* If not specified, the zone guard attacks any enemy coming inside its guard zone.&lt;br /&gt;
* If [filter_location_enemy] is given, it attacks any enemy entering this zone and once there are no more enemies, it goes back to patrol in its basic zone.&lt;br /&gt;
&lt;br /&gt;
Enable the Zone Guardian Micro AI by using&lt;br /&gt;
 ai_type=zone_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone to guard.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_enemy]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone where enemies have to be attacked.  If not given, it defaults to [filter_location].&lt;br /&gt;
* '''station_x,station_y''': By default, the zone guardian moves randomly within the zone if there is no enemy to attack.  However, if station_x/y is given, it moves to this position instead during turns when it does not have an enemy to attack.  This position does not need to be inside the zone defined above.  Note: obviously it does not make sense to define [filter_location], [filter_location_enemy] and station_x/y all at the same time.  Only [filter_location] and either one of the latter two (or none of them) should be provided for any zone guardian. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a zone guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=zone_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=zone3&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=22-31,4-11  # This is intentionally chosen to extend past the lake&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             station_x,station_y=32,8&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Hang Out Micro AI (ai_type=hang_out) ==&lt;br /&gt;
&lt;br /&gt;
The Hang Out Micro AI instructs units of the AI side to &amp;quot;hang out&amp;quot; around a certain location, or locations, until a condition for mobilizing the side is met.  If none of the optional keys are given, the AI controls all units of the side to hang around the leader while moving off castle tiles to make room for recruiting.&lt;br /&gt;
&lt;br /&gt;
The AI works by moving each unit to its best hex first using the minimum amount of movement points required for this move.  Once all AI units are in position, movement points are set to zero for all of them simultaneously such that they cannot be used by other AI candidate actions.  This is done because the mobilizing condition might be met as result of one of the moves of the Hang Out AI, after which we want units to be able to continue with other AI actions.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Hang Out and Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Hang Out Micro AI by using&lt;br /&gt;
 ai_type=hang_out&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Hang Out specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  In its default configuration, the Hang Out MAI controls all units of a side, keeping them as close to the side leader(s) as possible while moving off castle tiles to make room for recruiting.  For this purpose, the candidate action score defaults to 170,000, which is just below the score of the default recruiting CA.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''': A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  If neither of these two exists, this parameter is set to default to all castle terrain (&amp;lt;tt&amp;gt;terrain=C*,C*^*,*^C*&amp;lt;/tt&amp;gt;) so that units move off the castle to make room for recruiting.  Note that this does not include keep terrain, otherwise the leader would be moved off the keep as well.&lt;br /&gt;
* '''ca_score'''=170000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 170,000, which means that it is executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Hang Out action (&amp;lt;tt&amp;gt;side=&amp;lt;/tt&amp;gt; is always set and does not need to be provided).  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the locations ''near'' which the AI units should hang out.  This does not mean that the units only move onto terrain specified here, but that they minimize the distance to such terrain.  It defaults to the position of the side leader(s).&lt;br /&gt;
* '''[mobilize_condition]''':  Contains ([[ConditionalActionsWML#Condition_Tags|WML Condition Tags]])  Once the condition set up here has been met, the Hang Out MAI is disabled and the side goes over to other AI activity.  The Hang Out MAI remains disabled after this, even if the condition is not met any more later in the scenario.&lt;br /&gt;
* '''mobilize_on_gold_less_than''': (number) Since side gold cannot be checked in a WML condition tag without storing it in a variable first, this key is provided separately.  It disables the Hang Out Micro AI once the side gold goes below the value specified here for the first time.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
* The Hang Out AI still takes effect if no locations to hang out at are found or if all map locations are to be avoided.  In that case, it simply keeps all units at their current locations.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Hang Out Micro AI configuration from the &amp;quot;Hang Out and Messenger&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hang_out&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [avoid]&lt;br /&gt;
                 terrain=C*,H*,M*,A*,S*,*^F*&lt;br /&gt;
             [/avoid]&lt;br /&gt;
             [mobilize_condition]&lt;br /&gt;
                 [have_unit]&lt;br /&gt;
                     side=1&lt;br /&gt;
                     count=7-99&lt;br /&gt;
                 [/have_unit]&lt;br /&gt;
             [/mobilize_condition]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Healer Support Micro AI (ai_type=healer_support) ==&lt;br /&gt;
&lt;br /&gt;
The Healer Support Micro AI configures the healers of a side to stay behind the battle lines and heal injured and/or threatened units rather than participate in combat under all circumstances.  You can set different levels of aggressiveness for the healers.  &lt;br /&gt;
&lt;br /&gt;
'''Notes'''&lt;br /&gt;
* By default, ''all'' healers of the AI side are controlled by this AI.  This includes in particular also the side leader, if the leader is a healer.  If you do not want the leader to be included (and possibly be moved away from the keep etc.) use the [filter] tag with canrecruit=no (see below).&lt;br /&gt;
* It is not always better to use this Micro AI than the default AI, as it takes healers (which can be strong units) away from the pool of units to choose for attacks.&lt;br /&gt;
* {{DevFeature1.15|3}} This Micro AI now takes unit guardian status and the passive_leader aspect into account.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Healers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Healer Support Micro AI by using&lt;br /&gt;
 ai_type=healer_support&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Healer Support specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the healer units.  If set, only units on the AI side passing this filter ''and'' having the healing ability are considered by this Micro AI.&lt;br /&gt;
*'''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting units to receive healing. If set, only units on the AI side passing this filter ''and'' the other healee selection criteria will be selected for healing by this Micro AI.&lt;br /&gt;
* '''aggression'''=1.0: (float) Sets the aggressiveness of the AI.  This parameter is set up as a float to accommodate future functionality, but it currently acts as a boolean: if set to zero, the AI will never let its healers participate in combat, for all other values it allows them to attack after all other units have attacked and if the healers cannot find any more units to support.  The former behavior might be appropriate in scenarios with only one or few valuable healers, while the latter might work better in scenarios with many healers.&lt;br /&gt;
* '''injured_units_only'''=no: (boolean) If set to 'yes', the AI will only move healers next to units that are already injured and skip units that currently have full hitpoints, but might get injured during the next enemy turn.&lt;br /&gt;
* '''max_threats'''=9999: (integer) The maximum allowable number of enemies that can attack a hex in order for it to be considered for a healer.  As an example, setting 'max_threats=0' means that the AI only moves healers to locations that are entirely safe from the enemy (assuming that none of the units currently on the map dies).  Note that the value of this key is checked against the number of enemies that can make it to the hex, not the number of adjacent hexes from which the healer could be attacked.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI. Note: as attacks by the healers are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The Healer Support Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded.  It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Healer Support Micro AI configuration from the &amp;quot;Healers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             aggression=0&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Lurkers Micro AI (ai_type=lurkers) ==&lt;br /&gt;
&lt;br /&gt;
The Lurker Micro AI controls some or all units of a side in &amp;quot;lurker mode&amp;quot;.  A lurker is a unit that is capable of moving across most terrains, but that only stops on and attacks from specific terrain.  It might also have the ability to hide on this terrain (which is the reason why it is called a lurker).&lt;br /&gt;
&lt;br /&gt;
Lurkers move individually without strategy and always attack the weakest enemy within their reach.  If no enemy is in reach, the lurker does a random move instead - or it just sits and waits (lurks).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of different types of lurker behavior, check out the &amp;quot;Lurkers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Lurkers Micro AI by using&lt;br /&gt;
 ai_type=lurkers&lt;br /&gt;
in the [micro_ai] tag&lt;br /&gt;
&lt;br /&gt;
===Lurkers specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
* '''[filter]''': Specifies which units of the side are controlled by the Lurker Micro AI.  This is a [[StandardUnitFilter|Standard Unit Filter]].  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': Specifies the terrain from which the lurkers attack. This is a [[StandardLocationFilter|Standard Location Filter]].&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_wander]''': Specifies the terrain on which lurkers end their moves if there is no enemy in reach. This is a [[StandardLocationFilter|Standard Location Filter]].  If [filter_location_wander] is not given, it defaults to [filter_location].&lt;br /&gt;
* '''stationary'''=no: (boolean)  If set to yes, a lurker never moves if there is no enemy within attack range.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Lurkers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=4&lt;br /&gt;
             ai_type=lurkers&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Naga Fighter&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=W*,S*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             [filter_location_wander]&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location_wander]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Messenger Escort Micro AI (ai_type=messenger_escort) ==&lt;br /&gt;
&lt;br /&gt;
The Messenger Escort Micro AI lets you define a set of waypoints through which it tries to move one or several of its units, the messenger(s).  The other units escort the messenger, trying to protect it and attacking enemies that are in its way.  Note that a messenger might not strictly hit intermediate waypoints, but that getting close is good enough, especially if a waypoint is occupied by an enemy unit.  It does, however, always try to get to the last waypoint.&lt;br /&gt;
&lt;br /&gt;
'''Notes on the use of several messengers:''' {{DevFeature1.13|0}}  &lt;br /&gt;
* This Micro AI can control several messengers at the same time, which can but do not have to move in a group.  They can be put on the map altogether in one location, or be spread out on the map or across different turns.&lt;br /&gt;
* The farther a messenger has moved through the chain of waypoints, the higher a priority it is for the AI, meaning that farther advanced (on the map) messengers are moved first, and that the escort units try to clear the way and stay with those messengers more so than with those further back on the map.  This order can be inverted using the invert_order= key.&lt;br /&gt;
* If several messengers are on the map, escort units might move from one to the other, the AI making a judgement call where they are needed most.  Closeness to a messenger is generally the most important criterion, but not the only one.&lt;br /&gt;
* If a messenger dies, the escort units previously staying with it now join one of the other messengers if there are any left (otherwise they follow default AI behavior).&lt;br /&gt;
* If you want certain escort units to stay always with the same messenger, use several messenger [micro_ai] tags with different [filter]/[filter_second] combinations.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Messenger Escort Micro AI by using&lt;br /&gt;
 ai_type=messenger_escort&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Messenger Escort specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': {{DevFeature1.13|0}} (Previous to 1.13.0, only id= was allowed.) The id of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated list of waypoint coordinates through which the messenger will go in the given order to reach its goal, given by the last coordinate.  If you want the messenger to go directly to the goal, simply enter a single x,y coordinate here.  Note that the messenger does not have to hit each waypoint exactly (except for the final one), getting close to them is good enough. {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|11}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Messenger Escort AI consists of 3 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-2&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_death_chance'''=0.67 and '''messenger_death_chance'''=0.0: (both floats; ranging from 0 to 1)    If the messenger is adjacent to an enemy at the end of the move, it will only attack the enemy if the chance of death of the enemy is &amp;gt;= enemy_death_chance and if the messenger's chance of death is &amp;lt;= messenger_death_chance (or if it is an attack at a range for which the enemy does not have a weapon).&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|0}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the escort units.  If not set, this defaults to all enemy units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''invert_order'''=no: {{DevFeature1.13|0}} If set to 'yes', the order of priority with which messenger units are considered is inverted, starting with the messenger unit that is the least advanced on the map (see description above).  In most cases, selecting this option results in worse performance of the AI, so use it with caution.  (And obviously it has no effect at all if there is only one messenger controlled by the AI.)&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|11}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Messenger Escort&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=messenger_escort&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=messenger&lt;br /&gt;
             waypoint_x=31,24,27,28&lt;br /&gt;
             waypoint_y=20,14,7,1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Patrol Micro AI (ai_type=patrol) ==&lt;br /&gt;
&lt;br /&gt;
The defines a number of waypoints for a unit (or units) to follow.  Options can be set whether the route should be followed in a loop, as an out-and back, only once, whether units encountered along the way should be attacked, etc.&lt;br /&gt;
&lt;br /&gt;
With the default settings, getting to the next waypoint takes priority over attacking for a patroller, but this can be overridden (as of '''Wesnoth 1.15.12''', using the '''attack_range''' option).  Unless instructed otherwise, the AI thus prefers to move the patrol unit around enemies rather than straight for them.  Also, if a waypoint is occupied by a unit the AI is not instructed to attack, it will (eventually) abandon that waypoint once it gets close enough and move on to the next one.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different patrol behaviors, check out the &amp;quot;Patrols&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Patrol Micro AI it by using&lt;br /&gt;
 ai_type=patrol&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Patrol specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated lists of the waypoint coordinates through which the patrol travels (in order). {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack''': By default (if this key is not set), the patrol unit attacks any unit it ends up next to after a move, or that is within '''attack_range''' (see below).  Alternatively, this parameter can be set to a comma-separated list of unit ids. If that is done, the patrol only attacks these units when it ends up next to them or if it gets within attack_range.  It ignores any other unit.  This can in turn be used to have the patroller not attack at all, by setting ''attack='' to a non-existing id.  The exception to this is an enemy on the last waypoint with ''one_time_only=yes'', which will always be attacked.&lt;br /&gt;
* '''attack_range'''=1: If the patrol comes within this many hexes of an enemy unit, it interrupts its patrol route and attacks that enemy (assuming it still has enough movement left to get to the enemy).  The '''attack=''' instructions are taken into account for this.  Note that the patrol generally tries to move around enemies, which means that for the default value of 1, enemies are usually not within attack range, except at the end of the move.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''one_time_only'''=no: (boolean)  If set to 'yes', the unit goes through the patrol route only once and then hangs out at the last waypoint.  It also always attacks any enemy unit on the last waypoint, independent of what ''attack='' is set to.&lt;br /&gt;
* '''out_and_back'''=no: (boolean)  By default, a patrol unit returns to the first waypoint after reaching the last, doing the patrol route as a loop.  If this parameter is set to 'yes', the unit instead reverses its course and perpetually does out and backs of the route.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Patrols&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=patrol&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Konrad&lt;br /&gt;
             waypoint_x=9,24,25&lt;br /&gt;
             waypoint_y=21,23,15&lt;br /&gt;
             one_time_only=yes&lt;br /&gt;
             attack=Gertburt&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Protect Unit Micro AI (ai_type=protect_unit) ==&lt;br /&gt;
&lt;br /&gt;
The Protect Unit Micro AI tries to move one or several units to a given location while keeping them safe from enemy attacks. It does so by keeping the protected units away from enemy units and close to friendly units. Other units on the AI side are controlled by the default Wesnoth AI.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Protect Unit&amp;quot; and &amp;quot;The Elves Besieged&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Protect Unit Micro AI by using&lt;br /&gt;
 ai_type=protect_unit&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Protect Unit specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[unit]''': A separate [unit] tag is required for each protected unit(s).  Each tag has required keys '''id''', '''goal_x''', '''goal_y''' for each unit, as shown in the example below. {{DevFeature1.15|0}} '''goal_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''disable_move_leader_to_keep'''=no: (boolean) If set to 'yes', side leaders protected by this AI do not try to return to their keep.  This is necessary for this Micro AI to work with units that are side leaders and that are supposed to move to an off-keep location.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
*The Protect Unit Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].  Depending on the parameters, it might also modify the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|move leader to keep candidate action]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded. It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;HttT: The Elves Besieged&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=protect_unit&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Delfador&lt;br /&gt;
                 goal_x,goal_y=1,2&lt;br /&gt;
             [/unit]&lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
                 goal_x,goal_y=1,1&lt;br /&gt;
             [/unit]&lt;br /&gt;
 &lt;br /&gt;
             disable_move_leader_to_keep=true&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Recruiting Micro AI==&lt;br /&gt;
=== General Comments on the Recruiting Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Recruiting Micro AI replaces the default recruitment AI by alternative recruiting patterns.  Currently there are two recruiting algorithms you can choose from, random recruiting and the rush recruiting used in the new (as of Version 1.11.1) Experimental AI (see below).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of a different recruiting pattern, check out the &amp;quot;Recruiting&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  You can also explore the unit choices by watching the Experimental AI in a multiplayer game.&lt;br /&gt;
&lt;br /&gt;
'''Note:''' Obviously, the Recruitment Micro AI modifies the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|recruitment candidate action]].&lt;br /&gt;
&lt;br /&gt;
=== Random Recruitment AI (ai_type=recruit_random) ===&lt;br /&gt;
&lt;br /&gt;
This AI randomly selects a unit type from the recruitment list of the side.  The probability of choosing each type and the behavior in low-gold situations can be influenced with optional parameters.&lt;br /&gt;
&lt;br /&gt;
Enable the Random Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_random&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* '''[probability]''': This tag sets the probability of selecting the given unit type.  It has two required sub-keys:&lt;br /&gt;
** '''type''': comma-separated list of unit types to which this probability is to be applied&lt;br /&gt;
** '''probability''': (non-negative float)  The probability with which these unit types is to be selected.  All unit types for which no [probability] tag is defined receive a default probability of 1.0, thus values should be chosen relative to that.  As an example, in the code below swordsmen and peasants are given a probability of 8, while no other units have assigned probabilities.  Thus, each of these two unit types is 8 times more likely to be selected than any other individual unit type.&lt;br /&gt;
* '''skip_low_gold_recruiting'''=no: (boolean)  By default, the random recruitment AI chooses a unit to recruit only from those types for which it has sufficient gold.  If this parameter is set to 'yes', it will instead choose from all available unit types and end recruiting for the turn if there is not enough gold to recruit it this turn (even if other, affordable unit types exist).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a random recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_random&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [probability]&lt;br /&gt;
                 type=Swordsman,Peasant&lt;br /&gt;
                 probability=8&lt;br /&gt;
             [/probability]&lt;br /&gt;
 &lt;br /&gt;
             skip_low_gold_recruiting=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Rush Recruitment AI (ai_type=recruit_rushers) ===&lt;br /&gt;
&lt;br /&gt;
This AI replaces the default recruiting with the rush recruiting used in the new (as of Version 1.11.1) Experimental AI.&lt;br /&gt;
&lt;br /&gt;
Enable the Rush Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_rushers&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* {{DevFeature1.15|2}} '''high_level_fraction'''=0: (non-negative number) The approximate fraction of units of level 2 or higher to be recruited. This is defined as fraction of units on the map, not of new units being recruited during a turn or over several turns (which makes a difference if there are already units on the map). The effect is also cumulative per level (starting from level 2), meaning that 1/3 of L2 units will be recruited in the example above, 1/3*1/3=1/9 L3s etc. The default value is zero, which leaves the high-level unit recruiting to the default Experimental AI algorithm resulting usually in very few such units being recruited.&lt;br /&gt;
* '''randomness'''=0.1: (number)  A random number is applied to the rusher recruit engine's score to prevent the recruitment pattern from being too predictable. 0 causes no randomness to be applied, while larger numbers increase the random effect. A value of 1-2 generates results in which the random effect is approximately equal to the scored effect. Extremely high values are essentially entirely random.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a rush recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_rushers&lt;br /&gt;
             action=add&lt;br /&gt;
          [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Simple Attack Micro AI (ai_type=simple_attack) ==&lt;br /&gt;
&lt;br /&gt;
The Simple Attack Micro AI is meant as an addition to the default combat candidate action for when one wants to force the AI to do attacks that it would otherwise not do (for example on units that are 1 XP from leveling) or to execute certain attacks before all others (for example, attack with all &amp;quot;expendable&amp;quot; units first).  It is not set up to be a replacement for the default attacks.  Some notes:&lt;br /&gt;
&lt;br /&gt;
* The algorithm is different from and simpler than that of the default combat CA.  For example, no attack combinations are considered, only individual attacks.  As a result, the [[AiWML#List_of_AI_Aspects|default aspects]] (aggression, caution, [avoid], ...) are not taken into account.  Also, if lots of units are involved, evaluation of this Micro AI is potentially quite a bit slower than of the default attacks.&lt;br /&gt;
&lt;br /&gt;
* If an attack matching the Standard Unit Filters described below is found, this attack will always be executed, no matter how bad the odds are.  Thus, this Micro AI can be used to force the AI to do attacks it would otherwise avoid.&lt;br /&gt;
&lt;br /&gt;
* Other than selecting the attacker/defender units, this AI is currently not configurable.  Let us know if you would like to see certain options to be added.  (No guarantees that we will add them, but we will definitely consider them.)&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Simple Attack&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=simple_attack&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Simple Attack specific keys for the [micro_ai] tag===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  It is, however, in general not desirable to use this AI without at least one of the optional Standard Unit Filters below, as this AI is not meant as a replacement of the default combat candidate action.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=110000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 110,000, which means that it is executed right before the default combat candidate action.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Simple Attack action.  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units the AI should attack.  It defaults to all enemy units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
* '''weapon'''=-1: (integer) The number of the weapon that should be used for this attack.  Weapon numbers start at 1 (not 0).  -1 stands for &amp;quot;best weapon&amp;quot; (by whatever criterion the AI chooses to use).   '''Available from Wesnoth 1.11.8'''&lt;br /&gt;
&lt;br /&gt;
Below are 2 examples of Simple Attack Micro AI configurations from the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  The first one shows how to force the AI to attack with all Soulless and Walking Corpse units first:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110000&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless,Walking Corpse&lt;br /&gt;
             [/filter]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
The second example executes attacks by Soulless on all enemy units that are 1 XP from leveling.  Note that the Lua code in the preload event is there simply in order to code the &amp;quot;1 XP from advancing&amp;quot; condition.  It is not needed for most other SUFs, as can be seen in the previous example.&lt;br /&gt;
     [event]&lt;br /&gt;
         name=preload&lt;br /&gt;
         first_time_only=no&lt;br /&gt;
         [lua]&lt;br /&gt;
             code=&amp;lt;&amp;lt;&lt;br /&gt;
                 function close_to_advancing(unit)&lt;br /&gt;
                     if (unit.experience &amp;gt;= unit.max_experience-1) then&lt;br /&gt;
                         return true&lt;br /&gt;
                     else&lt;br /&gt;
                         return false&lt;br /&gt;
                     end&lt;br /&gt;
                 end&lt;br /&gt;
             &amp;gt;&amp;gt;&lt;br /&gt;
         [/lua]&lt;br /&gt;
     [/event]&lt;br /&gt;
 &lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110001&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless  # No Walking Corpses; L0 units don't advance enemy&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 lua_function = &amp;quot;close_to_advancing&amp;quot;&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
Several Simple Attack Micro AIs can be combined for the same AI side.  The two examples above are both used (note the different values for ca_score) in the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
== Custom Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|5}}&lt;br /&gt;
&lt;br /&gt;
From 1.13.5 onwards, adding a new Micro AI can be easily done without altering any core files or overriding the micro_ai tag. The [micro_ai] tag now looks up its ''ai_type'' parameter in the &amp;lt;code&amp;gt;wesnoth.micro_ais&amp;lt;/code&amp;gt; table and uses the micro AI setup function found there to determine which keys are required and allowed and what candidate actions to add.&lt;br /&gt;
&lt;br /&gt;
A micro AI setup function takes as its single parameter the contents of the [micro_ai] tag and returns three separate values:&lt;br /&gt;
*'''required''': A list of required keys and tags, as strings. Tags are indicated by enclosing them within square brackets in the string. If a key or tag in this list is missing from the [micro_ai] tag, an error will be printed.&lt;br /&gt;
*'''optional''': Similar to above, a list of optional keys and tags, as strings. No error will be printed if a key or tag in this list is missing, nor will an error be printed if it is present (as would be the case for unrecognized keys or tags).&lt;br /&gt;
*'''ca_params''': A table specifying the candidate actions that constitute the micro AI. It should contain an ''ai_id'' key (which specifies a base ID to be used for candidate actions of this micro AI) and a series of candidate action definitions. Each candidate action definition is a table containing the following keys:&lt;br /&gt;
**'''ca_id''': The ID used for the candidate action. This will be combined with the ''ai_id'' to produce the final ID.&lt;br /&gt;
**'''location''': Path to the Lua file that defines the candidate action.&lt;br /&gt;
**'''score''': The maximum score for the candidate action. If the [micro_ai] tag specified a ''ca_score'' parameter, the score here should normally be based on that.&lt;br /&gt;
&lt;br /&gt;
(The return statement would thus look like &amp;lt;syntaxhighlight lang='lua' inline&amp;gt;return required, optional, ca_params&amp;lt;/syntaxhighlight&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
'''Examples''' of micro AI setup functions can be found in the [https://github.com/wesnoth/wesnoth/tree/1.14/data/ai/micro_ais/mai-defs default MAI definitions directory].  See the [https://github.com/wesnoth/wesnoth/blob/1.14/data/ai/micro_ais/mai-defs/animals.lua#L4-L12 Big Animals MAI definition] for a simple example.  For custom micro AIs, the setup code should be put into a ''[lua]'' tag inside a preload event.&lt;br /&gt;
&lt;br /&gt;
Since the function has full access to the contents of the [micro_ai] tag, it can conditionally add candidate actions to '''ca_params''' depending on the values of other keys within the [micro_ai] tag. It can also perform other actions such as adding facets to an aspect; if it does such things, it should pay attention to the ''action'' key to determine whether it's being added or removed.&lt;br /&gt;
&lt;br /&gt;
There are two useful helper functions that can be used by Micro AI setup functions requiring aspect changes. They can be brought into scope by a line similar to the following:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='lua&amp;gt;local MAIH = wesnoth.require(&amp;quot;ai/micro_ais/micro_ai_helper.lua&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* MAIH.add_aspects(side, aspect_params)&lt;br /&gt;
* MAIH.delete_aspects(side, aspect_params)&lt;br /&gt;
&lt;br /&gt;
The ''aspect_params'' parameter is an array of tables, each of which contains the following keys:&lt;br /&gt;
*'''aspect''': The aspect to change.&lt;br /&gt;
*'''facet''': The WML defining the facet. This must include an ''id'' key in order for the aspect to be correctly deleted. For maximum compatibility, incorporate the ''ca_id'' key from the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=67034</id>
		<title>Micro AIs</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=67034"/>
		<updated>2021-03-14T02:19:42Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Messenger Escort specific keys for the [micro_ai] tag: */ add [avoid] tag&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== A Few General Words about Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
* Micro AIs allow Wesnoth campaign and scenario authors to add new AI functionality without the need for any AI programming. They bring new specialized behavior to Wesnoth that cannot be achieved, at least not easily, by adjusting default AI parameters (such as those described at [[AiWML]]) or with WML code.&lt;br /&gt;
&lt;br /&gt;
* A Micro AI is activated and configured (or deleted) via the [micro_ai] tag, requiring only a few lines of WML code.&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.6''', the Micro AI behavior was inconsistent when it came to '''hidden enemies and petrified units'''.  Some Micro AIs ignored those, while others did not.  In some case there was even a potential for AI errors when an AI unit ran into hidden or petrified enemies.  As of 1.13.6, this has all been fixed.  The Micro AIs now correctly and consistently ignore hidden units and treat petrified units as blocking the hex they are on but having no effect otherwise, in the same way as the default AI does.  '''Important notes:'''&lt;br /&gt;
** It is not possible to re-include hidden/petrified units via the Standard Unit Filters (SUFs) used by some of the Micro AIs. In fact, if this is attempted, the filters will generally not match any unit.&lt;br /&gt;
*** This only applies to SUFs which go directly into the [micro_ai] tag. Any SUF that is a sub-tag of another filter (for example, of a Standard Location Filter) is taken literally and returns all units matching the filter. If hidden or petrified units are to be excluded from such a sub-filter, that needs to be specified directly in that filter.&lt;br /&gt;
** We also note that, because all the Micro AIs are written in Lua, it is possible to [[Micro_AIs#Using_Development_.281.13.29_Version_Micro_AIs_in_your_Stable_.281.12.29_Version_Add-on|move a Micro AI's code into one's add-on]] and modify it to whatever scenario-specific behavior is desired (this does not only apply to hidden/petrified units, but to all behavior). Setting up general Micro AI behavior that works in every possible setting is not feasible, but adapting it to work (only) in the specific setting of a given scenario is often quite simple. If you do not know how to do this, please post a message on the Wesnoth forums and we will be happy to help you with it.&lt;br /&gt;
&lt;br /&gt;
* The '''[[AiWML#List_of_AI_Aspects|default AI aspects]] are, for the most part, not taken into account by the Micro AIs'''.  For example, unless noted otherwise, specifying a value for ''aggression'' does not change the MAI behavior, and locations specified in an ''[avoid]'' tag generally can be entered by units controlled by an MAI.  This is a feature of the Micro AIs, not a bug.  If you feel that a certain default AI aspect should also apply to a Micro AI, please let us know in the [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 Micro AIs Feedback thread].&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.7''', the Micro AIs did not all work reliably when the '''AI side is under shroud''' and it may have been necessary to turn shroud off for the side when using a Micro AI. {{DevFeature1.13|7}} The Micro AIs now all work for sides under shroud. They do this by disregarding shroud for terrain purposes when path finding, consistent with default Wesnoth AI behavior, while still ignoring hidden units (including those hidden under shroud) correctly.&lt;br /&gt;
&lt;br /&gt;
* Most Micro AIs in their default setting apply to all units of a side, although many of them provide parameters for filtering the units that should be affected by the new behavior.  If the Micro AI is applied only to part of the units of a side, the other units follow the default Wesnoth AI behavior.&lt;br /&gt;
&lt;br /&gt;
* Micro AIs are available in mainline since Wesnoth 1.11/1.12.&lt;br /&gt;
&lt;br /&gt;
== Micro AI Test and Demo Scenarios ==&lt;br /&gt;
&lt;br /&gt;
All Micro AIs described here can be checked out in a number of test and demo scenarios.  The easiest way to access the test scenarios is by assigning a hotkey to &amp;quot;Start Test Scenario&amp;quot; in the preferences (as of Wesnoth 1.13.8). Then, simply press the hotkey in the titlescreen and select the desired scenario from the list (e.g. 'micro_ai_test' for the overall &amp;quot;switchboard scenario&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Test scenarios can also be accessed from the command line by typing&lt;br /&gt;
 path/wesnoth-executable -t micro_ai_test&lt;br /&gt;
or (depending on your system and Wesnoth version)&lt;br /&gt;
 path/wesnoth-executable -tmicro_ai_test&lt;br /&gt;
This starts up a &amp;quot;switchboard scenario&amp;quot; in which you can select the Micro AI demonstration scenario you want to check out.  Here, ''path/wesnoth-executable'' needs to be replaced by whatever the path and filename of the Wesnoth executable is on your system.&lt;br /&gt;
&lt;br /&gt;
== Setting up a Micro AI ==&lt;br /&gt;
&lt;br /&gt;
Setting up a Micro AI requires only one simple step:&lt;br /&gt;
==== Activating and configuring (or deleting) a Micro AI ====&lt;br /&gt;
&lt;br /&gt;
Micro AIs are activated, deleted and configured using the [micro_ai] tag.  This tag needs to be placed in [[ActionWML]], that is, in an event, a menu option or the like.  As an example, the following code activates the healer_support Micro AI in its default configuration for Side 2 from the beginning of the scenario:&lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
 &lt;br /&gt;
         # Configure the healer support micro AI&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
'''Keys required for all Micro AIs:'''&lt;br /&gt;
All [micro_ai] tags must contain the following three required keys:&lt;br /&gt;
* '''side''': The side for which the Micro AI is to be added, changed or deleted.  This has to be an individual side number, lists of sides are not supported.&lt;br /&gt;
* '''ai_type''': The type of Micro AI to be added, changed or deleted.  See the following sections for allowed values.&lt;br /&gt;
* '''action''' (string): The action to take concerning the Micro AI.  The following values are allowed: &lt;br /&gt;
** ''add'': Add a Micro AI to a side.&lt;br /&gt;
** ''change'': Change the configuration of an existing Micro AI.  Note that this does not only change the specific parameters provided in the tag, but it replaces the entire existing Micro AI by a new version with the new (and only the new) configuration.  It is therefore equivalent to using first the ''delete'' and then the ''add'' action.&lt;br /&gt;
** ''delete'': Delete an existing Micro AI from a side.&lt;br /&gt;
&lt;br /&gt;
'''Keys required under certain circumstances:'''&lt;br /&gt;
* '''ca_id''': (string) If several MAIs of the same type are used for the same side and one or several of these are to be changed or deleted during a scenario, this key needs to be included so that the [micro_ai] tag knows which of them needs to be changed/deleted.  The same is also true if a Micro AI is changed more than once during a scenario.  We therefore '''strongly recommend always setting ''ca_id''''' for MAIs which are, or might be, changed or deleted during a scenario.&lt;br /&gt;
** '''Note:''' The id(s)/name(s) actually assigned to the CA(s) of the MAI by the engine might be different from the parameter passed here.  It is, however, sufficient to use this ca_id with the [micro_ai] tag add/delete/change actions to identify the MAI uniquely.  (In other words: you don't have to worry about this if you always use the [micro_ai] tag, but might have to figure out the actual name(s) of the CA(s) if you want to delete or change them manually.)&lt;br /&gt;
* Other keys may also be required depending on the Micro AI.  In additional, a number of optional keys may be available to configure the AI behavior.  See the sections on the individual Micro AIs for details.&lt;br /&gt;
&lt;br /&gt;
'''Required keys for deleting a Micro AI:'''&lt;br /&gt;
&lt;br /&gt;
Only a subset of the keys required for setting up a Micro AI are needed when deleting it.  '''side''', '''ai_type''' and '''action'''=delete always need to be provided.  '''ca_id''' needs to be given if it was provided when adding the Micro AI (see above for when this is needed).&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* The ''add'' and ''change'' actions ignore all keys that do not apply to the respective Micro AI type.  The ''delete'' action ignores all keys other than the required keys listed above.&lt;br /&gt;
&lt;br /&gt;
* Some of the Micro AIs use and/or modify default AI components (candidate actions or aspects) when they are added, or reset these components to their default values when they are deleted.  Thus, if you have modified the AI yourself using the default CAs or aspects, some of these modifications might be affected, or might be interfering with the Micro AI.  The sections below indicate whether a Micro AI modifies any default AI components.&lt;br /&gt;
&lt;br /&gt;
===Combining Different Micro AI Types for the Same Side===&lt;br /&gt;
&lt;br /&gt;
No extra steps need to be taken when combining different Micro AIs on the same side.  Simply set up the [micro_ai] tags for all AIs you want to use.&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* While it is ''technically'' possible to combine any number of Micro AIs, this does not mean that all Micro AIs work together in practice.  Some are not compatible because they control units in contradictory ways.  &lt;br /&gt;
* Not all combinations that ''should'' work have been tested yet.  If you find a combination that you believe should work but doesn't, [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 please let us know] and we will see if that can be fixed.&lt;br /&gt;
* The candidate action score almost certainly has to be set (using the ca_score= key) for one or several of the MAIs if several of them are used on the same side.&lt;br /&gt;
&lt;br /&gt;
== Animals Micro AIs ==&lt;br /&gt;
=== General Comments on the Animal Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Animals Micro AIs are meant to simulate the behaviors of certain types of animals.  They are, however, set up so that they can be used with arbitrary unit types and might be applicable in quite diverse situations.  The AIs nevertheless remain named after the animals for which they were written originally, so that they are (to some extent) descriptive of their behavior.&lt;br /&gt;
&lt;br /&gt;
Brief descriptions of the AI behaviors are given in the following subsections.  To get a better feeling for their behavior, check out the &amp;quot;Animals&amp;quot;, &amp;quot;Swarm&amp;quot;, &amp;quot;Wolves&amp;quot; and &amp;quot;Dragon&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  Note that some of the animal AIs are designed for animal types that do not exist in mainline.  In the test scenario, other animal types are therefore substituted for them.&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Wolves (ai_type=wolves) ===&lt;br /&gt;
&lt;br /&gt;
The Wolves Micro AI organizes all &amp;quot;wolf&amp;quot; units (predators) of the side to hunt in a single pack.  They actively chase after the closest prey and try to corner it (not always super successfully), but are easily distracted by other enemy units coming into attack range, to the point where the pack might split up.  The wolves can, however, be told to attack only prey units as well as to try to avoid other types of units (such as larger predators).  When no prey is left, the wolves wander randomly.&lt;br /&gt;
&lt;br /&gt;
This AI works best for a small number of predators (2 - 5) and sparse prey.  Also, the predators should be put on the map in a pack, that is, reasonably close to each other.  For a demonstration of the Wolves AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Note that another wolves AI exists, called [[Micro_AIs#Animals_AI:_Multi-pack_Wolves_.28ai_type.3Dwolves_multipacks.29|Multipack Wolves]], which distributes the wolves of a side into several packs, rather than all of them into one single pack as it is done here.&lt;br /&gt;
&lt;br /&gt;
Enable the Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the predator units.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the prey units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_only_prey'''=no: In the default configuration, the wolves attack all enemy units in attack range.  If this key is set to 'yes', they only attack prey units.&lt;br /&gt;
* '''avoid_type''': Comma-separated list of unit types (on enemy sides) which the wolves try to avoid.  They try to stay out of these units' way as much as possible when moving around (except when they are moving in to attack another enemy unit) and never attack them.&lt;br /&gt;
* '''ca_score'''=90000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 90,000, which is lower than the default combat CA, but higher than most other default CAs which move units.  It can be changed by setting this parameter but in most cases this will not make sense.  Note that the Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Notes on default (RCA) AI aspects:''' &lt;br /&gt;
* If ''attack_only_prey'' or ''avoid_type'' are set, the Wolves Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* Attacks by the wolves are left to the default (RCA) AI, meaning that the value of ''aggression'' has an influence on the behavior.&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI is taken into account. Note: as attacks by the wolves are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=6&lt;br /&gt;
             ai_type=wolves&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Wolf&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Vampire Bat&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             avoid_type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Multi-pack Wolves (ai_type=wolves_multipacks) ===&lt;br /&gt;
&lt;br /&gt;
The Multi-pack Wolves Micro AI is different from the [[Micro_AIs#Animals_AI:_Wolves_.28ai_type.3Dwolves.29|Wolves Micro AI]] in that there can be an arbitrary number of wolf packs, with the pack size being a free parameter.  At the beginning of the scenario, close wolves are grouped into packs in a semi-methodical way.  If the wolves are put on the map in distinct groups of close wolves of the correct number, they will be joined into packs matching these groups.  If, on the other hand, they are spread out all over the map, the method of assigning them to packs is semi-random.&lt;br /&gt;
&lt;br /&gt;
Wolves of the same pack begin by joining each other on the map.  After that, they stay together until only one wolf is left, which then tries to join up with an incomplete pack or with other single wolves.  Individual wolves entering the map during the scenario behave in that way as well.&lt;br /&gt;
&lt;br /&gt;
A second difference to the other Wolves AI is that wolves do not actively hunt here.  For the most part they just wander (often long distance).  However, the pack ferociously (and without regard for its own health) attacks any enemy units that come into range, as long as that does not mean separating the pack by more than a few hexes.  Staying together, or joining with a new wolf assigned to the pack, is the only thing that takes priority over satisfying the wolves' thirst for blood.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Multi-pack Wolves AI behavior, check out the &amp;quot;Wolves&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Multi-pack Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves_multipacks&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Multi-pack Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''pack_size'''=3: (integer) The size of the packs.&lt;br /&gt;
* '''show_pack_number'''=no: If set to &amp;quot;yes&amp;quot;, the wolves' pack numbers will be shown below them.  Note that this means that the pack number of a wolf killed in an attack will remain as a label on the map.  &lt;br /&gt;
* '''type'''=Wolf: Comma-separated list of the unit types which the AI controls. The default value is &amp;quot;Wolf&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves_multipacks [micro_ai] tag usage from the &amp;quot;Wolves&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=wolves_multipacks&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             show_pack_number=yes&lt;br /&gt;
             pack_size=4&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Big Animals (ai_type=big_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Big Animals Micro AI is a simulation of large predators that wander and hunt alone, such as Bears, Giant Spiders or Yetis.  For the most part, these just wander on the terrain that's been defined for them, but they attack enemy units if those happen to come into range.  The AI can be set up so that the Big Animals stay out of the way of other units (such as other large predators), in which case they only attack those if cornered or if they run into them accidentally (such as ambushers).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Big Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Big Animals Micro AI by using&lt;br /&gt;
 ai_type=big_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the animals to which this AI is applied.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': The big animals often go for long distance (multi-turn) journeys.  This tag is a [[StandardLocationFilter|Standard Location Filter]] describing the terrain from which the next goal is chosen.  The default is all terrain on the map. &lt;br /&gt;
* '''[filter_location_wander]''': The terrain on which the big animals will end their moves. This is a [[StandardLocationFilter|Standard Location Filter]].  The default is all terrain on the map.&lt;br /&gt;
* '''[avoid_unit]''': A [[StandardUnitFilter|Standard Unit Filter]] describing all enemy units that the big animals will avoid.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
Here's an example of big_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=big_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Ghast&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [avoid_unit]&lt;br /&gt;
                 type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
             [/avoid_unit]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=1-40,1-18&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Forest Animals (ai_type=forest_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Forest Animals Micro AI simulates a number of different animal behaviors that one might find in a forest, namely animals behaving like deer, rabbits and families of tuskers.  In the following, we will refer to those animals by these types, even though the actual unit types can be selected freely.  They behave as follows:&lt;br /&gt;
* Each deer wanders randomly on (selectable) terrain, except when enemies get in its (the deer's) maximum movement range, in which case it flees to the farthest point it can reach.&lt;br /&gt;
* Tuskers exhibit the same behavior as deer, except when an enemy is next to one of the tusklets.  This enemy is attacked unconditionally by the tuskers trying to protect their young.&lt;br /&gt;
* Tusklets blindly follow the closest adult tusker, except when there is no tusker left, in which case they behave the same as deer.&lt;br /&gt;
* Rabbits also wander randomly, but in addition disappear into their holes (if any are within reach) when enemies are close.  They reappear out of their holes at the beginning of the turn, if it is safe.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Forest Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Forest Animals Micro AI by using&lt;br /&gt;
 ai_type=forest_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Forest Animals AI consists of 4 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-3&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''rabbit_type, tusker_type, tusklet_type, deer_type''': Comma-separated lists of the unit types behaving as the respective animals.  If any of these parameters are not set, those types of animal behavior are skipped by the AI.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain on which the forest animals wander when they are not threatened by enemies.&lt;br /&gt;
* '''rabbit_number'''=6: (integer)  The number of rabbits for the AI to have on the map simultaneously.  The AI puts the missing number of rabbits onto the map at the beginning of the AI's turn (when the rabbits come out of their holes).&lt;br /&gt;
* '''rabbit_enemy_distance'''=3: (number)  Rabbits won't spawn in holes that aren't more than this distance away from all enemies.&lt;br /&gt;
* '''rabbit_hole_img''': Rabbit holes are marked by items on the map.  If this parameter is set, it must be the file name of the item image.  If it is not set, all items on the map are treated as rabbit holes.&lt;br /&gt;
** {{DevFeature1.14|10}} Items used for rabbit holes on the map border used to produce an error. Placing them on the map border therefore needed to be avoided on the scenario setup side. They are now automatically excluded from consideration by the AI code.&lt;br /&gt;
&lt;br /&gt;
Here's an example of forest_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=forest_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             deer_type=Vampire Bat&lt;br /&gt;
             rabbit_type=Giant Rat&lt;br /&gt;
             tusker_type=Ogre&lt;br /&gt;
             tusklet_type=Young Ogre&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=*^F*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Herd and Herders (ai_type=herding) ===&lt;br /&gt;
&lt;br /&gt;
The Herding Micro AI sets up an area on the map where a number of herder units (such as sheep dog) control and protect a herd (such as sheep).  For convenience, we simply refer to them as sheep dogs and sheep in the following, even though arbitrary unit types can be selected for either.&lt;br /&gt;
&lt;br /&gt;
Sheep dogs try to keep their sheep safe.  This involves keeping them inside the herding area, positioning themselves in between the sheep and approaching enemies, and attacking the enemies if those get too close.  If no active herding or protecting move is needed, the dogs go to a random location on the perimeter of the herding area.&lt;br /&gt;
&lt;br /&gt;
Sheep wander aimlessly except when a sheep dog is next to them, in which case they run away from the dog.  The dogs exploit this by positioning themselves on the outside of the sheep, if possible.  Sheep also run away from approaching enemies.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Herding AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Herding Micro AI by using&lt;br /&gt;
 ai_type=herding&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herder units (e.g. dogs).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herd units (e.g. sheep).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain forming the perimeter of the herding area.  In other words, this is the path the herders (dogs) use to patrol around the outside of the herding area.  This must be a set of coordinates ''completely enclosing'' the herding area.  Here's an example of setting up a rectangular perimeter:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         x=10-20,10-20,10,  ,20&lt;br /&gt;
         y=11,   21,   11-21,11-21&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
Another possibility is by defining the path by certain terrain on the map (as it is done in the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]), in which case the filter can simply look like this:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         terrain=Rb&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
* '''herd_x,herd_y''': The coordinate of one hex (''any'' one hex) inside the herding area.  This is needed to define which part of the map is on the ''inside'' of the herding area perimeter defined by [herding_perimeter], and which is the ''outside''. {{DevFeature1.15|0}} '''herd_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''attention_distance'''=8: (integer)  If an enemy is within this distance of a herd unit, but not as close as ''attack_distance'', the herders try to position themselves in between the enemy and the herd unit.&lt;br /&gt;
* '''attack_distance'''=4: (integer)  If an enemy is within this distance of a herd animal, the herders attack this enemy.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Herding AI consists of 6 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-5&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Herding [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=herding&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Footpad&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Troll Whelp&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             herd_x,herd_y=32,28&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=Rb&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Hunter (ai_type=hunter) ===&lt;br /&gt;
&lt;br /&gt;
The Hunter Micro AI leads a unit (or units) through hunt-and-rest cycles.  The units wander in a prescribed area of the map, their hunting ground, until an enemy unit comes within range.  Note that the units do not actively chase down enemies, but wander randomly through the hunting ground until an enemy is encountered.&lt;br /&gt;
&lt;br /&gt;
If enemies are within range of a hunter unit, the hunter attacks the weakest of them.  If a kill is made, it then retreats to its rest location, where it stays for a certain number of turns and until fully healed (whatever happens later).&lt;br /&gt;
&lt;br /&gt;
A few more details:&lt;br /&gt;
* If the hunter's way home is entirely blocked on the return (so that there is no possible path), the normal RCA AI takes over its behavior.&lt;br /&gt;
* However, if the way is blocked by an enemy unit occupying the rest hex, that enemy will be attacked unconditionally.&lt;br /&gt;
* A kill only makes the hunter go home when it is the attacker, not as defender.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Hunter AI behavior, check out the &amp;quot;Dragon&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]. &lt;br /&gt;
&lt;br /&gt;
Enable the Hunter Micro AI by using&lt;br /&gt;
 ai_type=hunter&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''home_x,home_y''': The coordinates of the hex to which the hunter returns and rests at after a kill. {{DevFeature1.15|0}} '''home_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the unit's hunting ground.  If this tag is not provided, the hunter uses all of the map.&lt;br /&gt;
* '''rest_turns'''=3: (integer)  The number of turns the AI stays at home_x,home_y after a kill.  It will not leave its home until this number of turns have passed ''and'' it is fully healed.&lt;br /&gt;
* '''show_messages'''=no: (boolean) If set to yes, the hunter announces whenever its behavior changes to the next phase.  Note that this is meant as debug output for the scenario author.  The messages are not translated and are not included in replays (and are only visible to the host in an MP game).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a hunter [micro_ai] tag usage from the &amp;quot;Dragon&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hunter&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Rowck&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=5-30,1-15&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             home_x,home_y=3,15&lt;br /&gt;
             rest_turns=2&lt;br /&gt;
 &lt;br /&gt;
             show_messages=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Swarm (ai_type=swarm) ===&lt;br /&gt;
&lt;br /&gt;
The Swarm Micro AI uses some very simply algorithms to simulate animal swarm behavior.  Without adjacent enemies, they simply do a random move, trying to stay together and at a certain distance from enemies.  However, if an enemy unit is close to any bat, the swarm scatters.  This is particular fun to watch when one places an enemy unit in the middle of the swarm.  After being scattered, the swarm members slowly rejoin, but not in a very organized way.  Sub-swarms or individual bats might roam around for quite some time before they find their way back.  It is also possible that individual bats (or small groups) split off from the larger swarm at times.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Swarm AI behavior, check out the &amp;quot;Swarm&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Swarm Micro AI by using&lt;br /&gt;
 ai_type=swarm&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Swarm AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_distance'''=5: (int) The minimum distance kept between units of the swarm and enemies when the swarm moves as a whole.&lt;br /&gt;
* '''scatter_distance'''=3: (int) Enemies within &amp;quot;scatter_distance&amp;quot; hexes of any swarm unit cause the swarm to scatter, by each unit trying to maximize its individual distance from the enemy.&lt;br /&gt;
* '''vision_distance'''=12: (int) Only units within this distance follow the overall swarm motion (either away from an enemy or of the swarm as a whole).  The smaller this value is set, the less likely the swarm is to stay together or rejoin.  This parameter is meant to simulate how far an individual member of the swarm can see, meaning that the swarm is &amp;quot;out of sight&amp;quot; (and the unit will not be able to follow it) if it is more than this distance away.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Swarm [micro_ai] tag usage from the &amp;quot;Swarm&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=swarm&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Assassin Squad Micro AI (ai_type=assassin) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|6}}&lt;br /&gt;
&lt;br /&gt;
This Micro AI controls one or several assassin units which try to circle around enemy units with the goal of hitting a target unit without being distracted or impeded by other enemies.  The units will always attack the target if such attacks are possible, otherwise they will follow the path of least resistance toward it. Notes:&lt;br /&gt;
&lt;br /&gt;
* The main priority for the AI is to find paths that avoid enemy units. However, if no such path can be found, the assassins will still advance toward the target, taking the path exposing the units to the least potential damage from enemies.&lt;br /&gt;
&lt;br /&gt;
* The units will not necessarily follow the same path. The best path is chosen individually for each unit.&lt;br /&gt;
&lt;br /&gt;
* While it is not a requirement for the AI to work, using ambusher or skirmisher units, or units with the quick trait, makes this AI more lethal in many cases.&lt;br /&gt;
&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded, it does not work otherwise. That means that '''ca_score''' is not a valid key for the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=assassin&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Assassin Squad specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the assassin units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the target unit.&lt;br /&gt;
** It is important to realize that the AI currently only works correctly with a single target unit. It is thus the responsibility of the scenario designer to make sure that this filter matches an individual enemy unit. If several enemy units are to be targeted in the same scenario, several instances of the Assassin Micro AI can be used.&lt;br /&gt;
** The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[prefer]''': A [[StandardLocationFilter|Standard Location Filter]] selecting locations on the map for the assassins to prefer when making their way toward the target unit. This could be used, for example, to make units with the ambush ability move (mostly) through forest or to have the assassins circle around the, say, northern part of the map rather than the south if that is desirable for scenario design purposes.&lt;br /&gt;
** As with avoiding enemies, the units will still move across non-preferred terrain if that is either the only option or ''much'' shorter than preferred terrain&lt;br /&gt;
** Attacking, once the target unit is in range, is not influenced by the [prefer] SLF.&lt;br /&gt;
&lt;br /&gt;
Here is an example of an Assassin Squad [micro_ai] tag usage:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=assassin&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Assassin,Master at Arms,Duelist&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             [prefer] # Circle around northern part of map&lt;br /&gt;
                 x=1-9, 10-99&lt;br /&gt;
                 y=1-37, 1-17&lt;br /&gt;
             [/prefer]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Bottleneck Defense Micro AI (ai_type=bottleneck_defense) ==&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense Micro AI lets you define a location on the map where the AI can take a defensive stance behind a narrow passage (bottleneck).  Units on the front line are backed up by healers and/or units with the leadership ability.  Injured units are moved to the back and replaced by uninjured (or less injured) units.  The units on the front line only attack when it is safe (no retaliation) or when there is a high chance that they will make a kill or level up.  Using this Micro AI only makes sense if there is no way for the enemy sides to move around the bottleneck and attack the AI's units from behind.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Bottleneck&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Bottleneck Defense Micro AI by using&lt;br /&gt;
 ai_type=bottleneck_defense&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Bottleneck Defense specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense AI requires two sets of coordinates that define where it should be taking up its defensive stance, and from which side the enemy attacks at that location.&lt;br /&gt;
&lt;br /&gt;
* '''x,y''': Comma separated lists of the hexes on the front line, where strong units are placed.  All hexes on which the AI makes contact with (can be attacked by) the enemy need to be included here.  Units are placed on them in the order in which they are listed here. {{DevFeature1.15|0}} '''location_id''' can be used instead with named/special locations.&lt;br /&gt;
* '''enemy_x,enemy_y''': Comma separated list of the hexes from which the enemy can attack the AI front line units.  This is needed for the AI to know on which side of the front line support units (healers etc.) need to be placed.  In many cases, it is sufficient to provide only one of the enemy front line hexes, but there are geometries for which that does not work.  It is therefore safer to list all enemy front line hexes here. {{DevFeature1.15|0}} '''enemy_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
By default, the AI places healers and units with the leadership ability both on the front line itself (if they are the strongest units available) and on hexes adjacent to and behind the line.  If different placement is desired, these locations can be overridden with the following keys.&lt;br /&gt;
&lt;br /&gt;
* '''active_side_leader'''=no: (boolean)  If set to 'yes', the side leader participates in the bottleneck defense action after the side's gold has been spent.  If set to 'no' (default), the leader follows default AI behavior (sitting on the keep and recruiting, for the most part).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Bottleneck Defense AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.15|3}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the units.  Only AI units matching this filter participate in the bottleneck defense.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''healer_x,healer_y''': Comma separated list of hexes where healers are placed.  This can be used to keep healers from the front line (or to take up only certain positions on the front line), and/or to override the default healing positions behind the line.  Healers are placed on them in the order in which the hexes are listed, with the exception that hexes on the line always take priority over hexes behind the line. {{DevFeature1.15|0}} '''healer_loc''' can be used instead with named/special locations.&lt;br /&gt;
* '''leadership_x,leadership_y''': Same as ''healer_x,healer_y'', but for units with the leadership ability. {{DevFeature1.15|0}} '''leadership_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
As an example, in the &amp;quot;Bottleneck&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]], there are three front line hexes, two of which are on hill terrain, while the third is a road with a lower defense rating.  The healer and side leader are supposed to participate in combat because they are strong units, but only from the hill hexes to keep them safer.  This is accomplished by using the following keys settings (check out the scenario to see which hex is which):&lt;br /&gt;
 [micro_ai]&lt;br /&gt;
     side=1&lt;br /&gt;
     ai_type=bottleneck_defense&lt;br /&gt;
     action=add&lt;br /&gt;
 &lt;br /&gt;
     x=14,14,14&lt;br /&gt;
     y= 7, 9, 8&lt;br /&gt;
     enemy_x=13,13&lt;br /&gt;
     enemy_y= 8, 9&lt;br /&gt;
 &lt;br /&gt;
     healer_x=14,14,15,15&lt;br /&gt;
     healer_y= 7, 9, 8, 9&lt;br /&gt;
     leadership_x=14,14,15,15&lt;br /&gt;
     leadership_y= 7, 9, 9 ,8&lt;br /&gt;
     active_side_leader=yes&lt;br /&gt;
 [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Fast Micro AI (ai_type=fast_ai) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|0}}&lt;br /&gt;
&lt;br /&gt;
This AI is meant as a fall-back AI for scenarios where there are so many units that attacks and moves done by the default AI are slow with long delays before each move.  It replaces the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions (CAs).  It should be obvious that in order to achieve the faster speed, compromises concerning the quality of the moves have to be made.&lt;br /&gt;
&lt;br /&gt;
Enable the Fast Micro AI by using&lt;br /&gt;
 ai_type=fast_ai&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Combat candidate action:'''&lt;br /&gt;
* Attackers are considered individually one at a time.  The best attack found for a unit is executed without checking whether a better attack by another unit might be possible.&lt;br /&gt;
* The default order in which attackers are considered is by decreasing hitpoints, starting with the strongest unit (in terms of HP).  This can be modified with the optional keys below.&lt;br /&gt;
* By default, if weaker units block attack hexes for a stronger unit, they do not move out of the way (this is the same as for the default AI).  If a hex becomes available after an attack (e.g. due to a weaker unit dying in the attack), all attacks that were possible ''before'' this happened are finished first.  Only after that are attacks by the stronger unit reconsidered.&lt;br /&gt;
* If include_occupied_attack_hexes=yes is set, weaker units move out of the way for stronger units if possible.  This can, however slow down the AI depending on the situation on the map.  Use with caution.&lt;br /&gt;
* {{DevFeature1.13|2}} Previous to this version, attacks by the leader were bunched in with the attacks by the other units.  Now leader attacks are treated separately and contain an extra layer of analysis to determine whether the attack position is &amp;quot;safe&amp;quot; (in an approximate way).  Currently, this is done with two simple considerations:&lt;br /&gt;
** Count all enemy units that can reach the leader's attack position.  If there are more than given by optional parameter 'leader_attack_max_units' (default value 3), do not attack.&lt;br /&gt;
** Add up the hitpoints of all enemies that can reach the leader's attack position.  If this value, multiplied by optional parameter 'leader_weight'(default value 2), is larger than the leaders hitpoints, do not attack.&lt;br /&gt;
** Two optional parameters, 'threatened_leader_fights' and 'leader_additional_threat', can also be used to let the leader attack when the threat at the attack hex is comparable to that at the leader's current position.&lt;br /&gt;
** In order to keep this AI as fast as possible, leader attacks are done by a separate candidate action that is only evaluated when all other CAs are done.&lt;br /&gt;
&lt;br /&gt;
'''Move-to-targets, villages and retreat candidate actions:'''&lt;br /&gt;
* The move-to-targets, villages and retreat candidate actions of the default AI are removed.  Move-to-targets and villages are taken over by the Fast Micro AI, whereas retreat is simply not done.&lt;br /&gt;
* This CA moves units toward goals after attacks are done.  Goals are unowned (if the AI side has a leader only) and enemy-owned villages and enemy leaders.&lt;br /&gt;
* For each goal, units are pre-sorted in order of the fewest moves needed to cover the ''distance'' toward the goal (that is, as if move cost on each hex toward the goal were 1).  Units are then considered sequentially and if a unit can get there &amp;quot;efficiently&amp;quot;, its move is executed without considering other units.&lt;br /&gt;
** A move is considered efficient if the move cost to get to the hex is not more than 'move_cost_factor' larger than the distance to the hex.  'move_cost_factor' is an optional key that defaults to 2.  It can be set to values of 1.1 or larger (see below).&lt;br /&gt;
** If the move for the current unit is not found to be efficient, the AI considers the next unit instead.  If no unit can do an efficient move toward the goal, the best (fastest) of the inefficient moves is selected instead.&lt;br /&gt;
&lt;br /&gt;
* Village goals:&lt;br /&gt;
** Village goals are considered before enemy leader goals so that the AI preferentially chooses scouts (or otherwise fast units) for them.&lt;br /&gt;
** The number of units sent toward villages depends on the value of the default AI aspect village_value.  It is set to village_value/4 times the number of units the AI has left to move.  Thus, by default (village_value=1.0) the AI sends a quarter of its units toward villages, and the rest toward enemy leaders.  This means that the meaningful range of values for village_value is 0..4.&lt;br /&gt;
** Only one unit is moved toward each village.  This consideration also overrides the number of units to move toward villages defined by village_value, topping it at the number of unowned/enemy-owned villages on the map.&lt;br /&gt;
** Setting village_value=0 disables moving units toward villages altogether.  That is, not only are no units chosen, but the village targeting phase of the AI is entirely disabled with no evaluation time used for it.  Note that, unlike with the default AI, this also means that no villages within range of an AI unit are grabbed.&lt;br /&gt;
** The order in which villages are chosen as goals is some combination of them being close to the AI side's leader and being spread out over the map.&lt;br /&gt;
&lt;br /&gt;
* Enemy leader goals:&lt;br /&gt;
** The order of enemy leaders goals is set according to their distance from the AI leader with the closest enemy leader being targeted first (if the AI has a leader, otherwise their order is random).  One unit is sent toward each enemy leader in this order, after which the AI starts with the first enemy leader again.  This is repeated until all units have moved.&lt;br /&gt;
&lt;br /&gt;
* Guardians (units with ai_special=guardian set) are ignored by the fast move-to-targets CA.&lt;br /&gt;
&lt;br /&gt;
'''Tips for speeding up/modifying the Fast Micro AI in specific scenarios:'''&lt;br /&gt;
&lt;br /&gt;
A number of optional keys and default AI aspects can be used to modify the behavior of the Fast Micro AI.  The MAI is set up so that it is a reasonable compromise between speed and quality of the moves in most situations.  However, this might not be ideal for all scenarios here are some things that can be done to speed up the AI (or to get better behavior in some situations by doing the opposite):&lt;br /&gt;
* Use large values for the default AI aspect aggression.  The largest delays before attacks happen (in some circumstances) when the Fast MAI has to consider lots of units with attacks left but decides not to attack with them.  See below for a description of how aggression is used by the fast AI.&lt;br /&gt;
* Use large values of move_cost_factor (see above for description of what this does).  If you want to emphasize units that can move across terrain easily (e.g. bats) to be chosen for village grabbing, you might want to use low values of move_cost_factor, but this might slow down the AI.  The default value should work reasonably well in most standard situations, but modifying the value might work better on specific maps.&lt;br /&gt;
* Don't use dungeon_mode=yes unless absolutely necessary, even in most dungeon crawls.  Test the AI without this setting first and only use it if it is the only way to solve the problem.  Often, a minor tweak to the map will have the same effect, without the speed penalty.&lt;br /&gt;
* Use include_occupied_attack_hexes=yes with caution.  It might (or might not) slow down the attack CA significantly, depending on the scenario.&lt;br /&gt;
* Set village_value=0, which makes the AI not consider villages as goals.  This has not been found to make a large difference on the maps tested so far, but it might have an effect on maps with a very large number of villages.&lt;br /&gt;
* Use attack_hidden_enemies=yes on maps where there are no hidden enemies. Excluding hidden enemies from the attack calculation requires an additional filter to be evaluated that takes extra time. In most cases this will not result in a large computation time saving.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_hidden_enemies'''=no: (boolean)  By default, hidden enemies are excluded from the attack calculation.  Setting this to 'yes' makes the AI also consider those enemies as attack targets.&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.13|2}} A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  As with the default AI, units will not step on hexes defined by the [avoid] tag.  In addition, no goals will be chosen that lie on those hexes.  The one minor exception is that a unit stepping out of the way for another unit might temporarily enter the avoided zone.&lt;br /&gt;
* '''dungeon_mode'''=no: (boolean)  As much as possible, the Fast Micro AI works with distances between hexes rather than path finding.  This works pretty well on open maps and even most dungeon crawls (and similar scenarios), but it might result in some units getting temporarily (and in rare cases permanently) stuck on maps with convoluted labyrinths.  If that is the case, setting dungeon_mode=yes triggers more path finding that avoids such situations (but as a result is somewhat slower).&lt;br /&gt;
* '''include_occupied_attack_hexes'''=no: (boolean) If set to 'yes', the AI moves weaker units out of the way for a stronger attacker.  This might lead to better attacks but is slower, so don't use if speed is essential.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the attackers.  Only AI units matching this filter will attack, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes the side= key set to the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the enemies.  Only enemy units matching this filter will be attacked, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''leader_weight'''=2: (number &amp;gt;= 0) {{DevFeature1.13|2}} A factor specifying how much more valuable the AI considers side leaders than non-leader units in the attack evaluation. This is also used to determine whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''leader_additional_threat'''=1: (number &amp;gt; 0) {{DevFeature1.13|2}} If threatened_leader_fights=yes, the threat at the attack position may be larger by this factor for the leader to leave the keep for an attack.&lt;br /&gt;
* '''leader_attack_max_units'''=3: (number &amp;gt;= 0) {{DevFeature1.13|2}} A number specifying how many enemy units within reach are acceptable when determining whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''move_cost_factor'''=2: (number &amp;gt;= 1.1)  The factor by which the move cost to a goal hex may be larger than the distance to the goal hex for a move to be considered &amp;quot;efficient&amp;quot;.  See description above.  1.1 is used if this is set to values smaller than 1.1.  Smaller values might lead to better behavior, while larger values might speed up the AI.&lt;br /&gt;
* '''weak_units_first'''=no: (boolean) If set to 'yes', inverts the order in which units are considered by the combat CA.  Attacks are then done in order of increasing hitpoints of the attackers.&lt;br /&gt;
* '''skip_combat_ca'''=no: (boolean) If set to 'yes', the fast combat CA is not used.  The default AI's high_xp_attack and combat CAs remains in place in this case.&lt;br /&gt;
* '''skip_move_ca'''=no: (boolean) If set to 'yes', the fast move-to-targets CA is not used.  The default AI's retreat, villages and move-to-targets CAs remains in place in this case.&lt;br /&gt;
* '''threatened_leader_fights'''=no: (boolean) {{DevFeature1.13|2}} By default, the AI leader will not attack if the threat at the attack position is too high.  If this key is set to 'yes', the leader will attack if the threat there is not larger than the threat at his current position multiplied by the value of 'leader_additional_threat'. This is meant as an option for the endgame: when the AI leader is under attack, he might as well go down fighting.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''aggression''': The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to set how aggressive the AI should attack.  It works in roughly the same way as the default AI aspect:&lt;br /&gt;
** The value of (1 - aggression) determines how much the AI values its own units compared to the enemy's units.  Thus, aggression=0 means that attacks are only done if the AI expects to deal (roughly) the same amount of damage as it will receive.  aggression=1 means that expected damage to the AI's units is not considered, only the expected damage to enemy units matters.&lt;br /&gt;
** Damage is calculated as a combination of hitpoints and chance to die, in &amp;quot;gold equivalent units&amp;quot;.&lt;br /&gt;
** Unlike the default AI, aggression is set to 1 for values larger than 1.  Allowed values are thus anything from -infinity to +infinity, although only -infinity to +1.0 make sense.&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.13|2}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
*'''attacks aspect''':  {{DevFeature1.13|2}} The standard RCA AI [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] used to filter own and enemy units for attacks is now also taken into account as long as neither [filter] nor [filter_second] (see above under 'optional keys') are defined in the [micro_ai] tag.&lt;br /&gt;
*'''village_value=1''': (number ranging from 0 to 4) The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to define how much villages should be targeted by the move-to-targets CA.  In the Fast MAI, this defines which fraction of the AI units are moved toward village goals (see above for details).  If set to 0, villages are ignored as goals.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded to the same values as those of the respective [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|default candidate actions]].  ca_score is therefore not a valid optional key.&lt;br /&gt;
* The fast Micro AI modifies (removes) the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions.&lt;br /&gt;
&lt;br /&gt;
Enabling the Fast Micro AI in its default configuration is as simple as this usage example from the &amp;quot;Fast AI&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=fast_ai&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Generalized Goto Micro AI (ai_type=goto) ==&lt;br /&gt;
&lt;br /&gt;
The Goto Micro AI provides a highly configurable method of sending a unit (or units) to a location or set of locations.  The units to be moved are defined using a [[StandardUnitFilter|Standard Unit Filter]] (SUF) [alternatively, all side units are used if the SUF is not provided), while the goto locations are given in a [[StandardLocationFilter|Standard Location Filter]] (SLF).&lt;br /&gt;
&lt;br /&gt;
By default, each unit matching the SUF will move toward the closest goal (in terms of turns needed to get there) matching the SLF via the fastest route given the current situation on the map (locations of enemy units etc.), but this behavior can be influenced by the optional keys listed below.  Note that this means that a unit's goal might change from one turn to the next depending on the situation on the map.&lt;br /&gt;
&lt;br /&gt;
The behavior of the unit(s) once they reach their goal location(s) is configurable as well.  By default, they will stay there until the Goto Micro AI is deleted or changed.&lt;br /&gt;
&lt;br /&gt;
For demonstrations of several Goto Micro AI usages, check out the &amp;quot;Goto&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Goto Micro AI by using&lt;br /&gt;
 ai_type=goto&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Goto specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the possible goal locations for the move.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists). Note that the area defined by [avoid] is not entered at all, even if the end point of the move would lie outside the area again.&lt;br /&gt;
* '''avoid_enemies''': (number&amp;gt;0) By default, units controlled by the Goto Micro AI will not take enemy units into account for route finding, other than the enemies' ZoCs.  By contrast, if this key is set, they will try to avoid traversing hexes on which they can be attacked by enemies.  The larger the value of ''avoid_enemies'', the more the units will try to stay out of the enemies' way.  Notes:&lt;br /&gt;
** Units are willing to take a path that is 'avoid_enemies' movement points longer for each enemy unit that can be avoided by doing so for each hex along the path.  As an example, if avoid_enemies=1 and the shortest path crosses 2 hexes that can be reached by 3 enemies each, the unit is willing to take a path that is up to 6 hexes longer instead.  If avoid_enemies=10 in the same situation, the unit will take a path up to 60 hexes longer.&lt;br /&gt;
** Hexes adjacent to enemy units are always strongly avoided, independent of the value of avoid_enemies.&lt;br /&gt;
** avoid_enemies is ignored if 'use_straight_line=true' is set.&lt;br /&gt;
** Due to an issue with the path finding code, '''tunnels and teleports are ignored in Wesnoth 1.14 and before''' if 'avoid_enemies' is used. This has been fixed in Wesnoth 1.15.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units to be moved.  Defaults to all units of the side if not defined.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''ignore_enemy_at_goal'''=no and '''ignore_units'''=no: (boolean)  In the default configuration, the Goto MAI does not move units if the path to all goal hexes is blocked by enemies.  It is therefore possible to disable the Goto MAI by putting enemy units on all the goal hexes.  That can be changed if either of these two keys is set to 'yes'.&lt;br /&gt;
** 'ignore_enemy_at_goal=yes' makes the AI choose its goals while disregarding any enemy units at the goal hexes.  Even so, a stiff rating penalty is applied to goal hexes occupied by enemies, thus that unoccupied goal hexes are chosen unless all possible goals are occupied by enemies.  Note that this setting ''only'' ignores enemy units at the goal hexes themselves.  It is still possible to disable the AI by putting enemies all around the goals.&lt;br /&gt;
** 'ignore_units=yes' let's the AI ignore ''all'' enemy units when deciding which goal hex(es) to move toward.&lt;br /&gt;
** In both cases, enemies are only ignored for deciding which unit to move toward which goal hex.  For the actual move itself, all enemies and their ZoCs are considered as usual.&lt;br /&gt;
** Both options only take effect if neither avoid_enemies is used, nor use_straight_line is set to 'yes', as neither of these consider enemies as blockers. Or in other words, if avoid_enemies is used, or use_straight_line=yes, it is unnecessary to set ignore_enemy_at_goal or ignore_units to yes.&lt;br /&gt;
* '''release_unit_at_goal'''=no: (boolean) By default, a unit that reaches a goal location will stay there until the Goto Micro AI is deleted or changed (or until the conditions specified in the [goto_goals] SLF change).  If this parameter is set, a unit that has reached its goal will not be controlled by this Micro AI any more starting from the next turn.&lt;br /&gt;
* '''release_all_units_at_goal'''=no: (boolean) Same as ''release_unit_at_goal'', but this will deactivate the Goto Micro AI for all units once the first unit reaches a goal.&lt;br /&gt;
* '''remove_movement'''=yes: (boolean):  {{DevFeature1.15|11}}  By default, when the Goto MAI cannot move a unit (for example, because the path is blocked by enemies), its movement points are still removed. If this option is set to 'no', this step does not happen, so that other AI candidate actions (or even another Goto MAI with different parameters) can take over instead.&lt;br /&gt;
* '''unique_goals'''=no: (boolean)  If set to 'yes', each unit will be sent to a different goal location.  Note that the best goals are recalculated each turn, so a unit might choose a different goal this turn from what it was heading for during the previous turn if the situation on the map has changed.  Also note that if there are more units than goal locations, the left over units will not be handled by the Goto MAI but follow default behavior instead.&lt;br /&gt;
* '''use_straight_line'''=no: (boolean)  By default, units choose the route by which they will get to their goal locations in the smallest number of turns.  If this parameter is set to 'yes', a straight line route (or something pretty close to it at least) will be chosen instead, irrespective of whether this leads into a dead end, whether it is blocked by enemies, etc.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* Depending on the parameters used, the Goto Micro AI might require a lot of path finding to be done.  Thus, while it is possible to set up this AI to move many units to many different locations, a usage like that might require quite a long calculation time.&lt;br /&gt;
&lt;br /&gt;
===Goto Micro AI example usages:===&lt;br /&gt;
&lt;br /&gt;
Here are several examples of goto [micro_ai] tag usages from the &amp;quot;Goto&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
* Send a unit toward a castle (similar to the RCA AI's Goto Candidate Action, but avoiding dead ends and areas ZoCed by enemies)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
             ca_id=messenger1&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=messenger1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=6-7,2-4&lt;br /&gt;
                 terrain=C*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             ca_score=210001&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send all units of the side toward the southern border of the map&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=8&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                  y=33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             avoid_enemies=1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send units to the four corners of the map (with each unit going to a different corner).&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x=1,1,44,44&lt;br /&gt;
                 y=1,33,1,33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             unique_goals=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Set up a guardian unit that doesn't move while no enemy unit is within 8 hexes (ironically, the Goto Micro AI can be used to keep units in place, although using one of the Guardian Micro AIs might often be a better idea)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=guard1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 [filter]&lt;br /&gt;
                     id=guard1&lt;br /&gt;
                 [/filter]&lt;br /&gt;
                 [not]&lt;br /&gt;
                     [filter]&lt;br /&gt;
                         side=1&lt;br /&gt;
                     [/filter]&lt;br /&gt;
                     radius=8&lt;br /&gt;
                 [/not]&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Guardian Micro AIs ==&lt;br /&gt;
=== General Comments on the Guardian Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Guardian Micro AIs can be used to set up different kind of guardian behaviors for a unit or a set of units.  Four different types of guardians are available and are described below.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different guardian behaviors, check out the &amp;quot;Guardians&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
=== Return Guardian  (ai_type=return_guardian)===&lt;br /&gt;
&lt;br /&gt;
A 'return guardian' is a variation of the standard Wesnoth guardian. It has an assigned guard position (GP) to which it returns after attacks on approaching enemies:&lt;br /&gt;
* If at GP with no enemy in reach, do nothing.&lt;br /&gt;
* If at GP with an enemy in reach, attack. Note that the attack is done by the default AI, which might result in the guardian not attacking if the enemy is deemed too strong (this can be influenced by setting the default ''aggression'' value to a different value).&lt;br /&gt;
* If not at GP, return there, no matter whether an enemy is in reach or not.&lt;br /&gt;
* If enemies are blocking the way back to GP, do your best to move around them.&lt;br /&gt;
* If the guardian ends up next to an enemy on the way back, attack that enemy after the move.&lt;br /&gt;
&lt;br /&gt;
Enable the Return Guardian Micro AI by using&lt;br /&gt;
 ai_type=return_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''return_x,return_y''': The coordinate of the hex the unit returns to after each attack.  {{DevFeature1.15|0}} '''return_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=100010: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  This AI consist of 2 CAs, one with score &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt; the other with &amp;lt;tt&amp;gt;ca_score-20&amp;lt;/tt&amp;gt;.  The default value is set so that the first CA gets executed right before the default combat CA, and the other right after.  It usually will not make sense to change this value, but it is possible to do so for custom applications.  {{DevFeature1.13|6}}: This is now changed to 100100 and 99900 in order to avoid conflicts with the new high_XP_attacks candidate action.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a return guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=return_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=return1&lt;br /&gt;
             return_x,return_y=20,2&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
===Stationed Guardian (ai_type=stationed_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A 'stationed guardian' is another variation of the standard Wesnoth guardian with a somewhat more complex behavior than that of the 'return guardian'. Two positions are defined for it, a 'station' and a 'guarded location', as well as a distance. The behavior is as follows:&lt;br /&gt;
* If no enemy is within 'distance' of the guard's current position, do nothing.  This is independent of the guardian being at its station or not.&lt;br /&gt;
* Otherwise: If an enemy is within 'distance' of the guard, but not also within the same distance of the guarded location and the station (all of this simultaneously), move the guard in the direction of the station (or keep the guard in place if the station is unreachable).&lt;br /&gt;
* Otherwise:&lt;br /&gt;
** Pick the enemy unit that is closest to the guarded location.&lt;br /&gt;
** If we can reach it, pick the adjacent hex with the highest defense rating and attack from there.&lt;br /&gt;
** If not in reach, move toward this unit.&lt;br /&gt;
&lt;br /&gt;
Enable the Stationed Guardian Micro AI by using&lt;br /&gt;
 ai_type=stationed_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance parameter as explained above&lt;br /&gt;
* '''station_x,station_y''': The x and y position of the hex that serves as the guardians station. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''guard_x, guard_y''': The x and y position of the guarded location.  {{DevFeature1.13|0}}  These are now optional keys, defaulting to station_x,station_y if not provided. {{DevFeature1.15|0}} '''guard_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a stationed guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=stationed_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=stationed1&lt;br /&gt;
             distance=4&lt;br /&gt;
             station_x,station_y=2,14&lt;br /&gt;
             guard_x,guard_y=3,13&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Coward (ai_type=coward) ===&lt;br /&gt;
&lt;br /&gt;
Cowards are units that, like guardians, sit around doing nothing until an enemy comes into range.  Unlike guardians, however, they run away once enemies approach. Applications might be wild animals, unarmed civilians getting in the way of a battle, etc.  The coward macro can be called with two optional locations, 'seek' and 'avoid':&lt;br /&gt;
* If neither is given, the coward retreats to the position farthest away from the approaching enemies.&lt;br /&gt;
* If 'seek' is given, it preferentially goes toward that location (but getting away from enemies takes priority).&lt;br /&gt;
* If 'avoid' is given, it in addition tries to avoid that location (with both maximizing distance from enemies and going toward 'seek' taking priority).&lt;br /&gt;
* Both 'seek' and 'avoid' may consist of only one coordinate ('x' or 'y'), in which case not a single hex, but a line of hexes is sought or avoided.&lt;br /&gt;
&lt;br /&gt;
Enable the Coward Micro AI by using&lt;br /&gt;
 ai_type=coward&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance within which an enemy must be to &amp;quot;scare&amp;quot; the unit&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_if_trapped'''='no':  {{DevFeature1.13|0}}  With the default settings, a coward never attacks.  If this key is set to 'yes', the coward attacks the weakest (lowest hitpoints) enemy next to which it ends up after moving (such as a cornered animal would do, blindly lashing out without regards for its own safety).&lt;br /&gt;
* '''avoid_x, avoid_y''': The x and y coordinate of the hex to avoid. {{DevFeature1.15|0}} '''avoid_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units from which the cowards flee.  If not set, this defaults to all enemy units.  Note, however, that no &amp;quot;enemy of AI side&amp;quot; condition is added if [filter_second] is specified, meaning that it can include allied and/or enemy units as desired (and even units on the same side as the coward).&lt;br /&gt;
* '''seek_x, seek_y''': The x and y coordinate of the hex to seek. {{DevFeature1.15|0}} '''seek_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a coward [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=coward&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=coward3&lt;br /&gt;
             distance=5&lt;br /&gt;
             seek_x,seek_y=24,5&lt;br /&gt;
             avoid_x,avoid_y=24,15&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Zone guardian (ai_type=zone_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A zone guardian is a unit that, as the name says, guards a zone. It moves randomly inside this zone until an enemy enters it (or a separately defined enemy zone, see below). Applications might be the defense of a castle, a nesting area or for protecting an unit.  The zone macro can be called with an optional SLF, [filter_location_enemy]:&lt;br /&gt;
* If not specified, the zone guard attacks any enemy coming inside its guard zone.&lt;br /&gt;
* If [filter_location_enemy] is given, it attacks any enemy entering this zone and once there are no more enemies, it goes back to patrol in its basic zone.&lt;br /&gt;
&lt;br /&gt;
Enable the Zone Guardian Micro AI by using&lt;br /&gt;
 ai_type=zone_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone to guard.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_enemy]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone where enemies have to be attacked.  If not given, it defaults to [filter_location].&lt;br /&gt;
* '''station_x,station_y''': By default, the zone guardian moves randomly within the zone if there is no enemy to attack.  However, if station_x/y is given, it moves to this position instead during turns when it does not have an enemy to attack.  This position does not need to be inside the zone defined above.  Note: obviously it does not make sense to define [filter_location], [filter_location_enemy] and station_x/y all at the same time.  Only [filter_location] and either one of the latter two (or none of them) should be provided for any zone guardian. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a zone guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=zone_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=zone3&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=22-31,4-11  # This is intentionally chosen to extend past the lake&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             station_x,station_y=32,8&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Hang Out Micro AI (ai_type=hang_out) ==&lt;br /&gt;
&lt;br /&gt;
The Hang Out Micro AI instructs units of the AI side to &amp;quot;hang out&amp;quot; around a certain location, or locations, until a condition for mobilizing the side is met.  If none of the optional keys are given, the AI controls all units of the side to hang around the leader while moving off castle tiles to make room for recruiting.&lt;br /&gt;
&lt;br /&gt;
The AI works by moving each unit to its best hex first using the minimum amount of movement points required for this move.  Once all AI units are in position, movement points are set to zero for all of them simultaneously such that they cannot be used by other AI candidate actions.  This is done because the mobilizing condition might be met as result of one of the moves of the Hang Out AI, after which we want units to be able to continue with other AI actions.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Hang Out and Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Hang Out Micro AI by using&lt;br /&gt;
 ai_type=hang_out&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Hang Out specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  In its default configuration, the Hang Out MAI controls all units of a side, keeping them as close to the side leader(s) as possible while moving off castle tiles to make room for recruiting.  For this purpose, the candidate action score defaults to 170,000, which is just below the score of the default recruiting CA.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''': A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  If neither of these two exists, this parameter is set to default to all castle terrain (&amp;lt;tt&amp;gt;terrain=C*,C*^*,*^C*&amp;lt;/tt&amp;gt;) so that units move off the castle to make room for recruiting.  Note that this does not include keep terrain, otherwise the leader would be moved off the keep as well.&lt;br /&gt;
* '''ca_score'''=170000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 170,000, which means that it is executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Hang Out action (&amp;lt;tt&amp;gt;side=&amp;lt;/tt&amp;gt; is always set and does not need to be provided).  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the locations ''near'' which the AI units should hang out.  This does not mean that the units only move onto terrain specified here, but that they minimize the distance to such terrain.  It defaults to the position of the side leader(s).&lt;br /&gt;
* '''[mobilize_condition]''':  Contains ([[ConditionalActionsWML#Condition_Tags|WML Condition Tags]])  Once the condition set up here has been met, the Hang Out MAI is disabled and the side goes over to other AI activity.  The Hang Out MAI remains disabled after this, even if the condition is not met any more later in the scenario.&lt;br /&gt;
* '''mobilize_on_gold_less_than''': (number) Since side gold cannot be checked in a WML condition tag without storing it in a variable first, this key is provided separately.  It disables the Hang Out Micro AI once the side gold goes below the value specified here for the first time.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
* The Hang Out AI still takes effect if no locations to hang out at are found or if all map locations are to be avoided.  In that case, it simply keeps all units at their current locations.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Hang Out Micro AI configuration from the &amp;quot;Hang Out and Messenger&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hang_out&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [avoid]&lt;br /&gt;
                 terrain=C*,H*,M*,A*,S*,*^F*&lt;br /&gt;
             [/avoid]&lt;br /&gt;
             [mobilize_condition]&lt;br /&gt;
                 [have_unit]&lt;br /&gt;
                     side=1&lt;br /&gt;
                     count=7-99&lt;br /&gt;
                 [/have_unit]&lt;br /&gt;
             [/mobilize_condition]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Healer Support Micro AI (ai_type=healer_support) ==&lt;br /&gt;
&lt;br /&gt;
The Healer Support Micro AI configures the healers of a side to stay behind the battle lines and heal injured and/or threatened units rather than participate in combat under all circumstances.  You can set different levels of aggressiveness for the healers.  &lt;br /&gt;
&lt;br /&gt;
'''Notes'''&lt;br /&gt;
* By default, ''all'' healers of the AI side are controlled by this AI.  This includes in particular also the side leader, if the leader is a healer.  If you do not want the leader to be included (and possibly be moved away from the keep etc.) use the [filter] tag with canrecruit=no (see below).&lt;br /&gt;
* It is not always better to use this Micro AI than the default AI, as it takes healers (which can be strong units) away from the pool of units to choose for attacks.&lt;br /&gt;
* {{DevFeature1.15|3}} This Micro AI now takes unit guardian status and the passive_leader aspect into account.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Healers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Healer Support Micro AI by using&lt;br /&gt;
 ai_type=healer_support&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Healer Support specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the healer units.  If set, only units on the AI side passing this filter ''and'' having the healing ability are considered by this Micro AI.&lt;br /&gt;
*'''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting units to receive healing. If set, only units on the AI side passing this filter ''and'' the other healee selection criteria will be selected for healing by this Micro AI.&lt;br /&gt;
* '''aggression'''=1.0: (float) Sets the aggressiveness of the AI.  This parameter is set up as a float to accommodate future functionality, but it currently acts as a boolean: if set to zero, the AI will never let its healers participate in combat, for all other values it allows them to attack after all other units have attacked and if the healers cannot find any more units to support.  The former behavior might be appropriate in scenarios with only one or few valuable healers, while the latter might work better in scenarios with many healers.&lt;br /&gt;
* '''injured_units_only'''=no: (boolean) If set to 'yes', the AI will only move healers next to units that are already injured and skip units that currently have full hitpoints, but might get injured during the next enemy turn.&lt;br /&gt;
* '''max_threats'''=9999: (integer) The maximum allowable number of enemies that can attack a hex in order for it to be considered for a healer.  As an example, setting 'max_threats=0' means that the AI only moves healers to locations that are entirely safe from the enemy (assuming that none of the units currently on the map dies).  Note that the value of this key is checked against the number of enemies that can make it to the hex, not the number of adjacent hexes from which the healer could be attacked.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI. Note: as attacks by the healers are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The Healer Support Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded.  It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Healer Support Micro AI configuration from the &amp;quot;Healers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             aggression=0&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Lurkers Micro AI (ai_type=lurkers) ==&lt;br /&gt;
&lt;br /&gt;
The Lurker Micro AI controls some or all units of a side in &amp;quot;lurker mode&amp;quot;.  A lurker is a unit that is capable of moving across most terrains, but that only stops on and attacks from specific terrain.  It might also have the ability to hide on this terrain (which is the reason why it is called a lurker).&lt;br /&gt;
&lt;br /&gt;
Lurkers move individually without strategy and always attack the weakest enemy within their reach.  If no enemy is in reach, the lurker does a random move instead - or it just sits and waits (lurks).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of different types of lurker behavior, check out the &amp;quot;Lurkers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Lurkers Micro AI by using&lt;br /&gt;
 ai_type=lurkers&lt;br /&gt;
in the [micro_ai] tag&lt;br /&gt;
&lt;br /&gt;
===Lurkers specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
* '''[filter]''': Specifies which units of the side are controlled by the Lurker Micro AI.  This is a [[StandardUnitFilter|Standard Unit Filter]].  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': Specifies the terrain from which the lurkers attack. This is a [[StandardLocationFilter|Standard Location Filter]].&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_wander]''': Specifies the terrain on which lurkers end their moves if there is no enemy in reach. This is a [[StandardLocationFilter|Standard Location Filter]].  If [filter_location_wander] is not given, it defaults to [filter_location].&lt;br /&gt;
* '''stationary'''=no: (boolean)  If set to yes, a lurker never moves if there is no enemy within attack range.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Lurkers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=4&lt;br /&gt;
             ai_type=lurkers&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Naga Fighter&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=W*,S*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             [filter_location_wander]&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location_wander]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Messenger Escort Micro AI (ai_type=messenger_escort) ==&lt;br /&gt;
&lt;br /&gt;
The Messenger Escort Micro AI lets you define a set of waypoints through which it tries to move one or several of its units, the messenger(s).  The other units escort the messenger, trying to protect it and attacking enemies that are in its way.  Note that a messenger might not strictly hit intermediate waypoints, but that getting close is good enough, especially if a waypoint is occupied by an enemy unit.  It does, however, always try to get to the last waypoint.&lt;br /&gt;
&lt;br /&gt;
'''Notes on the use of several messengers:''' {{DevFeature1.13|0}}  &lt;br /&gt;
* This Micro AI can control several messengers at the same time, which can but do not have to move in a group.  They can be put on the map altogether in one location, or be spread out on the map or across different turns.&lt;br /&gt;
* The farther a messenger has moved through the chain of waypoints, the higher a priority it is for the AI, meaning that farther advanced (on the map) messengers are moved first, and that the escort units try to clear the way and stay with those messengers more so than with those further back on the map.  This order can be inverted using the invert_order= key.&lt;br /&gt;
* If several messengers are on the map, escort units might move from one to the other, the AI making a judgement call where they are needed most.  Closeness to a messenger is generally the most important criterion, but not the only one.&lt;br /&gt;
* If a messenger dies, the escort units previously staying with it now join one of the other messengers if there are any left (otherwise they follow default AI behavior).&lt;br /&gt;
* If you want certain escort units to stay always with the same messenger, use several messenger [micro_ai] tags with different [filter]/[filter_second] combinations.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Messenger Escort Micro AI by using&lt;br /&gt;
 ai_type=messenger_escort&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Messenger Escort specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': {{DevFeature1.13|0}} (Previous to 1.13.0, only id= was allowed.) The id of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated list of waypoint coordinates through which the messenger will go in the given order to reach its goal, given by the last coordinate.  If you want the messenger to go directly to the goal, simply enter a single x,y coordinate here.  Note that the messenger does not have to hit each waypoint exactly (except for the final one), getting close to them is good enough. {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|11}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Messenger Escort AI consists of 3 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-2&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_death_chance'''=0.67 and '''messenger_death_chance'''=0.0: (both floats; ranging from 0 to 1)    If the messenger is adjacent to an enemy at the end of the move, it will only attack the enemy if the chance of death of the enemy is &amp;gt;= enemy_death_chance and if the messenger's chance of death is &amp;lt;= messenger_death_chance (or if it is an attack at a range for which the enemy does not have a weapon).&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|0}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the escort units.  If not set, this defaults to all enemy units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''invert_order'''=no: {{DevFeature1.13|0}} If set to 'yes', the order of priority with which messenger units are considered is inverted, starting with the messenger unit that is the least advanced on the map (see description above).  In most cases, selecting this option results in worse performance of the AI, so use it with caution.  (And obviously it has no effect at all if there is only one messenger controlled by the AI.)&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|11}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Messenger Escort&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=messenger_escort&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=messenger&lt;br /&gt;
             waypoint_x=31,24,27,28&lt;br /&gt;
             waypoint_y=20,14,7,1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Patrol Micro AI (ai_type=patrol) ==&lt;br /&gt;
&lt;br /&gt;
The defines a number of waypoints for a unit (or units) to follow.  Options can be set whether the route should be followed in a loop, as an out-and back, only once, whether units encountered along the way should be attacked, etc.&lt;br /&gt;
&lt;br /&gt;
No matter what options are chosen, getting to the next waypoint always takes priority over attacking for a patroller.  The AI thus prefers to move the patrol unit around enemies rather than straight for them.  Also, if a waypoint is occupied by a unit the AI is not instructed to attack, it will (eventually) abandon that waypoint once it gets close enough and move on to the next one.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different patrol behaviors, check out the &amp;quot;Patrols&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Patrol Micro AI it by using&lt;br /&gt;
 ai_type=patrol&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Patrol specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated lists of the waypoint coordinates through which the patrol travels (in order). {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack''': By default (if this key is not set), the patrol unit attacks any unit it ends up next to after a move.  Alternatively, this parameter can be set to a comma-separated list of unit ids. If that is done, the patrol only attacks these units when it ends up next to them and ignores any other unit.  This can in turn be used to have the patroller not attack at all, by setting ''attack='' to a non-existing id.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''one_time_only'''=no: (boolean)  If set to 'yes', the unit goes through the patrol route only once and then hangs out at the last waypoint.  It also always attacks any enemy unit on the last waypoint, independent of what ''attack='' is set to.&lt;br /&gt;
* '''out_and_back'''=no: (boolean)  By default, a patrol unit returns to the first waypoint after reaching the last, doing the patrol route as a loop.  If this parameter is set to 'yes', the unit instead reverses its course and perpetually does out and backs of the route.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Patrols&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=patrol&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Konrad&lt;br /&gt;
             waypoint_x=9,24,25&lt;br /&gt;
             waypoint_y=21,23,15&lt;br /&gt;
             one_time_only=yes&lt;br /&gt;
             attack=Gertburt&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Protect Unit Micro AI (ai_type=protect_unit) ==&lt;br /&gt;
&lt;br /&gt;
The Protect Unit Micro AI tries to move one or several units to a given location while keeping them safe from enemy attacks. It does so by keeping the protected units away from enemy units and close to friendly units. Other units on the AI side are controlled by the default Wesnoth AI.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Protect Unit&amp;quot; and &amp;quot;The Elves Besieged&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Protect Unit Micro AI by using&lt;br /&gt;
 ai_type=protect_unit&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Protect Unit specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[unit]''': A separate [unit] tag is required for each protected unit(s).  Each tag has required keys '''id''', '''goal_x''', '''goal_y''' for each unit, as shown in the example below. {{DevFeature1.15|0}} '''goal_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''disable_move_leader_to_keep'''=no: (boolean) If set to 'yes', side leaders protected by this AI do not try to return to their keep.  This is necessary for this Micro AI to work with units that are side leaders and that are supposed to move to an off-keep location.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
*The Protect Unit Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].  Depending on the parameters, it might also modify the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|move leader to keep candidate action]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded. It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;HttT: The Elves Besieged&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=protect_unit&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Delfador&lt;br /&gt;
                 goal_x,goal_y=1,2&lt;br /&gt;
             [/unit]&lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
                 goal_x,goal_y=1,1&lt;br /&gt;
             [/unit]&lt;br /&gt;
 &lt;br /&gt;
             disable_move_leader_to_keep=true&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Recruiting Micro AI==&lt;br /&gt;
=== General Comments on the Recruiting Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Recruiting Micro AI replaces the default recruitment AI by alternative recruiting patterns.  Currently there are two recruiting algorithms you can choose from, random recruiting and the rush recruiting used in the new (as of Version 1.11.1) Experimental AI (see below).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of a different recruiting pattern, check out the &amp;quot;Recruiting&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  You can also explore the unit choices by watching the Experimental AI in a multiplayer game.&lt;br /&gt;
&lt;br /&gt;
'''Note:''' Obviously, the Recruitment Micro AI modifies the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|recruitment candidate action]].&lt;br /&gt;
&lt;br /&gt;
=== Random Recruitment AI (ai_type=recruit_random) ===&lt;br /&gt;
&lt;br /&gt;
This AI randomly selects a unit type from the recruitment list of the side.  The probability of choosing each type and the behavior in low-gold situations can be influenced with optional parameters.&lt;br /&gt;
&lt;br /&gt;
Enable the Random Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_random&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* '''[probability]''': This tag sets the probability of selecting the given unit type.  It has two required sub-keys:&lt;br /&gt;
** '''type''': comma-separated list of unit types to which this probability is to be applied&lt;br /&gt;
** '''probability''': (non-negative float)  The probability with which these unit types is to be selected.  All unit types for which no [probability] tag is defined receive a default probability of 1.0, thus values should be chosen relative to that.  As an example, in the code below swordsmen and peasants are given a probability of 8, while no other units have assigned probabilities.  Thus, each of these two unit types is 8 times more likely to be selected than any other individual unit type.&lt;br /&gt;
* '''skip_low_gold_recruiting'''=no: (boolean)  By default, the random recruitment AI chooses a unit to recruit only from those types for which it has sufficient gold.  If this parameter is set to 'yes', it will instead choose from all available unit types and end recruiting for the turn if there is not enough gold to recruit it this turn (even if other, affordable unit types exist).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a random recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_random&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [probability]&lt;br /&gt;
                 type=Swordsman,Peasant&lt;br /&gt;
                 probability=8&lt;br /&gt;
             [/probability]&lt;br /&gt;
 &lt;br /&gt;
             skip_low_gold_recruiting=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Rush Recruitment AI (ai_type=recruit_rushers) ===&lt;br /&gt;
&lt;br /&gt;
This AI replaces the default recruiting with the rush recruiting used in the new (as of Version 1.11.1) Experimental AI.&lt;br /&gt;
&lt;br /&gt;
Enable the Rush Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_rushers&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* {{DevFeature1.15|2}} '''high_level_fraction'''=0: (non-negative number) The approximate fraction of units of level 2 or higher to be recruited. This is defined as fraction of units on the map, not of new units being recruited during a turn or over several turns (which makes a difference if there are already units on the map). The effect is also cumulative per level (starting from level 2), meaning that 1/3 of L2 units will be recruited in the example above, 1/3*1/3=1/9 L3s etc. The default value is zero, which leaves the high-level unit recruiting to the default Experimental AI algorithm resulting usually in very few such units being recruited.&lt;br /&gt;
* '''randomness'''=0.1: (number)  A random number is applied to the rusher recruit engine's score to prevent the recruitment pattern from being too predictable. 0 causes no randomness to be applied, while larger numbers increase the random effect. A value of 1-2 generates results in which the random effect is approximately equal to the scored effect. Extremely high values are essentially entirely random.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a rush recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_rushers&lt;br /&gt;
             action=add&lt;br /&gt;
          [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Simple Attack Micro AI (ai_type=simple_attack) ==&lt;br /&gt;
&lt;br /&gt;
The Simple Attack Micro AI is meant as an addition to the default combat candidate action for when one wants to force the AI to do attacks that it would otherwise not do (for example on units that are 1 XP from leveling) or to execute certain attacks before all others (for example, attack with all &amp;quot;expendable&amp;quot; units first).  It is not set up to be a replacement for the default attacks.  Some notes:&lt;br /&gt;
&lt;br /&gt;
* The algorithm is different from and simpler than that of the default combat CA.  For example, no attack combinations are considered, only individual attacks.  As a result, the [[AiWML#List_of_AI_Aspects|default aspects]] (aggression, caution, [avoid], ...) are not taken into account.  Also, if lots of units are involved, evaluation of this Micro AI is potentially quite a bit slower than of the default attacks.&lt;br /&gt;
&lt;br /&gt;
* If an attack matching the Standard Unit Filters described below is found, this attack will always be executed, no matter how bad the odds are.  Thus, this Micro AI can be used to force the AI to do attacks it would otherwise avoid.&lt;br /&gt;
&lt;br /&gt;
* Other than selecting the attacker/defender units, this AI is currently not configurable.  Let us know if you would like to see certain options to be added.  (No guarantees that we will add them, but we will definitely consider them.)&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Simple Attack&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=simple_attack&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Simple Attack specific keys for the [micro_ai] tag===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  It is, however, in general not desirable to use this AI without at least one of the optional Standard Unit Filters below, as this AI is not meant as a replacement of the default combat candidate action.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=110000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 110,000, which means that it is executed right before the default combat candidate action.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Simple Attack action.  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units the AI should attack.  It defaults to all enemy units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
* '''weapon'''=-1: (integer) The number of the weapon that should be used for this attack.  Weapon numbers start at 1 (not 0).  -1 stands for &amp;quot;best weapon&amp;quot; (by whatever criterion the AI chooses to use).   '''Available from Wesnoth 1.11.8'''&lt;br /&gt;
&lt;br /&gt;
Below are 2 examples of Simple Attack Micro AI configurations from the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  The first one shows how to force the AI to attack with all Soulless and Walking Corpse units first:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110000&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless,Walking Corpse&lt;br /&gt;
             [/filter]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
The second example executes attacks by Soulless on all enemy units that are 1 XP from leveling.  Note that the Lua code in the preload event is there simply in order to code the &amp;quot;1 XP from advancing&amp;quot; condition.  It is not needed for most other SUFs, as can be seen in the previous example.&lt;br /&gt;
     [event]&lt;br /&gt;
         name=preload&lt;br /&gt;
         first_time_only=no&lt;br /&gt;
         [lua]&lt;br /&gt;
             code=&amp;lt;&amp;lt;&lt;br /&gt;
                 function close_to_advancing(unit)&lt;br /&gt;
                     if (unit.experience &amp;gt;= unit.max_experience-1) then&lt;br /&gt;
                         return true&lt;br /&gt;
                     else&lt;br /&gt;
                         return false&lt;br /&gt;
                     end&lt;br /&gt;
                 end&lt;br /&gt;
             &amp;gt;&amp;gt;&lt;br /&gt;
         [/lua]&lt;br /&gt;
     [/event]&lt;br /&gt;
 &lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110001&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless  # No Walking Corpses; L0 units don't advance enemy&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 lua_function = &amp;quot;close_to_advancing&amp;quot;&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
Several Simple Attack Micro AIs can be combined for the same AI side.  The two examples above are both used (note the different values for ca_score) in the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
== Custom Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|5}}&lt;br /&gt;
&lt;br /&gt;
From 1.13.5 onwards, adding a new Micro AI can be easily done without altering any core files or overriding the micro_ai tag. The [micro_ai] tag now looks up its ''ai_type'' parameter in the &amp;lt;code&amp;gt;wesnoth.micro_ais&amp;lt;/code&amp;gt; table and uses the micro AI setup function found there to determine which keys are required and allowed and what candidate actions to add.&lt;br /&gt;
&lt;br /&gt;
A micro AI setup function takes as its single parameter the contents of the [micro_ai] tag and returns three separate values:&lt;br /&gt;
*'''required''': A list of required keys and tags, as strings. Tags are indicated by enclosing them within square brackets in the string. If a key or tag in this list is missing from the [micro_ai] tag, an error will be printed.&lt;br /&gt;
*'''optional''': Similar to above, a list of optional keys and tags, as strings. No error will be printed if a key or tag in this list is missing, nor will an error be printed if it is present (as would be the case for unrecognized keys or tags).&lt;br /&gt;
*'''ca_params''': A table specifying the candidate actions that constitute the micro AI. It should contain an ''ai_id'' key (which specifies a base ID to be used for candidate actions of this micro AI) and a series of candidate action definitions. Each candidate action definition is a table containing the following keys:&lt;br /&gt;
**'''ca_id''': The ID used for the candidate action. This will be combined with the ''ai_id'' to produce the final ID.&lt;br /&gt;
**'''location''': Path to the Lua file that defines the candidate action.&lt;br /&gt;
**'''score''': The maximum score for the candidate action. If the [micro_ai] tag specified a ''ca_score'' parameter, the score here should normally be based on that.&lt;br /&gt;
&lt;br /&gt;
(The return statement would thus look like &amp;lt;syntaxhighlight lang='lua' inline&amp;gt;return required, optional, ca_params&amp;lt;/syntaxhighlight&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
'''Examples''' of micro AI setup functions can be found in the [https://github.com/wesnoth/wesnoth/tree/1.14/data/ai/micro_ais/mai-defs default MAI definitions directory].  See the [https://github.com/wesnoth/wesnoth/blob/1.14/data/ai/micro_ais/mai-defs/animals.lua#L4-L12 Big Animals MAI definition] for a simple example.  For custom micro AIs, the setup code should be put into a ''[lua]'' tag inside a preload event.&lt;br /&gt;
&lt;br /&gt;
Since the function has full access to the contents of the [micro_ai] tag, it can conditionally add candidate actions to '''ca_params''' depending on the values of other keys within the [micro_ai] tag. It can also perform other actions such as adding facets to an aspect; if it does such things, it should pay attention to the ''action'' key to determine whether it's being added or removed.&lt;br /&gt;
&lt;br /&gt;
There are two useful helper functions that can be used by Micro AI setup functions requiring aspect changes. They can be brought into scope by a line similar to the following:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='lua&amp;gt;local MAIH = wesnoth.require(&amp;quot;ai/micro_ais/micro_ai_helper.lua&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* MAIH.add_aspects(side, aspect_params)&lt;br /&gt;
* MAIH.delete_aspects(side, aspect_params)&lt;br /&gt;
&lt;br /&gt;
The ''aspect_params'' parameter is an array of tables, each of which contains the following keys:&lt;br /&gt;
*'''aspect''': The aspect to change.&lt;br /&gt;
*'''facet''': The WML defining the facet. This must include an ''id'' key in order for the aspect to be correctly deleted. For maximum compatibility, incorporate the ''ca_id'' key from the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=66740</id>
		<title>Micro AIs</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=66740"/>
		<updated>2021-03-03T20:19:53Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Goto specific keys for the [micro_ai] tag: */ add new remove_movement= parameters&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== A Few General Words about Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
* Micro AIs allow Wesnoth campaign and scenario authors to add new AI functionality without the need for any AI programming. They bring new specialized behavior to Wesnoth that cannot be achieved, at least not easily, by adjusting default AI parameters (such as those described at [[AiWML]]) or with WML code.&lt;br /&gt;
&lt;br /&gt;
* A Micro AI is activated and configured (or deleted) via the [micro_ai] tag, requiring only a few lines of WML code.&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.6''', the Micro AI behavior was inconsistent when it came to '''hidden enemies and petrified units'''.  Some Micro AIs ignored those, while others did not.  In some case there was even a potential for AI errors when an AI unit ran into hidden or petrified enemies.  As of 1.13.6, this has all been fixed.  The Micro AIs now correctly and consistently ignore hidden units and treat petrified units as blocking the hex they are on but having no effect otherwise, in the same way as the default AI does.  '''Important notes:'''&lt;br /&gt;
** It is not possible to re-include hidden/petrified units via the Standard Unit Filters (SUFs) used by some of the Micro AIs. In fact, if this is attempted, the filters will generally not match any unit.&lt;br /&gt;
*** This only applies to SUFs which go directly into the [micro_ai] tag. Any SUF that is a sub-tag of another filter (for example, of a Standard Location Filter) is taken literally and returns all units matching the filter. If hidden or petrified units are to be excluded from such a sub-filter, that needs to be specified directly in that filter.&lt;br /&gt;
** We also note that, because all the Micro AIs are written in Lua, it is possible to [[Micro_AIs#Using_Development_.281.13.29_Version_Micro_AIs_in_your_Stable_.281.12.29_Version_Add-on|move a Micro AI's code into one's add-on]] and modify it to whatever scenario-specific behavior is desired (this does not only apply to hidden/petrified units, but to all behavior). Setting up general Micro AI behavior that works in every possible setting is not feasible, but adapting it to work (only) in the specific setting of a given scenario is often quite simple. If you do not know how to do this, please post a message on the Wesnoth forums and we will be happy to help you with it.&lt;br /&gt;
&lt;br /&gt;
* The '''[[AiWML#List_of_AI_Aspects|default AI aspects]] are, for the most part, not taken into account by the Micro AIs'''.  For example, unless noted otherwise, specifying a value for ''aggression'' does not change the MAI behavior, and locations specified in an ''[avoid]'' tag generally can be entered by units controlled by an MAI.  This is a feature of the Micro AIs, not a bug.  If you feel that a certain default AI aspect should also apply to a Micro AI, please let us know in the [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 Micro AIs Feedback thread].&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.7''', the Micro AIs did not all work reliably when the '''AI side is under shroud''' and it may have been necessary to turn shroud off for the side when using a Micro AI. {{DevFeature1.13|7}} The Micro AIs now all work for sides under shroud. They do this by disregarding shroud for terrain purposes when path finding, consistent with default Wesnoth AI behavior, while still ignoring hidden units (including those hidden under shroud) correctly.&lt;br /&gt;
&lt;br /&gt;
* Most Micro AIs in their default setting apply to all units of a side, although many of them provide parameters for filtering the units that should be affected by the new behavior.  If the Micro AI is applied only to part of the units of a side, the other units follow the default Wesnoth AI behavior.&lt;br /&gt;
&lt;br /&gt;
* Micro AIs are available in mainline since Wesnoth 1.11/1.12.&lt;br /&gt;
&lt;br /&gt;
== Micro AI Test and Demo Scenarios ==&lt;br /&gt;
&lt;br /&gt;
All Micro AIs described here can be checked out in a number of test and demo scenarios.  The easiest way to access the test scenarios is by assigning a hotkey to &amp;quot;Start Test Scenario&amp;quot; in the preferences (as of Wesnoth 1.13.8). Then, simply press the hotkey in the titlescreen and select the desired scenario from the list (e.g. 'micro_ai_test' for the overall &amp;quot;switchboard scenario&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Test scenarios can also be accessed from the command line by typing&lt;br /&gt;
 path/wesnoth-executable -t micro_ai_test&lt;br /&gt;
or (depending on your system and Wesnoth version)&lt;br /&gt;
 path/wesnoth-executable -tmicro_ai_test&lt;br /&gt;
This starts up a &amp;quot;switchboard scenario&amp;quot; in which you can select the Micro AI demonstration scenario you want to check out.  Here, ''path/wesnoth-executable'' needs to be replaced by whatever the path and filename of the Wesnoth executable is on your system.&lt;br /&gt;
&lt;br /&gt;
== Setting up a Micro AI ==&lt;br /&gt;
&lt;br /&gt;
Setting up a Micro AI requires only one simple step:&lt;br /&gt;
==== Activating and configuring (or deleting) a Micro AI ====&lt;br /&gt;
&lt;br /&gt;
Micro AIs are activated, deleted and configured using the [micro_ai] tag.  This tag needs to be placed in [[ActionWML]], that is, in an event, a menu option or the like.  As an example, the following code activates the healer_support Micro AI in its default configuration for Side 2 from the beginning of the scenario:&lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
 &lt;br /&gt;
         # Configure the healer support micro AI&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
'''Keys required for all Micro AIs:'''&lt;br /&gt;
All [micro_ai] tags must contain the following three required keys:&lt;br /&gt;
* '''side''': The side for which the Micro AI is to be added, changed or deleted.  This has to be an individual side number, lists of sides are not supported.&lt;br /&gt;
* '''ai_type''': The type of Micro AI to be added, changed or deleted.  See the following sections for allowed values.&lt;br /&gt;
* '''action''' (string): The action to take concerning the Micro AI.  The following values are allowed: &lt;br /&gt;
** ''add'': Add a Micro AI to a side.&lt;br /&gt;
** ''change'': Change the configuration of an existing Micro AI.  Note that this does not only change the specific parameters provided in the tag, but it replaces the entire existing Micro AI by a new version with the new (and only the new) configuration.  It is therefore equivalent to using first the ''delete'' and then the ''add'' action.&lt;br /&gt;
** ''delete'': Delete an existing Micro AI from a side.&lt;br /&gt;
&lt;br /&gt;
'''Keys required under certain circumstances:'''&lt;br /&gt;
* '''ca_id''': (string) If several MAIs of the same type are used for the same side and one or several of these are to be changed or deleted during a scenario, this key needs to be included so that the [micro_ai] tag knows which of them needs to be changed/deleted.  The same is also true if a Micro AI is changed more than once during a scenario.  We therefore '''strongly recommend always setting ''ca_id''''' for MAIs which are, or might be, changed or deleted during a scenario.&lt;br /&gt;
** '''Note:''' The id(s)/name(s) actually assigned to the CA(s) of the MAI by the engine might be different from the parameter passed here.  It is, however, sufficient to use this ca_id with the [micro_ai] tag add/delete/change actions to identify the MAI uniquely.  (In other words: you don't have to worry about this if you always use the [micro_ai] tag, but might have to figure out the actual name(s) of the CA(s) if you want to delete or change them manually.)&lt;br /&gt;
* Other keys may also be required depending on the Micro AI.  In additional, a number of optional keys may be available to configure the AI behavior.  See the sections on the individual Micro AIs for details.&lt;br /&gt;
&lt;br /&gt;
'''Required keys for deleting a Micro AI:'''&lt;br /&gt;
&lt;br /&gt;
Only a subset of the keys required for setting up a Micro AI are needed when deleting it.  '''side''', '''ai_type''' and '''action'''=delete always need to be provided.  '''ca_id''' needs to be given if it was provided when adding the Micro AI (see above for when this is needed).&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* The ''add'' and ''change'' actions ignore all keys that do not apply to the respective Micro AI type.  The ''delete'' action ignores all keys other than the required keys listed above.&lt;br /&gt;
&lt;br /&gt;
* Some of the Micro AIs use and/or modify default AI components (candidate actions or aspects) when they are added, or reset these components to their default values when they are deleted.  Thus, if you have modified the AI yourself using the default CAs or aspects, some of these modifications might be affected, or might be interfering with the Micro AI.  The sections below indicate whether a Micro AI modifies any default AI components.&lt;br /&gt;
&lt;br /&gt;
===Combining Different Micro AI Types for the Same Side===&lt;br /&gt;
&lt;br /&gt;
No extra steps need to be taken when combining different Micro AIs on the same side.  Simply set up the [micro_ai] tags for all AIs you want to use.&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* While it is ''technically'' possible to combine any number of Micro AIs, this does not mean that all Micro AIs work together in practice.  Some are not compatible because they control units in contradictory ways.  &lt;br /&gt;
* Not all combinations that ''should'' work have been tested yet.  If you find a combination that you believe should work but doesn't, [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 please let us know] and we will see if that can be fixed.&lt;br /&gt;
* The candidate action score almost certainly has to be set (using the ca_score= key) for one or several of the MAIs if several of them are used on the same side.&lt;br /&gt;
&lt;br /&gt;
== Animals Micro AIs ==&lt;br /&gt;
=== General Comments on the Animal Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Animals Micro AIs are meant to simulate the behaviors of certain types of animals.  They are, however, set up so that they can be used with arbitrary unit types and might be applicable in quite diverse situations.  The AIs nevertheless remain named after the animals for which they were written originally, so that they are (to some extent) descriptive of their behavior.&lt;br /&gt;
&lt;br /&gt;
Brief descriptions of the AI behaviors are given in the following subsections.  To get a better feeling for their behavior, check out the &amp;quot;Animals&amp;quot;, &amp;quot;Swarm&amp;quot;, &amp;quot;Wolves&amp;quot; and &amp;quot;Dragon&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  Note that some of the animal AIs are designed for animal types that do not exist in mainline.  In the test scenario, other animal types are therefore substituted for them.&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Wolves (ai_type=wolves) ===&lt;br /&gt;
&lt;br /&gt;
The Wolves Micro AI organizes all &amp;quot;wolf&amp;quot; units (predators) of the side to hunt in a single pack.  They actively chase after the closest prey and try to corner it (not always super successfully), but are easily distracted by other enemy units coming into attack range, to the point where the pack might split up.  The wolves can, however, be told to attack only prey units as well as to try to avoid other types of units (such as larger predators).  When no prey is left, the wolves wander randomly.&lt;br /&gt;
&lt;br /&gt;
This AI works best for a small number of predators (2 - 5) and sparse prey.  Also, the predators should be put on the map in a pack, that is, reasonably close to each other.  For a demonstration of the Wolves AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Note that another wolves AI exists, called [[Micro_AIs#Animals_AI:_Multi-pack_Wolves_.28ai_type.3Dwolves_multipacks.29|Multipack Wolves]], which distributes the wolves of a side into several packs, rather than all of them into one single pack as it is done here.&lt;br /&gt;
&lt;br /&gt;
Enable the Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the predator units.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the prey units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_only_prey'''=no: In the default configuration, the wolves attack all enemy units in attack range.  If this key is set to 'yes', they only attack prey units.&lt;br /&gt;
* '''avoid_type''': Comma-separated list of unit types (on enemy sides) which the wolves try to avoid.  They try to stay out of these units' way as much as possible when moving around (except when they are moving in to attack another enemy unit) and never attack them.&lt;br /&gt;
* '''ca_score'''=90000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 90,000, which is lower than the default combat CA, but higher than most other default CAs which move units.  It can be changed by setting this parameter but in most cases this will not make sense.  Note that the Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Notes on default (RCA) AI aspects:''' &lt;br /&gt;
* If ''attack_only_prey'' or ''avoid_type'' are set, the Wolves Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* Attacks by the wolves are left to the default (RCA) AI, meaning that the value of ''aggression'' has an influence on the behavior.&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI is taken into account. Note: as attacks by the wolves are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=6&lt;br /&gt;
             ai_type=wolves&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Wolf&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Vampire Bat&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             avoid_type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Multi-pack Wolves (ai_type=wolves_multipacks) ===&lt;br /&gt;
&lt;br /&gt;
The Multi-pack Wolves Micro AI is different from the [[Micro_AIs#Animals_AI:_Wolves_.28ai_type.3Dwolves.29|Wolves Micro AI]] in that there can be an arbitrary number of wolf packs, with the pack size being a free parameter.  At the beginning of the scenario, close wolves are grouped into packs in a semi-methodical way.  If the wolves are put on the map in distinct groups of close wolves of the correct number, they will be joined into packs matching these groups.  If, on the other hand, they are spread out all over the map, the method of assigning them to packs is semi-random.&lt;br /&gt;
&lt;br /&gt;
Wolves of the same pack begin by joining each other on the map.  After that, they stay together until only one wolf is left, which then tries to join up with an incomplete pack or with other single wolves.  Individual wolves entering the map during the scenario behave in that way as well.&lt;br /&gt;
&lt;br /&gt;
A second difference to the other Wolves AI is that wolves do not actively hunt here.  For the most part they just wander (often long distance).  However, the pack ferociously (and without regard for its own health) attacks any enemy units that come into range, as long as that does not mean separating the pack by more than a few hexes.  Staying together, or joining with a new wolf assigned to the pack, is the only thing that takes priority over satisfying the wolves' thirst for blood.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Multi-pack Wolves AI behavior, check out the &amp;quot;Wolves&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Multi-pack Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves_multipacks&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Multi-pack Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''pack_size'''=3: (integer) The size of the packs.&lt;br /&gt;
* '''show_pack_number'''=no: If set to &amp;quot;yes&amp;quot;, the wolves' pack numbers will be shown below them.  Note that this means that the pack number of a wolf killed in an attack will remain as a label on the map.  &lt;br /&gt;
* '''type'''=Wolf: Comma-separated list of the unit types which the AI controls. The default value is &amp;quot;Wolf&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves_multipacks [micro_ai] tag usage from the &amp;quot;Wolves&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=wolves_multipacks&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             show_pack_number=yes&lt;br /&gt;
             pack_size=4&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Big Animals (ai_type=big_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Big Animals Micro AI is a simulation of large predators that wander and hunt alone, such as Bears, Giant Spiders or Yetis.  For the most part, these just wander on the terrain that's been defined for them, but they attack enemy units if those happen to come into range.  The AI can be set up so that the Big Animals stay out of the way of other units (such as other large predators), in which case they only attack those if cornered or if they run into them accidentally (such as ambushers).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Big Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Big Animals Micro AI by using&lt;br /&gt;
 ai_type=big_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the animals to which this AI is applied.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': The big animals often go for long distance (multi-turn) journeys.  This tag is a [[StandardLocationFilter|Standard Location Filter]] describing the terrain from which the next goal is chosen.  The default is all terrain on the map. &lt;br /&gt;
* '''[filter_location_wander]''': The terrain on which the big animals will end their moves. This is a [[StandardLocationFilter|Standard Location Filter]].  The default is all terrain on the map.&lt;br /&gt;
* '''[avoid_unit]''': A [[StandardUnitFilter|Standard Unit Filter]] describing all enemy units that the big animals will avoid.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
Here's an example of big_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=big_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Ghast&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [avoid_unit]&lt;br /&gt;
                 type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
             [/avoid_unit]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=1-40,1-18&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Forest Animals (ai_type=forest_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Forest Animals Micro AI simulates a number of different animal behaviors that one might find in a forest, namely animals behaving like deer, rabbits and families of tuskers.  In the following, we will refer to those animals by these types, even though the actual unit types can be selected freely.  They behave as follows:&lt;br /&gt;
* Each deer wanders randomly on (selectable) terrain, except when enemies get in its (the deer's) maximum movement range, in which case it flees to the farthest point it can reach.&lt;br /&gt;
* Tuskers exhibit the same behavior as deer, except when an enemy is next to one of the tusklets.  This enemy is attacked unconditionally by the tuskers trying to protect their young.&lt;br /&gt;
* Tusklets blindly follow the closest adult tusker, except when there is no tusker left, in which case they behave the same as deer.&lt;br /&gt;
* Rabbits also wander randomly, but in addition disappear into their holes (if any are within reach) when enemies are close.  They reappear out of their holes at the beginning of the turn, if it is safe.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Forest Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Forest Animals Micro AI by using&lt;br /&gt;
 ai_type=forest_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Forest Animals AI consists of 4 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-3&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''rabbit_type, tusker_type, tusklet_type, deer_type''': Comma-separated lists of the unit types behaving as the respective animals.  If any of these parameters are not set, those types of animal behavior are skipped by the AI.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain on which the forest animals wander when they are not threatened by enemies.&lt;br /&gt;
* '''rabbit_number'''=6: (integer)  The number of rabbits for the AI to have on the map simultaneously.  The AI puts the missing number of rabbits onto the map at the beginning of the AI's turn (when the rabbits come out of their holes).&lt;br /&gt;
* '''rabbit_enemy_distance'''=3: (number)  Rabbits won't spawn in holes that aren't more than this distance away from all enemies.&lt;br /&gt;
* '''rabbit_hole_img''': Rabbit holes are marked by items on the map.  If this parameter is set, it must be the file name of the item image.  If it is not set, all items on the map are treated as rabbit holes.&lt;br /&gt;
** {{DevFeature1.14|10}} Items used for rabbit holes on the map border used to produce an error. Placing them on the map border therefore needed to be avoided on the scenario setup side. They are now automatically excluded from consideration by the AI code.&lt;br /&gt;
&lt;br /&gt;
Here's an example of forest_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=forest_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             deer_type=Vampire Bat&lt;br /&gt;
             rabbit_type=Giant Rat&lt;br /&gt;
             tusker_type=Ogre&lt;br /&gt;
             tusklet_type=Young Ogre&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=*^F*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Herd and Herders (ai_type=herding) ===&lt;br /&gt;
&lt;br /&gt;
The Herding Micro AI sets up an area on the map where a number of herder units (such as sheep dog) control and protect a herd (such as sheep).  For convenience, we simply refer to them as sheep dogs and sheep in the following, even though arbitrary unit types can be selected for either.&lt;br /&gt;
&lt;br /&gt;
Sheep dogs try to keep their sheep safe.  This involves keeping them inside the herding area, positioning themselves in between the sheep and approaching enemies, and attacking the enemies if those get too close.  If no active herding or protecting move is needed, the dogs go to a random location on the perimeter of the herding area.&lt;br /&gt;
&lt;br /&gt;
Sheep wander aimlessly except when a sheep dog is next to them, in which case they run away from the dog.  The dogs exploit this by positioning themselves on the outside of the sheep, if possible.  Sheep also run away from approaching enemies.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Herding AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Herding Micro AI by using&lt;br /&gt;
 ai_type=herding&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herder units (e.g. dogs).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herd units (e.g. sheep).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain forming the perimeter of the herding area.  In other words, this is the path the herders (dogs) use to patrol around the outside of the herding area.  This must be a set of coordinates ''completely enclosing'' the herding area.  Here's an example of setting up a rectangular perimeter:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         x=10-20,10-20,10,  ,20&lt;br /&gt;
         y=11,   21,   11-21,11-21&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
Another possibility is by defining the path by certain terrain on the map (as it is done in the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]), in which case the filter can simply look like this:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         terrain=Rb&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
* '''herd_x,herd_y''': The coordinate of one hex (''any'' one hex) inside the herding area.  This is needed to define which part of the map is on the ''inside'' of the herding area perimeter defined by [herding_perimeter], and which is the ''outside''. {{DevFeature1.15|0}} '''herd_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''attention_distance'''=8: (integer)  If an enemy is within this distance of a herd unit, but not as close as ''attack_distance'', the herders try to position themselves in between the enemy and the herd unit.&lt;br /&gt;
* '''attack_distance'''=4: (integer)  If an enemy is within this distance of a herd animal, the herders attack this enemy.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Herding AI consists of 6 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-5&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Herding [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=herding&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Footpad&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Troll Whelp&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             herd_x,herd_y=32,28&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=Rb&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Hunter (ai_type=hunter) ===&lt;br /&gt;
&lt;br /&gt;
The Hunter Micro AI leads a unit (or units) through hunt-and-rest cycles.  The units wander in a prescribed area of the map, their hunting ground, until an enemy unit comes within range.  Note that the units do not actively chase down enemies, but wander randomly through the hunting ground until an enemy is encountered.&lt;br /&gt;
&lt;br /&gt;
If enemies are within range of a hunter unit, the hunter attacks the weakest of them.  If a kill is made, it then retreats to its rest location, where it stays for a certain number of turns and until fully healed (whatever happens later).&lt;br /&gt;
&lt;br /&gt;
A few more details:&lt;br /&gt;
* If the hunter's way home is entirely blocked on the return (so that there is no possible path), the normal RCA AI takes over its behavior.&lt;br /&gt;
* However, if the way is blocked by an enemy unit occupying the rest hex, that enemy will be attacked unconditionally.&lt;br /&gt;
* A kill only makes the hunter go home when it is the attacker, not as defender.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Hunter AI behavior, check out the &amp;quot;Dragon&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]. &lt;br /&gt;
&lt;br /&gt;
Enable the Hunter Micro AI by using&lt;br /&gt;
 ai_type=hunter&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''home_x,home_y''': The coordinates of the hex to which the hunter returns and rests at after a kill. {{DevFeature1.15|0}} '''home_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the unit's hunting ground.  If this tag is not provided, the hunter uses all of the map.&lt;br /&gt;
* '''rest_turns'''=3: (integer)  The number of turns the AI stays at home_x,home_y after a kill.  It will not leave its home until this number of turns have passed ''and'' it is fully healed.&lt;br /&gt;
* '''show_messages'''=no: (boolean) If set to yes, the hunter announces whenever its behavior changes to the next phase.  Note that this is meant as debug output for the scenario author.  The messages are not translated and are not included in replays (and are only visible to the host in an MP game).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a hunter [micro_ai] tag usage from the &amp;quot;Dragon&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hunter&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Rowck&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=5-30,1-15&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             home_x,home_y=3,15&lt;br /&gt;
             rest_turns=2&lt;br /&gt;
 &lt;br /&gt;
             show_messages=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Swarm (ai_type=swarm) ===&lt;br /&gt;
&lt;br /&gt;
The Swarm Micro AI uses some very simply algorithms to simulate animal swarm behavior.  Without adjacent enemies, they simply do a random move, trying to stay together and at a certain distance from enemies.  However, if an enemy unit is close to any bat, the swarm scatters.  This is particular fun to watch when one places an enemy unit in the middle of the swarm.  After being scattered, the swarm members slowly rejoin, but not in a very organized way.  Sub-swarms or individual bats might roam around for quite some time before they find their way back.  It is also possible that individual bats (or small groups) split off from the larger swarm at times.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Swarm AI behavior, check out the &amp;quot;Swarm&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Swarm Micro AI by using&lt;br /&gt;
 ai_type=swarm&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Swarm AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_distance'''=5: (int) The minimum distance kept between units of the swarm and enemies when the swarm moves as a whole.&lt;br /&gt;
* '''scatter_distance'''=3: (int) Enemies within &amp;quot;scatter_distance&amp;quot; hexes of any swarm unit cause the swarm to scatter, by each unit trying to maximize its individual distance from the enemy.&lt;br /&gt;
* '''vision_distance'''=12: (int) Only units within this distance follow the overall swarm motion (either away from an enemy or of the swarm as a whole).  The smaller this value is set, the less likely the swarm is to stay together or rejoin.  This parameter is meant to simulate how far an individual member of the swarm can see, meaning that the swarm is &amp;quot;out of sight&amp;quot; (and the unit will not be able to follow it) if it is more than this distance away.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Swarm [micro_ai] tag usage from the &amp;quot;Swarm&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=swarm&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Assassin Squad Micro AI (ai_type=assassin) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|6}}&lt;br /&gt;
&lt;br /&gt;
This Micro AI controls one or several assassin units which try to circle around enemy units with the goal of hitting a target unit without being distracted or impeded by other enemies.  The units will always attack the target if such attacks are possible, otherwise they will follow the path of least resistance toward it. Notes:&lt;br /&gt;
&lt;br /&gt;
* The main priority for the AI is to find paths that avoid enemy units. However, if no such path can be found, the assassins will still advance toward the target, taking the path exposing the units to the least potential damage from enemies.&lt;br /&gt;
&lt;br /&gt;
* The units will not necessarily follow the same path. The best path is chosen individually for each unit.&lt;br /&gt;
&lt;br /&gt;
* While it is not a requirement for the AI to work, using ambusher or skirmisher units, or units with the quick trait, makes this AI more lethal in many cases.&lt;br /&gt;
&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded, it does not work otherwise. That means that '''ca_score''' is not a valid key for the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=assassin&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Assassin Squad specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the assassin units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the target unit.&lt;br /&gt;
** It is important to realize that the AI currently only works correctly with a single target unit. It is thus the responsibility of the scenario designer to make sure that this filter matches an individual enemy unit. If several enemy units are to be targeted in the same scenario, several instances of the Assassin Micro AI can be used.&lt;br /&gt;
** The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[prefer]''': A [[StandardLocationFilter|Standard Location Filter]] selecting locations on the map for the assassins to prefer when making their way toward the target unit. This could be used, for example, to make units with the ambush ability move (mostly) through forest or to have the assassins circle around the, say, northern part of the map rather than the south if that is desirable for scenario design purposes.&lt;br /&gt;
** As with avoiding enemies, the units will still move across non-preferred terrain if that is either the only option or ''much'' shorter than preferred terrain&lt;br /&gt;
** Attacking, once the target unit is in range, is not influenced by the [prefer] SLF.&lt;br /&gt;
&lt;br /&gt;
Here is an example of an Assassin Squad [micro_ai] tag usage:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=assassin&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Assassin,Master at Arms,Duelist&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             [prefer] # Circle around northern part of map&lt;br /&gt;
                 x=1-9, 10-99&lt;br /&gt;
                 y=1-37, 1-17&lt;br /&gt;
             [/prefer]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Bottleneck Defense Micro AI (ai_type=bottleneck_defense) ==&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense Micro AI lets you define a location on the map where the AI can take a defensive stance behind a narrow passage (bottleneck).  Units on the front line are backed up by healers and/or units with the leadership ability.  Injured units are moved to the back and replaced by uninjured (or less injured) units.  The units on the front line only attack when it is safe (no retaliation) or when there is a high chance that they will make a kill or level up.  Using this Micro AI only makes sense if there is no way for the enemy sides to move around the bottleneck and attack the AI's units from behind.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Bottleneck&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Bottleneck Defense Micro AI by using&lt;br /&gt;
 ai_type=bottleneck_defense&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Bottleneck Defense specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense AI requires two sets of coordinates that define where it should be taking up its defensive stance, and from which side the enemy attacks at that location.&lt;br /&gt;
&lt;br /&gt;
* '''x,y''': Comma separated lists of the hexes on the front line, where strong units are placed.  All hexes on which the AI makes contact with (can be attacked by) the enemy need to be included here.  Units are placed on them in the order in which they are listed here. {{DevFeature1.15|0}} '''location_id''' can be used instead with named/special locations.&lt;br /&gt;
* '''enemy_x,enemy_y''': Comma separated list of the hexes from which the enemy can attack the AI front line units.  This is needed for the AI to know on which side of the front line support units (healers etc.) need to be placed.  In many cases, it is sufficient to provide only one of the enemy front line hexes, but there are geometries for which that does not work.  It is therefore safer to list all enemy front line hexes here. {{DevFeature1.15|0}} '''enemy_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
By default, the AI places healers and units with the leadership ability both on the front line itself (if they are the strongest units available) and on hexes adjacent to and behind the line.  If different placement is desired, these locations can be overridden with the following keys.&lt;br /&gt;
&lt;br /&gt;
* '''active_side_leader'''=no: (boolean)  If set to 'yes', the side leader participates in the bottleneck defense action after the side's gold has been spent.  If set to 'no' (default), the leader follows default AI behavior (sitting on the keep and recruiting, for the most part).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Bottleneck Defense AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.15|3}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the units.  Only AI units matching this filter participate in the bottleneck defense.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''healer_x,healer_y''': Comma separated list of hexes where healers are placed.  This can be used to keep healers from the front line (or to take up only certain positions on the front line), and/or to override the default healing positions behind the line.  Healers are placed on them in the order in which the hexes are listed, with the exception that hexes on the line always take priority over hexes behind the line. {{DevFeature1.15|0}} '''healer_loc''' can be used instead with named/special locations.&lt;br /&gt;
* '''leadership_x,leadership_y''': Same as ''healer_x,healer_y'', but for units with the leadership ability. {{DevFeature1.15|0}} '''leadership_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
As an example, in the &amp;quot;Bottleneck&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]], there are three front line hexes, two of which are on hill terrain, while the third is a road with a lower defense rating.  The healer and side leader are supposed to participate in combat because they are strong units, but only from the hill hexes to keep them safer.  This is accomplished by using the following keys settings (check out the scenario to see which hex is which):&lt;br /&gt;
 [micro_ai]&lt;br /&gt;
     side=1&lt;br /&gt;
     ai_type=bottleneck_defense&lt;br /&gt;
     action=add&lt;br /&gt;
 &lt;br /&gt;
     x=14,14,14&lt;br /&gt;
     y= 7, 9, 8&lt;br /&gt;
     enemy_x=13,13&lt;br /&gt;
     enemy_y= 8, 9&lt;br /&gt;
 &lt;br /&gt;
     healer_x=14,14,15,15&lt;br /&gt;
     healer_y= 7, 9, 8, 9&lt;br /&gt;
     leadership_x=14,14,15,15&lt;br /&gt;
     leadership_y= 7, 9, 9 ,8&lt;br /&gt;
     active_side_leader=yes&lt;br /&gt;
 [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Fast Micro AI (ai_type=fast_ai) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|0}}&lt;br /&gt;
&lt;br /&gt;
This AI is meant as a fall-back AI for scenarios where there are so many units that attacks and moves done by the default AI are slow with long delays before each move.  It replaces the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions (CAs).  It should be obvious that in order to achieve the faster speed, compromises concerning the quality of the moves have to be made.&lt;br /&gt;
&lt;br /&gt;
Enable the Fast Micro AI by using&lt;br /&gt;
 ai_type=fast_ai&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Combat candidate action:'''&lt;br /&gt;
* Attackers are considered individually one at a time.  The best attack found for a unit is executed without checking whether a better attack by another unit might be possible.&lt;br /&gt;
* The default order in which attackers are considered is by decreasing hitpoints, starting with the strongest unit (in terms of HP).  This can be modified with the optional keys below.&lt;br /&gt;
* By default, if weaker units block attack hexes for a stronger unit, they do not move out of the way (this is the same as for the default AI).  If a hex becomes available after an attack (e.g. due to a weaker unit dying in the attack), all attacks that were possible ''before'' this happened are finished first.  Only after that are attacks by the stronger unit reconsidered.&lt;br /&gt;
* If include_occupied_attack_hexes=yes is set, weaker units move out of the way for stronger units if possible.  This can, however slow down the AI depending on the situation on the map.  Use with caution.&lt;br /&gt;
* {{DevFeature1.13|2}} Previous to this version, attacks by the leader were bunched in with the attacks by the other units.  Now leader attacks are treated separately and contain an extra layer of analysis to determine whether the attack position is &amp;quot;safe&amp;quot; (in an approximate way).  Currently, this is done with two simple considerations:&lt;br /&gt;
** Count all enemy units that can reach the leader's attack position.  If there are more than given by optional parameter 'leader_attack_max_units' (default value 3), do not attack.&lt;br /&gt;
** Add up the hitpoints of all enemies that can reach the leader's attack position.  If this value, multiplied by optional parameter 'leader_weight'(default value 2), is larger than the leaders hitpoints, do not attack.&lt;br /&gt;
** Two optional parameters, 'threatened_leader_fights' and 'leader_additional_threat', can also be used to let the leader attack when the threat at the attack hex is comparable to that at the leader's current position.&lt;br /&gt;
** In order to keep this AI as fast as possible, leader attacks are done by a separate candidate action that is only evaluated when all other CAs are done.&lt;br /&gt;
&lt;br /&gt;
'''Move-to-targets, villages and retreat candidate actions:'''&lt;br /&gt;
* The move-to-targets, villages and retreat candidate actions of the default AI are removed.  Move-to-targets and villages are taken over by the Fast Micro AI, whereas retreat is simply not done.&lt;br /&gt;
* This CA moves units toward goals after attacks are done.  Goals are unowned (if the AI side has a leader only) and enemy-owned villages and enemy leaders.&lt;br /&gt;
* For each goal, units are pre-sorted in order of the fewest moves needed to cover the ''distance'' toward the goal (that is, as if move cost on each hex toward the goal were 1).  Units are then considered sequentially and if a unit can get there &amp;quot;efficiently&amp;quot;, its move is executed without considering other units.&lt;br /&gt;
** A move is considered efficient if the move cost to get to the hex is not more than 'move_cost_factor' larger than the distance to the hex.  'move_cost_factor' is an optional key that defaults to 2.  It can be set to values of 1.1 or larger (see below).&lt;br /&gt;
** If the move for the current unit is not found to be efficient, the AI considers the next unit instead.  If no unit can do an efficient move toward the goal, the best (fastest) of the inefficient moves is selected instead.&lt;br /&gt;
&lt;br /&gt;
* Village goals:&lt;br /&gt;
** Village goals are considered before enemy leader goals so that the AI preferentially chooses scouts (or otherwise fast units) for them.&lt;br /&gt;
** The number of units sent toward villages depends on the value of the default AI aspect village_value.  It is set to village_value/4 times the number of units the AI has left to move.  Thus, by default (village_value=1.0) the AI sends a quarter of its units toward villages, and the rest toward enemy leaders.  This means that the meaningful range of values for village_value is 0..4.&lt;br /&gt;
** Only one unit is moved toward each village.  This consideration also overrides the number of units to move toward villages defined by village_value, topping it at the number of unowned/enemy-owned villages on the map.&lt;br /&gt;
** Setting village_value=0 disables moving units toward villages altogether.  That is, not only are no units chosen, but the village targeting phase of the AI is entirely disabled with no evaluation time used for it.  Note that, unlike with the default AI, this also means that no villages within range of an AI unit are grabbed.&lt;br /&gt;
** The order in which villages are chosen as goals is some combination of them being close to the AI side's leader and being spread out over the map.&lt;br /&gt;
&lt;br /&gt;
* Enemy leader goals:&lt;br /&gt;
** The order of enemy leaders goals is set according to their distance from the AI leader with the closest enemy leader being targeted first (if the AI has a leader, otherwise their order is random).  One unit is sent toward each enemy leader in this order, after which the AI starts with the first enemy leader again.  This is repeated until all units have moved.&lt;br /&gt;
&lt;br /&gt;
* Guardians (units with ai_special=guardian set) are ignored by the fast move-to-targets CA.&lt;br /&gt;
&lt;br /&gt;
'''Tips for speeding up/modifying the Fast Micro AI in specific scenarios:'''&lt;br /&gt;
&lt;br /&gt;
A number of optional keys and default AI aspects can be used to modify the behavior of the Fast Micro AI.  The MAI is set up so that it is a reasonable compromise between speed and quality of the moves in most situations.  However, this might not be ideal for all scenarios here are some things that can be done to speed up the AI (or to get better behavior in some situations by doing the opposite):&lt;br /&gt;
* Use large values for the default AI aspect aggression.  The largest delays before attacks happen (in some circumstances) when the Fast MAI has to consider lots of units with attacks left but decides not to attack with them.  See below for a description of how aggression is used by the fast AI.&lt;br /&gt;
* Use large values of move_cost_factor (see above for description of what this does).  If you want to emphasize units that can move across terrain easily (e.g. bats) to be chosen for village grabbing, you might want to use low values of move_cost_factor, but this might slow down the AI.  The default value should work reasonably well in most standard situations, but modifying the value might work better on specific maps.&lt;br /&gt;
* Don't use dungeon_mode=yes unless absolutely necessary, even in most dungeon crawls.  Test the AI without this setting first and only use it if it is the only way to solve the problem.  Often, a minor tweak to the map will have the same effect, without the speed penalty.&lt;br /&gt;
* Use include_occupied_attack_hexes=yes with caution.  It might (or might not) slow down the attack CA significantly, depending on the scenario.&lt;br /&gt;
* Set village_value=0, which makes the AI not consider villages as goals.  This has not been found to make a large difference on the maps tested so far, but it might have an effect on maps with a very large number of villages.&lt;br /&gt;
* Use attack_hidden_enemies=yes on maps where there are no hidden enemies. Excluding hidden enemies from the attack calculation requires an additional filter to be evaluated that takes extra time. In most cases this will not result in a large computation time saving.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_hidden_enemies'''=no: (boolean)  By default, hidden enemies are excluded from the attack calculation.  Setting this to 'yes' makes the AI also consider those enemies as attack targets.&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.13|2}} A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  As with the default AI, units will not step on hexes defined by the [avoid] tag.  In addition, no goals will be chosen that lie on those hexes.  The one minor exception is that a unit stepping out of the way for another unit might temporarily enter the avoided zone.&lt;br /&gt;
* '''dungeon_mode'''=no: (boolean)  As much as possible, the Fast Micro AI works with distances between hexes rather than path finding.  This works pretty well on open maps and even most dungeon crawls (and similar scenarios), but it might result in some units getting temporarily (and in rare cases permanently) stuck on maps with convoluted labyrinths.  If that is the case, setting dungeon_mode=yes triggers more path finding that avoids such situations (but as a result is somewhat slower).&lt;br /&gt;
* '''include_occupied_attack_hexes'''=no: (boolean) If set to 'yes', the AI moves weaker units out of the way for a stronger attacker.  This might lead to better attacks but is slower, so don't use if speed is essential.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the attackers.  Only AI units matching this filter will attack, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes the side= key set to the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the enemies.  Only enemy units matching this filter will be attacked, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''leader_weight'''=2: (number &amp;gt;= 0) {{DevFeature1.13|2}} A factor specifying how much more valuable the AI considers side leaders than non-leader units in the attack evaluation. This is also used to determine whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''leader_additional_threat'''=1: (number &amp;gt; 0) {{DevFeature1.13|2}} If threatened_leader_fights=yes, the threat at the attack position may be larger by this factor for the leader to leave the keep for an attack.&lt;br /&gt;
* '''leader_attack_max_units'''=3: (number &amp;gt;= 0) {{DevFeature1.13|2}} A number specifying how many enemy units within reach are acceptable when determining whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''move_cost_factor'''=2: (number &amp;gt;= 1.1)  The factor by which the move cost to a goal hex may be larger than the distance to the goal hex for a move to be considered &amp;quot;efficient&amp;quot;.  See description above.  1.1 is used if this is set to values smaller than 1.1.  Smaller values might lead to better behavior, while larger values might speed up the AI.&lt;br /&gt;
* '''weak_units_first'''=no: (boolean) If set to 'yes', inverts the order in which units are considered by the combat CA.  Attacks are then done in order of increasing hitpoints of the attackers.&lt;br /&gt;
* '''skip_combat_ca'''=no: (boolean) If set to 'yes', the fast combat CA is not used.  The default AI's high_xp_attack and combat CAs remains in place in this case.&lt;br /&gt;
* '''skip_move_ca'''=no: (boolean) If set to 'yes', the fast move-to-targets CA is not used.  The default AI's retreat, villages and move-to-targets CAs remains in place in this case.&lt;br /&gt;
* '''threatened_leader_fights'''=no: (boolean) {{DevFeature1.13|2}} By default, the AI leader will not attack if the threat at the attack position is too high.  If this key is set to 'yes', the leader will attack if the threat there is not larger than the threat at his current position multiplied by the value of 'leader_additional_threat'. This is meant as an option for the endgame: when the AI leader is under attack, he might as well go down fighting.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''aggression''': The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to set how aggressive the AI should attack.  It works in roughly the same way as the default AI aspect:&lt;br /&gt;
** The value of (1 - aggression) determines how much the AI values its own units compared to the enemy's units.  Thus, aggression=0 means that attacks are only done if the AI expects to deal (roughly) the same amount of damage as it will receive.  aggression=1 means that expected damage to the AI's units is not considered, only the expected damage to enemy units matters.&lt;br /&gt;
** Damage is calculated as a combination of hitpoints and chance to die, in &amp;quot;gold equivalent units&amp;quot;.&lt;br /&gt;
** Unlike the default AI, aggression is set to 1 for values larger than 1.  Allowed values are thus anything from -infinity to +infinity, although only -infinity to +1.0 make sense.&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.13|2}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
*'''attacks aspect''':  {{DevFeature1.13|2}} The standard RCA AI [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] used to filter own and enemy units for attacks is now also taken into account as long as neither [filter] nor [filter_second] (see above under 'optional keys') are defined in the [micro_ai] tag.&lt;br /&gt;
*'''village_value=1''': (number ranging from 0 to 4) The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to define how much villages should be targeted by the move-to-targets CA.  In the Fast MAI, this defines which fraction of the AI units are moved toward village goals (see above for details).  If set to 0, villages are ignored as goals.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded to the same values as those of the respective [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|default candidate actions]].  ca_score is therefore not a valid optional key.&lt;br /&gt;
* The fast Micro AI modifies (removes) the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions.&lt;br /&gt;
&lt;br /&gt;
Enabling the Fast Micro AI in its default configuration is as simple as this usage example from the &amp;quot;Fast AI&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=fast_ai&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Generalized Goto Micro AI (ai_type=goto) ==&lt;br /&gt;
&lt;br /&gt;
The Goto Micro AI provides a highly configurable method of sending a unit (or units) to a location or set of locations.  The units to be moved are defined using a [[StandardUnitFilter|Standard Unit Filter]] (SUF) [alternatively, all side units are used if the SUF is not provided), while the goto locations are given in a [[StandardLocationFilter|Standard Location Filter]] (SLF).&lt;br /&gt;
&lt;br /&gt;
By default, each unit matching the SUF will move toward the closest goal (in terms of turns needed to get there) matching the SLF via the fastest route given the current situation on the map (locations of enemy units etc.), but this behavior can be influenced by the optional keys listed below.  Note that this means that a unit's goal might change from one turn to the next depending on the situation on the map.&lt;br /&gt;
&lt;br /&gt;
The behavior of the unit(s) once they reach their goal location(s) is configurable as well.  By default, they will stay there until the Goto Micro AI is deleted or changed.&lt;br /&gt;
&lt;br /&gt;
For demonstrations of several Goto Micro AI usages, check out the &amp;quot;Goto&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Goto Micro AI by using&lt;br /&gt;
 ai_type=goto&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Goto specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the possible goal locations for the move.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists). Note that the area defined by [avoid] is not entered at all, even if the end point of the move would lie outside the area again.&lt;br /&gt;
* '''avoid_enemies''': (number&amp;gt;0) By default, units controlled by the Goto Micro AI will not take enemy units into account for route finding, other than the enemies' ZoCs.  By contrast, if this key is set, they will try to avoid traversing hexes on which they can be attacked by enemies.  The larger the value of ''avoid_enemies'', the more the units will try to stay out of the enemies' way.  Notes:&lt;br /&gt;
** Units are willing to take a path that is 'avoid_enemies' movement points longer for each enemy unit that can be avoided by doing so for each hex along the path.  As an example, if avoid_enemies=1 and the shortest path crosses 2 hexes that can be reached by 3 enemies each, the unit is willing to take a path that is up to 6 hexes longer instead.  If avoid_enemies=10 in the same situation, the unit will take a path up to 60 hexes longer.&lt;br /&gt;
** Hexes adjacent to enemy units are always strongly avoided, independent of the value of avoid_enemies.&lt;br /&gt;
** avoid_enemies is ignored if 'use_straight_line=true' is set.&lt;br /&gt;
** Due to an issue with the path finding code, '''tunnels and teleports are ignored in Wesnoth 1.14 and before''' if 'avoid_enemies' is used. This has been fixed in Wesnoth 1.15.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units to be moved.  Defaults to all units of the side if not defined.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''ignore_enemy_at_goal'''=no and '''ignore_units'''=no: (boolean)  In the default configuration, the Goto MAI does not move units if the path to all goal hexes is blocked by enemies.  It is therefore possible to disable the Goto MAI by putting enemy units on all the goal hexes.  That can be changed if either of these two keys is set to 'yes'.&lt;br /&gt;
** 'ignore_enemy_at_goal=yes' makes the AI choose its goals while disregarding any enemy units at the goal hexes.  Even so, a stiff rating penalty is applied to goal hexes occupied by enemies, thus that unoccupied goal hexes are chosen unless all possible goals are occupied by enemies.  Note that this setting ''only'' ignores enemy units at the goal hexes themselves.  It is still possible to disable the AI by putting enemies all around the goals.&lt;br /&gt;
** 'ignore_units=yes' let's the AI ignore ''all'' enemy units when deciding which goal hex(es) to move toward.&lt;br /&gt;
** In both cases, enemies are only ignored for deciding which unit to move toward which goal hex.  For the actual move itself, all enemies and their ZoCs are considered as usual.&lt;br /&gt;
** Both options only take effect if neither avoid_enemies is used, nor use_straight_line is set to 'yes', as neither of these consider enemies as blockers. Or in other words, if avoid_enemies is used, or use_straight_line=yes, it is unnecessary to set ignore_enemy_at_goal or ignore_units to yes.&lt;br /&gt;
* '''release_unit_at_goal'''=no: (boolean) By default, a unit that reaches a goal location will stay there until the Goto Micro AI is deleted or changed (or until the conditions specified in the [goto_goals] SLF change).  If this parameter is set, a unit that has reached its goal will not be controlled by this Micro AI any more starting from the next turn.&lt;br /&gt;
* '''release_all_units_at_goal'''=no: (boolean) Same as ''release_unit_at_goal'', but this will deactivate the Goto Micro AI for all units once the first unit reaches a goal.&lt;br /&gt;
* '''remove_movement'''=yes: (boolean):  {{DevFeature1.15|11}}  By default, when the Goto MAI cannot move a unit (for example, because the path is blocked by enemies), its movement points are still removed. If this option is set to 'no', this step does not happen, so that other AI candidate actions (or even another Goto MAI with different parameters) can take over instead.&lt;br /&gt;
* '''unique_goals'''=no: (boolean)  If set to 'yes', each unit will be sent to a different goal location.  Note that the best goals are recalculated each turn, so a unit might choose a different goal this turn from what it was heading for during the previous turn if the situation on the map has changed.  Also note that if there are more units than goal locations, the left over units will not be handled by the Goto MAI but follow default behavior instead.&lt;br /&gt;
* '''use_straight_line'''=no: (boolean)  By default, units choose the route by which they will get to their goal locations in the smallest number of turns.  If this parameter is set to 'yes', a straight line route (or something pretty close to it at least) will be chosen instead, irrespective of whether this leads into a dead end, whether it is blocked by enemies, etc.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* Depending on the parameters used, the Goto Micro AI might require a lot of path finding to be done.  Thus, while it is possible to set up this AI to move many units to many different locations, a usage like that might require quite a long calculation time.&lt;br /&gt;
&lt;br /&gt;
===Goto Micro AI example usages:===&lt;br /&gt;
&lt;br /&gt;
Here are several examples of goto [micro_ai] tag usages from the &amp;quot;Goto&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
* Send a unit toward a castle (similar to the RCA AI's Goto Candidate Action, but avoiding dead ends and areas ZoCed by enemies)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
             ca_id=messenger1&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=messenger1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=6-7,2-4&lt;br /&gt;
                 terrain=C*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             ca_score=210001&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send all units of the side toward the southern border of the map&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=8&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                  y=33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             avoid_enemies=1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send units to the four corners of the map (with each unit going to a different corner).&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x=1,1,44,44&lt;br /&gt;
                 y=1,33,1,33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             unique_goals=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Set up a guardian unit that doesn't move while no enemy unit is within 8 hexes (ironically, the Goto Micro AI can be used to keep units in place, although using one of the Guardian Micro AIs might often be a better idea)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=guard1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 [filter]&lt;br /&gt;
                     id=guard1&lt;br /&gt;
                 [/filter]&lt;br /&gt;
                 [not]&lt;br /&gt;
                     [filter]&lt;br /&gt;
                         side=1&lt;br /&gt;
                     [/filter]&lt;br /&gt;
                     radius=8&lt;br /&gt;
                 [/not]&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Guardian Micro AIs ==&lt;br /&gt;
=== General Comments on the Guardian Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Guardian Micro AIs can be used to set up different kind of guardian behaviors for a unit or a set of units.  Four different types of guardians are available and are described below.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different guardian behaviors, check out the &amp;quot;Guardians&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
=== Return Guardian  (ai_type=return_guardian)===&lt;br /&gt;
&lt;br /&gt;
A 'return guardian' is a variation of the standard Wesnoth guardian. It has an assigned guard position (GP) to which it returns after attacks on approaching enemies:&lt;br /&gt;
* If at GP with no enemy in reach, do nothing.&lt;br /&gt;
* If at GP with an enemy in reach, attack. Note that the attack is done by the default AI, which might result in the guardian not attacking if the enemy is deemed too strong (this can be influenced by setting the default ''aggression'' value to a different value).&lt;br /&gt;
* If not at GP, return there, no matter whether an enemy is in reach or not.&lt;br /&gt;
* If enemies are blocking the way back to GP, do your best to move around them.&lt;br /&gt;
* If the guardian ends up next to an enemy on the way back, attack that enemy after the move.&lt;br /&gt;
&lt;br /&gt;
Enable the Return Guardian Micro AI by using&lt;br /&gt;
 ai_type=return_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''return_x,return_y''': The coordinate of the hex the unit returns to after each attack.  {{DevFeature1.15|0}} '''return_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=100010: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  This AI consist of 2 CAs, one with score &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt; the other with &amp;lt;tt&amp;gt;ca_score-20&amp;lt;/tt&amp;gt;.  The default value is set so that the first CA gets executed right before the default combat CA, and the other right after.  It usually will not make sense to change this value, but it is possible to do so for custom applications.  {{DevFeature1.13|6}}: This is now changed to 100100 and 99900 in order to avoid conflicts with the new high_XP_attacks candidate action.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a return guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=return_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=return1&lt;br /&gt;
             return_x,return_y=20,2&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
===Stationed Guardian (ai_type=stationed_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A 'stationed guardian' is another variation of the standard Wesnoth guardian with a somewhat more complex behavior than that of the 'return guardian'. Two positions are defined for it, a 'station' and a 'guarded location', as well as a distance. The behavior is as follows:&lt;br /&gt;
* If no enemy is within 'distance' of the guard's current position, do nothing.  This is independent of the guardian being at its station or not.&lt;br /&gt;
* Otherwise: If an enemy is within 'distance' of the guard, but not also within the same distance of the guarded location and the station (all of this simultaneously), move the guard in the direction of the station (or keep the guard in place if the station is unreachable).&lt;br /&gt;
* Otherwise:&lt;br /&gt;
** Pick the enemy unit that is closest to the guarded location.&lt;br /&gt;
** If we can reach it, pick the adjacent hex with the highest defense rating and attack from there.&lt;br /&gt;
** If not in reach, move toward this unit.&lt;br /&gt;
&lt;br /&gt;
Enable the Stationed Guardian Micro AI by using&lt;br /&gt;
 ai_type=stationed_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance parameter as explained above&lt;br /&gt;
* '''station_x,station_y''': The x and y position of the hex that serves as the guardians station. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''guard_x, guard_y''': The x and y position of the guarded location.  {{DevFeature1.13|0}}  These are now optional keys, defaulting to station_x,station_y if not provided. {{DevFeature1.15|0}} '''guard_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a stationed guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=stationed_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=stationed1&lt;br /&gt;
             distance=4&lt;br /&gt;
             station_x,station_y=2,14&lt;br /&gt;
             guard_x,guard_y=3,13&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Coward (ai_type=coward) ===&lt;br /&gt;
&lt;br /&gt;
Cowards are units that, like guardians, sit around doing nothing until an enemy comes into range.  Unlike guardians, however, they run away once enemies approach. Applications might be wild animals, unarmed civilians getting in the way of a battle, etc.  The coward macro can be called with two optional locations, 'seek' and 'avoid':&lt;br /&gt;
* If neither is given, the coward retreats to the position farthest away from the approaching enemies.&lt;br /&gt;
* If 'seek' is given, it preferentially goes toward that location (but getting away from enemies takes priority).&lt;br /&gt;
* If 'avoid' is given, it in addition tries to avoid that location (with both maximizing distance from enemies and going toward 'seek' taking priority).&lt;br /&gt;
* Both 'seek' and 'avoid' may consist of only one coordinate ('x' or 'y'), in which case not a single hex, but a line of hexes is sought or avoided.&lt;br /&gt;
&lt;br /&gt;
Enable the Coward Micro AI by using&lt;br /&gt;
 ai_type=coward&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance within which an enemy must be to &amp;quot;scare&amp;quot; the unit&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_if_trapped'''='no':  {{DevFeature1.13|0}}  With the default settings, a coward never attacks.  If this key is set to 'yes', the coward attacks the weakest (lowest hitpoints) enemy next to which it ends up after moving (such as a cornered animal would do, blindly lashing out without regards for its own safety).&lt;br /&gt;
* '''avoid_x, avoid_y''': The x and y coordinate of the hex to avoid. {{DevFeature1.15|0}} '''avoid_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units from which the cowards flee.  If not set, this defaults to all enemy units.  Note, however, that no &amp;quot;enemy of AI side&amp;quot; condition is added if [filter_second] is specified, meaning that it can include allied and/or enemy units as desired (and even units on the same side as the coward).&lt;br /&gt;
* '''seek_x, seek_y''': The x and y coordinate of the hex to seek. {{DevFeature1.15|0}} '''seek_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a coward [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=coward&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=coward3&lt;br /&gt;
             distance=5&lt;br /&gt;
             seek_x,seek_y=24,5&lt;br /&gt;
             avoid_x,avoid_y=24,15&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Zone guardian (ai_type=zone_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A zone guardian is a unit that, as the name says, guards a zone. It moves randomly inside this zone until an enemy enters it (or a separately defined enemy zone, see below). Applications might be the defense of a castle, a nesting area or for protecting an unit.  The zone macro can be called with an optional SLF, [filter_location_enemy]:&lt;br /&gt;
* If not specified, the zone guard attacks any enemy coming inside its guard zone.&lt;br /&gt;
* If [filter_location_enemy] is given, it attacks any enemy entering this zone and once there are no more enemies, it goes back to patrol in its basic zone.&lt;br /&gt;
&lt;br /&gt;
Enable the Zone Guardian Micro AI by using&lt;br /&gt;
 ai_type=zone_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone to guard.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_enemy]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone where enemies have to be attacked.  If not given, it defaults to [filter_location].&lt;br /&gt;
* '''station_x,station_y''': By default, the zone guardian moves randomly within the zone if there is no enemy to attack.  However, if station_x/y is given, it moves to this position instead during turns when it does not have an enemy to attack.  This position does not need to be inside the zone defined above.  Note: obviously it does not make sense to define [filter_location], [filter_location_enemy] and station_x/y all at the same time.  Only [filter_location] and either one of the latter two (or none of them) should be provided for any zone guardian. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a zone guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=zone_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=zone3&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=22-31,4-11  # This is intentionally chosen to extend past the lake&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             station_x,station_y=32,8&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Hang Out Micro AI (ai_type=hang_out) ==&lt;br /&gt;
&lt;br /&gt;
The Hang Out Micro AI instructs units of the AI side to &amp;quot;hang out&amp;quot; around a certain location, or locations, until a condition for mobilizing the side is met.  If none of the optional keys are given, the AI controls all units of the side to hang around the leader while moving off castle tiles to make room for recruiting.&lt;br /&gt;
&lt;br /&gt;
The AI works by moving each unit to its best hex first using the minimum amount of movement points required for this move.  Once all AI units are in position, movement points are set to zero for all of them simultaneously such that they cannot be used by other AI candidate actions.  This is done because the mobilizing condition might be met as result of one of the moves of the Hang Out AI, after which we want units to be able to continue with other AI actions.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Hang Out and Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Hang Out Micro AI by using&lt;br /&gt;
 ai_type=hang_out&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Hang Out specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  In its default configuration, the Hang Out MAI controls all units of a side, keeping them as close to the side leader(s) as possible while moving off castle tiles to make room for recruiting.  For this purpose, the candidate action score defaults to 170,000, which is just below the score of the default recruiting CA.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''': A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  If neither of these two exists, this parameter is set to default to all castle terrain (&amp;lt;tt&amp;gt;terrain=C*,C*^*,*^C*&amp;lt;/tt&amp;gt;) so that units move off the castle to make room for recruiting.  Note that this does not include keep terrain, otherwise the leader would be moved off the keep as well.&lt;br /&gt;
* '''ca_score'''=170000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 170,000, which means that it is executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Hang Out action (&amp;lt;tt&amp;gt;side=&amp;lt;/tt&amp;gt; is always set and does not need to be provided).  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the locations ''near'' which the AI units should hang out.  This does not mean that the units only move onto terrain specified here, but that they minimize the distance to such terrain.  It defaults to the position of the side leader(s).&lt;br /&gt;
* '''[mobilize_condition]''':  Contains ([[ConditionalActionsWML#Condition_Tags|WML Condition Tags]])  Once the condition set up here has been met, the Hang Out MAI is disabled and the side goes over to other AI activity.  The Hang Out MAI remains disabled after this, even if the condition is not met any more later in the scenario.&lt;br /&gt;
* '''mobilize_on_gold_less_than''': (number) Since side gold cannot be checked in a WML condition tag without storing it in a variable first, this key is provided separately.  It disables the Hang Out Micro AI once the side gold goes below the value specified here for the first time.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
* The Hang Out AI still takes effect if no locations to hang out at are found or if all map locations are to be avoided.  In that case, it simply keeps all units at their current locations.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Hang Out Micro AI configuration from the &amp;quot;Hang Out and Messenger&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hang_out&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [avoid]&lt;br /&gt;
                 terrain=C*,H*,M*,A*,S*,*^F*&lt;br /&gt;
             [/avoid]&lt;br /&gt;
             [mobilize_condition]&lt;br /&gt;
                 [have_unit]&lt;br /&gt;
                     side=1&lt;br /&gt;
                     count=7-99&lt;br /&gt;
                 [/have_unit]&lt;br /&gt;
             [/mobilize_condition]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Healer Support Micro AI (ai_type=healer_support) ==&lt;br /&gt;
&lt;br /&gt;
The Healer Support Micro AI configures the healers of a side to stay behind the battle lines and heal injured and/or threatened units rather than participate in combat under all circumstances.  You can set different levels of aggressiveness for the healers.  &lt;br /&gt;
&lt;br /&gt;
'''Notes'''&lt;br /&gt;
* By default, ''all'' healers of the AI side are controlled by this AI.  This includes in particular also the side leader, if the leader is a healer.  If you do not want the leader to be included (and possibly be moved away from the keep etc.) use the [filter] tag with canrecruit=no (see below).&lt;br /&gt;
* It is not always better to use this Micro AI than the default AI, as it takes healers (which can be strong units) away from the pool of units to choose for attacks.&lt;br /&gt;
* {{DevFeature1.15|3}} This Micro AI now takes unit guardian status and the passive_leader aspect into account.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Healers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Healer Support Micro AI by using&lt;br /&gt;
 ai_type=healer_support&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Healer Support specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the healer units.  If set, only units on the AI side passing this filter ''and'' having the healing ability are considered by this Micro AI.&lt;br /&gt;
*'''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting units to receive healing. If set, only units on the AI side passing this filter ''and'' the other healee selection criteria will be selected for healing by this Micro AI.&lt;br /&gt;
* '''aggression'''=1.0: (float) Sets the aggressiveness of the AI.  This parameter is set up as a float to accommodate future functionality, but it currently acts as a boolean: if set to zero, the AI will never let its healers participate in combat, for all other values it allows them to attack after all other units have attacked and if the healers cannot find any more units to support.  The former behavior might be appropriate in scenarios with only one or few valuable healers, while the latter might work better in scenarios with many healers.&lt;br /&gt;
* '''injured_units_only'''=no: (boolean) If set to 'yes', the AI will only move healers next to units that are already injured and skip units that currently have full hitpoints, but might get injured during the next enemy turn.&lt;br /&gt;
* '''max_threats'''=9999: (integer) The maximum allowable number of enemies that can attack a hex in order for it to be considered for a healer.  As an example, setting 'max_threats=0' means that the AI only moves healers to locations that are entirely safe from the enemy (assuming that none of the units currently on the map dies).  Note that the value of this key is checked against the number of enemies that can make it to the hex, not the number of adjacent hexes from which the healer could be attacked.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI. Note: as attacks by the healers are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The Healer Support Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded.  It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Healer Support Micro AI configuration from the &amp;quot;Healers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             aggression=0&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Lurkers Micro AI (ai_type=lurkers) ==&lt;br /&gt;
&lt;br /&gt;
The Lurker Micro AI controls some or all units of a side in &amp;quot;lurker mode&amp;quot;.  A lurker is a unit that is capable of moving across most terrains, but that only stops on and attacks from specific terrain.  It might also have the ability to hide on this terrain (which is the reason why it is called a lurker).&lt;br /&gt;
&lt;br /&gt;
Lurkers move individually without strategy and always attack the weakest enemy within their reach.  If no enemy is in reach, the lurker does a random move instead - or it just sits and waits (lurks).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of different types of lurker behavior, check out the &amp;quot;Lurkers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Lurkers Micro AI by using&lt;br /&gt;
 ai_type=lurkers&lt;br /&gt;
in the [micro_ai] tag&lt;br /&gt;
&lt;br /&gt;
===Lurkers specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
* '''[filter]''': Specifies which units of the side are controlled by the Lurker Micro AI.  This is a [[StandardUnitFilter|Standard Unit Filter]].  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': Specifies the terrain from which the lurkers attack. This is a [[StandardLocationFilter|Standard Location Filter]].&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_wander]''': Specifies the terrain on which lurkers end their moves if there is no enemy in reach. This is a [[StandardLocationFilter|Standard Location Filter]].  If [filter_location_wander] is not given, it defaults to [filter_location].&lt;br /&gt;
* '''stationary'''=no: (boolean)  If set to yes, a lurker never moves if there is no enemy within attack range.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Lurkers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=4&lt;br /&gt;
             ai_type=lurkers&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Naga Fighter&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=W*,S*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             [filter_location_wander]&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location_wander]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Messenger Escort Micro AI (ai_type=messenger_escort) ==&lt;br /&gt;
&lt;br /&gt;
The Messenger Escort Micro AI lets you define a set of waypoints through which it tries to move one or several of its units, the messenger(s).  The other units escort the messenger, trying to protect it and attacking enemies that are in its way.  Note that a messenger might not strictly hit intermediate waypoints, but that getting close is good enough, especially if a waypoint is occupied by an enemy unit.  It does, however, always try to get to the last waypoint.&lt;br /&gt;
&lt;br /&gt;
'''Notes on the use of several messengers:''' {{DevFeature1.13|0}}  &lt;br /&gt;
* This Micro AI can control several messengers at the same time, which can but do not have to move in a group.  They can be put on the map altogether in one location, or be spread out on the map or across different turns.&lt;br /&gt;
* The farther a messenger has moved through the chain of waypoints, the higher a priority it is for the AI, meaning that farther advanced (on the map) messengers are moved first, and that the escort units try to clear the way and stay with those messengers more so than with those further back on the map.  This order can be inverted using the invert_order= key.&lt;br /&gt;
* If several messengers are on the map, escort units might move from one to the other, the AI making a judgement call where they are needed most.  Closeness to a messenger is generally the most important criterion, but not the only one.&lt;br /&gt;
* If a messenger dies, the escort units previously staying with it now join one of the other messengers if there are any left (otherwise they follow default AI behavior).&lt;br /&gt;
* If you want certain escort units to stay always with the same messenger, use several messenger [micro_ai] tags with different [filter]/[filter_second] combinations.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Messenger Escort Micro AI by using&lt;br /&gt;
 ai_type=messenger_escort&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Messenger Escort specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': {{DevFeature1.13|0}} (Previous to 1.13.0, only id= was allowed.) The id of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated list of waypoint coordinates through which the messenger will go in the given order to reach its goal, given by the last coordinate.  If you want the messenger to go directly to the goal, simply enter a single x,y coordinate here.  Note that the messenger does not have to hit each waypoint exactly (except for the final one), getting close to them is good enough. {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Messenger Escort AI consists of 3 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-2&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_death_chance'''=0.67 and '''messenger_death_chance'''=0.0: (both floats; ranging from 0 to 1)    If the messenger is adjacent to an enemy at the end of the move, it will only attack the enemy if the chance of death of the enemy is &amp;gt;= enemy_death_chance and if the messenger's chance of death is &amp;lt;= messenger_death_chance (or if it is an attack at a range for which the enemy does not have a weapon).&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|0}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the escort units.  If not set, this defaults to all enemy units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''invert_order'''=no: {{DevFeature1.13|0}} If set to 'yes', the order of priority with which messenger units are considered is inverted, starting with the messenger unit that is the least advanced on the map (see description above).  In most cases, selecting this option results in worse performance of the AI, so use it with caution.  (And obviously it has no effect at all if there is only one messenger controlled by the AI.)&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Messenger Escort&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=messenger_escort&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=messenger&lt;br /&gt;
             waypoint_x=31,24,27,28&lt;br /&gt;
             waypoint_y=20,14,7,1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Patrol Micro AI (ai_type=patrol) ==&lt;br /&gt;
&lt;br /&gt;
The defines a number of waypoints for a unit (or units) to follow.  Options can be set whether the route should be followed in a loop, as an out-and back, only once, whether units encountered along the way should be attacked, etc.&lt;br /&gt;
&lt;br /&gt;
No matter what options are chosen, getting to the next waypoint always takes priority over attacking for a patroller.  The AI thus prefers to move the patrol unit around enemies rather than straight for them.  Also, if a waypoint is occupied by a unit the AI is not instructed to attack, it will (eventually) abandon that waypoint once it gets close enough and move on to the next one.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different patrol behaviors, check out the &amp;quot;Patrols&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Patrol Micro AI it by using&lt;br /&gt;
 ai_type=patrol&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Patrol specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated lists of the waypoint coordinates through which the patrol travels (in order). {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack''': By default (if this key is not set), the patrol unit attacks any unit it ends up next to after a move.  Alternatively, this parameter can be set to a comma-separated list of unit ids. If that is done, the patrol only attacks these units when it ends up next to them and ignores any other unit.  This can in turn be used to have the patroller not attack at all, by setting ''attack='' to a non-existing id.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''one_time_only'''=no: (boolean)  If set to 'yes', the unit goes through the patrol route only once and then hangs out at the last waypoint.  It also always attacks any enemy unit on the last waypoint, independent of what ''attack='' is set to.&lt;br /&gt;
* '''out_and_back'''=no: (boolean)  By default, a patrol unit returns to the first waypoint after reaching the last, doing the patrol route as a loop.  If this parameter is set to 'yes', the unit instead reverses its course and perpetually does out and backs of the route.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Patrols&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=patrol&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Konrad&lt;br /&gt;
             waypoint_x=9,24,25&lt;br /&gt;
             waypoint_y=21,23,15&lt;br /&gt;
             one_time_only=yes&lt;br /&gt;
             attack=Gertburt&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Protect Unit Micro AI (ai_type=protect_unit) ==&lt;br /&gt;
&lt;br /&gt;
The Protect Unit Micro AI tries to move one or several units to a given location while keeping them safe from enemy attacks. It does so by keeping the protected units away from enemy units and close to friendly units. Other units on the AI side are controlled by the default Wesnoth AI.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Protect Unit&amp;quot; and &amp;quot;The Elves Besieged&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Protect Unit Micro AI by using&lt;br /&gt;
 ai_type=protect_unit&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Protect Unit specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[unit]''': A separate [unit] tag is required for each protected unit(s).  Each tag has required keys '''id''', '''goal_x''', '''goal_y''' for each unit, as shown in the example below. {{DevFeature1.15|0}} '''goal_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''disable_move_leader_to_keep'''=no: (boolean) If set to 'yes', side leaders protected by this AI do not try to return to their keep.  This is necessary for this Micro AI to work with units that are side leaders and that are supposed to move to an off-keep location.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
*The Protect Unit Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].  Depending on the parameters, it might also modify the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|move leader to keep candidate action]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded. It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;HttT: The Elves Besieged&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=protect_unit&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Delfador&lt;br /&gt;
                 goal_x,goal_y=1,2&lt;br /&gt;
             [/unit]&lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
                 goal_x,goal_y=1,1&lt;br /&gt;
             [/unit]&lt;br /&gt;
 &lt;br /&gt;
             disable_move_leader_to_keep=true&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Recruiting Micro AI==&lt;br /&gt;
=== General Comments on the Recruiting Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Recruiting Micro AI replaces the default recruitment AI by alternative recruiting patterns.  Currently there are two recruiting algorithms you can choose from, random recruiting and the rush recruiting used in the new (as of Version 1.11.1) Experimental AI (see below).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of a different recruiting pattern, check out the &amp;quot;Recruiting&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  You can also explore the unit choices by watching the Experimental AI in a multiplayer game.&lt;br /&gt;
&lt;br /&gt;
'''Note:''' Obviously, the Recruitment Micro AI modifies the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|recruitment candidate action]].&lt;br /&gt;
&lt;br /&gt;
=== Random Recruitment AI (ai_type=recruit_random) ===&lt;br /&gt;
&lt;br /&gt;
This AI randomly selects a unit type from the recruitment list of the side.  The probability of choosing each type and the behavior in low-gold situations can be influenced with optional parameters.&lt;br /&gt;
&lt;br /&gt;
Enable the Random Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_random&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* '''[probability]''': This tag sets the probability of selecting the given unit type.  It has two required sub-keys:&lt;br /&gt;
** '''type''': comma-separated list of unit types to which this probability is to be applied&lt;br /&gt;
** '''probability''': (non-negative float)  The probability with which these unit types is to be selected.  All unit types for which no [probability] tag is defined receive a default probability of 1.0, thus values should be chosen relative to that.  As an example, in the code below swordsmen and peasants are given a probability of 8, while no other units have assigned probabilities.  Thus, each of these two unit types is 8 times more likely to be selected than any other individual unit type.&lt;br /&gt;
* '''skip_low_gold_recruiting'''=no: (boolean)  By default, the random recruitment AI chooses a unit to recruit only from those types for which it has sufficient gold.  If this parameter is set to 'yes', it will instead choose from all available unit types and end recruiting for the turn if there is not enough gold to recruit it this turn (even if other, affordable unit types exist).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a random recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_random&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [probability]&lt;br /&gt;
                 type=Swordsman,Peasant&lt;br /&gt;
                 probability=8&lt;br /&gt;
             [/probability]&lt;br /&gt;
 &lt;br /&gt;
             skip_low_gold_recruiting=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Rush Recruitment AI (ai_type=recruit_rushers) ===&lt;br /&gt;
&lt;br /&gt;
This AI replaces the default recruiting with the rush recruiting used in the new (as of Version 1.11.1) Experimental AI.&lt;br /&gt;
&lt;br /&gt;
Enable the Rush Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_rushers&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* {{DevFeature1.15|2}} '''high_level_fraction'''=0: (non-negative number) The approximate fraction of units of level 2 or higher to be recruited. This is defined as fraction of units on the map, not of new units being recruited during a turn or over several turns (which makes a difference if there are already units on the map). The effect is also cumulative per level (starting from level 2), meaning that 1/3 of L2 units will be recruited in the example above, 1/3*1/3=1/9 L3s etc. The default value is zero, which leaves the high-level unit recruiting to the default Experimental AI algorithm resulting usually in very few such units being recruited.&lt;br /&gt;
* '''randomness'''=0.1: (number)  A random number is applied to the rusher recruit engine's score to prevent the recruitment pattern from being too predictable. 0 causes no randomness to be applied, while larger numbers increase the random effect. A value of 1-2 generates results in which the random effect is approximately equal to the scored effect. Extremely high values are essentially entirely random.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a rush recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_rushers&lt;br /&gt;
             action=add&lt;br /&gt;
          [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Simple Attack Micro AI (ai_type=simple_attack) ==&lt;br /&gt;
&lt;br /&gt;
The Simple Attack Micro AI is meant as an addition to the default combat candidate action for when one wants to force the AI to do attacks that it would otherwise not do (for example on units that are 1 XP from leveling) or to execute certain attacks before all others (for example, attack with all &amp;quot;expendable&amp;quot; units first).  It is not set up to be a replacement for the default attacks.  Some notes:&lt;br /&gt;
&lt;br /&gt;
* The algorithm is different from and simpler than that of the default combat CA.  For example, no attack combinations are considered, only individual attacks.  As a result, the [[AiWML#List_of_AI_Aspects|default aspects]] (aggression, caution, [avoid], ...) are not taken into account.  Also, if lots of units are involved, evaluation of this Micro AI is potentially quite a bit slower than of the default attacks.&lt;br /&gt;
&lt;br /&gt;
* If an attack matching the Standard Unit Filters described below is found, this attack will always be executed, no matter how bad the odds are.  Thus, this Micro AI can be used to force the AI to do attacks it would otherwise avoid.&lt;br /&gt;
&lt;br /&gt;
* Other than selecting the attacker/defender units, this AI is currently not configurable.  Let us know if you would like to see certain options to be added.  (No guarantees that we will add them, but we will definitely consider them.)&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Simple Attack&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=simple_attack&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Simple Attack specific keys for the [micro_ai] tag===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  It is, however, in general not desirable to use this AI without at least one of the optional Standard Unit Filters below, as this AI is not meant as a replacement of the default combat candidate action.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=110000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 110,000, which means that it is executed right before the default combat candidate action.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Simple Attack action.  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units the AI should attack.  It defaults to all enemy units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
* '''weapon'''=-1: (integer) The number of the weapon that should be used for this attack.  Weapon numbers start at 1 (not 0).  -1 stands for &amp;quot;best weapon&amp;quot; (by whatever criterion the AI chooses to use).   '''Available from Wesnoth 1.11.8'''&lt;br /&gt;
&lt;br /&gt;
Below are 2 examples of Simple Attack Micro AI configurations from the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  The first one shows how to force the AI to attack with all Soulless and Walking Corpse units first:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110000&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless,Walking Corpse&lt;br /&gt;
             [/filter]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
The second example executes attacks by Soulless on all enemy units that are 1 XP from leveling.  Note that the Lua code in the preload event is there simply in order to code the &amp;quot;1 XP from advancing&amp;quot; condition.  It is not needed for most other SUFs, as can be seen in the previous example.&lt;br /&gt;
     [event]&lt;br /&gt;
         name=preload&lt;br /&gt;
         first_time_only=no&lt;br /&gt;
         [lua]&lt;br /&gt;
             code=&amp;lt;&amp;lt;&lt;br /&gt;
                 function close_to_advancing(unit)&lt;br /&gt;
                     if (unit.experience &amp;gt;= unit.max_experience-1) then&lt;br /&gt;
                         return true&lt;br /&gt;
                     else&lt;br /&gt;
                         return false&lt;br /&gt;
                     end&lt;br /&gt;
                 end&lt;br /&gt;
             &amp;gt;&amp;gt;&lt;br /&gt;
         [/lua]&lt;br /&gt;
     [/event]&lt;br /&gt;
 &lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110001&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless  # No Walking Corpses; L0 units don't advance enemy&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 lua_function = &amp;quot;close_to_advancing&amp;quot;&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
Several Simple Attack Micro AIs can be combined for the same AI side.  The two examples above are both used (note the different values for ca_score) in the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
== Custom Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|5}}&lt;br /&gt;
&lt;br /&gt;
From 1.13.5 onwards, adding a new Micro AI can be easily done without altering any core files or overriding the micro_ai tag. The [micro_ai] tag now looks up its ''ai_type'' parameter in the &amp;lt;code&amp;gt;wesnoth.micro_ais&amp;lt;/code&amp;gt; table and uses the micro AI setup function found there to determine which keys are required and allowed and what candidate actions to add.&lt;br /&gt;
&lt;br /&gt;
A micro AI setup function takes as its single parameter the contents of the [micro_ai] tag and returns three separate values:&lt;br /&gt;
*'''required''': A list of required keys and tags, as strings. Tags are indicated by enclosing them within square brackets in the string. If a key or tag in this list is missing from the [micro_ai] tag, an error will be printed.&lt;br /&gt;
*'''optional''': Similar to above, a list of optional keys and tags, as strings. No error will be printed if a key or tag in this list is missing, nor will an error be printed if it is present (as would be the case for unrecognized keys or tags).&lt;br /&gt;
*'''ca_params''': A table specifying the candidate actions that constitute the micro AI. It should contain an ''ai_id'' key (which specifies a base ID to be used for candidate actions of this micro AI) and a series of candidate action definitions. Each candidate action definition is a table containing the following keys:&lt;br /&gt;
**'''ca_id''': The ID used for the candidate action. This will be combined with the ''ai_id'' to produce the final ID.&lt;br /&gt;
**'''location''': Path to the Lua file that defines the candidate action.&lt;br /&gt;
**'''score''': The maximum score for the candidate action. If the [micro_ai] tag specified a ''ca_score'' parameter, the score here should normally be based on that.&lt;br /&gt;
&lt;br /&gt;
(The return statement would thus look like &amp;lt;syntaxhighlight lang='lua' inline&amp;gt;return required, optional, ca_params&amp;lt;/syntaxhighlight&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
'''Examples''' of micro AI setup functions can be found in the [https://github.com/wesnoth/wesnoth/tree/1.14/data/ai/micro_ais/mai-defs default MAI definitions directory].  See the [https://github.com/wesnoth/wesnoth/blob/1.14/data/ai/micro_ais/mai-defs/animals.lua#L4-L12 Big Animals MAI definition] for a simple example.  For custom micro AIs, the setup code should be put into a ''[lua]'' tag inside a preload event.&lt;br /&gt;
&lt;br /&gt;
Since the function has full access to the contents of the [micro_ai] tag, it can conditionally add candidate actions to '''ca_params''' depending on the values of other keys within the [micro_ai] tag. It can also perform other actions such as adding facets to an aspect; if it does such things, it should pay attention to the ''action'' key to determine whether it's being added or removed.&lt;br /&gt;
&lt;br /&gt;
There are two useful helper functions that can be used by Micro AI setup functions requiring aspect changes. They can be brought into scope by a line similar to the following:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='lua&amp;gt;local MAIH = wesnoth.require(&amp;quot;ai/micro_ais/micro_ai_helper.lua&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* MAIH.add_aspects(side, aspect_params)&lt;br /&gt;
* MAIH.delete_aspects(side, aspect_params)&lt;br /&gt;
&lt;br /&gt;
The ''aspect_params'' parameter is an array of tables, each of which contains the following keys:&lt;br /&gt;
*'''aspect''': The aspect to change.&lt;br /&gt;
*'''facet''': The WML defining the facet. This must include an ''id'' key in order for the aspect to be correctly deleted. For maximum compatibility, incorporate the ''ca_id'' key from the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=66692</id>
		<title>Micro AIs</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=66692"/>
		<updated>2021-02-27T18:49:38Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Goto specific keys for the [micro_ai] tag: */ minor change to wording&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== A Few General Words about Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
* Micro AIs allow Wesnoth campaign and scenario authors to add new AI functionality without the need for any AI programming. They bring new specialized behavior to Wesnoth that cannot be achieved, at least not easily, by adjusting default AI parameters (such as those described at [[AiWML]]) or with WML code.&lt;br /&gt;
&lt;br /&gt;
* A Micro AI is activated and configured (or deleted) via the [micro_ai] tag, requiring only a few lines of WML code.&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.6''', the Micro AI behavior was inconsistent when it came to '''hidden enemies and petrified units'''.  Some Micro AIs ignored those, while others did not.  In some case there was even a potential for AI errors when an AI unit ran into hidden or petrified enemies.  As of 1.13.6, this has all been fixed.  The Micro AIs now correctly and consistently ignore hidden units and treat petrified units as blocking the hex they are on but having no effect otherwise, in the same way as the default AI does.  '''Important notes:'''&lt;br /&gt;
** It is not possible to re-include hidden/petrified units via the Standard Unit Filters (SUFs) used by some of the Micro AIs. In fact, if this is attempted, the filters will generally not match any unit.&lt;br /&gt;
*** This only applies to SUFs which go directly into the [micro_ai] tag. Any SUF that is a sub-tag of another filter (for example, of a Standard Location Filter) is taken literally and returns all units matching the filter. If hidden or petrified units are to be excluded from such a sub-filter, that needs to be specified directly in that filter.&lt;br /&gt;
** We also note that, because all the Micro AIs are written in Lua, it is possible to [[Micro_AIs#Using_Development_.281.13.29_Version_Micro_AIs_in_your_Stable_.281.12.29_Version_Add-on|move a Micro AI's code into one's add-on]] and modify it to whatever scenario-specific behavior is desired (this does not only apply to hidden/petrified units, but to all behavior). Setting up general Micro AI behavior that works in every possible setting is not feasible, but adapting it to work (only) in the specific setting of a given scenario is often quite simple. If you do not know how to do this, please post a message on the Wesnoth forums and we will be happy to help you with it.&lt;br /&gt;
&lt;br /&gt;
* The '''[[AiWML#List_of_AI_Aspects|default AI aspects]] are, for the most part, not taken into account by the Micro AIs'''.  For example, unless noted otherwise, specifying a value for ''aggression'' does not change the MAI behavior, and locations specified in an ''[avoid]'' tag generally can be entered by units controlled by an MAI.  This is a feature of the Micro AIs, not a bug.  If you feel that a certain default AI aspect should also apply to a Micro AI, please let us know in the [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 Micro AIs Feedback thread].&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.7''', the Micro AIs did not all work reliably when the '''AI side is under shroud''' and it may have been necessary to turn shroud off for the side when using a Micro AI. {{DevFeature1.13|7}} The Micro AIs now all work for sides under shroud. They do this by disregarding shroud for terrain purposes when path finding, consistent with default Wesnoth AI behavior, while still ignoring hidden units (including those hidden under shroud) correctly.&lt;br /&gt;
&lt;br /&gt;
* Most Micro AIs in their default setting apply to all units of a side, although many of them provide parameters for filtering the units that should be affected by the new behavior.  If the Micro AI is applied only to part of the units of a side, the other units follow the default Wesnoth AI behavior.&lt;br /&gt;
&lt;br /&gt;
* Micro AIs are available in mainline since Wesnoth 1.11/1.12.&lt;br /&gt;
&lt;br /&gt;
== Micro AI Test and Demo Scenarios ==&lt;br /&gt;
&lt;br /&gt;
All Micro AIs described here can be checked out in a number of test and demo scenarios.  The easiest way to access the test scenarios is by assigning a hotkey to &amp;quot;Start Test Scenario&amp;quot; in the preferences (as of Wesnoth 1.13.8). Then, simply press the hotkey in the titlescreen and select the desired scenario from the list (e.g. 'micro_ai_test' for the overall &amp;quot;switchboard scenario&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Test scenarios can also be accessed from the command line by typing&lt;br /&gt;
 path/wesnoth-executable -t micro_ai_test&lt;br /&gt;
or (depending on your system and Wesnoth version)&lt;br /&gt;
 path/wesnoth-executable -tmicro_ai_test&lt;br /&gt;
This starts up a &amp;quot;switchboard scenario&amp;quot; in which you can select the Micro AI demonstration scenario you want to check out.  Here, ''path/wesnoth-executable'' needs to be replaced by whatever the path and filename of the Wesnoth executable is on your system.&lt;br /&gt;
&lt;br /&gt;
== Setting up a Micro AI ==&lt;br /&gt;
&lt;br /&gt;
Setting up a Micro AI requires only one simple step:&lt;br /&gt;
==== Activating and configuring (or deleting) a Micro AI ====&lt;br /&gt;
&lt;br /&gt;
Micro AIs are activated, deleted and configured using the [micro_ai] tag.  This tag needs to be placed in [[ActionWML]], that is, in an event, a menu option or the like.  As an example, the following code activates the healer_support Micro AI in its default configuration for Side 2 from the beginning of the scenario:&lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
 &lt;br /&gt;
         # Configure the healer support micro AI&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
'''Keys required for all Micro AIs:'''&lt;br /&gt;
All [micro_ai] tags must contain the following three required keys:&lt;br /&gt;
* '''side''': The side for which the Micro AI is to be added, changed or deleted.  This has to be an individual side number, lists of sides are not supported.&lt;br /&gt;
* '''ai_type''': The type of Micro AI to be added, changed or deleted.  See the following sections for allowed values.&lt;br /&gt;
* '''action''' (string): The action to take concerning the Micro AI.  The following values are allowed: &lt;br /&gt;
** ''add'': Add a Micro AI to a side.&lt;br /&gt;
** ''change'': Change the configuration of an existing Micro AI.  Note that this does not only change the specific parameters provided in the tag, but it replaces the entire existing Micro AI by a new version with the new (and only the new) configuration.  It is therefore equivalent to using first the ''delete'' and then the ''add'' action.&lt;br /&gt;
** ''delete'': Delete an existing Micro AI from a side.&lt;br /&gt;
&lt;br /&gt;
'''Keys required under certain circumstances:'''&lt;br /&gt;
* '''ca_id''': (string) If several MAIs of the same type are used for the same side and one or several of these are to be changed or deleted during a scenario, this key needs to be included so that the [micro_ai] tag knows which of them needs to be changed/deleted.  The same is also true if a Micro AI is changed more than once during a scenario.  We therefore '''strongly recommend always setting ''ca_id''''' for MAIs which are, or might be, changed or deleted during a scenario.&lt;br /&gt;
** '''Note:''' The id(s)/name(s) actually assigned to the CA(s) of the MAI by the engine might be different from the parameter passed here.  It is, however, sufficient to use this ca_id with the [micro_ai] tag add/delete/change actions to identify the MAI uniquely.  (In other words: you don't have to worry about this if you always use the [micro_ai] tag, but might have to figure out the actual name(s) of the CA(s) if you want to delete or change them manually.)&lt;br /&gt;
* Other keys may also be required depending on the Micro AI.  In additional, a number of optional keys may be available to configure the AI behavior.  See the sections on the individual Micro AIs for details.&lt;br /&gt;
&lt;br /&gt;
'''Required keys for deleting a Micro AI:'''&lt;br /&gt;
&lt;br /&gt;
Only a subset of the keys required for setting up a Micro AI are needed when deleting it.  '''side''', '''ai_type''' and '''action'''=delete always need to be provided.  '''ca_id''' needs to be given if it was provided when adding the Micro AI (see above for when this is needed).&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* The ''add'' and ''change'' actions ignore all keys that do not apply to the respective Micro AI type.  The ''delete'' action ignores all keys other than the required keys listed above.&lt;br /&gt;
&lt;br /&gt;
* Some of the Micro AIs use and/or modify default AI components (candidate actions or aspects) when they are added, or reset these components to their default values when they are deleted.  Thus, if you have modified the AI yourself using the default CAs or aspects, some of these modifications might be affected, or might be interfering with the Micro AI.  The sections below indicate whether a Micro AI modifies any default AI components.&lt;br /&gt;
&lt;br /&gt;
===Combining Different Micro AI Types for the Same Side===&lt;br /&gt;
&lt;br /&gt;
No extra steps need to be taken when combining different Micro AIs on the same side.  Simply set up the [micro_ai] tags for all AIs you want to use.&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* While it is ''technically'' possible to combine any number of Micro AIs, this does not mean that all Micro AIs work together in practice.  Some are not compatible because they control units in contradictory ways.  &lt;br /&gt;
* Not all combinations that ''should'' work have been tested yet.  If you find a combination that you believe should work but doesn't, [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 please let us know] and we will see if that can be fixed.&lt;br /&gt;
* The candidate action score almost certainly has to be set (using the ca_score= key) for one or several of the MAIs if several of them are used on the same side.&lt;br /&gt;
&lt;br /&gt;
== Animals Micro AIs ==&lt;br /&gt;
=== General Comments on the Animal Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Animals Micro AIs are meant to simulate the behaviors of certain types of animals.  They are, however, set up so that they can be used with arbitrary unit types and might be applicable in quite diverse situations.  The AIs nevertheless remain named after the animals for which they were written originally, so that they are (to some extent) descriptive of their behavior.&lt;br /&gt;
&lt;br /&gt;
Brief descriptions of the AI behaviors are given in the following subsections.  To get a better feeling for their behavior, check out the &amp;quot;Animals&amp;quot;, &amp;quot;Swarm&amp;quot;, &amp;quot;Wolves&amp;quot; and &amp;quot;Dragon&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  Note that some of the animal AIs are designed for animal types that do not exist in mainline.  In the test scenario, other animal types are therefore substituted for them.&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Wolves (ai_type=wolves) ===&lt;br /&gt;
&lt;br /&gt;
The Wolves Micro AI organizes all &amp;quot;wolf&amp;quot; units (predators) of the side to hunt in a single pack.  They actively chase after the closest prey and try to corner it (not always super successfully), but are easily distracted by other enemy units coming into attack range, to the point where the pack might split up.  The wolves can, however, be told to attack only prey units as well as to try to avoid other types of units (such as larger predators).  When no prey is left, the wolves wander randomly.&lt;br /&gt;
&lt;br /&gt;
This AI works best for a small number of predators (2 - 5) and sparse prey.  Also, the predators should be put on the map in a pack, that is, reasonably close to each other.  For a demonstration of the Wolves AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Note that another wolves AI exists, called [[Micro_AIs#Animals_AI:_Multi-pack_Wolves_.28ai_type.3Dwolves_multipacks.29|Multipack Wolves]], which distributes the wolves of a side into several packs, rather than all of them into one single pack as it is done here.&lt;br /&gt;
&lt;br /&gt;
Enable the Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the predator units.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the prey units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_only_prey'''=no: In the default configuration, the wolves attack all enemy units in attack range.  If this key is set to 'yes', they only attack prey units.&lt;br /&gt;
* '''avoid_type''': Comma-separated list of unit types (on enemy sides) which the wolves try to avoid.  They try to stay out of these units' way as much as possible when moving around (except when they are moving in to attack another enemy unit) and never attack them.&lt;br /&gt;
* '''ca_score'''=90000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 90,000, which is lower than the default combat CA, but higher than most other default CAs which move units.  It can be changed by setting this parameter but in most cases this will not make sense.  Note that the Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Notes on default (RCA) AI aspects:''' &lt;br /&gt;
* If ''attack_only_prey'' or ''avoid_type'' are set, the Wolves Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* Attacks by the wolves are left to the default (RCA) AI, meaning that the value of ''aggression'' has an influence on the behavior.&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI is taken into account. Note: as attacks by the wolves are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=6&lt;br /&gt;
             ai_type=wolves&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Wolf&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Vampire Bat&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             avoid_type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Multi-pack Wolves (ai_type=wolves_multipacks) ===&lt;br /&gt;
&lt;br /&gt;
The Multi-pack Wolves Micro AI is different from the [[Micro_AIs#Animals_AI:_Wolves_.28ai_type.3Dwolves.29|Wolves Micro AI]] in that there can be an arbitrary number of wolf packs, with the pack size being a free parameter.  At the beginning of the scenario, close wolves are grouped into packs in a semi-methodical way.  If the wolves are put on the map in distinct groups of close wolves of the correct number, they will be joined into packs matching these groups.  If, on the other hand, they are spread out all over the map, the method of assigning them to packs is semi-random.&lt;br /&gt;
&lt;br /&gt;
Wolves of the same pack begin by joining each other on the map.  After that, they stay together until only one wolf is left, which then tries to join up with an incomplete pack or with other single wolves.  Individual wolves entering the map during the scenario behave in that way as well.&lt;br /&gt;
&lt;br /&gt;
A second difference to the other Wolves AI is that wolves do not actively hunt here.  For the most part they just wander (often long distance).  However, the pack ferociously (and without regard for its own health) attacks any enemy units that come into range, as long as that does not mean separating the pack by more than a few hexes.  Staying together, or joining with a new wolf assigned to the pack, is the only thing that takes priority over satisfying the wolves' thirst for blood.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Multi-pack Wolves AI behavior, check out the &amp;quot;Wolves&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Multi-pack Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves_multipacks&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Multi-pack Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''pack_size'''=3: (integer) The size of the packs.&lt;br /&gt;
* '''show_pack_number'''=no: If set to &amp;quot;yes&amp;quot;, the wolves' pack numbers will be shown below them.  Note that this means that the pack number of a wolf killed in an attack will remain as a label on the map.  &lt;br /&gt;
* '''type'''=Wolf: Comma-separated list of the unit types which the AI controls. The default value is &amp;quot;Wolf&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves_multipacks [micro_ai] tag usage from the &amp;quot;Wolves&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=wolves_multipacks&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             show_pack_number=yes&lt;br /&gt;
             pack_size=4&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Big Animals (ai_type=big_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Big Animals Micro AI is a simulation of large predators that wander and hunt alone, such as Bears, Giant Spiders or Yetis.  For the most part, these just wander on the terrain that's been defined for them, but they attack enemy units if those happen to come into range.  The AI can be set up so that the Big Animals stay out of the way of other units (such as other large predators), in which case they only attack those if cornered or if they run into them accidentally (such as ambushers).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Big Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Big Animals Micro AI by using&lt;br /&gt;
 ai_type=big_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the animals to which this AI is applied.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': The big animals often go for long distance (multi-turn) journeys.  This tag is a [[StandardLocationFilter|Standard Location Filter]] describing the terrain from which the next goal is chosen.  The default is all terrain on the map. &lt;br /&gt;
* '''[filter_location_wander]''': The terrain on which the big animals will end their moves. This is a [[StandardLocationFilter|Standard Location Filter]].  The default is all terrain on the map.&lt;br /&gt;
* '''[avoid_unit]''': A [[StandardUnitFilter|Standard Unit Filter]] describing all enemy units that the big animals will avoid.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
Here's an example of big_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=big_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Ghast&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [avoid_unit]&lt;br /&gt;
                 type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
             [/avoid_unit]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=1-40,1-18&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Forest Animals (ai_type=forest_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Forest Animals Micro AI simulates a number of different animal behaviors that one might find in a forest, namely animals behaving like deer, rabbits and families of tuskers.  In the following, we will refer to those animals by these types, even though the actual unit types can be selected freely.  They behave as follows:&lt;br /&gt;
* Each deer wanders randomly on (selectable) terrain, except when enemies get in its (the deer's) maximum movement range, in which case it flees to the farthest point it can reach.&lt;br /&gt;
* Tuskers exhibit the same behavior as deer, except when an enemy is next to one of the tusklets.  This enemy is attacked unconditionally by the tuskers trying to protect their young.&lt;br /&gt;
* Tusklets blindly follow the closest adult tusker, except when there is no tusker left, in which case they behave the same as deer.&lt;br /&gt;
* Rabbits also wander randomly, but in addition disappear into their holes (if any are within reach) when enemies are close.  They reappear out of their holes at the beginning of the turn, if it is safe.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Forest Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Forest Animals Micro AI by using&lt;br /&gt;
 ai_type=forest_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Forest Animals AI consists of 4 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-3&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''rabbit_type, tusker_type, tusklet_type, deer_type''': Comma-separated lists of the unit types behaving as the respective animals.  If any of these parameters are not set, those types of animal behavior are skipped by the AI.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain on which the forest animals wander when they are not threatened by enemies.&lt;br /&gt;
* '''rabbit_number'''=6: (integer)  The number of rabbits for the AI to have on the map simultaneously.  The AI puts the missing number of rabbits onto the map at the beginning of the AI's turn (when the rabbits come out of their holes).&lt;br /&gt;
* '''rabbit_enemy_distance'''=3: (number)  Rabbits won't spawn in holes that aren't more than this distance away from all enemies.&lt;br /&gt;
* '''rabbit_hole_img''': Rabbit holes are marked by items on the map.  If this parameter is set, it must be the file name of the item image.  If it is not set, all items on the map are treated as rabbit holes.&lt;br /&gt;
** {{DevFeature1.14|10}} Items used for rabbit holes on the map border used to produce an error. Placing them on the map border therefore needed to be avoided on the scenario setup side. They are now automatically excluded from consideration by the AI code.&lt;br /&gt;
&lt;br /&gt;
Here's an example of forest_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=forest_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             deer_type=Vampire Bat&lt;br /&gt;
             rabbit_type=Giant Rat&lt;br /&gt;
             tusker_type=Ogre&lt;br /&gt;
             tusklet_type=Young Ogre&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=*^F*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Herd and Herders (ai_type=herding) ===&lt;br /&gt;
&lt;br /&gt;
The Herding Micro AI sets up an area on the map where a number of herder units (such as sheep dog) control and protect a herd (such as sheep).  For convenience, we simply refer to them as sheep dogs and sheep in the following, even though arbitrary unit types can be selected for either.&lt;br /&gt;
&lt;br /&gt;
Sheep dogs try to keep their sheep safe.  This involves keeping them inside the herding area, positioning themselves in between the sheep and approaching enemies, and attacking the enemies if those get too close.  If no active herding or protecting move is needed, the dogs go to a random location on the perimeter of the herding area.&lt;br /&gt;
&lt;br /&gt;
Sheep wander aimlessly except when a sheep dog is next to them, in which case they run away from the dog.  The dogs exploit this by positioning themselves on the outside of the sheep, if possible.  Sheep also run away from approaching enemies.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Herding AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Herding Micro AI by using&lt;br /&gt;
 ai_type=herding&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herder units (e.g. dogs).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herd units (e.g. sheep).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain forming the perimeter of the herding area.  In other words, this is the path the herders (dogs) use to patrol around the outside of the herding area.  This must be a set of coordinates ''completely enclosing'' the herding area.  Here's an example of setting up a rectangular perimeter:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         x=10-20,10-20,10,  ,20&lt;br /&gt;
         y=11,   21,   11-21,11-21&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
Another possibility is by defining the path by certain terrain on the map (as it is done in the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]), in which case the filter can simply look like this:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         terrain=Rb&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
* '''herd_x,herd_y''': The coordinate of one hex (''any'' one hex) inside the herding area.  This is needed to define which part of the map is on the ''inside'' of the herding area perimeter defined by [herding_perimeter], and which is the ''outside''. {{DevFeature1.15|0}} '''herd_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''attention_distance'''=8: (integer)  If an enemy is within this distance of a herd unit, but not as close as ''attack_distance'', the herders try to position themselves in between the enemy and the herd unit.&lt;br /&gt;
* '''attack_distance'''=4: (integer)  If an enemy is within this distance of a herd animal, the herders attack this enemy.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Herding AI consists of 6 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-5&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Herding [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=herding&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Footpad&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Troll Whelp&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             herd_x,herd_y=32,28&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=Rb&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Hunter (ai_type=hunter) ===&lt;br /&gt;
&lt;br /&gt;
The Hunter Micro AI leads a unit (or units) through hunt-and-rest cycles.  The units wander in a prescribed area of the map, their hunting ground, until an enemy unit comes within range.  Note that the units do not actively chase down enemies, but wander randomly through the hunting ground until an enemy is encountered.&lt;br /&gt;
&lt;br /&gt;
If enemies are within range of a hunter unit, the hunter attacks the weakest of them.  If a kill is made, it then retreats to its rest location, where it stays for a certain number of turns and until fully healed (whatever happens later).&lt;br /&gt;
&lt;br /&gt;
A few more details:&lt;br /&gt;
* If the hunter's way home is entirely blocked on the return (so that there is no possible path), the normal RCA AI takes over its behavior.&lt;br /&gt;
* However, if the way is blocked by an enemy unit occupying the rest hex, that enemy will be attacked unconditionally.&lt;br /&gt;
* A kill only makes the hunter go home when it is the attacker, not as defender.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Hunter AI behavior, check out the &amp;quot;Dragon&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]. &lt;br /&gt;
&lt;br /&gt;
Enable the Hunter Micro AI by using&lt;br /&gt;
 ai_type=hunter&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''home_x,home_y''': The coordinates of the hex to which the hunter returns and rests at after a kill. {{DevFeature1.15|0}} '''home_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the unit's hunting ground.  If this tag is not provided, the hunter uses all of the map.&lt;br /&gt;
* '''rest_turns'''=3: (integer)  The number of turns the AI stays at home_x,home_y after a kill.  It will not leave its home until this number of turns have passed ''and'' it is fully healed.&lt;br /&gt;
* '''show_messages'''=no: (boolean) If set to yes, the hunter announces whenever its behavior changes to the next phase.  Note that this is meant as debug output for the scenario author.  The messages are not translated and are not included in replays (and are only visible to the host in an MP game).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a hunter [micro_ai] tag usage from the &amp;quot;Dragon&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hunter&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Rowck&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=5-30,1-15&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             home_x,home_y=3,15&lt;br /&gt;
             rest_turns=2&lt;br /&gt;
 &lt;br /&gt;
             show_messages=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Swarm (ai_type=swarm) ===&lt;br /&gt;
&lt;br /&gt;
The Swarm Micro AI uses some very simply algorithms to simulate animal swarm behavior.  Without adjacent enemies, they simply do a random move, trying to stay together and at a certain distance from enemies.  However, if an enemy unit is close to any bat, the swarm scatters.  This is particular fun to watch when one places an enemy unit in the middle of the swarm.  After being scattered, the swarm members slowly rejoin, but not in a very organized way.  Sub-swarms or individual bats might roam around for quite some time before they find their way back.  It is also possible that individual bats (or small groups) split off from the larger swarm at times.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Swarm AI behavior, check out the &amp;quot;Swarm&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Swarm Micro AI by using&lt;br /&gt;
 ai_type=swarm&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Swarm AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_distance'''=5: (int) The minimum distance kept between units of the swarm and enemies when the swarm moves as a whole.&lt;br /&gt;
* '''scatter_distance'''=3: (int) Enemies within &amp;quot;scatter_distance&amp;quot; hexes of any swarm unit cause the swarm to scatter, by each unit trying to maximize its individual distance from the enemy.&lt;br /&gt;
* '''vision_distance'''=12: (int) Only units within this distance follow the overall swarm motion (either away from an enemy or of the swarm as a whole).  The smaller this value is set, the less likely the swarm is to stay together or rejoin.  This parameter is meant to simulate how far an individual member of the swarm can see, meaning that the swarm is &amp;quot;out of sight&amp;quot; (and the unit will not be able to follow it) if it is more than this distance away.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Swarm [micro_ai] tag usage from the &amp;quot;Swarm&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=swarm&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Assassin Squad Micro AI (ai_type=assassin) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|6}}&lt;br /&gt;
&lt;br /&gt;
This Micro AI controls one or several assassin units which try to circle around enemy units with the goal of hitting a target unit without being distracted or impeded by other enemies.  The units will always attack the target if such attacks are possible, otherwise they will follow the path of least resistance toward it. Notes:&lt;br /&gt;
&lt;br /&gt;
* The main priority for the AI is to find paths that avoid enemy units. However, if no such path can be found, the assassins will still advance toward the target, taking the path exposing the units to the least potential damage from enemies.&lt;br /&gt;
&lt;br /&gt;
* The units will not necessarily follow the same path. The best path is chosen individually for each unit.&lt;br /&gt;
&lt;br /&gt;
* While it is not a requirement for the AI to work, using ambusher or skirmisher units, or units with the quick trait, makes this AI more lethal in many cases.&lt;br /&gt;
&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded, it does not work otherwise. That means that '''ca_score''' is not a valid key for the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=assassin&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Assassin Squad specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the assassin units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the target unit.&lt;br /&gt;
** It is important to realize that the AI currently only works correctly with a single target unit. It is thus the responsibility of the scenario designer to make sure that this filter matches an individual enemy unit. If several enemy units are to be targeted in the same scenario, several instances of the Assassin Micro AI can be used.&lt;br /&gt;
** The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[prefer]''': A [[StandardLocationFilter|Standard Location Filter]] selecting locations on the map for the assassins to prefer when making their way toward the target unit. This could be used, for example, to make units with the ambush ability move (mostly) through forest or to have the assassins circle around the, say, northern part of the map rather than the south if that is desirable for scenario design purposes.&lt;br /&gt;
** As with avoiding enemies, the units will still move across non-preferred terrain if that is either the only option or ''much'' shorter than preferred terrain&lt;br /&gt;
** Attacking, once the target unit is in range, is not influenced by the [prefer] SLF.&lt;br /&gt;
&lt;br /&gt;
Here is an example of an Assassin Squad [micro_ai] tag usage:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=assassin&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Assassin,Master at Arms,Duelist&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             [prefer] # Circle around northern part of map&lt;br /&gt;
                 x=1-9, 10-99&lt;br /&gt;
                 y=1-37, 1-17&lt;br /&gt;
             [/prefer]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Bottleneck Defense Micro AI (ai_type=bottleneck_defense) ==&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense Micro AI lets you define a location on the map where the AI can take a defensive stance behind a narrow passage (bottleneck).  Units on the front line are backed up by healers and/or units with the leadership ability.  Injured units are moved to the back and replaced by uninjured (or less injured) units.  The units on the front line only attack when it is safe (no retaliation) or when there is a high chance that they will make a kill or level up.  Using this Micro AI only makes sense if there is no way for the enemy sides to move around the bottleneck and attack the AI's units from behind.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Bottleneck&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Bottleneck Defense Micro AI by using&lt;br /&gt;
 ai_type=bottleneck_defense&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Bottleneck Defense specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense AI requires two sets of coordinates that define where it should be taking up its defensive stance, and from which side the enemy attacks at that location.&lt;br /&gt;
&lt;br /&gt;
* '''x,y''': Comma separated lists of the hexes on the front line, where strong units are placed.  All hexes on which the AI makes contact with (can be attacked by) the enemy need to be included here.  Units are placed on them in the order in which they are listed here. {{DevFeature1.15|0}} '''location_id''' can be used instead with named/special locations.&lt;br /&gt;
* '''enemy_x,enemy_y''': Comma separated list of the hexes from which the enemy can attack the AI front line units.  This is needed for the AI to know on which side of the front line support units (healers etc.) need to be placed.  In many cases, it is sufficient to provide only one of the enemy front line hexes, but there are geometries for which that does not work.  It is therefore safer to list all enemy front line hexes here. {{DevFeature1.15|0}} '''enemy_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
By default, the AI places healers and units with the leadership ability both on the front line itself (if they are the strongest units available) and on hexes adjacent to and behind the line.  If different placement is desired, these locations can be overridden with the following keys.&lt;br /&gt;
&lt;br /&gt;
* '''active_side_leader'''=no: (boolean)  If set to 'yes', the side leader participates in the bottleneck defense action after the side's gold has been spent.  If set to 'no' (default), the leader follows default AI behavior (sitting on the keep and recruiting, for the most part).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Bottleneck Defense AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.15|3}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the units.  Only AI units matching this filter participate in the bottleneck defense.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''healer_x,healer_y''': Comma separated list of hexes where healers are placed.  This can be used to keep healers from the front line (or to take up only certain positions on the front line), and/or to override the default healing positions behind the line.  Healers are placed on them in the order in which the hexes are listed, with the exception that hexes on the line always take priority over hexes behind the line. {{DevFeature1.15|0}} '''healer_loc''' can be used instead with named/special locations.&lt;br /&gt;
* '''leadership_x,leadership_y''': Same as ''healer_x,healer_y'', but for units with the leadership ability. {{DevFeature1.15|0}} '''leadership_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
As an example, in the &amp;quot;Bottleneck&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]], there are three front line hexes, two of which are on hill terrain, while the third is a road with a lower defense rating.  The healer and side leader are supposed to participate in combat because they are strong units, but only from the hill hexes to keep them safer.  This is accomplished by using the following keys settings (check out the scenario to see which hex is which):&lt;br /&gt;
 [micro_ai]&lt;br /&gt;
     side=1&lt;br /&gt;
     ai_type=bottleneck_defense&lt;br /&gt;
     action=add&lt;br /&gt;
 &lt;br /&gt;
     x=14,14,14&lt;br /&gt;
     y= 7, 9, 8&lt;br /&gt;
     enemy_x=13,13&lt;br /&gt;
     enemy_y= 8, 9&lt;br /&gt;
 &lt;br /&gt;
     healer_x=14,14,15,15&lt;br /&gt;
     healer_y= 7, 9, 8, 9&lt;br /&gt;
     leadership_x=14,14,15,15&lt;br /&gt;
     leadership_y= 7, 9, 9 ,8&lt;br /&gt;
     active_side_leader=yes&lt;br /&gt;
 [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Fast Micro AI (ai_type=fast_ai) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|0}}&lt;br /&gt;
&lt;br /&gt;
This AI is meant as a fall-back AI for scenarios where there are so many units that attacks and moves done by the default AI are slow with long delays before each move.  It replaces the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions (CAs).  It should be obvious that in order to achieve the faster speed, compromises concerning the quality of the moves have to be made.&lt;br /&gt;
&lt;br /&gt;
Enable the Fast Micro AI by using&lt;br /&gt;
 ai_type=fast_ai&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Combat candidate action:'''&lt;br /&gt;
* Attackers are considered individually one at a time.  The best attack found for a unit is executed without checking whether a better attack by another unit might be possible.&lt;br /&gt;
* The default order in which attackers are considered is by decreasing hitpoints, starting with the strongest unit (in terms of HP).  This can be modified with the optional keys below.&lt;br /&gt;
* By default, if weaker units block attack hexes for a stronger unit, they do not move out of the way (this is the same as for the default AI).  If a hex becomes available after an attack (e.g. due to a weaker unit dying in the attack), all attacks that were possible ''before'' this happened are finished first.  Only after that are attacks by the stronger unit reconsidered.&lt;br /&gt;
* If include_occupied_attack_hexes=yes is set, weaker units move out of the way for stronger units if possible.  This can, however slow down the AI depending on the situation on the map.  Use with caution.&lt;br /&gt;
* {{DevFeature1.13|2}} Previous to this version, attacks by the leader were bunched in with the attacks by the other units.  Now leader attacks are treated separately and contain an extra layer of analysis to determine whether the attack position is &amp;quot;safe&amp;quot; (in an approximate way).  Currently, this is done with two simple considerations:&lt;br /&gt;
** Count all enemy units that can reach the leader's attack position.  If there are more than given by optional parameter 'leader_attack_max_units' (default value 3), do not attack.&lt;br /&gt;
** Add up the hitpoints of all enemies that can reach the leader's attack position.  If this value, multiplied by optional parameter 'leader_weight'(default value 2), is larger than the leaders hitpoints, do not attack.&lt;br /&gt;
** Two optional parameters, 'threatened_leader_fights' and 'leader_additional_threat', can also be used to let the leader attack when the threat at the attack hex is comparable to that at the leader's current position.&lt;br /&gt;
** In order to keep this AI as fast as possible, leader attacks are done by a separate candidate action that is only evaluated when all other CAs are done.&lt;br /&gt;
&lt;br /&gt;
'''Move-to-targets, villages and retreat candidate actions:'''&lt;br /&gt;
* The move-to-targets, villages and retreat candidate actions of the default AI are removed.  Move-to-targets and villages are taken over by the Fast Micro AI, whereas retreat is simply not done.&lt;br /&gt;
* This CA moves units toward goals after attacks are done.  Goals are unowned (if the AI side has a leader only) and enemy-owned villages and enemy leaders.&lt;br /&gt;
* For each goal, units are pre-sorted in order of the fewest moves needed to cover the ''distance'' toward the goal (that is, as if move cost on each hex toward the goal were 1).  Units are then considered sequentially and if a unit can get there &amp;quot;efficiently&amp;quot;, its move is executed without considering other units.&lt;br /&gt;
** A move is considered efficient if the move cost to get to the hex is not more than 'move_cost_factor' larger than the distance to the hex.  'move_cost_factor' is an optional key that defaults to 2.  It can be set to values of 1.1 or larger (see below).&lt;br /&gt;
** If the move for the current unit is not found to be efficient, the AI considers the next unit instead.  If no unit can do an efficient move toward the goal, the best (fastest) of the inefficient moves is selected instead.&lt;br /&gt;
&lt;br /&gt;
* Village goals:&lt;br /&gt;
** Village goals are considered before enemy leader goals so that the AI preferentially chooses scouts (or otherwise fast units) for them.&lt;br /&gt;
** The number of units sent toward villages depends on the value of the default AI aspect village_value.  It is set to village_value/4 times the number of units the AI has left to move.  Thus, by default (village_value=1.0) the AI sends a quarter of its units toward villages, and the rest toward enemy leaders.  This means that the meaningful range of values for village_value is 0..4.&lt;br /&gt;
** Only one unit is moved toward each village.  This consideration also overrides the number of units to move toward villages defined by village_value, topping it at the number of unowned/enemy-owned villages on the map.&lt;br /&gt;
** Setting village_value=0 disables moving units toward villages altogether.  That is, not only are no units chosen, but the village targeting phase of the AI is entirely disabled with no evaluation time used for it.  Note that, unlike with the default AI, this also means that no villages within range of an AI unit are grabbed.&lt;br /&gt;
** The order in which villages are chosen as goals is some combination of them being close to the AI side's leader and being spread out over the map.&lt;br /&gt;
&lt;br /&gt;
* Enemy leader goals:&lt;br /&gt;
** The order of enemy leaders goals is set according to their distance from the AI leader with the closest enemy leader being targeted first (if the AI has a leader, otherwise their order is random).  One unit is sent toward each enemy leader in this order, after which the AI starts with the first enemy leader again.  This is repeated until all units have moved.&lt;br /&gt;
&lt;br /&gt;
* Guardians (units with ai_special=guardian set) are ignored by the fast move-to-targets CA.&lt;br /&gt;
&lt;br /&gt;
'''Tips for speeding up/modifying the Fast Micro AI in specific scenarios:'''&lt;br /&gt;
&lt;br /&gt;
A number of optional keys and default AI aspects can be used to modify the behavior of the Fast Micro AI.  The MAI is set up so that it is a reasonable compromise between speed and quality of the moves in most situations.  However, this might not be ideal for all scenarios here are some things that can be done to speed up the AI (or to get better behavior in some situations by doing the opposite):&lt;br /&gt;
* Use large values for the default AI aspect aggression.  The largest delays before attacks happen (in some circumstances) when the Fast MAI has to consider lots of units with attacks left but decides not to attack with them.  See below for a description of how aggression is used by the fast AI.&lt;br /&gt;
* Use large values of move_cost_factor (see above for description of what this does).  If you want to emphasize units that can move across terrain easily (e.g. bats) to be chosen for village grabbing, you might want to use low values of move_cost_factor, but this might slow down the AI.  The default value should work reasonably well in most standard situations, but modifying the value might work better on specific maps.&lt;br /&gt;
* Don't use dungeon_mode=yes unless absolutely necessary, even in most dungeon crawls.  Test the AI without this setting first and only use it if it is the only way to solve the problem.  Often, a minor tweak to the map will have the same effect, without the speed penalty.&lt;br /&gt;
* Use include_occupied_attack_hexes=yes with caution.  It might (or might not) slow down the attack CA significantly, depending on the scenario.&lt;br /&gt;
* Set village_value=0, which makes the AI not consider villages as goals.  This has not been found to make a large difference on the maps tested so far, but it might have an effect on maps with a very large number of villages.&lt;br /&gt;
* Use attack_hidden_enemies=yes on maps where there are no hidden enemies. Excluding hidden enemies from the attack calculation requires an additional filter to be evaluated that takes extra time. In most cases this will not result in a large computation time saving.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_hidden_enemies'''=no: (boolean)  By default, hidden enemies are excluded from the attack calculation.  Setting this to 'yes' makes the AI also consider those enemies as attack targets.&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.13|2}} A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  As with the default AI, units will not step on hexes defined by the [avoid] tag.  In addition, no goals will be chosen that lie on those hexes.  The one minor exception is that a unit stepping out of the way for another unit might temporarily enter the avoided zone.&lt;br /&gt;
* '''dungeon_mode'''=no: (boolean)  As much as possible, the Fast Micro AI works with distances between hexes rather than path finding.  This works pretty well on open maps and even most dungeon crawls (and similar scenarios), but it might result in some units getting temporarily (and in rare cases permanently) stuck on maps with convoluted labyrinths.  If that is the case, setting dungeon_mode=yes triggers more path finding that avoids such situations (but as a result is somewhat slower).&lt;br /&gt;
* '''include_occupied_attack_hexes'''=no: (boolean) If set to 'yes', the AI moves weaker units out of the way for a stronger attacker.  This might lead to better attacks but is slower, so don't use if speed is essential.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the attackers.  Only AI units matching this filter will attack, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes the side= key set to the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the enemies.  Only enemy units matching this filter will be attacked, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''leader_weight'''=2: (number &amp;gt;= 0) {{DevFeature1.13|2}} A factor specifying how much more valuable the AI considers side leaders than non-leader units in the attack evaluation. This is also used to determine whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''leader_additional_threat'''=1: (number &amp;gt; 0) {{DevFeature1.13|2}} If threatened_leader_fights=yes, the threat at the attack position may be larger by this factor for the leader to leave the keep for an attack.&lt;br /&gt;
* '''leader_attack_max_units'''=3: (number &amp;gt;= 0) {{DevFeature1.13|2}} A number specifying how many enemy units within reach are acceptable when determining whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''move_cost_factor'''=2: (number &amp;gt;= 1.1)  The factor by which the move cost to a goal hex may be larger than the distance to the goal hex for a move to be considered &amp;quot;efficient&amp;quot;.  See description above.  1.1 is used if this is set to values smaller than 1.1.  Smaller values might lead to better behavior, while larger values might speed up the AI.&lt;br /&gt;
* '''weak_units_first'''=no: (boolean) If set to 'yes', inverts the order in which units are considered by the combat CA.  Attacks are then done in order of increasing hitpoints of the attackers.&lt;br /&gt;
* '''skip_combat_ca'''=no: (boolean) If set to 'yes', the fast combat CA is not used.  The default AI's high_xp_attack and combat CAs remains in place in this case.&lt;br /&gt;
* '''skip_move_ca'''=no: (boolean) If set to 'yes', the fast move-to-targets CA is not used.  The default AI's retreat, villages and move-to-targets CAs remains in place in this case.&lt;br /&gt;
* '''threatened_leader_fights'''=no: (boolean) {{DevFeature1.13|2}} By default, the AI leader will not attack if the threat at the attack position is too high.  If this key is set to 'yes', the leader will attack if the threat there is not larger than the threat at his current position multiplied by the value of 'leader_additional_threat'. This is meant as an option for the endgame: when the AI leader is under attack, he might as well go down fighting.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''aggression''': The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to set how aggressive the AI should attack.  It works in roughly the same way as the default AI aspect:&lt;br /&gt;
** The value of (1 - aggression) determines how much the AI values its own units compared to the enemy's units.  Thus, aggression=0 means that attacks are only done if the AI expects to deal (roughly) the same amount of damage as it will receive.  aggression=1 means that expected damage to the AI's units is not considered, only the expected damage to enemy units matters.&lt;br /&gt;
** Damage is calculated as a combination of hitpoints and chance to die, in &amp;quot;gold equivalent units&amp;quot;.&lt;br /&gt;
** Unlike the default AI, aggression is set to 1 for values larger than 1.  Allowed values are thus anything from -infinity to +infinity, although only -infinity to +1.0 make sense.&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.13|2}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
*'''attacks aspect''':  {{DevFeature1.13|2}} The standard RCA AI [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] used to filter own and enemy units for attacks is now also taken into account as long as neither [filter] nor [filter_second] (see above under 'optional keys') are defined in the [micro_ai] tag.&lt;br /&gt;
*'''village_value=1''': (number ranging from 0 to 4) The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to define how much villages should be targeted by the move-to-targets CA.  In the Fast MAI, this defines which fraction of the AI units are moved toward village goals (see above for details).  If set to 0, villages are ignored as goals.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded to the same values as those of the respective [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|default candidate actions]].  ca_score is therefore not a valid optional key.&lt;br /&gt;
* The fast Micro AI modifies (removes) the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions.&lt;br /&gt;
&lt;br /&gt;
Enabling the Fast Micro AI in its default configuration is as simple as this usage example from the &amp;quot;Fast AI&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=fast_ai&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Generalized Goto Micro AI (ai_type=goto) ==&lt;br /&gt;
&lt;br /&gt;
The Goto Micro AI provides a highly configurable method of sending a unit (or units) to a location or set of locations.  The units to be moved are defined using a [[StandardUnitFilter|Standard Unit Filter]] (SUF) [alternatively, all side units are used if the SUF is not provided), while the goto locations are given in a [[StandardLocationFilter|Standard Location Filter]] (SLF).&lt;br /&gt;
&lt;br /&gt;
By default, each unit matching the SUF will move toward the closest goal (in terms of turns needed to get there) matching the SLF via the fastest route given the current situation on the map (locations of enemy units etc.), but this behavior can be influenced by the optional keys listed below.  Note that this means that a unit's goal might change from one turn to the next depending on the situation on the map.&lt;br /&gt;
&lt;br /&gt;
The behavior of the unit(s) once they reach their goal location(s) is configurable as well.  By default, they will stay there until the Goto Micro AI is deleted or changed.&lt;br /&gt;
&lt;br /&gt;
For demonstrations of several Goto Micro AI usages, check out the &amp;quot;Goto&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Goto Micro AI by using&lt;br /&gt;
 ai_type=goto&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Goto specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the possible goal locations for the move.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists). Note that the area defined by [avoid] is not entered at all, even if the end point of the move would lie outside the area again.&lt;br /&gt;
* '''avoid_enemies''': (number&amp;gt;0) By default, units controlled by the Goto Micro AI will not take enemy units into account for route finding, other than the enemies' ZoCs.  By contrast, if this key is set, they will try to avoid traversing hexes on which they can be attacked by enemies.  The larger the value of ''avoid_enemies'', the more the units will try to stay out of the enemies' way.  Notes:&lt;br /&gt;
** Units are willing to take a path that is 'avoid_enemies' movement points longer for each enemy unit that can be avoided by doing so for each hex along the path.  As an example, if avoid_enemies=1 and the shortest path crosses 2 hexes that can be reached by 3 enemies each, the unit is willing to take a path that is up to 6 hexes longer instead.  If avoid_enemies=10 in the same situation, the unit will take a path up to 60 hexes longer.&lt;br /&gt;
** Hexes adjacent to enemy units are always strongly avoided, independent of the value of avoid_enemies.&lt;br /&gt;
** avoid_enemies is ignored if 'use_straight_line=true' is set.&lt;br /&gt;
** Due to an issue with the path finding code, '''tunnels and teleports are ignored in Wesnoth 1.14 and before''' if 'avoid_enemies' is used. This has been fixed in Wesnoth 1.15.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units to be moved.  Defaults to all units of the side if not defined.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''ignore_enemy_at_goal'''=no and '''ignore_units'''=no: (boolean)  In the default configuration, the Goto MAI does not move units if the path to all goal hexes is blocked by enemies.  It is therefore possible to disable the Goto MAI by putting enemy units on all the goal hexes.  That can be changed if either of these two keys is set to 'yes'.&lt;br /&gt;
** 'ignore_enemy_at_goal=yes' makes the AI choose its goals while disregarding any enemy units at the goal hexes.  Even so, a stiff rating penalty is applied to goal hexes occupied by enemies, thus that unoccupied goal hexes are chosen unless all possible goals are occupied by enemies.  Note that this setting ''only'' ignores enemy units at the goal hexes themselves.  It is still possible to disable the AI by putting enemies all around the goals.&lt;br /&gt;
** 'ignore_units=yes' let's the AI ignore ''all'' enemy units when deciding which goal hex(es) to move toward.&lt;br /&gt;
** In both cases, enemies are only ignored for deciding which unit to move toward which goal hex.  For the actual move itself, all enemies and their ZoCs are considered as usual.&lt;br /&gt;
** Both options only take effect if neither avoid_enemies is used, nor use_straight_line is set to 'yes', as neither of these consider enemies as blockers. Or in other words, if avoid_enemies is used, or use_straight_line=yes, it is unnecessary to set ignore_enemy_at_goal or ignore_units to yes.&lt;br /&gt;
* '''release_unit_at_goal'''=no: (boolean) By default, a unit that reaches a goal location will stay there until the Goto Micro AI is deleted or changed (or until the conditions specified in the [goto_goals] SLF change).  If this parameter is set, a unit that has reached its goal will not be controlled by this Micro AI any more starting from the next turn.&lt;br /&gt;
* '''release_all_units_at_goal'''=no: (boolean) Same as ''release_unit_at_goal'', but this will deactivate the Goto Micro AI for all units once the first unit reaches a goal.&lt;br /&gt;
* '''unique_goals'''=no: (boolean)  If set to 'yes', each unit will be sent to a different goal location.  Note that the best goals are recalculated each turn, so a unit might choose a different goal this turn from what it was heading for during the previous turn if the situation on the map has changed.  Also note that if there are more units than goal locations, the left over units will not be handled by the Goto MAI but follow default behavior instead.&lt;br /&gt;
* '''use_straight_line'''=no: (boolean)  By default, units choose the route by which they will get to their goal locations in the smallest number of turns.  If this parameter is set to 'yes', a straight line route (or something pretty close to it at least) will be chosen instead, irrespective of whether this leads into a dead end, whether it is blocked by enemies, etc.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* Depending on the parameters used, the Goto Micro AI might require a lot of path finding to be done.  Thus, while it is possible to set up this AI to move many units to many different locations, a usage like that might require quite a long calculation time.&lt;br /&gt;
&lt;br /&gt;
===Goto Micro AI example usages:===&lt;br /&gt;
&lt;br /&gt;
Here are several examples of goto [micro_ai] tag usages from the &amp;quot;Goto&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
* Send a unit toward a castle (similar to the RCA AI's Goto Candidate Action, but avoiding dead ends and areas ZoCed by enemies)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
             ca_id=messenger1&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=messenger1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=6-7,2-4&lt;br /&gt;
                 terrain=C*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             ca_score=210001&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send all units of the side toward the southern border of the map&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=8&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                  y=33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             avoid_enemies=1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send units to the four corners of the map (with each unit going to a different corner).&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x=1,1,44,44&lt;br /&gt;
                 y=1,33,1,33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             unique_goals=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Set up a guardian unit that doesn't move while no enemy unit is within 8 hexes (ironically, the Goto Micro AI can be used to keep units in place, although using one of the Guardian Micro AIs might often be a better idea)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=guard1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 [filter]&lt;br /&gt;
                     id=guard1&lt;br /&gt;
                 [/filter]&lt;br /&gt;
                 [not]&lt;br /&gt;
                     [filter]&lt;br /&gt;
                         side=1&lt;br /&gt;
                     [/filter]&lt;br /&gt;
                     radius=8&lt;br /&gt;
                 [/not]&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Guardian Micro AIs ==&lt;br /&gt;
=== General Comments on the Guardian Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Guardian Micro AIs can be used to set up different kind of guardian behaviors for a unit or a set of units.  Four different types of guardians are available and are described below.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different guardian behaviors, check out the &amp;quot;Guardians&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
=== Return Guardian  (ai_type=return_guardian)===&lt;br /&gt;
&lt;br /&gt;
A 'return guardian' is a variation of the standard Wesnoth guardian. It has an assigned guard position (GP) to which it returns after attacks on approaching enemies:&lt;br /&gt;
* If at GP with no enemy in reach, do nothing.&lt;br /&gt;
* If at GP with an enemy in reach, attack. Note that the attack is done by the default AI, which might result in the guardian not attacking if the enemy is deemed too strong (this can be influenced by setting the default ''aggression'' value to a different value).&lt;br /&gt;
* If not at GP, return there, no matter whether an enemy is in reach or not.&lt;br /&gt;
* If enemies are blocking the way back to GP, do your best to move around them.&lt;br /&gt;
* If the guardian ends up next to an enemy on the way back, attack that enemy after the move.&lt;br /&gt;
&lt;br /&gt;
Enable the Return Guardian Micro AI by using&lt;br /&gt;
 ai_type=return_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''return_x,return_y''': The coordinate of the hex the unit returns to after each attack.  {{DevFeature1.15|0}} '''return_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=100010: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  This AI consist of 2 CAs, one with score &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt; the other with &amp;lt;tt&amp;gt;ca_score-20&amp;lt;/tt&amp;gt;.  The default value is set so that the first CA gets executed right before the default combat CA, and the other right after.  It usually will not make sense to change this value, but it is possible to do so for custom applications.  {{DevFeature1.13|6}}: This is now changed to 100100 and 99900 in order to avoid conflicts with the new high_XP_attacks candidate action.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a return guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=return_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=return1&lt;br /&gt;
             return_x,return_y=20,2&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
===Stationed Guardian (ai_type=stationed_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A 'stationed guardian' is another variation of the standard Wesnoth guardian with a somewhat more complex behavior than that of the 'return guardian'. Two positions are defined for it, a 'station' and a 'guarded location', as well as a distance. The behavior is as follows:&lt;br /&gt;
* If no enemy is within 'distance' of the guard's current position, do nothing.  This is independent of the guardian being at its station or not.&lt;br /&gt;
* Otherwise: If an enemy is within 'distance' of the guard, but not also within the same distance of the guarded location and the station (all of this simultaneously), move the guard in the direction of the station (or keep the guard in place if the station is unreachable).&lt;br /&gt;
* Otherwise:&lt;br /&gt;
** Pick the enemy unit that is closest to the guarded location.&lt;br /&gt;
** If we can reach it, pick the adjacent hex with the highest defense rating and attack from there.&lt;br /&gt;
** If not in reach, move toward this unit.&lt;br /&gt;
&lt;br /&gt;
Enable the Stationed Guardian Micro AI by using&lt;br /&gt;
 ai_type=stationed_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance parameter as explained above&lt;br /&gt;
* '''station_x,station_y''': The x and y position of the hex that serves as the guardians station. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''guard_x, guard_y''': The x and y position of the guarded location.  {{DevFeature1.13|0}}  These are now optional keys, defaulting to station_x,station_y if not provided. {{DevFeature1.15|0}} '''guard_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a stationed guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=stationed_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=stationed1&lt;br /&gt;
             distance=4&lt;br /&gt;
             station_x,station_y=2,14&lt;br /&gt;
             guard_x,guard_y=3,13&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Coward (ai_type=coward) ===&lt;br /&gt;
&lt;br /&gt;
Cowards are units that, like guardians, sit around doing nothing until an enemy comes into range.  Unlike guardians, however, they run away once enemies approach. Applications might be wild animals, unarmed civilians getting in the way of a battle, etc.  The coward macro can be called with two optional locations, 'seek' and 'avoid':&lt;br /&gt;
* If neither is given, the coward retreats to the position farthest away from the approaching enemies.&lt;br /&gt;
* If 'seek' is given, it preferentially goes toward that location (but getting away from enemies takes priority).&lt;br /&gt;
* If 'avoid' is given, it in addition tries to avoid that location (with both maximizing distance from enemies and going toward 'seek' taking priority).&lt;br /&gt;
* Both 'seek' and 'avoid' may consist of only one coordinate ('x' or 'y'), in which case not a single hex, but a line of hexes is sought or avoided.&lt;br /&gt;
&lt;br /&gt;
Enable the Coward Micro AI by using&lt;br /&gt;
 ai_type=coward&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance within which an enemy must be to &amp;quot;scare&amp;quot; the unit&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_if_trapped'''='no':  {{DevFeature1.13|0}}  With the default settings, a coward never attacks.  If this key is set to 'yes', the coward attacks the weakest (lowest hitpoints) enemy next to which it ends up after moving (such as a cornered animal would do, blindly lashing out without regards for its own safety).&lt;br /&gt;
* '''avoid_x, avoid_y''': The x and y coordinate of the hex to avoid. {{DevFeature1.15|0}} '''avoid_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units from which the cowards flee.  If not set, this defaults to all enemy units.  Note, however, that no &amp;quot;enemy of AI side&amp;quot; condition is added if [filter_second] is specified, meaning that it can include allied and/or enemy units as desired (and even units on the same side as the coward).&lt;br /&gt;
* '''seek_x, seek_y''': The x and y coordinate of the hex to seek. {{DevFeature1.15|0}} '''seek_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a coward [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=coward&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=coward3&lt;br /&gt;
             distance=5&lt;br /&gt;
             seek_x,seek_y=24,5&lt;br /&gt;
             avoid_x,avoid_y=24,15&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Zone guardian (ai_type=zone_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A zone guardian is a unit that, as the name says, guards a zone. It moves randomly inside this zone until an enemy enters it (or a separately defined enemy zone, see below). Applications might be the defense of a castle, a nesting area or for protecting an unit.  The zone macro can be called with an optional SLF, [filter_location_enemy]:&lt;br /&gt;
* If not specified, the zone guard attacks any enemy coming inside its guard zone.&lt;br /&gt;
* If [filter_location_enemy] is given, it attacks any enemy entering this zone and once there are no more enemies, it goes back to patrol in its basic zone.&lt;br /&gt;
&lt;br /&gt;
Enable the Zone Guardian Micro AI by using&lt;br /&gt;
 ai_type=zone_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone to guard.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_enemy]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone where enemies have to be attacked.  If not given, it defaults to [filter_location].&lt;br /&gt;
* '''station_x,station_y''': By default, the zone guardian moves randomly within the zone if there is no enemy to attack.  However, if station_x/y is given, it moves to this position instead during turns when it does not have an enemy to attack.  This position does not need to be inside the zone defined above.  Note: obviously it does not make sense to define [filter_location], [filter_location_enemy] and station_x/y all at the same time.  Only [filter_location] and either one of the latter two (or none of them) should be provided for any zone guardian. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a zone guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=zone_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=zone3&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=22-31,4-11  # This is intentionally chosen to extend past the lake&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             station_x,station_y=32,8&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Hang Out Micro AI (ai_type=hang_out) ==&lt;br /&gt;
&lt;br /&gt;
The Hang Out Micro AI instructs units of the AI side to &amp;quot;hang out&amp;quot; around a certain location, or locations, until a condition for mobilizing the side is met.  If none of the optional keys are given, the AI controls all units of the side to hang around the leader while moving off castle tiles to make room for recruiting.&lt;br /&gt;
&lt;br /&gt;
The AI works by moving each unit to its best hex first using the minimum amount of movement points required for this move.  Once all AI units are in position, movement points are set to zero for all of them simultaneously such that they cannot be used by other AI candidate actions.  This is done because the mobilizing condition might be met as result of one of the moves of the Hang Out AI, after which we want units to be able to continue with other AI actions.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Hang Out and Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Hang Out Micro AI by using&lt;br /&gt;
 ai_type=hang_out&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Hang Out specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  In its default configuration, the Hang Out MAI controls all units of a side, keeping them as close to the side leader(s) as possible while moving off castle tiles to make room for recruiting.  For this purpose, the candidate action score defaults to 170,000, which is just below the score of the default recruiting CA.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''': A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  If neither of these two exists, this parameter is set to default to all castle terrain (&amp;lt;tt&amp;gt;terrain=C*,C*^*,*^C*&amp;lt;/tt&amp;gt;) so that units move off the castle to make room for recruiting.  Note that this does not include keep terrain, otherwise the leader would be moved off the keep as well.&lt;br /&gt;
* '''ca_score'''=170000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 170,000, which means that it is executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Hang Out action (&amp;lt;tt&amp;gt;side=&amp;lt;/tt&amp;gt; is always set and does not need to be provided).  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the locations ''near'' which the AI units should hang out.  This does not mean that the units only move onto terrain specified here, but that they minimize the distance to such terrain.  It defaults to the position of the side leader(s).&lt;br /&gt;
* '''[mobilize_condition]''':  Contains ([[ConditionalActionsWML#Condition_Tags|WML Condition Tags]])  Once the condition set up here has been met, the Hang Out MAI is disabled and the side goes over to other AI activity.  The Hang Out MAI remains disabled after this, even if the condition is not met any more later in the scenario.&lt;br /&gt;
* '''mobilize_on_gold_less_than''': (number) Since side gold cannot be checked in a WML condition tag without storing it in a variable first, this key is provided separately.  It disables the Hang Out Micro AI once the side gold goes below the value specified here for the first time.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
* The Hang Out AI still takes effect if no locations to hang out at are found or if all map locations are to be avoided.  In that case, it simply keeps all units at their current locations.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Hang Out Micro AI configuration from the &amp;quot;Hang Out and Messenger&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hang_out&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [avoid]&lt;br /&gt;
                 terrain=C*,H*,M*,A*,S*,*^F*&lt;br /&gt;
             [/avoid]&lt;br /&gt;
             [mobilize_condition]&lt;br /&gt;
                 [have_unit]&lt;br /&gt;
                     side=1&lt;br /&gt;
                     count=7-99&lt;br /&gt;
                 [/have_unit]&lt;br /&gt;
             [/mobilize_condition]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Healer Support Micro AI (ai_type=healer_support) ==&lt;br /&gt;
&lt;br /&gt;
The Healer Support Micro AI configures the healers of a side to stay behind the battle lines and heal injured and/or threatened units rather than participate in combat under all circumstances.  You can set different levels of aggressiveness for the healers.  &lt;br /&gt;
&lt;br /&gt;
'''Notes'''&lt;br /&gt;
* By default, ''all'' healers of the AI side are controlled by this AI.  This includes in particular also the side leader, if the leader is a healer.  If you do not want the leader to be included (and possibly be moved away from the keep etc.) use the [filter] tag with canrecruit=no (see below).&lt;br /&gt;
* It is not always better to use this Micro AI than the default AI, as it takes healers (which can be strong units) away from the pool of units to choose for attacks.&lt;br /&gt;
* {{DevFeature1.15|3}} This Micro AI now takes unit guardian status and the passive_leader aspect into account.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Healers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Healer Support Micro AI by using&lt;br /&gt;
 ai_type=healer_support&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Healer Support specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the healer units.  If set, only units on the AI side passing this filter ''and'' having the healing ability are considered by this Micro AI.&lt;br /&gt;
*'''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting units to receive healing. If set, only units on the AI side passing this filter ''and'' the other healee selection criteria will be selected for healing by this Micro AI.&lt;br /&gt;
* '''aggression'''=1.0: (float) Sets the aggressiveness of the AI.  This parameter is set up as a float to accommodate future functionality, but it currently acts as a boolean: if set to zero, the AI will never let its healers participate in combat, for all other values it allows them to attack after all other units have attacked and if the healers cannot find any more units to support.  The former behavior might be appropriate in scenarios with only one or few valuable healers, while the latter might work better in scenarios with many healers.&lt;br /&gt;
* '''injured_units_only'''=no: (boolean) If set to 'yes', the AI will only move healers next to units that are already injured and skip units that currently have full hitpoints, but might get injured during the next enemy turn.&lt;br /&gt;
* '''max_threats'''=9999: (integer) The maximum allowable number of enemies that can attack a hex in order for it to be considered for a healer.  As an example, setting 'max_threats=0' means that the AI only moves healers to locations that are entirely safe from the enemy (assuming that none of the units currently on the map dies).  Note that the value of this key is checked against the number of enemies that can make it to the hex, not the number of adjacent hexes from which the healer could be attacked.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI. Note: as attacks by the healers are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The Healer Support Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded.  It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Healer Support Micro AI configuration from the &amp;quot;Healers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             aggression=0&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Lurkers Micro AI (ai_type=lurkers) ==&lt;br /&gt;
&lt;br /&gt;
The Lurker Micro AI controls some or all units of a side in &amp;quot;lurker mode&amp;quot;.  A lurker is a unit that is capable of moving across most terrains, but that only stops on and attacks from specific terrain.  It might also have the ability to hide on this terrain (which is the reason why it is called a lurker).&lt;br /&gt;
&lt;br /&gt;
Lurkers move individually without strategy and always attack the weakest enemy within their reach.  If no enemy is in reach, the lurker does a random move instead - or it just sits and waits (lurks).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of different types of lurker behavior, check out the &amp;quot;Lurkers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Lurkers Micro AI by using&lt;br /&gt;
 ai_type=lurkers&lt;br /&gt;
in the [micro_ai] tag&lt;br /&gt;
&lt;br /&gt;
===Lurkers specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
* '''[filter]''': Specifies which units of the side are controlled by the Lurker Micro AI.  This is a [[StandardUnitFilter|Standard Unit Filter]].  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': Specifies the terrain from which the lurkers attack. This is a [[StandardLocationFilter|Standard Location Filter]].&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_wander]''': Specifies the terrain on which lurkers end their moves if there is no enemy in reach. This is a [[StandardLocationFilter|Standard Location Filter]].  If [filter_location_wander] is not given, it defaults to [filter_location].&lt;br /&gt;
* '''stationary'''=no: (boolean)  If set to yes, a lurker never moves if there is no enemy within attack range.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Lurkers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=4&lt;br /&gt;
             ai_type=lurkers&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Naga Fighter&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=W*,S*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             [filter_location_wander]&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location_wander]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Messenger Escort Micro AI (ai_type=messenger_escort) ==&lt;br /&gt;
&lt;br /&gt;
The Messenger Escort Micro AI lets you define a set of waypoints through which it tries to move one or several of its units, the messenger(s).  The other units escort the messenger, trying to protect it and attacking enemies that are in its way.  Note that a messenger might not strictly hit intermediate waypoints, but that getting close is good enough, especially if a waypoint is occupied by an enemy unit.  It does, however, always try to get to the last waypoint.&lt;br /&gt;
&lt;br /&gt;
'''Notes on the use of several messengers:''' {{DevFeature1.13|0}}  &lt;br /&gt;
* This Micro AI can control several messengers at the same time, which can but do not have to move in a group.  They can be put on the map altogether in one location, or be spread out on the map or across different turns.&lt;br /&gt;
* The farther a messenger has moved through the chain of waypoints, the higher a priority it is for the AI, meaning that farther advanced (on the map) messengers are moved first, and that the escort units try to clear the way and stay with those messengers more so than with those further back on the map.  This order can be inverted using the invert_order= key.&lt;br /&gt;
* If several messengers are on the map, escort units might move from one to the other, the AI making a judgement call where they are needed most.  Closeness to a messenger is generally the most important criterion, but not the only one.&lt;br /&gt;
* If a messenger dies, the escort units previously staying with it now join one of the other messengers if there are any left (otherwise they follow default AI behavior).&lt;br /&gt;
* If you want certain escort units to stay always with the same messenger, use several messenger [micro_ai] tags with different [filter]/[filter_second] combinations.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Messenger Escort Micro AI by using&lt;br /&gt;
 ai_type=messenger_escort&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Messenger Escort specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': {{DevFeature1.13|0}} (Previous to 1.13.0, only id= was allowed.) The id of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated list of waypoint coordinates through which the messenger will go in the given order to reach its goal, given by the last coordinate.  If you want the messenger to go directly to the goal, simply enter a single x,y coordinate here.  Note that the messenger does not have to hit each waypoint exactly (except for the final one), getting close to them is good enough. {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Messenger Escort AI consists of 3 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-2&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_death_chance'''=0.67 and '''messenger_death_chance'''=0.0: (both floats; ranging from 0 to 1)    If the messenger is adjacent to an enemy at the end of the move, it will only attack the enemy if the chance of death of the enemy is &amp;gt;= enemy_death_chance and if the messenger's chance of death is &amp;lt;= messenger_death_chance (or if it is an attack at a range for which the enemy does not have a weapon).&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|0}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the escort units.  If not set, this defaults to all enemy units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''invert_order'''=no: {{DevFeature1.13|0}} If set to 'yes', the order of priority with which messenger units are considered is inverted, starting with the messenger unit that is the least advanced on the map (see description above).  In most cases, selecting this option results in worse performance of the AI, so use it with caution.  (And obviously it has no effect at all if there is only one messenger controlled by the AI.)&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Messenger Escort&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=messenger_escort&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=messenger&lt;br /&gt;
             waypoint_x=31,24,27,28&lt;br /&gt;
             waypoint_y=20,14,7,1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Patrol Micro AI (ai_type=patrol) ==&lt;br /&gt;
&lt;br /&gt;
The defines a number of waypoints for a unit (or units) to follow.  Options can be set whether the route should be followed in a loop, as an out-and back, only once, whether units encountered along the way should be attacked, etc.&lt;br /&gt;
&lt;br /&gt;
No matter what options are chosen, getting to the next waypoint always takes priority over attacking for a patroller.  The AI thus prefers to move the patrol unit around enemies rather than straight for them.  Also, if a waypoint is occupied by a unit the AI is not instructed to attack, it will (eventually) abandon that waypoint once it gets close enough and move on to the next one.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different patrol behaviors, check out the &amp;quot;Patrols&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Patrol Micro AI it by using&lt;br /&gt;
 ai_type=patrol&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Patrol specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated lists of the waypoint coordinates through which the patrol travels (in order). {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack''': By default (if this key is not set), the patrol unit attacks any unit it ends up next to after a move.  Alternatively, this parameter can be set to a comma-separated list of unit ids. If that is done, the patrol only attacks these units when it ends up next to them and ignores any other unit.  This can in turn be used to have the patroller not attack at all, by setting ''attack='' to a non-existing id.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''one_time_only'''=no: (boolean)  If set to 'yes', the unit goes through the patrol route only once and then hangs out at the last waypoint.  It also always attacks any enemy unit on the last waypoint, independent of what ''attack='' is set to.&lt;br /&gt;
* '''out_and_back'''=no: (boolean)  By default, a patrol unit returns to the first waypoint after reaching the last, doing the patrol route as a loop.  If this parameter is set to 'yes', the unit instead reverses its course and perpetually does out and backs of the route.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Patrols&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=patrol&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Konrad&lt;br /&gt;
             waypoint_x=9,24,25&lt;br /&gt;
             waypoint_y=21,23,15&lt;br /&gt;
             one_time_only=yes&lt;br /&gt;
             attack=Gertburt&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Protect Unit Micro AI (ai_type=protect_unit) ==&lt;br /&gt;
&lt;br /&gt;
The Protect Unit Micro AI tries to move one or several units to a given location while keeping them safe from enemy attacks. It does so by keeping the protected units away from enemy units and close to friendly units. Other units on the AI side are controlled by the default Wesnoth AI.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Protect Unit&amp;quot; and &amp;quot;The Elves Besieged&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Protect Unit Micro AI by using&lt;br /&gt;
 ai_type=protect_unit&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Protect Unit specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[unit]''': A separate [unit] tag is required for each protected unit(s).  Each tag has required keys '''id''', '''goal_x''', '''goal_y''' for each unit, as shown in the example below. {{DevFeature1.15|0}} '''goal_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''disable_move_leader_to_keep'''=no: (boolean) If set to 'yes', side leaders protected by this AI do not try to return to their keep.  This is necessary for this Micro AI to work with units that are side leaders and that are supposed to move to an off-keep location.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
*The Protect Unit Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].  Depending on the parameters, it might also modify the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|move leader to keep candidate action]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded. It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;HttT: The Elves Besieged&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=protect_unit&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Delfador&lt;br /&gt;
                 goal_x,goal_y=1,2&lt;br /&gt;
             [/unit]&lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
                 goal_x,goal_y=1,1&lt;br /&gt;
             [/unit]&lt;br /&gt;
 &lt;br /&gt;
             disable_move_leader_to_keep=true&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Recruiting Micro AI==&lt;br /&gt;
=== General Comments on the Recruiting Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Recruiting Micro AI replaces the default recruitment AI by alternative recruiting patterns.  Currently there are two recruiting algorithms you can choose from, random recruiting and the rush recruiting used in the new (as of Version 1.11.1) Experimental AI (see below).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of a different recruiting pattern, check out the &amp;quot;Recruiting&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  You can also explore the unit choices by watching the Experimental AI in a multiplayer game.&lt;br /&gt;
&lt;br /&gt;
'''Note:''' Obviously, the Recruitment Micro AI modifies the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|recruitment candidate action]].&lt;br /&gt;
&lt;br /&gt;
=== Random Recruitment AI (ai_type=recruit_random) ===&lt;br /&gt;
&lt;br /&gt;
This AI randomly selects a unit type from the recruitment list of the side.  The probability of choosing each type and the behavior in low-gold situations can be influenced with optional parameters.&lt;br /&gt;
&lt;br /&gt;
Enable the Random Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_random&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* '''[probability]''': This tag sets the probability of selecting the given unit type.  It has two required sub-keys:&lt;br /&gt;
** '''type''': comma-separated list of unit types to which this probability is to be applied&lt;br /&gt;
** '''probability''': (non-negative float)  The probability with which these unit types is to be selected.  All unit types for which no [probability] tag is defined receive a default probability of 1.0, thus values should be chosen relative to that.  As an example, in the code below swordsmen and peasants are given a probability of 8, while no other units have assigned probabilities.  Thus, each of these two unit types is 8 times more likely to be selected than any other individual unit type.&lt;br /&gt;
* '''skip_low_gold_recruiting'''=no: (boolean)  By default, the random recruitment AI chooses a unit to recruit only from those types for which it has sufficient gold.  If this parameter is set to 'yes', it will instead choose from all available unit types and end recruiting for the turn if there is not enough gold to recruit it this turn (even if other, affordable unit types exist).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a random recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_random&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [probability]&lt;br /&gt;
                 type=Swordsman,Peasant&lt;br /&gt;
                 probability=8&lt;br /&gt;
             [/probability]&lt;br /&gt;
 &lt;br /&gt;
             skip_low_gold_recruiting=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Rush Recruitment AI (ai_type=recruit_rushers) ===&lt;br /&gt;
&lt;br /&gt;
This AI replaces the default recruiting with the rush recruiting used in the new (as of Version 1.11.1) Experimental AI.&lt;br /&gt;
&lt;br /&gt;
Enable the Rush Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_rushers&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* {{DevFeature1.15|2}} '''high_level_fraction'''=0: (non-negative number) The approximate fraction of units of level 2 or higher to be recruited. This is defined as fraction of units on the map, not of new units being recruited during a turn or over several turns (which makes a difference if there are already units on the map). The effect is also cumulative per level (starting from level 2), meaning that 1/3 of L2 units will be recruited in the example above, 1/3*1/3=1/9 L3s etc. The default value is zero, which leaves the high-level unit recruiting to the default Experimental AI algorithm resulting usually in very few such units being recruited.&lt;br /&gt;
* '''randomness'''=0.1: (number)  A random number is applied to the rusher recruit engine's score to prevent the recruitment pattern from being too predictable. 0 causes no randomness to be applied, while larger numbers increase the random effect. A value of 1-2 generates results in which the random effect is approximately equal to the scored effect. Extremely high values are essentially entirely random.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a rush recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_rushers&lt;br /&gt;
             action=add&lt;br /&gt;
          [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Simple Attack Micro AI (ai_type=simple_attack) ==&lt;br /&gt;
&lt;br /&gt;
The Simple Attack Micro AI is meant as an addition to the default combat candidate action for when one wants to force the AI to do attacks that it would otherwise not do (for example on units that are 1 XP from leveling) or to execute certain attacks before all others (for example, attack with all &amp;quot;expendable&amp;quot; units first).  It is not set up to be a replacement for the default attacks.  Some notes:&lt;br /&gt;
&lt;br /&gt;
* The algorithm is different from and simpler than that of the default combat CA.  For example, no attack combinations are considered, only individual attacks.  As a result, the [[AiWML#List_of_AI_Aspects|default aspects]] (aggression, caution, [avoid], ...) are not taken into account.  Also, if lots of units are involved, evaluation of this Micro AI is potentially quite a bit slower than of the default attacks.&lt;br /&gt;
&lt;br /&gt;
* If an attack matching the Standard Unit Filters described below is found, this attack will always be executed, no matter how bad the odds are.  Thus, this Micro AI can be used to force the AI to do attacks it would otherwise avoid.&lt;br /&gt;
&lt;br /&gt;
* Other than selecting the attacker/defender units, this AI is currently not configurable.  Let us know if you would like to see certain options to be added.  (No guarantees that we will add them, but we will definitely consider them.)&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Simple Attack&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=simple_attack&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Simple Attack specific keys for the [micro_ai] tag===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  It is, however, in general not desirable to use this AI without at least one of the optional Standard Unit Filters below, as this AI is not meant as a replacement of the default combat candidate action.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=110000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 110,000, which means that it is executed right before the default combat candidate action.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Simple Attack action.  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units the AI should attack.  It defaults to all enemy units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
* '''weapon'''=-1: (integer) The number of the weapon that should be used for this attack.  Weapon numbers start at 1 (not 0).  -1 stands for &amp;quot;best weapon&amp;quot; (by whatever criterion the AI chooses to use).   '''Available from Wesnoth 1.11.8'''&lt;br /&gt;
&lt;br /&gt;
Below are 2 examples of Simple Attack Micro AI configurations from the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  The first one shows how to force the AI to attack with all Soulless and Walking Corpse units first:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110000&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless,Walking Corpse&lt;br /&gt;
             [/filter]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
The second example executes attacks by Soulless on all enemy units that are 1 XP from leveling.  Note that the Lua code in the preload event is there simply in order to code the &amp;quot;1 XP from advancing&amp;quot; condition.  It is not needed for most other SUFs, as can be seen in the previous example.&lt;br /&gt;
     [event]&lt;br /&gt;
         name=preload&lt;br /&gt;
         first_time_only=no&lt;br /&gt;
         [lua]&lt;br /&gt;
             code=&amp;lt;&amp;lt;&lt;br /&gt;
                 function close_to_advancing(unit)&lt;br /&gt;
                     if (unit.experience &amp;gt;= unit.max_experience-1) then&lt;br /&gt;
                         return true&lt;br /&gt;
                     else&lt;br /&gt;
                         return false&lt;br /&gt;
                     end&lt;br /&gt;
                 end&lt;br /&gt;
             &amp;gt;&amp;gt;&lt;br /&gt;
         [/lua]&lt;br /&gt;
     [/event]&lt;br /&gt;
 &lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110001&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless  # No Walking Corpses; L0 units don't advance enemy&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 lua_function = &amp;quot;close_to_advancing&amp;quot;&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
Several Simple Attack Micro AIs can be combined for the same AI side.  The two examples above are both used (note the different values for ca_score) in the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
== Custom Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|5}}&lt;br /&gt;
&lt;br /&gt;
From 1.13.5 onwards, adding a new Micro AI can be easily done without altering any core files or overriding the micro_ai tag. The [micro_ai] tag now looks up its ''ai_type'' parameter in the &amp;lt;code&amp;gt;wesnoth.micro_ais&amp;lt;/code&amp;gt; table and uses the micro AI setup function found there to determine which keys are required and allowed and what candidate actions to add.&lt;br /&gt;
&lt;br /&gt;
A micro AI setup function takes as its single parameter the contents of the [micro_ai] tag and returns three separate values:&lt;br /&gt;
*'''required''': A list of required keys and tags, as strings. Tags are indicated by enclosing them within square brackets in the string. If a key or tag in this list is missing from the [micro_ai] tag, an error will be printed.&lt;br /&gt;
*'''optional''': Similar to above, a list of optional keys and tags, as strings. No error will be printed if a key or tag in this list is missing, nor will an error be printed if it is present (as would be the case for unrecognized keys or tags).&lt;br /&gt;
*'''ca_params''': A table specifying the candidate actions that constitute the micro AI. It should contain an ''ai_id'' key (which specifies a base ID to be used for candidate actions of this micro AI) and a series of candidate action definitions. Each candidate action definition is a table containing the following keys:&lt;br /&gt;
**'''ca_id''': The ID used for the candidate action. This will be combined with the ''ai_id'' to produce the final ID.&lt;br /&gt;
**'''location''': Path to the Lua file that defines the candidate action.&lt;br /&gt;
**'''score''': The maximum score for the candidate action. If the [micro_ai] tag specified a ''ca_score'' parameter, the score here should normally be based on that.&lt;br /&gt;
&lt;br /&gt;
(The return statement would thus look like &amp;lt;syntaxhighlight lang='lua' inline&amp;gt;return required, optional, ca_params&amp;lt;/syntaxhighlight&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
'''Examples''' of micro AI setup functions can be found in the [https://github.com/wesnoth/wesnoth/tree/1.14/data/ai/micro_ais/mai-defs default MAI definitions directory].  See the [https://github.com/wesnoth/wesnoth/blob/1.14/data/ai/micro_ais/mai-defs/animals.lua#L4-L12 Big Animals MAI definition] for a simple example.  For custom micro AIs, the setup code should be put into a ''[lua]'' tag inside a preload event.&lt;br /&gt;
&lt;br /&gt;
Since the function has full access to the contents of the [micro_ai] tag, it can conditionally add candidate actions to '''ca_params''' depending on the values of other keys within the [micro_ai] tag. It can also perform other actions such as adding facets to an aspect; if it does such things, it should pay attention to the ''action'' key to determine whether it's being added or removed.&lt;br /&gt;
&lt;br /&gt;
There are two useful helper functions that can be used by Micro AI setup functions requiring aspect changes. They can be brought into scope by a line similar to the following:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='lua&amp;gt;local MAIH = wesnoth.require(&amp;quot;ai/micro_ais/micro_ai_helper.lua&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* MAIH.add_aspects(side, aspect_params)&lt;br /&gt;
* MAIH.delete_aspects(side, aspect_params)&lt;br /&gt;
&lt;br /&gt;
The ''aspect_params'' parameter is an array of tables, each of which contains the following keys:&lt;br /&gt;
*'''aspect''': The aspect to change.&lt;br /&gt;
*'''facet''': The WML defining the facet. This must include an ''id'' key in order for the aspect to be correctly deleted. For maximum compatibility, incorporate the ''ca_id'' key from the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AiWML&amp;diff=66691</id>
		<title>AiWML</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AiWML&amp;diff=66691"/>
		<updated>2021-02-27T16:41:04Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* List of AI Aspects */ Minor wording update to retreat aspects&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{WML Tags}}&lt;br /&gt;
&lt;br /&gt;
== Customizing the Default AI Using WML ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
This page contains:&lt;br /&gt;
* Descriptions of the aspects and goals of the default AI&lt;br /&gt;
* Instructions for setting them up at the beginning of the scenario in a [side] tag&lt;br /&gt;
&lt;br /&gt;
This page does not contain:&lt;br /&gt;
* Instructions for [[LuaAI#Dynamic_Lua_Aspects|setting up aspects dynamically using Lua]].&lt;br /&gt;
* Instructions for [[Modifying_AI_Components#Modifying_Standard_Aspects|changing aspects and goals mid scenario]], although for many of them (for example, for all [[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Aspects|standard aspects]]) the methods are almost identical&lt;br /&gt;
* Instructions for some of the [[Modifying_AI_Components|more complex AI customization tasks]].&lt;br /&gt;
&lt;br /&gt;
== Defining Aspects and Goals of the RCA AI ==&lt;br /&gt;
&lt;br /&gt;
[[RCA_AI#Use_of_Aspects_in_the_RCA_AI|Aspects]] and [[RCA_AI#Use_of_Goals_and_Targets_in_the_RCA_AI|goals]] of the [[RCA_AI|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 [[#List_of_AI_Aspects|aspects themselves]] as arguments, as well as the following general keys:&lt;br /&gt;
* '''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 [https://github.com/wesnoth/wesnoth/blob/master/data/core/macros/schedules.cfg 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.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
* '''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. {{DevFeature1.13|5}} In 1.13, the meaning of '''ai_algorithm''' has changed and no longer selects a native AI that's built into the engine, as the RCA AI has now become the core system and no alternatives exist anymore. Instead, it selects a predefined ''[ai]'' definition, such as those defined in ''data/ai/ais''. Since these are defined in WML, you can easily build your own.&lt;br /&gt;
&lt;br /&gt;
An example of a simple use of the [ai] tag to set AI parameters (aspects) is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=wml&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    time_of_day=dusk&lt;br /&gt;
    aggression=0.99&lt;br /&gt;
    caution=0.01&lt;br /&gt;
    [avoid]&lt;br /&gt;
        [filter]&lt;br /&gt;
            side=2,3&lt;br /&gt;
        [/filter]&lt;br /&gt;
        radius=2&lt;br /&gt;
    [/avoid]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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 &amp;lt;code&amp;gt;:inspect&amp;lt;/code&amp;gt; in debug mode to verify whether an aspect was changed successfully.&lt;br /&gt;
&lt;br /&gt;
==List of AI Aspects==&lt;br /&gt;
&lt;br /&gt;
'''Note:''' Only one instance of each aspect will be active for a side at any time. Where multiple instances of the same aspect are included in an [ai] tag, the last whose conditions (time of day, turns, etc.) match will be the only one that applies.&lt;br /&gt;
&lt;br /&gt;
* '''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 &amp;lt;u&amp;gt;only&amp;lt;/u&amp;gt; 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:'''&lt;br /&gt;
** This is a simplified version of the more complex (and arguably more useful) application of a dynamic Lua ''advancements'' aspect, as described [[LuaAI#The_advancements_Aspect|here]].&lt;br /&gt;
** The ''advancements'' aspect only takes effect during the AI turn, that is, on offense only, not on defense.&lt;br /&gt;
** 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).&lt;br /&gt;
&lt;br /&gt;
* '''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.  The description below applies as written to the ''combat'' CA only, but aggression is taken into account by the ''high_xp_attack'' and ''spread_poison'' CAs as well (as of '''Wesnoth 1.13.5''' and '''1.15.3''' respectively).&lt;br /&gt;
** '''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.&lt;br /&gt;
** 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 &amp;gt;0 is found.  The combat CA score is zero if the highest attack score is &amp;lt;=0).&lt;br /&gt;
** 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.&lt;br /&gt;
** All the negative contributions to the score are multiplied by '(1-aggression)'.  This means that:&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
** 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.&lt;br /&gt;
&lt;br /&gt;
*'''[attacks]''': Filters the units considered for combat, both on the AI and the enemy sides.  Applies to the ''combat'', ''high_xp_attacks'' and ''spread_poison'' CAs only.  It cannot be set in the same way as the other aspects and is therefore described in a [[#Filtering_Combat_with_the_attacks_Aspect|separate section]] below.&lt;br /&gt;
&lt;br /&gt;
* '''[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''.&lt;br /&gt;
** '''[[StandardLocationFilter]]''': The locations for the AI to avoid.  Do not use a [filter_location] tag.&lt;br /&gt;
**'''Note:''' Only one instance of an [avoid] aspect will be active for a side at any time. Where multiple instances are included in an [ai] tag, the last whose conditions (time of day, turns, etc.) match will be the only one that applies.&lt;br /&gt;
&lt;br /&gt;
* '''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 [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|evaluation scores]]):&lt;br /&gt;
** ''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].&lt;br /&gt;
** ''Combat'' CA: &lt;br /&gt;
*** 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 &amp;quot;exposure&amp;quot; rating of the units.  This exposure is then subtracted from the 'attack score' as described for ''aggression'' above.&lt;br /&gt;
*** 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.)&lt;br /&gt;
*** If the AI leader is used in an attack, the AI ''always'' uses 'caution=2' for the evaluation of that attack.&lt;br /&gt;
** ''Retreat'' CA: {{DevFeature1.15|3}} This CA has been removed.&lt;br /&gt;
*** 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 &amp;lt;tt&amp;gt;caution &amp;amp;times; their_power &amp;gt; our_power&amp;lt;/tt&amp;gt;. 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.&lt;br /&gt;
*** 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. &lt;br /&gt;
*** The AI never retreats if caution is set to 0 or lower.&lt;br /&gt;
** ''Move-to-targets'' CA:&lt;br /&gt;
*** 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. &amp;quot;Dangerous&amp;quot; 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 &amp;lt;tt&amp;gt;our_strength / their_strength &amp;gt; 0.5 + caution&amp;lt;/tt&amp;gt;. 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.&lt;br /&gt;
***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.&lt;br /&gt;
*** The AI also considers retreating units during the move-to-target phase based on criteria similar to those for the retreat CA.&lt;br /&gt;
&lt;br /&gt;
* '''grouping'''=&amp;quot;offensive&amp;quot;: (string) How the AI should try to group units.  Applies to ''move-to-targets'' CA only.  Possible values:&lt;br /&gt;
** ''offensive'': Makes the AI try to group units together before attacking.&lt;br /&gt;
** ''defensive'': Makes the AI group units together very conservatively, only advancing them much beyond its castle if it has overwhelming force.&lt;br /&gt;
** ''no'': Makes the AI use no grouping behavior.&lt;br /&gt;
&lt;br /&gt;
* '''leader_aggression'''=&amp;quot;-4.0&amp;quot;: 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''.&lt;br /&gt;
&lt;br /&gt;
* '''[leader_goal]'''=&amp;quot;&amp;quot;: Makes the AI try to move its leader to a specific location.  Applies to ''move-leader-to-goals'' CA only.&lt;br /&gt;
** '''x''', '''y''': The location toward which the AI should move its leader.&lt;br /&gt;
** '''auto_remove'''=no: (bool)  If 'no' (default), the AI moves the leader to the goal, after which he stays there until [leader_goal] is [[Modifying_AI_Components#Modifying_Standard_Aspects|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).&lt;br /&gt;
** '''id'''=&amp;quot;&amp;quot;: (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] [[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Aspects|facet]].  Thus, in principle for this to work, you need to set the id of the facet as described [[Modifying_AI_Components#Modifying_Standard_Aspects|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. {{DevFeature1.13|5}} 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.&lt;br /&gt;
**'''max_risk'''=1-caution: (double: meaningful values are &amp;gt;=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.&lt;br /&gt;
&lt;br /&gt;
* '''leader_ignores_keep'''=no: (bool) Set to 'yes' for this aspect to apply to all leaders of a side. {{DevFeature1.15|3}} In addition to 'yes/no', the aspect can now also be set to a comma-separated list of unit ids. The aspect then only applies to leaders with ids matching an id on that list. Setting leader_ignores_keep for some or all leaders means that:&lt;br /&gt;
** The affected 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). Thus, these leaders behave in the same way as any other unit of the AI side.&lt;br /&gt;
** This also means that these 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.&lt;br /&gt;
&lt;br /&gt;
* '''leader_value'''=3: (double) A number 0 or higher which determines the value of enemy leaders as [[#AI_Targets_and_Goals|targets]].  Affects ''move-to-targets CA'' only (and therefore specifically does not apply to attacks).&lt;br /&gt;
&lt;br /&gt;
* '''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''. {{DevFeature1.15|3}} In addition to 'yes/no', the aspect can now also be set to a comma-separated list of unit ids. The aspect then only applies to leaders with ids matching an id on that list.&lt;br /&gt;
&lt;br /&gt;
* '''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. {{DevFeature1.15|3}} In addition to 'yes/no', the aspect can now also be set to a comma-separated list of unit ids. The aspect then only applies to leaders with ids matching an id on that list.&lt;br /&gt;
&lt;br /&gt;
*'''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 [[AI_Recruitment|separate page]].&lt;br /&gt;
&lt;br /&gt;
*'''retreat_factor'''=0.25 (double) {{DevFeature1.15|11}} Affects ''retreat-injured'' CA only. Determines how injured AI units have to be before they attempt retreating. This is approximately the fraction of the units' hitpoints, but other factors play a role also, such as the terrain the units are on, and leaders and poisoned units retreat somewhat earlier. Setting this to 0 (or negative values) disables retreating. Note that even setting this to very large values still requires units to be somewhat injured before they start retreating.&lt;br /&gt;
&lt;br /&gt;
*'''retreat_enemy_weight'''=1.0 (double) {{DevFeature1.15|11}} Affects ''retreat-injured'' CA only. When deciding where to retreat to, the AI considers how much healing a location provides and weighs it against the threat of all the enemies that can get there (balanced by the own units that can get there). In this evaluation, it multiplies the enemy threat rating by ''retreat_enemy_weight'', meaning that the larger its value is, the more a unit tries to move away from enemies.  There are three regimes for this aspect:&lt;br /&gt;
** Positive values: Only locations that provide healing (both from terrain and from adjacent healers which have no MP left) are considered. For regenerating units, all hexes are treated as if they provide healing.&lt;br /&gt;
** Zero: Enemy threats are ignored&lt;br /&gt;
** Negative values: All hexes are considered for retreating. The enemy threat rating is multiplied by the absolute value of the aspect value. Thus, if very negative values are used (e.g. -100), this can be used to make units run away from enemies with little consideration for anything else.&lt;br /&gt;
&lt;br /&gt;
* '''scout_village_targeting'''=3: (double) The AI multiplies the value of village [[#AI_Targets_and_Goals|targets]] for scouts by this value.  Affects ''move-to-targets'' CA only.&lt;br /&gt;
&lt;br /&gt;
* '''simple_targeting'''=no: (bool) If 'yes', the AI moves its units toward [[#AI_Targets_and_Goals|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.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
* '''village_value'''=1: (double) A number 0 or higher which determines how much the AI tries to go for villages as [[#AI_Targets_and_Goals|targets]].  Affects ''move-to-targets'' CA only.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
=== Removed AI Aspects ===&lt;br /&gt;
&lt;br /&gt;
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. {{DevFeature1.13|5}} Except for attack_depth, these have now all been removed.&lt;br /&gt;
&lt;br /&gt;
* '''attack_depth'''=5: (int)&lt;br /&gt;
* '''number_of_possible_recruits_to_force_recruit'''=3.1: (double)&lt;br /&gt;
* '''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.&lt;br /&gt;
** It is, in principle, still possible to use this aspect together with the [[Wesnoth_AI_Framework#Recruitment_Stage|recruitment stage]], but this is discouraged for the reasons given at this link.&lt;br /&gt;
* '''recruitment_ignore_bad_combat'''=no: (bool)&lt;br /&gt;
* '''recruitment_ignore_bad_movement'''=no: (bool)&lt;br /&gt;
&lt;br /&gt;
==AI Targets and Goals==&lt;br /&gt;
&lt;br /&gt;
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 [[RCA_AI#RCA_AI_Aspect_and_Goal_Configuration|base values]].  Additional targets can be defined using the [goal] tag.&lt;br /&gt;
&lt;br /&gt;
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 [[RCA_AI#Use_of_Goals_and_Targets_in_the_RCA_AI|here]].  The sections below only describe how to set up goals in order to add targets for the ''move-to-targets'' CA.&lt;br /&gt;
&lt;br /&gt;
===The [goal] Tag===&lt;br /&gt;
&lt;br /&gt;
The [goal] tag defines units or locations as [[#AI_Targets_and_Goals|move targets]] (not attack targets!) for the AI.  '''Applies to ''move-to-targets'' CA only'''. &amp;lt;-- That is '''extremely important''' to understand, in particular the fact that the [goal] tag has '''no influence whatsoever on attacks'''.&lt;br /&gt;
&lt;br /&gt;
 The following keys/tags can be used:&lt;br /&gt;
* '''name'''=&amp;quot;target&amp;quot;: (string)  The following values are possible and result in different types of targets, as shown in the examples below:&lt;br /&gt;
** ''target'':  The (default) target goal specifies target units (not necessarily enemy units) toward which the AI should move its units. {{DevFeature1.13|5}} target_unit is now a synonym for this.&lt;br /&gt;
** ''target_location'': Specifies target locations toward which the AI should move its units.&lt;br /&gt;
** ''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.&lt;br /&gt;
** ''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.&lt;br /&gt;
** ''protect_my_unit'': ('''deprecated''') &amp;lt;s&amp;gt;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.&amp;lt;/s&amp;gt;&lt;br /&gt;
** ''lua_goal'': A dynamically calculated goal written in Lua. See [[LuaAI#Lua_Goals_and_Targets|here]] for details on how this works.&lt;br /&gt;
&lt;br /&gt;
* '''[criteria]'''=&amp;quot;&amp;quot;: Contains a [[StandardUnitFilter]] (for ''target'', ''protect_unit'' or ''protect_my_unit'') or [[StandardLocationFilter]] (for ''target_location'' or ''protect_location'') describing the targets.&lt;br /&gt;
&lt;br /&gt;
* '''value'''=0: (double) The value of the goal.&lt;br /&gt;
&lt;br /&gt;
* '''protect_radius'''=20: (int) The protection radius.  Applies to ''protect_location'', ''protect_unit'' and ''protect_my_unit''.&lt;br /&gt;
&lt;br /&gt;
===Examples of [goal] Tag Usage===&lt;br /&gt;
&lt;br /&gt;
'''target:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        [criteria] #NOTE: this is a SUF, because we're targeting a unit&lt;br /&gt;
            side=3&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''target_location:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=target_location&lt;br /&gt;
        [criteria] #NOTE: this is a SLF, because we're targeting a location&lt;br /&gt;
            x,y=42,20&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''protect_location:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=protect_location&lt;br /&gt;
        [criteria] #NOTE: this is a SLF, because we're protecting a location&lt;br /&gt;
            x,y=42,20&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        protect_radius=16&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''protect_unit:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=protect_unit&lt;br /&gt;
        [criteria] #NOTE: this is a SUF, because we're protecting a unit&lt;br /&gt;
            side=3&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        protect_radius=16&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''How to use protect_unit to protect the own leader''' (We are side 2)&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=protect_unit&lt;br /&gt;
        [criteria]&lt;br /&gt;
            side=2&lt;br /&gt;
            canrecruit=yes&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        protect_radius=8&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Deprecated AI Targeting Aspects ===&lt;br /&gt;
&lt;br /&gt;
The following AI targeting parameters currently still work, but have been superseded by the [[#The_.5Bgoal.5D_Tag|[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.&lt;br /&gt;
&lt;br /&gt;
* '''[target]'''=&amp;quot;&amp;quot;: '''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.&lt;br /&gt;
** '''[[StandardUnitFilter]]''': Do not use a [filter] tag.&lt;br /&gt;
** '''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.&lt;br /&gt;
&lt;br /&gt;
* '''[protect_location]'''=&amp;quot;&amp;quot;: '''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.&lt;br /&gt;
** '''x''', '''y''': Standard coordinates. These indicate the location the AI is protecting.&lt;br /&gt;
** '''radius''': The radius around it to protect (0 indicates a single hex). {{DevFeature1.13|5}} This key has been changed to protect_radius, though it's preferred to change old code to use the [goal] tag instead.&lt;br /&gt;
** '''value''': The importance of protecting this location.&lt;br /&gt;
&lt;br /&gt;
* '''[protect_unit]'''=&amp;quot;&amp;quot;: '''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.&lt;br /&gt;
** '''[[StandardUnitFilter]]''': The unit(s) to protect. Do not use a [filter] tag.&lt;br /&gt;
** '''radius''': The radius around it to protect (0 indicates a single hex). {{DevFeature1.13|5}} This key has been changed to protect_radius, though it's preferred to change old code to use the [goal] tag instead.&lt;br /&gt;
** '''value''': The importance of protecting this unit.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
== Filtering which Units Participate in which Actions==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}}&lt;br /&gt;
&lt;br /&gt;
Each [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|candidate action of the default AI]] can be restricted to apply to only a subset of the AI units. This is done by adding a '''[filter_own]''' tag to the '''[[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Candidate_Actions|[candidate_action]]]''' tag definition.&lt;br /&gt;
&lt;br /&gt;
Typical use cases:&lt;br /&gt;
* Restricting an action to certain units so that other units are not used to this. For example, village grabbing could be restricted to scout units only, so that slower units do not take detours on their way toward the enemies. See the example below.&lt;br /&gt;
* Excluding units from a certain action in order to have them available for other actions afterward. For example, one might want to add a custom candidate action which moves healers behind injured units (or use the existing [[Micro_AIs|Micro AI]] for this). Ideally, this should happen after ''all'' other units have moved, specifically also after the move-to-targets candidate action (MtT CA) is done. In the default setting, however, the MtT CA would also move the healers. Thus, we would want to exclude them using a '''[filter_own]''' tag.&lt;br /&gt;
* Adding two instances of the same candidate action with different scores to force the order in which units participate in this action. For example, one could add another instance of the move-leader-to-keep CA with a '''[filter_own]''' tag restricting it to one leader, in order to make this leader move to a keep (and therefore recruit) first.&lt;br /&gt;
&lt;br /&gt;
Note that for the combat AI, using the attacks aspect to filter units as described in the next section '''is slightly more efficient'''. Thus, that should be the preferred method if there are many units on the map.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[candidate_action]&lt;br /&gt;
	id=villages&lt;br /&gt;
	engine=cpp&lt;br /&gt;
	name=ai_default_rca::get_villages_phase&lt;br /&gt;
	max_score=60000&lt;br /&gt;
	score=60000&lt;br /&gt;
	[filter_own]&lt;br /&gt;
		type=Wolf Rider&lt;br /&gt;
	[/filter_own]&lt;br /&gt;
[/candidate_action]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Filtering Combat with the ''attacks'' Aspect==&lt;br /&gt;
&lt;br /&gt;
The ''attacks'' aspect lets us filter the units considered by the ''combat'', ''high_xp_attacks'' and ''spread_poison'' 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&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [aspect]&lt;br /&gt;
        id=attacks&lt;br /&gt;
        [facet]&lt;br /&gt;
            invalidate_on_gamestate_change=yes&lt;br /&gt;
            [filter_own]&lt;br /&gt;
                type=Elvish Sorceress,Elvish Enchantress,Elvish Sylph&lt;br /&gt;
            [/filter_own]&lt;br /&gt;
            [filter_enemy]&lt;br /&gt;
                race=undead&lt;br /&gt;
            [/filter_enemy]&lt;br /&gt;
        [/facet]&lt;br /&gt;
    [/aspect]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Several important notes:&lt;br /&gt;
* The ''attacks'' aspect is a ''composite'' aspect and cannot be defined using the simple syntax of the other ''standard'' aspects.  See [[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Aspects|here]] for a description of the syntax of the example.&lt;br /&gt;
* This only works if &amp;lt;code&amp;gt;invalidate_on_gamestate_change=yes&amp;lt;/code&amp;gt; is 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 [[Wesnoth_AI_Framework#Some_more_on_the_invalidation_keys|here]].&lt;br /&gt;
* If [filter_own] or [filter_enemy] are omitted, the selection defaults to all units of the respective sides.&lt;br /&gt;
* '''Most importantly''': The above example results in sorceress-line vs undead attacks being the &amp;lt;u&amp;gt;only&amp;lt;/u&amp;gt; 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:&lt;br /&gt;
** Setting up the [filter_own] or [filter_enemy] [[StandardUnitFilter]]s to adapt dynamically to the situation on the map.&lt;br /&gt;
** 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_AIs#Micro_AI_Test_and_Demo_Scenarios|Micro AI test scenario]] [https://github.com/wesnoth/wesnoth/blob/master/data/ai/micro_ais/scenarios/protect_unit.cfg ''Protect Unit''].&lt;br /&gt;
** Using [[LuaAI#Dynamic_Lua_Aspects|dynamic Lua aspects]] is in principle also possible, but far from easy due to the complex tables this aspect returns.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|5}}&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [attacks]&lt;br /&gt;
        invalidate_on_gamestate_change=yes&lt;br /&gt;
        [filter_own]&lt;br /&gt;
            type=Elvish Sorceress,Elvish Enchantress,Elvish Sylph&lt;br /&gt;
        [/filter_own]&lt;br /&gt;
        [filter_enemy]&lt;br /&gt;
            race=undead&lt;br /&gt;
        [/filter_enemy]&lt;br /&gt;
    [/attacks]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category: WML Reference]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AiWML&amp;diff=66690</id>
		<title>AiWML</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AiWML&amp;diff=66690"/>
		<updated>2021-02-27T16:35:27Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* List of AI Aspects */ add retreat_factor and retreat_enemy_weight aspects&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{WML Tags}}&lt;br /&gt;
&lt;br /&gt;
== Customizing the Default AI Using WML ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
This page contains:&lt;br /&gt;
* Descriptions of the aspects and goals of the default AI&lt;br /&gt;
* Instructions for setting them up at the beginning of the scenario in a [side] tag&lt;br /&gt;
&lt;br /&gt;
This page does not contain:&lt;br /&gt;
* Instructions for [[LuaAI#Dynamic_Lua_Aspects|setting up aspects dynamically using Lua]].&lt;br /&gt;
* Instructions for [[Modifying_AI_Components#Modifying_Standard_Aspects|changing aspects and goals mid scenario]], although for many of them (for example, for all [[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Aspects|standard aspects]]) the methods are almost identical&lt;br /&gt;
* Instructions for some of the [[Modifying_AI_Components|more complex AI customization tasks]].&lt;br /&gt;
&lt;br /&gt;
== Defining Aspects and Goals of the RCA AI ==&lt;br /&gt;
&lt;br /&gt;
[[RCA_AI#Use_of_Aspects_in_the_RCA_AI|Aspects]] and [[RCA_AI#Use_of_Goals_and_Targets_in_the_RCA_AI|goals]] of the [[RCA_AI|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 [[#List_of_AI_Aspects|aspects themselves]] as arguments, as well as the following general keys:&lt;br /&gt;
* '''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 [https://github.com/wesnoth/wesnoth/blob/master/data/core/macros/schedules.cfg 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.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
* '''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. {{DevFeature1.13|5}} In 1.13, the meaning of '''ai_algorithm''' has changed and no longer selects a native AI that's built into the engine, as the RCA AI has now become the core system and no alternatives exist anymore. Instead, it selects a predefined ''[ai]'' definition, such as those defined in ''data/ai/ais''. Since these are defined in WML, you can easily build your own.&lt;br /&gt;
&lt;br /&gt;
An example of a simple use of the [ai] tag to set AI parameters (aspects) is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=wml&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    time_of_day=dusk&lt;br /&gt;
    aggression=0.99&lt;br /&gt;
    caution=0.01&lt;br /&gt;
    [avoid]&lt;br /&gt;
        [filter]&lt;br /&gt;
            side=2,3&lt;br /&gt;
        [/filter]&lt;br /&gt;
        radius=2&lt;br /&gt;
    [/avoid]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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 &amp;lt;code&amp;gt;:inspect&amp;lt;/code&amp;gt; in debug mode to verify whether an aspect was changed successfully.&lt;br /&gt;
&lt;br /&gt;
==List of AI Aspects==&lt;br /&gt;
&lt;br /&gt;
'''Note:''' Only one instance of each aspect will be active for a side at any time. Where multiple instances of the same aspect are included in an [ai] tag, the last whose conditions (time of day, turns, etc.) match will be the only one that applies.&lt;br /&gt;
&lt;br /&gt;
* '''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 &amp;lt;u&amp;gt;only&amp;lt;/u&amp;gt; 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:'''&lt;br /&gt;
** This is a simplified version of the more complex (and arguably more useful) application of a dynamic Lua ''advancements'' aspect, as described [[LuaAI#The_advancements_Aspect|here]].&lt;br /&gt;
** The ''advancements'' aspect only takes effect during the AI turn, that is, on offense only, not on defense.&lt;br /&gt;
** 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).&lt;br /&gt;
&lt;br /&gt;
* '''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.  The description below applies as written to the ''combat'' CA only, but aggression is taken into account by the ''high_xp_attack'' and ''spread_poison'' CAs as well (as of '''Wesnoth 1.13.5''' and '''1.15.3''' respectively).&lt;br /&gt;
** '''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.&lt;br /&gt;
** 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 &amp;gt;0 is found.  The combat CA score is zero if the highest attack score is &amp;lt;=0).&lt;br /&gt;
** 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.&lt;br /&gt;
** All the negative contributions to the score are multiplied by '(1-aggression)'.  This means that:&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
** 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.&lt;br /&gt;
&lt;br /&gt;
*'''[attacks]''': Filters the units considered for combat, both on the AI and the enemy sides.  Applies to the ''combat'', ''high_xp_attacks'' and ''spread_poison'' CAs only.  It cannot be set in the same way as the other aspects and is therefore described in a [[#Filtering_Combat_with_the_attacks_Aspect|separate section]] below.&lt;br /&gt;
&lt;br /&gt;
* '''[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''.&lt;br /&gt;
** '''[[StandardLocationFilter]]''': The locations for the AI to avoid.  Do not use a [filter_location] tag.&lt;br /&gt;
**'''Note:''' Only one instance of an [avoid] aspect will be active for a side at any time. Where multiple instances are included in an [ai] tag, the last whose conditions (time of day, turns, etc.) match will be the only one that applies.&lt;br /&gt;
&lt;br /&gt;
* '''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 [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|evaluation scores]]):&lt;br /&gt;
** ''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].&lt;br /&gt;
** ''Combat'' CA: &lt;br /&gt;
*** 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 &amp;quot;exposure&amp;quot; rating of the units.  This exposure is then subtracted from the 'attack score' as described for ''aggression'' above.&lt;br /&gt;
*** 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.)&lt;br /&gt;
*** If the AI leader is used in an attack, the AI ''always'' uses 'caution=2' for the evaluation of that attack.&lt;br /&gt;
** ''Retreat'' CA: {{DevFeature1.15|3}} This CA has been removed.&lt;br /&gt;
*** 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 &amp;lt;tt&amp;gt;caution &amp;amp;times; their_power &amp;gt; our_power&amp;lt;/tt&amp;gt;. 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.&lt;br /&gt;
*** 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. &lt;br /&gt;
*** The AI never retreats if caution is set to 0 or lower.&lt;br /&gt;
** ''Move-to-targets'' CA:&lt;br /&gt;
*** 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. &amp;quot;Dangerous&amp;quot; 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 &amp;lt;tt&amp;gt;our_strength / their_strength &amp;gt; 0.5 + caution&amp;lt;/tt&amp;gt;. 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.&lt;br /&gt;
***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.&lt;br /&gt;
*** The AI also considers retreating units during the move-to-target phase based on criteria similar to those for the retreat CA.&lt;br /&gt;
&lt;br /&gt;
* '''grouping'''=&amp;quot;offensive&amp;quot;: (string) How the AI should try to group units.  Applies to ''move-to-targets'' CA only.  Possible values:&lt;br /&gt;
** ''offensive'': Makes the AI try to group units together before attacking.&lt;br /&gt;
** ''defensive'': Makes the AI group units together very conservatively, only advancing them much beyond its castle if it has overwhelming force.&lt;br /&gt;
** ''no'': Makes the AI use no grouping behavior.&lt;br /&gt;
&lt;br /&gt;
* '''leader_aggression'''=&amp;quot;-4.0&amp;quot;: 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''.&lt;br /&gt;
&lt;br /&gt;
* '''[leader_goal]'''=&amp;quot;&amp;quot;: Makes the AI try to move its leader to a specific location.  Applies to ''move-leader-to-goals'' CA only.&lt;br /&gt;
** '''x''', '''y''': The location toward which the AI should move its leader.&lt;br /&gt;
** '''auto_remove'''=no: (bool)  If 'no' (default), the AI moves the leader to the goal, after which he stays there until [leader_goal] is [[Modifying_AI_Components#Modifying_Standard_Aspects|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).&lt;br /&gt;
** '''id'''=&amp;quot;&amp;quot;: (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] [[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Aspects|facet]].  Thus, in principle for this to work, you need to set the id of the facet as described [[Modifying_AI_Components#Modifying_Standard_Aspects|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. {{DevFeature1.13|5}} 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.&lt;br /&gt;
**'''max_risk'''=1-caution: (double: meaningful values are &amp;gt;=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.&lt;br /&gt;
&lt;br /&gt;
* '''leader_ignores_keep'''=no: (bool) Set to 'yes' for this aspect to apply to all leaders of a side. {{DevFeature1.15|3}} In addition to 'yes/no', the aspect can now also be set to a comma-separated list of unit ids. The aspect then only applies to leaders with ids matching an id on that list. Setting leader_ignores_keep for some or all leaders means that:&lt;br /&gt;
** The affected 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). Thus, these leaders behave in the same way as any other unit of the AI side.&lt;br /&gt;
** This also means that these 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.&lt;br /&gt;
&lt;br /&gt;
* '''leader_value'''=3: (double) A number 0 or higher which determines the value of enemy leaders as [[#AI_Targets_and_Goals|targets]].  Affects ''move-to-targets CA'' only (and therefore specifically does not apply to attacks).&lt;br /&gt;
&lt;br /&gt;
* '''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''. {{DevFeature1.15|3}} In addition to 'yes/no', the aspect can now also be set to a comma-separated list of unit ids. The aspect then only applies to leaders with ids matching an id on that list.&lt;br /&gt;
&lt;br /&gt;
* '''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. {{DevFeature1.15|3}} In addition to 'yes/no', the aspect can now also be set to a comma-separated list of unit ids. The aspect then only applies to leaders with ids matching an id on that list.&lt;br /&gt;
&lt;br /&gt;
*'''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 [[AI_Recruitment|separate page]].&lt;br /&gt;
&lt;br /&gt;
*'''retreat_factor'''=0.25 (double) {{DevFeature1.15|11}} Determines how injured AI units have to be before they attempt retreating. This is approximately the fraction of the units' hitpoints, but other factors play a role also, such as the terrain the units are on, and leaders and poisoned units retreat somewhat earlier. Setting this to 0 (or negative values) disables retreating. Note that even setting this to very large values still requires units to be somewhat injured before they start retreating. Affects ''retreat-injured'' CA only.&lt;br /&gt;
&lt;br /&gt;
*'''retreat_enemy_weight'''=1.0 (double) {{DevFeature1.15|11}} When deciding where to retreat to, the AI considers how much healing a location provides and weighs it against the threat of all the enemies that can get there (balanced by the own units that can get there). In this evaluation, it multiplies the enemy threat rating by ''retreat_enemy_weight'', meaning that the larger its value is, the more a unit tries to move away from enemies.  There are three regimes for this aspect:&lt;br /&gt;
** Positive values: Only locations that provide healing (both from terrain and from adjacent healers which have no MP left) are considered. For regenerating units, all hexes are treated as if they provide healing.&lt;br /&gt;
** Zero: Enemy threats are ignored&lt;br /&gt;
** Negative values: All hexes are considered for retreating. Thus, if very negative values are used (e.g. -100), this can be used to make units run away from enemies with little consideration for anything else.&lt;br /&gt;
Affects ''retreat-injured'' CA only.&lt;br /&gt;
&lt;br /&gt;
* '''scout_village_targeting'''=3: (double) The AI multiplies the value of village [[#AI_Targets_and_Goals|targets]] for scouts by this value.  Affects ''move-to-targets'' CA only.&lt;br /&gt;
&lt;br /&gt;
* '''simple_targeting'''=no: (bool) If 'yes', the AI moves its units toward [[#AI_Targets_and_Goals|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.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
* '''village_value'''=1: (double) A number 0 or higher which determines how much the AI tries to go for villages as [[#AI_Targets_and_Goals|targets]].  Affects ''move-to-targets'' CA only.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
=== Removed AI Aspects ===&lt;br /&gt;
&lt;br /&gt;
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. {{DevFeature1.13|5}} Except for attack_depth, these have now all been removed.&lt;br /&gt;
&lt;br /&gt;
* '''attack_depth'''=5: (int)&lt;br /&gt;
* '''number_of_possible_recruits_to_force_recruit'''=3.1: (double)&lt;br /&gt;
* '''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.&lt;br /&gt;
** It is, in principle, still possible to use this aspect together with the [[Wesnoth_AI_Framework#Recruitment_Stage|recruitment stage]], but this is discouraged for the reasons given at this link.&lt;br /&gt;
* '''recruitment_ignore_bad_combat'''=no: (bool)&lt;br /&gt;
* '''recruitment_ignore_bad_movement'''=no: (bool)&lt;br /&gt;
&lt;br /&gt;
==AI Targets and Goals==&lt;br /&gt;
&lt;br /&gt;
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 [[RCA_AI#RCA_AI_Aspect_and_Goal_Configuration|base values]].  Additional targets can be defined using the [goal] tag.&lt;br /&gt;
&lt;br /&gt;
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 [[RCA_AI#Use_of_Goals_and_Targets_in_the_RCA_AI|here]].  The sections below only describe how to set up goals in order to add targets for the ''move-to-targets'' CA.&lt;br /&gt;
&lt;br /&gt;
===The [goal] Tag===&lt;br /&gt;
&lt;br /&gt;
The [goal] tag defines units or locations as [[#AI_Targets_and_Goals|move targets]] (not attack targets!) for the AI.  '''Applies to ''move-to-targets'' CA only'''. &amp;lt;-- That is '''extremely important''' to understand, in particular the fact that the [goal] tag has '''no influence whatsoever on attacks'''.&lt;br /&gt;
&lt;br /&gt;
 The following keys/tags can be used:&lt;br /&gt;
* '''name'''=&amp;quot;target&amp;quot;: (string)  The following values are possible and result in different types of targets, as shown in the examples below:&lt;br /&gt;
** ''target'':  The (default) target goal specifies target units (not necessarily enemy units) toward which the AI should move its units. {{DevFeature1.13|5}} target_unit is now a synonym for this.&lt;br /&gt;
** ''target_location'': Specifies target locations toward which the AI should move its units.&lt;br /&gt;
** ''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.&lt;br /&gt;
** ''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.&lt;br /&gt;
** ''protect_my_unit'': ('''deprecated''') &amp;lt;s&amp;gt;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.&amp;lt;/s&amp;gt;&lt;br /&gt;
** ''lua_goal'': A dynamically calculated goal written in Lua. See [[LuaAI#Lua_Goals_and_Targets|here]] for details on how this works.&lt;br /&gt;
&lt;br /&gt;
* '''[criteria]'''=&amp;quot;&amp;quot;: Contains a [[StandardUnitFilter]] (for ''target'', ''protect_unit'' or ''protect_my_unit'') or [[StandardLocationFilter]] (for ''target_location'' or ''protect_location'') describing the targets.&lt;br /&gt;
&lt;br /&gt;
* '''value'''=0: (double) The value of the goal.&lt;br /&gt;
&lt;br /&gt;
* '''protect_radius'''=20: (int) The protection radius.  Applies to ''protect_location'', ''protect_unit'' and ''protect_my_unit''.&lt;br /&gt;
&lt;br /&gt;
===Examples of [goal] Tag Usage===&lt;br /&gt;
&lt;br /&gt;
'''target:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        [criteria] #NOTE: this is a SUF, because we're targeting a unit&lt;br /&gt;
            side=3&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''target_location:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=target_location&lt;br /&gt;
        [criteria] #NOTE: this is a SLF, because we're targeting a location&lt;br /&gt;
            x,y=42,20&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''protect_location:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=protect_location&lt;br /&gt;
        [criteria] #NOTE: this is a SLF, because we're protecting a location&lt;br /&gt;
            x,y=42,20&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        protect_radius=16&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''protect_unit:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=protect_unit&lt;br /&gt;
        [criteria] #NOTE: this is a SUF, because we're protecting a unit&lt;br /&gt;
            side=3&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        protect_radius=16&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''How to use protect_unit to protect the own leader''' (We are side 2)&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=protect_unit&lt;br /&gt;
        [criteria]&lt;br /&gt;
            side=2&lt;br /&gt;
            canrecruit=yes&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        protect_radius=8&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Deprecated AI Targeting Aspects ===&lt;br /&gt;
&lt;br /&gt;
The following AI targeting parameters currently still work, but have been superseded by the [[#The_.5Bgoal.5D_Tag|[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.&lt;br /&gt;
&lt;br /&gt;
* '''[target]'''=&amp;quot;&amp;quot;: '''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.&lt;br /&gt;
** '''[[StandardUnitFilter]]''': Do not use a [filter] tag.&lt;br /&gt;
** '''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.&lt;br /&gt;
&lt;br /&gt;
* '''[protect_location]'''=&amp;quot;&amp;quot;: '''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.&lt;br /&gt;
** '''x''', '''y''': Standard coordinates. These indicate the location the AI is protecting.&lt;br /&gt;
** '''radius''': The radius around it to protect (0 indicates a single hex). {{DevFeature1.13|5}} This key has been changed to protect_radius, though it's preferred to change old code to use the [goal] tag instead.&lt;br /&gt;
** '''value''': The importance of protecting this location.&lt;br /&gt;
&lt;br /&gt;
* '''[protect_unit]'''=&amp;quot;&amp;quot;: '''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.&lt;br /&gt;
** '''[[StandardUnitFilter]]''': The unit(s) to protect. Do not use a [filter] tag.&lt;br /&gt;
** '''radius''': The radius around it to protect (0 indicates a single hex). {{DevFeature1.13|5}} This key has been changed to protect_radius, though it's preferred to change old code to use the [goal] tag instead.&lt;br /&gt;
** '''value''': The importance of protecting this unit.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
== Filtering which Units Participate in which Actions==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}}&lt;br /&gt;
&lt;br /&gt;
Each [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|candidate action of the default AI]] can be restricted to apply to only a subset of the AI units. This is done by adding a '''[filter_own]''' tag to the '''[[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Candidate_Actions|[candidate_action]]]''' tag definition.&lt;br /&gt;
&lt;br /&gt;
Typical use cases:&lt;br /&gt;
* Restricting an action to certain units so that other units are not used to this. For example, village grabbing could be restricted to scout units only, so that slower units do not take detours on their way toward the enemies. See the example below.&lt;br /&gt;
* Excluding units from a certain action in order to have them available for other actions afterward. For example, one might want to add a custom candidate action which moves healers behind injured units (or use the existing [[Micro_AIs|Micro AI]] for this). Ideally, this should happen after ''all'' other units have moved, specifically also after the move-to-targets candidate action (MtT CA) is done. In the default setting, however, the MtT CA would also move the healers. Thus, we would want to exclude them using a '''[filter_own]''' tag.&lt;br /&gt;
* Adding two instances of the same candidate action with different scores to force the order in which units participate in this action. For example, one could add another instance of the move-leader-to-keep CA with a '''[filter_own]''' tag restricting it to one leader, in order to make this leader move to a keep (and therefore recruit) first.&lt;br /&gt;
&lt;br /&gt;
Note that for the combat AI, using the attacks aspect to filter units as described in the next section '''is slightly more efficient'''. Thus, that should be the preferred method if there are many units on the map.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[candidate_action]&lt;br /&gt;
	id=villages&lt;br /&gt;
	engine=cpp&lt;br /&gt;
	name=ai_default_rca::get_villages_phase&lt;br /&gt;
	max_score=60000&lt;br /&gt;
	score=60000&lt;br /&gt;
	[filter_own]&lt;br /&gt;
		type=Wolf Rider&lt;br /&gt;
	[/filter_own]&lt;br /&gt;
[/candidate_action]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Filtering Combat with the ''attacks'' Aspect==&lt;br /&gt;
&lt;br /&gt;
The ''attacks'' aspect lets us filter the units considered by the ''combat'', ''high_xp_attacks'' and ''spread_poison'' 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&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [aspect]&lt;br /&gt;
        id=attacks&lt;br /&gt;
        [facet]&lt;br /&gt;
            invalidate_on_gamestate_change=yes&lt;br /&gt;
            [filter_own]&lt;br /&gt;
                type=Elvish Sorceress,Elvish Enchantress,Elvish Sylph&lt;br /&gt;
            [/filter_own]&lt;br /&gt;
            [filter_enemy]&lt;br /&gt;
                race=undead&lt;br /&gt;
            [/filter_enemy]&lt;br /&gt;
        [/facet]&lt;br /&gt;
    [/aspect]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Several important notes:&lt;br /&gt;
* The ''attacks'' aspect is a ''composite'' aspect and cannot be defined using the simple syntax of the other ''standard'' aspects.  See [[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Aspects|here]] for a description of the syntax of the example.&lt;br /&gt;
* This only works if &amp;lt;code&amp;gt;invalidate_on_gamestate_change=yes&amp;lt;/code&amp;gt; is 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 [[Wesnoth_AI_Framework#Some_more_on_the_invalidation_keys|here]].&lt;br /&gt;
* If [filter_own] or [filter_enemy] are omitted, the selection defaults to all units of the respective sides.&lt;br /&gt;
* '''Most importantly''': The above example results in sorceress-line vs undead attacks being the &amp;lt;u&amp;gt;only&amp;lt;/u&amp;gt; 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:&lt;br /&gt;
** Setting up the [filter_own] or [filter_enemy] [[StandardUnitFilter]]s to adapt dynamically to the situation on the map.&lt;br /&gt;
** 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_AIs#Micro_AI_Test_and_Demo_Scenarios|Micro AI test scenario]] [https://github.com/wesnoth/wesnoth/blob/master/data/ai/micro_ais/scenarios/protect_unit.cfg ''Protect Unit''].&lt;br /&gt;
** Using [[LuaAI#Dynamic_Lua_Aspects|dynamic Lua aspects]] is in principle also possible, but far from easy due to the complex tables this aspect returns.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|5}}&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [attacks]&lt;br /&gt;
        invalidate_on_gamestate_change=yes&lt;br /&gt;
        [filter_own]&lt;br /&gt;
            type=Elvish Sorceress,Elvish Enchantress,Elvish Sylph&lt;br /&gt;
        [/filter_own]&lt;br /&gt;
        [filter_enemy]&lt;br /&gt;
            race=undead&lt;br /&gt;
        [/filter_enemy]&lt;br /&gt;
    [/attacks]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category: WML Reference]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=66595</id>
		<title>Micro AIs</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=66595"/>
		<updated>2021-02-07T01:30:59Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Custom Micro AIs */ add link to MAI definition examples&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== A Few General Words about Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
* Micro AIs allow Wesnoth campaign and scenario authors to add new AI functionality without the need for any AI programming. They bring new specialized behavior to Wesnoth that cannot be achieved, at least not easily, by adjusting default AI parameters (such as those described at [[AiWML]]) or with WML code.&lt;br /&gt;
&lt;br /&gt;
* A Micro AI is activated and configured (or deleted) via the [micro_ai] tag, requiring only a few lines of WML code.&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.6''', the Micro AI behavior was inconsistent when it came to '''hidden enemies and petrified units'''.  Some Micro AIs ignored those, while others did not.  In some case there was even a potential for AI errors when an AI unit ran into hidden or petrified enemies.  As of 1.13.6, this has all been fixed.  The Micro AIs now correctly and consistently ignore hidden units and treat petrified units as blocking the hex they are on but having no effect otherwise, in the same way as the default AI does.  '''Important notes:'''&lt;br /&gt;
** It is not possible to re-include hidden/petrified units via the Standard Unit Filters (SUFs) used by some of the Micro AIs. In fact, if this is attempted, the filters will generally not match any unit.&lt;br /&gt;
*** This only applies to SUFs which go directly into the [micro_ai] tag. Any SUF that is a sub-tag of another filter (for example, of a Standard Location Filter) is taken literally and returns all units matching the filter. If hidden or petrified units are to be excluded from such a sub-filter, that needs to be specified directly in that filter.&lt;br /&gt;
** We also note that, because all the Micro AIs are written in Lua, it is possible to [[Micro_AIs#Using_Development_.281.13.29_Version_Micro_AIs_in_your_Stable_.281.12.29_Version_Add-on|move a Micro AI's code into one's add-on]] and modify it to whatever scenario-specific behavior is desired (this does not only apply to hidden/petrified units, but to all behavior). Setting up general Micro AI behavior that works in every possible setting is not feasible, but adapting it to work (only) in the specific setting of a given scenario is often quite simple. If you do not know how to do this, please post a message on the Wesnoth forums and we will be happy to help you with it.&lt;br /&gt;
&lt;br /&gt;
* The '''[[AiWML#List_of_AI_Aspects|default AI aspects]] are, for the most part, not taken into account by the Micro AIs'''.  For example, unless noted otherwise, specifying a value for ''aggression'' does not change the MAI behavior, and locations specified in an ''[avoid]'' tag generally can be entered by units controlled by an MAI.  This is a feature of the Micro AIs, not a bug.  If you feel that a certain default AI aspect should also apply to a Micro AI, please let us know in the [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 Micro AIs Feedback thread].&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.7''', the Micro AIs did not all work reliably when the '''AI side is under shroud''' and it may have been necessary to turn shroud off for the side when using a Micro AI. {{DevFeature1.13|7}} The Micro AIs now all work for sides under shroud. They do this by disregarding shroud for terrain purposes when path finding, consistent with default Wesnoth AI behavior, while still ignoring hidden units (including those hidden under shroud) correctly.&lt;br /&gt;
&lt;br /&gt;
* Most Micro AIs in their default setting apply to all units of a side, although many of them provide parameters for filtering the units that should be affected by the new behavior.  If the Micro AI is applied only to part of the units of a side, the other units follow the default Wesnoth AI behavior.&lt;br /&gt;
&lt;br /&gt;
* Micro AIs are available in mainline since Wesnoth 1.11/1.12.&lt;br /&gt;
&lt;br /&gt;
== Micro AI Test and Demo Scenarios ==&lt;br /&gt;
&lt;br /&gt;
All Micro AIs described here can be checked out in a number of test and demo scenarios.  The easiest way to access the test scenarios is by assigning a hotkey to &amp;quot;Start Test Scenario&amp;quot; in the preferences (as of Wesnoth 1.13.8). Then, simply press the hotkey in the titlescreen and select the desired scenario from the list (e.g. 'micro_ai_test' for the overall &amp;quot;switchboard scenario&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Test scenarios can also be accessed from the command line by typing&lt;br /&gt;
 path/wesnoth-executable -t micro_ai_test&lt;br /&gt;
or (depending on your system and Wesnoth version)&lt;br /&gt;
 path/wesnoth-executable -tmicro_ai_test&lt;br /&gt;
This starts up a &amp;quot;switchboard scenario&amp;quot; in which you can select the Micro AI demonstration scenario you want to check out.  Here, ''path/wesnoth-executable'' needs to be replaced by whatever the path and filename of the Wesnoth executable is on your system.&lt;br /&gt;
&lt;br /&gt;
== Setting up a Micro AI ==&lt;br /&gt;
&lt;br /&gt;
Setting up a Micro AI requires only one simple step:&lt;br /&gt;
==== Activating and configuring (or deleting) a Micro AI ====&lt;br /&gt;
&lt;br /&gt;
Micro AIs are activated, deleted and configured using the [micro_ai] tag.  This tag needs to be placed in [[ActionWML]], that is, in an event, a menu option or the like.  As an example, the following code activates the healer_support Micro AI in its default configuration for Side 2 from the beginning of the scenario:&lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
 &lt;br /&gt;
         # Configure the healer support micro AI&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
'''Keys required for all Micro AIs:'''&lt;br /&gt;
All [micro_ai] tags must contain the following three required keys:&lt;br /&gt;
* '''side''': The side for which the Micro AI is to be added, changed or deleted.  This has to be an individual side number, lists of sides are not supported.&lt;br /&gt;
* '''ai_type''': The type of Micro AI to be added, changed or deleted.  See the following sections for allowed values.&lt;br /&gt;
* '''action''' (string): The action to take concerning the Micro AI.  The following values are allowed: &lt;br /&gt;
** ''add'': Add a Micro AI to a side.&lt;br /&gt;
** ''change'': Change the configuration of an existing Micro AI.  Note that this does not only change the specific parameters provided in the tag, but it replaces the entire existing Micro AI by a new version with the new (and only the new) configuration.  It is therefore equivalent to using first the ''delete'' and then the ''add'' action.&lt;br /&gt;
** ''delete'': Delete an existing Micro AI from a side.&lt;br /&gt;
&lt;br /&gt;
'''Keys required under certain circumstances:'''&lt;br /&gt;
* '''ca_id''': (string) If several MAIs of the same type are used for the same side and one or several of these are to be changed or deleted during a scenario, this key needs to be included so that the [micro_ai] tag knows which of them needs to be changed/deleted.  The same is also true if a Micro AI is changed more than once during a scenario.  We therefore '''strongly recommend always setting ''ca_id''''' for MAIs which are, or might be, changed or deleted during a scenario.&lt;br /&gt;
** '''Note:''' The id(s)/name(s) actually assigned to the CA(s) of the MAI by the engine might be different from the parameter passed here.  It is, however, sufficient to use this ca_id with the [micro_ai] tag add/delete/change actions to identify the MAI uniquely.  (In other words: you don't have to worry about this if you always use the [micro_ai] tag, but might have to figure out the actual name(s) of the CA(s) if you want to delete or change them manually.)&lt;br /&gt;
* Other keys may also be required depending on the Micro AI.  In additional, a number of optional keys may be available to configure the AI behavior.  See the sections on the individual Micro AIs for details.&lt;br /&gt;
&lt;br /&gt;
'''Required keys for deleting a Micro AI:'''&lt;br /&gt;
&lt;br /&gt;
Only a subset of the keys required for setting up a Micro AI are needed when deleting it.  '''side''', '''ai_type''' and '''action'''=delete always need to be provided.  '''ca_id''' needs to be given if it was provided when adding the Micro AI (see above for when this is needed).&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* The ''add'' and ''change'' actions ignore all keys that do not apply to the respective Micro AI type.  The ''delete'' action ignores all keys other than the required keys listed above.&lt;br /&gt;
&lt;br /&gt;
* Some of the Micro AIs use and/or modify default AI components (candidate actions or aspects) when they are added, or reset these components to their default values when they are deleted.  Thus, if you have modified the AI yourself using the default CAs or aspects, some of these modifications might be affected, or might be interfering with the Micro AI.  The sections below indicate whether a Micro AI modifies any default AI components.&lt;br /&gt;
&lt;br /&gt;
===Combining Different Micro AI Types for the Same Side===&lt;br /&gt;
&lt;br /&gt;
No extra steps need to be taken when combining different Micro AIs on the same side.  Simply set up the [micro_ai] tags for all AIs you want to use.&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* While it is ''technically'' possible to combine any number of Micro AIs, this does not mean that all Micro AIs work together in practice.  Some are not compatible because they control units in contradictory ways.  &lt;br /&gt;
* Not all combinations that ''should'' work have been tested yet.  If you find a combination that you believe should work but doesn't, [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 please let us know] and we will see if that can be fixed.&lt;br /&gt;
* The candidate action score almost certainly has to be set (using the ca_score= key) for one or several of the MAIs if several of them are used on the same side.&lt;br /&gt;
&lt;br /&gt;
== Animals Micro AIs ==&lt;br /&gt;
=== General Comments on the Animal Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Animals Micro AIs are meant to simulate the behaviors of certain types of animals.  They are, however, set up so that they can be used with arbitrary unit types and might be applicable in quite diverse situations.  The AIs nevertheless remain named after the animals for which they were written originally, so that they are (to some extent) descriptive of their behavior.&lt;br /&gt;
&lt;br /&gt;
Brief descriptions of the AI behaviors are given in the following subsections.  To get a better feeling for their behavior, check out the &amp;quot;Animals&amp;quot;, &amp;quot;Swarm&amp;quot;, &amp;quot;Wolves&amp;quot; and &amp;quot;Dragon&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  Note that some of the animal AIs are designed for animal types that do not exist in mainline.  In the test scenario, other animal types are therefore substituted for them.&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Wolves (ai_type=wolves) ===&lt;br /&gt;
&lt;br /&gt;
The Wolves Micro AI organizes all &amp;quot;wolf&amp;quot; units (predators) of the side to hunt in a single pack.  They actively chase after the closest prey and try to corner it (not always super successfully), but are easily distracted by other enemy units coming into attack range, to the point where the pack might split up.  The wolves can, however, be told to attack only prey units as well as to try to avoid other types of units (such as larger predators).  When no prey is left, the wolves wander randomly.&lt;br /&gt;
&lt;br /&gt;
This AI works best for a small number of predators (2 - 5) and sparse prey.  Also, the predators should be put on the map in a pack, that is, reasonably close to each other.  For a demonstration of the Wolves AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Note that another wolves AI exists, called [[Micro_AIs#Animals_AI:_Multi-pack_Wolves_.28ai_type.3Dwolves_multipacks.29|Multipack Wolves]], which distributes the wolves of a side into several packs, rather than all of them into one single pack as it is done here.&lt;br /&gt;
&lt;br /&gt;
Enable the Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the predator units.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the prey units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_only_prey'''=no: In the default configuration, the wolves attack all enemy units in attack range.  If this key is set to 'yes', they only attack prey units.&lt;br /&gt;
* '''avoid_type''': Comma-separated list of unit types (on enemy sides) which the wolves try to avoid.  They try to stay out of these units' way as much as possible when moving around (except when they are moving in to attack another enemy unit) and never attack them.&lt;br /&gt;
* '''ca_score'''=90000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 90,000, which is lower than the default combat CA, but higher than most other default CAs which move units.  It can be changed by setting this parameter but in most cases this will not make sense.  Note that the Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Notes on default (RCA) AI aspects:''' &lt;br /&gt;
* If ''attack_only_prey'' or ''avoid_type'' are set, the Wolves Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* Attacks by the wolves are left to the default (RCA) AI, meaning that the value of ''aggression'' has an influence on the behavior.&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI is taken into account. Note: as attacks by the wolves are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=6&lt;br /&gt;
             ai_type=wolves&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Wolf&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Vampire Bat&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             avoid_type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Multi-pack Wolves (ai_type=wolves_multipacks) ===&lt;br /&gt;
&lt;br /&gt;
The Multi-pack Wolves Micro AI is different from the [[Micro_AIs#Animals_AI:_Wolves_.28ai_type.3Dwolves.29|Wolves Micro AI]] in that there can be an arbitrary number of wolf packs, with the pack size being a free parameter.  At the beginning of the scenario, close wolves are grouped into packs in a semi-methodical way.  If the wolves are put on the map in distinct groups of close wolves of the correct number, they will be joined into packs matching these groups.  If, on the other hand, they are spread out all over the map, the method of assigning them to packs is semi-random.&lt;br /&gt;
&lt;br /&gt;
Wolves of the same pack begin by joining each other on the map.  After that, they stay together until only one wolf is left, which then tries to join up with an incomplete pack or with other single wolves.  Individual wolves entering the map during the scenario behave in that way as well.&lt;br /&gt;
&lt;br /&gt;
A second difference to the other Wolves AI is that wolves do not actively hunt here.  For the most part they just wander (often long distance).  However, the pack ferociously (and without regard for its own health) attacks any enemy units that come into range, as long as that does not mean separating the pack by more than a few hexes.  Staying together, or joining with a new wolf assigned to the pack, is the only thing that takes priority over satisfying the wolves' thirst for blood.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Multi-pack Wolves AI behavior, check out the &amp;quot;Wolves&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Multi-pack Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves_multipacks&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Multi-pack Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''pack_size'''=3: (integer) The size of the packs.&lt;br /&gt;
* '''show_pack_number'''=no: If set to &amp;quot;yes&amp;quot;, the wolves' pack numbers will be shown below them.  Note that this means that the pack number of a wolf killed in an attack will remain as a label on the map.  &lt;br /&gt;
* '''type'''=Wolf: Comma-separated list of the unit types which the AI controls. The default value is &amp;quot;Wolf&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves_multipacks [micro_ai] tag usage from the &amp;quot;Wolves&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=wolves_multipacks&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             show_pack_number=yes&lt;br /&gt;
             pack_size=4&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Big Animals (ai_type=big_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Big Animals Micro AI is a simulation of large predators that wander and hunt alone, such as Bears, Giant Spiders or Yetis.  For the most part, these just wander on the terrain that's been defined for them, but they attack enemy units if those happen to come into range.  The AI can be set up so that the Big Animals stay out of the way of other units (such as other large predators), in which case they only attack those if cornered or if they run into them accidentally (such as ambushers).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Big Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Big Animals Micro AI by using&lt;br /&gt;
 ai_type=big_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the animals to which this AI is applied.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': The big animals often go for long distance (multi-turn) journeys.  This tag is a [[StandardLocationFilter|Standard Location Filter]] describing the terrain from which the next goal is chosen.  The default is all terrain on the map. &lt;br /&gt;
* '''[filter_location_wander]''': The terrain on which the big animals will end their moves. This is a [[StandardLocationFilter|Standard Location Filter]].  The default is all terrain on the map.&lt;br /&gt;
* '''[avoid_unit]''': A [[StandardUnitFilter|Standard Unit Filter]] describing all enemy units that the big animals will avoid.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
Here's an example of big_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=big_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Ghast&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [avoid_unit]&lt;br /&gt;
                 type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
             [/avoid_unit]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=1-40,1-18&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Forest Animals (ai_type=forest_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Forest Animals Micro AI simulates a number of different animal behaviors that one might find in a forest, namely animals behaving like deer, rabbits and families of tuskers.  In the following, we will refer to those animals by these types, even though the actual unit types can be selected freely.  They behave as follows:&lt;br /&gt;
* Each deer wanders randomly on (selectable) terrain, except when enemies get in its (the deer's) maximum movement range, in which case it flees to the farthest point it can reach.&lt;br /&gt;
* Tuskers exhibit the same behavior as deer, except when an enemy is next to one of the tusklets.  This enemy is attacked unconditionally by the tuskers trying to protect their young.&lt;br /&gt;
* Tusklets blindly follow the closest adult tusker, except when there is no tusker left, in which case they behave the same as deer.&lt;br /&gt;
* Rabbits also wander randomly, but in addition disappear into their holes (if any are within reach) when enemies are close.  They reappear out of their holes at the beginning of the turn, if it is safe.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Forest Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Forest Animals Micro AI by using&lt;br /&gt;
 ai_type=forest_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Forest Animals AI consists of 4 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-3&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''rabbit_type, tusker_type, tusklet_type, deer_type''': Comma-separated lists of the unit types behaving as the respective animals.  If any of these parameters are not set, those types of animal behavior are skipped by the AI.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain on which the forest animals wander when they are not threatened by enemies.&lt;br /&gt;
* '''rabbit_number'''=6: (integer)  The number of rabbits for the AI to have on the map simultaneously.  The AI puts the missing number of rabbits onto the map at the beginning of the AI's turn (when the rabbits come out of their holes).&lt;br /&gt;
* '''rabbit_enemy_distance'''=3: (number)  Rabbits won't spawn in holes that aren't more than this distance away from all enemies.&lt;br /&gt;
* '''rabbit_hole_img''': Rabbit holes are marked by items on the map.  If this parameter is set, it must be the file name of the item image.  If it is not set, all items on the map are treated as rabbit holes.&lt;br /&gt;
** {{DevFeature1.14|10}} Items used for rabbit holes on the map border used to produce an error. Placing them on the map border therefore needed to be avoided on the scenario setup side. They are now automatically excluded from consideration by the AI code.&lt;br /&gt;
&lt;br /&gt;
Here's an example of forest_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=forest_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             deer_type=Vampire Bat&lt;br /&gt;
             rabbit_type=Giant Rat&lt;br /&gt;
             tusker_type=Ogre&lt;br /&gt;
             tusklet_type=Young Ogre&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=*^F*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Herd and Herders (ai_type=herding) ===&lt;br /&gt;
&lt;br /&gt;
The Herding Micro AI sets up an area on the map where a number of herder units (such as sheep dog) control and protect a herd (such as sheep).  For convenience, we simply refer to them as sheep dogs and sheep in the following, even though arbitrary unit types can be selected for either.&lt;br /&gt;
&lt;br /&gt;
Sheep dogs try to keep their sheep safe.  This involves keeping them inside the herding area, positioning themselves in between the sheep and approaching enemies, and attacking the enemies if those get too close.  If no active herding or protecting move is needed, the dogs go to a random location on the perimeter of the herding area.&lt;br /&gt;
&lt;br /&gt;
Sheep wander aimlessly except when a sheep dog is next to them, in which case they run away from the dog.  The dogs exploit this by positioning themselves on the outside of the sheep, if possible.  Sheep also run away from approaching enemies.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Herding AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Herding Micro AI by using&lt;br /&gt;
 ai_type=herding&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herder units (e.g. dogs).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herd units (e.g. sheep).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain forming the perimeter of the herding area.  In other words, this is the path the herders (dogs) use to patrol around the outside of the herding area.  This must be a set of coordinates ''completely enclosing'' the herding area.  Here's an example of setting up a rectangular perimeter:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         x=10-20,10-20,10,  ,20&lt;br /&gt;
         y=11,   21,   11-21,11-21&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
Another possibility is by defining the path by certain terrain on the map (as it is done in the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]), in which case the filter can simply look like this:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         terrain=Rb&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
* '''herd_x,herd_y''': The coordinate of one hex (''any'' one hex) inside the herding area.  This is needed to define which part of the map is on the ''inside'' of the herding area perimeter defined by [herding_perimeter], and which is the ''outside''. {{DevFeature1.15|0}} '''herd_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''attention_distance'''=8: (integer)  If an enemy is within this distance of a herd unit, but not as close as ''attack_distance'', the herders try to position themselves in between the enemy and the herd unit.&lt;br /&gt;
* '''attack_distance'''=4: (integer)  If an enemy is within this distance of a herd animal, the herders attack this enemy.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Herding AI consists of 6 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-5&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Herding [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=herding&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Footpad&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Troll Whelp&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             herd_x,herd_y=32,28&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=Rb&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Hunter (ai_type=hunter) ===&lt;br /&gt;
&lt;br /&gt;
The Hunter Micro AI leads a unit (or units) through hunt-and-rest cycles.  The units wander in a prescribed area of the map, their hunting ground, until an enemy unit comes within range.  Note that the units do not actively chase down enemies, but wander randomly through the hunting ground until an enemy is encountered.&lt;br /&gt;
&lt;br /&gt;
If enemies are within range of a hunter unit, the hunter attacks the weakest of them.  If a kill is made, it then retreats to its rest location, where it stays for a certain number of turns and until fully healed (whatever happens later).&lt;br /&gt;
&lt;br /&gt;
A few more details:&lt;br /&gt;
* If the hunter's way home is entirely blocked on the return (so that there is no possible path), the normal RCA AI takes over its behavior.&lt;br /&gt;
* However, if the way is blocked by an enemy unit occupying the rest hex, that enemy will be attacked unconditionally.&lt;br /&gt;
* A kill only makes the hunter go home when it is the attacker, not as defender.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Hunter AI behavior, check out the &amp;quot;Dragon&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]. &lt;br /&gt;
&lt;br /&gt;
Enable the Hunter Micro AI by using&lt;br /&gt;
 ai_type=hunter&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''home_x,home_y''': The coordinates of the hex to which the hunter returns and rests at after a kill. {{DevFeature1.15|0}} '''home_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the unit's hunting ground.  If this tag is not provided, the hunter uses all of the map.&lt;br /&gt;
* '''rest_turns'''=3: (integer)  The number of turns the AI stays at home_x,home_y after a kill.  It will not leave its home until this number of turns have passed ''and'' it is fully healed.&lt;br /&gt;
* '''show_messages'''=no: (boolean) If set to yes, the hunter announces whenever its behavior changes to the next phase.  Note that this is meant as debug output for the scenario author.  The messages are not translated and are not included in replays (and are only visible to the host in an MP game).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a hunter [micro_ai] tag usage from the &amp;quot;Dragon&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hunter&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Rowck&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=5-30,1-15&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             home_x,home_y=3,15&lt;br /&gt;
             rest_turns=2&lt;br /&gt;
 &lt;br /&gt;
             show_messages=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Swarm (ai_type=swarm) ===&lt;br /&gt;
&lt;br /&gt;
The Swarm Micro AI uses some very simply algorithms to simulate animal swarm behavior.  Without adjacent enemies, they simply do a random move, trying to stay together and at a certain distance from enemies.  However, if an enemy unit is close to any bat, the swarm scatters.  This is particular fun to watch when one places an enemy unit in the middle of the swarm.  After being scattered, the swarm members slowly rejoin, but not in a very organized way.  Sub-swarms or individual bats might roam around for quite some time before they find their way back.  It is also possible that individual bats (or small groups) split off from the larger swarm at times.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Swarm AI behavior, check out the &amp;quot;Swarm&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Swarm Micro AI by using&lt;br /&gt;
 ai_type=swarm&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Swarm AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_distance'''=5: (int) The minimum distance kept between units of the swarm and enemies when the swarm moves as a whole.&lt;br /&gt;
* '''scatter_distance'''=3: (int) Enemies within &amp;quot;scatter_distance&amp;quot; hexes of any swarm unit cause the swarm to scatter, by each unit trying to maximize its individual distance from the enemy.&lt;br /&gt;
* '''vision_distance'''=12: (int) Only units within this distance follow the overall swarm motion (either away from an enemy or of the swarm as a whole).  The smaller this value is set, the less likely the swarm is to stay together or rejoin.  This parameter is meant to simulate how far an individual member of the swarm can see, meaning that the swarm is &amp;quot;out of sight&amp;quot; (and the unit will not be able to follow it) if it is more than this distance away.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Swarm [micro_ai] tag usage from the &amp;quot;Swarm&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=swarm&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Assassin Squad Micro AI (ai_type=assassin) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|6}}&lt;br /&gt;
&lt;br /&gt;
This Micro AI controls one or several assassin units which try to circle around enemy units with the goal of hitting a target unit without being distracted or impeded by other enemies.  The units will always attack the target if such attacks are possible, otherwise they will follow the path of least resistance toward it. Notes:&lt;br /&gt;
&lt;br /&gt;
* The main priority for the AI is to find paths that avoid enemy units. However, if no such path can be found, the assassins will still advance toward the target, taking the path exposing the units to the least potential damage from enemies.&lt;br /&gt;
&lt;br /&gt;
* The units will not necessarily follow the same path. The best path is chosen individually for each unit.&lt;br /&gt;
&lt;br /&gt;
* While it is not a requirement for the AI to work, using ambusher or skirmisher units, or units with the quick trait, makes this AI more lethal in many cases.&lt;br /&gt;
&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded, it does not work otherwise. That means that '''ca_score''' is not a valid key for the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=assassin&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Assassin Squad specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the assassin units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the target unit.&lt;br /&gt;
** It is important to realize that the AI currently only works correctly with a single target unit. It is thus the responsibility of the scenario designer to make sure that this filter matches an individual enemy unit. If several enemy units are to be targeted in the same scenario, several instances of the Assassin Micro AI can be used.&lt;br /&gt;
** The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[prefer]''': A [[StandardLocationFilter|Standard Location Filter]] selecting locations on the map for the assassins to prefer when making their way toward the target unit. This could be used, for example, to make units with the ambush ability move (mostly) through forest or to have the assassins circle around the, say, northern part of the map rather than the south if that is desirable for scenario design purposes.&lt;br /&gt;
** As with avoiding enemies, the units will still move across non-preferred terrain if that is either the only option or ''much'' shorter than preferred terrain&lt;br /&gt;
** Attacking, once the target unit is in range, is not influenced by the [prefer] SLF.&lt;br /&gt;
&lt;br /&gt;
Here is an example of an Assassin Squad [micro_ai] tag usage:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=assassin&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Assassin,Master at Arms,Duelist&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             [prefer] # Circle around northern part of map&lt;br /&gt;
                 x=1-9, 10-99&lt;br /&gt;
                 y=1-37, 1-17&lt;br /&gt;
             [/prefer]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Bottleneck Defense Micro AI (ai_type=bottleneck_defense) ==&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense Micro AI lets you define a location on the map where the AI can take a defensive stance behind a narrow passage (bottleneck).  Units on the front line are backed up by healers and/or units with the leadership ability.  Injured units are moved to the back and replaced by uninjured (or less injured) units.  The units on the front line only attack when it is safe (no retaliation) or when there is a high chance that they will make a kill or level up.  Using this Micro AI only makes sense if there is no way for the enemy sides to move around the bottleneck and attack the AI's units from behind.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Bottleneck&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Bottleneck Defense Micro AI by using&lt;br /&gt;
 ai_type=bottleneck_defense&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Bottleneck Defense specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense AI requires two sets of coordinates that define where it should be taking up its defensive stance, and from which side the enemy attacks at that location.&lt;br /&gt;
&lt;br /&gt;
* '''x,y''': Comma separated lists of the hexes on the front line, where strong units are placed.  All hexes on which the AI makes contact with (can be attacked by) the enemy need to be included here.  Units are placed on them in the order in which they are listed here. {{DevFeature1.15|0}} '''location_id''' can be used instead with named/special locations.&lt;br /&gt;
* '''enemy_x,enemy_y''': Comma separated list of the hexes from which the enemy can attack the AI front line units.  This is needed for the AI to know on which side of the front line support units (healers etc.) need to be placed.  In many cases, it is sufficient to provide only one of the enemy front line hexes, but there are geometries for which that does not work.  It is therefore safer to list all enemy front line hexes here. {{DevFeature1.15|0}} '''enemy_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
By default, the AI places healers and units with the leadership ability both on the front line itself (if they are the strongest units available) and on hexes adjacent to and behind the line.  If different placement is desired, these locations can be overridden with the following keys.&lt;br /&gt;
&lt;br /&gt;
* '''active_side_leader'''=no: (boolean)  If set to 'yes', the side leader participates in the bottleneck defense action after the side's gold has been spent.  If set to 'no' (default), the leader follows default AI behavior (sitting on the keep and recruiting, for the most part).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Bottleneck Defense AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.15|3}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the units.  Only AI units matching this filter participate in the bottleneck defense.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''healer_x,healer_y''': Comma separated list of hexes where healers are placed.  This can be used to keep healers from the front line (or to take up only certain positions on the front line), and/or to override the default healing positions behind the line.  Healers are placed on them in the order in which the hexes are listed, with the exception that hexes on the line always take priority over hexes behind the line. {{DevFeature1.15|0}} '''healer_loc''' can be used instead with named/special locations.&lt;br /&gt;
* '''leadership_x,leadership_y''': Same as ''healer_x,healer_y'', but for units with the leadership ability. {{DevFeature1.15|0}} '''leadership_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
As an example, in the &amp;quot;Bottleneck&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]], there are three front line hexes, two of which are on hill terrain, while the third is a road with a lower defense rating.  The healer and side leader are supposed to participate in combat because they are strong units, but only from the hill hexes to keep them safer.  This is accomplished by using the following keys settings (check out the scenario to see which hex is which):&lt;br /&gt;
 [micro_ai]&lt;br /&gt;
     side=1&lt;br /&gt;
     ai_type=bottleneck_defense&lt;br /&gt;
     action=add&lt;br /&gt;
 &lt;br /&gt;
     x=14,14,14&lt;br /&gt;
     y= 7, 9, 8&lt;br /&gt;
     enemy_x=13,13&lt;br /&gt;
     enemy_y= 8, 9&lt;br /&gt;
 &lt;br /&gt;
     healer_x=14,14,15,15&lt;br /&gt;
     healer_y= 7, 9, 8, 9&lt;br /&gt;
     leadership_x=14,14,15,15&lt;br /&gt;
     leadership_y= 7, 9, 9 ,8&lt;br /&gt;
     active_side_leader=yes&lt;br /&gt;
 [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Fast Micro AI (ai_type=fast_ai) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|0}}&lt;br /&gt;
&lt;br /&gt;
This AI is meant as a fall-back AI for scenarios where there are so many units that attacks and moves done by the default AI are slow with long delays before each move.  It replaces the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions (CAs).  It should be obvious that in order to achieve the faster speed, compromises concerning the quality of the moves have to be made.&lt;br /&gt;
&lt;br /&gt;
Enable the Fast Micro AI by using&lt;br /&gt;
 ai_type=fast_ai&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Combat candidate action:'''&lt;br /&gt;
* Attackers are considered individually one at a time.  The best attack found for a unit is executed without checking whether a better attack by another unit might be possible.&lt;br /&gt;
* The default order in which attackers are considered is by decreasing hitpoints, starting with the strongest unit (in terms of HP).  This can be modified with the optional keys below.&lt;br /&gt;
* By default, if weaker units block attack hexes for a stronger unit, they do not move out of the way (this is the same as for the default AI).  If a hex becomes available after an attack (e.g. due to a weaker unit dying in the attack), all attacks that were possible ''before'' this happened are finished first.  Only after that are attacks by the stronger unit reconsidered.&lt;br /&gt;
* If include_occupied_attack_hexes=yes is set, weaker units move out of the way for stronger units if possible.  This can, however slow down the AI depending on the situation on the map.  Use with caution.&lt;br /&gt;
* {{DevFeature1.13|2}} Previous to this version, attacks by the leader were bunched in with the attacks by the other units.  Now leader attacks are treated separately and contain an extra layer of analysis to determine whether the attack position is &amp;quot;safe&amp;quot; (in an approximate way).  Currently, this is done with two simple considerations:&lt;br /&gt;
** Count all enemy units that can reach the leader's attack position.  If there are more than given by optional parameter 'leader_attack_max_units' (default value 3), do not attack.&lt;br /&gt;
** Add up the hitpoints of all enemies that can reach the leader's attack position.  If this value, multiplied by optional parameter 'leader_weight'(default value 2), is larger than the leaders hitpoints, do not attack.&lt;br /&gt;
** Two optional parameters, 'threatened_leader_fights' and 'leader_additional_threat', can also be used to let the leader attack when the threat at the attack hex is comparable to that at the leader's current position.&lt;br /&gt;
** In order to keep this AI as fast as possible, leader attacks are done by a separate candidate action that is only evaluated when all other CAs are done.&lt;br /&gt;
&lt;br /&gt;
'''Move-to-targets, villages and retreat candidate actions:'''&lt;br /&gt;
* The move-to-targets, villages and retreat candidate actions of the default AI are removed.  Move-to-targets and villages are taken over by the Fast Micro AI, whereas retreat is simply not done.&lt;br /&gt;
* This CA moves units toward goals after attacks are done.  Goals are unowned (if the AI side has a leader only) and enemy-owned villages and enemy leaders.&lt;br /&gt;
* For each goal, units are pre-sorted in order of the fewest moves needed to cover the ''distance'' toward the goal (that is, as if move cost on each hex toward the goal were 1).  Units are then considered sequentially and if a unit can get there &amp;quot;efficiently&amp;quot;, its move is executed without considering other units.&lt;br /&gt;
** A move is considered efficient if the move cost to get to the hex is not more than 'move_cost_factor' larger than the distance to the hex.  'move_cost_factor' is an optional key that defaults to 2.  It can be set to values of 1.1 or larger (see below).&lt;br /&gt;
** If the move for the current unit is not found to be efficient, the AI considers the next unit instead.  If no unit can do an efficient move toward the goal, the best (fastest) of the inefficient moves is selected instead.&lt;br /&gt;
&lt;br /&gt;
* Village goals:&lt;br /&gt;
** Village goals are considered before enemy leader goals so that the AI preferentially chooses scouts (or otherwise fast units) for them.&lt;br /&gt;
** The number of units sent toward villages depends on the value of the default AI aspect village_value.  It is set to village_value/4 times the number of units the AI has left to move.  Thus, by default (village_value=1.0) the AI sends a quarter of its units toward villages, and the rest toward enemy leaders.  This means that the meaningful range of values for village_value is 0..4.&lt;br /&gt;
** Only one unit is moved toward each village.  This consideration also overrides the number of units to move toward villages defined by village_value, topping it at the number of unowned/enemy-owned villages on the map.&lt;br /&gt;
** Setting village_value=0 disables moving units toward villages altogether.  That is, not only are no units chosen, but the village targeting phase of the AI is entirely disabled with no evaluation time used for it.  Note that, unlike with the default AI, this also means that no villages within range of an AI unit are grabbed.&lt;br /&gt;
** The order in which villages are chosen as goals is some combination of them being close to the AI side's leader and being spread out over the map.&lt;br /&gt;
&lt;br /&gt;
* Enemy leader goals:&lt;br /&gt;
** The order of enemy leaders goals is set according to their distance from the AI leader with the closest enemy leader being targeted first (if the AI has a leader, otherwise their order is random).  One unit is sent toward each enemy leader in this order, after which the AI starts with the first enemy leader again.  This is repeated until all units have moved.&lt;br /&gt;
&lt;br /&gt;
* Guardians (units with ai_special=guardian set) are ignored by the fast move-to-targets CA.&lt;br /&gt;
&lt;br /&gt;
'''Tips for speeding up/modifying the Fast Micro AI in specific scenarios:'''&lt;br /&gt;
&lt;br /&gt;
A number of optional keys and default AI aspects can be used to modify the behavior of the Fast Micro AI.  The MAI is set up so that it is a reasonable compromise between speed and quality of the moves in most situations.  However, this might not be ideal for all scenarios here are some things that can be done to speed up the AI (or to get better behavior in some situations by doing the opposite):&lt;br /&gt;
* Use large values for the default AI aspect aggression.  The largest delays before attacks happen (in some circumstances) when the Fast MAI has to consider lots of units with attacks left but decides not to attack with them.  See below for a description of how aggression is used by the fast AI.&lt;br /&gt;
* Use large values of move_cost_factor (see above for description of what this does).  If you want to emphasize units that can move across terrain easily (e.g. bats) to be chosen for village grabbing, you might want to use low values of move_cost_factor, but this might slow down the AI.  The default value should work reasonably well in most standard situations, but modifying the value might work better on specific maps.&lt;br /&gt;
* Don't use dungeon_mode=yes unless absolutely necessary, even in most dungeon crawls.  Test the AI without this setting first and only use it if it is the only way to solve the problem.  Often, a minor tweak to the map will have the same effect, without the speed penalty.&lt;br /&gt;
* Use include_occupied_attack_hexes=yes with caution.  It might (or might not) slow down the attack CA significantly, depending on the scenario.&lt;br /&gt;
* Set village_value=0, which makes the AI not consider villages as goals.  This has not been found to make a large difference on the maps tested so far, but it might have an effect on maps with a very large number of villages.&lt;br /&gt;
* Use attack_hidden_enemies=yes on maps where there are no hidden enemies. Excluding hidden enemies from the attack calculation requires an additional filter to be evaluated that takes extra time. In most cases this will not result in a large computation time saving.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_hidden_enemies'''=no: (boolean)  By default, hidden enemies are excluded from the attack calculation.  Setting this to 'yes' makes the AI also consider those enemies as attack targets.&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.13|2}} A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  As with the default AI, units will not step on hexes defined by the [avoid] tag.  In addition, no goals will be chosen that lie on those hexes.  The one minor exception is that a unit stepping out of the way for another unit might temporarily enter the avoided zone.&lt;br /&gt;
* '''dungeon_mode'''=no: (boolean)  As much as possible, the Fast Micro AI works with distances between hexes rather than path finding.  This works pretty well on open maps and even most dungeon crawls (and similar scenarios), but it might result in some units getting temporarily (and in rare cases permanently) stuck on maps with convoluted labyrinths.  If that is the case, setting dungeon_mode=yes triggers more path finding that avoids such situations (but as a result is somewhat slower).&lt;br /&gt;
* '''include_occupied_attack_hexes'''=no: (boolean) If set to 'yes', the AI moves weaker units out of the way for a stronger attacker.  This might lead to better attacks but is slower, so don't use if speed is essential.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the attackers.  Only AI units matching this filter will attack, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes the side= key set to the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the enemies.  Only enemy units matching this filter will be attacked, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''leader_weight'''=2: (number &amp;gt;= 0) {{DevFeature1.13|2}} A factor specifying how much more valuable the AI considers side leaders than non-leader units in the attack evaluation. This is also used to determine whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''leader_additional_threat'''=1: (number &amp;gt; 0) {{DevFeature1.13|2}} If threatened_leader_fights=yes, the threat at the attack position may be larger by this factor for the leader to leave the keep for an attack.&lt;br /&gt;
* '''leader_attack_max_units'''=3: (number &amp;gt;= 0) {{DevFeature1.13|2}} A number specifying how many enemy units within reach are acceptable when determining whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''move_cost_factor'''=2: (number &amp;gt;= 1.1)  The factor by which the move cost to a goal hex may be larger than the distance to the goal hex for a move to be considered &amp;quot;efficient&amp;quot;.  See description above.  1.1 is used if this is set to values smaller than 1.1.  Smaller values might lead to better behavior, while larger values might speed up the AI.&lt;br /&gt;
* '''weak_units_first'''=no: (boolean) If set to 'yes', inverts the order in which units are considered by the combat CA.  Attacks are then done in order of increasing hitpoints of the attackers.&lt;br /&gt;
* '''skip_combat_ca'''=no: (boolean) If set to 'yes', the fast combat CA is not used.  The default AI's high_xp_attack and combat CAs remains in place in this case.&lt;br /&gt;
* '''skip_move_ca'''=no: (boolean) If set to 'yes', the fast move-to-targets CA is not used.  The default AI's retreat, villages and move-to-targets CAs remains in place in this case.&lt;br /&gt;
* '''threatened_leader_fights'''=no: (boolean) {{DevFeature1.13|2}} By default, the AI leader will not attack if the threat at the attack position is too high.  If this key is set to 'yes', the leader will attack if the threat there is not larger than the threat at his current position multiplied by the value of 'leader_additional_threat'. This is meant as an option for the endgame: when the AI leader is under attack, he might as well go down fighting.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''aggression''': The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to set how aggressive the AI should attack.  It works in roughly the same way as the default AI aspect:&lt;br /&gt;
** The value of (1 - aggression) determines how much the AI values its own units compared to the enemy's units.  Thus, aggression=0 means that attacks are only done if the AI expects to deal (roughly) the same amount of damage as it will receive.  aggression=1 means that expected damage to the AI's units is not considered, only the expected damage to enemy units matters.&lt;br /&gt;
** Damage is calculated as a combination of hitpoints and chance to die, in &amp;quot;gold equivalent units&amp;quot;.&lt;br /&gt;
** Unlike the default AI, aggression is set to 1 for values larger than 1.  Allowed values are thus anything from -infinity to +infinity, although only -infinity to +1.0 make sense.&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.13|2}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
*'''attacks aspect''':  {{DevFeature1.13|2}} The standard RCA AI [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] used to filter own and enemy units for attacks is now also taken into account as long as neither [filter] nor [filter_second] (see above under 'optional keys') are defined in the [micro_ai] tag.&lt;br /&gt;
*'''village_value=1''': (number ranging from 0 to 4) The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to define how much villages should be targeted by the move-to-targets CA.  In the Fast MAI, this defines which fraction of the AI units are moved toward village goals (see above for details).  If set to 0, villages are ignored as goals.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded to the same values as those of the respective [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|default candidate actions]].  ca_score is therefore not a valid optional key.&lt;br /&gt;
* The fast Micro AI modifies (removes) the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions.&lt;br /&gt;
&lt;br /&gt;
Enabling the Fast Micro AI in its default configuration is as simple as this usage example from the &amp;quot;Fast AI&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=fast_ai&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Generalized Goto Micro AI (ai_type=goto) ==&lt;br /&gt;
&lt;br /&gt;
The Goto Micro AI provides a highly configurable method of sending a unit (or units) to a location or set of locations.  The units to be moved are defined using a [[StandardUnitFilter|Standard Unit Filter]] (SUF) [alternatively, all side units are used if the SUF is not provided), while the goto locations are given in a [[StandardLocationFilter|Standard Location Filter]] (SLF).&lt;br /&gt;
&lt;br /&gt;
By default, each unit matching the SUF will move toward the closest goal (in terms of turns needed to get there) matching the SLF via the fastest route given the current situation on the map (locations of enemy units etc.), but this behavior can be influenced by the optional keys listed below.  Note that this means that a unit's goal might change from one turn to the next depending on the situation on the map.&lt;br /&gt;
&lt;br /&gt;
The behavior of the unit(s) once they reach their goal location(s) is configurable as well.  By default, they will stay there until the Goto Micro AI is deleted or changed.&lt;br /&gt;
&lt;br /&gt;
For demonstrations of several Goto Micro AI usages, check out the &amp;quot;Goto&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Goto Micro AI by using&lt;br /&gt;
 ai_type=goto&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Goto specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the possible goal locations for the move.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists). Note that the area defined by [avoid] is not entered at all, even if the end point of the move would lie outside the area again.&lt;br /&gt;
* '''avoid_enemies''': (number&amp;gt;0) By default, units controlled by the Goto Micro AI will not take enemy units into account for route finding, other than the enemies' ZoCs.  By contrast, if this key is set, they will try to avoid traversing hexes on which they can be attacked by enemies.  The larger the value of ''avoid_enemies'', the more the units will try to stay out of the enemies' way.  Notes:&lt;br /&gt;
** Units are willing to take a path that is 'avoid_enemies' movement points longer for each enemy unit that can be avoided by doing so for each hex along the path.  As an example, if avoid_enemies=1 and the shortest path crosses 2 hexes that can be reached by 3 enemies each, the unit is willing to take a path that is up to 6 hexes longer instead.  If avoid_enemies=10 in the same situation, the unit will take a path up to 60 hexes longer.&lt;br /&gt;
** Hexes adjacent to enemy units are always strongly avoided, independent of the value of avoid_enemies.&lt;br /&gt;
** avoid_enemies is ignored if 'use_straight_line=true' is set.&lt;br /&gt;
** Due to an issue with the path finding code, '''tunnels and teleports are ignored in Wesnoth 1.14 and before''' if 'avoid_enemies' is used. This has been fixed in Wesnoth 1.15.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units to be moved.  Defaults to all units of the side if not defined.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''ignore_enemy_at_goal'''=no and '''ignore_units'''=no: (boolean)  In the default configuration, the Goto MAI does not move units if the path to all goal hexes is blocked by enemies.  It is therefore possible to disable the Goto MAI by putting enemy units on all the goal hexes.  That can be changed if either of these two keys is set to 'yes'.&lt;br /&gt;
** 'ignore_enemy_at_goal=yes' makes the AI choose its goals while disregarding any enemy units at the goal hexes.  Even so, a stiff rating penalty is applied to goal hexes occupied by enemies, thus that unoccupied goal hexes are chosen unless all possible goals are occupied by enemies.  Note that this setting ''only'' ignores enemy units at the goal hexes themselves.  It is still possible to disable the AI by putting enemies all around the goals.&lt;br /&gt;
** 'ignore_units=yes' let's the AI ignore ''all'' enemy units when deciding which goal hex(es) to move toward.&lt;br /&gt;
** In both cases, enemies are only ignored for deciding which unit to move toward which goal hex.  For the actual move itself, all enemies and their ZoCs are considered as usual.&lt;br /&gt;
** Both options only take effect if neither avoid_enemies nor use_straight_line is set to 'yes', as neither of these consider enemies as blockers.&lt;br /&gt;
* '''release_unit_at_goal'''=no: (boolean) By default, a unit that reaches a goal location will stay there until the Goto Micro AI is deleted or changed (or until the conditions specified in the [goto_goals] SLF change).  If this parameter is set, a unit that has reached its goal will not be controlled by this Micro AI any more starting from the next turn.&lt;br /&gt;
* '''release_all_units_at_goal'''=no: (boolean) Same as ''release_unit_at_goal'', but this will deactivate the Goto Micro AI for all units once the first unit reaches a goal.&lt;br /&gt;
* '''unique_goals'''=no: (boolean)  If set to 'yes', each unit will be sent to a different goal location.  Note that the best goals are recalculated each turn, so a unit might choose a different goal this turn from what it was heading for during the previous turn if the situation on the map has changed.  Also note that if there are more units than goal locations, the left over units will not be handled by the Goto MAI but follow default behavior instead.&lt;br /&gt;
* '''use_straight_line'''=no: (boolean)  By default, units choose the route by which they will get to their goal locations in the smallest number of turns.  If this parameter is set to 'yes', a straight line route (or something pretty close to it at least) will be chosen instead, irrespective of whether this leads into a dead end, whether it is blocked by enemies, etc.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* Depending on the parameters used, the Goto Micro AI might require a lot of path finding to be done.  Thus, while it is possible to set up this AI to move many units to many different locations, a usage like that might require quite a long calculation time.&lt;br /&gt;
&lt;br /&gt;
===Goto Micro AI example usages:===&lt;br /&gt;
&lt;br /&gt;
Here are several examples of goto [micro_ai] tag usages from the &amp;quot;Goto&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
* Send a unit toward a castle (similar to the RCA AI's Goto Candidate Action, but avoiding dead ends and areas ZoCed by enemies)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
             ca_id=messenger1&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=messenger1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=6-7,2-4&lt;br /&gt;
                 terrain=C*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             ca_score=210001&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send all units of the side toward the southern border of the map&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=8&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                  y=33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             avoid_enemies=1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send units to the four corners of the map (with each unit going to a different corner).&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x=1,1,44,44&lt;br /&gt;
                 y=1,33,1,33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             unique_goals=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Set up a guardian unit that doesn't move while no enemy unit is within 8 hexes (ironically, the Goto Micro AI can be used to keep units in place, although using one of the Guardian Micro AIs might often be a better idea)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=guard1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 [filter]&lt;br /&gt;
                     id=guard1&lt;br /&gt;
                 [/filter]&lt;br /&gt;
                 [not]&lt;br /&gt;
                     [filter]&lt;br /&gt;
                         side=1&lt;br /&gt;
                     [/filter]&lt;br /&gt;
                     radius=8&lt;br /&gt;
                 [/not]&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Guardian Micro AIs ==&lt;br /&gt;
=== General Comments on the Guardian Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Guardian Micro AIs can be used to set up different kind of guardian behaviors for a unit or a set of units.  Four different types of guardians are available and are described below.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different guardian behaviors, check out the &amp;quot;Guardians&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
=== Return Guardian  (ai_type=return_guardian)===&lt;br /&gt;
&lt;br /&gt;
A 'return guardian' is a variation of the standard Wesnoth guardian. It has an assigned guard position (GP) to which it returns after attacks on approaching enemies:&lt;br /&gt;
* If at GP with no enemy in reach, do nothing.&lt;br /&gt;
* If at GP with an enemy in reach, attack. Note that the attack is done by the default AI, which might result in the guardian not attacking if the enemy is deemed too strong (this can be influenced by setting the default ''aggression'' value to a different value).&lt;br /&gt;
* If not at GP, return there, no matter whether an enemy is in reach or not.&lt;br /&gt;
* If enemies are blocking the way back to GP, do your best to move around them.&lt;br /&gt;
* If the guardian ends up next to an enemy on the way back, attack that enemy after the move.&lt;br /&gt;
&lt;br /&gt;
Enable the Return Guardian Micro AI by using&lt;br /&gt;
 ai_type=return_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''return_x,return_y''': The coordinate of the hex the unit returns to after each attack.  {{DevFeature1.15|0}} '''return_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=100010: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  This AI consist of 2 CAs, one with score &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt; the other with &amp;lt;tt&amp;gt;ca_score-20&amp;lt;/tt&amp;gt;.  The default value is set so that the first CA gets executed right before the default combat CA, and the other right after.  It usually will not make sense to change this value, but it is possible to do so for custom applications.  {{DevFeature1.13|6}}: This is now changed to 100100 and 99900 in order to avoid conflicts with the new high_XP_attacks candidate action.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a return guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=return_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=return1&lt;br /&gt;
             return_x,return_y=20,2&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
===Stationed Guardian (ai_type=stationed_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A 'stationed guardian' is another variation of the standard Wesnoth guardian with a somewhat more complex behavior than that of the 'return guardian'. Two positions are defined for it, a 'station' and a 'guarded location', as well as a distance. The behavior is as follows:&lt;br /&gt;
* If no enemy is within 'distance' of the guard's current position, do nothing.  This is independent of the guardian being at its station or not.&lt;br /&gt;
* Otherwise: If an enemy is within 'distance' of the guard, but not also within the same distance of the guarded location and the station (all of this simultaneously), move the guard in the direction of the station (or keep the guard in place if the station is unreachable).&lt;br /&gt;
* Otherwise:&lt;br /&gt;
** Pick the enemy unit that is closest to the guarded location.&lt;br /&gt;
** If we can reach it, pick the adjacent hex with the highest defense rating and attack from there.&lt;br /&gt;
** If not in reach, move toward this unit.&lt;br /&gt;
&lt;br /&gt;
Enable the Stationed Guardian Micro AI by using&lt;br /&gt;
 ai_type=stationed_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance parameter as explained above&lt;br /&gt;
* '''station_x,station_y''': The x and y position of the hex that serves as the guardians station. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''guard_x, guard_y''': The x and y position of the guarded location.  {{DevFeature1.13|0}}  These are now optional keys, defaulting to station_x,station_y if not provided. {{DevFeature1.15|0}} '''guard_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a stationed guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=stationed_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=stationed1&lt;br /&gt;
             distance=4&lt;br /&gt;
             station_x,station_y=2,14&lt;br /&gt;
             guard_x,guard_y=3,13&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Coward (ai_type=coward) ===&lt;br /&gt;
&lt;br /&gt;
Cowards are units that, like guardians, sit around doing nothing until an enemy comes into range.  Unlike guardians, however, they run away once enemies approach. Applications might be wild animals, unarmed civilians getting in the way of a battle, etc.  The coward macro can be called with two optional locations, 'seek' and 'avoid':&lt;br /&gt;
* If neither is given, the coward retreats to the position farthest away from the approaching enemies.&lt;br /&gt;
* If 'seek' is given, it preferentially goes toward that location (but getting away from enemies takes priority).&lt;br /&gt;
* If 'avoid' is given, it in addition tries to avoid that location (with both maximizing distance from enemies and going toward 'seek' taking priority).&lt;br /&gt;
* Both 'seek' and 'avoid' may consist of only one coordinate ('x' or 'y'), in which case not a single hex, but a line of hexes is sought or avoided.&lt;br /&gt;
&lt;br /&gt;
Enable the Coward Micro AI by using&lt;br /&gt;
 ai_type=coward&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance within which an enemy must be to &amp;quot;scare&amp;quot; the unit&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_if_trapped'''='no':  {{DevFeature1.13|0}}  With the default settings, a coward never attacks.  If this key is set to 'yes', the coward attacks the weakest (lowest hitpoints) enemy next to which it ends up after moving (such as a cornered animal would do, blindly lashing out without regards for its own safety).&lt;br /&gt;
* '''avoid_x, avoid_y''': The x and y coordinate of the hex to avoid. {{DevFeature1.15|0}} '''avoid_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units from which the cowards flee.  If not set, this defaults to all enemy units.  Note, however, that no &amp;quot;enemy of AI side&amp;quot; condition is added if [filter_second] is specified, meaning that it can include allied and/or enemy units as desired (and even units on the same side as the coward).&lt;br /&gt;
* '''seek_x, seek_y''': The x and y coordinate of the hex to seek. {{DevFeature1.15|0}} '''seek_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a coward [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=coward&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=coward3&lt;br /&gt;
             distance=5&lt;br /&gt;
             seek_x,seek_y=24,5&lt;br /&gt;
             avoid_x,avoid_y=24,15&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Zone guardian (ai_type=zone_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A zone guardian is a unit that, as the name says, guards a zone. It moves randomly inside this zone until an enemy enters it (or a separately defined enemy zone, see below). Applications might be the defense of a castle, a nesting area or for protecting an unit.  The zone macro can be called with an optional SLF, [filter_location_enemy]:&lt;br /&gt;
* If not specified, the zone guard attacks any enemy coming inside its guard zone.&lt;br /&gt;
* If [filter_location_enemy] is given, it attacks any enemy entering this zone and once there are no more enemies, it goes back to patrol in its basic zone.&lt;br /&gt;
&lt;br /&gt;
Enable the Zone Guardian Micro AI by using&lt;br /&gt;
 ai_type=zone_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone to guard.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_enemy]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone where enemies have to be attacked.  If not given, it defaults to [filter_location].&lt;br /&gt;
* '''station_x,station_y''': By default, the zone guardian moves randomly within the zone if there is no enemy to attack.  However, if station_x/y is given, it moves to this position instead during turns when it does not have an enemy to attack.  This position does not need to be inside the zone defined above.  Note: obviously it does not make sense to define [filter_location], [filter_location_enemy] and station_x/y all at the same time.  Only [filter_location] and either one of the latter two (or none of them) should be provided for any zone guardian. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a zone guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=zone_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=zone3&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=22-31,4-11  # This is intentionally chosen to extend past the lake&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             station_x,station_y=32,8&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Hang Out Micro AI (ai_type=hang_out) ==&lt;br /&gt;
&lt;br /&gt;
The Hang Out Micro AI instructs units of the AI side to &amp;quot;hang out&amp;quot; around a certain location, or locations, until a condition for mobilizing the side is met.  If none of the optional keys are given, the AI controls all units of the side to hang around the leader while moving off castle tiles to make room for recruiting.&lt;br /&gt;
&lt;br /&gt;
The AI works by moving each unit to its best hex first using the minimum amount of movement points required for this move.  Once all AI units are in position, movement points are set to zero for all of them simultaneously such that they cannot be used by other AI candidate actions.  This is done because the mobilizing condition might be met as result of one of the moves of the Hang Out AI, after which we want units to be able to continue with other AI actions.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Hang Out and Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Hang Out Micro AI by using&lt;br /&gt;
 ai_type=hang_out&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Hang Out specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  In its default configuration, the Hang Out MAI controls all units of a side, keeping them as close to the side leader(s) as possible while moving off castle tiles to make room for recruiting.  For this purpose, the candidate action score defaults to 170,000, which is just below the score of the default recruiting CA.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''': A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  If neither of these two exists, this parameter is set to default to all castle terrain (&amp;lt;tt&amp;gt;terrain=C*,C*^*,*^C*&amp;lt;/tt&amp;gt;) so that units move off the castle to make room for recruiting.  Note that this does not include keep terrain, otherwise the leader would be moved off the keep as well.&lt;br /&gt;
* '''ca_score'''=170000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 170,000, which means that it is executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Hang Out action (&amp;lt;tt&amp;gt;side=&amp;lt;/tt&amp;gt; is always set and does not need to be provided).  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the locations ''near'' which the AI units should hang out.  This does not mean that the units only move onto terrain specified here, but that they minimize the distance to such terrain.  It defaults to the position of the side leader(s).&lt;br /&gt;
* '''[mobilize_condition]''':  Contains ([[ConditionalActionsWML#Condition_Tags|WML Condition Tags]])  Once the condition set up here has been met, the Hang Out MAI is disabled and the side goes over to other AI activity.  The Hang Out MAI remains disabled after this, even if the condition is not met any more later in the scenario.&lt;br /&gt;
* '''mobilize_on_gold_less_than''': (number) Since side gold cannot be checked in a WML condition tag without storing it in a variable first, this key is provided separately.  It disables the Hang Out Micro AI once the side gold goes below the value specified here for the first time.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
* The Hang Out AI still takes effect if no locations to hang out at are found or if all map locations are to be avoided.  In that case, it simply keeps all units at their current locations.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Hang Out Micro AI configuration from the &amp;quot;Hang Out and Messenger&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hang_out&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [avoid]&lt;br /&gt;
                 terrain=C*,H*,M*,A*,S*,*^F*&lt;br /&gt;
             [/avoid]&lt;br /&gt;
             [mobilize_condition]&lt;br /&gt;
                 [have_unit]&lt;br /&gt;
                     side=1&lt;br /&gt;
                     count=7-99&lt;br /&gt;
                 [/have_unit]&lt;br /&gt;
             [/mobilize_condition]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Healer Support Micro AI (ai_type=healer_support) ==&lt;br /&gt;
&lt;br /&gt;
The Healer Support Micro AI configures the healers of a side to stay behind the battle lines and heal injured and/or threatened units rather than participate in combat under all circumstances.  You can set different levels of aggressiveness for the healers.  &lt;br /&gt;
&lt;br /&gt;
'''Notes'''&lt;br /&gt;
* By default, ''all'' healers of the AI side are controlled by this AI.  This includes in particular also the side leader, if the leader is a healer.  If you do not want the leader to be included (and possibly be moved away from the keep etc.) use the [filter] tag with canrecruit=no (see below).&lt;br /&gt;
* It is not always better to use this Micro AI than the default AI, as it takes healers (which can be strong units) away from the pool of units to choose for attacks.&lt;br /&gt;
* {{DevFeature1.15|3}} This Micro AI now takes unit guardian status and the passive_leader aspect into account.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Healers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Healer Support Micro AI by using&lt;br /&gt;
 ai_type=healer_support&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Healer Support specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the healer units.  If set, only units on the AI side passing this filter ''and'' having the healing ability are considered by this Micro AI.&lt;br /&gt;
*'''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting units to receive healing. If set, only units on the AI side passing this filter ''and'' the other healee selection criteria will be selected for healing by this Micro AI.&lt;br /&gt;
* '''aggression'''=1.0: (float) Sets the aggressiveness of the AI.  This parameter is set up as a float to accommodate future functionality, but it currently acts as a boolean: if set to zero, the AI will never let its healers participate in combat, for all other values it allows them to attack after all other units have attacked and if the healers cannot find any more units to support.  The former behavior might be appropriate in scenarios with only one or few valuable healers, while the latter might work better in scenarios with many healers.&lt;br /&gt;
* '''injured_units_only'''=no: (boolean) If set to 'yes', the AI will only move healers next to units that are already injured and skip units that currently have full hitpoints, but might get injured during the next enemy turn.&lt;br /&gt;
* '''max_threats'''=9999: (integer) The maximum allowable number of enemies that can attack a hex in order for it to be considered for a healer.  As an example, setting 'max_threats=0' means that the AI only moves healers to locations that are entirely safe from the enemy (assuming that none of the units currently on the map dies).  Note that the value of this key is checked against the number of enemies that can make it to the hex, not the number of adjacent hexes from which the healer could be attacked.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI. Note: as attacks by the healers are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The Healer Support Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded.  It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Healer Support Micro AI configuration from the &amp;quot;Healers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             aggression=0&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Lurkers Micro AI (ai_type=lurkers) ==&lt;br /&gt;
&lt;br /&gt;
The Lurker Micro AI controls some or all units of a side in &amp;quot;lurker mode&amp;quot;.  A lurker is a unit that is capable of moving across most terrains, but that only stops on and attacks from specific terrain.  It might also have the ability to hide on this terrain (which is the reason why it is called a lurker).&lt;br /&gt;
&lt;br /&gt;
Lurkers move individually without strategy and always attack the weakest enemy within their reach.  If no enemy is in reach, the lurker does a random move instead - or it just sits and waits (lurks).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of different types of lurker behavior, check out the &amp;quot;Lurkers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Lurkers Micro AI by using&lt;br /&gt;
 ai_type=lurkers&lt;br /&gt;
in the [micro_ai] tag&lt;br /&gt;
&lt;br /&gt;
===Lurkers specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
* '''[filter]''': Specifies which units of the side are controlled by the Lurker Micro AI.  This is a [[StandardUnitFilter|Standard Unit Filter]].  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': Specifies the terrain from which the lurkers attack. This is a [[StandardLocationFilter|Standard Location Filter]].&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_wander]''': Specifies the terrain on which lurkers end their moves if there is no enemy in reach. This is a [[StandardLocationFilter|Standard Location Filter]].  If [filter_location_wander] is not given, it defaults to [filter_location].&lt;br /&gt;
* '''stationary'''=no: (boolean)  If set to yes, a lurker never moves if there is no enemy within attack range.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Lurkers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=4&lt;br /&gt;
             ai_type=lurkers&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Naga Fighter&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=W*,S*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             [filter_location_wander]&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location_wander]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Messenger Escort Micro AI (ai_type=messenger_escort) ==&lt;br /&gt;
&lt;br /&gt;
The Messenger Escort Micro AI lets you define a set of waypoints through which it tries to move one or several of its units, the messenger(s).  The other units escort the messenger, trying to protect it and attacking enemies that are in its way.  Note that a messenger might not strictly hit intermediate waypoints, but that getting close is good enough, especially if a waypoint is occupied by an enemy unit.  It does, however, always try to get to the last waypoint.&lt;br /&gt;
&lt;br /&gt;
'''Notes on the use of several messengers:''' {{DevFeature1.13|0}}  &lt;br /&gt;
* This Micro AI can control several messengers at the same time, which can but do not have to move in a group.  They can be put on the map altogether in one location, or be spread out on the map or across different turns.&lt;br /&gt;
* The farther a messenger has moved through the chain of waypoints, the higher a priority it is for the AI, meaning that farther advanced (on the map) messengers are moved first, and that the escort units try to clear the way and stay with those messengers more so than with those further back on the map.  This order can be inverted using the invert_order= key.&lt;br /&gt;
* If several messengers are on the map, escort units might move from one to the other, the AI making a judgement call where they are needed most.  Closeness to a messenger is generally the most important criterion, but not the only one.&lt;br /&gt;
* If a messenger dies, the escort units previously staying with it now join one of the other messengers if there are any left (otherwise they follow default AI behavior).&lt;br /&gt;
* If you want certain escort units to stay always with the same messenger, use several messenger [micro_ai] tags with different [filter]/[filter_second] combinations.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Messenger Escort Micro AI by using&lt;br /&gt;
 ai_type=messenger_escort&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Messenger Escort specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': {{DevFeature1.13|0}} (Previous to 1.13.0, only id= was allowed.) The id of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated list of waypoint coordinates through which the messenger will go in the given order to reach its goal, given by the last coordinate.  If you want the messenger to go directly to the goal, simply enter a single x,y coordinate here.  Note that the messenger does not have to hit each waypoint exactly (except for the final one), getting close to them is good enough. {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Messenger Escort AI consists of 3 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-2&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_death_chance'''=0.67 and '''messenger_death_chance'''=0.0: (both floats; ranging from 0 to 1)    If the messenger is adjacent to an enemy at the end of the move, it will only attack the enemy if the chance of death of the enemy is &amp;gt;= enemy_death_chance and if the messenger's chance of death is &amp;lt;= messenger_death_chance (or if it is an attack at a range for which the enemy does not have a weapon).&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|0}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the escort units.  If not set, this defaults to all enemy units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''invert_order'''=no: {{DevFeature1.13|0}} If set to 'yes', the order of priority with which messenger units are considered is inverted, starting with the messenger unit that is the least advanced on the map (see description above).  In most cases, selecting this option results in worse performance of the AI, so use it with caution.  (And obviously it has no effect at all if there is only one messenger controlled by the AI.)&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Messenger Escort&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=messenger_escort&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=messenger&lt;br /&gt;
             waypoint_x=31,24,27,28&lt;br /&gt;
             waypoint_y=20,14,7,1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Patrol Micro AI (ai_type=patrol) ==&lt;br /&gt;
&lt;br /&gt;
The defines a number of waypoints for a unit (or units) to follow.  Options can be set whether the route should be followed in a loop, as an out-and back, only once, whether units encountered along the way should be attacked, etc.&lt;br /&gt;
&lt;br /&gt;
No matter what options are chosen, getting to the next waypoint always takes priority over attacking for a patroller.  The AI thus prefers to move the patrol unit around enemies rather than straight for them.  Also, if a waypoint is occupied by a unit the AI is not instructed to attack, it will (eventually) abandon that waypoint once it gets close enough and move on to the next one.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different patrol behaviors, check out the &amp;quot;Patrols&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Patrol Micro AI it by using&lt;br /&gt;
 ai_type=patrol&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Patrol specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated lists of the waypoint coordinates through which the patrol travels (in order). {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack''': By default (if this key is not set), the patrol unit attacks any unit it ends up next to after a move.  Alternatively, this parameter can be set to a comma-separated list of unit ids. If that is done, the patrol only attacks these units when it ends up next to them and ignores any other unit.  This can in turn be used to have the patroller not attack at all, by setting ''attack='' to a non-existing id.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''one_time_only'''=no: (boolean)  If set to 'yes', the unit goes through the patrol route only once and then hangs out at the last waypoint.  It also always attacks any enemy unit on the last waypoint, independent of what ''attack='' is set to.&lt;br /&gt;
* '''out_and_back'''=no: (boolean)  By default, a patrol unit returns to the first waypoint after reaching the last, doing the patrol route as a loop.  If this parameter is set to 'yes', the unit instead reverses its course and perpetually does out and backs of the route.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Patrols&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=patrol&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Konrad&lt;br /&gt;
             waypoint_x=9,24,25&lt;br /&gt;
             waypoint_y=21,23,15&lt;br /&gt;
             one_time_only=yes&lt;br /&gt;
             attack=Gertburt&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Protect Unit Micro AI (ai_type=protect_unit) ==&lt;br /&gt;
&lt;br /&gt;
The Protect Unit Micro AI tries to move one or several units to a given location while keeping them safe from enemy attacks. It does so by keeping the protected units away from enemy units and close to friendly units. Other units on the AI side are controlled by the default Wesnoth AI.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Protect Unit&amp;quot; and &amp;quot;The Elves Besieged&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Protect Unit Micro AI by using&lt;br /&gt;
 ai_type=protect_unit&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Protect Unit specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[unit]''': A separate [unit] tag is required for each protected unit(s).  Each tag has required keys '''id''', '''goal_x''', '''goal_y''' for each unit, as shown in the example below. {{DevFeature1.15|0}} '''goal_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''disable_move_leader_to_keep'''=no: (boolean) If set to 'yes', side leaders protected by this AI do not try to return to their keep.  This is necessary for this Micro AI to work with units that are side leaders and that are supposed to move to an off-keep location.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
*The Protect Unit Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].  Depending on the parameters, it might also modify the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|move leader to keep candidate action]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded. It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;HttT: The Elves Besieged&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=protect_unit&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Delfador&lt;br /&gt;
                 goal_x,goal_y=1,2&lt;br /&gt;
             [/unit]&lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
                 goal_x,goal_y=1,1&lt;br /&gt;
             [/unit]&lt;br /&gt;
 &lt;br /&gt;
             disable_move_leader_to_keep=true&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Recruiting Micro AI==&lt;br /&gt;
=== General Comments on the Recruiting Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Recruiting Micro AI replaces the default recruitment AI by alternative recruiting patterns.  Currently there are two recruiting algorithms you can choose from, random recruiting and the rush recruiting used in the new (as of Version 1.11.1) Experimental AI (see below).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of a different recruiting pattern, check out the &amp;quot;Recruiting&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  You can also explore the unit choices by watching the Experimental AI in a multiplayer game.&lt;br /&gt;
&lt;br /&gt;
'''Note:''' Obviously, the Recruitment Micro AI modifies the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|recruitment candidate action]].&lt;br /&gt;
&lt;br /&gt;
=== Random Recruitment AI (ai_type=recruit_random) ===&lt;br /&gt;
&lt;br /&gt;
This AI randomly selects a unit type from the recruitment list of the side.  The probability of choosing each type and the behavior in low-gold situations can be influenced with optional parameters.&lt;br /&gt;
&lt;br /&gt;
Enable the Random Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_random&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* '''[probability]''': This tag sets the probability of selecting the given unit type.  It has two required sub-keys:&lt;br /&gt;
** '''type''': comma-separated list of unit types to which this probability is to be applied&lt;br /&gt;
** '''probability''': (non-negative float)  The probability with which these unit types is to be selected.  All unit types for which no [probability] tag is defined receive a default probability of 1.0, thus values should be chosen relative to that.  As an example, in the code below swordsmen and peasants are given a probability of 8, while no other units have assigned probabilities.  Thus, each of these two unit types is 8 times more likely to be selected than any other individual unit type.&lt;br /&gt;
* '''skip_low_gold_recruiting'''=no: (boolean)  By default, the random recruitment AI chooses a unit to recruit only from those types for which it has sufficient gold.  If this parameter is set to 'yes', it will instead choose from all available unit types and end recruiting for the turn if there is not enough gold to recruit it this turn (even if other, affordable unit types exist).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a random recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_random&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [probability]&lt;br /&gt;
                 type=Swordsman,Peasant&lt;br /&gt;
                 probability=8&lt;br /&gt;
             [/probability]&lt;br /&gt;
 &lt;br /&gt;
             skip_low_gold_recruiting=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Rush Recruitment AI (ai_type=recruit_rushers) ===&lt;br /&gt;
&lt;br /&gt;
This AI replaces the default recruiting with the rush recruiting used in the new (as of Version 1.11.1) Experimental AI.&lt;br /&gt;
&lt;br /&gt;
Enable the Rush Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_rushers&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* {{DevFeature1.15|2}} '''high_level_fraction'''=0: (non-negative number) The approximate fraction of units of level 2 or higher to be recruited. This is defined as fraction of units on the map, not of new units being recruited during a turn or over several turns (which makes a difference if there are already units on the map). The effect is also cumulative per level (starting from level 2), meaning that 1/3 of L2 units will be recruited in the example above, 1/3*1/3=1/9 L3s etc. The default value is zero, which leaves the high-level unit recruiting to the default Experimental AI algorithm resulting usually in very few such units being recruited.&lt;br /&gt;
* '''randomness'''=0.1: (number)  A random number is applied to the rusher recruit engine's score to prevent the recruitment pattern from being too predictable. 0 causes no randomness to be applied, while larger numbers increase the random effect. A value of 1-2 generates results in which the random effect is approximately equal to the scored effect. Extremely high values are essentially entirely random.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a rush recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_rushers&lt;br /&gt;
             action=add&lt;br /&gt;
          [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Simple Attack Micro AI (ai_type=simple_attack) ==&lt;br /&gt;
&lt;br /&gt;
The Simple Attack Micro AI is meant as an addition to the default combat candidate action for when one wants to force the AI to do attacks that it would otherwise not do (for example on units that are 1 XP from leveling) or to execute certain attacks before all others (for example, attack with all &amp;quot;expendable&amp;quot; units first).  It is not set up to be a replacement for the default attacks.  Some notes:&lt;br /&gt;
&lt;br /&gt;
* The algorithm is different from and simpler than that of the default combat CA.  For example, no attack combinations are considered, only individual attacks.  As a result, the [[AiWML#List_of_AI_Aspects|default aspects]] (aggression, caution, [avoid], ...) are not taken into account.  Also, if lots of units are involved, evaluation of this Micro AI is potentially quite a bit slower than of the default attacks.&lt;br /&gt;
&lt;br /&gt;
* If an attack matching the Standard Unit Filters described below is found, this attack will always be executed, no matter how bad the odds are.  Thus, this Micro AI can be used to force the AI to do attacks it would otherwise avoid.&lt;br /&gt;
&lt;br /&gt;
* Other than selecting the attacker/defender units, this AI is currently not configurable.  Let us know if you would like to see certain options to be added.  (No guarantees that we will add them, but we will definitely consider them.)&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Simple Attack&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=simple_attack&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Simple Attack specific keys for the [micro_ai] tag===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  It is, however, in general not desirable to use this AI without at least one of the optional Standard Unit Filters below, as this AI is not meant as a replacement of the default combat candidate action.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=110000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 110,000, which means that it is executed right before the default combat candidate action.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Simple Attack action.  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units the AI should attack.  It defaults to all enemy units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
* '''weapon'''=-1: (integer) The number of the weapon that should be used for this attack.  Weapon numbers start at 1 (not 0).  -1 stands for &amp;quot;best weapon&amp;quot; (by whatever criterion the AI chooses to use).   '''Available from Wesnoth 1.11.8'''&lt;br /&gt;
&lt;br /&gt;
Below are 2 examples of Simple Attack Micro AI configurations from the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  The first one shows how to force the AI to attack with all Soulless and Walking Corpse units first:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110000&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless,Walking Corpse&lt;br /&gt;
             [/filter]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
The second example executes attacks by Soulless on all enemy units that are 1 XP from leveling.  Note that the Lua code in the preload event is there simply in order to code the &amp;quot;1 XP from advancing&amp;quot; condition.  It is not needed for most other SUFs, as can be seen in the previous example.&lt;br /&gt;
     [event]&lt;br /&gt;
         name=preload&lt;br /&gt;
         first_time_only=no&lt;br /&gt;
         [lua]&lt;br /&gt;
             code=&amp;lt;&amp;lt;&lt;br /&gt;
                 function close_to_advancing(unit)&lt;br /&gt;
                     if (unit.experience &amp;gt;= unit.max_experience-1) then&lt;br /&gt;
                         return true&lt;br /&gt;
                     else&lt;br /&gt;
                         return false&lt;br /&gt;
                     end&lt;br /&gt;
                 end&lt;br /&gt;
             &amp;gt;&amp;gt;&lt;br /&gt;
         [/lua]&lt;br /&gt;
     [/event]&lt;br /&gt;
 &lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110001&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless  # No Walking Corpses; L0 units don't advance enemy&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 lua_function = &amp;quot;close_to_advancing&amp;quot;&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
Several Simple Attack Micro AIs can be combined for the same AI side.  The two examples above are both used (note the different values for ca_score) in the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
== Custom Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|5}}&lt;br /&gt;
&lt;br /&gt;
From 1.13.5 onwards, adding a new Micro AI can be easily done without altering any core files or overriding the micro_ai tag. The [micro_ai] tag now looks up its ''ai_type'' parameter in the &amp;lt;code&amp;gt;wesnoth.micro_ais&amp;lt;/code&amp;gt; table and uses the micro AI setup function found there to determine which keys are required and allowed and what candidate actions to add.&lt;br /&gt;
&lt;br /&gt;
A micro AI setup function takes as its single parameter the contents of the [micro_ai] tag and returns three separate values:&lt;br /&gt;
*'''required''': A list of required keys and tags, as strings. Tags are indicated by enclosing them within square brackets in the string. If a key or tag in this list is missing from the [micro_ai] tag, an error will be printed.&lt;br /&gt;
*'''optional''': Similar to above, a list of optional keys and tags, as strings. No error will be printed if a key or tag in this list is missing, nor will an error be printed if it is present (as would be the case for unrecognized keys or tags).&lt;br /&gt;
*'''ca_params''': A table specifying the candidate actions that constitute the micro AI. It should contain an ''ai_id'' key (which specifies a base ID to be used for candidate actions of this micro AI) and a series of candidate action definitions. Each candidate action definition is a table containing the following keys:&lt;br /&gt;
**'''ca_id''': The ID used for the candidate action. This will be combined with the ''ai_id'' to produce the final ID.&lt;br /&gt;
**'''location''': Path to the Lua file that defines the candidate action.&lt;br /&gt;
**'''score''': The maximum score for the candidate action. If the [micro_ai] tag specified a ''ca_score'' parameter, the score here should normally be based on that.&lt;br /&gt;
&lt;br /&gt;
(The return statement would thus look like &amp;lt;syntaxhighlight lang='lua' inline&amp;gt;return required, optional, ca_params&amp;lt;/syntaxhighlight&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
'''Examples''' of micro AI setup functions can be found in the [https://github.com/wesnoth/wesnoth/tree/1.14/data/ai/micro_ais/mai-defs default MAI definitions directory].  See the [https://github.com/wesnoth/wesnoth/blob/1.14/data/ai/micro_ais/mai-defs/animals.lua#L4-L12 Big Animals MAI definition] for a simple example.  For custom micro AIs, the setup code should be put into a ''[lua]'' tag inside a preload event.&lt;br /&gt;
&lt;br /&gt;
Since the function has full access to the contents of the [micro_ai] tag, it can conditionally add candidate actions to '''ca_params''' depending on the values of other keys within the [micro_ai] tag. It can also perform other actions such as adding facets to an aspect; if it does such things, it should pay attention to the ''action'' key to determine whether it's being added or removed.&lt;br /&gt;
&lt;br /&gt;
There are two useful helper functions that can be used by Micro AI setup functions requiring aspect changes. They can be brought into scope by a line similar to the following:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='lua&amp;gt;local MAIH = wesnoth.require(&amp;quot;ai/micro_ais/micro_ai_helper.lua&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* MAIH.add_aspects(side, aspect_params)&lt;br /&gt;
* MAIH.delete_aspects(side, aspect_params)&lt;br /&gt;
&lt;br /&gt;
The ''aspect_params'' parameter is an array of tables, each of which contains the following keys:&lt;br /&gt;
*'''aspect''': The aspect to change.&lt;br /&gt;
*'''facet''': The WML defining the facet. This must include an ''id'' key in order for the aspect to be correctly deleted. For maximum compatibility, incorporate the ''ca_id'' key from the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=66594</id>
		<title>Micro AIs</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=66594"/>
		<updated>2021-02-07T01:19:41Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Simple Attack specific keys for the [micro_ai] tag */ add preload event to code example&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== A Few General Words about Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
* Micro AIs allow Wesnoth campaign and scenario authors to add new AI functionality without the need for any AI programming. They bring new specialized behavior to Wesnoth that cannot be achieved, at least not easily, by adjusting default AI parameters (such as those described at [[AiWML]]) or with WML code.&lt;br /&gt;
&lt;br /&gt;
* A Micro AI is activated and configured (or deleted) via the [micro_ai] tag, requiring only a few lines of WML code.&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.6''', the Micro AI behavior was inconsistent when it came to '''hidden enemies and petrified units'''.  Some Micro AIs ignored those, while others did not.  In some case there was even a potential for AI errors when an AI unit ran into hidden or petrified enemies.  As of 1.13.6, this has all been fixed.  The Micro AIs now correctly and consistently ignore hidden units and treat petrified units as blocking the hex they are on but having no effect otherwise, in the same way as the default AI does.  '''Important notes:'''&lt;br /&gt;
** It is not possible to re-include hidden/petrified units via the Standard Unit Filters (SUFs) used by some of the Micro AIs. In fact, if this is attempted, the filters will generally not match any unit.&lt;br /&gt;
*** This only applies to SUFs which go directly into the [micro_ai] tag. Any SUF that is a sub-tag of another filter (for example, of a Standard Location Filter) is taken literally and returns all units matching the filter. If hidden or petrified units are to be excluded from such a sub-filter, that needs to be specified directly in that filter.&lt;br /&gt;
** We also note that, because all the Micro AIs are written in Lua, it is possible to [[Micro_AIs#Using_Development_.281.13.29_Version_Micro_AIs_in_your_Stable_.281.12.29_Version_Add-on|move a Micro AI's code into one's add-on]] and modify it to whatever scenario-specific behavior is desired (this does not only apply to hidden/petrified units, but to all behavior). Setting up general Micro AI behavior that works in every possible setting is not feasible, but adapting it to work (only) in the specific setting of a given scenario is often quite simple. If you do not know how to do this, please post a message on the Wesnoth forums and we will be happy to help you with it.&lt;br /&gt;
&lt;br /&gt;
* The '''[[AiWML#List_of_AI_Aspects|default AI aspects]] are, for the most part, not taken into account by the Micro AIs'''.  For example, unless noted otherwise, specifying a value for ''aggression'' does not change the MAI behavior, and locations specified in an ''[avoid]'' tag generally can be entered by units controlled by an MAI.  This is a feature of the Micro AIs, not a bug.  If you feel that a certain default AI aspect should also apply to a Micro AI, please let us know in the [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 Micro AIs Feedback thread].&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.7''', the Micro AIs did not all work reliably when the '''AI side is under shroud''' and it may have been necessary to turn shroud off for the side when using a Micro AI. {{DevFeature1.13|7}} The Micro AIs now all work for sides under shroud. They do this by disregarding shroud for terrain purposes when path finding, consistent with default Wesnoth AI behavior, while still ignoring hidden units (including those hidden under shroud) correctly.&lt;br /&gt;
&lt;br /&gt;
* Most Micro AIs in their default setting apply to all units of a side, although many of them provide parameters for filtering the units that should be affected by the new behavior.  If the Micro AI is applied only to part of the units of a side, the other units follow the default Wesnoth AI behavior.&lt;br /&gt;
&lt;br /&gt;
* Micro AIs are available in mainline since Wesnoth 1.11/1.12.&lt;br /&gt;
&lt;br /&gt;
== Micro AI Test and Demo Scenarios ==&lt;br /&gt;
&lt;br /&gt;
All Micro AIs described here can be checked out in a number of test and demo scenarios.  The easiest way to access the test scenarios is by assigning a hotkey to &amp;quot;Start Test Scenario&amp;quot; in the preferences (as of Wesnoth 1.13.8). Then, simply press the hotkey in the titlescreen and select the desired scenario from the list (e.g. 'micro_ai_test' for the overall &amp;quot;switchboard scenario&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Test scenarios can also be accessed from the command line by typing&lt;br /&gt;
 path/wesnoth-executable -t micro_ai_test&lt;br /&gt;
or (depending on your system and Wesnoth version)&lt;br /&gt;
 path/wesnoth-executable -tmicro_ai_test&lt;br /&gt;
This starts up a &amp;quot;switchboard scenario&amp;quot; in which you can select the Micro AI demonstration scenario you want to check out.  Here, ''path/wesnoth-executable'' needs to be replaced by whatever the path and filename of the Wesnoth executable is on your system.&lt;br /&gt;
&lt;br /&gt;
== Setting up a Micro AI ==&lt;br /&gt;
&lt;br /&gt;
Setting up a Micro AI requires only one simple step:&lt;br /&gt;
==== Activating and configuring (or deleting) a Micro AI ====&lt;br /&gt;
&lt;br /&gt;
Micro AIs are activated, deleted and configured using the [micro_ai] tag.  This tag needs to be placed in [[ActionWML]], that is, in an event, a menu option or the like.  As an example, the following code activates the healer_support Micro AI in its default configuration for Side 2 from the beginning of the scenario:&lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
 &lt;br /&gt;
         # Configure the healer support micro AI&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
'''Keys required for all Micro AIs:'''&lt;br /&gt;
All [micro_ai] tags must contain the following three required keys:&lt;br /&gt;
* '''side''': The side for which the Micro AI is to be added, changed or deleted.  This has to be an individual side number, lists of sides are not supported.&lt;br /&gt;
* '''ai_type''': The type of Micro AI to be added, changed or deleted.  See the following sections for allowed values.&lt;br /&gt;
* '''action''' (string): The action to take concerning the Micro AI.  The following values are allowed: &lt;br /&gt;
** ''add'': Add a Micro AI to a side.&lt;br /&gt;
** ''change'': Change the configuration of an existing Micro AI.  Note that this does not only change the specific parameters provided in the tag, but it replaces the entire existing Micro AI by a new version with the new (and only the new) configuration.  It is therefore equivalent to using first the ''delete'' and then the ''add'' action.&lt;br /&gt;
** ''delete'': Delete an existing Micro AI from a side.&lt;br /&gt;
&lt;br /&gt;
'''Keys required under certain circumstances:'''&lt;br /&gt;
* '''ca_id''': (string) If several MAIs of the same type are used for the same side and one or several of these are to be changed or deleted during a scenario, this key needs to be included so that the [micro_ai] tag knows which of them needs to be changed/deleted.  The same is also true if a Micro AI is changed more than once during a scenario.  We therefore '''strongly recommend always setting ''ca_id''''' for MAIs which are, or might be, changed or deleted during a scenario.&lt;br /&gt;
** '''Note:''' The id(s)/name(s) actually assigned to the CA(s) of the MAI by the engine might be different from the parameter passed here.  It is, however, sufficient to use this ca_id with the [micro_ai] tag add/delete/change actions to identify the MAI uniquely.  (In other words: you don't have to worry about this if you always use the [micro_ai] tag, but might have to figure out the actual name(s) of the CA(s) if you want to delete or change them manually.)&lt;br /&gt;
* Other keys may also be required depending on the Micro AI.  In additional, a number of optional keys may be available to configure the AI behavior.  See the sections on the individual Micro AIs for details.&lt;br /&gt;
&lt;br /&gt;
'''Required keys for deleting a Micro AI:'''&lt;br /&gt;
&lt;br /&gt;
Only a subset of the keys required for setting up a Micro AI are needed when deleting it.  '''side''', '''ai_type''' and '''action'''=delete always need to be provided.  '''ca_id''' needs to be given if it was provided when adding the Micro AI (see above for when this is needed).&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* The ''add'' and ''change'' actions ignore all keys that do not apply to the respective Micro AI type.  The ''delete'' action ignores all keys other than the required keys listed above.&lt;br /&gt;
&lt;br /&gt;
* Some of the Micro AIs use and/or modify default AI components (candidate actions or aspects) when they are added, or reset these components to their default values when they are deleted.  Thus, if you have modified the AI yourself using the default CAs or aspects, some of these modifications might be affected, or might be interfering with the Micro AI.  The sections below indicate whether a Micro AI modifies any default AI components.&lt;br /&gt;
&lt;br /&gt;
===Combining Different Micro AI Types for the Same Side===&lt;br /&gt;
&lt;br /&gt;
No extra steps need to be taken when combining different Micro AIs on the same side.  Simply set up the [micro_ai] tags for all AIs you want to use.&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* While it is ''technically'' possible to combine any number of Micro AIs, this does not mean that all Micro AIs work together in practice.  Some are not compatible because they control units in contradictory ways.  &lt;br /&gt;
* Not all combinations that ''should'' work have been tested yet.  If you find a combination that you believe should work but doesn't, [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 please let us know] and we will see if that can be fixed.&lt;br /&gt;
* The candidate action score almost certainly has to be set (using the ca_score= key) for one or several of the MAIs if several of them are used on the same side.&lt;br /&gt;
&lt;br /&gt;
== Animals Micro AIs ==&lt;br /&gt;
=== General Comments on the Animal Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Animals Micro AIs are meant to simulate the behaviors of certain types of animals.  They are, however, set up so that they can be used with arbitrary unit types and might be applicable in quite diverse situations.  The AIs nevertheless remain named after the animals for which they were written originally, so that they are (to some extent) descriptive of their behavior.&lt;br /&gt;
&lt;br /&gt;
Brief descriptions of the AI behaviors are given in the following subsections.  To get a better feeling for their behavior, check out the &amp;quot;Animals&amp;quot;, &amp;quot;Swarm&amp;quot;, &amp;quot;Wolves&amp;quot; and &amp;quot;Dragon&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  Note that some of the animal AIs are designed for animal types that do not exist in mainline.  In the test scenario, other animal types are therefore substituted for them.&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Wolves (ai_type=wolves) ===&lt;br /&gt;
&lt;br /&gt;
The Wolves Micro AI organizes all &amp;quot;wolf&amp;quot; units (predators) of the side to hunt in a single pack.  They actively chase after the closest prey and try to corner it (not always super successfully), but are easily distracted by other enemy units coming into attack range, to the point where the pack might split up.  The wolves can, however, be told to attack only prey units as well as to try to avoid other types of units (such as larger predators).  When no prey is left, the wolves wander randomly.&lt;br /&gt;
&lt;br /&gt;
This AI works best for a small number of predators (2 - 5) and sparse prey.  Also, the predators should be put on the map in a pack, that is, reasonably close to each other.  For a demonstration of the Wolves AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Note that another wolves AI exists, called [[Micro_AIs#Animals_AI:_Multi-pack_Wolves_.28ai_type.3Dwolves_multipacks.29|Multipack Wolves]], which distributes the wolves of a side into several packs, rather than all of them into one single pack as it is done here.&lt;br /&gt;
&lt;br /&gt;
Enable the Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the predator units.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the prey units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_only_prey'''=no: In the default configuration, the wolves attack all enemy units in attack range.  If this key is set to 'yes', they only attack prey units.&lt;br /&gt;
* '''avoid_type''': Comma-separated list of unit types (on enemy sides) which the wolves try to avoid.  They try to stay out of these units' way as much as possible when moving around (except when they are moving in to attack another enemy unit) and never attack them.&lt;br /&gt;
* '''ca_score'''=90000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 90,000, which is lower than the default combat CA, but higher than most other default CAs which move units.  It can be changed by setting this parameter but in most cases this will not make sense.  Note that the Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Notes on default (RCA) AI aspects:''' &lt;br /&gt;
* If ''attack_only_prey'' or ''avoid_type'' are set, the Wolves Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* Attacks by the wolves are left to the default (RCA) AI, meaning that the value of ''aggression'' has an influence on the behavior.&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI is taken into account. Note: as attacks by the wolves are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=6&lt;br /&gt;
             ai_type=wolves&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Wolf&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Vampire Bat&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             avoid_type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Multi-pack Wolves (ai_type=wolves_multipacks) ===&lt;br /&gt;
&lt;br /&gt;
The Multi-pack Wolves Micro AI is different from the [[Micro_AIs#Animals_AI:_Wolves_.28ai_type.3Dwolves.29|Wolves Micro AI]] in that there can be an arbitrary number of wolf packs, with the pack size being a free parameter.  At the beginning of the scenario, close wolves are grouped into packs in a semi-methodical way.  If the wolves are put on the map in distinct groups of close wolves of the correct number, they will be joined into packs matching these groups.  If, on the other hand, they are spread out all over the map, the method of assigning them to packs is semi-random.&lt;br /&gt;
&lt;br /&gt;
Wolves of the same pack begin by joining each other on the map.  After that, they stay together until only one wolf is left, which then tries to join up with an incomplete pack or with other single wolves.  Individual wolves entering the map during the scenario behave in that way as well.&lt;br /&gt;
&lt;br /&gt;
A second difference to the other Wolves AI is that wolves do not actively hunt here.  For the most part they just wander (often long distance).  However, the pack ferociously (and without regard for its own health) attacks any enemy units that come into range, as long as that does not mean separating the pack by more than a few hexes.  Staying together, or joining with a new wolf assigned to the pack, is the only thing that takes priority over satisfying the wolves' thirst for blood.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Multi-pack Wolves AI behavior, check out the &amp;quot;Wolves&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Multi-pack Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves_multipacks&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Multi-pack Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''pack_size'''=3: (integer) The size of the packs.&lt;br /&gt;
* '''show_pack_number'''=no: If set to &amp;quot;yes&amp;quot;, the wolves' pack numbers will be shown below them.  Note that this means that the pack number of a wolf killed in an attack will remain as a label on the map.  &lt;br /&gt;
* '''type'''=Wolf: Comma-separated list of the unit types which the AI controls. The default value is &amp;quot;Wolf&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves_multipacks [micro_ai] tag usage from the &amp;quot;Wolves&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=wolves_multipacks&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             show_pack_number=yes&lt;br /&gt;
             pack_size=4&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Big Animals (ai_type=big_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Big Animals Micro AI is a simulation of large predators that wander and hunt alone, such as Bears, Giant Spiders or Yetis.  For the most part, these just wander on the terrain that's been defined for them, but they attack enemy units if those happen to come into range.  The AI can be set up so that the Big Animals stay out of the way of other units (such as other large predators), in which case they only attack those if cornered or if they run into them accidentally (such as ambushers).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Big Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Big Animals Micro AI by using&lt;br /&gt;
 ai_type=big_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the animals to which this AI is applied.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': The big animals often go for long distance (multi-turn) journeys.  This tag is a [[StandardLocationFilter|Standard Location Filter]] describing the terrain from which the next goal is chosen.  The default is all terrain on the map. &lt;br /&gt;
* '''[filter_location_wander]''': The terrain on which the big animals will end their moves. This is a [[StandardLocationFilter|Standard Location Filter]].  The default is all terrain on the map.&lt;br /&gt;
* '''[avoid_unit]''': A [[StandardUnitFilter|Standard Unit Filter]] describing all enemy units that the big animals will avoid.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
Here's an example of big_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=big_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Ghast&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [avoid_unit]&lt;br /&gt;
                 type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
             [/avoid_unit]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=1-40,1-18&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Forest Animals (ai_type=forest_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Forest Animals Micro AI simulates a number of different animal behaviors that one might find in a forest, namely animals behaving like deer, rabbits and families of tuskers.  In the following, we will refer to those animals by these types, even though the actual unit types can be selected freely.  They behave as follows:&lt;br /&gt;
* Each deer wanders randomly on (selectable) terrain, except when enemies get in its (the deer's) maximum movement range, in which case it flees to the farthest point it can reach.&lt;br /&gt;
* Tuskers exhibit the same behavior as deer, except when an enemy is next to one of the tusklets.  This enemy is attacked unconditionally by the tuskers trying to protect their young.&lt;br /&gt;
* Tusklets blindly follow the closest adult tusker, except when there is no tusker left, in which case they behave the same as deer.&lt;br /&gt;
* Rabbits also wander randomly, but in addition disappear into their holes (if any are within reach) when enemies are close.  They reappear out of their holes at the beginning of the turn, if it is safe.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Forest Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Forest Animals Micro AI by using&lt;br /&gt;
 ai_type=forest_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Forest Animals AI consists of 4 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-3&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''rabbit_type, tusker_type, tusklet_type, deer_type''': Comma-separated lists of the unit types behaving as the respective animals.  If any of these parameters are not set, those types of animal behavior are skipped by the AI.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain on which the forest animals wander when they are not threatened by enemies.&lt;br /&gt;
* '''rabbit_number'''=6: (integer)  The number of rabbits for the AI to have on the map simultaneously.  The AI puts the missing number of rabbits onto the map at the beginning of the AI's turn (when the rabbits come out of their holes).&lt;br /&gt;
* '''rabbit_enemy_distance'''=3: (number)  Rabbits won't spawn in holes that aren't more than this distance away from all enemies.&lt;br /&gt;
* '''rabbit_hole_img''': Rabbit holes are marked by items on the map.  If this parameter is set, it must be the file name of the item image.  If it is not set, all items on the map are treated as rabbit holes.&lt;br /&gt;
** {{DevFeature1.14|10}} Items used for rabbit holes on the map border used to produce an error. Placing them on the map border therefore needed to be avoided on the scenario setup side. They are now automatically excluded from consideration by the AI code.&lt;br /&gt;
&lt;br /&gt;
Here's an example of forest_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=forest_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             deer_type=Vampire Bat&lt;br /&gt;
             rabbit_type=Giant Rat&lt;br /&gt;
             tusker_type=Ogre&lt;br /&gt;
             tusklet_type=Young Ogre&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=*^F*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Herd and Herders (ai_type=herding) ===&lt;br /&gt;
&lt;br /&gt;
The Herding Micro AI sets up an area on the map where a number of herder units (such as sheep dog) control and protect a herd (such as sheep).  For convenience, we simply refer to them as sheep dogs and sheep in the following, even though arbitrary unit types can be selected for either.&lt;br /&gt;
&lt;br /&gt;
Sheep dogs try to keep their sheep safe.  This involves keeping them inside the herding area, positioning themselves in between the sheep and approaching enemies, and attacking the enemies if those get too close.  If no active herding or protecting move is needed, the dogs go to a random location on the perimeter of the herding area.&lt;br /&gt;
&lt;br /&gt;
Sheep wander aimlessly except when a sheep dog is next to them, in which case they run away from the dog.  The dogs exploit this by positioning themselves on the outside of the sheep, if possible.  Sheep also run away from approaching enemies.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Herding AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Herding Micro AI by using&lt;br /&gt;
 ai_type=herding&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herder units (e.g. dogs).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herd units (e.g. sheep).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain forming the perimeter of the herding area.  In other words, this is the path the herders (dogs) use to patrol around the outside of the herding area.  This must be a set of coordinates ''completely enclosing'' the herding area.  Here's an example of setting up a rectangular perimeter:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         x=10-20,10-20,10,  ,20&lt;br /&gt;
         y=11,   21,   11-21,11-21&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
Another possibility is by defining the path by certain terrain on the map (as it is done in the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]), in which case the filter can simply look like this:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         terrain=Rb&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
* '''herd_x,herd_y''': The coordinate of one hex (''any'' one hex) inside the herding area.  This is needed to define which part of the map is on the ''inside'' of the herding area perimeter defined by [herding_perimeter], and which is the ''outside''. {{DevFeature1.15|0}} '''herd_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''attention_distance'''=8: (integer)  If an enemy is within this distance of a herd unit, but not as close as ''attack_distance'', the herders try to position themselves in between the enemy and the herd unit.&lt;br /&gt;
* '''attack_distance'''=4: (integer)  If an enemy is within this distance of a herd animal, the herders attack this enemy.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Herding AI consists of 6 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-5&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Herding [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=herding&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Footpad&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Troll Whelp&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             herd_x,herd_y=32,28&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=Rb&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Hunter (ai_type=hunter) ===&lt;br /&gt;
&lt;br /&gt;
The Hunter Micro AI leads a unit (or units) through hunt-and-rest cycles.  The units wander in a prescribed area of the map, their hunting ground, until an enemy unit comes within range.  Note that the units do not actively chase down enemies, but wander randomly through the hunting ground until an enemy is encountered.&lt;br /&gt;
&lt;br /&gt;
If enemies are within range of a hunter unit, the hunter attacks the weakest of them.  If a kill is made, it then retreats to its rest location, where it stays for a certain number of turns and until fully healed (whatever happens later).&lt;br /&gt;
&lt;br /&gt;
A few more details:&lt;br /&gt;
* If the hunter's way home is entirely blocked on the return (so that there is no possible path), the normal RCA AI takes over its behavior.&lt;br /&gt;
* However, if the way is blocked by an enemy unit occupying the rest hex, that enemy will be attacked unconditionally.&lt;br /&gt;
* A kill only makes the hunter go home when it is the attacker, not as defender.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Hunter AI behavior, check out the &amp;quot;Dragon&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]. &lt;br /&gt;
&lt;br /&gt;
Enable the Hunter Micro AI by using&lt;br /&gt;
 ai_type=hunter&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''home_x,home_y''': The coordinates of the hex to which the hunter returns and rests at after a kill. {{DevFeature1.15|0}} '''home_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the unit's hunting ground.  If this tag is not provided, the hunter uses all of the map.&lt;br /&gt;
* '''rest_turns'''=3: (integer)  The number of turns the AI stays at home_x,home_y after a kill.  It will not leave its home until this number of turns have passed ''and'' it is fully healed.&lt;br /&gt;
* '''show_messages'''=no: (boolean) If set to yes, the hunter announces whenever its behavior changes to the next phase.  Note that this is meant as debug output for the scenario author.  The messages are not translated and are not included in replays (and are only visible to the host in an MP game).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a hunter [micro_ai] tag usage from the &amp;quot;Dragon&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hunter&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Rowck&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=5-30,1-15&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             home_x,home_y=3,15&lt;br /&gt;
             rest_turns=2&lt;br /&gt;
 &lt;br /&gt;
             show_messages=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Swarm (ai_type=swarm) ===&lt;br /&gt;
&lt;br /&gt;
The Swarm Micro AI uses some very simply algorithms to simulate animal swarm behavior.  Without adjacent enemies, they simply do a random move, trying to stay together and at a certain distance from enemies.  However, if an enemy unit is close to any bat, the swarm scatters.  This is particular fun to watch when one places an enemy unit in the middle of the swarm.  After being scattered, the swarm members slowly rejoin, but not in a very organized way.  Sub-swarms or individual bats might roam around for quite some time before they find their way back.  It is also possible that individual bats (or small groups) split off from the larger swarm at times.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Swarm AI behavior, check out the &amp;quot;Swarm&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Swarm Micro AI by using&lt;br /&gt;
 ai_type=swarm&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Swarm AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_distance'''=5: (int) The minimum distance kept between units of the swarm and enemies when the swarm moves as a whole.&lt;br /&gt;
* '''scatter_distance'''=3: (int) Enemies within &amp;quot;scatter_distance&amp;quot; hexes of any swarm unit cause the swarm to scatter, by each unit trying to maximize its individual distance from the enemy.&lt;br /&gt;
* '''vision_distance'''=12: (int) Only units within this distance follow the overall swarm motion (either away from an enemy or of the swarm as a whole).  The smaller this value is set, the less likely the swarm is to stay together or rejoin.  This parameter is meant to simulate how far an individual member of the swarm can see, meaning that the swarm is &amp;quot;out of sight&amp;quot; (and the unit will not be able to follow it) if it is more than this distance away.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Swarm [micro_ai] tag usage from the &amp;quot;Swarm&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=swarm&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Assassin Squad Micro AI (ai_type=assassin) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|6}}&lt;br /&gt;
&lt;br /&gt;
This Micro AI controls one or several assassin units which try to circle around enemy units with the goal of hitting a target unit without being distracted or impeded by other enemies.  The units will always attack the target if such attacks are possible, otherwise they will follow the path of least resistance toward it. Notes:&lt;br /&gt;
&lt;br /&gt;
* The main priority for the AI is to find paths that avoid enemy units. However, if no such path can be found, the assassins will still advance toward the target, taking the path exposing the units to the least potential damage from enemies.&lt;br /&gt;
&lt;br /&gt;
* The units will not necessarily follow the same path. The best path is chosen individually for each unit.&lt;br /&gt;
&lt;br /&gt;
* While it is not a requirement for the AI to work, using ambusher or skirmisher units, or units with the quick trait, makes this AI more lethal in many cases.&lt;br /&gt;
&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded, it does not work otherwise. That means that '''ca_score''' is not a valid key for the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=assassin&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Assassin Squad specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the assassin units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the target unit.&lt;br /&gt;
** It is important to realize that the AI currently only works correctly with a single target unit. It is thus the responsibility of the scenario designer to make sure that this filter matches an individual enemy unit. If several enemy units are to be targeted in the same scenario, several instances of the Assassin Micro AI can be used.&lt;br /&gt;
** The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[prefer]''': A [[StandardLocationFilter|Standard Location Filter]] selecting locations on the map for the assassins to prefer when making their way toward the target unit. This could be used, for example, to make units with the ambush ability move (mostly) through forest or to have the assassins circle around the, say, northern part of the map rather than the south if that is desirable for scenario design purposes.&lt;br /&gt;
** As with avoiding enemies, the units will still move across non-preferred terrain if that is either the only option or ''much'' shorter than preferred terrain&lt;br /&gt;
** Attacking, once the target unit is in range, is not influenced by the [prefer] SLF.&lt;br /&gt;
&lt;br /&gt;
Here is an example of an Assassin Squad [micro_ai] tag usage:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=assassin&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Assassin,Master at Arms,Duelist&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             [prefer] # Circle around northern part of map&lt;br /&gt;
                 x=1-9, 10-99&lt;br /&gt;
                 y=1-37, 1-17&lt;br /&gt;
             [/prefer]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Bottleneck Defense Micro AI (ai_type=bottleneck_defense) ==&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense Micro AI lets you define a location on the map where the AI can take a defensive stance behind a narrow passage (bottleneck).  Units on the front line are backed up by healers and/or units with the leadership ability.  Injured units are moved to the back and replaced by uninjured (or less injured) units.  The units on the front line only attack when it is safe (no retaliation) or when there is a high chance that they will make a kill or level up.  Using this Micro AI only makes sense if there is no way for the enemy sides to move around the bottleneck and attack the AI's units from behind.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Bottleneck&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Bottleneck Defense Micro AI by using&lt;br /&gt;
 ai_type=bottleneck_defense&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Bottleneck Defense specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense AI requires two sets of coordinates that define where it should be taking up its defensive stance, and from which side the enemy attacks at that location.&lt;br /&gt;
&lt;br /&gt;
* '''x,y''': Comma separated lists of the hexes on the front line, where strong units are placed.  All hexes on which the AI makes contact with (can be attacked by) the enemy need to be included here.  Units are placed on them in the order in which they are listed here. {{DevFeature1.15|0}} '''location_id''' can be used instead with named/special locations.&lt;br /&gt;
* '''enemy_x,enemy_y''': Comma separated list of the hexes from which the enemy can attack the AI front line units.  This is needed for the AI to know on which side of the front line support units (healers etc.) need to be placed.  In many cases, it is sufficient to provide only one of the enemy front line hexes, but there are geometries for which that does not work.  It is therefore safer to list all enemy front line hexes here. {{DevFeature1.15|0}} '''enemy_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
By default, the AI places healers and units with the leadership ability both on the front line itself (if they are the strongest units available) and on hexes adjacent to and behind the line.  If different placement is desired, these locations can be overridden with the following keys.&lt;br /&gt;
&lt;br /&gt;
* '''active_side_leader'''=no: (boolean)  If set to 'yes', the side leader participates in the bottleneck defense action after the side's gold has been spent.  If set to 'no' (default), the leader follows default AI behavior (sitting on the keep and recruiting, for the most part).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Bottleneck Defense AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.15|3}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the units.  Only AI units matching this filter participate in the bottleneck defense.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''healer_x,healer_y''': Comma separated list of hexes where healers are placed.  This can be used to keep healers from the front line (or to take up only certain positions on the front line), and/or to override the default healing positions behind the line.  Healers are placed on them in the order in which the hexes are listed, with the exception that hexes on the line always take priority over hexes behind the line. {{DevFeature1.15|0}} '''healer_loc''' can be used instead with named/special locations.&lt;br /&gt;
* '''leadership_x,leadership_y''': Same as ''healer_x,healer_y'', but for units with the leadership ability. {{DevFeature1.15|0}} '''leadership_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
As an example, in the &amp;quot;Bottleneck&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]], there are three front line hexes, two of which are on hill terrain, while the third is a road with a lower defense rating.  The healer and side leader are supposed to participate in combat because they are strong units, but only from the hill hexes to keep them safer.  This is accomplished by using the following keys settings (check out the scenario to see which hex is which):&lt;br /&gt;
 [micro_ai]&lt;br /&gt;
     side=1&lt;br /&gt;
     ai_type=bottleneck_defense&lt;br /&gt;
     action=add&lt;br /&gt;
 &lt;br /&gt;
     x=14,14,14&lt;br /&gt;
     y= 7, 9, 8&lt;br /&gt;
     enemy_x=13,13&lt;br /&gt;
     enemy_y= 8, 9&lt;br /&gt;
 &lt;br /&gt;
     healer_x=14,14,15,15&lt;br /&gt;
     healer_y= 7, 9, 8, 9&lt;br /&gt;
     leadership_x=14,14,15,15&lt;br /&gt;
     leadership_y= 7, 9, 9 ,8&lt;br /&gt;
     active_side_leader=yes&lt;br /&gt;
 [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Fast Micro AI (ai_type=fast_ai) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|0}}&lt;br /&gt;
&lt;br /&gt;
This AI is meant as a fall-back AI for scenarios where there are so many units that attacks and moves done by the default AI are slow with long delays before each move.  It replaces the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions (CAs).  It should be obvious that in order to achieve the faster speed, compromises concerning the quality of the moves have to be made.&lt;br /&gt;
&lt;br /&gt;
Enable the Fast Micro AI by using&lt;br /&gt;
 ai_type=fast_ai&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Combat candidate action:'''&lt;br /&gt;
* Attackers are considered individually one at a time.  The best attack found for a unit is executed without checking whether a better attack by another unit might be possible.&lt;br /&gt;
* The default order in which attackers are considered is by decreasing hitpoints, starting with the strongest unit (in terms of HP).  This can be modified with the optional keys below.&lt;br /&gt;
* By default, if weaker units block attack hexes for a stronger unit, they do not move out of the way (this is the same as for the default AI).  If a hex becomes available after an attack (e.g. due to a weaker unit dying in the attack), all attacks that were possible ''before'' this happened are finished first.  Only after that are attacks by the stronger unit reconsidered.&lt;br /&gt;
* If include_occupied_attack_hexes=yes is set, weaker units move out of the way for stronger units if possible.  This can, however slow down the AI depending on the situation on the map.  Use with caution.&lt;br /&gt;
* {{DevFeature1.13|2}} Previous to this version, attacks by the leader were bunched in with the attacks by the other units.  Now leader attacks are treated separately and contain an extra layer of analysis to determine whether the attack position is &amp;quot;safe&amp;quot; (in an approximate way).  Currently, this is done with two simple considerations:&lt;br /&gt;
** Count all enemy units that can reach the leader's attack position.  If there are more than given by optional parameter 'leader_attack_max_units' (default value 3), do not attack.&lt;br /&gt;
** Add up the hitpoints of all enemies that can reach the leader's attack position.  If this value, multiplied by optional parameter 'leader_weight'(default value 2), is larger than the leaders hitpoints, do not attack.&lt;br /&gt;
** Two optional parameters, 'threatened_leader_fights' and 'leader_additional_threat', can also be used to let the leader attack when the threat at the attack hex is comparable to that at the leader's current position.&lt;br /&gt;
** In order to keep this AI as fast as possible, leader attacks are done by a separate candidate action that is only evaluated when all other CAs are done.&lt;br /&gt;
&lt;br /&gt;
'''Move-to-targets, villages and retreat candidate actions:'''&lt;br /&gt;
* The move-to-targets, villages and retreat candidate actions of the default AI are removed.  Move-to-targets and villages are taken over by the Fast Micro AI, whereas retreat is simply not done.&lt;br /&gt;
* This CA moves units toward goals after attacks are done.  Goals are unowned (if the AI side has a leader only) and enemy-owned villages and enemy leaders.&lt;br /&gt;
* For each goal, units are pre-sorted in order of the fewest moves needed to cover the ''distance'' toward the goal (that is, as if move cost on each hex toward the goal were 1).  Units are then considered sequentially and if a unit can get there &amp;quot;efficiently&amp;quot;, its move is executed without considering other units.&lt;br /&gt;
** A move is considered efficient if the move cost to get to the hex is not more than 'move_cost_factor' larger than the distance to the hex.  'move_cost_factor' is an optional key that defaults to 2.  It can be set to values of 1.1 or larger (see below).&lt;br /&gt;
** If the move for the current unit is not found to be efficient, the AI considers the next unit instead.  If no unit can do an efficient move toward the goal, the best (fastest) of the inefficient moves is selected instead.&lt;br /&gt;
&lt;br /&gt;
* Village goals:&lt;br /&gt;
** Village goals are considered before enemy leader goals so that the AI preferentially chooses scouts (or otherwise fast units) for them.&lt;br /&gt;
** The number of units sent toward villages depends on the value of the default AI aspect village_value.  It is set to village_value/4 times the number of units the AI has left to move.  Thus, by default (village_value=1.0) the AI sends a quarter of its units toward villages, and the rest toward enemy leaders.  This means that the meaningful range of values for village_value is 0..4.&lt;br /&gt;
** Only one unit is moved toward each village.  This consideration also overrides the number of units to move toward villages defined by village_value, topping it at the number of unowned/enemy-owned villages on the map.&lt;br /&gt;
** Setting village_value=0 disables moving units toward villages altogether.  That is, not only are no units chosen, but the village targeting phase of the AI is entirely disabled with no evaluation time used for it.  Note that, unlike with the default AI, this also means that no villages within range of an AI unit are grabbed.&lt;br /&gt;
** The order in which villages are chosen as goals is some combination of them being close to the AI side's leader and being spread out over the map.&lt;br /&gt;
&lt;br /&gt;
* Enemy leader goals:&lt;br /&gt;
** The order of enemy leaders goals is set according to their distance from the AI leader with the closest enemy leader being targeted first (if the AI has a leader, otherwise their order is random).  One unit is sent toward each enemy leader in this order, after which the AI starts with the first enemy leader again.  This is repeated until all units have moved.&lt;br /&gt;
&lt;br /&gt;
* Guardians (units with ai_special=guardian set) are ignored by the fast move-to-targets CA.&lt;br /&gt;
&lt;br /&gt;
'''Tips for speeding up/modifying the Fast Micro AI in specific scenarios:'''&lt;br /&gt;
&lt;br /&gt;
A number of optional keys and default AI aspects can be used to modify the behavior of the Fast Micro AI.  The MAI is set up so that it is a reasonable compromise between speed and quality of the moves in most situations.  However, this might not be ideal for all scenarios here are some things that can be done to speed up the AI (or to get better behavior in some situations by doing the opposite):&lt;br /&gt;
* Use large values for the default AI aspect aggression.  The largest delays before attacks happen (in some circumstances) when the Fast MAI has to consider lots of units with attacks left but decides not to attack with them.  See below for a description of how aggression is used by the fast AI.&lt;br /&gt;
* Use large values of move_cost_factor (see above for description of what this does).  If you want to emphasize units that can move across terrain easily (e.g. bats) to be chosen for village grabbing, you might want to use low values of move_cost_factor, but this might slow down the AI.  The default value should work reasonably well in most standard situations, but modifying the value might work better on specific maps.&lt;br /&gt;
* Don't use dungeon_mode=yes unless absolutely necessary, even in most dungeon crawls.  Test the AI without this setting first and only use it if it is the only way to solve the problem.  Often, a minor tweak to the map will have the same effect, without the speed penalty.&lt;br /&gt;
* Use include_occupied_attack_hexes=yes with caution.  It might (or might not) slow down the attack CA significantly, depending on the scenario.&lt;br /&gt;
* Set village_value=0, which makes the AI not consider villages as goals.  This has not been found to make a large difference on the maps tested so far, but it might have an effect on maps with a very large number of villages.&lt;br /&gt;
* Use attack_hidden_enemies=yes on maps where there are no hidden enemies. Excluding hidden enemies from the attack calculation requires an additional filter to be evaluated that takes extra time. In most cases this will not result in a large computation time saving.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_hidden_enemies'''=no: (boolean)  By default, hidden enemies are excluded from the attack calculation.  Setting this to 'yes' makes the AI also consider those enemies as attack targets.&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.13|2}} A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  As with the default AI, units will not step on hexes defined by the [avoid] tag.  In addition, no goals will be chosen that lie on those hexes.  The one minor exception is that a unit stepping out of the way for another unit might temporarily enter the avoided zone.&lt;br /&gt;
* '''dungeon_mode'''=no: (boolean)  As much as possible, the Fast Micro AI works with distances between hexes rather than path finding.  This works pretty well on open maps and even most dungeon crawls (and similar scenarios), but it might result in some units getting temporarily (and in rare cases permanently) stuck on maps with convoluted labyrinths.  If that is the case, setting dungeon_mode=yes triggers more path finding that avoids such situations (but as a result is somewhat slower).&lt;br /&gt;
* '''include_occupied_attack_hexes'''=no: (boolean) If set to 'yes', the AI moves weaker units out of the way for a stronger attacker.  This might lead to better attacks but is slower, so don't use if speed is essential.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the attackers.  Only AI units matching this filter will attack, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes the side= key set to the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the enemies.  Only enemy units matching this filter will be attacked, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''leader_weight'''=2: (number &amp;gt;= 0) {{DevFeature1.13|2}} A factor specifying how much more valuable the AI considers side leaders than non-leader units in the attack evaluation. This is also used to determine whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''leader_additional_threat'''=1: (number &amp;gt; 0) {{DevFeature1.13|2}} If threatened_leader_fights=yes, the threat at the attack position may be larger by this factor for the leader to leave the keep for an attack.&lt;br /&gt;
* '''leader_attack_max_units'''=3: (number &amp;gt;= 0) {{DevFeature1.13|2}} A number specifying how many enemy units within reach are acceptable when determining whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''move_cost_factor'''=2: (number &amp;gt;= 1.1)  The factor by which the move cost to a goal hex may be larger than the distance to the goal hex for a move to be considered &amp;quot;efficient&amp;quot;.  See description above.  1.1 is used if this is set to values smaller than 1.1.  Smaller values might lead to better behavior, while larger values might speed up the AI.&lt;br /&gt;
* '''weak_units_first'''=no: (boolean) If set to 'yes', inverts the order in which units are considered by the combat CA.  Attacks are then done in order of increasing hitpoints of the attackers.&lt;br /&gt;
* '''skip_combat_ca'''=no: (boolean) If set to 'yes', the fast combat CA is not used.  The default AI's high_xp_attack and combat CAs remains in place in this case.&lt;br /&gt;
* '''skip_move_ca'''=no: (boolean) If set to 'yes', the fast move-to-targets CA is not used.  The default AI's retreat, villages and move-to-targets CAs remains in place in this case.&lt;br /&gt;
* '''threatened_leader_fights'''=no: (boolean) {{DevFeature1.13|2}} By default, the AI leader will not attack if the threat at the attack position is too high.  If this key is set to 'yes', the leader will attack if the threat there is not larger than the threat at his current position multiplied by the value of 'leader_additional_threat'. This is meant as an option for the endgame: when the AI leader is under attack, he might as well go down fighting.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''aggression''': The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to set how aggressive the AI should attack.  It works in roughly the same way as the default AI aspect:&lt;br /&gt;
** The value of (1 - aggression) determines how much the AI values its own units compared to the enemy's units.  Thus, aggression=0 means that attacks are only done if the AI expects to deal (roughly) the same amount of damage as it will receive.  aggression=1 means that expected damage to the AI's units is not considered, only the expected damage to enemy units matters.&lt;br /&gt;
** Damage is calculated as a combination of hitpoints and chance to die, in &amp;quot;gold equivalent units&amp;quot;.&lt;br /&gt;
** Unlike the default AI, aggression is set to 1 for values larger than 1.  Allowed values are thus anything from -infinity to +infinity, although only -infinity to +1.0 make sense.&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.13|2}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
*'''attacks aspect''':  {{DevFeature1.13|2}} The standard RCA AI [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] used to filter own and enemy units for attacks is now also taken into account as long as neither [filter] nor [filter_second] (see above under 'optional keys') are defined in the [micro_ai] tag.&lt;br /&gt;
*'''village_value=1''': (number ranging from 0 to 4) The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to define how much villages should be targeted by the move-to-targets CA.  In the Fast MAI, this defines which fraction of the AI units are moved toward village goals (see above for details).  If set to 0, villages are ignored as goals.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded to the same values as those of the respective [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|default candidate actions]].  ca_score is therefore not a valid optional key.&lt;br /&gt;
* The fast Micro AI modifies (removes) the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions.&lt;br /&gt;
&lt;br /&gt;
Enabling the Fast Micro AI in its default configuration is as simple as this usage example from the &amp;quot;Fast AI&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=fast_ai&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Generalized Goto Micro AI (ai_type=goto) ==&lt;br /&gt;
&lt;br /&gt;
The Goto Micro AI provides a highly configurable method of sending a unit (or units) to a location or set of locations.  The units to be moved are defined using a [[StandardUnitFilter|Standard Unit Filter]] (SUF) [alternatively, all side units are used if the SUF is not provided), while the goto locations are given in a [[StandardLocationFilter|Standard Location Filter]] (SLF).&lt;br /&gt;
&lt;br /&gt;
By default, each unit matching the SUF will move toward the closest goal (in terms of turns needed to get there) matching the SLF via the fastest route given the current situation on the map (locations of enemy units etc.), but this behavior can be influenced by the optional keys listed below.  Note that this means that a unit's goal might change from one turn to the next depending on the situation on the map.&lt;br /&gt;
&lt;br /&gt;
The behavior of the unit(s) once they reach their goal location(s) is configurable as well.  By default, they will stay there until the Goto Micro AI is deleted or changed.&lt;br /&gt;
&lt;br /&gt;
For demonstrations of several Goto Micro AI usages, check out the &amp;quot;Goto&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Goto Micro AI by using&lt;br /&gt;
 ai_type=goto&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Goto specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the possible goal locations for the move.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists). Note that the area defined by [avoid] is not entered at all, even if the end point of the move would lie outside the area again.&lt;br /&gt;
* '''avoid_enemies''': (number&amp;gt;0) By default, units controlled by the Goto Micro AI will not take enemy units into account for route finding, other than the enemies' ZoCs.  By contrast, if this key is set, they will try to avoid traversing hexes on which they can be attacked by enemies.  The larger the value of ''avoid_enemies'', the more the units will try to stay out of the enemies' way.  Notes:&lt;br /&gt;
** Units are willing to take a path that is 'avoid_enemies' movement points longer for each enemy unit that can be avoided by doing so for each hex along the path.  As an example, if avoid_enemies=1 and the shortest path crosses 2 hexes that can be reached by 3 enemies each, the unit is willing to take a path that is up to 6 hexes longer instead.  If avoid_enemies=10 in the same situation, the unit will take a path up to 60 hexes longer.&lt;br /&gt;
** Hexes adjacent to enemy units are always strongly avoided, independent of the value of avoid_enemies.&lt;br /&gt;
** avoid_enemies is ignored if 'use_straight_line=true' is set.&lt;br /&gt;
** Due to an issue with the path finding code, '''tunnels and teleports are ignored in Wesnoth 1.14 and before''' if 'avoid_enemies' is used. This has been fixed in Wesnoth 1.15.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units to be moved.  Defaults to all units of the side if not defined.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''ignore_enemy_at_goal'''=no and '''ignore_units'''=no: (boolean)  In the default configuration, the Goto MAI does not move units if the path to all goal hexes is blocked by enemies.  It is therefore possible to disable the Goto MAI by putting enemy units on all the goal hexes.  That can be changed if either of these two keys is set to 'yes'.&lt;br /&gt;
** 'ignore_enemy_at_goal=yes' makes the AI choose its goals while disregarding any enemy units at the goal hexes.  Even so, a stiff rating penalty is applied to goal hexes occupied by enemies, thus that unoccupied goal hexes are chosen unless all possible goals are occupied by enemies.  Note that this setting ''only'' ignores enemy units at the goal hexes themselves.  It is still possible to disable the AI by putting enemies all around the goals.&lt;br /&gt;
** 'ignore_units=yes' let's the AI ignore ''all'' enemy units when deciding which goal hex(es) to move toward.&lt;br /&gt;
** In both cases, enemies are only ignored for deciding which unit to move toward which goal hex.  For the actual move itself, all enemies and their ZoCs are considered as usual.&lt;br /&gt;
** Both options only take effect if neither avoid_enemies nor use_straight_line is set to 'yes', as neither of these consider enemies as blockers.&lt;br /&gt;
* '''release_unit_at_goal'''=no: (boolean) By default, a unit that reaches a goal location will stay there until the Goto Micro AI is deleted or changed (or until the conditions specified in the [goto_goals] SLF change).  If this parameter is set, a unit that has reached its goal will not be controlled by this Micro AI any more starting from the next turn.&lt;br /&gt;
* '''release_all_units_at_goal'''=no: (boolean) Same as ''release_unit_at_goal'', but this will deactivate the Goto Micro AI for all units once the first unit reaches a goal.&lt;br /&gt;
* '''unique_goals'''=no: (boolean)  If set to 'yes', each unit will be sent to a different goal location.  Note that the best goals are recalculated each turn, so a unit might choose a different goal this turn from what it was heading for during the previous turn if the situation on the map has changed.  Also note that if there are more units than goal locations, the left over units will not be handled by the Goto MAI but follow default behavior instead.&lt;br /&gt;
* '''use_straight_line'''=no: (boolean)  By default, units choose the route by which they will get to their goal locations in the smallest number of turns.  If this parameter is set to 'yes', a straight line route (or something pretty close to it at least) will be chosen instead, irrespective of whether this leads into a dead end, whether it is blocked by enemies, etc.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* Depending on the parameters used, the Goto Micro AI might require a lot of path finding to be done.  Thus, while it is possible to set up this AI to move many units to many different locations, a usage like that might require quite a long calculation time.&lt;br /&gt;
&lt;br /&gt;
===Goto Micro AI example usages:===&lt;br /&gt;
&lt;br /&gt;
Here are several examples of goto [micro_ai] tag usages from the &amp;quot;Goto&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
* Send a unit toward a castle (similar to the RCA AI's Goto Candidate Action, but avoiding dead ends and areas ZoCed by enemies)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
             ca_id=messenger1&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=messenger1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=6-7,2-4&lt;br /&gt;
                 terrain=C*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             ca_score=210001&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send all units of the side toward the southern border of the map&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=8&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                  y=33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             avoid_enemies=1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send units to the four corners of the map (with each unit going to a different corner).&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x=1,1,44,44&lt;br /&gt;
                 y=1,33,1,33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             unique_goals=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Set up a guardian unit that doesn't move while no enemy unit is within 8 hexes (ironically, the Goto Micro AI can be used to keep units in place, although using one of the Guardian Micro AIs might often be a better idea)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=guard1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 [filter]&lt;br /&gt;
                     id=guard1&lt;br /&gt;
                 [/filter]&lt;br /&gt;
                 [not]&lt;br /&gt;
                     [filter]&lt;br /&gt;
                         side=1&lt;br /&gt;
                     [/filter]&lt;br /&gt;
                     radius=8&lt;br /&gt;
                 [/not]&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Guardian Micro AIs ==&lt;br /&gt;
=== General Comments on the Guardian Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Guardian Micro AIs can be used to set up different kind of guardian behaviors for a unit or a set of units.  Four different types of guardians are available and are described below.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different guardian behaviors, check out the &amp;quot;Guardians&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
=== Return Guardian  (ai_type=return_guardian)===&lt;br /&gt;
&lt;br /&gt;
A 'return guardian' is a variation of the standard Wesnoth guardian. It has an assigned guard position (GP) to which it returns after attacks on approaching enemies:&lt;br /&gt;
* If at GP with no enemy in reach, do nothing.&lt;br /&gt;
* If at GP with an enemy in reach, attack. Note that the attack is done by the default AI, which might result in the guardian not attacking if the enemy is deemed too strong (this can be influenced by setting the default ''aggression'' value to a different value).&lt;br /&gt;
* If not at GP, return there, no matter whether an enemy is in reach or not.&lt;br /&gt;
* If enemies are blocking the way back to GP, do your best to move around them.&lt;br /&gt;
* If the guardian ends up next to an enemy on the way back, attack that enemy after the move.&lt;br /&gt;
&lt;br /&gt;
Enable the Return Guardian Micro AI by using&lt;br /&gt;
 ai_type=return_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''return_x,return_y''': The coordinate of the hex the unit returns to after each attack.  {{DevFeature1.15|0}} '''return_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=100010: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  This AI consist of 2 CAs, one with score &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt; the other with &amp;lt;tt&amp;gt;ca_score-20&amp;lt;/tt&amp;gt;.  The default value is set so that the first CA gets executed right before the default combat CA, and the other right after.  It usually will not make sense to change this value, but it is possible to do so for custom applications.  {{DevFeature1.13|6}}: This is now changed to 100100 and 99900 in order to avoid conflicts with the new high_XP_attacks candidate action.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a return guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=return_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=return1&lt;br /&gt;
             return_x,return_y=20,2&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
===Stationed Guardian (ai_type=stationed_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A 'stationed guardian' is another variation of the standard Wesnoth guardian with a somewhat more complex behavior than that of the 'return guardian'. Two positions are defined for it, a 'station' and a 'guarded location', as well as a distance. The behavior is as follows:&lt;br /&gt;
* If no enemy is within 'distance' of the guard's current position, do nothing.  This is independent of the guardian being at its station or not.&lt;br /&gt;
* Otherwise: If an enemy is within 'distance' of the guard, but not also within the same distance of the guarded location and the station (all of this simultaneously), move the guard in the direction of the station (or keep the guard in place if the station is unreachable).&lt;br /&gt;
* Otherwise:&lt;br /&gt;
** Pick the enemy unit that is closest to the guarded location.&lt;br /&gt;
** If we can reach it, pick the adjacent hex with the highest defense rating and attack from there.&lt;br /&gt;
** If not in reach, move toward this unit.&lt;br /&gt;
&lt;br /&gt;
Enable the Stationed Guardian Micro AI by using&lt;br /&gt;
 ai_type=stationed_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance parameter as explained above&lt;br /&gt;
* '''station_x,station_y''': The x and y position of the hex that serves as the guardians station. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''guard_x, guard_y''': The x and y position of the guarded location.  {{DevFeature1.13|0}}  These are now optional keys, defaulting to station_x,station_y if not provided. {{DevFeature1.15|0}} '''guard_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a stationed guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=stationed_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=stationed1&lt;br /&gt;
             distance=4&lt;br /&gt;
             station_x,station_y=2,14&lt;br /&gt;
             guard_x,guard_y=3,13&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Coward (ai_type=coward) ===&lt;br /&gt;
&lt;br /&gt;
Cowards are units that, like guardians, sit around doing nothing until an enemy comes into range.  Unlike guardians, however, they run away once enemies approach. Applications might be wild animals, unarmed civilians getting in the way of a battle, etc.  The coward macro can be called with two optional locations, 'seek' and 'avoid':&lt;br /&gt;
* If neither is given, the coward retreats to the position farthest away from the approaching enemies.&lt;br /&gt;
* If 'seek' is given, it preferentially goes toward that location (but getting away from enemies takes priority).&lt;br /&gt;
* If 'avoid' is given, it in addition tries to avoid that location (with both maximizing distance from enemies and going toward 'seek' taking priority).&lt;br /&gt;
* Both 'seek' and 'avoid' may consist of only one coordinate ('x' or 'y'), in which case not a single hex, but a line of hexes is sought or avoided.&lt;br /&gt;
&lt;br /&gt;
Enable the Coward Micro AI by using&lt;br /&gt;
 ai_type=coward&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance within which an enemy must be to &amp;quot;scare&amp;quot; the unit&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_if_trapped'''='no':  {{DevFeature1.13|0}}  With the default settings, a coward never attacks.  If this key is set to 'yes', the coward attacks the weakest (lowest hitpoints) enemy next to which it ends up after moving (such as a cornered animal would do, blindly lashing out without regards for its own safety).&lt;br /&gt;
* '''avoid_x, avoid_y''': The x and y coordinate of the hex to avoid. {{DevFeature1.15|0}} '''avoid_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units from which the cowards flee.  If not set, this defaults to all enemy units.  Note, however, that no &amp;quot;enemy of AI side&amp;quot; condition is added if [filter_second] is specified, meaning that it can include allied and/or enemy units as desired (and even units on the same side as the coward).&lt;br /&gt;
* '''seek_x, seek_y''': The x and y coordinate of the hex to seek. {{DevFeature1.15|0}} '''seek_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a coward [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=coward&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=coward3&lt;br /&gt;
             distance=5&lt;br /&gt;
             seek_x,seek_y=24,5&lt;br /&gt;
             avoid_x,avoid_y=24,15&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Zone guardian (ai_type=zone_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A zone guardian is a unit that, as the name says, guards a zone. It moves randomly inside this zone until an enemy enters it (or a separately defined enemy zone, see below). Applications might be the defense of a castle, a nesting area or for protecting an unit.  The zone macro can be called with an optional SLF, [filter_location_enemy]:&lt;br /&gt;
* If not specified, the zone guard attacks any enemy coming inside its guard zone.&lt;br /&gt;
* If [filter_location_enemy] is given, it attacks any enemy entering this zone and once there are no more enemies, it goes back to patrol in its basic zone.&lt;br /&gt;
&lt;br /&gt;
Enable the Zone Guardian Micro AI by using&lt;br /&gt;
 ai_type=zone_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone to guard.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_enemy]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone where enemies have to be attacked.  If not given, it defaults to [filter_location].&lt;br /&gt;
* '''station_x,station_y''': By default, the zone guardian moves randomly within the zone if there is no enemy to attack.  However, if station_x/y is given, it moves to this position instead during turns when it does not have an enemy to attack.  This position does not need to be inside the zone defined above.  Note: obviously it does not make sense to define [filter_location], [filter_location_enemy] and station_x/y all at the same time.  Only [filter_location] and either one of the latter two (or none of them) should be provided for any zone guardian. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a zone guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=zone_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=zone3&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=22-31,4-11  # This is intentionally chosen to extend past the lake&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             station_x,station_y=32,8&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Hang Out Micro AI (ai_type=hang_out) ==&lt;br /&gt;
&lt;br /&gt;
The Hang Out Micro AI instructs units of the AI side to &amp;quot;hang out&amp;quot; around a certain location, or locations, until a condition for mobilizing the side is met.  If none of the optional keys are given, the AI controls all units of the side to hang around the leader while moving off castle tiles to make room for recruiting.&lt;br /&gt;
&lt;br /&gt;
The AI works by moving each unit to its best hex first using the minimum amount of movement points required for this move.  Once all AI units are in position, movement points are set to zero for all of them simultaneously such that they cannot be used by other AI candidate actions.  This is done because the mobilizing condition might be met as result of one of the moves of the Hang Out AI, after which we want units to be able to continue with other AI actions.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Hang Out and Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Hang Out Micro AI by using&lt;br /&gt;
 ai_type=hang_out&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Hang Out specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  In its default configuration, the Hang Out MAI controls all units of a side, keeping them as close to the side leader(s) as possible while moving off castle tiles to make room for recruiting.  For this purpose, the candidate action score defaults to 170,000, which is just below the score of the default recruiting CA.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''': A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  If neither of these two exists, this parameter is set to default to all castle terrain (&amp;lt;tt&amp;gt;terrain=C*,C*^*,*^C*&amp;lt;/tt&amp;gt;) so that units move off the castle to make room for recruiting.  Note that this does not include keep terrain, otherwise the leader would be moved off the keep as well.&lt;br /&gt;
* '''ca_score'''=170000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 170,000, which means that it is executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Hang Out action (&amp;lt;tt&amp;gt;side=&amp;lt;/tt&amp;gt; is always set and does not need to be provided).  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the locations ''near'' which the AI units should hang out.  This does not mean that the units only move onto terrain specified here, but that they minimize the distance to such terrain.  It defaults to the position of the side leader(s).&lt;br /&gt;
* '''[mobilize_condition]''':  Contains ([[ConditionalActionsWML#Condition_Tags|WML Condition Tags]])  Once the condition set up here has been met, the Hang Out MAI is disabled and the side goes over to other AI activity.  The Hang Out MAI remains disabled after this, even if the condition is not met any more later in the scenario.&lt;br /&gt;
* '''mobilize_on_gold_less_than''': (number) Since side gold cannot be checked in a WML condition tag without storing it in a variable first, this key is provided separately.  It disables the Hang Out Micro AI once the side gold goes below the value specified here for the first time.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
* The Hang Out AI still takes effect if no locations to hang out at are found or if all map locations are to be avoided.  In that case, it simply keeps all units at their current locations.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Hang Out Micro AI configuration from the &amp;quot;Hang Out and Messenger&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hang_out&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [avoid]&lt;br /&gt;
                 terrain=C*,H*,M*,A*,S*,*^F*&lt;br /&gt;
             [/avoid]&lt;br /&gt;
             [mobilize_condition]&lt;br /&gt;
                 [have_unit]&lt;br /&gt;
                     side=1&lt;br /&gt;
                     count=7-99&lt;br /&gt;
                 [/have_unit]&lt;br /&gt;
             [/mobilize_condition]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Healer Support Micro AI (ai_type=healer_support) ==&lt;br /&gt;
&lt;br /&gt;
The Healer Support Micro AI configures the healers of a side to stay behind the battle lines and heal injured and/or threatened units rather than participate in combat under all circumstances.  You can set different levels of aggressiveness for the healers.  &lt;br /&gt;
&lt;br /&gt;
'''Notes'''&lt;br /&gt;
* By default, ''all'' healers of the AI side are controlled by this AI.  This includes in particular also the side leader, if the leader is a healer.  If you do not want the leader to be included (and possibly be moved away from the keep etc.) use the [filter] tag with canrecruit=no (see below).&lt;br /&gt;
* It is not always better to use this Micro AI than the default AI, as it takes healers (which can be strong units) away from the pool of units to choose for attacks.&lt;br /&gt;
* {{DevFeature1.15|3}} This Micro AI now takes unit guardian status and the passive_leader aspect into account.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Healers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Healer Support Micro AI by using&lt;br /&gt;
 ai_type=healer_support&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Healer Support specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the healer units.  If set, only units on the AI side passing this filter ''and'' having the healing ability are considered by this Micro AI.&lt;br /&gt;
*'''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting units to receive healing. If set, only units on the AI side passing this filter ''and'' the other healee selection criteria will be selected for healing by this Micro AI.&lt;br /&gt;
* '''aggression'''=1.0: (float) Sets the aggressiveness of the AI.  This parameter is set up as a float to accommodate future functionality, but it currently acts as a boolean: if set to zero, the AI will never let its healers participate in combat, for all other values it allows them to attack after all other units have attacked and if the healers cannot find any more units to support.  The former behavior might be appropriate in scenarios with only one or few valuable healers, while the latter might work better in scenarios with many healers.&lt;br /&gt;
* '''injured_units_only'''=no: (boolean) If set to 'yes', the AI will only move healers next to units that are already injured and skip units that currently have full hitpoints, but might get injured during the next enemy turn.&lt;br /&gt;
* '''max_threats'''=9999: (integer) The maximum allowable number of enemies that can attack a hex in order for it to be considered for a healer.  As an example, setting 'max_threats=0' means that the AI only moves healers to locations that are entirely safe from the enemy (assuming that none of the units currently on the map dies).  Note that the value of this key is checked against the number of enemies that can make it to the hex, not the number of adjacent hexes from which the healer could be attacked.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI. Note: as attacks by the healers are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The Healer Support Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded.  It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Healer Support Micro AI configuration from the &amp;quot;Healers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             aggression=0&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Lurkers Micro AI (ai_type=lurkers) ==&lt;br /&gt;
&lt;br /&gt;
The Lurker Micro AI controls some or all units of a side in &amp;quot;lurker mode&amp;quot;.  A lurker is a unit that is capable of moving across most terrains, but that only stops on and attacks from specific terrain.  It might also have the ability to hide on this terrain (which is the reason why it is called a lurker).&lt;br /&gt;
&lt;br /&gt;
Lurkers move individually without strategy and always attack the weakest enemy within their reach.  If no enemy is in reach, the lurker does a random move instead - or it just sits and waits (lurks).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of different types of lurker behavior, check out the &amp;quot;Lurkers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Lurkers Micro AI by using&lt;br /&gt;
 ai_type=lurkers&lt;br /&gt;
in the [micro_ai] tag&lt;br /&gt;
&lt;br /&gt;
===Lurkers specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
* '''[filter]''': Specifies which units of the side are controlled by the Lurker Micro AI.  This is a [[StandardUnitFilter|Standard Unit Filter]].  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': Specifies the terrain from which the lurkers attack. This is a [[StandardLocationFilter|Standard Location Filter]].&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_wander]''': Specifies the terrain on which lurkers end their moves if there is no enemy in reach. This is a [[StandardLocationFilter|Standard Location Filter]].  If [filter_location_wander] is not given, it defaults to [filter_location].&lt;br /&gt;
* '''stationary'''=no: (boolean)  If set to yes, a lurker never moves if there is no enemy within attack range.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Lurkers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=4&lt;br /&gt;
             ai_type=lurkers&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Naga Fighter&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=W*,S*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             [filter_location_wander]&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location_wander]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Messenger Escort Micro AI (ai_type=messenger_escort) ==&lt;br /&gt;
&lt;br /&gt;
The Messenger Escort Micro AI lets you define a set of waypoints through which it tries to move one or several of its units, the messenger(s).  The other units escort the messenger, trying to protect it and attacking enemies that are in its way.  Note that a messenger might not strictly hit intermediate waypoints, but that getting close is good enough, especially if a waypoint is occupied by an enemy unit.  It does, however, always try to get to the last waypoint.&lt;br /&gt;
&lt;br /&gt;
'''Notes on the use of several messengers:''' {{DevFeature1.13|0}}  &lt;br /&gt;
* This Micro AI can control several messengers at the same time, which can but do not have to move in a group.  They can be put on the map altogether in one location, or be spread out on the map or across different turns.&lt;br /&gt;
* The farther a messenger has moved through the chain of waypoints, the higher a priority it is for the AI, meaning that farther advanced (on the map) messengers are moved first, and that the escort units try to clear the way and stay with those messengers more so than with those further back on the map.  This order can be inverted using the invert_order= key.&lt;br /&gt;
* If several messengers are on the map, escort units might move from one to the other, the AI making a judgement call where they are needed most.  Closeness to a messenger is generally the most important criterion, but not the only one.&lt;br /&gt;
* If a messenger dies, the escort units previously staying with it now join one of the other messengers if there are any left (otherwise they follow default AI behavior).&lt;br /&gt;
* If you want certain escort units to stay always with the same messenger, use several messenger [micro_ai] tags with different [filter]/[filter_second] combinations.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Messenger Escort Micro AI by using&lt;br /&gt;
 ai_type=messenger_escort&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Messenger Escort specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': {{DevFeature1.13|0}} (Previous to 1.13.0, only id= was allowed.) The id of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated list of waypoint coordinates through which the messenger will go in the given order to reach its goal, given by the last coordinate.  If you want the messenger to go directly to the goal, simply enter a single x,y coordinate here.  Note that the messenger does not have to hit each waypoint exactly (except for the final one), getting close to them is good enough. {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Messenger Escort AI consists of 3 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-2&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_death_chance'''=0.67 and '''messenger_death_chance'''=0.0: (both floats; ranging from 0 to 1)    If the messenger is adjacent to an enemy at the end of the move, it will only attack the enemy if the chance of death of the enemy is &amp;gt;= enemy_death_chance and if the messenger's chance of death is &amp;lt;= messenger_death_chance (or if it is an attack at a range for which the enemy does not have a weapon).&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|0}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the escort units.  If not set, this defaults to all enemy units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''invert_order'''=no: {{DevFeature1.13|0}} If set to 'yes', the order of priority with which messenger units are considered is inverted, starting with the messenger unit that is the least advanced on the map (see description above).  In most cases, selecting this option results in worse performance of the AI, so use it with caution.  (And obviously it has no effect at all if there is only one messenger controlled by the AI.)&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Messenger Escort&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=messenger_escort&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=messenger&lt;br /&gt;
             waypoint_x=31,24,27,28&lt;br /&gt;
             waypoint_y=20,14,7,1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Patrol Micro AI (ai_type=patrol) ==&lt;br /&gt;
&lt;br /&gt;
The defines a number of waypoints for a unit (or units) to follow.  Options can be set whether the route should be followed in a loop, as an out-and back, only once, whether units encountered along the way should be attacked, etc.&lt;br /&gt;
&lt;br /&gt;
No matter what options are chosen, getting to the next waypoint always takes priority over attacking for a patroller.  The AI thus prefers to move the patrol unit around enemies rather than straight for them.  Also, if a waypoint is occupied by a unit the AI is not instructed to attack, it will (eventually) abandon that waypoint once it gets close enough and move on to the next one.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different patrol behaviors, check out the &amp;quot;Patrols&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Patrol Micro AI it by using&lt;br /&gt;
 ai_type=patrol&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Patrol specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated lists of the waypoint coordinates through which the patrol travels (in order). {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack''': By default (if this key is not set), the patrol unit attacks any unit it ends up next to after a move.  Alternatively, this parameter can be set to a comma-separated list of unit ids. If that is done, the patrol only attacks these units when it ends up next to them and ignores any other unit.  This can in turn be used to have the patroller not attack at all, by setting ''attack='' to a non-existing id.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''one_time_only'''=no: (boolean)  If set to 'yes', the unit goes through the patrol route only once and then hangs out at the last waypoint.  It also always attacks any enemy unit on the last waypoint, independent of what ''attack='' is set to.&lt;br /&gt;
* '''out_and_back'''=no: (boolean)  By default, a patrol unit returns to the first waypoint after reaching the last, doing the patrol route as a loop.  If this parameter is set to 'yes', the unit instead reverses its course and perpetually does out and backs of the route.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Patrols&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=patrol&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Konrad&lt;br /&gt;
             waypoint_x=9,24,25&lt;br /&gt;
             waypoint_y=21,23,15&lt;br /&gt;
             one_time_only=yes&lt;br /&gt;
             attack=Gertburt&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Protect Unit Micro AI (ai_type=protect_unit) ==&lt;br /&gt;
&lt;br /&gt;
The Protect Unit Micro AI tries to move one or several units to a given location while keeping them safe from enemy attacks. It does so by keeping the protected units away from enemy units and close to friendly units. Other units on the AI side are controlled by the default Wesnoth AI.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Protect Unit&amp;quot; and &amp;quot;The Elves Besieged&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Protect Unit Micro AI by using&lt;br /&gt;
 ai_type=protect_unit&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Protect Unit specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[unit]''': A separate [unit] tag is required for each protected unit(s).  Each tag has required keys '''id''', '''goal_x''', '''goal_y''' for each unit, as shown in the example below. {{DevFeature1.15|0}} '''goal_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''disable_move_leader_to_keep'''=no: (boolean) If set to 'yes', side leaders protected by this AI do not try to return to their keep.  This is necessary for this Micro AI to work with units that are side leaders and that are supposed to move to an off-keep location.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
*The Protect Unit Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].  Depending on the parameters, it might also modify the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|move leader to keep candidate action]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded. It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;HttT: The Elves Besieged&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=protect_unit&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Delfador&lt;br /&gt;
                 goal_x,goal_y=1,2&lt;br /&gt;
             [/unit]&lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
                 goal_x,goal_y=1,1&lt;br /&gt;
             [/unit]&lt;br /&gt;
 &lt;br /&gt;
             disable_move_leader_to_keep=true&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Recruiting Micro AI==&lt;br /&gt;
=== General Comments on the Recruiting Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Recruiting Micro AI replaces the default recruitment AI by alternative recruiting patterns.  Currently there are two recruiting algorithms you can choose from, random recruiting and the rush recruiting used in the new (as of Version 1.11.1) Experimental AI (see below).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of a different recruiting pattern, check out the &amp;quot;Recruiting&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  You can also explore the unit choices by watching the Experimental AI in a multiplayer game.&lt;br /&gt;
&lt;br /&gt;
'''Note:''' Obviously, the Recruitment Micro AI modifies the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|recruitment candidate action]].&lt;br /&gt;
&lt;br /&gt;
=== Random Recruitment AI (ai_type=recruit_random) ===&lt;br /&gt;
&lt;br /&gt;
This AI randomly selects a unit type from the recruitment list of the side.  The probability of choosing each type and the behavior in low-gold situations can be influenced with optional parameters.&lt;br /&gt;
&lt;br /&gt;
Enable the Random Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_random&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* '''[probability]''': This tag sets the probability of selecting the given unit type.  It has two required sub-keys:&lt;br /&gt;
** '''type''': comma-separated list of unit types to which this probability is to be applied&lt;br /&gt;
** '''probability''': (non-negative float)  The probability with which these unit types is to be selected.  All unit types for which no [probability] tag is defined receive a default probability of 1.0, thus values should be chosen relative to that.  As an example, in the code below swordsmen and peasants are given a probability of 8, while no other units have assigned probabilities.  Thus, each of these two unit types is 8 times more likely to be selected than any other individual unit type.&lt;br /&gt;
* '''skip_low_gold_recruiting'''=no: (boolean)  By default, the random recruitment AI chooses a unit to recruit only from those types for which it has sufficient gold.  If this parameter is set to 'yes', it will instead choose from all available unit types and end recruiting for the turn if there is not enough gold to recruit it this turn (even if other, affordable unit types exist).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a random recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_random&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [probability]&lt;br /&gt;
                 type=Swordsman,Peasant&lt;br /&gt;
                 probability=8&lt;br /&gt;
             [/probability]&lt;br /&gt;
 &lt;br /&gt;
             skip_low_gold_recruiting=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Rush Recruitment AI (ai_type=recruit_rushers) ===&lt;br /&gt;
&lt;br /&gt;
This AI replaces the default recruiting with the rush recruiting used in the new (as of Version 1.11.1) Experimental AI.&lt;br /&gt;
&lt;br /&gt;
Enable the Rush Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_rushers&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* {{DevFeature1.15|2}} '''high_level_fraction'''=0: (non-negative number) The approximate fraction of units of level 2 or higher to be recruited. This is defined as fraction of units on the map, not of new units being recruited during a turn or over several turns (which makes a difference if there are already units on the map). The effect is also cumulative per level (starting from level 2), meaning that 1/3 of L2 units will be recruited in the example above, 1/3*1/3=1/9 L3s etc. The default value is zero, which leaves the high-level unit recruiting to the default Experimental AI algorithm resulting usually in very few such units being recruited.&lt;br /&gt;
* '''randomness'''=0.1: (number)  A random number is applied to the rusher recruit engine's score to prevent the recruitment pattern from being too predictable. 0 causes no randomness to be applied, while larger numbers increase the random effect. A value of 1-2 generates results in which the random effect is approximately equal to the scored effect. Extremely high values are essentially entirely random.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a rush recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_rushers&lt;br /&gt;
             action=add&lt;br /&gt;
          [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Simple Attack Micro AI (ai_type=simple_attack) ==&lt;br /&gt;
&lt;br /&gt;
The Simple Attack Micro AI is meant as an addition to the default combat candidate action for when one wants to force the AI to do attacks that it would otherwise not do (for example on units that are 1 XP from leveling) or to execute certain attacks before all others (for example, attack with all &amp;quot;expendable&amp;quot; units first).  It is not set up to be a replacement for the default attacks.  Some notes:&lt;br /&gt;
&lt;br /&gt;
* The algorithm is different from and simpler than that of the default combat CA.  For example, no attack combinations are considered, only individual attacks.  As a result, the [[AiWML#List_of_AI_Aspects|default aspects]] (aggression, caution, [avoid], ...) are not taken into account.  Also, if lots of units are involved, evaluation of this Micro AI is potentially quite a bit slower than of the default attacks.&lt;br /&gt;
&lt;br /&gt;
* If an attack matching the Standard Unit Filters described below is found, this attack will always be executed, no matter how bad the odds are.  Thus, this Micro AI can be used to force the AI to do attacks it would otherwise avoid.&lt;br /&gt;
&lt;br /&gt;
* Other than selecting the attacker/defender units, this AI is currently not configurable.  Let us know if you would like to see certain options to be added.  (No guarantees that we will add them, but we will definitely consider them.)&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Simple Attack&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=simple_attack&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Simple Attack specific keys for the [micro_ai] tag===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  It is, however, in general not desirable to use this AI without at least one of the optional Standard Unit Filters below, as this AI is not meant as a replacement of the default combat candidate action.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=110000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 110,000, which means that it is executed right before the default combat candidate action.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Simple Attack action.  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units the AI should attack.  It defaults to all enemy units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
* '''weapon'''=-1: (integer) The number of the weapon that should be used for this attack.  Weapon numbers start at 1 (not 0).  -1 stands for &amp;quot;best weapon&amp;quot; (by whatever criterion the AI chooses to use).   '''Available from Wesnoth 1.11.8'''&lt;br /&gt;
&lt;br /&gt;
Below are 2 examples of Simple Attack Micro AI configurations from the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  The first one shows how to force the AI to attack with all Soulless and Walking Corpse units first:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110000&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless,Walking Corpse&lt;br /&gt;
             [/filter]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
The second example executes attacks by Soulless on all enemy units that are 1 XP from leveling.  Note that the Lua code in the preload event is there simply in order to code the &amp;quot;1 XP from advancing&amp;quot; condition.  It is not needed for most other SUFs, as can be seen in the previous example.&lt;br /&gt;
     [event]&lt;br /&gt;
         name=preload&lt;br /&gt;
         first_time_only=no&lt;br /&gt;
         [lua]&lt;br /&gt;
             code=&amp;lt;&amp;lt;&lt;br /&gt;
                 function close_to_advancing(unit)&lt;br /&gt;
                     if (unit.experience &amp;gt;= unit.max_experience-1) then&lt;br /&gt;
                         return true&lt;br /&gt;
                     else&lt;br /&gt;
                         return false&lt;br /&gt;
                     end&lt;br /&gt;
                 end&lt;br /&gt;
             &amp;gt;&amp;gt;&lt;br /&gt;
         [/lua]&lt;br /&gt;
     [/event]&lt;br /&gt;
 &lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110001&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless  # No Walking Corpses; L0 units don't advance enemy&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 lua_function = &amp;quot;close_to_advancing&amp;quot;&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
Several Simple Attack Micro AIs can be combined for the same AI side.  The two examples above are both used (note the different values for ca_score) in the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
== Custom Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|5}}&lt;br /&gt;
&lt;br /&gt;
From 1.13.5 onwards, adding a new Micro AI can be easily done without altering any core files or overriding the micro_ai tag. The [micro_ai] tag now looks up its ''ai_type'' parameter in the &amp;lt;code&amp;gt;wesnoth.micro_ais&amp;lt;/code&amp;gt; table and uses the micro AI setup function found there to determine which keys are required and allowed and what candidate actions to add.&lt;br /&gt;
&lt;br /&gt;
A micro AI setup function takes as its single parameter the contents of the [micro_ai] tag and returns three separate values:&lt;br /&gt;
*'''required''': A list of required keys and tags, as strings. Tags are indicated by enclosing them within square brackets in the string. If a key or tag in this list is missing from the [micro_ai] tag, an error will be printed.&lt;br /&gt;
*'''optional''': Similar to above, a list of optional keys and tags, as strings. No error will be printed if a key or tag in this list is missing, nor will an error be printed if it is present (as would be the case for unrecognized keys or tags).&lt;br /&gt;
*'''ca_params''': A table specifying the candidate actions that constitute the micro AI. It should contain an ''ai_id'' key (which specifies a base ID to be used for candidate actions of this micro AI) and a series of candidate action definitions. Each candidate action definition is a table containing the following keys:&lt;br /&gt;
**'''ca_id''': The ID used for the candidate action. This will be combined with the ''ai_id'' to produce the final ID.&lt;br /&gt;
**'''location''': Path to the Lua file that defines the candidate action.&lt;br /&gt;
**'''score''': The maximum score for the candidate action. If the [micro_ai] tag specified a ''ca_score'' parameter, the score here should normally be based on that.&lt;br /&gt;
&lt;br /&gt;
(The return statement would thus look like &amp;lt;syntaxhighlight lang='lua' inline&amp;gt;return required, optional, ca_params&amp;lt;/syntaxhighlight&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
Since the function has full access to the contents of the [micro_ai] tag, it can conditionally add candidate actions to '''ca_params''' depending on the values of other keys within the [micro_ai] tag. It can also perform other actions such as adding facets to an aspect; if it does such things, it should pay attention to the ''action'' key to determine whether it's being added or removed.&lt;br /&gt;
&lt;br /&gt;
There are two useful helper functions that can be used by Micro AI setup functions requiring aspect changes. They can be brought into scope by a line similar to the following:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='lua&amp;gt;local MAIH = wesnoth.require(&amp;quot;ai/micro_ais/micro_ai_helper.lua&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* MAIH.add_aspects(side, aspect_params)&lt;br /&gt;
* MAIH.delete_aspects(side, aspect_params)&lt;br /&gt;
&lt;br /&gt;
The ''aspect_params'' parameter is an array of tables, each of which contains the following keys:&lt;br /&gt;
*'''aspect''': The aspect to change.&lt;br /&gt;
*'''facet''': The WML defining the facet. This must include an ''id'' key in order for the aspect to be correctly deleted. For maximum compatibility, incorporate the ''ca_id'' key from the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=65890</id>
		<title>Micro AIs</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=65890"/>
		<updated>2020-08-10T20:14:43Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Micro AI Test and Demo Scenarios */ change order of methods to start test scenarios&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== A Few General Words about Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
* Micro AIs allow Wesnoth campaign and scenario authors to add new AI functionality without the need for any AI programming. They bring new specialized behavior to Wesnoth that cannot be achieved, at least not easily, by adjusting default AI parameters (such as those described at [[AiWML]]) or with WML code.&lt;br /&gt;
&lt;br /&gt;
* A Micro AI is activated and configured (or deleted) via the [micro_ai] tag, requiring only a few lines of WML code.&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.6''', the Micro AI behavior was inconsistent when it came to '''hidden enemies and petrified units'''.  Some Micro AIs ignored those, while others did not.  In some case there was even a potential for AI errors when an AI unit ran into hidden or petrified enemies.  As of 1.13.6, this has all been fixed.  The Micro AIs now correctly and consistently ignore hidden units and treat petrified units as blocking the hex they are on but having no effect otherwise, in the same way as the default AI does.  '''Important notes:'''&lt;br /&gt;
** It is not possible to re-include hidden/petrified units via the Standard Unit Filters (SUFs) used by some of the Micro AIs. In fact, if this is attempted, the filters will generally not match any unit.&lt;br /&gt;
*** This only applies to SUFs which go directly into the [micro_ai] tag. Any SUF that is a sub-tag of another filter (for example, of a Standard Location Filter) is taken literally and returns all units matching the filter. If hidden or petrified units are to be excluded from such a sub-filter, that needs to be specified directly in that filter.&lt;br /&gt;
** We also note that, because all the Micro AIs are written in Lua, it is possible to [[Micro_AIs#Using_Development_.281.13.29_Version_Micro_AIs_in_your_Stable_.281.12.29_Version_Add-on|move a Micro AI's code into one's add-on]] and modify it to whatever scenario-specific behavior is desired (this does not only apply to hidden/petrified units, but to all behavior). Setting up general Micro AI behavior that works in every possible setting is not feasible, but adapting it to work (only) in the specific setting of a given scenario is often quite simple. If you do not know how to do this, please post a message on the Wesnoth forums and we will be happy to help you with it.&lt;br /&gt;
&lt;br /&gt;
* The '''[[AiWML#List_of_AI_Aspects|default AI aspects]] are, for the most part, not taken into account by the Micro AIs'''.  For example, unless noted otherwise, specifying a value for ''aggression'' does not change the MAI behavior, and locations specified in an ''[avoid]'' tag generally can be entered by units controlled by an MAI.  This is a feature of the Micro AIs, not a bug.  If you feel that a certain default AI aspect should also apply to a Micro AI, please let us know in the [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 Micro AIs Feedback thread].&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.7''', the Micro AIs did not all work reliably when the '''AI side is under shroud''' and it may have been necessary to turn shroud off for the side when using a Micro AI. {{DevFeature1.13|7}} The Micro AIs now all work for sides under shroud. They do this by disregarding shroud for terrain purposes when path finding, consistent with default Wesnoth AI behavior, while still ignoring hidden units (including those hidden under shroud) correctly.&lt;br /&gt;
&lt;br /&gt;
* Most Micro AIs in their default setting apply to all units of a side, although many of them provide parameters for filtering the units that should be affected by the new behavior.  If the Micro AI is applied only to part of the units of a side, the other units follow the default Wesnoth AI behavior.&lt;br /&gt;
&lt;br /&gt;
* Micro AIs are available in mainline since Wesnoth 1.11/1.12.&lt;br /&gt;
&lt;br /&gt;
== Micro AI Test and Demo Scenarios ==&lt;br /&gt;
&lt;br /&gt;
All Micro AIs described here can be checked out in a number of test and demo scenarios.  The easiest way to access the test scenarios is by assigning a hotkey to &amp;quot;Start Test Scenario&amp;quot; in the preferences (as of Wesnoth 1.13.8). Then, simply press the hotkey in the titlescreen and select the desired scenario from the list (e.g. 'micro_ai_test' for the overall &amp;quot;switchboard scenario&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Test scenarios can also be accessed from the command line by typing&lt;br /&gt;
 path/wesnoth-executable -t micro_ai_test&lt;br /&gt;
or (depending on your system and Wesnoth version)&lt;br /&gt;
 path/wesnoth-executable -tmicro_ai_test&lt;br /&gt;
This starts up a &amp;quot;switchboard scenario&amp;quot; in which you can select the Micro AI demonstration scenario you want to check out.  Here, ''path/wesnoth-executable'' needs to be replaced by whatever the path and filename of the Wesnoth executable is on your system.&lt;br /&gt;
&lt;br /&gt;
== Setting up a Micro AI ==&lt;br /&gt;
&lt;br /&gt;
Setting up a Micro AI requires only one simple step:&lt;br /&gt;
==== Activating and configuring (or deleting) a Micro AI ====&lt;br /&gt;
&lt;br /&gt;
Micro AIs are activated, deleted and configured using the [micro_ai] tag.  This tag needs to be placed in [[ActionWML]], that is, in an event, a menu option or the like.  As an example, the following code activates the healer_support Micro AI in its default configuration for Side 2 from the beginning of the scenario:&lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
 &lt;br /&gt;
         # Configure the healer support micro AI&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
'''Keys required for all Micro AIs:'''&lt;br /&gt;
All [micro_ai] tags must contain the following three required keys:&lt;br /&gt;
* '''side''': The side for which the Micro AI is to be added, changed or deleted.  This has to be an individual side number, lists of sides are not supported.&lt;br /&gt;
* '''ai_type''': The type of Micro AI to be added, changed or deleted.  See the following sections for allowed values.&lt;br /&gt;
* '''action''' (string): The action to take concerning the Micro AI.  The following values are allowed: &lt;br /&gt;
** ''add'': Add a Micro AI to a side.&lt;br /&gt;
** ''change'': Change the configuration of an existing Micro AI.  Note that this does not only change the specific parameters provided in the tag, but it replaces the entire existing Micro AI by a new version with the new (and only the new) configuration.  It is therefore equivalent to using first the ''delete'' and then the ''add'' action.&lt;br /&gt;
** ''delete'': Delete an existing Micro AI from a side.&lt;br /&gt;
&lt;br /&gt;
'''Keys required under certain circumstances:'''&lt;br /&gt;
* '''ca_id''': (string) If several MAIs of the same type are used for the same side and one or several of these are to be changed or deleted during a scenario, this key needs to be included so that the [micro_ai] tag knows which of them needs to be changed/deleted.  The same is also true if a Micro AI is changed more than once during a scenario.  We therefore '''strongly recommend always setting ''ca_id''''' for MAIs which are, or might be, changed or deleted during a scenario.&lt;br /&gt;
** '''Note:''' The id(s)/name(s) actually assigned to the CA(s) of the MAI by the engine might be different from the parameter passed here.  It is, however, sufficient to use this ca_id with the [micro_ai] tag add/delete/change actions to identify the MAI uniquely.  (In other words: you don't have to worry about this if you always use the [micro_ai] tag, but might have to figure out the actual name(s) of the CA(s) if you want to delete or change them manually.)&lt;br /&gt;
* Other keys may also be required depending on the Micro AI.  In additional, a number of optional keys may be available to configure the AI behavior.  See the sections on the individual Micro AIs for details.&lt;br /&gt;
&lt;br /&gt;
'''Required keys for deleting a Micro AI:'''&lt;br /&gt;
&lt;br /&gt;
Only a subset of the keys required for setting up a Micro AI are needed when deleting it.  '''side''', '''ai_type''' and '''action'''=delete always need to be provided.  '''ca_id''' needs to be given if it was provided when adding the Micro AI (see above for when this is needed).&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* The ''add'' and ''change'' actions ignore all keys that do not apply to the respective Micro AI type.  The ''delete'' action ignores all keys other than the required keys listed above.&lt;br /&gt;
&lt;br /&gt;
* Some of the Micro AIs use and/or modify default AI components (candidate actions or aspects) when they are added, or reset these components to their default values when they are deleted.  Thus, if you have modified the AI yourself using the default CAs or aspects, some of these modifications might be affected, or might be interfering with the Micro AI.  The sections below indicate whether a Micro AI modifies any default AI components.&lt;br /&gt;
&lt;br /&gt;
===Combining Different Micro AI Types for the Same Side===&lt;br /&gt;
&lt;br /&gt;
No extra steps need to be taken when combining different Micro AIs on the same side.  Simply set up the [micro_ai] tags for all AIs you want to use.&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* While it is ''technically'' possible to combine any number of Micro AIs, this does not mean that all Micro AIs work together in practice.  Some are not compatible because they control units in contradictory ways.  &lt;br /&gt;
* Not all combinations that ''should'' work have been tested yet.  If you find a combination that you believe should work but doesn't, [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 please let us know] and we will see if that can be fixed.&lt;br /&gt;
* The candidate action score almost certainly has to be set (using the ca_score= key) for one or several of the MAIs if several of them are used on the same side.&lt;br /&gt;
&lt;br /&gt;
== Animals Micro AIs ==&lt;br /&gt;
=== General Comments on the Animal Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Animals Micro AIs are meant to simulate the behaviors of certain types of animals.  They are, however, set up so that they can be used with arbitrary unit types and might be applicable in quite diverse situations.  The AIs nevertheless remain named after the animals for which they were written originally, so that they are (to some extent) descriptive of their behavior.&lt;br /&gt;
&lt;br /&gt;
Brief descriptions of the AI behaviors are given in the following subsections.  To get a better feeling for their behavior, check out the &amp;quot;Animals&amp;quot;, &amp;quot;Swarm&amp;quot;, &amp;quot;Wolves&amp;quot; and &amp;quot;Dragon&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  Note that some of the animal AIs are designed for animal types that do not exist in mainline.  In the test scenario, other animal types are therefore substituted for them.&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Wolves (ai_type=wolves) ===&lt;br /&gt;
&lt;br /&gt;
The Wolves Micro AI organizes all &amp;quot;wolf&amp;quot; units (predators) of the side to hunt in a single pack.  They actively chase after the closest prey and try to corner it (not always super successfully), but are easily distracted by other enemy units coming into attack range, to the point where the pack might split up.  The wolves can, however, be told to attack only prey units as well as to try to avoid other types of units (such as larger predators).  When no prey is left, the wolves wander randomly.&lt;br /&gt;
&lt;br /&gt;
This AI works best for a small number of predators (2 - 5) and sparse prey.  Also, the predators should be put on the map in a pack, that is, reasonably close to each other.  For a demonstration of the Wolves AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Note that another wolves AI exists, called [[Micro_AIs#Animals_AI:_Multi-pack_Wolves_.28ai_type.3Dwolves_multipacks.29|Multipack Wolves]], which distributes the wolves of a side into several packs, rather than all of them into one single pack as it is done here.&lt;br /&gt;
&lt;br /&gt;
Enable the Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the predator units.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the prey units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_only_prey'''=no: In the default configuration, the wolves attack all enemy units in attack range.  If this key is set to 'yes', they only attack prey units.&lt;br /&gt;
* '''avoid_type''': Comma-separated list of unit types (on enemy sides) which the wolves try to avoid.  They try to stay out of these units' way as much as possible when moving around (except when they are moving in to attack another enemy unit) and never attack them.&lt;br /&gt;
* '''ca_score'''=90000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 90,000, which is lower than the default combat CA, but higher than most other default CAs which move units.  It can be changed by setting this parameter but in most cases this will not make sense.  Note that the Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Notes on default (RCA) AI aspects:''' &lt;br /&gt;
* If ''attack_only_prey'' or ''avoid_type'' are set, the Wolves Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* Attacks by the wolves are left to the default (RCA) AI, meaning that the value of ''aggression'' has an influence on the behavior.&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI is taken into account. Note: as attacks by the wolves are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=6&lt;br /&gt;
             ai_type=wolves&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Wolf&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Vampire Bat&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             avoid_type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Multi-pack Wolves (ai_type=wolves_multipacks) ===&lt;br /&gt;
&lt;br /&gt;
The Multi-pack Wolves Micro AI is different from the [[Micro_AIs#Animals_AI:_Wolves_.28ai_type.3Dwolves.29|Wolves Micro AI]] in that there can be an arbitrary number of wolf packs, with the pack size being a free parameter.  At the beginning of the scenario, close wolves are grouped into packs in a semi-methodical way.  If the wolves are put on the map in distinct groups of close wolves of the correct number, they will be joined into packs matching these groups.  If, on the other hand, they are spread out all over the map, the method of assigning them to packs is semi-random.&lt;br /&gt;
&lt;br /&gt;
Wolves of the same pack begin by joining each other on the map.  After that, they stay together until only one wolf is left, which then tries to join up with an incomplete pack or with other single wolves.  Individual wolves entering the map during the scenario behave in that way as well.&lt;br /&gt;
&lt;br /&gt;
A second difference to the other Wolves AI is that wolves do not actively hunt here.  For the most part they just wander (often long distance).  However, the pack ferociously (and without regard for its own health) attacks any enemy units that come into range, as long as that does not mean separating the pack by more than a few hexes.  Staying together, or joining with a new wolf assigned to the pack, is the only thing that takes priority over satisfying the wolves' thirst for blood.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Multi-pack Wolves AI behavior, check out the &amp;quot;Wolves&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Multi-pack Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves_multipacks&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Multi-pack Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''pack_size'''=3: (integer) The size of the packs.&lt;br /&gt;
* '''show_pack_number'''=no: If set to &amp;quot;yes&amp;quot;, the wolves' pack numbers will be shown below them.  Note that this means that the pack number of a wolf killed in an attack will remain as a label on the map.  &lt;br /&gt;
* '''type'''=Wolf: Comma-separated list of the unit types which the AI controls. The default value is &amp;quot;Wolf&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves_multipacks [micro_ai] tag usage from the &amp;quot;Wolves&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=wolves_multipacks&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             show_pack_number=yes&lt;br /&gt;
             pack_size=4&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Big Animals (ai_type=big_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Big Animals Micro AI is a simulation of large predators that wander and hunt alone, such as Bears, Giant Spiders or Yetis.  For the most part, these just wander on the terrain that's been defined for them, but they attack enemy units if those happen to come into range.  The AI can be set up so that the Big Animals stay out of the way of other units (such as other large predators), in which case they only attack those if cornered or if they run into them accidentally (such as ambushers).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Big Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Big Animals Micro AI by using&lt;br /&gt;
 ai_type=big_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the animals to which this AI is applied.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': The big animals often go for long distance (multi-turn) journeys.  This tag is a [[StandardLocationFilter|Standard Location Filter]] describing the terrain from which the next goal is chosen.  The default is all terrain on the map. &lt;br /&gt;
* '''[filter_location_wander]''': The terrain on which the big animals will end their moves. This is a [[StandardLocationFilter|Standard Location Filter]].  The default is all terrain on the map.&lt;br /&gt;
* '''[avoid_unit]''': A [[StandardUnitFilter|Standard Unit Filter]] describing all enemy units that the big animals will avoid.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
Here's an example of big_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=big_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Ghast&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [avoid_unit]&lt;br /&gt;
                 type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
             [/avoid_unit]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=1-40,1-18&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Forest Animals (ai_type=forest_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Forest Animals Micro AI simulates a number of different animal behaviors that one might find in a forest, namely animals behaving like deer, rabbits and families of tuskers.  In the following, we will refer to those animals by these types, even though the actual unit types can be selected freely.  They behave as follows:&lt;br /&gt;
* Each deer wanders randomly on (selectable) terrain, except when enemies get in its (the deer's) maximum movement range, in which case it flees to the farthest point it can reach.&lt;br /&gt;
* Tuskers exhibit the same behavior as deer, except when an enemy is next to one of the tusklets.  This enemy is attacked unconditionally by the tuskers trying to protect their young.&lt;br /&gt;
* Tusklets blindly follow the closest adult tusker, except when there is no tusker left, in which case they behave the same as deer.&lt;br /&gt;
* Rabbits also wander randomly, but in addition disappear into their holes (if any are within reach) when enemies are close.  They reappear out of their holes at the beginning of the turn, if it is safe.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Forest Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Forest Animals Micro AI by using&lt;br /&gt;
 ai_type=forest_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Forest Animals AI consists of 4 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-3&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''rabbit_type, tusker_type, tusklet_type, deer_type''': Comma-separated lists of the unit types behaving as the respective animals.  If any of these parameters are not set, those types of animal behavior are skipped by the AI.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain on which the forest animals wander when they are not threatened by enemies.&lt;br /&gt;
* '''rabbit_number'''=6: (integer)  The number of rabbits for the AI to have on the map simultaneously.  The AI puts the missing number of rabbits onto the map at the beginning of the AI's turn (when the rabbits come out of their holes).&lt;br /&gt;
* '''rabbit_enemy_distance'''=3: (number)  Rabbits won't spawn in holes that aren't more than this distance away from all enemies.&lt;br /&gt;
* '''rabbit_hole_img''': Rabbit holes are marked by items on the map.  If this parameter is set, it must be the file name of the item image.  If it is not set, all items on the map are treated as rabbit holes.&lt;br /&gt;
** {{DevFeature1.14|10}} Items used for rabbit holes on the map border used to produce an error. Placing them on the map border therefore needed to be avoided on the scenario setup side. They are now automatically excluded from consideration by the AI code.&lt;br /&gt;
&lt;br /&gt;
Here's an example of forest_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=forest_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             deer_type=Vampire Bat&lt;br /&gt;
             rabbit_type=Giant Rat&lt;br /&gt;
             tusker_type=Ogre&lt;br /&gt;
             tusklet_type=Young Ogre&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=*^F*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Herd and Herders (ai_type=herding) ===&lt;br /&gt;
&lt;br /&gt;
The Herding Micro AI sets up an area on the map where a number of herder units (such as sheep dog) control and protect a herd (such as sheep).  For convenience, we simply refer to them as sheep dogs and sheep in the following, even though arbitrary unit types can be selected for either.&lt;br /&gt;
&lt;br /&gt;
Sheep dogs try to keep their sheep safe.  This involves keeping them inside the herding area, positioning themselves in between the sheep and approaching enemies, and attacking the enemies if those get too close.  If no active herding or protecting move is needed, the dogs go to a random location on the perimeter of the herding area.&lt;br /&gt;
&lt;br /&gt;
Sheep wander aimlessly except when a sheep dog is next to them, in which case they run away from the dog.  The dogs exploit this by positioning themselves on the outside of the sheep, if possible.  Sheep also run away from approaching enemies.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Herding AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Herding Micro AI by using&lt;br /&gt;
 ai_type=herding&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herder units (e.g. dogs).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herd units (e.g. sheep).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain forming the perimeter of the herding area.  In other words, this is the path the herders (dogs) use to patrol around the outside of the herding area.  This must be a set of coordinates ''completely enclosing'' the herding area.  Here's an example of setting up a rectangular perimeter:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         x=10-20,10-20,10,  ,20&lt;br /&gt;
         y=11,   21,   11-21,11-21&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
Another possibility is by defining the path by certain terrain on the map (as it is done in the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]), in which case the filter can simply look like this:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         terrain=Rb&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
* '''herd_x,herd_y''': The coordinate of one hex (''any'' one hex) inside the herding area.  This is needed to define which part of the map is on the ''inside'' of the herding area perimeter defined by [herding_perimeter], and which is the ''outside''. {{DevFeature1.15|0}} '''herd_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''attention_distance'''=8: (integer)  If an enemy is within this distance of a herd unit, but not as close as ''attack_distance'', the herders try to position themselves in between the enemy and the herd unit.&lt;br /&gt;
* '''attack_distance'''=4: (integer)  If an enemy is within this distance of a herd animal, the herders attack this enemy.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Herding AI consists of 6 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-5&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Herding [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=herding&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Footpad&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Troll Whelp&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             herd_x,herd_y=32,28&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=Rb&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Hunter (ai_type=hunter) ===&lt;br /&gt;
&lt;br /&gt;
The Hunter Micro AI leads a unit (or units) through hunt-and-rest cycles.  The units wander in a prescribed area of the map, their hunting ground, until an enemy unit comes within range.  Note that the units do not actively chase down enemies, but wander randomly through the hunting ground until an enemy is encountered.&lt;br /&gt;
&lt;br /&gt;
If enemies are within range of a hunter unit, the hunter attacks the weakest of them.  If a kill is made, it then retreats to its rest location, where it stays for a certain number of turns and until fully healed (whatever happens later).&lt;br /&gt;
&lt;br /&gt;
A few more details:&lt;br /&gt;
* If the hunter's way home is entirely blocked on the return (so that there is no possible path), the normal RCA AI takes over its behavior.&lt;br /&gt;
* However, if the way is blocked by an enemy unit occupying the rest hex, that enemy will be attacked unconditionally.&lt;br /&gt;
* A kill only makes the hunter go home when it is the attacker, not as defender.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Hunter AI behavior, check out the &amp;quot;Dragon&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]. &lt;br /&gt;
&lt;br /&gt;
Enable the Hunter Micro AI by using&lt;br /&gt;
 ai_type=hunter&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''home_x,home_y''': The coordinates of the hex to which the hunter returns and rests at after a kill. {{DevFeature1.15|0}} '''home_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the unit's hunting ground.  If this tag is not provided, the hunter uses all of the map.&lt;br /&gt;
* '''rest_turns'''=3: (integer)  The number of turns the AI stays at home_x,home_y after a kill.  It will not leave its home until this number of turns have passed ''and'' it is fully healed.&lt;br /&gt;
* '''show_messages'''=no: (boolean) If set to yes, the hunter announces whenever its behavior changes to the next phase.  Note that this is meant as debug output for the scenario author.  The messages are not translated and are not included in replays (and are only visible to the host in an MP game).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a hunter [micro_ai] tag usage from the &amp;quot;Dragon&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hunter&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Rowck&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=5-30,1-15&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             home_x,home_y=3,15&lt;br /&gt;
             rest_turns=2&lt;br /&gt;
 &lt;br /&gt;
             show_messages=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Swarm (ai_type=swarm) ===&lt;br /&gt;
&lt;br /&gt;
The Swarm Micro AI uses some very simply algorithms to simulate animal swarm behavior.  Without adjacent enemies, they simply do a random move, trying to stay together and at a certain distance from enemies.  However, if an enemy unit is close to any bat, the swarm scatters.  This is particular fun to watch when one places an enemy unit in the middle of the swarm.  After being scattered, the swarm members slowly rejoin, but not in a very organized way.  Sub-swarms or individual bats might roam around for quite some time before they find their way back.  It is also possible that individual bats (or small groups) split off from the larger swarm at times.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Swarm AI behavior, check out the &amp;quot;Swarm&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Swarm Micro AI by using&lt;br /&gt;
 ai_type=swarm&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Swarm AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_distance'''=5: (int) The minimum distance kept between units of the swarm and enemies when the swarm moves as a whole.&lt;br /&gt;
* '''scatter_distance'''=3: (int) Enemies within &amp;quot;scatter_distance&amp;quot; hexes of any swarm unit cause the swarm to scatter, by each unit trying to maximize its individual distance from the enemy.&lt;br /&gt;
* '''vision_distance'''=12: (int) Only units within this distance follow the overall swarm motion (either away from an enemy or of the swarm as a whole).  The smaller this value is set, the less likely the swarm is to stay together or rejoin.  This parameter is meant to simulate how far an individual member of the swarm can see, meaning that the swarm is &amp;quot;out of sight&amp;quot; (and the unit will not be able to follow it) if it is more than this distance away.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Swarm [micro_ai] tag usage from the &amp;quot;Swarm&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=swarm&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Assassin Squad Micro AI (ai_type=assassin) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|6}}&lt;br /&gt;
&lt;br /&gt;
This Micro AI controls one or several assassin units which try to circle around enemy units with the goal of hitting a target unit without being distracted or impeded by other enemies.  The units will always attack the target if such attacks are possible, otherwise they will follow the path of least resistance toward it. Notes:&lt;br /&gt;
&lt;br /&gt;
* The main priority for the AI is to find paths that avoid enemy units. However, if no such path can be found, the assassins will still advance toward the target, taking the path exposing the units to the least potential damage from enemies.&lt;br /&gt;
&lt;br /&gt;
* The units will not necessarily follow the same path. The best path is chosen individually for each unit.&lt;br /&gt;
&lt;br /&gt;
* While it is not a requirement for the AI to work, using ambusher or skirmisher units, or units with the quick trait, makes this AI more lethal in many cases.&lt;br /&gt;
&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded, it does not work otherwise. That means that '''ca_score''' is not a valid key for the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=assassin&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Assassin Squad specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the assassin units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the target unit.&lt;br /&gt;
** It is important to realize that the AI currently only works correctly with a single target unit. It is thus the responsibility of the scenario designer to make sure that this filter matches an individual enemy unit. If several enemy units are to be targeted in the same scenario, several instances of the Assassin Micro AI can be used.&lt;br /&gt;
** The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[prefer]''': A [[StandardLocationFilter|Standard Location Filter]] selecting locations on the map for the assassins to prefer when making their way toward the target unit. This could be used, for example, to make units with the ambush ability move (mostly) through forest or to have the assassins circle around the, say, northern part of the map rather than the south if that is desirable for scenario design purposes.&lt;br /&gt;
** As with avoiding enemies, the units will still move across non-preferred terrain if that is either the only option or ''much'' shorter than preferred terrain&lt;br /&gt;
** Attacking, once the target unit is in range, is not influenced by the [prefer] SLF.&lt;br /&gt;
&lt;br /&gt;
Here is an example of an Assassin Squad [micro_ai] tag usage:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=assassin&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Assassin,Master at Arms,Duelist&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             [prefer] # Circle around northern part of map&lt;br /&gt;
                 x=1-9, 10-99&lt;br /&gt;
                 y=1-37, 1-17&lt;br /&gt;
             [/prefer]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Bottleneck Defense Micro AI (ai_type=bottleneck_defense) ==&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense Micro AI lets you define a location on the map where the AI can take a defensive stance behind a narrow passage (bottleneck).  Units on the front line are backed up by healers and/or units with the leadership ability.  Injured units are moved to the back and replaced by uninjured (or less injured) units.  The units on the front line only attack when it is safe (no retaliation) or when there is a high chance that they will make a kill or level up.  Using this Micro AI only makes sense if there is no way for the enemy sides to move around the bottleneck and attack the AI's units from behind.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Bottleneck&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Bottleneck Defense Micro AI by using&lt;br /&gt;
 ai_type=bottleneck_defense&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Bottleneck Defense specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense AI requires two sets of coordinates that define where it should be taking up its defensive stance, and from which side the enemy attacks at that location.&lt;br /&gt;
&lt;br /&gt;
* '''x,y''': Comma separated lists of the hexes on the front line, where strong units are placed.  All hexes on which the AI makes contact with (can be attacked by) the enemy need to be included here.  Units are placed on them in the order in which they are listed here. {{DevFeature1.15|0}} '''location_id''' can be used instead with named/special locations.&lt;br /&gt;
* '''enemy_x,enemy_y''': Comma separated list of the hexes from which the enemy can attack the AI front line units.  This is needed for the AI to know on which side of the front line support units (healers etc.) need to be placed.  In many cases, it is sufficient to provide only one of the enemy front line hexes, but there are geometries for which that does not work.  It is therefore safer to list all enemy front line hexes here. {{DevFeature1.15|0}} '''enemy_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
By default, the AI places healers and units with the leadership ability both on the front line itself (if they are the strongest units available) and on hexes adjacent to and behind the line.  If different placement is desired, these locations can be overridden with the following keys.&lt;br /&gt;
&lt;br /&gt;
* '''active_side_leader'''=no: (boolean)  If set to 'yes', the side leader participates in the bottleneck defense action after the side's gold has been spent.  If set to 'no' (default), the leader follows default AI behavior (sitting on the keep and recruiting, for the most part).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Bottleneck Defense AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.15|3}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the units.  Only AI units matching this filter participate in the bottleneck defense.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''healer_x,healer_y''': Comma separated list of hexes where healers are placed.  This can be used to keep healers from the front line (or to take up only certain positions on the front line), and/or to override the default healing positions behind the line.  Healers are placed on them in the order in which the hexes are listed, with the exception that hexes on the line always take priority over hexes behind the line. {{DevFeature1.15|0}} '''healer_loc''' can be used instead with named/special locations.&lt;br /&gt;
* '''leadership_x,leadership_y''': Same as ''healer_x,healer_y'', but for units with the leadership ability. {{DevFeature1.15|0}} '''leadership_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
As an example, in the &amp;quot;Bottleneck&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]], there are three front line hexes, two of which are on hill terrain, while the third is a road with a lower defense rating.  The healer and side leader are supposed to participate in combat because they are strong units, but only from the hill hexes to keep them safer.  This is accomplished by using the following keys settings (check out the scenario to see which hex is which):&lt;br /&gt;
 [micro_ai]&lt;br /&gt;
     side=1&lt;br /&gt;
     ai_type=bottleneck_defense&lt;br /&gt;
     action=add&lt;br /&gt;
 &lt;br /&gt;
     x=14,14,14&lt;br /&gt;
     y= 7, 9, 8&lt;br /&gt;
     enemy_x=13,13&lt;br /&gt;
     enemy_y= 8, 9&lt;br /&gt;
 &lt;br /&gt;
     healer_x=14,14,15,15&lt;br /&gt;
     healer_y= 7, 9, 8, 9&lt;br /&gt;
     leadership_x=14,14,15,15&lt;br /&gt;
     leadership_y= 7, 9, 9 ,8&lt;br /&gt;
     active_side_leader=yes&lt;br /&gt;
 [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Fast Micro AI (ai_type=fast_ai) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|0}}&lt;br /&gt;
&lt;br /&gt;
This AI is meant as a fall-back AI for scenarios where there are so many units that attacks and moves done by the default AI are slow with long delays before each move.  It replaces the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions (CAs).  It should be obvious that in order to achieve the faster speed, compromises concerning the quality of the moves have to be made.&lt;br /&gt;
&lt;br /&gt;
Enable the Fast Micro AI by using&lt;br /&gt;
 ai_type=fast_ai&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Combat candidate action:'''&lt;br /&gt;
* Attackers are considered individually one at a time.  The best attack found for a unit is executed without checking whether a better attack by another unit might be possible.&lt;br /&gt;
* The default order in which attackers are considered is by decreasing hitpoints, starting with the strongest unit (in terms of HP).  This can be modified with the optional keys below.&lt;br /&gt;
* By default, if weaker units block attack hexes for a stronger unit, they do not move out of the way (this is the same as for the default AI).  If a hex becomes available after an attack (e.g. due to a weaker unit dying in the attack), all attacks that were possible ''before'' this happened are finished first.  Only after that are attacks by the stronger unit reconsidered.&lt;br /&gt;
* If include_occupied_attack_hexes=yes is set, weaker units move out of the way for stronger units if possible.  This can, however slow down the AI depending on the situation on the map.  Use with caution.&lt;br /&gt;
* {{DevFeature1.13|2}} Previous to this version, attacks by the leader were bunched in with the attacks by the other units.  Now leader attacks are treated separately and contain an extra layer of analysis to determine whether the attack position is &amp;quot;safe&amp;quot; (in an approximate way).  Currently, this is done with two simple considerations:&lt;br /&gt;
** Count all enemy units that can reach the leader's attack position.  If there are more than given by optional parameter 'leader_attack_max_units' (default value 3), do not attack.&lt;br /&gt;
** Add up the hitpoints of all enemies that can reach the leader's attack position.  If this value, multiplied by optional parameter 'leader_weight'(default value 2), is larger than the leaders hitpoints, do not attack.&lt;br /&gt;
** Two optional parameters, 'threatened_leader_fights' and 'leader_additional_threat', can also be used to let the leader attack when the threat at the attack hex is comparable to that at the leader's current position.&lt;br /&gt;
** In order to keep this AI as fast as possible, leader attacks are done by a separate candidate action that is only evaluated when all other CAs are done.&lt;br /&gt;
&lt;br /&gt;
'''Move-to-targets, villages and retreat candidate actions:'''&lt;br /&gt;
* The move-to-targets, villages and retreat candidate actions of the default AI are removed.  Move-to-targets and villages are taken over by the Fast Micro AI, whereas retreat is simply not done.&lt;br /&gt;
* This CA moves units toward goals after attacks are done.  Goals are unowned (if the AI side has a leader only) and enemy-owned villages and enemy leaders.&lt;br /&gt;
* For each goal, units are pre-sorted in order of the fewest moves needed to cover the ''distance'' toward the goal (that is, as if move cost on each hex toward the goal were 1).  Units are then considered sequentially and if a unit can get there &amp;quot;efficiently&amp;quot;, its move is executed without considering other units.&lt;br /&gt;
** A move is considered efficient if the move cost to get to the hex is not more than 'move_cost_factor' larger than the distance to the hex.  'move_cost_factor' is an optional key that defaults to 2.  It can be set to values of 1.1 or larger (see below).&lt;br /&gt;
** If the move for the current unit is not found to be efficient, the AI considers the next unit instead.  If no unit can do an efficient move toward the goal, the best (fastest) of the inefficient moves is selected instead.&lt;br /&gt;
&lt;br /&gt;
* Village goals:&lt;br /&gt;
** Village goals are considered before enemy leader goals so that the AI preferentially chooses scouts (or otherwise fast units) for them.&lt;br /&gt;
** The number of units sent toward villages depends on the value of the default AI aspect village_value.  It is set to village_value/4 times the number of units the AI has left to move.  Thus, by default (village_value=1.0) the AI sends a quarter of its units toward villages, and the rest toward enemy leaders.  This means that the meaningful range of values for village_value is 0..4.&lt;br /&gt;
** Only one unit is moved toward each village.  This consideration also overrides the number of units to move toward villages defined by village_value, topping it at the number of unowned/enemy-owned villages on the map.&lt;br /&gt;
** Setting village_value=0 disables moving units toward villages altogether.  That is, not only are no units chosen, but the village targeting phase of the AI is entirely disabled with no evaluation time used for it.  Note that, unlike with the default AI, this also means that no villages within range of an AI unit are grabbed.&lt;br /&gt;
** The order in which villages are chosen as goals is some combination of them being close to the AI side's leader and being spread out over the map.&lt;br /&gt;
&lt;br /&gt;
* Enemy leader goals:&lt;br /&gt;
** The order of enemy leaders goals is set according to their distance from the AI leader with the closest enemy leader being targeted first (if the AI has a leader, otherwise their order is random).  One unit is sent toward each enemy leader in this order, after which the AI starts with the first enemy leader again.  This is repeated until all units have moved.&lt;br /&gt;
&lt;br /&gt;
* Guardians (units with ai_special=guardian set) are ignored by the fast move-to-targets CA.&lt;br /&gt;
&lt;br /&gt;
'''Tips for speeding up/modifying the Fast Micro AI in specific scenarios:'''&lt;br /&gt;
&lt;br /&gt;
A number of optional keys and default AI aspects can be used to modify the behavior of the Fast Micro AI.  The MAI is set up so that it is a reasonable compromise between speed and quality of the moves in most situations.  However, this might not be ideal for all scenarios here are some things that can be done to speed up the AI (or to get better behavior in some situations by doing the opposite):&lt;br /&gt;
* Use large values for the default AI aspect aggression.  The largest delays before attacks happen (in some circumstances) when the Fast MAI has to consider lots of units with attacks left but decides not to attack with them.  See below for a description of how aggression is used by the fast AI.&lt;br /&gt;
* Use large values of move_cost_factor (see above for description of what this does).  If you want to emphasize units that can move across terrain easily (e.g. bats) to be chosen for village grabbing, you might want to use low values of move_cost_factor, but this might slow down the AI.  The default value should work reasonably well in most standard situations, but modifying the value might work better on specific maps.&lt;br /&gt;
* Don't use dungeon_mode=yes unless absolutely necessary, even in most dungeon crawls.  Test the AI without this setting first and only use it if it is the only way to solve the problem.  Often, a minor tweak to the map will have the same effect, without the speed penalty.&lt;br /&gt;
* Use include_occupied_attack_hexes=yes with caution.  It might (or might not) slow down the attack CA significantly, depending on the scenario.&lt;br /&gt;
* Set village_value=0, which makes the AI not consider villages as goals.  This has not been found to make a large difference on the maps tested so far, but it might have an effect on maps with a very large number of villages.&lt;br /&gt;
* Use attack_hidden_enemies=yes on maps where there are no hidden enemies. Excluding hidden enemies from the attack calculation requires an additional filter to be evaluated that takes extra time. In most cases this will not result in a large computation time saving.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_hidden_enemies'''=no: (boolean)  By default, hidden enemies are excluded from the attack calculation.  Setting this to 'yes' makes the AI also consider those enemies as attack targets.&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.13|2}} A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  As with the default AI, units will not step on hexes defined by the [avoid] tag.  In addition, no goals will be chosen that lie on those hexes.  The one minor exception is that a unit stepping out of the way for another unit might temporarily enter the avoided zone.&lt;br /&gt;
* '''dungeon_mode'''=no: (boolean)  As much as possible, the Fast Micro AI works with distances between hexes rather than path finding.  This works pretty well on open maps and even most dungeon crawls (and similar scenarios), but it might result in some units getting temporarily (and in rare cases permanently) stuck on maps with convoluted labyrinths.  If that is the case, setting dungeon_mode=yes triggers more path finding that avoids such situations (but as a result is somewhat slower).&lt;br /&gt;
* '''include_occupied_attack_hexes'''=no: (boolean) If set to 'yes', the AI moves weaker units out of the way for a stronger attacker.  This might lead to better attacks but is slower, so don't use if speed is essential.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the attackers.  Only AI units matching this filter will attack, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes the side= key set to the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the enemies.  Only enemy units matching this filter will be attacked, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''leader_weight'''=2: (number &amp;gt;= 0) {{DevFeature1.13|2}} A factor specifying how much more valuable the AI considers side leaders than non-leader units in the attack evaluation. This is also used to determine whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''leader_additional_threat'''=1: (number &amp;gt; 0) {{DevFeature1.13|2}} If threatened_leader_fights=yes, the threat at the attack position may be larger by this factor for the leader to leave the keep for an attack.&lt;br /&gt;
* '''leader_attack_max_units'''=3: (number &amp;gt;= 0) {{DevFeature1.13|2}} A number specifying how many enemy units within reach are acceptable when determining whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''move_cost_factor'''=2: (number &amp;gt;= 1.1)  The factor by which the move cost to a goal hex may be larger than the distance to the goal hex for a move to be considered &amp;quot;efficient&amp;quot;.  See description above.  1.1 is used if this is set to values smaller than 1.1.  Smaller values might lead to better behavior, while larger values might speed up the AI.&lt;br /&gt;
* '''weak_units_first'''=no: (boolean) If set to 'yes', inverts the order in which units are considered by the combat CA.  Attacks are then done in order of increasing hitpoints of the attackers.&lt;br /&gt;
* '''skip_combat_ca'''=no: (boolean) If set to 'yes', the fast combat CA is not used.  The default AI's high_xp_attack and combat CAs remains in place in this case.&lt;br /&gt;
* '''skip_move_ca'''=no: (boolean) If set to 'yes', the fast move-to-targets CA is not used.  The default AI's retreat, villages and move-to-targets CAs remains in place in this case.&lt;br /&gt;
* '''threatened_leader_fights'''=no: (boolean) {{DevFeature1.13|2}} By default, the AI leader will not attack if the threat at the attack position is too high.  If this key is set to 'yes', the leader will attack if the threat there is not larger than the threat at his current position multiplied by the value of 'leader_additional_threat'. This is meant as an option for the endgame: when the AI leader is under attack, he might as well go down fighting.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''aggression''': The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to set how aggressive the AI should attack.  It works in roughly the same way as the default AI aspect:&lt;br /&gt;
** The value of (1 - aggression) determines how much the AI values its own units compared to the enemy's units.  Thus, aggression=0 means that attacks are only done if the AI expects to deal (roughly) the same amount of damage as it will receive.  aggression=1 means that expected damage to the AI's units is not considered, only the expected damage to enemy units matters.&lt;br /&gt;
** Damage is calculated as a combination of hitpoints and chance to die, in &amp;quot;gold equivalent units&amp;quot;.&lt;br /&gt;
** Unlike the default AI, aggression is set to 1 for values larger than 1.  Allowed values are thus anything from -infinity to +infinity, although only -infinity to +1.0 make sense.&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.13|2}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
*'''attacks aspect''':  {{DevFeature1.13|2}} The standard RCA AI [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] used to filter own and enemy units for attacks is now also taken into account as long as neither [filter] nor [filter_second] (see above under 'optional keys') are defined in the [micro_ai] tag.&lt;br /&gt;
*'''village_value=1''': (number ranging from 0 to 4) The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to define how much villages should be targeted by the move-to-targets CA.  In the Fast MAI, this defines which fraction of the AI units are moved toward village goals (see above for details).  If set to 0, villages are ignored as goals.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded to the same values as those of the respective [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|default candidate actions]].  ca_score is therefore not a valid optional key.&lt;br /&gt;
* The fast Micro AI modifies (removes) the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions.&lt;br /&gt;
&lt;br /&gt;
Enabling the Fast Micro AI in its default configuration is as simple as this usage example from the &amp;quot;Fast AI&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=fast_ai&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Generalized Goto Micro AI (ai_type=goto) ==&lt;br /&gt;
&lt;br /&gt;
The Goto Micro AI provides a highly configurable method of sending a unit (or units) to a location or set of locations.  The units to be moved are defined using a [[StandardUnitFilter|Standard Unit Filter]] (SUF) [alternatively, all side units are used if the SUF is not provided), while the goto locations are given in a [[StandardLocationFilter|Standard Location Filter]] (SLF).&lt;br /&gt;
&lt;br /&gt;
By default, each unit matching the SUF will move toward the closest goal (in terms of turns needed to get there) matching the SLF via the fastest route given the current situation on the map (locations of enemy units etc.), but this behavior can be influenced by the optional keys listed below.  Note that this means that a unit's goal might change from one turn to the next depending on the situation on the map.&lt;br /&gt;
&lt;br /&gt;
The behavior of the unit(s) once they reach their goal location(s) is configurable as well.  By default, they will stay there until the Goto Micro AI is deleted or changed.&lt;br /&gt;
&lt;br /&gt;
For demonstrations of several Goto Micro AI usages, check out the &amp;quot;Goto&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Goto Micro AI by using&lt;br /&gt;
 ai_type=goto&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Goto specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the possible goal locations for the move.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists). Note that the area defined by [avoid] is not entered at all, even if the end point of the move would lie outside the area again.&lt;br /&gt;
* '''avoid_enemies''': (number&amp;gt;0) By default, units controlled by the Goto Micro AI will not take enemy units into account for route finding, other than the enemies' ZoCs.  By contrast, if this key is set, they will try to avoid traversing hexes on which they can be attacked by enemies.  The larger the value of ''avoid_enemies'', the more the units will try to stay out of the enemies' way.  Notes:&lt;br /&gt;
** Units are willing to take a path that is 'avoid_enemies' movement points longer for each enemy unit that can be avoided by doing so for each hex along the path.  As an example, if avoid_enemies=1 and the shortest path crosses 2 hexes that can be reached by 3 enemies each, the unit is willing to take a path that is up to 6 hexes longer instead.  If avoid_enemies=10 in the same situation, the unit will take a path up to 60 hexes longer.&lt;br /&gt;
** Hexes adjacent to enemy units are always strongly avoided, independent of the value of avoid_enemies.&lt;br /&gt;
** avoid_enemies is ignored if 'use_straight_line=true' is set.&lt;br /&gt;
** Due to an issue with the path finding code, '''tunnels and teleports are ignored in Wesnoth 1.14 and before''' if 'avoid_enemies' is used. This has been fixed in Wesnoth 1.15.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units to be moved.  Defaults to all units of the side if not defined.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''ignore_enemy_at_goal'''=no and '''ignore_units'''=no: (boolean)  In the default configuration, the Goto MAI does not move units if the path to all goal hexes is blocked by enemies.  It is therefore possible to disable the Goto MAI by putting enemy units on all the goal hexes.  That can be changed if either of these two keys is set to 'yes'.&lt;br /&gt;
** 'ignore_enemy_at_goal=yes' makes the AI choose its goals while disregarding any enemy units at the goal hexes.  Even so, a stiff rating penalty is applied to goal hexes occupied by enemies, thus that unoccupied goal hexes are chosen unless all possible goals are occupied by enemies.  Note that this setting ''only'' ignores enemy units at the goal hexes themselves.  It is still possible to disable the AI by putting enemies all around the goals.&lt;br /&gt;
** 'ignore_units=yes' let's the AI ignore ''all'' enemy units when deciding which goal hex(es) to move toward.&lt;br /&gt;
** In both cases, enemies are only ignored for deciding which unit to move toward which goal hex.  For the actual move itself, all enemies and their ZoCs are considered as usual.&lt;br /&gt;
** Both options only take effect if neither avoid_enemies nor use_straight_line is set to 'yes', as neither of these consider enemies as blockers.&lt;br /&gt;
* '''release_unit_at_goal'''=no: (boolean) By default, a unit that reaches a goal location will stay there until the Goto Micro AI is deleted or changed (or until the conditions specified in the [goto_goals] SLF change).  If this parameter is set, a unit that has reached its goal will not be controlled by this Micro AI any more starting from the next turn.&lt;br /&gt;
* '''release_all_units_at_goal'''=no: (boolean) Same as ''release_unit_at_goal'', but this will deactivate the Goto Micro AI for all units once the first unit reaches a goal.&lt;br /&gt;
* '''unique_goals'''=no: (boolean)  If set to 'yes', each unit will be sent to a different goal location.  Note that the best goals are recalculated each turn, so a unit might choose a different goal this turn from what it was heading for during the previous turn if the situation on the map has changed.  Also note that if there are more units than goal locations, the left over units will not be handled by the Goto MAI but follow default behavior instead.&lt;br /&gt;
* '''use_straight_line'''=no: (boolean)  By default, units choose the route by which they will get to their goal locations in the smallest number of turns.  If this parameter is set to 'yes', a straight line route (or something pretty close to it at least) will be chosen instead, irrespective of whether this leads into a dead end, whether it is blocked by enemies, etc.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* Depending on the parameters used, the Goto Micro AI might require a lot of path finding to be done.  Thus, while it is possible to set up this AI to move many units to many different locations, a usage like that might require quite a long calculation time.&lt;br /&gt;
&lt;br /&gt;
===Goto Micro AI example usages:===&lt;br /&gt;
&lt;br /&gt;
Here are several examples of goto [micro_ai] tag usages from the &amp;quot;Goto&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
* Send a unit toward a castle (similar to the RCA AI's Goto Candidate Action, but avoiding dead ends and areas ZoCed by enemies)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
             ca_id=messenger1&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=messenger1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=6-7,2-4&lt;br /&gt;
                 terrain=C*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             ca_score=210001&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send all units of the side toward the southern border of the map&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=8&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                  y=33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             avoid_enemies=1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send units to the four corners of the map (with each unit going to a different corner).&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x=1,1,44,44&lt;br /&gt;
                 y=1,33,1,33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             unique_goals=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Set up a guardian unit that doesn't move while no enemy unit is within 8 hexes (ironically, the Goto Micro AI can be used to keep units in place, although using one of the Guardian Micro AIs might often be a better idea)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=guard1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 [filter]&lt;br /&gt;
                     id=guard1&lt;br /&gt;
                 [/filter]&lt;br /&gt;
                 [not]&lt;br /&gt;
                     [filter]&lt;br /&gt;
                         side=1&lt;br /&gt;
                     [/filter]&lt;br /&gt;
                     radius=8&lt;br /&gt;
                 [/not]&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Guardian Micro AIs ==&lt;br /&gt;
=== General Comments on the Guardian Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Guardian Micro AIs can be used to set up different kind of guardian behaviors for a unit or a set of units.  Four different types of guardians are available and are described below.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different guardian behaviors, check out the &amp;quot;Guardians&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
=== Return Guardian  (ai_type=return_guardian)===&lt;br /&gt;
&lt;br /&gt;
A 'return guardian' is a variation of the standard Wesnoth guardian. It has an assigned guard position (GP) to which it returns after attacks on approaching enemies:&lt;br /&gt;
* If at GP with no enemy in reach, do nothing.&lt;br /&gt;
* If at GP with an enemy in reach, attack. Note that the attack is done by the default AI, which might result in the guardian not attacking if the enemy is deemed too strong (this can be influenced by setting the default ''aggression'' value to a different value).&lt;br /&gt;
* If not at GP, return there, no matter whether an enemy is in reach or not.&lt;br /&gt;
* If enemies are blocking the way back to GP, do your best to move around them.&lt;br /&gt;
* If the guardian ends up next to an enemy on the way back, attack that enemy after the move.&lt;br /&gt;
&lt;br /&gt;
Enable the Return Guardian Micro AI by using&lt;br /&gt;
 ai_type=return_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''return_x,return_y''': The coordinate of the hex the unit returns to after each attack.  {{DevFeature1.15|0}} '''return_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=100010: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  This AI consist of 2 CAs, one with score &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt; the other with &amp;lt;tt&amp;gt;ca_score-20&amp;lt;/tt&amp;gt;.  The default value is set so that the first CA gets executed right before the default combat CA, and the other right after.  It usually will not make sense to change this value, but it is possible to do so for custom applications.  {{DevFeature1.13|6}}: This is now changed to 100100 and 99900 in order to avoid conflicts with the new high_XP_attacks candidate action.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a return guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=return_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=return1&lt;br /&gt;
             return_x,return_y=20,2&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
===Stationed Guardian (ai_type=stationed_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A 'stationed guardian' is another variation of the standard Wesnoth guardian with a somewhat more complex behavior than that of the 'return guardian'. Two positions are defined for it, a 'station' and a 'guarded location', as well as a distance. The behavior is as follows:&lt;br /&gt;
* If no enemy is within 'distance' of the guard's current position, do nothing.  This is independent of the guardian being at its station or not.&lt;br /&gt;
* Otherwise: If an enemy is within 'distance' of the guard, but not also within the same distance of the guarded location and the station (all of this simultaneously), move the guard in the direction of the station (or keep the guard in place if the station is unreachable).&lt;br /&gt;
* Otherwise:&lt;br /&gt;
** Pick the enemy unit that is closest to the guarded location.&lt;br /&gt;
** If we can reach it, pick the adjacent hex with the highest defense rating and attack from there.&lt;br /&gt;
** If not in reach, move toward this unit.&lt;br /&gt;
&lt;br /&gt;
Enable the Stationed Guardian Micro AI by using&lt;br /&gt;
 ai_type=stationed_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance parameter as explained above&lt;br /&gt;
* '''station_x,station_y''': The x and y position of the hex that serves as the guardians station. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''guard_x, guard_y''': The x and y position of the guarded location.  {{DevFeature1.13|0}}  These are now optional keys, defaulting to station_x,station_y if not provided. {{DevFeature1.15|0}} '''guard_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a stationed guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=stationed_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=stationed1&lt;br /&gt;
             distance=4&lt;br /&gt;
             station_x,station_y=2,14&lt;br /&gt;
             guard_x,guard_y=3,13&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Coward (ai_type=coward) ===&lt;br /&gt;
&lt;br /&gt;
Cowards are units that, like guardians, sit around doing nothing until an enemy comes into range.  Unlike guardians, however, they run away once enemies approach. Applications might be wild animals, unarmed civilians getting in the way of a battle, etc.  The coward macro can be called with two optional locations, 'seek' and 'avoid':&lt;br /&gt;
* If neither is given, the coward retreats to the position farthest away from the approaching enemies.&lt;br /&gt;
* If 'seek' is given, it preferentially goes toward that location (but getting away from enemies takes priority).&lt;br /&gt;
* If 'avoid' is given, it in addition tries to avoid that location (with both maximizing distance from enemies and going toward 'seek' taking priority).&lt;br /&gt;
* Both 'seek' and 'avoid' may consist of only one coordinate ('x' or 'y'), in which case not a single hex, but a line of hexes is sought or avoided.&lt;br /&gt;
&lt;br /&gt;
Enable the Coward Micro AI by using&lt;br /&gt;
 ai_type=coward&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance within which an enemy must be to &amp;quot;scare&amp;quot; the unit&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_if_trapped'''='no':  {{DevFeature1.13|0}}  With the default settings, a coward never attacks.  If this key is set to 'yes', the coward attacks the weakest (lowest hitpoints) enemy next to which it ends up after moving (such as a cornered animal would do, blindly lashing out without regards for its own safety).&lt;br /&gt;
* '''avoid_x, avoid_y''': The x and y coordinate of the hex to avoid. {{DevFeature1.15|0}} '''avoid_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units from which the cowards flee.  If not set, this defaults to all enemy units.  Note, however, that no &amp;quot;enemy of AI side&amp;quot; condition is added if [filter_second] is specified, meaning that it can include allied and/or enemy units as desired (and even units on the same side as the coward).&lt;br /&gt;
* '''seek_x, seek_y''': The x and y coordinate of the hex to seek. {{DevFeature1.15|0}} '''seek_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a coward [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=coward&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=coward3&lt;br /&gt;
             distance=5&lt;br /&gt;
             seek_x,seek_y=24,5&lt;br /&gt;
             avoid_x,avoid_y=24,15&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Zone guardian (ai_type=zone_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A zone guardian is a unit that, as the name says, guards a zone. It moves randomly inside this zone until an enemy enters it (or a separately defined enemy zone, see below). Applications might be the defense of a castle, a nesting area or for protecting an unit.  The zone macro can be called with an optional SLF, [filter_location_enemy]:&lt;br /&gt;
* If not specified, the zone guard attacks any enemy coming inside its guard zone.&lt;br /&gt;
* If [filter_location_enemy] is given, it attacks any enemy entering this zone and once there are no more enemies, it goes back to patrol in its basic zone.&lt;br /&gt;
&lt;br /&gt;
Enable the Zone Guardian Micro AI by using&lt;br /&gt;
 ai_type=zone_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone to guard.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_enemy]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone where enemies have to be attacked.  If not given, it defaults to [filter_location].&lt;br /&gt;
* '''station_x,station_y''': By default, the zone guardian moves randomly within the zone if there is no enemy to attack.  However, if station_x/y is given, it moves to this position instead during turns when it does not have an enemy to attack.  This position does not need to be inside the zone defined above.  Note: obviously it does not make sense to define [filter_location], [filter_location_enemy] and station_x/y all at the same time.  Only [filter_location] and either one of the latter two (or none of them) should be provided for any zone guardian. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a zone guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=zone_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=zone3&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=22-31,4-11  # This is intentionally chosen to extend past the lake&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             station_x,station_y=32,8&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Hang Out Micro AI (ai_type=hang_out) ==&lt;br /&gt;
&lt;br /&gt;
The Hang Out Micro AI instructs units of the AI side to &amp;quot;hang out&amp;quot; around a certain location, or locations, until a condition for mobilizing the side is met.  If none of the optional keys are given, the AI controls all units of the side to hang around the leader while moving off castle tiles to make room for recruiting.&lt;br /&gt;
&lt;br /&gt;
The AI works by moving each unit to its best hex first using the minimum amount of movement points required for this move.  Once all AI units are in position, movement points are set to zero for all of them simultaneously such that they cannot be used by other AI candidate actions.  This is done because the mobilizing condition might be met as result of one of the moves of the Hang Out AI, after which we want units to be able to continue with other AI actions.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Hang Out and Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Hang Out Micro AI by using&lt;br /&gt;
 ai_type=hang_out&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Hang Out specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  In its default configuration, the Hang Out MAI controls all units of a side, keeping them as close to the side leader(s) as possible while moving off castle tiles to make room for recruiting.  For this purpose, the candidate action score defaults to 170,000, which is just below the score of the default recruiting CA.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''': A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  If neither of these two exists, this parameter is set to default to all castle terrain (&amp;lt;tt&amp;gt;terrain=C*,C*^*,*^C*&amp;lt;/tt&amp;gt;) so that units move off the castle to make room for recruiting.  Note that this does not include keep terrain, otherwise the leader would be moved off the keep as well.&lt;br /&gt;
* '''ca_score'''=170000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 170,000, which means that it is executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Hang Out action (&amp;lt;tt&amp;gt;side=&amp;lt;/tt&amp;gt; is always set and does not need to be provided).  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the locations ''near'' which the AI units should hang out.  This does not mean that the units only move onto terrain specified here, but that they minimize the distance to such terrain.  It defaults to the position of the side leader(s).&lt;br /&gt;
* '''[mobilize_condition]''':  Contains ([[ConditionalActionsWML#Condition_Tags|WML Condition Tags]])  Once the condition set up here has been met, the Hang Out MAI is disabled and the side goes over to other AI activity.  The Hang Out MAI remains disabled after this, even if the condition is not met any more later in the scenario.&lt;br /&gt;
* '''mobilize_on_gold_less_than''': (number) Since side gold cannot be checked in a WML condition tag without storing it in a variable first, this key is provided separately.  It disables the Hang Out Micro AI once the side gold goes below the value specified here for the first time.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
* The Hang Out AI still takes effect if no locations to hang out at are found or if all map locations are to be avoided.  In that case, it simply keeps all units at their current locations.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Hang Out Micro AI configuration from the &amp;quot;Hang Out and Messenger&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hang_out&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [avoid]&lt;br /&gt;
                 terrain=C*,H*,M*,A*,S*,*^F*&lt;br /&gt;
             [/avoid]&lt;br /&gt;
             [mobilize_condition]&lt;br /&gt;
                 [have_unit]&lt;br /&gt;
                     side=1&lt;br /&gt;
                     count=7-99&lt;br /&gt;
                 [/have_unit]&lt;br /&gt;
             [/mobilize_condition]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Healer Support Micro AI (ai_type=healer_support) ==&lt;br /&gt;
&lt;br /&gt;
The Healer Support Micro AI configures the healers of a side to stay behind the battle lines and heal injured and/or threatened units rather than participate in combat under all circumstances.  You can set different levels of aggressiveness for the healers.  &lt;br /&gt;
&lt;br /&gt;
'''Notes'''&lt;br /&gt;
* By default, ''all'' healers of the AI side are controlled by this AI.  This includes in particular also the side leader, if the leader is a healer.  If you do not want the leader to be included (and possibly be moved away from the keep etc.) use the [filter] tag with canrecruit=no (see below).&lt;br /&gt;
* It is not always better to use this Micro AI than the default AI, as it takes healers (which can be strong units) away from the pool of units to choose for attacks.&lt;br /&gt;
* {{DevFeature1.15|3}} This Micro AI now takes unit guardian status and the passive_leader aspect into account.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Healers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Healer Support Micro AI by using&lt;br /&gt;
 ai_type=healer_support&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Healer Support specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the healer units.  If set, only units on the AI side passing this filter ''and'' having the healing ability are considered by this Micro AI.&lt;br /&gt;
*'''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting units to receive healing. If set, only units on the AI side passing this filter ''and'' the other healee selection criteria will be selected for healing by this Micro AI.&lt;br /&gt;
* '''aggression'''=1.0: (float) Sets the aggressiveness of the AI.  This parameter is set up as a float to accommodate future functionality, but it currently acts as a boolean: if set to zero, the AI will never let its healers participate in combat, for all other values it allows them to attack after all other units have attacked and if the healers cannot find any more units to support.  The former behavior might be appropriate in scenarios with only one or few valuable healers, while the latter might work better in scenarios with many healers.&lt;br /&gt;
* '''injured_units_only'''=no: (boolean) If set to 'yes', the AI will only move healers next to units that are already injured and skip units that currently have full hitpoints, but might get injured during the next enemy turn.&lt;br /&gt;
* '''max_threats'''=9999: (integer) The maximum allowable number of enemies that can attack a hex in order for it to be considered for a healer.  As an example, setting 'max_threats=0' means that the AI only moves healers to locations that are entirely safe from the enemy (assuming that none of the units currently on the map dies).  Note that the value of this key is checked against the number of enemies that can make it to the hex, not the number of adjacent hexes from which the healer could be attacked.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI. Note: as attacks by the healers are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The Healer Support Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded.  It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Healer Support Micro AI configuration from the &amp;quot;Healers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             aggression=0&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Lurkers Micro AI (ai_type=lurkers) ==&lt;br /&gt;
&lt;br /&gt;
The Lurker Micro AI controls some or all units of a side in &amp;quot;lurker mode&amp;quot;.  A lurker is a unit that is capable of moving across most terrains, but that only stops on and attacks from specific terrain.  It might also have the ability to hide on this terrain (which is the reason why it is called a lurker).&lt;br /&gt;
&lt;br /&gt;
Lurkers move individually without strategy and always attack the weakest enemy within their reach.  If no enemy is in reach, the lurker does a random move instead - or it just sits and waits (lurks).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of different types of lurker behavior, check out the &amp;quot;Lurkers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Lurkers Micro AI by using&lt;br /&gt;
 ai_type=lurkers&lt;br /&gt;
in the [micro_ai] tag&lt;br /&gt;
&lt;br /&gt;
===Lurkers specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
* '''[filter]''': Specifies which units of the side are controlled by the Lurker Micro AI.  This is a [[StandardUnitFilter|Standard Unit Filter]].  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': Specifies the terrain from which the lurkers attack. This is a [[StandardLocationFilter|Standard Location Filter]].&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_wander]''': Specifies the terrain on which lurkers end their moves if there is no enemy in reach. This is a [[StandardLocationFilter|Standard Location Filter]].  If [filter_location_wander] is not given, it defaults to [filter_location].&lt;br /&gt;
* '''stationary'''=no: (boolean)  If set to yes, a lurker never moves if there is no enemy within attack range.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Lurkers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=4&lt;br /&gt;
             ai_type=lurkers&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Naga Fighter&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=W*,S*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             [filter_location_wander]&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location_wander]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Messenger Escort Micro AI (ai_type=messenger_escort) ==&lt;br /&gt;
&lt;br /&gt;
The Messenger Escort Micro AI lets you define a set of waypoints through which it tries to move one or several of its units, the messenger(s).  The other units escort the messenger, trying to protect it and attacking enemies that are in its way.  Note that a messenger might not strictly hit intermediate waypoints, but that getting close is good enough, especially if a waypoint is occupied by an enemy unit.  It does, however, always try to get to the last waypoint.&lt;br /&gt;
&lt;br /&gt;
'''Notes on the use of several messengers:''' {{DevFeature1.13|0}}  &lt;br /&gt;
* This Micro AI can control several messengers at the same time, which can but do not have to move in a group.  They can be put on the map altogether in one location, or be spread out on the map or across different turns.&lt;br /&gt;
* The farther a messenger has moved through the chain of waypoints, the higher a priority it is for the AI, meaning that farther advanced (on the map) messengers are moved first, and that the escort units try to clear the way and stay with those messengers more so than with those further back on the map.  This order can be inverted using the invert_order= key.&lt;br /&gt;
* If several messengers are on the map, escort units might move from one to the other, the AI making a judgement call where they are needed most.  Closeness to a messenger is generally the most important criterion, but not the only one.&lt;br /&gt;
* If a messenger dies, the escort units previously staying with it now join one of the other messengers if there are any left (otherwise they follow default AI behavior).&lt;br /&gt;
* If you want certain escort units to stay always with the same messenger, use several messenger [micro_ai] tags with different [filter]/[filter_second] combinations.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Messenger Escort Micro AI by using&lt;br /&gt;
 ai_type=messenger_escort&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Messenger Escort specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': {{DevFeature1.13|0}} (Previous to 1.13.0, only id= was allowed.) The id of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated list of waypoint coordinates through which the messenger will go in the given order to reach its goal, given by the last coordinate.  If you want the messenger to go directly to the goal, simply enter a single x,y coordinate here.  Note that the messenger does not have to hit each waypoint exactly (except for the final one), getting close to them is good enough. {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Messenger Escort AI consists of 3 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-2&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_death_chance'''=0.67 and '''messenger_death_chance'''=0.0: (both floats; ranging from 0 to 1)    If the messenger is adjacent to an enemy at the end of the move, it will only attack the enemy if the chance of death of the enemy is &amp;gt;= enemy_death_chance and if the messenger's chance of death is &amp;lt;= messenger_death_chance (or if it is an attack at a range for which the enemy does not have a weapon).&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|0}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the escort units.  If not set, this defaults to all enemy units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''invert_order'''=no: {{DevFeature1.13|0}} If set to 'yes', the order of priority with which messenger units are considered is inverted, starting with the messenger unit that is the least advanced on the map (see description above).  In most cases, selecting this option results in worse performance of the AI, so use it with caution.  (And obviously it has no effect at all if there is only one messenger controlled by the AI.)&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Messenger Escort&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=messenger_escort&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=messenger&lt;br /&gt;
             waypoint_x=31,24,27,28&lt;br /&gt;
             waypoint_y=20,14,7,1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Patrol Micro AI (ai_type=patrol) ==&lt;br /&gt;
&lt;br /&gt;
The defines a number of waypoints for a unit (or units) to follow.  Options can be set whether the route should be followed in a loop, as an out-and back, only once, whether units encountered along the way should be attacked, etc.&lt;br /&gt;
&lt;br /&gt;
No matter what options are chosen, getting to the next waypoint always takes priority over attacking for a patroller.  The AI thus prefers to move the patrol unit around enemies rather than straight for them.  Also, if a waypoint is occupied by a unit the AI is not instructed to attack, it will (eventually) abandon that waypoint once it gets close enough and move on to the next one.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different patrol behaviors, check out the &amp;quot;Patrols&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Patrol Micro AI it by using&lt;br /&gt;
 ai_type=patrol&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Patrol specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated lists of the waypoint coordinates through which the patrol travels (in order). {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack''': By default (if this key is not set), the patrol unit attacks any unit it ends up next to after a move.  Alternatively, this parameter can be set to a comma-separated list of unit ids. If that is done, the patrol only attacks these units when it ends up next to them and ignores any other unit.  This can in turn be used to have the patroller not attack at all, by setting ''attack='' to a non-existing id.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''one_time_only'''=no: (boolean)  If set to 'yes', the unit goes through the patrol route only once and then hangs out at the last waypoint.  It also always attacks any enemy unit on the last waypoint, independent of what ''attack='' is set to.&lt;br /&gt;
* '''out_and_back'''=no: (boolean)  By default, a patrol unit returns to the first waypoint after reaching the last, doing the patrol route as a loop.  If this parameter is set to 'yes', the unit instead reverses its course and perpetually does out and backs of the route.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Patrols&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=patrol&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Konrad&lt;br /&gt;
             waypoint_x=9,24,25&lt;br /&gt;
             waypoint_y=21,23,15&lt;br /&gt;
             one_time_only=yes&lt;br /&gt;
             attack=Gertburt&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Protect Unit Micro AI (ai_type=protect_unit) ==&lt;br /&gt;
&lt;br /&gt;
The Protect Unit Micro AI tries to move one or several units to a given location while keeping them safe from enemy attacks. It does so by keeping the protected units away from enemy units and close to friendly units. Other units on the AI side are controlled by the default Wesnoth AI.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Protect Unit&amp;quot; and &amp;quot;The Elves Besieged&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Protect Unit Micro AI by using&lt;br /&gt;
 ai_type=protect_unit&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Protect Unit specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[unit]''': A separate [unit] tag is required for each protected unit(s).  Each tag has required keys '''id''', '''goal_x''', '''goal_y''' for each unit, as shown in the example below. {{DevFeature1.15|0}} '''goal_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''disable_move_leader_to_keep'''=no: (boolean) If set to 'yes', side leaders protected by this AI do not try to return to their keep.  This is necessary for this Micro AI to work with units that are side leaders and that are supposed to move to an off-keep location.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
*The Protect Unit Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].  Depending on the parameters, it might also modify the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|move leader to keep candidate action]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded. It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;HttT: The Elves Besieged&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=protect_unit&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Delfador&lt;br /&gt;
                 goal_x,goal_y=1,2&lt;br /&gt;
             [/unit]&lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
                 goal_x,goal_y=1,1&lt;br /&gt;
             [/unit]&lt;br /&gt;
 &lt;br /&gt;
             disable_move_leader_to_keep=true&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Recruiting Micro AI==&lt;br /&gt;
=== General Comments on the Recruiting Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Recruiting Micro AI replaces the default recruitment AI by alternative recruiting patterns.  Currently there are two recruiting algorithms you can choose from, random recruiting and the rush recruiting used in the new (as of Version 1.11.1) Experimental AI (see below).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of a different recruiting pattern, check out the &amp;quot;Recruiting&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  You can also explore the unit choices by watching the Experimental AI in a multiplayer game.&lt;br /&gt;
&lt;br /&gt;
'''Note:''' Obviously, the Recruitment Micro AI modifies the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|recruitment candidate action]].&lt;br /&gt;
&lt;br /&gt;
=== Random Recruitment AI (ai_type=recruit_random) ===&lt;br /&gt;
&lt;br /&gt;
This AI randomly selects a unit type from the recruitment list of the side.  The probability of choosing each type and the behavior in low-gold situations can be influenced with optional parameters.&lt;br /&gt;
&lt;br /&gt;
Enable the Random Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_random&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* '''[probability]''': This tag sets the probability of selecting the given unit type.  It has two required sub-keys:&lt;br /&gt;
** '''type''': comma-separated list of unit types to which this probability is to be applied&lt;br /&gt;
** '''probability''': (non-negative float)  The probability with which these unit types is to be selected.  All unit types for which no [probability] tag is defined receive a default probability of 1.0, thus values should be chosen relative to that.  As an example, in the code below swordsmen and peasants are given a probability of 8, while no other units have assigned probabilities.  Thus, each of these two unit types is 8 times more likely to be selected than any other individual unit type.&lt;br /&gt;
* '''skip_low_gold_recruiting'''=no: (boolean)  By default, the random recruitment AI chooses a unit to recruit only from those types for which it has sufficient gold.  If this parameter is set to 'yes', it will instead choose from all available unit types and end recruiting for the turn if there is not enough gold to recruit it this turn (even if other, affordable unit types exist).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a random recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_random&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [probability]&lt;br /&gt;
                 type=Swordsman,Peasant&lt;br /&gt;
                 probability=8&lt;br /&gt;
             [/probability]&lt;br /&gt;
 &lt;br /&gt;
             skip_low_gold_recruiting=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Rush Recruitment AI (ai_type=recruit_rushers) ===&lt;br /&gt;
&lt;br /&gt;
This AI replaces the default recruiting with the rush recruiting used in the new (as of Version 1.11.1) Experimental AI.&lt;br /&gt;
&lt;br /&gt;
Enable the Rush Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_rushers&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* {{DevFeature1.15|2}} '''high_level_fraction'''=0: (non-negative number) The approximate fraction of units of level 2 or higher to be recruited. This is defined as fraction of units on the map, not of new units being recruited during a turn or over several turns (which makes a difference if there are already units on the map). The effect is also cumulative per level (starting from level 2), meaning that 1/3 of L2 units will be recruited in the example above, 1/3*1/3=1/9 L3s etc. The default value is zero, which leaves the high-level unit recruiting to the default Experimental AI algorithm resulting usually in very few such units being recruited.&lt;br /&gt;
* '''randomness'''=0.1: (number)  A random number is applied to the rusher recruit engine's score to prevent the recruitment pattern from being too predictable. 0 causes no randomness to be applied, while larger numbers increase the random effect. A value of 1-2 generates results in which the random effect is approximately equal to the scored effect. Extremely high values are essentially entirely random.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a rush recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_rushers&lt;br /&gt;
             action=add&lt;br /&gt;
          [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Simple Attack Micro AI (ai_type=simple_attack) ==&lt;br /&gt;
&lt;br /&gt;
The Simple Attack Micro AI is meant as an addition to the default combat candidate action for when one wants to force the AI to do attacks that it would otherwise not do (for example on units that are 1 XP from leveling) or to execute certain attacks before all others (for example, attack with all &amp;quot;expendable&amp;quot; units first).  It is not set up to be a replacement for the default attacks.  Some notes:&lt;br /&gt;
&lt;br /&gt;
* The algorithm is different from and simpler than that of the default combat CA.  For example, no attack combinations are considered, only individual attacks.  As a result, the [[AiWML#List_of_AI_Aspects|default aspects]] (aggression, caution, [avoid], ...) are not taken into account.  Also, if lots of units are involved, evaluation of this Micro AI is potentially quite a bit slower than of the default attacks.&lt;br /&gt;
&lt;br /&gt;
* If an attack matching the Standard Unit Filters described below is found, this attack will always be executed, no matter how bad the odds are.  Thus, this Micro AI can be used to force the AI to do attacks it would otherwise avoid.&lt;br /&gt;
&lt;br /&gt;
* Other than selecting the attacker/defender units, this AI is currently not configurable.  Let us know if you would like to see certain options to be added.  (No guarantees that we will add them, but we will definitely consider them.)&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Simple Attack&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=simple_attack&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Simple Attack specific keys for the [micro_ai] tag===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  It is, however, in general not desirable to use this AI without at least one of the optional Standard Unit Filters below, as this AI is not meant as a replacement of the default combat candidate action.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=110000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 110,000, which means that it is executed right before the default combat candidate action.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Simple Attack action.  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units the AI should attack.  It defaults to all enemy units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
* '''weapon'''=-1: (integer) The number of the weapon that should be used for this attack.  Weapon numbers start at 1 (not 0).  -1 stands for &amp;quot;best weapon&amp;quot; (by whatever criterion the AI chooses to use).   '''Available from Wesnoth 1.11.8'''&lt;br /&gt;
&lt;br /&gt;
Below are 2 examples of Simple Attack Micro AI configurations from the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  The first one shows how to force the AI to attack with all Soulless and Walking Corpse units first:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110000&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless,Walking Corpse&lt;br /&gt;
             [/filter]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
The second executes attacks by Soulless on all enemy units that are 1 XP from leveling (something the RCA AI would generally not do).  Note that the Lua code bit is simply there in order to code the &amp;quot;1 XP from advancing&amp;quot; condition.  It is not needed for most other SUFs, as can be seen in the previous example.&lt;br /&gt;
         [lua]&lt;br /&gt;
             code=&amp;lt;&amp;lt;&lt;br /&gt;
                 function close_to_advancing(unit)&lt;br /&gt;
                     if (unit.experience &amp;gt;= unit.max_experience-1) then&lt;br /&gt;
                         return true&lt;br /&gt;
                     else&lt;br /&gt;
                         return false&lt;br /&gt;
                     end&lt;br /&gt;
                 end&lt;br /&gt;
             &amp;gt;&amp;gt;&lt;br /&gt;
         [/lua]&lt;br /&gt;
 &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110001&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless  # No Walking Corpses; L0 units don't advance enemy&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 lua_function = &amp;quot;close_to_advancing&amp;quot;&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
Several Simple Attack Micro AIs can be combined for the same AI side.  The two examples above are both used (note the different values for ca_score) in the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
== Custom Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|5}}&lt;br /&gt;
&lt;br /&gt;
From 1.13.5 onwards, adding a new Micro AI can be easily done without altering any core files or overriding the micro_ai tag. The [micro_ai] tag now looks up its ''ai_type'' parameter in the &amp;lt;code&amp;gt;wesnoth.micro_ais&amp;lt;/code&amp;gt; table and uses the micro AI setup function found there to determine which keys are required and allowed and what candidate actions to add.&lt;br /&gt;
&lt;br /&gt;
A micro AI setup function takes as its single parameter the contents of the [micro_ai] tag and returns three separate values:&lt;br /&gt;
*'''required''': A list of required keys and tags, as strings. Tags are indicated by enclosing them within square brackets in the string. If a key or tag in this list is missing from the [micro_ai] tag, an error will be printed.&lt;br /&gt;
*'''optional''': Similar to above, a list of optional keys and tags, as strings. No error will be printed if a key or tag in this list is missing, nor will an error be printed if it is present (as would be the case for unrecognized keys or tags).&lt;br /&gt;
*'''ca_params''': A table specifying the candidate actions that constitute the micro AI. It should contain an ''ai_id'' key (which specifies a base ID to be used for candidate actions of this micro AI) and a series of candidate action definitions. Each candidate action definition is a table containing the following keys:&lt;br /&gt;
**'''ca_id''': The ID used for the candidate action. This will be combined with the ''ai_id'' to produce the final ID.&lt;br /&gt;
**'''location''': Path to the Lua file that defines the candidate action.&lt;br /&gt;
**'''score''': The maximum score for the candidate action. If the [micro_ai] tag specified a ''ca_score'' parameter, the score here should normally be based on that.&lt;br /&gt;
&lt;br /&gt;
(The return statement would thus look like &amp;lt;syntaxhighlight lang='lua' inline&amp;gt;return required, optional, ca_params&amp;lt;/syntaxhighlight&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
Since the function has full access to the contents of the [micro_ai] tag, it can conditionally add candidate actions to '''ca_params''' depending on the values of other keys within the [micro_ai] tag. It can also perform other actions such as adding facets to an aspect; if it does such things, it should pay attention to the ''action'' key to determine whether it's being added or removed.&lt;br /&gt;
&lt;br /&gt;
There are two useful helper functions that can be used by Micro AI setup functions requiring aspect changes. They can be brought into scope by a line similar to the following:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='lua&amp;gt;local MAIH = wesnoth.require(&amp;quot;ai/micro_ais/micro_ai_helper.lua&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* MAIH.add_aspects(side, aspect_params)&lt;br /&gt;
* MAIH.delete_aspects(side, aspect_params)&lt;br /&gt;
&lt;br /&gt;
The ''aspect_params'' parameter is an array of tables, each of which contains the following keys:&lt;br /&gt;
*'''aspect''': The aspect to change.&lt;br /&gt;
*'''facet''': The WML defining the facet. This must include an ''id'' key in order for the aspect to be correctly deleted. For maximum compatibility, incorporate the ''ca_id'' key from the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Wesnoth_AI&amp;diff=65805</id>
		<title>Wesnoth AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Wesnoth_AI&amp;diff=65805"/>
		<updated>2020-07-19T18:21:07Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* AIs Available to Scenario and Map Designers */ updated with current status in 1.15.3+dev&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Wesnoth AIs==&lt;br /&gt;
&lt;br /&gt;
Wesnoth provides an artificial intelligence (AI) which is active by default for all computer-controlled sides, as well as several optional AIs.  These AIs are highly configurable and can be included and/or customized by scenario and map creators using simple WML code.  For AI developers, it is also possible to replace parts or all of the default AI with custom code.&lt;br /&gt;
&lt;br /&gt;
This page provides a brief summary of the available AIs and of the methods, tools and resources available for using and modifying them.  Links are provided to detailed documentation on each of the topics.  For reference, we also provide a brief timeline describing some of the main development milestones of the Wesnoth AIs.&lt;br /&gt;
&lt;br /&gt;
== Available AIs ==&lt;br /&gt;
&lt;br /&gt;
This section briefly lists the different AIs available in Wesnoth.  Click on the links provided here and in the resources section below for detailed descriptions of the AIs and for instructions for using and customizing them.&lt;br /&gt;
&lt;br /&gt;
=== AIs Available to Scenario and Map Designers ===&lt;br /&gt;
&lt;br /&gt;
Wesnoth comes with several built-in AIs from which scenario and map designers can choose:&lt;br /&gt;
&lt;br /&gt;
* '''[[RCA_AI|Default (RCA) AI]]''': If no special instructions are given, this AI is used automatically for all computer-controlled sides with no setup needed.  It can be customized in many different ways.&lt;br /&gt;
* The '''[[Experimental AI]]''' adds additional features to the RCA AI.  {{DevFeature1.15|3}} Several of the actions of the Experimental AI have been merged into the default AI.&lt;br /&gt;
* '''[[Micro AIs]]''' add specialized behavior to Wesnoth that cannot be achieved with the default AI.  There is a large number of available Micro AIs and they can be set up using just a few lines of simple WML code.&lt;br /&gt;
* '''[[Creating Custom AIs|Custom AIs]]''':  In addition to these AIs, Wesnoth provides a large variety of options for writing your own AI code.&lt;br /&gt;
&lt;br /&gt;
=== AIs Available in the Multiplayer Setup Menu ===&lt;br /&gt;
&lt;br /&gt;
Several AIs are available to be chosen for computer players in the multiplayer setup menu:&lt;br /&gt;
&lt;br /&gt;
* '''[[RCA_AI|Default AI (RCA)]]''': This is the same AI as listed above.&lt;br /&gt;
* '''[[Experimental_AI]]''': Again, the same AI as described above.&lt;br /&gt;
* Debug Mode '''Dev AIs''': If Wesnoth is in debug mode, several additional AIs are available in the menu.  For the most part, these are not meant as complete stand-alone AIs, or as AIs that are significantly different from the default AI, but rather as examples that can be used for development purposes.  {{DevFeature1.13|5}} The list of AIs has been cleaned up.  Only working and (somewhat) meaningful AIs remain.&lt;br /&gt;
* '''[[Creating Custom AIs|Custom AIs]]''':  Just as it is possible to include custom AIs in the scenario code, they can also be provided for selection from the MP setup menu.  These can be written either for specific scenarios/maps or so that they are available for all maps.&lt;br /&gt;
&lt;br /&gt;
== Available AI Tools ==&lt;br /&gt;
&lt;br /&gt;
The default AI is written in C++ and therefore difficult to modify directly.  However, it is a [[Wesnoth_AI_Framework#Wesnoth_AI_Framework:_A_Composite_AI|composite AI]] assembled from individual components which can be customized and modified in both WML and [[LuaWML|Lua]].  Documentation on the available AI tools can be found at:&lt;br /&gt;
&lt;br /&gt;
* [[AiWML]]: Customizing the default AI using WML&lt;br /&gt;
* [[Modifying AI Components]]: More complex customization tasks and mid-scenario changes&lt;br /&gt;
* [[Wesnoth AI Framework]]: Description of Wesnoth's composite AI configuration&lt;br /&gt;
* [[Creating Custom AIs]]: Instructions for writing your own AI components&lt;br /&gt;
* [[LuaAI|Lua AI]]: Recommended method for writing custom AIs&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* Wesnoth has its own internal language called 'Formula' which can also be used for AI development.  It is, however, not maintained any more and has by now been surpassed by Lua in ease of use in most cases.  We therefore do not recommend using it any more, but links to the legacy documentation are still provided in the resources section below. &lt;br /&gt;
* A Python API for AI development also existed for a while.  It was removed for security reasons.&lt;br /&gt;
&lt;br /&gt;
== List of Wesnoth AI Resources ==&lt;br /&gt;
&lt;br /&gt;
===AI Resources for Scenario and Map Creators===&lt;br /&gt;
&lt;br /&gt;
* [[RCA AI]]: Description of Wesnoth's default AI, with the following additional documentation:&lt;br /&gt;
** [[AiWML]]: Customizing the default AI using WML&lt;br /&gt;
** [[AI Recruitment]]: Customizing the recruitment of the default AI&lt;br /&gt;
** [[Modifying AI Components]]: Instructions for some of the more complex customization tasks&lt;br /&gt;
* [[Experimental AI]]: Experimental augmented version of the default AI with additional features&lt;br /&gt;
* [[Micro AIs]]: Additional specialized AI behavior that cannot be achieved with the default AI&lt;br /&gt;
&lt;br /&gt;
===Resources for AI Coders===&lt;br /&gt;
&lt;br /&gt;
* [[Wesnoth AI Framework]]: General principles of the inner workings of the Wesnoth AI mechanism&lt;br /&gt;
* [[Creating Custom AIs]]: Instructions for doing just that&lt;br /&gt;
* [[LuaAI|Lua AI]]: Recommended method for writing custom AIs&lt;br /&gt;
&lt;br /&gt;
==== Legacy Resources for AI Coders====&lt;br /&gt;
&lt;br /&gt;
* [[Lua AI Legacy Methods Howto]]: Old methods for writing custom AIs using Lua AI&lt;br /&gt;
* [[FormulaAI|Formula AI]]: Another language for writing custom AIs&lt;br /&gt;
&lt;br /&gt;
===Miscellaneous Other Resources===&lt;br /&gt;
&lt;br /&gt;
The following resources are not generally needed for understanding, using and modifying Wesnoth AIs, but they might nevertheless contain some useful bits of information for the interested reader.&lt;br /&gt;
&lt;br /&gt;
* [[AI Arena]]&lt;br /&gt;
* [[Machine Learning Recruiter]]&lt;br /&gt;
* [[WhyWritingAWesnothAIIsHard|Why Writing A Wesnoth AI Is Hard]]&lt;br /&gt;
* [[WritingYourOwnAI|Writing Your Own AI]]&lt;br /&gt;
&lt;br /&gt;
Finally, the link to the AI Category at the bottom of this page includes links to all the remaining AI-related pages.&lt;br /&gt;
&lt;br /&gt;
==A Very Brief History of Wesnoth AIs==&lt;br /&gt;
&lt;br /&gt;
While the general principle of the Wesnoth AI has been relatively stable throughout its development, the AI implementation has seen dramatic changes.  That said, basic scenario creator tasks such as [[AiWML|modifying AI parameters in WML]] are usually not changed much in order to keep backward compatibility.  On the other hand, the steps needed to write, for example,  [[Creating_Custom_AIs#Creating_Custom_Candidate_Actions|custom candidate actions]] and bind them into the existing AI framework have definitely evolved significantly from Wesnoth 1.8 to 1.10 to 1.12.&lt;br /&gt;
&lt;br /&gt;
It is not possible to capture all these changes on the Wesnoth AI wiki pages due to the complexity of the topic.  They generally only refer to the latest stable release series and the current development release series.  If, for some reason, you need to do AI work with an earlier version of Wesnoth, we still provide some of the legacy documentation, but you might also have to check out the history of the wiki pages.  Another way to go about it is by checking out examples of how things are done in mainline or add-ons of the respective release series.  In general, however, we ''strongly'' recommend using the latest Wesnoth versions, as modifying AI behavior has become ''a lot'' simpler than it used to be.&lt;br /&gt;
&lt;br /&gt;
For reference, here is a list of some of the major milestones of the AI development:&lt;br /&gt;
&lt;br /&gt;
* Up to '''Wesnoth 1.6''':&lt;br /&gt;
** The default AI of Wesnoth is, in fact, called ''Default AI'', with &amp;lt;code&amp;gt;id=default_ai&amp;lt;/code&amp;gt; in the configuration.&lt;br /&gt;
* '''Wesnoth 1.1/1.2''':&lt;br /&gt;
** A Python API for AI development is introduced.  This was later removed for security reasons.&lt;br /&gt;
* '''Wesnoth 1.5/1.6''':&lt;br /&gt;
** [[FormulaAI|Formula AI]] is introduced.&lt;br /&gt;
* '''Wesnoth 1.7/1.8''':&lt;br /&gt;
** [[Wesnoth_AI_Framework|Composite AI framework]] is introduced, with the default AI functionality ported to this framework in the form of the [[RCA AI]].&lt;br /&gt;
** The [[RCA AI]] becomes the default.&lt;br /&gt;
** [[LuaAI|Lua AI]] is introduced.&lt;br /&gt;
* '''Wesnoth 1.11/1.12''':&lt;br /&gt;
** Introduction of the [[Micro AIs]]&lt;br /&gt;
** External Lua candidate actions become fully usable.  They are now the recommended method for [[Creating_Custom_AIs|creating custom AIs]]&lt;br /&gt;
* '''Wesnoth 1.15/1.16''':&lt;br /&gt;
** Merging of most of the previous [[Experimental_AI]] candidate actions into the [[RCA AI]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Experimental_AI&amp;diff=65804</id>
		<title>Experimental AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Experimental_AI&amp;diff=65804"/>
		<updated>2020-07-19T18:15:22Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Experimental AI Details */ update to current status in 1.15.3+dev&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Experimental AI Summary ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} Several of the Experimental AI candidate actions (CAs) '''have been merged into the [[RCA_AI|default (RCA) AI]]''', see descriptions below for the individual CAs.&lt;br /&gt;
&lt;br /&gt;
An ''Experimental AI'' is available for use in both MP maps and SP scenarios.  At the moment, this AI contains the following [[Wesnoth_AI_Framework#The_Candidate_Actions_of_the_main_loop_Stage|candidate actions]]:&lt;br /&gt;
* New and improved recruiting&lt;br /&gt;
* More aggressive village grabbing&lt;br /&gt;
* Healer placement behind injured units and more assertive retreating of injured units to healing locations.&lt;br /&gt;
* Leader castle switching: useful on certain MP maps&lt;br /&gt;
* Poison spreading&lt;br /&gt;
* Moving toward any enemy, if there are no enemy leaders or villages&lt;br /&gt;
* All the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|default RCA AI candidate actions]] except for recruiting&lt;br /&gt;
&lt;br /&gt;
== Experimental AI Setup ==&lt;br /&gt;
&lt;br /&gt;
In multiplayer maps, this AI is available from the game setup menu as 'Experimental AI'.  In single-player scenarios, it can be included by using the following code in a [side] tag&lt;br /&gt;
 [ai]&lt;br /&gt;
     # Deprecated version (for Wesnoth 1.14 and earlier)&lt;br /&gt;
     {EXPERIMENTAL_AI}&lt;br /&gt;
 [/ai]&lt;br /&gt;
{{DevFeature1.15|0}} The EXPERIMENTAL_AI Macro is deprecated now. The Experimental AI should be included as follows instead (this works in Wesnoth 1.14 already):&lt;br /&gt;
 [ai]&lt;br /&gt;
     # Current version (works from Wesnoth 1.14 on)&lt;br /&gt;
     ai_algorithm=experimental_ai&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Note that the EXPERIMENTAL_AI macro must be inside &amp;lt;code&amp;gt;[side][ai]&amp;lt;/code&amp;gt;, it does not work with &amp;lt;code&amp;gt;[modify_side]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[modify_ai]&amp;lt;/code&amp;gt;. If the &amp;lt;code&amp;gt;ai_algorithm&amp;lt;/code&amp;gt; version is used, the Experimental AI can also be added as&lt;br /&gt;
     [modify_side]&lt;br /&gt;
         side=2&lt;br /&gt;
         [ai]&lt;br /&gt;
             ai_algorithm=experimental_ai&lt;br /&gt;
         [/ai]&lt;br /&gt;
     [/modify_side]&lt;br /&gt;
or&lt;br /&gt;
     [modify_side]&lt;br /&gt;
         side=2&lt;br /&gt;
         switch_ai=ai/ais/ai_generic_rush.cfg  # Wesnoth 1.14 and earlier&lt;br /&gt;
         #switch_ai=ai/ais/ai_experimental.cfg # Wesnoth 1.15 and later&lt;br /&gt;
     [/modify_side]&lt;br /&gt;
&lt;br /&gt;
=== Custom Parameter Setup ===&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|2}}&lt;br /&gt;
A small number of custom parameters can be passed to the Experimental AI. These cannot be set up using the &amp;lt;code&amp;gt;ai_algorithm&amp;lt;/code&amp;gt; syntax, but require the use of a macro in the &amp;lt;code&amp;gt;[side][ai]&amp;lt;/code&amp;gt; tag:&lt;br /&gt;
 [side]&lt;br /&gt;
     [ai]&lt;br /&gt;
         {CUSTOMIZABLE_EXPERIMENTAL_AI (&lt;br /&gt;
             high_level_fraction=0.33&lt;br /&gt;
             randomness=0.2&lt;br /&gt;
         )}&lt;br /&gt;
     [/ai]&lt;br /&gt;
 [/side]&lt;br /&gt;
Note that only the parameters one wants changed need to be defined in the macro, those omitted will take their default values.&lt;br /&gt;
&lt;br /&gt;
'''Custom parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''high_level_fraction'''=0: (non-negative number) The approximate fraction of units of level 2 or higher to be recruited. This is defined as fraction of units on the map, not of new units being recruited during a turn or over several turns (which makes a difference if there are already units on the map). The effect is also cumulative per level (starting from level 2), meaning that 1/3 of L2 units will be recruited in the example above, 1/3*1/3=1/9 L3s etc. The default value is zero, which leaves the high-level unit recruiting to the default Experimental AI algorithm resulting usually in very few such units being recruited.&lt;br /&gt;
* '''randomness'''=0.1: (number)  A random number is applied to the Experimental AI recruiting score to prevent the recruitment pattern from being too predictable. 0 causes no randomness to be applied, while larger numbers increase the random effect. A value of 1-2 generates results in which the random effect is approximately equal to the scored effect. Extremely high values are essentially entirely random.&lt;br /&gt;
&lt;br /&gt;
== Experimental AI Details ==&lt;br /&gt;
&lt;br /&gt;
The Experimental AI uses most of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|candidate actions]] (CAs) of the [[RCA AI]], the one exception being the recruitment CA which is replaced by another recruiting CA.  In addition, it also adds a number of new CAs.  The following are the differences between the Experimental AI and the default (RCA) AI:&lt;br /&gt;
&lt;br /&gt;
====Recruitment====&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} At the moment, the recruiting CA remains the main difference between the default and the Experimental AI.&lt;br /&gt;
&lt;br /&gt;
The Experimental AI has a completely different recruiting algorithm that was designed to emulate the choices of a human player, especially in the first few turns.  As such, it tries to pick units that counter both what is on the battlefield and that the opponent could recruit.  As it gains more units relative to the enemy, it picks harder hitting units, even at the cost of fragility, to break through enemy lines.&lt;br /&gt;
&lt;br /&gt;
Unlike the default recruitment algorithm, it also chooses where the units are recruited in order to maximize the number of villages that can be captured and if it knows the leader will be moving to a &amp;quot;better&amp;quot; keep (see below), it will under-recruit at the first keep to project more forward power, recruiting just enough to capture all villages.&lt;br /&gt;
&lt;br /&gt;
It also adjusts its recruitment based on map size, favouring faster units on larger maps, trusting that the economy advantage from capturing more villages will more than offset the price.&lt;br /&gt;
&lt;br /&gt;
It knows about poison and avoids recruiting units that depend on it for damage if the enemy is immune.&lt;br /&gt;
&lt;br /&gt;
It also looks at the expected cost of the unit over the next few turns, which prevents the recruitment of too many fast weak units unless the additional gold from an expected capture of a village is enough to offset the increased cost.&lt;br /&gt;
&lt;br /&gt;
Time of day is taken into account and it prefers units that will reach the current closest enemy at the favoured time of day. This allows, for example, for a slight bias towards saurians during the day and drakes at night on small maps (because they will reach the fight at the right time).&lt;br /&gt;
&lt;br /&gt;
Because the AI cannot use them well, it does not accurately measure the damage done by a unit with berserk, causing them to almost never be recruited.&lt;br /&gt;
&lt;br /&gt;
There is also a small amount of randomness added to allow two units that are almost equally good according to the evaluation to be chosen evenly, instead of only picking the first one every time.&lt;br /&gt;
&lt;br /&gt;
====Villages====&lt;br /&gt;
&lt;br /&gt;
The Experimental AI prefers to capture villages over fighting. This weakens it somewhat in raw fighting but it tends to get enough extra gold that this is actually to its advantage vs. the default AI, although its current implementation is probably too focused on villages.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} This CA has '''not''' been merged into the default AI. It is too simple to work in a general setting (and therefore not suited for the default AI) and there is a separate village grabbing CA in the default AI, so it does not make much of a difference. This CA needs to be either improved or removed.&lt;br /&gt;
&lt;br /&gt;
====Healing and Retreating====&lt;br /&gt;
&lt;br /&gt;
If a healer is not used to attack, the Experimental AI will try to heal units, instead of positioning to attack later.&lt;br /&gt;
&lt;br /&gt;
In a separate CA, units retreat to villages/healers when injured.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} These two CAs have been merged into the default AI.&lt;br /&gt;
&lt;br /&gt;
====Castle Switching====&lt;br /&gt;
&lt;br /&gt;
The leader will move to different keeps, providing a forward force. Usually this works very well, especially at the beginning of the game, but it keeps moving later, which sometimes might get it killed in some scenarios. In that case, it might be worth stopping this behaviour after several turn by removing this CA.&lt;br /&gt;
&lt;br /&gt;
====Enemy Poisoning====&lt;br /&gt;
&lt;br /&gt;
There is also an explicit poison attack routine that tries to poison unpoisoned units instead of repeatedly poisoning the same one.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} This CA has been merged into the default AI.&lt;br /&gt;
&lt;br /&gt;
====Move to Any Enemy====&lt;br /&gt;
&lt;br /&gt;
This is a fall-back CA that kicks in only when no other actions are taken. Its purpose is to not let the AI units sit idle on maps without enemy leaders and villages.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} This CA has been merged into the default AI.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Experimental_AI&amp;diff=65795</id>
		<title>Experimental AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Experimental_AI&amp;diff=65795"/>
		<updated>2020-07-19T18:11:07Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Experimental AI Summary */ Update to current status in 1.15.3+dev&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Experimental AI Summary ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} Several of the Experimental AI candidate actions (CAs) '''have been merged into the [[RCA_AI|default (RCA) AI]]''', see descriptions below for the individual CAs.&lt;br /&gt;
&lt;br /&gt;
An ''Experimental AI'' is available for use in both MP maps and SP scenarios.  At the moment, this AI contains the following [[Wesnoth_AI_Framework#The_Candidate_Actions_of_the_main_loop_Stage|candidate actions]]:&lt;br /&gt;
* New and improved recruiting&lt;br /&gt;
* More aggressive village grabbing&lt;br /&gt;
* Healer placement behind injured units and more assertive retreating of injured units to healing locations.&lt;br /&gt;
* Leader castle switching: useful on certain MP maps&lt;br /&gt;
* Poison spreading&lt;br /&gt;
* Moving toward any enemy, if there are no enemy leaders or villages&lt;br /&gt;
* All the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|default RCA AI candidate actions]] except for recruiting&lt;br /&gt;
&lt;br /&gt;
== Experimental AI Setup ==&lt;br /&gt;
&lt;br /&gt;
In multiplayer maps, this AI is available from the game setup menu as 'Experimental AI'.  In single-player scenarios, it can be included by using the following code in a [side] tag&lt;br /&gt;
 [ai]&lt;br /&gt;
     # Deprecated version (for Wesnoth 1.14 and earlier)&lt;br /&gt;
     {EXPERIMENTAL_AI}&lt;br /&gt;
 [/ai]&lt;br /&gt;
{{DevFeature1.15|0}} The EXPERIMENTAL_AI Macro is deprecated now. The Experimental AI should be included as follows instead (this works in Wesnoth 1.14 already):&lt;br /&gt;
 [ai]&lt;br /&gt;
     # Current version (works from Wesnoth 1.14 on)&lt;br /&gt;
     ai_algorithm=experimental_ai&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Note that the EXPERIMENTAL_AI macro must be inside &amp;lt;code&amp;gt;[side][ai]&amp;lt;/code&amp;gt;, it does not work with &amp;lt;code&amp;gt;[modify_side]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[modify_ai]&amp;lt;/code&amp;gt;. If the &amp;lt;code&amp;gt;ai_algorithm&amp;lt;/code&amp;gt; version is used, the Experimental AI can also be added as&lt;br /&gt;
     [modify_side]&lt;br /&gt;
         side=2&lt;br /&gt;
         [ai]&lt;br /&gt;
             ai_algorithm=experimental_ai&lt;br /&gt;
         [/ai]&lt;br /&gt;
     [/modify_side]&lt;br /&gt;
or&lt;br /&gt;
     [modify_side]&lt;br /&gt;
         side=2&lt;br /&gt;
         switch_ai=ai/ais/ai_generic_rush.cfg  # Wesnoth 1.14 and earlier&lt;br /&gt;
         #switch_ai=ai/ais/ai_experimental.cfg # Wesnoth 1.15 and later&lt;br /&gt;
     [/modify_side]&lt;br /&gt;
&lt;br /&gt;
=== Custom Parameter Setup ===&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|2}}&lt;br /&gt;
A small number of custom parameters can be passed to the Experimental AI. These cannot be set up using the &amp;lt;code&amp;gt;ai_algorithm&amp;lt;/code&amp;gt; syntax, but require the use of a macro in the &amp;lt;code&amp;gt;[side][ai]&amp;lt;/code&amp;gt; tag:&lt;br /&gt;
 [side]&lt;br /&gt;
     [ai]&lt;br /&gt;
         {CUSTOMIZABLE_EXPERIMENTAL_AI (&lt;br /&gt;
             high_level_fraction=0.33&lt;br /&gt;
             randomness=0.2&lt;br /&gt;
         )}&lt;br /&gt;
     [/ai]&lt;br /&gt;
 [/side]&lt;br /&gt;
Note that only the parameters one wants changed need to be defined in the macro, those omitted will take their default values.&lt;br /&gt;
&lt;br /&gt;
'''Custom parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''high_level_fraction'''=0: (non-negative number) The approximate fraction of units of level 2 or higher to be recruited. This is defined as fraction of units on the map, not of new units being recruited during a turn or over several turns (which makes a difference if there are already units on the map). The effect is also cumulative per level (starting from level 2), meaning that 1/3 of L2 units will be recruited in the example above, 1/3*1/3=1/9 L3s etc. The default value is zero, which leaves the high-level unit recruiting to the default Experimental AI algorithm resulting usually in very few such units being recruited.&lt;br /&gt;
* '''randomness'''=0.1: (number)  A random number is applied to the Experimental AI recruiting score to prevent the recruitment pattern from being too predictable. 0 causes no randomness to be applied, while larger numbers increase the random effect. A value of 1-2 generates results in which the random effect is approximately equal to the scored effect. Extremely high values are essentially entirely random.&lt;br /&gt;
&lt;br /&gt;
== Experimental AI Details ==&lt;br /&gt;
&lt;br /&gt;
The Experimental AI uses most of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|candidate actions]] (CAs) of the [[RCA AI]], the one exception being the recruitment CA which is replaced by another recruiting CA.  In addition, it also adds a number of new CAs.  The following are the differences between the Experimental AI and the default (RCA) AI:&lt;br /&gt;
&lt;br /&gt;
====Recruitment====&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} At the moment, the recruiting CA remains the main difference between the default and the Experimental AI. We are, however, currently working on adapting the Experimental AI's recruiting to work in more general settings (mostly this means with more than one leader), after which there will be a choice which recruiting to use in the default AI.&lt;br /&gt;
&lt;br /&gt;
The Experimental AI has a completely different recruiting algorithm that was designed to emulate the choices of a human player, especially in the first few turns.  As such, it tries to pick units that counter both what is on the battlefield and that the opponent could recruit.  As it gains more units relative to the enemy, it picks harder hitting units, even at the cost of fragility, to break through enemy lines.&lt;br /&gt;
&lt;br /&gt;
Unlike the default recruitment algorithm, it also chooses where the units are recruited in order to maximize the number of villages that can be captured and if it knows the leader will be moving to a &amp;quot;better&amp;quot; keep (see below), it will under-recruit at the first keep to project more forward power, recruiting just enough to capture all villages.&lt;br /&gt;
&lt;br /&gt;
It also adjusts its recruitment based on map size, favouring faster units on larger maps, trusting that the economy advantage from capturing more villages will more than offset the price.&lt;br /&gt;
&lt;br /&gt;
It knows about poison and avoids recruiting units that depend on it for damage if the enemy is immune.&lt;br /&gt;
&lt;br /&gt;
It also looks at the expected cost of the unit over the next few turns, which prevents the recruitment of too many fast weak units unless the additional gold from an expected capture of a village is enough to offset the increased cost.&lt;br /&gt;
&lt;br /&gt;
Time of day is taken into account and it prefers units that will reach the current closest enemy at the favoured time of day. This allows, for example, for a slight bias towards saurians during the day and drakes at night on small maps (because they will reach the fight at the right time).&lt;br /&gt;
&lt;br /&gt;
Because the AI cannot use them well, it does not accurately measure the damage done by a unit with berserk, causing them to almost never be recruited.&lt;br /&gt;
&lt;br /&gt;
There is also a small amount of randomness added to allow two units that are almost equally good according to the evaluation to be chosen evenly, instead of only picking the first one every time.&lt;br /&gt;
&lt;br /&gt;
====Villages====&lt;br /&gt;
&lt;br /&gt;
The Experimental AI prefers to capture villages over fighting. This weakens it somewhat in raw fighting but it tends to get enough extra gold that this is actually to its advantage vs. the default AI, although its current implementation is probably too focused on villages.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} Besides recruiting, this is the only CA that is different between the default and Experimental AIs. It is too simple to work in a general setting (and therefore not suited for the default AI) and there is a separate village grabbing CA in the default AI, so it does not make much of a difference. This CA needs to be either improved or removed.&lt;br /&gt;
&lt;br /&gt;
====Healing and Retreating====&lt;br /&gt;
&lt;br /&gt;
If a healer is not used to attack, the Experimental AI will try to heal units, instead of positioning to attack later.&lt;br /&gt;
&lt;br /&gt;
In a separate CA, units retreat to villages/healers when injured.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} These two CAs have been merged into the default AI.&lt;br /&gt;
&lt;br /&gt;
====Castle Switching====&lt;br /&gt;
&lt;br /&gt;
The leader will move to different keeps, providing a forward force. Usually this works very well, especially at the beginning of the game, but it keeps moving later, which sometimes might get it killed in some scenarios. In that case, it might be worth stopping this behaviour after several turn by removing this CA.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} This CA has been merged into the default AI.&lt;br /&gt;
&lt;br /&gt;
====Enemy Poisoning====&lt;br /&gt;
&lt;br /&gt;
There is also an explicit poison attack routine that tries to poison unpoisoned units instead of repeatedly poisoning the same one.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} This CA has been merged into the default AI.&lt;br /&gt;
&lt;br /&gt;
====Move to Any Enemy====&lt;br /&gt;
&lt;br /&gt;
This is a fall-back CA that kicks in only when no other actions are taken. Its purpose is to not let the AI units sit idle on maps without enemy leaders and villages.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} This CA has been merged into the default AI.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Creating_Custom_AIs&amp;diff=65769</id>
		<title>Creating Custom AIs</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Creating_Custom_AIs&amp;diff=65769"/>
		<updated>2020-07-15T19:39:58Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Starting from Version 1.13.5 */ fix argument order for Lua CAs&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Too Many Methods for Creating Custom AIs ==&lt;br /&gt;
&lt;br /&gt;
The methods available for creating custom AIs have been developed over [[Wesnoth_AI#A_Very_Brief_History_of_Wesnoth_AIs|many development cycles]].  As a result, it is now much easier to create custom AIs than it used to be.  The down side is that there are now a large number of different methods to do more or less the same thing, which is unnecessary and, quite frankly, confusing.  Also, while most of these methods still work for backward compatibility reasons, many of them are not maintained any more.&lt;br /&gt;
&lt;br /&gt;
This page therefore only describes one method of creating custom AIs, custom Lua candidate actions.  Links to legacy documentation of some of the old methods are still included at the bottom of this page, but just as the methods themselves, these wiki pages are not maintained and updated any more.  We recommend not using them any more and, ideally, update old code to the new methods.&lt;br /&gt;
&lt;br /&gt;
'''Note:''' The current type of custom Lua candidate actions was originally called ''external'' Lua CAs, to distinguish them from the original method (now the ''old'' method) of setting up Lua CAs.  However, as external Lua CAs are now the standard for creating custom AIs, we drop the 'external' in the description.  It is mentioned here only in case you come across the term somewhere in older documentation.&lt;br /&gt;
&lt;br /&gt;
== Different Types of AI Modifications ==&lt;br /&gt;
&lt;br /&gt;
Modifications of the AI behavior can be accomplished in a variety of different ways:&lt;br /&gt;
* Adding new candidate actions&lt;br /&gt;
** This is the main content of this page.&lt;br /&gt;
* Changing the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|existing candidate actions]]:&lt;br /&gt;
** Modifying the evaluation scores of the candidate actions&lt;br /&gt;
** Deleting candidate actions&lt;br /&gt;
** Instructions for these tasks are also given here.&lt;br /&gt;
* Configuring the parameters/weights which determine how the [[RCA_AI|default AI]] evaluates its moves.&lt;br /&gt;
** This can be done easily in WML by [[AiWML|setting aspects and goals]].&lt;br /&gt;
** It is also possible to [[LuaAI#Dynamic_Lua_Aspects|assign dynamic aspects]] using Lua.&lt;br /&gt;
** Technically this is not really creating custom AIs, but rather customizing the existing AI.  It is not described on this page.  See the links provided instead.&lt;br /&gt;
&lt;br /&gt;
== Setting Up a Custom AI ==&lt;br /&gt;
&lt;br /&gt;
=== Creating Custom Candidate Actions ===&lt;br /&gt;
&lt;br /&gt;
The recommended way of creating a custom AI is to add Lua candidate actions (CAs) to the ''main_loop'' stage of the [[RCA_AI|default AI]].  Note that, in the following, we assume a solid knowledge of how the [[Wesnoth_AI_Framework#Stages|evaluation loop of this stage]] works, [[Wesnoth_AI_Framework#The_Candidate_Actions_of_the_main_loop_Stage|what candidate actions are in general]] and how they [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|are used in the default AI]], as well as some basic knowledge of [[LuaWML|Lua in Wesnoth]] and [[LuaAI|Lua AI]].&lt;br /&gt;
&lt;br /&gt;
We demonstrate how this is done on the example of the [[Micro_AIs#Return_Guardian_.28ai_type.3Dreturn_guardian.29|Return Guardian Micro AI]].  This is a very simple AI that is a variation of the default Wesnoth guardian.  It has a defined guard position and only two states:&lt;br /&gt;
* When the guardian is not on the guard position, it tries to return there.&lt;br /&gt;
* When it is on the guard position, it is left free to attack enemy units that are within range, but may not do any other moves.&lt;br /&gt;
Note that some minor changes have been made to the code of the actual Return Guardian Micro AI for ease of presentation.&lt;br /&gt;
&lt;br /&gt;
The code for the AI is as follows (we assume that this is in a file called 'my_addon/lua/return_guardian.lua'):&lt;br /&gt;
&amp;lt;div style=&amp;quot;height: 250px; overflow:auto&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang='lua' line highlight='13,15,28,47'&amp;gt;&lt;br /&gt;
local AH = wesnoth.require &amp;quot;ai/lua/ai_helper.lua&amp;quot;&lt;br /&gt;
&lt;br /&gt;
local function get_guardian(cfg)&lt;br /&gt;
    local filter = cfg.filter or { id = cfg.id }&lt;br /&gt;
    local guardian = AH.get_units_with_moves {&lt;br /&gt;
        side = wesnoth.current.side,&lt;br /&gt;
        { &amp;quot;and&amp;quot;, filter }&lt;br /&gt;
    }[1]&lt;br /&gt;
&lt;br /&gt;
    return guardian&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local ca_return_guardian = {}&lt;br /&gt;
&lt;br /&gt;
function ca_return_guardian:evaluation(ai, cfg)&lt;br /&gt;
    local guardian = get_guardian(cfg)&lt;br /&gt;
    if guardian then&lt;br /&gt;
        if (guardian.x == cfg.return_x) and (guardian.y == cfg.return_y) then&lt;br /&gt;
            return 99990&lt;br /&gt;
        else&lt;br /&gt;
            return 100010&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    return 0&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function ca_return_guardian:execution(ai, cfg)&lt;br /&gt;
    local guardian = get_guardian(cfg)&lt;br /&gt;
&lt;br /&gt;
    -- In case the return hex is occupied:&lt;br /&gt;
    local x, y = cfg.return_x, cfg.return_y&lt;br /&gt;
    if (guardian.x ~= x) or (guardian.y ~= y) then&lt;br /&gt;
        x, y = wesnoth.find_vacant_tile(x, y, guardian)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    local next_hop = AH.next_hop(guardian, x, y)&lt;br /&gt;
    if (not next_hop) then next_hop = { guardian.x, guardian.y } end&lt;br /&gt;
&lt;br /&gt;
   if ((next_hop[1] ~= guardian.x) or (next_hop[2] ~= guardian.y)) then&lt;br /&gt;
       ai.move_full(guardian, next_hop[1], next_hop[2])&lt;br /&gt;
   else&lt;br /&gt;
       ai.stopunit_moves(guardian)&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return ca_return_guardian&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first 12 lines are not directly relevant to setting up the AI, they simply define some utility functions.  The first important line is L.13 in combination with lines 15, 28 and 47.  A Lua candidate action needs to return a table with an evaluation and an execution function.  The mentioned lines set up that table and the two functions.  Its name is chosen to be ''ca_return_guardian'', but that is really irrelevant as it is a variable local to the file.&lt;br /&gt;
&lt;br /&gt;
The execution and evaluation function can have several arguments. The available arguments and the order in which they appear '''depend on the Wesnoth version and usage'''.&lt;br /&gt;
&lt;br /&gt;
====Before Version 1.13.5====&lt;br /&gt;
*'''ai''': The [[LuaAI#Lua_AI_Functions|Lua AI function table]].  The execution function always needs this argument to be passed, the evaluation function only in some situations.  See below for details.&lt;br /&gt;
*'''cfg''': A comma-separated list of configuration parameters, which are passed from the [candidate_action] tag using the ''eval_parms'' and ''exec_parms'' keys.  These need to be in [[LuaWML|Lua table format]], but without the outer curly brackets &amp;lt;code&amp;gt;{ }&amp;lt;/code&amp;gt;.  See below for examples.&lt;br /&gt;
*'''self''': If the persistent variable ''data'' is needed, the evaluation/execution function definitions need to include a third parameter ''self''.  The persistent variable is then accessible as ''self.data''.  See below for more information on the ''data'' variable.&lt;br /&gt;
&lt;br /&gt;
These arguments &amp;lt;u&amp;gt;must&amp;lt;/u&amp;gt; appear in order &amp;lt;code&amp;gt;(ai, cfg, self)&amp;lt;/code&amp;gt; in the function calls, but as always in Lua, trailing unused arguments can be omitted.&lt;br /&gt;
&lt;br /&gt;
====Starting from Version 1.13.5====&lt;br /&gt;
The arguments passed to the functions are different for candidate actions using the recommended [args] subtag rather than eval_parms and exec_parms (these are explained in more detail below). The new arguments are as follows:&lt;br /&gt;
*'''self''': The engine data. By default, this replicates the ''self'' parameter from the old system for compatibility, meaning that ''self.data'' is the same as ''data''. If an [engine] tag was explicitly defined for Lua, however, this parameter is the value that the engine's code returned, which could be pretty much anything.&lt;br /&gt;
*'''cfg''': The contents of the [args] subtag; parameters specific to this candidate action. With this, you can use the same code with different parameters to produce two different candidate actions.&lt;br /&gt;
*'''data''': The persistent variable ''data''. This is the same as ''self.data'' in the old system. If an [engine] tag was explicitly defined for the Lua engine, this is initially equal to the value of the [data] subtag. However, unlike the other two parameters, this value is mutable.&lt;br /&gt;
*'''filter_own''': {{DevFeature1.15|3}} The content of the &amp;lt;code&amp;gt;[candidate action][filter_own]&amp;lt;/code&amp;gt; tag as described [[Wesnoth_AI_Framework#The_.5Bcandidate_action.5D_Tag|here]].&lt;br /&gt;
&lt;br /&gt;
If the functions are defined using '''colon notation''' as in the above example, then the ''self'' parameter is implicitly defined and the arguments must be in order &amp;lt;code&amp;gt;(cfg, data, filter_own)&amp;lt;/code&amp;gt; in the function calls. If you prefer to explicitly define it, use '''a dot instead of a colon''' when defining the function, in which case the arguments must be passed as &amp;lt;code&amp;gt;(self, cfg, data, filter_own)&amp;lt;/code&amp;gt;. As always in Lua, you can omit any of the arguments if the function does not need to access the respective information.&lt;br /&gt;
&lt;br /&gt;
Notice that the ''ai'' table is no longer included among the arguments passed to the table. This is because it's now available as a &amp;quot;scoped global variable&amp;quot; anywhere in AI code, so there is no need to pass it around.&lt;br /&gt;
&lt;br /&gt;
====Evaluation Function====&lt;br /&gt;
&lt;br /&gt;
In our example, the call to the evaluation contains the first two of these arguments, the ''ai'' table and the configuration table ''cfg''.  The ''ai'' table is not needed for this particular evaluation function.  It is there only as a placeholder because the ''cfg'' table is needed and always needs to be passed as the second argument.&lt;br /&gt;
&lt;br /&gt;
The ''cfg'' table contains information about the guardian unit and the guard position.  We will show how this is passed to the evaluation function later.  For now, we only need to know that it contains either a ''filter'' or ''id'' key, used in L.16 (call to function ''get_guardian'' defined in L.4) to check whether there is a guardian unit on the map, and keys ''return_x'' and ''return_y'' defining the guard position.&lt;br /&gt;
&lt;br /&gt;
A CA evaluation function returns a numerical score which determines when in the ''main_loop'' stage evaluation loop it is executed.  The return guardian code evaluation has three possible return values.  First (or rather: last), if no guardian unit is found it returns 0 (L.25), which means nothing is to be done for this CA.&lt;br /&gt;
&lt;br /&gt;
If a guardian unit is found, there are two possible scores.  The code first checks whether the guardian is at its guard position (L.18).  If so, the return score is 99,990, if not, it is 100,010.  The relevance of these values is that they are just below and just above the score of the ''combat'' (attack) CA of the [[RCA_AI|default AI]].  Thus, if the guardian is not at its return position, the return guardian CA has a higher score than any attacks done by the default AI, and the unit is returned to its guard position whether there is an enemy unit in range or not.&lt;br /&gt;
&lt;br /&gt;
If, on the other hand, the guardian is already at its guard position, the return score is lower than that of the default ''combat'' CA.  The default AI is thus free to use the unit in an attack.&lt;br /&gt;
&lt;br /&gt;
It is important to notice that the function may not return 0 when the guardian is at its guard position.  Imagine the situation that the guardian is at its position and has no unit to attack.  If the return score were 0 in this case, CAs with score lower than the combat CA would take over.  In particular, the ''move-to-targets'' CA would kick in and move the guardian toward one of the default AI [[RCA_AI#RCA_AI_Aspect_and_Goal_Configuration|goals]].  This could be done by removing these other CAs, but then it would not be available for other, non-guardian units of the AI side either.  A better way to deal with this is to remove the movement of the guardian unit if it is at its guard position and no attack happened.  This is done by using a return score of 99,990 in this situation, in combination with what the execution function does, as described in the following.&lt;br /&gt;
&lt;br /&gt;
====Execution Function====&lt;br /&gt;
&lt;br /&gt;
The execution function is called with the same two arguments as the evaluation function.  The ''cfg'' table is needed again in order to find the guardian (L.29) and the position to move to.  The position is either the return hex (L.32) or, in case it is occupied by a unit other than the guardian itself, a vacant hex close to it (L.34).  Lines 37 and 38 then find the closest hex the unit can move to in practice, using the [https://github.com/wesnoth/wesnoth/blob/master/data/ai/lua/ai_helper.lua ''ai_helper.lua''] utility function [https://github.com/wesnoth/wesnoth/blob/master/data/ai/lua/ai_helper.lua#L910 ''next_hop()''].&lt;br /&gt;
&lt;br /&gt;
Lines 40–44 contain the actual execution of the AI move.  If the guardian is not at the ''next_hop'' position, it will be moved there and movement will be set to zero; that's what ''ai.move_full()'' does, as opposed to ''ai.move()''.  If, on the other hand, the ''next_hop'' position is its current position (which means that it is either already at the guard position or cannot get any closer), movement is removed using ''ai.stopunit_moves()'', so that the other CAs of the default AI do not move it away afterward.  Note that, in the latter case, we do not remove attacks, so that the default AI is still free to attack if the guardian ends up next to an enemy unit.&lt;br /&gt;
&lt;br /&gt;
This is it for the AI code itself.  We can see that it is possible to set up reasonable versatile AI behavior with just a few dozen lines of code by working with and around the behavior of the default AI.  In the following, we show how to add this code to the default AI.  But first:&lt;br /&gt;
&lt;br /&gt;
'''A few more notes:'''&lt;br /&gt;
&lt;br /&gt;
* It is '''very important''' that CA evaluation functions do not change the game state as that leads to out of sync (OOS) errors.&lt;br /&gt;
* Also '''very important''': If a candidate action is selected for execution but fails to change the game state (e.g. by trying to do an illegal move/attack/recruit), then it will be blacklisted until the end of turn. This is done to prevent infinite loops and the blacklisting is handled automatically by the engine.  This means, however, that you need to be make sure that the execution of a move is possible, otherwise all subsequent moves which the CA would have done during this turn will not happen.  This sort of check can, for example, be done with the ''ai.check*()'' Lua AI functions.&lt;br /&gt;
** For a description of these functions, and all other functions of the ''ai'' table, see [[LuaAI#Lua_AI_Functions|here]].&lt;br /&gt;
* The table returned by the code  (''ca_return_guardian'' in our example) may also contain other functions.  For example, the ''get_guardian()'' function could be included in the table rather than by closure as we did it here.  It does not matter what other elements the table contains, as long as the evaluation and execution functions exist.&lt;br /&gt;
* For many more examples of other candidate actions, check out the [https://github.com/wesnoth/wesnoth/tree/master/data/ai/micro_ais/cas Micro AI CAs]] in the Wesnoth repository.&lt;br /&gt;
&lt;br /&gt;
=== Including Custom Candidate Actions in Scenarios ===&lt;br /&gt;
&lt;br /&gt;
The last step needed is adding this CA code into the AI configuration.  The following instruction are for adding the AI to a scenario (this works in both single and multi player) at scenario setup time.  Custom AIs can also be added to [[#Custom_AIs_in_Eras_and_Modifications|eras and modifications]] or [[#Mid-scenario_Candidate_Action_Changes|while a game is in progress]] in very similar ways.&lt;br /&gt;
&lt;br /&gt;
Let us assume that the code above is saved in a file called ''my_addon/lua/return_guardian.lua'' and that we want to activate it for a unit with id 'Bob' of side 1 with guard position (10,15).  The easiest way of inserting the candidate action is by using the MODIFY_AI_ADD_CANDIDATE_ACTION macro.&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    {MODIFY_AI_ADD_CANDIDATE_ACTION 1 main_loop (&lt;br /&gt;
        [candidate_action]&lt;br /&gt;
            engine=lua&lt;br /&gt;
            name=return_guardian_bob&lt;br /&gt;
            id=return_guardian_bob&lt;br /&gt;
            max_score=100010&lt;br /&gt;
            location=&amp;quot;~add-ons/my_addon/lua/return_guardian.lua&amp;quot;&lt;br /&gt;
            [args]&lt;br /&gt;
                id=Bob&lt;br /&gt;
                return_x=10&lt;br /&gt;
                return_y=15&lt;br /&gt;
            [/args]&lt;br /&gt;
            # In the older syntax, the above would have looked like this:&lt;br /&gt;
            # eval_parms=&amp;quot;id = 'Bob', return_x = 10, return_y = 15&amp;quot;&lt;br /&gt;
            # exec_parms=&amp;quot;id = 'Bob', return_x = 10, return_y = 15&amp;quot;&lt;br /&gt;
        [/candidate_action]&lt;br /&gt;
    )}&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
This needs to be placed in the [side] tag of side 1.  Of course, you can also use the [modfiy_ai] tag directly without going through the macro.  See [[Modifying_AI_Components#Modifying_Candidate_Action_of_the_main_loop_Stage|here]] for the syntax.&lt;br /&gt;
&lt;br /&gt;
Explanation of the keys:&lt;br /&gt;
*'''engine''': The engine to be used for this CA.  Possible values are 'cpp', 'lua' and 'fai', but for a Lua AI CA it always has to be 'lua'.&lt;br /&gt;
*'''id''' and '''name''': These are, in principle, optional.  However, they are generally useful to assign for clarity.  Furthermore, if you want to remove or change a candidate action later, you need to know its id.&lt;br /&gt;
*'''max_score''': The maximum score the CA evaluation function might return.  This parameter is also optional, but it is useful to reduce the evaluation time of the ''main_loop'' stage, which only evaluates CAs with max_score larger than those of the currently highest-scoring CA found.  See [[Wesnoth_AI_Framework#The_.5Bcandidate_action.5D_Tag|here]] for a little more on that.&lt;br /&gt;
*'''location''': File name, including path, of the AI code for the CA&lt;br /&gt;
*'''eval_parms''', '''exec_parms''': (string) The parameters to be passed to the evaluation and execution functions of the AI.  These need to be strings in form of a Lua table, without the outer curly brackets.  They are passed to the functions as the second argument, called ''cfg'' in the example above.&lt;br /&gt;
** The strings do not necessarily need to be in [[LuaWML#Encoding_WML_objects_into_Lua_tables|WML object Lua table]] format.  However, this is, of course, necessary if they are to be used in functions that require that format, as in the ''filter'' example shown below.&lt;br /&gt;
** {{DevFeature1.13|5}} These string parameters are now replaced by the [args] WML tag. For the time being, the string parameters will continue to work, but they will be removed entirely in the next development cycle.&lt;br /&gt;
*'''[args]''': {{DevFeature1.13|5}} WML data to be passed to both the evaluation and execution functions. A copy of the data is passed, so even if the functions modify it, it will always be the same on the next call. This replaces eval_parms and exec_parms.&lt;br /&gt;
&lt;br /&gt;
The example above requires&lt;br /&gt;
 cfg = { id = 'Bob', return_x = 10, return_y = 15 }&lt;br /&gt;
to be passed to the evaluation and execution functions.  This is used in the [candidate_action] tag code snippet shown above — without the out &amp;lt;code&amp;gt;{ }&amp;lt;/code&amp;gt;, as mentioned.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if you want to pass, for example, a [filter] tag that includes the unit id and type, it would look like this&lt;br /&gt;
 cfg={ { filter, { id = 'Bob', type = 'Assassin' } }, return_x = 10, return_y = 15&amp;quot; }&lt;br /&gt;
resulting in the following parameter string definition&lt;br /&gt;
 eval_parms=&amp;quot;{ filter, { id = 'Bob', type = 'Assassin' } }, return_x = 10, return_y = 15&amp;quot;&lt;br /&gt;
Again, identical except for the outer curly brackets, and repeated for clarity.&lt;br /&gt;
&lt;br /&gt;
That's most of what one needs to know for the creation of custom AIs.  Some additional information on variations of using custom CAs as well as other AI modifications is given in the following.  We also list some of the other methods available for adding custom AIs at the bottom of this page.  As we point out above, however, we do not recommend using those any more.  Neither the code itself nor the wiki pages are maintained any more at this time and, in an attempt to simplify and standardize the AI code, they might be removed at some point in the future.&lt;br /&gt;
&lt;br /&gt;
=== The Persistent ''data'' Variable ===&lt;br /&gt;
&lt;br /&gt;
It is sometimes necessary to transfer information between the evaluation and execution functions of the same CA or between CAs. It might also be necessary to have this information persist from turn to turn and across save/load cycles.  The Wesnoth AI therefore adds a Lua table called ''data'' which is added to the third argument, ''self'', of the evaluation and execution function declarations described above.&lt;br /&gt;
&lt;br /&gt;
Some more information on its implementation in Lua AI is given [[LuaAI#Lua_AI_Variables_.E2.80.94_Persistence|here]].  For most practical intents and purposes it is sufficient to know that you can write at will to this table and read the information again from other functions and/or at other turns.  The only things to keep in mind are:&lt;br /&gt;
* The functions requiring access to the table need to have the third parameter specified in their declaration.  If it is called ''self'', the table is accessible as ''self.data''.&lt;br /&gt;
* If you want content to persist over save/load cycles, all content of ''data'' needs to be in [[LuaWML#Encoding_WML_objects_into_Lua_tables|WML table format]].  Other format content can also be used during an AI turn, for example to pass it between the evaluation and execution functions of the same CA, but it is lost when saving the game.&lt;br /&gt;
* The CAs of the [[Micro AIs]] make extensive use of the ''data'' variable.  You can check out [https://github.com/wesnoth/wesnoth/tree/master/data/ai/micro_ais/cas their code] for examples.&lt;br /&gt;
&lt;br /&gt;
=== Custom AIs in Eras and Modifications ===&lt;br /&gt;
&lt;br /&gt;
Custom AIs can also be included in [[EraWML|eras]] and [[ModificationWML|modifications]] for use with any MP map that allows them.  The setup is entirely equivalent to that for scenarios, with two very minor differences:&lt;br /&gt;
* The MODIFY_AI_ADD_CANDIDATE_ACTION macro needs to be placed inside [era][ai] or [modification][ai] instead of in [side][ai].&lt;br /&gt;
* The '''description''' attribute needs to be set inside the [ai] tag.  This is the text which will show up in the computer player selection menu during the MP game setup.&lt;br /&gt;
* {{DevFeature1.13|5}} Scenarios wishing to use such AIs may reference them using the '''ai_algorithm''' key in [side][ai], and modify them further with additional AI component tags such as aspect values.&lt;br /&gt;
&lt;br /&gt;
As an example, the following code adds the CA described above to a modification.&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[modification]&lt;br /&gt;
    id=guardian_bob&lt;br /&gt;
    name= _ &amp;quot;Guardian Bob AI&amp;quot;&lt;br /&gt;
    description= _ &amp;quot;Adds code for Guardian Bob to the default AI&amp;quot;&lt;br /&gt;
    [ai]&lt;br /&gt;
        description=_&amp;quot;Multiplayer_AI^Default AI + Guardian Bob&amp;quot;&lt;br /&gt;
        {MODIFY_AI_ADD_CANDIDATE_ACTION 1 main_loop (&lt;br /&gt;
            [candidate_action]&lt;br /&gt;
                engine=lua&lt;br /&gt;
                name=return_guardian_bob&lt;br /&gt;
                id=return_guardian_bob&lt;br /&gt;
                max_score=100010&lt;br /&gt;
                location=&amp;quot;~add-ons/my_addon/lua/return_guardian.lua&amp;quot;&lt;br /&gt;
                eval_parms=&amp;quot;id = 'Bob', return_x = 10, return_y = 15&amp;quot;&lt;br /&gt;
                exec_parms=&amp;quot;id = 'Bob', return_x = 10, return_y = 15&amp;quot;&lt;br /&gt;
            [/candidate_action]&lt;br /&gt;
        )}&lt;br /&gt;
    [/ai]&lt;br /&gt;
[/modification]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Mid-scenario Candidate Action Changes ===&lt;br /&gt;
&lt;br /&gt;
The instructions given above are written for setting up custom candidate actions at the beginning of a game.  However, the [modify_ai] tag and the macros using it can be used at any time during a scenario in an [event] tag. It is entirely equivalent to the methods described above.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* In this case, the [modify_ai] tag is placed '''directly inside the event''', without requiring an [ai] tag.&lt;br /&gt;
* See  [[Modifying_AI_Components#The_.5Bmodify_ai.5D_Tag|here]] for details and examples.&lt;br /&gt;
&lt;br /&gt;
== Additional Information for Creating Custom AIs ==&lt;br /&gt;
&lt;br /&gt;
=== Adding the ''main_loop'' Stage from Scratch ===&lt;br /&gt;
&lt;br /&gt;
Sometimes it might be useful or convenient to add the entire ''main_loop'' stage from scratch, rather than adding or deleting individual candidate actions.  If that is desired, the syntax for the example of the default AI plus Guardian Bob from above looks as shown below.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} The example below is from a previous Wesnoth version. New candidate actions have since been added to the default AI. The latest up-to-date version can always be found in file [https://github.com/wesnoth/wesnoth/blob/master/data/ai/ais/ai_default_rca.cfg ai/ais/ai_default_rca.cfg] in the Wesnoth data directory&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    description=_&amp;quot;Multiplayer_AI^Default AI + Guardian Bob&amp;quot;&lt;br /&gt;
    version=10710&lt;br /&gt;
    [stage]&lt;br /&gt;
        id=main_loop&lt;br /&gt;
        name=ai_default_rca::candidate_action_evaluation_loop&lt;br /&gt;
        {AI_CA_GOTO}&lt;br /&gt;
        {AI_CA_RECRUITMENT}&lt;br /&gt;
        {AI_CA_MOVE_LEADER_TO_GOALS}&lt;br /&gt;
        {AI_CA_MOVE_LEADER_TO_KEEP}&lt;br /&gt;
        {AI_CA_HIGH_XP_ATTACK} # Wesnoth 1.13.6 and later only&lt;br /&gt;
        {AI_CA_COMBAT}&lt;br /&gt;
        {AI_CA_HEALING}&lt;br /&gt;
        {AI_CA_VILLAGES}&lt;br /&gt;
        {AI_CA_RETREAT}&lt;br /&gt;
        {AI_CA_MOVE_TO_TARGETS}&lt;br /&gt;
        {AI_CA_LEADER_SHARES_KEEP}&lt;br /&gt;
        [candidate_action]&lt;br /&gt;
            engine=lua&lt;br /&gt;
            name=return_guardian_bob&lt;br /&gt;
            id=return_guardian_bob&lt;br /&gt;
            max_score=100010&lt;br /&gt;
            location=&amp;quot;~add-ons/my_addon/lua/return_guardian.lua&amp;quot;&lt;br /&gt;
            eval_parms=&amp;quot;id = 'Bob', return_x = 10, return_y = 15&amp;quot;&lt;br /&gt;
            exec_parms=&amp;quot;id = 'Bob', return_x = 10, return_y = 15&amp;quot;&lt;br /&gt;
        [/candidate_action]&lt;br /&gt;
    [/stage]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
This can be included in [side], [era] or [modification] tags.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The '''description''' attribute is ignored if this is included in a [side] tag.  It can be omitted in that case.&lt;br /&gt;
* It used to be necessary to include even the unmodified [[RCA AI]] for computer controlled sides.  Usually this was done by including a file with the AI configuration, for example by adding a line such as&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
 {ai/ais/ai_default_rca.cfg}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
to the [side] tag.  While it does no harm to still do so, it is not needed any more.  If no other stage is defined, the default AI is automatically included for all computer controlled sides.&lt;br /&gt;
&lt;br /&gt;
=== Modifying Default AI Candidate Actions===&lt;br /&gt;
&lt;br /&gt;
In addition to customizing the default AI behavior using [[AiWML|aspects and goals]], the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|default candidate actions]] can be deleted selectively and their scores can be changed.&lt;br /&gt;
&lt;br /&gt;
==== Deleting Default AI Candidate Actions ====&lt;br /&gt;
&lt;br /&gt;
Just as all the previous tasks described on this page, this can also be done with the [[Modifying_AI_Components#The_.5Bmodify_ai.5D_Tag|[modify_ai]]] tag.  For example, if we wanted to create an AI that does not attack, we can remove the ''combat'' CA.&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[modify_ai]&lt;br /&gt;
    side=2&lt;br /&gt;
    action=delete&lt;br /&gt;
    path=stage[main_loop].candidate_action[combat]&lt;br /&gt;
[/modify_ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Changing the Score of a Default AI Candidate Action ====&lt;br /&gt;
&lt;br /&gt;
The return score is pretty much the only thing that can be changed about a default AI CA (other than the customizing with aspects and goals described at the link above).  This can be done using &amp;lt;code&amp;gt;action=change&amp;lt;/code&amp;gt; in the [[Modifying_AI_Components#The_.5Bmodify_ai.5D_Tag|[modify_ai]]] tag.  For example, if we want to have an AI that attacks only after the ''healing'', ''villages'' and ''retreat'' CAs (with default scores 80,000, 60,000 and 40,000) have finished, we can change the ''combat'' CA to have a score of 30,000.&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[modify_ai]&lt;br /&gt;
    side=1&lt;br /&gt;
    action=change&lt;br /&gt;
    path=stage[main_loop].candidate_action[combat]&lt;br /&gt;
    [candidate_action]&lt;br /&gt;
        id=combat&lt;br /&gt;
        engine=cpp&lt;br /&gt;
        name=ai_default_rca::combat_phase&lt;br /&gt;
        max_score=30000&lt;br /&gt;
        score=30000&lt;br /&gt;
    [/candidate_action]&lt;br /&gt;
[/modify_ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* Note that the ''change'' action is really a ''delete'' action followed by an ''add'' action.  Thus, the full content of the [candidate_action] tag needs to be given, not just the parameter that is being changed.&lt;br /&gt;
* [[Modifying_AI_Components#Modifying_AI_Components_Helper_Macros|Macro versions]] also exist for these actions.&lt;br /&gt;
* The configurations of the default CAs, including their ids and names, can be found in file [https://github.com/wesnoth/wesnoth/blob/master/data/core/macros/ai_candidate_actions.cfg data/core/macros/ai_candidate_actions.cfg].&lt;br /&gt;
&lt;br /&gt;
== On the Fly Candidate Action Testing ==&lt;br /&gt;
&lt;br /&gt;
One of the advantages of using Lua for custom AIs is that there is no need for restarting Wesnoth after the code is changed for the changes to take effect.  In fact, it is even possible to set things up so that not even a reload is required.  You can simply change the code and click on a context menu option (or even just use a keyboard shortcut) to test the new version of the candidate action's evaluation and/or execution function.  This section describes how to set up such a testing mechanism using the example of the Guardian Bob AI described above.&lt;br /&gt;
&lt;br /&gt;
The context menu option, along with hotkey 'x', is set up by adding the following code to the WML of whatever scenario you want to test this in.&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[set_menu_item]&lt;br /&gt;
    id = m01&lt;br /&gt;
    description=_&amp;quot;Run Lua test code&amp;quot;&lt;br /&gt;
    image=items/ring-red.png~CROP(26,26,20,20)&lt;br /&gt;
    [command]&lt;br /&gt;
        [lua]&lt;br /&gt;
            code=&amp;lt;&amp;lt;wesnoth.dofile &amp;quot;~add-ons/my_addon/lua/test_lua.lua&amp;quot;&amp;gt;&amp;gt;&lt;br /&gt;
        [/lua]&lt;br /&gt;
    [/command]&lt;br /&gt;
    [default_hotkey]&lt;br /&gt;
        key=x&lt;br /&gt;
    [/default_hotkey]&lt;br /&gt;
[/set_menu_item]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This should be pretty self-explanatory.  The only thing to note is that you need to have a file called 'test_lua.lua' at the path given in the ''code'' attribute (or change the path and file name used there, of course).  This file could, for example, look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang='lua'&amp;gt;&lt;br /&gt;
-- Set up CA specific variable&lt;br /&gt;
local fn = &amp;quot;~add-ons/my_addon/lua/return_guardian.lua&amp;quot;&lt;br /&gt;
local cfg = { id = 'Bob', return_x = 10, return_y = 15 }&lt;br /&gt;
&lt;br /&gt;
-- Clear screen and make sure we're in debug mode&lt;br /&gt;
wesnoth.clear_messages()&lt;br /&gt;
wesnoth.message('AI testing for side ' .. wesnoth.current.side)&lt;br /&gt;
if (not wesnoth.game_config.debug) then&lt;br /&gt;
    wesnoth.message(&amp;quot;***** This option requires debug mode. Activate by typing ':debug' *****&amp;quot;)&lt;br /&gt;
    return&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Set up the AI variables needed by the CA&lt;br /&gt;
local self = { data = {} }&lt;br /&gt;
local ai = wesnoth.debug_ai(wesnoth.current.side).ai&lt;br /&gt;
&lt;br /&gt;
-- Evaluate CA and execute if score &amp;gt; 0&lt;br /&gt;
local eval = wesnoth.dofile(fn):evaluation(ai, cfg, self)&lt;br /&gt;
wesnoth.message('Eval score:', eval)&lt;br /&gt;
if (eval &amp;gt; 0) then&lt;br /&gt;
    wesnoth.dofile(fn):execution(ai, cfg, self)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The ''fn'' and ''cfg'' variables define the file containing the CA to be tested, and the configuration table to be passed to the evaluation and execution tables.  We assume the same Guardian Bob AI here that is used in the example above.&lt;br /&gt;
&lt;br /&gt;
The next block is simply some &amp;quot;house keeping&amp;quot;, cleaning the screen and making sure that we are in debug mode.&lt;br /&gt;
&lt;br /&gt;
We then need to set up the ''self'' and ''ai'' tables required by the CA functions.  ''self'' does not need to contain anything other than an empty ''data'' table (and even that is not required for our example, we do this here simply to demonstrate how it is done).&lt;br /&gt;
&lt;br /&gt;
By contrast, the ''ai'' table needs to be populated with all the Lua AI functions.  These are not available by default outside the AI context, but can be accessed through &amp;lt;code&amp;gt;wesnoth.debug_ai(wesnoth.current.side).ai&amp;lt;/code&amp;gt; as described [[LuaAI#Debug_access_to_the_AI_tree|here]].  This requires debug mode to be active, which is why we check for it earlier in the code.&lt;br /&gt;
&lt;br /&gt;
The last few lines are then simply calling the evaluation function and, if it returns a positive score, the execution function, thus simulating the use of the CA by the ''main_loop'' stage.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* It is important to realize that we are using wesnoth.dofile() for including files, rather than wesnoth.require().  Using the latter, while recommended for the final version of your code, would require a reload each time a file is changed.  This is not needed with ''wesnoth.dofile()''.  You can simply change the file and press 'x' again to test the new code.&lt;br /&gt;
* It is, of course, also possible to test other Lua AI (or non-AI) functionality in this way, not just the evaluation and/or execution of CAs.  To that end:&lt;br /&gt;
** The [http://forum.wesnoth.org/viewtopic.php?f=58&amp;amp;t=28320&amp;amp;start=45 Wesnoth Lua Pack] debug utilities are an extremely powerful tool for trouble shooting AI code written in Lua; or for figuring out some of the Lua AI functions in the first place.  One could, for example, use ''debug_utils.dbms()'' to see all the content of the ''ai'' table, or check out the structure of the return tables of some of the more complex functions.&lt;br /&gt;
* If you are doing a lot of AI testing, it might be worth to set up a dedicated test scenario with a variety of right-click menu options, for example for CA evaluation only, CA evaluation plus execution, running your own little CA evaluation loop and executing random Lua code.  Having several menu options with their own hotkeys in combination with manipulating units on the map in debug mode is a very powerful and convenient method for AI testing.&lt;br /&gt;
&lt;br /&gt;
== Legacy Methods for Creating Custom AIs ==&lt;br /&gt;
&lt;br /&gt;
Several other (older) methods still exist for creating custom AIs.  However, there is nothing that can be done with them that cannot also be done with the methods described on this page.  While the old methods are still functional at the moment, they might therefore be removed at some point in order to simplify maintenance of the Wesnoth C++ code base.  We do not recommend using them for new AI code any more. Ideally, old AI code should be ported to the new methods.&lt;br /&gt;
&lt;br /&gt;
Nevertheless, for the time being the documentation for using the old methods still exists.  This includes:&lt;br /&gt;
* [[Lua_AI_Legacy_Methods_Howto|Old Lua AI candidate action syntax]] requiring a Lua engine definition&lt;br /&gt;
* [[Lua_AI_Legacy_Methods_Howto#Behavior_.28Sticky.29_Candidate_Actions|Behavior candidate actions]]&lt;br /&gt;
* [[FormulaAI|Formula AI]]&lt;br /&gt;
* Using [[Wesnoth_AI_Framework#Legacy_Stages|stages other than the ''main_loop'' stage]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=RCA_AI&amp;diff=65467</id>
		<title>RCA AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=RCA_AI&amp;diff=65467"/>
		<updated>2020-03-04T16:26:37Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* The Candidate Actions (CAs) of the main_loop Stage in the RCA AI */ castle switch was removed from mainline&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==RCA AI Summary==&lt;br /&gt;
&lt;br /&gt;
The RCA (Register Candidate Action) AI is the default AI since Wesnoth 1.9.  Its name stems from the fact that it has a list of potential actions, called ''candidate actions'' (CAs), that are evaluated for each move and are executed in order of evaluation score.  The RCA AI is highly configurable in WML.  It can also be modified by removing default CAs or adding custom CAs.&lt;br /&gt;
&lt;br /&gt;
This page describes how the RCA AI is assembled out of the different components of Wesnoth's [[Wesnoth_AI_Framework#Wesnoth_AI_Framework:_A_Composite_AI|composite AI framework]].  For related information also see:&lt;br /&gt;
* [[Wesnoth_AI_Framework#Types_of_AI_Components|Types of AI Components]]: General descriptions of the different AI components.&lt;br /&gt;
* [[AiWML]]: Customizing the RCA AI by setting AI parameters (aspects and goals)&lt;br /&gt;
* [[Creating Custom AIs]] and [[Modifying AI Components]]: Adding additional functionality to the RCA AI, or replacing parts or all of it.&lt;br /&gt;
* [[Wesnoth AI]]: Links to all Wesnoth AI resources on this wiki.&lt;br /&gt;
&lt;br /&gt;
== RCA AI components  ==&lt;br /&gt;
&lt;br /&gt;
The RCA AI utilizes the following components:&lt;br /&gt;
&lt;br /&gt;
* The [[Wesnoth_AI_Framework#Stages|'''''main_loop'' stage''']] with its candidate action evaluation loop.&lt;br /&gt;
&lt;br /&gt;
* [[Wesnoth_AI_Framework#The_Candidate_Actions_of_the_main_loop_Stage|'''Candidate actions''']] are the potential actions the AI might take during an AI turn, such as attacking, village grabbing, recruiting or retreating injured units.&lt;br /&gt;
&lt;br /&gt;
* [[Wesnoth_AI_Framework#Aspects_and_Goals|'''Aspects''']] are the parameters which can be used to configure the AI behavior.&lt;br /&gt;
&lt;br /&gt;
* [[Wesnoth_AI_Framework#Aspects_and_Goals|'''Goals''']] are a specific type of aspect which define targets toward which the AI moves.  It is ''very important'' to understand that these are only move-to targets, not attack targets.&lt;br /&gt;
&lt;br /&gt;
Links here are to the general descriptions of these components in the [[Wesnoth AI Framework]], while the remainder of this page is about their use in the RCA AI.&lt;br /&gt;
&lt;br /&gt;
=== The Candidate Actions (CAs) of the ''main_loop'' Stage in the RCA AI ===&lt;br /&gt;
&lt;br /&gt;
For each move of its turn, the AI evaluates a number of potential actions, called ''candidate actions'' (CAs). The CA with the highest evaluation score is executed and the process is repeated, until no more valid moves are found.  This ends the AI turn.  (Note: This is the general process of the [[Wesnoth_AI_Framework#Stages|''main_loop'' stage]], it is not specific to the RCA AI.)&lt;br /&gt;
&lt;br /&gt;
Most of the RCA AI CAs have a single fixed scores, with a few of the Lua CAs have two distinct (but otherwise also fixed) scores, as indicated below.  These values can, in principle, be modified, but generally only to other fixed values.  For example, the recruitment CA by default has a score of 180,000.  The default score for attacks is 100,000.  Thus, if recruiting is possible, it will always be done before attacks.  If the score of the recruitment CA were lowered to below 100,000, then attacks would always be evaluated (and executed, if possible) before recruiting.&lt;br /&gt;
&lt;br /&gt;
It is important to realize though that the default score settings do not mean that all recruiting always happens before all attacks.  The &amp;quot;if possible&amp;quot; clauses in the previous paragraph become important here.  Consider the case that all castle hexes are occupied by AI units which were recruited on the previous turn and have not been used yet this turn.  No recruiting is possible (in which case the CA returns 0), so the AI moves on to other CAs.  Once it gets to attacks, one of the units (or a group of units) might be selected to move off the castle and attack a close-by enemy.  Now a castle hex is available for recruiting, which now has the highest evaluation score and is executed.  This cycle might be repeated several times.&lt;br /&gt;
&lt;br /&gt;
In its default configuration, the RCA AI evaluates the following CAs, sorted here in order of their evaluation score:&lt;br /&gt;
&lt;br /&gt;
*'''Goto''' CA (score 200,000; C++ CA): Move units toward the coordinates set by [[SingleUnitWML|goto_x, goto_y]] in their unit WML.&lt;br /&gt;
&lt;br /&gt;
*{{DevFeature1.15|3}} '''Retreat injured''' CA (scores 192,000 and 95,000; Lua CA): retreat injured units that are threatened by enemies to healing locations and away from these enemies.&lt;br /&gt;
&lt;br /&gt;
*{{DevFeature1.15|3}} '''Spread poison''' CA (score 190,000; Lua CA): spread poison to enemies, while taking care not to try to poison unpoisonable or already poisoned units.&lt;br /&gt;
&lt;br /&gt;
*'''Recruitment''' CA (score 180,000; C++ CA): Recruit or recall units.&lt;br /&gt;
&lt;br /&gt;
*'''Move leader to goals''' CA (score 140,000; C++ CA): Move the leader toward goals set by [[AiWML#List_of_AI_Aspects|[leader_goal]]].&lt;br /&gt;
&lt;br /&gt;
*'''Move leader to keep''' CA (score 120,000; C++ CA): Move the leader toward the closest available keep.&lt;br /&gt;
&lt;br /&gt;
*'''High XP attack''' CA (score 100,010; Lua CA): An addition to the default AI as of '''Wesnoth 1.13.6''', this CA performs attacks on enemy units so close to leveling that the combat CA (below) does not attack them.&lt;br /&gt;
&lt;br /&gt;
*'''Combat''' CA (score 100,000; C++ CA): Attack enemy units that are in range.  Note that this CA includes the move to a hex adjacent to the enemy unit as well as the attack itself.&lt;br /&gt;
&lt;br /&gt;
*{{DevFeature1.15|3}} '''Place healers''' CA (score 96,000; Lua CA): Place healers adjacent to injured units that have already moved.&lt;br /&gt;
&lt;br /&gt;
*'''Healing''' CA (score 80,000; C++ CA): Move units onto healing locations (generally villages, but also next to healers that were placed previously).&lt;br /&gt;
&lt;br /&gt;
*'''Villages''' CA (score 60,000; C++ CA): Move units onto villages that are unowned or owned by an enemy.&lt;br /&gt;
&lt;br /&gt;
*'''Retreat''' CA (score 40,000; C++ CA): Evaluate if any units are in grave danger or hopelessly outnumbered, and if so, retreat them.  *{{DevFeature1.15|3}} This CA was removed and replaced by the &amp;quot;retreat injured&amp;quot; CA described above&lt;br /&gt;
&lt;br /&gt;
*'''Move to targets''' CA (score 20,000; C++ CA): Evaluate potential [[RCA_AI#Use_of_Goals_and_Targets_in_the_RCA_AI|targets]] and move units toward them.  The evaluation takes into account how valuable the targets are (this is configurable by setting [[AiWML#AI_Targets_and_Goals|goals]]), how easily the AI's units can get there, how exposed to attacks they will be, etc.  Targets are enemy leaders, enemy units that pose a threat to the AI leader and villages, as well as anything defined in a [[AiWML#The_.5Bgoal.5D_Tag|[goal] tag]].  It is important to understand that targets only apply to the ''move-to-targets'' CA.  If a target is within range of the AI's unit(s) on the current turn, other CAs, such as ''combat'', ''healing'' or ''villages'' have a higher evaluation score and are (usually) executed first.  Thus, a target is specifically &amp;lt;u&amp;gt;not&amp;lt;/u&amp;gt; an attack target, but a move target.&lt;br /&gt;
&lt;br /&gt;
*'''Leader shares keep''' CA (score 10,000; C++ CA): Move the leader off the keep to let an allied leader recruit, if an allied leader can get to the keep in one move.  Note: This CA used to be called ''passive_leader_shares_keep''.  That name still works for the time being but will be deprecated at some point.&lt;br /&gt;
&lt;br /&gt;
*{{DevFeature1.15|3}} '''Move to any enemy''' CA (score 1,000; Lua CA): Move AI units toward any enemy. That is a fall-back CA in case none of the other CAs moved the unit. This is generally only the case on maps without enemy leaders and villages.&lt;br /&gt;
&lt;br /&gt;
As already mentioned, the CAs are always evaluated (and executed, if a possible move is found) in the order given above.  For example, the ''goto'' CA is always the first move that is done (if ''goto_x'' and ''goto_y'' are defined and a move toward that location is possible).  Equivalently, if a valid attack is found in the ''combat'' CA, it is &amp;lt;u&amp;gt;always&amp;lt;/u&amp;gt; executed before any healing, village grabbing or move-to-target moves.  One of the effects this has is that attacks always have priority over any goals/targets of the move-to-target phase.  It is '''very important''' to understand this, as it is a frequent source of confusion for scenario creators.&lt;br /&gt;
&lt;br /&gt;
Note that nothing in Wesnoth's [[Wesnoth_AI_Framework|general AI mechanism]] limits CAs to having fixed evaluation scores.  For example, some of the [[Micro AIs]] as well as some of the new Lua AI CAs return variable scores.&lt;br /&gt;
&lt;br /&gt;
=== Use of Aspects in the RCA AI ===&lt;br /&gt;
&lt;br /&gt;
An aspect is a parameter that the AI framework provides to scenario creators for configuring the AI behavior.  For example, one can change how aggressive/careful the AI acts, how recruiting is prioritized or whether the leader participates in attacks.&lt;br /&gt;
&lt;br /&gt;
The RCA AI has a large number of aspects.  Their description is therefore given [[AiWML|its own page]].&lt;br /&gt;
&lt;br /&gt;
=== Use of Goals and Targets in the RCA AI ===&lt;br /&gt;
&lt;br /&gt;
''Goals'' define AI ''targets'' which, in turn, are used in the ''move-to-targets'' candidate action 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. A score is then assigned to each target based on this base value, as well as on the movement cost required to get there, whether moving there would put the units involved in danger, etc.&lt;br /&gt;
&lt;br /&gt;
It is possible to define additional targets or influence the relative ratings of the default targets. This is done with the [[AiWML#The_.5Bgoal.5D_Tag|[goal] tag]], in which we can set criteria selecting targets and define their base values.  These are then evaluated in the same way as default targets. Values set with the [goal] tag should always be relative to each other and to the values of the default targets (see [[RCA_AI#RCA_AI_Aspect_and_Goal_Configuration|below]] for the latter). The 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'.&lt;br /&gt;
&lt;br /&gt;
We stress again that these targets apply to the ''move-to-targets'' CA only and have no influence on other CAs. This is significant since CAs that deal with, for example, combat or village-grabbing have a higher score than the move-to-targets CA and are therefore always executed first. In practice that means that targets set with the [goal] tag only affect the AI behavior for targets that it cannot reach during the current turn, and only after combat, village-grabbing etc. are finished.&lt;br /&gt;
&lt;br /&gt;
==RCA AI Configuration==&lt;br /&gt;
&lt;br /&gt;
This section describes the configuration of the RCA AI.  An understanding of this setup is essential for [[Modifying AI Components]] or [[Creating Custom AIs]].&lt;br /&gt;
&lt;br /&gt;
In the following, we assume that the reader is familiar with the general principles of the [[Wesnoth AI Framework]].  The keys and tags used in the config snippets shown below are explained there.&lt;br /&gt;
&lt;br /&gt;
=== RCA AI Stage Configuration ===&lt;br /&gt;
&lt;br /&gt;
The RCA AI uses only the ''main_loop'' stage.  It is defined in file [https://github.com/wesnoth/wesnoth/blob/master/data/ai/ais/ai_default_rca.cfg ai/ais/ai_default_rca.cfg] in the Wesnoth data directory.  {{DevFeature1.15|3}} Note that the example below is from a previous Wesnoth version as new candidate actions have since been added to the default AI. The linked file always contains the most up-to-date configuration though and can be used as reference.&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    id=ai_default_rca&lt;br /&gt;
    description=_&amp;quot;Multiplayer_AI^Default AI (RCA)&amp;quot; # wmllint: no spellcheck&lt;br /&gt;
    [stage]&lt;br /&gt;
        id=main_loop&lt;br /&gt;
        name=ai_default_rca::candidate_action_evaluation_loop&lt;br /&gt;
        {AI_CA_GOTO}                         # id=&amp;quot;goto&amp;quot;&lt;br /&gt;
        {AI_CA_RECRUITMENT}                  # id=&amp;quot;recruitment&amp;quot;&lt;br /&gt;
        {AI_CA_MOVE_LEADER_TO_GOALS}         # id=&amp;quot;move_leader_to_goals&amp;quot;&lt;br /&gt;
        {AI_CA_MOVE_LEADER_TO_KEEP}          # id=&amp;quot;move_leader_to_keep&amp;quot;&lt;br /&gt;
        {AI_CA_HIGH_XP_ATTACK}               # id=&amp;quot;high_xp_attack&amp;quot; (Wesnoth 1.13.6 and later only)&lt;br /&gt;
        {AI_CA_COMBAT}                       # id=&amp;quot;combat&amp;quot;&lt;br /&gt;
        {AI_CA_HEALING}                      # id=&amp;quot;healing&amp;quot;&lt;br /&gt;
        {AI_CA_VILLAGES}                     # id=&amp;quot;villages&amp;quot;&lt;br /&gt;
        {AI_CA_RETREAT}                      # id=&amp;quot;retreat&amp;quot;&lt;br /&gt;
        {AI_CA_MOVE_TO_TARGETS}              # id=&amp;quot;move_to_targets&amp;quot;&lt;br /&gt;
        {AI_CA_PASSIVE_LEADER_SHARES_KEEP}   # id=&amp;quot;passive_leader_shares_keep&amp;quot;&lt;br /&gt;
     [/stage]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Note''': Before the RCA AI became the default, it was necessary to include this file (or one of its aliases) in the side definition.  That is not required any more – it is automatically included for all computer controlled sides, except when a stage is defined explicitly using the [stage] tag in the side definition.&lt;br /&gt;
&lt;br /&gt;
=== RCA AI Candidate Action Configuration ===&lt;br /&gt;
&lt;br /&gt;
In the configuration of the ''main_loop'' stage shown above, each candidate action is included as a macro.  Expanding the macros results in the following syntax (''combat'' CA example):&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[candidate_action]&lt;br /&gt;
    id=combat&lt;br /&gt;
    engine=cpp&lt;br /&gt;
    name=ai_default_rca::combat_phase&lt;br /&gt;
    max_score=100000&lt;br /&gt;
    score=100000&lt;br /&gt;
[/candidate_action]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Available Candidate Actions ====&lt;br /&gt;
&lt;br /&gt;
See file [https://github.com/wesnoth/wesnoth/blob/master/data/core/macros/ai_candidate_actions.cfg data/core/macros/ai_candidate_actions.cfg] for the definitions of all the macros, as well as for the ids, names and default scores of the available CAs.&lt;br /&gt;
&lt;br /&gt;
=== RCA AI Aspect and Goal Configuration ===&lt;br /&gt;
&lt;br /&gt;
When a game is created, '''aspects''' are initialized with the default values given at [[AiWML#List_of_AI_Aspects|AiWML]].  In the [https://github.com/wesnoth/wesnoth/blob/master/data/ai/utils/default_config.cfg configuration file], they are in ''composite'' aspect format such as&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[aspect]&lt;br /&gt;
    id=aggression&lt;br /&gt;
    engine=cpp&lt;br /&gt;
    name=composite_aspect&lt;br /&gt;
    [default]&lt;br /&gt;
        engine=cpp&lt;br /&gt;
        name=standard_aspect&lt;br /&gt;
        value=0.4&lt;br /&gt;
    [/default]&lt;br /&gt;
[/aspect]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
for ''aggression'', or&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[aspect]&lt;br /&gt;
    id=avoid&lt;br /&gt;
    engine=cpp&lt;br /&gt;
    name=composite_aspect&lt;br /&gt;
    [default]&lt;br /&gt;
        engine=cpp&lt;br /&gt;
        name=standard_aspect&lt;br /&gt;
        [value]&lt;br /&gt;
            [not]&lt;br /&gt;
            [/not]&lt;br /&gt;
        [/value]&lt;br /&gt;
    [/default]&lt;br /&gt;
[/aspect]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
for ''[avoid]''.&lt;br /&gt;
&lt;br /&gt;
The default '''goals''' are not initialized in configuration files.  Instead, they are defined in the C++ code of the ''move-to-targets'' candidate action as follows:&lt;br /&gt;
* Enemy leaders: Value is read from aspect ''leader_value'', which defaults to 3.&lt;br /&gt;
* Villages: Value is read from aspect ''village_value'', which defaults to 1.&lt;br /&gt;
* Any enemy unit that can attack the AI's leader on the enemy's next turn: The value of these targets is proportional to the threat they pose to the AI leader and generally higher than those of the other two types of default targets.&lt;br /&gt;
&lt;br /&gt;
The first two types of goals can be disabled by setting aspects ''village_value'' or ''leader_value'' to zero.  By contrast, close enemies cannot be removed from the list of targets, other than by removing the ''move-to-targets'' CA altogether.  One can, however, effectively disable them by setting other goals with vastly higher values (such as 100,000).&lt;br /&gt;
&lt;br /&gt;
==== Available Aspects and Goals ====&lt;br /&gt;
&lt;br /&gt;
See file [https://github.com/wesnoth/wesnoth/blob/master/data/ai/utils/default_config.cfg data/ai/utils/default_config.cfg] for the definitions of all the aspects and their default values.  The available goals are shown [[AiWML#AI_Targets_and_Goals|here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=65313</id>
		<title>Micro AIs</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=65313"/>
		<updated>2020-01-22T14:48:02Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Animals AI: Forest Animals (ai_type=forest_animals) */ add note about rabbit holes on map border&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== A Few General Words about Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
* Micro AIs allow Wesnoth campaign and scenario authors to add new AI functionality without the need for any AI programming. They bring new specialized behavior to Wesnoth that cannot be achieved, at least not easily, by adjusting default AI parameters (such as those described at [[AiWML]]) or with WML code.&lt;br /&gt;
&lt;br /&gt;
* A Micro AI is activated and configured (or deleted) via the [micro_ai] tag, requiring only a few lines of WML code.&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.6''', the Micro AI behavior was inconsistent when it came to '''hidden enemies and petrified units'''.  Some Micro AIs ignored those, while others did not.  In some case there was even a potential for AI errors when an AI unit ran into hidden or petrified enemies.  As of 1.13.6, this has all been fixed.  The Micro AIs now correctly and consistently ignore hidden units and treat petrified units as blocking the hex they are on but having no effect otherwise, in the same way as the default AI does.  '''Important notes:'''&lt;br /&gt;
** It is not possible to re-include hidden/petrified units via the Standard Unit Filters (SUFs) used by some of the Micro AIs. In fact, if this is attempted, the filters will generally not match any unit.&lt;br /&gt;
*** This only applies to SUFs which go directly into the [micro_ai] tag. Any SUF that is a sub-tag of another filter (for example, of a Standard Location Filter) is taken literally and returns all units matching the filter. If hidden or petrified units are to be excluded from such a sub-filter, that needs to be specified directly in that filter.&lt;br /&gt;
** We also note that, because all the Micro AIs are written in Lua, it is possible to [[Micro_AIs#Using_Development_.281.13.29_Version_Micro_AIs_in_your_Stable_.281.12.29_Version_Add-on|move a Micro AI's code into one's add-on]] and modify it to whatever scenario-specific behavior is desired (this does not only apply to hidden/petrified units, but to all behavior). Setting up general Micro AI behavior that works in every possible setting is not feasible, but adapting it to work (only) in the specific setting of a given scenario is often quite simple. If you do not know how to do this, please post a message on the Wesnoth forums and we will be happy to help you with it.&lt;br /&gt;
&lt;br /&gt;
* The '''[[AiWML#List_of_AI_Aspects|default AI aspects]] are, for the most part, not taken into account by the Micro AIs'''.  For example, unless noted otherwise, specifying a value for ''aggression'' does not change the MAI behavior, and locations specified in an ''[avoid]'' tag generally can be entered by units controlled by an MAI.  This is a feature of the Micro AIs, not a bug.  If you feel that a certain default AI aspect should also apply to a Micro AI, please let us know in the [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 Micro AIs Feedback thread].&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.7''', the Micro AIs did not all work reliably when the '''AI side is under shroud''' and it may have been necessary to turn shroud off for the side when using a Micro AI. {{DevFeature1.13|7}} The Micro AIs now all work for sides under shroud. They do this by disregarding shroud for terrain purposes when path finding, consistent with default Wesnoth AI behavior, while still ignoring hidden units (including those hidden under shroud) correctly.&lt;br /&gt;
&lt;br /&gt;
* Most Micro AIs in their default setting apply to all units of a side, although many of them provide parameters for filtering the units that should be affected by the new behavior.  If the Micro AI is applied only to part of the units of a side, the other units follow the default Wesnoth AI behavior.&lt;br /&gt;
&lt;br /&gt;
* Micro AIs are available in mainline since Wesnoth 1.11/1.12.&lt;br /&gt;
&lt;br /&gt;
== Micro AI Test and Demo Scenarios ==&lt;br /&gt;
&lt;br /&gt;
All Micro AIs described here can be checked out in a number of test and demo scenarios.  Test scenarios can be accessed from the command line by typing&lt;br /&gt;
 path/wesnoth-executable -t micro_ai_test&lt;br /&gt;
or (depending on your system and Wesnoth version)&lt;br /&gt;
 path/wesnoth-executable -tmicro_ai_test&lt;br /&gt;
This starts up a &amp;quot;switchboard scenario&amp;quot; in which you can select the Micro AI demonstration scenario you want to check out.  Here, ''path/wesnoth-executable'' needs to be replaced by whatever the path and filename of the Wesnoth executable is on your system.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|8}}&lt;br /&gt;
&lt;br /&gt;
You can also access the test scenarios by assigning a hotkey to &amp;quot;Start Test Scenario&amp;quot; in the preferences. Then, simply press the hotkey at the titlescreen, possibly wait out the loading screen, and select the desired scenario from the list.&lt;br /&gt;
&lt;br /&gt;
== Setting up a Micro AI ==&lt;br /&gt;
&lt;br /&gt;
Setting up a Micro AI requires only one simple step:&lt;br /&gt;
==== Activating and configuring (or deleting) a Micro AI ====&lt;br /&gt;
&lt;br /&gt;
Micro AIs are activated, deleted and configured using the [micro_ai] tag.  This tag needs to be placed in [[ActionWML]], that is, in an event, a menu option or the like.  As an example, the following code activates the healer_support Micro AI in its default configuration for Side 2 from the beginning of the scenario:&lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
 &lt;br /&gt;
         # Configure the healer support micro AI&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
'''Keys required for all Micro AIs:'''&lt;br /&gt;
All [micro_ai] tags must contain the following three required keys:&lt;br /&gt;
* '''side''': The side for which the Micro AI is to be added, changed or deleted.  This has to be an individual side number, lists of sides are not supported.&lt;br /&gt;
* '''ai_type''': The type of Micro AI to be added, changed or deleted.  See the following sections for allowed values.&lt;br /&gt;
* '''action''' (string): The action to take concerning the Micro AI.  The following values are allowed: &lt;br /&gt;
** ''add'': Add a Micro AI to a side.&lt;br /&gt;
** ''change'': Change the configuration of an existing Micro AI.  Note that this does not only change the specific parameters provided in the tag, but it replaces the entire existing Micro AI by a new version with the new (and only the new) configuration.  It is therefore equivalent to using first the ''delete'' and then the ''add'' action.&lt;br /&gt;
** ''delete'': Delete an existing Micro AI from a side.&lt;br /&gt;
&lt;br /&gt;
'''Keys required under certain circumstances:'''&lt;br /&gt;
* '''ca_id''': (string) If several MAIs of the same type are used for the same side and one or several of these are to be changed or deleted during a scenario, this key needs to be included so that the [micro_ai] tag knows which of them needs to be changed/deleted.  The same is also true if a Micro AI is changed more than once during a scenario.  We therefore '''strongly recommend always setting ''ca_id''''' for MAIs which are, or might be, changed or deleted during a scenario.&lt;br /&gt;
** '''Note:''' The id(s)/name(s) actually assigned to the CA(s) of the MAI by the engine might be different from the parameter passed here.  It is, however, sufficient to use this ca_id with the [micro_ai] tag add/delete/change actions to identify the MAI uniquely.  (In other words: you don't have to worry about this if you always use the [micro_ai] tag, but might have to figure out the actual name(s) of the CA(s) if you want to delete or change them manually.)&lt;br /&gt;
* Other keys may also be required depending on the Micro AI.  In additional, a number of optional keys may be available to configure the AI behavior.  See the sections on the individual Micro AIs for details.&lt;br /&gt;
&lt;br /&gt;
'''Required keys for deleting a Micro AI:'''&lt;br /&gt;
&lt;br /&gt;
Only a subset of the keys required for setting up a Micro AI are needed when deleting it.  '''side''', '''ai_type''' and '''action'''=delete always need to be provided.  '''ca_id''' needs to be given if it was provided when adding the Micro AI (see above for when this is needed).&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* The ''add'' and ''change'' actions ignore all keys that do not apply to the respective Micro AI type.  The ''delete'' action ignores all keys other than the required keys listed above.&lt;br /&gt;
&lt;br /&gt;
* Some of the Micro AIs use and/or modify default AI components (candidate actions or aspects) when they are added, or reset these components to their default values when they are deleted.  Thus, if you have modified the AI yourself using the default CAs or aspects, some of these modifications might be affected, or might be interfering with the Micro AI.  The sections below indicate whether a Micro AI modifies any default AI components.&lt;br /&gt;
&lt;br /&gt;
===Combining Different Micro AI Types for the Same Side===&lt;br /&gt;
&lt;br /&gt;
No extra steps need to be taken when combining different Micro AIs on the same side.  Simply set up the [micro_ai] tags for all AIs you want to use.&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* While it is ''technically'' possible to combine any number of Micro AIs, this does not mean that all Micro AIs work together in practice.  Some are not compatible because they control units in contradictory ways.  &lt;br /&gt;
* Not all combinations that ''should'' work have been tested yet.  If you find a combination that you believe should work but doesn't, [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 please let us know] and we will see if that can be fixed.&lt;br /&gt;
* The candidate action score almost certainly has to be set (using the ca_score= key) for one or several of the MAIs if several of them are used on the same side.&lt;br /&gt;
&lt;br /&gt;
== Animals Micro AIs ==&lt;br /&gt;
=== General Comments on the Animal Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Animals Micro AIs are meant to simulate the behaviors of certain types of animals.  They are, however, set up so that they can be used with arbitrary unit types and might be applicable in quite diverse situations.  The AIs nevertheless remain named after the animals for which they were written originally, so that they are (to some extent) descriptive of their behavior.&lt;br /&gt;
&lt;br /&gt;
Brief descriptions of the AI behaviors are given in the following subsections.  To get a better feeling for their behavior, check out the &amp;quot;Animals&amp;quot;, &amp;quot;Swarm&amp;quot;, &amp;quot;Wolves&amp;quot; and &amp;quot;Dragon&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  Note that some of the animal AIs are designed for animal types that do not exist in mainline.  In the test scenario, other animal types are therefore substituted for them.&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Wolves (ai_type=wolves) ===&lt;br /&gt;
&lt;br /&gt;
The Wolves Micro AI organizes all &amp;quot;wolf&amp;quot; units (predators) of the side to hunt in a single pack.  They actively chase after the closest prey and try to corner it (not always super successfully), but are easily distracted by other enemy units coming into attack range, to the point where the pack might split up.  The wolves can, however, be told to attack only prey units as well as to try to avoid other types of units (such as larger predators).  When no prey is left, the wolves wander randomly.&lt;br /&gt;
&lt;br /&gt;
This AI works best for a small number of predators (2 - 5) and sparse prey.  Also, the predators should be put on the map in a pack, that is, reasonably close to each other.  For a demonstration of the Wolves AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Note that another wolves AI exists, called [[Micro_AIs#Animals_AI:_Multi-pack_Wolves_.28ai_type.3Dwolves_multipacks.29|Multipack Wolves]], which distributes the wolves of a side into several packs, rather than all of them into one single pack as it is done here.&lt;br /&gt;
&lt;br /&gt;
Enable the Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the predator units.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the prey units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_only_prey'''=no: In the default configuration, the wolves attack all enemy units in attack range.  If this key is set to 'yes', they only attack prey units.&lt;br /&gt;
* '''avoid_type''': Comma-separated list of unit types (on enemy sides) which the wolves try to avoid.  They try to stay out of these units' way as much as possible when moving around (except when they are moving in to attack another enemy unit) and never attack them.&lt;br /&gt;
* '''ca_score'''=90000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 90,000, which is lower than the default combat CA, but higher than most other default CAs which move units.  It can be changed by setting this parameter but in most cases this will not make sense.  Note that the Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Notes on default (RCA) AI aspects:''' &lt;br /&gt;
* If ''attack_only_prey'' or ''avoid_type'' are set, the Wolves Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* Attacks by the wolves are left to the default (RCA) AI, meaning that the value of ''aggression'' has an influence on the behavior.&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI is taken into account. Note: as attacks by the wolves are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=6&lt;br /&gt;
             ai_type=wolves&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Wolf&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Vampire Bat&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             avoid_type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Multi-pack Wolves (ai_type=wolves_multipacks) ===&lt;br /&gt;
&lt;br /&gt;
The Multi-pack Wolves Micro AI is different from the [[Micro_AIs#Animals_AI:_Wolves_.28ai_type.3Dwolves.29|Wolves Micro AI]] in that there can be an arbitrary number of wolf packs, with the pack size being a free parameter.  At the beginning of the scenario, close wolves are grouped into packs in a semi-methodical way.  If the wolves are put on the map in distinct groups of close wolves of the correct number, they will be joined into packs matching these groups.  If, on the other hand, they are spread out all over the map, the method of assigning them to packs is semi-random.&lt;br /&gt;
&lt;br /&gt;
Wolves of the same pack begin by joining each other on the map.  After that, they stay together until only one wolf is left, which then tries to join up with an incomplete pack or with other single wolves.  Individual wolves entering the map during the scenario behave in that way as well.&lt;br /&gt;
&lt;br /&gt;
A second difference to the other Wolves AI is that wolves do not actively hunt here.  For the most part they just wander (often long distance).  However, the pack ferociously (and without regard for its own health) attacks any enemy units that come into range, as long as that does not mean separating the pack by more than a few hexes.  Staying together, or joining with a new wolf assigned to the pack, is the only thing that takes priority over satisfying the wolves' thirst for blood.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Multi-pack Wolves AI behavior, check out the &amp;quot;Wolves&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Multi-pack Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves_multipacks&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Multi-pack Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''pack_size'''=3: (integer) The size of the packs.&lt;br /&gt;
* '''show_pack_number'''=no: If set to &amp;quot;yes&amp;quot;, the wolves' pack numbers will be shown below them.  Note that this means that the pack number of a wolf killed in an attack will remain as a label on the map.  &lt;br /&gt;
* '''type'''=Wolf: Comma-separated list of the unit types which the AI controls. The default value is &amp;quot;Wolf&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves_multipacks [micro_ai] tag usage from the &amp;quot;Wolves&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=wolves_multipacks&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             show_pack_number=yes&lt;br /&gt;
             pack_size=4&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Big Animals (ai_type=big_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Big Animals Micro AI is a simulation of large predators that wander and hunt alone, such as Bears, Giant Spiders or Yetis.  For the most part, these just wander on the terrain that's been defined for them, but they attack enemy units if those happen to come into range.  The AI can be set up so that the Big Animals stay out of the way of other units (such as other large predators), in which case they only attack those if cornered or if they run into them accidentally (such as ambushers).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Big Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Big Animals Micro AI by using&lt;br /&gt;
 ai_type=big_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the animals to which this AI is applied.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': The big animals often go for long distance (multi-turn) journeys.  This tag is a [[StandardLocationFilter|Standard Location Filter]] describing the terrain from which the next goal is chosen.  The default is all terrain on the map. &lt;br /&gt;
* '''[filter_location_wander]''': The terrain on which the big animals will end their moves. This is a [[StandardLocationFilter|Standard Location Filter]].  The default is all terrain on the map.&lt;br /&gt;
* '''[avoid_unit]''': A [[StandardUnitFilter|Standard Unit Filter]] describing all enemy units that the big animals will avoid.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
Here's an example of big_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=big_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Ghast&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [avoid_unit]&lt;br /&gt;
                 type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
             [/avoid_unit]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=1-40,1-18&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Forest Animals (ai_type=forest_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Forest Animals Micro AI simulates a number of different animal behaviors that one might find in a forest, namely animals behaving like deer, rabbits and families of tuskers.  In the following, we will refer to those animals by these types, even though the actual unit types can be selected freely.  They behave as follows:&lt;br /&gt;
* Each deer wanders randomly on (selectable) terrain, except when enemies get in its (the deer's) maximum movement range, in which case it flees to the farthest point it can reach.&lt;br /&gt;
* Tuskers exhibit the same behavior as deer, except when an enemy is next to one of the tusklets.  This enemy is attacked unconditionally by the tuskers trying to protect their young.&lt;br /&gt;
* Tusklets blindly follow the closest adult tusker, except when there is no tusker left, in which case they behave the same as deer.&lt;br /&gt;
* Rabbits also wander randomly, but in addition disappear into their holes (if any are within reach) when enemies are close.  They reappear out of their holes at the beginning of the turn, if it is safe.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Forest Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Forest Animals Micro AI by using&lt;br /&gt;
 ai_type=forest_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Forest Animals AI consists of 4 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-3&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''rabbit_type, tusker_type, tusklet_type, deer_type''': Comma-separated lists of the unit types behaving as the respective animals.  If any of these parameters are not set, those types of animal behavior are skipped by the AI.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain on which the forest animals wander when they are not threatened by enemies.&lt;br /&gt;
* '''rabbit_number'''=6: (integer)  The number of rabbits for the AI to have on the map simultaneously.  The AI puts the missing number of rabbits onto the map at the beginning of the AI's turn (when the rabbits come out of their holes).&lt;br /&gt;
* '''rabbit_enemy_distance'''=3: (number)  Rabbits won't spawn in holes that aren't more than this distance away from all enemies.&lt;br /&gt;
* '''rabbit_hole_img''': Rabbit holes are marked by items on the map.  If this parameter is set, it must be the file name of the item image.  If it is not set, all items on the map are treated as rabbit holes.&lt;br /&gt;
** {{DevFeature1.14|10}} Items used for rabbit holes on the map border used to produce an error. Placing them on the map border therefore needed to be avoided on the scenario setup side. They are now automatically excluded from consideration by the AI code.&lt;br /&gt;
&lt;br /&gt;
Here's an example of forest_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=forest_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             deer_type=Vampire Bat&lt;br /&gt;
             rabbit_type=Giant Rat&lt;br /&gt;
             tusker_type=Ogre&lt;br /&gt;
             tusklet_type=Young Ogre&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=*^F*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Herd and Herders (ai_type=herding) ===&lt;br /&gt;
&lt;br /&gt;
The Herding Micro AI sets up an area on the map where a number of herder units (such as sheep dog) control and protect a herd (such as sheep).  For convenience, we simply refer to them as sheep dogs and sheep in the following, even though arbitrary unit types can be selected for either.&lt;br /&gt;
&lt;br /&gt;
Sheep dogs try to keep their sheep safe.  This involves keeping them inside the herding area, positioning themselves in between the sheep and approaching enemies, and attacking the enemies if those get too close.  If no active herding or protecting move is needed, the dogs go to a random location on the perimeter of the herding area.&lt;br /&gt;
&lt;br /&gt;
Sheep wander aimlessly except when a sheep dog is next to them, in which case they run away from the dog.  The dogs exploit this by positioning themselves on the outside of the sheep, if possible.  Sheep also run away from approaching enemies.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Herding AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Herding Micro AI by using&lt;br /&gt;
 ai_type=herding&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herder units (e.g. dogs).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herd units (e.g. sheep).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain forming the perimeter of the herding area.  In other words, this is the path the herders (dogs) use to patrol around the outside of the herding area.  This must be a set of coordinates ''completely enclosing'' the herding area.  Here's an example of setting up a rectangular perimeter:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         x=10-20,10-20,10,  ,20&lt;br /&gt;
         y=11,   21,   11-21,11-21&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
Another possibility is by defining the path by certain terrain on the map (as it is done in the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]), in which case the filter can simply look like this:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         terrain=Rb&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
* '''herd_x,herd_y''': The coordinate of one hex (''any'' one hex) inside the herding area.  This is needed to define which part of the map is on the ''inside'' of the herding area perimeter defined by [herding_perimeter], and which is the ''outside''. {{DevFeature1.15|0}} '''herd_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''attention_distance'''=8: (integer)  If an enemy is within this distance of a herd unit, but not as close as ''attack_distance'', the herders try to position themselves in between the enemy and the herd unit.&lt;br /&gt;
* '''attack_distance'''=4: (integer)  If an enemy is within this distance of a herd animal, the herders attack this enemy.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Herding AI consists of 6 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-5&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Herding [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=herding&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Footpad&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Troll Whelp&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             herd_x,herd_y=32,28&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=Rb&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Hunter (ai_type=hunter) ===&lt;br /&gt;
&lt;br /&gt;
The Hunter Micro AI leads a unit (or units) through hunt-and-rest cycles.  The units wander in a prescribed area of the map, their hunting ground, until an enemy unit comes within range.  Note that the units do not actively chase down enemies, but wander randomly through the hunting ground until an enemy is encountered.&lt;br /&gt;
&lt;br /&gt;
If enemies are within range of a hunter unit, the hunter attacks the weakest of them.  If a kill is made, it then retreats to its rest location, where it stays for a certain number of turns and until fully healed (whatever happens later).&lt;br /&gt;
&lt;br /&gt;
A few more details:&lt;br /&gt;
* If the hunter's way home is entirely blocked on the return (so that there is no possible path), the normal RCA AI takes over its behavior.&lt;br /&gt;
* However, if the way is blocked by an enemy unit occupying the rest hex, that enemy will be attacked unconditionally.&lt;br /&gt;
* A kill only makes the hunter go home when it is the attacker, not as defender.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Hunter AI behavior, check out the &amp;quot;Dragon&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]. &lt;br /&gt;
&lt;br /&gt;
Enable the Hunter Micro AI by using&lt;br /&gt;
 ai_type=hunter&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''home_x,home_y''': The coordinates of the hex to which the hunter returns and rests at after a kill. {{DevFeature1.15|0}} '''home_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the unit's hunting ground.  If this tag is not provided, the hunter uses all of the map.&lt;br /&gt;
* '''rest_turns'''=3: (integer)  The number of turns the AI stays at home_x,home_y after a kill.  It will not leave its home until this number of turns have passed ''and'' it is fully healed.&lt;br /&gt;
* '''show_messages'''=no: (boolean) If set to yes, the hunter announces whenever its behavior changes to the next phase.  Note that this is meant as debug output for the scenario author.  The messages are not translated and are not included in replays (and are only visible to the host in an MP game).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a hunter [micro_ai] tag usage from the &amp;quot;Dragon&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hunter&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Rowck&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=5-30,1-15&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             home_x,home_y=3,15&lt;br /&gt;
             rest_turns=2&lt;br /&gt;
 &lt;br /&gt;
             show_messages=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Swarm (ai_type=swarm) ===&lt;br /&gt;
&lt;br /&gt;
The Swarm Micro AI uses some very simply algorithms to simulate animal swarm behavior.  Without adjacent enemies, they simply do a random move, trying to stay together and at a certain distance from enemies.  However, if an enemy unit is close to any bat, the swarm scatters.  This is particular fun to watch when one places an enemy unit in the middle of the swarm.  After being scattered, the swarm members slowly rejoin, but not in a very organized way.  Sub-swarms or individual bats might roam around for quite some time before they find their way back.  It is also possible that individual bats (or small groups) split off from the larger swarm at times.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Swarm AI behavior, check out the &amp;quot;Swarm&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Swarm Micro AI by using&lt;br /&gt;
 ai_type=swarm&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Swarm AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_distance'''=5: (int) The minimum distance kept between units of the swarm and enemies when the swarm moves as a whole.&lt;br /&gt;
* '''scatter_distance'''=3: (int) Enemies within &amp;quot;scatter_distance&amp;quot; hexes of any swarm unit cause the swarm to scatter, by each unit trying to maximize its individual distance from the enemy.&lt;br /&gt;
* '''vision_distance'''=12: (int) Only units within this distance follow the overall swarm motion (either away from an enemy or of the swarm as a whole).  The smaller this value is set, the less likely the swarm is to stay together or rejoin.  This parameter is meant to simulate how far an individual member of the swarm can see, meaning that the swarm is &amp;quot;out of sight&amp;quot; (and the unit will not be able to follow it) if it is more than this distance away.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Swarm [micro_ai] tag usage from the &amp;quot;Swarm&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=swarm&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Assassin Squad Micro AI (ai_type=assassin) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|6}}&lt;br /&gt;
&lt;br /&gt;
This Micro AI controls one or several assassin units which try to circle around enemy units with the goal of hitting a target unit without being distracted or impeded by other enemies.  The units will always attack the target if such attacks are possible, otherwise they will follow the path of least resistance toward it. Notes:&lt;br /&gt;
&lt;br /&gt;
* The main priority for the AI is to find paths that avoid enemy units. However, if no such path can be found, the assassins will still advance toward the target, taking the path exposing the units to the least potential damage from enemies.&lt;br /&gt;
&lt;br /&gt;
* The units will not necessarily follow the same path. The best path is chosen individually for each unit.&lt;br /&gt;
&lt;br /&gt;
* While it is not a requirement for the AI to work, using ambusher or skirmisher units, or units with the quick trait, makes this AI more lethal in many cases.&lt;br /&gt;
&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded, it does not work otherwise. That means that '''ca_score''' is not a valid key for the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=assassin&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Assassin Squad specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the assassin units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the target unit.&lt;br /&gt;
** It is important to realize that the AI currently only works correctly with a single target unit. It is thus the responsibility of the scenario designer to make sure that this filter matches an individual enemy unit. If several enemy units are to be targeted in the same scenario, several instances of the Assassin Micro AI can be used.&lt;br /&gt;
** The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[prefer]''': A [[StandardLocationFilter|Standard Location Filter]] selecting locations on the map for the assassins to prefer when making their way toward the target unit. This could be used, for example, to make units with the ambush ability move (mostly) through forest or to have the assassins circle around the, say, northern part of the map rather than the south if that is desirable for scenario design purposes.&lt;br /&gt;
** As with avoiding enemies, the units will still move across non-preferred terrain if that is either the only option or ''much'' shorter than preferred terrain&lt;br /&gt;
** Attacking, once the target unit is in range, is not influenced by the [prefer] SLF.&lt;br /&gt;
&lt;br /&gt;
Here is an example of an Assassin Squad [micro_ai] tag usage:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=assassin&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Assassin,Master at Arms,Duelist&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             [prefer] # Circle around northern part of map&lt;br /&gt;
                 x=1-9, 10-99&lt;br /&gt;
                 y=1-37, 1-17&lt;br /&gt;
             [/prefer]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Bottleneck Defense Micro AI (ai_type=bottleneck_defense) ==&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense Micro AI lets you define a location on the map where the AI can take a defensive stance behind a narrow passage (bottleneck).  Units on the front line are backed up by healers and/or units with the leadership ability.  Injured units are moved to the back and replaced by uninjured (or less injured) units.  The units on the front line only attack when it is safe (no retaliation) or when there is a high chance that they will make a kill or level up.  Using this Micro AI only makes sense if there is no way for the enemy sides to move around the bottleneck and attack the AI's units from behind.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Bottleneck&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Bottleneck Defense Micro AI by using&lt;br /&gt;
 ai_type=bottleneck_defense&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Bottleneck Defense specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense AI requires two sets of coordinates that define where it should be taking up its defensive stance, and from which side the enemy attacks at that location.&lt;br /&gt;
&lt;br /&gt;
* '''x,y''': Comma separated lists of the hexes on the front line, where strong units are placed.  All hexes on which the AI makes contact with (can be attacked by) the enemy need to be included here.  Units are placed on them in the order in which they are listed here. {{DevFeature1.15|0}} '''location_id''' can be used instead with named/special locations.&lt;br /&gt;
* '''enemy_x,enemy_y''': Comma separated list of the hexes from which the enemy can attack the AI front line units.  This is needed for the AI to know on which side of the front line support units (healers etc.) need to be placed.  In many cases, it is sufficient to provide only one of the enemy front line hexes, but there are geometries for which that does not work.  It is therefore safer to list all enemy front line hexes here. {{DevFeature1.15|0}} '''enemy_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
By default, the AI places healers and units with the leadership ability both on the front line itself (if they are the strongest units available) and on hexes adjacent to and behind the line.  If different placement is desired, these locations can be overridden with the following keys.&lt;br /&gt;
&lt;br /&gt;
* '''active_side_leader'''=no: (boolean)  If set to 'yes', the side leader participates in the bottleneck defense action after the side's gold has been spent.  If set to 'no' (default), the leader follows default AI behavior (sitting on the keep and recruiting, for the most part).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Bottleneck Defense AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.15|3}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the units.  Only AI units matching this filter participate in the bottleneck defense.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''healer_x,healer_y''': Comma separated list of hexes where healers are placed.  This can be used to keep healers from the front line (or to take up only certain positions on the front line), and/or to override the default healing positions behind the line.  Healers are placed on them in the order in which the hexes are listed, with the exception that hexes on the line always take priority over hexes behind the line. {{DevFeature1.15|0}} '''healer_loc''' can be used instead with named/special locations.&lt;br /&gt;
* '''leadership_x,leadership_y''': Same as ''healer_x,healer_y'', but for units with the leadership ability. {{DevFeature1.15|0}} '''leadership_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
As an example, in the &amp;quot;Bottleneck&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]], there are three front line hexes, two of which are on hill terrain, while the third is a road with a lower defense rating.  The healer and side leader are supposed to participate in combat because they are strong units, but only from the hill hexes to keep them safer.  This is accomplished by using the following keys settings (check out the scenario to see which hex is which):&lt;br /&gt;
 [micro_ai]&lt;br /&gt;
     side=1&lt;br /&gt;
     ai_type=bottleneck_defense&lt;br /&gt;
     action=add&lt;br /&gt;
 &lt;br /&gt;
     x=14,14,14&lt;br /&gt;
     y= 7, 9, 8&lt;br /&gt;
     enemy_x=13,13&lt;br /&gt;
     enemy_y= 8, 9&lt;br /&gt;
 &lt;br /&gt;
     healer_x=14,14,15,15&lt;br /&gt;
     healer_y= 7, 9, 8, 9&lt;br /&gt;
     leadership_x=14,14,15,15&lt;br /&gt;
     leadership_y= 7, 9, 9 ,8&lt;br /&gt;
     active_side_leader=yes&lt;br /&gt;
 [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Fast Micro AI (ai_type=fast_ai) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|0}}&lt;br /&gt;
&lt;br /&gt;
This AI is meant as a fall-back AI for scenarios where there are so many units that attacks and moves done by the default AI are slow with long delays before each move.  It replaces the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions (CAs).  It should be obvious that in order to achieve the faster speed, compromises concerning the quality of the moves have to be made.&lt;br /&gt;
&lt;br /&gt;
Enable the Fast Micro AI by using&lt;br /&gt;
 ai_type=fast_ai&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Combat candidate action:'''&lt;br /&gt;
* Attackers are considered individually one at a time.  The best attack found for a unit is executed without checking whether a better attack by another unit might be possible.&lt;br /&gt;
* The default order in which attackers are considered is by decreasing hitpoints, starting with the strongest unit (in terms of HP).  This can be modified with the optional keys below.&lt;br /&gt;
* By default, if weaker units block attack hexes for a stronger unit, they do not move out of the way (this is the same as for the default AI).  If a hex becomes available after an attack (e.g. due to a weaker unit dying in the attack), all attacks that were possible ''before'' this happened are finished first.  Only after that are attacks by the stronger unit reconsidered.&lt;br /&gt;
* If include_occupied_attack_hexes=yes is set, weaker units move out of the way for stronger units if possible.  This can, however slow down the AI depending on the situation on the map.  Use with caution.&lt;br /&gt;
* {{DevFeature1.13|2}} Previous to this version, attacks by the leader were bunched in with the attacks by the other units.  Now leader attacks are treated separately and contain an extra layer of analysis to determine whether the attack position is &amp;quot;safe&amp;quot; (in an approximate way).  Currently, this is done with two simple considerations:&lt;br /&gt;
** Count all enemy units that can reach the leader's attack position.  If there are more than given by optional parameter 'leader_attack_max_units' (default value 3), do not attack.&lt;br /&gt;
** Add up the hitpoints of all enemies that can reach the leader's attack position.  If this value, multiplied by optional parameter 'leader_weight'(default value 2), is larger than the leaders hitpoints, do not attack.&lt;br /&gt;
** Two optional parameters, 'threatened_leader_fights' and 'leader_additional_threat', can also be used to let the leader attack when the threat at the attack hex is comparable to that at the leader's current position.&lt;br /&gt;
** In order to keep this AI as fast as possible, leader attacks are done by a separate candidate action that is only evaluated when all other CAs are done.&lt;br /&gt;
&lt;br /&gt;
'''Move-to-targets, villages and retreat candidate actions:'''&lt;br /&gt;
* The move-to-targets, villages and retreat candidate actions of the default AI are removed.  Move-to-targets and villages are taken over by the Fast Micro AI, whereas retreat is simply not done.&lt;br /&gt;
* This CA moves units toward goals after attacks are done.  Goals are unowned (if the AI side has a leader only) and enemy-owned villages and enemy leaders.&lt;br /&gt;
* For each goal, units are pre-sorted in order of the fewest moves needed to cover the ''distance'' toward the goal (that is, as if move cost on each hex toward the goal were 1).  Units are then considered sequentially and if a unit can get there &amp;quot;efficiently&amp;quot;, its move is executed without considering other units.&lt;br /&gt;
** A move is considered efficient if the move cost to get to the hex is not more than 'move_cost_factor' larger than the distance to the hex.  'move_cost_factor' is an optional key that defaults to 2.  It can be set to values of 1.1 or larger (see below).&lt;br /&gt;
** If the move for the current unit is not found to be efficient, the AI considers the next unit instead.  If no unit can do an efficient move toward the goal, the best (fastest) of the inefficient moves is selected instead.&lt;br /&gt;
&lt;br /&gt;
* Village goals:&lt;br /&gt;
** Village goals are considered before enemy leader goals so that the AI preferentially chooses scouts (or otherwise fast units) for them.&lt;br /&gt;
** The number of units sent toward villages depends on the value of the default AI aspect village_value.  It is set to village_value/4 times the number of units the AI has left to move.  Thus, by default (village_value=1.0) the AI sends a quarter of its units toward villages, and the rest toward enemy leaders.  This means that the meaningful range of values for village_value is 0..4.&lt;br /&gt;
** Only one unit is moved toward each village.  This consideration also overrides the number of units to move toward villages defined by village_value, topping it at the number of unowned/enemy-owned villages on the map.&lt;br /&gt;
** Setting village_value=0 disables moving units toward villages altogether.  That is, not only are no units chosen, but the village targeting phase of the AI is entirely disabled with no evaluation time used for it.  Note that, unlike with the default AI, this also means that no villages within range of an AI unit are grabbed.&lt;br /&gt;
** The order in which villages are chosen as goals is some combination of them being close to the AI side's leader and being spread out over the map.&lt;br /&gt;
&lt;br /&gt;
* Enemy leader goals:&lt;br /&gt;
** The order of enemy leaders goals is set according to their distance from the AI leader with the closest enemy leader being targeted first (if the AI has a leader, otherwise their order is random).  One unit is sent toward each enemy leader in this order, after which the AI starts with the first enemy leader again.  This is repeated until all units have moved.&lt;br /&gt;
&lt;br /&gt;
* Guardians (units with ai_special=guardian set) are ignored by the fast move-to-targets CA.&lt;br /&gt;
&lt;br /&gt;
'''Tips for speeding up/modifying the Fast Micro AI in specific scenarios:'''&lt;br /&gt;
&lt;br /&gt;
A number of optional keys and default AI aspects can be used to modify the behavior of the Fast Micro AI.  The MAI is set up so that it is a reasonable compromise between speed and quality of the moves in most situations.  However, this might not be ideal for all scenarios here are some things that can be done to speed up the AI (or to get better behavior in some situations by doing the opposite):&lt;br /&gt;
* Use large values for the default AI aspect aggression.  The largest delays before attacks happen (in some circumstances) when the Fast MAI has to consider lots of units with attacks left but decides not to attack with them.  See below for a description of how aggression is used by the fast AI.&lt;br /&gt;
* Use large values of move_cost_factor (see above for description of what this does).  If you want to emphasize units that can move across terrain easily (e.g. bats) to be chosen for village grabbing, you might want to use low values of move_cost_factor, but this might slow down the AI.  The default value should work reasonably well in most standard situations, but modifying the value might work better on specific maps.&lt;br /&gt;
* Don't use dungeon_mode=yes unless absolutely necessary, even in most dungeon crawls.  Test the AI without this setting first and only use it if it is the only way to solve the problem.  Often, a minor tweak to the map will have the same effect, without the speed penalty.&lt;br /&gt;
* Use include_occupied_attack_hexes=yes with caution.  It might (or might not) slow down the attack CA significantly, depending on the scenario.&lt;br /&gt;
* Set village_value=0, which makes the AI not consider villages as goals.  This has not been found to make a large difference on the maps tested so far, but it might have an effect on maps with a very large number of villages.&lt;br /&gt;
* Use attack_hidden_enemies=yes on maps where there are no hidden enemies. Excluding hidden enemies from the attack calculation requires an additional filter to be evaluated that takes extra time. In most cases this will not result in a large computation time saving.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_hidden_enemies'''=no: (boolean)  By default, hidden enemies are excluded from the attack calculation.  Setting this to 'yes' makes the AI also consider those enemies as attack targets.&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.13|2}} A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  As with the default AI, units will not step on hexes defined by the [avoid] tag.  In addition, no goals will be chosen that lie on those hexes.  The one minor exception is that a unit stepping out of the way for another unit might temporarily enter the avoided zone.&lt;br /&gt;
* '''dungeon_mode'''=no: (boolean)  As much as possible, the Fast Micro AI works with distances between hexes rather than path finding.  This works pretty well on open maps and even most dungeon crawls (and similar scenarios), but it might result in some units getting temporarily (and in rare cases permanently) stuck on maps with convoluted labyrinths.  If that is the case, setting dungeon_mode=yes triggers more path finding that avoids such situations (but as a result is somewhat slower).&lt;br /&gt;
* '''include_occupied_attack_hexes'''=no: (boolean) If set to 'yes', the AI moves weaker units out of the way for a stronger attacker.  This might lead to better attacks but is slower, so don't use if speed is essential.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the attackers.  Only AI units matching this filter will attack, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes the side= key set to the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the enemies.  Only enemy units matching this filter will be attacked, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''leader_weight'''=2: (number &amp;gt;= 0) {{DevFeature1.13|2}} A factor specifying how much more valuable the AI considers side leaders than non-leader units in the attack evaluation. This is also used to determine whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''leader_additional_threat'''=1: (number &amp;gt; 0) {{DevFeature1.13|2}} If threatened_leader_fights=yes, the threat at the attack position may be larger by this factor for the leader to leave the keep for an attack.&lt;br /&gt;
* '''leader_attack_max_units'''=3: (number &amp;gt;= 0) {{DevFeature1.13|2}} A number specifying how many enemy units within reach are acceptable when determining whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''move_cost_factor'''=2: (number &amp;gt;= 1.1)  The factor by which the move cost to a goal hex may be larger than the distance to the goal hex for a move to be considered &amp;quot;efficient&amp;quot;.  See description above.  1.1 is used if this is set to values smaller than 1.1.  Smaller values might lead to better behavior, while larger values might speed up the AI.&lt;br /&gt;
* '''weak_units_first'''=no: (boolean) If set to 'yes', inverts the order in which units are considered by the combat CA.  Attacks are then done in order of increasing hitpoints of the attackers.&lt;br /&gt;
* '''skip_combat_ca'''=no: (boolean) If set to 'yes', the fast combat CA is not used.  The default AI's high_xp_attack and combat CAs remains in place in this case.&lt;br /&gt;
* '''skip_move_ca'''=no: (boolean) If set to 'yes', the fast move-to-targets CA is not used.  The default AI's retreat, villages and move-to-targets CAs remains in place in this case.&lt;br /&gt;
* '''threatened_leader_fights'''=no: (boolean) {{DevFeature1.13|2}} By default, the AI leader will not attack if the threat at the attack position is too high.  If this key is set to 'yes', the leader will attack if the threat there is not larger than the threat at his current position multiplied by the value of 'leader_additional_threat'. This is meant as an option for the endgame: when the AI leader is under attack, he might as well go down fighting.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''aggression''': The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to set how aggressive the AI should attack.  It works in roughly the same way as the default AI aspect:&lt;br /&gt;
** The value of (1 - aggression) determines how much the AI values its own units compared to the enemy's units.  Thus, aggression=0 means that attacks are only done if the AI expects to deal (roughly) the same amount of damage as it will receive.  aggression=1 means that expected damage to the AI's units is not considered, only the expected damage to enemy units matters.&lt;br /&gt;
** Damage is calculated as a combination of hitpoints and chance to die, in &amp;quot;gold equivalent units&amp;quot;.&lt;br /&gt;
** Unlike the default AI, aggression is set to 1 for values larger than 1.  Allowed values are thus anything from -infinity to +infinity, although only -infinity to +1.0 make sense.&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.13|2}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
*'''attacks aspect''':  {{DevFeature1.13|2}} The standard RCA AI [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] used to filter own and enemy units for attacks is now also taken into account as long as neither [filter] nor [filter_second] (see above under 'optional keys') are defined in the [micro_ai] tag.&lt;br /&gt;
*'''village_value=1''': (number ranging from 0 to 4) The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to define how much villages should be targeted by the move-to-targets CA.  In the Fast MAI, this defines which fraction of the AI units are moved toward village goals (see above for details).  If set to 0, villages are ignored as goals.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded to the same values as those of the respective [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|default candidate actions]].  ca_score is therefore not a valid optional key.&lt;br /&gt;
* The fast Micro AI modifies (removes) the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions.&lt;br /&gt;
&lt;br /&gt;
Enabling the Fast Micro AI in its default configuration is as simple as this usage example from the &amp;quot;Fast AI&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=fast_ai&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Generalized Goto Micro AI (ai_type=goto) ==&lt;br /&gt;
&lt;br /&gt;
The Goto Micro AI provides a highly configurable method of sending a unit (or units) to a location or set of locations.  The units to be moved are defined using a [[StandardUnitFilter|Standard Unit Filter]] (SUF) [alternatively, all side units are used if the SUF is not provided), while the goto locations are given in a [[StandardLocationFilter|Standard Location Filter]] (SLF).&lt;br /&gt;
&lt;br /&gt;
By default, each unit matching the SUF will move toward the closest goal (in terms of turns needed to get there) matching the SLF via the fastest route given the current situation on the map (locations of enemy units etc.), but this behavior can be influenced by the optional keys listed below.  Note that this means that a unit's goal might change from one turn to the next depending on the situation on the map.&lt;br /&gt;
&lt;br /&gt;
The behavior of the unit(s) once they reach their goal location(s) is configurable as well.  By default, they will stay there until the Goto Micro AI is deleted or changed.&lt;br /&gt;
&lt;br /&gt;
For demonstrations of several Goto Micro AI usages, check out the &amp;quot;Goto&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Goto Micro AI by using&lt;br /&gt;
 ai_type=goto&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Goto specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the possible goal locations for the move.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists). Note that the area defined by [avoid] is not entered at all, even if the end point of the move would lie outside the area again.&lt;br /&gt;
* '''avoid_enemies''': (number&amp;gt;0) By default, units controlled by the Goto Micro AI will not take enemy units into account for route finding, other than the enemies' ZoCs.  By contrast, if this key is set, they will try to avoid traversing hexes on which they can be attacked by enemies.  The larger the value of ''avoid_enemies'', the more the units will try to stay out of the enemies' way.  Notes:&lt;br /&gt;
** Units are willing to take a path that is 'avoid_enemies' movement points longer for each enemy unit that can be avoided by doing so for each hex along the path.  As an example, if avoid_enemies=1 and the shortest path crosses 2 hexes that can be reached by 3 enemies each, the unit is willing to take a path that is up to 6 hexes longer instead.  If avoid_enemies=10 in the same situation, the unit will take a path up to 60 hexes longer.&lt;br /&gt;
** Hexes adjacent to enemy units are always strongly avoided, independent of the value of avoid_enemies.&lt;br /&gt;
** avoid_enemies is ignored if 'use_straight_line=true' is set.&lt;br /&gt;
** Due to an issue with the path finding code, '''tunnels and teleports are ignored in Wesnoth 1.14 and before''' if 'avoid_enemies' is used. This has been fixed in Wesnoth 1.15.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units to be moved.  Defaults to all units of the side if not defined.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''ignore_enemy_at_goal'''=no and '''ignore_units'''=no: (boolean)  In the default configuration, the Goto MAI does not move units if the path to all goal hexes is blocked by enemies.  It is therefore possible to disable the Goto MAI by putting enemy units on all the goal hexes.  That can be changed if either of these two keys is set to 'yes'.&lt;br /&gt;
** 'ignore_enemy_at_goal=yes' makes the AI choose its goals while disregarding any enemy units at the goal hexes.  Even so, a stiff rating penalty is applied to goal hexes occupied by enemies, thus that unoccupied goal hexes are chosen unless all possible goals are occupied by enemies.  Note that this setting ''only'' ignores enemy units at the goal hexes themselves.  It is still possible to disable the AI by putting enemies all around the goals.&lt;br /&gt;
** 'ignore_units=yes' let's the AI ignore ''all'' enemy units when deciding which goal hex(es) to move toward.&lt;br /&gt;
** In both cases, enemies are only ignored for deciding which unit to move toward which goal hex.  For the actual move itself, all enemies and their ZoCs are considered as usual.&lt;br /&gt;
** Both options only take effect if neither avoid_enemies nor use_straight_line is set to 'yes', as neither of these consider enemies as blockers.&lt;br /&gt;
* '''release_unit_at_goal'''=no: (boolean) By default, a unit that reaches a goal location will stay there until the Goto Micro AI is deleted or changed (or until the conditions specified in the [goto_goals] SLF change).  If this parameter is set, a unit that has reached its goal will not be controlled by this Micro AI any more starting from the next turn.&lt;br /&gt;
* '''release_all_units_at_goal'''=no: (boolean) Same as ''release_unit_at_goal'', but this will deactivate the Goto Micro AI for all units once the first unit reaches a goal.&lt;br /&gt;
* '''unique_goals'''=no: (boolean)  If set to 'yes', each unit will be sent to a different goal location.  Note that the best goals are recalculated each turn, so a unit might choose a different goal this turn from what it was heading for during the previous turn if the situation on the map has changed.  Also note that if there are more units than goal locations, the left over units will not be handled by the Goto MAI but follow default behavior instead.&lt;br /&gt;
* '''use_straight_line'''=no: (boolean)  By default, units choose the route by which they will get to their goal locations in the smallest number of turns.  If this parameter is set to 'yes', a straight line route (or something pretty close to it at least) will be chosen instead, irrespective of whether this leads into a dead end, whether it is blocked by enemies, etc.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* Depending on the parameters used, the Goto Micro AI might require a lot of path finding to be done.  Thus, while it is possible to set up this AI to move many units to many different locations, a usage like that might require quite a long calculation time.&lt;br /&gt;
&lt;br /&gt;
===Goto Micro AI example usages:===&lt;br /&gt;
&lt;br /&gt;
Here are several examples of goto [micro_ai] tag usages from the &amp;quot;Goto&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
* Send a unit toward a castle (similar to the RCA AI's Goto Candidate Action, but avoiding dead ends and areas ZoCed by enemies)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
             ca_id=messenger1&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=messenger1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=6-7,2-4&lt;br /&gt;
                 terrain=C*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             ca_score=210001&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send all units of the side toward the southern border of the map&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=8&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                  y=33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             avoid_enemies=1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send units to the four corners of the map (with each unit going to a different corner).&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x=1,1,44,44&lt;br /&gt;
                 y=1,33,1,33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             unique_goals=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Set up a guardian unit that doesn't move while no enemy unit is within 8 hexes (ironically, the Goto Micro AI can be used to keep units in place, although using one of the Guardian Micro AIs might often be a better idea)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=guard1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 [filter]&lt;br /&gt;
                     id=guard1&lt;br /&gt;
                 [/filter]&lt;br /&gt;
                 [not]&lt;br /&gt;
                     [filter]&lt;br /&gt;
                         side=1&lt;br /&gt;
                     [/filter]&lt;br /&gt;
                     radius=8&lt;br /&gt;
                 [/not]&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Guardian Micro AIs ==&lt;br /&gt;
=== General Comments on the Guardian Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Guardian Micro AIs can be used to set up different kind of guardian behaviors for a unit or a set of units.  Four different types of guardians are available and are described below.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different guardian behaviors, check out the &amp;quot;Guardians&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
=== Return Guardian  (ai_type=return_guardian)===&lt;br /&gt;
&lt;br /&gt;
A 'return guardian' is a variation of the standard Wesnoth guardian. It has an assigned guard position (GP) to which it returns after attacks on approaching enemies:&lt;br /&gt;
* If at GP with no enemy in reach, do nothing.&lt;br /&gt;
* If at GP with an enemy in reach, attack. Note that the attack is done by the default AI, which might result in the guardian not attacking if the enemy is deemed too strong (this can be influenced by setting the default ''aggression'' value to a different value).&lt;br /&gt;
* If not at GP, return there, no matter whether an enemy is in reach or not.&lt;br /&gt;
* If enemies are blocking the way back to GP, do your best to move around them.&lt;br /&gt;
* If the guardian ends up next to an enemy on the way back, attack that enemy after the move.&lt;br /&gt;
&lt;br /&gt;
Enable the Return Guardian Micro AI by using&lt;br /&gt;
 ai_type=return_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''return_x,return_y''': The coordinate of the hex the unit returns to after each attack.  {{DevFeature1.15|0}} '''return_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=100010: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  This AI consist of 2 CAs, one with score &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt; the other with &amp;lt;tt&amp;gt;ca_score-20&amp;lt;/tt&amp;gt;.  The default value is set so that the first CA gets executed right before the default combat CA, and the other right after.  It usually will not make sense to change this value, but it is possible to do so for custom applications.  {{DevFeature1.13|6}}: This is now changed to 100100 and 99900 in order to avoid conflicts with the new high_XP_attacks candidate action.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a return guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=return_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=return1&lt;br /&gt;
             return_x,return_y=20,2&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
===Stationed Guardian (ai_type=stationed_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A 'stationed guardian' is another variation of the standard Wesnoth guardian with a somewhat more complex behavior than that of the 'return guardian'. Two positions are defined for it, a 'station' and a 'guarded location', as well as a distance. The behavior is as follows:&lt;br /&gt;
* If no enemy is within 'distance' of the guard's current position, do nothing.  This is independent of the guardian being at its station or not.&lt;br /&gt;
* Otherwise: If an enemy is within 'distance' of the guard, but not also within the same distance of the guarded location and the station (all of this simultaneously), move the guard in the direction of the station (or keep the guard in place if the station is unreachable).&lt;br /&gt;
* Otherwise:&lt;br /&gt;
** Pick the enemy unit that is closest to the guarded location.&lt;br /&gt;
** If we can reach it, pick the adjacent hex with the highest defense rating and attack from there.&lt;br /&gt;
** If not in reach, move toward this unit.&lt;br /&gt;
&lt;br /&gt;
Enable the Stationed Guardian Micro AI by using&lt;br /&gt;
 ai_type=stationed_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance parameter as explained above&lt;br /&gt;
* '''station_x,station_y''': The x and y position of the hex that serves as the guardians station. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''guard_x, guard_y''': The x and y position of the guarded location.  {{DevFeature1.13|0}}  These are now optional keys, defaulting to station_x,station_y if not provided. {{DevFeature1.15|0}} '''guard_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a stationed guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=stationed_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=stationed1&lt;br /&gt;
             distance=4&lt;br /&gt;
             station_x,station_y=2,14&lt;br /&gt;
             guard_x,guard_y=3,13&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Coward (ai_type=coward) ===&lt;br /&gt;
&lt;br /&gt;
Cowards are units that, like guardians, sit around doing nothing until an enemy comes into range.  Unlike guardians, however, they run away once enemies approach. Applications might be wild animals, unarmed civilians getting in the way of a battle, etc.  The coward macro can be called with two optional locations, 'seek' and 'avoid':&lt;br /&gt;
* If neither is given, the coward retreats to the position farthest away from the approaching enemies.&lt;br /&gt;
* If 'seek' is given, it preferentially goes toward that location (but getting away from enemies takes priority).&lt;br /&gt;
* If 'avoid' is given, it in addition tries to avoid that location (with both maximizing distance from enemies and going toward 'seek' taking priority).&lt;br /&gt;
* Both 'seek' and 'avoid' may consist of only one coordinate ('x' or 'y'), in which case not a single hex, but a line of hexes is sought or avoided.&lt;br /&gt;
&lt;br /&gt;
Enable the Coward Micro AI by using&lt;br /&gt;
 ai_type=coward&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance within which an enemy must be to &amp;quot;scare&amp;quot; the unit&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_if_trapped'''='no':  {{DevFeature1.13|0}}  With the default settings, a coward never attacks.  If this key is set to 'yes', the coward attacks the weakest (lowest hitpoints) enemy next to which it ends up after moving (such as a cornered animal would do, blindly lashing out without regards for its own safety).&lt;br /&gt;
* '''avoid_x, avoid_y''': The x and y coordinate of the hex to avoid. {{DevFeature1.15|0}} '''avoid_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units from which the cowards flee.  If not set, this defaults to all enemy units.  Note, however, that no &amp;quot;enemy of AI side&amp;quot; condition is added if [filter_second] is specified, meaning that it can include allied and/or enemy units as desired (and even units on the same side as the coward).&lt;br /&gt;
* '''seek_x, seek_y''': The x and y coordinate of the hex to seek. {{DevFeature1.15|0}} '''seek_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a coward [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=coward&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=coward3&lt;br /&gt;
             distance=5&lt;br /&gt;
             seek_x,seek_y=24,5&lt;br /&gt;
             avoid_x,avoid_y=24,15&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Zone guardian (ai_type=zone_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A zone guardian is a unit that, as the name says, guards a zone. It moves randomly inside this zone until an enemy enters it (or a separately defined enemy zone, see below). Applications might be the defense of a castle, a nesting area or for protecting an unit.  The zone macro can be called with an optional SLF, [filter_location_enemy]:&lt;br /&gt;
* If not specified, the zone guard attacks any enemy coming inside its guard zone.&lt;br /&gt;
* If [filter_location_enemy] is given, it attacks any enemy entering this zone and once there are no more enemies, it goes back to patrol in its basic zone.&lt;br /&gt;
&lt;br /&gt;
Enable the Zone Guardian Micro AI by using&lt;br /&gt;
 ai_type=zone_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone to guard.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_enemy]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone where enemies have to be attacked.  If not given, it defaults to [filter_location].&lt;br /&gt;
* '''station_x,station_y''': By default, the zone guardian moves randomly within the zone if there is no enemy to attack.  However, if station_x/y is given, it moves to this position instead during turns when it does not have an enemy to attack.  This position does not need to be inside the zone defined above.  Note: obviously it does not make sense to define [filter_location], [filter_location_enemy] and station_x/y all at the same time.  Only [filter_location] and either one of the latter two (or none of them) should be provided for any zone guardian. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a zone guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=zone_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=zone3&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=22-31,4-11  # This is intentionally chosen to extend past the lake&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             station_x,station_y=32,8&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Hang Out Micro AI (ai_type=hang_out) ==&lt;br /&gt;
&lt;br /&gt;
The Hang Out Micro AI instructs units of the AI side to &amp;quot;hang out&amp;quot; around a certain location, or locations, until a condition for mobilizing the side is met.  If none of the optional keys are given, the AI controls all units of the side to hang around the leader while moving off castle tiles to make room for recruiting.&lt;br /&gt;
&lt;br /&gt;
The AI works by moving each unit to its best hex first using the minimum amount of movement points required for this move.  Once all AI units are in position, movement points are set to zero for all of them simultaneously such that they cannot be used by other AI candidate actions.  This is done because the mobilizing condition might be met as result of one of the moves of the Hang Out AI, after which we want units to be able to continue with other AI actions.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Hang Out and Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Hang Out Micro AI by using&lt;br /&gt;
 ai_type=hang_out&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Hang Out specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  In its default configuration, the Hang Out MAI controls all units of a side, keeping them as close to the side leader(s) as possible while moving off castle tiles to make room for recruiting.  For this purpose, the candidate action score defaults to 170,000, which is just below the score of the default recruiting CA.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''': A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  If neither of these two exists, this parameter is set to default to all castle terrain (&amp;lt;tt&amp;gt;terrain=C*,C*^*,*^C*&amp;lt;/tt&amp;gt;) so that units move off the castle to make room for recruiting.  Note that this does not include keep terrain, otherwise the leader would be moved off the keep as well.&lt;br /&gt;
* '''ca_score'''=170000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 170,000, which means that it is executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Hang Out action (&amp;lt;tt&amp;gt;side=&amp;lt;/tt&amp;gt; is always set and does not need to be provided).  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the locations ''near'' which the AI units should hang out.  This does not mean that the units only move onto terrain specified here, but that they minimize the distance to such terrain.  It defaults to the position of the side leader(s).&lt;br /&gt;
* '''[mobilize_condition]''':  Contains ([[ConditionalActionsWML#Condition_Tags|WML Condition Tags]])  Once the condition set up here has been met, the Hang Out MAI is disabled and the side goes over to other AI activity.  The Hang Out MAI remains disabled after this, even if the condition is not met any more later in the scenario.&lt;br /&gt;
* '''mobilize_on_gold_less_than''': (number) Since side gold cannot be checked in a WML condition tag without storing it in a variable first, this key is provided separately.  It disables the Hang Out Micro AI once the side gold goes below the value specified here for the first time.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
* The Hang Out AI still takes effect if no locations to hang out at are found or if all map locations are to be avoided.  In that case, it simply keeps all units at their current locations.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Hang Out Micro AI configuration from the &amp;quot;Hang Out and Messenger&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hang_out&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [avoid]&lt;br /&gt;
                 terrain=C*,H*,M*,A*,S*,*^F*&lt;br /&gt;
             [/avoid]&lt;br /&gt;
             [mobilize_condition]&lt;br /&gt;
                 [have_unit]&lt;br /&gt;
                     side=1&lt;br /&gt;
                     count=7-99&lt;br /&gt;
                 [/have_unit]&lt;br /&gt;
             [/mobilize_condition]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Healer Support Micro AI (ai_type=healer_support) ==&lt;br /&gt;
&lt;br /&gt;
The Healer Support Micro AI configures the healers of a side to stay behind the battle lines and heal injured and/or threatened units rather than participate in combat under all circumstances.  You can set different levels of aggressiveness for the healers.  &lt;br /&gt;
&lt;br /&gt;
'''Notes'''&lt;br /&gt;
* By default, ''all'' healers of the AI side are controlled by this AI.  This includes in particular also the side leader, if the leader is a healer.  If you do not want the leader to be included (and possibly be moved away from the keep etc.) use the [filter] tag with canrecruit=no (see below).&lt;br /&gt;
* It is not always better to use this Micro AI than the default AI, as it takes healers (which can be strong units) away from the pool of units to choose for attacks.&lt;br /&gt;
* {{DevFeature1.15|3}} This Micro AI now takes unit guardian status and the passive_leader aspect into account.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Healers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Healer Support Micro AI by using&lt;br /&gt;
 ai_type=healer_support&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Healer Support specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the healer units.  If set, only units on the AI side passing this filter ''and'' having the healing ability are considered by this Micro AI.&lt;br /&gt;
*'''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting units to receive healing. If set, only units on the AI side passing this filter ''and'' the other healee selection criteria will be selected for healing by this Micro AI.&lt;br /&gt;
* '''aggression'''=1.0: (float) Sets the aggressiveness of the AI.  This parameter is set up as a float to accommodate future functionality, but it currently acts as a boolean: if set to zero, the AI will never let its healers participate in combat, for all other values it allows them to attack after all other units have attacked and if the healers cannot find any more units to support.  The former behavior might be appropriate in scenarios with only one or few valuable healers, while the latter might work better in scenarios with many healers.&lt;br /&gt;
* '''injured_units_only'''=no: (boolean) If set to 'yes', the AI will only move healers next to units that are already injured and skip units that currently have full hitpoints, but might get injured during the next enemy turn.&lt;br /&gt;
* '''max_threats'''=9999: (integer) The maximum allowable number of enemies that can attack a hex in order for it to be considered for a healer.  As an example, setting 'max_threats=0' means that the AI only moves healers to locations that are entirely safe from the enemy (assuming that none of the units currently on the map dies).  Note that the value of this key is checked against the number of enemies that can make it to the hex, not the number of adjacent hexes from which the healer could be attacked.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI. Note: as attacks by the healers are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The Healer Support Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded.  It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Healer Support Micro AI configuration from the &amp;quot;Healers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             aggression=0&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Lurkers Micro AI (ai_type=lurkers) ==&lt;br /&gt;
&lt;br /&gt;
The Lurker Micro AI controls some or all units of a side in &amp;quot;lurker mode&amp;quot;.  A lurker is a unit that is capable of moving across most terrains, but that only stops on and attacks from specific terrain.  It might also have the ability to hide on this terrain (which is the reason why it is called a lurker).&lt;br /&gt;
&lt;br /&gt;
Lurkers move individually without strategy and always attack the weakest enemy within their reach.  If no enemy is in reach, the lurker does a random move instead - or it just sits and waits (lurks).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of different types of lurker behavior, check out the &amp;quot;Lurkers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Lurkers Micro AI by using&lt;br /&gt;
 ai_type=lurkers&lt;br /&gt;
in the [micro_ai] tag&lt;br /&gt;
&lt;br /&gt;
===Lurkers specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
* '''[filter]''': Specifies which units of the side are controlled by the Lurker Micro AI.  This is a [[StandardUnitFilter|Standard Unit Filter]].  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': Specifies the terrain from which the lurkers attack. This is a [[StandardLocationFilter|Standard Location Filter]].&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_wander]''': Specifies the terrain on which lurkers end their moves if there is no enemy in reach. This is a [[StandardLocationFilter|Standard Location Filter]].  If [filter_location_wander] is not given, it defaults to [filter_location].&lt;br /&gt;
* '''stationary'''=no: (boolean)  If set to yes, a lurker never moves if there is no enemy within attack range.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Lurkers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=4&lt;br /&gt;
             ai_type=lurkers&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Naga Fighter&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=W*,S*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             [filter_location_wander]&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location_wander]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Messenger Escort Micro AI (ai_type=messenger_escort) ==&lt;br /&gt;
&lt;br /&gt;
The Messenger Escort Micro AI lets you define a set of waypoints through which it tries to move one or several of its units, the messenger(s).  The other units escort the messenger, trying to protect it and attacking enemies that are in its way.  Note that a messenger might not strictly hit intermediate waypoints, but that getting close is good enough, especially if a waypoint is occupied by an enemy unit.  It does, however, always try to get to the last waypoint.&lt;br /&gt;
&lt;br /&gt;
'''Notes on the use of several messengers:''' {{DevFeature1.13|0}}  &lt;br /&gt;
* This Micro AI can control several messengers at the same time, which can but do not have to move in a group.  They can be put on the map altogether in one location, or be spread out on the map or across different turns.&lt;br /&gt;
* The farther a messenger has moved through the chain of waypoints, the higher a priority it is for the AI, meaning that farther advanced (on the map) messengers are moved first, and that the escort units try to clear the way and stay with those messengers more so than with those further back on the map.  This order can be inverted using the invert_order= key.&lt;br /&gt;
* If several messengers are on the map, escort units might move from one to the other, the AI making a judgement call where they are needed most.  Closeness to a messenger is generally the most important criterion, but not the only one.&lt;br /&gt;
* If a messenger dies, the escort units previously staying with it now join one of the other messengers if there are any left (otherwise they follow default AI behavior).&lt;br /&gt;
* If you want certain escort units to stay always with the same messenger, use several messenger [micro_ai] tags with different [filter]/[filter_second] combinations.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Messenger Escort Micro AI by using&lt;br /&gt;
 ai_type=messenger_escort&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Messenger Escort specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': {{DevFeature1.13|0}} (Previous to 1.13.0, only id= was allowed.) The id of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated list of waypoint coordinates through which the messenger will go in the given order to reach its goal, given by the last coordinate.  If you want the messenger to go directly to the goal, simply enter a single x,y coordinate here.  Note that the messenger does not have to hit each waypoint exactly (except for the final one), getting close to them is good enough. {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Messenger Escort AI consists of 3 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-2&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_death_chance'''=0.67 and '''messenger_death_chance'''=0.0: (both floats; ranging from 0 to 1)    If the messenger is adjacent to an enemy at the end of the move, it will only attack the enemy if the chance of death of the enemy is &amp;gt;= enemy_death_chance and if the messenger's chance of death is &amp;lt;= messenger_death_chance (or if it is an attack at a range for which the enemy does not have a weapon).&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|0}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the escort units.  If not set, this defaults to all enemy units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''invert_order'''=no: {{DevFeature1.13|0}} If set to 'yes', the order of priority with which messenger units are considered is inverted, starting with the messenger unit that is the least advanced on the map (see description above).  In most cases, selecting this option results in worse performance of the AI, so use it with caution.  (And obviously it has no effect at all if there is only one messenger controlled by the AI.)&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Messenger Escort&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=messenger_escort&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=messenger&lt;br /&gt;
             waypoint_x=31,24,27,28&lt;br /&gt;
             waypoint_y=20,14,7,1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Patrol Micro AI (ai_type=patrol) ==&lt;br /&gt;
&lt;br /&gt;
The defines a number of waypoints for a unit (or units) to follow.  Options can be set whether the route should be followed in a loop, as an out-and back, only once, whether units encountered along the way should be attacked, etc.&lt;br /&gt;
&lt;br /&gt;
No matter what options are chosen, getting to the next waypoint always takes priority over attacking for a patroller.  The AI thus prefers to move the patrol unit around enemies rather than straight for them.  Also, if a waypoint is occupied by a unit the AI is not instructed to attack, it will (eventually) abandon that waypoint once it gets close enough and move on to the next one.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different patrol behaviors, check out the &amp;quot;Patrols&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Patrol Micro AI it by using&lt;br /&gt;
 ai_type=patrol&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Patrol specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated lists of the waypoint coordinates through which the patrol travels (in order). {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack''': By default (if this key is not set), the patrol unit attacks any unit it ends up next to after a move.  Alternatively, this parameter can be set to a comma-separated list of unit ids. If that is done, the patrol only attacks these units when it ends up next to them and ignores any other unit.  This can in turn be used to have the patroller not attack at all, by setting ''attack='' to a non-existing id.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''one_time_only'''=no: (boolean)  If set to 'yes', the unit goes through the patrol route only once and then hangs out at the last waypoint.  It also always attacks any enemy unit on the last waypoint, independent of what ''attack='' is set to.&lt;br /&gt;
* '''out_and_back'''=no: (boolean)  By default, a patrol unit returns to the first waypoint after reaching the last, doing the patrol route as a loop.  If this parameter is set to 'yes', the unit instead reverses its course and perpetually does out and backs of the route.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Patrols&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=patrol&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Konrad&lt;br /&gt;
             waypoint_x=9,24,25&lt;br /&gt;
             waypoint_y=21,23,15&lt;br /&gt;
             one_time_only=yes&lt;br /&gt;
             attack=Gertburt&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Protect Unit Micro AI (ai_type=protect_unit) ==&lt;br /&gt;
&lt;br /&gt;
The Protect Unit Micro AI tries to move one or several units to a given location while keeping them safe from enemy attacks. It does so by keeping the protected units away from enemy units and close to friendly units. Other units on the AI side are controlled by the default Wesnoth AI.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Protect Unit&amp;quot; and &amp;quot;The Elves Besieged&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Protect Unit Micro AI by using&lt;br /&gt;
 ai_type=protect_unit&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Protect Unit specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[unit]''': A separate [unit] tag is required for each protected unit(s).  Each tag has required keys id=, goal_x=, goal_y= for each unit, as shown in the example below. {{DevFeature1.15|0}} '''goal_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''disable_move_leader_to_keep'''=no: (boolean) If set to 'yes', side leaders protected by this AI do not try to return to their keep.  This is necessary for this Micro AI to work with units that are side leaders and that are supposed to move to an off-keep location.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
*The Protect Unit Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].  Depending on the parameters, it might also modify the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|move leader to keep candidate action]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded. It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;HttT: The Elves Besieged&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=protect_unit&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Delfador&lt;br /&gt;
                 goal_x,goal_y=1,2&lt;br /&gt;
             [/unit]&lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
                 goal_x,goal_y=1,1&lt;br /&gt;
             [/unit]&lt;br /&gt;
 &lt;br /&gt;
             disable_move_leader_to_keep=true&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Recruiting Micro AI==&lt;br /&gt;
=== General Comments on the Recruiting Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Recruiting Micro AI replaces the default recruitment AI by alternative recruiting patterns.  Currently there are two recruiting algorithms you can choose from, random recruiting and the rush recruiting used in the new (as of Version 1.11.1) Experimental AI (see below).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of a different recruiting pattern, check out the &amp;quot;Recruiting&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  You can also explore the unit choices by watching the Experimental AI in a multiplayer game.&lt;br /&gt;
&lt;br /&gt;
'''Note:''' Obviously, the Recruitment Micro AI modifies the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|recruitment candidate action]].&lt;br /&gt;
&lt;br /&gt;
=== Random Recruitment AI (ai_type=recruit_random) ===&lt;br /&gt;
&lt;br /&gt;
This AI randomly selects a unit type from the recruitment list of the side.  The probability of choosing each type and the behavior in low-gold situations can be influenced with optional parameters.&lt;br /&gt;
&lt;br /&gt;
Enable the Random Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_random&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* '''[probability]''': This tag sets the probability of selecting the given unit type.  It has two required sub-keys:&lt;br /&gt;
** '''type''': comma-separated list of unit types to which this probability is to be applied&lt;br /&gt;
** '''probability''': (non-negative float)  The probability with which these unit types is to be selected.  All unit types for which no [probability] tag is defined receive a default probability of 1.0, thus values should be chosen relative to that.  As an example, in the code below swordsmen and peasants are given a probability of 8, while no other units have assigned probabilities.  Thus, each of these two unit types is 8 times more likely to be selected than any other individual unit type.&lt;br /&gt;
* '''skip_low_gold_recruiting'''=no: (boolean)  By default, the random recruitment AI chooses a unit to recruit only from those types for which it has sufficient gold.  If this parameter is set to 'yes', it will instead choose from all available unit types and end recruiting for the turn if there is not enough gold to recruit it this turn (even if other, affordable unit types exist).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a random recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_random&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [probability]&lt;br /&gt;
                 type=Swordsman,Peasant&lt;br /&gt;
                 probability=8&lt;br /&gt;
             [/probability]&lt;br /&gt;
 &lt;br /&gt;
             skip_low_gold_recruiting=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Rush Recruitment AI (ai_type=recruit_rushers) ===&lt;br /&gt;
&lt;br /&gt;
This AI replaces the default recruiting with the rush recruiting used in the new (as of Version 1.11.1) Experimental AI.&lt;br /&gt;
&lt;br /&gt;
Enable the Rush Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_rushers&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* {{DevFeature1.15|2}} '''high_level_fraction'''=0: (non-negative number) The approximate fraction of units of level 2 or higher to be recruited. This is defined as fraction of units on the map, not of new units being recruited during a turn or over several turns (which makes a difference if there are already units on the map). The effect is also cumulative per level (starting from level 2), meaning that 1/3 of L2 units will be recruited in the example above, 1/3*1/3=1/9 L3s etc. The default value is zero, which leaves the high-level unit recruiting to the default Experimental AI algorithm resulting usually in very few such units being recruited.&lt;br /&gt;
* '''randomness'''=0.1: (number)  A random number is applied to the rusher recruit engine's score to prevent the recruitment pattern from being too predictable. 0 causes no randomness to be applied, while larger numbers increase the random effect. A value of 1-2 generates results in which the random effect is approximately equal to the scored effect. Extremely high values are essentially entirely random.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a rush recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_rushers&lt;br /&gt;
             action=add&lt;br /&gt;
          [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Simple Attack Micro AI (ai_type=simple_attack) ==&lt;br /&gt;
&lt;br /&gt;
The Simple Attack Micro AI is meant as an addition to the default combat candidate action for when one wants to force the AI to do attacks that it would otherwise not do (for example on units that are 1 XP from leveling) or to execute certain attacks before all others (for example, attack with all &amp;quot;expendable&amp;quot; units first).  It is not set up to be a replacement for the default attacks.  Some notes:&lt;br /&gt;
&lt;br /&gt;
* The algorithm is different from and simpler than that of the default combat CA.  For example, no attack combinations are considered, only individual attacks.  As a result, the [[AiWML#List_of_AI_Aspects|default aspects]] (aggression, caution, [avoid], ...) are not taken into account.  Also, if lots of units are involved, evaluation of this Micro AI is potentially quite a bit slower than of the default attacks.&lt;br /&gt;
&lt;br /&gt;
* If an attack matching the Standard Unit Filters described below is found, this attack will always be executed, no matter how bad the odds are.  Thus, this Micro AI can be used to force the AI to do attacks it would otherwise avoid.&lt;br /&gt;
&lt;br /&gt;
* Other than selecting the attacker/defender units, this AI is currently not configurable.  Let us know if you would like to see certain options to be added.  (No guarantees that we will add them, but we will definitely consider them.)&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Simple Attack&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=simple_attack&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Simple Attack specific keys for the [micro_ai] tag===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  It is, however, in general not desirable to use this AI without at least one of the optional Standard Unit Filters below, as this AI is not meant as a replacement of the default combat candidate action.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=110000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 110,000, which means that it is executed right before the default combat candidate action.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Simple Attack action.  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units the AI should attack.  It defaults to all enemy units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
* '''weapon'''=-1: (integer) The number of the weapon that should be used for this attack.  Weapon numbers start at 1 (not 0).  -1 stands for &amp;quot;best weapon&amp;quot; (by whatever criterion the AI chooses to use).   '''Available from Wesnoth 1.11.8'''&lt;br /&gt;
&lt;br /&gt;
Below are 2 examples of Simple Attack Micro AI configurations from the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  The first one shows how to force the AI to attack with all Soulless and Walking Corpse units first:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110000&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless,Walking Corpse&lt;br /&gt;
             [/filter]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
The second executes attacks by Soulless on all enemy units that are 1 XP from leveling (something the RCA AI would generally not do).  Note that the Lua code bit is simply there in order to code the &amp;quot;1 XP from advancing&amp;quot; condition.  It is not needed for most other SUFs, as can be seen in the previous example.&lt;br /&gt;
         [lua]&lt;br /&gt;
             code=&amp;lt;&amp;lt;&lt;br /&gt;
                 function close_to_advancing(unit)&lt;br /&gt;
                     if (unit.experience &amp;gt;= unit.max_experience-1) then&lt;br /&gt;
                         return true&lt;br /&gt;
                     else&lt;br /&gt;
                         return false&lt;br /&gt;
                     end&lt;br /&gt;
                 end&lt;br /&gt;
             &amp;gt;&amp;gt;&lt;br /&gt;
         [/lua]&lt;br /&gt;
 &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110001&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless  # No Walking Corpses; L0 units don't advance enemy&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 lua_function = &amp;quot;close_to_advancing&amp;quot;&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
Several Simple Attack Micro AIs can be combined for the same AI side.  The two examples above are both used (note the different values for ca_score) in the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
== Custom Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|5}}&lt;br /&gt;
&lt;br /&gt;
From 1.13.5 onwards, adding a new Micro AI can be easily done without altering any core files or overriding the micro_ai tag. The [micro_ai] tag now looks up its ''ai_type'' parameter in the &amp;lt;code&amp;gt;wesnoth.micro_ais&amp;lt;/code&amp;gt; table and uses the micro AI setup function found there to determine which keys are required and allowed and what candidate actions to add.&lt;br /&gt;
&lt;br /&gt;
A micro AI setup function takes as its single parameter the contents of the [micro_ai] tag and returns three separate values:&lt;br /&gt;
*'''required''': A list of required keys and tags, as strings. Tags are indicated by enclosing them within square brackets in the string. If a key or tag in this list is missing from the [micro_ai] tag, an error will be printed.&lt;br /&gt;
*'''optional''': Similar to above, a list of optional keys and tags, as strings. No error will be printed if a key or tag in this list is missing, nor will an error be printed if it is present (as would be the case for unrecognized keys or tags).&lt;br /&gt;
*'''ca_params''': A table specifying the candidate actions that constitute the micro AI. It should contain an ''ai_id'' key (which specifies a base ID to be used for candidate actions of this micro AI) and a series of candidate action definitions. Each candidate action definition is a table containing the following keys:&lt;br /&gt;
**'''ca_id''': The ID used for the candidate action. This will be combined with the ''ai_id'' to produce the final ID.&lt;br /&gt;
**'''location''': Path to the Lua file that defines the candidate action.&lt;br /&gt;
**'''score''': The maximum score for the candidate action. If the [micro_ai] tag specified a ''ca_score'' parameter, the score here should normally be based on that.&lt;br /&gt;
&lt;br /&gt;
(The return statement would thus look like &amp;lt;syntaxhighlight lang='lua' inline&amp;gt;return required, optional, ca_params&amp;lt;/syntaxhighlight&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
Since the function has full access to the contents of the [micro_ai] tag, it can conditionally add candidate actions to '''ca_params''' depending on the values of other keys within the [micro_ai] tag. It can also perform other actions such as adding facets to an aspect; if it does such things, it should pay attention to the ''action'' key to determine whether it's being added or removed.&lt;br /&gt;
&lt;br /&gt;
There are two useful helper functions that can be used by Micro AI setup functions requiring aspect changes. They can be brought into scope by a line similar to the following:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='lua&amp;gt;local MAIH = wesnoth.require(&amp;quot;ai/micro_ais/micro_ai_helper.lua&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* MAIH.add_aspects(side, aspect_params)&lt;br /&gt;
* MAIH.delete_aspects(side, aspect_params)&lt;br /&gt;
&lt;br /&gt;
The ''aspect_params'' parameter is an array of tables, each of which contains the following keys:&lt;br /&gt;
*'''aspect''': The aspect to change.&lt;br /&gt;
*'''facet''': The WML defining the facet. This must include an ''id'' key in order for the aspect to be correctly deleted. For maximum compatibility, incorporate the ''ca_id'' key from the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AiWML&amp;diff=65255</id>
		<title>AiWML</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AiWML&amp;diff=65255"/>
		<updated>2020-01-02T14:55:04Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* List of AI Aspects */ some leader-related aspects now also take id lists&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{WML Tags}}&lt;br /&gt;
&lt;br /&gt;
== Customizing the Default AI Using WML ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
This page contains:&lt;br /&gt;
* Descriptions of the aspects and goals of the default AI&lt;br /&gt;
* Instructions for setting them up at the beginning of the scenario in a [side] tag&lt;br /&gt;
&lt;br /&gt;
This page does not contain:&lt;br /&gt;
* Instructions for [[LuaAI#Dynamic_Lua_Aspects|setting up aspects dynamically using Lua]].&lt;br /&gt;
* Instructions for [[Modifying_AI_Components#Modifying_Standard_Aspects|changing aspects and goals mid scenario]], although for many of them (for example, for all [[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Aspects|standard aspects]]) the methods are almost identical&lt;br /&gt;
* Instructions for some of the [[Modifying_AI_Components|more complex AI customization tasks]].&lt;br /&gt;
&lt;br /&gt;
== Defining Aspects and Goals of the RCA AI ==&lt;br /&gt;
&lt;br /&gt;
[[RCA_AI#Use_of_Aspects_in_the_RCA_AI|Aspects]] and [[RCA_AI#Use_of_Goals_and_Targets_in_the_RCA_AI|goals]] of the [[RCA_AI|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 [[#List_of_AI_Aspects|aspects themselves]] as arguments, as well as the following general keys:&lt;br /&gt;
* '''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 [https://github.com/wesnoth/wesnoth/blob/master/data/core/macros/schedules.cfg 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.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
* '''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. {{DevFeature1.13|5}} In 1.13, the meaning of '''ai_algorithm''' has changed and no longer selects a native AI that's built into the engine, as the RCA AI has now become the core system and no alternatives exist anymore. Instead, it selects a predefined ''[ai]'' definition, such as those defined in ''data/ai/ais''. Since these are defined in WML, you can easily build your own.&lt;br /&gt;
&lt;br /&gt;
An example of a simple use of the [ai] tag to set AI parameters (aspects) is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=wml&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    time_of_day=dusk&lt;br /&gt;
    aggression=0.99&lt;br /&gt;
    caution=0.01&lt;br /&gt;
    [avoid]&lt;br /&gt;
        [filter]&lt;br /&gt;
            side=2,3&lt;br /&gt;
        [/filter]&lt;br /&gt;
        radius=2&lt;br /&gt;
    [/avoid]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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 &amp;lt;code&amp;gt;:inspect&amp;lt;/code&amp;gt; in debug mode to verify whether an aspect was changed successfully.&lt;br /&gt;
&lt;br /&gt;
==List of AI Aspects==&lt;br /&gt;
&lt;br /&gt;
* '''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 &amp;lt;u&amp;gt;only&amp;lt;/u&amp;gt; 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:'''&lt;br /&gt;
** This is a simplified version of the more complex (and arguably more useful) application of a dynamic Lua ''advancements'' aspect, as described [[LuaAI#The_advancements_Aspect|here]].&lt;br /&gt;
** The ''advancements'' aspect only takes effect during the AI turn, that is, on offense only, not on defense.&lt;br /&gt;
** 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).&lt;br /&gt;
&lt;br /&gt;
* '''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.  The description below applies as written to the ''combat'' CA only, but aggression is taken into account by the ''high_xp_attack'' and ''spread_poison'' CAs as well (as of '''Wesnoth 1.13.5''' and '''1.15.3''' respectively).&lt;br /&gt;
** '''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.&lt;br /&gt;
** 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 &amp;gt;0 is found.  The combat CA score is zero if the highest attack score is &amp;lt;=0).&lt;br /&gt;
** 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.&lt;br /&gt;
** All the negative contributions to the score are multiplied by '(1-aggression)'.  This means that:&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
** 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.&lt;br /&gt;
&lt;br /&gt;
*'''[attacks]''': Filters the units considered for combat, both on the AI and the enemy sides.  Applies to the ''combat'', ''high_xp_attacks'' and ''spread_poison'' CAs only.  It cannot be set in the same way as the other aspects and is therefore described in a [[#Filtering_Combat_with_the_attacks_Aspect|separate section]] below.&lt;br /&gt;
&lt;br /&gt;
* '''[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''.&lt;br /&gt;
** '''[[StandardLocationFilter]]''': The locations for the AI to avoid.  Do not use a [filter_location] tag.&lt;br /&gt;
&lt;br /&gt;
* '''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 [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|evaluation scores]]):&lt;br /&gt;
** {{DevFeature1.15|3}} ''Retreat-injured'' CA:&lt;br /&gt;
*** Units only retreat if caution is greater than 0. Thus, this CA can be disabled simply by setting caution to zero or negative values.&lt;br /&gt;
*** For a unit to be considered for retreating, its hitpoints need to be below a certain threshold that depends on the terrain the unit is on and its level. In addition, there is also a multiplier that depends (non-linearly) on caution.  The higher the caution the higher the hitpoint threshold.  Or in other words, units start retreating with more hitpoints left when caution is higher.&lt;br /&gt;
** ''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].&lt;br /&gt;
** ''Combat'' CA: &lt;br /&gt;
*** 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 &amp;quot;exposure&amp;quot; rating of the units.  This exposure is then subtracted from the 'attack score' as described for ''aggression'' above.&lt;br /&gt;
*** 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.)&lt;br /&gt;
*** If the AI leader is used in an attack, the AI ''always'' uses 'caution=2' for the evaluation of that attack.&lt;br /&gt;
** ''Retreat'' CA: {{DevFeature1.15|3}} This CA has been removed.&lt;br /&gt;
*** 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 &amp;lt;tt&amp;gt;caution &amp;amp;times; their_power &amp;gt; our_power&amp;lt;/tt&amp;gt;. 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.&lt;br /&gt;
*** 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. &lt;br /&gt;
*** The AI never retreats if caution is set to 0 or lower.&lt;br /&gt;
** ''Move-to-targets'' CA:&lt;br /&gt;
*** 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. &amp;quot;Dangerous&amp;quot; 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 &amp;lt;tt&amp;gt;our_strength / their_strength &amp;gt; 0.5 + caution&amp;lt;/tt&amp;gt;. 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.&lt;br /&gt;
***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.&lt;br /&gt;
*** The AI also considers retreating units during the move-to-target phase based on criteria similar to those for the retreat CA.&lt;br /&gt;
&lt;br /&gt;
* '''[goal]'''=&amp;quot;&amp;quot;: Defines units or locations as [[#AI_Targets_and_Goals|move targets]] (not attack targets!) for the AI.  '''Applies to ''move-to-targets'' CA only'''. &amp;lt;-- 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 [[#The_.5Bgoal.5D_Tag|[goal] tag]] below for details.&lt;br /&gt;
&lt;br /&gt;
* '''grouping'''=&amp;quot;offensive&amp;quot;: (string) How the AI should try to group units.  Applies to ''move-to-targets'' CA only.  Possible values:&lt;br /&gt;
** ''offensive'': Makes the AI try to group units together before attacking.&lt;br /&gt;
** ''defensive'': Makes the AI group units together very conservatively, only advancing them much beyond its castle if it has overwhelming force.&lt;br /&gt;
** ''no'': Makes the AI use no grouping behavior.&lt;br /&gt;
&lt;br /&gt;
* '''leader_aggression'''=&amp;quot;-4.0&amp;quot;: 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''.&lt;br /&gt;
&lt;br /&gt;
* '''[leader_goal]'''=&amp;quot;&amp;quot;: Makes the AI try to move its leader to a specific location.  Applies to ''move-leader-to-goals'' CA only.&lt;br /&gt;
** '''x''', '''y''': The location toward which the AI should move its leader.&lt;br /&gt;
** '''auto_remove'''=no: (bool)  If 'no' (default), the AI moves the leader to the goal, after which he stays there until [leader_goal] is [[Modifying_AI_Components#Modifying_Standard_Aspects|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).&lt;br /&gt;
** '''id'''=&amp;quot;&amp;quot;: (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] [[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Aspects|facet]].  Thus, in principle for this to work, you need to set the id of the facet as described [[Modifying_AI_Components#Modifying_Standard_Aspects|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. {{DevFeature1.13|5}} 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.&lt;br /&gt;
**'''max_risk'''=1-caution: (double: meaningful values are &amp;gt;=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.&lt;br /&gt;
&lt;br /&gt;
* '''leader_ignores_keep'''=no: (bool) Set to 'yes' for this aspect to apply to all leaders of a side. {{DevFeature1.15|3}} In addition to 'yes/no', the aspect can now also be set to a comma-separated list of unit ids. The aspect then only applies to leaders with ids matching an id on that list. Setting leader_ignores_keep for some or all leaders means that:&lt;br /&gt;
** The affected 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). Thus, these leaders behave in the same way as any other unit of the AI side.&lt;br /&gt;
** This also means that these 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.&lt;br /&gt;
&lt;br /&gt;
* '''leader_value'''=3: (double) A number 0 or higher which determines the value of enemy leaders as [[#AI_Targets_and_Goals|targets]].  Affects ''move-to-targets CA'' only (and therefore specifically does not apply to attacks).&lt;br /&gt;
&lt;br /&gt;
* '''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''. {{DevFeature1.15|3}} In addition to 'yes/no', the aspect can now also be set to a comma-separated list of unit ids. The aspect then only applies to leaders with ids matching an id on that list.&lt;br /&gt;
&lt;br /&gt;
* '''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. {{DevFeature1.15|3}} In addition to 'yes/no', the aspect can now also be set to a comma-separated list of unit ids. The aspect then only applies to leaders with ids matching an id on that list.&lt;br /&gt;
&lt;br /&gt;
*'''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 [[AI_Recruitment|separate page]].&lt;br /&gt;
&lt;br /&gt;
* '''scout_village_targeting'''=3: (double) The AI multiplies the value of village [[#AI_Targets_and_Goals|targets]] for scouts by this value.  Affects ''move-to-targets'' CA only.&lt;br /&gt;
&lt;br /&gt;
* '''simple_targeting'''=no: (bool) If 'yes', the AI moves its units toward [[#AI_Targets_and_Goals|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.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
* '''village_value'''=1: (double) A number 0 or higher which determines how much the AI tries to go for villages as [[#AI_Targets_and_Goals|targets]].  Affects ''move-to-targets'' CA only.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
=== Removed AI Aspects ===&lt;br /&gt;
&lt;br /&gt;
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. {{DevFeature1.13|5}} Except for attack_depth, these have now all been removed.&lt;br /&gt;
&lt;br /&gt;
* '''attack_depth'''=5: (int)&lt;br /&gt;
* '''number_of_possible_recruits_to_force_recruit'''=3.1: (double)&lt;br /&gt;
* '''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.&lt;br /&gt;
** It is, in principle, still possible to use this aspect together with the [[Wesnoth_AI_Framework#Recruitment_Stage|recruitment stage]], but this is discouraged for the reasons given at this link.&lt;br /&gt;
* '''recruitment_ignore_bad_combat'''=no: (bool)&lt;br /&gt;
* '''recruitment_ignore_bad_movement'''=no: (bool)&lt;br /&gt;
&lt;br /&gt;
==AI Targets and Goals==&lt;br /&gt;
&lt;br /&gt;
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 [[RCA_AI#RCA_AI_Aspect_and_Goal_Configuration|base values]].  Additional targets can be defined using the [goal] tag.&lt;br /&gt;
&lt;br /&gt;
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 [[RCA_AI#Use_of_Goals_and_Targets_in_the_RCA_AI|here]].  The sections below only describe how to set up goals in order to add targets for the ''move-to-targets'' CA.&lt;br /&gt;
&lt;br /&gt;
===The [goal] Tag===&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
* '''name'''=&amp;quot;target&amp;quot;: (string)  The following values are possible and result in different types of targets, as shown in the examples below:&lt;br /&gt;
** ''target'':  The (default) target goal specifies target units (not necessarily enemy units) toward which the AI should move its units. {{DevFeature1.13|5}} target_unit is now a synonym for this.&lt;br /&gt;
** ''target_location'': Specifies target locations toward which the AI should move its units.&lt;br /&gt;
** ''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.&lt;br /&gt;
** ''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.&lt;br /&gt;
** ''protect_my_unit'': ('''deprecated''') &amp;lt;s&amp;gt;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.&amp;lt;/s&amp;gt;&lt;br /&gt;
** ''lua_goal'': A dynamically calculated goal written in Lua. See [[LuaAI#Lua_Goals_and_Targets|here]] for details on how this works.&lt;br /&gt;
&lt;br /&gt;
* '''[criteria]'''=&amp;quot;&amp;quot;: Contains a [[StandardUnitFilter]] (for ''target'', ''protect_unit'' or ''protect_my_unit'') or [[StandardLocationFilter]] (for ''target_location'' or ''protect_location'') describing the targets.&lt;br /&gt;
&lt;br /&gt;
* '''value'''=0: (double) The value of the goal.&lt;br /&gt;
&lt;br /&gt;
* '''protect_radius'''=20: (int) The protection radius.  Applies to ''protect_location'', ''protect_unit'' and ''protect_my_unit''.&lt;br /&gt;
&lt;br /&gt;
===Examples of [goal] Tag Usage===&lt;br /&gt;
&lt;br /&gt;
'''target:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        [criteria] #NOTE: this is a SUF, because we're targeting a unit&lt;br /&gt;
            side=3&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''target_location:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=target_location&lt;br /&gt;
        [criteria] #NOTE: this is a SLF, because we're targeting a location&lt;br /&gt;
            x,y=42,20&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''protect_location:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=protect_location&lt;br /&gt;
        [criteria] #NOTE: this is a SLF, because we're protecting a location&lt;br /&gt;
            x,y=42,20&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        protect_radius=16&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''protect_unit:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=protect_unit&lt;br /&gt;
        [criteria] #NOTE: this is a SUF, because we're protecting a unit&lt;br /&gt;
            side=3&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        protect_radius=16&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''How to use protect_unit to protect the own leader''' (We are side 2)&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=protect_unit&lt;br /&gt;
        [criteria]&lt;br /&gt;
            side=2&lt;br /&gt;
            canrecruit=yes&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        protect_radius=8&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Deprecated AI Targeting Aspects ===&lt;br /&gt;
&lt;br /&gt;
The following AI targeting parameters currently still work, but have been superseded by the [[#The_.5Bgoal.5D_Tag|[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.&lt;br /&gt;
&lt;br /&gt;
* '''[target]'''=&amp;quot;&amp;quot;: '''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.&lt;br /&gt;
** '''[[StandardUnitFilter]]''': Do not use a [filter] tag.&lt;br /&gt;
** '''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.&lt;br /&gt;
&lt;br /&gt;
* '''[protect_location]'''=&amp;quot;&amp;quot;: '''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.&lt;br /&gt;
** '''x''', '''y''': Standard coordinates. These indicate the location the AI is protecting.&lt;br /&gt;
** '''radius''': The radius around it to protect (0 indicates a single hex). {{DevFeature1.13|5}} This key has been changed to protect_radius, though it's preferred to change old code to use the [goal] tag instead.&lt;br /&gt;
** '''value''': The importance of protecting this location.&lt;br /&gt;
&lt;br /&gt;
* '''[protect_unit]'''=&amp;quot;&amp;quot;: '''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.&lt;br /&gt;
** '''[[StandardUnitFilter]]''': The unit(s) to protect. Do not use a [filter] tag.&lt;br /&gt;
** '''radius''': The radius around it to protect (0 indicates a single hex). {{DevFeature1.13|5}} This key has been changed to protect_radius, though it's preferred to change old code to use the [goal] tag instead.&lt;br /&gt;
** '''value''': The importance of protecting this unit.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
== Filtering which Units Participate in which Actions==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}}&lt;br /&gt;
&lt;br /&gt;
Each [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|candidate action of the default AI]] can be restricted to apply to only a subset of the AI units. This is done by adding a '''[filter_own]''' tag to the '''[[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Candidate_Actions|[candidate_action]]]''' tag definition.&lt;br /&gt;
&lt;br /&gt;
Typical use cases:&lt;br /&gt;
* Restricting an action to certain units so that other units are not used to this. For example, village grabbing could be restricted to scout units only, so that slower units do not take detours on their way toward the enemies. See the example below.&lt;br /&gt;
* Excluding units from a certain action in order to have them available for other actions afterward. For example, one might want to add a custom candidate action which moves healers behind injured units (or use the existing [[Micro_AIs|Micro AI]] for this). Ideally, this should happen after ''all'' other units have moved, specifically also after the move-to-targets candidate action (MtT CA) is done. In the default setting, however, the MtT CA would also move the healers. Thus, we would want to exclude them using a '''[filter_own]''' tag.&lt;br /&gt;
* Adding two instances of the same candidate action with different scores to force the order in which units participate in this action. For example, one could add another instance of the move-leader-to-keep CA with a '''[filter_own]''' tag restricting it to one leader, in order to make this leader move to a keep (and therefore recruit) first.&lt;br /&gt;
&lt;br /&gt;
Note that for the combat AI, using the attacks aspect to filter units as described in the next section '''is slightly more efficient'''. Thus, that should be the preferred method if there are many units on the map.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[candidate_action]&lt;br /&gt;
	id=villages&lt;br /&gt;
	engine=cpp&lt;br /&gt;
	name=ai_default_rca::get_villages_phase&lt;br /&gt;
	max_score=60000&lt;br /&gt;
	score=60000&lt;br /&gt;
	[filter_own]&lt;br /&gt;
		type=Wolf Rider&lt;br /&gt;
	[/filter_own]&lt;br /&gt;
[/candidate_action]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Filtering Combat with the ''attacks'' Aspect==&lt;br /&gt;
&lt;br /&gt;
The ''attacks'' aspect lets us filter the units considered by the ''combat'', ''high_xp_attacks'' and ''spread_poison'' 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&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [aspect]&lt;br /&gt;
        id=attacks&lt;br /&gt;
        [facet]&lt;br /&gt;
            invalidate_on_gamestate_change=yes&lt;br /&gt;
            [filter_own]&lt;br /&gt;
                type=Elvish Sorceress,Elvish Enchantress,Elvish Sylph&lt;br /&gt;
            [/filter_own]&lt;br /&gt;
            [filter_enemy]&lt;br /&gt;
                race=undead&lt;br /&gt;
            [/filter_enemy]&lt;br /&gt;
        [/facet]&lt;br /&gt;
    [/aspect]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Several important notes:&lt;br /&gt;
* The ''attacks'' aspect is a ''composite'' aspect and cannot be defined using the simple syntax of the other ''standard'' aspects.  See [[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Aspects|here]] for a description of the syntax of the example.&lt;br /&gt;
* This only works if &amp;lt;code&amp;gt;invalidate_on_gamestate_change=yes&amp;lt;/code&amp;gt; is 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 [[Wesnoth_AI_Framework#Some_more_on_the_invalidation_keys|here]].&lt;br /&gt;
* If [filter_own] or [filter_enemy] are omitted, the selection defaults to all units of the respective sides.&lt;br /&gt;
* '''Most importantly''': The above example results in sorceress-line vs undead attacks being the &amp;lt;u&amp;gt;only&amp;lt;/u&amp;gt; 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:&lt;br /&gt;
** Setting up the [filter_own] or [filter_enemy] [[StandardUnitFilter]]s to adapt dynamically to the situation on the map.&lt;br /&gt;
** 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_AIs#Micro_AI_Test_and_Demo_Scenarios|Micro AI test scenario]] [https://github.com/wesnoth/wesnoth/blob/master/data/ai/micro_ais/scenarios/protect_unit.cfg ''Protect Unit''].&lt;br /&gt;
** Using [[LuaAI#Dynamic_Lua_Aspects|dynamic Lua aspects]] is in principle also possible, but far from easy due to the complex tables this aspect returns.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|5}}&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [attacks]&lt;br /&gt;
        invalidate_on_gamestate_change=yes&lt;br /&gt;
        [filter_own]&lt;br /&gt;
            type=Elvish Sorceress,Elvish Enchantress,Elvish Sylph&lt;br /&gt;
        [/filter_own]&lt;br /&gt;
        [filter_enemy]&lt;br /&gt;
            race=undead&lt;br /&gt;
        [/filter_enemy]&lt;br /&gt;
    [/attacks]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category: WML Reference]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=RCA_AI&amp;diff=65231</id>
		<title>RCA AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=RCA_AI&amp;diff=65231"/>
		<updated>2019-12-22T19:53:53Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* The Candidate Actions (CAs) of the main_loop Stage in the RCA AI */ remove an outdated sentence&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==RCA AI Summary==&lt;br /&gt;
&lt;br /&gt;
The RCA (Register Candidate Action) AI is the default AI since Wesnoth 1.9.  Its name stems from the fact that it has a list of potential actions, called ''candidate actions'' (CAs), that are evaluated for each move and are executed in order of evaluation score.  The RCA AI is highly configurable in WML.  It can also be modified by removing default CAs or adding custom CAs.&lt;br /&gt;
&lt;br /&gt;
This page describes how the RCA AI is assembled out of the different components of Wesnoth's [[Wesnoth_AI_Framework#Wesnoth_AI_Framework:_A_Composite_AI|composite AI framework]].  For related information also see:&lt;br /&gt;
* [[Wesnoth_AI_Framework#Types_of_AI_Components|Types of AI Components]]: General descriptions of the different AI components.&lt;br /&gt;
* [[AiWML]]: Customizing the RCA AI by setting AI parameters (aspects and goals)&lt;br /&gt;
* [[Creating Custom AIs]] and [[Modifying AI Components]]: Adding additional functionality to the RCA AI, or replacing parts or all of it.&lt;br /&gt;
* [[Wesnoth AI]]: Links to all Wesnoth AI resources on this wiki.&lt;br /&gt;
&lt;br /&gt;
== RCA AI components  ==&lt;br /&gt;
&lt;br /&gt;
The RCA AI utilizes the following components:&lt;br /&gt;
&lt;br /&gt;
* The [[Wesnoth_AI_Framework#Stages|'''''main_loop'' stage''']] with its candidate action evaluation loop.&lt;br /&gt;
&lt;br /&gt;
* [[Wesnoth_AI_Framework#The_Candidate_Actions_of_the_main_loop_Stage|'''Candidate actions''']] are the potential actions the AI might take during an AI turn, such as attacking, village grabbing, recruiting or retreating injured units.&lt;br /&gt;
&lt;br /&gt;
* [[Wesnoth_AI_Framework#Aspects_and_Goals|'''Aspects''']] are the parameters which can be used to configure the AI behavior.&lt;br /&gt;
&lt;br /&gt;
* [[Wesnoth_AI_Framework#Aspects_and_Goals|'''Goals''']] are a specific type of aspect which define targets toward which the AI moves.  It is ''very important'' to understand that these are only move-to targets, not attack targets.&lt;br /&gt;
&lt;br /&gt;
Links here are to the general descriptions of these components in the [[Wesnoth AI Framework]], while the remainder of this page is about their use in the RCA AI.&lt;br /&gt;
&lt;br /&gt;
=== The Candidate Actions (CAs) of the ''main_loop'' Stage in the RCA AI ===&lt;br /&gt;
&lt;br /&gt;
For each move of its turn, the AI evaluates a number of potential actions, called ''candidate actions'' (CAs). The CA with the highest evaluation score is executed and the process is repeated, until no more valid moves are found.  This ends the AI turn.  (Note: This is the general process of the [[Wesnoth_AI_Framework#Stages|''main_loop'' stage]], it is not specific to the RCA AI.)&lt;br /&gt;
&lt;br /&gt;
Most of the RCA AI CAs have a single fixed scores, with a few of the Lua CAs have two distinct (but otherwise also fixed) scores, as indicated below.  These values can, in principle, be modified, but generally only to other fixed values.  For example, the recruitment CA by default has a score of 180,000.  The default score for attacks is 100,000.  Thus, if recruiting is possible, it will always be done before attacks.  If the score of the recruitment CA were lowered to below 100,000, then attacks would always be evaluated (and executed, if possible) before recruiting.&lt;br /&gt;
&lt;br /&gt;
It is important to realize though that the default score settings do not mean that all recruiting always happens before all attacks.  The &amp;quot;if possible&amp;quot; clauses in the previous paragraph become important here.  Consider the case that all castle hexes are occupied by AI units which were recruited on the previous turn and have not been used yet this turn.  No recruiting is possible (in which case the CA returns 0), so the AI moves on to other CAs.  Once it gets to attacks, one of the units (or a group of units) might be selected to move off the castle and attack a close-by enemy.  Now a castle hex is available for recruiting, which now has the highest evaluation score and is executed.  This cycle might be repeated several times.&lt;br /&gt;
&lt;br /&gt;
In its default configuration, the RCA AI evaluates the following CAs, sorted here in order of their evaluation score:&lt;br /&gt;
&lt;br /&gt;
*'''Goto''' CA (score 200,000; C++ CA): Move units toward the coordinates set by [[SingleUnitWML|goto_x, goto_y]] in their unit WML.&lt;br /&gt;
&lt;br /&gt;
*{{DevFeature1.15|3}} '''Castle switching''' CA (scores 195,000 and 15,000; Lua CA): move the leader to a different castle, generally one that is closer to the enemy leader(s).&lt;br /&gt;
&lt;br /&gt;
*{{DevFeature1.15|3}} '''Retreat injured''' CA (scores 192,000 and 95,000; Lua CA): retreat injured units that are threatened by enemies to healing locations and away from these enemies.&lt;br /&gt;
&lt;br /&gt;
*{{DevFeature1.15|3}} '''Spread poison''' CA (score 190,000; Lua CA): spread poison to enemies, while taking care not to try to poison unpoisonable or already poisoned units.&lt;br /&gt;
&lt;br /&gt;
*'''Recruitment''' CA (score 180,000; C++ CA): Recruit or recall units.&lt;br /&gt;
&lt;br /&gt;
*'''Move leader to goals''' CA (score 140,000; C++ CA): Move the leader toward goals set by [[AiWML#List_of_AI_Aspects|[leader_goal]]].&lt;br /&gt;
&lt;br /&gt;
*'''Move leader to keep''' CA (score 120,000; C++ CA): Move the leader toward the closest available keep.&lt;br /&gt;
&lt;br /&gt;
*'''High XP attack''' CA (score 100,010; Lua CA): An addition to the default AI as of '''Wesnoth 1.13.6''', this CA performs attacks on enemy units so close to leveling that the combat CA (below) does not attack them.&lt;br /&gt;
&lt;br /&gt;
*'''Combat''' CA (score 100,000; C++ CA): Attack enemy units that are in range.  Note that this CA includes the move to a hex adjacent to the enemy unit as well as the attack itself.&lt;br /&gt;
&lt;br /&gt;
*{{DevFeature1.15|3}} '''Place healers''' CA (score 96,000; Lua CA): Place healers adjacent to injured units that have already moved.&lt;br /&gt;
&lt;br /&gt;
*'''Healing''' CA (score 80,000; C++ CA): Move units onto healing locations (generally villages, but also next to healers that were placed previously).&lt;br /&gt;
&lt;br /&gt;
*'''Villages''' CA (score 60,000; C++ CA): Move units onto villages that are unowned or owned by an enemy.&lt;br /&gt;
&lt;br /&gt;
*'''Retreat''' CA (score 40,000; C++ CA): Evaluate if any units are in grave danger or hopelessly outnumbered, and if so, retreat them.  *{{DevFeature1.15|3}} This CA was removed and replaced by the &amp;quot;retreat injured&amp;quot; CA described above&lt;br /&gt;
&lt;br /&gt;
*'''Move to targets''' CA (score 20,000; C++ CA): Evaluate potential [[RCA_AI#Use_of_Goals_and_Targets_in_the_RCA_AI|targets]] and move units toward them.  The evaluation takes into account how valuable the targets are (this is configurable by setting [[AiWML#AI_Targets_and_Goals|goals]]), how easily the AI's units can get there, how exposed to attacks they will be, etc.  Targets are enemy leaders, enemy units that pose a threat to the AI leader and villages, as well as anything defined in a [[AiWML#The_.5Bgoal.5D_Tag|[goal] tag]].  It is important to understand that targets only apply to the ''move-to-targets'' CA.  If a target is within range of the AI's unit(s) on the current turn, other CAs, such as ''combat'', ''healing'' or ''villages'' have a higher evaluation score and are (usually) executed first.  Thus, a target is specifically &amp;lt;u&amp;gt;not&amp;lt;/u&amp;gt; an attack target, but a move target.&lt;br /&gt;
&lt;br /&gt;
*'''Leader shares keep''' CA (score 10,000; C++ CA): Move the leader off the keep to let an allied leader recruit, if an allied leader can get to the keep in one move.  Note: This CA used to be called ''passive_leader_shares_keep''.  That name still works for the time being but will be deprecated at some point.&lt;br /&gt;
&lt;br /&gt;
*{{DevFeature1.15|3}} '''Move to any enemy''' CA (score 1,000; Lua CA): Move AI units toward any enemy. That is a fall-back CA in case none of the other CAs moved the unit. This is generally only the case on maps without enemy leaders and villages.&lt;br /&gt;
&lt;br /&gt;
As already mentioned, the CAs are always evaluated (and executed, if a possible move is found) in the order given above.  For example, the ''goto'' CA is always the first move that is done (if ''goto_x'' and ''goto_y'' are defined and a move toward that location is possible).  Equivalently, if a valid attack is found in the ''combat'' CA, it is &amp;lt;u&amp;gt;always&amp;lt;/u&amp;gt; executed before any healing, village grabbing or move-to-target moves.  One of the effects this has is that attacks always have priority over any goals/targets of the move-to-target phase.  It is '''very important''' to understand this, as it is a frequent source of confusion for scenario creators.&lt;br /&gt;
&lt;br /&gt;
Note that nothing in Wesnoth's [[Wesnoth_AI_Framework|general AI mechanism]] limits CAs to having fixed evaluation scores.  For example, some of the [[Micro AIs]] as well as some of the new Lua AI CAs return variable scores.&lt;br /&gt;
&lt;br /&gt;
=== Use of Aspects in the RCA AI ===&lt;br /&gt;
&lt;br /&gt;
An aspect is a parameter that the AI framework provides to scenario creators for configuring the AI behavior.  For example, one can change how aggressive/careful the AI acts, how recruiting is prioritized or whether the leader participates in attacks.&lt;br /&gt;
&lt;br /&gt;
The RCA AI has a large number of aspects.  Their description is therefore given [[AiWML|its own page]].&lt;br /&gt;
&lt;br /&gt;
=== Use of Goals and Targets in the RCA AI ===&lt;br /&gt;
&lt;br /&gt;
''Goals'' define AI ''targets'' which, in turn, are used in the ''move-to-targets'' candidate action 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. A score is then assigned to each target based on this base value, as well as on the movement cost required to get there, whether moving there would put the units involved in danger, etc.&lt;br /&gt;
&lt;br /&gt;
It is possible to define additional targets or influence the relative ratings of the default targets. This is done with the [[AiWML#The_.5Bgoal.5D_Tag|[goal] tag]], in which we can set criteria selecting targets and define their base values.  These are then evaluated in the same way as default targets. Values set with the [goal] tag should always be relative to each other and to the values of the default targets (see [[RCA_AI#RCA_AI_Aspect_and_Goal_Configuration|below]] for the latter). The 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'.&lt;br /&gt;
&lt;br /&gt;
We stress again that these targets apply to the ''move-to-targets'' CA only and have no influence on other CAs. This is significant since CAs that deal with, for example, combat or village-grabbing have a higher score than the move-to-targets CA and are therefore always executed first. In practice that means that targets set with the [goal] tag only affect the AI behavior for targets that it cannot reach during the current turn, and only after combat, village-grabbing etc. are finished.&lt;br /&gt;
&lt;br /&gt;
==RCA AI Configuration==&lt;br /&gt;
&lt;br /&gt;
This section describes the configuration of the RCA AI.  An understanding of this setup is essential for [[Modifying AI Components]] or [[Creating Custom AIs]].&lt;br /&gt;
&lt;br /&gt;
In the following, we assume that the reader is familiar with the general principles of the [[Wesnoth AI Framework]].  The keys and tags used in the config snippets shown below are explained there.&lt;br /&gt;
&lt;br /&gt;
=== RCA AI Stage Configuration ===&lt;br /&gt;
&lt;br /&gt;
The RCA AI uses only the ''main_loop'' stage.  It is defined in file [https://github.com/wesnoth/wesnoth/blob/master/data/ai/ais/ai_default_rca.cfg ai/ais/ai_default_rca.cfg] in the Wesnoth data directory.  {{DevFeature1.15|3}} Note that the example below is from a previous Wesnoth version as new candidate actions have since been added to the default AI. The linked file always contains the most up-to-date configuration though and can be used as reference.&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    id=ai_default_rca&lt;br /&gt;
    description=_&amp;quot;Multiplayer_AI^Default AI (RCA)&amp;quot; # wmllint: no spellcheck&lt;br /&gt;
    [stage]&lt;br /&gt;
        id=main_loop&lt;br /&gt;
        name=ai_default_rca::candidate_action_evaluation_loop&lt;br /&gt;
        {AI_CA_GOTO}                         # id=&amp;quot;goto&amp;quot;&lt;br /&gt;
        {AI_CA_RECRUITMENT}                  # id=&amp;quot;recruitment&amp;quot;&lt;br /&gt;
        {AI_CA_MOVE_LEADER_TO_GOALS}         # id=&amp;quot;move_leader_to_goals&amp;quot;&lt;br /&gt;
        {AI_CA_MOVE_LEADER_TO_KEEP}          # id=&amp;quot;move_leader_to_keep&amp;quot;&lt;br /&gt;
        {AI_CA_HIGH_XP_ATTACK}               # id=&amp;quot;high_xp_attack&amp;quot; (Wesnoth 1.13.6 and later only)&lt;br /&gt;
        {AI_CA_COMBAT}                       # id=&amp;quot;combat&amp;quot;&lt;br /&gt;
        {AI_CA_HEALING}                      # id=&amp;quot;healing&amp;quot;&lt;br /&gt;
        {AI_CA_VILLAGES}                     # id=&amp;quot;villages&amp;quot;&lt;br /&gt;
        {AI_CA_RETREAT}                      # id=&amp;quot;retreat&amp;quot;&lt;br /&gt;
        {AI_CA_MOVE_TO_TARGETS}              # id=&amp;quot;move_to_targets&amp;quot;&lt;br /&gt;
        {AI_CA_PASSIVE_LEADER_SHARES_KEEP}   # id=&amp;quot;passive_leader_shares_keep&amp;quot;&lt;br /&gt;
     [/stage]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Note''': Before the RCA AI became the default, it was necessary to include this file (or one of its aliases) in the side definition.  That is not required any more – it is automatically included for all computer controlled sides, except when a stage is defined explicitly using the [stage] tag in the side definition.&lt;br /&gt;
&lt;br /&gt;
=== RCA AI Candidate Action Configuration ===&lt;br /&gt;
&lt;br /&gt;
In the configuration of the ''main_loop'' stage shown above, each candidate action is included as a macro.  Expanding the macros results in the following syntax (''combat'' CA example):&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[candidate_action]&lt;br /&gt;
    id=combat&lt;br /&gt;
    engine=cpp&lt;br /&gt;
    name=ai_default_rca::combat_phase&lt;br /&gt;
    max_score=100000&lt;br /&gt;
    score=100000&lt;br /&gt;
[/candidate_action]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Available Candidate Actions ====&lt;br /&gt;
&lt;br /&gt;
See file [https://github.com/wesnoth/wesnoth/blob/master/data/core/macros/ai_candidate_actions.cfg data/core/macros/ai_candidate_actions.cfg] for the definitions of all the macros, as well as for the ids, names and default scores of the available CAs.&lt;br /&gt;
&lt;br /&gt;
=== RCA AI Aspect and Goal Configuration ===&lt;br /&gt;
&lt;br /&gt;
When a game is created, '''aspects''' are initialized with the default values given at [[AiWML#List_of_AI_Aspects|AiWML]].  In the [https://github.com/wesnoth/wesnoth/blob/master/data/ai/utils/default_config.cfg configuration file], they are in ''composite'' aspect format such as&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[aspect]&lt;br /&gt;
    id=aggression&lt;br /&gt;
    engine=cpp&lt;br /&gt;
    name=composite_aspect&lt;br /&gt;
    [default]&lt;br /&gt;
        engine=cpp&lt;br /&gt;
        name=standard_aspect&lt;br /&gt;
        value=0.4&lt;br /&gt;
    [/default]&lt;br /&gt;
[/aspect]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
for ''aggression'', or&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[aspect]&lt;br /&gt;
    id=avoid&lt;br /&gt;
    engine=cpp&lt;br /&gt;
    name=composite_aspect&lt;br /&gt;
    [default]&lt;br /&gt;
        engine=cpp&lt;br /&gt;
        name=standard_aspect&lt;br /&gt;
        [value]&lt;br /&gt;
            [not]&lt;br /&gt;
            [/not]&lt;br /&gt;
        [/value]&lt;br /&gt;
    [/default]&lt;br /&gt;
[/aspect]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
for ''[avoid]''.&lt;br /&gt;
&lt;br /&gt;
The default '''goals''' are not initialized in configuration files.  Instead, they are defined in the C++ code of the ''move-to-targets'' candidate action as follows:&lt;br /&gt;
* Enemy leaders: Value is read from aspect ''leader_value'', which defaults to 3.&lt;br /&gt;
* Villages: Value is read from aspect ''village_value'', which defaults to 1.&lt;br /&gt;
* Any enemy unit that can attack the AI's leader on the enemy's next turn: The value of these targets is proportional to the threat they pose to the AI leader and generally higher than those of the other two types of default targets.&lt;br /&gt;
&lt;br /&gt;
The first two types of goals can be disabled by setting aspects ''village_value'' or ''leader_value'' to zero.  By contrast, close enemies cannot be removed from the list of targets, other than by removing the ''move-to-targets'' CA altogether.  One can, however, effectively disable them by setting other goals with vastly higher values (such as 100,000).&lt;br /&gt;
&lt;br /&gt;
==== Available Aspects and Goals ====&lt;br /&gt;
&lt;br /&gt;
See file [https://github.com/wesnoth/wesnoth/blob/master/data/ai/utils/default_config.cfg data/ai/utils/default_config.cfg] for the definitions of all the aspects and their default values.  The available goals are shown [[AiWML#AI_Targets_and_Goals|here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=65226</id>
		<title>Micro AIs</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=65226"/>
		<updated>2019-12-19T15:37:46Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Bottleneck Defense specific keys for the [micro_ai] tag: */ new [filter] optional parameter&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== A Few General Words about Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
* Micro AIs allow Wesnoth campaign and scenario authors to add new AI functionality without the need for any AI programming. They bring new specialized behavior to Wesnoth that cannot be achieved, at least not easily, by adjusting default AI parameters (such as those described at [[AiWML]]) or with WML code.&lt;br /&gt;
&lt;br /&gt;
* A Micro AI is activated and configured (or deleted) via the [micro_ai] tag, requiring only a few lines of WML code.&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.6''', the Micro AI behavior was inconsistent when it came to '''hidden enemies and petrified units'''.  Some Micro AIs ignored those, while others did not.  In some case there was even a potential for AI errors when an AI unit ran into hidden or petrified enemies.  As of 1.13.6, this has all been fixed.  The Micro AIs now correctly and consistently ignore hidden units and treat petrified units as blocking the hex they are on but having no effect otherwise, in the same way as the default AI does.  '''Important notes:'''&lt;br /&gt;
** It is not possible to re-include hidden/petrified units via the Standard Unit Filters (SUFs) used by some of the Micro AIs. In fact, if this is attempted, the filters will generally not match any unit.&lt;br /&gt;
*** This only applies to SUFs which go directly into the [micro_ai] tag. Any SUF that is a sub-tag of another filter (for example, of a Standard Location Filter) is taken literally and returns all units matching the filter. If hidden or petrified units are to be excluded from such a sub-filter, that needs to be specified directly in that filter.&lt;br /&gt;
** We also note that, because all the Micro AIs are written in Lua, it is possible to [[Micro_AIs#Using_Development_.281.13.29_Version_Micro_AIs_in_your_Stable_.281.12.29_Version_Add-on|move a Micro AI's code into one's add-on]] and modify it to whatever scenario-specific behavior is desired (this does not only apply to hidden/petrified units, but to all behavior). Setting up general Micro AI behavior that works in every possible setting is not feasible, but adapting it to work (only) in the specific setting of a given scenario is often quite simple. If you do not know how to do this, please post a message on the Wesnoth forums and we will be happy to help you with it.&lt;br /&gt;
&lt;br /&gt;
* The '''[[AiWML#List_of_AI_Aspects|default AI aspects]] are, for the most part, not taken into account by the Micro AIs'''.  For example, unless noted otherwise, specifying a value for ''aggression'' does not change the MAI behavior, and locations specified in an ''[avoid]'' tag generally can be entered by units controlled by an MAI.  This is a feature of the Micro AIs, not a bug.  If you feel that a certain default AI aspect should also apply to a Micro AI, please let us know in the [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 Micro AIs Feedback thread].&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.7''', the Micro AIs did not all work reliably when the '''AI side is under shroud''' and it may have been necessary to turn shroud off for the side when using a Micro AI. {{DevFeature1.13|7}} The Micro AIs now all work for sides under shroud. They do this by disregarding shroud for terrain purposes when path finding, consistent with default Wesnoth AI behavior, while still ignoring hidden units (including those hidden under shroud) correctly.&lt;br /&gt;
&lt;br /&gt;
* Most Micro AIs in their default setting apply to all units of a side, although many of them provide parameters for filtering the units that should be affected by the new behavior.  If the Micro AI is applied only to part of the units of a side, the other units follow the default Wesnoth AI behavior.&lt;br /&gt;
&lt;br /&gt;
* Micro AIs are available in mainline since Wesnoth 1.11/1.12.&lt;br /&gt;
&lt;br /&gt;
== Micro AI Test and Demo Scenarios ==&lt;br /&gt;
&lt;br /&gt;
All Micro AIs described here can be checked out in a number of test and demo scenarios.  Test scenarios can be accessed from the command line by typing&lt;br /&gt;
 path/wesnoth-executable -t micro_ai_test&lt;br /&gt;
or (depending on your system and Wesnoth version)&lt;br /&gt;
 path/wesnoth-executable -tmicro_ai_test&lt;br /&gt;
This starts up a &amp;quot;switchboard scenario&amp;quot; in which you can select the Micro AI demonstration scenario you want to check out.  Here, ''path/wesnoth-executable'' needs to be replaced by whatever the path and filename of the Wesnoth executable is on your system.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|8}}&lt;br /&gt;
&lt;br /&gt;
You can also access the test scenarios by assigning a hotkey to &amp;quot;Start Test Scenario&amp;quot; in the preferences. Then, simply press the hotkey at the titlescreen, possibly wait out the loading screen, and select the desired scenario from the list.&lt;br /&gt;
&lt;br /&gt;
== Setting up a Micro AI ==&lt;br /&gt;
&lt;br /&gt;
Setting up a Micro AI requires only one simple step:&lt;br /&gt;
==== Activating and configuring (or deleting) a Micro AI ====&lt;br /&gt;
&lt;br /&gt;
Micro AIs are activated, deleted and configured using the [micro_ai] tag.  This tag needs to be placed in [[ActionWML]], that is, in an event, a menu option or the like.  As an example, the following code activates the healer_support Micro AI in its default configuration for Side 2 from the beginning of the scenario:&lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
 &lt;br /&gt;
         # Configure the healer support micro AI&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
'''Keys required for all Micro AIs:'''&lt;br /&gt;
All [micro_ai] tags must contain the following three required keys:&lt;br /&gt;
* '''side''': The side for which the Micro AI is to be added, changed or deleted.  This has to be an individual side number, lists of sides are not supported.&lt;br /&gt;
* '''ai_type''': The type of Micro AI to be added, changed or deleted.  See the following sections for allowed values.&lt;br /&gt;
* '''action''' (string): The action to take concerning the Micro AI.  The following values are allowed: &lt;br /&gt;
** ''add'': Add a Micro AI to a side.&lt;br /&gt;
** ''change'': Change the configuration of an existing Micro AI.  Note that this does not only change the specific parameters provided in the tag, but it replaces the entire existing Micro AI by a new version with the new (and only the new) configuration.  It is therefore equivalent to using first the ''delete'' and then the ''add'' action.&lt;br /&gt;
** ''delete'': Delete an existing Micro AI from a side.&lt;br /&gt;
&lt;br /&gt;
'''Keys required under certain circumstances:'''&lt;br /&gt;
* '''ca_id''': (string) If several MAIs of the same type are used for the same side and one or several of these are to be changed or deleted during a scenario, this key needs to be included so that the [micro_ai] tag knows which of them needs to be changed/deleted.  The same is also true if a Micro AI is changed more than once during a scenario.  We therefore '''strongly recommend always setting ''ca_id''''' for MAIs which are, or might be, changed or deleted during a scenario.&lt;br /&gt;
** '''Note:''' The id(s)/name(s) actually assigned to the CA(s) of the MAI by the engine might be different from the parameter passed here.  It is, however, sufficient to use this ca_id with the [micro_ai] tag add/delete/change actions to identify the MAI uniquely.  (In other words: you don't have to worry about this if you always use the [micro_ai] tag, but might have to figure out the actual name(s) of the CA(s) if you want to delete or change them manually.)&lt;br /&gt;
* Other keys may also be required depending on the Micro AI.  In additional, a number of optional keys may be available to configure the AI behavior.  See the sections on the individual Micro AIs for details.&lt;br /&gt;
&lt;br /&gt;
'''Required keys for deleting a Micro AI:'''&lt;br /&gt;
&lt;br /&gt;
Only a subset of the keys required for setting up a Micro AI are needed when deleting it.  '''side''', '''ai_type''' and '''action'''=delete always need to be provided.  '''ca_id''' needs to be given if it was provided when adding the Micro AI (see above for when this is needed).&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* The ''add'' and ''change'' actions ignore all keys that do not apply to the respective Micro AI type.  The ''delete'' action ignores all keys other than the required keys listed above.&lt;br /&gt;
&lt;br /&gt;
* Some of the Micro AIs use and/or modify default AI components (candidate actions or aspects) when they are added, or reset these components to their default values when they are deleted.  Thus, if you have modified the AI yourself using the default CAs or aspects, some of these modifications might be affected, or might be interfering with the Micro AI.  The sections below indicate whether a Micro AI modifies any default AI components.&lt;br /&gt;
&lt;br /&gt;
===Combining Different Micro AI Types for the Same Side===&lt;br /&gt;
&lt;br /&gt;
No extra steps need to be taken when combining different Micro AIs on the same side.  Simply set up the [micro_ai] tags for all AIs you want to use.&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* While it is ''technically'' possible to combine any number of Micro AIs, this does not mean that all Micro AIs work together in practice.  Some are not compatible because they control units in contradictory ways.  &lt;br /&gt;
* Not all combinations that ''should'' work have been tested yet.  If you find a combination that you believe should work but doesn't, [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 please let us know] and we will see if that can be fixed.&lt;br /&gt;
* The candidate action score almost certainly has to be set (using the ca_score= key) for one or several of the MAIs if several of them are used on the same side.&lt;br /&gt;
&lt;br /&gt;
== Animals Micro AIs ==&lt;br /&gt;
=== General Comments on the Animal Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Animals Micro AIs are meant to simulate the behaviors of certain types of animals.  They are, however, set up so that they can be used with arbitrary unit types and might be applicable in quite diverse situations.  The AIs nevertheless remain named after the animals for which they were written originally, so that they are (to some extent) descriptive of their behavior.&lt;br /&gt;
&lt;br /&gt;
Brief descriptions of the AI behaviors are given in the following subsections.  To get a better feeling for their behavior, check out the &amp;quot;Animals&amp;quot;, &amp;quot;Swarm&amp;quot;, &amp;quot;Wolves&amp;quot; and &amp;quot;Dragon&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  Note that some of the animal AIs are designed for animal types that do not exist in mainline.  In the test scenario, other animal types are therefore substituted for them.&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Wolves (ai_type=wolves) ===&lt;br /&gt;
&lt;br /&gt;
The Wolves Micro AI organizes all &amp;quot;wolf&amp;quot; units (predators) of the side to hunt in a single pack.  They actively chase after the closest prey and try to corner it (not always super successfully), but are easily distracted by other enemy units coming into attack range, to the point where the pack might split up.  The wolves can, however, be told to attack only prey units as well as to try to avoid other types of units (such as larger predators).  When no prey is left, the wolves wander randomly.&lt;br /&gt;
&lt;br /&gt;
This AI works best for a small number of predators (2 - 5) and sparse prey.  Also, the predators should be put on the map in a pack, that is, reasonably close to each other.  For a demonstration of the Wolves AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Note that another wolves AI exists, called [[Micro_AIs#Animals_AI:_Multi-pack_Wolves_.28ai_type.3Dwolves_multipacks.29|Multipack Wolves]], which distributes the wolves of a side into several packs, rather than all of them into one single pack as it is done here.&lt;br /&gt;
&lt;br /&gt;
Enable the Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the predator units.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the prey units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_only_prey'''=no: In the default configuration, the wolves attack all enemy units in attack range.  If this key is set to 'yes', they only attack prey units.&lt;br /&gt;
* '''avoid_type''': Comma-separated list of unit types (on enemy sides) which the wolves try to avoid.  They try to stay out of these units' way as much as possible when moving around (except when they are moving in to attack another enemy unit) and never attack them.&lt;br /&gt;
* '''ca_score'''=90000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 90,000, which is lower than the default combat CA, but higher than most other default CAs which move units.  It can be changed by setting this parameter but in most cases this will not make sense.  Note that the Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Notes on default (RCA) AI aspects:''' &lt;br /&gt;
* If ''attack_only_prey'' or ''avoid_type'' are set, the Wolves Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* Attacks by the wolves are left to the default (RCA) AI, meaning that the value of ''aggression'' has an influence on the behavior.&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI is taken into account. Note: as attacks by the wolves are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=6&lt;br /&gt;
             ai_type=wolves&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Wolf&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Vampire Bat&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             avoid_type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Multi-pack Wolves (ai_type=wolves_multipacks) ===&lt;br /&gt;
&lt;br /&gt;
The Multi-pack Wolves Micro AI is different from the [[Micro_AIs#Animals_AI:_Wolves_.28ai_type.3Dwolves.29|Wolves Micro AI]] in that there can be an arbitrary number of wolf packs, with the pack size being a free parameter.  At the beginning of the scenario, close wolves are grouped into packs in a semi-methodical way.  If the wolves are put on the map in distinct groups of close wolves of the correct number, they will be joined into packs matching these groups.  If, on the other hand, they are spread out all over the map, the method of assigning them to packs is semi-random.&lt;br /&gt;
&lt;br /&gt;
Wolves of the same pack begin by joining each other on the map.  After that, they stay together until only one wolf is left, which then tries to join up with an incomplete pack or with other single wolves.  Individual wolves entering the map during the scenario behave in that way as well.&lt;br /&gt;
&lt;br /&gt;
A second difference to the other Wolves AI is that wolves do not actively hunt here.  For the most part they just wander (often long distance).  However, the pack ferociously (and without regard for its own health) attacks any enemy units that come into range, as long as that does not mean separating the pack by more than a few hexes.  Staying together, or joining with a new wolf assigned to the pack, is the only thing that takes priority over satisfying the wolves' thirst for blood.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Multi-pack Wolves AI behavior, check out the &amp;quot;Wolves&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Multi-pack Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves_multipacks&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Multi-pack Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''pack_size'''=3: (integer) The size of the packs.&lt;br /&gt;
* '''show_pack_number'''=no: If set to &amp;quot;yes&amp;quot;, the wolves' pack numbers will be shown below them.  Note that this means that the pack number of a wolf killed in an attack will remain as a label on the map.  &lt;br /&gt;
* '''type'''=Wolf: Comma-separated list of the unit types which the AI controls. The default value is &amp;quot;Wolf&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves_multipacks [micro_ai] tag usage from the &amp;quot;Wolves&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=wolves_multipacks&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             show_pack_number=yes&lt;br /&gt;
             pack_size=4&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Big Animals (ai_type=big_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Big Animals Micro AI is a simulation of large predators that wander and hunt alone, such as Bears, Giant Spiders or Yetis.  For the most part, these just wander on the terrain that's been defined for them, but they attack enemy units if those happen to come into range.  The AI can be set up so that the Big Animals stay out of the way of other units (such as other large predators), in which case they only attack those if cornered or if they run into them accidentally (such as ambushers).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Big Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Big Animals Micro AI by using&lt;br /&gt;
 ai_type=big_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the animals to which this AI is applied.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': The big animals often go for long distance (multi-turn) journeys.  This tag is a [[StandardLocationFilter|Standard Location Filter]] describing the terrain from which the next goal is chosen.  The default is all terrain on the map. &lt;br /&gt;
* '''[filter_location_wander]''': The terrain on which the big animals will end their moves. This is a [[StandardLocationFilter|Standard Location Filter]].  The default is all terrain on the map.&lt;br /&gt;
* '''[avoid_unit]''': A [[StandardUnitFilter|Standard Unit Filter]] describing all enemy units that the big animals will avoid.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
Here's an example of big_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=big_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Ghast&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [avoid_unit]&lt;br /&gt;
                 type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
             [/avoid_unit]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=1-40,1-18&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Forest Animals (ai_type=forest_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Forest Animals Micro AI simulates a number of different animal behaviors that one might find in a forest, namely animals behaving like deer, rabbits and families of tuskers.  In the following, we will refer to those animals by these types, even though the actual unit types can be selected freely.  They behave as follows:&lt;br /&gt;
* Each deer wanders randomly on (selectable) terrain, except when enemies get in its (the deer's) maximum movement range, in which case it flees to the farthest point it can reach.&lt;br /&gt;
* Tuskers exhibit the same behavior as deer, except when an enemy is next to one of the tusklets.  This enemy is attacked unconditionally by the tuskers trying to protect their young.&lt;br /&gt;
* Tusklets blindly follow the closest adult tusker, except when there is no tusker left, in which case they behave the same as deer.&lt;br /&gt;
* Rabbits also wander randomly, but in addition disappear into their holes (if any are within reach) when enemies are close.  They reappear out of their holes at the beginning of the turn, if it is safe.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Forest Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Forest Animals Micro AI by using&lt;br /&gt;
 ai_type=forest_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Forest Animals AI consists of 4 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-3&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''rabbit_type, tusker_type, tusklet_type, deer_type''': Comma-separated lists of the unit types behaving as the respective animals.  If any of these parameters are not set, those types of animal behavior are skipped by the AI.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain on which the forest animals wander when they are not threatened by enemies.&lt;br /&gt;
* '''rabbit_number'''=6: (integer)  The number of rabbits for the AI to have on the map simultaneously.  The AI puts the missing number of rabbits onto the map at the beginning of the AI's turn (when the rabbits come out of their holes).&lt;br /&gt;
* '''rabbit_enemy_distance'''=3: (number)  Rabbits won't spawn in holes that aren't more than this distance away from all enemies.&lt;br /&gt;
* '''rabbit_hole_img''': Rabbit holes are marked by items on the map.  If this parameter is set, it must be the file name of the item image.  If it is not set, all items on the map are treated as rabbit holes.&lt;br /&gt;
&lt;br /&gt;
Here's an example of forest_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=forest_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             deer_type=Vampire Bat&lt;br /&gt;
             rabbit_type=Giant Rat&lt;br /&gt;
             tusker_type=Ogre&lt;br /&gt;
             tusklet_type=Young Ogre&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=*^F*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Herd and Herders (ai_type=herding) ===&lt;br /&gt;
&lt;br /&gt;
The Herding Micro AI sets up an area on the map where a number of herder units (such as sheep dog) control and protect a herd (such as sheep).  For convenience, we simply refer to them as sheep dogs and sheep in the following, even though arbitrary unit types can be selected for either.&lt;br /&gt;
&lt;br /&gt;
Sheep dogs try to keep their sheep safe.  This involves keeping them inside the herding area, positioning themselves in between the sheep and approaching enemies, and attacking the enemies if those get too close.  If no active herding or protecting move is needed, the dogs go to a random location on the perimeter of the herding area.&lt;br /&gt;
&lt;br /&gt;
Sheep wander aimlessly except when a sheep dog is next to them, in which case they run away from the dog.  The dogs exploit this by positioning themselves on the outside of the sheep, if possible.  Sheep also run away from approaching enemies.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Herding AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Herding Micro AI by using&lt;br /&gt;
 ai_type=herding&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herder units (e.g. dogs).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herd units (e.g. sheep).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain forming the perimeter of the herding area.  In other words, this is the path the herders (dogs) use to patrol around the outside of the herding area.  This must be a set of coordinates ''completely enclosing'' the herding area.  Here's an example of setting up a rectangular perimeter:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         x=10-20,10-20,10,  ,20&lt;br /&gt;
         y=11,   21,   11-21,11-21&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
Another possibility is by defining the path by certain terrain on the map (as it is done in the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]), in which case the filter can simply look like this:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         terrain=Rb&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
* '''herd_x,herd_y''': The coordinate of one hex (''any'' one hex) inside the herding area.  This is needed to define which part of the map is on the ''inside'' of the herding area perimeter defined by [herding_perimeter], and which is the ''outside''. {{DevFeature1.15|0}} '''herd_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''attention_distance'''=8: (integer)  If an enemy is within this distance of a herd unit, but not as close as ''attack_distance'', the herders try to position themselves in between the enemy and the herd unit.&lt;br /&gt;
* '''attack_distance'''=4: (integer)  If an enemy is within this distance of a herd animal, the herders attack this enemy.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Herding AI consists of 6 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-5&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Herding [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=herding&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Footpad&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Troll Whelp&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             herd_x,herd_y=32,28&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=Rb&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Hunter (ai_type=hunter) ===&lt;br /&gt;
&lt;br /&gt;
The Hunter Micro AI leads a unit (or units) through hunt-and-rest cycles.  The units wander in a prescribed area of the map, their hunting ground, until an enemy unit comes within range.  Note that the units do not actively chase down enemies, but wander randomly through the hunting ground until an enemy is encountered.&lt;br /&gt;
&lt;br /&gt;
If enemies are within range of a hunter unit, the hunter attacks the weakest of them.  If a kill is made, it then retreats to its rest location, where it stays for a certain number of turns and until fully healed (whatever happens later).&lt;br /&gt;
&lt;br /&gt;
A few more details:&lt;br /&gt;
* If the hunter's way home is entirely blocked on the return (so that there is no possible path), the normal RCA AI takes over its behavior.&lt;br /&gt;
* However, if the way is blocked by an enemy unit occupying the rest hex, that enemy will be attacked unconditionally.&lt;br /&gt;
* A kill only makes the hunter go home when it is the attacker, not as defender.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Hunter AI behavior, check out the &amp;quot;Dragon&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]. &lt;br /&gt;
&lt;br /&gt;
Enable the Hunter Micro AI by using&lt;br /&gt;
 ai_type=hunter&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''home_x,home_y''': The coordinates of the hex to which the hunter returns and rests at after a kill. {{DevFeature1.15|0}} '''home_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the unit's hunting ground.  If this tag is not provided, the hunter uses all of the map.&lt;br /&gt;
* '''rest_turns'''=3: (integer)  The number of turns the AI stays at home_x,home_y after a kill.  It will not leave its home until this number of turns have passed ''and'' it is fully healed.&lt;br /&gt;
* '''show_messages'''=no: (boolean) If set to yes, the hunter announces whenever its behavior changes to the next phase.  Note that this is meant as debug output for the scenario author.  The messages are not translated and are not included in replays (and are only visible to the host in an MP game).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a hunter [micro_ai] tag usage from the &amp;quot;Dragon&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hunter&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Rowck&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=5-30,1-15&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             home_x,home_y=3,15&lt;br /&gt;
             rest_turns=2&lt;br /&gt;
 &lt;br /&gt;
             show_messages=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Swarm (ai_type=swarm) ===&lt;br /&gt;
&lt;br /&gt;
The Swarm Micro AI uses some very simply algorithms to simulate animal swarm behavior.  Without adjacent enemies, they simply do a random move, trying to stay together and at a certain distance from enemies.  However, if an enemy unit is close to any bat, the swarm scatters.  This is particular fun to watch when one places an enemy unit in the middle of the swarm.  After being scattered, the swarm members slowly rejoin, but not in a very organized way.  Sub-swarms or individual bats might roam around for quite some time before they find their way back.  It is also possible that individual bats (or small groups) split off from the larger swarm at times.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Swarm AI behavior, check out the &amp;quot;Swarm&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Swarm Micro AI by using&lt;br /&gt;
 ai_type=swarm&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Swarm AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_distance'''=5: (int) The minimum distance kept between units of the swarm and enemies when the swarm moves as a whole.&lt;br /&gt;
* '''scatter_distance'''=3: (int) Enemies within &amp;quot;scatter_distance&amp;quot; hexes of any swarm unit cause the swarm to scatter, by each unit trying to maximize its individual distance from the enemy.&lt;br /&gt;
* '''vision_distance'''=12: (int) Only units within this distance follow the overall swarm motion (either away from an enemy or of the swarm as a whole).  The smaller this value is set, the less likely the swarm is to stay together or rejoin.  This parameter is meant to simulate how far an individual member of the swarm can see, meaning that the swarm is &amp;quot;out of sight&amp;quot; (and the unit will not be able to follow it) if it is more than this distance away.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Swarm [micro_ai] tag usage from the &amp;quot;Swarm&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=swarm&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Assassin Squad Micro AI (ai_type=assassin) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|6}}&lt;br /&gt;
&lt;br /&gt;
This Micro AI controls one or several assassin units which try to circle around enemy units with the goal of hitting a target unit without being distracted or impeded by other enemies.  The units will always attack the target if such attacks are possible, otherwise they will follow the path of least resistance toward it. Notes:&lt;br /&gt;
&lt;br /&gt;
* The main priority for the AI is to find paths that avoid enemy units. However, if no such path can be found, the assassins will still advance toward the target, taking the path exposing the units to the least potential damage from enemies.&lt;br /&gt;
&lt;br /&gt;
* The units will not necessarily follow the same path. The best path is chosen individually for each unit.&lt;br /&gt;
&lt;br /&gt;
* While it is not a requirement for the AI to work, using ambusher or skirmisher units, or units with the quick trait, makes this AI more lethal in many cases.&lt;br /&gt;
&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded, it does not work otherwise. That means that '''ca_score''' is not a valid key for the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=assassin&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Assassin Squad specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the assassin units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the target unit.&lt;br /&gt;
** It is important to realize that the AI currently only works correctly with a single target unit. It is thus the responsibility of the scenario designer to make sure that this filter matches an individual enemy unit. If several enemy units are to be targeted in the same scenario, several instances of the Assassin Micro AI can be used.&lt;br /&gt;
** The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[prefer]''': A [[StandardLocationFilter|Standard Location Filter]] selecting locations on the map for the assassins to prefer when making their way toward the target unit. This could be used, for example, to make units with the ambush ability move (mostly) through forest or to have the assassins circle around the, say, northern part of the map rather than the south if that is desirable for scenario design purposes.&lt;br /&gt;
** As with avoiding enemies, the units will still move across non-preferred terrain if that is either the only option or ''much'' shorter than preferred terrain&lt;br /&gt;
** Attacking, once the target unit is in range, is not influenced by the [prefer] SLF.&lt;br /&gt;
&lt;br /&gt;
Here is an example of an Assassin Squad [micro_ai] tag usage:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=assassin&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Assassin,Master at Arms,Duelist&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             [prefer] # Circle around northern part of map&lt;br /&gt;
                 x=1-9, 10-99&lt;br /&gt;
                 y=1-37, 1-17&lt;br /&gt;
             [/prefer]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Bottleneck Defense Micro AI (ai_type=bottleneck_defense) ==&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense Micro AI lets you define a location on the map where the AI can take a defensive stance behind a narrow passage (bottleneck).  Units on the front line are backed up by healers and/or units with the leadership ability.  Injured units are moved to the back and replaced by uninjured (or less injured) units.  The units on the front line only attack when it is safe (no retaliation) or when there is a high chance that they will make a kill or level up.  Using this Micro AI only makes sense if there is no way for the enemy sides to move around the bottleneck and attack the AI's units from behind.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Bottleneck&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Bottleneck Defense Micro AI by using&lt;br /&gt;
 ai_type=bottleneck_defense&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Bottleneck Defense specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense AI requires two sets of coordinates that define where it should be taking up its defensive stance, and from which side the enemy attacks at that location.&lt;br /&gt;
&lt;br /&gt;
* '''x,y''': Comma separated lists of the hexes on the front line, where strong units are placed.  All hexes on which the AI makes contact with (can be attacked by) the enemy need to be included here.  Units are placed on them in the order in which they are listed here. {{DevFeature1.15|0}} '''location_id''' can be used instead with named/special locations.&lt;br /&gt;
* '''enemy_x,enemy_y''': Comma separated list of the hexes from which the enemy can attack the AI front line units.  This is needed for the AI to know on which side of the front line support units (healers etc.) need to be placed.  In many cases, it is sufficient to provide only one of the enemy front line hexes, but there are geometries for which that does not work.  It is therefore safer to list all enemy front line hexes here. {{DevFeature1.15|0}} '''enemy_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
By default, the AI places healers and units with the leadership ability both on the front line itself (if they are the strongest units available) and on hexes adjacent to and behind the line.  If different placement is desired, these locations can be overridden with the following keys.&lt;br /&gt;
&lt;br /&gt;
* '''active_side_leader'''=no: (boolean)  If set to 'yes', the side leader participates in the bottleneck defense action after the side's gold has been spent.  If set to 'no' (default), the leader follows default AI behavior (sitting on the keep and recruiting, for the most part).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Bottleneck Defense AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.15|3}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the units.  Only AI units matching this filter participate in the bottleneck defense.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''healer_x,healer_y''': Comma separated list of hexes where healers are placed.  This can be used to keep healers from the front line (or to take up only certain positions on the front line), and/or to override the default healing positions behind the line.  Healers are placed on them in the order in which the hexes are listed, with the exception that hexes on the line always take priority over hexes behind the line. {{DevFeature1.15|0}} '''healer_loc''' can be used instead with named/special locations.&lt;br /&gt;
* '''leadership_x,leadership_y''': Same as ''healer_x,healer_y'', but for units with the leadership ability. {{DevFeature1.15|0}} '''leadership_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
As an example, in the &amp;quot;Bottleneck&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]], there are three front line hexes, two of which are on hill terrain, while the third is a road with a lower defense rating.  The healer and side leader are supposed to participate in combat because they are strong units, but only from the hill hexes to keep them safer.  This is accomplished by using the following keys settings (check out the scenario to see which hex is which):&lt;br /&gt;
 [micro_ai]&lt;br /&gt;
     side=1&lt;br /&gt;
     ai_type=bottleneck_defense&lt;br /&gt;
     action=add&lt;br /&gt;
 &lt;br /&gt;
     x=14,14,14&lt;br /&gt;
     y= 7, 9, 8&lt;br /&gt;
     enemy_x=13,13&lt;br /&gt;
     enemy_y= 8, 9&lt;br /&gt;
 &lt;br /&gt;
     healer_x=14,14,15,15&lt;br /&gt;
     healer_y= 7, 9, 8, 9&lt;br /&gt;
     leadership_x=14,14,15,15&lt;br /&gt;
     leadership_y= 7, 9, 9 ,8&lt;br /&gt;
     active_side_leader=yes&lt;br /&gt;
 [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Fast Micro AI (ai_type=fast_ai) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|0}}&lt;br /&gt;
&lt;br /&gt;
This AI is meant as a fall-back AI for scenarios where there are so many units that attacks and moves done by the default AI are slow with long delays before each move.  It replaces the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions (CAs).  It should be obvious that in order to achieve the faster speed, compromises concerning the quality of the moves have to be made.&lt;br /&gt;
&lt;br /&gt;
Enable the Fast Micro AI by using&lt;br /&gt;
 ai_type=fast_ai&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Combat candidate action:'''&lt;br /&gt;
* Attackers are considered individually one at a time.  The best attack found for a unit is executed without checking whether a better attack by another unit might be possible.&lt;br /&gt;
* The default order in which attackers are considered is by decreasing hitpoints, starting with the strongest unit (in terms of HP).  This can be modified with the optional keys below.&lt;br /&gt;
* By default, if weaker units block attack hexes for a stronger unit, they do not move out of the way (this is the same as for the default AI).  If a hex becomes available after an attack (e.g. due to a weaker unit dying in the attack), all attacks that were possible ''before'' this happened are finished first.  Only after that are attacks by the stronger unit reconsidered.&lt;br /&gt;
* If include_occupied_attack_hexes=yes is set, weaker units move out of the way for stronger units if possible.  This can, however slow down the AI depending on the situation on the map.  Use with caution.&lt;br /&gt;
* {{DevFeature1.13|2}} Previous to this version, attacks by the leader were bunched in with the attacks by the other units.  Now leader attacks are treated separately and contain an extra layer of analysis to determine whether the attack position is &amp;quot;safe&amp;quot; (in an approximate way).  Currently, this is done with two simple considerations:&lt;br /&gt;
** Count all enemy units that can reach the leader's attack position.  If there are more than given by optional parameter 'leader_attack_max_units' (default value 3), do not attack.&lt;br /&gt;
** Add up the hitpoints of all enemies that can reach the leader's attack position.  If this value, multiplied by optional parameter 'leader_weight'(default value 2), is larger than the leaders hitpoints, do not attack.&lt;br /&gt;
** Two optional parameters, 'threatened_leader_fights' and 'leader_additional_threat', can also be used to let the leader attack when the threat at the attack hex is comparable to that at the leader's current position.&lt;br /&gt;
** In order to keep this AI as fast as possible, leader attacks are done by a separate candidate action that is only evaluated when all other CAs are done.&lt;br /&gt;
&lt;br /&gt;
'''Move-to-targets, villages and retreat candidate actions:'''&lt;br /&gt;
* The move-to-targets, villages and retreat candidate actions of the default AI are removed.  Move-to-targets and villages are taken over by the Fast Micro AI, whereas retreat is simply not done.&lt;br /&gt;
* This CA moves units toward goals after attacks are done.  Goals are unowned (if the AI side has a leader only) and enemy-owned villages and enemy leaders.&lt;br /&gt;
* For each goal, units are pre-sorted in order of the fewest moves needed to cover the ''distance'' toward the goal (that is, as if move cost on each hex toward the goal were 1).  Units are then considered sequentially and if a unit can get there &amp;quot;efficiently&amp;quot;, its move is executed without considering other units.&lt;br /&gt;
** A move is considered efficient if the move cost to get to the hex is not more than 'move_cost_factor' larger than the distance to the hex.  'move_cost_factor' is an optional key that defaults to 2.  It can be set to values of 1.1 or larger (see below).&lt;br /&gt;
** If the move for the current unit is not found to be efficient, the AI considers the next unit instead.  If no unit can do an efficient move toward the goal, the best (fastest) of the inefficient moves is selected instead.&lt;br /&gt;
&lt;br /&gt;
* Village goals:&lt;br /&gt;
** Village goals are considered before enemy leader goals so that the AI preferentially chooses scouts (or otherwise fast units) for them.&lt;br /&gt;
** The number of units sent toward villages depends on the value of the default AI aspect village_value.  It is set to village_value/4 times the number of units the AI has left to move.  Thus, by default (village_value=1.0) the AI sends a quarter of its units toward villages, and the rest toward enemy leaders.  This means that the meaningful range of values for village_value is 0..4.&lt;br /&gt;
** Only one unit is moved toward each village.  This consideration also overrides the number of units to move toward villages defined by village_value, topping it at the number of unowned/enemy-owned villages on the map.&lt;br /&gt;
** Setting village_value=0 disables moving units toward villages altogether.  That is, not only are no units chosen, but the village targeting phase of the AI is entirely disabled with no evaluation time used for it.  Note that, unlike with the default AI, this also means that no villages within range of an AI unit are grabbed.&lt;br /&gt;
** The order in which villages are chosen as goals is some combination of them being close to the AI side's leader and being spread out over the map.&lt;br /&gt;
&lt;br /&gt;
* Enemy leader goals:&lt;br /&gt;
** The order of enemy leaders goals is set according to their distance from the AI leader with the closest enemy leader being targeted first (if the AI has a leader, otherwise their order is random).  One unit is sent toward each enemy leader in this order, after which the AI starts with the first enemy leader again.  This is repeated until all units have moved.&lt;br /&gt;
&lt;br /&gt;
* Guardians (units with ai_special=guardian set) are ignored by the fast move-to-targets CA.&lt;br /&gt;
&lt;br /&gt;
'''Tips for speeding up/modifying the Fast Micro AI in specific scenarios:'''&lt;br /&gt;
&lt;br /&gt;
A number of optional keys and default AI aspects can be used to modify the behavior of the Fast Micro AI.  The MAI is set up so that it is a reasonable compromise between speed and quality of the moves in most situations.  However, this might not be ideal for all scenarios here are some things that can be done to speed up the AI (or to get better behavior in some situations by doing the opposite):&lt;br /&gt;
* Use large values for the default AI aspect aggression.  The largest delays before attacks happen (in some circumstances) when the Fast MAI has to consider lots of units with attacks left but decides not to attack with them.  See below for a description of how aggression is used by the fast AI.&lt;br /&gt;
* Use large values of move_cost_factor (see above for description of what this does).  If you want to emphasize units that can move across terrain easily (e.g. bats) to be chosen for village grabbing, you might want to use low values of move_cost_factor, but this might slow down the AI.  The default value should work reasonably well in most standard situations, but modifying the value might work better on specific maps.&lt;br /&gt;
* Don't use dungeon_mode=yes unless absolutely necessary, even in most dungeon crawls.  Test the AI without this setting first and only use it if it is the only way to solve the problem.  Often, a minor tweak to the map will have the same effect, without the speed penalty.&lt;br /&gt;
* Use include_occupied_attack_hexes=yes with caution.  It might (or might not) slow down the attack CA significantly, depending on the scenario.&lt;br /&gt;
* Set village_value=0, which makes the AI not consider villages as goals.  This has not been found to make a large difference on the maps tested so far, but it might have an effect on maps with a very large number of villages.&lt;br /&gt;
* Use attack_hidden_enemies=yes on maps where there are no hidden enemies. Excluding hidden enemies from the attack calculation requires an additional filter to be evaluated that takes extra time. In most cases this will not result in a large computation time saving.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_hidden_enemies'''=no: (boolean)  By default, hidden enemies are excluded from the attack calculation.  Setting this to 'yes' makes the AI also consider those enemies as attack targets.&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.13|2}} A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  As with the default AI, units will not step on hexes defined by the [avoid] tag.  In addition, no goals will be chosen that lie on those hexes.  The one minor exception is that a unit stepping out of the way for another unit might temporarily enter the avoided zone.&lt;br /&gt;
* '''dungeon_mode'''=no: (boolean)  As much as possible, the Fast Micro AI works with distances between hexes rather than path finding.  This works pretty well on open maps and even most dungeon crawls (and similar scenarios), but it might result in some units getting temporarily (and in rare cases permanently) stuck on maps with convoluted labyrinths.  If that is the case, setting dungeon_mode=yes triggers more path finding that avoids such situations (but as a result is somewhat slower).&lt;br /&gt;
* '''include_occupied_attack_hexes'''=no: (boolean) If set to 'yes', the AI moves weaker units out of the way for a stronger attacker.  This might lead to better attacks but is slower, so don't use if speed is essential.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the attackers.  Only AI units matching this filter will attack, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes the side= key set to the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the enemies.  Only enemy units matching this filter will be attacked, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''leader_weight'''=2: (number &amp;gt;= 0) {{DevFeature1.13|2}} A factor specifying how much more valuable the AI considers side leaders than non-leader units in the attack evaluation. This is also used to determine whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''leader_additional_threat'''=1: (number &amp;gt; 0) {{DevFeature1.13|2}} If threatened_leader_fights=yes, the threat at the attack position may be larger by this factor for the leader to leave the keep for an attack.&lt;br /&gt;
* '''leader_attack_max_units'''=3: (number &amp;gt;= 0) {{DevFeature1.13|2}} A number specifying how many enemy units within reach are acceptable when determining whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''move_cost_factor'''=2: (number &amp;gt;= 1.1)  The factor by which the move cost to a goal hex may be larger than the distance to the goal hex for a move to be considered &amp;quot;efficient&amp;quot;.  See description above.  1.1 is used if this is set to values smaller than 1.1.  Smaller values might lead to better behavior, while larger values might speed up the AI.&lt;br /&gt;
* '''weak_units_first'''=no: (boolean) If set to 'yes', inverts the order in which units are considered by the combat CA.  Attacks are then done in order of increasing hitpoints of the attackers.&lt;br /&gt;
* '''skip_combat_ca'''=no: (boolean) If set to 'yes', the fast combat CA is not used.  The default AI's high_xp_attack and combat CAs remains in place in this case.&lt;br /&gt;
* '''skip_move_ca'''=no: (boolean) If set to 'yes', the fast move-to-targets CA is not used.  The default AI's retreat, villages and move-to-targets CAs remains in place in this case.&lt;br /&gt;
* '''threatened_leader_fights'''=no: (boolean) {{DevFeature1.13|2}} By default, the AI leader will not attack if the threat at the attack position is too high.  If this key is set to 'yes', the leader will attack if the threat there is not larger than the threat at his current position multiplied by the value of 'leader_additional_threat'. This is meant as an option for the endgame: when the AI leader is under attack, he might as well go down fighting.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''aggression''': The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to set how aggressive the AI should attack.  It works in roughly the same way as the default AI aspect:&lt;br /&gt;
** The value of (1 - aggression) determines how much the AI values its own units compared to the enemy's units.  Thus, aggression=0 means that attacks are only done if the AI expects to deal (roughly) the same amount of damage as it will receive.  aggression=1 means that expected damage to the AI's units is not considered, only the expected damage to enemy units matters.&lt;br /&gt;
** Damage is calculated as a combination of hitpoints and chance to die, in &amp;quot;gold equivalent units&amp;quot;.&lt;br /&gt;
** Unlike the default AI, aggression is set to 1 for values larger than 1.  Allowed values are thus anything from -infinity to +infinity, although only -infinity to +1.0 make sense.&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.13|2}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
*'''attacks aspect''':  {{DevFeature1.13|2}} The standard RCA AI [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] used to filter own and enemy units for attacks is now also taken into account as long as neither [filter] nor [filter_second] (see above under 'optional keys') are defined in the [micro_ai] tag.&lt;br /&gt;
*'''village_value=1''': (number ranging from 0 to 4) The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to define how much villages should be targeted by the move-to-targets CA.  In the Fast MAI, this defines which fraction of the AI units are moved toward village goals (see above for details).  If set to 0, villages are ignored as goals.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded to the same values as those of the respective [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|default candidate actions]].  ca_score is therefore not a valid optional key.&lt;br /&gt;
* The fast Micro AI modifies (removes) the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions.&lt;br /&gt;
&lt;br /&gt;
Enabling the Fast Micro AI in its default configuration is as simple as this usage example from the &amp;quot;Fast AI&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=fast_ai&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Generalized Goto Micro AI (ai_type=goto) ==&lt;br /&gt;
&lt;br /&gt;
The Goto Micro AI provides a highly configurable method of sending a unit (or units) to a location or set of locations.  The units to be moved are defined using a [[StandardUnitFilter|Standard Unit Filter]] (SUF) [alternatively, all side units are used if the SUF is not provided), while the goto locations are given in a [[StandardLocationFilter|Standard Location Filter]] (SLF).&lt;br /&gt;
&lt;br /&gt;
By default, each unit matching the SUF will move toward the closest goal (in terms of turns needed to get there) matching the SLF via the fastest route given the current situation on the map (locations of enemy units etc.), but this behavior can be influenced by the optional keys listed below.  Note that this means that a unit's goal might change from one turn to the next depending on the situation on the map.&lt;br /&gt;
&lt;br /&gt;
The behavior of the unit(s) once they reach their goal location(s) is configurable as well.  By default, they will stay there until the Goto Micro AI is deleted or changed.&lt;br /&gt;
&lt;br /&gt;
For demonstrations of several Goto Micro AI usages, check out the &amp;quot;Goto&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Goto Micro AI by using&lt;br /&gt;
 ai_type=goto&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Goto specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the possible goal locations for the move.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists). Note that the area defined by [avoid] is not entered at all, even if the end point of the move would lie outside the area again.&lt;br /&gt;
* '''avoid_enemies''': (number&amp;gt;0) By default, units controlled by the Goto Micro AI will not take enemy units into account for route finding, other than the enemies' ZoCs.  By contrast, if this key is set, they will try to avoid traversing hexes on which they can be attacked by enemies.  The larger the value of ''avoid_enemies'', the more the units will try to stay out of the enemies' way.  Notes:&lt;br /&gt;
** Units are willing to take a path that is 'avoid_enemies' movement points longer for each enemy unit that can be avoided by doing so for each hex along the path.  As an example, if avoid_enemies=1 and the shortest path crosses 2 hexes that can be reached by 3 enemies each, the unit is willing to take a path that is up to 6 hexes longer instead.  If avoid_enemies=10 in the same situation, the unit will take a path up to 60 hexes longer.&lt;br /&gt;
** Hexes adjacent to enemy units are always strongly avoided, independent of the value of avoid_enemies.&lt;br /&gt;
** avoid_enemies is ignored if 'use_straight_line=true' is set.&lt;br /&gt;
** Due to an issue with the path finding code, '''tunnels and teleports are ignored in Wesnoth 1.14 and before''' if 'avoid_enemies' is used. This has been fixed in Wesnoth 1.15.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units to be moved.  Defaults to all units of the side if not defined.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''ignore_enemy_at_goal'''=no and '''ignore_units'''=no: (boolean)  In the default configuration, the Goto MAI does not move units if the path to all goal hexes is blocked by enemies.  It is therefore possible to disable the Goto MAI by putting enemy units on all the goal hexes.  That can be changed if either of these two keys is set to 'yes'.&lt;br /&gt;
** 'ignore_enemy_at_goal=yes' makes the AI choose its goals while disregarding any enemy units at the goal hexes.  Even so, a stiff rating penalty is applied to goal hexes occupied by enemies, thus that unoccupied goal hexes are chosen unless all possible goals are occupied by enemies.  Note that this setting ''only'' ignores enemy units at the goal hexes themselves.  It is still possible to disable the AI by putting enemies all around the goals.&lt;br /&gt;
** 'ignore_units=yes' let's the AI ignore ''all'' enemy units when deciding which goal hex(es) to move toward.&lt;br /&gt;
** In both cases, enemies are only ignored for deciding which unit to move toward which goal hex.  For the actual move itself, all enemies and their ZoCs are considered as usual.&lt;br /&gt;
** Both options only take effect if neither avoid_enemies nor use_straight_line is set to 'yes', as neither of these consider enemies as blockers.&lt;br /&gt;
* '''release_unit_at_goal'''=no: (boolean) By default, a unit that reaches a goal location will stay there until the Goto Micro AI is deleted or changed (or until the conditions specified in the [goto_goals] SLF change).  If this parameter is set, a unit that has reached its goal will not be controlled by this Micro AI any more starting from the next turn.&lt;br /&gt;
* '''release_all_units_at_goal'''=no: (boolean) Same as ''release_unit_at_goal'', but this will deactivate the Goto Micro AI for all units once the first unit reaches a goal.&lt;br /&gt;
* '''unique_goals'''=no: (boolean)  If set to 'yes', each unit will be sent to a different goal location.  Note that the best goals are recalculated each turn, so a unit might choose a different goal this turn from what it was heading for during the previous turn if the situation on the map has changed.  Also note that if there are more units than goal locations, the left over units will not be handled by the Goto MAI but follow default behavior instead.&lt;br /&gt;
* '''use_straight_line'''=no: (boolean)  By default, units choose the route by which they will get to their goal locations in the smallest number of turns.  If this parameter is set to 'yes', a straight line route (or something pretty close to it at least) will be chosen instead, irrespective of whether this leads into a dead end, whether it is blocked by enemies, etc.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* Depending on the parameters used, the Goto Micro AI might require a lot of path finding to be done.  Thus, while it is possible to set up this AI to move many units to many different locations, a usage like that might require quite a long calculation time.&lt;br /&gt;
&lt;br /&gt;
===Goto Micro AI example usages:===&lt;br /&gt;
&lt;br /&gt;
Here are several examples of goto [micro_ai] tag usages from the &amp;quot;Goto&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
* Send a unit toward a castle (similar to the RCA AI's Goto Candidate Action, but avoiding dead ends and areas ZoCed by enemies)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
             ca_id=messenger1&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=messenger1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=6-7,2-4&lt;br /&gt;
                 terrain=C*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             ca_score=210001&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send all units of the side toward the southern border of the map&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=8&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                  y=33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             avoid_enemies=1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send units to the four corners of the map (with each unit going to a different corner).&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x=1,1,44,44&lt;br /&gt;
                 y=1,33,1,33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             unique_goals=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Set up a guardian unit that doesn't move while no enemy unit is within 8 hexes (ironically, the Goto Micro AI can be used to keep units in place, although using one of the Guardian Micro AIs might often be a better idea)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=guard1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 [filter]&lt;br /&gt;
                     id=guard1&lt;br /&gt;
                 [/filter]&lt;br /&gt;
                 [not]&lt;br /&gt;
                     [filter]&lt;br /&gt;
                         side=1&lt;br /&gt;
                     [/filter]&lt;br /&gt;
                     radius=8&lt;br /&gt;
                 [/not]&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Guardian Micro AIs ==&lt;br /&gt;
=== General Comments on the Guardian Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Guardian Micro AIs can be used to set up different kind of guardian behaviors for a unit or a set of units.  Four different types of guardians are available and are described below.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different guardian behaviors, check out the &amp;quot;Guardians&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
=== Return Guardian  (ai_type=return_guardian)===&lt;br /&gt;
&lt;br /&gt;
A 'return guardian' is a variation of the standard Wesnoth guardian. It has an assigned guard position (GP) to which it returns after attacks on approaching enemies:&lt;br /&gt;
* If at GP with no enemy in reach, do nothing.&lt;br /&gt;
* If at GP with an enemy in reach, attack. Note that the attack is done by the default AI, which might result in the guardian not attacking if the enemy is deemed too strong (this can be influenced by setting the default ''aggression'' value to a different value).&lt;br /&gt;
* If not at GP, return there, no matter whether an enemy is in reach or not.&lt;br /&gt;
* If enemies are blocking the way back to GP, do your best to move around them.&lt;br /&gt;
* If the guardian ends up next to an enemy on the way back, attack that enemy after the move.&lt;br /&gt;
&lt;br /&gt;
Enable the Return Guardian Micro AI by using&lt;br /&gt;
 ai_type=return_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''return_x,return_y''': The coordinate of the hex the unit returns to after each attack.  {{DevFeature1.15|0}} '''return_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=100010: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  This AI consist of 2 CAs, one with score &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt; the other with &amp;lt;tt&amp;gt;ca_score-20&amp;lt;/tt&amp;gt;.  The default value is set so that the first CA gets executed right before the default combat CA, and the other right after.  It usually will not make sense to change this value, but it is possible to do so for custom applications.  {{DevFeature1.13|6}}: This is now changed to 100100 and 99900 in order to avoid conflicts with the new high_XP_attacks candidate action.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a return guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=return_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=return1&lt;br /&gt;
             return_x,return_y=20,2&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
===Stationed Guardian (ai_type=stationed_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A 'stationed guardian' is another variation of the standard Wesnoth guardian with a somewhat more complex behavior than that of the 'return guardian'. Two positions are defined for it, a 'station' and a 'guarded location', as well as a distance. The behavior is as follows:&lt;br /&gt;
* If no enemy is within 'distance' of the guard's current position, do nothing.  This is independent of the guardian being at its station or not.&lt;br /&gt;
* Otherwise: If an enemy is within 'distance' of the guard, but not also within the same distance of the guarded location and the station (all of this simultaneously), move the guard in the direction of the station (or keep the guard in place if the station is unreachable).&lt;br /&gt;
* Otherwise:&lt;br /&gt;
** Pick the enemy unit that is closest to the guarded location.&lt;br /&gt;
** If we can reach it, pick the adjacent hex with the highest defense rating and attack from there.&lt;br /&gt;
** If not in reach, move toward this unit.&lt;br /&gt;
&lt;br /&gt;
Enable the Stationed Guardian Micro AI by using&lt;br /&gt;
 ai_type=stationed_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance parameter as explained above&lt;br /&gt;
* '''station_x,station_y''': The x and y position of the hex that serves as the guardians station. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''guard_x, guard_y''': The x and y position of the guarded location.  {{DevFeature1.13|0}}  These are now optional keys, defaulting to station_x,station_y if not provided. {{DevFeature1.15|0}} '''guard_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a stationed guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=stationed_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=stationed1&lt;br /&gt;
             distance=4&lt;br /&gt;
             station_x,station_y=2,14&lt;br /&gt;
             guard_x,guard_y=3,13&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Coward (ai_type=coward) ===&lt;br /&gt;
&lt;br /&gt;
Cowards are units that, like guardians, sit around doing nothing until an enemy comes into range.  Unlike guardians, however, they run away once enemies approach. Applications might be wild animals, unarmed civilians getting in the way of a battle, etc.  The coward macro can be called with two optional locations, 'seek' and 'avoid':&lt;br /&gt;
* If neither is given, the coward retreats to the position farthest away from the approaching enemies.&lt;br /&gt;
* If 'seek' is given, it preferentially goes toward that location (but getting away from enemies takes priority).&lt;br /&gt;
* If 'avoid' is given, it in addition tries to avoid that location (with both maximizing distance from enemies and going toward 'seek' taking priority).&lt;br /&gt;
* Both 'seek' and 'avoid' may consist of only one coordinate ('x' or 'y'), in which case not a single hex, but a line of hexes is sought or avoided.&lt;br /&gt;
&lt;br /&gt;
Enable the Coward Micro AI by using&lt;br /&gt;
 ai_type=coward&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance within which an enemy must be to &amp;quot;scare&amp;quot; the unit&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_if_trapped'''='no':  {{DevFeature1.13|0}}  With the default settings, a coward never attacks.  If this key is set to 'yes', the coward attacks the weakest (lowest hitpoints) enemy next to which it ends up after moving (such as a cornered animal would do, blindly lashing out without regards for its own safety).&lt;br /&gt;
* '''avoid_x, avoid_y''': The x and y coordinate of the hex to avoid. {{DevFeature1.15|0}} '''avoid_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units from which the cowards flee.  If not set, this defaults to all enemy units.  Note, however, that no &amp;quot;enemy of AI side&amp;quot; condition is added if [filter_second] is specified, meaning that it can include allied and/or enemy units as desired (and even units on the same side as the coward).&lt;br /&gt;
* '''seek_x, seek_y''': The x and y coordinate of the hex to seek. {{DevFeature1.15|0}} '''seek_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a coward [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=coward&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=coward3&lt;br /&gt;
             distance=5&lt;br /&gt;
             seek_x,seek_y=24,5&lt;br /&gt;
             avoid_x,avoid_y=24,15&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Zone guardian (ai_type=zone_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A zone guardian is a unit that, as the name says, guards a zone. It moves randomly inside this zone until an enemy enters it (or a separately defined enemy zone, see below). Applications might be the defense of a castle, a nesting area or for protecting an unit.  The zone macro can be called with an optional SLF, [filter_location_enemy]:&lt;br /&gt;
* If not specified, the zone guard attacks any enemy coming inside its guard zone.&lt;br /&gt;
* If [filter_location_enemy] is given, it attacks any enemy entering this zone and once there are no more enemies, it goes back to patrol in its basic zone.&lt;br /&gt;
&lt;br /&gt;
Enable the Zone Guardian Micro AI by using&lt;br /&gt;
 ai_type=zone_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone to guard.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_enemy]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone where enemies have to be attacked.  If not given, it defaults to [filter_location].&lt;br /&gt;
* '''station_x,station_y''': By default, the zone guardian moves randomly within the zone if there is no enemy to attack.  However, if station_x/y is given, it moves to this position instead during turns when it does not have an enemy to attack.  This position does not need to be inside the zone defined above.  Note: obviously it does not make sense to define [filter_location], [filter_location_enemy] and station_x/y all at the same time.  Only [filter_location] and either one of the latter two (or none of them) should be provided for any zone guardian. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a zone guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=zone_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=zone3&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=22-31,4-11  # This is intentionally chosen to extend past the lake&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             station_x,station_y=32,8&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Hang Out Micro AI (ai_type=hang_out) ==&lt;br /&gt;
&lt;br /&gt;
The Hang Out Micro AI instructs units of the AI side to &amp;quot;hang out&amp;quot; around a certain location, or locations, until a condition for mobilizing the side is met.  If none of the optional keys are given, the AI controls all units of the side to hang around the leader while moving off castle tiles to make room for recruiting.&lt;br /&gt;
&lt;br /&gt;
The AI works by moving each unit to its best hex first using the minimum amount of movement points required for this move.  Once all AI units are in position, movement points are set to zero for all of them simultaneously such that they cannot be used by other AI candidate actions.  This is done because the mobilizing condition might be met as result of one of the moves of the Hang Out AI, after which we want units to be able to continue with other AI actions.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Hang Out and Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Hang Out Micro AI by using&lt;br /&gt;
 ai_type=hang_out&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Hang Out specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  In its default configuration, the Hang Out MAI controls all units of a side, keeping them as close to the side leader(s) as possible while moving off castle tiles to make room for recruiting.  For this purpose, the candidate action score defaults to 170,000, which is just below the score of the default recruiting CA.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''': A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  If neither of these two exists, this parameter is set to default to all castle terrain (&amp;lt;tt&amp;gt;terrain=C*,C*^*,*^C*&amp;lt;/tt&amp;gt;) so that units move off the castle to make room for recruiting.  Note that this does not include keep terrain, otherwise the leader would be moved off the keep as well.&lt;br /&gt;
* '''ca_score'''=170000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 170,000, which means that it is executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Hang Out action (&amp;lt;tt&amp;gt;side=&amp;lt;/tt&amp;gt; is always set and does not need to be provided).  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the locations ''near'' which the AI units should hang out.  This does not mean that the units only move onto terrain specified here, but that they minimize the distance to such terrain.  It defaults to the position of the side leader(s).&lt;br /&gt;
* '''[mobilize_condition]''':  Contains ([[ConditionalActionsWML#Condition_Tags|WML Condition Tags]])  Once the condition set up here has been met, the Hang Out MAI is disabled and the side goes over to other AI activity.  The Hang Out MAI remains disabled after this, even if the condition is not met any more later in the scenario.&lt;br /&gt;
* '''mobilize_on_gold_less_than''': (number) Since side gold cannot be checked in a WML condition tag without storing it in a variable first, this key is provided separately.  It disables the Hang Out Micro AI once the side gold goes below the value specified here for the first time.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
* The Hang Out AI still takes effect if no locations to hang out at are found or if all map locations are to be avoided.  In that case, it simply keeps all units at their current locations.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Hang Out Micro AI configuration from the &amp;quot;Hang Out and Messenger&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hang_out&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [avoid]&lt;br /&gt;
                 terrain=C*,H*,M*,A*,S*,*^F*&lt;br /&gt;
             [/avoid]&lt;br /&gt;
             [mobilize_condition]&lt;br /&gt;
                 [have_unit]&lt;br /&gt;
                     side=1&lt;br /&gt;
                     count=7-99&lt;br /&gt;
                 [/have_unit]&lt;br /&gt;
             [/mobilize_condition]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Healer Support Micro AI (ai_type=healer_support) ==&lt;br /&gt;
&lt;br /&gt;
The Healer Support Micro AI configures the healers of a side to stay behind the battle lines and heal injured and/or threatened units rather than participate in combat under all circumstances.  You can set different levels of aggressiveness for the healers.  &lt;br /&gt;
&lt;br /&gt;
'''Notes'''&lt;br /&gt;
* By default, ''all'' healers of the AI side are controlled by this AI.  This includes in particular also the side leader, if the leader is a healer.  If you do not want the leader to be included (and possibly be moved away from the keep etc.) use the [filter] tag with canrecruit=no (see below).&lt;br /&gt;
* It is not always better to use this Micro AI than the default AI, as it takes healers (which can be strong units) away from the pool of units to choose for attacks.&lt;br /&gt;
* {{DevFeature1.15|3}} This Micro AI now takes unit guardian status and the passive_leader aspect into account.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Healers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Healer Support Micro AI by using&lt;br /&gt;
 ai_type=healer_support&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Healer Support specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the healer units.  If set, only units on the AI side passing this filter ''and'' having the healing ability are considered by this Micro AI.&lt;br /&gt;
*'''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting units to receive healing. If set, only units on the AI side passing this filter ''and'' the other healee selection criteria will be selected for healing by this Micro AI.&lt;br /&gt;
* '''aggression'''=1.0: (float) Sets the aggressiveness of the AI.  This parameter is set up as a float to accommodate future functionality, but it currently acts as a boolean: if set to zero, the AI will never let its healers participate in combat, for all other values it allows them to attack after all other units have attacked and if the healers cannot find any more units to support.  The former behavior might be appropriate in scenarios with only one or few valuable healers, while the latter might work better in scenarios with many healers.&lt;br /&gt;
* '''injured_units_only'''=no: (boolean) If set to 'yes', the AI will only move healers next to units that are already injured and skip units that currently have full hitpoints, but might get injured during the next enemy turn.&lt;br /&gt;
* '''max_threats'''=9999: (integer) The maximum allowable number of enemies that can attack a hex in order for it to be considered for a healer.  As an example, setting 'max_threats=0' means that the AI only moves healers to locations that are entirely safe from the enemy (assuming that none of the units currently on the map dies).  Note that the value of this key is checked against the number of enemies that can make it to the hex, not the number of adjacent hexes from which the healer could be attacked.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI. Note: as attacks by the healers are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The Healer Support Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded.  It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Healer Support Micro AI configuration from the &amp;quot;Healers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             aggression=0&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Lurkers Micro AI (ai_type=lurkers) ==&lt;br /&gt;
&lt;br /&gt;
The Lurker Micro AI controls some or all units of a side in &amp;quot;lurker mode&amp;quot;.  A lurker is a unit that is capable of moving across most terrains, but that only stops on and attacks from specific terrain.  It might also have the ability to hide on this terrain (which is the reason why it is called a lurker).&lt;br /&gt;
&lt;br /&gt;
Lurkers move individually without strategy and always attack the weakest enemy within their reach.  If no enemy is in reach, the lurker does a random move instead - or it just sits and waits (lurks).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of different types of lurker behavior, check out the &amp;quot;Lurkers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Lurkers Micro AI by using&lt;br /&gt;
 ai_type=lurkers&lt;br /&gt;
in the [micro_ai] tag&lt;br /&gt;
&lt;br /&gt;
===Lurkers specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
* '''[filter]''': Specifies which units of the side are controlled by the Lurker Micro AI.  This is a [[StandardUnitFilter|Standard Unit Filter]].  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': Specifies the terrain from which the lurkers attack. This is a [[StandardLocationFilter|Standard Location Filter]].&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_wander]''': Specifies the terrain on which lurkers end their moves if there is no enemy in reach. This is a [[StandardLocationFilter|Standard Location Filter]].  If [filter_location_wander] is not given, it defaults to [filter_location].&lt;br /&gt;
* '''stationary'''=no: (boolean)  If set to yes, a lurker never moves if there is no enemy within attack range.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Lurkers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=4&lt;br /&gt;
             ai_type=lurkers&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Naga Fighter&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=W*,S*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             [filter_location_wander]&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location_wander]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Messenger Escort Micro AI (ai_type=messenger_escort) ==&lt;br /&gt;
&lt;br /&gt;
The Messenger Escort Micro AI lets you define a set of waypoints through which it tries to move one or several of its units, the messenger(s).  The other units escort the messenger, trying to protect it and attacking enemies that are in its way.  Note that a messenger might not strictly hit intermediate waypoints, but that getting close is good enough, especially if a waypoint is occupied by an enemy unit.  It does, however, always try to get to the last waypoint.&lt;br /&gt;
&lt;br /&gt;
'''Notes on the use of several messengers:''' {{DevFeature1.13|0}}  &lt;br /&gt;
* This Micro AI can control several messengers at the same time, which can but do not have to move in a group.  They can be put on the map altogether in one location, or be spread out on the map or across different turns.&lt;br /&gt;
* The farther a messenger has moved through the chain of waypoints, the higher a priority it is for the AI, meaning that farther advanced (on the map) messengers are moved first, and that the escort units try to clear the way and stay with those messengers more so than with those further back on the map.  This order can be inverted using the invert_order= key.&lt;br /&gt;
* If several messengers are on the map, escort units might move from one to the other, the AI making a judgement call where they are needed most.  Closeness to a messenger is generally the most important criterion, but not the only one.&lt;br /&gt;
* If a messenger dies, the escort units previously staying with it now join one of the other messengers if there are any left (otherwise they follow default AI behavior).&lt;br /&gt;
* If you want certain escort units to stay always with the same messenger, use several messenger [micro_ai] tags with different [filter]/[filter_second] combinations.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Messenger Escort Micro AI by using&lt;br /&gt;
 ai_type=messenger_escort&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Messenger Escort specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': {{DevFeature1.13|0}} (Previous to 1.13.0, only id= was allowed.) The id of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated list of waypoint coordinates through which the messenger will go in the given order to reach its goal, given by the last coordinate.  If you want the messenger to go directly to the goal, simply enter a single x,y coordinate here.  Note that the messenger does not have to hit each waypoint exactly (except for the final one), getting close to them is good enough. {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Messenger Escort AI consists of 3 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-2&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_death_chance'''=0.67 and '''messenger_death_chance'''=0.0: (both floats; ranging from 0 to 1)    If the messenger is adjacent to an enemy at the end of the move, it will only attack the enemy if the chance of death of the enemy is &amp;gt;= enemy_death_chance and if the messenger's chance of death is &amp;lt;= messenger_death_chance (or if it is an attack at a range for which the enemy does not have a weapon).&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|0}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the escort units.  If not set, this defaults to all enemy units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''invert_order'''=no: {{DevFeature1.13|0}} If set to 'yes', the order of priority with which messenger units are considered is inverted, starting with the messenger unit that is the least advanced on the map (see description above).  In most cases, selecting this option results in worse performance of the AI, so use it with caution.  (And obviously it has no effect at all if there is only one messenger controlled by the AI.)&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Messenger Escort&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=messenger_escort&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=messenger&lt;br /&gt;
             waypoint_x=31,24,27,28&lt;br /&gt;
             waypoint_y=20,14,7,1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Patrol Micro AI (ai_type=patrol) ==&lt;br /&gt;
&lt;br /&gt;
The defines a number of waypoints for a unit (or units) to follow.  Options can be set whether the route should be followed in a loop, as an out-and back, only once, whether units encountered along the way should be attacked, etc.&lt;br /&gt;
&lt;br /&gt;
No matter what options are chosen, getting to the next waypoint always takes priority over attacking for a patroller.  The AI thus prefers to move the patrol unit around enemies rather than straight for them.  Also, if a waypoint is occupied by a unit the AI is not instructed to attack, it will (eventually) abandon that waypoint once it gets close enough and move on to the next one.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different patrol behaviors, check out the &amp;quot;Patrols&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Patrol Micro AI it by using&lt;br /&gt;
 ai_type=patrol&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Patrol specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated lists of the waypoint coordinates through which the patrol travels (in order). {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack''': By default (if this key is not set), the patrol unit attacks any unit it ends up next to after a move.  Alternatively, this parameter can be set to a comma-separated list of unit ids. If that is done, the patrol only attacks these units when it ends up next to them and ignores any other unit.  This can in turn be used to have the patroller not attack at all, by setting ''attack='' to a non-existing id.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''one_time_only'''=no: (boolean)  If set to 'yes', the unit goes through the patrol route only once and then hangs out at the last waypoint.  It also always attacks any enemy unit on the last waypoint, independent of what ''attack='' is set to.&lt;br /&gt;
* '''out_and_back'''=no: (boolean)  By default, a patrol unit returns to the first waypoint after reaching the last, doing the patrol route as a loop.  If this parameter is set to 'yes', the unit instead reverses its course and perpetually does out and backs of the route.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Patrols&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=patrol&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Konrad&lt;br /&gt;
             waypoint_x=9,24,25&lt;br /&gt;
             waypoint_y=21,23,15&lt;br /&gt;
             one_time_only=yes&lt;br /&gt;
             attack=Gertburt&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Protect Unit Micro AI (ai_type=protect_unit) ==&lt;br /&gt;
&lt;br /&gt;
The Protect Unit Micro AI tries to move one or several units to a given location while keeping them safe from enemy attacks. It does so by keeping the protected units away from enemy units and close to friendly units. Other units on the AI side are controlled by the default Wesnoth AI.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Protect Unit&amp;quot; and &amp;quot;The Elves Besieged&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Protect Unit Micro AI by using&lt;br /&gt;
 ai_type=protect_unit&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Protect Unit specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[unit]''': A separate [unit] tag is required for each protected unit(s).  Each tag has required keys id=, goal_x=, goal_y= for each unit, as shown in the example below. {{DevFeature1.15|0}} '''goal_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''disable_move_leader_to_keep'''=no: (boolean) If set to 'yes', side leaders protected by this AI do not try to return to their keep.  This is necessary for this Micro AI to work with units that are side leaders and that are supposed to move to an off-keep location.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
*The Protect Unit Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].  Depending on the parameters, it might also modify the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|move leader to keep candidate action]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded. It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;HttT: The Elves Besieged&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=protect_unit&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Delfador&lt;br /&gt;
                 goal_x,goal_y=1,2&lt;br /&gt;
             [/unit]&lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
                 goal_x,goal_y=1,1&lt;br /&gt;
             [/unit]&lt;br /&gt;
 &lt;br /&gt;
             disable_move_leader_to_keep=true&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Recruiting Micro AI==&lt;br /&gt;
=== General Comments on the Recruiting Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Recruiting Micro AI replaces the default recruitment AI by alternative recruiting patterns.  Currently there are two recruiting algorithms you can choose from, random recruiting and the rush recruiting used in the new (as of Version 1.11.1) Experimental AI (see below).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of a different recruiting pattern, check out the &amp;quot;Recruiting&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  You can also explore the unit choices by watching the Experimental AI in a multiplayer game.&lt;br /&gt;
&lt;br /&gt;
'''Note:''' Obviously, the Recruitment Micro AI modifies the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|recruitment candidate action]].&lt;br /&gt;
&lt;br /&gt;
=== Random Recruitment AI (ai_type=recruit_random) ===&lt;br /&gt;
&lt;br /&gt;
This AI randomly selects a unit type from the recruitment list of the side.  The probability of choosing each type and the behavior in low-gold situations can be influenced with optional parameters.&lt;br /&gt;
&lt;br /&gt;
Enable the Random Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_random&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* '''[probability]''': This tag sets the probability of selecting the given unit type.  It has two required sub-keys:&lt;br /&gt;
** '''type''': comma-separated list of unit types to which this probability is to be applied&lt;br /&gt;
** '''probability''': (non-negative float)  The probability with which these unit types is to be selected.  All unit types for which no [probability] tag is defined receive a default probability of 1.0, thus values should be chosen relative to that.  As an example, in the code below swordsmen and peasants are given a probability of 8, while no other units have assigned probabilities.  Thus, each of these two unit types is 8 times more likely to be selected than any other individual unit type.&lt;br /&gt;
* '''skip_low_gold_recruiting'''=no: (boolean)  By default, the random recruitment AI chooses a unit to recruit only from those types for which it has sufficient gold.  If this parameter is set to 'yes', it will instead choose from all available unit types and end recruiting for the turn if there is not enough gold to recruit it this turn (even if other, affordable unit types exist).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a random recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_random&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [probability]&lt;br /&gt;
                 type=Swordsman,Peasant&lt;br /&gt;
                 probability=8&lt;br /&gt;
             [/probability]&lt;br /&gt;
 &lt;br /&gt;
             skip_low_gold_recruiting=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Rush Recruitment AI (ai_type=recruit_rushers) ===&lt;br /&gt;
&lt;br /&gt;
This AI replaces the default recruiting with the rush recruiting used in the new (as of Version 1.11.1) Experimental AI.&lt;br /&gt;
&lt;br /&gt;
Enable the Rush Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_rushers&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* {{DevFeature1.15|2}} '''high_level_fraction'''=0: (non-negative number) The approximate fraction of units of level 2 or higher to be recruited. This is defined as fraction of units on the map, not of new units being recruited during a turn or over several turns (which makes a difference if there are already units on the map). The effect is also cumulative per level (starting from level 2), meaning that 1/3 of L2 units will be recruited in the example above, 1/3*1/3=1/9 L3s etc. The default value is zero, which leaves the high-level unit recruiting to the default Experimental AI algorithm resulting usually in very few such units being recruited.&lt;br /&gt;
* '''randomness'''=0.1: (number)  A random number is applied to the rusher recruit engine's score to prevent the recruitment pattern from being too predictable. 0 causes no randomness to be applied, while larger numbers increase the random effect. A value of 1-2 generates results in which the random effect is approximately equal to the scored effect. Extremely high values are essentially entirely random.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a rush recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_rushers&lt;br /&gt;
             action=add&lt;br /&gt;
          [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Simple Attack Micro AI (ai_type=simple_attack) ==&lt;br /&gt;
&lt;br /&gt;
The Simple Attack Micro AI is meant as an addition to the default combat candidate action for when one wants to force the AI to do attacks that it would otherwise not do (for example on units that are 1 XP from leveling) or to execute certain attacks before all others (for example, attack with all &amp;quot;expendable&amp;quot; units first).  It is not set up to be a replacement for the default attacks.  Some notes:&lt;br /&gt;
&lt;br /&gt;
* The algorithm is different from and simpler than that of the default combat CA.  For example, no attack combinations are considered, only individual attacks.  As a result, the [[AiWML#List_of_AI_Aspects|default aspects]] (aggression, caution, [avoid], ...) are not taken into account.  Also, if lots of units are involved, evaluation of this Micro AI is potentially quite a bit slower than of the default attacks.&lt;br /&gt;
&lt;br /&gt;
* If an attack matching the Standard Unit Filters described below is found, this attack will always be executed, no matter how bad the odds are.  Thus, this Micro AI can be used to force the AI to do attacks it would otherwise avoid.&lt;br /&gt;
&lt;br /&gt;
* Other than selecting the attacker/defender units, this AI is currently not configurable.  Let us know if you would like to see certain options to be added.  (No guarantees that we will add them, but we will definitely consider them.)&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Simple Attack&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=simple_attack&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Simple Attack specific keys for the [micro_ai] tag===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  It is, however, in general not desirable to use this AI without at least one of the optional Standard Unit Filters below, as this AI is not meant as a replacement of the default combat candidate action.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=110000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 110,000, which means that it is executed right before the default combat candidate action.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Simple Attack action.  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units the AI should attack.  It defaults to all enemy units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
* '''weapon'''=-1: (integer) The number of the weapon that should be used for this attack.  Weapon numbers start at 1 (not 0).  -1 stands for &amp;quot;best weapon&amp;quot; (by whatever criterion the AI chooses to use).   '''Available from Wesnoth 1.11.8'''&lt;br /&gt;
&lt;br /&gt;
Below are 2 examples of Simple Attack Micro AI configurations from the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  The first one shows how to force the AI to attack with all Soulless and Walking Corpse units first:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110000&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless,Walking Corpse&lt;br /&gt;
             [/filter]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
The second executes attacks by Soulless on all enemy units that are 1 XP from leveling (something the RCA AI would generally not do).  Note that the Lua code bit is simply there in order to code the &amp;quot;1 XP from advancing&amp;quot; condition.  It is not needed for most other SUFs, as can be seen in the previous example.&lt;br /&gt;
         [lua]&lt;br /&gt;
             code=&amp;lt;&amp;lt;&lt;br /&gt;
                 function close_to_advancing(unit)&lt;br /&gt;
                     if (unit.experience &amp;gt;= unit.max_experience-1) then&lt;br /&gt;
                         return true&lt;br /&gt;
                     else&lt;br /&gt;
                         return false&lt;br /&gt;
                     end&lt;br /&gt;
                 end&lt;br /&gt;
             &amp;gt;&amp;gt;&lt;br /&gt;
         [/lua]&lt;br /&gt;
 &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110001&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless  # No Walking Corpses; L0 units don't advance enemy&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 lua_function = &amp;quot;close_to_advancing&amp;quot;&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
Several Simple Attack Micro AIs can be combined for the same AI side.  The two examples above are both used (note the different values for ca_score) in the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
== Custom Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|5}}&lt;br /&gt;
&lt;br /&gt;
From 1.13.5 onwards, adding a new Micro AI can be easily done without altering any core files or overriding the micro_ai tag. The [micro_ai] tag now looks up its ''ai_type'' parameter in the &amp;lt;code&amp;gt;wesnoth.micro_ais&amp;lt;/code&amp;gt; table and uses the micro AI setup function found there to determine which keys are required and allowed and what candidate actions to add.&lt;br /&gt;
&lt;br /&gt;
A micro AI setup function takes as its single parameter the contents of the [micro_ai] tag and returns three separate values:&lt;br /&gt;
*'''required''': A list of required keys and tags, as strings. Tags are indicated by enclosing them within square brackets in the string. If a key or tag in this list is missing from the [micro_ai] tag, an error will be printed.&lt;br /&gt;
*'''optional''': Similar to above, a list of optional keys and tags, as strings. No error will be printed if a key or tag in this list is missing, nor will an error be printed if it is present (as would be the case for unrecognized keys or tags).&lt;br /&gt;
*'''ca_params''': A table specifying the candidate actions that constitute the micro AI. It should contain an ''ai_id'' key (which specifies a base ID to be used for candidate actions of this micro AI) and a series of candidate action definitions. Each candidate action definition is a table containing the following keys:&lt;br /&gt;
**'''ca_id''': The ID used for the candidate action. This will be combined with the ''ai_id'' to produce the final ID.&lt;br /&gt;
**'''location''': Path to the Lua file that defines the candidate action.&lt;br /&gt;
**'''score''': The maximum score for the candidate action. If the [micro_ai] tag specified a ''ca_score'' parameter, the score here should normally be based on that.&lt;br /&gt;
&lt;br /&gt;
(The return statement would thus look like &amp;lt;syntaxhighlight lang='lua' inline&amp;gt;return required, optional, ca_params&amp;lt;/syntaxhighlight&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
Since the function has full access to the contents of the [micro_ai] tag, it can conditionally add candidate actions to '''ca_params''' depending on the values of other keys within the [micro_ai] tag. It can also perform other actions such as adding facets to an aspect; if it does such things, it should pay attention to the ''action'' key to determine whether it's being added or removed.&lt;br /&gt;
&lt;br /&gt;
There are two useful helper functions that can be used by Micro AI setup functions requiring aspect changes. They can be brought into scope by a line similar to the following:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='lua&amp;gt;local MAIH = wesnoth.require(&amp;quot;ai/micro_ais/micro_ai_helper.lua&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* MAIH.add_aspects(side, aspect_params)&lt;br /&gt;
* MAIH.delete_aspects(side, aspect_params)&lt;br /&gt;
&lt;br /&gt;
The ''aspect_params'' parameter is an array of tables, each of which contains the following keys:&lt;br /&gt;
*'''aspect''': The aspect to change.&lt;br /&gt;
*'''facet''': The WML defining the facet. This must include an ''id'' key in order for the aspect to be correctly deleted. For maximum compatibility, incorporate the ''ca_id'' key from the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=65224</id>
		<title>Micro AIs</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=65224"/>
		<updated>2019-12-19T00:37:42Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Goto specific keys for the [micro_ai] tag: */ comment on how [avoid] works with this MAI&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== A Few General Words about Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
* Micro AIs allow Wesnoth campaign and scenario authors to add new AI functionality without the need for any AI programming. They bring new specialized behavior to Wesnoth that cannot be achieved, at least not easily, by adjusting default AI parameters (such as those described at [[AiWML]]) or with WML code.&lt;br /&gt;
&lt;br /&gt;
* A Micro AI is activated and configured (or deleted) via the [micro_ai] tag, requiring only a few lines of WML code.&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.6''', the Micro AI behavior was inconsistent when it came to '''hidden enemies and petrified units'''.  Some Micro AIs ignored those, while others did not.  In some case there was even a potential for AI errors when an AI unit ran into hidden or petrified enemies.  As of 1.13.6, this has all been fixed.  The Micro AIs now correctly and consistently ignore hidden units and treat petrified units as blocking the hex they are on but having no effect otherwise, in the same way as the default AI does.  '''Important notes:'''&lt;br /&gt;
** It is not possible to re-include hidden/petrified units via the Standard Unit Filters (SUFs) used by some of the Micro AIs. In fact, if this is attempted, the filters will generally not match any unit.&lt;br /&gt;
*** This only applies to SUFs which go directly into the [micro_ai] tag. Any SUF that is a sub-tag of another filter (for example, of a Standard Location Filter) is taken literally and returns all units matching the filter. If hidden or petrified units are to be excluded from such a sub-filter, that needs to be specified directly in that filter.&lt;br /&gt;
** We also note that, because all the Micro AIs are written in Lua, it is possible to [[Micro_AIs#Using_Development_.281.13.29_Version_Micro_AIs_in_your_Stable_.281.12.29_Version_Add-on|move a Micro AI's code into one's add-on]] and modify it to whatever scenario-specific behavior is desired (this does not only apply to hidden/petrified units, but to all behavior). Setting up general Micro AI behavior that works in every possible setting is not feasible, but adapting it to work (only) in the specific setting of a given scenario is often quite simple. If you do not know how to do this, please post a message on the Wesnoth forums and we will be happy to help you with it.&lt;br /&gt;
&lt;br /&gt;
* The '''[[AiWML#List_of_AI_Aspects|default AI aspects]] are, for the most part, not taken into account by the Micro AIs'''.  For example, unless noted otherwise, specifying a value for ''aggression'' does not change the MAI behavior, and locations specified in an ''[avoid]'' tag generally can be entered by units controlled by an MAI.  This is a feature of the Micro AIs, not a bug.  If you feel that a certain default AI aspect should also apply to a Micro AI, please let us know in the [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 Micro AIs Feedback thread].&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.7''', the Micro AIs did not all work reliably when the '''AI side is under shroud''' and it may have been necessary to turn shroud off for the side when using a Micro AI. {{DevFeature1.13|7}} The Micro AIs now all work for sides under shroud. They do this by disregarding shroud for terrain purposes when path finding, consistent with default Wesnoth AI behavior, while still ignoring hidden units (including those hidden under shroud) correctly.&lt;br /&gt;
&lt;br /&gt;
* Most Micro AIs in their default setting apply to all units of a side, although many of them provide parameters for filtering the units that should be affected by the new behavior.  If the Micro AI is applied only to part of the units of a side, the other units follow the default Wesnoth AI behavior.&lt;br /&gt;
&lt;br /&gt;
* Micro AIs are available in mainline since Wesnoth 1.11/1.12.&lt;br /&gt;
&lt;br /&gt;
== Micro AI Test and Demo Scenarios ==&lt;br /&gt;
&lt;br /&gt;
All Micro AIs described here can be checked out in a number of test and demo scenarios.  Test scenarios can be accessed from the command line by typing&lt;br /&gt;
 path/wesnoth-executable -t micro_ai_test&lt;br /&gt;
or (depending on your system and Wesnoth version)&lt;br /&gt;
 path/wesnoth-executable -tmicro_ai_test&lt;br /&gt;
This starts up a &amp;quot;switchboard scenario&amp;quot; in which you can select the Micro AI demonstration scenario you want to check out.  Here, ''path/wesnoth-executable'' needs to be replaced by whatever the path and filename of the Wesnoth executable is on your system.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|8}}&lt;br /&gt;
&lt;br /&gt;
You can also access the test scenarios by assigning a hotkey to &amp;quot;Start Test Scenario&amp;quot; in the preferences. Then, simply press the hotkey at the titlescreen, possibly wait out the loading screen, and select the desired scenario from the list.&lt;br /&gt;
&lt;br /&gt;
== Setting up a Micro AI ==&lt;br /&gt;
&lt;br /&gt;
Setting up a Micro AI requires only one simple step:&lt;br /&gt;
==== Activating and configuring (or deleting) a Micro AI ====&lt;br /&gt;
&lt;br /&gt;
Micro AIs are activated, deleted and configured using the [micro_ai] tag.  This tag needs to be placed in [[ActionWML]], that is, in an event, a menu option or the like.  As an example, the following code activates the healer_support Micro AI in its default configuration for Side 2 from the beginning of the scenario:&lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
 &lt;br /&gt;
         # Configure the healer support micro AI&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
'''Keys required for all Micro AIs:'''&lt;br /&gt;
All [micro_ai] tags must contain the following three required keys:&lt;br /&gt;
* '''side''': The side for which the Micro AI is to be added, changed or deleted.  This has to be an individual side number, lists of sides are not supported.&lt;br /&gt;
* '''ai_type''': The type of Micro AI to be added, changed or deleted.  See the following sections for allowed values.&lt;br /&gt;
* '''action''' (string): The action to take concerning the Micro AI.  The following values are allowed: &lt;br /&gt;
** ''add'': Add a Micro AI to a side.&lt;br /&gt;
** ''change'': Change the configuration of an existing Micro AI.  Note that this does not only change the specific parameters provided in the tag, but it replaces the entire existing Micro AI by a new version with the new (and only the new) configuration.  It is therefore equivalent to using first the ''delete'' and then the ''add'' action.&lt;br /&gt;
** ''delete'': Delete an existing Micro AI from a side.&lt;br /&gt;
&lt;br /&gt;
'''Keys required under certain circumstances:'''&lt;br /&gt;
* '''ca_id''': (string) If several MAIs of the same type are used for the same side and one or several of these are to be changed or deleted during a scenario, this key needs to be included so that the [micro_ai] tag knows which of them needs to be changed/deleted.  The same is also true if a Micro AI is changed more than once during a scenario.  We therefore '''strongly recommend always setting ''ca_id''''' for MAIs which are, or might be, changed or deleted during a scenario.&lt;br /&gt;
** '''Note:''' The id(s)/name(s) actually assigned to the CA(s) of the MAI by the engine might be different from the parameter passed here.  It is, however, sufficient to use this ca_id with the [micro_ai] tag add/delete/change actions to identify the MAI uniquely.  (In other words: you don't have to worry about this if you always use the [micro_ai] tag, but might have to figure out the actual name(s) of the CA(s) if you want to delete or change them manually.)&lt;br /&gt;
* Other keys may also be required depending on the Micro AI.  In additional, a number of optional keys may be available to configure the AI behavior.  See the sections on the individual Micro AIs for details.&lt;br /&gt;
&lt;br /&gt;
'''Required keys for deleting a Micro AI:'''&lt;br /&gt;
&lt;br /&gt;
Only a subset of the keys required for setting up a Micro AI are needed when deleting it.  '''side''', '''ai_type''' and '''action'''=delete always need to be provided.  '''ca_id''' needs to be given if it was provided when adding the Micro AI (see above for when this is needed).&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* The ''add'' and ''change'' actions ignore all keys that do not apply to the respective Micro AI type.  The ''delete'' action ignores all keys other than the required keys listed above.&lt;br /&gt;
&lt;br /&gt;
* Some of the Micro AIs use and/or modify default AI components (candidate actions or aspects) when they are added, or reset these components to their default values when they are deleted.  Thus, if you have modified the AI yourself using the default CAs or aspects, some of these modifications might be affected, or might be interfering with the Micro AI.  The sections below indicate whether a Micro AI modifies any default AI components.&lt;br /&gt;
&lt;br /&gt;
===Combining Different Micro AI Types for the Same Side===&lt;br /&gt;
&lt;br /&gt;
No extra steps need to be taken when combining different Micro AIs on the same side.  Simply set up the [micro_ai] tags for all AIs you want to use.&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* While it is ''technically'' possible to combine any number of Micro AIs, this does not mean that all Micro AIs work together in practice.  Some are not compatible because they control units in contradictory ways.  &lt;br /&gt;
* Not all combinations that ''should'' work have been tested yet.  If you find a combination that you believe should work but doesn't, [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 please let us know] and we will see if that can be fixed.&lt;br /&gt;
* The candidate action score almost certainly has to be set (using the ca_score= key) for one or several of the MAIs if several of them are used on the same side.&lt;br /&gt;
&lt;br /&gt;
== Animals Micro AIs ==&lt;br /&gt;
=== General Comments on the Animal Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Animals Micro AIs are meant to simulate the behaviors of certain types of animals.  They are, however, set up so that they can be used with arbitrary unit types and might be applicable in quite diverse situations.  The AIs nevertheless remain named after the animals for which they were written originally, so that they are (to some extent) descriptive of their behavior.&lt;br /&gt;
&lt;br /&gt;
Brief descriptions of the AI behaviors are given in the following subsections.  To get a better feeling for their behavior, check out the &amp;quot;Animals&amp;quot;, &amp;quot;Swarm&amp;quot;, &amp;quot;Wolves&amp;quot; and &amp;quot;Dragon&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  Note that some of the animal AIs are designed for animal types that do not exist in mainline.  In the test scenario, other animal types are therefore substituted for them.&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Wolves (ai_type=wolves) ===&lt;br /&gt;
&lt;br /&gt;
The Wolves Micro AI organizes all &amp;quot;wolf&amp;quot; units (predators) of the side to hunt in a single pack.  They actively chase after the closest prey and try to corner it (not always super successfully), but are easily distracted by other enemy units coming into attack range, to the point where the pack might split up.  The wolves can, however, be told to attack only prey units as well as to try to avoid other types of units (such as larger predators).  When no prey is left, the wolves wander randomly.&lt;br /&gt;
&lt;br /&gt;
This AI works best for a small number of predators (2 - 5) and sparse prey.  Also, the predators should be put on the map in a pack, that is, reasonably close to each other.  For a demonstration of the Wolves AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Note that another wolves AI exists, called [[Micro_AIs#Animals_AI:_Multi-pack_Wolves_.28ai_type.3Dwolves_multipacks.29|Multipack Wolves]], which distributes the wolves of a side into several packs, rather than all of them into one single pack as it is done here.&lt;br /&gt;
&lt;br /&gt;
Enable the Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the predator units.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the prey units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_only_prey'''=no: In the default configuration, the wolves attack all enemy units in attack range.  If this key is set to 'yes', they only attack prey units.&lt;br /&gt;
* '''avoid_type''': Comma-separated list of unit types (on enemy sides) which the wolves try to avoid.  They try to stay out of these units' way as much as possible when moving around (except when they are moving in to attack another enemy unit) and never attack them.&lt;br /&gt;
* '''ca_score'''=90000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 90,000, which is lower than the default combat CA, but higher than most other default CAs which move units.  It can be changed by setting this parameter but in most cases this will not make sense.  Note that the Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Notes on default (RCA) AI aspects:''' &lt;br /&gt;
* If ''attack_only_prey'' or ''avoid_type'' are set, the Wolves Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* Attacks by the wolves are left to the default (RCA) AI, meaning that the value of ''aggression'' has an influence on the behavior.&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI is taken into account. Note: as attacks by the wolves are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=6&lt;br /&gt;
             ai_type=wolves&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Wolf&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Vampire Bat&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             avoid_type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Multi-pack Wolves (ai_type=wolves_multipacks) ===&lt;br /&gt;
&lt;br /&gt;
The Multi-pack Wolves Micro AI is different from the [[Micro_AIs#Animals_AI:_Wolves_.28ai_type.3Dwolves.29|Wolves Micro AI]] in that there can be an arbitrary number of wolf packs, with the pack size being a free parameter.  At the beginning of the scenario, close wolves are grouped into packs in a semi-methodical way.  If the wolves are put on the map in distinct groups of close wolves of the correct number, they will be joined into packs matching these groups.  If, on the other hand, they are spread out all over the map, the method of assigning them to packs is semi-random.&lt;br /&gt;
&lt;br /&gt;
Wolves of the same pack begin by joining each other on the map.  After that, they stay together until only one wolf is left, which then tries to join up with an incomplete pack or with other single wolves.  Individual wolves entering the map during the scenario behave in that way as well.&lt;br /&gt;
&lt;br /&gt;
A second difference to the other Wolves AI is that wolves do not actively hunt here.  For the most part they just wander (often long distance).  However, the pack ferociously (and without regard for its own health) attacks any enemy units that come into range, as long as that does not mean separating the pack by more than a few hexes.  Staying together, or joining with a new wolf assigned to the pack, is the only thing that takes priority over satisfying the wolves' thirst for blood.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Multi-pack Wolves AI behavior, check out the &amp;quot;Wolves&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Multi-pack Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves_multipacks&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Multi-pack Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''pack_size'''=3: (integer) The size of the packs.&lt;br /&gt;
* '''show_pack_number'''=no: If set to &amp;quot;yes&amp;quot;, the wolves' pack numbers will be shown below them.  Note that this means that the pack number of a wolf killed in an attack will remain as a label on the map.  &lt;br /&gt;
* '''type'''=Wolf: Comma-separated list of the unit types which the AI controls. The default value is &amp;quot;Wolf&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves_multipacks [micro_ai] tag usage from the &amp;quot;Wolves&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=wolves_multipacks&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             show_pack_number=yes&lt;br /&gt;
             pack_size=4&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Big Animals (ai_type=big_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Big Animals Micro AI is a simulation of large predators that wander and hunt alone, such as Bears, Giant Spiders or Yetis.  For the most part, these just wander on the terrain that's been defined for them, but they attack enemy units if those happen to come into range.  The AI can be set up so that the Big Animals stay out of the way of other units (such as other large predators), in which case they only attack those if cornered or if they run into them accidentally (such as ambushers).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Big Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Big Animals Micro AI by using&lt;br /&gt;
 ai_type=big_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the animals to which this AI is applied.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': The big animals often go for long distance (multi-turn) journeys.  This tag is a [[StandardLocationFilter|Standard Location Filter]] describing the terrain from which the next goal is chosen.  The default is all terrain on the map. &lt;br /&gt;
* '''[filter_location_wander]''': The terrain on which the big animals will end their moves. This is a [[StandardLocationFilter|Standard Location Filter]].  The default is all terrain on the map.&lt;br /&gt;
* '''[avoid_unit]''': A [[StandardUnitFilter|Standard Unit Filter]] describing all enemy units that the big animals will avoid.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
Here's an example of big_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=big_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Ghast&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [avoid_unit]&lt;br /&gt;
                 type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
             [/avoid_unit]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=1-40,1-18&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Forest Animals (ai_type=forest_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Forest Animals Micro AI simulates a number of different animal behaviors that one might find in a forest, namely animals behaving like deer, rabbits and families of tuskers.  In the following, we will refer to those animals by these types, even though the actual unit types can be selected freely.  They behave as follows:&lt;br /&gt;
* Each deer wanders randomly on (selectable) terrain, except when enemies get in its (the deer's) maximum movement range, in which case it flees to the farthest point it can reach.&lt;br /&gt;
* Tuskers exhibit the same behavior as deer, except when an enemy is next to one of the tusklets.  This enemy is attacked unconditionally by the tuskers trying to protect their young.&lt;br /&gt;
* Tusklets blindly follow the closest adult tusker, except when there is no tusker left, in which case they behave the same as deer.&lt;br /&gt;
* Rabbits also wander randomly, but in addition disappear into their holes (if any are within reach) when enemies are close.  They reappear out of their holes at the beginning of the turn, if it is safe.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Forest Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Forest Animals Micro AI by using&lt;br /&gt;
 ai_type=forest_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Forest Animals AI consists of 4 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-3&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''rabbit_type, tusker_type, tusklet_type, deer_type''': Comma-separated lists of the unit types behaving as the respective animals.  If any of these parameters are not set, those types of animal behavior are skipped by the AI.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain on which the forest animals wander when they are not threatened by enemies.&lt;br /&gt;
* '''rabbit_number'''=6: (integer)  The number of rabbits for the AI to have on the map simultaneously.  The AI puts the missing number of rabbits onto the map at the beginning of the AI's turn (when the rabbits come out of their holes).&lt;br /&gt;
* '''rabbit_enemy_distance'''=3: (number)  Rabbits won't spawn in holes that aren't more than this distance away from all enemies.&lt;br /&gt;
* '''rabbit_hole_img''': Rabbit holes are marked by items on the map.  If this parameter is set, it must be the file name of the item image.  If it is not set, all items on the map are treated as rabbit holes.&lt;br /&gt;
&lt;br /&gt;
Here's an example of forest_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=forest_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             deer_type=Vampire Bat&lt;br /&gt;
             rabbit_type=Giant Rat&lt;br /&gt;
             tusker_type=Ogre&lt;br /&gt;
             tusklet_type=Young Ogre&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=*^F*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Herd and Herders (ai_type=herding) ===&lt;br /&gt;
&lt;br /&gt;
The Herding Micro AI sets up an area on the map where a number of herder units (such as sheep dog) control and protect a herd (such as sheep).  For convenience, we simply refer to them as sheep dogs and sheep in the following, even though arbitrary unit types can be selected for either.&lt;br /&gt;
&lt;br /&gt;
Sheep dogs try to keep their sheep safe.  This involves keeping them inside the herding area, positioning themselves in between the sheep and approaching enemies, and attacking the enemies if those get too close.  If no active herding or protecting move is needed, the dogs go to a random location on the perimeter of the herding area.&lt;br /&gt;
&lt;br /&gt;
Sheep wander aimlessly except when a sheep dog is next to them, in which case they run away from the dog.  The dogs exploit this by positioning themselves on the outside of the sheep, if possible.  Sheep also run away from approaching enemies.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Herding AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Herding Micro AI by using&lt;br /&gt;
 ai_type=herding&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herder units (e.g. dogs).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herd units (e.g. sheep).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain forming the perimeter of the herding area.  In other words, this is the path the herders (dogs) use to patrol around the outside of the herding area.  This must be a set of coordinates ''completely enclosing'' the herding area.  Here's an example of setting up a rectangular perimeter:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         x=10-20,10-20,10,  ,20&lt;br /&gt;
         y=11,   21,   11-21,11-21&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
Another possibility is by defining the path by certain terrain on the map (as it is done in the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]), in which case the filter can simply look like this:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         terrain=Rb&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
* '''herd_x,herd_y''': The coordinate of one hex (''any'' one hex) inside the herding area.  This is needed to define which part of the map is on the ''inside'' of the herding area perimeter defined by [herding_perimeter], and which is the ''outside''. {{DevFeature1.15|0}} '''herd_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''attention_distance'''=8: (integer)  If an enemy is within this distance of a herd unit, but not as close as ''attack_distance'', the herders try to position themselves in between the enemy and the herd unit.&lt;br /&gt;
* '''attack_distance'''=4: (integer)  If an enemy is within this distance of a herd animal, the herders attack this enemy.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Herding AI consists of 6 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-5&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Herding [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=herding&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Footpad&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Troll Whelp&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             herd_x,herd_y=32,28&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=Rb&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Hunter (ai_type=hunter) ===&lt;br /&gt;
&lt;br /&gt;
The Hunter Micro AI leads a unit (or units) through hunt-and-rest cycles.  The units wander in a prescribed area of the map, their hunting ground, until an enemy unit comes within range.  Note that the units do not actively chase down enemies, but wander randomly through the hunting ground until an enemy is encountered.&lt;br /&gt;
&lt;br /&gt;
If enemies are within range of a hunter unit, the hunter attacks the weakest of them.  If a kill is made, it then retreats to its rest location, where it stays for a certain number of turns and until fully healed (whatever happens later).&lt;br /&gt;
&lt;br /&gt;
A few more details:&lt;br /&gt;
* If the hunter's way home is entirely blocked on the return (so that there is no possible path), the normal RCA AI takes over its behavior.&lt;br /&gt;
* However, if the way is blocked by an enemy unit occupying the rest hex, that enemy will be attacked unconditionally.&lt;br /&gt;
* A kill only makes the hunter go home when it is the attacker, not as defender.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Hunter AI behavior, check out the &amp;quot;Dragon&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]. &lt;br /&gt;
&lt;br /&gt;
Enable the Hunter Micro AI by using&lt;br /&gt;
 ai_type=hunter&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''home_x,home_y''': The coordinates of the hex to which the hunter returns and rests at after a kill. {{DevFeature1.15|0}} '''home_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the unit's hunting ground.  If this tag is not provided, the hunter uses all of the map.&lt;br /&gt;
* '''rest_turns'''=3: (integer)  The number of turns the AI stays at home_x,home_y after a kill.  It will not leave its home until this number of turns have passed ''and'' it is fully healed.&lt;br /&gt;
* '''show_messages'''=no: (boolean) If set to yes, the hunter announces whenever its behavior changes to the next phase.  Note that this is meant as debug output for the scenario author.  The messages are not translated and are not included in replays (and are only visible to the host in an MP game).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a hunter [micro_ai] tag usage from the &amp;quot;Dragon&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hunter&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Rowck&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=5-30,1-15&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             home_x,home_y=3,15&lt;br /&gt;
             rest_turns=2&lt;br /&gt;
 &lt;br /&gt;
             show_messages=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Swarm (ai_type=swarm) ===&lt;br /&gt;
&lt;br /&gt;
The Swarm Micro AI uses some very simply algorithms to simulate animal swarm behavior.  Without adjacent enemies, they simply do a random move, trying to stay together and at a certain distance from enemies.  However, if an enemy unit is close to any bat, the swarm scatters.  This is particular fun to watch when one places an enemy unit in the middle of the swarm.  After being scattered, the swarm members slowly rejoin, but not in a very organized way.  Sub-swarms or individual bats might roam around for quite some time before they find their way back.  It is also possible that individual bats (or small groups) split off from the larger swarm at times.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Swarm AI behavior, check out the &amp;quot;Swarm&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Swarm Micro AI by using&lt;br /&gt;
 ai_type=swarm&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Swarm AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_distance'''=5: (int) The minimum distance kept between units of the swarm and enemies when the swarm moves as a whole.&lt;br /&gt;
* '''scatter_distance'''=3: (int) Enemies within &amp;quot;scatter_distance&amp;quot; hexes of any swarm unit cause the swarm to scatter, by each unit trying to maximize its individual distance from the enemy.&lt;br /&gt;
* '''vision_distance'''=12: (int) Only units within this distance follow the overall swarm motion (either away from an enemy or of the swarm as a whole).  The smaller this value is set, the less likely the swarm is to stay together or rejoin.  This parameter is meant to simulate how far an individual member of the swarm can see, meaning that the swarm is &amp;quot;out of sight&amp;quot; (and the unit will not be able to follow it) if it is more than this distance away.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Swarm [micro_ai] tag usage from the &amp;quot;Swarm&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=swarm&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Assassin Squad Micro AI (ai_type=assassin) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|6}}&lt;br /&gt;
&lt;br /&gt;
This Micro AI controls one or several assassin units which try to circle around enemy units with the goal of hitting a target unit without being distracted or impeded by other enemies.  The units will always attack the target if such attacks are possible, otherwise they will follow the path of least resistance toward it. Notes:&lt;br /&gt;
&lt;br /&gt;
* The main priority for the AI is to find paths that avoid enemy units. However, if no such path can be found, the assassins will still advance toward the target, taking the path exposing the units to the least potential damage from enemies.&lt;br /&gt;
&lt;br /&gt;
* The units will not necessarily follow the same path. The best path is chosen individually for each unit.&lt;br /&gt;
&lt;br /&gt;
* While it is not a requirement for the AI to work, using ambusher or skirmisher units, or units with the quick trait, makes this AI more lethal in many cases.&lt;br /&gt;
&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded, it does not work otherwise. That means that '''ca_score''' is not a valid key for the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=assassin&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Assassin Squad specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the assassin units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the target unit.&lt;br /&gt;
** It is important to realize that the AI currently only works correctly with a single target unit. It is thus the responsibility of the scenario designer to make sure that this filter matches an individual enemy unit. If several enemy units are to be targeted in the same scenario, several instances of the Assassin Micro AI can be used.&lt;br /&gt;
** The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[prefer]''': A [[StandardLocationFilter|Standard Location Filter]] selecting locations on the map for the assassins to prefer when making their way toward the target unit. This could be used, for example, to make units with the ambush ability move (mostly) through forest or to have the assassins circle around the, say, northern part of the map rather than the south if that is desirable for scenario design purposes.&lt;br /&gt;
** As with avoiding enemies, the units will still move across non-preferred terrain if that is either the only option or ''much'' shorter than preferred terrain&lt;br /&gt;
** Attacking, once the target unit is in range, is not influenced by the [prefer] SLF.&lt;br /&gt;
&lt;br /&gt;
Here is an example of an Assassin Squad [micro_ai] tag usage:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=assassin&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Assassin,Master at Arms,Duelist&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             [prefer] # Circle around northern part of map&lt;br /&gt;
                 x=1-9, 10-99&lt;br /&gt;
                 y=1-37, 1-17&lt;br /&gt;
             [/prefer]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Bottleneck Defense Micro AI (ai_type=bottleneck_defense) ==&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense Micro AI lets you define a location on the map where the AI can take a defensive stance behind a narrow passage (bottleneck).  Units on the front line are backed up by healers and/or units with the leadership ability.  Injured units are moved to the back and replaced by uninjured (or less injured) units.  The units on the front line only attack when it is safe (no retaliation) or when there is a high chance that they will make a kill or level up.  Using this Micro AI only makes sense if there is no way for the enemy sides to move around the bottleneck and attack the AI's units from behind.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Bottleneck&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Bottleneck Defense Micro AI by using&lt;br /&gt;
 ai_type=bottleneck_defense&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Bottleneck Defense specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense AI requires two sets of coordinates that define where it should be taking up its defensive stance, and from which side the enemy attacks at that location.&lt;br /&gt;
&lt;br /&gt;
* '''x,y''': Comma separated lists of the hexes on the front line, where strong units are placed.  All hexes on which the AI makes contact with (can be attacked by) the enemy need to be included here.  Units are placed on them in the order in which they are listed here. {{DevFeature1.15|0}} '''location_id''' can be used instead with named/special locations.&lt;br /&gt;
* '''enemy_x,enemy_y''': Comma separated list of the hexes from which the enemy can attack the AI front line units.  This is needed for the AI to know on which side of the front line support units (healers etc.) need to be placed.  In many cases, it is sufficient to provide only one of the enemy front line hexes, but there are geometries for which that does not work.  It is therefore safer to list all enemy front line hexes here. {{DevFeature1.15|0}} '''enemy_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
By default, the AI places healers and units with the leadership ability both on the front line itself (if they are the strongest units available) and on hexes adjacent to and behind the line.  If different placement is desired, these locations can be overridden with the following keys.&lt;br /&gt;
&lt;br /&gt;
* '''active_side_leader'''=no: (boolean)  If set to 'yes', the side leader participates in the bottleneck defense action after the side's gold has been spent.  If set to 'no' (default), the leader follows default AI behavior (sitting on the keep and recruiting, for the most part).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Bottleneck Defense AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''healer_x,healer_y''': Comma separated list of hexes where healers are placed.  This can be used to keep healers from the front line (or to take up only certain positions on the front line), and/or to override the default healing positions behind the line.  Healers are placed on them in the order in which the hexes are listed, with the exception that hexes on the line always take priority over hexes behind the line. {{DevFeature1.15|0}} '''healer_loc''' can be used instead with named/special locations.&lt;br /&gt;
* '''leadership_x,leadership_y''': Same as ''healer_x,healer_y'', but for units with the leadership ability. {{DevFeature1.15|0}} '''leadership_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
As an example, in the &amp;quot;Bottleneck&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]], there are three front line hexes, two of which are on hill terrain, while the third is a road with a lower defense rating.  The healer and side leader are supposed to participate in combat because they are strong units, but only from the hill hexes to keep them safer.  This is accomplished by using the following keys settings (check out the scenario to see which hex is which):&lt;br /&gt;
 [micro_ai]&lt;br /&gt;
     side=1&lt;br /&gt;
     ai_type=bottleneck_defense&lt;br /&gt;
     action=add&lt;br /&gt;
 &lt;br /&gt;
     x=14,14,14&lt;br /&gt;
     y= 7, 9, 8&lt;br /&gt;
     enemy_x=13,13&lt;br /&gt;
     enemy_y= 8, 9&lt;br /&gt;
 &lt;br /&gt;
     healer_x=14,14,15,15&lt;br /&gt;
     healer_y= 7, 9, 8, 9&lt;br /&gt;
     leadership_x=14,14,15,15&lt;br /&gt;
     leadership_y= 7, 9, 9 ,8&lt;br /&gt;
     active_side_leader=yes&lt;br /&gt;
 [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Fast Micro AI (ai_type=fast_ai) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|0}}&lt;br /&gt;
&lt;br /&gt;
This AI is meant as a fall-back AI for scenarios where there are so many units that attacks and moves done by the default AI are slow with long delays before each move.  It replaces the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions (CAs).  It should be obvious that in order to achieve the faster speed, compromises concerning the quality of the moves have to be made.&lt;br /&gt;
&lt;br /&gt;
Enable the Fast Micro AI by using&lt;br /&gt;
 ai_type=fast_ai&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Combat candidate action:'''&lt;br /&gt;
* Attackers are considered individually one at a time.  The best attack found for a unit is executed without checking whether a better attack by another unit might be possible.&lt;br /&gt;
* The default order in which attackers are considered is by decreasing hitpoints, starting with the strongest unit (in terms of HP).  This can be modified with the optional keys below.&lt;br /&gt;
* By default, if weaker units block attack hexes for a stronger unit, they do not move out of the way (this is the same as for the default AI).  If a hex becomes available after an attack (e.g. due to a weaker unit dying in the attack), all attacks that were possible ''before'' this happened are finished first.  Only after that are attacks by the stronger unit reconsidered.&lt;br /&gt;
* If include_occupied_attack_hexes=yes is set, weaker units move out of the way for stronger units if possible.  This can, however slow down the AI depending on the situation on the map.  Use with caution.&lt;br /&gt;
* {{DevFeature1.13|2}} Previous to this version, attacks by the leader were bunched in with the attacks by the other units.  Now leader attacks are treated separately and contain an extra layer of analysis to determine whether the attack position is &amp;quot;safe&amp;quot; (in an approximate way).  Currently, this is done with two simple considerations:&lt;br /&gt;
** Count all enemy units that can reach the leader's attack position.  If there are more than given by optional parameter 'leader_attack_max_units' (default value 3), do not attack.&lt;br /&gt;
** Add up the hitpoints of all enemies that can reach the leader's attack position.  If this value, multiplied by optional parameter 'leader_weight'(default value 2), is larger than the leaders hitpoints, do not attack.&lt;br /&gt;
** Two optional parameters, 'threatened_leader_fights' and 'leader_additional_threat', can also be used to let the leader attack when the threat at the attack hex is comparable to that at the leader's current position.&lt;br /&gt;
** In order to keep this AI as fast as possible, leader attacks are done by a separate candidate action that is only evaluated when all other CAs are done.&lt;br /&gt;
&lt;br /&gt;
'''Move-to-targets, villages and retreat candidate actions:'''&lt;br /&gt;
* The move-to-targets, villages and retreat candidate actions of the default AI are removed.  Move-to-targets and villages are taken over by the Fast Micro AI, whereas retreat is simply not done.&lt;br /&gt;
* This CA moves units toward goals after attacks are done.  Goals are unowned (if the AI side has a leader only) and enemy-owned villages and enemy leaders.&lt;br /&gt;
* For each goal, units are pre-sorted in order of the fewest moves needed to cover the ''distance'' toward the goal (that is, as if move cost on each hex toward the goal were 1).  Units are then considered sequentially and if a unit can get there &amp;quot;efficiently&amp;quot;, its move is executed without considering other units.&lt;br /&gt;
** A move is considered efficient if the move cost to get to the hex is not more than 'move_cost_factor' larger than the distance to the hex.  'move_cost_factor' is an optional key that defaults to 2.  It can be set to values of 1.1 or larger (see below).&lt;br /&gt;
** If the move for the current unit is not found to be efficient, the AI considers the next unit instead.  If no unit can do an efficient move toward the goal, the best (fastest) of the inefficient moves is selected instead.&lt;br /&gt;
&lt;br /&gt;
* Village goals:&lt;br /&gt;
** Village goals are considered before enemy leader goals so that the AI preferentially chooses scouts (or otherwise fast units) for them.&lt;br /&gt;
** The number of units sent toward villages depends on the value of the default AI aspect village_value.  It is set to village_value/4 times the number of units the AI has left to move.  Thus, by default (village_value=1.0) the AI sends a quarter of its units toward villages, and the rest toward enemy leaders.  This means that the meaningful range of values for village_value is 0..4.&lt;br /&gt;
** Only one unit is moved toward each village.  This consideration also overrides the number of units to move toward villages defined by village_value, topping it at the number of unowned/enemy-owned villages on the map.&lt;br /&gt;
** Setting village_value=0 disables moving units toward villages altogether.  That is, not only are no units chosen, but the village targeting phase of the AI is entirely disabled with no evaluation time used for it.  Note that, unlike with the default AI, this also means that no villages within range of an AI unit are grabbed.&lt;br /&gt;
** The order in which villages are chosen as goals is some combination of them being close to the AI side's leader and being spread out over the map.&lt;br /&gt;
&lt;br /&gt;
* Enemy leader goals:&lt;br /&gt;
** The order of enemy leaders goals is set according to their distance from the AI leader with the closest enemy leader being targeted first (if the AI has a leader, otherwise their order is random).  One unit is sent toward each enemy leader in this order, after which the AI starts with the first enemy leader again.  This is repeated until all units have moved.&lt;br /&gt;
&lt;br /&gt;
* Guardians (units with ai_special=guardian set) are ignored by the fast move-to-targets CA.&lt;br /&gt;
&lt;br /&gt;
'''Tips for speeding up/modifying the Fast Micro AI in specific scenarios:'''&lt;br /&gt;
&lt;br /&gt;
A number of optional keys and default AI aspects can be used to modify the behavior of the Fast Micro AI.  The MAI is set up so that it is a reasonable compromise between speed and quality of the moves in most situations.  However, this might not be ideal for all scenarios here are some things that can be done to speed up the AI (or to get better behavior in some situations by doing the opposite):&lt;br /&gt;
* Use large values for the default AI aspect aggression.  The largest delays before attacks happen (in some circumstances) when the Fast MAI has to consider lots of units with attacks left but decides not to attack with them.  See below for a description of how aggression is used by the fast AI.&lt;br /&gt;
* Use large values of move_cost_factor (see above for description of what this does).  If you want to emphasize units that can move across terrain easily (e.g. bats) to be chosen for village grabbing, you might want to use low values of move_cost_factor, but this might slow down the AI.  The default value should work reasonably well in most standard situations, but modifying the value might work better on specific maps.&lt;br /&gt;
* Don't use dungeon_mode=yes unless absolutely necessary, even in most dungeon crawls.  Test the AI without this setting first and only use it if it is the only way to solve the problem.  Often, a minor tweak to the map will have the same effect, without the speed penalty.&lt;br /&gt;
* Use include_occupied_attack_hexes=yes with caution.  It might (or might not) slow down the attack CA significantly, depending on the scenario.&lt;br /&gt;
* Set village_value=0, which makes the AI not consider villages as goals.  This has not been found to make a large difference on the maps tested so far, but it might have an effect on maps with a very large number of villages.&lt;br /&gt;
* Use attack_hidden_enemies=yes on maps where there are no hidden enemies. Excluding hidden enemies from the attack calculation requires an additional filter to be evaluated that takes extra time. In most cases this will not result in a large computation time saving.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_hidden_enemies'''=no: (boolean)  By default, hidden enemies are excluded from the attack calculation.  Setting this to 'yes' makes the AI also consider those enemies as attack targets.&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.13|2}} A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  As with the default AI, units will not step on hexes defined by the [avoid] tag.  In addition, no goals will be chosen that lie on those hexes.  The one minor exception is that a unit stepping out of the way for another unit might temporarily enter the avoided zone.&lt;br /&gt;
* '''dungeon_mode'''=no: (boolean)  As much as possible, the Fast Micro AI works with distances between hexes rather than path finding.  This works pretty well on open maps and even most dungeon crawls (and similar scenarios), but it might result in some units getting temporarily (and in rare cases permanently) stuck on maps with convoluted labyrinths.  If that is the case, setting dungeon_mode=yes triggers more path finding that avoids such situations (but as a result is somewhat slower).&lt;br /&gt;
* '''include_occupied_attack_hexes'''=no: (boolean) If set to 'yes', the AI moves weaker units out of the way for a stronger attacker.  This might lead to better attacks but is slower, so don't use if speed is essential.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the attackers.  Only AI units matching this filter will attack, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes the side= key set to the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the enemies.  Only enemy units matching this filter will be attacked, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''leader_weight'''=2: (number &amp;gt;= 0) {{DevFeature1.13|2}} A factor specifying how much more valuable the AI considers side leaders than non-leader units in the attack evaluation. This is also used to determine whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''leader_additional_threat'''=1: (number &amp;gt; 0) {{DevFeature1.13|2}} If threatened_leader_fights=yes, the threat at the attack position may be larger by this factor for the leader to leave the keep for an attack.&lt;br /&gt;
* '''leader_attack_max_units'''=3: (number &amp;gt;= 0) {{DevFeature1.13|2}} A number specifying how many enemy units within reach are acceptable when determining whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''move_cost_factor'''=2: (number &amp;gt;= 1.1)  The factor by which the move cost to a goal hex may be larger than the distance to the goal hex for a move to be considered &amp;quot;efficient&amp;quot;.  See description above.  1.1 is used if this is set to values smaller than 1.1.  Smaller values might lead to better behavior, while larger values might speed up the AI.&lt;br /&gt;
* '''weak_units_first'''=no: (boolean) If set to 'yes', inverts the order in which units are considered by the combat CA.  Attacks are then done in order of increasing hitpoints of the attackers.&lt;br /&gt;
* '''skip_combat_ca'''=no: (boolean) If set to 'yes', the fast combat CA is not used.  The default AI's high_xp_attack and combat CAs remains in place in this case.&lt;br /&gt;
* '''skip_move_ca'''=no: (boolean) If set to 'yes', the fast move-to-targets CA is not used.  The default AI's retreat, villages and move-to-targets CAs remains in place in this case.&lt;br /&gt;
* '''threatened_leader_fights'''=no: (boolean) {{DevFeature1.13|2}} By default, the AI leader will not attack if the threat at the attack position is too high.  If this key is set to 'yes', the leader will attack if the threat there is not larger than the threat at his current position multiplied by the value of 'leader_additional_threat'. This is meant as an option for the endgame: when the AI leader is under attack, he might as well go down fighting.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''aggression''': The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to set how aggressive the AI should attack.  It works in roughly the same way as the default AI aspect:&lt;br /&gt;
** The value of (1 - aggression) determines how much the AI values its own units compared to the enemy's units.  Thus, aggression=0 means that attacks are only done if the AI expects to deal (roughly) the same amount of damage as it will receive.  aggression=1 means that expected damage to the AI's units is not considered, only the expected damage to enemy units matters.&lt;br /&gt;
** Damage is calculated as a combination of hitpoints and chance to die, in &amp;quot;gold equivalent units&amp;quot;.&lt;br /&gt;
** Unlike the default AI, aggression is set to 1 for values larger than 1.  Allowed values are thus anything from -infinity to +infinity, although only -infinity to +1.0 make sense.&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.13|2}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
*'''attacks aspect''':  {{DevFeature1.13|2}} The standard RCA AI [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] used to filter own and enemy units for attacks is now also taken into account as long as neither [filter] nor [filter_second] (see above under 'optional keys') are defined in the [micro_ai] tag.&lt;br /&gt;
*'''village_value=1''': (number ranging from 0 to 4) The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to define how much villages should be targeted by the move-to-targets CA.  In the Fast MAI, this defines which fraction of the AI units are moved toward village goals (see above for details).  If set to 0, villages are ignored as goals.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded to the same values as those of the respective [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|default candidate actions]].  ca_score is therefore not a valid optional key.&lt;br /&gt;
* The fast Micro AI modifies (removes) the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions.&lt;br /&gt;
&lt;br /&gt;
Enabling the Fast Micro AI in its default configuration is as simple as this usage example from the &amp;quot;Fast AI&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=fast_ai&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Generalized Goto Micro AI (ai_type=goto) ==&lt;br /&gt;
&lt;br /&gt;
The Goto Micro AI provides a highly configurable method of sending a unit (or units) to a location or set of locations.  The units to be moved are defined using a [[StandardUnitFilter|Standard Unit Filter]] (SUF) [alternatively, all side units are used if the SUF is not provided), while the goto locations are given in a [[StandardLocationFilter|Standard Location Filter]] (SLF).&lt;br /&gt;
&lt;br /&gt;
By default, each unit matching the SUF will move toward the closest goal (in terms of turns needed to get there) matching the SLF via the fastest route given the current situation on the map (locations of enemy units etc.), but this behavior can be influenced by the optional keys listed below.  Note that this means that a unit's goal might change from one turn to the next depending on the situation on the map.&lt;br /&gt;
&lt;br /&gt;
The behavior of the unit(s) once they reach their goal location(s) is configurable as well.  By default, they will stay there until the Goto Micro AI is deleted or changed.&lt;br /&gt;
&lt;br /&gt;
For demonstrations of several Goto Micro AI usages, check out the &amp;quot;Goto&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Goto Micro AI by using&lt;br /&gt;
 ai_type=goto&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Goto specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the possible goal locations for the move.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists). Note that the area defined by [avoid] is not entered at all, even if the end point of the move would lie outside the area again.&lt;br /&gt;
* '''avoid_enemies''': (number&amp;gt;0) By default, units controlled by the Goto Micro AI will not take enemy units into account for route finding, other than the enemies' ZoCs.  By contrast, if this key is set, they will try to avoid traversing hexes on which they can be attacked by enemies.  The larger the value of ''avoid_enemies'', the more the units will try to stay out of the enemies' way.  Notes:&lt;br /&gt;
** Units are willing to take a path that is 'avoid_enemies' movement points longer for each enemy unit that can be avoided by doing so for each hex along the path.  As an example, if avoid_enemies=1 and the shortest path crosses 2 hexes that can be reached by 3 enemies each, the unit is willing to take a path that is up to 6 hexes longer instead.  If avoid_enemies=10 in the same situation, the unit will take a path up to 60 hexes longer.&lt;br /&gt;
** Hexes adjacent to enemy units are always strongly avoided, independent of the value of avoid_enemies.&lt;br /&gt;
** avoid_enemies is ignored if 'use_straight_line=true' is set.&lt;br /&gt;
** Due to an issue with the path finding code, '''tunnels and teleports are ignored in Wesnoth 1.14 and before''' if 'avoid_enemies' is used. This has been fixed in Wesnoth 1.15.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units to be moved.  Defaults to all units of the side if not defined.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''ignore_enemy_at_goal'''=no and '''ignore_units'''=no: (boolean)  In the default configuration, the Goto MAI does not move units if the path to all goal hexes is blocked by enemies.  It is therefore possible to disable the Goto MAI by putting enemy units on all the goal hexes.  That can be changed if either of these two keys is set to 'yes'.&lt;br /&gt;
** 'ignore_enemy_at_goal=yes' makes the AI choose its goals while disregarding any enemy units at the goal hexes.  Even so, a stiff rating penalty is applied to goal hexes occupied by enemies, thus that unoccupied goal hexes are chosen unless all possible goals are occupied by enemies.  Note that this setting ''only'' ignores enemy units at the goal hexes themselves.  It is still possible to disable the AI by putting enemies all around the goals.&lt;br /&gt;
** 'ignore_units=yes' let's the AI ignore ''all'' enemy units when deciding which goal hex(es) to move toward.&lt;br /&gt;
** In both cases, enemies are only ignored for deciding which unit to move toward which goal hex.  For the actual move itself, all enemies and their ZoCs are considered as usual.&lt;br /&gt;
** Both options only take effect if neither avoid_enemies nor use_straight_line is set to 'yes', as neither of these consider enemies as blockers.&lt;br /&gt;
* '''release_unit_at_goal'''=no: (boolean) By default, a unit that reaches a goal location will stay there until the Goto Micro AI is deleted or changed (or until the conditions specified in the [goto_goals] SLF change).  If this parameter is set, a unit that has reached its goal will not be controlled by this Micro AI any more starting from the next turn.&lt;br /&gt;
* '''release_all_units_at_goal'''=no: (boolean) Same as ''release_unit_at_goal'', but this will deactivate the Goto Micro AI for all units once the first unit reaches a goal.&lt;br /&gt;
* '''unique_goals'''=no: (boolean)  If set to 'yes', each unit will be sent to a different goal location.  Note that the best goals are recalculated each turn, so a unit might choose a different goal this turn from what it was heading for during the previous turn if the situation on the map has changed.  Also note that if there are more units than goal locations, the left over units will not be handled by the Goto MAI but follow default behavior instead.&lt;br /&gt;
* '''use_straight_line'''=no: (boolean)  By default, units choose the route by which they will get to their goal locations in the smallest number of turns.  If this parameter is set to 'yes', a straight line route (or something pretty close to it at least) will be chosen instead, irrespective of whether this leads into a dead end, whether it is blocked by enemies, etc.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* Depending on the parameters used, the Goto Micro AI might require a lot of path finding to be done.  Thus, while it is possible to set up this AI to move many units to many different locations, a usage like that might require quite a long calculation time.&lt;br /&gt;
&lt;br /&gt;
===Goto Micro AI example usages:===&lt;br /&gt;
&lt;br /&gt;
Here are several examples of goto [micro_ai] tag usages from the &amp;quot;Goto&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
* Send a unit toward a castle (similar to the RCA AI's Goto Candidate Action, but avoiding dead ends and areas ZoCed by enemies)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
             ca_id=messenger1&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=messenger1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=6-7,2-4&lt;br /&gt;
                 terrain=C*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             ca_score=210001&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send all units of the side toward the southern border of the map&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=8&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                  y=33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             avoid_enemies=1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send units to the four corners of the map (with each unit going to a different corner).&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x=1,1,44,44&lt;br /&gt;
                 y=1,33,1,33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             unique_goals=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Set up a guardian unit that doesn't move while no enemy unit is within 8 hexes (ironically, the Goto Micro AI can be used to keep units in place, although using one of the Guardian Micro AIs might often be a better idea)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=guard1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 [filter]&lt;br /&gt;
                     id=guard1&lt;br /&gt;
                 [/filter]&lt;br /&gt;
                 [not]&lt;br /&gt;
                     [filter]&lt;br /&gt;
                         side=1&lt;br /&gt;
                     [/filter]&lt;br /&gt;
                     radius=8&lt;br /&gt;
                 [/not]&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Guardian Micro AIs ==&lt;br /&gt;
=== General Comments on the Guardian Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Guardian Micro AIs can be used to set up different kind of guardian behaviors for a unit or a set of units.  Four different types of guardians are available and are described below.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different guardian behaviors, check out the &amp;quot;Guardians&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
=== Return Guardian  (ai_type=return_guardian)===&lt;br /&gt;
&lt;br /&gt;
A 'return guardian' is a variation of the standard Wesnoth guardian. It has an assigned guard position (GP) to which it returns after attacks on approaching enemies:&lt;br /&gt;
* If at GP with no enemy in reach, do nothing.&lt;br /&gt;
* If at GP with an enemy in reach, attack. Note that the attack is done by the default AI, which might result in the guardian not attacking if the enemy is deemed too strong (this can be influenced by setting the default ''aggression'' value to a different value).&lt;br /&gt;
* If not at GP, return there, no matter whether an enemy is in reach or not.&lt;br /&gt;
* If enemies are blocking the way back to GP, do your best to move around them.&lt;br /&gt;
* If the guardian ends up next to an enemy on the way back, attack that enemy after the move.&lt;br /&gt;
&lt;br /&gt;
Enable the Return Guardian Micro AI by using&lt;br /&gt;
 ai_type=return_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''return_x,return_y''': The coordinate of the hex the unit returns to after each attack.  {{DevFeature1.15|0}} '''return_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=100010: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  This AI consist of 2 CAs, one with score &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt; the other with &amp;lt;tt&amp;gt;ca_score-20&amp;lt;/tt&amp;gt;.  The default value is set so that the first CA gets executed right before the default combat CA, and the other right after.  It usually will not make sense to change this value, but it is possible to do so for custom applications.  {{DevFeature1.13|6}}: This is now changed to 100100 and 99900 in order to avoid conflicts with the new high_XP_attacks candidate action.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a return guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=return_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=return1&lt;br /&gt;
             return_x,return_y=20,2&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
===Stationed Guardian (ai_type=stationed_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A 'stationed guardian' is another variation of the standard Wesnoth guardian with a somewhat more complex behavior than that of the 'return guardian'. Two positions are defined for it, a 'station' and a 'guarded location', as well as a distance. The behavior is as follows:&lt;br /&gt;
* If no enemy is within 'distance' of the guard's current position, do nothing.  This is independent of the guardian being at its station or not.&lt;br /&gt;
* Otherwise: If an enemy is within 'distance' of the guard, but not also within the same distance of the guarded location and the station (all of this simultaneously), move the guard in the direction of the station (or keep the guard in place if the station is unreachable).&lt;br /&gt;
* Otherwise:&lt;br /&gt;
** Pick the enemy unit that is closest to the guarded location.&lt;br /&gt;
** If we can reach it, pick the adjacent hex with the highest defense rating and attack from there.&lt;br /&gt;
** If not in reach, move toward this unit.&lt;br /&gt;
&lt;br /&gt;
Enable the Stationed Guardian Micro AI by using&lt;br /&gt;
 ai_type=stationed_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance parameter as explained above&lt;br /&gt;
* '''station_x,station_y''': The x and y position of the hex that serves as the guardians station. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''guard_x, guard_y''': The x and y position of the guarded location.  {{DevFeature1.13|0}}  These are now optional keys, defaulting to station_x,station_y if not provided. {{DevFeature1.15|0}} '''guard_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a stationed guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=stationed_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=stationed1&lt;br /&gt;
             distance=4&lt;br /&gt;
             station_x,station_y=2,14&lt;br /&gt;
             guard_x,guard_y=3,13&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Coward (ai_type=coward) ===&lt;br /&gt;
&lt;br /&gt;
Cowards are units that, like guardians, sit around doing nothing until an enemy comes into range.  Unlike guardians, however, they run away once enemies approach. Applications might be wild animals, unarmed civilians getting in the way of a battle, etc.  The coward macro can be called with two optional locations, 'seek' and 'avoid':&lt;br /&gt;
* If neither is given, the coward retreats to the position farthest away from the approaching enemies.&lt;br /&gt;
* If 'seek' is given, it preferentially goes toward that location (but getting away from enemies takes priority).&lt;br /&gt;
* If 'avoid' is given, it in addition tries to avoid that location (with both maximizing distance from enemies and going toward 'seek' taking priority).&lt;br /&gt;
* Both 'seek' and 'avoid' may consist of only one coordinate ('x' or 'y'), in which case not a single hex, but a line of hexes is sought or avoided.&lt;br /&gt;
&lt;br /&gt;
Enable the Coward Micro AI by using&lt;br /&gt;
 ai_type=coward&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance within which an enemy must be to &amp;quot;scare&amp;quot; the unit&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_if_trapped'''='no':  {{DevFeature1.13|0}}  With the default settings, a coward never attacks.  If this key is set to 'yes', the coward attacks the weakest (lowest hitpoints) enemy next to which it ends up after moving (such as a cornered animal would do, blindly lashing out without regards for its own safety).&lt;br /&gt;
* '''avoid_x, avoid_y''': The x and y coordinate of the hex to avoid. {{DevFeature1.15|0}} '''avoid_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units from which the cowards flee.  If not set, this defaults to all enemy units.  Note, however, that no &amp;quot;enemy of AI side&amp;quot; condition is added if [filter_second] is specified, meaning that it can include allied and/or enemy units as desired (and even units on the same side as the coward).&lt;br /&gt;
* '''seek_x, seek_y''': The x and y coordinate of the hex to seek. {{DevFeature1.15|0}} '''seek_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a coward [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=coward&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=coward3&lt;br /&gt;
             distance=5&lt;br /&gt;
             seek_x,seek_y=24,5&lt;br /&gt;
             avoid_x,avoid_y=24,15&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Zone guardian (ai_type=zone_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A zone guardian is a unit that, as the name says, guards a zone. It moves randomly inside this zone until an enemy enters it (or a separately defined enemy zone, see below). Applications might be the defense of a castle, a nesting area or for protecting an unit.  The zone macro can be called with an optional SLF, [filter_location_enemy]:&lt;br /&gt;
* If not specified, the zone guard attacks any enemy coming inside its guard zone.&lt;br /&gt;
* If [filter_location_enemy] is given, it attacks any enemy entering this zone and once there are no more enemies, it goes back to patrol in its basic zone.&lt;br /&gt;
&lt;br /&gt;
Enable the Zone Guardian Micro AI by using&lt;br /&gt;
 ai_type=zone_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone to guard.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_enemy]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone where enemies have to be attacked.  If not given, it defaults to [filter_location].&lt;br /&gt;
* '''station_x,station_y''': By default, the zone guardian moves randomly within the zone if there is no enemy to attack.  However, if station_x/y is given, it moves to this position instead during turns when it does not have an enemy to attack.  This position does not need to be inside the zone defined above.  Note: obviously it does not make sense to define [filter_location], [filter_location_enemy] and station_x/y all at the same time.  Only [filter_location] and either one of the latter two (or none of them) should be provided for any zone guardian. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a zone guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=zone_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=zone3&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=22-31,4-11  # This is intentionally chosen to extend past the lake&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             station_x,station_y=32,8&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Hang Out Micro AI (ai_type=hang_out) ==&lt;br /&gt;
&lt;br /&gt;
The Hang Out Micro AI instructs units of the AI side to &amp;quot;hang out&amp;quot; around a certain location, or locations, until a condition for mobilizing the side is met.  If none of the optional keys are given, the AI controls all units of the side to hang around the leader while moving off castle tiles to make room for recruiting.&lt;br /&gt;
&lt;br /&gt;
The AI works by moving each unit to its best hex first using the minimum amount of movement points required for this move.  Once all AI units are in position, movement points are set to zero for all of them simultaneously such that they cannot be used by other AI candidate actions.  This is done because the mobilizing condition might be met as result of one of the moves of the Hang Out AI, after which we want units to be able to continue with other AI actions.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Hang Out and Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Hang Out Micro AI by using&lt;br /&gt;
 ai_type=hang_out&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Hang Out specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  In its default configuration, the Hang Out MAI controls all units of a side, keeping them as close to the side leader(s) as possible while moving off castle tiles to make room for recruiting.  For this purpose, the candidate action score defaults to 170,000, which is just below the score of the default recruiting CA.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''': A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  If neither of these two exists, this parameter is set to default to all castle terrain (&amp;lt;tt&amp;gt;terrain=C*,C*^*,*^C*&amp;lt;/tt&amp;gt;) so that units move off the castle to make room for recruiting.  Note that this does not include keep terrain, otherwise the leader would be moved off the keep as well.&lt;br /&gt;
* '''ca_score'''=170000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 170,000, which means that it is executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Hang Out action (&amp;lt;tt&amp;gt;side=&amp;lt;/tt&amp;gt; is always set and does not need to be provided).  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the locations ''near'' which the AI units should hang out.  This does not mean that the units only move onto terrain specified here, but that they minimize the distance to such terrain.  It defaults to the position of the side leader(s).&lt;br /&gt;
* '''[mobilize_condition]''':  Contains ([[ConditionalActionsWML#Condition_Tags|WML Condition Tags]])  Once the condition set up here has been met, the Hang Out MAI is disabled and the side goes over to other AI activity.  The Hang Out MAI remains disabled after this, even if the condition is not met any more later in the scenario.&lt;br /&gt;
* '''mobilize_on_gold_less_than''': (number) Since side gold cannot be checked in a WML condition tag without storing it in a variable first, this key is provided separately.  It disables the Hang Out Micro AI once the side gold goes below the value specified here for the first time.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
* The Hang Out AI still takes effect if no locations to hang out at are found or if all map locations are to be avoided.  In that case, it simply keeps all units at their current locations.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Hang Out Micro AI configuration from the &amp;quot;Hang Out and Messenger&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hang_out&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [avoid]&lt;br /&gt;
                 terrain=C*,H*,M*,A*,S*,*^F*&lt;br /&gt;
             [/avoid]&lt;br /&gt;
             [mobilize_condition]&lt;br /&gt;
                 [have_unit]&lt;br /&gt;
                     side=1&lt;br /&gt;
                     count=7-99&lt;br /&gt;
                 [/have_unit]&lt;br /&gt;
             [/mobilize_condition]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Healer Support Micro AI (ai_type=healer_support) ==&lt;br /&gt;
&lt;br /&gt;
The Healer Support Micro AI configures the healers of a side to stay behind the battle lines and heal injured and/or threatened units rather than participate in combat under all circumstances.  You can set different levels of aggressiveness for the healers.  &lt;br /&gt;
&lt;br /&gt;
'''Notes'''&lt;br /&gt;
* By default, ''all'' healers of the AI side are controlled by this AI.  This includes in particular also the side leader, if the leader is a healer.  If you do not want the leader to be included (and possibly be moved away from the keep etc.) use the [filter] tag with canrecruit=no (see below).&lt;br /&gt;
* It is not always better to use this Micro AI than the default AI, as it takes healers (which can be strong units) away from the pool of units to choose for attacks.&lt;br /&gt;
* {{DevFeature1.15|3}} This Micro AI now takes unit guardian status and the passive_leader aspect into account.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Healers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Healer Support Micro AI by using&lt;br /&gt;
 ai_type=healer_support&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Healer Support specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the healer units.  If set, only units on the AI side passing this filter ''and'' having the healing ability are considered by this Micro AI.&lt;br /&gt;
*'''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting units to receive healing. If set, only units on the AI side passing this filter ''and'' the other healee selection criteria will be selected for healing by this Micro AI.&lt;br /&gt;
* '''aggression'''=1.0: (float) Sets the aggressiveness of the AI.  This parameter is set up as a float to accommodate future functionality, but it currently acts as a boolean: if set to zero, the AI will never let its healers participate in combat, for all other values it allows them to attack after all other units have attacked and if the healers cannot find any more units to support.  The former behavior might be appropriate in scenarios with only one or few valuable healers, while the latter might work better in scenarios with many healers.&lt;br /&gt;
* '''injured_units_only'''=no: (boolean) If set to 'yes', the AI will only move healers next to units that are already injured and skip units that currently have full hitpoints, but might get injured during the next enemy turn.&lt;br /&gt;
* '''max_threats'''=9999: (integer) The maximum allowable number of enemies that can attack a hex in order for it to be considered for a healer.  As an example, setting 'max_threats=0' means that the AI only moves healers to locations that are entirely safe from the enemy (assuming that none of the units currently on the map dies).  Note that the value of this key is checked against the number of enemies that can make it to the hex, not the number of adjacent hexes from which the healer could be attacked.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI. Note: as attacks by the healers are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The Healer Support Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded.  It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Healer Support Micro AI configuration from the &amp;quot;Healers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             aggression=0&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Lurkers Micro AI (ai_type=lurkers) ==&lt;br /&gt;
&lt;br /&gt;
The Lurker Micro AI controls some or all units of a side in &amp;quot;lurker mode&amp;quot;.  A lurker is a unit that is capable of moving across most terrains, but that only stops on and attacks from specific terrain.  It might also have the ability to hide on this terrain (which is the reason why it is called a lurker).&lt;br /&gt;
&lt;br /&gt;
Lurkers move individually without strategy and always attack the weakest enemy within their reach.  If no enemy is in reach, the lurker does a random move instead - or it just sits and waits (lurks).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of different types of lurker behavior, check out the &amp;quot;Lurkers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Lurkers Micro AI by using&lt;br /&gt;
 ai_type=lurkers&lt;br /&gt;
in the [micro_ai] tag&lt;br /&gt;
&lt;br /&gt;
===Lurkers specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
* '''[filter]''': Specifies which units of the side are controlled by the Lurker Micro AI.  This is a [[StandardUnitFilter|Standard Unit Filter]].  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': Specifies the terrain from which the lurkers attack. This is a [[StandardLocationFilter|Standard Location Filter]].&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_wander]''': Specifies the terrain on which lurkers end their moves if there is no enemy in reach. This is a [[StandardLocationFilter|Standard Location Filter]].  If [filter_location_wander] is not given, it defaults to [filter_location].&lt;br /&gt;
* '''stationary'''=no: (boolean)  If set to yes, a lurker never moves if there is no enemy within attack range.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Lurkers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=4&lt;br /&gt;
             ai_type=lurkers&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Naga Fighter&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=W*,S*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             [filter_location_wander]&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location_wander]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Messenger Escort Micro AI (ai_type=messenger_escort) ==&lt;br /&gt;
&lt;br /&gt;
The Messenger Escort Micro AI lets you define a set of waypoints through which it tries to move one or several of its units, the messenger(s).  The other units escort the messenger, trying to protect it and attacking enemies that are in its way.  Note that a messenger might not strictly hit intermediate waypoints, but that getting close is good enough, especially if a waypoint is occupied by an enemy unit.  It does, however, always try to get to the last waypoint.&lt;br /&gt;
&lt;br /&gt;
'''Notes on the use of several messengers:''' {{DevFeature1.13|0}}  &lt;br /&gt;
* This Micro AI can control several messengers at the same time, which can but do not have to move in a group.  They can be put on the map altogether in one location, or be spread out on the map or across different turns.&lt;br /&gt;
* The farther a messenger has moved through the chain of waypoints, the higher a priority it is for the AI, meaning that farther advanced (on the map) messengers are moved first, and that the escort units try to clear the way and stay with those messengers more so than with those further back on the map.  This order can be inverted using the invert_order= key.&lt;br /&gt;
* If several messengers are on the map, escort units might move from one to the other, the AI making a judgement call where they are needed most.  Closeness to a messenger is generally the most important criterion, but not the only one.&lt;br /&gt;
* If a messenger dies, the escort units previously staying with it now join one of the other messengers if there are any left (otherwise they follow default AI behavior).&lt;br /&gt;
* If you want certain escort units to stay always with the same messenger, use several messenger [micro_ai] tags with different [filter]/[filter_second] combinations.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Messenger Escort Micro AI by using&lt;br /&gt;
 ai_type=messenger_escort&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Messenger Escort specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': {{DevFeature1.13|0}} (Previous to 1.13.0, only id= was allowed.) The id of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated list of waypoint coordinates through which the messenger will go in the given order to reach its goal, given by the last coordinate.  If you want the messenger to go directly to the goal, simply enter a single x,y coordinate here.  Note that the messenger does not have to hit each waypoint exactly (except for the final one), getting close to them is good enough. {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Messenger Escort AI consists of 3 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-2&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_death_chance'''=0.67 and '''messenger_death_chance'''=0.0: (both floats; ranging from 0 to 1)    If the messenger is adjacent to an enemy at the end of the move, it will only attack the enemy if the chance of death of the enemy is &amp;gt;= enemy_death_chance and if the messenger's chance of death is &amp;lt;= messenger_death_chance (or if it is an attack at a range for which the enemy does not have a weapon).&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|0}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the escort units.  If not set, this defaults to all enemy units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''invert_order'''=no: {{DevFeature1.13|0}} If set to 'yes', the order of priority with which messenger units are considered is inverted, starting with the messenger unit that is the least advanced on the map (see description above).  In most cases, selecting this option results in worse performance of the AI, so use it with caution.  (And obviously it has no effect at all if there is only one messenger controlled by the AI.)&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Messenger Escort&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=messenger_escort&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=messenger&lt;br /&gt;
             waypoint_x=31,24,27,28&lt;br /&gt;
             waypoint_y=20,14,7,1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Patrol Micro AI (ai_type=patrol) ==&lt;br /&gt;
&lt;br /&gt;
The defines a number of waypoints for a unit (or units) to follow.  Options can be set whether the route should be followed in a loop, as an out-and back, only once, whether units encountered along the way should be attacked, etc.&lt;br /&gt;
&lt;br /&gt;
No matter what options are chosen, getting to the next waypoint always takes priority over attacking for a patroller.  The AI thus prefers to move the patrol unit around enemies rather than straight for them.  Also, if a waypoint is occupied by a unit the AI is not instructed to attack, it will (eventually) abandon that waypoint once it gets close enough and move on to the next one.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different patrol behaviors, check out the &amp;quot;Patrols&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Patrol Micro AI it by using&lt;br /&gt;
 ai_type=patrol&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Patrol specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated lists of the waypoint coordinates through which the patrol travels (in order). {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack''': By default (if this key is not set), the patrol unit attacks any unit it ends up next to after a move.  Alternatively, this parameter can be set to a comma-separated list of unit ids. If that is done, the patrol only attacks these units when it ends up next to them and ignores any other unit.  This can in turn be used to have the patroller not attack at all, by setting ''attack='' to a non-existing id.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''one_time_only'''=no: (boolean)  If set to 'yes', the unit goes through the patrol route only once and then hangs out at the last waypoint.  It also always attacks any enemy unit on the last waypoint, independent of what ''attack='' is set to.&lt;br /&gt;
* '''out_and_back'''=no: (boolean)  By default, a patrol unit returns to the first waypoint after reaching the last, doing the patrol route as a loop.  If this parameter is set to 'yes', the unit instead reverses its course and perpetually does out and backs of the route.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Patrols&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=patrol&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Konrad&lt;br /&gt;
             waypoint_x=9,24,25&lt;br /&gt;
             waypoint_y=21,23,15&lt;br /&gt;
             one_time_only=yes&lt;br /&gt;
             attack=Gertburt&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Protect Unit Micro AI (ai_type=protect_unit) ==&lt;br /&gt;
&lt;br /&gt;
The Protect Unit Micro AI tries to move one or several units to a given location while keeping them safe from enemy attacks. It does so by keeping the protected units away from enemy units and close to friendly units. Other units on the AI side are controlled by the default Wesnoth AI.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Protect Unit&amp;quot; and &amp;quot;The Elves Besieged&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Protect Unit Micro AI by using&lt;br /&gt;
 ai_type=protect_unit&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Protect Unit specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[unit]''': A separate [unit] tag is required for each protected unit(s).  Each tag has required keys id=, goal_x=, goal_y= for each unit, as shown in the example below. {{DevFeature1.15|0}} '''goal_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''disable_move_leader_to_keep'''=no: (boolean) If set to 'yes', side leaders protected by this AI do not try to return to their keep.  This is necessary for this Micro AI to work with units that are side leaders and that are supposed to move to an off-keep location.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
*The Protect Unit Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].  Depending on the parameters, it might also modify the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|move leader to keep candidate action]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded. It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;HttT: The Elves Besieged&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=protect_unit&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Delfador&lt;br /&gt;
                 goal_x,goal_y=1,2&lt;br /&gt;
             [/unit]&lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
                 goal_x,goal_y=1,1&lt;br /&gt;
             [/unit]&lt;br /&gt;
 &lt;br /&gt;
             disable_move_leader_to_keep=true&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Recruiting Micro AI==&lt;br /&gt;
=== General Comments on the Recruiting Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Recruiting Micro AI replaces the default recruitment AI by alternative recruiting patterns.  Currently there are two recruiting algorithms you can choose from, random recruiting and the rush recruiting used in the new (as of Version 1.11.1) Experimental AI (see below).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of a different recruiting pattern, check out the &amp;quot;Recruiting&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  You can also explore the unit choices by watching the Experimental AI in a multiplayer game.&lt;br /&gt;
&lt;br /&gt;
'''Note:''' Obviously, the Recruitment Micro AI modifies the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|recruitment candidate action]].&lt;br /&gt;
&lt;br /&gt;
=== Random Recruitment AI (ai_type=recruit_random) ===&lt;br /&gt;
&lt;br /&gt;
This AI randomly selects a unit type from the recruitment list of the side.  The probability of choosing each type and the behavior in low-gold situations can be influenced with optional parameters.&lt;br /&gt;
&lt;br /&gt;
Enable the Random Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_random&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* '''[probability]''': This tag sets the probability of selecting the given unit type.  It has two required sub-keys:&lt;br /&gt;
** '''type''': comma-separated list of unit types to which this probability is to be applied&lt;br /&gt;
** '''probability''': (non-negative float)  The probability with which these unit types is to be selected.  All unit types for which no [probability] tag is defined receive a default probability of 1.0, thus values should be chosen relative to that.  As an example, in the code below swordsmen and peasants are given a probability of 8, while no other units have assigned probabilities.  Thus, each of these two unit types is 8 times more likely to be selected than any other individual unit type.&lt;br /&gt;
* '''skip_low_gold_recruiting'''=no: (boolean)  By default, the random recruitment AI chooses a unit to recruit only from those types for which it has sufficient gold.  If this parameter is set to 'yes', it will instead choose from all available unit types and end recruiting for the turn if there is not enough gold to recruit it this turn (even if other, affordable unit types exist).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a random recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_random&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [probability]&lt;br /&gt;
                 type=Swordsman,Peasant&lt;br /&gt;
                 probability=8&lt;br /&gt;
             [/probability]&lt;br /&gt;
 &lt;br /&gt;
             skip_low_gold_recruiting=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Rush Recruitment AI (ai_type=recruit_rushers) ===&lt;br /&gt;
&lt;br /&gt;
This AI replaces the default recruiting with the rush recruiting used in the new (as of Version 1.11.1) Experimental AI.&lt;br /&gt;
&lt;br /&gt;
Enable the Rush Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_rushers&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* {{DevFeature1.15|2}} '''high_level_fraction'''=0: (non-negative number) The approximate fraction of units of level 2 or higher to be recruited. This is defined as fraction of units on the map, not of new units being recruited during a turn or over several turns (which makes a difference if there are already units on the map). The effect is also cumulative per level (starting from level 2), meaning that 1/3 of L2 units will be recruited in the example above, 1/3*1/3=1/9 L3s etc. The default value is zero, which leaves the high-level unit recruiting to the default Experimental AI algorithm resulting usually in very few such units being recruited.&lt;br /&gt;
* '''randomness'''=0.1: (number)  A random number is applied to the rusher recruit engine's score to prevent the recruitment pattern from being too predictable. 0 causes no randomness to be applied, while larger numbers increase the random effect. A value of 1-2 generates results in which the random effect is approximately equal to the scored effect. Extremely high values are essentially entirely random.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a rush recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_rushers&lt;br /&gt;
             action=add&lt;br /&gt;
          [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Simple Attack Micro AI (ai_type=simple_attack) ==&lt;br /&gt;
&lt;br /&gt;
The Simple Attack Micro AI is meant as an addition to the default combat candidate action for when one wants to force the AI to do attacks that it would otherwise not do (for example on units that are 1 XP from leveling) or to execute certain attacks before all others (for example, attack with all &amp;quot;expendable&amp;quot; units first).  It is not set up to be a replacement for the default attacks.  Some notes:&lt;br /&gt;
&lt;br /&gt;
* The algorithm is different from and simpler than that of the default combat CA.  For example, no attack combinations are considered, only individual attacks.  As a result, the [[AiWML#List_of_AI_Aspects|default aspects]] (aggression, caution, [avoid], ...) are not taken into account.  Also, if lots of units are involved, evaluation of this Micro AI is potentially quite a bit slower than of the default attacks.&lt;br /&gt;
&lt;br /&gt;
* If an attack matching the Standard Unit Filters described below is found, this attack will always be executed, no matter how bad the odds are.  Thus, this Micro AI can be used to force the AI to do attacks it would otherwise avoid.&lt;br /&gt;
&lt;br /&gt;
* Other than selecting the attacker/defender units, this AI is currently not configurable.  Let us know if you would like to see certain options to be added.  (No guarantees that we will add them, but we will definitely consider them.)&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Simple Attack&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=simple_attack&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Simple Attack specific keys for the [micro_ai] tag===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  It is, however, in general not desirable to use this AI without at least one of the optional Standard Unit Filters below, as this AI is not meant as a replacement of the default combat candidate action.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=110000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 110,000, which means that it is executed right before the default combat candidate action.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Simple Attack action.  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units the AI should attack.  It defaults to all enemy units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
* '''weapon'''=-1: (integer) The number of the weapon that should be used for this attack.  Weapon numbers start at 1 (not 0).  -1 stands for &amp;quot;best weapon&amp;quot; (by whatever criterion the AI chooses to use).   '''Available from Wesnoth 1.11.8'''&lt;br /&gt;
&lt;br /&gt;
Below are 2 examples of Simple Attack Micro AI configurations from the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  The first one shows how to force the AI to attack with all Soulless and Walking Corpse units first:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110000&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless,Walking Corpse&lt;br /&gt;
             [/filter]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
The second executes attacks by Soulless on all enemy units that are 1 XP from leveling (something the RCA AI would generally not do).  Note that the Lua code bit is simply there in order to code the &amp;quot;1 XP from advancing&amp;quot; condition.  It is not needed for most other SUFs, as can be seen in the previous example.&lt;br /&gt;
         [lua]&lt;br /&gt;
             code=&amp;lt;&amp;lt;&lt;br /&gt;
                 function close_to_advancing(unit)&lt;br /&gt;
                     if (unit.experience &amp;gt;= unit.max_experience-1) then&lt;br /&gt;
                         return true&lt;br /&gt;
                     else&lt;br /&gt;
                         return false&lt;br /&gt;
                     end&lt;br /&gt;
                 end&lt;br /&gt;
             &amp;gt;&amp;gt;&lt;br /&gt;
         [/lua]&lt;br /&gt;
 &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110001&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless  # No Walking Corpses; L0 units don't advance enemy&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 lua_function = &amp;quot;close_to_advancing&amp;quot;&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
Several Simple Attack Micro AIs can be combined for the same AI side.  The two examples above are both used (note the different values for ca_score) in the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
== Custom Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|5}}&lt;br /&gt;
&lt;br /&gt;
From 1.13.5 onwards, adding a new Micro AI can be easily done without altering any core files or overriding the micro_ai tag. The [micro_ai] tag now looks up its ''ai_type'' parameter in the &amp;lt;code&amp;gt;wesnoth.micro_ais&amp;lt;/code&amp;gt; table and uses the micro AI setup function found there to determine which keys are required and allowed and what candidate actions to add.&lt;br /&gt;
&lt;br /&gt;
A micro AI setup function takes as its single parameter the contents of the [micro_ai] tag and returns three separate values:&lt;br /&gt;
*'''required''': A list of required keys and tags, as strings. Tags are indicated by enclosing them within square brackets in the string. If a key or tag in this list is missing from the [micro_ai] tag, an error will be printed.&lt;br /&gt;
*'''optional''': Similar to above, a list of optional keys and tags, as strings. No error will be printed if a key or tag in this list is missing, nor will an error be printed if it is present (as would be the case for unrecognized keys or tags).&lt;br /&gt;
*'''ca_params''': A table specifying the candidate actions that constitute the micro AI. It should contain an ''ai_id'' key (which specifies a base ID to be used for candidate actions of this micro AI) and a series of candidate action definitions. Each candidate action definition is a table containing the following keys:&lt;br /&gt;
**'''ca_id''': The ID used for the candidate action. This will be combined with the ''ai_id'' to produce the final ID.&lt;br /&gt;
**'''location''': Path to the Lua file that defines the candidate action.&lt;br /&gt;
**'''score''': The maximum score for the candidate action. If the [micro_ai] tag specified a ''ca_score'' parameter, the score here should normally be based on that.&lt;br /&gt;
&lt;br /&gt;
(The return statement would thus look like &amp;lt;syntaxhighlight lang='lua' inline&amp;gt;return required, optional, ca_params&amp;lt;/syntaxhighlight&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
Since the function has full access to the contents of the [micro_ai] tag, it can conditionally add candidate actions to '''ca_params''' depending on the values of other keys within the [micro_ai] tag. It can also perform other actions such as adding facets to an aspect; if it does such things, it should pay attention to the ''action'' key to determine whether it's being added or removed.&lt;br /&gt;
&lt;br /&gt;
There are two useful helper functions that can be used by Micro AI setup functions requiring aspect changes. They can be brought into scope by a line similar to the following:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='lua&amp;gt;local MAIH = wesnoth.require(&amp;quot;ai/micro_ais/micro_ai_helper.lua&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* MAIH.add_aspects(side, aspect_params)&lt;br /&gt;
* MAIH.delete_aspects(side, aspect_params)&lt;br /&gt;
&lt;br /&gt;
The ''aspect_params'' parameter is an array of tables, each of which contains the following keys:&lt;br /&gt;
*'''aspect''': The aspect to change.&lt;br /&gt;
*'''facet''': The WML defining the facet. This must include an ''id'' key in order for the aspect to be correctly deleted. For maximum compatibility, incorporate the ''ca_id'' key from the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=65218</id>
		<title>Micro AIs</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=65218"/>
		<updated>2019-12-12T16:57:12Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Healer Support Micro AI (ai_type=healer_support) */ add guardian and passive-leader&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== A Few General Words about Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
* Micro AIs allow Wesnoth campaign and scenario authors to add new AI functionality without the need for any AI programming. They bring new specialized behavior to Wesnoth that cannot be achieved, at least not easily, by adjusting default AI parameters (such as those described at [[AiWML]]) or with WML code.&lt;br /&gt;
&lt;br /&gt;
* A Micro AI is activated and configured (or deleted) via the [micro_ai] tag, requiring only a few lines of WML code.&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.6''', the Micro AI behavior was inconsistent when it came to '''hidden enemies and petrified units'''.  Some Micro AIs ignored those, while others did not.  In some case there was even a potential for AI errors when an AI unit ran into hidden or petrified enemies.  As of 1.13.6, this has all been fixed.  The Micro AIs now correctly and consistently ignore hidden units and treat petrified units as blocking the hex they are on but having no effect otherwise, in the same way as the default AI does.  '''Important notes:'''&lt;br /&gt;
** It is not possible to re-include hidden/petrified units via the Standard Unit Filters (SUFs) used by some of the Micro AIs. In fact, if this is attempted, the filters will generally not match any unit.&lt;br /&gt;
*** This only applies to SUFs which go directly into the [micro_ai] tag. Any SUF that is a sub-tag of another filter (for example, of a Standard Location Filter) is taken literally and returns all units matching the filter. If hidden or petrified units are to be excluded from such a sub-filter, that needs to be specified directly in that filter.&lt;br /&gt;
** We also note that, because all the Micro AIs are written in Lua, it is possible to [[Micro_AIs#Using_Development_.281.13.29_Version_Micro_AIs_in_your_Stable_.281.12.29_Version_Add-on|move a Micro AI's code into one's add-on]] and modify it to whatever scenario-specific behavior is desired (this does not only apply to hidden/petrified units, but to all behavior). Setting up general Micro AI behavior that works in every possible setting is not feasible, but adapting it to work (only) in the specific setting of a given scenario is often quite simple. If you do not know how to do this, please post a message on the Wesnoth forums and we will be happy to help you with it.&lt;br /&gt;
&lt;br /&gt;
* The '''[[AiWML#List_of_AI_Aspects|default AI aspects]] are, for the most part, not taken into account by the Micro AIs'''.  For example, unless noted otherwise, specifying a value for ''aggression'' does not change the MAI behavior, and locations specified in an ''[avoid]'' tag generally can be entered by units controlled by an MAI.  This is a feature of the Micro AIs, not a bug.  If you feel that a certain default AI aspect should also apply to a Micro AI, please let us know in the [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 Micro AIs Feedback thread].&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.7''', the Micro AIs did not all work reliably when the '''AI side is under shroud''' and it may have been necessary to turn shroud off for the side when using a Micro AI. {{DevFeature1.13|7}} The Micro AIs now all work for sides under shroud. They do this by disregarding shroud for terrain purposes when path finding, consistent with default Wesnoth AI behavior, while still ignoring hidden units (including those hidden under shroud) correctly.&lt;br /&gt;
&lt;br /&gt;
* Most Micro AIs in their default setting apply to all units of a side, although many of them provide parameters for filtering the units that should be affected by the new behavior.  If the Micro AI is applied only to part of the units of a side, the other units follow the default Wesnoth AI behavior.&lt;br /&gt;
&lt;br /&gt;
* Micro AIs are available in mainline since Wesnoth 1.11/1.12.&lt;br /&gt;
&lt;br /&gt;
== Micro AI Test and Demo Scenarios ==&lt;br /&gt;
&lt;br /&gt;
All Micro AIs described here can be checked out in a number of test and demo scenarios.  Test scenarios can be accessed from the command line by typing&lt;br /&gt;
 path/wesnoth-executable -t micro_ai_test&lt;br /&gt;
or (depending on your system and Wesnoth version)&lt;br /&gt;
 path/wesnoth-executable -tmicro_ai_test&lt;br /&gt;
This starts up a &amp;quot;switchboard scenario&amp;quot; in which you can select the Micro AI demonstration scenario you want to check out.  Here, ''path/wesnoth-executable'' needs to be replaced by whatever the path and filename of the Wesnoth executable is on your system.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|8}}&lt;br /&gt;
&lt;br /&gt;
You can also access the test scenarios by assigning a hotkey to &amp;quot;Start Test Scenario&amp;quot; in the preferences. Then, simply press the hotkey at the titlescreen, possibly wait out the loading screen, and select the desired scenario from the list.&lt;br /&gt;
&lt;br /&gt;
== Setting up a Micro AI ==&lt;br /&gt;
&lt;br /&gt;
Setting up a Micro AI requires only one simple step:&lt;br /&gt;
==== Activating and configuring (or deleting) a Micro AI ====&lt;br /&gt;
&lt;br /&gt;
Micro AIs are activated, deleted and configured using the [micro_ai] tag.  This tag needs to be placed in [[ActionWML]], that is, in an event, a menu option or the like.  As an example, the following code activates the healer_support Micro AI in its default configuration for Side 2 from the beginning of the scenario:&lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
 &lt;br /&gt;
         # Configure the healer support micro AI&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
'''Keys required for all Micro AIs:'''&lt;br /&gt;
All [micro_ai] tags must contain the following three required keys:&lt;br /&gt;
* '''side''': The side for which the Micro AI is to be added, changed or deleted.  This has to be an individual side number, lists of sides are not supported.&lt;br /&gt;
* '''ai_type''': The type of Micro AI to be added, changed or deleted.  See the following sections for allowed values.&lt;br /&gt;
* '''action''' (string): The action to take concerning the Micro AI.  The following values are allowed: &lt;br /&gt;
** ''add'': Add a Micro AI to a side.&lt;br /&gt;
** ''change'': Change the configuration of an existing Micro AI.  Note that this does not only change the specific parameters provided in the tag, but it replaces the entire existing Micro AI by a new version with the new (and only the new) configuration.  It is therefore equivalent to using first the ''delete'' and then the ''add'' action.&lt;br /&gt;
** ''delete'': Delete an existing Micro AI from a side.&lt;br /&gt;
&lt;br /&gt;
'''Keys required under certain circumstances:'''&lt;br /&gt;
* '''ca_id''': (string) If several MAIs of the same type are used for the same side and one or several of these are to be changed or deleted during a scenario, this key needs to be included so that the [micro_ai] tag knows which of them needs to be changed/deleted.  The same is also true if a Micro AI is changed more than once during a scenario.  We therefore '''strongly recommend always setting ''ca_id''''' for MAIs which are, or might be, changed or deleted during a scenario.&lt;br /&gt;
** '''Note:''' The id(s)/name(s) actually assigned to the CA(s) of the MAI by the engine might be different from the parameter passed here.  It is, however, sufficient to use this ca_id with the [micro_ai] tag add/delete/change actions to identify the MAI uniquely.  (In other words: you don't have to worry about this if you always use the [micro_ai] tag, but might have to figure out the actual name(s) of the CA(s) if you want to delete or change them manually.)&lt;br /&gt;
* Other keys may also be required depending on the Micro AI.  In additional, a number of optional keys may be available to configure the AI behavior.  See the sections on the individual Micro AIs for details.&lt;br /&gt;
&lt;br /&gt;
'''Required keys for deleting a Micro AI:'''&lt;br /&gt;
&lt;br /&gt;
Only a subset of the keys required for setting up a Micro AI are needed when deleting it.  '''side''', '''ai_type''' and '''action'''=delete always need to be provided.  '''ca_id''' needs to be given if it was provided when adding the Micro AI (see above for when this is needed).&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* The ''add'' and ''change'' actions ignore all keys that do not apply to the respective Micro AI type.  The ''delete'' action ignores all keys other than the required keys listed above.&lt;br /&gt;
&lt;br /&gt;
* Some of the Micro AIs use and/or modify default AI components (candidate actions or aspects) when they are added, or reset these components to their default values when they are deleted.  Thus, if you have modified the AI yourself using the default CAs or aspects, some of these modifications might be affected, or might be interfering with the Micro AI.  The sections below indicate whether a Micro AI modifies any default AI components.&lt;br /&gt;
&lt;br /&gt;
===Combining Different Micro AI Types for the Same Side===&lt;br /&gt;
&lt;br /&gt;
No extra steps need to be taken when combining different Micro AIs on the same side.  Simply set up the [micro_ai] tags for all AIs you want to use.&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* While it is ''technically'' possible to combine any number of Micro AIs, this does not mean that all Micro AIs work together in practice.  Some are not compatible because they control units in contradictory ways.  &lt;br /&gt;
* Not all combinations that ''should'' work have been tested yet.  If you find a combination that you believe should work but doesn't, [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 please let us know] and we will see if that can be fixed.&lt;br /&gt;
* The candidate action score almost certainly has to be set (using the ca_score= key) for one or several of the MAIs if several of them are used on the same side.&lt;br /&gt;
&lt;br /&gt;
== Animals Micro AIs ==&lt;br /&gt;
=== General Comments on the Animal Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Animals Micro AIs are meant to simulate the behaviors of certain types of animals.  They are, however, set up so that they can be used with arbitrary unit types and might be applicable in quite diverse situations.  The AIs nevertheless remain named after the animals for which they were written originally, so that they are (to some extent) descriptive of their behavior.&lt;br /&gt;
&lt;br /&gt;
Brief descriptions of the AI behaviors are given in the following subsections.  To get a better feeling for their behavior, check out the &amp;quot;Animals&amp;quot;, &amp;quot;Swarm&amp;quot;, &amp;quot;Wolves&amp;quot; and &amp;quot;Dragon&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  Note that some of the animal AIs are designed for animal types that do not exist in mainline.  In the test scenario, other animal types are therefore substituted for them.&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Wolves (ai_type=wolves) ===&lt;br /&gt;
&lt;br /&gt;
The Wolves Micro AI organizes all &amp;quot;wolf&amp;quot; units (predators) of the side to hunt in a single pack.  They actively chase after the closest prey and try to corner it (not always super successfully), but are easily distracted by other enemy units coming into attack range, to the point where the pack might split up.  The wolves can, however, be told to attack only prey units as well as to try to avoid other types of units (such as larger predators).  When no prey is left, the wolves wander randomly.&lt;br /&gt;
&lt;br /&gt;
This AI works best for a small number of predators (2 - 5) and sparse prey.  Also, the predators should be put on the map in a pack, that is, reasonably close to each other.  For a demonstration of the Wolves AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Note that another wolves AI exists, called [[Micro_AIs#Animals_AI:_Multi-pack_Wolves_.28ai_type.3Dwolves_multipacks.29|Multipack Wolves]], which distributes the wolves of a side into several packs, rather than all of them into one single pack as it is done here.&lt;br /&gt;
&lt;br /&gt;
Enable the Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the predator units.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the prey units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_only_prey'''=no: In the default configuration, the wolves attack all enemy units in attack range.  If this key is set to 'yes', they only attack prey units.&lt;br /&gt;
* '''avoid_type''': Comma-separated list of unit types (on enemy sides) which the wolves try to avoid.  They try to stay out of these units' way as much as possible when moving around (except when they are moving in to attack another enemy unit) and never attack them.&lt;br /&gt;
* '''ca_score'''=90000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 90,000, which is lower than the default combat CA, but higher than most other default CAs which move units.  It can be changed by setting this parameter but in most cases this will not make sense.  Note that the Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Notes on default (RCA) AI aspects:''' &lt;br /&gt;
* If ''attack_only_prey'' or ''avoid_type'' are set, the Wolves Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* Attacks by the wolves are left to the default (RCA) AI, meaning that the value of ''aggression'' has an influence on the behavior.&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI is taken into account. Note: as attacks by the wolves are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=6&lt;br /&gt;
             ai_type=wolves&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Wolf&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Vampire Bat&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             avoid_type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Multi-pack Wolves (ai_type=wolves_multipacks) ===&lt;br /&gt;
&lt;br /&gt;
The Multi-pack Wolves Micro AI is different from the [[Micro_AIs#Animals_AI:_Wolves_.28ai_type.3Dwolves.29|Wolves Micro AI]] in that there can be an arbitrary number of wolf packs, with the pack size being a free parameter.  At the beginning of the scenario, close wolves are grouped into packs in a semi-methodical way.  If the wolves are put on the map in distinct groups of close wolves of the correct number, they will be joined into packs matching these groups.  If, on the other hand, they are spread out all over the map, the method of assigning them to packs is semi-random.&lt;br /&gt;
&lt;br /&gt;
Wolves of the same pack begin by joining each other on the map.  After that, they stay together until only one wolf is left, which then tries to join up with an incomplete pack or with other single wolves.  Individual wolves entering the map during the scenario behave in that way as well.&lt;br /&gt;
&lt;br /&gt;
A second difference to the other Wolves AI is that wolves do not actively hunt here.  For the most part they just wander (often long distance).  However, the pack ferociously (and without regard for its own health) attacks any enemy units that come into range, as long as that does not mean separating the pack by more than a few hexes.  Staying together, or joining with a new wolf assigned to the pack, is the only thing that takes priority over satisfying the wolves' thirst for blood.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Multi-pack Wolves AI behavior, check out the &amp;quot;Wolves&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Multi-pack Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves_multipacks&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Multi-pack Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''pack_size'''=3: (integer) The size of the packs.&lt;br /&gt;
* '''show_pack_number'''=no: If set to &amp;quot;yes&amp;quot;, the wolves' pack numbers will be shown below them.  Note that this means that the pack number of a wolf killed in an attack will remain as a label on the map.  &lt;br /&gt;
* '''type'''=Wolf: Comma-separated list of the unit types which the AI controls. The default value is &amp;quot;Wolf&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves_multipacks [micro_ai] tag usage from the &amp;quot;Wolves&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=wolves_multipacks&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             show_pack_number=yes&lt;br /&gt;
             pack_size=4&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Big Animals (ai_type=big_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Big Animals Micro AI is a simulation of large predators that wander and hunt alone, such as Bears, Giant Spiders or Yetis.  For the most part, these just wander on the terrain that's been defined for them, but they attack enemy units if those happen to come into range.  The AI can be set up so that the Big Animals stay out of the way of other units (such as other large predators), in which case they only attack those if cornered or if they run into them accidentally (such as ambushers).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Big Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Big Animals Micro AI by using&lt;br /&gt;
 ai_type=big_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the animals to which this AI is applied.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': The big animals often go for long distance (multi-turn) journeys.  This tag is a [[StandardLocationFilter|Standard Location Filter]] describing the terrain from which the next goal is chosen.  The default is all terrain on the map. &lt;br /&gt;
* '''[filter_location_wander]''': The terrain on which the big animals will end their moves. This is a [[StandardLocationFilter|Standard Location Filter]].  The default is all terrain on the map.&lt;br /&gt;
* '''[avoid_unit]''': A [[StandardUnitFilter|Standard Unit Filter]] describing all enemy units that the big animals will avoid.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
Here's an example of big_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=big_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Ghast&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [avoid_unit]&lt;br /&gt;
                 type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
             [/avoid_unit]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=1-40,1-18&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Forest Animals (ai_type=forest_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Forest Animals Micro AI simulates a number of different animal behaviors that one might find in a forest, namely animals behaving like deer, rabbits and families of tuskers.  In the following, we will refer to those animals by these types, even though the actual unit types can be selected freely.  They behave as follows:&lt;br /&gt;
* Each deer wanders randomly on (selectable) terrain, except when enemies get in its (the deer's) maximum movement range, in which case it flees to the farthest point it can reach.&lt;br /&gt;
* Tuskers exhibit the same behavior as deer, except when an enemy is next to one of the tusklets.  This enemy is attacked unconditionally by the tuskers trying to protect their young.&lt;br /&gt;
* Tusklets blindly follow the closest adult tusker, except when there is no tusker left, in which case they behave the same as deer.&lt;br /&gt;
* Rabbits also wander randomly, but in addition disappear into their holes (if any are within reach) when enemies are close.  They reappear out of their holes at the beginning of the turn, if it is safe.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Forest Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Forest Animals Micro AI by using&lt;br /&gt;
 ai_type=forest_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Forest Animals AI consists of 4 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-3&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''rabbit_type, tusker_type, tusklet_type, deer_type''': Comma-separated lists of the unit types behaving as the respective animals.  If any of these parameters are not set, those types of animal behavior are skipped by the AI.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain on which the forest animals wander when they are not threatened by enemies.&lt;br /&gt;
* '''rabbit_number'''=6: (integer)  The number of rabbits for the AI to have on the map simultaneously.  The AI puts the missing number of rabbits onto the map at the beginning of the AI's turn (when the rabbits come out of their holes).&lt;br /&gt;
* '''rabbit_enemy_distance'''=3: (number)  Rabbits won't spawn in holes that aren't more than this distance away from all enemies.&lt;br /&gt;
* '''rabbit_hole_img''': Rabbit holes are marked by items on the map.  If this parameter is set, it must be the file name of the item image.  If it is not set, all items on the map are treated as rabbit holes.&lt;br /&gt;
&lt;br /&gt;
Here's an example of forest_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=forest_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             deer_type=Vampire Bat&lt;br /&gt;
             rabbit_type=Giant Rat&lt;br /&gt;
             tusker_type=Ogre&lt;br /&gt;
             tusklet_type=Young Ogre&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=*^F*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Herd and Herders (ai_type=herding) ===&lt;br /&gt;
&lt;br /&gt;
The Herding Micro AI sets up an area on the map where a number of herder units (such as sheep dog) control and protect a herd (such as sheep).  For convenience, we simply refer to them as sheep dogs and sheep in the following, even though arbitrary unit types can be selected for either.&lt;br /&gt;
&lt;br /&gt;
Sheep dogs try to keep their sheep safe.  This involves keeping them inside the herding area, positioning themselves in between the sheep and approaching enemies, and attacking the enemies if those get too close.  If no active herding or protecting move is needed, the dogs go to a random location on the perimeter of the herding area.&lt;br /&gt;
&lt;br /&gt;
Sheep wander aimlessly except when a sheep dog is next to them, in which case they run away from the dog.  The dogs exploit this by positioning themselves on the outside of the sheep, if possible.  Sheep also run away from approaching enemies.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Herding AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Herding Micro AI by using&lt;br /&gt;
 ai_type=herding&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herder units (e.g. dogs).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herd units (e.g. sheep).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain forming the perimeter of the herding area.  In other words, this is the path the herders (dogs) use to patrol around the outside of the herding area.  This must be a set of coordinates ''completely enclosing'' the herding area.  Here's an example of setting up a rectangular perimeter:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         x=10-20,10-20,10,  ,20&lt;br /&gt;
         y=11,   21,   11-21,11-21&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
Another possibility is by defining the path by certain terrain on the map (as it is done in the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]), in which case the filter can simply look like this:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         terrain=Rb&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
* '''herd_x,herd_y''': The coordinate of one hex (''any'' one hex) inside the herding area.  This is needed to define which part of the map is on the ''inside'' of the herding area perimeter defined by [herding_perimeter], and which is the ''outside''. {{DevFeature1.15|0}} '''herd_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''attention_distance'''=8: (integer)  If an enemy is within this distance of a herd unit, but not as close as ''attack_distance'', the herders try to position themselves in between the enemy and the herd unit.&lt;br /&gt;
* '''attack_distance'''=4: (integer)  If an enemy is within this distance of a herd animal, the herders attack this enemy.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Herding AI consists of 6 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-5&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Herding [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=herding&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Footpad&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Troll Whelp&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             herd_x,herd_y=32,28&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=Rb&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Hunter (ai_type=hunter) ===&lt;br /&gt;
&lt;br /&gt;
The Hunter Micro AI leads a unit (or units) through hunt-and-rest cycles.  The units wander in a prescribed area of the map, their hunting ground, until an enemy unit comes within range.  Note that the units do not actively chase down enemies, but wander randomly through the hunting ground until an enemy is encountered.&lt;br /&gt;
&lt;br /&gt;
If enemies are within range of a hunter unit, the hunter attacks the weakest of them.  If a kill is made, it then retreats to its rest location, where it stays for a certain number of turns and until fully healed (whatever happens later).&lt;br /&gt;
&lt;br /&gt;
A few more details:&lt;br /&gt;
* If the hunter's way home is entirely blocked on the return (so that there is no possible path), the normal RCA AI takes over its behavior.&lt;br /&gt;
* However, if the way is blocked by an enemy unit occupying the rest hex, that enemy will be attacked unconditionally.&lt;br /&gt;
* A kill only makes the hunter go home when it is the attacker, not as defender.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Hunter AI behavior, check out the &amp;quot;Dragon&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]. &lt;br /&gt;
&lt;br /&gt;
Enable the Hunter Micro AI by using&lt;br /&gt;
 ai_type=hunter&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''home_x,home_y''': The coordinates of the hex to which the hunter returns and rests at after a kill. {{DevFeature1.15|0}} '''home_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the unit's hunting ground.  If this tag is not provided, the hunter uses all of the map.&lt;br /&gt;
* '''rest_turns'''=3: (integer)  The number of turns the AI stays at home_x,home_y after a kill.  It will not leave its home until this number of turns have passed ''and'' it is fully healed.&lt;br /&gt;
* '''show_messages'''=no: (boolean) If set to yes, the hunter announces whenever its behavior changes to the next phase.  Note that this is meant as debug output for the scenario author.  The messages are not translated and are not included in replays (and are only visible to the host in an MP game).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a hunter [micro_ai] tag usage from the &amp;quot;Dragon&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hunter&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Rowck&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=5-30,1-15&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             home_x,home_y=3,15&lt;br /&gt;
             rest_turns=2&lt;br /&gt;
 &lt;br /&gt;
             show_messages=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Swarm (ai_type=swarm) ===&lt;br /&gt;
&lt;br /&gt;
The Swarm Micro AI uses some very simply algorithms to simulate animal swarm behavior.  Without adjacent enemies, they simply do a random move, trying to stay together and at a certain distance from enemies.  However, if an enemy unit is close to any bat, the swarm scatters.  This is particular fun to watch when one places an enemy unit in the middle of the swarm.  After being scattered, the swarm members slowly rejoin, but not in a very organized way.  Sub-swarms or individual bats might roam around for quite some time before they find their way back.  It is also possible that individual bats (or small groups) split off from the larger swarm at times.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Swarm AI behavior, check out the &amp;quot;Swarm&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Swarm Micro AI by using&lt;br /&gt;
 ai_type=swarm&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Swarm AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_distance'''=5: (int) The minimum distance kept between units of the swarm and enemies when the swarm moves as a whole.&lt;br /&gt;
* '''scatter_distance'''=3: (int) Enemies within &amp;quot;scatter_distance&amp;quot; hexes of any swarm unit cause the swarm to scatter, by each unit trying to maximize its individual distance from the enemy.&lt;br /&gt;
* '''vision_distance'''=12: (int) Only units within this distance follow the overall swarm motion (either away from an enemy or of the swarm as a whole).  The smaller this value is set, the less likely the swarm is to stay together or rejoin.  This parameter is meant to simulate how far an individual member of the swarm can see, meaning that the swarm is &amp;quot;out of sight&amp;quot; (and the unit will not be able to follow it) if it is more than this distance away.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Swarm [micro_ai] tag usage from the &amp;quot;Swarm&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=swarm&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Assassin Squad Micro AI (ai_type=assassin) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|6}}&lt;br /&gt;
&lt;br /&gt;
This Micro AI controls one or several assassin units which try to circle around enemy units with the goal of hitting a target unit without being distracted or impeded by other enemies.  The units will always attack the target if such attacks are possible, otherwise they will follow the path of least resistance toward it. Notes:&lt;br /&gt;
&lt;br /&gt;
* The main priority for the AI is to find paths that avoid enemy units. However, if no such path can be found, the assassins will still advance toward the target, taking the path exposing the units to the least potential damage from enemies.&lt;br /&gt;
&lt;br /&gt;
* The units will not necessarily follow the same path. The best path is chosen individually for each unit.&lt;br /&gt;
&lt;br /&gt;
* While it is not a requirement for the AI to work, using ambusher or skirmisher units, or units with the quick trait, makes this AI more lethal in many cases.&lt;br /&gt;
&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded, it does not work otherwise. That means that '''ca_score''' is not a valid key for the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=assassin&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Assassin Squad specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the assassin units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the target unit.&lt;br /&gt;
** It is important to realize that the AI currently only works correctly with a single target unit. It is thus the responsibility of the scenario designer to make sure that this filter matches an individual enemy unit. If several enemy units are to be targeted in the same scenario, several instances of the Assassin Micro AI can be used.&lt;br /&gt;
** The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[prefer]''': A [[StandardLocationFilter|Standard Location Filter]] selecting locations on the map for the assassins to prefer when making their way toward the target unit. This could be used, for example, to make units with the ambush ability move (mostly) through forest or to have the assassins circle around the, say, northern part of the map rather than the south if that is desirable for scenario design purposes.&lt;br /&gt;
** As with avoiding enemies, the units will still move across non-preferred terrain if that is either the only option or ''much'' shorter than preferred terrain&lt;br /&gt;
** Attacking, once the target unit is in range, is not influenced by the [prefer] SLF.&lt;br /&gt;
&lt;br /&gt;
Here is an example of an Assassin Squad [micro_ai] tag usage:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=assassin&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Assassin,Master at Arms,Duelist&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             [prefer] # Circle around northern part of map&lt;br /&gt;
                 x=1-9, 10-99&lt;br /&gt;
                 y=1-37, 1-17&lt;br /&gt;
             [/prefer]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Bottleneck Defense Micro AI (ai_type=bottleneck_defense) ==&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense Micro AI lets you define a location on the map where the AI can take a defensive stance behind a narrow passage (bottleneck).  Units on the front line are backed up by healers and/or units with the leadership ability.  Injured units are moved to the back and replaced by uninjured (or less injured) units.  The units on the front line only attack when it is safe (no retaliation) or when there is a high chance that they will make a kill or level up.  Using this Micro AI only makes sense if there is no way for the enemy sides to move around the bottleneck and attack the AI's units from behind.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Bottleneck&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Bottleneck Defense Micro AI by using&lt;br /&gt;
 ai_type=bottleneck_defense&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Bottleneck Defense specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense AI requires two sets of coordinates that define where it should be taking up its defensive stance, and from which side the enemy attacks at that location.&lt;br /&gt;
&lt;br /&gt;
* '''x,y''': Comma separated lists of the hexes on the front line, where strong units are placed.  All hexes on which the AI makes contact with (can be attacked by) the enemy need to be included here.  Units are placed on them in the order in which they are listed here. {{DevFeature1.15|0}} '''location_id''' can be used instead with named/special locations.&lt;br /&gt;
* '''enemy_x,enemy_y''': Comma separated list of the hexes from which the enemy can attack the AI front line units.  This is needed for the AI to know on which side of the front line support units (healers etc.) need to be placed.  In many cases, it is sufficient to provide only one of the enemy front line hexes, but there are geometries for which that does not work.  It is therefore safer to list all enemy front line hexes here. {{DevFeature1.15|0}} '''enemy_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
By default, the AI places healers and units with the leadership ability both on the front line itself (if they are the strongest units available) and on hexes adjacent to and behind the line.  If different placement is desired, these locations can be overridden with the following keys.&lt;br /&gt;
&lt;br /&gt;
* '''active_side_leader'''=no: (boolean)  If set to 'yes', the side leader participates in the bottleneck defense action after the side's gold has been spent.  If set to 'no' (default), the leader follows default AI behavior (sitting on the keep and recruiting, for the most part).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Bottleneck Defense AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''healer_x,healer_y''': Comma separated list of hexes where healers are placed.  This can be used to keep healers from the front line (or to take up only certain positions on the front line), and/or to override the default healing positions behind the line.  Healers are placed on them in the order in which the hexes are listed, with the exception that hexes on the line always take priority over hexes behind the line. {{DevFeature1.15|0}} '''healer_loc''' can be used instead with named/special locations.&lt;br /&gt;
* '''leadership_x,leadership_y''': Same as ''healer_x,healer_y'', but for units with the leadership ability. {{DevFeature1.15|0}} '''leadership_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
As an example, in the &amp;quot;Bottleneck&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]], there are three front line hexes, two of which are on hill terrain, while the third is a road with a lower defense rating.  The healer and side leader are supposed to participate in combat because they are strong units, but only from the hill hexes to keep them safer.  This is accomplished by using the following keys settings (check out the scenario to see which hex is which):&lt;br /&gt;
 [micro_ai]&lt;br /&gt;
     side=1&lt;br /&gt;
     ai_type=bottleneck_defense&lt;br /&gt;
     action=add&lt;br /&gt;
 &lt;br /&gt;
     x=14,14,14&lt;br /&gt;
     y= 7, 9, 8&lt;br /&gt;
     enemy_x=13,13&lt;br /&gt;
     enemy_y= 8, 9&lt;br /&gt;
 &lt;br /&gt;
     healer_x=14,14,15,15&lt;br /&gt;
     healer_y= 7, 9, 8, 9&lt;br /&gt;
     leadership_x=14,14,15,15&lt;br /&gt;
     leadership_y= 7, 9, 9 ,8&lt;br /&gt;
     active_side_leader=yes&lt;br /&gt;
 [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Fast Micro AI (ai_type=fast_ai) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|0}}&lt;br /&gt;
&lt;br /&gt;
This AI is meant as a fall-back AI for scenarios where there are so many units that attacks and moves done by the default AI are slow with long delays before each move.  It replaces the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions (CAs).  It should be obvious that in order to achieve the faster speed, compromises concerning the quality of the moves have to be made.&lt;br /&gt;
&lt;br /&gt;
Enable the Fast Micro AI by using&lt;br /&gt;
 ai_type=fast_ai&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Combat candidate action:'''&lt;br /&gt;
* Attackers are considered individually one at a time.  The best attack found for a unit is executed without checking whether a better attack by another unit might be possible.&lt;br /&gt;
* The default order in which attackers are considered is by decreasing hitpoints, starting with the strongest unit (in terms of HP).  This can be modified with the optional keys below.&lt;br /&gt;
* By default, if weaker units block attack hexes for a stronger unit, they do not move out of the way (this is the same as for the default AI).  If a hex becomes available after an attack (e.g. due to a weaker unit dying in the attack), all attacks that were possible ''before'' this happened are finished first.  Only after that are attacks by the stronger unit reconsidered.&lt;br /&gt;
* If include_occupied_attack_hexes=yes is set, weaker units move out of the way for stronger units if possible.  This can, however slow down the AI depending on the situation on the map.  Use with caution.&lt;br /&gt;
* {{DevFeature1.13|2}} Previous to this version, attacks by the leader were bunched in with the attacks by the other units.  Now leader attacks are treated separately and contain an extra layer of analysis to determine whether the attack position is &amp;quot;safe&amp;quot; (in an approximate way).  Currently, this is done with two simple considerations:&lt;br /&gt;
** Count all enemy units that can reach the leader's attack position.  If there are more than given by optional parameter 'leader_attack_max_units' (default value 3), do not attack.&lt;br /&gt;
** Add up the hitpoints of all enemies that can reach the leader's attack position.  If this value, multiplied by optional parameter 'leader_weight'(default value 2), is larger than the leaders hitpoints, do not attack.&lt;br /&gt;
** Two optional parameters, 'threatened_leader_fights' and 'leader_additional_threat', can also be used to let the leader attack when the threat at the attack hex is comparable to that at the leader's current position.&lt;br /&gt;
** In order to keep this AI as fast as possible, leader attacks are done by a separate candidate action that is only evaluated when all other CAs are done.&lt;br /&gt;
&lt;br /&gt;
'''Move-to-targets, villages and retreat candidate actions:'''&lt;br /&gt;
* The move-to-targets, villages and retreat candidate actions of the default AI are removed.  Move-to-targets and villages are taken over by the Fast Micro AI, whereas retreat is simply not done.&lt;br /&gt;
* This CA moves units toward goals after attacks are done.  Goals are unowned (if the AI side has a leader only) and enemy-owned villages and enemy leaders.&lt;br /&gt;
* For each goal, units are pre-sorted in order of the fewest moves needed to cover the ''distance'' toward the goal (that is, as if move cost on each hex toward the goal were 1).  Units are then considered sequentially and if a unit can get there &amp;quot;efficiently&amp;quot;, its move is executed without considering other units.&lt;br /&gt;
** A move is considered efficient if the move cost to get to the hex is not more than 'move_cost_factor' larger than the distance to the hex.  'move_cost_factor' is an optional key that defaults to 2.  It can be set to values of 1.1 or larger (see below).&lt;br /&gt;
** If the move for the current unit is not found to be efficient, the AI considers the next unit instead.  If no unit can do an efficient move toward the goal, the best (fastest) of the inefficient moves is selected instead.&lt;br /&gt;
&lt;br /&gt;
* Village goals:&lt;br /&gt;
** Village goals are considered before enemy leader goals so that the AI preferentially chooses scouts (or otherwise fast units) for them.&lt;br /&gt;
** The number of units sent toward villages depends on the value of the default AI aspect village_value.  It is set to village_value/4 times the number of units the AI has left to move.  Thus, by default (village_value=1.0) the AI sends a quarter of its units toward villages, and the rest toward enemy leaders.  This means that the meaningful range of values for village_value is 0..4.&lt;br /&gt;
** Only one unit is moved toward each village.  This consideration also overrides the number of units to move toward villages defined by village_value, topping it at the number of unowned/enemy-owned villages on the map.&lt;br /&gt;
** Setting village_value=0 disables moving units toward villages altogether.  That is, not only are no units chosen, but the village targeting phase of the AI is entirely disabled with no evaluation time used for it.  Note that, unlike with the default AI, this also means that no villages within range of an AI unit are grabbed.&lt;br /&gt;
** The order in which villages are chosen as goals is some combination of them being close to the AI side's leader and being spread out over the map.&lt;br /&gt;
&lt;br /&gt;
* Enemy leader goals:&lt;br /&gt;
** The order of enemy leaders goals is set according to their distance from the AI leader with the closest enemy leader being targeted first (if the AI has a leader, otherwise their order is random).  One unit is sent toward each enemy leader in this order, after which the AI starts with the first enemy leader again.  This is repeated until all units have moved.&lt;br /&gt;
&lt;br /&gt;
* Guardians (units with ai_special=guardian set) are ignored by the fast move-to-targets CA.&lt;br /&gt;
&lt;br /&gt;
'''Tips for speeding up/modifying the Fast Micro AI in specific scenarios:'''&lt;br /&gt;
&lt;br /&gt;
A number of optional keys and default AI aspects can be used to modify the behavior of the Fast Micro AI.  The MAI is set up so that it is a reasonable compromise between speed and quality of the moves in most situations.  However, this might not be ideal for all scenarios here are some things that can be done to speed up the AI (or to get better behavior in some situations by doing the opposite):&lt;br /&gt;
* Use large values for the default AI aspect aggression.  The largest delays before attacks happen (in some circumstances) when the Fast MAI has to consider lots of units with attacks left but decides not to attack with them.  See below for a description of how aggression is used by the fast AI.&lt;br /&gt;
* Use large values of move_cost_factor (see above for description of what this does).  If you want to emphasize units that can move across terrain easily (e.g. bats) to be chosen for village grabbing, you might want to use low values of move_cost_factor, but this might slow down the AI.  The default value should work reasonably well in most standard situations, but modifying the value might work better on specific maps.&lt;br /&gt;
* Don't use dungeon_mode=yes unless absolutely necessary, even in most dungeon crawls.  Test the AI without this setting first and only use it if it is the only way to solve the problem.  Often, a minor tweak to the map will have the same effect, without the speed penalty.&lt;br /&gt;
* Use include_occupied_attack_hexes=yes with caution.  It might (or might not) slow down the attack CA significantly, depending on the scenario.&lt;br /&gt;
* Set village_value=0, which makes the AI not consider villages as goals.  This has not been found to make a large difference on the maps tested so far, but it might have an effect on maps with a very large number of villages.&lt;br /&gt;
* Use attack_hidden_enemies=yes on maps where there are no hidden enemies. Excluding hidden enemies from the attack calculation requires an additional filter to be evaluated that takes extra time. In most cases this will not result in a large computation time saving.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_hidden_enemies'''=no: (boolean)  By default, hidden enemies are excluded from the attack calculation.  Setting this to 'yes' makes the AI also consider those enemies as attack targets.&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.13|2}} A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  As with the default AI, units will not step on hexes defined by the [avoid] tag.  In addition, no goals will be chosen that lie on those hexes.  The one minor exception is that a unit stepping out of the way for another unit might temporarily enter the avoided zone.&lt;br /&gt;
* '''dungeon_mode'''=no: (boolean)  As much as possible, the Fast Micro AI works with distances between hexes rather than path finding.  This works pretty well on open maps and even most dungeon crawls (and similar scenarios), but it might result in some units getting temporarily (and in rare cases permanently) stuck on maps with convoluted labyrinths.  If that is the case, setting dungeon_mode=yes triggers more path finding that avoids such situations (but as a result is somewhat slower).&lt;br /&gt;
* '''include_occupied_attack_hexes'''=no: (boolean) If set to 'yes', the AI moves weaker units out of the way for a stronger attacker.  This might lead to better attacks but is slower, so don't use if speed is essential.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the attackers.  Only AI units matching this filter will attack, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes the side= key set to the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the enemies.  Only enemy units matching this filter will be attacked, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''leader_weight'''=2: (number &amp;gt;= 0) {{DevFeature1.13|2}} A factor specifying how much more valuable the AI considers side leaders than non-leader units in the attack evaluation. This is also used to determine whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''leader_additional_threat'''=1: (number &amp;gt; 0) {{DevFeature1.13|2}} If threatened_leader_fights=yes, the threat at the attack position may be larger by this factor for the leader to leave the keep for an attack.&lt;br /&gt;
* '''leader_attack_max_units'''=3: (number &amp;gt;= 0) {{DevFeature1.13|2}} A number specifying how many enemy units within reach are acceptable when determining whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''move_cost_factor'''=2: (number &amp;gt;= 1.1)  The factor by which the move cost to a goal hex may be larger than the distance to the goal hex for a move to be considered &amp;quot;efficient&amp;quot;.  See description above.  1.1 is used if this is set to values smaller than 1.1.  Smaller values might lead to better behavior, while larger values might speed up the AI.&lt;br /&gt;
* '''weak_units_first'''=no: (boolean) If set to 'yes', inverts the order in which units are considered by the combat CA.  Attacks are then done in order of increasing hitpoints of the attackers.&lt;br /&gt;
* '''skip_combat_ca'''=no: (boolean) If set to 'yes', the fast combat CA is not used.  The default AI's high_xp_attack and combat CAs remains in place in this case.&lt;br /&gt;
* '''skip_move_ca'''=no: (boolean) If set to 'yes', the fast move-to-targets CA is not used.  The default AI's retreat, villages and move-to-targets CAs remains in place in this case.&lt;br /&gt;
* '''threatened_leader_fights'''=no: (boolean) {{DevFeature1.13|2}} By default, the AI leader will not attack if the threat at the attack position is too high.  If this key is set to 'yes', the leader will attack if the threat there is not larger than the threat at his current position multiplied by the value of 'leader_additional_threat'. This is meant as an option for the endgame: when the AI leader is under attack, he might as well go down fighting.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''aggression''': The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to set how aggressive the AI should attack.  It works in roughly the same way as the default AI aspect:&lt;br /&gt;
** The value of (1 - aggression) determines how much the AI values its own units compared to the enemy's units.  Thus, aggression=0 means that attacks are only done if the AI expects to deal (roughly) the same amount of damage as it will receive.  aggression=1 means that expected damage to the AI's units is not considered, only the expected damage to enemy units matters.&lt;br /&gt;
** Damage is calculated as a combination of hitpoints and chance to die, in &amp;quot;gold equivalent units&amp;quot;.&lt;br /&gt;
** Unlike the default AI, aggression is set to 1 for values larger than 1.  Allowed values are thus anything from -infinity to +infinity, although only -infinity to +1.0 make sense.&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.13|2}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
*'''attacks aspect''':  {{DevFeature1.13|2}} The standard RCA AI [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] used to filter own and enemy units for attacks is now also taken into account as long as neither [filter] nor [filter_second] (see above under 'optional keys') are defined in the [micro_ai] tag.&lt;br /&gt;
*'''village_value=1''': (number ranging from 0 to 4) The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to define how much villages should be targeted by the move-to-targets CA.  In the Fast MAI, this defines which fraction of the AI units are moved toward village goals (see above for details).  If set to 0, villages are ignored as goals.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded to the same values as those of the respective [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|default candidate actions]].  ca_score is therefore not a valid optional key.&lt;br /&gt;
* The fast Micro AI modifies (removes) the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions.&lt;br /&gt;
&lt;br /&gt;
Enabling the Fast Micro AI in its default configuration is as simple as this usage example from the &amp;quot;Fast AI&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=fast_ai&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Generalized Goto Micro AI (ai_type=goto) ==&lt;br /&gt;
&lt;br /&gt;
The Goto Micro AI provides a highly configurable method of sending a unit (or units) to a location or set of locations.  The units to be moved are defined using a [[StandardUnitFilter|Standard Unit Filter]] (SUF) [alternatively, all side units are used if the SUF is not provided), while the goto locations are given in a [[StandardLocationFilter|Standard Location Filter]] (SLF).&lt;br /&gt;
&lt;br /&gt;
By default, each unit matching the SUF will move toward the closest goal (in terms of turns needed to get there) matching the SLF via the fastest route given the current situation on the map (locations of enemy units etc.), but this behavior can be influenced by the optional keys listed below.  Note that this means that a unit's goal might change from one turn to the next depending on the situation on the map.&lt;br /&gt;
&lt;br /&gt;
The behavior of the unit(s) once they reach their goal location(s) is configurable as well.  By default, they will stay there until the Goto Micro AI is deleted or changed.&lt;br /&gt;
&lt;br /&gt;
For demonstrations of several Goto Micro AI usages, check out the &amp;quot;Goto&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Goto Micro AI by using&lt;br /&gt;
 ai_type=goto&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Goto specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the possible goal locations for the move.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''avoid_enemies''': (number&amp;gt;0) By default, units controlled by the Goto Micro AI will not take enemy units into account for route finding, other than the enemies' ZoCs.  By contrast, if this key is set, they will try to avoid traversing hexes on which they can be attacked by enemies.  The larger the value of ''avoid_enemies'', the more the units will try to stay out of the enemies' way.  Notes:&lt;br /&gt;
** Units are willing to take a path that is 'avoid_enemies' movement points longer for each enemy unit that can be avoided by doing so for each hex along the path.  As an example, if avoid_enemies=1 and the shortest path crosses 2 hexes that can be reached by 3 enemies each, the unit is willing to take a path that is up to 6 hexes longer instead.  If avoid_enemies=10 in the same situation, the unit will take a path up to 60 hexes longer.&lt;br /&gt;
** Hexes adjacent to enemy units are always strongly avoided, independent of the value of avoid_enemies.&lt;br /&gt;
** avoid_enemies is ignored if 'use_straight_line=true' is set.&lt;br /&gt;
** Due to an issue with the path finding code, '''tunnels and teleports are ignored in Wesnoth 1.14 and before''' if 'avoid_enemies' is used. This has been fixed in Wesnoth 1.15.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units to be moved.  Defaults to all units of the side if not defined.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''ignore_enemy_at_goal'''=no and '''ignore_units'''=no: (boolean)  In the default configuration, the Goto MAI does not move units if the path to all goal hexes is blocked by enemies.  It is therefore possible to disable the Goto MAI by putting enemy units on all the goal hexes.  That can be changed if either of these two keys is set to 'yes'.&lt;br /&gt;
** 'ignore_enemy_at_goal=yes' makes the AI choose its goals while disregarding any enemy units at the goal hexes.  Even so, a stiff rating penalty is applied to goal hexes occupied by enemies, thus that unoccupied goal hexes are chosen unless all possible goals are occupied by enemies.  Note that this setting ''only'' ignores enemy units at the goal hexes themselves.  It is still possible to disable the AI by putting enemies all around the goals.&lt;br /&gt;
** 'ignore_units=yes' let's the AI ignore ''all'' enemy units when deciding which goal hex(es) to move toward.&lt;br /&gt;
** In both cases, enemies are only ignored for deciding which unit to move toward which goal hex.  For the actual move itself, all enemies and their ZoCs are considered as usual.&lt;br /&gt;
** Both options only take effect if neither avoid_enemies nor use_straight_line is set to 'yes', as neither of these consider enemies as blockers.&lt;br /&gt;
* '''release_unit_at_goal'''=no: (boolean) By default, a unit that reaches a goal location will stay there until the Goto Micro AI is deleted or changed (or until the conditions specified in the [goto_goals] SLF change).  If this parameter is set, a unit that has reached its goal will not be controlled by this Micro AI any more starting from the next turn.&lt;br /&gt;
* '''release_all_units_at_goal'''=no: (boolean) Same as ''release_unit_at_goal'', but this will deactivate the Goto Micro AI for all units once the first unit reaches a goal.&lt;br /&gt;
* '''unique_goals'''=no: (boolean)  If set to 'yes', each unit will be sent to a different goal location.  Note that the best goals are recalculated each turn, so a unit might choose a different goal this turn from what it was heading for during the previous turn if the situation on the map has changed.  Also note that if there are more units than goal locations, the left over units will not be handled by the Goto MAI but follow default behavior instead.&lt;br /&gt;
* '''use_straight_line'''=no: (boolean)  By default, units choose the route by which they will get to their goal locations in the smallest number of turns.  If this parameter is set to 'yes', a straight line route (or something pretty close to it at least) will be chosen instead, irrespective of whether this leads into a dead end, whether it is blocked by enemies, etc.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* Depending on the parameters used, the Goto Micro AI might require a lot of path finding to be done.  Thus, while it is possible to set up this AI to move many units to many different locations, a usage like that might require quite a long calculation time.&lt;br /&gt;
&lt;br /&gt;
===Goto Micro AI example usages:===&lt;br /&gt;
&lt;br /&gt;
Here are several examples of goto [micro_ai] tag usages from the &amp;quot;Goto&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
* Send a unit toward a castle (similar to the RCA AI's Goto Candidate Action, but avoiding dead ends and areas ZoCed by enemies)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
             ca_id=messenger1&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=messenger1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=6-7,2-4&lt;br /&gt;
                 terrain=C*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             ca_score=210001&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send all units of the side toward the southern border of the map&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=8&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                  y=33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             avoid_enemies=1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send units to the four corners of the map (with each unit going to a different corner).&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x=1,1,44,44&lt;br /&gt;
                 y=1,33,1,33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             unique_goals=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Set up a guardian unit that doesn't move while no enemy unit is within 8 hexes (ironically, the Goto Micro AI can be used to keep units in place, although using one of the Guardian Micro AIs might often be a better idea)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=guard1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 [filter]&lt;br /&gt;
                     id=guard1&lt;br /&gt;
                 [/filter]&lt;br /&gt;
                 [not]&lt;br /&gt;
                     [filter]&lt;br /&gt;
                         side=1&lt;br /&gt;
                     [/filter]&lt;br /&gt;
                     radius=8&lt;br /&gt;
                 [/not]&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Guardian Micro AIs ==&lt;br /&gt;
=== General Comments on the Guardian Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Guardian Micro AIs can be used to set up different kind of guardian behaviors for a unit or a set of units.  Four different types of guardians are available and are described below.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different guardian behaviors, check out the &amp;quot;Guardians&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
=== Return Guardian  (ai_type=return_guardian)===&lt;br /&gt;
&lt;br /&gt;
A 'return guardian' is a variation of the standard Wesnoth guardian. It has an assigned guard position (GP) to which it returns after attacks on approaching enemies:&lt;br /&gt;
* If at GP with no enemy in reach, do nothing.&lt;br /&gt;
* If at GP with an enemy in reach, attack. Note that the attack is done by the default AI, which might result in the guardian not attacking if the enemy is deemed too strong (this can be influenced by setting the default ''aggression'' value to a different value).&lt;br /&gt;
* If not at GP, return there, no matter whether an enemy is in reach or not.&lt;br /&gt;
* If enemies are blocking the way back to GP, do your best to move around them.&lt;br /&gt;
* If the guardian ends up next to an enemy on the way back, attack that enemy after the move.&lt;br /&gt;
&lt;br /&gt;
Enable the Return Guardian Micro AI by using&lt;br /&gt;
 ai_type=return_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''return_x,return_y''': The coordinate of the hex the unit returns to after each attack.  {{DevFeature1.15|0}} '''return_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=100010: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  This AI consist of 2 CAs, one with score &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt; the other with &amp;lt;tt&amp;gt;ca_score-20&amp;lt;/tt&amp;gt;.  The default value is set so that the first CA gets executed right before the default combat CA, and the other right after.  It usually will not make sense to change this value, but it is possible to do so for custom applications.  {{DevFeature1.13|6}}: This is now changed to 100100 and 99900 in order to avoid conflicts with the new high_XP_attacks candidate action.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a return guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=return_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=return1&lt;br /&gt;
             return_x,return_y=20,2&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
===Stationed Guardian (ai_type=stationed_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A 'stationed guardian' is another variation of the standard Wesnoth guardian with a somewhat more complex behavior than that of the 'return guardian'. Two positions are defined for it, a 'station' and a 'guarded location', as well as a distance. The behavior is as follows:&lt;br /&gt;
* If no enemy is within 'distance' of the guard's current position, do nothing.  This is independent of the guardian being at its station or not.&lt;br /&gt;
* Otherwise: If an enemy is within 'distance' of the guard, but not also within the same distance of the guarded location and the station (all of this simultaneously), move the guard in the direction of the station (or keep the guard in place if the station is unreachable).&lt;br /&gt;
* Otherwise:&lt;br /&gt;
** Pick the enemy unit that is closest to the guarded location.&lt;br /&gt;
** If we can reach it, pick the adjacent hex with the highest defense rating and attack from there.&lt;br /&gt;
** If not in reach, move toward this unit.&lt;br /&gt;
&lt;br /&gt;
Enable the Stationed Guardian Micro AI by using&lt;br /&gt;
 ai_type=stationed_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance parameter as explained above&lt;br /&gt;
* '''station_x,station_y''': The x and y position of the hex that serves as the guardians station. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''guard_x, guard_y''': The x and y position of the guarded location.  {{DevFeature1.13|0}}  These are now optional keys, defaulting to station_x,station_y if not provided. {{DevFeature1.15|0}} '''guard_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a stationed guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=stationed_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=stationed1&lt;br /&gt;
             distance=4&lt;br /&gt;
             station_x,station_y=2,14&lt;br /&gt;
             guard_x,guard_y=3,13&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Coward (ai_type=coward) ===&lt;br /&gt;
&lt;br /&gt;
Cowards are units that, like guardians, sit around doing nothing until an enemy comes into range.  Unlike guardians, however, they run away once enemies approach. Applications might be wild animals, unarmed civilians getting in the way of a battle, etc.  The coward macro can be called with two optional locations, 'seek' and 'avoid':&lt;br /&gt;
* If neither is given, the coward retreats to the position farthest away from the approaching enemies.&lt;br /&gt;
* If 'seek' is given, it preferentially goes toward that location (but getting away from enemies takes priority).&lt;br /&gt;
* If 'avoid' is given, it in addition tries to avoid that location (with both maximizing distance from enemies and going toward 'seek' taking priority).&lt;br /&gt;
* Both 'seek' and 'avoid' may consist of only one coordinate ('x' or 'y'), in which case not a single hex, but a line of hexes is sought or avoided.&lt;br /&gt;
&lt;br /&gt;
Enable the Coward Micro AI by using&lt;br /&gt;
 ai_type=coward&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance within which an enemy must be to &amp;quot;scare&amp;quot; the unit&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_if_trapped'''='no':  {{DevFeature1.13|0}}  With the default settings, a coward never attacks.  If this key is set to 'yes', the coward attacks the weakest (lowest hitpoints) enemy next to which it ends up after moving (such as a cornered animal would do, blindly lashing out without regards for its own safety).&lt;br /&gt;
* '''avoid_x, avoid_y''': The x and y coordinate of the hex to avoid. {{DevFeature1.15|0}} '''avoid_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units from which the cowards flee.  If not set, this defaults to all enemy units.  Note, however, that no &amp;quot;enemy of AI side&amp;quot; condition is added if [filter_second] is specified, meaning that it can include allied and/or enemy units as desired (and even units on the same side as the coward).&lt;br /&gt;
* '''seek_x, seek_y''': The x and y coordinate of the hex to seek. {{DevFeature1.15|0}} '''seek_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a coward [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=coward&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=coward3&lt;br /&gt;
             distance=5&lt;br /&gt;
             seek_x,seek_y=24,5&lt;br /&gt;
             avoid_x,avoid_y=24,15&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Zone guardian (ai_type=zone_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A zone guardian is a unit that, as the name says, guards a zone. It moves randomly inside this zone until an enemy enters it (or a separately defined enemy zone, see below). Applications might be the defense of a castle, a nesting area or for protecting an unit.  The zone macro can be called with an optional SLF, [filter_location_enemy]:&lt;br /&gt;
* If not specified, the zone guard attacks any enemy coming inside its guard zone.&lt;br /&gt;
* If [filter_location_enemy] is given, it attacks any enemy entering this zone and once there are no more enemies, it goes back to patrol in its basic zone.&lt;br /&gt;
&lt;br /&gt;
Enable the Zone Guardian Micro AI by using&lt;br /&gt;
 ai_type=zone_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone to guard.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_enemy]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone where enemies have to be attacked.  If not given, it defaults to [filter_location].&lt;br /&gt;
* '''station_x,station_y''': By default, the zone guardian moves randomly within the zone if there is no enemy to attack.  However, if station_x/y is given, it moves to this position instead during turns when it does not have an enemy to attack.  This position does not need to be inside the zone defined above.  Note: obviously it does not make sense to define [filter_location], [filter_location_enemy] and station_x/y all at the same time.  Only [filter_location] and either one of the latter two (or none of them) should be provided for any zone guardian. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a zone guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=zone_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=zone3&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=22-31,4-11  # This is intentionally chosen to extend past the lake&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             station_x,station_y=32,8&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Hang Out Micro AI (ai_type=hang_out) ==&lt;br /&gt;
&lt;br /&gt;
The Hang Out Micro AI instructs units of the AI side to &amp;quot;hang out&amp;quot; around a certain location, or locations, until a condition for mobilizing the side is met.  If none of the optional keys are given, the AI controls all units of the side to hang around the leader while moving off castle tiles to make room for recruiting.&lt;br /&gt;
&lt;br /&gt;
The AI works by moving each unit to its best hex first using the minimum amount of movement points required for this move.  Once all AI units are in position, movement points are set to zero for all of them simultaneously such that they cannot be used by other AI candidate actions.  This is done because the mobilizing condition might be met as result of one of the moves of the Hang Out AI, after which we want units to be able to continue with other AI actions.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Hang Out and Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Hang Out Micro AI by using&lt;br /&gt;
 ai_type=hang_out&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Hang Out specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  In its default configuration, the Hang Out MAI controls all units of a side, keeping them as close to the side leader(s) as possible while moving off castle tiles to make room for recruiting.  For this purpose, the candidate action score defaults to 170,000, which is just below the score of the default recruiting CA.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''': A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  If neither of these two exists, this parameter is set to default to all castle terrain (&amp;lt;tt&amp;gt;terrain=C*,C*^*,*^C*&amp;lt;/tt&amp;gt;) so that units move off the castle to make room for recruiting.  Note that this does not include keep terrain, otherwise the leader would be moved off the keep as well.&lt;br /&gt;
* '''ca_score'''=170000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 170,000, which means that it is executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Hang Out action (&amp;lt;tt&amp;gt;side=&amp;lt;/tt&amp;gt; is always set and does not need to be provided).  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the locations ''near'' which the AI units should hang out.  This does not mean that the units only move onto terrain specified here, but that they minimize the distance to such terrain.  It defaults to the position of the side leader(s).&lt;br /&gt;
* '''[mobilize_condition]''':  Contains ([[ConditionalActionsWML#Condition_Tags|WML Condition Tags]])  Once the condition set up here has been met, the Hang Out MAI is disabled and the side goes over to other AI activity.  The Hang Out MAI remains disabled after this, even if the condition is not met any more later in the scenario.&lt;br /&gt;
* '''mobilize_on_gold_less_than''': (number) Since side gold cannot be checked in a WML condition tag without storing it in a variable first, this key is provided separately.  It disables the Hang Out Micro AI once the side gold goes below the value specified here for the first time.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
* The Hang Out AI still takes effect if no locations to hang out at are found or if all map locations are to be avoided.  In that case, it simply keeps all units at their current locations.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Hang Out Micro AI configuration from the &amp;quot;Hang Out and Messenger&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hang_out&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [avoid]&lt;br /&gt;
                 terrain=C*,H*,M*,A*,S*,*^F*&lt;br /&gt;
             [/avoid]&lt;br /&gt;
             [mobilize_condition]&lt;br /&gt;
                 [have_unit]&lt;br /&gt;
                     side=1&lt;br /&gt;
                     count=7-99&lt;br /&gt;
                 [/have_unit]&lt;br /&gt;
             [/mobilize_condition]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Healer Support Micro AI (ai_type=healer_support) ==&lt;br /&gt;
&lt;br /&gt;
The Healer Support Micro AI configures the healers of a side to stay behind the battle lines and heal injured and/or threatened units rather than participate in combat under all circumstances.  You can set different levels of aggressiveness for the healers.  &lt;br /&gt;
&lt;br /&gt;
'''Notes'''&lt;br /&gt;
* By default, ''all'' healers of the AI side are controlled by this AI.  This includes in particular also the side leader, if the leader is a healer.  If you do not want the leader to be included (and possibly be moved away from the keep etc.) use the [filter] tag with canrecruit=no (see below).&lt;br /&gt;
* It is not always better to use this Micro AI than the default AI, as it takes healers (which can be strong units) away from the pool of units to choose for attacks.&lt;br /&gt;
* {{DevFeature1.15|3}} This Micro AI now takes unit guardian status and the passive_leader aspect into account.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Healers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Healer Support Micro AI by using&lt;br /&gt;
 ai_type=healer_support&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Healer Support specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the healer units.  If set, only units on the AI side passing this filter ''and'' having the healing ability are considered by this Micro AI.&lt;br /&gt;
*'''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting units to receive healing. If set, only units on the AI side passing this filter ''and'' the other healee selection criteria will be selected for healing by this Micro AI.&lt;br /&gt;
* '''aggression'''=1.0: (float) Sets the aggressiveness of the AI.  This parameter is set up as a float to accommodate future functionality, but it currently acts as a boolean: if set to zero, the AI will never let its healers participate in combat, for all other values it allows them to attack after all other units have attacked and if the healers cannot find any more units to support.  The former behavior might be appropriate in scenarios with only one or few valuable healers, while the latter might work better in scenarios with many healers.&lt;br /&gt;
* '''injured_units_only'''=no: (boolean) If set to 'yes', the AI will only move healers next to units that are already injured and skip units that currently have full hitpoints, but might get injured during the next enemy turn.&lt;br /&gt;
* '''max_threats'''=9999: (integer) The maximum allowable number of enemies that can attack a hex in order for it to be considered for a healer.  As an example, setting 'max_threats=0' means that the AI only moves healers to locations that are entirely safe from the enemy (assuming that none of the units currently on the map dies).  Note that the value of this key is checked against the number of enemies that can make it to the hex, not the number of adjacent hexes from which the healer could be attacked.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI. Note: as attacks by the healers are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The Healer Support Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded.  It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Healer Support Micro AI configuration from the &amp;quot;Healers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             aggression=0&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Lurkers Micro AI (ai_type=lurkers) ==&lt;br /&gt;
&lt;br /&gt;
The Lurker Micro AI controls some or all units of a side in &amp;quot;lurker mode&amp;quot;.  A lurker is a unit that is capable of moving across most terrains, but that only stops on and attacks from specific terrain.  It might also have the ability to hide on this terrain (which is the reason why it is called a lurker).&lt;br /&gt;
&lt;br /&gt;
Lurkers move individually without strategy and always attack the weakest enemy within their reach.  If no enemy is in reach, the lurker does a random move instead - or it just sits and waits (lurks).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of different types of lurker behavior, check out the &amp;quot;Lurkers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Lurkers Micro AI by using&lt;br /&gt;
 ai_type=lurkers&lt;br /&gt;
in the [micro_ai] tag&lt;br /&gt;
&lt;br /&gt;
===Lurkers specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
* '''[filter]''': Specifies which units of the side are controlled by the Lurker Micro AI.  This is a [[StandardUnitFilter|Standard Unit Filter]].  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': Specifies the terrain from which the lurkers attack. This is a [[StandardLocationFilter|Standard Location Filter]].&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_wander]''': Specifies the terrain on which lurkers end their moves if there is no enemy in reach. This is a [[StandardLocationFilter|Standard Location Filter]].  If [filter_location_wander] is not given, it defaults to [filter_location].&lt;br /&gt;
* '''stationary'''=no: (boolean)  If set to yes, a lurker never moves if there is no enemy within attack range.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Lurkers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=4&lt;br /&gt;
             ai_type=lurkers&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Naga Fighter&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=W*,S*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             [filter_location_wander]&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location_wander]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Messenger Escort Micro AI (ai_type=messenger_escort) ==&lt;br /&gt;
&lt;br /&gt;
The Messenger Escort Micro AI lets you define a set of waypoints through which it tries to move one or several of its units, the messenger(s).  The other units escort the messenger, trying to protect it and attacking enemies that are in its way.  Note that a messenger might not strictly hit intermediate waypoints, but that getting close is good enough, especially if a waypoint is occupied by an enemy unit.  It does, however, always try to get to the last waypoint.&lt;br /&gt;
&lt;br /&gt;
'''Notes on the use of several messengers:''' {{DevFeature1.13|0}}  &lt;br /&gt;
* This Micro AI can control several messengers at the same time, which can but do not have to move in a group.  They can be put on the map altogether in one location, or be spread out on the map or across different turns.&lt;br /&gt;
* The farther a messenger has moved through the chain of waypoints, the higher a priority it is for the AI, meaning that farther advanced (on the map) messengers are moved first, and that the escort units try to clear the way and stay with those messengers more so than with those further back on the map.  This order can be inverted using the invert_order= key.&lt;br /&gt;
* If several messengers are on the map, escort units might move from one to the other, the AI making a judgement call where they are needed most.  Closeness to a messenger is generally the most important criterion, but not the only one.&lt;br /&gt;
* If a messenger dies, the escort units previously staying with it now join one of the other messengers if there are any left (otherwise they follow default AI behavior).&lt;br /&gt;
* If you want certain escort units to stay always with the same messenger, use several messenger [micro_ai] tags with different [filter]/[filter_second] combinations.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Messenger Escort Micro AI by using&lt;br /&gt;
 ai_type=messenger_escort&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Messenger Escort specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': {{DevFeature1.13|0}} (Previous to 1.13.0, only id= was allowed.) The id of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated list of waypoint coordinates through which the messenger will go in the given order to reach its goal, given by the last coordinate.  If you want the messenger to go directly to the goal, simply enter a single x,y coordinate here.  Note that the messenger does not have to hit each waypoint exactly (except for the final one), getting close to them is good enough. {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Messenger Escort AI consists of 3 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-2&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_death_chance'''=0.67 and '''messenger_death_chance'''=0.0: (both floats; ranging from 0 to 1)    If the messenger is adjacent to an enemy at the end of the move, it will only attack the enemy if the chance of death of the enemy is &amp;gt;= enemy_death_chance and if the messenger's chance of death is &amp;lt;= messenger_death_chance (or if it is an attack at a range for which the enemy does not have a weapon).&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|0}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the escort units.  If not set, this defaults to all enemy units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''invert_order'''=no: {{DevFeature1.13|0}} If set to 'yes', the order of priority with which messenger units are considered is inverted, starting with the messenger unit that is the least advanced on the map (see description above).  In most cases, selecting this option results in worse performance of the AI, so use it with caution.  (And obviously it has no effect at all if there is only one messenger controlled by the AI.)&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Messenger Escort&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=messenger_escort&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=messenger&lt;br /&gt;
             waypoint_x=31,24,27,28&lt;br /&gt;
             waypoint_y=20,14,7,1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Patrol Micro AI (ai_type=patrol) ==&lt;br /&gt;
&lt;br /&gt;
The defines a number of waypoints for a unit (or units) to follow.  Options can be set whether the route should be followed in a loop, as an out-and back, only once, whether units encountered along the way should be attacked, etc.&lt;br /&gt;
&lt;br /&gt;
No matter what options are chosen, getting to the next waypoint always takes priority over attacking for a patroller.  The AI thus prefers to move the patrol unit around enemies rather than straight for them.  Also, if a waypoint is occupied by a unit the AI is not instructed to attack, it will (eventually) abandon that waypoint once it gets close enough and move on to the next one.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different patrol behaviors, check out the &amp;quot;Patrols&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Patrol Micro AI it by using&lt;br /&gt;
 ai_type=patrol&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Patrol specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated lists of the waypoint coordinates through which the patrol travels (in order). {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack''': By default (if this key is not set), the patrol unit attacks any unit it ends up next to after a move.  Alternatively, this parameter can be set to a comma-separated list of unit ids. If that is done, the patrol only attacks these units when it ends up next to them and ignores any other unit.  This can in turn be used to have the patroller not attack at all, by setting ''attack='' to a non-existing id.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''one_time_only'''=no: (boolean)  If set to 'yes', the unit goes through the patrol route only once and then hangs out at the last waypoint.  It also always attacks any enemy unit on the last waypoint, independent of what ''attack='' is set to.&lt;br /&gt;
* '''out_and_back'''=no: (boolean)  By default, a patrol unit returns to the first waypoint after reaching the last, doing the patrol route as a loop.  If this parameter is set to 'yes', the unit instead reverses its course and perpetually does out and backs of the route.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Patrols&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=patrol&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Konrad&lt;br /&gt;
             waypoint_x=9,24,25&lt;br /&gt;
             waypoint_y=21,23,15&lt;br /&gt;
             one_time_only=yes&lt;br /&gt;
             attack=Gertburt&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Protect Unit Micro AI (ai_type=protect_unit) ==&lt;br /&gt;
&lt;br /&gt;
The Protect Unit Micro AI tries to move one or several units to a given location while keeping them safe from enemy attacks. It does so by keeping the protected units away from enemy units and close to friendly units. Other units on the AI side are controlled by the default Wesnoth AI.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Protect Unit&amp;quot; and &amp;quot;The Elves Besieged&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Protect Unit Micro AI by using&lt;br /&gt;
 ai_type=protect_unit&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Protect Unit specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[unit]''': A separate [unit] tag is required for each protected unit(s).  Each tag has required keys id=, goal_x=, goal_y= for each unit, as shown in the example below. {{DevFeature1.15|0}} '''goal_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''disable_move_leader_to_keep'''=no: (boolean) If set to 'yes', side leaders protected by this AI do not try to return to their keep.  This is necessary for this Micro AI to work with units that are side leaders and that are supposed to move to an off-keep location.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
*The Protect Unit Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].  Depending on the parameters, it might also modify the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|move leader to keep candidate action]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded. It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;HttT: The Elves Besieged&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=protect_unit&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Delfador&lt;br /&gt;
                 goal_x,goal_y=1,2&lt;br /&gt;
             [/unit]&lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
                 goal_x,goal_y=1,1&lt;br /&gt;
             [/unit]&lt;br /&gt;
 &lt;br /&gt;
             disable_move_leader_to_keep=true&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Recruiting Micro AI==&lt;br /&gt;
=== General Comments on the Recruiting Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Recruiting Micro AI replaces the default recruitment AI by alternative recruiting patterns.  Currently there are two recruiting algorithms you can choose from, random recruiting and the rush recruiting used in the new (as of Version 1.11.1) Experimental AI (see below).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of a different recruiting pattern, check out the &amp;quot;Recruiting&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  You can also explore the unit choices by watching the Experimental AI in a multiplayer game.&lt;br /&gt;
&lt;br /&gt;
'''Note:''' Obviously, the Recruitment Micro AI modifies the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|recruitment candidate action]].&lt;br /&gt;
&lt;br /&gt;
=== Random Recruitment AI (ai_type=recruit_random) ===&lt;br /&gt;
&lt;br /&gt;
This AI randomly selects a unit type from the recruitment list of the side.  The probability of choosing each type and the behavior in low-gold situations can be influenced with optional parameters.&lt;br /&gt;
&lt;br /&gt;
Enable the Random Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_random&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* '''[probability]''': This tag sets the probability of selecting the given unit type.  It has two required sub-keys:&lt;br /&gt;
** '''type''': comma-separated list of unit types to which this probability is to be applied&lt;br /&gt;
** '''probability''': (non-negative float)  The probability with which these unit types is to be selected.  All unit types for which no [probability] tag is defined receive a default probability of 1.0, thus values should be chosen relative to that.  As an example, in the code below swordsmen and peasants are given a probability of 8, while no other units have assigned probabilities.  Thus, each of these two unit types is 8 times more likely to be selected than any other individual unit type.&lt;br /&gt;
* '''skip_low_gold_recruiting'''=no: (boolean)  By default, the random recruitment AI chooses a unit to recruit only from those types for which it has sufficient gold.  If this parameter is set to 'yes', it will instead choose from all available unit types and end recruiting for the turn if there is not enough gold to recruit it this turn (even if other, affordable unit types exist).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a random recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_random&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [probability]&lt;br /&gt;
                 type=Swordsman,Peasant&lt;br /&gt;
                 probability=8&lt;br /&gt;
             [/probability]&lt;br /&gt;
 &lt;br /&gt;
             skip_low_gold_recruiting=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Rush Recruitment AI (ai_type=recruit_rushers) ===&lt;br /&gt;
&lt;br /&gt;
This AI replaces the default recruiting with the rush recruiting used in the new (as of Version 1.11.1) Experimental AI.&lt;br /&gt;
&lt;br /&gt;
Enable the Rush Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_rushers&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* {{DevFeature1.15|2}} '''high_level_fraction'''=0: (non-negative number) The approximate fraction of units of level 2 or higher to be recruited. This is defined as fraction of units on the map, not of new units being recruited during a turn or over several turns (which makes a difference if there are already units on the map). The effect is also cumulative per level (starting from level 2), meaning that 1/3 of L2 units will be recruited in the example above, 1/3*1/3=1/9 L3s etc. The default value is zero, which leaves the high-level unit recruiting to the default Experimental AI algorithm resulting usually in very few such units being recruited.&lt;br /&gt;
* '''randomness'''=0.1: (number)  A random number is applied to the rusher recruit engine's score to prevent the recruitment pattern from being too predictable. 0 causes no randomness to be applied, while larger numbers increase the random effect. A value of 1-2 generates results in which the random effect is approximately equal to the scored effect. Extremely high values are essentially entirely random.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a rush recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_rushers&lt;br /&gt;
             action=add&lt;br /&gt;
          [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Simple Attack Micro AI (ai_type=simple_attack) ==&lt;br /&gt;
&lt;br /&gt;
The Simple Attack Micro AI is meant as an addition to the default combat candidate action for when one wants to force the AI to do attacks that it would otherwise not do (for example on units that are 1 XP from leveling) or to execute certain attacks before all others (for example, attack with all &amp;quot;expendable&amp;quot; units first).  It is not set up to be a replacement for the default attacks.  Some notes:&lt;br /&gt;
&lt;br /&gt;
* The algorithm is different from and simpler than that of the default combat CA.  For example, no attack combinations are considered, only individual attacks.  As a result, the [[AiWML#List_of_AI_Aspects|default aspects]] (aggression, caution, [avoid], ...) are not taken into account.  Also, if lots of units are involved, evaluation of this Micro AI is potentially quite a bit slower than of the default attacks.&lt;br /&gt;
&lt;br /&gt;
* If an attack matching the Standard Unit Filters described below is found, this attack will always be executed, no matter how bad the odds are.  Thus, this Micro AI can be used to force the AI to do attacks it would otherwise avoid.&lt;br /&gt;
&lt;br /&gt;
* Other than selecting the attacker/defender units, this AI is currently not configurable.  Let us know if you would like to see certain options to be added.  (No guarantees that we will add them, but we will definitely consider them.)&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Simple Attack&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=simple_attack&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Simple Attack specific keys for the [micro_ai] tag===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  It is, however, in general not desirable to use this AI without at least one of the optional Standard Unit Filters below, as this AI is not meant as a replacement of the default combat candidate action.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=110000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 110,000, which means that it is executed right before the default combat candidate action.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Simple Attack action.  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units the AI should attack.  It defaults to all enemy units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
* '''weapon'''=-1: (integer) The number of the weapon that should be used for this attack.  Weapon numbers start at 1 (not 0).  -1 stands for &amp;quot;best weapon&amp;quot; (by whatever criterion the AI chooses to use).   '''Available from Wesnoth 1.11.8'''&lt;br /&gt;
&lt;br /&gt;
Below are 2 examples of Simple Attack Micro AI configurations from the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  The first one shows how to force the AI to attack with all Soulless and Walking Corpse units first:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110000&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless,Walking Corpse&lt;br /&gt;
             [/filter]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
The second executes attacks by Soulless on all enemy units that are 1 XP from leveling (something the RCA AI would generally not do).  Note that the Lua code bit is simply there in order to code the &amp;quot;1 XP from advancing&amp;quot; condition.  It is not needed for most other SUFs, as can be seen in the previous example.&lt;br /&gt;
         [lua]&lt;br /&gt;
             code=&amp;lt;&amp;lt;&lt;br /&gt;
                 function close_to_advancing(unit)&lt;br /&gt;
                     if (unit.experience &amp;gt;= unit.max_experience-1) then&lt;br /&gt;
                         return true&lt;br /&gt;
                     else&lt;br /&gt;
                         return false&lt;br /&gt;
                     end&lt;br /&gt;
                 end&lt;br /&gt;
             &amp;gt;&amp;gt;&lt;br /&gt;
         [/lua]&lt;br /&gt;
 &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110001&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless  # No Walking Corpses; L0 units don't advance enemy&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 lua_function = &amp;quot;close_to_advancing&amp;quot;&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
Several Simple Attack Micro AIs can be combined for the same AI side.  The two examples above are both used (note the different values for ca_score) in the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
== Custom Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|5}}&lt;br /&gt;
&lt;br /&gt;
From 1.13.5 onwards, adding a new Micro AI can be easily done without altering any core files or overriding the micro_ai tag. The [micro_ai] tag now looks up its ''ai_type'' parameter in the &amp;lt;code&amp;gt;wesnoth.micro_ais&amp;lt;/code&amp;gt; table and uses the micro AI setup function found there to determine which keys are required and allowed and what candidate actions to add.&lt;br /&gt;
&lt;br /&gt;
A micro AI setup function takes as its single parameter the contents of the [micro_ai] tag and returns three separate values:&lt;br /&gt;
*'''required''': A list of required keys and tags, as strings. Tags are indicated by enclosing them within square brackets in the string. If a key or tag in this list is missing from the [micro_ai] tag, an error will be printed.&lt;br /&gt;
*'''optional''': Similar to above, a list of optional keys and tags, as strings. No error will be printed if a key or tag in this list is missing, nor will an error be printed if it is present (as would be the case for unrecognized keys or tags).&lt;br /&gt;
*'''ca_params''': A table specifying the candidate actions that constitute the micro AI. It should contain an ''ai_id'' key (which specifies a base ID to be used for candidate actions of this micro AI) and a series of candidate action definitions. Each candidate action definition is a table containing the following keys:&lt;br /&gt;
**'''ca_id''': The ID used for the candidate action. This will be combined with the ''ai_id'' to produce the final ID.&lt;br /&gt;
**'''location''': Path to the Lua file that defines the candidate action.&lt;br /&gt;
**'''score''': The maximum score for the candidate action. If the [micro_ai] tag specified a ''ca_score'' parameter, the score here should normally be based on that.&lt;br /&gt;
&lt;br /&gt;
(The return statement would thus look like &amp;lt;syntaxhighlight lang='lua' inline&amp;gt;return required, optional, ca_params&amp;lt;/syntaxhighlight&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
Since the function has full access to the contents of the [micro_ai] tag, it can conditionally add candidate actions to '''ca_params''' depending on the values of other keys within the [micro_ai] tag. It can also perform other actions such as adding facets to an aspect; if it does such things, it should pay attention to the ''action'' key to determine whether it's being added or removed.&lt;br /&gt;
&lt;br /&gt;
There are two useful helper functions that can be used by Micro AI setup functions requiring aspect changes. They can be brought into scope by a line similar to the following:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='lua&amp;gt;local MAIH = wesnoth.require(&amp;quot;ai/micro_ais/micro_ai_helper.lua&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* MAIH.add_aspects(side, aspect_params)&lt;br /&gt;
* MAIH.delete_aspects(side, aspect_params)&lt;br /&gt;
&lt;br /&gt;
The ''aspect_params'' parameter is an array of tables, each of which contains the following keys:&lt;br /&gt;
*'''aspect''': The aspect to change.&lt;br /&gt;
*'''facet''': The WML defining the facet. This must include an ''id'' key in order for the aspect to be correctly deleted. For maximum compatibility, incorporate the ''ca_id'' key from the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=65217</id>
		<title>Micro AIs</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Micro_AIs&amp;diff=65217"/>
		<updated>2019-12-12T16:51:03Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Fast Micro AI (ai_type=fast_ai) */ complete list of default AI CAs replaced&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== A Few General Words about Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
* Micro AIs allow Wesnoth campaign and scenario authors to add new AI functionality without the need for any AI programming. They bring new specialized behavior to Wesnoth that cannot be achieved, at least not easily, by adjusting default AI parameters (such as those described at [[AiWML]]) or with WML code.&lt;br /&gt;
&lt;br /&gt;
* A Micro AI is activated and configured (or deleted) via the [micro_ai] tag, requiring only a few lines of WML code.&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.6''', the Micro AI behavior was inconsistent when it came to '''hidden enemies and petrified units'''.  Some Micro AIs ignored those, while others did not.  In some case there was even a potential for AI errors when an AI unit ran into hidden or petrified enemies.  As of 1.13.6, this has all been fixed.  The Micro AIs now correctly and consistently ignore hidden units and treat petrified units as blocking the hex they are on but having no effect otherwise, in the same way as the default AI does.  '''Important notes:'''&lt;br /&gt;
** It is not possible to re-include hidden/petrified units via the Standard Unit Filters (SUFs) used by some of the Micro AIs. In fact, if this is attempted, the filters will generally not match any unit.&lt;br /&gt;
*** This only applies to SUFs which go directly into the [micro_ai] tag. Any SUF that is a sub-tag of another filter (for example, of a Standard Location Filter) is taken literally and returns all units matching the filter. If hidden or petrified units are to be excluded from such a sub-filter, that needs to be specified directly in that filter.&lt;br /&gt;
** We also note that, because all the Micro AIs are written in Lua, it is possible to [[Micro_AIs#Using_Development_.281.13.29_Version_Micro_AIs_in_your_Stable_.281.12.29_Version_Add-on|move a Micro AI's code into one's add-on]] and modify it to whatever scenario-specific behavior is desired (this does not only apply to hidden/petrified units, but to all behavior). Setting up general Micro AI behavior that works in every possible setting is not feasible, but adapting it to work (only) in the specific setting of a given scenario is often quite simple. If you do not know how to do this, please post a message on the Wesnoth forums and we will be happy to help you with it.&lt;br /&gt;
&lt;br /&gt;
* The '''[[AiWML#List_of_AI_Aspects|default AI aspects]] are, for the most part, not taken into account by the Micro AIs'''.  For example, unless noted otherwise, specifying a value for ''aggression'' does not change the MAI behavior, and locations specified in an ''[avoid]'' tag generally can be entered by units controlled by an MAI.  This is a feature of the Micro AIs, not a bug.  If you feel that a certain default AI aspect should also apply to a Micro AI, please let us know in the [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 Micro AIs Feedback thread].&lt;br /&gt;
&lt;br /&gt;
* '''Before Wesnoth 1.13.7''', the Micro AIs did not all work reliably when the '''AI side is under shroud''' and it may have been necessary to turn shroud off for the side when using a Micro AI. {{DevFeature1.13|7}} The Micro AIs now all work for sides under shroud. They do this by disregarding shroud for terrain purposes when path finding, consistent with default Wesnoth AI behavior, while still ignoring hidden units (including those hidden under shroud) correctly.&lt;br /&gt;
&lt;br /&gt;
* Most Micro AIs in their default setting apply to all units of a side, although many of them provide parameters for filtering the units that should be affected by the new behavior.  If the Micro AI is applied only to part of the units of a side, the other units follow the default Wesnoth AI behavior.&lt;br /&gt;
&lt;br /&gt;
* Micro AIs are available in mainline since Wesnoth 1.11/1.12.&lt;br /&gt;
&lt;br /&gt;
== Micro AI Test and Demo Scenarios ==&lt;br /&gt;
&lt;br /&gt;
All Micro AIs described here can be checked out in a number of test and demo scenarios.  Test scenarios can be accessed from the command line by typing&lt;br /&gt;
 path/wesnoth-executable -t micro_ai_test&lt;br /&gt;
or (depending on your system and Wesnoth version)&lt;br /&gt;
 path/wesnoth-executable -tmicro_ai_test&lt;br /&gt;
This starts up a &amp;quot;switchboard scenario&amp;quot; in which you can select the Micro AI demonstration scenario you want to check out.  Here, ''path/wesnoth-executable'' needs to be replaced by whatever the path and filename of the Wesnoth executable is on your system.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|8}}&lt;br /&gt;
&lt;br /&gt;
You can also access the test scenarios by assigning a hotkey to &amp;quot;Start Test Scenario&amp;quot; in the preferences. Then, simply press the hotkey at the titlescreen, possibly wait out the loading screen, and select the desired scenario from the list.&lt;br /&gt;
&lt;br /&gt;
== Setting up a Micro AI ==&lt;br /&gt;
&lt;br /&gt;
Setting up a Micro AI requires only one simple step:&lt;br /&gt;
==== Activating and configuring (or deleting) a Micro AI ====&lt;br /&gt;
&lt;br /&gt;
Micro AIs are activated, deleted and configured using the [micro_ai] tag.  This tag needs to be placed in [[ActionWML]], that is, in an event, a menu option or the like.  As an example, the following code activates the healer_support Micro AI in its default configuration for Side 2 from the beginning of the scenario:&lt;br /&gt;
     [event]&lt;br /&gt;
         name=prestart&lt;br /&gt;
 &lt;br /&gt;
         # Configure the healer support micro AI&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
     [/event]&lt;br /&gt;
'''Keys required for all Micro AIs:'''&lt;br /&gt;
All [micro_ai] tags must contain the following three required keys:&lt;br /&gt;
* '''side''': The side for which the Micro AI is to be added, changed or deleted.  This has to be an individual side number, lists of sides are not supported.&lt;br /&gt;
* '''ai_type''': The type of Micro AI to be added, changed or deleted.  See the following sections for allowed values.&lt;br /&gt;
* '''action''' (string): The action to take concerning the Micro AI.  The following values are allowed: &lt;br /&gt;
** ''add'': Add a Micro AI to a side.&lt;br /&gt;
** ''change'': Change the configuration of an existing Micro AI.  Note that this does not only change the specific parameters provided in the tag, but it replaces the entire existing Micro AI by a new version with the new (and only the new) configuration.  It is therefore equivalent to using first the ''delete'' and then the ''add'' action.&lt;br /&gt;
** ''delete'': Delete an existing Micro AI from a side.&lt;br /&gt;
&lt;br /&gt;
'''Keys required under certain circumstances:'''&lt;br /&gt;
* '''ca_id''': (string) If several MAIs of the same type are used for the same side and one or several of these are to be changed or deleted during a scenario, this key needs to be included so that the [micro_ai] tag knows which of them needs to be changed/deleted.  The same is also true if a Micro AI is changed more than once during a scenario.  We therefore '''strongly recommend always setting ''ca_id''''' for MAIs which are, or might be, changed or deleted during a scenario.&lt;br /&gt;
** '''Note:''' The id(s)/name(s) actually assigned to the CA(s) of the MAI by the engine might be different from the parameter passed here.  It is, however, sufficient to use this ca_id with the [micro_ai] tag add/delete/change actions to identify the MAI uniquely.  (In other words: you don't have to worry about this if you always use the [micro_ai] tag, but might have to figure out the actual name(s) of the CA(s) if you want to delete or change them manually.)&lt;br /&gt;
* Other keys may also be required depending on the Micro AI.  In additional, a number of optional keys may be available to configure the AI behavior.  See the sections on the individual Micro AIs for details.&lt;br /&gt;
&lt;br /&gt;
'''Required keys for deleting a Micro AI:'''&lt;br /&gt;
&lt;br /&gt;
Only a subset of the keys required for setting up a Micro AI are needed when deleting it.  '''side''', '''ai_type''' and '''action'''=delete always need to be provided.  '''ca_id''' needs to be given if it was provided when adding the Micro AI (see above for when this is needed).&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* The ''add'' and ''change'' actions ignore all keys that do not apply to the respective Micro AI type.  The ''delete'' action ignores all keys other than the required keys listed above.&lt;br /&gt;
&lt;br /&gt;
* Some of the Micro AIs use and/or modify default AI components (candidate actions or aspects) when they are added, or reset these components to their default values when they are deleted.  Thus, if you have modified the AI yourself using the default CAs or aspects, some of these modifications might be affected, or might be interfering with the Micro AI.  The sections below indicate whether a Micro AI modifies any default AI components.&lt;br /&gt;
&lt;br /&gt;
===Combining Different Micro AI Types for the Same Side===&lt;br /&gt;
&lt;br /&gt;
No extra steps need to be taken when combining different Micro AIs on the same side.  Simply set up the [micro_ai] tags for all AIs you want to use.&lt;br /&gt;
&lt;br /&gt;
'''Notes:''' &lt;br /&gt;
* While it is ''technically'' possible to combine any number of Micro AIs, this does not mean that all Micro AIs work together in practice.  Some are not compatible because they control units in contradictory ways.  &lt;br /&gt;
* Not all combinations that ''should'' work have been tested yet.  If you find a combination that you believe should work but doesn't, [http://forums.wesnoth.org/viewtopic.php?f=10&amp;amp;t=39456 please let us know] and we will see if that can be fixed.&lt;br /&gt;
* The candidate action score almost certainly has to be set (using the ca_score= key) for one or several of the MAIs if several of them are used on the same side.&lt;br /&gt;
&lt;br /&gt;
== Animals Micro AIs ==&lt;br /&gt;
=== General Comments on the Animal Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Animals Micro AIs are meant to simulate the behaviors of certain types of animals.  They are, however, set up so that they can be used with arbitrary unit types and might be applicable in quite diverse situations.  The AIs nevertheless remain named after the animals for which they were written originally, so that they are (to some extent) descriptive of their behavior.&lt;br /&gt;
&lt;br /&gt;
Brief descriptions of the AI behaviors are given in the following subsections.  To get a better feeling for their behavior, check out the &amp;quot;Animals&amp;quot;, &amp;quot;Swarm&amp;quot;, &amp;quot;Wolves&amp;quot; and &amp;quot;Dragon&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  Note that some of the animal AIs are designed for animal types that do not exist in mainline.  In the test scenario, other animal types are therefore substituted for them.&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Wolves (ai_type=wolves) ===&lt;br /&gt;
&lt;br /&gt;
The Wolves Micro AI organizes all &amp;quot;wolf&amp;quot; units (predators) of the side to hunt in a single pack.  They actively chase after the closest prey and try to corner it (not always super successfully), but are easily distracted by other enemy units coming into attack range, to the point where the pack might split up.  The wolves can, however, be told to attack only prey units as well as to try to avoid other types of units (such as larger predators).  When no prey is left, the wolves wander randomly.&lt;br /&gt;
&lt;br /&gt;
This AI works best for a small number of predators (2 - 5) and sparse prey.  Also, the predators should be put on the map in a pack, that is, reasonably close to each other.  For a demonstration of the Wolves AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Note that another wolves AI exists, called [[Micro_AIs#Animals_AI:_Multi-pack_Wolves_.28ai_type.3Dwolves_multipacks.29|Multipack Wolves]], which distributes the wolves of a side into several packs, rather than all of them into one single pack as it is done here.&lt;br /&gt;
&lt;br /&gt;
Enable the Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the predator units.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the prey units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_only_prey'''=no: In the default configuration, the wolves attack all enemy units in attack range.  If this key is set to 'yes', they only attack prey units.&lt;br /&gt;
* '''avoid_type''': Comma-separated list of unit types (on enemy sides) which the wolves try to avoid.  They try to stay out of these units' way as much as possible when moving around (except when they are moving in to attack another enemy unit) and never attack them.&lt;br /&gt;
* '''ca_score'''=90000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 90,000, which is lower than the default combat CA, but higher than most other default CAs which move units.  It can be changed by setting this parameter but in most cases this will not make sense.  Note that the Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Notes on default (RCA) AI aspects:''' &lt;br /&gt;
* If ''attack_only_prey'' or ''avoid_type'' are set, the Wolves Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* Attacks by the wolves are left to the default (RCA) AI, meaning that the value of ''aggression'' has an influence on the behavior.&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI is taken into account. Note: as attacks by the wolves are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=6&lt;br /&gt;
             ai_type=wolves&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Wolf&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Vampire Bat&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             avoid_type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Multi-pack Wolves (ai_type=wolves_multipacks) ===&lt;br /&gt;
&lt;br /&gt;
The Multi-pack Wolves Micro AI is different from the [[Micro_AIs#Animals_AI:_Wolves_.28ai_type.3Dwolves.29|Wolves Micro AI]] in that there can be an arbitrary number of wolf packs, with the pack size being a free parameter.  At the beginning of the scenario, close wolves are grouped into packs in a semi-methodical way.  If the wolves are put on the map in distinct groups of close wolves of the correct number, they will be joined into packs matching these groups.  If, on the other hand, they are spread out all over the map, the method of assigning them to packs is semi-random.&lt;br /&gt;
&lt;br /&gt;
Wolves of the same pack begin by joining each other on the map.  After that, they stay together until only one wolf is left, which then tries to join up with an incomplete pack or with other single wolves.  Individual wolves entering the map during the scenario behave in that way as well.&lt;br /&gt;
&lt;br /&gt;
A second difference to the other Wolves AI is that wolves do not actively hunt here.  For the most part they just wander (often long distance).  However, the pack ferociously (and without regard for its own health) attacks any enemy units that come into range, as long as that does not mean separating the pack by more than a few hexes.  Staying together, or joining with a new wolf assigned to the pack, is the only thing that takes priority over satisfying the wolves' thirst for blood.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Multi-pack Wolves AI behavior, check out the &amp;quot;Wolves&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Multi-pack Wolves Micro AI by using&lt;br /&gt;
 ai_type=wolves_multipacks&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Multi-pack Wolves AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''pack_size'''=3: (integer) The size of the packs.&lt;br /&gt;
* '''show_pack_number'''=no: If set to &amp;quot;yes&amp;quot;, the wolves' pack numbers will be shown below them.  Note that this means that the pack number of a wolf killed in an attack will remain as a label on the map.  &lt;br /&gt;
* '''type'''=Wolf: Comma-separated list of the unit types which the AI controls. The default value is &amp;quot;Wolf&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a wolves_multipacks [micro_ai] tag usage from the &amp;quot;Wolves&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=wolves_multipacks&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             show_pack_number=yes&lt;br /&gt;
             pack_size=4&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Big Animals (ai_type=big_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Big Animals Micro AI is a simulation of large predators that wander and hunt alone, such as Bears, Giant Spiders or Yetis.  For the most part, these just wander on the terrain that's been defined for them, but they attack enemy units if those happen to come into range.  The AI can be set up so that the Big Animals stay out of the way of other units (such as other large predators), in which case they only attack those if cornered or if they run into them accidentally (such as ambushers).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Big Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Big Animals Micro AI by using&lt;br /&gt;
 ai_type=big_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the animals to which this AI is applied.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': The big animals often go for long distance (multi-turn) journeys.  This tag is a [[StandardLocationFilter|Standard Location Filter]] describing the terrain from which the next goal is chosen.  The default is all terrain on the map. &lt;br /&gt;
* '''[filter_location_wander]''': The terrain on which the big animals will end their moves. This is a [[StandardLocationFilter|Standard Location Filter]].  The default is all terrain on the map.&lt;br /&gt;
* '''[avoid_unit]''': A [[StandardUnitFilter|Standard Unit Filter]] describing all enemy units that the big animals will avoid.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
Here's an example of big_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=big_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Ghast&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [avoid_unit]&lt;br /&gt;
                 type=Yeti,Giant Spider,Ghast,Footpad&lt;br /&gt;
             [/avoid_unit]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=1-40,1-18&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Forest Animals (ai_type=forest_animals) ===&lt;br /&gt;
&lt;br /&gt;
The Forest Animals Micro AI simulates a number of different animal behaviors that one might find in a forest, namely animals behaving like deer, rabbits and families of tuskers.  In the following, we will refer to those animals by these types, even though the actual unit types can be selected freely.  They behave as follows:&lt;br /&gt;
* Each deer wanders randomly on (selectable) terrain, except when enemies get in its (the deer's) maximum movement range, in which case it flees to the farthest point it can reach.&lt;br /&gt;
* Tuskers exhibit the same behavior as deer, except when an enemy is next to one of the tusklets.  This enemy is attacked unconditionally by the tuskers trying to protect their young.&lt;br /&gt;
* Tusklets blindly follow the closest adult tusker, except when there is no tusker left, in which case they behave the same as deer.&lt;br /&gt;
* Rabbits also wander randomly, but in addition disappear into their holes (if any are within reach) when enemies are close.  They reappear out of their holes at the beginning of the turn, if it is safe.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Forest Animals AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Forest Animals Micro AI by using&lt;br /&gt;
 ai_type=forest_animals&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Forest Animals AI consists of 4 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-3&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''rabbit_type, tusker_type, tusklet_type, deer_type''': Comma-separated lists of the unit types behaving as the respective animals.  If any of these parameters are not set, those types of animal behavior are skipped by the AI.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain on which the forest animals wander when they are not threatened by enemies.&lt;br /&gt;
* '''rabbit_number'''=6: (integer)  The number of rabbits for the AI to have on the map simultaneously.  The AI puts the missing number of rabbits onto the map at the beginning of the AI's turn (when the rabbits come out of their holes).&lt;br /&gt;
* '''rabbit_enemy_distance'''=3: (number)  Rabbits won't spawn in holes that aren't more than this distance away from all enemies.&lt;br /&gt;
* '''rabbit_hole_img''': Rabbit holes are marked by items on the map.  If this parameter is set, it must be the file name of the item image.  If it is not set, all items on the map are treated as rabbit holes.&lt;br /&gt;
&lt;br /&gt;
Here's an example of forest_animals [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=forest_animals&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             deer_type=Vampire Bat&lt;br /&gt;
             rabbit_type=Giant Rat&lt;br /&gt;
             tusker_type=Ogre&lt;br /&gt;
             tusklet_type=Young Ogre&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=*^F*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Herd and Herders (ai_type=herding) ===&lt;br /&gt;
&lt;br /&gt;
The Herding Micro AI sets up an area on the map where a number of herder units (such as sheep dog) control and protect a herd (such as sheep).  For convenience, we simply refer to them as sheep dogs and sheep in the following, even though arbitrary unit types can be selected for either.&lt;br /&gt;
&lt;br /&gt;
Sheep dogs try to keep their sheep safe.  This involves keeping them inside the herding area, positioning themselves in between the sheep and approaching enemies, and attacking the enemies if those get too close.  If no active herding or protecting move is needed, the dogs go to a random location on the perimeter of the herding area.&lt;br /&gt;
&lt;br /&gt;
Sheep wander aimlessly except when a sheep dog is next to them, in which case they run away from the dog.  The dogs exploit this by positioning themselves on the outside of the sheep, if possible.  Sheep also run away from approaching enemies.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Herding AI behavior, check out the &amp;quot;Animals&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Herding Micro AI by using&lt;br /&gt;
 ai_type=herding&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herder units (e.g. dogs).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] describing the herd units (e.g. sheep).  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the terrain forming the perimeter of the herding area.  In other words, this is the path the herders (dogs) use to patrol around the outside of the herding area.  This must be a set of coordinates ''completely enclosing'' the herding area.  Here's an example of setting up a rectangular perimeter:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         x=10-20,10-20,10,  ,20&lt;br /&gt;
         y=11,   21,   11-21,11-21&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
Another possibility is by defining the path by certain terrain on the map (as it is done in the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]), in which case the filter can simply look like this:&lt;br /&gt;
     [filter_location]&lt;br /&gt;
         terrain=Rb&lt;br /&gt;
     [/filter_location]&lt;br /&gt;
* '''herd_x,herd_y''': The coordinate of one hex (''any'' one hex) inside the herding area.  This is needed to define which part of the map is on the ''inside'' of the herding area perimeter defined by [herding_perimeter], and which is the ''outside''. {{DevFeature1.15|0}} '''herd_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''attention_distance'''=8: (integer)  If an enemy is within this distance of a herd unit, but not as close as ''attack_distance'', the herders try to position themselves in between the enemy and the herd unit.&lt;br /&gt;
* '''attack_distance'''=4: (integer)  If an enemy is within this distance of a herd animal, the herders attack this enemy.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Herding AI consists of 6 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-5&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Herding [micro_ai] tag usage from the &amp;quot;Animals&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=herding&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Footpad&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 type=Troll Whelp&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             herd_x,herd_y=32,28&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=Rb&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Hunter (ai_type=hunter) ===&lt;br /&gt;
&lt;br /&gt;
The Hunter Micro AI leads a unit (or units) through hunt-and-rest cycles.  The units wander in a prescribed area of the map, their hunting ground, until an enemy unit comes within range.  Note that the units do not actively chase down enemies, but wander randomly through the hunting ground until an enemy is encountered.&lt;br /&gt;
&lt;br /&gt;
If enemies are within range of a hunter unit, the hunter attacks the weakest of them.  If a kill is made, it then retreats to its rest location, where it stays for a certain number of turns and until fully healed (whatever happens later).&lt;br /&gt;
&lt;br /&gt;
A few more details:&lt;br /&gt;
* If the hunter's way home is entirely blocked on the return (so that there is no possible path), the normal RCA AI takes over its behavior.&lt;br /&gt;
* However, if the way is blocked by an enemy unit occupying the rest hex, that enemy will be attacked unconditionally.&lt;br /&gt;
* A kill only makes the hunter go home when it is the attacker, not as defender.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Hunter AI behavior, check out the &amp;quot;Dragon&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]. &lt;br /&gt;
&lt;br /&gt;
Enable the Hunter Micro AI by using&lt;br /&gt;
 ai_type=hunter&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''home_x,home_y''': The coordinates of the hex to which the hunter returns and rests at after a kill. {{DevFeature1.15|0}} '''home_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the unit's hunting ground.  If this tag is not provided, the hunter uses all of the map.&lt;br /&gt;
* '''rest_turns'''=3: (integer)  The number of turns the AI stays at home_x,home_y after a kill.  It will not leave its home until this number of turns have passed ''and'' it is fully healed.&lt;br /&gt;
* '''show_messages'''=no: (boolean) If set to yes, the hunter announces whenever its behavior changes to the next phase.  Note that this is meant as debug output for the scenario author.  The messages are not translated and are not included in replays (and are only visible to the host in an MP game).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a hunter [micro_ai] tag usage from the &amp;quot;Dragon&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hunter&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Rowck&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=5-30,1-15&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             home_x,home_y=3,15&lt;br /&gt;
             rest_turns=2&lt;br /&gt;
 &lt;br /&gt;
             show_messages=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Animals AI: Swarm (ai_type=swarm) ===&lt;br /&gt;
&lt;br /&gt;
The Swarm Micro AI uses some very simply algorithms to simulate animal swarm behavior.  Without adjacent enemies, they simply do a random move, trying to stay together and at a certain distance from enemies.  However, if an enemy unit is close to any bat, the swarm scatters.  This is particular fun to watch when one places an enemy unit in the middle of the swarm.  After being scattered, the swarm members slowly rejoin, but not in a very organized way.  Sub-swarms or individual bats might roam around for quite some time before they find their way back.  It is also possible that individual bats (or small groups) split off from the larger swarm at times.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of the Swarm AI behavior, check out the &amp;quot;Swarm&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Swarm Micro AI by using&lt;br /&gt;
 ai_type=swarm&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Swarm AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_distance'''=5: (int) The minimum distance kept between units of the swarm and enemies when the swarm moves as a whole.&lt;br /&gt;
* '''scatter_distance'''=3: (int) Enemies within &amp;quot;scatter_distance&amp;quot; hexes of any swarm unit cause the swarm to scatter, by each unit trying to maximize its individual distance from the enemy.&lt;br /&gt;
* '''vision_distance'''=12: (int) Only units within this distance follow the overall swarm motion (either away from an enemy or of the swarm as a whole).  The smaller this value is set, the less likely the swarm is to stay together or rejoin.  This parameter is meant to simulate how far an individual member of the swarm can see, meaning that the swarm is &amp;quot;out of sight&amp;quot; (and the unit will not be able to follow it) if it is more than this distance away.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a Swarm [micro_ai] tag usage from the &amp;quot;Swarm&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=swarm&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Assassin Squad Micro AI (ai_type=assassin) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|6}}&lt;br /&gt;
&lt;br /&gt;
This Micro AI controls one or several assassin units which try to circle around enemy units with the goal of hitting a target unit without being distracted or impeded by other enemies.  The units will always attack the target if such attacks are possible, otherwise they will follow the path of least resistance toward it. Notes:&lt;br /&gt;
&lt;br /&gt;
* The main priority for the AI is to find paths that avoid enemy units. However, if no such path can be found, the assassins will still advance toward the target, taking the path exposing the units to the least potential damage from enemies.&lt;br /&gt;
&lt;br /&gt;
* The units will not necessarily follow the same path. The best path is chosen individually for each unit.&lt;br /&gt;
&lt;br /&gt;
* While it is not a requirement for the AI to work, using ambusher or skirmisher units, or units with the quick trait, makes this AI more lethal in many cases.&lt;br /&gt;
&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded, it does not work otherwise. That means that '''ca_score''' is not a valid key for the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=assassin&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Assassin Squad specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the assassin units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] defining the target unit.&lt;br /&gt;
** It is important to realize that the AI currently only works correctly with a single target unit. It is thus the responsibility of the scenario designer to make sure that this filter matches an individual enemy unit. If several enemy units are to be targeted in the same scenario, several instances of the Assassin Micro AI can be used.&lt;br /&gt;
** The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[prefer]''': A [[StandardLocationFilter|Standard Location Filter]] selecting locations on the map for the assassins to prefer when making their way toward the target unit. This could be used, for example, to make units with the ambush ability move (mostly) through forest or to have the assassins circle around the, say, northern part of the map rather than the south if that is desirable for scenario design purposes.&lt;br /&gt;
** As with avoiding enemies, the units will still move across non-preferred terrain if that is either the only option or ''much'' shorter than preferred terrain&lt;br /&gt;
** Attacking, once the target unit is in range, is not influenced by the [prefer] SLF.&lt;br /&gt;
&lt;br /&gt;
Here is an example of an Assassin Squad [micro_ai] tag usage:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=assassin&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Assassin,Master at Arms,Duelist&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
             [prefer] # Circle around northern part of map&lt;br /&gt;
                 x=1-9, 10-99&lt;br /&gt;
                 y=1-37, 1-17&lt;br /&gt;
             [/prefer]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Bottleneck Defense Micro AI (ai_type=bottleneck_defense) ==&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense Micro AI lets you define a location on the map where the AI can take a defensive stance behind a narrow passage (bottleneck).  Units on the front line are backed up by healers and/or units with the leadership ability.  Injured units are moved to the back and replaced by uninjured (or less injured) units.  The units on the front line only attack when it is safe (no retaliation) or when there is a high chance that they will make a kill or level up.  Using this Micro AI only makes sense if there is no way for the enemy sides to move around the bottleneck and attack the AI's units from behind.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Bottleneck&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Bottleneck Defense Micro AI by using&lt;br /&gt;
 ai_type=bottleneck_defense&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Bottleneck Defense specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
The Bottleneck Defense AI requires two sets of coordinates that define where it should be taking up its defensive stance, and from which side the enemy attacks at that location.&lt;br /&gt;
&lt;br /&gt;
* '''x,y''': Comma separated lists of the hexes on the front line, where strong units are placed.  All hexes on which the AI makes contact with (can be attacked by) the enemy need to be included here.  Units are placed on them in the order in which they are listed here. {{DevFeature1.15|0}} '''location_id''' can be used instead with named/special locations.&lt;br /&gt;
* '''enemy_x,enemy_y''': Comma separated list of the hexes from which the enemy can attack the AI front line units.  This is needed for the AI to know on which side of the front line support units (healers etc.) need to be placed.  In many cases, it is sufficient to provide only one of the enemy front line hexes, but there are geometries for which that does not work.  It is therefore safer to list all enemy front line hexes here. {{DevFeature1.15|0}} '''enemy_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
By default, the AI places healers and units with the leadership ability both on the front line itself (if they are the strongest units available) and on hexes adjacent to and behind the line.  If different placement is desired, these locations can be overridden with the following keys.&lt;br /&gt;
&lt;br /&gt;
* '''active_side_leader'''=no: (boolean)  If set to 'yes', the side leader participates in the bottleneck defense action after the side's gold has been spent.  If set to 'no' (default), the leader follows default AI behavior (sitting on the keep and recruiting, for the most part).&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Bottleneck Defense AI consists of 2 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''healer_x,healer_y''': Comma separated list of hexes where healers are placed.  This can be used to keep healers from the front line (or to take up only certain positions on the front line), and/or to override the default healing positions behind the line.  Healers are placed on them in the order in which the hexes are listed, with the exception that hexes on the line always take priority over hexes behind the line. {{DevFeature1.15|0}} '''healer_loc''' can be used instead with named/special locations.&lt;br /&gt;
* '''leadership_x,leadership_y''': Same as ''healer_x,healer_y'', but for units with the leadership ability. {{DevFeature1.15|0}} '''leadership_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
As an example, in the &amp;quot;Bottleneck&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]], there are three front line hexes, two of which are on hill terrain, while the third is a road with a lower defense rating.  The healer and side leader are supposed to participate in combat because they are strong units, but only from the hill hexes to keep them safer.  This is accomplished by using the following keys settings (check out the scenario to see which hex is which):&lt;br /&gt;
 [micro_ai]&lt;br /&gt;
     side=1&lt;br /&gt;
     ai_type=bottleneck_defense&lt;br /&gt;
     action=add&lt;br /&gt;
 &lt;br /&gt;
     x=14,14,14&lt;br /&gt;
     y= 7, 9, 8&lt;br /&gt;
     enemy_x=13,13&lt;br /&gt;
     enemy_y= 8, 9&lt;br /&gt;
 &lt;br /&gt;
     healer_x=14,14,15,15&lt;br /&gt;
     healer_y= 7, 9, 8, 9&lt;br /&gt;
     leadership_x=14,14,15,15&lt;br /&gt;
     leadership_y= 7, 9, 9 ,8&lt;br /&gt;
     active_side_leader=yes&lt;br /&gt;
 [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Fast Micro AI (ai_type=fast_ai) ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|0}}&lt;br /&gt;
&lt;br /&gt;
This AI is meant as a fall-back AI for scenarios where there are so many units that attacks and moves done by the default AI are slow with long delays before each move.  It replaces the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions (CAs).  It should be obvious that in order to achieve the faster speed, compromises concerning the quality of the moves have to be made.&lt;br /&gt;
&lt;br /&gt;
Enable the Fast Micro AI by using&lt;br /&gt;
 ai_type=fast_ai&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Combat candidate action:'''&lt;br /&gt;
* Attackers are considered individually one at a time.  The best attack found for a unit is executed without checking whether a better attack by another unit might be possible.&lt;br /&gt;
* The default order in which attackers are considered is by decreasing hitpoints, starting with the strongest unit (in terms of HP).  This can be modified with the optional keys below.&lt;br /&gt;
* By default, if weaker units block attack hexes for a stronger unit, they do not move out of the way (this is the same as for the default AI).  If a hex becomes available after an attack (e.g. due to a weaker unit dying in the attack), all attacks that were possible ''before'' this happened are finished first.  Only after that are attacks by the stronger unit reconsidered.&lt;br /&gt;
* If include_occupied_attack_hexes=yes is set, weaker units move out of the way for stronger units if possible.  This can, however slow down the AI depending on the situation on the map.  Use with caution.&lt;br /&gt;
* {{DevFeature1.13|2}} Previous to this version, attacks by the leader were bunched in with the attacks by the other units.  Now leader attacks are treated separately and contain an extra layer of analysis to determine whether the attack position is &amp;quot;safe&amp;quot; (in an approximate way).  Currently, this is done with two simple considerations:&lt;br /&gt;
** Count all enemy units that can reach the leader's attack position.  If there are more than given by optional parameter 'leader_attack_max_units' (default value 3), do not attack.&lt;br /&gt;
** Add up the hitpoints of all enemies that can reach the leader's attack position.  If this value, multiplied by optional parameter 'leader_weight'(default value 2), is larger than the leaders hitpoints, do not attack.&lt;br /&gt;
** Two optional parameters, 'threatened_leader_fights' and 'leader_additional_threat', can also be used to let the leader attack when the threat at the attack hex is comparable to that at the leader's current position.&lt;br /&gt;
** In order to keep this AI as fast as possible, leader attacks are done by a separate candidate action that is only evaluated when all other CAs are done.&lt;br /&gt;
&lt;br /&gt;
'''Move-to-targets, villages and retreat candidate actions:'''&lt;br /&gt;
* The move-to-targets, villages and retreat candidate actions of the default AI are removed.  Move-to-targets and villages are taken over by the Fast Micro AI, whereas retreat is simply not done.&lt;br /&gt;
* This CA moves units toward goals after attacks are done.  Goals are unowned (if the AI side has a leader only) and enemy-owned villages and enemy leaders.&lt;br /&gt;
* For each goal, units are pre-sorted in order of the fewest moves needed to cover the ''distance'' toward the goal (that is, as if move cost on each hex toward the goal were 1).  Units are then considered sequentially and if a unit can get there &amp;quot;efficiently&amp;quot;, its move is executed without considering other units.&lt;br /&gt;
** A move is considered efficient if the move cost to get to the hex is not more than 'move_cost_factor' larger than the distance to the hex.  'move_cost_factor' is an optional key that defaults to 2.  It can be set to values of 1.1 or larger (see below).&lt;br /&gt;
** If the move for the current unit is not found to be efficient, the AI considers the next unit instead.  If no unit can do an efficient move toward the goal, the best (fastest) of the inefficient moves is selected instead.&lt;br /&gt;
&lt;br /&gt;
* Village goals:&lt;br /&gt;
** Village goals are considered before enemy leader goals so that the AI preferentially chooses scouts (or otherwise fast units) for them.&lt;br /&gt;
** The number of units sent toward villages depends on the value of the default AI aspect village_value.  It is set to village_value/4 times the number of units the AI has left to move.  Thus, by default (village_value=1.0) the AI sends a quarter of its units toward villages, and the rest toward enemy leaders.  This means that the meaningful range of values for village_value is 0..4.&lt;br /&gt;
** Only one unit is moved toward each village.  This consideration also overrides the number of units to move toward villages defined by village_value, topping it at the number of unowned/enemy-owned villages on the map.&lt;br /&gt;
** Setting village_value=0 disables moving units toward villages altogether.  That is, not only are no units chosen, but the village targeting phase of the AI is entirely disabled with no evaluation time used for it.  Note that, unlike with the default AI, this also means that no villages within range of an AI unit are grabbed.&lt;br /&gt;
** The order in which villages are chosen as goals is some combination of them being close to the AI side's leader and being spread out over the map.&lt;br /&gt;
&lt;br /&gt;
* Enemy leader goals:&lt;br /&gt;
** The order of enemy leaders goals is set according to their distance from the AI leader with the closest enemy leader being targeted first (if the AI has a leader, otherwise their order is random).  One unit is sent toward each enemy leader in this order, after which the AI starts with the first enemy leader again.  This is repeated until all units have moved.&lt;br /&gt;
&lt;br /&gt;
* Guardians (units with ai_special=guardian set) are ignored by the fast move-to-targets CA.&lt;br /&gt;
&lt;br /&gt;
'''Tips for speeding up/modifying the Fast Micro AI in specific scenarios:'''&lt;br /&gt;
&lt;br /&gt;
A number of optional keys and default AI aspects can be used to modify the behavior of the Fast Micro AI.  The MAI is set up so that it is a reasonable compromise between speed and quality of the moves in most situations.  However, this might not be ideal for all scenarios here are some things that can be done to speed up the AI (or to get better behavior in some situations by doing the opposite):&lt;br /&gt;
* Use large values for the default AI aspect aggression.  The largest delays before attacks happen (in some circumstances) when the Fast MAI has to consider lots of units with attacks left but decides not to attack with them.  See below for a description of how aggression is used by the fast AI.&lt;br /&gt;
* Use large values of move_cost_factor (see above for description of what this does).  If you want to emphasize units that can move across terrain easily (e.g. bats) to be chosen for village grabbing, you might want to use low values of move_cost_factor, but this might slow down the AI.  The default value should work reasonably well in most standard situations, but modifying the value might work better on specific maps.&lt;br /&gt;
* Don't use dungeon_mode=yes unless absolutely necessary, even in most dungeon crawls.  Test the AI without this setting first and only use it if it is the only way to solve the problem.  Often, a minor tweak to the map will have the same effect, without the speed penalty.&lt;br /&gt;
* Use include_occupied_attack_hexes=yes with caution.  It might (or might not) slow down the attack CA significantly, depending on the scenario.&lt;br /&gt;
* Set village_value=0, which makes the AI not consider villages as goals.  This has not been found to make a large difference on the maps tested so far, but it might have an effect on maps with a very large number of villages.&lt;br /&gt;
* Use attack_hidden_enemies=yes on maps where there are no hidden enemies. Excluding hidden enemies from the attack calculation requires an additional filter to be evaluated that takes extra time. In most cases this will not result in a large computation time saving.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_hidden_enemies'''=no: (boolean)  By default, hidden enemies are excluded from the attack calculation.  Setting this to 'yes' makes the AI also consider those enemies as attack targets.&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.13|2}} A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  As with the default AI, units will not step on hexes defined by the [avoid] tag.  In addition, no goals will be chosen that lie on those hexes.  The one minor exception is that a unit stepping out of the way for another unit might temporarily enter the avoided zone.&lt;br /&gt;
* '''dungeon_mode'''=no: (boolean)  As much as possible, the Fast Micro AI works with distances between hexes rather than path finding.  This works pretty well on open maps and even most dungeon crawls (and similar scenarios), but it might result in some units getting temporarily (and in rare cases permanently) stuck on maps with convoluted labyrinths.  If that is the case, setting dungeon_mode=yes triggers more path finding that avoids such situations (but as a result is somewhat slower).&lt;br /&gt;
* '''include_occupied_attack_hexes'''=no: (boolean) If set to 'yes', the AI moves weaker units out of the way for a stronger attacker.  This might lead to better attacks but is slower, so don't use if speed is essential.&lt;br /&gt;
* '''[filter]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the attackers.  Only AI units matching this filter will attack, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes the side= key set to the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|2}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the enemies.  Only enemy units matching this filter will be attacked, analog to filtering units using [filter_own] with the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] for the default AI.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.  It does not affect the move-to-targets CA.&lt;br /&gt;
* '''leader_weight'''=2: (number &amp;gt;= 0) {{DevFeature1.13|2}} A factor specifying how much more valuable the AI considers side leaders than non-leader units in the attack evaluation. This is also used to determine whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''leader_additional_threat'''=1: (number &amp;gt; 0) {{DevFeature1.13|2}} If threatened_leader_fights=yes, the threat at the attack position may be larger by this factor for the leader to leave the keep for an attack.&lt;br /&gt;
* '''leader_attack_max_units'''=3: (number &amp;gt;= 0) {{DevFeature1.13|2}} A number specifying how many enemy units within reach are acceptable when determining whether an attack by the leader is safe (see description of the combat candidate action above).&lt;br /&gt;
* '''move_cost_factor'''=2: (number &amp;gt;= 1.1)  The factor by which the move cost to a goal hex may be larger than the distance to the goal hex for a move to be considered &amp;quot;efficient&amp;quot;.  See description above.  1.1 is used if this is set to values smaller than 1.1.  Smaller values might lead to better behavior, while larger values might speed up the AI.&lt;br /&gt;
* '''weak_units_first'''=no: (boolean) If set to 'yes', inverts the order in which units are considered by the combat CA.  Attacks are then done in order of increasing hitpoints of the attackers.&lt;br /&gt;
* '''skip_combat_ca'''=no: (boolean) If set to 'yes', the fast combat CA is not used.  The default AI's high_xp_attack and combat CAs remains in place in this case.&lt;br /&gt;
* '''skip_move_ca'''=no: (boolean) If set to 'yes', the fast move-to-targets CA is not used.  The default AI's retreat, villages and move-to-targets CAs remains in place in this case.&lt;br /&gt;
* '''threatened_leader_fights'''=no: (boolean) {{DevFeature1.13|2}} By default, the AI leader will not attack if the threat at the attack position is too high.  If this key is set to 'yes', the leader will attack if the threat there is not larger than the threat at his current position multiplied by the value of 'leader_additional_threat'. This is meant as an option for the endgame: when the AI leader is under attack, he might as well go down fighting.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''aggression''': The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to set how aggressive the AI should attack.  It works in roughly the same way as the default AI aspect:&lt;br /&gt;
** The value of (1 - aggression) determines how much the AI values its own units compared to the enemy's units.  Thus, aggression=0 means that attacks are only done if the AI expects to deal (roughly) the same amount of damage as it will receive.  aggression=1 means that expected damage to the AI's units is not considered, only the expected damage to enemy units matters.&lt;br /&gt;
** Damage is calculated as a combination of hitpoints and chance to die, in &amp;quot;gold equivalent units&amp;quot;.&lt;br /&gt;
** Unlike the default AI, aggression is set to 1 for values larger than 1.  Allowed values are thus anything from -infinity to +infinity, although only -infinity to +1.0 make sense.&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.13|2}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
*'''attacks aspect''':  {{DevFeature1.13|2}} The standard RCA AI [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]] used to filter own and enemy units for attacks is now also taken into account as long as neither [filter] nor [filter_second] (see above under 'optional keys') are defined in the [micro_ai] tag.&lt;br /&gt;
*'''village_value=1''': (number ranging from 0 to 4) The [[AiWML#List_of_AI_Aspects|default RCA AI aspect]] used to define how much villages should be targeted by the move-to-targets CA.  In the Fast MAI, this defines which fraction of the AI units are moved toward village goals (see above for details).  If set to 0, villages are ignored as goals.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded to the same values as those of the respective [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|default candidate actions]].  ca_score is therefore not a valid optional key.&lt;br /&gt;
* The fast Micro AI modifies (removes) the default castle-switch, retreat-injured, spread-poison, high-xp-attack, combat, place-healers, move-to-targets, villages and retreat candidate actions.&lt;br /&gt;
&lt;br /&gt;
Enabling the Fast Micro AI in its default configuration is as simple as this usage example from the &amp;quot;Fast AI&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=fast_ai&lt;br /&gt;
             action=add&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Generalized Goto Micro AI (ai_type=goto) ==&lt;br /&gt;
&lt;br /&gt;
The Goto Micro AI provides a highly configurable method of sending a unit (or units) to a location or set of locations.  The units to be moved are defined using a [[StandardUnitFilter|Standard Unit Filter]] (SUF) [alternatively, all side units are used if the SUF is not provided), while the goto locations are given in a [[StandardLocationFilter|Standard Location Filter]] (SLF).&lt;br /&gt;
&lt;br /&gt;
By default, each unit matching the SUF will move toward the closest goal (in terms of turns needed to get there) matching the SLF via the fastest route given the current situation on the map (locations of enemy units etc.), but this behavior can be influenced by the optional keys listed below.  Note that this means that a unit's goal might change from one turn to the next depending on the situation on the map.&lt;br /&gt;
&lt;br /&gt;
The behavior of the unit(s) once they reach their goal location(s) is configurable as well.  By default, they will stay there until the Goto Micro AI is deleted or changed.&lt;br /&gt;
&lt;br /&gt;
For demonstrations of several Goto Micro AI usages, check out the &amp;quot;Goto&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Goto Micro AI by using&lt;br /&gt;
 ai_type=goto&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Goto specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the possible goal locations for the move.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''':  {{DevFeature1.15|0}} A [[StandardLocationFilter|Standard Location Filter]] for the locations which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).&lt;br /&gt;
* '''avoid_enemies''': (number&amp;gt;0) By default, units controlled by the Goto Micro AI will not take enemy units into account for route finding, other than the enemies' ZoCs.  By contrast, if this key is set, they will try to avoid traversing hexes on which they can be attacked by enemies.  The larger the value of ''avoid_enemies'', the more the units will try to stay out of the enemies' way.  Notes:&lt;br /&gt;
** Units are willing to take a path that is 'avoid_enemies' movement points longer for each enemy unit that can be avoided by doing so for each hex along the path.  As an example, if avoid_enemies=1 and the shortest path crosses 2 hexes that can be reached by 3 enemies each, the unit is willing to take a path that is up to 6 hexes longer instead.  If avoid_enemies=10 in the same situation, the unit will take a path up to 60 hexes longer.&lt;br /&gt;
** Hexes adjacent to enemy units are always strongly avoided, independent of the value of avoid_enemies.&lt;br /&gt;
** avoid_enemies is ignored if 'use_straight_line=true' is set.&lt;br /&gt;
** Due to an issue with the path finding code, '''tunnels and teleports are ignored in Wesnoth 1.14 and before''' if 'avoid_enemies' is used. This has been fixed in Wesnoth 1.15.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units to be moved.  Defaults to all units of the side if not defined.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''ignore_enemy_at_goal'''=no and '''ignore_units'''=no: (boolean)  In the default configuration, the Goto MAI does not move units if the path to all goal hexes is blocked by enemies.  It is therefore possible to disable the Goto MAI by putting enemy units on all the goal hexes.  That can be changed if either of these two keys is set to 'yes'.&lt;br /&gt;
** 'ignore_enemy_at_goal=yes' makes the AI choose its goals while disregarding any enemy units at the goal hexes.  Even so, a stiff rating penalty is applied to goal hexes occupied by enemies, thus that unoccupied goal hexes are chosen unless all possible goals are occupied by enemies.  Note that this setting ''only'' ignores enemy units at the goal hexes themselves.  It is still possible to disable the AI by putting enemies all around the goals.&lt;br /&gt;
** 'ignore_units=yes' let's the AI ignore ''all'' enemy units when deciding which goal hex(es) to move toward.&lt;br /&gt;
** In both cases, enemies are only ignored for deciding which unit to move toward which goal hex.  For the actual move itself, all enemies and their ZoCs are considered as usual.&lt;br /&gt;
** Both options only take effect if neither avoid_enemies nor use_straight_line is set to 'yes', as neither of these consider enemies as blockers.&lt;br /&gt;
* '''release_unit_at_goal'''=no: (boolean) By default, a unit that reaches a goal location will stay there until the Goto Micro AI is deleted or changed (or until the conditions specified in the [goto_goals] SLF change).  If this parameter is set, a unit that has reached its goal will not be controlled by this Micro AI any more starting from the next turn.&lt;br /&gt;
* '''release_all_units_at_goal'''=no: (boolean) Same as ''release_unit_at_goal'', but this will deactivate the Goto Micro AI for all units once the first unit reaches a goal.&lt;br /&gt;
* '''unique_goals'''=no: (boolean)  If set to 'yes', each unit will be sent to a different goal location.  Note that the best goals are recalculated each turn, so a unit might choose a different goal this turn from what it was heading for during the previous turn if the situation on the map has changed.  Also note that if there are more units than goal locations, the left over units will not be handled by the Goto MAI but follow default behavior instead.&lt;br /&gt;
* '''use_straight_line'''=no: (boolean)  By default, units choose the route by which they will get to their goal locations in the smallest number of turns.  If this parameter is set to 'yes', a straight line route (or something pretty close to it at least) will be chosen instead, irrespective of whether this leads into a dead end, whether it is blocked by enemies, etc.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''':  {{DevFeature1.15|0}} The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* Depending on the parameters used, the Goto Micro AI might require a lot of path finding to be done.  Thus, while it is possible to set up this AI to move many units to many different locations, a usage like that might require quite a long calculation time.&lt;br /&gt;
&lt;br /&gt;
===Goto Micro AI example usages:===&lt;br /&gt;
&lt;br /&gt;
Here are several examples of goto [micro_ai] tag usages from the &amp;quot;Goto&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
* Send a unit toward a castle (similar to the RCA AI's Goto Candidate Action, but avoiding dead ends and areas ZoCed by enemies)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
             ca_id=messenger1&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=messenger1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=6-7,2-4&lt;br /&gt;
                 terrain=C*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             ca_score=210001&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send all units of the side toward the southern border of the map&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=8&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                  y=33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             avoid_enemies=1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Send units to the four corners of the map (with each unit going to a different corner).&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=7&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x=1,1,44,44&lt;br /&gt;
                 y=1,33,1,33&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             unique_goals=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
* Set up a guardian unit that doesn't move while no enemy unit is within 8 hexes (ironically, the Goto Micro AI can be used to keep units in place, although using one of the Guardian Micro AIs might often be a better idea)&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=3&lt;br /&gt;
             ai_type=goto&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 id=guard1&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 [filter]&lt;br /&gt;
                     id=guard1&lt;br /&gt;
                 [/filter]&lt;br /&gt;
                 [not]&lt;br /&gt;
                     [filter]&lt;br /&gt;
                         side=1&lt;br /&gt;
                     [/filter]&lt;br /&gt;
                     radius=8&lt;br /&gt;
                 [/not]&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Guardian Micro AIs ==&lt;br /&gt;
=== General Comments on the Guardian Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Guardian Micro AIs can be used to set up different kind of guardian behaviors for a unit or a set of units.  Four different types of guardians are available and are described below.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different guardian behaviors, check out the &amp;quot;Guardians&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
=== Return Guardian  (ai_type=return_guardian)===&lt;br /&gt;
&lt;br /&gt;
A 'return guardian' is a variation of the standard Wesnoth guardian. It has an assigned guard position (GP) to which it returns after attacks on approaching enemies:&lt;br /&gt;
* If at GP with no enemy in reach, do nothing.&lt;br /&gt;
* If at GP with an enemy in reach, attack. Note that the attack is done by the default AI, which might result in the guardian not attacking if the enemy is deemed too strong (this can be influenced by setting the default ''aggression'' value to a different value).&lt;br /&gt;
* If not at GP, return there, no matter whether an enemy is in reach or not.&lt;br /&gt;
* If enemies are blocking the way back to GP, do your best to move around them.&lt;br /&gt;
* If the guardian ends up next to an enemy on the way back, attack that enemy after the move.&lt;br /&gt;
&lt;br /&gt;
Enable the Return Guardian Micro AI by using&lt;br /&gt;
 ai_type=return_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''return_x,return_y''': The coordinate of the hex the unit returns to after each attack.  {{DevFeature1.15|0}} '''return_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=100010: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  This AI consist of 2 CAs, one with score &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt; the other with &amp;lt;tt&amp;gt;ca_score-20&amp;lt;/tt&amp;gt;.  The default value is set so that the first CA gets executed right before the default combat CA, and the other right after.  It usually will not make sense to change this value, but it is possible to do so for custom applications.  {{DevFeature1.13|6}}: This is now changed to 100100 and 99900 in order to avoid conflicts with the new high_XP_attacks candidate action.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a return guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=return_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=return1&lt;br /&gt;
             return_x,return_y=20,2&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
===Stationed Guardian (ai_type=stationed_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A 'stationed guardian' is another variation of the standard Wesnoth guardian with a somewhat more complex behavior than that of the 'return guardian'. Two positions are defined for it, a 'station' and a 'guarded location', as well as a distance. The behavior is as follows:&lt;br /&gt;
* If no enemy is within 'distance' of the guard's current position, do nothing.  This is independent of the guardian being at its station or not.&lt;br /&gt;
* Otherwise: If an enemy is within 'distance' of the guard, but not also within the same distance of the guarded location and the station (all of this simultaneously), move the guard in the direction of the station (or keep the guard in place if the station is unreachable).&lt;br /&gt;
* Otherwise:&lt;br /&gt;
** Pick the enemy unit that is closest to the guarded location.&lt;br /&gt;
** If we can reach it, pick the adjacent hex with the highest defense rating and attack from there.&lt;br /&gt;
** If not in reach, move toward this unit.&lt;br /&gt;
&lt;br /&gt;
Enable the Stationed Guardian Micro AI by using&lt;br /&gt;
 ai_type=stationed_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance parameter as explained above&lt;br /&gt;
* '''station_x,station_y''': The x and y position of the hex that serves as the guardians station. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''guard_x, guard_y''': The x and y position of the guarded location.  {{DevFeature1.13|0}}  These are now optional keys, defaulting to station_x,station_y if not provided. {{DevFeature1.15|0}} '''guard_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a stationed guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=stationed_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=stationed1&lt;br /&gt;
             distance=4&lt;br /&gt;
             station_x,station_y=2,14&lt;br /&gt;
             guard_x,guard_y=3,13&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Coward (ai_type=coward) ===&lt;br /&gt;
&lt;br /&gt;
Cowards are units that, like guardians, sit around doing nothing until an enemy comes into range.  Unlike guardians, however, they run away once enemies approach. Applications might be wild animals, unarmed civilians getting in the way of a battle, etc.  The coward macro can be called with two optional locations, 'seek' and 'avoid':&lt;br /&gt;
* If neither is given, the coward retreats to the position farthest away from the approaching enemies.&lt;br /&gt;
* If 'seek' is given, it preferentially goes toward that location (but getting away from enemies takes priority).&lt;br /&gt;
* If 'avoid' is given, it in addition tries to avoid that location (with both maximizing distance from enemies and going toward 'seek' taking priority).&lt;br /&gt;
* Both 'seek' and 'avoid' may consist of only one coordinate ('x' or 'y'), in which case not a single hex, but a line of hexes is sought or avoided.&lt;br /&gt;
&lt;br /&gt;
Enable the Coward Micro AI by using&lt;br /&gt;
 ai_type=coward&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''distance''': The distance within which an enemy must be to &amp;quot;scare&amp;quot; the unit&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack_if_trapped'''='no':  {{DevFeature1.13|0}}  With the default settings, a coward never attacks.  If this key is set to 'yes', the coward attacks the weakest (lowest hitpoints) enemy next to which it ends up after moving (such as a cornered animal would do, blindly lashing out without regards for its own safety).&lt;br /&gt;
* '''avoid_x, avoid_y''': The x and y coordinate of the hex to avoid. {{DevFeature1.15|0}} '''avoid_loc''' can be used instead with a named/special location.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units from which the cowards flee.  If not set, this defaults to all enemy units.  Note, however, that no &amp;quot;enemy of AI side&amp;quot; condition is added if [filter_second] is specified, meaning that it can include allied and/or enemy units as desired (and even units on the same side as the coward).&lt;br /&gt;
* '''seek_x, seek_y''': The x and y coordinate of the hex to seek. {{DevFeature1.15|0}} '''seek_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a coward [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=coward&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=coward3&lt;br /&gt;
             distance=5&lt;br /&gt;
             seek_x,seek_y=24,5&lt;br /&gt;
             avoid_x,avoid_y=24,15&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Zone guardian (ai_type=zone_guardian) ===&lt;br /&gt;
&lt;br /&gt;
A zone guardian is a unit that, as the name says, guards a zone. It moves randomly inside this zone until an enemy enters it (or a separately defined enemy zone, see below). Applications might be the defense of a castle, a nesting area or for protecting an unit.  The zone macro can be called with an optional SLF, [filter_location_enemy]:&lt;br /&gt;
* If not specified, the zone guard attacks any enemy coming inside its guard zone.&lt;br /&gt;
* If [filter_location_enemy] is given, it attacks any enemy entering this zone and once there are no more enemies, it goes back to patrol in its basic zone.&lt;br /&gt;
&lt;br /&gt;
Enable the Zone Guardian Micro AI by using&lt;br /&gt;
 ai_type=zone_guardian&lt;br /&gt;
in the [micro_ai] tag together with the following keys:&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone to guard.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_enemy]''': A [[StandardLocationFilter|Standard Location Filter]] designating the zone where enemies have to be attacked.  If not given, it defaults to [filter_location].&lt;br /&gt;
* '''station_x,station_y''': By default, the zone guardian moves randomly within the zone if there is no enemy to attack.  However, if station_x/y is given, it moves to this position instead during turns when it does not have an enemy to attack.  This position does not need to be inside the zone defined above.  Note: obviously it does not make sense to define [filter_location], [filter_location_enemy] and station_x/y all at the same time.  Only [filter_location] and either one of the latter two (or none of them) should be provided for any zone guardian. {{DevFeature1.15|0}} '''station_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a zone guardian [micro_ai] tag usage from the &amp;quot;Guardians&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=zone_guardian&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=zone3&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 x,y=22-31,4-11  # This is intentionally chosen to extend past the lake&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             station_x,station_y=32,8&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Hang Out Micro AI (ai_type=hang_out) ==&lt;br /&gt;
&lt;br /&gt;
The Hang Out Micro AI instructs units of the AI side to &amp;quot;hang out&amp;quot; around a certain location, or locations, until a condition for mobilizing the side is met.  If none of the optional keys are given, the AI controls all units of the side to hang around the leader while moving off castle tiles to make room for recruiting.&lt;br /&gt;
&lt;br /&gt;
The AI works by moving each unit to its best hex first using the minimum amount of movement points required for this move.  Once all AI units are in position, movement points are set to zero for all of them simultaneously such that they cannot be used by other AI candidate actions.  This is done because the mobilizing condition might be met as result of one of the moves of the Hang Out AI, after which we want units to be able to continue with other AI actions.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Hang Out and Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Hang Out Micro AI by using&lt;br /&gt;
 ai_type=hang_out&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Hang Out specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  In its default configuration, the Hang Out MAI controls all units of a side, keeping them as close to the side leader(s) as possible while moving off castle tiles to make room for recruiting.  For this purpose, the candidate action score defaults to 170,000, which is just below the score of the default recruiting CA.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[avoid]''': A [[StandardLocationFilter|Standard Location Filter]] for the terrain which the AI units should avoid.  If it is not given, the existence of the default (RCA) AI's [avoid] tag is checked (and used if it exists).  If neither of these two exists, this parameter is set to default to all castle terrain (&amp;lt;tt&amp;gt;terrain=C*,C*^*,*^C*&amp;lt;/tt&amp;gt;) so that units move off the castle to make room for recruiting.  Note that this does not include keep terrain, otherwise the leader would be moved off the keep as well.&lt;br /&gt;
* '''ca_score'''=170000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 170,000, which means that it is executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Hang Out action (&amp;lt;tt&amp;gt;side=&amp;lt;/tt&amp;gt; is always set and does not need to be provided).  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': A [[StandardLocationFilter|Standard Location Filter]] describing the locations ''near'' which the AI units should hang out.  This does not mean that the units only move onto terrain specified here, but that they minimize the distance to such terrain.  It defaults to the position of the side leader(s).&lt;br /&gt;
* '''[mobilize_condition]''':  Contains ([[ConditionalActionsWML#Condition_Tags|WML Condition Tags]])  Once the condition set up here has been met, the Hang Out MAI is disabled and the side goes over to other AI activity.  The Hang Out MAI remains disabled after this, even if the condition is not met any more later in the scenario.&lt;br /&gt;
* '''mobilize_on_gold_less_than''': (number) Since side gold cannot be checked in a WML condition tag without storing it in a variable first, this key is provided separately.  It disables the Hang Out Micro AI once the side gold goes below the value specified here for the first time.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI.  See above (under 'optional keys') for details.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
* The Hang Out AI still takes effect if no locations to hang out at are found or if all map locations are to be avoided.  In that case, it simply keeps all units at their current locations.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Hang Out Micro AI configuration from the &amp;quot;Hang Out and Messenger&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=hang_out&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [avoid]&lt;br /&gt;
                 terrain=C*,H*,M*,A*,S*,*^F*&lt;br /&gt;
             [/avoid]&lt;br /&gt;
             [mobilize_condition]&lt;br /&gt;
                 [have_unit]&lt;br /&gt;
                     side=1&lt;br /&gt;
                     count=7-99&lt;br /&gt;
                 [/have_unit]&lt;br /&gt;
             [/mobilize_condition]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Healer Support Micro AI (ai_type=healer_support) ==&lt;br /&gt;
&lt;br /&gt;
The Healer Support Micro AI configures the healers of a side to stay behind the battle lines and heal injured and/or threatened units rather than participate in combat under all circumstances.  You can set different levels of aggressiveness for the healers.  &lt;br /&gt;
&lt;br /&gt;
'''Notes'''&lt;br /&gt;
* By default, ''all'' healers of the AI side are controlled by this AI.  This includes in particular also the side leader, if the leader is a healer.  If you do not want the leader to be included (and possibly be moved away from the keep etc.) use the [filter] tag with canrecruit=no (see below).&lt;br /&gt;
* It is not always better to use this Micro AI than the default AI, as it takes healers (which can be strong units) away from the pool of units to choose for attacks.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Healers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Healer Support Micro AI by using&lt;br /&gt;
 ai_type=healer_support&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Healer Support specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the healer units.  If set, only units on the AI side passing this filter ''and'' having the healing ability are considered by this Micro AI.&lt;br /&gt;
*'''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting units to receive healing. If set, only units on the AI side passing this filter ''and'' the other healee selection criteria will be selected for healing by this Micro AI.&lt;br /&gt;
* '''aggression'''=1.0: (float) Sets the aggressiveness of the AI.  This parameter is set up as a float to accommodate future functionality, but it currently acts as a boolean: if set to zero, the AI will never let its healers participate in combat, for all other values it allows them to attack after all other units have attacked and if the healers cannot find any more units to support.  The former behavior might be appropriate in scenarios with only one or few valuable healers, while the latter might work better in scenarios with many healers.&lt;br /&gt;
* '''injured_units_only'''=no: (boolean) If set to 'yes', the AI will only move healers next to units that are already injured and skip units that currently have full hitpoints, but might get injured during the next enemy turn.&lt;br /&gt;
* '''max_threats'''=9999: (integer) The maximum allowable number of enemies that can attack a hex in order for it to be considered for a healer.  As an example, setting 'max_threats=0' means that the AI only moves healers to locations that are entirely safe from the enemy (assuming that none of the units currently on the map dies).  Note that the value of this key is checked against the number of enemies that can make it to the hex, not the number of adjacent hexes from which the healer could be attacked.&lt;br /&gt;
&lt;br /&gt;
'''Standard RCA AI aspects respected by this Micro AI:'''&lt;br /&gt;
*'''[avoid]''': The standard RCA AI aspect used to define locations that should be avoided by the AI. Note: as attacks by the healers are left to the default AI, there is no separate ''[avoid]'' tag for this Micro AI in order to prevent inconsistencies.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The Healer Support Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded.  It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
This is an example of a Healer Support Micro AI configuration from the &amp;quot;Healers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=healer_support&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             aggression=0&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Lurkers Micro AI (ai_type=lurkers) ==&lt;br /&gt;
&lt;br /&gt;
The Lurker Micro AI controls some or all units of a side in &amp;quot;lurker mode&amp;quot;.  A lurker is a unit that is capable of moving across most terrains, but that only stops on and attacks from specific terrain.  It might also have the ability to hide on this terrain (which is the reason why it is called a lurker).&lt;br /&gt;
&lt;br /&gt;
Lurkers move individually without strategy and always attack the weakest enemy within their reach.  If no enemy is in reach, the lurker does a random move instead - or it just sits and waits (lurks).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of different types of lurker behavior, check out the &amp;quot;Lurkers&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Lurkers Micro AI by using&lt;br /&gt;
 ai_type=lurkers&lt;br /&gt;
in the [micro_ai] tag&lt;br /&gt;
&lt;br /&gt;
===Lurkers specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
* '''[filter]''': Specifies which units of the side are controlled by the Lurker Micro AI.  This is a [[StandardUnitFilter|Standard Unit Filter]].  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_location]''': Specifies the terrain from which the lurkers attack. This is a [[StandardLocationFilter|Standard Location Filter]].&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''[filter_location_wander]''': Specifies the terrain on which lurkers end their moves if there is no enemy in reach. This is a [[StandardLocationFilter|Standard Location Filter]].  If [filter_location_wander] is not given, it defaults to [filter_location].&lt;br /&gt;
* '''stationary'''=no: (boolean)  If set to yes, a lurker never moves if there is no enemy within attack range.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Lurkers&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=4&lt;br /&gt;
             ai_type=lurkers&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Naga Fighter&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_location]&lt;br /&gt;
                 terrain=W*,S*&lt;br /&gt;
             [/filter_location]&lt;br /&gt;
             [filter_location_wander]&lt;br /&gt;
                 terrain=W*&lt;br /&gt;
             [/filter_location_wander]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Messenger Escort Micro AI (ai_type=messenger_escort) ==&lt;br /&gt;
&lt;br /&gt;
The Messenger Escort Micro AI lets you define a set of waypoints through which it tries to move one or several of its units, the messenger(s).  The other units escort the messenger, trying to protect it and attacking enemies that are in its way.  Note that a messenger might not strictly hit intermediate waypoints, but that getting close is good enough, especially if a waypoint is occupied by an enemy unit.  It does, however, always try to get to the last waypoint.&lt;br /&gt;
&lt;br /&gt;
'''Notes on the use of several messengers:''' {{DevFeature1.13|0}}  &lt;br /&gt;
* This Micro AI can control several messengers at the same time, which can but do not have to move in a group.  They can be put on the map altogether in one location, or be spread out on the map or across different turns.&lt;br /&gt;
* The farther a messenger has moved through the chain of waypoints, the higher a priority it is for the AI, meaning that farther advanced (on the map) messengers are moved first, and that the escort units try to clear the way and stay with those messengers more so than with those further back on the map.  This order can be inverted using the invert_order= key.&lt;br /&gt;
* If several messengers are on the map, escort units might move from one to the other, the AI making a judgement call where they are needed most.  Closeness to a messenger is generally the most important criterion, but not the only one.&lt;br /&gt;
* If a messenger dies, the escort units previously staying with it now join one of the other messengers if there are any left (otherwise they follow default AI behavior).&lt;br /&gt;
* If you want certain escort units to stay always with the same messenger, use several messenger [micro_ai] tags with different [filter]/[filter_second] combinations.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Messenger&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Messenger Escort Micro AI by using&lt;br /&gt;
 ai_type=messenger_escort&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Messenger Escort specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': {{DevFeature1.13|0}} (Previous to 1.13.0, only id= was allowed.) The id of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated list of waypoint coordinates through which the messenger will go in the given order to reach its goal, given by the last coordinate.  If you want the messenger to go directly to the goal, simply enter a single x,y coordinate here.  Note that the messenger does not have to hit each waypoint exactly (except for the final one), getting close to them is good enough. {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate actions controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.  Note that the Messenger Escort AI consists of 3 CAs with scores ranging from &amp;lt;tt&amp;gt;ca_score-2&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;ca_score&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* '''enemy_death_chance'''=0.67 and '''messenger_death_chance'''=0.0: (both floats; ranging from 0 to 1)    If the messenger is adjacent to an enemy at the end of the move, it will only attack the enemy if the chance of death of the enemy is &amp;gt;= enemy_death_chance and if the messenger's chance of death is &amp;lt;= messenger_death_chance (or if it is an attack at a range for which the enemy does not have a weapon).&lt;br /&gt;
* '''[filter_second]''': {{DevFeature1.13|0}} A [[StandardUnitFilter|Standard Unit Filter]] selecting the escort units.  If not set, this defaults to all enemy units. The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''invert_order'''=no: {{DevFeature1.13|0}} If set to 'yes', the order of priority with which messenger units are considered is inverted, starting with the messenger unit that is the least advanced on the map (see description above).  In most cases, selecting this option results in worse performance of the AI, so use it with caution.  (And obviously it has no effect at all if there is only one messenger controlled by the AI.)&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Messenger Escort&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=messenger_escort&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=messenger&lt;br /&gt;
             waypoint_x=31,24,27,28&lt;br /&gt;
             waypoint_y=20,14,7,1&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Patrol Micro AI (ai_type=patrol) ==&lt;br /&gt;
&lt;br /&gt;
The defines a number of waypoints for a unit (or units) to follow.  Options can be set whether the route should be followed in a loop, as an out-and back, only once, whether units encountered along the way should be attacked, etc.&lt;br /&gt;
&lt;br /&gt;
No matter what options are chosen, getting to the next waypoint always takes priority over attacking for a patroller.  The AI thus prefers to move the patrol unit around enemies rather than straight for them.  Also, if a waypoint is occupied by a unit the AI is not instructed to attack, it will (eventually) abandon that waypoint once it gets close enough and move on to the next one.&lt;br /&gt;
&lt;br /&gt;
For a demonstration of several different patrol behaviors, check out the &amp;quot;Patrols&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Patrol Micro AI it by using&lt;br /&gt;
 ai_type=patrol&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Patrol specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''id''' or '''[filter]''': The ID of a unit or a [[StandardUnitFilter|Standard Unit Filter]] selecting the units controlled by this Micro AI.&lt;br /&gt;
** One or the other is required&lt;br /&gt;
** If both are given, [filter] takes priority over id=&lt;br /&gt;
** The AI contains an implicit filter for units on the AI side. Thus, if all AI units are to be selected, an empty [filter] tag can be used.&lt;br /&gt;
** If several units match the filter, the order in which they are controlled is not guaranteed. If the order matters, set up several MAIs using the id= key and different values for ca_score=.&lt;br /&gt;
* '''waypoint_x,waypoint_y''': Comma-separated lists of the waypoint coordinates through which the patrol travels (in order). {{DevFeature1.15|0}} '''waypoint_loc''' can be used instead with named/special locations.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''attack''': By default (if this key is not set), the patrol unit attacks any unit it ends up next to after a move.  Alternatively, this parameter can be set to a comma-separated list of unit ids. If that is done, the patrol only attacks these units when it ends up next to them and ignores any other unit.  This can in turn be used to have the patroller not attack at all, by setting ''attack='' to a non-existing id.&lt;br /&gt;
* '''ca_score'''=300000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 300,000, which is higher than any of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|RCA AI candidate actions]].  By setting it, for example, to 170,000, we can have it be executed after the default recruitment CA, but before most of the other default CAs.&lt;br /&gt;
* '''one_time_only'''=no: (boolean)  If set to 'yes', the unit goes through the patrol route only once and then hangs out at the last waypoint.  It also always attacks any enemy unit on the last waypoint, independent of what ''attack='' is set to.&lt;br /&gt;
* '''out_and_back'''=no: (boolean)  By default, a patrol unit returns to the first waypoint after reaching the last, doing the patrol route as a loop.  If this parameter is set to 'yes', the unit instead reverses its course and perpetually does out and backs of the route.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;Patrols&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=patrol&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             id=Konrad&lt;br /&gt;
             waypoint_x=9,24,25&lt;br /&gt;
             waypoint_y=21,23,15&lt;br /&gt;
             one_time_only=yes&lt;br /&gt;
             attack=Gertburt&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Protect Unit Micro AI (ai_type=protect_unit) ==&lt;br /&gt;
&lt;br /&gt;
The Protect Unit Micro AI tries to move one or several units to a given location while keeping them safe from enemy attacks. It does so by keeping the protected units away from enemy units and close to friendly units. Other units on the AI side are controlled by the default Wesnoth AI.&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Protect Unit&amp;quot; and &amp;quot;The Elves Besieged&amp;quot; scenarios from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Protect Unit Micro AI by using&lt;br /&gt;
 ai_type=protect_unit&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Protect Unit specific keys for the [micro_ai] tag:===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''[unit]''': A separate [unit] tag is required for each protected unit(s).  Each tag has required keys id=, goal_x=, goal_y= for each unit, as shown in the example below. {{DevFeature1.15|0}} '''goal_loc''' can be used instead with a named/special location.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''disable_move_leader_to_keep'''=no: (boolean) If set to 'yes', side leaders protected by this AI do not try to return to their keep.  This is necessary for this Micro AI to work with units that are side leaders and that are supposed to move to an off-keep location.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
*The Protect Unit Micro AI modifies the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|attacks aspect]].  Depending on the parameters, it might also modify the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|move leader to keep candidate action]].&lt;br /&gt;
* The CA evaluation scores of this AI are hard-coded. It does not work otherwise.&lt;br /&gt;
&lt;br /&gt;
Here's an example of [micro_ai] tag usage from the &amp;quot;HttT: The Elves Besieged&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=protect_unit&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Delfador&lt;br /&gt;
                 goal_x,goal_y=1,2&lt;br /&gt;
             [/unit]&lt;br /&gt;
             [unit]&lt;br /&gt;
                 id=Konrad&lt;br /&gt;
                 goal_x,goal_y=1,1&lt;br /&gt;
             [/unit]&lt;br /&gt;
 &lt;br /&gt;
             disable_move_leader_to_keep=true&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Recruiting Micro AI==&lt;br /&gt;
=== General Comments on the Recruiting Micro AIs ===&lt;br /&gt;
&lt;br /&gt;
The Recruiting Micro AI replaces the default recruitment AI by alternative recruiting patterns.  Currently there are two recruiting algorithms you can choose from, random recruiting and the rush recruiting used in the new (as of Version 1.11.1) Experimental AI (see below).&lt;br /&gt;
&lt;br /&gt;
For a demonstration of a different recruiting pattern, check out the &amp;quot;Recruiting&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  You can also explore the unit choices by watching the Experimental AI in a multiplayer game.&lt;br /&gt;
&lt;br /&gt;
'''Note:''' Obviously, the Recruitment Micro AI modifies the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|recruitment candidate action]].&lt;br /&gt;
&lt;br /&gt;
=== Random Recruitment AI (ai_type=recruit_random) ===&lt;br /&gt;
&lt;br /&gt;
This AI randomly selects a unit type from the recruitment list of the side.  The probability of choosing each type and the behavior in low-gold situations can be influenced with optional parameters.&lt;br /&gt;
&lt;br /&gt;
Enable the Random Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_random&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* '''[probability]''': This tag sets the probability of selecting the given unit type.  It has two required sub-keys:&lt;br /&gt;
** '''type''': comma-separated list of unit types to which this probability is to be applied&lt;br /&gt;
** '''probability''': (non-negative float)  The probability with which these unit types is to be selected.  All unit types for which no [probability] tag is defined receive a default probability of 1.0, thus values should be chosen relative to that.  As an example, in the code below swordsmen and peasants are given a probability of 8, while no other units have assigned probabilities.  Thus, each of these two unit types is 8 times more likely to be selected than any other individual unit type.&lt;br /&gt;
* '''skip_low_gold_recruiting'''=no: (boolean)  By default, the random recruitment AI chooses a unit to recruit only from those types for which it has sufficient gold.  If this parameter is set to 'yes', it will instead choose from all available unit types and end recruiting for the turn if there is not enough gold to recruit it this turn (even if other, affordable unit types exist).&lt;br /&gt;
&lt;br /&gt;
Here's an example of a random recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_random&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             [probability]&lt;br /&gt;
                 type=Swordsman,Peasant&lt;br /&gt;
                 probability=8&lt;br /&gt;
             [/probability]&lt;br /&gt;
 &lt;br /&gt;
             skip_low_gold_recruiting=yes&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
=== Rush Recruitment AI (ai_type=recruit_rushers) ===&lt;br /&gt;
&lt;br /&gt;
This AI replaces the default recruiting with the rush recruiting used in the new (as of Version 1.11.1) Experimental AI.&lt;br /&gt;
&lt;br /&gt;
Enable the Rush Recruiting Micro AI by using&lt;br /&gt;
 ai_type=recruit_rushers&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=180000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 180,000.  This is identical to the default Recruiting CA which it replaces.&lt;br /&gt;
* {{DevFeature1.15|2}} '''high_level_fraction'''=0: (non-negative number) The approximate fraction of units of level 2 or higher to be recruited. This is defined as fraction of units on the map, not of new units being recruited during a turn or over several turns (which makes a difference if there are already units on the map). The effect is also cumulative per level (starting from level 2), meaning that 1/3 of L2 units will be recruited in the example above, 1/3*1/3=1/9 L3s etc. The default value is zero, which leaves the high-level unit recruiting to the default Experimental AI algorithm resulting usually in very few such units being recruited.&lt;br /&gt;
* '''randomness'''=0.1: (number)  A random number is applied to the rusher recruit engine's score to prevent the recruitment pattern from being too predictable. 0 causes no randomness to be applied, while larger numbers increase the random effect. A value of 1-2 generates results in which the random effect is approximately equal to the scored effect. Extremely high values are essentially entirely random.&lt;br /&gt;
&lt;br /&gt;
Here's an example of a rush recruiting [micro_ai] tag usage from the &amp;quot;Recruiting&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]]: &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=1&lt;br /&gt;
             ai_type=recruit_rushers&lt;br /&gt;
             action=add&lt;br /&gt;
          [/micro_ai]&lt;br /&gt;
&lt;br /&gt;
== Simple Attack Micro AI (ai_type=simple_attack) ==&lt;br /&gt;
&lt;br /&gt;
The Simple Attack Micro AI is meant as an addition to the default combat candidate action for when one wants to force the AI to do attacks that it would otherwise not do (for example on units that are 1 XP from leveling) or to execute certain attacks before all others (for example, attack with all &amp;quot;expendable&amp;quot; units first).  It is not set up to be a replacement for the default attacks.  Some notes:&lt;br /&gt;
&lt;br /&gt;
* The algorithm is different from and simpler than that of the default combat CA.  For example, no attack combinations are considered, only individual attacks.  As a result, the [[AiWML#List_of_AI_Aspects|default aspects]] (aggression, caution, [avoid], ...) are not taken into account.  Also, if lots of units are involved, evaluation of this Micro AI is potentially quite a bit slower than of the default attacks.&lt;br /&gt;
&lt;br /&gt;
* If an attack matching the Standard Unit Filters described below is found, this attack will always be executed, no matter how bad the odds are.  Thus, this Micro AI can be used to force the AI to do attacks it would otherwise avoid.&lt;br /&gt;
&lt;br /&gt;
* Other than selecting the attacker/defender units, this AI is currently not configurable.  Let us know if you would like to see certain options to be added.  (No guarantees that we will add them, but we will definitely consider them.)&lt;br /&gt;
&lt;br /&gt;
For a demonstration, check out the &amp;quot;Simple Attack&amp;quot; scenario from the [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
Enable the Simple Attack Micro AI by using&lt;br /&gt;
 ai_type=simple_attack&lt;br /&gt;
in the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
===Simple Attack specific keys for the [micro_ai] tag===&lt;br /&gt;
&lt;br /&gt;
'''Required keys:'''&lt;br /&gt;
&lt;br /&gt;
There are no required keys.  It is, however, in general not desirable to use this AI without at least one of the optional Standard Unit Filters below, as this AI is not meant as a replacement of the default combat candidate action.&lt;br /&gt;
&lt;br /&gt;
'''Optional keys:'''&lt;br /&gt;
&lt;br /&gt;
* '''ca_score'''=110000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI.  By default, it is set to 110,000, which means that it is executed right before the default combat candidate action.&lt;br /&gt;
* '''[filter]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units of the AI side participating in the Simple Attack action.  It defaults to all units of the side.  The filter automatically includes the side= key set to the AI side.&lt;br /&gt;
* '''[filter_second]''': A [[StandardUnitFilter|Standard Unit Filter]] selecting the units the AI should attack.  It defaults to all enemy units.  The filter automatically includes a [[StandardSideFilter|Standard Side Filter]] set to all enemies of the AI side.&lt;br /&gt;
* '''weapon'''=-1: (integer) The number of the weapon that should be used for this attack.  Weapon numbers start at 1 (not 0).  -1 stands for &amp;quot;best weapon&amp;quot; (by whatever criterion the AI chooses to use).   '''Available from Wesnoth 1.11.8'''&lt;br /&gt;
&lt;br /&gt;
Below are 2 examples of Simple Attack Micro AI configurations from the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].  The first one shows how to force the AI to attack with all Soulless and Walking Corpse units first:&lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110000&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless,Walking Corpse&lt;br /&gt;
             [/filter]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
The second executes attacks by Soulless on all enemy units that are 1 XP from leveling (something the RCA AI would generally not do).  Note that the Lua code bit is simply there in order to code the &amp;quot;1 XP from advancing&amp;quot; condition.  It is not needed for most other SUFs, as can be seen in the previous example.&lt;br /&gt;
         [lua]&lt;br /&gt;
             code=&amp;lt;&amp;lt;&lt;br /&gt;
                 function close_to_advancing(unit)&lt;br /&gt;
                     if (unit.experience &amp;gt;= unit.max_experience-1) then&lt;br /&gt;
                         return true&lt;br /&gt;
                     else&lt;br /&gt;
                         return false&lt;br /&gt;
                     end&lt;br /&gt;
                 end&lt;br /&gt;
             &amp;gt;&amp;gt;&lt;br /&gt;
         [/lua]&lt;br /&gt;
 &lt;br /&gt;
         [micro_ai]&lt;br /&gt;
             side=2&lt;br /&gt;
             ai_type=simple_attack&lt;br /&gt;
             action=add&lt;br /&gt;
 &lt;br /&gt;
             ca_score=110001&lt;br /&gt;
             [filter]&lt;br /&gt;
                 type=Soulless  # No Walking Corpses; L0 units don't advance enemy&lt;br /&gt;
             [/filter]&lt;br /&gt;
             [filter_second]&lt;br /&gt;
                 lua_function = &amp;quot;close_to_advancing&amp;quot;&lt;br /&gt;
             [/filter_second]&lt;br /&gt;
         [/micro_ai]&lt;br /&gt;
Several Simple Attack Micro AIs can be combined for the same AI side.  The two examples above are both used (note the different values for ca_score) in the &amp;quot;Simple Attack&amp;quot; [[Micro_AIs#Micro_AI_Test_and_Demo_Scenarios|test scenario]].&lt;br /&gt;
&lt;br /&gt;
== Custom Micro AIs ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|5}}&lt;br /&gt;
&lt;br /&gt;
From 1.13.5 onwards, adding a new Micro AI can be easily done without altering any core files or overriding the micro_ai tag. The [micro_ai] tag now looks up its ''ai_type'' parameter in the &amp;lt;code&amp;gt;wesnoth.micro_ais&amp;lt;/code&amp;gt; table and uses the micro AI setup function found there to determine which keys are required and allowed and what candidate actions to add.&lt;br /&gt;
&lt;br /&gt;
A micro AI setup function takes as its single parameter the contents of the [micro_ai] tag and returns three separate values:&lt;br /&gt;
*'''required''': A list of required keys and tags, as strings. Tags are indicated by enclosing them within square brackets in the string. If a key or tag in this list is missing from the [micro_ai] tag, an error will be printed.&lt;br /&gt;
*'''optional''': Similar to above, a list of optional keys and tags, as strings. No error will be printed if a key or tag in this list is missing, nor will an error be printed if it is present (as would be the case for unrecognized keys or tags).&lt;br /&gt;
*'''ca_params''': A table specifying the candidate actions that constitute the micro AI. It should contain an ''ai_id'' key (which specifies a base ID to be used for candidate actions of this micro AI) and a series of candidate action definitions. Each candidate action definition is a table containing the following keys:&lt;br /&gt;
**'''ca_id''': The ID used for the candidate action. This will be combined with the ''ai_id'' to produce the final ID.&lt;br /&gt;
**'''location''': Path to the Lua file that defines the candidate action.&lt;br /&gt;
**'''score''': The maximum score for the candidate action. If the [micro_ai] tag specified a ''ca_score'' parameter, the score here should normally be based on that.&lt;br /&gt;
&lt;br /&gt;
(The return statement would thus look like &amp;lt;syntaxhighlight lang='lua' inline&amp;gt;return required, optional, ca_params&amp;lt;/syntaxhighlight&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
Since the function has full access to the contents of the [micro_ai] tag, it can conditionally add candidate actions to '''ca_params''' depending on the values of other keys within the [micro_ai] tag. It can also perform other actions such as adding facets to an aspect; if it does such things, it should pay attention to the ''action'' key to determine whether it's being added or removed.&lt;br /&gt;
&lt;br /&gt;
There are two useful helper functions that can be used by Micro AI setup functions requiring aspect changes. They can be brought into scope by a line similar to the following:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='lua&amp;gt;local MAIH = wesnoth.require(&amp;quot;ai/micro_ais/micro_ai_helper.lua&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* MAIH.add_aspects(side, aspect_params)&lt;br /&gt;
* MAIH.delete_aspects(side, aspect_params)&lt;br /&gt;
&lt;br /&gt;
The ''aspect_params'' parameter is an array of tables, each of which contains the following keys:&lt;br /&gt;
*'''aspect''': The aspect to change.&lt;br /&gt;
*'''facet''': The WML defining the facet. This must include an ''id'' key in order for the aspect to be correctly deleted. For maximum compatibility, incorporate the ''ca_id'' key from the [micro_ai] tag.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AiWML&amp;diff=65216</id>
		<title>AiWML</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AiWML&amp;diff=65216"/>
		<updated>2019-12-12T16:38:34Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* List of AI Aspects */ add mentions of the new, former ExpAI CAs&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{WML Tags}}&lt;br /&gt;
&lt;br /&gt;
== Customizing the Default AI Using WML ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
This page contains:&lt;br /&gt;
* Descriptions of the aspects and goals of the default AI&lt;br /&gt;
* Instructions for setting them up at the beginning of the scenario in a [side] tag&lt;br /&gt;
&lt;br /&gt;
This page does not contain:&lt;br /&gt;
* Instructions for [[LuaAI#Dynamic_Lua_Aspects|setting up aspects dynamically using Lua]].&lt;br /&gt;
* Instructions for [[Modifying_AI_Components#Modifying_Standard_Aspects|changing aspects and goals mid scenario]], although for many of them (for example, for all [[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Aspects|standard aspects]]) the methods are almost identical&lt;br /&gt;
* Instructions for some of the [[Modifying_AI_Components|more complex AI customization tasks]].&lt;br /&gt;
&lt;br /&gt;
== Defining Aspects and Goals of the RCA AI ==&lt;br /&gt;
&lt;br /&gt;
[[RCA_AI#Use_of_Aspects_in_the_RCA_AI|Aspects]] and [[RCA_AI#Use_of_Goals_and_Targets_in_the_RCA_AI|goals]] of the [[RCA_AI|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 [[#List_of_AI_Aspects|aspects themselves]] as arguments, as well as the following general keys:&lt;br /&gt;
* '''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 [https://github.com/wesnoth/wesnoth/blob/master/data/core/macros/schedules.cfg 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.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
* '''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. {{DevFeature1.13|5}} In 1.13, the meaning of '''ai_algorithm''' has changed and no longer selects a native AI that's built into the engine, as the RCA AI has now become the core system and no alternatives exist anymore. Instead, it selects a predefined ''[ai]'' definition, such as those defined in ''data/ai/ais''. Since these are defined in WML, you can easily build your own.&lt;br /&gt;
&lt;br /&gt;
An example of a simple use of the [ai] tag to set AI parameters (aspects) is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=wml&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    time_of_day=dusk&lt;br /&gt;
    aggression=0.99&lt;br /&gt;
    caution=0.01&lt;br /&gt;
    [avoid]&lt;br /&gt;
        [filter]&lt;br /&gt;
            side=2,3&lt;br /&gt;
        [/filter]&lt;br /&gt;
        radius=2&lt;br /&gt;
    [/avoid]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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 &amp;lt;code&amp;gt;:inspect&amp;lt;/code&amp;gt; in debug mode to verify whether an aspect was changed successfully.&lt;br /&gt;
&lt;br /&gt;
==List of AI Aspects==&lt;br /&gt;
&lt;br /&gt;
* '''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 &amp;lt;u&amp;gt;only&amp;lt;/u&amp;gt; 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:'''&lt;br /&gt;
** This is a simplified version of the more complex (and arguably more useful) application of a dynamic Lua ''advancements'' aspect, as described [[LuaAI#The_advancements_Aspect|here]].&lt;br /&gt;
** The ''advancements'' aspect only takes effect during the AI turn, that is, on offense only, not on defense.&lt;br /&gt;
** 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).&lt;br /&gt;
&lt;br /&gt;
* '''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.  The description below applies as written to the ''combat'' CA only, but aggression is taken into account by the ''high_xp_attack'' and ''spread_poison'' CAs as well (as of '''Wesnoth 1.13.5''' and '''1.15.3''' respectively).&lt;br /&gt;
** '''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.&lt;br /&gt;
** 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 &amp;gt;0 is found.  The combat CA score is zero if the highest attack score is &amp;lt;=0).&lt;br /&gt;
** 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.&lt;br /&gt;
** All the negative contributions to the score are multiplied by '(1-aggression)'.  This means that:&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
** 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.&lt;br /&gt;
&lt;br /&gt;
*'''[attacks]''': Filters the units considered for combat, both on the AI and the enemy sides.  Applies to the ''combat'', ''high_xp_attacks'' and ''spread_poison'' CAs only.  It cannot be set in the same way as the other aspects and is therefore described in a [[#Filtering_Combat_with_the_attacks_Aspect|separate section]] below.&lt;br /&gt;
&lt;br /&gt;
* '''[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''.&lt;br /&gt;
** '''[[StandardLocationFilter]]''': The locations for the AI to avoid.  Do not use a [filter_location] tag.&lt;br /&gt;
&lt;br /&gt;
* '''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 [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|evaluation scores]]):&lt;br /&gt;
** {{DevFeature1.15|3}} ''Retreat-injured'' CA:&lt;br /&gt;
*** Units only retreat if caution is greater than 0. Thus, this CA can be disabled simply by setting caution to zero or negative values.&lt;br /&gt;
*** For a unit to be considered for retreating, its hitpoints need to be below a certain threshold that depends on the terrain the unit is on and its level. In addition, there is also a multiplier that depends (non-linearly) on caution.  The higher the caution the higher the hitpoint threshold.  Or in other words, units start retreating with more hitpoints left when caution is higher.&lt;br /&gt;
** ''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].&lt;br /&gt;
** ''Combat'' CA: &lt;br /&gt;
*** 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 &amp;quot;exposure&amp;quot; rating of the units.  This exposure is then subtracted from the 'attack score' as described for ''aggression'' above.&lt;br /&gt;
*** 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.)&lt;br /&gt;
*** If the AI leader is used in an attack, the AI ''always'' uses 'caution=2' for the evaluation of that attack.&lt;br /&gt;
** ''Retreat'' CA: {{DevFeature1.15|3}} This CA has been removed.&lt;br /&gt;
*** 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 &amp;lt;tt&amp;gt;caution &amp;amp;times; their_power &amp;gt; our_power&amp;lt;/tt&amp;gt;. 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.&lt;br /&gt;
*** 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. &lt;br /&gt;
*** The AI never retreats if caution is set to 0 or lower.&lt;br /&gt;
** ''Move-to-targets'' CA:&lt;br /&gt;
*** 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. &amp;quot;Dangerous&amp;quot; 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 &amp;lt;tt&amp;gt;our_strength / their_strength &amp;gt; 0.5 + caution&amp;lt;/tt&amp;gt;. 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.&lt;br /&gt;
***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.&lt;br /&gt;
*** The AI also considers retreating units during the move-to-target phase based on criteria similar to those for the retreat CA.&lt;br /&gt;
&lt;br /&gt;
* '''[goal]'''=&amp;quot;&amp;quot;: Defines units or locations as [[#AI_Targets_and_Goals|move targets]] (not attack targets!) for the AI.  '''Applies to ''move-to-targets'' CA only'''. &amp;lt;-- 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 [[#The_.5Bgoal.5D_Tag|[goal] tag]] below for details.&lt;br /&gt;
&lt;br /&gt;
* '''grouping'''=&amp;quot;offensive&amp;quot;: (string) How the AI should try to group units.  Applies to ''move-to-targets'' CA only.  Possible values:&lt;br /&gt;
** ''offensive'': Makes the AI try to group units together before attacking.&lt;br /&gt;
** ''defensive'': Makes the AI group units together very conservatively, only advancing them much beyond its castle if it has overwhelming force.&lt;br /&gt;
** ''no'': Makes the AI use no grouping behavior.&lt;br /&gt;
&lt;br /&gt;
* '''leader_aggression'''=&amp;quot;-4.0&amp;quot;: 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''.&lt;br /&gt;
&lt;br /&gt;
* '''[leader_goal]'''=&amp;quot;&amp;quot;: Makes the AI try to move its leader to a specific location.  Applies to ''move-leader-to-goals'' CA only.&lt;br /&gt;
** '''x''', '''y''': The location toward which the AI should move its leader.&lt;br /&gt;
** '''auto_remove'''=no: (bool)  If 'no' (default), the AI moves the leader to the goal, after which he stays there until [leader_goal] is [[Modifying_AI_Components#Modifying_Standard_Aspects|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).&lt;br /&gt;
** '''id'''=&amp;quot;&amp;quot;: (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] [[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Aspects|facet]].  Thus, in principle for this to work, you need to set the id of the facet as described [[Modifying_AI_Components#Modifying_Standard_Aspects|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. {{DevFeature1.13|5}} 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.&lt;br /&gt;
**'''max_risk'''=1-caution: (double: meaningful values are &amp;gt;=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.&lt;br /&gt;
&lt;br /&gt;
* '''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:&lt;br /&gt;
** Leaders participate in all the moves and attacks in the same way as any other unit of the AI side.&lt;br /&gt;
** 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.&lt;br /&gt;
&lt;br /&gt;
* '''leader_value'''=3: (double) A number 0 or higher which determines the value of enemy leaders as [[#AI_Targets_and_Goals|targets]].  Affects ''move-to-targets CA'' only (and therefore specifically does not apply to attacks).&lt;br /&gt;
&lt;br /&gt;
* '''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''.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
*'''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 [[AI_Recruitment|separate page]].&lt;br /&gt;
&lt;br /&gt;
* '''scout_village_targeting'''=3: (double) The AI multiplies the value of village [[#AI_Targets_and_Goals|targets]] for scouts by this value.  Affects ''move-to-targets'' CA only.&lt;br /&gt;
&lt;br /&gt;
* '''simple_targeting'''=no: (bool) If 'yes', the AI moves its units toward [[#AI_Targets_and_Goals|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.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
* '''village_value'''=1: (double) A number 0 or higher which determines how much the AI tries to go for villages as [[#AI_Targets_and_Goals|targets]].  Affects ''move-to-targets'' CA only.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
=== Removed AI Aspects ===&lt;br /&gt;
&lt;br /&gt;
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. {{DevFeature1.13|5}} Except for attack_depth, these have now all been removed.&lt;br /&gt;
&lt;br /&gt;
* '''attack_depth'''=5: (int)&lt;br /&gt;
* '''number_of_possible_recruits_to_force_recruit'''=3.1: (double)&lt;br /&gt;
* '''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.&lt;br /&gt;
** It is, in principle, still possible to use this aspect together with the [[Wesnoth_AI_Framework#Recruitment_Stage|recruitment stage]], but this is discouraged for the reasons given at this link.&lt;br /&gt;
* '''recruitment_ignore_bad_combat'''=no: (bool)&lt;br /&gt;
* '''recruitment_ignore_bad_movement'''=no: (bool)&lt;br /&gt;
&lt;br /&gt;
==AI Targets and Goals==&lt;br /&gt;
&lt;br /&gt;
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 [[RCA_AI#RCA_AI_Aspect_and_Goal_Configuration|base values]].  Additional targets can be defined using the [goal] tag.&lt;br /&gt;
&lt;br /&gt;
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 [[RCA_AI#Use_of_Goals_and_Targets_in_the_RCA_AI|here]].  The sections below only describe how to set up goals in order to add targets for the ''move-to-targets'' CA.&lt;br /&gt;
&lt;br /&gt;
===The [goal] Tag===&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
* '''name'''=&amp;quot;target&amp;quot;: (string)  The following values are possible and result in different types of targets, as shown in the examples below:&lt;br /&gt;
** ''target'':  The (default) target goal specifies target units (not necessarily enemy units) toward which the AI should move its units. {{DevFeature1.13|5}} target_unit is now a synonym for this.&lt;br /&gt;
** ''target_location'': Specifies target locations toward which the AI should move its units.&lt;br /&gt;
** ''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.&lt;br /&gt;
** ''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.&lt;br /&gt;
** ''protect_my_unit'': ('''deprecated''') &amp;lt;s&amp;gt;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.&amp;lt;/s&amp;gt;&lt;br /&gt;
** ''lua_goal'': A dynamically calculated goal written in Lua. See [[LuaAI#Lua_Goals_and_Targets|here]] for details on how this works.&lt;br /&gt;
&lt;br /&gt;
* '''[criteria]'''=&amp;quot;&amp;quot;: Contains a [[StandardUnitFilter]] (for ''target'', ''protect_unit'' or ''protect_my_unit'') or [[StandardLocationFilter]] (for ''target_location'' or ''protect_location'') describing the targets.&lt;br /&gt;
&lt;br /&gt;
* '''value'''=0: (double) The value of the goal.&lt;br /&gt;
&lt;br /&gt;
* '''protect_radius'''=20: (int) The protection radius.  Applies to ''protect_location'', ''protect_unit'' and ''protect_my_unit''.&lt;br /&gt;
&lt;br /&gt;
===Examples of [goal] Tag Usage===&lt;br /&gt;
&lt;br /&gt;
'''target:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        [criteria] #NOTE: this is a SUF, because we're targeting a unit&lt;br /&gt;
            side=3&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''target_location:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=target_location&lt;br /&gt;
        [criteria] #NOTE: this is a SLF, because we're targeting a location&lt;br /&gt;
            x,y=42,20&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''protect_location:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=protect_location&lt;br /&gt;
        [criteria] #NOTE: this is a SLF, because we're protecting a location&lt;br /&gt;
            x,y=42,20&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        protect_radius=16&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''protect_unit:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=protect_unit&lt;br /&gt;
        [criteria] #NOTE: this is a SUF, because we're protecting a unit&lt;br /&gt;
            side=3&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        protect_radius=16&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''How to use protect_unit to protect the own leader''' (We are side 2)&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=protect_unit&lt;br /&gt;
        [criteria]&lt;br /&gt;
            side=2&lt;br /&gt;
            canrecruit=yes&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        protect_radius=8&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Deprecated AI Targeting Aspects ===&lt;br /&gt;
&lt;br /&gt;
The following AI targeting parameters currently still work, but have been superseded by the [[#The_.5Bgoal.5D_Tag|[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.&lt;br /&gt;
&lt;br /&gt;
* '''[target]'''=&amp;quot;&amp;quot;: '''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.&lt;br /&gt;
** '''[[StandardUnitFilter]]''': Do not use a [filter] tag.&lt;br /&gt;
** '''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.&lt;br /&gt;
&lt;br /&gt;
* '''[protect_location]'''=&amp;quot;&amp;quot;: '''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.&lt;br /&gt;
** '''x''', '''y''': Standard coordinates. These indicate the location the AI is protecting.&lt;br /&gt;
** '''radius''': The radius around it to protect (0 indicates a single hex). {{DevFeature1.13|5}} This key has been changed to protect_radius, though it's preferred to change old code to use the [goal] tag instead.&lt;br /&gt;
** '''value''': The importance of protecting this location.&lt;br /&gt;
&lt;br /&gt;
* '''[protect_unit]'''=&amp;quot;&amp;quot;: '''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.&lt;br /&gt;
** '''[[StandardUnitFilter]]''': The unit(s) to protect. Do not use a [filter] tag.&lt;br /&gt;
** '''radius''': The radius around it to protect (0 indicates a single hex). {{DevFeature1.13|5}} This key has been changed to protect_radius, though it's preferred to change old code to use the [goal] tag instead.&lt;br /&gt;
** '''value''': The importance of protecting this unit.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
== Filtering which Units Participate in which Actions==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}}&lt;br /&gt;
&lt;br /&gt;
Each [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|candidate action of the default AI]] can be restricted to apply to only a subset of the AI units. This is done by adding a '''[filter_own]''' tag to the '''[[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Candidate_Actions|[candidate_action]]]''' tag definition.&lt;br /&gt;
&lt;br /&gt;
Typical use cases:&lt;br /&gt;
* Restricting an action to certain units so that other units are not used to this. For example, village grabbing could be restricted to scout units only, so that slower units do not take detours on their way toward the enemies. See the example below.&lt;br /&gt;
* Excluding units from a certain action in order to have them available for other actions afterward. For example, one might want to add a custom candidate action which moves healers behind injured units (or use the existing [[Micro_AIs|Micro AI]] for this). Ideally, this should happen after ''all'' other units have moved, specifically also after the move-to-targets candidate action (MtT CA) is done. In the default setting, however, the MtT CA would also move the healers. Thus, we would want to exclude them using a '''[filter_own]''' tag.&lt;br /&gt;
* Adding two instances of the same candidate action with different scores to force the order in which units participate in this action. For example, one could add another instance of the move-leader-to-keep CA with a '''[filter_own]''' tag restricting it to one leader, in order to make this leader move to a keep (and therefore recruit) first.&lt;br /&gt;
&lt;br /&gt;
Note that for the combat AI, using the attacks aspect to filter units as described in the next section '''is slightly more efficient'''. Thus, that should be the preferred method if there are many units on the map.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[candidate_action]&lt;br /&gt;
	id=villages&lt;br /&gt;
	engine=cpp&lt;br /&gt;
	name=ai_default_rca::get_villages_phase&lt;br /&gt;
	max_score=60000&lt;br /&gt;
	score=60000&lt;br /&gt;
	[filter_own]&lt;br /&gt;
		type=Wolf Rider&lt;br /&gt;
	[/filter_own]&lt;br /&gt;
[/candidate_action]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Filtering Combat with the ''attacks'' Aspect==&lt;br /&gt;
&lt;br /&gt;
The ''attacks'' aspect lets us filter the units considered by the ''combat'', ''high_xp_attacks'' and ''spread_poison'' 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&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [aspect]&lt;br /&gt;
        id=attacks&lt;br /&gt;
        [facet]&lt;br /&gt;
            invalidate_on_gamestate_change=yes&lt;br /&gt;
            [filter_own]&lt;br /&gt;
                type=Elvish Sorceress,Elvish Enchantress,Elvish Sylph&lt;br /&gt;
            [/filter_own]&lt;br /&gt;
            [filter_enemy]&lt;br /&gt;
                race=undead&lt;br /&gt;
            [/filter_enemy]&lt;br /&gt;
        [/facet]&lt;br /&gt;
    [/aspect]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Several important notes:&lt;br /&gt;
* The ''attacks'' aspect is a ''composite'' aspect and cannot be defined using the simple syntax of the other ''standard'' aspects.  See [[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Aspects|here]] for a description of the syntax of the example.&lt;br /&gt;
* This only works if &amp;lt;code&amp;gt;invalidate_on_gamestate_change=yes&amp;lt;/code&amp;gt; is 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 [[Wesnoth_AI_Framework#Some_more_on_the_invalidation_keys|here]].&lt;br /&gt;
* If [filter_own] or [filter_enemy] are omitted, the selection defaults to all units of the respective sides.&lt;br /&gt;
* '''Most importantly''': The above example results in sorceress-line vs undead attacks being the &amp;lt;u&amp;gt;only&amp;lt;/u&amp;gt; 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:&lt;br /&gt;
** Setting up the [filter_own] or [filter_enemy] [[StandardUnitFilter]]s to adapt dynamically to the situation on the map.&lt;br /&gt;
** 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_AIs#Micro_AI_Test_and_Demo_Scenarios|Micro AI test scenario]] [https://github.com/wesnoth/wesnoth/blob/master/data/ai/micro_ais/scenarios/protect_unit.cfg ''Protect Unit''].&lt;br /&gt;
** Using [[LuaAI#Dynamic_Lua_Aspects|dynamic Lua aspects]] is in principle also possible, but far from easy due to the complex tables this aspect returns.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|5}}&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [attacks]&lt;br /&gt;
        invalidate_on_gamestate_change=yes&lt;br /&gt;
        [filter_own]&lt;br /&gt;
            type=Elvish Sorceress,Elvish Enchantress,Elvish Sylph&lt;br /&gt;
        [/filter_own]&lt;br /&gt;
        [filter_enemy]&lt;br /&gt;
            race=undead&lt;br /&gt;
        [/filter_enemy]&lt;br /&gt;
    [/attacks]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category: WML Reference]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AiWML&amp;diff=65215</id>
		<title>AiWML</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AiWML&amp;diff=65215"/>
		<updated>2019-12-12T16:20:53Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Filtering Combat with the attacks Aspect */ now also applies to spread_poison CA&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{WML Tags}}&lt;br /&gt;
&lt;br /&gt;
== Customizing the Default AI Using WML ==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
This page contains:&lt;br /&gt;
* Descriptions of the aspects and goals of the default AI&lt;br /&gt;
* Instructions for setting them up at the beginning of the scenario in a [side] tag&lt;br /&gt;
&lt;br /&gt;
This page does not contain:&lt;br /&gt;
* Instructions for [[LuaAI#Dynamic_Lua_Aspects|setting up aspects dynamically using Lua]].&lt;br /&gt;
* Instructions for [[Modifying_AI_Components#Modifying_Standard_Aspects|changing aspects and goals mid scenario]], although for many of them (for example, for all [[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Aspects|standard aspects]]) the methods are almost identical&lt;br /&gt;
* Instructions for some of the [[Modifying_AI_Components|more complex AI customization tasks]].&lt;br /&gt;
&lt;br /&gt;
== Defining Aspects and Goals of the RCA AI ==&lt;br /&gt;
&lt;br /&gt;
[[RCA_AI#Use_of_Aspects_in_the_RCA_AI|Aspects]] and [[RCA_AI#Use_of_Goals_and_Targets_in_the_RCA_AI|goals]] of the [[RCA_AI|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 [[#List_of_AI_Aspects|aspects themselves]] as arguments, as well as the following general keys:&lt;br /&gt;
* '''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 [https://github.com/wesnoth/wesnoth/blob/master/data/core/macros/schedules.cfg 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.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
* '''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. {{DevFeature1.13|5}} In 1.13, the meaning of '''ai_algorithm''' has changed and no longer selects a native AI that's built into the engine, as the RCA AI has now become the core system and no alternatives exist anymore. Instead, it selects a predefined ''[ai]'' definition, such as those defined in ''data/ai/ais''. Since these are defined in WML, you can easily build your own.&lt;br /&gt;
&lt;br /&gt;
An example of a simple use of the [ai] tag to set AI parameters (aspects) is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=wml&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    time_of_day=dusk&lt;br /&gt;
    aggression=0.99&lt;br /&gt;
    caution=0.01&lt;br /&gt;
    [avoid]&lt;br /&gt;
        [filter]&lt;br /&gt;
            side=2,3&lt;br /&gt;
        [/filter]&lt;br /&gt;
        radius=2&lt;br /&gt;
    [/avoid]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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 &amp;lt;code&amp;gt;:inspect&amp;lt;/code&amp;gt; in debug mode to verify whether an aspect was changed successfully.&lt;br /&gt;
&lt;br /&gt;
==List of AI Aspects==&lt;br /&gt;
&lt;br /&gt;
* '''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 &amp;lt;u&amp;gt;only&amp;lt;/u&amp;gt; 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:'''&lt;br /&gt;
** This is a simplified version of the more complex (and arguably more useful) application of a dynamic Lua ''advancements'' aspect, as described [[LuaAI#The_advancements_Aspect|here]].&lt;br /&gt;
** The ''advancements'' aspect only takes effect during the AI turn, that is, on offense only, not on defense.&lt;br /&gt;
** 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).&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
** '''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.&lt;br /&gt;
** 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 &amp;gt;0 is found.  The combat CA score is zero if the highest attack score is &amp;lt;=0).&lt;br /&gt;
** 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.&lt;br /&gt;
** All the negative contributions to the score are multiplied by '(1-aggression)'.  This means that:&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
*** 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.&lt;br /&gt;
** 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.&lt;br /&gt;
&lt;br /&gt;
*'''[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 [[#Filtering_Combat_with_the_attacks_Aspect|separate section]] below.&lt;br /&gt;
&lt;br /&gt;
* '''[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''.&lt;br /&gt;
** '''[[StandardLocationFilter]]''': The locations for the AI to avoid.  Do not use a [filter_location] tag.&lt;br /&gt;
&lt;br /&gt;
* '''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 [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|evaluation scores]]):&lt;br /&gt;
** ''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].&lt;br /&gt;
** ''Combat'' CA: &lt;br /&gt;
*** 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 &amp;quot;exposure&amp;quot; rating of the units.  This exposure is then subtracted from the 'attack score' as described for ''aggression'' above.&lt;br /&gt;
*** 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.)&lt;br /&gt;
*** If the AI leader is used in an attack, the AI ''always'' uses 'caution=2' for the evaluation of that attack.&lt;br /&gt;
** ''Retreat'' CA: &lt;br /&gt;
*** 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 &amp;lt;tt&amp;gt;caution &amp;amp;times; their_power &amp;gt; our_power&amp;lt;/tt&amp;gt;. 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.&lt;br /&gt;
*** 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. &lt;br /&gt;
*** The AI never retreats if caution is set to 0 or lower.&lt;br /&gt;
** ''Move-to-targets'' CA:&lt;br /&gt;
*** 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. &amp;quot;Dangerous&amp;quot; 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 &amp;lt;tt&amp;gt;our_strength / their_strength &amp;gt; 0.5 + caution&amp;lt;/tt&amp;gt;. 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.&lt;br /&gt;
***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.&lt;br /&gt;
*** The AI also considers retreating units during the move-to-target phase based on criteria similar to those for the retreat CA.&lt;br /&gt;
&lt;br /&gt;
* '''[goal]'''=&amp;quot;&amp;quot;: Defines units or locations as [[#AI_Targets_and_Goals|move targets]] (not attack targets!) for the AI.  '''Applies to ''move-to-targets'' CA only'''. &amp;lt;-- 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 [[#The_.5Bgoal.5D_Tag|[goal] tag]] below for details.&lt;br /&gt;
&lt;br /&gt;
* '''grouping'''=&amp;quot;offensive&amp;quot;: (string) How the AI should try to group units.  Applies to ''move-to-targets'' CA only.  Possible values:&lt;br /&gt;
** ''offensive'': Makes the AI try to group units together before attacking.&lt;br /&gt;
** ''defensive'': Makes the AI group units together very conservatively, only advancing them much beyond its castle if it has overwhelming force.&lt;br /&gt;
** ''no'': Makes the AI use no grouping behavior.&lt;br /&gt;
&lt;br /&gt;
* '''leader_aggression'''=&amp;quot;-4.0&amp;quot;: 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''.&lt;br /&gt;
&lt;br /&gt;
* '''[leader_goal]'''=&amp;quot;&amp;quot;: Makes the AI try to move its leader to a specific location.  Applies to ''move-leader-to-goals'' CA only.&lt;br /&gt;
** '''x''', '''y''': The location toward which the AI should move its leader.&lt;br /&gt;
** '''auto_remove'''=no: (bool)  If 'no' (default), the AI moves the leader to the goal, after which he stays there until [leader_goal] is [[Modifying_AI_Components#Modifying_Standard_Aspects|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).&lt;br /&gt;
** '''id'''=&amp;quot;&amp;quot;: (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] [[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Aspects|facet]].  Thus, in principle for this to work, you need to set the id of the facet as described [[Modifying_AI_Components#Modifying_Standard_Aspects|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. {{DevFeature1.13|5}} 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.&lt;br /&gt;
**'''max_risk'''=1-caution: (double: meaningful values are &amp;gt;=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.&lt;br /&gt;
&lt;br /&gt;
* '''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:&lt;br /&gt;
** Leaders participate in all the moves and attacks in the same way as any other unit of the AI side.&lt;br /&gt;
** 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.&lt;br /&gt;
&lt;br /&gt;
* '''leader_value'''=3: (double) A number 0 or higher which determines the value of enemy leaders as [[#AI_Targets_and_Goals|targets]].  Affects ''move-to-targets CA'' only (and therefore specifically does not apply to attacks).&lt;br /&gt;
&lt;br /&gt;
* '''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''.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
*'''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 [[AI_Recruitment|separate page]].&lt;br /&gt;
&lt;br /&gt;
* '''scout_village_targeting'''=3: (double) The AI multiplies the value of village [[#AI_Targets_and_Goals|targets]] for scouts by this value.  Affects ''move-to-targets'' CA only.&lt;br /&gt;
&lt;br /&gt;
* '''simple_targeting'''=no: (bool) If 'yes', the AI moves its units toward [[#AI_Targets_and_Goals|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.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
* '''village_value'''=1: (double) A number 0 or higher which determines how much the AI tries to go for villages as [[#AI_Targets_and_Goals|targets]].  Affects ''move-to-targets'' CA only.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
=== Removed AI Aspects ===&lt;br /&gt;
&lt;br /&gt;
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. {{DevFeature1.13|5}} Except for attack_depth, these have now all been removed.&lt;br /&gt;
&lt;br /&gt;
* '''attack_depth'''=5: (int)&lt;br /&gt;
* '''number_of_possible_recruits_to_force_recruit'''=3.1: (double)&lt;br /&gt;
* '''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.&lt;br /&gt;
** It is, in principle, still possible to use this aspect together with the [[Wesnoth_AI_Framework#Recruitment_Stage|recruitment stage]], but this is discouraged for the reasons given at this link.&lt;br /&gt;
* '''recruitment_ignore_bad_combat'''=no: (bool)&lt;br /&gt;
* '''recruitment_ignore_bad_movement'''=no: (bool)&lt;br /&gt;
&lt;br /&gt;
==AI Targets and Goals==&lt;br /&gt;
&lt;br /&gt;
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 [[RCA_AI#RCA_AI_Aspect_and_Goal_Configuration|base values]].  Additional targets can be defined using the [goal] tag.&lt;br /&gt;
&lt;br /&gt;
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 [[RCA_AI#Use_of_Goals_and_Targets_in_the_RCA_AI|here]].  The sections below only describe how to set up goals in order to add targets for the ''move-to-targets'' CA.&lt;br /&gt;
&lt;br /&gt;
===The [goal] Tag===&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
* '''name'''=&amp;quot;target&amp;quot;: (string)  The following values are possible and result in different types of targets, as shown in the examples below:&lt;br /&gt;
** ''target'':  The (default) target goal specifies target units (not necessarily enemy units) toward which the AI should move its units. {{DevFeature1.13|5}} target_unit is now a synonym for this.&lt;br /&gt;
** ''target_location'': Specifies target locations toward which the AI should move its units.&lt;br /&gt;
** ''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.&lt;br /&gt;
** ''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.&lt;br /&gt;
** ''protect_my_unit'': ('''deprecated''') &amp;lt;s&amp;gt;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.&amp;lt;/s&amp;gt;&lt;br /&gt;
** ''lua_goal'': A dynamically calculated goal written in Lua. See [[LuaAI#Lua_Goals_and_Targets|here]] for details on how this works.&lt;br /&gt;
&lt;br /&gt;
* '''[criteria]'''=&amp;quot;&amp;quot;: Contains a [[StandardUnitFilter]] (for ''target'', ''protect_unit'' or ''protect_my_unit'') or [[StandardLocationFilter]] (for ''target_location'' or ''protect_location'') describing the targets.&lt;br /&gt;
&lt;br /&gt;
* '''value'''=0: (double) The value of the goal.&lt;br /&gt;
&lt;br /&gt;
* '''protect_radius'''=20: (int) The protection radius.  Applies to ''protect_location'', ''protect_unit'' and ''protect_my_unit''.&lt;br /&gt;
&lt;br /&gt;
===Examples of [goal] Tag Usage===&lt;br /&gt;
&lt;br /&gt;
'''target:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        [criteria] #NOTE: this is a SUF, because we're targeting a unit&lt;br /&gt;
            side=3&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''target_location:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=target_location&lt;br /&gt;
        [criteria] #NOTE: this is a SLF, because we're targeting a location&lt;br /&gt;
            x,y=42,20&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''protect_location:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=protect_location&lt;br /&gt;
        [criteria] #NOTE: this is a SLF, because we're protecting a location&lt;br /&gt;
            x,y=42,20&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        protect_radius=16&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''protect_unit:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=protect_unit&lt;br /&gt;
        [criteria] #NOTE: this is a SUF, because we're protecting a unit&lt;br /&gt;
            side=3&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        protect_radius=16&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''How to use protect_unit to protect the own leader''' (We are side 2)&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [goal]&lt;br /&gt;
        name=protect_unit&lt;br /&gt;
        [criteria]&lt;br /&gt;
            side=2&lt;br /&gt;
            canrecruit=yes&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        protect_radius=8&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Deprecated AI Targeting Aspects ===&lt;br /&gt;
&lt;br /&gt;
The following AI targeting parameters currently still work, but have been superseded by the [[#The_.5Bgoal.5D_Tag|[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.&lt;br /&gt;
&lt;br /&gt;
* '''[target]'''=&amp;quot;&amp;quot;: '''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.&lt;br /&gt;
** '''[[StandardUnitFilter]]''': Do not use a [filter] tag.&lt;br /&gt;
** '''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.&lt;br /&gt;
&lt;br /&gt;
* '''[protect_location]'''=&amp;quot;&amp;quot;: '''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.&lt;br /&gt;
** '''x''', '''y''': Standard coordinates. These indicate the location the AI is protecting.&lt;br /&gt;
** '''radius''': The radius around it to protect (0 indicates a single hex). {{DevFeature1.13|5}} This key has been changed to protect_radius, though it's preferred to change old code to use the [goal] tag instead.&lt;br /&gt;
** '''value''': The importance of protecting this location.&lt;br /&gt;
&lt;br /&gt;
* '''[protect_unit]'''=&amp;quot;&amp;quot;: '''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.&lt;br /&gt;
** '''[[StandardUnitFilter]]''': The unit(s) to protect. Do not use a [filter] tag.&lt;br /&gt;
** '''radius''': The radius around it to protect (0 indicates a single hex). {{DevFeature1.13|5}} This key has been changed to protect_radius, though it's preferred to change old code to use the [goal] tag instead.&lt;br /&gt;
** '''value''': The importance of protecting this unit.&lt;br /&gt;
&lt;br /&gt;
* '''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.&lt;br /&gt;
&lt;br /&gt;
== Filtering which Units Participate in which Actions==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}}&lt;br /&gt;
&lt;br /&gt;
Each [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|candidate action of the default AI]] can be restricted to apply to only a subset of the AI units. This is done by adding a '''[filter_own]''' tag to the '''[[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Candidate_Actions|[candidate_action]]]''' tag definition.&lt;br /&gt;
&lt;br /&gt;
Typical use cases:&lt;br /&gt;
* Restricting an action to certain units so that other units are not used to this. For example, village grabbing could be restricted to scout units only, so that slower units do not take detours on their way toward the enemies. See the example below.&lt;br /&gt;
* Excluding units from a certain action in order to have them available for other actions afterward. For example, one might want to add a custom candidate action which moves healers behind injured units (or use the existing [[Micro_AIs|Micro AI]] for this). Ideally, this should happen after ''all'' other units have moved, specifically also after the move-to-targets candidate action (MtT CA) is done. In the default setting, however, the MtT CA would also move the healers. Thus, we would want to exclude them using a '''[filter_own]''' tag.&lt;br /&gt;
* Adding two instances of the same candidate action with different scores to force the order in which units participate in this action. For example, one could add another instance of the move-leader-to-keep CA with a '''[filter_own]''' tag restricting it to one leader, in order to make this leader move to a keep (and therefore recruit) first.&lt;br /&gt;
&lt;br /&gt;
Note that for the combat AI, using the attacks aspect to filter units as described in the next section '''is slightly more efficient'''. Thus, that should be the preferred method if there are many units on the map.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[candidate_action]&lt;br /&gt;
	id=villages&lt;br /&gt;
	engine=cpp&lt;br /&gt;
	name=ai_default_rca::get_villages_phase&lt;br /&gt;
	max_score=60000&lt;br /&gt;
	score=60000&lt;br /&gt;
	[filter_own]&lt;br /&gt;
		type=Wolf Rider&lt;br /&gt;
	[/filter_own]&lt;br /&gt;
[/candidate_action]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Filtering Combat with the ''attacks'' Aspect==&lt;br /&gt;
&lt;br /&gt;
The ''attacks'' aspect lets us filter the units considered by the ''combat'', ''high_xp_attacks'' and ''spread_poison'' 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&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [aspect]&lt;br /&gt;
        id=attacks&lt;br /&gt;
        [facet]&lt;br /&gt;
            invalidate_on_gamestate_change=yes&lt;br /&gt;
            [filter_own]&lt;br /&gt;
                type=Elvish Sorceress,Elvish Enchantress,Elvish Sylph&lt;br /&gt;
            [/filter_own]&lt;br /&gt;
            [filter_enemy]&lt;br /&gt;
                race=undead&lt;br /&gt;
            [/filter_enemy]&lt;br /&gt;
        [/facet]&lt;br /&gt;
    [/aspect]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Several important notes:&lt;br /&gt;
* The ''attacks'' aspect is a ''composite'' aspect and cannot be defined using the simple syntax of the other ''standard'' aspects.  See [[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Aspects|here]] for a description of the syntax of the example.&lt;br /&gt;
* This only works if &amp;lt;code&amp;gt;invalidate_on_gamestate_change=yes&amp;lt;/code&amp;gt; is 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 [[Wesnoth_AI_Framework#Some_more_on_the_invalidation_keys|here]].&lt;br /&gt;
* If [filter_own] or [filter_enemy] are omitted, the selection defaults to all units of the respective sides.&lt;br /&gt;
* '''Most importantly''': The above example results in sorceress-line vs undead attacks being the &amp;lt;u&amp;gt;only&amp;lt;/u&amp;gt; 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:&lt;br /&gt;
** Setting up the [filter_own] or [filter_enemy] [[StandardUnitFilter]]s to adapt dynamically to the situation on the map.&lt;br /&gt;
** 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_AIs#Micro_AI_Test_and_Demo_Scenarios|Micro AI test scenario]] [https://github.com/wesnoth/wesnoth/blob/master/data/ai/micro_ais/scenarios/protect_unit.cfg ''Protect Unit''].&lt;br /&gt;
** Using [[LuaAI#Dynamic_Lua_Aspects|dynamic Lua aspects]] is in principle also possible, but far from easy due to the complex tables this aspect returns.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.13|5}}&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    [attacks]&lt;br /&gt;
        invalidate_on_gamestate_change=yes&lt;br /&gt;
        [filter_own]&lt;br /&gt;
            type=Elvish Sorceress,Elvish Enchantress,Elvish Sylph&lt;br /&gt;
        [/filter_own]&lt;br /&gt;
        [filter_enemy]&lt;br /&gt;
            race=undead&lt;br /&gt;
        [/filter_enemy]&lt;br /&gt;
    [/attacks]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category: WML Reference]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=RCA_AI&amp;diff=65213</id>
		<title>RCA AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=RCA_AI&amp;diff=65213"/>
		<updated>2019-12-12T03:44:07Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* The Candidate Actions (CAs) of the main_loop Stage in the RCA AI */ Add former Experimental AI CAs&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==RCA AI Summary==&lt;br /&gt;
&lt;br /&gt;
The RCA (Register Candidate Action) AI is the default AI since Wesnoth 1.9.  Its name stems from the fact that it has a list of potential actions, called ''candidate actions'' (CAs), that are evaluated for each move and are executed in order of evaluation score.  The RCA AI is highly configurable in WML.  It can also be modified by removing default CAs or adding custom CAs.&lt;br /&gt;
&lt;br /&gt;
This page describes how the RCA AI is assembled out of the different components of Wesnoth's [[Wesnoth_AI_Framework#Wesnoth_AI_Framework:_A_Composite_AI|composite AI framework]].  For related information also see:&lt;br /&gt;
* [[Wesnoth_AI_Framework#Types_of_AI_Components|Types of AI Components]]: General descriptions of the different AI components.&lt;br /&gt;
* [[AiWML]]: Customizing the RCA AI by setting AI parameters (aspects and goals)&lt;br /&gt;
* [[Creating Custom AIs]] and [[Modifying AI Components]]: Adding additional functionality to the RCA AI, or replacing parts or all of it.&lt;br /&gt;
* [[Wesnoth AI]]: Links to all Wesnoth AI resources on this wiki.&lt;br /&gt;
&lt;br /&gt;
== RCA AI components  ==&lt;br /&gt;
&lt;br /&gt;
The RCA AI utilizes the following components:&lt;br /&gt;
&lt;br /&gt;
* The [[Wesnoth_AI_Framework#Stages|'''''main_loop'' stage''']] with its candidate action evaluation loop.&lt;br /&gt;
&lt;br /&gt;
* [[Wesnoth_AI_Framework#The_Candidate_Actions_of_the_main_loop_Stage|'''Candidate actions''']] are the potential actions the AI might take during an AI turn, such as attacking, village grabbing, recruiting or retreating injured units.&lt;br /&gt;
&lt;br /&gt;
* [[Wesnoth_AI_Framework#Aspects_and_Goals|'''Aspects''']] are the parameters which can be used to configure the AI behavior.&lt;br /&gt;
&lt;br /&gt;
* [[Wesnoth_AI_Framework#Aspects_and_Goals|'''Goals''']] are a specific type of aspect which define targets toward which the AI moves.  It is ''very important'' to understand that these are only move-to targets, not attack targets.&lt;br /&gt;
&lt;br /&gt;
Links here are to the general descriptions of these components in the [[Wesnoth AI Framework]], while the remainder of this page is about their use in the RCA AI.&lt;br /&gt;
&lt;br /&gt;
=== The Candidate Actions (CAs) of the ''main_loop'' Stage in the RCA AI ===&lt;br /&gt;
&lt;br /&gt;
For each move of its turn, the AI evaluates a number of potential actions, called ''candidate actions'' (CAs). The CA with the highest evaluation score is executed and the process is repeated, until no more valid moves are found.  This ends the AI turn.  (Note: This is the general process of the [[Wesnoth_AI_Framework#Stages|''main_loop'' stage]], it is not specific to the RCA AI.)&lt;br /&gt;
&lt;br /&gt;
Most of the RCA AI CAs have a single fixed scores, with a few of the Lua CAs have two distinct (but otherwise also fixed) scores, as indicated below.  These values can, in principle, be modified, but generally only to other fixed values.  For example, the recruitment CA by default has a score of 180,000.  The default score for attacks is 100,000.  Thus, if recruiting is possible, it will always be done before attacks.  If the score of the recruitment CA were lowered to below 100,000, then attacks would always be evaluated (and executed, if possible) before recruiting.&lt;br /&gt;
&lt;br /&gt;
It is important to realize though that the default score settings do not mean that all recruiting always happens before all attacks.  The &amp;quot;if possible&amp;quot; clauses in the previous paragraph become important here.  Consider the case that all castle hexes are occupied by AI units which were recruited on the previous turn and have not been used yet this turn.  No recruiting is possible (in which case the CA returns 0), so the AI moves on to other CAs.  Once it gets to attacks, one of the units (or a group of units) might be selected to move off the castle and attack a close-by enemy.  Now a castle hex is available for recruiting, which now has the highest evaluation score and is executed.  This cycle might be repeated several times.&lt;br /&gt;
&lt;br /&gt;
In its default configuration, the RCA AI evaluates the following CAs, sorted here in order of their evaluation score:&lt;br /&gt;
&lt;br /&gt;
*'''Goto''' CA (score 200,000; C++ CA): Move units toward the coordinates set by [[SingleUnitWML|goto_x, goto_y]] in their unit WML.&lt;br /&gt;
&lt;br /&gt;
*{{DevFeature1.15|3}} '''Castle switching''' CA (scores 195,000 and 15,000; Lua CA): move the leader to a different castle, generally one that is closer to the enemy leader(s).&lt;br /&gt;
&lt;br /&gt;
*{{DevFeature1.15|3}} '''Retreat injured''' CA (scores 192,000 and 95,000; Lua CA): retreat injured units that are threatened by enemies to healing locations and away from these enemies.&lt;br /&gt;
&lt;br /&gt;
*{{DevFeature1.15|3}} '''Spread poison''' CA (score 190,000; Lua CA): spread poison to enemies, while taking care not to try to poison unpoisonable or already poisoned units.&lt;br /&gt;
&lt;br /&gt;
*'''Recruitment''' CA (score 180,000; C++ CA): Recruit or recall units.&lt;br /&gt;
&lt;br /&gt;
*'''Move leader to goals''' CA (score 140,000; C++ CA): Move the leader toward goals set by [[AiWML#List_of_AI_Aspects|[leader_goal]]].&lt;br /&gt;
&lt;br /&gt;
*'''Move leader to keep''' CA (score 120,000; C++ CA): Move the leader toward the closest available keep.&lt;br /&gt;
&lt;br /&gt;
*'''High XP attack''' CA (score 100,010; Lua CA): An addition to the default AI as of '''Wesnoth 1.13.6''', this CA performs attacks on enemy units so close to leveling that the combat CA (below) does not attack them. Unlike all the other RCA AI candidate actions, this CA is written in Lua.&lt;br /&gt;
&lt;br /&gt;
*'''Combat''' CA (score 100,000; C++ CA): Attack enemy units that are in range.  Note that this CA includes the move to a hex adjacent to the enemy unit as well as the attack itself.&lt;br /&gt;
&lt;br /&gt;
*{{DevFeature1.15|3}} '''Place healers''' CA (score 96,000; Lua CA): Place healers adjacent to injured units that have already moved.&lt;br /&gt;
&lt;br /&gt;
*'''Healing''' CA (score 80,000; C++ CA): Move units onto healing locations (generally villages, but also next to healers that were placed previously).&lt;br /&gt;
&lt;br /&gt;
*'''Villages''' CA (score 60,000; C++ CA): Move units onto villages that are unowned or owned by an enemy.&lt;br /&gt;
&lt;br /&gt;
*'''Retreat''' CA (score 40,000; C++ CA): Evaluate if any units are in grave danger or hopelessly outnumbered, and if so, retreat them.  *{{DevFeature1.15|3}} This CA was removed and replaced by the &amp;quot;retreat injured&amp;quot; CA described above&lt;br /&gt;
&lt;br /&gt;
*'''Move to targets''' CA (score 20,000; C++ CA): Evaluate potential [[RCA_AI#Use_of_Goals_and_Targets_in_the_RCA_AI|targets]] and move units toward them.  The evaluation takes into account how valuable the targets are (this is configurable by setting [[AiWML#AI_Targets_and_Goals|goals]]), how easily the AI's units can get there, how exposed to attacks they will be, etc.  Targets are enemy leaders, enemy units that pose a threat to the AI leader and villages, as well as anything defined in a [[AiWML#The_.5Bgoal.5D_Tag|[goal] tag]].  It is important to understand that targets only apply to the ''move-to-targets'' CA.  If a target is within range of the AI's unit(s) on the current turn, other CAs, such as ''combat'', ''healing'' or ''villages'' have a higher evaluation score and are (usually) executed first.  Thus, a target is specifically &amp;lt;u&amp;gt;not&amp;lt;/u&amp;gt; an attack target, but a move target.&lt;br /&gt;
&lt;br /&gt;
*'''Leader shares keep''' CA (score 10,000; C++ CA): Move the leader off the keep to let an allied leader recruit, if an allied leader can get to the keep in one move.  Note: This CA used to be called ''passive_leader_shares_keep''.  That name still works for the time being but will be deprecated at some point.&lt;br /&gt;
&lt;br /&gt;
*{{DevFeature1.15|3}} '''Move to any enemy''' CA (score 1,000; Lua CA): Move AI units toward any enemy. That is a fall-back CA in case none of the other CAs moved the unit. This is generally only the case on maps without enemy leaders and villages.&lt;br /&gt;
&lt;br /&gt;
As already mentioned, the CAs are always evaluated (and executed, if a possible move is found) in the order given above.  For example, the ''goto'' CA is always the first move that is done (if ''goto_x'' and ''goto_y'' are defined and a move toward that location is possible).  Equivalently, if a valid attack is found in the ''combat'' CA, it is &amp;lt;u&amp;gt;always&amp;lt;/u&amp;gt; executed before any healing, village grabbing or move-to-target moves.  One of the effects this has is that attacks always have priority over any goals/targets of the move-to-target phase.  It is '''very important''' to understand this, as it is a frequent source of confusion for scenario creators.&lt;br /&gt;
&lt;br /&gt;
Note that nothing in Wesnoth's [[Wesnoth_AI_Framework|general AI mechanism]] limits CAs to having fixed evaluation scores.  For example, some of the [[Micro AIs]] as well as some of the new Lua AI CAs return variable scores.&lt;br /&gt;
&lt;br /&gt;
=== Use of Aspects in the RCA AI ===&lt;br /&gt;
&lt;br /&gt;
An aspect is a parameter that the AI framework provides to scenario creators for configuring the AI behavior.  For example, one can change how aggressive/careful the AI acts, how recruiting is prioritized or whether the leader participates in attacks.&lt;br /&gt;
&lt;br /&gt;
The RCA AI has a large number of aspects.  Their description is therefore given [[AiWML|its own page]].&lt;br /&gt;
&lt;br /&gt;
=== Use of Goals and Targets in the RCA AI ===&lt;br /&gt;
&lt;br /&gt;
''Goals'' define AI ''targets'' which, in turn, are used in the ''move-to-targets'' candidate action 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. A score is then assigned to each target based on this base value, as well as on the movement cost required to get there, whether moving there would put the units involved in danger, etc.&lt;br /&gt;
&lt;br /&gt;
It is possible to define additional targets or influence the relative ratings of the default targets. This is done with the [[AiWML#The_.5Bgoal.5D_Tag|[goal] tag]], in which we can set criteria selecting targets and define their base values.  These are then evaluated in the same way as default targets. Values set with the [goal] tag should always be relative to each other and to the values of the default targets (see [[RCA_AI#RCA_AI_Aspect_and_Goal_Configuration|below]] for the latter). The 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'.&lt;br /&gt;
&lt;br /&gt;
We stress again that these targets apply to the ''move-to-targets'' CA only and have no influence on other CAs. This is significant since CAs that deal with, for example, combat or village-grabbing have a higher score than the move-to-targets CA and are therefore always executed first. In practice that means that targets set with the [goal] tag only affect the AI behavior for targets that it cannot reach during the current turn, and only after combat, village-grabbing etc. are finished.&lt;br /&gt;
&lt;br /&gt;
==RCA AI Configuration==&lt;br /&gt;
&lt;br /&gt;
This section describes the configuration of the RCA AI.  An understanding of this setup is essential for [[Modifying AI Components]] or [[Creating Custom AIs]].&lt;br /&gt;
&lt;br /&gt;
In the following, we assume that the reader is familiar with the general principles of the [[Wesnoth AI Framework]].  The keys and tags used in the config snippets shown below are explained there.&lt;br /&gt;
&lt;br /&gt;
=== RCA AI Stage Configuration ===&lt;br /&gt;
&lt;br /&gt;
The RCA AI uses only the ''main_loop'' stage.  It is defined in file [https://github.com/wesnoth/wesnoth/blob/master/data/ai/ais/ai_default_rca.cfg ai/ais/ai_default_rca.cfg] in the Wesnoth data directory.  {{DevFeature1.15|3}} Note that the example below is from a previous Wesnoth version as new candidate actions have since been added to the default AI. The linked file always contains the most up-to-date configuration though and can be used as reference.&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    id=ai_default_rca&lt;br /&gt;
    description=_&amp;quot;Multiplayer_AI^Default AI (RCA)&amp;quot; # wmllint: no spellcheck&lt;br /&gt;
    [stage]&lt;br /&gt;
        id=main_loop&lt;br /&gt;
        name=ai_default_rca::candidate_action_evaluation_loop&lt;br /&gt;
        {AI_CA_GOTO}                         # id=&amp;quot;goto&amp;quot;&lt;br /&gt;
        {AI_CA_RECRUITMENT}                  # id=&amp;quot;recruitment&amp;quot;&lt;br /&gt;
        {AI_CA_MOVE_LEADER_TO_GOALS}         # id=&amp;quot;move_leader_to_goals&amp;quot;&lt;br /&gt;
        {AI_CA_MOVE_LEADER_TO_KEEP}          # id=&amp;quot;move_leader_to_keep&amp;quot;&lt;br /&gt;
        {AI_CA_HIGH_XP_ATTACK}               # id=&amp;quot;high_xp_attack&amp;quot; (Wesnoth 1.13.6 and later only)&lt;br /&gt;
        {AI_CA_COMBAT}                       # id=&amp;quot;combat&amp;quot;&lt;br /&gt;
        {AI_CA_HEALING}                      # id=&amp;quot;healing&amp;quot;&lt;br /&gt;
        {AI_CA_VILLAGES}                     # id=&amp;quot;villages&amp;quot;&lt;br /&gt;
        {AI_CA_RETREAT}                      # id=&amp;quot;retreat&amp;quot;&lt;br /&gt;
        {AI_CA_MOVE_TO_TARGETS}              # id=&amp;quot;move_to_targets&amp;quot;&lt;br /&gt;
        {AI_CA_PASSIVE_LEADER_SHARES_KEEP}   # id=&amp;quot;passive_leader_shares_keep&amp;quot;&lt;br /&gt;
     [/stage]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Note''': Before the RCA AI became the default, it was necessary to include this file (or one of its aliases) in the side definition.  That is not required any more – it is automatically included for all computer controlled sides, except when a stage is defined explicitly using the [stage] tag in the side definition.&lt;br /&gt;
&lt;br /&gt;
=== RCA AI Candidate Action Configuration ===&lt;br /&gt;
&lt;br /&gt;
In the configuration of the ''main_loop'' stage shown above, each candidate action is included as a macro.  Expanding the macros results in the following syntax (''combat'' CA example):&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[candidate_action]&lt;br /&gt;
    id=combat&lt;br /&gt;
    engine=cpp&lt;br /&gt;
    name=ai_default_rca::combat_phase&lt;br /&gt;
    max_score=100000&lt;br /&gt;
    score=100000&lt;br /&gt;
[/candidate_action]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Available Candidate Actions ====&lt;br /&gt;
&lt;br /&gt;
See file [https://github.com/wesnoth/wesnoth/blob/master/data/core/macros/ai_candidate_actions.cfg data/core/macros/ai_candidate_actions.cfg] for the definitions of all the macros, as well as for the ids, names and default scores of the available CAs.&lt;br /&gt;
&lt;br /&gt;
=== RCA AI Aspect and Goal Configuration ===&lt;br /&gt;
&lt;br /&gt;
When a game is created, '''aspects''' are initialized with the default values given at [[AiWML#List_of_AI_Aspects|AiWML]].  In the [https://github.com/wesnoth/wesnoth/blob/master/data/ai/utils/default_config.cfg configuration file], they are in ''composite'' aspect format such as&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[aspect]&lt;br /&gt;
    id=aggression&lt;br /&gt;
    engine=cpp&lt;br /&gt;
    name=composite_aspect&lt;br /&gt;
    [default]&lt;br /&gt;
        engine=cpp&lt;br /&gt;
        name=standard_aspect&lt;br /&gt;
        value=0.4&lt;br /&gt;
    [/default]&lt;br /&gt;
[/aspect]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
for ''aggression'', or&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[aspect]&lt;br /&gt;
    id=avoid&lt;br /&gt;
    engine=cpp&lt;br /&gt;
    name=composite_aspect&lt;br /&gt;
    [default]&lt;br /&gt;
        engine=cpp&lt;br /&gt;
        name=standard_aspect&lt;br /&gt;
        [value]&lt;br /&gt;
            [not]&lt;br /&gt;
            [/not]&lt;br /&gt;
        [/value]&lt;br /&gt;
    [/default]&lt;br /&gt;
[/aspect]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
for ''[avoid]''.&lt;br /&gt;
&lt;br /&gt;
The default '''goals''' are not initialized in configuration files.  Instead, they are defined in the C++ code of the ''move-to-targets'' candidate action as follows:&lt;br /&gt;
* Enemy leaders: Value is read from aspect ''leader_value'', which defaults to 3.&lt;br /&gt;
* Villages: Value is read from aspect ''village_value'', which defaults to 1.&lt;br /&gt;
* Any enemy unit that can attack the AI's leader on the enemy's next turn: The value of these targets is proportional to the threat they pose to the AI leader and generally higher than those of the other two types of default targets.&lt;br /&gt;
&lt;br /&gt;
The first two types of goals can be disabled by setting aspects ''village_value'' or ''leader_value'' to zero.  By contrast, close enemies cannot be removed from the list of targets, other than by removing the ''move-to-targets'' CA altogether.  One can, however, effectively disable them by setting other goals with vastly higher values (such as 100,000).&lt;br /&gt;
&lt;br /&gt;
==== Available Aspects and Goals ====&lt;br /&gt;
&lt;br /&gt;
See file [https://github.com/wesnoth/wesnoth/blob/master/data/ai/utils/default_config.cfg data/ai/utils/default_config.cfg] for the definitions of all the aspects and their default values.  The available goals are shown [[AiWML#AI_Targets_and_Goals|here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=RCA_AI&amp;diff=65212</id>
		<title>RCA AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=RCA_AI&amp;diff=65212"/>
		<updated>2019-12-12T01:48:45Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* RCA AI Stage Configuration */ Add note on new CAs from ExpAI&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==RCA AI Summary==&lt;br /&gt;
&lt;br /&gt;
The RCA (Register Candidate Action) AI is the default AI since Wesnoth 1.9.  Its name stems from the fact that it has a list of potential actions, called ''candidate actions'' (CAs), that are evaluated for each move and are executed in order of evaluation score.  The RCA AI is highly configurable in WML.  It can also be modified by removing default CAs or adding custom CAs.&lt;br /&gt;
&lt;br /&gt;
This page describes how the RCA AI is assembled out of the different components of Wesnoth's [[Wesnoth_AI_Framework#Wesnoth_AI_Framework:_A_Composite_AI|composite AI framework]].  For related information also see:&lt;br /&gt;
* [[Wesnoth_AI_Framework#Types_of_AI_Components|Types of AI Components]]: General descriptions of the different AI components.&lt;br /&gt;
* [[AiWML]]: Customizing the RCA AI by setting AI parameters (aspects and goals)&lt;br /&gt;
* [[Creating Custom AIs]] and [[Modifying AI Components]]: Adding additional functionality to the RCA AI, or replacing parts or all of it.&lt;br /&gt;
* [[Wesnoth AI]]: Links to all Wesnoth AI resources on this wiki.&lt;br /&gt;
&lt;br /&gt;
== RCA AI components  ==&lt;br /&gt;
&lt;br /&gt;
The RCA AI utilizes the following components:&lt;br /&gt;
&lt;br /&gt;
* The [[Wesnoth_AI_Framework#Stages|'''''main_loop'' stage''']] with its candidate action evaluation loop.&lt;br /&gt;
&lt;br /&gt;
* [[Wesnoth_AI_Framework#The_Candidate_Actions_of_the_main_loop_Stage|'''Candidate actions''']] are the potential actions the AI might take during an AI turn, such as attacking, village grabbing, recruiting or retreating injured units.&lt;br /&gt;
&lt;br /&gt;
* [[Wesnoth_AI_Framework#Aspects_and_Goals|'''Aspects''']] are the parameters which can be used to configure the AI behavior.&lt;br /&gt;
&lt;br /&gt;
* [[Wesnoth_AI_Framework#Aspects_and_Goals|'''Goals''']] are a specific type of aspect which define targets toward which the AI moves.  It is ''very important'' to understand that these are only move-to targets, not attack targets.&lt;br /&gt;
&lt;br /&gt;
Links here are to the general descriptions of these components in the [[Wesnoth AI Framework]], while the remainder of this page is about their use in the RCA AI.&lt;br /&gt;
&lt;br /&gt;
=== The Candidate Actions (CAs) of the ''main_loop'' Stage in the RCA AI ===&lt;br /&gt;
&lt;br /&gt;
For each move of its turn, the AI evaluates a number of potential actions, called ''candidate actions'' (CAs). The CA with the highest evaluation score is executed and the process is repeated, until no more valid moves are found.  This ends the AI turn.  (Note: This is the general process of the [[Wesnoth_AI_Framework#Stages|''main_loop'' stage]], it is not specific to the RCA AI.)&lt;br /&gt;
&lt;br /&gt;
The scores of the RCA AI CAs have fixed values.  These values can be modified, but only to other fixed values.  For example, the recruitment CA by default has a score of 180,000.  The default score for attacks is 100,000.  Thus, if recruiting is possible, it will always be done before attacks.  However, that does not mean that all recruiting always happens before all attacks.&lt;br /&gt;
&lt;br /&gt;
Consider the case that all castle hexes are occupied by AI units which were recruited on the previous turn and have not been used yet this turn.  No recruiting is possible, so the AI moves on to other CAs.  Once it gets to attacks, one of the units (or a group of units) might be selected to move off the castle and attack a close-by enemy.  Now a castle hex is available for recruiting, which now has the highest evaluation score and is executed.  This cycle might be repeated several times.&lt;br /&gt;
&lt;br /&gt;
In its default configuration, the RCA AI evaluates the following CAs, sorted here in order of their evaluation score:&lt;br /&gt;
&lt;br /&gt;
*'''Goto''' CA (score 200,000): Move units toward the coordinates set by [[SingleUnitWML|goto_x, goto_y]] in their unit WML.&lt;br /&gt;
&lt;br /&gt;
*'''Recruitment''' CA (score 180,000): Recruit or recall units.&lt;br /&gt;
&lt;br /&gt;
*'''Move leader to goals''' CA (score 140,000): Move the leader toward goals set by [[AiWML#List_of_AI_Aspects|[leader_goal]]].&lt;br /&gt;
&lt;br /&gt;
*'''Move leader to keep''' CA (score 120,000): Move the leader toward the closest available keep.&lt;br /&gt;
&lt;br /&gt;
*'''High XP attack''' CA (score 100,010): An addition to the default AI as of '''Wesnoth 1.13.6''', this CA performs attacks on enemy units so close to leveling that the combat CA (below) does not attack them. Unlike all the other RCA AI candidate actions, this CA is written in Lua.&lt;br /&gt;
&lt;br /&gt;
*'''Combat''' CA (score 100,000): Attack enemy units that are in range.  Note that this CA includes the move to a hex adjacent to the enemy unit as well as the attack itself.&lt;br /&gt;
&lt;br /&gt;
*'''Healing''' CA (score 80,000): Move units onto healing locations (generally villages).&lt;br /&gt;
&lt;br /&gt;
*'''Villages''' CA (score 60,000): Move units onto villages that are unowned or owned by an enemy.&lt;br /&gt;
&lt;br /&gt;
*'''Retreat''' CA (score 40,000): Evaluate if any units are in grave danger or hopelessly outnumbered, and if so, retreat them.&lt;br /&gt;
&lt;br /&gt;
*'''Move to targets''' CA (score 20,000): Evaluate potential [[RCA_AI#Use_of_Goals_and_Targets_in_the_RCA_AI|targets]] and move units toward them.  The evaluation takes into account how valuable the targets are (this is configurable by setting [[AiWML#AI_Targets_and_Goals|goals]]), how easily the AI's units can get there, how exposed to attacks they will be, etc.  Targets are enemy leaders, enemy units that pose a threat to the AI leader and villages, as well as anything defined in a [[AiWML#The_.5Bgoal.5D_Tag|[goal] tag]].  It is important to understand that targets only apply to the ''move-to-targets'' CA.  If a target is within range of the AI's unit(s) on the current turn, other CAs, such as ''combat'', ''healing'' or ''villages'' have a higher evaluation score and are (usually) executed first.  Thus, a target is specifically &amp;lt;u&amp;gt;not&amp;lt;/u&amp;gt; an attack target, but a move target.&lt;br /&gt;
&lt;br /&gt;
*'''Leader shares keep''' CA (score 10,000): Move the leader off the keep to let an allied leader recruit, if an allied leader can get to the keep in one move.  Note: This CA used to be called ''passive_leader_shares_keep''.  That name still works for the time being but will be deprecated at some point.&lt;br /&gt;
&lt;br /&gt;
As already mentioned, the CAs are always evaluated (and executed, if a possible move is found) in the order given above.  For example, the ''goto'' CA is always the first move that is done (if ''goto_x'' and ''goto_y'' are defined and a move toward that location is possible).  Equivalently, if a valid attack is found in the ''combat'' CA, it is &amp;lt;u&amp;gt;always&amp;lt;/u&amp;gt; executed before any healing, village grabbing or move-to-target moves.  One of the effects this has is that attacks always have priority over any goals/targets of the move-to-target phase.  It is '''very important''' to understand this, as it is a frequent source of confusion for scenario creators.&lt;br /&gt;
&lt;br /&gt;
Note that nothing in Wesnoth's [[Wesnoth_AI_Framework|general AI mechanism]] limits CAs to have fixed evaluations scores.  For examples, some of the [[Micro AIs]] as well as some custom AI CAs return variable scores.&lt;br /&gt;
&lt;br /&gt;
=== Use of Aspects in the RCA AI ===&lt;br /&gt;
&lt;br /&gt;
An aspect is a parameter that the AI framework provides to scenario creators for configuring the AI behavior.  For example, one can change how aggressive/careful the AI acts, how recruiting is prioritized or whether the leader participates in attacks.&lt;br /&gt;
&lt;br /&gt;
The RCA AI has a large number of aspects.  Their description is therefore given [[AiWML|its own page]].&lt;br /&gt;
&lt;br /&gt;
=== Use of Goals and Targets in the RCA AI ===&lt;br /&gt;
&lt;br /&gt;
''Goals'' define AI ''targets'' which, in turn, are used in the ''move-to-targets'' candidate action 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. A score is then assigned to each target based on this base value, as well as on the movement cost required to get there, whether moving there would put the units involved in danger, etc.&lt;br /&gt;
&lt;br /&gt;
It is possible to define additional targets or influence the relative ratings of the default targets. This is done with the [[AiWML#The_.5Bgoal.5D_Tag|[goal] tag]], in which we can set criteria selecting targets and define their base values.  These are then evaluated in the same way as default targets. Values set with the [goal] tag should always be relative to each other and to the values of the default targets (see [[RCA_AI#RCA_AI_Aspect_and_Goal_Configuration|below]] for the latter). The 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'.&lt;br /&gt;
&lt;br /&gt;
We stress again that these targets apply to the ''move-to-targets'' CA only and have no influence on other CAs. This is significant since CAs that deal with, for example, combat or village-grabbing have a higher score than the move-to-targets CA and are therefore always executed first. In practice that means that targets set with the [goal] tag only affect the AI behavior for targets that it cannot reach during the current turn, and only after combat, village-grabbing etc. are finished.&lt;br /&gt;
&lt;br /&gt;
==RCA AI Configuration==&lt;br /&gt;
&lt;br /&gt;
This section describes the configuration of the RCA AI.  An understanding of this setup is essential for [[Modifying AI Components]] or [[Creating Custom AIs]].&lt;br /&gt;
&lt;br /&gt;
In the following, we assume that the reader is familiar with the general principles of the [[Wesnoth AI Framework]].  The keys and tags used in the config snippets shown below are explained there.&lt;br /&gt;
&lt;br /&gt;
=== RCA AI Stage Configuration ===&lt;br /&gt;
&lt;br /&gt;
The RCA AI uses only the ''main_loop'' stage.  It is defined in file [https://github.com/wesnoth/wesnoth/blob/master/data/ai/ais/ai_default_rca.cfg ai/ais/ai_default_rca.cfg] in the Wesnoth data directory.  {{DevFeature1.15|3}} Note that the example below is from a previous Wesnoth version as new candidate actions have since been added to the default AI. The linked file always contains the most up-to-date configuration though and can be used as reference.&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    id=ai_default_rca&lt;br /&gt;
    description=_&amp;quot;Multiplayer_AI^Default AI (RCA)&amp;quot; # wmllint: no spellcheck&lt;br /&gt;
    [stage]&lt;br /&gt;
        id=main_loop&lt;br /&gt;
        name=ai_default_rca::candidate_action_evaluation_loop&lt;br /&gt;
        {AI_CA_GOTO}                         # id=&amp;quot;goto&amp;quot;&lt;br /&gt;
        {AI_CA_RECRUITMENT}                  # id=&amp;quot;recruitment&amp;quot;&lt;br /&gt;
        {AI_CA_MOVE_LEADER_TO_GOALS}         # id=&amp;quot;move_leader_to_goals&amp;quot;&lt;br /&gt;
        {AI_CA_MOVE_LEADER_TO_KEEP}          # id=&amp;quot;move_leader_to_keep&amp;quot;&lt;br /&gt;
        {AI_CA_HIGH_XP_ATTACK}               # id=&amp;quot;high_xp_attack&amp;quot; (Wesnoth 1.13.6 and later only)&lt;br /&gt;
        {AI_CA_COMBAT}                       # id=&amp;quot;combat&amp;quot;&lt;br /&gt;
        {AI_CA_HEALING}                      # id=&amp;quot;healing&amp;quot;&lt;br /&gt;
        {AI_CA_VILLAGES}                     # id=&amp;quot;villages&amp;quot;&lt;br /&gt;
        {AI_CA_RETREAT}                      # id=&amp;quot;retreat&amp;quot;&lt;br /&gt;
        {AI_CA_MOVE_TO_TARGETS}              # id=&amp;quot;move_to_targets&amp;quot;&lt;br /&gt;
        {AI_CA_PASSIVE_LEADER_SHARES_KEEP}   # id=&amp;quot;passive_leader_shares_keep&amp;quot;&lt;br /&gt;
     [/stage]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Note''': Before the RCA AI became the default, it was necessary to include this file (or one of its aliases) in the side definition.  That is not required any more – it is automatically included for all computer controlled sides, except when a stage is defined explicitly using the [stage] tag in the side definition.&lt;br /&gt;
&lt;br /&gt;
=== RCA AI Candidate Action Configuration ===&lt;br /&gt;
&lt;br /&gt;
In the configuration of the ''main_loop'' stage shown above, each candidate action is included as a macro.  Expanding the macros results in the following syntax (''combat'' CA example):&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[candidate_action]&lt;br /&gt;
    id=combat&lt;br /&gt;
    engine=cpp&lt;br /&gt;
    name=ai_default_rca::combat_phase&lt;br /&gt;
    max_score=100000&lt;br /&gt;
    score=100000&lt;br /&gt;
[/candidate_action]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Available Candidate Actions ====&lt;br /&gt;
&lt;br /&gt;
See file [https://github.com/wesnoth/wesnoth/blob/master/data/core/macros/ai_candidate_actions.cfg data/core/macros/ai_candidate_actions.cfg] for the definitions of all the macros, as well as for the ids, names and default scores of the available CAs.&lt;br /&gt;
&lt;br /&gt;
=== RCA AI Aspect and Goal Configuration ===&lt;br /&gt;
&lt;br /&gt;
When a game is created, '''aspects''' are initialized with the default values given at [[AiWML#List_of_AI_Aspects|AiWML]].  In the [https://github.com/wesnoth/wesnoth/blob/master/data/ai/utils/default_config.cfg configuration file], they are in ''composite'' aspect format such as&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[aspect]&lt;br /&gt;
    id=aggression&lt;br /&gt;
    engine=cpp&lt;br /&gt;
    name=composite_aspect&lt;br /&gt;
    [default]&lt;br /&gt;
        engine=cpp&lt;br /&gt;
        name=standard_aspect&lt;br /&gt;
        value=0.4&lt;br /&gt;
    [/default]&lt;br /&gt;
[/aspect]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
for ''aggression'', or&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[aspect]&lt;br /&gt;
    id=avoid&lt;br /&gt;
    engine=cpp&lt;br /&gt;
    name=composite_aspect&lt;br /&gt;
    [default]&lt;br /&gt;
        engine=cpp&lt;br /&gt;
        name=standard_aspect&lt;br /&gt;
        [value]&lt;br /&gt;
            [not]&lt;br /&gt;
            [/not]&lt;br /&gt;
        [/value]&lt;br /&gt;
    [/default]&lt;br /&gt;
[/aspect]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
for ''[avoid]''.&lt;br /&gt;
&lt;br /&gt;
The default '''goals''' are not initialized in configuration files.  Instead, they are defined in the C++ code of the ''move-to-targets'' candidate action as follows:&lt;br /&gt;
* Enemy leaders: Value is read from aspect ''leader_value'', which defaults to 3.&lt;br /&gt;
* Villages: Value is read from aspect ''village_value'', which defaults to 1.&lt;br /&gt;
* Any enemy unit that can attack the AI's leader on the enemy's next turn: The value of these targets is proportional to the threat they pose to the AI leader and generally higher than those of the other two types of default targets.&lt;br /&gt;
&lt;br /&gt;
The first two types of goals can be disabled by setting aspects ''village_value'' or ''leader_value'' to zero.  By contrast, close enemies cannot be removed from the list of targets, other than by removing the ''move-to-targets'' CA altogether.  One can, however, effectively disable them by setting other goals with vastly higher values (such as 100,000).&lt;br /&gt;
&lt;br /&gt;
==== Available Aspects and Goals ====&lt;br /&gt;
&lt;br /&gt;
See file [https://github.com/wesnoth/wesnoth/blob/master/data/ai/utils/default_config.cfg data/ai/utils/default_config.cfg] for the definitions of all the aspects and their default values.  The available goals are shown [[AiWML#AI_Targets_and_Goals|here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Modifying_AI_Components&amp;diff=65211</id>
		<title>Modifying AI Components</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Modifying_AI_Components&amp;diff=65211"/>
		<updated>2019-12-11T21:34:19Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Modifying Candidate Action of the main_loop Stage */ comment on other attack CAs&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Methods for Modifying the AI Mid Scenario ==&lt;br /&gt;
&lt;br /&gt;
The AI and its parameters are usually set up at the beginning of a scenario and often left unchanged for the entire scenario.  See [[AiWML]] and [[Creating Custom AIs]] for instructions on how to do this, or the complete list of AI resources at [[Wesnoth AI]].&lt;br /&gt;
&lt;br /&gt;
Sometimes it is, however, desirable to modify parts or all of the AI mid-scenario.  Wesnoth provides different tools to accomplish this:&lt;br /&gt;
* Standard aspects can be modified mid scenario using the [modify_side] tag&lt;br /&gt;
* Composite aspects, goals and all other AI components can be modified mid scenario using the [modify_ai] tag.&lt;br /&gt;
** A large number of helper macros are available to facilitate these tasks.&lt;br /&gt;
* Micro AIs can be modified mid scenario using the [micro_ai] tag&lt;br /&gt;
** The [micro_ai] tag is always used in an event.  There is no difference between using it at the beginning of the scenario, that is, in a ''prestart'' or ''start'' event, or in any other event.  We therefore refer to the [[Micro AIs]] page for this.&lt;br /&gt;
&lt;br /&gt;
== Using [modify_side] to Change Aspects Mid Scenario ==&lt;br /&gt;
&lt;br /&gt;
The [modify_side] tag can contain an [ai] tag for modifying aspects while a game is in progress.  This works, however, &amp;lt;u&amp;gt;only&amp;lt;/u&amp;gt; for [[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Aspects|standard aspects]].  An example is&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[modify_side]&lt;br /&gt;
    side=2&lt;br /&gt;
    [ai]&lt;br /&gt;
        aggression=0.765&lt;br /&gt;
    [/ai]&lt;br /&gt;
[/modify_side]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Aspects|Composite aspects]] and all other [[Wesnoth_AI_Framework#Types_of_AI_Components|AI components]] cannot be modified in this way.  Use the [modify_ai] tag described below for these tasks.&lt;br /&gt;
&lt;br /&gt;
Note that modifying an aspect in this fashion does not replace the [[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Aspects|facet]] used to define the aspect in the AI configuration.  It simply adds another facet that then takes precedence over the already existing ones.  In most situations, this does not make a difference, but there might be some cases (for example when facets are only defined for certain times of day or turns), when it might be necessary to replace the existing facet rather than adding a new one.  This can also be done using the [modify_ai] tag as described below.&lt;br /&gt;
&lt;br /&gt;
Note that it is, in principle, also possible to define the aspect using this syntax&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[modify_side]&lt;br /&gt;
    side=2&lt;br /&gt;
    [ai]&lt;br /&gt;
        [aspect]&lt;br /&gt;
            id=aggression&lt;br /&gt;
            [facet]&lt;br /&gt;
                value=0.765&lt;br /&gt;
            [/facet]&lt;br /&gt;
        [/aspect]&lt;br /&gt;
   [/ai]&lt;br /&gt;
[/modify_side]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
However, as this &amp;lt;u&amp;gt;only&amp;lt;/u&amp;gt; works for standard aspects which can also be set with the simpler syntax shown above, there is really no point in doing so.&lt;br /&gt;
&lt;br /&gt;
== The [modify_ai] Tag ==&lt;br /&gt;
&lt;br /&gt;
The [modify_ai] tag is a tool for modifying [[Wesnoth_AI_Framework#Stages|stages]], [[Wesnoth_AI_Framework#The_Candidate_Actions_of_the_main_loop_Stage|candidate actions]], [[Wesnoth_AI_Framework#Aspects_and_Goals|aspects and goals]] of the active AI on the fly at any time during a scenario.  Since all this functionality is packed into a single tag, its syntax is a bit more complex than that of other tags.  To facilitate this, Wesnoth also provides a large variety of [[#Modifying_AI_Components_Helper_Macros|helper macros]], but even for using those one still needs to understand how the general syntax of the [modify_ai] tag.&lt;br /&gt;
&lt;br /&gt;
Note that the [modify_ai] tag can be used both in [event] tags and in [side][ai], [era][ai] or [modification][ai] tags, meaning it can also be used to change the AI configuration at game setup time.&lt;br /&gt;
&lt;br /&gt;
=== [modify_ai] Tag Syntax ===&lt;br /&gt;
&lt;br /&gt;
The [modify_ai] tag takes the following keys:&lt;br /&gt;
&lt;br /&gt;
* '''side''': The side of the AI to be modified if used in an event.  This key is not needed if used in [side][ai].  In fact, if given there, it is simply ignored.&lt;br /&gt;
** More generally, a [[StandardSideFilter]] can be used.&lt;br /&gt;
* '''action''': (string) The action to take concerning the component.  The following values are allowed:&lt;br /&gt;
** 'add': Add a component.&lt;br /&gt;
** 'change': Change an existing component.&lt;br /&gt;
** 'delete': Delete an existing component.&lt;br /&gt;
** 'try_delete': Delete a component if it exists.  This does not produce a warning if [modify_ai] fails to change the AI.&lt;br /&gt;
* '''path''': (string) Defines the component of the AI to be modified.  See below.&lt;br /&gt;
* '''New configuration''' for the component: This depends on the type of component and is shown in the examples below.&lt;br /&gt;
&lt;br /&gt;
====Possible values for [modify_ai]path ====&lt;br /&gt;
&lt;br /&gt;
Depending on the AI component one wants to modify, the '''path''' key can take on different values. The general form of a path is a series of elements separated by dots; each element consists of a component type followed by an ID in square brackets. In many cases, the path consists of only a single level (goals or stages) or two levels (aspect/facet and stage/CA), but {{DevFeature1.13|5}} three or even four levels is occasionally possible with aspects. Some examples:&lt;br /&gt;
&lt;br /&gt;
* Select facet 'quark' of the aggression (composite) aspect: &amp;lt;code&amp;gt;aspect[aggression].facet[quark]&amp;lt;/code&amp;gt;&lt;br /&gt;
* {{DevFeature1.13|5}} Select recruit job 'bobby' of the recruitment_instructions aspect defined as a standard aspect: &amp;lt;code&amp;gt;aspect[recruitment_instructions].recruit[bobby]&amp;lt;/code&amp;gt;&lt;br /&gt;
* {{DevFeature1.13|5}} Select the default facet of the (composite) village_value aspect: &amp;lt;code&amp;gt;aspect[village_value].facet[default_facet]&amp;lt;/code&amp;gt;&lt;br /&gt;
* Select goal 'lighthouse': &amp;lt;code&amp;gt;goal[lighthouse]&amp;lt;/code&amp;gt;&lt;br /&gt;
* Select the 'main_loop' stage: &amp;lt;code&amp;gt;stage[main_loop]&amp;lt;/code&amp;gt;&lt;br /&gt;
* Select candidate action 'healer_support' of the 'main_loop' stage: &amp;lt;code&amp;gt;stage[main_loop].candidate_action[healer_support]&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Notes on the different ids:'''&lt;br /&gt;
&lt;br /&gt;
* ''aspect_id'': name of the aspect, such as 'aggression'&lt;br /&gt;
* ''CA_evaluation_loop_stage_id'': usually this will be 'main_loop', although it is in principle possible to assign a different id to this stage&lt;br /&gt;
* ''CA_id'': The id of the candidate action.  See [[RCA_AI#Available_Candidate_Actions|here]] for the ids of the default CAs.&lt;br /&gt;
* ''facet_id'', ''goal_id'' and ''stage_id'': these can take on a variety of different formats:&lt;br /&gt;
** When adding one of these components, these ids are meaningless. In fact, they do not define the actual ids and can therefore simply be left empty. Instead, the ids are either assigned automatically or can be defined in the body of the component configuration.  More on that below.&lt;br /&gt;
** By contrast, when deleting or changing a component, these ids need to be given.  The following values are possible:&lt;br /&gt;
*** A string identifying the id used when defining the component&lt;br /&gt;
*** An integer, starting at 0, defining the number of the component in the order in which they were defined&lt;br /&gt;
*** *: All components of this type&lt;br /&gt;
&lt;br /&gt;
If all this seemed a bit theoretical, don't worry.  Below, we provide many examples for how to do this in practice.&lt;br /&gt;
&lt;br /&gt;
== Modifying AI Components Helper Macros ==&lt;br /&gt;
&lt;br /&gt;
A large number of helper macros are available to simplify the WML needed for modifying AI components.  These macros are defined in file 'data/core/macros/ai.cfg', see the [http://www.wesnoth.org/macro-reference.xhtml#file:ai.cfg list] of available macros or the [https://github.com/wesnoth/wesnoth/blob/master/data/core/macros/ai.cfg full definitions].&lt;br /&gt;
&lt;br /&gt;
'''Notes on the macros:'''&lt;br /&gt;
&lt;br /&gt;
* We do not describe the individual macros here.  There are way too many of them and, quite frankly, most of them are not really needed as they barely simplify the syntax compared to the full [modify_ai] tag.  Instead, a variety of examples are given in the following sections.  If in doubt how to use a macro, or if you want to see all available macros, check out their definitions at the link above.&lt;br /&gt;
* There are several macros with ''SIMPLE_ASPECT'' in the name.  Formally, there is no such things as a ''simple'' aspect, only ''standard'' and ''composite'' aspects.  This is just a convention used by the macros for standard aspects which consist of a single scalar value.  Thus, all simple aspects (such as ''aggression'') are standard aspects, but not all standard aspects (such as ''avoid'') are simple aspects.&lt;br /&gt;
* There are several macros with ''ALWAYS_ASPECT'' in the name.  These are macros for aspects which apply at all times of day and at all turns.  However, the 'always' used as macro argument is actually the facet id, not the values of either the ''time_of_day'' or ''turns'' keys.  This is done so that the facet ids are standardized should removal of these aspects be desired later.&lt;br /&gt;
&lt;br /&gt;
== Modifying AI Component Examples ==&lt;br /&gt;
&lt;br /&gt;
=== Modifying Standard Aspects ===&lt;br /&gt;
&lt;br /&gt;
As was shown above, standard aspects, and &amp;lt;u&amp;gt;only&amp;lt;/u&amp;gt; standard aspects, can be modified using [modify_side].  A simple example is&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[modify_side]&lt;br /&gt;
    side=1&lt;br /&gt;
    [ai]&lt;br /&gt;
        aggression=0.765&lt;br /&gt;
    [/ai]&lt;br /&gt;
[/modify_side]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
This is the simplest syntax for modifying standard aspects (without using macros).  However, as we explain [[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Aspects|here]], internally &amp;lt;u&amp;gt;all&amp;lt;/u&amp;gt; aspects are set up as composite aspects.  Thus, [modify_ai] and composite aspect syntax can also be used for simple aspects and we use this example to demonstrate this:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[modify_ai]&lt;br /&gt;
    side=1&lt;br /&gt;
    action=add&lt;br /&gt;
    path=aspect[aggression].facet[]&lt;br /&gt;
    [facet]&lt;br /&gt;
        value=0.765&lt;br /&gt;
    [/facet]&lt;br /&gt;
[/modify_ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Finally, using one of the helper macros results in this:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
{MODIFY_AI_ADD_SIMPLE_ASPECT 1 aggression 0.765}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing this setting of the aggression aspect again is done as:&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[modify_ai]&lt;br /&gt;
    side=1&lt;br /&gt;
    action=delete  # or try_delete&lt;br /&gt;
    path=aspect[aggression].facet[0]&lt;br /&gt;
[/modify_ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Assuming we know that it is the first non-default facet of the ''aggression'' aspect that needs to be removed.  If, instead, we want to remove all custom definitions of aggression, we use&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[modify_ai]&lt;br /&gt;
    side=1&lt;br /&gt;
    action=delete  # or try_delete&lt;br /&gt;
    path=aspect[aggression].facet[*]&lt;br /&gt;
[/modify_ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using macros, this is done with&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
{MODIFY_AI_DELETE_ASPECT 1 aggression 0}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
{MODIFY_AI_DELETE_ASPECT 1 aggression &amp;quot;*&amp;quot;}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we don't know the number of the facet and do not want to remove all of them, we need to assign an id to the facet defining the aspect (the simple syntax of the very first example cannot be used in that case) and use that id for selective removal.  For example, the definition could look like this&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[modify_ai]&lt;br /&gt;
    side=1&lt;br /&gt;
    action=add&lt;br /&gt;
    path=aspect[aggression].facet[]&lt;br /&gt;
    [facet]&lt;br /&gt;
        id=my_custom_aggression&lt;br /&gt;
        value=0.765&lt;br /&gt;
    [/facet]&lt;br /&gt;
[/modify_ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and removal like this&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[modify_ai]&lt;br /&gt;
    side=1&lt;br /&gt;
    action=delete  # or try_delete&lt;br /&gt;
    path=aspect[aggression].facet[my_custom_aggression]&lt;br /&gt;
[/modify_ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The macro versions of these are&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
{MODIFY_AI_ADD_ASPECT 1 aggression (        &lt;br /&gt;
    [facet]&lt;br /&gt;
        id=my_custom_aggression&lt;br /&gt;
        value=0.765&lt;br /&gt;
    [/facet]&lt;br /&gt;
)}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and &lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
{MODIFY_AI_DELETE_ASPECT 1 aggression my_custom_aggression}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, we stress that a ''standard'' aspect does not need to be restricted to a single, scalar value.  For example, ''avoid'' is a standard aspect and can be defined using both types of syntax, such as&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[modify_side]&lt;br /&gt;
    side=1&lt;br /&gt;
    [ai]&lt;br /&gt;
        [avoid]&lt;br /&gt;
            x,y=20,16&lt;br /&gt;
            radius=6&lt;br /&gt;
        [/avoid]&lt;br /&gt;
    [/ai]&lt;br /&gt;
[/modify_side]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[modify_ai]&lt;br /&gt;
    side=1&lt;br /&gt;
    action=add&lt;br /&gt;
    path=aspect[avoid].facet[]&lt;br /&gt;
    [facet]&lt;br /&gt;
        [value]&lt;br /&gt;
            x,y=20,16&lt;br /&gt;
            radius=6&lt;br /&gt;
        [/value]&lt;br /&gt;
    [/facet]&lt;br /&gt;
[/modify_ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Finale note: Any aspect listed at [[AiWML]] which is not specifically pointed out to be a composite aspect (and there are very few of those), is a standard aspect.&lt;br /&gt;
&lt;br /&gt;
=== Modifying Composite Aspects ===&lt;br /&gt;
&lt;br /&gt;
A small number of the aspects listed at [[AiWML]] are available in ''composite'' aspect syntax only.  However, there is really nothing new about modifying them compared to what is shown above for ''standard'' aspects.  The only difference (as far as modifying the aspects is concerned) is that we &amp;lt;u&amp;gt;have to&amp;lt;/u&amp;gt; use composite aspect syntax, standard aspect syntax does not work.  Thus, we only show one example of the [[AiWML#Filtering_Combat_with_the_attacks_Aspect|''attacks'' aspect]] here.&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[modify_ai]&lt;br /&gt;
    side=2&lt;br /&gt;
    action=add&lt;br /&gt;
    path=aspect[attacks].facet[]&lt;br /&gt;
    [facet]&lt;br /&gt;
        invalidate_on_gamestate_change=yes&lt;br /&gt;
        [filter_own]&lt;br /&gt;
            type=Elvish Sorceress,Elvish Enchantress,Elvish Sylph&lt;br /&gt;
        [/filter_own]&lt;br /&gt;
        [filter_enemy]&lt;br /&gt;
            race=undead&lt;br /&gt;
        [/filter_enemy]&lt;br /&gt;
    [/facet]&lt;br /&gt;
[/modify_ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A Few More Notes on Modifying Aspects === &lt;br /&gt;
&lt;br /&gt;
* The aspect itself and its [[Wesnoth_AI_Framework#The_.5Bai.5D_Tag_.E2.80.94_Aspects|[default]]] facet cannot be deleted, only the custom facets. {{DevFeature1.13|5}} However, they can be changed, replacing them with a new definition. (The id attribute must match when changing an aspect.) This could change a standard aspect to a composite aspect or vice versa.&lt;br /&gt;
* For standard aspects, it is generally not necessary to delete custom facets.  One can simply overwrite the current value with a new one.  The only times when one might have to remove a standard aspect is when it is defined for specific times of day or turns, or when it is defined so many times that it might bloat the AI configuration (and therefore the savefile).&lt;br /&gt;
* We can also use &amp;lt;code&amp;gt;action=change&amp;lt;/code&amp;gt; to delete an existing facet and overwrite it with a new definition.  If &amp;lt;code&amp;gt;*&amp;lt;/code&amp;gt; is used as ''facet_id'', this deletes all existing facets of this aspect and replaces them with a single facet with the new definition.&lt;br /&gt;
* Adding a facet with the same ''id'' as an existing facet overwrites the previous occurrence, making this equivalent to changing that facet.&lt;br /&gt;
* If in doubt about the exact syntax of a given aspect, one can always open the gamestate inspector (by typing &amp;lt;code&amp;gt;:inspect&amp;lt;/code&amp;gt; in-game in debug mode) and check out the [default] tag of the respective aspect under 'ai config full' for a given side (team).  Note though that it might be necessary to play through at least one partial turn of that side, as the AI config of the side is not necessarily fully initialized until that happens.&lt;br /&gt;
&lt;br /&gt;
=== Modifying Goals ===&lt;br /&gt;
&lt;br /&gt;
Goals can be added and removed using a syntax that is very similar to that for composite aspects&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[modify_ai]&lt;br /&gt;
    side=1&lt;br /&gt;
    action=add&lt;br /&gt;
    path=goal[]&lt;br /&gt;
    [goal]&lt;br /&gt;
        id=my_custom_goal&lt;br /&gt;
        [criteria]&lt;br /&gt;
            side=3&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
[/modify_ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[modify_ai]&lt;br /&gt;
    side=1&lt;br /&gt;
    action=delete&lt;br /&gt;
    path=goal[my_custom_goal]&lt;br /&gt;
[/modify_ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Note that we assigned a custom id to the goal here, that was then used for removal.  We could also use the method described above for aspects, using the number of the goal or &amp;lt;code&amp;gt;*&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The macro versions of these examples are&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
{MODIFY_AI_ADD_GOAL 1 (&lt;br /&gt;
    [goal]&lt;br /&gt;
        id=my_custom_goal&lt;br /&gt;
        [criteria]&lt;br /&gt;
            side=3&lt;br /&gt;
        [/criteria]&lt;br /&gt;
        value=5&lt;br /&gt;
    [/goal]&lt;br /&gt;
)}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
{MODIFY_AI_DELETE_GOAL 1 my_custom_goal}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Modifying Candidate Action of the ''main_loop'' Stage ===&lt;br /&gt;
&lt;br /&gt;
The syntax for modifying candidate actions is entirely equivalent to that for aspects and goals shown above. If one wants to, for example, set up an AI that does not fight, this can be done by removing the ''combat'' CA.  {{DevFeature1.15|3}} There are now additional CAs which also perform attacks. Check out [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|the candidate actions of the default AI]] to see which CAs need to be remove in order to produce an AI that truly does not attack.&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[modify_ai]&lt;br /&gt;
    side=2&lt;br /&gt;
    action=delete&lt;br /&gt;
    path=stage[main_loop].candidate_action[combat]&lt;br /&gt;
[/modify_ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
{MODIFY_AI_DELETE_CANDIDATE_ACTION 2 main_loop combat}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Adding a [[Creating_Custom_AIs#Creating_Custom_Candidate_Actions|custom Lua candidate action]] is done as follows&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[modify_ai]&lt;br /&gt;
    side=1&lt;br /&gt;
    action=add&lt;br /&gt;
    path=stage[main_loop].candidate_action[]&lt;br /&gt;
    [candidate_action]&lt;br /&gt;
        engine=lua&lt;br /&gt;
        name=return_guardian_bob&lt;br /&gt;
        id=return_guardian_bob&lt;br /&gt;
        max_score=100010&lt;br /&gt;
        location=&amp;quot;~add-ons/my_addon/lua/return_guardian.lua&amp;quot;&lt;br /&gt;
        eval_parms=&amp;quot;id = 'Bob', return_x = 10, return_y = 15&amp;quot;&lt;br /&gt;
        exec_parms=&amp;quot;id = 'Bob', return_x = 10, return_y = 15&amp;quot;&lt;br /&gt;
    [/candidate_action]&lt;br /&gt;
[/modify_ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We'll omit the macro version here as it is almost identical to previous examples.&lt;br /&gt;
&lt;br /&gt;
=== Modifying Stages ===&lt;br /&gt;
&lt;br /&gt;
Working with a single stage, the [[Wesnoth_AI_Framework#Stages|''main_loop'' stage]], should be sufficient for essentially all Wesnoth AI projects these days.  It should therefore rarely ever be necessary to add or remove stages any more.  It can, however, be done in the same way as for the other AI components.  The macro definitions linked to above can be used as syntax reference if you ever need to do so.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Creating_Custom_AIs&amp;diff=65210</id>
		<title>Creating Custom AIs</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Creating_Custom_AIs&amp;diff=65210"/>
		<updated>2019-12-11T21:29:11Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Adding the main_loop Stage from Scratch */ add comment about new CAs added to default AI&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Too Many Methods for Creating Custom AIs ==&lt;br /&gt;
&lt;br /&gt;
The methods available for creating custom AIs have been developed over [[Wesnoth_AI#A_Very_Brief_History_of_Wesnoth_AIs|many development cycles]].  As a result, it is now much easier to create custom AIs than it used to be.  The down side is that there are now a large number of different methods to do more or less the same thing, which is unnecessary and, quite frankly, confusing.  Also, while most of these methods still work for backward compatibility reasons, many of them are not maintained any more.&lt;br /&gt;
&lt;br /&gt;
This page therefore only describes one method of creating custom AIs, custom Lua candidate actions.  Links to legacy documentation of some of the old methods are still included at the bottom of this page, but just as the methods themselves, these wiki pages are not maintained and updated any more.  We recommend not using them any more and, ideally, update old code to the new methods.&lt;br /&gt;
&lt;br /&gt;
'''Note:''' The current type of custom Lua candidate actions was originally called ''external'' Lua CAs, to distinguish them from the original method (now the ''old'' method) of setting up Lua CAs.  However, as external Lua CAs are now the standard for creating custom AIs, we drop the 'external' in the description.  It is mentioned here only in case you come across the term somewhere in older documentation.&lt;br /&gt;
&lt;br /&gt;
== Different Types of AI Modifications ==&lt;br /&gt;
&lt;br /&gt;
Modifications of the AI behavior can be accomplished in a variety of different ways:&lt;br /&gt;
* Adding new candidate actions&lt;br /&gt;
** This is the main content of this page.&lt;br /&gt;
* Changing the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|existing candidate actions]]:&lt;br /&gt;
** Modifying the evaluation scores of the candidate actions&lt;br /&gt;
** Deleting candidate actions&lt;br /&gt;
** Instructions for these tasks are also given here.&lt;br /&gt;
* Configuring the parameters/weights which determine how the [[RCA_AI|default AI]] evaluates its moves.&lt;br /&gt;
** This can be done easily in WML by [[AiWML|setting aspects and goals]].&lt;br /&gt;
** It is also possible to [[LuaAI#Dynamic_Lua_Aspects|assign dynamic aspects]] using Lua.&lt;br /&gt;
** Technically this is not really creating custom AIs, but rather customizing the existing AI.  It is not described on this page.  See the links provided instead.&lt;br /&gt;
&lt;br /&gt;
== Setting Up a Custom AI ==&lt;br /&gt;
&lt;br /&gt;
=== Creating Custom Candidate Actions ===&lt;br /&gt;
&lt;br /&gt;
The recommended way of creating a custom AI is to add Lua candidate actions (CAs) to the ''main_loop'' stage of the [[RCA_AI|default AI]].  Note that, in the following, we assume a solid knowledge of how the [[Wesnoth_AI_Framework#Stages|evaluation loop of this stage]] works, [[Wesnoth_AI_Framework#The_Candidate_Actions_of_the_main_loop_Stage|what candidate actions are in general]] and how they [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|are used in the default AI]], as well as some basic knowledge of [[LuaWML|Lua in Wesnoth]] and [[LuaAI|Lua AI]].&lt;br /&gt;
&lt;br /&gt;
We demonstrate how this is done on the example of the [[Micro_AIs#Return_Guardian_.28ai_type.3Dreturn_guardian.29|Return Guardian Micro AI]].  This is a very simple AI that is a variation of the default Wesnoth guardian.  It has a defined guard position and only two states:&lt;br /&gt;
* When the guardian is not on the guard position, it tries to return there.&lt;br /&gt;
* When it is on the guard position, it is left free to attack enemy units that are within range, but may not do any other moves.&lt;br /&gt;
Note that some minor changes have been made to the code of the actual Return Guardian Micro AI for ease of presentation.&lt;br /&gt;
&lt;br /&gt;
The code for the AI is as follows (we assume that this is in a file called 'my_addon/lua/return_guardian.lua'):&lt;br /&gt;
&amp;lt;div style=&amp;quot;height: 250px; overflow:auto&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang='lua' line highlight='13,15,28,47'&amp;gt;&lt;br /&gt;
local AH = wesnoth.require &amp;quot;ai/lua/ai_helper.lua&amp;quot;&lt;br /&gt;
&lt;br /&gt;
local function get_guardian(cfg)&lt;br /&gt;
    local filter = cfg.filter or { id = cfg.id }&lt;br /&gt;
    local guardian = AH.get_units_with_moves {&lt;br /&gt;
        side = wesnoth.current.side,&lt;br /&gt;
        { &amp;quot;and&amp;quot;, filter }&lt;br /&gt;
    }[1]&lt;br /&gt;
&lt;br /&gt;
    return guardian&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local ca_return_guardian = {}&lt;br /&gt;
&lt;br /&gt;
function ca_return_guardian:evaluation(ai, cfg)&lt;br /&gt;
    local guardian = get_guardian(cfg)&lt;br /&gt;
    if guardian then&lt;br /&gt;
        if (guardian.x == cfg.return_x) and (guardian.y == cfg.return_y) then&lt;br /&gt;
            return 99990&lt;br /&gt;
        else&lt;br /&gt;
            return 100010&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    return 0&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function ca_return_guardian:execution(ai, cfg)&lt;br /&gt;
    local guardian = get_guardian(cfg)&lt;br /&gt;
&lt;br /&gt;
    -- In case the return hex is occupied:&lt;br /&gt;
    local x, y = cfg.return_x, cfg.return_y&lt;br /&gt;
    if (guardian.x ~= x) or (guardian.y ~= y) then&lt;br /&gt;
        x, y = wesnoth.find_vacant_tile(x, y, guardian)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    local next_hop = AH.next_hop(guardian, x, y)&lt;br /&gt;
    if (not next_hop) then next_hop = { guardian.x, guardian.y } end&lt;br /&gt;
&lt;br /&gt;
   if ((next_hop[1] ~= guardian.x) or (next_hop[2] ~= guardian.y)) then&lt;br /&gt;
       ai.move_full(guardian, next_hop[1], next_hop[2])&lt;br /&gt;
   else&lt;br /&gt;
       ai.stopunit_moves(guardian)&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return ca_return_guardian&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first 12 lines are not directly relevant to setting up the AI, they simply define some utility functions.  The first important line is L.13 in combination with lines 15, 28 and 47.  A Lua candidate action needs to return a table with an evaluation and an execution function.  The mentioned lines set up that table and the two functions.  Its name is chosen to be ''ca_return_guardian'', but that is really irrelevant as it is a variable local to the file.&lt;br /&gt;
&lt;br /&gt;
The execution and evaluation function can have several arguments. The available arguments and the order in which they appear '''depend on the Wesnoth version and usage'''.&lt;br /&gt;
&lt;br /&gt;
====Before Version 1.13.5====&lt;br /&gt;
*'''ai''': The [[LuaAI#Lua_AI_Functions|Lua AI function table]].  The execution function always needs this argument to be passed, the evaluation function only in some situations.  See below for details.&lt;br /&gt;
*'''cfg''': A comma-separated list of configuration parameters, which are passed from the [candidate_action] tag using the ''eval_parms'' and ''exec_parms'' keys.  These need to be in [[LuaWML|Lua table format]], but without the outer curly brackets &amp;lt;code&amp;gt;{ }&amp;lt;/code&amp;gt;.  See below for examples.&lt;br /&gt;
*'''self''': If the persistent variable ''data'' is needed, the evaluation/execution function definitions need to include a third parameter ''self''.  The persistent variable is then accessible as ''self.data''.  See below for more information on the ''data'' variable.&lt;br /&gt;
&lt;br /&gt;
These arguments &amp;lt;u&amp;gt;must&amp;lt;/u&amp;gt; appear in order &amp;lt;code&amp;gt;(ai, cfg, self)&amp;lt;/code&amp;gt; in the function calls, but as always in Lua, trailing unused arguments can be omitted.&lt;br /&gt;
&lt;br /&gt;
====Starting from Version 1.13.5====&lt;br /&gt;
The arguments passed to the functions are different for candidate actions using the recommended [args] subtag rather than eval_parms and exec_parms (these are explained in more detail below). The new arguments are as follows:&lt;br /&gt;
*'''self''': The engine data. By default, this replicates the ''self'' parameter from the old system for compatibility, meaning that ''self.data'' is the same as ''data''. If an [engine] tag was explicitly defined for Lua, however, this parameter is the value that the engine's code returned, which could be pretty much anything.&lt;br /&gt;
*'''cfg''': The contents of the [args] subtag; parameters specific to this candidate action. With this, you can use the same code with different parameters to produce two different candidate actions.&lt;br /&gt;
*'''data''': The persistent variable ''data''. This is the same as ''self.data'' in the old system. If an [engine] tag was explicitly defined for the Lua engine, this is initially equal to the value of the [data] subtag. However, unlike the other two parameters, this value is mutable.&lt;br /&gt;
*'''filter_own''': {{DevFeature1.15|3}} The content of the &amp;lt;code&amp;gt;[candidate action][filter_own]&amp;lt;/code&amp;gt; tag as described [[Wesnoth_AI_Framework#The_.5Bcandidate_action.5D_Tag|here]].&lt;br /&gt;
&lt;br /&gt;
If the functions are defined using '''colon notation''' as in the above example, then the ''self'' parameter is implicitly defined and the arguments must be in order &amp;lt;code&amp;gt;(ai, cfg, filter_own)&amp;lt;/code&amp;gt; in the function calls. If you prefer to explicitly define it, use '''a dot instead of a colon''' when defining the function, in which case the arguments must be passed as &amp;lt;code&amp;gt;(self, ai, cfg, filter_own)&amp;lt;/code&amp;gt;. As always in Lua, you can omit any of the arguments if the function does not need to access the respective information.&lt;br /&gt;
&lt;br /&gt;
Notice that the ''ai'' table is no longer included among the arguments passed to the table. This is because it's now available as a &amp;quot;scoped global variable&amp;quot; anywhere in AI code, so there is no need to pass it around.&lt;br /&gt;
&lt;br /&gt;
====Evaluation Function====&lt;br /&gt;
&lt;br /&gt;
In our example, the call to the evaluation contains the first two of these arguments, the ''ai'' table and the configuration table ''cfg''.  The ''ai'' table is not needed for this particular evaluation function.  It is there only as a placeholder because the ''cfg'' table is needed and always needs to be passed as the second argument.&lt;br /&gt;
&lt;br /&gt;
The ''cfg'' table contains information about the guardian unit and the guard position.  We will show how this is passed to the evaluation function later.  For now, we only need to know that it contains either a ''filter'' or ''id'' key, used in L.16 (call to function ''get_guardian'' defined in L.4) to check whether there is a guardian unit on the map, and keys ''return_x'' and ''return_y'' defining the guard position.&lt;br /&gt;
&lt;br /&gt;
A CA evaluation function returns a numerical score which determines when in the ''main_loop'' stage evaluation loop it is executed.  The return guardian code evaluation has three possible return values.  First (or rather: last), if no guardian unit is found it returns 0 (L.25), which means nothing is to be done for this CA.&lt;br /&gt;
&lt;br /&gt;
If a guardian unit is found, there are two possible scores.  The code first checks whether the guardian is at its guard position (L.18).  If so, the return score is 99,990, if not, it is 100,010.  The relevance of these values is that they are just below and just above the score of the ''combat'' (attack) CA of the [[RCA_AI|default AI]].  Thus, if the guardian is not at its return position, the return guardian CA has a higher score than any attacks done by the default AI, and the unit is returned to its guard position whether there is an enemy unit in range or not.&lt;br /&gt;
&lt;br /&gt;
If, on the other hand, the guardian is already at its guard position, the return score is lower than that of the default ''combat'' CA.  The default AI is thus free to use the unit in an attack.&lt;br /&gt;
&lt;br /&gt;
It is important to notice that the function may not return 0 when the guardian is at its guard position.  Imagine the situation that the guardian is at its position and has no unit to attack.  If the return score were 0 in this case, CAs with score lower than the combat CA would take over.  In particular, the ''move-to-targets'' CA would kick in and move the guardian toward one of the default AI [[RCA_AI#RCA_AI_Aspect_and_Goal_Configuration|goals]].  This could be done by removing these other CAs, but then it would not be available for other, non-guardian units of the AI side either.  A better way to deal with this is to remove the movement of the guardian unit if it is at its guard position and no attack happened.  This is done by using a return score of 99,990 in this situation, in combination with what the execution function does, as described in the following.&lt;br /&gt;
&lt;br /&gt;
====Execution Function====&lt;br /&gt;
&lt;br /&gt;
The execution function is called with the same two arguments as the evaluation function.  The ''cfg'' table is needed again in order to find the guardian (L.29) and the position to move to.  The position is either the return hex (L.32) or, in case it is occupied by a unit other than the guardian itself, a vacant hex close to it (L.34).  Lines 37 and 38 then find the closest hex the unit can move to in practice, using the [https://github.com/wesnoth/wesnoth/blob/master/data/ai/lua/ai_helper.lua ''ai_helper.lua''] utility function [https://github.com/wesnoth/wesnoth/blob/master/data/ai/lua/ai_helper.lua#L910 ''next_hop()''].&lt;br /&gt;
&lt;br /&gt;
Lines 40–44 contain the actual execution of the AI move.  If the guardian is not at the ''next_hop'' position, it will be moved there and movement will be set to zero; that's what ''ai.move_full()'' does, as opposed to ''ai.move()''.  If, on the other hand, the ''next_hop'' position is its current position (which means that it is either already at the guard position or cannot get any closer), movement is removed using ''ai.stopunit_moves()'', so that the other CAs of the default AI do not move it away afterward.  Note that, in the latter case, we do not remove attacks, so that the default AI is still free to attack if the guardian ends up next to an enemy unit.&lt;br /&gt;
&lt;br /&gt;
This is it for the AI code itself.  We can see that it is possible to set up reasonable versatile AI behavior with just a few dozen lines of code by working with and around the behavior of the default AI.  In the following, we show how to add this code to the default AI.  But first:&lt;br /&gt;
&lt;br /&gt;
'''A few more notes:'''&lt;br /&gt;
&lt;br /&gt;
* It is '''very important''' that CA evaluation functions do not change the game state as that leads to out of sync (OOS) errors.&lt;br /&gt;
* Also '''very important''': If a candidate action is selected for execution but fails to change the game state (e.g. by trying to do an illegal move/attack/recruit), then it will be blacklisted until the end of turn. This is done to prevent infinite loops and the blacklisting is handled automatically by the engine.  This means, however, that you need to be make sure that the execution of a move is possible, otherwise all subsequent moves which the CA would have done during this turn will not happen.  This sort of check can, for example, be done with the ''ai.check*()'' Lua AI functions.&lt;br /&gt;
** For a description of these functions, and all other functions of the ''ai'' table, see [[LuaAI#Lua_AI_Functions|here]].&lt;br /&gt;
* The table returned by the code  (''ca_return_guardian'' in our example) may also contain other functions.  For example, the ''get_guardian()'' function could be included in the table rather than by closure as we did it here.  It does not matter what other elements the table contains, as long as the evaluation and execution functions exist.&lt;br /&gt;
* For many more examples of other candidate actions, check out the [https://github.com/wesnoth/wesnoth/tree/master/data/ai/micro_ais/cas Micro AI CAs]] in the Wesnoth repository.&lt;br /&gt;
&lt;br /&gt;
=== Including Custom Candidate Actions in Scenarios ===&lt;br /&gt;
&lt;br /&gt;
The last step needed is adding this CA code into the AI configuration.  The following instruction are for adding the AI to a scenario (this works in both single and multi player) at scenario setup time.  Custom AIs can also be added to [[#Custom_AIs_in_Eras_and_Modifications|eras and modifications]] or [[#Mid-scenario_Candidate_Action_Changes|while a game is in progress]] in very similar ways.&lt;br /&gt;
&lt;br /&gt;
Let us assume that the code above is saved in a file called ''my_addon/lua/return_guardian.lua'' and that we want to activate it for a unit with id 'Bob' of side 1 with guard position (10,15).  The easiest way of inserting the candidate action is by using the MODIFY_AI_ADD_CANDIDATE_ACTION macro.&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    {MODIFY_AI_ADD_CANDIDATE_ACTION 1 main_loop (&lt;br /&gt;
        [candidate_action]&lt;br /&gt;
            engine=lua&lt;br /&gt;
            name=return_guardian_bob&lt;br /&gt;
            id=return_guardian_bob&lt;br /&gt;
            max_score=100010&lt;br /&gt;
            location=&amp;quot;~add-ons/my_addon/lua/return_guardian.lua&amp;quot;&lt;br /&gt;
            [args]&lt;br /&gt;
                id=Bob&lt;br /&gt;
                return_x=10&lt;br /&gt;
                return_y=15&lt;br /&gt;
            [/args]&lt;br /&gt;
            # In the older syntax, the above would have looked like this:&lt;br /&gt;
            # eval_parms=&amp;quot;id = 'Bob', return_x = 10, return_y = 15&amp;quot;&lt;br /&gt;
            # exec_parms=&amp;quot;id = 'Bob', return_x = 10, return_y = 15&amp;quot;&lt;br /&gt;
        [/candidate_action]&lt;br /&gt;
    )}&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
This needs to be placed in the [side] tag of side 1.  Of course, you can also use the [modfiy_ai] tag directly without going through the macro.  See [[Modifying_AI_Components#Modifying_Candidate_Action_of_the_main_loop_Stage|here]] for the syntax.&lt;br /&gt;
&lt;br /&gt;
Explanation of the keys:&lt;br /&gt;
*'''engine''': The engine to be used for this CA.  Possible values are 'cpp', 'lua' and 'fai', but for a Lua AI CA it always has to be 'lua'.&lt;br /&gt;
*'''id''' and '''name''': These are, in principle, optional.  However, they are generally useful to assign for clarity.  Furthermore, if you want to remove or change a candidate action later, you need to know its id.&lt;br /&gt;
*'''max_score''': The maximum score the CA evaluation function might return.  This parameter is also optional, but it is useful to reduce the evaluation time of the ''main_loop'' stage, which only evaluates CAs with max_score larger than those of the currently highest-scoring CA found.  See [[Wesnoth_AI_Framework#The_.5Bcandidate_action.5D_Tag|here]] for a little more on that.&lt;br /&gt;
*'''location''': File name, including path, of the AI code for the CA&lt;br /&gt;
*'''eval_parms''', '''exec_parms''': (string) The parameters to be passed to the evaluation and execution functions of the AI.  These need to be strings in form of a Lua table, without the outer curly brackets.  They are passed to the functions as the second argument, called ''cfg'' in the example above.&lt;br /&gt;
** The strings do not necessarily need to be in [[LuaWML#Encoding_WML_objects_into_Lua_tables|WML object Lua table]] format.  However, this is, of course, necessary if they are to be used in functions that require that format, as in the ''filter'' example shown below.&lt;br /&gt;
** {{DevFeature1.13|5}} These string parameters are now replaced by the [args] WML tag. For the time being, the string parameters will continue to work, but they will be removed entirely in the next development cycle.&lt;br /&gt;
*'''[args]''': {{DevFeature1.13|5}} WML data to be passed to both the evaluation and execution functions. A copy of the data is passed, so even if the functions modify it, it will always be the same on the next call. This replaces eval_parms and exec_parms.&lt;br /&gt;
&lt;br /&gt;
The example above requires&lt;br /&gt;
 cfg = { id = 'Bob', return_x = 10, return_y = 15 }&lt;br /&gt;
to be passed to the evaluation and execution functions.  This is used in the [candidate_action] tag code snippet shown above — without the out &amp;lt;code&amp;gt;{ }&amp;lt;/code&amp;gt;, as mentioned.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if you want to pass, for example, a [filter] tag that includes the unit id and type, it would look like this&lt;br /&gt;
 cfg={ { filter, { id = 'Bob', type = 'Assassin' } }, return_x = 10, return_y = 15&amp;quot; }&lt;br /&gt;
resulting in the following parameter string definition&lt;br /&gt;
 eval_parms=&amp;quot;{ filter, { id = 'Bob', type = 'Assassin' } }, return_x = 10, return_y = 15&amp;quot;&lt;br /&gt;
Again, identical except for the outer curly brackets, and repeated for clarity.&lt;br /&gt;
&lt;br /&gt;
That's most of what one needs to know for the creation of custom AIs.  Some additional information on variations of using custom CAs as well as other AI modifications is given in the following.  We also list some of the other methods available for adding custom AIs at the bottom of this page.  As we point out above, however, we do not recommend using those any more.  Neither the code itself nor the wiki pages are maintained any more at this time and, in an attempt to simplify and standardize the AI code, they might be removed at some point in the future.&lt;br /&gt;
&lt;br /&gt;
=== The Persistent ''data'' Variable ===&lt;br /&gt;
&lt;br /&gt;
It is sometimes necessary to transfer information between the evaluation and execution functions of the same CA or between CAs. It might also be necessary to have this information persist from turn to turn and across save/load cycles.  The Wesnoth AI therefore adds a Lua table called ''data'' which is added to the third argument, ''self'', of the evaluation and execution function declarations described above.&lt;br /&gt;
&lt;br /&gt;
Some more information on its implementation in Lua AI is given [[LuaAI#Lua_AI_Variables_.E2.80.94_Persistence|here]].  For most practical intents and purposes it is sufficient to know that you can write at will to this table and read the information again from other functions and/or at other turns.  The only things to keep in mind are:&lt;br /&gt;
* The functions requiring access to the table need to have the third parameter specified in their declaration.  If it is called ''self'', the table is accessible as ''self.data''.&lt;br /&gt;
* If you want content to persist over save/load cycles, all content of ''data'' needs to be in [[LuaWML#Encoding_WML_objects_into_Lua_tables|WML table format]].  Other format content can also be used during an AI turn, for example to pass it between the evaluation and execution functions of the same CA, but it is lost when saving the game.&lt;br /&gt;
* The CAs of the [[Micro AIs]] make extensive use of the ''data'' variable.  You can check out [https://github.com/wesnoth/wesnoth/tree/master/data/ai/micro_ais/cas their code] for examples.&lt;br /&gt;
&lt;br /&gt;
=== Custom AIs in Eras and Modifications ===&lt;br /&gt;
&lt;br /&gt;
Custom AIs can also be included in [[EraWML|eras]] and [[ModificationWML|modifications]] for use with any MP map that allows them.  The setup is entirely equivalent to that for scenarios, with two very minor differences:&lt;br /&gt;
* The MODIFY_AI_ADD_CANDIDATE_ACTION macro needs to be placed inside [era][ai] or [modification][ai] instead of in [side][ai].&lt;br /&gt;
* The '''description''' attribute needs to be set inside the [ai] tag.  This is the text which will show up in the computer player selection menu during the MP game setup.&lt;br /&gt;
* {{DevFeature1.13|5}} Scenarios wishing to use such AIs may reference them using the '''ai_algorithm''' key in [side][ai], and modify them further with additional AI component tags such as aspect values.&lt;br /&gt;
&lt;br /&gt;
As an example, the following code adds the CA described above to a modification.&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[modification]&lt;br /&gt;
    id=guardian_bob&lt;br /&gt;
    name= _ &amp;quot;Guardian Bob AI&amp;quot;&lt;br /&gt;
    description= _ &amp;quot;Adds code for Guardian Bob to the default AI&amp;quot;&lt;br /&gt;
    [ai]&lt;br /&gt;
        description=_&amp;quot;Multiplayer_AI^Default AI + Guardian Bob&amp;quot;&lt;br /&gt;
        {MODIFY_AI_ADD_CANDIDATE_ACTION 1 main_loop (&lt;br /&gt;
            [candidate_action]&lt;br /&gt;
                engine=lua&lt;br /&gt;
                name=return_guardian_bob&lt;br /&gt;
                id=return_guardian_bob&lt;br /&gt;
                max_score=100010&lt;br /&gt;
                location=&amp;quot;~add-ons/my_addon/lua/return_guardian.lua&amp;quot;&lt;br /&gt;
                eval_parms=&amp;quot;id = 'Bob', return_x = 10, return_y = 15&amp;quot;&lt;br /&gt;
                exec_parms=&amp;quot;id = 'Bob', return_x = 10, return_y = 15&amp;quot;&lt;br /&gt;
            [/candidate_action]&lt;br /&gt;
        )}&lt;br /&gt;
    [/ai]&lt;br /&gt;
[/modification]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Mid-scenario Candidate Action Changes ===&lt;br /&gt;
&lt;br /&gt;
The instructions given above are written for setting up custom candidate actions at the beginning of a game.  However, the [modify_ai] tag and the macros using it can be used at any time during a scenario in an [event] tag. It is entirely equivalent to the methods described above.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* In this case, the [modify_ai] tag is placed '''directly inside the event''', without requiring an [ai] tag.&lt;br /&gt;
* See  [[Modifying_AI_Components#The_.5Bmodify_ai.5D_Tag|here]] for details and examples.&lt;br /&gt;
&lt;br /&gt;
== Additional Information for Creating Custom AIs ==&lt;br /&gt;
&lt;br /&gt;
=== Adding the ''main_loop'' Stage from Scratch ===&lt;br /&gt;
&lt;br /&gt;
Sometimes it might be useful or convenient to add the entire ''main_loop'' stage from scratch, rather than adding or deleting individual candidate actions.  If that is desired, the syntax for the example of the default AI plus Guardian Bob from above looks as shown below.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} The example below is from a previous Wesnoth version. New candidate actions have since been added to the default AI. The latest up-to-date version can always be found in file [https://github.com/wesnoth/wesnoth/blob/master/data/ai/ais/ai_default_rca.cfg ai/ais/ai_default_rca.cfg] in the Wesnoth data directory&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[ai]&lt;br /&gt;
    description=_&amp;quot;Multiplayer_AI^Default AI + Guardian Bob&amp;quot;&lt;br /&gt;
    version=10710&lt;br /&gt;
    [stage]&lt;br /&gt;
        id=main_loop&lt;br /&gt;
        name=ai_default_rca::candidate_action_evaluation_loop&lt;br /&gt;
        {AI_CA_GOTO}&lt;br /&gt;
        {AI_CA_RECRUITMENT}&lt;br /&gt;
        {AI_CA_MOVE_LEADER_TO_GOALS}&lt;br /&gt;
        {AI_CA_MOVE_LEADER_TO_KEEP}&lt;br /&gt;
        {AI_CA_HIGH_XP_ATTACK} # Wesnoth 1.13.6 and later only&lt;br /&gt;
        {AI_CA_COMBAT}&lt;br /&gt;
        {AI_CA_HEALING}&lt;br /&gt;
        {AI_CA_VILLAGES}&lt;br /&gt;
        {AI_CA_RETREAT}&lt;br /&gt;
        {AI_CA_MOVE_TO_TARGETS}&lt;br /&gt;
        {AI_CA_LEADER_SHARES_KEEP}&lt;br /&gt;
        [candidate_action]&lt;br /&gt;
            engine=lua&lt;br /&gt;
            name=return_guardian_bob&lt;br /&gt;
            id=return_guardian_bob&lt;br /&gt;
            max_score=100010&lt;br /&gt;
            location=&amp;quot;~add-ons/my_addon/lua/return_guardian.lua&amp;quot;&lt;br /&gt;
            eval_parms=&amp;quot;id = 'Bob', return_x = 10, return_y = 15&amp;quot;&lt;br /&gt;
            exec_parms=&amp;quot;id = 'Bob', return_x = 10, return_y = 15&amp;quot;&lt;br /&gt;
        [/candidate_action]&lt;br /&gt;
    [/stage]&lt;br /&gt;
[/ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
This can be included in [side], [era] or [modification] tags.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* The '''description''' attribute is ignored if this is included in a [side] tag.  It can be omitted in that case.&lt;br /&gt;
* It used to be necessary to include even the unmodified [[RCA AI]] for computer controlled sides.  Usually this was done by including a file with the AI configuration, for example by adding a line such as&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
 {ai/ais/ai_default_rca.cfg}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
to the [side] tag.  While it does no harm to still do so, it is not needed any more.  If no other stage is defined, the default AI is automatically included for all computer controlled sides.&lt;br /&gt;
&lt;br /&gt;
=== Modifying Default AI Candidate Actions===&lt;br /&gt;
&lt;br /&gt;
In addition to customizing the default AI behavior using [[AiWML|aspects and goals]], the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|default candidate actions]] can be deleted selectively and their scores can be changed.&lt;br /&gt;
&lt;br /&gt;
==== Deleting Default AI Candidate Actions ====&lt;br /&gt;
&lt;br /&gt;
Just as all the previous tasks described on this page, this can also be done with the [[Modifying_AI_Components#The_.5Bmodify_ai.5D_Tag|[modify_ai]]] tag.  For example, if we wanted to create an AI that does not attack, we can remove the ''combat'' CA.&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[modify_ai]&lt;br /&gt;
    side=2&lt;br /&gt;
    action=delete&lt;br /&gt;
    path=stage[main_loop].candidate_action[combat]&lt;br /&gt;
[/modify_ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Changing the Score of a Default AI Candidate Action ====&lt;br /&gt;
&lt;br /&gt;
The return score is pretty much the only thing that can be changed about a default AI CA (other than the customizing with aspects and goals described at the link above).  This can be done using &amp;lt;code&amp;gt;action=change&amp;lt;/code&amp;gt; in the [[Modifying_AI_Components#The_.5Bmodify_ai.5D_Tag|[modify_ai]]] tag.  For example, if we want to have an AI that attacks only after the ''healing'', ''villages'' and ''retreat'' CAs (with default scores 80,000, 60,000 and 40,000) have finished, we can change the ''combat'' CA to have a score of 30,000.&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[modify_ai]&lt;br /&gt;
    side=1&lt;br /&gt;
    action=change&lt;br /&gt;
    path=stage[main_loop].candidate_action[combat]&lt;br /&gt;
    [candidate_action]&lt;br /&gt;
        id=combat&lt;br /&gt;
        engine=cpp&lt;br /&gt;
        name=ai_default_rca::combat_phase&lt;br /&gt;
        max_score=30000&lt;br /&gt;
        score=30000&lt;br /&gt;
    [/candidate_action]&lt;br /&gt;
[/modify_ai]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* Note that the ''change'' action is really a ''delete'' action followed by an ''add'' action.  Thus, the full content of the [candidate_action] tag needs to be given, not just the parameter that is being changed.&lt;br /&gt;
* [[Modifying_AI_Components#Modifying_AI_Components_Helper_Macros|Macro versions]] also exist for these actions.&lt;br /&gt;
* The configurations of the default CAs, including their ids and names, can be found in file [https://github.com/wesnoth/wesnoth/blob/master/data/core/macros/ai_candidate_actions.cfg data/core/macros/ai_candidate_actions.cfg].&lt;br /&gt;
&lt;br /&gt;
== On the Fly Candidate Action Testing ==&lt;br /&gt;
&lt;br /&gt;
One of the advantages of using Lua for custom AIs is that there is no need for restarting Wesnoth after the code is changed for the changes to take effect.  In fact, it is even possible to set things up so that not even a reload is required.  You can simply change the code and click on a context menu option (or even just use a keyboard shortcut) to test the new version of the candidate action's evaluation and/or execution function.  This section describes how to set up such a testing mechanism using the example of the Guardian Bob AI described above.&lt;br /&gt;
&lt;br /&gt;
The context menu option, along with hotkey 'x', is set up by adding the following code to the WML of whatever scenario you want to test this in.&lt;br /&gt;
&amp;lt;syntaxhighlight lang='wml'&amp;gt;&lt;br /&gt;
[set_menu_item]&lt;br /&gt;
    id = m01&lt;br /&gt;
    description=_&amp;quot;Run Lua test code&amp;quot;&lt;br /&gt;
    image=items/ring-red.png~CROP(26,26,20,20)&lt;br /&gt;
    [command]&lt;br /&gt;
        [lua]&lt;br /&gt;
            code=&amp;lt;&amp;lt;wesnoth.dofile &amp;quot;~add-ons/my_addon/lua/test_lua.lua&amp;quot;&amp;gt;&amp;gt;&lt;br /&gt;
        [/lua]&lt;br /&gt;
    [/command]&lt;br /&gt;
    [default_hotkey]&lt;br /&gt;
        key=x&lt;br /&gt;
    [/default_hotkey]&lt;br /&gt;
[/set_menu_item]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This should be pretty self-explanatory.  The only thing to note is that you need to have a file called 'test_lua.lua' at the path given in the ''code'' attribute (or change the path and file name used there, of course).  This file could, for example, look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang='lua'&amp;gt;&lt;br /&gt;
-- Set up CA specific variable&lt;br /&gt;
local fn = &amp;quot;~add-ons/my_addon/lua/return_guardian.lua&amp;quot;&lt;br /&gt;
local cfg = { id = 'Bob', return_x = 10, return_y = 15 }&lt;br /&gt;
&lt;br /&gt;
-- Clear screen and make sure we're in debug mode&lt;br /&gt;
wesnoth.clear_messages()&lt;br /&gt;
wesnoth.message('AI testing for side ' .. wesnoth.current.side)&lt;br /&gt;
if (not wesnoth.game_config.debug) then&lt;br /&gt;
    wesnoth.message(&amp;quot;***** This option requires debug mode. Activate by typing ':debug' *****&amp;quot;)&lt;br /&gt;
    return&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Set up the AI variables needed by the CA&lt;br /&gt;
local self = { data = {} }&lt;br /&gt;
local ai = wesnoth.debug_ai(wesnoth.current.side).ai&lt;br /&gt;
&lt;br /&gt;
-- Evaluate CA and execute if score &amp;gt; 0&lt;br /&gt;
local eval = wesnoth.dofile(fn):evaluation(ai, cfg, self)&lt;br /&gt;
wesnoth.message('Eval score:', eval)&lt;br /&gt;
if (eval &amp;gt; 0) then&lt;br /&gt;
    wesnoth.dofile(fn):execution(ai, cfg, self)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The ''fn'' and ''cfg'' variables define the file containing the CA to be tested, and the configuration table to be passed to the evaluation and execution tables.  We assume the same Guardian Bob AI here that is used in the example above.&lt;br /&gt;
&lt;br /&gt;
The next block is simply some &amp;quot;house keeping&amp;quot;, cleaning the screen and making sure that we are in debug mode.&lt;br /&gt;
&lt;br /&gt;
We then need to set up the ''self'' and ''ai'' tables required by the CA functions.  ''self'' does not need to contain anything other than an empty ''data'' table (and even that is not required for our example, we do this here simply to demonstrate how it is done).&lt;br /&gt;
&lt;br /&gt;
By contrast, the ''ai'' table needs to be populated with all the Lua AI functions.  These are not available by default outside the AI context, but can be accessed through &amp;lt;code&amp;gt;wesnoth.debug_ai(wesnoth.current.side).ai&amp;lt;/code&amp;gt; as described [[LuaAI#Debug_access_to_the_AI_tree|here]].  This requires debug mode to be active, which is why we check for it earlier in the code.&lt;br /&gt;
&lt;br /&gt;
The last few lines are then simply calling the evaluation function and, if it returns a positive score, the execution function, thus simulating the use of the CA by the ''main_loop'' stage.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* It is important to realize that we are using wesnoth.dofile() for including files, rather than wesnoth.require().  Using the latter, while recommended for the final version of your code, would require a reload each time a file is changed.  This is not needed with ''wesnoth.dofile()''.  You can simply change the file and press 'x' again to test the new code.&lt;br /&gt;
* It is, of course, also possible to test other Lua AI (or non-AI) functionality in this way, not just the evaluation and/or execution of CAs.  To that end:&lt;br /&gt;
** The [http://forum.wesnoth.org/viewtopic.php?f=58&amp;amp;t=28320&amp;amp;start=45 Wesnoth Lua Pack] debug utilities are an extremely powerful tool for trouble shooting AI code written in Lua; or for figuring out some of the Lua AI functions in the first place.  One could, for example, use ''debug_utils.dbms()'' to see all the content of the ''ai'' table, or check out the structure of the return tables of some of the more complex functions.&lt;br /&gt;
* If you are doing a lot of AI testing, it might be worth to set up a dedicated test scenario with a variety of right-click menu options, for example for CA evaluation only, CA evaluation plus execution, running your own little CA evaluation loop and executing random Lua code.  Having several menu options with their own hotkeys in combination with manipulating units on the map in debug mode is a very powerful and convenient method for AI testing.&lt;br /&gt;
&lt;br /&gt;
== Legacy Methods for Creating Custom AIs ==&lt;br /&gt;
&lt;br /&gt;
Several other (older) methods still exist for creating custom AIs.  However, there is nothing that can be done with them that cannot also be done with the methods described on this page.  While the old methods are still functional at the moment, they might therefore be removed at some point in order to simplify maintenance of the Wesnoth C++ code base.  We do not recommend using them for new AI code any more. Ideally, old AI code should be ported to the new methods.&lt;br /&gt;
&lt;br /&gt;
Nevertheless, for the time being the documentation for using the old methods still exists.  This includes:&lt;br /&gt;
* [[Lua_AI_Legacy_Methods_Howto|Old Lua AI candidate action syntax]] requiring a Lua engine definition&lt;br /&gt;
* [[Lua_AI_Legacy_Methods_Howto#Behavior_.28Sticky.29_Candidate_Actions|Behavior candidate actions]]&lt;br /&gt;
* [[FormulaAI|Formula AI]]&lt;br /&gt;
* Using [[Wesnoth_AI_Framework#Legacy_Stages|stages other than the ''main_loop'' stage]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Wesnoth_AI&amp;diff=65209</id>
		<title>Wesnoth AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Wesnoth_AI&amp;diff=65209"/>
		<updated>2019-12-11T20:58:03Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* AIs Available to Scenario and Map Designers */ clarify a statement&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Wesnoth AIs==&lt;br /&gt;
&lt;br /&gt;
Wesnoth provides an artificial intelligence (AI) which is active by default for all computer-controlled sides, as well as several optional AIs.  These AIs are highly configurable and can be included and/or customized by scenario and map creators using simple WML code.  For AI developers, it is also possible to replace parts or all of the default AI with custom code.&lt;br /&gt;
&lt;br /&gt;
This page provides a brief summary of the available AIs and of the methods, tools and resources available for using and modifying them.  Links are provided to detailed documentation on each of the topics.  For reference, we also provide a brief timeline describing some of the main development milestones of the Wesnoth AIs.&lt;br /&gt;
&lt;br /&gt;
== Available AIs ==&lt;br /&gt;
&lt;br /&gt;
This section briefly lists the different AIs available in Wesnoth.  Click on the links provided here and in the resources section below for detailed descriptions of the AIs and for instructions for using and customizing them.&lt;br /&gt;
&lt;br /&gt;
=== AIs Available to Scenario and Map Designers ===&lt;br /&gt;
&lt;br /&gt;
Wesnoth comes with several built-in AIs from which scenario and map designers can choose:&lt;br /&gt;
&lt;br /&gt;
* '''[[RCA_AI|Default (RCA) AI]]''': If no special instructions are given, this AI is used automatically for all computer-controlled sides with no setup needed.  It can be customized in many different ways.&lt;br /&gt;
* The '''[[Experimental AI]]''' adds additional features to the RCA AI.  {{DevFeature1.15|3}} Most of the actions of the Experimental AI have been merged into the default AI. The Experimental AI is therefore currently only available in debug mode in the MP computer player selection menu, as it is almost the same as the default AI. This may change in the future if new features are added.&lt;br /&gt;
* '''[[Micro AIs]]''' add specialized behavior to Wesnoth that cannot be achieved with the default AI.  There is a large number of available Micro AIs and they can be set up using just a few lines of simple WML code.&lt;br /&gt;
* '''[[Creating Custom AIs|Custom AIs]]''':  In addition to these AIs, Wesnoth provides a large variety of options for writing your own AI code.&lt;br /&gt;
&lt;br /&gt;
=== AIs Available in the Multiplayer Setup Menu ===&lt;br /&gt;
&lt;br /&gt;
Several AIs are available to be chosen for computer players in the multiplayer setup menu:&lt;br /&gt;
&lt;br /&gt;
* '''[[RCA_AI|Default AI (RCA)]]''': This is the same AI as listed above.&lt;br /&gt;
* '''[[Experimental_AI]]''': Again, the same AI as described above.&lt;br /&gt;
* Debug Mode '''Dev AIs''': If Wesnoth is in debug mode, several additional AIs are available in the menu.  For the most part, these are not meant as complete stand-alone AIs, or as AIs that are significantly different from the default AI, but rather as examples that can be used for development purposes.  {{DevFeature1.13|5}} The list of AIs has been cleaned up.  Only working and (somewhat) meaningful AIs remain.&lt;br /&gt;
* '''[[Creating Custom AIs|Custom AIs]]''':  Just as it is possible to include custom AIs in the scenario code, they can also be provided for selection from the MP setup menu.  These can be written either for specific scenarios/maps or so that they are available for all maps.&lt;br /&gt;
&lt;br /&gt;
== Available AI Tools ==&lt;br /&gt;
&lt;br /&gt;
The default AI is written in C++ and therefore difficult to modify directly.  However, it is a [[Wesnoth_AI_Framework#Wesnoth_AI_Framework:_A_Composite_AI|composite AI]] assembled from individual components which can be customized and modified in both WML and [[LuaWML|Lua]].  Documentation on the available AI tools can be found at:&lt;br /&gt;
&lt;br /&gt;
* [[AiWML]]: Customizing the default AI using WML&lt;br /&gt;
* [[Modifying AI Components]]: More complex customization tasks&lt;br /&gt;
* [[Wesnoth AI Framework]]: Description of Wesnoth's composite AI configuration&lt;br /&gt;
* [[Creating Custom AIs]]: Instructions for writing your own AI components&lt;br /&gt;
* [[LuaAI|Lua AI]]: Recommended method for writing custom AIs&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* Wesnoth has its own internal language called 'Formula' which can also be used for AI development.  It is, however, not maintained any more and has by now been surpassed by Lua in ease of use in most cases.  We therefore do not recommend using it any more, but links to the legacy documentation are still provided in the resources section below. &lt;br /&gt;
* A Python API for AI development also existed for a while.  It was removed for security reasons.&lt;br /&gt;
&lt;br /&gt;
== List of Wesnoth AI Resources ==&lt;br /&gt;
&lt;br /&gt;
===AI Resources for Scenario and Map Creators===&lt;br /&gt;
&lt;br /&gt;
* [[RCA AI]]: Description of Wesnoth's default AI, with the following additional documentation:&lt;br /&gt;
** [[AiWML]]: Customizing the default AI using WML&lt;br /&gt;
** [[AI Recruitment]]: Customizing the recruitment of the default AI&lt;br /&gt;
** [[Modifying AI Components]]: Instructions for some of the more complex customization tasks&lt;br /&gt;
* [[Experimental AI]]: Experimental augmented version of the default AI with additional features&lt;br /&gt;
* [[Micro AIs]]: Additional specialized AI behavior that cannot be achieved with the default AI&lt;br /&gt;
&lt;br /&gt;
===Resources for AI Coders===&lt;br /&gt;
&lt;br /&gt;
* [[Wesnoth AI Framework]]: General principles of the inner workings of the Wesnoth AI mechanism&lt;br /&gt;
* [[Creating Custom AIs]]: Instructions for doing just that&lt;br /&gt;
* [[LuaAI|Lua AI]]: Recommended method for writing custom AIs&lt;br /&gt;
&lt;br /&gt;
==== Legacy Resources for AI Coders====&lt;br /&gt;
&lt;br /&gt;
* [[Lua AI Legacy Methods Howto]]: Old methods for writing custom AIs using Lua AI&lt;br /&gt;
* [[FormulaAI|Formula AI]]: Another language for writing custom AIs&lt;br /&gt;
&lt;br /&gt;
===Miscellaneous Other Resources===&lt;br /&gt;
&lt;br /&gt;
The following resources are not generally needed for understanding, using and modifying Wesnoth AIs, but they might nevertheless contain some useful bits of information for the interested reader.&lt;br /&gt;
&lt;br /&gt;
* [[AI Arena]]&lt;br /&gt;
* [[Machine Learning Recruiter]]&lt;br /&gt;
* [[WhyWritingAWesnothAIIsHard|Why Writing A Wesnoth AI Is Hard]]&lt;br /&gt;
* [[WritingYourOwnAI|Writing Your Own AI]]&lt;br /&gt;
&lt;br /&gt;
Finally, the link to the AI Category at the bottom of this page includes links to all the remaining AI-related pages.&lt;br /&gt;
&lt;br /&gt;
==A Very Brief History of Wesnoth AIs==&lt;br /&gt;
&lt;br /&gt;
While the general principle of the Wesnoth AI has been relatively stable throughout its development, the AI implementation has seen dramatic changes.  That said, basic scenario creator tasks such as [[AiWML|modifying AI parameters in WML]] are usually not changed much in order to keep backward compatibility.  On the other hand, the steps needed to write, for example,  [[Creating_Custom_AIs#Creating_Custom_Candidate_Actions|custom candidate actions]] and bind them into the existing AI framework have definitely evolved significantly from Wesnoth 1.8 to 1.10 to 1.12.&lt;br /&gt;
&lt;br /&gt;
It is not possible to capture all these changes on the Wesnoth AI wiki pages due to the complexity of the topic.  They generally only refer to the latest stable release series and the current development release series.  If, for some reason, you need to do AI work with an earlier version of Wesnoth, we still provide some of the legacy documentation, but you might also have to check out the history of the wiki pages.  Another way to go about it is by checking out examples of how things are done in mainline or add-ons of the respective release series.  In general, however, we ''strongly'' recommend using the latest Wesnoth versions, as modifying AI behavior has become ''a lot'' simpler than it used to be.&lt;br /&gt;
&lt;br /&gt;
For reference, here is a list of some of the major milestones of the AI development:&lt;br /&gt;
&lt;br /&gt;
* Up to '''Wesnoth 1.6''':&lt;br /&gt;
** The default AI of Wesnoth is, in fact, called ''Default AI'', with &amp;lt;code&amp;gt;id=default_ai&amp;lt;/code&amp;gt; in the configuration.&lt;br /&gt;
* '''Wesnoth 1.1/1.2''':&lt;br /&gt;
** A Python API for AI development is introduced.  This was later removed for security reasons.&lt;br /&gt;
* '''Wesnoth 1.5/1.6''':&lt;br /&gt;
** [[FormulaAI|Formula AI]] is introduced.&lt;br /&gt;
* '''Wesnoth 1.7/1.8''':&lt;br /&gt;
** [[Wesnoth_AI_Framework|Composite AI framework]] is introduced, with the default AI functionality ported to this framework in the form of the [[RCA AI]].&lt;br /&gt;
** The [[RCA AI]] becomes the default.&lt;br /&gt;
** [[LuaAI|Lua AI]] is introduced.&lt;br /&gt;
* '''Wesnoth 1.11/1.12''':&lt;br /&gt;
** Introduction of the [[Micro AIs]]&lt;br /&gt;
** External Lua candidate actions become fully usable.  They are now the recommended method for [[Creating_Custom_AIs|creating custom AIs]]&lt;br /&gt;
* '''Wesnoth 1.15/1.16''':&lt;br /&gt;
** Merging of most of the previous [[Experimental_AI]] candidate actions into the [[RCA AI]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Experimental_AI&amp;diff=65208</id>
		<title>Experimental AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Experimental_AI&amp;diff=65208"/>
		<updated>2019-12-11T20:56:26Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Experimental AI Summary */ add a forgotten bullet&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Experimental AI Summary ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} Most of the Experimental AI candidate actions (CAs) '''have been merged into the [[RCA_AI|default (RCA) AI]]''', see descriptions below for the individual CAs. The Experimental AI is therefore currently only available in debug mode in the MP computer player selection menu, as it is almost the same as the default AI. This may change in the future if new features are added.  In scenario WML code, the methods described below to choose the Experimental AI work as before (but, of course, there will not be much of a difference to the default AI).&lt;br /&gt;
&lt;br /&gt;
An ''Experimental AI'' is available for use in both MP maps and SP scenarios.  At the moment, this AI contains the following [[Wesnoth_AI_Framework#The_Candidate_Actions_of_the_main_loop_Stage|candidate actions]]:&lt;br /&gt;
* New and improved recruiting&lt;br /&gt;
* More aggressive village grabbing&lt;br /&gt;
* Healer placement behind injured units and more assertive retreating of injured units to healing locations.&lt;br /&gt;
* Leader castle switching: useful on certain MP maps&lt;br /&gt;
* Poison spreading&lt;br /&gt;
* Moving toward any enemy, if there are no enemy leaders or villages&lt;br /&gt;
* All the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|default RCA AI candidate actions]] except for recruiting&lt;br /&gt;
&lt;br /&gt;
== Experimental AI Setup ==&lt;br /&gt;
&lt;br /&gt;
In multiplayer maps, this AI is available from the game setup menu as 'Experimental AI'.  In single-player scenarios, it can be included by using the following code in a [side] tag&lt;br /&gt;
 [ai]&lt;br /&gt;
     # Deprecated version (for Wesnoth 1.14 and earlier)&lt;br /&gt;
     {EXPERIMENTAL_AI}&lt;br /&gt;
 [/ai]&lt;br /&gt;
{{DevFeature1.15|0}} The EXPERIMENTAL_AI Macro is deprecated now. The Experimental AI should be included as follows instead (this works in Wesnoth 1.14 already):&lt;br /&gt;
 [ai]&lt;br /&gt;
     # Current version (works from Wesnoth 1.14 on)&lt;br /&gt;
     ai_algorithm=experimental_ai&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Note that the EXPERIMENTAL_AI macro must be inside &amp;lt;code&amp;gt;[side][ai]&amp;lt;/code&amp;gt;, it does not work with &amp;lt;code&amp;gt;[modify_side]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[modify_ai]&amp;lt;/code&amp;gt;. If the &amp;lt;code&amp;gt;ai_algorithm&amp;lt;/code&amp;gt; version is used, the Experimental AI can also be added as&lt;br /&gt;
     [modify_side]&lt;br /&gt;
         side=2&lt;br /&gt;
         [ai]&lt;br /&gt;
             ai_algorithm=experimental_ai&lt;br /&gt;
         [/ai]&lt;br /&gt;
     [/modify_side]&lt;br /&gt;
or&lt;br /&gt;
     [modify_side]&lt;br /&gt;
         side=2&lt;br /&gt;
         switch_ai=ai/ais/ai_generic_rush.cfg  # Wesnoth 1.14 and earlier&lt;br /&gt;
         #switch_ai=ai/ais/ai_experimental.cfg # Wesnoth 1.15 and later&lt;br /&gt;
     [/modify_side]&lt;br /&gt;
&lt;br /&gt;
=== Custom Parameter Setup ===&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|2}}&lt;br /&gt;
A small number of custom parameters can be passed to the Experimental AI. These cannot be set up using the &amp;lt;code&amp;gt;ai_algorithm&amp;lt;/code&amp;gt; syntax, but require the use of a macro in the &amp;lt;code&amp;gt;[side][ai]&amp;lt;/code&amp;gt; tag:&lt;br /&gt;
 [side]&lt;br /&gt;
     [ai]&lt;br /&gt;
         {CUSTOMIZABLE_EXPERIMENTAL_AI (&lt;br /&gt;
             high_level_fraction=0.33&lt;br /&gt;
             randomness=0.2&lt;br /&gt;
         )}&lt;br /&gt;
     [/ai]&lt;br /&gt;
 [/side]&lt;br /&gt;
Note that only the parameters one wants changed need to be defined in the macro, those omitted will take their default values.&lt;br /&gt;
&lt;br /&gt;
'''Custom parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''high_level_fraction'''=0: (non-negative number) The approximate fraction of units of level 2 or higher to be recruited. This is defined as fraction of units on the map, not of new units being recruited during a turn or over several turns (which makes a difference if there are already units on the map). The effect is also cumulative per level (starting from level 2), meaning that 1/3 of L2 units will be recruited in the example above, 1/3*1/3=1/9 L3s etc. The default value is zero, which leaves the high-level unit recruiting to the default Experimental AI algorithm resulting usually in very few such units being recruited.&lt;br /&gt;
* '''randomness'''=0.1: (number)  A random number is applied to the Experimental AI recruiting score to prevent the recruitment pattern from being too predictable. 0 causes no randomness to be applied, while larger numbers increase the random effect. A value of 1-2 generates results in which the random effect is approximately equal to the scored effect. Extremely high values are essentially entirely random.&lt;br /&gt;
&lt;br /&gt;
== Experimental AI Details ==&lt;br /&gt;
&lt;br /&gt;
The Experimental AI uses most of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|candidate actions]] (CAs) of the [[RCA AI]], the one exception being the recruitment CA which is replaced by another recruiting CA.  In addition, it also adds a number of new CAs.  The following are the differences between the Experimental AI and the default (RCA) AI:&lt;br /&gt;
&lt;br /&gt;
====Recruitment====&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} At the moment, the recruiting CA remains the main difference between the default and the Experimental AI. We are, however, currently working on adapting the Experimental AI's recruiting to work in more general settings (mostly this means with more than one leader), after which there will be a choice which recruiting to use in the default AI.&lt;br /&gt;
&lt;br /&gt;
The Experimental AI has a completely different recruiting algorithm that was designed to emulate the choices of a human player, especially in the first few turns.  As such, it tries to pick units that counter both what is on the battlefield and that the opponent could recruit.  As it gains more units relative to the enemy, it picks harder hitting units, even at the cost of fragility, to break through enemy lines.&lt;br /&gt;
&lt;br /&gt;
Unlike the default recruitment algorithm, it also chooses where the units are recruited in order to maximize the number of villages that can be captured and if it knows the leader will be moving to a &amp;quot;better&amp;quot; keep (see below), it will under-recruit at the first keep to project more forward power, recruiting just enough to capture all villages.&lt;br /&gt;
&lt;br /&gt;
It also adjusts its recruitment based on map size, favouring faster units on larger maps, trusting that the economy advantage from capturing more villages will more than offset the price.&lt;br /&gt;
&lt;br /&gt;
It knows about poison and avoids recruiting units that depend on it for damage if the enemy is immune.&lt;br /&gt;
&lt;br /&gt;
It also looks at the expected cost of the unit over the next few turns, which prevents the recruitment of too many fast weak units unless the additional gold from an expected capture of a village is enough to offset the increased cost.&lt;br /&gt;
&lt;br /&gt;
Time of day is taken into account and it prefers units that will reach the current closest enemy at the favoured time of day. This allows, for example, for a slight bias towards saurians during the day and drakes at night on small maps (because they will reach the fight at the right time).&lt;br /&gt;
&lt;br /&gt;
Because the AI cannot use them well, it does not accurately measure the damage done by a unit with berserk, causing them to almost never be recruited.&lt;br /&gt;
&lt;br /&gt;
There is also a small amount of randomness added to allow two units that are almost equally good according to the evaluation to be chosen evenly, instead of only picking the first one every time.&lt;br /&gt;
&lt;br /&gt;
====Villages====&lt;br /&gt;
&lt;br /&gt;
The Experimental AI prefers to capture villages over fighting. This weakens it somewhat in raw fighting but it tends to get enough extra gold that this is actually to its advantage vs. the default AI, although its current implementation is probably too focused on villages.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} Besides recruiting, this is the only CA that is different between the default and Experimental AIs. It is too simple to work in a general setting (and therefore not suited for the default AI) and there is a separate village grabbing CA in the default AI, so it does not make much of a difference. This CA needs to be either improved or removed.&lt;br /&gt;
&lt;br /&gt;
====Healing and Retreating====&lt;br /&gt;
&lt;br /&gt;
If a healer is not used to attack, the Experimental AI will try to heal units, instead of positioning to attack later.&lt;br /&gt;
&lt;br /&gt;
In a separate CA, units retreat to villages/healers when injured.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} These two CAs have been merged into the default AI.&lt;br /&gt;
&lt;br /&gt;
====Castle Switching====&lt;br /&gt;
&lt;br /&gt;
The leader will move to different keeps, providing a forward force. Usually this works very well, especially at the beginning of the game, but it keeps moving later, which sometimes might get it killed in some scenarios. In that case, it might be worth stopping this behaviour after several turn by removing this CA.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} This CA has been merged into the default AI.&lt;br /&gt;
&lt;br /&gt;
====Enemy Poisoning====&lt;br /&gt;
&lt;br /&gt;
There is also an explicit poison attack routine that tries to poison unpoisoned units instead of repeatedly poisoning the same one.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} This CA has been merged into the default AI.&lt;br /&gt;
&lt;br /&gt;
====Move to Any Enemy====&lt;br /&gt;
&lt;br /&gt;
This is a fall-back CA that kicks in only when no other actions are taken. Its purpose is to not let the AI units sit idle on maps without enemy leaders and villages.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} This CA has been merged into the default AI.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Experimental_AI&amp;diff=65207</id>
		<title>Experimental AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Experimental_AI&amp;diff=65207"/>
		<updated>2019-12-11T20:54:17Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: Update after merge of ExpAI CAs into the default AI&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Experimental AI Summary ==&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} Most of the Experimental AI candidate actions (CAs) '''have been merged into the [[RCA_AI|default (RCA) AI]]''', see descriptions below for the individual CAs. The Experimental AI is therefore currently only available in debug mode in the MP computer player selection menu, as it is almost the same as the default AI. This may change in the future if new features are added.  In scenario WML code, the methods described below to choose the Experimental AI work as before (but, of course, there will not be much of a difference to the default AI).&lt;br /&gt;
&lt;br /&gt;
An ''Experimental AI'' is available for use in both MP maps and SP scenarios.  At the moment, this AI contains the following [[Wesnoth_AI_Framework#The_Candidate_Actions_of_the_main_loop_Stage|candidate actions]]:&lt;br /&gt;
* New and improved recruiting&lt;br /&gt;
* More aggressive village grabbing&lt;br /&gt;
* Healer placement behind injured units&lt;br /&gt;
* Leader castle switching: useful on certain MP maps&lt;br /&gt;
* Poison spreading&lt;br /&gt;
* All the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|default RCA AI candidate actions]] except for recruiting&lt;br /&gt;
&lt;br /&gt;
== Experimental AI Setup ==&lt;br /&gt;
&lt;br /&gt;
In multiplayer maps, this AI is available from the game setup menu as 'Experimental AI'.  In single-player scenarios, it can be included by using the following code in a [side] tag&lt;br /&gt;
 [ai]&lt;br /&gt;
     # Deprecated version (for Wesnoth 1.14 and earlier)&lt;br /&gt;
     {EXPERIMENTAL_AI}&lt;br /&gt;
 [/ai]&lt;br /&gt;
{{DevFeature1.15|0}} The EXPERIMENTAL_AI Macro is deprecated now. The Experimental AI should be included as follows instead (this works in Wesnoth 1.14 already):&lt;br /&gt;
 [ai]&lt;br /&gt;
     # Current version (works from Wesnoth 1.14 on)&lt;br /&gt;
     ai_algorithm=experimental_ai&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Note that the EXPERIMENTAL_AI macro must be inside &amp;lt;code&amp;gt;[side][ai]&amp;lt;/code&amp;gt;, it does not work with &amp;lt;code&amp;gt;[modify_side]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[modify_ai]&amp;lt;/code&amp;gt;. If the &amp;lt;code&amp;gt;ai_algorithm&amp;lt;/code&amp;gt; version is used, the Experimental AI can also be added as&lt;br /&gt;
     [modify_side]&lt;br /&gt;
         side=2&lt;br /&gt;
         [ai]&lt;br /&gt;
             ai_algorithm=experimental_ai&lt;br /&gt;
         [/ai]&lt;br /&gt;
     [/modify_side]&lt;br /&gt;
or&lt;br /&gt;
     [modify_side]&lt;br /&gt;
         side=2&lt;br /&gt;
         switch_ai=ai/ais/ai_generic_rush.cfg  # Wesnoth 1.14 and earlier&lt;br /&gt;
         #switch_ai=ai/ais/ai_experimental.cfg # Wesnoth 1.15 and later&lt;br /&gt;
     [/modify_side]&lt;br /&gt;
&lt;br /&gt;
=== Custom Parameter Setup ===&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|2}}&lt;br /&gt;
A small number of custom parameters can be passed to the Experimental AI. These cannot be set up using the &amp;lt;code&amp;gt;ai_algorithm&amp;lt;/code&amp;gt; syntax, but require the use of a macro in the &amp;lt;code&amp;gt;[side][ai]&amp;lt;/code&amp;gt; tag:&lt;br /&gt;
 [side]&lt;br /&gt;
     [ai]&lt;br /&gt;
         {CUSTOMIZABLE_EXPERIMENTAL_AI (&lt;br /&gt;
             high_level_fraction=0.33&lt;br /&gt;
             randomness=0.2&lt;br /&gt;
         )}&lt;br /&gt;
     [/ai]&lt;br /&gt;
 [/side]&lt;br /&gt;
Note that only the parameters one wants changed need to be defined in the macro, those omitted will take their default values.&lt;br /&gt;
&lt;br /&gt;
'''Custom parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''high_level_fraction'''=0: (non-negative number) The approximate fraction of units of level 2 or higher to be recruited. This is defined as fraction of units on the map, not of new units being recruited during a turn or over several turns (which makes a difference if there are already units on the map). The effect is also cumulative per level (starting from level 2), meaning that 1/3 of L2 units will be recruited in the example above, 1/3*1/3=1/9 L3s etc. The default value is zero, which leaves the high-level unit recruiting to the default Experimental AI algorithm resulting usually in very few such units being recruited.&lt;br /&gt;
* '''randomness'''=0.1: (number)  A random number is applied to the Experimental AI recruiting score to prevent the recruitment pattern from being too predictable. 0 causes no randomness to be applied, while larger numbers increase the random effect. A value of 1-2 generates results in which the random effect is approximately equal to the scored effect. Extremely high values are essentially entirely random.&lt;br /&gt;
&lt;br /&gt;
== Experimental AI Details ==&lt;br /&gt;
&lt;br /&gt;
The Experimental AI uses most of the [[RCA_AI#The_Candidate_Actions_.28CAs.29_of_the_main_loop_Stage_in_the_RCA_AI|candidate actions]] (CAs) of the [[RCA AI]], the one exception being the recruitment CA which is replaced by another recruiting CA.  In addition, it also adds a number of new CAs.  The following are the differences between the Experimental AI and the default (RCA) AI:&lt;br /&gt;
&lt;br /&gt;
====Recruitment====&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} At the moment, the recruiting CA remains the main difference between the default and the Experimental AI. We are, however, currently working on adapting the Experimental AI's recruiting to work in more general settings (mostly this means with more than one leader), after which there will be a choice which recruiting to use in the default AI.&lt;br /&gt;
&lt;br /&gt;
The Experimental AI has a completely different recruiting algorithm that was designed to emulate the choices of a human player, especially in the first few turns.  As such, it tries to pick units that counter both what is on the battlefield and that the opponent could recruit.  As it gains more units relative to the enemy, it picks harder hitting units, even at the cost of fragility, to break through enemy lines.&lt;br /&gt;
&lt;br /&gt;
Unlike the default recruitment algorithm, it also chooses where the units are recruited in order to maximize the number of villages that can be captured and if it knows the leader will be moving to a &amp;quot;better&amp;quot; keep (see below), it will under-recruit at the first keep to project more forward power, recruiting just enough to capture all villages.&lt;br /&gt;
&lt;br /&gt;
It also adjusts its recruitment based on map size, favouring faster units on larger maps, trusting that the economy advantage from capturing more villages will more than offset the price.&lt;br /&gt;
&lt;br /&gt;
It knows about poison and avoids recruiting units that depend on it for damage if the enemy is immune.&lt;br /&gt;
&lt;br /&gt;
It also looks at the expected cost of the unit over the next few turns, which prevents the recruitment of too many fast weak units unless the additional gold from an expected capture of a village is enough to offset the increased cost.&lt;br /&gt;
&lt;br /&gt;
Time of day is taken into account and it prefers units that will reach the current closest enemy at the favoured time of day. This allows, for example, for a slight bias towards saurians during the day and drakes at night on small maps (because they will reach the fight at the right time).&lt;br /&gt;
&lt;br /&gt;
Because the AI cannot use them well, it does not accurately measure the damage done by a unit with berserk, causing them to almost never be recruited.&lt;br /&gt;
&lt;br /&gt;
There is also a small amount of randomness added to allow two units that are almost equally good according to the evaluation to be chosen evenly, instead of only picking the first one every time.&lt;br /&gt;
&lt;br /&gt;
====Villages====&lt;br /&gt;
&lt;br /&gt;
The Experimental AI prefers to capture villages over fighting. This weakens it somewhat in raw fighting but it tends to get enough extra gold that this is actually to its advantage vs. the default AI, although its current implementation is probably too focused on villages.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} Besides recruiting, this is the only CA that is different between the default and Experimental AIs. It is too simple to work in a general setting (and therefore not suited for the default AI) and there is a separate village grabbing CA in the default AI, so it does not make much of a difference. This CA needs to be either improved or removed.&lt;br /&gt;
&lt;br /&gt;
====Healing and Retreating====&lt;br /&gt;
&lt;br /&gt;
If a healer is not used to attack, the Experimental AI will try to heal units, instead of positioning to attack later.&lt;br /&gt;
&lt;br /&gt;
In a separate CA, units retreat to villages/healers when injured.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} These two CAs have been merged into the default AI.&lt;br /&gt;
&lt;br /&gt;
====Castle Switching====&lt;br /&gt;
&lt;br /&gt;
The leader will move to different keeps, providing a forward force. Usually this works very well, especially at the beginning of the game, but it keeps moving later, which sometimes might get it killed in some scenarios. In that case, it might be worth stopping this behaviour after several turn by removing this CA.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} This CA has been merged into the default AI.&lt;br /&gt;
&lt;br /&gt;
====Enemy Poisoning====&lt;br /&gt;
&lt;br /&gt;
There is also an explicit poison attack routine that tries to poison unpoisoned units instead of repeatedly poisoning the same one.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} This CA has been merged into the default AI.&lt;br /&gt;
&lt;br /&gt;
====Move to Any Enemy====&lt;br /&gt;
&lt;br /&gt;
This is a fall-back CA that kicks in only when no other actions are taken. Its purpose is to not let the AI units sit idle on maps without enemy leaders and villages.&lt;br /&gt;
&lt;br /&gt;
{{DevFeature1.15|3}} This CA has been merged into the default AI.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''See also:''' [[Wesnoth AI]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Wesnoth_AI&amp;diff=65206</id>
		<title>Wesnoth AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Wesnoth_AI&amp;diff=65206"/>
		<updated>2019-12-11T20:23:57Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* A Very Brief History of Wesnoth AIs */ Add ExpAI CA merge to timeline&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Wesnoth AIs==&lt;br /&gt;
&lt;br /&gt;
Wesnoth provides an artificial intelligence (AI) which is active by default for all computer-controlled sides, as well as several optional AIs.  These AIs are highly configurable and can be included and/or customized by scenario and map creators using simple WML code.  For AI developers, it is also possible to replace parts or all of the default AI with custom code.&lt;br /&gt;
&lt;br /&gt;
This page provides a brief summary of the available AIs and of the methods, tools and resources available for using and modifying them.  Links are provided to detailed documentation on each of the topics.  For reference, we also provide a brief timeline describing some of the main development milestones of the Wesnoth AIs.&lt;br /&gt;
&lt;br /&gt;
== Available AIs ==&lt;br /&gt;
&lt;br /&gt;
This section briefly lists the different AIs available in Wesnoth.  Click on the links provided here and in the resources section below for detailed descriptions of the AIs and for instructions for using and customizing them.&lt;br /&gt;
&lt;br /&gt;
=== AIs Available to Scenario and Map Designers ===&lt;br /&gt;
&lt;br /&gt;
Wesnoth comes with several built-in AIs from which scenario and map designers can choose:&lt;br /&gt;
&lt;br /&gt;
* '''[[RCA_AI|Default (RCA) AI]]''': If no special instructions are given, this AI is used automatically for all computer-controlled sides with no setup needed.  It can be customized in many different ways.&lt;br /&gt;
* The '''[[Experimental AI]]''' adds additional features to the RCA AI.  {{DevFeature1.15|3}} Most of the actions of the Experimental AI have been merged into the default AI. The Experimental AI is therefore currently only available in debug mode as it is almost the same as the default AI. This may change in the future if new features are added.&lt;br /&gt;
* '''[[Micro AIs]]''' add specialized behavior to Wesnoth that cannot be achieved with the default AI.  There is a large number of available Micro AIs and they can be set up using just a few lines of simple WML code.&lt;br /&gt;
* '''[[Creating Custom AIs|Custom AIs]]''':  In addition to these AIs, Wesnoth provides a large variety of options for writing your own AI code.&lt;br /&gt;
&lt;br /&gt;
=== AIs Available in the Multiplayer Setup Menu ===&lt;br /&gt;
&lt;br /&gt;
Several AIs are available to be chosen for computer players in the multiplayer setup menu:&lt;br /&gt;
&lt;br /&gt;
* '''[[RCA_AI|Default AI (RCA)]]''': This is the same AI as listed above.&lt;br /&gt;
* '''[[Experimental_AI]]''': Again, the same AI as described above.&lt;br /&gt;
* Debug Mode '''Dev AIs''': If Wesnoth is in debug mode, several additional AIs are available in the menu.  For the most part, these are not meant as complete stand-alone AIs, or as AIs that are significantly different from the default AI, but rather as examples that can be used for development purposes.  {{DevFeature1.13|5}} The list of AIs has been cleaned up.  Only working and (somewhat) meaningful AIs remain.&lt;br /&gt;
* '''[[Creating Custom AIs|Custom AIs]]''':  Just as it is possible to include custom AIs in the scenario code, they can also be provided for selection from the MP setup menu.  These can be written either for specific scenarios/maps or so that they are available for all maps.&lt;br /&gt;
&lt;br /&gt;
== Available AI Tools ==&lt;br /&gt;
&lt;br /&gt;
The default AI is written in C++ and therefore difficult to modify directly.  However, it is a [[Wesnoth_AI_Framework#Wesnoth_AI_Framework:_A_Composite_AI|composite AI]] assembled from individual components which can be customized and modified in both WML and [[LuaWML|Lua]].  Documentation on the available AI tools can be found at:&lt;br /&gt;
&lt;br /&gt;
* [[AiWML]]: Customizing the default AI using WML&lt;br /&gt;
* [[Modifying AI Components]]: More complex customization tasks&lt;br /&gt;
* [[Wesnoth AI Framework]]: Description of Wesnoth's composite AI configuration&lt;br /&gt;
* [[Creating Custom AIs]]: Instructions for writing your own AI components&lt;br /&gt;
* [[LuaAI|Lua AI]]: Recommended method for writing custom AIs&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* Wesnoth has its own internal language called 'Formula' which can also be used for AI development.  It is, however, not maintained any more and has by now been surpassed by Lua in ease of use in most cases.  We therefore do not recommend using it any more, but links to the legacy documentation are still provided in the resources section below. &lt;br /&gt;
* A Python API for AI development also existed for a while.  It was removed for security reasons.&lt;br /&gt;
&lt;br /&gt;
== List of Wesnoth AI Resources ==&lt;br /&gt;
&lt;br /&gt;
===AI Resources for Scenario and Map Creators===&lt;br /&gt;
&lt;br /&gt;
* [[RCA AI]]: Description of Wesnoth's default AI, with the following additional documentation:&lt;br /&gt;
** [[AiWML]]: Customizing the default AI using WML&lt;br /&gt;
** [[AI Recruitment]]: Customizing the recruitment of the default AI&lt;br /&gt;
** [[Modifying AI Components]]: Instructions for some of the more complex customization tasks&lt;br /&gt;
* [[Experimental AI]]: Experimental augmented version of the default AI with additional features&lt;br /&gt;
* [[Micro AIs]]: Additional specialized AI behavior that cannot be achieved with the default AI&lt;br /&gt;
&lt;br /&gt;
===Resources for AI Coders===&lt;br /&gt;
&lt;br /&gt;
* [[Wesnoth AI Framework]]: General principles of the inner workings of the Wesnoth AI mechanism&lt;br /&gt;
* [[Creating Custom AIs]]: Instructions for doing just that&lt;br /&gt;
* [[LuaAI|Lua AI]]: Recommended method for writing custom AIs&lt;br /&gt;
&lt;br /&gt;
==== Legacy Resources for AI Coders====&lt;br /&gt;
&lt;br /&gt;
* [[Lua AI Legacy Methods Howto]]: Old methods for writing custom AIs using Lua AI&lt;br /&gt;
* [[FormulaAI|Formula AI]]: Another language for writing custom AIs&lt;br /&gt;
&lt;br /&gt;
===Miscellaneous Other Resources===&lt;br /&gt;
&lt;br /&gt;
The following resources are not generally needed for understanding, using and modifying Wesnoth AIs, but they might nevertheless contain some useful bits of information for the interested reader.&lt;br /&gt;
&lt;br /&gt;
* [[AI Arena]]&lt;br /&gt;
* [[Machine Learning Recruiter]]&lt;br /&gt;
* [[WhyWritingAWesnothAIIsHard|Why Writing A Wesnoth AI Is Hard]]&lt;br /&gt;
* [[WritingYourOwnAI|Writing Your Own AI]]&lt;br /&gt;
&lt;br /&gt;
Finally, the link to the AI Category at the bottom of this page includes links to all the remaining AI-related pages.&lt;br /&gt;
&lt;br /&gt;
==A Very Brief History of Wesnoth AIs==&lt;br /&gt;
&lt;br /&gt;
While the general principle of the Wesnoth AI has been relatively stable throughout its development, the AI implementation has seen dramatic changes.  That said, basic scenario creator tasks such as [[AiWML|modifying AI parameters in WML]] are usually not changed much in order to keep backward compatibility.  On the other hand, the steps needed to write, for example,  [[Creating_Custom_AIs#Creating_Custom_Candidate_Actions|custom candidate actions]] and bind them into the existing AI framework have definitely evolved significantly from Wesnoth 1.8 to 1.10 to 1.12.&lt;br /&gt;
&lt;br /&gt;
It is not possible to capture all these changes on the Wesnoth AI wiki pages due to the complexity of the topic.  They generally only refer to the latest stable release series and the current development release series.  If, for some reason, you need to do AI work with an earlier version of Wesnoth, we still provide some of the legacy documentation, but you might also have to check out the history of the wiki pages.  Another way to go about it is by checking out examples of how things are done in mainline or add-ons of the respective release series.  In general, however, we ''strongly'' recommend using the latest Wesnoth versions, as modifying AI behavior has become ''a lot'' simpler than it used to be.&lt;br /&gt;
&lt;br /&gt;
For reference, here is a list of some of the major milestones of the AI development:&lt;br /&gt;
&lt;br /&gt;
* Up to '''Wesnoth 1.6''':&lt;br /&gt;
** The default AI of Wesnoth is, in fact, called ''Default AI'', with &amp;lt;code&amp;gt;id=default_ai&amp;lt;/code&amp;gt; in the configuration.&lt;br /&gt;
* '''Wesnoth 1.1/1.2''':&lt;br /&gt;
** A Python API for AI development is introduced.  This was later removed for security reasons.&lt;br /&gt;
* '''Wesnoth 1.5/1.6''':&lt;br /&gt;
** [[FormulaAI|Formula AI]] is introduced.&lt;br /&gt;
* '''Wesnoth 1.7/1.8''':&lt;br /&gt;
** [[Wesnoth_AI_Framework|Composite AI framework]] is introduced, with the default AI functionality ported to this framework in the form of the [[RCA AI]].&lt;br /&gt;
** The [[RCA AI]] becomes the default.&lt;br /&gt;
** [[LuaAI|Lua AI]] is introduced.&lt;br /&gt;
* '''Wesnoth 1.11/1.12''':&lt;br /&gt;
** Introduction of the [[Micro AIs]]&lt;br /&gt;
** External Lua candidate actions become fully usable.  They are now the recommended method for [[Creating_Custom_AIs|creating custom AIs]]&lt;br /&gt;
* '''Wesnoth 1.15/1.16''':&lt;br /&gt;
** Merging of most of the previous [[Experimental_AI]] candidate actions into the [[RCA AI]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Wesnoth_AI&amp;diff=65205</id>
		<title>Wesnoth AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Wesnoth_AI&amp;diff=65205"/>
		<updated>2019-12-11T20:20:01Z</updated>

		<summary type="html">&lt;p&gt;Mattsc: /* Available AIs */ Explain merge of ExpAI CAs into default AI&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to the Wesnoth AIs==&lt;br /&gt;
&lt;br /&gt;
Wesnoth provides an artificial intelligence (AI) which is active by default for all computer-controlled sides, as well as several optional AIs.  These AIs are highly configurable and can be included and/or customized by scenario and map creators using simple WML code.  For AI developers, it is also possible to replace parts or all of the default AI with custom code.&lt;br /&gt;
&lt;br /&gt;
This page provides a brief summary of the available AIs and of the methods, tools and resources available for using and modifying them.  Links are provided to detailed documentation on each of the topics.  For reference, we also provide a brief timeline describing some of the main development milestones of the Wesnoth AIs.&lt;br /&gt;
&lt;br /&gt;
== Available AIs ==&lt;br /&gt;
&lt;br /&gt;
This section briefly lists the different AIs available in Wesnoth.  Click on the links provided here and in the resources section below for detailed descriptions of the AIs and for instructions for using and customizing them.&lt;br /&gt;
&lt;br /&gt;
=== AIs Available to Scenario and Map Designers ===&lt;br /&gt;
&lt;br /&gt;
Wesnoth comes with several built-in AIs from which scenario and map designers can choose:&lt;br /&gt;
&lt;br /&gt;
* '''[[RCA_AI|Default (RCA) AI]]''': If no special instructions are given, this AI is used automatically for all computer-controlled sides with no setup needed.  It can be customized in many different ways.&lt;br /&gt;
* The '''[[Experimental AI]]''' adds additional features to the RCA AI.  {{DevFeature1.15|3}} Most of the actions of the Experimental AI have been merged into the default AI. The Experimental AI is therefore currently only available in debug mode as it is almost the same as the default AI. This may change in the future if new features are added.&lt;br /&gt;
* '''[[Micro AIs]]''' add specialized behavior to Wesnoth that cannot be achieved with the default AI.  There is a large number of available Micro AIs and they can be set up using just a few lines of simple WML code.&lt;br /&gt;
* '''[[Creating Custom AIs|Custom AIs]]''':  In addition to these AIs, Wesnoth provides a large variety of options for writing your own AI code.&lt;br /&gt;
&lt;br /&gt;
=== AIs Available in the Multiplayer Setup Menu ===&lt;br /&gt;
&lt;br /&gt;
Several AIs are available to be chosen for computer players in the multiplayer setup menu:&lt;br /&gt;
&lt;br /&gt;
* '''[[RCA_AI|Default AI (RCA)]]''': This is the same AI as listed above.&lt;br /&gt;
* '''[[Experimental_AI]]''': Again, the same AI as described above.&lt;br /&gt;
* Debug Mode '''Dev AIs''': If Wesnoth is in debug mode, several additional AIs are available in the menu.  For the most part, these are not meant as complete stand-alone AIs, or as AIs that are significantly different from the default AI, but rather as examples that can be used for development purposes.  {{DevFeature1.13|5}} The list of AIs has been cleaned up.  Only working and (somewhat) meaningful AIs remain.&lt;br /&gt;
* '''[[Creating Custom AIs|Custom AIs]]''':  Just as it is possible to include custom AIs in the scenario code, they can also be provided for selection from the MP setup menu.  These can be written either for specific scenarios/maps or so that they are available for all maps.&lt;br /&gt;
&lt;br /&gt;
== Available AI Tools ==&lt;br /&gt;
&lt;br /&gt;
The default AI is written in C++ and therefore difficult to modify directly.  However, it is a [[Wesnoth_AI_Framework#Wesnoth_AI_Framework:_A_Composite_AI|composite AI]] assembled from individual components which can be customized and modified in both WML and [[LuaWML|Lua]].  Documentation on the available AI tools can be found at:&lt;br /&gt;
&lt;br /&gt;
* [[AiWML]]: Customizing the default AI using WML&lt;br /&gt;
* [[Modifying AI Components]]: More complex customization tasks&lt;br /&gt;
* [[Wesnoth AI Framework]]: Description of Wesnoth's composite AI configuration&lt;br /&gt;
* [[Creating Custom AIs]]: Instructions for writing your own AI components&lt;br /&gt;
* [[LuaAI|Lua AI]]: Recommended method for writing custom AIs&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
* Wesnoth has its own internal language called 'Formula' which can also be used for AI development.  It is, however, not maintained any more and has by now been surpassed by Lua in ease of use in most cases.  We therefore do not recommend using it any more, but links to the legacy documentation are still provided in the resources section below. &lt;br /&gt;
* A Python API for AI development also existed for a while.  It was removed for security reasons.&lt;br /&gt;
&lt;br /&gt;
== List of Wesnoth AI Resources ==&lt;br /&gt;
&lt;br /&gt;
===AI Resources for Scenario and Map Creators===&lt;br /&gt;
&lt;br /&gt;
* [[RCA AI]]: Description of Wesnoth's default AI, with the following additional documentation:&lt;br /&gt;
** [[AiWML]]: Customizing the default AI using WML&lt;br /&gt;
** [[AI Recruitment]]: Customizing the recruitment of the default AI&lt;br /&gt;
** [[Modifying AI Components]]: Instructions for some of the more complex customization tasks&lt;br /&gt;
* [[Experimental AI]]: Experimental augmented version of the default AI with additional features&lt;br /&gt;
* [[Micro AIs]]: Additional specialized AI behavior that cannot be achieved with the default AI&lt;br /&gt;
&lt;br /&gt;
===Resources for AI Coders===&lt;br /&gt;
&lt;br /&gt;
* [[Wesnoth AI Framework]]: General principles of the inner workings of the Wesnoth AI mechanism&lt;br /&gt;
* [[Creating Custom AIs]]: Instructions for doing just that&lt;br /&gt;
* [[LuaAI|Lua AI]]: Recommended method for writing custom AIs&lt;br /&gt;
&lt;br /&gt;
==== Legacy Resources for AI Coders====&lt;br /&gt;
&lt;br /&gt;
* [[Lua AI Legacy Methods Howto]]: Old methods for writing custom AIs using Lua AI&lt;br /&gt;
* [[FormulaAI|Formula AI]]: Another language for writing custom AIs&lt;br /&gt;
&lt;br /&gt;
===Miscellaneous Other Resources===&lt;br /&gt;
&lt;br /&gt;
The following resources are not generally needed for understanding, using and modifying Wesnoth AIs, but they might nevertheless contain some useful bits of information for the interested reader.&lt;br /&gt;
&lt;br /&gt;
* [[AI Arena]]&lt;br /&gt;
* [[Machine Learning Recruiter]]&lt;br /&gt;
* [[WhyWritingAWesnothAIIsHard|Why Writing A Wesnoth AI Is Hard]]&lt;br /&gt;
* [[WritingYourOwnAI|Writing Your Own AI]]&lt;br /&gt;
&lt;br /&gt;
Finally, the link to the AI Category at the bottom of this page includes links to all the remaining AI-related pages.&lt;br /&gt;
&lt;br /&gt;
==A Very Brief History of Wesnoth AIs==&lt;br /&gt;
&lt;br /&gt;
While the general principle of the Wesnoth AI has been relatively stable throughout its development, the AI implementation has seen dramatic changes.  That said, basic scenario creator tasks such as [[AiWML|modifying AI parameters in WML]] are usually not changed much in order to keep backward compatibility.  On the other hand, the steps needed to write, for example,  [[Creating_Custom_AIs#Creating_Custom_Candidate_Actions|custom candidate actions]] and bind them into the existing AI framework have definitely evolved significantly from Wesnoth 1.8 to 1.10 to 1.12.&lt;br /&gt;
&lt;br /&gt;
It is not possible to capture all these changes on the Wesnoth AI wiki pages due to the complexity of the topic.  They generally only refer to the latest stable release series and the current development release series.  If, for some reason, you need to do AI work with an earlier version of Wesnoth, we still provide some of the legacy documentation, but you might also have to check out the history of the wiki pages.  Another way to go about it is by checking out examples of how things are done in mainline or add-ons of the respective release series.  In general, however, we ''strongly'' recommend using the latest Wesnoth versions, as modifying AI behavior has become ''a lot'' simpler than it used to be.&lt;br /&gt;
&lt;br /&gt;
For reference, here is a list of some of the major milestones of the AI development:&lt;br /&gt;
&lt;br /&gt;
* Up to '''Wesnoth 1.6''':&lt;br /&gt;
** The default AI of Wesnoth is, in fact, called ''Default AI'', with &amp;lt;code&amp;gt;id=default_ai&amp;lt;/code&amp;gt; in the configuration.&lt;br /&gt;
* '''Wesnoth 1.1/1.2''':&lt;br /&gt;
** A Python API for AI development is introduced.  This was later removed for security reasons.&lt;br /&gt;
* '''Wesnoth 1.5/1.6''':&lt;br /&gt;
** [[FormulaAI|Formula AI]] is introduced.&lt;br /&gt;
* '''Wesnoth 1.7/1.8''':&lt;br /&gt;
** [[Wesnoth_AI_Framework|Composite AI framework]] is introduced, with the default AI functionality ported to this framework in the form of the [[RCA AI]].&lt;br /&gt;
** The [[RCA AI]] becomes the default.&lt;br /&gt;
** [[LuaAI|Lua AI]] is introduced.&lt;br /&gt;
* '''Wesnoth 1.11/1.12''':&lt;br /&gt;
** Introduction of the [[Micro AIs]]&lt;br /&gt;
** External Lua candidate actions become fully usable.  They are now the recommended method for [[Creating_Custom_AIs|creating custom AIs]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>Mattsc</name></author>
		
	</entry>
</feed>