Monster behavior

Monsters in the Doom engine games have a primitive artificial intelligence that follows a set of behavioral rules. The following rules apply to all monsters, unless stated otherwise.

Initial state and alertness
When the map is loaded, all monsters spawn in the idle state. Idle monsters are stationary and appear to be walking in place. This is because Doom monsters have no idle frames, instead going through their first two walking frames in a loop.

An idle monster looks in a 180-degree cone towards the direction of its initial orientation . The monster's initial target is usually the player who awakened the monster by moving into its line of sight, by making an attack in an area the monster can hear, or by harming the monster. If the initial target was another monster and the monster has not perceived a player while fighting the other monster, it returns to the idle state after killing its target monster.

In addition, a monster may have the "deaf" flag set onto its map thing. "Deaf" is actually a misnomer, as this flag (called MF_AMBUSH in the source code) is not so straightforward. A monster can still hear the player but it will not awake, instead going into the "state of full spatial alertness". In this state, the monster awakes when it has a direct line of sight to the player, even without the need to face him directly (by means of a simple line-of-sight check performed between two mobjs).

Choosing and switching targets
A monster always chases one target at a time. Monsters will only choose another monster as their target if it accidentally hits them with an attack. Arch-viles can never be the target of another monster, nor can pain elementals, as the lost souls launched by a pain elemental are targeted instead.

If the monster's current target is another monster, it always turns on the player after it is done with its current enemy, and monsters resurrected by arch-viles immediately turn on the player. Lost souls damaged by other monsters forget their targets after a single counterattack, whether it was successful or not, and then turn back to the player.

Targeting threshold
After a monster awakens from its idle state and it is hit for the first time, a target countdown timer called "threshold" is activated. The longer a monster walks around with the same target, the lower the threshold gets. As long as the threshold remains positive, the monster will not change its target even if it is hit by another player or a monster. The monster will only choose a new target if the threshold reaches zero and the monster is hit by another monster or player, or if its current target dies. The threshold system does not apply to the arch-vile, which changes its target immediately to the player or monster that hurts it.

The base threshold counter is set to 100 tics, or about 3 seconds.

Movement
Monster movement system can be generally described with several rules :


 * Monsters can move in any cardinal or ordinal direction.
 * Monsters are blocked by walls and other impassable objects, including other monsters.
 * Floor elevation changes must not exceed 24 units between any two adjoining sectors.
 * The difference in height between the floor and ceiling must be equal or greater than the monster's height.
 * The space between walls and other impassable objects must exceed the monsters width by some amount. There is no general consensus on this, commonly mentioned values are between 1 and 4 map units . Additionally, monsters in very narrow passages may exhibit some counter-intuitive behavior.
 * Stairs mechanics are far more complicated. Monsters have problems with stairs with different ceiling heights between steps, as there must be clearance between the highest floor segment and the lowest ceiling segment the monster can occupy at the same time. Also, there needs to be sufficient width the steps for the monster to navigate, but again the exact values are unclear . Lastly, the faster and wider the monster is, the harder it is for it to negotiate stairs.

Monsters generally try to close in on their target via the shortest possible route. If the direct route to the target is blocked by a wall or a solid thing that the monster walks into, it randomly picks another direction to move in for a short while (to find another route to its target) until it reverts to heading straight toward its target again.

In their pursuit, monsters will try to open doors and activate lifts, except for those marked as player-only, however sometimes they succeed in that also. In addition, they have difficulties when trying to open an already opened door.

A monster does not need to have a line of sight to follow its target, and will attempt to close in on its position within the level.

If a monster has no target, it is in the idle state and will not move.

Flying movement
Monsters with the ability to fly (cacodemons, pain elementals and lost souls) are generally subjected to the rules above, except since they do not walk, they are not hindered by steps and dropoffs. Still, they obey additional restrictions :


 * A flying monster can only fly as high as the highest height it has been on.
 * A flying monster can gain new altitude (rise higher than its starting altitude) by "climbing up" against the obstacle, e.g. a decoration or the linedef between the two sectors of different heights. If said linedef is set to block monster movement, the monster will not be able to gain altitude at all.
 * A flying monster can only change elevation as fast as its base movement speed.

It should be noted that attacking lost souls move like projectiles and are not subject to these mechanics. In all other respects, they follow these patterns.

Melee and ranged attack use
When the monster wakes up upon seeing or hearing the player, it will delay its first attack by at least 8 tics (called reactiontime in the code). If aroused by damage, or when playing on Nightmare! skill level, reactiontime is set to zero, making it attack right away without any delay.

The further a monster is from its target, the less it will use its ranged attack (if it has one), and the closer, the more. This way the monster will close in on its target, where it can score more succesful hits. At long distances, spiderdemons and lost souls in particular have higher attack probability than other monsters; for cyberdemons, the probability is even higher.

While hitscans cease to trace after 2048 map units, this does not prevent monsters with bullet attack to aim and shoot at their targets beyond this range. Arch-viles will not target enemies further than 896 units away.

After attacking, the monster will make at least one move before attacking again, compelled by the game logic. On Nightmare! or when the -fast parameter is used, this constraint is omitted, making the monster shoot as long as its target stays in sight, unless it flinches after being hit.

The monster will use its melee attack if the distance is less than 64 units. Revenants are hardcoded to attack only in melee when in range of 196 units, however they still must get closer than 64 units to administer a punch.

If a monster with no melee attack reaches its target and bumps into it, it will move away in a random direction in a similar fashion it does when hitting any other obstacle.