mirror of
				https://github.com/vcmi/vcmi.git
				synced 2025-10-31 00:07:39 +02:00 
			
		
		
		
	Slight simplification of bonus system node class
This commit is contained in:
		| @@ -22,7 +22,6 @@ | ||||
| #include "../../lib/CHeroHandler.h" | ||||
| #include "../../lib/GameSettings.h" | ||||
| #include "../../lib/gameState/CGameState.h" | ||||
| #include "../../lib/bonuses/CBonusSystemNode.h" | ||||
| #include "../../lib/bonuses/Limiters.h" | ||||
| #include "../../lib/bonuses/Updaters.h" | ||||
| #include "../../lib/bonuses/Propagators.h" | ||||
|   | ||||
| @@ -82,7 +82,6 @@ | ||||
| #include "../lib/UnlockGuard.h" | ||||
| #include "../lib/VCMIDirs.h" | ||||
|  | ||||
| #include "../lib/bonuses/CBonusSystemNode.h" | ||||
| #include "../lib/bonuses/Limiters.h" | ||||
| #include "../lib/bonuses/Propagators.h" | ||||
| #include "../lib/bonuses/Updaters.h" | ||||
|   | ||||
| @@ -23,8 +23,6 @@ PlayerState::PlayerState() | ||||
| 	setNodeType(PLAYER); | ||||
| } | ||||
|  | ||||
| PlayerState::PlayerState(PlayerState && other) noexcept = default; | ||||
|  | ||||
| PlayerState::~PlayerState() = default; | ||||
|  | ||||
| std::string PlayerState::nodeName() const | ||||
|   | ||||
| @@ -67,7 +67,6 @@ public: | ||||
| 	TurnTimerInfo turnTimer; | ||||
|  | ||||
| 	PlayerState(); | ||||
| 	PlayerState(PlayerState && other) noexcept; | ||||
| 	~PlayerState(); | ||||
|  | ||||
| 	std::string nodeName() const override; | ||||
| @@ -123,7 +122,6 @@ public: | ||||
| 	std::unique_ptr<boost::multi_array<ui8, 3>> fogOfWarMap; //[z][x][y] true - visible, false - hidden | ||||
|  | ||||
| 	TeamState(); | ||||
| 	TeamState(TeamState && other) noexcept; | ||||
|  | ||||
| 	template <typename Handler> void serialize(Handler &h, const int version) | ||||
| 	{ | ||||
|   | ||||
| @@ -16,7 +16,6 @@ | ||||
| #include "CBonusTypeHandler.h" | ||||
| #include "BattleFieldHandler.h" | ||||
| #include "ObstacleHandler.h" | ||||
| #include "bonuses/CBonusSystemNode.h" | ||||
| #include "bonuses/Limiters.h" | ||||
| #include "bonuses/Propagators.h" | ||||
| #include "bonuses/Updaters.h" | ||||
|   | ||||
| @@ -10,7 +10,6 @@ | ||||
|  | ||||
| #include "StdInc.h" | ||||
| #include "Bonus.h" | ||||
| #include "CBonusSystemNode.h" | ||||
| #include "Limiters.h" | ||||
| #include "Updaters.h" | ||||
| #include "Propagators.h" | ||||
|   | ||||
| @@ -20,16 +20,15 @@ VCMI_LIB_NAMESPACE_BEGIN | ||||
| std::atomic<int64_t> CBonusSystemNode::treeChanged(1); | ||||
| constexpr bool CBonusSystemNode::cachingEnabled = true; | ||||
|  | ||||
| #define FOREACH_PARENT(pname) 	TNodes lparents; getParents(lparents); for(CBonusSystemNode *pname : lparents) | ||||
| #define FOREACH_RED_CHILD(pname) 	TNodes lchildren; getRedChildren(lchildren); for(CBonusSystemNode *pname : lchildren) | ||||
|  | ||||
| std::shared_ptr<Bonus> CBonusSystemNode::getBonusLocalFirst(const CSelector & selector) | ||||
| { | ||||
| 	auto ret = bonuses.getFirst(selector); | ||||
| 	if(ret) | ||||
| 		return ret; | ||||
|  | ||||
| 	FOREACH_PARENT(pname) | ||||
| 	TNodes lparents; | ||||
| 	getParents(lparents); | ||||
| 	for(CBonusSystemNode *pname : lparents) | ||||
| 	{ | ||||
| 		ret = pname->getBonusLocalFirst(selector); | ||||
| 		if (ret) | ||||
| @@ -227,39 +226,6 @@ CBonusSystemNode::CBonusSystemNode(ENodeTypes NodeType): | ||||
| { | ||||
| } | ||||
|  | ||||
| CBonusSystemNode::CBonusSystemNode(CBonusSystemNode && other) noexcept: | ||||
| 	bonuses(std::move(other.bonuses)), | ||||
| 	exportedBonuses(std::move(other.exportedBonuses)), | ||||
| 	nodeType(other.nodeType), | ||||
| 	cachedLast(0), | ||||
| 	isHypotheticNode(other.isHypotheticNode) | ||||
| { | ||||
| 	std::swap(parents, other.parents); | ||||
| 	std::swap(children, other.children); | ||||
|  | ||||
| 	//fixing bonus tree without recalculation | ||||
|  | ||||
| 	if(!isHypothetic()) | ||||
| 	{ | ||||
| 		for(CBonusSystemNode * n : parents) | ||||
| 		{ | ||||
| 			n->children -= &other; | ||||
| 			n->children.push_back(this); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	for(CBonusSystemNode * n : children) | ||||
| 	{ | ||||
| 		n->parents -= &other; | ||||
| 		n->parents.push_back(this); | ||||
| 	} | ||||
|  | ||||
| 	//cache ignored | ||||
|  | ||||
| 	//cachedBonuses | ||||
| 	//cachedRequests | ||||
| } | ||||
|  | ||||
| CBonusSystemNode::~CBonusSystemNode() | ||||
| { | ||||
| 	detachFromAll(); | ||||
| @@ -405,8 +371,10 @@ void CBonusSystemNode::propagateBonus(const std::shared_ptr<Bonus> & b, const CB | ||||
| 		logBonus->trace("#$# %s #propagated to# %s",  propagated->Description(), nodeName()); | ||||
| 	} | ||||
|  | ||||
| 	FOREACH_RED_CHILD(child) | ||||
| 		child->propagateBonus(b, source); | ||||
| 	TNodes lchildren; | ||||
| 	getRedChildren(lchildren); | ||||
| 	for(CBonusSystemNode *pname : lchildren) | ||||
| 		pname->propagateBonus(b, source); | ||||
| } | ||||
|  | ||||
| void CBonusSystemNode::unpropagateBonus(const std::shared_ptr<Bonus> & b) | ||||
| @@ -417,8 +385,10 @@ void CBonusSystemNode::unpropagateBonus(const std::shared_ptr<Bonus> & b) | ||||
| 		logBonus->trace("#$# %s #is no longer propagated to# %s",  b->Description(), nodeName()); | ||||
| 	} | ||||
|  | ||||
| 	FOREACH_RED_CHILD(child) | ||||
| 		child->unpropagateBonus(b); | ||||
| 	TNodes lchildren; | ||||
| 	getRedChildren(lchildren); | ||||
| 	for(CBonusSystemNode *pname : lchildren) | ||||
| 		pname->unpropagateBonus(b); | ||||
| } | ||||
|  | ||||
| void CBonusSystemNode::newChildAttached(CBonusSystemNode & child) | ||||
| @@ -467,9 +437,11 @@ void CBonusSystemNode::deserializationFix() | ||||
|  | ||||
| } | ||||
|  | ||||
| void CBonusSystemNode::getRedParents(TNodes & out) | ||||
| void CBonusSystemNode::getRedParents(TCNodes & out) const | ||||
| { | ||||
| 	FOREACH_PARENT(pname) | ||||
| 	TCNodes lparents; | ||||
| 	getParents(lparents); | ||||
| 	for(const CBonusSystemNode *pname : lparents) | ||||
| 	{ | ||||
| 		if(pname->actsAsBonusSourceOnly()) | ||||
| 		{ | ||||
| @@ -488,7 +460,9 @@ void CBonusSystemNode::getRedParents(TNodes & out) | ||||
|  | ||||
| void CBonusSystemNode::getRedChildren(TNodes &out) | ||||
| { | ||||
| 	FOREACH_PARENT(pname) | ||||
| 	TNodes lparents; | ||||
| 	getParents(lparents); | ||||
| 	for(CBonusSystemNode *pname : lparents) | ||||
| 	{ | ||||
| 		if(!pname->actsAsBonusSourceOnly()) | ||||
| 		{ | ||||
| @@ -512,7 +486,7 @@ void CBonusSystemNode::newRedDescendant(CBonusSystemNode & descendant) | ||||
| 		if(b->propagator) | ||||
| 			descendant.propagateBonus(b, *this); | ||||
| 	} | ||||
| 	TNodes redParents; | ||||
| 	TCNodes redParents; | ||||
| 	getRedAncestors(redParents); //get all red parents recursively | ||||
|  | ||||
| 	for(auto * parent : redParents) | ||||
| @@ -531,7 +505,7 @@ void CBonusSystemNode::removedRedDescendant(CBonusSystemNode & descendant) | ||||
| 		if(b->propagator) | ||||
| 			descendant.unpropagateBonus(b); | ||||
|  | ||||
| 	TNodes redParents; | ||||
| 	TCNodes redParents; | ||||
| 	getRedAncestors(redParents); //get all red parents recursively | ||||
|  | ||||
| 	for(auto * parent : redParents) | ||||
| @@ -542,14 +516,14 @@ void CBonusSystemNode::removedRedDescendant(CBonusSystemNode & descendant) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| void CBonusSystemNode::getRedAncestors(TNodes &out) | ||||
| void CBonusSystemNode::getRedAncestors(TCNodes &out) const | ||||
| { | ||||
| 	getRedParents(out); | ||||
|  | ||||
| 	TNodes redParents;  | ||||
| 	TCNodes redParents; | ||||
| 	getRedParents(redParents); | ||||
|  | ||||
| 	for(CBonusSystemNode * parent : redParents) | ||||
| 	for(const CBonusSystemNode * parent : redParents) | ||||
| 		parent->getRedAncestors(out); | ||||
| } | ||||
|  | ||||
| @@ -646,4 +620,4 @@ int64_t CBonusSystemNode::getTreeVersion() const | ||||
| 	return treeChanged; | ||||
| } | ||||
|  | ||||
| VCMI_LIB_NAMESPACE_END | ||||
| VCMI_LIB_NAMESPACE_END | ||||
|   | ||||
| @@ -33,7 +33,7 @@ private: | ||||
| 	BonusList bonuses; //wielded bonuses (local or up-propagated here) | ||||
| 	BonusList exportedBonuses; //bonuses coming from this node (wielded or propagated away) | ||||
|  | ||||
| 	TNodesVector parents; //parents -> we inherit bonuses from them, we may attach our bonuses to them | ||||
| 	TNodesVector parents; // we inherit bonuses from them, we may attach our bonuses to them | ||||
| 	TNodesVector children; | ||||
|  | ||||
| 	ENodeTypes nodeType; | ||||
| @@ -54,8 +54,8 @@ private: | ||||
| 	TConstBonusListPtr getAllBonusesWithoutCaching(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root = nullptr) const; | ||||
| 	std::shared_ptr<Bonus> getUpdatedBonus(const std::shared_ptr<Bonus> & b, const TUpdaterPtr & updater) const; | ||||
|  | ||||
| 	void getRedParents(TNodes &out);  //retrieves list of red parent nodes (nodes bonuses propagate from) | ||||
| 	void getRedAncestors(TNodes &out); | ||||
| 	void getRedParents(TCNodes &out) const;  //retrieves list of red parent nodes (nodes bonuses propagate from) | ||||
| 	void getRedAncestors(TCNodes &out) const; | ||||
| 	void getRedChildren(TNodes &out); | ||||
|  | ||||
| 	void getAllParents(TCNodes & out) const; | ||||
| @@ -80,7 +80,6 @@ protected: | ||||
| public: | ||||
| 	explicit CBonusSystemNode(bool isHypotetic = false); | ||||
| 	explicit CBonusSystemNode(ENodeTypes NodeType); | ||||
| 	CBonusSystemNode(CBonusSystemNode && other) noexcept; | ||||
| 	virtual ~CBonusSystemNode(); | ||||
|  | ||||
| 	void limitBonuses(const BonusList &allBonuses, BonusList &out) const; //out will bo populed with bonuses that are not limited here | ||||
| @@ -128,14 +127,12 @@ public: | ||||
|  | ||||
| 	template <typename Handler> void serialize(Handler &h, const int version) | ||||
| 	{ | ||||
| //		h & bonuses; | ||||
| 		h & nodeType; | ||||
| 		h & exportedBonuses; | ||||
| 		BONUS_TREE_DESERIALIZATION_FIX | ||||
| 		//h & parents & children; | ||||
| 	} | ||||
|  | ||||
| 	friend class CBonusProxy; | ||||
| }; | ||||
|  | ||||
| VCMI_LIB_NAMESPACE_END | ||||
| VCMI_LIB_NAMESPACE_END | ||||
|   | ||||
| @@ -10,7 +10,7 @@ | ||||
|  | ||||
| #include "StdInc.h" | ||||
|  | ||||
| #include "CBonusSystemNode.h" | ||||
| #include "IBonusBearer.h" | ||||
| #include "BonusList.h" | ||||
|  | ||||
| VCMI_LIB_NAMESPACE_BEGIN | ||||
|   | ||||
| @@ -1928,14 +1928,6 @@ TeamState::TeamState() | ||||
| 	fogOfWarMap = std::make_unique<boost::multi_array<ui8, 3>>(); | ||||
| } | ||||
|  | ||||
| TeamState::TeamState(TeamState && other) noexcept: | ||||
| 	CBonusSystemNode(std::move(other)), | ||||
| 	id(other.id) | ||||
| { | ||||
| 	std::swap(players, other.players); | ||||
| 	std::swap(fogOfWarMap, other.fogOfWarMap); | ||||
| } | ||||
|  | ||||
| CRandomGenerator & CGameState::getRandomGenerator() | ||||
| { | ||||
| 	return rand; | ||||
|   | ||||
| @@ -416,26 +416,10 @@ public: | ||||
| 		ui32 length = readAndCheckLength(); | ||||
| 		data.clear(); | ||||
| 		T1 key; | ||||
| 		T2 value; | ||||
| 		for(ui32 i=0;i<length;i++) | ||||
| 		{ | ||||
| 			load(key); | ||||
| 			load(value); | ||||
| 			data.insert(std::pair<T1, T2>(std::move(key), std::move(value))); | ||||
| 		} | ||||
| 	} | ||||
| 	template <typename T1, typename T2> | ||||
| 	void load(std::multimap<T1, T2> &data) | ||||
| 	{ | ||||
| 		ui32 length = readAndCheckLength(); | ||||
| 		data.clear(); | ||||
| 		T1 key; | ||||
| 		T2 value; | ||||
| 		for(ui32 i = 0; i < length; i++) | ||||
| 		{ | ||||
| 			load(key); | ||||
| 			load(value); | ||||
| 			data.insert(std::pair<T1, T2>(std::move(key), std::move(value))); | ||||
| 			load(data[key]); | ||||
| 		} | ||||
| 	} | ||||
| 	void load(std::string &data) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user