Scene script modules¶
Each level's scripts are one file, SCRIPTCS/_GAMEREL/<LEVEL>.PS2 (HIDEOUT and M00 to M13, no M07). ScriptEngine::AttachSceneScripts (src/engine/zstdlib/zscriptc.cpp) loads it, relocates it and calls each script's Initialize. The code is MIPS from GCC 3.2, compiled from C that IOI's script translator wrote; the original script language is not on the disc.
tools/port/script_explorer.py reads the modules on the user's disc and lists what each script contains. Its output is derived from the disc, so it goes under build/scripts/ and is not committed.
tools/third_party/venv/bin/python3 tools/port/script_explorer.py # every module
tools/third_party/venv/bin/python3 tools/port/script_explorer.py M00 --script Alllevels_Bird
tools/third_party/venv/bin/python3 tools/port/script_explorer.py M00 --disasm # annotated assembly per script
The script headers, and what is missing for new scripts, are in docs/internals/scripts/script-sdk.md.
It writes <MODULE>.txt (a readable listing), <MODULE>.json (everything), index.txt (which scripts are in which modules and whether their code is the same) and, with --disasm, <MODULE>/<script>.s. It needs rabbitizer from make helpers. --module-file reads a .PS2 from a file instead of the ISO.
File layout¶
All values are little-endian.
| Part | Contents |
|---|---|
| Header, 16 bytes | Scripts (image offset of the script table), lCodeSize, lGPStart, lAlign |
Image, lCodeSize bytes |
Data first, then code. In M00 the code starts at 0x16378 of 0xa0580. |
| Symbols | A count, then one image offset per symbol. Symbols have no names. |
| Imports | A count, then {symbol index, name length, name} for each undefined symbol. The modules import ScriptImports (the 716 engine interfaces), SF (the 27 service functions) and memset. The engine could also supply ISF and fptoui. |
| Relocations | A count, then RelocInfo {symbol, offset, type} records. |
Relocation types used: 2 adds the symbol's address to a word, 4 sets a jal/j target to the symbol (the instruction field is ignored), and 5/6 are a %hi/%lo pair whose addend is the two halves in place. The loader also accepts 0, 3 (PC-relative word) and 7/8 (GP-relative); no shipped module uses them.
Script tables¶
Scripts points at a registry with a reserved word at entry 0, followed by SCRIPTCREATOR * entries and a terminating null. The reserved word is 0x3B4 in all shipped modules. The engine starts at entry 1. The structures are in src/engine/eventsbase/scriptc.h.
- SCRIPTCREATOR (0x38 bytes): the name (
FindScriptmatches it case-insensitively, with/and\read as_), the sizes of the script and state variable blocks, the root state, the base script, a table of virtual states, the script-level message handler,Initialize, the saved statics, theImports/StaticImports/UnpackResources/UnpackStaticResourcescallbacks and the property list. - Properties (
SCRIPTIMPORT, 16 bits each):type:3, count:13, ending with type 6. The types are byte, short, long, float, string and REF. These are the values a level sets on each instance of the script;Importsreads them in order withSF.Input. Their names are not in the module. - Saved statics (
SAVEGAMESTATICS, 8 bytes each):type:8, size:24, then an address, ending with type 5. - STATECONTROLLER (0x20 bytes): run, enter and destroy functions, a message handler, the functions table,
m_lLevel(nesting depth, 1 for a root),m_lScriptLevel, the parent state and string offsets. A nested state's parent is the state around it. A derived script's root state's parent is the base script's root state. - FUNCTIONCONTROLLER (0xc bytes, padded to 0x10): entry point, argument block size, local variable size, string offsets. Every state step and every script function has one.
- Functions table: an indexed array of
FUNCTIONCONTROLLER *that a state carries. Derived scripts list their base's functions where they do not override them. The array ends after its last slot. The explorer names a slotF<n>.
A relocated pointer word can resolve to image offset zero. M09's alligator state 2 uses such a pointer to reach its run controller at the start of the image.
No state, function or variable names are stored, so the explorer names states root, state1, state2... in tree order, and functions by their role (state3.run) or table slot (F2).
What the code looks like¶
- Resumable functions. A script function is
float f(ScriptState *). It begins by fetching the current frame (LocalVarEntry) from a runtime function, reads the resume index at offset 0xc (bits 0 to 14) and dispatches on it, with compare chains or a jump table. Before it yields, it stores the next index and returns the time to wait. A negative return is a request to the scheduler (ZScriptC::SchedUpdateinsrc/engine/eventsbase/scriptc.cpp): -2 ends the script, -3 and -5 keep running in the same update (-3 only while the 5,000-cycle budget lasts), -4 forks a state, -6 sets the thread's priority and -7 runs again next update. - State changes store the new
STATECONTROLLERintoScriptState::m_pNextStateController(offset 0x1c). - Message handlers are
void f(ScriptState *, ZMSG msg, void *pData). They comparemsg & 0xffffagainst constants, or switch on it with a jump table, and pass anything else to the parent state's handler, often as a tailj. - Message numbers are the
ZMSGids of the engine's message trie (ZScriptC_ZMessagesandUniquesinzscriptc.cpp, walked byGetRegisterZMessageID); the explorer names them from that trie. - Engine calls load an entry of
ScriptImportsorSFandjalrto it. GCC keeps the table base in a register across calls (lw $v0, 0x38($s0)with$s0=ScriptImports + 0x91ccalls entry 597). It also merges identical call tails, so onejalrcan call different interfaces on different paths. The explorer follows register values along the branches of each function to resolve both. - Runtime functions. About 20 functions are called from nearly every script: get the current frame, return from a script function, call another script function (its controller passed in
$a3), allocate and free frames, and copy and free strings. They are the translator's support code, linked into every module.
What the shipped modules contain¶
Over the 14 modules:
- Scripts and states: 693 scripts, 436 distinct names, 4,467 states (2,021 with a message handler) and 27,431 functions assigned to scripts. The 280 shared runtime functions and 183 unplaced functions bring the total to 27,894.
- Shared scripts: 34 scripts appear in more than one module: the
Alllevels_*AI and objects. 22 of them have the same code everywhere; the others differ between levels,Alllevels_Humanin all 14. - Calls: 34,280 engine call sites to 614 of the 716 interfaces, 39,185 service calls and 2,118 calls through pointers read at run time (the next handler in a message chain).
- Dead code: 816 functions are never called. The translator emits every function a script defines.
The explorer resolves every jump table in the shipped modules and finds no unreachable code.
Limits¶
- Messages: "messages sent" covers
SF.SendCommandandSF.SendScriptCommandwith a constant message. Interfaces that take aZSC_EVENTare not decoded. - Unplaced functions: an unused function whose controller sits between two scripts' records has no owner; the module summary lists these.
- Paths: register tracking ignores which path a branch takes. A base register that differs between two paths into a block becomes unknown rather than a guess.