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