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