BattleActionsController.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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. if(owner.stacksController->getActiveStack()->doubleWide())
  596. {
  597. BattleHexArray acc = owner.getBattle()->battleGetAvailableHexes(owner.stacksController->getActiveStack(), false);
  598. BattleHex shiftedDest = targetHex.cloneInDirection(owner.stacksController->getActiveStack()->destShiftDir(), false);
  599. if(acc.contains(targetHex))
  600. owner.giveCommand(EActionType::WALK, targetHex);
  601. else if(acc.contains(shiftedDest))
  602. owner.giveCommand(EActionType::WALK, shiftedDest);
  603. }
  604. else
  605. {
  606. owner.giveCommand(EActionType::WALK, targetHex);
  607. }
  608. return;
  609. }
  610. case PossiblePlayerBattleAction::ATTACK:
  611. case PossiblePlayerBattleAction::WALK_AND_ATTACK:
  612. case PossiblePlayerBattleAction::ATTACK_AND_RETURN: //TODO: allow to disable return
  613. {
  614. bool returnAfterAttack = action.get() == PossiblePlayerBattleAction::ATTACK_AND_RETURN;
  615. BattleHex attackFromHex = owner.fieldController->fromWhichHexAttack(targetHex);
  616. if(attackFromHex.isValid()) //we can be in this line when unreachable creature is L - clicked (as of revision 1308)
  617. {
  618. BattleAction command = BattleAction::makeMeleeAttack(owner.stacksController->getActiveStack(), targetHex, attackFromHex, returnAfterAttack);
  619. owner.sendCommand(command, owner.stacksController->getActiveStack());
  620. }
  621. return;
  622. }
  623. case PossiblePlayerBattleAction::SHOOT:
  624. {
  625. owner.giveCommand(EActionType::SHOOT, targetHex);
  626. return;
  627. }
  628. case PossiblePlayerBattleAction::HEAL:
  629. {
  630. owner.giveCommand(EActionType::STACK_HEAL, targetHex);
  631. return;
  632. };
  633. case PossiblePlayerBattleAction::CATAPULT:
  634. {
  635. owner.giveCommand(EActionType::CATAPULT, targetHex);
  636. return;
  637. }
  638. case PossiblePlayerBattleAction::CREATURE_INFO:
  639. {
  640. ENGINE->windows().createAndPushWindow<CStackWindow>(targetStack, false);
  641. return;
  642. }
  643. case PossiblePlayerBattleAction::HERO_INFO:
  644. {
  645. if (targetHex == BattleHex::HERO_ATTACKER)
  646. owner.attackingHero->heroLeftClicked();
  647. if (targetHex == BattleHex::HERO_DEFENDER)
  648. owner.defendingHero->heroLeftClicked();
  649. return;
  650. }
  651. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  652. case PossiblePlayerBattleAction::ANY_LOCATION:
  653. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL: //we assume that teleport / sacrifice will never be available as random spell
  654. case PossiblePlayerBattleAction::TELEPORT:
  655. case PossiblePlayerBattleAction::OBSTACLE:
  656. case PossiblePlayerBattleAction::SACRIFICE:
  657. case PossiblePlayerBattleAction::FREE_LOCATION:
  658. {
  659. if (action.get() == PossiblePlayerBattleAction::AIMED_SPELL_CREATURE )
  660. {
  661. if (action.spell() == SpellID::SACRIFICE)
  662. {
  663. heroSpellToCast->aimToHex(targetHex);
  664. possibleActions.push_back({PossiblePlayerBattleAction::SACRIFICE, action.spell()});
  665. selectedStack = targetStack;
  666. return;
  667. }
  668. if (action.spell() == SpellID::TELEPORT)
  669. {
  670. heroSpellToCast->aimToUnit(targetStack);
  671. possibleActions.push_back({PossiblePlayerBattleAction::TELEPORT, action.spell()});
  672. selectedStack = targetStack;
  673. return;
  674. }
  675. }
  676. if (!heroSpellcastingModeActive())
  677. {
  678. if (action.spell().hasValue())
  679. {
  680. owner.giveCommand(EActionType::MONSTER_SPELL, targetHex, action.spell());
  681. }
  682. else //unknown random spell
  683. {
  684. owner.giveCommand(EActionType::MONSTER_SPELL, targetHex);
  685. }
  686. }
  687. else
  688. {
  689. assert(getHeroSpellToCast());
  690. switch (getHeroSpellToCast()->id.toEnum())
  691. {
  692. case SpellID::SACRIFICE:
  693. heroSpellToCast->aimToUnit(targetStack);//victim
  694. break;
  695. default:
  696. heroSpellToCast->aimToHex(targetHex);
  697. break;
  698. }
  699. owner.curInt->cb->battleMakeSpellAction(owner.getBattleID(), *heroSpellToCast);
  700. endCastingSpell();
  701. }
  702. selectedStack = nullptr;
  703. return;
  704. }
  705. }
  706. assert(0);
  707. return;
  708. }
  709. PossiblePlayerBattleAction BattleActionsController::selectAction(const BattleHex & targetHex)
  710. {
  711. assert(owner.stacksController->getActiveStack() != nullptr);
  712. assert(!possibleActions.empty());
  713. assert(targetHex.isValid());
  714. if (owner.stacksController->getActiveStack() == nullptr)
  715. return PossiblePlayerBattleAction::INVALID;
  716. if (possibleActions.empty())
  717. return PossiblePlayerBattleAction::INVALID;
  718. const CStack * targetStack = getStackForHex(targetHex);
  719. reorderPossibleActionsPriority(owner.stacksController->getActiveStack(), targetStack);
  720. for (PossiblePlayerBattleAction action : possibleActions)
  721. {
  722. if (actionIsLegal(action, targetHex))
  723. return action;
  724. }
  725. return possibleActions.front();
  726. }
  727. void BattleActionsController::onHexHovered(const BattleHex & hoveredHex)
  728. {
  729. if (owner.openingPlaying())
  730. {
  731. currentConsoleMsg = LIBRARY->generaltexth->translate("vcmi.battleWindow.pressKeyToSkipIntro");
  732. ENGINE->statusbar()->write(currentConsoleMsg);
  733. return;
  734. }
  735. if (owner.stacksController->getActiveStack() == nullptr)
  736. return;
  737. if (hoveredHex == BattleHex::INVALID)
  738. {
  739. if (!currentConsoleMsg.empty())
  740. ENGINE->statusbar()->clearIfMatching(currentConsoleMsg);
  741. currentConsoleMsg.clear();
  742. ENGINE->cursor().set(Cursor::Combat::BLOCKED);
  743. return;
  744. }
  745. auto action = selectAction(hoveredHex);
  746. std::string newConsoleMsg;
  747. if (actionIsLegal(action, hoveredHex))
  748. {
  749. actionSetCursor(action, hoveredHex);
  750. newConsoleMsg = actionGetStatusMessage(action, hoveredHex);
  751. }
  752. else
  753. {
  754. actionSetCursorBlocked(action, hoveredHex);
  755. newConsoleMsg = actionGetStatusMessageBlocked(action, hoveredHex);
  756. }
  757. if (!currentConsoleMsg.empty())
  758. ENGINE->statusbar()->clearIfMatching(currentConsoleMsg);
  759. if (!newConsoleMsg.empty())
  760. ENGINE->statusbar()->write(newConsoleMsg);
  761. currentConsoleMsg = newConsoleMsg;
  762. }
  763. void BattleActionsController::onHoverEnded()
  764. {
  765. ENGINE->cursor().set(Cursor::Combat::POINTER);
  766. if (!currentConsoleMsg.empty())
  767. ENGINE->statusbar()->clearIfMatching(currentConsoleMsg);
  768. currentConsoleMsg.clear();
  769. }
  770. void BattleActionsController::onHexLeftClicked(const BattleHex & clickedHex)
  771. {
  772. if (owner.stacksController->getActiveStack() == nullptr)
  773. return;
  774. auto action = selectAction(clickedHex);
  775. std::string newConsoleMsg;
  776. if (!actionIsLegal(action, clickedHex))
  777. return;
  778. actionRealize(action, clickedHex);
  779. ENGINE->statusbar()->clear();
  780. }
  781. void BattleActionsController::tryActivateStackSpellcasting(const CStack *casterStack)
  782. {
  783. creatureSpells.clear();
  784. bool spellcaster = casterStack->hasBonusOfType(BonusType::SPELLCASTER);
  785. if(casterStack->canCast() && spellcaster)
  786. {
  787. // faerie dragon can cast only one, randomly selected spell until their next move
  788. //TODO: faerie dragon type spell should be selected by server
  789. const auto spellToCast = owner.getBattle()->getRandomCastedSpell(CRandomGenerator::getDefault(), casterStack);
  790. if (spellToCast.hasValue())
  791. creatureSpells.push_back(spellToCast.toSpell());
  792. }
  793. TConstBonusListPtr bl = casterStack->getBonusesOfType(BonusType::SPELLCASTER);
  794. for(const auto & bonus : *bl)
  795. {
  796. if (bonus->additionalInfo[0] <= 0 && bonus->subtype.as<SpellID>().hasValue())
  797. creatureSpells.push_back(bonus->subtype.as<SpellID>().toSpell());
  798. }
  799. }
  800. const spells::Caster * BattleActionsController::getCurrentSpellcaster() const
  801. {
  802. if (heroSpellToCast)
  803. return owner.currentHero();
  804. else
  805. return owner.stacksController->getActiveStack();
  806. }
  807. spells::Mode BattleActionsController::getCurrentCastMode() const
  808. {
  809. if (heroSpellToCast)
  810. return spells::Mode::HERO;
  811. else
  812. return spells::Mode::CREATURE_ACTIVE;
  813. }
  814. bool BattleActionsController::isCastingPossibleHere(const CSpell * currentSpell, const CStack *targetStack, const BattleHex & targetHex)
  815. {
  816. assert(currentSpell);
  817. if (!currentSpell)
  818. return false;
  819. auto caster = getCurrentSpellcaster();
  820. const spells::Mode mode = heroSpellToCast ? spells::Mode::HERO : spells::Mode::CREATURE_ACTIVE;
  821. spells::Target target;
  822. if(targetStack)
  823. target.emplace_back(targetStack);
  824. target.emplace_back(targetHex);
  825. spells::BattleCast cast(owner.getBattle().get(), caster, mode, currentSpell);
  826. auto m = currentSpell->battleMechanics(&cast);
  827. spells::detail::ProblemImpl problem; //todo: display problem in status bar
  828. return m->canBeCastAt(target, problem);
  829. }
  830. bool BattleActionsController::canStackMoveHere(const CStack * stackToMove, const BattleHex & myNumber) const
  831. {
  832. BattleHexArray acc = owner.getBattle()->battleGetAvailableHexes(stackToMove, false);
  833. BattleHex shiftedDest = myNumber.cloneInDirection(stackToMove->destShiftDir(), false);
  834. if (acc.contains(myNumber))
  835. return true;
  836. else if (stackToMove->doubleWide() && acc.contains(shiftedDest))
  837. return true;
  838. else
  839. return false;
  840. }
  841. void BattleActionsController::activateStack()
  842. {
  843. const CStack * s = owner.stacksController->getActiveStack();
  844. if(s)
  845. {
  846. tryActivateStackSpellcasting(s);
  847. possibleActions = getPossibleActionsForStack(s);
  848. std::list<PossiblePlayerBattleAction> actionsToSelect;
  849. if(!possibleActions.empty())
  850. {
  851. auto primaryAction = possibleActions.front().get();
  852. if(primaryAction == PossiblePlayerBattleAction::SHOOT || primaryAction == PossiblePlayerBattleAction::AIMED_SPELL_CREATURE
  853. || primaryAction == PossiblePlayerBattleAction::ANY_LOCATION || primaryAction == PossiblePlayerBattleAction::ATTACK_AND_RETURN)
  854. {
  855. actionsToSelect.push_back(possibleActions.front());
  856. auto shootActionPredicate = [](const PossiblePlayerBattleAction& action)
  857. {
  858. return action.get() == PossiblePlayerBattleAction::SHOOT;
  859. };
  860. bool hasShootSecondaryAction = std::any_of(possibleActions.begin() + 1, possibleActions.end(), shootActionPredicate);
  861. if(hasShootSecondaryAction) //casters may have shooting capabilities, for example storm elementals
  862. actionsToSelect.emplace_back(PossiblePlayerBattleAction::SHOOT);
  863. /* TODO: maybe it would also make sense to check spellcast as non-top priority action ("NO_SPELLCAST_BY_DEFAULT" bonus)?
  864. * it would require going beyond this "if" block for melee casters
  865. * F button helps, but some mod creatures may have that bonus and more than 1 castable spell */
  866. actionsToSelect.emplace_back(PossiblePlayerBattleAction::ATTACK); //always allow melee attack as last option
  867. }
  868. }
  869. owner.windowObject->setAlternativeActions(actionsToSelect);
  870. }
  871. }
  872. void BattleActionsController::onHexRightClicked(const BattleHex & clickedHex)
  873. {
  874. bool isCurrentStackInSpellcastMode = creatureSpellcastingModeActive();
  875. if (heroSpellcastingModeActive() || isCurrentStackInSpellcastMode)
  876. {
  877. endCastingSpell();
  878. CRClickPopup::createAndPush(LIBRARY->generaltexth->translate("core.genrltxt.731")); // spell cancelled
  879. return;
  880. }
  881. auto selectedStack = owner.getBattle()->battleGetStackByPos(clickedHex, true);
  882. if (selectedStack != nullptr)
  883. ENGINE->windows().createAndPushWindow<CStackWindow>(selectedStack, true);
  884. if (clickedHex == BattleHex::HERO_ATTACKER && owner.attackingHero)
  885. owner.attackingHero->heroRightClicked();
  886. if (clickedHex == BattleHex::HERO_DEFENDER && owner.defendingHero)
  887. owner.defendingHero->heroRightClicked();
  888. }
  889. bool BattleActionsController::heroSpellcastingModeActive() const
  890. {
  891. return heroSpellToCast != nullptr;
  892. }
  893. bool BattleActionsController::creatureSpellcastingModeActive() const
  894. {
  895. auto spellcastModePredicate = [](const PossiblePlayerBattleAction & action)
  896. {
  897. return action.spellcast() || action.get() == PossiblePlayerBattleAction::SHOOT; //for hotkey-eligible SPELL_LIKE_ATTACK creature should have only SHOOT action
  898. };
  899. return !possibleActions.empty() && std::all_of(possibleActions.begin(), possibleActions.end(), spellcastModePredicate);
  900. }
  901. bool BattleActionsController::currentActionSpellcasting(const BattleHex & hoveredHex)
  902. {
  903. if (heroSpellToCast)
  904. return true;
  905. if (!owner.stacksController->getActiveStack())
  906. return false;
  907. auto action = selectAction(hoveredHex);
  908. return action.spellcast();
  909. }
  910. const std::vector<PossiblePlayerBattleAction> & BattleActionsController::getPossibleActions() const
  911. {
  912. return possibleActions;
  913. }
  914. void BattleActionsController::removePossibleAction(PossiblePlayerBattleAction action)
  915. {
  916. vstd::erase(possibleActions, action);
  917. }
  918. void BattleActionsController::pushFrontPossibleAction(PossiblePlayerBattleAction action)
  919. {
  920. possibleActions.insert(possibleActions.begin(), action);
  921. }
  922. void BattleActionsController::resetCurrentStackPossibleActions()
  923. {
  924. possibleActions = getPossibleActionsForStack(owner.stacksController->getActiveStack());
  925. }