From a07a80ac3cf4cd61a91f25092342142cc68894b1 Mon Sep 17 00:00:00 2001 From: Bardur Arantsson Date: Mon, 23 Feb 2015 09:11:56 +0100 Subject: Simplify spell_type_for_each to spell_type_get_schools --- src/spell_type.cc | 17 ++++++----------- src/spell_type.hpp | 3 ++- src/spells3.cc | 23 ++++++++--------------- src/spells4.cc | 43 +++++++++++++++++++++---------------------- src/spells6.cc | 26 ++++++++++++++------------ 5 files changed, 51 insertions(+), 61 deletions(-) diff --git a/src/spell_type.cc b/src/spell_type.cc index 9b5fba28..eef93e6d 100644 --- a/src/spell_type.cc +++ b/src/spell_type.cc @@ -46,7 +46,7 @@ struct spell_type range_type mana_range; - int school_idxs_count; + size_t school_idxs_count; s32b school_idxs[3]; public: @@ -357,19 +357,14 @@ s16b spell_type_random_type(spell_type *spell) return spell->random_type; } -bool_ spell_type_school_foreach(spell_type *spell, bool_ (*callback)(void *data, s32b school_idx), void *data) +std::vector const spell_type_get_schools(spell_type *spell) { - int i; - - for (i = 0; i < spell->school_idxs_count; i++) + std::vector school_idxs; + for (size_t i = 0; i < spell->school_idxs_count; i++) { - if (!callback(data, spell->school_idxs[i])) - { - return FALSE; - } + school_idxs.push_back(spell->school_idxs[i]); } - - return TRUE; + return school_idxs; } bool_ spell_type_inertia(spell_type *spell, s32b *difficulty, s32b *delay) diff --git a/src/spell_type.hpp b/src/spell_type.hpp index ac710341..c053f9fd 100644 --- a/src/spell_type.hpp +++ b/src/spell_type.hpp @@ -1,6 +1,7 @@ #pragma once #include "spell_type_fwd.hpp" +#include #include #include #include "h-basic.h" @@ -76,7 +77,7 @@ bool_ spell_type_castable_while_blind(spell_type *spell); bool_ spell_type_castable_while_confused(spell_type *spell); s16b spell_type_minimum_pval(spell_type *spell); s16b spell_type_random_type(spell_type *spell); -bool_ spell_type_school_foreach(spell_type *spell, bool_ (*callback)(void *data, s32b school_idx), void *data); +std::vector const spell_type_get_schools(spell_type *spell); bool_ spell_type_inertia(spell_type *spell, s32b *difficulty, s32b *delay); s32b spell_type_failure_rate(spell_type *spell); s16b spell_type_casting_stat(spell_type *spell); diff --git a/src/spells3.cc b/src/spells3.cc index 4cabf892..9050cab9 100644 --- a/src/spells3.cc +++ b/src/spells3.cc @@ -2884,20 +2884,6 @@ casting_result tulkas_whirlwind() return cast(fire_ball(GF_ATTACK, 0, 1, 1)); } -static bool_ check_school_is_udun(void *data, s32b school_idx) -{ - int *count = (int *) data; - - if ((school_idx == SCHOOL_UDUN) || - (school_idx == SCHOOL_MELKOR)) - { - (*count)++; - } - - /* Keep going */ - return TRUE; -} - /* Return the number of Udun/Melkor spells in a given book */ int udun_in_book(s32b sval, s32b pval) { @@ -2912,7 +2898,14 @@ int udun_in_book(s32b sval, s32b pval) /* Go through spells */ for (auto spell_idx : school_book->spell_idx_list->v) { spell_type *spell = spell_at(spell_idx); - spell_type_school_foreach(spell, check_school_is_udun, &count); + for (auto school_idx : spell_type_get_schools(spell)) + { + if ((school_idx == SCHOOL_UDUN) || + (school_idx == SCHOOL_MELKOR)) + { + count++; + } + } } return count; diff --git a/src/spells4.cc b/src/spells4.cc index f3ffbfe7..9b71bbb4 100644 --- a/src/spells4.cc +++ b/src/spells4.cc @@ -1,6 +1,7 @@ #include "angband.h" #include +#include #include "lua_bind.hpp" #include "spell_type.hpp" @@ -396,30 +397,28 @@ void random_book_setup(s16b sval, s32b spell_idx) } } -static bool_ spell_school_name_callback(void *data, s32b sch) +static std::string spell_school_name(spell_type *spell) { - school_type *school = school_at(sch); - char *buf = (char *) data; + std::stringstream buf; + bool first = true; - /* Add separator? */ - if (buf[0] != '\0') + for (s32b school_idx : spell_type_get_schools(spell)) { - strcat(buf, "/"); + school_type *school = school_at(school_idx); + // Add separator? + if (first) + { + first = false; // Skip separator + } + else + { + buf << "/"; + } + // Put in the school's name + buf << school->name; } - /* Add school name */ - strcat(buf, school->name); - - /* Keep going */ - return TRUE; -} - -static void spell_school_name(char *buf, spell_type *spell) -{ - buf[0] = '\0'; - spell_type_school_foreach(spell, - spell_school_name_callback, - buf); + return buf.str(); } int print_spell(cptr label_, byte color, int y, s32b s) @@ -427,14 +426,14 @@ int print_spell(cptr label_, byte color, int y, s32b s) s32b level; bool_ na; spell_type *spell = spell_at(s); - char sch_str[128]; cptr spell_info = spell_type_info(spell); cptr label = (label_ == NULL) ? "" : label_; char level_str[8] = "n/a"; char buf[128]; get_level_school(spell, 50, -50, &level, &na); - spell_school_name(sch_str, spell); + + std::string sch_str(spell_school_name(spell)); if (!na) { @@ -444,7 +443,7 @@ int print_spell(cptr label_, byte color, int y, s32b s) sprintf(buf, "%s%-20s%-16s %s %4d %3d%% %s", label, spell_type_name(spell_at(s)), - sch_str, + sch_str.c_str(), level_str, get_mana(s), (int) spell_chance_book(s), diff --git a/src/spells6.cc b/src/spells6.cc index b1db90d0..7fc615ac 100644 --- a/src/spells6.cc +++ b/src/spells6.cc @@ -152,7 +152,6 @@ long get_provided_levels(school_type *school) return 0; } -typedef struct get_level_school_callback_data get_level_school_callback_data; struct get_level_school_callback_data { bool_ allow_spell_power; long bonus; @@ -160,9 +159,8 @@ struct get_level_school_callback_data { long num; }; -static bool_ get_level_school_callback(void *data_, int school_idx) +static bool get_level_school_callback(struct get_level_school_callback_data *data, int school_idx) { - get_level_school_callback_data *data = static_cast(data_); school_type *school = school_at(school_idx); long r = 0, s = 0, p = 0, ok = 0; @@ -170,7 +168,7 @@ static bool_ get_level_school_callback(void *data_, int school_idx) if ((school->deity_idx > 0) && (school->deity_idx != p_ptr->pgod)) { - return FALSE; + return false; } /* Take the basic skill value */ @@ -180,7 +178,7 @@ static bool_ get_level_school_callback(void *data_, int school_idx) if ((school->depends_satisfied != NULL) && (!school->depends_satisfied())) { - return FALSE; + return false; } /* Include effects of Sorcery (if applicable) */ @@ -219,7 +217,7 @@ static bool_ get_level_school_callback(void *data_, int school_idx) /* All schools must be non-zero to be able to use it. */ if (ok <= 0) { - return FALSE; + return false; } /* Apply it */ @@ -227,7 +225,7 @@ static bool_ get_level_school_callback(void *data_, int school_idx) data->num += 1; /* Keep going */ - return TRUE; + return true; } void get_level_school(spell_type *spell, s32b max, s32b min, s32b *level, bool_ *na) @@ -250,12 +248,16 @@ void get_level_school(spell_type *spell, s32b max, s32b min, s32b *level, bool_ data.lvl = 0; data.num = 0; - /* Go through all the spell's schools. */ - if (!spell_type_school_foreach(spell, get_level_school_callback, &data)) + // Go through all the spell's schools and count up all the + // levels and make sure we can actually cast the spell. + for (auto school_idx : spell_type_get_schools(spell)) { - *level = min; - *na = TRUE; - return; + if (!get_level_school_callback(&data, school_idx)) + { + *level = min; + *na = TRUE; + return; + } } /* Add the Spellpower skill as a bonus on top */ -- cgit v1.2.3