CBonusSystemNode.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. public:
  47. explicit CBonusSystemNode();
  48. explicit CBonusSystemNode(bool isHypotetic);
  49. explicit CBonusSystemNode(ENodeTypes NodeType);
  50. CBonusSystemNode(CBonusSystemNode && other) noexcept;
  51. virtual ~CBonusSystemNode();
  52. void limitBonuses(const BonusList &allBonuses, BonusList &out) const; //out will bo populed with bonuses that are not limited here
  53. TBonusListPtr limitBonuses(const BonusList &allBonuses) const; //same as above, returns out by val for convienence
  54. TConstBonusListPtr getAllBonuses(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root = nullptr, const std::string &cachingStr = "") const override;
  55. void getParents(TCNodes &out) const; //retrieves list of parent nodes (nodes to inherit bonuses from),
  56. std::shared_ptr<const Bonus> getBonusLocalFirst(const CSelector & selector) const;
  57. //non-const interface
  58. void getParents(TNodes &out); //retrieves list of parent nodes (nodes to inherit bonuses from)
  59. void getRedParents(TNodes &out); //retrieves list of red parent nodes (nodes bonuses propagate from)
  60. void getRedAncestors(TNodes &out);
  61. void getRedChildren(TNodes &out);
  62. void getAllParents(TCNodes & out) const;
  63. static PlayerColor retrieveNodeOwner(const CBonusSystemNode * node);
  64. std::shared_ptr<Bonus> getBonusLocalFirst(const CSelector & selector);
  65. void attachTo(CBonusSystemNode & parent);
  66. void detachFrom(CBonusSystemNode & parent);
  67. void detachFromAll();
  68. virtual void addNewBonus(const std::shared_ptr<Bonus>& b);
  69. void accumulateBonus(const std::shared_ptr<Bonus>& b); //add value of bonus with same type/subtype or create new
  70. void newChildAttached(CBonusSystemNode & child);
  71. void childDetached(CBonusSystemNode & child);
  72. void propagateBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & source);
  73. void unpropagateBonus(const std::shared_ptr<Bonus> & b);
  74. void removeBonus(const std::shared_ptr<Bonus>& b);
  75. void removeBonuses(const CSelector & selector);
  76. void removeBonusesRecursive(const CSelector & s);
  77. void newRedDescendant(CBonusSystemNode & descendant); //propagation needed
  78. void removedRedDescendant(CBonusSystemNode & descendant); //de-propagation needed
  79. bool isIndependentNode() const; //node is independent when it has no parents nor children
  80. bool actsAsBonusSourceOnly() const;
  81. ///updates count of remaining turns and removes outdated bonuses by selector
  82. void reduceBonusDurations(const CSelector &s);
  83. virtual std::string bonusToString(const std::shared_ptr<Bonus>& bonus, bool description) const {return "";}; //description or bonus name
  84. virtual std::string nodeName() const;
  85. virtual std::string nodeShortInfo() const;
  86. bool isHypothetic() const { return isHypotheticNode; }
  87. void deserializationFix();
  88. void exportBonus(const std::shared_ptr<Bonus> & b);
  89. void exportBonuses();
  90. const BonusList &getBonusList() const;
  91. BonusList & getExportedBonusList();
  92. const BonusList & getExportedBonusList() const;
  93. CBonusSystemNode::ENodeTypes getNodeType() const;
  94. void setNodeType(CBonusSystemNode::ENodeTypes type);
  95. const TNodesVector &getParentNodes() const;
  96. const TNodesVector &getChildrenNodes() const;
  97. const std::string &getDescription() const;
  98. void setDescription(const std::string &description);
  99. static void treeHasChanged();
  100. int64_t getTreeVersion() const override;
  101. virtual PlayerColor getOwner() const
  102. {
  103. return PlayerColor::NEUTRAL;
  104. }
  105. template <typename Handler> void serialize(Handler &h, const int version)
  106. {
  107. // h & bonuses;
  108. h & nodeType;
  109. h & exportedBonuses;
  110. h & description;
  111. BONUS_TREE_DESERIALIZATION_FIX
  112. //h & parents & children;
  113. }
  114. friend class CBonusProxy;
  115. };
  116. VCMI_LIB_NAMESPACE_END