BonusList.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. bool hasIndepMax = false;
  94. bool hasIndepMin = false;
  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:
  127. hasIndepMax = true;
  128. vstd::amax(accumulated.indepMax, valModified);
  129. break;
  130. case BonusValueType::INDEPENDENT_MIN:
  131. hasIndepMin = true;
  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(hasIndepMin && hasIndepMax && accumulated.indepMin < accumulated.indepMax)
  140. accumulated.indepMax = accumulated.indepMin;
  141. const int notIndepBonuses = static_cast<int>(std::count_if(bonuses.cbegin(), bonuses.cend(), [](const std::shared_ptr<Bonus>& b)
  142. {
  143. return b->valType != BonusValueType::INDEPENDENT_MAX && b->valType != BonusValueType::INDEPENDENT_MIN;
  144. }));
  145. if(notIndepBonuses)
  146. return std::clamp(valFirst, accumulated.indepMax, accumulated.indepMin);
  147. if (hasIndepMin)
  148. return accumulated.indepMin;
  149. if (hasIndepMax)
  150. return accumulated.indepMax;
  151. return 0;
  152. }
  153. std::shared_ptr<Bonus> BonusList::getFirst(const CSelector &select)
  154. {
  155. for (auto & b : bonuses)
  156. {
  157. if(select(b.get()))
  158. return b;
  159. }
  160. return nullptr;
  161. }
  162. std::shared_ptr<const Bonus> BonusList::getFirst(const CSelector &selector) const
  163. {
  164. for(const auto & b : bonuses)
  165. {
  166. if(selector(b.get()))
  167. return b;
  168. }
  169. return nullptr;
  170. }
  171. void BonusList::getBonuses(BonusList & out, const CSelector &selector, const CSelector &limit) const
  172. {
  173. out.reserve(bonuses.size());
  174. for(const auto & b : bonuses)
  175. {
  176. if(selector(b.get()) && (!limit || ((bool)limit && limit(b.get()))))
  177. out.push_back(b);
  178. }
  179. }
  180. void BonusList::getAllBonuses(BonusList &out) const
  181. {
  182. for(const auto & b : bonuses)
  183. out.push_back(b);
  184. }
  185. int BonusList::valOfBonuses(const CSelector &select) const
  186. {
  187. BonusList ret;
  188. CSelector limit = nullptr;
  189. getBonuses(ret, select, limit);
  190. return ret.totalValue();
  191. }
  192. JsonNode BonusList::toJsonNode() const
  193. {
  194. JsonNode node;
  195. for(const std::shared_ptr<Bonus> & b : bonuses)
  196. node.Vector().push_back(b->toJsonNode());
  197. return node;
  198. }
  199. void BonusList::push_back(const std::shared_ptr<Bonus> & x)
  200. {
  201. bonuses.push_back(x);
  202. changed();
  203. }
  204. BonusList::TInternalContainer::iterator BonusList::erase(const int position)
  205. {
  206. changed();
  207. return bonuses.erase(bonuses.begin() + position);
  208. }
  209. void BonusList::clear()
  210. {
  211. bonuses.clear();
  212. changed();
  213. }
  214. std::vector<BonusList *>::size_type BonusList::operator-=(const std::shared_ptr<Bonus> & i)
  215. {
  216. auto itr = std::find(bonuses.begin(), bonuses.end(), i);
  217. if(itr == bonuses.end())
  218. return false;
  219. bonuses.erase(itr);
  220. changed();
  221. return true;
  222. }
  223. void BonusList::resize(BonusList::TInternalContainer::size_type sz, const std::shared_ptr<Bonus> & c)
  224. {
  225. bonuses.resize(sz, c);
  226. changed();
  227. }
  228. void BonusList::reserve(TInternalContainer::size_type sz)
  229. {
  230. bonuses.reserve(sz);
  231. }
  232. void BonusList::insert(BonusList::TInternalContainer::iterator position, BonusList::TInternalContainer::size_type n, const std::shared_ptr<Bonus> & x)
  233. {
  234. bonuses.insert(position, n, x);
  235. changed();
  236. }
  237. DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const BonusList &bonusList)
  238. {
  239. for (ui32 i = 0; i < bonusList.size(); i++)
  240. {
  241. const auto & b = bonusList[i];
  242. out << "Bonus " << i << "\n" << *b << std::endl;
  243. }
  244. return out;
  245. }
  246. VCMI_LIB_NAMESPACE_END