Updaters.cpp 6.0 KB

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