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