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

498 lines
18 KiB
C++
Raw Normal View History

2012-04-11 16:29:36 +00:00
#include "spells.hpp"
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>
2015-07-09 17:22:04 +00:00
#include <components/misc/rng.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
{
Spells::Spells()
: mSpellsChanged(false)
{
}
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
}
const ESM::Spell* Spells::getSpell(const std::string& id) const
{
return MWBase::Environment::get().getWorld()->getStore().get<ESM::Spell>().find(id);
}
void Spells::rebuildEffects() const
{
mEffects = MagicEffects();
mSourcedEffects.clear();
for (TIterator iter = mSpells.begin(); iter!=mSpells.end(); ++iter)
{
const ESM::Spell *spell = iter->first;
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)
{
if (iter->second.mPurgedEffects.find(i) != iter->second.mPurgedEffects.end())
continue; // effect was purged
float random = 1.f;
if (iter->second.mEffectRands.find(i) != iter->second.mEffectRands.end())
random = iter->second.mEffectRands.at(i);
float magnitude = it->mMagnMin + (it->mMagnMax - it->mMagnMin) * random;
mEffects.add (*it, magnitude);
mSourcedEffects[spell].add(MWMechanics::EffectKey(*it), magnitude);
++i;
}
}
}
for (std::map<SpellKey, MagicEffects>::const_iterator it = mPermanentSpellEffects.begin(); it != mPermanentSpellEffects.end(); ++it)
{
mEffects += it->second;
mSourcedEffects[it->first] += it->second;
}
}
bool Spells::hasSpell(const std::string &spell) const
{
return hasSpell(getSpell(spell));
}
bool Spells::hasSpell(const ESM::Spell *spell) const
{
return mSpells.find(spell) != mSpells.end();
}
void Spells::add (const ESM::Spell* spell)
2012-04-11 16:29:36 +00:00
{
if (mSpells.find (spell)==mSpells.end())
{
2016-01-25 13:13:16 +00:00
std::map<int, float> random;
2014-05-12 19:04:02 +00:00
// 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)
2015-04-22 15:58:55 +00:00
random[i] = Misc::Rng::rollClosedProbability();
2014-05-12 19:04:02 +00:00
}
}
if (hasCorprusEffect(spell))
{
CorprusStats corprus;
corprus.mWorsenings = 0;
corprus.mNextWorsening = MWBase::Environment::get().getWorld()->getTimeStamp() + CorprusStats::sWorseningPeriod;
mCorprusSpells[spell] = corprus;
}
SpellParams params;
params.mEffectRands = random;
mSpells.insert (std::make_pair (spell, params));
mSpellsChanged = true;
}
2012-04-11 16:29:36 +00:00
}
void Spells::add (const std::string& spellId)
{
add(getSpell(spellId));
}
2012-04-11 17:40:42 +00:00
void Spells::remove (const std::string& spellId)
2012-04-11 16:29:36 +00:00
{
const ESM::Spell* spell = getSpell(spellId);
TContainer::iterator iter = mSpells.find (spell);
std::map<SpellKey, CorprusStats>::iterator corprusIt = mCorprusSpells.find(spell);
2012-04-11 16:29:36 +00:00
2014-08-21 01:11:49 +00:00
// if it's corprus, remove negative and keep positive effects
if (corprusIt != mCorprusSpells.end())
2014-08-21 01:11:49 +00:00
{
worsenCorprus(spell);
if (mPermanentSpellEffects.find(spell) != mPermanentSpellEffects.end())
2014-08-21 01:11:49 +00:00
{
MagicEffects & effects = mPermanentSpellEffects[spell];
2014-11-02 14:40:08 +00:00
for (MagicEffects::Collection::const_iterator effectIt = effects.begin(); effectIt != effects.end();)
2014-08-21 01:11:49 +00:00
{
const ESM::MagicEffect * magicEffect = MWBase::Environment::get().getWorld()->getStore().get<ESM::MagicEffect>().find(effectIt->first.mId);
if (magicEffect->mData.mFlags & ESM::MagicEffect::Harmful)
2014-11-02 14:40:08 +00:00
effects.remove((effectIt++)->first);
else
++effectIt;
2014-08-21 01:11:49 +00:00
}
}
mCorprusSpells.erase(corprusIt);
2014-08-21 01:11:49 +00:00
}
if (iter!=mSpells.end())
{
2014-08-21 01:11:49 +00:00
mSpells.erase (iter);
mSpellsChanged = true;
}
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
{
if (mSpellsChanged) {
rebuildEffects();
mSpellsChanged = false;
2014-08-21 01:11:49 +00:00
}
return mEffects;
2012-04-11 16:29:36 +00:00
}
void Spells::clear()
{
2012-04-11 17:40:42 +00:00
mSpells.clear();
mSpellsChanged = true;
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;
}
bool Spells::isSpellActive(const std::string &id) const
{
TContainer::const_iterator found = mSpells.find(getSpell(id));
if (found != mSpells.end())
{
const ESM::Spell *spell = found->first;
return (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);
}
return false;
}
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 = 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 = 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 = iter->first;
if (spell->mData.mType == ESM::Spell::ST_Disease)
{
mSpells.erase(iter++);
mSpellsChanged = true;
}
else
++iter;
}
}
void Spells::purgeBlightDisease()
{
for (TContainer::iterator iter = mSpells.begin(); iter!=mSpells.end();)
{
const ESM::Spell *spell = iter->first;
2014-08-19 20:13:37 +00:00
if (spell->mData.mType == ESM::Spell::ST_Blight && !hasCorprusEffect(spell))
{
mSpells.erase(iter++);
mSpellsChanged = true;
}
else
++iter;
}
}
void Spells::purgeCorprusDisease()
{
for (TContainer::iterator iter = mSpells.begin(); iter!=mSpells.end();)
{
const ESM::Spell *spell = iter->first;
2014-08-19 20:13:37 +00:00
if (hasCorprusEffect(spell))
{
mSpells.erase(iter++);
mSpellsChanged = true;
}
else
++iter;
}
}
void Spells::purgeCurses()
{
for (TContainer::iterator iter = mSpells.begin(); iter!=mSpells.end();)
{
const ESM::Spell *spell = iter->first;
if (spell->mData.mType == ESM::Spell::ST_Curse)
{
mSpells.erase(iter++);
mSpellsChanged = true;
}
else
++iter;
}
}
void Spells::removeEffects(const std::string &id)
{
if (isSpellActive(id))
{
for (TContainer::iterator spell = mSpells.begin(); spell != mSpells.end(); ++spell)
{
if (spell->first == getSpell(id))
{
for (long unsigned int i = 0; i != spell->first->mEffects.mList.size(); i++)
{
spell->second.mPurgedEffects.insert(i);
}
}
}
mSpellsChanged = true;
}
}
void Spells::visitEffectSources(EffectSourceVisitor &visitor) const
{
if (mSpellsChanged) {
rebuildEffects();
mSpellsChanged = false;
}
for (std::map<SpellKey, MagicEffects>::const_iterator it = mSourcedEffects.begin();
it != mSourcedEffects.end(); ++it)
{
const ESM::Spell * spell = it->first;
for (MagicEffects::Collection::const_iterator effectIt = it->second.begin();
effectIt != it->second.end(); ++effectIt)
{
visitor.visit(effectIt->first, spell->mName, spell->mId, -1, effectIt->second.getMagnitude());
}
}
}
2014-05-12 19:04:02 +00:00
void Spells::worsenCorprus(const ESM::Spell* spell)
{
mCorprusSpells[spell].mNextWorsening = MWBase::Environment::get().getWorld()->getTimeStamp() + CorprusStats::sWorseningPeriod;
mCorprusSpells[spell].mWorsenings++;
2014-08-21 01:11:49 +00:00
// update worsened effects
mPermanentSpellEffects[spell] = MagicEffects();
2014-08-21 01:11:49 +00:00
int i=0;
for (std::vector<ESM::ENAMstruct>::const_iterator effectIt = spell->mEffects.mList.begin(); effectIt != spell->mEffects.mList.end(); ++effectIt, ++i)
{
const ESM::MagicEffect * magicEffect = MWBase::Environment::get().getWorld()->getStore().get<ESM::MagicEffect>().find(effectIt->mEffectID);
if ((effectIt->mEffectID != ESM::MagicEffect::Corprus) && (magicEffect->mData.mFlags & ESM::MagicEffect::UncappedDamage)) // APPLIED_ONCE
{
float random = 1.f;
if (mSpells[spell].mEffectRands.find(i) != mSpells[spell].mEffectRands.end())
random = mSpells[spell].mEffectRands.at(i);
2014-08-21 01:11:49 +00:00
float magnitude = effectIt->mMagnMin + (effectIt->mMagnMax - effectIt->mMagnMin) * random;
magnitude *= std::max(1, mCorprusSpells[spell].mWorsenings);
mPermanentSpellEffects[spell].add(MWMechanics::EffectKey(*effectIt), MWMechanics::EffectParam(magnitude));
mSpellsChanged = true;
2014-08-21 01:11:49 +00:00
}
}
}
bool Spells::hasCorprusEffect(const ESM::Spell *spell)
{
for (std::vector<ESM::ENAMstruct>::const_iterator effectIt = spell->mEffects.mList.begin(); effectIt != spell->mEffects.mList.end(); ++effectIt)
{
if (effectIt->mEffectID == ESM::MagicEffect::Corprus)
{
return true;
}
}
return false;
}
const std::map<Spells::SpellKey, Spells::CorprusStats> &Spells::getCorprusSpells() const
2014-08-20 10:40:38 +00:00
{
return mCorprusSpells;
}
void Spells::purgeEffect(int effectId)
{
for (TContainer::iterator spellIt = mSpells.begin(); spellIt != mSpells.end(); ++spellIt)
{
int i = 0;
for (std::vector<ESM::ENAMstruct>::const_iterator effectIt = spellIt->first->mEffects.mList.begin(); effectIt != spellIt->first->mEffects.mList.end(); ++effectIt)
{
if (effectIt->mEffectID == effectId)
{
spellIt->second.mPurgedEffects.insert(i);
mSpellsChanged = true;
}
++i;
}
}
}
void Spells::purgeEffect(int effectId, const std::string & sourceId)
{
const ESM::Spell * spell = MWBase::Environment::get().getWorld()->getStore().get<ESM::Spell>().find(sourceId);
TContainer::iterator spellIt = mSpells.find(spell);
if (spellIt == mSpells.end())
return;
int i = 0;
for (std::vector<ESM::ENAMstruct>::const_iterator effectIt = spellIt->first->mEffects.mList.begin(); effectIt != spellIt->first->mEffects.mList.end(); ++effectIt)
{
if (effectIt->mEffectID == effectId)
{
spellIt->second.mPurgedEffects.insert(i);
mSpellsChanged = true;
}
++i;
}
}
bool Spells::canUsePower(const ESM::Spell* spell) const
2014-05-12 19:04:02 +00:00
{
std::map<SpellKey, MWWorld::TimeStamp>::const_iterator it = mUsedPowers.find(spell);
2014-05-12 19:04:02 +00:00
if (it == mUsedPowers.end() || it->second + 24 <= MWBase::Environment::get().getWorld()->getTimeStamp())
return true;
else
return false;
}
void Spells::usePower(const ESM::Spell* spell)
2014-05-12 19:04:02 +00:00
{
mUsedPowers[spell] = MWBase::Environment::get().getWorld()->getTimeStamp();
2014-05-12 19:04:02 +00:00
}
void Spells::readState(const ESM::SpellState &state)
{
for (ESM::SpellState::TContainer::const_iterator it = state.mSpells.begin(); it != state.mSpells.end(); ++it)
2014-05-12 19:04:02 +00:00
{
// Discard spells that are no longer available due to changed content files
const ESM::Spell* spell = MWBase::Environment::get().getWorld()->getStore().get<ESM::Spell>().search(it->first);
if (spell)
2014-05-12 19:04:02 +00:00
{
mSpells[spell].mEffectRands = it->second.mEffectRands;
mSpells[spell].mPurgedEffects = it->second.mPurgedEffects;
if (it->first == state.mSelectedSpell)
mSelectedSpell = it->first;
2014-05-12 19:04:02 +00:00
}
}
for (std::map<std::string, ESM::TimeStamp>::const_iterator it = state.mUsedPowers.begin(); it != state.mUsedPowers.end(); ++it)
{
const ESM::Spell* spell = MWBase::Environment::get().getWorld()->getStore().get<ESM::Spell>().search(it->first);
if (!spell)
continue;
mUsedPowers[spell] = MWWorld::TimeStamp(it->second);
}
2014-08-21 01:11:49 +00:00
for (std::map<std::string, std::vector<ESM::SpellState::PermanentSpellEffectInfo> >::const_iterator it =
state.mPermanentSpellEffects.begin(); it != state.mPermanentSpellEffects.end(); ++it)
{
const ESM::Spell * spell = MWBase::Environment::get().getWorld()->getStore().get<ESM::Spell>().search(it->first);
if (!spell)
continue;
mPermanentSpellEffects[spell] = MagicEffects();
2014-08-21 01:11:49 +00:00
for (std::vector<ESM::SpellState::PermanentSpellEffectInfo>::const_iterator effectIt = it->second.begin(); effectIt != it->second.end(); ++effectIt)
{
mPermanentSpellEffects[spell].add(EffectKey(effectIt->mId, effectIt->mArg), effectIt->mMagnitude);
2014-08-21 01:11:49 +00:00
}
}
mCorprusSpells.clear();
for (std::map<std::string, ESM::SpellState::CorprusStats>::const_iterator it = state.mCorprusSpells.begin(); it != state.mCorprusSpells.end(); ++it)
{
const ESM::Spell* spell = MWBase::Environment::get().getWorld()->getStore().get<ESM::Spell>().search(it->first);
if (!spell) // Discard unavailable corprus spells
continue;
mCorprusSpells[spell].mWorsenings = state.mCorprusSpells.at(it->first).mWorsenings;
mCorprusSpells[spell].mNextWorsening = MWWorld::TimeStamp(state.mCorprusSpells.at(it->first).mNextWorsening);
}
mSpellsChanged = true;
2014-05-12 19:04:02 +00:00
}
void Spells::writeState(ESM::SpellState &state) const
{
for (TContainer::const_iterator it = mSpells.begin(); it != mSpells.end(); ++it)
{
ESM::SpellState::SpellParams params;
params.mEffectRands = it->second.mEffectRands;
params.mPurgedEffects = it->second.mPurgedEffects;
state.mSpells.insert(std::make_pair(it->first->mId, params));
}
2014-05-12 19:04:02 +00:00
state.mSelectedSpell = mSelectedSpell;
for (std::map<SpellKey, MWWorld::TimeStamp>::const_iterator it = mUsedPowers.begin(); it != mUsedPowers.end(); ++it)
state.mUsedPowers[it->first->mId] = it->second.toEsm();
for (std::map<SpellKey, MagicEffects>::const_iterator it = mPermanentSpellEffects.begin(); it != mPermanentSpellEffects.end(); ++it)
2014-08-21 01:11:49 +00:00
{
std::vector<ESM::SpellState::PermanentSpellEffectInfo> effectList;
for (MagicEffects::Collection::const_iterator effectIt = it->second.begin(); effectIt != it->second.end(); ++effectIt)
{
ESM::SpellState::PermanentSpellEffectInfo info;
info.mId = effectIt->first.mId;
info.mArg = effectIt->first.mArg;
info.mMagnitude = effectIt->second.getModifier();
effectList.push_back(info);
}
state.mPermanentSpellEffects[it->first->mId] = effectList;
2014-08-21 01:11:49 +00:00
}
for (std::map<SpellKey, CorprusStats>::const_iterator it = mCorprusSpells.begin(); it != mCorprusSpells.end(); ++it)
{
state.mCorprusSpells[it->first->mId].mWorsenings = mCorprusSpells.at(it->first).mWorsenings;
state.mCorprusSpells[it->first->mId].mNextWorsening = mCorprusSpells.at(it->first).mNextWorsening.toEsm();
}
2014-05-12 19:04:02 +00:00
}
2012-04-11 16:29:36 +00:00
}