8 #include <editline/readline.h>
13 char* xstrdup(const char* s)
15 char* ptr = strdup(s);
18 // exclude from coverage analysis because we can't simulate an out of memory error in testing
19 fprintf(stderr, "Out of memory!\n");
25 void* xmalloc(size_t size)
27 void* ptr = malloc(size);
30 // exclude from coverage analysis because we can't simulate an out of memory error in testing
31 fprintf(stderr, "Out of memory!\n");
38 void packed_to_token(long packed, char token[6])
40 // The advent->ascii mapping.
41 const char advent_to_ascii[] = {
42 ' ', '!', '"', '#', '$', '%', '&', '\'',
43 '(', ')', '*', '+', ',', '-', '.', '/',
44 '0', '1', '2', '3', '4', '5', '6', '7',
45 '8', '9', ':', ';', '<', '=', '>', '?',
46 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
47 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
48 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
49 'X', 'Y', 'Z', '\0', '\0', '\0', '\0', '\0',
52 // Unpack and map back to ASCII.
53 for (int i = 0; i < 5; ++i) {
54 char advent = (packed >> i * 6) & 63;
55 token[i] = advent_to_ascii[(int) advent];
58 // Ensure the last character is \0.
61 // Replace trailing whitespace with \0.
62 for (int i = 4; i >= 0; --i) {
63 if (token[i] == ' ' || token[i] == '\t')
70 long token_to_packed(const char token[6])
72 const char ascii_to_advent[] = {
73 63, 63, 63, 63, 63, 63, 63, 63,
74 63, 63, 63, 63, 63, 63, 63, 63,
75 63, 63, 63, 63, 63, 63, 63, 63,
76 63, 63, 63, 63, 63, 63, 63, 63,
78 0, 1, 2, 3, 4, 5, 6, 7,
79 8, 9, 10, 11, 12, 13, 14, 15,
80 16, 17, 18, 19, 20, 21, 22, 23,
81 24, 25, 26, 27, 28, 29, 30, 31,
82 32, 33, 34, 35, 36, 37, 38, 39,
83 40, 41, 42, 43, 44, 45, 46, 47,
84 48, 49, 50, 51, 52, 53, 54, 55,
85 56, 57, 58, 59, 60, 61, 62, 63,
87 63, 63, 63, 63, 63, 63, 63, 63,
88 63, 63, 63, 63, 63, 63, 63, 63,
89 63, 63, 63, 63, 63, 63, 63, 63,
90 63, 63, 63, 63, 63, 63, 63, 63,
93 size_t t_len = strlen(token);
95 for (size_t i = 0; i < t_len; ++i) {
96 char mapped = ascii_to_advent[(int) token[i]];
97 packed |= (mapped << (6 * i));
102 void tokenize(char* raw, long tokens[4])
104 // set each token to 0
105 for (int i = 0; i < 4; ++i)
108 // grab the first two words
110 words[0] = (char*) xmalloc(strlen(raw) + 1);
111 words[1] = (char*) xmalloc(strlen(raw) + 1);
112 int word_count = sscanf(raw, "%s%s", words[0], words[1]);
114 // make space for substrings and zero it out
115 char chunk_data[][6] = {
122 // break the words into up to 4 5-char substrings
123 sscanf(words[0], "%5s%5s", chunk_data[0], chunk_data[1]);
125 sscanf(words[1], "%5s%5s", chunk_data[2], chunk_data[3]);
129 // uppercase all the substrings
130 for (int i = 0; i < 4; ++i)
131 for (unsigned int j = 0; j < strlen(chunk_data[i]); ++j)
132 chunk_data[i][j] = (char) toupper(chunk_data[i][j]);
134 // pack the substrings
135 for (int i = 0; i < 4; ++i)
136 tokens[i] = token_to_packed(chunk_data[i]);
139 /* Hide the fact that wods are corrently packed longs */
141 bool wordeq(token_t a, token_t b)
146 bool wordempty(token_t a)
151 void wordclear(token_t *v)
156 /* I/O routines (speak, pspeak, rspeak, get_input, yes) */
158 void vspeak(const char* msg, va_list ap)
160 // Do nothing if we got a null pointer.
164 // Do nothing if we got an empty string.
165 if (strlen(msg) == 0)
168 // Print a newline if the global game.blklin says to.
169 if (game.blklin == true)
172 int msglen = strlen(msg);
175 ssize_t size = 2000; /* msglen > 50 ? msglen*2 : 100; */
176 char* rendered = xmalloc(size);
177 char* renderp = rendered;
179 // Handle format specifiers (including the custom %C, %L, %S) by
180 // adjusting the parameter accordingly, and replacing the
181 // specifier with %s.
182 long previous_arg = 0;
183 for (int i = 0; i < msglen; i++) {
188 long arg = va_arg(ap, long);
190 arg = 0; // LCOV_EXCL_LINE - don't think we can get here.
192 // Integer specifier. In order to accommodate the fact
193 // that PARMS can have both legitimate integers *and*
194 // packed tokens, stringify everything. Future work may
195 // eliminate the need for this.
197 int ret = snprintf(renderp, size, "%ld", arg);
204 // Unmodified string specifier.
206 packed_to_token(arg, renderp); /* unpack directly to destination */
207 size_t len = strlen(renderp);
212 // Singular/plural specifier.
214 if (previous_arg > 1) { // look at the *previous* parameter (which by necessity must be numeric)
220 /* Version specifier */
222 strcpy(renderp, VERSION);
223 size_t len = strlen(VERSION);
228 // All-lowercase specifier.
229 if (msg[i] == 'L' || msg[i] == 'C') {
230 packed_to_token(arg, renderp); /* unpack directly to destination */
231 int len = strlen(renderp);
232 for (int j = 0; j < len; ++j) {
233 renderp[j] = tolower(renderp[j]);
235 if (msg[i] == 'C') // First char uppercase, rest lowercase.
236 renderp[0] = toupper(renderp[0]);
246 // Print the message.
247 printf("%s\n", rendered);
252 void speak(const char* msg, ...)
260 void pspeak(vocab_t msg, enum speaktype mode, int skip, ...)
261 /* Find the skip+1st message from msg and print it. Modes are:
262 * feel = for inventory, what you can touch
263 * look = the long description for the state the object is in
264 * listen = the sound for the state the object is in
265 * study = text on the object. */
271 vspeak(objects[msg].inventory, ap);
274 vspeak(objects[msg].descriptions[skip], ap);
277 vspeak(objects[msg].sounds[skip], ap);
280 vspeak(objects[msg].texts[skip], ap);
283 vspeak(objects[msg].changes[skip], ap);
289 void rspeak(vocab_t i, ...)
290 /* Print the i-th "random" message (section 6 of database). */
294 vspeak(arbitrary_messages[i], ap);
298 void echo_input(FILE* destination, const char* input_prompt, const char* input)
300 size_t len = strlen(input_prompt) + strlen(input) + 1;
301 char* prompt_and_input = (char*) xmalloc(len);
302 strcpy(prompt_and_input, input_prompt);
303 strcat(prompt_and_input, input);
304 fprintf(destination, "%s\n", prompt_and_input);
305 free(prompt_and_input);
308 int word_count(char* s)
310 char* copy = xstrdup(s);
311 char delims[] = " \t";
315 word = strtok(copy, delims);
316 while (word != NULL) {
317 word = strtok(NULL, delims);
327 char input_prompt[] = "> ";
329 input_prompt[0] = '\0';
331 // Print a blank line if game.blklin tells us to.
332 if (game.blklin == true)
337 input = readline(input_prompt);
339 if (input == NULL) // Got EOF; return with it.
341 else if (input[0] == '#') { // Ignore comments.
344 } else // We have a 'normal' line; leave the loop.
348 // Strip trailing newlines from the input
349 input[strcspn(input, "\n")] = 0;
354 echo_input(stdout, input_prompt, input);
357 echo_input(logfp, "", input);
371 // Should be unreachable. Reply should never be NULL
377 char* firstword = (char*) xmalloc(strlen(reply) + 1);
378 sscanf(reply, "%s", firstword);
382 for (int i = 0; i < (int)strlen(firstword); ++i)
383 firstword[i] = tolower(firstword[i]);
385 int yes = strncmp("yes", firstword, sizeof("yes") - 1);
386 int y = strncmp("y", firstword, sizeof("y") - 1);
387 int no = strncmp("no", firstword, sizeof("no") - 1);
388 int n = strncmp("n", firstword, sizeof("n") - 1);
392 if (yes == 0 || y == 0) {
395 } else if (no == 0 || n == 0) {
399 rspeak(PLEASE_ANSWER);
405 bool yes(const char* question, const char* yes_response, const char* no_response)
406 /* Print message X, wait for yes/no answer. If yes, print Y and return true;
407 * if no, print Z and return false. */
418 // Should be unreachable. Reply should never be NULL
424 char* firstword = (char*) xmalloc(strlen(reply) + 1);
425 sscanf(reply, "%s", firstword);
429 for (int i = 0; i < (int)strlen(firstword); ++i)
430 firstword[i] = tolower(firstword[i]);
432 int yes = strncmp("yes", firstword, sizeof("yes") - 1);
433 int y = strncmp("y", firstword, sizeof("y") - 1);
434 int no = strncmp("no", firstword, sizeof("no") - 1);
435 int n = strncmp("n", firstword, sizeof("n") - 1);
439 if (yes == 0 || y == 0) {
443 } else if (no == 0 || n == 0) {
448 rspeak(PLEASE_ANSWER);
455 /* Data structure routines */
457 int get_motion_vocab_id(const char* word)
458 // Return the first motion number that has 'word' as one of its words.
460 for (int i = 0; i < NMOTIONS; ++i) {
461 for (int j = 0; j < motions[i].words.n; ++j) {
462 if (strcasecmp(word, motions[i].words.strs[j]) == 0 && (strlen(word) > 1 || strchr(ignore, word[0]) == NULL || !oldstyle))
466 // If execution reaches here, we didn't find the word.
467 return (WORD_NOT_FOUND);
470 int get_object_vocab_id(const char* word)
471 // Return the first object number that has 'word' as one of its words.
473 for (int i = 0; i < NOBJECTS + 1; ++i) { // FIXME: the + 1 should go when 1-indexing for objects is removed
474 for (int j = 0; j < objects[i].words.n; ++j) {
475 if (strcasecmp(word, objects[i].words.strs[j]) == 0)
479 // If execution reaches here, we didn't find the word.
480 return (WORD_NOT_FOUND);
483 int get_action_vocab_id(const char* word)
484 // Return the first motion number that has 'word' as one of its words.
486 for (int i = 0; i < NACTIONS; ++i) {
487 for (int j = 0; j < actions[i].words.n; ++j) {
488 if (strcasecmp(word, actions[i].words.strs[j]) == 0 && (strlen(word) > 1 || strchr(ignore, word[0]) == NULL || !oldstyle))
492 // If execution reaches here, we didn't find the word.
493 return (WORD_NOT_FOUND);
496 int get_special_vocab_id(const char* word)
497 // Return the first special number that has 'word' as one of its words.
499 for (int i = 0; i < NSPECIALS; ++i) {
500 for (int j = 0; j < specials[i].words.n; ++j) {
501 if (strcasecmp(word, specials[i].words.strs[j]) == 0)
505 // If execution reaches here, we didn't find the word.
506 return (WORD_NOT_FOUND);
509 long get_vocab_id(const char* word)
510 // Search the vocab categories in order for the supplied word.
514 /* FIXME: Magic numbers related to vocabulary */
515 ref_num = get_motion_vocab_id(word);
516 if (ref_num != WORD_NOT_FOUND)
517 return (ref_num + 0); // FIXME: replace with a proper hash
519 ref_num = get_object_vocab_id(word);
520 if (ref_num != WORD_NOT_FOUND)
521 return (ref_num + 1000); // FIXME: replace with a proper hash
523 ref_num = get_action_vocab_id(word);
524 if (ref_num != WORD_NOT_FOUND)
525 return (ref_num + 2000); // FIXME: replace with a proper hash
527 ref_num = get_special_vocab_id(word);
528 if (ref_num != WORD_NOT_FOUND)
529 return (ref_num + 3000); // FIXME: replace with a proper hash
531 // Check for the reservoir magic word.
532 if (strcasecmp(word, game.zzword) == 0)
533 return (PART + 2000); // FIXME: replace with a proper hash
535 return (WORD_NOT_FOUND);
538 void juggle(long object)
539 /* Juggle an object by picking it up and putting it down again, the purpose
540 * being to get the object to the front of the chain of things at its loc. */
544 i = game.place[object];
545 j = game.fixed[object];
547 move(object + NOBJECTS, j);
550 void move(long object, long where)
551 /* Place any object anywhere by picking it up and dropping it. May
552 * already be toting, in which case the carry is a no-op. Mustn't
553 * pick up objects which are not at any loc, since carry wants to
554 * remove objects from game.atloc chains. */
558 if (object > NOBJECTS)
559 from = game.fixed[object - NOBJECTS];
561 from = game.place[object];
562 if (from != LOC_NOWHERE && from != CARRIED && !SPECIAL(from))
567 long put(long object, long where, long pval)
568 /* PUT is the same as MOVE, except it returns a value used to set up the
569 * negated game.prop values for the repository objects. */
575 void carry(long object, long where)
576 /* Start toting an object, removing it from the list of things at its former
577 * location. Incr holdng unless it was already being toted. If object>NOBJECTS
578 * (moving "fixed" second loc), don't change game.place or game.holdng. */
582 if (object <= NOBJECTS) {
583 if (game.place[object] == CARRIED)
585 game.place[object] = CARRIED;
588 if (game.atloc[where] == object) {
589 game.atloc[where] = game.link[object];
592 temp = game.atloc[where];
593 while (game.link[temp] != object) {
594 temp = game.link[temp];
596 game.link[temp] = game.link[object];
599 void drop(long object, long where)
600 /* Place an object at a given loc, prefixing it onto the game.atloc list. Decr
601 * game.holdng if the object was being toted. */
603 if (object > NOBJECTS)
604 game.fixed[object - NOBJECTS] = where;
606 if (game.place[object] == CARRIED)
608 game.place[object] = where;
612 game.link[object] = game.atloc[where];
613 game.atloc[where] = object;
616 long atdwrf(long where)
617 /* Return the index of first dwarf at the given location, zero if no dwarf is
618 * there (or if dwarves not active yet), -1 if all dwarves are dead. Ignore
619 * the pirate (6th dwarf). */
627 for (long i = 1; i <= NDWARVES - 1; i++) {
628 if (game.dloc[i] == where)
630 if (game.dloc[i] != 0)
636 /* Utility routines (setbit, tstbit, set_seed, get_next_lcg_value,
639 long setbit(long bit)
640 /* Returns 2**bit for use in constructing bit-masks. */
645 bool tstbit(long mask, int bit)
646 /* Returns true if the specified bit is set in the mask. */
648 return (mask & (1 << bit)) != 0;
651 void set_seed(long seedval)
652 /* Set the LCG seed */
654 game.lcg_x = (unsigned long) seedval % game.lcg_m;
656 // once seed is set, we need to generate the Z`ZZZ word
657 make_zzword(game.zzword);
660 unsigned long get_next_lcg_value(void)
661 /* Return the LCG's current value, and then iterate it. */
663 unsigned long old_x = game.lcg_x;
664 game.lcg_x = (game.lcg_a * game.lcg_x + game.lcg_c) % game.lcg_m;
668 long randrange(long range)
669 /* Return a random integer from [0, range). */
671 return range * get_next_lcg_value() / game.lcg_m;
674 void make_zzword(char zzword[6])
676 for (int i = 0; i < 5; ++i) {
677 zzword[i] = 'A' + randrange(26);
679 zzword[1] = '\''; // force second char to apostrophe
683 void datime(long* d, long* t)
686 gettimeofday(&tv, NULL);
687 *d = (long) tv.tv_sec;
688 *t = (long) tv.tv_usec;
692 void bug(enum bugtype num, const char *error_string)
694 fprintf(stderr, "Fatal error %d, %s.\n", num, error_string);