Updaters.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Updaters.cpp, 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. #include "StdInc.h"
  11. #include "Updaters.h"
  12. #include "Limiters.h"
  13. #include "../json/JsonNode.h"
  14. #include "../mapObjects/CGHeroInstance.h"
  15. #include "../CStack.h"
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. std::shared_ptr<Bonus> IUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
  18. {
  19. return b;
  20. }
  21. std::string IUpdater::toString() const
  22. {
  23. return typeid(*this).name();
  24. }
  25. JsonNode IUpdater::toJsonNode() const
  26. {
  27. return JsonNode();
  28. }
  29. GrowsWithLevelUpdater::GrowsWithLevelUpdater(int valPer20, int stepSize)
  30. : valPer20(valPer20)
  31. , stepSize(stepSize)
  32. {
  33. }
  34. std::shared_ptr<Bonus> GrowsWithLevelUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
  35. {
  36. if(context.getNodeType() == BonusNodeType::HERO)
  37. {
  38. int level = dynamic_cast<const CGHeroInstance &>(context).level;
  39. int steps = stepSize ? level / stepSize : level;
  40. //rounding follows format for HMM3 creature specialty bonus
  41. int newVal = (valPer20 * steps + 19) / 20;
  42. //return copy of bonus with updated val
  43. auto newBonus = std::make_shared<Bonus>(*b);
  44. newBonus->val = newVal;
  45. return newBonus;
  46. }
  47. return b;
  48. }
  49. std::string GrowsWithLevelUpdater::toString() const
  50. {
  51. return boost::str(boost::format("GrowsWithLevelUpdater(valPer20=%d, stepSize=%d)") % valPer20 % stepSize);
  52. }
  53. JsonNode GrowsWithLevelUpdater::toJsonNode() const
  54. {
  55. JsonNode root;
  56. root["type"].String() = "GROWS_WITH_LEVEL";
  57. root["parameters"].Vector().emplace_back(valPer20);
  58. if(stepSize > 1)
  59. root["parameters"].Vector().emplace_back(stepSize);
  60. return root;
  61. }
  62. std::shared_ptr<Bonus> TimesHeroLevelUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
  63. {
  64. if(context.getNodeType() == BonusNodeType::HERO)
  65. {
  66. int level = dynamic_cast<const CGHeroInstance &>(context).level;
  67. auto newBonus = std::make_shared<Bonus>(*b);
  68. newBonus->val *= level / stepSize;
  69. return newBonus;
  70. }
  71. return b;
  72. }
  73. std::string TimesHeroLevelUpdater::toString() const
  74. {
  75. return "TimesHeroLevelUpdater";
  76. }
  77. JsonNode TimesHeroLevelUpdater::toJsonNode() const
  78. {
  79. return JsonNode("TIMES_HERO_LEVEL");
  80. }
  81. std::shared_ptr<Bonus> TimesHeroLevelDivideStackLevelUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
  82. {
  83. if(context.getNodeType() == BonusNodeType::HERO)
  84. {
  85. auto newBonus = TimesHeroLevelUpdater::createUpdatedBonus(b, context);
  86. newBonus->updater = divideStackLevel;
  87. return newBonus;
  88. }
  89. return b;
  90. }
  91. std::string TimesHeroLevelDivideStackLevelUpdater::toString() const
  92. {
  93. return "TimesHeroLevelDivideStackLevelUpdater";
  94. }
  95. JsonNode TimesHeroLevelDivideStackLevelUpdater::toJsonNode() const
  96. {
  97. return JsonNode("TIMES_HERO_LEVEL_DIVIDE_STACK_LEVEL");
  98. }
  99. std::shared_ptr<Bonus> TimesStackSizeUpdater::apply(const std::shared_ptr<Bonus> & b, int count) const
  100. {
  101. auto newBonus = std::make_shared<Bonus>(*b);
  102. newBonus->val *= std::clamp(count / stepSize, minimum, maximum);
  103. return newBonus;
  104. }
  105. std::shared_ptr<Bonus> TimesStackSizeUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
  106. {
  107. if(context.getNodeType() == BonusNodeType::STACK_INSTANCE || context.getNodeType() == BonusNodeType::COMMANDER)
  108. {
  109. int count = dynamic_cast<const CStackInstance &>(context).getCount();
  110. return apply(b, count);
  111. }
  112. if(context.getNodeType() == BonusNodeType::STACK_BATTLE)
  113. {
  114. const auto & stack = dynamic_cast<const CStack &>(context);
  115. return apply(b, stack.getCount());
  116. }
  117. return b;
  118. }
  119. std::string TimesStackSizeUpdater::toString() const
  120. {
  121. return "TimesStackSizeUpdater";
  122. }
  123. JsonNode TimesStackSizeUpdater::toJsonNode() const
  124. {
  125. return JsonNode("TIMES_STACK_SIZE");
  126. }
  127. std::shared_ptr<Bonus> TimesStackLevelUpdater::apply(const std::shared_ptr<Bonus> & b, int level) const
  128. {
  129. auto newBonus = std::make_shared<Bonus>(*b);
  130. newBonus->val *= level;
  131. newBonus->updater = nullptr; // prevent double-apply
  132. return newBonus;
  133. }
  134. std::shared_ptr<Bonus> TimesStackLevelUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
  135. {
  136. if(context.getNodeType() == BonusNodeType::STACK_INSTANCE || context.getNodeType() == BonusNodeType::COMMANDER)
  137. {
  138. int level = dynamic_cast<const CStackInstance &>(context).getLevel();
  139. return apply(b, level);
  140. }
  141. if(context.getNodeType() == BonusNodeType::STACK_BATTLE)
  142. {
  143. const auto & stack = dynamic_cast<const CStack &>(context);
  144. //update if stack doesn't have an instance (summons, war machines)
  145. if(stack.base == nullptr)
  146. return apply(b, stack.unitType()->getLevel());
  147. // If these are not handled here, the final outcome may potentially be incorrect.
  148. int level = dynamic_cast<const CStackInstance*>(stack.base)->getLevel();
  149. return apply(b, level);
  150. }
  151. return b;
  152. }
  153. std::string TimesStackLevelUpdater::toString() const
  154. {
  155. return "TimesStackLevelUpdater";
  156. }
  157. JsonNode TimesStackLevelUpdater::toJsonNode() const
  158. {
  159. return JsonNode("TIMES_STACK_LEVEL");
  160. }
  161. std::shared_ptr<Bonus> DivideStackLevelUpdater::apply(const std::shared_ptr<Bonus> & b, int level) const
  162. {
  163. if (level == 0)
  164. return b; // e.g. war machines & other special units
  165. auto newBonus = std::make_shared<Bonus>(*b);
  166. level = std::max(1, level);
  167. newBonus->val /= level;
  168. newBonus->updater = nullptr; // prevent double-apply
  169. return newBonus;
  170. }
  171. std::shared_ptr<Bonus> DivideStackLevelUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
  172. {
  173. if(context.getNodeType() == BonusNodeType::STACK_INSTANCE || context.getNodeType() == BonusNodeType::COMMANDER)
  174. {
  175. int level = dynamic_cast<const CStackInstance &>(context).getLevel();
  176. return apply(b, level);
  177. }
  178. if(context.getNodeType() == BonusNodeType::STACK_BATTLE)
  179. {
  180. const auto & stack = dynamic_cast<const CStack &>(context);
  181. //update if stack doesn't have an instance (summons, war machines)
  182. if(stack.base == nullptr)
  183. return apply(b, stack.unitType()->getLevel());
  184. // If these are not handled here, the final outcome may potentially be incorrect.
  185. int level = dynamic_cast<const CStackInstance*>(stack.base)->getLevel();
  186. return apply(b, level);
  187. }
  188. return b;
  189. }
  190. std::string DivideStackLevelUpdater::toString() const
  191. {
  192. return "DivideStackLevelUpdater";
  193. }
  194. JsonNode DivideStackLevelUpdater::toJsonNode() const
  195. {
  196. return JsonNode("DIVIDE_STACK_LEVEL");
  197. }
  198. std::string OwnerUpdater::toString() const
  199. {
  200. return "OwnerUpdater";
  201. }
  202. JsonNode OwnerUpdater::toJsonNode() const
  203. {
  204. return JsonNode("BONUS_OWNER_UPDATER");
  205. }
  206. std::shared_ptr<Bonus> OwnerUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
  207. {
  208. PlayerColor owner = context.getOwner();
  209. if(owner == PlayerColor::UNFLAGGABLE)
  210. owner = PlayerColor::NEUTRAL;
  211. std::shared_ptr<Bonus> updated =
  212. std::make_shared<Bonus>(*b);
  213. updated->bonusOwner = owner;
  214. return updated;
  215. }
  216. VCMI_LIB_NAMESPACE_END