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