mirror of
https://github.com/vcmi/vcmi.git
synced 2025-06-06 23:26:26 +02:00
avoid one loop in BonusList::totalValue()
This commit is contained in:
parent
4108b5d71f
commit
a6c7fda649
@ -104,8 +104,8 @@ int BonusList::totalValue() const
|
|||||||
};
|
};
|
||||||
|
|
||||||
BonusCollection accumulated;
|
BonusCollection accumulated;
|
||||||
bool hasIndepMax = false;
|
int indexMaxCount = 0;
|
||||||
bool hasIndepMin = false;
|
int indexMinCount = 0;
|
||||||
|
|
||||||
std::array<int, vstd::to_underlying(BonusSource::NUM_BONUS_SOURCE)> percentToSource = {};
|
std::array<int, vstd::to_underlying(BonusSource::NUM_BONUS_SOURCE)> percentToSource = {};
|
||||||
|
|
||||||
@ -141,12 +141,12 @@ int BonusList::totalValue() const
|
|||||||
case BonusValueType::ADDITIVE_VALUE:
|
case BonusValueType::ADDITIVE_VALUE:
|
||||||
accumulated.additive += valModified;
|
accumulated.additive += valModified;
|
||||||
break;
|
break;
|
||||||
case BonusValueType::INDEPENDENT_MAX:
|
case BonusValueType::INDEPENDENT_MAX: // actual meaning: at least this value
|
||||||
hasIndepMax = true;
|
indexMaxCount++;
|
||||||
vstd::amax(accumulated.indepMax, valModified);
|
vstd::amax(accumulated.indepMax, valModified);
|
||||||
break;
|
break;
|
||||||
case BonusValueType::INDEPENDENT_MIN:
|
case BonusValueType::INDEPENDENT_MIN: // actual meaning: at most this value
|
||||||
hasIndepMin = true;
|
indexMinCount++;
|
||||||
vstd::amin(accumulated.indepMin, valModified);
|
vstd::amin(accumulated.indepMin, valModified);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -156,21 +156,18 @@ int BonusList::totalValue() const
|
|||||||
accumulated.base += accumulated.additive;
|
accumulated.base += accumulated.additive;
|
||||||
auto valFirst = applyPercentage(accumulated.base ,accumulated.percentToAll);
|
auto valFirst = applyPercentage(accumulated.base ,accumulated.percentToAll);
|
||||||
|
|
||||||
if(hasIndepMin && hasIndepMax && accumulated.indepMin < accumulated.indepMax)
|
if(indexMinCount && indexMaxCount && accumulated.indepMin < accumulated.indepMax)
|
||||||
accumulated.indepMax = accumulated.indepMin;
|
accumulated.indepMax = accumulated.indepMin;
|
||||||
|
|
||||||
const int notIndepBonuses = static_cast<int>(std::count_if(bonuses.cbegin(), bonuses.cend(), [](const std::shared_ptr<Bonus>& b)
|
const int notIndepBonuses = bonuses.size() - indexMaxCount - indexMinCount;
|
||||||
{
|
|
||||||
return b->valType != BonusValueType::INDEPENDENT_MAX && b->valType != BonusValueType::INDEPENDENT_MIN;
|
|
||||||
}));
|
|
||||||
|
|
||||||
if(notIndepBonuses)
|
if(notIndepBonuses)
|
||||||
return std::clamp(valFirst, accumulated.indepMax, accumulated.indepMin);
|
return std::clamp(valFirst, accumulated.indepMax, accumulated.indepMin);
|
||||||
|
|
||||||
if (hasIndepMin)
|
if (indexMinCount)
|
||||||
return accumulated.indepMin;
|
return accumulated.indepMin;
|
||||||
|
|
||||||
if (hasIndepMax)
|
if (indexMaxCount)
|
||||||
return accumulated.indepMax;
|
return accumulated.indepMax;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user