summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBardur Arantsson <bardur@scientician.net>2016-03-10 18:41:54 +0100
committerBardur Arantsson <bardur@scientician.net>2016-03-10 19:02:23 +0100
commit8915788267bb1036c110195eb0a8826b6592cbd5 (patch)
treed60518d32b6d8f8090fa80670c78ffa074d4b58e /src
parent7af6bc9b2ec5326181fee27dad9e465992b2b8a8 (diff)
Split use_symbiotic_power() into two separate functions
This split makes it much clearer which functionality is being used at call sites. We also eliminate a little bit of duplicate code.
Diffstat (limited to 'src')
-rw-r--r--src/cmd5.cc79
-rw-r--r--src/cmd5.hpp3
-rw-r--r--src/cmd7.cc15
3 files changed, 57 insertions, 40 deletions
diff --git a/src/cmd5.cc b/src/cmd5.cc
index 120e2e96..a7589e6c 100644
--- a/src/cmd5.cc
+++ b/src/cmd5.cc
@@ -660,9 +660,20 @@ std::vector<int> extract_monster_powers(monster_race const *r_ptr, bool great)
}
/**
+ * Calculate mana required for a given monster power.
+ */
+static int calc_monster_spell_mana(monster_power const *mp_ptr)
+{
+ int mana = mp_ptr->mana / 10;
+ if (mana > p_ptr->msp) mana = p_ptr->msp;
+ if (!mana) mana = 1;
+ return mana;
+}
+
+/**
* Choose a monster power
*/
-static std::tuple<int, int> choose_monster_power(monster_race const *r_ptr, bool great, bool no_cost)
+static std::tuple<int, int> choose_monster_power(monster_race const *r_ptr, bool great, bool symbiosis)
{
/* Extract available monster powers */
std::vector<int> powers = extract_monster_powers(r_ptr, great);
@@ -682,7 +693,7 @@ static std::tuple<int, int> choose_monster_power(monster_race const *r_ptr, bool
char out_val[160];
strnfmt(out_val, 78,
"(Powers a-%c, ESC=exit) Use which power of your %s? ",
- label, (no_cost ? "symbiote" : "body"));
+ label, (symbiosis ? "symbiote" : "body"));
/* Save the screen */
character_icky = TRUE;
@@ -706,16 +717,12 @@ static std::tuple<int, int> choose_monster_power(monster_race const *r_ptr, bool
while (ctr < num)
{
monster_power *mp_ptr = &monster_powers[powers[ctr]];
- int mana = mp_ptr->mana / 10;
-
- if (mana > p_ptr->msp) mana = p_ptr->msp;
-
- if (!mana) mana = 1;
label = (ctr < 26) ? I2A(ctr) : I2D(ctr - 26);
- if (!no_cost)
+ if (!symbiosis)
{
+ int mana = calc_monster_spell_mana(mp_ptr);
strnfmt(dummy, 80, " %c) %2d %s",
label, mana, mp_ptr->name);
}
@@ -1874,15 +1881,13 @@ static void apply_monster_power(monster_race const *r_ptr, int power)
/*
- * Use a power of the monster in symbiosis
+ * Use a monster power and call the given callback.
*/
-int use_symbiotic_power(int r_idx, bool great, bool no_cost)
+static int use_monster_power_aux(monster_race const *r_ptr, bool great, bool symbiosis, std::function<void(monster_power const *power)> f)
{
- monster_race const *r_ptr = &r_info[r_idx];
-
int power;
int num;
- std::tie(power, num) = choose_monster_power(r_ptr, great, no_cost);
+ std::tie(power, num) = choose_monster_power(r_ptr, great, symbiosis);
// Early exit?
if (power == 0) {
@@ -1894,25 +1899,11 @@ int use_symbiotic_power(int r_idx, bool great, bool no_cost)
return -1;
}
- /* Apply the effect */
+ // Apply the effect
apply_monster_power(r_ptr, power);
- /* Take some SP */
- if (!no_cost)
- {
- int chance = (monster_powers[power].mana + r_ptr->level);
- int pchance = adj_str_wgt[p_ptr->stat_ind[A_WIS]] / 2 + get_skill(SKILL_POSSESSION);
-
- if (rand_int(chance) >= pchance)
- {
- int m = monster_powers[power].mana / 10;
-
- if (m > p_ptr->msp) m = p_ptr->msp;
- if (!m) m = 1;
-
- p_ptr->csp -= m;
- }
- }
+ // Post-processing
+ f(&monster_powers[power]);
/* Redraw mana */
p_ptr->redraw |= (PR_FRAME);
@@ -1923,6 +1914,34 @@ int use_symbiotic_power(int r_idx, bool great, bool no_cost)
return (num);
}
+/**
+ * Use a power of the monster in symbiosis
+ */
+int use_symbiotic_power(int r_idx, bool great)
+{
+ monster_race const *r_ptr = &r_info[r_idx];
+ return use_monster_power_aux(r_ptr, great, true, [](monster_power const *) {
+ // Don't need to do anything post-cast.
+ });
+}
+
+/**
+ * Use a power of a possessed body.
+ */
+void use_monster_power(int r_idx, bool great)
+{
+ monster_race const *r_ptr = &r_info[r_idx];
+ use_monster_power_aux(r_ptr, great, false, [r_ptr](monster_power const *power) {
+ // Sometimes give a free cast.
+ int chance = (power->mana + r_ptr->level);
+ int pchance = adj_str_wgt[p_ptr->stat_ind[A_WIS]] / 2 + get_skill(SKILL_POSSESSION);
+ if (rand_int(chance) >= pchance)
+ {
+ p_ptr->csp -= calc_monster_spell_mana(power);
+ }
+ });
+}
+
/*
* Schooled magic
*/
diff --git a/src/cmd5.hpp b/src/cmd5.hpp
index 1b436f0d..4b116596 100644
--- a/src/cmd5.hpp
+++ b/src/cmd5.hpp
@@ -11,7 +11,8 @@ extern void do_cmd_browse(void);
extern void fetch(int dir, int wgt, bool_ require_los);
extern void do_poly_self(void);
extern cptr symbiote_name(bool_ capitalize);
-extern int use_symbiotic_power(int r_idx, bool great, bool no_cost);
+extern int use_symbiotic_power(int r_idx, bool great);
+extern void use_monster_power(int r_idx, bool great);
extern bool_ is_ok_spell(s32b spell_idx, s32b pval);
extern s32b get_school_spell(cptr do_what, s16b force_book);
extern void do_cmd_copy_spell(void);
diff --git a/src/cmd7.cc b/src/cmd7.cc
index 711bf2ca..a27d7e4b 100644
--- a/src/cmd7.cc
+++ b/src/cmd7.cc
@@ -1841,7 +1841,7 @@ void do_cmd_possessor()
bool use_great = (get_skill_scale(SKILL_POSSESSION, 100) >= r_info[p_ptr->body_monster].level);
/* Select power */
- use_symbiotic_power(p_ptr->body_monster, use_great, false);
+ use_monster_power(p_ptr->body_monster, use_great);
if (p_ptr->csp < 0)
{
@@ -4268,7 +4268,7 @@ void do_cmd_symbiotic(void)
break;
}
- if (0 > use_symbiotic_power(o_ptr->pval, false, true))
+ if (0 > use_symbiotic_power(o_ptr->pval, false))
return;
break;
@@ -4307,7 +4307,7 @@ void do_cmd_symbiotic(void)
break;
}
- if(0 > use_symbiotic_power(o_ptr->pval, true, true))
+ if(0 > use_symbiotic_power(o_ptr->pval, true))
return;
break;
@@ -4325,17 +4325,14 @@ void do_cmd_symbiotic(void)
case 8:
{
int y, x;
- cave_type *c_ptr;
- monster_type *m_ptr;
-
if (!tgt_pt(&x, &y)) return;
- c_ptr = &cave[y][x];
+ cave_type *c_ptr = &cave[y][x];
if (!c_ptr->m_idx) break;
- m_ptr = &m_list[c_ptr->m_idx];
- use_symbiotic_power(m_ptr->r_idx, true, true);
+ monster_type *m_ptr = &m_list[c_ptr->m_idx];
+ use_symbiotic_power(m_ptr->r_idx, true);
break;
}