mirror of
				https://github.com/vcmi/vcmi.git
				synced 2025-10-31 00:07:39 +02:00 
			
		
		
		
	vcmi: split IBonusBearer
This commit is contained in:
		| @@ -12,6 +12,7 @@ | ||||
| #include <vcmi/FactionMember.h> | ||||
|  | ||||
| #include "../../lib/bonuses/HeroBonus.h" | ||||
| #include "../../lib/bonuses/IBonusBearer.h" | ||||
| #include "../widgets/CWindowWithArtifacts.h" | ||||
| #include "../widgets/CGarrisonInt.h" | ||||
|  | ||||
|   | ||||
| @@ -31,6 +31,7 @@ macro(add_main_lib TARGET_NAME LIBRARY_TYPE) | ||||
| 		${MAIN_LIB_DIR}/bonuses/CBonusProxy.cpp | ||||
| 		${MAIN_LIB_DIR}/bonuses/CBonusSystemNode.cpp | ||||
| 		${MAIN_LIB_DIR}/bonuses/HeroBonus.cpp | ||||
| 		${MAIN_LIB_DIR}/bonuses/IBonusBearer.cpp | ||||
| 		${MAIN_LIB_DIR}/bonuses/Limiters.cpp | ||||
| 		${MAIN_LIB_DIR}/bonuses/Propagators.cpp | ||||
| 		${MAIN_LIB_DIR}/bonuses/Updaters.cpp | ||||
| @@ -311,6 +312,7 @@ macro(add_main_lib TARGET_NAME LIBRARY_TYPE) | ||||
| 		${MAIN_LIB_DIR}/bonuses/CBonusProxy.h | ||||
| 		${MAIN_LIB_DIR}/bonuses/CBonusSystemNode.h | ||||
| 		${MAIN_LIB_DIR}/bonuses/HeroBonus.h | ||||
| 		${MAIN_LIB_DIR}/bonuses/IBonusBearer.h | ||||
| 		${MAIN_LIB_DIR}/bonuses/Limiters.h | ||||
| 		${MAIN_LIB_DIR}/bonuses/Propagators.h | ||||
| 		${MAIN_LIB_DIR}/bonuses/Updaters.h | ||||
|   | ||||
| @@ -13,6 +13,7 @@ | ||||
| #include "VCMI_Lib.h" | ||||
| #include "GameConstants.h" | ||||
| #include "bonuses/HeroBonus.h" | ||||
| #include "bonuses/IBonusBearer.h" | ||||
|  | ||||
| #include <vcmi/Creature.h> | ||||
| #include <vcmi/Faction.h> | ||||
|   | ||||
| @@ -14,6 +14,7 @@ | ||||
| #include <vcmi/spells/Caster.h> | ||||
|  | ||||
| #include "../bonuses/HeroBonus.h" | ||||
| #include "../bonuses/IBonusBearer.h" | ||||
|  | ||||
| #include "IUnitInfo.h" | ||||
| #include "BattleHex.h" | ||||
|   | ||||
| @@ -10,6 +10,7 @@ | ||||
|  | ||||
| #include "StdInc.h" | ||||
| #include "CBonusProxy.h" | ||||
| #include "IBonusBearer.h" | ||||
|  | ||||
| VCMI_LIB_NAMESPACE_BEGIN | ||||
|  | ||||
|   | ||||
| @@ -9,7 +9,7 @@ | ||||
|  */ | ||||
| #pragma once | ||||
|  | ||||
| #include "HeroBonus.h" | ||||
| #include "IBonusBearer.h" | ||||
|  | ||||
| VCMI_LIB_NAMESPACE_BEGIN | ||||
|  | ||||
|   | ||||
| @@ -399,70 +399,6 @@ void BonusList::insert(BonusList::TInternalContainer::iterator position, BonusLi | ||||
| 	changed(); | ||||
| } | ||||
|  | ||||
| int IBonusBearer::valOfBonuses(Bonus::BonusType type, int subtype) const | ||||
| { | ||||
| 	//This part is performance-critical | ||||
| 	std::string cachingStr = "type_" + std::to_string(static_cast<int>(type)) + "_" + std::to_string(subtype); | ||||
|  | ||||
| 	CSelector s = Selector::type()(type); | ||||
| 	if(subtype != -1) | ||||
| 		s = s.And(Selector::subtype()(subtype)); | ||||
|  | ||||
| 	return valOfBonuses(s, cachingStr); | ||||
| } | ||||
|  | ||||
| int IBonusBearer::valOfBonuses(const CSelector &selector, const std::string &cachingStr) const | ||||
| { | ||||
| 	CSelector limit = nullptr; | ||||
| 	TConstBonusListPtr hlp = getAllBonuses(selector, limit, 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; | ||||
| } | ||||
|  | ||||
| bool IBonusBearer::hasBonus(const CSelector &selector, const CSelector &limit, const std::string &cachingStr) const | ||||
| { | ||||
| 	return getBonuses(selector, limit, cachingStr)->size() > 0; | ||||
| } | ||||
|  | ||||
| bool IBonusBearer::hasBonusOfType(Bonus::BonusType type, int subtype) const | ||||
| { | ||||
| 	//This part is performance-ciritcal | ||||
| 	std::string cachingStr = "type_" + std::to_string(static_cast<int>(type)) + "_" + std::to_string(subtype); | ||||
|  | ||||
| 	CSelector s = Selector::type()(type); | ||||
| 	if(subtype != -1) | ||||
| 		s = s.And(Selector::subtype()(subtype)); | ||||
|  | ||||
| 	return hasBonus(s, cachingStr); | ||||
| } | ||||
|  | ||||
| TConstBonusListPtr IBonusBearer::getBonuses(const CSelector &selector, const std::string &cachingStr) const | ||||
| { | ||||
| 	return getAllBonuses(selector, nullptr, nullptr, cachingStr); | ||||
| } | ||||
|  | ||||
| TConstBonusListPtr IBonusBearer::getBonuses(const CSelector &selector, const CSelector &limit, const std::string &cachingStr) const | ||||
| { | ||||
| 	return getAllBonuses(selector, limit, nullptr, cachingStr); | ||||
| } | ||||
|  | ||||
| bool IBonusBearer::hasBonusFrom(Bonus::BonusSource source, ui32 sourceID) const | ||||
| { | ||||
| 	boost::format fmt("source_%did_%d"); | ||||
| 	fmt % static_cast<int>(source) % sourceID; | ||||
|  | ||||
| 	return hasBonus(Selector::source(source,sourceID), fmt.str()); | ||||
| } | ||||
| std::shared_ptr<const Bonus> IBonusBearer::getBonus(const CSelector &selector) const | ||||
| { | ||||
| 	auto bonuses = getAllBonuses(selector, Selector::all); | ||||
| 	return bonuses->getFirst(Selector::all); | ||||
| } | ||||
|  | ||||
| std::string Bonus::Description(std::optional<si32> customValue) const | ||||
| { | ||||
| 	std::ostringstream str; | ||||
|   | ||||
| @@ -562,33 +562,6 @@ public: | ||||
|  | ||||
| DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const BonusList &bonusList); | ||||
|  | ||||
|  | ||||
| class DLL_LINKAGE IBonusBearer | ||||
| { | ||||
| public: | ||||
| 	//new bonusing node interface | ||||
| 	// * selector is predicate that tests if HeroBonus matches our criteria | ||||
| 	// * root is node on which call was made (nullptr will be replaced with this) | ||||
| 	//interface | ||||
| 	IBonusBearer() = default; | ||||
| 	virtual ~IBonusBearer() = default; | ||||
| 	virtual TConstBonusListPtr getAllBonuses(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root = nullptr, const std::string &cachingStr = "") const = 0; | ||||
| 	int valOfBonuses(const CSelector &selector, const std::string &cachingStr = "") const; | ||||
| 	bool hasBonus(const CSelector &selector, const std::string &cachingStr = "") const; | ||||
| 	bool hasBonus(const CSelector &selector, const CSelector &limit, const std::string &cachingStr = "") const; | ||||
| 	TConstBonusListPtr getBonuses(const CSelector &selector, const CSelector &limit, const std::string &cachingStr = "") const; | ||||
| 	TConstBonusListPtr getBonuses(const CSelector &selector, const std::string &cachingStr = "") const; | ||||
|  | ||||
| 	std::shared_ptr<const Bonus> 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(Bonus::BonusType type, int subtype = -1) const; //subtype -> subtype of bonus, if -1 then anyt; | ||||
| 	bool hasBonusOfType(Bonus::BonusType type, int subtype = -1) const;//determines if hero has a bonus of given type (and optionally subtype) | ||||
| 	bool hasBonusFrom(Bonus::BonusSource source, ui32 sourceID) const; | ||||
|  | ||||
| 	virtual int64_t getTreeVersion() const = 0; | ||||
| }; | ||||
|  | ||||
| template<typename T> | ||||
| class CSelectFieldEqual | ||||
| { | ||||
|   | ||||
							
								
								
									
										81
									
								
								lib/bonuses/IBonusBearer.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										81
									
								
								lib/bonuses/IBonusBearer.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,81 @@ | ||||
| /* | ||||
|  * IBonusBearer.cpp, part of VCMI engine | ||||
|  * | ||||
|  * Authors: listed in file AUTHORS in main folder | ||||
|  * | ||||
|  * License: GNU General Public License v2.0 or later | ||||
|  * Full text of license available in license.txt file, in main folder | ||||
|  * | ||||
|  */ | ||||
|  | ||||
| #include "StdInc.h" | ||||
|  | ||||
| #include "CBonusSystemNode.h" | ||||
|  | ||||
| VCMI_LIB_NAMESPACE_BEGIN | ||||
|  | ||||
| int IBonusBearer::valOfBonuses(Bonus::BonusType type, int subtype) const | ||||
| { | ||||
| 	//This part is performance-critical | ||||
| 	std::string cachingStr = "type_" + std::to_string(static_cast<int>(type)) + "_" + std::to_string(subtype); | ||||
|  | ||||
| 	CSelector s = Selector::type()(type); | ||||
| 	if(subtype != -1) | ||||
| 		s = s.And(Selector::subtype()(subtype)); | ||||
|  | ||||
| 	return valOfBonuses(s, cachingStr); | ||||
| } | ||||
|  | ||||
| int IBonusBearer::valOfBonuses(const CSelector &selector, const std::string &cachingStr) const | ||||
| { | ||||
| 	CSelector limit = nullptr; | ||||
| 	TConstBonusListPtr hlp = getAllBonuses(selector, limit, 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; | ||||
| } | ||||
|  | ||||
| bool IBonusBearer::hasBonus(const CSelector &selector, const CSelector &limit, const std::string &cachingStr) const | ||||
| { | ||||
| 	return getBonuses(selector, limit, cachingStr)->size() > 0; | ||||
| } | ||||
|  | ||||
| bool IBonusBearer::hasBonusOfType(Bonus::BonusType type, int subtype) const | ||||
| { | ||||
| 	//This part is performance-ciritcal | ||||
| 	std::string cachingStr = "type_" + std::to_string(static_cast<int>(type)) + "_" + std::to_string(subtype); | ||||
|  | ||||
| 	CSelector s = Selector::type()(type); | ||||
| 	if(subtype != -1) | ||||
| 		s = s.And(Selector::subtype()(subtype)); | ||||
|  | ||||
| 	return hasBonus(s, cachingStr); | ||||
| } | ||||
|  | ||||
| TConstBonusListPtr IBonusBearer::getBonuses(const CSelector &selector, const std::string &cachingStr) const | ||||
| { | ||||
| 	return getAllBonuses(selector, nullptr, nullptr, cachingStr); | ||||
| } | ||||
|  | ||||
| TConstBonusListPtr IBonusBearer::getBonuses(const CSelector &selector, const CSelector &limit, const std::string &cachingStr) const | ||||
| { | ||||
| 	return getAllBonuses(selector, limit, nullptr, cachingStr); | ||||
| } | ||||
|  | ||||
| bool IBonusBearer::hasBonusFrom(Bonus::BonusSource source, ui32 sourceID) const | ||||
| { | ||||
| 	boost::format fmt("source_%did_%d"); | ||||
| 	fmt % static_cast<int>(source) % sourceID; | ||||
|  | ||||
| 	return hasBonus(Selector::source(source,sourceID), fmt.str()); | ||||
| } | ||||
| std::shared_ptr<const Bonus> IBonusBearer::getBonus(const CSelector &selector) const | ||||
| { | ||||
| 	auto bonuses = getAllBonuses(selector, Selector::all); | ||||
| 	return bonuses->getFirst(Selector::all); | ||||
| } | ||||
|  | ||||
| VCMI_LIB_NAMESPACE_END | ||||
							
								
								
									
										43
									
								
								lib/bonuses/IBonusBearer.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								lib/bonuses/IBonusBearer.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,43 @@ | ||||
|  | ||||
| /* | ||||
|  * CBonusSystemNode.h, part of VCMI engine | ||||
|  * | ||||
|  * Authors: listed in file AUTHORS in main folder | ||||
|  * | ||||
|  * License: GNU General Public License v2.0 or later | ||||
|  * Full text of license available in license.txt file, in main folder | ||||
|  * | ||||
|  */ | ||||
| #pragma once | ||||
|  | ||||
| #include "HeroBonus.h" | ||||
|  | ||||
| VCMI_LIB_NAMESPACE_BEGIN | ||||
|  | ||||
| class DLL_LINKAGE IBonusBearer | ||||
| { | ||||
| public: | ||||
| 	//new bonusing node interface | ||||
| 	// * selector is predicate that tests if HeroBonus matches our criteria | ||||
| 	// * root is node on which call was made (nullptr will be replaced with this) | ||||
| 	//interface | ||||
| 	IBonusBearer() = default; | ||||
| 	virtual ~IBonusBearer() = default; | ||||
| 	virtual TConstBonusListPtr getAllBonuses(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root = nullptr, const std::string &cachingStr = "") const = 0; | ||||
| 	int valOfBonuses(const CSelector &selector, const std::string &cachingStr = "") const; | ||||
| 	bool hasBonus(const CSelector &selector, const std::string &cachingStr = "") const; | ||||
| 	bool hasBonus(const CSelector &selector, const CSelector &limit, const std::string &cachingStr = "") const; | ||||
| 	TConstBonusListPtr getBonuses(const CSelector &selector, const CSelector &limit, const std::string &cachingStr = "") const; | ||||
| 	TConstBonusListPtr getBonuses(const CSelector &selector, const std::string &cachingStr = "") const; | ||||
|  | ||||
| 	std::shared_ptr<const Bonus> 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(Bonus::BonusType type, int subtype = -1) const; //subtype -> subtype of bonus, if -1 then anyt; | ||||
| 	bool hasBonusOfType(Bonus::BonusType type, int subtype = -1) const;//determines if hero has a bonus of given type (and optionally subtype) | ||||
| 	bool hasBonusFrom(Bonus::BonusSource source, ui32 sourceID) const; | ||||
|  | ||||
| 	virtual int64_t getTreeVersion() const = 0; | ||||
| }; | ||||
|  | ||||
| VCMI_LIB_NAMESPACE_END | ||||
| @@ -16,6 +16,7 @@ | ||||
| #include "../LuaStack.h" | ||||
| #include "../LuaCallWrapper.h" | ||||
| #include "../../../lib/bonuses/HeroBonus.h" | ||||
| #include "../../../lib/bonuses/IBonusBearer.h" | ||||
|  | ||||
| VCMI_LIB_NAMESPACE_BEGIN | ||||
|  | ||||
|   | ||||
| @@ -12,6 +12,7 @@ | ||||
| #include "BonusSystem.h" | ||||
|  | ||||
| #include "../../../lib/bonuses/HeroBonus.h" | ||||
| #include "../../../lib/bonuses/IBonusBearer.h" | ||||
|  | ||||
| #include "Registry.h" | ||||
|  | ||||
|   | ||||
| @@ -16,6 +16,7 @@ | ||||
| #include "../LuaStack.h" | ||||
| #include "../LuaCallWrapper.h" | ||||
| #include "../../../lib/bonuses/HeroBonus.h" | ||||
| #include "../../../lib/bonuses/IBonusBearer.h" | ||||
|  | ||||
| VCMI_LIB_NAMESPACE_BEGIN | ||||
|  | ||||
|   | ||||
| @@ -11,6 +11,7 @@ | ||||
| #pragma once | ||||
|  | ||||
| #include "../../lib/bonuses/HeroBonus.h" | ||||
| #include "../../lib/bonuses/IBonusBearer.h" | ||||
|  | ||||
|  | ||||
| class BonusBearerMock : public IBonusBearer | ||||
|   | ||||
		Reference in New Issue
	
	Block a user