Monster behavior modes
MonsterMode · 1 byte, mode field of MonsterRuntime
Monsters · Monster state flags · The engine in ten minutes
A monster's mode is what it does at each update: pursue the party, patrol, wander, wait, stay peaceful, or stay held. Levels set the initial mode of each monster in their INF data, and scripts set it with Place a monster.1 During play the mode changes only as listed under changing mode.
Directions are numbered clockwise (Direction), so −1 turns
left and +1 right.2
typedef enum MonsterMode {
MONSTER_MODE_PURSUE = 0, /* Attack when possible, otherwise move toward the destination (usually the party). */
MONSTER_MODE_PATROL_TURN_BACK = 1, /* Walk straight ahead; when blocked, turn around. */
MONSTER_MODE_PATROL_TURN_LEFT = 2, /* Walk straight ahead; when blocked, turn left. */
MONSTER_MODE_PATROL_TURN_RIGHT = 3, /* Walk straight ahead; when blocked, turn right. */
MONSTER_MODE_WAIT = 4, /* Stay in place until the party is noticed. */
MONSTER_MODE_WANDER_LEFT = 5, /* Roam the corridors, taking openings on the left. */
MONSTER_MODE_WANDER_RIGHT = 6, /* Roam the corridors, taking openings on the right. */
MONSTER_MODE_DELAYED = 7, /* Inactive for a set number of updates, then pursue. */
MONSTER_MODE_PEACEFUL = 8, /* Inactive and ignores the party until a monster in this mode is attacked. */
MONSTER_MODE_UNKNOWN_9 = 9, /* Inactive; ignores the party until provoked; not used by the reference data. */
MONSTER_MODE_PARALYZED = 10 /* Held by a spell for a set number of updates, then pursue. */
} MonsterMode;
A stored value of 11 to 255 bypasses the mode table, so no mode routine runs. The game sign-extends the byte and compares it, unsigned, with 10: the values 128 to 255, which read as negative, fail the test like 11 to 127. The provocation check reads the same byte as signed and runs only above 0, so 128 to 255 also escape it. No original file contains such a value and no write found in the code produces one.1
How each mode behaves
At every update, the game first checks whether the monster notices the party, then runs the routine of its mode.1
Noticing the party. In modes 0 to 6, a living monster switches to
MONSTER_MODE_PURSUE, with the party's block as destination, when the party is
less than four blocks away, unless the party is in the sector behind it and not
adjacent. Modes 7 to 10 never notice the party this way. Being provoked has
the same effect at the monster's next update, except on a paralyzed monster;
see provoked monsters. The check is
detailed in noticing the party.3
| Mode | Moves | Notices the party | Walking frame alternates | Idle animation | Leaves the mode when |
|---|---|---|---|---|---|
MONSTER_MODE_PURSUE | Toward its destination; attacks | yes | yes | yes | The next block is its destination but the party is not there: it starts wandering, left or right at random. |
MONSTER_MODE_PATROL_TURN_* | Straight on; turns when blocked | yes | yes | yes | It notices the party or is provoked. |
MONSTER_MODE_WAIT | No | yes | no | yes | It notices the party or is provoked. |
MONSTER_MODE_WANDER_* | Along the corridors, favoring one side | yes | yes | yes | It notices the party or is provoked. |
MONSTER_MODE_DELAYED | No | no | no | no | Its counter, decremented at each update, reaches zero; or it is provoked. |
MONSTER_MODE_PEACEFUL | No | no | no | yes | One of them is attacked or killed: all of them pursue. A script, a hold or Turn undead changes only that monster. |
MONSTER_MODE_UNKNOWN_9 | No | no | yes | yes | It is provoked. |
MONSTER_MODE_PARALYZED | No | no | yes | no | Its counter reaches zero. |
For a pursuing monster, noticing the party only refreshes its destination. "Provoked" covers an attack by a character and a script setting a monster flag; see provoked monsters. The idle animation is a separate timed update that moves a monster's drawing slightly; a monster turned to stone has neither animation.1
Pursuit is described in how a monster acts.
Patrols. The three patrol modes share one routine: the monster tries to step forward; when it cannot, it turns by +2 (half a turn), −1 or +1 quarter-turns according to its mode. A turn takes the update: the monster does not move until the next one.4
Wandering. The two wandering modes share another routine, which follows
openings on one side, like keeping one hand on a wall. direction_offset is −1
for MONSTER_MODE_WANDER_LEFT and +1 for MONSTER_MODE_WANDER_RIGHT.
step_state is a signed byte of MonsterRuntime,
so a stored 255 reads as −1 and follows the negative branch. try_move and try_turn
are the same lower-level operation with different arguments; their obstacle
rules are described in obstacles and other monsters.4
if signed(step_state) >= 0:
if step_state == 0:
try moving forward; if blocked, turn by -direction_offset
side = (direction + direction_offset) & 3
if step_state == 0:
if side has the passage flag:
return
step_state = -1
return
if side has the passage flag:
try_turn(side)
step_state = -1
return
if try_move(forward) succeeds:
step_state = 1
else:
try_turn((direction - direction_offset) & 3)
step_state = 0
"Has the passage flag" means that the wall between the monster's block and
the neighbor on that side has
WALL_RUNTIME_MONSTER_PASSAGE, read on the
neighbor's face toward the monster. That test alone decides which branch is
taken; try_move and try_turn then apply the full
move check: party,
wall, door and occupancy. A try_turn, called with no destination block,
only changes the facing and always succeeds.4
Delayed and paralyzed monsters. Both modes share a counter routine: a
nonzero state_counter is decremented, and when it reaches zero the monster
switches to MONSTER_MODE_PURSUE. A counter already at zero never changes, so
such a monster stays inactive until something else changes its mode. The
INF record's parameter and the spawn operand of the same name give the
initial counter.5
One routine sets MONSTER_MODE_PARALYZED with a counter of 15, unless the
monster's type has a particular immunity bit. The
spell effects on a monster use it for
Hold Person, Hold Monster and Power Word Stun.5
No code was found that sets MONSTER_MODE_DELAYED, MONSTER_MODE_PEACEFUL
or MONSTER_MODE_UNKNOWN_9: they come only from the level data or a spawn.
The two delayed monsters of the reference levels have a counter of 0, so they
stay inactive until provoked. MONSTER_MODE_UNKNOWN_9 is tested only by the
check that keeps it from noticing the party, and nothing in the code or
data gives it another purpose: such a monster stands still, does not
notice the party, keeps alternating its walking frame and pursues once
provoked. Its name stays descriptive because no use of it is known.5
Use in the reference files
The initial monsters of the 16 reference INF files use MONSTER_MODE_PURSUE
(104 monsters), MONSTER_MODE_WAIT (65), MONSTER_MODE_PEACEFUL (11),
MONSTER_MODE_WANDER_LEFT (9), and MONSTER_MODE_PATROL_TURN_LEFT,
MONSTER_MODE_PATROL_TURN_RIGHT, MONSTER_MODE_WANDER_RIGHT and
MONSTER_MODE_DELAYED (two or three each). Scripts place
monsters in MONSTER_MODE_PURSUE, MONSTER_MODE_WAIT,
MONSTER_MODE_WANDER_LEFT and MONSTER_MODE_WANDER_RIGHT only.
MONSTER_MODE_PATROL_TURN_BACK, MONSTER_MODE_UNKNOWN_9 and
MONSTER_MODE_PARALYZED never occur in the data; MONSTER_MODE_PARALYZED
arises during play from spells.6
Sources
Locations refer to the game executable reference.