BattleActionsController.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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 "../CGameInfo.h"
  19. #include "../CPlayerInterface.h"
  20. #include "../gui/CursorHandler.h"
  21. #include "../gui/CGuiHandler.h"
  22. #include "../gui/CIntObject.h"
  23. #include "../windows/CCreatureWindow.h"
  24. #include "../../CCallback.h"
  25. #include "../../lib/CStack.h"
  26. #include "../../lib/battle/BattleAction.h"
  27. #include "../../lib/spells/CSpellHandler.h"
  28. #include "../../lib/spells/ISpellMechanics.h"
  29. #include "../../lib/spells/Problem.h"
  30. #include "../../lib/CGeneralTextHandler.h"
  31. static std::string formatDmgRange(std::pair<ui32, ui32> dmgRange)
  32. {
  33. if (dmgRange.first != dmgRange.second)
  34. return (boost::format("%d - %d") % dmgRange.first % dmgRange.second).str();
  35. else
  36. return (boost::format("%d") % dmgRange.first).str();
  37. }
  38. BattleActionsController::BattleActionsController(BattleInterface & owner):
  39. owner(owner),
  40. heroSpellToCast(nullptr),
  41. creatureSpellToCast(nullptr)
  42. {}
  43. void BattleActionsController::endCastingSpell()
  44. {
  45. if(heroSpellToCast)
  46. heroSpellToCast.reset();
  47. if(owner.stacksController->getActiveStack())
  48. possibleActions = getPossibleActionsForStack(owner.stacksController->getActiveStack()); //restore actions after they were cleared
  49. GH.fakeMouseMove();
  50. }
  51. bool BattleActionsController::isActiveStackSpellcaster() const
  52. {
  53. const CStack * casterStack = owner.stacksController->getActiveStack();
  54. if (!casterStack)
  55. return false;
  56. const auto randomSpellcaster = casterStack->getBonusLocalFirst(Selector::type()(Bonus::SPELLCASTER));
  57. return (randomSpellcaster && casterStack->canCast());
  58. }
  59. void BattleActionsController::enterCreatureCastingMode()
  60. {
  61. //silently check for possible errors
  62. if (owner.tacticsMode)
  63. return;
  64. //hero is casting a spell
  65. if (heroSpellToCast)
  66. return;
  67. if (!owner.stacksController->getActiveStack())
  68. return;
  69. if (!isActiveStackSpellcaster())
  70. return;
  71. if (vstd::contains(possibleActions, PossiblePlayerBattleAction::NO_LOCATION))
  72. {
  73. const spells::Caster * caster = owner.stacksController->getActiveStack();
  74. const CSpell * spell = getStackSpellToCast();
  75. spells::Target target;
  76. target.emplace_back();
  77. spells::BattleCast cast(owner.curInt->cb.get(), caster, spells::Mode::CREATURE_ACTIVE, spell);
  78. auto m = spell->battleMechanics(&cast);
  79. spells::detail::ProblemImpl ignored;
  80. const bool isCastingPossible = m->canBeCastAt(target, ignored);
  81. if (isCastingPossible)
  82. {
  83. owner.giveCommand(EActionType::MONSTER_SPELL, BattleHex::INVALID, spell->getId());
  84. owner.stacksController->setSelectedStack(nullptr);
  85. CCS->curh->set(Cursor::Combat::POINTER);
  86. }
  87. }
  88. else
  89. {
  90. possibleActions = getPossibleActionsForStack(owner.stacksController->getActiveStack());
  91. auto actionFilterPredicate = [](const PossiblePlayerBattleAction x)
  92. {
  93. return (x != PossiblePlayerBattleAction::ANY_LOCATION) && (x != PossiblePlayerBattleAction::NO_LOCATION) &&
  94. (x != PossiblePlayerBattleAction::FREE_LOCATION) && (x != PossiblePlayerBattleAction::AIMED_SPELL_CREATURE) &&
  95. (x != PossiblePlayerBattleAction::OBSTACLE);
  96. };
  97. vstd::erase_if(possibleActions, actionFilterPredicate);
  98. GH.fakeMouseMove();
  99. }
  100. }
  101. std::vector<PossiblePlayerBattleAction> BattleActionsController::getPossibleActionsForStack(const CStack *stack) const
  102. {
  103. BattleClientInterfaceData data; //hard to get rid of these things so for now they're required data to pass
  104. if (getStackSpellToCast())
  105. data.creatureSpellToCast = getStackSpellToCast()->getId();
  106. else
  107. data.creatureSpellToCast = SpellID::NONE;
  108. data.tacticsMode = owner.tacticsMode;
  109. auto allActions = owner.curInt->cb->getClientActionsForStack(stack, data);
  110. allActions.push_back(PossiblePlayerBattleAction::CREATURE_INFO);
  111. return std::vector<PossiblePlayerBattleAction>(allActions);
  112. }
  113. void BattleActionsController::reorderPossibleActionsPriority(const CStack * stack, MouseHoveredHexContext context)
  114. {
  115. if(owner.tacticsMode || possibleActions.empty()) return; //this function is not supposed to be called in tactics mode or before getPossibleActionsForStack
  116. auto assignPriority = [&](PossiblePlayerBattleAction const & item) -> uint8_t //large lambda assigning priority which would have to be part of possibleActions without it
  117. {
  118. switch(item)
  119. {
  120. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  121. case PossiblePlayerBattleAction::ANY_LOCATION:
  122. case PossiblePlayerBattleAction::NO_LOCATION:
  123. case PossiblePlayerBattleAction::FREE_LOCATION:
  124. case PossiblePlayerBattleAction::OBSTACLE:
  125. if(!stack->hasBonusOfType(Bonus::NO_SPELLCAST_BY_DEFAULT) && context == MouseHoveredHexContext::OCCUPIED_HEX)
  126. return 1;
  127. else
  128. return 100;//bottom priority
  129. break;
  130. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL:
  131. return 2; break;
  132. case PossiblePlayerBattleAction::SHOOT:
  133. return 4; break;
  134. case PossiblePlayerBattleAction::ATTACK_AND_RETURN:
  135. return 5; break;
  136. case PossiblePlayerBattleAction::ATTACK:
  137. return 6; break;
  138. case PossiblePlayerBattleAction::WALK_AND_ATTACK:
  139. return 7; break;
  140. case PossiblePlayerBattleAction::MOVE_STACK:
  141. return 8; break;
  142. case PossiblePlayerBattleAction::CATAPULT:
  143. return 9; break;
  144. case PossiblePlayerBattleAction::HEAL:
  145. return 10; break;
  146. case PossiblePlayerBattleAction::CREATURE_INFO:
  147. return 11; break;
  148. case PossiblePlayerBattleAction::TELEPORT:
  149. return 12; break;
  150. default:
  151. assert(0);
  152. return 200; break;
  153. }
  154. };
  155. auto comparer = [&](PossiblePlayerBattleAction const & lhs, PossiblePlayerBattleAction const & rhs)
  156. {
  157. return assignPriority(lhs) < assignPriority(rhs);
  158. };
  159. std::sort(possibleActions.begin(), possibleActions.end(), comparer);
  160. }
  161. void BattleActionsController::castThisSpell(SpellID spellID)
  162. {
  163. heroSpellToCast = std::make_shared<BattleAction>();
  164. heroSpellToCast->actionType = EActionType::HERO_SPELL;
  165. heroSpellToCast->actionSubtype = spellID; //spell number
  166. heroSpellToCast->stackNumber = (owner.attackingHeroInstance->tempOwner == owner.curInt->playerID) ? -1 : -2;
  167. heroSpellToCast->side = owner.defendingHeroInstance ? (owner.curInt->playerID == owner.defendingHeroInstance->tempOwner) : false;
  168. //choosing possible targets
  169. const CGHeroInstance *castingHero = (owner.attackingHeroInstance->tempOwner == owner.curInt->playerID) ? owner.attackingHeroInstance : owner.defendingHeroInstance;
  170. assert(castingHero); // code below assumes non-null hero
  171. PossiblePlayerBattleAction spellSelMode = owner.curInt->cb->getCasterAction(spellID.toSpell(), castingHero, spells::Mode::HERO);
  172. if (spellSelMode == PossiblePlayerBattleAction::NO_LOCATION) //user does not have to select location
  173. {
  174. heroSpellToCast->aimToHex(BattleHex::INVALID);
  175. owner.curInt->cb->battleMakeAction(heroSpellToCast.get());
  176. endCastingSpell();
  177. }
  178. else
  179. {
  180. possibleActions.clear();
  181. possibleActions.push_back (spellSelMode); //only this one action can be performed at the moment
  182. GH.fakeMouseMove();//update cursor
  183. }
  184. }
  185. const CSpell * BattleActionsController::getHeroSpellToCast( ) const
  186. {
  187. if (heroSpellToCast)
  188. return SpellID(heroSpellToCast->actionSubtype).toSpell();
  189. return nullptr;
  190. }
  191. const CSpell * BattleActionsController::getStackSpellToCast( ) const
  192. {
  193. if (isActiveStackSpellcaster())
  194. return creatureSpellToCast;
  195. return nullptr;
  196. }
  197. const CSpell * BattleActionsController::getCurrentSpell( ) const
  198. {
  199. if (getHeroSpellToCast())
  200. return getHeroSpellToCast();
  201. return getStackSpellToCast();
  202. }
  203. const CStack * BattleActionsController::getStackForHex(BattleHex hoveredHex)
  204. {
  205. const CStack * shere = owner.curInt->cb->battleGetStackByPos(hoveredHex, true);
  206. if(shere)
  207. return shere;
  208. return owner.curInt->cb->battleGetStackByPos(hoveredHex, false);
  209. }
  210. void BattleActionsController::actionSetCursor(PossiblePlayerBattleAction action, BattleHex targetHex)
  211. {
  212. switch (action)
  213. {
  214. case PossiblePlayerBattleAction::CHOOSE_TACTICS_STACK:
  215. CCS->curh->set(Cursor::Combat::POINTER);
  216. return;
  217. case PossiblePlayerBattleAction::MOVE_TACTICS:
  218. case PossiblePlayerBattleAction::MOVE_STACK:
  219. if (owner.stacksController->getActiveStack()->hasBonusOfType(Bonus::FLYING))
  220. CCS->curh->set(Cursor::Combat::FLY);
  221. else
  222. CCS->curh->set(Cursor::Combat::MOVE);
  223. return;
  224. case PossiblePlayerBattleAction::ATTACK:
  225. case PossiblePlayerBattleAction::WALK_AND_ATTACK:
  226. case PossiblePlayerBattleAction::ATTACK_AND_RETURN:
  227. owner.fieldController->setBattleCursor(targetHex);
  228. return;
  229. case PossiblePlayerBattleAction::SHOOT:
  230. if (owner.curInt->cb->battleHasShootingPenalty(owner.stacksController->getActiveStack(), targetHex))
  231. CCS->curh->set(Cursor::Combat::SHOOT_PENALTY);
  232. else
  233. CCS->curh->set(Cursor::Combat::SHOOT);
  234. return;
  235. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  236. case PossiblePlayerBattleAction::ANY_LOCATION:
  237. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL:
  238. case PossiblePlayerBattleAction::FREE_LOCATION:
  239. case PossiblePlayerBattleAction::OBSTACLE:
  240. CCS->curh->set(Cursor::Spellcast::SPELL);
  241. return;
  242. case PossiblePlayerBattleAction::TELEPORT:
  243. CCS->curh->set(Cursor::Combat::TELEPORT);
  244. return;
  245. case PossiblePlayerBattleAction::SACRIFICE:
  246. CCS->curh->set(Cursor::Combat::SACRIFICE);
  247. return;
  248. case PossiblePlayerBattleAction::HEAL:
  249. CCS->curh->set(Cursor::Combat::HEAL);
  250. return;
  251. case PossiblePlayerBattleAction::CATAPULT:
  252. CCS->curh->set(Cursor::Combat::SHOOT_CATAPULT);
  253. return;
  254. case PossiblePlayerBattleAction::CREATURE_INFO:
  255. CCS->curh->set(Cursor::Combat::QUERY);
  256. return;
  257. }
  258. assert(0);
  259. }
  260. void BattleActionsController::actionSetCursorBlocked(PossiblePlayerBattleAction action, BattleHex targetHex)
  261. {
  262. switch (action)
  263. {
  264. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  265. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL:
  266. case PossiblePlayerBattleAction::TELEPORT:
  267. case PossiblePlayerBattleAction::SACRIFICE:
  268. case PossiblePlayerBattleAction::FREE_LOCATION:
  269. CCS->curh->set(Cursor::Combat::BLOCKED);
  270. return;
  271. default:
  272. if (targetHex == -1)
  273. CCS->curh->set(Cursor::Combat::POINTER);
  274. else
  275. CCS->curh->set(Cursor::Combat::BLOCKED);
  276. return;
  277. }
  278. assert(0);
  279. }
  280. std::string BattleActionsController::actionGetStatusMessage(PossiblePlayerBattleAction action, BattleHex targetHex)
  281. {
  282. const CStack * targetStack = getStackForHex(targetHex);
  283. switch (action) //display console message, realize selected action
  284. {
  285. case PossiblePlayerBattleAction::CHOOSE_TACTICS_STACK:
  286. return (boost::format(CGI->generaltexth->allTexts[481]) % targetStack->getName()).str(); //Select %s
  287. case PossiblePlayerBattleAction::MOVE_TACTICS:
  288. case PossiblePlayerBattleAction::MOVE_STACK:
  289. if (owner.stacksController->getActiveStack()->hasBonusOfType(Bonus::FLYING))
  290. return (boost::format(CGI->generaltexth->allTexts[295]) % owner.stacksController->getActiveStack()->getName()).str(); //Fly %s here
  291. else
  292. return (boost::format(CGI->generaltexth->allTexts[294]) % owner.stacksController->getActiveStack()->getName()).str(); //Move %s here
  293. case PossiblePlayerBattleAction::ATTACK:
  294. case PossiblePlayerBattleAction::WALK_AND_ATTACK:
  295. case PossiblePlayerBattleAction::ATTACK_AND_RETURN: //TODO: allow to disable return
  296. {
  297. BattleHex attackFromHex = owner.fieldController->fromWhichHexAttack(targetHex);
  298. TDmgRange damage = owner.curInt->cb->battleEstimateDamage(owner.stacksController->getActiveStack(), targetStack, attackFromHex);
  299. std::string estDmgText = formatDmgRange(std::make_pair((ui32)damage.first, (ui32)damage.second)); //calculating estimated dmg
  300. return (boost::format(CGI->generaltexth->allTexts[36]) % targetStack->getName() % estDmgText).str(); //Attack %s (%s damage)
  301. }
  302. case PossiblePlayerBattleAction::SHOOT:
  303. {
  304. auto const * shooter = owner.stacksController->getActiveStack();
  305. TDmgRange damage = owner.curInt->cb->battleEstimateDamage(shooter, targetStack, shooter->getPosition());
  306. std::string estDmgText = formatDmgRange(std::make_pair((ui32)damage.first, (ui32)damage.second)); //calculating estimated dmg
  307. //printing - Shoot %s (%d shots left, %s damage)
  308. return (boost::format(CGI->generaltexth->allTexts[296]) % targetStack->getName() % shooter->shots.available() % estDmgText).str();
  309. }
  310. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  311. return boost::str(boost::format(CGI->generaltexth->allTexts[27]) % getCurrentSpell()->getNameTranslated() % targetStack->getName()); //Cast %s on %s
  312. case PossiblePlayerBattleAction::ANY_LOCATION:
  313. return boost::str(boost::format(CGI->generaltexth->allTexts[26]) % getCurrentSpell()->getNameTranslated()); //Cast %s
  314. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL: //we assume that teleport / sacrifice will never be available as random spell
  315. return boost::str(boost::format(CGI->generaltexth->allTexts[301]) % targetStack->getName()); //Cast a spell on %
  316. case PossiblePlayerBattleAction::TELEPORT:
  317. return CGI->generaltexth->allTexts[25]; //Teleport Here
  318. case PossiblePlayerBattleAction::OBSTACLE:
  319. return CGI->generaltexth->allTexts[550];
  320. case PossiblePlayerBattleAction::SACRIFICE:
  321. return (boost::format(CGI->generaltexth->allTexts[549]) % targetStack->getName()).str(); //sacrifice the %s
  322. case PossiblePlayerBattleAction::FREE_LOCATION:
  323. return boost::str(boost::format(CGI->generaltexth->allTexts[26]) % getCurrentSpell()->getNameTranslated()); //Cast %s
  324. case PossiblePlayerBattleAction::HEAL:
  325. return (boost::format(CGI->generaltexth->allTexts[419]) % targetStack->getName()).str(); //Apply first aid to the %s
  326. case PossiblePlayerBattleAction::CATAPULT:
  327. return ""; // TODO
  328. case PossiblePlayerBattleAction::CREATURE_INFO:
  329. return (boost::format(CGI->generaltexth->allTexts[297]) % targetStack->getName()).str();
  330. }
  331. assert(0);
  332. return "";
  333. }
  334. std::string BattleActionsController::actionGetStatusMessageBlocked(PossiblePlayerBattleAction action, BattleHex targetHex)
  335. {
  336. switch (action)
  337. {
  338. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  339. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL:
  340. return CGI->generaltexth->allTexts[23];
  341. break;
  342. case PossiblePlayerBattleAction::TELEPORT:
  343. return CGI->generaltexth->allTexts[24]; //Invalid Teleport Destination
  344. break;
  345. case PossiblePlayerBattleAction::SACRIFICE:
  346. return CGI->generaltexth->allTexts[543]; //choose army to sacrifice
  347. break;
  348. case PossiblePlayerBattleAction::FREE_LOCATION:
  349. return boost::str(boost::format(CGI->generaltexth->allTexts[181]) % getCurrentSpell()->getNameTranslated()); //No room to place %s here
  350. break;
  351. default:
  352. return "";
  353. }
  354. }
  355. bool BattleActionsController::actionIsLegal(PossiblePlayerBattleAction action, BattleHex targetHex)
  356. {
  357. const CStack * targetStack = getStackForHex(targetHex);
  358. bool targetStackOwned = targetStack && targetStack->owner == owner.curInt->playerID;
  359. switch (action)
  360. {
  361. case PossiblePlayerBattleAction::CHOOSE_TACTICS_STACK:
  362. case PossiblePlayerBattleAction::CREATURE_INFO:
  363. return (targetStack && targetStackOwned);
  364. case PossiblePlayerBattleAction::MOVE_TACTICS:
  365. case PossiblePlayerBattleAction::MOVE_STACK:
  366. if (!(targetStack && targetStack->alive())) //we can walk on dead stacks
  367. {
  368. if(canStackMoveHere(owner.stacksController->getActiveStack(), targetHex))
  369. return true;
  370. }
  371. return false;
  372. case PossiblePlayerBattleAction::ATTACK:
  373. case PossiblePlayerBattleAction::WALK_AND_ATTACK:
  374. case PossiblePlayerBattleAction::ATTACK_AND_RETURN:
  375. if(owner.curInt->cb->battleCanAttack(owner.stacksController->getActiveStack(), targetStack, targetHex))
  376. {
  377. if (owner.fieldController->isTileAttackable(targetHex)) // move isTileAttackable to be part of battleCanAttack?
  378. return true;
  379. }
  380. return false;
  381. case PossiblePlayerBattleAction::SHOOT:
  382. return owner.curInt->cb->battleCanShoot(owner.stacksController->getActiveStack(), targetHex);
  383. case PossiblePlayerBattleAction::ANY_LOCATION:
  384. return isCastingPossibleHere(owner.stacksController->getActiveStack(), targetStack, targetHex);
  385. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  386. return targetStack && isCastingPossibleHere(owner.stacksController->getActiveStack(), targetStack, targetHex);
  387. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL:
  388. if(targetStack && targetStackOwned && targetStack != owner.stacksController->getActiveStack() && targetStack->alive()) //only positive spells for other allied creatures
  389. {
  390. int spellID = owner.curInt->cb->battleGetRandomStackSpell(CRandomGenerator::getDefault(), targetStack, CBattleInfoCallback::RANDOM_GENIE);
  391. return spellID > -1;
  392. }
  393. return false;
  394. case PossiblePlayerBattleAction::TELEPORT:
  395. {
  396. ui8 skill = getCurrentSpellcaster()->getEffectLevel(SpellID(SpellID::TELEPORT).toSpell());
  397. return owner.curInt->cb->battleCanTeleportTo(owner.stacksController->getSelectedStack(), targetHex, skill);
  398. }
  399. case PossiblePlayerBattleAction::SACRIFICE: //choose our living stack to sacrifice
  400. return targetStack && targetStack != owner.stacksController->getSelectedStack() && targetStackOwned && targetStack->alive();
  401. case PossiblePlayerBattleAction::OBSTACLE:
  402. case PossiblePlayerBattleAction::FREE_LOCATION:
  403. return isCastingPossibleHere(owner.stacksController->getActiveStack(), targetStack, targetHex);
  404. return isCastingPossibleHere(owner.stacksController->getActiveStack(), targetStack, targetHex);
  405. case PossiblePlayerBattleAction::CATAPULT:
  406. return owner.siegeController && owner.siegeController->isAttackableByCatapult(targetHex);
  407. case PossiblePlayerBattleAction::HEAL:
  408. return targetStack && targetStackOwned && targetStack->canBeHealed();
  409. }
  410. assert(0);
  411. return false;
  412. }
  413. void BattleActionsController::actionRealize(PossiblePlayerBattleAction action, BattleHex targetHex)
  414. {
  415. const CStack * targetStack = getStackForHex(targetHex);
  416. switch (action) //display console message, realize selected action
  417. {
  418. case PossiblePlayerBattleAction::CHOOSE_TACTICS_STACK:
  419. {
  420. owner.stackActivated(targetStack);
  421. return;
  422. }
  423. case PossiblePlayerBattleAction::MOVE_TACTICS:
  424. case PossiblePlayerBattleAction::MOVE_STACK:
  425. {
  426. if(owner.stacksController->getActiveStack()->doubleWide())
  427. {
  428. std::vector<BattleHex> acc = owner.curInt->cb->battleGetAvailableHexes(owner.stacksController->getActiveStack());
  429. BattleHex shiftedDest = targetHex.cloneInDirection(owner.stacksController->getActiveStack()->destShiftDir(), false);
  430. if(vstd::contains(acc, targetHex))
  431. owner.giveCommand(EActionType::WALK, targetHex);
  432. else if(vstd::contains(acc, shiftedDest))
  433. owner.giveCommand(EActionType::WALK, shiftedDest);
  434. }
  435. else
  436. {
  437. owner.giveCommand(EActionType::WALK, targetHex);
  438. }
  439. return;
  440. }
  441. case PossiblePlayerBattleAction::ATTACK:
  442. case PossiblePlayerBattleAction::WALK_AND_ATTACK:
  443. case PossiblePlayerBattleAction::ATTACK_AND_RETURN: //TODO: allow to disable return
  444. {
  445. bool returnAfterAttack = action == PossiblePlayerBattleAction::ATTACK_AND_RETURN;
  446. BattleHex attackFromHex = owner.fieldController->fromWhichHexAttack(targetHex);
  447. if(attackFromHex.isValid()) //we can be in this line when unreachable creature is L - clicked (as of revision 1308)
  448. {
  449. auto command = new BattleAction(BattleAction::makeMeleeAttack(owner.stacksController->getActiveStack(), targetHex, attackFromHex, returnAfterAttack));
  450. owner.sendCommand(command, owner.stacksController->getActiveStack());
  451. }
  452. return;
  453. }
  454. case PossiblePlayerBattleAction::SHOOT:
  455. {
  456. owner.giveCommand(EActionType::SHOOT, targetHex);
  457. return;
  458. }
  459. case PossiblePlayerBattleAction::HEAL:
  460. {
  461. owner.giveCommand(EActionType::STACK_HEAL, targetHex);
  462. return;
  463. };
  464. case PossiblePlayerBattleAction::CATAPULT:
  465. {
  466. owner.giveCommand(EActionType::CATAPULT, targetHex);
  467. return;
  468. }
  469. case PossiblePlayerBattleAction::CREATURE_INFO:
  470. {
  471. GH.pushIntT<CStackWindow>(targetStack, false);
  472. return;
  473. }
  474. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  475. case PossiblePlayerBattleAction::ANY_LOCATION:
  476. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL: //we assume that teleport / sacrifice will never be available as random spell
  477. case PossiblePlayerBattleAction::TELEPORT:
  478. case PossiblePlayerBattleAction::OBSTACLE:
  479. case PossiblePlayerBattleAction::SACRIFICE:
  480. case PossiblePlayerBattleAction::FREE_LOCATION:
  481. {
  482. if (action == PossiblePlayerBattleAction::AIMED_SPELL_CREATURE )
  483. {
  484. if (getCurrentSpell()->id == SpellID::SACRIFICE)
  485. {
  486. heroSpellToCast->aimToHex(targetHex);
  487. possibleActions.push_back(PossiblePlayerBattleAction::SACRIFICE);
  488. owner.stacksController->setSelectedStack(targetStack);
  489. return;
  490. }
  491. if (getCurrentSpell()->id == SpellID::TELEPORT)
  492. {
  493. heroSpellToCast->aimToUnit(targetStack);
  494. possibleActions.push_back(PossiblePlayerBattleAction::TELEPORT);
  495. owner.stacksController->setSelectedStack(targetStack);
  496. return;
  497. }
  498. }
  499. if (!spellcastingModeActive())
  500. {
  501. if (getStackSpellToCast())
  502. {
  503. owner.giveCommand(EActionType::MONSTER_SPELL, targetHex, getStackSpellToCast()->getId());
  504. }
  505. else //unknown random spell
  506. {
  507. owner.giveCommand(EActionType::MONSTER_SPELL, targetHex);
  508. }
  509. }
  510. else
  511. {
  512. assert(getHeroSpellToCast());
  513. switch (getHeroSpellToCast()->id.toEnum())
  514. {
  515. case SpellID::SACRIFICE:
  516. heroSpellToCast->aimToUnit(targetStack);//victim
  517. break;
  518. default:
  519. heroSpellToCast->aimToHex(targetHex);
  520. break;
  521. }
  522. owner.curInt->cb->battleMakeAction(heroSpellToCast.get());
  523. endCastingSpell();
  524. }
  525. owner.stacksController->setSelectedStack(nullptr);
  526. return;
  527. }
  528. }
  529. assert(0);
  530. return;
  531. }
  532. PossiblePlayerBattleAction BattleActionsController::selectAction(BattleHex targetHex)
  533. {
  534. assert(owner.stacksController->getActiveStack() != nullptr);
  535. assert(!possibleActions.empty());
  536. assert(targetHex.isValid());
  537. if (owner.stacksController->getActiveStack() == nullptr)
  538. return PossiblePlayerBattleAction::INVALID;
  539. if (possibleActions.empty())
  540. return PossiblePlayerBattleAction::INVALID;
  541. const CStack * targetStack = getStackForHex(targetHex);
  542. reorderPossibleActionsPriority(owner.stacksController->getActiveStack(), targetStack ? MouseHoveredHexContext::OCCUPIED_HEX : MouseHoveredHexContext::UNOCCUPIED_HEX);
  543. for (PossiblePlayerBattleAction action : possibleActions)
  544. {
  545. if (actionIsLegal(action, targetHex))
  546. return action;
  547. }
  548. return possibleActions.front();
  549. }
  550. void BattleActionsController::onHexHovered(BattleHex hoveredHex)
  551. {
  552. if (owner.stacksController->getActiveStack() == nullptr)
  553. return;
  554. if (hoveredHex == BattleHex::INVALID)
  555. {
  556. if (!currentConsoleMsg.empty())
  557. GH.statusbar->clearIfMatching(currentConsoleMsg);
  558. currentConsoleMsg.clear();
  559. return;
  560. }
  561. auto action = selectAction(hoveredHex);
  562. std::string newConsoleMsg;
  563. if (actionIsLegal(action, hoveredHex))
  564. {
  565. actionSetCursor(action, hoveredHex);
  566. newConsoleMsg = actionGetStatusMessage(action, hoveredHex);
  567. }
  568. else
  569. {
  570. actionSetCursorBlocked(action, hoveredHex);
  571. newConsoleMsg = actionGetStatusMessageBlocked(action, hoveredHex);
  572. }
  573. if (!currentConsoleMsg.empty())
  574. GH.statusbar->clearIfMatching(currentConsoleMsg);
  575. if (!newConsoleMsg.empty())
  576. GH.statusbar->write(newConsoleMsg);
  577. currentConsoleMsg = newConsoleMsg;
  578. }
  579. void BattleActionsController::onHexClicked(BattleHex clickedHex)
  580. {
  581. if (owner.stacksController->getActiveStack() == nullptr)
  582. return;
  583. auto action = selectAction(clickedHex);
  584. std::string newConsoleMsg;
  585. if (!actionIsLegal(action, clickedHex))
  586. return;
  587. actionRealize(action, clickedHex);
  588. GH.statusbar->clear();
  589. }
  590. void BattleActionsController::tryActivateStackSpellcasting(const CStack *casterStack)
  591. {
  592. const auto spellcaster = casterStack->getBonusLocalFirst(Selector::type()(Bonus::SPELLCASTER));
  593. if(casterStack->canCast() && spellcaster)
  594. {
  595. // faerie dragon can cast only one, randomly selected spell until their next move
  596. //TODO: faerie dragon type spell should be selected by server
  597. creatureSpellToCast = owner.curInt->cb->battleGetRandomStackSpell(CRandomGenerator::getDefault(), casterStack, CBattleInfoCallback::RANDOM_AIMED).toSpell();
  598. }
  599. }
  600. const spells::Caster * BattleActionsController::getCurrentSpellcaster() const
  601. {
  602. if (heroSpellToCast)
  603. return owner.getActiveHero();
  604. else
  605. return owner.stacksController->getActiveStack();
  606. }
  607. spells::Mode BattleActionsController::getCurrentCastMode() const
  608. {
  609. if (heroSpellToCast)
  610. return spells::Mode::HERO;
  611. else
  612. return spells::Mode::CREATURE_ACTIVE;
  613. }
  614. bool BattleActionsController::isCastingPossibleHere(const CStack *casterStack, const CStack *targetStack, BattleHex targetHex)
  615. {
  616. auto currentSpell = getCurrentSpell();
  617. assert(currentSpell);
  618. if (!currentSpell)
  619. return false;
  620. auto caster = getCurrentSpellcaster();
  621. const spells::Mode mode = heroSpellToCast ? spells::Mode::HERO : spells::Mode::CREATURE_ACTIVE;
  622. spells::Target target;
  623. target.emplace_back(targetHex);
  624. spells::BattleCast cast(owner.curInt->cb.get(), caster, mode, currentSpell);
  625. auto m = currentSpell->battleMechanics(&cast);
  626. spells::detail::ProblemImpl problem; //todo: display problem in status bar
  627. return m->canBeCastAt(target, problem);
  628. }
  629. bool BattleActionsController::canStackMoveHere(const CStack * stackToMove, BattleHex myNumber) const
  630. {
  631. std::vector<BattleHex> acc = owner.curInt->cb->battleGetAvailableHexes(stackToMove);
  632. BattleHex shiftedDest = myNumber.cloneInDirection(stackToMove->destShiftDir(), false);
  633. if (vstd::contains(acc, myNumber))
  634. return true;
  635. else if (stackToMove->doubleWide() && vstd::contains(acc, shiftedDest))
  636. return true;
  637. else
  638. return false;
  639. }
  640. void BattleActionsController::activateStack()
  641. {
  642. const CStack * s = owner.stacksController->getActiveStack();
  643. if(s)
  644. {
  645. tryActivateStackSpellcasting(s);
  646. possibleActions = getPossibleActionsForStack(s);
  647. std::list<PossiblePlayerBattleAction> actionsToSelect;
  648. if(!possibleActions.empty())
  649. {
  650. switch(possibleActions.front())
  651. {
  652. case PossiblePlayerBattleAction::SHOOT:
  653. actionsToSelect.push_back(possibleActions.front());
  654. actionsToSelect.push_back(PossiblePlayerBattleAction::ATTACK);
  655. break;
  656. case PossiblePlayerBattleAction::ATTACK_AND_RETURN:
  657. actionsToSelect.push_back(possibleActions.front());
  658. actionsToSelect.push_back(PossiblePlayerBattleAction::WALK_AND_ATTACK);
  659. break;
  660. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  661. actionsToSelect.push_back(possibleActions.front());
  662. break;
  663. }
  664. }
  665. owner.windowObject->setAlternativeActions(actionsToSelect);
  666. }
  667. }
  668. bool BattleActionsController::spellcastingModeActive() const
  669. {
  670. return heroSpellToCast != nullptr;;
  671. }
  672. bool BattleActionsController::currentActionSpellcasting(BattleHex hoveredHex)
  673. {
  674. if (heroSpellToCast)
  675. return true;
  676. if (!owner.stacksController->getActiveStack())
  677. return false;
  678. auto action = selectAction(hoveredHex);
  679. return
  680. action == PossiblePlayerBattleAction::ANY_LOCATION ||
  681. action == PossiblePlayerBattleAction::NO_LOCATION ||
  682. action == PossiblePlayerBattleAction::FREE_LOCATION ||
  683. action == PossiblePlayerBattleAction::AIMED_SPELL_CREATURE ||
  684. action == PossiblePlayerBattleAction::OBSTACLE;
  685. }
  686. const std::vector<PossiblePlayerBattleAction> & BattleActionsController::getPossibleActions() const
  687. {
  688. return possibleActions;
  689. }
  690. void BattleActionsController::removePossibleAction(PossiblePlayerBattleAction action)
  691. {
  692. vstd::erase(possibleActions, action);
  693. }
  694. void BattleActionsController::pushFrontPossibleAction(PossiblePlayerBattleAction action)
  695. {
  696. possibleActions.insert(possibleActions.begin(), action);
  697. }