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