Skip to main content

Level block

LevelBlockRuntime · 10-byte runtime structure

The engine in ten minutes · The world · Scripts

In brief​

A level is a grid of 32 × 32 blocks, kept in memory as an array of 1,024 ten-byte entries. Each entry combines what the level files say about a block — its four walls and its script — with what changes during play: occupancy, event bits and the items lying there. It is not an on-disk record.1

Layout​

/* One block of the current level (10 bytes, little-endian). */
typedef struct LevelBlockRuntime {
uint8_t wall_references[4]; /* Indexed by Direction: the face turned north, east, south, west. */
uint16_t state; /* Occupancy count (low three bits) and event-enabling bits. */
uint16_t item_chain_head; /* First item lying in the block; zero when there is none. */
uint16_t script_offset; /* Offset of the block's script; zero disables dispatch. */
} LevelBlockRuntime;

There is no padding. Selected state bits are described by LevelBlockStateFlags. The item chain links ItemRuntime entries, as used by scripted identification.2

From the files to memory​

On the examined loading path:1

  1. the whole table is cleared, then the six header bytes of the MAZ file are read into the first entry, that of [00:00];
  2. the four bytes of each MAZ cell are copied into wall_references, or come from the level's record when it has one; the other fields are not written;
  3. for each decoded INF cell binding, the binding's flags are ORed into state and its reference is copied into script_offset.

Anomaly

The map header ends up in the state of [00:00] (ANOMALY-64). The map reader uses the block table as the destination of the six header bytes. The copy of the cells then overwrites the four wall bytes of [00:00], but not its state, which keeps the header's third number: 4 in every original file, the cell size. Each load therefore starts [00:00] with an occupancy count of 4 instead of 0, before the bindings and the monsters are added, so its count carries into the event bits sooner (see block state flags). No consequence in play has been found: [00:00] only receives the step-count event, which needs no state bit, and monster-entry events need a bit that a count of this size never reaches. A faithful reimplementation sets the state of [00:00] from the header's third word before applying the bindings.

When an event reaches a block, the dispatcher adds a nonzero script_offset to the base of the level's script buffer and starts reading commands there. See monster movement events for the ordering and conditions.

Changes during play​

Monster placement updates the occupancy bits. Scripts can change wall_references, either all four sides or one side; see scripted wall assignment and scripted wall toggling.

Only wall_references is kept in a level's record when the party leaves the level or saves the game. When the level is loaded again, state and script_offset come back from the INF bindings, the item chains are rebuilt from the item table, and one is added to state for each of the 30 monster slots in the block of that slot; see level records.3

Sources​

Locations refer to the game executable reference.

Footnotes​

  1. The array is the level block table. Clearing, header read and map copy in the map reader; bindings applied by copy INF scripts and cell bindings; occupancy updated by place a monster on a block; script lookup in the block event dispatcher. ↩ ↩2

  2. Mark a chain's items identified, called by the identify-items command. ↩

  3. Pack and unpack a level record and rebuild the block item chains. ↩