BattleActionsController.cpp 28 KB

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