summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBardur Arantsson <bardur@scientician.net>2016-09-17 09:58:14 +0200
committerBardur Arantsson <bardur@scientician.net>2016-09-17 09:58:14 +0200
commitcbcc1ba9ddafd8c2480e1c647d4b7b5f630bb63e (patch)
treec61f5f66315c0f5618cf234dca59aa3403417de2 /src
parent2ec260320655504b88f8042d4b24e3e7910846a7 (diff)
Refactor spellbinder to use std::vector<>
Diffstat (limited to 'src')
-rw-r--r--src/loadsave.cc31
-rw-r--r--src/spellbinder.hpp8
-rw-r--r--src/spells1.cc9
-rw-r--r--src/spells3.cc24
4 files changed, 42 insertions, 30 deletions
diff --git a/src/loadsave.cc b/src/loadsave.cc
index ea9828d9..292c80b6 100644
--- a/src/loadsave.cc
+++ b/src/loadsave.cc
@@ -263,11 +263,11 @@ static void do_std_string(std::string &s, ls_flag_t flag)
}
}
-/*
- * Load/save flag set
- */
namespace {
+/**
+ * Load/save flag set
+ */
template<std::size_t Tiers> void do_flag_set(flag_set<Tiers> *flags, ls_flag_t flag)
{
for (std::size_t i = 0; i < flags->size(); i++)
@@ -276,6 +276,25 @@ template<std::size_t Tiers> void do_flag_set(flag_set<Tiers> *flags, ls_flag_t f
}
}
+template<typename T, typename F> void do_vector(ls_flag_t flag, std::vector<T> &v, F f)
+{
+ u32b n = v.size();
+
+ do_u32b(&n, flag);
+
+ if (flag == ls_flag_t::LOAD)
+ {
+ v.clear(); // Make sure it's empty
+ v.reserve(n);
+ std::fill_n(std::back_inserter(v), n, T());
+ }
+
+ for (std::size_t i = 0; i < n; i++)
+ {
+ f(&v[i], flag);
+ }
+}
+
} // namespace (anonymous)
@@ -568,12 +587,8 @@ static bool_ do_extra(ls_flag_t flag)
/* Save/load spellbinder */
- do_byte(&p_ptr->spellbinder.num, flag);
do_byte(&p_ptr->spellbinder.trigger, flag);
- for (std::size_t i = 0; i < 4; i++)
- {
- do_u32b(&p_ptr->spellbinder.spells[i], flag);
- }
+ do_vector(flag, p_ptr->spellbinder.spell_idxs, do_u32b);
// Quests
{
diff --git a/src/spellbinder.hpp b/src/spellbinder.hpp
index 1f5cdd99..078d9eac 100644
--- a/src/spellbinder.hpp
+++ b/src/spellbinder.hpp
@@ -1,6 +1,7 @@
#pragma once
#include "h-basic.h"
+#include <vector>
/**
* Spellbinder state
@@ -8,14 +9,9 @@
struct spellbinder {
/**
- * Number of bound spells.
- */
- byte num = 0;
-
- /**
* Bound spells.
*/
- u32b spells[4] = { 0 };
+ std::vector<u32b> spell_idxs;
/**
* Trigger condition.
diff --git a/src/spells1.cc b/src/spells1.cc
index 9ebec08e..93602d13 100644
--- a/src/spells1.cc
+++ b/src/spells1.cc
@@ -1206,13 +1206,14 @@ void spellbinder_trigger()
auto spellbinder = &p_ptr->spellbinder;
cmsg_print(TERM_L_GREEN, "The spellbinder is triggered!");
- for (int i = 0; i < spellbinder->num; i++)
+
+ for (auto spell_idx: spellbinder->spell_idxs)
{
- msg_format("Triggering spell %s.", spell_type_name(spell_at(spellbinder->spells[i])));
- lua_cast_school_spell(spellbinder->spells[i], TRUE);
+ msg_format("Triggering spell %s.", spell_type_name(spell_at(spell_idx)));
+ lua_cast_school_spell(spell_idx, TRUE);
}
- spellbinder->num = 0;
+ spellbinder->spell_idxs.clear();
spellbinder->trigger = 0;
}
diff --git a/src/spells3.cc b/src/spells3.cc
index 69267c7d..68b3a2f0 100644
--- a/src/spells3.cc
+++ b/src/spells3.cc
@@ -2326,7 +2326,7 @@ casting_result meta_spellbinder()
{
auto spellbinder = &p_ptr->spellbinder;
- if (spellbinder->num != 0)
+ if (spellbinder->spell_idxs.size() > 0)
{
struct trigger {
int idx;
@@ -2339,7 +2339,6 @@ casting_result meta_spellbinder()
{ -1, NULL, },
};
int trigger_idx = -1;
- int i;
assert(spellbinder->trigger >= 0);
@@ -2354,9 +2353,9 @@ casting_result meta_spellbinder()
msg_print("The spellbinder is already active.");
msg_format("It will trigger at %s.", triggers[trigger_idx].desc);
msg_print("With the spells: ");
- for (i = 0; i < spellbinder->num; i++)
+ for (auto spell_idx : spellbinder->spell_idxs)
{
- msg_print(spell_type_name(spell_at(spellbinder->spells[i])));
+ msg_print(spell_type_name(spell_at(spell_idx)));
}
/* Doesn't cost anything */
@@ -2365,7 +2364,6 @@ casting_result meta_spellbinder()
else
{
char c;
- int i;
if (!get_com("Trigger at [a]75% hp [b]50% hp [c]25% hp?", &c))
{
@@ -2388,17 +2386,19 @@ casting_result meta_spellbinder()
}
- spellbinder->num = get_spellbinder_max();
- i = spellbinder->num;
- while (i > 0)
+ std::size_t n = get_spellbinder_max();
+ while (n > 0)
{
s32b s = get_school_spell("bind", 0);
+
if (s == -1)
{
spellbinder->trigger = 0;
- spellbinder->num = 0;
+ spellbinder->spell_idxs.clear();
return CAST_OBVIOUS;
- } else {
+ }
+ else
+ {
if (spell_type_skill_level(spell_at(s)) > 7 + get_level_s(SPELLBINDER, 35))
{
msg_format("You are only allowed spells with a base level of " FMTs32b ".", (7 + get_level_s(SPELLBINDER, 35)));
@@ -2406,8 +2406,8 @@ casting_result meta_spellbinder()
}
}
- spellbinder->spells[i] = s;
- i = i - 1;
+ spellbinder->spell_idxs.push_back(s);
+ n--;
}
p_ptr->energy = p_ptr->energy - 3100;