BattleSpellMechanics.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /*
  2. * BattleSpellMechanics.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 "BattleSpellMechanics.h"
  12. #include "Problem.h"
  13. #include "CSpellHandler.h"
  14. #include "../battle/IBattleState.h"
  15. #include "../battle/CBattleInfoCallback.h"
  16. #include "../networkPacks/PacksForClientBattle.h"
  17. #include "../networkPacks/SetStackEffect.h"
  18. #include "../CStack.h"
  19. #include <boost/assign.hpp>
  20. #include <vstd/RNG.h>
  21. VCMI_LIB_NAMESPACE_BEGIN
  22. namespace spells
  23. {
  24. namespace SRSLPraserHelpers
  25. {
  26. static int XYToHex(int x, int y)
  27. {
  28. return x + GameConstants::BFIELD_WIDTH * y;
  29. }
  30. static int XYToHex(std::pair<int, int> xy)
  31. {
  32. return XYToHex(xy.first, xy.second);
  33. }
  34. static int hexToY(int battleFieldPosition)
  35. {
  36. return battleFieldPosition/GameConstants::BFIELD_WIDTH;
  37. }
  38. static int hexToX(int battleFieldPosition)
  39. {
  40. int pos = battleFieldPosition - hexToY(battleFieldPosition) * GameConstants::BFIELD_WIDTH;
  41. return pos;
  42. }
  43. static std::pair<int, int> hexToPair(int battleFieldPosition)
  44. {
  45. return std::make_pair(hexToX(battleFieldPosition), hexToY(battleFieldPosition));
  46. }
  47. //moves hex by one hex in given direction
  48. //0 - left top, 1 - right top, 2 - right, 3 - right bottom, 4 - left bottom, 5 - left
  49. static std::pair<int, int> gotoDir(int x, int y, int direction)
  50. {
  51. switch(direction)
  52. {
  53. case 0: //top left
  54. return std::make_pair((y%2) ? x-1 : x, y-1);
  55. case 1: //top right
  56. return std::make_pair((y%2) ? x : x+1, y-1);
  57. case 2: //right
  58. return std::make_pair(x+1, y);
  59. case 3: //right bottom
  60. return std::make_pair((y%2) ? x : x+1, y+1);
  61. case 4: //left bottom
  62. return std::make_pair((y%2) ? x-1 : x, y+1);
  63. case 5: //left
  64. return std::make_pair(x-1, y);
  65. default:
  66. throw std::runtime_error("Disaster: wrong direction in SRSLPraserHelpers::gotoDir!\n");
  67. }
  68. }
  69. static std::pair<int, int> gotoDir(std::pair<int, int> xy, int direction)
  70. {
  71. return gotoDir(xy.first, xy.second, direction);
  72. }
  73. static bool isGoodHex(std::pair<int, int> xy)
  74. {
  75. return xy.first >=0 && xy.first < GameConstants::BFIELD_WIDTH && xy.second >= 0 && xy.second < GameConstants::BFIELD_HEIGHT;
  76. }
  77. //helper function for rangeInHexes
  78. static std::set<ui16> getInRange(unsigned int center, int low, int high)
  79. {
  80. std::set<ui16> ret;
  81. if(low == 0)
  82. {
  83. ret.insert(center);
  84. }
  85. std::pair<int, int> mainPointForLayer[6]; //A, B, C, D, E, F points
  86. for(auto & elem : mainPointForLayer)
  87. elem = hexToPair(center);
  88. for(int it=1; it<=high; ++it) //it - distance to the center
  89. {
  90. for(int b=0; b<6; ++b)
  91. mainPointForLayer[b] = gotoDir(mainPointForLayer[b], b);
  92. if(it>=low)
  93. {
  94. std::pair<int, int> curHex;
  95. //adding lines (A-b, B-c, C-d, etc)
  96. for(int v=0; v<6; ++v)
  97. {
  98. curHex = mainPointForLayer[v];
  99. for(int h=0; h<it; ++h)
  100. {
  101. if(isGoodHex(curHex))
  102. ret.insert(XYToHex(curHex));
  103. curHex = gotoDir(curHex, (v+2)%6);
  104. }
  105. }
  106. } //if(it>=low)
  107. }
  108. return ret;
  109. }
  110. }
  111. BattleSpellMechanics::BattleSpellMechanics(const IBattleCast * event,
  112. std::shared_ptr<effects::Effects> effects_,
  113. std::shared_ptr<IReceptiveCheck> targetCondition_):
  114. BaseMechanics(event),
  115. effects(std::move(effects_)),
  116. targetCondition(std::move(targetCondition_))
  117. {}
  118. BattleSpellMechanics::~BattleSpellMechanics() = default;
  119. void BattleSpellMechanics::applyEffects(ServerCallback * server, const Target & targets, bool indirect, bool ignoreImmunity) const
  120. {
  121. auto callback = [&](const effects::Effect * effect, bool & stop)
  122. {
  123. if(indirect == effect->indirect)
  124. {
  125. if(ignoreImmunity)
  126. {
  127. effect->apply(server, this, targets);
  128. }
  129. else
  130. {
  131. EffectTarget filtered = effect->filterTarget(this, targets);
  132. effect->apply(server, this, filtered);
  133. }
  134. }
  135. };
  136. effects->forEachEffect(getEffectLevel(), callback);
  137. }
  138. bool BattleSpellMechanics::canBeCast(Problem & problem) const
  139. {
  140. auto genProblem = battle()->battleCanCastSpell(caster, mode);
  141. if(genProblem != ESpellCastProblem::OK)
  142. return adaptProblem(genProblem, problem);
  143. switch(mode)
  144. {
  145. case Mode::HERO:
  146. {
  147. const auto * castingHero = dynamic_cast<const CGHeroInstance *>(caster); //todo: unify hero|creature spell cost
  148. if(!castingHero)
  149. {
  150. logGlobal->debug("CSpell::canBeCast: invalid caster");
  151. genProblem = ESpellCastProblem::NO_HERO_TO_CAST_SPELL;
  152. }
  153. else if(!castingHero->getArt(ArtifactPosition::SPELLBOOK))
  154. genProblem = ESpellCastProblem::NO_SPELLBOOK;
  155. else if(!castingHero->canCastThisSpell(owner))
  156. genProblem = ESpellCastProblem::HERO_DOESNT_KNOW_SPELL;
  157. else if(castingHero->mana < battle()->battleGetSpellCost(owner, castingHero)) //not enough mana
  158. genProblem = ESpellCastProblem::NOT_ENOUGH_MANA;
  159. }
  160. break;
  161. }
  162. if(genProblem != ESpellCastProblem::OK)
  163. return adaptProblem(genProblem, problem);
  164. if(!owner->isCombat())
  165. return adaptProblem(ESpellCastProblem::ADVMAP_SPELL_INSTEAD_OF_BATTLE_SPELL, problem);
  166. const PlayerColor player = caster->getCasterOwner();
  167. const BattleSide side = battle()->playerToSide(player);
  168. if(side == BattleSide::NONE)
  169. return adaptProblem(ESpellCastProblem::INVALID, problem);
  170. //effect like Recanter's Cloak. Blocks also passive casting.
  171. //TODO: check creature abilities to block
  172. //TODO: check any possible caster
  173. if(battle()->battleMaxSpellLevel(side) < getSpellLevel() || battle()->battleMinSpellLevel(side) > getSpellLevel())
  174. return adaptProblem(ESpellCastProblem::SPELL_LEVEL_LIMIT_EXCEEDED, problem);
  175. return effects->applicable(problem, this);
  176. }
  177. bool BattleSpellMechanics::canBeCastAt(const Target & target, Problem & problem) const
  178. {
  179. if(!canBeCast(problem))
  180. return false;
  181. Target spellTarget = transformSpellTarget(target);
  182. const battle::Unit * mainTarget = nullptr;
  183. if(spellTarget.front().unitValue)
  184. {
  185. mainTarget = target.front().unitValue;
  186. }
  187. else if(spellTarget.front().hexValue.isValid())
  188. {
  189. mainTarget = battle()->battleGetUnitByPos(target.front().hexValue, true);
  190. }
  191. if (!getSpell()->canCastOnSelf() && !getSpell()->canCastOnlyOnSelf())
  192. {
  193. if(mainTarget && mainTarget == caster)
  194. return false; // can't cast on self
  195. if(mainTarget && mainTarget->isInvincible() && !getSpell()->getPositiveness())
  196. return false;
  197. }
  198. else if(getSpell()->canCastOnlyOnSelf())
  199. {
  200. if(mainTarget && mainTarget != caster)
  201. return false; // can't cast on others
  202. }
  203. return effects->applicable(problem, this, target, spellTarget);
  204. }
  205. std::vector<const CStack *> BattleSpellMechanics::getAffectedStacks(const Target & target) const
  206. {
  207. Target spellTarget = transformSpellTarget(target);
  208. EffectTarget all;
  209. effects->forEachEffect(getEffectLevel(), [&all, &target, &spellTarget, this](const effects::Effect * e, bool & stop)
  210. {
  211. EffectTarget one = e->transformTarget(this, target, spellTarget);
  212. vstd::concatenate(all, one);
  213. });
  214. std::set<const CStack *> stacks;
  215. for(const Destination & dest : all)
  216. {
  217. if(dest.unitValue && !dest.unitValue->isInvincible())
  218. {
  219. //FIXME: remove and return battle::Unit
  220. stacks.insert(battle()->battleGetStackByID(dest.unitValue->unitId(), false));
  221. }
  222. }
  223. std::vector<const CStack *> res;
  224. std::copy(stacks.begin(), stacks.end(), std::back_inserter(res));
  225. return res;
  226. }
  227. void BattleSpellMechanics::cast(ServerCallback * server, const Target & target)
  228. {
  229. BattleSpellCast sc;
  230. int spellCost = 0;
  231. sc.side = casterSide;
  232. sc.spellID = getSpellId();
  233. sc.battleID = battle()->getBattle()->getBattleID();
  234. sc.tile = target.at(0).hexValue;
  235. sc.castByHero = mode == Mode::HERO;
  236. if (mode != Mode::HERO)
  237. sc.casterStack = caster->getCasterUnitId();
  238. sc.manaGained = 0;
  239. sc.activeCast = false;
  240. affectedUnits.clear();
  241. const CGHeroInstance * otherHero = nullptr;
  242. {
  243. //check it there is opponent hero
  244. const BattleSide otherSide = battle()->otherSide(casterSide);
  245. if(battle()->battleHasHero(otherSide))
  246. otherHero = battle()->battleGetFightingHero(otherSide);
  247. }
  248. //calculate spell cost
  249. if(mode == Mode::HERO)
  250. {
  251. const auto * casterHero = dynamic_cast<const CGHeroInstance *>(caster);
  252. spellCost = battle()->battleGetSpellCost(owner, casterHero);
  253. if(nullptr != otherHero) //handle mana channel
  254. {
  255. int manaChannel = 0;
  256. for(const auto * stack : battle()->battleGetAllStacks(true)) //TODO: shouldn't bonus system handle it somehow?
  257. {
  258. if(stack->unitOwner() == otherHero->tempOwner && stack->alive())
  259. vstd::amax(manaChannel, stack->valOfBonuses(BonusType::MANA_CHANNELING));
  260. }
  261. sc.manaGained = (manaChannel * spellCost) / 100;
  262. }
  263. sc.activeCast = true;
  264. }
  265. else if(mode == Mode::CREATURE_ACTIVE || mode == Mode::ENCHANTER)
  266. {
  267. spellCost = 1;
  268. sc.activeCast = true;
  269. }
  270. beforeCast(sc, *server->getRNG(), target);
  271. BattleLogMessage castDescription;
  272. castDescription.battleID = battle()->getBattle()->getBattleID();
  273. switch (mode)
  274. {
  275. case Mode::CREATURE_ACTIVE:
  276. case Mode::ENCHANTER:
  277. case Mode::HERO:
  278. case Mode::PASSIVE:
  279. case Mode::MAGIC_MIRROR:
  280. {
  281. MetaString line;
  282. caster->getCastDescription(owner, affectedUnits, line);
  283. if(!line.empty())
  284. castDescription.lines.push_back(line);
  285. }
  286. break;
  287. default:
  288. break;
  289. }
  290. doRemoveEffects(server, affectedUnits, std::bind(&BattleSpellMechanics::counteringSelector, this, _1));
  291. for(auto & unit : affectedUnits)
  292. sc.affectedCres.insert(unit->unitId());
  293. if(!castDescription.lines.empty())
  294. server->apply(castDescription);
  295. server->apply(sc);
  296. for(auto & p : effectsToApply)
  297. p.first->apply(server, this, p.second);
  298. if(sc.activeCast)
  299. {
  300. caster->spendMana(server, spellCost);
  301. if(sc.manaGained > 0)
  302. {
  303. assert(otherHero);
  304. otherHero->spendMana(server, -sc.manaGained);
  305. }
  306. }
  307. // send empty event to client
  308. // temporary(?) workaround to force animations to trigger
  309. StacksInjured fakeEvent;
  310. fakeEvent.battleID = battle()->getBattle()->getBattleID();
  311. server->apply(fakeEvent);
  312. }
  313. void BattleSpellMechanics::beforeCast(BattleSpellCast & sc, vstd::RNG & rng, const Target & target)
  314. {
  315. affectedUnits.clear();
  316. Target spellTarget = transformSpellTarget(target);
  317. std::vector <const battle::Unit *> resisted;
  318. auto filterResisted = [&, this](const battle::Unit * unit) -> bool
  319. {
  320. if(isNegativeSpell() && isMagicalEffect())
  321. {
  322. //magic resistance
  323. const int prob = std::min(unit->magicResistance(), 100); //probability of resistance in %
  324. if(rng.nextInt(0, 99) < prob)
  325. return true;
  326. }
  327. return false;
  328. };
  329. auto filterUnit = [&](const battle::Unit * unit)
  330. {
  331. if(filterResisted(unit))
  332. resisted.push_back(unit);
  333. else
  334. affectedUnits.push_back(unit);
  335. };
  336. //prepare targets
  337. effectsToApply = effects->prepare(this, target, spellTarget);
  338. std::set<const battle::Unit *> unitTargets = collectTargets();
  339. if (unitTargets.size()==1 && isReflected(*(unitTargets.begin()), rng))
  340. {
  341. return reflect(sc, rng, *(unitTargets.begin()));
  342. }
  343. //process them
  344. for(const auto * unit : unitTargets)
  345. filterUnit(unit);
  346. //and update targets
  347. for(auto & p : effectsToApply)
  348. {
  349. vstd::erase_if(p.second, [&](const Destination & d)
  350. {
  351. if(!d.unitValue)
  352. return false;
  353. return vstd::contains(resisted, d.unitValue);
  354. });
  355. }
  356. for(const auto * unit : resisted)
  357. sc.resistedCres.insert(unit->unitId());
  358. }
  359. bool BattleSpellMechanics::isReflected(const battle::Unit * unit, vstd::RNG & rng)
  360. {
  361. auto range = owner -> getLevelInfo(getRangeLevel()).range;
  362. const auto directSpellRange = boost::assign::list_of(0);
  363. const std::string magicMirrorCacheStr = "type_MAGIC_MIRROR";
  364. static const auto magicMirrorSelector = Selector::type()(BonusType::MAGIC_MIRROR);
  365. bool spellIsDirect = !isMassive() && owner -> getLevelInfo(getRangeLevel()).range == directSpellRange;
  366. bool spellIsReflectable = spellIsDirect && (mode == Mode::HERO || mode == Mode::MAGIC_MIRROR) && isNegativeSpell();
  367. bool targetCanReflectSpell = spellIsReflectable && unit->getAllBonuses(Selector::type()(BonusType::MAGIC_MIRROR))->size()>0;
  368. return targetCanReflectSpell && rng.nextInt(0, 99) < unit->valOfBonuses(magicMirrorSelector, magicMirrorCacheStr);
  369. }
  370. void BattleSpellMechanics::reflect(BattleSpellCast & sc, vstd::RNG & rng, const battle::Unit* unit)
  371. {
  372. auto otherSide = battle()->otherSide(unit->unitSide());
  373. auto newTarget = getRandomUnit(rng, otherSide);
  374. auto reflectedTo = newTarget->getPosition();
  375. mode = Mode::MAGIC_MIRROR;
  376. sc.reflectedCres.insert(unit->unitId());
  377. sc.tile = reflectedTo;
  378. if (!isReceptive(newTarget))
  379. {
  380. sc.resistedCres.insert(newTarget->unitId()); //A spell can be reflected to then resisted by an immune unit. Consistent with the original game.
  381. }
  382. beforeCast(sc, rng,
  383. boost::assign::list_of(reflectedTo));
  384. }
  385. const battle::Unit* BattleSpellMechanics::getRandomUnit(vstd::RNG & rng, BattleSide & side)
  386. {
  387. auto targets = battle()->getBattle()->getUnitsIf([this, & side](const battle::Unit * unit)
  388. {
  389. return unit->unitSide() == side && unit->isValidTarget(false) &&
  390. !unit->hasBonusOfType(BonusType::SIEGE_WEAPON);
  391. });
  392. return !targets.empty() ? (*RandomGeneratorUtil::nextItem(targets, rng)) : nullptr;
  393. }
  394. void BattleSpellMechanics::castEval(ServerCallback * server, const Target & target)
  395. {
  396. affectedUnits.clear();
  397. //TODO: evaluate caster updates (mana usage etc.)
  398. //TODO: evaluate random values
  399. Target spellTarget = transformSpellTarget(target);
  400. effectsToApply = effects->prepare(this, target, spellTarget);
  401. std::set<const battle::Unit *> unitTargets = collectTargets();
  402. auto selector = std::bind(&BattleSpellMechanics::counteringSelector, this, _1);
  403. std::copy(std::begin(unitTargets), std::end(unitTargets), std::back_inserter(affectedUnits));
  404. doRemoveEffects(server, affectedUnits, selector);
  405. for(auto & p : effectsToApply)
  406. p.first->apply(server, this, p.second);
  407. }
  408. std::set<const battle::Unit *> BattleSpellMechanics::collectTargets() const
  409. {
  410. std::set<const battle::Unit *> result;
  411. for(const auto & p : effectsToApply)
  412. {
  413. for(const Destination & d : p.second)
  414. if(d.unitValue)
  415. result.insert(d.unitValue);
  416. }
  417. return result;
  418. }
  419. void BattleSpellMechanics::doRemoveEffects(ServerCallback * server, const battle::Units & targets, const CSelector & selector)
  420. {
  421. SetStackEffect sse;
  422. sse.battleID = battle()->getBattle()->getBattleID();
  423. for(const auto * unit : targets)
  424. {
  425. std::vector<Bonus> buffer;
  426. auto bl = unit->getBonuses(selector);
  427. for(const auto & item : *bl)
  428. buffer.emplace_back(*item);
  429. if(!buffer.empty())
  430. sse.toRemove.emplace_back(unit->unitId(), buffer);
  431. }
  432. if(!sse.toRemove.empty())
  433. server->apply(sse);
  434. }
  435. bool BattleSpellMechanics::counteringSelector(const Bonus * bonus) const
  436. {
  437. if(bonus->source != BonusSource::SPELL_EFFECT)
  438. return false;
  439. for(const SpellID & id : owner->counteredSpells)
  440. {
  441. if(bonus->sid.as<SpellID>() == id)
  442. return true;
  443. }
  444. return false;
  445. }
  446. BattleHexArray BattleSpellMechanics::spellRangeInHexes(const BattleHex & centralHex) const
  447. {
  448. using namespace SRSLPraserHelpers;
  449. BattleHexArray ret;
  450. std::vector<int> rng = owner->getLevelInfo(getRangeLevel()).range;
  451. for(auto & elem : rng)
  452. {
  453. std::set<ui16> curLayer = getInRange(centralHex.toInt(), elem, elem);
  454. //adding obtained hexes
  455. for(const auto & curLayer_it : curLayer)
  456. ret.insert(curLayer_it);
  457. }
  458. return ret;
  459. }
  460. Target BattleSpellMechanics::transformSpellTarget(const Target & aimPoint) const
  461. {
  462. Target spellTarget;
  463. if(aimPoint.empty())
  464. {
  465. logGlobal->error("Aimed spell cast with no destination.");
  466. }
  467. else
  468. {
  469. const Destination & primary = aimPoint.at(0);
  470. BattleHex aimPointHex = primary.hexValue;
  471. //transform primary spell target with spell range (if it`s valid), leave anything else to effects
  472. if(aimPointHex.isValid())
  473. {
  474. auto spellRange = spellRangeInHexes(aimPointHex);
  475. for(const auto & hex : spellRange)
  476. spellTarget.push_back(Destination(hex));
  477. }
  478. }
  479. if(spellTarget.empty())
  480. spellTarget.push_back(Destination(BattleHex::INVALID));
  481. return spellTarget;
  482. }
  483. std::vector<AimType> BattleSpellMechanics::getTargetTypes() const
  484. {
  485. auto ret = BaseMechanics::getTargetTypes();
  486. if(!ret.empty())
  487. {
  488. effects->forEachEffect(getEffectLevel(), [&](const effects::Effect * e, bool & stop)
  489. {
  490. e->adjustTargetTypes(ret);
  491. stop = ret.empty();
  492. });
  493. }
  494. return ret;
  495. }
  496. std::vector<Destination> BattleSpellMechanics::getPossibleDestinations(size_t index, AimType aimType, const Target & current, bool fast) const
  497. {
  498. //TODO: BattleSpellMechanics::getPossibleDestinations
  499. if(index != 0)
  500. return std::vector<Destination>();
  501. std::vector<Destination> ret;
  502. switch(aimType)
  503. {
  504. case AimType::CREATURE:
  505. {
  506. auto stacks = battle()->battleGetAllStacks();
  507. for(auto stack : stacks)
  508. {
  509. Target tmp = current;
  510. tmp.emplace_back(stack->getPosition());
  511. detail::ProblemImpl ignored;
  512. if(canBeCastAt(tmp, ignored))
  513. ret.emplace_back(stack->getPosition());
  514. }
  515. break;
  516. }
  517. case AimType::LOCATION:
  518. if(fast)
  519. {
  520. auto stacks = battle()->battleGetAllStacks();
  521. BattleHexArray hexesToCheck;
  522. for(auto stack : stacks)
  523. {
  524. hexesToCheck.insert(stack->getPosition());
  525. hexesToCheck.insert(stack->getPosition().getNeighbouringTiles());
  526. }
  527. for(const auto & hex : hexesToCheck)
  528. {
  529. if(hex.isAvailable())
  530. {
  531. Target tmp = current;
  532. tmp.emplace_back(hex);
  533. detail::ProblemImpl ignored;
  534. if(canBeCastAt(tmp, ignored))
  535. ret.emplace_back(hex);
  536. }
  537. }
  538. }
  539. else
  540. {
  541. for(int i = 0; i < GameConstants::BFIELD_SIZE; i++)
  542. {
  543. BattleHex dest(i);
  544. if(dest.isAvailable())
  545. {
  546. Target tmp = current;
  547. tmp.emplace_back(dest);
  548. detail::ProblemImpl ignored;
  549. if(canBeCastAt(tmp, ignored))
  550. ret.emplace_back(dest);
  551. }
  552. }
  553. }
  554. break;
  555. case AimType::NO_TARGET:
  556. ret.emplace_back();
  557. break;
  558. default:
  559. break;
  560. }
  561. return ret;
  562. }
  563. bool BattleSpellMechanics::isReceptive(const battle::Unit * target) const
  564. {
  565. return targetCondition->isReceptive(this, target);
  566. }
  567. bool BattleSpellMechanics::isSmart() const
  568. {
  569. return mode != Mode::MAGIC_MIRROR && BaseMechanics::isSmart();
  570. }
  571. BattleHexArray BattleSpellMechanics::rangeInHexes(const BattleHex & centralHex) const
  572. {
  573. if(isMassive() || !centralHex.isValid())
  574. return BattleHexArray();
  575. Target aimPoint;
  576. aimPoint.push_back(Destination(centralHex));
  577. Target spellTarget = transformSpellTarget(aimPoint);
  578. BattleHexArray effectRange;
  579. effects->forEachEffect(getEffectLevel(), [&](const effects::Effect * effect, bool & stop)
  580. {
  581. if(!effect->indirect)
  582. {
  583. effect->adjustAffectedHexes(effectRange, this, spellTarget);
  584. }
  585. });
  586. return effectRange;
  587. }
  588. const Spell * BattleSpellMechanics::getSpell() const
  589. {
  590. return owner;
  591. }
  592. }
  593. VCMI_LIB_NAMESPACE_END