BattleWindow.cpp 21 KB

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