1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-02-01 02:15:32 +00:00

Issue #314: Generalised ActiveSpells class so that it can handle lasting effects from potions too

This commit is contained in:
Marc Zinnschlag 2012-06-24 11:39:21 +02:00
parent 797c2c538d
commit aa827442e8
2 changed files with 31 additions and 11 deletions

View file

@ -3,6 +3,9 @@
#include <cstdlib> #include <cstdlib>
#include <components/esm/loadalch.hpp>
#include <components/esm/loadspel.hpp>
#include "../mwbase/environment.hpp" #include "../mwbase/environment.hpp"
#include "../mwworld/world.hpp" #include "../mwworld/world.hpp"
@ -42,14 +45,13 @@ namespace MWMechanics
for (TIterator iter (begin()); iter!=end(); ++iter) for (TIterator iter (begin()); iter!=end(); ++iter)
{ {
const ESM::Spell& spell = const ESM::EffectList& effects = getEffectList (iter->first);
*MWBase::Environment::get().getWorld()->getStore().spells.find (iter->first);
const MWWorld::TimeStamp& start = iter->second.first; const MWWorld::TimeStamp& start = iter->second.first;
float magnitude = iter->second.second; float magnitude = iter->second.second;
for (std::vector<ESM::ENAMstruct>::const_iterator iter (spell.effects.list.begin()); for (std::vector<ESM::ENAMstruct>::const_iterator iter (effects.list.begin());
iter!=spell.effects.list.end(); ++iter) iter!=effects.list.end(); ++iter)
{ {
if (iter->duration) if (iter->duration)
{ {
@ -70,18 +72,31 @@ namespace MWMechanics
} }
} }
const ESM::EffectList& ActiveSpells::getEffectList (const std::string& id) const
{
if (const ESM::Spell *spell =
MWBase::Environment::get().getWorld()->getStore().spells.search (id))
return spell->effects;
if (const ESM::Potion *potion =
MWBase::Environment::get().getWorld()->getStore().potions.search (id))
return potion->effects;
throw std::runtime_error ("ID " + id + " can not produce lasting effects");
}
ActiveSpells::ActiveSpells() ActiveSpells::ActiveSpells()
: mSpellsChanged (false), mLastUpdate (MWBase::Environment::get().getWorld()->getTimeStamp()) : mSpellsChanged (false), mLastUpdate (MWBase::Environment::get().getWorld()->getTimeStamp())
{} {}
void ActiveSpells::addSpell (const std::string& id) void ActiveSpells::addSpell (const std::string& id)
{ {
const ESM::Spell& spell = *MWBase::Environment::get().getWorld()->getStore().spells.find (id); const ESM::EffectList& effects = getEffectList (id);
bool found = false; bool found = false;
for (std::vector<ESM::ENAMstruct>::const_iterator iter (spell.effects.list.begin()); for (std::vector<ESM::ENAMstruct>::const_iterator iter (effects.list.begin());
iter!=spell.effects.list.end(); ++iter) iter!=effects.list.end(); ++iter)
{ {
if (iter->duration) if (iter->duration)
{ {
@ -137,13 +152,12 @@ namespace MWMechanics
double ActiveSpells::timeToExpire (const TIterator& iterator) const double ActiveSpells::timeToExpire (const TIterator& iterator) const
{ {
const ESM::Spell& spell = const ESM::EffectList& effects = getEffectList (iterator->first);
*MWBase::Environment::get().getWorld()->getStore().spells.find (iterator->first);
int duration = 0; int duration = 0;
for (std::vector<ESM::ENAMstruct>::const_iterator iter (spell.effects.list.begin()); for (std::vector<ESM::ENAMstruct>::const_iterator iter (effects.list.begin());
iter!=spell.effects.list.end(); ++iter) iter!=effects.list.end(); ++iter)
{ {
if (iter->duration>duration) if (iter->duration>duration)
duration = iter->duration; duration = iter->duration;

View file

@ -12,11 +12,15 @@
namespace ESM namespace ESM
{ {
struct Spell; struct Spell;
struct EffectList;
} }
namespace MWMechanics namespace MWMechanics
{ {
/// \brief Lasting spell effects /// \brief Lasting spell effects
///
/// \note The name of this class is slightly misleading, since it also handels lasting potion
/// effects.
class ActiveSpells class ActiveSpells
{ {
public: public:
@ -33,6 +37,8 @@ namespace MWMechanics
void update() const; void update() const;
const ESM::EffectList& getEffectList (const std::string& id) const;
public: public:
ActiveSpells(); ActiveSpells();