Difference between revisions of "FormulaAI Functions"

From The Battle for Wesnoth Wiki
('unit_chooser' function)
m (Fix broken page formatting)
 
(89 intermediate revisions by 14 users not shown)
Line 1: Line 1:
 +
{| class="wikitable" style="text-align: center;"
 +
|'''Warning'''
 +
|-
 +
|Formula AI is still functional for the time being, but it is not maintained any more.
 +
That applies to both the code and these wiki pages.
 +
|}
 +
 +
See also: [[Formula_AI_Howto]]
 +
 
= Overview =
 
= Overview =
  
Line 15: Line 24:
 
Also function may return only single argument, or be able to return a whole list or a map.
 
Also function may return only single argument, or be able to return a whole list or a map.
  
There are a wide variety of functions which can be used to accomplish many different tasks. You can also [http://www.wesnoth.org/wiki/FormulaAI#Custom_Functions define your own functions].
+
There are a wide variety of functions which can be used to accomplish many different tasks. You can also [http://www.wesnoth.org/wiki/Wesnoth Formula Language#Functions define your own functions].
  
=core functions=
+
In addition to the functions listed here, there are several [[Wesnoth Formula Language#Core Functions|core functions]] available.
  
=== 'abs' function ===
+
= Base functions =
  
<number> = abs( <input number> )
+
== 'attack' function ==
  
Function returns absolute value of an <input number>, for example
+
<action> = attack( <attacker's position>, <destination>, <attack location> [,  <weapon> ] )
  
abs( -5 )
+
The first three parameters are locations. At the begining, unit which is standing at <attacker's position> is moved to <destination> place. Then, from that place unit is attacking unit which stands in place marked by <attack location>. Fourth optional parameter is number, and indicates which weapon attacker should use - if not specified, best possible weapon is chosed automatically.
  
will return 5.
+
== 'fallback' function ==
  
=== 'choose' function ===
+
<action> = fallback( [<name>] )
  
<result> = choose( <input list> , <formula> )
+
This function allows to chose different AI or to human player, who will take control over side untill the end of current turn. For example:
  
This function evaluates <formula> for each item in the <input list>. Will evaluate to the one item which <formula> gave the highest value. For example:
+
fallback()
  
choose(my_units, level)
+
will transfer control to the default C++ AI. You can specify a name of different AI (for example python_ai) to transfer control to it. If you want to give control to the player, use:
  
gives back the unit with the highest level.
+
fallback( 'human' )
  
'''Note:''' The implicit input when evaluating a mapping/filtering function's <formula> component will be that specific item under evaluation (in this example one of "my_units"), and it can be explicitly referenced as 'self' when necessary.
+
== 'get_unit_type' function ==
  
=== 'dir' function ===
+
<unit> = get_unit_type( <unit name> )
  
<list of names> = dir ( <input object> )
+
Function returns unit_type object of desired type, for example:
  
This function return list with all names of <input object's> members. For example:
+
get_unit_type( 'Mage' )
  
dir( my_leader )
+
will result in returning unit_type of a Mage.
  
will result in output:
+
== 'move' function ==
  
  [ 'x', 'y', 'loc', 'id', 'leader', 'hitpoints', 'max_hitpoints', 'experience', 'max_experience', 'level',
+
  <action> = move( <nowiki><source></nowiki> , <destination> )
'total_movement', 'movement_left', 'side', 'is_enemy', 'is_mine']
 
  
This command is useful in formula command line, to get information about members of different type of data. To get list of members of the ai, type:
+
Moves unit from <nowiki><source></nowiki> to <destination> and sets unit movement to 0. For example unit formula like:
  
  dir( self )
+
  move(my_leader.loc, loc(my_leader.loc.x, my_leader.loc.y - 1) )
  
=== 'filter' function ===
+
will make leader move one hex north. Leader's movement points will be reset to 0.
  
<reult list> = filter( <input list>, <formula> )
+
== 'move_partial' function ==
  
This function will run <formula> on each item in the <input list>. Will evaluate to a <result list> which only contains items the <formula> was true for. For example:
+
<action> = move_partial( <nowiki><source></nowiki> , <destination> )
  
filter(my_units, hitpoints < max_hitpoints)
+
Moves unit from <nowiki><source></nowiki> to <destination>. For example unit formula like:
  
will return all of your units which have less than maximum hitpoints. For instance this could be used if looking for candidates for healing.
+
move(my_leader.loc, loc(my_leader.loc.x, my_leader.loc.y - 1) )
  
=== 'find' function ===
+
will make leader move one hex north.
 
<result> = find( <input list>, <formula> )
 
  
This function will run <formula> on each item in the <input list> and will return a first item for which <formula> was true. For example:
 
  
filter(units, id = 'Elvish Archer' )
+
== 'run_file' function ==
  
will return first unit with id equal to 'Elvish Archer'.
+
<formula> = run_file( <string_file_location> )
  
=== 'head' function ===
+
Parses and runs the specified .fai file. 'def'-style functions in the file are added to the function table of the current AI.
 +
File location follows the usual WML convention, for example, run_file('ai/formula/test.fai') will lead to './data/ai/formula/test.fai' being loaded.
  
<variable> = head( <list of variables> )
+
At the moment, this function is useful mainly for debugging from the game console.
  
Head returns first item from the <list of variables>, for example
+
== 'set_unit_var' function ==
  
  head( [ 5, 7, 9] )           #returns 5
+
  <action> = set_unit_var( <key>, <value> , <location>)
head( [ 'Orc', 'Human' ] )    #returns 'Orc'
 
  
=== 'if' function ===
+
This action creates a new variable which is attached to the unit at <location>
  
<result> = if( <condition> , <if true> , <otherwise> )
 
  
 +
== 'set_var' function ==
  
If the <condition> parameter is true, the function will evaluate to being equal to its second input ( <if true> ), otherwise it will evaluate to being equal to its third input ( <otherwise> ).
+
<action> = set_var( <key> , <value> )
For instance, an AI that recruits Wolf Riders on the first turn, and Orcish Grunts thereafter might look like this:
 
  
move="if(turn = 1, recruit('Wolf Rider'), recruit('Orcish Grunt'))"
+
This action sets new variable, for example:
  
=== 'keys' function ===
+
set_var( 'Number one' , 1 )
  
<result list> = keys( <input map> )
+
Will create variable with name 'Number one' and assign 1 to it.
  
Extract key values from a <input map> and return them as a <result list>
+
= Evaluation =
 +
== 'calculate_outcome' function ==
  
  keys( { 'Elvish Fighter' -> 50, 'Elvish Archer' -> 60 }
+
  [<list of outcomes>]  = calculate_outcome(<attacker current location>, <attacker location> , <defender location> , [<weapon>] )
  
Returns
+
returns a list of possible outcomes when unit standing at <attacker current location> attacks the unit at <defender location> from <attacker location>
  
[ 'Elvish Fighter', 'Elvish Archer' ]
+
if no weapon is provided, it will return for the weapon considered the best by the C++ weapon choice algorithm (the one used to select default weapon in normal games)
  
=== 'map' function ===
+
== 'chance to hit' function ==
  
  <result list> = map( <input list> , <formula> )
+
  <number> = chance_to_hit( <unit> , <location> )
  
This function will run <formula> on each item in the <input list>, and evaluate to a new <result list> which contains the same number of items as in <input list>, with the formulas run on each item. For example:
+
This function returns how possible ( in % ) it is to hit given <unit> in a specific <location>. For example:
  
  map(my_units, hitpoints)  
+
  chance_to_hit( my_leader , my_leader.loc )
  
will give a list back with the number of hitpoints each unit has. This is more useful in conjunction with other functions.
+
shows how easy it is to hit your leader has in a place he is currently standing on.
  
=== 'max' function ===
 
  
<number> = max( <list of numbers> )
+
== 'evaluate_for_position' function ==
  
Function will return maximal number from a list,
+
<variant> = evaluate_for_position(<position>, <formula> )
  
max( [ 2, 8, -10, 3] )
+
Returns the result of <formula> as if <formula> was evaluated with a position of <position> instead of the current position
  
will return 8.
+
== 'max_possible_damage' function ==
  
=== 'min' function ===
+
<number> = max_possible_damage( <attacking unit> , <defending unit> )
  
<number> = min( <list of numbers> )
+
Function returns highest possible damage that <attacking unit> can inflict to <defending unit>.
  
Function will return minimal number from a list,
+
== 'max_possible_damage_with_retaliation' function ==
  
  min( [ 3, 7, -2, 6] )
+
  <number> = max_possible_damage_with_retaliation( <attacking unit> , <defending unit> )
  
will return -2.
+
Function returns an array:
  
=== 'size' function ===
+
[ <attacker_melee>, <attacker_ranged>, <defender_melee>, <defender_ranged> ]
  
<number> = size( <list of variables> )
+
in which first two elements are highest possible damage that <attacking unit> can inflict to <defending unit> with melee and ranged attacks, and latter two elements are highest possible damage that <defending unit> can inflict to <attacking unit> also with melee and ranged attacks.
  
This function returns how many variables are stored in a list:
+
== 'movement_cost' function ==
  
  size( [ 5, 7, 9] )                #return 3
+
  <number> = movement_cost( <unit> , <location> )
size( [ 'Archer', 'Fighter' ] )   #return 2
 
  
=== 'sort' function ===
+
This function returns movement cost of given <unit> in a specific <location>. For example:
  
  <result list> = sort( <input list> , <formula> )
+
  movement_cost( my_leader , my_leader.loc )
  
This function evaluates to a <result list> sorted according to the comparison <formula> for each item 'a' and its successor 'b'. For instance, sorting units according to hitpoints would be done by:
+
shows what movement cost your leader has in a place he is currently standing on.
  
sort( my_units, a.hitpoints > b.hitpoints )
+
== 'next_hop' function ==
 +
  [<location>] = next_hop( src,dst, [unit_location])
 +
this function returns the 'next hop' location to move for the unit at unit_location standing at src if it wants to move to dst. if unit_location is omitted, it defaults to src.
  
=== 'sum' function ===
+
== 'outcomes' function ==
 +
  [<list of positions>] = outcomes( <attack> )
  
<number> = sum( <list of numbers> )
+
this function take an attack (i.e a member of the "attacks" global variable) and return an array of positions
  
This function evaluates to the sum of the items in the <list of numbers>. For example
+
each position has a "chance" readable member that gives the chance of this outcome of occuring
  
sum( [ 2, 5, 8] )
+
other members of the position are not readable
  
returns 15, and:
 
  
sum( map( my_units,  max_hitpoints - hitpoints ) )
+
positions are used as parameters for the '''evaluate_for_position''' function
  
finds the total damage your units have taken.
 
  
=== 'switch' function ===
+
== 'timeofday_modifier' function ==
  
  <result> = switch( <variable>, <value 1>, <outcome 1>, ... , <value N>, <outcome N> [, <default outcome> ] >
+
  <number> = timeofday_modifier(<unit> [, <location>] )
  
Switch funtion takes variable, and checks if it is equal to any of the specified <values>. If matching value is found, <outcome> assigned to it is returned, if not, then function returns either <default outcome> (if specified) or null.
+
This function returns combat modifier (percentage, such as 25 for lawful unit during the day) due to time of day, taking alignment, illuminate, time of day and fearless trait into account. note: 'leadership' and 'slowed' are not taken into account.
  
 +
unit - unit for which to evaluate
  
=== 'values' function ===
+
location - location for which to evaluate (optional, defaults to unit's current location)
  
<result list> = values( <input map> )
+
= Gamemap functions =
  
Extract values assigned to keys from a <input map> and return them as a <result list>
 
  
values( { 'Elvish Fighter' -> 50, 'Elvish Archer' -> 60 }
+
== 'adjacent_locs' function ==
 
+
Returns
+
  <loc list> = adjacent_locs( <location> )
 
 
[ 50, 60 ]
 
 
 
= AI specific functions =
 
 
 
== Base functions ==
 
 
 
=== 'attack' function ===
 
 
 
  <action> = attack( <attacker's position>, <destination>, <attack location> [,  <weapon> ] )
 
 
 
The first three parameters are locations. At the begining, unit which is standing at <attacker's position> is moved to <destination> place. Then, from that place unit is attacking unit which stands in place marked by <attack location>. Fourth optional parameter is number, and indicates which weapon attacker should use - if not specified, best possible weapon is chosed automatically.
 
  
=== 'fallback' function ===
+
Returns a list containing  the list of the six locations adjacent to <location>
  
<action> = fallback( <name> )
 
  
This function allows to chose different AI which will take control over side untill the end of current turn. For example:
+
== 'castle_locs' function ==
  
  fallback( 'default' )
+
  [<loc list>] = castle_locs(<location>)
  
will transfer control to the default C++ AI.
 
  
 +
given a hex, this function will return the list of all locations that is connected to <location> only by keeps or castle. I.e the hexes you can recruit from, assumin that you are at a keep at <location>
  
=== 'move' function ===
+
== 'close enemies' function ==
 
 
<action> = move( <source> , <destination> )
 
 
 
For example unit formula like:
 
 
 
move(me.loc, loc(me.loc.x, me.loc.y - 1) )
 
 
 
will make unit move one hex north.
 
 
 
=== 'set_var' function ===
 
 
 
<action> = set_var( <key> , <value> )
 
 
 
This action sets new variable, for example:
 
 
 
set_var( 'Number one' , 1 )
 
 
 
Will create variable with name 'Number one' and assign 1 to it.
 
 
 
== Evaluation ==
 
 
 
=== 'chance to hit' function ===
 
 
 
<number> = chance_to_hit( <unit> , <location> )
 
 
 
This function returns how possible ( in % ) it is to hit given <unit> in a specific <location>. For example:
 
 
 
chance_to_hit( my_leader , my_leader.loc )
 
 
 
shows how easy it is to hit your leader has in a place he is currently standing on.
 
 
 
=== 'max_possible_damage' function ===
 
 
 
<number> = max_possible_damage( <attacking unit> , <defending unit> )
 
 
 
Function returns highest possible damage that <attacking unit> can inflict to <defending unit>.
 
 
 
== Gamemap functions ==
 
 
 
 
 
=== 'close enemies' function ===
 
  
 
  <units list> = close_enemies( <location> , <distance> )
 
  <units list> = close_enemies( <location> , <distance> )
Line 261: Line 211:
 
gives back a list of enemies in the distance of 5 tiles or less from the tile (10, 10).
 
gives back a list of enemies in the distance of 5 tiles or less from the tile (10, 10).
  
=== 'distance_between' function ===
+
== 'distance_between' function ==
  
 
  <number> = distance_between( <location A> , <location B> )
 
  <number> = distance_between( <location A> , <location B> )
Line 271: Line 221:
 
will return 3.
 
will return 3.
  
=== 'distance_to_nearest_unowned_village' function ===
+
== 'distance_to_nearest_unowned_village' function ==
  
 
  <number> = distance_to_nearest_unowned_village( <location A> )
 
  <number> = distance_to_nearest_unowned_village( <location A> )
Line 277: Line 227:
 
This function returns distance (in hexes) between <location A> and nearest unowned village.
 
This function returns distance (in hexes) between <location A> and nearest unowned village.
  
=== 'defense_on' function ===
+
== 'defense_on' function ==
  
 
  <number> = defense_on( <unit> , <location> )
 
  <number> = defense_on( <unit> , <location> )
Line 287: Line 237:
 
shows how good defense your leader has in a place he is currently standing on.
 
shows how good defense your leader has in a place he is currently standing on.
  
=== 'is_village' function ===
+
== 'find_shroud' function ==
 +
 
 +
<locations list> = find_shroud()
 +
 
 +
This function will return a list of locations of shrouded hexes.
 +
 
 +
 
 +
== 'is_avoided_location' function ==
 +
<boolean> = is_avoided_location(map_location)
 +
 
 +
This function will return the answer to question 'is location avoided by current ai?'
 +
 
 +
== 'is_unowned_village' function ==
 +
 
 +
<boolean> = is_unowned_village( <map or ai.map> , <location> )  #1
 +
 
 +
<boolean> = is_unowned_village( <map or ai.map> , <coordinate x> , <coordinate y> )  #2
 +
 
 +
The first argument is always a 'map' - member of the ai which provides information about the gamemap.
 +
 
 +
This will return true if we don't own the village (i.e it is unowned, owned by an ennemy, or owned by al allie)
 +
 +
 
 +
== 'is_village' function ==
  
 
  <boolean> = is_village( <map or ai.map> , <location> )  #1
 
  <boolean> = is_village( <map or ai.map> , <location> )  #1
Line 305: Line 278:
 
Remember, when using is_village in custom function, you either have to access map by writing 'ai.map', or specify ai as a 'default input'.
 
Remember, when using is_village in custom function, you either have to access map by writing 'ai.map', or specify ai as a 'default input'.
  
=== 'loc' function ===
+
== 'loc' function ==
  
 
  <location> = loc( <X number>, <Y number> )
 
  <location> = loc( <X number>, <Y number> )
Line 311: Line 284:
 
Function will return a location (pair of numbers) from two given input arguments.
 
Function will return a location (pair of numbers) from two given input arguments.
  
=== 'unit_at' function ===
+
== 'shortest_path' function ==
 +
 
 +
<list of locations> = shortest_path( <location A> , <location B> [, <unit location> ] )
 +
 
 +
When only 2 parameters are specified, function returns list with all locations that unit standing on <location A> has to move through to get to <location B>. If optional 3rd parameter is specified, it returns list with all locations that unit standing on <unit location> would need to move through to get from <location A> to <location B>. This function takes into account zone of control of enemy units.
 +
 
 +
== 'simplest_path' function ==
 +
 
 +
<list of locations> = simplest_path( <location A> , <location B> [, <unit location> ] )
 +
 
 +
When only 2 parameters are specified, function returns list with all locations that unit standing on <location A> has to move through to get to <location B>. If optional 3rd parameter is specified, it returns list with all locations that unit standing on <unit location> would need to move through to get from <location A> to <location B>. This function does not take into account zone of control or enemy units.
 +
 
 +
== 'suitable_keep' function ==
 +
<keep location> = suitable_keep( <input location> )
 +
 
 +
Function returns location of nearest keep to the unit which is located specified <input location>.It takes a location on which a unit is standing, and returns, in order of decreasing preference: nearest (by pathfinding) empty keep reachable by that unit within 1 turn, nearest (by pathfinding) occupied keep reachable within 1 turn, result of nearest_keep() implementation, null location.
 +
 
 +
== 'unit_at' function ==
  
 
  <unit> = unit_at( <location> )  
 
  <unit> = unit_at( <location> )  
Line 319: Line 309:
 
  unit_at( loc( 4, 4) )
 
  unit_at( loc( 4, 4) )
  
=== 'nearest_keep' function ===
+
== 'nearest_keep' function ==
  
 
  <keep location> = nearest_keep( <input location> )
 
  <keep location> = nearest_keep( <input location> )
Line 325: Line 315:
 
Function returns location of nearest keep to the specified <input location>, or null if there is no keep on the map.
 
Function returns location of nearest keep to the specified <input location>, or null if there is no keep on the map.
  
=== 'unit_moves' function ===
+
== 'nearest_loc' function ==
 +
 
 +
<keep location> = nearest_keep( <input location>, <list of locations> )
 +
 
 +
Function returns location that is the nearest to <input location>.
 +
For example,  " nearest_loc(loc(17,7),map(my_units,self.loc)) " - returns the location of a player-owned unit which is nearest to location (17,7)
 +
 
 +
== 'unit_moves' function ==
  
 
  <locations list> = unit_moves( <unit location> )
 
  <locations list> = unit_moves( <unit location> )
Line 331: Line 328:
 
Function returns list of all possible locations which unit standing at <unit location> can reach. If unit can't move, or there is no unit standing at given location, empty list is returned.
 
Function returns list of all possible locations which unit standing at <unit location> can reach. If unit can't move, or there is no unit standing at given location, empty list is returned.
  
=== 'units_can_reach' function ===
+
== 'units_can_reach' function ==
  
 
  <units list> = units_can_reach( <possible moves list>, <location> )
 
  <units list> = units_can_reach( <possible moves list>, <location> )
Line 337: Line 334:
 
Function takes as an input list of possible moves ( ai.my_moves for units that belong to AI, or ai.enemy_moves for units that belong to the opponent ) and checks which units from that list can reach <location>.
 
Function takes as an input list of possible moves ( ai.my_moves for units that belong to AI, or ai.enemy_moves for units that belong to the opponent ) and checks which units from that list can reach <location>.
  
== Recruitment ==
+
= Recruitment =
  
=== 'recruit' function ===
+
== 'recruit' function ==
  
 
  <action> = recruit( <unit name> [, <location> ] )
 
  <action> = recruit( <unit name> [, <location> ] )
Line 349: Line 346:
 
will result in recruting Footpad at castle hex with coordinates 3,3.
 
will result in recruting Footpad at castle hex with coordinates 3,3.
  
=== 'unit_chooser' function ===
 
 
<action> = unit_chooser( <map with unit names and weights> )
 
 
This function takes a map of unit names and attached to these names numbers - representing how should 'ideal army' look like - and as a result it recruits one unit which is most needed, according to the input map. For example:
 
 
unit_chooser( { 'Elvish Fighter' -> 60, 'Elvish Archer' -> 30, 'Elvish Shaman' -> 10 } )
 
  
We 'inform' the function that our army should consist of 60% fighters, 30% archers and 10% shamans, function checks the current ratio of the units and choses one unit that is most needed at the moment.
+
[[Category:AI]]

Latest revision as of 17:26, 18 March 2016

Warning
Formula AI is still functional for the time being, but it is not maintained any more.

That applies to both the code and these wiki pages.

See also: Formula_AI_Howto

Overview

Syntax used to explain functions usage in this document is:

<result> = <function name>( <comma-separated list of parameters> [, <comma-separated list of optional parameters] )

Function may return <result> as:

  • <variable> - any of the supported variable types
  • <boolean> - false ( 0 or null ) or true ( 1 )
  • <unit> - unit
  • <location> - place on a gamemap
  • <action> - object, which, if later passed to 'move= ' as the result of formula evaluation, make the AI perform a desired action.
  • <result> - any of the above

Also function may return only single argument, or be able to return a whole list or a map.

There are a wide variety of functions which can be used to accomplish many different tasks. You can also Formula Language#Functions define your own functions.

In addition to the functions listed here, there are several core functions available.

Base functions

'attack' function

<action> = attack( <attacker's position>, <destination>, <attack location> [,  <weapon> ] )

The first three parameters are locations. At the begining, unit which is standing at <attacker's position> is moved to <destination> place. Then, from that place unit is attacking unit which stands in place marked by <attack location>. Fourth optional parameter is number, and indicates which weapon attacker should use - if not specified, best possible weapon is chosed automatically.

'fallback' function

<action> = fallback( [<name>] )

This function allows to chose different AI or to human player, who will take control over side untill the end of current turn. For example:

fallback()

will transfer control to the default C++ AI. You can specify a name of different AI (for example python_ai) to transfer control to it. If you want to give control to the player, use:

fallback( 'human' )

'get_unit_type' function

<unit> = get_unit_type( <unit name> )

Function returns unit_type object of desired type, for example:

get_unit_type( 'Mage' )

will result in returning unit_type of a Mage.

'move' function

<action> = move( <source> , <destination> )

Moves unit from <source> to <destination> and sets unit movement to 0. For example unit formula like:

move(my_leader.loc, loc(my_leader.loc.x, my_leader.loc.y - 1) )

will make leader move one hex north. Leader's movement points will be reset to 0.

'move_partial' function

<action> = move_partial( <source> , <destination> )

Moves unit from <source> to <destination>. For example unit formula like:

move(my_leader.loc, loc(my_leader.loc.x, my_leader.loc.y - 1) )

will make leader move one hex north.


'run_file' function

<formula> = run_file( <string_file_location> )

Parses and runs the specified .fai file. 'def'-style functions in the file are added to the function table of the current AI. File location follows the usual WML convention, for example, run_file('ai/formula/test.fai') will lead to './data/ai/formula/test.fai' being loaded.

At the moment, this function is useful mainly for debugging from the game console.

'set_unit_var' function

<action> = set_unit_var( <key>, <value> , <location>)

This action creates a new variable which is attached to the unit at <location>


'set_var' function

<action> = set_var( <key> , <value> )

This action sets new variable, for example:

set_var( 'Number one' , 1 )

Will create variable with name 'Number one' and assign 1 to it.

Evaluation

'calculate_outcome' function

[<list of outcomes>]  = calculate_outcome(<attacker current location>, <attacker location> , <defender location> , [<weapon>] )

returns a list of possible outcomes when unit standing at <attacker current location> attacks the unit at <defender location> from <attacker location>

if no weapon is provided, it will return for the weapon considered the best by the C++ weapon choice algorithm (the one used to select default weapon in normal games)

'chance to hit' function

<number> = chance_to_hit( <unit> , <location> )

This function returns how possible ( in % ) it is to hit given <unit> in a specific <location>. For example:

chance_to_hit( my_leader , my_leader.loc )

shows how easy it is to hit your leader has in a place he is currently standing on.


'evaluate_for_position' function

<variant> = evaluate_for_position(<position>, <formula> )

Returns the result of <formula> as if <formula> was evaluated with a position of <position> instead of the current position

'max_possible_damage' function

<number> = max_possible_damage( <attacking unit> , <defending unit> )

Function returns highest possible damage that <attacking unit> can inflict to <defending unit>.

'max_possible_damage_with_retaliation' function

<number> = max_possible_damage_with_retaliation( <attacking unit> , <defending unit> )

Function returns an array:

[ <attacker_melee>, <attacker_ranged>, <defender_melee>, <defender_ranged> ] 

in which first two elements are highest possible damage that <attacking unit> can inflict to <defending unit> with melee and ranged attacks, and latter two elements are highest possible damage that <defending unit> can inflict to <attacking unit> also with melee and ranged attacks.

'movement_cost' function

<number> = movement_cost( <unit> , <location> )

This function returns movement cost of given <unit> in a specific <location>. For example:

movement_cost( my_leader , my_leader.loc )

shows what movement cost your leader has in a place he is currently standing on.

'next_hop' function

 [<location>] = next_hop( src,dst, [unit_location])

this function returns the 'next hop' location to move for the unit at unit_location standing at src if it wants to move to dst. if unit_location is omitted, it defaults to src.

'outcomes' function

 [<list of positions>] = outcomes( <attack> )

this function take an attack (i.e a member of the "attacks" global variable) and return an array of positions

each position has a "chance" readable member that gives the chance of this outcome of occuring

other members of the position are not readable


positions are used as parameters for the evaluate_for_position function


'timeofday_modifier' function

<number> = timeofday_modifier(<unit> [, <location>] )

This function returns combat modifier (percentage, such as 25 for lawful unit during the day) due to time of day, taking alignment, illuminate, time of day and fearless trait into account. note: 'leadership' and 'slowed' are not taken into account.

unit - unit for which to evaluate

location - location for which to evaluate (optional, defaults to unit's current location)

Gamemap functions

'adjacent_locs' function

<loc list> = adjacent_locs( <location> )

Returns a list containing the list of the six locations adjacent to <location>


'castle_locs' function

[<loc list>] = castle_locs(<location>)


given a hex, this function will return the list of all locations that is connected to <location> only by keeps or castle. I.e the hexes you can recruit from, assumin that you are at a keep at <location>

'close enemies' function

<units list> = close_enemies( <location> , <distance> )

This function gets a list of enemies in the given or smaller distance from the location. For example:

close_enemies(loc(10,10), 5)

gives back a list of enemies in the distance of 5 tiles or less from the tile (10, 10).

'distance_between' function

<number> = distance_between( <location A> , <location B> )

This function returns distance (in hexes) between <location A> and <location B>. For example:

distance_between( loc( 1, 1) , loc( 3, 3) )

will return 3.

'distance_to_nearest_unowned_village' function

<number> = distance_to_nearest_unowned_village( <location A> )

This function returns distance (in hexes) between <location A> and nearest unowned village.

'defense_on' function

<number> = defense_on( <unit> , <location> )

This function returns defense rate of given <unit> in a specific <location>. For example:

defense_on( my_leader , my_leader.loc )

shows how good defense your leader has in a place he is currently standing on.

'find_shroud' function

<locations list> = find_shroud()

This function will return a list of locations of shrouded hexes.


'is_avoided_location' function

<boolean> = is_avoided_location(map_location)

This function will return the answer to question 'is location avoided by current ai?'

'is_unowned_village' function

<boolean> = is_unowned_village( <map or ai.map> , <location> )   #1
<boolean> = is_unowned_village( <map or ai.map> , <coordinate x> , <coordinate y> )   #2

The first argument is always a 'map' - member of the ai which provides information about the gamemap.

This will return true if we don't own the village (i.e it is unowned, owned by an ennemy, or owned by al allie)


'is_village' function

<boolean> = is_village( <map or ai.map> , <location> )   #1
<boolean> = is_village( <map or ai.map> , <coordinate x> , <coordinate y> )   #2

The first argument is always a 'map' - member of the ai which provides information about the gamemap.

In #1 usage, we put in as a second argument location. In #2, second and third arguments are numbers: coordniates of the certain hex on a map. Function checks if that place is a village, and returns either 1 (yes, it is village) or 0 (no, it isn't village). Example of usage:

is_village( map , loc( 2, 3) )
is_village( map , 2, 3)

Both check, if hex with coordinates 2,3 is a village.

Remember, when using is_village in custom function, you either have to access map by writing 'ai.map', or specify ai as a 'default input'.

'loc' function

<location> = loc( <X number>, <Y number> )

Function will return a location (pair of numbers) from two given input arguments.

'shortest_path' function

<list of locations> = shortest_path( <location A> , <location B> [, <unit location> ] )

When only 2 parameters are specified, function returns list with all locations that unit standing on <location A> has to move through to get to <location B>. If optional 3rd parameter is specified, it returns list with all locations that unit standing on <unit location> would need to move through to get from <location A> to <location B>. This function takes into account zone of control of enemy units.

'simplest_path' function

<list of locations> = simplest_path( <location A> , <location B> [, <unit location> ] )

When only 2 parameters are specified, function returns list with all locations that unit standing on <location A> has to move through to get to <location B>. If optional 3rd parameter is specified, it returns list with all locations that unit standing on <unit location> would need to move through to get from <location A> to <location B>. This function does not take into account zone of control or enemy units.

'suitable_keep' function

<keep location> = suitable_keep( <input location> )

Function returns location of nearest keep to the unit which is located specified <input location>.It takes a location on which a unit is standing, and returns, in order of decreasing preference: nearest (by pathfinding) empty keep reachable by that unit within 1 turn, nearest (by pathfinding) occupied keep reachable within 1 turn, result of nearest_keep() implementation, null location.

'unit_at' function

<unit> = unit_at( <location> ) 

This function takes only one argument - location, and returns unit if there is one standing in that location, or null otherwise. Example of usage:

unit_at( loc( 4, 4) )

'nearest_keep' function

<keep location> = nearest_keep( <input location> )

Function returns location of nearest keep to the specified <input location>, or null if there is no keep on the map.

'nearest_loc' function

<keep location> = nearest_keep( <input location>, <list of locations> )

Function returns location that is the nearest to <input location>. For example, " nearest_loc(loc(17,7),map(my_units,self.loc)) " - returns the location of a player-owned unit which is nearest to location (17,7)

'unit_moves' function

<locations list> = unit_moves( <unit location> )

Function returns list of all possible locations which unit standing at <unit location> can reach. If unit can't move, or there is no unit standing at given location, empty list is returned.

'units_can_reach' function

<units list> = units_can_reach( <possible moves list>, <location> )

Function takes as an input list of possible moves ( ai.my_moves for units that belong to AI, or ai.enemy_moves for units that belong to the opponent ) and checks which units from that list can reach <location>.

Recruitment

'recruit' function

<action> = recruit( <unit name> [, <location> ] )

This function results in recruting a unit specifed by <unit name> at first free castle hex, or at given <location>. Function:

recruit('Footpad', loc(3,3) ) 

will result in recruting Footpad at castle hex with coordinates 3,3.

This page was last edited on 18 March 2016, at 17:26.