Dialogue screen
SCRIPT_DIALOGUE · opcode 0xe3 · 2 to 22 bytes
Script virtual machine · Opcode list · Scripted dialogue
Drives the illustrated dialogue screen: it opens and closes that screen, draws pictures, shows texts and asks the player to choose an answer.
Encoding
Offsets count from the opcode byte. Words are little-endian. The signed byte
at offset 1 is a ScriptDialogueCommand
and decides what follows. Sizes include the opcode and the selector.
| Selector | Byte | Operands after the selector | Size |
|---|---|---|---|
SCRIPT_DIALOGUE_BITMAP | 0xd3 | name_raw: 13 bytes, frame: s8, source_x: u16le, source_y: u16le, flags: u16le | 22 bytes |
SCRIPT_DIALOGUE_RESTORE | 0xd4 | None | 2 bytes |
SCRIPT_DIALOGUE_INITIALIZE | 0xd5 | None | 2 bytes |
SCRIPT_DIALOGUE_BOX | 0xd6 | None | 2 bytes |
SCRIPT_DIALOGUE_CHOICE | 0xd8 | text_id: u16le, option_1: u16le, option_2: u16le, option_3: u16le | 10 bytes |
SCRIPT_DIALOGUE_TEXT | 0xf8 | text_id: u16le, prompt: u16le | 6 bytes |
| Any other value | — | None; the handler returns after the selector | 2 bytes |
Two kinds of text are involved. A text_id selects a text of
TEXT.DAT, loaded when the screen opens.
prompt and the option_* words are indices of strings in the level's
message table, the one that Print a message uses.
The handler does not check that the commands come in a sensible order; what
happens when they do not is described under
edge cases.1
Example (constructed, not from a level): e3 f8 14 00 05 00 shows text 20 of
TEXT.DAT with message 5 of the level as the prompt label.
Opening and closing the screen
SCRIPT_DIALOGUE_INITIALIZE forgets the last drawn picture, sets the prompt
label to the built-in string MORE, marks the dialogue screen as active and
reads up to 32 000 bytes of TEXT.DAT into the dialogue-text buffer. It then
makes the dialogue text window the active one, with
its cursor at the top left, and turns page waits on, with the dialogue's own
wait prompt as the routine that waits. It also
closes the inventory panel and the spell book and plays sound 0, which stops
the sound in progress.2
SCRIPT_DIALOGUE_RESTORE marks the screen as inactive, forgets the last
picture, turns page waits off, makes the message area at the bottom of the
screen the active text window again, rebuilds the play screen (wall graphics, view and portraits), plays sound 2,
and
requests a scene update, as
Move the party, monsters or items also
does.2
SCRIPT_DIALOGUE_BOX takes no operand. It draws a framed box over the lower
part of the screen, the full width from row 121 to row 199, and clears the
text window, whose cursor returns to the top left.3
Drawing a picture
| Operand | Meaning |
|---|---|
name_raw | Zero-terminated resource base name. In all 237 reference instructions it is at most 8 characters and names an existing .CPS file. |
frame | A ScriptDialogueBitmapFrame: index into the destination tables below. |
source_x | Left edge of the source region, in 8-pixel columns. |
source_y | Top edge of the source region, in pixels. |
flags | ScriptDialogueBitmapFlags; only SCRIPT_DIALOGUE_BITMAP_KEEP_BACKGROUND and SCRIPT_DIALOGUE_BITMAP_TRANSITION are tested. |
The bytes after the name's terminator are not read. In the reference files they hold unrelated leftover bytes, so an editor must preserve the whole 13-byte field rather than regenerate it.4
The destination and size of the copied region come from two-entry tables in
the executable, indexed by frame; other values read neighboring data, as
described under edge cases:
frame | Destination (pixels) | Size (pixels) |
|---|---|---|
SCRIPT_DIALOGUE_FRAME_PICTURE (0) | x 8, y 8 | 160 × 96 |
SCRIPT_DIALOGUE_FRAME_FULL (1) | x 0, y 0 | 320 × 121 |
The picture is then drawn in these steps:4
- Choose the target page: the visible screen if
flagshasSCRIPT_DIALOGUE_BITMAP_TRANSITIONorframeis nonzero, otherwise a work page. - If no picture is remembered, or
name_rawdiffers from the remembered name (case-sensitive):- for
frame0 only, draw the 184 × 121 frame area from theBORDERpicture, or keep the current screen content ifflagshasSCRIPT_DIALOGUE_BITMAP_KEEP_BACKGROUND; - load the named picture and remember it.
- for
- Copy the region at (
source_x× 8,source_y) of the table's size to the table's destination. WithSCRIPT_DIALOGUE_BITMAP_TRANSITION, the pixel dissolve performs the copy directly to the screen, one pixel at a time in a shuffled order. - If the work page was used, copy its 184 × 121 area to the screen.
The handler remembers a pointer to the name inside the script buffer, not a
copy of it. SCRIPT_DIALOGUE_INITIALIZE and SCRIPT_DIALOGUE_RESTORE clear
it, so the first picture after either is always loaded.
The reference scripts use only source_x 0 or 20 and source_y 0 or 96. With
frame 0 these select one of four 160 × 96 regions of a 320 × 192 area of the
picture. Every picture drawn with frame 0 is a full-screen image holding
such a 2 × 2 grid; its last eight rows are not used. In some files the four
pictures are variants of one scene that differ in small parts, as in
KHELBEN or STONEGIA; in others they are unrelated pictures, as in
SOUT1. The bottom-right picture of TANGLOR is empty and never drawn,
that of SOUT4 is never drawn, and that of SUICIDE, a plain colour-12
area, is drawn. DARKMOON, the only picture drawn with frame 1, is used
from its top 121 rows.
Text window and pages
Texts appear in a window of 304 × 64 pixels whose top left corner is at x 8,
y 125, below the picture area. With the 6 × 6 font the game uses during play
(inferred: the game installs that font when play starts and the dialogue
routines do not change it), the window holds 10 lines of 50 characters. Letters are drawn in colour 15 on
the interface's background colour. A text starts where the window's cursor
is: at the top left after SCRIPT_DIALOGUE_INITIALIZE,
SCRIPT_DIALOGUE_BOX or a wait, and otherwise just
after the previous text, so two texts shown without a prompt or box between
them run on.5
Three control bytes occur in the texts of TEXT.DAT:
| Byte | Effect |
|---|---|
0x01 | Page break: wait for the player at once, then go on at the top of the cleared window. |
0x06 n | Draw the rest of the text in colour n; the texts use 9 (the colour of a highlighted answer) and 15. |
0x0d | Start a new line. |
The colour returns to 15 when the text ends. The game's text printer knows a few other control bytes, listed in the executable reference; the reference texts do not use them.
Words are wrapped at spaces. The printer collects characters until they would pass the right edge, then cuts the line:
when the pending characters do not fit in the room left on the line:
cut after the last space that fits
if there is no such space:
if the line already holds text: move all of them to the next line
else: cut at the right edge, in the middle of the word
drop the space at the cut, and one more if it is doubled
On the last three lines of each page (lines 8 to 10) the room is seven characters shorter, 43 characters with the 6-pixel font.
A text that needs more lines than the window holds is shown in pages: when a
line would fall below the bottom, the game waits for the player with the
current prompt label, normally MORE, then clears the window and goes on at
the top. A page therefore shows ten new lines. A text that just fits does not
wait; only its prompt, if any, does.
Waiting for the player
A wait shows one button below the text window: a framed box of 95 × 9 pixels at x 221, y 189, with the prompt label centered in colour 15. The mouse pointer stays visible (inferred from the pointer bookkeeping around the wait). Keys pressed before it appears are discarded. The wait then ends on:
- Space, Enter (either key) or M, with or without Shift, Ctrl or Alt;
- a press of either mouse button with the pointer inside the button.
Every other key, a click elsewhere and button releases are ignored, and there is no time limit. The button is then erased and the text window cleared. The same wait serves page breaks and the prompt that follows a text.6
The wait does nothing but read the input queue. The script runs inside the step of the game loop that started it, so neither monsters nor the game's timed updates advance while the player reads: they resume when the script ends, each overdue update running once.6
Displaying a text
| Operand | Meaning |
|---|---|
text_id | One-based entry of the offset table at the start of the dialogue-text buffer. |
prompt | Message used as the label of a wait prompt after the text; an empty string shows no prompt. |
The text routine reads entry text_id − 1 of the table and prints the text
at that offset in the same buffer, as described under
text window and pages. If the prompt string exists
and is not empty, the routine copies it to the prompt label,
waits for the player, then resets the label to
MORE. The index −1 gives no string and therefore no prompt. A text_id of
zero or beyond the table is not checked.7
In the reference TEXT.DAT, the table has 122 entries and every reference
text_id lies between 1 and 122. The most frequent value, 20 (86 of the 168
text instructions), selects an entry consisting of a single space: such
instructions mainly display their prompt. Thirty-four of the 168 instructions
use an empty prompt string.
Asking for a choice
| Operand | Meaning |
|---|---|
text_id | Text displayed first, as for SCRIPT_DIALOGUE_TEXT but without a prompt. |
option_1, option_2 | Messages labelling the first two answers. |
option_3 | Third answer, or −1 for a two-answer question. |
The game shows the text, then two answers when the third message does not
exist and three otherwise. The answer chosen by the player, numbered 1, 2 or
3, is stored in the run variable read by SCRIPT_EXPR_RUN_VARIABLE of
ScriptExpressionToken.8
A two-answer question works because the lookup gives no string for −1. Only the third answer is tested this way. −1 as the first or second answer still gives a button whose label is a null pointer. The label's length counts as zero, so the text starts at the middle of the button, but the text routine does not check the pointer: it draws the bytes found at the very start of memory, the processor's interrupt table, up to the first zero byte, and wraps at the right edge of the screen. What appears therefore depends on the machine. The reference scripts never do this.
In the reachable scripts of the 16 reference INF files, 21 of the 25 choices have two answers and four have three. The conditions that follow a choice compare the run variable with 1 (15 times), 2 (7 times) and 3 (once).
Answer buttons
The answers are buttons like the wait button, 95 × 9 pixels, on one row whose top is 4 pixels below the line after the text's last line: y = 125 + (cursor line + 1) × 6 + 4 with the 6-pixel font. Two answers start at x 59 and 166; three at x 4, 112 and 220. The highlighted answer has its label in colour 9, the others in colour 15; the first answer is highlighted at the start.9
The game then reads one input at a time. Before acting on it, if the mouse pointer lies on an answer, that answer becomes the highlighted one; the highlight therefore follows the pointer only when a key or a mouse button is used. Then:
| Input | Effect |
|---|---|
| Enter (either key), Space or keypad 5 | Chooses the highlighted answer. |
| Press of either mouse button on an answer | Chooses that answer. A press elsewhere does nothing. |
| Left arrow or keypad 4 | Highlights the previous answer; from the first, the last. |
| Right arrow or keypad 6 | Highlights the next answer; from the last, the first. |
| Up and Down arrows, keypad 8 and 2 | Move by rows of three answers; with two or three answers they do nothing. |
| Anything else, including Escape | Nothing; the question cannot be dismissed. |
The grey arrow keys and the keypad arrows give the same result, and so do
the two Enter keys. Once an answer is chosen the game draws the dialogue box
again, as SCRIPT_DIALOGUE_BOX does, which also clears the text window. As
for a wait, the game loop does not run while the question is
displayed.9
ANOMALY-81 — Arrow keys in a menu of more than three entries. The same menu routine serves the character menus of special events, which can have more than three entries, laid out in rows of three. Entries are numbered from 0; the four keys apply these rules:
left(sel): sel = sel - 1; if sel < 0: sel = min(count - 1, 2)
right(sel): sel = sel + 1; if sel > 2 or sel > count - 1: sel = 0
up(sel): if sel - 3 > 0: sel = sel - 3
else if sel + 3 < count: sel = sel + 3
down(sel): if sel + 3 < count: sel = sel + 3
else if sel - 3 > 0: sel = sel - 3
then apply up(sel) as well
Right therefore returns to entry 0 from entry 2 or any later entry, Up treats entry 3 as if it were on the first row, and Down, which ends with the Up rule, usually undoes its own move. With seven entries, Up takes 0 to 3, 1 to 4, 2 to 5, 3 to 6, 4 to 1, 5 to 2 and 6 to 3, while Down moves only from entry 0, to entry 6. With two or three entries, as in every dialogue question, these rules give the expected Left and Right and leave Up and Down without effect. A faithful reimplementation must apply them as written.
Consequences (inferred from the rules): up to seven entries, every entry can still be highlighted with the keys, by indirect paths. With eight or more, which the resurrection menu can produce (it has no limit on its length), the keys never reach entry 7 or any later one; only the mouse can choose them.9
Edge cases and unknowns
A text or question without SCRIPT_DIALOGUE_INITIALIZE. Nothing checks
that the screen is open:10
- The text is read from the dialogue-text buffer, whatever it last
received:
TEXT.DATif nothing has used the buffer since the last dialogue or parchment, otherwise unrelated data, such as the level data that the buffer holds while a level loads. The bytes found are used as the offset table and the text, up to the first zero byte. - The text goes to the active text window at its current cursor, normally the message area at the bottom of the screen, where page waits are off: a long text scrolls through it without waiting.
- A prompt still shows the wait button and waits, and a question still places its answers below the active window's cursor line, at the bottom edge of the screen for the message area. After the answer, the dialogue box is drawn over the lower part of the screen.
- The screen is not marked as active, so a level change made later in the same run does not restore the normal view first, as it does during a dialogue.
The reference scripts never do this. Every text and every question follows a
SCRIPT_DIALOGUE_INITIALIZE of the same dialogue, with one apparent
exception: the temple entrance
of level 4 skips the initialization when level flag 6 is set, and that flag is
set only by a script that has just opened the screen and then moves the party
onto the entrance, which runs the entrance script inside that open dialogue.
frame outside 0–1. The value is not checked. The four tables (width
in 8-pixel columns, height, x in 8-pixel columns, y) lie one after the
other, each two words long, between tables of pointers; the picture routine
reads, in each, the word frame places after its first entry, which for
other values falls in the next table or in the pointers around them. Any
nonzero frame also draws directly on the screen without the BORDER
frame. For the nearest values the words read are:11
frame | Width | Height | x | y |
|---|---|---|---|---|
| −2 | 5669 | 20 | 96 | 1 |
| −1 | address | 40 | 121 | 0 |
| 2 | 96 | 1 | 8 | 5709 |
| 3 | 121 | 0 | 0 | address |
"Address" marks a word that the DOS loader adjusts to the program's load
address, so its value differs between runs. Further values reach other
pointer tables. Without SCRIPT_DIALOGUE_BITMAP_TRANSITION, none of these
values draws anything. The picture is still loaded and remembered, but the
copy rejects the destination column
of −2 and −1 and the video driver the destination row of 2 and 3. For 3 the
"address" word holds the location of the program's data, which stays above
row 200 (inferred for any memory configuration that can run the game).
With SCRIPT_DIALOGUE_BITMAP_TRANSITION, the
pixel dissolve receives
the region without its size being limited. For 2 and 3 it draws nothing
visible. For −2 it writes 45,352 column numbers into a 5,120-byte work area
and overwrites program code (inferred consequence: the game stops
working). For −1 the size depends on where the program is loaded. The
reference scripts use only 0 and 1.
Other cases. For an unrecognized selector, the handler returns after the
selector; this does not establish that such selectors are valid. Malformed
TEXT.DAT data is not characterized.
Observed use
The 782 reachable instructions of the 16 reference INF files use
SCRIPT_DIALOGUE_BITMAP 237 times, SCRIPT_DIALOGUE_TEXT 168,
SCRIPT_DIALOGUE_BOX 143, SCRIPT_DIALOGUE_RESTORE 110,
SCRIPT_DIALOGUE_INITIALIZE 99 and SCRIPT_DIALOGUE_CHOICE 25 times.
Pictures use 24 distinct names, frame 0 in 236 instructions and 1 once, and
flags 0 (208), SCRIPT_DIALOGUE_BITMAP_KEEP_BACKGROUND (20) or
SCRIPT_DIALOGUE_BITMAP_TRANSITION (9).