CBonusSystemNode.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. mutable BonusList cachedBonuses;
  49. mutable int32_t cachedLast;
  50. std::atomic<int32_t> nodeChanged;
  51. void invalidateChildrenNodes(int32_t changeCounter);
  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<int32_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 propagateBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & source);
  67. void unpropagateBonus(const std::shared_ptr<Bonus> & b);
  68. bool actsAsBonusSourceOnly() const;
  69. void newRedDescendant(CBonusSystemNode & descendant) const; //propagation needed
  70. void removedRedDescendant(CBonusSystemNode & descendant) const; //de-propagation needed
  71. std::string nodeShortInfo() const;
  72. void exportBonus(const std::shared_ptr<Bonus> & b);
  73. protected:
  74. bool isIndependentNode() const; //node is independent when it has no parents nor children
  75. void exportBonuses();
  76. public:
  77. explicit CBonusSystemNode(bool isHypotetic = false);
  78. explicit CBonusSystemNode(ENodeTypes NodeType);
  79. virtual ~CBonusSystemNode();
  80. TConstBonusListPtr getAllBonuses(const CSelector &selector, const CSelector &limit, const std::string &cachingStr = "") const override;
  81. void getParents(TCNodes &out) const; //retrieves list of parent nodes (nodes to inherit bonuses from),
  82. /// Returns first bonus matching selector
  83. std::shared_ptr<const Bonus> getFirstBonus(const CSelector & selector) const;
  84. /// Provides write access to first bonus from this node that matches selector
  85. std::shared_ptr<Bonus> getLocalBonus(const CSelector & selector);
  86. void attachTo(CBonusSystemNode & parent);
  87. void attachToSource(const CBonusSystemNode & parent);
  88. void detachFrom(CBonusSystemNode & parent);
  89. void detachFromSource(const CBonusSystemNode & parent);
  90. void detachFromAll();
  91. virtual void addNewBonus(const std::shared_ptr<Bonus>& b);
  92. void accumulateBonus(const std::shared_ptr<Bonus>& b); //add value of bonus with same type/subtype or create new
  93. void removeBonus(const std::shared_ptr<Bonus>& b);
  94. void removeBonuses(const CSelector & selector);
  95. void removeBonusesRecursive(const CSelector & s);
  96. ///updates count of remaining turns and removes outdated bonuses by selector
  97. void reduceBonusDurations(const CSelector &s);
  98. virtual std::string bonusToString(const std::shared_ptr<Bonus>& bonus, bool description) const {return "";}; //description or bonus name
  99. virtual std::string nodeName() const;
  100. bool isHypothetic() const { return isHypotheticNode; }
  101. BonusList & getExportedBonusList();
  102. const BonusList & getExportedBonusList() const;
  103. CBonusSystemNode::ENodeTypes getNodeType() const;
  104. void setNodeType(CBonusSystemNode::ENodeTypes type);
  105. const TCNodesVector & getParentNodes() const;
  106. void nodeHasChanged();
  107. int32_t getTreeVersion() const override;
  108. virtual PlayerColor getOwner() const
  109. {
  110. return PlayerColor::NEUTRAL;
  111. }
  112. template <typename Handler> void serialize(Handler &h)
  113. {
  114. h & nodeType;
  115. h & exportedBonuses;
  116. if(!h.saving && h.loadingGamestate)
  117. exportBonuses();
  118. }
  119. friend class CBonusProxy;
  120. };
  121. VCMI_LIB_NAMESPACE_END