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