BattleControlPanel.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 "../gui/CAnimation.h"
  21. #include "../windows/CSpellWindow.h"
  22. #include "../widgets/Buttons.h"
  23. #include "../widgets/Images.h"
  24. #include "../../CCallback.h"
  25. #include "../../lib/CGeneralTextHandler.h"
  26. #include "../../lib/mapObjects/CGHeroInstance.h"
  27. #include "../../lib/CStack.h"
  28. #include "../../lib/CConfigHandler.h"
  29. #include "../../lib/filesystem/ResourceID.h"
  30. BattleControlPanel::BattleControlPanel(BattleInterface & owner, const Point & position):
  31. owner(owner)
  32. {
  33. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  34. REGISTER_BUILDER("battleConsole", &BattleControlPanel::buildBattleConsole);
  35. pos += position;
  36. const JsonNode config(ResourceID("config/widgets/battleControlPanel.json"));
  37. addCallback("options", std::bind(&BattleControlPanel::bOptionsf, this));
  38. addCallback("surrender", std::bind(&BattleControlPanel::bSurrenderf, this));
  39. addCallback("flee", std::bind(&BattleControlPanel::bFleef, this));
  40. addCallback("autofight", std::bind(&BattleControlPanel::bAutofightf, this));
  41. addCallback("spellbook", std::bind(&BattleControlPanel::bSpellf, this));
  42. addCallback("wait", std::bind(&BattleControlPanel::bWaitf, this));
  43. addCallback("defence", std::bind(&BattleControlPanel::bDefencef, this));
  44. addCallback("consoleUp", std::bind(&BattleControlPanel::bConsoleUpf, this));
  45. addCallback("consoleDown", std::bind(&BattleControlPanel::bConsoleDownf, this));
  46. addCallback("tacticNext", std::bind(&BattleControlPanel::bTacticNextStack, this));
  47. addCallback("tacticEnd", std::bind(&BattleControlPanel::bTacticPhaseEnd, this));
  48. addCallback("alternativeAction", std::bind(&BattleControlPanel::bSwitchActionf, this));
  49. build(config);
  50. console = widget<BattleConsole>("console");
  51. GH.statusbar = console;
  52. if ( owner.tacticsMode )
  53. tacticPhaseStarted();
  54. else
  55. tacticPhaseEnded();
  56. }
  57. std::shared_ptr<BattleConsole> BattleControlPanel::buildBattleConsole(const JsonNode & config) const
  58. {
  59. return std::make_shared<BattleConsole>(readRect(config["rect"]));
  60. }
  61. void BattleControlPanel::show(SDL_Surface * to)
  62. {
  63. //show menu before all other elements to keep it in background
  64. if(auto w = widget<CPicture>("menu"))
  65. w->show(to);
  66. CIntObject::show(to);
  67. }
  68. void BattleControlPanel::showAll(SDL_Surface * to)
  69. {
  70. //show menu before all other elements to keep it in background
  71. if(auto w = widget<CPicture>("menu"))
  72. w->showAll(to);
  73. CIntObject::showAll(to);
  74. }
  75. void BattleControlPanel::tacticPhaseStarted()
  76. {
  77. build(variables["tacticItems"]);
  78. if(auto w = widget<CPicture>("menu"))
  79. {
  80. w->colorize(owner.curInt->playerID);
  81. w->recActions &= ~(SHOWALL | UPDATE);
  82. }
  83. }
  84. void BattleControlPanel::tacticPhaseEnded()
  85. {
  86. build(variables["battleItems"]);
  87. if(auto w = widget<CButton>("tacticNext"))
  88. w.reset();
  89. if(auto w = widget<CButton>("tacticEnd"))
  90. w.reset();
  91. if(auto w = widget<CPicture>("menu"))
  92. {
  93. w->colorize(owner.curInt->playerID);
  94. w->recActions &= ~(SHOWALL | UPDATE);
  95. }
  96. }
  97. void BattleControlPanel::bOptionsf()
  98. {
  99. if (owner.actionsController->spellcastingModeActive())
  100. return;
  101. CCS->curh->changeGraphic(ECursor::ADVENTURE,0);
  102. GH.pushIntT<BattleOptionsWindow>(owner);
  103. }
  104. void BattleControlPanel::bSurrenderf()
  105. {
  106. if (owner.actionsController->spellcastingModeActive())
  107. return;
  108. int cost = owner.curInt->cb->battleGetSurrenderCost();
  109. if(cost >= 0)
  110. {
  111. std::string enemyHeroName = owner.curInt->cb->battleGetEnemyHero().name;
  112. if(enemyHeroName.empty())
  113. {
  114. logGlobal->warn("Surrender performed without enemy hero, should not happen!");
  115. enemyHeroName = "#ENEMY#";
  116. }
  117. 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."
  118. owner.curInt->showYesNoDialog(surrenderMessage, [this](){ reallySurrender(); }, nullptr);
  119. }
  120. }
  121. void BattleControlPanel::bFleef()
  122. {
  123. if (owner.actionsController->spellcastingModeActive())
  124. return;
  125. if ( owner.curInt->cb->battleCanFlee() )
  126. {
  127. CFunctionList<void()> ony = std::bind(&BattleControlPanel::reallyFlee,this);
  128. owner.curInt->showYesNoDialog(CGI->generaltexth->allTexts[28], ony, nullptr); //Are you sure you want to retreat?
  129. }
  130. else
  131. {
  132. std::vector<std::shared_ptr<CComponent>> comps;
  133. std::string heroName;
  134. //calculating fleeing hero's name
  135. if (owner.attackingHeroInstance)
  136. if (owner.attackingHeroInstance->tempOwner == owner.curInt->cb->getMyColor())
  137. heroName = owner.attackingHeroInstance->name;
  138. if (owner.defendingHeroInstance)
  139. if (owner.defendingHeroInstance->tempOwner == owner.curInt->cb->getMyColor())
  140. heroName = owner.defendingHeroInstance->name;
  141. //calculating text
  142. auto txt = boost::format(CGI->generaltexth->allTexts[340]) % heroName; //The Shackles of War are present. %s can not retreat!
  143. //printing message
  144. owner.curInt->showInfoDialog(boost::to_string(txt), comps);
  145. }
  146. }
  147. void BattleControlPanel::reallyFlee()
  148. {
  149. owner.giveCommand(EActionType::RETREAT);
  150. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  151. }
  152. void BattleControlPanel::reallySurrender()
  153. {
  154. if (owner.curInt->cb->getResourceAmount(Res::GOLD) < owner.curInt->cb->battleGetSurrenderCost())
  155. {
  156. owner.curInt->showInfoDialog(CGI->generaltexth->allTexts[29]); //You don't have enough gold!
  157. }
  158. else
  159. {
  160. owner.giveCommand(EActionType::SURRENDER);
  161. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  162. }
  163. }
  164. void BattleControlPanel::showAlternativeActionIcon(PossiblePlayerBattleAction action)
  165. {
  166. auto w = widget<CButton>("alternativeAction");
  167. if(!w)
  168. return;
  169. std::string iconName = variables["actionIconDefault"].String();
  170. switch(action)
  171. {
  172. case PossiblePlayerBattleAction::ATTACK:
  173. iconName = variables["actionIconAttack"].String();
  174. break;
  175. case PossiblePlayerBattleAction::SHOOT:
  176. iconName = variables["actionIconShoot"].String();
  177. break;
  178. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  179. iconName = variables["actionIconSpell"].String();
  180. break;
  181. //TODO: figure out purpose of this icon
  182. //case PossiblePlayerBattleAction::???:
  183. //iconName = variables["actionIconWalk"].String();
  184. //break;
  185. case PossiblePlayerBattleAction::ATTACK_AND_RETURN:
  186. iconName = variables["actionIconReturn"].String();
  187. break;
  188. case PossiblePlayerBattleAction::WALK_AND_ATTACK:
  189. iconName = variables["actionIconNoReturn"].String();
  190. break;
  191. }
  192. auto anim = std::make_shared<CAnimation>(iconName);
  193. w->setImage(anim, false);
  194. }
  195. void BattleControlPanel::setAlternativeActions(const std::list<PossiblePlayerBattleAction> & actions)
  196. {
  197. alternativeActions = actions;
  198. defaultAction = PossiblePlayerBattleAction::INVALID;
  199. if(alternativeActions.size() > 1)
  200. defaultAction = alternativeActions.back();
  201. if(!alternativeActions.empty())
  202. showAlternativeActionIcon(alternativeActions.front());
  203. else
  204. showAlternativeActionIcon(defaultAction);
  205. }
  206. void BattleControlPanel::bAutofightf()
  207. {
  208. if (owner.actionsController->spellcastingModeActive())
  209. return;
  210. //Stop auto-fight mode
  211. if(owner.curInt->isAutoFightOn)
  212. {
  213. assert(owner.curInt->autofightingAI);
  214. owner.curInt->isAutoFightOn = false;
  215. logGlobal->trace("Stopping the autofight...");
  216. }
  217. else if(!owner.curInt->autofightingAI)
  218. {
  219. owner.curInt->isAutoFightOn = true;
  220. blockUI(true);
  221. auto ai = CDynLibHandler::getNewBattleAI(settings["server"]["friendlyAI"].String());
  222. ai->initBattleInterface(owner.curInt->env, owner.curInt->cb);
  223. ai->battleStart(owner.army1, owner.army2, int3(0,0,0), owner.attackingHeroInstance, owner.defendingHeroInstance, owner.curInt->cb->battleGetMySide());
  224. owner.curInt->autofightingAI = ai;
  225. owner.curInt->cb->registerBattleInterface(ai);
  226. owner.requestAutofightingAIToTakeAction();
  227. }
  228. }
  229. void BattleControlPanel::bSpellf()
  230. {
  231. if (owner.actionsController->spellcastingModeActive())
  232. return;
  233. if (!owner.myTurn)
  234. return;
  235. auto myHero = owner.currentHero();
  236. if(!myHero)
  237. return;
  238. CCS->curh->changeGraphic(ECursor::ADVENTURE,0);
  239. ESpellCastProblem::ESpellCastProblem spellCastProblem = owner.curInt->cb->battleCanCastSpell(myHero, spells::Mode::HERO);
  240. if(spellCastProblem == ESpellCastProblem::OK)
  241. {
  242. GH.pushIntT<CSpellWindow>(myHero, owner.curInt.get());
  243. }
  244. else if (spellCastProblem == ESpellCastProblem::MAGIC_IS_BLOCKED)
  245. {
  246. //TODO: move to spell mechanics, add more information to spell cast problem
  247. //Handle Orb of Inhibition-like effects -> we want to display dialog with info, why casting is impossible
  248. auto blockingBonus = owner.currentHero()->getBonusLocalFirst(Selector::type()(Bonus::BLOCK_ALL_MAGIC));
  249. if (!blockingBonus)
  250. return;
  251. if (blockingBonus->source == Bonus::ARTIFACT)
  252. {
  253. const auto artID = ArtifactID(blockingBonus->sid);
  254. //If we have artifact, put name of our hero. Otherwise assume it's the enemy.
  255. //TODO check who *really* is source of bonus
  256. std::string heroName = myHero->hasArt(artID) ? myHero->name : owner.enemyHero().name;
  257. //%s wields the %s, an ancient artifact which creates a p dead to all magic.
  258. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[683])
  259. % heroName % CGI->artifacts()->getByIndex(artID)->getName()));
  260. }
  261. }
  262. }
  263. void BattleControlPanel::bSwitchActionf()
  264. {
  265. if(alternativeActions.empty())
  266. return;
  267. if(alternativeActions.front() == defaultAction)
  268. {
  269. alternativeActions.push_back(alternativeActions.front());
  270. alternativeActions.pop_front();
  271. }
  272. auto actions = owner.actionsController->getPossibleActions();
  273. if(!actions.empty() && actions.front() == alternativeActions.front())
  274. {
  275. owner.actionsController->removePossibleAction(alternativeActions.front());
  276. showAlternativeActionIcon(defaultAction);
  277. }
  278. else
  279. {
  280. owner.actionsController->pushFrontPossibleAction(alternativeActions.front());
  281. showAlternativeActionIcon(alternativeActions.front());
  282. }
  283. alternativeActions.push_back(alternativeActions.front());
  284. alternativeActions.pop_front();
  285. }
  286. void BattleControlPanel::bWaitf()
  287. {
  288. if (owner.actionsController->spellcastingModeActive())
  289. return;
  290. if (owner.stacksController->getActiveStack() != nullptr)
  291. owner.giveCommand(EActionType::WAIT);
  292. }
  293. void BattleControlPanel::bDefencef()
  294. {
  295. if (owner.actionsController->spellcastingModeActive())
  296. return;
  297. if (owner.stacksController->getActiveStack() != nullptr)
  298. owner.giveCommand(EActionType::DEFEND);
  299. }
  300. void BattleControlPanel::bConsoleUpf()
  301. {
  302. if (owner.actionsController->spellcastingModeActive())
  303. return;
  304. console->scrollUp();
  305. }
  306. void BattleControlPanel::bConsoleDownf()
  307. {
  308. if (owner.actionsController->spellcastingModeActive())
  309. return;
  310. console->scrollDown();
  311. }
  312. void BattleControlPanel::bTacticNextStack()
  313. {
  314. owner.tacticNextStack(nullptr);
  315. }
  316. void BattleControlPanel::bTacticPhaseEnd()
  317. {
  318. owner.tacticPhaseEnd();
  319. }
  320. void BattleControlPanel::blockUI(bool on)
  321. {
  322. bool canCastSpells = false;
  323. auto hero = owner.curInt->cb->battleGetMyHero();
  324. if(hero)
  325. {
  326. ESpellCastProblem::ESpellCastProblem spellcastingProblem = owner.curInt->cb->battleCanCastSpell(hero, spells::Mode::HERO);
  327. //if magic is blocked, we leave button active, so the message can be displayed after button click
  328. canCastSpells = spellcastingProblem == ESpellCastProblem::OK || spellcastingProblem == ESpellCastProblem::MAGIC_IS_BLOCKED;
  329. }
  330. bool canWait = owner.stacksController->getActiveStack() ? !owner.stacksController->getActiveStack()->waitedThisTurn : false;
  331. if(auto w = widget<CButton>("options"))
  332. w->block(on);
  333. if(auto w = widget<CButton>("flee"))
  334. w->block(on || !owner.curInt->cb->battleCanFlee());
  335. if(auto w = widget<CButton>("surrender"))
  336. w->block(on || owner.curInt->cb->battleGetSurrenderCost() < 0);
  337. if(auto w = widget<CButton>("cast"))
  338. w->block(on || owner.tacticsMode || !canCastSpells);
  339. if(auto w = widget<CButton>("wait"))
  340. w->block(on || owner.tacticsMode || !canWait);
  341. if(auto w = widget<CButton>("defence"))
  342. w->block(on || owner.tacticsMode);
  343. if(auto w = widget<CButton>("alternativeAction"))
  344. w->block(on || owner.tacticsMode);
  345. // block only if during enemy turn and auto-fight is off
  346. // otherwise - crash on accessing non-exisiting active stack
  347. if(auto w = widget<CButton>("options"))
  348. w->block(!owner.curInt->isAutoFightOn && !owner.stacksController->getActiveStack());
  349. auto btactEnd = widget<CButton>("tacticEnd");
  350. auto btactNext = widget<CButton>("tacticNext");
  351. if(owner.tacticsMode && btactEnd && btactNext)
  352. {
  353. btactNext->block(on);
  354. btactEnd->block(on);
  355. }
  356. else
  357. {
  358. auto bConsoleUp = widget<CButton>("consoleUp");
  359. auto bConsoleDown = widget<CButton>("consoleDown");
  360. if(bConsoleUp && bConsoleDown)
  361. {
  362. bConsoleUp->block(on);
  363. bConsoleDown->block(on);
  364. }
  365. }
  366. }