Item entry
ItemRuntime · 14-byte runtime structure
The engine in ten minutes · Items · ITEM.DAT format
In brief
Items are entries of one array of 600 fourteen-byte records, shared by the
items lying on any level and those carried by characters. The initial entries
are read from ITEM.DAT. Items lying in the same block
are linked into a circular chain that starts at the block's
LevelBlockRuntime entry.1
Layout
Every byte the game reads as a number is sign-extended: both name indices,
the icon, the type, the position, the level and the value. flags is only
tested and set bit by bit. The location and link words are only compared for
equality, so their sign never matters.
/* One item (14 bytes, little-endian). */
typedef struct ItemRuntime {
int8_t unidentified_name_index; /* Name shown before identification. */
int8_t identified_name_index; /* Name shown after identification. */
uint8_t flags; /* ItemRuntimeFlags; for wands, the low six bits count the charges. */
int8_t icon_raw; /* Icon; can be overridden when a script creates the item. */
int8_t type_raw; /* Item type; compared as a signed byte when removing items. */
int8_t position_raw; /* 0-3 floor quarter (0 NW, 1 NE, 2 SW, 3 SE); 4-7 the same quarter in flight; 8 a wall niche; 0 in a quiver. */
uint16_t location_raw; /* Block when lying or flying; 0 carried; -2 in a quiver or a free conjured-weapon entry; -1 unused entry. */
uint16_t previous_index; /* Previous item in the block's (or quiver's) circular chain. */
uint16_t next_index; /* Next item in the block's (or quiver's) circular chain. */
int8_t level_raw; /* Level of location_raw; 0 while carried; 255 (no level) for a negative location. */
int8_t value_raw; /* Meaning by ItemKind: enchantment, spell definition, potion, wand spell, food points...; sign-extended. */
} ItemRuntime;
There is no padding. Index 0 is the "no item" value of the chain words and of
a block's chain head. flags uses ItemRuntimeFlags.
The identification flag chooses which of the two name indices is displayed;
both address the name slots loaded from ITEM.DAT.2 type_raw selects the
shared ItemTypeRuntime entry loaded from
ITEMTYPE.DAT.3 For an
ITEM_TYPE_MAGE_SCROLL, value_raw is the mage spell
ID that the scroll teaches.4 The meaning of value_raw for each
ItemKind is described in
using items: the enchantment of
worn items and weapons, negative when cursed; the spell definition cast by a
scroll; the potion; the wand's spell; the food points of a ration; the text
of a parchment.
Locations
An entry does not record who carries it: location_raw and level_raw
only say whether it lies in the world. The states are listed in
where an item is: −1 for an unused
entry, a block for an item lying there or flying through it, 0 and 0 for an
item carried in an inventory slot or held, −2 and 255 for an item in a
quiver or a free conjured-weapon entry. A carried item is reached only
through the inventory slot, the quiver or the held-item word that refers to
it. New items are copies of a template made by the
duplication routine into the first
entry from 1 to 599 whose location_raw is −1; freeing an item writes −1
back.5
Block chains
The walks check nothing: a chain whose links never come back to its head is walked forever unless an item matches, and the game stops responding; a link of 0 leads to entry 0, whose own link is 0, with the same result. A link of 600 or more, or a negative one, addresses the game's data after (or before) the item table, read as item records; identifying then sets a bit in each of them.
A zero chain head means the block is empty. Otherwise the chain is walked until it returns to its first item. Three shared routines maintain it:1
| Routine | Effect |
|---|---|
| Insertion | Makes the item the new head and sets position_raw and location_raw; level_raw becomes the current level, or 255, no level, when the location is negative. |
| Unlinking | Removes the first item matching an index (or, with index -1, a position) and clears its location_raw, both links and level_raw. |
| Search | Returns the first item matching an index or a sign-extended type, or counts matches; it can skip items whose position_raw is 4–7. |
A chain is a pile: the insertion makes the new item the head, so the item
put down last is found first, and nothing limits the number of items. The
unlinking by position takes the first item whose position_raw equals the
position exactly, so the floor quarters 0–3, the flight positions 4–7 and
the niche position 8 are distinct. A separate routine
appends an item at the end of a chain,
and deleting items by type leaves the others in reverse order. A
character's quiver is a chain of the same kind whose head is inventory slot
16.1
SCRIPT_MOVE_PARTY_OR_OBJECT
uses all three routines and also writes level_raw and location_raw
directly for items on other levels.
The chains themselves are not saved. Whenever a level is loaded, the game
empties every block's chain and inserts again each item whose level_raw is
the new level and whose location_raw is positive, at its position_raw,
in the order of the table: the item with the highest index comes out on top
of each pile.
The whole array is saved with the game; see
level records and level changes.6
When items are removed by type, the routine compares type_raw as a signed
byte and writes the all-bits-set value into the location_raw of each
matching item.7 Scripts can also
create an item from a template,
optionally overriding its icon and value.8
Sources
Locations refer to the game executable reference; comparative sources are listed in the technical references.