diff --git a/lib/bonuses/IBonusBearer.cpp b/lib/bonuses/IBonusBearer.cpp index 3444535bc..219d730bd 100644 --- a/lib/bonuses/IBonusBearer.cpp +++ b/lib/bonuses/IBonusBearer.cpp @@ -15,44 +15,21 @@ VCMI_LIB_NAMESPACE_BEGIN -int IBonusBearer::valOfBonuses(BonusType type, std::optional subtype) const -{ - //This part is performance-critical - std::string cachingStr = "type_" + std::to_string(static_cast(type)) + (subtype ? "_" + std::to_string(*subtype) : ""); - - CSelector s = Selector::type()(type); - if(subtype) - s = s.And(Selector::subtype()(*subtype)); - - return valOfBonuses(s, cachingStr); -} - int IBonusBearer::valOfBonuses(const CSelector &selector, const std::string &cachingStr) const { TConstBonusListPtr hlp = getAllBonuses(selector, nullptr, nullptr, cachingStr); return hlp->totalValue(); } + bool IBonusBearer::hasBonus(const CSelector &selector, const std::string &cachingStr) const { //TODO: We don't need to count all bonuses and could break on first matching - return getBonuses(selector, cachingStr)->size() > 0; + return !getBonuses(selector, cachingStr)->empty(); } bool IBonusBearer::hasBonus(const CSelector &selector, const CSelector &limit, const std::string &cachingStr) const { - return getBonuses(selector, limit, cachingStr)->size() > 0; -} - -bool IBonusBearer::hasBonusOfType(BonusType type, std::optional subtype) const -{ - //This part is performance-ciritcal - std::string cachingStr = "type_" + std::to_string(static_cast(type)) + (subtype ? "_" + std::to_string(*subtype) : ""); - - CSelector s = Selector::type()(type); - if(subtype) - s = s.And(Selector::subtype()(*subtype)); - - return hasBonus(s, cachingStr); + return !getBonuses(selector, limit, cachingStr)->empty(); } TConstBonusListPtr IBonusBearer::getBonuses(const CSelector &selector, const std::string &cachingStr) const @@ -65,6 +42,46 @@ TConstBonusListPtr IBonusBearer::getBonuses(const CSelector &selector, const CSe return getAllBonuses(selector, limit, nullptr, cachingStr); } +int IBonusBearer::valOfBonuses(BonusType type) const +{ + //This part is performance-critical + std::string cachingStr = "type_" + std::to_string(static_cast(type)); + + CSelector s = Selector::type()(type); + + return valOfBonuses(s, cachingStr); +} + +bool IBonusBearer::hasBonusOfType(BonusType type) const +{ + //This part is performance-critical + std::string cachingStr = "type_" + std::to_string(static_cast(type)); + + CSelector s = Selector::type()(type); + + return hasBonus(s, cachingStr); +} + +int IBonusBearer::valOfBonuses(BonusType type, int subtype) const +{ + //This part is performance-critical + std::string cachingStr = "type_" + std::to_string(static_cast(type)) + "_" + std::to_string(subtype); + + CSelector s = Selector::typeSubtype(type, subtype); + + return valOfBonuses(s, cachingStr); +} + +bool IBonusBearer::hasBonusOfType(BonusType type, int subtype) const +{ + //This part is performance-critical + std::string cachingStr = "type_" + std::to_string(static_cast(type)) + "_" + std::to_string(subtype); + + CSelector s = Selector::typeSubtype(type, subtype); + + return hasBonus(s, cachingStr); +} + bool IBonusBearer::hasBonusFrom(BonusSource source, ui32 sourceID) const { boost::format fmt("source_%did_%d"); diff --git a/lib/bonuses/IBonusBearer.h b/lib/bonuses/IBonusBearer.h index b70910f04..242c66f83 100644 --- a/lib/bonuses/IBonusBearer.h +++ b/lib/bonuses/IBonusBearer.h @@ -33,8 +33,10 @@ public: std::shared_ptr getBonus(const CSelector &selector) const; //returns any bonus visible on node that matches (or nullptr if none matches) //Optimized interface (with auto-caching) - int valOfBonuses(BonusType type, std::optional subtype = std::nullopt) const; //subtype -> subtype of bonus; - bool hasBonusOfType(BonusType type, std::optional subtype = std::nullopt) const;//determines if hero has a bonus of given type (and optionally subtype) + int valOfBonuses(BonusType type) const; //subtype -> subtype of bonus; + bool hasBonusOfType(BonusType type) const;//determines if hero has a bonus of given type (and optionally subtype) + int valOfBonuses(BonusType type, int subtype) const; //subtype -> subtype of bonus; + bool hasBonusOfType(BonusType type, int subtype) const;//determines if hero has a bonus of given type (and optionally subtype) bool hasBonusFrom(BonusSource source, ui32 sourceID) const; virtual int64_t getTreeVersion() const = 0;