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