From 2c4cad7d9ce1f7becab19902ad535d924532d5b5 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Mon, 1 Jan 2024 00:44:09 +0200 Subject: [PATCH] Slight simplification of bonus system node class --- AI/VCAI/VCAI.cpp | 1 - client/CPlayerInterface.cpp | 1 - lib/CPlayerState.cpp | 2 - lib/CPlayerState.h | 2 - lib/IGameCallback.cpp | 1 - lib/bonuses/Bonus.cpp | 1 - lib/bonuses/CBonusSystemNode.cpp | 74 ++++++++++------------------- lib/bonuses/CBonusSystemNode.h | 11 ++--- lib/bonuses/IBonusBearer.cpp | 2 +- lib/gameState/CGameState.cpp | 8 ---- lib/serializer/BinaryDeserializer.h | 18 +------ 11 files changed, 30 insertions(+), 91 deletions(-) diff --git a/AI/VCAI/VCAI.cpp b/AI/VCAI/VCAI.cpp index 3939304f6..8954ff191 100644 --- a/AI/VCAI/VCAI.cpp +++ b/AI/VCAI/VCAI.cpp @@ -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" diff --git a/client/CPlayerInterface.cpp b/client/CPlayerInterface.cpp index c405ff491..fa80b647f 100644 --- a/client/CPlayerInterface.cpp +++ b/client/CPlayerInterface.cpp @@ -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" diff --git a/lib/CPlayerState.cpp b/lib/CPlayerState.cpp index 9cb13671a..2ac3e39db 100644 --- a/lib/CPlayerState.cpp +++ b/lib/CPlayerState.cpp @@ -23,8 +23,6 @@ PlayerState::PlayerState() setNodeType(PLAYER); } -PlayerState::PlayerState(PlayerState && other) noexcept = default; - PlayerState::~PlayerState() = default; std::string PlayerState::nodeName() const diff --git a/lib/CPlayerState.h b/lib/CPlayerState.h index 175d6ff21..87959cdec 100644 --- a/lib/CPlayerState.h +++ b/lib/CPlayerState.h @@ -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> fogOfWarMap; //[z][x][y] true - visible, false - hidden TeamState(); - TeamState(TeamState && other) noexcept; template void serialize(Handler &h, const int version) { diff --git a/lib/IGameCallback.cpp b/lib/IGameCallback.cpp index f40aebcd8..a555c5c1f 100644 --- a/lib/IGameCallback.cpp +++ b/lib/IGameCallback.cpp @@ -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" diff --git a/lib/bonuses/Bonus.cpp b/lib/bonuses/Bonus.cpp index bcf782b78..c0324824c 100644 --- a/lib/bonuses/Bonus.cpp +++ b/lib/bonuses/Bonus.cpp @@ -10,7 +10,6 @@ #include "StdInc.h" #include "Bonus.h" -#include "CBonusSystemNode.h" #include "Limiters.h" #include "Updaters.h" #include "Propagators.h" diff --git a/lib/bonuses/CBonusSystemNode.cpp b/lib/bonuses/CBonusSystemNode.cpp index 8306e3ae1..226648d01 100644 --- a/lib/bonuses/CBonusSystemNode.cpp +++ b/lib/bonuses/CBonusSystemNode.cpp @@ -20,16 +20,15 @@ VCMI_LIB_NAMESPACE_BEGIN std::atomic 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 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 & 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 & b) @@ -417,8 +385,10 @@ void CBonusSystemNode::unpropagateBonus(const std::shared_ptr & 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 \ No newline at end of file +VCMI_LIB_NAMESPACE_END diff --git a/lib/bonuses/CBonusSystemNode.h b/lib/bonuses/CBonusSystemNode.h index b5d4f8f71..7dd5f782a 100644 --- a/lib/bonuses/CBonusSystemNode.h +++ b/lib/bonuses/CBonusSystemNode.h @@ -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 getUpdatedBonus(const std::shared_ptr & 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 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 \ No newline at end of file +VCMI_LIB_NAMESPACE_END diff --git a/lib/bonuses/IBonusBearer.cpp b/lib/bonuses/IBonusBearer.cpp index 63ab0b631..a0bfc7087 100644 --- a/lib/bonuses/IBonusBearer.cpp +++ b/lib/bonuses/IBonusBearer.cpp @@ -10,7 +10,7 @@ #include "StdInc.h" -#include "CBonusSystemNode.h" +#include "IBonusBearer.h" #include "BonusList.h" VCMI_LIB_NAMESPACE_BEGIN diff --git a/lib/gameState/CGameState.cpp b/lib/gameState/CGameState.cpp index 089d4aced..5529df727 100644 --- a/lib/gameState/CGameState.cpp +++ b/lib/gameState/CGameState.cpp @@ -1928,14 +1928,6 @@ TeamState::TeamState() fogOfWarMap = std::make_unique>(); } -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; diff --git a/lib/serializer/BinaryDeserializer.h b/lib/serializer/BinaryDeserializer.h index 8b2696957..aa2b40f21 100644 --- a/lib/serializer/BinaryDeserializer.h +++ b/lib/serializer/BinaryDeserializer.h @@ -416,26 +416,10 @@ public: ui32 length = readAndCheckLength(); data.clear(); T1 key; - T2 value; for(ui32 i=0;i(std::move(key), std::move(value))); - } - } - template - void load(std::multimap &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(std::move(key), std::move(value))); + load(data[key]); } } void load(std::string &data)