Updaters.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Updaters.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 "Bonus.h"
  12. #include "../serializer/Serializeable.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. class AggregateLimiter;
  15. class CCreatureTypeLimiter;
  16. class HasAnotherBonusLimiter;
  17. class TerrainLimiter;
  18. class CreatureLevelLimiter;
  19. class FactionLimiter;
  20. class CreatureAlignmentLimiter;
  21. class OppositeSideLimiter;
  22. class RankRangeLimiter;
  23. class UnitOnHexLimiter;
  24. // observers for updating bonuses based on certain events (e.g. hero gaining level)
  25. class DLL_LINKAGE IUpdater : public Serializeable
  26. {
  27. public:
  28. virtual ~IUpdater() = default;
  29. virtual std::shared_ptr<Bonus> createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const;
  30. virtual std::string toString() const;
  31. virtual JsonNode toJsonNode() const;
  32. template <typename Handler> void serialize(Handler & h)
  33. {
  34. }
  35. };
  36. class DLL_LINKAGE GrowsWithLevelUpdater : public IUpdater
  37. {
  38. public:
  39. int valPer20 = 0;
  40. int stepSize = 1;
  41. GrowsWithLevelUpdater() = default;
  42. GrowsWithLevelUpdater(int valPer20, int stepSize = 1);
  43. template <typename Handler> void serialize(Handler & h)
  44. {
  45. h & static_cast<IUpdater &>(*this);
  46. h & valPer20;
  47. h & stepSize;
  48. }
  49. std::shared_ptr<Bonus> createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const override;
  50. std::string toString() const override;
  51. JsonNode toJsonNode() const override;
  52. };
  53. class DLL_LINKAGE TimesHeroLevelUpdater : public IUpdater
  54. {
  55. int stepSize = 1;
  56. public:
  57. TimesHeroLevelUpdater() = default;
  58. TimesHeroLevelUpdater(int stepSize)
  59. : stepSize(stepSize)
  60. {
  61. assert(stepSize > 0);
  62. }
  63. template <typename Handler> void serialize(Handler & h)
  64. {
  65. h & static_cast<IUpdater &>(*this);
  66. if (h.hasFeature(Handler::Version::UNIVERSITY_CONFIG))
  67. h & stepSize;
  68. }
  69. std::shared_ptr<Bonus> createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const override;
  70. std::string toString() const override;
  71. JsonNode toJsonNode() const override;
  72. };
  73. class DLL_LINKAGE TimesStackSizeUpdater : public IUpdater
  74. {
  75. std::shared_ptr<Bonus> apply(const std::shared_ptr<Bonus> & b, int count) const;
  76. int minimum = std::numeric_limits<int>::min();
  77. int maximum = std::numeric_limits<int>::max();
  78. int stepSize = 1;
  79. int stepValue = 1;
  80. public:
  81. TimesStackSizeUpdater() = default;
  82. TimesStackSizeUpdater(int minimum, int maximum, int stepSize, int stepValue)
  83. : minimum(minimum)
  84. , maximum(maximum)
  85. , stepSize(stepSize)
  86. , stepValue(stepValue)
  87. {}
  88. std::shared_ptr<Bonus> createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const override;
  89. std::string toString() const override;
  90. JsonNode toJsonNode() const override;
  91. template <typename Handler> void serialize(Handler & h)
  92. {
  93. h & static_cast<IUpdater &>(*this);
  94. h & minimum;
  95. h & maximum;
  96. h & stepSize;
  97. }
  98. };
  99. class DLL_LINKAGE TimesArmySizeUpdater : public IUpdater
  100. {
  101. public:
  102. int minimum = std::numeric_limits<int>::min();
  103. int maximum = std::numeric_limits<int>::max();
  104. int stepSize = 1;
  105. int filteredLevel = -1;
  106. CreatureID filteredCreature;
  107. FactionID filteredFaction;
  108. TimesArmySizeUpdater() = default;
  109. std::shared_ptr<Bonus> createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const override;
  110. std::string toString() const override;
  111. JsonNode toJsonNode() const override;
  112. template <typename Handler> void serialize(Handler & h)
  113. {
  114. h & static_cast<IUpdater &>(*this);
  115. h & minimum;
  116. h & maximum;
  117. h & stepSize;
  118. h & filteredLevel;
  119. h & filteredCreature;
  120. h & filteredFaction;
  121. }
  122. };
  123. class DLL_LINKAGE TimesStackLevelUpdater : public IUpdater
  124. {
  125. std::shared_ptr<Bonus> apply(const std::shared_ptr<Bonus> & b, int level) const;
  126. public:
  127. std::shared_ptr<Bonus> createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const override;
  128. std::string toString() const override;
  129. JsonNode toJsonNode() const override;
  130. };
  131. class DLL_LINKAGE DivideStackLevelUpdater : public IUpdater
  132. {
  133. std::shared_ptr<Bonus> apply(const std::shared_ptr<Bonus> & b, int level) const;
  134. public:
  135. std::shared_ptr<Bonus> createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const override;
  136. std::string toString() const override;
  137. JsonNode toJsonNode() const override;
  138. };
  139. class DLL_LINKAGE TimesHeroLevelDivideStackLevelUpdater : public TimesHeroLevelUpdater
  140. {
  141. std::shared_ptr<DivideStackLevelUpdater> divideStackLevel;
  142. public:
  143. template <typename Handler> void serialize(Handler & h)
  144. {
  145. h & static_cast<TimesHeroLevelUpdater &>(*this);
  146. h & divideStackLevel;
  147. }
  148. TimesHeroLevelDivideStackLevelUpdater()
  149. : divideStackLevel(std::make_shared<DivideStackLevelUpdater>())
  150. {}
  151. std::shared_ptr<Bonus> createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const override;
  152. std::string toString() const override;
  153. JsonNode toJsonNode() const override;
  154. };
  155. class DLL_LINKAGE OwnerUpdater : public IUpdater
  156. {
  157. public:
  158. std::shared_ptr<Bonus> createUpdatedBonus(const std::shared_ptr<Bonus>& b, const CBonusSystemNode& context) const override;
  159. std::string toString() const override;
  160. JsonNode toJsonNode() const override;
  161. };
  162. class DLL_LINKAGE CompositeUpdater : public IUpdater
  163. {
  164. public:
  165. std::vector<TUpdaterPtr> updaters;
  166. std::shared_ptr<Bonus> createUpdatedBonus(const std::shared_ptr<Bonus>& b, const CBonusSystemNode& context) const override;
  167. std::string toString() const override;
  168. JsonNode toJsonNode() const override;
  169. template <typename Handler> void serialize(Handler & h)
  170. {
  171. h & updaters;
  172. }
  173. };
  174. VCMI_LIB_NAMESPACE_END