CBattleControlPanel.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * CBattleControlPanel.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 "CBattleControlPanel.h"
  12. #include "CBattleInterface.h"
  13. #include "CBattleInterfaceClasses.h"
  14. #include "CBattleStacksController.h"
  15. #include "CBattleActionsController.h"
  16. #include "../widgets/Buttons.h"
  17. #include "../widgets/Images.h"
  18. #include "../CGameInfo.h"
  19. #include "../../lib/CGeneralTextHandler.h"
  20. #include "../../lib/mapObjects/CGHeroInstance.h"
  21. #include "../CPlayerInterface.h"
  22. #include "../../CCallback.h"
  23. #include "../gui/CCursorHandler.h"
  24. #include "../gui/CGuiHandler.h"
  25. #include "../windows/CSpellWindow.h"
  26. #include "../../lib/CStack.h"
  27. #include "../../lib/CConfigHandler.h"
  28. CBattleControlPanel::CBattleControlPanel(CBattleInterface * owner):
  29. owner(owner)
  30. {
  31. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  32. //preparing buttons and console
  33. bOptions = std::make_shared<CButton> (Point( 3, 5), "icm003.def", CGI->generaltexth->zelp[381], std::bind(&CBattleControlPanel::bOptionsf,this), SDLK_o);
  34. bSurrender = std::make_shared<CButton> (Point( 54, 5), "icm001.def", CGI->generaltexth->zelp[379], std::bind(&CBattleControlPanel::bSurrenderf,this), SDLK_s);
  35. bFlee = std::make_shared<CButton> (Point(105, 5), "icm002.def", CGI->generaltexth->zelp[380], std::bind(&CBattleControlPanel::bFleef,this), SDLK_r);
  36. bAutofight = std::make_shared<CButton> (Point(157, 5), "icm004.def", CGI->generaltexth->zelp[382], std::bind(&CBattleControlPanel::bAutofightf,this), SDLK_a);
  37. bSpell = std::make_shared<CButton> (Point(645, 5), "icm005.def", CGI->generaltexth->zelp[385], std::bind(&CBattleControlPanel::bSpellf,this), SDLK_c);
  38. bWait = std::make_shared<CButton> (Point(696, 5), "icm006.def", CGI->generaltexth->zelp[386], std::bind(&CBattleControlPanel::bWaitf,this), SDLK_w);
  39. bDefence = std::make_shared<CButton> (Point(747, 5), "icm007.def", CGI->generaltexth->zelp[387], std::bind(&CBattleControlPanel::bDefencef,this), SDLK_d);
  40. bConsoleUp = std::make_shared<CButton> (Point(624, 5), "ComSlide.def", std::make_pair("", ""), std::bind(&CBattleControlPanel::bConsoleUpf,this), SDLK_UP);
  41. bConsoleDown = std::make_shared<CButton>(Point(624, 24), "ComSlide.def", std::make_pair("", ""), std::bind(&CBattleControlPanel::bConsoleDownf,this), SDLK_DOWN);
  42. bDefence->assignedKeys.insert(SDLK_SPACE);
  43. bConsoleUp->setImageOrder(0, 1, 0, 0);
  44. bConsoleDown->setImageOrder(2, 3, 2, 2);
  45. console = std::make_shared<CBattleConsole>(Rect(211, 4, 406,38));
  46. GH.statusbar = console;
  47. if ( owner->tacticsMode )
  48. tacticPhaseStarted();
  49. else
  50. tacticPhaseEnded();
  51. }
  52. void CBattleControlPanel::tacticPhaseStarted()
  53. {
  54. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  55. btactNext = std::make_shared<CButton>(Point(213, 4), "icm011.def", std::make_pair("", ""), [&]() { bTacticNextStack();}, SDLK_SPACE);
  56. btactEnd = std::make_shared<CButton>(Point(419, 4), "icm012.def", std::make_pair("", ""), [&](){ bTacticPhaseEnd();}, SDLK_RETURN);
  57. menu = std::make_shared<CPicture>("COPLACBR.BMP", 0, 0);
  58. menu->colorize(owner->curInt->playerID);
  59. }
  60. void CBattleControlPanel::tacticPhaseEnded()
  61. {
  62. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  63. btactNext.reset();
  64. btactEnd.reset();
  65. menu = std::make_shared<CPicture>("CBAR.BMP", 0, 0);
  66. menu->colorize(owner->curInt->playerID);
  67. }
  68. void CBattleControlPanel::bOptionsf()
  69. {
  70. if (owner->actionsController->spellcastingModeActive())
  71. return;
  72. CCS->curh->changeGraphic(ECursor::ADVENTURE,0);
  73. Rect tempRect = genRect(431, 481, 160, 84);
  74. tempRect += pos.topLeft();
  75. GH.pushIntT<CBattleOptionsWindow>(tempRect, owner);
  76. }
  77. void CBattleControlPanel::bSurrenderf()
  78. {
  79. if (owner->actionsController->spellcastingModeActive())
  80. return;
  81. int cost = owner->curInt->cb->battleGetSurrenderCost();
  82. if(cost >= 0)
  83. {
  84. std::string enemyHeroName = owner->curInt->cb->battleGetEnemyHero().name;
  85. if(enemyHeroName.empty())
  86. {
  87. logGlobal->warn("Surrender performed without enemy hero, should not happen!");
  88. enemyHeroName = "#ENEMY#";
  89. }
  90. 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."
  91. owner->curInt->showYesNoDialog(surrenderMessage, [this](){ reallySurrender(); }, nullptr);
  92. }
  93. }
  94. void CBattleControlPanel::bFleef()
  95. {
  96. if (owner->actionsController->spellcastingModeActive())
  97. return;
  98. if ( owner->curInt->cb->battleCanFlee() )
  99. {
  100. CFunctionList<void()> ony = std::bind(&CBattleControlPanel::reallyFlee,this);
  101. owner->curInt->showYesNoDialog(CGI->generaltexth->allTexts[28], ony, nullptr); //Are you sure you want to retreat?
  102. }
  103. else
  104. {
  105. std::vector<std::shared_ptr<CComponent>> comps;
  106. std::string heroName;
  107. //calculating fleeing hero's name
  108. if (owner->attackingHeroInstance)
  109. if (owner->attackingHeroInstance->tempOwner == owner->curInt->cb->getMyColor())
  110. heroName = owner->attackingHeroInstance->name;
  111. if (owner->defendingHeroInstance)
  112. if (owner->defendingHeroInstance->tempOwner == owner->curInt->cb->getMyColor())
  113. heroName = owner->defendingHeroInstance->name;
  114. //calculating text
  115. auto txt = boost::format(CGI->generaltexth->allTexts[340]) % heroName; //The Shackles of War are present. %s can not retreat!
  116. //printing message
  117. owner->curInt->showInfoDialog(boost::to_string(txt), comps);
  118. }
  119. }
  120. void CBattleControlPanel::reallyFlee()
  121. {
  122. owner->giveCommand(EActionType::RETREAT);
  123. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  124. }
  125. void CBattleControlPanel::reallySurrender()
  126. {
  127. if (owner->curInt->cb->getResourceAmount(Res::GOLD) < owner->curInt->cb->battleGetSurrenderCost())
  128. {
  129. owner->curInt->showInfoDialog(CGI->generaltexth->allTexts[29]); //You don't have enough gold!
  130. }
  131. else
  132. {
  133. owner->giveCommand(EActionType::SURRENDER);
  134. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  135. }
  136. }
  137. void CBattleControlPanel::bAutofightf()
  138. {
  139. if (owner->actionsController->spellcastingModeActive())
  140. return;
  141. //Stop auto-fight mode
  142. if(owner->curInt->isAutoFightOn)
  143. {
  144. assert(owner->curInt->autofightingAI);
  145. owner->curInt->isAutoFightOn = false;
  146. logGlobal->trace("Stopping the autofight...");
  147. }
  148. else if(!owner->curInt->autofightingAI)
  149. {
  150. owner->curInt->isAutoFightOn = true;
  151. blockUI(true);
  152. auto ai = CDynLibHandler::getNewBattleAI(settings["server"]["friendlyAI"].String());
  153. ai->init(owner->curInt->env, owner->curInt->cb);
  154. ai->battleStart(owner->army1, owner->army2, int3(0,0,0), owner->attackingHeroInstance, owner->defendingHeroInstance, owner->curInt->cb->battleGetMySide());
  155. owner->curInt->autofightingAI = ai;
  156. owner->curInt->cb->registerBattleInterface(ai);
  157. owner->requestAutofightingAIToTakeAction();
  158. }
  159. }
  160. void CBattleControlPanel::bSpellf()
  161. {
  162. if (owner->actionsController->spellcastingModeActive())
  163. return;
  164. if (!owner->myTurn)
  165. return;
  166. auto myHero = owner->currentHero();
  167. if(!myHero)
  168. return;
  169. CCS->curh->changeGraphic(ECursor::ADVENTURE,0);
  170. ESpellCastProblem::ESpellCastProblem spellCastProblem = owner->curInt->cb->battleCanCastSpell(myHero, spells::Mode::HERO);
  171. if(spellCastProblem == ESpellCastProblem::OK)
  172. {
  173. GH.pushIntT<CSpellWindow>(myHero, owner->curInt.get());
  174. }
  175. else if (spellCastProblem == ESpellCastProblem::MAGIC_IS_BLOCKED)
  176. {
  177. //TODO: move to spell mechanics, add more information to spell cast problem
  178. //Handle Orb of Inhibition-like effects -> we want to display dialog with info, why casting is impossible
  179. auto blockingBonus = owner->currentHero()->getBonusLocalFirst(Selector::type()(Bonus::BLOCK_ALL_MAGIC));
  180. if (!blockingBonus)
  181. return;
  182. if (blockingBonus->source == Bonus::ARTIFACT)
  183. {
  184. const int32_t artID = blockingBonus->sid;
  185. //If we have artifact, put name of our hero. Otherwise assume it's the enemy.
  186. //TODO check who *really* is source of bonus
  187. std::string heroName = myHero->hasArt(artID) ? myHero->name : owner->enemyHero().name;
  188. //%s wields the %s, an ancient artifact which creates a p dead to all magic.
  189. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[683])
  190. % heroName % CGI->artifacts()->getByIndex(artID)->getName()));
  191. }
  192. }
  193. }
  194. void CBattleControlPanel::bWaitf()
  195. {
  196. if (owner->actionsController->spellcastingModeActive())
  197. return;
  198. if (owner->stacksController->getActiveStack() != nullptr)
  199. owner->giveCommand(EActionType::WAIT);
  200. }
  201. void CBattleControlPanel::bDefencef()
  202. {
  203. if (owner->actionsController->spellcastingModeActive())
  204. return;
  205. if (owner->stacksController->getActiveStack() != nullptr)
  206. owner->giveCommand(EActionType::DEFEND);
  207. }
  208. void CBattleControlPanel::bConsoleUpf()
  209. {
  210. if (owner->actionsController->spellcastingModeActive())
  211. return;
  212. console->scrollUp();
  213. }
  214. void CBattleControlPanel::bConsoleDownf()
  215. {
  216. if (owner->actionsController->spellcastingModeActive())
  217. return;
  218. console->scrollDown();
  219. }
  220. void CBattleControlPanel::bTacticNextStack()
  221. {
  222. owner->tacticNextStack(nullptr);
  223. }
  224. void CBattleControlPanel::bTacticPhaseEnd()
  225. {
  226. owner->tacticPhaseEnd();
  227. }
  228. void CBattleControlPanel::blockUI(bool on)
  229. {
  230. bool canCastSpells = false;
  231. auto hero = owner->curInt->cb->battleGetMyHero();
  232. if(hero)
  233. {
  234. ESpellCastProblem::ESpellCastProblem spellcastingProblem = owner->curInt->cb->battleCanCastSpell(hero, spells::Mode::HERO);
  235. //if magic is blocked, we leave button active, so the message can be displayed after button click
  236. canCastSpells = spellcastingProblem == ESpellCastProblem::OK || spellcastingProblem == ESpellCastProblem::MAGIC_IS_BLOCKED;
  237. }
  238. bool canWait = owner->stacksController->getActiveStack() ? !owner->stacksController->getActiveStack()->waitedThisTurn : false;
  239. bOptions->block(on);
  240. bFlee->block(on || !owner->curInt->cb->battleCanFlee());
  241. bSurrender->block(on || owner->curInt->cb->battleGetSurrenderCost() < 0);
  242. // block only if during enemy turn and auto-fight is off
  243. // otherwise - crash on accessing non-exisiting active stack
  244. bAutofight->block(!owner->curInt->isAutoFightOn && !owner->stacksController->getActiveStack());
  245. if (owner->tacticsMode && btactEnd && btactNext)
  246. {
  247. btactNext->block(on);
  248. btactEnd->block(on);
  249. }
  250. else
  251. {
  252. bConsoleUp->block(on);
  253. bConsoleDown->block(on);
  254. }
  255. bSpell->block(on || owner->tacticsMode || !canCastSpells);
  256. bWait->block(on || owner->tacticsMode || !canWait);
  257. bDefence->block(on || owner->tacticsMode);
  258. }