UnitEffect.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 "../../NetPacksBase.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(std::set<BattleHex> & 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, true, _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. {
  39. MetaString text;
  40. text.addTxt(MetaString::GENERAL_TXT, 185);
  41. problem.add(std::move(text), Problem::NORMAL);
  42. return false;
  43. }
  44. return true;
  45. }
  46. bool UnitEffect::applicable(Problem & problem, const Mechanics * m, const EffectTarget & target) const
  47. {
  48. //stack effect is applicable if it affects at least one smart target
  49. //assume target correctly transformed, just reapply smart filter
  50. for(const auto & item : target)
  51. if(item.unitValue)
  52. if(getStackFilter(m, true, item.unitValue))
  53. return true;
  54. return false;
  55. }
  56. bool UnitEffect::getStackFilter(const Mechanics * m, bool alwaysSmart, const battle::Unit * s) const
  57. {
  58. return isValidTarget(m, s) && isSmartTarget(m, s, alwaysSmart);
  59. }
  60. bool UnitEffect::eraseByImmunityFilter(const Mechanics * m, const battle::Unit * s) const
  61. {
  62. return !isReceptive(m, s);
  63. }
  64. EffectTarget UnitEffect::filterTarget(const Mechanics * m, const EffectTarget & target) const
  65. {
  66. EffectTarget res;
  67. vstd::copy_if(target, std::back_inserter(res), [this, m](const Destination & d)
  68. {
  69. return d.unitValue && isValidTarget(m, d.unitValue) && isReceptive(m, d.unitValue);
  70. });
  71. return res;
  72. }
  73. EffectTarget UnitEffect::transformTarget(const Mechanics * m, const Target & aimPoint, const Target & spellTarget) const
  74. {
  75. if(chainLength > 1)
  76. return transformTargetByChain(m, aimPoint, spellTarget);
  77. else
  78. return transformTargetByRange(m, aimPoint, spellTarget);
  79. }
  80. EffectTarget UnitEffect::transformTargetByRange(const Mechanics * m, const Target & aimPoint, const Target & spellTarget) const
  81. {
  82. auto mainFilter = std::bind(&UnitEffect::getStackFilter, this, m, false, _1);
  83. Target spellTargetCopy(spellTarget);
  84. // make sure that we have valid target with valid aim, even if spell have invalid range configured
  85. // TODO: check than spell range is actually not valid
  86. // also hackfix for banned creature massive spells
  87. // FIXME: potentially breaking change: aimPoint may NOT be in Target - example: frost ring
  88. if(!aimPoint.empty() && spellTarget.empty())
  89. spellTargetCopy.insert(spellTargetCopy.begin(), Destination(aimPoint.front()));
  90. std::set<const battle::Unit *> targets;
  91. if(m->isMassive())
  92. {
  93. //ignore spellTarget and add all stacks
  94. auto units = m->battle()->battleGetUnitsIf(mainFilter);
  95. for(const auto *unit : units)
  96. targets.insert(unit);
  97. }
  98. else
  99. {
  100. //process each tile
  101. for(const Destination & dest : spellTargetCopy)
  102. {
  103. if(dest.unitValue)
  104. {
  105. if(mainFilter(dest.unitValue))
  106. targets.insert(dest.unitValue);
  107. }
  108. else if(dest.hexValue.isValid())
  109. {
  110. //select one unit on tile, prefer alive one
  111. const battle::Unit * targetOnTile = nullptr;
  112. auto predicate = [&](const battle::Unit * unit)
  113. {
  114. return unit->coversPos(dest.hexValue) && mainFilter(unit);
  115. };
  116. auto units = m->battle()->battleGetUnitsIf(predicate);
  117. for(const auto *unit : units)
  118. {
  119. if(unit->alive())
  120. {
  121. targetOnTile = unit;
  122. break;
  123. }
  124. }
  125. if(targetOnTile == nullptr && !units.empty())
  126. targetOnTile = units.front();
  127. if(targetOnTile)
  128. targets.insert(targetOnTile);
  129. }
  130. else
  131. {
  132. logGlobal->debug("Invalid destination in spell Target");
  133. }
  134. }
  135. }
  136. auto predicate = std::bind(&UnitEffect::eraseByImmunityFilter, this, m, _1);
  137. vstd::erase_if(targets, predicate);
  138. if(m->alwaysHitFirstTarget())
  139. {
  140. if(!aimPoint.empty() && aimPoint.front().unitValue)
  141. targets.insert(aimPoint.front().unitValue);
  142. else
  143. logGlobal->error("Spell-like attack with no primary target.");
  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. std::set<BattleHex> possibleHexes;
  163. auto possibleTargets = m->battle()->battleGetUnitsIf([&](const battle::Unit * unit) -> bool
  164. {
  165. return isValidTarget(m, unit);
  166. });
  167. for(const auto *unit : possibleTargets)
  168. {
  169. for(auto hex : battle::Unit::getHexes(unit->getPosition(), unit->doubleWide(), unit->unitSide()))
  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. if(m->alwaysHitFirstTarget() && targetIndex == 0)
  180. effectTarget.emplace_back(unit);
  181. else if(isReceptive(m, unit) && isValidTarget(m, unit))
  182. effectTarget.emplace_back(unit);
  183. else
  184. effectTarget.emplace_back();
  185. for(auto hex : battle::Unit::getHexes(unit->getPosition(), unit->doubleWide(), unit->unitSide()))
  186. possibleHexes.erase(hex);
  187. if(possibleHexes.empty())
  188. break;
  189. destHex = BattleHex::getClosestTile(unit->unitSide(), destHex, possibleHexes);
  190. }
  191. return effectTarget;
  192. }
  193. bool UnitEffect::isValidTarget(const Mechanics * m, const battle::Unit * unit) const
  194. {
  195. // TODO: override in rising effect
  196. // TODO: check absolute immunity here
  197. return unit->isValidTarget(false);
  198. }
  199. bool UnitEffect::isReceptive(const Mechanics * m, const battle::Unit * unit) const
  200. {
  201. if(ignoreImmunity)
  202. {
  203. //ignore all immunities, except specific absolute immunity(VCMI addition)
  204. //SPELL_IMMUNITY absolute case
  205. std::stringstream cachingStr;
  206. cachingStr << "type_" << Bonus::SPELL_IMMUNITY << "subtype_" << m->getSpellIndex() << "addInfo_1";
  207. return !unit->hasBonus(Selector::typeSubtypeInfo(Bonus::SPELL_IMMUNITY, m->getSpellIndex(), 1), cachingStr.str());
  208. }
  209. else
  210. {
  211. return m->isReceptive(unit);
  212. }
  213. }
  214. bool UnitEffect::isSmartTarget(const Mechanics * m, const battle::Unit * unit, bool alwaysSmart) const
  215. {
  216. const bool smart = m->isSmart() || alwaysSmart;
  217. const bool ignoreOwner = !smart;
  218. return ignoreOwner || m->ownerMatches(unit);
  219. }
  220. void UnitEffect::serializeJsonEffect(JsonSerializeFormat & handler)
  221. {
  222. handler.serializeBool("ignoreImmunity", ignoreImmunity);
  223. handler.serializeInt("chainLength", chainLength, 0);
  224. handler.serializeFloat("chainFactor", chainFactor, 0);
  225. serializeJsonUnitEffect(handler);
  226. }
  227. }
  228. }
  229. VCMI_LIB_NAMESPACE_END