Skip to main content

Place a monster

SCRIPT_SPAWN_MONSTER · opcode 0xfb · 15 bytes

Script virtual machine · Opcode list · Scripted monster placement

Puts a new monster in a block. The game uses a free monster slot or, when all 30 slots are alive, takes the slot of the monster farthest from the party.

Encoding​

Offsets count from the opcode byte. Words are little-endian. The operands are the fields of an INF monster record in a different order, without the slot index.

OffsetTypeFieldMeaning
0u8opcode0xfb
1u8skippedNot read; no effect. It is 0xf3 in all 283 reference instructions; preserve it.
2s8unitUpdate group: stored as unit * 2 + (slot index & 1) in update_group, which sets how often the monster acts (update cadence).
3u16leblockDestination block. A zero block clears the chosen slot and places nothing.
5s8position0–3 a quarter of the block, 4 its centre (sharing a block); 4 is also special in the checks below.
6s8directionFacing, a Direction.
7s8type_indexMonster type: an entry of the runtime monster-type area (46 bytes each).
8s8graphics_indexGraphics bank of the level: 0 the first, any other value the second (monster images).
9s8modeInitial MonsterMode.
10s8parameterStored as state_counter: the updates left in MONSTER_MODE_DELAYED or MONSTER_MODE_PARALYZED, the moves left while fleeing.
11u16lerandom_itemWhen nonzero, item of which the monster leaves a copy when it dies, one time in ten (when a monster dies).
13u16lefixed_itemWhen nonzero, item of which the monster always leaves a copy when it dies.

The destination fields belong to MonsterRuntime.

Behavior​

When nothing happens​

The instruction does nothing, and the script continues, when any of these checks fails. They run in this order:1

  1. The block's occupancy (low three bits of its state word, see LevelBlockStateFlags) equals 7.
  2. The block is the party's current block.
  3. The occupancy is nonzero and either the new monster type's occupancy class (occupancy_class in INF) is nonzero or position is 4.
  4. A monster slot already in the block has position 4 or a type with a nonzero occupancy class. Hit points are not looked at.

Choosing the slot​

The game takes the first of the 30 slots whose hit points are zero or negative. If every slot is alive, it takes, among the slots without MONSTER_RUNTIME_KEEP_SLOT, the one whose block is farthest from the party (at a nonzero distance). If no slot qualifies, nothing happens. A living monster in the chosen slot is first removed from the level.1

Initializing the monster​

The slot is zero-filled and then, for a nonzero block, filled as follows:2

Slot fieldValue
type_index, position, direction, graphics_index, mode, state_counter, random_item_raw, fixed_item_rawThe operands above.
update_groupunit * 2 + (slot index & 1).
max_hp_raw, current_hp_rawOne roll of the type's hit-point dice (type bytes +3, +4, +5), in both.
ranged_attacks_leftThe type's number of ranged attacks.
ranged_weapon_index0.
slot_mod_3Slot index modulo 3.
ranged_waitA roll of 1d3, the ranged attack wait counter.
sublevelThe low byte of the current sublevel word.

Finally the monster is placed on its block, which raises the monster-enter event of that block; see monster movement events.

Sublevels and eviction​

When all slots are alive, the choice does not look at the monsters' sublevel: a monster of another sublevel is measured by its block number like the others, and a monster of the current sublevel can be evicted. Only the monster count of conditions reads it; see sublevels. Whether play can reach 30 living monsters is unknown.1

Observed use​

The 283 reachable instructions of the 16 reference INF files use unit 0 (159) or 1 (124); position 4 (163 times), otherwise 0–3; direction 0–3; type_index 0–5; graphics_index 0 (202) or 1 (81); mode 0 (186), 4 (50), 5 (33) or 6 (14); parameter always 0; an item word is nonzero in 29. Blocks range from 12 to 1003. No instruction uses a zero block or a negative operand.

Sources​

Footnotes​

  1. Handler for 0xfb and the monster spawn routine, which uses the distance routine. Comparative source: DarkMoonEngine::replaceMonster in REF-SCUMM-013. Statistics come from decoding the scripts reachable in the 16 reference INF files. ↩ ↩2 ↩3

  2. Monster slot initialization, the dice roll, the placement routine and the sublevel word. The item words are read by kill a monster, the graphics bank by the monster shapes. ↩