CBonusSystemNode.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "HeroBonus.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. using TNodes = std::set<CBonusSystemNode *>;
  14. using TCNodes = std::set<const CBonusSystemNode *>;
  15. using TNodesVector = std::vector<CBonusSystemNode *>;
  16. class DLL_LINKAGE CBonusSystemNode : public virtual IBonusBearer, public boost::noncopyable
  17. {
  18. public:
  19. enum ENodeTypes
  20. {
  21. NONE = -1,
  22. UNKNOWN, STACK_INSTANCE, STACK_BATTLE, SPECIALTY, ARTIFACT, CREATURE, ARTIFACT_INSTANCE, HERO, PLAYER, TEAM,
  23. TOWN_AND_VISITOR, BATTLE, COMMANDER, GLOBAL_EFFECTS, ALL_CREATURES, TOWN
  24. };
  25. private:
  26. BonusList bonuses; //wielded bonuses (local or up-propagated here)
  27. BonusList exportedBonuses; //bonuses coming from this node (wielded or propagated away)
  28. TNodesVector parents; //parents -> we inherit bonuses from them, we may attach our bonuses to them
  29. TNodesVector children;
  30. ENodeTypes nodeType;
  31. std::string description;
  32. bool isHypotheticNode;
  33. static const bool cachingEnabled;
  34. mutable BonusList cachedBonuses;
  35. mutable int64_t cachedLast;
  36. static std::atomic<int64_t> treeChanged;
  37. // Setting a value to cachingStr before getting any bonuses caches the result for later requests.
  38. // This string needs to be unique, that's why it has to be setted in the following manner:
  39. // [property key]_[value] => only for selector
  40. mutable std::map<std::string, TBonusListPtr > cachedRequests;
  41. mutable boost::mutex sync;
  42. void getAllBonusesRec(BonusList &out, const CSelector & selector) const;
  43. TConstBonusListPtr getAllBonusesWithoutCaching(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root = nullptr) const;
  44. std::shared_ptr<Bonus> getUpdatedBonus(const std::shared_ptr<Bonus> & b, const TUpdaterPtr & updater) const;
  45. public:
  46. explicit CBonusSystemNode();
  47. explicit CBonusSystemNode(bool isHypotetic);
  48. explicit CBonusSystemNode(ENodeTypes NodeType);
  49. CBonusSystemNode(CBonusSystemNode && other) noexcept;
  50. virtual ~CBonusSystemNode();
  51. void limitBonuses(const BonusList &allBonuses, BonusList &out) const; //out will bo populed with bonuses that are not limited here
  52. TBonusListPtr limitBonuses(const BonusList &allBonuses) const; //same as above, returns out by val for convienence
  53. TConstBonusListPtr getAllBonuses(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root = nullptr, const std::string &cachingStr = "") const override;
  54. void getParents(TCNodes &out) const; //retrieves list of parent nodes (nodes to inherit bonuses from),
  55. std::shared_ptr<const Bonus> getBonusLocalFirst(const CSelector & selector) const;
  56. //non-const interface
  57. void getParents(TNodes &out); //retrieves list of parent nodes (nodes to inherit bonuses from)
  58. void getRedParents(TNodes &out); //retrieves list of red parent nodes (nodes bonuses propagate from)
  59. void getRedAncestors(TNodes &out);
  60. void getRedChildren(TNodes &out);
  61. void getAllParents(TCNodes & out) const;
  62. static PlayerColor retrieveNodeOwner(const CBonusSystemNode * node);
  63. std::shared_ptr<Bonus> getBonusLocalFirst(const CSelector & selector);
  64. void attachTo(CBonusSystemNode & parent);
  65. void detachFrom(CBonusSystemNode & parent);
  66. void detachFromAll();
  67. virtual void addNewBonus(const std::shared_ptr<Bonus>& b);
  68. void accumulateBonus(const std::shared_ptr<Bonus>& b); //add value of bonus with same type/subtype or create new
  69. void newChildAttached(CBonusSystemNode & child);
  70. void childDetached(CBonusSystemNode & child);
  71. void propagateBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & source);
  72. void unpropagateBonus(const std::shared_ptr<Bonus> & b);
  73. void removeBonus(const std::shared_ptr<Bonus>& b);
  74. void removeBonuses(const CSelector & selector);
  75. void removeBonusesRecursive(const CSelector & s);
  76. void newRedDescendant(CBonusSystemNode & descendant); //propagation needed
  77. void removedRedDescendant(CBonusSystemNode & descendant); //de-propagation needed
  78. bool isIndependentNode() const; //node is independent when it has no parents nor children
  79. bool actsAsBonusSourceOnly() const;
  80. ///updates count of remaining turns and removes outdated bonuses by selector
  81. void reduceBonusDurations(const CSelector &s);
  82. virtual std::string bonusToString(const std::shared_ptr<Bonus>& bonus, bool description) const {return "";}; //description or bonus name
  83. virtual std::string nodeName() const;
  84. virtual std::string nodeShortInfo() const;
  85. bool isHypothetic() const { return isHypotheticNode; }
  86. void deserializationFix();
  87. void exportBonus(const std::shared_ptr<Bonus> & b);
  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