BattleWindow.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. CIntObject::activate();
  119. LOCPLINT->cingconsole->activate();
  120. }
  121. void BattleWindow::deactivate()
  122. {
  123. CIntObject::deactivate();
  124. LOCPLINT->cingconsole->deactivate();
  125. }
  126. void BattleWindow::keyPressed(const SDL_KeyboardEvent & key)
  127. {
  128. if(key.keysym.sym == SDLK_q && key.state == SDL_PRESSED)
  129. {
  130. if(settings["battle"]["showQueue"].Bool()) //hide queue
  131. hideQueue();
  132. else
  133. showQueue();
  134. }
  135. else if(key.keysym.sym == SDLK_f && key.state == SDL_PRESSED)
  136. {
  137. owner.actionsController->enterCreatureCastingMode();
  138. }
  139. else if(key.keysym.sym == SDLK_ESCAPE)
  140. {
  141. if(owner.getAnimationCondition(EAnimationEvents::OPENING) == true)
  142. CCS->soundh->stopSound(owner.battleIntroSoundChannel);
  143. else
  144. owner.actionsController->endCastingSpell();
  145. }
  146. }
  147. void BattleWindow::clickRight(tribool down, bool previousState)
  148. {
  149. if (!down)
  150. owner.actionsController->endCastingSpell();
  151. }
  152. void BattleWindow::tacticPhaseStarted()
  153. {
  154. menuBattle->disable();
  155. console->disable();
  156. menuTactics->enable();
  157. btactNext->enable();
  158. btactEnd->enable();
  159. }
  160. void BattleWindow::tacticPhaseEnded()
  161. {
  162. menuBattle->enable();
  163. console->enable();
  164. menuTactics->disable();
  165. btactNext->disable();
  166. btactEnd->disable();
  167. }
  168. void BattleWindow::bOptionsf()
  169. {
  170. if (owner.actionsController->spellcastingModeActive())
  171. return;
  172. CCS->curh->set(Cursor::Map::POINTER);
  173. GH.pushIntT<BattleOptionsWindow>(owner);
  174. }
  175. void BattleWindow::bSurrenderf()
  176. {
  177. if (owner.actionsController->spellcastingModeActive())
  178. return;
  179. int cost = owner.curInt->cb->battleGetSurrenderCost();
  180. if(cost >= 0)
  181. {
  182. std::string enemyHeroName = owner.curInt->cb->battleGetEnemyHero().name;
  183. if(enemyHeroName.empty())
  184. {
  185. logGlobal->warn("Surrender performed without enemy hero, should not happen!");
  186. enemyHeroName = "#ENEMY#";
  187. }
  188. 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."
  189. owner.curInt->showYesNoDialog(surrenderMessage, [this](){ reallySurrender(); }, nullptr);
  190. }
  191. }
  192. void BattleWindow::bFleef()
  193. {
  194. if (owner.actionsController->spellcastingModeActive())
  195. return;
  196. if ( owner.curInt->cb->battleCanFlee() )
  197. {
  198. CFunctionList<void()> ony = std::bind(&BattleWindow::reallyFlee,this);
  199. owner.curInt->showYesNoDialog(CGI->generaltexth->allTexts[28], ony, nullptr); //Are you sure you want to retreat?
  200. }
  201. else
  202. {
  203. std::vector<std::shared_ptr<CComponent>> comps;
  204. std::string heroName;
  205. //calculating fleeing hero's name
  206. if (owner.attackingHeroInstance)
  207. if (owner.attackingHeroInstance->tempOwner == owner.curInt->cb->getMyColor())
  208. heroName = owner.attackingHeroInstance->name;
  209. if (owner.defendingHeroInstance)
  210. if (owner.defendingHeroInstance->tempOwner == owner.curInt->cb->getMyColor())
  211. heroName = owner.defendingHeroInstance->name;
  212. //calculating text
  213. auto txt = boost::format(CGI->generaltexth->allTexts[340]) % heroName; //The Shackles of War are present. %s can not retreat!
  214. //printing message
  215. owner.curInt->showInfoDialog(boost::to_string(txt), comps);
  216. }
  217. }
  218. void BattleWindow::reallyFlee()
  219. {
  220. owner.giveCommand(EActionType::RETREAT);
  221. CCS->curh->set(Cursor::Map::POINTER);
  222. }
  223. void BattleWindow::reallySurrender()
  224. {
  225. if (owner.curInt->cb->getResourceAmount(Res::GOLD) < owner.curInt->cb->battleGetSurrenderCost())
  226. {
  227. owner.curInt->showInfoDialog(CGI->generaltexth->allTexts[29]); //You don't have enough gold!
  228. }
  229. else
  230. {
  231. owner.giveCommand(EActionType::SURRENDER);
  232. CCS->curh->set(Cursor::Map::POINTER);
  233. }
  234. }
  235. void BattleWindow::bAutofightf()
  236. {
  237. if (owner.actionsController->spellcastingModeActive())
  238. return;
  239. //Stop auto-fight mode
  240. if(owner.curInt->isAutoFightOn)
  241. {
  242. assert(owner.curInt->autofightingAI);
  243. owner.curInt->isAutoFightOn = false;
  244. logGlobal->trace("Stopping the autofight...");
  245. }
  246. else if(!owner.curInt->autofightingAI)
  247. {
  248. owner.curInt->isAutoFightOn = true;
  249. blockUI(true);
  250. auto ai = CDynLibHandler::getNewBattleAI(settings["server"]["friendlyAI"].String());
  251. ai->initBattleInterface(owner.curInt->env, owner.curInt->cb);
  252. ai->battleStart(owner.army1, owner.army2, int3(0,0,0), owner.attackingHeroInstance, owner.defendingHeroInstance, owner.curInt->cb->battleGetMySide());
  253. owner.curInt->autofightingAI = ai;
  254. owner.curInt->cb->registerBattleInterface(ai);
  255. owner.requestAutofightingAIToTakeAction();
  256. }
  257. }
  258. void BattleWindow::bSpellf()
  259. {
  260. if (owner.actionsController->spellcastingModeActive())
  261. return;
  262. if (!owner.myTurn)
  263. return;
  264. auto myHero = owner.currentHero();
  265. if(!myHero)
  266. return;
  267. CCS->curh->set(Cursor::Map::POINTER);
  268. ESpellCastProblem::ESpellCastProblem spellCastProblem = owner.curInt->cb->battleCanCastSpell(myHero, spells::Mode::HERO);
  269. if(spellCastProblem == ESpellCastProblem::OK)
  270. {
  271. GH.pushIntT<CSpellWindow>(myHero, owner.curInt.get());
  272. }
  273. else if (spellCastProblem == ESpellCastProblem::MAGIC_IS_BLOCKED)
  274. {
  275. //TODO: move to spell mechanics, add more information to spell cast problem
  276. //Handle Orb of Inhibition-like effects -> we want to display dialog with info, why casting is impossible
  277. auto blockingBonus = owner.currentHero()->getBonusLocalFirst(Selector::type()(Bonus::BLOCK_ALL_MAGIC));
  278. if (!blockingBonus)
  279. return;
  280. if (blockingBonus->source == Bonus::ARTIFACT)
  281. {
  282. const auto artID = ArtifactID(blockingBonus->sid);
  283. //If we have artifact, put name of our hero. Otherwise assume it's the enemy.
  284. //TODO check who *really* is source of bonus
  285. std::string heroName = myHero->hasArt(artID) ? myHero->name : owner.enemyHero().name;
  286. //%s wields the %s, an ancient artifact which creates a p dead to all magic.
  287. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[683])
  288. % heroName % CGI->artifacts()->getByIndex(artID)->getName()));
  289. }
  290. }
  291. }
  292. void BattleWindow::bWaitf()
  293. {
  294. if (owner.actionsController->spellcastingModeActive())
  295. return;
  296. if (owner.stacksController->getActiveStack() != nullptr)
  297. owner.giveCommand(EActionType::WAIT);
  298. }
  299. void BattleWindow::bDefencef()
  300. {
  301. if (owner.actionsController->spellcastingModeActive())
  302. return;
  303. if (owner.stacksController->getActiveStack() != nullptr)
  304. owner.giveCommand(EActionType::DEFEND);
  305. }
  306. void BattleWindow::bConsoleUpf()
  307. {
  308. if (owner.actionsController->spellcastingModeActive())
  309. return;
  310. console->scrollUp();
  311. }
  312. void BattleWindow::bConsoleDownf()
  313. {
  314. if (owner.actionsController->spellcastingModeActive())
  315. return;
  316. console->scrollDown();
  317. }
  318. void BattleWindow::bTacticNextStack()
  319. {
  320. owner.tacticNextStack(nullptr);
  321. }
  322. void BattleWindow::bTacticPhaseEnd()
  323. {
  324. owner.tacticPhaseEnd();
  325. }
  326. void BattleWindow::blockUI(bool on)
  327. {
  328. bool canCastSpells = false;
  329. auto hero = owner.curInt->cb->battleGetMyHero();
  330. if(hero)
  331. {
  332. ESpellCastProblem::ESpellCastProblem spellcastingProblem = owner.curInt->cb->battleCanCastSpell(hero, spells::Mode::HERO);
  333. //if magic is blocked, we leave button active, so the message can be displayed after button click
  334. canCastSpells = spellcastingProblem == ESpellCastProblem::OK || spellcastingProblem == ESpellCastProblem::MAGIC_IS_BLOCKED;
  335. }
  336. bool canWait = owner.stacksController->getActiveStack() ? !owner.stacksController->getActiveStack()->waitedThisTurn : false;
  337. bOptions->block(on);
  338. bFlee->block(on || !owner.curInt->cb->battleCanFlee());
  339. bSurrender->block(on || owner.curInt->cb->battleGetSurrenderCost() < 0);
  340. // block only if during enemy turn and auto-fight is off
  341. // otherwise - crash on accessing non-exisiting active stack
  342. bAutofight->block(!owner.curInt->isAutoFightOn && !owner.stacksController->getActiveStack());
  343. if (owner.tacticsMode && btactEnd && btactNext)
  344. {
  345. btactNext->block(on);
  346. btactEnd->block(on);
  347. }
  348. else
  349. {
  350. bConsoleUp->block(on);
  351. bConsoleDown->block(on);
  352. }
  353. bSpell->block(on || owner.tacticsMode || !canCastSpells);
  354. bWait->block(on || owner.tacticsMode || !canWait);
  355. bDefence->block(on || owner.tacticsMode);
  356. }
  357. void BattleWindow::showAll(SDL_Surface *to)
  358. {
  359. CIntObject::showAll(to);
  360. if (screen->w != 800 || screen->h !=600)
  361. CMessage::drawBorder(owner.curInt->playerID, to, pos.w+28, pos.h+29, pos.x-14, pos.y-15);
  362. }
  363. void BattleWindow::show(SDL_Surface *to)
  364. {
  365. CIntObject::show(to);
  366. LOCPLINT->cingconsole->show(to);
  367. }