Skip to main content

Magic routines

Routines · Data · Address index

Spell books, casting from the book or from an item, targets, spell effects and their timers, and spell animations. The routines page explains how the entries are written.

0x21a8d — End a spell's effect​

0x21f47 — Draw the spell-book page​

  • Range: 0x21f47–0x222c6, with 0x21e4f and 0x21f05
  • Status: the coordinates and colours of its drawing calls (0x21f4f–0x220ff, 0x2218a–0x2221c) are not read.
  • Role: Draws six tabs numbered 1 to 6 (x 0x44 + 18 i), the selected group being DS:0xcd16 (spell level − 1), then six lines: line i shows the name of slot 10 × group + DS:0xd0ab + i when its value is 0 or more and DS:0xd0ab + i < 9, so used slots and entry 9 are never shown, and an empty slot draws the empty name. A seventh row is ABORT SPELL (DS:0x57a5, DS:0x57b1 when selected). While bit 2 of the caster's +0x20 is set, 0x21f05 covers the lines with a grid of a shape; when slot 6 of the level is not ready, a shape (DS:0xe4ca) covers the page arrows, drawn by 0x21e4f. The line selected is DS:0x936a (0–5, 6 for the abort row). For the cleric book (DS:0xe4c6 = 1) a line's definition is the slot value + 33 (0x22c6a–0x22c9a).
  • Used by: Choosing a spell in the book › The book, Casting a spell

0x22326, 0x22428, 0x22ad1, 0x222c7 — Spell-book buttons​

  • Anomaly: ANOMALY-152: in 0x22ad1, the readiness test 0x22c07–0x22c0b is bypassed for argument 7, the Space button, at 0x22c0d–0x22c11; the selected slot is then read and passed to the cast with no test (0x22c6a–0x22ca4).
  • Role: The book's buttons (records from DS:0x4840, entries 1 to 16 of the array DS:0x50d0 that the button list links while the book is open). 0x22326, the six tabs and keys 1–6: sets the group, shows the first page and selects its first ready line, otherwise the abort row, and writes +0x151, +0x150 and +0x152 back (0x22378–0x2239e). 0x22428, a click on the arrows: swaps the page when slot 6 is ready or the second page is shown, selects line 0 and writes the position back (0x22461–0x22474). 0x22ad1, the six lines, keys , and . (codes 0x35, 0x36) and Space (0x3d); the two keys first test di at 0x22b10, set only when a slot is ready (0x22b05), so a value left by the caller (0x2fb7b) passes the test (ANOMALY-144): a click on a ready line selects it and, unless bit 2 of the caster's +0x20 is set (0x22c5c), casts it through 0x2252e (0x22ca4); the two keys move to the previous or next ready entry, wrapping through the abort row and turning the page; Space casts the selected line, or closes the book on the abort row (0x22ca9–0x22cc2); the position is written back at 0x22cd7–0x22cea. 0x222c7, ABORT SPELL and Escape: clears DS:0xe441, restores the background under the book and rebuilds the buttons. It is also called when the caster drops to 0 hit points or below (0x20aec), is paralyzed or petrified (0x20bbb), on a swap of characters, and from 0x1ed9b, reached on the party's defeat and on a level load.
  • Used by: Choosing a spell in the book › The book, Choosing a spell in the book, Casting a spell, Anomalies and bugs

0x20f40 — Sort a character's spell slots​

  • Role: Bubble-sorts each of the 16 groups of ten slots from +0x39 (both slot arrays) by a key: a ready ID itself, a used one − ID + 40, an empty slot 80; ready spells come first by ID, then used ones, then empty slots. The camp menu calls it for every character when it closes (overlay 247 0x44bf4) and at some points inside it (0x44b44).
  • Comparative source: REF-SCUMM-013, sortCharacterSpellList.
  • Used by: Casting a spell › Using the spell book

0x2252e — Cast a spell from the book​

0x227bc — Start a spell​

0x20247 — Recompute a character's defense​

0x20e64 — Check a hand item for a hand spell​

  • Range: 0x20e64–0x20e7e
  • Role: Returns 1 for the item entries 11 to 17 (unsigned bounds 0x20e6b and 0x20e70, cmp si,0x11; ja), the conjured-weapon items and entry 17, one beyond the 11 to 16 that the conjured-item search 0x21125 examines (0x21148) (ANOMALY-137); a hand holding one keeps a character from casting a spell with SPELL_FLAG_NEEDS_FREE_HAND, and Remove Curse leaves them alone.
  • Comparative source: REF-SCUMM-013, isMagicEffectItem.
  • Used by: Anomalies and bugs

0x22a90, 0x22485, 0x20fff — Cast a spell from an item​

  • Ranges: 0x22a90–0x22ad0; 0x22485–0x2252d; 0x20fff–0x2105c
  • Anomaly: the jump at 0x22951 in the spell start.
  • Role: 0x22a90(character, definition, slot) saves the caster (DS:0xd0b0) and the book shown (DS:0xe4c6) in DS:0xef46 and DS:0xef4a, sets DS:0x71c8 to slot + 1, makes the character the caster, shows the cleric book for a definition above 33 and calls the cast routine. There 0x22485 replaces the marking of a book slot: item types 26, 34, 35 and 58 are removed; for types 48 and 62, unless the value is 5, the item is removed when the six low bits of its flags are 1 and those bits are decremented otherwise; other types are kept. At the end of the start (0x22a32), and when the target prompt is cancelled, 0x20fff toggles the hand's bit of disabled_slots, sets an 18-tick timer with event hand + 2, redraws the portrait, restores the saved caster and book and clears DS:0x71c8.
  • Used by: Potions, scrolls and wands › Spells cast from items, Potions, scrolls and wands, Initial items (ITEM.DAT), Item state flags, Item types, Anomalies and bugs

0x215ff — Choose a spell's target​

0x21d16 — Open a spell book​

0x20ec1 — Start a spell timer​

  • Range: 0x20ec1–0x20f3f
  • Role: Arguments: definition number and the four parameter words base, unit, per level and update. Takes a level of the caster (DS:0xd0b0) from 0x21219 when DS:0xe4c6 is 1, otherwise from 0x211b0, and sets a character timer on the target (DS:0xcbc2) of unit × base + unit × (per level × level) ticks, with the definition number negated as event code and update as the last argument.
  • Comparative source: REF-SCUMM-013, setSpellEventTimer.
  • Used by: Lasting spells › The timer, Spell definition › How the game uses it, Magic

0x212c5 — Ask for a spell's target​

0x21385, 0x212dc, 0x2140a — Spell animations​

  • Status: the images are not examined.
  • Role: 0x21385(slot, or −1 for every character) draws eight steps over the portraits, redrawing each portrait in turn; a present character is drawn only if the character panel is closed or the character is selected. 0x212dc(slot, step) draws one step: the portrait, then up to four small shapes, each chosen by a 2-bit field of the byte DS:0x55dc[step] (0x55e4, 0x55e8), from the table at DS:0xcc34, placed by DS:0x55ec around the portrait's position (DS:0x55f4, DS:0x5600), or at a fixed place when the panel is open; 0x2140a saves part of the view and draws sixteen steps over it through slot 44 of the driver function table.
  • Comparative source: REF-SCUMM-013, sparkEffectDefensive and sparkEffectOffensive.
  • Used by: Casting a spell › The steps of a cast, The game clock › Pauses

0x22f37, 0x233b6 — Explosion and vortex animations​

  • Ranges: 0x22f37–0x233b5, 0x233b6–0x23912
  • Status: the bodies of the two routines apart from the counter stops (0x22f4a–0x2339f, 0x233b9–0x23902) are not read, so the images are not examined.
  • Role: 0x22cfb(object) runs the explosion routine for a flying object's block. 0x22f37 draws an explosion over the view; the explosion routine 0x22d19 calls it at 0x22f1c with one of three parameter sets. 0x22d19(block, …, flying object) first decides whether to draw: while the party rests it only plays sound 35 from the party's block (0x22d24–0x22d3b). Otherwise it looks for the block among the 18 visible blocks; when it is not there, nothing at all happens (0x22d72–0x22d77). The byte DS:0x56b7[position] (0, 0, 1, 2, 1, 0, 0, 0, 1, 2, 1, 0, 0, 2, 0, 0, 2, 0) then decides: 0 plays sound 35 from the party's block without a picture (0x22d86); 1 does the same when the object's direction and the party's are not on the same axis (0x22d9f–0x22dbe); otherwise the explosion is drawn and sound 35 played at once (0x22e9c). 0x233b6 draws the vortex of Cone of Cold, called by its start routine at 0x24252. Both stop the shared counter while they draw (0x22f43–0x233a3, 0x2348b–0x23906).
  • Comparative source: REF-SCUMM-022, explodeObject (drawExplosion) and spellCallback_start_coneOfCold (drawVortex).
  • Used by: The game clock › Pauses, Sound effects

0x23a72–0x24eca — Spell start and end routines​

  • Role: The routines named by the initial definition table, segment 0x13a7. "Hit" marks the second routine of a projectile spell, which 0x2166a copies into the flying object and the hit test calls with the object (0x27d4c); for the other spells the second routine runs when the effect ends.

    StartSecondSpell (definitions)What the routine does
    0x23a72—Dispel Magic (12, 44)0x2391b on every present character.
    0x23a9c—Improved Identify (9)Sets 0x40 in the item flags (DS:0x71cc + 14 × item) of both hand items of the target.
    0x23ad0—Armor (1)Byte +0x145 = mage level + 8; message DS:0x58e2 when the target's defense is already at most 6 + its dexterity modifier.
    0x23b33—Burning Hands (2)Six shapes (DS:0x5890, 0x5896, 0x589c), then 0x1b319 on the block ahead: 1d3 + 2 × level, category 4.
    0x23c0d0x23c2bDetect Magic (3, 37)Redraw the held item (0x18c77) and the cursor (0x33cb0); called at 0x22946, before the mask is set at 0x22978, and at 0x21b03, before it is cleared at 0x21b31.
    0x23c490x23c6a hitMagic Missile (4)Launch object type 0; hit (1, 4, 1) × max(1, (level − 1) >> 1).
    0x23c9d0x23d54Shocking Grasp (6)Conjured weapon of 1d8 + level in the free hand (0x21199); failure: DS:0x5912, ends definition 6, DS:0xdf38 = 1. End: frees the first conjured item of the hands.
    0x23d9b0x23dbd hitMelf's Acid Arrow (11)Type 1; hit (2, 4, 0) × max(1, level / 3), with a hit roll.
    0x23df20x23e14 hitFireball (13)Type 2; hit (1, 6, 0) × level on the whole block.
    0x23e430x23e65 hitHold Person (15, 41)Type 3; hit on monsters: effect 0, category 4; on the party: up to 1d4 characters (0x23eea), from slot 1d6, 1 to 6 (0x23efd), held for 5 units.
    0x23f6a0x23f8c hitLightning Bolt (17)Type 4; hit (1, 6, 0) × level; always reports a miss.
    0x23fbb0x24074Vampiric Touch (18)Conjured weapon of (level >> 1)d6; failure DS:0x5929. End: caps hit_points at +0x1d, then 0x23d54.
    0x2409b—Fear (19)Effect 6, category 4, on every monster of the block ahead.
    0x240f90x2411b hitIce Storm (20)Type 5; hit (1, 6, 0) × level, repeated on the four neighbours (DS:0x58a2) when it hit.
    0x241ac—Remove Curse (22)Inventory slots 0–26 except 16: items with flag 0x20 that are not conjured get 0x20 cleared and 0x40 set.
    0x2420f—Cone of Cold (23)Vortex (0x233b6); seven blocks from the cone offsets DS:0x58d2[dir]; 0x20808(level, 4, level, 0x41, category 4, effect 0).
    0x243040x24326 hitHold Monster (24)Type 6; effect 1, category 4.
    0x24440—Wall of Force (25)Temporary wall ahead.
    0x24595—Disintegrate (26)Effect 4, category 4, on the first monster in line; then the wall of force ahead.
    0x245f9—Flesh to Stone (27)Effect 5, category 4, on the nearest monster ahead; otherwise DS:0x594c.
    0x24661—Stone to Flesh (28)Clears status bit 0x08, otherwise 0x20e52.
    0x2467d0x2468cTrue Seeing (29, 57)Illusion walls hidden, then shown.
    0x2469b—Finger of Death (30), Slay Living (56)Effect 3, category 4; when saved (0), damage 2d8 + 1 with experience.
    0x24708—Power Word Stun (31)Effect 2, category 5, when the monster's current hit points (+0x0e) are below 90.
    0x2474e—Cause Light Wounds (35)0x21bfb(1, 8, 0).
    0x24766—Cure Light Wounds (36)Heal 1d8.
    0x2478a0x24839Aid (39)Fails on a dead or absent target; on an aided target (+0x148 > 0), message DS:0x5966 (0x247bb) and the Aid ends (0x24818–0x2482d); otherwise 1d8 into +0x148 and hit_points, uncapped, and bit 0x1000 of spell_effects. End: removes what is left of the bonus.
    0x2485f0x23d54Flame Blade (40)Conjured weapon of 1d4 + 4; failure DS:0x5997, but ends definition 6 and timer −6 (0x248e1–0x248f6).
    0x249090x24962Slow Poison (42)On a poisoned target: bit 0x2000 of spell_effects and a timer(1, 32760, cleric level from 0x24939, 1) at 0x24930–0x24951; otherwise 0x20e52, leaving the timer set at 0x22905. End: clears the bit.
    0x24978—Create Food (43)food = 100 for every present living character.
    0x249a8—Remove Paralysis (47)Clears status bit 0x04 on up to four characters.
    0x249e1—Cure Serious Wounds (49)Heal 2d8 + 1.
    0x24a05—Cause Serious Wounds (48)0x21bfb(2, 8, 1).
    0x24a1e—Neutralize Poison (50)0x20e7f on a poisoned target, otherwise 0x20e52.
    0x24a41—Cause Critical Wounds (52)0x21bfb(3, 8, 3).
    0x24a5a—Cure Critical Wounds (53)Heal 3d8 + 3.
    0x24a7e0x24aa0 hitFlame Strike (54)Type 7; hit (6, 8, 0) on the whole block.
    0x24abf—Raise Dead (55)When hit_points is −10 and race_and_sex >> 1 is not 1: hit_points = 1; otherwise 0x20e52.
    0x24af4—Harm (58)0x21bfb(−1, −1, −1): the monster's maximum hit points (+0x0c) minus 1d4.
    0x24b07—Heal (59)Heal up to the maximum hit points; otherwise 0x20e52.
    0x24b56—Turn undead (62)Turn undead.
    0x24e18—Lay on Hands (61)Heal 2 × the caster's byte +0x26.
    —0x24e3d hitMonster spell, type 8 (63)Hit (12, 10, 6, 0); while party effect bit 17 (DS:0x6dde & 2) is set, (4, 10, 6, 0) instead, and a hit clears the bit.
    —0x24ead hitMonster spell, type 9 (65)Hit (0, 0, 18, 0): 18 to each present character, halved by a saving throw.
    —0x24cda hitMonster spell, type 10 (66)On the party block: "The party has been hit by a death spell!" (DS:0x59c5); from the first spell target, up to 1d4 characters whose first class level (+0x26) is below 8 take 300 points of damage.
    —0x24c66 hitMonster spell, type 11 (67)On the party block: the first spell target takes 300 points of damage, with "%s has been disintegrated!" (DS:0x59a9), unless the ray saving throw with value 1 returns nonzero, which never happens. A saved target would be retried without moving on (0x24c91); unreachable.
    —0x24d51 hitMonster spell, type 12 (68)On the party block: the first spell target takes 3d8 + 3 with "%s has been hit by cause serious wounds." (DS:0x59f1). The dice are those of Cause Critical Wounds (0x24a41), not those of Cause Serious Wounds (2d8 + 1, 0x24a05).
    —0x24db3 hitMonster spell, type 13 (69)On the party block: from the first spell target, the first character for which the ray saving throw with value 2 returns 0 is petrified (category 5); otherwise the next target is tried (0x24e04). That saving throw always returns 1, so nobody is petrified.
  • Comparative source: REF-SCUMM-013, the spellCallback_start_* and spellCallback_end_* functions; REF-SCUMM-021, the spellCallback_end_monster_* functions.

  • Used by: Character conditions, Lasting spell effects, Attack spells › Notation, Saving-throw categories

0x21735 — Magic object hit​

0x211b0, 0x21219, 0x21199 — Caster levels and free hand​

  • Role: 0x211b0 returns a character's mage level, 0x21219 its cleric or paladin level, from the level bytes at +0x26; a paladin level above 8 counts as level − 8, otherwise 1. Both return 9 while DS:0x71c8 is set, and, for slot −1, 5 or 9 depending on the current level. 0x21199 returns the hand slot a conjured weapon goes to: 1 when the second hand is empty, otherwise 0.
  • Comparative source: REF-SCUMM-013, getMageLevel, getClericPaladinLevel.
  • Used by: Ranged attacks › Monster spells, Attack spells › Notation, Launch an object › Behavior

0x2391b — Remove all of a character's effects​

0x288e6 — Clear a character's timers with a given event​

  • Role: Clears every timer of the character whose event code is the one given, then re-arms the character's timer update.
  • Used by:

0x20e52, 0x19956 — Spell messages​

  • Role: 0x20e52 prints "no effect." (DS:0x5711); 0x19956 prints a warning and plays sound 79.
  • Comparative source: REF-SCUMM-013, printWarning.
  • Used by: Sound effects

0x20e7f — Neutralize poison on a character​

  • Role: Clears status bit 0x02 and SPELL_EFFECT_SLOW_POISON, clears the character's timers with event −34 (0x20ea9, 0x288e6), and redraws its portrait. −34 is the end event of definition 34, Bless, not of Slow Poison (42).
  • Used by: Lasting spell effects, Anomalies and bugs

0x1b6fd — Dexterity defense modifier​

0x21bfb — Cause wounds​

  • Role: Arguments: dice count, sides and bonus. Refuses with "%s must be in the front ranks to hit!" (DS:0x577e) when the byte of the caster's slot in DS:0x56a2 is 0. The four bytes there are 1, 1, 0, 0 and the next two bytes, read for slots 4 and 5, belong to the following table and are nonzero, so slots 4 and 5 may cast. Then takes the nearest monster ahead ("%s missed the monster.", DS:0x5766, when there is none), makes a hit roll (0x1f55c) and calls 0x20808 with damage flags 0x801, category 4 and effect 2, which keeps the full damage on a successful save. With −1 for all three, the damage is the monster's maximum hit points (+0x0c) minus 1d4.
  • Comparative source: REF-SCUMM-013, the Cause Wounds and Harm spell callbacks.
  • Used by: Attack spells › Notation, Anomalies and bugs

0x20808 — Damage a monster, with a saving throw​

  • Role: Arguments: monster, dice count, sides, bonus, damage flags, saving-throw category and effect. Computes the damage with 0x20662, which rolls the saving throw (category 5 never saves; effect 0 halves, effect 2 keeps the damage), then calls the monster damage with the experience flag 0x800 of the damage flags when the damage is positive.
  • Comparative source: REF-SCUMM-013, calcAndInflictMonsterDamage.
  • Callers: 0x1b3a0 (Burning Hands through 0x1b319, flags 0x21), 0x1eca2 (turn undead, 0x200), 0x1f91f (flying item, 0x110 or 0x910), 0x21882 (magic object hit, the definition's damage_flags), 0x21caa (cause wounds, 0x801) and 0x242db (Cone of Cold, 0x41); the melee attack calls 0x20662 directly (0x1fa9d, 0x100 or 0x101). No overlay calls either routine and no pointer to them is stored, so no damage of kind 0x0080 or 0x0400 is ever produced.
  • Used by: Attack spells › Notation, Damage to a monster › Kinds that never occur, Kinds of damage dealt to a monster

0x1b442, 0x1b299, 0x24395, 0x1b319 — Finding spell targets​

  • Role: 0x1b442 lists the monsters of a block hit at a position and direction, singly or the whole block. 0x1b299 finds the nearest monster ahead of a character: the centre spot first, then the spots of DS:0x30e6. 0x24395 follows the line ahead for up to the given number of blocks (0x1e36f) and stops at a wall without WALL_RUNTIME_PASSAGE (0x243db). 0x1b319 damages the monsters at the six positions of the table at file offset 0x2d340 for the party's direction; for north the entry is 2, 3, 4, 1, 2, 5, so position 0 is never hit and position 2 twice.
  • Comparative source: REF-SCUMM-013, findBlockMonsters, getClosestMonster, findSingleSpellTarget; the data-tool table kEoB2BurningHandsDestDOS.
  • Used by: Attack spells › Notation, Wall properties, Anomalies and bugs

0x1b242 — Monsters of a block by position​

  • Range: 0x1b242–0x1b298
  • Role: 0x1b242(array, block) fills six words with −1, then stores in the entry position the slot of each monster of the block. When two monsters share a position the later slot stays. 0x1b299 and 0x1b319 use it (0x1b2a9, 0x1b329).
  • Used by:

0x1b3ba — The monster a single-target object hits​

  • Range: 0x1b3ba–0x1b441
  • Role: 0x1b3ba(block, position, direction) searches the 30 monster slots for monsters of the block. One at exactly position is returned at once (0x1b405–0x1b409), even if an earlier slot already ranked in the table. Otherwise it keeps the monster whose position comes first in the row of the table for that direction and position, the earlier slot on a tie (0x1b40f–0x1b426), and returns −1 when the block has no monster. 0x1b442 calls it for an object with flag 0x01 and without 0x02 (0x1b4b3, flags read by the magic object hit at 0x2174c–0x2175b).
  • Used by: Attack spells › Projectiles

0x239e7, 0x23a36 — First and next spell target in the party​

  • Ranges: 0x239e7–0x23a35, 0x23a36–0x23a71
  • Role: 0x239e7 starts at slot 1d6, 1 to 6 (0x239eb–0x239fc), and returns the first slot passing eligibility mask 3, wrapping to 0 only after slot 6 (0x23a1e), within six tests counted in DS:0xef48; 0x23a36 continues from a given slot with the same count. Slot 6 lies past the six character records. Both return −1 when no slot passes. Used by Hold Person on the party and by the monster spell hits of types 10 to 13.
  • Comparative source: REF-SCUMM-021, findFirstCharacterSpellTarget and findNextCharacterSpellTarget.
  • Used by: Ranged attacks › Monster spells

0x20b26 — Afflict a character with a condition​

0x21d69, 0x21db1, 0x22646 — Spell names and slots​