Test a condition
SCRIPT_JUMP_UNLESS · opcode 0xee · variable length
Script virtual machine · Opcode list · Script conditions
Evaluates a condition written as a postfix expression. If the condition is true, execution continues with the next instruction; if it is false, it jumps to another position of the script buffer. This is how scripts express “if … then …”.
Encoding
Offsets count from the opcode byte. Words are little-endian.
| Offset | Type | Field | Meaning |
|---|---|---|---|
| 0 | u8 | opcode | 0xee |
| 1 | sequence of tokens | expression | Tokens and their operands, up to and including the token byte 0xee (SCRIPT_EXPR_END). See ScriptExpressionToken. |
| n | u16le | target | Offset from the start of the runtime script buffer, used when the result is zero. Always present. |
The instruction length is one byte for the opcode, the bytes of all tokens and
their operands, one byte for the terminator, and two bytes for target.
Example (constructed, not from a level), which tests whether bit 1 of the current level's flags is zero:
ee opcode SCRIPT_JUMP_UNLESS
ef 01 SCRIPT_EXPR_LEVEL_FLAG, bit 1
d2 00 00 SCRIPT_EXPR_CONSTANT 0
ff SCRIPT_EXPR_EQUAL
ee SCRIPT_EXPR_END
xx xx target (when the result is zero)
Behavior
The expression is evaluated on a stack of 16-bit words. Operand tokens read their operand bytes and push one word; operators pop two words and push 0 or
- At the terminator, one word is popped:1
| Final word | Continues at |
|---|---|
| nonzero | The byte after target. The target word is read but not used. |
| zero | The script buffer start plus target, the same base as Jump. |
if not expression:
goto script_buffer + target
Evaluation rules
- A token is read as a signed byte. Bytes
0xce–0xffselect an entry of a 50-entry table; all other bytes, and the entries documented as “no effect”, are skipped as one-byte tokens. Operand bytes are read only by their token, so a byte0xeeinside an operand does not end the expression. - Operators take the earlier-pushed value as the left operand and the top word
as the right operand. Comparisons are signed 16-bit;
ANDandORtreat any nonzero word as true. - The evaluator does not check for underflow or overflow, and does not check that exactly one word remains; see the expression stack. A skipped operand selector, such as an unsupported item selector, pushes nothing.
targetis not checked against the buffer size.
The expression stack
The stack is one array of 16-bit words with one index, shared by every condition of every script. The index is zero when the program starts. Nothing resets it: not the start of an instruction, of a script run or of a level, and not loading a saved game, which does not store it. Only restarting the game sets it back to zero.
push(v): stack[index] = v; index = index + 1
operator: index = index - 1; right = stack[index]
index = index - 1; left = stack[index]
stack[index] = left OP right; index = index + 1
end: index = index - 1; result = stack[index]
A well-formed expression leaves exactly one word, so the index is back where it started after the terminator. Every reachable expression of the reference levels is well-formed and needs at most four words, so in play the index stays at zero. An expression that is not well-formed has a lasting effect:1
- Words left over. If k words remain at the terminator, the top one is the result and the index stays k − 1 higher. Every later condition, in any script and any level, works that much higher in the array.
- Too few words. An operator or the terminator that finds no word of its own reads the word below: whatever an earlier expression left there, or, below the start of the array, the bytes that precede it. The index then stays lower, and an operator writes its result below the array. The word just below the array is the fill colour of the box that shows a hand's attack result: a terminator that finds the stack empty takes that colour word, which is not zero, as a true result, and the next value pushed replaces the colour until the game restarts (inferred). The reference scripts never do this.
- Overflow. The index has no limit. The 30 words after the start of the array are used by nothing else that was found; the 31st overlaps the level start selector and the selected character, the 34th the character selected by the script, and further words reach the run's stop flag and the index itself. The declared size of the array is not known.
An interpreter that reproduces the original keeps one stack and one index for the whole program and applies these rules; one that rejects unbalanced expressions departs from it only for scripts that the reference levels do not contain.
Monster counts
The monster-list form of SCRIPT_EXPR_BLOCK_OCCUPANCY_OR_MONSTER_COUNT counts,
for each listed type, the MonsterRuntime
slots whose type_index equals the type, whose sublevel equals the current
sublevel word, and whose current_hp_raw, read as signed, is not negative. It
does not compare the slot's block. The byte after the token only selects
this form and is not counted as a type; the types are the bytes that follow
it, up to the end byte 0xff. sublevel is set when a monster is placed
(see Place a monster) to the sublevel then
current, and this count is the only code that reads it; see
sublevels.2
Items and characters
Several tokens read state that other instructions also use: block and item
data (LevelBlockRuntime,
ItemRuntime,
ItemTypeRuntime), party characters (
CharacterRuntime), the level and
global flags written by Set a flag and
Clear a flag, and the party's block and facing. The
character-selecting tokens set the script's selected character, which
instructions with a random character selector then use. The item tokens are
described by ScriptExpressionItemSelector.
The character tokens first call the eligibility test with a slot and a
CharacterEligibilityMask:
CHARACTER_ELIGIBLE_PRESENT | CHARACTER_ELIGIBLE_CONSCIOUS for the search
tokens, PRESENT | NOT_DEAD | CONSCIOUS | NOT_PARALYZED_OR_PETRIFIED for
SCRIPT_EXPR_ACTIVE_CHARACTER_COUNT and CHARACTER_ELIGIBLE_PRESENT in the
helper used by SCRIPT_EXPR_PARTY_BLOCK_OR_ITEM_HOLDERS. In game terms the
search tokens consider the conscious characters, the active count the
characters able to act, and the item-holder count every character in the
party, dead or not; the status bits are the
CharacterStatusFlags.1
The item-holder form of SCRIPT_EXPR_PARTY_BLOCK_OR_ITEM_HOLDERS searches
each character's 27 inventory slots for an item of a type and value; the
searches of a block's items leave out items in flight, except
SCRIPT_EXPR_BLOCK_ITEM_TYPE_COUNT with a nonzero flag operand. The exact rules are on the
token page, and
what the original levels test with them is summarized under
what the conditions test.3
Verification against original scripts
The encoding was checked by decoding every script reachable from the cell
bindings of the 16 reference INF files (see
decoding status). The reachable code contains
1958 instructions of this kind. In each, the stack holds exactly one word at
the terminator, and both the byte after target and the byte at target are
instruction boundaries of the decoded flow. Of the 1047 distinct script
references in the cell bindings, 623 begin with this opcode. Operators
observed: ==, !=, <, >, AND, OR; <= and >= do not occur.