Skip to main content

Condition expression tokens

ScriptExpressionToken · signed byte, sign-extended; the tokens of the condition read by Test a condition

Script instructions · Scripts · Virtual machine

Script conditions are small postfix expressions: values are pushed on a stack and operators combine them, as in "the party stands on this block AND the lever is down". Each byte of the expression is a token. Operand tokens push a value read from the game, such as a flag, a block, an item or a character; operator tokens compare or combine the two values on top of the stack. The expression ends with a terminator token, which takes the result from the stack. One stack serves every condition of the game and is never reset; see the expression stack for what an unbalanced expression does.1

The evaluator treats bytes 0xce to 0xff (signed −50 to −1) as tokens and skips every other byte without effect.

typedef enum ScriptExpressionToken {
/* Terminator. */
SCRIPT_EXPR_END = 0xee, /* Ends the expression; see the instruction page. */

/* Operators: pop two words, push 0 or 1. "lower" is the value pushed first. */
SCRIPT_EXPR_EQUAL = 0xff, /* lower == top. */
SCRIPT_EXPR_NOT_EQUAL = 0xfe, /* lower != top. */
SCRIPT_EXPR_LESS = 0xfd, /* lower < top, signed 16-bit. */
SCRIPT_EXPR_LESS_OR_EQUAL = 0xfc, /* lower <= top, signed 16-bit. */
SCRIPT_EXPR_GREATER = 0xfb, /* lower > top, signed 16-bit. */
SCRIPT_EXPR_GREATER_OR_EQUAL = 0xfa, /* lower >= top, signed 16-bit. */
SCRIPT_EXPR_AND = 0xf9, /* Both words nonzero. */
SCRIPT_EXPR_OR = 0xf8, /* At least one word nonzero. */

/* Operands: push one word. Operand bytes follow the token. */
SCRIPT_EXPR_CONSTANT = 0xd2, /* u16le immediate. */
SCRIPT_EXPR_LEVEL_FLAG = 0xef, /* u8 bit index: test a flag of the current level. */
SCRIPT_EXPR_GLOBAL_FLAG = 0xf0, /* u8 bit index: test a global flag. */
SCRIPT_EXPR_PARTY_BLOCK_OR_ITEM_HOLDERS = 0xf1, /* Selector-dependent: party block, or holders of an item. */
SCRIPT_EXPR_BLOCK_FIRST_BYTE = 0xf7, /* u16le block: byte 0 of the block entry. */
SCRIPT_EXPR_BLOCK_BYTE = 0xe9, /* s8 offset, u16le block: a byte of the block entry. */
SCRIPT_EXPR_BLOCK_OCCUPANCY_OR_MONSTER_COUNT = 0xf3, /* Selector-dependent; see below. */
SCRIPT_EXPR_ITEM_IN_BLOCK = 0xf2, /* u16le item index, u16le block. */
SCRIPT_EXPR_BLOCK_ITEM_TYPE_COUNT = 0xf5, /* u8, s8, u16le block. */
SCRIPT_EXPR_HELD_ITEM = 0xe7, /* ScriptExpressionItemSelector, then its operands. */
SCRIPT_EXPR_LAST_USED_ITEM = 0xd7, /* ScriptExpressionItemSelector, then its operands. */
SCRIPT_EXPR_ROLL_DICE = 0xdb, /* s8 count, s8 sides, s8 bonus. */
SCRIPT_EXPR_ACTIVE_CHARACTER_COUNT = 0xe8, /* No operand. */
SCRIPT_EXPR_NO_ELIGIBLE_CHARACTER = 0xda, /* No operand. */
SCRIPT_EXPR_PARTY_DIRECTION = 0xed, /* No operand; the party's facing. */
SCRIPT_EXPR_FIND_CHARACTER_BY_PORTRAIT = 0xce, /* s8 value; selects a character. */
SCRIPT_EXPR_FIND_CHARACTER_BY_RACE = 0xdd, /* s8 CharacterRace; selects a random matching character. */
SCRIPT_EXPR_FIND_CHARACTER_BY_CLASS_MASK = 0xdc, /* s8 mask; selects a random matching character. */
SCRIPT_EXPR_EVENT_MASK = 0xe0, /* No operand; the BlockEventMask that started this run. */
SCRIPT_EXPR_RUN_VARIABLE = 0xe4, /* No operand; the run variable, e.g. the dialogue choice. */
SCRIPT_EXPR_LAST_CAST_SPELL = 0xdf /* No operand; the definition number of the last spell cast. */
} ScriptExpressionToken;

Every other value from 0xce to 0xff is a one-byte token with no operand and no effect on the stack: 0xcf, 0xd0, 0xd1, 0xd3–0xd6, 0xd8, 0xd9, 0xde, 0xe1–0xe3, 0xe5, 0xe6, 0xea–0xec, 0xf4 and 0xf6. They have no symbol because none of them does anything as a token. Some of these bytes are meaningful as item property selectors.

Operand layouts​

Multi-byte values are little-endian, operands follow the token in the order shown, and the stack holds 16-bit words. "Result" is the word pushed.

TokenOperands in orderResult
SCRIPT_EXPR_CONSTANTu16The operand.
SCRIPT_EXPR_LEVEL_FLAGu8 bit1 if the bit is set in the current level's 32-bit flag value, otherwise 0.
SCRIPT_EXPR_GLOBAL_FLAGu8 bit1 if the bit is set in the 32-bit global flag value, otherwise 0.
SCRIPT_EXPR_BLOCK_FIRST_BYTEu16 blockSigned byte at offset 0 of the LevelBlockRuntime entry.
SCRIPT_EXPR_BLOCK_BYTEs8 offset, u16 blockSigned byte at offset in that entry. The handler does not limit the offset.
SCRIPT_EXPR_BLOCK_OCCUPANCY_OR_MONSTER_COUNTu8 selector; selector 0xff: u16 block; any other selector: a list of u8 monster types ended by 0xffSelector 0xff: the block's state word masked with LEVEL_BLOCK_OCCUPANCY_MASK. Otherwise the number of monsters of the listed types; the selector byte is not used.
SCRIPT_EXPR_ITEM_IN_BLOCKu16 item, u16 blockThe item index if the item is in the block's item chain and not in flight, otherwise 0.
SCRIPT_EXPR_BLOCK_ITEM_TYPE_COUNTu8 flag, s8 type, u16 blockThe number of items of that type in the block's item chain, −1 counting every type. With flag 0, items in flight (position_raw 4 to 7) are left out; any other value counts them.
SCRIPT_EXPR_PARTY_BLOCK_OR_ITEM_HOLDERSs8 selector; selector 0xf5: u16 type, u16 value; other selectors: u16 blockSelector 0xf5: the number of characters with CHARACTER_STATUS_PRESENT who carry, in one of their 27 inventory slots, an item of that type and value_raw (−1 matching any); only the first item of a quiver is examined, and the item held with the mouse is not. Other selectors: 1 if the party stands on the block, otherwise 0.
SCRIPT_EXPR_HELD_ITEMs8 selector, then the selector's operandsSee item properties in conditions.
SCRIPT_EXPR_LAST_USED_ITEMs8 selectorSee item properties in conditions.
SCRIPT_EXPR_ROLL_DICEs8 count, s8 sides, s8 bonusA roll of count dice with sides sides, plus bonus.
SCRIPT_EXPR_ACTIVE_CHARACTER_COUNTnoneThe number of party slots that pass the eligibility test with PRESENT, NOT_DEAD, CONSCIOUS and NOT_PARALYZED_OR_PETRIFIED.
SCRIPT_EXPR_NO_ELIGIBLE_CHARACTERnone1 if no character with CHARACTER_STATUS_PRESENT is without SPELL_EFFECT_INVISIBILITY in its spell_effects, otherwise 0: every present character, dead ones included, is invisible, or no character is present. SPELL_EFFECT_IMPROVED_INVISIBILITY does not count.
SCRIPT_EXPR_FIND_CHARACTER_BY_PORTRAITs8 value1 if an eligible slot has portrait equal to value, and that slot becomes the selected character; otherwise 0.
SCRIPT_EXPR_FIND_CHARACTER_BY_RACEs8 valueStarting after a random slot, 1 if an eligible slot's race, race_and_sex shifted right once, equals value, and that slot becomes the selected character; otherwise 0.
SCRIPT_EXPR_FIND_CHARACTER_BY_CLASS_MASKs8 maskStarting after a random slot, 1 if an eligible slot's class mask shares a bit with mask, and that slot becomes the selected character; otherwise 0.
SCRIPT_EXPR_PARTY_DIRECTIONnoneThe party's facing.
SCRIPT_EXPR_EVENT_MASKnoneThe BlockEventMask word that started the current script run.
SCRIPT_EXPR_RUN_VARIABLEnoneThe run variable, sign-extended: zero when a run starts, then written by the dialogue choice and by the SCRIPT_FLAG_TARGET_RUN_VARIABLE flag target.
SCRIPT_EXPR_LAST_CAST_SPELLnoneThe definition number of the last spell cast, which the cast routine records; the reference scripts compare it with 12 and 44, the mage and cleric Dispel Magic.

Character fields are those of CharacterRuntime, status bits are CharacterStatusFlags and spell effects are SpellEffectFlags. "Eligible" means passing the eligibility test with CHARACTER_ELIGIBLE_PRESENT | CHARACTER_ELIGIBLE_CONSCIOUS. Other instructions use the selected character when their own character selector is −1; see, for example, character selector for item transfers.2

How the encodings were checked​

Every expression reachable from the cell bindings of the 16 reference INF files was decoded: 1958 expressions with 8762 tokens. Each ends with exactly one word on the stack, so none is unbalanced.

All tokens occur in the reference scripts except SCRIPT_EXPR_LESS_OR_EQUAL, SCRIPT_EXPR_GREATER_OR_EQUAL, SCRIPT_EXPR_ITEM_IN_BLOCK, SCRIPT_EXPR_NO_ELIGIBLE_CHARACTER and SCRIPT_EXPR_FIND_CHARACTER_BY_PORTRAIT. The monster-list form of SCRIPT_EXPR_BLOCK_OCCUPANCY_OR_MONSTER_COUNT and the identified-name selector of item properties in conditions do not occur either: their layouts are known from the code only.

  • Event mask. The script runner stores its event argument when a run starts. The reference scripts compare it only with documented BlockEventMask values (1, 2, 4, 8, 16, 64, 256, 512, 1024 and 2048), never with BLOCK_EVENT_STEP_COUNT or BLOCK_EVENT_TIMER. This holds across all scripts.3
  • Run variable. The runner clears it at the start of a run, and the dialogue choice stores the number of the chosen answer, starting at 1. The reference scripts compare it with 1, 2 and 3 right after a choice.3

Sources​

Locations refer to the game executable reference.

Footnotes​

  1. The expression evaluator of Test a condition, with the expression value stack. Comparative source: EoBInfProcessor::oeob_eval_v2 in REF-SCUMM-003. ↩

  2. The evaluator reads the party direction, the class mask table and the last spell cast, and records the selected character. It calls the dice roll, eligibility test, block item search, item-holder count and inventory search routines. ↩

  3. The block event dispatcher sets the event mask and clears the run variable when a run starts. ↩ ↩2