Control flow
Script virtual machine · Instruction encoding · How a script runs
In brief
Five commands steer a run: a jump, a condition that jumps when its expression is zero, a call that saves the next position on a return stack of ten, a return, and an end. The return stack belongs to the whole program, not to a run, so a run started inside another can return into the script that started it.
The five commands
| Command | Effect |
|---|---|
Jump (SCRIPT_JUMP) | Continue at a script offset. |
Test a condition (SCRIPT_JUMP_UNLESS) | Evaluate an expression; continue if it is nonzero, otherwise jump. |
Call a subroutine (SCRIPT_CALL) | Save the position after the instruction and continue at a script offset; if the return stack is full, just continue. |
Return from a call (SCRIPT_RETURN) | Resume at the last saved position; with an empty return stack, stop the run. |
End the script (SCRIPT_END) | Stop the run and empty the return stack. |
The return stack holds up to ten positions. A level change also stops the run and empties the return stack.1
The return stack, like the expression stack, belongs to the whole program, not to a run. A run that starts while no other is under way always finds it empty, because each way of stopping a run leaves it empty. A run can also start inside another, when a command sends a block event (scripts started by scripts); that run starts with the outer run's return stack as it is.
ANOMALY-83. When a run starts, the dispatcher resets the run's other state
— run variable, block, event, stop flag, selected character, level-load flag
— but not the depth of the return stack, although
moving the party
saves and restores that depth with the rest. A script started from inside a
subroutine therefore inherits the pending call. Its first
SCRIPT_RETURN without a call of its own does not stop it: it resumes the
outer script after that call, and the rest of the outer script runs inside
the nested run, with the nested run's block and event. A
SCRIPT_END of the nested run empties the stack; the party-move command then
puts the depth back, so the outer script continues after the move and, at
its own return, takes the same saved position again unless the nested run
overwrote it with a call. A faithful reimplementation keeps a single return
stack and does not clear it when a run starts.
The reference levels contain the conditions for it. In level 14 the script
of [01:00] calls a subroutine that can move the party from [01:06]
onto [29:12], and the script of [29:12] then ends with a return. The part of the
outer script that the nested run then executes only tests the party's
position on other blocks. The party reaches this state by standing on
[01:06] when the timer opens the pit there (inferred from the map). The
rest of the timer script then runs twice, once inside the nested run, and it
only tests the party's position on the hall pits, never on the lower grid
where the fall ends, so neither run has a visible effect. The same
subroutines hold ten more moves of this kind, onto [31:13], [29:15],
[30:15], [30:12], [30:14], [31:14], [31:12], [30:13], [29:14] and
[31:15]. Their scripts have the same shape and return into the same tail, so
they act alike whenever the party stands on the block that the timer opens;
how often it can depends on the state of the puzzle.
No other subroutine of the reference levels gives this effect. The party moves
in subroutines of levels 4, 8 and 9 reach no script, or one that ends with
SCRIPT_END. The monster placements in subroutines of levels 7, 9, 11 and 16,
and the level changes in subroutines of levels 11, 13 and 14, land on blocks
without a script.2
Sources
Locations refer to the game executable reference; comparative sources are listed in the technical references.