Spell definition
SpellDefinitionRuntime · 25-byte runtime structure
The engine in ten minutes · Magic · Mage and cleric spells
In brief
Besides its display name, each spell has a definition: its full name, the routine that carries it out, flags that choose which steps of casting apply, the sound it plays, how long it lasts, and the effects it leaves on a character or on the party. The definitions form one table of 25-byte entries, built into the executable. Mage spell n uses definition n; cleric spell n uses definition n + 33.1
Layout
/* One spell definition (25 bytes, little-endian). */
typedef struct SpellDefinitionRuntime {
uint16_t name_offset; /* 0x00: full name, far pointer: offset ... */
uint16_t name_segment; /* 0x02: ... and segment. */
uint16_t handler_offset; /* 0x04: routine run when the spell starts; zero if none. */
uint16_t handler_segment; /* 0x06 */
uint16_t flags; /* 0x08: SpellDefinitionFlags. */
uint16_t timer_offset; /* 0x0a: far pointer to the SpellTimerParameters ... */
uint16_t timer_segment; /* 0x0c: ... read with SPELL_FLAG_TIMED. */
uint16_t end_offset; /* 0x0e: optional routine run when the spell ends. */
uint16_t end_segment; /* 0x10 */
uint8_t sound_effect; /* 0x12: sound effect played when the spell starts. */
uint32_t effect_mask; /* 0x13: SpellEffectFlags applied to characters or to the party. */
uint16_t damage_flags; /* 0x17: MonsterDamageFlags of the spell's hits. */
} SpellDefinitionRuntime;
/* The four words a timed spell's timer pointer addresses. */
typedef struct SpellTimerParameters {
int16_t base; /* Units of duration whatever the caster's level. */
int16_t unit; /* Ticks per unit: 546, or 32760 for Slow Poison. */
int16_t per_level; /* Units added per level of the caster. */
int16_t update; /* Passed to the character timer; 1 in every definition. */
} SpellTimerParameters;
There is no padding. Far pointers are an offset followed by a segment; the segments are relocated when DOS loads the program, so they are not fixed addresses. Entries 0 and 33 are empty.
How the game uses it
The casting steps read the fields in this order:2
- Casting from the book records the definition number as the last spell
cast and checks
flagsfor a free hand and for the target. - Starting the spell copies the start routine, plays
sound_effect, checks whether the effect is already active, starts a timer from theSpellTimerParameterswithSPELL_FLAG_TIMED, calls the start routine, and setseffect_mask, a set ofSpellEffectFlags, where the flags say. It then sendsBLOCK_EVENT_SPELL_CASTto the party's block, so a level script can react; seeBlockEventMask. - When the timer expires, or when another path ends the effect, the end
routine is called if not null and
effect_maskis cleared again.3
A timed spell lasts unit × (base + per_level × level) ticks, the level
being the caster's; see lasting spells.
damage_flags is a set of MonsterDamageFlags:
MONSTER_DAMAGE_MAGIC on the 19 spells of the initial table that attack,
plus MONSTER_DAMAGE_FIRE, MONSTER_DAMAGE_COLD or
MONSTER_DAMAGE_ELECTRICITY on some of them, and 0x0010 on Melf's Acid
Arrow. The magic object hit passes it
to the damage calculation.
No code writes the table: every reference to its segment in the
executable is a read, and no pointer to it is kept in the data segment. Its
contents are therefore the same for the whole game, and a saved game does
not hold them; see where spell state lives.
A null start routine is skipped: the other steps still run, and the timed or
masked spells without one act only through their flags and effect_mask.
Entries 32 and 60 are zero after their name. The mage and cleric spells lists the data of every
named spell.
Besides casting, three other places read the table: a monster's spell
plays the definition's sound_effect, the name of a mage or cleric scroll is
taken from a definition's full name, and a magic flying object copies
end_offset and end_segment, its hit routine, from the definition when
it is launched.4
Sources
Locations refer to the game executable reference.