1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00
vcmi/lib/HeroBonus.cpp

2791 lines
75 KiB
C++
Raw Normal View History

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
*
*/
#include "StdInc.h"
#include "HeroBonus.h"
#include "VCMI_Lib.h"
#include "spells/CSpellHandler.h"
#include "CCreatureHandler.h"
#include "CCreatureSet.h"
#include "CHeroHandler.h"
#include "CTownHandler.h"
#include "CGeneralTextHandler.h"
#include "CSkillHandler.h"
#include "CStack.h"
#include "CArtHandler.h"
#include "CModHandler.h"
#include "TerrainHandler.h"
#include "StringConstants.h"
#include "battle/BattleInfo.h"
2010-04-03 06:33:46 +03:00
VCMI_LIB_NAMESPACE_BEGIN
#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)
2014-10-02 18:43:46 +03:00
#define BONUS_NAME(x) { #x, Bonus::x },
const std::map<std::string, Bonus::BonusType> bonusNameMap = {
BONUS_LIST
};
#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 };
#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 };
#undef BONUS_SOURCE
2014-10-02 18:43:46 +03:00
#define BONUS_ITEM(x) { #x, Bonus::x },
2016-08-18 14:03:20 +02:00
const std::map<std::string, ui16> bonusDurationMap =
2014-10-02 18:43:46 +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)
BONUS_ITEM(UNTIL_BEING_ATTACKED)
BONUS_ITEM(UNTIL_ATTACK)
BONUS_ITEM(STACK_GETS_TURN)
2014-10-02 18:43:46 +03:00
BONUS_ITEM(COMMANDER_KILLED)
{ "UNITL_BEING_ATTACKED", Bonus::UNTIL_BEING_ATTACKED }//typo, but used in some mods
2014-10-02 18:43:46 +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
{
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 =
{
{"SHOOTER_ONLY", std::make_shared<HasAnotherBonusLimiter>(Bonus::SHOOTER)},
{"DRAGON_NATURE", std::make_shared<HasAnotherBonusLimiter>(Bonus::DRAGON_NATURE)},
{"IS_UNDEAD", std::make_shared<HasAnotherBonusLimiter>(Bonus::UNDEAD)},
{"CREATURE_NATIVE_TERRAIN", std::make_shared<CreatureTerrainLimiter>()},
2021-03-23 16:47:07 +02:00
{"CREATURE_FACTION", std::make_shared<CreatureFactionLimiter>()},
{"OPPOSITE_SIDE", std::make_shared<OppositeSideLimiter>()}
2014-10-02 18:43:46 +03:00
};
const std::map<std::string, TPropagatorPtr> bonusPropagatorMap =
{
{"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
{"GLOBAL_EFFECT", std::make_shared<CPropagatorNodeType>(CBonusSystemNode::GLOBAL_EFFECTS)},
{"ALL_CREATURES", std::make_shared<CPropagatorNodeType>(CBonusSystemNode::ALL_CREATURES)}
2014-10-02 18:43:46 +03:00
}; //untested
const std::map<std::string, TUpdaterPtr> bonusUpdaterMap =
{
2017-09-19 01:43:13 +02:00
{"TIMES_HERO_LEVEL", std::make_shared<TimesHeroLevelUpdater>()},
{"TIMES_STACK_LEVEL", std::make_shared<TimesStackLevelUpdater>()},
{"ARMY_MOVEMENT", std::make_shared<ArmyMovementUpdater>()},
{"BONUS_OWNER_UPDATER", std::make_shared<OwnerUpdater>()}
};
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",
"BLOCK_MORALE",
"BLOCK_LUCK",
"SELF_MORALE",
"SELF_LUCK"
};
///CBonusProxy
2023-03-13 23:26:44 +02:00
CBonusProxy::CBonusProxy(const IBonusBearer * Target, CSelector Selector):
bonusListCachedLast(0),
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
target(Target),
2023-03-13 23:26:44 +02:00
selector(std::move(Selector)),
currentBonusListIndex(0)
{
}
2023-03-13 23:26:44 +02:00
CBonusProxy::CBonusProxy(const CBonusProxy & other):
bonusListCachedLast(other.bonusListCachedLast),
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
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)
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
{
2022-04-04 09:16:32 +02:00
bonusList[currentBonusListIndex] = other.bonusList[currentBonusListIndex];
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
}
2023-03-13 23:26:44 +02:00
CBonusProxy::CBonusProxy(CBonusProxy && other) noexcept:
bonusListCachedLast(0),
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
target(other.target),
2023-03-13 23:26:44 +02:00
currentBonusListIndex(0)
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
{
std::swap(bonusListCachedLast, other.bonusListCachedLast);
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
std::swap(selector, other.selector);
std::swap(bonusList, other.bonusList);
2022-04-04 09:16:32 +02:00
std::swap(currentBonusListIndex, other.currentBonusListIndex);
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
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);
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
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;
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
return *this;
}
2023-03-13 23:26:44 +02:00
CBonusProxy & CBonusProxy::operator=(CBonusProxy && other) noexcept
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
{
std::swap(bonusListCachedLast, other.bonusListCachedLast);
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
std::swap(selector, other.selector);
std::swap(bonusList, other.bonusList);
2022-04-04 09:16:32 +02:00
std::swap(currentBonusListIndex, other.currentBonusListIndex);
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
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;
}
TConstBonusListPtr CBonusProxy::getBonusList() const
{
2022-04-04 09:16:32 +02:00
auto needUpdateBonusList = [&]() -> bool
{
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();
}
}
2022-04-04 09:16:32 +02:00
return bonusList[currentBonusListIndex];
}
const BonusList * CBonusProxy::operator->() const
{
return getBonusList().get();
}
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)
: 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();
if(treeVersion != valueCachedLast)
2020-06-28 15:19:27 +02:00
{
auto bonuses = getBonusList();
2020-06-28 15:19:27 +02:00
value = initialValue + bonuses->totalValue();
valueCachedLast = treeVersion;
2020-06-28 15:19:27 +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
if(treeVersion != valueCachedLast)
{
value = initialValue + outBonusList->totalValue();
valueCachedLast = treeVersion;
}
2020-06-28 15:19:27 +02:00
return value;
}
int CTotalsProxy::getMeleeValue() const
{
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
{
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;
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);
}
si32 & CAddInfo::operator[](size_type pos)
{
if(pos >= size())
resize(pos + 1, CAddInfo::NONE);
return vector::operator[](pos);
}
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;
}
}
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
std::atomic<int32_t> CBonusSystemNode::treeChanged(1);
const bool CBonusSystemNode::cachingEnabled = true;
BonusList::BonusList(bool BelongsToTree) : belongsToTree(BelongsToTree)
{
}
2023-03-13 23:26:44 +02:00
BonusList::BonusList(const BonusList & bonusList): belongsToTree(false)
{
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);
}
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();
}
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
{
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;
std::shared_ptr<Bonus> last = bonuses[next-1];
std::shared_ptr<Bonus> current = bonuses[next];
if(current->stacking.empty())
remove = current == last;
else if(current->stacking == "ALWAYS")
remove = false;
else
remove = current->stacking == last->stacking
&& current->type == last->type
&& current->subtype == last->subtype
&& current->valType == last->valType;
if(remove)
bonuses.erase(bonuses.begin() + next);
else
next++;
}
}
int BonusList::totalValue() const
{
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;
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 {
return static_cast<int>(vstd::clamp((base * (100 + percent)) / 100, std::numeric_limits<int>::min(), std::numeric_limits<int>::max()));
};
std::array <BonusCollection, Bonus::BonusSource::NUM_BONUS_SOURCE> sources = {};
BonusCollection any;
bool hasIndepMax = false;
bool hasIndepMin = false;
2023-03-18 14:32:31 +02:00
for(const auto & b : bonuses)
{
switch(b->valType)
{
case Bonus::BASE_NUMBER:
sources[b->source].base += b->val;
break;
case Bonus::PERCENT_TO_ALL:
sources[b->source].percentToAll += b->val;
break;
case Bonus::PERCENT_TO_BASE:
sources[b->source].percentToBase += b->val;
break;
case Bonus::PERCENT_TO_SOURCE:
sources[b->source].percentToSource += b->val;
break;
case Bonus::PERCENT_TO_TARGET_TYPE:
sources[b->targetSourceType].percentToSource += b->val;
break;
case Bonus::ADDITIVE_VALUE:
sources[b->source].additive += b->val;
break;
case Bonus::INDEPENDENT_MAX:
hasIndepMax = true;
vstd::amax(sources[b->source].indepMax, b->val);
break;
case Bonus::INDEPENDENT_MIN:
hasIndepMin = true;
vstd::amin(sources[b->source].indepMin, b->val);
break;
}
}
2023-03-18 14:32:31 +02:00
for(const auto & src : sources)
{
2023-03-18 14:32:31 +02:00
any.base += percent(src.base, src.percentToSource);
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));
}
any.base = percent(any.base, any.percentToBase);
any.base += any.additive;
auto valFirst = percent(any.base ,any.percentToAll);
2023-03-18 16:04:52 +02:00
if(hasIndepMin && hasIndepMax && any.indepMin < any.indepMax)
any.indepMax = any.indepMin;
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)
{
return b->valType != Bonus::INDEPENDENT_MAX && b->valType != Bonus::INDEPENDENT_MIN;
2023-03-13 23:26:44 +02:00
}));
2023-03-18 16:04:52 +02:00
if(notIndepBonuses)
return vstd::clamp(valFirst, any.indepMax, any.indepMin);
return hasIndepMin ? any.indepMin : hasIndepMax ? any.indepMax : 0;
}
std::shared_ptr<Bonus> BonusList::getFirst(const CSelector &select)
{
for (auto & b : bonuses)
{
if(select(b.get()))
return b;
}
return nullptr;
}
std::shared_ptr<const Bonus> BonusList::getFirst(const CSelector &selector) const
{
2023-03-13 23:26:44 +02:00
for(const auto & b : bonuses)
{
if(selector(b.get()))
return b;
}
return nullptr;
}
void BonusList::getBonuses(BonusList & out, const CSelector &selector, const CSelector &limit) const
{
out.reserve(bonuses.size());
2023-03-13 23:26:44 +02:00
for(const auto & b : bonuses)
{
//add matching bonuses that matches limit predicate or have NO_LIMIT if no given predicate
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()))))
out.push_back(b);
}
}
void BonusList::getAllBonuses(BonusList &out) const
{
2023-03-13 23:26:44 +02:00
for(const auto & b : bonuses)
out.push_back(b);
}
int BonusList::valOfBonuses(const CSelector &select) const
{
BonusList ret;
CSelector limit = nullptr;
getBonuses(ret, select, limit);
return ret.totalValue();
}
JsonNode BonusList::toJsonNode() const
{
JsonNode node(JsonNode::JsonType::DATA_VECTOR);
2023-03-13 23:26:44 +02:00
for(const std::shared_ptr<Bonus> & b : bonuses)
node.Vector().push_back(b->toJsonNode());
return node;
}
2023-03-13 23:26:44 +02:00
void BonusList::push_back(const std::shared_ptr<Bonus> & x)
{
bonuses.push_back(x);
2016-07-29 12:10:32 +02:00
changed();
}
BonusList::TInternalContainer::iterator BonusList::erase(const int position)
{
2016-07-29 12:10:32 +02:00
changed();
return bonuses.erase(bonuses.begin() + position);
}
void BonusList::clear()
{
bonuses.clear();
2016-07-29 12:10:32 +02:00
changed();
}
2023-03-13 23:26:44 +02:00
std::vector<BonusList *>::size_type BonusList::operator-=(const std::shared_ptr<Bonus> & i)
{
auto itr = std::find(bonuses.begin(), bonuses.end(), i);
if(itr == bonuses.end())
return false;
bonuses.erase(itr);
2016-07-29 12:10:32 +02:00
changed();
return true;
}
2023-03-13 23:26:44 +02:00
void BonusList::resize(BonusList::TInternalContainer::size_type sz, const std::shared_ptr<Bonus> & c)
{
bonuses.resize(sz, c);
2016-07-29 12:10:32 +02:00
changed();
}
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)
{
bonuses.insert(position, n, x);
2016-07-29 12:10:32 +02:00
changed();
}
CSelector IBonusBearer::anaffectedByMoraleSelector =
Selector::type()(Bonus::NON_LIVING)
.Or(Selector::type()(Bonus::UNDEAD))
.Or(Selector::type()(Bonus::SIEGE_WEAPON))
.Or(Selector::type()(Bonus::NO_MORALE));
2020-06-28 15:19:27 +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),
luckValue(this, luckSelector, 0)
2020-06-28 15:19:27 +02:00
{
}
int IBonusBearer::valOfBonuses(Bonus::BonusType type, const CSelector &selector) const
{
return valOfBonuses(Selector::type()(type).And(selector));
}
int IBonusBearer::valOfBonuses(Bonus::BonusType type, int subtype) const
{
//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);
CSelector s = Selector::type()(type);
if(subtype != -1)
s = s.And(Selector::subtype()(subtype));
return valOfBonuses(s, cachingStr);
}
int IBonusBearer::valOfBonuses(const CSelector &selector, const std::string &cachingStr) const
{
CSelector limit = nullptr;
TConstBonusListPtr hlp = getAllBonuses(selector, limit, nullptr, cachingStr);
return hlp->totalValue();
}
bool IBonusBearer::hasBonus(const CSelector &selector, const std::string &cachingStr) const
{
//TODO: We don't need to count all bonuses and could break on first matching
return getBonuses(selector, cachingStr)->size() > 0;
}
bool IBonusBearer::hasBonus(const CSelector &selector, const CSelector &limit, const std::string &cachingStr) const
{
return getBonuses(selector, limit, cachingStr)->size() > 0;
}
bool IBonusBearer::hasBonusOfType(Bonus::BonusType type, int subtype) const
{
//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);
CSelector s = Selector::type()(type);
if(subtype != -1)
s = s.And(Selector::subtype()(subtype));
return hasBonus(s, cachingStr);
}
TConstBonusListPtr IBonusBearer::getBonuses(const CSelector &selector, const std::string &cachingStr) const
{
return getAllBonuses(selector, nullptr, nullptr, cachingStr);
2010-04-03 06:33:46 +03:00
}
TConstBonusListPtr IBonusBearer::getBonuses(const CSelector &selector, const CSelector &limit, const std::string &cachingStr) const
2010-04-03 06:33:46 +03:00
{
return getAllBonuses(selector, limit, nullptr, cachingStr);
2010-04-03 06:33:46 +03:00
}
bool IBonusBearer::hasBonusFrom(Bonus::BonusSource source, ui32 sourceID) const
{
boost::format fmt("source_%did_%d");
2023-03-13 23:26:44 +02:00
fmt % static_cast<int>(source) % sourceID;
return hasBonus(Selector::source(source,sourceID), fmt.str());
}
int IBonusBearer::MoraleVal() const
2010-04-03 06:33:46 +03:00
{
2020-06-28 15:19:27 +02:00
if(anaffectedByMorale.getHasBonus())
return 0;
2020-06-28 15:19:27 +02:00
int ret = moraleValue.getValue();
2010-04-03 06:33:46 +03:00
return vstd::abetween(ret, -3, +3);
2010-04-03 06:33:46 +03:00
}
int IBonusBearer::LuckVal() const
2010-04-03 06:33:46 +03:00
{
if(hasBonusOfType(Bonus::NO_LUCK))
return 0;
int ret = luckValue.getValue();
return vstd::abetween(ret, -3, +3);
}
int IBonusBearer::MoraleValAndBonusList(TConstBonusListPtr & bonusList) const
{
if(anaffectedByMorale.getHasBonus())
{
if(!bonusList->empty())
bonusList = std::make_shared<const BonusList>();
return 0;
}
int ret = moraleValue.getValueAndList(bonusList);
return vstd::abetween(ret, -3, +3);
}
int IBonusBearer::LuckValAndBonusList(TConstBonusListPtr & bonusList) const
{
if(hasBonusOfType(Bonus::NO_LUCK))
{
if(!bonusList->empty())
bonusList = std::make_shared<const BonusList>();
return 0;
}
int ret = luckValue.getValueAndList(bonusList);
return vstd::abetween(ret, -3, +3);
2010-04-03 06:33:46 +03:00
}
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
ui32 IBonusBearer::MaxHealth() const
2010-04-03 06:33:46 +03:00
{
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
const std::string cachingStr = "type_STACK_HEALTH";
static const auto selector = Selector::type()(Bonus::STACK_HEALTH);
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
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
}
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
int IBonusBearer::getAttack(bool ranged) const
2010-04-03 06:33:46 +03:00
{
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
const std::string cachingStr = "type_PRIMARY_SKILLs_ATTACK";
2010-04-03 06:33:46 +03:00
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
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
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
return getBonuses(selector, nullptr, cachingStr)->totalValue();
2010-04-03 06:33:46 +03:00
}
int IBonusBearer::getDefense(bool ranged) const
2010-04-03 06:33:46 +03:00
{
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
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
}
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
int IBonusBearer::getMinDamage(bool ranged) const
{
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
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);
}
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
int IBonusBearer::getMaxDamage(bool ranged) const
{
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
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);
}
si32 IBonusBearer::manaLimit() const
{
return 0;
}
int IBonusBearer::getPrimSkillLevel(PrimarySkill::PrimarySkill id) const
{
static const CSelector selectorAllSkills = Selector::type()(Bonus::PRIMARY_SKILL);
static const std::string keyAllSkills = "type_PRIMARY_SKILL";
auto allSkills = getBonuses(selectorAllSkills, keyAllSkills);
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
return ret; //sp=0 works in old saves
}
si32 IBonusBearer::magicResistance() const
{
return valOfBonuses(Bonus::MAGIC_RESISTANCE);
}
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
ui32 IBonusBearer::Speed(int turn, bool useBind) const
2013-07-22 13:39:11 +03:00
{
//war machines cannot move
if(hasBonus(Selector::type()(Bonus::SIEGE_WEAPON).And(Selector::turns(turn))))
2013-07-22 13:39:11 +03:00
{
return 0;
2013-07-22 13:39:11 +03:00
}
//bind effect check - doesn't influence stack initiative
if(useBind && hasBonus(Selector::type()(Bonus::BIND_EFFECT).And(Selector::turns(turn))))
2013-07-22 13:39:11 +03:00
{
return 0;
}
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
bool IBonusBearer::isLiving() const //TODO: theoreticaly there exists "LIVING" bonus in stack experience documentation
{
static const std::string cachingStr = "IBonusBearer::isLiving";
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));
return !hasBonus(selector, cachingStr);
}
std::shared_ptr<const Bonus> IBonusBearer::getBonus(const CSelector &selector) const
{
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
auto bonuses = getAllBonuses(selector, Selector::all);
return bonuses->getFirst(Selector::all);
}
2021-03-23 16:47:07 +02:00
const CStack * retrieveStackBattle(const CBonusSystemNode * node)
{
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)
{
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)
{
return node ? node->getOwner() : PlayerColor::CANNOT_DETERMINE;
2021-03-23 16:47:07 +02:00
}
std::shared_ptr<Bonus> CBonusSystemNode::getBonusLocalFirst(const CSelector & selector)
{
auto ret = bonuses.getFirst(selector);
if(ret)
return ret;
FOREACH_PARENT(pname)
{
ret = pname->getBonusLocalFirst(selector);
if (ret)
return ret;
}
return nullptr;
}
2021-03-23 16:47:07 +02:00
std::shared_ptr<const Bonus> CBonusSystemNode::getBonusLocalFirst(const CSelector & selector) const
{
return (const_cast<CBonusSystemNode*>(this))->getBonusLocalFirst(selector);
}
void CBonusSystemNode::getParents(TCNodes & out) const /*retrieves list of parent nodes (nodes to inherit bonuses from) */
{
2023-03-13 23:26:44 +02:00
for(const auto & elem : parents)
{
const CBonusSystemNode *parent = elem;
out.insert(parent);
}
}
void CBonusSystemNode::getParents(TNodes &out)
{
for (auto & elem : parents)
{
const CBonusSystemNode *parent = elem;
out.insert(const_cast<CBonusSystemNode*>(parent));
}
}
2021-03-23 16:47:07 +02:00
void CBonusSystemNode::getAllParents(TCNodes & out) const //retrieves list of parent nodes (nodes to inherit bonuses from)
{
2023-03-13 23:26:44 +02:00
for(auto * parent : parents)
{
out.insert(parent);
parent->getAllParents(out);
}
}
void CBonusSystemNode::getAllBonusesRec(BonusList &out, const CSelector & selector) const
{
//out has been reserved sufficient capacity at getAllBonuses() call
BonusList beforeUpdate;
TCNodes lparents;
getAllParents(lparents);
2023-03-13 23:26:44 +02:00
if(!lparents.empty())
{
//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
}
2023-03-13 23:26:44 +02:00
for(const auto * parent : lparents)
{
parent->getAllBonusesRec(beforeUpdate, selector);
}
bonuses.getAllBonuses(beforeUpdate);
2022-09-14 11:37:12 +02:00
for(const auto & b : beforeUpdate)
{
//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)
: b;
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);
}
}
TConstBonusListPtr CBonusSystemNode::getAllBonuses(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root, const std::string &cachingStr) const
{
bool limitOnUs = (!root || root == this); //caching won't work when we want to limit bonuses against an external node
if (CBonusSystemNode::cachingEnabled && limitOnUs)
{
// Exclusive access for one thread
boost::lock_guard<boost::mutex> lock(sync);
// If the bonus system tree changes(state of a single node or the relations to each other) then
// cache all bonus objects. Selector objects doesn't matter.
if (cachedLast != treeChanged)
{
BonusList allBonuses;
allBonuses.reserve(cachedBonuses.capacity()); //we assume we'll get about the same number of bonuses
cachedBonuses.clear();
cachedRequests.clear();
getAllBonusesRec(allBonuses, Selector::all);
limitBonuses(allBonuses, cachedBonuses);
cachedBonuses.stackBonuses();
cachedLast = treeChanged;
}
// 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.
if(!cachingStr.empty())
{
auto it = cachedRequests.find(cachingStr);
if(it != cachedRequests.end())
{
//Cached list contains bonuses for our query with applied limiters
return it->second;
}
}
//We still don't have the bonuses (didn't returned them from cache)
//Perform bonus selection
auto ret = std::make_shared<BonusList>();
cachedBonuses.getBonuses(*ret, selector, limit);
// Save the results in the cache
if(!cachingStr.empty())
cachedRequests[cachingStr] = ret;
return ret;
}
else
{
return getAllBonusesWithoutCaching(selector, limit, root);
}
}
TConstBonusListPtr CBonusSystemNode::getAllBonusesWithoutCaching(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root) const
{
auto ret = std::make_shared<BonusList>();
// Get bonus results without caching enabled.
2023-03-13 23:26:44 +02:00
BonusList beforeLimiting;
BonusList afterLimiting;
getAllBonusesRec(beforeLimiting, selector);
if(!root || root == this)
{
limitBonuses(beforeLimiting, afterLimiting);
}
else if(root)
{
//We want to limit our query against an external node. We get all its bonuses,
// 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;
getAllBonusesRec(rootBonuses, selector);
2023-03-13 23:26:44 +02:00
for(const auto & b : beforeLimiting)
rootBonuses.push_back(b);
root->limitBonuses(rootBonuses, limitedRootBonuses);
2023-03-13 23:26:44 +02:00
for(const auto & b : beforeLimiting)
if(vstd::contains(limitedRootBonuses, b))
afterLimiting.push_back(b);
}
afterLimiting.getBonuses(*ret, selector, limit);
ret->stackBonuses();
return ret;
}
2023-03-13 23:26:44 +02:00
std::shared_ptr<Bonus> CBonusSystemNode::getUpdatedBonus(const std::shared_ptr<Bonus> & b, const TUpdaterPtr & updater) const
{
assert(updater);
return updater->createUpdatedBonus(b, * this);
}
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
CBonusSystemNode::CBonusSystemNode()
:CBonusSystemNode(false)
{
}
2023-03-13 23:26:44 +02:00
CBonusSystemNode::CBonusSystemNode(bool isHypotetic):
bonuses(true),
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
exportedBonuses(true),
nodeType(UNKNOWN),
cachedLast(0),
isHypotheticNode(isHypotetic)
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
{
}
2023-03-13 23:26:44 +02:00
CBonusSystemNode::CBonusSystemNode(ENodeTypes NodeType):
bonuses(true),
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
exportedBonuses(true),
nodeType(NodeType),
cachedLast(0),
isHypotheticNode(false)
{
}
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)),
nodeType(other.nodeType),
description(other.description),
cachedLast(0),
isHypotheticNode(other.isHypotheticNode)
{
std::swap(parents, other.parents);
std::swap(children, other.children);
2016-08-15 10:04:45 +02:00
//fixing bonus tree without recalculation
if(!isHypothetic())
2016-08-15 10:04:45 +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
//cachedBonuses
//cachedRequests
}
CBonusSystemNode::~CBonusSystemNode()
{
detachFromAll();
2023-03-13 23:26:44 +02:00
if(!children.empty())
{
2023-03-13 23:26:44 +02:00
while(!children.empty())
children.front()->detachFrom(*this);
}
}
void CBonusSystemNode::attachTo(CBonusSystemNode & parent)
{
assert(!vstd::contains(parents, &parent));
parents.push_back(&parent);
if(!isHypothetic())
{
if(parent.actsAsBonusSourceOnly())
parent.newRedDescendant(*this);
else
newRedDescendant(parent);
parent.newChildAttached(*this);
}
CBonusSystemNode::treeHasChanged();
}
void CBonusSystemNode::detachFrom(CBonusSystemNode & parent)
{
assert(vstd::contains(parents, &parent));
if(!isHypothetic())
{
if(parent.actsAsBonusSourceOnly())
parent.removedRedDescendant(*this);
else
removedRedDescendant(parent);
}
if (vstd::contains(parents, &parent))
2021-03-23 16:47:07 +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)"
, nodeShortInfo(), nodeType, parent.nodeShortInfo(), parent.nodeType);
2021-03-23 16:47:07 +02:00
}
if(!isHypothetic())
{
parent.childDetached(*this);
}
CBonusSystemNode::treeHasChanged();
}
void CBonusSystemNode::removeBonusesRecursive(const CSelector & s)
{
removeBonuses(s);
for(CBonusSystemNode * child : children)
child->removeBonusesRecursive(s);
}
void CBonusSystemNode::reduceBonusDurations(const CSelector &s)
{
BonusList bl;
exportedBonuses.getBonuses(bl, s, Selector::all);
2023-03-13 23:26:44 +02:00
for(const auto & b : bl)
{
b->turnsRemain--;
if(b->turnsRemain <= 0)
removeBonus(b);
}
for(CBonusSystemNode *child : children)
child->reduceBonusDurations(s);
}
void CBonusSystemNode::addNewBonus(const std::shared_ptr<Bonus>& b)
{
//turnsRemain shouldn't be zero for following durations
if(Bonus::NTurns(b.get()) || Bonus::NDays(b.get()) || Bonus::OneWeek(b.get()))
{
assert(b->turnsRemain);
}
assert(!vstd::contains(exportedBonuses, b));
2010-11-19 00:22:51 +02:00
exportedBonuses.push_back(b);
exportBonus(b);
CBonusSystemNode::treeHasChanged();
}
void CBonusSystemNode::accumulateBonus(const std::shared_ptr<Bonus>& b)
{
auto bonus = exportedBonuses.getFirst(Selector::typeSubtype(b->type, b->subtype)); //only local bonuses are interesting //TODO: what about value type?
if(bonus)
bonus->val += b->val;
else
addNewBonus(std::make_shared<Bonus>(*b)); //duplicate needed, original may get destroyed
}
void CBonusSystemNode::removeBonus(const std::shared_ptr<Bonus>& b)
{
2010-11-19 00:22:51 +02:00
exportedBonuses -= b;
2010-11-20 02:03:31 +02:00
if(b->propagator)
unpropagateBonus(b);
2010-11-20 02:03:31 +02:00
else
bonuses -= b;
CBonusSystemNode::treeHasChanged();
2010-11-20 02:03:31 +02:00
}
void CBonusSystemNode::removeBonuses(const CSelector & selector)
{
BonusList toRemove;
exportedBonuses.getBonuses(toRemove, selector, Selector::all);
2023-03-13 23:26:44 +02:00
for(const auto & bonus : toRemove)
removeBonus(bonus);
}
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
{
if(b->propagator->shouldBeAttached(this))
{
auto propagated = b->propagationUpdater
? source.getUpdatedBonus(b, b->propagationUpdater)
: b;
bonuses.push_back(propagated);
logBonus->trace("#$# %s #propagated to# %s", propagated->Description(), nodeName());
}
FOREACH_RED_CHILD(child)
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)
{
if(b->propagator->shouldBeAttached(this))
{
bonuses -= b;
logBonus->trace("#$# %s #is no longer propagated to# %s", b->Description(), nodeName());
}
FOREACH_RED_CHILD(child)
child->unpropagateBonus(b);
}
void CBonusSystemNode::newChildAttached(CBonusSystemNode & child)
{
assert(!vstd::contains(children, &child));
children.push_back(&child);
}
void CBonusSystemNode::childDetached(CBonusSystemNode & child)
{
if(vstd::contains(children, &child))
children -= &child;
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)"
, child.nodeShortInfo(), child.nodeType, nodeShortInfo(), nodeType);
}
}
void CBonusSystemNode::detachFromAll()
{
2023-03-13 23:26:44 +02:00
while(!parents.empty())
detachFrom(*parents.front());
}
bool CBonusSystemNode::isIndependentNode() const
{
return parents.empty() && children.empty();
}
std::string CBonusSystemNode::nodeName() const
{
2023-03-13 23:26:44 +02:00
return !description.empty()
? description
: std::string("Bonus system node of type ") + typeid(*this).name();
}
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();
}
void CBonusSystemNode::deserializationFix()
{
2011-03-01 12:19:05 +02:00
exportBonuses();
}
2021-03-23 16:47:07 +02:00
void CBonusSystemNode::getRedParents(TNodes & out)
{
FOREACH_PARENT(pname)
{
if(pname->actsAsBonusSourceOnly())
{
out.insert(pname);
}
}
if(!actsAsBonusSourceOnly())
{
for(CBonusSystemNode *child : children)
{
out.insert(child);
}
}
}
void CBonusSystemNode::getRedChildren(TNodes &out)
{
FOREACH_PARENT(pname)
{
if(!pname->actsAsBonusSourceOnly())
{
out.insert(pname);
}
}
if(actsAsBonusSourceOnly())
{
for(CBonusSystemNode *child : children)
{
out.insert(child);
}
}
}
void CBonusSystemNode::newRedDescendant(CBonusSystemNode & descendant)
{
2023-03-13 23:26:44 +02:00
for(const auto & b : exportedBonuses)
2021-03-23 16:47:07 +02:00
{
if(b->propagator)
descendant.propagateBonus(b, *this);
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-03-23 16:47:07 +02:00
{
if(b->propagator)
descendant.propagateBonus(b, *this);
2021-03-23 16:47:07 +02:00
}
}
}
void CBonusSystemNode::removedRedDescendant(CBonusSystemNode & descendant)
{
2023-03-13 23:26:44 +02:00
for(const auto & b : exportedBonuses)
if(b->propagator)
descendant.unpropagateBonus(b);
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)
if(b->propagator)
descendant.unpropagateBonus(b);
2021-03-23 16:47:07 +02:00
}
}
void CBonusSystemNode::getRedAncestors(TNodes &out)
{
getRedParents(out);
2021-03-23 16:47:07 +02:00
TNodes redParents;
getRedParents(redParents);
2021-03-23 16:47:07 +02:00
for(CBonusSystemNode * parent : redParents)
parent->getRedAncestors(out);
}
void CBonusSystemNode::getRedDescendants(TNodes &out)
{
getRedChildren(out);
FOREACH_RED_CHILD(c)
c->getRedChildren(out);
}
2023-03-13 23:26:44 +02:00
void CBonusSystemNode::exportBonus(const std::shared_ptr<Bonus> & b)
{
if(b->propagator)
propagateBonus(b, *this);
else
bonuses.push_back(b);
CBonusSystemNode::treeHasChanged();
}
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);
}
CBonusSystemNode::ENodeTypes CBonusSystemNode::getNodeType() const
{
return nodeType;
}
const BonusList& CBonusSystemNode::getBonusList() const
{
return bonuses;
}
const TNodesVector& CBonusSystemNode::getParentNodes() const
{
return parents;
}
const TNodesVector& CBonusSystemNode::getChildrenNodes() const
{
return children;
}
void CBonusSystemNode::setNodeType(CBonusSystemNode::ENodeTypes type)
{
nodeType = type;
}
BonusList & CBonusSystemNode::getExportedBonusList()
{
return exportedBonuses;
}
const BonusList & CBonusSystemNode::getExportedBonusList() const
{
return exportedBonuses;
}
const std::string& CBonusSystemNode::getDescription() const
{
return description;
}
void CBonusSystemNode::setDescription(const std::string &description)
{
this->description = description;
}
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;
while(true)
{
int undecidedCount = static_cast<int>(undecided.size());
for(int i = 0; i < undecided.size(); i++)
{
auto b = undecided[i];
BonusLimitationContext context = {b, *this, out, undecided};
2013-01-20 15:06:18 +03:00
int decision = b->limiter ? b->limiter->limit(context) : ILimiter::ACCEPT; //bonuses without limiters will be accepted by default
if(decision == ILimiter::DISCARD)
{
undecided.erase(i);
i--; continue;
}
else if(decision == ILimiter::ACCEPT)
{
accepted.push_back(b);
undecided.erase(i);
i--; continue;
}
else
assert(decision == ILimiter::NOT_SURE);
}
if(undecided.size() == undecidedCount) //we haven't moved a single bonus -> limiters reached a stable state
return;
}
}
TBonusListPtr CBonusSystemNode::limitBonuses(const BonusList &allBonuses) const
{
auto ret = std::make_shared<BonusList>();
limitBonuses(allBonuses, *ret);
return ret;
}
void CBonusSystemNode::treeHasChanged()
{
treeChanged++;
}
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
int64_t CBonusSystemNode::getTreeVersion() const
{
int64_t ret = treeChanged;
return ret << 32;
}
int NBonus::valOf(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype)
2010-04-03 06:33:46 +03:00
{
if(obj)
return obj->valOfBonuses(type, subtype);
return 0;
}
bool NBonus::hasOfType(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype)
2010-04-03 06:33:46 +03:00
{
if(obj)
return obj->hasBonusOfType(type, subtype);
return false;
}
std::string Bonus::Description(boost::optional<si32> customValue) const
{
std::ostringstream str;
2016-08-18 14:03:20 +02:00
if(description.empty())
{
if(stacking.empty() || stacking == "ALWAYS")
{
switch(source)
{
case ARTIFACT:
str << ArtifactID(sid).toArtifact(VLC->artifacts())->getNameTranslated();
break;
case SPELL_EFFECT:
str << SpellID(sid).toSpell(VLC->spells())->getNameTranslated();
break;
case CREATURE_ABILITY:
str << VLC->creh->objects[sid]->getNamePluralTranslated();
break;
case SECONDARY_SKILL:
str << VLC->skillh->getByIndex(sid)->getNameTranslated();
break;
case HERO_SPECIAL:
str << VLC->heroh->objects[sid]->getNameTranslated();
break;
default:
//todo: handle all possible sources
str << "Unknown";
break;
}
}
else
str << stacking;
}
else
{
str << description;
}
2016-08-18 14:03:20 +02:00
if(auto value = customValue.value_or(val))
str << " " << std::showpos << value;
2016-08-18 14:03:20 +02:00
return str.str();
}
JsonNode subtypeToJson(Bonus::BonusType type, int subtype)
{
switch(type)
{
case Bonus::PRIMARY_SKILL:
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:
case Bonus::SPELL:
2017-09-12 10:56:59 +02:00
case Bonus::SPECIAL_PECULIAR_ENCHANT:
case Bonus::SPECIAL_ADD_VALUE_ENCHANT:
case Bonus::SPECIAL_FIXED_VALUE_ENCHANT:
return JsonUtils::stringNode(CModHandler::makeFullIdentifier("", "spell", SpellID::encode(subtype)));
case Bonus::IMPROVED_NECROMANCY:
2017-09-12 10:56:59 +02:00
case Bonus::SPECIAL_UPGRADE:
return JsonUtils::stringNode(CModHandler::makeFullIdentifier("", "creature", CreatureID::encode(subtype)));
2017-09-12 10:56:59 +02:00
case Bonus::GENERATE_RESOURCE:
return JsonUtils::stringNode("resource." + GameConstants::RESOURCE_NAMES[subtype]);
default:
2017-09-12 10:56:59 +02:00
return JsonUtils::intNode(subtype);
}
2017-09-12 10:56:59 +02:00
}
JsonNode additionalInfoToJson(Bonus::BonusType type, CAddInfo addInfo)
2017-09-12 10:56:59 +02:00
{
switch(type)
{
case Bonus::SPECIAL_UPGRADE:
return JsonUtils::stringNode(CModHandler::makeFullIdentifier("", "creature", CreatureID::encode(addInfo[0])));
2017-09-12 10:56:59 +02:00
default:
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)
node.Vector().push_back(JsonUtils::stringNode(dur));
return node;
2017-09-12 10:56:59 +02:00
}
}
JsonNode Bonus::toJsonNode() const
{
2017-09-12 10:56:59 +02:00
JsonNode root(JsonNode::JsonType::DATA_STRUCT);
// only add values that might reasonably be found in config files
root["type"].String() = vstd::findKey(bonusNameMap, type);
if(subtype != -1)
root["subtype"] = subtypeToJson(type, subtype);
if(additionalInfo != CAddInfo::NONE)
2017-09-12 10:56:59 +02:00
root["addInfo"] = additionalInfoToJson(type, additionalInfo);
if(duration != 0)
{
JsonNode durationVec(JsonNode::JsonType::DATA_VECTOR);
2023-03-13 23:26:44 +02:00
for(const auto & kv : bonusDurationMap)
{
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)
root["sourceType"].String() = vstd::findKey(bonusSourceMap, source);
if(targetSourceType != OTHER)
root["targetSourceType"].String() = vstd::findKey(bonusSourceMap, targetSourceType);
if(sid != 0)
root["sourceID"].Integer() = sid;
if(val != 0)
root["val"].Integer() = val;
if(valType != ADDITIVE_VALUE)
root["valueType"].String() = vstd::findKey(bonusValueMap, valType);
if(!stacking.empty())
root["stacking"].String() = stacking;
if(!description.empty())
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;
if(limiter)
root["limiters"] = limiter->toJsonNode();
if(updater)
root["updater"] = updater->toJsonNode();
if(propagator)
root["propagator"].String() = vstd::findKey(bonusPropagatorMap, propagator);
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:
case Bonus::SPELL:
2017-09-15 00:48:34 +02:00
case Bonus::SPECIAL_PECULIAR_ENCHANT:
case Bonus::SPECIAL_ADD_VALUE_ENCHANT:
case Bonus::SPECIAL_FIXED_VALUE_ENCHANT:
2023-03-13 23:26:44 +02:00
return (*VLC->spellh)[static_cast<SpellID::ESpellID>(subtype)]->identifier;
2017-09-15 00:48:34 +02:00
case Bonus::SPECIAL_UPGRADE:
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);
}
}
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;
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;
subtype = Res::GOLD;
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;
else if (deprecatedTypeStr == "SELF_MORALE")
{
type = Bonus::MORALE;
val = 1;
valRelevant = true;
valueType = Bonus::INDEPENDENT_MAX;
valueTypeRelevant = true;
}
else if (deprecatedTypeStr == "SELF_LUCK")
{
type = Bonus::LUCK;
val = 1;
valRelevant = true;
valueType = Bonus::INDEPENDENT_MAX;
valueTypeRelevant = true;
}
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;
};
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))
{
boost::algorithm::trim(description);
targetSourceType = OTHER;
}
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)
{
turnsRemain = 0;
effectRange = NO_LIMIT;
targetSourceType = OTHER;
}
2023-03-13 23:26:44 +02:00
std::shared_ptr<Bonus> Bonus::addPropagator(const TPropagatorPtr & Propagator)
{
propagator = Propagator;
return this->shared_from_this();
}
namespace Selector
{
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;
}
DLL_LINKAGE CSelectFieldEqual<Bonus::BonusSource> & targetSourceType()
{
static CSelectFieldEqual<Bonus::BonusSource> ssourceType(&Bonus::targetSourceType);
return ssourceType;
}
DLL_LINKAGE CSelectFieldEqual<Bonus::LimitEffect> & effectRange()
{
static CSelectFieldEqual<Bonus::LimitEffect> seffectRange(&Bonus::effectRange);
return seffectRange;
}
DLL_LINKAGE CWillLastTurns turns;
DLL_LINKAGE CWillLastDays days;
2013-02-16 17:03:47 +03:00
CSelector DLL_LINKAGE typeSubtype(Bonus::BonusType Type, TBonusSubtype Subtype)
{
return type()(Type).And(subtype()(Subtype));
}
2023-03-13 23:26:44 +02:00
CSelector DLL_LINKAGE typeSubtypeInfo(Bonus::BonusType type, TBonusSubtype subtype, const CAddInfo & info)
{
return CSelectFieldEqual<Bonus::BonusType>(&Bonus::type)(type)
.And(CSelectFieldEqual<TBonusSubtype>(&Bonus::subtype)(subtype))
.And(CSelectFieldEqual<CAddInfo>(&Bonus::additionalInfo)(info));
}
CSelector DLL_LINKAGE source(Bonus::BonusSource source, ui32 sourceID)
{
return CSelectFieldEqual<Bonus::BonusSource>(&Bonus::source)(source)
.And(CSelectFieldEqual<ui32>(&Bonus::sid)(sourceID));
}
CSelector DLL_LINKAGE sourceTypeSel(Bonus::BonusSource source)
{
return CSelectFieldEqual<Bonus::BonusSource>(&Bonus::source)(source);
}
CSelector DLL_LINKAGE valueType(Bonus::ValueType valType)
{
return CSelectFieldEqual<Bonus::ValueType>(&Bonus::valType)(valType);
}
DLL_LINKAGE CSelector all([](const Bonus * b){return true;});
DLL_LINKAGE CSelector none([](const Bonus * b){return false;});
2013-02-16 17:03:47 +03:00
bool DLL_LINKAGE matchesType(const CSelector &sel, Bonus::BonusType type)
{
Bonus dummy;
dummy.type = type;
return sel(&dummy);
}
2013-02-16 17:03:47 +03:00
bool DLL_LINKAGE matchesTypeSubtype(const CSelector &sel, Bonus::BonusType type, TBonusSubtype subtype)
{
Bonus dummy;
dummy.type = type;
dummy.subtype = subtype;
return sel(&dummy);
}
}
2010-11-20 02:03:31 +02:00
const CCreature * retrieveCreature(const CBonusSystemNode *node)
{
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));
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:
const CStackInstance * csi = retrieveStackInstance(node);
if(csi)
return csi->type;
return nullptr;
2010-11-20 02:03:31 +02:00
}
}
DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const BonusList &bonusList)
{
for (ui32 i = 0; i < bonusList.size(); i++)
{
2023-03-13 23:26:44 +02:00
const auto & b = bonusList[i];
out << "Bonus " << i << "\n" << *b << std::endl;
}
return out;
}
DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const Bonus &bonus)
{
2023-03-13 23:26:44 +02:00
for(const auto & i : bonusNameMap)
if(i.second == bonus.type)
out << "\tType: " << i.first << " \t";
#define printField(field) out << "\t" #field ": " << (int)bonus.field << "\n"
printField(val);
printField(subtype);
printField(duration);
printField(source);
printField(sid);
if(bonus.additionalInfo != CAddInfo::NONE)
out << "\taddInfo: " << bonus.additionalInfo.toString() << "\n";
printField(turnsRemain);
printField(valType);
if(!bonus.stacking.empty())
out << "\tstacking: \"" << bonus.stacking << "\"\n";
printField(effectRange);
#undef printField
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";
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-20 15:06:18 +03:00
if (limiter)
{
//If we already have limiter list, retrieve it
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
limiterList = std::make_shared<AllOfLimiter>();
2013-01-20 15:06:18 +03:00
limiterList->add(limiter);
limiter = limiterList;
}
limiterList->add(Limiter);
}
else
{
limiter = Limiter;
}
return this->shared_from_this();
}
int ILimiter::limit(const BonusLimitationContext &context) const /*return true to drop the bonus */
{
return false;
}
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);
root["type"].String() = toString();
return root;
}
int CCreatureTypeLimiter::limit(const BonusLimitationContext &context) const
{
2023-01-14 19:05:36 +02:00
const CCreature *c = retrieveCreature(&context.node);
if(!c)
return true;
return c->getId() != creature->getId() && (!includeUpgrades || !creature->isMyUpgrade(c));
//drop bonus if it's not our creature and (we don`t check upgrades or its not our upgrade)
}
CCreatureTypeLimiter::CCreatureTypeLimiter(const CCreature & creature_, bool IncludeUpgrades)
: creature(&creature_), includeUpgrades(IncludeUpgrades)
{
}
2023-03-13 23:26:44 +02:00
void CCreatureTypeLimiter::setCreature(const CreatureID & id)
2013-01-18 14:49:15 +03:00
{
creature = VLC->creh->objects[id];
2013-01-18 14:49:15 +03:00
}
std::string CCreatureTypeLimiter::toString() const
{
boost::format fmt("CCreatureTypeLimiter(creature=%s, includeUpgrades=%s)");
fmt % creature->getJsonKey() % (includeUpgrades ? "true" : "false");
return fmt.str();
}
JsonNode CCreatureTypeLimiter::toJsonNode() const
{
2017-09-12 10:56:59 +02:00
JsonNode root(JsonNode::JsonType::DATA_STRUCT);
root["type"].String() = "CREATURE_TYPE_LIMITER";
root["parameters"].Vector().push_back(JsonUtils::stringNode(creature->getJsonKey()));
root["parameters"].Vector().push_back(JsonUtils::boolNode(includeUpgrades));
return root;
}
2013-02-16 17:03:47 +03:00
HasAnotherBonusLimiter::HasAnotherBonusLimiter( Bonus::BonusType bonus )
: type(bonus), subtype(0), isSubtypeRelevant(false), isSourceRelevant(false), isSourceIDRelevant(false)
{
}
2013-02-16 17:03:47 +03:00
HasAnotherBonusLimiter::HasAnotherBonusLimiter( Bonus::BonusType bonus, TBonusSubtype _subtype )
: 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)
{
}
int HasAnotherBonusLimiter::limit(const BonusLimitationContext &context) const
{
//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));
//if we have a bonus of required type accepted, limiter should accept also this bonus
if(context.alreadyAccepted.getFirst(mySelector))
return ACCEPT;
//if there are no matching bonuses pending, we can (and must) reject right away
if(!context.stillUndecided.getFirst(mySelector))
return DISCARD;
//do not accept for now but it may change if more bonuses gets included
return NOT_SURE;
}
2010-11-19 00:22:51 +02:00
std::string HasAnotherBonusLimiter::toString() const
{
std::string typeName = vstd::findKey(bonusNameMap, type);
if(isSubtypeRelevant)
{
boost::format fmt("HasAnotherBonusLimiter(type=%s, subtype=%d)");
fmt % typeName % subtype;
return fmt.str();
}
else
{
boost::format fmt("HasAnotherBonusLimiter(type=%s)");
fmt % typeName;
return fmt.str();
}
}
JsonNode HasAnotherBonusLimiter::toJsonNode() const
{
JsonNode root(JsonNode::JsonType::DATA_STRUCT);
std::string typeName = vstd::findKey(bonusNameMap, type);
auto sourceTypeName = vstd::findKey(bonusSourceMap, source);
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));
if(isSourceRelevant)
root["parameters"].Vector().push_back(JsonUtils::stringNode(sourceTypeName));
return root;
}
bool IPropagator::shouldBeAttached(CBonusSystemNode *dest)
{
return false;
}
CBonusSystemNode::ENodeTypes IPropagator::getPropagatorType() const
{
return CBonusSystemNode::ENodeTypes::NONE;
}
CPropagatorNodeType::CPropagatorNodeType()
:nodeType(CBonusSystemNode::ENodeTypes::UNKNOWN)
{
}
CPropagatorNodeType::CPropagatorNodeType(CBonusSystemNode::ENodeTypes NodeType)
: nodeType(NodeType)
2010-11-20 02:03:31 +02:00
{
}
CBonusSystemNode::ENodeTypes CPropagatorNodeType::getPropagatorType() const
{
return nodeType;
}
bool CPropagatorNodeType::shouldBeAttached(CBonusSystemNode *dest)
{
return nodeType == dest->getNodeType();
2010-11-20 02:03:31 +02:00
}
CreatureTerrainLimiter::CreatureTerrainLimiter()
: 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
}
int CreatureTerrainLimiter::limit(const BonusLimitationContext &context) const
2010-11-20 02:03:31 +02:00
{
const CStack *stack = retrieveStackBattle(&context.node);
if(stack)
{
if (terrainType == ETerrainId::NATIVE_TERRAIN)//terrainType not specified = native
2022-09-21 11:34:23 +02:00
{
return !stack->isOnNativeTerrain();
2022-09-21 11:34:23 +02:00
}
else
{
return !stack->isOnTerrain(terrainType);
}
}
return true;
//TODO neutral creatues
2010-11-20 02:03:31 +02:00
}
std::string CreatureTerrainLimiter::toString() const
{
boost::format fmt("CreatureTerrainLimiter(terrainType=%s)");
auto terrainName = VLC->terrainTypeHandler->getById(terrainType)->getJsonKey();
fmt % (terrainType == ETerrainId::NATIVE_TERRAIN ? "native" : terrainName);
return fmt.str();
}
JsonNode CreatureTerrainLimiter::toJsonNode() const
{
JsonNode root(JsonNode::JsonType::DATA_STRUCT);
root["type"].String() = "CREATURE_TERRAIN_LIMITER";
auto terrainName = VLC->terrainTypeHandler->getById(terrainType)->getJsonKey();
2022-09-21 11:34:23 +02:00
root["parameters"].Vector().push_back(JsonUtils::stringNode(terrainName));
return root;
}
2020-12-10 03:05:37 +02:00
CreatureFactionLimiter::CreatureFactionLimiter(TFaction creatureFaction)
: faction(creatureFaction)
2010-11-20 02:03:31 +02:00
{
}
2023-03-13 23:26:44 +02:00
CreatureFactionLimiter::CreatureFactionLimiter():
faction(static_cast<TFaction>(-1))
2010-11-20 02:03:31 +02:00
{
}
int CreatureFactionLimiter::limit(const BonusLimitationContext &context) const
2010-11-20 02:03:31 +02:00
{
const CCreature *c = retrieveCreature(&context.node);
2010-11-20 02:03:31 +02:00
return !c || c->faction != faction; //drop bonus for non-creatures or non-native residents
}
std::string CreatureFactionLimiter::toString() const
{
boost::format fmt("CreatureFactionLimiter(faction=%s)");
fmt % VLC->factions()->getByIndex(faction)->getJsonKey();
return fmt.str();
}
JsonNode CreatureFactionLimiter::toJsonNode() const
{
JsonNode root(JsonNode::JsonType::DATA_STRUCT);
root["type"].String() = "CREATURE_FACTION_LIMITER";
root["parameters"].Vector().push_back(JsonUtils::stringNode(VLC->factions()->getByIndex(faction)->getJsonKey()));
return root;
}
2010-11-20 02:03:31 +02:00
CreatureAlignmentLimiter::CreatureAlignmentLimiter()
2016-11-27 16:48:18 +02:00
: alignment(-1)
2010-11-20 02:03:31 +02:00
{
}
CreatureAlignmentLimiter::CreatureAlignmentLimiter(si8 Alignment)
: alignment(Alignment)
{
}
int CreatureAlignmentLimiter::limit(const BonusLimitationContext &context) const
2010-11-20 02:03:31 +02:00
{
const CCreature *c = retrieveCreature(&context.node);
if(!c)
2010-11-20 02:03:31 +02:00
return true;
switch(alignment)
{
case EAlignment::GOOD:
2010-11-20 02:03:31 +02:00
return !c->isGood(); //if not good -> return true (drop bonus)
case EAlignment::NEUTRAL:
2010-11-20 02:03:31 +02:00
return c->isEvil() || c->isGood();
case EAlignment::EVIL:
2010-11-20 02:03:31 +02:00
return !c->isEvil();
default:
2017-08-10 18:39:27 +02:00
logBonus->warn("Warning: illegal alignment in limiter!");
return true;
2010-11-20 02:03:31 +02:00
}
}
std::string CreatureAlignmentLimiter::toString() const
{
boost::format fmt("CreatureAlignmentLimiter(alignment=%s)");
fmt % EAlignment::names[alignment];
return fmt.str();
}
JsonNode CreatureAlignmentLimiter::toJsonNode() const
{
JsonNode root(JsonNode::JsonType::DATA_STRUCT);
root["type"].String() = "CREATURE_ALIGNMENT_LIMITER";
root["parameters"].Vector().push_back(JsonUtils::stringNode(EAlignment::names[alignment]));
return root;
}
RankRangeLimiter::RankRangeLimiter(ui8 Min, ui8 Max)
:minRank(Min), maxRank(Max)
{
}
RankRangeLimiter::RankRangeLimiter()
{
minRank = maxRank = -1;
}
int RankRangeLimiter::limit(const BonusLimitationContext &context) const
{
const CStackInstance * csi = retrieveStackInstance(&context.node);
if(csi)
{
if (csi->getNodeType() == CBonusSystemNode::COMMANDER) //no stack exp bonuses for commander creatures
return true;
return csi->getExpRank() < minRank || csi->getExpRank() > maxRank;
}
return true;
}
int StackOwnerLimiter::limit(const BonusLimitationContext &context) const
{
const CStack * s = retrieveStackBattle(&context.node);
if(s)
return s->owner != owner;
const CStackInstance * csi = retrieveStackInstance(&context.node);
if(csi && csi->armyObj)
return csi->armyObj->tempOwner != owner;
return true;
}
StackOwnerLimiter::StackOwnerLimiter()
: owner(-1)
{
}
2023-03-13 23:26:44 +02:00
StackOwnerLimiter::StackOwnerLimiter(const PlayerColor & Owner):
owner(Owner)
{
}
2013-01-20 15:06:18 +03:00
2023-03-13 23:26:44 +02:00
OppositeSideLimiter::OppositeSideLimiter():
owner(PlayerColor::CANNOT_DETERMINE)
2021-03-23 16:47:07 +02:00
{
}
2023-03-13 23:26:44 +02:00
OppositeSideLimiter::OppositeSideLimiter(const PlayerColor & Owner):
owner(Owner)
2021-03-23 16:47:07 +02:00
{
}
int OppositeSideLimiter::limit(const BonusLimitationContext & context) const
{
auto contextOwner = CBonusSystemNode::retrieveNodeOwner(& context.node);
auto decision = (owner == contextOwner || owner == PlayerColor::CANNOT_DETERMINE) ? ILimiter::DISCARD : ILimiter::ACCEPT;
return decision;
}
// Aggregate/Boolean Limiters
2023-03-13 23:26:44 +02:00
void AggregateLimiter::add(const TLimiterPtr & limiter)
{
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)
result.Vector().push_back(l->toJsonNode());
return result;
}
const std::string AllOfLimiter::aggregator = "allOf";
const std::string & AllOfLimiter::getAggregator() const
{
return aggregator;
}
int 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);
if(result == ILimiter::DISCARD)
return result;
if(result == ILimiter::NOT_SURE)
wasntSure = true;
}
return wasntSure ? ILimiter::NOT_SURE : ILimiter::ACCEPT;
}
const std::string AnyOfLimiter::aggregator = "anyOf";
const std::string & AnyOfLimiter::getAggregator() const
2013-01-20 15:06:18 +03:00
{
return aggregator;
}
int AnyOfLimiter::limit(const BonusLimitationContext & context) const
{
bool wasntSure = false;
2023-03-13 23:26:44 +02:00
for(const auto & limiter : limiters)
{
auto result = limiter->limit(context);
if(result == ILimiter::ACCEPT)
return result;
if(result == ILimiter::NOT_SURE)
wasntSure = true;
}
return wasntSure ? ILimiter::NOT_SURE : ILimiter::DISCARD;
}
const std::string NoneOfLimiter::aggregator = "noneOf";
const std::string & NoneOfLimiter::getAggregator() const
{
return aggregator;
}
int NoneOfLimiter::limit(const BonusLimitationContext & context) const
{
bool wasntSure = false;
2023-03-13 23:26:44 +02:00
for(const auto & limiter : limiters)
{
auto result = limiter->limit(context);
if(result == ILimiter::ACCEPT)
return ILimiter::DISCARD;
if(result == ILimiter::NOT_SURE)
wasntSure = true;
}
return wasntSure ? ILimiter::NOT_SURE : ILimiter::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)
{
updater = Updater;
return this->shared_from_this();
}
std::shared_ptr<Bonus> IUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
{
return b;
}
2017-09-11 02:36:02 +02:00
std::string IUpdater::toString() const
{
return typeid(*this).name();
}
JsonNode IUpdater::toJsonNode() const
2017-09-10 05:20:47 +02:00
{
return JsonNode(JsonNode::JsonType::DATA_NULL);
2017-09-10 05:20:47 +02:00
}
GrowsWithLevelUpdater::GrowsWithLevelUpdater(int valPer20, int stepSize) : valPer20(valPer20), stepSize(stepSize)
{
}
std::shared_ptr<Bonus> GrowsWithLevelUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
2017-09-09 07:43:53 +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;
//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
}
return b;
2017-09-09 07:43:53 +02:00
}
std::string GrowsWithLevelUpdater::toString() const
2017-09-11 02:36:02 +02:00
{
return boost::str(boost::format("GrowsWithLevelUpdater(valPer20=%d, stepSize=%d)") % valPer20 % stepSize);
2017-09-11 02:36:02 +02:00
}
JsonNode GrowsWithLevelUpdater::toJsonNode() const
{
2017-09-12 10:56:59 +02:00
JsonNode root(JsonNode::JsonType::DATA_STRUCT);
root["type"].String() = "GROWS_WITH_LEVEL";
root["parameters"].Vector().push_back(JsonUtils::intNode(valPer20));
if(stepSize > 1)
root["parameters"].Vector().push_back(JsonUtils::intNode(stepSize));
return root;
}
std::shared_ptr<Bonus> TimesHeroLevelUpdater::createUpdatedBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & context) const
{
if(context.getNodeType() == CBonusSystemNode::HERO)
{
2023-03-13 23:26:44 +02:00
int level = dynamic_cast<const CGHeroInstance &>(context).level;
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
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)
{
}
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)
{
auto speed = static_cast<const CGHeroInstance &>(context).getLowestCreatureSpeed();
si32 armySpeed = speed * base / divider;
auto counted = armySpeed * multiplier;
auto newBonus = std::make_shared<Bonus>(*b);
newBonus->source = Bonus::ARMY;
newBonus->val += vstd::amin(counted, max);
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
{
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;
}
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)
{
int level = stack.type->level;
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");
}
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);
updated->limiter = std::make_shared<OppositeSideLimiter>(owner);
return updated;
}
VCMI_LIB_NAMESPACE_END