AI

The artificial intelligence (abbreviated as AI) refers to the entity/computer controlling the enemy units whenever a player battles on a map, or the player's units when Auto-Battle is used. This includes the decision-making the computer uses to move/attack a player's units.

Movement patterns
All enemies in the game will wait until they are engaged by the player, after which the enemy will begin attacking the player units. An enemy unit is engaged if it is attacked or a player unit is present in its danger zone during the enemy phase. Enemies may also use Assists regardless of whether they have been engaged. Additionally, enemies may have additional attributes that control their individual behavior:


 * Start turn: An enemy unit may start attacking the player after waiting for a number of turns. In particular, enemies are allowed to wait for 0 turns and start attacking the player on Turn 1. Once the enemy finishes waiting, it starts attacking regardless of whether other movement attributes are defined. All enemies from Arena duels and Rival Domains start moving on Turn 1.
 * Movement group: Enemies may be split into movement groups such that engaging any enemy of that group would cause all enemies from the same group to start attacking the player. Ungrouped enemies do not affect other units when they are engaged. Radiant Hero is an example where all units are ungrouped, so enemies would remain passive even if their allies are attacked.
 * Movement delay: Enemies that belong to a movement group may additionally have a per-unit movement delay which indicates the additional number of turns to wait after any other unit from the group is engaged. Attacking any other enemy from the group would cause that enemy to start attacking 1 turn earlier. For example, the from Dual Cavaliers waits for 2 more turns if a player unit is present in the danger zone of any other enemy unit, 1 more turn if a player unit attacks any other enemy unit, and 0 turns if the unit itself is engaged.
 * Breakable terrain: Enemy units may choose to attack breakable terrain or treat breakable terrain as walls.
 * Return to base: Enemy units may or may not return to their initial positions if they cannot attack any player unit or use Assists.

The movement pattern of an enemy unit remains the same throughout the map but may change across different difficulties of the same map, such as waiting for different numbers of turns. Movement patterns for Story maps and Paralogue maps are carried over when the maps are used in other game modes such as Training Tower and Voting Gauntlet.

Spawn criteria
Foes from a map may be separated into initial units or reinforcements. At the end of each enemy phase, the game iterates through the list of reinforcement entries, checks if each entry satisfies all of the following requirements, and spawns a foe in that case:


 * Spawn location: The map must contain a free space for the reinforcement to spawn (see Spawn location below).
 * Spawn count: Each reinforcement entry spawns the same unit for a fixed number of times. Most entries only spawn 1 unit, but some may spawn as many as 3. One unit is spawned on each turn per entry until the maximum amount of spawns is reached.
 * Spawn turn: Reinforcements may have to wait for a number of turns before any unit can be spawned.
 * Alive target: A reinforcement entry is allowed to check the number of units with a given name that are alive by the end of each enemy phase, and spawn a foe only if units with that name are not present at all. In particular, an entry may check for the existence of the same unit it spawns; in this case, both initial enemies and reinforcements with this unit's name must be killed in order for new reinforcements to spawn.
 * Kill target: Similarly, a reinforcement entry may check the number of units with a given name that have been killed by the end of each enemy phase, and spawn a foe only if that kill count exceeds a given value. A reinforcement entry may only specify either an alive target or a kill target, never both at the same time. If multiple entries share the same target but have different kill counts, and more than one target unit is killed on the same turn, then both entries will spawn their enemies simultaneously, for example in Legion: Masked Maniac (map).

Slot index
The slot index is a unique integer value assigned to each unit, which increases by 1 for each unit spawned, including initial units. Reinforcements always have greater slot indices than units that have appeared before them; among reinforcements spawned on the same turn, units defined later in the map files have greater slot indices than units defined earlier. Killing a unit does not free up its slot index; newer reinforcements, including those from the same reinforcement entry, will occupy new slot indices. Player units also have slot indices which are defined according to their team/brigade's unit order.

Arena and Rival Domains maps place their initial units according to the slot indices of both teams and the internal order of the initial spawn positions. The slot index is also the final tiebreaker for several AI actions; therefore, the relative slot order between units spawned together can be determined empirically, without reading the map files.

Spawn location
When a unit is considered for spawning, its designated cell might be occupied and the game has to search for another cell to place the unit. This is done by ordering all cells on the map that are unoccupied by units, as shown below:


 * 1) If the spawn location is unoccupied, it is always chosen even if it is inaccessible.
 * 2) If the spawn location is occupied by another unit, cells closer to the spawn position have higher priority over farther cells.
 * 3) * If there is a continuous path between the cell and the spawn position, such that all cells on the path (including both endpoints) are accessible by the unit's move type, all such cells with the shortest path length, ignoring terrain costs, are chosen.
 * 4) * Otherwise, if there are accessible cells blocked off by inaccessible terrain, all the cells with the shortest distance to the spawn location, ignoring terrain costs, are chosen.
 * 5) * Otherwise, all the inaccessible cells with the shortest distance to the spawn location, ignoring terrain costs, are chosen.
 * 6) Among the chosen cells with the same distance, cells closer to the top have higher priority over cells closer to the bottom.
 * 7) Among those cells on the same row, cells closer to the right have higher priority over cells to the left.

The cell with the highest priority is then used to spawn the unit. Each reinforcement entry finishes spawning before the next entry checks for an unoccupied cell. If all cells are occupied (which does not occur through normal gameplay), the unit is not spawned and waits until any cell is unoccupied at the end of an enemy phase.

Arena Rules
The following rules will count for Arena only. If none of a player's units are within range of the enemy:


 * Movement Order
 * The order enemy units will move is as follows:
 * Melee Units (No Support Skill)
 * Ranged Units (No Support Skill)
 * Melee Units (With Support Skill)
 * Ranged Units (With Support Skill)
 * Units with no Attack (e.g. Healers)


 * If two enemy units are the same type (e.g. Two Melee Units with no Support Skills), the unit that will move first is determined by:
 * Whichever unit has the shortest movement path to a player's ally units
 * It depends on the movement cost per unit. As an example, for units, the forests count as much as 2 plains for sake of the calculation.
 * Slot Order (See Arena Maps for exact slot order/spawn locations for each Arena map, ascending order)
 * There is one exception to the rules above; if an ally uses // on the unit, then that action will take priority. If a unit can't move because they are blocked by other units, their turn will be skipped until another unit moves. If the remaining units can't move, they will get skipped and the enemy turn ends.
 * If a unit will move on a tile, which is in the range of the player's units, it will check before the unit moves if any other unit can rally this unit and will rally before the other unit moves.


 * Support Skills
 * Units will use their support skills under the following conditions:


 * For ally-positioning skills (e.g., ) – To move an ally out of enemy range
 * The AI will use the danger zone of the enemy units at the time of the AI's decision in order to determine how to move an ally out of range. This means they will not take into consideration skills that activate at the beginning of the next turn, such as 🇸🇬, and by extension the true danger zone it brings.
 * For self-positioning skills (e.g. ) – To move themself closer to the enemy
 * For bonus skills (e.g. Rallies, ) – If an ally is in enemy range
 * For healing skills (e.g. ) – If a unit can be healed


 * Movement Decisions
 * The enemy AI units when moving will have a target unit they will move towards, and they will always move in a way to shrink the distance to that target unit. The enemy units will not stop moving towards this target unit unless they are in combat range. The enemy selects targets based on who they would deal the most damage to. They will choose a target, if they have no target or their previous target died. Any set: bonuses or set: penalties will be taken into account at that time, however, they will not change their target, even if there is a more favorable one at that time.


 * They will not take their attack range in consideration, which means they will choose the shortest path to move towards their target's location rather the shortest path towards a square that attacks the target.


 * If a unit has the option to use an assist to bring a friendly unit out of fewer enemy danger zones, it will have priority over any other actions (except for //) and do so. If multiple units are able to perform this action, it will use the slot order as a tiebreaker.


 * If a unit has the option to move onto a defensive tile before doing the planned action (Support), it will do so.


 * If no other factors are involved, the unit will move in a way that seeks to equalize X-axis and Y-axis distance towards said target.


 * Breakable Blocks
 * If any breakable blocks are in the path between the enemy unit and his designated target, he will destroy the breakable blocks over moving aside even if a free path is available.

If a player's units are within range of the enemy:


 * Movement Order
 * The order enemy units will move when in range of a player's units changes based on:


 * Whether a kill can be achieved. If the enemy AI can kill a unit, it will prioritize this movement. When determining if a kill can be achieved, the AI takes into consideration:
 * Attacks from multiple units (e.g. Enemy Unit 1 and 2 attacking together to kill that unit)
 * Charge skills (e.g. Enemy Unit 1 with ready to finish a unit)
 * Movement passives (e.g. 🇸🇬, 🇸🇬)
 * Assist skills (e.g., )
 * Movement assist skills are not taken in consideration (e.g., )
 * If multiple targets can be killed by a unit during its action, it will choose to kill the target with the highest remaining HP.


 * Most Damage Dealt
 * Battles that do more damage will happen first (e.g. Enemy Unit A can do 20 damage while Enemy Unit B can do 10 damage. Enemy Unit A will move first). To determine the damage dealt, the AI takes the following factors into consideration:


 * Charge skills (e.g. Enemy Unit 1 with Astra charged to kill a unit)
 * Passive skills (e.g. 🇸🇬, 🇸🇬)
 * Movement passives (e.g. 🇸🇬, 🇸🇬)
 * Movement assist skills are not taken into consideration (e.g., )


 * Least Damage Taken
 * If the battles do the same amount of damage, it will prioritize the battles that will result in less damage taken.
 * The AI will take defensive tiles into consideration.
 * For both kills and damage dealt, the AI does NOT take into consideration whether a kill can be achieved by having a unit move into a free space. For example, if a melee unit is blocking a space that a ranged unit can move to get a kill, the enemy AI will not take this into consideration.
 * Additionally, a Refresher will prioritize attacking a unit over using // on another target even if the Dance/Sing/Gray Waves would lead to a kill afterwards.
 * It will, however, check the situation of the map after every move, so if a melee unit moved away to attack a unit and frees up the space for a range unit to kill a unit or deal more damage than the other possible battles, it will do so.
 * If a battle would result in less than 5 damage done to the target unit, the battle will have a lower priority (among all battles) and they will prioritize their Bonus Assist skills (e.g. ) or Dance/Sing/Gray Waves over initiating said battle. It still retains a higher priority over any movement without the use of any assist skill.
 * However, they will still initiate the battle, even if the unit dies before dealing damage (e.g. 🇸🇬) or does no damage to the target unit, if they have no Bonus Assist skills available.
 * Healing


 * If an enemy AI has a healing skill (e.g., ), then the enemy AI will heal the unit with the most missing HP.
 * If it is possible to attack an enemy, they will do so and prioritize it over healing based on the rules above.

Non-Arena Rules
 Whether the AI starts to move as soon as it can or some condition has to be fulfilled first depends on the map itself. Aside from that, the AI can act in 2 different ways concerning battles and decision making. The first is the aggressive stance, where the AI uses the same rules as the Arena (Listed above) and focus on dealing as much damage to the foe as possible. The second is the defensive stance, where the AI will choose to prioritize their own survival. The AI for Auto-Battle appears to use the defensive stance.

Defensive Stance rules

 * Attacking a unit
 * Any fight, that can lead to death will have the lowest prioritization possible. Priority list (highest to lowest):
 * Killing a unit
 * Taking the least amount of damage
 * Tiebreaker: Dealing the most damage
 * Tiebreaker: In the case of both taking the same amount of damage and dealing the same amount of damage, the final tiebreaker is the order of the units in the team setup. All other things (deaths avoided, kills achieved, damage dealt, damage taken) being equal, the AI prioritizes the unit in the rightmost slot of the team setup, then the second rightmost, and so on.
 * Healing a unit (unknown prioritization)
 * Allies
 * The AI will use the opponent's danger zone to determine movement assist usage to move an ally out of the danger zone. They use the danger zone at the time of their decision, meaning they will not take into consideration skills that activate at the beginning of the next phase, such as 🇸🇬, and by extension the true danger zone it brings.
 * If the AI cannot use a movement assist to save their ally if the ally cannot counterattack, they will attempt to block the opponent's path to their ally, even if they cannot counter themselves.