Creature AI

## Targeting

Targeting is done every 3 seconds if there is a player within 32 meters, otherwise every 10 seconds.

In hunt mode, the creature is always alerted.

If the creature doesn't manage to attack during the last 15 seconds, it will enter the "Unable to attack the target" mode for 15 seconds which makes structure targeting more likely.

### Creatures

The closest creature or player with following conditions is selected as the target:

* The target is hostile.
* The target can be heard or seen.
* The target is not sleeping.
* The target is within the alert range of the stay location (for tamed creatures that are commanded to stay still).
* The target is within the alert range of the player location (for tamed creatures that are commanded to follow).

If the creature is in hunt mode and can't find a target, it will automatically target the closest player within 200 meters. This doesn't require hearing or seeing the player.

### Priority structures

The closest priority structure (most Player base structures, everything in crafting tab, chests and boats) within the creature view range is selected as the target if:

* No target creature or the target creature can't be reached (when in "Unable to attack the target" mode).
* The creature can target structures (for example Deathsquitos can't).
* The creature is not tamed.

### Other structures

Random structure within 10 meters is selected as the target if:

* The creature is in "Unable to attack the target" mode.
* Target creature is a player that can't be reached.
* No target structure or the target structure can't be reached.
* The creature can target structures (for example Deathsquitos can't).
* The creature is not tamed.

## Dropping aggro

Creatures drop aggro in following conditions:

* The target hasn't been seen or heard for 30 seconds.
* The creature hasn't attacked for 60 seconds. This is skipped for bosses and event creatures (hunt mode).
* The target is outside the maximum chase distance and hasn't been seen or heard for 1 second. This is skipped for creatures with infinite chase distance and also for bosses and event creatures (hunt mode).

When dropping aggro, creatures lose the alerted status (unless in hunt mode) and forget their current target. This also resets the timer for not attacking. The next target check will be done after 5 seconds.

## Actions

Following actions are checked in order:

* Fleeing
* Consuming items
* Using skills
* Following a player (tamed only)
* Idle movement

### Randomly move around the patrol point (if set), current location (if tamed or hunting) or spawn location.

Note: Trying to heal/support/attack depends on available skills. Chosen every second.

### Fleeing

Following fleeing conditions are checked in order:

* Fleeing towards water (water creatures when on land).
* Fleeing from the closest player when trying to despawn (night time creatures during the day or event creatures after event)
* Fleeing from the target when avoiding conflict (some creatures when they are not alerted)
* Fleeing from the target when low health (currently no creatures have this behavior)
* Fleeing from fire (creatures afraid of fire)
* Fleeing from "no monsters" zone
* Fleeing from unreachable target (non-boss creatures)

#### Fleeing towards water

Creatures that are set to avoid land and are not swimming, will try to find the deepest water and flee towards it. This behavior is enabled for Leeches and Serpents.

The water is searched every 0.5 or 2 seconds (longer interval when water has been found already).

The creature checks 10 random points from 4 to 20 meters. If the lowest point is below water level, the creature will run towards it. If no sufficient point is found, the creature keeps the current direction. Pathfinding is not used for this behavior.

#### Fleeing from the closest player when trying to despawn

During day time, creatures that are set to despawn during the day will flee from the nearest player if they aren't targeting a creature or they don't have line of sight to the target. This behavior is enabled for creatures that are spawned from night-time only spawn zones. This behavior is disabled for creatures that have started taming process.

After event ends, creatures that are spawned from the event will flee from the nearest player if they aren't targeting a creature or they don't have line of sight to the target.

The creature finds the closest player within 40 meters. If no player is found, the creature disappears. Otherwise the creature uses pathfinding to move 5 meters away from the player. The pathfinding is refreshed every tick.

### When avoiding conflict: Flee from the target when not alerted and target is not within the alert range

Boars, necks when not from event.

### Using skills

Creatures update their next skill every second when they have a target. Following conditions are used for the selection:

* Attack skills are only available if the target is within the minimum and maximum attack range.
* Healing skills are only available when the creature is hurt or there is a hurt friendly creature within the vision range (line of sight is not required).
* Support skills are only available when there is another friendly creature within the vision range (line of sight is not required).
* Skills which are on cooldown won't be available.
* Skills which are only usable when walking, swimming or flying won't be available when in the wrong mode.

The next skill is randomly selected from the available attacks, unless one of the skills is marked as priority.

If no skills are available, then the next skill is randomly selected from attack skills that can't reach the target (excluding attacks where the target is too close). If still no skill, then the next skill is randomly selected from all attacks, even if they are on cooldown (excluding attacks where the target is too close).

If the next skill is on cooldown:

* Circulate around the target within random move distance, if the creature is set to circle around the target.
* Move towards the target until within attack range and wait until the skill is ready.

If the next skill is ready:

* Structures: Move towards the target until within attack range and there is line of sight. Then attack the target.
* Hidden creatures: Move towards the last known location. When reached, randomly move around the location. After 15 seconds of not attacking, prioritize structures for 15 seconds.
* Detected creatures: Update last known location. Move towards the target until within attack range and there is line of sight. Become alerted when the target is within alert range and there is line of sight. Attack the target when target is within the attack range.