8 #include <editline/readline.h>
13 static void* xmalloc(size_t size)
15 void* ptr = malloc(size);
18 // exclude from coverage analysis because we can't simulate an out of memory error in testing
19 fprintf(stderr, "Out of memory!\n");
26 void packed_to_token(long packed, char token[TOKLEN+1])
28 // The advent->ascii mapping.
29 const char advent_to_ascii[] = {
30 ' ', '!', '"', '#', '$', '%', '&', '\'',
31 '(', ')', '*', '+', ',', '-', '.', '/',
32 '0', '1', '2', '3', '4', '5', '6', '7',
33 '8', '9', ':', ';', '<', '=', '>', '?',
34 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
35 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
36 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
37 'X', 'Y', 'Z', '\0', '\0', '\0', '\0', '\0',
40 // Unpack and map back to ASCII.
41 for (int i = 0; i < 5; ++i) {
42 char advent = (packed >> i * 6) & 63;
43 token[i] = advent_to_ascii[(int) advent];
46 // Ensure the last character is \0.
49 // Replace trailing whitespace with \0.
50 for (int i = 4; i >= 0; --i) {
51 if (token[i] == ' ' ||
59 long token_to_packed(const char token[])
61 const char ascii_to_advent[] = {
62 63, 63, 63, 63, 63, 63, 63, 63,
63 63, 63, 63, 63, 63, 63, 63, 63,
64 63, 63, 63, 63, 63, 63, 63, 63,
65 63, 63, 63, 63, 63, 63, 63, 63,
67 0, 1, 2, 3, 4, 5, 6, 7,
68 8, 9, 10, 11, 12, 13, 14, 15,
69 16, 17, 18, 19, 20, 21, 22, 23,
70 24, 25, 26, 27, 28, 29, 30, 31,
71 32, 33, 34, 35, 36, 37, 38, 39,
72 40, 41, 42, 43, 44, 45, 46, 47,
73 48, 49, 50, 51, 52, 53, 54, 55,
74 56, 57, 58, 59, 60, 61, 62, 63,
76 63, 63, 63, 63, 63, 63, 63, 63,
77 63, 63, 63, 63, 63, 63, 63, 63,
78 63, 63, 63, 63, 63, 63, 63, 63,
79 63, 63, 63, 63, 63, 63, 63, 63,
82 size_t t_len = strlen(token);
86 for (size_t i = 0; i < t_len; ++i) {
87 char mapped = ascii_to_advent[(int) toupper(token[i])];
88 packed |= (mapped << (6 * i));
93 void tokenize(char* raw, struct command_t *cmd)
95 memset(cmd, '\0', sizeof(struct command_t));
97 /* FIXME: put a bound prefix on the %s to prevent buffer overflow */
98 int word_count = sscanf(raw, "%s%s", cmd->raw1, cmd->raw2);
100 // pack the substrings
101 cmd->wd1 = token_to_packed(cmd->raw1);
102 cmd->wd2 = token_to_packed(cmd->raw2);
105 /* Hide the fact that wods are corrently packed longs */
107 bool wordeq(token_t a, token_t b)
112 bool wordempty(token_t a)
117 void wordclear(token_t *v)
122 /* I/O routines (speak, pspeak, rspeak, sspeak, get_input, yes) */
124 void vspeak(const char* msg, bool blank, va_list ap)
126 // Do nothing if we got a null pointer.
130 // Do nothing if we got an empty string.
131 if (strlen(msg) == 0)
137 int msglen = strlen(msg);
140 ssize_t size = 2000; /* msglen > 50 ? msglen*2 : 100; */
141 char* rendered = xmalloc(size);
142 char* renderp = rendered;
144 // Handle format specifiers (including the custom %C, %L, %S) by
145 // adjusting the parameter accordingly, and replacing the
146 // specifier with %s.
147 long previous_arg = 0;
148 for (int i = 0; i < msglen; i++) {
153 long arg = va_arg(ap, long);
155 arg = 0; // LCOV_EXCL_LINE - don't think we can get here.
157 // Integer specifier. In order to accommodate the fact
158 // that PARMS can have both legitimate integers *and*
159 // packed tokens, stringify everything. Future work may
160 // eliminate the need for this.
162 int ret = snprintf(renderp, size, "%ld", arg);
169 // Unmodified string specifier.
171 packed_to_token(arg, renderp); /* unpack directly to destination */
172 size_t len = strlen(renderp);
177 // Singular/plural specifier.
179 if (previous_arg > 1) { // look at the *previous* parameter (which by necessity must be numeric)
185 /* Version specifier */
187 strcpy(renderp, VERSION);
188 size_t len = strlen(VERSION);
193 // All-lowercase specifier.
196 packed_to_token(arg, renderp); /* unpack directly to destination */
197 int len = strlen(renderp);
198 for (int j = 0; j < len; ++j) {
199 renderp[j] = tolower(renderp[j]);
201 if (msg[i] == 'C') // First char uppercase, rest lowercase.
202 renderp[0] = toupper(renderp[0]);
212 // Print the message.
213 printf("%s\n", rendered);
218 void speak(const char* msg, ...)
222 vspeak(msg, true, ap);
226 void sspeak(const long msg, ...)
231 vprintf(arbitrary_messages[msg], ap);
236 void pspeak(vocab_t msg, enum speaktype mode, int skip, bool blank, ...)
237 /* Find the skip+1st message from msg and print it. Modes are:
238 * feel = for inventory, what you can touch
239 * look = the long description for the state the object is in
240 * listen = the sound for the state the object is in
241 * study = text on the object. */
247 vspeak(objects[msg].inventory, blank, ap);
250 vspeak(objects[msg].descriptions[skip], blank, ap);
253 vspeak(objects[msg].sounds[skip], blank, ap);
256 vspeak(objects[msg].texts[skip], blank, ap);
259 vspeak(objects[msg].changes[skip], blank, ap);
265 void rspeak(vocab_t i, ...)
266 /* Print the i-th "random" message (section 6 of database). */
270 vspeak(arbitrary_messages[i], true, ap);
274 void echo_input(FILE* destination, const char* input_prompt, const char* input)
276 size_t len = strlen(input_prompt) + strlen(input) + 1;
277 char* prompt_and_input = (char*) xmalloc(len);
278 strcpy(prompt_and_input, input_prompt);
279 strcat(prompt_and_input, input);
280 fprintf(destination, "%s\n", prompt_and_input);
281 free(prompt_and_input);
284 int word_count(char* str)
286 char delims[] = " \t";
290 for (char *s = str; *s; s++)
292 if (strchr(delims, *s) == 0) {
297 if (strchr(delims, *s) != 0) {
308 char input_prompt[] = "> ";
309 if (!settings.prompt)
310 input_prompt[0] = '\0';
312 // Print a blank line
317 input = readline(input_prompt);
319 if (input == NULL) // Got EOF; return with it.
321 else if (input[0] == '#') { // Ignore comments.
324 } else // We have a 'normal' line; leave the loop.
328 // Strip trailing newlines from the input
329 input[strcspn(input, "\n")] = 0;
334 echo_input(stdout, input_prompt, input);
337 echo_input(settings.logfp, "", input);
351 // Should be unreachable. Reply should never be NULL
357 char* firstword = (char*) xmalloc(strlen(reply) + 1);
358 sscanf(reply, "%s", firstword);
362 for (int i = 0; i < (int)strlen(firstword); ++i)
363 firstword[i] = tolower(firstword[i]);
365 int yes = strncmp("yes", firstword, sizeof("yes") - 1);
366 int y = strncmp("y", firstword, sizeof("y") - 1);
367 int no = strncmp("no", firstword, sizeof("no") - 1);
368 int n = strncmp("n", firstword, sizeof("n") - 1);
376 } else if (no == 0 ||
381 rspeak(PLEASE_ANSWER);
387 bool yes(const char* question, const char* yes_response, const char* no_response)
388 /* Print message X, wait for yes/no answer. If yes, print Y and return true;
389 * if no, print Z and return false. */
400 // Should be unreachable. Reply should never be NULL
406 char* firstword = (char*) xmalloc(strlen(reply) + 1);
407 sscanf(reply, "%s", firstword);
411 for (int i = 0; i < (int)strlen(firstword); ++i)
412 firstword[i] = tolower(firstword[i]);
414 int yes = strncmp("yes", firstword, sizeof("yes") - 1);
415 int y = strncmp("y", firstword, sizeof("y") - 1);
416 int no = strncmp("no", firstword, sizeof("no") - 1);
417 int n = strncmp("n", firstword, sizeof("n") - 1);
426 } else if (no == 0 ||
432 rspeak(PLEASE_ANSWER);
439 /* Data structure routines */
441 int get_motion_vocab_id(const char* word)
442 // Return the first motion number that has 'word' as one of its words.
444 for (int i = 0; i < NMOTIONS; ++i) {
445 for (int j = 0; j < motions[i].words.n; ++j) {
446 if (strcasecmp(word, motions[i].words.strs[j]) == 0 && (strlen(word) > 1 ||
447 strchr(ignore, word[0]) == NULL ||
452 // If execution reaches here, we didn't find the word.
453 return (WORD_NOT_FOUND);
456 int get_object_vocab_id(const char* word)
457 // Return the first object number that has 'word' as one of its words.
459 for (int i = 0; i < NOBJECTS + 1; ++i) { // FIXME: the + 1 should go when 1-indexing for objects is removed
460 for (int j = 0; j < objects[i].words.n; ++j) {
461 if (strcasecmp(word, objects[i].words.strs[j]) == 0)
465 // If execution reaches here, we didn't find the word.
466 return (WORD_NOT_FOUND);
469 int get_action_vocab_id(const char* word)
470 // Return the first motion number that has 'word' as one of its words.
472 for (int i = 0; i < NACTIONS; ++i) {
473 for (int j = 0; j < actions[i].words.n; ++j) {
474 if (strcasecmp(word, actions[i].words.strs[j]) == 0 && (strlen(word) > 1 ||
475 strchr(ignore, word[0]) == NULL ||
480 // If execution reaches here, we didn't find the word.
481 return (WORD_NOT_FOUND);
484 int get_special_vocab_id(const char* word)
485 // Return the first special number that has 'word' as one of its words.
487 for (int i = 0; i < NSPECIALS; ++i) {
488 for (int j = 0; j < specials[i].words.n; ++j) {
489 if (strcasecmp(word, specials[i].words.strs[j]) == 0)
493 // If execution reaches here, we didn't find the word.
494 return (WORD_NOT_FOUND);
497 long get_vocab_id(const char* word)
498 // Search the vocab categories in order for the supplied word.
502 /* FIXME: Magic numbers related to vocabulary */
503 ref_num = get_motion_vocab_id(word);
504 if (ref_num != WORD_NOT_FOUND)
505 return (ref_num + 0); // FIXME: replace with a proper hash
507 ref_num = get_object_vocab_id(word);
508 if (ref_num != WORD_NOT_FOUND)
509 return (ref_num + 1000); // FIXME: replace with a proper hash
511 ref_num = get_action_vocab_id(word);
512 if (ref_num != WORD_NOT_FOUND)
513 return (ref_num + 2000); // FIXME: replace with a proper hash
515 ref_num = get_special_vocab_id(word);
516 if (ref_num != WORD_NOT_FOUND)
517 return (ref_num + 3000); // FIXME: replace with a proper hash
519 // Check for the reservoir magic word.
520 if (strcasecmp(word, game.zzword) == 0)
521 return (PART + 2000); // FIXME: replace with a proper hash
523 return (WORD_NOT_FOUND);
526 void juggle(long object)
527 /* Juggle an object by picking it up and putting it down again, the purpose
528 * being to get the object to the front of the chain of things at its loc. */
532 i = game.place[object];
533 j = game.fixed[object];
535 move(object + NOBJECTS, j);
538 void move(long object, long where)
539 /* Place any object anywhere by picking it up and dropping it. May
540 * already be toting, in which case the carry is a no-op. Mustn't
541 * pick up objects which are not at any loc, since carry wants to
542 * remove objects from game.atloc chains. */
546 if (object > NOBJECTS)
547 from = game.fixed[object - NOBJECTS];
549 from = game.place[object];
550 if (from != LOC_NOWHERE && from != CARRIED && !SPECIAL(from))
555 long put(long object, long where, long pval)
556 /* PUT is the same as MOVE, except it returns a value used to set up the
557 * negated game.prop values for the repository objects. */
563 void carry(long object, long where)
564 /* Start toting an object, removing it from the list of things at its former
565 * location. Incr holdng unless it was already being toted. If object>NOBJECTS
566 * (moving "fixed" second loc), don't change game.place or game.holdng. */
570 if (object <= NOBJECTS) {
571 if (game.place[object] == CARRIED)
573 game.place[object] = CARRIED;
576 if (game.atloc[where] == object) {
577 game.atloc[where] = game.link[object];
580 temp = game.atloc[where];
581 while (game.link[temp] != object) {
582 temp = game.link[temp];
584 game.link[temp] = game.link[object];
587 void drop(long object, long where)
588 /* Place an object at a given loc, prefixing it onto the game.atloc list. Decr
589 * game.holdng if the object was being toted. */
591 if (object > NOBJECTS)
592 game.fixed[object - NOBJECTS] = where;
594 if (game.place[object] == CARRIED)
596 game.place[object] = where;
600 game.link[object] = game.atloc[where];
601 game.atloc[where] = object;
604 long atdwrf(long where)
605 /* Return the index of first dwarf at the given location, zero if no dwarf is
606 * there (or if dwarves not active yet), -1 if all dwarves are dead. Ignore
607 * the pirate (6th dwarf). */
615 for (long i = 1; i <= NDWARVES - 1; i++) {
616 if (game.dloc[i] == where)
618 if (game.dloc[i] != 0)
624 /* Utility routines (setbit, tstbit, set_seed, get_next_lcg_value,
627 long setbit(long bit)
628 /* Returns 2**bit for use in constructing bit-masks. */
633 bool tstbit(long mask, int bit)
634 /* Returns true if the specified bit is set in the mask. */
636 return (mask & (1 << bit)) != 0;
639 void set_seed(long seedval)
640 /* Set the LCG seed */
642 game.lcg_x = (unsigned long) seedval % game.lcg_m;
644 // once seed is set, we need to generate the Z`ZZZ word
645 make_zzword(game.zzword);
648 unsigned long get_next_lcg_value(void)
649 /* Return the LCG's current value, and then iterate it. */
651 unsigned long old_x = game.lcg_x;
652 game.lcg_x = (game.lcg_a * game.lcg_x + game.lcg_c) % game.lcg_m;
656 long randrange(long range)
657 /* Return a random integer from [0, range). */
659 return range * get_next_lcg_value() / game.lcg_m;
662 void make_zzword(char zzword[TOKLEN+1])
664 for (int i = 0; i < 5; ++i) {
665 zzword[i] = 'A' + randrange(26);
667 zzword[1] = '\''; // force second char to apostrophe
672 void bug(enum bugtype num, const char *error_string)
674 fprintf(stderr, "Fatal error %d, %s.\n", num, error_string);