Skip to main content

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.

The script buffer of a level, 7,508 bytes reused by every level. Its first word is the offset of the message table. Script code starts at offset 2 and fills the bytes up to the messages, a sequence of zero-terminated strings; the rest of the buffer is unused. The cell bindings give each scripted block an offset into the code, and jumps and calls also count from the start of the buffer.
Code and messages share one buffer; every offset counts from its start.

Where scripts live​

  • The script buffer. When a level is loaded, the INF field script_data is 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_MESSAGE and 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​

PageWhat it covers
Instruction encodingCommand bytes, operands, selectors, the dispatcher; the 30 commands.
Control flowJumps, conditions, calls and returns; the return stack across runs.
Decoding status and unexpected valuesWhat a decoder reaches in the original levels; values out of range.
Values passed on to other routinesWhat each routine reached from a script does with an out-of-range value.
Script instructionsOne page per command, with its encoding and behavior.

Sources​

Locations refer to the game executable reference; comparative sources are listed in the technical references.

Footnotes​

  1. The cell-binding loader copies script_data into the script buffer; the message handler reads the buffer's first word to find the strings. See the INF schema. ↩

  2. The block event dispatcher, reached from the party move, the script timers, the step trigger, the level change and the level settle, among others; the cell-binding loader writes the block offsets; the side forms of the set and toggle handlers; the level record restore. The routines at offset 2 were found with the decoder's --gaps listing. ↩