UnitEffect.cpp 7.4 KB

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