BattleWindow.cpp 22 KB

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