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