From 347afc50368137901a989ac4a4fc9298056c7377 Mon Sep 17 00:00:00 2001 From: Bardur Arantsson Date: Sat, 17 Sep 2016 09:58:13 +0200 Subject: Unify store_info_type::item_* info std::vector --- src/init1.cc | 34 +++++++++++++--------------------- src/store.cc | 8 ++++---- src/store_info_type.hpp | 10 +++------- src/store_item.hpp | 18 ++++++++++++++++++ 4 files changed, 38 insertions(+), 32 deletions(-) create mode 100644 src/store_item.hpp (limited to 'src') diff --git a/src/init1.cc b/src/init1.cc index def2cdb1..6fef9561 100644 --- a/src/init1.cc +++ b/src/init1.cc @@ -5723,7 +5723,7 @@ static errr grab_one_store_flag(store_flag_set *flags, cptr what) */ errr init_st_info_txt(FILE *fp) { - int i = 0, item_idx = 0; + int i = 0; char buf[1024]; char *s; @@ -5783,9 +5783,6 @@ errr init_st_info_txt(FILE *fp) assert(!st_ptr->name); st_ptr->name = my_strdup(s); - /* We are ready for a new set of objects */ - item_idx = 0; - /* Next... */ continue; } @@ -5808,15 +5805,11 @@ errr init_st_info_txt(FILE *fp) /* Paranoia -- require a name */ if (!*s) return (1); - /* Get the index */ - st_ptr->item_chance[item_idx] = atoi(buf + 2); - - /* Append chars to the name */ - st_ptr->item_kind[item_idx] = test_item_name(s); - - item_idx++; - st_ptr->item_num = item_idx; - assert(st_ptr->item_num <= STORE_CHOICES); + /* Add to items array */ + store_item item; + item.chance = atoi(buf + 2); + item.kind = test_item_name(s); + st_ptr->items.emplace_back(item); /* Next... */ continue; @@ -5831,14 +5824,13 @@ errr init_st_info_txt(FILE *fp) if (3 != sscanf(buf + 2, "%d:%d:%d", &rar1, &tv1, &sv1)) return (1); - /* Get the index */ - st_ptr->item_chance[item_idx] = rar1; - /* Hack -- 256 as a sval means all possible items */ - st_ptr->item_kind[item_idx] = (sv1 < 256) ? lookup_kind(tv1, sv1) : tv1 + 10000; - - item_idx++; - st_ptr->item_num = item_idx; - assert(st_ptr->item_num <= STORE_CHOICES); + /* Add to the items array */ + store_item item; + item.chance = rar1; + item.kind = (sv1 < 256) + ? lookup_kind(tv1, sv1) + : tv1 + 10000; /* An SVAL of 256 means all possible items. */ + st_ptr->items.emplace_back(item); /* Next... */ continue; diff --git a/src/store.cc b/src/store.cc index 63f9f0d1..68d3900e 100644 --- a/src/store.cc +++ b/src/store.cc @@ -1197,7 +1197,7 @@ static bool_ kind_is_storeok(int k_idx) */ static void store_create(void) { - int i = 0, tries, level = 0, chance, item; + int i = 0, tries, level = 0; object_type forge; object_type *q_ptr = NULL; @@ -1281,9 +1281,9 @@ static void store_create(void) else { /* Hack -- Pick an item to sell */ - item = rand_int(st_info[st_ptr->st_idx].item_num); - i = st_info[st_ptr->st_idx].item_kind[item]; - chance = st_info[st_ptr->st_idx].item_chance[item]; + auto const &item = st_info[st_ptr->st_idx].items[rand_int(st_info[st_ptr->st_idx].items.size())]; + i = item.kind; + auto chance = item.chance; /* Don't allow k_info artifacts */ if ((i <= 10000) && (k_info[i].flags & TR_NORM_ART)) diff --git a/src/store_info_type.hpp b/src/store_info_type.hpp index a735fdfc..9d0ba55f 100644 --- a/src/store_info_type.hpp +++ b/src/store_info_type.hpp @@ -2,11 +2,9 @@ #include "h-basic.h" #include "store_flag_set.hpp" +#include "store_item.hpp" -/** - * Number of items to choose stock from - */ -constexpr int STORE_CHOICES = 56; +#include /** * Store descriptor. @@ -15,9 +13,7 @@ struct store_info_type { const char *name = nullptr; /* Name */ - s16b item_kind[STORE_CHOICES] = { 0 }; /* Table -- Legal item kinds */ - s16b item_chance[STORE_CHOICES] = { 0 }; - byte item_num = 0; /* Number of items */ + std::vector items; /* Table -- Legal item kinds */ s16b max_obj = 0; /* Number of items this store can hold */ diff --git a/src/store_item.hpp b/src/store_item.hpp new file mode 100644 index 00000000..62d3f2a9 --- /dev/null +++ b/src/store_item.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include "h-basic.h" + +struct store_item +{ + /** + * Legal item kinds; if > 10000, designates (TVAL - 10000) and any SVAL. + * Otherwise designates an entry in k_info.txt. + */ + s16b kind = 0; + + /** + * Percentage chance of generation if the entry is chosen. + */ + s16b chance = 0; + +}; -- cgit v1.2.3