Skip to main content

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:

  1. 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;
  2. finishes at once every door that is opening or closing;
  3. 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:

PartSize in bytesContents
Wallsup to 1,200The four wall references of each of the 1,024 blocks, compressed with the method of type-4 containers.
Monsters900The 30 monster slots, as they are in memory.
Temporary walls30The 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:

  1. settles the current level;
  2. writes the current level's record to LEVELS.TMP and marks the level as having one (see the level-16 anomaly);
  3. 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;
  4. 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;
  5. 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

The steps of a level change. The game settles the level, writes its record to LEVELS.TMP and sets its bit, then reads the new level's INF data. If the new level has a record, its walls, monsters and temporary walls come from the record; otherwise the walls come from the map and the monsters from the INF file. In both cases the item lists of the blocks are rebuilt from the item table.
A level met before comes back from its record; the item lists are always rebuilt.

Anomaly

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.

Footnotes​

  1. Settle the level, with the flying-object records, the block event dispatcher and door completion. ↩

  2. Pack and unpack a level record, the saved-level bits and the temporary-wall slots. Schemas: SAV.ksy and LEVELS_TMP.ksy. ↩

  3. The program start-up, which clears the saved-level bits and runs the start menu once before the game loop; the new-game start, write a level record, save a game and read a saved game. Reference files in the file inventory. ↩

  4. The level-change command, which calls settle the level, write a level record and the INF level loader; the map reader, read a level record, initial monster states, rebuild the block item chains, script timers and the step script limit. ↩

  5. Saved-level bits, the level change, the save writer and the tests of the INF level loader. ↩