CBonusSystemNode.h 5.3 KB

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