Rationalize attack logic

Fix the compiler warnings about bunching ands with ors
This commit is contained in:
NHOrus 2017-07-02 19:41:57 +03:00
parent 346bcf9458
commit 8456b866ff
2 changed files with 49 additions and 27 deletions

View file

@ -19,40 +19,62 @@ static int attack(struct command_t *command)
{ {
vocab_t verb = command->verb; vocab_t verb = command->verb;
vocab_t obj = command->obj; vocab_t obj = command->obj;
if (obj == INTRANSITIVE) {
return GO_UNKNOWN;
}
long spk = actions[verb].message; long spk = actions[verb].message;
if (obj == 0 || obj == INTRANSITIVE) { if (obj == 0 || obj == INTRANSITIVE) {
if (atdwrf(game.loc) > 0) int changes = 0;
if (atdwrf(game.loc) > 0) {
obj = DWARF; obj = DWARF;
if (HERE(SNAKE)) ++changes;
obj = obj * NOBJECTS + SNAKE; }
if (AT(DRAGON) && game.prop[DRAGON] == DRAGON_BARS) if (HERE(SNAKE)) {
obj = obj * NOBJECTS + DRAGON; obj = SNAKE;
if (AT(TROLL)) ++changes;
obj = obj * NOBJECTS + TROLL; }
if (AT(OGRE)) if (AT(DRAGON) && game.prop[DRAGON] == DRAGON_BARS) {
obj = obj * NOBJECTS + OGRE; obj = DRAGON;
if (HERE(BEAR) && game.prop[BEAR] == UNTAMED_BEAR) ++changes;
obj = obj * NOBJECTS + BEAR; }
if (obj > NOBJECTS) if (AT(TROLL)) {
return GO_UNKNOWN; 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) { if (obj == 0) {
/* Can't attack bird or machine by throwing axe. */ /* Can't attack bird or machine by throwing axe. */
if (HERE(BIRD) && verb != THROW) if (HERE(BIRD) && verb != THROW) {
obj = BIRD; obj = BIRD;
if (HERE(VEND) && verb != THROW) ++changes;
obj = obj * NOBJECTS + VEND; }
if (HERE(VEND) && verb != THROW) {
obj = VEND;
++changes;
}
/* Clam and oyster both treated as clam for intransitive case; /* Clam and oyster both treated as clam for intransitive case;
* no harm done. */ * no harm done. */
if (HERE(CLAM) || HERE(OYSTER)) if (HERE(CLAM) || HERE(OYSTER)) {
obj = NOBJECTS * obj + CLAM; obj = CLAM;
if (obj > NOBJECTS) ++changes;
return GO_UNKNOWN; }
} }
if (changes >= 2)
return GO_UNKNOWN;
} }
if (obj == BIRD) { if (obj == BIRD) {
spk = UNHAPPY_BIRD;
if (game.closed) { if (game.closed) {
rspeak(spk); rspeak(UNHAPPY_BIRD);
return GO_CLEAROBJ; return GO_CLEAROBJ;
} }
DESTROY(BIRD); DESTROY(BIRD);
@ -529,8 +551,8 @@ static int extinguish(token_t verb, int obj)
if (HERE(URN) && game.prop[URN] == URN_LIT) if (HERE(URN) && game.prop[URN] == URN_LIT)
obj = URN; obj = URN;
if (obj == INTRANSITIVE || if (obj == INTRANSITIVE ||
HERE(LAMP) && game.prop[LAMP] == LAMP_BRIGHT && (HERE(LAMP) && game.prop[LAMP] == LAMP_BRIGHT &&
HERE(URN) && game.prop[URN] == URN_LIT) HERE(URN) && game.prop[URN] == URN_LIT))
return GO_UNKNOWN; return GO_UNKNOWN;
} }
@ -742,8 +764,8 @@ static int light(token_t verb, token_t obj)
if (HERE(URN) && game.prop[URN] == URN_DARK) if (HERE(URN) && game.prop[URN] == URN_DARK)
obj = URN; obj = URN;
if (obj == INTRANSITIVE || if (obj == INTRANSITIVE ||
HERE(LAMP) && game.prop[LAMP] == LAMP_DARK && game.limit >= 0 && (HERE(LAMP) && game.prop[LAMP] == LAMP_DARK && game.limit >= 0 &&
HERE(URN) && game.prop[URN] == URN_DARK) HERE(URN) && game.prop[URN] == URN_DARK))
return GO_UNKNOWN; return GO_UNKNOWN;
} }