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
22 // Global variables for use in functions below that can gradually disappear as code is cleaned up
25 static char INLINE[LINESIZE + 1];
28 // Storage for what comes out of the database
33 long OBJSND[NOBJECTS + 1];
34 long OBJTXT[NOBJECTS + 1];
36 long LOCSND[LOCSIZ + 1];
37 long LINES[LINSIZ + 1];
38 long TRAVEL[TRVSIZ + 1];
39 long KTAB[TABSIZ + 1];
40 long ATAB[TABSIZ + 1];
41 long PLAC[NOBJECTS + 1];
42 long FIXD[NOBJECTS + 1];
43 long ACTSPK[VRBSIZ + 1];
45 static long GETTXT(long SKIP, long ONEWRD, long UPPER)
47 /* Take characters from an input line and pack them into 30-bit words.
48 * Skip says to skip leading blanks. ONEWRD says stop if we come to a
49 * blank. UPPER says to map all letters to uppercase. If we reach the
50 * end of the line, the word is filled up with blanks (which encode as 0's).
51 * If we're already at end of line when GETTXT is called, we return -1. */
54 static long SPLITTING = -1;
56 if (LNPOSN != SPLITTING)
62 if ((!SKIP) || INLINE[LNPOSN] != 0)
68 for (int I = 1; I <= TOKLEN; I++) {
70 if (LNPOSN > LNLENG || (ONEWRD && INLINE[LNPOSN] == 0))
72 char current = INLINE[LNPOSN];
75 if (UPPER && current >= 37)
76 current = current - 26;
77 TEXT = TEXT + current;
81 if (SPLITTING != LNPOSN) {
87 TEXT = TEXT + current - 63;
95 static void MAPLIN(FILE *OPENED)
97 /* Read a line of input, from the specified input source,
98 * translate the chars to integers in the range 0-126 and store
99 * them in the common array "INLINE". Integer values are as follows:
100 * 0 = space [ASCII CODE 40 octal, 32 decimal]
101 * 1-2 = !" [ASCII 41-42 octal, 33-34 decimal]
102 * 3-10 = '()*+,-. [ASCII 47-56 octal, 39-46 decimal]
103 * 11-36 = upper-case letters
104 * 37-62 = lower-case letters
105 * 63 = percent (%) [ASCII 45 octal, 37 decimal]
106 * 64-73 = digits, 0 through 9
107 * Remaining characters can be translated any way that is convenient;
108 * The "TYPE" routine below is used to map them back to characters when
109 * necessary. The above mappings are required so that certain special
110 * characters are known to fit in 6 bits and/or can be easily spotted.
111 * Array elements beyond the end of the line should be filled with 0,
112 * and LNLENG should be set to the index of the last character.
114 * If the data file uses a character other than space (e.g., tab) to
115 * separate numbers, that character should also translate to 0.
117 * This procedure may use the map1,map2 arrays to maintain static data for
118 * the mapping. MAP2(1) is set to 0 when the program starts
119 * and is not changed thereafter unless the routines on this page choose
123 if (NULL == fgets(INLINE + 1, sizeof(INLINE) - 1, OPENED)) {
124 printf("Failed fgets()\n");
126 } while (!feof(OPENED) && INLINE[1] == '#');
129 for (size_t i = 1; i < sizeof(INLINE) && INLINE[i] != 0; ++i) {
130 char val = INLINE[i];
131 INLINE[i] = ascii_to_advent[(unsigned)val];
138 static long GETNUM(FILE *source)
140 /* Obtain the next integer from an input line. If K>0, we first read a
141 * new input line from a file; if K<0, we read a line from the keyboard;
142 * if K=0 we use a line that has already been read (and perhaps partially
143 * scanned). If we're at the end of the line or encounter an illegal
144 * character (not a digit, hyphen, or blank), we return 0. */
146 long DIGIT, GETNUM, SIGN;
148 if (source != NULL) MAPLIN(source);
151 while (INLINE[LNPOSN] == 0) {
152 if (LNPOSN > LNLENG) return (GETNUM);
156 if (INLINE[LNPOSN] != 9) {
162 while (!(LNPOSN > LNLENG || INLINE[LNPOSN] == 0)) {
163 DIGIT = INLINE[LNPOSN] - 64;
164 if (DIGIT < 0 || DIGIT > 9) {
168 GETNUM = GETNUM * 10 + DIGIT;
172 GETNUM = GETNUM * SIGN;
177 /* Sections 1, 2, 5, 6, 10, 14. Read messages and set up pointers. */
178 static void read_messages(FILE* database, long sect)
184 loc = GETNUM(database);
185 if (LNLENG >= LNPOSN + 70)
186 BUG(MESSAGE_LINE_GT_70_CHARACTERS);
187 if (loc == -1) return;
189 BUG(NULL_LINE_IN_MESSAGE);
193 BUG(TOO_MANY_WORDS_OF_MESSAGES);
194 LINES[KK] = GETTXT(false, false, false);
195 } while (LINES[KK] != -1);
197 if (loc == OLDLOC) continue;
200 if (sect == 10 || sect == 14) {
201 /* now parsed from YAML */
205 /* Now handled in YAML */
209 /* Now handled in YAML */
213 /* Now handled in YAML */
219 /* The stuff for section 3 is encoded here. Each "from-location" gets a
220 * contiguous section of the "TRAVEL" array. Each entry in travel is
221 * newloc*1000 + KEYWORD (from section 4, motion verbs), and is negated if
222 * this is the last entry for this location. KEY(N) is the index in travel
223 * of the first option at location N. */
224 static void read_section3_stuff(FILE* database)
227 while ((loc = GETNUM(database)) != -1) {
228 long newloc = GETNUM(NULL);
233 TRAVEL[TRVS - 1] = -TRAVEL[TRVS - 1];
235 while ((L = GETNUM(NULL)) != 0) {
236 TRAVEL[TRVS] = newloc * 1000 + L;
239 BUG(TOO_MANY_TRAVEL_OPTIONS);
241 TRAVEL[TRVS - 1] = -TRAVEL[TRVS - 1];
245 /* Here we read in the vocabulary. KTAB(N) is the word number, ATAB(N) is
246 * the corresponding word. The -1 at the end of section 4 is left in KTAB
247 * as an end-marker. */
248 static void read_vocabulary(FILE* database)
250 for (TABNDX = 1; TABNDX <= TABSIZ; TABNDX++) {
251 KTAB[TABNDX] = GETNUM(database);
252 if (KTAB[TABNDX] == -1) return;
253 ATAB[TABNDX] = GETTXT(true, true, true);
255 BUG(TOO_MANY_VOCABULARY_WORDS);
258 /* Read in the initial locations for each object. Also the immovability info.
259 * plac contains initial locations of objects. FIXD is -1 for immovable
260 * objects (including the snake), or = second loc for two-placed objects. */
261 static void read_initial_locations(FILE* database)
264 while ((OBJ = GETNUM(database)) != -1) {
265 PLAC[OBJ] = GETNUM(NULL);
266 FIXD[OBJ] = GETNUM(NULL);
270 /* Read default message numbers for action verbs, store in ACTSPK. */
271 static void read_action_verb_message_nr(FILE* database)
274 while ((verb = GETNUM(database)) != -1) {
275 ACTSPK[verb] = GETNUM(NULL);
279 /* Read info about available liquids and other conditions. */
280 static void read_conditions(FILE* database)
283 while ((K = GETNUM(database)) != -1) {
285 while ((loc = GETNUM(NULL)) != 0) {
286 continue; /* COND is no longer used */
292 /* Read data for hints. */
293 static void read_hints(FILE* database)
296 while ((K = GETNUM(database)) != -1) {
297 for (int I = 1; I <= 4; I++) {
298 /* consume - actual array-building now done in YAML. */
304 /* Read the sound/text info, store in OBJSND, OBJTXT, LOCSND. */
305 static void read_sound_text(FILE* database)
308 while ((K = GETNUM(database)) != -1) {
309 long KK = GETNUM(NULL);
310 long I = GETNUM(NULL);
312 OBJSND[K] = (KK > 0 ? KK : 0);
313 OBJTXT[K] = (I > 0 ? I : 0);
322 static int read_database(FILE* database)
324 /* Clear out the various text-pointer arrays. All text is stored
325 * in array lines; each line is preceded by a word pointing to
326 * the next pointer (i.e. the word following the end of the
327 * line). The pointer is negative if this is first line of a
328 * message. The text-pointer arrays contain indices of
329 * pointer-words in lines. PTEXT(N) points to
330 * message for game.prop(N)=0. Successive prop messages are
331 * found by chasing pointers. */
332 for (int I = 1; I <= NOBJECTS; I++) {
336 for (int I = 1; I <= LOCSIZ; I++) {
345 /* Start new data section. Sect is the section number. */
348 long sect = GETNUM(database);
354 read_messages(database, sect);
357 read_messages(database, sect);
360 read_section3_stuff(database);
363 read_vocabulary(database);
366 read_messages(database, sect);
369 read_messages(database, sect);
372 read_initial_locations(database);
375 read_action_verb_message_nr(database);
378 read_conditions(database);
381 read_messages(database, sect);
384 read_hints(database);
389 read_sound_text(database);
392 read_messages(database, sect);
395 BUG(INVALID_SECTION_NUMBER_IN_DATABASE);
400 /* Finish constructing internal data format */
402 /* Having read in the database, certain things are now constructed.
403 * game.propS are set to zero. The PLAC and FIXD arrays are used
404 * to set up game.atloc(N) as the first object at location N, and
405 * game.link(OBJ) as the next object at the same location as OBJ.
406 * (OBJ>NOBJECTS indicates that game.fixed(OBJ-NOBJECTS)=LOC; game.link(OBJ) is
407 * still the correct link to use.) game.abbrev is zeroed; it controls
408 * whether the abbreviated description is printed. Counts modulo 5
409 * unless "LOOK" is used. */
411 static void write_1d(FILE* header_file, long array[], long dim, const char* varname)
413 fprintf(header_file, "LOCATION long %s[] INITIALIZE(= {\n", varname);
414 for (int i = 0; i < dim; ++i) {
417 fprintf(header_file, "\n");
418 fprintf(header_file, " ");
420 fprintf(header_file, "%ld, ", array[i]);
422 fprintf(header_file, "\n});\n");
425 static void write_file(FILE* header_file)
427 fprintf(header_file, "#ifndef DATABASE_H\n");
428 fprintf(header_file, "#define DATABASE_H\n");
429 fprintf(header_file, "\n");
431 fprintf(header_file, "#include \"common.h\"\n");
432 fprintf(header_file, "#define TABSIZ 330\n");
433 fprintf(header_file, "#define TOKLEN %d\n", TOKLEN);
434 fprintf(header_file, "\n");
436 fprintf(header_file, "\n");
437 fprintf(header_file, "#ifdef DEFINE_GLOBALS_FROM_INCLUDES\n");
438 fprintf(header_file, "#define LOCATION\n");
439 fprintf(header_file, "#define INITIALIZE(...) __VA_ARGS__\n");
440 fprintf(header_file, "#else\n");
441 fprintf(header_file, "#define LOCATION extern\n");
442 fprintf(header_file, "#define INITIALIZE(...)\n");
443 fprintf(header_file, "#endif\n");
444 fprintf(header_file, "\n");
447 write_1d(header_file, OBJSND, NOBJECTS + 1, "OBJSND");
448 write_1d(header_file, OBJTXT, NOBJECTS + 1, "OBJTXT");
449 write_1d(header_file, KEY, LOCSIZ + 1, "KEY");
450 write_1d(header_file, LOCSND, LOCSIZ + 1, "LOCSND");
451 write_1d(header_file, TRAVEL, TRVSIZ + 1, "TRAVEL");
452 write_1d(header_file, KTAB, TABSIZ + 1, "KTAB");
453 write_1d(header_file, ATAB, TABSIZ + 1, "ATAB");
454 write_1d(header_file, PLAC, NOBJECTS + 1, "PLAC");
455 write_1d(header_file, FIXD, NOBJECTS + 1, "FIXD");
456 write_1d(header_file, ACTSPK, VRBSIZ + 1, "ACTSPK");
458 fprintf(header_file, "#undef LOCATION\n");
459 fprintf(header_file, "#undef INITIALIZE\n");
460 fprintf(header_file, "#endif\n");
463 void bug(enum bugtype num, const char *error_string)
465 fprintf(stderr, "Fatal error %d, %s.\n", num, error_string);
471 FILE* database = fopen("adventure.text", "r");
472 read_database(database);
475 FILE* header_file = fopen("database.h", "w");
476 write_file(header_file);
479 return (EXIT_SUCCESS);