BattleControlPanel.cpp 11 KB

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