Instruction encoding
Script virtual machine · Control flow · ScriptOpcode
In brief
An instruction is a command byte, from 0xe2 to 0xff, followed by its
operands: bytes and little-endian words. Many commands start with a selector
byte that picks a variant and its operands, so lengths vary; a decoder must
follow them from a known entry point. The table lists the 30 commands with a
link to the page of each.
Commands and operands
- An instruction is a command byte followed by its operands. The 30
commands use the bytes
0xe2to0xff(ScriptOpcode). - Operands are bytes and 16-bit words. Words are little-endian. Block
indexes are words: block
YY × 32 + XXis the position[XX:YY](columnXX, rowYY). Script offsets are words counted from the start of the script buffer. - Many commands take a selector byte first, which picks a variant and determines the operands that follow. The handlers generally read selectors as signed bytes; this documentation writes them in hexadecimal and names them on enumeration pages.
- Lengths vary with the command and its selector; each command page gives the length of every variant. A decoder must follow these lengths from a known entry point: an operand can contain a byte that looks like a command.
The dispatcher reads the command byte as a signed value and selects one of 30
handlers in a table. Each handler reads its operands and returns how far to
advance; control-flow commands set the next position instead. A byte below
0xe2 calls no handler: the run skips it and goes on with the next byte. No
reachable script of the reference files contains one.1
Commands
| Byte | Command | Identifier | What it does |
|---|---|---|---|
0xff | Set walls or a direction | SCRIPT_SET | Assigns wall references, or a direction-related value. |
0xfe | Toggle walls or a door | SCRIPT_TOGGLE | Switches wall references between two values, or toggles a door. |
0xfd | Open a door | SCRIPT_OPEN_DOOR | Requests opening the door of a block. |
0xfc | Close a door | SCRIPT_CLOSE_DOOR | Requests closing the door of an empty block. |
0xfb | Place a monster | SCRIPT_SPAWN_MONSTER | Places a monster in a free or reused slot. |
0xfa | Move the party, monsters or items | SCRIPT_MOVE_PARTY_OR_OBJECT | Moves the party, the monsters of a block or its items. |
0xf9 | Move an inventory item to a block | SCRIPT_MOVE_INVENTORY_ITEM_TO_BLOCK | Puts an eligible item from a character's inventory into a block. |
0xf8 | Print a message | SCRIPT_PRINT_MESSAGE | Displays one of the level's strings. |
0xf7 | Set a flag | SCRIPT_SET_FLAGS | Sets a level, global or monster flag, or a control value. |
0xf6 | Play a sound | SCRIPT_PLAY_SOUND | Plays a sound, optionally associated with a block. |
0xf5 | Clear a flag | SCRIPT_CLEAR_FLAGS | Clears a level or global flag, or a control value. |
0xf4 | Change hit points | SCRIPT_MODIFY_CHARACTER_HP | Adds to the hit points of one or all characters. |
0xf3 | Damage characters | SCRIPT_DAMAGE_CHARACTERS | Rolls damage, with an optional saving throw, for one or all characters. |
0xf2 | Jump | SCRIPT_JUMP | Continues at another offset. |
0xf1 | End the script | SCRIPT_END | Stops the run. |
0xf0 | Return from a call | SCRIPT_RETURN | Returns from a call, or stops the run. |
0xef | Call a subroutine | SCRIPT_CALL | Calls a subroutine. |
0xee | Test a condition | SCRIPT_JUMP_UNLESS | Branches on a condition. |
0xed | Delete items | SCRIPT_DELETE_ITEM | Removes the held item or items of a block. |
0xec | Change level or load monster resources | SCRIPT_LOAD_LEVEL_OR_MONSTERS | Changes level, or loads monster resources. |
0xeb | Award experience | SCRIPT_AWARD_EXPERIENCE | Shares experience between eligible characters. |
0xea | Create an item | SCRIPT_CREATE_ITEM | Copies a template item and places it. |
0xe9 | Launch an object | SCRIPT_LAUNCH_OBJECT | Sends an item or a magic effect flying. |
0xe8 | Turn the party or flying objects | SCRIPT_CHANGE_DIRECTION | Turns the party or moving objects. |
0xe7 | Identify items | SCRIPT_IDENTIFY_ITEMS | Marks the items of a block as identified. |
0xe6 | Run a sequence | SCRIPT_RUN_SEQUENCE | Starts a conversation, animation or protection sequence. |
0xe5 | Wait | SCRIPT_DELAY | Waits before continuing. |
0xe4 | Redraw the scene | SCRIPT_DRAW_SCENE | Redraws the view. |
0xe3 | Dialogue screen | SCRIPT_DIALOGUE | Drives the illustrated dialogue screen. |
0xe2 | Run a special event | SCRIPT_SPECIAL_EVENT | Runs one of seven fixed game routines. |
Sources
Locations refer to the game executable reference; comparative sources are listed in the technical references.