BattleWindow.cpp 21 KB

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