From b5260417dd83b655a4ebe797bbe0548f7ede4099 Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Wed, 28 Jun 2017 17:07:15 -0400 Subject: [PATCH] Magic-number elimination. --- actions.c | 43 +++++++++++++++++++++++++++---------------- adventure.yaml | 13 +++++++------ main.c | 5 +++-- 3 files changed, 37 insertions(+), 24 deletions(-) diff --git a/actions.c b/actions.c index c79a666..677b609 100644 --- a/actions.c +++ b/actions.c @@ -28,7 +28,8 @@ static int attack(FILE *input, struct command_t *command) if (AT(DRAGON) && game.prop[DRAGON] == 0)obj = obj * NOBJECTS + DRAGON; if (AT(TROLL))obj = obj * NOBJECTS + TROLL; if (AT(OGRE))obj = obj * NOBJECTS + OGRE; - if (HERE(BEAR) && game.prop[BEAR] == 0)obj = obj * NOBJECTS + BEAR; + if (HERE(BEAR) && game.prop[BEAR] == UNTAMED_BEAR) + obj = obj * NOBJECTS + BEAR; if (obj > NOBJECTS) return GO_UNKNOWN; if (obj == 0) { /* Can't attack bird or machine by throwing axe. */ @@ -50,9 +51,8 @@ static int attack(FILE *input, struct command_t *command) DESTROY(BIRD); spk = BIRD_DEAD; } else if (obj == VEND) { - bool blocking = (game.prop[VEND] == VEND_BLOCKS); - game.prop[VEND] = blocking ? VEND_UNBLOCKS : VEND_BLOCKS; - rspeak(blocking ? MACHINE_SWINGOUT : MACHINE_SWINGBACK); + state_change(VEND, + game.prop[VEND]==VEND_BLOCKS ? VEND_UNBLOCKS : VEND_BLOCKS); return GO_CLEAROBJ; } @@ -230,8 +230,10 @@ static int vcarry(token_t verb, token_t obj) } spk = YOU_JOKING; if (obj == PLANT && game.prop[PLANT] <= 0)spk = DEEP_ROOTS; - if (obj == BEAR && game.prop[BEAR] == 1)spk = BEAR_CHAINED; - if (obj == CHAIN && game.prop[BEAR] != 0)spk = STILL_LOCKED; + if (obj == BEAR && game.prop[BEAR] == SITTING_BEAR) + spk = BEAR_CHAINED; + if (obj == CHAIN && game.prop[BEAR] != UNTAMED_BEAR) + spk = STILL_LOCKED; if (obj == URN)spk = URN_NOBUDGE; if (obj == CAVITY)spk = DOUGHNUT_HOLES; if (obj == BLOOD)spk = FEW_DROPS; @@ -298,26 +300,33 @@ static int chain(token_t verb) int spk; if (verb != LOCK) { spk = CHAIN_UNLOCKED; - if (game.prop[BEAR] == 0)spk = BEAR_BLOCKS; - if (game.prop[CHAIN] == 0)spk = ALREADY_UNLOCKED; + if (game.prop[BEAR] == UNTAMED_BEAR) + spk = BEAR_BLOCKS; + if (game.prop[CHAIN] == 0) + spk = ALREADY_UNLOCKED; if (spk != CHAIN_UNLOCKED) { rspeak(spk); return GO_CLEAROBJ; } game.prop[CHAIN] = 0; game.fixed[CHAIN] = 0; - if (game.prop[BEAR] != 3)game.prop[BEAR] = 2; + if (game.prop[BEAR] != BEAR_DEAD) + game.prop[BEAR] = CONTENTED_BEAR; + /* FIXME: Arithmetic on state numbers */ game.fixed[BEAR] = 2 - game.prop[BEAR]; } else { spk = CHAIN_LOCKED; - if (game.prop[CHAIN] != 0)spk = ALREADY_LOCKED; - if (game.loc != objects[CHAIN].plac)spk = NO_LOCKSITE; + if (game.prop[CHAIN] != 0) + spk = ALREADY_LOCKED; + if (game.loc != objects[CHAIN].plac) + spk = NO_LOCKSITE; if (spk != CHAIN_LOCKED) { rspeak(spk); return GO_CLEAROBJ; } game.prop[CHAIN] = 2; - if (TOTING(CHAIN))drop(CHAIN, game.loc); + if (TOTING(CHAIN)) + drop(CHAIN, game.loc); game.fixed[CHAIN] = -1; } rspeak(spk); @@ -492,11 +501,13 @@ static int feed(token_t verb, token_t obj) spk = REALLY_MAD; } } else if (obj == BEAR) { - if (game.prop[BEAR] == 0)spk = NOTHING_EDIBLE; - if (game.prop[BEAR] == 3)spk = RIDICULOUS_ATTEMPT; + if (game.prop[BEAR] == UNTAMED_BEAR) + spk = NOTHING_EDIBLE; + if (game.prop[BEAR] == BEAR_DEAD) + spk = RIDICULOUS_ATTEMPT; if (HERE(FOOD)) { DESTROY(FOOD); - game.prop[BEAR] = 1; + game.prop[BEAR] = SITTING_BEAR; game.fixed[AXE] = 0; game.prop[AXE] = 0; spk = BEAR_TAMED; @@ -931,7 +942,7 @@ static int throw (FILE *cmdin, struct command_t *command) return throw_support(TROLL_RETURNS); else if (AT(OGRE)) return throw_support(OGRE_DODGE); - else if (HERE(BEAR) && game.prop[BEAR] == 0) { + else if (HERE(BEAR) && game.prop[BEAR] == UNTAMED_BEAR) { /* This'll teach him to throw the axe at the bear! */ drop(AXE, game.loc); game.fixed[AXE] = -1; diff --git a/adventure.yaml b/adventure.yaml index f7f867d..1e7bc32 100644 --- a/adventure.yaml +++ b/adventure.yaml @@ -2969,8 +2969,6 @@ arbitrary_messages: !!omap - ADVENTURE_NEWS: 'Open Adventure is an author-approved open-source release of\nVersion 2.5 with, as yet, no gameplay changes.\nVersion 2.5 was essentially the same as Version II; the cave and the\nhazards therein are unchanged, and top score is still 430 points.\nThere are a few more hints, especially for some of the more obscure\npuzzles. There are a few minor bugfixes and cosmetic changes. You\ncan now save a game and resume it at once (formerly you had to wait a\nwhile first), but it now costs you a few points each time you save the\ngame. Saved games are now stored in much smaller files than before.' - GO_UNNEEDED: 'You don''t have to say "go" every time; just specify a direction or, if\nit''s nearby, name the place to which you wish to move.' - ARB_279: !!null -- MACHINE_SWINGOUT: 'As you strike the vending machine, it pivots backward along with a\nsection of wall, revealing a dark passage leading south.' -- MACHINE_SWINGBACK: 'The vending machine swings back to block the passage.' classes: - threshold: 0 @@ -3295,10 +3293,10 @@ objects: !!omap locations: LOC_BARRENROOM immovable: true descriptions: - - 'There is a ferocious cave bear eying you from the far end of the room!' - - 'There is a gentle cave bear sitting placidly in one corner.' - - 'There is a contented-looking bear wandering about nearby.' - - '' # dead + - [UNTAMED_BEAR, 'There is a ferocious cave bear eying you from the far end of the room!'] + - [SITTING_BEAR, 'There is a gentle cave bear sitting placidly in one corner.'] + - [CONTENTED_BEAR, 'There is a contented-looking bear wandering about nearby.'] + - [BEAR_DEAD, ''] - MESSAG: words: ['messa'] inventory: '*message in second maze' @@ -3322,6 +3320,9 @@ objects: !!omap descriptions: - [VEND_BLOCKS, 'There is a massive and somewhat battered vending machine here. The\ninstructions on it read: "Drop coins here to receive fresh batteries."'] - [VEND_UNBLOCKS, 'There is a massive vending machine here, swung back to reveal a\nsouthward passage.'] + changes: + - 'The vending machine swings back to block the passage.' + - 'As you strike the vending machine, it pivots backward along with a\nsection of wall, revealing a dark passage leading south.' texts: - '"Drop coins here to receive fresh batteries."' - '"Drop coins here to receive fresh batteries."' diff --git a/main.c b/main.c index 3f41409..8f98db5 100644 --- a/main.c +++ b/main.c @@ -717,7 +717,7 @@ static bool playermove(token_t verb, int motion) game.prop[TROLL] = 2; drop(BEAR, game.newloc); game.fixed[BEAR] = -1; - game.prop[BEAR] = 3; + game.prop[BEAR] = BEAR_DEAD; game.oldlc2 = game.newloc; croak(); return true; @@ -780,7 +780,8 @@ static bool closecheck(void) move(TROLL2, objects[TROLL].plac); move(TROLL2 + NOBJECTS, objects[TROLL].fixd); juggle(CHASM); - if (game.prop[BEAR] != 3)DESTROY(BEAR); + if (game.prop[BEAR] != BEAR_DEAD) + DESTROY(BEAR); game.prop[CHAIN] = 0; game.fixed[CHAIN] = 0; game.prop[AXE] = 0; -- 2.31.1