BattleSpellMechanics.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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 "../battle/Unit.h"
  17. #include "../networkPacks/PacksForClientBattle.h"
  18. #include "../networkPacks/SetStackEffect.h"
  19. #include "../CStack.h"
  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. void BattleSpellMechanics::forEachEffect(const std::function<bool (const spells::effects::Effect &)> & fn) const
  119. {
  120. if (!effects)
  121. return;
  122. effects->forEachEffect(getEffectLevel(), [&](const spells::effects::Effect * eff, bool & stop)
  123. {
  124. if(!eff)
  125. return;
  126. if(fn(*eff))
  127. stop = true;
  128. });
  129. }
  130. BattleSpellMechanics::~BattleSpellMechanics() = default;
  131. void BattleSpellMechanics::applyEffects(ServerCallback * server, const Target & targets, bool indirect, bool ignoreImmunity) const
  132. {
  133. auto callback = [&](const effects::Effect * effect, bool & stop)
  134. {
  135. if(indirect == effect->indirect)
  136. {
  137. if(ignoreImmunity)
  138. {
  139. effect->apply(server, this, targets);
  140. }
  141. else
  142. {
  143. EffectTarget filtered = effect->filterTarget(this, targets);
  144. effect->apply(server, this, filtered);
  145. }
  146. }
  147. };
  148. effects->forEachEffect(getEffectLevel(), callback);
  149. }
  150. bool BattleSpellMechanics::canBeCast(Problem & problem) const
  151. {
  152. auto genProblem = battle()->battleCanCastSpell(caster, mode);
  153. if(genProblem != ESpellCastProblem::OK)
  154. return adaptProblem(genProblem, problem);
  155. switch(mode)
  156. {
  157. case Mode::HERO:
  158. {
  159. const auto * castingHero = dynamic_cast<const CGHeroInstance *>(caster); //todo: unify hero|creature spell cost
  160. if(!castingHero)
  161. {
  162. logGlobal->debug("CSpell::canBeCast: invalid caster");
  163. genProblem = ESpellCastProblem::NO_HERO_TO_CAST_SPELL;
  164. }
  165. else if(!castingHero->getArt(ArtifactPosition::SPELLBOOK))
  166. genProblem = ESpellCastProblem::NO_SPELLBOOK;
  167. else if(!castingHero->canCastThisSpell(owner))
  168. genProblem = ESpellCastProblem::HERO_DOESNT_KNOW_SPELL;
  169. else if(castingHero->mana < battle()->battleGetSpellCost(owner, castingHero)) //not enough mana
  170. genProblem = ESpellCastProblem::NOT_ENOUGH_MANA;
  171. }
  172. break;
  173. }
  174. if(genProblem != ESpellCastProblem::OK)
  175. return adaptProblem(genProblem, problem);
  176. if(!owner->isCombat())
  177. return adaptProblem(ESpellCastProblem::ADVMAP_SPELL_INSTEAD_OF_BATTLE_SPELL, problem);
  178. const PlayerColor player = caster->getCasterOwner();
  179. const BattleSide side = battle()->playerToSide(player);
  180. if(side == BattleSide::NONE)
  181. return adaptProblem(ESpellCastProblem::INVALID, problem);
  182. //effect like Recanter's Cloak. Blocks also passive casting.
  183. //TODO: check creature abilities to block
  184. //TODO: check any possible caster
  185. if(battle()->battleMaxSpellLevel(side) < getSpellLevel() || battle()->battleMinSpellLevel(side) > getSpellLevel())
  186. return adaptProblem(ESpellCastProblem::SPELL_LEVEL_LIMIT_EXCEEDED, problem);
  187. return effects->applicable(problem, this);
  188. }
  189. bool BattleSpellMechanics::canCastAtTarget(const battle::Unit * target) const
  190. {
  191. if(mode == Mode::HERO)
  192. return true;
  193. if(!target)
  194. return true;
  195. auto spell = getSpell();
  196. int range = caster->getEffectRange(spell);
  197. if(range <= 0)
  198. return true;
  199. auto casterStack = battle()->battleGetStackByID(caster->getCasterUnitId(), false);
  200. std::vector<BattleHex> casterPos = { casterStack->getPosition() };
  201. BattleHex casterWidePos = casterStack->occupiedHex();
  202. if(casterWidePos != BattleHex::INVALID)
  203. casterPos.push_back(casterWidePos);
  204. std::vector<BattleHex> destPos = { target->getPosition() };
  205. BattleHex destWidePos = target->occupiedHex();
  206. if(destWidePos != BattleHex::INVALID)
  207. destPos.push_back(destWidePos);
  208. int minDistance = std::numeric_limits<int>::max();
  209. for(auto & caster : casterPos)
  210. for(auto & dest : destPos)
  211. {
  212. int distance = BattleHex::getDistance(caster, dest);
  213. if(distance < minDistance)
  214. minDistance = distance;
  215. }
  216. if(minDistance > range)
  217. return false;
  218. return true;
  219. }
  220. bool BattleSpellMechanics::canBeCastAt(const Target & target) const
  221. {
  222. spells::detail::ProblemImpl ignore;
  223. return canBeCastAt(target, ignore);
  224. }
  225. bool BattleSpellMechanics::canBeCastAt(const Target & target, Problem & problem) const
  226. {
  227. if(!canBeCast(problem))
  228. return false;
  229. Target spellTarget = transformSpellTarget(target);
  230. const battle::Unit * mainTarget = nullptr;
  231. if(spellTarget.front().unitValue)
  232. {
  233. mainTarget = target.front().unitValue;
  234. }
  235. else if(spellTarget.front().hexValue.isValid())
  236. {
  237. mainTarget = battle()->battleGetUnitByPos(target.front().hexValue, true);
  238. }
  239. if(!canCastAtTarget(mainTarget))
  240. return false;
  241. if (!getSpell()->canCastOnSelf() && !getSpell()->canCastOnlyOnSelf())
  242. {
  243. if(mainTarget && mainTarget == caster)
  244. return false; // can't cast on self
  245. if(mainTarget && mainTarget->isInvincible() && !getSpell()->getPositiveness())
  246. return false;
  247. }
  248. else if(getSpell()->canCastOnlyOnSelf())
  249. {
  250. if(mainTarget && mainTarget != caster)
  251. return false; // can't cast on others
  252. }
  253. return effects->applicable(problem, this, target, spellTarget);
  254. }
  255. std::vector<const CStack *> BattleSpellMechanics::getAffectedStacks(const Target & target) const
  256. {
  257. Target spellTarget = transformSpellTarget(target);
  258. EffectTarget all;
  259. effects->forEachEffect(getEffectLevel(), [&all, &target, &spellTarget, this](const effects::Effect * e, bool & stop)
  260. {
  261. EffectTarget one = e->transformTarget(this, target, spellTarget);
  262. vstd::concatenate(all, one);
  263. });
  264. std::set<const CStack *> stacks;
  265. for(const Destination & dest : all)
  266. {
  267. if(dest.unitValue && !dest.unitValue->isInvincible())
  268. {
  269. //FIXME: remove and return battle::Unit
  270. stacks.insert(battle()->battleGetStackByID(dest.unitValue->unitId(), false));
  271. }
  272. }
  273. std::vector<const CStack *> res;
  274. std::copy(stacks.begin(), stacks.end(), std::back_inserter(res));
  275. return res;
  276. }
  277. void BattleSpellMechanics::cast(ServerCallback * server, const Target & target)
  278. {
  279. BattleSpellCast sc;
  280. int spellCost = 0;
  281. sc.side = casterSide;
  282. sc.spellID = getSpellId();
  283. sc.battleID = battle()->getBattle()->getBattleID();
  284. sc.tile = target.at(0).hexValue;
  285. sc.castByHero = mode == Mode::HERO;
  286. if (mode != Mode::HERO)
  287. sc.casterStack = caster->getCasterUnitId();
  288. sc.manaGained = 0;
  289. sc.activeCast = false;
  290. affectedUnits.clear();
  291. const CGHeroInstance * otherHero = nullptr;
  292. {
  293. //check it there is opponent hero
  294. const BattleSide otherSide = battle()->otherSide(casterSide);
  295. if(battle()->battleHasHero(otherSide))
  296. otherHero = battle()->battleGetFightingHero(otherSide);
  297. }
  298. //calculate spell cost
  299. if(mode == Mode::HERO)
  300. {
  301. const auto * casterHero = dynamic_cast<const CGHeroInstance *>(caster);
  302. spellCost = battle()->battleGetSpellCost(owner, casterHero);
  303. if(nullptr != otherHero) //handle mana channel
  304. {
  305. int manaChannel = 0;
  306. for(const auto * stack : battle()->battleGetAllStacks(true)) //TODO: shouldn't bonus system handle it somehow?
  307. {
  308. if(stack->unitOwner() == otherHero->tempOwner && stack->alive())
  309. vstd::amax(manaChannel, stack->valOfBonuses(BonusType::MANA_CHANNELING));
  310. }
  311. sc.manaGained = (manaChannel * spellCost) / 100;
  312. }
  313. sc.activeCast = true;
  314. }
  315. else if(mode == Mode::CREATURE_ACTIVE || mode == Mode::ENCHANTER)
  316. {
  317. spellCost = 1;
  318. sc.activeCast = true;
  319. }
  320. beforeCast(sc, *server->getRNG(), target);
  321. BattleLogMessage castDescription;
  322. castDescription.battleID = battle()->getBattle()->getBattleID();
  323. switch (mode)
  324. {
  325. case Mode::CREATURE_ACTIVE:
  326. case Mode::ENCHANTER:
  327. case Mode::HERO:
  328. case Mode::PASSIVE:
  329. case Mode::MAGIC_MIRROR:
  330. {
  331. MetaString line;
  332. caster->getCastDescription(owner, affectedUnits, line);
  333. if(!line.empty())
  334. castDescription.lines.push_back(line);
  335. }
  336. break;
  337. default:
  338. break;
  339. }
  340. doRemoveEffects(server, affectedUnits, std::bind(&BattleSpellMechanics::counteringSelector, this, _1));
  341. for(auto & unit : affectedUnits)
  342. sc.affectedCres.insert(unit->unitId());
  343. if(!castDescription.lines.empty())
  344. server->apply(castDescription);
  345. server->apply(sc);
  346. for(auto & p : effectsToApply)
  347. p.first->apply(server, this, p.second);
  348. if(sc.activeCast)
  349. {
  350. caster->spendMana(server, spellCost);
  351. if(sc.manaGained > 0)
  352. {
  353. assert(otherHero);
  354. otherHero->spendMana(server, -sc.manaGained);
  355. }
  356. }
  357. // send empty event to client
  358. // temporary(?) workaround to force animations to trigger
  359. StacksInjured fakeEvent;
  360. fakeEvent.battleID = battle()->getBattle()->getBattleID();
  361. server->apply(fakeEvent);
  362. }
  363. void BattleSpellMechanics::beforeCast(BattleSpellCast & sc, vstd::RNG & rng, const Target & target)
  364. {
  365. affectedUnits.clear();
  366. Target spellTarget = transformSpellTarget(target);
  367. std::vector <const battle::Unit *> resisted;
  368. resistantUnitIds.clear();
  369. if(isNegativeSpell() && isMagicalEffect())
  370. {
  371. //magic resistance
  372. for (const auto * unit : battle()->battleGetAllUnits(false))
  373. {
  374. const int prob = std::min(unit->magicResistance(), 100); //probability of resistance in %
  375. if(rng.nextInt(0, 99) < prob)
  376. resistantUnitIds.insert(unit->unitId());
  377. }
  378. }
  379. auto filterResisted = [&, this](const battle::Unit * unit) -> bool
  380. {
  381. return resistantUnitIds.contains(unit->unitId());
  382. };
  383. auto filterUnit = [&](const battle::Unit * unit)
  384. {
  385. if(filterResisted(unit))
  386. resisted.push_back(unit);
  387. else
  388. affectedUnits.push_back(unit);
  389. };
  390. if (!target.empty())
  391. {
  392. const battle::Unit * targetedUnit = battle()->battleGetUnitByPos(target.front().hexValue, true);
  393. if (isReflected(targetedUnit, rng)) {
  394. reflect(sc, rng, targetedUnit);
  395. return;
  396. }
  397. }
  398. //prepare targets
  399. effectsToApply = effects->prepare(this, target, spellTarget);
  400. std::set<const battle::Unit *> unitTargets = collectTargets();
  401. //process them
  402. for(const auto * unit : unitTargets)
  403. filterUnit(unit);
  404. //and update targets
  405. for(auto & p : effectsToApply)
  406. {
  407. vstd::erase_if(p.second, [&](const Destination & d)
  408. {
  409. if(!d.unitValue)
  410. return false;
  411. return vstd::contains(resisted, d.unitValue);
  412. });
  413. }
  414. for(const auto * unit : resisted)
  415. sc.resistedCres.insert(unit->unitId());
  416. resistantUnitIds.clear();
  417. }
  418. bool BattleSpellMechanics::isReflected(const battle::Unit * unit, vstd::RNG & rng)
  419. {
  420. if (unit == nullptr)
  421. return false;
  422. const std::vector<int> directSpellRange = { 0 };
  423. bool isDirectSpell = !isMassive() && owner -> getLevelInfo(getRangeLevel()).range == directSpellRange;
  424. bool spellIsReflectable = isDirectSpell && (mode == Mode::HERO || mode == Mode::MAGIC_MIRROR) && isNegativeSpell();
  425. bool targetCanReflectSpell = spellIsReflectable && unit->getAllBonuses(Selector::type()(BonusType::MAGIC_MIRROR))->size()>0;
  426. return targetCanReflectSpell && rng.nextInt(0, 99) < unit->valOfBonuses(BonusType::MAGIC_MIRROR);
  427. }
  428. void BattleSpellMechanics::reflect(BattleSpellCast & sc, vstd::RNG & rng, const battle::Unit * unit)
  429. {
  430. auto otherSide = battle()->otherSide(unit->unitSide());
  431. auto newTarget = getRandomUnit(rng, otherSide);
  432. if (newTarget == nullptr)
  433. throw std::runtime_error("Failed to find random unit to reflect spell!");
  434. auto reflectedTo = newTarget->getPosition();
  435. mode = Mode::MAGIC_MIRROR;
  436. sc.reflectedCres.insert(unit->unitId());
  437. sc.tile = reflectedTo;
  438. if (!isReceptive(newTarget))
  439. sc.resistedCres.insert(newTarget->unitId()); //A spell can be reflected to then resisted by an immune unit. Consistent with the original game.
  440. beforeCast(sc, rng, { Destination(reflectedTo) });
  441. }
  442. const battle::Unit * BattleSpellMechanics::getRandomUnit(vstd::RNG & rng, const BattleSide & side)
  443. {
  444. auto targets = battle()->getBattle()->getUnitsIf([&side](const battle::Unit * unit)
  445. {
  446. return unit->unitSide() == side && unit->isValidTarget(false) &&
  447. !unit->hasBonusOfType(BonusType::SIEGE_WEAPON);
  448. });
  449. return !targets.empty() ? (*RandomGeneratorUtil::nextItem(targets, rng)) : nullptr;
  450. }
  451. void BattleSpellMechanics::castEval(ServerCallback * server, const Target & target)
  452. {
  453. affectedUnits.clear();
  454. //TODO: evaluate caster updates (mana usage etc.)
  455. //TODO: evaluate random values
  456. Target spellTarget = transformSpellTarget(target);
  457. effectsToApply = effects->prepare(this, target, spellTarget);
  458. std::set<const battle::Unit *> unitTargets = collectTargets();
  459. auto selector = std::bind(&BattleSpellMechanics::counteringSelector, this, _1);
  460. std::copy(std::begin(unitTargets), std::end(unitTargets), std::back_inserter(affectedUnits));
  461. doRemoveEffects(server, affectedUnits, selector);
  462. for(auto & p : effectsToApply)
  463. p.first->apply(server, this, p.second);
  464. }
  465. std::set<const battle::Unit *> BattleSpellMechanics::collectTargets() const
  466. {
  467. std::set<const battle::Unit *> result;
  468. for(const auto & p : effectsToApply)
  469. {
  470. for(const Destination & d : p.second)
  471. if(d.unitValue)
  472. result.insert(d.unitValue);
  473. }
  474. return result;
  475. }
  476. void BattleSpellMechanics::doRemoveEffects(ServerCallback * server, const battle::Units & targets, const CSelector & selector)
  477. {
  478. SetStackEffect sse;
  479. sse.battleID = battle()->getBattle()->getBattleID();
  480. for(const auto * unit : targets)
  481. {
  482. std::vector<Bonus> buffer;
  483. auto bl = unit->getBonuses(selector);
  484. for(const auto & item : *bl)
  485. buffer.emplace_back(*item);
  486. if(!buffer.empty())
  487. sse.toRemove.emplace_back(unit->unitId(), buffer);
  488. }
  489. if(!sse.toRemove.empty())
  490. server->apply(sse);
  491. }
  492. bool BattleSpellMechanics::counteringSelector(const Bonus * bonus) const
  493. {
  494. if(bonus->source != BonusSource::SPELL_EFFECT)
  495. return false;
  496. for(const SpellID & id : owner->counteredSpells)
  497. {
  498. if(bonus->sid.as<SpellID>() == id)
  499. return true;
  500. }
  501. return false;
  502. }
  503. BattleHexArray BattleSpellMechanics::spellRangeInHexes(const BattleHex & centralHex) const
  504. {
  505. using namespace SRSLPraserHelpers;
  506. BattleHexArray ret;
  507. std::vector<int> rng = owner->getLevelInfo(getRangeLevel()).range;
  508. for(auto & elem : rng)
  509. {
  510. std::set<ui16> curLayer = getInRange(centralHex.toInt(), elem, elem);
  511. //adding obtained hexes
  512. for(const auto & curLayer_it : curLayer)
  513. ret.insert(curLayer_it);
  514. }
  515. return ret;
  516. }
  517. Target BattleSpellMechanics::transformSpellTarget(const Target & aimPoint) const
  518. {
  519. Target spellTarget;
  520. if(aimPoint.empty())
  521. {
  522. logGlobal->error("Aimed spell cast with no destination.");
  523. }
  524. else
  525. {
  526. const Destination & primary = aimPoint.at(0);
  527. BattleHex aimPointHex = primary.hexValue;
  528. //transform primary spell target with spell range (if it`s valid), leave anything else to effects
  529. if(aimPointHex.isValid())
  530. {
  531. auto spellRange = spellRangeInHexes(aimPointHex);
  532. for(const auto & hex : spellRange)
  533. spellTarget.push_back(Destination(hex));
  534. }
  535. }
  536. if(spellTarget.empty())
  537. spellTarget.push_back(Destination(BattleHex::INVALID));
  538. return spellTarget;
  539. }
  540. std::vector<AimType> BattleSpellMechanics::getTargetTypes() const
  541. {
  542. auto ret = BaseMechanics::getTargetTypes();
  543. if(!ret.empty())
  544. {
  545. effects->forEachEffect(getEffectLevel(), [&](const effects::Effect * e, bool & stop)
  546. {
  547. e->adjustTargetTypes(ret);
  548. stop = ret.empty();
  549. });
  550. }
  551. return ret;
  552. }
  553. std::vector<Destination> BattleSpellMechanics::getPossibleDestinations(size_t index, AimType aimType, const Target & current, bool fast) const
  554. {
  555. //TODO: BattleSpellMechanics::getPossibleDestinations
  556. if(index != 0)
  557. return std::vector<Destination>();
  558. std::vector<Destination> ret;
  559. switch(aimType)
  560. {
  561. case AimType::CREATURE:
  562. {
  563. auto stacks = battle()->battleGetAllStacks();
  564. for(auto stack : stacks)
  565. {
  566. Target tmp = current;
  567. tmp.emplace_back(stack->getPosition());
  568. if(canBeCastAt(tmp))
  569. ret.emplace_back(stack->getPosition());
  570. }
  571. break;
  572. }
  573. case AimType::LOCATION:
  574. if(fast)
  575. {
  576. auto stacks = battle()->battleGetAllStacks();
  577. BattleHexArray hexesToCheck;
  578. for(auto stack : stacks)
  579. {
  580. hexesToCheck.insert(stack->getPosition());
  581. hexesToCheck.insert(stack->getPosition().getNeighbouringTiles());
  582. }
  583. for(const auto & hex : hexesToCheck)
  584. {
  585. if(hex.isAvailable())
  586. {
  587. Target tmp = current;
  588. tmp.emplace_back(hex);
  589. if(canBeCastAt(tmp))
  590. ret.emplace_back(hex);
  591. }
  592. }
  593. }
  594. else
  595. {
  596. for(int i = 0; i < GameConstants::BFIELD_SIZE; i++)
  597. {
  598. BattleHex dest(i);
  599. if(dest.isAvailable())
  600. {
  601. Target tmp = current;
  602. tmp.emplace_back(dest);
  603. if(canBeCastAt(tmp))
  604. ret.emplace_back(dest);
  605. }
  606. }
  607. }
  608. break;
  609. case AimType::NO_TARGET:
  610. ret.emplace_back();
  611. break;
  612. default:
  613. break;
  614. }
  615. return ret;
  616. }
  617. bool BattleSpellMechanics::isReceptive(const battle::Unit * target) const
  618. {
  619. return targetCondition->isReceptive(this, target);
  620. }
  621. bool BattleSpellMechanics::isSmart() const
  622. {
  623. return mode != Mode::MAGIC_MIRROR && BaseMechanics::isSmart();
  624. }
  625. bool BattleSpellMechanics::wouldResist(const battle::Unit * unit) const
  626. {
  627. return resistantUnitIds.contains(unit->unitId());
  628. }
  629. BattleHexArray BattleSpellMechanics::rangeInHexes(const BattleHex & centralHex) const
  630. {
  631. if(isMassive() || !centralHex.isValid())
  632. return BattleHexArray();
  633. Target aimPoint;
  634. aimPoint.push_back(Destination(centralHex));
  635. Target spellTarget = transformSpellTarget(aimPoint);
  636. BattleHexArray effectRange;
  637. effects->forEachEffect(getEffectLevel(), [&](const effects::Effect * effect, bool & stop)
  638. {
  639. if(!effect->indirect)
  640. {
  641. effect->adjustAffectedHexes(effectRange, this, spellTarget);
  642. }
  643. });
  644. return effectRange;
  645. }
  646. Target BattleSpellMechanics::canonicalizeTarget(const Target & aim) const
  647. {
  648. return transformSpellTarget(aim);
  649. }
  650. const Spell * BattleSpellMechanics::getSpell() const
  651. {
  652. return owner;
  653. }
  654. }
  655. VCMI_LIB_NAMESPACE_END