YAMLify section 11 (hints).
[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
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, store in COND. */
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             if (is_set(COND[loc], K))
310                 BUG(LOCATION_HAS_CONDITION_BIT_BEING_SET_TWICE);
311             COND[loc] = COND[loc] + (1l << K);
312         }
313     }
314 }
315
316
317 /*  Read data for hints. */
318 static void read_hints(FILE* database)
319 {
320     long K;
321     HNTMAX = 0;
322     while ((K = GETNUM(database)) != -1) {
323         if (K <= 0 || K > HNTSIZ)
324             BUG(TOO_MANY_HINTS);
325         for (int I = 1; I <= 4; I++) {
326             /* consume - actual arrqy-building now done in YAML. */
327             GETNUM(NULL);
328         } /* end loop */
329         HNTMAX = (HNTMAX > K ? HNTMAX : K);
330     }
331 }
332
333 /*  Read the sound/text info, store in OBJSND, OBJTXT, LOCSND. */
334 static void read_sound_text(FILE* database)
335 {
336     long K;
337     while ((K = GETNUM(database)) != -1) {
338         long KK = GETNUM(NULL);
339         long I = GETNUM(NULL);
340         if (I != 0) {
341             OBJSND[K] = (KK > 0 ? KK : 0);
342             OBJTXT[K] = (I > 0 ? I : 0);
343             continue;
344         }
345
346         LOCSND[K] = KK;
347     }
348 }
349
350
351 static int read_database(FILE* database)
352 {
353     /*  Clear out the various text-pointer arrays.  All text is stored
354      *  in array lines; each line is preceded by a word pointing to
355      *  the next pointer (i.e.  the word following the end of the
356      *  line).  The pointer is negative if this is first line of a
357      *  message.  The text-pointer arrays contain indices of
358      *  pointer-words in lines.  STEXT(N) is short description of
359      *  location N.  LTEXT(N) is long description.  PTEXT(N) points to
360      *  message for game.prop(N)=0.  Successive prop messages are
361      *  found by chasing pointers.  RTEXT contains section 6's stuff.
362      *  TTEXT is for section 14.  We also clear COND (see description
363      *  of section 9 for details). */
364     for (int I = 1; I <= NOBJECTS; I++) {
365         PTEXT[I] = 0;
366         OBJSND[I] = 0;
367         OBJTXT[I] = 0;
368     }
369     for (int I = 1; I <= RTXSIZ; I++) {
370         RTEXT[I] = 0;
371     }
372     for (int I = 1; I <= LOCSIZ; I++) {
373         STEXT[I] = 0;
374         LTEXT[I] = 0;
375         COND[I] = 0;
376         KEY[I] = 0;
377         LOCSND[I] = 0;
378     }
379
380     LINUSE = 1;
381     TRVS = 1;
382     TRNVLS = 0;
383
384     /*  Start new data section.  Sect is the section number. */
385
386     while (true) {
387         long sect = GETNUM(database);
388         OLDLOC = -1;
389         switch (sect) {
390         case 0:
391             return (0);
392         case 1:
393             read_messages(database, sect);
394             break;
395         case 2:
396             read_messages(database, sect);
397             break;
398         case 3:
399             read_section3_stuff(database);
400             break;
401         case 4:
402             read_vocabulary(database);
403             break;
404         case 5:
405             read_messages(database, sect);
406             break;
407         case 6:
408             read_messages(database, sect);
409             break;
410         case 7:
411             read_initial_locations(database);
412             break;
413         case 8:
414             read_action_verb_message_nr(database);
415             break;
416         case 9:
417             read_conditions(database);
418             break;
419         case 10:
420             read_messages(database, sect);
421             break;
422         case 11:
423             read_hints(database);
424             break;
425         case 12:
426             break;
427         case 13:
428             read_sound_text(database);
429             break;
430         case 14:
431             read_messages(database, sect);
432             break;
433         default:
434             BUG(INVALID_SECTION_NUMBER_IN_DATABASE);
435         }
436     }
437 }
438
439 /*  Finish constructing internal data format */
440
441 /*  Having read in the database, certain things are now constructed.
442  *  game.propS are set to zero.  We finish setting up COND by checking for
443  *  forced-motion travel entries.  The PLAC and FIXD arrays are used
444  *  to set up game.atloc(N) as the first object at location N, and
445  *  game.link(OBJ) as the next object at the same location as OBJ.
446  *  (OBJ>NOBJECTS indicates that game.fixed(OBJ-NOBJECTS)=LOC; game.link(OBJ) is
447  *  still the correct link to use.)  game.abbrev is zeroed; it controls
448  *  whether the abbreviated description is printed.  Counts modulo 5
449  *  unless "LOOK" is used. */
450
451 static void write_0d(FILE* header_file, long single, const char* varname)
452 {
453     fprintf(header_file, "LOCATION long %s INITIALIZE(= %ld);\n", varname, single);
454 }
455
456 static void write_1d(FILE* header_file, long array[], long dim, const char* varname)
457 {
458     fprintf(header_file, "LOCATION long %s[] INITIALIZE(= {\n", varname);
459     for (int i = 0; i < dim; ++i) {
460         if (i % 10 == 0) {
461             if (i > 0)
462                 fprintf(header_file, "\n");
463             fprintf(header_file, "  ");
464         }
465         fprintf(header_file, "%ld, ", array[i]);
466     }
467     fprintf(header_file, "\n});\n");
468 }
469
470 static void write_file(FILE* header_file)
471 {
472     fprintf(header_file, "#ifndef DATABASE_H\n");
473     fprintf(header_file, "#define DATABASE_H\n");
474     fprintf(header_file, "\n");
475
476     fprintf(header_file, "#include \"common.h\"\n");
477     fprintf(header_file, "#define TABSIZ 330\n");
478     fprintf(header_file, "#define HNTSIZ 20\n");
479     fprintf(header_file, "#define TOKLEN %d\n", TOKLEN);
480     fprintf(header_file, "\n");
481
482     fprintf(header_file, "\n");
483     fprintf(header_file, "#ifdef DEFINE_GLOBALS_FROM_INCLUDES\n");
484     fprintf(header_file, "#define LOCATION\n");
485     fprintf(header_file, "#define INITIALIZE(...) __VA_ARGS__\n");
486     fprintf(header_file, "#else\n");
487     fprintf(header_file, "#define LOCATION extern\n");
488     fprintf(header_file, "#define INITIALIZE(...)\n");
489     fprintf(header_file, "#endif\n");
490     fprintf(header_file, "\n");
491
492     // content variables
493     write_0d(header_file, HNTMAX, "HNTMAX");
494     write_1d(header_file, OBJSND, NOBJECTS + 1, "OBJSND");
495     write_1d(header_file, OBJTXT, NOBJECTS + 1, "OBJTXT");
496     write_1d(header_file, COND, LOCSIZ + 1, "COND");
497     write_1d(header_file, KEY, LOCSIZ + 1, "KEY");
498     write_1d(header_file, LOCSND, LOCSIZ + 1, "LOCSND");
499     write_1d(header_file, TRAVEL, TRVSIZ + 1, "TRAVEL");
500     write_1d(header_file, KTAB, TABSIZ + 1, "KTAB");
501     write_1d(header_file, ATAB, TABSIZ + 1, "ATAB");
502     write_1d(header_file, PLAC, NOBJECTS + 1, "PLAC");
503     write_1d(header_file, FIXD, NOBJECTS + 1, "FIXD");
504     write_1d(header_file, ACTSPK, VRBSIZ + 1, "ACTSPK");
505
506     fprintf(header_file, "#undef LOCATION\n");
507     fprintf(header_file, "#undef INITIALIZE\n");
508     fprintf(header_file, "#endif\n");
509 }
510
511 void bug(enum bugtype num, const char *error_string)
512 {
513     fprintf(stderr, "Fatal error %d, %s.\n", num, error_string);
514     exit(EXIT_FAILURE);
515 }
516
517 int main(void)
518 {
519     FILE* database = fopen("adventure.text", "r");
520     read_database(database);
521     fclose(database);
522
523     FILE* header_file = fopen("database.h", "w");
524     write_file(header_file);
525     fclose(header_file);
526
527     return (EXIT_SUCCESS);
528 }