UnitEffect.cpp 7.5 KB

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