CBonusSystemNode.h 5.1 KB

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