Updaters.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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> TimesArmySizeUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
  128. {
  129. if(context.getNodeType() == BonusNodeType::ARMY || context.getNodeType() == BonusNodeType::HERO || context.getNodeType() == BonusNodeType::TOWN)
  130. {
  131. const auto & army = dynamic_cast<const CArmedInstance &>(context);
  132. int totalSize = 0;
  133. for (const auto & unit : army.Slots())
  134. {
  135. if (filteredCreature.hasValue() && filteredCreature != unit.second->getCreatureID())
  136. continue;
  137. if (filteredFaction.hasValue() && filteredFaction != unit.second->getFactionID())
  138. continue;
  139. if (filteredLevel != -1 && filteredLevel != unit.second->getLevel())
  140. continue;
  141. totalSize += unit.second->getCount();
  142. }
  143. auto newBonus = std::make_shared<Bonus>(*b);
  144. newBonus->val *= std::clamp(totalSize / stepSize, minimum, maximum);
  145. return newBonus;
  146. }
  147. return b;
  148. }
  149. std::string TimesArmySizeUpdater::toString() const
  150. {
  151. return "TimesArmySizeUpdater";
  152. }
  153. JsonNode TimesArmySizeUpdater::toJsonNode() const
  154. {
  155. return JsonNode("TIMES_ARMY_SIZE");
  156. }
  157. std::shared_ptr<Bonus> TimesStackLevelUpdater::apply(const std::shared_ptr<Bonus> & b, int level) const
  158. {
  159. auto newBonus = std::make_shared<Bonus>(*b);
  160. newBonus->val *= level;
  161. newBonus->updater = nullptr; // prevent double-apply
  162. return newBonus;
  163. }
  164. std::shared_ptr<Bonus> TimesStackLevelUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
  165. {
  166. if(context.getNodeType() == BonusNodeType::STACK_INSTANCE || context.getNodeType() == BonusNodeType::COMMANDER)
  167. {
  168. int level = dynamic_cast<const CStackInstance &>(context).getLevel();
  169. return apply(b, level);
  170. }
  171. if(context.getNodeType() == BonusNodeType::STACK_BATTLE)
  172. {
  173. const auto & stack = dynamic_cast<const CStack &>(context);
  174. //update if stack doesn't have an instance (summons, war machines)
  175. if(stack.base == nullptr)
  176. return apply(b, stack.unitType()->getLevel());
  177. // If these are not handled here, the final outcome may potentially be incorrect.
  178. int level = dynamic_cast<const CStackInstance*>(stack.base)->getLevel();
  179. return apply(b, level);
  180. }
  181. return b;
  182. }
  183. std::string TimesStackLevelUpdater::toString() const
  184. {
  185. return "TimesStackLevelUpdater";
  186. }
  187. JsonNode TimesStackLevelUpdater::toJsonNode() const
  188. {
  189. return JsonNode("TIMES_STACK_LEVEL");
  190. }
  191. std::shared_ptr<Bonus> DivideStackLevelUpdater::apply(const std::shared_ptr<Bonus> & b, int level) const
  192. {
  193. if (level == 0)
  194. return b; // e.g. war machines & other special units
  195. auto newBonus = std::make_shared<Bonus>(*b);
  196. level = std::max(1, level);
  197. newBonus->val /= level;
  198. newBonus->updater = nullptr; // prevent double-apply
  199. return newBonus;
  200. }
  201. std::shared_ptr<Bonus> DivideStackLevelUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
  202. {
  203. if(context.getNodeType() == BonusNodeType::STACK_INSTANCE || context.getNodeType() == BonusNodeType::COMMANDER)
  204. {
  205. int level = dynamic_cast<const CStackInstance &>(context).getLevel();
  206. return apply(b, level);
  207. }
  208. if(context.getNodeType() == BonusNodeType::STACK_BATTLE)
  209. {
  210. const auto & stack = dynamic_cast<const CStack &>(context);
  211. //update if stack doesn't have an instance (summons, war machines)
  212. if(stack.base == nullptr)
  213. return apply(b, stack.unitType()->getLevel());
  214. // If these are not handled here, the final outcome may potentially be incorrect.
  215. int level = dynamic_cast<const CStackInstance*>(stack.base)->getLevel();
  216. return apply(b, level);
  217. }
  218. return b;
  219. }
  220. std::string DivideStackLevelUpdater::toString() const
  221. {
  222. return "DivideStackLevelUpdater";
  223. }
  224. JsonNode DivideStackLevelUpdater::toJsonNode() const
  225. {
  226. return JsonNode("DIVIDE_STACK_LEVEL");
  227. }
  228. std::string OwnerUpdater::toString() const
  229. {
  230. return "OwnerUpdater";
  231. }
  232. JsonNode OwnerUpdater::toJsonNode() const
  233. {
  234. return JsonNode("BONUS_OWNER_UPDATER");
  235. }
  236. std::shared_ptr<Bonus> OwnerUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
  237. {
  238. PlayerColor owner = context.getOwner();
  239. if(owner == PlayerColor::UNFLAGGABLE)
  240. owner = PlayerColor::NEUTRAL;
  241. std::shared_ptr<Bonus> updated =
  242. std::make_shared<Bonus>(*b);
  243. updated->bonusOwner = owner;
  244. return updated;
  245. }
  246. VCMI_LIB_NAMESPACE_END