The game loop
The engine · Timed updates · How the systems affect one another
In brief
The program shows the start menu once, then enters a loop that has no exit of its own. There is no fixed frame rate: the loop repeats immediately, and everything periodic waits for a deadline of the shared counter. Each pass checks the party, reads the player's input, runs the timed updates that are due, redraws the view at most once and plays the pending sound. The program ends from inside one of these steps: when the player exits from the camp menu, when a defeated party does not load a game, at the end of the game, or on an error.
One pass of the game loop
Each pass does the following, in this order:1
- Party check. If no character can still act (present, conscious and
free of two state conditions:
CharacterEligibilityMaskvalue 13), the game starts its defeat sequence. - Input. A pending key, if any, is read. The interface buttons are processed and the action of a used button runs, such as moving the party or activating the wall in front. The resulting key code then goes to the keyboard commands.
- Timed updates. Every timed update that is due runs, in a fixed order: character-exchange blink, flying objects, monster groups 0 to 3, character timers, doors, wall animation, food and monster idle animation. Then come the step-triggered script, the timed block scripts and the expiry of temporary walls.
- Redraw. If any step of the pass marked the view as changed, the scene is redrawn once and the mark is cleared.
- Ambient sound. When its deadline has passed, the game sets a new one 18 × (1d10 + 3) ticks ahead and queues sound 27 or 28 on a block further along the map row: the party's block index plus 0 to 11.
- Sound flush. The environmental sound still pending from this pass is played.
What the order means
- Events run where they are sent. The block event dispatcher is called directly, so a block's script runs inside the step that sent the event. A script triggered by the party entering a block runs during the input step, before any monster of that pass acts; a script started by a flying object, a monster move or a timer runs during the timed updates, in the position of its sender in the table.
- Flying objects move before monsters. Within one pass, the flying-object update comes before the four monster groups, and the monster groups come in group order.
- One redraw per pass. Changes made by several updates in the same pass appear together in the next redraw. Paths that redraw the scene themselves, such as the scene redraw command, are not bound by this.
- Late passes do not catch up. Each timed update runs at most once per pass, and its next deadline counts from when it ran.
- Sounds are deferred. A request for environmental sound plays the one queued before it and waits; the end of the pass plays whatever is still waiting.
How the game ends
The program runs the start-up screen once, then enters the loop. The program
ends from inside one of the loop's steps, by calling the routine that
restores the machine and returns to DOS. Apart from FINAL.SAV at the end of
the game, nothing is saved on the way out. The ways out reached in play
are:2
- Exit the game from the camp menu, after confirming;
- defeat, when the player declines to load a game or the load fails (below);
- the end of the game: the death of the final opponent's second form on
level 16 writes
FINAL.SAV, plays the ending and returns to DOS when the player presses Escape on its last picture; - a failed protection question, asked by a script;
- errors: a level's walls that do not fit in its record (map compression failure), lack of memory, a fatal disk error.
Every exit call of the program, on start-up and error paths included, is listed with its message and exit code in exit the program.
The start-up screen can also exit, before the loop starts. Saving or loading never leaves the loop: a game loaded from the camp menu or after a defeat goes on in the same loop.
When the party is defeated
The party check of each pass counts the characters that can act. When none can, the game:3
- redraws the six portraits;
- if the global flag
SCRIPT_GLOBAL_FLAG_KHELBEN_VISIONis set (from Khelben's vision on level 12 until the party next enters that block or meets the skull), plays a short scene with theKHELDRANpicture and two dialogue texts; - announces that the whole party has been defeated and asks, with Yes and No, whether to load a saved game;
- on No, returns to DOS;
- on Yes, closes the inventory panel and the spell book if they are open and shows the load menu. If the player leaves the menu or the chosen game cannot be read, the program returns to DOS; otherwise the loaded game goes on from the input step of the same pass.
The same check runs, with the same sequence, when the party leaves the camp menu. Resting asks the check only whether someone can act, without starting the sequence.
Sources
Locations refer to the game executable reference; comparative sources are listed in the technical references.