Turbo Pascal Text Adventure Primer cover: BEACON, the adventure the primer builds in Turbo Pascal 2.0, mid-game on the Tatung Einstein
← Back to Modules
module
16

How The Real Games Did It

Introduction

Three adventures from the TatungBytes library have come up throughout the primer. This section puts together what their discs show: what is on each, how the text is stored, how the words are recognised, and how much of it you can now rebuild in Pascal yourself. Everything here was read off the discs, byte by byte, or seen by playing the games.

What Is On The Discs

DiscProgramData
Zork I, II, IIIZORK1.COM, ZORK2.COM, ZORK3.COM, 8,704 bytes each.DAT files of 84,992, 90,112 and 82,944 bytes
The Hitchhiker's Guide to the GalaxyHITCHHIK.COM, 8,704 bytesHITCHHIK.DAT, 113,664 bytes
Alice and the March HareALICE.COM, 12,800 bytesALICE.DAT, 24,576 bytes

Infocom: One Program, Four Stories

Compare the four Infocom programs byte by byte, and they are the same program. The only bytes that differ are eight near the start, which hold the name of the data file to open: ZORK1 in one, HITCHHIK in another. The program is a player; the game is all in the data file.

Each data file starts with a header of numbers, two bytes each, high byte first. Among them are the game's release and serial number, which are the same as the title screen shows (Revision 88 / Serial number 840726 for Zork I), and the length of the story with a check total of every byte in it. Zork I's story is 84,876 bytes.

The story is read as you play. Take the disc out of the drive just after Zork has started, type a move, and the disc system itself answers: No Drive, Drive 0:. Leave the disc in for one move and take it out after, and the next room still appears: what the game read for one move covered more than one room.

The text is packed three letters to two bytes, in 5-bit codes, much as S14 packed BEACON's: codes for a space and the letters, shift codes for capitals and for a table of punctuation, and the top bit of the last pair of bytes marking the end. Common words are numbered, as in S15: 96 of them, the, You, There, and on to Unfortunately. Between the two, Zork's opening sentence, 83 characters long, takes 48 bytes.

The dictionary holds every word the game knows: 697 in Zork I, 971 in The Hitchhiker's Guide to the Galaxy. They are in order, as S9's binary search needs, and each is stored as its first six letters, packed into four bytes. That is why Zork took MAILBOXXYZ for the mailbox and refused MAILBIX: only the first six letters are ever compared (S7). No two of Zork I's 697 entries are the same, so six letters keep them all apart.

An unknown word is quoted back in small letters (I don't know the word "frobnicate".) and, unlike every other command, does not count as a move.

Building A Zork Dictionary Entry

You know enough to build an entry yourself. The six letters become six codes, A as 6 up to Z as 31, and a word shorter than six is padded with 5s. The first three codes make one integer, the last three another, and the second gets its top bit set to mark the end. Printed in hexadecimal, high byte first, that is exactly what is on the disc.

The program below does it. For MAILBOX it prints 48 CE C4 F4; those four bytes are entry 370 of Zork I's 697, in its own data file. LAMP and XYZZY are entries 336 and 688.

Alice And The March Hare: Plain And Simple

SYNTAXSOFT's game, from 1986, is the other way about.

Its text is plain. Every message in ALICE.DAT is ordinary characters, a $ marking the end and a # marking a new line on the screen. Nothing is packed.

Its words are whole, with tokens. The word list is each word, a $, and one byte, its token: S8's table, stored in a file. Synonyms share a token (kill and stab are both 20; quit, stop, end and finish all 40), and the one-letter words l, i, n are synonyms of look, inventory and north. In play only a whole word matches: INVENTORY and I work, INVENTOR and NORT get No such command!. A word it knows used where it makes no sense gets a different answer, No luck!, so the player can tell the two apart.

Two Ways, Both Right

Zork, Hitchhiker'sAlice and the March Hare
Textpacked, common words numberedplain characters
Wordsfirst six letters, in orderwhole words, each with a token
Storyread from the disc as you play
Unknown wordquoted backNo such command!

Infocom's way squeezes a large story onto a disc and reads it as it goes, at the cost of a player program that unpacks everything. SYNTAXSOFT's way keeps a smaller game simple, readable even in its data file. BEACON, in part three, sits with Alice: whole words with tokens, and plain text in a file. But it borrows Zork's ordered table and its binary search.

The Code

program ZWord;
type
  Str12 = string[12];
var
  W: Str12;

procedure Hex(B: Integer);
const
  D: string[16] = '0123456789ABCDEF';
begin
  Write(D[B shr 4 + 1], D[B and 15 + 1], ' ');
end;

procedure Entry(W: Str12);
var
  C: array[1..6] of Integer;
  I, A, B: Integer;
begin
  for I := 1 to 6 do
    if I <= Length(W) then
      C[I] := Ord(UpCase(W[I])) - 59
    else
      C[I] := 5;
  A := C[1] shl 10 + C[2] shl 5 + C[3];
  B := C[4] shl 10 + C[5] shl 5 + C[6];
  B := B or $8000;
  Write(W, ': ');
  Hex(A shr 8); Hex(A and 255);
  Hex(B shr 8); Hex(B and 255);
  Writeln;
end;

begin
  repeat
    Write('Word? ');
    Readln(W);
    if W <> '' then
      Entry(W);
  until W = '';
end.

Ord('A') is 65, so Ord - 59 makes A 6. Hex prints a byte as two hexadecimal digits, picking each out of a string.

Starting from

A new work file ZWORD.

What you should see

Word? MAILBOX
MAILBOX: 48 CE C4 F4
Word? LAMP
LAMP: 44 D2 D4 A5
Word? XYZZY
XYZZY: 77 DF FF C5

Change One Thing

  • Change - 59 to - 58. What happens to every byte, and would Zork find the word?
  • Take out B := B or $8000;. Which bytes change, and by how much?
  • Change if I <= Length(W) to if I <= Length(W) - 1. Which of the three words still comes out right, and why that one?

Exercises

16.1 Print the six codes before the four bytes, so you can see the packing happen.

16.2 Go the other way: given the two integers $48CE and $C4F4, print the letters they hold. (Codes under 6 are padding.)

16.3 Read VOCAB.TXT and find any two words that would become the same entry if cut to six letters. How many are there?

Worked solutions are in Appendix II.

Summary

The four Infocom games share one program and keep everything in a data file read from the disc as they play: text packed three letters to two bytes, 96 common words by number, and a dictionary of six-letter entries in order, which a dozen lines of Pascal can reproduce exactly. Alice and the March Hare keeps plain text and whole words with one-byte tokens. BEACON takes whole words and tokens from Alice and the ordered table from Zork.

Next

S17, The World As Data: part three. BEACON begins.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.

This form is protected by reCAPTCHA, and the Google Privacy Policy and Terms of Service apply.