From a57d93ce35ccdea6fa4f260bf004d6f8f9924b2f Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Fri, 16 Jun 2017 07:00:57 -0400 Subject: [PATCH] Magic-number elimination. --- actions.c | 18 +++++++++--------- advent.h | 6 +++++- init.c | 4 ++-- main.c | 12 ++++++------ misc.c | 8 ++++---- score.c | 4 ++-- 6 files changed, 28 insertions(+), 24 deletions(-) diff --git a/actions.c b/actions.c index fdb5f85..7a401eb 100644 --- a/actions.c +++ b/actions.c @@ -283,7 +283,7 @@ static int carry(token_t verb, token_t obj) CARRY(BIRD+CAGE-obj,game.loc); CARRY(obj,game.loc); if (obj == BOTTLE && LIQUID() != 0) - game.place[LIQUID()] = -1; + game.place[LIQUID()] = CARRIED; if (GSTONE(obj) && game.prop[obj] != 0) { game.prop[obj]=0; game.prop[CAVITY]=1; @@ -384,7 +384,7 @@ static int discard(token_t verb, token_t obj, bool just_do_it) int k = LIQUID(); if (k == obj)obj=BOTTLE; if (obj == BOTTLE && k != 0) - game.place[k]=0; + game.place[k] = NOWHERE; if (obj == CAGE && game.prop[BIRD] == 1)DROP(BIRD,game.loc); DROP(obj,game.loc); if (obj != BIRD) return GO_CLEAROBJ; @@ -403,8 +403,8 @@ static int drink(token_t verb, token_t obj) if (obj != BLOOD) { if (obj != 0 && obj != WATER)spk=RIDICULOUS_ATTEMPT; if (spk != RIDICULOUS_ATTEMPT && LIQUID() == WATER && HERE(BOTTLE)) { - game.prop[BOTTLE]=1; - game.place[WATER]=0; + game.prop[BOTTLE] = 1; + game.place[WATER] = NOWHERE; spk=BOTTLE_EMPTY; } } else { @@ -535,8 +535,8 @@ int fill(token_t verb, token_t obj) spk=FILL_INVALID; k=LIQUID(); if (k == 0 || !HERE(BOTTLE)) {RSPEAK(spk); return GO_CLEAROBJ;} - game.place[k]=0; - game.prop[BOTTLE]=1; + game.place[k] = NOWHERE; + game.prop[BOTTLE] = 1; if (k == OIL)game.prop[URN]=1; spk=WATER_URN+game.prop[URN]; RSPEAK(spk); @@ -559,7 +559,7 @@ int fill(token_t verb, token_t obj) game.prop[BOTTLE]=MOD(COND[game.loc],4)/2*2; k=LIQUID(); if (TOTING(BOTTLE)) - game.place[k] = -1; + game.place[k] = CARRIED; if (k == OIL) spk=BOTTLED_OIL; } @@ -750,8 +750,8 @@ static int pour(token_t verb, token_t obj) if (obj != OIL && obj != WATER) {RSPEAK(spk); return GO_CLEAROBJ;} if (HERE(URN) && game.prop[URN] == 0) return fill(verb, URN); - game.prop[BOTTLE]=1; - game.place[obj]=0; + game.prop[BOTTLE] = 1; + game.place[obj] = NOWHERE; spk=GROUND_WET; if (!(AT(PLANT) || AT(DOOR))) {RSPEAK(spk); return GO_CLEAROBJ;} diff --git a/advent.h b/advent.h index 1f2bd8c..3802c83 100644 --- a/advent.h +++ b/advent.h @@ -132,7 +132,7 @@ extern int saveresume(FILE *, bool); * TOTING(OBJ) = true if the OBJ is being carried */ #define MOD(N,M) ((N) % (M)) -#define TOTING(OBJ) (game.place[OBJ] == -1) +#define TOTING(OBJ) (game.place[OBJ] == CARRIED) #define AT(OBJ) (game.place[OBJ] == game.loc || game.fixed[OBJ] == game.loc) #define HERE(OBJ) (AT(OBJ) || TOTING(OBJ)) #define LIQ2(PBOTL) ((1-(PBOTL))*WATER+((PBOTL)/2)*(WATER+OIL)) @@ -207,6 +207,10 @@ enum speechpart {unknown, intransitive, transitive}; #define HOGRE 19 /* Trying to deal with ogre */ #define HJADE 20 /* Found all treasures except jade */ +/* Special object statuses in game.place - can also be a location number (> 0) */ +#define CARRIED -1 /* Player is toting it */ +#define NOWHERE 0 /* It's destroyed */ + /* hack to ignore GCC Unused Result */ #define IGNORE(r) do{if (r){}}while(0) diff --git a/init.c b/init.c index bbe155d..d9cdd4d 100644 --- a/init.c +++ b/init.c @@ -176,8 +176,8 @@ void initialise(void) printf("Initialising...\n"); for (int i=1; i<=NOBJECTS; i++) { - game.place[i]=0; - game.prop[i]=0; + game.place[i] = NOWHERE; + game.prop[i] = 0; game.link[i+NOBJECTS]=game.link[i]=0; } diff --git a/main.c b/main.c index fbd7de0..54d88b1 100644 --- a/main.c +++ b/main.c @@ -262,8 +262,8 @@ bool spotted_by_pirate(int i) /* The pirate's spotted him. He leaves him alone once we've * found chest. K counts if a treasure is here. If not, and - * tally=1 for an unseen chest, let the pirate be spotted. - * Note that game.place[CHEST]=0 might mean that he's thrown + * tally=1 for an unseen chest, let the pirate be spotted. Note + * that game.place[CHEST] = NOWHERE might mean that he's thrown * it to the troll, but in that case he's seen the chest * (game.prop=0). */ if (game.loc == game.chloc || game.prop[CHEST] >= 0) @@ -284,7 +284,7 @@ bool spotted_by_pirate(int i) } } /* Force chest placement before player finds last treasure */ - if (game.tally == 1 && snarfed == 0 && game.place[CHEST] == 0 && HERE(LAMP) && game.prop[LAMP] == 1) { + if (game.tally == 1 && snarfed == 0 && game.place[CHEST] == NOWHERE && HERE(LAMP) && game.prop[LAMP] == 1) { RSPEAK(PIRATE_SPOTTED); movechest = true; } @@ -481,8 +481,8 @@ static void croak(FILE *cmdin) score(endgame); if (game.numdie == MAXDIE) score(endgame); - game.place[WATER]=0; - game.place[OIL]=0; + game.place[WATER] = NOWHERE; + game.place[OIL] = NOWHERE; if (TOTING(LAMP)) game.prop[LAMP]=0; for (int j=1; j<=NOBJECTS; j++) { @@ -848,7 +848,7 @@ static void lampcheck(void) if (!game.lmwarn && HERE(LAMP)) { game.lmwarn=true; int spk=GET_BATTERIES; - if (game.place[BATTER] == 0)spk=LAMP_DIM; + if (game.place[BATTER] == NOWHERE)spk=LAMP_DIM; if (game.prop[BATTER] == 1)spk=MISSING_BATTERIES; RSPEAK(spk); } diff --git a/misc.c b/misc.c index 243381a..b4da635 100644 --- a/misc.c +++ b/misc.c @@ -367,7 +367,7 @@ void MOVE(long object, long where) from=game.fixed[object-NOBJECTS]; else from=game.place[object]; - if (from > 0 && !SPECIAL(from)) + if (from > NOWHERE && !SPECIAL(from)) CARRY(object,from); DROP(object,where); } @@ -388,9 +388,9 @@ void CARRY(long object, long where) long temp; if (object <= NOBJECTS) { - if (game.place[object] == -1) + if (game.place[object] == CARRIED) return; - game.place[object]= -1; + game.place[object] = CARRIED; ++game.holdng; } if (game.atloc[where] == object) { @@ -412,7 +412,7 @@ void DROP(long object, long where) game.fixed[object-NOBJECTS] = where; else { - if (game.place[object] == -1) + if (game.place[object] == CARRIED) --game.holdng; game.place[object] = where; } diff --git a/score.c b/score.c index 594b637..645a35e 100644 --- a/score.c +++ b/score.c @@ -41,7 +41,7 @@ void score(enum termination mode) if(i > CHEST)k=16; if(game.prop[i] >= 0) score += 2; - if(game.place[i] == 3 && game.prop[i] == 0) + if(game.place[i] == LOC_BUILDING && game.prop[i] == 0) score += k-2; mxscor += k; } @@ -75,7 +75,7 @@ void score(enum termination mode) mxscor += 45; /* Did he come to Witt's End as he should? */ - if(game.place[MAGZIN] == 108) + if(game.place[MAGZIN] == LOC_WITTSEND) score += 1; mxscor += 1; -- 2.31.1