Skip to main content

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

EventWhen it is dispatchedBlock
BLOCK_EVENT_PARTY_ENTERThe party has moved into a new block.New party block
BLOCK_EVENT_PARTY_LEAVEThe same move, just before the entry event.Previous party block
BLOCK_EVENT_ITEM_PLACEDAn 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_REMOVEDThe player has taken an item out of a block.Block that held it
BLOCK_EVENT_FLYING_OBJECTA 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_COUNTThe party has taken more steps than the level's threshold.[00:00]
BLOCK_EVENT_WALL_DECORATIONThe player has activated a wall decoration, lever or script wall.Block in front of the party
BLOCK_EVENT_TIMERA level script timer has expired; see timed block scripts.Timer target
BLOCK_EVENT_ITEM_USEDA character has used an inventory item.Block in front of the party
BLOCK_EVENT_MONSTER_ENTERA monster has been placed in a block; see monster movement events.Monster's new block
BLOCK_EVENT_MONSTER_EXITA monster is leaving a block.Monster's old block
BLOCK_EVENT_SPELL_CASTA 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_ITEM for 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_script value 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 reachedGateBlock examined
The player clicks the wall in front of the partyBLOCK_EVENT_WALL_DECORATIONThe block in front of the party
A flying object is stopped by the wallBLOCK_EVENT_FLYING_OBJECTThe block it tried to enter
The keyboard command that activates the wall aheadBLOCK_EVENT_KEYBOARD_ACTIVATIONThe 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_SCRIPT walls always dispatch BLOCK_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_SCRIPT and INF_WALL_SPECIAL_SCRIPT_ALT) pass the gate itself as the event. Their scripts therefore see BLOCK_EVENT_WALL_DECORATION, BLOCK_EVENT_FLYING_OBJECT or BLOCK_EVENT_KEYBOARD_ACTIVATION, which matches no single event: a test such as SCRIPT_EXPR_EVENT_MASK == BLOCK_EVENT_WALL_DECORATION fails 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

KeyScript event
UBLOCK_EVENT_ITEM_USED, block in front
DBLOCK_EVENT_ITEM_PLACED
TBLOCK_EVENT_FLYING_OBJECT in each block entered, BLOCK_EVENT_ITEM_PLACED where it lands
GBLOCK_EVENT_ITEM_REMOVED, and BLOCK_EVENT_ITEM_PLACED when a held item is put down in exchange
MThrough 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

  1. dispatches BLOCK_EVENT_MONSTER_EXIT to the old block, if that block held monsters, after decrementing its occupancy;
  2. stores the new block in the monster's MonsterRuntime, increments the new block's occupancy and dispatches BLOCK_EVENT_MONSTER_ENTER to it;
  3. 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.

What happens when an event reaches a block. If the block has no script, nothing runs. If it has one but does not accept the event, nothing runs either. Otherwise a run starts at the block's script and executes until it stops, and control returns to the caller.
A script runs only when the block has one and accepts the event.

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.

Footnotes​

  1. Dispatching sites: floor drop and pick-up, wall niche, item use with the item in use, spell start with the spell being cast, step trigger with the step counter, incremented by moving the party, and the step threshold set by level loading. The full list of call sites is on the BlockEventMask page. Comparative source: runLevelScript, updateScriptTimers, clickedSceneDropPickupItem, clickedNiche, gui_processWeaponSlotClickRight, startSpell, endObjectFlight and timerProcessFlyingObjects (REF-SCUMM-012). ↩

  2. Block event dispatcher and command table. Byte scans of the whole executable find the table referenced only inside the dispatcher, and the 23 far calls listed on the BlockEventMask page as the only encodings of the dispatcher's address. ↩

  3. Wall runner, reached from a click, a flying object or the keyboard, with the gate, the decoration hit test and the wall handlers. ↩

  4. Keyboard commands, called by the key loop with the code returned by the key reader; selection movement; the control byte, also written by the mouse initialization. The key numbers are 0x12, 0x20, 0x2e, 0x1f (W, S, Z, A), 0x17 (U), 0x21 (D), 0x15 (T), 0x23 (G) and 0x34 (M), the numbers the keyboard handler gives those keys. ↩

  5. Place a monster on a block and the block event dispatcher; the enabling bits come from the cell bindings. Comparative sources: EoBInfProcessor::run (REF-SCUMM-003) and placeMonster (REF-SCUMM-008). ↩

  6. Block event dispatcher and moving the party. The binding statistics on the LevelBlockStateFlags page come from the reference INF files. ↩