Skip to main content

The game clock

The engine · Timed updates · Character timers · Timed scripts

In brief​

The game counts time in ticks of a shared counter, which the PC's timer interrupt advances about 18.2 times per second: a tick lasts about 55 ms. Every deadline of the game, for timed updates, character timers, timed block scripts and temporary walls, is a value of this counter. The game pauses it while the camp menu is open, while a level loads and during some animations; everything that waits on the counter waits with it. A second counter never stops, and the game times its waits with it.

Time survives level changes and saved games only partly. Character timers and temporary walls keep the time they have left; the timed block scripts and the step counter start again whenever a level is loaded; the counter itself and the table of timed updates are not saved. See what persists.

Schematic of the two tick counters against real time. The free-running counter rises steadily all the time. The shared counter rises with it during play but stays flat while the camp menu is open and while a level loads; every deadline is a value of the shared counter, so timed updates, character timers and timed scripts wait during these pauses.
Every deadline is a value of the shared counter, so everything that waits on it stops during a pause.

Ticks and the two counters​

The shared counter is a 32-bit count of ticks. The timer interrupt that the BIOS raises on each tick of the PC's system timer adds one to it.1

The sound drivers make the system timer run faster for their music, but pass only one interrupt in 48, or one in 16, on to the BIOS, which keeps the rate of the ticks:

Sound driverSystem timerPassed on to the BIOSTicks per second
None18.2065 Hzevery interrupt18.2065
Driver 1, which uses the AdLib ports874.1 Hzone in 4818.2109
Driver 2, the PC speaker; driver 3, the Tandy chip291.3 Hzone in 1618.2065

The rates are computed from the divisors the code programs, with the chip's standard input clock of 1 193 182 Hz.

A reimplementation can therefore count a tick as 1/18.2 s, about 54.9 ms. Some durations the game uses:

TicksReal timeUsed for
181 sthe result of a hand's use; the unit of timed block scripts
724 sthe spell book after a spell is cast from it
54630 sa dose of poison; the unit of lasting spells
108059 sfood
32 76030 minone hour of rest

A second counter, advanced by the same interrupt, never stops. The game times its waits with it, such as a script wait: a wait loops until that counter has moved by the number of ticks requested. The game loop does not run meanwhile, but the shared counter keeps advancing unless it is paused.1

Pauses​

The game pauses time by switching off the shared counter; the interrupt keeps running, with the music and the second counter. Nothing else is adjusted: since every deadline is a value of the counter, every timed update, character timer, timed block script and temporary wall waits with it and keeps the time it had left. The counter is off when the game starts and first runs at the end of the first level load.2

The counter is switched off:

  • while the camp menu is open, rest included; the rest moves the counter forward itself;
  • while a level is loaded from its INF file, and while the state of a level is packed into or unpacked from its level record;
  • during five animations: a monster's attack when it stands directly in front of the party, the flash of a monster damaged in view, the sparks of a spell over the view, an explosion and the vortex of Cone of Cold.

The switch is a single bit, not a count: switching it on ends a pause even when another one is still under way.

Anomaly

Saving a game restarts time while the camp menu is still open. Packing the current level into the saved game switches the counter off and on again, and the menu stays open after the save. Until the player leaves the camp, the counter runs while the game loop does not. On leaving, each timed update that came due runs once, and the character timers, timed block scripts and temporary walls whose deadlines passed end or run at their next check. A faithful reimplementation lets time run from the moment a game is saved until the camp closes.2

Bug

The routines that switch a counter off and on return its previous state, but read the bit above the one they change. No caller uses that value, so play is not affected.2

Sources​

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

Footnotes​

  1. Timer interrupt, which advances the shared counter and the free-running counter; the sound driver timers, whose devices are named in the sound programs; the script wait. Comparative source: REF-SCUMM-022, _tickLength. ↩ ↩2

  2. Stop and restart a counter, the counter switches and their callers: the camp menu, the INF level loader, pack and unpack a level record, the close-range action, flash a damaged monster, the spell animations, draw an explosion and the Cone of Cold vortex; the anomaly involves the save writer. Comparative source: REF-SCUMM-022, disableSysTimer, enableSysTimer and the functions that call them. ↩ ↩2 ↩3