From: Eric S. Raymond Date: Mon, 10 Apr 2023 14:28:18 +0000 (-0400) Subject: Magic-number elimination. X-Git-Tag: 1.16~7 X-Git-Url: https://jxself.org/git/?p=open-adventure.git;a=commitdiff_plain;h=4d4e8dce96cc9401151181a259c5160291031947 Magic-number elimination. --- diff --git a/advent.h b/advent.h index 42491e7..cb2b71b 100644 --- a/advent.h +++ b/advent.h @@ -61,6 +61,7 @@ #define PROP_SET_FOUND(obj) (game.objects[obj].prop = STATE_FOUND) #define PROP_SET_NOT_FOUND(obj) (game.objects[obj].prop = STATE_NOTFOUND) #define PROP_IS_NOTFOUND2(g, o) (g.objects[o].prop == STATE_NOTFOUND) +#define PROP_IS_INVALID(val) (val < -MAX_STATE - 1 || val > MAX_STATE) #else #define PROP_STASHED(obj) (-game.objects[obj].prop) #define PROP_IS_STASHED(obj) (game.objects[obj].prop < 0) @@ -70,9 +71,8 @@ #define PROP_SET_FOUND(obj) do {game.objects[obj].found = true; game.objects[obj].prop = STATE_FOUND;} while(0) #define PROP_SET_NOT_FOUND(obj) game.objects[obj].found = false #define PROP_IS_NOTFOUND2(g, o) (!g.objects[o].found) +#define PROP_IS_INVALID(val) (val < -MAX_STATE || val > MAX_STATE) #endif -/* Magic number -2 allows a PROP_STASHED version of state 1 */ -#define PROP_IS_INVALID(val) (val < -2 || val > 1) #define PROMPT "> " diff --git a/make_dungeon.py b/make_dungeon.py index a9193b6..bae97f2 100755 --- a/make_dungeon.py +++ b/make_dungeon.py @@ -136,6 +136,7 @@ def get_objects(obj): }}, }}, """ + max_state = 0 obj_str = "" for (i, item) in enumerate(obj): attr = item[1] @@ -159,6 +160,7 @@ def get_objects(obj): statedefines += "/* States for %s */\n" % item[0] for (n, label) in enumerate(labels): statedefines += "#define %s\t%d\n" % (label, n) + max_state = max(max_state, n) statedefines += "\n" sounds_str = "" if attr.get("sounds") is None: @@ -192,6 +194,7 @@ def get_objects(obj): treasure = "true" if attr.get("treasure") else "false" obj_str += template.format(i, item[0], words_str, i_msg, locs[0], locs[1], treasure, descriptions_str, sounds_str, texts_str, changes_str) obj_str = obj_str[:-1] # trim trailing newline + statedefines += "/* Maximum state value */\n#define MAX_STATE %d\n" % max_state return obj_str def get_obituaries(obit): diff --git a/saveresume.c b/saveresume.c index 9c788d1..70b9461 100644 --- a/saveresume.c +++ b/saveresume.c @@ -230,28 +230,7 @@ bool is_valid(struct game_t valgame) /* Check that properties of objects aren't beyond expected */ for (obj_t obj = 0; obj <= NOBJECTS; obj++) { if (PROP_IS_INVALID(valgame.objects[obj].prop)) { - switch (obj) { - case RUG: - case DRAGON: - case BIRD: - case BOTTLE: - case PLANT: - case PLANT2: - case TROLL: - case URN: - case EGGS: - case VASE: - case CHAIN: - if (valgame.objects[obj].prop == 2) // There are multiple different states, but it's convenient to clump them together - continue; // LCOV_EXCL_LINE - /* FALLTHRU */ - case BEAR: - if (valgame.objects[BEAR].prop == CONTENTED_BEAR || valgame.objects[BEAR].prop == BEAR_DEAD) - continue; - /* FALLTHRU */ - default: - return false; // LCOV_EXCL_LINE - } + return false; // LCOV_EXCL_LINE } }