UnitEffect.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. if(!d.unitValue)
  77. return false;
  78. if(!isValidTarget(m, d.unitValue))
  79. return false;
  80. if(!isReceptive(m, d.unitValue))
  81. return false;
  82. return true;
  83. });
  84. return res;
  85. }
  86. EffectTarget UnitEffect::transformTarget(const Mechanics * m, const Target & aimPoint, const Target & spellTarget) const
  87. {
  88. if(chainLength > 1)
  89. return transformTargetByChain(m, aimPoint, spellTarget);
  90. else
  91. return transformTargetByRange(m, aimPoint, spellTarget);
  92. }
  93. EffectTarget UnitEffect::transformTargetByRange(const Mechanics * m, const Target & aimPoint, const Target & spellTarget) const
  94. {
  95. auto mainFilter = std::bind(&UnitEffect::getStackFilter, this, m, false, _1);
  96. Target spellTargetCopy(spellTarget);
  97. //make sure that we have valid target with valid aim, even if spell have invalid range configured
  98. //TODO: check than spell range is actually not valid
  99. //also hackfix for banned creature massive spells
  100. if(!aimPoint.empty())
  101. spellTargetCopy.insert(spellTargetCopy.begin(), Destination(aimPoint.front()));
  102. std::set<const battle::Unit *> targets;
  103. if(m->isMassive())
  104. {
  105. //ignore spellTarget and add all stacks
  106. auto units = m->cb->battleGetUnitsIf(mainFilter);
  107. for(auto unit : units)
  108. targets.insert(unit);
  109. }
  110. else
  111. {
  112. //process each tile
  113. for(const Destination & dest : spellTargetCopy)
  114. {
  115. if(dest.unitValue)
  116. {
  117. if(mainFilter(dest.unitValue))
  118. targets.insert(dest.unitValue);
  119. }
  120. else if(dest.hexValue.isValid())
  121. {
  122. //select one unit on tile, prefer alive one
  123. const battle::Unit * targetOnTile = nullptr;
  124. auto predicate = [&](const battle::Unit * unit)
  125. {
  126. return unit->coversPos(dest.hexValue) && mainFilter(unit);
  127. };
  128. auto units = m->cb->battleGetUnitsIf(predicate);
  129. for(auto unit : units)
  130. {
  131. if(unit->alive())
  132. {
  133. targetOnTile = unit;
  134. break;
  135. }
  136. }
  137. if(targetOnTile == nullptr && !units.empty())
  138. targetOnTile = units.front();
  139. if(targetOnTile)
  140. targets.insert(targetOnTile);
  141. }
  142. else
  143. {
  144. logGlobal->debug("Invalid destination in spell Target");
  145. }
  146. }
  147. }
  148. auto predicate = std::bind(&UnitEffect::eraseByImmunityFilter, this, m, _1);
  149. vstd::erase_if(targets, predicate);
  150. if(m->alwaysHitFirstTarget())
  151. {
  152. if(!aimPoint.empty() && aimPoint.front().unitValue)
  153. targets.insert(aimPoint.front().unitValue);
  154. else
  155. logGlobal->error("Spell-like attack with no primary target.");
  156. }
  157. EffectTarget effectTarget;
  158. for(auto s : targets)
  159. effectTarget.push_back(Destination(s));
  160. return effectTarget;
  161. }
  162. EffectTarget UnitEffect::transformTargetByChain(const Mechanics * m, const Target & aimPoint, const Target & spellTarget) const
  163. {
  164. EffectTarget byRange = transformTargetByRange(m, aimPoint, spellTarget);
  165. if(byRange.empty())
  166. {
  167. return EffectTarget();
  168. }
  169. const Destination & mainDestination = byRange.front();
  170. if(!mainDestination.hexValue.isValid())
  171. {
  172. return EffectTarget();
  173. }
  174. std::set<BattleHex> possibleHexes;
  175. auto possibleTargets = m->cb->battleGetUnitsIf([&](const battle::Unit * unit) -> bool
  176. {
  177. return isValidTarget(m, unit);
  178. });
  179. for(auto unit : possibleTargets)
  180. {
  181. for(auto hex : battle::Unit::getHexes(unit->getPosition(), unit->doubleWide(), unit->unitSide()))
  182. possibleHexes.insert(hex);
  183. }
  184. BattleHex destHex = mainDestination.hexValue;
  185. EffectTarget effectTarget;
  186. for(int32_t targetIndex = 0; targetIndex < chainLength; ++targetIndex)
  187. {
  188. auto unit = m->cb->battleGetUnitByPos(destHex, true);
  189. if(!unit)
  190. break;
  191. if(m->alwaysHitFirstTarget() && targetIndex == 0)
  192. effectTarget.emplace_back(unit);
  193. else if(isReceptive(m, unit) && isValidTarget(m, unit))
  194. effectTarget.emplace_back(unit);
  195. else
  196. effectTarget.emplace_back();
  197. for(auto hex : battle::Unit::getHexes(unit->getPosition(), unit->doubleWide(), unit->unitSide()))
  198. possibleHexes.erase(hex);
  199. if(possibleHexes.empty())
  200. break;
  201. destHex = BattleHex::getClosestTile(unit->unitSide(), destHex, possibleHexes);
  202. }
  203. return effectTarget;
  204. }
  205. bool UnitEffect::isValidTarget(const Mechanics * m, const battle::Unit * unit) const
  206. {
  207. // TODO: override in rising effect
  208. // TODO: check absolute immunity here
  209. return unit->isValidTarget(false);
  210. }
  211. bool UnitEffect::isReceptive(const Mechanics * m, const battle::Unit * unit) const
  212. {
  213. if(ignoreImmunity)
  214. {
  215. //ignore all immunities, except specific absolute immunity(VCMI addition)
  216. //SPELL_IMMUNITY absolute case
  217. std::stringstream cachingStr;
  218. cachingStr << "type_" << Bonus::SPELL_IMMUNITY << "subtype_" << m->getSpellIndex() << "addInfo_1";
  219. if(unit->hasBonus(Selector::typeSubtypeInfo(Bonus::SPELL_IMMUNITY, m->getSpellIndex(), 1), cachingStr.str()))
  220. return false;
  221. return true;
  222. }
  223. else
  224. {
  225. return m->isReceptive(unit);
  226. }
  227. }
  228. bool UnitEffect::isSmartTarget(const Mechanics * m, const battle::Unit * unit, bool alwaysSmart) const
  229. {
  230. const bool smart = m->isSmart() || alwaysSmart;
  231. const bool ignoreOwner = !smart;
  232. return ignoreOwner || m->ownerMatches(unit);
  233. }
  234. void UnitEffect::serializeJsonEffect(JsonSerializeFormat & handler)
  235. {
  236. handler.serializeBool("ignoreImmunity", ignoreImmunity);
  237. handler.serializeInt("chainLength", chainLength, 0);
  238. handler.serializeFloat("chainFactor", chainFactor, 0);
  239. serializeJsonUnitEffect(handler);
  240. }
  241. }
  242. }