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
24 // Global variables for use in functions below that can gradually disappear as code is cleaned up
27 static char INLINE[LINESIZE + 1];
30 // Storage for what comes out of the database
36 long PTEXT[NOBJECTS + 1];
37 long RTEXT[RTXSIZ + 1];
38 long OBJSND[NOBJECTS + 1];
39 long OBJTXT[NOBJECTS + 1];
40 long STEXT[LOCSIZ + 1];
41 long LTEXT[LOCSIZ + 1];
42 long COND[LOCSIZ + 1];
44 long LOCSND[LOCSIZ + 1];
45 long LINES[LINSIZ + 1];
46 long TTEXT[TRNSIZ + 1];
47 long TRNVAL[TRNSIZ + 1];
48 long TRAVEL[TRVSIZ + 1];
49 long KTAB[TABSIZ + 1];
50 long ATAB[TABSIZ + 1];
51 long PLAC[NOBJECTS + 1];
52 long FIXD[NOBJECTS + 1];
53 long ACTSPK[VRBSIZ + 1];
55 static bool is_set(long var, long position)
57 long mask = 1l << position;
58 bool result = (var & mask) == mask;
62 static long GETTXT(long SKIP, long ONEWRD, long UPPER)
64 /* Take characters from an input line and pack them into 30-bit words.
65 * Skip says to skip leading blanks. ONEWRD says stop if we come to a
66 * blank. UPPER says to map all letters to uppercase. If we reach the
67 * end of the line, the word is filled up with blanks (which encode as 0's).
68 * If we're already at end of line when GETTXT is called, we return -1. */
71 static long SPLITTING = -1;
73 if (LNPOSN != SPLITTING)
79 if ((!SKIP) || INLINE[LNPOSN] != 0)
85 for (int I = 1; I <= TOKLEN; I++) {
87 if (LNPOSN > LNLENG || (ONEWRD && INLINE[LNPOSN] == 0))
89 char current = INLINE[LNPOSN];
92 if (UPPER && current >= 37)
93 current = current - 26;
94 TEXT = TEXT + current;
98 if (SPLITTING != LNPOSN) {
104 TEXT = TEXT + current - 63;
112 static void MAPLIN(FILE *OPENED)
114 /* Read a line of input, from the specified input source,
115 * translate the chars to integers in the range 0-126 and store
116 * them in the common array "INLINE". Integer values are as follows:
117 * 0 = space [ASCII CODE 40 octal, 32 decimal]
118 * 1-2 = !" [ASCII 41-42 octal, 33-34 decimal]
119 * 3-10 = '()*+,-. [ASCII 47-56 octal, 39-46 decimal]
120 * 11-36 = upper-case letters
121 * 37-62 = lower-case letters
122 * 63 = percent (%) [ASCII 45 octal, 37 decimal]
123 * 64-73 = digits, 0 through 9
124 * Remaining characters can be translated any way that is convenient;
125 * The "TYPE" routine below is used to map them back to characters when
126 * necessary. The above mappings are required so that certain special
127 * characters are known to fit in 6 bits and/or can be easily spotted.
128 * Array elements beyond the end of the line should be filled with 0,
129 * and LNLENG should be set to the index of the last character.
131 * If the data file uses a character other than space (e.g., tab) to
132 * separate numbers, that character should also translate to 0.
134 * This procedure may use the map1,map2 arrays to maintain static data for
135 * the mapping. MAP2(1) is set to 0 when the program starts
136 * and is not changed thereafter unless the routines on this page choose
140 if (NULL == fgets(INLINE + 1, sizeof(INLINE) - 1, OPENED)) {
141 printf("Failed fgets()\n");
143 } while (!feof(OPENED) && INLINE[1] == '#');
146 for (size_t i = 1; i < sizeof(INLINE) && INLINE[i] != 0; ++i) {
147 char val = INLINE[i];
148 INLINE[i] = ascii_to_advent[(unsigned)val];
155 static long GETNUM(FILE *source)
157 /* Obtain the next integer from an input line. If K>0, we first read a
158 * new input line from a file; if K<0, we read a line from the keyboard;
159 * if K=0 we use a line that has already been read (and perhaps partially
160 * scanned). If we're at the end of the line or encounter an illegal
161 * character (not a digit, hyphen, or blank), we return 0. */
163 long DIGIT, GETNUM, SIGN;
165 if (source != NULL) MAPLIN(source);
168 while (INLINE[LNPOSN] == 0) {
169 if (LNPOSN > LNLENG) return (GETNUM);
173 if (INLINE[LNPOSN] != 9) {
179 while (!(LNPOSN > LNLENG || INLINE[LNPOSN] == 0)) {
180 DIGIT = INLINE[LNPOSN] - 64;
181 if (DIGIT < 0 || DIGIT > 9) {
185 GETNUM = GETNUM * 10 + DIGIT;
189 GETNUM = GETNUM * SIGN;
194 /* Sections 1, 2, 5, 6, 10, 14. Read messages and set up pointers. */
195 static void read_messages(FILE* database, long sect)
201 loc = GETNUM(database);
202 if (LNLENG >= LNPOSN + 70)
203 BUG(MESSAGE_LINE_GT_70_CHARACTERS);
204 if (loc == -1) return;
206 BUG(NULL_LINE_IN_MESSAGE);
210 BUG(TOO_MANY_WORDS_OF_MESSAGES);
211 LINES[KK] = GETTXT(false, false, false);
212 } while (LINES[KK] != -1);
214 if (loc == OLDLOC) continue;
217 if (sect == 10 || sect == 14) {
218 /* now parsed from YAML */
223 BUG(TOO_MANY_RTEXT_MESSAGES);
228 if (loc > 0 && loc <= NOBJECTS)PTEXT[loc] = LINUSE;
232 BUG(TOO_MANY_LOCATIONS);
242 /* The stuff for section 3 is encoded here. Each "from-location" gets a
243 * contiguous section of the "TRAVEL" array. Each entry in travel is
244 * newloc*1000 + KEYWORD (from section 4, motion verbs), and is negated if
245 * this is the last entry for this location. KEY(N) is the index in travel
246 * of the first option at location N. */
247 static void read_section3_stuff(FILE* database)
250 while ((loc = GETNUM(database)) != -1) {
251 long newloc = GETNUM(NULL);
256 TRAVEL[TRVS - 1] = -TRAVEL[TRVS - 1];
258 while ((L = GETNUM(NULL)) != 0) {
259 TRAVEL[TRVS] = newloc * 1000 + L;
262 BUG(TOO_MANY_TRAVEL_OPTIONS);
264 TRAVEL[TRVS - 1] = -TRAVEL[TRVS - 1];
268 /* Here we read in the vocabulary. KTAB(N) is the word number, ATAB(N) is
269 * the corresponding word. The -1 at the end of section 4 is left in KTAB
270 * as an end-marker. */
271 static void read_vocabulary(FILE* database)
273 for (TABNDX = 1; TABNDX <= TABSIZ; TABNDX++) {
274 KTAB[TABNDX] = GETNUM(database);
275 if (KTAB[TABNDX] == -1) return;
276 ATAB[TABNDX] = GETTXT(true, true, true);
278 BUG(TOO_MANY_VOCABULARY_WORDS);
281 /* Read in the initial locations for each object. Also the immovability info.
282 * plac contains initial locations of objects. FIXD is -1 for immovable
283 * objects (including the snake), or = second loc for two-placed objects. */
284 static void read_initial_locations(FILE* database)
287 while ((OBJ = GETNUM(database)) != -1) {
288 PLAC[OBJ] = GETNUM(NULL);
289 FIXD[OBJ] = GETNUM(NULL);
293 /* Read default message numbers for action verbs, store in ACTSPK. */
294 static void read_action_verb_message_nr(FILE* database)
297 while ((verb = GETNUM(database)) != -1) {
298 ACTSPK[verb] = GETNUM(NULL);
302 /* Read info about available liquids and other conditions, store in COND. */
303 static void read_conditions(FILE* database)
306 while ((K = GETNUM(database)) != -1) {
308 while ((loc = GETNUM(NULL)) != 0) {
309 if (is_set(COND[loc], K))
310 BUG(LOCATION_HAS_CONDITION_BIT_BEING_SET_TWICE);
311 COND[loc] = COND[loc] + (1l << K);
317 /* Read data for hints. */
318 static void read_hints(FILE* database)
322 while ((K = GETNUM(database)) != -1) {
323 if (K <= 0 || K > HNTSIZ)
325 for (int I = 1; I <= 4; I++) {
326 /* consume - actual arrqy-building now done in YAML. */
329 HNTMAX = (HNTMAX > K ? HNTMAX : K);
333 /* Read the sound/text info, store in OBJSND, OBJTXT, LOCSND. */
334 static void read_sound_text(FILE* database)
337 while ((K = GETNUM(database)) != -1) {
338 long KK = GETNUM(NULL);
339 long I = GETNUM(NULL);
341 OBJSND[K] = (KK > 0 ? KK : 0);
342 OBJTXT[K] = (I > 0 ? I : 0);
351 static int read_database(FILE* database)
353 /* Clear out the various text-pointer arrays. All text is stored
354 * in array lines; each line is preceded by a word pointing to
355 * the next pointer (i.e. the word following the end of the
356 * line). The pointer is negative if this is first line of a
357 * message. The text-pointer arrays contain indices of
358 * pointer-words in lines. STEXT(N) is short description of
359 * location N. LTEXT(N) is long description. PTEXT(N) points to
360 * message for game.prop(N)=0. Successive prop messages are
361 * found by chasing pointers. RTEXT contains section 6's stuff.
362 * TTEXT is for section 14. We also clear COND (see description
363 * of section 9 for details). */
364 for (int I = 1; I <= NOBJECTS; I++) {
369 for (int I = 1; I <= RTXSIZ; I++) {
372 for (int I = 1; I <= LOCSIZ; I++) {
384 /* Start new data section. Sect is the section number. */
387 long sect = GETNUM(database);
393 read_messages(database, sect);
396 read_messages(database, sect);
399 read_section3_stuff(database);
402 read_vocabulary(database);
405 read_messages(database, sect);
408 read_messages(database, sect);
411 read_initial_locations(database);
414 read_action_verb_message_nr(database);
417 read_conditions(database);
420 read_messages(database, sect);
423 read_hints(database);
428 read_sound_text(database);
431 read_messages(database, sect);
434 BUG(INVALID_SECTION_NUMBER_IN_DATABASE);
439 /* Finish constructing internal data format */
441 /* Having read in the database, certain things are now constructed.
442 * game.propS are set to zero. We finish setting up COND by checking for
443 * forced-motion travel entries. The PLAC and FIXD arrays are used
444 * to set up game.atloc(N) as the first object at location N, and
445 * game.link(OBJ) as the next object at the same location as OBJ.
446 * (OBJ>NOBJECTS indicates that game.fixed(OBJ-NOBJECTS)=LOC; game.link(OBJ) is
447 * still the correct link to use.) game.abbrev is zeroed; it controls
448 * whether the abbreviated description is printed. Counts modulo 5
449 * unless "LOOK" is used. */
451 static void write_0d(FILE* header_file, long single, const char* varname)
453 fprintf(header_file, "LOCATION long %s INITIALIZE(= %ld);\n", varname, single);
456 static void write_1d(FILE* header_file, long array[], long dim, const char* varname)
458 fprintf(header_file, "LOCATION long %s[] INITIALIZE(= {\n", varname);
459 for (int i = 0; i < dim; ++i) {
462 fprintf(header_file, "\n");
463 fprintf(header_file, " ");
465 fprintf(header_file, "%ld, ", array[i]);
467 fprintf(header_file, "\n});\n");
470 static void write_file(FILE* header_file)
472 fprintf(header_file, "#ifndef DATABASE_H\n");
473 fprintf(header_file, "#define DATABASE_H\n");
474 fprintf(header_file, "\n");
476 fprintf(header_file, "#include \"common.h\"\n");
477 fprintf(header_file, "#define TABSIZ 330\n");
478 fprintf(header_file, "#define HNTSIZ 20\n");
479 fprintf(header_file, "#define TOKLEN %d\n", TOKLEN);
480 fprintf(header_file, "\n");
482 fprintf(header_file, "\n");
483 fprintf(header_file, "#ifdef DEFINE_GLOBALS_FROM_INCLUDES\n");
484 fprintf(header_file, "#define LOCATION\n");
485 fprintf(header_file, "#define INITIALIZE(...) __VA_ARGS__\n");
486 fprintf(header_file, "#else\n");
487 fprintf(header_file, "#define LOCATION extern\n");
488 fprintf(header_file, "#define INITIALIZE(...)\n");
489 fprintf(header_file, "#endif\n");
490 fprintf(header_file, "\n");
493 write_0d(header_file, HNTMAX, "HNTMAX");
494 write_1d(header_file, OBJSND, NOBJECTS + 1, "OBJSND");
495 write_1d(header_file, OBJTXT, NOBJECTS + 1, "OBJTXT");
496 write_1d(header_file, COND, LOCSIZ + 1, "COND");
497 write_1d(header_file, KEY, LOCSIZ + 1, "KEY");
498 write_1d(header_file, LOCSND, LOCSIZ + 1, "LOCSND");
499 write_1d(header_file, TRAVEL, TRVSIZ + 1, "TRAVEL");
500 write_1d(header_file, KTAB, TABSIZ + 1, "KTAB");
501 write_1d(header_file, ATAB, TABSIZ + 1, "ATAB");
502 write_1d(header_file, PLAC, NOBJECTS + 1, "PLAC");
503 write_1d(header_file, FIXD, NOBJECTS + 1, "FIXD");
504 write_1d(header_file, ACTSPK, VRBSIZ + 1, "ACTSPK");
506 fprintf(header_file, "#undef LOCATION\n");
507 fprintf(header_file, "#undef INITIALIZE\n");
508 fprintf(header_file, "#endif\n");
511 void bug(enum bugtype num, const char *error_string)
513 fprintf(stderr, "Fatal error %d, %s.\n", num, error_string);
519 FILE* database = fopen("adventure.text", "r");
520 read_database(database);
523 FILE* header_file = fopen("database.h", "w");
524 write_file(header_file);
527 return (EXIT_SUCCESS);