BonusList.cpp 6.9 KB

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