summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBardur Arantsson <bardur@scientician.net>2016-06-20 22:49:05 +0200
committerBardur Arantsson <bardur@scientician.net>2016-06-20 22:49:05 +0200
commita6c2ddeb9253fdcf5c0ac6153f77fa65f4a72e2b (patch)
tree529a84da5e5c30351203b9307de8144616941a6a
parent61a9e3c08009d143def75765169ed844944c370e (diff)
Change store_type to non-POD type
-rw-r--r--src/files.cc37
-rw-r--r--src/init2.cc10
-rw-r--r--src/loadsave.cc20
-rw-r--r--src/store.cc400
-rw-r--r--src/store_type.hpp21
5 files changed, 211 insertions, 277 deletions
diff --git a/src/files.cc b/src/files.cc
index 8758e692..a3dfe12a 100644
--- a/src/files.cc
+++ b/src/files.cc
@@ -2482,17 +2482,16 @@ void file_character_print_item(FILE *fff, char label, object_type *obj, bool_ fu
*/
void file_character_print_store(FILE *fff, wilderness_type_info *place, int store, bool_ full)
{
- int i;
town_type *town = &town_info[place->entrance];
store_type *st_ptr = &town->store[store];
- if (st_ptr->stock_num)
+ if (st_ptr->stock.size())
{
/* Header with name of the town */
fprintf(fff, " [%s Inventory - %s]\n\n", st_info[store].name, place->name);
/* Dump all available items */
- for (i = 0; i < st_ptr->stock_num; i++)
+ for (std::size_t i = 0; i < st_ptr->stock.size(); i++)
{
file_character_print_item(fff, I2A(i%24), &st_ptr->stock[i], full);
}
@@ -4211,14 +4210,10 @@ static void print_tomb(void)
*/
static void show_info(void)
{
- int i, j, k;
- object_type *o_ptr;
- store_type *st_ptr;
-
/* Hack -- Know everything in the inven/equip */
- for (i = 0; i < INVEN_TOTAL; i++)
+ for (int i = 0; i < INVEN_TOTAL; i++)
{
- o_ptr = &p_ptr->inventory[i];
+ auto o_ptr = &p_ptr->inventory[i];
/* Skip non-objects */
if (!o_ptr->k_idx) continue;
@@ -4228,14 +4223,13 @@ static void show_info(void)
object_known(o_ptr);
}
- for (i = 1; i < max_towns; i++)
+ /* Hack -- Know everything in the home */
+ for (int i = 1; i < max_towns; i++)
{
- st_ptr = &town_info[i].store[7];
-
- /* Hack -- Know everything in the home */
- for (j = 0; j < st_ptr->stock_num; j++)
+ auto st_ptr = &town_info[i].store[7];
+ for (auto &o_ref: st_ptr->stock)
{
- o_ptr = &st_ptr->stock[j];
+ auto o_ptr = &o_ref;
/* Skip non-objects */
if (!o_ptr->k_idx) continue;
@@ -4324,27 +4318,28 @@ static void show_info(void)
}
/* Homes in the different towns */
- for (k = 1; k < max_towns; k++)
+ for (int k = 1; k < max_towns; k++)
{
- st_ptr = &town_info[k].store[7];
+ store_type *st_ptr = &town_info[k].store[7];
/* Home -- if anything there */
- if (st_ptr->stock_num)
+ if (!st_ptr->stock.empty())
{
+ std::size_t i;
/* Display contents of the home */
- for (k = 0, i = 0; i < st_ptr->stock_num; k++)
+ for (k = 0, i = 0; i < st_ptr->stock.size(); k++)
{
/* Clear screen */
Term_clear();
/* Show 12 items */
- for (j = 0; (j < 12) && (i < st_ptr->stock_num); j++, i++)
+ for (int j = 0; (j < 12) && (i < st_ptr->stock.size()); j++, i++)
{
char o_name[80];
char tmp_val[80];
/* Acquire item */
- o_ptr = &st_ptr->stock[i];
+ auto o_ptr = &st_ptr->stock[i];
/* Print header, clear line */
sprintf(tmp_val, "%c) ", I2A(j));
diff --git a/src/init2.cc b/src/init2.cc
index 8bb9a25e..4201d214 100644
--- a/src/init2.cc
+++ b/src/init2.cc
@@ -26,6 +26,7 @@
#include "monster_race_flag.hpp"
#include "monster_type.hpp"
#include "object_kind.hpp"
+#include "object_type.hpp"
#include "owner_type.hpp"
#include "player_class.hpp"
#include "player_race.hpp"
@@ -688,7 +689,7 @@ static errr init_towns(void)
if (i <= max_real_towns) town_info[i].flags |= (TOWN_REAL);
/* Allocate the stores */
- town_info[i].store = make_array<store_type>(max_st_idx);
+ town_info[i].store = new store_type[max_st_idx];
/* Fill in each store */
for (j = 0; j < max_st_idx; j++)
@@ -708,20 +709,19 @@ static errr init_towns(void)
void create_stores_stock(int t)
{
- int j;
town_type *t_ptr = &town_info[t];
if (t_ptr->stocked) return;
- for (j = 0; j < max_st_idx; j++)
+ for (int j = 0; j < max_st_idx; j++)
{
store_type *st_ptr = &t_ptr->store[j];
/* Assume full stock */
st_ptr->stock_size = st_info[j].max_obj;
- /* Allocate the stock */
- st_ptr->stock = make_array<object_type>(st_ptr->stock_size);
+ /* Reserve space for stock */
+ st_ptr->stock.reserve(st_ptr->stock_size);
}
t_ptr->stocked = TRUE;
}
diff --git a/src/loadsave.cc b/src/loadsave.cc
index 32d401af..c42dd4b9 100644
--- a/src/loadsave.cc
+++ b/src/loadsave.cc
@@ -1688,7 +1688,10 @@ static void do_store(store_type *str, ls_flag_t flag)
/* Could be cleaner, done this way for benefit of the for loop later on */
byte num;
- if (flag == ls_flag_t::SAVE) num = str->stock_num;
+ if (flag == ls_flag_t::SAVE)
+ {
+ num = str->stock.size();
+ }
do_byte(&num, flag);
/* Last visit */
@@ -1698,7 +1701,6 @@ static void do_store(store_type *str, ls_flag_t flag)
for (int j = 0; j < num; j++)
{
if (flag == ls_flag_t::LOAD)
- /* Can't this be cleaner? */
{
object_type forge;
/* Wipe the object */
@@ -1706,15 +1708,17 @@ static void do_store(store_type *str, ls_flag_t flag)
/* Read the item */
do_item(&forge, ls_flag_t::LOAD);
/* Acquire valid items */
- if ((str->stock_num < store_inven_max) && (str->stock_num < str->stock_size))
+ if ((str->stock.size() < store_inven_max) && (str->stock.size() < str->stock_size))
{
- int k = str->stock_num++;
-
- /* Acquire the item */
- object_copy(&str->stock[k], &forge);
+ object_type stock_obj;
+ object_copy(&stock_obj, &forge);
+ str->stock.push_back(stock_obj);
}
}
- if (flag == ls_flag_t::SAVE) do_item(&str->stock[j], flag);
+ if (flag == ls_flag_t::SAVE)
+ {
+ do_item(&str->stock[j], flag);
+ }
}
}
diff --git a/src/store.cc b/src/store.cc
index f2f92cd0..041cf74c 100644
--- a/src/store.cc
+++ b/src/store.cc
@@ -583,24 +583,23 @@ static void store_object_absorb(object_type *o_ptr, object_type *j_ptr)
*/
static bool_ store_check_num(object_type *o_ptr)
{
- int i;
- object_type *j_ptr;
-
/* Free space is always usable */
- if (st_ptr->stock_num < st_ptr->stock_size) return TRUE;
+ if (st_ptr->stock.size() < st_ptr->stock_size)
+ {
+ return TRUE;
+ }
/* The "home" acts like the player */
- if ((cur_store_num == 7) ||
- (st_info[st_ptr->st_idx].flags1 & SF1_MUSEUM))
+ if ((cur_store_num == 7) || (st_info[st_ptr->st_idx].flags1 & SF1_MUSEUM))
{
/* Check all the items */
- for (i = 0; i < st_ptr->stock_num; i++)
+ for (auto const &o_ref: st_ptr->stock)
{
- /* Get the existing item */
- j_ptr = &st_ptr->stock[i];
-
/* Can the new object be combined with the old one? */
- if (object_similar(j_ptr, o_ptr)) return (TRUE);
+ if (object_similar(&o_ref, o_ptr))
+ {
+ return TRUE;
+ }
}
}
@@ -608,18 +607,18 @@ static bool_ store_check_num(object_type *o_ptr)
else
{
/* Check all the items */
- for (i = 0; i < st_ptr->stock_num; i++)
+ for (auto const &o_ref: st_ptr->stock)
{
- /* Get the existing item */
- j_ptr = &st_ptr->stock[i];
-
/* Can the new object be combined with the old one? */
- if (store_object_similar(j_ptr, o_ptr)) return (TRUE);
+ if (store_object_similar(&o_ref, o_ptr))
+ {
+ return TRUE;
+ }
}
}
/* But there was no room at the inn... */
- return (FALSE);
+ return FALSE;
}
@@ -868,17 +867,13 @@ static bool store_will_buy(object_type const *o_ptr)
*/
static int home_carry(object_type *o_ptr)
{
- int slot;
- s32b value, j_value;
- int i;
- object_type *j_ptr;
-
+ std::size_t slot;
/* Check each existing item (try to combine) */
- for (slot = 0; slot < st_ptr->stock_num; slot++)
+ for (slot = 0; slot < st_ptr->stock.size(); slot++)
{
/* Get the existing item */
- j_ptr = &st_ptr->stock[slot];
+ auto j_ptr = &st_ptr->stock[slot];
/* The home acts just like the player */
if (object_similar(j_ptr, o_ptr))
@@ -887,22 +882,24 @@ static int home_carry(object_type *o_ptr)
object_absorb(j_ptr, o_ptr);
/* All done */
- return (slot);
+ return slot;
}
}
/* No space? */
- if (st_ptr->stock_num >= st_ptr->stock_size) return ( -1);
-
+ if (st_ptr->stock.size() >= st_ptr->stock_size)
+ {
+ return -1;
+ }
/* Determine the "value" of the item */
- value = object_value(o_ptr);
+ auto const value = object_value(o_ptr);
/* Check existing slots to see if we must "slide" */
- for (slot = 0; slot < st_ptr->stock_num; slot++)
+ for (slot = 0; slot < st_ptr->stock.size(); slot++)
{
/* Get that item */
- j_ptr = &st_ptr->stock[slot];
+ auto j_ptr = &st_ptr->stock[slot];
/* Objects sort by decreasing type */
if (o_ptr->tval > j_ptr->tval) break;
@@ -932,25 +929,16 @@ static int home_carry(object_type *o_ptr)
}
/* Objects sort by decreasing value */
- j_value = object_value(j_ptr);
+ auto const j_value = object_value(j_ptr);
if (value > j_value) break;
if (value < j_value) continue;
}
- /* Slide the others up */
- for (i = st_ptr->stock_num; i > slot; i--)
- {
- st_ptr->stock[i] = st_ptr->stock[i - 1];
- }
-
- /* More stuff now */
- st_ptr->stock_num++;
-
- /* Insert the new item */
- st_ptr->stock[slot] = *o_ptr;
+ /* Insert */
+ st_ptr->stock.insert(st_ptr->stock.begin() + slot, *o_ptr);
/* Return the location */
- return (slot);
+ return slot;
}
@@ -968,16 +956,16 @@ static int home_carry(object_type *o_ptr)
*/
static int store_carry(object_type *o_ptr)
{
- int i, slot;
- s32b value, j_value;
- object_type *j_ptr;
-
+ std::size_t slot;
/* Evaluate the object */
- value = object_value(o_ptr);
+ auto const value = object_value(o_ptr);
/* Cursed/Worthless items "disappear" when sold */
- if (value <= 0) return ( -1);
+ if (value <= 0)
+ {
+ return -1;
+ }
/* All store items are fully *identified* */
o_ptr->ident |= IDENT_MENTAL;
@@ -986,10 +974,10 @@ static int store_carry(object_type *o_ptr)
o_ptr->note = 0;
/* Check each existing item (try to combine) */
- for (slot = 0; slot < st_ptr->stock_num; slot++)
+ for (slot = 0; slot < st_ptr->stock.size(); slot++)
{
/* Get the existing item */
- j_ptr = &st_ptr->stock[slot];
+ auto j_ptr = &st_ptr->stock[slot];
/* Can the existing items be incremented? */
if (store_object_similar(j_ptr, o_ptr))
@@ -998,19 +986,22 @@ static int store_carry(object_type *o_ptr)
store_object_absorb(j_ptr, o_ptr);
/* All done */
- return (slot);
+ return slot;
}
}
/* No space? */
- if (st_ptr->stock_num >= st_ptr->stock_size) return ( -1);
+ if (st_ptr->stock.size() >= st_ptr->stock_size)
+ {
+ return -1;
+ }
/* Check existing slots to see if we must "slide" */
- for (slot = 0; slot < st_ptr->stock_num; slot++)
+ for (slot = 0; slot < st_ptr->stock.size(); slot++)
{
/* Get that item */
- j_ptr = &st_ptr->stock[slot];
+ auto j_ptr = &st_ptr->stock[slot];
/* Objects sort by decreasing type */
if (o_ptr->tval > j_ptr->tval) break;
@@ -1032,27 +1023,18 @@ static int store_carry(object_type *o_ptr)
}
/* Evaluate that slot */
- j_value = object_value(j_ptr);
+ auto const j_value = object_value(j_ptr);
/* Objects sort by decreasing value */
if (value > j_value) break;
if (value < j_value) continue;
}
- /* Slide the others up */
- for (i = st_ptr->stock_num; i > slot; i--)
- {
- st_ptr->stock[i] = st_ptr->stock[i - 1];
- }
-
- /* More stuff now */
- st_ptr->stock_num++;
-
/* Insert the new item */
- st_ptr->stock[slot] = *o_ptr;
+ st_ptr->stock.insert(st_ptr->stock.begin() + slot, *o_ptr);
/* Return the location */
- return (slot);
+ return slot;
}
@@ -1062,14 +1044,11 @@ static int store_carry(object_type *o_ptr)
*/
static void store_item_increase(int item, int num)
{
- int cnt;
- object_type *o_ptr;
-
/* Get the item */
- o_ptr = &st_ptr->stock[item];
+ auto o_ptr = &st_ptr->stock[item];
/* Verify the number */
- cnt = o_ptr->number + num;
+ int cnt = o_ptr->number + num;
if (cnt > 255) cnt = 255;
else if (cnt < 0) cnt = 0;
num = cnt - o_ptr->number;
@@ -1084,11 +1063,8 @@ static void store_item_increase(int item, int num)
*/
static void store_item_optimize(int item)
{
- int j;
- object_type *o_ptr;
-
/* Get the item */
- o_ptr = &st_ptr->stock[item];
+ auto const o_ptr = &st_ptr->stock[item];
/* Must exist */
if (!o_ptr->k_idx) return;
@@ -1096,17 +1072,11 @@ static void store_item_optimize(int item)
/* Must have no items */
if (o_ptr->number) return;
- /* One less item */
- st_ptr->stock_num--;
-
- /* Slide everyone */
- for (j = item; j < st_ptr->stock_num; j++)
- {
- st_ptr->stock[j] = st_ptr->stock[j + 1];
- }
+ /* Wipe the item */
+ object_wipe(&st_ptr->stock[item]);
- /* Nuke the final slot */
- object_wipe(&st_ptr->stock[j]);
+ /* Erase the item */
+ st_ptr->stock.erase(st_ptr->stock.begin() + item);
}
@@ -1117,8 +1087,6 @@ static void store_item_optimize(int item)
*/
static bool_ black_market_crap(object_type *o_ptr)
{
- int i, j;
-
/* Ego items are never crap */
if (o_ptr->name2) return (FALSE);
@@ -1128,18 +1096,16 @@ static bool_ black_market_crap(object_type *o_ptr)
if (o_ptr->to_d > 0) return (FALSE);
/* Check all stores */
- for (i = 0; i < max_st_idx; i++)
+ for (std::size_t i = 0; i < max_st_idx; i++)
{
if (i == STORE_HOME) continue;
if (st_info[i].flags1 & SF1_MUSEUM) continue;
/* Check every item in the store */
- for (j = 0; j < town_info[p_ptr->town_num].store[i].stock_num; j++)
+ for (auto const &stock_obj: town_info[p_ptr->town_num].store[i].stock)
{
- object_type *j_ptr = &town_info[p_ptr->town_num].store[i].stock[j];
-
/* Duplicate item "type", assume crappy */
- if (o_ptr->k_idx == j_ptr->k_idx) return (TRUE);
+ if (o_ptr->k_idx == stock_obj.k_idx) return (TRUE);
}
}
@@ -1154,13 +1120,11 @@ static bool_ black_market_crap(object_type *o_ptr)
*/
static void store_delete(void)
{
- int what, num;
-
/* Pick a random slot */
- what = rand_int(st_ptr->stock_num);
+ int const what = rand_int(st_ptr->stock.size());
/* Determine how many items are here */
- num = st_ptr->stock[what].number;
+ int num = st_ptr->stock[what].number;
/* Hack -- sometimes, only destroy half the items */
if (rand_int(100) < 50) num = (num + 1) / 2;
@@ -1243,7 +1207,10 @@ static void store_create(void)
/* Paranoia -- no room left */
- if (st_ptr->stock_num >= st_ptr->stock_size) return;
+ if (st_ptr->stock.size() >= st_ptr->stock_size)
+ {
+ return;
+ }
/* Hack -- consider up to four items */
@@ -1440,28 +1407,19 @@ static void store_create(void)
*/
static void display_entry(int pos)
{
- int i, cur_col;
- object_type *o_ptr;
- s32b x;
-
- char o_name[80];
- char out_val[160];
-
-
- int maxwid = 75;
-
/* Get the item */
- o_ptr = &st_ptr->stock[pos];
+ auto o_ptr = &st_ptr->stock[pos];
/* Get the "offset" */
- i = (pos % 12);
+ auto const i = (pos % 12);
/* Label it, clear the line --(-- */
+ char out_val[160];
strnfmt(out_val, 160, "%c) ", I2A(i));
c_prt(get_item_letter_color(o_ptr), out_val, i + 6, 0);
- cur_col = 3;
+ int cur_col = 3;
{
byte a = object_attr(o_ptr);
char c = object_char(o_ptr);
@@ -1476,12 +1434,13 @@ static void display_entry(int pos)
if ((cur_store_num == 7) ||
(st_info[st_ptr->st_idx].flags1 & SF1_MUSEUM))
{
- maxwid = 75;
+ int maxwid = 75;
/* Leave room for weights */
maxwid -= 10;
/* Describe the object */
+ char o_name[80];
object_desc(o_name, o_ptr, TRUE, 3);
o_name[maxwid] = '\0';
c_put_str(tval_to_attr[o_ptr->tval], o_name, i + 6, cur_col);
@@ -1501,12 +1460,13 @@ static void display_entry(int pos)
byte color = TERM_WHITE;
/* Must leave room for the "price" */
- maxwid = 65;
+ int maxwid = 65;
/* Leave room for weights */
maxwid -= 7;
/* Describe the object (fully) */
+ char o_name[80];
object_desc_store(o_name, o_ptr, TRUE, 3);
o_name[maxwid] = '\0';
c_put_str(tval_to_attr[o_ptr->tval], o_name, i + 6, cur_col);
@@ -1520,7 +1480,7 @@ static void display_entry(int pos)
}
/* Extract the "minimum" price */
- x = price_item(o_ptr, ot_ptr->inflation, FALSE);
+ auto const x = price_item(o_ptr, ot_ptr->inflation, FALSE);
/* Can we buy one ? */
if (x > p_ptr->au) color = TERM_L_DARK;
@@ -1544,7 +1504,10 @@ static void display_inventory(void)
for (k = 0; k < 12; k++)
{
/* Do not display "dead" items */
- if (store_top + k >= st_ptr->stock_num) break;
+ if (store_top + k >= static_cast<int>(st_ptr->stock.size()))
+ {
+ break;
+ }
/* Display that line */
display_entry(store_top + k);
@@ -1557,7 +1520,7 @@ static void display_inventory(void)
put_str(" ", 5, 20);
/* Visual reminder of "more items" */
- if (st_ptr->stock_num > 12)
+ if (st_ptr->stock.size() > 12)
{
/* Show "more" reminder (after the last item) */
prt("-more-", k + 6, 3);
@@ -1892,18 +1855,6 @@ static bool_ retire_owner_p(void)
*/
void store_stole(void)
{
- int i, amt;
- int item, item_new;
-
- object_type forge;
- object_type *j_ptr;
-
- object_type *o_ptr;
-
- char o_name[80];
-
- char out_val[160];
-
if (cur_store_num == 7)
{
msg_print("You can't steal from your home!");
@@ -1911,7 +1862,7 @@ void store_stole(void)
}
/* Empty? */
- if (st_ptr->stock_num <= 0)
+ if (st_ptr->stock.empty())
{
msg_print("There is no item to steal.");
return;
@@ -1919,30 +1870,31 @@ void store_stole(void)
/* Find the number of objects on this and following pages */
- i = (st_ptr->stock_num - store_top);
+ int i = (st_ptr->stock.size() - store_top);
/* And then restrict it to the current page */
if (i > 12) i = 12;
/* Prompt */
+ char out_val[160];
strnfmt(out_val, 160, "Which item do you want to steal? ");
/* Get the item number to be bought */
+ int item;
if (!get_stock(&item, out_val, 0, i - 1)) return;
/* Get the actual index */
item = item + store_top;
/* Get the actual item */
- o_ptr = &st_ptr->stock[item];
+ object_type *o_ptr = &st_ptr->stock[item];
/* Assume the player wants just one of them */
- amt = 1;
-
- /* Get local object */
- j_ptr = &forge;
+ int amt = 1;
/* Get a copy of the object */
+ object_type forge;
+ object_type *j_ptr = &forge;
object_copy(j_ptr, o_ptr);
/* Modify quantity */
@@ -2006,6 +1958,7 @@ void store_stole(void)
}
/* Describe the transaction */
+ char o_name[80];
object_desc(o_name, j_ptr, TRUE, 3);
/* Message */
@@ -2015,7 +1968,7 @@ void store_stole(void)
j_ptr->note = 0;
/* Give it to the player */
- item_new = inven_carry(j_ptr, FALSE);
+ int const item_new = inven_carry(j_ptr, FALSE);
/* Describe the final result */
object_desc(o_name, &p_ptr->inventory[item_new], TRUE, 3);
@@ -2028,14 +1981,14 @@ void store_stole(void)
handle_stuff();
/* Note how many slots the store used to have */
- i = st_ptr->stock_num;
+ auto prev_stock_size = st_ptr->stock.size();
/* Remove the bought items from the store */
store_item_increase(item, -amt);
store_item_optimize(item);
/* Store is empty */
- if (st_ptr->stock_num == 0)
+ if (st_ptr->stock.empty())
{
/* Shuffle */
if (retire_owner_p())
@@ -2055,7 +2008,7 @@ void store_stole(void)
}
/* New inventory */
- for (i = 0; i < 10; i++)
+ for (int k = 0; k < 10; k++)
{
/* Maintain the store */
store_maint(p_ptr->town_num, cur_store_num);
@@ -2069,10 +2022,13 @@ void store_stole(void)
}
/* The item is gone */
- else if (st_ptr->stock_num != i)
+ else if (st_ptr->stock.size() != prev_stock_size)
{
/* Pick the correct screen */
- if (store_top >= st_ptr->stock_num) store_top -= 12;
+ if (store_top >= static_cast<int>(st_ptr->stock.size()))
+ {
+ store_top -= 12;
+ }
/* Redraw everything */
display_inventory();
@@ -2103,20 +2059,6 @@ void store_stole(void)
*/
void store_purchase(void)
{
- int i, amt = 1, choice;
- int item, item_new;
-
- s32b price, best;
-
- object_type forge;
- object_type *j_ptr;
-
- object_type *o_ptr;
-
- char o_name[80];
-
- char out_val[160];
-
/* Museum? */
if (st_info[st_ptr->st_idx].flags1 & SF1_MUSEUM)
{
@@ -2125,7 +2067,7 @@ void store_purchase(void)
}
/* Empty? */
- if (st_ptr->stock_num <= 0)
+ if (st_ptr->stock.empty())
{
if (cur_store_num == 7) msg_print("Your home is empty.");
else msg_print("I am currently out of stock.");
@@ -2134,12 +2076,13 @@ void store_purchase(void)
/* Find the number of objects on this and following pages */
- i = (st_ptr->stock_num - store_top);
+ int i = (st_ptr->stock.size() - store_top);
/* And then restrict it to the current page */
if (i > 12) i = 12;
/* Prompt */
+ char out_val[160];
if (cur_store_num == 7)
{
strnfmt(out_val, 160, "Which item do you want to take? ");
@@ -2150,18 +2093,18 @@ void store_purchase(void)
}
/* Get the item number to be bought */
+ int item;
if (!get_stock(&item, out_val, 0, i - 1)) return;
/* Get the actual index */
item = item + store_top;
/* Get the actual item */
- o_ptr = &st_ptr->stock[item];
-
- /* Get local object */
- j_ptr = &forge;
+ auto o_ptr = &st_ptr->stock[item];
/* Get a copy of one object to determine the price */
+ object_type forge;
+ auto j_ptr = &forge;
object_copy(j_ptr, o_ptr);
/* Modify quantity */
@@ -2181,9 +2124,10 @@ void store_purchase(void)
}
/* Determine the "best" price (per item) */
- best = price_item(j_ptr, ot_ptr->inflation, FALSE);
+ auto const best = price_item(j_ptr, ot_ptr->inflation, FALSE);
/* Find out how many the player wants */
+ int amt = 1;
if (o_ptr->number > 1)
{
s32b q;
@@ -2219,10 +2163,8 @@ void store_purchase(void)
if (amt <= 0) return;
}
- /* Get local object */
- j_ptr = &forge;
-
/* Get desired object */
+ j_ptr = &forge;
object_copy(j_ptr, o_ptr);
/* Modify quantity */
@@ -2247,14 +2189,15 @@ void store_purchase(void)
if (cur_store_num != 7)
{
/* Haggle for a final price */
- choice = purchase_haggle(j_ptr, &price);
+ s32b price;
+ auto const choice = purchase_haggle(j_ptr, &price);
/* Hack -- Got kicked out */
if (st_ptr->store_open >= turn) return;
/* Player wants it */
- if (choice == 0)
+ if (!choice)
{
/* Player can afford it */
if (p_ptr->au >= price)
@@ -2279,6 +2222,7 @@ void store_purchase(void)
j_ptr->found_aux1 = st_ptr->st_idx;
/* Describe the transaction */
+ char o_name[80];
object_desc(o_name, j_ptr, TRUE, 3);
/* Message */
@@ -2298,7 +2242,7 @@ void store_purchase(void)
}
/* Give it to the player */
- item_new = inven_carry(j_ptr, FALSE);
+ int const item_new = inven_carry(j_ptr, FALSE);
/* Describe the final result */
object_desc(o_name, &p_ptr->inventory[item_new], TRUE, 3);
@@ -2311,14 +2255,14 @@ void store_purchase(void)
handle_stuff();
/* Note how many slots the store used to have */
- i = st_ptr->stock_num;
+ auto prev_stock_size = st_ptr->stock.size();
/* Remove the bought items from the store */
store_item_increase(item, -amt);
store_item_optimize(item);
/* Store is empty */
- if (st_ptr->stock_num == 0)
+ if (st_ptr->stock.empty())
{
/* Shuffle */
if (retire_owner_p())
@@ -2338,7 +2282,7 @@ void store_purchase(void)
}
/* New inventory */
- for (i = 0; i < 10; i++)
+ for (int k = 0; k < 10; k++)
{
/* Maintain the store */
store_maint(p_ptr->town_num, cur_store_num);
@@ -2349,10 +2293,13 @@ void store_purchase(void)
}
/* The item is gone */
- else if (st_ptr->stock_num != i)
+ else if (st_ptr->stock.size() != prev_stock_size)
{
/* Pick the correct screen */
- if (store_top >= st_ptr->stock_num) store_top -= 12;
+ if (store_top >= static_cast<int>(st_ptr->stock.size()))
+ {
+ store_top -= 12;
+ }
}
/* Redraw everything */
@@ -2382,9 +2329,10 @@ void store_purchase(void)
}
/* Give it to the player */
- item_new = inven_carry(j_ptr, FALSE);
+ int const item_new = inven_carry(j_ptr, FALSE);
/* Describe just the result */
+ char o_name[80];
object_desc(o_name, &p_ptr->inventory[item_new], TRUE, 3);
/* Message */
@@ -2394,14 +2342,14 @@ void store_purchase(void)
handle_stuff();
/* Take note if we take the last one */
- i = st_ptr->stock_num;
+ std::size_t prev_stock_size = st_ptr->stock.size();
/* Remove the items from the home */
store_item_increase(item, -amt);
store_item_optimize(item);
/* Hack -- Item is still here */
- if (i == st_ptr->stock_num)
+ if (prev_stock_size == st_ptr->stock.size())
{
/* Redraw the item */
display_entry(item);
@@ -2411,10 +2359,16 @@ void store_purchase(void)
else
{
/* Nothing left */
- if (st_ptr->stock_num == 0) store_top = 0;
+ if (st_ptr->stock.empty() == 0)
+ {
+ store_top = 0;
+ }
/* Nothing left on that screen */
- else if (store_top >= st_ptr->stock_num) store_top -= 12;
+ else if (store_top >= static_cast<int>(st_ptr->stock.size()))
+ {
+ store_top -= 12;
+ }
/* Redraw everything */
display_inventory();
@@ -2737,18 +2691,8 @@ void store_sell(void)
*/
void store_examine(void)
{
- int i;
- int item;
-
- object_type *o_ptr;
-
- char o_name[80];
-
- char out_val[160];
-
-
/* Empty? */
- if (st_ptr->stock_num <= 0)
+ if (st_ptr->stock.empty())
{
if (cur_store_num == 7) msg_print("Your home is empty.");
else if (st_info[st_ptr->st_idx].flags1 & SF1_MUSEUM) msg_print("The museum is empty.");
@@ -2758,22 +2702,23 @@ void store_examine(void)
/* Find the number of objects on this and following pages */
- i = (st_ptr->stock_num - store_top);
+ int i = (st_ptr->stock.size() - store_top);
/* And then restrict it to the current page */
- if (i > 12) i = 12;
-
- /* Prompt */
- strnfmt(out_val, 160, "Which item do you want to examine? ");
+ if (i > 12)
+ {
+ i = 12;
+ }
/* Get the item number to be examined */
- if (!get_stock(&item, out_val, 0, i - 1)) return;
+ int item;
+ if (!get_stock(&item, "Which item do you want to examine? ", 0, i - 1)) return;
/* Get the actual index */
item = item + store_top;
/* Get the actual item */
- o_ptr = &st_ptr->stock[item];
+ auto o_ptr = &st_ptr->stock[item];
/* Debug hack */
if (wizard)
@@ -2790,6 +2735,7 @@ void store_examine(void)
}
/* Description */
+ char o_name[80];
object_desc(o_name, o_ptr, TRUE, 3);
/* Describe */
@@ -2880,14 +2826,17 @@ static bool_ store_process_command(void)
/* Browse */
case ' ':
{
- if (st_ptr->stock_num <= 12)
+ if (st_ptr->stock.size() <= 12)
{
msg_print("Entire inventory is shown.");
}
else
{
store_top += 12;
- if (store_top >= st_ptr->stock_num) store_top = 0;
+ if (store_top >= static_cast<int>(st_ptr->stock.size()))
+ {
+ store_top = 0;
+ }
display_inventory();
}
break;
@@ -2896,7 +2845,7 @@ static bool_ store_process_command(void)
/* Browse backwards */
case '-':
{
- if (st_ptr->stock_num <= 12)
+ if (st_ptr->stock.size() <= 12)
{
msg_print("Entire inventory is shown.");
}
@@ -2905,7 +2854,7 @@ static bool_ store_process_command(void)
store_top -= 12;
if (store_top < 0)
{
- store_top = ((st_ptr->stock_num - 1) / 12) * 12;
+ store_top = ((st_ptr->stock.size() - 1) / 12) * 12;
}
display_inventory();
}
@@ -3270,7 +3219,7 @@ void do_cmd_store(void)
prt(") Exit.", 22, 4);
/* Browse if necessary */
- if (st_ptr->stock_num > 12)
+ if (st_ptr->stock.size() > 12)
{
c_prt(TERM_YELLOW, " SPACE", 23, 0);
prt(") Next page", 23, 6);
@@ -3427,9 +3376,6 @@ void do_cmd_store(void)
*/
void store_shuffle(int which)
{
- int i, j;
-
-
/* Ignore home */
if (which == STORE_HOME) return;
@@ -3444,7 +3390,7 @@ void store_shuffle(int which)
st_ptr = &town_info[p_ptr->town_num].store[cur_store_num];
/* Pick a new owner */
- for (j = st_ptr->owner; j == st_ptr->owner; )
+ for (auto j = st_ptr->owner; j == st_ptr->owner; )
{
st_ptr->owner = st_info[st_ptr->st_idx].owners[rand_int(4)];
}
@@ -3458,12 +3404,9 @@ void store_shuffle(int which)
/* Hack -- discount all the items */
- for (i = 0; i < st_ptr->stock_num; i++)
+ for (auto &o_ref: st_ptr->stock)
{
- object_type *o_ptr;
-
- /* Get the item */
- o_ptr = &st_ptr->stock[i];
+ auto o_ptr = &o_ref;
/* Hack -- Sell all old items for "half price" */
if (!(o_ptr->art_name))
@@ -3480,9 +3423,7 @@ void store_shuffle(int which)
*/
void store_maint(int town_num, int store_num)
{
- int j, tries = 100;
-
- int old_rating = rating;
+ int const old_rating = rating;
cur_store_num = store_num;
@@ -3502,7 +3443,7 @@ void store_maint(int town_num, int store_num)
if (st_info[st_ptr->st_idx].flags1 & SF1_ALL_ITEM)
{
/* Destroy crappy black market items */
- for (j = st_ptr->stock_num - 1; j >= 0; j--)
+ for (int j = st_ptr->stock.size() - 1; j >= 0; j--)
{
object_type *o_ptr = &st_ptr->stock[j];
@@ -3518,7 +3459,7 @@ void store_maint(int town_num, int store_num)
/* Choose the number of slots to keep */
- j = st_ptr->stock_num;
+ int j = st_ptr->stock.size();
/* Sell a few items */
j = j - randint(STORE_TURNOVER);
@@ -3533,11 +3474,13 @@ void store_maint(int town_num, int store_num)
if (j < 0) j = 0;
/* Destroy objects until only "j" slots are left */
- while (st_ptr->stock_num > j) store_delete();
-
+ while (j < static_cast<int>(st_ptr->stock.size()))
+ {
+ store_delete();
+ }
/* Choose the number of slots to fill */
- j = st_ptr->stock_num;
+ j = st_ptr->stock.size();
/* Buy some more items */
j = j + randint(STORE_TURNOVER);
@@ -3549,13 +3492,15 @@ void store_maint(int town_num, int store_num)
if (j < STORE_MIN_KEEP) j = STORE_MIN_KEEP;
/* Hack -- prevent "overflow" */
- if (j >= st_ptr->stock_size) j = st_ptr->stock_size - 1;
+ if (j >= st_ptr->stock_size)
+ {
+ j = st_ptr->stock_size - 1;
+ }
/* Acquire some new items */
- while ((st_ptr->stock_num < j) && tries)
+ for (int tries = 0; (tries < 100) && (static_cast<int>(st_ptr->stock.size()) < j); tries++)
{
store_create();
- tries--;
}
/* Hack -- Restore the rating */
@@ -3568,8 +3513,6 @@ void store_maint(int town_num, int store_num)
*/
void store_init(int town_num, int store_num)
{
- int k;
-
cur_store_num = store_num;
/* Activate that store */
@@ -3587,19 +3530,14 @@ void store_init(int town_num, int store_num)
st_ptr->store_open = 0;
/* Nothing in stock */
- st_ptr->stock_num = 0;
+ 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
*/
st_ptr->last_visit = -100L * STORE_TURNS;
-
- /* Clear any old items */
- for (k = 0; k < st_ptr->stock_size; k++)
- {
- object_wipe(&st_ptr->stock[k]);
- }
}
@@ -3714,7 +3652,7 @@ void do_cmd_home_trump(void)
prt(" ESC) Exit from Building.", 22, 0);
/* Browse if necessary */
- if (st_ptr->stock_num > 12)
+ if (st_ptr->stock.size() > 12)
{
prt(" SPACE) Next page of stock", 23, 0);
}
diff --git a/src/store_type.hpp b/src/store_type.hpp
index e3f917ac..5fb4ac7f 100644
--- a/src/store_type.hpp
+++ b/src/store_type.hpp
@@ -1,7 +1,9 @@
#pragma once
#include "h-basic.h"
-#include "object_type.hpp"
+#include "object_type_fwd.hpp"
+
+#include <vector>
/**
* A store, with an owner, various state flags, a current stock
@@ -9,35 +11,30 @@
*/
struct store_type
{
- u16b st_idx;
+ u16b st_idx = 0;
/**
* Owner index
*/
- u16b owner;
+ u16b owner = 0;
/**
* Closed until this turn.
*/
- s32b store_open;
+ s32b store_open = 0;
/**
* Last visited on this turn.
*/
- s32b last_visit;
-
- /**
- * Stock: Number of entries.
- */
- byte stock_num;
+ s32b last_visit = 0;
/**
* Stock: Total size of array
*/
- s16b stock_size;
+ u16b stock_size = 0;
/**
* Stock: Actual stock items
*/
- object_type *stock;
+ std::vector<object_type> stock;
};