Micro AIs

From The Battle for Wesnoth Wiki
Revision as of 02:48, 15 April 2013 by Mattsc (talk | contribs) (Goto specific keys for the [micro_ai] tag:)

Mirco AIs allow Wesnoth campaign and scenario authors to add new AI functionality without the need for any AI programming. A Micro AI is activated and configured (or deleted) via the [micro_ai] tag, requiring only a few lines of WML code. Note that this is a new functionality and theat we are very much looking for feedback on bugs, new feature requests for the existing AIs or ideas for new Micro AIs.

Micro AIs are available in mainline Wesnoth starting from Version 1.11.2. Additional AIs that are still in an experimental stage are available in the AI Modification Demos add-on, which also might contain more advanced (development) versions of the mainline Micro AIs.

Contents

A Few General Words about Micro AIs

  • Micro AIs are meant to bring new specialized behavior to Wesnoth that cannot be achieved, at least not easily, by adjusting default AI parameters (such as those described at AiWML) or with WML code.
  • Most Micro AIs in their default setting apply to all units of a side, although many of them provide parameters for filtering the units that should be affected by the new behavior. If the Micro AI is applied only to part of the units of a side, the other units follow the default Wesnoth AI behavior.
  • By contrast, a few Micro AIs attach directly to a unit and only affect that unit's behavior. These are so-called Behavior Candidate Actions (BCAs). This type of Micro AI has an ai_type= value ending in _unit, in order to distinguish it from the side-wide Micro AIs.

Micro AI Test and Demo Scenarios

All Micro AIs described here can be checked out in a number of test and demo scenarios. Test scenarios can be accessed from the command line by typing

path/wesnoth-executable -t micro_ai_test

This starts up a "switchboard scenario" in which you can select the Micro AI demonstration scenario you want to check out. Here, path/wesnoth-executable needs to be replaced by whatever the path and filename of the Wesnoth executable is on your system.

Another way to test the Micro AIs (as well as some other AIs still under development) is to download the AI Modification Demos add-on. All the test/demo scenarios are also available there.

Setting up a Micro AI

Setting up a Micro AI is done in two simple steps:

1. Enabling the Micro AI functionality in the [side] tag

The desired AI functionality needs to be enabled for a side before it can be used. This is done by including a single macro line inside the [side] tag, such as:

    {MICRO_AI_HEALER_SUPPORT}

The exact macro name depends on the specific Micro AI and is given below for each AI. This macro must be put directly into the [side] tag, it does not go inside an [ai] tag. This step does not yet activate the Micro AI, it simply sets up the desired functionality.

2. Activating and configuring (or deleting) a Micro AI

Micro AIs are activated, deleted and configured using the [micro_ai] tag. This tag needs to be placed in ActionWML, that is, in an event, a menu option or the like. As an example, the following code activates the healer_support Micro AI in its default configuration for Side 2 from the beginning of the scenario:

    [event]
        name=prestart

        # Configure the healer support micro AI
        [micro_ai]
            side=2
            ai_type=healer_support
            action=add
        [/micro_ai]
    [/event]

All [micro_ai] tags must contain the following three (or four) required keys:

  • side: The side for which the Micro AI is to be added, changed or deleted
  • ai_type: The type of Micro AI to be added, changed or deleted. See the following sections for allowed values.
  • action (string): The action to take concerning the Micro AI. The following values are allowed:
    • add: Add a Micro AI to a side.
    • change: Change the configuration of an existing Micro AI. Note that this does not only change the specific parameters provided in the tag, but it replaces the entire existing Micro AI by a new version with the new (and only the new) configuration. It is therefore equivalent to using first the delete and then the add action.
    • delete: Delete an existing Micro AI from a side.
  • id: If the Micro AI attaches directly to a unit, rather than being side-wide, the id of the unit is also a required parameter.

Other keys may also be required depending on the Micro AI. In additional, a number of optional keys may be available to configure the AI behavior. See the following sections on the individual Micro AIs for details.

Notes:

  • The add and change actions ignore all keys that do not apply to the respective Micro AI type. The delete action ignores all keys other than the three (or four) required keys listed here.
  • Some of the Micro AIs use and/or modify default AI components (candidate actions or aspects) when they are added, or reset these components to their default values when they are deleted. Thus, if you have modified the AI yourself using the default CAs or aspects, some of these modifications might be affected, or might be interfering with the Micro AI. The sections below indicate whether a Micro AI modifies any default AI components.
  • It is currently not possible to combine different Micro AI types (AIs using different values of ai_type=) on the same side. In other words, you can use several guardians (and even different guardian types) on the same side (as they are all handled by the Guardian Micro AI), but it is not possible to put, for example, guardians and patrollers on the same side. This restriction will be lifted in an upcoming release. In the meantime, you will have to use different (allied) sides to accomplish this.

Animals Micro AI (ai_type=animals)

The Animals Micro AIs are meant to simulate the behaviors of certain types of animals. They are, however, set up so that they can be used with arbitrary unit types and might be applicable in quite diverse situations. The AIs nevertheless remain named after the animals for which they were written originally, so that they are (to some extent) descriptive of their behavior.

Brief descriptions of the AI behaviors are given in the following subsections. To get a better feeling for their behavior, check out the "Animals", "Swarm", "Wolves" and "Dragon" scenarios from the test scenario or in the AI Modification Demos add-on. Note that some of the animal AIs are designed for animal types that do not exist in mainline. In the test scenario, other animal types are therefore substituted for them. If you want to see them with the animal types for which they were written, check out the AI Demos add-on.

Enable an Animals Micro AI by putting

{MICRO_AI_ANIMALS}

into the [side] tag and configure it by using

ai_type=animals

in the [micro_ai] tag. The animal_type= tag is then used to select the desired animal AI.

Animals AI: Wolves (animal_type=wolves)

The Wolves Micro AI organizes all "wolf" units (predators) of the side to hunt in a single pack. They actively chase after the closest prey and try to corner it (not always super successfully), but are easily distracted by other prey coming into range, to the point where the pack splits up. The wolves can also be told to try to avoid other types of units (such as larger predators), except when they are going in for an attack. When no prey is left, the wolves wander randomly.

For a demonstration of the Wolves AI behavior, check out the "Animals" scenario from the test scenario or in the AI Modification Demos add-on.

Note that another wolves AI exists, called Multipack Wolves, which distributes the wolves of a side into several packs, rather than all of them into one single pack as it is done here.

You can configure the Wolves Micro AI by using the following keys in the [micro_ai] tag:

Required keys:

Optional keys:

  • avoid_type: Comma-separated list of unit types which the wolves try to avoid

Note: The Wolves Micro AI modifies the attacks aspect.

Here's an example of a wolves [micro_ai] tag usage from the "Animals" test scenario:

        [micro_ai]
            side=6
            ai_type=animals
            action=add

            animal_type=wolves
            [predators]
                type=Wolf
            [/predators]
            [prey]
                type=Deer
            [/prey]
            avoid_type=Yeti,Giant Spider,Tarantula,Bear,Dog
        [/micro_ai]

Animals AI: Multi-pack Wolves (animal_type=wolves_multipacks)

The Multi-pack Wolves Micro AI is different from the Wolves Micro AI in that there can be an arbitrary number of wolf packs, with the pack size being a free parameter. At the beginning of the scenario, close wolves are grouped into packs in a semi-methodical way. If the wolves are put on the map in distinct groups of close wolves of the correct number, they will be joined into packs matching these groups. If, on the other hand, they are spread out all over the map, the method of assigning them to packs is semi-random.

Wolves of the same pack begin by joining each other on the map. After that, they stay together until only one wolf is left, which then tries to join up with an incomplete pack or with other single wolves. Individual wolves entering the map during the scenario behave in that way as well.

A second difference to the other Wolves AI is that wolves do not actively hunt here. For the most part they just wander (often long distance). However, the pack ferociously (and without regard for its own health) attacks any enemy units that come into range, as long as that does not mean separating the pack by more than a few hexes. Staying together, or joining with a new wolf assigned to the pack, is the only thing that takes priority over satisfying the wolves' thirst for blood.

For a demonstration of the Multi-pack Wolves AI behavior, check out the "Wolves" scenario from the test scenario or in the AI Modification Demos add-on.

You can configure the Multi-pack Wolves Micro AI by using the following keys in the [micro_ai] tag:

Required keys:

  • animal_type: Must be "wolves_multipacks".

Optional keys:

  • type=Wolf: Comma-separated list of the unit types which the AI controls. The default value is "Wolf".
  • pack_size=3: (integer) The size of the packs.
  • show_pack_number=no: If set to "yes", the wolves' pack numbers will be shown below them.

Here's an example of a wolves_multipacks [micro_ai] tag usage from the "Wolves" test scenario:

        [micro_ai]
            side=2
            ai_type=animals
            action=add

            animal_type=wolves_multipacks
            show_pack_number=yes
            pack_size=4
        [/micro_ai]

Animals AI: Big Animals (animal_type=big_animals)

The Big Animals Micro AI is a simulation of large predators that wander and hunt alone, such as Bears, Giant Spiders or Yetis. For the most part, these just wander on the terrain that's been defined for them, but they attack enemy units if those happen to come into range. The AI can be set up so that the Big Animals stay out of the way of other units (such as other large predators), in which case they only attack those if cornered.

For a demonstration of the Big Animals AI behavior, check out the "Animals" scenario from the test scenario or in the AI Modification Demos add-on.

You can configure the Big Animals Micro AI by using the following keys in the [micro_ai] tag:

Required keys:

  • animal_type: Must be "big_animals"
  • [big_animals]: A Standard Unit Filter selecting the animals to which this AI is applied.

Optional keys:

  • [goal_terrain]: The big animals often go for long distance (multi-turn) wanders. This tag is a Standard Location Filter describing the terrain from which the next wander goal is chosen. The default is all terrain on the map.
  • [wander_terrain]: The terrain on which the big animals will end their moves. This is a Standard Location Filter. The default is all terrain on the map.
  • [avoid_unit]: A Standard Unit Filter describing all enemy units that the big animals will avoid. Note that the condition that this must be enemy units is added by the AI automatically and does not need to be included in the filter.

Here's an example of big_animals [micro_ai] tag usage from the "Animals" test scenario:

        [micro_ai]
            side=3
            ai_type=animals
            action=add

            animal_type=big_animals
            [big_animals]
                type=Bear
            [/big_animals]
            [avoid_unit]
                type=Yeti,Giant Spider,Tarantula,Bear,Dog
            [/avoid_unit]
            [goal_terrain]
                x,y=1-40,1-18
            [/goal_terrain]
            [wander_terrain]
                terrain=*
            [/wander_terrain]
        [/micro_ai]

Animals AI: Forest Animals (animal_type=forest_animals)

The Forest Animals Micro AI simulates a number of different animal behaviors that one might find in a forest, namely animals behaving like deer, rabbits and families of tuskers. In the following, we will refer to those animals by these types, even though the actual unit types can be selected freely. They behave as follows:

  • Each deer wanders randomly on (selectable) terrain, except when enemies get in its (the deer's) maximum movement range, in which case it flees to the farthest point it can reach.
  • Tuskers exhibit the same behavior as deer, except when an enemy is next to one of the tusklets. This enemy is attacked unconditionally by the tuskers trying to protect their young.
  • Tusklets blindly follow the closest adult tusker, except when there is no tusker left, in which case they behave the same as deer.
  • Rabbits also wander randomly, but in addition disappear into their holes (if any are within reach) when enemies are close. They reappear out of their holes at the beginning of the turn, if it is safe.

For a demonstration of the Forest Animals AI behavior, check out the "Animals" scenario from the test scenario or in the AI Modification Demos add-on.

You can configure the Forest Animals Micro AI by using the following keys in the [micro_ai] tag:

Required keys:

  • animal_type: Must be "forest_animals"

Optional keys:

  • rabbit_type, tusker_type, tusklet_type, deer_type: Comma-separated lists of the unit types behaving as the respective animals. If any of these parameters are not set, those types of animal behavior are skipped by the AI.
  • [wander_terrain]: A Standard Location Filter describing the terrain on which the forest animals wander when they are not threatened by enemies.
  • rabbit_number=6: (integer) The number of rabbits for the AI to have on the map simultaneously. The AI puts the missing number of rabbits onto the map at the beginning of the AI's turn (when the rabbits come out of their holes).
  • rabbit_enemy_distance=3: (number) Rabbits won't spawn in holes that aren't more than this distance away from all enemies.
  • rabbit_hole_img: Rabbit holes are marked by items on the map. If this parameter is set, it must be the file name of the item image. If it is not set, all items on the map are treated as rabbit holes.

Here's an example of forest_animals [micro_ai] tag usage from the "Animals" test scenario:

        [micro_ai]
            side=2
            ai_type=animals
            action=add

            animal_type=forest_animals
            deer_type=Deer
            rabbit_type=Rabbit
            tusker_type=Tusker
            tusklet_type=Tusklet
            [wander_terrain]
                terrain=*^F*
            [/wander_terrain]
        [/micro_ai]

Animals AI: Herd and Herders (animal_type=herding)

The Herding Micro AI sets up an area on the map where a number of herder units (such as sheep dog) control and protect a herd (such as sheep). For convenience, we simply refer to them as sheep dogs and sheep in the following, even though arbitrary unit types can be selected for either.

Sheep dogs try to keep their sheep safe. This involves keeping them inside the herding area, positioning themselves in between the sheep and approaching enemies, and attacking the enemies if those get too close. If no active herding or protecting move is needed, the dogs go to a random location on the perimeter of the herding area.

Sheep wander aimlessly except when a sheep dog is next to them, in which case they run away from the dog. The dogs exploit this by positioning themselves on the outside of the sheep, if possible. Sheep also run away from approaching enemies.

For a demonstration of the Herding AI behavior, check out the "Animals" scenario from the test scenario or in the AI Modification Demos add-on.

You can configure the Herding Micro AI by using the following keys in the [micro_ai] tag:

Required keys:

  • animal_type: Must be "herding"
  • [herd]: A Standard Unit Filter describing the herd units (e.g. sheep).
  • [herders]: A Standard Unit Filter describing the herder units (e.g. dogs).
  • [herding_perimeter]: A Standard Location Filter describing the terrain forming the perimeter of the herding area. In other words, this is the path the herders (dogs) use to patrol around the outside of the herding area. This must be a set of coordinates completely enclosing the herding area. Here's an example of setting up a rectangular perimeter:
    [herding_perimeter]
        x=10-20,10-20,10,  ,20
        y=11,   21,   11-21,11-21
    [/herding_perimeter]

Another possibility is by defining the path by certain terrain on the map (as it is done in the "Animals" test scenario), in which case the filter can simply look like this:

    [herding_perimeter]
        terrain=Rb
    [/herding_perimeter]
  • herd_x,herd_y: The coordinate of one hex (any one hex) inside the herding area. This is needed to define which part of the map is on the inside of the herding area perimeter defined by [herding_perimeter], and which is the outside.

Optional keys:

  • attention_distance=8: (integer) If an enemy is within this distance of a herd unit, but not as close as attack_distance, the herders try to position themselves in between the enemy and the herd unit.
  • attack_distance=4: (integer) If an enemy is within this distance of a herd animal, the herders attack this enemy.

Here's an example of a Herding [micro_ai] tag usage from the "Animals" test scenario:

        [micro_ai]
            side=7
            ai_type=animals
            action=add

            animal_type=herding
            [herd]
                type=Sheep,Ram
            [/herd]
            [herders]
                type=Dog
            [/herders]
            herd_x,herd_y=32,28
            [herding_perimeter]
                terrain=Rb
            [/herding_perimeter]
        [/micro_ai]

Animals AI: Hunter (animal_type=hunter_unit)

The Hunter Micro AI attaches to an individual unit which it leads through hunt-and-rest cycles. The unit wanders in a prescribed area of the map, its hunting ground, until an enemy unit comes within range. Note that it does not actively chase down enemies, but wanders randomly through its hunting ground until an enemy is encountered.

If enemies are within range, the hunter attacks the weakest of them. If a kill is made, it then retreats to its rest location, where it stays for a certain number of turns or until fully healed.

A few more details:

  • If the hunter's way home is entirely blocked on the return (so that there is no possible path), the normal RCA AI takes over its behavior.
  • However, if the way is blocked by an enemy unit occupying the rest hex, that enemy will be attacked unconditionally.
  • A kill only makes the hunter go home when it is the attacker, not as defender.

Multiple hunter units can be used on the same side, by setting up a separate [micro_ai] tag for each unit.

For a demonstration of the Hunter AI behavior, check out the "Dragon" scenario from the test scenario or in the AI Modification Demos add-on.

You can configure the Hunter Micro AI by using the following keys in the [micro_ai] tag:

Required keys:

  • animal_type: Must be "hunter_unit"
  • id: The ID of the unit to which this AI is to be attached.
  • home_x,home_y: The coordinates of the hex to which the hunter returns and rests at after a kill.

Optional keys:

  • [hunting_ground]: A Standard Location Filter describing the unit's hunting ground. If this tag is not provided, the hunter uses all of the map.
  • rest_turns=3: (integer) The number of turns the AI stays at home_x,home_y after a kill.
  • show_messages=no: (boolean) If set to yes, the hunter announces whenever its behavior changes to the next phase.

Here's an example of a hunter_unit [micro_ai] tag usage from the "Dragon" test scenario:

        [micro_ai]
            side=1
            ai_type=animals
            action=add

            animal_type=hunter_unit
            id=Rowck
            home_x,home_y=3,15

            [hunting_ground]
                x,y=5-30,1-15
            [/hunting_ground]
            rest_turns=2
            show_messages=yes
        [/micro_ai]

Animals AI: Swarm (animal_type=swarm)

The Swarm Micro AI uses some very simply algorithms to simulate animal swarm behavior. Without adjacent enemies, they simply do a random move, trying to stay together and at a certain distance from enemies. However, if an enemy unit is close to any bat, the swarm scatters. This is particular fun to watch when one places an enemy unit in the middle of the swarm. After being scattered, the swarm members slowly rejoin, but not in a very organized way. Sub-swarms or individual bats might roam around for quite some time before they find their way back. It is also possible that individual bats (or small groups) split off from the larger swarm at times.

For a demonstration of the Swarm AI behavior, check out the "Swarm" scenario from the test scenario or in the AI Modification Demos add-on.

You can configure the Swarm Micro AI by using the following keys in the [micro_ai] tag:

Required keys:

  • animal_type: Must be 'swarm'.

Optional keys:

  • scatter_distance=3: (int) Enemies within "scatter_distance" hexes of any swarm unit cause the swarm to scatter, by each unit trying to maximize its individual distance from the enemy.
  • enemy_distance=5: (int) The minimum distance kept between units of the swarm and enemies when the swarm moves as a whole.
  • vision_distance=12: (int) Only units within this distance follow the overall swarm motion (either away from an enemy or of the swarm as a whole). The smaller this value is set, the less likely the swarm is to stay together or rejoin. This parameter is meant to simulate how far an individual member of the swarm can see, meaning that the swarm is "out of sight" (and the unit will not be able to follow it) if it is more than this distance away.

Here's an example of a Swarm [micro_ai] tag usage from the "Swarm" test scenario:

        [micro_ai]
            side=2
            ai_type=animals
            action=add

            animal_type=swarm
        [/micro_ai]

Bottleneck Defense Micro AI (ai_type=bottleneck_defense)

The Bottleneck Defense Micro AI lets you define a location on the map where the AI can take a defensive stance behind a narrow passage (bottleneck). Units on the front line are backed up by healers and/or units with the leadership ability. Injured units are moved to the back and replaced by uninjured (or less injured) units. The units on the front line only attack when it is safe (no retaliation) or when there is a high chance that they will make a kill or level up. Using this Micro AI only makes sense if there is no way for the enemy sides to move around the bottleneck and attack the AI's units from behind.

For a demonstration, check out the "Bottleneck" scenario from the test scenario or in the AI Modification Demos add-on.

Enable the Bottleneck Defense Micro AI by putting

{MICRO_AI_BOTTLENECK_DEFENSE}

into the [side] tag and configure it by using

ai_type=bottleneck_defense

in the [micro_ai] tag.

Bottleneck Defense specific keys for the [micro_ai] tag:

Required keys:

The Bottleneck Defense AI requires two sets of coordinates that define where it should be taking up its defensive stance, and from which side the enemy attacks at that location.

  • x,y: Comma separated lists of the hexes on the front line, where strong units are placed. All hexes on which the AI makes contact with (can be attacked by) the enemy need to be included here. Units are placed on them in the order in which they are listed here.
  • enemy_x,enemy_y: Comma separated list of the hexes from which the enemy can attack the AI front line units. This is needed for the AI to know on which side of the front line support units (healers etc.) need to be placed. In many cases, it is sufficient to provide only one of the enemy front line hexes, but there are geometries for which that does not work. It is therefore safer to list all enemy front line hexes here.

Optional keys:

By default, the AI places healers and units with the leadership ability both on the front line itself (if they are the strongest units available) and on hexes adjacent to and behind the line. If different placement is desired, these locations can be overridden with the following keys.

  • healer_x,healer_y: Comma separated list of hexes where healers are placed. This can be used to keep healers from the front line (or to take up only certain positions on the front line), and/or to override the default healing positions behind the line. Healers are placed on them in the order in which the hexes are listed, with the exception that hexes on the line always take priority over hexes behind the line.
  • leadership_x,leadership_y: Same as healer_x,healer_y, but for units with the leadership ability.
  • active_side_leader=no: (boolean) If set to 'yes', the side leader participates in the bottleneck defense action after the side's gold has been spent. If set to 'no' (default), the leader follows default AI behavior (sitting on the keep and recruiting, for the most part).

As an example, in the "Bottleneck" scenario of the test scenario, there are three front line hexes, two of which are on hill terrain, while the third is a road with a lower defense rating. The healer and side leader are supposed to participate in combat because they are strong units, but only from the hill hexes to keep them safer. This is accomplished by using the following keys settings (check out the scenario to see which hex is which):

[micro_ai]
    side=1
    ai_type=bottleneck_defense
    action=add

    x=14,14,14
    y= 7, 9, 8
    enemy_x=13,13
    enemy_y= 8, 9

    healer_x=14,14,15,15
    healer_y= 7, 9, 8, 9
    leadership_x=14,14,15,15
    leadership_y= 7, 9, 9 ,8
    active_side_leader=yes
[/micro_ai]

Generalized Goto Micro AI (ai_type=goto)

*** Note: this Micro AI does not exist yet ***

The Goto Micro AI provides a highly configurable method of sending a unit (or units) to a location or set of locations. The units to be moved are defined using a Standard Unit Filter (SUF), while the goto locations are given in a Standard Location Filter (SLF).

By default, each unit matching the SUF will move toward the closest goal (in terms of turns needed to get there) matching the SLF via the fastest route given the current situation on the map (locations of enemy units etc.), but this behavior can be influenced by the optional keys listed below. Note that this means that a unit's goal might change from one turn to the next depending on the location of units on the map.

The behavior of the unit(s) once they reach their goal location(s) is configurable as well. By default, they will stay there until the Goto Micro AI is deleted or changed.

For demonstrations of several Goto Micro AI usages, check out the "Goto" scenario from the test scenario or in the AI Modification Demos add-on.

Enable the Goto Micro AI by putting

{MICRO_AI_GOTO}

into the [side] tag and configure it by using

ai_type=goto

in the [micro_ai] tag.

Goto specific keys for the [micro_ai] tag:

Required keys:

Optional keys:

  • avoid_enemies=0: (non-negative number) By default, units controlled by the Goto Micro AI will not take enemy units into account for route finding, other than the enemies' ZoCs. The larger the value of avoid_enemies, the more units will try to stay out of the enemies' way. (I will be more specific here once I have coded this, need to figure out how exactly it will work first).
  • ca_score=210000: (non-negative integer) The evaluation score of the candidate action controlling this Micro AI. By default, it is set to 210,000, which is higher than any of the RCA AI candidate actions. By setting it, for example, to 90,000, we can have it be executed after the combat CA, but before most of the other CAs which move units. Maybe more importantly, this key can be used to determine which Goto Micro AI is executed first if several are defined for the same side.
  • release_unit_at_goal=no: (boolean) By default, a unit that reaches a goal location will stay there until the Goto Micro AI is deleted or changed (or until the conditions specified in the [goto_goals] SLF change). If this parameter is set, a unit that has reached its goal will not be controlled by this Micro AI any more instead.
  • release_all_units_at_goal=no: (boolean) Same as release_unit_at_goal, but this will deactivate the Goto Micro AI for all units once the first unit reaches a goal.
  • use_straight_line=no: (boolean) By default, units choose the route by which they will get to their goal locations in the smallest number of turns. If this parameter is set to 'yes', a straight line route (or something pretty close to it at least) will be chosen instead, irrespective of whether this leads into a dead end, whether it is blocked by enemies, etc.
  • unique_goals=no: (boolean) If set to 'yes', each unit will be sent to a different goal location.

Goto Micro AI example usages:

Here are several examples of goto [micro_ai] tag usages (code to be filled in once this MAI is coded):

  • Send a unit towards a hex specified by coordinates (similar to the RCA AI's Goto Candidate Action, but avoiding dead ends and areas ZoCed by enemies)
  • Send all scouts of the side toward the southern border of the map
  • Set up a set of guardian units that doesn't move while no enemy unit is within 6 hexes (ironically, the Goto Micro AI can be used to keep units in place!)
  • Send an important unit to one of several sign posts, choosing the safest route
  • Send all units not involved in active battle toward a specific enemy unit

Guardian Micro AI (ai_type=guardian_unit)

The Guardian Micro AI attaches to an individual unit and can be used to set up different kind of guardian behaviors. Three different types of guardians are available and are described below.

Multiple guardian units can be used on the same side, by setting up a separate [micro_ai] tag for each unit.

For a demonstration of several different guardian behaviors, check out the "Guardians" scenario from the test scenario or in the AI Modification Demos add-on.

Enable the Guardians Micro AI by putting

{MICRO_AI_GUARDIAN}

into the [side] tag and configure it by using

ai_type=guardian_unit

in the [micro_ai] tag. The guardian_type= key also needs to be set, as shown in the following.

Return Guardian (guardian_type=return_guardian)

A 'return guardian' is a variation of the standard Wesnoth guardian. It has an assigned guard position (GP) to which it returns after attacks on approaching enemies:

  • If at GP with no enemy in reach, do nothing.
  • If at GP with an enemy in reach, attack (note that the attack is done by default AI, which might result in the guardian not attacking if the enemy is deemed too strong).
  • If not at GP, return there, no matter whether an enemy is in reach or not.
  • If enemies are blocking the way back to GP, do your best to move around them.
  • If the guardian ends up next to an enemy on the way back, attack that enemy after the move.

You can configure each return guardian using the following keys in the [micro_ai] tag:

Required keys:

  • guardian_type: Must be set to "return_guardian"
  • id: The ID of the unit to which this AI should be attached
  • return_x,return_y: The coordinate of the hex the unit returns to after each attack

Optional keys:

There are no optional keys.

Here's an example of a return guardian [micro_ai] tag usage from the "Guardians" test scenario:

        [micro_ai]
            side=2
            ai_type=guardian_unit
            action=add

            guardian_type=return_guardian
            id=return1
            return_x,return_y=20,2
        [/micro_ai]

Stationed Guardian (guardian_type=stationed_guardian)

A 'stationed guardian' is another variation of the standard Wesnoth guardian with a somewhat more complex behavior than that of the 'return guardian'. Two positions are defined for it, a 'station' and a 'guarded location', as well as a distance. The behavior is as follows:

  • If no enemy is within 'distance' of the guard's current position, do nothing. This is independent of the guardian being at its station or not.
  • Otherwise: If an enemy is within 'distance' of the guard, but not also within the same distance of the guarded location and the station (all of this simultaneously), move the guard in the direction of the station.
  • Otherwise:
    • Pick the enemy unit that is closest to the guarded location.
    • If we can reach it, pick the adjacent hex with the highest defense rating and attack from there.
    • If not in reach, move toward this unit.

You can configure each stationed guardian using the following keys in the [micro_ai] tag:

Required keys:

  • guardian_type: Must be "stationed_guardian"
  • id: The ID of the unit to which this AI should be attached
  • distance: The distance parameter as explained above
  • station_x,station_y: The x and y position of the hex that serves as the guardians station.
  • guard_x, guard_y: The x and y position of the guarded location.

Optional keys:

There are no optional keys.

Here's an example of a stationed guardian [micro_ai] tag usage from the "Guardians" test scenario:

        [micro_ai]
            side=2
            ai_type=guardian_unit
            action=add

            guardian_type=stationed_guardian
            id=stationed1
            distance=4
            station_x,station_y=2,14
            guard_x,guard_y=3,13
        [/micro_ai]

Coward (guardian_type=coward)

Cowards are units that, like guardians, sit around doing nothing until an enemy comes into range. Unlike guardians, however, they run away once enemies approach. Applications might be wild animals, unarmed civilians getting in the way of a battle, etc. The coward macro can be called with two optional locations, 'seek' and 'avoid':

  • If neither is given, the coward retreats to the position farthest away from the approaching enemies.
  • If 'seek' is given, it preferentially goes toward that location (but getting away from enemies takes priority).
  • If 'avoid' is given, it in addition tries to avoid that location (with both maximizing distance from enemies and going toward 'seek' taking priority).
  • Both 'seek' and 'avoid' may consist of only one coordinate ('x' or 'y'), in which case not a single hex, but a line of hexes is sought or avoided.

You can configure each coward using the following keys in the [micro_ai] tag:

Required keys:

  • guardian_type: Must be "coward"
  • id: The ID of the unit to which this AI should be attached
  • distance: The distance within which an enemy must be to "scare" the unit

Optional keys:

  • seek_x, seek_y: The x and y coordinate of the hex to seek
  • avoid_x, avoid_y: The x and y coordinate of the hex to avoid

Here's an example of a coward [micro_ai] tag usage from the "Guardians" test scenario:

        [micro_ai]
            side=2
            ai_type=guardian_unit
            action=add

            guardian_type=coward
            id=coward3
            distance=5
            seek_x,seek_y=24,5
            avoid_x,avoid_y=24,15
        [/micro_ai]

Healer Support Micro AI (ai_type=healer_support)

The Healer Support Micro AI configures the healers of a side to stay behind the battle lines and heal injured and/or threatened units rather than participate in combat under all circumstances. You can set different levels of aggressiveness for the healers. Note that it is not always better to use this Micro AI than the default AI, as it takes healers (which can be strong units) away from the pool of units to choose for attacks.

For a demonstration, check out the "Healers" scenario from the test scenario or in the AI Modification Demos add-on.

Enable the Healer Support Micro AI by putting

{MICRO_AI_HEALER_SUPPORT}

into the [side] tag and configure it by using

ai_type=healer_support

in the [micro_ai] tag.

Healer Support specific keys for the [micro_ai] tag:

Required keys:

There are no required keys.

Optional keys:

  • [healers]: A Standard Unit Filter selecting the healer units. If set, only units passing this filter and having the healing ability are considered by this Micro AI. Available from Wesnoth 1.11.3
  • aggression=1.0: (float) Sets the aggressiveness of the AI. This parameter is set up as a float to accommodate future functionality, but it currently acts as a boolean: if set to zero, the AI will never let its healers participate in combat, for all other values it allows them to attack after all other units have attacked and if the healers cannot find any more units to support. The former behavior might be appropriate in scenarios with only one or few valuable healers, while the latter might work better in scenarios with many healers.
  • injured_units_only=no: (boolean) If set to 'yes', the AI will only move healers next to units that are already injured and skip units that currently have full hitpoints, but might get injured during the next enemy turn.
  • max_threats=9999: (integer) The maximum allowable number of enemies that can attack a hex in order for it to be considered for a healer. As an example, setting 'max_threats=0' means that the AI only moves healers to locations that are entirely safe from the enemy (assuming that none of the units currently on the map dies). Note that the value of this key is checked against the number of enemies that can make it to the hex, not the number of adjacent hexes from which the healer could be attacked.

Note: The Healer Support Micro AI modifies the attacks aspect.

This is an example of a Healer Support Micro AI configuration:

        [micro_ai]
            side=2
            ai_type=healer_support
            action=add

            aggression=0
        [/micro_ai]

Lurkers Micro AI (ai_type=lurkers)

The Lurker Micro AI controls some or all units of a side in "lurker mode". A lurker is a unit that is capable of moving across most terrains, but that only stops on and attacks from specific terrain. It might also have the ability to hide on this terrain (which is the reason why it is called a lurker).

Lurkers move individually without strategy and always attack the weakest enemy within their reach. If no enemy is in reach, the lurker does a random move instead - or it just sits and waits (lurks).

For a demonstration of different types of lurker behavior, check out the "Lurkers" scenario from the test scenario or in the AI Modification Demos add-on.

Enable the Lurkers Micro AI by putting

{MICRO_AI_LURKERS}

into the [side] tag and configure it by using

ai_type=lurkers

in the [micro_ai] tag

Lurkers specific keys for the [micro_ai] tag:

Required keys:

  • [lurkers]: Specifies which units of the side are controlled by the Lurker Micro AI. This is a Standard Unit Filter.
  • [attack_terrain]: Specifies the terrain from which the lurkers attack. This is a Standard Location Filter.

Optional keys:

  • [wander_terrain]: Specifies the terrain on which lurkers end their moves if there is no enemy in reach. This is a Standard Location Filter. If [wander_terrain] is not given, it defaults to [attack_terrain].
  • stationary=no: (boolean) If set to yes, a lurker never moves if there is no enemy within attack range.

Here's an example of [micro_ai] tag usage from the "Lurkers" test scenario:

        [micro_ai]
            side=4
            ai_type=lurkers
            action=add

            [lurkers]
                type=Naga Fighter
            [/lurkers]
            [attack_terrain]
                terrain=W*,S*
            [/attack_terrain]
            [wander_terrain]
                terrain=W*
            [/wander_terrain]
        [/micro_ai]

Messenger Escort Micro AI (ai_type=messenger_escort)

The Messenger Escort Micro AI lets you define a set of waypoints through which it tries to move one of its units, the messenger. The other units escort the messenger, trying to protect it and attacking enemies that are in its way. Note that the messenger might not strictly hit intermediate waypoints, but that getting close is good enough, especially if a waypoint is occupied by an enemy unit. It does, however, always try to get to the last waypoint.

For a demonstration, check out the "Messenger" scenario from the test scenario or in the AI Modification Demos add-on.

Enable the Messenger Escort Micro AI by putting

{MICRO_AI_MESSENGER_ESCORT}

into the [side] tag and configure it by using

ai_type=messenger_escort

in the [micro_ai] tag.

Messenger Escort specific keys for the [micro_ai] tag:

Required keys:

  • id: The id of the messenger. All other units of the side protect the messenger.
  • waypoint_x,waypoint_y: Comma-separated list of waypoint coordinates through which the messenger will go in the given order to reach its goal, given by the last coordinate. If you want the messenger to go directly to the goal, simply enter a single x,y coordinate here. Note that the messenger does not have to hit each waypoint exactly (except for the final one), getting close to them is good enough.

Optional keys:

  • enemy_death_chance=0.67 and messenger_death_chance=0.0: (both floats; ranging from 0 to 1) If the messenger is adjacent to an enemy at the end of the move, it will only attack the enemy if the chance of death of the enemy is >= enemy_death_chance and if the messenger's chance of death is <= messenger_death_chance (or if it is an attack at a range for which the enemy does not have a weapon).

Here's an example of [micro_ai] tag usage from the "Messenger Escort" test scenario:

        [micro_ai]
            side=2
            ai_type=messenger_escort
            action=add

            id=messenger
            waypoint_x=31,24,27,28
            waypoint_y=20,14,7,1
        [/micro_ai]

Patrol Micro AI (ai_type=patrol_unit)

The Patrol Micro AI attaches to an individual unit and defines a number of waypoints for that unit to follow. Options can be set whether the route should be followed in a loop, as an out-and back, only once, whether units encountered along the way should be attacked, etc.

No matter what options are chosen, getting to the next waypoint always takes priority over attacking for a patroller. The AI thus prefers to move the patrol unit around enemies rather than straight for them. Also, if a waypoint is occupied by a unit the AI is not instructed to attack, it will (eventually) abandon that waypoint once it gets close enough and move on to the next one.

Multiple patrol units can be used on the same side, by setting up a separate [micro_ai] tag for each unit.

For a demonstration of several different patrol behaviors, check out the "Patrols" scenario from the test scenario or in the AI Modification Demos add-on.

Enable the Patrol Micro AI by putting

{MICRO_AI_PATROL}

into the [side] tag and configure it by using

ai_type=patrol_unit

in the [micro_ai] tag.

NOTE: The [micro_ai] tag needs to be placed after the creation of the unit it controls in the scenario event.

Patrol specific keys for the [micro_ai] tag:

Required keys:

  • id: The id of the patrol unit.
  • waypoint_x,waypoint_y: Comma-separated lists of the waypoint coordinates through which the patrol travels (in order).

Optional keys:

  • out_and_back=no: (boolean) By default, a patrol unit returns to the first waypoint after reaching the last, doing the patrol route as a loop. If this parameter is set to 'yes', the unit instead reverses its course and perpetually does out and backs of the route.
  • one_time_only=no: (boolean) If set to 'yes', the unit goes through the patrol route only once and then hangs out at the last waypoint. It also always attacks any enemy unit on the last waypoint, independent of what attack= is set to.
  • attack: By default (if this key is not set), the patrol unit attacks any unit it ends up next to after a move. Alternatively, this parameter can be set to a comma-separated list of unit ids. If that is done, the patrol only attacks these units when it ends up next to them and ignores any other unit. This can in turn be used to have the patroller not attack at all, by setting attack= to a non-existing id.

Here's an example of [micro_ai] tag usage from the "Patrols" test scenario:

        [micro_ai]
            side=2
            ai_type=patrol_unit
            action=add

            id=Konrad
            waypoint_x=9,24,25
            waypoint_y=21,23,15
            one_time_only=yes
            attack=Gertburt
        [/micro_ai]

Protect Unit Micro AI (ai_type=protect_unit)

The Protect Unit Micro AI tries to move one or several units to a given location while keeping them safe from enem attacks. It does so by keeping the protected units away from enemy units and close to friendly units. Other units on the AI side are controlled by the default Wesnoth AI.

For a demonstration, check out the "Protect Unit" and "The Elves Besieged" scenarios from the test scenario or in the AI Modification Demos add-on.

Enable the Protect Unit Micro AI by putting

{MICRO_AI_PROTECT_UNIT}

into the [side] tag and configure it by using

ai_type=protect_unit

in the [micro_ai] tag.

Protect Unit specific keys for the [micro_ai] tag:

Required keys:

  • [unit]: A separate [unit] tag is required for each protected unit(s). Each tag has required keys id=, goal_x=, goal_y= for each unit, as shown in the example below.

Optional keys:

  • disable_move_leader_to_keep=no: (boolean) If set to 'yes', side leaders protected by this AI do not try to return to their keep. This is necessary for this Micro AI to work with units that are side leaders and that are supposed to move to an off-keep location.

Note: The Protect Unit Micro AI modifies the attacks aspect. Depending on the parameters, it might also modify the move leader to keep candidate action.

Here's an example of [micro_ai] tag usage from the "HttT: The Elves Besieged" test scenario:

        [micro_ai]
            # Required keys of [micro_ai] tag
            side=1
            ai_type=protect_unit
            action=add

            [unit]
                id=Delfador
                goal_x,goal_y=1,2
            [/unit]
            [unit]
                id=Konrad
                goal_x,goal_y=1,1
            [/unit]

            disable_move_leader_to_keep=true
        [/micro_ai]

Recruiting Micro AI (ai_type=recruiting)

The Recruiting Micro AI replaces the default recruitment AI by alternative recruiting patterns. Currently there are two recruiting algorithms you can choose from, random recruiting and the rush recruiting used in the new (as of Version 1.11.1) Experimental AI (see below).

For a demonstration of a different recruiting pattern, check out the "Recruiting" scenario from the test scenario or in the AI Modification Demos add-on. You can also explore the unit choices by watching the Experimental AI in a multiplayer game.

Enable the Recruiting Micro AI by putting

{MICRO_AI_RECRUITING}

into the [side] tag and configure it by using

ai_type=recruiting

in the [micro_ai] tag.

Note: Obviously, the Recruitment Micro AI modifies the recruitment candidate action.

Random Recruitment AI (recruiting_type=random)

This AI randomly selects a unit type from the recruitment list of the side. The probability of choosing each type and the behavior in low-gold situations can be influenced with optional parameters.

Required keys:

  • recruiting_type: Must be "random"

Optional keys:

  • [probability]: This tag sets the probability of selecting the given unit type. It has two required sub-keys:
    • type: comma-separated list of unit types to which this probability is to be applied
    • probability: (non-negative float) The probability with which these unit types is to be selected. All unit types for which no [probability] tag is defined receive a default probability of 1.0, thus values should be chosen relative to that. As an example, in the code below swordsmen and peasants are given a probability of 8, while no other units have assigned probabilities. Thus, each of these two unit types is 8 times more likely to be selected than any other individual unit type.
  • skip_low_gold_recruit=no: (boolean) By default, the random recruitment AI chooses a unit to recruit only from those types for which it has sufficient gold. If this parameter is set to 'yes', it will instead choose from all available unit types and end recruiting for the turn if there is not enough gold to recruit it this turn (even if other, affordable unit types exist).

Here's an example of a random recruiting [micro_ai] tag usage from the "Recruiting" test scenario:

        [micro_ai]
            side=1
            ai_type=recruiting
            action=add

            recruiting_type=random
            [probability]
                type=Swordsman,Peasant
                probability=8
            [/probability]

            skip_low_gold_recruiting=yes
        [/micro_ai]

Rush Recruitment AI (recruiting_type=rushers)

This AI replaces the default recruiting with the rush recruiting used in the new (as of Version 1.11.1) Experimental AI.

Required keys:

  • recruiting_type: Must be "rushers"

Optional keys:

  • randomness=0.1: (number) A random number is applied to the rusher recruit engine's score to prevent the recruitment pattern from being too predictable. 0 causes no randomness to be applied, while larger numbers increase the random effect. A value of 1-2 generates results in which the random effect is approximately equal to the scored effect. Extremely high values are essentially entirely random.

Rush recruiting is set up like this:

        [micro_ai]
            side=1
            ai_type=recruiting
            action=add

            recruiting_type=rushers
        [/micro_ai]

Other Potential Micro AIs

The following Micro AIs might be added at some point. Feel free to add to this list, if you have other ideas.

  • Leader support
  • Targeted enemy poisoning
  • Protect unit/location/area
  • Annoying AI (grab targets and avoid fights as much as possible)
  • Orderly retreat