UnitEffect.cpp 7.3 KB

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