UnitEffect.cpp 8.0 KB

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