From: Eric S. Raymond Date: Tue, 4 Jul 2017 18:15:20 +0000 (-0400) Subject: Magic-number elimination. X-Git-Tag: takebird~29 X-Git-Url: https://jxself.org/git/?p=open-adventure.git;a=commitdiff_plain;h=f37a4135246fef3f10112bf16fda181c31178588 Magic-number elimination. --- diff --git a/actions.c b/actions.c index ec17c07..73f8f2e 100644 --- a/actions.c +++ b/actions.c @@ -7,6 +7,7 @@ static int fill(token_t, token_t); 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; pspeak(obj, change, state, true); @@ -377,17 +378,16 @@ static int vcarry(token_t verb, token_t obj) } game.prop[BIRD] = BIRD_CAGED; } - /* FIXME: Arithmetic on state numbers */ if ((obj == BIRD || obj == CAGE) && (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(obj, game.loc); if (obj == BOTTLE && LIQUID() != NO_OBJECT) game.place[LIQUID()] = CARRIED; - if (GSTONE(obj) && game.prop[obj] != 0) { - game.prop[obj] - = STATE_GROUND; + if (GSTONE(obj) && game.prop[obj] != STATE_GROUND) { + game.prop[obj] = STATE_GROUND; game.prop[CAVITY] = CAVITY_EMPTY; } 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)) { rspeak(GEM_FITS); - game.prop[obj] = 1; + game.prop[obj] = STATE_IN_CAVITY; game.prop[CAVITY] = CAVITY_FULL; if (HERE(RUG) && ((obj == EMERALD && game.prop[RUG] != RUG_HOVER) || (obj == RUBY && game.prop[RUG] == RUG_HOVER))) { diff --git a/advent.h b/advent.h index 3c33725..aaae641 100644 --- a/advent.h +++ b/advent.h @@ -28,6 +28,7 @@ /* Special object-state values - integers > 0 are object-specific */ #define STATE_NOTFOUND -1 // 'Not found" state of treasures */ #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 * picked up but the value can be recovered later. Avoid colliding with -1, @@ -190,7 +191,7 @@ struct command_t { token_t wd2; long id1; long id2; - char raw1[BUFSIZ], raw2[BUFSIZ]; + char raw1[LINESIZE], raw2[LINESIZE]; }; extern struct game_t game; diff --git a/adventure.yaml b/adventure.yaml index d9876b7..17d88a9 100644 --- a/adventure.yaml +++ b/adventure.yaml @@ -3251,6 +3251,7 @@ objects: !!omap inventory: '*steps' locations: [LOC_PITTOP, LOC_MISTHALL] immovable: true + states: [STEPS_DOWN, STEPS_UP] descriptions: - 'Rough stone steps lead down the pit.' - 'Rough stone steps lead up the dome.' diff --git a/main.c b/main.c index 71a7dac..00b8e46 100644 --- a/main.c +++ b/main.c @@ -765,6 +765,7 @@ static bool closecheck(void) * problems arise from the use of negative prop numbers to suppress * 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) --game.clock1; @@ -936,8 +937,10 @@ static void listobjects(void) * (so goes the rationalisation). */ } int kk = game.prop[obj]; - if (obj == STEPS && game.loc == game.fixed[STEPS]) - kk = 1; + if (obj == STEPS) + kk = (game.loc == game.fixed[STEPS]) + ? STEPS_UP + : STEPS_DOWN; pspeak(obj, look, kk, true); } } @@ -1019,10 +1022,9 @@ L2600: checkhints(); /* If closing time, check for any objects being toted with - * game.prop < 0 and set the prop to -1-game.prop. This way - * objects won't be described until they've been picked up - * and put down separate from their respective piles. Don't - * tick game.clock1 unless well into cave (and not at Y2). */ + * game.prop < 0 and stash them. This way objects won't be + * described until they've been picked up and put down + * separate from their respective piles. */ if (game.closed) { if (game.prop[OYSTER] < 0 && TOTING(OYSTER)) pspeak(OYSTER, look, 1, true); diff --git a/misc.c b/misc.c index 5d400db..36deef4 100644 --- a/misc.c +++ b/misc.c @@ -94,7 +94,9 @@ void tokenize(char* raw, struct command_t *cmd) { 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); // pack the substrings