BattleWindow.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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. case PossiblePlayerBattleAction::ANY_LOCATION:
  377. iconName = variables["actionIconSpell"].String();
  378. break;
  379. //TODO: figure out purpose of this icon
  380. //case PossiblePlayerBattleAction::???:
  381. //iconName = variables["actionIconWalk"].String();
  382. //break;
  383. case PossiblePlayerBattleAction::ATTACK_AND_RETURN:
  384. iconName = variables["actionIconReturn"].String();
  385. break;
  386. case PossiblePlayerBattleAction::WALK_AND_ATTACK:
  387. iconName = variables["actionIconNoReturn"].String();
  388. break;
  389. }
  390. auto anim = std::make_shared<CAnimation>(iconName);
  391. w->setImage(anim);
  392. w->redraw();
  393. }
  394. void BattleWindow::setAlternativeActions(const std::list<PossiblePlayerBattleAction> & actions)
  395. {
  396. alternativeActions = actions;
  397. defaultAction = PossiblePlayerBattleAction::INVALID;
  398. if(alternativeActions.size() > 1)
  399. defaultAction = alternativeActions.back();
  400. if(!alternativeActions.empty())
  401. showAlternativeActionIcon(alternativeActions.front());
  402. else
  403. showAlternativeActionIcon(defaultAction);
  404. }
  405. void BattleWindow::bAutofightf()
  406. {
  407. if (owner.actionsController->spellcastingModeActive())
  408. return;
  409. //Stop auto-fight mode
  410. if(owner.curInt->isAutoFightOn)
  411. {
  412. assert(owner.curInt->autofightingAI);
  413. owner.curInt->isAutoFightOn = false;
  414. logGlobal->trace("Stopping the autofight...");
  415. }
  416. else if(!owner.curInt->autofightingAI)
  417. {
  418. owner.curInt->isAutoFightOn = true;
  419. blockUI(true);
  420. auto ai = CDynLibHandler::getNewBattleAI(settings["server"]["friendlyAI"].String());
  421. AutocombatPreferences autocombatPreferences = AutocombatPreferences();
  422. autocombatPreferences.enableSpellsUsage = settings["battle"]["enableAutocombatSpells"].Bool();
  423. ai->initBattleInterface(owner.curInt->env, owner.curInt->cb, autocombatPreferences);
  424. ai->battleStart(owner.army1, owner.army2, int3(0,0,0), owner.attackingHeroInstance, owner.defendingHeroInstance, owner.curInt->cb->battleGetMySide(), false);
  425. owner.curInt->autofightingAI = ai;
  426. owner.curInt->cb->registerBattleInterface(ai);
  427. owner.requestAutofightingAIToTakeAction();
  428. }
  429. }
  430. void BattleWindow::bSpellf()
  431. {
  432. if (owner.actionsController->spellcastingModeActive())
  433. return;
  434. if (!owner.makingTurn())
  435. return;
  436. auto myHero = owner.currentHero();
  437. if(!myHero)
  438. return;
  439. CCS->curh->set(Cursor::Map::POINTER);
  440. ESpellCastProblem::ESpellCastProblem spellCastProblem = owner.curInt->cb->battleCanCastSpell(myHero, spells::Mode::HERO);
  441. if(spellCastProblem == ESpellCastProblem::OK)
  442. {
  443. GH.windows().createAndPushWindow<CSpellWindow>(myHero, owner.curInt.get());
  444. }
  445. else if (spellCastProblem == ESpellCastProblem::MAGIC_IS_BLOCKED)
  446. {
  447. //TODO: move to spell mechanics, add more information to spell cast problem
  448. //Handle Orb of Inhibition-like effects -> we want to display dialog with info, why casting is impossible
  449. auto blockingBonus = owner.currentHero()->getBonusLocalFirst(Selector::type()(BonusType::BLOCK_ALL_MAGIC));
  450. if (!blockingBonus)
  451. return;
  452. if (blockingBonus->source == BonusSource::ARTIFACT)
  453. {
  454. const auto artID = ArtifactID(blockingBonus->sid);
  455. //If we have artifact, put name of our hero. Otherwise assume it's the enemy.
  456. //TODO check who *really* is source of bonus
  457. std::string heroName = myHero->hasArt(artID) ? myHero->getNameTranslated() : owner.enemyHero().name;
  458. //%s wields the %s, an ancient artifact which creates a p dead to all magic.
  459. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[683])
  460. % heroName % CGI->artifacts()->getByIndex(artID)->getNameTranslated()));
  461. }
  462. }
  463. }
  464. void BattleWindow::bSwitchActionf()
  465. {
  466. if(alternativeActions.empty())
  467. return;
  468. if(alternativeActions.front() == defaultAction)
  469. {
  470. alternativeActions.push_back(alternativeActions.front());
  471. alternativeActions.pop_front();
  472. }
  473. auto actions = owner.actionsController->getPossibleActions();
  474. if(!actions.empty() && actions.front() == alternativeActions.front())
  475. {
  476. owner.actionsController->removePossibleAction(alternativeActions.front());
  477. showAlternativeActionIcon(defaultAction);
  478. }
  479. else
  480. {
  481. owner.actionsController->pushFrontPossibleAction(alternativeActions.front());
  482. showAlternativeActionIcon(alternativeActions.front());
  483. }
  484. alternativeActions.push_back(alternativeActions.front());
  485. alternativeActions.pop_front();
  486. }
  487. void BattleWindow::bWaitf()
  488. {
  489. if (owner.actionsController->spellcastingModeActive())
  490. return;
  491. if (owner.stacksController->getActiveStack() != nullptr)
  492. owner.giveCommand(EActionType::WAIT);
  493. }
  494. void BattleWindow::bDefencef()
  495. {
  496. if (owner.actionsController->spellcastingModeActive())
  497. return;
  498. if (owner.stacksController->getActiveStack() != nullptr)
  499. owner.giveCommand(EActionType::DEFEND);
  500. }
  501. void BattleWindow::bConsoleUpf()
  502. {
  503. if (owner.actionsController->spellcastingModeActive())
  504. return;
  505. console->scrollUp();
  506. }
  507. void BattleWindow::bConsoleDownf()
  508. {
  509. if (owner.actionsController->spellcastingModeActive())
  510. return;
  511. console->scrollDown();
  512. }
  513. void BattleWindow::bTacticNextStack()
  514. {
  515. owner.tacticNextStack(nullptr);
  516. }
  517. void BattleWindow::bTacticPhaseEnd()
  518. {
  519. owner.tacticPhaseEnd();
  520. }
  521. void BattleWindow::blockUI(bool on)
  522. {
  523. bool canCastSpells = false;
  524. auto hero = owner.curInt->cb->battleGetMyHero();
  525. if(hero)
  526. {
  527. ESpellCastProblem::ESpellCastProblem spellcastingProblem = owner.curInt->cb->battleCanCastSpell(hero, spells::Mode::HERO);
  528. //if magic is blocked, we leave button active, so the message can be displayed after button click
  529. canCastSpells = spellcastingProblem == ESpellCastProblem::OK || spellcastingProblem == ESpellCastProblem::MAGIC_IS_BLOCKED;
  530. }
  531. bool canWait = owner.stacksController->getActiveStack() ? !owner.stacksController->getActiveStack()->waitedThisTurn : false;
  532. setShortcutBlocked(EShortcut::GLOBAL_OPTIONS, on);
  533. setShortcutBlocked(EShortcut::BATTLE_RETREAT, on || !owner.curInt->cb->battleCanFlee());
  534. setShortcutBlocked(EShortcut::BATTLE_SURRENDER, on || owner.curInt->cb->battleGetSurrenderCost() < 0);
  535. setShortcutBlocked(EShortcut::BATTLE_CAST_SPELL, on || owner.tacticsMode || !canCastSpells);
  536. setShortcutBlocked(EShortcut::BATTLE_WAIT, on || owner.tacticsMode || !canWait);
  537. setShortcutBlocked(EShortcut::BATTLE_DEFEND, on || owner.tacticsMode);
  538. setShortcutBlocked(EShortcut::BATTLE_SELECT_ACTION, on || owner.tacticsMode);
  539. setShortcutBlocked(EShortcut::BATTLE_AUTOCOMBAT, owner.actionsController->spellcastingModeActive());
  540. setShortcutBlocked(EShortcut::BATTLE_TACTICS_END, on && owner.tacticsMode);
  541. setShortcutBlocked(EShortcut::BATTLE_TACTICS_NEXT, on && owner.tacticsMode);
  542. setShortcutBlocked(EShortcut::BATTLE_CONSOLE_DOWN, on && !owner.tacticsMode);
  543. setShortcutBlocked(EShortcut::BATTLE_CONSOLE_UP, on && !owner.tacticsMode);
  544. }
  545. std::optional<uint32_t> BattleWindow::getQueueHoveredUnitId()
  546. {
  547. return queue->getHoveredUnitIdIfAny();
  548. }
  549. void BattleWindow::showAll(Canvas & to)
  550. {
  551. CIntObject::showAll(to);
  552. if (GH.screenDimensions().x != 800 || GH.screenDimensions().y !=600)
  553. CMessage::drawBorder(owner.curInt->playerID, to.getInternalSurface(), pos.w+28, pos.h+29, pos.x-14, pos.y-15);
  554. }
  555. void BattleWindow::show(Canvas & to)
  556. {
  557. CIntObject::show(to);
  558. LOCPLINT->cingconsole->show(to);
  559. }
  560. void BattleWindow::close()
  561. {
  562. if(!GH.windows().isTopWindow(this))
  563. logGlobal->error("Only top interface must be closed");
  564. GH.windows().popWindows(1);
  565. }