Skip to main content

Script instructions

ScriptOpcode · signed byte, first byte of every script instruction

Virtual machine · Scripts · The engine in ten minutes

Level scripts are sequences of instructions. The first byte of each instruction selects what it does: change walls, open a door, place a monster, move the party, show a dialogue, test a condition, jump. The interpreter accepts 30 such bytes, from 0xe2 to 0xff; it reads the byte as a signed value to select the handler, and does not dispatch other bytes as instructions.1

Some operands and effects remain unknown; each instruction page says which.

typedef enum ScriptOpcode {
SCRIPT_SET = 0xff, /* Assign walls or a direction-related value. */
SCRIPT_TOGGLE = 0xfe, /* Switch walls or a door between two states. */
SCRIPT_OPEN_DOOR = 0xfd, /* Request opening the door of a block. */
SCRIPT_CLOSE_DOOR = 0xfc, /* Request closing the door of a block if it is unoccupied. */
SCRIPT_SPAWN_MONSTER = 0xfb, /* Place a monster in a free or evicted slot, or clear a slot. */
SCRIPT_MOVE_PARTY_OR_OBJECT = 0xfa, /* Move the party, monsters or items. */
SCRIPT_MOVE_INVENTORY_ITEM_TO_BLOCK = 0xf9, /* Move an item from a character's inventory to a block. */
SCRIPT_PRINT_MESSAGE = 0xf8, /* Display an indexed script message. */
SCRIPT_SET_FLAGS = 0xf7, /* Set a flag selected by a ScriptFlagTarget. */
SCRIPT_PLAY_SOUND = 0xf6, /* Play a sound, optionally associated with a block. */
SCRIPT_CLEAR_FLAGS = 0xf5, /* Clear a flag selected by a ScriptFlagTarget. */
SCRIPT_MODIFY_CHARACTER_HP = 0xf4, /* Change the hit points of selected characters. */
SCRIPT_DAMAGE_CHARACTERS = 0xf3, /* Compute and apply damage to characters. */
SCRIPT_JUMP = 0xf2, /* Continue at a script-buffer offset. */
SCRIPT_END = 0xf1, /* Stop this run and empty the shared return stack. */
SCRIPT_RETURN = 0xf0, /* Resume at the saved return position, or stop if there is none. */
SCRIPT_CALL = 0xef, /* Call a script-buffer offset if the return stack has room. */
SCRIPT_JUMP_UNLESS = 0xee, /* Evaluate a condition; jump to a buffer offset if it is zero. */
SCRIPT_DELETE_ITEM = 0xed, /* Remove the held item or items of a block. */
SCRIPT_LOAD_LEVEL_OR_MONSTERS = 0xec, /* Load a level, or monster resources. */
SCRIPT_AWARD_EXPERIENCE = 0xeb, /* Share an experience amount. */
SCRIPT_CREATE_ITEM = 0xea, /* Copy an item and place it, with optional overrides. */
SCRIPT_LAUNCH_OBJECT = 0xe9, /* Launch a magic flying object or a copy of an item. */
SCRIPT_CHANGE_DIRECTION = 0xe8, /* Turn the party or moving objects. */
SCRIPT_IDENTIFY_ITEMS = 0xe7, /* Identify the items of a block. */
SCRIPT_RUN_SEQUENCE = 0xe6, /* Run a scripted sequence. */
SCRIPT_DELAY = 0xe5, /* Wait until a shared counter reaches a target, then continue. */
SCRIPT_DRAW_SCENE = 0xe4, /* Request a scene redraw. */
SCRIPT_DIALOGUE = 0xe3, /* Run a dialogue-screen command. */
SCRIPT_SPECIAL_EVENT = 0xe2 /* Run one of the fixed special events. */
} ScriptOpcode;

Instructions​

Each page gives the operands in order, the instruction length and the effects. Operands that select a branch have their own enumeration.

ByteInstructionIdentifierSelectors and flags
0xffSet walls or a directionSCRIPT_SETAssignment targets
0xfeToggle walls or a doorSCRIPT_TOGGLEToggle targets
0xfdOpen a doorSCRIPT_OPEN_DOOR—
0xfcClose a doorSCRIPT_CLOSE_DOOR—
0xfbPlace a monsterSCRIPT_SPAWN_MONSTERMonster behavior modes
0xfaMove the party, monsters or itemsSCRIPT_MOVE_PARTY_OR_OBJECTMove instruction targets, level markers
0xf9Move an inventory item to a blockSCRIPT_MOVE_INVENTORY_ITEM_TO_BLOCKCharacter selector
0xf8Print a messageSCRIPT_PRINT_MESSAGE—
0xf7Set a flagSCRIPT_SET_FLAGSFlag operation targets
0xf6Play a soundSCRIPT_PLAY_SOUND—
0xf5Clear a flagSCRIPT_CLEAR_FLAGSFlag operation targets
0xf4Change hit pointsSCRIPT_MODIFY_CHARACTER_HPCharacter target
0xf3Damage charactersSCRIPT_DAMAGE_CHARACTERSCharacter target, damage flags, saving throws and their effects
0xf2JumpSCRIPT_JUMP—
0xf1End the scriptSCRIPT_END—
0xf0Return from a callSCRIPT_RETURN—
0xefCall a subroutineSCRIPT_CALL—
0xeeTest a conditionSCRIPT_JUMP_UNLESSCondition expression tokens, item properties
0xedDelete itemsSCRIPT_DELETE_ITEMItem removal targets
0xecChange level or load monster resourcesSCRIPT_LOAD_LEVEL_OR_MONSTERSLoad instruction targets, facing after a level load
0xebAward experienceSCRIPT_AWARD_EXPERIENCEExperience award target
0xeaCreate an itemSCRIPT_CREATE_ITEMItem creation overrides, destinations
0xe9Launch an objectSCRIPT_LAUNCH_OBJECTLaunched object kinds
0xe8Turn the party or flying objectsSCRIPT_CHANGE_DIRECTIONDirection change targets
0xe7Identify itemsSCRIPT_IDENTIFY_ITEMS—
0xe6Run a sequenceSCRIPT_RUN_SEQUENCEScripted sequences
0xe5WaitSCRIPT_DELAY—
0xe4Redraw the sceneSCRIPT_DRAW_SCENE—
0xe3Dialogue screenSCRIPT_DIALOGUEDialogue commands
0xe2Run a special eventSCRIPT_SPECIAL_EVENTSpecial events

The flag instructions read their own selector byte after the instruction byte; the instruction byte and the selector are different bytes with different roles. The four control-flow instructions are explained in script control flow.

Sources​

Locations refer to the game executable reference.

Footnotes​

  1. The block event dispatcher runs the command loop through the handler table, whose index is −(byte + 1). Each handler is listed in the reference under its instruction. ↩