1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-16 16:49:55 +00:00
openmw-tes3mp/apps/openmw/mwmechanics/spells.cpp

266 lines
8.6 KiB
C++
Raw Normal View History

2012-04-11 16:29:36 +00:00
#include "spells.hpp"
#include <cstdlib>
2012-04-11 17:40:42 +00:00
#include <components/esm/loadspel.hpp>
2014-05-12 19:04:02 +00:00
#include <components/esm/spellstate.hpp>
2012-04-11 16:29:36 +00:00
#include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp"
2012-04-11 16:29:36 +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
{
if (mSpells.find (spellId)==mSpells.end())
{
const ESM::Spell* spell = MWBase::Environment::get().getWorld()->getStore().get<ESM::Spell>().find(spellId);
2014-05-12 19:04:02 +00:00
std::map<const int, float> random;
// Determine the random magnitudes (unless this is a castable spell, in which case
// they will be determined when the spell is cast)
if (spell->mData.mType != ESM::Spell::ST_Power && spell->mData.mType != ESM::Spell::ST_Spell)
{
for (unsigned int i=0; i<spell->mEffects.mList.size();++i)
{
if (spell->mEffects.mList[i].mMagnMin != spell->mEffects.mList[i].mMagnMax)
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));
}
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);
if (spellId==mSelectedSpell)
mSelectedSpell.clear();
2012-04-11 16:29:36 +00:00
}
MagicEffects Spells::getMagicEffects() const
2012-04-11 16:29:36 +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 =
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)
{
int i=0;
for (std::vector<ESM::ENAMstruct>::const_iterator it = spell->mEffects.mList.begin(); it != spell->mEffects.mList.end(); ++it)
{
2014-05-12 19:04:02 +00:00
float random = 1.f;
if (iter->second.find(i) != iter->second.end())
random = iter->second.at(i);
effects.add (*it, it->mMagnMin + (it->mMagnMax - it->mMagnMin) * random);
++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
}
void Spells::setSelectedSpell (const std::string& spellId)
{
mSelectedSpell = spellId;
}
const std::string Spells::getSelectedSpell() const
{
return mSelectedSpell;
}
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 =
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;
}
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 =
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;
}
return false;
2012-11-09 17:16:29 +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);
if (spell->mData.mType == ESM::Spell::ST_Disease)
mSpells.erase(iter++);
else
++iter;
}
}
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);
if (spell->mData.mType == ESM::Spell::ST_Blight)
mSpells.erase(iter++);
else
++iter;
}
}
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
++iter;
}
}
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
++iter;
}
}
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)
{
2014-05-12 19:04:02 +00:00
float random = 1.f;
if (it->second.find(i) != it->second.end())
random = it->second.at(i);
float magnitude = effectIt->mMagnMin + (effectIt->mMagnMax - effectIt->mMagnMin) * random;
2014-05-14 05:14:08 +00:00
visitor.visit(MWMechanics::EffectKey(*effectIt), spell->mName, -1, magnitude);
}
}
}
2014-05-12 19:04:02 +00:00
bool Spells::canUsePower(const std::string &power) const
{
std::map<std::string, MWWorld::TimeStamp>::const_iterator it = mUsedPowers.find(power);
if (it == mUsedPowers.end() || it->second + 24 <= MWBase::Environment::get().getWorld()->getTimeStamp())
return true;
else
return false;
}
void Spells::usePower(const std::string &power)
{
mUsedPowers[power] = MWBase::Environment::get().getWorld()->getTimeStamp();
}
void Spells::readState(const ESM::SpellState &state)
{
mSpells = state.mSpells;
mSelectedSpell = state.mSelectedSpell;
// Discard spells that are no longer available due to changed content files
for (TContainer::iterator iter = mSpells.begin(); iter!=mSpells.end();)
{
const ESM::Spell* spell = MWBase::Environment::get().getWorld()->getStore().get<ESM::Spell>().search(iter->first);
if (!spell)
{
if (iter->first == mSelectedSpell)
mSelectedSpell = "";
mSpells.erase(iter++);
}
else
++iter;
}
// No need to discard spells here (doesn't really matter if non existent ids are kept)
for (std::map<std::string, ESM::TimeStamp>::const_iterator it = state.mUsedPowers.begin(); it != state.mUsedPowers.end(); ++it)
mUsedPowers[it->first] = MWWorld::TimeStamp(it->second);
}
void Spells::writeState(ESM::SpellState &state) const
{
state.mSpells = mSpells;
state.mSelectedSpell = mSelectedSpell;
for (std::map<std::string, MWWorld::TimeStamp>::const_iterator it = mUsedPowers.begin(); it != mUsedPowers.end(); ++it)
state.mUsedPowers[it->first] = it->second.toEsm();
}
2012-04-11 16:29:36 +00:00
}