Script virtual machine
Scripts · The levels · INF format
In brief
Level scripts are a small bytecode language. Each level's INF file carries one buffer of script code; the map's blocks point into it. When an event starts a run, the interpreter reads one command byte, lets the command's handler consume its operands and act, then moves on to the next command, until the run stops. This reference describes the language precisely enough to decode, edit and interpret the scripts of the DOS release; each command has its own page with its operands and effects. What scripts do in the game is explained in the scripts chapter.
Where scripts live
- The script buffer. When a level is loaded, the INF field
script_datais copied to the runtime script buffer. Every script offset — in the cell bindings, in jumps and in calls — counts from the start of that buffer, not from the INF file or the current instruction. The buffer is set aside once, when the game starts, with room for 7,508 bytes, and every level reuses it.1 - The message table. The buffer's first little-endian word is the offset of
the level's message strings, a sequence of zero-terminated strings used by
SCRIPT_PRINT_MESSAGEand by dialogue answers. Script code fills the bytes between that first word and the strings, so the first possible script offset is 2. - Entry points. The cell bindings of the INF file give each scripted block a script offset and the events it accepts. Several blocks can share a script, and scripts can call shared subroutines.
How a run starts
Every run starts in one place, the block-event dispatcher, and always at the script offset of the block that receives the event: a timed event, the step count, a level change and the end of a flight all send a block event like the player's actions do. A block with offset 0 has no script. The block offsets come from the cell bindings when the level loads, and nothing else in the examined code writes them, with one exception: the side forms of Set walls and Toggle walls write a byte at any distance from a block's walls and could reach an offset (see unexpected values). The reference scripts only write the four walls. Restoring a level from its record writes the walls and the occupancy counts of its blocks, not these offsets.
Within a run, execution reaches other code only through jumps, conditions, calls and returns. A return always goes to the byte after a call. As a result, two routines are never executed: the nine bytes at offset 2 of levels 12 and 14 (see decoding status). Each is a Damage characters on the whole party (2d6, saving throw allowed) followed by Return from a call. The pit scripts of the two levels contain the same damage instruction, byte for byte, written inline (11 times in level 12, 90 in level 14), so the routines look like an unused shared subroutine (inferred).2
The pages of this reference
| Page | What it covers |
|---|---|
| Instruction encoding | Command bytes, operands, selectors, the dispatcher; the 30 commands. |
| Control flow | Jumps, conditions, calls and returns; the return stack across runs. |
| Decoding status and unexpected values | What a decoder reaches in the original levels; values out of range. |
| Values passed on to other routines | What each routine reached from a script does with an out-of-range value. |
| Script instructions | One page per command, with its encoding and behavior. |
Sources
Locations refer to the game executable reference; comparative sources are listed in the technical references.