2012-12-19 17:54:10 +03:00
|
|
|
/*
|
|
|
|
* HeroBonus.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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
#include "StdInc.h"
|
2010-02-10 04:56:00 +02:00
|
|
|
#include "HeroBonus.h"
|
2011-12-14 00:23:17 +03:00
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
#include "VCMI_Lib.h"
|
2015-02-02 10:25:26 +02:00
|
|
|
#include "spells/CSpellHandler.h"
|
2010-12-20 23:22:53 +02:00
|
|
|
#include "CCreatureHandler.h"
|
2010-07-12 13:20:25 +03:00
|
|
|
#include "CCreatureSet.h"
|
2010-12-20 23:22:53 +02:00
|
|
|
#include "CHeroHandler.h"
|
2018-04-17 14:59:30 +02:00
|
|
|
#include "CTownHandler.h"
|
2011-01-15 04:17:56 +02:00
|
|
|
#include "CGeneralTextHandler.h"
|
2017-08-30 12:35:23 +02:00
|
|
|
#include "CSkillHandler.h"
|
2017-03-17 17:48:44 +02:00
|
|
|
#include "CStack.h"
|
2011-02-22 11:47:25 +02:00
|
|
|
#include "CArtHandler.h"
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
#include "CModHandler.h"
|
2023-01-09 01:17:37 +02:00
|
|
|
#include "TerrainHandler.h"
|
2017-09-12 08:15:13 +02:00
|
|
|
#include "StringConstants.h"
|
2018-04-17 14:59:30 +02:00
|
|
|
#include "battle/BattleInfo.h"
|
2010-04-03 06:33:46 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
#define FOREACH_PARENT(pname) TNodes lparents; getParents(lparents); for(CBonusSystemNode *pname : lparents)
|
|
|
|
#define FOREACH_RED_CHILD(pname) TNodes lchildren; getRedChildren(lchildren); for(CBonusSystemNode *pname : lchildren)
|
2010-02-10 04:56:00 +02:00
|
|
|
|
2014-10-02 18:43:46 +03:00
|
|
|
#define BONUS_NAME(x) { #x, Bonus::x },
|
2023-02-19 13:40:28 +02:00
|
|
|
const std::map<std::string, Bonus::BonusType> bonusNameMap = {
|
|
|
|
BONUS_LIST
|
|
|
|
};
|
2010-07-12 13:20:25 +03:00
|
|
|
#undef BONUS_NAME
|
|
|
|
|
2014-10-02 18:43:46 +03:00
|
|
|
#define BONUS_VALUE(x) { #x, Bonus::x },
|
|
|
|
const std::map<std::string, Bonus::ValueType> bonusValueMap = { BONUS_VALUE_LIST };
|
2012-08-23 21:46:43 +03:00
|
|
|
#undef BONUS_VALUE
|
|
|
|
|
2014-10-02 18:43:46 +03:00
|
|
|
#define BONUS_SOURCE(x) { #x, Bonus::x },
|
|
|
|
const std::map<std::string, Bonus::BonusSource> bonusSourceMap = { BONUS_SOURCE_LIST };
|
2012-08-23 21:46:43 +03:00
|
|
|
#undef BONUS_SOURCE
|
|
|
|
|
2014-10-02 18:43:46 +03:00
|
|
|
#define BONUS_ITEM(x) { #x, Bonus::x },
|
2012-08-23 21:46:43 +03:00
|
|
|
|
2016-08-18 14:03:20 +02:00
|
|
|
const std::map<std::string, ui16> bonusDurationMap =
|
2014-10-02 18:43:46 +03:00
|
|
|
{
|
2012-08-23 21:46:43 +03:00
|
|
|
BONUS_ITEM(PERMANENT)
|
|
|
|
BONUS_ITEM(ONE_BATTLE)
|
|
|
|
BONUS_ITEM(ONE_DAY)
|
|
|
|
BONUS_ITEM(ONE_WEEK)
|
|
|
|
BONUS_ITEM(N_TURNS)
|
|
|
|
BONUS_ITEM(N_DAYS)
|
2016-09-30 00:57:33 +02:00
|
|
|
BONUS_ITEM(UNTIL_BEING_ATTACKED)
|
2012-08-23 21:46:43 +03:00
|
|
|
BONUS_ITEM(UNTIL_ATTACK)
|
|
|
|
BONUS_ITEM(STACK_GETS_TURN)
|
2014-10-02 18:43:46 +03:00
|
|
|
BONUS_ITEM(COMMANDER_KILLED)
|
2016-09-30 00:57:33 +02:00
|
|
|
{ "UNITL_BEING_ATTACKED", Bonus::UNTIL_BEING_ATTACKED }//typo, but used in some mods
|
2014-10-02 18:43:46 +03:00
|
|
|
};
|
2012-08-23 21:46:43 +03:00
|
|
|
|
2016-08-18 14:03:20 +02:00
|
|
|
const std::map<std::string, Bonus::LimitEffect> bonusLimitEffect =
|
2014-10-02 18:43:46 +03:00
|
|
|
{
|
2012-08-23 21:46:43 +03:00
|
|
|
BONUS_ITEM(NO_LIMIT)
|
|
|
|
BONUS_ITEM(ONLY_DISTANCE_FIGHT)
|
2014-05-21 12:02:20 +03:00
|
|
|
BONUS_ITEM(ONLY_MELEE_FIGHT)
|
2014-10-02 18:43:46 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const std::map<std::string, TLimiterPtr> bonusLimiterMap =
|
|
|
|
{
|
2015-12-29 04:43:33 +02:00
|
|
|
{"SHOOTER_ONLY", std::make_shared<HasAnotherBonusLimiter>(Bonus::SHOOTER)},
|
|
|
|
{"DRAGON_NATURE", std::make_shared<HasAnotherBonusLimiter>(Bonus::DRAGON_NATURE)},
|
2018-04-17 14:59:30 +02:00
|
|
|
{"IS_UNDEAD", std::make_shared<HasAnotherBonusLimiter>(Bonus::UNDEAD)},
|
2021-01-14 00:02:13 +02:00
|
|
|
{"CREATURE_NATIVE_TERRAIN", std::make_shared<CreatureTerrainLimiter>()},
|
2023-04-09 03:36:16 +02:00
|
|
|
{"CREATURE_FACTION", std::make_shared<AllOfLimiter>(std::initializer_list<TLimiterPtr>{std::make_shared<CreatureLevelLimiter>(), std::make_shared<FactionLimiter>()})},
|
|
|
|
{"SAME_FACTION", std::make_shared<FactionLimiter>()},
|
2023-04-06 19:06:41 +02:00
|
|
|
{"CREATURES_ONLY", std::make_shared<CreatureLevelLimiter>()},
|
2023-03-28 01:14:18 +02:00
|
|
|
{"OPPOSITE_SIDE", std::make_shared<OppositeSideLimiter>()},
|
2014-10-02 18:43:46 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const std::map<std::string, TPropagatorPtr> bonusPropagatorMap =
|
|
|
|
{
|
2015-12-29 04:43:33 +02:00
|
|
|
{"BATTLE_WIDE", std::make_shared<CPropagatorNodeType>(CBonusSystemNode::BATTLE)},
|
|
|
|
{"VISITED_TOWN_AND_VISITOR", std::make_shared<CPropagatorNodeType>(CBonusSystemNode::TOWN_AND_VISITOR)},
|
|
|
|
{"PLAYER_PROPAGATOR", std::make_shared<CPropagatorNodeType>(CBonusSystemNode::PLAYER)},
|
|
|
|
{"HERO", std::make_shared<CPropagatorNodeType>(CBonusSystemNode::HERO)},
|
|
|
|
{"TEAM_PROPAGATOR", std::make_shared<CPropagatorNodeType>(CBonusSystemNode::TEAM)}, //untested
|
2023-04-06 19:03:17 +02:00
|
|
|
{"GLOBAL_EFFECT", std::make_shared<CPropagatorNodeType>(CBonusSystemNode::GLOBAL_EFFECTS)}
|
2014-10-02 18:43:46 +03:00
|
|
|
}; //untested
|
2012-09-27 19:48:46 +03:00
|
|
|
|
2017-09-19 00:08:28 +02:00
|
|
|
const std::map<std::string, TUpdaterPtr> bonusUpdaterMap =
|
|
|
|
{
|
2017-09-19 01:43:13 +02:00
|
|
|
{"TIMES_HERO_LEVEL", std::make_shared<TimesHeroLevelUpdater>()},
|
2023-02-18 20:01:32 +02:00
|
|
|
{"TIMES_STACK_LEVEL", std::make_shared<TimesStackLevelUpdater>()},
|
2023-03-09 12:05:47 +02:00
|
|
|
{"ARMY_MOVEMENT", std::make_shared<ArmyMovementUpdater>()},
|
|
|
|
{"BONUS_OWNER_UPDATER", std::make_shared<OwnerUpdater>()}
|
2017-09-19 00:08:28 +02:00
|
|
|
};
|
|
|
|
|
2023-03-05 02:16:05 +02:00
|
|
|
const std::set<std::string> deprecatedBonusSet = {
|
|
|
|
"SECONDARY_SKILL_PREMY",
|
|
|
|
"SECONDARY_SKILL_VAL2",
|
|
|
|
"MAXED_SPELL",
|
|
|
|
"LAND_MOVEMENT",
|
|
|
|
"SEA_MOVEMENT",
|
|
|
|
"SIGHT_RADIOUS",
|
|
|
|
"NO_TYPE",
|
|
|
|
"SPECIAL_SECONDARY_SKILL",
|
|
|
|
"FULL_HP_REGENERATION",
|
|
|
|
"KING1",
|
|
|
|
"KING2",
|
|
|
|
"KING3",
|
2023-03-09 10:59:38 +02:00
|
|
|
"BLOCK_MORALE",
|
2023-03-09 11:08:32 +02:00
|
|
|
"BLOCK_LUCK",
|
2023-03-09 11:25:03 +02:00
|
|
|
"SELF_MORALE",
|
2023-03-09 11:31:04 +02:00
|
|
|
"SELF_LUCK"
|
2023-03-05 02:16:05 +02:00
|
|
|
};
|
|
|
|
|
2017-07-04 13:24:46 +02:00
|
|
|
///CBonusProxy
|
2023-03-13 23:26:44 +02:00
|
|
|
CBonusProxy::CBonusProxy(const IBonusBearer * Target, CSelector Selector):
|
|
|
|
bonusListCachedLast(0),
|
2017-07-20 06:08:49 +02:00
|
|
|
target(Target),
|
2023-03-13 23:26:44 +02:00
|
|
|
selector(std::move(Selector)),
|
|
|
|
currentBonusListIndex(0)
|
2017-07-04 13:24:46 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
CBonusProxy::CBonusProxy(const CBonusProxy & other):
|
|
|
|
bonusListCachedLast(other.bonusListCachedLast),
|
2017-07-20 06:08:49 +02:00
|
|
|
target(other.target),
|
|
|
|
selector(other.selector),
|
2023-03-13 23:26:44 +02:00
|
|
|
currentBonusListIndex(other.currentBonusListIndex)
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
2022-04-04 09:16:32 +02:00
|
|
|
bonusList[currentBonusListIndex] = other.bonusList[currentBonusListIndex];
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
CBonusProxy::CBonusProxy(CBonusProxy && other) noexcept:
|
|
|
|
bonusListCachedLast(0),
|
2017-07-20 06:08:49 +02:00
|
|
|
target(other.target),
|
2023-03-13 23:26:44 +02:00
|
|
|
currentBonusListIndex(0)
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
2020-11-11 21:43:40 +02:00
|
|
|
std::swap(bonusListCachedLast, other.bonusListCachedLast);
|
2017-07-20 06:08:49 +02:00
|
|
|
std::swap(selector, other.selector);
|
2020-11-11 21:43:40 +02:00
|
|
|
std::swap(bonusList, other.bonusList);
|
2022-04-04 09:16:32 +02:00
|
|
|
std::swap(currentBonusListIndex, other.currentBonusListIndex);
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CBonusProxy & CBonusProxy::operator=(const CBonusProxy & other)
|
|
|
|
{
|
2022-04-04 09:16:32 +02:00
|
|
|
boost::lock_guard<boost::mutex> lock(swapGuard);
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
selector = other.selector;
|
2022-04-04 09:16:32 +02:00
|
|
|
swapBonusList(other.bonusList[other.currentBonusListIndex]);
|
|
|
|
bonusListCachedLast = other.bonusListCachedLast;
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
CBonusProxy & CBonusProxy::operator=(CBonusProxy && other) noexcept
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
2020-11-11 21:43:40 +02:00
|
|
|
std::swap(bonusListCachedLast, other.bonusListCachedLast);
|
2017-07-20 06:08:49 +02:00
|
|
|
std::swap(selector, other.selector);
|
2020-11-11 21:43:40 +02:00
|
|
|
std::swap(bonusList, other.bonusList);
|
2022-04-04 09:16:32 +02:00
|
|
|
std::swap(currentBonusListIndex, other.currentBonusListIndex);
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-04-04 09:16:32 +02:00
|
|
|
void CBonusProxy::swapBonusList(TConstBonusListPtr other) const
|
|
|
|
{
|
|
|
|
// The idea here is to avoid changing active bonusList while it can be read by a different thread.
|
|
|
|
// Because such use of shared ptr is not thread safe
|
|
|
|
// So to avoid this we change the second offline instance and swap active index
|
|
|
|
auto newCurrent = 1 - currentBonusListIndex;
|
2023-03-13 23:26:44 +02:00
|
|
|
bonusList[newCurrent] = std::move(other);
|
2022-04-04 09:16:32 +02:00
|
|
|
currentBonusListIndex = newCurrent;
|
|
|
|
}
|
|
|
|
|
2020-11-11 21:43:40 +02:00
|
|
|
TConstBonusListPtr CBonusProxy::getBonusList() const
|
2017-07-04 13:24:46 +02:00
|
|
|
{
|
2022-04-04 09:16:32 +02:00
|
|
|
auto needUpdateBonusList = [&]() -> bool
|
2017-07-04 13:24:46 +02:00
|
|
|
{
|
2022-04-04 09:16:32 +02:00
|
|
|
return target->getTreeVersion() != bonusListCachedLast || !bonusList[currentBonusListIndex];
|
|
|
|
};
|
|
|
|
|
|
|
|
// avoid locking if everything is up-to-date
|
|
|
|
if(needUpdateBonusList())
|
|
|
|
{
|
|
|
|
boost::lock_guard<boost::mutex>lock(swapGuard);
|
|
|
|
|
|
|
|
if(needUpdateBonusList())
|
|
|
|
{
|
|
|
|
//TODO: support limiters
|
|
|
|
swapBonusList(target->getAllBonuses(selector, Selector::all));
|
|
|
|
bonusListCachedLast = target->getTreeVersion();
|
|
|
|
}
|
2017-07-04 13:24:46 +02:00
|
|
|
}
|
2022-04-04 09:16:32 +02:00
|
|
|
|
|
|
|
return bonusList[currentBonusListIndex];
|
2017-07-04 13:24:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const BonusList * CBonusProxy::operator->() const
|
|
|
|
{
|
2020-11-11 21:43:40 +02:00
|
|
|
return getBonusList().get();
|
2017-07-04 13:24:46 +02:00
|
|
|
}
|
2012-09-27 19:48:46 +03:00
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
CTotalsProxy::CTotalsProxy(const IBonusBearer * Target, CSelector Selector, int InitialValue):
|
|
|
|
CBonusProxy(Target, std::move(Selector)),
|
2020-06-28 15:19:27 +02:00
|
|
|
initialValue(InitialValue),
|
|
|
|
meleeCachedLast(0),
|
|
|
|
meleeValue(0),
|
|
|
|
rangedCachedLast(0),
|
2023-03-13 23:26:44 +02:00
|
|
|
rangedValue(0)
|
2020-06-28 15:19:27 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CTotalsProxy::CTotalsProxy(const CTotalsProxy & other)
|
2020-11-11 21:43:40 +02:00
|
|
|
: CBonusProxy(other),
|
2020-06-28 15:19:27 +02:00
|
|
|
initialValue(other.initialValue),
|
|
|
|
meleeCachedLast(other.meleeCachedLast),
|
|
|
|
meleeValue(other.meleeValue),
|
|
|
|
rangedCachedLast(other.rangedCachedLast),
|
|
|
|
rangedValue(other.rangedValue)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int CTotalsProxy::getValue() const
|
|
|
|
{
|
|
|
|
const auto treeVersion = target->getTreeVersion();
|
|
|
|
|
2020-11-11 21:43:40 +02:00
|
|
|
if(treeVersion != valueCachedLast)
|
2020-06-28 15:19:27 +02:00
|
|
|
{
|
2020-11-11 21:43:40 +02:00
|
|
|
auto bonuses = getBonusList();
|
2020-06-28 15:19:27 +02:00
|
|
|
|
|
|
|
value = initialValue + bonuses->totalValue();
|
2020-11-11 21:43:40 +02:00
|
|
|
valueCachedLast = treeVersion;
|
2020-06-28 15:19:27 +02:00
|
|
|
}
|
2020-11-11 21:43:40 +02:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CTotalsProxy::getValueAndList(TConstBonusListPtr & outBonusList) const
|
|
|
|
{
|
|
|
|
const auto treeVersion = target->getTreeVersion();
|
|
|
|
outBonusList = getBonusList();
|
2020-06-28 15:19:27 +02:00
|
|
|
|
2020-11-11 21:43:40 +02:00
|
|
|
if(treeVersion != valueCachedLast)
|
|
|
|
{
|
|
|
|
value = initialValue + outBonusList->totalValue();
|
|
|
|
valueCachedLast = treeVersion;
|
|
|
|
}
|
2020-06-28 15:19:27 +02:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CTotalsProxy::getMeleeValue() const
|
|
|
|
{
|
2020-11-11 21:43:40 +02:00
|
|
|
static const auto limit = Selector::effectRange()(Bonus::NO_LIMIT).Or(Selector::effectRange()(Bonus::ONLY_MELEE_FIGHT));
|
2020-06-28 15:19:27 +02:00
|
|
|
|
|
|
|
const auto treeVersion = target->getTreeVersion();
|
|
|
|
|
|
|
|
if(treeVersion != meleeCachedLast)
|
|
|
|
{
|
|
|
|
auto bonuses = target->getBonuses(selector, limit);
|
|
|
|
meleeValue = initialValue + bonuses->totalValue();
|
|
|
|
meleeCachedLast = treeVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
return meleeValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CTotalsProxy::getRangedValue() const
|
|
|
|
{
|
2020-11-11 21:43:40 +02:00
|
|
|
static const auto limit = Selector::effectRange()(Bonus::NO_LIMIT).Or(Selector::effectRange()(Bonus::ONLY_DISTANCE_FIGHT));
|
2020-06-28 15:19:27 +02:00
|
|
|
|
|
|
|
const auto treeVersion = target->getTreeVersion();
|
|
|
|
|
|
|
|
if(treeVersion != rangedCachedLast)
|
|
|
|
{
|
|
|
|
auto bonuses = target->getBonuses(selector, limit);
|
|
|
|
rangedValue = initialValue + bonuses->totalValue();
|
|
|
|
rangedCachedLast = treeVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rangedValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
///CCheckProxy
|
2023-03-13 23:26:44 +02:00
|
|
|
CCheckProxy::CCheckProxy(const IBonusBearer * Target, CSelector Selector):
|
|
|
|
target(Target),
|
|
|
|
selector(std::move(Selector)),
|
2020-06-28 15:19:27 +02:00
|
|
|
cachedLast(0),
|
|
|
|
hasBonus(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
//This constructor should be placed here to avoid side effects
|
|
|
|
CCheckProxy::CCheckProxy(const CCheckProxy & other) = default;
|
2020-06-28 15:19:27 +02:00
|
|
|
|
|
|
|
bool CCheckProxy::getHasBonus() const
|
|
|
|
{
|
|
|
|
const auto treeVersion = target->getTreeVersion();
|
|
|
|
|
|
|
|
if(treeVersion != cachedLast)
|
|
|
|
{
|
|
|
|
hasBonus = target->hasBonus(selector);
|
|
|
|
cachedLast = treeVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hasBonus;
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
//This constructor should be placed here to avoid side effects
|
|
|
|
CAddInfo::CAddInfo() = default;
|
2018-03-12 07:20:18 +02:00
|
|
|
|
|
|
|
CAddInfo::CAddInfo(si32 value)
|
|
|
|
{
|
|
|
|
if(value != CAddInfo::NONE)
|
|
|
|
push_back(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CAddInfo::operator==(si32 value) const
|
|
|
|
{
|
|
|
|
switch(size())
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return value == CAddInfo::NONE;
|
|
|
|
case 1:
|
|
|
|
return operator[](0) == value;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CAddInfo::operator!=(si32 value) const
|
|
|
|
{
|
|
|
|
return !operator==(value);
|
|
|
|
}
|
|
|
|
|
2018-03-17 00:03:02 +02:00
|
|
|
si32 & CAddInfo::operator[](size_type pos)
|
|
|
|
{
|
2018-03-20 01:58:02 +02:00
|
|
|
if(pos >= size())
|
2018-03-17 00:03:02 +02:00
|
|
|
resize(pos + 1, CAddInfo::NONE);
|
|
|
|
return vector::operator[](pos);
|
|
|
|
}
|
|
|
|
|
2018-03-12 07:20:18 +02:00
|
|
|
si32 CAddInfo::operator[](size_type pos) const
|
|
|
|
{
|
|
|
|
return pos < size() ? vector::operator[](pos) : CAddInfo::NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CAddInfo::toString() const
|
|
|
|
{
|
|
|
|
return toJsonNode().toJson(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode CAddInfo::toJsonNode() const
|
|
|
|
{
|
|
|
|
if(size() < 2)
|
|
|
|
{
|
|
|
|
return JsonUtils::intNode(operator[](0));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
JsonNode node(JsonNode::JsonType::DATA_VECTOR);
|
|
|
|
for(si32 value : *this)
|
|
|
|
node.Vector().push_back(JsonUtils::intNode(value));
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-29 13:17:09 +02:00
|
|
|
std::atomic<int64_t> CBonusSystemNode::treeChanged(1);
|
|
|
|
constexpr bool CBonusSystemNode::cachingEnabled = true;
|
2011-06-25 16:53:15 +03:00
|
|
|
|
2017-07-15 13:08:20 +02:00
|
|
|
BonusList::BonusList(bool BelongsToTree) : belongsToTree(BelongsToTree)
|
2011-07-13 21:39:02 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
BonusList::BonusList(const BonusList & bonusList): belongsToTree(false)
|
2011-07-13 21:39:02 +03:00
|
|
|
{
|
|
|
|
bonuses.resize(bonusList.size());
|
|
|
|
std::copy(bonusList.begin(), bonusList.end(), bonuses.begin());
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
BonusList::BonusList(BonusList && other) noexcept: belongsToTree(false)
|
2016-08-15 08:28:00 +02:00
|
|
|
{
|
|
|
|
std::swap(belongsToTree, other.belongsToTree);
|
|
|
|
std::swap(bonuses, other.bonuses);
|
|
|
|
}
|
|
|
|
|
2011-07-13 21:39:02 +03:00
|
|
|
BonusList& BonusList::operator=(const BonusList &bonusList)
|
|
|
|
{
|
|
|
|
bonuses.resize(bonusList.size());
|
|
|
|
std::copy(bonusList.begin(), bonusList.end(), bonuses.begin());
|
|
|
|
belongsToTree = false;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
void BonusList::changed() const
|
2016-07-29 12:10:32 +02:00
|
|
|
{
|
|
|
|
if(belongsToTree)
|
|
|
|
CBonusSystemNode::treeHasChanged();
|
|
|
|
}
|
|
|
|
|
2018-03-27 09:54:58 +02:00
|
|
|
void BonusList::stackBonuses()
|
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
boost::sort(bonuses, [](const std::shared_ptr<Bonus> & b1, const std::shared_ptr<Bonus> & b2) -> bool
|
2018-03-27 09:54:58 +02:00
|
|
|
{
|
|
|
|
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())
|
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
bool remove = false;
|
2020-10-01 07:55:41 +02:00
|
|
|
std::shared_ptr<Bonus> last = bonuses[next-1];
|
|
|
|
std::shared_ptr<Bonus> current = bonuses[next];
|
2018-03-27 09:54:58 +02:00
|
|
|
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-13 21:39:02 +03:00
|
|
|
int BonusList::totalValue() const
|
2010-02-10 04:56:00 +02:00
|
|
|
{
|
2023-02-15 21:19:35 +02:00
|
|
|
struct BonusCollection
|
|
|
|
{
|
|
|
|
int base = 0;
|
|
|
|
int percentToBase = 0;
|
|
|
|
int percentToAll = 0;
|
|
|
|
int additive = 0;
|
2023-03-18 14:32:31 +02:00
|
|
|
int percentToSource = 0;
|
2023-02-15 21:19:35 +02:00
|
|
|
int indepMin = std::numeric_limits<int>::max();
|
|
|
|
int indepMax = std::numeric_limits<int>::min();
|
|
|
|
};
|
|
|
|
|
2023-03-18 16:04:52 +02:00
|
|
|
auto percent = [](int64_t base, int64_t percent) -> int {
|
2023-04-11 00:00:46 +02:00
|
|
|
return static_cast<int>(std::clamp<int64_t>((base * (100 + percent)) / 100, std::numeric_limits<int>::min(), std::numeric_limits<int>::max()));
|
2023-03-18 16:04:52 +02:00
|
|
|
};
|
2023-02-15 21:19:35 +02:00
|
|
|
std::array <BonusCollection, Bonus::BonusSource::NUM_BONUS_SOURCE> sources = {};
|
|
|
|
BonusCollection any;
|
2010-08-05 12:11:38 +03:00
|
|
|
bool hasIndepMax = false;
|
2011-02-12 18:12:48 +02:00
|
|
|
bool hasIndepMin = false;
|
2010-02-10 04:56:00 +02:00
|
|
|
|
2023-03-18 14:32:31 +02:00
|
|
|
for(const auto & b : bonuses)
|
2010-02-10 04:56:00 +02:00
|
|
|
{
|
2011-07-16 16:57:25 +03:00
|
|
|
switch(b->valType)
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
|
|
|
case Bonus::BASE_NUMBER:
|
2023-02-15 21:19:35 +02:00
|
|
|
sources[b->source].base += b->val;
|
2010-05-02 21:20:26 +03:00
|
|
|
break;
|
|
|
|
case Bonus::PERCENT_TO_ALL:
|
2023-02-15 21:19:35 +02:00
|
|
|
sources[b->source].percentToAll += b->val;
|
2010-05-02 21:20:26 +03:00
|
|
|
break;
|
|
|
|
case Bonus::PERCENT_TO_BASE:
|
2023-02-15 21:19:35 +02:00
|
|
|
sources[b->source].percentToBase += b->val;
|
2010-05-02 21:20:26 +03:00
|
|
|
break;
|
2023-02-13 10:57:11 +02:00
|
|
|
case Bonus::PERCENT_TO_SOURCE:
|
2023-02-15 21:19:35 +02:00
|
|
|
sources[b->source].percentToSource += b->val;
|
|
|
|
break;
|
|
|
|
case Bonus::PERCENT_TO_TARGET_TYPE:
|
|
|
|
sources[b->targetSourceType].percentToSource += b->val;
|
2023-02-13 10:57:11 +02:00
|
|
|
break;
|
2010-05-02 21:20:26 +03:00
|
|
|
case Bonus::ADDITIVE_VALUE:
|
2023-02-15 21:19:35 +02:00
|
|
|
sources[b->source].additive += b->val;
|
2010-08-05 12:11:38 +03:00
|
|
|
break;
|
|
|
|
case Bonus::INDEPENDENT_MAX:
|
2023-02-15 21:19:35 +02:00
|
|
|
hasIndepMax = true;
|
|
|
|
vstd::amax(sources[b->source].indepMax, b->val);
|
2011-02-12 18:12:48 +02:00
|
|
|
break;
|
|
|
|
case Bonus::INDEPENDENT_MIN:
|
2023-02-15 21:19:35 +02:00
|
|
|
hasIndepMin = true;
|
|
|
|
vstd::amin(sources[b->source].indepMin, b->val);
|
2010-05-02 21:20:26 +03:00
|
|
|
break;
|
|
|
|
}
|
2010-02-10 04:56:00 +02:00
|
|
|
}
|
2023-03-18 14:32:31 +02:00
|
|
|
for(const auto & src : sources)
|
2023-02-13 10:57:11 +02:00
|
|
|
{
|
2023-03-18 14:32:31 +02:00
|
|
|
any.base += percent(src.base, src.percentToSource);
|
2023-02-15 21:19:35 +02:00
|
|
|
any.percentToBase += percent(src.percentToBase, src.percentToSource);
|
|
|
|
any.percentToAll += percent(src.percentToAll, src.percentToSource);
|
|
|
|
any.additive += percent(src.additive, src.percentToSource);
|
|
|
|
if(hasIndepMin)
|
|
|
|
vstd::amin(any.indepMin, percent(src.indepMin, src.percentToSource));
|
|
|
|
if(hasIndepMax)
|
2023-03-18 14:32:31 +02:00
|
|
|
vstd::amax(any.indepMax, percent(src.indepMax, src.percentToSource));
|
2023-02-13 10:57:11 +02:00
|
|
|
}
|
2023-02-15 21:19:35 +02:00
|
|
|
any.base = percent(any.base, any.percentToBase);
|
|
|
|
any.base += any.additive;
|
|
|
|
auto valFirst = percent(any.base ,any.percentToAll);
|
2011-02-12 18:12:48 +02:00
|
|
|
|
2023-03-18 16:04:52 +02:00
|
|
|
if(hasIndepMin && hasIndepMax && any.indepMin < any.indepMax)
|
|
|
|
any.indepMax = any.indepMin;
|
2012-08-26 12:07:48 +03:00
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
const int notIndepBonuses = static_cast<int>(std::count_if(bonuses.cbegin(), bonuses.cend(), [](const std::shared_ptr<Bonus>& b)
|
2013-02-13 22:35:43 +03:00
|
|
|
{
|
|
|
|
return b->valType != Bonus::INDEPENDENT_MAX && b->valType != Bonus::INDEPENDENT_MIN;
|
2023-03-13 23:26:44 +02:00
|
|
|
}));
|
2012-08-26 12:07:48 +03:00
|
|
|
|
2023-03-18 16:04:52 +02:00
|
|
|
if(notIndepBonuses)
|
2023-04-11 00:00:46 +02:00
|
|
|
return std::clamp(valFirst, any.indepMax, any.indepMin);
|
2023-03-18 16:04:52 +02:00
|
|
|
|
|
|
|
return hasIndepMin ? any.indepMin : hasIndepMax ? any.indepMax : 0;
|
2010-02-10 04:56:00 +02:00
|
|
|
}
|
2016-09-19 23:36:35 +02:00
|
|
|
|
|
|
|
std::shared_ptr<Bonus> BonusList::getFirst(const CSelector &select)
|
2010-02-10 04:56:00 +02:00
|
|
|
{
|
2016-09-19 23:36:35 +02:00
|
|
|
for (auto & b : bonuses)
|
2011-06-25 16:53:15 +03:00
|
|
|
{
|
2016-09-19 23:36:35 +02:00
|
|
|
if(select(b.get()))
|
|
|
|
return b;
|
2011-06-25 16:53:15 +03:00
|
|
|
}
|
2013-06-26 14:18:27 +03:00
|
|
|
return nullptr;
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
2010-02-10 04:56:00 +02:00
|
|
|
|
2020-10-01 07:55:41 +02:00
|
|
|
std::shared_ptr<const Bonus> BonusList::getFirst(const CSelector &selector) const
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const auto & b : bonuses)
|
2011-06-25 16:53:15 +03:00
|
|
|
{
|
2016-09-19 23:36:35 +02:00
|
|
|
if(selector(b.get()))
|
|
|
|
return b;
|
2011-06-25 16:53:15 +03:00
|
|
|
}
|
2013-06-26 14:18:27 +03:00
|
|
|
return nullptr;
|
2010-02-10 04:56:00 +02:00
|
|
|
}
|
|
|
|
|
2012-03-06 19:59:55 +03:00
|
|
|
void BonusList::getBonuses(BonusList & out, const CSelector &selector, const CSelector &limit) const
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2022-09-14 11:00:40 +02:00
|
|
|
out.reserve(bonuses.size());
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const auto & b : bonuses)
|
2011-06-25 16:53:15 +03:00
|
|
|
{
|
|
|
|
//add matching bonuses that matches limit predicate or have NO_LIMIT if no given predicate
|
2023-03-09 12:05:47 +02:00
|
|
|
auto noFightLimit = b->effectRange == Bonus::NO_LIMIT;
|
2021-03-23 16:47:07 +02:00
|
|
|
if(selector(b.get()) && ((!limit && noFightLimit) || ((bool)limit && limit(b.get()))))
|
2012-03-06 19:59:55 +03:00
|
|
|
out.push_back(b);
|
2011-06-25 16:53:15 +03:00
|
|
|
}
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
2010-08-31 13:22:21 +03:00
|
|
|
|
2012-03-06 19:59:55 +03:00
|
|
|
void BonusList::getAllBonuses(BonusList &out) const
|
2011-07-13 21:39:02 +03:00
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const auto & b : bonuses)
|
2012-03-06 19:59:55 +03:00
|
|
|
out.push_back(b);
|
2011-07-13 21:39:02 +03:00
|
|
|
}
|
|
|
|
|
2012-03-06 19:59:55 +03:00
|
|
|
int BonusList::valOfBonuses(const CSelector &select) const
|
2010-07-12 13:20:25 +03:00
|
|
|
{
|
2012-03-06 19:59:55 +03:00
|
|
|
BonusList ret;
|
2013-06-29 16:05:48 +03:00
|
|
|
CSelector limit = nullptr;
|
2012-03-06 19:59:55 +03:00
|
|
|
getBonuses(ret, select, limit);
|
|
|
|
return ret.totalValue();
|
2010-07-12 13:20:25 +03:00
|
|
|
}
|
|
|
|
|
2018-03-27 09:54:58 +02:00
|
|
|
JsonNode BonusList::toJsonNode() const
|
2011-07-13 21:39:02 +03:00
|
|
|
{
|
2018-03-27 09:54:58 +02:00
|
|
|
JsonNode node(JsonNode::JsonType::DATA_VECTOR);
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const std::shared_ptr<Bonus> & b : bonuses)
|
2018-03-27 09:54:58 +02:00
|
|
|
node.Vector().push_back(b->toJsonNode());
|
|
|
|
return node;
|
2011-07-13 21:39:02 +03:00
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
void BonusList::push_back(const std::shared_ptr<Bonus> & x)
|
2011-02-27 13:26:18 +02:00
|
|
|
{
|
2011-07-13 21:39:02 +03:00
|
|
|
bonuses.push_back(x);
|
2016-07-29 12:10:32 +02:00
|
|
|
changed();
|
2011-07-13 21:39:02 +03:00
|
|
|
}
|
|
|
|
|
2016-09-19 23:36:35 +02:00
|
|
|
BonusList::TInternalContainer::iterator BonusList::erase(const int position)
|
2011-07-13 21:39:02 +03:00
|
|
|
{
|
2016-07-29 12:10:32 +02:00
|
|
|
changed();
|
2011-07-16 16:57:25 +03:00
|
|
|
return bonuses.erase(bonuses.begin() + position);
|
2011-07-13 21:39:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void BonusList::clear()
|
|
|
|
{
|
|
|
|
bonuses.clear();
|
2016-07-29 12:10:32 +02:00
|
|
|
changed();
|
2011-07-13 21:39:02 +03:00
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
std::vector<BonusList *>::size_type BonusList::operator-=(const std::shared_ptr<Bonus> & i)
|
2011-07-13 21:39:02 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
auto itr = std::find(bonuses.begin(), bonuses.end(), i);
|
2011-07-13 21:39:02 +03:00
|
|
|
if(itr == bonuses.end())
|
|
|
|
return false;
|
|
|
|
bonuses.erase(itr);
|
2016-07-29 12:10:32 +02:00
|
|
|
changed();
|
2011-07-13 21:39:02 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
void BonusList::resize(BonusList::TInternalContainer::size_type sz, const std::shared_ptr<Bonus> & c)
|
2011-07-13 21:39:02 +03:00
|
|
|
{
|
|
|
|
bonuses.resize(sz, c);
|
2016-07-29 12:10:32 +02:00
|
|
|
changed();
|
2011-07-13 21:39:02 +03:00
|
|
|
}
|
|
|
|
|
2022-09-14 11:00:40 +02:00
|
|
|
void BonusList::reserve(TInternalContainer::size_type sz)
|
|
|
|
{
|
|
|
|
bonuses.reserve(sz);
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
void BonusList::insert(BonusList::TInternalContainer::iterator position, BonusList::TInternalContainer::size_type n, const std::shared_ptr<Bonus> & x)
|
2011-07-13 21:39:02 +03:00
|
|
|
{
|
|
|
|
bonuses.insert(position, n, x);
|
2016-07-29 12:10:32 +02:00
|
|
|
changed();
|
2011-02-27 13:26:18 +02:00
|
|
|
}
|
|
|
|
|
2021-02-03 19:19:56 +02:00
|
|
|
CSelector IBonusBearer::anaffectedByMoraleSelector =
|
|
|
|
Selector::type()(Bonus::NON_LIVING)
|
|
|
|
.Or(Selector::type()(Bonus::UNDEAD))
|
|
|
|
.Or(Selector::type()(Bonus::SIEGE_WEAPON))
|
2023-03-09 10:59:38 +02:00
|
|
|
.Or(Selector::type()(Bonus::NO_MORALE));
|
2020-06-28 15:19:27 +02:00
|
|
|
|
2020-11-11 21:43:40 +02:00
|
|
|
CSelector IBonusBearer::moraleSelector = Selector::type()(Bonus::MORALE);
|
|
|
|
CSelector IBonusBearer::luckSelector = Selector::type()(Bonus::LUCK);
|
2020-06-28 15:19:27 +02:00
|
|
|
|
|
|
|
IBonusBearer::IBonusBearer()
|
|
|
|
:anaffectedByMorale(this, anaffectedByMoraleSelector),
|
|
|
|
moraleValue(this, moraleSelector, 0),
|
2023-03-09 11:31:04 +02:00
|
|
|
luckValue(this, luckSelector, 0)
|
2020-06-28 15:19:27 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-02-27 21:58:14 +02:00
|
|
|
int IBonusBearer::valOfBonuses(Bonus::BonusType type, const CSelector &selector) const
|
2010-08-25 11:40:50 +03:00
|
|
|
{
|
2020-11-11 21:43:40 +02:00
|
|
|
return valOfBonuses(Selector::type()(type).And(selector));
|
2010-08-25 11:40:50 +03:00
|
|
|
}
|
2010-12-06 01:10:02 +02:00
|
|
|
|
2017-07-15 13:08:20 +02:00
|
|
|
int IBonusBearer::valOfBonuses(Bonus::BonusType type, int subtype) const
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2022-09-14 11:00:40 +02:00
|
|
|
//This part is performance-critical
|
2023-03-13 23:26:44 +02:00
|
|
|
std::string cachingStr = "type_" + std::to_string(static_cast<int>(type)) + "_" + std::to_string(subtype);
|
2013-02-13 22:35:43 +03:00
|
|
|
|
2020-11-11 21:43:40 +02:00
|
|
|
CSelector s = Selector::type()(type);
|
2010-05-02 21:20:26 +03:00
|
|
|
if(subtype != -1)
|
2020-11-11 21:43:40 +02:00
|
|
|
s = s.And(Selector::subtype()(subtype));
|
2010-05-02 21:20:26 +03:00
|
|
|
|
2022-09-14 11:00:40 +02:00
|
|
|
return valOfBonuses(s, cachingStr);
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
2010-12-06 01:10:02 +02:00
|
|
|
|
2011-07-16 16:57:25 +03:00
|
|
|
int IBonusBearer::valOfBonuses(const CSelector &selector, const std::string &cachingStr) const
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
CSelector limit = nullptr;
|
2020-10-01 07:55:41 +02:00
|
|
|
TConstBonusListPtr hlp = getAllBonuses(selector, limit, nullptr, cachingStr);
|
2011-06-25 16:53:15 +03:00
|
|
|
return hlp->totalValue();
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
2017-07-15 13:08:20 +02:00
|
|
|
bool IBonusBearer::hasBonus(const CSelector &selector, const std::string &cachingStr) const
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2022-09-14 11:00:40 +02:00
|
|
|
//TODO: We don't need to count all bonuses and could break on first matching
|
2011-07-13 21:39:02 +03:00
|
|
|
return getBonuses(selector, cachingStr)->size() > 0;
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
|
|
|
|
2017-07-15 13:08:20 +02:00
|
|
|
bool IBonusBearer::hasBonus(const CSelector &selector, const CSelector &limit, const std::string &cachingStr) const
|
2016-09-30 16:11:17 +02:00
|
|
|
{
|
|
|
|
return getBonuses(selector, limit, cachingStr)->size() > 0;
|
|
|
|
}
|
|
|
|
|
2017-07-15 13:08:20 +02:00
|
|
|
bool IBonusBearer::hasBonusOfType(Bonus::BonusType type, int subtype) const
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2022-09-14 11:00:40 +02:00
|
|
|
//This part is performance-ciritcal
|
2023-03-13 23:26:44 +02:00
|
|
|
std::string cachingStr = "type_" + std::to_string(static_cast<int>(type)) + "_" + std::to_string(subtype);
|
2011-07-16 16:57:25 +03:00
|
|
|
|
2020-11-11 21:43:40 +02:00
|
|
|
CSelector s = Selector::type()(type);
|
2010-05-02 21:20:26 +03:00
|
|
|
if(subtype != -1)
|
2020-11-11 21:43:40 +02:00
|
|
|
s = s.And(Selector::subtype()(subtype));
|
2010-05-02 21:20:26 +03:00
|
|
|
|
2022-09-14 11:00:40 +02:00
|
|
|
return hasBonus(s, cachingStr);
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
|
|
|
|
2020-10-01 07:55:41 +02:00
|
|
|
TConstBonusListPtr IBonusBearer::getBonuses(const CSelector &selector, const std::string &cachingStr) const
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
return getAllBonuses(selector, nullptr, nullptr, cachingStr);
|
2010-04-03 06:33:46 +03:00
|
|
|
}
|
|
|
|
|
2020-10-01 07:55:41 +02:00
|
|
|
TConstBonusListPtr IBonusBearer::getBonuses(const CSelector &selector, const CSelector &limit, const std::string &cachingStr) const
|
2010-04-03 06:33:46 +03:00
|
|
|
{
|
2013-06-26 14:18:27 +03:00
|
|
|
return getAllBonuses(selector, limit, nullptr, cachingStr);
|
2010-04-03 06:33:46 +03:00
|
|
|
}
|
|
|
|
|
2013-02-02 01:04:25 +03:00
|
|
|
bool IBonusBearer::hasBonusFrom(Bonus::BonusSource source, ui32 sourceID) const
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2019-01-15 05:00:00 +02:00
|
|
|
boost::format fmt("source_%did_%d");
|
2023-03-13 23:26:44 +02:00
|
|
|
fmt % static_cast<int>(source) % sourceID;
|
2019-01-15 05:00:00 +02:00
|
|
|
|
|
|
|
return hasBonus(Selector::source(source,sourceID), fmt.str());
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
|
|
|
|
2011-02-27 21:58:14 +02:00
|
|
|
int IBonusBearer::MoraleVal() const
|
2010-04-03 06:33:46 +03:00
|
|
|
{
|
2020-06-28 15:19:27 +02:00
|
|
|
if(anaffectedByMorale.getHasBonus())
|
2010-05-02 21:20:26 +03:00
|
|
|
return 0;
|
2013-02-13 22:35:43 +03:00
|
|
|
|
2023-04-11 00:00:46 +02:00
|
|
|
return std::clamp(moraleValue.getValue(), -3, +3);
|
2010-04-03 06:33:46 +03:00
|
|
|
}
|
|
|
|
|
2011-02-27 21:58:14 +02:00
|
|
|
int IBonusBearer::LuckVal() const
|
2010-04-03 06:33:46 +03:00
|
|
|
{
|
2010-05-02 21:20:26 +03:00
|
|
|
if(hasBonusOfType(Bonus::NO_LUCK))
|
|
|
|
return 0;
|
2013-02-13 22:35:43 +03:00
|
|
|
|
2023-04-11 00:00:46 +02:00
|
|
|
return std::clamp(luckValue.getValue(), -3, +3);
|
2020-11-11 21:43:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int IBonusBearer::MoraleValAndBonusList(TConstBonusListPtr & bonusList) const
|
|
|
|
{
|
|
|
|
if(anaffectedByMorale.getHasBonus())
|
|
|
|
{
|
|
|
|
if(!bonusList->empty())
|
|
|
|
bonusList = std::make_shared<const BonusList>();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-04-11 00:00:46 +02:00
|
|
|
return std::clamp(moraleValue.getValueAndList(bonusList), -3, +3);
|
2020-11-11 21:43:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int IBonusBearer::LuckValAndBonusList(TConstBonusListPtr & bonusList) const
|
|
|
|
{
|
|
|
|
if(hasBonusOfType(Bonus::NO_LUCK))
|
|
|
|
{
|
|
|
|
if(!bonusList->empty())
|
|
|
|
bonusList = std::make_shared<const BonusList>();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-04-11 00:00:46 +02:00
|
|
|
return std::clamp(luckValue.getValueAndList(bonusList), -3, +3);
|
2010-04-03 06:33:46 +03:00
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
ui32 IBonusBearer::MaxHealth() const
|
2010-04-03 06:33:46 +03:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
const std::string cachingStr = "type_STACK_HEALTH";
|
2020-11-11 21:43:40 +02:00
|
|
|
static const auto selector = Selector::type()(Bonus::STACK_HEALTH);
|
2017-07-20 06:08:49 +02:00
|
|
|
auto value = valOfBonuses(selector, cachingStr);
|
|
|
|
return std::max(1, value); //never 0
|
2010-04-03 06:33:46 +03:00
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
int IBonusBearer::getAttack(bool ranged) const
|
2010-04-03 06:33:46 +03:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
const std::string cachingStr = "type_PRIMARY_SKILLs_ATTACK";
|
2010-04-03 06:33:46 +03:00
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
static const auto selector = Selector::typeSubtype(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK);
|
2010-04-03 06:33:46 +03:00
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
return getBonuses(selector, nullptr, cachingStr)->totalValue();
|
2010-04-03 06:33:46 +03:00
|
|
|
}
|
|
|
|
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
int IBonusBearer::getDefense(bool ranged) const
|
2010-04-03 06:33:46 +03:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
const std::string cachingStr = "type_PRIMARY_SKILLs_DEFENSE";
|
|
|
|
|
|
|
|
static const auto selector = Selector::typeSubtype(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE);
|
|
|
|
|
|
|
|
return getBonuses(selector, nullptr, cachingStr)->totalValue();
|
2010-04-03 06:33:46 +03:00
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
int IBonusBearer::getMinDamage(bool ranged) const
|
2010-08-30 21:06:17 +03:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
const std::string cachingStr = "type_CREATURE_DAMAGEs_0Otype_CREATURE_DAMAGEs_1";
|
|
|
|
static const auto selector = Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 0).Or(Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 1));
|
|
|
|
return valOfBonuses(selector, cachingStr);
|
2010-08-30 21:06:17 +03:00
|
|
|
}
|
2017-07-20 06:08:49 +02:00
|
|
|
|
|
|
|
int IBonusBearer::getMaxDamage(bool ranged) const
|
2010-08-30 21:06:17 +03:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
const std::string cachingStr = "type_CREATURE_DAMAGEs_0Otype_CREATURE_DAMAGEs_2";
|
|
|
|
static const auto selector = Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 0).Or(Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 2));
|
|
|
|
return valOfBonuses(selector, cachingStr);
|
2010-08-30 21:06:17 +03:00
|
|
|
}
|
|
|
|
|
2013-02-06 13:16:44 +03:00
|
|
|
int IBonusBearer::getPrimSkillLevel(PrimarySkill::PrimarySkill id) const
|
2011-02-27 21:58:14 +02:00
|
|
|
{
|
2020-11-11 21:43:40 +02:00
|
|
|
static const CSelector selectorAllSkills = Selector::type()(Bonus::PRIMARY_SKILL);
|
2019-01-15 05:00:00 +02:00
|
|
|
static const std::string keyAllSkills = "type_PRIMARY_SKILL";
|
|
|
|
auto allSkills = getBonuses(selectorAllSkills, keyAllSkills);
|
2021-02-12 23:24:14 +02:00
|
|
|
auto ret = allSkills->valOfBonuses(Selector::subtype()(id));
|
2021-03-23 16:47:07 +02:00
|
|
|
auto minSkillValue = (id == PrimarySkill::SPELL_POWER || id == PrimarySkill::KNOWLEDGE) ? 1 : 0;
|
|
|
|
vstd::amax(ret, minSkillValue); //otherwise, some artifacts may cause negative skill value effect
|
2021-02-12 23:24:14 +02:00
|
|
|
return ret; //sp=0 works in old saves
|
2011-02-27 21:58:14 +02:00
|
|
|
}
|
|
|
|
|
2011-05-29 21:28:50 +03:00
|
|
|
si32 IBonusBearer::magicResistance() const
|
|
|
|
{
|
2011-07-16 16:57:25 +03:00
|
|
|
return valOfBonuses(Bonus::MAGIC_RESISTANCE);
|
2011-05-29 21:28:50 +03:00
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
ui32 IBonusBearer::Speed(int turn, bool useBind) const
|
2013-07-22 13:39:11 +03:00
|
|
|
{
|
2013-07-22 20:06:32 +03:00
|
|
|
//war machines cannot move
|
2020-11-11 21:43:40 +02:00
|
|
|
if(hasBonus(Selector::type()(Bonus::SIEGE_WEAPON).And(Selector::turns(turn))))
|
2013-07-22 13:39:11 +03:00
|
|
|
{
|
2013-07-22 20:06:32 +03:00
|
|
|
return 0;
|
2013-07-22 13:39:11 +03:00
|
|
|
}
|
|
|
|
//bind effect check - doesn't influence stack initiative
|
2020-11-11 21:43:40 +02:00
|
|
|
if(useBind && hasBonus(Selector::type()(Bonus::BIND_EFFECT).And(Selector::turns(turn))))
|
2013-07-22 13:39:11 +03:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-11-11 21:43:40 +02:00
|
|
|
return valOfBonuses(Selector::type()(Bonus::STACKS_SPEED).And(Selector::turns(turn)));
|
2016-08-18 14:03:20 +02:00
|
|
|
}
|
2013-07-22 13:39:11 +03:00
|
|
|
|
2011-06-25 18:05:01 +03:00
|
|
|
bool IBonusBearer::isLiving() const //TODO: theoreticaly there exists "LIVING" bonus in stack experience documentation
|
|
|
|
{
|
2019-01-15 05:00:00 +02:00
|
|
|
static const std::string cachingStr = "IBonusBearer::isLiving";
|
2020-11-11 21:43:40 +02:00
|
|
|
static const CSelector selector = Selector::type()(Bonus::UNDEAD)
|
|
|
|
.Or(Selector::type()(Bonus::NON_LIVING))
|
|
|
|
.Or(Selector::type()(Bonus::GARGOYLE))
|
|
|
|
.Or(Selector::type()(Bonus::SIEGE_WEAPON));
|
2019-01-15 05:00:00 +02:00
|
|
|
|
|
|
|
return !hasBonus(selector, cachingStr);
|
2011-06-25 18:05:01 +03:00
|
|
|
}
|
|
|
|
|
2020-10-01 07:55:41 +02:00
|
|
|
std::shared_ptr<const Bonus> IBonusBearer::getBonus(const CSelector &selector) const
|
2012-09-20 19:55:21 +03:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
auto bonuses = getAllBonuses(selector, Selector::all);
|
|
|
|
return bonuses->getFirst(Selector::all);
|
2012-09-20 19:55:21 +03:00
|
|
|
}
|
|
|
|
|
2021-03-23 16:47:07 +02:00
|
|
|
const CStack * retrieveStackBattle(const CBonusSystemNode * node)
|
|
|
|
{
|
2021-08-19 00:45:28 +02:00
|
|
|
switch(node->getNodeType())
|
2021-03-23 16:47:07 +02:00
|
|
|
{
|
|
|
|
case CBonusSystemNode::STACK_BATTLE:
|
2023-03-13 23:26:44 +02:00
|
|
|
return dynamic_cast<const CStack *>(node);
|
2021-03-23 16:47:07 +02:00
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const CStackInstance * retrieveStackInstance(const CBonusSystemNode * node)
|
|
|
|
{
|
2021-08-19 00:45:28 +02:00
|
|
|
switch(node->getNodeType())
|
2021-03-23 16:47:07 +02:00
|
|
|
{
|
|
|
|
case CBonusSystemNode::STACK_INSTANCE:
|
2023-03-13 23:26:44 +02:00
|
|
|
return (dynamic_cast<const CStackInstance *>(node));
|
2021-03-23 16:47:07 +02:00
|
|
|
case CBonusSystemNode::STACK_BATTLE:
|
2023-03-13 23:26:44 +02:00
|
|
|
return (dynamic_cast<const CStack *>(node))->base;
|
2021-03-23 16:47:07 +02:00
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PlayerColor CBonusSystemNode::retrieveNodeOwner(const CBonusSystemNode * node)
|
|
|
|
{
|
2021-09-12 13:30:54 +02:00
|
|
|
return node ? node->getOwner() : PlayerColor::CANNOT_DETERMINE;
|
2021-03-23 16:47:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Bonus> CBonusSystemNode::getBonusLocalFirst(const CSelector & selector)
|
2011-02-27 21:58:14 +02:00
|
|
|
{
|
2016-09-19 23:36:35 +02:00
|
|
|
auto ret = bonuses.getFirst(selector);
|
2011-02-27 21:58:14 +02:00
|
|
|
if(ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
FOREACH_PARENT(pname)
|
|
|
|
{
|
2012-09-20 19:55:21 +03:00
|
|
|
ret = pname->getBonusLocalFirst(selector);
|
2011-02-27 21:58:14 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
return nullptr;
|
2011-02-27 21:58:14 +02:00
|
|
|
}
|
|
|
|
|
2021-03-23 16:47:07 +02:00
|
|
|
std::shared_ptr<const Bonus> CBonusSystemNode::getBonusLocalFirst(const CSelector & selector) const
|
2011-02-27 21:58:14 +02:00
|
|
|
{
|
2012-09-20 19:55:21 +03:00
|
|
|
return (const_cast<CBonusSystemNode*>(this))->getBonusLocalFirst(selector);
|
2011-02-27 21:58:14 +02:00
|
|
|
}
|
|
|
|
|
2018-02-10 20:52:23 +02:00
|
|
|
void CBonusSystemNode::getParents(TCNodes & out) const /*retrieves list of parent nodes (nodes to inherit bonuses from) */
|
2011-02-27 21:58:14 +02:00
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const auto & elem : parents)
|
2011-06-25 16:53:15 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
const CBonusSystemNode *parent = elem;
|
2011-02-27 21:58:14 +02:00
|
|
|
out.insert(parent);
|
2011-06-25 16:53:15 +03:00
|
|
|
}
|
2011-02-27 21:58:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CBonusSystemNode::getParents(TNodes &out)
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for (auto & elem : parents)
|
2011-06-25 16:53:15 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
const CBonusSystemNode *parent = elem;
|
2011-02-27 21:58:14 +02:00
|
|
|
out.insert(const_cast<CBonusSystemNode*>(parent));
|
2013-02-13 22:35:43 +03:00
|
|
|
}
|
2011-02-27 21:58:14 +02:00
|
|
|
}
|
|
|
|
|
2021-03-23 16:47:07 +02:00
|
|
|
void CBonusSystemNode::getAllParents(TCNodes & out) const //retrieves list of parent nodes (nodes to inherit bonuses from)
|
2011-02-27 21:58:14 +02:00
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
for(auto * parent : parents)
|
2012-03-06 19:59:55 +03:00
|
|
|
{
|
2021-02-05 00:56:21 +02:00
|
|
|
out.insert(parent);
|
|
|
|
parent->getAllParents(out);
|
2012-03-06 19:59:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-12 13:39:36 +02:00
|
|
|
void CBonusSystemNode::getAllBonusesRec(BonusList &out, const CSelector & selector) const
|
2012-03-06 19:59:55 +03:00
|
|
|
{
|
2022-09-14 11:00:40 +02:00
|
|
|
//out has been reserved sufficient capacity at getAllBonuses() call
|
|
|
|
|
2017-09-16 02:26:48 +02:00
|
|
|
BonusList beforeUpdate;
|
2021-02-05 00:56:21 +02:00
|
|
|
TCNodes lparents;
|
|
|
|
getAllParents(lparents);
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
if(!lparents.empty())
|
2022-09-14 11:00:40 +02:00
|
|
|
{
|
|
|
|
//estimate on how many bonuses are missing yet - must be positive
|
|
|
|
beforeUpdate.reserve(std::max(out.capacity() - out.size(), bonuses.size()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
beforeUpdate.reserve(bonuses.size()); //at most all local bonuses
|
|
|
|
}
|
2021-02-05 00:56:21 +02:00
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const auto * parent : lparents)
|
2022-09-14 11:00:40 +02:00
|
|
|
{
|
2023-03-12 13:39:36 +02:00
|
|
|
parent->getAllBonusesRec(beforeUpdate, selector);
|
2022-09-14 11:00:40 +02:00
|
|
|
}
|
2017-09-16 02:26:48 +02:00
|
|
|
bonuses.getAllBonuses(beforeUpdate);
|
2012-03-06 19:59:55 +03:00
|
|
|
|
2022-09-14 11:37:12 +02:00
|
|
|
for(const auto & b : beforeUpdate)
|
2021-09-12 13:30:54 +02:00
|
|
|
{
|
2023-03-12 13:39:36 +02:00
|
|
|
//We should not run updaters on non-selected bonuses
|
|
|
|
auto updated = selector(b.get()) && b->updater
|
2023-01-14 19:05:36 +02:00
|
|
|
? getUpdatedBonus(b, b->updater)
|
2021-09-12 13:30:54 +02:00
|
|
|
: b;
|
2022-09-14 11:00:40 +02:00
|
|
|
|
2023-01-14 19:05:36 +02:00
|
|
|
//do not add bonus with updater
|
|
|
|
bool bonusExists = false;
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const auto & bonus : out)
|
2023-01-14 19:05:36 +02:00
|
|
|
{
|
|
|
|
if (bonus == updated)
|
|
|
|
bonusExists = true;
|
|
|
|
if (bonus->updater && bonus->updater == updated->updater)
|
|
|
|
bonusExists = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bonusExists)
|
2022-09-05 09:38:09 +02:00
|
|
|
out.push_back(updated);
|
2021-09-12 13:30:54 +02:00
|
|
|
}
|
2011-02-27 21:58:14 +02:00
|
|
|
}
|
|
|
|
|
2020-10-01 07:55:41 +02:00
|
|
|
TConstBonusListPtr CBonusSystemNode::getAllBonuses(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root, const std::string &cachingStr) const
|
2011-06-25 16:53:15 +03:00
|
|
|
{
|
2012-03-06 19:59:55 +03:00
|
|
|
bool limitOnUs = (!root || root == this); //caching won't work when we want to limit bonuses against an external node
|
2013-02-13 22:35:43 +03:00
|
|
|
if (CBonusSystemNode::cachingEnabled && limitOnUs)
|
2011-06-25 16:53:15 +03:00
|
|
|
{
|
2011-07-16 16:57:25 +03:00
|
|
|
// Exclusive access for one thread
|
2021-05-16 19:53:11 +02:00
|
|
|
boost::lock_guard<boost::mutex> lock(sync);
|
2011-07-13 21:39:02 +03:00
|
|
|
|
2013-02-13 22:35:43 +03:00
|
|
|
// If the bonus system tree changes(state of a single node or the relations to each other) then
|
2011-07-16 16:57:25 +03:00
|
|
|
// cache all bonus objects. Selector objects doesn't matter.
|
2011-06-25 16:53:15 +03:00
|
|
|
if (cachedLast != treeChanged)
|
|
|
|
{
|
2022-09-14 11:00:40 +02:00
|
|
|
BonusList allBonuses;
|
|
|
|
allBonuses.reserve(cachedBonuses.capacity()); //we assume we'll get about the same number of bonuses
|
|
|
|
|
2012-03-06 19:59:55 +03:00
|
|
|
cachedBonuses.clear();
|
2011-06-25 16:53:15 +03:00
|
|
|
cachedRequests.clear();
|
2012-03-06 19:59:55 +03:00
|
|
|
|
2023-03-12 13:39:36 +02:00
|
|
|
getAllBonusesRec(allBonuses, Selector::all);
|
2012-03-06 19:59:55 +03:00
|
|
|
limitBonuses(allBonuses, cachedBonuses);
|
2018-03-27 09:54:58 +02:00
|
|
|
cachedBonuses.stackBonuses();
|
2012-03-06 19:59:55 +03:00
|
|
|
|
2011-06-25 16:53:15 +03:00
|
|
|
cachedLast = treeChanged;
|
|
|
|
}
|
2013-02-13 22:35:43 +03:00
|
|
|
|
2011-07-16 16:57:25 +03:00
|
|
|
// If a bonus system request comes with a caching string then look up in the map if there are any
|
|
|
|
// pre-calculated bonus results. Limiters can't be cached so they have to be calculated.
|
2022-11-15 02:20:55 +02:00
|
|
|
if(!cachingStr.empty())
|
2011-06-25 16:53:15 +03:00
|
|
|
{
|
2012-03-06 19:59:55 +03:00
|
|
|
auto it = cachedRequests.find(cachingStr);
|
|
|
|
if(it != cachedRequests.end())
|
2011-06-25 16:53:15 +03:00
|
|
|
{
|
2012-03-06 19:59:55 +03:00
|
|
|
//Cached list contains bonuses for our query with applied limiters
|
|
|
|
return it->second;
|
2011-06-25 16:53:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-06 19:59:55 +03:00
|
|
|
//We still don't have the bonuses (didn't returned them from cache)
|
|
|
|
//Perform bonus selection
|
2015-12-29 04:43:33 +02:00
|
|
|
auto ret = std::make_shared<BonusList>();
|
2013-02-13 22:35:43 +03:00
|
|
|
cachedBonuses.getBonuses(*ret, selector, limit);
|
2012-03-06 19:59:55 +03:00
|
|
|
|
|
|
|
// Save the results in the cache
|
2022-11-15 02:20:55 +02:00
|
|
|
if(!cachingStr.empty())
|
2011-06-25 16:53:15 +03:00
|
|
|
cachedRequests[cachingStr] = ret;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-06 19:59:55 +03:00
|
|
|
return getAllBonusesWithoutCaching(selector, limit, root);
|
|
|
|
}
|
|
|
|
}
|
2011-07-16 16:57:25 +03:00
|
|
|
|
2020-10-01 07:55:41 +02:00
|
|
|
TConstBonusListPtr CBonusSystemNode::getAllBonusesWithoutCaching(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root) const
|
2012-03-06 19:59:55 +03:00
|
|
|
{
|
2015-12-29 04:43:33 +02:00
|
|
|
auto ret = std::make_shared<BonusList>();
|
2011-07-16 16:57:25 +03:00
|
|
|
|
2012-03-06 19:59:55 +03:00
|
|
|
// Get bonus results without caching enabled.
|
2023-03-13 23:26:44 +02:00
|
|
|
BonusList beforeLimiting;
|
|
|
|
BonusList afterLimiting;
|
2023-03-12 13:39:36 +02:00
|
|
|
getAllBonusesRec(beforeLimiting, selector);
|
2012-03-06 19:59:55 +03:00
|
|
|
|
|
|
|
if(!root || root == this)
|
|
|
|
{
|
|
|
|
limitBonuses(beforeLimiting, afterLimiting);
|
2011-06-25 16:53:15 +03:00
|
|
|
}
|
2012-03-06 19:59:55 +03:00
|
|
|
else if(root)
|
|
|
|
{
|
2013-02-13 22:35:43 +03:00
|
|
|
//We want to limit our query against an external node. We get all its bonuses,
|
2012-03-06 19:59:55 +03:00
|
|
|
// add the ones we're considering and see if they're cut out by limiters
|
2023-03-13 23:26:44 +02:00
|
|
|
BonusList rootBonuses;
|
|
|
|
BonusList limitedRootBonuses;
|
2023-03-12 13:39:36 +02:00
|
|
|
getAllBonusesRec(rootBonuses, selector);
|
2012-03-06 19:59:55 +03:00
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const auto & b : beforeLimiting)
|
2012-03-06 19:59:55 +03:00
|
|
|
rootBonuses.push_back(b);
|
|
|
|
|
|
|
|
root->limitBonuses(rootBonuses, limitedRootBonuses);
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const auto & b : beforeLimiting)
|
2012-03-06 19:59:55 +03:00
|
|
|
if(vstd::contains(limitedRootBonuses, b))
|
|
|
|
afterLimiting.push_back(b);
|
|
|
|
|
|
|
|
}
|
2018-03-27 09:54:58 +02:00
|
|
|
afterLimiting.getBonuses(*ret, selector, limit);
|
|
|
|
ret->stackBonuses();
|
2012-03-06 19:59:55 +03:00
|
|
|
return ret;
|
2011-06-25 16:53:15 +03:00
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
std::shared_ptr<Bonus> CBonusSystemNode::getUpdatedBonus(const std::shared_ptr<Bonus> & b, const TUpdaterPtr & updater) const
|
2017-09-16 02:26:48 +02:00
|
|
|
{
|
2021-09-12 13:30:54 +02:00
|
|
|
assert(updater);
|
|
|
|
return updater->createUpdatedBonus(b, * this);
|
2017-09-16 02:26:48 +02:00
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
CBonusSystemNode::CBonusSystemNode()
|
2021-05-23 13:28:43 +02:00
|
|
|
:CBonusSystemNode(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
CBonusSystemNode::CBonusSystemNode(bool isHypotetic):
|
|
|
|
bonuses(true),
|
2017-07-20 06:08:49 +02:00
|
|
|
exportedBonuses(true),
|
|
|
|
nodeType(UNKNOWN),
|
2021-05-16 19:53:11 +02:00
|
|
|
cachedLast(0),
|
2021-05-23 13:28:43 +02:00
|
|
|
isHypotheticNode(isHypotetic)
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
CBonusSystemNode::CBonusSystemNode(ENodeTypes NodeType):
|
|
|
|
bonuses(true),
|
2017-07-20 06:08:49 +02:00
|
|
|
exportedBonuses(true),
|
|
|
|
nodeType(NodeType),
|
2021-05-16 19:53:11 +02:00
|
|
|
cachedLast(0),
|
2021-03-01 21:38:50 +02:00
|
|
|
isHypotheticNode(false)
|
2010-07-12 13:20:25 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
CBonusSystemNode::CBonusSystemNode(CBonusSystemNode && other) noexcept:
|
2016-08-15 08:28:00 +02:00
|
|
|
bonuses(std::move(other.bonuses)),
|
|
|
|
exportedBonuses(std::move(other.exportedBonuses)),
|
2016-07-29 11:27:55 +02:00
|
|
|
nodeType(other.nodeType),
|
|
|
|
description(other.description),
|
2021-05-16 19:53:11 +02:00
|
|
|
cachedLast(0),
|
2021-05-23 13:28:43 +02:00
|
|
|
isHypotheticNode(other.isHypotheticNode)
|
2016-07-29 11:27:55 +02:00
|
|
|
{
|
|
|
|
std::swap(parents, other.parents);
|
|
|
|
std::swap(children, other.children);
|
|
|
|
|
2016-08-15 10:04:45 +02:00
|
|
|
//fixing bonus tree without recalculation
|
|
|
|
|
2021-05-23 13:28:43 +02:00
|
|
|
if(!isHypothetic())
|
2016-08-15 10:04:45 +02:00
|
|
|
{
|
2021-05-23 13:28:43 +02:00
|
|
|
for(CBonusSystemNode * n : parents)
|
|
|
|
{
|
|
|
|
n->children -= &other;
|
|
|
|
n->children.push_back(this);
|
|
|
|
}
|
2016-08-15 10:04:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for(CBonusSystemNode * n : children)
|
|
|
|
{
|
|
|
|
n->parents -= &other;
|
|
|
|
n->parents.push_back(this);
|
|
|
|
}
|
|
|
|
|
2016-08-15 08:28:00 +02:00
|
|
|
//cache ignored
|
2016-07-29 11:27:55 +02:00
|
|
|
|
|
|
|
//cachedBonuses
|
|
|
|
//cachedRequests
|
|
|
|
}
|
|
|
|
|
2010-07-12 13:20:25 +03:00
|
|
|
CBonusSystemNode::~CBonusSystemNode()
|
|
|
|
{
|
2010-12-06 01:10:02 +02:00
|
|
|
detachFromAll();
|
2010-07-12 13:20:25 +03:00
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
if(!children.empty())
|
2010-11-27 03:46:19 +02:00
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
while(!children.empty())
|
2022-11-06 01:26:13 +02:00
|
|
|
children.front()->detachFrom(*this);
|
2010-11-27 03:46:19 +02:00
|
|
|
}
|
2010-07-12 13:20:25 +03:00
|
|
|
}
|
|
|
|
|
2022-11-06 01:26:13 +02:00
|
|
|
void CBonusSystemNode::attachTo(CBonusSystemNode & parent)
|
2010-11-13 22:26:15 +02:00
|
|
|
{
|
2022-11-06 01:26:13 +02:00
|
|
|
assert(!vstd::contains(parents, &parent));
|
|
|
|
parents.push_back(&parent);
|
2011-02-21 06:13:00 +02:00
|
|
|
|
2021-03-01 21:38:50 +02:00
|
|
|
if(!isHypothetic())
|
|
|
|
{
|
2022-11-06 01:26:13 +02:00
|
|
|
if(parent.actsAsBonusSourceOnly())
|
|
|
|
parent.newRedDescendant(*this);
|
2021-03-01 21:38:50 +02:00
|
|
|
else
|
|
|
|
newRedDescendant(parent);
|
|
|
|
|
2022-11-06 01:26:13 +02:00
|
|
|
parent.newChildAttached(*this);
|
2021-03-01 21:38:50 +02:00
|
|
|
}
|
2010-11-22 02:34:46 +02:00
|
|
|
|
2015-04-11 07:13:28 +02:00
|
|
|
CBonusSystemNode::treeHasChanged();
|
2010-11-13 22:26:15 +02:00
|
|
|
}
|
|
|
|
|
2022-11-06 01:26:13 +02:00
|
|
|
void CBonusSystemNode::detachFrom(CBonusSystemNode & parent)
|
2010-11-13 22:26:15 +02:00
|
|
|
{
|
2022-11-06 01:26:13 +02:00
|
|
|
assert(vstd::contains(parents, &parent));
|
2010-11-13 22:26:15 +02:00
|
|
|
|
2021-03-01 21:38:50 +02:00
|
|
|
if(!isHypothetic())
|
|
|
|
{
|
2022-11-06 01:26:13 +02:00
|
|
|
if(parent.actsAsBonusSourceOnly())
|
|
|
|
parent.removedRedDescendant(*this);
|
2021-03-01 21:38:50 +02:00
|
|
|
else
|
|
|
|
removedRedDescendant(parent);
|
|
|
|
}
|
2011-02-21 06:13:00 +02:00
|
|
|
|
2022-11-06 01:26:13 +02:00
|
|
|
if (vstd::contains(parents, &parent))
|
2021-03-23 16:47:07 +02:00
|
|
|
{
|
2022-11-06 01:26:13 +02:00
|
|
|
parents -= &parent;
|
2021-03-23 16:47:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logBonus->error("Error on Detach. Node %s (nodeType=%d) has not parent %s (nodeType=%d)"
|
2022-11-06 01:26:13 +02:00
|
|
|
, nodeShortInfo(), nodeType, parent.nodeShortInfo(), parent.nodeType);
|
2021-03-23 16:47:07 +02:00
|
|
|
}
|
2021-03-01 21:38:50 +02:00
|
|
|
|
|
|
|
if(!isHypothetic())
|
|
|
|
{
|
2022-11-06 01:26:13 +02:00
|
|
|
parent.childDetached(*this);
|
2021-03-01 21:38:50 +02:00
|
|
|
}
|
2015-04-11 07:13:28 +02:00
|
|
|
CBonusSystemNode::treeHasChanged();
|
2010-11-13 22:26:15 +02:00
|
|
|
}
|
|
|
|
|
2018-03-05 13:03:14 +02:00
|
|
|
void CBonusSystemNode::removeBonusesRecursive(const CSelector & s)
|
2010-11-19 00:06:56 +02:00
|
|
|
{
|
2018-03-05 13:03:14 +02:00
|
|
|
removeBonuses(s);
|
|
|
|
for(CBonusSystemNode * child : children)
|
2018-03-05 08:51:35 +02:00
|
|
|
child->removeBonusesRecursive(s);
|
2010-11-19 00:06:56 +02:00
|
|
|
}
|
|
|
|
|
2017-09-09 14:25:55 +02:00
|
|
|
void CBonusSystemNode::reduceBonusDurations(const CSelector &s)
|
2015-11-09 13:18:36 +02:00
|
|
|
{
|
|
|
|
BonusList bl;
|
2016-09-30 16:11:17 +02:00
|
|
|
exportedBonuses.getBonuses(bl, s, Selector::all);
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const auto & b : bl)
|
2015-11-09 13:18:36 +02:00
|
|
|
{
|
2015-11-09 14:48:07 +02:00
|
|
|
b->turnsRemain--;
|
|
|
|
if(b->turnsRemain <= 0)
|
|
|
|
removeBonus(b);
|
2015-11-09 13:18:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for(CBonusSystemNode *child : children)
|
2017-09-09 14:25:55 +02:00
|
|
|
child->reduceBonusDurations(s);
|
|
|
|
}
|
|
|
|
|
2016-09-19 23:36:35 +02:00
|
|
|
void CBonusSystemNode::addNewBonus(const std::shared_ptr<Bonus>& b)
|
2010-11-19 00:06:56 +02:00
|
|
|
{
|
2015-11-20 13:37:57 +02:00
|
|
|
//turnsRemain shouldn't be zero for following durations
|
2016-09-19 23:36:35 +02:00
|
|
|
if(Bonus::NTurns(b.get()) || Bonus::NDays(b.get()) || Bonus::OneWeek(b.get()))
|
2015-11-20 13:37:57 +02:00
|
|
|
{
|
|
|
|
assert(b->turnsRemain);
|
|
|
|
}
|
|
|
|
|
2016-09-19 23:36:35 +02:00
|
|
|
assert(!vstd::contains(exportedBonuses, b));
|
2010-11-19 00:22:51 +02:00
|
|
|
exportedBonuses.push_back(b);
|
2011-02-23 05:57:45 +02:00
|
|
|
exportBonus(b);
|
2015-04-11 07:13:28 +02:00
|
|
|
CBonusSystemNode::treeHasChanged();
|
2010-11-19 00:06:56 +02:00
|
|
|
}
|
|
|
|
|
2016-09-19 23:36:35 +02:00
|
|
|
void CBonusSystemNode::accumulateBonus(const std::shared_ptr<Bonus>& b)
|
2012-04-04 20:41:55 +03:00
|
|
|
{
|
2016-09-19 23:36:35 +02:00
|
|
|
auto bonus = exportedBonuses.getFirst(Selector::typeSubtype(b->type, b->subtype)); //only local bonuses are interesting //TODO: what about value type?
|
2012-04-04 20:41:55 +03:00
|
|
|
if(bonus)
|
2016-09-19 23:36:35 +02:00
|
|
|
bonus->val += b->val;
|
2012-04-04 20:41:55 +03:00
|
|
|
else
|
2016-09-19 23:36:35 +02:00
|
|
|
addNewBonus(std::make_shared<Bonus>(*b)); //duplicate needed, original may get destroyed
|
2012-04-04 20:41:55 +03:00
|
|
|
}
|
|
|
|
|
2016-09-19 23:36:35 +02:00
|
|
|
void CBonusSystemNode::removeBonus(const std::shared_ptr<Bonus>& b)
|
2010-11-19 00:06:56 +02:00
|
|
|
{
|
2010-11-19 00:22:51 +02:00
|
|
|
exportedBonuses -= b;
|
2010-11-20 02:03:31 +02:00
|
|
|
if(b->propagator)
|
2011-02-21 06:13:00 +02:00
|
|
|
unpropagateBonus(b);
|
2010-11-20 02:03:31 +02:00
|
|
|
else
|
2011-02-21 06:13:00 +02:00
|
|
|
bonuses -= b;
|
2015-04-11 07:13:28 +02:00
|
|
|
CBonusSystemNode::treeHasChanged();
|
2010-11-20 02:03:31 +02:00
|
|
|
}
|
|
|
|
|
2018-03-05 13:03:14 +02:00
|
|
|
void CBonusSystemNode::removeBonuses(const CSelector & selector)
|
2018-02-28 21:44:05 +02:00
|
|
|
{
|
|
|
|
BonusList toRemove;
|
2018-03-05 13:03:14 +02:00
|
|
|
exportedBonuses.getBonuses(toRemove, selector, Selector::all);
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const auto & bonus : toRemove)
|
2018-02-28 21:44:05 +02:00
|
|
|
removeBonus(bonus);
|
|
|
|
}
|
|
|
|
|
2011-02-21 06:13:00 +02:00
|
|
|
bool CBonusSystemNode::actsAsBonusSourceOnly() const
|
2010-11-20 19:36:02 +02:00
|
|
|
{
|
|
|
|
switch(nodeType)
|
|
|
|
{
|
|
|
|
case CREATURE:
|
|
|
|
case ARTIFACT:
|
|
|
|
case ARTIFACT_INSTANCE:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
void CBonusSystemNode::propagateBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & source)
|
2010-11-20 19:36:02 +02:00
|
|
|
{
|
2011-02-21 06:13:00 +02:00
|
|
|
if(b->propagator->shouldBeAttached(this))
|
|
|
|
{
|
2021-09-12 13:30:54 +02:00
|
|
|
auto propagated = b->propagationUpdater
|
|
|
|
? source.getUpdatedBonus(b, b->propagationUpdater)
|
|
|
|
: b;
|
|
|
|
bonuses.push_back(propagated);
|
|
|
|
logBonus->trace("#$# %s #propagated to# %s", propagated->Description(), nodeName());
|
2011-02-21 06:13:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
FOREACH_RED_CHILD(child)
|
2021-09-12 13:30:54 +02:00
|
|
|
child->propagateBonus(b, source);
|
2010-11-20 19:36:02 +02:00
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
void CBonusSystemNode::unpropagateBonus(const std::shared_ptr<Bonus> & b)
|
2010-11-22 02:34:46 +02:00
|
|
|
{
|
2011-02-21 06:13:00 +02:00
|
|
|
if(b->propagator->shouldBeAttached(this))
|
|
|
|
{
|
|
|
|
bonuses -= b;
|
2017-08-11 19:03:05 +02:00
|
|
|
logBonus->trace("#$# %s #is no longer propagated to# %s", b->Description(), nodeName());
|
2011-02-21 06:13:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
FOREACH_RED_CHILD(child)
|
|
|
|
child->unpropagateBonus(b);
|
2010-11-22 02:34:46 +02:00
|
|
|
}
|
|
|
|
|
2022-11-06 01:26:13 +02:00
|
|
|
void CBonusSystemNode::newChildAttached(CBonusSystemNode & child)
|
2010-11-22 02:34:46 +02:00
|
|
|
{
|
2022-11-06 01:26:13 +02:00
|
|
|
assert(!vstd::contains(children, &child));
|
|
|
|
children.push_back(&child);
|
2010-11-22 02:34:46 +02:00
|
|
|
}
|
|
|
|
|
2022-11-06 01:26:13 +02:00
|
|
|
void CBonusSystemNode::childDetached(CBonusSystemNode & child)
|
2010-11-22 02:34:46 +02:00
|
|
|
{
|
2022-11-06 01:26:13 +02:00
|
|
|
if(vstd::contains(children, &child))
|
|
|
|
children -= &child;
|
2014-09-17 12:03:53 +03:00
|
|
|
else
|
|
|
|
{
|
2021-03-23 16:47:07 +02:00
|
|
|
logBonus->error("Error on Detach. Node %s (nodeType=%d) is not a child of %s (nodeType=%d)"
|
2022-11-06 01:26:13 +02:00
|
|
|
, child.nodeShortInfo(), child.nodeType, nodeShortInfo(), nodeType);
|
2014-09-17 12:03:53 +03:00
|
|
|
}
|
2010-11-22 02:34:46 +02:00
|
|
|
}
|
|
|
|
|
2010-12-06 01:10:02 +02:00
|
|
|
void CBonusSystemNode::detachFromAll()
|
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
while(!parents.empty())
|
2022-11-06 01:26:13 +02:00
|
|
|
detachFrom(*parents.front());
|
2010-12-06 01:10:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CBonusSystemNode::isIndependentNode() const
|
|
|
|
{
|
|
|
|
return parents.empty() && children.empty();
|
|
|
|
}
|
|
|
|
|
2010-12-12 01:11:26 +02:00
|
|
|
std::string CBonusSystemNode::nodeName() const
|
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
return !description.empty()
|
2011-02-10 15:12:53 +02:00
|
|
|
? description
|
|
|
|
: std::string("Bonus system node of type ") + typeid(*this).name();
|
2010-12-12 01:11:26 +02:00
|
|
|
}
|
|
|
|
|
2021-03-23 16:47:07 +02:00
|
|
|
std::string CBonusSystemNode::nodeShortInfo() const
|
|
|
|
{
|
|
|
|
std::ostringstream str;
|
|
|
|
str << "'" << typeid(* this).name() << "'";
|
|
|
|
description.length() > 0
|
|
|
|
? str << " (" << description << ")"
|
|
|
|
: str << " (no description)";
|
|
|
|
return str.str();
|
|
|
|
}
|
|
|
|
|
2011-02-04 16:58:14 +02:00
|
|
|
void CBonusSystemNode::deserializationFix()
|
|
|
|
{
|
2011-03-01 12:19:05 +02:00
|
|
|
exportBonuses();
|
|
|
|
|
2011-02-04 16:58:14 +02:00
|
|
|
}
|
|
|
|
|
2021-03-23 16:47:07 +02:00
|
|
|
void CBonusSystemNode::getRedParents(TNodes & out)
|
2011-02-21 06:13:00 +02:00
|
|
|
{
|
|
|
|
FOREACH_PARENT(pname)
|
|
|
|
{
|
|
|
|
if(pname->actsAsBonusSourceOnly())
|
|
|
|
{
|
|
|
|
out.insert(pname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!actsAsBonusSourceOnly())
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(CBonusSystemNode *child : children)
|
2011-02-21 06:13:00 +02:00
|
|
|
{
|
|
|
|
out.insert(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBonusSystemNode::getRedChildren(TNodes &out)
|
|
|
|
{
|
|
|
|
FOREACH_PARENT(pname)
|
|
|
|
{
|
|
|
|
if(!pname->actsAsBonusSourceOnly())
|
|
|
|
{
|
|
|
|
out.insert(pname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(actsAsBonusSourceOnly())
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(CBonusSystemNode *child : children)
|
2011-02-21 06:13:00 +02:00
|
|
|
{
|
|
|
|
out.insert(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-06 01:26:13 +02:00
|
|
|
void CBonusSystemNode::newRedDescendant(CBonusSystemNode & descendant)
|
2011-02-21 06:13:00 +02:00
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const auto & b : exportedBonuses)
|
2021-03-23 16:47:07 +02:00
|
|
|
{
|
2011-02-21 06:13:00 +02:00
|
|
|
if(b->propagator)
|
2022-11-06 01:26:13 +02:00
|
|
|
descendant.propagateBonus(b, *this);
|
2021-03-23 16:47:07 +02:00
|
|
|
}
|
|
|
|
TNodes redParents;
|
|
|
|
getRedAncestors(redParents); //get all red parents recursively
|
2011-02-21 06:13:00 +02:00
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
for(auto * parent : redParents)
|
2021-03-23 16:47:07 +02:00
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const auto & b : parent->exportedBonuses)
|
2021-03-23 16:47:07 +02:00
|
|
|
{
|
|
|
|
if(b->propagator)
|
2022-11-06 01:26:13 +02:00
|
|
|
descendant.propagateBonus(b, *this);
|
2021-03-23 16:47:07 +02:00
|
|
|
}
|
|
|
|
}
|
2011-02-21 06:13:00 +02:00
|
|
|
}
|
|
|
|
|
2022-11-06 01:26:13 +02:00
|
|
|
void CBonusSystemNode::removedRedDescendant(CBonusSystemNode & descendant)
|
2011-02-21 06:13:00 +02:00
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const auto & b : exportedBonuses)
|
2011-02-21 06:13:00 +02:00
|
|
|
if(b->propagator)
|
2022-11-06 01:26:13 +02:00
|
|
|
descendant.unpropagateBonus(b);
|
2011-02-21 06:13:00 +02:00
|
|
|
|
2021-03-23 16:47:07 +02:00
|
|
|
TNodes redParents;
|
|
|
|
getRedAncestors(redParents); //get all red parents recursively
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
for(auto * parent : redParents)
|
2021-03-23 16:47:07 +02:00
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const auto & b : parent->exportedBonuses)
|
2021-08-19 00:45:28 +02:00
|
|
|
if(b->propagator)
|
2022-11-06 01:26:13 +02:00
|
|
|
descendant.unpropagateBonus(b);
|
2021-03-23 16:47:07 +02:00
|
|
|
}
|
2011-02-21 06:13:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CBonusSystemNode::getRedAncestors(TNodes &out)
|
|
|
|
{
|
|
|
|
getRedParents(out);
|
2021-03-23 16:47:07 +02:00
|
|
|
|
|
|
|
TNodes redParents;
|
|
|
|
getRedParents(redParents);
|
2022-09-14 11:00:40 +02:00
|
|
|
|
2021-03-23 16:47:07 +02:00
|
|
|
for(CBonusSystemNode * parent : redParents)
|
|
|
|
parent->getRedAncestors(out);
|
2011-02-21 06:13:00 +02:00
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
void CBonusSystemNode::exportBonus(const std::shared_ptr<Bonus> & b)
|
2011-02-23 05:57:45 +02:00
|
|
|
{
|
|
|
|
if(b->propagator)
|
2021-09-12 13:30:54 +02:00
|
|
|
propagateBonus(b, *this);
|
2011-02-23 05:57:45 +02:00
|
|
|
else
|
|
|
|
bonuses.push_back(b);
|
2011-07-13 21:39:02 +03:00
|
|
|
|
2015-04-11 07:13:28 +02:00
|
|
|
CBonusSystemNode::treeHasChanged();
|
2011-02-23 05:57:45 +02:00
|
|
|
}
|
|
|
|
|
2011-03-01 12:19:05 +02:00
|
|
|
void CBonusSystemNode::exportBonuses()
|
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const auto & b : exportedBonuses)
|
2011-03-01 12:19:05 +02:00
|
|
|
exportBonus(b);
|
|
|
|
}
|
|
|
|
|
2013-02-02 22:28:39 +03:00
|
|
|
CBonusSystemNode::ENodeTypes CBonusSystemNode::getNodeType() const
|
2011-07-13 21:39:02 +03:00
|
|
|
{
|
|
|
|
return nodeType;
|
|
|
|
}
|
|
|
|
|
|
|
|
const BonusList& CBonusSystemNode::getBonusList() const
|
|
|
|
{
|
|
|
|
return bonuses;
|
|
|
|
}
|
|
|
|
|
|
|
|
const TNodesVector& CBonusSystemNode::getParentNodes() const
|
|
|
|
{
|
|
|
|
return parents;
|
|
|
|
}
|
|
|
|
|
|
|
|
const TNodesVector& CBonusSystemNode::getChildrenNodes() const
|
|
|
|
{
|
|
|
|
return children;
|
|
|
|
}
|
|
|
|
|
2013-02-02 22:28:39 +03:00
|
|
|
void CBonusSystemNode::setNodeType(CBonusSystemNode::ENodeTypes type)
|
2011-07-13 21:39:02 +03:00
|
|
|
{
|
|
|
|
nodeType = type;
|
|
|
|
}
|
|
|
|
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
BonusList & CBonusSystemNode::getExportedBonusList()
|
|
|
|
{
|
|
|
|
return exportedBonuses;
|
|
|
|
}
|
|
|
|
|
|
|
|
const BonusList & CBonusSystemNode::getExportedBonusList() const
|
2011-07-13 21:39:02 +03:00
|
|
|
{
|
|
|
|
return exportedBonuses;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& CBonusSystemNode::getDescription() const
|
|
|
|
{
|
|
|
|
return description;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBonusSystemNode::setDescription(const std::string &description)
|
|
|
|
{
|
|
|
|
this->description = description;
|
|
|
|
}
|
|
|
|
|
2012-03-06 19:59:55 +03:00
|
|
|
void CBonusSystemNode::limitBonuses(const BonusList &allBonuses, BonusList &out) const
|
|
|
|
{
|
|
|
|
assert(&allBonuses != &out); //todo should it work in-place?
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
BonusList undecided = allBonuses;
|
|
|
|
BonusList & accepted = out;
|
2012-03-06 19:59:55 +03:00
|
|
|
|
|
|
|
while(true)
|
|
|
|
{
|
2020-10-01 10:38:06 +02:00
|
|
|
int undecidedCount = static_cast<int>(undecided.size());
|
2012-03-06 19:59:55 +03:00
|
|
|
for(int i = 0; i < undecided.size(); i++)
|
|
|
|
{
|
2016-09-19 23:36:35 +02:00
|
|
|
auto b = undecided[i];
|
2018-04-01 13:17:34 +02:00
|
|
|
BonusLimitationContext context = {b, *this, out, undecided};
|
2023-03-29 12:16:13 +02:00
|
|
|
auto decision = b->limiter ? b->limiter->limit(context) : ILimiter::EDecision::ACCEPT; //bonuses without limiters will be accepted by default
|
|
|
|
if(decision == ILimiter::EDecision::DISCARD)
|
2012-03-06 19:59:55 +03:00
|
|
|
{
|
|
|
|
undecided.erase(i);
|
2013-02-13 22:35:43 +03:00
|
|
|
i--; continue;
|
2012-03-06 19:59:55 +03:00
|
|
|
}
|
2023-03-29 12:16:13 +02:00
|
|
|
else if(decision == ILimiter::EDecision::ACCEPT)
|
2012-03-06 19:59:55 +03:00
|
|
|
{
|
|
|
|
accepted.push_back(b);
|
|
|
|
undecided.erase(i);
|
2013-02-13 22:35:43 +03:00
|
|
|
i--; continue;
|
2012-03-06 19:59:55 +03:00
|
|
|
}
|
|
|
|
else
|
2023-03-29 12:16:13 +02:00
|
|
|
assert(decision == ILimiter::EDecision::NOT_SURE);
|
2012-03-06 19:59:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if(undecided.size() == undecidedCount) //we haven't moved a single bonus -> limiters reached a stable state
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TBonusListPtr CBonusSystemNode::limitBonuses(const BonusList &allBonuses) const
|
|
|
|
{
|
2015-12-29 04:43:33 +02:00
|
|
|
auto ret = std::make_shared<BonusList>();
|
2012-03-06 19:59:55 +03:00
|
|
|
limitBonuses(allBonuses, *ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBonusSystemNode::treeHasChanged()
|
|
|
|
{
|
|
|
|
treeChanged++;
|
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
int64_t CBonusSystemNode::getTreeVersion() const
|
|
|
|
{
|
2023-03-29 13:17:09 +02:00
|
|
|
return treeChanged;
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
|
2023-01-24 17:35:30 +02:00
|
|
|
std::string Bonus::Description(boost::optional<si32> customValue) const
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
|
|
|
std::ostringstream str;
|
2016-08-18 14:03:20 +02:00
|
|
|
|
2015-09-04 18:38:42 +02:00
|
|
|
if(description.empty())
|
2018-03-27 09:54:58 +02:00
|
|
|
{
|
|
|
|
if(stacking.empty() || stacking == "ALWAYS")
|
2015-09-04 18:38:42 +02:00
|
|
|
{
|
2018-03-27 09:54:58 +02:00
|
|
|
switch(source)
|
|
|
|
{
|
|
|
|
case ARTIFACT:
|
2023-01-02 15:58:56 +02:00
|
|
|
str << ArtifactID(sid).toArtifact(VLC->artifacts())->getNameTranslated();
|
2018-03-27 09:54:58 +02:00
|
|
|
break;
|
|
|
|
case SPELL_EFFECT:
|
2023-01-02 00:06:42 +02:00
|
|
|
str << SpellID(sid).toSpell(VLC->spells())->getNameTranslated();
|
2018-03-27 09:54:58 +02:00
|
|
|
break;
|
|
|
|
case CREATURE_ABILITY:
|
2023-01-02 18:00:51 +02:00
|
|
|
str << VLC->creh->objects[sid]->getNamePluralTranslated();
|
2018-03-27 09:54:58 +02:00
|
|
|
break;
|
|
|
|
case SECONDARY_SKILL:
|
2023-01-01 22:20:41 +02:00
|
|
|
str << VLC->skillh->getByIndex(sid)->getNameTranslated();
|
2018-03-27 09:54:58 +02:00
|
|
|
break;
|
2018-10-29 22:33:13 +02:00
|
|
|
case HERO_SPECIAL:
|
2023-01-02 13:27:03 +02:00
|
|
|
str << VLC->heroh->objects[sid]->getNameTranslated();
|
2018-10-29 22:33:13 +02:00
|
|
|
break;
|
2018-03-27 09:54:58 +02:00
|
|
|
default:
|
|
|
|
//todo: handle all possible sources
|
|
|
|
str << "Unknown";
|
|
|
|
break;
|
|
|
|
}
|
2015-09-04 18:38:42 +02:00
|
|
|
}
|
2018-03-27 09:54:58 +02:00
|
|
|
else
|
|
|
|
str << stacking;
|
|
|
|
}
|
2015-09-04 18:38:42 +02:00
|
|
|
else
|
2018-03-27 09:54:58 +02:00
|
|
|
{
|
2015-09-04 18:38:42 +02:00
|
|
|
str << description;
|
2018-03-27 09:54:58 +02:00
|
|
|
}
|
2016-08-18 14:03:20 +02:00
|
|
|
|
2023-01-24 17:35:30 +02:00
|
|
|
if(auto value = customValue.value_or(val))
|
|
|
|
str << " " << std::showpos << value;
|
2016-08-18 14:03:20 +02:00
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
return str.str();
|
|
|
|
}
|
|
|
|
|
2017-09-12 08:15:13 +02:00
|
|
|
JsonNode subtypeToJson(Bonus::BonusType type, int subtype)
|
|
|
|
{
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case Bonus::PRIMARY_SKILL:
|
2017-09-13 08:58:14 +02:00
|
|
|
return JsonUtils::stringNode("primSkill." + PrimarySkill::names[subtype]);
|
2017-09-12 10:56:59 +02:00
|
|
|
case Bonus::SPECIAL_SPELL_LEV:
|
|
|
|
case Bonus::SPECIFIC_SPELL_DAMAGE:
|
2023-03-05 03:12:21 +02:00
|
|
|
case Bonus::SPELL:
|
2017-09-12 10:56:59 +02:00
|
|
|
case Bonus::SPECIAL_PECULIAR_ENCHANT:
|
2021-02-09 00:39:52 +02:00
|
|
|
case Bonus::SPECIAL_ADD_VALUE_ENCHANT:
|
|
|
|
case Bonus::SPECIAL_FIXED_VALUE_ENCHANT:
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
return JsonUtils::stringNode(CModHandler::makeFullIdentifier("", "spell", SpellID::encode(subtype)));
|
2018-03-27 09:54:58 +02:00
|
|
|
case Bonus::IMPROVED_NECROMANCY:
|
2017-09-12 10:56:59 +02:00
|
|
|
case Bonus::SPECIAL_UPGRADE:
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
return JsonUtils::stringNode(CModHandler::makeFullIdentifier("", "creature", CreatureID::encode(subtype)));
|
2017-09-12 10:56:59 +02:00
|
|
|
case Bonus::GENERATE_RESOURCE:
|
2017-09-17 00:02:04 +02:00
|
|
|
return JsonUtils::stringNode("resource." + GameConstants::RESOURCE_NAMES[subtype]);
|
2017-09-12 08:15:13 +02:00
|
|
|
default:
|
2017-09-12 10:56:59 +02:00
|
|
|
return JsonUtils::intNode(subtype);
|
2017-09-12 08:15:13 +02:00
|
|
|
}
|
2017-09-12 10:56:59 +02:00
|
|
|
}
|
2017-09-12 08:15:13 +02:00
|
|
|
|
2018-03-12 07:20:18 +02:00
|
|
|
JsonNode additionalInfoToJson(Bonus::BonusType type, CAddInfo addInfo)
|
2017-09-12 10:56:59 +02:00
|
|
|
{
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case Bonus::SPECIAL_UPGRADE:
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
return JsonUtils::stringNode(CModHandler::makeFullIdentifier("", "creature", CreatureID::encode(addInfo[0])));
|
2017-09-12 10:56:59 +02:00
|
|
|
default:
|
2018-03-27 09:54:58 +02:00
|
|
|
return addInfo.toJsonNode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode durationToJson(ui16 duration)
|
|
|
|
{
|
|
|
|
std::vector<std::string> durationNames;
|
|
|
|
for(ui16 durBit = 1; durBit; durBit = durBit << 1)
|
|
|
|
{
|
|
|
|
if(duration & durBit)
|
|
|
|
durationNames.push_back(vstd::findKey(bonusDurationMap, durBit));
|
|
|
|
}
|
|
|
|
if(durationNames.size() == 1)
|
|
|
|
{
|
|
|
|
return JsonUtils::stringNode(durationNames[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
JsonNode node(JsonNode::JsonType::DATA_VECTOR);
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const std::string & dur : durationNames)
|
2018-03-27 09:54:58 +02:00
|
|
|
node.Vector().push_back(JsonUtils::stringNode(dur));
|
|
|
|
return node;
|
2017-09-12 10:56:59 +02:00
|
|
|
}
|
2017-09-12 08:15:13 +02:00
|
|
|
}
|
|
|
|
|
2017-09-11 08:21:24 +02:00
|
|
|
JsonNode Bonus::toJsonNode() const
|
|
|
|
{
|
2017-09-12 10:56:59 +02:00
|
|
|
JsonNode root(JsonNode::JsonType::DATA_STRUCT);
|
2018-03-27 09:54:58 +02:00
|
|
|
// only add values that might reasonably be found in config files
|
2017-09-12 08:15:13 +02:00
|
|
|
root["type"].String() = vstd::findKey(bonusNameMap, type);
|
2017-09-11 08:21:24 +02:00
|
|
|
if(subtype != -1)
|
2017-09-12 08:15:13 +02:00
|
|
|
root["subtype"] = subtypeToJson(type, subtype);
|
2018-03-12 07:20:18 +02:00
|
|
|
if(additionalInfo != CAddInfo::NONE)
|
2017-09-12 10:56:59 +02:00
|
|
|
root["addInfo"] = additionalInfoToJson(type, additionalInfo);
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
if(duration != 0)
|
|
|
|
{
|
|
|
|
JsonNode durationVec(JsonNode::JsonType::DATA_VECTOR);
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const auto & kv : bonusDurationMap)
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
{
|
|
|
|
if(duration & kv.second)
|
|
|
|
durationVec.Vector().push_back(JsonUtils::stringNode(kv.first));
|
|
|
|
}
|
|
|
|
root["duration"] = durationVec;
|
|
|
|
}
|
|
|
|
if(turnsRemain != 0)
|
|
|
|
root["turns"].Integer() = turnsRemain;
|
|
|
|
if(source != OTHER)
|
2023-02-13 10:52:11 +02:00
|
|
|
root["sourceType"].String() = vstd::findKey(bonusSourceMap, source);
|
2023-02-15 21:19:35 +02:00
|
|
|
if(targetSourceType != OTHER)
|
|
|
|
root["targetSourceType"].String() = vstd::findKey(bonusSourceMap, targetSourceType);
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
if(sid != 0)
|
|
|
|
root["sourceID"].Integer() = sid;
|
2017-09-11 08:21:24 +02:00
|
|
|
if(val != 0)
|
2017-09-12 08:15:13 +02:00
|
|
|
root["val"].Integer() = val;
|
2017-09-11 08:21:24 +02:00
|
|
|
if(valType != ADDITIVE_VALUE)
|
2017-09-17 00:02:04 +02:00
|
|
|
root["valueType"].String() = vstd::findKey(bonusValueMap, valType);
|
2022-11-15 02:20:55 +02:00
|
|
|
if(!stacking.empty())
|
2018-03-27 09:54:58 +02:00
|
|
|
root["stacking"].String() = stacking;
|
2022-11-15 02:20:55 +02:00
|
|
|
if(!description.empty())
|
2018-03-27 09:54:58 +02:00
|
|
|
root["description"].String() = description;
|
|
|
|
if(effectRange != NO_LIMIT)
|
|
|
|
root["effectRange"].String() = vstd::findKey(bonusLimitEffect, effectRange);
|
|
|
|
if(duration != PERMANENT)
|
|
|
|
root["duration"] = durationToJson(duration);
|
|
|
|
if(turnsRemain)
|
|
|
|
root["turns"].Integer() = turnsRemain;
|
2017-09-12 01:56:38 +02:00
|
|
|
if(limiter)
|
2018-04-01 13:17:34 +02:00
|
|
|
root["limiters"] = limiter->toJsonNode();
|
2017-09-11 08:21:24 +02:00
|
|
|
if(updater)
|
|
|
|
root["updater"] = updater->toJsonNode();
|
2018-03-27 09:54:58 +02:00
|
|
|
if(propagator)
|
|
|
|
root["propagator"].String() = vstd::findKey(bonusPropagatorMap, propagator);
|
2017-09-11 08:21:24 +02:00
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2017-09-15 00:48:34 +02:00
|
|
|
std::string Bonus::nameForBonus() const
|
|
|
|
{
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case Bonus::PRIMARY_SKILL:
|
|
|
|
return PrimarySkill::names[subtype];
|
|
|
|
case Bonus::SPECIAL_SPELL_LEV:
|
|
|
|
case Bonus::SPECIFIC_SPELL_DAMAGE:
|
2023-03-05 03:12:21 +02:00
|
|
|
case Bonus::SPELL:
|
2017-09-15 00:48:34 +02:00
|
|
|
case Bonus::SPECIAL_PECULIAR_ENCHANT:
|
2021-02-09 00:39:52 +02:00
|
|
|
case Bonus::SPECIAL_ADD_VALUE_ENCHANT:
|
|
|
|
case Bonus::SPECIAL_FIXED_VALUE_ENCHANT:
|
2023-03-27 13:56:39 +02:00
|
|
|
return VLC->spells()->getByIndex(subtype)->getJsonKey();
|
2017-09-15 00:48:34 +02:00
|
|
|
case Bonus::SPECIAL_UPGRADE:
|
2018-03-12 07:20:18 +02:00
|
|
|
return CreatureID::encode(subtype) + "2" + CreatureID::encode(additionalInfo[0]);
|
2017-09-15 00:48:34 +02:00
|
|
|
case Bonus::GENERATE_RESOURCE:
|
|
|
|
return GameConstants::RESOURCE_NAMES[subtype];
|
|
|
|
case Bonus::STACKS_SPEED:
|
|
|
|
return "speed";
|
|
|
|
default:
|
|
|
|
return vstd::findKey(bonusNameMap, type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-05 02:16:05 +02:00
|
|
|
BonusParams::BonusParams(std::string deprecatedTypeStr, std::string deprecatedSubtypeStr, int deprecatedSubtype):
|
|
|
|
isConverted(true)
|
|
|
|
{
|
|
|
|
if(deprecatedTypeStr == "SECONDARY_SKILL_PREMY" || deprecatedTypeStr == "SPECIAL_SECONDARY_SKILL")
|
|
|
|
{
|
|
|
|
if(deprecatedSubtype == SecondarySkill::PATHFINDING || deprecatedSubtypeStr == "skill.pathfinding")
|
|
|
|
type = Bonus::ROUGH_TERRAIN_DISCOUNT;
|
|
|
|
else if(deprecatedSubtype == SecondarySkill::DIPLOMACY || deprecatedSubtypeStr == "skill.diplomacy")
|
|
|
|
type = Bonus::WANDERING_CREATURES_JOIN_BONUS;
|
|
|
|
else if(deprecatedSubtype == SecondarySkill::WISDOM || deprecatedSubtypeStr == "skill.wisdom")
|
|
|
|
type = Bonus::MAX_LEARNABLE_SPELL_LEVEL;
|
|
|
|
else if(deprecatedSubtype == SecondarySkill::MYSTICISM || deprecatedSubtypeStr == "skill.mysticism")
|
|
|
|
type = Bonus::MANA_REGENERATION;
|
|
|
|
else if(deprecatedSubtype == SecondarySkill::NECROMANCY || deprecatedSubtypeStr == "skill.necromancy")
|
|
|
|
type = Bonus::UNDEAD_RAISE_PERCENTAGE;
|
|
|
|
else if(deprecatedSubtype == SecondarySkill::LEARNING || deprecatedSubtypeStr == "skill.learning")
|
|
|
|
type = Bonus::HERO_EXPERIENCE_GAIN_PERCENT;
|
|
|
|
else if(deprecatedSubtype == SecondarySkill::RESISTANCE || deprecatedSubtypeStr == "skill.resistance")
|
|
|
|
type = Bonus::MAGIC_RESISTANCE;
|
|
|
|
else if(deprecatedSubtype == SecondarySkill::EAGLE_EYE || deprecatedSubtypeStr == "skill.eagleEye")
|
|
|
|
type = Bonus::LEARN_BATTLE_SPELL_CHANCE;
|
2023-03-17 17:50:57 +02:00
|
|
|
else if(deprecatedSubtype == SecondarySkill::SCOUTING || deprecatedSubtypeStr == "skill.scouting")
|
|
|
|
type = Bonus::SIGHT_RADIUS;
|
2023-03-05 02:16:05 +02:00
|
|
|
else if(deprecatedSubtype == SecondarySkill::INTELLIGENCE || deprecatedSubtypeStr == "skill.intelligence")
|
|
|
|
{
|
|
|
|
type = Bonus::MANA_PER_KNOWLEDGE;
|
|
|
|
valueType = Bonus::PERCENT_TO_BASE;
|
|
|
|
valueTypeRelevant = true;
|
|
|
|
}
|
|
|
|
else if(deprecatedSubtype == SecondarySkill::SORCERY || deprecatedSubtypeStr == "skill.sorcery")
|
|
|
|
type = Bonus::SPELL_DAMAGE;
|
|
|
|
else if(deprecatedSubtype == SecondarySkill::SCHOLAR || deprecatedSubtypeStr == "skill.scholar")
|
|
|
|
type = Bonus::LEARN_MEETING_SPELL_LIMIT;
|
|
|
|
else if(deprecatedSubtype == SecondarySkill::ARCHERY|| deprecatedSubtypeStr == "skill.archery")
|
|
|
|
{
|
|
|
|
subtype = 1;
|
|
|
|
subtypeRelevant = true;
|
|
|
|
type = Bonus::PERCENTAGE_DAMAGE_BOOST;
|
|
|
|
}
|
|
|
|
else if(deprecatedSubtype == SecondarySkill::OFFENCE || deprecatedSubtypeStr == "skill.offence")
|
|
|
|
{
|
|
|
|
subtype = 0;
|
|
|
|
subtypeRelevant = true;
|
|
|
|
type = Bonus::PERCENTAGE_DAMAGE_BOOST;
|
|
|
|
}
|
|
|
|
else if(deprecatedSubtype == SecondarySkill::ARMORER || deprecatedSubtypeStr == "skill.armorer")
|
|
|
|
{
|
|
|
|
subtype = -1;
|
|
|
|
subtypeRelevant = true;
|
|
|
|
type = Bonus::GENERAL_DAMAGE_REDUCTION;
|
|
|
|
}
|
|
|
|
else if(deprecatedSubtype == SecondarySkill::NAVIGATION || deprecatedSubtypeStr == "skill.navigation")
|
|
|
|
{
|
|
|
|
subtype = 0;
|
|
|
|
subtypeRelevant = true;
|
|
|
|
valueType = Bonus::PERCENT_TO_BASE;
|
|
|
|
valueTypeRelevant = true;
|
|
|
|
type = Bonus::MOVEMENT;
|
|
|
|
}
|
|
|
|
else if(deprecatedSubtype == SecondarySkill::LOGISTICS || deprecatedSubtypeStr == "skill.logistics")
|
|
|
|
{
|
|
|
|
subtype = 0;
|
|
|
|
subtypeRelevant = true;
|
|
|
|
valueType = Bonus::PERCENT_TO_BASE;
|
|
|
|
valueTypeRelevant = true;
|
|
|
|
type = Bonus::MOVEMENT;
|
|
|
|
}
|
|
|
|
else if(deprecatedSubtype == SecondarySkill::ESTATES || deprecatedSubtypeStr == "skill.estates")
|
|
|
|
{
|
|
|
|
type = Bonus::GENERATE_RESOURCE;
|
2023-04-05 02:26:29 +02:00
|
|
|
subtype = GameResID(EGameResID::GOLD);
|
2023-03-05 02:16:05 +02:00
|
|
|
subtypeRelevant = true;
|
|
|
|
}
|
|
|
|
else if(deprecatedSubtype == SecondarySkill::AIR_MAGIC || deprecatedSubtypeStr == "skill.airMagic")
|
|
|
|
{
|
|
|
|
type = Bonus::MAGIC_SCHOOL_SKILL;
|
|
|
|
subtypeRelevant = true;
|
|
|
|
subtype = 4;
|
|
|
|
}
|
|
|
|
else if(deprecatedSubtype == SecondarySkill::WATER_MAGIC || deprecatedSubtypeStr == "skill.waterMagic")
|
|
|
|
{
|
|
|
|
type = Bonus::MAGIC_SCHOOL_SKILL;
|
|
|
|
subtypeRelevant = true;
|
|
|
|
subtype = 1;
|
|
|
|
}
|
|
|
|
else if(deprecatedSubtype == SecondarySkill::FIRE_MAGIC || deprecatedSubtypeStr == "skill.fireMagic")
|
|
|
|
{
|
|
|
|
type = Bonus::MAGIC_SCHOOL_SKILL;
|
|
|
|
subtypeRelevant = true;
|
|
|
|
subtype = 2;
|
|
|
|
}
|
|
|
|
else if(deprecatedSubtype == SecondarySkill::EARTH_MAGIC || deprecatedSubtypeStr == "skill.earthMagic")
|
|
|
|
{
|
|
|
|
type = Bonus::MAGIC_SCHOOL_SKILL;
|
|
|
|
subtypeRelevant = true;
|
|
|
|
subtype = 8;
|
|
|
|
}
|
|
|
|
else if (deprecatedSubtype == SecondarySkill::ARTILLERY || deprecatedSubtypeStr == "skill.artillery")
|
|
|
|
{
|
|
|
|
type = Bonus::BONUS_DAMAGE_CHANCE;
|
|
|
|
subtypeRelevant = true;
|
|
|
|
subtypeStr = "core:creature.ballista";
|
|
|
|
}
|
|
|
|
else if (deprecatedSubtype == SecondarySkill::FIRST_AID || deprecatedSubtypeStr == "skill.firstAid")
|
|
|
|
{
|
|
|
|
type = Bonus::SPECIFIC_SPELL_POWER;
|
|
|
|
subtypeRelevant = true;
|
|
|
|
subtypeStr = "core:spell.firstAid";
|
|
|
|
}
|
|
|
|
else if (deprecatedSubtype == SecondarySkill::BALLISTICS || deprecatedSubtypeStr == "skill.ballistics")
|
|
|
|
{
|
|
|
|
type = Bonus::CATAPULT_EXTRA_SHOTS;
|
|
|
|
subtypeRelevant = true;
|
|
|
|
subtypeStr = "core:spell.catapultShot";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
isConverted = false;
|
|
|
|
}
|
|
|
|
else if (deprecatedTypeStr == "SECONDARY_SKILL_VAL2")
|
|
|
|
{
|
|
|
|
if(deprecatedSubtype == SecondarySkill::EAGLE_EYE || deprecatedSubtypeStr == "skill.eagleEye")
|
|
|
|
type = Bonus::LEARN_BATTLE_SPELL_LEVEL_LIMIT;
|
|
|
|
else if (deprecatedSubtype == SecondarySkill::ARTILLERY || deprecatedSubtypeStr == "skill.artillery")
|
|
|
|
{
|
|
|
|
type = Bonus::HERO_GRANTS_ATTACKS;
|
|
|
|
subtypeRelevant = true;
|
|
|
|
subtypeStr = "core:creature.ballista";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
isConverted = false;
|
|
|
|
}
|
|
|
|
else if (deprecatedTypeStr == "SEA_MOVEMENT")
|
|
|
|
{
|
|
|
|
subtype = 0;
|
|
|
|
subtypeRelevant = true;
|
|
|
|
valueType = Bonus::ADDITIVE_VALUE;
|
|
|
|
valueTypeRelevant = true;
|
|
|
|
type = Bonus::MOVEMENT;
|
|
|
|
}
|
|
|
|
else if (deprecatedTypeStr == "LAND_MOVEMENT")
|
|
|
|
{
|
|
|
|
subtype = 1;
|
|
|
|
subtypeRelevant = true;
|
|
|
|
valueType = Bonus::ADDITIVE_VALUE;
|
|
|
|
valueTypeRelevant = true;
|
|
|
|
type = Bonus::MOVEMENT;
|
|
|
|
}
|
|
|
|
else if (deprecatedTypeStr == "MAXED_SPELL")
|
|
|
|
{
|
|
|
|
type = Bonus::SPELL;
|
|
|
|
subtypeStr = deprecatedSubtypeStr;
|
|
|
|
subtypeRelevant = true;
|
|
|
|
valueType = Bonus::INDEPENDENT_MAX;
|
|
|
|
valueTypeRelevant = true;
|
|
|
|
val = 3;
|
|
|
|
valRelevant = true;
|
|
|
|
}
|
|
|
|
else if (deprecatedTypeStr == "FULL_HP_REGENERATION")
|
|
|
|
{
|
|
|
|
type = Bonus::HP_REGENERATION;
|
|
|
|
val = 100000; //very high value to always chose stack health
|
|
|
|
valRelevant = true;
|
|
|
|
}
|
|
|
|
else if (deprecatedTypeStr == "KING1")
|
|
|
|
{
|
|
|
|
type = Bonus::KING;
|
|
|
|
val = 0;
|
|
|
|
valRelevant = true;
|
|
|
|
}
|
|
|
|
else if (deprecatedTypeStr == "KING2")
|
|
|
|
{
|
|
|
|
type = Bonus::KING;
|
|
|
|
val = 2;
|
|
|
|
valRelevant = true;
|
|
|
|
}
|
|
|
|
else if (deprecatedTypeStr == "KING3")
|
|
|
|
{
|
|
|
|
type = Bonus::KING;
|
|
|
|
val = 3;
|
|
|
|
valRelevant = true;
|
|
|
|
}
|
|
|
|
else if (deprecatedTypeStr == "SIGHT_RADIOUS")
|
|
|
|
type = Bonus::SIGHT_RADIUS;
|
2023-03-09 11:25:03 +02:00
|
|
|
else if (deprecatedTypeStr == "SELF_MORALE")
|
|
|
|
{
|
|
|
|
type = Bonus::MORALE;
|
|
|
|
val = 1;
|
|
|
|
valRelevant = true;
|
|
|
|
valueType = Bonus::INDEPENDENT_MAX;
|
|
|
|
valueTypeRelevant = true;
|
|
|
|
}
|
2023-03-09 11:31:04 +02:00
|
|
|
else if (deprecatedTypeStr == "SELF_LUCK")
|
|
|
|
{
|
|
|
|
type = Bonus::LUCK;
|
|
|
|
val = 1;
|
|
|
|
valRelevant = true;
|
|
|
|
valueType = Bonus::INDEPENDENT_MAX;
|
|
|
|
valueTypeRelevant = true;
|
|
|
|
}
|
2023-03-05 02:16:05 +02:00
|
|
|
else
|
|
|
|
isConverted = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const JsonNode & BonusParams::toJson()
|
|
|
|
{
|
|
|
|
assert(isConverted);
|
|
|
|
if(ret.isNull())
|
|
|
|
{
|
|
|
|
ret["type"].String() = vstd::findKey(bonusNameMap, type);
|
|
|
|
if(subtypeRelevant && !subtypeStr.empty())
|
|
|
|
ret["subtype"].String() = subtypeStr;
|
|
|
|
else if(subtypeRelevant)
|
|
|
|
ret["subtype"].Integer() = subtype;
|
|
|
|
if(valueTypeRelevant)
|
|
|
|
ret["valueType"].String() = vstd::findKey(bonusValueMap, valueType);
|
|
|
|
if(valRelevant)
|
|
|
|
ret["val"].Float() = val;
|
|
|
|
if(targetTypeRelevant)
|
|
|
|
ret["targetSourceType"].String() = vstd::findKey(bonusSourceMap, targetType);
|
|
|
|
jsonCreated = true;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
2023-03-09 01:16:18 +02:00
|
|
|
CSelector BonusParams::toSelector()
|
|
|
|
{
|
|
|
|
assert(isConverted);
|
|
|
|
if(subtypeRelevant && !subtypeStr.empty())
|
|
|
|
JsonUtils::resolveIdentifier(subtype, toJson(), "subtype");
|
|
|
|
|
|
|
|
auto ret = Selector::type()(type);
|
|
|
|
if(subtypeRelevant)
|
|
|
|
ret = ret.And(Selector::subtype()(subtype));
|
|
|
|
if(valueTypeRelevant)
|
|
|
|
ret = ret.And(Selector::valueType(valueType));
|
|
|
|
if(targetTypeRelevant)
|
|
|
|
ret = ret.And(Selector::targetSourceType()(targetType));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
Bonus::Bonus(Bonus::BonusDuration Duration, BonusType Type, BonusSource Src, si32 Val, ui32 ID, std::string Desc, si32 Subtype):
|
|
|
|
duration(static_cast<ui16>(Duration)),
|
|
|
|
type(Type),
|
|
|
|
subtype(Subtype),
|
|
|
|
source(Src),
|
|
|
|
val(Val),
|
|
|
|
sid(ID),
|
|
|
|
description(std::move(Desc))
|
2010-07-13 08:25:40 +03:00
|
|
|
{
|
|
|
|
boost::algorithm::trim(description);
|
2023-02-15 21:19:35 +02:00
|
|
|
targetSourceType = OTHER;
|
2010-07-13 08:25:40 +03:00
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
Bonus::Bonus(Bonus::BonusDuration Duration, BonusType Type, BonusSource Src, si32 Val, ui32 ID, si32 Subtype, ValueType ValType):
|
|
|
|
duration(static_cast<ui16>(Duration)),
|
|
|
|
type(Type),
|
|
|
|
subtype(Subtype),
|
|
|
|
source(Src),
|
|
|
|
val(Val),
|
|
|
|
sid(ID),
|
|
|
|
valType(ValType)
|
2010-07-13 08:25:40 +03:00
|
|
|
{
|
|
|
|
turnsRemain = 0;
|
|
|
|
effectRange = NO_LIMIT;
|
2023-02-15 21:19:35 +02:00
|
|
|
targetSourceType = OTHER;
|
2010-07-13 08:25:40 +03:00
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
std::shared_ptr<Bonus> Bonus::addPropagator(const TPropagatorPtr & Propagator)
|
2011-02-21 06:13:00 +02:00
|
|
|
{
|
|
|
|
propagator = Propagator;
|
2016-09-19 23:36:35 +02:00
|
|
|
return this->shared_from_this();
|
2010-07-13 08:25:40 +03:00
|
|
|
}
|
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
namespace Selector
|
|
|
|
{
|
2020-11-11 21:43:40 +02:00
|
|
|
DLL_LINKAGE CSelectFieldEqual<Bonus::BonusType> & type()
|
|
|
|
{
|
|
|
|
static CSelectFieldEqual<Bonus::BonusType> stype(&Bonus::type);
|
|
|
|
return stype;
|
|
|
|
}
|
|
|
|
|
|
|
|
DLL_LINKAGE CSelectFieldEqual<TBonusSubtype> & subtype()
|
|
|
|
{
|
|
|
|
static CSelectFieldEqual<TBonusSubtype> ssubtype(&Bonus::subtype);
|
|
|
|
return ssubtype;
|
|
|
|
}
|
|
|
|
|
|
|
|
DLL_LINKAGE CSelectFieldEqual<CAddInfo> & info()
|
|
|
|
{
|
|
|
|
static CSelectFieldEqual<CAddInfo> sinfo(&Bonus::additionalInfo);
|
|
|
|
return sinfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
DLL_LINKAGE CSelectFieldEqual<Bonus::BonusSource> & sourceType()
|
|
|
|
{
|
|
|
|
static CSelectFieldEqual<Bonus::BonusSource> ssourceType(&Bonus::source);
|
|
|
|
return ssourceType;
|
|
|
|
}
|
|
|
|
|
2023-02-15 21:19:35 +02:00
|
|
|
DLL_LINKAGE CSelectFieldEqual<Bonus::BonusSource> & targetSourceType()
|
|
|
|
{
|
|
|
|
static CSelectFieldEqual<Bonus::BonusSource> ssourceType(&Bonus::targetSourceType);
|
|
|
|
return ssourceType;
|
|
|
|
}
|
|
|
|
|
2020-11-11 21:43:40 +02:00
|
|
|
DLL_LINKAGE CSelectFieldEqual<Bonus::LimitEffect> & effectRange()
|
|
|
|
{
|
|
|
|
static CSelectFieldEqual<Bonus::LimitEffect> seffectRange(&Bonus::effectRange);
|
|
|
|
return seffectRange;
|
|
|
|
}
|
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
DLL_LINKAGE CWillLastTurns turns;
|
2015-11-09 14:01:58 +02:00
|
|
|
DLL_LINKAGE CWillLastDays days;
|
2010-05-02 21:20:26 +03:00
|
|
|
|
2013-02-16 17:03:47 +03:00
|
|
|
CSelector DLL_LINKAGE typeSubtype(Bonus::BonusType Type, TBonusSubtype Subtype)
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2020-11-11 21:43:40 +02:00
|
|
|
return type()(Type).And(subtype()(Subtype));
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
CSelector DLL_LINKAGE typeSubtypeInfo(Bonus::BonusType type, TBonusSubtype subtype, const CAddInfo & info)
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2013-07-02 15:08:30 +03:00
|
|
|
return CSelectFieldEqual<Bonus::BonusType>(&Bonus::type)(type)
|
|
|
|
.And(CSelectFieldEqual<TBonusSubtype>(&Bonus::subtype)(subtype))
|
2018-03-12 07:20:18 +02:00
|
|
|
.And(CSelectFieldEqual<CAddInfo>(&Bonus::additionalInfo)(info));
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
|
|
|
|
2013-02-02 01:04:25 +03:00
|
|
|
CSelector DLL_LINKAGE source(Bonus::BonusSource source, ui32 sourceID)
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2013-07-02 15:08:30 +03:00
|
|
|
return CSelectFieldEqual<Bonus::BonusSource>(&Bonus::source)(source)
|
|
|
|
.And(CSelectFieldEqual<ui32>(&Bonus::sid)(sourceID));
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
|
|
|
|
2013-02-02 01:04:25 +03:00
|
|
|
CSelector DLL_LINKAGE sourceTypeSel(Bonus::BonusSource source)
|
2010-08-31 13:22:21 +03:00
|
|
|
{
|
2013-07-02 15:08:30 +03:00
|
|
|
return CSelectFieldEqual<Bonus::BonusSource>(&Bonus::source)(source);
|
2010-08-31 13:22:21 +03:00
|
|
|
}
|
|
|
|
|
2017-08-27 05:35:04 +02:00
|
|
|
CSelector DLL_LINKAGE valueType(Bonus::ValueType valType)
|
|
|
|
{
|
|
|
|
return CSelectFieldEqual<Bonus::ValueType>(&Bonus::valType)(valType);
|
|
|
|
}
|
|
|
|
|
2016-09-30 16:11:17 +02:00
|
|
|
DLL_LINKAGE CSelector all([](const Bonus * b){return true;});
|
|
|
|
DLL_LINKAGE CSelector none([](const Bonus * b){return false;});
|
2011-02-21 06:13:00 +02:00
|
|
|
}
|
|
|
|
|
2010-11-20 02:03:31 +02:00
|
|
|
const CCreature * retrieveCreature(const CBonusSystemNode *node)
|
|
|
|
{
|
2011-07-13 21:39:02 +03:00
|
|
|
switch(node->getNodeType())
|
2010-11-20 02:03:31 +02:00
|
|
|
{
|
|
|
|
case CBonusSystemNode::CREATURE:
|
2023-03-13 23:26:44 +02:00
|
|
|
return (dynamic_cast<const CCreature *>(node));
|
2017-09-06 14:27:26 +02:00
|
|
|
case CBonusSystemNode::STACK_BATTLE:
|
2023-03-13 23:26:44 +02:00
|
|
|
return (dynamic_cast<const CStack *>(node))->type;
|
2010-11-20 02:03:31 +02:00
|
|
|
default:
|
2018-02-10 20:52:23 +02:00
|
|
|
const CStackInstance * csi = retrieveStackInstance(node);
|
2011-02-12 18:12:48 +02:00
|
|
|
if(csi)
|
|
|
|
return csi->type;
|
2013-06-26 14:18:27 +03:00
|
|
|
return nullptr;
|
2010-11-20 02:03:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const BonusList &bonusList)
|
2010-07-12 13:20:25 +03:00
|
|
|
{
|
2011-12-14 00:23:17 +03:00
|
|
|
for (ui32 i = 0; i < bonusList.size(); i++)
|
2010-07-12 13:20:25 +03:00
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
const auto & b = bonusList[i];
|
2011-06-25 16:53:15 +03:00
|
|
|
out << "Bonus " << i << "\n" << *b << std::endl;
|
2010-07-12 13:20:25 +03:00
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const Bonus &bonus)
|
2010-07-12 13:20:25 +03:00
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const auto & i : bonusNameMap)
|
|
|
|
if(i.second == bonus.type)
|
|
|
|
out << "\tType: " << i.first << " \t";
|
2010-07-12 13:20:25 +03:00
|
|
|
|
|
|
|
#define printField(field) out << "\t" #field ": " << (int)bonus.field << "\n"
|
|
|
|
printField(val);
|
|
|
|
printField(subtype);
|
|
|
|
printField(duration);
|
|
|
|
printField(source);
|
2011-02-20 20:32:39 +02:00
|
|
|
printField(sid);
|
2018-03-12 07:20:18 +02:00
|
|
|
if(bonus.additionalInfo != CAddInfo::NONE)
|
|
|
|
out << "\taddInfo: " << bonus.additionalInfo.toString() << "\n";
|
2010-07-12 13:20:25 +03:00
|
|
|
printField(turnsRemain);
|
|
|
|
printField(valType);
|
2018-03-27 09:54:58 +02:00
|
|
|
if(!bonus.stacking.empty())
|
|
|
|
out << "\tstacking: \"" << bonus.stacking << "\"\n";
|
2010-07-12 13:20:25 +03:00
|
|
|
printField(effectRange);
|
|
|
|
#undef printField
|
|
|
|
|
2017-09-12 01:56:38 +02:00
|
|
|
if(bonus.limiter)
|
|
|
|
out << "\tLimiter: " << bonus.limiter->toString() << "\n";
|
2017-09-11 02:36:02 +02:00
|
|
|
if(bonus.updater)
|
|
|
|
out << "\tUpdater: " << bonus.updater->toString() << "\n";
|
|
|
|
|
2010-07-12 13:20:25 +03:00
|
|
|
return out;
|
|
|
|
}
|
2013-01-20 15:06:18 +03:00
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
std::shared_ptr<Bonus> Bonus::addLimiter(const TLimiterPtr & Limiter)
|
2013-01-16 16:02:01 +03:00
|
|
|
{
|
2013-01-20 15:06:18 +03:00
|
|
|
if (limiter)
|
2013-01-16 16:02:01 +03:00
|
|
|
{
|
2014-03-23 15:59:03 +03:00
|
|
|
//If we already have limiter list, retrieve it
|
2018-04-01 13:17:34 +02:00
|
|
|
auto limiterList = std::dynamic_pointer_cast<AllOfLimiter>(limiter);
|
2013-01-20 15:06:18 +03:00
|
|
|
if(!limiterList)
|
|
|
|
{
|
|
|
|
//Create a new limiter list with old limiter and the new one will be pushed later
|
2018-04-01 13:17:34 +02:00
|
|
|
limiterList = std::make_shared<AllOfLimiter>();
|
2013-01-20 15:06:18 +03:00
|
|
|
limiterList->add(limiter);
|
|
|
|
limiter = limiterList;
|
|
|
|
}
|
|
|
|
|
|
|
|
limiterList->add(Limiter);
|
2013-01-16 16:02:01 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-01-19 20:38:37 +03:00
|
|
|
limiter = Limiter;
|
2013-01-16 16:02:01 +03:00
|
|
|
}
|
2016-09-19 23:36:35 +02:00
|
|
|
return this->shared_from_this();
|
2012-12-13 13:49:12 +03:00
|
|
|
}
|
|
|
|
|
2023-03-29 12:16:13 +02:00
|
|
|
ILimiter::EDecision ILimiter::limit(const BonusLimitationContext &context) const /*return true to drop the bonus */
|
2010-07-12 13:20:25 +03:00
|
|
|
{
|
2023-03-29 12:16:13 +02:00
|
|
|
return ILimiter::EDecision::ACCEPT;
|
2010-07-12 13:20:25 +03:00
|
|
|
}
|
|
|
|
|
2017-09-12 01:56:38 +02:00
|
|
|
std::string ILimiter::toString() const
|
|
|
|
{
|
|
|
|
return typeid(*this).name();
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode ILimiter::toJsonNode() const
|
|
|
|
{
|
2017-09-12 10:56:59 +02:00
|
|
|
JsonNode root(JsonNode::JsonType::DATA_STRUCT);
|
2017-09-12 01:56:38 +02:00
|
|
|
root["type"].String() = toString();
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2023-03-29 12:16:13 +02:00
|
|
|
ILimiter::EDecision CCreatureTypeLimiter::limit(const BonusLimitationContext &context) const
|
2010-07-12 13:20:25 +03:00
|
|
|
{
|
2023-01-14 19:05:36 +02:00
|
|
|
const CCreature *c = retrieveCreature(&context.node);
|
|
|
|
if(!c)
|
2023-03-29 12:16:13 +02:00
|
|
|
return ILimiter::EDecision::DISCARD;
|
|
|
|
|
|
|
|
auto accept = c->getId() == creature->getId() || (includeUpgrades && creature->isMyUpgrade(c));
|
|
|
|
return accept ? ILimiter::EDecision::ACCEPT : ILimiter::EDecision::DISCARD;
|
2023-01-14 19:05:36 +02:00
|
|
|
//drop bonus if it's not our creature and (we don`t check upgrades or its not our upgrade)
|
|
|
|
}
|
|
|
|
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
CCreatureTypeLimiter::CCreatureTypeLimiter(const CCreature & creature_, bool IncludeUpgrades)
|
|
|
|
: creature(&creature_), includeUpgrades(IncludeUpgrades)
|
2010-07-12 13:20:25 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
void CCreatureTypeLimiter::setCreature(const CreatureID & id)
|
2013-01-18 14:49:15 +03:00
|
|
|
{
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
creature = VLC->creh->objects[id];
|
2013-01-18 14:49:15 +03:00
|
|
|
}
|
|
|
|
|
2017-09-12 01:56:38 +02:00
|
|
|
std::string CCreatureTypeLimiter::toString() const
|
|
|
|
{
|
2018-04-17 14:59:30 +02:00
|
|
|
boost::format fmt("CCreatureTypeLimiter(creature=%s, includeUpgrades=%s)");
|
2023-01-02 18:00:51 +02:00
|
|
|
fmt % creature->getJsonKey() % (includeUpgrades ? "true" : "false");
|
2018-04-17 14:59:30 +02:00
|
|
|
return fmt.str();
|
2017-09-12 01:56:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode CCreatureTypeLimiter::toJsonNode() const
|
|
|
|
{
|
2017-09-12 10:56:59 +02:00
|
|
|
JsonNode root(JsonNode::JsonType::DATA_STRUCT);
|
2017-09-12 01:56:38 +02:00
|
|
|
|
|
|
|
root["type"].String() = "CREATURE_TYPE_LIMITER";
|
2023-01-02 18:00:51 +02:00
|
|
|
root["parameters"].Vector().push_back(JsonUtils::stringNode(creature->getJsonKey()));
|
2017-09-12 01:56:38 +02:00
|
|
|
root["parameters"].Vector().push_back(JsonUtils::boolNode(includeUpgrades));
|
|
|
|
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2013-02-16 17:03:47 +03:00
|
|
|
HasAnotherBonusLimiter::HasAnotherBonusLimiter( Bonus::BonusType bonus )
|
2023-03-01 23:54:10 +02:00
|
|
|
: type(bonus), subtype(0), isSubtypeRelevant(false), isSourceRelevant(false), isSourceIDRelevant(false)
|
2010-08-06 13:46:40 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-02-16 17:03:47 +03:00
|
|
|
HasAnotherBonusLimiter::HasAnotherBonusLimiter( Bonus::BonusType bonus, TBonusSubtype _subtype )
|
2023-03-01 23:54:10 +02:00
|
|
|
: type(bonus), subtype(_subtype), isSubtypeRelevant(true), isSourceRelevant(false), isSourceIDRelevant(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
HasAnotherBonusLimiter::HasAnotherBonusLimiter(Bonus::BonusType bonus, Bonus::BonusSource src)
|
|
|
|
: type(bonus), source(src), isSubtypeRelevant(false), isSourceRelevant(true), isSourceIDRelevant(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
HasAnotherBonusLimiter::HasAnotherBonusLimiter(Bonus::BonusType bonus, TBonusSubtype _subtype, Bonus::BonusSource src)
|
|
|
|
: type(bonus), subtype(_subtype), isSubtypeRelevant(true), source(src), isSourceRelevant(true), isSourceIDRelevant(false)
|
2010-08-06 13:46:40 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-03-29 12:16:13 +02:00
|
|
|
ILimiter::EDecision HasAnotherBonusLimiter::limit(const BonusLimitationContext &context) const
|
2010-08-06 13:46:40 +03:00
|
|
|
{
|
2023-03-01 23:54:10 +02:00
|
|
|
//TODO: proper selector config with parsing of JSON
|
|
|
|
auto mySelector = Selector::type()(type);
|
|
|
|
|
|
|
|
if(isSubtypeRelevant)
|
|
|
|
mySelector = mySelector.And(Selector::subtype()(subtype));
|
|
|
|
if(isSourceRelevant && isSourceIDRelevant)
|
|
|
|
mySelector = mySelector.And(Selector::source(source, sid));
|
|
|
|
else if (isSourceRelevant)
|
|
|
|
mySelector = mySelector.And(Selector::sourceTypeSel(source));
|
2012-03-06 19:59:55 +03:00
|
|
|
|
|
|
|
//if we have a bonus of required type accepted, limiter should accept also this bonus
|
|
|
|
if(context.alreadyAccepted.getFirst(mySelector))
|
2023-03-29 12:16:13 +02:00
|
|
|
return ILimiter::EDecision::ACCEPT;
|
2012-03-06 19:59:55 +03:00
|
|
|
|
2018-04-01 13:17:34 +02:00
|
|
|
//if there are no matching bonuses pending, we can (and must) reject right away
|
|
|
|
if(!context.stillUndecided.getFirst(mySelector))
|
2023-03-29 12:16:13 +02:00
|
|
|
return ILimiter::EDecision::DISCARD;
|
2018-04-01 13:17:34 +02:00
|
|
|
|
2012-03-06 19:59:55 +03:00
|
|
|
//do not accept for now but it may change if more bonuses gets included
|
2023-03-29 12:16:13 +02:00
|
|
|
return ILimiter::EDecision::NOT_SURE;
|
2010-08-06 13:46:40 +03:00
|
|
|
}
|
2010-11-19 00:22:51 +02:00
|
|
|
|
2017-09-17 04:45:23 +02:00
|
|
|
std::string HasAnotherBonusLimiter::toString() const
|
|
|
|
{
|
|
|
|
std::string typeName = vstd::findKey(bonusNameMap, type);
|
|
|
|
if(isSubtypeRelevant)
|
2018-04-17 14:59:30 +02:00
|
|
|
{
|
|
|
|
boost::format fmt("HasAnotherBonusLimiter(type=%s, subtype=%d)");
|
|
|
|
fmt % typeName % subtype;
|
|
|
|
return fmt.str();
|
|
|
|
}
|
2017-09-17 04:45:23 +02:00
|
|
|
else
|
2018-04-17 14:59:30 +02:00
|
|
|
{
|
|
|
|
boost::format fmt("HasAnotherBonusLimiter(type=%s)");
|
|
|
|
fmt % typeName;
|
|
|
|
return fmt.str();
|
|
|
|
}
|
2017-09-17 04:45:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode HasAnotherBonusLimiter::toJsonNode() const
|
|
|
|
{
|
|
|
|
JsonNode root(JsonNode::JsonType::DATA_STRUCT);
|
|
|
|
std::string typeName = vstd::findKey(bonusNameMap, type);
|
2023-03-01 23:54:10 +02:00
|
|
|
auto sourceTypeName = vstd::findKey(bonusSourceMap, source);
|
2017-09-17 04:45:23 +02:00
|
|
|
|
|
|
|
root["type"].String() = "HAS_ANOTHER_BONUS_LIMITER";
|
|
|
|
root["parameters"].Vector().push_back(JsonUtils::stringNode(typeName));
|
|
|
|
if(isSubtypeRelevant)
|
|
|
|
root["parameters"].Vector().push_back(JsonUtils::intNode(subtype));
|
2023-03-01 23:54:10 +02:00
|
|
|
if(isSourceRelevant)
|
|
|
|
root["parameters"].Vector().push_back(JsonUtils::stringNode(sourceTypeName));
|
2017-09-17 04:45:23 +02:00
|
|
|
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2023-03-28 01:14:18 +02:00
|
|
|
ILimiter::EDecision UnitOnHexLimiter::limit(const BonusLimitationContext &context) const
|
|
|
|
{
|
|
|
|
const auto * stack = retrieveStackBattle(&context.node);
|
|
|
|
if(!stack)
|
|
|
|
return ILimiter::EDecision::DISCARD;
|
|
|
|
|
|
|
|
auto accept = false;
|
|
|
|
for (const auto & hex : stack->getHexes())
|
|
|
|
accept |= !!applicableHexes.count(hex);
|
|
|
|
|
|
|
|
return accept ? ILimiter::EDecision::ACCEPT : ILimiter::EDecision::DISCARD;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnitOnHexLimiter::UnitOnHexLimiter(const std::set<BattleHex> & applicableHexes):
|
|
|
|
applicableHexes(applicableHexes)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode UnitOnHexLimiter::toJsonNode() const
|
|
|
|
{
|
|
|
|
JsonNode root(JsonNode::JsonType::DATA_STRUCT);
|
|
|
|
|
|
|
|
root["type"].String() = "UNIT_ON_HEXES";
|
|
|
|
for(const auto & hex : applicableHexes)
|
|
|
|
root["parameters"].Vector().push_back(JsonUtils::intNode(hex));
|
|
|
|
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2011-02-21 06:13:00 +02:00
|
|
|
bool IPropagator::shouldBeAttached(CBonusSystemNode *dest)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-14 00:02:13 +02:00
|
|
|
CBonusSystemNode::ENodeTypes IPropagator::getPropagatorType() const
|
|
|
|
{
|
|
|
|
return CBonusSystemNode::ENodeTypes::NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
CPropagatorNodeType::CPropagatorNodeType(CBonusSystemNode::ENodeTypes NodeType)
|
2011-02-21 06:13:00 +02:00
|
|
|
: nodeType(NodeType)
|
2010-11-20 02:03:31 +02:00
|
|
|
{
|
2011-02-21 06:13:00 +02:00
|
|
|
}
|
|
|
|
|
2021-01-14 00:02:13 +02:00
|
|
|
CBonusSystemNode::ENodeTypes CPropagatorNodeType::getPropagatorType() const
|
|
|
|
{
|
|
|
|
return nodeType;
|
|
|
|
}
|
|
|
|
|
2011-02-21 06:13:00 +02:00
|
|
|
bool CPropagatorNodeType::shouldBeAttached(CBonusSystemNode *dest)
|
|
|
|
{
|
2011-07-13 21:39:02 +03:00
|
|
|
return nodeType == dest->getNodeType();
|
2010-11-20 02:03:31 +02:00
|
|
|
}
|
|
|
|
|
2018-04-17 14:59:30 +02:00
|
|
|
CreatureTerrainLimiter::CreatureTerrainLimiter()
|
2022-12-20 18:35:40 +02:00
|
|
|
: terrainType(ETerrainId::NATIVE_TERRAIN)
|
2010-11-20 02:03:31 +02:00
|
|
|
{
|
2022-09-21 11:34:23 +02:00
|
|
|
}
|
2010-11-20 02:03:31 +02:00
|
|
|
|
2022-09-29 11:44:46 +02:00
|
|
|
CreatureTerrainLimiter::CreatureTerrainLimiter(TerrainId terrain):
|
2022-09-21 11:34:23 +02:00
|
|
|
terrainType(terrain)
|
|
|
|
{
|
2010-11-20 02:03:31 +02:00
|
|
|
}
|
2012-03-06 19:59:55 +03:00
|
|
|
|
2023-03-29 12:16:13 +02:00
|
|
|
ILimiter::EDecision CreatureTerrainLimiter::limit(const BonusLimitationContext &context) const
|
2010-11-20 02:03:31 +02:00
|
|
|
{
|
2018-04-17 14:59:30 +02:00
|
|
|
const CStack *stack = retrieveStackBattle(&context.node);
|
|
|
|
if(stack)
|
|
|
|
{
|
2023-03-29 12:16:13 +02:00
|
|
|
if (terrainType == ETerrainId::NATIVE_TERRAIN && stack->isOnNativeTerrain())//terrainType not specified = native
|
|
|
|
return ILimiter::EDecision::ACCEPT;
|
|
|
|
|
|
|
|
if(terrainType != ETerrainId::NATIVE_TERRAIN && stack->isOnTerrain(terrainType))
|
|
|
|
return ILimiter::EDecision::ACCEPT;
|
|
|
|
|
2018-04-17 14:59:30 +02:00
|
|
|
}
|
2023-03-29 12:16:13 +02:00
|
|
|
return ILimiter::EDecision::DISCARD;
|
2011-02-21 06:13:00 +02:00
|
|
|
//TODO neutral creatues
|
2010-11-20 02:03:31 +02:00
|
|
|
}
|
|
|
|
|
2018-04-17 14:59:30 +02:00
|
|
|
std::string CreatureTerrainLimiter::toString() const
|
|
|
|
{
|
|
|
|
boost::format fmt("CreatureTerrainLimiter(terrainType=%s)");
|
2023-01-01 22:42:28 +02:00
|
|
|
auto terrainName = VLC->terrainTypeHandler->getById(terrainType)->getJsonKey();
|
2022-12-20 18:35:40 +02:00
|
|
|
fmt % (terrainType == ETerrainId::NATIVE_TERRAIN ? "native" : terrainName);
|
2018-04-17 14:59:30 +02:00
|
|
|
return fmt.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode CreatureTerrainLimiter::toJsonNode() const
|
|
|
|
{
|
|
|
|
JsonNode root(JsonNode::JsonType::DATA_STRUCT);
|
|
|
|
|
|
|
|
root["type"].String() = "CREATURE_TERRAIN_LIMITER";
|
2023-01-01 22:42:28 +02:00
|
|
|
auto terrainName = VLC->terrainTypeHandler->getById(terrainType)->getJsonKey();
|
2022-09-21 11:34:23 +02:00
|
|
|
root["parameters"].Vector().push_back(JsonUtils::stringNode(terrainName));
|
2018-04-17 14:59:30 +02:00
|
|
|
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2023-04-09 03:36:16 +02:00
|
|
|
FactionLimiter::FactionLimiter(FactionID creatureFaction)
|
2020-12-10 03:05:37 +02:00
|
|
|
: faction(creatureFaction)
|
2010-11-20 02:03:31 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-04-09 03:36:16 +02:00
|
|
|
ILimiter::EDecision FactionLimiter::limit(const BonusLimitationContext &context) const
|
2010-11-20 02:03:31 +02:00
|
|
|
{
|
2023-04-09 03:36:16 +02:00
|
|
|
const auto * bearer = dynamic_cast<const INativeTerrainProvider*>(&context.node);
|
|
|
|
|
|
|
|
if(bearer)
|
|
|
|
{
|
|
|
|
if(faction != FactionID::DEFAULT)
|
|
|
|
return bearer->getFaction() == faction ? ILimiter::EDecision::ACCEPT : ILimiter::EDecision::DISCARD;
|
|
|
|
|
|
|
|
switch(context.b->source)
|
|
|
|
{
|
|
|
|
case Bonus::CREATURE_ABILITY:
|
|
|
|
return faction == CreatureID(context.b->sid).toCreature()->getFaction() ? ILimiter::EDecision::ACCEPT : ILimiter::EDecision::DISCARD;
|
|
|
|
|
|
|
|
case Bonus::TOWN_STRUCTURE:
|
|
|
|
return faction == FactionID(Bonus::getHighFromSid32(context.b->sid)) ? ILimiter::EDecision::ACCEPT : ILimiter::EDecision::DISCARD;
|
|
|
|
|
|
|
|
//TODO: other sources of bonuses
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ILimiter::EDecision::DISCARD; //Discard by default
|
2010-11-20 02:03:31 +02:00
|
|
|
}
|
|
|
|
|
2023-04-09 03:36:16 +02:00
|
|
|
std::string FactionLimiter::toString() const
|
2018-04-17 14:59:30 +02:00
|
|
|
{
|
2023-04-09 03:36:16 +02:00
|
|
|
boost::format fmt("FactionLimiter(faction=%s)");
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
fmt % VLC->factions()->getByIndex(faction)->getJsonKey();
|
2018-04-17 14:59:30 +02:00
|
|
|
return fmt.str();
|
|
|
|
}
|
|
|
|
|
2023-04-09 03:36:16 +02:00
|
|
|
JsonNode FactionLimiter::toJsonNode() const
|
2018-04-17 14:59:30 +02:00
|
|
|
{
|
|
|
|
JsonNode root(JsonNode::JsonType::DATA_STRUCT);
|
|
|
|
|
2023-04-09 03:36:16 +02:00
|
|
|
root["type"].String() = "FACTION_LIMITER";
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
root["parameters"].Vector().push_back(JsonUtils::stringNode(VLC->factions()->getByIndex(faction)->getJsonKey()));
|
2018-04-17 14:59:30 +02:00
|
|
|
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2023-04-06 17:28:02 +02:00
|
|
|
CreatureLevelLimiter::CreatureLevelLimiter(uint32_t minLevel, uint32_t maxLevel) :
|
|
|
|
minLevel(minLevel),
|
|
|
|
maxLevel(maxLevel)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ILimiter::EDecision CreatureLevelLimiter::limit(const BonusLimitationContext &context) const
|
|
|
|
{
|
|
|
|
const auto *c = retrieveCreature(&context.node);
|
|
|
|
auto accept = c && (c->getLevel() < maxLevel && c->getLevel() >= minLevel);
|
|
|
|
return accept ? ILimiter::EDecision::ACCEPT : ILimiter::EDecision::DISCARD; //drop bonus for non-creatures or non-native residents
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CreatureLevelLimiter::toString() const
|
|
|
|
{
|
|
|
|
boost::format fmt("CreatureLevelLimiter(minLevel=%d,maxLevel=%d)");
|
|
|
|
fmt % minLevel % maxLevel;
|
|
|
|
return fmt.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode CreatureLevelLimiter::toJsonNode() const
|
|
|
|
{
|
|
|
|
JsonNode root(JsonNode::JsonType::DATA_STRUCT);
|
|
|
|
|
|
|
|
root["type"].String() = "CREATURE_LEVEL_LIMITER";
|
|
|
|
root["parameters"].Vector().push_back(JsonUtils::intNode(minLevel));
|
|
|
|
root["parameters"].Vector().push_back(JsonUtils::intNode(maxLevel));
|
|
|
|
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2023-04-04 12:36:42 +02:00
|
|
|
CreatureAlignmentLimiter::CreatureAlignmentLimiter(EAlignment Alignment)
|
2010-11-20 02:03:31 +02:00
|
|
|
: alignment(Alignment)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-03-29 12:16:13 +02:00
|
|
|
ILimiter::EDecision CreatureAlignmentLimiter::limit(const BonusLimitationContext &context) const
|
2010-11-20 02:03:31 +02:00
|
|
|
{
|
2023-03-29 12:16:13 +02:00
|
|
|
const auto * c = retrieveCreature(&context.node);
|
|
|
|
if(c) {
|
|
|
|
if(alignment == EAlignment::GOOD && c->isGood())
|
|
|
|
return ILimiter::EDecision::ACCEPT;
|
|
|
|
if(alignment == EAlignment::EVIL && c->isEvil())
|
|
|
|
return ILimiter::EDecision::ACCEPT;
|
|
|
|
if(alignment == EAlignment::NEUTRAL && !c->isEvil() && !c->isGood())
|
|
|
|
return ILimiter::EDecision::ACCEPT;
|
2010-11-20 02:03:31 +02:00
|
|
|
}
|
2023-03-29 12:16:13 +02:00
|
|
|
|
|
|
|
return ILimiter::EDecision::DISCARD;
|
2011-02-11 10:20:26 +02:00
|
|
|
}
|
|
|
|
|
2018-04-17 14:59:30 +02:00
|
|
|
std::string CreatureAlignmentLimiter::toString() const
|
|
|
|
{
|
|
|
|
boost::format fmt("CreatureAlignmentLimiter(alignment=%s)");
|
2023-04-04 12:36:42 +02:00
|
|
|
fmt % GameConstants::ALIGNMENT_NAMES[vstd::to_underlying(alignment)];
|
2018-04-17 14:59:30 +02:00
|
|
|
return fmt.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode CreatureAlignmentLimiter::toJsonNode() const
|
|
|
|
{
|
|
|
|
JsonNode root(JsonNode::JsonType::DATA_STRUCT);
|
|
|
|
|
|
|
|
root["type"].String() = "CREATURE_ALIGNMENT_LIMITER";
|
2023-04-04 12:36:42 +02:00
|
|
|
root["parameters"].Vector().push_back(JsonUtils::stringNode(GameConstants::ALIGNMENT_NAMES[vstd::to_underlying(alignment)]));
|
2018-04-17 14:59:30 +02:00
|
|
|
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2011-02-12 18:12:48 +02:00
|
|
|
RankRangeLimiter::RankRangeLimiter(ui8 Min, ui8 Max)
|
|
|
|
:minRank(Min), maxRank(Max)
|
2011-02-11 10:20:26 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-02-21 06:13:00 +02:00
|
|
|
RankRangeLimiter::RankRangeLimiter()
|
|
|
|
{
|
|
|
|
minRank = maxRank = -1;
|
|
|
|
}
|
|
|
|
|
2023-03-29 12:16:13 +02:00
|
|
|
ILimiter::EDecision RankRangeLimiter::limit(const BonusLimitationContext &context) const
|
2011-02-11 10:20:26 +02:00
|
|
|
{
|
2018-02-10 20:52:23 +02:00
|
|
|
const CStackInstance * csi = retrieveStackInstance(&context.node);
|
2011-02-12 18:12:48 +02:00
|
|
|
if(csi)
|
2012-08-20 19:10:50 +03:00
|
|
|
{
|
2013-02-05 22:56:28 +03:00
|
|
|
if (csi->getNodeType() == CBonusSystemNode::COMMANDER) //no stack exp bonuses for commander creatures
|
2023-03-29 12:16:13 +02:00
|
|
|
return ILimiter::EDecision::DISCARD;
|
|
|
|
if (csi->getExpRank() > minRank && csi->getExpRank() < maxRank)
|
|
|
|
return ILimiter::EDecision::ACCEPT;
|
2012-08-20 19:10:50 +03:00
|
|
|
}
|
2023-03-29 12:16:13 +02:00
|
|
|
return ILimiter::EDecision::DISCARD;
|
2011-02-21 06:13:00 +02:00
|
|
|
}
|
|
|
|
|
2023-04-09 03:36:16 +02:00
|
|
|
OppositeSideLimiter::OppositeSideLimiter(PlayerColor Owner):
|
|
|
|
owner(std::move(Owner))
|
2021-03-23 16:47:07 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-03-29 12:16:13 +02:00
|
|
|
ILimiter::EDecision OppositeSideLimiter::limit(const BonusLimitationContext & context) const
|
2021-03-23 16:47:07 +02:00
|
|
|
{
|
|
|
|
auto contextOwner = CBonusSystemNode::retrieveNodeOwner(& context.node);
|
2023-03-29 12:16:13 +02:00
|
|
|
auto decision = (owner == contextOwner || owner == PlayerColor::CANNOT_DETERMINE) ? ILimiter::EDecision::DISCARD : ILimiter::EDecision::ACCEPT;
|
2021-03-23 16:47:07 +02:00
|
|
|
return decision;
|
|
|
|
}
|
|
|
|
|
2018-04-01 13:17:34 +02:00
|
|
|
// Aggregate/Boolean Limiters
|
|
|
|
|
2023-04-09 03:36:16 +02:00
|
|
|
AggregateLimiter::AggregateLimiter(std::vector<TLimiterPtr> limiters):
|
|
|
|
limiters(std::move(limiters))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
void AggregateLimiter::add(const TLimiterPtr & limiter)
|
2018-04-01 13:17:34 +02:00
|
|
|
{
|
|
|
|
if(limiter)
|
|
|
|
limiters.push_back(limiter);
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode AggregateLimiter::toJsonNode() const
|
|
|
|
{
|
|
|
|
JsonNode result(JsonNode::JsonType::DATA_VECTOR);
|
|
|
|
result.Vector().push_back(JsonUtils::stringNode(getAggregator()));
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const auto & l : limiters)
|
2018-04-01 13:17:34 +02:00
|
|
|
result.Vector().push_back(l->toJsonNode());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string AllOfLimiter::aggregator = "allOf";
|
|
|
|
const std::string & AllOfLimiter::getAggregator() const
|
|
|
|
{
|
|
|
|
return aggregator;
|
|
|
|
}
|
|
|
|
|
2023-04-09 03:36:16 +02:00
|
|
|
AllOfLimiter::AllOfLimiter(std::vector<TLimiterPtr> limiters):
|
|
|
|
AggregateLimiter(limiters)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-03-29 12:16:13 +02:00
|
|
|
ILimiter::EDecision AllOfLimiter::limit(const BonusLimitationContext & context) const
|
2013-01-20 15:06:18 +03:00
|
|
|
{
|
|
|
|
bool wasntSure = false;
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const auto & limiter : limiters)
|
2013-01-20 15:06:18 +03:00
|
|
|
{
|
|
|
|
auto result = limiter->limit(context);
|
2023-03-29 12:16:13 +02:00
|
|
|
if(result == ILimiter::EDecision::DISCARD)
|
2013-01-20 15:06:18 +03:00
|
|
|
return result;
|
2023-03-29 12:16:13 +02:00
|
|
|
if(result == ILimiter::EDecision::NOT_SURE)
|
2013-01-20 15:06:18 +03:00
|
|
|
wasntSure = true;
|
|
|
|
}
|
|
|
|
|
2023-03-29 12:16:13 +02:00
|
|
|
return wasntSure ? ILimiter::EDecision::NOT_SURE : ILimiter::EDecision::ACCEPT;
|
2013-01-20 15:06:18 +03:00
|
|
|
}
|
|
|
|
|
2018-04-01 13:17:34 +02:00
|
|
|
const std::string AnyOfLimiter::aggregator = "anyOf";
|
|
|
|
const std::string & AnyOfLimiter::getAggregator() const
|
2013-01-20 15:06:18 +03:00
|
|
|
{
|
2018-04-01 13:17:34 +02:00
|
|
|
return aggregator;
|
|
|
|
}
|
|
|
|
|
2023-04-09 03:36:16 +02:00
|
|
|
AnyOfLimiter::AnyOfLimiter(std::vector<TLimiterPtr> limiters):
|
|
|
|
AggregateLimiter(limiters)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-03-29 12:16:13 +02:00
|
|
|
ILimiter::EDecision AnyOfLimiter::limit(const BonusLimitationContext & context) const
|
2018-04-01 13:17:34 +02:00
|
|
|
{
|
|
|
|
bool wasntSure = false;
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const auto & limiter : limiters)
|
2018-04-01 13:17:34 +02:00
|
|
|
{
|
|
|
|
auto result = limiter->limit(context);
|
2023-03-29 12:16:13 +02:00
|
|
|
if(result == ILimiter::EDecision::ACCEPT)
|
2018-04-01 13:17:34 +02:00
|
|
|
return result;
|
2023-03-29 12:16:13 +02:00
|
|
|
if(result == ILimiter::EDecision::NOT_SURE)
|
2018-04-01 13:17:34 +02:00
|
|
|
wasntSure = true;
|
|
|
|
}
|
|
|
|
|
2023-03-29 12:16:13 +02:00
|
|
|
return wasntSure ? ILimiter::EDecision::NOT_SURE : ILimiter::EDecision::DISCARD;
|
2018-04-01 13:17:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::string NoneOfLimiter::aggregator = "noneOf";
|
|
|
|
const std::string & NoneOfLimiter::getAggregator() const
|
|
|
|
{
|
|
|
|
return aggregator;
|
|
|
|
}
|
|
|
|
|
2023-04-09 03:36:16 +02:00
|
|
|
NoneOfLimiter::NoneOfLimiter(std::vector<TLimiterPtr> limiters):
|
|
|
|
AggregateLimiter(limiters)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-03-29 12:16:13 +02:00
|
|
|
ILimiter::EDecision NoneOfLimiter::limit(const BonusLimitationContext & context) const
|
2018-04-01 13:17:34 +02:00
|
|
|
{
|
|
|
|
bool wasntSure = false;
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const auto & limiter : limiters)
|
2018-04-01 13:17:34 +02:00
|
|
|
{
|
|
|
|
auto result = limiter->limit(context);
|
2023-03-29 12:16:13 +02:00
|
|
|
if(result == ILimiter::EDecision::ACCEPT)
|
|
|
|
return ILimiter::EDecision::DISCARD;
|
|
|
|
if(result == ILimiter::EDecision::NOT_SURE)
|
2018-04-01 13:17:34 +02:00
|
|
|
wasntSure = true;
|
|
|
|
}
|
|
|
|
|
2023-03-29 12:16:13 +02:00
|
|
|
return wasntSure ? ILimiter::EDecision::NOT_SURE : ILimiter::EDecision::ACCEPT;
|
2013-01-20 15:06:18 +03:00
|
|
|
}
|
2017-09-09 07:43:53 +02:00
|
|
|
|
2017-09-10 05:20:47 +02:00
|
|
|
// Updaters
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
std::shared_ptr<Bonus> Bonus::addUpdater(const TUpdaterPtr & Updater)
|
2017-09-12 01:56:38 +02:00
|
|
|
{
|
|
|
|
updater = Updater;
|
|
|
|
return this->shared_from_this();
|
|
|
|
}
|
|
|
|
|
2021-09-12 13:30:54 +02:00
|
|
|
std::shared_ptr<Bonus> IUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
|
2017-09-19 00:08:28 +02:00
|
|
|
{
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2017-09-11 02:36:02 +02:00
|
|
|
std::string IUpdater::toString() const
|
|
|
|
{
|
|
|
|
return typeid(*this).name();
|
|
|
|
}
|
|
|
|
|
2017-09-19 00:08:28 +02:00
|
|
|
JsonNode IUpdater::toJsonNode() const
|
2017-09-10 05:20:47 +02:00
|
|
|
{
|
2017-09-19 00:08:28 +02:00
|
|
|
return JsonNode(JsonNode::JsonType::DATA_NULL);
|
2017-09-10 05:20:47 +02:00
|
|
|
}
|
|
|
|
|
2017-09-19 00:08:28 +02:00
|
|
|
GrowsWithLevelUpdater::GrowsWithLevelUpdater(int valPer20, int stepSize) : valPer20(valPer20), stepSize(stepSize)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-09-12 13:30:54 +02:00
|
|
|
std::shared_ptr<Bonus> GrowsWithLevelUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
|
2017-09-09 07:43:53 +02:00
|
|
|
{
|
2017-09-09 14:25:55 +02:00
|
|
|
if(context.getNodeType() == CBonusSystemNode::HERO)
|
2017-09-09 07:43:53 +02:00
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
int level = dynamic_cast<const CGHeroInstance &>(context).level;
|
2017-09-09 07:43:53 +02:00
|
|
|
int steps = stepSize ? level / stepSize : level;
|
|
|
|
//rounding follows format for HMM3 creature specialty bonus
|
2017-09-09 23:32:54 +02:00
|
|
|
int newVal = (valPer20 * steps + 19) / 20;
|
2017-09-16 02:26:48 +02:00
|
|
|
//return copy of bonus with updated val
|
|
|
|
std::shared_ptr<Bonus> newBonus = std::make_shared<Bonus>(*b);
|
|
|
|
newBonus->val = newVal;
|
|
|
|
return newBonus;
|
2017-09-09 07:43:53 +02:00
|
|
|
}
|
2017-09-16 02:26:48 +02:00
|
|
|
return b;
|
2017-09-09 07:43:53 +02:00
|
|
|
}
|
|
|
|
|
2017-09-19 00:08:28 +02:00
|
|
|
std::string GrowsWithLevelUpdater::toString() const
|
2017-09-11 02:36:02 +02:00
|
|
|
{
|
2018-02-20 12:36:00 +02:00
|
|
|
return boost::str(boost::format("GrowsWithLevelUpdater(valPer20=%d, stepSize=%d)") % valPer20 % stepSize);
|
2017-09-11 02:36:02 +02:00
|
|
|
}
|
|
|
|
|
2017-09-19 00:08:28 +02:00
|
|
|
JsonNode GrowsWithLevelUpdater::toJsonNode() const
|
2017-09-11 08:21:24 +02:00
|
|
|
{
|
2017-09-12 10:56:59 +02:00
|
|
|
JsonNode root(JsonNode::JsonType::DATA_STRUCT);
|
2017-09-11 08:21:24 +02:00
|
|
|
|
|
|
|
root["type"].String() = "GROWS_WITH_LEVEL";
|
2017-09-12 01:56:38 +02:00
|
|
|
root["parameters"].Vector().push_back(JsonUtils::intNode(valPer20));
|
2017-09-11 08:21:24 +02:00
|
|
|
if(stepSize > 1)
|
2017-09-12 01:56:38 +02:00
|
|
|
root["parameters"].Vector().push_back(JsonUtils::intNode(stepSize));
|
2017-09-11 08:21:24 +02:00
|
|
|
|
|
|
|
return root;
|
|
|
|
}
|
2017-09-19 00:08:28 +02:00
|
|
|
|
2021-09-12 13:30:54 +02:00
|
|
|
std::shared_ptr<Bonus> TimesHeroLevelUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
|
2017-09-19 00:08:28 +02:00
|
|
|
{
|
|
|
|
if(context.getNodeType() == CBonusSystemNode::HERO)
|
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
int level = dynamic_cast<const CGHeroInstance &>(context).level;
|
2017-09-19 00:08:28 +02:00
|
|
|
std::shared_ptr<Bonus> newBonus = std::make_shared<Bonus>(*b);
|
|
|
|
newBonus->val *= level;
|
|
|
|
return newBonus;
|
|
|
|
}
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TimesHeroLevelUpdater::toString() const
|
|
|
|
{
|
|
|
|
return "TimesHeroLevelUpdater";
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode TimesHeroLevelUpdater::toJsonNode() const
|
|
|
|
{
|
|
|
|
return JsonUtils::stringNode("TIMES_HERO_LEVEL");
|
|
|
|
}
|
2017-09-19 01:43:13 +02:00
|
|
|
|
2023-03-11 22:09:16 +02:00
|
|
|
ArmyMovementUpdater::ArmyMovementUpdater():
|
|
|
|
base(20),
|
|
|
|
divider(3),
|
|
|
|
multiplier(10),
|
|
|
|
max(700)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ArmyMovementUpdater::ArmyMovementUpdater(int base, int divider, int multiplier, int max):
|
|
|
|
base(base),
|
|
|
|
divider(divider),
|
|
|
|
multiplier(multiplier),
|
|
|
|
max(max)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-02-18 20:01:32 +02:00
|
|
|
std::shared_ptr<Bonus> ArmyMovementUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
|
|
|
|
{
|
|
|
|
if(b->type == Bonus::MOVEMENT && context.getNodeType() == CBonusSystemNode::HERO)
|
|
|
|
{
|
2023-03-11 22:09:16 +02:00
|
|
|
auto speed = static_cast<const CGHeroInstance &>(context).getLowestCreatureSpeed();
|
|
|
|
si32 armySpeed = speed * base / divider;
|
|
|
|
auto counted = armySpeed * multiplier;
|
2023-02-18 20:01:32 +02:00
|
|
|
auto newBonus = std::make_shared<Bonus>(*b);
|
|
|
|
newBonus->source = Bonus::ARMY;
|
2023-03-12 13:39:36 +02:00
|
|
|
newBonus->val += vstd::amin(counted, max);
|
2023-02-18 20:01:32 +02:00
|
|
|
return newBonus;
|
|
|
|
}
|
|
|
|
if(b->type != Bonus::MOVEMENT)
|
|
|
|
logGlobal->error("ArmyMovementUpdater should only be used for MOVEMENT bonus!");
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ArmyMovementUpdater::toString() const
|
|
|
|
{
|
|
|
|
return "ArmyMovementUpdater";
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode ArmyMovementUpdater::toJsonNode() const
|
|
|
|
{
|
2023-03-11 22:09:16 +02:00
|
|
|
JsonNode root(JsonNode::JsonType::DATA_STRUCT);
|
|
|
|
|
|
|
|
root["type"].String() = "ARMY_MOVEMENT";
|
|
|
|
root["parameters"].Vector().push_back(JsonUtils::intNode(base));
|
|
|
|
root["parameters"].Vector().push_back(JsonUtils::intNode(divider));
|
|
|
|
root["parameters"].Vector().push_back(JsonUtils::intNode(multiplier));
|
|
|
|
root["parameters"].Vector().push_back(JsonUtils::intNode(max));
|
|
|
|
|
|
|
|
return root;
|
2023-02-18 20:01:32 +02:00
|
|
|
}
|
2021-09-12 13:30:54 +02:00
|
|
|
std::shared_ptr<Bonus> TimesStackLevelUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
|
2017-09-19 01:43:13 +02:00
|
|
|
{
|
|
|
|
if(context.getNodeType() == CBonusSystemNode::STACK_INSTANCE)
|
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
int level = dynamic_cast<const CStackInstance &>(context).getLevel();
|
2017-09-19 01:43:13 +02:00
|
|
|
std::shared_ptr<Bonus> newBonus = std::make_shared<Bonus>(*b);
|
|
|
|
newBonus->val *= level;
|
|
|
|
return newBonus;
|
|
|
|
}
|
|
|
|
else if(context.getNodeType() == CBonusSystemNode::STACK_BATTLE)
|
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
const auto & stack = dynamic_cast<const CStack &>(context);
|
2017-09-19 01:43:13 +02:00
|
|
|
//only update if stack doesn't have an instance (summons, war machines)
|
|
|
|
//otherwise we'd end up multiplying twice
|
|
|
|
if(stack.base == nullptr)
|
|
|
|
{
|
2023-04-05 02:26:29 +02:00
|
|
|
int level = stack.type->getLevel();
|
2017-09-19 01:43:13 +02:00
|
|
|
std::shared_ptr<Bonus> newBonus = std::make_shared<Bonus>(*b);
|
|
|
|
newBonus->val *= level;
|
|
|
|
return newBonus;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TimesStackLevelUpdater::toString() const
|
|
|
|
{
|
|
|
|
return "TimesStackLevelUpdater";
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode TimesStackLevelUpdater::toJsonNode() const
|
|
|
|
{
|
|
|
|
return JsonUtils::stringNode("TIMES_STACK_LEVEL");
|
|
|
|
}
|
2021-09-12 13:30:54 +02:00
|
|
|
|
|
|
|
std::string OwnerUpdater::toString() const
|
|
|
|
{
|
|
|
|
return "OwnerUpdater";
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode OwnerUpdater::toJsonNode() const
|
|
|
|
{
|
|
|
|
return JsonUtils::stringNode("BONUS_OWNER_UPDATER");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Bonus> OwnerUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
|
|
|
|
{
|
|
|
|
auto owner = CBonusSystemNode::retrieveNodeOwner(&context);
|
|
|
|
|
|
|
|
if(owner == PlayerColor::UNFLAGGABLE)
|
|
|
|
owner = PlayerColor::NEUTRAL;
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
std::shared_ptr<Bonus> updated =
|
|
|
|
std::make_shared<Bonus>(static_cast<Bonus::BonusDuration>(b->duration), b->type, b->source, b->val, b->sid, b->subtype, b->valType);
|
2021-09-12 13:30:54 +02:00
|
|
|
updated->limiter = std::make_shared<OppositeSideLimiter>(owner);
|
|
|
|
return updated;
|
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|