CBonusSystemNode.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * CBonusSystemNode.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "GameConstants.h"
  12. #include "BonusList.h"
  13. #include "IBonusBearer.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. using TNodes = std::set<CBonusSystemNode *>;
  16. using TCNodes = std::set<const CBonusSystemNode *>;
  17. using TNodesVector = std::vector<CBonusSystemNode *>;
  18. class DLL_LINKAGE CBonusSystemNode : public virtual IBonusBearer, public boost::noncopyable
  19. {
  20. public:
  21. enum ENodeTypes
  22. {
  23. NONE = -1,
  24. UNKNOWN, STACK_INSTANCE, STACK_BATTLE, SPECIALTY, ARTIFACT, CREATURE, ARTIFACT_INSTANCE, HERO, PLAYER, TEAM,
  25. TOWN_AND_VISITOR, BATTLE, COMMANDER, GLOBAL_EFFECTS, ALL_CREATURES, TOWN
  26. };
  27. private:
  28. BonusList bonuses; //wielded bonuses (local or up-propagated here)
  29. BonusList exportedBonuses; //bonuses coming from this node (wielded or propagated away)
  30. TNodesVector parents; //parents -> we inherit bonuses from them, we may attach our bonuses to them
  31. TNodesVector children;
  32. ENodeTypes nodeType;
  33. bool isHypotheticNode;
  34. static const bool cachingEnabled;
  35. mutable BonusList cachedBonuses;
  36. mutable int64_t cachedLast;
  37. static std::atomic<int64_t> treeChanged;
  38. // Setting a value to cachingStr before getting any bonuses caches the result for later requests.
  39. // This string needs to be unique, that's why it has to be setted in the following manner:
  40. // [property key]_[value] => only for selector
  41. mutable std::map<std::string, TBonusListPtr > cachedRequests;
  42. mutable boost::mutex sync;
  43. void getAllBonusesRec(BonusList &out, const CSelector & selector) const;
  44. TConstBonusListPtr getAllBonusesWithoutCaching(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root = nullptr) const;
  45. std::shared_ptr<Bonus> getUpdatedBonus(const std::shared_ptr<Bonus> & b, const TUpdaterPtr & updater) const;
  46. void getRedParents(TNodes &out); //retrieves list of red parent nodes (nodes bonuses propagate from)
  47. void getRedAncestors(TNodes &out);
  48. void getRedChildren(TNodes &out);
  49. void getAllParents(TCNodes & out) const;
  50. void newChildAttached(CBonusSystemNode & child);
  51. void childDetached(CBonusSystemNode & child);
  52. void propagateBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & source);
  53. void unpropagateBonus(const std::shared_ptr<Bonus> & b);
  54. bool actsAsBonusSourceOnly() const;
  55. void newRedDescendant(CBonusSystemNode & descendant); //propagation needed
  56. void removedRedDescendant(CBonusSystemNode & descendant); //de-propagation needed
  57. std::string nodeShortInfo() const;
  58. void exportBonus(const std::shared_ptr<Bonus> & b);
  59. protected:
  60. bool isIndependentNode() const; //node is independent when it has no parents nor children
  61. void exportBonuses();
  62. public:
  63. explicit CBonusSystemNode(bool isHypotetic = false);
  64. explicit CBonusSystemNode(ENodeTypes NodeType);
  65. CBonusSystemNode(CBonusSystemNode && other) noexcept;
  66. virtual ~CBonusSystemNode();
  67. void limitBonuses(const BonusList &allBonuses, BonusList &out) const; //out will bo populed with bonuses that are not limited here
  68. TBonusListPtr limitBonuses(const BonusList &allBonuses) const; //same as above, returns out by val for convienence
  69. TConstBonusListPtr getAllBonuses(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root = nullptr, const std::string &cachingStr = "") const override;
  70. void getParents(TCNodes &out) const; //retrieves list of parent nodes (nodes to inherit bonuses from),
  71. std::shared_ptr<const Bonus> getBonusLocalFirst(const CSelector & selector) const;
  72. //non-const interface
  73. void getParents(TNodes &out); //retrieves list of parent nodes (nodes to inherit bonuses from)
  74. std::shared_ptr<Bonus> getBonusLocalFirst(const CSelector & selector);
  75. void attachTo(CBonusSystemNode & parent);
  76. void detachFrom(CBonusSystemNode & parent);
  77. void detachFromAll();
  78. virtual void addNewBonus(const std::shared_ptr<Bonus>& b);
  79. void accumulateBonus(const std::shared_ptr<Bonus>& b); //add value of bonus with same type/subtype or create new
  80. void removeBonus(const std::shared_ptr<Bonus>& b);
  81. void removeBonuses(const CSelector & selector);
  82. void removeBonusesRecursive(const CSelector & s);
  83. ///updates count of remaining turns and removes outdated bonuses by selector
  84. void reduceBonusDurations(const CSelector &s);
  85. virtual std::string bonusToString(const std::shared_ptr<Bonus>& bonus, bool description) const {return "";}; //description or bonus name
  86. virtual std::string nodeName() const;
  87. bool isHypothetic() const { return isHypotheticNode; }
  88. void deserializationFix();
  89. BonusList & getExportedBonusList();
  90. const BonusList & getExportedBonusList() const;
  91. CBonusSystemNode::ENodeTypes getNodeType() const;
  92. void setNodeType(CBonusSystemNode::ENodeTypes type);
  93. const TNodesVector & getParentNodes() const;
  94. static void treeHasChanged();
  95. int64_t getTreeVersion() const override;
  96. virtual PlayerColor getOwner() const
  97. {
  98. return PlayerColor::NEUTRAL;
  99. }
  100. template <typename Handler> void serialize(Handler &h, const int version)
  101. {
  102. // h & bonuses;
  103. h & nodeType;
  104. h & exportedBonuses;
  105. BONUS_TREE_DESERIALIZATION_FIX
  106. //h & parents & children;
  107. }
  108. friend class CBonusProxy;
  109. };
  110. VCMI_LIB_NAMESPACE_END