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 dormant state. Dormant 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.

A dormant 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 dormant 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 dormant 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 of bigger to the monsters 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 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 with no melee attack reaches its target and "bumps" into it, it will move away from it for a while in a similar fashion it does when hitting any other obstacle.

If a monster has no target, it is in the dormant 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.

Ranged attack use
The further a monster is from its target, the more it will use its ranged attack (if it has one), and the closer, the less. The logic behind this is, that when the monster is in close proximity of its target, it will try to get to its target faster to be able to hit it with its melee attack.