#include "magiceffects.hpp" #include #include #include namespace MWMechanics { EffectKey::EffectKey() : mId (0), mArg (-1) {} EffectKey::EffectKey (const ESM::ENAMstruct& effect) { mId = effect.mEffectID; mArg = -1; if (effect.mSkill!=-1) mArg = effect.mSkill; if (effect.mAttribute!=-1) { if (mArg!=-1) throw std::runtime_error ( "magic effect can't have both a skill and an attribute argument"); mArg = effect.mAttribute; } } bool operator< (const EffectKey& left, const EffectKey& right) { if (left.mIdright.mId) return false; return left.mArgsecond += param; } } void MagicEffects::add (const ESM::EffectList& list, float magnitude) { for (std::vector::const_iterator iter (list.mList.begin()); iter!=list.mList.end(); ++iter) { EffectParam param; if (iter->mMagnMin>=iter->mMagnMax) param.mMagnitude = iter->mMagnMin; else { if (magnitude==-1) magnitude = static_cast (std::rand()) / RAND_MAX; param.mMagnitude = static_cast ( (iter->mMagnMax-iter->mMagnMin+1)*magnitude + iter->mMagnMin); } add (*iter, param); } } MagicEffects& MagicEffects::operator+= (const MagicEffects& effects) { if (this==&effects) { MagicEffects temp (effects); *this += temp; return *this; } for (Collection::const_iterator iter (effects.begin()); iter!=effects.end(); ++iter) { Collection::iterator result = mCollection.find (iter->first); if (result!=mCollection.end()) result->second += iter->second; else mCollection.insert (*iter); } return *this; } EffectParam MagicEffects::get (const EffectKey& key) const { Collection::const_iterator iter = mCollection.find (key); if (iter==mCollection.end()) { return EffectParam(); } else { return iter->second; } } MagicEffects MagicEffects::diff (const MagicEffects& prev, const MagicEffects& now) { MagicEffects result; // adding/changing for (Collection::const_iterator iter (now.begin()); iter!=now.end(); ++iter) { Collection::const_iterator other = prev.mCollection.find (iter->first); if (other==prev.end()) { // adding result.add (iter->first, iter->second); } else { // changing result.add (iter->first, iter->second - other->second); } } // removing for (Collection::const_iterator iter (prev.begin()); iter!=prev.end(); ++iter) { Collection::const_iterator other = now.mCollection.find (iter->first); if (other==prev.end()) { result.add (iter->first, EffectParam() - iter->second); } } return result; } }