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:
- 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.
- 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.
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
| Code | Set when | Duration (ticks) |
|---|---|---|
| − n | a lasting spell with definition n starts on the character | the spell's duration |
| 0, 1 | a use of hand 0 or 1 ends in CAN'T REACH, NO AMMO or BASH | 18 |
| 2, 3 | any other use of hand 0 or 1, including a spell cast from the item it holds | 18 |
| 4, 5 | code 2 or 3 ends | 36, or 9 under SPELL_EFFECT_HASTE |
| 6 | the character takes damage | 18 |
| 7 | the character gains giant strength | 546 × (1d4 + 4) |
| 8 | the character is poisoned, then at each dose | 546 |
| 9 | the character is paralyzed | 2 730 |
| 11 | the character casts a spell from the spell book | 72 |
| 12 | the character gains SPELL_EFFECT_HASTE other than from the spell | 546 × (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
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.