10 #include "linenoise/linenoise.h"
13 const char new_advent_to_ascii[] = {
14 ' ', '!', '"', '#', '$', '%', '&', '\'',
15 '(', ')', '*', '+', ',', '-', '.', '/',
16 '0', '1', '2', '3', '4', '5', '6', '7',
17 '8', '9', ':', ';', '<', '=', '>', '?',
18 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
19 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
20 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
21 'X', 'Y', 'Z', '\0', '\0', '\0', '\0', '\0',
24 const char new_ascii_to_advent[] = {
25 63, 63, 63, 63, 63, 63, 63, 63,
26 63, 63, 63, 63, 63, 63, 63, 63,
27 63, 63, 63, 63, 63, 63, 63, 63,
28 63, 63, 63, 63, 63, 63, 63, 63,
30 0, 1, 2, 3, 4, 5, 6, 7,
31 8, 9, 10, 11, 12, 13, 14, 15,
32 16, 17, 18, 19, 20, 21, 22, 23,
33 24, 25, 26, 27, 28, 29, 30, 31,
34 32, 33, 34, 35, 36, 37, 38, 39,
35 40, 41, 42, 43, 44, 45, 46, 47,
36 48, 49, 50, 51, 52, 53, 54, 55,
37 56, 57, 58, 59, 60, 61, 62, 63,
39 63, 63, 63, 63, 63, 63, 63, 63,
40 63, 63, 63, 63, 63, 63, 63, 63,
41 63, 63, 63, 63, 63, 63, 63, 63,
42 63, 63, 63, 63, 63, 63, 63, 63,
45 char* xstrdup(const char* s)
47 char* ptr = strdup(s);
50 // exclude from coverage analysis because we can't simulate an out of memory error in testing
51 fprintf(stderr, "Out of memory!\n");
57 void* xmalloc(size_t size)
59 void* ptr = malloc(size);
62 // exclude from coverage analysis because we can't simulate an out of memory error in testing
63 fprintf(stderr, "Out of memory!\n");
70 void packed_to_token(long packed, char token[6])
72 // Unpack and map back to ASCII.
73 for (int i = 0; i < 5; ++i) {
74 char advent = (packed >> i * 6) & 63;
75 token[i] = new_advent_to_ascii[(int) advent];
78 // Ensure the last character is \0.
81 // Replace trailing whitespace with \0.
82 for (int i = 4; i >= 0; --i) {
83 if (token[i] == ' ' || token[i] == '\t')
90 long token_to_packed(const char token[6])
92 size_t t_len = strlen(token);
94 for (size_t i = 0; i < t_len; ++i) {
95 char mapped = new_ascii_to_advent[(int) token[i]];
96 packed |= (mapped << (6 * i));
101 void tokenize(char* raw, long tokens[4])
103 // set each token to 0
104 for (int i = 0; i < 4; ++i)
107 // grab the first two words
109 words[0] = (char*) xmalloc(strlen(raw) + 1);
110 words[1] = (char*) xmalloc(strlen(raw) + 1);
111 int word_count = sscanf(raw, "%s%s", words[0], words[1]);
113 // make space for substrings and zero it out
114 char chunk_data[][6] = {
121 // break the words into up to 4 5-char substrings
122 sscanf(words[0], "%5s%5s", chunk_data[0], chunk_data[1]);
124 sscanf(words[1], "%5s%5s", chunk_data[2], chunk_data[3]);
128 // uppercase all the substrings
129 for (int i = 0; i < 4; ++i)
130 for (unsigned int j = 0; j < strlen(chunk_data[i]); ++j)
131 chunk_data[i][j] = (char) toupper(chunk_data[i][j]);
133 // pack the substrings
134 for (int i = 0; i < 4; ++i)
135 tokens[i] = token_to_packed(chunk_data[i]);
138 /* Hide the fact that wods are corrently packed longs */
140 bool wordeq(token_t a, token_t b)
145 bool wordempty(token_t a)
150 void wordclear(token_t *v)
155 /* I/O routines (speak, pspeak, rspeak, GETIN, YES) */
157 void vspeak(const char* msg, va_list ap)
159 // Do nothing if we got a null pointer.
163 // Do nothing if we got an empty string.
164 if (strlen(msg) == 0)
167 // Print a newline if the global game.blklin says to.
168 if (game.blklin == true)
171 int msglen = strlen(msg);
174 ssize_t size = 2000; /* msglen > 50 ? msglen*2 : 100; */
175 char* rendered = xmalloc(size);
176 char* renderp = rendered;
178 // Handle format specifiers (including the custom %C, %L, %S) by adjusting the parameter accordingly, and replacing the specifier with %s.
179 long previous_arg = 0;
180 for (int i = 0; i < msglen; i++) {
185 long arg = va_arg(ap, long);
189 // Integer specifier. In order to accommodate the fact that PARMS can have both legitimate integers *and* packed tokens, stringify everything. Future work may eliminate the need for this.
191 int ret = snprintf(renderp, size, "%ld", arg);
198 // Unmodified string specifier.
200 packed_to_token(arg, renderp); /* unpack directly to destination */
201 size_t len = strlen(renderp);
206 // Singular/plural specifier.
208 if (previous_arg > 1) { // look at the *previous* parameter (which by necessity must be numeric)
214 // All-lowercase specifier.
215 if (msg[i] == 'L' || msg[i] == 'C') {
216 packed_to_token(arg, renderp); /* unpack directly to destination */
217 int len = strlen(renderp);
218 for (int j = 0; j < len; ++j) {
219 renderp[j] = tolower(renderp[j]);
221 if (msg[i] == 'C') // First char uppercase, rest lowercase.
222 renderp[0] = toupper(renderp[0]);
232 // Print the message.
233 printf("%s\n", rendered);
238 void speak(const char* msg, ...)
246 void pspeak(vocab_t msg, enum speaktype mode, int skip, ...)
247 /* Find the skip+1st message from msg and print it. Modes are:
248 * feel = for inventory, what you can touch
249 * look = the long description for the state the object is in
250 * listen = the sound for the state the object is in
251 * study = text on the object. */
257 vspeak(objects[msg].inventory, ap);
260 vspeak(objects[msg].descriptions[skip], ap);
263 vspeak(objects[msg].sounds[skip], ap);
266 vspeak(objects[msg].texts[skip], ap);
269 vspeak(objects[msg].changes[skip], ap);
275 void rspeak(vocab_t i, ...)
276 /* Print the i-th "random" message (section 6 of database). */
280 vspeak(arbitrary_messages[i], ap);
284 void echo_input(FILE* destination, char* input_prompt, char* input)
286 size_t len = strlen(input_prompt) + strlen(input) + 1;
287 char* prompt_and_input = (char*) xmalloc(len);
288 strcpy(prompt_and_input, input_prompt);
289 strcat(prompt_and_input, input);
290 fprintf(destination, "%s\n", prompt_and_input);
291 free(prompt_and_input);
294 int word_count(char* s)
296 char* copy = xstrdup(s);
297 char delims[] = " \t";
301 word = strtok(copy, delims);
302 while (word != NULL) {
303 word = strtok(NULL, delims);
313 char input_prompt[] = "> ";
315 input_prompt[0] = '\0';
317 // Print a blank line if game.blklin tells us to.
318 if (game.blklin == true)
324 input = linenoise(input_prompt);
330 // Should be unreachable in tests, as they will use a non-interactive shell.
331 printf("%s", input_prompt);
333 ssize_t numread = getline(&input, &n, stdin);
334 if (numread == -1) // Got EOF; return with it.
338 if (input == NULL) // Got EOF; return with it.
340 else if (input[0] == '#') // Ignore comments.
342 else // We have a 'normal' line; leave the loop.
346 // Strip trailing newlines from the input
347 input[strcspn(input, "\n")] = 0;
349 linenoiseHistoryAdd(input);
352 echo_input(stdout, input_prompt, input);
355 echo_input(logfp, input_prompt, input);
369 // Should be unreachable. Reply should never be NULL
370 linenoiseFree(reply);
375 char* firstword = (char*) xmalloc(strlen(reply) + 1);
376 sscanf(reply, "%s", firstword);
378 for (int i = 0; i < (int)strlen(firstword); ++i)
379 firstword[i] = tolower(firstword[i]);
381 int yes = strncmp("yes", firstword, sizeof("yes") - 1);
382 int y = strncmp("y", firstword, sizeof("y") - 1);
383 int no = strncmp("no", firstword, sizeof("no") - 1);
384 int n = strncmp("n", firstword, sizeof("n") - 1);
388 if (yes == 0 || y == 0) {
391 } else if (no == 0 || n == 0) {
395 rspeak(PLEASE_ANSWER);
397 linenoiseFree(reply);
402 bool yes(const char* question, const char* yes_response, const char* no_response)
403 /* Print message X, wait for yes/no answer. If yes, print Y and return true;
404 * if no, print Z and return false. */
415 // Should be unreachable. Reply should never be NULL
416 linenoiseFree(reply);
421 char* firstword = (char*) xmalloc(strlen(reply) + 1);
422 sscanf(reply, "%s", firstword);
424 for (int i = 0; i < (int)strlen(firstword); ++i)
425 firstword[i] = tolower(firstword[i]);
427 int yes = strncmp("yes", firstword, sizeof("yes") - 1);
428 int y = strncmp("y", firstword, sizeof("y") - 1);
429 int no = strncmp("no", firstword, sizeof("no") - 1);
430 int n = strncmp("n", firstword, sizeof("n") - 1);
434 if (yes == 0 || y == 0) {
438 } else if (no == 0 || n == 0) {
443 rspeak(PLEASE_ANSWER);
445 linenoiseFree(reply);
449 /* Data structure routines */
451 int get_motion_vocab_id(const char* word)
452 // Return the first motion number that has 'word' as one of its words.
454 for (int i = 0; i < NMOTIONS; ++i) {
455 for (int j = 0; j < motions[i].words.n; ++j) {
456 if (strcasecmp(word, motions[i].words.strs[j]) == 0)
460 // If execution reaches here, we didn't find the word.
461 return (WORD_NOT_FOUND);
464 int get_object_vocab_id(const char* word)
465 // Return the first object number that has 'word' as one of its words.
467 for (int i = 0; i < NOBJECTS + 1; ++i) { // FIXME: the + 1 should go when 1-indexing for objects is removed
468 for (int j = 0; j < objects[i].words.n; ++j) {
469 if (strcasecmp(word, objects[i].words.strs[j]) == 0)
473 // If execution reaches here, we didn't find the word.
474 return (WORD_NOT_FOUND);
477 int get_action_vocab_id(const char* word)
478 // Return the first motion number that has 'word' as one of its words.
480 for (int i = 0; i < NACTIONS; ++i) {
481 for (int j = 0; j < actions[i].words.n; ++j) {
482 if (strcasecmp(word, actions[i].words.strs[j]) == 0)
486 // If execution reaches here, we didn't find the word.
487 return (WORD_NOT_FOUND);
490 int get_special_vocab_id(const char* word)
491 // Return the first special number that has 'word' as one of its words.
493 for (int i = 0; i < NSPECIALS; ++i) {
494 for (int j = 0; j < specials[i].words.n; ++j) {
495 if (strcasecmp(word, specials[i].words.strs[j]) == 0)
499 // If execution reaches here, we didn't find the word.
500 return (WORD_NOT_FOUND);
503 long get_vocab_id(const char* word)
504 // Search the vocab categories in order for the supplied word.
508 ref_num = get_motion_vocab_id(word);
509 if (ref_num != WORD_NOT_FOUND)
510 return (ref_num + 0); // FIXME: replace with a proper hash
512 ref_num = get_object_vocab_id(word);
513 if (ref_num != WORD_NOT_FOUND)
514 return (ref_num + 1000); // FIXME: replace with a proper hash
516 ref_num = get_action_vocab_id(word);
517 if (ref_num != WORD_NOT_FOUND)
518 return (ref_num + 2000); // FIXME: replace with a proper hash
520 ref_num = get_special_vocab_id(word);
521 if (ref_num != WORD_NOT_FOUND)
522 return (ref_num + 3000); // FIXME: replace with a proper hash
524 // Check for the reservoir magic word.
525 if (strcasecmp(word, game.zzword) == 0)
526 return (PART + 2000); // FIXME: replace with a proper hash
528 return (WORD_NOT_FOUND);
531 void juggle(long object)
532 /* Juggle an object by picking it up and putting it down again, the purpose
533 * being to get the object to the front of the chain of things at its loc. */
537 i = game.place[object];
538 j = game.fixed[object];
540 move(object + NOBJECTS, j);
543 void move(long object, long where)
544 /* Place any object anywhere by picking it up and dropping it. May
545 * already be toting, in which case the carry is a no-op. Mustn't
546 * pick up objects which are not at any loc, since carry wants to
547 * remove objects from game.atloc chains. */
551 if (object > NOBJECTS)
552 from = game.fixed[object - NOBJECTS];
554 from = game.place[object];
555 if (from != LOC_NOWHERE && from != CARRIED && !SPECIAL(from))
560 long put(long object, long where, long pval)
561 /* PUT is the same as MOVE, except it returns a value used to set up the
562 * negated game.prop values for the repository objects. */
568 void carry(long object, long where)
569 /* Start toting an object, removing it from the list of things at its former
570 * location. Incr holdng unless it was already being toted. If object>NOBJECTS
571 * (moving "fixed" second loc), don't change game.place or game.holdng. */
575 if (object <= NOBJECTS) {
576 if (game.place[object] == CARRIED)
578 game.place[object] = CARRIED;
581 if (game.atloc[where] == object) {
582 game.atloc[where] = game.link[object];
585 temp = game.atloc[where];
586 while (game.link[temp] != object) {
587 temp = game.link[temp];
589 game.link[temp] = game.link[object];
592 void drop(long object, long where)
593 /* Place an object at a given loc, prefixing it onto the game.atloc list. Decr
594 * game.holdng if the object was being toted. */
596 if (object > NOBJECTS)
597 game.fixed[object - NOBJECTS] = where;
599 if (game.place[object] == CARRIED)
601 game.place[object] = where;
605 game.link[object] = game.atloc[where];
606 game.atloc[where] = object;
609 long atdwrf(long where)
610 /* Return the index of first dwarf at the given location, zero if no dwarf is
611 * there (or if dwarves not active yet), -1 if all dwarves are dead. Ignore
612 * the pirate (6th dwarf). */
620 for (long i = 1; i <= NDWARVES - 1; i++) {
621 if (game.dloc[i] == where)
623 if (game.dloc[i] != 0)
629 /* Utility routines (SETBIT, TSTBIT, set_seed, get_next_lcg_value,
630 * randrange, RNDVOC) */
632 long setbit(long bit)
633 /* Returns 2**bit for use in constructing bit-masks. */
638 bool tstbit(long mask, int bit)
639 /* Returns true if the specified bit is set in the mask. */
641 return (mask & (1 << bit)) != 0;
644 void set_seed(long seedval)
645 /* Set the LCG seed */
647 game.lcg_x = (unsigned long) seedval % game.lcg_m;
650 unsigned long get_next_lcg_value(void)
651 /* Return the LCG's current value, and then iterate it. */
653 unsigned long old_x = game.lcg_x;
654 game.lcg_x = (game.lcg_a * game.lcg_x + game.lcg_c) % game.lcg_m;
658 long randrange(long range)
659 /* Return a random integer from [0, range). */
661 return range * get_next_lcg_value() / game.lcg_m;
664 void make_zzword(char zzword[6])
666 for (int i = 0; i < 5; ++i) {
667 zzword[i] = 'A' + randrange(26);
669 zzword[1] = '\''; // force second char to apostrophe
673 void datime(long* d, long* t)
676 gettimeofday(&tv, NULL);
677 *d = (long) tv.tv_sec;
678 *t = (long) tv.tv_usec;
682 void bug(enum bugtype num, const char *error_string)
684 fprintf(stderr, "Fatal error %d, %s.\n", num, error_string);