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
25 // Global variables for use in functions below that can gradually disappear as code is cleaned up
28 static char INLINE[LINESIZE + 1];
31 // Storage for what comes out of the database
37 long PTEXT[NOBJECTS + 1];
38 long RTEXT[RTXSIZ + 1];
39 long OBJSND[NOBJECTS + 1];
40 long OBJTXT[NOBJECTS + 1];
41 long STEXT[LOCSIZ + 1];
42 long LTEXT[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. */
303 static void read_conditions(FILE* database)
306 while ((K = GETNUM(database)) != -1) {
308 while ((loc = GETNUM(NULL)) != 0) {
309 continue; /* COND is no longer used */
315 /* Read data for hints. */
316 static void read_hints(FILE* database)
320 while ((K = GETNUM(database)) != -1) {
321 if (K <= 0 || K > HNTSIZ)
323 for (int I = 1; I <= 4; I++) {
324 /* consume - actual arrqy-building now done in YAML. */
327 HNTMAX = (HNTMAX > K ? HNTMAX : K);
331 /* Read the sound/text info, store in OBJSND, OBJTXT, LOCSND. */
332 static void read_sound_text(FILE* database)
335 while ((K = GETNUM(database)) != -1) {
336 long KK = GETNUM(NULL);
337 long I = GETNUM(NULL);
339 OBJSND[K] = (KK > 0 ? KK : 0);
340 OBJTXT[K] = (I > 0 ? I : 0);
349 static int read_database(FILE* database)
351 /* Clear out the various text-pointer arrays. All text is stored
352 * in array lines; each line is preceded by a word pointing to
353 * the next pointer (i.e. the word following the end of the
354 * line). The pointer is negative if this is first line of a
355 * message. The text-pointer arrays contain indices of
356 * pointer-words in lines. STEXT(N) is short description of
357 * location N. LTEXT(N) is long description. PTEXT(N) points to
358 * message for game.prop(N)=0. Successive prop messages are
359 * found by chasing pointers. RTEXT contains section 6's stuff.
360 * TTEXT is for section 14. */
361 for (int I = 1; I <= NOBJECTS; I++) {
366 for (int I = 1; I <= RTXSIZ; I++) {
369 for (int I = 1; I <= LOCSIZ; I++) {
380 /* Start new data section. Sect is the section number. */
383 long sect = GETNUM(database);
389 read_messages(database, sect);
392 read_messages(database, sect);
395 read_section3_stuff(database);
398 read_vocabulary(database);
401 read_messages(database, sect);
404 read_messages(database, sect);
407 read_initial_locations(database);
410 read_action_verb_message_nr(database);
413 read_conditions(database);
416 read_messages(database, sect);
419 read_hints(database);
424 read_sound_text(database);
427 read_messages(database, sect);
430 BUG(INVALID_SECTION_NUMBER_IN_DATABASE);
435 /* Finish constructing internal data format */
437 /* Having read in the database, certain things are now constructed.
438 * game.propS are set to zero. The PLAC and FIXD arrays are used
439 * to set up game.atloc(N) as the first object at location N, and
440 * game.link(OBJ) as the next object at the same location as OBJ.
441 * (OBJ>NOBJECTS indicates that game.fixed(OBJ-NOBJECTS)=LOC; game.link(OBJ) is
442 * still the correct link to use.) game.abbrev is zeroed; it controls
443 * whether the abbreviated description is printed. Counts modulo 5
444 * unless "LOOK" is used. */
446 static void write_1d(FILE* header_file, long array[], long dim, const char* varname)
448 fprintf(header_file, "LOCATION long %s[] INITIALIZE(= {\n", varname);
449 for (int i = 0; i < dim; ++i) {
452 fprintf(header_file, "\n");
453 fprintf(header_file, " ");
455 fprintf(header_file, "%ld, ", array[i]);
457 fprintf(header_file, "\n});\n");
460 static void write_file(FILE* header_file)
462 fprintf(header_file, "#ifndef DATABASE_H\n");
463 fprintf(header_file, "#define DATABASE_H\n");
464 fprintf(header_file, "\n");
466 fprintf(header_file, "#include \"common.h\"\n");
467 fprintf(header_file, "#define TABSIZ 330\n");
468 fprintf(header_file, "#define HNTSIZ 20\n");
469 fprintf(header_file, "#define TOKLEN %d\n", TOKLEN);
470 fprintf(header_file, "\n");
472 fprintf(header_file, "\n");
473 fprintf(header_file, "#ifdef DEFINE_GLOBALS_FROM_INCLUDES\n");
474 fprintf(header_file, "#define LOCATION\n");
475 fprintf(header_file, "#define INITIALIZE(...) __VA_ARGS__\n");
476 fprintf(header_file, "#else\n");
477 fprintf(header_file, "#define LOCATION extern\n");
478 fprintf(header_file, "#define INITIALIZE(...)\n");
479 fprintf(header_file, "#endif\n");
480 fprintf(header_file, "\n");
483 write_1d(header_file, OBJSND, NOBJECTS + 1, "OBJSND");
484 write_1d(header_file, OBJTXT, NOBJECTS + 1, "OBJTXT");
485 write_1d(header_file, KEY, LOCSIZ + 1, "KEY");
486 write_1d(header_file, LOCSND, LOCSIZ + 1, "LOCSND");
487 write_1d(header_file, TRAVEL, TRVSIZ + 1, "TRAVEL");
488 write_1d(header_file, KTAB, TABSIZ + 1, "KTAB");
489 write_1d(header_file, ATAB, TABSIZ + 1, "ATAB");
490 write_1d(header_file, PLAC, NOBJECTS + 1, "PLAC");
491 write_1d(header_file, FIXD, NOBJECTS + 1, "FIXD");
492 write_1d(header_file, ACTSPK, VRBSIZ + 1, "ACTSPK");
494 fprintf(header_file, "#undef LOCATION\n");
495 fprintf(header_file, "#undef INITIALIZE\n");
496 fprintf(header_file, "#endif\n");
499 void bug(enum bugtype num, const char *error_string)
501 fprintf(stderr, "Fatal error %d, %s.\n", num, error_string);
507 FILE* database = fopen("adventure.text", "r");
508 read_database(database);
511 FILE* header_file = fopen("database.h", "w");
512 write_file(header_file);
515 return (EXIT_SUCCESS);