Scripts, Text & Audio

QuestScript

The readable language the port uses for level scripts - converted from the disc's compiled scripts and compiled back to the same bytecode - with the language, the conversion and its check, raw instructions, the rta-qsc command line and the limits.

Every level's quests, dialogs, triggers, waves and doors are driven by its script.scr: a compiled program in Small (the language that became Pawn). QuestScript is a readable language that compiles to exactly that bytecode, so a QuestScript file runs on the same machine, with the same functions, as a disc script.

The port can:

  • convert any disc script.scr into QuestScript, checked to run exactly as the original,
  • compile a mod's script.qs in place of the disc's script.

So every level's script can be changed, extended, rewritten or replaced, and custom maps can have scripts of their own. You edit them in the Script Editor. Level scripts can also be written in Pawn, or as blueprints.

Where scripts go

What File in the mod Notes
A disc level's script BG/DATA/<LEVEL>.GOB/<lump>.lmp/script.qs Replaces that lump's script.scr. The level is converted again the next time it is opened.
Files it includes Beside it: BG/DATA/<LEVEL>.GOB/<lump>.lmp/<name>.qs include "<name>.qs";
A custom map's script maps/<id>.qs Runs in place of the base level's script. Includes are read from maps/.

A script.qs that doesn't compile is reported in the log with its file, line and column, and the level then runs the disc's script. A broken map script leaves the map without a script.

Warning Converted scripts are made on your machine from your own disc. A mod may change them, but think of the converted text as the disc's: see what a mod may contain.

The language

It reads like C. Here is a small level script:

// BLOOD1: the captain's quest
include "common.qs";          // another file of the same lump (or maps/)

const kOrcs = 12;             // a named number
var orcsLeft = kOrcs;         // a global: it keeps its value while the level runs
var seen[4];                  // an array of 4 cells, zeroed

public start()                // publics are what the game and the level's objects run, by name
{
    hideMonster("captainAmbush");
    showBust("orcPortrait", "orcs_remaining", orcsLeft, kOrcs);
}

public onOrcDeath()           // an orc with deathscript=onOrcDeath
{
    orcsLeft--;
    showBust("orcPortrait", "orcs_remaining", orcsLeft, kOrcs);
    if (orcsLeft == 0) {
        setv("blood1Orcs", 1);
        trigger("exitGate", 1);
    }
}

public talkCaptain()          // an NPC with dialogscript=talkCaptain
{
    startDialog(getv("blood1Orcs") ? "captainDone" : "captain", 1, 0);
}

function clamp(v, lo, hi)     // a function only the script calls
{
    if (v < lo) return lo;
    return v > hi ? hi : v;
}

Values

Every value is a 32-bit integer. A string literal ("exitGate") is passed to functions that take a name or a text key. Numbers can be written as 12, 0x1f, 0b101 or 'a'.

Declarations

  • var declares globals and locals: var a, b = 2, list[3] = {1, 2, 3}; or var grid[10];.
  • const declares named numbers.
  • include "file.qs"; belongs at the top level.
  • Lists of strings (globals): var names[] = {"orc1", "orc2", "orc3"};. names[i] is a string, passed where a function takes a name (generateMonster(names[i], "orc")). The list can't be changed.
  • 2-D arrays (globals): var odds[16][4] = {{1, 2, 3, 4}, {5, 6}};, read and written as odds[row][column] (rows and values not listed start at 0).

Functions

  • public name() { } is visible to the game: the level's objects and the game run publics by name. Public names are at most 19 characters, and the game calls them without arguments.
  • function name(a, b) { } declares a helper only the script calls.
  • A function that ends without return returns 0.

Statements and expressions

  • if / else, while, do ... while, for (init; test; step), break, continue, return, goto label; and label: work as in C.
  • switch follows the original's rules: there is no fall-through, a case can list several values (case 1, 2:), and default: is optional.
  • All of C's operators work, including &&, ||, ?:, ++ / --, compound assignment and >>> (unsigned shift). / and % round toward minus infinity, as the original's do.
  • Comments are // and /* */.

The game's functions (natives)

The game has 191 functions scripts call by name: showing and hiding monsters, dialogs, variables kept in the save (getv / setv), triggers, doors, the shop, busts and messages. The Script Editor's Natives tab lists them all by group, with their parameters and what they do.

The compiler warns about a function it doesn't know, a wrong argument count, and a number passed where a function takes a name.

Who runs a public

The level's objects name publics in their script fields: a creature's deathscript runs when it dies, an NPC's dialogscript when it is talked to, a trigger's t when it is entered, a use spot's action when used. The game runs start() when the level begins. The Script Editor's Functions tab lists, for each public, the objects that run it. See Map objects for the fields.

Raw instructions: asm

A function can be written as the machine's own instructions:

public strange() asm {
    proc;
    load.pri g_3;          // globals and functions by name, name+4 for an offset into one
    push.pri;
    push.c 4;
    sysreq.c getv;         // a native by name
    stack 8;
  L_done:
    retn;
}

The converter writes a function this way only when it can't express its exact behaviour as statements. None of the disc's 1,828 functions need it any more, but a script of your own may use it. Every instruction of the Small machine is available; operands can be numbers, name, name+N, labels and "string" literals, and casetbl default, value label, ... writes a jump table.

Converting the disc's scripts

The converter reads the bytecode back into statements: it rebuilds &&, || and ?:, if / else, the loops, switch, for and goto. It writes:

  • a header naming the level, and a comment above each public listing who runs it (from the level's objects, for example // run by Orc orc3 (deathscript)),
  • the text of each text key the script uses, as a comment,
  • the names it can't know as g_N (globals), l1 (locals), p1 (parameters) and fn_XXXX (functions that aren't public). Rename them freely.

How a conversion is checked

The checker compiles the converted text again, then runs every public of the original and of the rebuilt script side by side, 48 times each, in the same simulated game: the functions answer with the same random values, and the globals start from the same random values. It compares everything each run does: every function called with its arguments, the values returned, errors, and the globals it leaves.

If a public differs, the converter writes it (and the functions it calls) as asm, so the result always runs as the original. On the disc, every one of the 1,828 functions comes back as statements.

The same check is the Script Editor's Compare with original: after you edit a script, it lists the publics that now behave differently, which should be exactly the ones you meant to change.

The command line: rta-qsc

rta-qsc compile <in.qs> [-o out.scr]           compile (includes are read beside the file)
rta-qsc convert <in.scr> [-o out.qs]           a script.scr to QuestScript, checked against the original
rta-qsc decompile <in.scr> [-o out.qs] [--asm] the same without the check (--asm: every function as asm)
rta-qsc verify <original.scr> <script.qs>      does the .qs run as the original?
rta-qsc disc <disc image> <out dir>            every level script on the disc, converted

See Command line tools.

Limits

  • A level script can't call another level's functions, and its globals last only while its level runs. Use getv / setv to keep a value in the hero's save. As on the disc, every save variable goes back to 0 when the party enters a level of another plane (realm group): the plane left starts over. Keep a value across planes with a profile variable (getpv / setpv), which lasts across games too.
  • The functions are the game's: a new one needs engine code. For more, use a Pawn or Lua game script alongside.
  • A call that runs more than 5,000,000 instructions (a runaway loop) is stopped, and so is nesting more than 32 calls deep.