Updaters.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 = vstd::divideAndCeil(valPer20 * steps, 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. // FIXME: current logic is (val * hero level) / stack level
  86. // However H3 logic is val * (hero level / stack level)
  87. // Probably needs creation of temporary limiter & passing hero level into it, to perform all math in one place
  88. auto newBonus = TimesHeroLevelUpdater::createUpdatedBonus(b, context);
  89. newBonus->updater = divideStackLevel;
  90. return newBonus;
  91. }
  92. return b;
  93. }
  94. std::string TimesHeroLevelDivideStackLevelUpdater::toString() const
  95. {
  96. return "TimesHeroLevelDivideStackLevelUpdater";
  97. }
  98. JsonNode TimesHeroLevelDivideStackLevelUpdater::toJsonNode() const
  99. {
  100. return JsonNode("TIMES_HERO_LEVEL_DIVIDE_STACK_LEVEL");
  101. }
  102. std::shared_ptr<Bonus> TimesStackSizeUpdater::apply(const std::shared_ptr<Bonus> & b, int count) const
  103. {
  104. auto newBonus = std::make_shared<Bonus>(*b);
  105. newBonus->val = stepValue * std::clamp(count / stepSize, minimum, maximum);
  106. return newBonus;
  107. }
  108. std::shared_ptr<Bonus> TimesStackSizeUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
  109. {
  110. if(context.getNodeType() == BonusNodeType::STACK_INSTANCE || context.getNodeType() == BonusNodeType::COMMANDER)
  111. {
  112. int count = dynamic_cast<const CStackInstance &>(context).getCount();
  113. return apply(b, count);
  114. }
  115. if(context.getNodeType() == BonusNodeType::STACK_BATTLE)
  116. {
  117. const auto & stack = dynamic_cast<const CStack &>(context);
  118. return apply(b, stack.getCount());
  119. }
  120. return b;
  121. }
  122. std::string TimesStackSizeUpdater::toString() const
  123. {
  124. return "TimesStackSizeUpdater";
  125. }
  126. JsonNode TimesStackSizeUpdater::toJsonNode() const
  127. {
  128. return JsonNode("TIMES_STACK_SIZE");
  129. }
  130. std::shared_ptr<Bonus> TimesArmySizeUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
  131. {
  132. if(context.getNodeType() == BonusNodeType::ARMY || context.getNodeType() == BonusNodeType::HERO || context.getNodeType() == BonusNodeType::TOWN)
  133. {
  134. const auto & army = dynamic_cast<const CArmedInstance &>(context);
  135. int totalSize = 0;
  136. for (const auto & unit : army.Slots())
  137. {
  138. if (filteredCreature.hasValue() && filteredCreature != unit.second->getCreatureID())
  139. continue;
  140. if (filteredFaction.hasValue() && filteredFaction != unit.second->getFactionID())
  141. continue;
  142. if (filteredLevel != -1 && filteredLevel != unit.second->getLevel())
  143. continue;
  144. totalSize += unit.second->getCount();
  145. }
  146. auto newBonus = std::make_shared<Bonus>(*b);
  147. newBonus->val *= std::clamp(totalSize / stepSize, minimum, maximum);
  148. return newBonus;
  149. }
  150. return b;
  151. }
  152. std::string TimesArmySizeUpdater::toString() const
  153. {
  154. return "TimesArmySizeUpdater";
  155. }
  156. JsonNode TimesArmySizeUpdater::toJsonNode() const
  157. {
  158. return JsonNode("TIMES_ARMY_SIZE");
  159. }
  160. std::shared_ptr<Bonus> TimesStackLevelUpdater::apply(const std::shared_ptr<Bonus> & b, int level) const
  161. {
  162. auto newBonus = std::make_shared<Bonus>(*b);
  163. newBonus->val *= level;
  164. newBonus->updater = nullptr; // prevent double-apply
  165. return newBonus;
  166. }
  167. std::shared_ptr<Bonus> TimesStackLevelUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
  168. {
  169. if(context.getNodeType() == BonusNodeType::STACK_INSTANCE || context.getNodeType() == BonusNodeType::COMMANDER)
  170. {
  171. int level = dynamic_cast<const CStackInstance &>(context).getLevel();
  172. return apply(b, level);
  173. }
  174. if(context.getNodeType() == BonusNodeType::STACK_BATTLE)
  175. {
  176. const auto & stack = dynamic_cast<const CStack &>(context);
  177. //update if stack doesn't have an instance (summons, war machines)
  178. if(stack.base == nullptr)
  179. return apply(b, stack.unitType()->getLevel());
  180. // If these are not handled here, the final outcome may potentially be incorrect.
  181. int level = dynamic_cast<const CStackInstance*>(stack.base)->getLevel();
  182. return apply(b, level);
  183. }
  184. return b;
  185. }
  186. std::string TimesStackLevelUpdater::toString() const
  187. {
  188. return "TimesStackLevelUpdater";
  189. }
  190. JsonNode TimesStackLevelUpdater::toJsonNode() const
  191. {
  192. return JsonNode("TIMES_STACK_LEVEL");
  193. }
  194. std::shared_ptr<Bonus> DivideStackLevelUpdater::apply(const std::shared_ptr<Bonus> & b, int level) const
  195. {
  196. if (level == 0)
  197. return b; // e.g. war machines & other special units
  198. auto newBonus = std::make_shared<Bonus>(*b);
  199. level = std::max(1, level);
  200. newBonus->val /= level;
  201. newBonus->updater = nullptr; // prevent double-apply
  202. return newBonus;
  203. }
  204. std::shared_ptr<Bonus> DivideStackLevelUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
  205. {
  206. if(context.getNodeType() == BonusNodeType::STACK_INSTANCE || context.getNodeType() == BonusNodeType::COMMANDER)
  207. {
  208. int level = dynamic_cast<const CStackInstance &>(context).getLevel();
  209. return apply(b, level);
  210. }
  211. if(context.getNodeType() == BonusNodeType::STACK_BATTLE)
  212. {
  213. const auto & stack = dynamic_cast<const CStack &>(context);
  214. //update if stack doesn't have an instance (summons, war machines)
  215. if(stack.base == nullptr)
  216. return apply(b, stack.unitType()->getLevel());
  217. // If these are not handled here, the final outcome may potentially be incorrect.
  218. int level = dynamic_cast<const CStackInstance*>(stack.base)->getLevel();
  219. return apply(b, level);
  220. }
  221. return b;
  222. }
  223. std::string DivideStackLevelUpdater::toString() const
  224. {
  225. return "DivideStackLevelUpdater";
  226. }
  227. JsonNode DivideStackLevelUpdater::toJsonNode() const
  228. {
  229. return JsonNode("DIVIDE_STACK_LEVEL");
  230. }
  231. std::string OwnerUpdater::toString() const
  232. {
  233. return "OwnerUpdater";
  234. }
  235. JsonNode OwnerUpdater::toJsonNode() const
  236. {
  237. return JsonNode("BONUS_OWNER_UPDATER");
  238. }
  239. std::shared_ptr<Bonus> OwnerUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
  240. {
  241. PlayerColor owner = context.getOwner();
  242. if(owner == PlayerColor::UNFLAGGABLE)
  243. owner = PlayerColor::NEUTRAL;
  244. std::shared_ptr<Bonus> updated =
  245. std::make_shared<Bonus>(*b);
  246. updated->bonusOwner = owner;
  247. return updated;
  248. }
  249. std::string CompositeUpdater::toString() const
  250. {
  251. return "CompositeUpdater";
  252. }
  253. JsonNode CompositeUpdater::toJsonNode() const
  254. {
  255. return JsonNode("COMPOSITE_UPDATER");
  256. }
  257. std::shared_ptr<Bonus> CompositeUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
  258. {
  259. std::shared_ptr<Bonus> result = b;
  260. for (const auto & updater : updaters)
  261. result = updater->createUpdatedBonus(result, context);
  262. return result;
  263. }
  264. VCMI_LIB_NAMESPACE_END