BattleWindow.cpp 20 KB

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