Skip to main content

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.

An example instruction, the bytes fd 6d 01. The command byte fd is SCRIPT_OPEN_DOOR. The two bytes 6d 01 are a little-endian word, 0x016d = 365, the block [13:11]. Command bytes range from e2 to ff, thirty commands; a byte below e2 where a command is expected is skipped.
The block of this instruction is read low byte first.

Commands and operands​

  • An instruction is a command byte followed by its operands. The 30 commands use the bytes 0xe2 to 0xff (ScriptOpcode).
  • Operands are bytes and 16-bit words. Words are little-endian. Block indexes are words: block YY × 32 + XX is the position [XX:YY] (column XX, row YY). 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​

ByteCommandIdentifierWhat it does
0xffSet walls or a directionSCRIPT_SETAssigns wall references, or a direction-related value.
0xfeToggle walls or a doorSCRIPT_TOGGLESwitches wall references between two values, or toggles a door.
0xfdOpen a doorSCRIPT_OPEN_DOORRequests opening the door of a block.
0xfcClose a doorSCRIPT_CLOSE_DOORRequests closing the door of an empty block.
0xfbPlace a monsterSCRIPT_SPAWN_MONSTERPlaces a monster in a free or reused slot.
0xfaMove the party, monsters or itemsSCRIPT_MOVE_PARTY_OR_OBJECTMoves the party, the monsters of a block or its items.
0xf9Move an inventory item to a blockSCRIPT_MOVE_INVENTORY_ITEM_TO_BLOCKPuts an eligible item from a character's inventory into a block.
0xf8Print a messageSCRIPT_PRINT_MESSAGEDisplays one of the level's strings.
0xf7Set a flagSCRIPT_SET_FLAGSSets a level, global or monster flag, or a control value.
0xf6Play a soundSCRIPT_PLAY_SOUNDPlays a sound, optionally associated with a block.
0xf5Clear a flagSCRIPT_CLEAR_FLAGSClears a level or global flag, or a control value.
0xf4Change hit pointsSCRIPT_MODIFY_CHARACTER_HPAdds to the hit points of one or all characters.
0xf3Damage charactersSCRIPT_DAMAGE_CHARACTERSRolls damage, with an optional saving throw, for one or all characters.
0xf2JumpSCRIPT_JUMPContinues at another offset.
0xf1End the scriptSCRIPT_ENDStops the run.
0xf0Return from a callSCRIPT_RETURNReturns from a call, or stops the run.
0xefCall a subroutineSCRIPT_CALLCalls a subroutine.
0xeeTest a conditionSCRIPT_JUMP_UNLESSBranches on a condition.
0xedDelete itemsSCRIPT_DELETE_ITEMRemoves the held item or items of a block.
0xecChange level or load monster resourcesSCRIPT_LOAD_LEVEL_OR_MONSTERSChanges level, or loads monster resources.
0xebAward experienceSCRIPT_AWARD_EXPERIENCEShares experience between eligible characters.
0xeaCreate an itemSCRIPT_CREATE_ITEMCopies a template item and places it.
0xe9Launch an objectSCRIPT_LAUNCH_OBJECTSends an item or a magic effect flying.
0xe8Turn the party or flying objectsSCRIPT_CHANGE_DIRECTIONTurns the party or moving objects.
0xe7Identify itemsSCRIPT_IDENTIFY_ITEMSMarks the items of a block as identified.
0xe6Run a sequenceSCRIPT_RUN_SEQUENCEStarts a conversation, animation or protection sequence.
0xe5WaitSCRIPT_DELAYWaits before continuing.
0xe4Redraw the sceneSCRIPT_DRAW_SCENERedraws the view.
0xe3Dialogue screenSCRIPT_DIALOGUEDrives the illustrated dialogue screen.
0xe2Run a special eventSCRIPT_SPECIAL_EVENTRuns one of seven fixed game routines.

Sources​

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

Footnotes​

  1. The block event dispatcher and its command table, indexed by −(command + 1). ↩