2023-04-05 17:56:28 +02:00
|
|
|
/*
|
|
|
|
* BasicTypes.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"
|
2023-04-09 18:30:29 +02:00
|
|
|
|
|
|
|
#include "VCMI_Lib.h"
|
2023-04-05 17:56:28 +02:00
|
|
|
#include "GameConstants.h"
|
2023-05-02 00:05:59 +02:00
|
|
|
#include "GameSettings.h"
|
2023-05-05 23:45:09 +02:00
|
|
|
#include "JsonNode.h"
|
2023-04-30 19:34:25 +02:00
|
|
|
#include "bonuses/BonusList.h"
|
2023-05-01 19:29:53 +02:00
|
|
|
#include "bonuses/Bonus.h"
|
2023-04-30 18:13:55 +02:00
|
|
|
#include "bonuses/IBonusBearer.h"
|
2023-04-05 17:56:28 +02:00
|
|
|
|
2023-04-27 15:10:33 +02:00
|
|
|
#include <vcmi/Creature.h>
|
2023-04-09 18:30:29 +02:00
|
|
|
#include <vcmi/Faction.h>
|
2023-04-27 15:10:33 +02:00
|
|
|
#include <vcmi/FactionMember.h>
|
2023-04-09 18:30:29 +02:00
|
|
|
#include <vcmi/FactionService.h>
|
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
2023-04-05 17:56:28 +02:00
|
|
|
|
2023-04-09 18:30:29 +02:00
|
|
|
bool INativeTerrainProvider::isNativeTerrain(TerrainId terrain) const
|
2023-04-05 17:56:28 +02:00
|
|
|
{
|
|
|
|
auto native = getNativeTerrain();
|
|
|
|
return native == terrain || native == ETerrainId::ANY_TERRAIN;
|
2023-04-09 18:30:29 +02:00
|
|
|
}
|
|
|
|
|
2023-04-30 17:21:02 +02:00
|
|
|
TerrainId AFactionMember::getNativeTerrain() const
|
2023-04-09 18:30:29 +02:00
|
|
|
{
|
|
|
|
constexpr auto any = TerrainId(ETerrainId::ANY_TERRAIN);
|
|
|
|
const std::string cachingStringNoTerrainPenalty = "type_NO_TERRAIN_PENALTY_sANY";
|
2023-05-01 00:20:01 +02:00
|
|
|
static const auto selectorNoTerrainPenalty = Selector::typeSubtype(BonusType::NO_TERRAIN_PENALTY, any);
|
2023-04-09 18:30:29 +02:00
|
|
|
|
|
|
|
//this code is used in the CreatureTerrainLimiter::limit to setup battle bonuses
|
|
|
|
//and in the CGHeroInstance::getNativeTerrain() to setup movement bonuses or/and penalties.
|
|
|
|
return getBonusBearer()->hasBonus(selectorNoTerrainPenalty, cachingStringNoTerrainPenalty)
|
|
|
|
? any : VLC->factions()->getById(getFaction())->getNativeTerrain();
|
|
|
|
}
|
|
|
|
|
2023-04-30 17:21:02 +02:00
|
|
|
int32_t AFactionMember::magicResistance() const
|
2023-04-26 22:40:21 +02:00
|
|
|
{
|
2023-05-01 00:20:01 +02:00
|
|
|
si32 val = getBonusBearer()->valOfBonuses(Selector::type()(BonusType::MAGIC_RESISTANCE));
|
2023-04-26 22:40:21 +02:00
|
|
|
vstd::amin (val, 100);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2023-04-30 17:21:02 +02:00
|
|
|
int AFactionMember::getAttack(bool ranged) const
|
2023-04-26 23:11:04 +02:00
|
|
|
{
|
|
|
|
const std::string cachingStr = "type_PRIMARY_SKILLs_ATTACK";
|
|
|
|
|
2023-08-19 20:43:50 +02:00
|
|
|
static const auto selector = Selector::typeSubtype(BonusType::PRIMARY_SKILL, static_cast<int>(PrimarySkill::ATTACK));
|
2023-04-26 23:11:04 +02:00
|
|
|
|
|
|
|
return getBonusBearer()->valOfBonuses(selector, cachingStr);
|
|
|
|
}
|
|
|
|
|
2023-04-30 17:21:02 +02:00
|
|
|
int AFactionMember::getDefense(bool ranged) const
|
2023-04-26 23:11:04 +02:00
|
|
|
{
|
|
|
|
const std::string cachingStr = "type_PRIMARY_SKILLs_DEFENSE";
|
|
|
|
|
2023-08-19 20:43:50 +02:00
|
|
|
static const auto selector = Selector::typeSubtype(BonusType::PRIMARY_SKILL, static_cast<int>(PrimarySkill::DEFENSE));
|
2023-04-26 23:11:04 +02:00
|
|
|
|
|
|
|
return getBonusBearer()->valOfBonuses(selector, cachingStr);
|
|
|
|
}
|
|
|
|
|
2023-04-30 17:21:02 +02:00
|
|
|
int AFactionMember::getMinDamage(bool ranged) const
|
2023-04-26 23:11:04 +02:00
|
|
|
{
|
|
|
|
const std::string cachingStr = "type_CREATURE_DAMAGEs_0Otype_CREATURE_DAMAGEs_1";
|
2023-05-01 00:20:01 +02:00
|
|
|
static const auto selector = Selector::typeSubtype(BonusType::CREATURE_DAMAGE, 0).Or(Selector::typeSubtype(BonusType::CREATURE_DAMAGE, 1));
|
2023-04-26 23:11:04 +02:00
|
|
|
return getBonusBearer()->valOfBonuses(selector, cachingStr);
|
|
|
|
}
|
|
|
|
|
2023-04-30 17:21:02 +02:00
|
|
|
int AFactionMember::getMaxDamage(bool ranged) const
|
2023-04-26 23:11:04 +02:00
|
|
|
{
|
|
|
|
const std::string cachingStr = "type_CREATURE_DAMAGEs_0Otype_CREATURE_DAMAGEs_2";
|
2023-05-01 00:20:01 +02:00
|
|
|
static const auto selector = Selector::typeSubtype(BonusType::CREATURE_DAMAGE, 0).Or(Selector::typeSubtype(BonusType::CREATURE_DAMAGE, 2));
|
2023-04-26 23:11:04 +02:00
|
|
|
return getBonusBearer()->valOfBonuses(selector, cachingStr);
|
|
|
|
}
|
|
|
|
|
2023-08-19 20:43:50 +02:00
|
|
|
int AFactionMember::getPrimSkillLevel(PrimarySkill id) const
|
2023-04-27 15:10:33 +02:00
|
|
|
{
|
2023-05-01 00:20:01 +02:00
|
|
|
static const CSelector selectorAllSkills = Selector::type()(BonusType::PRIMARY_SKILL);
|
2023-04-27 15:10:33 +02:00
|
|
|
static const std::string keyAllSkills = "type_PRIMARY_SKILL";
|
|
|
|
auto allSkills = getBonusBearer()->getBonuses(selectorAllSkills, keyAllSkills);
|
2023-08-19 20:43:50 +02:00
|
|
|
auto ret = allSkills->valOfBonuses(Selector::subtype()(static_cast<int>(id)));
|
2023-04-27 15:10:33 +02:00
|
|
|
auto minSkillValue = (id == PrimarySkill::SPELL_POWER || id == PrimarySkill::KNOWLEDGE) ? 1 : 0;
|
|
|
|
return std::max(ret, minSkillValue); //otherwise, some artifacts may cause negative skill value effect, sp=0 works in old saves
|
|
|
|
}
|
|
|
|
|
2023-05-02 00:05:59 +02:00
|
|
|
int AFactionMember::moraleValAndBonusList(TConstBonusListPtr & bonusList) const
|
2023-04-30 14:04:54 +02:00
|
|
|
{
|
2023-05-01 00:20:01 +02:00
|
|
|
static const auto unaffectedByMoraleSelector = Selector::type()(BonusType::NON_LIVING).Or(Selector::type()(BonusType::UNDEAD))
|
|
|
|
.Or(Selector::type()(BonusType::SIEGE_WEAPON)).Or(Selector::type()(BonusType::NO_MORALE));
|
2023-04-30 14:04:54 +02:00
|
|
|
|
2023-04-30 17:21:02 +02:00
|
|
|
static const std::string cachingStrUn = "AFactionMember::unaffectedByMoraleSelector";
|
2023-04-30 14:04:54 +02:00
|
|
|
auto unaffected = getBonusBearer()->hasBonus(unaffectedByMoraleSelector, cachingStrUn);
|
|
|
|
if(unaffected)
|
|
|
|
{
|
|
|
|
if(bonusList && !bonusList->empty())
|
|
|
|
bonusList = std::make_shared<const BonusList>();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-01 00:20:01 +02:00
|
|
|
static const auto moraleSelector = Selector::type()(BonusType::MORALE);
|
2023-04-30 14:04:54 +02:00
|
|
|
static const std::string cachingStrMor = "type_MORALE";
|
|
|
|
bonusList = getBonusBearer()->getBonuses(moraleSelector, cachingStrMor);
|
|
|
|
|
2023-05-02 00:05:59 +02:00
|
|
|
int32_t maxGoodMorale = VLC->settings()->getVector(EGameSettings::COMBAT_GOOD_MORALE_DICE).size();
|
|
|
|
int32_t maxBadMorale = -VLC->settings()->getVector(EGameSettings::COMBAT_BAD_MORALE_DICE).size();
|
|
|
|
|
|
|
|
return std::clamp(bonusList->totalValue(), maxBadMorale, maxGoodMorale);
|
2023-04-30 14:04:54 +02:00
|
|
|
}
|
|
|
|
|
2023-05-02 00:05:59 +02:00
|
|
|
int AFactionMember::luckValAndBonusList(TConstBonusListPtr & bonusList) const
|
2023-04-30 14:04:54 +02:00
|
|
|
{
|
2023-05-01 00:20:01 +02:00
|
|
|
if(getBonusBearer()->hasBonusOfType(BonusType::NO_LUCK))
|
2023-04-30 14:04:54 +02:00
|
|
|
{
|
|
|
|
if(bonusList && !bonusList->empty())
|
|
|
|
bonusList = std::make_shared<const BonusList>();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-01 00:20:01 +02:00
|
|
|
static const auto luckSelector = Selector::type()(BonusType::LUCK);
|
2023-04-30 14:04:54 +02:00
|
|
|
static const std::string cachingStrLuck = "type_LUCK";
|
|
|
|
bonusList = getBonusBearer()->getBonuses(luckSelector, cachingStrLuck);
|
|
|
|
|
2023-05-02 00:05:59 +02:00
|
|
|
int32_t maxGoodLuck = VLC->settings()->getVector(EGameSettings::COMBAT_GOOD_LUCK_DICE).size();
|
|
|
|
int32_t maxBadLuck = -VLC->settings()->getVector(EGameSettings::COMBAT_BAD_LUCK_DICE).size();
|
|
|
|
|
|
|
|
return std::clamp(bonusList->totalValue(), maxBadLuck, maxGoodLuck);
|
2023-04-30 14:04:54 +02:00
|
|
|
}
|
|
|
|
|
2023-05-02 00:05:59 +02:00
|
|
|
int AFactionMember::moraleVal() const
|
2023-04-30 14:04:54 +02:00
|
|
|
{
|
|
|
|
TConstBonusListPtr tmp = nullptr;
|
2023-05-02 00:05:59 +02:00
|
|
|
return moraleValAndBonusList(tmp);
|
2023-04-30 14:04:54 +02:00
|
|
|
}
|
|
|
|
|
2023-05-02 00:05:59 +02:00
|
|
|
int AFactionMember::luckVal() const
|
2023-04-30 14:04:54 +02:00
|
|
|
{
|
|
|
|
TConstBonusListPtr tmp = nullptr;
|
2023-05-02 00:05:59 +02:00
|
|
|
return luckValAndBonusList(tmp);
|
2023-04-30 14:04:54 +02:00
|
|
|
}
|
|
|
|
|
2023-05-01 19:29:53 +02:00
|
|
|
ui32 ACreature::getMaxHealth() const
|
2023-04-26 23:11:04 +02:00
|
|
|
{
|
|
|
|
const std::string cachingStr = "type_STACK_HEALTH";
|
2023-05-01 00:20:01 +02:00
|
|
|
static const auto selector = Selector::type()(BonusType::STACK_HEALTH);
|
2023-04-26 23:11:04 +02:00
|
|
|
auto value = getBonusBearer()->valOfBonuses(selector, cachingStr);
|
|
|
|
return std::max(1, value); //never 0
|
|
|
|
}
|
|
|
|
|
2023-05-02 00:05:59 +02:00
|
|
|
ui32 ACreature::speed(int turn, bool useBind) const
|
2023-04-26 23:11:04 +02:00
|
|
|
{
|
|
|
|
//war machines cannot move
|
2023-05-01 00:20:01 +02:00
|
|
|
if(getBonusBearer()->hasBonus(Selector::type()(BonusType::SIEGE_WEAPON).And(Selector::turns(turn))))
|
2023-04-26 23:11:04 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
//bind effect check - doesn't influence stack initiative
|
2023-05-01 00:20:01 +02:00
|
|
|
if(useBind && getBonusBearer()->hasBonus(Selector::type()(BonusType::BIND_EFFECT).And(Selector::turns(turn))))
|
2023-04-26 23:11:04 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-01 00:20:01 +02:00
|
|
|
return getBonusBearer()->valOfBonuses(Selector::type()(BonusType::STACKS_SPEED).And(Selector::turns(turn)));
|
2023-04-26 23:11:04 +02:00
|
|
|
}
|
2023-04-27 15:10:33 +02:00
|
|
|
|
2023-04-30 17:21:02 +02:00
|
|
|
bool ACreature::isLiving() const //TODO: theoreticaly there exists "LIVING" bonus in stack experience documentation
|
2023-04-27 15:10:33 +02:00
|
|
|
{
|
2023-04-30 17:21:02 +02:00
|
|
|
static const std::string cachingStr = "ACreature::isLiving";
|
2023-05-01 00:20:01 +02:00
|
|
|
static const CSelector selector = Selector::type()(BonusType::UNDEAD)
|
|
|
|
.Or(Selector::type()(BonusType::NON_LIVING))
|
|
|
|
.Or(Selector::type()(BonusType::GARGOYLE))
|
|
|
|
.Or(Selector::type()(BonusType::SIEGE_WEAPON));
|
2023-04-27 15:10:33 +02:00
|
|
|
|
|
|
|
return !getBonusBearer()->hasBonus(selector, cachingStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-19 20:43:50 +02:00
|
|
|
VCMI_LIB_NAMESPACE_END
|