BattleWindow.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. /*
  2. * BattleWindow.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 "BattleWindow.h"
  12. #include "BattleInterface.h"
  13. #include "BattleInterfaceClasses.h"
  14. #include "BattleFieldController.h"
  15. #include "BattleStacksController.h"
  16. #include "BattleActionsController.h"
  17. #include "../CGameInfo.h"
  18. #include "../CPlayerInterface.h"
  19. #include "../CMusicHandler.h"
  20. #include "../gui/CursorHandler.h"
  21. #include "../gui/CGuiHandler.h"
  22. #include "../gui/Shortcut.h"
  23. #include "../gui/WindowHandler.h"
  24. #include "../windows/CSpellWindow.h"
  25. #include "../widgets/Buttons.h"
  26. #include "../widgets/Images.h"
  27. #include "../windows/CMessage.h"
  28. #include "../render/CAnimation.h"
  29. #include "../render/Canvas.h"
  30. #include "../adventureMap/CInGameConsole.h"
  31. #include "../../CCallback.h"
  32. #include "../../lib/CGeneralTextHandler.h"
  33. #include "../../lib/gameState/InfoAboutArmy.h"
  34. #include "../../lib/mapObjects/CGHeroInstance.h"
  35. #include "../../lib/CStack.h"
  36. #include "../../lib/CConfigHandler.h"
  37. #include "../../lib/filesystem/ResourceID.h"
  38. #include "../windows/settings/SettingsMainWindow.h"
  39. BattleWindow::BattleWindow(BattleInterface & owner):
  40. owner(owner),
  41. defaultAction(PossiblePlayerBattleAction::INVALID)
  42. {
  43. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  44. pos.w = 800;
  45. pos.h = 600;
  46. pos = center();
  47. REGISTER_BUILDER("battleConsole", &BattleWindow::buildBattleConsole);
  48. const JsonNode config(ResourceID("config/widgets/BattleWindow2.json"));
  49. addShortcut(EShortcut::GLOBAL_OPTIONS, std::bind(&BattleWindow::bOptionsf, this));
  50. addShortcut(EShortcut::BATTLE_SURRENDER, std::bind(&BattleWindow::bSurrenderf, this));
  51. addShortcut(EShortcut::BATTLE_RETREAT, std::bind(&BattleWindow::bFleef, this));
  52. addShortcut(EShortcut::BATTLE_AUTOCOMBAT, std::bind(&BattleWindow::bAutofightf, this));
  53. addShortcut(EShortcut::BATTLE_CAST_SPELL, std::bind(&BattleWindow::bSpellf, this));
  54. addShortcut(EShortcut::BATTLE_WAIT, std::bind(&BattleWindow::bWaitf, this));
  55. addShortcut(EShortcut::BATTLE_DEFEND, std::bind(&BattleWindow::bDefencef, this));
  56. addShortcut(EShortcut::BATTLE_CONSOLE_UP, std::bind(&BattleWindow::bConsoleUpf, this));
  57. addShortcut(EShortcut::BATTLE_CONSOLE_DOWN, std::bind(&BattleWindow::bConsoleDownf, this));
  58. addShortcut(EShortcut::BATTLE_TACTICS_NEXT, std::bind(&BattleWindow::bTacticNextStack, this));
  59. addShortcut(EShortcut::BATTLE_TACTICS_END, std::bind(&BattleWindow::bTacticPhaseEnd, this));
  60. addShortcut(EShortcut::BATTLE_SELECT_ACTION, std::bind(&BattleWindow::bSwitchActionf, this));
  61. addShortcut(EShortcut::BATTLE_TOGGLE_QUEUE, [this](){ this->toggleQueueVisibility();});
  62. addShortcut(EShortcut::BATTLE_TOGGLE_HEROES_STATS, [this](){ this->toggleStickyHeroWindowsVisibility();});
  63. addShortcut(EShortcut::BATTLE_USE_CREATURE_SPELL, [this](){ this->owner.actionsController->enterCreatureCastingMode(); });
  64. addShortcut(EShortcut::GLOBAL_CANCEL, [this](){ this->owner.actionsController->endCastingSpell(); });
  65. build(config);
  66. console = widget<BattleConsole>("console");
  67. owner.console = console;
  68. owner.fieldController.reset( new BattleFieldController(owner));
  69. owner.fieldController->createHeroes();
  70. createQueue();
  71. createStickyHeroInfoWindows();
  72. if ( owner.tacticsMode )
  73. tacticPhaseStarted();
  74. else
  75. tacticPhaseEnded();
  76. addUsedEvents(LCLICK | KEYBOARD);
  77. }
  78. void BattleWindow::createQueue()
  79. {
  80. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  81. //create stack queue and adjust our own position
  82. bool embedQueue;
  83. bool showQueue = settings["battle"]["showQueue"].Bool();
  84. std::string queueSize = settings["battle"]["queueSize"].String();
  85. if(queueSize == "auto")
  86. embedQueue = GH.screenDimensions().y < 700;
  87. else
  88. embedQueue = GH.screenDimensions().y < 700 || queueSize == "small";
  89. queue = std::make_shared<StackQueue>(embedQueue, owner);
  90. if(!embedQueue && showQueue)
  91. {
  92. //re-center, taking into account stack queue position
  93. pos.y -= queue->pos.h;
  94. pos.h += queue->pos.h;
  95. pos = center();
  96. }
  97. if (!showQueue)
  98. queue->disable();
  99. }
  100. void BattleWindow::createStickyHeroInfoWindows()
  101. {
  102. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  103. if(owner.defendingHeroInstance)
  104. {
  105. InfoAboutHero info;
  106. info.initFromHero(owner.defendingHeroInstance, InfoAboutHero::EInfoLevel::INBATTLE);
  107. Point position = (GH.screenDimensions().x >= 1000)
  108. ? Point(pos.x + pos.w + 15, pos.y)
  109. : Point(pos.x + pos.w -79, pos.y + 135);
  110. defenderHeroWindow = std::make_shared<HeroInfoBasicPanel>(info, &position);
  111. }
  112. if(owner.attackingHeroInstance)
  113. {
  114. InfoAboutHero info;
  115. info.initFromHero(owner.attackingHeroInstance, InfoAboutHero::EInfoLevel::INBATTLE);
  116. Point position = (GH.screenDimensions().x >= 1000)
  117. ? Point(pos.x - 93, pos.y)
  118. : Point(pos.x + 1, pos.y + 135);
  119. attackerHeroWindow = std::make_shared<HeroInfoBasicPanel>(info, &position);
  120. }
  121. bool showInfoWindows = settings["battle"]["stickyHeroInfoWindows"].Bool();
  122. if(!showInfoWindows)
  123. {
  124. if(attackerHeroWindow)
  125. attackerHeroWindow->disable();
  126. if(defenderHeroWindow)
  127. defenderHeroWindow->disable();
  128. }
  129. }
  130. BattleWindow::~BattleWindow()
  131. {
  132. CPlayerInterface::battleInt = nullptr;
  133. }
  134. std::shared_ptr<BattleConsole> BattleWindow::buildBattleConsole(const JsonNode & config) const
  135. {
  136. auto rect = readRect(config["rect"]);
  137. auto offset = readPosition(config["imagePosition"]);
  138. auto background = widget<CPicture>("menuBattle");
  139. return std::make_shared<BattleConsole>(background, rect.topLeft(), offset, rect.dimensions() );
  140. }
  141. void BattleWindow::toggleQueueVisibility()
  142. {
  143. if(settings["battle"]["showQueue"].Bool())
  144. hideQueue();
  145. else
  146. showQueue();
  147. }
  148. void BattleWindow::hideQueue()
  149. {
  150. if(settings["battle"]["showQueue"].Bool() == false)
  151. return;
  152. Settings showQueue = settings.write["battle"]["showQueue"];
  153. showQueue->Bool() = false;
  154. queue->disable();
  155. if (!queue->embedded)
  156. {
  157. //re-center, taking into account stack queue position
  158. pos.y += queue->pos.h;
  159. pos.h -= queue->pos.h;
  160. pos = center();
  161. }
  162. GH.windows().totalRedraw();
  163. }
  164. void BattleWindow::showQueue()
  165. {
  166. if(settings["battle"]["showQueue"].Bool() == true)
  167. return;
  168. Settings showQueue = settings.write["battle"]["showQueue"];
  169. showQueue->Bool() = true;
  170. createQueue();
  171. updateQueue();
  172. GH.windows().totalRedraw();
  173. }
  174. void BattleWindow::toggleStickyHeroWindowsVisibility()
  175. {
  176. if(settings["battle"]["stickyHeroInfoWindows"].Bool())
  177. hideStickyHeroWindows();
  178. else
  179. showStickyHeroWindows();
  180. }
  181. void BattleWindow::hideStickyHeroWindows()
  182. {
  183. if(settings["battle"]["stickyHeroInfoWindows"].Bool() == false)
  184. return;
  185. Settings showStickyHeroInfoWindows = settings.write["battle"]["stickyHeroInfoWindows"];
  186. showStickyHeroInfoWindows->Bool() = false;
  187. if(attackerHeroWindow)
  188. attackerHeroWindow->disable();
  189. if(defenderHeroWindow)
  190. defenderHeroWindow->disable();
  191. GH.windows().totalRedraw();
  192. }
  193. void BattleWindow::showStickyHeroWindows()
  194. {
  195. if(settings["battle"]["stickyHeroInfoWindows"].Bool() == true)
  196. return;
  197. Settings showStickyHeroInfoWindows = settings.write["battle"]["stickyHeroInfoWindows"];
  198. showStickyHeroInfoWindows->Bool() = true;
  199. createStickyHeroInfoWindows();
  200. GH.windows().totalRedraw();
  201. }
  202. void BattleWindow::updateQueue()
  203. {
  204. queue->update();
  205. }
  206. void BattleWindow::updateHeroInfoWindow(uint8_t side, const InfoAboutHero & hero)
  207. {
  208. std::shared_ptr<HeroInfoBasicPanel> panelToUpdate = side == 0 ? attackerHeroWindow : defenderHeroWindow;
  209. panelToUpdate->update(hero);
  210. }
  211. void BattleWindow::heroManaPointsChanged(const CGHeroInstance * hero)
  212. {
  213. if(hero == owner.attackingHeroInstance || hero == owner.defendingHeroInstance)
  214. {
  215. InfoAboutHero heroInfo = InfoAboutHero();
  216. heroInfo.initFromHero(hero, InfoAboutHero::INBATTLE);
  217. updateHeroInfoWindow(hero == owner.attackingHeroInstance ? 0 : 1, heroInfo);
  218. }
  219. else
  220. {
  221. logGlobal->error("BattleWindow::heroManaPointsChanged: 'Mana points changed' called for hero not belonging to current battle window");
  222. }
  223. }
  224. void BattleWindow::activate()
  225. {
  226. GH.setStatusbar(console);
  227. CIntObject::activate();
  228. LOCPLINT->cingconsole->activate();
  229. }
  230. void BattleWindow::deactivate()
  231. {
  232. GH.setStatusbar(nullptr);
  233. CIntObject::deactivate();
  234. LOCPLINT->cingconsole->deactivate();
  235. }
  236. bool BattleWindow::captureThisKey(EShortcut key)
  237. {
  238. return owner.openingPlaying();
  239. }
  240. void BattleWindow::keyPressed(EShortcut key)
  241. {
  242. if (owner.openingPlaying())
  243. {
  244. owner.openingEnd();
  245. return;
  246. }
  247. InterfaceObjectConfigurable::keyPressed(key);
  248. }
  249. void BattleWindow::clickPressed(const Point & cursorPosition)
  250. {
  251. if (owner.openingPlaying())
  252. {
  253. owner.openingEnd();
  254. return;
  255. }
  256. InterfaceObjectConfigurable::clickPressed(cursorPosition);
  257. }
  258. void BattleWindow::tacticPhaseStarted()
  259. {
  260. auto menuBattle = widget<CIntObject>("menuBattle");
  261. auto console = widget<CIntObject>("console");
  262. auto menuTactics = widget<CIntObject>("menuTactics");
  263. auto tacticNext = widget<CIntObject>("tacticNext");
  264. auto tacticEnd = widget<CIntObject>("tacticEnd");
  265. auto alternativeAction = widget<CIntObject>("alternativeAction");
  266. menuBattle->disable();
  267. console->disable();
  268. if (alternativeAction)
  269. alternativeAction->disable();
  270. menuTactics->enable();
  271. tacticNext->enable();
  272. tacticEnd->enable();
  273. redraw();
  274. }
  275. void BattleWindow::tacticPhaseEnded()
  276. {
  277. auto menuBattle = widget<CIntObject>("menuBattle");
  278. auto console = widget<CIntObject>("console");
  279. auto menuTactics = widget<CIntObject>("menuTactics");
  280. auto tacticNext = widget<CIntObject>("tacticNext");
  281. auto tacticEnd = widget<CIntObject>("tacticEnd");
  282. auto alternativeAction = widget<CIntObject>("alternativeAction");
  283. menuBattle->enable();
  284. console->enable();
  285. if (alternativeAction)
  286. alternativeAction->enable();
  287. menuTactics->disable();
  288. tacticNext->disable();
  289. tacticEnd->disable();
  290. redraw();
  291. }
  292. void BattleWindow::bOptionsf()
  293. {
  294. if (owner.actionsController->spellcastingModeActive())
  295. return;
  296. CCS->curh->set(Cursor::Map::POINTER);
  297. GH.windows().createAndPushWindow<SettingsMainWindow>(&owner);
  298. }
  299. void BattleWindow::bSurrenderf()
  300. {
  301. if (owner.actionsController->spellcastingModeActive())
  302. return;
  303. int cost = owner.curInt->cb->battleGetSurrenderCost();
  304. if(cost >= 0)
  305. {
  306. std::string enemyHeroName = owner.curInt->cb->battleGetEnemyHero().name;
  307. if(enemyHeroName.empty())
  308. {
  309. logGlobal->warn("Surrender performed without enemy hero, should not happen!");
  310. enemyHeroName = "#ENEMY#";
  311. }
  312. std::string surrenderMessage = boost::str(boost::format(CGI->generaltexth->allTexts[32]) % enemyHeroName % cost); //%s states: "I will accept your surrender and grant you and your troops safe passage for the price of %d gold."
  313. owner.curInt->showYesNoDialog(surrenderMessage, [this](){ reallySurrender(); }, nullptr);
  314. }
  315. }
  316. void BattleWindow::bFleef()
  317. {
  318. if (owner.actionsController->spellcastingModeActive())
  319. return;
  320. if ( owner.curInt->cb->battleCanFlee() )
  321. {
  322. CFunctionList<void()> ony = std::bind(&BattleWindow::reallyFlee,this);
  323. owner.curInt->showYesNoDialog(CGI->generaltexth->allTexts[28], ony, nullptr); //Are you sure you want to retreat?
  324. }
  325. else
  326. {
  327. std::vector<std::shared_ptr<CComponent>> comps;
  328. std::string heroName;
  329. //calculating fleeing hero's name
  330. if (owner.attackingHeroInstance)
  331. if (owner.attackingHeroInstance->tempOwner == owner.curInt->cb->getMyColor())
  332. heroName = owner.attackingHeroInstance->getNameTranslated();
  333. if (owner.defendingHeroInstance)
  334. if (owner.defendingHeroInstance->tempOwner == owner.curInt->cb->getMyColor())
  335. heroName = owner.defendingHeroInstance->getNameTranslated();
  336. //calculating text
  337. auto txt = boost::format(CGI->generaltexth->allTexts[340]) % heroName; //The Shackles of War are present. %s can not retreat!
  338. //printing message
  339. owner.curInt->showInfoDialog(boost::to_string(txt), comps);
  340. }
  341. }
  342. void BattleWindow::reallyFlee()
  343. {
  344. owner.giveCommand(EActionType::RETREAT);
  345. CCS->curh->set(Cursor::Map::POINTER);
  346. }
  347. void BattleWindow::reallySurrender()
  348. {
  349. if (owner.curInt->cb->getResourceAmount(EGameResID::GOLD) < owner.curInt->cb->battleGetSurrenderCost())
  350. {
  351. owner.curInt->showInfoDialog(CGI->generaltexth->allTexts[29]); //You don't have enough gold!
  352. }
  353. else
  354. {
  355. owner.giveCommand(EActionType::SURRENDER);
  356. CCS->curh->set(Cursor::Map::POINTER);
  357. }
  358. }
  359. void BattleWindow::showAlternativeActionIcon(PossiblePlayerBattleAction action)
  360. {
  361. auto w = widget<CButton>("alternativeAction");
  362. if(!w)
  363. return;
  364. std::string iconName = variables["actionIconDefault"].String();
  365. switch(action.get())
  366. {
  367. case PossiblePlayerBattleAction::ATTACK:
  368. iconName = variables["actionIconAttack"].String();
  369. break;
  370. case PossiblePlayerBattleAction::SHOOT:
  371. iconName = variables["actionIconShoot"].String();
  372. break;
  373. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  374. iconName = variables["actionIconSpell"].String();
  375. break;
  376. //TODO: figure out purpose of this icon
  377. //case PossiblePlayerBattleAction::???:
  378. //iconName = variables["actionIconWalk"].String();
  379. //break;
  380. case PossiblePlayerBattleAction::ATTACK_AND_RETURN:
  381. iconName = variables["actionIconReturn"].String();
  382. break;
  383. case PossiblePlayerBattleAction::WALK_AND_ATTACK:
  384. iconName = variables["actionIconNoReturn"].String();
  385. break;
  386. }
  387. auto anim = std::make_shared<CAnimation>(iconName);
  388. w->setImage(anim);
  389. w->redraw();
  390. }
  391. void BattleWindow::setAlternativeActions(const std::list<PossiblePlayerBattleAction> & actions)
  392. {
  393. alternativeActions = actions;
  394. defaultAction = PossiblePlayerBattleAction::INVALID;
  395. if(alternativeActions.size() > 1)
  396. defaultAction = alternativeActions.back();
  397. if(!alternativeActions.empty())
  398. showAlternativeActionIcon(alternativeActions.front());
  399. else
  400. showAlternativeActionIcon(defaultAction);
  401. }
  402. void BattleWindow::bAutofightf()
  403. {
  404. if (owner.actionsController->spellcastingModeActive())
  405. return;
  406. //Stop auto-fight mode
  407. if(owner.curInt->isAutoFightOn)
  408. {
  409. assert(owner.curInt->autofightingAI);
  410. owner.curInt->isAutoFightOn = false;
  411. logGlobal->trace("Stopping the autofight...");
  412. }
  413. else if(!owner.curInt->autofightingAI)
  414. {
  415. owner.curInt->isAutoFightOn = true;
  416. blockUI(true);
  417. auto ai = CDynLibHandler::getNewBattleAI(settings["server"]["friendlyAI"].String());
  418. ai->initBattleInterface(owner.curInt->env, owner.curInt->cb);
  419. ai->battleStart(owner.army1, owner.army2, int3(0,0,0), owner.attackingHeroInstance, owner.defendingHeroInstance, owner.curInt->cb->battleGetMySide(), false);
  420. owner.curInt->autofightingAI = ai;
  421. owner.curInt->cb->registerBattleInterface(ai);
  422. owner.requestAutofightingAIToTakeAction();
  423. }
  424. }
  425. void BattleWindow::bSpellf()
  426. {
  427. if (owner.actionsController->spellcastingModeActive())
  428. return;
  429. if (!owner.makingTurn())
  430. return;
  431. auto myHero = owner.currentHero();
  432. if(!myHero)
  433. return;
  434. CCS->curh->set(Cursor::Map::POINTER);
  435. ESpellCastProblem::ESpellCastProblem spellCastProblem = owner.curInt->cb->battleCanCastSpell(myHero, spells::Mode::HERO);
  436. if(spellCastProblem == ESpellCastProblem::OK)
  437. {
  438. GH.windows().createAndPushWindow<CSpellWindow>(myHero, owner.curInt.get());
  439. }
  440. else if (spellCastProblem == ESpellCastProblem::MAGIC_IS_BLOCKED)
  441. {
  442. //TODO: move to spell mechanics, add more information to spell cast problem
  443. //Handle Orb of Inhibition-like effects -> we want to display dialog with info, why casting is impossible
  444. auto blockingBonus = owner.currentHero()->getBonusLocalFirst(Selector::type()(BonusType::BLOCK_ALL_MAGIC));
  445. if (!blockingBonus)
  446. return;
  447. if (blockingBonus->source == BonusSource::ARTIFACT)
  448. {
  449. const auto artID = ArtifactID(blockingBonus->sid);
  450. //If we have artifact, put name of our hero. Otherwise assume it's the enemy.
  451. //TODO check who *really* is source of bonus
  452. std::string heroName = myHero->hasArt(artID) ? myHero->getNameTranslated() : owner.enemyHero().name;
  453. //%s wields the %s, an ancient artifact which creates a p dead to all magic.
  454. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[683])
  455. % heroName % CGI->artifacts()->getByIndex(artID)->getNameTranslated()));
  456. }
  457. }
  458. }
  459. void BattleWindow::bSwitchActionf()
  460. {
  461. if(alternativeActions.empty())
  462. return;
  463. if(alternativeActions.front() == defaultAction)
  464. {
  465. alternativeActions.push_back(alternativeActions.front());
  466. alternativeActions.pop_front();
  467. }
  468. auto actions = owner.actionsController->getPossibleActions();
  469. if(!actions.empty() && actions.front() == alternativeActions.front())
  470. {
  471. owner.actionsController->removePossibleAction(alternativeActions.front());
  472. showAlternativeActionIcon(defaultAction);
  473. }
  474. else
  475. {
  476. owner.actionsController->pushFrontPossibleAction(alternativeActions.front());
  477. showAlternativeActionIcon(alternativeActions.front());
  478. }
  479. alternativeActions.push_back(alternativeActions.front());
  480. alternativeActions.pop_front();
  481. }
  482. void BattleWindow::bWaitf()
  483. {
  484. if (owner.actionsController->spellcastingModeActive())
  485. return;
  486. if (owner.stacksController->getActiveStack() != nullptr)
  487. owner.giveCommand(EActionType::WAIT);
  488. }
  489. void BattleWindow::bDefencef()
  490. {
  491. if (owner.actionsController->spellcastingModeActive())
  492. return;
  493. if (owner.stacksController->getActiveStack() != nullptr)
  494. owner.giveCommand(EActionType::DEFEND);
  495. }
  496. void BattleWindow::bConsoleUpf()
  497. {
  498. if (owner.actionsController->spellcastingModeActive())
  499. return;
  500. console->scrollUp();
  501. }
  502. void BattleWindow::bConsoleDownf()
  503. {
  504. if (owner.actionsController->spellcastingModeActive())
  505. return;
  506. console->scrollDown();
  507. }
  508. void BattleWindow::bTacticNextStack()
  509. {
  510. owner.tacticNextStack(nullptr);
  511. }
  512. void BattleWindow::bTacticPhaseEnd()
  513. {
  514. owner.tacticPhaseEnd();
  515. }
  516. void BattleWindow::blockUI(bool on)
  517. {
  518. bool canCastSpells = false;
  519. auto hero = owner.curInt->cb->battleGetMyHero();
  520. if(hero)
  521. {
  522. ESpellCastProblem::ESpellCastProblem spellcastingProblem = owner.curInt->cb->battleCanCastSpell(hero, spells::Mode::HERO);
  523. //if magic is blocked, we leave button active, so the message can be displayed after button click
  524. canCastSpells = spellcastingProblem == ESpellCastProblem::OK || spellcastingProblem == ESpellCastProblem::MAGIC_IS_BLOCKED;
  525. }
  526. bool canWait = owner.stacksController->getActiveStack() ? !owner.stacksController->getActiveStack()->waitedThisTurn : false;
  527. setShortcutBlocked(EShortcut::GLOBAL_OPTIONS, on);
  528. setShortcutBlocked(EShortcut::BATTLE_RETREAT, on || !owner.curInt->cb->battleCanFlee());
  529. setShortcutBlocked(EShortcut::BATTLE_SURRENDER, on || owner.curInt->cb->battleGetSurrenderCost() < 0);
  530. setShortcutBlocked(EShortcut::BATTLE_CAST_SPELL, on || owner.tacticsMode || !canCastSpells);
  531. setShortcutBlocked(EShortcut::BATTLE_WAIT, on || owner.tacticsMode || !canWait);
  532. setShortcutBlocked(EShortcut::BATTLE_DEFEND, on || owner.tacticsMode);
  533. setShortcutBlocked(EShortcut::BATTLE_SELECT_ACTION, on || owner.tacticsMode);
  534. setShortcutBlocked(EShortcut::BATTLE_AUTOCOMBAT, owner.actionsController->spellcastingModeActive());
  535. setShortcutBlocked(EShortcut::BATTLE_TACTICS_END, on && owner.tacticsMode);
  536. setShortcutBlocked(EShortcut::BATTLE_TACTICS_NEXT, on && owner.tacticsMode);
  537. setShortcutBlocked(EShortcut::BATTLE_CONSOLE_DOWN, on && !owner.tacticsMode);
  538. setShortcutBlocked(EShortcut::BATTLE_CONSOLE_UP, on && !owner.tacticsMode);
  539. }
  540. std::optional<uint32_t> BattleWindow::getQueueHoveredUnitId()
  541. {
  542. return queue->getHoveredUnitIdIfAny();
  543. }
  544. void BattleWindow::showAll(Canvas & to)
  545. {
  546. CIntObject::showAll(to);
  547. if (GH.screenDimensions().x != 800 || GH.screenDimensions().y !=600)
  548. CMessage::drawBorder(owner.curInt->playerID, to.getInternalSurface(), pos.w+28, pos.h+29, pos.x-14, pos.y-15);
  549. }
  550. void BattleWindow::show(Canvas & to)
  551. {
  552. CIntObject::show(to);
  553. LOCPLINT->cingconsole->show(to);
  554. }
  555. void BattleWindow::close()
  556. {
  557. if(!GH.windows().isTopWindow(this))
  558. logGlobal->error("Only top interface must be closed");
  559. GH.windows().popWindows(1);
  560. }