BattleWindow.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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::updateStackInfoWindow(const CStack * stack)
  213. {
  214. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  215. bool showInfoWindows = settings["battle"]["stickyHeroInfoWindows"].Bool();
  216. if(stack && stack->unitSide() == BattleSide::DEFENDER)
  217. {
  218. Point position = (GH.screenDimensions().x >= 1000)
  219. ? Point(pos.x + pos.w + 15, pos.y + 250)
  220. : Point(pos.x + pos.w -79, pos.y + 135);
  221. defenderStackWindow = std::make_shared<StackInfoBasicPanel>(stack, &position);
  222. defenderStackWindow->setEnabled(showInfoWindows);
  223. }
  224. else
  225. defenderStackWindow = nullptr;
  226. if(stack && stack->unitSide() == BattleSide::ATTACKER)
  227. {
  228. Point position = (GH.screenDimensions().x >= 1000)
  229. ? Point(pos.x - 93, pos.y + 250)
  230. : Point(pos.x + 1, pos.y + 135);
  231. attackerStackWindow = std::make_shared<StackInfoBasicPanel>(stack, &position);
  232. attackerStackWindow->setEnabled(showInfoWindows);
  233. }
  234. else
  235. attackerStackWindow = nullptr;
  236. }
  237. void BattleWindow::heroManaPointsChanged(const CGHeroInstance * hero)
  238. {
  239. if(hero == owner.attackingHeroInstance || hero == owner.defendingHeroInstance)
  240. {
  241. InfoAboutHero heroInfo = InfoAboutHero();
  242. heroInfo.initFromHero(hero, InfoAboutHero::INBATTLE);
  243. updateHeroInfoWindow(hero == owner.attackingHeroInstance ? 0 : 1, heroInfo);
  244. }
  245. else
  246. {
  247. logGlobal->error("BattleWindow::heroManaPointsChanged: 'Mana points changed' called for hero not belonging to current battle window");
  248. }
  249. }
  250. void BattleWindow::activate()
  251. {
  252. GH.setStatusbar(console);
  253. CIntObject::activate();
  254. LOCPLINT->cingconsole->activate();
  255. }
  256. void BattleWindow::deactivate()
  257. {
  258. GH.setStatusbar(nullptr);
  259. CIntObject::deactivate();
  260. LOCPLINT->cingconsole->deactivate();
  261. }
  262. bool BattleWindow::captureThisKey(EShortcut key)
  263. {
  264. return owner.openingPlaying();
  265. }
  266. void BattleWindow::keyPressed(EShortcut key)
  267. {
  268. if (owner.openingPlaying())
  269. {
  270. owner.openingEnd();
  271. return;
  272. }
  273. InterfaceObjectConfigurable::keyPressed(key);
  274. }
  275. void BattleWindow::clickPressed(const Point & cursorPosition)
  276. {
  277. if (owner.openingPlaying())
  278. {
  279. owner.openingEnd();
  280. return;
  281. }
  282. InterfaceObjectConfigurable::clickPressed(cursorPosition);
  283. }
  284. void BattleWindow::tacticPhaseStarted()
  285. {
  286. auto menuBattle = widget<CIntObject>("menuBattle");
  287. auto console = widget<CIntObject>("console");
  288. auto menuTactics = widget<CIntObject>("menuTactics");
  289. auto tacticNext = widget<CIntObject>("tacticNext");
  290. auto tacticEnd = widget<CIntObject>("tacticEnd");
  291. auto alternativeAction = widget<CIntObject>("alternativeAction");
  292. menuBattle->disable();
  293. console->disable();
  294. if (alternativeAction)
  295. alternativeAction->disable();
  296. menuTactics->enable();
  297. tacticNext->enable();
  298. tacticEnd->enable();
  299. redraw();
  300. }
  301. void BattleWindow::tacticPhaseEnded()
  302. {
  303. auto menuBattle = widget<CIntObject>("menuBattle");
  304. auto console = widget<CIntObject>("console");
  305. auto menuTactics = widget<CIntObject>("menuTactics");
  306. auto tacticNext = widget<CIntObject>("tacticNext");
  307. auto tacticEnd = widget<CIntObject>("tacticEnd");
  308. auto alternativeAction = widget<CIntObject>("alternativeAction");
  309. menuBattle->enable();
  310. console->enable();
  311. if (alternativeAction)
  312. alternativeAction->enable();
  313. menuTactics->disable();
  314. tacticNext->disable();
  315. tacticEnd->disable();
  316. redraw();
  317. }
  318. void BattleWindow::bOptionsf()
  319. {
  320. if (owner.actionsController->spellcastingModeActive())
  321. return;
  322. CCS->curh->set(Cursor::Map::POINTER);
  323. GH.windows().createAndPushWindow<SettingsMainWindow>(&owner);
  324. }
  325. void BattleWindow::bSurrenderf()
  326. {
  327. if (owner.actionsController->spellcastingModeActive())
  328. return;
  329. int cost = owner.getBattle()->battleGetSurrenderCost();
  330. if(cost >= 0)
  331. {
  332. std::string enemyHeroName = owner.getBattle()->battleGetEnemyHero().name;
  333. if(enemyHeroName.empty())
  334. {
  335. logGlobal->warn("Surrender performed without enemy hero, should not happen!");
  336. enemyHeroName = "#ENEMY#";
  337. }
  338. 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."
  339. owner.curInt->showYesNoDialog(surrenderMessage, [this](){ reallySurrender(); }, nullptr);
  340. }
  341. }
  342. void BattleWindow::bFleef()
  343. {
  344. if (owner.actionsController->spellcastingModeActive())
  345. return;
  346. if ( owner.getBattle()->battleCanFlee() )
  347. {
  348. CFunctionList<void()> ony = std::bind(&BattleWindow::reallyFlee,this);
  349. owner.curInt->showYesNoDialog(CGI->generaltexth->allTexts[28], ony, nullptr); //Are you sure you want to retreat?
  350. }
  351. else
  352. {
  353. std::vector<std::shared_ptr<CComponent>> comps;
  354. std::string heroName;
  355. //calculating fleeing hero's name
  356. if (owner.attackingHeroInstance)
  357. if (owner.attackingHeroInstance->tempOwner == owner.curInt->cb->getPlayerID())
  358. heroName = owner.attackingHeroInstance->getNameTranslated();
  359. if (owner.defendingHeroInstance)
  360. if (owner.defendingHeroInstance->tempOwner == owner.curInt->cb->getPlayerID())
  361. heroName = owner.defendingHeroInstance->getNameTranslated();
  362. //calculating text
  363. auto txt = boost::format(CGI->generaltexth->allTexts[340]) % heroName; //The Shackles of War are present. %s can not retreat!
  364. //printing message
  365. owner.curInt->showInfoDialog(boost::str(txt), comps);
  366. }
  367. }
  368. void BattleWindow::reallyFlee()
  369. {
  370. owner.giveCommand(EActionType::RETREAT);
  371. CCS->curh->set(Cursor::Map::POINTER);
  372. }
  373. void BattleWindow::reallySurrender()
  374. {
  375. if (owner.curInt->cb->getResourceAmount(EGameResID::GOLD) < owner.getBattle()->battleGetSurrenderCost())
  376. {
  377. owner.curInt->showInfoDialog(CGI->generaltexth->allTexts[29]); //You don't have enough gold!
  378. }
  379. else
  380. {
  381. owner.giveCommand(EActionType::SURRENDER);
  382. CCS->curh->set(Cursor::Map::POINTER);
  383. }
  384. }
  385. void BattleWindow::showAlternativeActionIcon(PossiblePlayerBattleAction action)
  386. {
  387. auto w = widget<CButton>("alternativeAction");
  388. if(!w)
  389. return;
  390. AnimationPath iconName = AnimationPath::fromJson(variables["actionIconDefault"]);
  391. switch(action.get())
  392. {
  393. case PossiblePlayerBattleAction::ATTACK:
  394. iconName = AnimationPath::fromJson(variables["actionIconAttack"]);
  395. break;
  396. case PossiblePlayerBattleAction::SHOOT:
  397. iconName = AnimationPath::fromJson(variables["actionIconShoot"]);
  398. break;
  399. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  400. iconName = AnimationPath::fromJson(variables["actionIconSpell"]);
  401. break;
  402. case PossiblePlayerBattleAction::ANY_LOCATION:
  403. iconName = AnimationPath::fromJson(variables["actionIconSpell"]);
  404. break;
  405. //TODO: figure out purpose of this icon
  406. //case PossiblePlayerBattleAction::???:
  407. //iconName = variables["actionIconWalk"].String();
  408. //break;
  409. case PossiblePlayerBattleAction::ATTACK_AND_RETURN:
  410. iconName = AnimationPath::fromJson(variables["actionIconReturn"]);
  411. break;
  412. case PossiblePlayerBattleAction::WALK_AND_ATTACK:
  413. iconName = AnimationPath::fromJson(variables["actionIconNoReturn"]);
  414. break;
  415. }
  416. auto anim = GH.renderHandler().loadAnimation(iconName);
  417. w->setImage(anim);
  418. w->redraw();
  419. }
  420. void BattleWindow::setAlternativeActions(const std::list<PossiblePlayerBattleAction> & actions)
  421. {
  422. alternativeActions = actions;
  423. defaultAction = PossiblePlayerBattleAction::INVALID;
  424. if(alternativeActions.size() > 1)
  425. defaultAction = alternativeActions.back();
  426. if(!alternativeActions.empty())
  427. showAlternativeActionIcon(alternativeActions.front());
  428. else
  429. showAlternativeActionIcon(defaultAction);
  430. }
  431. void BattleWindow::bAutofightf()
  432. {
  433. if (owner.actionsController->spellcastingModeActive())
  434. return;
  435. //Stop auto-fight mode
  436. if(owner.curInt->isAutoFightOn)
  437. {
  438. assert(owner.curInt->autofightingAI);
  439. owner.curInt->isAutoFightOn = false;
  440. logGlobal->trace("Stopping the autofight...");
  441. }
  442. else if(!owner.curInt->autofightingAI)
  443. {
  444. owner.curInt->isAutoFightOn = true;
  445. blockUI(true);
  446. auto ai = CDynLibHandler::getNewBattleAI(settings["server"]["friendlyAI"].String());
  447. AutocombatPreferences autocombatPreferences = AutocombatPreferences();
  448. autocombatPreferences.enableSpellsUsage = settings["battle"]["enableAutocombatSpells"].Bool();
  449. ai->initBattleInterface(owner.curInt->env, owner.curInt->cb, autocombatPreferences);
  450. ai->battleStart(owner.getBattleID(), owner.army1, owner.army2, int3(0,0,0), owner.attackingHeroInstance, owner.defendingHeroInstance, owner.getBattle()->battleGetMySide(), false);
  451. owner.curInt->autofightingAI = ai;
  452. owner.curInt->cb->registerBattleInterface(ai);
  453. owner.requestAutofightingAIToTakeAction();
  454. }
  455. }
  456. void BattleWindow::bSpellf()
  457. {
  458. if (owner.actionsController->spellcastingModeActive())
  459. return;
  460. if (!owner.makingTurn())
  461. return;
  462. auto myHero = owner.currentHero();
  463. if(!myHero)
  464. return;
  465. CCS->curh->set(Cursor::Map::POINTER);
  466. ESpellCastProblem spellCastProblem = owner.getBattle()->battleCanCastSpell(myHero, spells::Mode::HERO);
  467. if(spellCastProblem == ESpellCastProblem::OK)
  468. {
  469. GH.windows().createAndPushWindow<CSpellWindow>(myHero, owner.curInt.get());
  470. }
  471. else if (spellCastProblem == ESpellCastProblem::MAGIC_IS_BLOCKED)
  472. {
  473. //TODO: move to spell mechanics, add more information to spell cast problem
  474. //Handle Orb of Inhibition-like effects -> we want to display dialog with info, why casting is impossible
  475. auto blockingBonus = owner.currentHero()->getBonusLocalFirst(Selector::type()(BonusType::BLOCK_ALL_MAGIC));
  476. if (!blockingBonus)
  477. return;
  478. if (blockingBonus->source == BonusSource::ARTIFACT)
  479. {
  480. const auto artID = blockingBonus->sid.as<ArtifactID>();
  481. //If we have artifact, put name of our hero. Otherwise assume it's the enemy.
  482. //TODO check who *really* is source of bonus
  483. std::string heroName = myHero->hasArt(artID) ? myHero->getNameTranslated() : owner.enemyHero().name;
  484. //%s wields the %s, an ancient artifact which creates a p dead to all magic.
  485. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[683])
  486. % heroName % CGI->artifacts()->getByIndex(artID)->getNameTranslated()));
  487. }
  488. else if(blockingBonus->source == BonusSource::OBJECT_TYPE)
  489. {
  490. if(blockingBonus->sid.as<MapObjectID>() == Obj::GARRISON || blockingBonus->sid.as<MapObjectID>() == Obj::GARRISON2)
  491. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[684]);
  492. }
  493. }
  494. else
  495. {
  496. logGlobal->warn("Unexpected problem with readiness to cast spell");
  497. }
  498. }
  499. void BattleWindow::bSwitchActionf()
  500. {
  501. if(alternativeActions.empty())
  502. return;
  503. if(alternativeActions.front() == defaultAction)
  504. {
  505. alternativeActions.push_back(alternativeActions.front());
  506. alternativeActions.pop_front();
  507. }
  508. auto actions = owner.actionsController->getPossibleActions();
  509. if(!actions.empty() && actions.front() == alternativeActions.front())
  510. {
  511. owner.actionsController->removePossibleAction(alternativeActions.front());
  512. showAlternativeActionIcon(defaultAction);
  513. }
  514. else
  515. {
  516. owner.actionsController->pushFrontPossibleAction(alternativeActions.front());
  517. showAlternativeActionIcon(alternativeActions.front());
  518. }
  519. alternativeActions.push_back(alternativeActions.front());
  520. alternativeActions.pop_front();
  521. }
  522. void BattleWindow::bWaitf()
  523. {
  524. if (owner.actionsController->spellcastingModeActive())
  525. return;
  526. if (owner.stacksController->getActiveStack() != nullptr)
  527. owner.giveCommand(EActionType::WAIT);
  528. }
  529. void BattleWindow::bDefencef()
  530. {
  531. if (owner.actionsController->spellcastingModeActive())
  532. return;
  533. if (owner.stacksController->getActiveStack() != nullptr)
  534. owner.giveCommand(EActionType::DEFEND);
  535. }
  536. void BattleWindow::bConsoleUpf()
  537. {
  538. if (owner.actionsController->spellcastingModeActive())
  539. return;
  540. console->scrollUp();
  541. }
  542. void BattleWindow::bConsoleDownf()
  543. {
  544. if (owner.actionsController->spellcastingModeActive())
  545. return;
  546. console->scrollDown();
  547. }
  548. void BattleWindow::bTacticNextStack()
  549. {
  550. owner.tacticNextStack(nullptr);
  551. }
  552. void BattleWindow::bTacticPhaseEnd()
  553. {
  554. owner.tacticPhaseEnd();
  555. }
  556. void BattleWindow::blockUI(bool on)
  557. {
  558. bool canCastSpells = false;
  559. auto hero = owner.getBattle()->battleGetMyHero();
  560. if(hero)
  561. {
  562. ESpellCastProblem spellcastingProblem = owner.getBattle()->battleCanCastSpell(hero, spells::Mode::HERO);
  563. //if magic is blocked, we leave button active, so the message can be displayed after button click
  564. canCastSpells = spellcastingProblem == ESpellCastProblem::OK || spellcastingProblem == ESpellCastProblem::MAGIC_IS_BLOCKED;
  565. }
  566. bool canWait = owner.stacksController->getActiveStack() ? !owner.stacksController->getActiveStack()->waitedThisTurn : false;
  567. setShortcutBlocked(EShortcut::GLOBAL_OPTIONS, on);
  568. setShortcutBlocked(EShortcut::BATTLE_RETREAT, on || !owner.getBattle()->battleCanFlee());
  569. setShortcutBlocked(EShortcut::BATTLE_SURRENDER, on || owner.getBattle()->battleGetSurrenderCost() < 0);
  570. setShortcutBlocked(EShortcut::BATTLE_CAST_SPELL, on || owner.tacticsMode || !canCastSpells);
  571. setShortcutBlocked(EShortcut::BATTLE_WAIT, on || owner.tacticsMode || !canWait);
  572. setShortcutBlocked(EShortcut::BATTLE_DEFEND, on || owner.tacticsMode);
  573. setShortcutBlocked(EShortcut::BATTLE_SELECT_ACTION, on || owner.tacticsMode);
  574. setShortcutBlocked(EShortcut::BATTLE_AUTOCOMBAT, owner.actionsController->spellcastingModeActive());
  575. setShortcutBlocked(EShortcut::BATTLE_TACTICS_END, on && owner.tacticsMode);
  576. setShortcutBlocked(EShortcut::BATTLE_TACTICS_NEXT, on && owner.tacticsMode);
  577. setShortcutBlocked(EShortcut::BATTLE_CONSOLE_DOWN, on && !owner.tacticsMode);
  578. setShortcutBlocked(EShortcut::BATTLE_CONSOLE_UP, on && !owner.tacticsMode);
  579. }
  580. std::optional<uint32_t> BattleWindow::getQueueHoveredUnitId()
  581. {
  582. return queue->getHoveredUnitIdIfAny();
  583. }
  584. void BattleWindow::showAll(Canvas & to)
  585. {
  586. CIntObject::showAll(to);
  587. if (GH.screenDimensions().x != 800 || GH.screenDimensions().y !=600)
  588. CMessage::drawBorder(owner.curInt->playerID, to.getInternalSurface(), pos.w+28, pos.h+29, pos.x-14, pos.y-15);
  589. }
  590. void BattleWindow::show(Canvas & to)
  591. {
  592. CIntObject::show(to);
  593. LOCPLINT->cingconsole->show(to);
  594. }
  595. void BattleWindow::close()
  596. {
  597. if(!GH.windows().isTopWindow(this))
  598. logGlobal->error("Only top interface must be closed");
  599. GH.windows().popWindows(1);
  600. }