BonusList.cpp 5.7 KB

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