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 tokenize(char* raw, struct command_t *cmd)
28 memset(cmd, '\0', sizeof(struct command_t));
30 /* Bound prefix on the %s would be needed to prevent buffer
31 * overflow. but we shortstop this more simply by making each
32 * raw-input buffer as long as the enrire inout buffer. */
33 sscanf(raw, "%s%s", cmd->raw1, cmd->raw2);
35 /* (ESR) In oldstyle mode, simulate the uppercasing and truncating
36 * effect on raw tokens of packing them into sixbit characters, 5
37 * to a 32-bit word. This is something the FORTRAN version did
38 * becuse archaic FORTRAN had no string types. Don Wood's
39 * mechanical translation of 2.5 to C retained the packing and
40 * thus this misfeature.
42 * It's philosophically questionable whether this is the right
43 * thing to do even in oldstyle mode. On one hand, the text
44 * mangling was not authorial intent, but a result of limitations
45 * in their tools. On the other, not simulating this misbehavior
46 * goes against the goal of making oldstyle as accurate as
47 * possible an emulation of the original UI.
49 if (settings.oldstyle) {
50 cmd->raw1[TOKLEN + TOKLEN] = cmd->raw2[TOKLEN + TOKLEN] = '\0';
51 for (size_t i = 0; i < strlen(cmd->raw1); i++)
52 cmd->raw1[i] = toupper(cmd->raw1[i]);
53 for (size_t i = 0; i < strlen(cmd->raw2); i++)
54 cmd->raw2[i] = toupper(cmd->raw2[i]);
58 /* I/O routines (speak, pspeak, rspeak, sspeak, get_input, yes) */
60 static void vspeak(const char* msg, bool blank, va_list ap)
62 // Do nothing if we got a null pointer.
66 // Do nothing if we got an empty string.
73 int msglen = strlen(msg);
76 ssize_t size = 2000; /* msglen > 50 ? msglen*2 : 100; */
77 char* rendered = xmalloc(size);
78 char* renderp = rendered;
80 // Handle format specifiers (including the custom %S) by
81 // adjusting the parameter accordingly, and replacing the
83 bool pluralize = false;
84 for (int i = 0; i < msglen; i++) {
86 /* Ugh. Least obtrusive way to deal with artifacts "on the floor"
87 * being dropped outside of both cave and building. */
88 if (strncmp(msg + i, "floor", 5) == 0 && strchr(" .", msg[i + 5]) && !INSIDE(game.loc)) {
89 strcpy(renderp, "ground");
99 // Integer specifier. In order to accommodate the fact
100 // that PARMS can have both legitimate integers *and*
101 // packed tokens, stringify everything. Future work may
102 // eliminate the need for this.
104 long arg = va_arg(ap, long);
105 int ret = snprintf(renderp, size, "%ld", arg);
110 pluralize = (arg != 1);
113 // Unmodified string specifier.
115 char *arg = va_arg(ap, char *);
116 strncat(renderp, arg, size);
117 size_t len = strlen(renderp);
122 // Singular/plural specifier.
124 // look at the *previous* numeric parameter
131 /* Version specifier */
133 strcpy(renderp, VERSION);
134 size_t len = strlen(VERSION);
142 // Print the message.
143 printf("%s\n", rendered);
148 void speak(const char* msg, ...)
152 vspeak(msg, true, ap);
156 void sspeak(const long msg, ...)
161 vprintf(arbitrary_messages[msg], ap);
166 void pspeak(vocab_t msg, enum speaktype mode, int skip, bool blank, ...)
167 /* Find the skip+1st message from msg and print it. Modes are:
168 * feel = for inventory, what you can touch
169 * look = the long description for the state the object is in
170 * listen = the sound for the state the object is in
171 * study = text on the object. */
177 vspeak(objects[msg].inventory, blank, ap);
180 vspeak(objects[msg].descriptions[skip], blank, ap);
183 vspeak(objects[msg].sounds[skip], blank, ap);
186 vspeak(objects[msg].texts[skip], blank, ap);
189 vspeak(objects[msg].changes[skip], blank, ap);
195 void rspeak(vocab_t i, ...)
196 /* Print the i-th "random" message (section 6 of database). */
200 vspeak(arbitrary_messages[i], true, ap);
204 void echo_input(FILE* destination, const char* input_prompt, const char* input)
206 size_t len = strlen(input_prompt) + strlen(input) + 1;
207 char* prompt_and_input = (char*) xmalloc(len);
208 strcpy(prompt_and_input, input_prompt);
209 strcat(prompt_and_input, input);
210 fprintf(destination, "%s\n", prompt_and_input);
211 free(prompt_and_input);
214 int word_count(char* str)
216 char delims[] = " \t";
220 for (char *s = str; *s; s++)
222 if (strchr(delims, *s) == 0) {
227 if (strchr(delims, *s) != 0) {
238 char input_prompt[] = "> ";
239 if (!settings.prompt)
240 input_prompt[0] = '\0';
242 // Print a blank line
247 input = readline(input_prompt);
249 if (input == NULL) // Got EOF; return with it.
251 if (input[0] == '#') { // Ignore comments.
255 // We have a 'normal' line; leave the loop.
259 // Strip trailing newlines from the input
260 input[strcspn(input, "\n")] = 0;
265 echo_input(stdout, input_prompt, input);
268 echo_input(settings.logfp, "", input);
275 bool outcome = false;
278 char* reply = get_input();
281 // Should be unreachable. Reply should never be NULL
286 if (strlen(reply) == 0) {
288 rspeak(PLEASE_ANSWER);
292 char* firstword = (char*) xmalloc(strlen(reply) + 1);
293 sscanf(reply, "%s", firstword);
297 for (int i = 0; i < (int)strlen(firstword); ++i)
298 firstword[i] = tolower(firstword[i]);
300 int yes = strncmp("yes", firstword, sizeof("yes") - 1);
301 int y = strncmp("y", firstword, sizeof("y") - 1);
302 int no = strncmp("no", firstword, sizeof("no") - 1);
303 int n = strncmp("n", firstword, sizeof("n") - 1);
311 } else if (no == 0 ||
316 rspeak(PLEASE_ANSWER);
322 bool yes(const char* question, const char* yes_response, const char* no_response)
323 /* Print message X, wait for yes/no answer. If yes, print Y and return true;
324 * if no, print Z and return false. */
326 bool outcome = false;
331 char* reply = get_input();
334 // Should be unreachable. Reply should never be NULL
340 if (strlen(reply) == 0) {
342 rspeak(PLEASE_ANSWER);
346 char* firstword = (char*) xmalloc(strlen(reply) + 1);
347 sscanf(reply, "%s", firstword);
351 for (int i = 0; i < (int)strlen(firstword); ++i)
352 firstword[i] = tolower(firstword[i]);
354 int yes = strncmp("yes", firstword, sizeof("yes") - 1);
355 int y = strncmp("y", firstword, sizeof("y") - 1);
356 int no = strncmp("no", firstword, sizeof("no") - 1);
357 int n = strncmp("n", firstword, sizeof("n") - 1);
366 } else if (no == 0 ||
372 rspeak(PLEASE_ANSWER);
379 /* Data structure routines */
381 static int get_motion_vocab_id(const char* word)
382 // Return the first motion number that has 'word' as one of its words.
384 for (int i = 0; i < NMOTIONS; ++i) {
385 for (int j = 0; j < motions[i].words.n; ++j) {
386 if (strncasecmp(word, motions[i].words.strs[j], TOKLEN) == 0 && (strlen(word) > 1 ||
387 strchr(ignore, word[0]) == NULL ||
392 // If execution reaches here, we didn't find the word.
393 return (WORD_NOT_FOUND);
396 static int get_object_vocab_id(const char* word)
397 // Return the first object number that has 'word' as one of its words.
399 for (int i = 0; i < NOBJECTS + 1; ++i) { // FIXME: the + 1 should go when 1-indexing for objects is removed
400 for (int j = 0; j < objects[i].words.n; ++j) {
401 if (strncasecmp(word, objects[i].words.strs[j], TOKLEN) == 0)
405 // If execution reaches here, we didn't find the word.
406 return (WORD_NOT_FOUND);
409 static int get_action_vocab_id(const char* word)
410 // Return the first motion number that has 'word' as one of its words.
412 for (int i = 0; i < NACTIONS; ++i) {
413 for (int j = 0; j < actions[i].words.n; ++j) {
414 if (strncasecmp(word, actions[i].words.strs[j], TOKLEN) == 0 && (strlen(word) > 1 ||
415 strchr(ignore, word[0]) == NULL ||
420 // If execution reaches here, we didn't find the word.
421 return (WORD_NOT_FOUND);
424 static int get_special_vocab_id(const char* word)
425 // Return the first special number that has 'word' as one of its words.
427 for (int i = 0; i < NSPECIALS; ++i) {
428 for (int j = 0; j < specials[i].words.n; ++j) {
429 if (strncasecmp(word, specials[i].words.strs[j], TOKLEN) == 0)
433 // If execution reaches here, we didn't find the word.
434 return (WORD_NOT_FOUND);
437 void get_vocab_metadata(const char* word, long* id, enum wordtype* type)
439 /* Check for an empty string */
440 if (strncmp(word, "", sizeof("")) == 0) {
442 *type = NO_WORD_TYPE;
448 ref_num = get_motion_vocab_id(word);
449 if (ref_num != WORD_NOT_FOUND) {
455 ref_num = get_object_vocab_id(word);
456 if (ref_num != WORD_NOT_FOUND) {
462 ref_num = get_action_vocab_id(word);
463 if (ref_num != WORD_NOT_FOUND) {
469 ref_num = get_special_vocab_id(word);
470 if (ref_num != WORD_NOT_FOUND) {
476 // Check for the reservoir magic word.
477 if (strcasecmp(word, game.zzword) == 0) {
483 *id = WORD_NOT_FOUND;
484 *type = NO_WORD_TYPE;
488 void juggle(obj_t object)
489 /* Juggle an object by picking it up and putting it down again, the purpose
490 * being to get the object to the front of the chain of things at its loc. */
494 i = game.place[object];
495 j = game.fixed[object];
497 move(object + NOBJECTS, j);
500 void move(obj_t object, loc_t where)
501 /* Place any object anywhere by picking it up and dropping it. May
502 * already be toting, in which case the carry is a no-op. Mustn't
503 * pick up objects which are not at any loc, since carry wants to
504 * remove objects from game.atloc chains. */
508 if (object > NOBJECTS)
509 from = game.fixed[object - NOBJECTS];
511 from = game.place[object];
512 /* (ESR) Used to check for !SPECIAL(from). I *think* that was wrong... */
513 if (from != LOC_NOWHERE && from != CARRIED)
518 long put(obj_t object, loc_t where, long pval)
519 /* put() is the same as move(), except it returns a value used to set up the
520 * negated game.prop values for the repository objects. */
523 return STASHED(pval);
526 void carry(obj_t object, loc_t where)
527 /* Start toting an object, removing it from the list of things at its former
528 * location. Incr holdng unless it was already being toted. If object>NOBJECTS
529 * (moving "fixed" second loc), don't change game.place or game.holdng. */
533 if (object <= NOBJECTS) {
534 if (game.place[object] == CARRIED)
536 game.place[object] = CARRIED;
539 if (game.atloc[where] == object) {
540 game.atloc[where] = game.link[object];
543 temp = game.atloc[where];
544 while (game.link[temp] != object) {
545 temp = game.link[temp];
547 game.link[temp] = game.link[object];
550 void drop(obj_t object, loc_t where)
551 /* Place an object at a given loc, prefixing it onto the game.atloc list. Decr
552 * game.holdng if the object was being toted. */
554 if (object > NOBJECTS)
555 game.fixed[object - NOBJECTS] = where;
557 if (game.place[object] == CARRIED)
559 game.place[object] = where;
561 if (where == LOC_NOWHERE ||
564 game.link[object] = game.atloc[where];
565 game.atloc[where] = object;
568 long atdwrf(loc_t where)
569 /* Return the index of first dwarf at the given location, zero if no dwarf is
570 * there (or if dwarves not active yet), -1 if all dwarves are dead. Ignore
571 * the pirate (6th dwarf). */
579 for (long i = 1; i <= NDWARVES - 1; i++) {
580 if (game.dloc[i] == where)
582 if (game.dloc[i] != 0)
588 /* Utility routines (setbit, tstbit, set_seed, get_next_lcg_value,
591 long setbit(long bit)
592 /* Returns 2**bit for use in constructing bit-masks. */
597 bool tstbit(long mask, int bit)
598 /* Returns true if the specified bit is set in the mask. */
600 return (mask & (1 << bit)) != 0;
603 void set_seed(long seedval)
604 /* Set the LCG seed */
606 game.lcg_x = (unsigned long) seedval % game.lcg_m;
608 // once seed is set, we need to generate the Z`ZZZ word
609 make_zzword(game.zzword);
612 unsigned long get_next_lcg_value(void)
613 /* Return the LCG's current value, and then iterate it. */
615 unsigned long old_x = game.lcg_x;
616 game.lcg_x = (game.lcg_a * game.lcg_x + game.lcg_c) % game.lcg_m;
620 long randrange(long range)
621 /* Return a random integer from [0, range). */
623 return range * get_next_lcg_value() / game.lcg_m;
626 void make_zzword(char zzword[TOKLEN + 1])
628 for (int i = 0; i < 5; ++i) {
629 zzword[i] = 'A' + randrange(26);
631 zzword[1] = '\''; // force second char to apostrophe
636 void bug(enum bugtype num, const char *error_string)
638 fprintf(stderr, "Fatal error %d, %s.\n", num, error_string);
645 void state_change(obj_t obj, long state)
646 /* Object must have a change-message list for this to be useful; only some do */
648 game.prop[obj] = state;
649 pspeak(obj, change, state, true);