BattleWindow.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 "../CMessage.h"
  19. #include "../CPlayerInterface.h"
  20. #include "../CMusicHandler.h"
  21. #include "../gui/Canvas.h"
  22. #include "../gui/CCursorHandler.h"
  23. #include "../gui/CGuiHandler.h"
  24. #include "../windows/CSpellWindow.h"
  25. #include "../widgets/AdventureMapClasses.h"
  26. #include "../widgets/Buttons.h"
  27. #include "../widgets/Images.h"
  28. #include "../../CCallback.h"
  29. #include "../../lib/CGeneralTextHandler.h"
  30. #include "../../lib/mapObjects/CGHeroInstance.h"
  31. #include "../../lib/CStack.h"
  32. #include "../../lib/CConfigHandler.h"
  33. BattleWindow::BattleWindow(BattleInterface & owner):
  34. owner(owner)
  35. {
  36. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  37. pos.w = 800;
  38. pos.h = 600;
  39. pos = center();
  40. // create background for panel/ribbon at the bottom
  41. menuTactics = std::make_shared<CPicture>("COPLACBR.BMP", 0, 556);
  42. menuBattle = std::make_shared<CPicture>("CBAR.BMP", 0, 556);
  43. menuTactics->colorize(owner.curInt->playerID);
  44. menuBattle->colorize(owner.curInt->playerID);
  45. //preparing buttons and console
  46. bOptions = std::make_shared<CButton> (Point( 3, 5 + 556), "icm003.def", CGI->generaltexth->zelp[381], std::bind(&BattleWindow::bOptionsf,this), SDLK_o);
  47. bSurrender = std::make_shared<CButton> (Point( 54, 5 + 556), "icm001.def", CGI->generaltexth->zelp[379], std::bind(&BattleWindow::bSurrenderf,this), SDLK_s);
  48. bFlee = std::make_shared<CButton> (Point(105, 5 + 556), "icm002.def", CGI->generaltexth->zelp[380], std::bind(&BattleWindow::bFleef,this), SDLK_r);
  49. bAutofight = std::make_shared<CButton> (Point(158, 5 + 556), "icm004.def", CGI->generaltexth->zelp[382], std::bind(&BattleWindow::bAutofightf,this), SDLK_a);
  50. bSpell = std::make_shared<CButton> (Point(645, 5 + 556), "icm005.def", CGI->generaltexth->zelp[385], std::bind(&BattleWindow::bSpellf,this), SDLK_c);
  51. bWait = std::make_shared<CButton> (Point(696, 5 + 556), "icm006.def", CGI->generaltexth->zelp[386], std::bind(&BattleWindow::bWaitf,this), SDLK_w);
  52. bDefence = std::make_shared<CButton> (Point(747, 5 + 556), "icm007.def", CGI->generaltexth->zelp[387], std::bind(&BattleWindow::bDefencef,this), SDLK_d);
  53. bConsoleUp = std::make_shared<CButton> (Point(624, 5 + 556), "ComSlide.def", std::make_pair("", ""), std::bind(&BattleWindow::bConsoleUpf,this), SDLK_UP);
  54. bConsoleDown = std::make_shared<CButton>(Point(624, 24 + 556), "ComSlide.def", std::make_pair("", ""), std::bind(&BattleWindow::bConsoleDownf,this), SDLK_DOWN);
  55. bDefence->assignedKeys.insert(SDLK_SPACE);
  56. bConsoleUp->setImageOrder(0, 1, 0, 0);
  57. bConsoleDown->setImageOrder(2, 3, 2, 2);
  58. btactNext = std::make_shared<CButton>(Point(213, 4 + 556), "icm011.def", std::make_pair("", ""), [&]() { bTacticNextStack();}, SDLK_SPACE);
  59. btactEnd = std::make_shared<CButton>(Point(419, 4 + 556), "icm012.def", std::make_pair("", ""), [&](){ bTacticPhaseEnd();}, SDLK_RETURN);
  60. console = std::make_shared<BattleConsole>(menuBattle, Point(211, 4 + 556), Point(211, 4), Point(406,38));
  61. GH.statusbar = console;
  62. owner.console = console;
  63. owner.fieldController.reset( new BattleFieldController(owner));
  64. owner.fieldController->createHeroes();
  65. //create stack queue and adjust our own position
  66. bool embedQueue;
  67. std::string queueSize = settings["battle"]["queueSize"].String();
  68. if(queueSize == "auto")
  69. embedQueue = screen->h < 700;
  70. else
  71. embedQueue = screen->h < 700 || queueSize == "small";
  72. queue = std::make_shared<StackQueue>(embedQueue, owner);
  73. if(!embedQueue && settings["battle"]["showQueue"].Bool())
  74. {
  75. //re-center, taking into account stack queue position
  76. pos.y -= queue->pos.h;
  77. pos.h += queue->pos.h;
  78. pos = center();
  79. }
  80. if ( owner.tacticsMode )
  81. tacticPhaseStarted();
  82. else
  83. tacticPhaseEnded();
  84. addUsedEvents(RCLICK | KEYBOARD);
  85. }
  86. BattleWindow::~BattleWindow()
  87. {
  88. CPlayerInterface::battleInt = nullptr;
  89. }
  90. void BattleWindow::hideQueue()
  91. {
  92. Settings showQueue = settings.write["battle"]["showQueue"];
  93. showQueue->Bool() = false;
  94. queue->disable();
  95. if (!queue->embedded)
  96. {
  97. moveBy(Point(0, -queue->pos.h / 2));
  98. GH.totalRedraw();
  99. }
  100. }
  101. void BattleWindow::showQueue()
  102. {
  103. Settings showQueue = settings.write["battle"]["showQueue"];
  104. showQueue->Bool() = true;
  105. queue->enable();
  106. if (!queue->embedded)
  107. {
  108. moveBy(Point(0, +queue->pos.h / 2));
  109. GH.totalRedraw();
  110. }
  111. }
  112. void BattleWindow::updateQueue()
  113. {
  114. queue->update();
  115. }
  116. void BattleWindow::activate()
  117. {
  118. GH.statusbar = console;
  119. CIntObject::activate();
  120. LOCPLINT->cingconsole->activate();
  121. }
  122. void BattleWindow::deactivate()
  123. {
  124. CIntObject::deactivate();
  125. LOCPLINT->cingconsole->deactivate();
  126. }
  127. void BattleWindow::keyPressed(const SDL_KeyboardEvent & key)
  128. {
  129. if(key.keysym.sym == SDLK_q && key.state == SDL_PRESSED)
  130. {
  131. if(settings["battle"]["showQueue"].Bool()) //hide queue
  132. hideQueue();
  133. else
  134. showQueue();
  135. }
  136. else if(key.keysym.sym == SDLK_f && key.state == SDL_PRESSED)
  137. {
  138. owner.actionsController->enterCreatureCastingMode();
  139. }
  140. else if(key.keysym.sym == SDLK_ESCAPE)
  141. {
  142. if(owner.getAnimationCondition(EAnimationEvents::OPENING) == true)
  143. CCS->soundh->stopSound(owner.battleIntroSoundChannel);
  144. else
  145. owner.actionsController->endCastingSpell();
  146. }
  147. }
  148. void BattleWindow::clickRight(tribool down, bool previousState)
  149. {
  150. if (!down)
  151. owner.actionsController->endCastingSpell();
  152. }
  153. void BattleWindow::tacticPhaseStarted()
  154. {
  155. menuBattle->disable();
  156. console->disable();
  157. menuTactics->enable();
  158. btactNext->enable();
  159. btactEnd->enable();
  160. }
  161. void BattleWindow::tacticPhaseEnded()
  162. {
  163. menuBattle->enable();
  164. console->enable();
  165. menuTactics->disable();
  166. btactNext->disable();
  167. btactEnd->disable();
  168. }
  169. void BattleWindow::bOptionsf()
  170. {
  171. if (owner.actionsController->spellcastingModeActive())
  172. return;
  173. CCS->curh->set(Cursor::Map::POINTER);
  174. GH.pushIntT<BattleOptionsWindow>(owner);
  175. }
  176. void BattleWindow::bSurrenderf()
  177. {
  178. if (owner.actionsController->spellcastingModeActive())
  179. return;
  180. int cost = owner.curInt->cb->battleGetSurrenderCost();
  181. if(cost >= 0)
  182. {
  183. std::string enemyHeroName = owner.curInt->cb->battleGetEnemyHero().name;
  184. if(enemyHeroName.empty())
  185. {
  186. logGlobal->warn("Surrender performed without enemy hero, should not happen!");
  187. enemyHeroName = "#ENEMY#";
  188. }
  189. 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."
  190. owner.curInt->showYesNoDialog(surrenderMessage, [this](){ reallySurrender(); }, nullptr);
  191. }
  192. }
  193. void BattleWindow::bFleef()
  194. {
  195. if (owner.actionsController->spellcastingModeActive())
  196. return;
  197. if ( owner.curInt->cb->battleCanFlee() )
  198. {
  199. CFunctionList<void()> ony = std::bind(&BattleWindow::reallyFlee,this);
  200. owner.curInt->showYesNoDialog(CGI->generaltexth->allTexts[28], ony, nullptr); //Are you sure you want to retreat?
  201. }
  202. else
  203. {
  204. std::vector<std::shared_ptr<CComponent>> comps;
  205. std::string heroName;
  206. //calculating fleeing hero's name
  207. if (owner.attackingHeroInstance)
  208. if (owner.attackingHeroInstance->tempOwner == owner.curInt->cb->getMyColor())
  209. heroName = owner.attackingHeroInstance->name;
  210. if (owner.defendingHeroInstance)
  211. if (owner.defendingHeroInstance->tempOwner == owner.curInt->cb->getMyColor())
  212. heroName = owner.defendingHeroInstance->name;
  213. //calculating text
  214. auto txt = boost::format(CGI->generaltexth->allTexts[340]) % heroName; //The Shackles of War are present. %s can not retreat!
  215. //printing message
  216. owner.curInt->showInfoDialog(boost::to_string(txt), comps);
  217. }
  218. }
  219. void BattleWindow::reallyFlee()
  220. {
  221. owner.giveCommand(EActionType::RETREAT);
  222. CCS->curh->set(Cursor::Map::POINTER);
  223. }
  224. void BattleWindow::reallySurrender()
  225. {
  226. if (owner.curInt->cb->getResourceAmount(Res::GOLD) < owner.curInt->cb->battleGetSurrenderCost())
  227. {
  228. owner.curInt->showInfoDialog(CGI->generaltexth->allTexts[29]); //You don't have enough gold!
  229. }
  230. else
  231. {
  232. owner.giveCommand(EActionType::SURRENDER);
  233. CCS->curh->set(Cursor::Map::POINTER);
  234. }
  235. }
  236. void BattleWindow::bAutofightf()
  237. {
  238. if (owner.actionsController->spellcastingModeActive())
  239. return;
  240. //Stop auto-fight mode
  241. if(owner.curInt->isAutoFightOn)
  242. {
  243. assert(owner.curInt->autofightingAI);
  244. owner.curInt->isAutoFightOn = false;
  245. logGlobal->trace("Stopping the autofight...");
  246. }
  247. else if(!owner.curInt->autofightingAI)
  248. {
  249. owner.curInt->isAutoFightOn = true;
  250. blockUI(true);
  251. auto ai = CDynLibHandler::getNewBattleAI(settings["server"]["friendlyAI"].String());
  252. ai->initBattleInterface(owner.curInt->env, owner.curInt->cb);
  253. ai->battleStart(owner.army1, owner.army2, int3(0,0,0), owner.attackingHeroInstance, owner.defendingHeroInstance, owner.curInt->cb->battleGetMySide());
  254. owner.curInt->autofightingAI = ai;
  255. owner.curInt->cb->registerBattleInterface(ai);
  256. owner.requestAutofightingAIToTakeAction();
  257. }
  258. }
  259. void BattleWindow::bSpellf()
  260. {
  261. if (owner.actionsController->spellcastingModeActive())
  262. return;
  263. if (!owner.myTurn)
  264. return;
  265. auto myHero = owner.currentHero();
  266. if(!myHero)
  267. return;
  268. CCS->curh->set(Cursor::Map::POINTER);
  269. ESpellCastProblem::ESpellCastProblem spellCastProblem = owner.curInt->cb->battleCanCastSpell(myHero, spells::Mode::HERO);
  270. if(spellCastProblem == ESpellCastProblem::OK)
  271. {
  272. GH.pushIntT<CSpellWindow>(myHero, owner.curInt.get());
  273. }
  274. else if (spellCastProblem == ESpellCastProblem::MAGIC_IS_BLOCKED)
  275. {
  276. //TODO: move to spell mechanics, add more information to spell cast problem
  277. //Handle Orb of Inhibition-like effects -> we want to display dialog with info, why casting is impossible
  278. auto blockingBonus = owner.currentHero()->getBonusLocalFirst(Selector::type()(Bonus::BLOCK_ALL_MAGIC));
  279. if (!blockingBonus)
  280. return;
  281. if (blockingBonus->source == Bonus::ARTIFACT)
  282. {
  283. const auto artID = ArtifactID(blockingBonus->sid);
  284. //If we have artifact, put name of our hero. Otherwise assume it's the enemy.
  285. //TODO check who *really* is source of bonus
  286. std::string heroName = myHero->hasArt(artID) ? myHero->name : owner.enemyHero().name;
  287. //%s wields the %s, an ancient artifact which creates a p dead to all magic.
  288. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[683])
  289. % heroName % CGI->artifacts()->getByIndex(artID)->getName()));
  290. }
  291. }
  292. }
  293. void BattleWindow::bWaitf()
  294. {
  295. if (owner.actionsController->spellcastingModeActive())
  296. return;
  297. if (owner.stacksController->getActiveStack() != nullptr)
  298. owner.giveCommand(EActionType::WAIT);
  299. }
  300. void BattleWindow::bDefencef()
  301. {
  302. if (owner.actionsController->spellcastingModeActive())
  303. return;
  304. if (owner.stacksController->getActiveStack() != nullptr)
  305. owner.giveCommand(EActionType::DEFEND);
  306. }
  307. void BattleWindow::bConsoleUpf()
  308. {
  309. if (owner.actionsController->spellcastingModeActive())
  310. return;
  311. console->scrollUp();
  312. }
  313. void BattleWindow::bConsoleDownf()
  314. {
  315. if (owner.actionsController->spellcastingModeActive())
  316. return;
  317. console->scrollDown();
  318. }
  319. void BattleWindow::bTacticNextStack()
  320. {
  321. owner.tacticNextStack(nullptr);
  322. }
  323. void BattleWindow::bTacticPhaseEnd()
  324. {
  325. owner.tacticPhaseEnd();
  326. }
  327. void BattleWindow::blockUI(bool on)
  328. {
  329. bool canCastSpells = false;
  330. auto hero = owner.curInt->cb->battleGetMyHero();
  331. if(hero)
  332. {
  333. ESpellCastProblem::ESpellCastProblem spellcastingProblem = owner.curInt->cb->battleCanCastSpell(hero, spells::Mode::HERO);
  334. //if magic is blocked, we leave button active, so the message can be displayed after button click
  335. canCastSpells = spellcastingProblem == ESpellCastProblem::OK || spellcastingProblem == ESpellCastProblem::MAGIC_IS_BLOCKED;
  336. }
  337. bool canWait = owner.stacksController->getActiveStack() ? !owner.stacksController->getActiveStack()->waitedThisTurn : false;
  338. bOptions->block(on);
  339. bFlee->block(on || !owner.curInt->cb->battleCanFlee());
  340. bSurrender->block(on || owner.curInt->cb->battleGetSurrenderCost() < 0);
  341. // block only if during enemy turn and auto-fight is off
  342. // otherwise - crash on accessing non-exisiting active stack
  343. bAutofight->block(!owner.curInt->isAutoFightOn && !owner.stacksController->getActiveStack());
  344. if (owner.tacticsMode && btactEnd && btactNext)
  345. {
  346. btactNext->block(on);
  347. btactEnd->block(on);
  348. }
  349. else
  350. {
  351. bConsoleUp->block(on);
  352. bConsoleDown->block(on);
  353. }
  354. bSpell->block(on || owner.tacticsMode || !canCastSpells);
  355. bWait->block(on || owner.tacticsMode || !canWait);
  356. bDefence->block(on || owner.tacticsMode);
  357. }
  358. void BattleWindow::showAll(SDL_Surface *to)
  359. {
  360. CIntObject::showAll(to);
  361. if (screen->w != 800 || screen->h !=600)
  362. CMessage::drawBorder(owner.curInt->playerID, to, pos.w+28, pos.h+29, pos.x-14, pos.y-15);
  363. }
  364. void BattleWindow::show(SDL_Surface *to)
  365. {
  366. CIntObject::show(to);
  367. LOCPLINT->cingconsole->show(to);
  368. }