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