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