1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-19 22:23:51 +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 <components/esm/loadalch.hpp>
#include <components/esm/loadspel.hpp>
#include "../mwbase/environment.hpp"
#include "../mwworld/world.hpp"
@ -42,14 +45,13 @@ namespace MWMechanics
for (TIterator iter (begin()); iter!=end(); ++iter)
{
const ESM::Spell& spell =
*MWBase::Environment::get().getWorld()->getStore().spells.find (iter->first);
const ESM::EffectList& effects = getEffectList (iter->first);
const MWWorld::TimeStamp& start = iter->second.first;
float magnitude = iter->second.second;
for (std::vector<ESM::ENAMstruct>::const_iterator iter (spell.effects.list.begin());
iter!=spell.effects.list.end(); ++iter)
for (std::vector<ESM::ENAMstruct>::const_iterator iter (effects.list.begin());
iter!=effects.list.end(); ++iter)
{
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()
: mSpellsChanged (false), mLastUpdate (MWBase::Environment::get().getWorld()->getTimeStamp())
{}
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;
for (std::vector<ESM::ENAMstruct>::const_iterator iter (spell.effects.list.begin());
iter!=spell.effects.list.end(); ++iter)
for (std::vector<ESM::ENAMstruct>::const_iterator iter (effects.list.begin());
iter!=effects.list.end(); ++iter)
{
if (iter->duration)
{
@ -137,13 +152,12 @@ namespace MWMechanics
double ActiveSpells::timeToExpire (const TIterator& iterator) const
{
const ESM::Spell& spell =
*MWBase::Environment::get().getWorld()->getStore().spells.find (iterator->first);
const ESM::EffectList& effects = getEffectList (iterator->first);
int duration = 0;
for (std::vector<ESM::ENAMstruct>::const_iterator iter (spell.effects.list.begin());
iter!=spell.effects.list.end(); ++iter)
for (std::vector<ESM::ENAMstruct>::const_iterator iter (effects.list.begin());
iter!=effects.list.end(); ++iter)
{
if (iter->duration>duration)
duration = iter->duration;

View file

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