BonusList.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * BonusList.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 "CBonusSystemNode.h"
  12. #include "../json/JsonNode.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. void BonusList::stackBonuses()
  15. {
  16. boost::sort(bonuses, [](const std::shared_ptr<Bonus> & b1, const std::shared_ptr<Bonus> & b2) -> bool
  17. {
  18. if(b1 == b2)
  19. return false;
  20. #define COMPARE_ATT(ATT) if(b1->ATT != b2->ATT) return b1->ATT < b2->ATT
  21. COMPARE_ATT(stacking);
  22. COMPARE_ATT(type);
  23. COMPARE_ATT(subtype);
  24. COMPARE_ATT(valType);
  25. #undef COMPARE_ATT
  26. return b1->val > b2->val;
  27. });
  28. // remove non-stacking
  29. size_t next = 1;
  30. while(next < bonuses.size())
  31. {
  32. bool remove = false;
  33. std::shared_ptr<Bonus> last = bonuses[next-1];
  34. std::shared_ptr<Bonus> current = bonuses[next];
  35. if(current->stacking.empty())
  36. remove = current == last;
  37. else if(current->stacking == "ALWAYS")
  38. remove = false;
  39. else
  40. remove = current->stacking == last->stacking
  41. && current->type == last->type
  42. && current->subtype == last->subtype
  43. && current->valType == last->valType;
  44. if(remove)
  45. bonuses.erase(bonuses.begin() + next);
  46. else
  47. next++;
  48. }
  49. }
  50. int BonusList::totalValue(int baseValue) const
  51. {
  52. if (bonuses.empty())
  53. return baseValue;
  54. struct BonusCollection
  55. {
  56. int base = 0;
  57. int percentToBase = 0;
  58. int percentToAll = 0;
  59. int additive = 0;
  60. int percentToSource = 0;
  61. int indepMin = std::numeric_limits<int>::max();
  62. int indepMax = std::numeric_limits<int>::min();
  63. };
  64. auto applyPercentageRoundUp = [](int base, int percent) -> int {
  65. if (base >= 0)
  66. return (static_cast<int64_t>(base) * (100 + percent) + 99) / 100;
  67. else
  68. return (static_cast<int64_t>(base) * (100 + percent) - 99) / 100;
  69. };
  70. auto applyPercentageRoundDown = [](int base, int percent) -> int {
  71. return (static_cast<int64_t>(base) * (100 + percent)) / 100;
  72. };
  73. BonusCollection accumulated;
  74. accumulated.base = baseValue;
  75. int indexMaxCount = 0;
  76. int indexMinCount = 0;
  77. std::array<int, vstd::to_underlying(BonusSource::NUM_BONUS_SOURCE)> percentToSource = {};
  78. for(const auto & b : bonuses)
  79. {
  80. switch(b->valType)
  81. {
  82. case BonusValueType::PERCENT_TO_SOURCE:
  83. percentToSource[vstd::to_underlying(b->source)] += b->val;
  84. break;
  85. case BonusValueType::PERCENT_TO_TARGET_TYPE:
  86. percentToSource[vstd::to_underlying(b->targetSourceType)] += b->val;
  87. break;
  88. }
  89. }
  90. for(const auto & b : bonuses)
  91. {
  92. int sourceIndex = vstd::to_underlying(b->source);
  93. // Workaround: creature hero specialties in H3 is the only place that uses rounding up in bonuses
  94. // TODO: try to find more elegant solution?
  95. int valModified = b->source == BonusSource::CREATURE_ABILITY ?
  96. applyPercentageRoundUp(b->val, percentToSource[sourceIndex]):
  97. applyPercentageRoundDown(b->val, percentToSource[sourceIndex]);
  98. switch(b->valType)
  99. {
  100. case BonusValueType::BASE_NUMBER:
  101. accumulated.base += valModified;
  102. break;
  103. case BonusValueType::PERCENT_TO_ALL:
  104. accumulated.percentToAll += valModified;
  105. break;
  106. case BonusValueType::PERCENT_TO_BASE:
  107. accumulated.percentToBase += valModified;
  108. break;
  109. case BonusValueType::ADDITIVE_VALUE:
  110. accumulated.additive += valModified;
  111. break;
  112. case BonusValueType::INDEPENDENT_MAX: // actual meaning: at least this value
  113. indexMaxCount++;
  114. vstd::amax(accumulated.indepMax, valModified);
  115. break;
  116. case BonusValueType::INDEPENDENT_MIN: // actual meaning: at most this value
  117. indexMinCount++;
  118. vstd::amin(accumulated.indepMin, valModified);
  119. break;
  120. }
  121. }
  122. accumulated.base = applyPercentageRoundDown(accumulated.base, accumulated.percentToBase);
  123. accumulated.base += accumulated.additive;
  124. auto valFirst = applyPercentageRoundDown(accumulated.base ,accumulated.percentToAll);
  125. if(indexMinCount && indexMaxCount && accumulated.indepMin < accumulated.indepMax)
  126. accumulated.indepMax = accumulated.indepMin;
  127. const int notIndepBonuses = bonuses.size() - indexMaxCount - indexMinCount;
  128. if(notIndepBonuses)
  129. return std::clamp(valFirst, accumulated.indepMax, accumulated.indepMin);
  130. if (indexMinCount)
  131. return accumulated.indepMin;
  132. if (indexMaxCount)
  133. return accumulated.indepMax;
  134. return 0;
  135. }
  136. std::shared_ptr<Bonus> BonusList::getFirst(const CSelector &select)
  137. {
  138. for (auto & b : bonuses)
  139. {
  140. if(select(b.get()))
  141. return b;
  142. }
  143. return nullptr;
  144. }
  145. std::shared_ptr<const Bonus> BonusList::getFirst(const CSelector &selector) const
  146. {
  147. for(const auto & b : bonuses)
  148. {
  149. if(selector(b.get()))
  150. return b;
  151. }
  152. return nullptr;
  153. }
  154. void BonusList::getBonuses(BonusList & out, const CSelector &selector) const
  155. {
  156. for(const auto & b : bonuses)
  157. {
  158. if(selector(b.get()))
  159. out.push_back(b);
  160. }
  161. }
  162. void BonusList::getAllBonuses(BonusList &out) const
  163. {
  164. for(const auto & b : bonuses)
  165. out.push_back(b);
  166. }
  167. int BonusList::valOfBonuses(const CSelector &select, int baseValue) const
  168. {
  169. BonusList ret;
  170. getBonuses(ret, select);
  171. return ret.totalValue(baseValue);
  172. }
  173. JsonNode BonusList::toJsonNode() const
  174. {
  175. JsonNode node;
  176. for(const std::shared_ptr<Bonus> & b : bonuses)
  177. node.Vector().push_back(b->toJsonNode());
  178. return node;
  179. }
  180. void BonusList::push_back(const std::shared_ptr<Bonus> & x)
  181. {
  182. bonuses.push_back(x);
  183. }
  184. BonusList::TInternalContainer::iterator BonusList::erase(const int position)
  185. {
  186. return bonuses.erase(bonuses.begin() + position);
  187. }
  188. void BonusList::clear()
  189. {
  190. bonuses.clear();
  191. }
  192. std::vector<BonusList *>::size_type BonusList::operator-=(const std::shared_ptr<Bonus> & i)
  193. {
  194. auto itr = std::find(bonuses.begin(), bonuses.end(), i);
  195. if(itr == bonuses.end())
  196. return false;
  197. bonuses.erase(itr);
  198. return true;
  199. }
  200. void BonusList::resize(BonusList::TInternalContainer::size_type sz, const std::shared_ptr<Bonus> & c)
  201. {
  202. bonuses.resize(sz, c);
  203. }
  204. void BonusList::insert(BonusList::TInternalContainer::iterator position, BonusList::TInternalContainer::size_type n, const std::shared_ptr<Bonus> & x)
  205. {
  206. bonuses.insert(position, n, x);
  207. }
  208. DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const BonusList &bonusList)
  209. {
  210. for (ui32 i = 0; i < bonusList.size(); i++)
  211. {
  212. const auto & b = bonusList[i];
  213. out << "Bonus " << i << "\n" << *b << std::endl;
  214. }
  215. return out;
  216. }
  217. VCMI_LIB_NAMESPACE_END