Skip to main content

Character timers

The engine · Timed updates · CharacterRuntime

In brief​

Each character has ten timers. A timer is a deadline of the shared counter and an event code; when the deadline passes, the event runs: a hand becomes usable again, a spell or a potion's effect ends, poison strikes again, paralysis wears off. Setting a code that is already pending restarts it. One timed update per character checks its timers; it sleeps until the character's earliest deadline.

Setting and running a timer​

The character record lists what each code does when its deadline passes. A timer is set with a duration in ticks and a code:

  1. If the character's timed update (entries 6 to 11 of the timed updates) is off, the timer takes the first pair, and the update is switched on for its deadline.
  2. Otherwise the update is brought forward if the new deadline is earlier. The timer then replaces the pair that holds the same code, or takes the first free pair.
How a character timer is set. If the character's update is off, pair 0 takes the timer and the update is switched on for its deadline. Otherwise the update is brought forward if the new deadline is earlier; then a pair that holds the same code is replaced, so that timer restarts; failing that, the first free pair takes it; with no free pair, the timer is written before the first pair, which is a bug.
A timer with a code already held restarts; a new one takes a free pair.
set_timer(character, duration, code):
deadline = counter + duration
update = timed_updates[6 + character]
if not update.enabled:
pair[0] = (deadline, code)
update.deadline = deadline
update.enabled = true
return
update.deadline = min(update.deadline, deadline)
i = the pair holding code, else the first free pair, else -1
pair[i] = (deadline, code)

A code is thus never pending twice for a character: setting it again restarts it. The routine also has a mode that only takes a free pair, which no caller uses.

When the update runs, it ends every timer whose deadline the counter has reached, in pair order, freeing the pair before running its event. It then schedules itself for the earliest deadline left, or switches itself off when none is left. A timer therefore ends at the first pass of the game loop after its deadline.1

The timers the game sets​

CodeSet whenDuration (ticks)
− na lasting spell with definition n starts on the characterthe spell's duration
0, 1a use of hand 0 or 1 ends in CAN'T REACH, NO AMMO or BASH18
2, 3any other use of hand 0 or 1, including a spell cast from the item it holds18
4, 5code 2 or 3 ends36, or 9 under SPELL_EFFECT_HASTE
6the character takes damage18
7the character gains giant strength546 × (1d4 + 4)
8the character is poisoned, then at each dose546
9the character is paralyzed2 730
11the character casts a spell from the spell book72
12the character gains SPELL_EFFECT_HASTE other than from the spell546 × (1d4 + 4)

Code 10 is never set. A hand is therefore usable again 54 ticks after it was used, 27 under Haste, or 18 ticks after a CAN'T REACH, NO AMMO or BASH result; see when the hand is ready again. Codes 7 and 12 are set by the potions of giant strength and speed. Dispel Magic and petrification clear all ten timers of a character at once; see the anomaly.1

Bug

When a character's ten timers are all in use and none holds the code being set, the new timer is written just before the first pair. Its deadline replaces the contents of the two ring slots of the inventory: the first ring slot receives the low half of the deadline, the second its high half. The rings leave the inventory without being put anywhere else. The first slot then holds a number that is usually beyond the 600 items. Once the counter has passed 65,535 ticks, about an hour of play, the second slot names another item of the game. The new timer's code becomes the top byte of the tenth deadline, which postpones the tenth timer by about 256 hours per unit of the code (about 2,800 hours for code 11), unless the code is 0. The new timer itself is in no pair and never runs, so the spell or condition it should end goes on. For code 11, the spell book's wait does not end with it (what ends it then is unknown). A faithful reimplementation must reproduce these writes.1

Sources​

Locations refer to the game executable reference; comparative sources are listed in the technical references.

Footnotes​

  1. Set a character timer, which lists the callers; the timer handler; end a spell's effect; remove all effects. Comparative source: REF-SCUMM-022, setCharEventTimer, timerSpecialCharacterUpdate and castSpell. ↩ ↩2 ↩3