summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBardur Arantsson <bardur@scientician.net>2016-09-17 09:58:13 +0200
committerBardur Arantsson <bardur@scientician.net>2016-09-17 09:58:13 +0200
commit1f861bb683f70856787b100219fd40ff31be12b8 (patch)
tree1ed334f14ba6dc9bee69ce7b227e7e548248ddc0 /src
parentcb9fde0cde82d4122c8bd9836309720294dee77f (diff)
Rework store_info_type::actions into a std::vector<>
Diffstat (limited to 'src')
-rw-r--r--src/bldg.cc27
-rw-r--r--src/bldg.hpp7
-rw-r--r--src/init1.cc18
-rw-r--r--src/store.cc61
-rw-r--r--src/store_info_type.hpp2
5 files changed, 58 insertions, 57 deletions
diff --git a/src/bldg.cc b/src/bldg.cc
index 34a4576c..04153636 100644
--- a/src/bldg.cc
+++ b/src/bldg.cc
@@ -51,7 +51,7 @@ static int building_loc = 0;
/*
* A helper function for is_state
*/
-static bool_ is_state_aux(store_type *s_ptr, int state)
+static bool_ is_state_aux(store_type const *s_ptr, int state)
{
owner_type *ow_ptr = &ow_info[s_ptr->owner];
@@ -76,7 +76,7 @@ static bool_ is_state_aux(store_type *s_ptr, int state)
/*
* Test if the state accords with the player
*/
-bool_ is_state(store_type *s_ptr, int state)
+bool_ is_state(store_type const *s_ptr, int state)
{
if (state == STORE_NORMAL)
{
@@ -110,25 +110,18 @@ static void clear_bldg(int min_row, int max_row)
/*
* Display a building.
*/
-void show_building(store_type *s_ptr)
+void show_building(store_type const *s_ptr)
{
- char buff[20];
-
- int i;
-
- byte action_color;
-
- char tmp_str[80];
-
store_info_type *st_ptr = &st_info[s_ptr->st_idx];
-
- for (i = 0; i < 6; i++)
+ for (std::size_t i = 0; i < st_ptr->actions.size(); i++)
{
store_action_type *ba_ptr = &ba_info[st_ptr->actions[i]];
- if (ba_ptr->letter != '.')
{
+ byte action_color;
+ char buff[20];
+
if (ba_ptr->action_restr == 0)
{
if ((is_state(s_ptr, STORE_LIKED) && (ba_ptr->costs[STORE_LIKED] == 0)) ||
@@ -197,6 +190,8 @@ void show_building(store_type *s_ptr)
}
}
+ char tmp_str[80];
+
strnfmt(tmp_str, 80, " %c", ba_ptr->letter);
c_put_str(TERM_YELLOW, tmp_str, 21 + (i / 2), 17 + (30 * (i % 2)));
@@ -1094,10 +1089,8 @@ static bool_ research_item(void)
/*
* Execute a building command
*/
-bool_ bldg_process_command(store_type *s_ptr, int i)
+bool_ bldg_process_command(const store_type *s_ptr, store_action_type const *ba_ptr)
{
- store_action_type *ba_ptr = &ba_info[st_info[s_ptr->st_idx].actions[i]];
-
int bact = ba_ptr->action;
int bcost;
diff --git a/src/bldg.hpp b/src/bldg.hpp
index 0daebbee..00eb6a9e 100644
--- a/src/bldg.hpp
+++ b/src/bldg.hpp
@@ -1,9 +1,10 @@
#pragma once
#include "h-basic.h"
+#include "store_action_type_fwd.hpp"
#include "store_type_fwd.hpp"
-extern bool_ bldg_process_command(store_type *s_ptr, int i);
-extern void show_building(store_type *s_ptr);
-extern bool_ is_state(store_type *s_ptr, int state);
+extern bool_ bldg_process_command(store_type const *s_ptr, store_action_type const *action);
+extern void show_building(store_type const *s_ptr);
+extern bool_ is_state(store_type const *s_ptr, int state);
extern void enter_quest(void);
diff --git a/src/init1.cc b/src/init1.cc
index 3dec9b6e..1d04509b 100644
--- a/src/init1.cc
+++ b/src/init1.cc
@@ -5870,12 +5870,18 @@ errr init_st_info_txt(FILE *fp)
&a1, &a2, &a3, &a4, &a5, &a6)) return (1);
/* Save the values */
- st_ptr->actions[0] = a1;
- st_ptr->actions[1] = a2;
- st_ptr->actions[2] = a3;
- st_ptr->actions[3] = a4;
- st_ptr->actions[4] = a5;
- st_ptr->actions[5] = a6;
+ st_ptr->actions.push_back(a1);
+ st_ptr->actions.push_back(a2);
+ st_ptr->actions.push_back(a3);
+ st_ptr->actions.push_back(a4);
+ st_ptr->actions.push_back(a5);
+ st_ptr->actions.push_back(a6);
+
+ /* Remove zero entries since they have no effect */
+ st_ptr->actions.erase(
+ std::remove(st_ptr->actions.begin(), st_ptr->actions.end(), 0),
+ st_ptr->actions.end()
+ );
/* Next... */
continue;
diff --git a/src/store.cc b/src/store.cc
index a7f40a6e..566a7ce0 100644
--- a/src/store.cc
+++ b/src/store.cc
@@ -2772,6 +2772,31 @@ static bool_ leave_store = FALSE;
/*
+ * Find building action for command. Returns nullptr if no matching
+ * action is found.
+ */
+static store_action_type *find_store_action(s16b command_cmd)
+{
+ for (std::size_t i = 0; i < st_info[st_ptr->st_idx].actions.size(); i++)
+ {
+ auto ba_ptr = &ba_info[st_info[st_ptr->st_idx].actions[i]];
+
+ if (ba_ptr->letter && (ba_ptr->letter == command_cmd))
+ {
+ return ba_ptr;
+ }
+
+ if (ba_ptr->letter_aux && (ba_ptr->letter_aux == command_cmd))
+ {
+ return ba_ptr;
+ }
+ }
+
+ return nullptr;
+}
+
+
+/*
* Process a command in a store
*
* Note that we must allow the use of a few "special" commands
@@ -2781,39 +2806,17 @@ static bool_ leave_store = FALSE;
*/
static bool_ store_process_command(void)
{
- bool_ validcmd = FALSE;
- int i;
- store_action_type *ba_ptr;
bool_ recreate = FALSE;
/* Handle repeating the last command */
repeat_check();
- for (i = 0; i < 6; i++)
- {
- ba_ptr = &ba_info[st_info[st_ptr->st_idx].actions[i]];
-
- if (ba_ptr->letter)
- {
- if (ba_ptr->letter == command_cmd)
- {
- validcmd = TRUE;
- break;
- }
- }
- if (ba_ptr->letter_aux)
- {
- if (ba_ptr->letter_aux == command_cmd)
- {
- validcmd = TRUE;
- break;
- }
- }
- }
+ store_action_type *ba_ptr =
+ find_store_action(command_cmd);
- if (validcmd)
+ if (ba_ptr)
{
- recreate = bldg_process_command(st_ptr, i);
+ recreate = bldg_process_command(st_ptr, ba_ptr);
}
else
{
@@ -3193,13 +3196,11 @@ void do_cmd_store(void)
display_store();
/* Mega-Hack -- Ignore keymaps on store action letters */
- for (i = 0; i < 6; i++)
+ for (std::size_t i = 0; i < st_info[st_ptr->st_idx].actions.size(); i++)
{
- store_action_type *ba_ptr =
- &ba_info[st_info[st_ptr->st_idx].actions[i]];
+ store_action_type *ba_ptr = &ba_info[st_info[st_ptr->st_idx].actions[i]];
request_command_ignore_keymaps[2*i] = ba_ptr->letter;
request_command_ignore_keymaps[2*i+1] = ba_ptr->letter_aux;
-
}
/* Do not leave */
diff --git a/src/store_info_type.hpp b/src/store_info_type.hpp
index fad32a93..abb87095 100644
--- a/src/store_info_type.hpp
+++ b/src/store_info_type.hpp
@@ -19,7 +19,7 @@ struct store_info_type
std::vector<u16b> owners; /* List of owners; refers to ow_info */
- u16b actions[6] = { 0 }; /* Actions(refers to ba_info) */
+ std::vector<u16b> actions; /* Actions; refers to ba_info */
byte d_attr = 0; /* Default building attribute */
char d_char = '\0'; /* Default building character */