UnitEffect.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * UnitEffect.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 "UnitEffect.h"
  12. #include "../ISpellMechanics.h"
  13. #include "../../bonuses/BonusSelector.h"
  14. #include "../../battle/CBattleInfoCallback.h"
  15. #include "../../battle/Unit.h"
  16. #include "../../serializer/JsonSerializeFormat.h"
  17. VCMI_LIB_NAMESPACE_BEGIN
  18. namespace spells
  19. {
  20. namespace effects
  21. {
  22. void UnitEffect::adjustTargetTypes(std::vector<TargetType> & types) const
  23. {
  24. }
  25. void UnitEffect::adjustAffectedHexes(BattleHexArray & hexes, const Mechanics * m, const Target & spellTarget) const
  26. {
  27. for(const auto & destnation : spellTarget)
  28. hexes.insert(destnation.hexValue);
  29. }
  30. bool UnitEffect::applicable(Problem & problem, const Mechanics * m) const
  31. {
  32. //stack effect is applicable in general if there is at least one smart target
  33. auto mainFilter = std::bind(&UnitEffect::getStackFilter, this, m, false, _1);
  34. auto predicate = std::bind(&UnitEffect::eraseByImmunityFilter, this, m, _1);
  35. auto targets = m->battle()->battleGetUnitsIf(mainFilter);
  36. vstd::erase_if(targets, predicate);
  37. if(targets.empty())
  38. return m->adaptProblem(ESpellCastProblem::NO_APPROPRIATE_TARGET, problem);
  39. return true;
  40. }
  41. bool UnitEffect::applicable(Problem & problem, const Mechanics * m, const EffectTarget & target) const
  42. {
  43. //stack effect is applicable if it affects at least one target (smartness should not be checked)
  44. //assume target correctly transformed, just reapply filter
  45. for(const auto & item : target)
  46. if(item.unitValue)
  47. if(getStackFilter(m, false, item.unitValue))
  48. return true;
  49. return false;
  50. }
  51. bool UnitEffect::getStackFilter(const Mechanics * m, bool alwaysSmart, const battle::Unit * s) const
  52. {
  53. return isValidTarget(m, s) && isSmartTarget(m, s, alwaysSmart);
  54. }
  55. bool UnitEffect::eraseByImmunityFilter(const Mechanics * m, const battle::Unit * s) const
  56. {
  57. return !isReceptive(m, s);
  58. }
  59. EffectTarget UnitEffect::filterTarget(const Mechanics * m, const EffectTarget & target) const
  60. {
  61. EffectTarget res;
  62. vstd::copy_if(target, std::back_inserter(res), [this, m](const Destination & d)
  63. {
  64. return d.unitValue && isValidTarget(m, d.unitValue) && isReceptive(m, d.unitValue);
  65. });
  66. return res;
  67. }
  68. EffectTarget UnitEffect::transformTarget(const Mechanics * m, const Target & aimPoint, const Target & spellTarget) const
  69. {
  70. if(chainLength > 1)
  71. return transformTargetByChain(m, aimPoint, spellTarget);
  72. else
  73. return transformTargetByRange(m, aimPoint, spellTarget);
  74. }
  75. EffectTarget UnitEffect::transformTargetByRange(const Mechanics * m, const Target & aimPoint, const Target & spellTarget) const
  76. {
  77. auto mainFilter = std::bind(&UnitEffect::getStackFilter, this, m, false, _1);
  78. Target spellTargetCopy(spellTarget);
  79. // make sure that we have valid target with valid aim, even if spell have invalid range configured
  80. // TODO: check than spell range is actually not valid
  81. // also hackfix for banned creature massive spells
  82. // FIXME: potentially breaking change: aimPoint may NOT be in Target - example: frost ring
  83. if(!aimPoint.empty() && spellTarget.empty())
  84. spellTargetCopy.insert(spellTargetCopy.begin(), Destination(aimPoint.front()));
  85. std::set<const battle::Unit *> targets;
  86. if(m->isMassive())
  87. {
  88. //ignore spellTarget and add all stacks
  89. auto units = m->battle()->battleGetUnitsIf(mainFilter);
  90. for(const auto *unit : units)
  91. targets.insert(unit);
  92. }
  93. else
  94. {
  95. //process each tile
  96. for(const Destination & dest : spellTargetCopy)
  97. {
  98. if(dest.unitValue)
  99. {
  100. if(mainFilter(dest.unitValue))
  101. targets.insert(dest.unitValue);
  102. }
  103. else if(dest.hexValue.isValid())
  104. {
  105. //select one unit on tile, prefer alive one
  106. const battle::Unit * targetOnTile = nullptr;
  107. auto predicate = [&](const battle::Unit * unit)
  108. {
  109. return unit->coversPos(dest.hexValue) && mainFilter(unit);
  110. };
  111. auto units = m->battle()->battleGetUnitsIf(predicate);
  112. for(const auto *unit : units)
  113. {
  114. if(unit->alive())
  115. {
  116. targetOnTile = unit;
  117. break;
  118. }
  119. }
  120. if(targetOnTile == nullptr && !units.empty())
  121. targetOnTile = units.front();
  122. if(targetOnTile)
  123. targets.insert(targetOnTile);
  124. }
  125. else
  126. {
  127. logGlobal->debug("Invalid destination in spell Target");
  128. }
  129. }
  130. }
  131. auto predicate = std::bind(&UnitEffect::eraseByImmunityFilter, this, m, _1);
  132. vstd::erase_if(targets, predicate);
  133. if(m->alwaysHitFirstTarget())
  134. {
  135. //TODO: examine if adjustments needed related to INVINCIBLE bonus
  136. if(!aimPoint.empty() && aimPoint.front().unitValue)
  137. targets.insert(aimPoint.front().unitValue);
  138. }
  139. EffectTarget effectTarget;
  140. for(const auto *s : targets)
  141. effectTarget.push_back(Destination(s));
  142. return effectTarget;
  143. }
  144. EffectTarget UnitEffect::transformTargetByChain(const Mechanics * m, const Target & aimPoint, const Target & spellTarget) const
  145. {
  146. EffectTarget byRange = transformTargetByRange(m, aimPoint, spellTarget);
  147. if(byRange.empty())
  148. {
  149. return EffectTarget();
  150. }
  151. const Destination & mainDestination = byRange.front();
  152. if(!mainDestination.hexValue.isValid())
  153. {
  154. return EffectTarget();
  155. }
  156. BattleHexArray possibleHexes;
  157. auto possibleTargets = m->battle()->battleGetUnitsIf([&](const battle::Unit * unit) -> bool
  158. {
  159. return isReceptive(m, unit) && isValidTarget(m, unit);
  160. });
  161. for(const auto *unit : possibleTargets)
  162. {
  163. for(auto hex : battle::Unit::getHexes(unit->getPosition(), unit->doubleWide(), unit->unitSide()))
  164. possibleHexes.insert(hex);
  165. }
  166. BattleHex destHex = mainDestination.hexValue;
  167. EffectTarget effectTarget;
  168. for(int32_t targetIndex = 0; targetIndex < chainLength; ++targetIndex)
  169. {
  170. const auto *unit = m->battle()->battleGetUnitByPos(destHex, true);
  171. if(!unit)
  172. break;
  173. if(m->alwaysHitFirstTarget() && targetIndex == 0)
  174. effectTarget.emplace_back(unit);
  175. else if(isReceptive(m, unit) && isValidTarget(m, unit))
  176. effectTarget.emplace_back(unit);
  177. else
  178. effectTarget.emplace_back();
  179. for(auto hex : battle::Unit::getHexes(unit->getPosition(), unit->doubleWide(), unit->unitSide()))
  180. possibleHexes.erase(hex.toInt());
  181. if(possibleHexes.empty())
  182. break;
  183. destHex = BattleHex::getClosestTile(unit->unitSide(), destHex, possibleHexes);
  184. }
  185. return effectTarget;
  186. }
  187. bool UnitEffect::isValidTarget(const Mechanics * m, const battle::Unit * unit) const
  188. {
  189. // TODO: override in rising effect
  190. // TODO: check absolute immunity here
  191. return unit->isValidTarget(false);
  192. }
  193. bool UnitEffect::isReceptive(const Mechanics * m, const battle::Unit * unit) const
  194. {
  195. if(ignoreImmunity)
  196. {
  197. //ignore all immunities, except specific absolute immunity(VCMI addition)
  198. //SPELL_IMMUNITY absolute case
  199. std::stringstream cachingStr;
  200. cachingStr << "type_" << vstd::to_underlying(BonusType::SPELL_IMMUNITY) << "subtype_" << m->getSpellIndex() << "addInfo_1";
  201. return !unit->hasBonus(Selector::typeSubtypeInfo(BonusType::SPELL_IMMUNITY, BonusSubtypeID(m->getSpellId()), 1), cachingStr.str());
  202. }
  203. else
  204. {
  205. return m->isReceptive(unit);
  206. }
  207. }
  208. bool UnitEffect::isSmartTarget(const Mechanics * m, const battle::Unit * unit, bool alwaysSmart) const
  209. {
  210. const bool smart = m->isSmart() || alwaysSmart;
  211. const bool ignoreOwner = !smart;
  212. return ignoreOwner || m->ownerMatches(unit);
  213. }
  214. void UnitEffect::serializeJsonEffect(JsonSerializeFormat & handler)
  215. {
  216. handler.serializeBool("ignoreImmunity", ignoreImmunity);
  217. handler.serializeInt("chainLength", chainLength, 0);
  218. handler.serializeFloat("chainFactor", chainFactor, 0);
  219. serializeJsonUnitEffect(handler);
  220. }
  221. }
  222. }
  223. VCMI_LIB_NAMESPACE_END