Rationalize attack logic
[open-adventure.git] / actions.c
index 409e58e110eaaceb23718318dab19583f7dc7692..dd6d64b6e3bf01cda79554758b47a287fa26f305 100644 (file)
--- a/actions.c
+++ b/actions.c
@@ -19,40 +19,62 @@ static int attack(struct command_t *command)
 {
     vocab_t verb = command->verb;
     vocab_t obj = command->obj;
+
+    if (obj == INTRANSITIVE) {
+        return GO_UNKNOWN;
+    }
     long spk = actions[verb].message;
     if (obj == 0 || obj == INTRANSITIVE) {
-        if (atdwrf(game.loc) > 0)
+        int changes = 0;
+        if (atdwrf(game.loc) > 0) {
             obj = DWARF;
-        if (HERE(SNAKE))
-            obj = obj * NOBJECTS + SNAKE;
-        if (AT(DRAGON) && game.prop[DRAGON] == DRAGON_BARS)
-            obj = obj * NOBJECTS + DRAGON;
-        if (AT(TROLL))
-            obj = obj * NOBJECTS + TROLL;
-        if (AT(OGRE))
-            obj = obj * NOBJECTS + OGRE;
-        if (HERE(BEAR) && game.prop[BEAR] == UNTAMED_BEAR)
-            obj = obj * NOBJECTS + BEAR;
-        if (obj > NOBJECTS)
-            return GO_UNKNOWN;
+            ++changes;
+        }
+        if (HERE(SNAKE)) {
+            obj = SNAKE;
+            ++changes;
+        }
+        if (AT(DRAGON) && game.prop[DRAGON] == DRAGON_BARS) {
+            obj = DRAGON;
+            ++changes;
+        }
+        if (AT(TROLL)) {
+            obj = TROLL;
+            ++changes;
+        }
+        if (AT(OGRE)) {
+            obj = OGRE;
+            ++changes;
+        }
+        if (HERE(BEAR) && game.prop[BEAR] == UNTAMED_BEAR) {
+            obj = BEAR;
+            ++changes;
+        }
+        /* check for low-priority targets */
         if (obj == 0) {
             /* Can't attack bird or machine by throwing axe. */
-            if (HERE(BIRD) && verb != THROW)
+            if (HERE(BIRD) && verb != THROW) {
                 obj = BIRD;
-            if (HERE(VEND) && verb != THROW)
-                obj = obj * NOBJECTS + VEND;
+                ++changes;
+            }
+            if (HERE(VEND) && verb != THROW) {
+                obj = VEND;
+                ++changes;
+            }
             /* Clam and oyster both treated as clam for intransitive case;
              * no harm done. */
-            if (HERE(CLAM) || HERE(OYSTER))
-                obj = NOBJECTS * obj + CLAM;
-            if (obj > NOBJECTS)
-                return GO_UNKNOWN;
+            if (HERE(CLAM) || HERE(OYSTER)) {
+                obj = CLAM;
+                ++changes;
+            }
         }
+        if (changes >= 2)
+            return GO_UNKNOWN;
+
     }
     if (obj == BIRD) {
-        spk = UNHAPPY_BIRD;
         if (game.closed) {
-            rspeak(spk);
+            rspeak(UNHAPPY_BIRD);
             return GO_CLEAROBJ;
         }
         DESTROY(BIRD);
@@ -523,25 +545,33 @@ static int eat(token_t verb, token_t obj)
 static int extinguish(token_t verb, int obj)
 /* Extinguish.  Lamp, urn, dragon/volcano (nice try). */
 {
-    int spk = actions[verb].message;
     if (obj == INTRANSITIVE) {
         if (HERE(LAMP) && game.prop[LAMP] == LAMP_BRIGHT)
             obj = LAMP;
-        if (HERE(URN) && game.prop[URN] == 2)
-            obj = obj * NOBJECTS + URN;
-        if (obj == INTRANSITIVE || obj == 0 || obj > NOBJECTS)
+        if (HERE(URN) && game.prop[URN] == URN_LIT)
+            obj = URN;
+        if (obj == INTRANSITIVE ||
+            (HERE(LAMP) && game.prop[LAMP] == LAMP_BRIGHT &&
+             HERE(URN) && game.prop[URN] == URN_LIT))
             return GO_UNKNOWN;
     }
 
     if (obj == URN) {
-        game.prop[URN] = game.prop[URN] / 2;
-        spk = URN_DARK;
+        if (game.prop[URN] != URN_EMPTY) {
+            state_change(URN, URN_DARK);
+        } else {
+            pspeak(URN, change, URN_DARK);
+        }
+
     } else if (obj == LAMP) {
         state_change(LAMP, LAMP_DARK);
-        spk = DARK(game.loc) ? PITCH_DARK : NO_MESSAGE;
-    } else if (obj == DRAGON || obj == VOLCANO)
-        spk = BEYOND_POWER;
-    rspeak(spk);
+        rspeak(DARK(game.loc) ? PITCH_DARK : NO_MESSAGE);
+    } else if (obj == DRAGON || obj == VOLCANO) {
+        rspeak(BEYOND_POWER);
+
+    } else {
+        rspeak(actions[verb].message);
+    }
     return GO_CLEAROBJ;
 }
 
@@ -728,32 +758,27 @@ static int inven(void)
 static int light(token_t verb, token_t obj)
 /*  Light.  Applicable only to lamp and urn. */
 {
-    int spk = actions[verb].message;
     if (obj == INTRANSITIVE) {
         if (HERE(LAMP) && game.prop[LAMP] == LAMP_DARK && game.limit >= 0)
             obj = LAMP;
-        if (HERE(URN) && game.prop[URN] == 1)
-            obj = obj * NOBJECTS + URN;
-        if (obj == INTRANSITIVE || obj == 0 || obj > NOBJECTS)
+        if (HERE(URN) && game.prop[URN] == URN_DARK)
+            obj =  URN;
+        if (obj == INTRANSITIVE ||
+            (HERE(LAMP) && game.prop[LAMP] == LAMP_DARK && game.limit >= 0 &&
+             HERE(URN) && game.prop[URN] == URN_DARK))
             return GO_UNKNOWN;
     }
 
     if (obj == URN) {
-        if (game.prop[URN] == 0) {
-            rspeak(URN_EMPTY);
-        } else {
-            game.prop[URN] = 2;
-            rspeak(URN_LIT);
-        }
+        state_change(URN, game.prop[URN] == URN_EMPTY ? URN_EMPTY : URN_LIT);
         return GO_CLEAROBJ;
     } else {
         if (obj != LAMP) {
-            rspeak(spk);
+            rspeak(actions[verb].message);
             return GO_CLEAROBJ;
         }
-        spk = LAMP_OUT;
         if (game.limit < 0) {
-            rspeak(spk);
+            rspeak(LAMP_OUT);
             return GO_CLEAROBJ;
         }
         state_change(LAMP, LAMP_BRIGHT);
@@ -808,8 +833,6 @@ static int lock(token_t verb, token_t obj)
             obj = DOOR;
         if (AT(GRATE))
             obj = GRATE;
-        if (obj != 0 && HERE(CHAIN))
-            return GO_UNKNOWN;
         if (HERE(CHAIN))
             obj = CHAIN;
         if (obj == 0 || obj == INTRANSITIVE) {
@@ -824,7 +847,7 @@ static int lock(token_t verb, token_t obj)
         return bivalve(verb, obj);
     if (obj == DOOR)
         spk = RUSTY_DOOR;
-    if (obj == DOOR && game.prop[DOOR] == 1)
+    if (obj == DOOR && game.prop[DOOR] == DOOR_UNRUSTED)
         spk = OK_MAN;
     if (obj == CAGE)
         spk = NO_LOCK;
@@ -868,7 +891,7 @@ static int pour(token_t verb, token_t obj)
         rspeak(spk);
         return GO_CLEAROBJ;
     }
-    if (HERE(URN) && game.prop[URN] == 0)
+    if (HERE(URN) && game.prop[URN] == URN_EMPTY)
         return fill(verb, URN);
     game.prop[BOTTLE] = EMPTY_BOTTLE;
     game.place[obj] = LOC_NOWHERE;
@@ -951,7 +974,7 @@ static int rub(token_t verb, token_t obj)
     int spk = actions[verb].message;
     if (obj != LAMP)
         spk = PECULIAR_NOTHING;
-    if (obj == URN && game.prop[URN] == 2) {
+    if (obj == URN && game.prop[URN] == URN_LIT) {
         DESTROY(URN);
         drop(AMBER, game.loc);
         game.prop[AMBER] = 1;