Updaters.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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() == CBonusSystemNode::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() == CBonusSystemNode::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() == CBonusSystemNode::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. newBonus->updater = nullptr; // prevent double-apply
  104. return newBonus;
  105. }
  106. std::shared_ptr<Bonus> TimesStackSizeUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
  107. {
  108. if(context.getNodeType() == CBonusSystemNode::STACK_INSTANCE || context.getNodeType() == CBonusSystemNode::COMMANDER)
  109. {
  110. int count = dynamic_cast<const CStackInstance &>(context).getCount();
  111. return apply(b, count);
  112. }
  113. if(context.getNodeType() == CBonusSystemNode::STACK_BATTLE)
  114. {
  115. const auto & stack = dynamic_cast<const CStack &>(context);
  116. return apply(b, stack.getCount());
  117. }
  118. return b;
  119. }
  120. std::string TimesStackSizeUpdater::toString() const
  121. {
  122. return "TimesStackSizeUpdater";
  123. }
  124. JsonNode TimesStackSizeUpdater::toJsonNode() const
  125. {
  126. return JsonNode("TIMES_STACK_SIZE");
  127. }
  128. std::shared_ptr<Bonus> TimesStackLevelUpdater::apply(const std::shared_ptr<Bonus> & b, int level) const
  129. {
  130. auto newBonus = std::make_shared<Bonus>(*b);
  131. newBonus->val *= level;
  132. newBonus->updater = nullptr; // prevent double-apply
  133. return newBonus;
  134. }
  135. std::shared_ptr<Bonus> TimesStackLevelUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
  136. {
  137. if(context.getNodeType() == CBonusSystemNode::STACK_INSTANCE || context.getNodeType() == CBonusSystemNode::COMMANDER)
  138. {
  139. int level = dynamic_cast<const CStackInstance &>(context).getLevel();
  140. return apply(b, level);
  141. }
  142. if(context.getNodeType() == CBonusSystemNode::STACK_BATTLE)
  143. {
  144. const auto & stack = dynamic_cast<const CStack &>(context);
  145. //update if stack doesn't have an instance (summons, war machines)
  146. if(stack.base == nullptr)
  147. return apply(b, stack.unitType()->getLevel());
  148. // If these are not handled here, the final outcome may potentially be incorrect.
  149. int level = dynamic_cast<const CStackInstance*>(stack.base)->getLevel();
  150. return apply(b, level);
  151. }
  152. return b;
  153. }
  154. std::string TimesStackLevelUpdater::toString() const
  155. {
  156. return "TimesStackLevelUpdater";
  157. }
  158. JsonNode TimesStackLevelUpdater::toJsonNode() const
  159. {
  160. return JsonNode("TIMES_STACK_LEVEL");
  161. }
  162. std::shared_ptr<Bonus> DivideStackLevelUpdater::apply(const std::shared_ptr<Bonus> & b, int level) const
  163. {
  164. if (level == 0)
  165. return b; // e.g. war machines & other special units
  166. auto newBonus = std::make_shared<Bonus>(*b);
  167. level = std::max(1, level);
  168. newBonus->val /= level;
  169. newBonus->updater = nullptr; // prevent double-apply
  170. return newBonus;
  171. }
  172. std::shared_ptr<Bonus> DivideStackLevelUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
  173. {
  174. if(context.getNodeType() == CBonusSystemNode::STACK_INSTANCE || context.getNodeType() == CBonusSystemNode::COMMANDER)
  175. {
  176. int level = dynamic_cast<const CStackInstance &>(context).getLevel();
  177. return apply(b, level);
  178. }
  179. if(context.getNodeType() == CBonusSystemNode::STACK_BATTLE)
  180. {
  181. const auto & stack = dynamic_cast<const CStack &>(context);
  182. //update if stack doesn't have an instance (summons, war machines)
  183. if(stack.base == nullptr)
  184. return apply(b, stack.unitType()->getLevel());
  185. // If these are not handled here, the final outcome may potentially be incorrect.
  186. int level = dynamic_cast<const CStackInstance*>(stack.base)->getLevel();
  187. return apply(b, level);
  188. }
  189. return b;
  190. }
  191. std::string DivideStackLevelUpdater::toString() const
  192. {
  193. return "DivideStackLevelUpdater";
  194. }
  195. JsonNode DivideStackLevelUpdater::toJsonNode() const
  196. {
  197. return JsonNode("DIVIDE_STACK_LEVEL");
  198. }
  199. std::string OwnerUpdater::toString() const
  200. {
  201. return "OwnerUpdater";
  202. }
  203. JsonNode OwnerUpdater::toJsonNode() const
  204. {
  205. return JsonNode("BONUS_OWNER_UPDATER");
  206. }
  207. std::shared_ptr<Bonus> OwnerUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
  208. {
  209. PlayerColor owner = context.getOwner();
  210. if(owner == PlayerColor::UNFLAGGABLE)
  211. owner = PlayerColor::NEUTRAL;
  212. std::shared_ptr<Bonus> updated =
  213. std::make_shared<Bonus>(*b);
  214. updated->bonusOwner = owner;
  215. return updated;
  216. }
  217. VCMI_LIB_NAMESPACE_END