2
0

Updaters.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 CreatureTerrainLimiter;
  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. public:
  80. TimesStackSizeUpdater() = default;
  81. TimesStackSizeUpdater(int minimum, int maximum, int stepSize)
  82. : minimum(minimum)
  83. , maximum(maximum)
  84. , stepSize(stepSize)
  85. {}
  86. std::shared_ptr<Bonus> createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const override;
  87. std::string toString() const override;
  88. JsonNode toJsonNode() const override;
  89. template <typename Handler> void serialize(Handler & h)
  90. {
  91. h & static_cast<IUpdater &>(*this);
  92. h & minimum;
  93. h & maximum;
  94. h & stepSize;
  95. }
  96. };
  97. class DLL_LINKAGE TimesArmySizeUpdater : public IUpdater
  98. {
  99. public:
  100. int minimum = std::numeric_limits<int>::min();
  101. int maximum = std::numeric_limits<int>::max();
  102. int stepSize = 1;
  103. int filteredLevel = -1;
  104. CreatureID filteredCreature;
  105. FactionID filteredFaction;
  106. TimesArmySizeUpdater() = default;
  107. std::shared_ptr<Bonus> createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const override;
  108. std::string toString() const override;
  109. JsonNode toJsonNode() const override;
  110. template <typename Handler> void serialize(Handler & h)
  111. {
  112. h & static_cast<IUpdater &>(*this);
  113. h & minimum;
  114. h & maximum;
  115. h & stepSize;
  116. h & filteredLevel;
  117. h & filteredCreature;
  118. h & filteredFaction;
  119. }
  120. };
  121. class DLL_LINKAGE TimesStackLevelUpdater : public IUpdater
  122. {
  123. std::shared_ptr<Bonus> apply(const std::shared_ptr<Bonus> & b, int level) const;
  124. public:
  125. std::shared_ptr<Bonus> createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const override;
  126. std::string toString() const override;
  127. JsonNode toJsonNode() const override;
  128. };
  129. class DLL_LINKAGE DivideStackLevelUpdater : public IUpdater
  130. {
  131. std::shared_ptr<Bonus> apply(const std::shared_ptr<Bonus> & b, int level) const;
  132. public:
  133. std::shared_ptr<Bonus> createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const override;
  134. std::string toString() const override;
  135. JsonNode toJsonNode() const override;
  136. };
  137. class DLL_LINKAGE TimesHeroLevelDivideStackLevelUpdater : public TimesHeroLevelUpdater
  138. {
  139. std::shared_ptr<DivideStackLevelUpdater> divideStackLevel;
  140. public:
  141. template <typename Handler> void serialize(Handler & h)
  142. {
  143. h & static_cast<TimesHeroLevelUpdater &>(*this);
  144. h & divideStackLevel;
  145. }
  146. TimesHeroLevelDivideStackLevelUpdater()
  147. : divideStackLevel(std::make_shared<DivideStackLevelUpdater>())
  148. {}
  149. std::shared_ptr<Bonus> createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const override;
  150. std::string toString() const override;
  151. JsonNode toJsonNode() const override;
  152. };
  153. class DLL_LINKAGE OwnerUpdater : public IUpdater
  154. {
  155. public:
  156. std::shared_ptr<Bonus> createUpdatedBonus(const std::shared_ptr<Bonus>& b, const CBonusSystemNode& context) const override;
  157. std::string toString() const override;
  158. JsonNode toJsonNode() const override;
  159. };
  160. VCMI_LIB_NAMESPACE_END