Skip to main content

Monster slot

MonsterRuntime · 30-byte runtime structure

The engine in ten minutes · Monsters · MonsterMode · INF format

In brief​

The game keeps every monster of the current level in a fixed array of 30 slots of 30 bytes each. A slot holds the monster's type, position, facing, behavior mode, hit points and destination; it is built from the 14-byte monster record of the level's INF data, or by a script that spawns a monster.1

Layout​

Fields marked inferred in the comments are inferred from the data flow and comparative sources.

/* One monster's state while the game is running (30 bytes, little-endian). */
typedef struct MonsterRuntime {
uint8_t type_index; /* 0x00: selects the monster type's properties */
uint8_t update_group; /* 0x01: update group; timed updates exist for groups 0-3 */
uint16_t block; /* 0x02: level block currently occupied */
uint8_t position; /* 0x04: 0-3 the quarters of the block, numbered as item floor positions; 4 the centre */
uint8_t direction; /* 0x05: facing, a Direction value */
uint8_t walk_frame; /* 0x06: 0 or 1, alternated by updates outside the waiting, delayed and peaceful modes and by the melee animation; selects the drawing frame */
uint8_t graphics_index; /* 0x07: graphics slot of the level: 0 slot 0, nonzero slot 1 (shapes and DCR decorations from 18) */
uint8_t mode; /* 0x08: MonsterMode, the current behavior */
uint8_t step_state; /* 0x09: movement state; read as signed by the wander modes */
int8_t attack_frame; /* 0x0a: signed; -2 and -1 during the melee animation, 0 otherwise; cleared when the level is left */
uint8_t state_counter; /* 0x0b: updates left in MONSTER_MODE_DELAYED or MONSTER_MODE_PARALYZED; moves left while fleeing */
uint16_t max_hp_raw; /* 0x0c: initial maximum hit points */
uint16_t current_hp_raw; /* 0x0e: current hit points; −1 once dead */
uint16_t destination_block; /* 0x10: block the monster is heading for; 0 ([00:00]) until first set */
uint16_t random_item_raw; /* 0x12: random item, dropped as a copy with a one-in-ten chance at death */
uint16_t fixed_item_raw; /* 0x14: fixed item, always dropped as a copy at death */
uint8_t flags; /* 0x16: MonsterRuntimeFlags */
uint8_t idle_offset; /* 0x17: drawing offset, x in the high four bits, y in the low four */
uint8_t ranged_weapon_index; /* 0x18: next of the type's ranged weapons when they are used in turn; 0 at initialization */
uint8_t ranged_attacks_left; /* 0x19: ranged attacks left, from the monster type; 0 none, 255 never runs out */
uint8_t slot_mod_3; /* 0x1a: slot index modulo 3; 1 or 2 draws the body in that colour variant of its image */
uint8_t skip_update; /* 0x1b: set to 1 when a class-1 monster of its block turns it; it misses its next update */
uint8_t ranged_wait; /* 0x1c: wait counter of ranged attacks; 1d3 at initialization */
uint8_t sublevel; /* 0x1d: sublevel current at initialization */
} MonsterRuntime;

There is no padding. mode holds a MonsterMode value and flags combines MonsterRuntimeFlags. The wander modes read step_state as signed: a stored 255 means −1 there.2 How the destination is set and used is explained in where the destination comes from; a block is written [XX:YY], column then row, with block = YY × 32 + XX. How position is chosen when monsters share a block is described in sharing a block. ranged_weapon_index, ranged_attacks_left and ranged_wait drive ranged attacks; walk_frame, attack_frame and skip_update are described in what a monster's state changes.3

From the INF record to the slot​

The 14-byte INF record is not copied as is. Initialization clears the whole slot, copies or transforms the record's fields, derives other values from the monster type, then places the monster on its block. The loader's input is the decoded INF record.1

INF fieldSlot fieldInitialization
slot_index—Chooses the slot; not stored in it.
type_indextype_indexCopied.
unitupdate_groupDoubled, plus one for an odd slot index; see update cadence.
block, directionblock, directionSet by the placement step; -1 leaves the field as it was.
position, graphics_index, modeCorresponding fieldsCopied.
parameterstate_counterCopied: the initial counter of MONSTER_MODE_DELAYED.
random_item, fixed_itemrandom_item_raw, fixed_item_rawCopied.
— (monster type's hit dice)max_hp_raw, current_hp_rawBoth receive the same dice roll.
—destination_blockCleared to 0 with the slot; set later by behavior.

Other initialized fields​

Initialization, for level monsters and spawned ones alike, also sets ranged_weapon_index to 0, ranged_attacks_left from the monster type, slot_mod_3 to the slot index modulo 3, ranged_wait to a 1d3 roll and sublevel to the low byte of the current sublevel. SCRIPT_SPAWN_MONSTER uses the same initialization with its own operands. Condition scripts count only the monsters whose sublevel is the current one; nothing else reads the field, so the monsters of another sublevel are updated and drawn like the others (see sublevels). The level loader then sets MONSTER_RUNTIME_KEEP_SLOT in the flags of every monster it places.4

Moving a slot​

After initialization, SCRIPT_MOVE_MONSTERS calls the same placement step but passes position as the direction argument, so it copies position into direction.

Copying runtime state​

Before loading another level, the level-change path copies the 900 bytes of the 30 slots, destinations included, into a level record written to LEVELS.TMP; a saved game holds one such record per level. When a level that has a record is loaded again, the slots are restored from it and the level's initial monsters are not placed again. Just before the copy, attack_frame of every slot is cleared. See level records and level changes.5

Sources​

Locations refer to the game executable reference; comparative sources are listed in the technical references.

Footnotes​

  1. The monster slot table and the monster type properties. Initialize a monster slot, reached from the INF monster-state reader; slot clearing by the fill helper; placement by place a monster on a block. Comparative source: REF-SCUMM-007, initMonster. ↩ ↩2

  2. Wander modes. ↩

  3. Monster update, turn a block's monsters, close-range action, pursuit decision, ranged action, move toward a destination and the monsters of a visible block, which also reads graphics_index and slot_mod_3 (see monster images and the colour variants); the position numbering is inferred from the positions within an occupied block. Comparative source: REF-SCUMM-020, EoBMonsterInPlay. ↩

  4. Initialize a monster slot, monster spawn and the INF monster-state reader. ↩

  5. Prepare the level-state buffer, restore it, with the far memory copy; read and write the LEVELS.TMP record; the write is made by the script command that loads a level; settle the level clears byte 0x0a; initial monster states are skipped for a level with a record. ↩