2023-04-30 18:13:55 +02:00
|
|
|
/*
|
|
|
|
* IBonusBearer.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"
|
|
|
|
|
2024-01-01 00:44:09 +02:00
|
|
|
#include "IBonusBearer.h"
|
2023-04-30 19:34:25 +02:00
|
|
|
#include "BonusList.h"
|
2023-04-30 18:13:55 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
int IBonusBearer::valOfBonuses(const CSelector &selector, const std::string &cachingStr) const
|
|
|
|
{
|
2024-08-25 12:44:11 +02:00
|
|
|
TConstBonusListPtr hlp = getAllBonuses(selector, nullptr, cachingStr);
|
2023-04-30 18:13:55 +02:00
|
|
|
return hlp->totalValue();
|
|
|
|
}
|
2023-05-05 19:39:57 +02:00
|
|
|
|
2023-04-30 18:13:55 +02:00
|
|
|
bool IBonusBearer::hasBonus(const CSelector &selector, const std::string &cachingStr) const
|
|
|
|
{
|
|
|
|
//TODO: We don't need to count all bonuses and could break on first matching
|
2023-05-05 19:39:57 +02:00
|
|
|
return !getBonuses(selector, cachingStr)->empty();
|
2023-04-30 18:13:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IBonusBearer::hasBonus(const CSelector &selector, const CSelector &limit, const std::string &cachingStr) const
|
|
|
|
{
|
2023-05-05 19:39:57 +02:00
|
|
|
return !getBonuses(selector, limit, cachingStr)->empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
TConstBonusListPtr IBonusBearer::getBonuses(const CSelector &selector, const std::string &cachingStr) const
|
|
|
|
{
|
2024-08-25 12:44:11 +02:00
|
|
|
return getAllBonuses(selector, nullptr, cachingStr);
|
2023-05-05 19:39:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TConstBonusListPtr IBonusBearer::getBonuses(const CSelector &selector, const CSelector &limit, const std::string &cachingStr) const
|
|
|
|
{
|
2024-08-25 12:44:11 +02:00
|
|
|
return getAllBonuses(selector, limit, cachingStr);
|
2023-05-05 19:39:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int IBonusBearer::valOfBonuses(BonusType type) const
|
|
|
|
{
|
|
|
|
//This part is performance-critical
|
|
|
|
std::string cachingStr = "type_" + std::to_string(static_cast<int>(type));
|
|
|
|
|
|
|
|
CSelector s = Selector::type()(type);
|
|
|
|
|
|
|
|
return valOfBonuses(s, cachingStr);
|
2023-04-30 18:13:55 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 19:39:57 +02:00
|
|
|
bool IBonusBearer::hasBonusOfType(BonusType type) const
|
2023-04-30 18:13:55 +02:00
|
|
|
{
|
2023-05-05 19:39:57 +02:00
|
|
|
//This part is performance-critical
|
|
|
|
std::string cachingStr = "type_" + std::to_string(static_cast<int>(type));
|
2023-04-30 18:13:55 +02:00
|
|
|
|
|
|
|
CSelector s = Selector::type()(type);
|
|
|
|
|
|
|
|
return hasBonus(s, cachingStr);
|
|
|
|
}
|
|
|
|
|
2023-10-21 13:50:42 +02:00
|
|
|
int IBonusBearer::valOfBonuses(BonusType type, BonusSubtypeID subtype) const
|
2023-04-30 18:13:55 +02:00
|
|
|
{
|
2023-05-05 19:39:57 +02:00
|
|
|
//This part is performance-critical
|
2023-10-05 15:13:52 +02:00
|
|
|
std::string cachingStr = "type_" + std::to_string(static_cast<int>(type)) + "_" + subtype.toString();
|
2023-05-05 19:39:57 +02:00
|
|
|
|
|
|
|
CSelector s = Selector::typeSubtype(type, subtype);
|
|
|
|
|
|
|
|
return valOfBonuses(s, cachingStr);
|
2023-04-30 18:13:55 +02:00
|
|
|
}
|
|
|
|
|
2023-10-21 13:50:42 +02:00
|
|
|
bool IBonusBearer::hasBonusOfType(BonusType type, BonusSubtypeID subtype) const
|
2023-04-30 18:13:55 +02:00
|
|
|
{
|
2023-05-05 19:39:57 +02:00
|
|
|
//This part is performance-critical
|
2023-10-05 15:13:52 +02:00
|
|
|
std::string cachingStr = "type_" + std::to_string(static_cast<int>(type)) + "_" + subtype.toString();
|
2023-05-05 19:39:57 +02:00
|
|
|
|
|
|
|
CSelector s = Selector::typeSubtype(type, subtype);
|
|
|
|
|
|
|
|
return hasBonus(s, cachingStr);
|
2023-04-30 18:13:55 +02:00
|
|
|
}
|
|
|
|
|
2023-10-21 13:50:42 +02:00
|
|
|
bool IBonusBearer::hasBonusFrom(BonusSource source, BonusSourceID sourceID) const
|
2023-04-30 18:13:55 +02:00
|
|
|
{
|
2024-08-12 15:29:43 +02:00
|
|
|
return hasBonus(Selector::source(source,sourceID));
|
2023-04-30 18:13:55 +02:00
|
|
|
}
|
2024-08-12 15:29:43 +02:00
|
|
|
|
2023-04-30 18:13:55 +02:00
|
|
|
std::shared_ptr<const Bonus> IBonusBearer::getBonus(const CSelector &selector) const
|
|
|
|
{
|
|
|
|
auto bonuses = getAllBonuses(selector, Selector::all);
|
|
|
|
return bonuses->getFirst(Selector::all);
|
|
|
|
}
|
|
|
|
|
2023-10-05 15:13:52 +02:00
|
|
|
VCMI_LIB_NAMESPACE_END
|