8 #include <editline/readline.h>
13 static 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 static 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[TOKLEN+1])
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] == ' ' ||
71 long token_to_packed(const char token[])
73 const char ascii_to_advent[] = {
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,
77 63, 63, 63, 63, 63, 63, 63, 63,
79 0, 1, 2, 3, 4, 5, 6, 7,
80 8, 9, 10, 11, 12, 13, 14, 15,
81 16, 17, 18, 19, 20, 21, 22, 23,
82 24, 25, 26, 27, 28, 29, 30, 31,
83 32, 33, 34, 35, 36, 37, 38, 39,
84 40, 41, 42, 43, 44, 45, 46, 47,
85 48, 49, 50, 51, 52, 53, 54, 55,
86 56, 57, 58, 59, 60, 61, 62, 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,
91 63, 63, 63, 63, 63, 63, 63, 63,
94 size_t t_len = strlen(token);
98 for (size_t i = 0; i < t_len; ++i) {
99 char mapped = ascii_to_advent[(int) toupper(token[i])];
100 packed |= (mapped << (6 * i));
105 void tokenize(char* raw, struct command_t *cmd)
107 memset(cmd, '\0', sizeof(struct command_t));
109 /* FIXME: put a bound prefix on the %s to prevent buffer overflow */
110 int word_count = sscanf(raw, "%s%s", cmd->raw1, cmd->raw2);
112 // pack the substrings
113 cmd->wd1 = token_to_packed(cmd->raw1);
114 cmd->wd2 = token_to_packed(cmd->raw2);
117 /* Hide the fact that wods are corrently packed longs */
119 bool wordeq(token_t a, token_t b)
124 bool wordempty(token_t a)
129 void wordclear(token_t *v)
134 /* I/O routines (speak, pspeak, rspeak, sspeak, get_input, yes) */
136 void vspeak(const char* msg, bool blank, va_list ap)
138 // Do nothing if we got a null pointer.
142 // Do nothing if we got an empty string.
143 if (strlen(msg) == 0)
149 int msglen = strlen(msg);
152 ssize_t size = 2000; /* msglen > 50 ? msglen*2 : 100; */
153 char* rendered = xmalloc(size);
154 char* renderp = rendered;
156 // Handle format specifiers (including the custom %C, %L, %S) by
157 // adjusting the parameter accordingly, and replacing the
158 // specifier with %s.
159 long previous_arg = 0;
160 for (int i = 0; i < msglen; i++) {
165 long arg = va_arg(ap, long);
167 arg = 0; // LCOV_EXCL_LINE - don't think we can get here.
169 // Integer specifier. In order to accommodate the fact
170 // that PARMS can have both legitimate integers *and*
171 // packed tokens, stringify everything. Future work may
172 // eliminate the need for this.
174 int ret = snprintf(renderp, size, "%ld", arg);
181 // Unmodified string specifier.
183 packed_to_token(arg, renderp); /* unpack directly to destination */
184 size_t len = strlen(renderp);
189 // Singular/plural specifier.
191 if (previous_arg > 1) { // look at the *previous* parameter (which by necessity must be numeric)
197 /* Version specifier */
199 strcpy(renderp, VERSION);
200 size_t len = strlen(VERSION);
205 // All-lowercase specifier.
208 packed_to_token(arg, renderp); /* unpack directly to destination */
209 int len = strlen(renderp);
210 for (int j = 0; j < len; ++j) {
211 renderp[j] = tolower(renderp[j]);
213 if (msg[i] == 'C') // First char uppercase, rest lowercase.
214 renderp[0] = toupper(renderp[0]);
224 // Print the message.
225 printf("%s\n", rendered);
230 void speak(const char* msg, ...)
234 vspeak(msg, true, ap);
238 void sspeak(const long msg, ...)
243 vprintf(arbitrary_messages[msg], ap);
248 void pspeak(vocab_t msg, enum speaktype mode, int skip, bool blank, ...)
249 /* Find the skip+1st message from msg and print it. Modes are:
250 * feel = for inventory, what you can touch
251 * look = the long description for the state the object is in
252 * listen = the sound for the state the object is in
253 * study = text on the object. */
259 vspeak(objects[msg].inventory, blank, ap);
262 vspeak(objects[msg].descriptions[skip], blank, ap);
265 vspeak(objects[msg].sounds[skip], blank, ap);
268 vspeak(objects[msg].texts[skip], blank, ap);
271 vspeak(objects[msg].changes[skip], blank, ap);
277 void rspeak(vocab_t i, ...)
278 /* Print the i-th "random" message (section 6 of database). */
282 vspeak(arbitrary_messages[i], true, ap);
286 void echo_input(FILE* destination, const char* input_prompt, const char* input)
288 size_t len = strlen(input_prompt) + strlen(input) + 1;
289 char* prompt_and_input = (char*) xmalloc(len);
290 strcpy(prompt_and_input, input_prompt);
291 strcat(prompt_and_input, input);
292 fprintf(destination, "%s\n", prompt_and_input);
293 free(prompt_and_input);
296 int word_count(char* s)
298 char* copy = xstrdup(s);
299 char delims[] = " \t";
303 word = strtok(copy, delims);
304 while (word != NULL) {
305 word = strtok(NULL, delims);
315 char input_prompt[] = "> ";
316 if (!settings.prompt)
317 input_prompt[0] = '\0';
319 // Print a blank line
324 input = readline(input_prompt);
326 if (input == NULL) // Got EOF; return with it.
328 else if (input[0] == '#') { // Ignore comments.
331 } else // We have a 'normal' line; leave the loop.
335 // Strip trailing newlines from the input
336 input[strcspn(input, "\n")] = 0;
341 echo_input(stdout, input_prompt, input);
344 echo_input(settings.logfp, "", input);
358 // Should be unreachable. Reply should never be NULL
364 char* firstword = (char*) xmalloc(strlen(reply) + 1);
365 sscanf(reply, "%s", firstword);
369 for (int i = 0; i < (int)strlen(firstword); ++i)
370 firstword[i] = tolower(firstword[i]);
372 int yes = strncmp("yes", firstword, sizeof("yes") - 1);
373 int y = strncmp("y", firstword, sizeof("y") - 1);
374 int no = strncmp("no", firstword, sizeof("no") - 1);
375 int n = strncmp("n", firstword, sizeof("n") - 1);
383 } else if (no == 0 ||
388 rspeak(PLEASE_ANSWER);
394 bool yes(const char* question, const char* yes_response, const char* no_response)
395 /* Print message X, wait for yes/no answer. If yes, print Y and return true;
396 * if no, print Z and return false. */
407 // Should be unreachable. Reply should never be NULL
413 char* firstword = (char*) xmalloc(strlen(reply) + 1);
414 sscanf(reply, "%s", firstword);
418 for (int i = 0; i < (int)strlen(firstword); ++i)
419 firstword[i] = tolower(firstword[i]);
421 int yes = strncmp("yes", firstword, sizeof("yes") - 1);
422 int y = strncmp("y", firstword, sizeof("y") - 1);
423 int no = strncmp("no", firstword, sizeof("no") - 1);
424 int n = strncmp("n", firstword, sizeof("n") - 1);
433 } else if (no == 0 ||
439 rspeak(PLEASE_ANSWER);
446 /* Data structure routines */
448 int get_motion_vocab_id(const char* word)
449 // Return the first motion number that has 'word' as one of its words.
451 for (int i = 0; i < NMOTIONS; ++i) {
452 for (int j = 0; j < motions[i].words.n; ++j) {
453 if (strcasecmp(word, motions[i].words.strs[j]) == 0 && (strlen(word) > 1 ||
454 strchr(ignore, word[0]) == NULL ||
459 // If execution reaches here, we didn't find the word.
460 return (WORD_NOT_FOUND);
463 int get_object_vocab_id(const char* word)
464 // Return the first object number that has 'word' as one of its words.
466 for (int i = 0; i < NOBJECTS + 1; ++i) { // FIXME: the + 1 should go when 1-indexing for objects is removed
467 for (int j = 0; j < objects[i].words.n; ++j) {
468 if (strcasecmp(word, objects[i].words.strs[j]) == 0)
472 // If execution reaches here, we didn't find the word.
473 return (WORD_NOT_FOUND);
476 int get_action_vocab_id(const char* word)
477 // Return the first motion number that has 'word' as one of its words.
479 for (int i = 0; i < NACTIONS; ++i) {
480 for (int j = 0; j < actions[i].words.n; ++j) {
481 if (strcasecmp(word, actions[i].words.strs[j]) == 0 && (strlen(word) > 1 ||
482 strchr(ignore, word[0]) == NULL ||
487 // If execution reaches here, we didn't find the word.
488 return (WORD_NOT_FOUND);
491 int get_special_vocab_id(const char* word)
492 // Return the first special number that has 'word' as one of its words.
494 for (int i = 0; i < NSPECIALS; ++i) {
495 for (int j = 0; j < specials[i].words.n; ++j) {
496 if (strcasecmp(word, specials[i].words.strs[j]) == 0)
500 // If execution reaches here, we didn't find the word.
501 return (WORD_NOT_FOUND);
504 long get_vocab_id(const char* word)
505 // Search the vocab categories in order for the supplied word.
509 /* FIXME: Magic numbers related to vocabulary */
510 ref_num = get_motion_vocab_id(word);
511 if (ref_num != WORD_NOT_FOUND)
512 return (ref_num + 0); // FIXME: replace with a proper hash
514 ref_num = get_object_vocab_id(word);
515 if (ref_num != WORD_NOT_FOUND)
516 return (ref_num + 1000); // FIXME: replace with a proper hash
518 ref_num = get_action_vocab_id(word);
519 if (ref_num != WORD_NOT_FOUND)
520 return (ref_num + 2000); // FIXME: replace with a proper hash
522 ref_num = get_special_vocab_id(word);
523 if (ref_num != WORD_NOT_FOUND)
524 return (ref_num + 3000); // FIXME: replace with a proper hash
526 // Check for the reservoir magic word.
527 if (strcasecmp(word, game.zzword) == 0)
528 return (PART + 2000); // FIXME: replace with a proper hash
530 return (WORD_NOT_FOUND);
533 void juggle(long object)
534 /* Juggle an object by picking it up and putting it down again, the purpose
535 * being to get the object to the front of the chain of things at its loc. */
539 i = game.place[object];
540 j = game.fixed[object];
542 move(object + NOBJECTS, j);
545 void move(long object, long where)
546 /* Place any object anywhere by picking it up and dropping it. May
547 * already be toting, in which case the carry is a no-op. Mustn't
548 * pick up objects which are not at any loc, since carry wants to
549 * remove objects from game.atloc chains. */
553 if (object > NOBJECTS)
554 from = game.fixed[object - NOBJECTS];
556 from = game.place[object];
557 if (from != LOC_NOWHERE && from != CARRIED && !SPECIAL(from))
562 long put(long object, long where, long pval)
563 /* PUT is the same as MOVE, except it returns a value used to set up the
564 * negated game.prop values for the repository objects. */
570 void carry(long object, long where)
571 /* Start toting an object, removing it from the list of things at its former
572 * location. Incr holdng unless it was already being toted. If object>NOBJECTS
573 * (moving "fixed" second loc), don't change game.place or game.holdng. */
577 if (object <= NOBJECTS) {
578 if (game.place[object] == CARRIED)
580 game.place[object] = CARRIED;
583 if (game.atloc[where] == object) {
584 game.atloc[where] = game.link[object];
587 temp = game.atloc[where];
588 while (game.link[temp] != object) {
589 temp = game.link[temp];
591 game.link[temp] = game.link[object];
594 void drop(long object, long where)
595 /* Place an object at a given loc, prefixing it onto the game.atloc list. Decr
596 * game.holdng if the object was being toted. */
598 if (object > NOBJECTS)
599 game.fixed[object - NOBJECTS] = where;
601 if (game.place[object] == CARRIED)
603 game.place[object] = where;
607 game.link[object] = game.atloc[where];
608 game.atloc[where] = object;
611 long atdwrf(long where)
612 /* Return the index of first dwarf at the given location, zero if no dwarf is
613 * there (or if dwarves not active yet), -1 if all dwarves are dead. Ignore
614 * the pirate (6th dwarf). */
622 for (long i = 1; i <= NDWARVES - 1; i++) {
623 if (game.dloc[i] == where)
625 if (game.dloc[i] != 0)
631 /* Utility routines (setbit, tstbit, set_seed, get_next_lcg_value,
634 long setbit(long bit)
635 /* Returns 2**bit for use in constructing bit-masks. */
640 bool tstbit(long mask, int bit)
641 /* Returns true if the specified bit is set in the mask. */
643 return (mask & (1 << bit)) != 0;
646 void set_seed(long seedval)
647 /* Set the LCG seed */
649 game.lcg_x = (unsigned long) seedval % game.lcg_m;
651 // once seed is set, we need to generate the Z`ZZZ word
652 make_zzword(game.zzword);
655 unsigned long get_next_lcg_value(void)
656 /* Return the LCG's current value, and then iterate it. */
658 unsigned long old_x = game.lcg_x;
659 game.lcg_x = (game.lcg_a * game.lcg_x + game.lcg_c) % game.lcg_m;
663 long randrange(long range)
664 /* Return a random integer from [0, range). */
666 return range * get_next_lcg_value() / game.lcg_m;
669 void make_zzword(char zzword[TOKLEN+1])
671 for (int i = 0; i < 5; ++i) {
672 zzword[i] = 'A' + randrange(26);
674 zzword[1] = '\''; // force second char to apostrophe
679 void bug(enum bugtype num, const char *error_string)
681 fprintf(stderr, "Fatal error %d, %s.\n", num, error_string);