Level records and level changes
Saving and restoring · Changing level · Level records (LEVELS.TMP)
In brief
Before it saves a game or leaves a level, the game settles the level:
objects in flight drop or vanish and doors finish moving. A level's record
keeps its walls, its 30 monsters and its temporary walls; one bit per level
says whether it has one. Leaving a level writes its record to LEVELS.TMP,
and coming back restores the level from it instead of from its files.
Settling the level
Before it saves a game, before it loads one and before the party changes level, the game:
- ends every flying object. A thrown item falls to the floor of the block it
is in, and that block receives
BLOCK_EVENT_ITEM_PLACED, which can run the block's script. Other flying objects, such as spells, disappear; - finishes at once every door that is opening or closing;
- clears the attack frame of each monster slot (
attack_frame, which holds a melee animation in progress), so no monster is saved in the middle of an attack.
These changes apply to the game in progress, not only to the copy being written. A saved game or level record therefore never holds an object in flight or a door in motion.1
Level records
A level record is 2,130 bytes:
| Part | Size in bytes | Contents |
|---|---|---|
| Walls | up to 1,200 | The four wall references of each of the 1,024 blocks, compressed with the method of type-4 containers. |
| Monsters | 900 | The 30 monster slots, as they are in memory. |
| Temporary walls | 30 | The five temporary walls, each with the time it has left. |
The walls are stored as they are, not as a difference from the level's map, so a record keeps every wall change made by doors and scripts. The rest of each block (which events it accepts, its script, its item list and its monster count) is not stored: it is rebuilt when the level is loaded.
One bit per level, kept with the party variables, records which levels have a record. Setting the bit is how the game knows to restore a level instead of starting it from its files.2
A new game
A new game neither creates nor empties LEVELS.TMP. When the program
starts, it clears the memory area that holds the record bits, so every bit
is clear; the start-up screen, and with it a new game, runs only once per
run of the program, before any level is left. The records already in the
file are therefore ignored: each level of the new game starts from its
files.
The file is first written when the party leaves a level: the game opens it without truncating it (creating it if it does not exist) and writes that level's record at its place, so the other records keep whatever the file held. Saving copies the whole file, stale records included, into the saved game, where their bits are clear; the reference saved game holds 16 such records besides that of level 4. Loading a game replaces the whole file with the saved records. Nothing deletes the file when the program ends.3
Changing level
When the party changes level, the game:
- settles the current level;
- writes the current level's record to
LEVELS.TMPand marks the level as having one (see the level-16 anomaly); - loads the new level. If the new level has a record, its walls, monsters and temporary walls come from the record, and its initial monsters are not placed again. Otherwise its walls come from its map and its monsters from its INF file;
- in both cases, rebuilds the item list of every block from the shared item table: each item whose level is the new level and whose block is positive is put back at its position;
- in both cases, reads the scripts, the cell bindings, the monster update intervals, the timed block scripts and the step-triggered script from the INF file, which restarts their timers and the step counter.
The characters, the items, the flags and the periodic updates belong to the whole game: a level change does not touch them.4
ANOMALY-138 — The saved-level bits form a 32-bit value, but the bit of a
level is built as a 16-bit value and then extended with its sign. For level
16 it is the top bit of the 16-bit value, so recording level 16 also sets the
bits of levels 17 to 32, and the test for level 16 looks at all of them. No
level above 16 exists and levels 1 to 15 use only their own bits, so play is
not affected. A faithful reimplementation sets bits 15 to 31 when it records
level 16.5
Sources
Locations refer to the game executable reference; comparative sources are listed in the technical references. File sizes and hashes are in the file inventory.