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