2 * The dungeon compiler. Turns adventure.text into a set of C initializers
3 * defining invariant state.
7 * 12600 words of message text (LINES, LINSIZ).
8 * 330 vocabulary words (KTAB, ATAB, TABSIZ).
9 * There are also limits which cannot be exceeded due to the structure of
10 * the database. (E.G., The vocabulary uses n/1000 to determine word type,
11 * so there can't be more than 1000 words.) These upper limits are:
12 * 1000 non-synonymous vocabulary words
17 /* Description of the database format
20 * The data file contains several sections. Each begins with a line containing
21 * a number identifying the section, and ends with a line containing "-1".
23 * Section 4: Vocabulary. Each line contains a number (n), a tab, and a
24 * five-letter word. Call M=N/1000. If M=0, then the word is a motion
25 * verb for use in travelling (see section 3). Else, if M=1, the word is
26 * an object. Else, if M=2, the word is an action verb (such as "carry"
27 * or "attack"). Else, if M=3, the word is a special case verb (such as
28 * "dig") and N % 1000 is an index into section 6. Objects from 50 to
29 * (currently, anyway) 79 are considered treasures (for pirate, closeout).
30 * Section 0: End of database.
32 * Other sections are obsolete and ignored */
48 // Global variables for use in functions below that can gradually disappear as code is cleaned up
51 static char INLINE[LINESIZE + 1];
55 // Storage for what comes out of the database
57 long LINES[LINSIZ + 1];
58 long KTAB[TABSIZ + 1];
59 long ATAB[TABSIZ + 1];
61 static long GETTXT(long SKIP, long ONEWRD, long UPPER)
63 /* Take characters from an input line and pack them into 30-bit words.
64 * Skip says to skip leading blanks. ONEWRD says stop if we come to a
65 * blank. UPPER says to map all letters to uppercase. If we reach the
66 * end of the line, the word is filled up with blanks (which encode as 0's).
67 * If we're already at end of line when GETTXT is called, we return -1. */
70 static long SPLITTING = -1;
72 if (LNPOSN != SPLITTING)
78 if ((!SKIP) || INLINE[LNPOSN] != 0)
84 for (int I = 1; I <= TOKLEN; I++) {
86 if (LNPOSN > LNLENG || (ONEWRD && INLINE[LNPOSN] == 0))
88 char current = INLINE[LNPOSN];
91 if (UPPER && current >= 37)
92 current = current - 26;
93 TEXT = TEXT + current;
97 if (SPLITTING != LNPOSN) {
103 TEXT = TEXT + current - 63;
111 static void MAPLIN(FILE *OPENED)
113 /* Read a line of input, from the specified input source,
114 * translate the chars to integers in the range 0-126 and store
115 * them in the common array "INLINE". Integer values are as follows:
116 * 0 = space [ASCII CODE 40 octal, 32 decimal]
117 * 1-2 = !" [ASCII 41-42 octal, 33-34 decimal]
118 * 3-10 = '()*+,-. [ASCII 47-56 octal, 39-46 decimal]
119 * 11-36 = upper-case letters
120 * 37-62 = lower-case letters
121 * 63 = percent (%) [ASCII 45 octal, 37 decimal]
122 * 64-73 = digits, 0 through 9
123 * Remaining characters can be translated any way that is convenient;
124 * The "TYPE" routine below is used to map them back to characters when
125 * necessary. The above mappings are required so that certain special
126 * characters are known to fit in 6 bits and/or can be easily spotted.
127 * Array elements beyond the end of the line should be filled with 0,
128 * and LNLENG should be set to the index of the last character.
130 * If the data file uses a character other than space (e.g., tab) to
131 * separate numbers, that character should also translate to 0.
133 * This procedure may use the map1,map2 arrays to maintain static data for
134 * the mapping. MAP2(1) is set to 0 when the program starts
135 * and is not changed thereafter unless the routines on this page choose
139 if (NULL == fgets(INLINE + 1, sizeof(INLINE) - 1, OPENED)) {
140 printf("Failed fgets()\n");
142 } while (!feof(OPENED) && INLINE[1] == '#');
145 for (size_t i = 1; i < sizeof(INLINE) && INLINE[i] != 0; ++i) {
146 char val = INLINE[i];
147 INLINE[i] = ascii_to_advent[(unsigned)val];
154 static long GETNUM(FILE *source)
156 /* Obtain the next integer from an input line. If K>0, we first read a
157 * new input line from a file; if K<0, we read a line from the keyboard;
158 * if K=0 we use a line that has already been read (and perhaps partially
159 * scanned). If we're at the end of the line or encounter an illegal
160 * character (not a digit, hyphen, or blank), we return 0. */
162 long DIGIT, GETNUM, sign;
164 if (source != NULL) MAPLIN(source);
167 while (INLINE[LNPOSN] == 0) {
168 if (LNPOSN > LNLENG) return (GETNUM);
172 if (INLINE[LNPOSN] != 9) {
178 while (!(LNPOSN > LNLENG || INLINE[LNPOSN] == 0)) {
179 DIGIT = INLINE[LNPOSN] - 64;
180 if (DIGIT < 0 || DIGIT > 9) {
184 GETNUM = GETNUM * 10 + DIGIT;
188 GETNUM = GETNUM * sign;
193 /* Sections 1, 2, 5, 6, 10, 14. Skip these, they're all in YAML now. */
194 static void read_messages(FILE* database)
198 if (NULL == fgets(INLINE + 1, sizeof(INLINE) - 1, database)) {
199 printf("Failed fgets()\n");
201 } while (!feof(database) && INLINE[1] == '#');
202 if (strncmp(INLINE + 1, "-1\n", 3) == 0)
207 /* The stuff for section 3 is encoded here. Each "from-location" gets a
208 * contiguous section of the "travel" array. Each entry in travel is
209 * newloc*1000 + KEYWORD (from section 4, motion verbs), and is negated if
210 * this is the last entry for this location. KEY(N) is the index in travel
211 * of the first option at location N. */
212 static void read_section3_stuff(FILE* database)
215 while ((loc = GETNUM(database)) != -1) {
216 /* Now done from YAML */
220 /* Here we read in the vocabulary. KTAB(N) is the word number, ATAB(N) is
221 * the corresponding word. The -1 at the end of section 4 is left in KTAB
222 * as an end-marker. */
223 static void read_vocabulary(FILE* database)
225 for (TABNDX = 1; TABNDX <= TABSIZ; TABNDX++) {
226 KTAB[TABNDX] = GETNUM(database);
227 if (KTAB[TABNDX] == -1) return;
228 ATAB[TABNDX] = GETTXT(true, true, true);
230 BUG(TOO_MANY_VOCABULARY_WORDS);
233 /* Read in the initial locations for each object. Also the immovability info.
234 * plac contains initial locations of objects. FIXD is -1 for immovable
235 * objects (including the snake), or = second loc for two-placed objects. */
236 static void read_initial_locations(FILE* database)
239 while ((OBJ = GETNUM(database)) != -1) {
240 /* all done from YAML now */
244 /* Read default message numbers for action verbs. */
245 static void read_action_verb_message_nr(FILE* database)
248 while ((verb = GETNUM(database)) != -1) {
249 /* now declared in YAML */
253 /* Read info about available liquids and other conditions. */
254 static void read_conditions(FILE* database)
257 while ((K = GETNUM(database)) != -1) {
259 while ((loc = GETNUM(NULL)) != 0) {
260 continue; /* COND is no longer used */
266 /* Read data for hints. */
267 static void read_hints(FILE* database)
270 while ((K = GETNUM(database)) != -1) {
271 for (int I = 1; I <= 4; I++) {
272 /* consume - actual array-building now done in YAML. */
278 /* Read the sound/text info */
279 static void read_sound_text(FILE* database)
282 while ((K = GETNUM(database)) != -1) {
283 /* this stuff is in YAML now */
288 static int read_database(FILE* database)
290 /* Clear out the various text-pointer arrays. All text is stored
291 * in array lines; each line is preceded by a word pointing to
292 * the next pointer (i.e. the word following the end of the
293 * line). The pointer is negative if this is first line of a
294 * message. The text-pointer arrays contain indices of
295 * pointer-words in lines. PTEXT(N) points to
296 * message for game.prop(N)=0. Successive prop messages are
297 * found by chasing pointers. */
301 /* Start new data section. Sect is the section number. */
304 long sect = GETNUM(database);
310 read_messages(database);
313 read_messages(database);
316 read_section3_stuff(database);
319 read_vocabulary(database);
322 read_messages(database);
325 read_messages(database);
328 read_initial_locations(database);
331 read_action_verb_message_nr(database);
334 read_conditions(database);
337 read_messages(database);
340 read_hints(database);
345 read_sound_text(database);
348 read_messages(database);
351 BUG(INVALID_SECTION_NUMBER_IN_DATABASE);
356 /* Finish constructing internal data format */
358 /* Having read in the database, certain things are now constructed.
359 * game.propS are set to zero. The PLAC and FIXD arrays are used
360 * to set up game.atloc(N) as the first object at location N, and
361 * game.link(OBJ) as the next object at the same location as OBJ.
362 * (OBJ>NOBJECTS indicates that game.fixed(OBJ-NOBJECTS)=LOC; game.link(OBJ) is
363 * still the correct link to use.) game.abbrev is zeroed; it controls
364 * whether the abbreviated description is printed. Counts modulo 5
365 * unless "LOOK" is used. */
367 static void write_1d(FILE* header_file, long array[], long dim, const char* varname)
369 fprintf(header_file, "LOCATION long %s[] INITIALIZE(= {\n", varname);
370 for (int i = 0; i < dim; ++i) {
373 fprintf(header_file, "\n");
374 fprintf(header_file, " ");
376 fprintf(header_file, "%ld, ", array[i]);
378 fprintf(header_file, "\n});\n");
381 static void write_file(FILE* header_file)
383 fprintf(header_file, "#ifndef DATABASE_H\n");
384 fprintf(header_file, "#define DATABASE_H\n");
385 fprintf(header_file, "\n");
387 fprintf(header_file, "#include \"common.h\"\n");
388 fprintf(header_file, "#define TABSIZ 330\n");
389 fprintf(header_file, "#define TOKLEN %d\n", TOKLEN);
390 fprintf(header_file, "\n");
392 fprintf(header_file, "\n");
393 fprintf(header_file, "#ifdef DEFINE_GLOBALS_FROM_INCLUDES\n");
394 fprintf(header_file, "#define LOCATION\n");
395 fprintf(header_file, "#define INITIALIZE(...) __VA_ARGS__\n");
396 fprintf(header_file, "#else\n");
397 fprintf(header_file, "#define LOCATION extern\n");
398 fprintf(header_file, "#define INITIALIZE(...)\n");
399 fprintf(header_file, "#endif\n");
400 fprintf(header_file, "\n");
403 write_1d(header_file, KTAB, TABSIZ + 1, "KTAB");
404 write_1d(header_file, ATAB, TABSIZ + 1, "ATAB");
406 fprintf(header_file, "#undef LOCATION\n");
407 fprintf(header_file, "#undef INITIALIZE\n");
408 fprintf(header_file, "#endif\n");
411 void bug(enum bugtype num, const char *error_string)
413 fprintf(stderr, "Fatal error %d, %s.\n", num, error_string);
419 FILE* database = fopen("adventure.text", "r");
420 read_database(database);
423 FILE* header_file = fopen("database.h", "w");
424 write_file(header_file);
427 return (EXIT_SUCCESS);