From e17e742edb3809d45ce1edc716d71b3bb93056d6 Mon Sep 17 00:00:00 2001 From: Bardur Arantsson Date: Mon, 20 Jun 2016 22:49:05 +0200 Subject: Make obj_theme a non-POD struct --- src/defines.h | 6 -- src/dungeon_info_type.hpp | 2 +- src/init1.cc | 18 +++--- src/monster2.cc | 10 +-- src/monster_race.hpp | 3 +- src/obj_theme.hpp | 45 +++++++++++-- src/object2.cc | 156 ++++++++++++++++++++++------------------------ src/object2.hpp | 2 +- src/q_rand.cc | 9 +-- src/store.cc | 16 +---- src/wizard2.cc | 10 +-- 11 files changed, 132 insertions(+), 145 deletions(-) (limited to 'src') diff --git a/src/defines.h b/src/defines.h index fcbb5342..a809f8d3 100644 --- a/src/defines.h +++ b/src/defines.h @@ -3265,12 +3265,6 @@ #define MEGO_CHANCE 18 /* % chances of getting ego monsters */ -/* Object generation */ -#define OBJ_GENE_TREASURE 20 -#define OBJ_GENE_COMBAT 20 -#define OBJ_GENE_MAGIC 20 -#define OBJ_GENE_TOOL 20 - /* * Used (or should be) by various functions and tables needing access to * single bits diff --git a/src/dungeon_info_type.hpp b/src/dungeon_info_type.hpp index c4edc770..b15cf9fe 100644 --- a/src/dungeon_info_type.hpp +++ b/src/dungeon_info_type.hpp @@ -63,7 +63,7 @@ struct dungeon_info_type int ox = 0; /* Wilderness coordinates of exit */ int oy = 0; /* Wilderness coordinates of exit */ - obj_theme objs = obj_theme { 0, 0, 0, 0 }; /* The drops type */ + obj_theme objs; /* The drops type */ int d_dice[4] = { 0 }; /* Number of dices */ int d_side[4] = { 0 }; /* Number of sides */ diff --git a/src/init1.cc b/src/init1.cc index 7ff179fb..cfd767f8 100644 --- a/src/init1.cc +++ b/src/init1.cc @@ -5575,12 +5575,11 @@ errr init_r_info_txt(FILE *fp) /* Ensure empty description */ r_ptr->text = my_strdup(""); - /* HACK -- Those ones HAVE to have a set default value */ - r_ptr->drops.treasure = OBJ_GENE_TREASURE; - r_ptr->drops.combat = OBJ_GENE_COMBAT; - r_ptr->drops.magic = OBJ_GENE_MAGIC; - r_ptr->drops.tools = OBJ_GENE_TOOL; - r_ptr->freq_inate = r_ptr->freq_spell = 0; + /* Set default drop theme */ + r_ptr->drops = obj_theme::defaults(); + + r_ptr->freq_inate = 0; + r_ptr->freq_spell = 0; /* Next... */ continue; @@ -6662,11 +6661,8 @@ errr init_d_info_txt(FILE *fp) for (k = 0; k < 5; k++) d_ptr->rules[j].r_char[k] = 0; } - /* HACK -- Those ones HAVE to have a set default value */ - d_ptr->objs.treasure = OBJ_GENE_TREASURE; - d_ptr->objs.combat = OBJ_GENE_COMBAT; - d_ptr->objs.magic = OBJ_GENE_MAGIC; - d_ptr->objs.tools = OBJ_GENE_TOOL; + /* Set default drop theme */ + d_ptr->objs = obj_theme::defaults(); /* The default generator */ strcpy(d_ptr->generator, "dungeon"); diff --git a/src/monster2.cc b/src/monster2.cc index 1b942a04..1adc9e7b 100644 --- a/src/monster2.cc +++ b/src/monster2.cc @@ -2313,17 +2313,11 @@ s16b place_monster_one(int y, int x, int r_idx, int ego, bool_ slp, int status) if (r_ptr->flags7 & RF7_DROP_RANDART) { int tries = 1000; - obj_theme theme; - /* Get local object */ q_ptr = &forge; - theme.treasure = 101; - theme.combat = 101; - theme.magic = 101; - theme.tools = 101; - - init_match_theme(theme); + /* No theme */ + init_match_theme(obj_theme::no_theme()); /* Apply restriction */ get_obj_num_hook = kind_is_legal; diff --git a/src/monster_race.hpp b/src/monster_race.hpp index 9fa9f590..94896c64 100644 --- a/src/monster_race.hpp +++ b/src/monster_race.hpp @@ -83,8 +83,7 @@ struct monster_race byte total_visible = 0; /* Amount of this race that are visible */ - obj_theme drops = obj_theme /* The drops type */ - { 0, 0, 0, 0 }; + obj_theme drops; /* The drops type */ }; diff --git a/src/obj_theme.hpp b/src/obj_theme.hpp index 13f185e8..d10d17fa 100644 --- a/src/obj_theme.hpp +++ b/src/obj_theme.hpp @@ -8,8 +8,45 @@ */ struct obj_theme { - byte treasure; - byte combat; - byte magic; - byte tools; + byte treasure = 0; + byte combat = 0; + byte magic = 0; + byte tools = 0; + + bool operator == (obj_theme const &other) const + { + return + (treasure == other.treasure) && + (combat == other.combat) && + (magic == other.magic) && + (tools == other.tools); + } + + bool operator != (obj_theme const &other) const + { + return !(*this == other); + } + + static constexpr obj_theme no_theme() + { + return equal_spread(100); + } + + static constexpr obj_theme defaults() + { + return equal_spread(20); + } + +private: + + static constexpr obj_theme equal_spread(byte v) + { + obj_theme ot; + ot.treasure = v; + ot.combat = v; + ot.magic = v; + ot.tools = v; + return ot; + } + }; diff --git a/src/object2.cc b/src/object2.cc index 54ed2636..ebf185b2 100644 --- a/src/object2.cc +++ b/src/object2.cc @@ -4296,52 +4296,51 @@ try_an_other_ego: /* The themed objects to use */ -static obj_theme match_theme; +static obj_theme *match_theme = nullptr; /* * XXX XXX XXX It relies on the fact that obj_theme is a four byte structure * for its efficient operation. A horrendous hack, I'd say. */ -void init_match_theme(obj_theme const &theme) +bool init_match_theme(obj_theme const &theme) { - /* Save the theme */ - match_theme = theme; -} - -/* - * Ditto XXX XXX XXX - */ -static bool_ theme_changed(obj_theme theme) -{ - /* Any of the themes has been changed */ - if (theme.treasure != match_theme.treasure) return (TRUE); - if (theme.combat != match_theme.combat) return (TRUE); - if (theme.magic != match_theme.magic) return (TRUE); - if (theme.tools != match_theme.tools) return (TRUE); - - /* No changes */ - return (FALSE); + if (match_theme == nullptr) + { + match_theme = new obj_theme(theme); + return true; + } + else if (*match_theme != theme) + { + *match_theme = theme; + return true; + } + else + { + return false; + } } - /* * Maga-Hack -- match certain types of object only. */ -static bool kind_is_theme(int k_idx) +static bool kind_is_theme(obj_theme const *theme, int k_idx) { + assert(theme != nullptr); + object_kind *k_ptr = &k_info[k_idx]; s32b prob = 0; - /* * Paranoia -- Prevent accidental "(Nothing)" * that are results of uninitialised theme structs. * * Caution: Junks go into the allocation table. */ - if (match_theme.treasure + match_theme.combat + - match_theme.magic + match_theme.tools == 0) return (TRUE); + if (theme->treasure + theme->combat + theme->magic + theme->tools == 0) + { + return TRUE; + } /* Pick probability to use */ @@ -4360,150 +4359,150 @@ static bool kind_is_theme(int k_idx) * larger than theme components, or we would see * unexpected, well, junks. */ - prob = 100 - (match_theme.treasure + match_theme.combat + - match_theme.magic + match_theme.tools); + prob = 100 - (theme->treasure + theme->combat + + theme->magic + theme->tools); break; } case TV_CHEST: - prob = match_theme.treasure; + prob = theme->treasure; break; case TV_CROWN: - prob = match_theme.treasure; + prob = theme->treasure; break; case TV_DRAG_ARMOR: - prob = match_theme.treasure; + prob = theme->treasure; break; case TV_AMULET: - prob = match_theme.treasure; + prob = theme->treasure; break; case TV_RING: - prob = match_theme.treasure; + prob = theme->treasure; break; case TV_SHOT: - prob = match_theme.combat; + prob = theme->combat; break; case TV_ARROW: - prob = match_theme.combat; + prob = theme->combat; break; case TV_BOLT: - prob = match_theme.combat; + prob = theme->combat; break; case TV_BOOMERANG: - prob = match_theme.combat; + prob = theme->combat; break; case TV_BOW: - prob = match_theme.combat; + prob = theme->combat; break; case TV_HAFTED: - prob = match_theme.combat; + prob = theme->combat; break; case TV_POLEARM: - prob = match_theme.combat; + prob = theme->combat; break; case TV_SWORD: - prob = match_theme.combat; + prob = theme->combat; break; case TV_AXE: - prob = match_theme.combat; + prob = theme->combat; break; case TV_GLOVES: - prob = match_theme.combat; + prob = theme->combat; break; case TV_HELM: - prob = match_theme.combat; + prob = theme->combat; break; case TV_SHIELD: - prob = match_theme.combat; + prob = theme->combat; break; case TV_SOFT_ARMOR: - prob = match_theme.combat; + prob = theme->combat; break; case TV_HARD_ARMOR: - prob = match_theme.combat; + prob = theme->combat; break; case TV_MSTAFF: - prob = match_theme.magic; + prob = theme->magic; break; case TV_STAFF: - prob = match_theme.magic; + prob = theme->magic; break; case TV_WAND: - prob = match_theme.magic; + prob = theme->magic; break; case TV_ROD: - prob = match_theme.magic; + prob = theme->magic; break; case TV_ROD_MAIN: - prob = match_theme.magic; + prob = theme->magic; break; case TV_SCROLL: - prob = match_theme.magic; + prob = theme->magic; break; case TV_PARCHMENT: - prob = match_theme.magic; + prob = theme->magic; break; case TV_POTION: - prob = match_theme.magic; + prob = theme->magic; break; case TV_POTION2: - prob = match_theme.magic; + prob = theme->magic; break; case TV_RANDART: - prob = match_theme.magic; + prob = theme->magic; break; case TV_RUNE1: - prob = match_theme.magic; + prob = theme->magic; break; case TV_RUNE2: - prob = match_theme.magic; + prob = theme->magic; break; case TV_BOOK: - prob = match_theme.magic; + prob = theme->magic; break; case TV_SYMBIOTIC_BOOK: - prob = match_theme.magic; + prob = theme->magic; break; case TV_MUSIC_BOOK: - prob = match_theme.magic; + prob = theme->magic; break; case TV_DRUID_BOOK: - prob = match_theme.magic; + prob = theme->magic; break; case TV_DAEMON_BOOK: - prob = match_theme.magic; + prob = theme->magic; break; case TV_LITE: - prob = match_theme.tools; + prob = theme->tools; break; case TV_CLOAK: - prob = match_theme.tools; + prob = theme->tools; break; case TV_BOOTS: - prob = match_theme.tools; + prob = theme->tools; break; case TV_SPIKE: - prob = match_theme.tools; + prob = theme->tools; break; case TV_DIGGING: - prob = match_theme.tools; + prob = theme->tools; break; case TV_FLASK: - prob = match_theme.tools; + prob = theme->tools; break; case TV_FOOD: - prob = match_theme.tools; + prob = theme->tools; break; case TV_TOOL: - prob = match_theme.tools; + prob = theme->tools; break; case TV_INSTRUMENT: - prob = match_theme.tools; + prob = theme->tools; break; case TV_TRAPKIT: - prob = match_theme.tools; + prob = theme->tools; break; } @@ -4521,7 +4520,7 @@ bool_ kind_is_legal(int k_idx) { object_kind *k_ptr = &k_info[k_idx]; - if (!kind_is_theme(k_idx)) return FALSE; + if (!kind_is_theme(match_theme, k_idx)) return FALSE; if (k_ptr->flags4 & TR4_SPECIAL_GENE) { @@ -4715,14 +4714,9 @@ bool_ make_object(object_type *j_ptr, bool_ good, bool_ great, obj_theme const & /* Generate a special object, or a normal object */ if ((rand_int(invprob) != 0) || !make_artifact_special(j_ptr)) { - int k_idx; - - /* See if the theme has been changed XXX XXX XXX */ - if (theme_changed(theme)) + /* See if the theme has been changed */ + if (init_match_theme(theme)) { - /* Select items based on "theme" */ - init_match_theme(theme); - /* Invalidate the cached allocation table */ alloc_kind_table_valid = FALSE; } @@ -4751,7 +4745,7 @@ bool_ make_object(object_type *j_ptr, bool_ good, bool_ great, obj_theme const & } /* Pick a random object */ - k_idx = get_obj_num(base); + int k_idx = get_obj_num(base); /* Good objects */ if (good) diff --git a/src/object2.hpp b/src/object2.hpp index 26d07b25..a8d0fab9 100644 --- a/src/object2.hpp +++ b/src/object2.hpp @@ -14,7 +14,7 @@ extern void inc_stack_size_ex(int item, int delta, optimize_flag opt, describe_f extern object_type *get_object(int item); extern s32b calc_total_weight(void); extern void add_random_ego_flag(object_type *o_ptr, int fego, bool_ *limit_blows); -extern void init_match_theme(obj_theme const &theme); +extern bool init_match_theme(obj_theme const &theme); extern bool_ kind_is_artifactable(int k_idx); extern bool_ kind_is_legal(int k_idx); extern void inven_item_charges(int item); diff --git a/src/q_rand.cc b/src/q_rand.cc index c5d2adac..bc3f2609 100644 --- a/src/q_rand.cc +++ b/src/q_rand.cc @@ -213,7 +213,6 @@ bool_ is_randhero(int level) static void do_get_new_obj(int y, int x) { - obj_theme theme; object_type *q_ptr[3], forge[3]; int res, i; @@ -227,14 +226,8 @@ static void do_get_new_obj(int y, int x) /* Wipe the object */ object_wipe(q_ptr[i]); - /* No themes */ - theme.treasure = 100; - theme.combat = 100; - theme.magic = 100; - theme.tools = 100; - /* Make a great object */ - make_object(q_ptr[i], TRUE, TRUE, theme); + make_object(q_ptr[i], TRUE, TRUE, obj_theme::no_theme()); q_ptr[i]->found = OBJ_FOUND_REWARD; char buf[100]; diff --git a/src/store.cc b/src/store.cc index 0fbe2e9b..d2947a7f 100644 --- a/src/store.cc +++ b/src/store.cc @@ -1286,14 +1286,8 @@ static void store_create(void) /* Black Market */ else if (st_info[st_ptr->st_idx].flags1 & SF1_ALL_ITEM) { - obj_theme theme; - /* No themes */ - theme.treasure = 100; - theme.combat = 100; - theme.magic = 100; - theme.tools = 100; - init_match_theme(theme); + init_match_theme(obj_theme::no_theme()); /* * Even in Black Markets, illegal objects can be @@ -1339,14 +1333,8 @@ static void store_create(void) /* Hack -- i > 10000 means it's a tval and all svals are allowed */ if (i > 10000) { - obj_theme theme; - /* No themes */ - theme.treasure = 100; - theme.combat = 100; - theme.magic = 100; - theme.tools = 100; - init_match_theme(theme); + init_match_theme(obj_theme::no_theme()); /* Activate restriction */ get_obj_num_hook = kind_is_storeok; diff --git a/src/wizard2.cc b/src/wizard2.cc index 837d778b..0614022c 100644 --- a/src/wizard2.cc +++ b/src/wizard2.cc @@ -866,16 +866,8 @@ static void wiz_statistics(object_type *o_ptr) object_type forge; object_type *q_ptr; - obj_theme theme; - cptr q = "Rolls: %ld, Matches: %ld, Better: %ld, Worse: %ld, Other: %ld"; - /* We can have everything */ - theme.treasure = OBJ_GENE_TREASURE; - theme.combat = OBJ_GENE_COMBAT; - theme.magic = OBJ_GENE_MAGIC; - theme.tools = OBJ_GENE_TOOL; - /* XXX XXX XXX Mega-Hack -- allow multiple artifacts */ if (artifact_p(o_ptr)) { @@ -963,7 +955,7 @@ static void wiz_statistics(object_type *o_ptr) object_wipe(q_ptr); /* Create an object */ - make_object(q_ptr, good, great, theme); + make_object(q_ptr, good, great, obj_theme::defaults()); /* XXX XXX XXX Mega-Hack -- allow multiple artifacts */ -- cgit v1.2.3