2012-04-11 16:29:36 +00:00
|
|
|
|
|
|
|
#include "spells.hpp"
|
|
|
|
|
2013-01-13 13:52:55 +00:00
|
|
|
#include <cstdlib>
|
2012-07-03 10:30:50 +00:00
|
|
|
|
2012-04-11 17:40:42 +00:00
|
|
|
#include <components/esm/loadspel.hpp>
|
2012-04-11 16:29:36 +00:00
|
|
|
|
2012-04-23 13:27:03 +00:00
|
|
|
#include "../mwbase/environment.hpp"
|
2012-07-03 10:30:50 +00:00
|
|
|
#include "../mwbase/world.hpp"
|
2012-04-11 16:29:36 +00:00
|
|
|
|
2013-01-13 13:52:55 +00:00
|
|
|
#include "../mwworld/esmstore.hpp"
|
|
|
|
|
2012-04-11 16:29:36 +00:00
|
|
|
#include "magiceffects.hpp"
|
|
|
|
|
|
|
|
namespace MWMechanics
|
|
|
|
{
|
2012-04-11 17:40:42 +00:00
|
|
|
Spells::TIterator Spells::begin() const
|
2012-04-11 16:29:36 +00:00
|
|
|
{
|
2012-04-11 17:40:42 +00:00
|
|
|
return mSpells.begin();
|
2012-04-11 16:29:36 +00:00
|
|
|
}
|
|
|
|
|
2012-04-11 17:40:42 +00:00
|
|
|
Spells::TIterator Spells::end() const
|
2012-04-11 16:29:36 +00:00
|
|
|
{
|
2012-04-11 17:40:42 +00:00
|
|
|
return mSpells.end();
|
2012-04-11 16:29:36 +00:00
|
|
|
}
|
|
|
|
|
2012-04-11 17:40:42 +00:00
|
|
|
void Spells::add (const std::string& spellId)
|
2012-04-11 16:29:36 +00:00
|
|
|
{
|
2013-01-12 12:10:20 +00:00
|
|
|
if (mSpells.find (spellId)==mSpells.end())
|
2013-11-13 17:51:28 +00:00
|
|
|
{
|
|
|
|
const ESM::Spell* spell = MWBase::Environment::get().getWorld()->getStore().get<ESM::Spell>().find(spellId);
|
|
|
|
|
|
|
|
std::vector<float> random;
|
|
|
|
random.resize(spell->mEffects.mList.size());
|
2013-11-14 12:30:48 +00:00
|
|
|
for (unsigned int i=0; i<random.size();++i)
|
2013-11-13 17:51:28 +00:00
|
|
|
random[i] = static_cast<float> (std::rand()) / RAND_MAX;
|
2014-01-03 04:19:10 +00:00
|
|
|
mSpells.insert (std::make_pair (Misc::StringUtils::lowerCase(spellId), random));
|
2013-11-13 17:51:28 +00:00
|
|
|
}
|
2012-04-11 16:29:36 +00:00
|
|
|
}
|
|
|
|
|
2012-04-11 17:40:42 +00:00
|
|
|
void Spells::remove (const std::string& spellId)
|
2012-04-11 16:29:36 +00:00
|
|
|
{
|
2014-01-03 04:19:10 +00:00
|
|
|
std::string lower = Misc::StringUtils::lowerCase(spellId);
|
|
|
|
TContainer::iterator iter = mSpells.find (lower);
|
2012-04-11 16:29:36 +00:00
|
|
|
|
2012-04-11 17:40:42 +00:00
|
|
|
if (iter!=mSpells.end())
|
|
|
|
mSpells.erase (iter);
|
2012-04-13 08:49:45 +00:00
|
|
|
|
|
|
|
if (spellId==mSelectedSpell)
|
|
|
|
mSelectedSpell.clear();
|
2012-04-11 16:29:36 +00:00
|
|
|
}
|
|
|
|
|
2012-04-23 13:27:03 +00:00
|
|
|
MagicEffects Spells::getMagicEffects() const
|
2012-04-11 16:29:36 +00:00
|
|
|
{
|
2013-11-14 12:30:48 +00:00
|
|
|
// TODO: These are recalculated every frame, no need to do that
|
|
|
|
|
2012-04-11 16:29:36 +00:00
|
|
|
MagicEffects effects;
|
|
|
|
|
2012-04-11 17:40:42 +00:00
|
|
|
for (TIterator iter = mSpells.begin(); iter!=mSpells.end(); ++iter)
|
|
|
|
{
|
2012-11-06 10:44:56 +00:00
|
|
|
const ESM::Spell *spell =
|
2013-01-12 12:10:20 +00:00
|
|
|
MWBase::Environment::get().getWorld()->getStore().get<ESM::Spell>().find (iter->first);
|
2012-04-11 17:40:42 +00:00
|
|
|
|
2012-09-17 07:37:50 +00:00
|
|
|
if (spell->mData.mType==ESM::Spell::ST_Ability || spell->mData.mType==ESM::Spell::ST_Blight ||
|
|
|
|
spell->mData.mType==ESM::Spell::ST_Disease || spell->mData.mType==ESM::Spell::ST_Curse)
|
2013-11-13 17:51:28 +00:00
|
|
|
{
|
|
|
|
int i=0;
|
|
|
|
for (std::vector<ESM::ENAMstruct>::const_iterator it = spell->mEffects.mList.begin(); it != spell->mEffects.mList.end(); ++it)
|
|
|
|
{
|
2013-11-20 23:27:22 +00:00
|
|
|
effects.add (*it, it->mMagnMin + (it->mMagnMax - it->mMagnMin) * iter->second[i]);
|
2013-11-13 17:51:28 +00:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
2012-04-11 17:40:42 +00:00
|
|
|
}
|
2012-04-11 16:29:36 +00:00
|
|
|
|
|
|
|
return effects;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Spells::clear()
|
|
|
|
{
|
2012-04-11 17:40:42 +00:00
|
|
|
mSpells.clear();
|
2012-04-11 16:29:36 +00:00
|
|
|
}
|
2012-04-13 08:49:45 +00:00
|
|
|
|
|
|
|
void Spells::setSelectedSpell (const std::string& spellId)
|
|
|
|
{
|
|
|
|
mSelectedSpell = spellId;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string Spells::getSelectedSpell() const
|
|
|
|
{
|
|
|
|
return mSelectedSpell;
|
|
|
|
}
|
2013-01-12 12:10:20 +00:00
|
|
|
|
2012-11-09 17:16:29 +00:00
|
|
|
bool Spells::hasCommonDisease() const
|
|
|
|
{
|
|
|
|
for (TIterator iter = mSpells.begin(); iter!=mSpells.end(); ++iter)
|
|
|
|
{
|
|
|
|
const ESM::Spell *spell =
|
2013-01-12 12:10:20 +00:00
|
|
|
MWBase::Environment::get().getWorld()->getStore().get<ESM::Spell>().find (iter->first);
|
|
|
|
|
2013-04-14 15:51:17 +00:00
|
|
|
if (spell->mData.mType == ESM::Spell::ST_Disease)
|
2012-11-09 17:16:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-01-12 12:10:20 +00:00
|
|
|
|
2012-11-09 17:16:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Spells::hasBlightDisease() const
|
|
|
|
{
|
|
|
|
for (TIterator iter = mSpells.begin(); iter!=mSpells.end(); ++iter)
|
|
|
|
{
|
|
|
|
const ESM::Spell *spell =
|
2013-01-12 12:10:20 +00:00
|
|
|
MWBase::Environment::get().getWorld()->getStore().get<ESM::Spell>().find (iter->first);
|
|
|
|
|
2013-04-14 15:51:17 +00:00
|
|
|
if (spell->mData.mType == ESM::Spell::ST_Blight)
|
2012-11-09 17:16:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-01-12 12:10:20 +00:00
|
|
|
|
|
|
|
return false;
|
2012-11-09 17:16:29 +00:00
|
|
|
}
|
2013-11-15 19:29:47 +00:00
|
|
|
|
2013-11-17 22:15:57 +00:00
|
|
|
void Spells::purgeCommonDisease()
|
|
|
|
{
|
|
|
|
for (TContainer::iterator iter = mSpells.begin(); iter!=mSpells.end();)
|
|
|
|
{
|
|
|
|
const ESM::Spell *spell =
|
|
|
|
MWBase::Environment::get().getWorld()->getStore().get<ESM::Spell>().find (iter->first);
|
|
|
|
|
2013-12-27 17:29:15 +00:00
|
|
|
if (spell->mData.mType == ESM::Spell::ST_Disease)
|
2013-11-17 22:15:57 +00:00
|
|
|
mSpells.erase(iter++);
|
|
|
|
else
|
2014-04-27 17:03:33 +00:00
|
|
|
++iter;
|
2013-11-17 22:15:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Spells::purgeBlightDisease()
|
|
|
|
{
|
|
|
|
for (TContainer::iterator iter = mSpells.begin(); iter!=mSpells.end();)
|
|
|
|
{
|
|
|
|
const ESM::Spell *spell =
|
|
|
|
MWBase::Environment::get().getWorld()->getStore().get<ESM::Spell>().find (iter->first);
|
|
|
|
|
2013-12-27 17:29:15 +00:00
|
|
|
if (spell->mData.mType == ESM::Spell::ST_Blight)
|
2013-11-17 22:15:57 +00:00
|
|
|
mSpells.erase(iter++);
|
|
|
|
else
|
2014-04-27 17:03:33 +00:00
|
|
|
++iter;
|
2013-11-17 22:15:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Spells::purgeCorprusDisease()
|
|
|
|
{
|
|
|
|
for (TContainer::iterator iter = mSpells.begin(); iter!=mSpells.end();)
|
|
|
|
{
|
|
|
|
const ESM::Spell *spell =
|
|
|
|
MWBase::Environment::get().getWorld()->getStore().get<ESM::Spell>().find (iter->first);
|
|
|
|
|
|
|
|
if (Misc::StringUtils::ciEqual(spell->mId, "corprus"))
|
|
|
|
mSpells.erase(iter++);
|
|
|
|
else
|
2014-04-27 17:03:33 +00:00
|
|
|
++iter;
|
2013-11-17 22:15:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Spells::purgeCurses()
|
|
|
|
{
|
|
|
|
for (TContainer::iterator iter = mSpells.begin(); iter!=mSpells.end();)
|
|
|
|
{
|
|
|
|
const ESM::Spell *spell =
|
|
|
|
MWBase::Environment::get().getWorld()->getStore().get<ESM::Spell>().find (iter->first);
|
|
|
|
|
|
|
|
if (spell->mData.mType == ESM::Spell::ST_Curse)
|
|
|
|
mSpells.erase(iter++);
|
|
|
|
else
|
2014-04-27 17:03:33 +00:00
|
|
|
++iter;
|
2013-11-17 22:15:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-15 19:29:47 +00:00
|
|
|
void Spells::visitEffectSources(EffectSourceVisitor &visitor) const
|
|
|
|
{
|
|
|
|
for (TIterator it = begin(); it != end(); ++it)
|
|
|
|
{
|
|
|
|
const ESM::Spell* spell = MWBase::Environment::get().getWorld()->getStore().get<ESM::Spell>().find(it->first);
|
|
|
|
|
|
|
|
// these are the spell types that are permanently in effect
|
|
|
|
if (!(spell->mData.mType == ESM::Spell::ST_Ability)
|
|
|
|
&& !(spell->mData.mType == ESM::Spell::ST_Disease)
|
|
|
|
&& !(spell->mData.mType == ESM::Spell::ST_Curse)
|
|
|
|
&& !(spell->mData.mType == ESM::Spell::ST_Blight))
|
|
|
|
continue;
|
|
|
|
const ESM::EffectList& list = spell->mEffects;
|
|
|
|
int i=0;
|
|
|
|
for (std::vector<ESM::ENAMstruct>::const_iterator effectIt = list.mList.begin();
|
|
|
|
effectIt != list.mList.end(); ++effectIt, ++i)
|
|
|
|
{
|
|
|
|
float magnitude = effectIt->mMagnMin + (effectIt->mMagnMax - effectIt->mMagnMin) * it->second[i];
|
2014-01-02 20:21:28 +00:00
|
|
|
visitor.visit(MWMechanics::EffectKey(*effectIt), spell->mName, "", magnitude);
|
2013-11-15 19:29:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-11 16:29:36 +00:00
|
|
|
}
|