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