BasicTypes.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * BasicTypes.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "VCMI_Lib.h"
  12. #include "GameConstants.h"
  13. #include "bonuses/HeroBonus.h"
  14. #include <vcmi/Creature.h>
  15. #include <vcmi/Faction.h>
  16. #include <vcmi/FactionMember.h>
  17. #include <vcmi/FactionService.h>
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. bool INativeTerrainProvider::isNativeTerrain(TerrainId terrain) const
  20. {
  21. auto native = getNativeTerrain();
  22. return native == terrain || native == ETerrainId::ANY_TERRAIN;
  23. }
  24. TerrainId IFactionMember::getNativeTerrain() const
  25. {
  26. constexpr auto any = TerrainId(ETerrainId::ANY_TERRAIN);
  27. const std::string cachingStringNoTerrainPenalty = "type_NO_TERRAIN_PENALTY_sANY";
  28. static const auto selectorNoTerrainPenalty = Selector::typeSubtype(Bonus::NO_TERRAIN_PENALTY, any);
  29. //this code is used in the CreatureTerrainLimiter::limit to setup battle bonuses
  30. //and in the CGHeroInstance::getNativeTerrain() to setup movement bonuses or/and penalties.
  31. return getBonusBearer()->hasBonus(selectorNoTerrainPenalty, cachingStringNoTerrainPenalty)
  32. ? any : VLC->factions()->getById(getFaction())->getNativeTerrain();
  33. }
  34. int32_t IFactionMember::magicResistance() const
  35. {
  36. si32 val = getBonusBearer()->valOfBonuses(Selector::type()(Bonus::MAGIC_RESISTANCE));
  37. vstd::amin (val, 100);
  38. return val;
  39. }
  40. int IFactionMember::getAttack(bool ranged) const
  41. {
  42. const std::string cachingStr = "type_PRIMARY_SKILLs_ATTACK";
  43. static const auto selector = Selector::typeSubtype(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK);
  44. return getBonusBearer()->valOfBonuses(selector, cachingStr);
  45. }
  46. int IFactionMember::getDefense(bool ranged) const
  47. {
  48. const std::string cachingStr = "type_PRIMARY_SKILLs_DEFENSE";
  49. static const auto selector = Selector::typeSubtype(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE);
  50. return getBonusBearer()->valOfBonuses(selector, cachingStr);
  51. }
  52. int IFactionMember::getMinDamage(bool ranged) const
  53. {
  54. const std::string cachingStr = "type_CREATURE_DAMAGEs_0Otype_CREATURE_DAMAGEs_1";
  55. static const auto selector = Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 0).Or(Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 1));
  56. return getBonusBearer()->valOfBonuses(selector, cachingStr);
  57. }
  58. int IFactionMember::getMaxDamage(bool ranged) const
  59. {
  60. const std::string cachingStr = "type_CREATURE_DAMAGEs_0Otype_CREATURE_DAMAGEs_2";
  61. static const auto selector = Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 0).Or(Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 2));
  62. return getBonusBearer()->valOfBonuses(selector, cachingStr);
  63. }
  64. int IFactionMember::getPrimSkillLevel(PrimarySkill::PrimarySkill id) const
  65. {
  66. static const CSelector selectorAllSkills = Selector::type()(Bonus::PRIMARY_SKILL);
  67. static const std::string keyAllSkills = "type_PRIMARY_SKILL";
  68. auto allSkills = getBonusBearer()->getBonuses(selectorAllSkills, keyAllSkills);
  69. auto ret = allSkills->valOfBonuses(Selector::subtype()(id));
  70. auto minSkillValue = (id == PrimarySkill::SPELL_POWER || id == PrimarySkill::KNOWLEDGE) ? 1 : 0;
  71. return std::max(ret, minSkillValue); //otherwise, some artifacts may cause negative skill value effect, sp=0 works in old saves
  72. }
  73. ui32 ICreature::MaxHealth() const
  74. {
  75. const std::string cachingStr = "type_STACK_HEALTH";
  76. static const auto selector = Selector::type()(Bonus::STACK_HEALTH);
  77. auto value = getBonusBearer()->valOfBonuses(selector, cachingStr);
  78. return std::max(1, value); //never 0
  79. }
  80. ui32 ICreature::Speed(int turn, bool useBind) const
  81. {
  82. //war machines cannot move
  83. if(getBonusBearer()->hasBonus(Selector::type()(Bonus::SIEGE_WEAPON).And(Selector::turns(turn))))
  84. {
  85. return 0;
  86. }
  87. //bind effect check - doesn't influence stack initiative
  88. if(useBind && getBonusBearer()->hasBonus(Selector::type()(Bonus::BIND_EFFECT).And(Selector::turns(turn))))
  89. {
  90. return 0;
  91. }
  92. return getBonusBearer()->valOfBonuses(Selector::type()(Bonus::STACKS_SPEED).And(Selector::turns(turn)));
  93. }
  94. bool ICreature::isLiving() const //TODO: theoreticaly there exists "LIVING" bonus in stack experience documentation
  95. {
  96. static const std::string cachingStr = "IBonusBearer::isLiving";
  97. static const CSelector selector = Selector::type()(Bonus::UNDEAD)
  98. .Or(Selector::type()(Bonus::NON_LIVING))
  99. .Or(Selector::type()(Bonus::GARGOYLE))
  100. .Or(Selector::type()(Bonus::SIEGE_WEAPON));
  101. return !getBonusBearer()->hasBonus(selector, cachingStr);
  102. }
  103. VCMI_LIB_NAMESPACE_END