Files
Telvanni 4Life 9159788c9b Address reviewer feedback and ran clang-format on modified files:
- Changed reverse lookup map constructor for gmsts, names, and ESM::MagicEffectIds to be more generic.
- Changed refIdToIndex to use map instead of array traversal.
- Removed redundant check from refIdToGmstString.
- Removed sMagicEffectNames and associated map. Removed dead code from loadmgef.cpp/hpp.
- Changed return types of magic effect bindings to RefId.
- Reverted ESMStore search to find.
- Changed std::map to std::unordered_map for faster lookups in resistance and weakness functions.
- Changed loadmgef To methods to pass RefId by value.
- Removed StringRefId alias MagicEffectId.
- Changed all effectId arguments in PR to pass-by-value.
- Removed ESM::MagicEffect::refIdToName function.
- Changed effect key type from long to ESM::RefId in OpGetEffect of mwscript.
- Removed superfluous MWWorld::Store<ESM::GameSettings> argument to MagicEffect store's setUp method.
- Removed transformation code from decompose for ENAM and IRDT structs.
- Changed resistance and weakness maps to initialization to use initialization instead of assignment.
- Changed sGmstEffectIds to constexpr array since all parameters are known at compile time.
- Changed getBoundItemsMap to use an unordered_map and have string_view values.
- MagicEffect store definition removed (uses standard template class TypedDynamicStore).
- Fixed bug in calculating sun damage that would have made vampires daywalkers in Mournhold.
- Removed polymorphic EffectKey constructor and added overload to getOrDefault.
- Placed ESM spec IRDT and ENAM structs in anonymous namespace.
- Added exception if attempting to serialize ENAM subrecords with out of bounds index.
- Added include format statements to satisfy Ubuntu CI linker.
2026-01-08 07:44:06 -05:00

120 lines
2.9 KiB
C++

#ifndef GAME_MWMECHANICS_MAGICEFFECTS_H
#define GAME_MWMECHANICS_MAGICEFFECTS_H
#include <map>
#include <optional>
#include <string>
#include <components/esm/refid.hpp>
namespace ESM
{
struct Attribute;
struct ENAMstruct;
struct EffectList;
struct MagicEffect;
struct MagicEffects;
struct Skill;
}
namespace MWMechanics
{
struct EffectKey
{
ESM::RefId mId;
ESM::RefId mArg; // skill or ability
EffectKey();
EffectKey(ESM::RefId id, ESM::RefId arg = {})
: mId(id)
, mArg(arg)
{
}
EffectKey(const ESM::ENAMstruct& effect);
std::string toString() const;
};
bool operator<(const EffectKey& left, const EffectKey& right);
bool operator==(const EffectKey& left, const EffectKey& right);
struct EffectParam
{
private:
// Note usually this would be int, but applying partial resistance might introduce a decimal point.
float mModifier;
int mBase;
public:
/// Get the total magnitude including base and modifier.
float getMagnitude() const;
void setModifier(float mod);
float getModifier() const;
/// Change mBase by \a diff
void modifyBase(int diff);
void setBase(int base);
int getBase() const;
EffectParam();
EffectParam(float magnitude)
: mModifier(magnitude)
, mBase(0)
{
}
EffectParam& operator+=(const EffectParam& param);
EffectParam& operator-=(const EffectParam& param);
};
inline EffectParam operator+(const EffectParam& left, const EffectParam& right)
{
EffectParam param(left);
return param += right;
}
inline EffectParam operator-(const EffectParam& left, const EffectParam& right)
{
EffectParam param(left);
return param -= right;
}
/// \brief Effects currently affecting a NPC or creature
class MagicEffects
{
public:
typedef std::map<EffectKey, EffectParam> Collection;
private:
Collection mCollection;
public:
Collection::const_iterator begin() const { return mCollection.begin(); }
Collection::const_iterator end() const { return mCollection.end(); }
void readState(const ESM::MagicEffects& state);
void writeState(ESM::MagicEffects& state) const;
void add(const EffectKey& key, const EffectParam& param);
void modifyBase(const EffectKey& key, int diff);
EffectParam getOrDefault(const EffectKey& key) const;
EffectParam getOrDefault(ESM::RefId effectId) const;
std::optional<EffectParam> get(const EffectKey& key) const;
///< This function can safely be used for keys that are not present.
};
std::string getMagicEffectString(
const ESM::MagicEffect& effect, const ESM::Attribute* attribute, const ESM::Skill* skill);
}
#endif