Magic-number elimination.

This commit is contained in:
Eric S. Raymond 2017-06-16 07:00:57 -04:00
parent 1b5ab6c808
commit a57d93ce35
6 changed files with 28 additions and 24 deletions

View file

@ -283,7 +283,7 @@ static int carry(token_t verb, token_t obj)
CARRY(BIRD+CAGE-obj,game.loc); CARRY(BIRD+CAGE-obj,game.loc);
CARRY(obj,game.loc); CARRY(obj,game.loc);
if (obj == BOTTLE && LIQUID() != 0) if (obj == BOTTLE && LIQUID() != 0)
game.place[LIQUID()] = -1; game.place[LIQUID()] = CARRIED;
if (GSTONE(obj) && game.prop[obj] != 0) { if (GSTONE(obj) && game.prop[obj] != 0) {
game.prop[obj]=0; game.prop[obj]=0;
game.prop[CAVITY]=1; game.prop[CAVITY]=1;
@ -384,7 +384,7 @@ static int discard(token_t verb, token_t obj, bool just_do_it)
int k = LIQUID(); int k = LIQUID();
if (k == obj)obj=BOTTLE; if (k == obj)obj=BOTTLE;
if (obj == BOTTLE && k != 0) if (obj == BOTTLE && k != 0)
game.place[k]=0; game.place[k] = NOWHERE;
if (obj == CAGE && game.prop[BIRD] == 1)DROP(BIRD,game.loc); if (obj == CAGE && game.prop[BIRD] == 1)DROP(BIRD,game.loc);
DROP(obj,game.loc); DROP(obj,game.loc);
if (obj != BIRD) return GO_CLEAROBJ; if (obj != BIRD) return GO_CLEAROBJ;
@ -404,7 +404,7 @@ static int drink(token_t verb, token_t obj)
if (obj != 0 && obj != WATER)spk=RIDICULOUS_ATTEMPT; if (obj != 0 && obj != WATER)spk=RIDICULOUS_ATTEMPT;
if (spk != RIDICULOUS_ATTEMPT && LIQUID() == WATER && HERE(BOTTLE)) { if (spk != RIDICULOUS_ATTEMPT && LIQUID() == WATER && HERE(BOTTLE)) {
game.prop[BOTTLE] = 1; game.prop[BOTTLE] = 1;
game.place[WATER]=0; game.place[WATER] = NOWHERE;
spk=BOTTLE_EMPTY; spk=BOTTLE_EMPTY;
} }
} else { } else {
@ -535,7 +535,7 @@ int fill(token_t verb, token_t obj)
spk=FILL_INVALID; spk=FILL_INVALID;
k=LIQUID(); k=LIQUID();
if (k == 0 || !HERE(BOTTLE)) {RSPEAK(spk); return GO_CLEAROBJ;} if (k == 0 || !HERE(BOTTLE)) {RSPEAK(spk); return GO_CLEAROBJ;}
game.place[k]=0; game.place[k] = NOWHERE;
game.prop[BOTTLE] = 1; game.prop[BOTTLE] = 1;
if (k == OIL)game.prop[URN]=1; if (k == OIL)game.prop[URN]=1;
spk=WATER_URN+game.prop[URN]; spk=WATER_URN+game.prop[URN];
@ -559,7 +559,7 @@ int fill(token_t verb, token_t obj)
game.prop[BOTTLE]=MOD(COND[game.loc],4)/2*2; game.prop[BOTTLE]=MOD(COND[game.loc],4)/2*2;
k=LIQUID(); k=LIQUID();
if (TOTING(BOTTLE)) if (TOTING(BOTTLE))
game.place[k] = -1; game.place[k] = CARRIED;
if (k == OIL) if (k == OIL)
spk=BOTTLED_OIL; spk=BOTTLED_OIL;
} }
@ -751,7 +751,7 @@ static int pour(token_t verb, token_t obj)
if (HERE(URN) && game.prop[URN] == 0) if (HERE(URN) && game.prop[URN] == 0)
return fill(verb, URN); return fill(verb, URN);
game.prop[BOTTLE] = 1; game.prop[BOTTLE] = 1;
game.place[obj]=0; game.place[obj] = NOWHERE;
spk=GROUND_WET; spk=GROUND_WET;
if (!(AT(PLANT) || AT(DOOR))) if (!(AT(PLANT) || AT(DOOR)))
{RSPEAK(spk); return GO_CLEAROBJ;} {RSPEAK(spk); return GO_CLEAROBJ;}

View file

@ -132,7 +132,7 @@ extern int saveresume(FILE *, bool);
* TOTING(OBJ) = true if the OBJ is being carried */ * TOTING(OBJ) = true if the OBJ is being carried */
#define MOD(N,M) ((N) % (M)) #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 AT(OBJ) (game.place[OBJ] == game.loc || game.fixed[OBJ] == game.loc)
#define HERE(OBJ) (AT(OBJ) || TOTING(OBJ)) #define HERE(OBJ) (AT(OBJ) || TOTING(OBJ))
#define LIQ2(PBOTL) ((1-(PBOTL))*WATER+((PBOTL)/2)*(WATER+OIL)) #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 HOGRE 19 /* Trying to deal with ogre */
#define HJADE 20 /* Found all treasures except jade */ #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 */ /* hack to ignore GCC Unused Result */
#define IGNORE(r) do{if (r){}}while(0) #define IGNORE(r) do{if (r){}}while(0)

2
init.c
View file

@ -176,7 +176,7 @@ void initialise(void)
printf("Initialising...\n"); printf("Initialising...\n");
for (int i=1; i<=NOBJECTS; i++) { for (int i=1; i<=NOBJECTS; i++) {
game.place[i]=0; game.place[i] = NOWHERE;
game.prop[i] = 0; game.prop[i] = 0;
game.link[i+NOBJECTS]=game.link[i]=0; game.link[i+NOBJECTS]=game.link[i]=0;
} }

12
main.c
View file

@ -262,8 +262,8 @@ bool spotted_by_pirate(int i)
/* The pirate's spotted him. He leaves him alone once we've /* The pirate's spotted him. He leaves him alone once we've
* found chest. K counts if a treasure is here. If not, and * found chest. K counts if a treasure is here. If not, and
* tally=1 for an unseen chest, let the pirate be spotted. * tally=1 for an unseen chest, let the pirate be spotted. Note
* Note that game.place[CHEST]=0 might mean that he's thrown * that game.place[CHEST] = NOWHERE might mean that he's thrown
* it to the troll, but in that case he's seen the chest * it to the troll, but in that case he's seen the chest
* (game.prop=0). */ * (game.prop=0). */
if (game.loc == game.chloc || game.prop[CHEST] >= 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 */ /* 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); RSPEAK(PIRATE_SPOTTED);
movechest = true; movechest = true;
} }
@ -481,8 +481,8 @@ static void croak(FILE *cmdin)
score(endgame); score(endgame);
if (game.numdie == MAXDIE) if (game.numdie == MAXDIE)
score(endgame); score(endgame);
game.place[WATER]=0; game.place[WATER] = NOWHERE;
game.place[OIL]=0; game.place[OIL] = NOWHERE;
if (TOTING(LAMP)) if (TOTING(LAMP))
game.prop[LAMP]=0; game.prop[LAMP]=0;
for (int j=1; j<=NOBJECTS; j++) { for (int j=1; j<=NOBJECTS; j++) {
@ -848,7 +848,7 @@ static void lampcheck(void)
if (!game.lmwarn && HERE(LAMP)) { if (!game.lmwarn && HERE(LAMP)) {
game.lmwarn=true; game.lmwarn=true;
int spk=GET_BATTERIES; 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; if (game.prop[BATTER] == 1)spk=MISSING_BATTERIES;
RSPEAK(spk); RSPEAK(spk);
} }

8
misc.c
View file

@ -367,7 +367,7 @@ void MOVE(long object, long where)
from=game.fixed[object-NOBJECTS]; from=game.fixed[object-NOBJECTS];
else else
from=game.place[object]; from=game.place[object];
if (from > 0 && !SPECIAL(from)) if (from > NOWHERE && !SPECIAL(from))
CARRY(object,from); CARRY(object,from);
DROP(object,where); DROP(object,where);
} }
@ -388,9 +388,9 @@ void CARRY(long object, long where)
long temp; long temp;
if (object <= NOBJECTS) { if (object <= NOBJECTS) {
if (game.place[object] == -1) if (game.place[object] == CARRIED)
return; return;
game.place[object]= -1; game.place[object] = CARRIED;
++game.holdng; ++game.holdng;
} }
if (game.atloc[where] == object) { if (game.atloc[where] == object) {
@ -412,7 +412,7 @@ void DROP(long object, long where)
game.fixed[object-NOBJECTS] = where; game.fixed[object-NOBJECTS] = where;
else else
{ {
if (game.place[object] == -1) if (game.place[object] == CARRIED)
--game.holdng; --game.holdng;
game.place[object] = where; game.place[object] = where;
} }

View file

@ -41,7 +41,7 @@ void score(enum termination mode)
if(i > CHEST)k=16; if(i > CHEST)k=16;
if(game.prop[i] >= 0) if(game.prop[i] >= 0)
score += 2; score += 2;
if(game.place[i] == 3 && game.prop[i] == 0) if(game.place[i] == LOC_BUILDING && game.prop[i] == 0)
score += k-2; score += k-2;
mxscor += k; mxscor += k;
} }
@ -75,7 +75,7 @@ void score(enum termination mode)
mxscor += 45; mxscor += 45;
/* Did he come to Witt's End as he should? */ /* Did he come to Witt's End as he should? */
if(game.place[MAGZIN] == 108) if(game.place[MAGZIN] == LOC_WITTSEND)
score += 1; score += 1;
mxscor += 1; mxscor += 1;