From 1f078e246ff03587e3ead220cac17bf343fb3328 Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Sat, 19 Mar 2011 11:43:50 +0100 Subject: Some renamings to try to make things more readable AT_TY -> StackableType TY_ST -> Stackable TY_NST -> NonStackable TY_NSTB -> NonStackableBonus AME_TY -> ModifierEffectType AME_MULT -> Multiplicative AME_ADD -> Additive Got rid of related documentation, which is now stating the obvious. Also renamed many related variables. Reviewed-by: Freeyorp --- src/defines.h | 33 +++++---- src/game-server/attribute.cpp | 135 +++++++++++++++++++++-------------- src/game-server/attribute.h | 32 ++++++--- src/game-server/attributemanager.cpp | 56 +++++++-------- 4 files changed, 146 insertions(+), 110 deletions(-) (limited to 'src') diff --git a/src/defines.h b/src/defines.h index 9c3829c0..c14aca50 100644 --- a/src/defines.h +++ b/src/defines.h @@ -173,36 +173,35 @@ enum }; /** - * Attribute types. Can be one of stackable, non stackable, - * or non stackable bonus. + * Stackable types. * @todo non-stackable malus layers */ -enum AT_TY +enum StackableType { - TY_ST, - TY_NST, - TY_NSTB, - TY_NONE // Should only be used on types - // that have not yet been properly defined. + Stackable, + NonStackable, + NonStackableBonus }; /** * Attribute augmentation methods. - * Can be additive or multiplicative. */ -enum AME_TY +enum ModifierEffectType { - AME_MULT, - AME_ADD + Multiplicative, + Additive }; - struct AttributeInfoType { - AT_TY sType; - AME_TY eType; - AttributeInfoType(AT_TY s, AME_TY e) : sType(s), eType(e) - {} + StackableType stackableType; + ModifierEffectType effectType; + + AttributeInfoType(StackableType s, + ModifierEffectType effect) : + stackableType(s), + effectType(effect) + {} }; #endif // DEFINES_H diff --git a/src/game-server/attribute.cpp b/src/game-server/attribute.cpp index e5ca1b64..22c6e638 100644 --- a/src/game-server/attribute.cpp +++ b/src/game-server/attribute.cpp @@ -23,12 +23,20 @@ #include "utils/logger.h" #include -AttributeModifiersEffect::AttributeModifiersEffect(AT_TY sType, AME_TY eType) - : mMod(eType == AME_MULT ? 1 : 0), mSType(sType), mEType(eType) +AttributeModifiersEffect::AttributeModifiersEffect(StackableType stackableType, + ModifierEffectType effectType) : + mMod(effectType == Multiplicative ? 1 : 0), + mStackableType(stackableType), + mEffectType(effectType) { - assert(eType == AME_MULT || eType == AME_ADD); - assert(sType == TY_ST || sType == TY_NST || sType == TY_NSTB); - LOG_DEBUG("Layer created with eType " << eType << " and sType " << sType << "."); + assert(effectType == Multiplicative + || effectType == Additive); + assert(stackableType == Stackable + || stackableType == NonStackable + || stackableType == NonStackableBonus); + + LOG_DEBUG("Layer created with effectType " << effectType + << " and stackableType " << stackableType << "."); } AttributeModifiersEffect::~AttributeModifiersEffect() @@ -38,17 +46,20 @@ AttributeModifiersEffect::~AttributeModifiersEffect() LOG_WARN("DELETION of attribute effect!"); } -bool AttributeModifiersEffect::add(unsigned short duration, double value, double prevLayerValue, int level) +bool AttributeModifiersEffect::add(unsigned short duration, + double value, + double prevLayerValue, + int level) { LOG_DEBUG("Adding modifier with value " << value << " with a previous layer value of " << prevLayerValue << ". " "Current mod at this layer: " << mMod << "."); bool ret = false; mStates.push_back(new AttributeModifierState(duration, value, level)); - switch (mSType) { - case TY_ST: - switch (mEType) { - case AME_ADD: + switch (mStackableType) { + case Stackable: + switch (mEffectType) { + case Additive: if (value) { ret = true; @@ -56,7 +67,7 @@ bool AttributeModifiersEffect::add(unsigned short duration, double value, double mCacheVal = prevLayerValue + mMod; } break; - case AME_MULT: + case Multiplicative: if (value != 1) { ret = true; @@ -66,14 +77,14 @@ bool AttributeModifiersEffect::add(unsigned short duration, double value, double break; default: LOG_FATAL("Attribute modifiers effect: unhandled type '" - << mEType << "' as a stackable!"); + << mEffectType << "' as a stackable!"); assert(0); break; } break; - case TY_NST: - switch (mEType) { - case AME_ADD: + case NonStackable: + switch (mEffectType) { + case Additive: if (value > mMod) { ret = true; @@ -84,32 +95,32 @@ bool AttributeModifiersEffect::add(unsigned short duration, double value, double break; default: LOG_FATAL("Attribute modifiers effect: unhandled type '" - << mEType << "' as a non-stackable!"); + << mEffectType << "' as a non-stackable!"); assert(0); } // A multiplicative type would also be nonsensical break; - case TY_NSTB: - switch (mEType) { - case AME_ADD: - case AME_MULT: + case NonStackableBonus: + switch (mEffectType) { + case Additive: + case Multiplicative: if (value > mMod) { ret = true; mMod = value; - mCacheVal = mEType == AME_ADD ? prevLayerValue + mMod + mCacheVal = mEffectType == Additive ? prevLayerValue + mMod : prevLayerValue * mMod; } break; default: LOG_FATAL("Attribute modifiers effect: unhandled type '" - << mEType << "' as a non-stackable bonus!"); + << mEffectType << "' as a non-stackable bonus!"); assert(0); } break; default: - LOG_FATAL("Attribute modifiers effect: unknown modifier type '" - << mSType << "'!"); + LOG_FATAL("Attribute modifiers effect: unknown stackable type '" + << mStackableType << "'!"); assert(0); } return ret; @@ -117,9 +128,13 @@ bool AttributeModifiersEffect::add(unsigned short duration, double value, double bool durationCompare(const AttributeModifierState *lhs, const AttributeModifierState *rhs) -{ return lhs->mDuration < rhs->mDuration; } +{ + return lhs->mDuration < rhs->mDuration; +} -bool AttributeModifiersEffect::remove(double value, unsigned int id, bool fullCheck) { +bool AttributeModifiersEffect::remove(double value, unsigned int id, + bool fullCheck) +{ /* We need to find and check this entry exists, and erase the entry from the list too. */ if (!fullCheck) @@ -142,7 +157,7 @@ bool AttributeModifiersEffect::remove(double value, unsigned int id, bool fullCh mStates.erase(it++); /* If this is stackable, we need to update for every modifier affected */ - if (mSType == TY_ST) + if (mStackableType == Stackable) updateMod(); ret = true; @@ -154,18 +169,20 @@ bool AttributeModifiersEffect::remove(double value, unsigned int id, bool fullCh * from scratch. This is done at the end after modifications have been * made as necessary. */ - if (ret && mSType != TY_ST) + if (ret && mStackableType != Stackable) updateMod(); return ret; } void AttributeModifiersEffect::updateMod(double value) { - if (mSType == TY_ST) + if (mStackableType == Stackable) { - if (mEType == AME_ADD) + if (mEffectType == Additive) + { mMod -= value; - else if (mEType == AME_MULT) + } + else if (mEffectType == Multiplicative) { if (value) mMod /= value; @@ -181,9 +198,9 @@ void AttributeModifiersEffect::updateMod(double value) } } else LOG_ERROR("Attribute modifiers effect: unhandled type '" - << mEType << "' as a stackable in cache update!"); + << mEffectType << "' as a stackable in cache update!"); } - else if (mSType == TY_NST || mSType == TY_NSTB) + else if (mStackableType == NonStackable || mStackableType == NonStackableBonus) { if (mMod == value) { @@ -197,38 +214,43 @@ void AttributeModifiersEffect::updateMod(double value) mMod = (*it)->mValue; } } - else LOG_ERROR("Attribute modifiers effect: unknown modifier type '" - << mSType << "' in cache update!"); + else + { + LOG_ERROR("Attribute modifiers effect: unknown stackable type '" + << mStackableType << "' in cache update!"); + } } bool AttributeModifiersEffect::recalculateModifiedValue(double newPrevLayerValue) - { +{ double oldValue = mCacheVal; - switch (mEType) { - case AME_ADD: - switch (mSType) { - case TY_ST: - case TY_NSTB: + switch (mEffectType) { + case Additive: + switch (mStackableType) { + case Stackable: + case NonStackableBonus: mCacheVal = newPrevLayerValue + mMod; break; - case TY_NST: + case NonStackable: mCacheVal = newPrevLayerValue < mMod ? mMod : newPrevLayerValue; break; default: - LOG_FATAL("Unknown stack type '" << mEType << "'!"); + LOG_FATAL("Unknown effect type '" << mEffectType << "'!"); assert(0); } break; - case AME_MULT: - mCacheVal = mSType == TY_ST ? newPrevLayerValue * mMod : newPrevLayerValue * mMod; + case Multiplicative: + mCacheVal = mStackableType == Stackable ? newPrevLayerValue * mMod : newPrevLayerValue * mMod; break; default: - LOG_FATAL("Unknown effect type '" << mEType << "'!"); + LOG_FATAL("Unknown effect type '" << mEffectType << "'!"); assert(0); } return oldValue != mCacheVal; } -bool Attribute::add(unsigned short duration, double value, unsigned int layer, int level) + +bool Attribute::add(unsigned short duration, double value, + unsigned int layer, int level) { assert(mMods.size() > layer); LOG_DEBUG("Adding modifier to attribute with duration " << duration << @@ -256,7 +278,8 @@ bool Attribute::add(unsigned short duration, double value, unsigned int layer, i return false; } -bool Attribute::remove(double value, unsigned int layer, int lvl, bool fullcheck) +bool Attribute::remove(double value, unsigned int layer, + int lvl, bool fullcheck) { assert(mMods.size() > layer); if (mMods.at(layer)->remove(value, lvl, fullcheck)) @@ -295,9 +318,10 @@ Attribute::Attribute(const std::vector &type) LOG_DEBUG("Construction of new attribute with '" << type.size() << "' layers."); for (unsigned int i = 0; i < type.size(); ++i) { - LOG_DEBUG("Adding layer with stack type " << type[i].sType << " and effect type " << type[i].eType << "."); - mMods.push_back(new AttributeModifiersEffect(type[i].sType, - type[i].eType)); + LOG_DEBUG("Adding layer with stackable type " << type[i].stackableType + << " and effect type " << type[i].effectType << "."); + mMods.push_back(new AttributeModifiersEffect(type[i].stackableType, + type[i].effectType)); LOG_DEBUG("Layer added."); } } @@ -335,24 +359,27 @@ bool Attribute::tick() void Attribute::clearMods() { for (std::vector::iterator it = mMods.begin(), - it_end = mMods.end(); it != it_end; ++it) + it_end = mMods.end(); it != it_end; ++it) (*it)->clearMods(mBase); } -void Attribute::setBase(double base) { +void Attribute::setBase(double base) +{ LOG_DEBUG("Setting base attribute from " << mBase << " to " << base << "."); double prev = mBase = base; std::vector::iterator it = mMods.begin(); while (it != mMods.end()) + { if ((*it)->recalculateModifiedValue(prev)) prev = (*it++)->getCachedModifiedValue(); else break; + } } void AttributeModifiersEffect::clearMods(double baseValue) { mStates.clear(); mCacheVal = baseValue; - mMod = mEType == AME_ADD ? 0 : 1; + mMod = mEffectType == Additive ? 0 : 1; } diff --git a/src/game-server/attribute.h b/src/game-server/attribute.h index c2b8bcfb..85254437 100644 --- a/src/game-server/attribute.h +++ b/src/game-server/attribute.h @@ -28,11 +28,19 @@ class AttributeModifierState { public: - AttributeModifierState(unsigned short duration, double value, + AttributeModifierState(unsigned short duration, + double value, unsigned int id) - : mDuration(duration), mValue(value), mId(id) {} - ~AttributeModifierState() {} + : mDuration(duration) + , mValue(value) + , mId(id) + {} + + ~AttributeModifierState() + {} + bool tick() { return mDuration ? !--mDuration : false; } + private: /** Number of ticks (0 means permanent, e.g. equipment). */ unsigned short mDuration; @@ -48,10 +56,13 @@ class AttributeModifierState friend class AttributeModifiersEffect; }; -class AttributeModifiersEffect { +class AttributeModifiersEffect +{ public: - AttributeModifiersEffect(AT_TY sType, AME_TY eType); + AttributeModifiersEffect(StackableType stackableType, + ModifierEffectType effectType); ~AttributeModifiersEffect(); + /** * Recalculates the value for this level. * @returns True if the value changed, false if it did not change. @@ -102,7 +113,7 @@ class AttributeModifiersEffect { private: /** List of all modifications present at this level */ - std::list< AttributeModifierState * > mStates; + std::list mStates; /** * Stores the value that results from mStates. This takes into * account all previous layers. @@ -113,8 +124,8 @@ class AttributeModifiersEffect { * 0 for additive modifiers and 1 for multiplicative modifiers. */ double mMod; - const AT_TY mSType; - const AME_TY mEType; + const StackableType mStackableType; + const ModifierEffectType mEffectType; }; class Attribute @@ -132,7 +143,7 @@ class Attribute { return mMods.empty() ? mBase : (*mMods.rbegin())->getCachedModifiedValue(); } - /** + /* * add() and remove() are the standard functions used to add and * remove modifiers while keeping track of the modifier state. */ @@ -147,7 +158,6 @@ class Attribute * @param id Used to identify this effect. * @return Whether the modified attribute value was changed. */ - bool add(unsigned short duration, double value, unsigned int layer, int id = 0); /** @@ -178,7 +188,7 @@ class Attribute private: double mBase; - std::vector< AttributeModifiersEffect * > mMods; + std::vector mMods; }; #endif // ATTRIBUTE_H diff --git a/src/game-server/attributemanager.cpp b/src/game-server/attributemanager.cpp index 50cced31..8f7d32d1 100644 --- a/src/game-server/attributemanager.cpp +++ b/src/game-server/attributemanager.cpp @@ -78,54 +78,54 @@ void AttributeManager::reload() { if (xmlStrEqual(subnode->name, BAD_CAST "modifier")) { - std::string sType = utils::toUpper( - XML::getProperty(subnode, "stacktype", - std::string())); - std::string eType = utils::toUpper( - XML::getProperty(subnode, "modtype", - std::string())); + std::string stackableTypeString = utils::toUpper( + XML::getProperty(subnode, "stacktype", + std::string())); + std::string effectTypeString = utils::toUpper( + XML::getProperty(subnode, "modtype", + std::string())); std::string tag = utils::toUpper( XML::getProperty(subnode, "tag", std::string())); - AT_TY pSType; - AME_TY pEType; - if (!sType.empty()) + StackableType stackableType; + ModifierEffectType effectType; + if (!stackableTypeString.empty()) { - if (!eType.empty()) + if (!effectTypeString.empty()) { bool fail = false; - if (sType == "STACKABLE") - pSType = TY_ST; - else if (sType == "NON STACKABLE") - pSType = TY_NST; - else if (sType == "NON STACKABLE BONUS") - pSType = TY_NSTB; + if (stackableTypeString == "STACKABLE") + stackableType = Stackable; + else if (stackableTypeString == "NON STACKABLE") + stackableType = NonStackable; + else if (stackableTypeString == "NON STACKABLE BONUS") + stackableType = NonStackableBonus; else { LOG_WARN("Attribute manager: attribute '" << id << "' has unknown stack type '" - << sType << "', skipping modifier!"); + << stackableTypeString << "', skipping modifier!"); fail = true; } if (!fail) { - if (eType == "ADDITIVE") - pEType = AME_ADD; - else if (eType == "MULTIPLICATIVE") - pEType = AME_MULT; + if (effectTypeString == "ADDITIVE") + effectType = Additive; + else if (effectTypeString == "MULTIPLICATIVE") + effectType = Multiplicative; else { LOG_WARN( "Attribute manager: attribute '" << id << "' has unknown modification type '" - << sType << "', skipping modifier!"); + << stackableTypeString << "', skipping modifier!"); fail = true; } if (!fail) { mAttributeMap[id].second.push_back( - AttributeInfoType(pSType, pEType)); + AttributeInfoType(stackableType, effectType)); std::string tag = XML::getProperty( subnode, "tag", std::string()); @@ -184,9 +184,9 @@ void AttributeManager::reload() } LOG_DEBUG("attribute map:"); - LOG_DEBUG("TY_ST is " << TY_ST << ", TY_ NST is " << TY_NST - << ", TY_NSTB is " << TY_NSTB << "."); - LOG_DEBUG("AME_ADD is " << AME_ADD << ", AME_MULT is " << AME_MULT << "."); + LOG_DEBUG("Stackable is " << Stackable << ", NonStackable is " << NonStackable + << ", NonStackableBonus is " << NonStackableBonus << "."); + LOG_DEBUG("Additive is " << Additive << ", Multiplicative is " << Multiplicative << "."); const std::string *tag; unsigned int count = 0; for (AttributeMap::const_iterator i = mAttributeMap.begin(); @@ -200,8 +200,8 @@ void AttributeManager::reload() { tag = getTagFromInfo(i->first, lCount); std::string end = tag ? "tag of '" + (*tag) + "'." : "no tag."; - LOG_DEBUG(" sType: " << j->sType << ", eType: " << j->eType << ", " - "and " << end); + LOG_DEBUG(" stackableType: " << j->stackableType + << ", effectType: " << j->effectType << ", and " << end); ++lCount; ++count; } -- cgit v1.2.3-60-g2f50