CBonusSystemNode.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "BonusList.h"
  12. #include "IBonusBearer.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. using TNodes = std::set<CBonusSystemNode *>;
  15. using TCNodes = std::set<const CBonusSystemNode *>;
  16. using TNodesVector = std::vector<CBonusSystemNode *>;
  17. class DLL_LINKAGE CBonusSystemNode : public virtual IBonusBearer, public boost::noncopyable
  18. {
  19. public:
  20. enum ENodeTypes
  21. {
  22. NONE = -1,
  23. UNKNOWN, STACK_INSTANCE, STACK_BATTLE, SPECIALTY, ARTIFACT, CREATURE, ARTIFACT_INSTANCE, HERO, PLAYER, TEAM,
  24. TOWN_AND_VISITOR, BATTLE, COMMANDER, GLOBAL_EFFECTS, ALL_CREATURES, TOWN
  25. };
  26. private:
  27. BonusList bonuses; //wielded bonuses (local or up-propagated here)
  28. BonusList exportedBonuses; //bonuses coming from this node (wielded or propagated away)
  29. TNodesVector parents; //parents -> we inherit bonuses from them, we may attach our bonuses to them
  30. TNodesVector children;
  31. ENodeTypes nodeType;
  32. std::string description;
  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. public:
  60. explicit CBonusSystemNode(bool isHypotetic = false);
  61. explicit CBonusSystemNode(ENodeTypes NodeType);
  62. CBonusSystemNode(CBonusSystemNode && other) noexcept;
  63. virtual ~CBonusSystemNode();
  64. void limitBonuses(const BonusList &allBonuses, BonusList &out) const; //out will bo populed with bonuses that are not limited here
  65. TBonusListPtr limitBonuses(const BonusList &allBonuses) const; //same as above, returns out by val for convienence
  66. TConstBonusListPtr getAllBonuses(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root = nullptr, const std::string &cachingStr = "") const override;
  67. void getParents(TCNodes &out) const; //retrieves list of parent nodes (nodes to inherit bonuses from),
  68. std::shared_ptr<const Bonus> getBonusLocalFirst(const CSelector & selector) const;
  69. //non-const interface
  70. void getParents(TNodes &out); //retrieves list of parent nodes (nodes to inherit bonuses from)
  71. static PlayerColor retrieveNodeOwner(const CBonusSystemNode * node);
  72. std::shared_ptr<Bonus> getBonusLocalFirst(const CSelector & selector);
  73. void attachTo(CBonusSystemNode & parent);
  74. void detachFrom(CBonusSystemNode & parent);
  75. void detachFromAll();
  76. virtual void addNewBonus(const std::shared_ptr<Bonus>& b);
  77. void accumulateBonus(const std::shared_ptr<Bonus>& b); //add value of bonus with same type/subtype or create new
  78. void removeBonus(const std::shared_ptr<Bonus>& b);
  79. void removeBonuses(const CSelector & selector);
  80. void removeBonusesRecursive(const CSelector & s);
  81. bool isIndependentNode() const; //node is independent when it has no parents nor children
  82. ///updates count of remaining turns and removes outdated bonuses by selector
  83. void reduceBonusDurations(const CSelector &s);
  84. virtual std::string bonusToString(const std::shared_ptr<Bonus>& bonus, bool description) const {return "";}; //description or bonus name
  85. virtual std::string nodeName() const;
  86. bool isHypothetic() const { return isHypotheticNode; }
  87. void deserializationFix();
  88. void exportBonuses();
  89. const BonusList &getBonusList() const;
  90. BonusList & getExportedBonusList();
  91. const BonusList & getExportedBonusList() const;
  92. CBonusSystemNode::ENodeTypes getNodeType() const;
  93. void setNodeType(CBonusSystemNode::ENodeTypes type);
  94. const TNodesVector & getParentNodes() const;
  95. const TNodesVector & getChildrenNodes() const;
  96. const std::string & getDescription() const;
  97. void setDescription(const std::string & description);
  98. static void treeHasChanged();
  99. int64_t getTreeVersion() const override;
  100. virtual PlayerColor getOwner() const
  101. {
  102. return PlayerColor::NEUTRAL;
  103. }
  104. template <typename Handler> void serialize(Handler &h, const int version)
  105. {
  106. // h & bonuses;
  107. h & nodeType;
  108. h & exportedBonuses;
  109. h & description;
  110. BONUS_TREE_DESERIALIZATION_FIX
  111. //h & parents & children;
  112. }
  113. friend class CBonusProxy;
  114. };
  115. VCMI_LIB_NAMESPACE_END