Character record
CharacterRuntime · 345-byte runtime structure
The engine in ten minutes · The party · Magic
In brief
The six party characters are stored one after another in an array of records 345 bytes long. Its fields hold the attributes and alignment, the status bits and hit points tested by the eligibility checks, the class with the level and experience of each of its base classes, the lasting spell effects, the mage spells a character knows, its mage and cleric spell slots, the items it carries, its food, its timers and its portrait; nine unused bytes remain.1 The records of a new party are filled when the player creates the party; see initialization.
Layout
/* One party character (345 bytes, little-endian); partial view. */
typedef struct CharacterRuntime {
uint8_t unknown_00; /* 0x000: the slot number when written; never tested; see "Unused bytes". */
uint8_t status_flags; /* 0x001: CharacterStatusFlags. */
char name[11]; /* 0x002: zero-terminated name, printed in messages. */
uint8_t strength; /* 0x00d: gives the strength hit and damage modifiers and the chance to force a door. */
uint8_t strength_max; /* 0x00e: copied into strength by "remove all effects". */
uint8_t exceptional_strength; /* 0x00f: percentage of an 18 strength; replaces the modifiers when nonzero. */
uint8_t exceptional_strength_max; /* 0x010: second byte of exceptional_strength. */
uint8_t intelligence; /* 0x011: limits at creation and the character sheet; name inferred. */
uint8_t intelligence_max; /* 0x012: second byte of intelligence. */
uint8_t wisdom; /* 0x013: gives the bonus cleric spell slots. */
uint8_t wisdom_max; /* 0x014: second byte of wisdom. */
uint8_t dexterity; /* 0x015: gives the defense and missile modifiers. */
uint8_t dexterity_max; /* 0x016: second byte of dexterity. */
uint8_t constitution; /* 0x017: hit points per level and saving throws. */
uint8_t constitution_max; /* 0x018: second byte of constitution. */
uint8_t charisma; /* 0x019: limits at creation and the character sheet; name inferred. */
uint8_t charisma_max; /* 0x01a: second byte of charisma. */
int16_t hit_points; /* 0x01b: current hit points, signed; compared with 0 and -10. */
int16_t hit_points_max; /* 0x01d: healing does not go above it. */
uint8_t defense; /* 0x01f: recomputed from dexterity, worn armour, shields, bracers and rings; lower is better. */
uint8_t disabled_slots; /* 0x020: bit n toggled when hand n is used, bit 2 after casting. */
uint8_t race_and_sex; /* 0x021: 2 x CharacterRace + 1 for a female character. */
uint8_t class_index; /* 0x022: index of the CharacterClassMask table. */
uint8_t alignment; /* 0x023: 0-8, index of the alignment name; restricts the creation menus. */
int8_t portrait; /* 0x024: portrait number; >= 0 CHARGENA image, < 0 OUTPORTS image. */
uint8_t food; /* 0x025: 100 for a new party; decreased by the food update; 0 stops it. */
uint8_t levels[3]; /* 0x026: level in each class, read for the caster levels. */
uint32_t experience[3]; /* 0x029: experience of each class, same order as levels. */
uint32_t face_image; /* 0x035: far pointer to the portrait image in memory. */
int8_t mage_spell_slots[80]; /* 0x039: eight groups of ten mage spell entries. */
int8_t cleric_spell_slots[80]; /* 0x089: eight groups of ten cleric spell entries. */
uint32_t known_mage_spells; /* 0x0d9: bit i set = mage spell ID i + 1 is known. */
uint16_t inventory[27]; /* 0x0dd: ItemRuntime indices, zero empty; see "Inventory slots" below. */
uint32_t timer_deadlines[10]; /* 0x113: counter deadlines; 0 = unused. */
int8_t timer_events[10]; /* 0x13b: event code of each deadline; negative = a lasting spell. */
int8_t armor_spell_points; /* 0x145: what is left of Armor; damage reduces it, Armor ends at 0. */
uint8_t unknown_146; /* 0x146: never set; read only by the portrait frame; see "Unused bytes". */
uint8_t unknown_147; /* 0x147: only cleared; never read. */
int8_t aid_bonus_points; /* 0x148: the hit points Aid added that damage has not taken back. */
uint32_t spell_effects; /* 0x149: SpellEffectFlags of the spells active on the character. */
uint8_t last_damage; /* 0x14d: low byte of the last damage inflicted. */
int8_t slot_status[2]; /* 0x14e: result of the last use of each hand, shown on the portrait. */
uint8_t book_entry; /* 0x150: spell-book entry last selected. */
uint8_t book_group; /* 0x151: spell-book group last selected. */
uint8_t book_offset; /* 0x152: 0 or 6, the book page offset last used. */
uint8_t unknown_153[6]; /* 0x153: never accessed, only copied with the record. */
} CharacterRuntime;
There is no padding.
Attributes
The names intelligence and charisma are inferred from the order in which
the character sheet prints the attributes and from a comparative source. The
_max names of the second bytes are descriptive: the second bytes hold the
character's normal values, not limits.
Each of the six attributes is followed by a second byte. At the end of party
setup the game copies each attribute into its second byte, and
hit_points into hit_points_max; removing all magical effects copies
strength_max back into strength, and so does the end of giant strength.
Only strength_max and exceptional_strength_max are ever read; no code
compares an attribute with its second byte, and the other five second bytes
are never read. The character sheet prints
alignment, the strength and the five other attributes; what each attribute
changes in play is described under
attributes. The class's levels and
experience follow the class components, in
the same order.2
Status, hit points and class
status_flagsandhit_pointsare what the eligibility checks test: a character is present, not dead or conscious according to them. Damage subtracts fromhit_points; at −10 or less the death path keeps only the lowest bit ofstatus_flagsand clearsfood; see damage characters.3status_flagsuses only its four low bits, theCharacterStatusFlags; no code sets or tests bits0x10to0x80.4race_and_sexholds the race in its upper bits andCHARACTER_SEX_FEMALEin its lowest bit.class_indexselects the character'sCharacterClassMask, and scripts can look for a character by race orportrait; see condition expression tokens.spell_effectsholds theSpellEffectFlagsthat the spells cast on the character set while they last.5- The spells use
levelsfor the caster's level, heal up tohit_points_max, and keep the reserves of Armor and Aid inarmor_spell_pointsandaid_bonus_points. constitutionandrace_and_sextake part in saving throws;defensereceives the defense adjustment of an equipped item type.
Inventory slots
inventory holds 27 item indices: hands 0 and 1, backpack 2 to 15, the
quiver 16, body armour 17, bracers 18, helmet 19, neck 20, boots 21, belt
22 to 24 and rings 25 and 26. Slot 16 is the head of a chain of arrows, not
a single item. Which items each slot accepts, and how they are moved, is
described in the inventory.
When a character's spell book is opened, book_entry, book_group and
book_offset give the entry selected first.6
Spell slots
A slot holds a spell ID, read as a signed byte:
| Value | Meaning |
|---|---|
| 0 | Empty slot. |
| positive | A spell ready to be cast from the spell book. |
| negative | A spell chosen in the memorize or pray menu and not yet gained, or cast since; resting turns it positive. |
The list builder counts entries by absolute ID and separately counts the positive ones. A used spell becomes positive again only while the party rests, one slot at a time, for a character that carries a holy symbol or a spellbook; see resting. Resting also casts healers' cure spells, which marks their slots used, and writes Lay on Hands into a paladin's first ten cleric slots.7
Each array holds eight groups of ten slots, one group per spell level:
spell level g, the spell's group in the mage and cleric spells,
uses entries 10 (g − 1) to 10 (g − 1) + 9. Only the
memorize and pray menus fill the
slots: when the player leaves them, the array is rewritten level by level,
in the order of the spell IDs, ready copies before the others. They and
the setup of a new party put 29, the cleric ID of Turn undead, into
entry 0 of cleric_spell_slots (offset 0x89), and the menus skip that
value when they count the slots, in the mage array too (see
the True Seeing bug). Resting makes
the negative slots positive; casting makes a positive slot negative.8
Known mage spells
known_mage_spells is separate from the slots. When a mage copies a scroll,
the game checks the spell's bit and sets it once the spell is chosen; see
learning from scrolls. Spell names
are displayed through the separate SpellNamePointer
tables.9
Character timers
Each character has ten timer pairs: a deadline in shared-counter ticks and an event code. Setting one stores the counter plus a duration, replacing a pending timer with the same code, and makes the character's entry in the game's timed updates come back at the earliest deadline; character timers gives the rules and what sets each code. A negative code − n belongs to a lasting spell: when its deadline passes, the effect of spell definition n ends. The other codes do the following when their deadline passes:10
| Code | Effect |
|---|---|
| 0, 1 | Hand 0 for code 0, hand 1 for code 1: its bit of disabled_slots is toggled, its slot_status is cleared and the portrait is redrawn. |
| 2, 3 | The same hand's slot_status is cleared and the portrait redrawn; code 4 or 5 is then set for that hand, 9 ticks later under SPELL_EFFECT_HASTE, 36 otherwise. |
| 4, 5 | As codes 0 and 1. |
| 6 | last_damage is cleared and the portrait redrawn; each damage to the character sets this code 18 ticks ahead. |
| 7 | "name no longer has giant strength.": strength and exceptional_strength take back the values of their second bytes. |
| 8 | Poison: while the character has CHARACTER_STATUS_POISONED, 5 points of damage, none under SPELL_EFFECT_SLOW_POISON, then the same code again 546 ticks later; see effects on characters. |
| 9 | If the character is still paralyzed: "name is no longer paralyzed!" and CHARACTER_STATUS_PARALYZED is cleared. |
| 10 | Nothing; no caller sets it. |
| 11 | Bit 2 of disabled_slots is cleared, and the spell book is redrawn when it is open for the character: the book, unusable since a spell was cast from it 72 ticks earlier, can be used again. |
| 12 | SPELL_EFFECT_HASTE is cleared: "name slows down." |
The status bits are CharacterStatusFlags.
A saved game stores each timer as the number of ticks it has left (1 for a timer already due, 0 for an unused one). Loading adds the current counter back and schedules the character's entry for its earliest timer; see loading, failures and the end of the game.11
Portrait
When a game is loaded, the portrait of each character is drawn again from
portrait: a number of 0 or more selects an image of CHARGENA.CPS, a
negative number − n − 1 selects image n of OUTPORTS.CPS. The image goes
to the memory addressed by face_image, which is a run-time pointer: loading
keeps the pointer already in memory and ignores the saved one.11
The food byte is decreased by one every 1080 ticks for each present character whose food is not zero and whose hit points are above −10; see food.12
Initialization
The records of a new party are written in two stages, described under creating the party:13
- When a character is created:
race_and_sex,class_index,alignment,nameandportraitfrom the player's choices; every attribute and its second byte from the roll;levelsfrom the class; all threeexperiencetotals, also those of unused components;defense;hit_pointsandhit_points_max. Modifying the character changes only the first byte of each attribute. - When the party enters the game:
status_flagsset toCHARACTER_STATUS_PRESENT,foodto 100 andunknown_00to the slot number; starting items ininventory;known_mage_spellsfor mages and Turn undead incleric_spell_slotsfor clerics and paladins; the attributes andhit_pointscopied again into their second bytes andhit_points_max;defenserecomputed with the items.
A transferred party and a recruited character are written in other ways, described on those pages.
Unused bytes
Every access to these offsets found in the resident code and the overlays is listed below. An access through a record pointer received as an argument could escape the search for byte 0; none is known.
Nine bytes take no part in the examined game logic. They are saved and loaded with the rest of the record, and copied with it when two characters swap places or a companion joins; the six prepared companion records stored in the executable hold zero in all of them.4
| Offset | Field | What the code does with it |
|---|---|---|
0x000 | unknown_00 | Written with the slot number: for slots 1 to 5 when the game starts, and for slots 0 to 3 when a new party enters the game. A party transfer copies it from the Eye of the Beholder I record and keeps it when it clears a record. No code bases a decision on it, and nothing writes it again when characters change places, so it does not follow the slot. |
0x146 | unknown_146 | Only cleared, when all of a character's effects are removed. The portrait frame of protective spells is also drawn when it is nonzero, but no code gives it another value. |
0x147 | unknown_147 | Only cleared, with 0x146. |
0x153–0x158 | unknown_153 | Never read or written on their own. |
A reimplementation can keep these bytes only to preserve the record and saved-game layout.
Sources
Locations refer to the game executable reference.