0fbd17e29c86a8df4f27b02221e785938600f4ab
[open-adventure.git] / dungeon.c
1 /*
2  * The dungeon compiler. Turns adventure.text into a set of C initializers
3  * defining (mostly) invariant state.  A couple of slots are messed with
4  * at runtime.
5  */
6
7 #define LINESIZE 100
8 #define RTXSIZ 277
9 #define CLSMAX 12
10 #define LINSIZ 12600
11 #define TRNSIZ 5
12 #define TABSIZ 330
13 #define VRBSIZ 35
14 #define TRVSIZ 885
15 #define TOKLEN 5
16 #define HINTLEN 5
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <stdbool.h>
21 #include <unistd.h>
22 #include "common.h"
23
24 // Global variables for use in functions below that can gradually disappear as code is cleaned up
25 static long LNLENG;
26 static long LNPOSN;
27 static char INLINE[LINESIZE + 1];
28 static long OLDLOC;
29
30 // Storage for what comes out of the database
31 long LINUSE;
32 long TRVS;
33 long CLSSES;
34 long TRNVLS;
35 long TABNDX;
36 long HNTMAX;
37 long PTEXT[NOBJECTS + 1];
38 long RTEXT[RTXSIZ + 1];
39 long CTEXT[CLSMAX + 1];
40 long OBJSND[NOBJECTS + 1];
41 long OBJTXT[NOBJECTS + 1];
42 long STEXT[LOCSIZ + 1];
43 long LTEXT[LOCSIZ + 1];
44 long COND[LOCSIZ + 1];
45 long KEY[LOCSIZ + 1];
46 long LOCSND[LOCSIZ + 1];
47 long LINES[LINSIZ + 1];
48 long CVAL[CLSMAX + 1];
49 long TTEXT[TRNSIZ + 1];
50 long TRNVAL[TRNSIZ + 1];
51 long TRAVEL[TRVSIZ + 1];
52 long KTAB[TABSIZ + 1];
53 long ATAB[TABSIZ + 1];
54 long PLAC[NOBJECTS + 1];
55 long FIXD[NOBJECTS + 1];
56 long ACTSPK[VRBSIZ + 1];
57 long HINTS[HNTSIZ + 1][HINTLEN];
58
59
60 static bool is_set(long var, long position)
61 {
62     long mask = 1l << position;
63     bool result = (var & mask) == mask;
64     return (result);
65 }
66
67 static long GETTXT(long SKIP, long ONEWRD, long UPPER)
68 {
69     /*  Take characters from an input line and pack them into 30-bit words.
70      *  Skip says to skip leading blanks.  ONEWRD says stop if we come to a
71      *  blank.  UPPER says to map all letters to uppercase.  If we reach the
72      *  end of the line, the word is filled up with blanks (which encode as 0's).
73      *  If we're already at end of line when GETTXT is called, we return -1. */
74
75     long TEXT;
76     static long SPLITTING = -1;
77
78     if (LNPOSN != SPLITTING)
79         SPLITTING = -1;
80     TEXT = -1;
81     while (true) {
82         if (LNPOSN > LNLENG)
83             return (TEXT);
84         if ((!SKIP) || INLINE[LNPOSN] != 0)
85             break;
86         LNPOSN = LNPOSN + 1;
87     }
88
89     TEXT = 0;
90     for (int I = 1; I <= TOKLEN; I++) {
91         TEXT = TEXT * 64;
92         if (LNPOSN > LNLENG || (ONEWRD && INLINE[LNPOSN] == 0))
93             continue;
94         char current = INLINE[LNPOSN];
95         if (current < 63) {
96             SPLITTING = -1;
97             if (UPPER && current >= 37)
98                 current = current - 26;
99             TEXT = TEXT + current;
100             LNPOSN = LNPOSN + 1;
101             continue;
102         }
103         if (SPLITTING != LNPOSN) {
104             TEXT = TEXT + 63;
105             SPLITTING = LNPOSN;
106             continue;
107         }
108
109         TEXT = TEXT + current - 63;
110         SPLITTING = -1;
111         LNPOSN = LNPOSN + 1;
112     }
113
114     return (TEXT);
115 }
116
117 static void MAPLIN(FILE *OPENED)
118 {
119     /*  Read a line of input, from the specified input source,
120      *  translate the chars to integers in the range 0-126 and store
121      *  them in the common array "INLINE".  Integer values are as follows:
122      *     0   = space [ASCII CODE 40 octal, 32 decimal]
123      *    1-2  = !" [ASCII 41-42 octal, 33-34 decimal]
124      *    3-10 = '()*+,-. [ASCII 47-56 octal, 39-46 decimal]
125      *   11-36 = upper-case letters
126      *   37-62 = lower-case letters
127      *    63   = percent (%) [ASCII 45 octal, 37 decimal]
128      *   64-73 = digits, 0 through 9
129      *  Remaining characters can be translated any way that is convenient;
130      *  The "TYPE" routine below is used to map them back to characters when
131      *  necessary.  The above mappings are required so that certain special
132      *  characters are known to fit in 6 bits and/or can be easily spotted.
133      *  Array elements beyond the end of the line should be filled with 0,
134      *  and LNLENG should be set to the index of the last character.
135      *
136      *  If the data file uses a character other than space (e.g., tab) to
137      *  separate numbers, that character should also translate to 0.
138      *
139      *  This procedure may use the map1,map2 arrays to maintain static data for
140      *  the mapping.  MAP2(1) is set to 0 when the program starts
141      *  and is not changed thereafter unless the routines on this page choose
142      *  to do so. */
143
144     do {
145         if (NULL == fgets(INLINE + 1, sizeof(INLINE) - 1, OPENED)) {
146             printf("Failed fgets()\n");
147         }
148     } while (!feof(OPENED) && INLINE[1] == '#');
149
150     LNLENG = 0;
151     for (size_t i = 1; i < sizeof(INLINE) && INLINE[i] != 0; ++i) {
152         char val = INLINE[i];
153         INLINE[i] = ascii_to_advent[(unsigned)val];
154         if (INLINE[i] != 0)
155             LNLENG = i;
156     }
157     LNPOSN = 1;
158 }
159
160 static long GETNUM(FILE *source)
161 {
162     /*  Obtain the next integer from an input line.  If K>0, we first read a
163      *  new input line from a file; if K<0, we read a line from the keyboard;
164      *  if K=0 we use a line that has already been read (and perhaps partially
165      *  scanned).  If we're at the end of the line or encounter an illegal
166      *  character (not a digit, hyphen, or blank), we return 0. */
167
168     long DIGIT, GETNUM, SIGN;
169
170     if (source != NULL) MAPLIN(source);
171     GETNUM = 0;
172
173     while (INLINE[LNPOSN] == 0) {
174         if (LNPOSN > LNLENG) return (GETNUM);
175         ++LNPOSN;
176     }
177
178     if (INLINE[LNPOSN] != 9) {
179         SIGN = 1;
180     } else {
181         SIGN = -1;
182         LNPOSN = LNPOSN + 1;
183     }
184     while (!(LNPOSN > LNLENG || INLINE[LNPOSN] == 0)) {
185         DIGIT = INLINE[LNPOSN] - 64;
186         if (DIGIT < 0 || DIGIT > 9) {
187             GETNUM = 0;
188             break;
189         }
190         GETNUM = GETNUM * 10 + DIGIT;
191         LNPOSN = LNPOSN + 1;
192     }
193
194     GETNUM = GETNUM * SIGN;
195     LNPOSN = LNPOSN + 1;
196     return (GETNUM);
197 }
198
199 /*  Sections 1, 2, 5, 6, 10, 14.  Read messages and set up pointers. */
200 static void read_messages(FILE* database, long sect)
201 {
202     long KK = LINUSE;
203     while (true) {
204         long loc;
205         LINUSE = KK;
206         loc = GETNUM(database);
207         if (LNLENG >= LNPOSN + 70)
208             BUG(MESSAGE_LINE_GT_70_CHARACTERS);
209         if (loc == -1) return;
210         if (LNLENG < LNPOSN)
211             BUG(NULL_LINE_IN_MESSAGE);
212         do {
213             KK = KK + 1;
214             if (KK >= LINSIZ)
215                 BUG(TOO_MANY_WORDS_OF_MESSAGES);
216             LINES[KK] = GETTXT(false, false, false);
217         } while (LINES[KK] != -1);
218         LINES[LINUSE] = KK;
219         if (loc == OLDLOC) continue;
220         OLDLOC = loc;
221         LINES[LINUSE] = -KK;
222         if (sect == 14) {
223             TRNVLS = TRNVLS + 1;
224             if (TRNVLS > TRNSIZ)
225                 BUG(TOO_MANY_CLASS_OR_TURN_MESSAGES);
226             TTEXT[TRNVLS] = LINUSE;
227             TRNVAL[TRNVLS] = loc;
228             continue;
229         }
230         if (sect == 10) {
231             CLSSES = CLSSES + 1;
232             if (CLSSES > CLSMAX)
233                 BUG(TOO_MANY_CLASS_OR_TURN_MESSAGES);
234             CTEXT[CLSSES] = LINUSE;
235             CVAL[CLSSES] = loc;
236             continue;
237         }
238         if (sect == 6) {
239             if (loc > RTXSIZ)
240                 BUG(TOO_MANY_RTEXT_MESSAGES);
241             RTEXT[loc] = LINUSE;
242             continue;
243         }
244         if (sect == 5) {
245             if (loc > 0 && loc <= NOBJECTS)PTEXT[loc] = LINUSE;
246             continue;
247         }
248         if (loc > LOCSIZ)
249             BUG(TOO_MANY_LOCATIONS);
250         if (sect == 1) {
251             LTEXT[loc] = LINUSE;
252             continue;
253         }
254
255         STEXT[loc] = LINUSE;
256     }
257 }
258
259 /*  The stuff for section 3 is encoded here.  Each "from-location" gets a
260  *  contiguous section of the "TRAVEL" array.  Each entry in travel is
261  *  newloc*1000 + KEYWORD (from section 4, motion verbs), and is negated if
262  *  this is the last entry for this location.  KEY(N) is the index in travel
263  *  of the first option at location N. */
264 static void read_section3_stuff(FILE* database)
265 {
266     long loc;
267     while ((loc = GETNUM(database)) != -1) {
268         long newloc = GETNUM(NULL);
269         long L;
270         if (KEY[loc] == 0) {
271             KEY[loc] = TRVS;
272         } else {
273             TRAVEL[TRVS - 1] = -TRAVEL[TRVS - 1];
274         }
275         while ((L = GETNUM(NULL)) != 0) {
276             TRAVEL[TRVS] = newloc * 1000 + L;
277             TRVS = TRVS + 1;
278             if (TRVS == TRVSIZ)
279                 BUG(TOO_MANY_TRAVEL_OPTIONS);
280         }
281         TRAVEL[TRVS - 1] = -TRAVEL[TRVS - 1];
282     }
283 }
284
285 /*  Here we read in the vocabulary.  KTAB(N) is the word number, ATAB(N) is
286  *  the corresponding word.  The -1 at the end of section 4 is left in KTAB
287  *  as an end-marker. */
288 static void read_vocabulary(FILE* database)
289 {
290     for (TABNDX = 1; TABNDX <= TABSIZ; TABNDX++) {
291         KTAB[TABNDX] = GETNUM(database);
292         if (KTAB[TABNDX] == -1) return;
293         ATAB[TABNDX] = GETTXT(true, true, true);
294     } /* end loop */
295     BUG(TOO_MANY_VOCABULARY_WORDS);
296 }
297
298 /*  Read in the initial locations for each object.  Also the immovability info.
299  *  plac contains initial locations of objects.  FIXD is -1 for immovable
300  *  objects (including the snake), or = second loc for two-placed objects. */
301 static void read_initial_locations(FILE* database)
302 {
303     long OBJ;
304     while ((OBJ = GETNUM(database)) != -1) {
305         PLAC[OBJ] = GETNUM(NULL);
306         FIXD[OBJ] = GETNUM(NULL);
307     }
308 }
309
310 /*  Read default message numbers for action verbs, store in ACTSPK. */
311 static void read_action_verb_message_nr(FILE* database)
312 {
313     long verb;
314     while ((verb = GETNUM(database)) != -1) {
315         ACTSPK[verb] = GETNUM(NULL);
316     }
317 }
318
319 /*  Read info about available liquids and other conditions, store in COND. */
320 static void read_conditions(FILE* database)
321 {
322     long K;
323     while ((K = GETNUM(database)) != -1) {
324         long loc;
325         while ((loc = GETNUM(NULL)) != 0) {
326             if (is_set(COND[loc], K))
327                 BUG(LOCATION_HAS_CONDITION_BIT_BEING_SET_TWICE);
328             COND[loc] = COND[loc] + (1l << K);
329         }
330     }
331 }
332
333
334 /*  Read data for hints. */
335 static void read_hints(FILE* database)
336 {
337     long K;
338     HNTMAX = 0;
339     while ((K = GETNUM(database)) != -1) {
340         if (K <= 0 || K > HNTSIZ)
341             BUG(TOO_MANY_HINTS);
342         for (int I = 1; I <= 4; I++) {
343             HINTS[K][I] = GETNUM(NULL);
344         } /* end loop */
345         HNTMAX = (HNTMAX > K ? HNTMAX : K);
346     }
347 }
348
349 /*  Read the sound/text info, store in OBJSND, OBJTXT, LOCSND. */
350 static void read_sound_text(FILE* database)
351 {
352     long K;
353     while ((K = GETNUM(database)) != -1) {
354         long KK = GETNUM(NULL);
355         long I = GETNUM(NULL);
356         if (I != 0) {
357             OBJSND[K] = (KK > 0 ? KK : 0);
358             OBJTXT[K] = (I > 0 ? I : 0);
359             continue;
360         }
361
362         LOCSND[K] = KK;
363     }
364 }
365
366
367 static int read_database(FILE* database)
368 {
369
370     /*  Clear out the various text-pointer arrays.  All text is stored in array
371      *  lines; each line is preceded by a word pointing to the next pointer (i.e.
372      *  the word following the end of the line).  The pointer is negative if this is
373      *  first line of a message.  The text-pointer arrays contain indices of
374      *  pointer-words in lines.  STEXT(N) is short description of location N.
375      *  LTEXT(N) is long description.  PTEXT(N) points to message for game.prop(N)=0.
376      *  Successive prop messages are found by chasing pointers.  RTEXT contains
377      *  section 6's stuff.  CTEXT(N) points to a player-class message.  TTEXT is for
378      *  section 14.  We also clear COND (see description of section 9 for details). */
379
380     for (int I = 1; I <= NOBJECTS; I++) {
381         PTEXT[I] = 0;
382         OBJSND[I] = 0;
383         OBJTXT[I] = 0;
384     }
385     for (int I = 1; I <= RTXSIZ; I++) {
386         RTEXT[I] = 0;
387     }
388     for (int I = 1; I <= CLSMAX; I++) {
389         CTEXT[I] = 0;
390     }
391     for (int I = 1; I <= LOCSIZ; I++) {
392         STEXT[I] = 0;
393         LTEXT[I] = 0;
394         COND[I] = 0;
395         KEY[I] = 0;
396         LOCSND[I] = 0;
397     }
398
399     LINUSE = 1;
400     TRVS = 1;
401     CLSSES = 0;
402     TRNVLS = 0;
403
404     /*  Start new data section.  Sect is the section number. */
405
406     while (true) {
407         long sect = GETNUM(database);
408         OLDLOC = -1;
409         switch (sect) {
410         case 0:
411             return (0);
412         case 1:
413             read_messages(database, sect);
414             break;
415         case 2:
416             read_messages(database, sect);
417             break;
418         case 3:
419             read_section3_stuff(database);
420             break;
421         case 4:
422             read_vocabulary(database);
423             break;
424         case 5:
425             read_messages(database, sect);
426             break;
427         case 6:
428             read_messages(database, sect);
429             break;
430         case 7:
431             read_initial_locations(database);
432             break;
433         case 8:
434             read_action_verb_message_nr(database);
435             break;
436         case 9:
437             read_conditions(database);
438             break;
439         case 10:
440             read_messages(database, sect);
441             break;
442         case 11:
443             read_hints(database);
444             break;
445         case 12:
446             break;
447         case 13:
448             read_sound_text(database);
449             break;
450         case 14:
451             read_messages(database, sect);
452             break;
453         default:
454             BUG(INVALID_SECTION_NUMBER_IN_DATABASE);
455         }
456     }
457 }
458
459 /*  Finish constructing internal data format */
460
461 /*  Having read in the database, certain things are now constructed.
462  *  game.propS are set to zero.  We finish setting up COND by checking for
463  *  forced-motion travel entries.  The PLAC and FIXD arrays are used
464  *  to set up game.atloc(N) as the first object at location N, and
465  *  game.link(OBJ) as the next object at the same location as OBJ.
466  *  (OBJ>NOBJECTS indicates that game.fixed(OBJ-NOBJECTS)=LOC; game.link(OBJ) is
467  *  still the correct link to use.)  game.abbrev is zeroed; it controls
468  *  whether the abbreviated description is printed.  Counts modulo 5
469  *  unless "LOOK" is used. */
470
471 static void write_0d(FILE* header_file, long single, const char* varname)
472 {
473     fprintf(header_file, "LOCATION long %s INITIALIZE(= %ld);\n", varname, single);
474 }
475
476 static void write_1d(FILE* header_file, long array[], long dim, const char* varname)
477 {
478     fprintf(header_file, "LOCATION long %s[] INITIALIZE(= {\n", varname);
479     for (int i = 0; i < dim; ++i) {
480         if (i % 10 == 0) {
481             if (i > 0)
482                 fprintf(header_file, "\n");
483             fprintf(header_file, "  ");
484         }
485         fprintf(header_file, "%ld, ", array[i]);
486     }
487     fprintf(header_file, "\n});\n");
488 }
489
490 static void write_hints(FILE* header_file, long matrix[][HINTLEN], long dim1, long dim2, const char* varname)
491 {
492     fprintf(header_file, "LOCATION long %s[][%ld] INITIALIZE(= {\n", varname, dim2);
493     for (int i = 0; i < dim1; ++i) {
494         fprintf(header_file, "  {");
495         for (int j = 0; j < dim2; ++j) {
496             fprintf(header_file, "%ld, ", matrix[i][j]);
497         }
498         fprintf(header_file, "},\n");
499     }
500     fprintf(header_file, "});\n");
501 }
502
503 static void write_file(FILE* header_file)
504 {
505     fprintf(header_file, "#ifndef DATABASE_H\n");
506     fprintf(header_file, "#define DATABASE_H\n");
507     fprintf(header_file, "\n");
508
509     fprintf(header_file, "#include \"common.h\"\n");
510     fprintf(header_file, "#define TABSIZ 330\n");
511     fprintf(header_file, "#define HNTSIZ 20\n");
512     fprintf(header_file, "#define TOKLEN %d\n", TOKLEN);
513     fprintf(header_file, "\n");
514
515     fprintf(header_file, "\n");
516     fprintf(header_file, "#ifdef DEFINE_GLOBALS_FROM_INCLUDES\n");
517     fprintf(header_file, "#define LOCATION\n");
518     fprintf(header_file, "#define INITIALIZE(...) __VA_ARGS__\n");
519     fprintf(header_file, "#else\n");
520     fprintf(header_file, "#define LOCATION extern\n");
521     fprintf(header_file, "#define INITIALIZE(...)\n");
522     fprintf(header_file, "#endif\n");
523     fprintf(header_file, "\n");
524
525     // content variables
526     write_0d(header_file, HNTMAX, "HNTMAX");
527     write_1d(header_file, OBJSND, NOBJECTS + 1, "OBJSND");
528     write_1d(header_file, OBJTXT, NOBJECTS + 1, "OBJTXT");
529     write_1d(header_file, COND, LOCSIZ + 1, "COND");
530     write_1d(header_file, KEY, LOCSIZ + 1, "KEY");
531     write_1d(header_file, LOCSND, LOCSIZ + 1, "LOCSND");
532     write_1d(header_file, CVAL, CLSMAX + 1, "CVAL");
533     write_1d(header_file, TRAVEL, TRVSIZ + 1, "TRAVEL");
534     write_1d(header_file, KTAB, TABSIZ + 1, "KTAB");
535     write_1d(header_file, ATAB, TABSIZ + 1, "ATAB");
536     write_1d(header_file, PLAC, NOBJECTS + 1, "PLAC");
537     write_1d(header_file, FIXD, NOBJECTS + 1, "FIXD");
538     write_1d(header_file, ACTSPK, VRBSIZ + 1, "ACTSPK");
539     write_hints(header_file, HINTS, HNTSIZ + 1, 5, "HINTS");
540
541     fprintf(header_file, "#undef LOCATION\n");
542     fprintf(header_file, "#undef INITIALIZE\n");
543     fprintf(header_file, "#endif\n");
544 }
545
546 void bug(enum bugtype num, const char *error_string)
547 {
548     fprintf(stderr, "Fatal error %d, %s.\n", num, error_string);
549     exit(EXIT_FAILURE);
550 }
551
552 int main(void)
553 {
554     FILE* database = fopen("adventure.text", "r");
555     read_database(database);
556     fclose(database);
557
558     FILE* header_file = fopen("database.h", "w");
559     write_file(header_file);
560     fclose(header_file);
561
562     return (EXIT_SUCCESS);
563 }