Casting a spell
Magic · Lasting spells · Choosing a spell
In brief
A spell is cast from the spell book, which a spellbook or a holy symbol opens. Casting uses the slot at once, asks for a target when the spell needs one, plays its sound and animation, starts its timer, runs its own routine and sets its lasting effect; the book then rests for 72 ticks and the party's block receives a spell-cast event that level scripts can test.
Using the spell book
Using a spellbook opens the mage spell book, using a holy symbol the cleric one; see using items. How the book is laid out and how the player chooses a spell in it are described under choosing a spell in the book.
Casting from the book uses the slot at once: the slots after it in the level move up one place, and the spell goes, as a used slot, to the ninth place. The ready spells thus stay at the front of the level. If the line now selected holds no ready spell, the selection moves up one line, and after the last ready spell of a level it reaches ABORT SPELL; the book stays open. For the next 72 ticks the lines are covered and a click casts nothing, but tabs and pages still work. Turn undead uses no slot.1
The game also sorts the slots of every character, level by level, when the camp menu closes and at some points inside it: ready spells first by ID, then used ones by ID, then the empty slots.
The book closes by itself when the caster falls to 0 hit points or below, is paralyzed or petrified, when the player swaps two characters, and when a level is loaded or the party is defeated.
A spell from the book stopped by its own routine skips the end of the
cast (ANOMALY-69). Only the three conjured
weapons stop themselves, when Shocking Grasp, Vampiric Touch or Flame
Blade cannot make its weapon ("The spell fails!"). The slot is then
already used and moved, and the spell's lasting timer started, but the
book's 72-tick wait, the redraw of the page and the spell-cast event are all
skipped. The page then still shows the old list while the slots have moved,
so a click can cast the spell that moved into that line, without the wait
(inferred). Spells cast from items
have the same gap. A faithful reimplementation keeps the slot spent and skips the same
steps.
The steps of a cast
What each spell's own start and end routines do is not covered here; see the attack spells.
When a character casts a spell from the spell book, the game goes through
these steps; the flags are SpellDefinitionFlags
of the spell's definition:2
- Casting. The definition number is recorded as the last spell cast,
which scripts can test with
SCRIPT_EXPR_LAST_CAST_SPELL.- With
SPELL_FLAG_ENDS_INVISIBILITY, a caster withSPELL_EFFECT_INVISIBILITYbecomes visible: the Invisibility effect ends, with its message. - With
SPELL_FLAG_NEEDS_FREE_HAND, the spell is refused when both hands are full ("You must have a free hand for this spell.") or when a hand already holds an item of that kind ("You can't have two of this spell type active."). - The spell's slot is marked used, except for Turn undead. The game prints "caster casts spell."
- With
SPELL_FLAG_CHOOSE_TARGETit asks "Cast spell on which character?" and waits; otherwise the caster is the target.
- With
- Start. The definition's sound effect plays, and an animation runs on
the target's portrait, on every portrait or over the view.
- The spell is refused when its effect is already active: "name is already under the effect of a spell spell." for the target, "The party is already under the effect of a spell spell." for the party.
- With
SPELL_FLAG_TIMED, a timer is started on the target (see lasting spells). - The spell's own start routine runs; it can stop the start here.
- The lasting effect is set: on the target, on the party, or on every living party member.
- The target's defense is recomputed with
SPELL_FLAG_RECALCULATE_DEFENSE, and the portraits are redrawn.
- Afterwards. For a spell from the book, the caster's spell book
cannot be used for 72 ticks, about four seconds, until a
character timer ends the wait. The game then
sends
BLOCK_EVENT_SPELL_CASTto the party's block, so a level script can react; see events that start scripts.
Sources
Locations refer to the game executable reference.