From cb9fde0cde82d4122c8bd9836309720294dee77f Mon Sep 17 00:00:00 2001 From: Bardur Arantsson Date: Sat, 17 Sep 2016 09:58:13 +0200 Subject: Rework store_info_type::owners to std::vector<> --- src/init1.cc | 20 +++++++++++++++----- src/store.cc | 39 ++++++++++++++++++--------------------- src/store_info_type.hpp | 2 +- 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/init1.cc b/src/init1.cc index 6fef9561..3dec9b6e 100644 --- a/src/init1.cc +++ b/src/init1.cc @@ -5900,13 +5900,23 @@ errr init_st_info_txt(FILE *fp) /* Scan for the values */ if (4 != sscanf(buf + 2, "%d:%d:%d:%d", - &a1, &a2, &a3, &a4)) return (1); + &a1, &a2, &a3, &a4)) + { + return 1; + } + + /* Get a reference to the owners */ + auto owners = &st_ptr->owners; /* Save the values */ - st_ptr->owners[0] = a1; - st_ptr->owners[1] = a2; - st_ptr->owners[2] = a3; - st_ptr->owners[3] = a4; + owners->push_back(a1); + owners->push_back(a2); + owners->push_back(a3); + owners->push_back(a4); + + /* Sort and remove duplicates */ + std::sort(owners->begin(), owners->end()); + owners->erase(std::unique(owners->begin(), owners->end()), owners->end()); /* Next... */ continue; diff --git a/src/store.cc b/src/store.cc index 68d3900e..a7f40a6e 100644 --- a/src/store.cc +++ b/src/store.cc @@ -1834,24 +1834,21 @@ static bool_ sell_haggle(object_type *o_ptr, s32b *price) /* * Will the owner retire? */ -static bool_ retire_owner_p(void) +static bool retire_owner_p(void) { store_info_type *sti_ptr = &st_info[town_info[p_ptr->town_num].store[cur_store_num].st_idx]; - if ((sti_ptr->owners[0] == sti_ptr->owners[1]) && - (sti_ptr->owners[0] == sti_ptr->owners[2]) && - (sti_ptr->owners[0] == sti_ptr->owners[3])) + if (sti_ptr->owners.size() > 1) { - /* there is no other owner */ - return FALSE; + return false; // No other possible owner } if (rand_int(STORE_SHUFFLE) != 0) { - return FALSE; + return false; } - return TRUE; + return true; } /* @@ -3399,7 +3396,7 @@ void store_shuffle(int which) /* Pick a new owner */ for (auto j = st_ptr->owner; j == st_ptr->owner; ) { - st_ptr->owner = st_info[st_ptr->st_idx].owners[rand_int(4)]; + st_ptr->owner = *uniform_element(st_info[st_ptr->st_idx].owners); } /* Activate the new owner */ @@ -3522,28 +3519,28 @@ void store_init(int town_num, int store_num) { cur_store_num = store_num; - /* Activate that store */ + // Activate store st_ptr = &town_info[town_num].store[store_num]; + // Pick an owner. We use 0 for st_info[] which haven't been + // initialized, i.e. where there's no entry in st_info.txt. + st_ptr->owner = st_info[st_ptr->st_idx].owners.empty() + ? 0 + : *uniform_element(st_info[st_ptr->st_idx].owners) + ; - /* Pick an owner */ - st_ptr->owner = st_info[st_ptr->st_idx].owners[rand_int(4)]; - - /* Activate the new owner */ + // Activate the new owner ot_ptr = &ow_info[st_ptr->owner]; - - /* Initialize the store */ + // Initialize the store st_ptr->store_open = 0; - /* Nothing in stock */ + // Nothing in stock st_ptr->stock.reserve(st_ptr->stock_size); st_ptr->stock.clear(); - /* - * MEGA-HACK - Last visit to store is - * BEFORE player birth to enable store restocking - */ + // MEGA-HACK - Last visit to store is BEFORE player + // birth to enable store restocking. st_ptr->last_visit = -100L * STORE_TURNS; } diff --git a/src/store_info_type.hpp b/src/store_info_type.hpp index 9d0ba55f..fad32a93 100644 --- a/src/store_info_type.hpp +++ b/src/store_info_type.hpp @@ -17,7 +17,7 @@ struct store_info_type s16b max_obj = 0; /* Number of items this store can hold */ - u16b owners[4] = { 0 }; /* List of owners(refers to ow_info) */ + std::vector owners; /* List of owners; refers to ow_info */ u16b actions[6] = { 0 }; /* Actions(refers to ba_info) */ -- cgit v1.2.3