BonusList.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. return (static_cast<int64_t>(base) * (100 + percent) + 99) / 100;
  66. };
  67. auto applyPercentageRoundDown = [](int base, int percent) -> int {
  68. return (static_cast<int64_t>(base) * (100 + percent)) / 100;
  69. };
  70. BonusCollection accumulated;
  71. accumulated.base = baseValue;
  72. int indexMaxCount = 0;
  73. int indexMinCount = 0;
  74. std::array<int, vstd::to_underlying(BonusSource::NUM_BONUS_SOURCE)> percentToSource = {};
  75. for(const auto & b : bonuses)
  76. {
  77. switch(b->valType)
  78. {
  79. case BonusValueType::PERCENT_TO_SOURCE:
  80. percentToSource[vstd::to_underlying(b->source)] += b->val;
  81. break;
  82. case BonusValueType::PERCENT_TO_TARGET_TYPE:
  83. percentToSource[vstd::to_underlying(b->targetSourceType)] += b->val;
  84. break;
  85. }
  86. }
  87. for(const auto & b : bonuses)
  88. {
  89. int sourceIndex = vstd::to_underlying(b->source);
  90. // Workaround: creature hero specialties in H3 is the only place that uses rounding up in bonuses
  91. // TODO: try to find more elegant solution?
  92. int valModified = b->source == BonusSource::CREATURE_ABILITY ?
  93. applyPercentageRoundUp(b->val, percentToSource[sourceIndex]):
  94. applyPercentageRoundDown(b->val, percentToSource[sourceIndex]);
  95. switch(b->valType)
  96. {
  97. case BonusValueType::BASE_NUMBER:
  98. accumulated.base += valModified;
  99. break;
  100. case BonusValueType::PERCENT_TO_ALL:
  101. accumulated.percentToAll += valModified;
  102. break;
  103. case BonusValueType::PERCENT_TO_BASE:
  104. accumulated.percentToBase += valModified;
  105. break;
  106. case BonusValueType::ADDITIVE_VALUE:
  107. accumulated.additive += valModified;
  108. break;
  109. case BonusValueType::INDEPENDENT_MAX: // actual meaning: at least this value
  110. indexMaxCount++;
  111. vstd::amax(accumulated.indepMax, valModified);
  112. break;
  113. case BonusValueType::INDEPENDENT_MIN: // actual meaning: at most this value
  114. indexMinCount++;
  115. vstd::amin(accumulated.indepMin, valModified);
  116. break;
  117. }
  118. }
  119. accumulated.base = applyPercentageRoundDown(accumulated.base, accumulated.percentToBase);
  120. accumulated.base += accumulated.additive;
  121. auto valFirst = applyPercentageRoundDown(accumulated.base ,accumulated.percentToAll);
  122. if(indexMinCount && indexMaxCount && accumulated.indepMin < accumulated.indepMax)
  123. accumulated.indepMax = accumulated.indepMin;
  124. const int notIndepBonuses = bonuses.size() - indexMaxCount - indexMinCount;
  125. if(notIndepBonuses)
  126. return std::clamp(valFirst, accumulated.indepMax, accumulated.indepMin);
  127. if (indexMinCount)
  128. return accumulated.indepMin;
  129. if (indexMaxCount)
  130. return accumulated.indepMax;
  131. return 0;
  132. }
  133. std::shared_ptr<Bonus> BonusList::getFirst(const CSelector &select)
  134. {
  135. for (auto & b : bonuses)
  136. {
  137. if(select(b.get()))
  138. return b;
  139. }
  140. return nullptr;
  141. }
  142. std::shared_ptr<const Bonus> BonusList::getFirst(const CSelector &selector) const
  143. {
  144. for(const auto & b : bonuses)
  145. {
  146. if(selector(b.get()))
  147. return b;
  148. }
  149. return nullptr;
  150. }
  151. void BonusList::getBonuses(BonusList & out, const CSelector &selector, const CSelector &limit) const
  152. {
  153. for(const auto & b : bonuses)
  154. {
  155. if(selector(b.get()) && (!limit || ((bool)limit && limit(b.get()))))
  156. out.push_back(b);
  157. }
  158. }
  159. void BonusList::getAllBonuses(BonusList &out) const
  160. {
  161. for(const auto & b : bonuses)
  162. out.push_back(b);
  163. }
  164. int BonusList::valOfBonuses(const CSelector &select, int baseValue) const
  165. {
  166. BonusList ret;
  167. CSelector limit = nullptr;
  168. getBonuses(ret, select, limit);
  169. return ret.totalValue(baseValue);
  170. }
  171. JsonNode BonusList::toJsonNode() const
  172. {
  173. JsonNode node;
  174. for(const std::shared_ptr<Bonus> & b : bonuses)
  175. node.Vector().push_back(b->toJsonNode());
  176. return node;
  177. }
  178. void BonusList::push_back(const std::shared_ptr<Bonus> & x)
  179. {
  180. bonuses.push_back(x);
  181. }
  182. BonusList::TInternalContainer::iterator BonusList::erase(const int position)
  183. {
  184. return bonuses.erase(bonuses.begin() + position);
  185. }
  186. void BonusList::clear()
  187. {
  188. bonuses.clear();
  189. }
  190. std::vector<BonusList *>::size_type BonusList::operator-=(const std::shared_ptr<Bonus> & i)
  191. {
  192. auto itr = std::find(bonuses.begin(), bonuses.end(), i);
  193. if(itr == bonuses.end())
  194. return false;
  195. bonuses.erase(itr);
  196. return true;
  197. }
  198. void BonusList::resize(BonusList::TInternalContainer::size_type sz, const std::shared_ptr<Bonus> & c)
  199. {
  200. bonuses.resize(sz, c);
  201. }
  202. void BonusList::insert(BonusList::TInternalContainer::iterator position, BonusList::TInternalContainer::size_type n, const std::shared_ptr<Bonus> & x)
  203. {
  204. bonuses.insert(position, n, x);
  205. }
  206. DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const BonusList &bonusList)
  207. {
  208. for (ui32 i = 0; i < bonusList.size(); i++)
  209. {
  210. const auto & b = bonusList[i];
  211. out << "Bonus " << i << "\n" << *b << std::endl;
  212. }
  213. return out;
  214. }
  215. VCMI_LIB_NAMESPACE_END