How a script runs
Scripts · Events that start scripts · Control flow
In brief
A run starts at the block's script with a clean state and executes its commands one after another until it stops; conditions test flags, blocks, the party, characters and items. A command that sends an event can start another block's script inside the running one. A script keeps nothing of its own: everything it changes is ordinary game state, kept with the level or saved with the game.
A run
A new run starts at the block's script offset with a clean state: the run variable is zero, no character is selected, and the block and the event are recorded. Commands then execute one after another until the script ends; jumps, calls and returns are described under control flow.1
A command that sends an event can start another block's script inside the running one. Only the party-move command protects the running script's state from that nested run; see scripts started by scripts. The return stack is not part of the clean state, and a nested run can return into the script that started it; see the return stack across runs.
Conditions
SCRIPT_JUMP_UNLESS is the
conditional branch of the script language. It evaluates an expression built from
ScriptExpressionToken tokens — flags,
block contents, party and character properties, the current item, dice rolls
and constants, combined with comparisons and logical operators. If the result
is nonzero the script continues; otherwise it jumps elsewhere. Many scripts of
the original levels start with such a test, and several are often chained.1
Two tokens read the state of the run itself, which is how a script knows why it was started and what the player answered:
SCRIPT_EXPR_EVENT_MASKgives the event that started the run.SCRIPT_EXPR_RUN_VARIABLEgives the run variable, set by dialogue choices, some special events and the flag commands.
Some tokens that search for a character also select it, so a condition can choose which character a later command affects or names in a message.
What the conditions test
The counts come from the 1958 conditions reachable in the 16 original levels.
- Walls. The most frequent test reads a block's
wall references:
SCRIPT_EXPR_BLOCK_BYTE(522 times) with offset 0, 1, 2 or 3, the face turned north, east, south or west, andSCRIPT_EXPR_BLOCK_FIRST_BYTE(74 times) for the north face. They tell which wall a face shows now, and so whether a lever, a button or a door has been changed by the wall commands. No other offset is used. - Characters. The race search (19 times)
looks for an elf 7 times and a dwarf 9 times, and once each for a human,
a half-elf and a halfling; it is usually combined with a level flag, so
that a character of that race remarks on something once. The
class search (97 times) looks for a
thief 67 times, a mage 14, a fighter 8, a cleric 7 and a paladin once.
Both consider only conscious characters and select the one found. The
portrait search and
SCRIPT_EXPR_NO_ELIGIBLE_CHARACTER, true when every character in the party is invisible, are not used. - Items carried. The item-holder form of
SCRIPT_EXPR_PARTY_BLOCK_OR_ITEM_HOLDERScounts the characters in the party, dead or not, who carry an item of a given type and value anywhere in their inventory. It is used 7 times: level 11 sends the party to one of two places depending on whether nobody carries anITEM_TYPE_SPELLBOOKor anITEM_TYPE_HOLY_SYMBOL; level 13 chooses its dialogue by whether as many characters carry anITEM_TYPE_MEDUSA_SHIELDas there are active characters, which a dead character's shield can satisfy in place of an active one's; levels 2 and 15 check that exactly one character carries a givenITEM_TYPE_QUEST_ITEM. - Items on the floor.
SCRIPT_EXPR_BLOCK_ITEM_TYPE_COUNT(124 times) mostly asks whether any item lies in a block (101 times, flag 0, so an item flying through does not count). The 23 tests with flag 1 also count flying items, for example threeITEM_TYPE_GEMin a block, or anITEM_TYPE_STICKY_PAPERthat stops the items a level 15 script moves along a row.SCRIPT_EXPR_ITEM_IN_BLOCKis not used.2
Scripts and the game's state
The table summarizes the commands of the walls and doors, movement, monsters and items and characters, flags and messages pages, and what persists.
A script keeps nothing of its own beyond the run: everything it changes is ordinary game state, kept or rebuilt like the rest. The table gives, for each kind of command, what it changes and what becomes of the change.3
| Commands | State they change | When the party leaves the level | In a saved game |
|---|---|---|---|
| Walls, doors | Wall references of the current level's blocks | Kept in the level's record | Saved |
| Doors | A door in motion | Finished at once | Finished before saving |
Moving and turning the party, SCRIPT_SET_DIRECTION | The party's block and facing | Set by the level change | Saved |
| Changing level | The current level and sublevel | — | Saved |
| Placing, moving or provoking monsters | The current level's monster slots | Kept in the level's record | Saved |
| Item commands, moving items, removing quest items | The shared item table, which holds the items of every level | Kept | Saved |
| Launching and turning flying objects | Objects in flight | Ended: thrown items fall, the rest vanish | Ended before saving |
| Character commands, special events and sequences | The characters: hit points, experience, levels, life, party membership | Kept | Saved |
| Flags and resting | Level flags, global flags, the rest refusal | Kept | Saved |
| Conditions, dialogue, flag commands | The run variable and the selected character | Reset at the start of every run | Not saved |
| Messages, dialogue, sound, waits and redraws | The screen and the sound only | — | — |
Some state is out of a script's reach:
- Bindings. Which blocks have a script, the script each one starts and the events it accepts are written only by the level loader, from the INF cell bindings, and read again at every load. No command changes them. The same holds for the level's timed block scripts and step-triggered script.
- Other levels. Only the current level is in play: the monsters, timers and blocks of other levels wait in their records and receive no event. A script reaches another level only by moving items there, which appear when that level is next loaded, and by changing level itself.
When scripts run. A script runs inside the step of the game loop that sent its event, and nothing else of the loop happens until it ends; see what the order means. During a wait the shared counter keeps running unless time is paused, so timed updates that come due run once, at the next timed-update step after the script. Wall changes in view mark the view, which the game redraws once in the redraw step of the same pass, unless the script redraws it first. A level change stops the run, and a command that sends an event can start another script inside the running one.
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.