CBonusSystemNode.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #include "../serializer/Serializeable.h"
  14. #include <tbb/concurrent_hash_map.h>
  15. VCMI_LIB_NAMESPACE_BEGIN
  16. using TNodes = std::set<CBonusSystemNode *>;
  17. using TCNodes = std::set<const CBonusSystemNode *>;
  18. using TNodesVector = std::vector<CBonusSystemNode *>;
  19. using TCNodesVector = std::vector<const CBonusSystemNode *>;
  20. class DLL_LINKAGE CBonusSystemNode : public virtual IBonusBearer, public virtual Serializeable, public boost::noncopyable
  21. {
  22. public:
  23. struct HashStringCompare {
  24. static size_t hash(const std::string& data)
  25. {
  26. std::hash<std::string> hasher;
  27. return hasher(data);
  28. }
  29. static bool equal(const std::string& x, const std::string& y)
  30. {
  31. return x == y;
  32. }
  33. };
  34. private:
  35. /// List of bonuses that affect this node, whether local, or propagated to this node
  36. BonusList bonuses;
  37. /// List of bonuses that ar ecoming from this node.
  38. /// Also includes nodes that are propagated away from this node, and might not affect this node itself
  39. BonusList exportedBonuses;
  40. TCNodesVector parentsToInherit; // we inherit bonuses from them
  41. TNodesVector parentsToPropagate; // we may attach our bonuses to them
  42. TNodesVector children;
  43. BonusNodeType nodeType;
  44. bool isHypotheticNode;
  45. mutable BonusList cachedBonuses;
  46. mutable int32_t cachedLast;
  47. std::atomic<int32_t> nodeChanged;
  48. void invalidateChildrenNodes(int32_t changeCounter);
  49. // Setting a value to cachingStr before getting any bonuses caches the result for later requests.
  50. // This string needs to be unique, that's why it has to be set in the following manner:
  51. // [property key]_[value] => only for selector
  52. using RequestsMap = tbb::concurrent_hash_map<std::string, std::pair<int32_t, TBonusListPtr>, HashStringCompare>;
  53. mutable RequestsMap cachedRequests;
  54. mutable std::shared_mutex sync;
  55. void getAllBonusesRec(BonusList &out) const;
  56. TConstBonusListPtr getAllBonusesWithoutCaching(const CSelector &selector) const;
  57. std::shared_ptr<Bonus> getUpdatedBonus(const std::shared_ptr<Bonus> & b, const TUpdaterPtr & updater) const;
  58. void limitBonuses(const BonusList &allBonuses, BonusList &out) const; //out will bo populed with bonuses that are not limited here
  59. void getRedParents(TCNodes &out) const; //retrieves list of red parent nodes (nodes bonuses propagate from)
  60. void getRedAncestors(TCNodes &out) const;
  61. void getRedChildren(TNodes &out);
  62. void propagateBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & source);
  63. void unpropagateBonus(const std::shared_ptr<Bonus> & b);
  64. bool actsAsBonusSourceOnly() const;
  65. void newRedDescendant(CBonusSystemNode & descendant) const; //propagation needed
  66. void removedRedDescendant(CBonusSystemNode & descendant) const; //de-propagation needed
  67. std::string nodeShortInfo() const;
  68. void exportBonus(const std::shared_ptr<Bonus> & b);
  69. protected:
  70. bool isIndependentNode() const; //node is independent when it has no parents nor children
  71. void exportBonuses();
  72. public:
  73. explicit CBonusSystemNode(BonusNodeType nodeType, bool isHypotetic);
  74. explicit CBonusSystemNode(BonusNodeType nodeType);
  75. virtual ~CBonusSystemNode();
  76. TConstBonusListPtr getAllBonuses(const CSelector &selector, const std::string &cachingStr = "") const override;
  77. void getDirectParents(TCNodes &out) const; //retrieves list of parent nodes (nodes to inherit bonuses from),
  78. /// Returns first bonus matching selector
  79. std::shared_ptr<const Bonus> getFirstBonus(const CSelector & selector) const;
  80. /// Provides write access to first bonus from this node that matches selector
  81. std::shared_ptr<Bonus> getLocalBonus(const CSelector & selector);
  82. void attachTo(CBonusSystemNode & parent);
  83. void attachToSource(const CBonusSystemNode & parent);
  84. void detachFrom(CBonusSystemNode & parent);
  85. void detachFromSource(const CBonusSystemNode & parent);
  86. void detachFromAll();
  87. virtual void addNewBonus(const std::shared_ptr<Bonus>& b);
  88. void accumulateBonus(const std::shared_ptr<Bonus>& b); //add value of bonus with same type/subtype or create new
  89. void removeBonus(const std::shared_ptr<Bonus>& b);
  90. void removeBonuses(const CSelector & selector);
  91. void removeBonusesRecursive(const CSelector & s);
  92. ///updates count of remaining turns and removes outdated bonuses by selector
  93. void reduceBonusDurations(const CSelector &s);
  94. virtual std::string bonusToString(const std::shared_ptr<Bonus>& bonus) const {return "";}; //description or bonus name
  95. virtual std::string nodeName() const;
  96. bool isHypothetic() const { return isHypotheticNode; }
  97. BonusList & getExportedBonusList();
  98. const BonusList & getExportedBonusList() const;
  99. BonusNodeType getNodeType() const;
  100. const TCNodesVector & getParentNodes() const;
  101. void nodeHasChanged();
  102. int32_t getTreeVersion() const override;
  103. virtual PlayerColor getOwner() const
  104. {
  105. return PlayerColor::NEUTRAL;
  106. }
  107. template <typename Handler> void serialize(Handler &h)
  108. {
  109. h & nodeType;
  110. h & exportedBonuses;
  111. if(!h.saving && h.loadingGamestate)
  112. exportBonuses();
  113. }
  114. friend class CBonusProxy;
  115. };
  116. VCMI_LIB_NAMESPACE_END