Events that start scripts
Scripts · How a script runs · BlockEventMask
In brief
Every script run starts from an event sent to a block: the party enters or leaves it, an item is put down, taken or used, a flying object arrives, a wall is clicked, a timer expires, the step count is reached, a monster comes or goes, a spell is cast. The block runs its script only if it has one and accepts that event; walls with a special type reach scripts through a gate of their own.
The events
Every script run starts from a single event, a
BlockEventMask value that the script can read
back with SCRIPT_EXPR_EVENT_MASK. The block-event dispatcher is the only place
where a run starts that a byte scan of the whole executable finds (a pointer
computed at run time cannot be excluded), and it is called for the following
events:12
| Event | When it is dispatched | Block |
|---|---|---|
BLOCK_EVENT_PARTY_ENTER | The party has moved into a new block. | New party block |
BLOCK_EVENT_PARTY_LEAVE | The same move, just before the entry event. | Previous party block |
BLOCK_EVENT_ITEM_PLACED | An item has been put into a block: dropped by the player, put in a wall niche, placed by a keyboard action, or at the end of a flight. | Receiving block |
BLOCK_EVENT_ITEM_REMOVED | The player has taken an item out of a block. | Block that held it |
BLOCK_EVENT_FLYING_OBJECT | A flying object has moved into a new block, or has struck a lever or script wall. | Block entered or block of that wall |
BLOCK_EVENT_STEP_COUNT | The party has taken more steps than the level's threshold. | [00:00] |
BLOCK_EVENT_WALL_DECORATION | The player has activated a wall decoration, lever or script wall. | Block in front of the party |
BLOCK_EVENT_TIMER | A level script timer has expired; see timed block scripts. | Timer target |
BLOCK_EVENT_ITEM_USED | A character has used an inventory item. | Block in front of the party |
BLOCK_EVENT_MONSTER_ENTER | A monster has been placed in a block; see monster movement events. | Monster's new block |
BLOCK_EVENT_MONSTER_EXIT | A monster is leaving a block. | Monster's old block |
BLOCK_EVENT_SPELL_CAST | A spell is being started. | Party block |
Some events give the script extra context:
- Picking up and dropping items. When the player picks an item up from the floor, it is already in hand when the script runs; for a wall niche, the script runs first.
- Using an item. The used item is available to the script through
SCRIPT_EXPR_LAST_USED_ITEMfor the duration of the run. No event is dispatched when a character in the back rows uses a melee weapon (using items). - Casting a spell. The spell's definition number is recorded and scripts
read it through
SCRIPT_EXPR_LAST_CAST_SPELL(expression tokens); the original levels compare it with the two Dispel Magic definitions. - Counting steps. Every party move increments a step counter. When the
level's
steps_until_scriptvalue in the INF file is nonzero and the counter exceeds it, the periodic update runs the script of [00:00] and resets the counter; loading the level also resets it. In the reference files, the 11 levels with a threshold (20 to 100 steps) are exactly the 11 that bind a script to [00:00].
Activating walls
Walls with an InfWallSpecialType — levers,
niches, decorations and script walls — are not bound to events themselves. When
a wall is reached, a wall runner looks at the face of the block turned towards
the party or the object, together with a gate value that says how the wall
was reached. It skips the wall unless the block
accepts the gate, then calls the handler of the wall's
special type:3
| How the wall is reached | Gate | Block examined |
|---|---|---|
| The player clicks the wall in front of the party | BLOCK_EVENT_WALL_DECORATION | The block in front of the party |
| A flying object is stopped by the wall | BLOCK_EVENT_FLYING_OBJECT | The block it tried to enter |
| The keyboard command that activates the wall ahead | BLOCK_EVENT_KEYBOARD_ACTIVATION | The block in front of the party |
The gate changes what the handlers see:
- Only a click checks that the mouse is on the decoration; the other two ways always hit it.
INF_WALL_SPECIAL_DECORATION_SCRIPTwalls always dispatchBLOCK_EVENT_WALL_DECORATION, so their scripts see the same event for a click, a keyboard activation or a flying object.- Lever and script-only walls (
INF_WALL_SPECIAL_LEVER_INCREMENT,INF_WALL_SPECIAL_LEVER_DECREMENT,INF_WALL_SPECIAL_SCRIPTandINF_WALL_SPECIAL_SCRIPT_ALT) pass the gate itself as the event. Their scripts therefore seeBLOCK_EVENT_WALL_DECORATION,BLOCK_EVENT_FLYING_OBJECTorBLOCK_EVENT_KEYBOARD_ACTIVATION, which matches no single event: a test such asSCRIPT_EXPR_EVENT_MASK == BLOCK_EVENT_WALL_DECORATIONfails when the wall is activated from the keyboard.
Events from the keyboard
The keyboard commands act only when the game is
played without the mouse: when no mouse was detected at start-up, or
when the setup program was told not to use one (a
control byte records it). In that mode the
held item — used by these commands and by SCRIPT_EXPR_HELD_ITEM — is
the item in the selected hand slot of the selected character. W, S, Z and A send
no event; the others send these:4
| Key | Script event |
|---|---|
| U | BLOCK_EVENT_ITEM_USED, block in front |
| D | BLOCK_EVENT_ITEM_PLACED |
| T | BLOCK_EVENT_FLYING_OBJECT in each block entered, BLOCK_EVENT_ITEM_PLACED where it lands |
| G | BLOCK_EVENT_ITEM_REMOVED, and BLOCK_EVENT_ITEM_PLACED when a held item is put down in exchange |
| M | Through the wall runner with gate BLOCK_EVENT_KEYBOARD_ACTIVATION |
Monsters entering and leaving blocks
When a monster is placed in a new block, the game:5
- dispatches
BLOCK_EVENT_MONSTER_EXITto the old block, if that block held monsters, after decrementing its occupancy; - stores the new block in the monster's
MonsterRuntime, increments the new block's occupancy and dispatchesBLOCK_EVENT_MONSTER_ENTERto it; - updates the monster's direction, if a new one was given.
Each dispatch runs its script immediately, so an exit script runs before the
monster is in its new block, and an entry script before its direction changes.
Turning on the spot dispatches nothing. A monster that
dies is placed on [00:00] in the same
way.
Which events a block accepts
A block reacts only if it has a script and accepts the event. Each
LevelBlockRuntime stores a script
offset and a set of enabling bits (LevelBlockStateFlags),
loaded from the
INF cell bindings. The dispatcher shifts those bits into line with the event
masks and always adds three events:6
accepted = ((state & ~LEVEL_BLOCK_OCCUPANCY_MASK) >> 3)
| BLOCK_EVENT_STEP_COUNT | BLOCK_EVENT_WALL_DECORATION | BLOCK_EVENT_TIMER
run the block's script if script_offset != 0 and (event & accepted) != 0
So LEVEL_BLOCK_PARTY_ENTER_EVENT enables BLOCK_EVENT_PARTY_ENTER,
LEVEL_BLOCK_MONSTER_ENTER_EVENT enables BLOCK_EVENT_MONSTER_ENTER, and so
on, while the step, wall-decoration and timer events need no enabling bit.
A run executes to completion before the dispatcher returns. Runs can nest: when
a script moves the party, the move dispatches
BLOCK_EVENT_PARTY_LEAVE to the old block and BLOCK_EVENT_PARTY_ENTER to the
new one, so those scripts run in the middle of the moving instruction, which
saves and restores the state of its own run around them.
Sources
Locations refer to the game executable reference; comparative sources are listed in the technical references. Statements about the original levels come from decoding the scripts of the 16 reference INF files listed in the file inventory.