CBonusSystemNode.h 5.5 KB

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