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];
43 long COND[LOCSIZ + 1];
45 long LOCSND[LOCSIZ + 1];
46 long LINES[LINSIZ + 1];
47 long TTEXT[TRNSIZ + 1];
48 long TRNVAL[TRNSIZ + 1];
49 long TRAVEL[TRVSIZ + 1];
50 long KTAB[TABSIZ + 1];
51 long ATAB[TABSIZ + 1];
52 long PLAC[NOBJECTS + 1];
53 long FIXD[NOBJECTS + 1];
54 long ACTSPK[VRBSIZ + 1];
56 static bool is_set(long var, long position)
58 long mask = 1L << position;
59 bool result = (var & mask) == mask;
63 static long GETTXT(long SKIP, long ONEWRD, long UPPER)
65 /* Take characters from an input line and pack them into 30-bit words.
66 * Skip says to skip leading blanks. ONEWRD says stop if we come to a
67 * blank. UPPER says to map all letters to uppercase. If we reach the
68 * end of the line, the word is filled up with blanks (which encode as 0's).
69 * If we're already at end of line when GETTXT is called, we return -1. */
72 static long SPLITTING = -1;
74 if (LNPOSN != SPLITTING)
80 if ((!SKIP) || INLINE[LNPOSN] != 0)
86 for (int I = 1; I <= TOKLEN; I++) {
88 if (LNPOSN > LNLENG || (ONEWRD && INLINE[LNPOSN] == 0))
90 char current = INLINE[LNPOSN];
93 if (UPPER && current >= 37)
94 current = current - 26;
95 TEXT = TEXT + current;
99 if (SPLITTING != LNPOSN) {
105 TEXT = TEXT + current - 63;
113 static void MAPLIN(FILE *OPENED)
115 /* Read a line of input, from the specified input source,
116 * translate the chars to integers in the range 0-126 and store
117 * them in the common array "INLINE". Integer values are as follows:
118 * 0 = space [ASCII CODE 40 octal, 32 decimal]
119 * 1-2 = !" [ASCII 41-42 octal, 33-34 decimal]
120 * 3-10 = '()*+,-. [ASCII 47-56 octal, 39-46 decimal]
121 * 11-36 = upper-case letters
122 * 37-62 = lower-case letters
123 * 63 = percent (%) [ASCII 45 octal, 37 decimal]
124 * 64-73 = digits, 0 through 9
125 * Remaining characters can be translated any way that is convenient;
126 * The "TYPE" routine below is used to map them back to characters when
127 * necessary. The above mappings are required so that certain special
128 * characters are known to fit in 6 bits and/or can be easily spotted.
129 * Array elements beyond the end of the line should be filled with 0,
130 * and LNLENG should be set to the index of the last character.
132 * If the data file uses a character other than space (e.g., tab) to
133 * separate numbers, that character should also translate to 0.
135 * This procedure may use the map1,map2 arrays to maintain static data for
136 * the mapping. MAP2(1) is set to 0 when the program starts
137 * and is not changed thereafter unless the routines on this page choose
141 if (NULL == fgets(INLINE + 1, sizeof(INLINE) - 1, OPENED)) {
142 printf("Failed fgets()\n");
144 } while (!feof(OPENED) && INLINE[1] == '#');
147 for (size_t i = 1; i < sizeof(INLINE) && INLINE[i] != 0; ++i) {
148 char val = INLINE[i];
149 INLINE[i] = ascii_to_advent[(unsigned)val];
156 static long GETNUM(FILE *source)
158 /* Obtain the next integer from an input line. If K>0, we first read a
159 * new input line from a file; if K<0, we read a line from the keyboard;
160 * if K=0 we use a line that has already been read (and perhaps partially
161 * scanned). If we're at the end of the line or encounter an illegal
162 * character (not a digit, hyphen, or blank), we return 0. */
164 long DIGIT, GETNUM, SIGN;
166 if (source != NULL) MAPLIN(source);
169 while (INLINE[LNPOSN] == 0) {
170 if (LNPOSN > LNLENG) return (GETNUM);
174 if (INLINE[LNPOSN] != 9) {
180 while (!(LNPOSN > LNLENG || INLINE[LNPOSN] == 0)) {
181 DIGIT = INLINE[LNPOSN] - 64;
182 if (DIGIT < 0 || DIGIT > 9) {
186 GETNUM = GETNUM * 10 + DIGIT;
190 GETNUM = GETNUM * SIGN;
195 /* Sections 1, 2, 5, 6, 10, 14. Read messages and set up pointers. */
196 static void read_messages(FILE* database, long sect)
202 loc = GETNUM(database);
203 if (LNLENG >= LNPOSN + 70)
204 BUG(MESSAGE_LINE_GT_70_CHARACTERS);
205 if (loc == -1) return;
207 BUG(NULL_LINE_IN_MESSAGE);
211 BUG(TOO_MANY_WORDS_OF_MESSAGES);
212 LINES[KK] = GETTXT(false, false, false);
213 } while (LINES[KK] != -1);
215 if (loc == OLDLOC) continue;
218 if (sect == 10 || sect == 14) {
219 /* now parsed from YAML */
224 BUG(TOO_MANY_RTEXT_MESSAGES);
229 if (loc > 0 && loc <= NOBJECTS)PTEXT[loc] = LINUSE;
233 BUG(TOO_MANY_LOCATIONS);
243 /* The stuff for section 3 is encoded here. Each "from-location" gets a
244 * contiguous section of the "TRAVEL" array. Each entry in travel is
245 * newloc*1000 + KEYWORD (from section 4, motion verbs), and is negated if
246 * this is the last entry for this location. KEY(N) is the index in travel
247 * of the first option at location N. */
248 static void read_section3_stuff(FILE* database)
251 while ((loc = GETNUM(database)) != -1) {
252 long newloc = GETNUM(NULL);
257 TRAVEL[TRVS - 1] = -TRAVEL[TRVS - 1];
259 while ((L = GETNUM(NULL)) != 0) {
260 TRAVEL[TRVS] = newloc * 1000 + L;
263 BUG(TOO_MANY_TRAVEL_OPTIONS);
265 TRAVEL[TRVS - 1] = -TRAVEL[TRVS - 1];
269 /* Here we read in the vocabulary. KTAB(N) is the word number, ATAB(N) is
270 * the corresponding word. The -1 at the end of section 4 is left in KTAB
271 * as an end-marker. */
272 static void read_vocabulary(FILE* database)
274 for (TABNDX = 1; TABNDX <= TABSIZ; TABNDX++) {
275 KTAB[TABNDX] = GETNUM(database);
276 if (KTAB[TABNDX] == -1) return;
277 ATAB[TABNDX] = GETTXT(true, true, true);
279 BUG(TOO_MANY_VOCABULARY_WORDS);
282 /* Read in the initial locations for each object. Also the immovability info.
283 * plac contains initial locations of objects. FIXD is -1 for immovable
284 * objects (including the snake), or = second loc for two-placed objects. */
285 static void read_initial_locations(FILE* database)
288 while ((OBJ = GETNUM(database)) != -1) {
289 PLAC[OBJ] = GETNUM(NULL);
290 FIXD[OBJ] = GETNUM(NULL);
294 /* Read default message numbers for action verbs, store in ACTSPK. */
295 static void read_action_verb_message_nr(FILE* database)
298 while ((verb = GETNUM(database)) != -1) {
299 ACTSPK[verb] = GETNUM(NULL);
303 /* Read info about available liquids and other conditions, store in COND. */
304 static void read_conditions(FILE* database)
307 while ((K = GETNUM(database)) != -1) {
309 while ((loc = GETNUM(NULL)) != 0) {
310 if (is_set(COND[loc], K))
311 BUG(LOCATION_HAS_CONDITION_BIT_BEING_SET_TWICE);
312 COND[loc] = COND[loc] + (1L << K);
318 /* Read data for hints. */
319 static void read_hints(FILE* database)
323 while ((K = GETNUM(database)) != -1) {
324 if (K <= 0 || K > HNTSIZ)
326 for (int I = 1; I <= 4; I++) {
327 /* consume - actual arrqy-building now done in YAML. */
330 HNTMAX = (HNTMAX > K ? HNTMAX : K);
334 /* Read the sound/text info, store in OBJSND, OBJTXT, LOCSND. */
335 static void read_sound_text(FILE* database)
338 while ((K = GETNUM(database)) != -1) {
339 long KK = GETNUM(NULL);
340 long I = GETNUM(NULL);
342 OBJSND[K] = (KK > 0 ? KK : 0);
343 OBJTXT[K] = (I > 0 ? I : 0);
352 static int read_database(FILE* database)
354 /* Clear out the various text-pointer arrays. All text is stored
355 * in array lines; each line is preceded by a word pointing to
356 * the next pointer (i.e. the word following the end of the
357 * line). The pointer is negative if this is first line of a
358 * message. The text-pointer arrays contain indices of
359 * pointer-words in lines. STEXT(N) is short description of
360 * location N. LTEXT(N) is long description. PTEXT(N) points to
361 * message for game.prop(N)=0. Successive prop messages are
362 * found by chasing pointers. RTEXT contains section 6's stuff.
363 * TTEXT is for section 14. We also clear COND (see description
364 * of section 9 for details). */
365 for (int I = 1; I <= NOBJECTS; I++) {
370 for (int I = 1; I <= RTXSIZ; I++) {
373 for (int I = 1; I <= LOCSIZ; I++) {
385 /* Start new data section. Sect is the section number. */
388 long sect = GETNUM(database);
394 read_messages(database, sect);
397 read_messages(database, sect);
400 read_section3_stuff(database);
403 read_vocabulary(database);
406 read_messages(database, sect);
409 read_messages(database, sect);
412 read_initial_locations(database);
415 read_action_verb_message_nr(database);
418 read_conditions(database);
421 read_messages(database, sect);
424 read_hints(database);
429 read_sound_text(database);
432 read_messages(database, sect);
435 BUG(INVALID_SECTION_NUMBER_IN_DATABASE);
440 /* Finish constructing internal data format */
442 /* Having read in the database, certain things are now constructed.
443 * game.propS are set to zero. We finish setting up COND by checking for
444 * forced-motion travel entries. The PLAC and FIXD arrays are used
445 * to set up game.atloc(N) as the first object at location N, and
446 * game.link(OBJ) as the next object at the same location as OBJ.
447 * (OBJ>NOBJECTS indicates that game.fixed(OBJ-NOBJECTS)=LOC; game.link(OBJ) is
448 * still the correct link to use.) game.abbrev is zeroed; it controls
449 * whether the abbreviated description is printed. Counts modulo 5
450 * unless "LOOK" is used. */
452 static void write_1d(FILE* header_file, long array[], long dim, const char* varname)
454 fprintf(header_file, "LOCATION long %s[] INITIALIZE(= {\n", varname);
455 for (int i = 0; i < dim; ++i) {
458 fprintf(header_file, "\n");
459 fprintf(header_file, " ");
461 fprintf(header_file, "%ld, ", array[i]);
463 fprintf(header_file, "\n});\n");
466 static void write_file(FILE* header_file)
468 fprintf(header_file, "#ifndef DATABASE_H\n");
469 fprintf(header_file, "#define DATABASE_H\n");
470 fprintf(header_file, "\n");
472 fprintf(header_file, "#include \"common.h\"\n");
473 fprintf(header_file, "#define TABSIZ 330\n");
474 fprintf(header_file, "#define HNTSIZ 20\n");
475 fprintf(header_file, "#define TOKLEN %d\n", TOKLEN);
476 fprintf(header_file, "\n");
478 fprintf(header_file, "\n");
479 fprintf(header_file, "#ifdef DEFINE_GLOBALS_FROM_INCLUDES\n");
480 fprintf(header_file, "#define LOCATION\n");
481 fprintf(header_file, "#define INITIALIZE(...) __VA_ARGS__\n");
482 fprintf(header_file, "#else\n");
483 fprintf(header_file, "#define LOCATION extern\n");
484 fprintf(header_file, "#define INITIALIZE(...)\n");
485 fprintf(header_file, "#endif\n");
486 fprintf(header_file, "\n");
489 write_1d(header_file, OBJSND, NOBJECTS + 1, "OBJSND");
490 write_1d(header_file, OBJTXT, NOBJECTS + 1, "OBJTXT");
491 write_1d(header_file, COND, LOCSIZ + 1, "COND");
492 write_1d(header_file, KEY, LOCSIZ + 1, "KEY");
493 write_1d(header_file, LOCSND, LOCSIZ + 1, "LOCSND");
494 write_1d(header_file, TRAVEL, TRVSIZ + 1, "TRAVEL");
495 write_1d(header_file, KTAB, TABSIZ + 1, "KTAB");
496 write_1d(header_file, ATAB, TABSIZ + 1, "ATAB");
497 write_1d(header_file, PLAC, NOBJECTS + 1, "PLAC");
498 write_1d(header_file, FIXD, NOBJECTS + 1, "FIXD");
499 write_1d(header_file, ACTSPK, VRBSIZ + 1, "ACTSPK");
501 fprintf(header_file, "#undef LOCATION\n");
502 fprintf(header_file, "#undef INITIALIZE\n");
503 fprintf(header_file, "#endif\n");
506 void bug(enum bugtype num, const char *error_string)
508 fprintf(stderr, "Fatal error %d, %s.\n", num, error_string);
514 FILE* database = fopen("adventure.text", "r");
515 read_database(database);
518 FILE* header_file = fopen("database.h", "w");
519 write_file(header_file);
522 return (EXIT_SUCCESS);