BattleActionsController.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  1. /*
  2. * BattleActionsController.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 "BattleActionsController.h"
  12. #include "BattleWindow.h"
  13. #include "BattleStacksController.h"
  14. #include "BattleInterface.h"
  15. #include "BattleFieldController.h"
  16. #include "BattleSiegeController.h"
  17. #include "BattleInterfaceClasses.h"
  18. #include "../CPlayerInterface.h"
  19. #include "../gui/CursorHandler.h"
  20. #include "../GameEngine.h"
  21. #include "../GameInstance.h"
  22. #include "../gui/CIntObject.h"
  23. #include "../gui/WindowHandler.h"
  24. #include "../windows/CCreatureWindow.h"
  25. #include "../windows/InfoWindows.h"
  26. #include "../../lib/CConfigHandler.h"
  27. #include "../../lib/GameLibrary.h"
  28. #include "../../lib/texts/CGeneralTextHandler.h"
  29. #include "../../lib/CRandomGenerator.h"
  30. #include "../../lib/CStack.h"
  31. #include "../../lib/battle/BattleAction.h"
  32. #include "../../lib/battle/CPlayerBattleCallback.h"
  33. #include "../../lib/callback/CCallback.h"
  34. #include "../../lib/spells/CSpellHandler.h"
  35. #include "../../lib/spells/ISpellMechanics.h"
  36. #include "../../lib/spells/Problem.h"
  37. struct TextReplacement
  38. {
  39. std::string placeholder;
  40. std::string replacement;
  41. };
  42. using TextReplacementList = std::vector<TextReplacement>;
  43. static std::string replacePlaceholders(std::string input, const TextReplacementList & format )
  44. {
  45. for(const auto & entry : format)
  46. boost::replace_all(input, entry.placeholder, entry.replacement);
  47. return input;
  48. }
  49. static std::string translatePlural(int amount, const std::string& baseTextID)
  50. {
  51. if(amount == 1)
  52. return LIBRARY->generaltexth->translate(baseTextID + ".1");
  53. return LIBRARY->generaltexth->translate(baseTextID);
  54. }
  55. static std::string formatPluralImpl(int amount, const std::string & amountString, const std::string & baseTextID)
  56. {
  57. std::string baseString = translatePlural(amount, baseTextID);
  58. TextReplacementList replacements {
  59. { "%d", amountString }
  60. };
  61. return replacePlaceholders(baseString, replacements);
  62. }
  63. static std::string formatPlural(int amount, const std::string & baseTextID)
  64. {
  65. return formatPluralImpl(amount, std::to_string(amount), baseTextID);
  66. }
  67. static std::string formatPlural(DamageRange range, const std::string & baseTextID)
  68. {
  69. if (range.min == range.max)
  70. return formatPlural(range.min, baseTextID);
  71. std::string rangeString = std::to_string(range.min) + " - " + std::to_string(range.max);
  72. return formatPluralImpl(range.max, rangeString, baseTextID);
  73. }
  74. static std::string formatAttack(const DamageEstimation & estimation, const std::string & creatureName, const std::string & baseTextID, int shotsLeft)
  75. {
  76. TextReplacementList replacements = {
  77. { "%CREATURE", creatureName },
  78. { "%DAMAGE", formatPlural(estimation.damage, "vcmi.battleWindow.damageEstimation.damage") },
  79. { "%SHOTS", formatPlural(shotsLeft, "vcmi.battleWindow.damageEstimation.shots") },
  80. { "%KILLS", formatPlural(estimation.kills, "vcmi.battleWindow.damageEstimation.kills") },
  81. };
  82. return replacePlaceholders(LIBRARY->generaltexth->translate(baseTextID), replacements);
  83. }
  84. static std::string formatMeleeAttack(const DamageEstimation & estimation, const std::string & creatureName)
  85. {
  86. std::string baseTextID = estimation.kills.max == 0 ?
  87. "vcmi.battleWindow.damageEstimation.melee" :
  88. "vcmi.battleWindow.damageEstimation.meleeKills";
  89. return formatAttack(estimation, creatureName, baseTextID, 0);
  90. }
  91. static std::string formatRangedAttack(const DamageEstimation & estimation, const std::string & creatureName, int shotsLeft)
  92. {
  93. std::string baseTextID = estimation.kills.max == 0 ?
  94. "vcmi.battleWindow.damageEstimation.ranged" :
  95. "vcmi.battleWindow.damageEstimation.rangedKills";
  96. return formatAttack(estimation, creatureName, baseTextID, shotsLeft);
  97. }
  98. static std::string formatRetaliation(const DamageEstimation & estimation, bool mayBeKilled)
  99. {
  100. if (estimation.damage.max == 0)
  101. return LIBRARY->generaltexth->translate("vcmi.battleWindow.damageRetaliation.never");
  102. std::string baseTextID = estimation.kills.max == 0 ?
  103. "vcmi.battleWindow.damageRetaliation.damage" :
  104. "vcmi.battleWindow.damageRetaliation.damageKills";
  105. std::string prefixTextID = mayBeKilled ?
  106. "vcmi.battleWindow.damageRetaliation.may" :
  107. "vcmi.battleWindow.damageRetaliation.will";
  108. return LIBRARY->generaltexth->translate(prefixTextID) + formatAttack(estimation, "", baseTextID, 0);
  109. }
  110. BattleActionsController::BattleActionsController(BattleInterface & owner):
  111. owner(owner),
  112. selectedStack(nullptr),
  113. heroSpellToCast(nullptr)
  114. {
  115. }
  116. void BattleActionsController::endCastingSpell()
  117. {
  118. if(heroSpellToCast)
  119. {
  120. heroSpellToCast.reset();
  121. owner.windowObject->blockUI(false);
  122. }
  123. if(owner.stacksController->getActiveStack())
  124. possibleActions = getPossibleActionsForStack(owner.stacksController->getActiveStack()); //restore actions after they were cleared
  125. selectedStack = nullptr;
  126. ENGINE->fakeMouseMove();
  127. }
  128. bool BattleActionsController::isActiveStackSpellcaster() const
  129. {
  130. const CStack * casterStack = owner.stacksController->getActiveStack();
  131. if (!casterStack)
  132. return false;
  133. bool spellcaster = casterStack->hasBonusOfType(BonusType::SPELLCASTER);
  134. return (spellcaster && casterStack->canCast());
  135. }
  136. void BattleActionsController::enterCreatureCastingMode()
  137. {
  138. //silently check for possible errors
  139. if (owner.tacticsMode)
  140. return;
  141. //hero is casting a spell
  142. if (heroSpellToCast)
  143. return;
  144. if (!owner.stacksController->getActiveStack())
  145. return;
  146. if(owner.getBattle()->battleCanTargetEmptyHex(owner.stacksController->getActiveStack()))
  147. {
  148. auto actionFilterPredicate = [](const PossiblePlayerBattleAction x)
  149. {
  150. return x.get() != PossiblePlayerBattleAction::SHOOT;
  151. };
  152. vstd::erase_if(possibleActions, actionFilterPredicate);
  153. ENGINE->fakeMouseMove();
  154. return;
  155. }
  156. if (!isActiveStackSpellcaster())
  157. return;
  158. for(const auto & action : possibleActions)
  159. {
  160. if (action.get() != PossiblePlayerBattleAction::NO_LOCATION)
  161. continue;
  162. const spells::Caster * caster = owner.stacksController->getActiveStack();
  163. const CSpell * spell = action.spell().toSpell();
  164. spells::Target target;
  165. target.emplace_back();
  166. spells::BattleCast cast(owner.getBattle().get(), caster, spells::Mode::CREATURE_ACTIVE, spell);
  167. auto m = spell->battleMechanics(&cast);
  168. spells::detail::ProblemImpl ignored;
  169. const bool isCastingPossible = m->canBeCastAt(target, ignored);
  170. if (isCastingPossible)
  171. {
  172. owner.giveCommand(EActionType::MONSTER_SPELL, BattleHex::INVALID, spell->getId());
  173. selectedStack = nullptr;
  174. ENGINE->cursor().set(Cursor::Combat::POINTER);
  175. }
  176. return;
  177. }
  178. possibleActions = getPossibleActionsForStack(owner.stacksController->getActiveStack());
  179. auto actionFilterPredicate = [](const PossiblePlayerBattleAction x)
  180. {
  181. return !x.spellcast();
  182. };
  183. vstd::erase_if(possibleActions, actionFilterPredicate);
  184. ENGINE->fakeMouseMove();
  185. }
  186. std::vector<PossiblePlayerBattleAction> BattleActionsController::getPossibleActionsForStack(const CStack *stack) const
  187. {
  188. BattleClientInterfaceData data; //hard to get rid of these things so for now they're required data to pass
  189. for(const auto & spell : creatureSpells)
  190. data.creatureSpellsToCast.push_back(spell->id);
  191. data.tacticsMode = owner.tacticsMode;
  192. auto allActions = owner.getBattle()->getClientActionsForStack(stack, data);
  193. allActions.push_back(PossiblePlayerBattleAction::HERO_INFO);
  194. allActions.push_back(PossiblePlayerBattleAction::CREATURE_INFO);
  195. return std::vector<PossiblePlayerBattleAction>(allActions);
  196. }
  197. void BattleActionsController::reorderPossibleActionsPriority(const CStack * stack, const CStack * targetStack)
  198. {
  199. if(owner.tacticsMode || possibleActions.empty()) return; //this function is not supposed to be called in tactics mode or before getPossibleActionsForStack
  200. auto assignPriority = [&](const PossiblePlayerBattleAction & item
  201. ) -> uint8_t //large lambda assigning priority which would have to be part of possibleActions without it
  202. {
  203. switch(item.get())
  204. {
  205. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  206. case PossiblePlayerBattleAction::ANY_LOCATION:
  207. case PossiblePlayerBattleAction::NO_LOCATION:
  208. case PossiblePlayerBattleAction::FREE_LOCATION:
  209. case PossiblePlayerBattleAction::OBSTACLE:
  210. if(!stack->hasBonusOfType(BonusType::NO_SPELLCAST_BY_DEFAULT) && targetStack != nullptr)
  211. {
  212. PlayerColor stackOwner = owner.getBattle()->battleGetOwner(targetStack);
  213. bool enemyTargetingPositiveSpellcast = item.spell().toSpell()->isPositive() && stackOwner != owner.curInt->playerID;
  214. bool friendTargetingNegativeSpellcast = item.spell().toSpell()->isNegative() && stackOwner == owner.curInt->playerID;
  215. if(!enemyTargetingPositiveSpellcast && !friendTargetingNegativeSpellcast)
  216. return 1;
  217. }
  218. return 100; //bottom priority
  219. break;
  220. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL:
  221. return 2;
  222. break;
  223. case PossiblePlayerBattleAction::SHOOT:
  224. if(targetStack == nullptr || targetStack->unitSide() == stack->unitSide() || !targetStack->alive())
  225. return 100; //bottom priority
  226. return 4;
  227. break;
  228. case PossiblePlayerBattleAction::ATTACK_AND_RETURN:
  229. return 5;
  230. break;
  231. case PossiblePlayerBattleAction::ATTACK:
  232. return 6;
  233. break;
  234. case PossiblePlayerBattleAction::WALK_AND_ATTACK:
  235. return 7;
  236. break;
  237. case PossiblePlayerBattleAction::MOVE_STACK:
  238. return 8;
  239. break;
  240. case PossiblePlayerBattleAction::CATAPULT:
  241. return 9;
  242. break;
  243. case PossiblePlayerBattleAction::HEAL:
  244. return 10;
  245. break;
  246. case PossiblePlayerBattleAction::CREATURE_INFO:
  247. return 11;
  248. break;
  249. case PossiblePlayerBattleAction::HERO_INFO:
  250. return 12;
  251. break;
  252. case PossiblePlayerBattleAction::TELEPORT:
  253. return 13;
  254. break;
  255. default:
  256. assert(0);
  257. return 200;
  258. break;
  259. }
  260. };
  261. auto comparer = [&](const PossiblePlayerBattleAction & lhs, const PossiblePlayerBattleAction & rhs)
  262. {
  263. return assignPriority(lhs) < assignPriority(rhs);
  264. };
  265. std::sort(possibleActions.begin(), possibleActions.end(), comparer);
  266. }
  267. void BattleActionsController::castThisSpell(SpellID spellID)
  268. {
  269. heroSpellToCast = std::make_shared<BattleAction>();
  270. heroSpellToCast->actionType = EActionType::HERO_SPELL;
  271. heroSpellToCast->spell = spellID;
  272. heroSpellToCast->stackNumber = -1;
  273. heroSpellToCast->side = owner.curInt->cb->getBattle(owner.getBattleID())->battleGetMySide();
  274. //choosing possible targets
  275. const CGHeroInstance *castingHero = (owner.attackingHeroInstance->tempOwner == owner.curInt->playerID) ? owner.attackingHeroInstance : owner.defendingHeroInstance;
  276. assert(castingHero); // code below assumes non-null hero
  277. PossiblePlayerBattleAction spellSelMode = owner.getBattle()->getCasterAction(spellID.toSpell(), castingHero, spells::Mode::HERO);
  278. if (spellSelMode.get() == PossiblePlayerBattleAction::NO_LOCATION) //user does not have to select location
  279. {
  280. heroSpellToCast->aimToHex(BattleHex::INVALID);
  281. owner.curInt->cb->battleMakeSpellAction(owner.getBattleID(), *heroSpellToCast);
  282. endCastingSpell();
  283. }
  284. else
  285. {
  286. possibleActions.clear();
  287. possibleActions.push_back (spellSelMode); //only this one action can be performed at the moment
  288. ENGINE->fakeMouseMove();//update cursor
  289. }
  290. owner.windowObject->blockUI(true);
  291. }
  292. const CSpell * BattleActionsController::getHeroSpellToCast( ) const
  293. {
  294. if (heroSpellToCast)
  295. return heroSpellToCast->spell.toSpell();
  296. return nullptr;
  297. }
  298. const CSpell * BattleActionsController::getStackSpellToCast(const BattleHex & hoveredHex)
  299. {
  300. if (heroSpellToCast)
  301. return nullptr;
  302. if (!owner.stacksController->getActiveStack())
  303. return nullptr;
  304. if (!hoveredHex.isValid())
  305. return nullptr;
  306. auto action = selectAction(hoveredHex);
  307. if(owner.stacksController->getActiveStack()->hasBonusOfType(BonusType::SPELL_LIKE_ATTACK))
  308. {
  309. auto bonus = owner.stacksController->getActiveStack()->getBonus(Selector::type()(BonusType::SPELL_LIKE_ATTACK));
  310. return bonus->subtype.as<SpellID>().toSpell();
  311. }
  312. if (action.spell() == SpellID::NONE)
  313. return nullptr;
  314. return action.spell().toSpell();
  315. }
  316. const CSpell * BattleActionsController::getCurrentSpell(const BattleHex & hoveredHex)
  317. {
  318. if (getHeroSpellToCast())
  319. return getHeroSpellToCast();
  320. return getStackSpellToCast(hoveredHex);
  321. }
  322. const CStack * BattleActionsController::getStackForHex(const BattleHex & hoveredHex)
  323. {
  324. const CStack * shere = owner.getBattle()->battleGetStackByPos(hoveredHex, true);
  325. if(shere)
  326. return shere;
  327. return owner.getBattle()->battleGetStackByPos(hoveredHex, false);
  328. }
  329. void BattleActionsController::actionSetCursor(PossiblePlayerBattleAction action, const BattleHex & targetHex)
  330. {
  331. switch (action.get())
  332. {
  333. case PossiblePlayerBattleAction::CHOOSE_TACTICS_STACK:
  334. ENGINE->cursor().set(Cursor::Combat::POINTER);
  335. return;
  336. case PossiblePlayerBattleAction::MOVE_TACTICS:
  337. case PossiblePlayerBattleAction::MOVE_STACK:
  338. if (owner.stacksController->getActiveStack()->hasBonusOfType(BonusType::FLYING))
  339. ENGINE->cursor().set(Cursor::Combat::FLY);
  340. else
  341. ENGINE->cursor().set(Cursor::Combat::MOVE);
  342. return;
  343. case PossiblePlayerBattleAction::ATTACK:
  344. case PossiblePlayerBattleAction::WALK_AND_ATTACK:
  345. case PossiblePlayerBattleAction::ATTACK_AND_RETURN:
  346. {
  347. static const std::map<BattleHex::EDir, Cursor::Combat> sectorCursor = {
  348. {BattleHex::TOP_LEFT, Cursor::Combat::HIT_SOUTHEAST},
  349. {BattleHex::TOP_RIGHT, Cursor::Combat::HIT_SOUTHWEST},
  350. {BattleHex::RIGHT, Cursor::Combat::HIT_WEST },
  351. {BattleHex::BOTTOM_RIGHT, Cursor::Combat::HIT_NORTHWEST},
  352. {BattleHex::BOTTOM_LEFT, Cursor::Combat::HIT_NORTHEAST},
  353. {BattleHex::LEFT, Cursor::Combat::HIT_EAST },
  354. {BattleHex::TOP, Cursor::Combat::HIT_SOUTH },
  355. {BattleHex::BOTTOM, Cursor::Combat::HIT_NORTH }
  356. };
  357. auto direction = owner.fieldController->selectAttackDirection(targetHex);
  358. assert(sectorCursor.count(direction) > 0);
  359. if (sectorCursor.count(direction))
  360. ENGINE->cursor().set(sectorCursor.at(direction));
  361. return;
  362. }
  363. case PossiblePlayerBattleAction::SHOOT:
  364. if (owner.getBattle()->battleHasShootingPenalty(owner.stacksController->getActiveStack(), targetHex))
  365. ENGINE->cursor().set(Cursor::Combat::SHOOT_PENALTY);
  366. else
  367. ENGINE->cursor().set(Cursor::Combat::SHOOT);
  368. return;
  369. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  370. case PossiblePlayerBattleAction::ANY_LOCATION:
  371. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL:
  372. case PossiblePlayerBattleAction::FREE_LOCATION:
  373. case PossiblePlayerBattleAction::OBSTACLE:
  374. ENGINE->cursor().set(Cursor::Spellcast::SPELL);
  375. return;
  376. case PossiblePlayerBattleAction::TELEPORT:
  377. ENGINE->cursor().set(Cursor::Combat::TELEPORT);
  378. return;
  379. case PossiblePlayerBattleAction::SACRIFICE:
  380. ENGINE->cursor().set(Cursor::Combat::SACRIFICE);
  381. return;
  382. case PossiblePlayerBattleAction::HEAL:
  383. ENGINE->cursor().set(Cursor::Combat::HEAL);
  384. return;
  385. case PossiblePlayerBattleAction::CATAPULT:
  386. ENGINE->cursor().set(Cursor::Combat::SHOOT_CATAPULT);
  387. return;
  388. case PossiblePlayerBattleAction::CREATURE_INFO:
  389. ENGINE->cursor().set(Cursor::Combat::QUERY);
  390. return;
  391. case PossiblePlayerBattleAction::HERO_INFO:
  392. ENGINE->cursor().set(Cursor::Combat::HERO);
  393. return;
  394. }
  395. assert(0);
  396. }
  397. void BattleActionsController::actionSetCursorBlocked(PossiblePlayerBattleAction action, const BattleHex & targetHex)
  398. {
  399. switch (action.get())
  400. {
  401. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  402. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL:
  403. case PossiblePlayerBattleAction::TELEPORT:
  404. case PossiblePlayerBattleAction::SACRIFICE:
  405. case PossiblePlayerBattleAction::FREE_LOCATION:
  406. ENGINE->cursor().set(Cursor::Combat::BLOCKED);
  407. return;
  408. default:
  409. if (targetHex == -1)
  410. ENGINE->cursor().set(Cursor::Combat::POINTER);
  411. else
  412. ENGINE->cursor().set(Cursor::Combat::BLOCKED);
  413. return;
  414. }
  415. assert(0);
  416. }
  417. std::string BattleActionsController::actionGetStatusMessage(PossiblePlayerBattleAction action, const BattleHex & targetHex)
  418. {
  419. const CStack * targetStack = getStackForHex(targetHex);
  420. switch (action.get()) //display console message, realize selected action
  421. {
  422. case PossiblePlayerBattleAction::CHOOSE_TACTICS_STACK:
  423. return (boost::format(LIBRARY->generaltexth->allTexts[481]) % targetStack->getName()).str(); //Select %s
  424. case PossiblePlayerBattleAction::MOVE_TACTICS:
  425. case PossiblePlayerBattleAction::MOVE_STACK:
  426. if (owner.stacksController->getActiveStack()->hasBonusOfType(BonusType::FLYING))
  427. return (boost::format(LIBRARY->generaltexth->allTexts[295]) % owner.stacksController->getActiveStack()->getName()).str(); //Fly %s here
  428. else
  429. return (boost::format(LIBRARY->generaltexth->allTexts[294]) % owner.stacksController->getActiveStack()->getName()).str(); //Move %s here
  430. case PossiblePlayerBattleAction::ATTACK:
  431. case PossiblePlayerBattleAction::WALK_AND_ATTACK:
  432. case PossiblePlayerBattleAction::ATTACK_AND_RETURN: //TODO: allow to disable return
  433. {
  434. const auto * attacker = owner.stacksController->getActiveStack();
  435. BattleHex attackFromHex = owner.fieldController->fromWhichHexAttack(targetHex);
  436. int distance = attacker->position.isValid() ? owner.getBattle()->battleGetDistances(attacker, attacker->getPosition())[attackFromHex.toInt()] : 0;
  437. DamageEstimation retaliation;
  438. BattleAttackInfo attackInfo(attacker, targetStack, distance, false );
  439. DamageEstimation estimation = owner.getBattle()->battleEstimateDamage(attackInfo, &retaliation);
  440. estimation.kills.max = std::min<int64_t>(estimation.kills.max, targetStack->getCount());
  441. estimation.kills.min = std::min<int64_t>(estimation.kills.min, targetStack->getCount());
  442. bool enemyMayBeKilled = estimation.kills.max == targetStack->getCount();
  443. return formatMeleeAttack(estimation, targetStack->getName()) + "\n" + formatRetaliation(retaliation, enemyMayBeKilled);
  444. }
  445. case PossiblePlayerBattleAction::SHOOT:
  446. {
  447. if(targetStack == nullptr) //should be true only for spell-like attack
  448. {
  449. auto spellLikeAttackBonus = owner.stacksController->getActiveStack()->getBonus(Selector::type()(BonusType::SPELL_LIKE_ATTACK));
  450. assert(spellLikeAttackBonus != nullptr);
  451. return boost::str(boost::format(LIBRARY->generaltexth->allTexts[26]) % spellLikeAttackBonus->subtype.as<SpellID>().toSpell()->getNameTranslated());
  452. }
  453. const auto * shooter = owner.stacksController->getActiveStack();
  454. DamageEstimation retaliation;
  455. BattleAttackInfo attackInfo(shooter, targetStack, 0, true );
  456. DamageEstimation estimation = owner.getBattle()->battleEstimateDamage(attackInfo, &retaliation);
  457. estimation.kills.max = std::min<int64_t>(estimation.kills.max, targetStack->getCount());
  458. estimation.kills.min = std::min<int64_t>(estimation.kills.min, targetStack->getCount());
  459. return formatRangedAttack(estimation, targetStack->getName(), shooter->shots.available());
  460. }
  461. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  462. return boost::str(boost::format(LIBRARY->generaltexth->allTexts[27]) % action.spell().toSpell()->getNameTranslated() % targetStack->getName()); //Cast %s on %s
  463. case PossiblePlayerBattleAction::ANY_LOCATION:
  464. return boost::str(boost::format(LIBRARY->generaltexth->allTexts[26]) % action.spell().toSpell()->getNameTranslated()); //Cast %s
  465. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL: //we assume that teleport / sacrifice will never be available as random spell
  466. return boost::str(boost::format(LIBRARY->generaltexth->allTexts[301]) % targetStack->getName()); //Cast a spell on %
  467. case PossiblePlayerBattleAction::TELEPORT:
  468. return LIBRARY->generaltexth->allTexts[25]; //Teleport Here
  469. case PossiblePlayerBattleAction::OBSTACLE:
  470. return LIBRARY->generaltexth->allTexts[550];
  471. case PossiblePlayerBattleAction::SACRIFICE:
  472. return (boost::format(LIBRARY->generaltexth->allTexts[549]) % targetStack->getName()).str(); //sacrifice the %s
  473. case PossiblePlayerBattleAction::FREE_LOCATION:
  474. return boost::str(boost::format(LIBRARY->generaltexth->allTexts[26]) % action.spell().toSpell()->getNameTranslated()); //Cast %s
  475. case PossiblePlayerBattleAction::HEAL:
  476. return (boost::format(LIBRARY->generaltexth->allTexts[419]) % targetStack->getName()).str(); //Apply first aid to the %s
  477. case PossiblePlayerBattleAction::CATAPULT:
  478. return ""; // TODO
  479. case PossiblePlayerBattleAction::CREATURE_INFO:
  480. return (boost::format(LIBRARY->generaltexth->allTexts[297]) % targetStack->getName()).str();
  481. case PossiblePlayerBattleAction::HERO_INFO:
  482. return LIBRARY->generaltexth->translate("core.genrltxt.417"); // "View Hero Stats"
  483. }
  484. assert(0);
  485. return "";
  486. }
  487. std::string BattleActionsController::actionGetStatusMessageBlocked(PossiblePlayerBattleAction action, const BattleHex & targetHex)
  488. {
  489. switch (action.get())
  490. {
  491. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  492. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL:
  493. return LIBRARY->generaltexth->allTexts[23];
  494. break;
  495. case PossiblePlayerBattleAction::TELEPORT:
  496. return LIBRARY->generaltexth->allTexts[24]; //Invalid Teleport Destination
  497. break;
  498. case PossiblePlayerBattleAction::SACRIFICE:
  499. return LIBRARY->generaltexth->allTexts[543]; //choose army to sacrifice
  500. break;
  501. case PossiblePlayerBattleAction::FREE_LOCATION:
  502. return boost::str(boost::format(LIBRARY->generaltexth->allTexts[181]) % action.spell().toSpell()->getNameTranslated()); //No room to place %s here
  503. break;
  504. default:
  505. return "";
  506. }
  507. }
  508. bool BattleActionsController::actionIsLegal(PossiblePlayerBattleAction action, const BattleHex & targetHex)
  509. {
  510. const CStack * targetStack = getStackForHex(targetHex);
  511. bool targetStackOwned = targetStack && targetStack->unitOwner() == owner.curInt->playerID;
  512. switch (action.get())
  513. {
  514. case PossiblePlayerBattleAction::CHOOSE_TACTICS_STACK:
  515. return (targetStack && targetStackOwned && targetStack->getMovementRange() > 0);
  516. case PossiblePlayerBattleAction::CREATURE_INFO:
  517. return (targetStack && targetStackOwned && targetStack->alive());
  518. case PossiblePlayerBattleAction::HERO_INFO:
  519. if (targetHex == BattleHex::HERO_ATTACKER)
  520. return owner.attackingHero != nullptr;
  521. if (targetHex == BattleHex::HERO_DEFENDER)
  522. return owner.defendingHero != nullptr;
  523. return false;
  524. case PossiblePlayerBattleAction::MOVE_TACTICS:
  525. case PossiblePlayerBattleAction::MOVE_STACK:
  526. if (!(targetStack && targetStack->alive())) //we can walk on dead stacks
  527. {
  528. if(canStackMoveHere(owner.stacksController->getActiveStack(), targetHex))
  529. return true;
  530. }
  531. return false;
  532. case PossiblePlayerBattleAction::ATTACK:
  533. case PossiblePlayerBattleAction::WALK_AND_ATTACK:
  534. case PossiblePlayerBattleAction::ATTACK_AND_RETURN:
  535. if(owner.getBattle()->battleCanAttack(owner.stacksController->getActiveStack(), targetStack, targetHex))
  536. {
  537. if (owner.fieldController->isTileAttackable(targetHex)) // move isTileAttackable to be part of battleCanAttack?
  538. return true;
  539. }
  540. return false;
  541. case PossiblePlayerBattleAction::SHOOT:
  542. {
  543. auto currentStack = owner.stacksController->getActiveStack();
  544. if(!owner.getBattle()->battleCanShoot(currentStack, targetHex))
  545. return false;
  546. if(targetStack == nullptr && owner.getBattle()->battleCanTargetEmptyHex(currentStack))
  547. {
  548. auto spellLikeAttackBonus = currentStack->getBonus(Selector::type()(BonusType::SPELL_LIKE_ATTACK));
  549. const CSpell * spellDataToCheck = spellLikeAttackBonus->subtype.as<SpellID>().toSpell();
  550. return isCastingPossibleHere(spellDataToCheck, nullptr, targetHex);
  551. }
  552. return true;
  553. }
  554. case PossiblePlayerBattleAction::NO_LOCATION:
  555. return false;
  556. case PossiblePlayerBattleAction::ANY_LOCATION:
  557. return isCastingPossibleHere(action.spell().toSpell(), nullptr, targetHex);
  558. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  559. return !selectedStack && targetStack && isCastingPossibleHere(action.spell().toSpell(), nullptr, targetHex);
  560. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL:
  561. if(targetStack && targetStackOwned && targetStack != owner.stacksController->getActiveStack() && targetStack->alive()) //only positive spells for other allied creatures
  562. {
  563. SpellID spellID = owner.getBattle()->getRandomBeneficialSpell(CRandomGenerator::getDefault(), owner.stacksController->getActiveStack(), targetStack);
  564. return spellID != SpellID::NONE;
  565. }
  566. return false;
  567. case PossiblePlayerBattleAction::TELEPORT:
  568. return selectedStack && isCastingPossibleHere(action.spell().toSpell(), selectedStack, targetHex);
  569. case PossiblePlayerBattleAction::SACRIFICE: //choose our living stack to sacrifice
  570. return targetStack && targetStack != selectedStack && targetStackOwned && targetStack->alive();
  571. case PossiblePlayerBattleAction::OBSTACLE:
  572. case PossiblePlayerBattleAction::FREE_LOCATION:
  573. return isCastingPossibleHere(action.spell().toSpell(), nullptr, targetHex);
  574. case PossiblePlayerBattleAction::CATAPULT:
  575. return owner.siegeController && owner.siegeController->isAttackableByCatapult(targetHex);
  576. case PossiblePlayerBattleAction::HEAL:
  577. return targetStack && targetStackOwned && targetStack->canBeHealed();
  578. }
  579. assert(0);
  580. return false;
  581. }
  582. void BattleActionsController::actionRealize(PossiblePlayerBattleAction action, const BattleHex & targetHex)
  583. {
  584. const CStack * targetStack = getStackForHex(targetHex);
  585. switch (action.get()) //display console message, realize selected action
  586. {
  587. case PossiblePlayerBattleAction::CHOOSE_TACTICS_STACK:
  588. {
  589. owner.stackActivated(targetStack);
  590. return;
  591. }
  592. case PossiblePlayerBattleAction::MOVE_TACTICS:
  593. case PossiblePlayerBattleAction::MOVE_STACK:
  594. {
  595. const auto * activeStack = owner.stacksController->getActiveStack();
  596. const bool backwardsMove = activeStack->unitSide() == BattleSide::ATTACKER ?
  597. targetHex.getX() < activeStack->getPosition().getX():
  598. targetHex.getX() > activeStack->getPosition().getX();
  599. if(activeStack->doubleWide() && backwardsMove)
  600. {
  601. BattleHexArray acc = owner.getBattle()->battleGetAvailableHexes(activeStack, false);
  602. BattleHex shiftedDest = targetHex.cloneInDirection(activeStack->destShiftDir(), false);
  603. if(acc.contains(shiftedDest))
  604. owner.giveCommand(EActionType::WALK, shiftedDest);
  605. else
  606. owner.giveCommand(EActionType::WALK, targetHex);
  607. }
  608. else
  609. {
  610. owner.giveCommand(EActionType::WALK, targetHex);
  611. }
  612. return;
  613. }
  614. case PossiblePlayerBattleAction::ATTACK:
  615. case PossiblePlayerBattleAction::WALK_AND_ATTACK:
  616. case PossiblePlayerBattleAction::ATTACK_AND_RETURN: //TODO: allow to disable return
  617. {
  618. bool returnAfterAttack = action.get() == PossiblePlayerBattleAction::ATTACK_AND_RETURN;
  619. BattleHex attackFromHex = owner.fieldController->fromWhichHexAttack(targetHex);
  620. if(attackFromHex.isValid()) //we can be in this line when unreachable creature is L - clicked (as of revision 1308)
  621. {
  622. BattleAction command = BattleAction::makeMeleeAttack(owner.stacksController->getActiveStack(), targetHex, attackFromHex, returnAfterAttack);
  623. owner.sendCommand(command, owner.stacksController->getActiveStack());
  624. }
  625. return;
  626. }
  627. case PossiblePlayerBattleAction::SHOOT:
  628. {
  629. owner.giveCommand(EActionType::SHOOT, targetHex);
  630. return;
  631. }
  632. case PossiblePlayerBattleAction::HEAL:
  633. {
  634. owner.giveCommand(EActionType::STACK_HEAL, targetHex);
  635. return;
  636. };
  637. case PossiblePlayerBattleAction::CATAPULT:
  638. {
  639. owner.giveCommand(EActionType::CATAPULT, targetHex);
  640. return;
  641. }
  642. case PossiblePlayerBattleAction::CREATURE_INFO:
  643. {
  644. ENGINE->windows().createAndPushWindow<CStackWindow>(targetStack, false);
  645. return;
  646. }
  647. case PossiblePlayerBattleAction::HERO_INFO:
  648. {
  649. if (targetHex == BattleHex::HERO_ATTACKER)
  650. owner.attackingHero->heroLeftClicked();
  651. if (targetHex == BattleHex::HERO_DEFENDER)
  652. owner.defendingHero->heroLeftClicked();
  653. return;
  654. }
  655. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  656. case PossiblePlayerBattleAction::ANY_LOCATION:
  657. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL: //we assume that teleport / sacrifice will never be available as random spell
  658. case PossiblePlayerBattleAction::TELEPORT:
  659. case PossiblePlayerBattleAction::OBSTACLE:
  660. case PossiblePlayerBattleAction::SACRIFICE:
  661. case PossiblePlayerBattleAction::FREE_LOCATION:
  662. {
  663. if (action.get() == PossiblePlayerBattleAction::AIMED_SPELL_CREATURE )
  664. {
  665. if (action.spell() == SpellID::SACRIFICE)
  666. {
  667. heroSpellToCast->aimToHex(targetHex);
  668. possibleActions.push_back({PossiblePlayerBattleAction::SACRIFICE, action.spell()});
  669. selectedStack = targetStack;
  670. return;
  671. }
  672. if (action.spell() == SpellID::TELEPORT)
  673. {
  674. heroSpellToCast->aimToUnit(targetStack);
  675. possibleActions.push_back({PossiblePlayerBattleAction::TELEPORT, action.spell()});
  676. selectedStack = targetStack;
  677. return;
  678. }
  679. }
  680. if (!heroSpellcastingModeActive())
  681. {
  682. if (action.spell().hasValue())
  683. {
  684. owner.giveCommand(EActionType::MONSTER_SPELL, targetHex, action.spell());
  685. }
  686. else //unknown random spell
  687. {
  688. owner.giveCommand(EActionType::MONSTER_SPELL, targetHex);
  689. }
  690. }
  691. else
  692. {
  693. assert(getHeroSpellToCast());
  694. switch (getHeroSpellToCast()->id.toEnum())
  695. {
  696. case SpellID::SACRIFICE:
  697. heroSpellToCast->aimToUnit(targetStack);//victim
  698. break;
  699. default:
  700. heroSpellToCast->aimToHex(targetHex);
  701. break;
  702. }
  703. owner.curInt->cb->battleMakeSpellAction(owner.getBattleID(), *heroSpellToCast);
  704. endCastingSpell();
  705. }
  706. selectedStack = nullptr;
  707. return;
  708. }
  709. }
  710. assert(0);
  711. return;
  712. }
  713. PossiblePlayerBattleAction BattleActionsController::selectAction(const BattleHex & targetHex)
  714. {
  715. assert(owner.stacksController->getActiveStack() != nullptr);
  716. assert(!possibleActions.empty());
  717. assert(targetHex.isValid());
  718. if (owner.stacksController->getActiveStack() == nullptr)
  719. return PossiblePlayerBattleAction::INVALID;
  720. if (possibleActions.empty())
  721. return PossiblePlayerBattleAction::INVALID;
  722. const CStack * targetStack = getStackForHex(targetHex);
  723. reorderPossibleActionsPriority(owner.stacksController->getActiveStack(), targetStack);
  724. for (PossiblePlayerBattleAction action : possibleActions)
  725. {
  726. if (actionIsLegal(action, targetHex))
  727. return action;
  728. }
  729. return possibleActions.front();
  730. }
  731. void BattleActionsController::onHexHovered(const BattleHex & hoveredHex)
  732. {
  733. if (owner.openingPlaying())
  734. {
  735. currentConsoleMsg = LIBRARY->generaltexth->translate("vcmi.battleWindow.pressKeyToSkipIntro");
  736. ENGINE->statusbar()->write(currentConsoleMsg);
  737. return;
  738. }
  739. if (owner.stacksController->getActiveStack() == nullptr)
  740. return;
  741. if (hoveredHex == BattleHex::INVALID)
  742. {
  743. if (!currentConsoleMsg.empty())
  744. ENGINE->statusbar()->clearIfMatching(currentConsoleMsg);
  745. currentConsoleMsg.clear();
  746. ENGINE->cursor().set(Cursor::Combat::BLOCKED);
  747. return;
  748. }
  749. auto action = selectAction(hoveredHex);
  750. std::string newConsoleMsg;
  751. if (actionIsLegal(action, hoveredHex))
  752. {
  753. actionSetCursor(action, hoveredHex);
  754. newConsoleMsg = actionGetStatusMessage(action, hoveredHex);
  755. }
  756. else
  757. {
  758. actionSetCursorBlocked(action, hoveredHex);
  759. newConsoleMsg = actionGetStatusMessageBlocked(action, hoveredHex);
  760. }
  761. if (!currentConsoleMsg.empty())
  762. ENGINE->statusbar()->clearIfMatching(currentConsoleMsg);
  763. if (!newConsoleMsg.empty())
  764. ENGINE->statusbar()->write(newConsoleMsg);
  765. currentConsoleMsg = newConsoleMsg;
  766. }
  767. void BattleActionsController::onHoverEnded()
  768. {
  769. ENGINE->cursor().set(Cursor::Combat::POINTER);
  770. if (!currentConsoleMsg.empty())
  771. ENGINE->statusbar()->clearIfMatching(currentConsoleMsg);
  772. currentConsoleMsg.clear();
  773. }
  774. void BattleActionsController::onHexLeftClicked(const BattleHex & clickedHex)
  775. {
  776. if (owner.stacksController->getActiveStack() == nullptr)
  777. return;
  778. auto action = selectAction(clickedHex);
  779. std::string newConsoleMsg;
  780. if (!actionIsLegal(action, clickedHex))
  781. return;
  782. actionRealize(action, clickedHex);
  783. ENGINE->statusbar()->clear();
  784. }
  785. void BattleActionsController::tryActivateStackSpellcasting(const CStack *casterStack)
  786. {
  787. creatureSpells.clear();
  788. bool spellcaster = casterStack->hasBonusOfType(BonusType::SPELLCASTER);
  789. if(casterStack->canCast() && spellcaster)
  790. {
  791. // faerie dragon can cast only one, randomly selected spell until their next move
  792. //TODO: faerie dragon type spell should be selected by server
  793. const auto spellToCast = owner.getBattle()->getRandomCastedSpell(CRandomGenerator::getDefault(), casterStack);
  794. if (spellToCast.hasValue())
  795. creatureSpells.push_back(spellToCast.toSpell());
  796. }
  797. TConstBonusListPtr bl = casterStack->getBonusesOfType(BonusType::SPELLCASTER);
  798. for(const auto & bonus : *bl)
  799. {
  800. if (bonus->additionalInfo[0] <= 0 && bonus->subtype.as<SpellID>().hasValue())
  801. creatureSpells.push_back(bonus->subtype.as<SpellID>().toSpell());
  802. }
  803. }
  804. const spells::Caster * BattleActionsController::getCurrentSpellcaster() const
  805. {
  806. if (heroSpellToCast)
  807. return owner.currentHero();
  808. else
  809. return owner.stacksController->getActiveStack();
  810. }
  811. spells::Mode BattleActionsController::getCurrentCastMode() const
  812. {
  813. if (heroSpellToCast)
  814. return spells::Mode::HERO;
  815. else
  816. return spells::Mode::CREATURE_ACTIVE;
  817. }
  818. bool BattleActionsController::isCastingPossibleHere(const CSpell * currentSpell, const CStack *targetStack, const BattleHex & targetHex)
  819. {
  820. assert(currentSpell);
  821. if (!currentSpell)
  822. return false;
  823. auto caster = getCurrentSpellcaster();
  824. const spells::Mode mode = heroSpellToCast ? spells::Mode::HERO : spells::Mode::CREATURE_ACTIVE;
  825. spells::Target target;
  826. if(targetStack)
  827. target.emplace_back(targetStack);
  828. target.emplace_back(targetHex);
  829. spells::BattleCast cast(owner.getBattle().get(), caster, mode, currentSpell);
  830. auto m = currentSpell->battleMechanics(&cast);
  831. spells::detail::ProblemImpl problem; //todo: display problem in status bar
  832. return m->canBeCastAt(target, problem);
  833. }
  834. bool BattleActionsController::canStackMoveHere(const CStack * stackToMove, const BattleHex & myNumber) const
  835. {
  836. BattleHexArray acc = owner.getBattle()->battleGetAvailableHexes(stackToMove, false);
  837. BattleHex shiftedDest = myNumber.cloneInDirection(stackToMove->destShiftDir(), false);
  838. if (acc.contains(myNumber))
  839. return true;
  840. else if (stackToMove->doubleWide() && acc.contains(shiftedDest))
  841. return true;
  842. else
  843. return false;
  844. }
  845. void BattleActionsController::activateStack()
  846. {
  847. const CStack * s = owner.stacksController->getActiveStack();
  848. if(s)
  849. {
  850. tryActivateStackSpellcasting(s);
  851. possibleActions = getPossibleActionsForStack(s);
  852. std::list<PossiblePlayerBattleAction> actionsToSelect;
  853. if(!possibleActions.empty())
  854. {
  855. auto primaryAction = possibleActions.front().get();
  856. if(primaryAction == PossiblePlayerBattleAction::SHOOT || primaryAction == PossiblePlayerBattleAction::AIMED_SPELL_CREATURE
  857. || primaryAction == PossiblePlayerBattleAction::ANY_LOCATION || primaryAction == PossiblePlayerBattleAction::ATTACK_AND_RETURN)
  858. {
  859. actionsToSelect.push_back(possibleActions.front());
  860. auto shootActionPredicate = [](const PossiblePlayerBattleAction& action)
  861. {
  862. return action.get() == PossiblePlayerBattleAction::SHOOT;
  863. };
  864. bool hasShootSecondaryAction = std::any_of(possibleActions.begin() + 1, possibleActions.end(), shootActionPredicate);
  865. if(hasShootSecondaryAction) //casters may have shooting capabilities, for example storm elementals
  866. actionsToSelect.emplace_back(PossiblePlayerBattleAction::SHOOT);
  867. /* TODO: maybe it would also make sense to check spellcast as non-top priority action ("NO_SPELLCAST_BY_DEFAULT" bonus)?
  868. * it would require going beyond this "if" block for melee casters
  869. * F button helps, but some mod creatures may have that bonus and more than 1 castable spell */
  870. actionsToSelect.emplace_back(PossiblePlayerBattleAction::ATTACK); //always allow melee attack as last option
  871. }
  872. }
  873. owner.windowObject->setAlternativeActions(actionsToSelect);
  874. }
  875. }
  876. void BattleActionsController::onHexRightClicked(const BattleHex & clickedHex)
  877. {
  878. bool isCurrentStackInSpellcastMode = creatureSpellcastingModeActive();
  879. if (heroSpellcastingModeActive() || isCurrentStackInSpellcastMode)
  880. {
  881. endCastingSpell();
  882. CRClickPopup::createAndPush(LIBRARY->generaltexth->translate("core.genrltxt.731")); // spell cancelled
  883. return;
  884. }
  885. auto selectedStack = owner.getBattle()->battleGetStackByPos(clickedHex, true);
  886. if (selectedStack != nullptr)
  887. ENGINE->windows().createAndPushWindow<CStackWindow>(selectedStack, true);
  888. if (clickedHex == BattleHex::HERO_ATTACKER && owner.attackingHero)
  889. owner.attackingHero->heroRightClicked();
  890. if (clickedHex == BattleHex::HERO_DEFENDER && owner.defendingHero)
  891. owner.defendingHero->heroRightClicked();
  892. }
  893. bool BattleActionsController::heroSpellcastingModeActive() const
  894. {
  895. return heroSpellToCast != nullptr;
  896. }
  897. bool BattleActionsController::creatureSpellcastingModeActive() const
  898. {
  899. auto spellcastModePredicate = [](const PossiblePlayerBattleAction & action)
  900. {
  901. return action.spellcast() || action.get() == PossiblePlayerBattleAction::SHOOT; //for hotkey-eligible SPELL_LIKE_ATTACK creature should have only SHOOT action
  902. };
  903. return !possibleActions.empty() && std::all_of(possibleActions.begin(), possibleActions.end(), spellcastModePredicate);
  904. }
  905. bool BattleActionsController::currentActionSpellcasting(const BattleHex & hoveredHex)
  906. {
  907. if (heroSpellToCast)
  908. return true;
  909. if (!owner.stacksController->getActiveStack())
  910. return false;
  911. auto action = selectAction(hoveredHex);
  912. return action.spellcast();
  913. }
  914. const std::vector<PossiblePlayerBattleAction> & BattleActionsController::getPossibleActions() const
  915. {
  916. return possibleActions;
  917. }
  918. void BattleActionsController::removePossibleAction(PossiblePlayerBattleAction action)
  919. {
  920. vstd::erase(possibleActions, action);
  921. }
  922. void BattleActionsController::pushFrontPossibleAction(PossiblePlayerBattleAction action)
  923. {
  924. possibleActions.insert(possibleActions.begin(), action);
  925. }
  926. void BattleActionsController::resetCurrentStackPossibleActions()
  927. {
  928. possibleActions = getPossibleActionsForStack(owner.stacksController->getActiveStack());
  929. }