Eliminate last logic guard on a packed value.
[open-adventure.git] / misc.c
diff --git a/misc.c b/misc.c
index be2599098d70d5398e12934485ab982299e8b87d..ae5048d9a92825b948f770c4b218f9f83559abc8 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -128,16 +128,6 @@ void tokenize(char* raw, struct command_t *cmd)
 
 /* Hide the fact that wods are corrently packed longs */
 
-bool wordeq(token_t a, token_t b)
-{
-    return a == b;
-}
-
-bool wordempty(token_t a)
-{
-    return a == 0;
-}
-
 void wordclear(token_t *v)
 {
     *v = 0;
@@ -145,7 +135,7 @@ void wordclear(token_t *v)
 
 /*  I/O routines (speak, pspeak, rspeak, sspeak, get_input, yes) */
 
-void vspeak(const char* msg, bool blank, va_list ap)
+static void vspeak(const char* msg, bool blank, va_list ap)
 {
     // Do nothing if we got a null pointer.
     if (msg == NULL)
@@ -171,8 +161,17 @@ void vspeak(const char* msg, bool blank, va_list ap)
     long previous_arg = 0;
     for (int i = 0; i < msglen; i++) {
         if (msg[i] != '%') {
-            *renderp++ = msg[i];
-            size--;
+            /* Ugh.  Least obtrusive way to deal with artifacts "on the floor"
+             * being dropped outside of both cave and building. */
+            if (strncmp(msg + i, "floor", 5) == 0 && strchr(" .", msg[i + 5]) && !INSIDE(game.loc)) {
+                strcpy(renderp, "ground");
+                renderp += 6;
+                i += 4;
+                size -= 5;
+            } else {
+                *renderp++ = msg[i];
+                size--;
+            }
         } else {
             long arg = va_arg(ap, long);
             if (arg == -1)
@@ -328,11 +327,12 @@ char* get_input()
 
         if (input == NULL) // Got EOF; return with it.
             return (input);
-        else if (input[0] == '#') { // Ignore comments.
+        if (input[0] == '#') { // Ignore comments.
             free(input);
             continue;
-        } else // We have a 'normal' line; leave the loop.
-            break;
+        }
+        // We have a 'normal' line; leave the loop.
+        break;
     }
 
     // Strip trailing newlines from the input
@@ -351,7 +351,7 @@ char* get_input()
 
 bool silent_yes()
 {
-    bool outcome;
+    bool outcome = false;
 
     for (;;) {
         char* reply = get_input();
@@ -362,6 +362,11 @@ bool silent_yes()
             exit(EXIT_SUCCESS);
             // LCOV_EXCL_STOP
         }
+        if (strlen(reply) == 0) {
+            free(reply);
+            rspeak(PLEASE_ANSWER);
+            continue;
+        }
 
         char* firstword = (char*) xmalloc(strlen(reply) + 1);
         sscanf(reply, "%s", firstword);
@@ -397,7 +402,7 @@ bool yes(const char* question, const char* yes_response, const char* no_response
 /*  Print message X, wait for yes/no answer.  If yes, print Y and return true;
  *  if no, print Z and return false. */
 {
-    bool outcome;
+    bool outcome = false;
 
     for (;;) {
         speak(question);
@@ -411,6 +416,12 @@ bool yes(const char* question, const char* yes_response, const char* no_response
             // LCOV_EXCL_STOP
         }
 
+        if (strlen(reply) == 0) {
+            free(reply);
+            rspeak(PLEASE_ANSWER);
+            continue;
+        }
+
         char* firstword = (char*) xmalloc(strlen(reply) + 1);
         sscanf(reply, "%s", firstword);
 
@@ -451,7 +462,7 @@ int get_motion_vocab_id(const char* word)
 {
     for (int i = 0; i < NMOTIONS; ++i) {
         for (int j = 0; j < motions[i].words.n; ++j) {
-            if (strcasecmp(word, motions[i].words.strs[j]) == 0 && (strlen(word) > 1 ||
+            if (strncasecmp(word, motions[i].words.strs[j], TOKLEN) == 0 && (strlen(word) > 1 ||
                     strchr(ignore, word[0]) == NULL ||
                     !settings.oldstyle))
                 return (i);
@@ -466,7 +477,7 @@ int get_object_vocab_id(const char* word)
 {
     for (int i = 0; i < NOBJECTS + 1; ++i) { // FIXME: the + 1 should go when 1-indexing for objects is removed
         for (int j = 0; j < objects[i].words.n; ++j) {
-            if (strcasecmp(word, objects[i].words.strs[j]) == 0)
+            if (strncasecmp(word, objects[i].words.strs[j], TOKLEN) == 0)
                 return (i);
         }
     }
@@ -479,7 +490,7 @@ int get_action_vocab_id(const char* word)
 {
     for (int i = 0; i < NACTIONS; ++i) {
         for (int j = 0; j < actions[i].words.n; ++j) {
-            if (strcasecmp(word, actions[i].words.strs[j]) == 0 && (strlen(word) > 1 ||
+            if (strncasecmp(word, actions[i].words.strs[j], TOKLEN) == 0 && (strlen(word) > 1 ||
                     strchr(ignore, word[0]) == NULL ||
                     !settings.oldstyle))
                 return (i);
@@ -494,7 +505,7 @@ int get_special_vocab_id(const char* word)
 {
     for (int i = 0; i < NSPECIALS; ++i) {
         for (int j = 0; j < specials[i].words.n; ++j) {
-            if (strcasecmp(word, specials[i].words.strs[j]) == 0)
+            if (strncasecmp(word, specials[i].words.strs[j], TOKLEN) == 0)
                 return (i);
         }
     }
@@ -502,37 +513,55 @@ int get_special_vocab_id(const char* word)
     return (WORD_NOT_FOUND);
 }
 
-long get_vocab_id(const char* word)
-// Search the vocab categories in order for the supplied word.
+void get_vocab_metadata(const char* word, long* id, enum wordtype* type)
 {
     /* Check for an empty string */
-    if (strncmp(word, "", sizeof("")) == 0)
-        return (WORD_EMPTY);
+    if (strncmp(word, "", sizeof("")) == 0) {
+        *id = WORD_EMPTY;
+        *type = NO_WORD_TYPE;
+        return;
+    }
 
     long ref_num;
 
-    /* FIXME: Magic numbers related to vocabulary */
     ref_num = get_motion_vocab_id(word);
-    if (ref_num != WORD_NOT_FOUND)
-        return MOTION_WORD(ref_num);
+    if (ref_num != WORD_NOT_FOUND) {
+        *id = ref_num;
+        *type = MOTION;
+        return;
+    }
 
     ref_num = get_object_vocab_id(word);
-    if (ref_num != WORD_NOT_FOUND)
-        return OBJECT_WORD(ref_num);
+    if (ref_num != WORD_NOT_FOUND) {
+        *id = ref_num;
+        *type = OBJECT;
+        return;
+    }
 
     ref_num = get_action_vocab_id(word);
-    if (ref_num != WORD_NOT_FOUND)
-        return ACTION_WORD(ref_num);
+    if (ref_num != WORD_NOT_FOUND) {
+        *id = ref_num;
+        *type = ACTION;
+        return;
+    }
 
     ref_num = get_special_vocab_id(word);
-    if (ref_num != WORD_NOT_FOUND)
-        return SPECIAL_WORD(ref_num);
+    if (ref_num != WORD_NOT_FOUND) {
+        *id = ref_num;
+        *type = SPECIAL;
+        return;
+    }
 
     // Check for the reservoir magic word.
-    if (strcasecmp(word, game.zzword) == 0)
-        return ACTION_WORD(PART);
+    if (strcasecmp(word, game.zzword) == 0) {
+        *id = PART;
+        *type = ACTION;
+        return;
+    }
 
-    return (WORD_NOT_FOUND);
+    *id = WORD_NOT_FOUND;
+    *type = NO_WORD_TYPE;
+    return;
 }
 
 void juggle(obj_t object)
@@ -559,7 +588,8 @@ void move(obj_t object, loc_t where)
         from = game.fixed[object - NOBJECTS];
     else
         from = game.place[object];
-    if (from != LOC_NOWHERE && from != CARRIED && !SPECIAL(from))
+    /* (ESR) Used to check for !SPECIAL(from). I *think* that was wrong... */
+    if (from != LOC_NOWHERE && from != CARRIED)
         carry(object, from);
     drop(object, where);
 }
@@ -607,7 +637,8 @@ void drop(obj_t object, loc_t where)
             --game.holdng;
         game.place[object] = where;
     }
-    if (where <= 0)
+    if (where == LOC_NOWHERE ||
+        where == CARRIED)
         return;
     game.link[object] = game.atloc[where];
     game.atloc[where] = object;