1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-02-15 18:39:51 +00:00

simplifying Spells class

This commit is contained in:
Marc Zinnschlag 2012-04-11 19:40:42 +02:00
parent e04ccfced0
commit 77065390d7
3 changed files with 37 additions and 44 deletions

View File

@ -71,7 +71,7 @@ namespace MWMechanics
for (std::vector<std::string>::const_iterator iter (race->powers.list.begin()); for (std::vector<std::string>::const_iterator iter (race->powers.list.begin());
iter!=race->powers.list.end(); ++iter) iter!=race->powers.list.end(); ++iter)
{ {
creatureStats.mSpells.add (*iter, mEnvironment); creatureStats.mSpells.add (*iter);
} }
} }
@ -85,7 +85,7 @@ namespace MWMechanics
for (std::vector<std::string>::const_iterator iter (sign->powers.list.begin()); for (std::vector<std::string>::const_iterator iter (sign->powers.list.begin());
iter!=sign->powers.list.end(); ++iter) iter!=sign->powers.list.end(); ++iter)
{ {
creatureStats.mSpells.add (*iter, mEnvironment); creatureStats.mSpells.add (*iter);
} }
} }

View File

@ -1,7 +1,7 @@
#include "spells.hpp" #include "spells.hpp"
#include <cassert> #include <components/esm/loadspel.hpp>
#include "../mwworld/environment.hpp" #include "../mwworld/environment.hpp"
#include "../mwworld/world.hpp" #include "../mwworld/world.hpp"
@ -21,57 +21,48 @@ namespace MWMechanics
} }
} }
Spells::TIterator Spells::begin (ESM::Spell::SpellType type) const Spells::TIterator Spells::begin() const
{ {
assert (type>=0 && type<sTypes); return mSpells.begin();
return mSpells[type].begin();
} }
Spells::TIterator Spells::end (ESM::Spell::SpellType type) const Spells::TIterator Spells::end() const
{ {
assert (type>=0 && type<sTypes); return mSpells.end();
return mSpells[type].end();
} }
void Spells::add (const std::string& spellId, MWWorld::Environment& environment) void Spells::add (const std::string& spellId)
{ {
const ESM::Spell *spell = environment.mWorld->getStore().spells.find (spellId); if (std::find (mSpells.begin(), mSpells.end(), spellId)!=mSpells.end())
mSpells.push_back (spellId);
int type = spell->data.type;
assert (type>=0 && type<sTypes);
if (std::find (mSpells[type].begin(), mSpells[type].end(), spell)!=mSpells[type].end())
mSpells[type].push_back (spell);
} }
void Spells::remove (const std::string& spellId, MWWorld::Environment& environment) void Spells::remove (const std::string& spellId)
{ {
const ESM::Spell *spell = environment.mWorld->getStore().spells.find (spellId); TContainer::iterator iter = std::find (mSpells.begin(), mSpells.end(), spellId);
int type = spell->data.type; if (iter!=mSpells.end())
mSpells.erase (iter);
TContainer::iterator iter = std::find (mSpells[type].begin(), mSpells[type].end(), spell);
if (iter!=mSpells[type].end())
mSpells[type].erase (iter);
} }
MagicEffects Spells::getMagicEffects (MWWorld::Environment& environment) const MagicEffects Spells::getMagicEffects (const MWWorld::Environment& environment) const
{ {
MagicEffects effects; MagicEffects effects;
for (int i=ESM::Spell::ST_Ability; i<=ESM::Spell::ST_Curse; ++i) for (TIterator iter = mSpells.begin(); iter!=mSpells.end(); ++iter)
for (TIterator iter (begin (static_cast<ESM::Spell::SpellType> (i))); {
iter!=end(static_cast<ESM::Spell::SpellType> (i)); ++iter) const ESM::Spell *spell = environment.mWorld->getStore().spells.find (*iter);
addSpell (*iter, effects);
if (spell->data.type==ESM::Spell::ST_Ability || spell->data.type==ESM::Spell::ST_Blight ||
spell->data.type==ESM::Spell::ST_Disease || spell->data.type==ESM::Spell::ST_Curse)
addSpell (spell, effects);
}
return effects; return effects;
} }
void Spells::clear() void Spells::clear()
{ {
for (int i=0; i<sTypes; ++i) mSpells.clear();
mSpells[i].clear();
} }
} }

View File

@ -1,9 +1,13 @@
#ifndef GAME_MWMECHANICS_SPELLS_H #ifndef GAME_MWMECHANICS_SPELLS_H
#define GAME_MWMECHANICS_SPELLS_H #define GAME_MWMECHANICS_SPELLS_H
#include <components/esm/loadspel.hpp>
#include <vector> #include <vector>
#include <string>
namespace ESM
{
struct Spell;
}
namespace MWWorld namespace MWWorld
{ {
@ -22,29 +26,27 @@ namespace MWMechanics
{ {
public: public:
typedef std::vector<const ESM::Spell *> TContainer; typedef std::vector<std::string> TContainer;
typedef TContainer::const_iterator TIterator; typedef TContainer::const_iterator TIterator;
private: private:
static const int sTypes = 6; std::vector<std::string> mSpells;
std::vector<const ESM::Spell *> mSpells[sTypes];
void addSpell (const ESM::Spell *, MagicEffects& effects) const; void addSpell (const ESM::Spell *, MagicEffects& effects) const;
public: public:
TIterator begin (ESM::Spell::SpellType type) const; TIterator begin() const;
TIterator end (ESM::Spell::SpellType type) const; TIterator end() const;
void add (const std::string& spell, MWWorld::Environment& environment); void add (const std::string& spell);
/// \note Adding a spell that is already listed in *this is a no-op. /// \note Adding a spell that is already listed in *this is a no-op.
void remove (const std::string& spell, MWWorld::Environment& environment); void remove (const std::string& spell);
MagicEffects getMagicEffects (MWWorld::Environment& environment) const; MagicEffects getMagicEffects (const MWWorld::Environment& environment) const;
///< Return sum of magic effects resulting from abilities, blights, deseases and curses. ///< Return sum of magic effects resulting from abilities, blights, deseases and curses.
void clear(); void clear();