Magic-number elimination.

This commit is contained in:
Eric S. Raymond 2017-07-04 14:15:20 -04:00
parent a8ac2f477a
commit f37a413524
5 changed files with 19 additions and 13 deletions

View file

@ -7,6 +7,7 @@
static int fill(token_t, token_t); static int fill(token_t, token_t);
static void state_change(long obj, long state) static void state_change(long obj, long state)
/* Object must have a change-message list for this to be useful; only some do */
{ {
game.prop[obj] = state; game.prop[obj] = state;
pspeak(obj, change, state, true); pspeak(obj, change, state, true);
@ -377,17 +378,16 @@ static int vcarry(token_t verb, token_t obj)
} }
game.prop[BIRD] = BIRD_CAGED; game.prop[BIRD] = BIRD_CAGED;
} }
/* FIXME: Arithmetic on state numbers */
if ((obj == BIRD || if ((obj == BIRD ||
obj == CAGE) && obj == CAGE) &&
(game.prop[BIRD] == BIRD_CAGED || STASHED(BIRD) == BIRD_CAGED)) (game.prop[BIRD] == BIRD_CAGED || STASHED(BIRD) == BIRD_CAGED))
/* expression maps BIRD to CAGE and CAGE to BIRD */
carry(BIRD + CAGE - obj, game.loc); carry(BIRD + CAGE - obj, game.loc);
carry(obj, game.loc); carry(obj, game.loc);
if (obj == BOTTLE && LIQUID() != NO_OBJECT) if (obj == BOTTLE && LIQUID() != NO_OBJECT)
game.place[LIQUID()] = CARRIED; game.place[LIQUID()] = CARRIED;
if (GSTONE(obj) && game.prop[obj] != 0) { if (GSTONE(obj) && game.prop[obj] != STATE_GROUND) {
game.prop[obj] game.prop[obj] = STATE_GROUND;
= STATE_GROUND;
game.prop[CAVITY] = CAVITY_EMPTY; game.prop[CAVITY] = CAVITY_EMPTY;
} }
rspeak(OK_MAN); rspeak(OK_MAN);
@ -463,7 +463,7 @@ static int discard(token_t verb, token_t obj, bool just_do_it)
} else if ((GSTONE(obj) && AT(CAVITY) && game.prop[CAVITY] != CAVITY_FULL)) { } else if ((GSTONE(obj) && AT(CAVITY) && game.prop[CAVITY] != CAVITY_FULL)) {
rspeak(GEM_FITS); rspeak(GEM_FITS);
game.prop[obj] = 1; game.prop[obj] = STATE_IN_CAVITY;
game.prop[CAVITY] = CAVITY_FULL; game.prop[CAVITY] = CAVITY_FULL;
if (HERE(RUG) && ((obj == EMERALD && game.prop[RUG] != RUG_HOVER) || if (HERE(RUG) && ((obj == EMERALD && game.prop[RUG] != RUG_HOVER) ||
(obj == RUBY && game.prop[RUG] == RUG_HOVER))) { (obj == RUBY && game.prop[RUG] == RUG_HOVER))) {

View file

@ -28,6 +28,7 @@
/* Special object-state values - integers > 0 are object-specific */ /* Special object-state values - integers > 0 are object-specific */
#define STATE_NOTFOUND -1 // 'Not found" state of treasures */ #define STATE_NOTFOUND -1 // 'Not found" state of treasures */
#define STATE_GROUND 0 // After discovered, before messed with #define STATE_GROUND 0 // After discovered, before messed with
#define STATE_IN_CAVITY 1 // State value common to all gemstones
/* Map a state property value to a negative range, where the object cannot be /* Map a state property value to a negative range, where the object cannot be
* picked up but the value can be recovered later. Avoid colliding with -1, * picked up but the value can be recovered later. Avoid colliding with -1,
@ -190,7 +191,7 @@ struct command_t {
token_t wd2; token_t wd2;
long id1; long id1;
long id2; long id2;
char raw1[BUFSIZ], raw2[BUFSIZ]; char raw1[LINESIZE], raw2[LINESIZE];
}; };
extern struct game_t game; extern struct game_t game;

View file

@ -3251,6 +3251,7 @@ objects: !!omap
inventory: '*steps' inventory: '*steps'
locations: [LOC_PITTOP, LOC_MISTHALL] locations: [LOC_PITTOP, LOC_MISTHALL]
immovable: true immovable: true
states: [STEPS_DOWN, STEPS_UP]
descriptions: descriptions:
- 'Rough stone steps lead down the pit.' - 'Rough stone steps lead down the pit.'
- 'Rough stone steps lead up the dome.' - 'Rough stone steps lead up the dome.'

14
main.c
View file

@ -765,6 +765,7 @@ static bool closecheck(void)
* problems arise from the use of negative prop numbers to suppress * problems arise from the use of negative prop numbers to suppress
* the object descriptions until he's actually moved the objects. */ * the object descriptions until he's actually moved the objects. */
{ {
/* Don't tick game.clock1 unless well into cave (and not at Y2). */
if (game.tally == 0 && INDEEP(game.loc) && game.loc != LOC_Y2) if (game.tally == 0 && INDEEP(game.loc) && game.loc != LOC_Y2)
--game.clock1; --game.clock1;
@ -936,8 +937,10 @@ static void listobjects(void)
* (so goes the rationalisation). */ * (so goes the rationalisation). */
} }
int kk = game.prop[obj]; int kk = game.prop[obj];
if (obj == STEPS && game.loc == game.fixed[STEPS]) if (obj == STEPS)
kk = 1; kk = (game.loc == game.fixed[STEPS])
? STEPS_UP
: STEPS_DOWN;
pspeak(obj, look, kk, true); pspeak(obj, look, kk, true);
} }
} }
@ -1019,10 +1022,9 @@ L2600:
checkhints(); checkhints();
/* If closing time, check for any objects being toted with /* If closing time, check for any objects being toted with
* game.prop < 0 and set the prop to -1-game.prop. This way * game.prop < 0 and stash them. This way objects won't be
* objects won't be described until they've been picked up * described until they've been picked up and put down
* and put down separate from their respective piles. Don't * separate from their respective piles. */
* tick game.clock1 unless well into cave (and not at Y2). */
if (game.closed) { if (game.closed) {
if (game.prop[OYSTER] < 0 && TOTING(OYSTER)) if (game.prop[OYSTER] < 0 && TOTING(OYSTER))
pspeak(OYSTER, look, 1, true); pspeak(OYSTER, look, 1, true);

4
misc.c
View file

@ -94,7 +94,9 @@ void tokenize(char* raw, struct command_t *cmd)
{ {
memset(cmd, '\0', sizeof(struct command_t)); memset(cmd, '\0', sizeof(struct command_t));
/* FIXME: put a bound prefix on the %s to prevent buffer overflow */ /* Bound prefix on the %s would be needed to prevent buffer
* overflow. but we shortstop this more simply by making each
* raw-input buffer as long as the enrire inout buffer. */
sscanf(raw, "%s%s", cmd->raw1, cmd->raw2); sscanf(raw, "%s%s", cmd->raw1, cmd->raw2);
// pack the substrings // pack the substrings