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