Skip to main content

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 timed updates in the order the game checks them at each pass, with their interval in ticks and whether they are on at the start: blink the portrait being exchanged (inferred), every 9 ticks, off until an exchange; flying objects every 3; monster groups 0 to 3 every 20 until a level sets its own interval; character timers at the character's next deadline, off until a timer is set; doors every 5; teleporter and temporary wall animation every 10; food every 1080; monster idle animation (inferred) every 25. Then come the step-triggered script, the timed block scripts and the expiry of temporary walls.
Each pass walks the table in order, then runs the step-triggered script, the timed block scripts and the temporary walls.

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

EntryUpdateInterval (ticks)Initially
0Blink the portrait of the character being exchanged (inferred)9off; on during an exchange
1Advance every flying object3on
2–5Run monster update groups 0, 1, 2 and 320, or the level'son
6–11Check the timers of characters 1 to 6until the character's next deadlineoff; on when one is set
12Advance doors that are opening or closing by one step5on
13Animate the teleporters and temporary walls in view10on
14Food: each eligible character loses one point1080on
15Monster idle animation (inferred)25on

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:

  1. the step-triggered script;
  2. the timed block scripts;
  3. 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.

Footnotes​

  1. Timed callbacks over the timed-callback table, whose handlers are the flying-object mover, the door step and the other handlers; switched on and off by set a timed callback and set a character timer. Comparative source: REF-SCUMM-014, setupTimers and the timer handlers. ↩ ↩2

  2. Temporary walls and their slots. Comparative source: REF-SCUMM-014, updateWallOfForceTimers and destroyWallOfForce. ↩ ↩2

  3. Monster-timer loader, monster update and the MonsterRuntime initialization; the statistics come from the monster blocks of the 16 reference INF files. ↩ ↩2 ↩3

  4. Food handler and the two-slot item search. Comparative source: REF-SCUMM-014, timerUpdateFoodStatus. ↩