BonusList.cpp 6.4 KB

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