<?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=Flixx</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=Flixx"/>
	<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/Special:Contributions/Flixx"/>
	<updated>2026-04-05T10:37:11Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.31.16</generator>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=LuaWML/Pathfinder&amp;diff=52164</id>
		<title>LuaWML/Pathfinder</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=LuaWML/Pathfinder&amp;diff=52164"/>
		<updated>2013-10-09T13:38:14Z</updated>

		<summary type="html">&lt;p&gt;Flixx: insert find_cost_map&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page describes the [[LuaWML]] functions and helpers for finding paths.&lt;br /&gt;
&lt;br /&gt;
==== wesnoth.find_path ====&lt;br /&gt;
&lt;br /&gt;
Returns the shortest path from one location to another. The source location is given either by coordinates as two arguments x and y; there must be a unit at the source location when using the standard path calculator. The source location can also be given by a unit as a single argument (as returned by the functions from [[LuaWML:Units]]). The second location is given by its coordinates. The last argument is an optional table that can be used to parametrize the pathfinder. Its options are:&lt;br /&gt;
* '''max_cost''': if set, the pathfinder will ignore paths longer than its value&lt;br /&gt;
* '''ignore_units''': if set, the path will go through units and ignore zones of control&lt;br /&gt;
* '''ignore_teleport''': if set, the teleport ability of the unit is ignored&lt;br /&gt;
* '''viewing_side''': if set to a valid side number, fog and shroud for this side will be taken into account; if set to an invalid number (e.g. 0), fog and shroud will be ignored; if left unset, the viewing side will be the unit side&lt;br /&gt;
&lt;br /&gt;
The path is returned as a table of coordinate pairs. It contains both the source and destination tile if a path was found. The total cost of the path is also available as a second return value, if needed.&lt;br /&gt;
&lt;br /&gt;
 -- Display some items along the path from (x1,y1) to (x2,y2).&lt;br /&gt;
 local u = wesnoth.get_units({ x = x1, y = y1 })[1]&lt;br /&gt;
 local path, cost = wesnoth.find_path(u, x2, y2, { ignore_units = true, viewing_side = 0 })&lt;br /&gt;
 if cost &amp;gt; u.moves then&lt;br /&gt;
     wesnoth.message(&amp;quot;That's too far!&amp;quot;)&lt;br /&gt;
 else&lt;br /&gt;
     for i, loc in ipairs(path) do&lt;br /&gt;
         wesnoth.fire(&amp;quot;item&amp;quot;, { x = loc[1], y = loc[2], image = &amp;quot;items/buckler.png&amp;quot; })&lt;br /&gt;
     end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Instead of a parameter table, a cost function can be passed to the pathfinder. It will be called for all the tiles the computed path may possibly go through. It receives three arguments. The first two are the coordinates of the tile, the last one is the current cost for reaching that tile. The function should return a floating-point value that is the cost for entering the given tile. This cost should be greater or equal to one.&lt;br /&gt;
&lt;br /&gt;
 -- Count how many turns it would take, assuming the worst case (3 movement points per tile)&lt;br /&gt;
 local max_moves = wesnoth.get_units({ x = x1, y = y1 })[1].max_moves&lt;br /&gt;
 local path, cost = wesnoth.find_path(x1, y2, x2, y2,&lt;br /&gt;
     function(x, y, current_cost)&lt;br /&gt;
         local remaining_moves = max_moves - (current_cost % max_moves)&lt;br /&gt;
         if remaining_moves &amp;lt; 3 then current_cost = current_cost + remaining_moves end&lt;br /&gt;
         return current_cost + 3&lt;br /&gt;
     end)&lt;br /&gt;
 wesnoth.message(string.format(&amp;quot;It would take %d turns.&amp;quot;, math.ceil(cost / 3)))&lt;br /&gt;
&lt;br /&gt;
==== wesnoth.find_vacant_tile ====&lt;br /&gt;
&lt;br /&gt;
Returns the two coordinates of an empty tile the closest to the tile passed by coordinates. An optional unit (either a WML table or a proxy object) can be passed as a third argument; if so, the returned tile has terrain which is passable for the passed unit.&lt;br /&gt;
&lt;br /&gt;
 function teleport(src_x, src_y, dst_x, dst_y)&lt;br /&gt;
   local u = wesnoth.get_units({x = src_x, y = src_y })[1]&lt;br /&gt;
   local ut = u.__cfg&lt;br /&gt;
   dst_x, dst_y = wesnoth.find_vacant_tile(dst_x, dst_y, u)&lt;br /&gt;
   wesnoth.put_unit(src_x, src_y)&lt;br /&gt;
   wesnoth.put_unit(dst_x, dst_y, ut)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
==== wesnoth.find_reach ====&lt;br /&gt;
&lt;br /&gt;
Returns all the locations reachable by a unit. The unit is given either by its two coordinates or by a proxy object.  The last argument is an optional table that can be used to parametrize the pathfinder. Its options are:&lt;br /&gt;
* '''additional_turns''': if set to an integer n, the pathfinder will consider tiles that can be reached in n+1 turns&lt;br /&gt;
* '''ignore_units''': if set, the paths will go through units and ignore zones of control&lt;br /&gt;
* '''ignore_teleport''': if set, the teleport ability of the unit is ignored&lt;br /&gt;
* '''viewing_side''': if set to a valid side number, fog and shroud for this side will be taken into account; if set to an invalid number (e.g. 0), fog and shroud will be ignored; if left unset, the viewing side will be the unit side&lt;br /&gt;
The locations are stored as triples in an array. The first two elements of a triple are the coordinates of a reachable tile, the third one is the number of movement points left when reaching the tile.&lt;br /&gt;
&lt;br /&gt;
 -- overlay the number of turns needed to reach each tile&lt;br /&gt;
 local t = wesnoth.find_reach(u, { additional_turns = 8 })&lt;br /&gt;
 local m = u.max_moves&lt;br /&gt;
 for i,l in ipairs(t) do&lt;br /&gt;
   wesnoth.fire(&amp;quot;label&amp;quot;, { x = l[1], y = l[2], text = math.ceil(9 - l[3]/m) })&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
==== wesnoth.find_cost_map ====&lt;br /&gt;
&lt;br /&gt;
Builds a cost map for one, multiple units or unit types.&lt;br /&gt;
In a cost map each hex is mapped to two values:&lt;br /&gt;
a) The summed cost to reach this hex for all input units&lt;br /&gt;
b) A value which indicates how many units can reach this hex&lt;br /&gt;
The caller can divide a) with b) to get a average cost to reach this hex for the input units.&lt;br /&gt;
The costs will consider movement lost during turn changes. (So with simple calculus it is possible to get the turns to reach a hex)&lt;br /&gt;
 &lt;br /&gt;
Input arguments:&lt;br /&gt;
* 1 + 2. A units location&lt;br /&gt;
* '''OR''' 1. A unit&lt;br /&gt;
* '''OR''' 1. A unit filter&lt;br /&gt;
* 2. (optional) A array of triples (coordinates + unit type as string)&lt;br /&gt;
* 3. (optional) A table with options: ignore_units, ignore_teleport, viewing_side, debug, use_max_moves&lt;br /&gt;
* 4. (optional) A [[StandardLocationFilter|Standard Location Filter]].&lt;br /&gt;
 &lt;br /&gt;
If the array of unit types is given the units will be added to the first parameter. Use a empty filter or a invalid location to only add unit types.&lt;br /&gt;
 &lt;br /&gt;
Return value:&lt;br /&gt;
A array of quadruples (coordinates + a summed cost + reach count)&lt;br /&gt;
 &lt;br /&gt;
A location set can be build by calling location.set.of_pairs(retval).&lt;br /&gt;
&lt;br /&gt;
==== helper.distance_between ====&lt;br /&gt;
&lt;br /&gt;
Returns the distance between two tiles given by their coordinates.&lt;br /&gt;
&lt;br /&gt;
 local d = distance_between(x1, y1, x2, y2)&lt;br /&gt;
&lt;br /&gt;
==== helper.adjacent_tiles ====&lt;br /&gt;
&lt;br /&gt;
Returns an iterator on the (at most six) tiles around a given location that are on the map. If the third argument is ''true'', tiles on the map border are also visited.&lt;br /&gt;
&lt;br /&gt;
 -- remove all the units next to the (a,b) tile&lt;br /&gt;
 for x, y in helper.adjacent_tiles(a, b) do&lt;br /&gt;
     wesnoth.put_unit(x, y)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
[[Category: Lua Reference]]&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52161</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52161"/>
		<updated>2013-10-06T23:10:14Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode ''and'' the game is started with the parameter &amp;lt;code&amp;gt;--log-info=ai/recruitment&amp;lt;/code&amp;gt; the important hexes are marked with a white 'X'.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0.&lt;br /&gt;
The minimum depends on the aspect &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (recruitment_diversity * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Even if only a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tag is used, the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet will get overwritten. Include the &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; mentioned in the previous point to let the AI recruit something.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
         [recruit]         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#Do not forget to include the default&amp;lt;/span&amp;gt;&lt;br /&gt;
           importance=0    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#[recruit]-tag. Otherwise the AI won't&amp;lt;/span&amp;gt;&lt;br /&gt;
         [/recruit]        &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#recruit anything.&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it can be a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. When the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (5 hexes).&lt;br /&gt;
&lt;br /&gt;
'''Important note:''' Although the intuition says that the AI will play better when it tries to save some gold, it doesn't. Tests shown that it is always good for the AI to have as many units as possible. (Probably it will do better in village-capturing then). The aspect is activated by default because playing against wave-like recruiting is fun.&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=2: (int)''' From this turn on the aspect will be active. So the AI will always spend all gold until the defined turn. &amp;lt;code&amp;gt;active=0&amp;lt;/code&amp;gt; can be used to always deactivate gold saving. (The default value is 2 so nobody will get confused if the AI doesn't recruit in the first turn)&lt;br /&gt;
* '''begin=1.5: (double)''' See explanation above.&lt;br /&gt;
* '''end=1.1: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the AI will never switch to the state SPEND_ALL_GOLD.&lt;br /&gt;
* '''save_on_negative_income=no: (boolean)''' If this is set to yes, the AI will switch to the state SAVE_GOLD even if the income is negative. (estimation over 5 turns, village gain and unit loss estimation included).&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=2&lt;br /&gt;
         begin=1.5&lt;br /&gt;
         end=1.1&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
         save_on_negative_income=no&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
A macro can be used to deactivate recruitment_save_gold:&lt;br /&gt;
&lt;br /&gt;
 {AI_ASPECT recruitment_save_gold {AI_DEACTIVATE_SAVE_GOLD} }&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=2.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. When the value is 0.0, the AI will only recruit the best unit.(see [[AI_Recruitment#Implementation_details_for_interested_readers| above]] for implementation details).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=50 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Making recruitment strong=&lt;br /&gt;
&lt;br /&gt;
The default aspects are chosen so it is fun to play against the AI.&lt;br /&gt;
To let the AI recruit stronger this settings are recommended:&lt;br /&gt;
* '''recruitment_diversity=0.8''' Let the AI recruit the best units only.&lt;br /&gt;
* '''recruitment_randomness=0''' Let the recruitment not be random.&lt;br /&gt;
* '''villages_per_scout=0''' Test shown that the AI plays slightly better with less or no scouts at all.&lt;br /&gt;
* '''deactivate recruitment_save_gold''' (see above). Test shown that the AI plays better when it spends all money immediately.&lt;br /&gt;
&lt;br /&gt;
In Multiplayer games the AI 'Strong AI (RCA)' can be chosen where the settings are as described here.&lt;br /&gt;
&lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52160</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52160"/>
		<updated>2013-10-06T23:10:01Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* The Aspect */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode ''and'' the game is started with the parameter &amp;lt;code&amp;gt;--log-info=ai/recruitment&amp;lt;/code&amp;gt; the important hexes are marked with a white 'X'.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0.&lt;br /&gt;
The minimum depends on the aspect &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (recruitment_diversity * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Even if only a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tag is used, the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet will get overwritten. Include the &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; mentioned in the previous point to let the AI recruit something.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
         [recruit]         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#Do not forget to include the default&amp;lt;/span&amp;gt;&lt;br /&gt;
           importance=0    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#[recruit]-tag. Otherwise the AI won't&amp;lt;/span&amp;gt;&lt;br /&gt;
         [/recruit]        &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#recruit anything.&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it can be a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. When the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (5 hexes).&lt;br /&gt;
&lt;br /&gt;
'''Important note:''' Although the intuition says that the AI will play better when it tries to save some gold, it doesn't. Tests shown that it is always good for the AI to have as many units as possible. (Probably it will do better in village-capturing then). The aspect is activated by default because playing against wave-like recruiting is fun.&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=2: (int)''' From this turn on the aspect will be active. So the AI will always spend all gold until the defined turn. &amp;lt;code&amp;gt;active=0&amp;lt;/code&amp;gt; can be used to always deactivate gold saving. (The default value is 2 so nobody will get confused if the AI doesn't recruit in the first turn)&lt;br /&gt;
* '''begin=1.5: (double)''' See explanation above.&lt;br /&gt;
* '''end=1.1: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the AI will never switch to the state SPEND_ALL_GOLD.&lt;br /&gt;
* '''save_on_negative_income=no: (boolean)''' If this is set to yes, the AI will switch to the state SAVE_GOLD even if the income is negative. (estimation over 5 turns, village gain and unit loss estimation included).&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=2&lt;br /&gt;
         begin=1.1&lt;br /&gt;
         end=0.9&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
         save_on_negative_income=no&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
A macro can be used to deactivate recruitment_save_gold:&lt;br /&gt;
&lt;br /&gt;
 {AI_ASPECT recruitment_save_gold {AI_DEACTIVATE_SAVE_GOLD} }&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=2.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. When the value is 0.0, the AI will only recruit the best unit.(see [[AI_Recruitment#Implementation_details_for_interested_readers| above]] for implementation details).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=50 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Making recruitment strong=&lt;br /&gt;
&lt;br /&gt;
The default aspects are chosen so it is fun to play against the AI.&lt;br /&gt;
To let the AI recruit stronger this settings are recommended:&lt;br /&gt;
* '''recruitment_diversity=0.8''' Let the AI recruit the best units only.&lt;br /&gt;
* '''recruitment_randomness=0''' Let the recruitment not be random.&lt;br /&gt;
* '''villages_per_scout=0''' Test shown that the AI plays slightly better with less or no scouts at all.&lt;br /&gt;
* '''deactivate recruitment_save_gold''' (see above). Test shown that the AI plays better when it spends all money immediately.&lt;br /&gt;
&lt;br /&gt;
In Multiplayer games the AI 'Strong AI (RCA)' can be chosen where the settings are as described here.&lt;br /&gt;
&lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52159</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52159"/>
		<updated>2013-10-06T23:00:14Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode ''and'' the game is started with the parameter &amp;lt;code&amp;gt;--log-info=ai/recruitment&amp;lt;/code&amp;gt; the important hexes are marked with a white 'X'.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0.&lt;br /&gt;
The minimum depends on the aspect &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (recruitment_diversity * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Even if only a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tag is used, the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet will get overwritten. Include the &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; mentioned in the previous point to let the AI recruit something.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
         [recruit]         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#Do not forget to include the default&amp;lt;/span&amp;gt;&lt;br /&gt;
           importance=0    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#[recruit]-tag. Otherwise the AI won't&amp;lt;/span&amp;gt;&lt;br /&gt;
         [/recruit]        &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#recruit anything.&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it can be a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. When the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (5 hexes).&lt;br /&gt;
&lt;br /&gt;
'''Important note:''' Although the intuition says that the AI will play better when it tries to save some gold, it doesn't. Tests shown that it is always good for the AI to have as many units as possible. (Probably it will do better in village-capturing then). The aspect is activated by default because playing against wave-like recruiting is fun.&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=2: (int)''' From this turn on the aspect will be active. So the AI will always spend all gold until the defined turn. &amp;lt;code&amp;gt;active=0&amp;lt;/code&amp;gt; can be used to always deactivate gold saving. (The default value is 2 so nobody will get confused if the AI doesn't recruit in the first turn)&lt;br /&gt;
* '''begin=1.1: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.9: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the AI will never switch to the state SPEND_ALL_GOLD.&lt;br /&gt;
* '''save_on_negative_income=no: (boolean)''' If this is set to yes, the AI will switch to the state SAVE_GOLD even if the income is negative. (estimation over 5 turns, village gain and unit loss estimation included).&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=2&lt;br /&gt;
         begin=1.1&lt;br /&gt;
         end=0.9&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
         save_on_negative_income=no&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
A macro can be used to deactivate recruitment_save_gold:&lt;br /&gt;
&lt;br /&gt;
 {AI_ASPECT recruitment_save_gold {AI_DEACTIVATE_SAVE_GOLD} }&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=2.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. When the value is 0.0, the AI will only recruit the best unit.(see [[AI_Recruitment#Implementation_details_for_interested_readers| above]] for implementation details).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=50 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Making recruitment strong=&lt;br /&gt;
&lt;br /&gt;
The default aspects are chosen so it is fun to play against the AI.&lt;br /&gt;
To let the AI recruit stronger this settings are recommended:&lt;br /&gt;
* '''recruitment_diversity=0.8''' Let the AI recruit the best units only.&lt;br /&gt;
* '''recruitment_randomness=0''' Let the recruitment not be random.&lt;br /&gt;
* '''villages_per_scout=0''' Test shown that the AI plays slightly better with less or no scouts at all.&lt;br /&gt;
* '''deactivate recruitment_save_gold''' (see above). Test shown that the AI plays better when it spends all money immediately.&lt;br /&gt;
&lt;br /&gt;
In Multiplayer games the AI 'Strong AI (RCA)' can be chosen where the settings are as described here.&lt;br /&gt;
&lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52158</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52158"/>
		<updated>2013-10-06T22:57:01Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* The Aspect */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode ''and'' the game is started with the parameter &amp;lt;code&amp;gt;--log-info=ai/recruitment&amp;lt;/code&amp;gt; the important hexes are marked with a white 'X'.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0.&lt;br /&gt;
The minimum depends on the aspect &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (recruitment_diversity * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Even if only a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tag is used, the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet will get overwritten. Include the &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; mentioned in the previous point to let the AI recruit something.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
         [recruit]         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#Do not forget to include the default&amp;lt;/span&amp;gt;&lt;br /&gt;
           importance=0    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#[recruit]-tag. Otherwise the AI won't&amp;lt;/span&amp;gt;&lt;br /&gt;
         [/recruit]        &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#recruit anything.&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it can be a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. When the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (5 hexes).&lt;br /&gt;
&lt;br /&gt;
'''Important note:''' Although the intuition says that the AI will play better when it tries to save some gold, it doesn't. Tests shown that it is always good for the AI to have as many units as possible. (Probably it will do better in village-capturing then). The aspect is activated by default because playing against wave-like recruiting is fun.&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=2: (int)''' From this turn on the aspect will be active. So the AI will always spend all gold until the defined turn. &amp;lt;code&amp;gt;active=0&amp;lt;/code&amp;gt; can be used to always deactivate gold saving. (The default value is 2 so nobody will get confused if the AI doesn't recruit in the first turn)&lt;br /&gt;
* '''begin=1.1: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.9: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the AI will never switch to the state SPEND_ALL_GOLD.&lt;br /&gt;
* '''save_on_negative_income=no: (boolean)''' If this is set to yes, the AI will switch to the state SAVE_GOLD even if the income is negative. (estimation over 5 turns, village gain and unit loss estimation included).&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=2&lt;br /&gt;
         begin=1.1&lt;br /&gt;
         end=0.9&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
A macro can be used to deactivate recruitment_save_gold:&lt;br /&gt;
&lt;br /&gt;
 {AI_ASPECT recruitment_save_gold {AI_DEACTIVATE_SAVE_GOLD} }&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=2.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. When the value is 0.0, the AI will only recruit the best unit.(see [[AI_Recruitment#Implementation_details_for_interested_readers| above]] for implementation details).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=50 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Making recruitment strong=&lt;br /&gt;
&lt;br /&gt;
The default aspects are chosen so it is fun to play against the AI.&lt;br /&gt;
To let the AI recruit stronger this settings are recommended:&lt;br /&gt;
* '''recruitment_diversity=0.8''' Let the AI recruit the best units only.&lt;br /&gt;
* '''recruitment_randomness=0''' Let the recruitment not be random.&lt;br /&gt;
* '''villages_per_scout=0''' Test shown that the AI plays slightly better with less or no scouts at all.&lt;br /&gt;
* '''deactivate recruitment_save_gold''' (see above). Test shown that the AI plays better when it spends all money immediately.&lt;br /&gt;
&lt;br /&gt;
In Multiplayer games the AI 'Strong AI (RCA)' can be chosen where the settings are as described here.&lt;br /&gt;
&lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=EasyCoding&amp;diff=52155</id>
		<title>EasyCoding</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=EasyCoding&amp;diff=52155"/>
		<updated>2013-10-04T15:18:15Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Improvements to AI */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Foreword ==&lt;br /&gt;
&lt;br /&gt;
'''We are currently in the process of 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 I'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;
--[[User:Boucman|Boucman]] 20:48, 3 October 2006 (CEST)&lt;br /&gt;
&lt;br /&gt;
== MP related features ==&lt;br /&gt;
=== Add possibility to give reason for &amp;quot;ignore&amp;quot; ===&lt;br /&gt;
As per FR #16001 [https://gna.org/bugs/?16001]&lt;br /&gt;
&lt;br /&gt;
* See https://github.com/wesnoth/wesnoth-old/pull/1&lt;br /&gt;
&lt;br /&gt;
=== Add possibility to switch off dependency checking ===&lt;br /&gt;
MP add-ons 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;
== WML related features ==&lt;br /&gt;
&lt;br /&gt;
=== Side-specific results ===&lt;br /&gt;
Giving result=defeat or result=victory for specific sides. ([http://gna.org/bugs/index.php?4960 FR #4960]) -- [[User:dlr365|dlr365]] -- patch submitted [https://gna.org/bugs/index.php?4960]&lt;br /&gt;
&lt;br /&gt;
--[[User:Boucman|Boucman]] 08:58, 28 February 2010 (UTC) Patch seems abandonned, but can be used for further work feel free to take over the FR&lt;br /&gt;
&lt;br /&gt;
=== Support for leaderless multiplayergames ===&lt;br /&gt;
Add support for the WML key victory_when_enemies_defeated= in the scenario tag during multiplayergames. ([https://gna.org/bugs/index.php?8106 FR #8106])&lt;br /&gt;
&lt;br /&gt;
=== Support variable recall cost in wml ===&lt;br /&gt;
see https://gna.org/bugs/?16538&lt;br /&gt;
&lt;br /&gt;
the syntax needs to be discussed and refined, but the overall idea is good&lt;br /&gt;
&lt;br /&gt;
make sure to update whiteboard to handle this correctly&lt;br /&gt;
&lt;br /&gt;
Ask Boucman&lt;br /&gt;
&lt;br /&gt;
=== Other Ideas ===&lt;br /&gt;
See [[FutureWML]]; some ideas there are easier than others.&lt;br /&gt;
&lt;br /&gt;
== Improvements to AI ==&lt;br /&gt;
&lt;br /&gt;
Note: not all of these are simple tasks.  Talk to Crab or mattsc.&lt;br /&gt;
&lt;br /&gt;
* Make external CAs fully usable.  This requires adding a persistent data variable that is accessible across the CAs and being able to pass parameters to the CA eval and exec functions.&lt;br /&gt;
&lt;br /&gt;
* Add option to take avoided hexes into account in wesnoth.find_path()&lt;br /&gt;
&lt;br /&gt;
* Make the (old) simple aspect syntax work when the Lua engine is defined for a side.  Setting aspects in the [side] tag like this:&lt;br /&gt;
 [ai]&lt;br /&gt;
     aggression=0.123&lt;br /&gt;
 [/ai]&lt;br /&gt;
does not work if the Lua engine is defined. You either need to use the [aspect] tag in the [side] tag with 'engine=lua', or use the code above in a [modify_side] tag in an event.  Making the above syntax available (as it is with the c++ engine) would be nice.  The same also applies to the FAI engine and to aspects set for AI sides in MP games.&lt;br /&gt;
&lt;br /&gt;
* Add more replay-safe actions to the Lua AI actions.  A first step could be to add an AI action (and add lua function to do that) to use a right-click menu item.&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;
* 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.  (This is not a simple task.  Some first attempts to do it have been made, but work seems abandoned at the moment.)&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;
== GUI related features ==&lt;br /&gt;
-------------------&lt;br /&gt;
&lt;br /&gt;
Note at the moment Mordante is working on a new GUI system, these &lt;br /&gt;
changes will probably affect the way these items need to be implemented.&lt;br /&gt;
Contact Mordante on IRC before starting to work on these.&lt;br /&gt;
  &lt;br /&gt;
--[[User:SkeletonCrew|SkeletonCrew]] 14:04, 9 March 2008 (EDT)&lt;br /&gt;
&lt;br /&gt;
=== Theme Changes ===&lt;br /&gt;
&lt;br /&gt;
* hide the hourglass item from the statusbar when there is no timer&lt;br /&gt;
&lt;br /&gt;
=== Widget Changes ===&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;
* input history (chat, commands, ..)&lt;br /&gt;
&lt;br /&gt;
== GUI2 related features ==&lt;br /&gt;
GUI2 is the new gui engine Mordante/SkeletonCrew is working on. &lt;br /&gt;
* Information on the wiki can be found here http://www.wesnoth.org/wiki/GUIToolkit&lt;br /&gt;
* The source code is under src/gui/&lt;br /&gt;
* The configuration config files are under data/gui/default&lt;br /&gt;
&lt;br /&gt;
Some tasks need the --new-widgets since the code is only shown in the experimental mode. Tasks which need this switch have the * in the title.&lt;br /&gt;
&lt;br /&gt;
=== Port the random map generator dialog to GUI2 ===&lt;br /&gt;
The map generator currently doesn't use the GUI2 framework, but there's no reason why it couldn't. Ask lipk (forum)/lipkab (IRC).&lt;br /&gt;
&lt;br /&gt;
== Miscellaneous ==&lt;br /&gt;
&lt;br /&gt;
=== More powerful village naming ===&lt;br /&gt;
'''Adding mountain names and other features to village names, having a second random name in village names'''&lt;br /&gt;
&lt;br /&gt;
Currently the village naming engine has a very good structure that could allow &lt;br /&gt;
more powerfull names to be generated. &lt;br /&gt;
Understanding how it works should be quite easy, and a few usefull improvements could be added.&lt;br /&gt;
&lt;br /&gt;
* Currently villages can use lake names and river names, this should be extended to other features like bridges, swamps, mountains etc...&lt;br /&gt;
* It would be nice to have a separate list of &amp;quot;first sylabus&amp;quot; and &amp;quot;last sylabus&amp;quot; for naming. That's not really needed in english, but some translations could use it&lt;br /&gt;
* Again, it is common to have villages with more than one &amp;quot;random&amp;quot; word in them. having a $name2 variable would be nice&lt;br /&gt;
&lt;br /&gt;
=== Debug Mode ===&lt;br /&gt;
* New debug command functionality (setting additional status.variables, possibly terrain)&lt;br /&gt;
* Add a right-click option in debug mode to kill the unit under the cursor ([[User:PL_kolek|PL_kolek]], Patch submitted [https://gna.org/patch/index.php?3905])&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>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52154</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52154"/>
		<updated>2013-10-04T13:12:52Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* How it works */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode ''and'' the game is started with the parameter &amp;lt;code&amp;gt;--log-info=ai/recruitment&amp;lt;/code&amp;gt; the important hexes are marked with a white 'X'.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0.&lt;br /&gt;
The minimum depends on the aspect &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (recruitment_diversity * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Even if only a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tag is used, the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet will get overwritten. Include the &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; mentioned in the previous point to let the AI recruit something.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
         [recruit]         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#Do not forget to include the default&amp;lt;/span&amp;gt;&lt;br /&gt;
           importance=0    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#[recruit]-tag. Otherwise the AI won't&amp;lt;/span&amp;gt;&lt;br /&gt;
         [/recruit]        &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#recruit anything.&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it can be a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. When the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (5 hexes).&lt;br /&gt;
&lt;br /&gt;
'''Important note:''' Although the intuition says that the AI will play better when it tries to save some gold, it doesn't. Tests shown that it is always good for the AI to have as many units as possible. (Probably it will do better in village-capturing then). The aspect is activated by default because playing against wave-like recruiting is fun.&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=2: (int)''' From this turn on the aspect will be active. So the AI will always spend all gold until the defined turn. &amp;lt;code&amp;gt;active=0&amp;lt;/code&amp;gt; can be used to always deactivate gold saving. (The default value is 2 so nobody will get confused if the AI doesn't recruit in the first turn)&lt;br /&gt;
* '''begin=1.1: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.9: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the AI will never switch to the state SPEND_ALL_GOLD.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=2&lt;br /&gt;
         begin=1.1&lt;br /&gt;
         end=0.9&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
A macro can be used to deactivate recruitment_save_gold:&lt;br /&gt;
&lt;br /&gt;
 {AI_ASPECT recruitment_save_gold {AI_DEACTIVATE_SAVE_GOLD} }&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=2.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. When the value is 0.0, the AI will only recruit the best unit.(see [[AI_Recruitment#Implementation_details_for_interested_readers| above]] for implementation details).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=50 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Making recruitment strong=&lt;br /&gt;
&lt;br /&gt;
The default aspects are chosen so it is fun to play against the AI.&lt;br /&gt;
To let the AI recruit stronger this settings are recommended:&lt;br /&gt;
* '''recruitment_diversity=0.8''' Let the AI recruit the best units only.&lt;br /&gt;
* '''recruitment_randomness=0''' Let the recruitment not be random.&lt;br /&gt;
* '''villages_per_scout=0''' Test shown that the AI plays slightly better with less or no scouts at all.&lt;br /&gt;
* '''deactivate recruitment_save_gold''' (see above). Test shown that the AI plays better when it spends all money immediately.&lt;br /&gt;
&lt;br /&gt;
In Multiplayer games the AI 'Strong AI (RCA)' can be chosen where the settings are as described here.&lt;br /&gt;
&lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52153</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52153"/>
		<updated>2013-10-04T13:12:01Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* The Aspect */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode ''and'' the game is started with the parameter &amp;lt;code&amp;gt;--log-info=ai/recruitment&amp;lt;/code&amp;gt; the important hexes are marked with a white 'X'.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0.&lt;br /&gt;
The minimum depends on the aspect &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (recruitment_diversity * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Even if only a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tag is used, the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet will get overwritten. Include the &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; mentioned in the previous point to let the AI recruit something.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
         [recruit]         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#Do not forget to include the default&amp;lt;/span&amp;gt;&lt;br /&gt;
           importance=0    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#[recruit]-tag. Otherwise the AI won't&amp;lt;/span&amp;gt;&lt;br /&gt;
         [/recruit]        &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#recruit anything.&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it can be a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (5 hexes).&lt;br /&gt;
&lt;br /&gt;
'''Important note:''' Although the intuition says that the AI will play better when it tries to save some gold, it doesn't. Tests shown that it is always good for the AI to have as many units as possible. (Probably it will do better in village-capturing then). The aspect is activated by default because playing against wave-like recruiting is fun.&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=2: (int)''' From this turn on the aspect will be active. So the AI will always spend all gold until the defined turn. &amp;lt;code&amp;gt;active=0&amp;lt;/code&amp;gt; can be used to always deactivate gold saving. (The default value is 2 so nobody will get confused if the AI doesn't recruit in the first turn)&lt;br /&gt;
* '''begin=1.1: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.9: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the AI will never switch to the state SPEND_ALL_GOLD.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=2&lt;br /&gt;
         begin=1.1&lt;br /&gt;
         end=0.9&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
A macro can be used to deactivate recruitment_save_gold:&lt;br /&gt;
&lt;br /&gt;
 {AI_ASPECT recruitment_save_gold {AI_DEACTIVATE_SAVE_GOLD} }&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=2.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. When the value is 0.0, the AI will only recruit the best unit.(see [[AI_Recruitment#Implementation_details_for_interested_readers| above]] for implementation details).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=50 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Making recruitment strong=&lt;br /&gt;
&lt;br /&gt;
The default aspects are chosen so it is fun to play against the AI.&lt;br /&gt;
To let the AI recruit stronger this settings are recommended:&lt;br /&gt;
* '''recruitment_diversity=0.8''' Let the AI recruit the best units only.&lt;br /&gt;
* '''recruitment_randomness=0''' Let the recruitment not be random.&lt;br /&gt;
* '''villages_per_scout=0''' Test shown that the AI plays slightly better with less or no scouts at all.&lt;br /&gt;
* '''deactivate recruitment_save_gold''' (see above). Test shown that the AI plays better when it spends all money immediately.&lt;br /&gt;
&lt;br /&gt;
In Multiplayer games the AI 'Strong AI (RCA)' can be chosen where the settings are as described here.&lt;br /&gt;
&lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52152</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52152"/>
		<updated>2013-10-04T13:10:28Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Map analyis */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode ''and'' the game is started with the parameter &amp;lt;code&amp;gt;--log-info=ai/recruitment&amp;lt;/code&amp;gt; the important hexes are marked with a white 'X'.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0.&lt;br /&gt;
The minimum depends on the aspect &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (recruitment_diversity * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Even if only a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tag is used, the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet will get overwritten. Include the &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; mentioned in the previous point to let the AI recruit something.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
         [recruit]         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#Do not forget to include the default&amp;lt;/span&amp;gt;&lt;br /&gt;
           importance=0    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#[recruit]-tag. Otherwise the AI won't&amp;lt;/span&amp;gt;&lt;br /&gt;
         [/recruit]        &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#recruit anything.&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it can be a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (5 hexes).&lt;br /&gt;
&lt;br /&gt;
'''Important note:''' Although the intuition says that the AI will play better when it tries to save some gold, it doesn't. Tests shown that it is always good for the AI to have as many units as possible. (Probably it will do better in village-capturing then). The aspect is activated by default because playing against wave-like recruiting is fun.&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=2: (int)''' From this turn on the aspect will be active. So the AI will always spend all gold until the defined turn. &amp;lt;code&amp;gt;active=0&amp;lt;/code&amp;gt; can be used to always deactivate gold saving. (The default value is 2 so nobody will get confused if the AI doesn't recruit in the first turn)&lt;br /&gt;
* '''begin=1.1: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.9: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=2&lt;br /&gt;
         begin=1.1&lt;br /&gt;
         end=0.9&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
A macro can be used to deactivate recruitment_save_gold:&lt;br /&gt;
&lt;br /&gt;
 {AI_ASPECT recruitment_save_gold {AI_DEACTIVATE_SAVE_GOLD} }&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=2.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. When the value is 0.0, the AI will only recruit the best unit.(see [[AI_Recruitment#Implementation_details_for_interested_readers| above]] for implementation details).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=50 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Making recruitment strong=&lt;br /&gt;
&lt;br /&gt;
The default aspects are chosen so it is fun to play against the AI.&lt;br /&gt;
To let the AI recruit stronger this settings are recommended:&lt;br /&gt;
* '''recruitment_diversity=0.8''' Let the AI recruit the best units only.&lt;br /&gt;
* '''recruitment_randomness=0''' Let the recruitment not be random.&lt;br /&gt;
* '''villages_per_scout=0''' Test shown that the AI plays slightly better with less or no scouts at all.&lt;br /&gt;
* '''deactivate recruitment_save_gold''' (see above). Test shown that the AI plays better when it spends all money immediately.&lt;br /&gt;
&lt;br /&gt;
In Multiplayer games the AI 'Strong AI (RCA)' can be chosen where the settings are as described here.&lt;br /&gt;
&lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52151</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52151"/>
		<updated>2013-10-04T13:10:05Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Map analyis */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode ''and'' the game is started with the parameter &amp;quot;--log-info=ai/recruitment&amp;quot; the important hexes are marked with a white 'X'.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0.&lt;br /&gt;
The minimum depends on the aspect &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (recruitment_diversity * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Even if only a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tag is used, the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet will get overwritten. Include the &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; mentioned in the previous point to let the AI recruit something.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
         [recruit]         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#Do not forget to include the default&amp;lt;/span&amp;gt;&lt;br /&gt;
           importance=0    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#[recruit]-tag. Otherwise the AI won't&amp;lt;/span&amp;gt;&lt;br /&gt;
         [/recruit]        &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#recruit anything.&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it can be a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (5 hexes).&lt;br /&gt;
&lt;br /&gt;
'''Important note:''' Although the intuition says that the AI will play better when it tries to save some gold, it doesn't. Tests shown that it is always good for the AI to have as many units as possible. (Probably it will do better in village-capturing then). The aspect is activated by default because playing against wave-like recruiting is fun.&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=2: (int)''' From this turn on the aspect will be active. So the AI will always spend all gold until the defined turn. &amp;lt;code&amp;gt;active=0&amp;lt;/code&amp;gt; can be used to always deactivate gold saving. (The default value is 2 so nobody will get confused if the AI doesn't recruit in the first turn)&lt;br /&gt;
* '''begin=1.1: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.9: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=2&lt;br /&gt;
         begin=1.1&lt;br /&gt;
         end=0.9&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
A macro can be used to deactivate recruitment_save_gold:&lt;br /&gt;
&lt;br /&gt;
 {AI_ASPECT recruitment_save_gold {AI_DEACTIVATE_SAVE_GOLD} }&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=2.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. When the value is 0.0, the AI will only recruit the best unit.(see [[AI_Recruitment#Implementation_details_for_interested_readers| above]] for implementation details).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=50 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Making recruitment strong=&lt;br /&gt;
&lt;br /&gt;
The default aspects are chosen so it is fun to play against the AI.&lt;br /&gt;
To let the AI recruit stronger this settings are recommended:&lt;br /&gt;
* '''recruitment_diversity=0.8''' Let the AI recruit the best units only.&lt;br /&gt;
* '''recruitment_randomness=0''' Let the recruitment not be random.&lt;br /&gt;
* '''villages_per_scout=0''' Test shown that the AI plays slightly better with less or no scouts at all.&lt;br /&gt;
* '''deactivate recruitment_save_gold''' (see above). Test shown that the AI plays better when it spends all money immediately.&lt;br /&gt;
&lt;br /&gt;
In Multiplayer games the AI 'Strong AI (RCA)' can be chosen where the settings are as described here.&lt;br /&gt;
&lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52148</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52148"/>
		<updated>2013-10-03T18:52:11Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* How it works */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white 'X'.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0.&lt;br /&gt;
The minimum depends on the aspect &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (recruitment_diversity * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Even if only a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tag is used, the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet will get overwritten. Include the &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; mentioned in the previous point to let the AI recruit something.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
         [recruit]         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#Do not forget to include the default&amp;lt;/span&amp;gt;&lt;br /&gt;
           importance=0    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#[recruit]-tag. Otherwise the AI won't&amp;lt;/span&amp;gt;&lt;br /&gt;
         [/recruit]        &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#recruit anything.&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it can be a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (5 hexes).&lt;br /&gt;
&lt;br /&gt;
'''Important note:''' Although the intuition says that the AI will play better when it tries to save some gold, it doesn't. Tests shown that it is always good for the AI to have as many units as possible. (Probably it will do better in village-capturing then). The aspect is activated by default because playing against wave-like recruiting is fun.&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=2: (int)''' From this turn on the aspect will be active. So the AI will always spend all gold until the defined turn. &amp;lt;code&amp;gt;active=0&amp;lt;/code&amp;gt; can be used to always deactivate gold saving. (The default value is 2 so nobody will get confused if the AI doesn't recruit in the first turn)&lt;br /&gt;
* '''begin=1.1: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.9: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=2&lt;br /&gt;
         begin=1.1&lt;br /&gt;
         end=0.9&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
A macro can be used to deactivate recruitment_save_gold:&lt;br /&gt;
&lt;br /&gt;
 {AI_ASPECT recruitment_save_gold {AI_DEACTIVATE_SAVE_GOLD} }&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=2.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. When the value is 0.0, the AI will only recruit the best unit.(see [[AI_Recruitment#Implementation_details_for_interested_readers| above]] for implementation details).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=50 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Making recruitment strong=&lt;br /&gt;
&lt;br /&gt;
The default aspects are chosen so it is fun to play against the AI.&lt;br /&gt;
To let the AI recruit stronger this settings are recommended:&lt;br /&gt;
* '''recruitment_diversity=0.8''' Let the AI recruit the best units only.&lt;br /&gt;
* '''recruitment_randomness=0''' Let the recruitment not be random.&lt;br /&gt;
* '''villages_per_scout=0''' Test shown that the AI plays slightly better with less or no scouts at all.&lt;br /&gt;
* '''deactivate recruitment_save_gold''' (see above). Test shown that the AI plays better when it spends all money immediately.&lt;br /&gt;
&lt;br /&gt;
In Multiplayer games the AI 'Strong AI (RCA)' can be chosen where the settings are as described here.&lt;br /&gt;
&lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52131</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52131"/>
		<updated>2013-09-26T15:40:03Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Aspect recruitment_instruction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white 'X'.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0.&lt;br /&gt;
The minimum depends on the aspect &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (recruitment_diversity * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Even if only a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tag is used, the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet will get overwritten. Include the &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; mentioned in the previous point to let the AI recruit something.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
         [recruit]         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#Do not forget to include the default&amp;lt;/span&amp;gt;&lt;br /&gt;
           importance=0    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#[recruit]-tag. Otherwise the AI won't&amp;lt;/span&amp;gt;&lt;br /&gt;
         [/recruit]        &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#recruit anything.&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it can be a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
'''Important note:''' Although the intuition says that the AI will play better when it tries to save some gold, it doesn't. Tests shown that it is always good for the AI to have as many units as possible. (Probably it will do better in village-capturing then). The aspect is activated by default because playing against wave-like recruiting is fun.&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=2: (int)''' From this turn on the aspect will be active. So the AI will always spend all gold until the defined turn. &amp;lt;code&amp;gt;active=0&amp;lt;/code&amp;gt; can be used to always deactivate gold saving. (The default value is 2 so nobody will get confused if the AI doesn't recruit in the first turn)&lt;br /&gt;
* '''begin=1.1: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.9: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=2&lt;br /&gt;
         begin=1.1&lt;br /&gt;
         end=0.9&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
A macro can be used to deactivate recruitment_save_gold:&lt;br /&gt;
&lt;br /&gt;
 {AI_ASPECT recruitment_save_gold {AI_DEACTIVATE_SAVE_GOLD} }&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=2.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. When the value is 0.0, the AI will only recruit the best unit.(see [[AI_Recruitment#Implementation_details_for_interested_readers| above]] for implementation details).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=50 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Making recruitment strong=&lt;br /&gt;
&lt;br /&gt;
The default aspects are chosen so it is fun to play against the AI.&lt;br /&gt;
To let the AI recruit stronger this settings are recommended:&lt;br /&gt;
* '''recruitment_diversity=0.8''' Let the AI recruit the best units only.&lt;br /&gt;
* '''recruitment_randomness=0''' Let the recruitment not be random.&lt;br /&gt;
* '''villages_per_scout=0''' Test shown that the AI plays slightly better with less or no scouts at all.&lt;br /&gt;
* '''deactivate recruitment_save_gold''' (see above). Test shown that the AI plays better when it spends all money immediately.&lt;br /&gt;
&lt;br /&gt;
In Multiplayer games the AI 'Strong AI (RCA)' can be chosen where the settings are as described here.&lt;br /&gt;
&lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52130</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52130"/>
		<updated>2013-09-26T13:56:17Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* How it works */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white 'X'.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0.&lt;br /&gt;
The minimum depends on the aspect &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (recruitment_diversity * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Even if only a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tag is used, the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet will get overwritten. Include the &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; mentioned in the previous point to let the AI recruit something.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
         [recruit]         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#Do not forget to include the default&amp;lt;/span&amp;gt;&lt;br /&gt;
           importance=0    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#[recruit]-tag. Otherwise the AI won't&amp;lt;/span&amp;gt;&lt;br /&gt;
         [/recruit]        &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#recruit anything.&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it can be a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
'''Important note:''' Although the intuition says that the AI will play better when it tries to save some gold, it doesn't. Tests shown that it is always good for the AI to have as many units as possible. (Probably it will do better in village-capturing then). The aspect is activated by default because playing against wave-like recruiting is fun.&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=2: (int)''' From this turn on the aspect will be active. So the AI will always spend all gold until the defined turn. &amp;lt;code&amp;gt;active=0&amp;lt;/code&amp;gt; can be used to always deactivate gold saving. (The default value is 2 so nobody will get confused if the AI doesn't recruit in the first turn)&lt;br /&gt;
* '''begin=1.1: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.9: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=2&lt;br /&gt;
         begin=1.1&lt;br /&gt;
         end=0.9&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
A macro can be used to deactivate recruitment_save_gold:&lt;br /&gt;
&lt;br /&gt;
 {AI_ASPECT recruitment_save_gold {AI_DEACTIVATE_SAVE_GOLD} }&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=2.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. When the value is 0.0, the AI will only recruit the best unit.(see [[AI_Recruitment#Implementation_details_for_interested_readers| above]] for implementation details).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=50 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Making recruitment strong=&lt;br /&gt;
&lt;br /&gt;
The default aspects are chosen so it is fun to play against the AI.&lt;br /&gt;
To let the AI recruit stronger this settings are recommended:&lt;br /&gt;
* '''recruitment_diversity=0.8''' Let the AI recruit the best units only.&lt;br /&gt;
* '''recruitment_randomness=0''' Let the recruitment not be random.&lt;br /&gt;
* '''villages_per_scout=0''' Test shown that the AI plays slightly better with less or no scouts at all.&lt;br /&gt;
* '''deactivate recruitment_save_gold''' (see above). Test shown that the AI plays better when it spends all money immediately.&lt;br /&gt;
&lt;br /&gt;
In Multiplayer games the AI 'Strong AI (RCA)' can be chosen where the settings are as described here.&lt;br /&gt;
&lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52129</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52129"/>
		<updated>2013-09-26T13:52:31Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white 'X'.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0.&lt;br /&gt;
The minimum depends on the aspect &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (recruitment_diversity * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Even if only a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tag is used, the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet will get overwritten. Include the &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; mentioned in the previous point to let the AI recruit something.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
         [recruit]         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#Do not forget to include the default&amp;lt;/span&amp;gt;&lt;br /&gt;
           importance=0    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#[recruit]-tag. Otherwise the AI won't&amp;lt;/span&amp;gt;&lt;br /&gt;
         [/recruit]        &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#recruit anything.&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it is a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
'''Important note:''' Although the intuition says that the AI will play better when it tries to save some gold, it doesn't. Tests shown that it is always good for the AI to have as many units as possible. (Probably it will do better in village-capturing then). The aspect is activated by default because playing against wave-like recruiting is fun. &lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=2: (int)''' From this turn on the aspect will be active. So the AI will always spend all gold until the defined turn. &amp;lt;code&amp;gt;active=0&amp;lt;/code&amp;gt; can be used to always deactivate gold saving. (The default value is 2 so nobody will get confused if the AI doesn't recruit in the first turn)&lt;br /&gt;
* '''begin=1.1: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.9: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=2&lt;br /&gt;
         begin=1.1&lt;br /&gt;
         end=0.9&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
A macro can be used to deactivate recruitment_save_gold:&lt;br /&gt;
&lt;br /&gt;
 {AI_ASPECT recruitment_save_gold {AI_DEACTIVATE_SAVE_GOLD} }&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=2.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. When the value is 0.0, the AI will only recruit the best unit.(see [[AI_Recruitment#Implementation_details_for_interested_readers| above]] for implementation details).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=50 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Making recruitment strong=&lt;br /&gt;
&lt;br /&gt;
The default aspects are chosen so it is fun to play against the AI.&lt;br /&gt;
To let the AI recruit stronger this settings are recommended:&lt;br /&gt;
* '''recruitment_diversity=0.8''' Let the AI recruit the best units only.&lt;br /&gt;
* '''recruitment_randomness=0''' Let the recruitment not be random.&lt;br /&gt;
* '''villages_per_scout=0''' Test shown that the AI plays slightly better with less or no scouts at all.&lt;br /&gt;
* '''deactivate recruitment_save_gold''' (see above). Test shown that the AI plays better when it spends all money immediately.&lt;br /&gt;
&lt;br /&gt;
In Multiplayer games the AI 'Strong AI (RCA)' can be chosen where the settings are as described here.&lt;br /&gt;
&lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52128</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52128"/>
		<updated>2013-09-26T13:50:42Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Making recruitment strong */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white 'X'.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0.&lt;br /&gt;
The minimum depends on the aspect &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (recruitment_diversity * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Even if only a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tag is used, the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet will get overwritten. Include the &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; mentioned in the previous point to let the AI recruit something.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
         [recruit]         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#Do not forget to include the default&amp;lt;/span&amp;gt;&lt;br /&gt;
           importance=0    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#[recruit]-tag. Otherwise the AI won't&amp;lt;/span&amp;gt;&lt;br /&gt;
         [/recruit]        &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#recruit anything.&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it is a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
'''Important note:''' Although the intuition says that the AI will play better when it tries to save some gold, it doesn't. Tests shown that it is always good for the AI to have as many units as possible. (Probably it will do better in village-capturing then). The aspect is activated by default because playing against wave-like recruiting is fun. &lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=2: (int)''' From this turn on the aspect will be active. So the AI will always spend all gold until the defined turn. &amp;lt;code&amp;gt;active=0&amp;lt;/code&amp;gt; can be used to always deactivate gold saving. (The default value is 2 so nobody will get confused if the AI doesn't recruit in the first turn)&lt;br /&gt;
* '''begin=1.1: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.9: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=2&lt;br /&gt;
         begin=1.1&lt;br /&gt;
         end=0.9&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=2.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. When the value is 0.0, the AI will only recruit the best unit.(see [[AI_Recruitment#Implementation_details_for_interested_readers| above]] for implementation details).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=50 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Making recruitment strong=&lt;br /&gt;
&lt;br /&gt;
The default aspects are chosen so it is fun to play against the AI.&lt;br /&gt;
To let the AI recruit stronger this settings are recommended:&lt;br /&gt;
* '''recruitment_diversity=0.8''' Let the AI recruit the best units only.&lt;br /&gt;
* '''recruitment_randomness=0''' Let the recruitment not be random.&lt;br /&gt;
* '''villages_per_scout=0''' Test shown that the AI plays slightly better with less or no scouts at all.&lt;br /&gt;
* '''deactivate recruitment_save_gold''' (see above). Test shown that the AI plays better when it spends all money immediately.&lt;br /&gt;
&lt;br /&gt;
In Multiplayer games the AI 'Strong AI (RCA)' can be chosen where the settings are as described here.&lt;br /&gt;
&lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52127</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52127"/>
		<updated>2013-09-26T13:49:22Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Implementation details for interested readers */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white 'X'.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0.&lt;br /&gt;
The minimum depends on the aspect &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (recruitment_diversity * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Even if only a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tag is used, the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet will get overwritten. Include the &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; mentioned in the previous point to let the AI recruit something.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
         [recruit]         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#Do not forget to include the default&amp;lt;/span&amp;gt;&lt;br /&gt;
           importance=0    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#[recruit]-tag. Otherwise the AI won't&amp;lt;/span&amp;gt;&lt;br /&gt;
         [/recruit]        &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#recruit anything.&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it is a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
'''Important note:''' Although the intuition says that the AI will play better when it tries to save some gold, it doesn't. Tests shown that it is always good for the AI to have as many units as possible. (Probably it will do better in village-capturing then). The aspect is activated by default because playing against wave-like recruiting is fun. &lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=2: (int)''' From this turn on the aspect will be active. So the AI will always spend all gold until the defined turn. &amp;lt;code&amp;gt;active=0&amp;lt;/code&amp;gt; can be used to always deactivate gold saving. (The default value is 2 so nobody will get confused if the AI doesn't recruit in the first turn)&lt;br /&gt;
* '''begin=1.1: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.9: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=2&lt;br /&gt;
         begin=1.1&lt;br /&gt;
         end=0.9&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=2.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. When the value is 0.0, the AI will only recruit the best unit.(see [[AI_Recruitment#Implementation_details_for_interested_readers| above]] for implementation details).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=50 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Making recruitment strong=&lt;br /&gt;
&lt;br /&gt;
The default aspects are chosen so it is fun to play against the AI.&lt;br /&gt;
To let the AI recruit stronger this settings are recommended:&lt;br /&gt;
* '''recruitment_diversity=0''' Let the AI recruit the best units.&lt;br /&gt;
* '''recruitment_randomness=0''' Let the recruitment not be random.&lt;br /&gt;
* '''villages_per_scout=0''' Test shown that the AI plays slightly better with less or no scouts at all.&lt;br /&gt;
* '''deactivate recruitment_save_gold''' (see above). Test shown that the AI plays better when it spends all money immediately.&lt;br /&gt;
&lt;br /&gt;
In Multiplayer games the AI 'Strong AI (RCA)' can be chosen where the settings are as described here.&lt;br /&gt;
&lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52126</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52126"/>
		<updated>2013-09-26T13:47:35Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Other aspects */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white 'X'.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Even if only a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tag is used, the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet will get overwritten. Include the &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; mentioned in the previous point to let the AI recruit something.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
         [recruit]         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#Do not forget to include the default&amp;lt;/span&amp;gt;&lt;br /&gt;
           importance=0    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#[recruit]-tag. Otherwise the AI won't&amp;lt;/span&amp;gt;&lt;br /&gt;
         [/recruit]        &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#recruit anything.&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it is a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
'''Important note:''' Although the intuition says that the AI will play better when it tries to save some gold, it doesn't. Tests shown that it is always good for the AI to have as many units as possible. (Probably it will do better in village-capturing then). The aspect is activated by default because playing against wave-like recruiting is fun. &lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=2: (int)''' From this turn on the aspect will be active. So the AI will always spend all gold until the defined turn. &amp;lt;code&amp;gt;active=0&amp;lt;/code&amp;gt; can be used to always deactivate gold saving. (The default value is 2 so nobody will get confused if the AI doesn't recruit in the first turn)&lt;br /&gt;
* '''begin=1.1: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.9: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=2&lt;br /&gt;
         begin=1.1&lt;br /&gt;
         end=0.9&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=2.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. When the value is 0.0, the AI will only recruit the best unit.(see [[AI_Recruitment#Implementation_details_for_interested_readers| above]] for implementation details).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=50 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Making recruitment strong=&lt;br /&gt;
&lt;br /&gt;
The default aspects are chosen so it is fun to play against the AI.&lt;br /&gt;
To let the AI recruit stronger this settings are recommended:&lt;br /&gt;
* '''recruitment_diversity=0''' Let the AI recruit the best units.&lt;br /&gt;
* '''recruitment_randomness=0''' Let the recruitment not be random.&lt;br /&gt;
* '''villages_per_scout=0''' Test shown that the AI plays slightly better with less or no scouts at all.&lt;br /&gt;
* '''deactivate recruitment_save_gold''' (see above). Test shown that the AI plays better when it spends all money immediately.&lt;br /&gt;
&lt;br /&gt;
In Multiplayer games the AI 'Strong AI (RCA)' can be chosen where the settings are as described here.&lt;br /&gt;
&lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52122</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52122"/>
		<updated>2013-09-26T02:29:35Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Other aspects */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white 'X'.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Even if only a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tag is used, the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet will get overwritten. Include the &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; mentioned in the previous point to let the AI recruit something.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
         [recruit]         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#Do not forget to include the default&amp;lt;/span&amp;gt;&lt;br /&gt;
           importance=0    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#[recruit]-tag. Otherwise the AI won't&amp;lt;/span&amp;gt;&lt;br /&gt;
         [/recruit]        &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#recruit anything.&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it is a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
'''Important note:''' Although the intuition says that the AI will play better when it tries to save some gold, it doesn't. Tests shown that it is always good for the AI to have as many units as possible. (Probably it will do better in village-capturing then). The aspect is activated by default because playing against wave-like recruiting is fun. &lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=2: (int)''' From this turn on the aspect will be active. So the AI will always spend all gold until the defined turn. &amp;lt;code&amp;gt;active=0&amp;lt;/code&amp;gt; can be used to always deactivate gold saving. (The default value is 2 so nobody will get confused if the AI doesn't recruit in the first turn)&lt;br /&gt;
* '''begin=1.1: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.9: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=2&lt;br /&gt;
         begin=1.1&lt;br /&gt;
         end=0.9&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=50 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Making recruitment strong=&lt;br /&gt;
&lt;br /&gt;
The default aspects are chosen so it is fun to play against the AI.&lt;br /&gt;
To let the AI recruit stronger this settings are recommended:&lt;br /&gt;
* '''recruitment_diversity=0''' Let the AI recruit the best units.&lt;br /&gt;
* '''recruitment_randomness=0''' Let the recruitment not be random.&lt;br /&gt;
* '''villages_per_scout=0''' Test shown that the AI plays slightly better with less or no scouts at all.&lt;br /&gt;
* '''deactivate recruitment_save_gold''' (see above). Test shown that the AI plays better when it spends all money immediately.&lt;br /&gt;
&lt;br /&gt;
In Multiplayer games the AI 'Strong AI (RCA)' can be chosen where the settings are as described here.&lt;br /&gt;
&lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52121</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52121"/>
		<updated>2013-09-26T01:09:21Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white 'X'.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Even if only a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tag is used, the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet will get overwritten. Include the &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; mentioned in the previous point to let the AI recruit something.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
         [recruit]         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#Do not forget to include the default&amp;lt;/span&amp;gt;&lt;br /&gt;
           importance=0    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#[recruit]-tag. Otherwise the AI won't&amp;lt;/span&amp;gt;&lt;br /&gt;
         [/recruit]        &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#recruit anything.&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it is a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
'''Important note:''' Although the intuition says that the AI will play better when it tries to save some gold, it doesn't. Tests shown that it is always good for the AI to have as many units as possible. (Probably it will do better in village-capturing then). The aspect is activated by default because playing against wave-like recruiting is fun. &lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=2: (int)''' From this turn on the aspect will be active. So the AI will always spend all gold until the defined turn. &amp;lt;code&amp;gt;active=0&amp;lt;/code&amp;gt; can be used to always deactivate gold saving. (The default value is 2 so nobody will get confused if the AI doesn't recruit in the first turn)&lt;br /&gt;
* '''begin=1.1: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.9: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=2&lt;br /&gt;
         begin=1.1&lt;br /&gt;
         end=0.9&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Making recruitment strong=&lt;br /&gt;
&lt;br /&gt;
The default aspects are chosen so it is fun to play against the AI.&lt;br /&gt;
To let the AI recruit stronger this settings are recommended:&lt;br /&gt;
* '''recruitment_diversity=0''' Let the AI recruit the best units.&lt;br /&gt;
* '''recruitment_randomness=0''' Let the recruitment not be random.&lt;br /&gt;
* '''villages_per_scout=0''' Test shown that the AI plays slightly better with less or no scouts at all.&lt;br /&gt;
* '''deactivate recruitment_save_gold''' (see above). Test shown that the AI plays better when it spends all money immediately.&lt;br /&gt;
&lt;br /&gt;
In Multiplayer games the AI 'Strong AI (RCA)' can be chosen where the settings are as described here.&lt;br /&gt;
&lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52120</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52120"/>
		<updated>2013-09-26T01:08:47Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* The Aspect */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white 'X'.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Even if only a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tag is used, the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet will get overwritten. Include the &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; mentioned in the previous point to let the AI recruit something.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
         [recruit]         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#Do not forget to include the default&amp;lt;/span&amp;gt;&lt;br /&gt;
           importance=0    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#[recruit]-tag. Otherwise the AI won't&amp;lt;/span&amp;gt;&lt;br /&gt;
         [/recruit]        &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#recruit anything.&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it is a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
'''Important note:''' Although the intuition says that the AI will play better when it tries to save some gold, it doesn't. Tests shown that it is always good for the AI to have as many units as possible. (Probably it will do better in village-capturing then). The aspect is activated by default because playing against wave-like recruiting is fun. &lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=2: (int)''' From this turn on the aspect will be active. So the AI will always spend all gold until the defined turn. &amp;lt;code&amp;gt;active=0&amp;lt;/code&amp;gt; can be used to always deactivate gold saving. (The default value is 2 so nobody will get confused if the AI doesn't recruit in the first turn)&lt;br /&gt;
* '''begin=1.1: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.9: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=2&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Making recruitment strong=&lt;br /&gt;
&lt;br /&gt;
The default aspects are chosen so it is fun to play against the AI.&lt;br /&gt;
To let the AI recruit stronger this settings are recommended:&lt;br /&gt;
* '''recruitment_diversity=0''' Let the AI recruit the best units.&lt;br /&gt;
* '''recruitment_randomness=0''' Let the recruitment not be random.&lt;br /&gt;
* '''villages_per_scout=0''' Test shown that the AI plays slightly better with less or no scouts at all.&lt;br /&gt;
* '''deactivate recruitment_save_gold''' (see above). Test shown that the AI plays better when it spends all money immediately.&lt;br /&gt;
&lt;br /&gt;
In Multiplayer games the AI 'Strong AI (RCA)' can be chosen where the settings are as described here.&lt;br /&gt;
&lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52117</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52117"/>
		<updated>2013-09-25T14:35:17Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Making recruitment strong */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white 'X'.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Even if only a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tag is used, the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet will get overwritten. Include the &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; mentioned in the previous point to let the AI recruit something.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
         [recruit]         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#Do not forget to include the default&amp;lt;/span&amp;gt;&lt;br /&gt;
           importance=0    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#[recruit]-tag. Otherwise the AI won't&amp;lt;/span&amp;gt;&lt;br /&gt;
         [/recruit]        &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#recruit anything.&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it is a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
'''Important note:''' Although the intuition says that the AI will play better when it tries to save some gold, it doesn't. Tests shown that it is always good for the AI to have as many units as possible. (Probably it will do better in village-capturing then). The aspect is activated by default because playing against wave-like recruiting is fun. &lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=2: (int)''' From this turn on the aspect will be active. So the AI will always spend all gold until the defined turn. &amp;lt;code&amp;gt;active=0&amp;lt;/code&amp;gt; can be used to always deactivate gold saving. (The default value is 2 so nobody will get confused if the AI doesn't recruit in the first turn)&lt;br /&gt;
* '''begin=1.0: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.7: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=2&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Making recruitment strong=&lt;br /&gt;
&lt;br /&gt;
The default aspects are chosen so it is fun to play against the AI.&lt;br /&gt;
To let the AI recruit stronger this settings are recommended:&lt;br /&gt;
* '''recruitment_diversity=0''' Let the AI recruit the best units.&lt;br /&gt;
* '''recruitment_randomness=0''' Let the recruitment not be random.&lt;br /&gt;
* '''villages_per_scout=0''' Test shown that the AI plays slightly better with less or no scouts at all.&lt;br /&gt;
* '''deactivate recruitment_save_gold''' (see above). Test shown that the AI plays better when it spends all money immediately.&lt;br /&gt;
&lt;br /&gt;
In Multiplayer games the AI 'Strong AI (RCA)' can be chosen where the settings are as described here.&lt;br /&gt;
&lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52116</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52116"/>
		<updated>2013-09-25T14:32:49Z</updated>

		<summary type="html">&lt;p&gt;Flixx: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white 'X'.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Even if only a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tag is used, the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet will get overwritten. Include the &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; mentioned in the previous point to let the AI recruit something.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
         [recruit]         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#Do not forget to include the default&amp;lt;/span&amp;gt;&lt;br /&gt;
           importance=0    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#[recruit]-tag. Otherwise the AI won't&amp;lt;/span&amp;gt;&lt;br /&gt;
         [/recruit]        &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#recruit anything.&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it is a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
'''Important note:''' Although the intuition says that the AI will play better when it tries to save some gold, it doesn't. Tests shown that it is always good for the AI to have as many units as possible. (Probably it will do better in village-capturing then). The aspect is activated by default because playing against wave-like recruiting is fun. &lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=2: (int)''' From this turn on the aspect will be active. So the AI will always spend all gold until the defined turn. &amp;lt;code&amp;gt;active=0&amp;lt;/code&amp;gt; can be used to always deactivate gold saving. (The default value is 2 so nobody will get confused if the AI doesn't recruit in the first turn)&lt;br /&gt;
* '''begin=1.0: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.7: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=2&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Making recruitment strong=&lt;br /&gt;
&lt;br /&gt;
The default aspects are chosen so it is fun to play against the AI.&lt;br /&gt;
To let the AI recruit stronger this settings are recommended:&lt;br /&gt;
* '''recruitment_diversity=0''' Let the AI recruit the best units.&lt;br /&gt;
* '''recruitment_randomness=0''' Let the recruitment not be random.&lt;br /&gt;
* '''villages_per_scout=0''' Test shown that the AI plays slightly better with less or no scouts at all.&lt;br /&gt;
* '''deactivate recruitment_save_gold''' (see above). Test shown that the AI plays better when it spends all money immediately.&lt;br /&gt;
 &lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52115</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52115"/>
		<updated>2013-09-25T13:54:40Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Map analyis */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white 'X'.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Even if only a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tag is used, the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet will get overwritten. Include the &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; mentioned in the previous point to let the AI recruit something.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
         [recruit]         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#Do not forget to include the default&amp;lt;/span&amp;gt;&lt;br /&gt;
           importance=0    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#[recruit]-tag. Otherwise the AI won't&amp;lt;/span&amp;gt;&lt;br /&gt;
         [/recruit]        &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#recruit anything.&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it is a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=2: (int)''' From this turn on the aspect will be active. So the AI will always spend all gold until the defined turn. &amp;lt;code&amp;gt;active=0&amp;lt;/code&amp;gt; can be used to always deactivate gold saving. (The default value is 2 so nobody will get confused if the AI doesn't recruit in the first turn)&lt;br /&gt;
* '''begin=1.0: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.7: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=2&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52114</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52114"/>
		<updated>2013-09-25T13:54:28Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Map analyis */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white &amp;quot;X&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Even if only a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tag is used, the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet will get overwritten. Include the &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; mentioned in the previous point to let the AI recruit something.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
         [recruit]         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#Do not forget to include the default&amp;lt;/span&amp;gt;&lt;br /&gt;
           importance=0    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#[recruit]-tag. Otherwise the AI won't&amp;lt;/span&amp;gt;&lt;br /&gt;
         [/recruit]        &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#recruit anything.&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it is a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=2: (int)''' From this turn on the aspect will be active. So the AI will always spend all gold until the defined turn. &amp;lt;code&amp;gt;active=0&amp;lt;/code&amp;gt; can be used to always deactivate gold saving. (The default value is 2 so nobody will get confused if the AI doesn't recruit in the first turn)&lt;br /&gt;
* '''begin=1.0: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.7: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=2&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52102</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52102"/>
		<updated>2013-09-23T21:37:25Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white dot.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Even if only a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tag is used, the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet will get overwritten. Include the &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; mentioned in the previous point to let the AI recruit something.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
         [recruit]         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#Do not forget to include the default&amp;lt;/span&amp;gt;&lt;br /&gt;
           importance=0    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#[recruit]-tag. Otherwise the AI won't&amp;lt;/span&amp;gt;&lt;br /&gt;
         [/recruit]        &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#recruit anything.&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it is a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=2: (int)''' From this turn on the aspect will be active. So the AI will always spend all gold until the defined turn. &amp;lt;code&amp;gt;active=0&amp;lt;/code&amp;gt; can be used to always deactivate gold saving. (The default value is 2 so nobody will get confused if the AI doesn't recruit in the first turn)&lt;br /&gt;
* '''begin=1.0: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.7: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=2&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52101</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52101"/>
		<updated>2013-09-23T21:34:16Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* The Aspect */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white dot.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Even if only a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tag is used, the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet will get overwritten. Include the &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; mentioned in the previous point to let the AI recruit something.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
         [recruit]         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#Do not forget to include the default&amp;lt;/span&amp;gt;&lt;br /&gt;
           importance=0    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#[recruit]-tag. Otherwise the AI won't&amp;lt;/span&amp;gt;&lt;br /&gt;
         [/recruit]        &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#recruit anything.&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it is a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=2: (int)''' From this turn on the aspect will be active. So the AI will always spend all gold until the defined turn. &amp;lt;code&amp;gt;active=0&amp;lt;/code&amp;gt; can be used to always deactivate gold saving. (The default value is 2 so nobody will get confused if the AI doesn't recruit in the first turn)&lt;br /&gt;
* '''begin=1.0: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.7: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=yes&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52100</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52100"/>
		<updated>2013-09-23T21:33:56Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* The Aspect */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white dot.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Even if only a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tag is used, the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet will get overwritten. Include the &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; mentioned in the previous point to let the AI recruit something.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
         [recruit]         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#Do not forget to include the default&amp;lt;/span&amp;gt;&lt;br /&gt;
           importance=0    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#[recruit]-tag. Otherwise the AI won't&amp;lt;/span&amp;gt;&lt;br /&gt;
         [/recruit]        &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#recruit anything.&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it is a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=2: (int)''' From this turn on the aspect will be active (So the AI will always spend all gold until the defined turn). &amp;lt;code&amp;gt;active=0&amp;lt;/code&amp;gt; can be used to always deactivate gold saving. (The default value is 2 so nobody will get confused if the AI doesn't recruit in the first turn)&lt;br /&gt;
* '''begin=1.0: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.7: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=yes&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52099</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=52099"/>
		<updated>2013-09-23T21:31:47Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* The Aspect */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white dot.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Even if only a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tag is used, the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet will get overwritten. Include the &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; mentioned in the previous point to let the AI recruit something.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
         [recruit]         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#Do not forget to include the default&amp;lt;/span&amp;gt;&lt;br /&gt;
           importance=0    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#[recruit]-tag. Otherwise the AI won't&amp;lt;/span&amp;gt;&lt;br /&gt;
         [/recruit]        &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#recruit anything.&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it is a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=2: (int)''' From this turn on the aspect will be active. &amp;lt;code&amp;gt;active=0&amp;lt;/code&amp;gt; can be used to always deactivate gold saving. (The default value is 2 so nobody will get confused if the AI doesn't recruit in the first turn)&lt;br /&gt;
* '''begin=1.0: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.7: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=yes&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=User:Flixx/GSoC_2013/AI:_AI:_Refactor_recruitment_algorithm&amp;diff=51999</id>
		<title>User:Flixx/GSoC 2013/AI: AI: Refactor recruitment algorithm</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=User:Flixx/GSoC_2013/AI:_AI:_Refactor_recruitment_algorithm&amp;diff=51999"/>
		<updated>2013-09-14T17:26:37Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Goals / Milestones */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{SoC2013Student}}&lt;br /&gt;
[[Category:SoC_Ideas_AI_Recruitment_2013]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Description=&lt;br /&gt;
&amp;lt;h4&amp;gt;flix: Refactor recruitment algorithm&amp;lt;/h4&amp;gt;&lt;br /&gt;
An AI opponent have to decide in a separate phase which units to recruit. Right now the default recruitment algorithm is very simple and can be improved in many ways. I want to make the AI recruiting better, more fun to play against and more configurable by a scenario editor.&lt;br /&gt;
&lt;br /&gt;
= Make it better =&lt;br /&gt;
(In terms of harder to play against)&lt;br /&gt;
&lt;br /&gt;
== Map Analysis==&lt;br /&gt;
&lt;br /&gt;
Previous attempts have been made to this:&lt;br /&gt;
* analyze_potential_recruit_movements() analyzes nearby targets and how to reach them.&lt;br /&gt;
* the &amp;quot;Formula AI dev&amp;quot;-Recruitment uses the terrain near to villages (&amp;quot;important locations&amp;quot;) and recruits units which defend good in those areas. According to Crab_ the algorithm easily beats the RCA AI (default-AI). The algorithm is written in Formula-AI and hard to understand (and probably therefore not in use). Additionally Formula-AI is not longer supported in future developments. &lt;br /&gt;
&lt;br /&gt;
In my project I want to refactor the &amp;quot;Formula AI dev&amp;quot;-Recruitment in C++, test it, improve it and make it easy to configure. &lt;br /&gt;
&lt;br /&gt;
== Combat Analysis ==&lt;br /&gt;
&lt;br /&gt;
The current RCA AI make much use of a combat analysis. This works actually pretty well. &lt;br /&gt;
Although there are still some improvements possible.&lt;br /&gt;
In context of my Game Theory Algorithm, I already tried to weight the attacks in a different way. Tests have shown that this was a good thing to do.&lt;br /&gt;
&lt;br /&gt;
A lot of things that have a minor effects in combat analysis are not considered yet. Examples for this are traits, movement-costs, daytime, abilities like leadership or healing.&lt;br /&gt;
&lt;br /&gt;
I will give the combat analysis a refactoring and try to include all those specialties.&lt;br /&gt;
&lt;br /&gt;
== Counter Recruitment Strategies ==&lt;br /&gt;
In order to recruit the best units for a set of enemy units and make the best use of a combat analysis, it is sometimes useful to wait for the enemy to recruit. (Not spend all money in the first round). The RCA AI will always recruit when it has enough gold available. This can be improved. The hardest part when implementing this is to decide when such a strategy is appropriate.&lt;br /&gt;
&lt;br /&gt;
I want first to introduce a state &amp;quot;Counter Recruitment&amp;quot;. When the AI is in this state it will first wait (and discover) enemy units before spending all the money. Then I will test (via benchmarking) in which cases it is useful for the AI to enter or leave this state.&lt;br /&gt;
&lt;br /&gt;
== Counter-Counter Recruitment Strategies ==&lt;br /&gt;
Note: I need a better name for this ;)&lt;br /&gt;
&lt;br /&gt;
We don't want the enemy to counter easy our own units. This could happen when all of our recruited units can be easily countered by one unit type of the enemy. So when we recruit we need to recruit in a way our units will look diverse from a combat point of view.&lt;br /&gt;
&lt;br /&gt;
As a part of the proposal I already implemented a Proof of Concept for such a strategy. For this I've used tools from Game Theory. (Finding a mixed strategy Nash Equilibrium). &lt;br /&gt;
See [[User:Flixx/Game Theory for Recruiting]] for more.&lt;br /&gt;
&lt;br /&gt;
= Make it more Fun =&lt;br /&gt;
&lt;br /&gt;
In terms of recruitment ''more fun'' is achieved when the AI is recruiting not only one type but a good mix of all available units. This can easily be achieved by just favor rare units. The hard part here is again, how to weight it against other options.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Implementation Details: Score Map =&lt;br /&gt;
&lt;br /&gt;
So far I spoke about several analyze algorithms: map-analyze, combat-analyze, counter- and counter-counter strategies, and some possibility to boost diversity.&lt;br /&gt;
Here I want to say something how this algorithms could work together.&lt;br /&gt;
&lt;br /&gt;
At the beginning of the recruitment phase (but after filtering units according to configurations) a '''score map''' is created with all possible units which can be recruited. In the analysis phases (map analysis, combat analysis, ...) it will be filled with scores. More specifically:&lt;br /&gt;
 Let i denote a analysis algorithm and j a unit.&lt;br /&gt;
 Score_j = sum_over_all_i (score_ij * weight_i)&lt;br /&gt;
&lt;br /&gt;
After the score-map is completed the recruitments will be executed according to it. One Example:&lt;br /&gt;
&lt;br /&gt;
 '''Unit'''		'''Score'''		'''percentage'''&lt;br /&gt;
   A		  50		    1 %&lt;br /&gt;
   B		2000		   40 %&lt;br /&gt;
   C		 450		    9 %&lt;br /&gt;
   D		1000		   20 %&lt;br /&gt;
   E		 500		   10 %&lt;br /&gt;
   F		1000		   20 %&lt;br /&gt;
&lt;br /&gt;
 If 5 Hexes are available for recruiting the Leader will recruit 2 B's , 1 D, 1 F and 1 E.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# To make the recruitment-analysis efficient, all analyses algorithms should only run once per CA Execution.&lt;br /&gt;
# The score map will most likely also have a score for 'recruit no unit' for making Counter-Recruiting-Strategies work.&lt;br /&gt;
# To make this work with multiple leaders: The score-map can be expanded so there are scores for each leader. The separate analyse-algorithms can then set for a unit a different score for leader 1 then for leader 2. To start easy on this we can first assume that for all leaders the scores will be the same. Later one could extend the analyse-algorithms so they can set different scores for leaders. (This could be useful for example if the Combat-Analysis can figure out that one specific unit can fight better against the units around leader 1).  After the score-map is completed each leader will recruit according to their score-map (They could swap after each recruitment if money is rare so the units will be split equally)&lt;br /&gt;
# One could also think about negative scores. For example when the combat-analyzes wants to make sure that we not recruit Woses when there are fire-units around, although Woses seemed best for Map-analysis.&lt;br /&gt;
# The Cost could influence the score too. Without cost the AI would only recruit good and expensive units.&lt;br /&gt;
# Even when ''diversity'' is weighted to zero, the current units on the map should influence the scores too. Otherwise if the leader can only recruit one unit per turn he would always recruit the same unit.&lt;br /&gt;
# One could also think about giving scouts in the first rounds a higher score.&lt;br /&gt;
&lt;br /&gt;
= Make it more configurable =&lt;br /&gt;
&lt;br /&gt;
For a better *development flow* I want first specify the configurations (in the wiki) before implementing them. Before it comes to implementation I will also use the forum to discuss my suggestions.&lt;br /&gt;
&lt;br /&gt;
See [[User:Flixx/Configuration Specifications]]&lt;br /&gt;
&lt;br /&gt;
= Purposes Idea =&lt;br /&gt;
For the Idea see this page: [[User:Flixx/GSoC 2013/Idea AI Recruitment: Purposes]]&lt;br /&gt;
&lt;br /&gt;
In the scope of this project I want to take some time to implement a Proof of Concept of this idea for further evaluation. I think it is a good idea but I don't want to define the implementation of this as a project goal. Probably Purpose-Driven-Recruitment will get too complex, and I don't want to fail the goals. Though I want to plan some time at the end of the summer for this idea to see if it's worth something.&lt;br /&gt;
&lt;br /&gt;
= Multiple Leader =&lt;br /&gt;
&lt;br /&gt;
Although the AI could have multiple recruiters available it will currently only recruit with one leader. I want to change this. All necessary configurations for this are already in place (a scenario-editor can specify multiple leaders and leader specific recruitment lists).&lt;br /&gt;
&lt;br /&gt;
I already wrote some thoughts how to make the recruitment work. I will repeat it here:&lt;br /&gt;
&lt;br /&gt;
''To make this work with multiple leaders: The score-map can be expanded so there are scores for each leader. The separate analyse-algorithms can then set for a unit a different score for leader 1 then for leader 2. To start easy on this we can first assume that for all leaders the scores will be the same. Later one could extend the analyse-algorithms so they can set different scores for leaders. (This could be useful for example if the Combat-Analysis can figure out that one specific unit can fight better against the units around leader 1). After the score-map is completed each leader will recruit according to their score-map (They could swap after each recruitment if money is rare so the units will be split equally)''&lt;br /&gt;
&lt;br /&gt;
Additionally I need to extend the Move_Leader_To_Keep CA so that both leaders will advised to return to a keep. I want to implement this early so I can test everything with multiple leaders from the beginning on. &lt;br /&gt;
&lt;br /&gt;
When I have time I want to make the Move_Leader_To_Keep CA actually ''intelligent'' so it can be decided if it is really necessary to move all recruiters back to a keep (and if not decide which recruiter). I marked this as 'optional'.&lt;br /&gt;
&lt;br /&gt;
= Set of Goals - Brief overwiew =&lt;br /&gt;
&lt;br /&gt;
* Refactor the &amp;quot;Formula AI dev&amp;quot;-Recruitment in C++, test it, improve it and make it easy to configure.&lt;br /&gt;
* Refactor Combat analysis&lt;br /&gt;
* Implement a Counter-Recruitment-Strategy and find good conditions when to use it&lt;br /&gt;
* Implementing a system to favor rare units&lt;br /&gt;
* Find good default-weights for all sub-algorithms&lt;br /&gt;
* Make those weights configurable&lt;br /&gt;
* Introduce easy understandalbe configurations&lt;br /&gt;
* Support recruitment with Multiple Leaders&lt;br /&gt;
* (optional) Implementing a Proof of Concept for the Purpose Idea and test it&lt;br /&gt;
&lt;br /&gt;
= Timeline =&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
| style=&amp;quot;width: 125px;&amp;quot; | May 27 || ''Accepted student proposals announced by Google'' &lt;br /&gt;
|-&lt;br /&gt;
| May 28 - Jun 17 || ''Before the official Coding Phase:'' Do Step 1 to Step 5 (Goals). Start Coding already. Maybe provide some patches with have not necessarily something to do with recruitment to familiarize more with the Code.  &lt;br /&gt;
|-&lt;br /&gt;
| Jun 18 - Jul 01 || Time off. See [http://wiki.wesnoth.org/index.php?title=User:Flixx/GSoC_2013/AI:_AI:_Refactor_recruitment_algorithm#time 1.7) in the Questionnaire]&lt;br /&gt;
|-&lt;br /&gt;
| Jul 02 - Jul 28 || Do everything (mandatory) up to Milestone 1.  &lt;br /&gt;
|-&lt;br /&gt;
| Jul 29 - Aug 02 || ''Mid term evaluation''  &lt;br /&gt;
|-&lt;br /&gt;
| Aug 03 - Aug 25 || Do everything (mandatory) up to Milestone 2. &lt;br /&gt;
|-&lt;br /&gt;
| Aug 26 - Sep 15 || Do most steps up to Milestone 3. &lt;br /&gt;
|-&lt;br /&gt;
| Sep 16 || ''Suggested 'pencils down' date. Take a week to scrub code, write tests, improve documentation, etc.''&lt;br /&gt;
|-&lt;br /&gt;
| Sep 16 - Sep 23 || Do documentary steps up to Milestone 3 and finish at least mandatory steps of Milestone 3&lt;br /&gt;
|-&lt;br /&gt;
| Sep 23 - Oct 01 || ''Final Evaluation, submitting required code samples to Google''&lt;br /&gt;
|-&lt;br /&gt;
| Afterwards || If Purpose-Driven-Recruitment works -&amp;gt; improve it. Otherwise I'm sure I'll find something to work on ;) &amp;lt;br/&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Goals / Milestones =&lt;br /&gt;
(Note: I added a column &amp;quot;complexity&amp;quot; and filled it with values between 1 (easy) and 3 (complex). This is a rough guess but it helped me to balance the steps between the Milestones.)&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot;&lt;br /&gt;
! ID !! PRIORITY !! DESCRIPTION !! &amp;lt;small&amp;gt;COMPLEXITY&amp;lt;/small&amp;gt; !! &amp;lt;small&amp;gt;PROGRESS&amp;lt;/small&amp;gt; &lt;br /&gt;
|-&lt;br /&gt;
| 1 ||  style=&amp;quot;width: 110px;&amp;quot; | &amp;lt;p style=&amp;quot;color:red&amp;quot;&amp;gt;MANDATORY&amp;lt;/p&amp;gt; || Specify configurations and [http://forums.wesnoth.org/viewtopic.php?f=8&amp;amp;t=38885 discuss them in the Forums]. Write the results in  [http://wiki.wesnoth.org/User:Flixx/Configuration_Specifications this wikipage]. || 2 || done without feedback&lt;br /&gt;
|-&lt;br /&gt;
| 2 ||  &amp;lt;p style=&amp;quot;color:red&amp;quot;&amp;gt;MANDATORY&amp;lt;/p&amp;gt; || Completely understand the Map Analyses of &amp;quot;Formula AI dev&amp;quot; and write a explanation in [http://wiki.wesnoth.org/User:Flixx/Recruitment_Algorithm_Collection this wikipage]. || 1 || done&lt;br /&gt;
|-&lt;br /&gt;
| 3 ||  &amp;lt;p style=&amp;quot;color:red&amp;quot;&amp;gt;MANDATORY&amp;lt;/p&amp;gt; || Set up a own CA for recruitment, implement a score table. Let units recruit according to some mock values in the score table. || 1 || [https://github.com/flixx/wesnoth-old/commit/fa859891f40203e6aaa61d183eda62e09b83f06e done]&lt;br /&gt;
|-&lt;br /&gt;
| 4 ||  &amp;lt;p style=&amp;quot;color:blue&amp;quot;&amp;gt;OPTIONAL&amp;lt;/p&amp;gt; || Collect experimental recruitment algorithms which were written over the last years. Evaluate them and think if they could be of some use. Write results down. || 2 || 80 %&lt;br /&gt;
|-&lt;br /&gt;
| 5 ||  &amp;lt;p style=&amp;quot;color:blue&amp;quot;&amp;gt;OPTIONAL&amp;lt;/p&amp;gt; || Think about what to pay attention so all following steps will be implemented to work with multiple leaders. Maybe implement Multiple Leader Support in the current Recruitment Algorithms. || 2 || done &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| - ||  &amp;lt;p style=&amp;quot;color:green&amp;quot;&amp;gt;MILESTONE 0&amp;lt;/p&amp;gt;|| Have at least all mandatory steps above done when the coding period starts (Jun 16) || 4 + 4 ||&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 6 ||  &amp;lt;p style=&amp;quot;color:red&amp;quot;&amp;gt;MANDATORY&amp;lt;/p&amp;gt; || Add Multiple Leader support for the Move_Leader_To_Keep CA. || 2 || [https://github.com/flixx/wesnoth-old/commit/d866480d1a44cb6d02ade98d3fce6f48946ad50d done]&lt;br /&gt;
|-&lt;br /&gt;
| 7 ||  &amp;lt;p style=&amp;quot;color:blue&amp;quot;&amp;gt;OPTIONAL&amp;lt;/p&amp;gt; || Make this implementation (Multiple Leader in MLTK CA) 'intelligent', so that it can be decided which leader shall go to a keep. || 3 ||&lt;br /&gt;
|-&lt;br /&gt;
| 8 ||  &amp;lt;p style=&amp;quot;color:red&amp;quot;&amp;gt;MANDATORY&amp;lt;/p&amp;gt; || Refactor the Map Analyses of &amp;quot;Formula AI dev&amp;quot; in C++. || 3 || done&lt;br /&gt;
|-&lt;br /&gt;
| 9 ||  &amp;lt;p style=&amp;quot;color:red&amp;quot;&amp;gt;MANDATORY&amp;lt;/p&amp;gt; || Test implementation of Map Analyses. || 1 || done&lt;br /&gt;
|-&lt;br /&gt;
| 10 ||  &amp;lt;p style=&amp;quot;color:blue&amp;quot;&amp;gt;OPTIONAL&amp;lt;/p&amp;gt; || Extract parameters which could later be configured by aspects. || 1 || done&lt;br /&gt;
|-&lt;br /&gt;
| 11 ||  &amp;lt;p style=&amp;quot;color:blue&amp;quot;&amp;gt;OPTIONAL&amp;lt;/p&amp;gt; || Batch test Map Analyses (with parameter variations) || 2 || &lt;br /&gt;
|-&lt;br /&gt;
| 12 ||  &amp;lt;p style=&amp;quot;color:red&amp;quot;&amp;gt;MANDATORY&amp;lt;/p&amp;gt; || Integrate current Combat-Analysis to work with the score map. When weighting the Map-Analysis with 0 the AI should now exactly recruit the same as it did before the refactoring. || 2 || skipped&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| - ||  &amp;lt;p style=&amp;quot;color:green&amp;quot;&amp;gt;MILESTONE 1&amp;lt;/p&amp;gt;|| Have at least all mandatory steps above done until July 28. || 8 + 6 ||&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 13 ||  &amp;lt;p style=&amp;quot;color:red&amp;quot;&amp;gt;MANDATORY&amp;lt;/p&amp;gt; || Refactor current Combat Analysis. || 3 || done&lt;br /&gt;
|-&lt;br /&gt;
| 14 ||  &amp;lt;p style=&amp;quot;color:blue&amp;quot;&amp;gt;OPTIONAL&amp;lt;/p&amp;gt; || Extract parameters of Combat Analysis which could later be configured by aspects. Batch Test parameter variations || 2 || done&lt;br /&gt;
|-&lt;br /&gt;
| 15 ||  &amp;lt;p style=&amp;quot;color:red&amp;quot;&amp;gt;MANDATORY&amp;lt;/p&amp;gt; || Implement Counter-Recruitment-Strategies. || 2 || done&lt;br /&gt;
|-&lt;br /&gt;
| 16 ||  &amp;lt;p style=&amp;quot;color:red&amp;quot;&amp;gt;MANDATORY&amp;lt;/p&amp;gt; || Extract parameters of Counter-Recruitment-Strategies which could later be configured by aspects. || 1 || done&lt;br /&gt;
|-&lt;br /&gt;
| 17 ||  &amp;lt;p style=&amp;quot;color:blue&amp;quot;&amp;gt;OPTIONAL&amp;lt;/p&amp;gt; || Implement 'Counter-Counter-Recruitment-Strategies' and test it. || 3 || done&lt;br /&gt;
|-&lt;br /&gt;
| 18 ||  &amp;lt;p style=&amp;quot;color:blue&amp;quot;&amp;gt;OPTIONAL&amp;lt;/p&amp;gt; || Batch-test and improve Counter- and Counter-Counter Recruitment Strategies. || 2 || &lt;br /&gt;
|-&lt;br /&gt;
| 19 ||  &amp;lt;p style=&amp;quot;color:red&amp;quot;&amp;gt;MANDATORY&amp;lt;/p&amp;gt; || Implement 'Diversity'. When weighting all other phases with 0 the AI should only recruit units, which are currently rare on the map. || 1 || done&lt;br /&gt;
|-&lt;br /&gt;
| 20 ||  &amp;lt;p style=&amp;quot;color:red&amp;quot;&amp;gt;MANDATORY&amp;lt;/p&amp;gt; || Set up a parameterizable weight-system. || 1 || done&lt;br /&gt;
|-&lt;br /&gt;
| 21 ||  &amp;lt;p style=&amp;quot;color:red&amp;quot;&amp;gt;MANDATORY&amp;lt;/p&amp;gt; || Batch-test with different weights. Document results in this wiki page, define default weights (they will be normalized to 1 for easy configuration then) || 3 || done / not necessary&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| - ||  &amp;lt;p style=&amp;quot;color:green&amp;quot;&amp;gt;MILESTONE 2&amp;lt;/p&amp;gt;|| Have at least all mandatory steps above done until Aug 25. || 11 + 7 ||&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| 22 ||  &amp;lt;p style=&amp;quot;color:red&amp;quot;&amp;gt;MANDATORY&amp;lt;/p&amp;gt; || Review Configuration Specifications from Step 1 and adjust them if necessary. || 1 || done&lt;br /&gt;
|-&lt;br /&gt;
| 23 ||  &amp;lt;p style=&amp;quot;color:red&amp;quot;&amp;gt;MANDATORY&amp;lt;/p&amp;gt; || Define Aspects for the Configurations and implement missing aspects (like 'recruit-more=') and make them work with the score map. || 3 || done&lt;br /&gt;
|-&lt;br /&gt;
| 24 ||  &amp;lt;p style=&amp;quot;color:red&amp;quot;&amp;gt;MANDATORY&amp;lt;/p&amp;gt; || Test those new aspects. || 2 || done&lt;br /&gt;
|-&lt;br /&gt;
| 25 ||  &amp;lt;p style=&amp;quot;color:red&amp;quot;&amp;gt;MANDATORY&amp;lt;/p&amp;gt; || Write a separate wikipage for scenario-editors about those aspects and provide some examples and describe use-cases. || 2 || [[AI_Recruitment|done]]&lt;br /&gt;
|-&lt;br /&gt;
| 26 ||  &amp;lt;p style=&amp;quot;color:red&amp;quot;&amp;gt;MANDATORY&amp;lt;/p&amp;gt; || Test recruitment with multiple leaders (I should implement and test every step with multiple leaders so there is hopefully not much to do here)  || 1 || done&lt;br /&gt;
|-&lt;br /&gt;
| 27 ||  &amp;lt;p style=&amp;quot;color:blue&amp;quot;&amp;gt;OPTIONAL&amp;lt;/p&amp;gt; || Introduce a purpose memory for the AI-Units || 2 ||&lt;br /&gt;
|-&lt;br /&gt;
| 28 ||  &amp;lt;p style=&amp;quot;color:blue&amp;quot;&amp;gt;OPTIONAL&amp;lt;/p&amp;gt; || Implement a Proof of Concept for my Purpose-Driven-Recruitment Idea || 3 ||&lt;br /&gt;
|-&lt;br /&gt;
| 29 ||  &amp;lt;p style=&amp;quot;color:blue&amp;quot;&amp;gt;OPTIONAL&amp;lt;/p&amp;gt; || Test this Implementation. || 2 ||&lt;br /&gt;
|-&lt;br /&gt;
| 30 ||  &amp;lt;p style=&amp;quot;color:blue&amp;quot;&amp;gt;OPTIONAL&amp;lt;/p&amp;gt; || If successful write further steps for Purpose-Driven-Recruitment in a wikipage (e.g. how the purposes could be work in other phases) || 2 ||&lt;br /&gt;
|-&lt;br /&gt;
| 31 ||  || Clean everything up and document || n/a ||&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| - ||  &amp;lt;p style=&amp;quot;color:green&amp;quot;&amp;gt;MILESTONE 3&amp;lt;/p&amp;gt;||  Have at least all mandatory steps above done until Sep 23. || 9 + 9 ||&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=IRC=&lt;br /&gt;
flix&lt;br /&gt;
=Questionnaire=&lt;br /&gt;
&lt;br /&gt;
'''1) Basics'''&lt;br /&gt;
&lt;br /&gt;
'''1.1) Write a small introduction to yourself.'''&lt;br /&gt;
&lt;br /&gt;
Hi! I'm Felix, 22 and from Germany/Berlin. I'm currently as a exchange student in Amman/Jordan (until June) studying Computer Science at Princess Sumaya University for Technology. &lt;br /&gt;
&lt;br /&gt;
'''1.2) State your preferred email address.'''&lt;br /&gt;
&lt;br /&gt;
I will add my address in the google application.&lt;br /&gt;
&lt;br /&gt;
'''1.3) If you have chosen a nick for IRC and Wesnoth forums, what is it?'''&lt;br /&gt;
&lt;br /&gt;
flix&lt;br /&gt;
&lt;br /&gt;
'''1.4) Why do you want to participate in summer of code?'''&lt;br /&gt;
&lt;br /&gt;
I've played with the thought to join a Open Source Community for some time now. And GSoC seems like a great way to to this. I admire the idea of Open Source and the way how international communities can assemble such great software! &lt;br /&gt;
&lt;br /&gt;
'''1.5) What are you studying, subject, level and school?'''&lt;br /&gt;
&lt;br /&gt;
When I'm not in Jordan I study something fancy called &amp;quot;Science in the Information Society&amp;quot; at Technical University in Berlin. I'm currently in my 3rd year. This courses are great because we have over 50% optional courses and we could study nearly anything (Physics, Math, Chemistry, ...) I chose to focus on Computer Science very early (in my first year). So I had a lot courses with the Computer Scientists and consider myself at the same level as a Computer Scientist in his/her 3rd year.&lt;br /&gt;
&lt;br /&gt;
Here in Jordan I learnt to love [http://en.wikipedia.org/wiki/Massive_open_online_course MOOCs]. I mention this because I also finished the course [https://www.edx.org/courses/BerkeleyX/CS188.1x/2012_Fall/about Artificial Intelligence (BerkeleyX)] It was the best course I ever had (online and offline).&lt;br /&gt;
&lt;br /&gt;
'''1.6) What country are you from, at what time are you most likely to be able to join IRC?'''&lt;br /&gt;
&lt;br /&gt;
Germany (UTC+01:00) My favorite time to work is between 4 pm and 2 am. Although I'm quite flexible with times and can try to be online when my mentor is.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;time&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
'''1.7) Do you have other commitments for the summer period ? Do you plan to take any vacations ? If yes, when.'''&lt;br /&gt;
&lt;br /&gt;
Yes! I'm currently in Amman/Jordan as a exchange student. I will go back to Germany on the 24th of June. And around this date I need some time off. More specific: I'm only partly available from 18th of June to the 1st of July.&lt;br /&gt;
&lt;br /&gt;
Apart from that I have no other commitments and would consider SoC as a full-time job. Also I can start early (before 18th of June) to work on my project.&lt;br /&gt;
&lt;br /&gt;
'''2) Experience'''&lt;br /&gt;
&lt;br /&gt;
'''2.1) What programs/software have you worked on before?'''&lt;br /&gt;
*In High-school I started to tweak phpBB for my purposes (don't work on this anymore)&lt;br /&gt;
*In my first year in university I developed for a project a AI for the Game [http://en.wikipedia.org/wiki/Hive_(game) Hive] (Java).&lt;br /&gt;
*I'm currently developing a mobile-tagging software for images in Java (Tomcat Server) and Android.&lt;br /&gt;
&lt;br /&gt;
'''2.2) Have you developed software in a team environment before? (As opposed to hacking on something on your own)'''&lt;br /&gt;
&lt;br /&gt;
Yes, for the AI I mentioned for example (team of 8). I strongly prefer to develop in teams.&lt;br /&gt;
&lt;br /&gt;
'''2.3) Have you participated to the Google Summer of Code before? As a mentor or a student? In what project? Were you successful? If not, why?'''&lt;br /&gt;
&lt;br /&gt;
No.&lt;br /&gt;
&lt;br /&gt;
'''2.4) Are you already involved with any open source development projects? If yes, please describe the project and the scope of your involvement.'''&lt;br /&gt;
&lt;br /&gt;
No, I'm not :(&lt;br /&gt;
&lt;br /&gt;
'''2.5) Gaming experience - Are you a gamer?'''&lt;br /&gt;
&lt;br /&gt;
I wouldn't consider myself as a gamer...&lt;br /&gt;
&lt;br /&gt;
'''2.5.1) What type of gamer are you?'''&lt;br /&gt;
&lt;br /&gt;
...although I love to play once in while. Especially multi-player games with friends.&lt;br /&gt;
In a month I maybe play 6 nights long ;) &lt;br /&gt;
&lt;br /&gt;
'''2.5.2) What type of games?'''&lt;br /&gt;
&lt;br /&gt;
*Turnbased Stategy games like Wesnoth, Civilisations,&lt;br /&gt;
*Old-school realtime strategy games like Empire Earth, WarcraftIII &lt;br /&gt;
&lt;br /&gt;
'''2.5.3) What type of opponents do you prefer?'''&lt;br /&gt;
&lt;br /&gt;
I prefer AI opponents. &lt;br /&gt;
&lt;br /&gt;
'''2.5.4) Are you more interested in story or gameplay?'''&lt;br /&gt;
&lt;br /&gt;
Definitely gameplay. I'm a story-skipper ;).&lt;br /&gt;
&lt;br /&gt;
'''2.5.5) Have you played Wesnoth? If so, tell us roughly for how long and whether you lean towards single player or multiplayer.'''&lt;br /&gt;
&lt;br /&gt;
I started to play Wesnoth half a year ago and have played it with a friend once in a while since. I play the mainline campaigns too. I like both.&lt;br /&gt;
&lt;br /&gt;
'''2.6) If you have contributed any patches to Wesnoth, please list them below. You can also list patches that have been submitted but not committed yet and patches that have not been specifically written for GSoC. If you have gained commit access to our repository (during the evaluation period or earlier) please state so.'''&lt;br /&gt;
&lt;br /&gt;
Yes, I enjoyed it to dig into the code and trying to do some things from the EasyCoding Page.&lt;br /&gt;
&lt;br /&gt;
*[https://gna.org/patch/?3875 Added new aspect 'advancements']&lt;br /&gt;
*[https://gna.org/patch/?3849 Improved ai behavior when using goto_x / goto_x in WML]&lt;br /&gt;
*[https://gna.org/patch/?3888 Command 'lua wesnoth.debug_ai(side).ai]&lt;br /&gt;
&lt;br /&gt;
*Additionally: Some work on the ai-testing suite and my Game-Theory-Algorithm can be found [https://github.com/flixx/wesnoth-old/tree/game_theory/src/ai/flix here].   &lt;br /&gt;
&lt;br /&gt;
'''3) Communication skills'''&lt;br /&gt;
&lt;br /&gt;
'''3.1) Though most of our developers are not native English speakers, English is the project's working language.  Describe your fluency level in written English.'''&lt;br /&gt;
&lt;br /&gt;
Should be more then enough to communicate with the community.&lt;br /&gt;
&lt;br /&gt;
'''3.2) What spoken languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
German, English (Arabic is way too hard ;) )&lt;br /&gt;
&lt;br /&gt;
'''3.3) Are you good at interacting with other players? Our developer community is friendly, but the player community can be a bit rough.'''&lt;br /&gt;
&lt;br /&gt;
Yes, I think so.&lt;br /&gt;
&lt;br /&gt;
'''3.4) Do you give constructive advice?'''&lt;br /&gt;
&lt;br /&gt;
When I can, sure!&lt;br /&gt;
&lt;br /&gt;
'''3.5) Do you receive advice well?''' &lt;br /&gt;
&lt;br /&gt;
Depends on who want to advice me. But in general of course!&lt;br /&gt;
&lt;br /&gt;
'''3.6) Are you good at sorting useful criticisms from useless ones?'''&lt;br /&gt;
&lt;br /&gt;
I think so, yes.&lt;br /&gt;
&lt;br /&gt;
'''3.7) How autonomous are you when developing ? Would you rather discuss intensively changes and not start coding until you know what you want to do or would you rather code a proof of concept to &amp;quot;see how it turn out&amp;quot;, taking the risk of having it thrown away if it doesn't match what the project want'''&lt;br /&gt;
&lt;br /&gt;
I think I am the autonomous developer who would code a proof of concept.&lt;br /&gt;
Although since i'm new to this community I would definitively ask questions and discuss the project goals so the risk of failing them is minimized.&lt;br /&gt;
&lt;br /&gt;
I always try to figure out problems of technical nature myself.&lt;br /&gt;
&lt;br /&gt;
'''4) Project'''&lt;br /&gt;
&lt;br /&gt;
'''4.1) Did you select a project from our list? If that is the case, what project did you select? What do you want to especially concentrate on?'''&lt;br /&gt;
&lt;br /&gt;
The Project &amp;quot;AI Refactor recruitment algorithm&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
'''4.2) If you have invented your own project, please describe the project and the scope.'''&lt;br /&gt;
&lt;br /&gt;
-&lt;br /&gt;
&lt;br /&gt;
'''4.3) Why did you choose this project?'''&lt;br /&gt;
&lt;br /&gt;
Basically because Crab_ said so ;). When I first read the project page I wanted to do the &amp;quot;defensive AI&amp;quot; project. But after some advice I concentrated on the Recruitment Idea. And meanwhile I really like this Idea and prefer it over the others. I would love to improve the AIs recruitment.&lt;br /&gt;
&lt;br /&gt;
'''4.4) Include an estimated timeline for your work on the project. Don't forget to mention special things like &amp;quot;I booked holidays between A and B&amp;quot; and &amp;quot;I got an exam at ABC and won't be doing much then&amp;quot;.'''&lt;br /&gt;
&lt;br /&gt;
See above.&lt;br /&gt;
&lt;br /&gt;
'''4.5) Include as much technical detail about your implementation as you can'''&lt;br /&gt;
&lt;br /&gt;
See above.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''4.6) What do you expect to gain from this project?'''&lt;br /&gt;
&lt;br /&gt;
I would finally be part of a Open Source Community. Furthermore I expect to gain experience in C++ (and Lua). I'm quite new to C++ and I think Wesnoth is great for improving C++-skills.&lt;br /&gt;
 &lt;br /&gt;
'''4.7) What would make you stay in the Wesnoth community after the conclusion of SOC? '''&lt;br /&gt;
&lt;br /&gt;
A failed project would be demotivating, but otherwise I would love to stay in the community if I find some time when university starts again.&lt;br /&gt;
&lt;br /&gt;
'''5) Practical considerations'''&lt;br /&gt;
&lt;br /&gt;
'''5.1) Are you familiar with any of the following tools or languages?'''&lt;br /&gt;
&lt;br /&gt;
*'''Git (used for all commits)'''&lt;br /&gt;
**Yes&lt;br /&gt;
*'''C++ (language used for all the normal source code)'''&lt;br /&gt;
**I'm new to C++. But since I worked on some patches I've already learned a lot and feel capable to work with Wesnoth's code. &lt;br /&gt;
*'''STL, Boost, Sdl (C++ libraries used by Wesnoth)'''&lt;br /&gt;
**I've heard of it and made use of it in the patches, but without big background knowledge.&lt;br /&gt;
*'''Python (optional, mainly used for tools)'''&lt;br /&gt;
**Yes&lt;br /&gt;
*'''build environments (eg cmake/scons)'''&lt;br /&gt;
**I know how to build. That's it.&lt;br /&gt;
*'''WML (the wesnoth specific scenario language)'''&lt;br /&gt;
**Yes&lt;br /&gt;
*'''Lua (used in combination with WML to create scenarios)'''&lt;br /&gt;
**Not really, but ironically I know how C++ and Lua are communicating through the Lua Stack since I worked on two patches dealing with this. Lua seems easy enough though.&lt;br /&gt;
&lt;br /&gt;
'''5.2) Which tools do you normally use for development? Why do you use them?'''&lt;br /&gt;
&lt;br /&gt;
I use Eclipse on ubuntu. I am used to it from Java development.&lt;br /&gt;
&lt;br /&gt;
'''5.3) What programming languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
I list them from most fluent to *I have coded once with it*&lt;br /&gt;
&lt;br /&gt;
Java, Python, PHP, C++, JavaScript, Ruby, Scala, Pearl, Lua. &lt;br /&gt;
&lt;br /&gt;
'''5.4) Would you mind talking with your mentor on telephone / internet phone? We would like to have a backup way for communications for the case that somehow emails and IRC do fail. If you are willing to do so, please do list a phone number (including international code) so that we are able to contact you. You should probably *only* add this number in the application for you submit to google since the info in the wiki is available in public. We will *not* make any use of your number unless some case of &amp;quot;there is no way to contact you&amp;quot; does arise!'''&lt;br /&gt;
&lt;br /&gt;
I will add my number in the google application.&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51998</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51998"/>
		<updated>2013-09-14T17:02:19Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Aspect recruitment_instruction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white dot.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Even if only a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tag is used, the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet will get overwritten. Include the &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; mentioned in the previous point to let the AI recruit something.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
         [recruit]         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#Do not forget to include the default&amp;lt;/span&amp;gt;&lt;br /&gt;
           importance=0    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#[recruit]-tag. Otherwise the AI won't&amp;lt;/span&amp;gt;&lt;br /&gt;
         [/recruit]        &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#recruit anything.&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it is a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=yes: (boolean)''' If &amp;lt;code&amp;gt;active=no&amp;lt;/code&amp;gt; the AI will always recruit if gold is available.&lt;br /&gt;
* '''begin=1.0: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.7: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=yes&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51997</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51997"/>
		<updated>2013-09-14T16:56:40Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white dot.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
         [recruit]         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#Do not forget to include the default&amp;lt;/span&amp;gt;&lt;br /&gt;
           importance=0    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#[recruit]-tag. Otherwise the AI won't&amp;lt;/span&amp;gt;&lt;br /&gt;
         [/recruit]        &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#recruit anything.&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it is a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=yes: (boolean)''' If &amp;lt;code&amp;gt;active=no&amp;lt;/code&amp;gt; the AI will always recruit if gold is available.&lt;br /&gt;
* '''begin=1.0: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.7: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=yes&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51991</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51991"/>
		<updated>2013-09-14T13:07:47Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white dot.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it is a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=yes: (boolean)''' If &amp;lt;code&amp;gt;active=no&amp;lt;/code&amp;gt; the AI will always recruit if gold is available.&lt;br /&gt;
* '''begin=1.0: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.7: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=yes&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51984</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51984"/>
		<updated>2013-09-13T16:06:40Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Other aspects */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white dot.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it is a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=yes: (boolean)''' If &amp;lt;code&amp;gt;active=no&amp;lt;/code&amp;gt; the AI will always recruit if gold is available.&lt;br /&gt;
* '''begin=1.0: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.7: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=yes&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
=Some notes on multiple leaders=&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51983</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51983"/>
		<updated>2013-09-13T16:01:24Z</updated>

		<summary type="html">&lt;p&gt;Flixx: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white dot.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it is a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=yes: (boolean)''' If &amp;lt;code&amp;gt;active=no&amp;lt;/code&amp;gt; the AI will always recruit if gold is available.&lt;br /&gt;
* '''begin=1.0: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.7: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=yes&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
==Some notes on multiple leaders==&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51982</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51982"/>
		<updated>2013-09-13T15:58:38Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Some notes on multiple leaders */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''This page is work in progress'''&lt;br /&gt;
&lt;br /&gt;
The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white dot.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it is a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=yes: (boolean)''' If &amp;lt;code&amp;gt;active=no&amp;lt;/code&amp;gt; the AI will always recruit if gold is available.&lt;br /&gt;
* '''begin=1.0: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.7: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=yes&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
==Some notes on multiple leaders==&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units than fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51981</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51981"/>
		<updated>2013-09-13T15:54:11Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Other aspects */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''This page is work in progress'''&lt;br /&gt;
&lt;br /&gt;
The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white dot.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it is a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=yes: (boolean)''' If &amp;lt;code&amp;gt;active=no&amp;lt;/code&amp;gt; the AI will always recruit if gold is available.&lt;br /&gt;
* '''begin=1.0: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.7: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=yes&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also [[UnitTypeWML]]).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
==Some notes on multiple leaders==&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units then fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51980</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51980"/>
		<updated>2013-09-13T15:51:59Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Other aspects */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''This page is work in progress'''&lt;br /&gt;
&lt;br /&gt;
The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white dot.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it is a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=yes: (boolean)''' If &amp;lt;code&amp;gt;active=no&amp;lt;/code&amp;gt; the AI will always recruit if gold is available.&lt;br /&gt;
* '''begin=1.0: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.7: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=yes&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) With &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; it is possible to add 50 to the Grunt's score.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also UnitTypeWML).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
==Some notes on multiple leaders==&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units then fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51979</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51979"/>
		<updated>2013-09-13T15:50:40Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Other aspects */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''This page is work in progress'''&lt;br /&gt;
&lt;br /&gt;
The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white dot.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it is a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=yes: (boolean)''' If &amp;lt;code&amp;gt;active=no&amp;lt;/code&amp;gt; the AI will always recruit if gold is available.&lt;br /&gt;
* '''begin=1.0: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.7: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=yes&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-type's score.) &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; is possible to add 50.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also UnitTypeWML).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
==Some notes on multiple leaders==&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units then fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51978</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51978"/>
		<updated>2013-09-13T15:49:48Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Other aspects */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''This page is work in progress'''&lt;br /&gt;
&lt;br /&gt;
The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white dot.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it is a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=yes: (boolean)''' If &amp;lt;code&amp;gt;active=no&amp;lt;/code&amp;gt; the AI will always recruit if gold is available.&lt;br /&gt;
* '''begin=1.0: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.7: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=yes&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make an easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-types score.) &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; is possible to add 50.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also UnitTypeWML).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
==Some notes on multiple leaders==&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units then fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51977</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51977"/>
		<updated>2013-09-13T15:47:38Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''This page is work in progress'''&lt;br /&gt;
&lt;br /&gt;
The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white dot.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it is a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=yes: (boolean)''' If &amp;lt;code&amp;gt;active=no&amp;lt;/code&amp;gt; the AI will always recruit if gold is available.&lt;br /&gt;
* '''begin=1.0: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.7: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and can be used for quick copying.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=yes&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make a easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-types score). &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; is possible to add 50.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also UnitTypeWML).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
==Some notes on multiple leaders==&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units then fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51976</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51976"/>
		<updated>2013-09-13T15:45:54Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* The Aspect */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''This page is work in progress'''&lt;br /&gt;
&lt;br /&gt;
The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white dot.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it is a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt;: ''(with default values)'' &lt;br /&gt;
* '''active=yes: (boolean)''' If &amp;lt;code&amp;gt;active=no&amp;lt;/code&amp;gt; the AI will always recruit if gold is available.&lt;br /&gt;
* '''begin=1.0: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.7: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the team's start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and there for quick coping.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=yes&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make a easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-types score). &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; is possible to add 50.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also UnitTypeWML).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
==Some notes on multiple leaders==&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units then fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51975</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51975"/>
		<updated>2013-09-13T15:44:05Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* How it works */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''This page is work in progress'''&lt;br /&gt;
&lt;br /&gt;
The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white dot.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it is a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI does not recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
With this behavior the AI can save quite a lot of gold. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start an offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. This happens when an enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt; ''(with default values)'' &lt;br /&gt;
* '''active=yes: (boolean)''' If &amp;lt;code&amp;gt;active=no&amp;lt;/code&amp;gt; the AI will always recruit if gold is available.&lt;br /&gt;
* '''begin=1.0: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.7: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the teams start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and there for quick coping.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=yes&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make a easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-types score). &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; is possible to add 50.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also UnitTypeWML).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
==Some notes on multiple leaders==&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units then fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51974</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51974"/>
		<updated>2013-09-13T15:38:52Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* How it works */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''This page is work in progress'''&lt;br /&gt;
&lt;br /&gt;
The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white dot.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
For several reasons it is a good idea not to spend all gold at once:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save gold for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in 'waves' (not to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this there is the aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a 'unit_ratio'. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold ('begin') '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI doesn't recruit units anymore until the unit_ratio falls below another predefined threshold ('end'). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
One can imagine that the AI can save quite a lot of gold with this behavior. But saving gold forever is senseless. So when the AI has more gold then a predefined value ('spend_all_gold'), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start a offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. That happens when a enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt; ''(with default values)'' &lt;br /&gt;
* '''active=yes: (boolean)''' If &amp;lt;code&amp;gt;active=no&amp;lt;/code&amp;gt; the AI will always recruit if gold is available.&lt;br /&gt;
* '''begin=1.0: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.7: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the teams start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and there for quick coping.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=yes&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make a easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-types score). &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; is possible to add 50.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also UnitTypeWML).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
==Some notes on multiple leaders==&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units then fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51973</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51973"/>
		<updated>2013-09-13T15:33:05Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''This page is work in progress'''&lt;br /&gt;
&lt;br /&gt;
The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white dot.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit random units always.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work because only one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one has to create a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 1-2, a &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 3 and another &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
Sometimes it's a good idea not to spend all gold immediately for several reasons:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save money for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in &amp;quot;waves&amp;quot; (in contrary to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this we have the aspect &amp;quot;recruitment_save_gold&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a &amp;quot;unit_ratio&amp;quot;. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold (&amp;quot;begin&amp;quot;) '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI doesn't recruit units anymore until the unit_ratio falls below another predefined threshold (&amp;quot;end&amp;quot;). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
One can imagine that the AI can save quite a lot of gold with this behavior. But saving gold forever is senseless. So when the AI has more gold then a predefined value (&amp;quot;spend_all_gold&amp;quot;), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start a offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. That happens when a enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt; ''(with default values)'' &lt;br /&gt;
* '''active=yes: (boolean)''' If &amp;lt;code&amp;gt;active=no&amp;lt;/code&amp;gt; the AI will always recruit if gold is available.&lt;br /&gt;
* '''begin=1.0: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.7: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the teams start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and there for quick coping.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=yes&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make a easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-types score). &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; is possible to add 50.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also UnitTypeWML).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
==Some notes on multiple leaders==&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units then fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51972</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51972"/>
		<updated>2013-09-13T15:25:26Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''This page is work in progress'''&lt;br /&gt;
&lt;br /&gt;
The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white dot.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit as many scouts as necessary until there are 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader 2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit always random units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work. Only one facet can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one have to create a facet for turn 1-2, a facet for turn 3 and another facet for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
Sometimes it's a good idea not to spend all gold immediately for several reasons:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save money for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in &amp;quot;waves&amp;quot; (in contrary to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this we have the aspect &amp;quot;recruitment_save_gold&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a &amp;quot;unit_ratio&amp;quot;. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold (&amp;quot;begin&amp;quot;) '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI doesn't recruit units anymore until the unit_ratio falls below another predefined threshold (&amp;quot;end&amp;quot;). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
One can imagine that the AI can save quite a lot of gold with this behavior. But saving gold forever is senseless. So when the AI has more gold then a predefined value (&amp;quot;spend_all_gold&amp;quot;), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start a offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. That happens when a enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt; ''(with default values)'' &lt;br /&gt;
* '''active=yes: (boolean)''' If &amp;lt;code&amp;gt;active=no&amp;lt;/code&amp;gt; the AI will always recruit if gold is available.&lt;br /&gt;
* '''begin=1.0: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.7: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the teams start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and there for quick coping.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=yes&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make a easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-types score). &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; is possible to add 50.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also UnitTypeWML).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
==Some notes on multiple leaders==&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units then fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51971</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51971"/>
		<updated>2013-09-13T15:15:31Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Aspect recruitment_instruction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''This page is work in progress'''&lt;br /&gt;
&lt;br /&gt;
The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white dot.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* If recruitment is specified for a turn, the AI will '''not recruit any other unit''' by default. (So if the AI is told to recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit so, that there are at least 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader 2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit always random units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work. Only one facet can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one have to create a facet for turn 1-2, a facet for turn 3 and another facet for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
Sometimes it's a good idea not to spend all gold immediately for several reasons:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save money for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in &amp;quot;waves&amp;quot; (in contrary to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this we have the aspect &amp;quot;recruitment_save_gold&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a &amp;quot;unit_ratio&amp;quot;. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold (&amp;quot;begin&amp;quot;) '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI doesn't recruit units anymore until the unit_ratio falls below another predefined threshold (&amp;quot;end&amp;quot;). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
One can imagine that the AI can save quite a lot of gold with this behavior. But saving gold forever is senseless. So when the AI has more gold then a predefined value (&amp;quot;spend_all_gold&amp;quot;), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start a offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. That happens when a enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt; ''(with default values)'' &lt;br /&gt;
* '''active=yes: (boolean)''' If &amp;lt;code&amp;gt;active=no&amp;lt;/code&amp;gt; the AI will always recruit if gold is available.&lt;br /&gt;
* '''begin=1.0: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.7: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the teams start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and there for quick coping.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=yes&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make a easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-types score). &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; is possible to add 50.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also UnitTypeWML).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
==Some notes on multiple leaders==&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units then fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51970</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51970"/>
		<updated>2013-09-13T15:13:21Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Aspect recruitment_instruction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''This page is work in progress'''&lt;br /&gt;
&lt;br /&gt;
The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white dot.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'' the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* By default when recruitment is specified for a turn, the AI will '''not recruit any other unit'''. (So if we say recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit so, that there are at least 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader 2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit always random units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work. Only one facet can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one have to create a facet for turn 1-2, a facet for turn 3 and another facet for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
Sometimes it's a good idea not to spend all gold immediately for several reasons:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save money for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in &amp;quot;waves&amp;quot; (in contrary to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this we have the aspect &amp;quot;recruitment_save_gold&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a &amp;quot;unit_ratio&amp;quot;. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold (&amp;quot;begin&amp;quot;) '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI doesn't recruit units anymore until the unit_ratio falls below another predefined threshold (&amp;quot;end&amp;quot;). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
One can imagine that the AI can save quite a lot of gold with this behavior. But saving gold forever is senseless. So when the AI has more gold then a predefined value (&amp;quot;spend_all_gold&amp;quot;), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start a offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. That happens when a enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt; ''(with default values)'' &lt;br /&gt;
* '''active=yes: (boolean)''' If &amp;lt;code&amp;gt;active=no&amp;lt;/code&amp;gt; the AI will always recruit if gold is available.&lt;br /&gt;
* '''begin=1.0: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.7: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the teams start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and there for quick coping.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=yes&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make a easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-types score). &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; is possible to add 50.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also UnitTypeWML).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
==Some notes on multiple leaders==&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units then fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51969</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51969"/>
		<updated>2013-09-13T15:11:07Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Aspect recruitment_instruction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''This page is work in progress'''&lt;br /&gt;
&lt;br /&gt;
The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white dot.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified does has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' If total is set to ''yes'' the AI will count the own units on the map which match at least one of the given types and will then recruit the difference between &amp;lt;code&amp;gt;number&amp;lt;/code&amp;gt; and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'', the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes, the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing the unit-type, usage or level. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* By default when recruitment is specified for a turn, the AI will '''not recruit any other unit'''. (So if we say recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit so, that there are at least 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader 2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit always random units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work. Only one facet can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one have to create a facet for turn 1-2, a facet for turn 3 and another facet for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
Sometimes it's a good idea not to spend all gold immediately for several reasons:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save money for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in &amp;quot;waves&amp;quot; (in contrary to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this we have the aspect &amp;quot;recruitment_save_gold&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a &amp;quot;unit_ratio&amp;quot;. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold (&amp;quot;begin&amp;quot;) '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI doesn't recruit units anymore until the unit_ratio falls below another predefined threshold (&amp;quot;end&amp;quot;). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
One can imagine that the AI can save quite a lot of gold with this behavior. But saving gold forever is senseless. So when the AI has more gold then a predefined value (&amp;quot;spend_all_gold&amp;quot;), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start a offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. That happens when a enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt; ''(with default values)'' &lt;br /&gt;
* '''active=yes: (boolean)''' If &amp;lt;code&amp;gt;active=no&amp;lt;/code&amp;gt; the AI will always recruit if gold is available.&lt;br /&gt;
* '''begin=1.0: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.7: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the teams start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and there for quick coping.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=yes&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make a easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-types score). &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; is possible to add 50.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also UnitTypeWML).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
==Some notes on multiple leaders==&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units then fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51968</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51968"/>
		<updated>2013-09-13T15:08:09Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Aspect recruitment_instruction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''This page is work in progress'''&lt;br /&gt;
&lt;br /&gt;
The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white dot.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. An empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader who shall execute this job. Empty string means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function &amp;lt;code&amp;gt;check_recruit_action()&amp;lt;/code&amp;gt; in contexts.hpp will automatically choose another leader if the specified has no free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' When total is set to ''yes'' the AI will count the own units on the map which matches at least one of the given types and will then recruit the difference between ''number'' and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'', the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes, the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing the unit-type, usage or level. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* By default when recruitment is specified for a turn, the AI will '''not recruit any other unit'''. (So if we say recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit so, that there are at least 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader 2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit always random units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work. Only one facet can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one have to create a facet for turn 1-2, a facet for turn 3 and another facet for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
Sometimes it's a good idea not to spend all gold immediately for several reasons:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save money for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in &amp;quot;waves&amp;quot; (in contrary to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this we have the aspect &amp;quot;recruitment_save_gold&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a &amp;quot;unit_ratio&amp;quot;. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold (&amp;quot;begin&amp;quot;) '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI doesn't recruit units anymore until the unit_ratio falls below another predefined threshold (&amp;quot;end&amp;quot;). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
One can imagine that the AI can save quite a lot of gold with this behavior. But saving gold forever is senseless. So when the AI has more gold then a predefined value (&amp;quot;spend_all_gold&amp;quot;), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start a offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. That happens when a enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt; ''(with default values)'' &lt;br /&gt;
* '''active=yes: (boolean)''' If &amp;lt;code&amp;gt;active=no&amp;lt;/code&amp;gt; the AI will always recruit if gold is available.&lt;br /&gt;
* '''begin=1.0: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.7: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the teams start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and there for quick coping.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=yes&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make a easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-types score). &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; is possible to add 50.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also UnitTypeWML).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
==Some notes on multiple leaders==&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units then fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51967</id>
		<title>AI Recruitment</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=AI_Recruitment&amp;diff=51967"/>
		<updated>2013-09-13T15:01:34Z</updated>

		<summary type="html">&lt;p&gt;Flixx: /* Aspect recruitment_instruction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''This page is work in progress'''&lt;br /&gt;
&lt;br /&gt;
The new Recruitment Candidate Action is highly configurable and supports multiple leader recruiting.&lt;br /&gt;
&lt;br /&gt;
=How it works=&lt;br /&gt;
==Map analyis==&lt;br /&gt;
In a first step the CA will analyse the map and try to find &amp;quot;important hexes&amp;quot;. Those are locations on the map where a fight will occur with high probability. The important hexes are later used in combat simulations to calculate an average defense for the combatants.&lt;br /&gt;
&lt;br /&gt;
When the game is running in debug mode the important hexes are marked with a white dot.&lt;br /&gt;
&lt;br /&gt;
==Score Map==&lt;br /&gt;
A score map will be created for all leaders who are able to recruit. All unit-types from the recruit- and recall lists are mapped to a score. &lt;br /&gt;
&lt;br /&gt;
In the end the scores represent the desired unit-ratios on the map. (This means the AI will ''not'' just recruit the unit with the highest score, but in such a way that the mix of units on the map comes as close as possible to the mix represented by the scores.)&lt;br /&gt;
The scores are filled with values coming from combat analysis (see below).&lt;br /&gt;
&lt;br /&gt;
After that the AI modifies the scores in several ways:&lt;br /&gt;
* Similar units get a penalty for being similar. Similar units are units in one advancement tree.&lt;br /&gt;
 Example (Archer can advance to Ranger):&lt;br /&gt;
 			before	after&lt;br /&gt;
 Elvish Fighter: 	  50	 50&lt;br /&gt;
 Elvish Archer:		  50	 25&lt;br /&gt;
 Elvish Ranger:		  50	 25&lt;br /&gt;
* The aspects &amp;lt;code&amp;gt;recruitment_more&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;recruitment_diversity&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; are handled ([[#Other_aspects|see below]]).&lt;br /&gt;
&lt;br /&gt;
==Combat Analysis==&lt;br /&gt;
For each unit-type the AI's leaders can recruit, the CA will simulate a fight against all enemy units on the map. If there are less then 5 enemy units, the enemies recruitment list(s) will be taken into account.&lt;br /&gt;
&lt;br /&gt;
After Combat Analysis the score map may look like this:&lt;br /&gt;
&lt;br /&gt;
 Goblin Spearman score:		16.1252&lt;br /&gt;
 Naga Fighter score:		0&lt;br /&gt;
 Orcish Archer score:		61.6442&lt;br /&gt;
 Orcish Assassin score:		88.0737&lt;br /&gt;
 Orcish Grunt score:		100&lt;br /&gt;
 Troll Whelp score:		26.7646&lt;br /&gt;
 Wolf Rider score:		0&lt;br /&gt;
&lt;br /&gt;
===Implementation details for interested readers===&lt;br /&gt;
&lt;br /&gt;
Combat Analysis will use the function &amp;lt;code&amp;gt;compare_unit_types(A, B)&amp;lt;/code&amp;gt;. It takes two unit-types and simulates a fight using a cache because simulation is expensive. The function returns a positive value if unit-type &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; and a negative value if unit-type &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; is better then &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt;. If the return value is 2.0 it means that unit-type A is twice as good as unit-type B.&lt;br /&gt;
&lt;br /&gt;
Here is a important code-snipped of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;damage_to_b&amp;lt;/code&amp;gt; comes from simulations and is the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; chooses weapon) plus the average damage dealt by &amp;lt;code&amp;gt;A&amp;lt;/code&amp;gt; when &amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; attacks (&amp;lt;code&amp;gt;B&amp;lt;/code&amp;gt; chooses weapon). &amp;lt;code&amp;gt;damage_to_a&amp;lt;/code&amp;gt; is equivalent.&lt;br /&gt;
 double value_of_a = damage_to_b / (b_max_hp * a_cost);&lt;br /&gt;
 double value_of_b = damage_to_a / (a_max_hp * b_cost);&lt;br /&gt;
 &lt;br /&gt;
 if (value_of_a &amp;gt; value_of_b) {&lt;br /&gt;
   return value_of_a / value_of_b;&lt;br /&gt;
 } else if (value_of_a &amp;lt; value_of_b) {&lt;br /&gt;
   return -value_of_b / value_of_a;&lt;br /&gt;
 } else {&lt;br /&gt;
   return 0.;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The return-value of &amp;lt;code&amp;gt;compare_unit_types()&amp;lt;/code&amp;gt; will be multiplied by the enemies current hp and added to the score map.&lt;br /&gt;
&lt;br /&gt;
Included in the damage calculations are average defense (coming from &amp;quot;important hexes&amp;quot;), resistance, resistance abilities, average time of day, drain, poison, berserk, swarm and slows.&lt;br /&gt;
&lt;br /&gt;
At this point the scores can be quite unhandy. They could all be negative, very high or too similar. So the scores will be transformed linearly. The resulting scores are numbers between 0.0 and 100.0. &lt;br /&gt;
&lt;br /&gt;
Simplified code-snipped:&lt;br /&gt;
 double new_100 = max_score;&lt;br /&gt;
 double new_0 = max_score - (1.5 * (max_score - average_score));&lt;br /&gt;
 &lt;br /&gt;
 BOOST_FOREACH(double&amp;amp; score, scores) {&lt;br /&gt;
   score = 100 * ((score - new_0) / (new_100 - new_0)); // linear transformation&lt;br /&gt;
   if (score &amp;lt; 0.) {&lt;br /&gt;
     score = 0.;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_instruction=&lt;br /&gt;
This is a powerful aspect to completely control recruitment via WML. After the score maps are created the CA will start to execute 'jobs' (or &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags). If there is no job, the AI will do nothing at all.&lt;br /&gt;
&lt;br /&gt;
This aspect is quite complex and requires the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form of an aspect]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instruction&lt;br /&gt;
     [facet]&lt;br /&gt;
        turns=&lt;br /&gt;
        [value]&lt;br /&gt;
          &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(here multiple [recruit] and/or [limit]-tags)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [recruit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/recruit]&lt;br /&gt;
          [limit]&lt;br /&gt;
            &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
          [/limit]          &lt;br /&gt;
        [value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;:&lt;br /&gt;
* '''turns=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing numbers specifying the turns. '-' can be used between two values to define a range. An empty String means ''all turns''.&lt;br /&gt;
* See [[AiWML#A Bit More on_Simple vs. Composite Aspects|here]] for more information. But note, that &amp;lt;code&amp;gt;invalidate_on_gamestate_change&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;invalidate_on_minor_gamestate_change&amp;lt;/code&amp;gt; will have no effect on &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags. They will be read at the beginning of the turn and then be immutable. &lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing the unit-type, usage or level. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. A empty string means ''all units''. If more then one unit is specified the AI will decide according to the score map what to recruit.&lt;br /&gt;
* '''number=-1: (integer)''' A number greater than 0 will tell the AI to recruit n units. -1 means ''as much as possible''. 0 means ''do not recruit''.&lt;br /&gt;
* '''importance=1: (integer)''' The importance of a recruitment tells the AI first to recruit units with the highest importance. If gold is lacking or the castle is full, only the most important units will be recruited, the other &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-jobs will be dropped then.&lt;br /&gt;
* '''leader_id=&amp;quot;&amp;quot;: (string)''' ID of the leader which shall execute this job. Empty sting means ''all leaders''. (Note: This is only a recommendation for the AI. If the specified leader has no free hexes the AI will use other leaders to do the job. This is because the function check_recruit_action() in contexts.hpp will automatically choose another leader if the specified hasn't any free hexes)  &lt;br /&gt;
* '''total=no: (boolean)''' When total is set to ''yes'' the AI will count the own units on the map which matches at least one of the given types and will then recruit the difference between ''number'' and the counted amount.&lt;br /&gt;
* '''blocker=yes: (boolean)''' If set to ''yes'', the AI will stop recruiting when this job cannot be done (because of lacking gold or a &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; for example). If set to ''no'' the AI will drop this job and try to continue with less important ones.&lt;br /&gt;
* '''pattern=no: (boolean)''' If set to yes, the unit to recruit will not be chosen by the AI but randomly according to the frequency in the type attribute. For example when &amp;quot;type=Orcish Grunt, Orcish Grunt, scout&amp;quot; and &amp;quot;number=6&amp;quot;, the AI will recruit 6 units whereas the probability that one unit is a Grunt is twice as big as the probability that the AI will recruit a scout. If the type-attribute is empty the AI will recruit randomly. (See also [[#Other_aspects|recruitment_pattern]])&lt;br /&gt;
&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;: ''(with default values)''&lt;br /&gt;
* '''type=&amp;quot;&amp;quot;: (string)''' This key takes a comma separated list containing the unit-type, usage or level. A empty string means ''all units''.&lt;br /&gt;
* '''max=0: (int)''' The maximum of units of the given type.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
* &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt; has higher priority than &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The aspect &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; has higher priority then &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;. So if exact recruiting is wished, deactivate &amp;lt;code&amp;gt;recruitment_save_gold&amp;lt;/code&amp;gt; ([[#Aspect_recruitment_save_gold|see below]])&lt;br /&gt;
* By default when recruitment is specified for a turn, the AI will '''not recruit any other unit'''. (So if we say recruit 1 scout in turn 1 then the AI will only recruit 1 scout and not more). To prevent this behavior one can add this &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag:&lt;br /&gt;
 [recruit]&lt;br /&gt;
    importance=0&lt;br /&gt;
 [/recruit]&lt;br /&gt;
According to all the default values above (all types, as much as possible, ...) the AI will now ''fall back'' when all other recruitment jobs are done. This is also the content of the &amp;lt;code&amp;gt;[default]&amp;lt;/code&amp;gt;-facet for the aspect.&lt;br /&gt;
* Only one facet at a time can be active. When there are more &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt;s defined in a turn the behavior is undefined. In one &amp;lt;code&amp;gt;[facet]&amp;lt;/code&amp;gt; can be many &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;[limit]&amp;lt;/code&amp;gt;-tags.&lt;br /&gt;
* Do not define more than one &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tags with the same importance. Note that the aspect &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;[recruit]&amp;lt;/code&amp;gt;-tag with importance=1.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
Recruit 3 Grunts (and nothing more) in turn 3-5.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3-5'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt'''&lt;br /&gt;
           '''number=3'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit so, that there are at least 4 scouts in total and then recruit other units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=4&lt;br /&gt;
           '''total=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         '''[recruit]'''&lt;br /&gt;
           '''importance=0'''&lt;br /&gt;
         '''[/recruit]'''&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 6 Grunts or level 2 units (whatever seems better for the AI) and nothing more.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type=Orcish Grunt, 2'''&lt;br /&gt;
           number=6&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit 5 scouts with leader1 and 5 Grunts with leader 2. Do even try to recruit the Grunts if leader1 can not recruit all of the scouts.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
           '''leader_id=leader1'''&lt;br /&gt;
           '''blocker=no'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
           '''leader_id=leader2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Recruit always random units.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           '''type='''&lt;br /&gt;
           '''pattern=yes'''&lt;br /&gt;
           importance=1&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Do not recruit in turn 4.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=4'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           importance=1&lt;br /&gt;
           '''number=0'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
Do not have more than 6 units on the map.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value] &lt;br /&gt;
         [limit]&lt;br /&gt;
           type=    &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#empty type means all units&amp;lt;/span&amp;gt;&lt;br /&gt;
           max=6&lt;br /&gt;
         [/limit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
This will not work. Only one facet can be active.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment-instructions&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=1-10'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=scout&lt;br /&gt;
           number=5&lt;br /&gt;
           '''importance=1'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
     [facet]&lt;br /&gt;
       '''turns=3'''&lt;br /&gt;
       [value] &lt;br /&gt;
         [recruit]&lt;br /&gt;
           type=Orcish Grunt&lt;br /&gt;
           number=3&lt;br /&gt;
           '''importance=2'''&lt;br /&gt;
         [/recruit]&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
To achieve this behavior one have to create a facet for turn 1-2, a facet for turn 3 and another facet for turn 4-10.&lt;br /&gt;
&lt;br /&gt;
=Aspect recruitment_save_gold=&lt;br /&gt;
==How it works==&lt;br /&gt;
Sometimes it's a good idea not to spend all gold immediately for several reasons:&lt;br /&gt;
* To save the upkeep,&lt;br /&gt;
* To wait for the enemy to recruit first to counter easily,&lt;br /&gt;
* To save money for the next scenario when the enemy is almost defeated.&lt;br /&gt;
&lt;br /&gt;
At the same time it is a good idea (especially for the AI) to recruit in &amp;quot;waves&amp;quot; (in contrary to recruit one unit each turn).&lt;br /&gt;
&lt;br /&gt;
For this we have the aspect &amp;quot;recruitment_save_gold&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The Recruitment CA is always in one of the following states:&lt;br /&gt;
* NORMAL,&lt;br /&gt;
* SAVE_GOLD,&lt;br /&gt;
* SPEND_ALL_GOLD,&lt;br /&gt;
* LEADER_IN_DANGER.&lt;br /&gt;
Except the state LEADER_IN_DANGER, the states are persistent over turns.&lt;br /&gt;
&lt;br /&gt;
The AI keeps track of a &amp;quot;unit_ratio&amp;quot;. This is &amp;lt;code&amp;gt;our_total_unit_costs / enemy_total_unit_costs&amp;lt;/code&amp;gt; whereas the costs are the sum of the cost of all units on the map weighted by their HP.&lt;br /&gt;
Also the AI tries to estimate the total income over the next 5 turns. (This is a quite rough estimation, but that's fine.)&lt;br /&gt;
&lt;br /&gt;
When the AI is in state NORMAL '''and''' the unit_ratio exceeds a predefined threshold (&amp;quot;begin&amp;quot;) '''and''' the estimated income is positive, the AI will switch to the state SAVE_GOLD. Now the AI doesn't recruit units anymore until the unit_ratio falls below another predefined threshold (&amp;quot;end&amp;quot;). Then the AI will switch to the NORMAL state and recruit again.&lt;br /&gt;
&lt;br /&gt;
One can imagine that the AI can save quite a lot of gold with this behavior. But saving gold forever is senseless. So when the AI has more gold then a predefined value (&amp;quot;spend_all_gold&amp;quot;), it will switch into the state SPEND_ALL_GOLD and recruit a big army to start a offensive wave.&lt;br /&gt;
&lt;br /&gt;
The AI will ignore all those gold saving strategies when there is a LEADER_IN_DANGER. That happens when a enemy is near the leader (3 hexes).&lt;br /&gt;
&lt;br /&gt;
==The Aspect==&lt;br /&gt;
This aspect requires also the [[AiWML#A Bit More on_Simple vs. Composite Aspects|composite form]].&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         &amp;lt;span style=&amp;quot;color:#999999&amp;quot;&amp;gt;#(attributes)&amp;lt;/span&amp;gt;&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
Attributes inside &amp;lt;code&amp;gt;[value]&amp;lt;/code&amp;gt; ''(with default values)'' &lt;br /&gt;
* '''active=yes: (boolean)''' If &amp;lt;code&amp;gt;active=no&amp;lt;/code&amp;gt; the AI will always recruit if gold is available.&lt;br /&gt;
* '''begin=1.0: (double)''' See explanation above.&lt;br /&gt;
* '''end=0.7: (double)''' See explanation above.&lt;br /&gt;
* '''spend_all_gold=-1: (int)''' See explanation above. If set to -1 the value will automatically set to the teams start gold + 1.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example is filled with all default values and there for quick coping.&lt;br /&gt;
 [ai]&lt;br /&gt;
   [aspect]&lt;br /&gt;
     id=recruitment_save_gold&lt;br /&gt;
     [facet]&lt;br /&gt;
       [value]&lt;br /&gt;
         active=yes&lt;br /&gt;
         begin=1.0&lt;br /&gt;
         end=0.7&lt;br /&gt;
         spend_all_gold=-1&lt;br /&gt;
       [/value]&lt;br /&gt;
     [/facet]&lt;br /&gt;
   [/aspect]&lt;br /&gt;
 [/ai]&lt;br /&gt;
&lt;br /&gt;
=Other aspects=&lt;br /&gt;
*'''recruitment_diversity'''=1.0 (double) When this value is high, the AI will recruit more units which are currently rare on the map. (recruitment_diversity * 25 will be added to each score).&lt;br /&gt;
*'''recruitment_more'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. This is meant to let a scenario editor make a easy hack when he/she wants the AI to recruit more units of a specific type. (25 will be added to the unit-types score). &amp;lt;code&amp;gt;recruitment-more=&amp;quot;Orcish Grunt, Orcish Grunt&amp;quot;&amp;lt;/code&amp;gt; is possible to add 50.&lt;br /&gt;
*'''recruitment_randomness'''=15 (int) To each score a random value between 0 and &amp;lt;code&amp;gt;recruitment_randomness&amp;lt;/code&amp;gt; will be added. Don't hesitate to use a high value (like 200) to increase randomness.&lt;br /&gt;
*'''recruitment_pattern'''=&amp;quot;&amp;quot; (string) This key takes a comma separated list containing unit-types, usages or levels. Common usages are: 'scout', 'fighter', 'archer', 'healer' and 'mixed fighter'. This tells the AI with what probability it should recruit different types of units. The usage is listed in the unit type config files (see data/core/units/ for mainline units; see also UnitTypeWML).&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;recruitment_pattern=fighter,fighter,2&amp;lt;/code&amp;gt; means that the AI recruits on average twice as many fighters as level 2 units. It does not mean that it recruits two fighters first, then a level 2 unit, then two fighters again, etc.&lt;br /&gt;
**Internally &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; will expand into a &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;pattern=yes&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt;. (So &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; is a shortcut for a special &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt;). Make sure that there is no &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;importance=1&amp;lt;/code&amp;gt; when using &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;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.&lt;br /&gt;
&lt;br /&gt;
==Some notes on multiple leaders==&lt;br /&gt;
The CA ''can'' recruit with multiple leaders - even with different recruitment lists. Also the CA 'Move_Leader_To_Keep' will try to move all units with &amp;lt;code&amp;gt;canrecruit=yes&amp;lt;/code&amp;gt; to the next keep. &lt;br /&gt;
&lt;br /&gt;
However there are some drawbacks:&lt;br /&gt;
* In the current version the leaders will recruit by default almost the same amount of units. This can partly configured with the aspect &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; and it's attribute &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt;. A leader will recruit more units when he/she is in danger (enemies are near).&lt;br /&gt;
* As mentioned above &amp;lt;code&amp;gt;leader_id&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;recruitment_instruction&amp;lt;/code&amp;gt; is only a recommendation.&lt;br /&gt;
* Multiple leaders with different recruitment lists in combination with a &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt; may be confusing. (For the AI it's more important that the leaders recruit the same amount of units then fulfilling the ratios given in the &amp;lt;code&amp;gt;recruitment_pattern&amp;lt;/code&amp;gt;.&lt;/div&gt;</summary>
		<author><name>Flixx</name></author>
		
	</entry>
</feed>