Timed updates
The engine · The game clock · The game loop
In brief
Everything that happens on its own, such as monsters acting, flying objects and doors moving or food running out, is driven by a fixed table of sixteen timed updates. Each has its own interval in ticks; once per pass of the game loop, the game runs every update whose deadline has passed, in table order. Three checks follow: the step-triggered script, the timed block scripts and the expiry of temporary walls. A late pass delays an update; it never runs it twice.
The table of timed updates
The game keeps sixteen periodic updates in a fixed table. Each has an interval in counter ticks, a deadline and an on/off switch. In every pass of the game loop, the game walks the table in table order:
for entry in timed_updates: # entries 0 to 15
if entry.enabled and counter > entry.deadline:
entry.handler(entry.argument)
if entry.enabled: # the handler may switch itself off
entry.deadline = counter + entry.interval
As with the timed block scripts, the next deadline counts from the moment the update ran, so a late pass delays an update and never runs it twice.1
| Entry | Update | Interval (ticks) | Initially |
|---|---|---|---|
| 0 | Blink the portrait of the character being exchanged (inferred) | 9 | off; on during an exchange |
| 1 | Advance every flying object | 3 | on |
| 2–5 | Run monster update groups 0, 1, 2 and 3 | 20, or the level's | on |
| 6–11 | Check the timers of characters 1 to 6 | until the character's next deadline | off; on when one is set |
| 12 | Advance doors that are opening or closing by one step | 5 | on |
| 13 | Animate the teleporters and temporary walls in view | 10 | on |
| 14 | Food: each eligible character loses one point | 1080 | on |
| 15 | Monster idle animation (inferred) | 25 | on |
The executable's data gives monster groups 0–3 first deadlines of 0, 7, 14 and 14; loading a level with monsters replaces them, as described under monster update cadence.1 The character entries never use their interval of 50: setting a timer and each run of the entry reschedule it for the character's earliest deadline; see character timers.
Once the table is done, three more checks follow, in this order:
- the step-triggered script;
- the timed block scripts;
- the temporary walls: each one whose deadline the counter has strictly passed is removed (its four wall bytes are reset to 0) and the view is marked for redrawing.2
Temporary walls
A temporary wall is created on the block ahead of the party when that block
has no walls and no monster (its
LEVEL_BLOCK_OCCUPANCY_MASK count is
zero); otherwise the spell fails. Its four walls become
WALL_REFERENCE_TEMPORARY_WALL, and it lasts 546 + 273 × the caster's mage
level ticks. At most five exist at once; creating a sixth removes the one
that expires first. The start routine of the Wall of Force spell creates
them.2
Monster update cadence
Each monster belongs to an update group, set when its slot is initialized:
two times its INF unit, plus one for an odd slot. Each INF timer entry v
of a level sets the interval of both groups 2 v and 2 v + 1, so a unit's
monsters are split between two timers with the same interval, one for even
and one for odd slots.3
Loading a level whose INF file has a monster block, from the files or from its record, also restarts the four groups: the even groups 0 and 2 next run one interval later, the odd groups 1 and 3 half an interval later, so even and odd slots of a unit take turns. On level 10, which has no monster block, the groups keep the intervals and deadlines they had. The interval byte is read as signed: a value of 128 or more gives a negative interval. An odd group's first run then lies about 2^31 ticks ahead, more than three years. An even group's next run is the counter plus the negative interval: once the counter has reached the interval's size, that deadline always lies behind the counter and the group runs at every pass; while the counter is still below it, the deadline wraps to about 2^32 ticks ahead and the group does not run either. The original levels use none.3
In the 16 reference levels, only units 0 and 1 occur, and their intervals are 15, 25 or 35 ticks; level 10 has no monster block.3
Food
Every 1080 ticks, about a minute, each character that is present, has food left and has more than −10 hit points loses one point of food. The character panel shows it as a bar of 51 × 5 pixels out of 100, in colour 3, in colour 5 below 33 and in colour 8 below 20 (bars).
The update calls the same two-slot item search that script damage uses, with
item type ITEM_TYPE_RING and value 2, but skips a
character only when the search returns a negative value, which it never
does. The search therefore never spares a character.4
Resting does not run this update. It moves the counter forward by 32 760 ticks per rested hour, and its own food step never consumes food; see the anomaly.
Sources
Locations refer to the game executable reference; comparative sources are listed in the technical references.