Enemy AI

Every entity in *Risk of Rain 2* that is not controlled by the player implements a set of predefined behaviors and by using a series of logical steps it chooses how to move, engage in combat, or disengage. A lot of these behaviors have requirements to be met before they are chosen and as such the player can prevent any enemy from using a specific skill, or know when to expect a certain attack and prepare themselves in advance.

## Behaviors

Every behavior can be described by its requirements for valid selection and what actions the enemy will take upon selecting it.

### Requirements

Not every requirement has to be defined for a behavior, in which case it is assumed to have the least restrictive value.

* skill slot: defines which skill type is to be used, i.e., Primary, Secondary, Utility, or Special. Note that not all monsters have all four. The keyword None is used to not necessitate the existence of any specific skill.
* skill: the enemy must possess a certain skill. This is mostly used for the Umbra and  Survivors who can have alternative skills for any of the skill slots. If the 'skill slot' is defined, but the 'skill' is not, it will simply use whichever skill is occupying that slot.
* skill is ready: the selected skill must have at least one charge.
* equipment is ready: the user must possess an equipment and have at least one charge.
* user health: the user must have less than, more than, or a range of some health percentage.
* target health: the target must have less than, more than, or a range of some health percentage. This is mostly used by healing drones.
* distance: the target must be in less than, more than, or a range of some distance.
* line of sight: the user must have the target in line of sight, even if they are behind them.
* aim target: the user must be looking in the direction of the target.
* grounded: the user must be on the ground.
* no repetition: the same behavior cannot be chosen twice in a row.

### Actions

Only the actions defined for a specific behavior will be taken.

* ignore node graph: the user will move without using the predifined points in an Environment as target destinations, but will attempt to reach the target destination directly.
* move type: defines how the user will move, i.e., Chase, Strafe, Flee, or Stop.
* target type: defines what the user will target, i.e., Enemy, Friend, Leader, or Custom. Friend is the nearest teammate within skill range, Leader is used for the owner of any minions, e.g., Drones, , etc, and Custom is used for any targets that do not fit under the previous definitions.
* aim type: defines where the user will look, i.e., Target, Enemy, Leader, Move Direction, or None.
* sprint: the user will attempt to sprint.
* fire equipment: the user will attempt to use the equipment. Equipment that require target confirmation, such as the , can make sure the activation will succeed by using the 'line of sight' and 'aim target' requirements.
* behavior timer: how long the user will stick with this behavior before selecting a new one. A negative value means a selection will be forced on the next frame.
* next behavior: when the 'behavior timer' triggers a new selection, check if a predefined next selection is valid. This can be used to chain certain actions.
* reset enemy target: upon selecting a different behavior to the current one, choose a new enemy target. This may end up being the same one.

## Behavior Priority

Every monster has a predetermined list of behaviors and when it comes to choosing a new one the game will go over all of them in order until it finds the first one that passes all of its requirements. As such, the behaviors are generally ordered from most to least restrictive requirements. Considering skill cooldown timers or the fact that most bosses and special bosses use their Special skill only after having lost a percentage of their health, the ordering priority tends to be Special > Utility > Secondary > Primary. After that a monster may also have a few failsafe behaviors when it can't use any skills, such as strafing an enemy while waiting for a skill cooldown, repositioning, or finding a new target.

Certain behaviors can also be mutually exclusive. For example, an enemy that prefers to stay within a certain distance to use a skill will have 3 distinct behaviors which can be described as "flee and use skill if closer than some distance", "strafe and use skill if within the ideal distance range", "approach and use skill if further than some distance".

The vast number of combinations for skills, requirements, and actions can result in an engagement exhibiting complex behaviors. For example, when the  uses its Homing Orb followed by the Orb Volley, it may give the impression that the Orb Volley is just a distraction tactic to force the player to evade the attack and not focus on the homing orb. However, this is technically the result of the Secondary skill being on cooldown and the Primary skill being the only other available option.

## AI States

### Wander

The majority of enemies will be in this state when they first spawn and will have no preset behavior. They will select a random nearby position from the node graph, look at it and move towards it. After 0.5 seconds they will select a new position and will repeat this process every 0.2 seconds from then on. For a random interval between 0.5 seconds and 4 seconds the enemy selects a new look position depending on which direction they are facing. If there is something in front of them within a 25 meter distance, they may switch to the **Look Busy** state. Every frame they will also try to select a valid behavior. If one is found, they will switch to the **Combat** state.

### Look Busy

The enemy will select a duration between 2 seconds and 7 seconds randomly to stay in this state, during which they will stand still. If during this time a valid behavior is chosen, they will switch to the **Combat** state. Once this state expires, the enemy will revert back to the **Wander** state.

### Guard

This is like the **Look Busy** state, but the duration is effectively 0. Only  and the Twisted Scavenger spawn in this state. The point of this state is for the enemy to stand still until engaged in combat. Mithrix, due to the arena size and him having 360° field vision, will instantly find a target and engage in combat. The Twisted Scavenger sits facing away from the player, so until the player attacks him so he can be registered as an enemy to retaliate, or the player walks in front of the vision of the Scavenger, he will remain idle.

### Combat

This is the state that enemies will spend the majority of their lifetime. They will update their movement, aim direction, and issue button presses based on what the current behavior dictates. If in the process of selecting a new behavior the list has been exhausted without finding a valid one, the enemy will revert to its main state type.