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