BattleActionsController.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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. owner.fieldController->setBattleCursor(myNumber); // temporary - needed for following function :(
  257. BattleHex attackFromHex = owner.fieldController->fromWhichHexAttack(myNumber);
  258. if (attackFromHex >= 0) //we can be in this line when unreachable creature is L - clicked (as of revision 1308)
  259. legalAction = true;
  260. }
  261. }
  262. }
  263. break;
  264. case PossiblePlayerBattleAction::SHOOT:
  265. if(owner.curInt->cb->battleCanShoot(owner.stacksController->getActiveStack(), myNumber))
  266. legalAction = true;
  267. break;
  268. case PossiblePlayerBattleAction::ANY_LOCATION:
  269. if (myNumber > -1) //TODO: this should be checked for all actions
  270. {
  271. if(isCastingPossibleHere(owner.stacksController->getActiveStack(), shere, myNumber))
  272. legalAction = true;
  273. }
  274. break;
  275. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  276. if(shere && isCastingPossibleHere(owner.stacksController->getActiveStack(), shere, myNumber))
  277. legalAction = true;
  278. break;
  279. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL:
  280. {
  281. if(shere && ourStack && shere != owner.stacksController->getActiveStack() && shere->alive()) //only positive spells for other allied creatures
  282. {
  283. int spellID = owner.curInt->cb->battleGetRandomStackSpell(CRandomGenerator::getDefault(), shere, CBattleInfoCallback::RANDOM_GENIE);
  284. if(spellID > -1)
  285. {
  286. legalAction = true;
  287. }
  288. }
  289. }
  290. break;
  291. case PossiblePlayerBattleAction::OBSTACLE:
  292. if(isCastingPossibleHere(owner.stacksController->getActiveStack(), shere, myNumber))
  293. legalAction = true;
  294. break;
  295. case PossiblePlayerBattleAction::TELEPORT:
  296. {
  297. //todo: move to mechanics
  298. ui8 skill = 0;
  299. if (creatureCasting)
  300. skill = owner.stacksController->getActiveStack()->getEffectLevel(SpellID(SpellID::TELEPORT).toSpell());
  301. else
  302. skill = owner.getActiveHero()->getEffectLevel(SpellID(SpellID::TELEPORT).toSpell());
  303. //TODO: explicitely save power, skill
  304. if (owner.curInt->cb->battleCanTeleportTo(owner.stacksController->getSelectedStack(), myNumber, skill))
  305. legalAction = true;
  306. else
  307. notLegal = true;
  308. }
  309. break;
  310. case PossiblePlayerBattleAction::SACRIFICE: //choose our living stack to sacrifice
  311. if (shere && shere != owner.stacksController->getSelectedStack() && ourStack && shere->alive())
  312. legalAction = true;
  313. else
  314. notLegal = true;
  315. break;
  316. case PossiblePlayerBattleAction::FREE_LOCATION:
  317. legalAction = true;
  318. if(!isCastingPossibleHere(owner.stacksController->getActiveStack(), shere, myNumber))
  319. {
  320. legalAction = false;
  321. notLegal = true;
  322. }
  323. break;
  324. case PossiblePlayerBattleAction::CATAPULT:
  325. if (owner.siegeController && owner.siegeController->isAttackableByCatapult(myNumber))
  326. legalAction = true;
  327. break;
  328. case PossiblePlayerBattleAction::HEAL:
  329. if (shere && ourStack && shere->canBeHealed())
  330. legalAction = true;
  331. break;
  332. case PossiblePlayerBattleAction::RISE_DEMONS:
  333. if (shere && ourStack && !shere->alive())
  334. {
  335. if (!(shere->hasBonusOfType(Bonus::UNDEAD)
  336. || shere->hasBonusOfType(Bonus::NON_LIVING)
  337. || shere->hasBonusOfType(Bonus::GARGOYLE)
  338. || shere->summoned
  339. || shere->isClone()
  340. || shere->hasBonusOfType(Bonus::SIEGE_WEAPON)
  341. ))
  342. legalAction = true;
  343. }
  344. break;
  345. }
  346. if (legalAction)
  347. localActions.push_back (action);
  348. else if (notLegal || forcedAction)
  349. illegalActions.push_back (action);
  350. }
  351. illegalAction = PossiblePlayerBattleAction::INVALID; //clear it in first place
  352. if (vstd::contains(localActions, selectedAction)) //try to use last selected action by default
  353. currentAction = selectedAction;
  354. else if (localActions.size()) //if not possible, select first available action (they are sorted by suggested priority)
  355. currentAction = localActions.front();
  356. else //no legal action possible
  357. {
  358. currentAction = PossiblePlayerBattleAction::INVALID; //don't allow to do anything
  359. if (vstd::contains(illegalActions, selectedAction))
  360. illegalAction = selectedAction;
  361. else if (illegalActions.size())
  362. illegalAction = illegalActions.front();
  363. else if (shere && ourStack && shere->alive()) //last possibility - display info about our creature
  364. {
  365. currentAction = PossiblePlayerBattleAction::CREATURE_INFO;
  366. }
  367. else
  368. illegalAction = PossiblePlayerBattleAction::INVALID; //we should never be here
  369. }
  370. bool isCastingPossible = false;
  371. bool secondaryTarget = false;
  372. if (currentAction > PossiblePlayerBattleAction::INVALID)
  373. {
  374. switch (currentAction) //display console message, realize selected action
  375. {
  376. case PossiblePlayerBattleAction::CHOOSE_TACTICS_STACK:
  377. newConsoleMsg = (boost::format(CGI->generaltexth->allTexts[481]) % shere->getName()).str(); //Select %s
  378. realizeAction = [=](){ owner.stackActivated(shere); };
  379. break;
  380. case PossiblePlayerBattleAction::MOVE_TACTICS:
  381. case PossiblePlayerBattleAction::MOVE_STACK:
  382. if (owner.stacksController->getActiveStack()->hasBonusOfType(Bonus::FLYING))
  383. {
  384. cursorFrame = Cursor::Combat::FLY;
  385. newConsoleMsg = (boost::format(CGI->generaltexth->allTexts[295]) % owner.stacksController->getActiveStack()->getName()).str(); //Fly %s here
  386. }
  387. else
  388. {
  389. cursorFrame = Cursor::Combat::MOVE;
  390. newConsoleMsg = (boost::format(CGI->generaltexth->allTexts[294]) % owner.stacksController->getActiveStack()->getName()).str(); //Move %s here
  391. }
  392. realizeAction = [=]()
  393. {
  394. if(owner.stacksController->getActiveStack()->doubleWide())
  395. {
  396. std::vector<BattleHex> acc = owner.curInt->cb->battleGetAvailableHexes(owner.stacksController->getActiveStack());
  397. BattleHex shiftedDest = myNumber.cloneInDirection(owner.stacksController->getActiveStack()->destShiftDir(), false);
  398. if(vstd::contains(acc, myNumber))
  399. owner.giveCommand(EActionType::WALK, myNumber);
  400. else if(vstd::contains(acc, shiftedDest))
  401. owner.giveCommand(EActionType::WALK, shiftedDest);
  402. }
  403. else
  404. {
  405. owner.giveCommand(EActionType::WALK, myNumber);
  406. }
  407. };
  408. break;
  409. case PossiblePlayerBattleAction::ATTACK:
  410. case PossiblePlayerBattleAction::WALK_AND_ATTACK:
  411. case PossiblePlayerBattleAction::ATTACK_AND_RETURN: //TODO: allow to disable return
  412. {
  413. owner.fieldController->setBattleCursor(myNumber); //handle direction of cursor and attackable tile
  414. setCursor = false; //don't overwrite settings from the call above //TODO: what does it mean?
  415. bool returnAfterAttack = currentAction == PossiblePlayerBattleAction::ATTACK_AND_RETURN;
  416. realizeAction = [=]()
  417. {
  418. BattleHex attackFromHex = owner.fieldController->fromWhichHexAttack(myNumber);
  419. if(attackFromHex.isValid()) //we can be in this line when unreachable creature is L - clicked (as of revision 1308)
  420. {
  421. auto command = new BattleAction(BattleAction::makeMeleeAttack(owner.stacksController->getActiveStack(), myNumber, attackFromHex, returnAfterAttack));
  422. owner.sendCommand(command, owner.stacksController->getActiveStack());
  423. }
  424. };
  425. TDmgRange damage = owner.curInt->cb->battleEstimateDamage(owner.stacksController->getActiveStack(), shere);
  426. std::string estDmgText = formatDmgRange(std::make_pair((ui32)damage.first, (ui32)damage.second)); //calculating estimated dmg
  427. newConsoleMsg = (boost::format(CGI->generaltexth->allTexts[36]) % shere->getName() % estDmgText).str(); //Attack %s (%s damage)
  428. }
  429. break;
  430. case PossiblePlayerBattleAction::SHOOT:
  431. {
  432. if (owner.curInt->cb->battleHasShootingPenalty(owner.stacksController->getActiveStack(), myNumber))
  433. cursorFrame = Cursor::Combat::SHOOT_PENALTY;
  434. else
  435. cursorFrame = Cursor::Combat::SHOOT;
  436. realizeAction = [=](){owner.giveCommand(EActionType::SHOOT, myNumber);};
  437. TDmgRange damage = owner.curInt->cb->battleEstimateDamage(owner.stacksController->getActiveStack(), shere);
  438. std::string estDmgText = formatDmgRange(std::make_pair((ui32)damage.first, (ui32)damage.second)); //calculating estimated dmg
  439. //printing - Shoot %s (%d shots left, %s damage)
  440. newConsoleMsg = (boost::format(CGI->generaltexth->allTexts[296]) % shere->getName() % owner.stacksController->getActiveStack()->shots.available() % estDmgText).str();
  441. }
  442. break;
  443. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  444. currentSpell = CGI->spellh->objects[creatureCasting ? owner.stacksController->activeStackSpellToCast() : spellToCast->actionSubtype]; //necessary if creature has random Genie spell at same time
  445. newConsoleMsg = boost::str(boost::format(CGI->generaltexth->allTexts[27]) % currentSpell->name % shere->getName()); //Cast %s on %s
  446. switch (currentSpell->id)
  447. {
  448. case SpellID::SACRIFICE:
  449. case SpellID::TELEPORT:
  450. owner.stacksController->setSelectedStack(shere); //remember first target
  451. secondaryTarget = true;
  452. break;
  453. }
  454. isCastingPossible = true;
  455. break;
  456. case PossiblePlayerBattleAction::ANY_LOCATION:
  457. currentSpell = CGI->spellh->objects[creatureCasting ? owner.stacksController->activeStackSpellToCast() : spellToCast->actionSubtype]; //necessary if creature has random Genie spell at same time
  458. newConsoleMsg = boost::str(boost::format(CGI->generaltexth->allTexts[26]) % currentSpell->name); //Cast %s
  459. isCastingPossible = true;
  460. break;
  461. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL: //we assume that teleport / sacrifice will never be available as random spell
  462. currentSpell = nullptr;
  463. newConsoleMsg = boost::str(boost::format(CGI->generaltexth->allTexts[301]) % shere->getName()); //Cast a spell on %
  464. creatureCasting = true;
  465. isCastingPossible = true;
  466. break;
  467. case PossiblePlayerBattleAction::TELEPORT:
  468. newConsoleMsg = CGI->generaltexth->allTexts[25]; //Teleport Here
  469. cursorFrame = Cursor::Combat::TELEPORT;
  470. isCastingPossible = true;
  471. break;
  472. case PossiblePlayerBattleAction::OBSTACLE:
  473. newConsoleMsg = CGI->generaltexth->allTexts[550];
  474. //TODO: remove obstacle cursor
  475. isCastingPossible = true;
  476. break;
  477. case PossiblePlayerBattleAction::SACRIFICE:
  478. newConsoleMsg = (boost::format(CGI->generaltexth->allTexts[549]) % shere->getName()).str(); //sacrifice the %s
  479. cursorFrame = Cursor::Combat::SACRIFICE;
  480. isCastingPossible = true;
  481. break;
  482. case PossiblePlayerBattleAction::FREE_LOCATION:
  483. newConsoleMsg = boost::str(boost::format(CGI->generaltexth->allTexts[26]) % currentSpell->name); //Cast %s
  484. isCastingPossible = true;
  485. break;
  486. case PossiblePlayerBattleAction::HEAL:
  487. cursorFrame = Cursor::Combat::HEAL;
  488. newConsoleMsg = (boost::format(CGI->generaltexth->allTexts[419]) % shere->getName()).str(); //Apply first aid to the %s
  489. realizeAction = [=](){ owner.giveCommand(EActionType::STACK_HEAL, myNumber); }; //command healing
  490. break;
  491. case PossiblePlayerBattleAction::RISE_DEMONS:
  492. spellcastingCursor = true;
  493. realizeAction = [=]()
  494. {
  495. owner.giveCommand(EActionType::DAEMON_SUMMONING, myNumber);
  496. };
  497. break;
  498. case PossiblePlayerBattleAction::CATAPULT:
  499. cursorFrame = Cursor::Combat::SHOOT_CATAPULT;
  500. realizeAction = [=](){ owner.giveCommand(EActionType::CATAPULT, myNumber); };
  501. break;
  502. case PossiblePlayerBattleAction::CREATURE_INFO:
  503. {
  504. cursorFrame = Cursor::Combat::QUERY;
  505. newConsoleMsg = (boost::format(CGI->generaltexth->allTexts[297]) % shere->getName()).str();
  506. realizeAction = [=](){ GH.pushIntT<CStackWindow>(shere, false); };
  507. break;
  508. }
  509. }
  510. }
  511. else //no possible valid action, display message
  512. {
  513. switch (illegalAction)
  514. {
  515. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  516. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL:
  517. cursorFrame = Cursor::Combat::BLOCKED;
  518. newConsoleMsg = CGI->generaltexth->allTexts[23];
  519. break;
  520. case PossiblePlayerBattleAction::TELEPORT:
  521. cursorFrame = Cursor::Combat::BLOCKED;
  522. newConsoleMsg = CGI->generaltexth->allTexts[24]; //Invalid Teleport Destination
  523. break;
  524. case PossiblePlayerBattleAction::SACRIFICE:
  525. newConsoleMsg = CGI->generaltexth->allTexts[543]; //choose army to sacrifice
  526. break;
  527. case PossiblePlayerBattleAction::FREE_LOCATION:
  528. cursorFrame = Cursor::Combat::BLOCKED;
  529. newConsoleMsg = boost::str(boost::format(CGI->generaltexth->allTexts[181]) % currentSpell->name); //No room to place %s here
  530. break;
  531. default:
  532. if (myNumber == -1)
  533. CCS->curh->set(Cursor::Combat::POINTER);
  534. else
  535. cursorFrame = Cursor::Combat::BLOCKED;
  536. break;
  537. }
  538. }
  539. if (isCastingPossible) //common part
  540. {
  541. switch (currentAction) //don't use that with teleport / sacrifice
  542. {
  543. case PossiblePlayerBattleAction::TELEPORT: //FIXME: more generic solution?
  544. case PossiblePlayerBattleAction::SACRIFICE:
  545. break;
  546. default:
  547. spellcastingCursor = true;
  548. if (newConsoleMsg.empty() && currentSpell)
  549. newConsoleMsg = boost::str(boost::format(CGI->generaltexth->allTexts[26]) % currentSpell->name); //Cast %s
  550. break;
  551. }
  552. realizeAction = [=]()
  553. {
  554. if(secondaryTarget) //select that target now
  555. {
  556. possibleActions.clear();
  557. switch (currentSpell->id.toEnum())
  558. {
  559. case SpellID::TELEPORT: //don't cast spell yet, only select target
  560. spellToCast->aimToUnit(shere);
  561. possibleActions.push_back(PossiblePlayerBattleAction::TELEPORT);
  562. break;
  563. case SpellID::SACRIFICE:
  564. spellToCast->aimToHex(myNumber);
  565. possibleActions.push_back(PossiblePlayerBattleAction::SACRIFICE);
  566. break;
  567. }
  568. }
  569. else
  570. {
  571. if (creatureCasting)
  572. {
  573. if (currentSpell)
  574. {
  575. owner.giveCommand(EActionType::MONSTER_SPELL, myNumber, owner.stacksController->activeStackSpellToCast());
  576. }
  577. else //unknown random spell
  578. {
  579. owner.giveCommand(EActionType::MONSTER_SPELL, myNumber);
  580. }
  581. }
  582. else
  583. {
  584. assert(currentSpell);
  585. switch (currentSpell->id.toEnum())
  586. {
  587. case SpellID::SACRIFICE:
  588. spellToCast->aimToUnit(shere);//victim
  589. break;
  590. default:
  591. spellToCast->aimToHex(myNumber);
  592. break;
  593. }
  594. owner.curInt->cb->battleMakeAction(spellToCast.get());
  595. endCastingSpell();
  596. }
  597. owner.stacksController->setSelectedStack(nullptr);
  598. }
  599. };
  600. }
  601. {
  602. if (eventType == CIntObject::MOVE)
  603. {
  604. if (setCursor)
  605. {
  606. if (spellcastingCursor)
  607. CCS->curh->set(Cursor::Spellcast::SPELL);
  608. else
  609. CCS->curh->set(cursorFrame);
  610. }
  611. if (!currentConsoleMsg.empty())
  612. owner.controlPanel->console->clearIfMatching(currentConsoleMsg);
  613. if (!newConsoleMsg.empty())
  614. owner.controlPanel->console->write(newConsoleMsg);
  615. currentConsoleMsg = newConsoleMsg;
  616. }
  617. if (eventType == CIntObject::LCLICK && realizeAction)
  618. {
  619. //opening creature window shouldn't affect myTurn...
  620. if ((currentAction != PossiblePlayerBattleAction::CREATURE_INFO) && !secondaryTarget)
  621. {
  622. owner.myTurn = false; //tends to crash with empty calls
  623. }
  624. realizeAction();
  625. if (!secondaryTarget) //do not replace teleport or sacrifice cursor
  626. CCS->curh->set(Cursor::Combat::POINTER);
  627. owner.controlPanel->console->clear();
  628. }
  629. }
  630. }
  631. bool BattleActionsController::isCastingPossibleHere(const CStack *sactive, const CStack *shere, BattleHex myNumber)
  632. {
  633. creatureCasting = owner.stacksController->activeStackSpellcaster() && !spellDestSelectMode; //TODO: allow creatures to cast aimed spells
  634. bool isCastingPossible = true;
  635. int spellID = -1;
  636. if (creatureCasting)
  637. {
  638. if (owner.stacksController->activeStackSpellToCast() != SpellID::NONE && (shere != sactive)) //can't cast on itself
  639. spellID = owner.stacksController->activeStackSpellToCast(); //TODO: merge with SpellTocast?
  640. }
  641. else //hero casting
  642. {
  643. spellID = spellToCast->actionSubtype;
  644. }
  645. currentSpell = nullptr;
  646. if (spellID >= 0)
  647. currentSpell = CGI->spellh->objects[spellID];
  648. if (currentSpell)
  649. {
  650. const spells::Caster *caster = creatureCasting ? static_cast<const spells::Caster *>(sactive) : static_cast<const spells::Caster *>(owner.curInt->cb->battleGetMyHero());
  651. if (caster == nullptr)
  652. {
  653. isCastingPossible = false;//just in case
  654. }
  655. else
  656. {
  657. const spells::Mode mode = creatureCasting ? spells::Mode::CREATURE_ACTIVE : spells::Mode::HERO;
  658. spells::Target target;
  659. target.emplace_back(myNumber);
  660. spells::BattleCast cast(owner.curInt->cb.get(), caster, mode, currentSpell);
  661. auto m = currentSpell->battleMechanics(&cast);
  662. spells::detail::ProblemImpl problem; //todo: display problem in status bar
  663. isCastingPossible = m->canBeCastAt(target, problem);
  664. }
  665. }
  666. else
  667. isCastingPossible = false;
  668. if (!myNumber.isAvailable() && !shere) //empty tile outside battlefield (or in the unavailable border column)
  669. isCastingPossible = false;
  670. return isCastingPossible;
  671. }
  672. bool BattleActionsController::canStackMoveHere(const CStack * stackToMove, BattleHex myNumber) const
  673. {
  674. std::vector<BattleHex> acc = owner.curInt->cb->battleGetAvailableHexes(stackToMove);
  675. BattleHex shiftedDest = myNumber.cloneInDirection(stackToMove->destShiftDir(), false);
  676. if (vstd::contains(acc, myNumber))
  677. return true;
  678. else if (stackToMove->doubleWide() && vstd::contains(acc, shiftedDest))
  679. return true;
  680. else
  681. return false;
  682. }
  683. void BattleActionsController::activateStack()
  684. {
  685. const CStack * s = owner.stacksController->getActiveStack();
  686. if(s)
  687. possibleActions = getPossibleActionsForStack(s);
  688. }
  689. bool BattleActionsController::spellcastingModeActive() const
  690. {
  691. return spellDestSelectMode;
  692. }
  693. SpellID BattleActionsController::selectedSpell() const
  694. {
  695. if (!spellToCast)
  696. return SpellID::NONE;
  697. return SpellID(spellToCast->actionSubtype);
  698. }