BonusList.cpp 7.1 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. struct BonusCollection
  78. {
  79. int base = 0;
  80. int percentToBase = 0;
  81. int percentToAll = 0;
  82. int additive = 0;
  83. int percentToSource = 0;
  84. int indepMin = std::numeric_limits<int>::max();
  85. int indepMax = std::numeric_limits<int>::min();
  86. };
  87. auto percent = [](int64_t base, int64_t percent) -> int {
  88. return static_cast<int>(std::clamp<int64_t>((base * (100 + percent)) / 100, std::numeric_limits<int>::min(), std::numeric_limits<int>::max()));
  89. };
  90. std::array <BonusCollection, vstd::to_underlying(BonusSource::NUM_BONUS_SOURCE)> sources = {};
  91. BonusCollection any;
  92. bool hasIndepMax = false;
  93. bool hasIndepMin = false;
  94. for(const auto & b : bonuses)
  95. {
  96. switch(b->valType)
  97. {
  98. case BonusValueType::BASE_NUMBER:
  99. sources[vstd::to_underlying(b->source)].base += b->val;
  100. break;
  101. case BonusValueType::PERCENT_TO_ALL:
  102. sources[vstd::to_underlying(b->source)].percentToAll += b->val;
  103. break;
  104. case BonusValueType::PERCENT_TO_BASE:
  105. sources[vstd::to_underlying(b->source)].percentToBase += b->val;
  106. break;
  107. case BonusValueType::PERCENT_TO_SOURCE:
  108. sources[vstd::to_underlying(b->source)].percentToSource += b->val;
  109. break;
  110. case BonusValueType::PERCENT_TO_TARGET_TYPE:
  111. sources[vstd::to_underlying(b->targetSourceType)].percentToSource += b->val;
  112. break;
  113. case BonusValueType::ADDITIVE_VALUE:
  114. sources[vstd::to_underlying(b->source)].additive += b->val;
  115. break;
  116. case BonusValueType::INDEPENDENT_MAX:
  117. hasIndepMax = true;
  118. vstd::amax(sources[vstd::to_underlying(b->source)].indepMax, b->val);
  119. break;
  120. case BonusValueType::INDEPENDENT_MIN:
  121. hasIndepMin = true;
  122. vstd::amin(sources[vstd::to_underlying(b->source)].indepMin, b->val);
  123. break;
  124. }
  125. }
  126. for(const auto & src : sources)
  127. {
  128. any.base += percent(src.base, src.percentToSource);
  129. any.percentToBase += percent(src.percentToBase, src.percentToSource);
  130. any.percentToAll += percent(src.percentToAll, src.percentToSource);
  131. any.additive += percent(src.additive, src.percentToSource);
  132. if(hasIndepMin)
  133. vstd::amin(any.indepMin, percent(src.indepMin, src.percentToSource));
  134. if(hasIndepMax)
  135. vstd::amax(any.indepMax, percent(src.indepMax, src.percentToSource));
  136. }
  137. any.base = percent(any.base, any.percentToBase);
  138. any.base += any.additive;
  139. auto valFirst = percent(any.base ,any.percentToAll);
  140. if(hasIndepMin && hasIndepMax && any.indepMin < any.indepMax)
  141. any.indepMax = any.indepMin;
  142. const int notIndepBonuses = static_cast<int>(std::count_if(bonuses.cbegin(), bonuses.cend(), [](const std::shared_ptr<Bonus>& b)
  143. {
  144. return b->valType != BonusValueType::INDEPENDENT_MAX && b->valType != BonusValueType::INDEPENDENT_MIN;
  145. }));
  146. if(notIndepBonuses)
  147. return std::clamp(valFirst, any.indepMax, any.indepMin);
  148. return hasIndepMin ? any.indepMin : hasIndepMax ? any.indepMax : 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. out.reserve(bonuses.size());
  171. for(const auto & b : bonuses)
  172. {
  173. //add matching bonuses that matches limit predicate or have NO_LIMIT if no given predicate
  174. auto noFightLimit = b->effectRange == BonusLimitEffect::NO_LIMIT;
  175. if(selector(b.get()) && ((!limit && noFightLimit) || ((bool)limit && limit(b.get()))))
  176. out.push_back(b);
  177. }
  178. }
  179. void BonusList::getAllBonuses(BonusList &out) const
  180. {
  181. for(const auto & b : bonuses)
  182. out.push_back(b);
  183. }
  184. int BonusList::valOfBonuses(const CSelector &select) const
  185. {
  186. BonusList ret;
  187. CSelector limit = nullptr;
  188. getBonuses(ret, select, limit);
  189. return ret.totalValue();
  190. }
  191. JsonNode BonusList::toJsonNode() const
  192. {
  193. JsonNode node;
  194. for(const std::shared_ptr<Bonus> & b : bonuses)
  195. node.Vector().push_back(b->toJsonNode());
  196. return node;
  197. }
  198. void BonusList::push_back(const std::shared_ptr<Bonus> & x)
  199. {
  200. bonuses.push_back(x);
  201. changed();
  202. }
  203. BonusList::TInternalContainer::iterator BonusList::erase(const int position)
  204. {
  205. changed();
  206. return bonuses.erase(bonuses.begin() + position);
  207. }
  208. void BonusList::clear()
  209. {
  210. bonuses.clear();
  211. changed();
  212. }
  213. std::vector<BonusList *>::size_type BonusList::operator-=(const std::shared_ptr<Bonus> & i)
  214. {
  215. auto itr = std::find(bonuses.begin(), bonuses.end(), i);
  216. if(itr == bonuses.end())
  217. return false;
  218. bonuses.erase(itr);
  219. changed();
  220. return true;
  221. }
  222. void BonusList::resize(BonusList::TInternalContainer::size_type sz, const std::shared_ptr<Bonus> & c)
  223. {
  224. bonuses.resize(sz, c);
  225. changed();
  226. }
  227. void BonusList::reserve(TInternalContainer::size_type sz)
  228. {
  229. bonuses.reserve(sz);
  230. }
  231. void BonusList::insert(BonusList::TInternalContainer::iterator position, BonusList::TInternalContainer::size_type n, const std::shared_ptr<Bonus> & x)
  232. {
  233. bonuses.insert(position, n, x);
  234. changed();
  235. }
  236. DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const BonusList &bonusList)
  237. {
  238. for (ui32 i = 0; i < bonusList.size(); i++)
  239. {
  240. const auto & b = bonusList[i];
  241. out << "Bonus " << i << "\n" << *b << std::endl;
  242. }
  243. return out;
  244. }
  245. VCMI_LIB_NAMESPACE_END